txx9dmac.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  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_64BIT_PHYS_ADDR)
  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. dma_async_tx_callback callback;
  361. void *param;
  362. struct dma_async_tx_descriptor *txd = &desc->txd;
  363. struct txx9dmac_slave *ds = dc->chan.private;
  364. dev_vdbg(chan2dev(&dc->chan), "descriptor %u %p complete\n",
  365. txd->cookie, desc);
  366. dma_cookie_complete(txd);
  367. callback = txd->callback;
  368. param = txd->callback_param;
  369. txx9dmac_sync_desc_for_cpu(dc, desc);
  370. list_splice_init(&desc->tx_list, &dc->free_list);
  371. list_move(&desc->desc_node, &dc->free_list);
  372. dma_descriptor_unmap(txd);
  373. /*
  374. * The API requires that no submissions are done from a
  375. * callback, so we don't need to drop the lock here
  376. */
  377. if (callback)
  378. callback(param);
  379. dma_run_dependencies(txd);
  380. }
  381. static void txx9dmac_dequeue(struct txx9dmac_chan *dc, struct list_head *list)
  382. {
  383. struct txx9dmac_dev *ddev = dc->ddev;
  384. struct txx9dmac_desc *desc;
  385. struct txx9dmac_desc *prev = NULL;
  386. BUG_ON(!list_empty(list));
  387. do {
  388. desc = txx9dmac_first_queued(dc);
  389. if (prev) {
  390. desc_write_CHAR(dc, prev, desc->txd.phys);
  391. dma_sync_single_for_device(chan2parent(&dc->chan),
  392. prev->txd.phys, ddev->descsize,
  393. DMA_TO_DEVICE);
  394. }
  395. prev = txx9dmac_last_child(desc);
  396. list_move_tail(&desc->desc_node, list);
  397. /* Make chain-completion interrupt happen */
  398. if ((desc->txd.flags & DMA_PREP_INTERRUPT) &&
  399. !txx9dmac_chan_INTENT(dc))
  400. break;
  401. } while (!list_empty(&dc->queue));
  402. }
  403. static void txx9dmac_complete_all(struct txx9dmac_chan *dc)
  404. {
  405. struct txx9dmac_desc *desc, *_desc;
  406. LIST_HEAD(list);
  407. /*
  408. * Submit queued descriptors ASAP, i.e. before we go through
  409. * the completed ones.
  410. */
  411. list_splice_init(&dc->active_list, &list);
  412. if (!list_empty(&dc->queue)) {
  413. txx9dmac_dequeue(dc, &dc->active_list);
  414. txx9dmac_dostart(dc, txx9dmac_first_active(dc));
  415. }
  416. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  417. txx9dmac_descriptor_complete(dc, desc);
  418. }
  419. static void txx9dmac_dump_desc(struct txx9dmac_chan *dc,
  420. struct txx9dmac_hwdesc *desc)
  421. {
  422. if (is_dmac64(dc)) {
  423. #ifdef TXX9_DMA_USE_SIMPLE_CHAIN
  424. dev_crit(chan2dev(&dc->chan),
  425. " desc: ch%#llx s%#llx d%#llx c%#x\n",
  426. (u64)desc->CHAR, desc->SAR, desc->DAR, desc->CNTR);
  427. #else
  428. dev_crit(chan2dev(&dc->chan),
  429. " desc: ch%#llx s%#llx d%#llx c%#x"
  430. " si%#x di%#x cc%#x cs%#x\n",
  431. (u64)desc->CHAR, desc->SAR, desc->DAR, desc->CNTR,
  432. desc->SAIR, desc->DAIR, desc->CCR, desc->CSR);
  433. #endif
  434. } else {
  435. struct txx9dmac_hwdesc32 *d = (struct txx9dmac_hwdesc32 *)desc;
  436. #ifdef TXX9_DMA_USE_SIMPLE_CHAIN
  437. dev_crit(chan2dev(&dc->chan),
  438. " desc: ch%#x s%#x d%#x c%#x\n",
  439. d->CHAR, d->SAR, d->DAR, d->CNTR);
  440. #else
  441. dev_crit(chan2dev(&dc->chan),
  442. " desc: ch%#x s%#x d%#x c%#x"
  443. " si%#x di%#x cc%#x cs%#x\n",
  444. d->CHAR, d->SAR, d->DAR, d->CNTR,
  445. d->SAIR, d->DAIR, d->CCR, d->CSR);
  446. #endif
  447. }
  448. }
  449. static void txx9dmac_handle_error(struct txx9dmac_chan *dc, u32 csr)
  450. {
  451. struct txx9dmac_desc *bad_desc;
  452. struct txx9dmac_desc *child;
  453. u32 errors;
  454. /*
  455. * The descriptor currently at the head of the active list is
  456. * borked. Since we don't have any way to report errors, we'll
  457. * just have to scream loudly and try to carry on.
  458. */
  459. dev_crit(chan2dev(&dc->chan), "Abnormal Chain Completion\n");
  460. txx9dmac_dump_regs(dc);
  461. bad_desc = txx9dmac_first_active(dc);
  462. list_del_init(&bad_desc->desc_node);
  463. /* Clear all error flags and try to restart the controller */
  464. errors = csr & (TXX9_DMA_CSR_ABCHC |
  465. TXX9_DMA_CSR_CFERR | TXX9_DMA_CSR_CHERR |
  466. TXX9_DMA_CSR_DESERR | TXX9_DMA_CSR_SORERR);
  467. channel_writel(dc, CSR, errors);
  468. if (list_empty(&dc->active_list) && !list_empty(&dc->queue))
  469. txx9dmac_dequeue(dc, &dc->active_list);
  470. if (!list_empty(&dc->active_list))
  471. txx9dmac_dostart(dc, txx9dmac_first_active(dc));
  472. dev_crit(chan2dev(&dc->chan),
  473. "Bad descriptor submitted for DMA! (cookie: %d)\n",
  474. bad_desc->txd.cookie);
  475. txx9dmac_dump_desc(dc, &bad_desc->hwdesc);
  476. list_for_each_entry(child, &bad_desc->tx_list, desc_node)
  477. txx9dmac_dump_desc(dc, &child->hwdesc);
  478. /* Pretend the descriptor completed successfully */
  479. txx9dmac_descriptor_complete(dc, bad_desc);
  480. }
  481. static void txx9dmac_scan_descriptors(struct txx9dmac_chan *dc)
  482. {
  483. dma_addr_t chain;
  484. struct txx9dmac_desc *desc, *_desc;
  485. struct txx9dmac_desc *child;
  486. u32 csr;
  487. if (is_dmac64(dc)) {
  488. chain = channel64_read_CHAR(dc);
  489. csr = channel64_readl(dc, CSR);
  490. channel64_writel(dc, CSR, csr);
  491. } else {
  492. chain = channel32_readl(dc, CHAR);
  493. csr = channel32_readl(dc, CSR);
  494. channel32_writel(dc, CSR, csr);
  495. }
  496. /* For dynamic chain, we should look at XFACT instead of NCHNC */
  497. if (!(csr & (TXX9_DMA_CSR_XFACT | TXX9_DMA_CSR_ABCHC))) {
  498. /* Everything we've submitted is done */
  499. txx9dmac_complete_all(dc);
  500. return;
  501. }
  502. if (!(csr & TXX9_DMA_CSR_CHNEN))
  503. chain = 0; /* last descriptor of this chain */
  504. dev_vdbg(chan2dev(&dc->chan), "scan_descriptors: char=%#llx\n",
  505. (u64)chain);
  506. list_for_each_entry_safe(desc, _desc, &dc->active_list, desc_node) {
  507. if (desc_read_CHAR(dc, desc) == chain) {
  508. /* This one is currently in progress */
  509. if (csr & TXX9_DMA_CSR_ABCHC)
  510. goto scan_done;
  511. return;
  512. }
  513. list_for_each_entry(child, &desc->tx_list, desc_node)
  514. if (desc_read_CHAR(dc, child) == chain) {
  515. /* Currently in progress */
  516. if (csr & TXX9_DMA_CSR_ABCHC)
  517. goto scan_done;
  518. return;
  519. }
  520. /*
  521. * No descriptors so far seem to be in progress, i.e.
  522. * this one must be done.
  523. */
  524. txx9dmac_descriptor_complete(dc, desc);
  525. }
  526. scan_done:
  527. if (csr & TXX9_DMA_CSR_ABCHC) {
  528. txx9dmac_handle_error(dc, csr);
  529. return;
  530. }
  531. dev_err(chan2dev(&dc->chan),
  532. "BUG: All descriptors done, but channel not idle!\n");
  533. /* Try to continue after resetting the channel... */
  534. txx9dmac_reset_chan(dc);
  535. if (!list_empty(&dc->queue)) {
  536. txx9dmac_dequeue(dc, &dc->active_list);
  537. txx9dmac_dostart(dc, txx9dmac_first_active(dc));
  538. }
  539. }
  540. static void txx9dmac_chan_tasklet(unsigned long data)
  541. {
  542. int irq;
  543. u32 csr;
  544. struct txx9dmac_chan *dc;
  545. dc = (struct txx9dmac_chan *)data;
  546. csr = channel_readl(dc, CSR);
  547. dev_vdbg(chan2dev(&dc->chan), "tasklet: status=%x\n", csr);
  548. spin_lock(&dc->lock);
  549. if (csr & (TXX9_DMA_CSR_ABCHC | TXX9_DMA_CSR_NCHNC |
  550. TXX9_DMA_CSR_NTRNFC))
  551. txx9dmac_scan_descriptors(dc);
  552. spin_unlock(&dc->lock);
  553. irq = dc->irq;
  554. enable_irq(irq);
  555. }
  556. static irqreturn_t txx9dmac_chan_interrupt(int irq, void *dev_id)
  557. {
  558. struct txx9dmac_chan *dc = dev_id;
  559. dev_vdbg(chan2dev(&dc->chan), "interrupt: status=%#x\n",
  560. channel_readl(dc, CSR));
  561. tasklet_schedule(&dc->tasklet);
  562. /*
  563. * Just disable the interrupts. We'll turn them back on in the
  564. * softirq handler.
  565. */
  566. disable_irq_nosync(irq);
  567. return IRQ_HANDLED;
  568. }
  569. static void txx9dmac_tasklet(unsigned long data)
  570. {
  571. int irq;
  572. u32 csr;
  573. struct txx9dmac_chan *dc;
  574. struct txx9dmac_dev *ddev = (struct txx9dmac_dev *)data;
  575. u32 mcr;
  576. int i;
  577. mcr = dma_readl(ddev, MCR);
  578. dev_vdbg(ddev->chan[0]->dma.dev, "tasklet: mcr=%x\n", mcr);
  579. for (i = 0; i < TXX9_DMA_MAX_NR_CHANNELS; i++) {
  580. if ((mcr >> (24 + i)) & 0x11) {
  581. dc = ddev->chan[i];
  582. csr = channel_readl(dc, CSR);
  583. dev_vdbg(chan2dev(&dc->chan), "tasklet: status=%x\n",
  584. csr);
  585. spin_lock(&dc->lock);
  586. if (csr & (TXX9_DMA_CSR_ABCHC | TXX9_DMA_CSR_NCHNC |
  587. TXX9_DMA_CSR_NTRNFC))
  588. txx9dmac_scan_descriptors(dc);
  589. spin_unlock(&dc->lock);
  590. }
  591. }
  592. irq = ddev->irq;
  593. enable_irq(irq);
  594. }
  595. static irqreturn_t txx9dmac_interrupt(int irq, void *dev_id)
  596. {
  597. struct txx9dmac_dev *ddev = dev_id;
  598. dev_vdbg(ddev->chan[0]->dma.dev, "interrupt: status=%#x\n",
  599. dma_readl(ddev, MCR));
  600. tasklet_schedule(&ddev->tasklet);
  601. /*
  602. * Just disable the interrupts. We'll turn them back on in the
  603. * softirq handler.
  604. */
  605. disable_irq_nosync(irq);
  606. return IRQ_HANDLED;
  607. }
  608. /*----------------------------------------------------------------------*/
  609. static dma_cookie_t txx9dmac_tx_submit(struct dma_async_tx_descriptor *tx)
  610. {
  611. struct txx9dmac_desc *desc = txd_to_txx9dmac_desc(tx);
  612. struct txx9dmac_chan *dc = to_txx9dmac_chan(tx->chan);
  613. dma_cookie_t cookie;
  614. spin_lock_bh(&dc->lock);
  615. cookie = dma_cookie_assign(tx);
  616. dev_vdbg(chan2dev(tx->chan), "tx_submit: queued %u %p\n",
  617. desc->txd.cookie, desc);
  618. list_add_tail(&desc->desc_node, &dc->queue);
  619. spin_unlock_bh(&dc->lock);
  620. return cookie;
  621. }
  622. static struct dma_async_tx_descriptor *
  623. txx9dmac_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  624. size_t len, unsigned long flags)
  625. {
  626. struct txx9dmac_chan *dc = to_txx9dmac_chan(chan);
  627. struct txx9dmac_dev *ddev = dc->ddev;
  628. struct txx9dmac_desc *desc;
  629. struct txx9dmac_desc *first;
  630. struct txx9dmac_desc *prev;
  631. size_t xfer_count;
  632. size_t offset;
  633. dev_vdbg(chan2dev(chan), "prep_dma_memcpy d%#llx s%#llx l%#zx f%#lx\n",
  634. (u64)dest, (u64)src, len, flags);
  635. if (unlikely(!len)) {
  636. dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
  637. return NULL;
  638. }
  639. prev = first = NULL;
  640. for (offset = 0; offset < len; offset += xfer_count) {
  641. xfer_count = min_t(size_t, len - offset, TXX9_DMA_MAX_COUNT);
  642. /*
  643. * Workaround for ERT-TX49H2-033, ERT-TX49H3-020,
  644. * ERT-TX49H4-016 (slightly conservative)
  645. */
  646. if (__is_dmac64(ddev)) {
  647. if (xfer_count > 0x100 &&
  648. (xfer_count & 0xff) >= 0xfa &&
  649. (xfer_count & 0xff) <= 0xff)
  650. xfer_count -= 0x20;
  651. } else {
  652. if (xfer_count > 0x80 &&
  653. (xfer_count & 0x7f) >= 0x7e &&
  654. (xfer_count & 0x7f) <= 0x7f)
  655. xfer_count -= 0x20;
  656. }
  657. desc = txx9dmac_desc_get(dc);
  658. if (!desc) {
  659. txx9dmac_desc_put(dc, first);
  660. return NULL;
  661. }
  662. if (__is_dmac64(ddev)) {
  663. desc->hwdesc.SAR = src + offset;
  664. desc->hwdesc.DAR = dest + offset;
  665. desc->hwdesc.CNTR = xfer_count;
  666. txx9dmac_desc_set_nosimple(ddev, desc, 8, 8,
  667. dc->ccr | TXX9_DMA_CCR_XFACT);
  668. } else {
  669. desc->hwdesc32.SAR = src + offset;
  670. desc->hwdesc32.DAR = dest + offset;
  671. desc->hwdesc32.CNTR = xfer_count;
  672. txx9dmac_desc_set_nosimple(ddev, desc, 4, 4,
  673. dc->ccr | TXX9_DMA_CCR_XFACT);
  674. }
  675. /*
  676. * The descriptors on tx_list are not reachable from
  677. * the dc->queue list or dc->active_list after a
  678. * submit. If we put all descriptors on active_list,
  679. * calling of callback on the completion will be more
  680. * complex.
  681. */
  682. if (!first) {
  683. first = desc;
  684. } else {
  685. desc_write_CHAR(dc, prev, desc->txd.phys);
  686. dma_sync_single_for_device(chan2parent(&dc->chan),
  687. prev->txd.phys, ddev->descsize,
  688. DMA_TO_DEVICE);
  689. list_add_tail(&desc->desc_node, &first->tx_list);
  690. }
  691. prev = desc;
  692. }
  693. /* Trigger interrupt after last block */
  694. if (flags & DMA_PREP_INTERRUPT)
  695. txx9dmac_desc_set_INTENT(ddev, prev);
  696. desc_write_CHAR(dc, prev, 0);
  697. dma_sync_single_for_device(chan2parent(&dc->chan),
  698. prev->txd.phys, ddev->descsize,
  699. DMA_TO_DEVICE);
  700. first->txd.flags = flags;
  701. first->len = len;
  702. return &first->txd;
  703. }
  704. static struct dma_async_tx_descriptor *
  705. txx9dmac_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  706. unsigned int sg_len, enum dma_transfer_direction direction,
  707. unsigned long flags, void *context)
  708. {
  709. struct txx9dmac_chan *dc = to_txx9dmac_chan(chan);
  710. struct txx9dmac_dev *ddev = dc->ddev;
  711. struct txx9dmac_slave *ds = chan->private;
  712. struct txx9dmac_desc *prev;
  713. struct txx9dmac_desc *first;
  714. unsigned int i;
  715. struct scatterlist *sg;
  716. dev_vdbg(chan2dev(chan), "prep_dma_slave\n");
  717. BUG_ON(!ds || !ds->reg_width);
  718. if (ds->tx_reg)
  719. BUG_ON(direction != DMA_MEM_TO_DEV);
  720. else
  721. BUG_ON(direction != DMA_DEV_TO_MEM);
  722. if (unlikely(!sg_len))
  723. return NULL;
  724. prev = first = NULL;
  725. for_each_sg(sgl, sg, sg_len, i) {
  726. struct txx9dmac_desc *desc;
  727. dma_addr_t mem;
  728. u32 sai, dai;
  729. desc = txx9dmac_desc_get(dc);
  730. if (!desc) {
  731. txx9dmac_desc_put(dc, first);
  732. return NULL;
  733. }
  734. mem = sg_dma_address(sg);
  735. if (__is_dmac64(ddev)) {
  736. if (direction == DMA_MEM_TO_DEV) {
  737. desc->hwdesc.SAR = mem;
  738. desc->hwdesc.DAR = ds->tx_reg;
  739. } else {
  740. desc->hwdesc.SAR = ds->rx_reg;
  741. desc->hwdesc.DAR = mem;
  742. }
  743. desc->hwdesc.CNTR = sg_dma_len(sg);
  744. } else {
  745. if (direction == DMA_MEM_TO_DEV) {
  746. desc->hwdesc32.SAR = mem;
  747. desc->hwdesc32.DAR = ds->tx_reg;
  748. } else {
  749. desc->hwdesc32.SAR = ds->rx_reg;
  750. desc->hwdesc32.DAR = mem;
  751. }
  752. desc->hwdesc32.CNTR = sg_dma_len(sg);
  753. }
  754. if (direction == DMA_MEM_TO_DEV) {
  755. sai = ds->reg_width;
  756. dai = 0;
  757. } else {
  758. sai = 0;
  759. dai = ds->reg_width;
  760. }
  761. txx9dmac_desc_set_nosimple(ddev, desc, sai, dai,
  762. dc->ccr | TXX9_DMA_CCR_XFACT);
  763. if (!first) {
  764. first = desc;
  765. } else {
  766. desc_write_CHAR(dc, prev, desc->txd.phys);
  767. dma_sync_single_for_device(chan2parent(&dc->chan),
  768. prev->txd.phys,
  769. ddev->descsize,
  770. DMA_TO_DEVICE);
  771. list_add_tail(&desc->desc_node, &first->tx_list);
  772. }
  773. prev = desc;
  774. }
  775. /* Trigger interrupt after last block */
  776. if (flags & DMA_PREP_INTERRUPT)
  777. txx9dmac_desc_set_INTENT(ddev, prev);
  778. desc_write_CHAR(dc, prev, 0);
  779. dma_sync_single_for_device(chan2parent(&dc->chan),
  780. prev->txd.phys, ddev->descsize,
  781. DMA_TO_DEVICE);
  782. first->txd.flags = flags;
  783. first->len = 0;
  784. return &first->txd;
  785. }
  786. static int txx9dmac_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  787. unsigned long arg)
  788. {
  789. struct txx9dmac_chan *dc = to_txx9dmac_chan(chan);
  790. struct txx9dmac_desc *desc, *_desc;
  791. LIST_HEAD(list);
  792. /* Only supports DMA_TERMINATE_ALL */
  793. if (cmd != DMA_TERMINATE_ALL)
  794. return -EINVAL;
  795. dev_vdbg(chan2dev(chan), "terminate_all\n");
  796. spin_lock_bh(&dc->lock);
  797. txx9dmac_reset_chan(dc);
  798. /* active_list entries will end up before queued entries */
  799. list_splice_init(&dc->queue, &list);
  800. list_splice_init(&dc->active_list, &list);
  801. spin_unlock_bh(&dc->lock);
  802. /* Flush all pending and queued descriptors */
  803. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  804. txx9dmac_descriptor_complete(dc, desc);
  805. return 0;
  806. }
  807. static enum dma_status
  808. txx9dmac_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
  809. struct dma_tx_state *txstate)
  810. {
  811. struct txx9dmac_chan *dc = to_txx9dmac_chan(chan);
  812. enum dma_status ret;
  813. ret = dma_cookie_status(chan, cookie, txstate);
  814. if (ret == DMA_COMPLETE)
  815. return DMA_COMPLETE;
  816. spin_lock_bh(&dc->lock);
  817. txx9dmac_scan_descriptors(dc);
  818. spin_unlock_bh(&dc->lock);
  819. return dma_cookie_status(chan, cookie, txstate);
  820. }
  821. static void txx9dmac_chain_dynamic(struct txx9dmac_chan *dc,
  822. struct txx9dmac_desc *prev)
  823. {
  824. struct txx9dmac_dev *ddev = dc->ddev;
  825. struct txx9dmac_desc *desc;
  826. LIST_HEAD(list);
  827. prev = txx9dmac_last_child(prev);
  828. txx9dmac_dequeue(dc, &list);
  829. desc = list_entry(list.next, struct txx9dmac_desc, desc_node);
  830. desc_write_CHAR(dc, prev, desc->txd.phys);
  831. dma_sync_single_for_device(chan2parent(&dc->chan),
  832. prev->txd.phys, ddev->descsize,
  833. DMA_TO_DEVICE);
  834. mmiowb();
  835. if (!(channel_readl(dc, CSR) & TXX9_DMA_CSR_CHNEN) &&
  836. channel_read_CHAR(dc) == prev->txd.phys)
  837. /* Restart chain DMA */
  838. channel_write_CHAR(dc, desc->txd.phys);
  839. list_splice_tail(&list, &dc->active_list);
  840. }
  841. static void txx9dmac_issue_pending(struct dma_chan *chan)
  842. {
  843. struct txx9dmac_chan *dc = to_txx9dmac_chan(chan);
  844. spin_lock_bh(&dc->lock);
  845. if (!list_empty(&dc->active_list))
  846. txx9dmac_scan_descriptors(dc);
  847. if (!list_empty(&dc->queue)) {
  848. if (list_empty(&dc->active_list)) {
  849. txx9dmac_dequeue(dc, &dc->active_list);
  850. txx9dmac_dostart(dc, txx9dmac_first_active(dc));
  851. } else if (txx9_dma_have_SMPCHN()) {
  852. struct txx9dmac_desc *prev = txx9dmac_last_active(dc);
  853. if (!(prev->txd.flags & DMA_PREP_INTERRUPT) ||
  854. txx9dmac_chan_INTENT(dc))
  855. txx9dmac_chain_dynamic(dc, prev);
  856. }
  857. }
  858. spin_unlock_bh(&dc->lock);
  859. }
  860. static int txx9dmac_alloc_chan_resources(struct dma_chan *chan)
  861. {
  862. struct txx9dmac_chan *dc = to_txx9dmac_chan(chan);
  863. struct txx9dmac_slave *ds = chan->private;
  864. struct txx9dmac_desc *desc;
  865. int i;
  866. dev_vdbg(chan2dev(chan), "alloc_chan_resources\n");
  867. /* ASSERT: channel is idle */
  868. if (channel_readl(dc, CSR) & TXX9_DMA_CSR_XFACT) {
  869. dev_dbg(chan2dev(chan), "DMA channel not idle?\n");
  870. return -EIO;
  871. }
  872. dma_cookie_init(chan);
  873. dc->ccr = TXX9_DMA_CCR_IMMCHN | TXX9_DMA_CCR_INTENE | CCR_LE;
  874. txx9dmac_chan_set_SMPCHN(dc);
  875. if (!txx9_dma_have_SMPCHN() || (dc->ccr & TXX9_DMA_CCR_SMPCHN))
  876. dc->ccr |= TXX9_DMA_CCR_INTENC;
  877. if (chan->device->device_prep_dma_memcpy) {
  878. if (ds)
  879. return -EINVAL;
  880. dc->ccr |= TXX9_DMA_CCR_XFSZ_X8;
  881. } else {
  882. if (!ds ||
  883. (ds->tx_reg && ds->rx_reg) || (!ds->tx_reg && !ds->rx_reg))
  884. return -EINVAL;
  885. dc->ccr |= TXX9_DMA_CCR_EXTRQ |
  886. TXX9_DMA_CCR_XFSZ(__ffs(ds->reg_width));
  887. txx9dmac_chan_set_INTENT(dc);
  888. }
  889. spin_lock_bh(&dc->lock);
  890. i = dc->descs_allocated;
  891. while (dc->descs_allocated < TXX9_DMA_INITIAL_DESC_COUNT) {
  892. spin_unlock_bh(&dc->lock);
  893. desc = txx9dmac_desc_alloc(dc, GFP_KERNEL);
  894. if (!desc) {
  895. dev_info(chan2dev(chan),
  896. "only allocated %d descriptors\n", i);
  897. spin_lock_bh(&dc->lock);
  898. break;
  899. }
  900. txx9dmac_desc_put(dc, desc);
  901. spin_lock_bh(&dc->lock);
  902. i = ++dc->descs_allocated;
  903. }
  904. spin_unlock_bh(&dc->lock);
  905. dev_dbg(chan2dev(chan),
  906. "alloc_chan_resources allocated %d descriptors\n", i);
  907. return i;
  908. }
  909. static void txx9dmac_free_chan_resources(struct dma_chan *chan)
  910. {
  911. struct txx9dmac_chan *dc = to_txx9dmac_chan(chan);
  912. struct txx9dmac_dev *ddev = dc->ddev;
  913. struct txx9dmac_desc *desc, *_desc;
  914. LIST_HEAD(list);
  915. dev_dbg(chan2dev(chan), "free_chan_resources (descs allocated=%u)\n",
  916. dc->descs_allocated);
  917. /* ASSERT: channel is idle */
  918. BUG_ON(!list_empty(&dc->active_list));
  919. BUG_ON(!list_empty(&dc->queue));
  920. BUG_ON(channel_readl(dc, CSR) & TXX9_DMA_CSR_XFACT);
  921. spin_lock_bh(&dc->lock);
  922. list_splice_init(&dc->free_list, &list);
  923. dc->descs_allocated = 0;
  924. spin_unlock_bh(&dc->lock);
  925. list_for_each_entry_safe(desc, _desc, &list, desc_node) {
  926. dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
  927. dma_unmap_single(chan2parent(chan), desc->txd.phys,
  928. ddev->descsize, DMA_TO_DEVICE);
  929. kfree(desc);
  930. }
  931. dev_vdbg(chan2dev(chan), "free_chan_resources done\n");
  932. }
  933. /*----------------------------------------------------------------------*/
  934. static void txx9dmac_off(struct txx9dmac_dev *ddev)
  935. {
  936. dma_writel(ddev, MCR, 0);
  937. mmiowb();
  938. }
  939. static int __init txx9dmac_chan_probe(struct platform_device *pdev)
  940. {
  941. struct txx9dmac_chan_platform_data *cpdata =
  942. dev_get_platdata(&pdev->dev);
  943. struct platform_device *dmac_dev = cpdata->dmac_dev;
  944. struct txx9dmac_platform_data *pdata = dev_get_platdata(&dmac_dev->dev);
  945. struct txx9dmac_chan *dc;
  946. int err;
  947. int ch = pdev->id % TXX9_DMA_MAX_NR_CHANNELS;
  948. int irq;
  949. dc = devm_kzalloc(&pdev->dev, sizeof(*dc), GFP_KERNEL);
  950. if (!dc)
  951. return -ENOMEM;
  952. dc->dma.dev = &pdev->dev;
  953. dc->dma.device_alloc_chan_resources = txx9dmac_alloc_chan_resources;
  954. dc->dma.device_free_chan_resources = txx9dmac_free_chan_resources;
  955. dc->dma.device_control = txx9dmac_control;
  956. dc->dma.device_tx_status = txx9dmac_tx_status;
  957. dc->dma.device_issue_pending = txx9dmac_issue_pending;
  958. if (pdata && pdata->memcpy_chan == ch) {
  959. dc->dma.device_prep_dma_memcpy = txx9dmac_prep_dma_memcpy;
  960. dma_cap_set(DMA_MEMCPY, dc->dma.cap_mask);
  961. } else {
  962. dc->dma.device_prep_slave_sg = txx9dmac_prep_slave_sg;
  963. dma_cap_set(DMA_SLAVE, dc->dma.cap_mask);
  964. dma_cap_set(DMA_PRIVATE, dc->dma.cap_mask);
  965. }
  966. INIT_LIST_HEAD(&dc->dma.channels);
  967. dc->ddev = platform_get_drvdata(dmac_dev);
  968. if (dc->ddev->irq < 0) {
  969. irq = platform_get_irq(pdev, 0);
  970. if (irq < 0)
  971. return irq;
  972. tasklet_init(&dc->tasklet, txx9dmac_chan_tasklet,
  973. (unsigned long)dc);
  974. dc->irq = irq;
  975. err = devm_request_irq(&pdev->dev, dc->irq,
  976. txx9dmac_chan_interrupt, 0, dev_name(&pdev->dev), dc);
  977. if (err)
  978. return err;
  979. } else
  980. dc->irq = -1;
  981. dc->ddev->chan[ch] = dc;
  982. dc->chan.device = &dc->dma;
  983. list_add_tail(&dc->chan.device_node, &dc->chan.device->channels);
  984. dma_cookie_init(&dc->chan);
  985. if (is_dmac64(dc))
  986. dc->ch_regs = &__txx9dmac_regs(dc->ddev)->CHAN[ch];
  987. else
  988. dc->ch_regs = &__txx9dmac_regs32(dc->ddev)->CHAN[ch];
  989. spin_lock_init(&dc->lock);
  990. INIT_LIST_HEAD(&dc->active_list);
  991. INIT_LIST_HEAD(&dc->queue);
  992. INIT_LIST_HEAD(&dc->free_list);
  993. txx9dmac_reset_chan(dc);
  994. platform_set_drvdata(pdev, dc);
  995. err = dma_async_device_register(&dc->dma);
  996. if (err)
  997. return err;
  998. dev_dbg(&pdev->dev, "TXx9 DMA Channel (dma%d%s%s)\n",
  999. dc->dma.dev_id,
  1000. dma_has_cap(DMA_MEMCPY, dc->dma.cap_mask) ? " memcpy" : "",
  1001. dma_has_cap(DMA_SLAVE, dc->dma.cap_mask) ? " slave" : "");
  1002. return 0;
  1003. }
  1004. static int txx9dmac_chan_remove(struct platform_device *pdev)
  1005. {
  1006. struct txx9dmac_chan *dc = platform_get_drvdata(pdev);
  1007. dma_async_device_unregister(&dc->dma);
  1008. if (dc->irq >= 0)
  1009. tasklet_kill(&dc->tasklet);
  1010. dc->ddev->chan[pdev->id % TXX9_DMA_MAX_NR_CHANNELS] = NULL;
  1011. return 0;
  1012. }
  1013. static int __init txx9dmac_probe(struct platform_device *pdev)
  1014. {
  1015. struct txx9dmac_platform_data *pdata = dev_get_platdata(&pdev->dev);
  1016. struct resource *io;
  1017. struct txx9dmac_dev *ddev;
  1018. u32 mcr;
  1019. int err;
  1020. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1021. if (!io)
  1022. return -EINVAL;
  1023. ddev = devm_kzalloc(&pdev->dev, sizeof(*ddev), GFP_KERNEL);
  1024. if (!ddev)
  1025. return -ENOMEM;
  1026. if (!devm_request_mem_region(&pdev->dev, io->start, resource_size(io),
  1027. dev_name(&pdev->dev)))
  1028. return -EBUSY;
  1029. ddev->regs = devm_ioremap(&pdev->dev, io->start, resource_size(io));
  1030. if (!ddev->regs)
  1031. return -ENOMEM;
  1032. ddev->have_64bit_regs = pdata->have_64bit_regs;
  1033. if (__is_dmac64(ddev))
  1034. ddev->descsize = sizeof(struct txx9dmac_hwdesc);
  1035. else
  1036. ddev->descsize = sizeof(struct txx9dmac_hwdesc32);
  1037. /* force dma off, just in case */
  1038. txx9dmac_off(ddev);
  1039. ddev->irq = platform_get_irq(pdev, 0);
  1040. if (ddev->irq >= 0) {
  1041. tasklet_init(&ddev->tasklet, txx9dmac_tasklet,
  1042. (unsigned long)ddev);
  1043. err = devm_request_irq(&pdev->dev, ddev->irq,
  1044. txx9dmac_interrupt, 0, dev_name(&pdev->dev), ddev);
  1045. if (err)
  1046. return err;
  1047. }
  1048. mcr = TXX9_DMA_MCR_MSTEN | MCR_LE;
  1049. if (pdata && pdata->memcpy_chan >= 0)
  1050. mcr |= TXX9_DMA_MCR_FIFUM(pdata->memcpy_chan);
  1051. dma_writel(ddev, MCR, mcr);
  1052. platform_set_drvdata(pdev, ddev);
  1053. return 0;
  1054. }
  1055. static int txx9dmac_remove(struct platform_device *pdev)
  1056. {
  1057. struct txx9dmac_dev *ddev = platform_get_drvdata(pdev);
  1058. txx9dmac_off(ddev);
  1059. if (ddev->irq >= 0)
  1060. tasklet_kill(&ddev->tasklet);
  1061. return 0;
  1062. }
  1063. static void txx9dmac_shutdown(struct platform_device *pdev)
  1064. {
  1065. struct txx9dmac_dev *ddev = platform_get_drvdata(pdev);
  1066. txx9dmac_off(ddev);
  1067. }
  1068. static int txx9dmac_suspend_noirq(struct device *dev)
  1069. {
  1070. struct platform_device *pdev = to_platform_device(dev);
  1071. struct txx9dmac_dev *ddev = platform_get_drvdata(pdev);
  1072. txx9dmac_off(ddev);
  1073. return 0;
  1074. }
  1075. static int txx9dmac_resume_noirq(struct device *dev)
  1076. {
  1077. struct platform_device *pdev = to_platform_device(dev);
  1078. struct txx9dmac_dev *ddev = platform_get_drvdata(pdev);
  1079. struct txx9dmac_platform_data *pdata = dev_get_platdata(&pdev->dev);
  1080. u32 mcr;
  1081. mcr = TXX9_DMA_MCR_MSTEN | MCR_LE;
  1082. if (pdata && pdata->memcpy_chan >= 0)
  1083. mcr |= TXX9_DMA_MCR_FIFUM(pdata->memcpy_chan);
  1084. dma_writel(ddev, MCR, mcr);
  1085. return 0;
  1086. }
  1087. static const struct dev_pm_ops txx9dmac_dev_pm_ops = {
  1088. .suspend_noirq = txx9dmac_suspend_noirq,
  1089. .resume_noirq = txx9dmac_resume_noirq,
  1090. };
  1091. static struct platform_driver txx9dmac_chan_driver = {
  1092. .remove = txx9dmac_chan_remove,
  1093. .driver = {
  1094. .name = "txx9dmac-chan",
  1095. },
  1096. };
  1097. static struct platform_driver txx9dmac_driver = {
  1098. .remove = txx9dmac_remove,
  1099. .shutdown = txx9dmac_shutdown,
  1100. .driver = {
  1101. .name = "txx9dmac",
  1102. .pm = &txx9dmac_dev_pm_ops,
  1103. },
  1104. };
  1105. static int __init txx9dmac_init(void)
  1106. {
  1107. int rc;
  1108. rc = platform_driver_probe(&txx9dmac_driver, txx9dmac_probe);
  1109. if (!rc) {
  1110. rc = platform_driver_probe(&txx9dmac_chan_driver,
  1111. txx9dmac_chan_probe);
  1112. if (rc)
  1113. platform_driver_unregister(&txx9dmac_driver);
  1114. }
  1115. return rc;
  1116. }
  1117. module_init(txx9dmac_init);
  1118. static void __exit txx9dmac_exit(void)
  1119. {
  1120. platform_driver_unregister(&txx9dmac_chan_driver);
  1121. platform_driver_unregister(&txx9dmac_driver);
  1122. }
  1123. module_exit(txx9dmac_exit);
  1124. MODULE_LICENSE("GPL");
  1125. MODULE_DESCRIPTION("TXx9 DMA Controller driver");
  1126. MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
  1127. MODULE_ALIAS("platform:txx9dmac");
  1128. MODULE_ALIAS("platform:txx9dmac-chan");