txx9dmac.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  1. /*
  2. * Driver for the TXx9 SoC DMA Controller
  3. *
  4. * Copyright (C) 2009 Atsushi Nemoto
  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. #include <linux/dma-mapping.h>
  11. #include <linux/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/io.h>
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/slab.h>
  17. #include <linux/scatterlist.h>
  18. #include "dmaengine.h"
  19. #include "txx9dmac.h"
  20. static struct txx9dmac_chan *to_txx9dmac_chan(struct dma_chan *chan)
  21. {
  22. return container_of(chan, struct txx9dmac_chan, chan);
  23. }
  24. static struct txx9dmac_cregs __iomem *__dma_regs(const struct txx9dmac_chan *dc)
  25. {
  26. return dc->ch_regs;
  27. }
  28. static struct txx9dmac_cregs32 __iomem *__dma_regs32(
  29. const struct txx9dmac_chan *dc)
  30. {
  31. return dc->ch_regs;
  32. }
  33. #define channel64_readq(dc, name) \
  34. __raw_readq(&(__dma_regs(dc)->name))
  35. #define channel64_writeq(dc, name, val) \
  36. __raw_writeq((val), &(__dma_regs(dc)->name))
  37. #define channel64_readl(dc, name) \
  38. __raw_readl(&(__dma_regs(dc)->name))
  39. #define channel64_writel(dc, name, val) \
  40. __raw_writel((val), &(__dma_regs(dc)->name))
  41. #define channel32_readl(dc, name) \
  42. __raw_readl(&(__dma_regs32(dc)->name))
  43. #define channel32_writel(dc, name, val) \
  44. __raw_writel((val), &(__dma_regs32(dc)->name))
  45. #define channel_readq(dc, name) channel64_readq(dc, name)
  46. #define channel_writeq(dc, name, val) channel64_writeq(dc, name, val)
  47. #define channel_readl(dc, name) \
  48. (is_dmac64(dc) ? \
  49. channel64_readl(dc, name) : channel32_readl(dc, name))
  50. #define channel_writel(dc, name, val) \
  51. (is_dmac64(dc) ? \
  52. channel64_writel(dc, name, val) : channel32_writel(dc, name, val))
  53. static dma_addr_t channel64_read_CHAR(const struct txx9dmac_chan *dc)
  54. {
  55. if (sizeof(__dma_regs(dc)->CHAR) == sizeof(u64))
  56. return channel64_readq(dc, CHAR);
  57. else
  58. return channel64_readl(dc, CHAR);
  59. }
  60. static void channel64_write_CHAR(const struct txx9dmac_chan *dc, dma_addr_t val)
  61. {
  62. if (sizeof(__dma_regs(dc)->CHAR) == sizeof(u64))
  63. channel64_writeq(dc, CHAR, val);
  64. else
  65. channel64_writel(dc, CHAR, val);
  66. }
  67. static void channel64_clear_CHAR(const struct txx9dmac_chan *dc)
  68. {
  69. #if defined(CONFIG_32BIT) && !defined(CONFIG_PHYS_ADDR_T_64BIT)
  70. channel64_writel(dc, CHAR, 0);
  71. channel64_writel(dc, __pad_CHAR, 0);
  72. #else
  73. channel64_writeq(dc, CHAR, 0);
  74. #endif
  75. }
  76. static dma_addr_t channel_read_CHAR(const struct txx9dmac_chan *dc)
  77. {
  78. if (is_dmac64(dc))
  79. return channel64_read_CHAR(dc);
  80. else
  81. return channel32_readl(dc, CHAR);
  82. }
  83. static void channel_write_CHAR(const struct txx9dmac_chan *dc, dma_addr_t val)
  84. {
  85. if (is_dmac64(dc))
  86. channel64_write_CHAR(dc, val);
  87. else
  88. channel32_writel(dc, CHAR, val);
  89. }
  90. static struct txx9dmac_regs __iomem *__txx9dmac_regs(
  91. const struct txx9dmac_dev *ddev)
  92. {
  93. return ddev->regs;
  94. }
  95. static struct txx9dmac_regs32 __iomem *__txx9dmac_regs32(
  96. const struct txx9dmac_dev *ddev)
  97. {
  98. return ddev->regs;
  99. }
  100. #define dma64_readl(ddev, name) \
  101. __raw_readl(&(__txx9dmac_regs(ddev)->name))
  102. #define dma64_writel(ddev, name, val) \
  103. __raw_writel((val), &(__txx9dmac_regs(ddev)->name))
  104. #define dma32_readl(ddev, name) \
  105. __raw_readl(&(__txx9dmac_regs32(ddev)->name))
  106. #define dma32_writel(ddev, name, val) \
  107. __raw_writel((val), &(__txx9dmac_regs32(ddev)->name))
  108. #define dma_readl(ddev, name) \
  109. (__is_dmac64(ddev) ? \
  110. dma64_readl(ddev, name) : dma32_readl(ddev, name))
  111. #define dma_writel(ddev, name, val) \
  112. (__is_dmac64(ddev) ? \
  113. dma64_writel(ddev, name, val) : dma32_writel(ddev, name, val))
  114. static struct device *chan2dev(struct dma_chan *chan)
  115. {
  116. return &chan->dev->device;
  117. }
  118. static struct device *chan2parent(struct dma_chan *chan)
  119. {
  120. return chan->dev->device.parent;
  121. }
  122. static struct txx9dmac_desc *
  123. txd_to_txx9dmac_desc(struct dma_async_tx_descriptor *txd)
  124. {
  125. return container_of(txd, struct txx9dmac_desc, txd);
  126. }
  127. static dma_addr_t desc_read_CHAR(const struct txx9dmac_chan *dc,
  128. const struct txx9dmac_desc *desc)
  129. {
  130. return is_dmac64(dc) ? desc->hwdesc.CHAR : desc->hwdesc32.CHAR;
  131. }
  132. static void desc_write_CHAR(const struct txx9dmac_chan *dc,
  133. struct txx9dmac_desc *desc, dma_addr_t val)
  134. {
  135. if (is_dmac64(dc))
  136. desc->hwdesc.CHAR = val;
  137. else
  138. desc->hwdesc32.CHAR = val;
  139. }
  140. #define TXX9_DMA_MAX_COUNT 0x04000000
  141. #define TXX9_DMA_INITIAL_DESC_COUNT 64
  142. static struct txx9dmac_desc *txx9dmac_first_active(struct txx9dmac_chan *dc)
  143. {
  144. return list_entry(dc->active_list.next,
  145. struct txx9dmac_desc, desc_node);
  146. }
  147. static struct txx9dmac_desc *txx9dmac_last_active(struct txx9dmac_chan *dc)
  148. {
  149. return list_entry(dc->active_list.prev,
  150. struct txx9dmac_desc, desc_node);
  151. }
  152. static struct txx9dmac_desc *txx9dmac_first_queued(struct txx9dmac_chan *dc)
  153. {
  154. return list_entry(dc->queue.next, struct txx9dmac_desc, desc_node);
  155. }
  156. static struct txx9dmac_desc *txx9dmac_last_child(struct txx9dmac_desc *desc)
  157. {
  158. if (!list_empty(&desc->tx_list))
  159. desc = list_entry(desc->tx_list.prev, typeof(*desc), desc_node);
  160. return desc;
  161. }
  162. static dma_cookie_t txx9dmac_tx_submit(struct dma_async_tx_descriptor *tx);
  163. static struct txx9dmac_desc *txx9dmac_desc_alloc(struct txx9dmac_chan *dc,
  164. gfp_t flags)
  165. {
  166. struct txx9dmac_dev *ddev = dc->ddev;
  167. struct txx9dmac_desc *desc;
  168. desc = kzalloc(sizeof(*desc), flags);
  169. if (!desc)
  170. return NULL;
  171. INIT_LIST_HEAD(&desc->tx_list);
  172. dma_async_tx_descriptor_init(&desc->txd, &dc->chan);
  173. desc->txd.tx_submit = txx9dmac_tx_submit;
  174. /* txd.flags will be overwritten in prep funcs */
  175. desc->txd.flags = DMA_CTRL_ACK;
  176. desc->txd.phys = dma_map_single(chan2parent(&dc->chan), &desc->hwdesc,
  177. ddev->descsize, DMA_TO_DEVICE);
  178. return desc;
  179. }
  180. static struct txx9dmac_desc *txx9dmac_desc_get(struct txx9dmac_chan *dc)
  181. {
  182. struct txx9dmac_desc *desc, *_desc;
  183. struct txx9dmac_desc *ret = NULL;
  184. unsigned int i = 0;
  185. spin_lock_bh(&dc->lock);
  186. list_for_each_entry_safe(desc, _desc, &dc->free_list, desc_node) {
  187. if (async_tx_test_ack(&desc->txd)) {
  188. list_del(&desc->desc_node);
  189. ret = desc;
  190. break;
  191. }
  192. dev_dbg(chan2dev(&dc->chan), "desc %p not ACKed\n", desc);
  193. i++;
  194. }
  195. spin_unlock_bh(&dc->lock);
  196. dev_vdbg(chan2dev(&dc->chan), "scanned %u descriptors on freelist\n",
  197. i);
  198. if (!ret) {
  199. ret = txx9dmac_desc_alloc(dc, GFP_ATOMIC);
  200. if (ret) {
  201. spin_lock_bh(&dc->lock);
  202. dc->descs_allocated++;
  203. spin_unlock_bh(&dc->lock);
  204. } else
  205. dev_err(chan2dev(&dc->chan),
  206. "not enough descriptors available\n");
  207. }
  208. return ret;
  209. }
  210. static void txx9dmac_sync_desc_for_cpu(struct txx9dmac_chan *dc,
  211. struct txx9dmac_desc *desc)
  212. {
  213. struct txx9dmac_dev *ddev = dc->ddev;
  214. struct txx9dmac_desc *child;
  215. list_for_each_entry(child, &desc->tx_list, desc_node)
  216. dma_sync_single_for_cpu(chan2parent(&dc->chan),
  217. child->txd.phys, ddev->descsize,
  218. DMA_TO_DEVICE);
  219. dma_sync_single_for_cpu(chan2parent(&dc->chan),
  220. desc->txd.phys, ddev->descsize,
  221. DMA_TO_DEVICE);
  222. }
  223. /*
  224. * Move a descriptor, including any children, to the free list.
  225. * `desc' must not be on any lists.
  226. */
  227. static void txx9dmac_desc_put(struct txx9dmac_chan *dc,
  228. struct txx9dmac_desc *desc)
  229. {
  230. if (desc) {
  231. struct txx9dmac_desc *child;
  232. txx9dmac_sync_desc_for_cpu(dc, desc);
  233. spin_lock_bh(&dc->lock);
  234. list_for_each_entry(child, &desc->tx_list, desc_node)
  235. dev_vdbg(chan2dev(&dc->chan),
  236. "moving child desc %p to freelist\n",
  237. child);
  238. list_splice_init(&desc->tx_list, &dc->free_list);
  239. dev_vdbg(chan2dev(&dc->chan), "moving desc %p to freelist\n",
  240. desc);
  241. list_add(&desc->desc_node, &dc->free_list);
  242. spin_unlock_bh(&dc->lock);
  243. }
  244. }
  245. /*----------------------------------------------------------------------*/
  246. static void txx9dmac_dump_regs(struct txx9dmac_chan *dc)
  247. {
  248. if (is_dmac64(dc))
  249. dev_err(chan2dev(&dc->chan),
  250. " CHAR: %#llx SAR: %#llx DAR: %#llx CNTR: %#x"
  251. " SAIR: %#x DAIR: %#x CCR: %#x CSR: %#x\n",
  252. (u64)channel64_read_CHAR(dc),
  253. channel64_readq(dc, SAR),
  254. channel64_readq(dc, DAR),
  255. channel64_readl(dc, CNTR),
  256. channel64_readl(dc, SAIR),
  257. channel64_readl(dc, DAIR),
  258. channel64_readl(dc, CCR),
  259. channel64_readl(dc, CSR));
  260. else
  261. dev_err(chan2dev(&dc->chan),
  262. " CHAR: %#x SAR: %#x DAR: %#x CNTR: %#x"
  263. " SAIR: %#x DAIR: %#x CCR: %#x CSR: %#x\n",
  264. channel32_readl(dc, CHAR),
  265. channel32_readl(dc, SAR),
  266. channel32_readl(dc, DAR),
  267. channel32_readl(dc, CNTR),
  268. channel32_readl(dc, SAIR),
  269. channel32_readl(dc, DAIR),
  270. channel32_readl(dc, CCR),
  271. channel32_readl(dc, CSR));
  272. }
  273. static void txx9dmac_reset_chan(struct txx9dmac_chan *dc)
  274. {
  275. channel_writel(dc, CCR, TXX9_DMA_CCR_CHRST);
  276. if (is_dmac64(dc)) {
  277. channel64_clear_CHAR(dc);
  278. channel_writeq(dc, SAR, 0);
  279. channel_writeq(dc, DAR, 0);
  280. } else {
  281. channel_writel(dc, CHAR, 0);
  282. channel_writel(dc, SAR, 0);
  283. channel_writel(dc, DAR, 0);
  284. }
  285. channel_writel(dc, CNTR, 0);
  286. channel_writel(dc, SAIR, 0);
  287. channel_writel(dc, DAIR, 0);
  288. channel_writel(dc, CCR, 0);
  289. mmiowb();
  290. }
  291. /* Called with dc->lock held and bh disabled */
  292. static void txx9dmac_dostart(struct txx9dmac_chan *dc,
  293. struct txx9dmac_desc *first)
  294. {
  295. struct txx9dmac_slave *ds = dc->chan.private;
  296. u32 sai, dai;
  297. dev_vdbg(chan2dev(&dc->chan), "dostart %u %p\n",
  298. first->txd.cookie, first);
  299. /* ASSERT: channel is idle */
  300. if (channel_readl(dc, CSR) & TXX9_DMA_CSR_XFACT) {
  301. dev_err(chan2dev(&dc->chan),
  302. "BUG: Attempted to start non-idle channel\n");
  303. txx9dmac_dump_regs(dc);
  304. /* The tasklet will hopefully advance the queue... */
  305. return;
  306. }
  307. if (is_dmac64(dc)) {
  308. channel64_writel(dc, CNTR, 0);
  309. channel64_writel(dc, CSR, 0xffffffff);
  310. if (ds) {
  311. if (ds->tx_reg) {
  312. sai = ds->reg_width;
  313. dai = 0;
  314. } else {
  315. sai = 0;
  316. dai = ds->reg_width;
  317. }
  318. } else {
  319. sai = 8;
  320. dai = 8;
  321. }
  322. channel64_writel(dc, SAIR, sai);
  323. channel64_writel(dc, DAIR, dai);
  324. /* All 64-bit DMAC supports SMPCHN */
  325. channel64_writel(dc, CCR, dc->ccr);
  326. /* Writing a non zero value to CHAR will assert XFACT */
  327. channel64_write_CHAR(dc, first->txd.phys);
  328. } else {
  329. channel32_writel(dc, CNTR, 0);
  330. channel32_writel(dc, CSR, 0xffffffff);
  331. if (ds) {
  332. if (ds->tx_reg) {
  333. sai = ds->reg_width;
  334. dai = 0;
  335. } else {
  336. sai = 0;
  337. dai = ds->reg_width;
  338. }
  339. } else {
  340. sai = 4;
  341. dai = 4;
  342. }
  343. channel32_writel(dc, SAIR, sai);
  344. channel32_writel(dc, DAIR, dai);
  345. if (txx9_dma_have_SMPCHN()) {
  346. channel32_writel(dc, CCR, dc->ccr);
  347. /* Writing a non zero value to CHAR will assert XFACT */
  348. channel32_writel(dc, CHAR, first->txd.phys);
  349. } else {
  350. channel32_writel(dc, CHAR, first->txd.phys);
  351. channel32_writel(dc, CCR, dc->ccr);
  352. }
  353. }
  354. }
  355. /*----------------------------------------------------------------------*/
  356. static void
  357. txx9dmac_descriptor_complete(struct txx9dmac_chan *dc,
  358. struct txx9dmac_desc *desc)
  359. {
  360. struct dmaengine_desc_callback cb;
  361. struct dma_async_tx_descriptor *txd = &desc->txd;
  362. dev_vdbg(chan2dev(&dc->chan), "descriptor %u %p complete\n",
  363. txd->cookie, desc);
  364. dma_cookie_complete(txd);
  365. dmaengine_desc_get_callback(txd, &cb);
  366. txx9dmac_sync_desc_for_cpu(dc, desc);
  367. list_splice_init(&desc->tx_list, &dc->free_list);
  368. list_move(&desc->desc_node, &dc->free_list);
  369. dma_descriptor_unmap(txd);
  370. /*
  371. * The API requires that no submissions are done from a
  372. * callback, so we don't need to drop the lock here
  373. */
  374. dmaengine_desc_callback_invoke(&cb, NULL);
  375. dma_run_dependencies(txd);
  376. }
  377. static void txx9dmac_dequeue(struct txx9dmac_chan *dc, struct list_head *list)
  378. {
  379. struct txx9dmac_dev *ddev = dc->ddev;
  380. struct txx9dmac_desc *desc;
  381. struct txx9dmac_desc *prev = NULL;
  382. BUG_ON(!list_empty(list));
  383. do {
  384. desc = txx9dmac_first_queued(dc);
  385. if (prev) {
  386. desc_write_CHAR(dc, prev, desc->txd.phys);
  387. dma_sync_single_for_device(chan2parent(&dc->chan),
  388. prev->txd.phys, ddev->descsize,
  389. DMA_TO_DEVICE);
  390. }
  391. prev = txx9dmac_last_child(desc);
  392. list_move_tail(&desc->desc_node, list);
  393. /* Make chain-completion interrupt happen */
  394. if ((desc->txd.flags & DMA_PREP_INTERRUPT) &&
  395. !txx9dmac_chan_INTENT(dc))
  396. break;
  397. } while (!list_empty(&dc->queue));
  398. }
  399. static void txx9dmac_complete_all(struct txx9dmac_chan *dc)
  400. {
  401. struct txx9dmac_desc *desc, *_desc;
  402. LIST_HEAD(list);
  403. /*
  404. * Submit queued descriptors ASAP, i.e. before we go through
  405. * the completed ones.
  406. */
  407. list_splice_init(&dc->active_list, &list);
  408. if (!list_empty(&dc->queue)) {
  409. txx9dmac_dequeue(dc, &dc->active_list);
  410. txx9dmac_dostart(dc, txx9dmac_first_active(dc));
  411. }
  412. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  413. txx9dmac_descriptor_complete(dc, desc);
  414. }
  415. static void txx9dmac_dump_desc(struct txx9dmac_chan *dc,
  416. struct txx9dmac_hwdesc *desc)
  417. {
  418. if (is_dmac64(dc)) {
  419. #ifdef TXX9_DMA_USE_SIMPLE_CHAIN
  420. dev_crit(chan2dev(&dc->chan),
  421. " desc: ch%#llx s%#llx d%#llx c%#x\n",
  422. (u64)desc->CHAR, desc->SAR, desc->DAR, desc->CNTR);
  423. #else
  424. dev_crit(chan2dev(&dc->chan),
  425. " desc: ch%#llx s%#llx d%#llx c%#x"
  426. " si%#x di%#x cc%#x cs%#x\n",
  427. (u64)desc->CHAR, desc->SAR, desc->DAR, desc->CNTR,
  428. desc->SAIR, desc->DAIR, desc->CCR, desc->CSR);
  429. #endif
  430. } else {
  431. struct txx9dmac_hwdesc32 *d = (struct txx9dmac_hwdesc32 *)desc;
  432. #ifdef TXX9_DMA_USE_SIMPLE_CHAIN
  433. dev_crit(chan2dev(&dc->chan),
  434. " desc: ch%#x s%#x d%#x c%#x\n",
  435. d->CHAR, d->SAR, d->DAR, d->CNTR);
  436. #else
  437. dev_crit(chan2dev(&dc->chan),
  438. " desc: ch%#x s%#x d%#x c%#x"
  439. " si%#x di%#x cc%#x cs%#x\n",
  440. d->CHAR, d->SAR, d->DAR, d->CNTR,
  441. d->SAIR, d->DAIR, d->CCR, d->CSR);
  442. #endif
  443. }
  444. }
  445. static void txx9dmac_handle_error(struct txx9dmac_chan *dc, u32 csr)
  446. {
  447. struct txx9dmac_desc *bad_desc;
  448. struct txx9dmac_desc *child;
  449. u32 errors;
  450. /*
  451. * The descriptor currently at the head of the active list is
  452. * borked. Since we don't have any way to report errors, we'll
  453. * just have to scream loudly and try to carry on.
  454. */
  455. dev_crit(chan2dev(&dc->chan), "Abnormal Chain Completion\n");
  456. txx9dmac_dump_regs(dc);
  457. bad_desc = txx9dmac_first_active(dc);
  458. list_del_init(&bad_desc->desc_node);
  459. /* Clear all error flags and try to restart the controller */
  460. errors = csr & (TXX9_DMA_CSR_ABCHC |
  461. TXX9_DMA_CSR_CFERR | TXX9_DMA_CSR_CHERR |
  462. TXX9_DMA_CSR_DESERR | TXX9_DMA_CSR_SORERR);
  463. channel_writel(dc, CSR, errors);
  464. if (list_empty(&dc->active_list) && !list_empty(&dc->queue))
  465. txx9dmac_dequeue(dc, &dc->active_list);
  466. if (!list_empty(&dc->active_list))
  467. txx9dmac_dostart(dc, txx9dmac_first_active(dc));
  468. dev_crit(chan2dev(&dc->chan),
  469. "Bad descriptor submitted for DMA! (cookie: %d)\n",
  470. bad_desc->txd.cookie);
  471. txx9dmac_dump_desc(dc, &bad_desc->hwdesc);
  472. list_for_each_entry(child, &bad_desc->tx_list, desc_node)
  473. txx9dmac_dump_desc(dc, &child->hwdesc);
  474. /* Pretend the descriptor completed successfully */
  475. txx9dmac_descriptor_complete(dc, bad_desc);
  476. }
  477. static void txx9dmac_scan_descriptors(struct txx9dmac_chan *dc)
  478. {
  479. dma_addr_t chain;
  480. struct txx9dmac_desc *desc, *_desc;
  481. struct txx9dmac_desc *child;
  482. u32 csr;
  483. if (is_dmac64(dc)) {
  484. chain = channel64_read_CHAR(dc);
  485. csr = channel64_readl(dc, CSR);
  486. channel64_writel(dc, CSR, csr);
  487. } else {
  488. chain = channel32_readl(dc, CHAR);
  489. csr = channel32_readl(dc, CSR);
  490. channel32_writel(dc, CSR, csr);
  491. }
  492. /* For dynamic chain, we should look at XFACT instead of NCHNC */
  493. if (!(csr & (TXX9_DMA_CSR_XFACT | TXX9_DMA_CSR_ABCHC))) {
  494. /* Everything we've submitted is done */
  495. txx9dmac_complete_all(dc);
  496. return;
  497. }
  498. if (!(csr & TXX9_DMA_CSR_CHNEN))
  499. chain = 0; /* last descriptor of this chain */
  500. dev_vdbg(chan2dev(&dc->chan), "scan_descriptors: char=%#llx\n",
  501. (u64)chain);
  502. list_for_each_entry_safe(desc, _desc, &dc->active_list, desc_node) {
  503. if (desc_read_CHAR(dc, desc) == chain) {
  504. /* This one is currently in progress */
  505. if (csr & TXX9_DMA_CSR_ABCHC)
  506. goto scan_done;
  507. return;
  508. }
  509. list_for_each_entry(child, &desc->tx_list, desc_node)
  510. if (desc_read_CHAR(dc, child) == chain) {
  511. /* Currently in progress */
  512. if (csr & TXX9_DMA_CSR_ABCHC)
  513. goto scan_done;
  514. return;
  515. }
  516. /*
  517. * No descriptors so far seem to be in progress, i.e.
  518. * this one must be done.
  519. */
  520. txx9dmac_descriptor_complete(dc, desc);
  521. }
  522. scan_done:
  523. if (csr & TXX9_DMA_CSR_ABCHC) {
  524. txx9dmac_handle_error(dc, csr);
  525. return;
  526. }
  527. dev_err(chan2dev(&dc->chan),
  528. "BUG: All descriptors done, but channel not idle!\n");
  529. /* Try to continue after resetting the channel... */
  530. txx9dmac_reset_chan(dc);
  531. if (!list_empty(&dc->queue)) {
  532. txx9dmac_dequeue(dc, &dc->active_list);
  533. txx9dmac_dostart(dc, txx9dmac_first_active(dc));
  534. }
  535. }
  536. static void txx9dmac_chan_tasklet(unsigned long data)
  537. {
  538. int irq;
  539. u32 csr;
  540. struct txx9dmac_chan *dc;
  541. dc = (struct txx9dmac_chan *)data;
  542. csr = channel_readl(dc, CSR);
  543. dev_vdbg(chan2dev(&dc->chan), "tasklet: status=%x\n", csr);
  544. spin_lock(&dc->lock);
  545. if (csr & (TXX9_DMA_CSR_ABCHC | TXX9_DMA_CSR_NCHNC |
  546. TXX9_DMA_CSR_NTRNFC))
  547. txx9dmac_scan_descriptors(dc);
  548. spin_unlock(&dc->lock);
  549. irq = dc->irq;
  550. enable_irq(irq);
  551. }
  552. static irqreturn_t txx9dmac_chan_interrupt(int irq, void *dev_id)
  553. {
  554. struct txx9dmac_chan *dc = dev_id;
  555. dev_vdbg(chan2dev(&dc->chan), "interrupt: status=%#x\n",
  556. channel_readl(dc, CSR));
  557. tasklet_schedule(&dc->tasklet);
  558. /*
  559. * Just disable the interrupts. We'll turn them back on in the
  560. * softirq handler.
  561. */
  562. disable_irq_nosync(irq);
  563. return IRQ_HANDLED;
  564. }
  565. static void txx9dmac_tasklet(unsigned long data)
  566. {
  567. int irq;
  568. u32 csr;
  569. struct txx9dmac_chan *dc;
  570. struct txx9dmac_dev *ddev = (struct txx9dmac_dev *)data;
  571. u32 mcr;
  572. int i;
  573. mcr = dma_readl(ddev, MCR);
  574. dev_vdbg(ddev->chan[0]->dma.dev, "tasklet: mcr=%x\n", mcr);
  575. for (i = 0; i < TXX9_DMA_MAX_NR_CHANNELS; i++) {
  576. if ((mcr >> (24 + i)) & 0x11) {
  577. dc = ddev->chan[i];
  578. csr = channel_readl(dc, CSR);
  579. dev_vdbg(chan2dev(&dc->chan), "tasklet: status=%x\n",
  580. csr);
  581. spin_lock(&dc->lock);
  582. if (csr & (TXX9_DMA_CSR_ABCHC | TXX9_DMA_CSR_NCHNC |
  583. TXX9_DMA_CSR_NTRNFC))
  584. txx9dmac_scan_descriptors(dc);
  585. spin_unlock(&dc->lock);
  586. }
  587. }
  588. irq = ddev->irq;
  589. enable_irq(irq);
  590. }
  591. static irqreturn_t txx9dmac_interrupt(int irq, void *dev_id)
  592. {
  593. struct txx9dmac_dev *ddev = dev_id;
  594. dev_vdbg(ddev->chan[0]->dma.dev, "interrupt: status=%#x\n",
  595. dma_readl(ddev, MCR));
  596. tasklet_schedule(&ddev->tasklet);
  597. /*
  598. * Just disable the interrupts. We'll turn them back on in the
  599. * softirq handler.
  600. */
  601. disable_irq_nosync(irq);
  602. return IRQ_HANDLED;
  603. }
  604. /*----------------------------------------------------------------------*/
  605. static dma_cookie_t txx9dmac_tx_submit(struct dma_async_tx_descriptor *tx)
  606. {
  607. struct txx9dmac_desc *desc = txd_to_txx9dmac_desc(tx);
  608. struct txx9dmac_chan *dc = to_txx9dmac_chan(tx->chan);
  609. dma_cookie_t cookie;
  610. spin_lock_bh(&dc->lock);
  611. cookie = dma_cookie_assign(tx);
  612. dev_vdbg(chan2dev(tx->chan), "tx_submit: queued %u %p\n",
  613. desc->txd.cookie, desc);
  614. list_add_tail(&desc->desc_node, &dc->queue);
  615. spin_unlock_bh(&dc->lock);
  616. return cookie;
  617. }
  618. static struct dma_async_tx_descriptor *
  619. txx9dmac_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  620. size_t len, unsigned long flags)
  621. {
  622. struct txx9dmac_chan *dc = to_txx9dmac_chan(chan);
  623. struct txx9dmac_dev *ddev = dc->ddev;
  624. struct txx9dmac_desc *desc;
  625. struct txx9dmac_desc *first;
  626. struct txx9dmac_desc *prev;
  627. size_t xfer_count;
  628. size_t offset;
  629. dev_vdbg(chan2dev(chan), "prep_dma_memcpy d%#llx s%#llx l%#zx f%#lx\n",
  630. (u64)dest, (u64)src, len, flags);
  631. if (unlikely(!len)) {
  632. dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
  633. return NULL;
  634. }
  635. prev = first = NULL;
  636. for (offset = 0; offset < len; offset += xfer_count) {
  637. xfer_count = min_t(size_t, len - offset, TXX9_DMA_MAX_COUNT);
  638. /*
  639. * Workaround for ERT-TX49H2-033, ERT-TX49H3-020,
  640. * ERT-TX49H4-016 (slightly conservative)
  641. */
  642. if (__is_dmac64(ddev)) {
  643. if (xfer_count > 0x100 &&
  644. (xfer_count & 0xff) >= 0xfa &&
  645. (xfer_count & 0xff) <= 0xff)
  646. xfer_count -= 0x20;
  647. } else {
  648. if (xfer_count > 0x80 &&
  649. (xfer_count & 0x7f) >= 0x7e &&
  650. (xfer_count & 0x7f) <= 0x7f)
  651. xfer_count -= 0x20;
  652. }
  653. desc = txx9dmac_desc_get(dc);
  654. if (!desc) {
  655. txx9dmac_desc_put(dc, first);
  656. return NULL;
  657. }
  658. if (__is_dmac64(ddev)) {
  659. desc->hwdesc.SAR = src + offset;
  660. desc->hwdesc.DAR = dest + offset;
  661. desc->hwdesc.CNTR = xfer_count;
  662. txx9dmac_desc_set_nosimple(ddev, desc, 8, 8,
  663. dc->ccr | TXX9_DMA_CCR_XFACT);
  664. } else {
  665. desc->hwdesc32.SAR = src + offset;
  666. desc->hwdesc32.DAR = dest + offset;
  667. desc->hwdesc32.CNTR = xfer_count;
  668. txx9dmac_desc_set_nosimple(ddev, desc, 4, 4,
  669. dc->ccr | TXX9_DMA_CCR_XFACT);
  670. }
  671. /*
  672. * The descriptors on tx_list are not reachable from
  673. * the dc->queue list or dc->active_list after a
  674. * submit. If we put all descriptors on active_list,
  675. * calling of callback on the completion will be more
  676. * complex.
  677. */
  678. if (!first) {
  679. first = desc;
  680. } else {
  681. desc_write_CHAR(dc, prev, desc->txd.phys);
  682. dma_sync_single_for_device(chan2parent(&dc->chan),
  683. prev->txd.phys, ddev->descsize,
  684. DMA_TO_DEVICE);
  685. list_add_tail(&desc->desc_node, &first->tx_list);
  686. }
  687. prev = desc;
  688. }
  689. /* Trigger interrupt after last block */
  690. if (flags & DMA_PREP_INTERRUPT)
  691. txx9dmac_desc_set_INTENT(ddev, prev);
  692. desc_write_CHAR(dc, prev, 0);
  693. dma_sync_single_for_device(chan2parent(&dc->chan),
  694. prev->txd.phys, ddev->descsize,
  695. DMA_TO_DEVICE);
  696. first->txd.flags = flags;
  697. first->len = len;
  698. return &first->txd;
  699. }
  700. static struct dma_async_tx_descriptor *
  701. txx9dmac_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  702. unsigned int sg_len, enum dma_transfer_direction direction,
  703. unsigned long flags, void *context)
  704. {
  705. struct txx9dmac_chan *dc = to_txx9dmac_chan(chan);
  706. struct txx9dmac_dev *ddev = dc->ddev;
  707. struct txx9dmac_slave *ds = chan->private;
  708. struct txx9dmac_desc *prev;
  709. struct txx9dmac_desc *first;
  710. unsigned int i;
  711. struct scatterlist *sg;
  712. dev_vdbg(chan2dev(chan), "prep_dma_slave\n");
  713. BUG_ON(!ds || !ds->reg_width);
  714. if (ds->tx_reg)
  715. BUG_ON(direction != DMA_MEM_TO_DEV);
  716. else
  717. BUG_ON(direction != DMA_DEV_TO_MEM);
  718. if (unlikely(!sg_len))
  719. return NULL;
  720. prev = first = NULL;
  721. for_each_sg(sgl, sg, sg_len, i) {
  722. struct txx9dmac_desc *desc;
  723. dma_addr_t mem;
  724. u32 sai, dai;
  725. desc = txx9dmac_desc_get(dc);
  726. if (!desc) {
  727. txx9dmac_desc_put(dc, first);
  728. return NULL;
  729. }
  730. mem = sg_dma_address(sg);
  731. if (__is_dmac64(ddev)) {
  732. if (direction == DMA_MEM_TO_DEV) {
  733. desc->hwdesc.SAR = mem;
  734. desc->hwdesc.DAR = ds->tx_reg;
  735. } else {
  736. desc->hwdesc.SAR = ds->rx_reg;
  737. desc->hwdesc.DAR = mem;
  738. }
  739. desc->hwdesc.CNTR = sg_dma_len(sg);
  740. } else {
  741. if (direction == DMA_MEM_TO_DEV) {
  742. desc->hwdesc32.SAR = mem;
  743. desc->hwdesc32.DAR = ds->tx_reg;
  744. } else {
  745. desc->hwdesc32.SAR = ds->rx_reg;
  746. desc->hwdesc32.DAR = mem;
  747. }
  748. desc->hwdesc32.CNTR = sg_dma_len(sg);
  749. }
  750. if (direction == DMA_MEM_TO_DEV) {
  751. sai = ds->reg_width;
  752. dai = 0;
  753. } else {
  754. sai = 0;
  755. dai = ds->reg_width;
  756. }
  757. txx9dmac_desc_set_nosimple(ddev, desc, sai, dai,
  758. dc->ccr | TXX9_DMA_CCR_XFACT);
  759. if (!first) {
  760. first = desc;
  761. } else {
  762. desc_write_CHAR(dc, prev, desc->txd.phys);
  763. dma_sync_single_for_device(chan2parent(&dc->chan),
  764. prev->txd.phys,
  765. ddev->descsize,
  766. DMA_TO_DEVICE);
  767. list_add_tail(&desc->desc_node, &first->tx_list);
  768. }
  769. prev = desc;
  770. }
  771. /* Trigger interrupt after last block */
  772. if (flags & DMA_PREP_INTERRUPT)
  773. txx9dmac_desc_set_INTENT(ddev, prev);
  774. desc_write_CHAR(dc, prev, 0);
  775. dma_sync_single_for_device(chan2parent(&dc->chan),
  776. prev->txd.phys, ddev->descsize,
  777. DMA_TO_DEVICE);
  778. first->txd.flags = flags;
  779. first->len = 0;
  780. return &first->txd;
  781. }
  782. static int txx9dmac_terminate_all(struct dma_chan *chan)
  783. {
  784. struct txx9dmac_chan *dc = to_txx9dmac_chan(chan);
  785. struct txx9dmac_desc *desc, *_desc;
  786. LIST_HEAD(list);
  787. dev_vdbg(chan2dev(chan), "terminate_all\n");
  788. spin_lock_bh(&dc->lock);
  789. txx9dmac_reset_chan(dc);
  790. /* active_list entries will end up before queued entries */
  791. list_splice_init(&dc->queue, &list);
  792. list_splice_init(&dc->active_list, &list);
  793. spin_unlock_bh(&dc->lock);
  794. /* Flush all pending and queued descriptors */
  795. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  796. txx9dmac_descriptor_complete(dc, desc);
  797. return 0;
  798. }
  799. static enum dma_status
  800. txx9dmac_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
  801. struct dma_tx_state *txstate)
  802. {
  803. struct txx9dmac_chan *dc = to_txx9dmac_chan(chan);
  804. enum dma_status ret;
  805. ret = dma_cookie_status(chan, cookie, txstate);
  806. if (ret == DMA_COMPLETE)
  807. return DMA_COMPLETE;
  808. spin_lock_bh(&dc->lock);
  809. txx9dmac_scan_descriptors(dc);
  810. spin_unlock_bh(&dc->lock);
  811. return dma_cookie_status(chan, cookie, txstate);
  812. }
  813. static void txx9dmac_chain_dynamic(struct txx9dmac_chan *dc,
  814. struct txx9dmac_desc *prev)
  815. {
  816. struct txx9dmac_dev *ddev = dc->ddev;
  817. struct txx9dmac_desc *desc;
  818. LIST_HEAD(list);
  819. prev = txx9dmac_last_child(prev);
  820. txx9dmac_dequeue(dc, &list);
  821. desc = list_entry(list.next, struct txx9dmac_desc, desc_node);
  822. desc_write_CHAR(dc, prev, desc->txd.phys);
  823. dma_sync_single_for_device(chan2parent(&dc->chan),
  824. prev->txd.phys, ddev->descsize,
  825. DMA_TO_DEVICE);
  826. mmiowb();
  827. if (!(channel_readl(dc, CSR) & TXX9_DMA_CSR_CHNEN) &&
  828. channel_read_CHAR(dc) == prev->txd.phys)
  829. /* Restart chain DMA */
  830. channel_write_CHAR(dc, desc->txd.phys);
  831. list_splice_tail(&list, &dc->active_list);
  832. }
  833. static void txx9dmac_issue_pending(struct dma_chan *chan)
  834. {
  835. struct txx9dmac_chan *dc = to_txx9dmac_chan(chan);
  836. spin_lock_bh(&dc->lock);
  837. if (!list_empty(&dc->active_list))
  838. txx9dmac_scan_descriptors(dc);
  839. if (!list_empty(&dc->queue)) {
  840. if (list_empty(&dc->active_list)) {
  841. txx9dmac_dequeue(dc, &dc->active_list);
  842. txx9dmac_dostart(dc, txx9dmac_first_active(dc));
  843. } else if (txx9_dma_have_SMPCHN()) {
  844. struct txx9dmac_desc *prev = txx9dmac_last_active(dc);
  845. if (!(prev->txd.flags & DMA_PREP_INTERRUPT) ||
  846. txx9dmac_chan_INTENT(dc))
  847. txx9dmac_chain_dynamic(dc, prev);
  848. }
  849. }
  850. spin_unlock_bh(&dc->lock);
  851. }
  852. static int txx9dmac_alloc_chan_resources(struct dma_chan *chan)
  853. {
  854. struct txx9dmac_chan *dc = to_txx9dmac_chan(chan);
  855. struct txx9dmac_slave *ds = chan->private;
  856. struct txx9dmac_desc *desc;
  857. int i;
  858. dev_vdbg(chan2dev(chan), "alloc_chan_resources\n");
  859. /* ASSERT: channel is idle */
  860. if (channel_readl(dc, CSR) & TXX9_DMA_CSR_XFACT) {
  861. dev_dbg(chan2dev(chan), "DMA channel not idle?\n");
  862. return -EIO;
  863. }
  864. dma_cookie_init(chan);
  865. dc->ccr = TXX9_DMA_CCR_IMMCHN | TXX9_DMA_CCR_INTENE | CCR_LE;
  866. txx9dmac_chan_set_SMPCHN(dc);
  867. if (!txx9_dma_have_SMPCHN() || (dc->ccr & TXX9_DMA_CCR_SMPCHN))
  868. dc->ccr |= TXX9_DMA_CCR_INTENC;
  869. if (chan->device->device_prep_dma_memcpy) {
  870. if (ds)
  871. return -EINVAL;
  872. dc->ccr |= TXX9_DMA_CCR_XFSZ_X8;
  873. } else {
  874. if (!ds ||
  875. (ds->tx_reg && ds->rx_reg) || (!ds->tx_reg && !ds->rx_reg))
  876. return -EINVAL;
  877. dc->ccr |= TXX9_DMA_CCR_EXTRQ |
  878. TXX9_DMA_CCR_XFSZ(__ffs(ds->reg_width));
  879. txx9dmac_chan_set_INTENT(dc);
  880. }
  881. spin_lock_bh(&dc->lock);
  882. i = dc->descs_allocated;
  883. while (dc->descs_allocated < TXX9_DMA_INITIAL_DESC_COUNT) {
  884. spin_unlock_bh(&dc->lock);
  885. desc = txx9dmac_desc_alloc(dc, GFP_KERNEL);
  886. if (!desc) {
  887. dev_info(chan2dev(chan),
  888. "only allocated %d descriptors\n", i);
  889. spin_lock_bh(&dc->lock);
  890. break;
  891. }
  892. txx9dmac_desc_put(dc, desc);
  893. spin_lock_bh(&dc->lock);
  894. i = ++dc->descs_allocated;
  895. }
  896. spin_unlock_bh(&dc->lock);
  897. dev_dbg(chan2dev(chan),
  898. "alloc_chan_resources allocated %d descriptors\n", i);
  899. return i;
  900. }
  901. static void txx9dmac_free_chan_resources(struct dma_chan *chan)
  902. {
  903. struct txx9dmac_chan *dc = to_txx9dmac_chan(chan);
  904. struct txx9dmac_dev *ddev = dc->ddev;
  905. struct txx9dmac_desc *desc, *_desc;
  906. LIST_HEAD(list);
  907. dev_dbg(chan2dev(chan), "free_chan_resources (descs allocated=%u)\n",
  908. dc->descs_allocated);
  909. /* ASSERT: channel is idle */
  910. BUG_ON(!list_empty(&dc->active_list));
  911. BUG_ON(!list_empty(&dc->queue));
  912. BUG_ON(channel_readl(dc, CSR) & TXX9_DMA_CSR_XFACT);
  913. spin_lock_bh(&dc->lock);
  914. list_splice_init(&dc->free_list, &list);
  915. dc->descs_allocated = 0;
  916. spin_unlock_bh(&dc->lock);
  917. list_for_each_entry_safe(desc, _desc, &list, desc_node) {
  918. dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
  919. dma_unmap_single(chan2parent(chan), desc->txd.phys,
  920. ddev->descsize, DMA_TO_DEVICE);
  921. kfree(desc);
  922. }
  923. dev_vdbg(chan2dev(chan), "free_chan_resources done\n");
  924. }
  925. /*----------------------------------------------------------------------*/
  926. static void txx9dmac_off(struct txx9dmac_dev *ddev)
  927. {
  928. dma_writel(ddev, MCR, 0);
  929. mmiowb();
  930. }
  931. static int __init txx9dmac_chan_probe(struct platform_device *pdev)
  932. {
  933. struct txx9dmac_chan_platform_data *cpdata =
  934. dev_get_platdata(&pdev->dev);
  935. struct platform_device *dmac_dev = cpdata->dmac_dev;
  936. struct txx9dmac_platform_data *pdata = dev_get_platdata(&dmac_dev->dev);
  937. struct txx9dmac_chan *dc;
  938. int err;
  939. int ch = pdev->id % TXX9_DMA_MAX_NR_CHANNELS;
  940. int irq;
  941. dc = devm_kzalloc(&pdev->dev, sizeof(*dc), GFP_KERNEL);
  942. if (!dc)
  943. return -ENOMEM;
  944. dc->dma.dev = &pdev->dev;
  945. dc->dma.device_alloc_chan_resources = txx9dmac_alloc_chan_resources;
  946. dc->dma.device_free_chan_resources = txx9dmac_free_chan_resources;
  947. dc->dma.device_terminate_all = txx9dmac_terminate_all;
  948. dc->dma.device_tx_status = txx9dmac_tx_status;
  949. dc->dma.device_issue_pending = txx9dmac_issue_pending;
  950. if (pdata && pdata->memcpy_chan == ch) {
  951. dc->dma.device_prep_dma_memcpy = txx9dmac_prep_dma_memcpy;
  952. dma_cap_set(DMA_MEMCPY, dc->dma.cap_mask);
  953. } else {
  954. dc->dma.device_prep_slave_sg = txx9dmac_prep_slave_sg;
  955. dma_cap_set(DMA_SLAVE, dc->dma.cap_mask);
  956. dma_cap_set(DMA_PRIVATE, dc->dma.cap_mask);
  957. }
  958. INIT_LIST_HEAD(&dc->dma.channels);
  959. dc->ddev = platform_get_drvdata(dmac_dev);
  960. if (dc->ddev->irq < 0) {
  961. irq = platform_get_irq(pdev, 0);
  962. if (irq < 0)
  963. return irq;
  964. tasklet_init(&dc->tasklet, txx9dmac_chan_tasklet,
  965. (unsigned long)dc);
  966. dc->irq = irq;
  967. err = devm_request_irq(&pdev->dev, dc->irq,
  968. txx9dmac_chan_interrupt, 0, dev_name(&pdev->dev), dc);
  969. if (err)
  970. return err;
  971. } else
  972. dc->irq = -1;
  973. dc->ddev->chan[ch] = dc;
  974. dc->chan.device = &dc->dma;
  975. list_add_tail(&dc->chan.device_node, &dc->chan.device->channels);
  976. dma_cookie_init(&dc->chan);
  977. if (is_dmac64(dc))
  978. dc->ch_regs = &__txx9dmac_regs(dc->ddev)->CHAN[ch];
  979. else
  980. dc->ch_regs = &__txx9dmac_regs32(dc->ddev)->CHAN[ch];
  981. spin_lock_init(&dc->lock);
  982. INIT_LIST_HEAD(&dc->active_list);
  983. INIT_LIST_HEAD(&dc->queue);
  984. INIT_LIST_HEAD(&dc->free_list);
  985. txx9dmac_reset_chan(dc);
  986. platform_set_drvdata(pdev, dc);
  987. err = dma_async_device_register(&dc->dma);
  988. if (err)
  989. return err;
  990. dev_dbg(&pdev->dev, "TXx9 DMA Channel (dma%d%s%s)\n",
  991. dc->dma.dev_id,
  992. dma_has_cap(DMA_MEMCPY, dc->dma.cap_mask) ? " memcpy" : "",
  993. dma_has_cap(DMA_SLAVE, dc->dma.cap_mask) ? " slave" : "");
  994. return 0;
  995. }
  996. static int txx9dmac_chan_remove(struct platform_device *pdev)
  997. {
  998. struct txx9dmac_chan *dc = platform_get_drvdata(pdev);
  999. dma_async_device_unregister(&dc->dma);
  1000. if (dc->irq >= 0) {
  1001. devm_free_irq(&pdev->dev, dc->irq, dc);
  1002. tasklet_kill(&dc->tasklet);
  1003. }
  1004. dc->ddev->chan[pdev->id % TXX9_DMA_MAX_NR_CHANNELS] = NULL;
  1005. return 0;
  1006. }
  1007. static int __init txx9dmac_probe(struct platform_device *pdev)
  1008. {
  1009. struct txx9dmac_platform_data *pdata = dev_get_platdata(&pdev->dev);
  1010. struct resource *io;
  1011. struct txx9dmac_dev *ddev;
  1012. u32 mcr;
  1013. int err;
  1014. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1015. if (!io)
  1016. return -EINVAL;
  1017. ddev = devm_kzalloc(&pdev->dev, sizeof(*ddev), GFP_KERNEL);
  1018. if (!ddev)
  1019. return -ENOMEM;
  1020. if (!devm_request_mem_region(&pdev->dev, io->start, resource_size(io),
  1021. dev_name(&pdev->dev)))
  1022. return -EBUSY;
  1023. ddev->regs = devm_ioremap(&pdev->dev, io->start, resource_size(io));
  1024. if (!ddev->regs)
  1025. return -ENOMEM;
  1026. ddev->have_64bit_regs = pdata->have_64bit_regs;
  1027. if (__is_dmac64(ddev))
  1028. ddev->descsize = sizeof(struct txx9dmac_hwdesc);
  1029. else
  1030. ddev->descsize = sizeof(struct txx9dmac_hwdesc32);
  1031. /* force dma off, just in case */
  1032. txx9dmac_off(ddev);
  1033. ddev->irq = platform_get_irq(pdev, 0);
  1034. if (ddev->irq >= 0) {
  1035. tasklet_init(&ddev->tasklet, txx9dmac_tasklet,
  1036. (unsigned long)ddev);
  1037. err = devm_request_irq(&pdev->dev, ddev->irq,
  1038. txx9dmac_interrupt, 0, dev_name(&pdev->dev), ddev);
  1039. if (err)
  1040. return err;
  1041. }
  1042. mcr = TXX9_DMA_MCR_MSTEN | MCR_LE;
  1043. if (pdata && pdata->memcpy_chan >= 0)
  1044. mcr |= TXX9_DMA_MCR_FIFUM(pdata->memcpy_chan);
  1045. dma_writel(ddev, MCR, mcr);
  1046. platform_set_drvdata(pdev, ddev);
  1047. return 0;
  1048. }
  1049. static int txx9dmac_remove(struct platform_device *pdev)
  1050. {
  1051. struct txx9dmac_dev *ddev = platform_get_drvdata(pdev);
  1052. txx9dmac_off(ddev);
  1053. if (ddev->irq >= 0) {
  1054. devm_free_irq(&pdev->dev, ddev->irq, ddev);
  1055. tasklet_kill(&ddev->tasklet);
  1056. }
  1057. return 0;
  1058. }
  1059. static void txx9dmac_shutdown(struct platform_device *pdev)
  1060. {
  1061. struct txx9dmac_dev *ddev = platform_get_drvdata(pdev);
  1062. txx9dmac_off(ddev);
  1063. }
  1064. static int txx9dmac_suspend_noirq(struct device *dev)
  1065. {
  1066. struct txx9dmac_dev *ddev = dev_get_drvdata(dev);
  1067. txx9dmac_off(ddev);
  1068. return 0;
  1069. }
  1070. static int txx9dmac_resume_noirq(struct device *dev)
  1071. {
  1072. struct txx9dmac_dev *ddev = dev_get_drvdata(dev);
  1073. struct txx9dmac_platform_data *pdata = dev_get_platdata(dev);
  1074. u32 mcr;
  1075. mcr = TXX9_DMA_MCR_MSTEN | MCR_LE;
  1076. if (pdata && pdata->memcpy_chan >= 0)
  1077. mcr |= TXX9_DMA_MCR_FIFUM(pdata->memcpy_chan);
  1078. dma_writel(ddev, MCR, mcr);
  1079. return 0;
  1080. }
  1081. static const struct dev_pm_ops txx9dmac_dev_pm_ops = {
  1082. .suspend_noirq = txx9dmac_suspend_noirq,
  1083. .resume_noirq = txx9dmac_resume_noirq,
  1084. };
  1085. static struct platform_driver txx9dmac_chan_driver = {
  1086. .remove = txx9dmac_chan_remove,
  1087. .driver = {
  1088. .name = "txx9dmac-chan",
  1089. },
  1090. };
  1091. static struct platform_driver txx9dmac_driver = {
  1092. .remove = txx9dmac_remove,
  1093. .shutdown = txx9dmac_shutdown,
  1094. .driver = {
  1095. .name = "txx9dmac",
  1096. .pm = &txx9dmac_dev_pm_ops,
  1097. },
  1098. };
  1099. static int __init txx9dmac_init(void)
  1100. {
  1101. int rc;
  1102. rc = platform_driver_probe(&txx9dmac_driver, txx9dmac_probe);
  1103. if (!rc) {
  1104. rc = platform_driver_probe(&txx9dmac_chan_driver,
  1105. txx9dmac_chan_probe);
  1106. if (rc)
  1107. platform_driver_unregister(&txx9dmac_driver);
  1108. }
  1109. return rc;
  1110. }
  1111. module_init(txx9dmac_init);
  1112. static void __exit txx9dmac_exit(void)
  1113. {
  1114. platform_driver_unregister(&txx9dmac_chan_driver);
  1115. platform_driver_unregister(&txx9dmac_driver);
  1116. }
  1117. module_exit(txx9dmac_exit);
  1118. MODULE_LICENSE("GPL");
  1119. MODULE_DESCRIPTION("TXx9 DMA Controller driver");
  1120. MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
  1121. MODULE_ALIAS("platform:txx9dmac");
  1122. MODULE_ALIAS("platform:txx9dmac-chan");