cppi41.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. #include <linux/delay.h>
  2. #include <linux/dmaengine.h>
  3. #include <linux/dma-mapping.h>
  4. #include <linux/platform_device.h>
  5. #include <linux/module.h>
  6. #include <linux/of.h>
  7. #include <linux/slab.h>
  8. #include <linux/of_dma.h>
  9. #include <linux/of_irq.h>
  10. #include <linux/dmapool.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/of_address.h>
  13. #include <linux/pm_runtime.h>
  14. #include "dmaengine.h"
  15. #define DESC_TYPE 27
  16. #define DESC_TYPE_HOST 0x10
  17. #define DESC_TYPE_TEARD 0x13
  18. #define TD_DESC_IS_RX (1 << 16)
  19. #define TD_DESC_DMA_NUM 10
  20. #define DESC_LENGTH_BITS_NUM 21
  21. #define DESC_TYPE_USB (5 << 26)
  22. #define DESC_PD_COMPLETE (1 << 31)
  23. /* DMA engine */
  24. #define DMA_TDFDQ 4
  25. #define DMA_TXGCR(x) (0x800 + (x) * 0x20)
  26. #define DMA_RXGCR(x) (0x808 + (x) * 0x20)
  27. #define RXHPCRA0 4
  28. #define GCR_CHAN_ENABLE (1 << 31)
  29. #define GCR_TEARDOWN (1 << 30)
  30. #define GCR_STARV_RETRY (1 << 24)
  31. #define GCR_DESC_TYPE_HOST (1 << 14)
  32. /* DMA scheduler */
  33. #define DMA_SCHED_CTRL 0
  34. #define DMA_SCHED_CTRL_EN (1 << 31)
  35. #define DMA_SCHED_WORD(x) ((x) * 4 + 0x800)
  36. #define SCHED_ENTRY0_CHAN(x) ((x) << 0)
  37. #define SCHED_ENTRY0_IS_RX (1 << 7)
  38. #define SCHED_ENTRY1_CHAN(x) ((x) << 8)
  39. #define SCHED_ENTRY1_IS_RX (1 << 15)
  40. #define SCHED_ENTRY2_CHAN(x) ((x) << 16)
  41. #define SCHED_ENTRY2_IS_RX (1 << 23)
  42. #define SCHED_ENTRY3_CHAN(x) ((x) << 24)
  43. #define SCHED_ENTRY3_IS_RX (1 << 31)
  44. /* Queue manager */
  45. /* 4 KiB of memory for descriptors, 2 for each endpoint */
  46. #define ALLOC_DECS_NUM 128
  47. #define DESCS_AREAS 1
  48. #define TOTAL_DESCS_NUM (ALLOC_DECS_NUM * DESCS_AREAS)
  49. #define QMGR_SCRATCH_SIZE (TOTAL_DESCS_NUM * 4)
  50. #define QMGR_LRAM0_BASE 0x80
  51. #define QMGR_LRAM_SIZE 0x84
  52. #define QMGR_LRAM1_BASE 0x88
  53. #define QMGR_MEMBASE(x) (0x1000 + (x) * 0x10)
  54. #define QMGR_MEMCTRL(x) (0x1004 + (x) * 0x10)
  55. #define QMGR_MEMCTRL_IDX_SH 16
  56. #define QMGR_MEMCTRL_DESC_SH 8
  57. #define QMGR_NUM_PEND 5
  58. #define QMGR_PEND(x) (0x90 + (x) * 4)
  59. #define QMGR_PENDING_SLOT_Q(x) (x / 32)
  60. #define QMGR_PENDING_BIT_Q(x) (x % 32)
  61. #define QMGR_QUEUE_A(n) (0x2000 + (n) * 0x10)
  62. #define QMGR_QUEUE_B(n) (0x2004 + (n) * 0x10)
  63. #define QMGR_QUEUE_C(n) (0x2008 + (n) * 0x10)
  64. #define QMGR_QUEUE_D(n) (0x200c + (n) * 0x10)
  65. /* Glue layer specific */
  66. /* USBSS / USB AM335x */
  67. #define USBSS_IRQ_STATUS 0x28
  68. #define USBSS_IRQ_ENABLER 0x2c
  69. #define USBSS_IRQ_CLEARR 0x30
  70. #define USBSS_IRQ_PD_COMP (1 << 2)
  71. /* Packet Descriptor */
  72. #define PD2_ZERO_LENGTH (1 << 19)
  73. struct cppi41_channel {
  74. struct dma_chan chan;
  75. struct dma_async_tx_descriptor txd;
  76. struct cppi41_dd *cdd;
  77. struct cppi41_desc *desc;
  78. dma_addr_t desc_phys;
  79. void __iomem *gcr_reg;
  80. int is_tx;
  81. u32 residue;
  82. unsigned int q_num;
  83. unsigned int q_comp_num;
  84. unsigned int port_num;
  85. unsigned td_retry;
  86. unsigned td_queued:1;
  87. unsigned td_seen:1;
  88. unsigned td_desc_seen:1;
  89. };
  90. struct cppi41_desc {
  91. u32 pd0;
  92. u32 pd1;
  93. u32 pd2;
  94. u32 pd3;
  95. u32 pd4;
  96. u32 pd5;
  97. u32 pd6;
  98. u32 pd7;
  99. } __aligned(32);
  100. struct chan_queues {
  101. u16 submit;
  102. u16 complete;
  103. };
  104. struct cppi41_dd {
  105. struct dma_device ddev;
  106. void *qmgr_scratch;
  107. dma_addr_t scratch_phys;
  108. struct cppi41_desc *cd;
  109. dma_addr_t descs_phys;
  110. u32 first_td_desc;
  111. struct cppi41_channel *chan_busy[ALLOC_DECS_NUM];
  112. void __iomem *usbss_mem;
  113. void __iomem *ctrl_mem;
  114. void __iomem *sched_mem;
  115. void __iomem *qmgr_mem;
  116. unsigned int irq;
  117. const struct chan_queues *queues_rx;
  118. const struct chan_queues *queues_tx;
  119. struct chan_queues td_queue;
  120. /* context for suspend/resume */
  121. unsigned int dma_tdfdq;
  122. };
  123. #define FIST_COMPLETION_QUEUE 93
  124. static struct chan_queues usb_queues_tx[] = {
  125. /* USB0 ENDP 1 */
  126. [ 0] = { .submit = 32, .complete = 93},
  127. [ 1] = { .submit = 34, .complete = 94},
  128. [ 2] = { .submit = 36, .complete = 95},
  129. [ 3] = { .submit = 38, .complete = 96},
  130. [ 4] = { .submit = 40, .complete = 97},
  131. [ 5] = { .submit = 42, .complete = 98},
  132. [ 6] = { .submit = 44, .complete = 99},
  133. [ 7] = { .submit = 46, .complete = 100},
  134. [ 8] = { .submit = 48, .complete = 101},
  135. [ 9] = { .submit = 50, .complete = 102},
  136. [10] = { .submit = 52, .complete = 103},
  137. [11] = { .submit = 54, .complete = 104},
  138. [12] = { .submit = 56, .complete = 105},
  139. [13] = { .submit = 58, .complete = 106},
  140. [14] = { .submit = 60, .complete = 107},
  141. /* USB1 ENDP1 */
  142. [15] = { .submit = 62, .complete = 125},
  143. [16] = { .submit = 64, .complete = 126},
  144. [17] = { .submit = 66, .complete = 127},
  145. [18] = { .submit = 68, .complete = 128},
  146. [19] = { .submit = 70, .complete = 129},
  147. [20] = { .submit = 72, .complete = 130},
  148. [21] = { .submit = 74, .complete = 131},
  149. [22] = { .submit = 76, .complete = 132},
  150. [23] = { .submit = 78, .complete = 133},
  151. [24] = { .submit = 80, .complete = 134},
  152. [25] = { .submit = 82, .complete = 135},
  153. [26] = { .submit = 84, .complete = 136},
  154. [27] = { .submit = 86, .complete = 137},
  155. [28] = { .submit = 88, .complete = 138},
  156. [29] = { .submit = 90, .complete = 139},
  157. };
  158. static const struct chan_queues usb_queues_rx[] = {
  159. /* USB0 ENDP 1 */
  160. [ 0] = { .submit = 1, .complete = 109},
  161. [ 1] = { .submit = 2, .complete = 110},
  162. [ 2] = { .submit = 3, .complete = 111},
  163. [ 3] = { .submit = 4, .complete = 112},
  164. [ 4] = { .submit = 5, .complete = 113},
  165. [ 5] = { .submit = 6, .complete = 114},
  166. [ 6] = { .submit = 7, .complete = 115},
  167. [ 7] = { .submit = 8, .complete = 116},
  168. [ 8] = { .submit = 9, .complete = 117},
  169. [ 9] = { .submit = 10, .complete = 118},
  170. [10] = { .submit = 11, .complete = 119},
  171. [11] = { .submit = 12, .complete = 120},
  172. [12] = { .submit = 13, .complete = 121},
  173. [13] = { .submit = 14, .complete = 122},
  174. [14] = { .submit = 15, .complete = 123},
  175. /* USB1 ENDP 1 */
  176. [15] = { .submit = 16, .complete = 141},
  177. [16] = { .submit = 17, .complete = 142},
  178. [17] = { .submit = 18, .complete = 143},
  179. [18] = { .submit = 19, .complete = 144},
  180. [19] = { .submit = 20, .complete = 145},
  181. [20] = { .submit = 21, .complete = 146},
  182. [21] = { .submit = 22, .complete = 147},
  183. [22] = { .submit = 23, .complete = 148},
  184. [23] = { .submit = 24, .complete = 149},
  185. [24] = { .submit = 25, .complete = 150},
  186. [25] = { .submit = 26, .complete = 151},
  187. [26] = { .submit = 27, .complete = 152},
  188. [27] = { .submit = 28, .complete = 153},
  189. [28] = { .submit = 29, .complete = 154},
  190. [29] = { .submit = 30, .complete = 155},
  191. };
  192. struct cppi_glue_infos {
  193. irqreturn_t (*isr)(int irq, void *data);
  194. const struct chan_queues *queues_rx;
  195. const struct chan_queues *queues_tx;
  196. struct chan_queues td_queue;
  197. };
  198. static struct cppi41_channel *to_cpp41_chan(struct dma_chan *c)
  199. {
  200. return container_of(c, struct cppi41_channel, chan);
  201. }
  202. static struct cppi41_channel *desc_to_chan(struct cppi41_dd *cdd, u32 desc)
  203. {
  204. struct cppi41_channel *c;
  205. u32 descs_size;
  206. u32 desc_num;
  207. descs_size = sizeof(struct cppi41_desc) * ALLOC_DECS_NUM;
  208. if (!((desc >= cdd->descs_phys) &&
  209. (desc < (cdd->descs_phys + descs_size)))) {
  210. return NULL;
  211. }
  212. desc_num = (desc - cdd->descs_phys) / sizeof(struct cppi41_desc);
  213. BUG_ON(desc_num >= ALLOC_DECS_NUM);
  214. c = cdd->chan_busy[desc_num];
  215. cdd->chan_busy[desc_num] = NULL;
  216. return c;
  217. }
  218. static void cppi_writel(u32 val, void *__iomem *mem)
  219. {
  220. __raw_writel(val, mem);
  221. }
  222. static u32 cppi_readl(void *__iomem *mem)
  223. {
  224. return __raw_readl(mem);
  225. }
  226. static u32 pd_trans_len(u32 val)
  227. {
  228. return val & ((1 << (DESC_LENGTH_BITS_NUM + 1)) - 1);
  229. }
  230. static u32 cppi41_pop_desc(struct cppi41_dd *cdd, unsigned queue_num)
  231. {
  232. u32 desc;
  233. desc = cppi_readl(cdd->qmgr_mem + QMGR_QUEUE_D(queue_num));
  234. desc &= ~0x1f;
  235. return desc;
  236. }
  237. static irqreturn_t cppi41_irq(int irq, void *data)
  238. {
  239. struct cppi41_dd *cdd = data;
  240. struct cppi41_channel *c;
  241. u32 status;
  242. int i;
  243. status = cppi_readl(cdd->usbss_mem + USBSS_IRQ_STATUS);
  244. if (!(status & USBSS_IRQ_PD_COMP))
  245. return IRQ_NONE;
  246. cppi_writel(status, cdd->usbss_mem + USBSS_IRQ_STATUS);
  247. for (i = QMGR_PENDING_SLOT_Q(FIST_COMPLETION_QUEUE); i < QMGR_NUM_PEND;
  248. i++) {
  249. u32 val;
  250. u32 q_num;
  251. val = cppi_readl(cdd->qmgr_mem + QMGR_PEND(i));
  252. if (i == QMGR_PENDING_SLOT_Q(FIST_COMPLETION_QUEUE) && val) {
  253. u32 mask;
  254. /* set corresponding bit for completetion Q 93 */
  255. mask = 1 << QMGR_PENDING_BIT_Q(FIST_COMPLETION_QUEUE);
  256. /* not set all bits for queues less than Q 93 */
  257. mask--;
  258. /* now invert and keep only Q 93+ set */
  259. val &= ~mask;
  260. }
  261. if (val)
  262. __iormb();
  263. while (val) {
  264. u32 desc, len;
  265. q_num = __fls(val);
  266. val &= ~(1 << q_num);
  267. q_num += 32 * i;
  268. desc = cppi41_pop_desc(cdd, q_num);
  269. c = desc_to_chan(cdd, desc);
  270. if (WARN_ON(!c)) {
  271. pr_err("%s() q %d desc %08x\n", __func__,
  272. q_num, desc);
  273. continue;
  274. }
  275. if (c->desc->pd2 & PD2_ZERO_LENGTH)
  276. len = 0;
  277. else
  278. len = pd_trans_len(c->desc->pd0);
  279. c->residue = pd_trans_len(c->desc->pd6) - len;
  280. dma_cookie_complete(&c->txd);
  281. c->txd.callback(c->txd.callback_param);
  282. }
  283. }
  284. return IRQ_HANDLED;
  285. }
  286. static dma_cookie_t cppi41_tx_submit(struct dma_async_tx_descriptor *tx)
  287. {
  288. dma_cookie_t cookie;
  289. cookie = dma_cookie_assign(tx);
  290. return cookie;
  291. }
  292. static int cppi41_dma_alloc_chan_resources(struct dma_chan *chan)
  293. {
  294. struct cppi41_channel *c = to_cpp41_chan(chan);
  295. dma_cookie_init(chan);
  296. dma_async_tx_descriptor_init(&c->txd, chan);
  297. c->txd.tx_submit = cppi41_tx_submit;
  298. if (!c->is_tx)
  299. cppi_writel(c->q_num, c->gcr_reg + RXHPCRA0);
  300. return 0;
  301. }
  302. static void cppi41_dma_free_chan_resources(struct dma_chan *chan)
  303. {
  304. }
  305. static enum dma_status cppi41_dma_tx_status(struct dma_chan *chan,
  306. dma_cookie_t cookie, struct dma_tx_state *txstate)
  307. {
  308. struct cppi41_channel *c = to_cpp41_chan(chan);
  309. enum dma_status ret;
  310. /* lock */
  311. ret = dma_cookie_status(chan, cookie, txstate);
  312. if (txstate && ret == DMA_COMPLETE)
  313. txstate->residue = c->residue;
  314. /* unlock */
  315. return ret;
  316. }
  317. static void push_desc_queue(struct cppi41_channel *c)
  318. {
  319. struct cppi41_dd *cdd = c->cdd;
  320. u32 desc_num;
  321. u32 desc_phys;
  322. u32 reg;
  323. desc_phys = lower_32_bits(c->desc_phys);
  324. desc_num = (desc_phys - cdd->descs_phys) / sizeof(struct cppi41_desc);
  325. WARN_ON(cdd->chan_busy[desc_num]);
  326. cdd->chan_busy[desc_num] = c;
  327. reg = (sizeof(struct cppi41_desc) - 24) / 4;
  328. reg |= desc_phys;
  329. cppi_writel(reg, cdd->qmgr_mem + QMGR_QUEUE_D(c->q_num));
  330. }
  331. static void cppi41_dma_issue_pending(struct dma_chan *chan)
  332. {
  333. struct cppi41_channel *c = to_cpp41_chan(chan);
  334. u32 reg;
  335. c->residue = 0;
  336. reg = GCR_CHAN_ENABLE;
  337. if (!c->is_tx) {
  338. reg |= GCR_STARV_RETRY;
  339. reg |= GCR_DESC_TYPE_HOST;
  340. reg |= c->q_comp_num;
  341. }
  342. cppi_writel(reg, c->gcr_reg);
  343. /*
  344. * We don't use writel() but __raw_writel() so we have to make sure
  345. * that the DMA descriptor in coherent memory made to the main memory
  346. * before starting the dma engine.
  347. */
  348. __iowmb();
  349. push_desc_queue(c);
  350. }
  351. static u32 get_host_pd0(u32 length)
  352. {
  353. u32 reg;
  354. reg = DESC_TYPE_HOST << DESC_TYPE;
  355. reg |= length;
  356. return reg;
  357. }
  358. static u32 get_host_pd1(struct cppi41_channel *c)
  359. {
  360. u32 reg;
  361. reg = 0;
  362. return reg;
  363. }
  364. static u32 get_host_pd2(struct cppi41_channel *c)
  365. {
  366. u32 reg;
  367. reg = DESC_TYPE_USB;
  368. reg |= c->q_comp_num;
  369. return reg;
  370. }
  371. static u32 get_host_pd3(u32 length)
  372. {
  373. u32 reg;
  374. /* PD3 = packet size */
  375. reg = length;
  376. return reg;
  377. }
  378. static u32 get_host_pd6(u32 length)
  379. {
  380. u32 reg;
  381. /* PD6 buffer size */
  382. reg = DESC_PD_COMPLETE;
  383. reg |= length;
  384. return reg;
  385. }
  386. static u32 get_host_pd4_or_7(u32 addr)
  387. {
  388. u32 reg;
  389. reg = addr;
  390. return reg;
  391. }
  392. static u32 get_host_pd5(void)
  393. {
  394. u32 reg;
  395. reg = 0;
  396. return reg;
  397. }
  398. static struct dma_async_tx_descriptor *cppi41_dma_prep_slave_sg(
  399. struct dma_chan *chan, struct scatterlist *sgl, unsigned sg_len,
  400. enum dma_transfer_direction dir, unsigned long tx_flags, void *context)
  401. {
  402. struct cppi41_channel *c = to_cpp41_chan(chan);
  403. struct cppi41_desc *d;
  404. struct scatterlist *sg;
  405. unsigned int i;
  406. unsigned int num;
  407. num = 0;
  408. d = c->desc;
  409. for_each_sg(sgl, sg, sg_len, i) {
  410. u32 addr;
  411. u32 len;
  412. /* We need to use more than one desc once musb supports sg */
  413. BUG_ON(num > 0);
  414. addr = lower_32_bits(sg_dma_address(sg));
  415. len = sg_dma_len(sg);
  416. d->pd0 = get_host_pd0(len);
  417. d->pd1 = get_host_pd1(c);
  418. d->pd2 = get_host_pd2(c);
  419. d->pd3 = get_host_pd3(len);
  420. d->pd4 = get_host_pd4_or_7(addr);
  421. d->pd5 = get_host_pd5();
  422. d->pd6 = get_host_pd6(len);
  423. d->pd7 = get_host_pd4_or_7(addr);
  424. d++;
  425. }
  426. return &c->txd;
  427. }
  428. static void cppi41_compute_td_desc(struct cppi41_desc *d)
  429. {
  430. d->pd0 = DESC_TYPE_TEARD << DESC_TYPE;
  431. }
  432. static int cppi41_tear_down_chan(struct cppi41_channel *c)
  433. {
  434. struct cppi41_dd *cdd = c->cdd;
  435. struct cppi41_desc *td;
  436. u32 reg;
  437. u32 desc_phys;
  438. u32 td_desc_phys;
  439. td = cdd->cd;
  440. td += cdd->first_td_desc;
  441. td_desc_phys = cdd->descs_phys;
  442. td_desc_phys += cdd->first_td_desc * sizeof(struct cppi41_desc);
  443. if (!c->td_queued) {
  444. cppi41_compute_td_desc(td);
  445. __iowmb();
  446. reg = (sizeof(struct cppi41_desc) - 24) / 4;
  447. reg |= td_desc_phys;
  448. cppi_writel(reg, cdd->qmgr_mem +
  449. QMGR_QUEUE_D(cdd->td_queue.submit));
  450. reg = GCR_CHAN_ENABLE;
  451. if (!c->is_tx) {
  452. reg |= GCR_STARV_RETRY;
  453. reg |= GCR_DESC_TYPE_HOST;
  454. reg |= c->q_comp_num;
  455. }
  456. reg |= GCR_TEARDOWN;
  457. cppi_writel(reg, c->gcr_reg);
  458. c->td_queued = 1;
  459. c->td_retry = 500;
  460. }
  461. if (!c->td_seen || !c->td_desc_seen) {
  462. desc_phys = cppi41_pop_desc(cdd, cdd->td_queue.complete);
  463. if (!desc_phys)
  464. desc_phys = cppi41_pop_desc(cdd, c->q_comp_num);
  465. if (desc_phys == c->desc_phys) {
  466. c->td_desc_seen = 1;
  467. } else if (desc_phys == td_desc_phys) {
  468. u32 pd0;
  469. __iormb();
  470. pd0 = td->pd0;
  471. WARN_ON((pd0 >> DESC_TYPE) != DESC_TYPE_TEARD);
  472. WARN_ON(!c->is_tx && !(pd0 & TD_DESC_IS_RX));
  473. WARN_ON((pd0 & 0x1f) != c->port_num);
  474. c->td_seen = 1;
  475. } else if (desc_phys) {
  476. WARN_ON_ONCE(1);
  477. }
  478. }
  479. c->td_retry--;
  480. /*
  481. * If the TX descriptor / channel is in use, the caller needs to poke
  482. * his TD bit multiple times. After that he hardware releases the
  483. * transfer descriptor followed by TD descriptor. Waiting seems not to
  484. * cause any difference.
  485. * RX seems to be thrown out right away. However once the TearDown
  486. * descriptor gets through we are done. If we have seens the transfer
  487. * descriptor before the TD we fetch it from enqueue, it has to be
  488. * there waiting for us.
  489. */
  490. if (!c->td_seen && c->td_retry) {
  491. udelay(1);
  492. return -EAGAIN;
  493. }
  494. WARN_ON(!c->td_retry);
  495. if (!c->td_desc_seen) {
  496. desc_phys = cppi41_pop_desc(cdd, c->q_num);
  497. if (!desc_phys)
  498. desc_phys = cppi41_pop_desc(cdd, c->q_comp_num);
  499. WARN_ON(!desc_phys);
  500. }
  501. c->td_queued = 0;
  502. c->td_seen = 0;
  503. c->td_desc_seen = 0;
  504. cppi_writel(0, c->gcr_reg);
  505. return 0;
  506. }
  507. static int cppi41_stop_chan(struct dma_chan *chan)
  508. {
  509. struct cppi41_channel *c = to_cpp41_chan(chan);
  510. struct cppi41_dd *cdd = c->cdd;
  511. u32 desc_num;
  512. u32 desc_phys;
  513. int ret;
  514. desc_phys = lower_32_bits(c->desc_phys);
  515. desc_num = (desc_phys - cdd->descs_phys) / sizeof(struct cppi41_desc);
  516. if (!cdd->chan_busy[desc_num])
  517. return 0;
  518. ret = cppi41_tear_down_chan(c);
  519. if (ret)
  520. return ret;
  521. WARN_ON(!cdd->chan_busy[desc_num]);
  522. cdd->chan_busy[desc_num] = NULL;
  523. return 0;
  524. }
  525. static void cleanup_chans(struct cppi41_dd *cdd)
  526. {
  527. while (!list_empty(&cdd->ddev.channels)) {
  528. struct cppi41_channel *cchan;
  529. cchan = list_first_entry(&cdd->ddev.channels,
  530. struct cppi41_channel, chan.device_node);
  531. list_del(&cchan->chan.device_node);
  532. kfree(cchan);
  533. }
  534. }
  535. static int cppi41_add_chans(struct device *dev, struct cppi41_dd *cdd)
  536. {
  537. struct cppi41_channel *cchan;
  538. int i;
  539. int ret;
  540. u32 n_chans;
  541. ret = of_property_read_u32(dev->of_node, "#dma-channels",
  542. &n_chans);
  543. if (ret)
  544. return ret;
  545. /*
  546. * The channels can only be used as TX or as RX. So we add twice
  547. * that much dma channels because USB can only do RX or TX.
  548. */
  549. n_chans *= 2;
  550. for (i = 0; i < n_chans; i++) {
  551. cchan = kzalloc(sizeof(*cchan), GFP_KERNEL);
  552. if (!cchan)
  553. goto err;
  554. cchan->cdd = cdd;
  555. if (i & 1) {
  556. cchan->gcr_reg = cdd->ctrl_mem + DMA_TXGCR(i >> 1);
  557. cchan->is_tx = 1;
  558. } else {
  559. cchan->gcr_reg = cdd->ctrl_mem + DMA_RXGCR(i >> 1);
  560. cchan->is_tx = 0;
  561. }
  562. cchan->port_num = i >> 1;
  563. cchan->desc = &cdd->cd[i];
  564. cchan->desc_phys = cdd->descs_phys;
  565. cchan->desc_phys += i * sizeof(struct cppi41_desc);
  566. cchan->chan.device = &cdd->ddev;
  567. list_add_tail(&cchan->chan.device_node, &cdd->ddev.channels);
  568. }
  569. cdd->first_td_desc = n_chans;
  570. return 0;
  571. err:
  572. cleanup_chans(cdd);
  573. return -ENOMEM;
  574. }
  575. static void purge_descs(struct device *dev, struct cppi41_dd *cdd)
  576. {
  577. unsigned int mem_decs;
  578. int i;
  579. mem_decs = ALLOC_DECS_NUM * sizeof(struct cppi41_desc);
  580. for (i = 0; i < DESCS_AREAS; i++) {
  581. cppi_writel(0, cdd->qmgr_mem + QMGR_MEMBASE(i));
  582. cppi_writel(0, cdd->qmgr_mem + QMGR_MEMCTRL(i));
  583. dma_free_coherent(dev, mem_decs, cdd->cd,
  584. cdd->descs_phys);
  585. }
  586. }
  587. static void disable_sched(struct cppi41_dd *cdd)
  588. {
  589. cppi_writel(0, cdd->sched_mem + DMA_SCHED_CTRL);
  590. }
  591. static void deinit_cppi41(struct device *dev, struct cppi41_dd *cdd)
  592. {
  593. disable_sched(cdd);
  594. purge_descs(dev, cdd);
  595. cppi_writel(0, cdd->qmgr_mem + QMGR_LRAM0_BASE);
  596. cppi_writel(0, cdd->qmgr_mem + QMGR_LRAM0_BASE);
  597. dma_free_coherent(dev, QMGR_SCRATCH_SIZE, cdd->qmgr_scratch,
  598. cdd->scratch_phys);
  599. }
  600. static int init_descs(struct device *dev, struct cppi41_dd *cdd)
  601. {
  602. unsigned int desc_size;
  603. unsigned int mem_decs;
  604. int i;
  605. u32 reg;
  606. u32 idx;
  607. BUILD_BUG_ON(sizeof(struct cppi41_desc) &
  608. (sizeof(struct cppi41_desc) - 1));
  609. BUILD_BUG_ON(sizeof(struct cppi41_desc) < 32);
  610. BUILD_BUG_ON(ALLOC_DECS_NUM < 32);
  611. desc_size = sizeof(struct cppi41_desc);
  612. mem_decs = ALLOC_DECS_NUM * desc_size;
  613. idx = 0;
  614. for (i = 0; i < DESCS_AREAS; i++) {
  615. reg = idx << QMGR_MEMCTRL_IDX_SH;
  616. reg |= (ilog2(desc_size) - 5) << QMGR_MEMCTRL_DESC_SH;
  617. reg |= ilog2(ALLOC_DECS_NUM) - 5;
  618. BUILD_BUG_ON(DESCS_AREAS != 1);
  619. cdd->cd = dma_alloc_coherent(dev, mem_decs,
  620. &cdd->descs_phys, GFP_KERNEL);
  621. if (!cdd->cd)
  622. return -ENOMEM;
  623. cppi_writel(cdd->descs_phys, cdd->qmgr_mem + QMGR_MEMBASE(i));
  624. cppi_writel(reg, cdd->qmgr_mem + QMGR_MEMCTRL(i));
  625. idx += ALLOC_DECS_NUM;
  626. }
  627. return 0;
  628. }
  629. static void init_sched(struct cppi41_dd *cdd)
  630. {
  631. unsigned ch;
  632. unsigned word;
  633. u32 reg;
  634. word = 0;
  635. cppi_writel(0, cdd->sched_mem + DMA_SCHED_CTRL);
  636. for (ch = 0; ch < 15 * 2; ch += 2) {
  637. reg = SCHED_ENTRY0_CHAN(ch);
  638. reg |= SCHED_ENTRY1_CHAN(ch) | SCHED_ENTRY1_IS_RX;
  639. reg |= SCHED_ENTRY2_CHAN(ch + 1);
  640. reg |= SCHED_ENTRY3_CHAN(ch + 1) | SCHED_ENTRY3_IS_RX;
  641. cppi_writel(reg, cdd->sched_mem + DMA_SCHED_WORD(word));
  642. word++;
  643. }
  644. reg = 15 * 2 * 2 - 1;
  645. reg |= DMA_SCHED_CTRL_EN;
  646. cppi_writel(reg, cdd->sched_mem + DMA_SCHED_CTRL);
  647. }
  648. static int init_cppi41(struct device *dev, struct cppi41_dd *cdd)
  649. {
  650. int ret;
  651. BUILD_BUG_ON(QMGR_SCRATCH_SIZE > ((1 << 14) - 1));
  652. cdd->qmgr_scratch = dma_alloc_coherent(dev, QMGR_SCRATCH_SIZE,
  653. &cdd->scratch_phys, GFP_KERNEL);
  654. if (!cdd->qmgr_scratch)
  655. return -ENOMEM;
  656. cppi_writel(cdd->scratch_phys, cdd->qmgr_mem + QMGR_LRAM0_BASE);
  657. cppi_writel(QMGR_SCRATCH_SIZE, cdd->qmgr_mem + QMGR_LRAM_SIZE);
  658. cppi_writel(0, cdd->qmgr_mem + QMGR_LRAM1_BASE);
  659. ret = init_descs(dev, cdd);
  660. if (ret)
  661. goto err_td;
  662. cppi_writel(cdd->td_queue.submit, cdd->ctrl_mem + DMA_TDFDQ);
  663. init_sched(cdd);
  664. return 0;
  665. err_td:
  666. deinit_cppi41(dev, cdd);
  667. return ret;
  668. }
  669. static struct platform_driver cpp41_dma_driver;
  670. /*
  671. * The param format is:
  672. * X Y
  673. * X: Port
  674. * Y: 0 = RX else TX
  675. */
  676. #define INFO_PORT 0
  677. #define INFO_IS_TX 1
  678. static bool cpp41_dma_filter_fn(struct dma_chan *chan, void *param)
  679. {
  680. struct cppi41_channel *cchan;
  681. struct cppi41_dd *cdd;
  682. const struct chan_queues *queues;
  683. u32 *num = param;
  684. if (chan->device->dev->driver != &cpp41_dma_driver.driver)
  685. return false;
  686. cchan = to_cpp41_chan(chan);
  687. if (cchan->port_num != num[INFO_PORT])
  688. return false;
  689. if (cchan->is_tx && !num[INFO_IS_TX])
  690. return false;
  691. cdd = cchan->cdd;
  692. if (cchan->is_tx)
  693. queues = cdd->queues_tx;
  694. else
  695. queues = cdd->queues_rx;
  696. BUILD_BUG_ON(ARRAY_SIZE(usb_queues_rx) != ARRAY_SIZE(usb_queues_tx));
  697. if (WARN_ON(cchan->port_num > ARRAY_SIZE(usb_queues_rx)))
  698. return false;
  699. cchan->q_num = queues[cchan->port_num].submit;
  700. cchan->q_comp_num = queues[cchan->port_num].complete;
  701. return true;
  702. }
  703. static struct of_dma_filter_info cpp41_dma_info = {
  704. .filter_fn = cpp41_dma_filter_fn,
  705. };
  706. static struct dma_chan *cppi41_dma_xlate(struct of_phandle_args *dma_spec,
  707. struct of_dma *ofdma)
  708. {
  709. int count = dma_spec->args_count;
  710. struct of_dma_filter_info *info = ofdma->of_dma_data;
  711. if (!info || !info->filter_fn)
  712. return NULL;
  713. if (count != 2)
  714. return NULL;
  715. return dma_request_channel(info->dma_cap, info->filter_fn,
  716. &dma_spec->args[0]);
  717. }
  718. static const struct cppi_glue_infos usb_infos = {
  719. .isr = cppi41_irq,
  720. .queues_rx = usb_queues_rx,
  721. .queues_tx = usb_queues_tx,
  722. .td_queue = { .submit = 31, .complete = 0 },
  723. };
  724. static const struct of_device_id cppi41_dma_ids[] = {
  725. { .compatible = "ti,am3359-cppi41", .data = &usb_infos},
  726. {},
  727. };
  728. MODULE_DEVICE_TABLE(of, cppi41_dma_ids);
  729. static const struct cppi_glue_infos *get_glue_info(struct device *dev)
  730. {
  731. const struct of_device_id *of_id;
  732. of_id = of_match_node(cppi41_dma_ids, dev->of_node);
  733. if (!of_id)
  734. return NULL;
  735. return of_id->data;
  736. }
  737. #define CPPI41_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
  738. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
  739. BIT(DMA_SLAVE_BUSWIDTH_3_BYTES) | \
  740. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
  741. static int cppi41_dma_probe(struct platform_device *pdev)
  742. {
  743. struct cppi41_dd *cdd;
  744. struct device *dev = &pdev->dev;
  745. const struct cppi_glue_infos *glue_info;
  746. int irq;
  747. int ret;
  748. glue_info = get_glue_info(dev);
  749. if (!glue_info)
  750. return -EINVAL;
  751. cdd = devm_kzalloc(&pdev->dev, sizeof(*cdd), GFP_KERNEL);
  752. if (!cdd)
  753. return -ENOMEM;
  754. dma_cap_set(DMA_SLAVE, cdd->ddev.cap_mask);
  755. cdd->ddev.device_alloc_chan_resources = cppi41_dma_alloc_chan_resources;
  756. cdd->ddev.device_free_chan_resources = cppi41_dma_free_chan_resources;
  757. cdd->ddev.device_tx_status = cppi41_dma_tx_status;
  758. cdd->ddev.device_issue_pending = cppi41_dma_issue_pending;
  759. cdd->ddev.device_prep_slave_sg = cppi41_dma_prep_slave_sg;
  760. cdd->ddev.device_terminate_all = cppi41_stop_chan;
  761. cdd->ddev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  762. cdd->ddev.src_addr_widths = CPPI41_DMA_BUSWIDTHS;
  763. cdd->ddev.dst_addr_widths = CPPI41_DMA_BUSWIDTHS;
  764. cdd->ddev.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  765. cdd->ddev.dev = dev;
  766. INIT_LIST_HEAD(&cdd->ddev.channels);
  767. cpp41_dma_info.dma_cap = cdd->ddev.cap_mask;
  768. cdd->usbss_mem = of_iomap(dev->of_node, 0);
  769. cdd->ctrl_mem = of_iomap(dev->of_node, 1);
  770. cdd->sched_mem = of_iomap(dev->of_node, 2);
  771. cdd->qmgr_mem = of_iomap(dev->of_node, 3);
  772. if (!cdd->usbss_mem || !cdd->ctrl_mem || !cdd->sched_mem ||
  773. !cdd->qmgr_mem)
  774. return -ENXIO;
  775. pm_runtime_enable(dev);
  776. ret = pm_runtime_get_sync(dev);
  777. if (ret < 0)
  778. goto err_get_sync;
  779. cdd->queues_rx = glue_info->queues_rx;
  780. cdd->queues_tx = glue_info->queues_tx;
  781. cdd->td_queue = glue_info->td_queue;
  782. ret = init_cppi41(dev, cdd);
  783. if (ret)
  784. goto err_init_cppi;
  785. ret = cppi41_add_chans(dev, cdd);
  786. if (ret)
  787. goto err_chans;
  788. irq = irq_of_parse_and_map(dev->of_node, 0);
  789. if (!irq) {
  790. ret = -EINVAL;
  791. goto err_irq;
  792. }
  793. cppi_writel(USBSS_IRQ_PD_COMP, cdd->usbss_mem + USBSS_IRQ_ENABLER);
  794. ret = devm_request_irq(&pdev->dev, irq, glue_info->isr, IRQF_SHARED,
  795. dev_name(dev), cdd);
  796. if (ret)
  797. goto err_irq;
  798. cdd->irq = irq;
  799. ret = dma_async_device_register(&cdd->ddev);
  800. if (ret)
  801. goto err_dma_reg;
  802. ret = of_dma_controller_register(dev->of_node,
  803. cppi41_dma_xlate, &cpp41_dma_info);
  804. if (ret)
  805. goto err_of;
  806. platform_set_drvdata(pdev, cdd);
  807. return 0;
  808. err_of:
  809. dma_async_device_unregister(&cdd->ddev);
  810. err_dma_reg:
  811. err_irq:
  812. cppi_writel(0, cdd->usbss_mem + USBSS_IRQ_CLEARR);
  813. cleanup_chans(cdd);
  814. err_chans:
  815. deinit_cppi41(dev, cdd);
  816. err_init_cppi:
  817. pm_runtime_put(dev);
  818. err_get_sync:
  819. pm_runtime_disable(dev);
  820. iounmap(cdd->usbss_mem);
  821. iounmap(cdd->ctrl_mem);
  822. iounmap(cdd->sched_mem);
  823. iounmap(cdd->qmgr_mem);
  824. return ret;
  825. }
  826. static int cppi41_dma_remove(struct platform_device *pdev)
  827. {
  828. struct cppi41_dd *cdd = platform_get_drvdata(pdev);
  829. of_dma_controller_free(pdev->dev.of_node);
  830. dma_async_device_unregister(&cdd->ddev);
  831. cppi_writel(0, cdd->usbss_mem + USBSS_IRQ_CLEARR);
  832. devm_free_irq(&pdev->dev, cdd->irq, cdd);
  833. cleanup_chans(cdd);
  834. deinit_cppi41(&pdev->dev, cdd);
  835. iounmap(cdd->usbss_mem);
  836. iounmap(cdd->ctrl_mem);
  837. iounmap(cdd->sched_mem);
  838. iounmap(cdd->qmgr_mem);
  839. pm_runtime_put(&pdev->dev);
  840. pm_runtime_disable(&pdev->dev);
  841. return 0;
  842. }
  843. #ifdef CONFIG_PM_SLEEP
  844. static int cppi41_suspend(struct device *dev)
  845. {
  846. struct cppi41_dd *cdd = dev_get_drvdata(dev);
  847. cdd->dma_tdfdq = cppi_readl(cdd->ctrl_mem + DMA_TDFDQ);
  848. cppi_writel(0, cdd->usbss_mem + USBSS_IRQ_CLEARR);
  849. disable_sched(cdd);
  850. return 0;
  851. }
  852. static int cppi41_resume(struct device *dev)
  853. {
  854. struct cppi41_dd *cdd = dev_get_drvdata(dev);
  855. struct cppi41_channel *c;
  856. int i;
  857. for (i = 0; i < DESCS_AREAS; i++)
  858. cppi_writel(cdd->descs_phys, cdd->qmgr_mem + QMGR_MEMBASE(i));
  859. list_for_each_entry(c, &cdd->ddev.channels, chan.device_node)
  860. if (!c->is_tx)
  861. cppi_writel(c->q_num, c->gcr_reg + RXHPCRA0);
  862. init_sched(cdd);
  863. cppi_writel(cdd->dma_tdfdq, cdd->ctrl_mem + DMA_TDFDQ);
  864. cppi_writel(cdd->scratch_phys, cdd->qmgr_mem + QMGR_LRAM0_BASE);
  865. cppi_writel(QMGR_SCRATCH_SIZE, cdd->qmgr_mem + QMGR_LRAM_SIZE);
  866. cppi_writel(0, cdd->qmgr_mem + QMGR_LRAM1_BASE);
  867. cppi_writel(USBSS_IRQ_PD_COMP, cdd->usbss_mem + USBSS_IRQ_ENABLER);
  868. return 0;
  869. }
  870. #endif
  871. static SIMPLE_DEV_PM_OPS(cppi41_pm_ops, cppi41_suspend, cppi41_resume);
  872. static struct platform_driver cpp41_dma_driver = {
  873. .probe = cppi41_dma_probe,
  874. .remove = cppi41_dma_remove,
  875. .driver = {
  876. .name = "cppi41-dma-engine",
  877. .pm = &cppi41_pm_ops,
  878. .of_match_table = of_match_ptr(cppi41_dma_ids),
  879. },
  880. };
  881. module_platform_driver(cpp41_dma_driver);
  882. MODULE_LICENSE("GPL");
  883. MODULE_AUTHOR("Sebastian Andrzej Siewior <bigeasy@linutronix.de>");