txx9dmac.c 35 KB

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