pch_dma.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  1. /*
  2. * Topcliff PCH DMA controller driver
  3. * Copyright (c) 2010 Intel Corporation
  4. * Copyright (C) 2011 LAPIS Semiconductor Co., Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/dmaengine.h>
  16. #include <linux/dma-mapping.h>
  17. #include <linux/init.h>
  18. #include <linux/pci.h>
  19. #include <linux/slab.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/module.h>
  22. #include <linux/pch_dma.h>
  23. #include "dmaengine.h"
  24. #define DRV_NAME "pch-dma"
  25. #define DMA_CTL0_DISABLE 0x0
  26. #define DMA_CTL0_SG 0x1
  27. #define DMA_CTL0_ONESHOT 0x2
  28. #define DMA_CTL0_MODE_MASK_BITS 0x3
  29. #define DMA_CTL0_DIR_SHIFT_BITS 2
  30. #define DMA_CTL0_BITS_PER_CH 4
  31. #define DMA_CTL2_START_SHIFT_BITS 8
  32. #define DMA_CTL2_IRQ_ENABLE_MASK ((1UL << DMA_CTL2_START_SHIFT_BITS) - 1)
  33. #define DMA_STATUS_IDLE 0x0
  34. #define DMA_STATUS_DESC_READ 0x1
  35. #define DMA_STATUS_WAIT 0x2
  36. #define DMA_STATUS_ACCESS 0x3
  37. #define DMA_STATUS_BITS_PER_CH 2
  38. #define DMA_STATUS_MASK_BITS 0x3
  39. #define DMA_STATUS_SHIFT_BITS 16
  40. #define DMA_STATUS_IRQ(x) (0x1 << (x))
  41. #define DMA_STATUS0_ERR(x) (0x1 << ((x) + 8))
  42. #define DMA_STATUS2_ERR(x) (0x1 << (x))
  43. #define DMA_DESC_WIDTH_SHIFT_BITS 12
  44. #define DMA_DESC_WIDTH_1_BYTE (0x3 << DMA_DESC_WIDTH_SHIFT_BITS)
  45. #define DMA_DESC_WIDTH_2_BYTES (0x2 << DMA_DESC_WIDTH_SHIFT_BITS)
  46. #define DMA_DESC_WIDTH_4_BYTES (0x0 << DMA_DESC_WIDTH_SHIFT_BITS)
  47. #define DMA_DESC_MAX_COUNT_1_BYTE 0x3FF
  48. #define DMA_DESC_MAX_COUNT_2_BYTES 0x3FF
  49. #define DMA_DESC_MAX_COUNT_4_BYTES 0x7FF
  50. #define DMA_DESC_END_WITHOUT_IRQ 0x0
  51. #define DMA_DESC_END_WITH_IRQ 0x1
  52. #define DMA_DESC_FOLLOW_WITHOUT_IRQ 0x2
  53. #define DMA_DESC_FOLLOW_WITH_IRQ 0x3
  54. #define MAX_CHAN_NR 12
  55. #define DMA_MASK_CTL0_MODE 0x33333333
  56. #define DMA_MASK_CTL2_MODE 0x00003333
  57. static unsigned int init_nr_desc_per_channel = 64;
  58. module_param(init_nr_desc_per_channel, uint, 0644);
  59. MODULE_PARM_DESC(init_nr_desc_per_channel,
  60. "initial descriptors per channel (default: 64)");
  61. struct pch_dma_desc_regs {
  62. u32 dev_addr;
  63. u32 mem_addr;
  64. u32 size;
  65. u32 next;
  66. };
  67. struct pch_dma_regs {
  68. u32 dma_ctl0;
  69. u32 dma_ctl1;
  70. u32 dma_ctl2;
  71. u32 dma_ctl3;
  72. u32 dma_sts0;
  73. u32 dma_sts1;
  74. u32 dma_sts2;
  75. u32 reserved3;
  76. struct pch_dma_desc_regs desc[MAX_CHAN_NR];
  77. };
  78. struct pch_dma_desc {
  79. struct pch_dma_desc_regs regs;
  80. struct dma_async_tx_descriptor txd;
  81. struct list_head desc_node;
  82. struct list_head tx_list;
  83. };
  84. struct pch_dma_chan {
  85. struct dma_chan chan;
  86. void __iomem *membase;
  87. enum dma_transfer_direction dir;
  88. struct tasklet_struct tasklet;
  89. unsigned long err_status;
  90. spinlock_t lock;
  91. struct list_head active_list;
  92. struct list_head queue;
  93. struct list_head free_list;
  94. unsigned int descs_allocated;
  95. };
  96. #define PDC_DEV_ADDR 0x00
  97. #define PDC_MEM_ADDR 0x04
  98. #define PDC_SIZE 0x08
  99. #define PDC_NEXT 0x0C
  100. #define channel_readl(pdc, name) \
  101. readl((pdc)->membase + PDC_##name)
  102. #define channel_writel(pdc, name, val) \
  103. writel((val), (pdc)->membase + PDC_##name)
  104. struct pch_dma {
  105. struct dma_device dma;
  106. void __iomem *membase;
  107. struct pci_pool *pool;
  108. struct pch_dma_regs regs;
  109. struct pch_dma_desc_regs ch_regs[MAX_CHAN_NR];
  110. struct pch_dma_chan channels[MAX_CHAN_NR];
  111. };
  112. #define PCH_DMA_CTL0 0x00
  113. #define PCH_DMA_CTL1 0x04
  114. #define PCH_DMA_CTL2 0x08
  115. #define PCH_DMA_CTL3 0x0C
  116. #define PCH_DMA_STS0 0x10
  117. #define PCH_DMA_STS1 0x14
  118. #define PCH_DMA_STS2 0x18
  119. #define dma_readl(pd, name) \
  120. readl((pd)->membase + PCH_DMA_##name)
  121. #define dma_writel(pd, name, val) \
  122. writel((val), (pd)->membase + PCH_DMA_##name)
  123. static inline
  124. struct pch_dma_desc *to_pd_desc(struct dma_async_tx_descriptor *txd)
  125. {
  126. return container_of(txd, struct pch_dma_desc, txd);
  127. }
  128. static inline struct pch_dma_chan *to_pd_chan(struct dma_chan *chan)
  129. {
  130. return container_of(chan, struct pch_dma_chan, chan);
  131. }
  132. static inline struct pch_dma *to_pd(struct dma_device *ddev)
  133. {
  134. return container_of(ddev, struct pch_dma, dma);
  135. }
  136. static inline struct device *chan2dev(struct dma_chan *chan)
  137. {
  138. return &chan->dev->device;
  139. }
  140. static inline struct device *chan2parent(struct dma_chan *chan)
  141. {
  142. return chan->dev->device.parent;
  143. }
  144. static inline
  145. struct pch_dma_desc *pdc_first_active(struct pch_dma_chan *pd_chan)
  146. {
  147. return list_first_entry(&pd_chan->active_list,
  148. struct pch_dma_desc, desc_node);
  149. }
  150. static inline
  151. struct pch_dma_desc *pdc_first_queued(struct pch_dma_chan *pd_chan)
  152. {
  153. return list_first_entry(&pd_chan->queue,
  154. struct pch_dma_desc, desc_node);
  155. }
  156. static void pdc_enable_irq(struct dma_chan *chan, int enable)
  157. {
  158. struct pch_dma *pd = to_pd(chan->device);
  159. u32 val;
  160. int pos;
  161. if (chan->chan_id < 8)
  162. pos = chan->chan_id;
  163. else
  164. pos = chan->chan_id + 8;
  165. val = dma_readl(pd, CTL2);
  166. if (enable)
  167. val |= 0x1 << pos;
  168. else
  169. val &= ~(0x1 << pos);
  170. dma_writel(pd, CTL2, val);
  171. dev_dbg(chan2dev(chan), "pdc_enable_irq: chan %d -> %x\n",
  172. chan->chan_id, val);
  173. }
  174. static void pdc_set_dir(struct dma_chan *chan)
  175. {
  176. struct pch_dma_chan *pd_chan = to_pd_chan(chan);
  177. struct pch_dma *pd = to_pd(chan->device);
  178. u32 val;
  179. u32 mask_mode;
  180. u32 mask_ctl;
  181. if (chan->chan_id < 8) {
  182. val = dma_readl(pd, CTL0);
  183. mask_mode = DMA_CTL0_MODE_MASK_BITS <<
  184. (DMA_CTL0_BITS_PER_CH * chan->chan_id);
  185. mask_ctl = DMA_MASK_CTL0_MODE & ~(DMA_CTL0_MODE_MASK_BITS <<
  186. (DMA_CTL0_BITS_PER_CH * chan->chan_id));
  187. val &= mask_mode;
  188. if (pd_chan->dir == DMA_MEM_TO_DEV)
  189. val |= 0x1 << (DMA_CTL0_BITS_PER_CH * chan->chan_id +
  190. DMA_CTL0_DIR_SHIFT_BITS);
  191. else
  192. val &= ~(0x1 << (DMA_CTL0_BITS_PER_CH * chan->chan_id +
  193. DMA_CTL0_DIR_SHIFT_BITS));
  194. val |= mask_ctl;
  195. dma_writel(pd, CTL0, val);
  196. } else {
  197. int ch = chan->chan_id - 8; /* ch8-->0 ch9-->1 ... ch11->3 */
  198. val = dma_readl(pd, CTL3);
  199. mask_mode = DMA_CTL0_MODE_MASK_BITS <<
  200. (DMA_CTL0_BITS_PER_CH * ch);
  201. mask_ctl = DMA_MASK_CTL2_MODE & ~(DMA_CTL0_MODE_MASK_BITS <<
  202. (DMA_CTL0_BITS_PER_CH * ch));
  203. val &= mask_mode;
  204. if (pd_chan->dir == DMA_MEM_TO_DEV)
  205. val |= 0x1 << (DMA_CTL0_BITS_PER_CH * ch +
  206. DMA_CTL0_DIR_SHIFT_BITS);
  207. else
  208. val &= ~(0x1 << (DMA_CTL0_BITS_PER_CH * ch +
  209. DMA_CTL0_DIR_SHIFT_BITS));
  210. val |= mask_ctl;
  211. dma_writel(pd, CTL3, val);
  212. }
  213. dev_dbg(chan2dev(chan), "pdc_set_dir: chan %d -> %x\n",
  214. chan->chan_id, val);
  215. }
  216. static void pdc_set_mode(struct dma_chan *chan, u32 mode)
  217. {
  218. struct pch_dma *pd = to_pd(chan->device);
  219. u32 val;
  220. u32 mask_ctl;
  221. u32 mask_dir;
  222. if (chan->chan_id < 8) {
  223. mask_ctl = DMA_MASK_CTL0_MODE & ~(DMA_CTL0_MODE_MASK_BITS <<
  224. (DMA_CTL0_BITS_PER_CH * chan->chan_id));
  225. mask_dir = 1 << (DMA_CTL0_BITS_PER_CH * chan->chan_id +\
  226. DMA_CTL0_DIR_SHIFT_BITS);
  227. val = dma_readl(pd, CTL0);
  228. val &= mask_dir;
  229. val |= mode << (DMA_CTL0_BITS_PER_CH * chan->chan_id);
  230. val |= mask_ctl;
  231. dma_writel(pd, CTL0, val);
  232. } else {
  233. int ch = chan->chan_id - 8; /* ch8-->0 ch9-->1 ... ch11->3 */
  234. mask_ctl = DMA_MASK_CTL2_MODE & ~(DMA_CTL0_MODE_MASK_BITS <<
  235. (DMA_CTL0_BITS_PER_CH * ch));
  236. mask_dir = 1 << (DMA_CTL0_BITS_PER_CH * ch +\
  237. DMA_CTL0_DIR_SHIFT_BITS);
  238. val = dma_readl(pd, CTL3);
  239. val &= mask_dir;
  240. val |= mode << (DMA_CTL0_BITS_PER_CH * ch);
  241. val |= mask_ctl;
  242. dma_writel(pd, CTL3, val);
  243. }
  244. dev_dbg(chan2dev(chan), "pdc_set_mode: chan %d -> %x\n",
  245. chan->chan_id, val);
  246. }
  247. static u32 pdc_get_status0(struct pch_dma_chan *pd_chan)
  248. {
  249. struct pch_dma *pd = to_pd(pd_chan->chan.device);
  250. u32 val;
  251. val = dma_readl(pd, STS0);
  252. return DMA_STATUS_MASK_BITS & (val >> (DMA_STATUS_SHIFT_BITS +
  253. DMA_STATUS_BITS_PER_CH * pd_chan->chan.chan_id));
  254. }
  255. static u32 pdc_get_status2(struct pch_dma_chan *pd_chan)
  256. {
  257. struct pch_dma *pd = to_pd(pd_chan->chan.device);
  258. u32 val;
  259. val = dma_readl(pd, STS2);
  260. return DMA_STATUS_MASK_BITS & (val >> (DMA_STATUS_SHIFT_BITS +
  261. DMA_STATUS_BITS_PER_CH * (pd_chan->chan.chan_id - 8)));
  262. }
  263. static bool pdc_is_idle(struct pch_dma_chan *pd_chan)
  264. {
  265. u32 sts;
  266. if (pd_chan->chan.chan_id < 8)
  267. sts = pdc_get_status0(pd_chan);
  268. else
  269. sts = pdc_get_status2(pd_chan);
  270. if (sts == DMA_STATUS_IDLE)
  271. return true;
  272. else
  273. return false;
  274. }
  275. static void pdc_dostart(struct pch_dma_chan *pd_chan, struct pch_dma_desc* desc)
  276. {
  277. if (!pdc_is_idle(pd_chan)) {
  278. dev_err(chan2dev(&pd_chan->chan),
  279. "BUG: Attempt to start non-idle channel\n");
  280. return;
  281. }
  282. dev_dbg(chan2dev(&pd_chan->chan), "chan %d -> dev_addr: %x\n",
  283. pd_chan->chan.chan_id, desc->regs.dev_addr);
  284. dev_dbg(chan2dev(&pd_chan->chan), "chan %d -> mem_addr: %x\n",
  285. pd_chan->chan.chan_id, desc->regs.mem_addr);
  286. dev_dbg(chan2dev(&pd_chan->chan), "chan %d -> size: %x\n",
  287. pd_chan->chan.chan_id, desc->regs.size);
  288. dev_dbg(chan2dev(&pd_chan->chan), "chan %d -> next: %x\n",
  289. pd_chan->chan.chan_id, desc->regs.next);
  290. if (list_empty(&desc->tx_list)) {
  291. channel_writel(pd_chan, DEV_ADDR, desc->regs.dev_addr);
  292. channel_writel(pd_chan, MEM_ADDR, desc->regs.mem_addr);
  293. channel_writel(pd_chan, SIZE, desc->regs.size);
  294. channel_writel(pd_chan, NEXT, desc->regs.next);
  295. pdc_set_mode(&pd_chan->chan, DMA_CTL0_ONESHOT);
  296. } else {
  297. channel_writel(pd_chan, NEXT, desc->txd.phys);
  298. pdc_set_mode(&pd_chan->chan, DMA_CTL0_SG);
  299. }
  300. }
  301. static void pdc_chain_complete(struct pch_dma_chan *pd_chan,
  302. struct pch_dma_desc *desc)
  303. {
  304. struct dma_async_tx_descriptor *txd = &desc->txd;
  305. struct dmaengine_desc_callback cb;
  306. dmaengine_desc_get_callback(txd, &cb);
  307. list_splice_init(&desc->tx_list, &pd_chan->free_list);
  308. list_move(&desc->desc_node, &pd_chan->free_list);
  309. dmaengine_desc_callback_invoke(&cb, NULL);
  310. }
  311. static void pdc_complete_all(struct pch_dma_chan *pd_chan)
  312. {
  313. struct pch_dma_desc *desc, *_d;
  314. LIST_HEAD(list);
  315. BUG_ON(!pdc_is_idle(pd_chan));
  316. if (!list_empty(&pd_chan->queue))
  317. pdc_dostart(pd_chan, pdc_first_queued(pd_chan));
  318. list_splice_init(&pd_chan->active_list, &list);
  319. list_splice_init(&pd_chan->queue, &pd_chan->active_list);
  320. list_for_each_entry_safe(desc, _d, &list, desc_node)
  321. pdc_chain_complete(pd_chan, desc);
  322. }
  323. static void pdc_handle_error(struct pch_dma_chan *pd_chan)
  324. {
  325. struct pch_dma_desc *bad_desc;
  326. bad_desc = pdc_first_active(pd_chan);
  327. list_del(&bad_desc->desc_node);
  328. list_splice_init(&pd_chan->queue, pd_chan->active_list.prev);
  329. if (!list_empty(&pd_chan->active_list))
  330. pdc_dostart(pd_chan, pdc_first_active(pd_chan));
  331. dev_crit(chan2dev(&pd_chan->chan), "Bad descriptor submitted\n");
  332. dev_crit(chan2dev(&pd_chan->chan), "descriptor cookie: %d\n",
  333. bad_desc->txd.cookie);
  334. pdc_chain_complete(pd_chan, bad_desc);
  335. }
  336. static void pdc_advance_work(struct pch_dma_chan *pd_chan)
  337. {
  338. if (list_empty(&pd_chan->active_list) ||
  339. list_is_singular(&pd_chan->active_list)) {
  340. pdc_complete_all(pd_chan);
  341. } else {
  342. pdc_chain_complete(pd_chan, pdc_first_active(pd_chan));
  343. pdc_dostart(pd_chan, pdc_first_active(pd_chan));
  344. }
  345. }
  346. static dma_cookie_t pd_tx_submit(struct dma_async_tx_descriptor *txd)
  347. {
  348. struct pch_dma_desc *desc = to_pd_desc(txd);
  349. struct pch_dma_chan *pd_chan = to_pd_chan(txd->chan);
  350. spin_lock(&pd_chan->lock);
  351. if (list_empty(&pd_chan->active_list)) {
  352. list_add_tail(&desc->desc_node, &pd_chan->active_list);
  353. pdc_dostart(pd_chan, desc);
  354. } else {
  355. list_add_tail(&desc->desc_node, &pd_chan->queue);
  356. }
  357. spin_unlock(&pd_chan->lock);
  358. return 0;
  359. }
  360. static struct pch_dma_desc *pdc_alloc_desc(struct dma_chan *chan, gfp_t flags)
  361. {
  362. struct pch_dma_desc *desc = NULL;
  363. struct pch_dma *pd = to_pd(chan->device);
  364. dma_addr_t addr;
  365. desc = pci_pool_zalloc(pd->pool, flags, &addr);
  366. if (desc) {
  367. INIT_LIST_HEAD(&desc->tx_list);
  368. dma_async_tx_descriptor_init(&desc->txd, chan);
  369. desc->txd.tx_submit = pd_tx_submit;
  370. desc->txd.flags = DMA_CTRL_ACK;
  371. desc->txd.phys = addr;
  372. }
  373. return desc;
  374. }
  375. static struct pch_dma_desc *pdc_desc_get(struct pch_dma_chan *pd_chan)
  376. {
  377. struct pch_dma_desc *desc, *_d;
  378. struct pch_dma_desc *ret = NULL;
  379. int i = 0;
  380. spin_lock(&pd_chan->lock);
  381. list_for_each_entry_safe(desc, _d, &pd_chan->free_list, desc_node) {
  382. i++;
  383. if (async_tx_test_ack(&desc->txd)) {
  384. list_del(&desc->desc_node);
  385. ret = desc;
  386. break;
  387. }
  388. dev_dbg(chan2dev(&pd_chan->chan), "desc %p not ACKed\n", desc);
  389. }
  390. spin_unlock(&pd_chan->lock);
  391. dev_dbg(chan2dev(&pd_chan->chan), "scanned %d descriptors\n", i);
  392. if (!ret) {
  393. ret = pdc_alloc_desc(&pd_chan->chan, GFP_ATOMIC);
  394. if (ret) {
  395. spin_lock(&pd_chan->lock);
  396. pd_chan->descs_allocated++;
  397. spin_unlock(&pd_chan->lock);
  398. } else {
  399. dev_err(chan2dev(&pd_chan->chan),
  400. "failed to alloc desc\n");
  401. }
  402. }
  403. return ret;
  404. }
  405. static void pdc_desc_put(struct pch_dma_chan *pd_chan,
  406. struct pch_dma_desc *desc)
  407. {
  408. if (desc) {
  409. spin_lock(&pd_chan->lock);
  410. list_splice_init(&desc->tx_list, &pd_chan->free_list);
  411. list_add(&desc->desc_node, &pd_chan->free_list);
  412. spin_unlock(&pd_chan->lock);
  413. }
  414. }
  415. static int pd_alloc_chan_resources(struct dma_chan *chan)
  416. {
  417. struct pch_dma_chan *pd_chan = to_pd_chan(chan);
  418. struct pch_dma_desc *desc;
  419. LIST_HEAD(tmp_list);
  420. int i;
  421. if (!pdc_is_idle(pd_chan)) {
  422. dev_dbg(chan2dev(chan), "DMA channel not idle ?\n");
  423. return -EIO;
  424. }
  425. if (!list_empty(&pd_chan->free_list))
  426. return pd_chan->descs_allocated;
  427. for (i = 0; i < init_nr_desc_per_channel; i++) {
  428. desc = pdc_alloc_desc(chan, GFP_KERNEL);
  429. if (!desc) {
  430. dev_warn(chan2dev(chan),
  431. "Only allocated %d initial descriptors\n", i);
  432. break;
  433. }
  434. list_add_tail(&desc->desc_node, &tmp_list);
  435. }
  436. spin_lock_irq(&pd_chan->lock);
  437. list_splice(&tmp_list, &pd_chan->free_list);
  438. pd_chan->descs_allocated = i;
  439. dma_cookie_init(chan);
  440. spin_unlock_irq(&pd_chan->lock);
  441. pdc_enable_irq(chan, 1);
  442. return pd_chan->descs_allocated;
  443. }
  444. static void pd_free_chan_resources(struct dma_chan *chan)
  445. {
  446. struct pch_dma_chan *pd_chan = to_pd_chan(chan);
  447. struct pch_dma *pd = to_pd(chan->device);
  448. struct pch_dma_desc *desc, *_d;
  449. LIST_HEAD(tmp_list);
  450. BUG_ON(!pdc_is_idle(pd_chan));
  451. BUG_ON(!list_empty(&pd_chan->active_list));
  452. BUG_ON(!list_empty(&pd_chan->queue));
  453. spin_lock_irq(&pd_chan->lock);
  454. list_splice_init(&pd_chan->free_list, &tmp_list);
  455. pd_chan->descs_allocated = 0;
  456. spin_unlock_irq(&pd_chan->lock);
  457. list_for_each_entry_safe(desc, _d, &tmp_list, desc_node)
  458. pci_pool_free(pd->pool, desc, desc->txd.phys);
  459. pdc_enable_irq(chan, 0);
  460. }
  461. static enum dma_status pd_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
  462. struct dma_tx_state *txstate)
  463. {
  464. return dma_cookie_status(chan, cookie, txstate);
  465. }
  466. static void pd_issue_pending(struct dma_chan *chan)
  467. {
  468. struct pch_dma_chan *pd_chan = to_pd_chan(chan);
  469. if (pdc_is_idle(pd_chan)) {
  470. spin_lock(&pd_chan->lock);
  471. pdc_advance_work(pd_chan);
  472. spin_unlock(&pd_chan->lock);
  473. }
  474. }
  475. static struct dma_async_tx_descriptor *pd_prep_slave_sg(struct dma_chan *chan,
  476. struct scatterlist *sgl, unsigned int sg_len,
  477. enum dma_transfer_direction direction, unsigned long flags,
  478. void *context)
  479. {
  480. struct pch_dma_chan *pd_chan = to_pd_chan(chan);
  481. struct pch_dma_slave *pd_slave = chan->private;
  482. struct pch_dma_desc *first = NULL;
  483. struct pch_dma_desc *prev = NULL;
  484. struct pch_dma_desc *desc = NULL;
  485. struct scatterlist *sg;
  486. dma_addr_t reg;
  487. int i;
  488. if (unlikely(!sg_len)) {
  489. dev_info(chan2dev(chan), "prep_slave_sg: length is zero!\n");
  490. return NULL;
  491. }
  492. if (direction == DMA_DEV_TO_MEM)
  493. reg = pd_slave->rx_reg;
  494. else if (direction == DMA_MEM_TO_DEV)
  495. reg = pd_slave->tx_reg;
  496. else
  497. return NULL;
  498. pd_chan->dir = direction;
  499. pdc_set_dir(chan);
  500. for_each_sg(sgl, sg, sg_len, i) {
  501. desc = pdc_desc_get(pd_chan);
  502. if (!desc)
  503. goto err_desc_get;
  504. desc->regs.dev_addr = reg;
  505. desc->regs.mem_addr = sg_dma_address(sg);
  506. desc->regs.size = sg_dma_len(sg);
  507. desc->regs.next = DMA_DESC_FOLLOW_WITHOUT_IRQ;
  508. switch (pd_slave->width) {
  509. case PCH_DMA_WIDTH_1_BYTE:
  510. if (desc->regs.size > DMA_DESC_MAX_COUNT_1_BYTE)
  511. goto err_desc_get;
  512. desc->regs.size |= DMA_DESC_WIDTH_1_BYTE;
  513. break;
  514. case PCH_DMA_WIDTH_2_BYTES:
  515. if (desc->regs.size > DMA_DESC_MAX_COUNT_2_BYTES)
  516. goto err_desc_get;
  517. desc->regs.size |= DMA_DESC_WIDTH_2_BYTES;
  518. break;
  519. case PCH_DMA_WIDTH_4_BYTES:
  520. if (desc->regs.size > DMA_DESC_MAX_COUNT_4_BYTES)
  521. goto err_desc_get;
  522. desc->regs.size |= DMA_DESC_WIDTH_4_BYTES;
  523. break;
  524. default:
  525. goto err_desc_get;
  526. }
  527. if (!first) {
  528. first = desc;
  529. } else {
  530. prev->regs.next |= desc->txd.phys;
  531. list_add_tail(&desc->desc_node, &first->tx_list);
  532. }
  533. prev = desc;
  534. }
  535. if (flags & DMA_PREP_INTERRUPT)
  536. desc->regs.next = DMA_DESC_END_WITH_IRQ;
  537. else
  538. desc->regs.next = DMA_DESC_END_WITHOUT_IRQ;
  539. first->txd.cookie = -EBUSY;
  540. desc->txd.flags = flags;
  541. return &first->txd;
  542. err_desc_get:
  543. dev_err(chan2dev(chan), "failed to get desc or wrong parameters\n");
  544. pdc_desc_put(pd_chan, first);
  545. return NULL;
  546. }
  547. static int pd_device_terminate_all(struct dma_chan *chan)
  548. {
  549. struct pch_dma_chan *pd_chan = to_pd_chan(chan);
  550. struct pch_dma_desc *desc, *_d;
  551. LIST_HEAD(list);
  552. spin_lock_irq(&pd_chan->lock);
  553. pdc_set_mode(&pd_chan->chan, DMA_CTL0_DISABLE);
  554. list_splice_init(&pd_chan->active_list, &list);
  555. list_splice_init(&pd_chan->queue, &list);
  556. list_for_each_entry_safe(desc, _d, &list, desc_node)
  557. pdc_chain_complete(pd_chan, desc);
  558. spin_unlock_irq(&pd_chan->lock);
  559. return 0;
  560. }
  561. static void pdc_tasklet(unsigned long data)
  562. {
  563. struct pch_dma_chan *pd_chan = (struct pch_dma_chan *)data;
  564. unsigned long flags;
  565. if (!pdc_is_idle(pd_chan)) {
  566. dev_err(chan2dev(&pd_chan->chan),
  567. "BUG: handle non-idle channel in tasklet\n");
  568. return;
  569. }
  570. spin_lock_irqsave(&pd_chan->lock, flags);
  571. if (test_and_clear_bit(0, &pd_chan->err_status))
  572. pdc_handle_error(pd_chan);
  573. else
  574. pdc_advance_work(pd_chan);
  575. spin_unlock_irqrestore(&pd_chan->lock, flags);
  576. }
  577. static irqreturn_t pd_irq(int irq, void *devid)
  578. {
  579. struct pch_dma *pd = (struct pch_dma *)devid;
  580. struct pch_dma_chan *pd_chan;
  581. u32 sts0;
  582. u32 sts2;
  583. int i;
  584. int ret0 = IRQ_NONE;
  585. int ret2 = IRQ_NONE;
  586. sts0 = dma_readl(pd, STS0);
  587. sts2 = dma_readl(pd, STS2);
  588. dev_dbg(pd->dma.dev, "pd_irq sts0: %x\n", sts0);
  589. for (i = 0; i < pd->dma.chancnt; i++) {
  590. pd_chan = &pd->channels[i];
  591. if (i < 8) {
  592. if (sts0 & DMA_STATUS_IRQ(i)) {
  593. if (sts0 & DMA_STATUS0_ERR(i))
  594. set_bit(0, &pd_chan->err_status);
  595. tasklet_schedule(&pd_chan->tasklet);
  596. ret0 = IRQ_HANDLED;
  597. }
  598. } else {
  599. if (sts2 & DMA_STATUS_IRQ(i - 8)) {
  600. if (sts2 & DMA_STATUS2_ERR(i))
  601. set_bit(0, &pd_chan->err_status);
  602. tasklet_schedule(&pd_chan->tasklet);
  603. ret2 = IRQ_HANDLED;
  604. }
  605. }
  606. }
  607. /* clear interrupt bits in status register */
  608. if (ret0)
  609. dma_writel(pd, STS0, sts0);
  610. if (ret2)
  611. dma_writel(pd, STS2, sts2);
  612. return ret0 | ret2;
  613. }
  614. #ifdef CONFIG_PM
  615. static void pch_dma_save_regs(struct pch_dma *pd)
  616. {
  617. struct pch_dma_chan *pd_chan;
  618. struct dma_chan *chan, *_c;
  619. int i = 0;
  620. pd->regs.dma_ctl0 = dma_readl(pd, CTL0);
  621. pd->regs.dma_ctl1 = dma_readl(pd, CTL1);
  622. pd->regs.dma_ctl2 = dma_readl(pd, CTL2);
  623. pd->regs.dma_ctl3 = dma_readl(pd, CTL3);
  624. list_for_each_entry_safe(chan, _c, &pd->dma.channels, device_node) {
  625. pd_chan = to_pd_chan(chan);
  626. pd->ch_regs[i].dev_addr = channel_readl(pd_chan, DEV_ADDR);
  627. pd->ch_regs[i].mem_addr = channel_readl(pd_chan, MEM_ADDR);
  628. pd->ch_regs[i].size = channel_readl(pd_chan, SIZE);
  629. pd->ch_regs[i].next = channel_readl(pd_chan, NEXT);
  630. i++;
  631. }
  632. }
  633. static void pch_dma_restore_regs(struct pch_dma *pd)
  634. {
  635. struct pch_dma_chan *pd_chan;
  636. struct dma_chan *chan, *_c;
  637. int i = 0;
  638. dma_writel(pd, CTL0, pd->regs.dma_ctl0);
  639. dma_writel(pd, CTL1, pd->regs.dma_ctl1);
  640. dma_writel(pd, CTL2, pd->regs.dma_ctl2);
  641. dma_writel(pd, CTL3, pd->regs.dma_ctl3);
  642. list_for_each_entry_safe(chan, _c, &pd->dma.channels, device_node) {
  643. pd_chan = to_pd_chan(chan);
  644. channel_writel(pd_chan, DEV_ADDR, pd->ch_regs[i].dev_addr);
  645. channel_writel(pd_chan, MEM_ADDR, pd->ch_regs[i].mem_addr);
  646. channel_writel(pd_chan, SIZE, pd->ch_regs[i].size);
  647. channel_writel(pd_chan, NEXT, pd->ch_regs[i].next);
  648. i++;
  649. }
  650. }
  651. static int pch_dma_suspend(struct pci_dev *pdev, pm_message_t state)
  652. {
  653. struct pch_dma *pd = pci_get_drvdata(pdev);
  654. if (pd)
  655. pch_dma_save_regs(pd);
  656. pci_save_state(pdev);
  657. pci_disable_device(pdev);
  658. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  659. return 0;
  660. }
  661. static int pch_dma_resume(struct pci_dev *pdev)
  662. {
  663. struct pch_dma *pd = pci_get_drvdata(pdev);
  664. int err;
  665. pci_set_power_state(pdev, PCI_D0);
  666. pci_restore_state(pdev);
  667. err = pci_enable_device(pdev);
  668. if (err) {
  669. dev_dbg(&pdev->dev, "failed to enable device\n");
  670. return err;
  671. }
  672. if (pd)
  673. pch_dma_restore_regs(pd);
  674. return 0;
  675. }
  676. #endif
  677. static int pch_dma_probe(struct pci_dev *pdev,
  678. const struct pci_device_id *id)
  679. {
  680. struct pch_dma *pd;
  681. struct pch_dma_regs *regs;
  682. unsigned int nr_channels;
  683. int err;
  684. int i;
  685. nr_channels = id->driver_data;
  686. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  687. if (!pd)
  688. return -ENOMEM;
  689. pci_set_drvdata(pdev, pd);
  690. err = pci_enable_device(pdev);
  691. if (err) {
  692. dev_err(&pdev->dev, "Cannot enable PCI device\n");
  693. goto err_free_mem;
  694. }
  695. if (!(pci_resource_flags(pdev, 1) & IORESOURCE_MEM)) {
  696. dev_err(&pdev->dev, "Cannot find proper base address\n");
  697. err = -ENODEV;
  698. goto err_disable_pdev;
  699. }
  700. err = pci_request_regions(pdev, DRV_NAME);
  701. if (err) {
  702. dev_err(&pdev->dev, "Cannot obtain PCI resources\n");
  703. goto err_disable_pdev;
  704. }
  705. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  706. if (err) {
  707. dev_err(&pdev->dev, "Cannot set proper DMA config\n");
  708. goto err_free_res;
  709. }
  710. regs = pd->membase = pci_iomap(pdev, 1, 0);
  711. if (!pd->membase) {
  712. dev_err(&pdev->dev, "Cannot map MMIO registers\n");
  713. err = -ENOMEM;
  714. goto err_free_res;
  715. }
  716. pci_set_master(pdev);
  717. pd->dma.dev = &pdev->dev;
  718. err = request_irq(pdev->irq, pd_irq, IRQF_SHARED, DRV_NAME, pd);
  719. if (err) {
  720. dev_err(&pdev->dev, "Failed to request IRQ\n");
  721. goto err_iounmap;
  722. }
  723. pd->pool = pci_pool_create("pch_dma_desc_pool", pdev,
  724. sizeof(struct pch_dma_desc), 4, 0);
  725. if (!pd->pool) {
  726. dev_err(&pdev->dev, "Failed to alloc DMA descriptors\n");
  727. err = -ENOMEM;
  728. goto err_free_irq;
  729. }
  730. INIT_LIST_HEAD(&pd->dma.channels);
  731. for (i = 0; i < nr_channels; i++) {
  732. struct pch_dma_chan *pd_chan = &pd->channels[i];
  733. pd_chan->chan.device = &pd->dma;
  734. dma_cookie_init(&pd_chan->chan);
  735. pd_chan->membase = &regs->desc[i];
  736. spin_lock_init(&pd_chan->lock);
  737. INIT_LIST_HEAD(&pd_chan->active_list);
  738. INIT_LIST_HEAD(&pd_chan->queue);
  739. INIT_LIST_HEAD(&pd_chan->free_list);
  740. tasklet_init(&pd_chan->tasklet, pdc_tasklet,
  741. (unsigned long)pd_chan);
  742. list_add_tail(&pd_chan->chan.device_node, &pd->dma.channels);
  743. }
  744. dma_cap_zero(pd->dma.cap_mask);
  745. dma_cap_set(DMA_PRIVATE, pd->dma.cap_mask);
  746. dma_cap_set(DMA_SLAVE, pd->dma.cap_mask);
  747. pd->dma.device_alloc_chan_resources = pd_alloc_chan_resources;
  748. pd->dma.device_free_chan_resources = pd_free_chan_resources;
  749. pd->dma.device_tx_status = pd_tx_status;
  750. pd->dma.device_issue_pending = pd_issue_pending;
  751. pd->dma.device_prep_slave_sg = pd_prep_slave_sg;
  752. pd->dma.device_terminate_all = pd_device_terminate_all;
  753. err = dma_async_device_register(&pd->dma);
  754. if (err) {
  755. dev_err(&pdev->dev, "Failed to register DMA device\n");
  756. goto err_free_pool;
  757. }
  758. return 0;
  759. err_free_pool:
  760. pci_pool_destroy(pd->pool);
  761. err_free_irq:
  762. free_irq(pdev->irq, pd);
  763. err_iounmap:
  764. pci_iounmap(pdev, pd->membase);
  765. err_free_res:
  766. pci_release_regions(pdev);
  767. err_disable_pdev:
  768. pci_disable_device(pdev);
  769. err_free_mem:
  770. kfree(pd);
  771. return err;
  772. }
  773. static void pch_dma_remove(struct pci_dev *pdev)
  774. {
  775. struct pch_dma *pd = pci_get_drvdata(pdev);
  776. struct pch_dma_chan *pd_chan;
  777. struct dma_chan *chan, *_c;
  778. if (pd) {
  779. dma_async_device_unregister(&pd->dma);
  780. free_irq(pdev->irq, pd);
  781. list_for_each_entry_safe(chan, _c, &pd->dma.channels,
  782. device_node) {
  783. pd_chan = to_pd_chan(chan);
  784. tasklet_kill(&pd_chan->tasklet);
  785. }
  786. pci_pool_destroy(pd->pool);
  787. pci_iounmap(pdev, pd->membase);
  788. pci_release_regions(pdev);
  789. pci_disable_device(pdev);
  790. kfree(pd);
  791. }
  792. }
  793. /* PCI Device ID of DMA device */
  794. #define PCI_VENDOR_ID_ROHM 0x10DB
  795. #define PCI_DEVICE_ID_EG20T_PCH_DMA_8CH 0x8810
  796. #define PCI_DEVICE_ID_EG20T_PCH_DMA_4CH 0x8815
  797. #define PCI_DEVICE_ID_ML7213_DMA1_8CH 0x8026
  798. #define PCI_DEVICE_ID_ML7213_DMA2_8CH 0x802B
  799. #define PCI_DEVICE_ID_ML7213_DMA3_4CH 0x8034
  800. #define PCI_DEVICE_ID_ML7213_DMA4_12CH 0x8032
  801. #define PCI_DEVICE_ID_ML7223_DMA1_4CH 0x800B
  802. #define PCI_DEVICE_ID_ML7223_DMA2_4CH 0x800E
  803. #define PCI_DEVICE_ID_ML7223_DMA3_4CH 0x8017
  804. #define PCI_DEVICE_ID_ML7223_DMA4_4CH 0x803B
  805. #define PCI_DEVICE_ID_ML7831_DMA1_8CH 0x8810
  806. #define PCI_DEVICE_ID_ML7831_DMA2_4CH 0x8815
  807. static const struct pci_device_id pch_dma_id_table[] = {
  808. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_EG20T_PCH_DMA_8CH), 8 },
  809. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_EG20T_PCH_DMA_4CH), 4 },
  810. { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7213_DMA1_8CH), 8}, /* UART Video */
  811. { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7213_DMA2_8CH), 8}, /* PCMIF SPI */
  812. { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7213_DMA3_4CH), 4}, /* FPGA */
  813. { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7213_DMA4_12CH), 12}, /* I2S */
  814. { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7223_DMA1_4CH), 4}, /* UART */
  815. { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7223_DMA2_4CH), 4}, /* Video SPI */
  816. { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7223_DMA3_4CH), 4}, /* Security */
  817. { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7223_DMA4_4CH), 4}, /* FPGA */
  818. { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7831_DMA1_8CH), 8}, /* UART */
  819. { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7831_DMA2_4CH), 4}, /* SPI */
  820. { 0, },
  821. };
  822. static struct pci_driver pch_dma_driver = {
  823. .name = DRV_NAME,
  824. .id_table = pch_dma_id_table,
  825. .probe = pch_dma_probe,
  826. .remove = pch_dma_remove,
  827. #ifdef CONFIG_PM
  828. .suspend = pch_dma_suspend,
  829. .resume = pch_dma_resume,
  830. #endif
  831. };
  832. module_pci_driver(pch_dma_driver);
  833. MODULE_DESCRIPTION("Intel EG20T PCH / LAPIS Semicon ML7213/ML7223/ML7831 IOH "
  834. "DMA controller driver");
  835. MODULE_AUTHOR("Yong Wang <yong.y.wang@intel.com>");
  836. MODULE_LICENSE("GPL v2");
  837. MODULE_DEVICE_TABLE(pci, pch_dma_id_table);