omap-dma.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666
  1. /*
  2. * OMAP DMAengine support
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/dmaengine.h>
  10. #include <linux/dma-mapping.h>
  11. #include <linux/dmapool.h>
  12. #include <linux/err.h>
  13. #include <linux/init.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/list.h>
  16. #include <linux/module.h>
  17. #include <linux/omap-dma.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/slab.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/of_dma.h>
  22. #include <linux/of_device.h>
  23. #include "virt-dma.h"
  24. #define OMAP_SDMA_REQUESTS 127
  25. #define OMAP_SDMA_CHANNELS 32
  26. struct omap_dmadev {
  27. struct dma_device ddev;
  28. spinlock_t lock;
  29. void __iomem *base;
  30. const struct omap_dma_reg *reg_map;
  31. struct omap_system_dma_plat_info *plat;
  32. bool legacy;
  33. bool ll123_supported;
  34. struct dma_pool *desc_pool;
  35. unsigned dma_requests;
  36. spinlock_t irq_lock;
  37. uint32_t irq_enable_mask;
  38. struct omap_chan **lch_map;
  39. };
  40. struct omap_chan {
  41. struct virt_dma_chan vc;
  42. void __iomem *channel_base;
  43. const struct omap_dma_reg *reg_map;
  44. uint32_t ccr;
  45. struct dma_slave_config cfg;
  46. unsigned dma_sig;
  47. bool cyclic;
  48. bool paused;
  49. bool running;
  50. int dma_ch;
  51. struct omap_desc *desc;
  52. unsigned sgidx;
  53. };
  54. #define DESC_NXT_SV_REFRESH (0x1 << 24)
  55. #define DESC_NXT_SV_REUSE (0x2 << 24)
  56. #define DESC_NXT_DV_REFRESH (0x1 << 26)
  57. #define DESC_NXT_DV_REUSE (0x2 << 26)
  58. #define DESC_NTYPE_TYPE2 (0x2 << 29)
  59. /* Type 2 descriptor with Source or Destination address update */
  60. struct omap_type2_desc {
  61. uint32_t next_desc;
  62. uint32_t en;
  63. uint32_t addr; /* src or dst */
  64. uint16_t fn;
  65. uint16_t cicr;
  66. int16_t cdei;
  67. int16_t csei;
  68. int32_t cdfi;
  69. int32_t csfi;
  70. } __packed;
  71. struct omap_sg {
  72. dma_addr_t addr;
  73. uint32_t en; /* number of elements (24-bit) */
  74. uint32_t fn; /* number of frames (16-bit) */
  75. int32_t fi; /* for double indexing */
  76. int16_t ei; /* for double indexing */
  77. /* Linked list */
  78. struct omap_type2_desc *t2_desc;
  79. dma_addr_t t2_desc_paddr;
  80. };
  81. struct omap_desc {
  82. struct virt_dma_desc vd;
  83. bool using_ll;
  84. enum dma_transfer_direction dir;
  85. dma_addr_t dev_addr;
  86. int32_t fi; /* for OMAP_DMA_SYNC_PACKET / double indexing */
  87. int16_t ei; /* for double indexing */
  88. uint8_t es; /* CSDP_DATA_TYPE_xxx */
  89. uint32_t ccr; /* CCR value */
  90. uint16_t clnk_ctrl; /* CLNK_CTRL value */
  91. uint16_t cicr; /* CICR value */
  92. uint32_t csdp; /* CSDP value */
  93. unsigned sglen;
  94. struct omap_sg sg[0];
  95. };
  96. enum {
  97. CAPS_0_SUPPORT_LL123 = BIT(20), /* Linked List type1/2/3 */
  98. CAPS_0_SUPPORT_LL4 = BIT(21), /* Linked List type4 */
  99. CCR_FS = BIT(5),
  100. CCR_READ_PRIORITY = BIT(6),
  101. CCR_ENABLE = BIT(7),
  102. CCR_AUTO_INIT = BIT(8), /* OMAP1 only */
  103. CCR_REPEAT = BIT(9), /* OMAP1 only */
  104. CCR_OMAP31_DISABLE = BIT(10), /* OMAP1 only */
  105. CCR_SUSPEND_SENSITIVE = BIT(8), /* OMAP2+ only */
  106. CCR_RD_ACTIVE = BIT(9), /* OMAP2+ only */
  107. CCR_WR_ACTIVE = BIT(10), /* OMAP2+ only */
  108. CCR_SRC_AMODE_CONSTANT = 0 << 12,
  109. CCR_SRC_AMODE_POSTINC = 1 << 12,
  110. CCR_SRC_AMODE_SGLIDX = 2 << 12,
  111. CCR_SRC_AMODE_DBLIDX = 3 << 12,
  112. CCR_DST_AMODE_CONSTANT = 0 << 14,
  113. CCR_DST_AMODE_POSTINC = 1 << 14,
  114. CCR_DST_AMODE_SGLIDX = 2 << 14,
  115. CCR_DST_AMODE_DBLIDX = 3 << 14,
  116. CCR_CONSTANT_FILL = BIT(16),
  117. CCR_TRANSPARENT_COPY = BIT(17),
  118. CCR_BS = BIT(18),
  119. CCR_SUPERVISOR = BIT(22),
  120. CCR_PREFETCH = BIT(23),
  121. CCR_TRIGGER_SRC = BIT(24),
  122. CCR_BUFFERING_DISABLE = BIT(25),
  123. CCR_WRITE_PRIORITY = BIT(26),
  124. CCR_SYNC_ELEMENT = 0,
  125. CCR_SYNC_FRAME = CCR_FS,
  126. CCR_SYNC_BLOCK = CCR_BS,
  127. CCR_SYNC_PACKET = CCR_BS | CCR_FS,
  128. CSDP_DATA_TYPE_8 = 0,
  129. CSDP_DATA_TYPE_16 = 1,
  130. CSDP_DATA_TYPE_32 = 2,
  131. CSDP_SRC_PORT_EMIFF = 0 << 2, /* OMAP1 only */
  132. CSDP_SRC_PORT_EMIFS = 1 << 2, /* OMAP1 only */
  133. CSDP_SRC_PORT_OCP_T1 = 2 << 2, /* OMAP1 only */
  134. CSDP_SRC_PORT_TIPB = 3 << 2, /* OMAP1 only */
  135. CSDP_SRC_PORT_OCP_T2 = 4 << 2, /* OMAP1 only */
  136. CSDP_SRC_PORT_MPUI = 5 << 2, /* OMAP1 only */
  137. CSDP_SRC_PACKED = BIT(6),
  138. CSDP_SRC_BURST_1 = 0 << 7,
  139. CSDP_SRC_BURST_16 = 1 << 7,
  140. CSDP_SRC_BURST_32 = 2 << 7,
  141. CSDP_SRC_BURST_64 = 3 << 7,
  142. CSDP_DST_PORT_EMIFF = 0 << 9, /* OMAP1 only */
  143. CSDP_DST_PORT_EMIFS = 1 << 9, /* OMAP1 only */
  144. CSDP_DST_PORT_OCP_T1 = 2 << 9, /* OMAP1 only */
  145. CSDP_DST_PORT_TIPB = 3 << 9, /* OMAP1 only */
  146. CSDP_DST_PORT_OCP_T2 = 4 << 9, /* OMAP1 only */
  147. CSDP_DST_PORT_MPUI = 5 << 9, /* OMAP1 only */
  148. CSDP_DST_PACKED = BIT(13),
  149. CSDP_DST_BURST_1 = 0 << 14,
  150. CSDP_DST_BURST_16 = 1 << 14,
  151. CSDP_DST_BURST_32 = 2 << 14,
  152. CSDP_DST_BURST_64 = 3 << 14,
  153. CSDP_WRITE_NON_POSTED = 0 << 16,
  154. CSDP_WRITE_POSTED = 1 << 16,
  155. CSDP_WRITE_LAST_NON_POSTED = 2 << 16,
  156. CICR_TOUT_IE = BIT(0), /* OMAP1 only */
  157. CICR_DROP_IE = BIT(1),
  158. CICR_HALF_IE = BIT(2),
  159. CICR_FRAME_IE = BIT(3),
  160. CICR_LAST_IE = BIT(4),
  161. CICR_BLOCK_IE = BIT(5),
  162. CICR_PKT_IE = BIT(7), /* OMAP2+ only */
  163. CICR_TRANS_ERR_IE = BIT(8), /* OMAP2+ only */
  164. CICR_SUPERVISOR_ERR_IE = BIT(10), /* OMAP2+ only */
  165. CICR_MISALIGNED_ERR_IE = BIT(11), /* OMAP2+ only */
  166. CICR_DRAIN_IE = BIT(12), /* OMAP2+ only */
  167. CICR_SUPER_BLOCK_IE = BIT(14), /* OMAP2+ only */
  168. CLNK_CTRL_ENABLE_LNK = BIT(15),
  169. CDP_DST_VALID_INC = 0 << 0,
  170. CDP_DST_VALID_RELOAD = 1 << 0,
  171. CDP_DST_VALID_REUSE = 2 << 0,
  172. CDP_SRC_VALID_INC = 0 << 2,
  173. CDP_SRC_VALID_RELOAD = 1 << 2,
  174. CDP_SRC_VALID_REUSE = 2 << 2,
  175. CDP_NTYPE_TYPE1 = 1 << 4,
  176. CDP_NTYPE_TYPE2 = 2 << 4,
  177. CDP_NTYPE_TYPE3 = 3 << 4,
  178. CDP_TMODE_NORMAL = 0 << 8,
  179. CDP_TMODE_LLIST = 1 << 8,
  180. CDP_FAST = BIT(10),
  181. };
  182. static const unsigned es_bytes[] = {
  183. [CSDP_DATA_TYPE_8] = 1,
  184. [CSDP_DATA_TYPE_16] = 2,
  185. [CSDP_DATA_TYPE_32] = 4,
  186. };
  187. static struct of_dma_filter_info omap_dma_info = {
  188. .filter_fn = omap_dma_filter_fn,
  189. };
  190. static inline struct omap_dmadev *to_omap_dma_dev(struct dma_device *d)
  191. {
  192. return container_of(d, struct omap_dmadev, ddev);
  193. }
  194. static inline struct omap_chan *to_omap_dma_chan(struct dma_chan *c)
  195. {
  196. return container_of(c, struct omap_chan, vc.chan);
  197. }
  198. static inline struct omap_desc *to_omap_dma_desc(struct dma_async_tx_descriptor *t)
  199. {
  200. return container_of(t, struct omap_desc, vd.tx);
  201. }
  202. static void omap_dma_desc_free(struct virt_dma_desc *vd)
  203. {
  204. struct omap_desc *d = to_omap_dma_desc(&vd->tx);
  205. if (d->using_ll) {
  206. struct omap_dmadev *od = to_omap_dma_dev(vd->tx.chan->device);
  207. int i;
  208. for (i = 0; i < d->sglen; i++) {
  209. if (d->sg[i].t2_desc)
  210. dma_pool_free(od->desc_pool, d->sg[i].t2_desc,
  211. d->sg[i].t2_desc_paddr);
  212. }
  213. }
  214. kfree(d);
  215. }
  216. static void omap_dma_fill_type2_desc(struct omap_desc *d, int idx,
  217. enum dma_transfer_direction dir, bool last)
  218. {
  219. struct omap_sg *sg = &d->sg[idx];
  220. struct omap_type2_desc *t2_desc = sg->t2_desc;
  221. if (idx)
  222. d->sg[idx - 1].t2_desc->next_desc = sg->t2_desc_paddr;
  223. if (last)
  224. t2_desc->next_desc = 0xfffffffc;
  225. t2_desc->en = sg->en;
  226. t2_desc->addr = sg->addr;
  227. t2_desc->fn = sg->fn & 0xffff;
  228. t2_desc->cicr = d->cicr;
  229. if (!last)
  230. t2_desc->cicr &= ~CICR_BLOCK_IE;
  231. switch (dir) {
  232. case DMA_DEV_TO_MEM:
  233. t2_desc->cdei = sg->ei;
  234. t2_desc->csei = d->ei;
  235. t2_desc->cdfi = sg->fi;
  236. t2_desc->csfi = d->fi;
  237. t2_desc->en |= DESC_NXT_DV_REFRESH;
  238. t2_desc->en |= DESC_NXT_SV_REUSE;
  239. break;
  240. case DMA_MEM_TO_DEV:
  241. t2_desc->cdei = d->ei;
  242. t2_desc->csei = sg->ei;
  243. t2_desc->cdfi = d->fi;
  244. t2_desc->csfi = sg->fi;
  245. t2_desc->en |= DESC_NXT_SV_REFRESH;
  246. t2_desc->en |= DESC_NXT_DV_REUSE;
  247. break;
  248. default:
  249. return;
  250. }
  251. t2_desc->en |= DESC_NTYPE_TYPE2;
  252. }
  253. static void omap_dma_write(uint32_t val, unsigned type, void __iomem *addr)
  254. {
  255. switch (type) {
  256. case OMAP_DMA_REG_16BIT:
  257. writew_relaxed(val, addr);
  258. break;
  259. case OMAP_DMA_REG_2X16BIT:
  260. writew_relaxed(val, addr);
  261. writew_relaxed(val >> 16, addr + 2);
  262. break;
  263. case OMAP_DMA_REG_32BIT:
  264. writel_relaxed(val, addr);
  265. break;
  266. default:
  267. WARN_ON(1);
  268. }
  269. }
  270. static unsigned omap_dma_read(unsigned type, void __iomem *addr)
  271. {
  272. unsigned val;
  273. switch (type) {
  274. case OMAP_DMA_REG_16BIT:
  275. val = readw_relaxed(addr);
  276. break;
  277. case OMAP_DMA_REG_2X16BIT:
  278. val = readw_relaxed(addr);
  279. val |= readw_relaxed(addr + 2) << 16;
  280. break;
  281. case OMAP_DMA_REG_32BIT:
  282. val = readl_relaxed(addr);
  283. break;
  284. default:
  285. WARN_ON(1);
  286. val = 0;
  287. }
  288. return val;
  289. }
  290. static void omap_dma_glbl_write(struct omap_dmadev *od, unsigned reg, unsigned val)
  291. {
  292. const struct omap_dma_reg *r = od->reg_map + reg;
  293. WARN_ON(r->stride);
  294. omap_dma_write(val, r->type, od->base + r->offset);
  295. }
  296. static unsigned omap_dma_glbl_read(struct omap_dmadev *od, unsigned reg)
  297. {
  298. const struct omap_dma_reg *r = od->reg_map + reg;
  299. WARN_ON(r->stride);
  300. return omap_dma_read(r->type, od->base + r->offset);
  301. }
  302. static void omap_dma_chan_write(struct omap_chan *c, unsigned reg, unsigned val)
  303. {
  304. const struct omap_dma_reg *r = c->reg_map + reg;
  305. omap_dma_write(val, r->type, c->channel_base + r->offset);
  306. }
  307. static unsigned omap_dma_chan_read(struct omap_chan *c, unsigned reg)
  308. {
  309. const struct omap_dma_reg *r = c->reg_map + reg;
  310. return omap_dma_read(r->type, c->channel_base + r->offset);
  311. }
  312. static void omap_dma_clear_csr(struct omap_chan *c)
  313. {
  314. if (dma_omap1())
  315. omap_dma_chan_read(c, CSR);
  316. else
  317. omap_dma_chan_write(c, CSR, ~0);
  318. }
  319. static unsigned omap_dma_get_csr(struct omap_chan *c)
  320. {
  321. unsigned val = omap_dma_chan_read(c, CSR);
  322. if (!dma_omap1())
  323. omap_dma_chan_write(c, CSR, val);
  324. return val;
  325. }
  326. static void omap_dma_assign(struct omap_dmadev *od, struct omap_chan *c,
  327. unsigned lch)
  328. {
  329. c->channel_base = od->base + od->plat->channel_stride * lch;
  330. od->lch_map[lch] = c;
  331. }
  332. static void omap_dma_start(struct omap_chan *c, struct omap_desc *d)
  333. {
  334. struct omap_dmadev *od = to_omap_dma_dev(c->vc.chan.device);
  335. uint16_t cicr = d->cicr;
  336. if (__dma_omap15xx(od->plat->dma_attr))
  337. omap_dma_chan_write(c, CPC, 0);
  338. else
  339. omap_dma_chan_write(c, CDAC, 0);
  340. omap_dma_clear_csr(c);
  341. if (d->using_ll) {
  342. uint32_t cdp = CDP_TMODE_LLIST | CDP_NTYPE_TYPE2 | CDP_FAST;
  343. if (d->dir == DMA_DEV_TO_MEM)
  344. cdp |= (CDP_DST_VALID_RELOAD | CDP_SRC_VALID_REUSE);
  345. else
  346. cdp |= (CDP_DST_VALID_REUSE | CDP_SRC_VALID_RELOAD);
  347. omap_dma_chan_write(c, CDP, cdp);
  348. omap_dma_chan_write(c, CNDP, d->sg[0].t2_desc_paddr);
  349. omap_dma_chan_write(c, CCDN, 0);
  350. omap_dma_chan_write(c, CCFN, 0xffff);
  351. omap_dma_chan_write(c, CCEN, 0xffffff);
  352. cicr &= ~CICR_BLOCK_IE;
  353. } else if (od->ll123_supported) {
  354. omap_dma_chan_write(c, CDP, 0);
  355. }
  356. /* Enable interrupts */
  357. omap_dma_chan_write(c, CICR, cicr);
  358. /* Enable channel */
  359. omap_dma_chan_write(c, CCR, d->ccr | CCR_ENABLE);
  360. c->running = true;
  361. }
  362. static void omap_dma_drain_chan(struct omap_chan *c)
  363. {
  364. int i;
  365. u32 val;
  366. /* Wait for sDMA FIFO to drain */
  367. for (i = 0; ; i++) {
  368. val = omap_dma_chan_read(c, CCR);
  369. if (!(val & (CCR_RD_ACTIVE | CCR_WR_ACTIVE)))
  370. break;
  371. if (i > 100)
  372. break;
  373. udelay(5);
  374. }
  375. if (val & (CCR_RD_ACTIVE | CCR_WR_ACTIVE))
  376. dev_err(c->vc.chan.device->dev,
  377. "DMA drain did not complete on lch %d\n",
  378. c->dma_ch);
  379. }
  380. static int omap_dma_stop(struct omap_chan *c)
  381. {
  382. struct omap_dmadev *od = to_omap_dma_dev(c->vc.chan.device);
  383. uint32_t val;
  384. /* disable irq */
  385. omap_dma_chan_write(c, CICR, 0);
  386. omap_dma_clear_csr(c);
  387. val = omap_dma_chan_read(c, CCR);
  388. if (od->plat->errata & DMA_ERRATA_i541 && val & CCR_TRIGGER_SRC) {
  389. uint32_t sysconfig;
  390. sysconfig = omap_dma_glbl_read(od, OCP_SYSCONFIG);
  391. val = sysconfig & ~DMA_SYSCONFIG_MIDLEMODE_MASK;
  392. val |= DMA_SYSCONFIG_MIDLEMODE(DMA_IDLEMODE_NO_IDLE);
  393. omap_dma_glbl_write(od, OCP_SYSCONFIG, val);
  394. val = omap_dma_chan_read(c, CCR);
  395. val &= ~CCR_ENABLE;
  396. omap_dma_chan_write(c, CCR, val);
  397. if (!(c->ccr & CCR_BUFFERING_DISABLE))
  398. omap_dma_drain_chan(c);
  399. omap_dma_glbl_write(od, OCP_SYSCONFIG, sysconfig);
  400. } else {
  401. if (!(val & CCR_ENABLE))
  402. return -EINVAL;
  403. val &= ~CCR_ENABLE;
  404. omap_dma_chan_write(c, CCR, val);
  405. if (!(c->ccr & CCR_BUFFERING_DISABLE))
  406. omap_dma_drain_chan(c);
  407. }
  408. mb();
  409. if (!__dma_omap15xx(od->plat->dma_attr) && c->cyclic) {
  410. val = omap_dma_chan_read(c, CLNK_CTRL);
  411. if (dma_omap1())
  412. val |= 1 << 14; /* set the STOP_LNK bit */
  413. else
  414. val &= ~CLNK_CTRL_ENABLE_LNK;
  415. omap_dma_chan_write(c, CLNK_CTRL, val);
  416. }
  417. c->running = false;
  418. return 0;
  419. }
  420. static void omap_dma_start_sg(struct omap_chan *c, struct omap_desc *d)
  421. {
  422. struct omap_sg *sg = d->sg + c->sgidx;
  423. unsigned cxsa, cxei, cxfi;
  424. if (d->dir == DMA_DEV_TO_MEM || d->dir == DMA_MEM_TO_MEM) {
  425. cxsa = CDSA;
  426. cxei = CDEI;
  427. cxfi = CDFI;
  428. } else {
  429. cxsa = CSSA;
  430. cxei = CSEI;
  431. cxfi = CSFI;
  432. }
  433. omap_dma_chan_write(c, cxsa, sg->addr);
  434. omap_dma_chan_write(c, cxei, sg->ei);
  435. omap_dma_chan_write(c, cxfi, sg->fi);
  436. omap_dma_chan_write(c, CEN, sg->en);
  437. omap_dma_chan_write(c, CFN, sg->fn);
  438. omap_dma_start(c, d);
  439. c->sgidx++;
  440. }
  441. static void omap_dma_start_desc(struct omap_chan *c)
  442. {
  443. struct virt_dma_desc *vd = vchan_next_desc(&c->vc);
  444. struct omap_desc *d;
  445. unsigned cxsa, cxei, cxfi;
  446. if (!vd) {
  447. c->desc = NULL;
  448. return;
  449. }
  450. list_del(&vd->node);
  451. c->desc = d = to_omap_dma_desc(&vd->tx);
  452. c->sgidx = 0;
  453. /*
  454. * This provides the necessary barrier to ensure data held in
  455. * DMA coherent memory is visible to the DMA engine prior to
  456. * the transfer starting.
  457. */
  458. mb();
  459. omap_dma_chan_write(c, CCR, d->ccr);
  460. if (dma_omap1())
  461. omap_dma_chan_write(c, CCR2, d->ccr >> 16);
  462. if (d->dir == DMA_DEV_TO_MEM || d->dir == DMA_MEM_TO_MEM) {
  463. cxsa = CSSA;
  464. cxei = CSEI;
  465. cxfi = CSFI;
  466. } else {
  467. cxsa = CDSA;
  468. cxei = CDEI;
  469. cxfi = CDFI;
  470. }
  471. omap_dma_chan_write(c, cxsa, d->dev_addr);
  472. omap_dma_chan_write(c, cxei, d->ei);
  473. omap_dma_chan_write(c, cxfi, d->fi);
  474. omap_dma_chan_write(c, CSDP, d->csdp);
  475. omap_dma_chan_write(c, CLNK_CTRL, d->clnk_ctrl);
  476. omap_dma_start_sg(c, d);
  477. }
  478. static void omap_dma_callback(int ch, u16 status, void *data)
  479. {
  480. struct omap_chan *c = data;
  481. struct omap_desc *d;
  482. unsigned long flags;
  483. spin_lock_irqsave(&c->vc.lock, flags);
  484. d = c->desc;
  485. if (d) {
  486. if (c->cyclic) {
  487. vchan_cyclic_callback(&d->vd);
  488. } else if (d->using_ll || c->sgidx == d->sglen) {
  489. omap_dma_start_desc(c);
  490. vchan_cookie_complete(&d->vd);
  491. } else {
  492. omap_dma_start_sg(c, d);
  493. }
  494. }
  495. spin_unlock_irqrestore(&c->vc.lock, flags);
  496. }
  497. static irqreturn_t omap_dma_irq(int irq, void *devid)
  498. {
  499. struct omap_dmadev *od = devid;
  500. unsigned status, channel;
  501. spin_lock(&od->irq_lock);
  502. status = omap_dma_glbl_read(od, IRQSTATUS_L1);
  503. status &= od->irq_enable_mask;
  504. if (status == 0) {
  505. spin_unlock(&od->irq_lock);
  506. return IRQ_NONE;
  507. }
  508. while ((channel = ffs(status)) != 0) {
  509. unsigned mask, csr;
  510. struct omap_chan *c;
  511. channel -= 1;
  512. mask = BIT(channel);
  513. status &= ~mask;
  514. c = od->lch_map[channel];
  515. if (c == NULL) {
  516. /* This should never happen */
  517. dev_err(od->ddev.dev, "invalid channel %u\n", channel);
  518. continue;
  519. }
  520. csr = omap_dma_get_csr(c);
  521. omap_dma_glbl_write(od, IRQSTATUS_L1, mask);
  522. omap_dma_callback(channel, csr, c);
  523. }
  524. spin_unlock(&od->irq_lock);
  525. return IRQ_HANDLED;
  526. }
  527. static int omap_dma_alloc_chan_resources(struct dma_chan *chan)
  528. {
  529. struct omap_dmadev *od = to_omap_dma_dev(chan->device);
  530. struct omap_chan *c = to_omap_dma_chan(chan);
  531. struct device *dev = od->ddev.dev;
  532. int ret;
  533. if (od->legacy) {
  534. ret = omap_request_dma(c->dma_sig, "DMA engine",
  535. omap_dma_callback, c, &c->dma_ch);
  536. } else {
  537. ret = omap_request_dma(c->dma_sig, "DMA engine", NULL, NULL,
  538. &c->dma_ch);
  539. }
  540. dev_dbg(dev, "allocating channel %u for %u\n", c->dma_ch, c->dma_sig);
  541. if (ret >= 0) {
  542. omap_dma_assign(od, c, c->dma_ch);
  543. if (!od->legacy) {
  544. unsigned val;
  545. spin_lock_irq(&od->irq_lock);
  546. val = BIT(c->dma_ch);
  547. omap_dma_glbl_write(od, IRQSTATUS_L1, val);
  548. od->irq_enable_mask |= val;
  549. omap_dma_glbl_write(od, IRQENABLE_L1, od->irq_enable_mask);
  550. val = omap_dma_glbl_read(od, IRQENABLE_L0);
  551. val &= ~BIT(c->dma_ch);
  552. omap_dma_glbl_write(od, IRQENABLE_L0, val);
  553. spin_unlock_irq(&od->irq_lock);
  554. }
  555. }
  556. if (dma_omap1()) {
  557. if (__dma_omap16xx(od->plat->dma_attr)) {
  558. c->ccr = CCR_OMAP31_DISABLE;
  559. /* Duplicate what plat-omap/dma.c does */
  560. c->ccr |= c->dma_ch + 1;
  561. } else {
  562. c->ccr = c->dma_sig & 0x1f;
  563. }
  564. } else {
  565. c->ccr = c->dma_sig & 0x1f;
  566. c->ccr |= (c->dma_sig & ~0x1f) << 14;
  567. }
  568. if (od->plat->errata & DMA_ERRATA_IFRAME_BUFFERING)
  569. c->ccr |= CCR_BUFFERING_DISABLE;
  570. return ret;
  571. }
  572. static void omap_dma_free_chan_resources(struct dma_chan *chan)
  573. {
  574. struct omap_dmadev *od = to_omap_dma_dev(chan->device);
  575. struct omap_chan *c = to_omap_dma_chan(chan);
  576. if (!od->legacy) {
  577. spin_lock_irq(&od->irq_lock);
  578. od->irq_enable_mask &= ~BIT(c->dma_ch);
  579. omap_dma_glbl_write(od, IRQENABLE_L1, od->irq_enable_mask);
  580. spin_unlock_irq(&od->irq_lock);
  581. }
  582. c->channel_base = NULL;
  583. od->lch_map[c->dma_ch] = NULL;
  584. vchan_free_chan_resources(&c->vc);
  585. omap_free_dma(c->dma_ch);
  586. dev_dbg(od->ddev.dev, "freeing channel %u used for %u\n", c->dma_ch,
  587. c->dma_sig);
  588. c->dma_sig = 0;
  589. }
  590. static size_t omap_dma_sg_size(struct omap_sg *sg)
  591. {
  592. return sg->en * sg->fn;
  593. }
  594. static size_t omap_dma_desc_size(struct omap_desc *d)
  595. {
  596. unsigned i;
  597. size_t size;
  598. for (size = i = 0; i < d->sglen; i++)
  599. size += omap_dma_sg_size(&d->sg[i]);
  600. return size * es_bytes[d->es];
  601. }
  602. static size_t omap_dma_desc_size_pos(struct omap_desc *d, dma_addr_t addr)
  603. {
  604. unsigned i;
  605. size_t size, es_size = es_bytes[d->es];
  606. for (size = i = 0; i < d->sglen; i++) {
  607. size_t this_size = omap_dma_sg_size(&d->sg[i]) * es_size;
  608. if (size)
  609. size += this_size;
  610. else if (addr >= d->sg[i].addr &&
  611. addr < d->sg[i].addr + this_size)
  612. size += d->sg[i].addr + this_size - addr;
  613. }
  614. return size;
  615. }
  616. /*
  617. * OMAP 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
  618. * read before the DMA controller finished disabling the channel.
  619. */
  620. static uint32_t omap_dma_chan_read_3_3(struct omap_chan *c, unsigned reg)
  621. {
  622. struct omap_dmadev *od = to_omap_dma_dev(c->vc.chan.device);
  623. uint32_t val;
  624. val = omap_dma_chan_read(c, reg);
  625. if (val == 0 && od->plat->errata & DMA_ERRATA_3_3)
  626. val = omap_dma_chan_read(c, reg);
  627. return val;
  628. }
  629. static dma_addr_t omap_dma_get_src_pos(struct omap_chan *c)
  630. {
  631. struct omap_dmadev *od = to_omap_dma_dev(c->vc.chan.device);
  632. dma_addr_t addr, cdac;
  633. if (__dma_omap15xx(od->plat->dma_attr)) {
  634. addr = omap_dma_chan_read(c, CPC);
  635. } else {
  636. addr = omap_dma_chan_read_3_3(c, CSAC);
  637. cdac = omap_dma_chan_read_3_3(c, CDAC);
  638. /*
  639. * CDAC == 0 indicates that the DMA transfer on the channel has
  640. * not been started (no data has been transferred so far).
  641. * Return the programmed source start address in this case.
  642. */
  643. if (cdac == 0)
  644. addr = omap_dma_chan_read(c, CSSA);
  645. }
  646. if (dma_omap1())
  647. addr |= omap_dma_chan_read(c, CSSA) & 0xffff0000;
  648. return addr;
  649. }
  650. static dma_addr_t omap_dma_get_dst_pos(struct omap_chan *c)
  651. {
  652. struct omap_dmadev *od = to_omap_dma_dev(c->vc.chan.device);
  653. dma_addr_t addr;
  654. if (__dma_omap15xx(od->plat->dma_attr)) {
  655. addr = omap_dma_chan_read(c, CPC);
  656. } else {
  657. addr = omap_dma_chan_read_3_3(c, CDAC);
  658. /*
  659. * CDAC == 0 indicates that the DMA transfer on the channel
  660. * has not been started (no data has been transferred so
  661. * far). Return the programmed destination start address in
  662. * this case.
  663. */
  664. if (addr == 0)
  665. addr = omap_dma_chan_read(c, CDSA);
  666. }
  667. if (dma_omap1())
  668. addr |= omap_dma_chan_read(c, CDSA) & 0xffff0000;
  669. return addr;
  670. }
  671. static enum dma_status omap_dma_tx_status(struct dma_chan *chan,
  672. dma_cookie_t cookie, struct dma_tx_state *txstate)
  673. {
  674. struct omap_chan *c = to_omap_dma_chan(chan);
  675. struct virt_dma_desc *vd;
  676. enum dma_status ret;
  677. unsigned long flags;
  678. ret = dma_cookie_status(chan, cookie, txstate);
  679. if (!c->paused && c->running) {
  680. uint32_t ccr = omap_dma_chan_read(c, CCR);
  681. /*
  682. * The channel is no longer active, set the return value
  683. * accordingly
  684. */
  685. if (!(ccr & CCR_ENABLE))
  686. ret = DMA_COMPLETE;
  687. }
  688. if (ret == DMA_COMPLETE || !txstate)
  689. return ret;
  690. spin_lock_irqsave(&c->vc.lock, flags);
  691. vd = vchan_find_desc(&c->vc, cookie);
  692. if (vd) {
  693. txstate->residue = omap_dma_desc_size(to_omap_dma_desc(&vd->tx));
  694. } else if (c->desc && c->desc->vd.tx.cookie == cookie) {
  695. struct omap_desc *d = c->desc;
  696. dma_addr_t pos;
  697. if (d->dir == DMA_MEM_TO_DEV)
  698. pos = omap_dma_get_src_pos(c);
  699. else if (d->dir == DMA_DEV_TO_MEM || d->dir == DMA_MEM_TO_MEM)
  700. pos = omap_dma_get_dst_pos(c);
  701. else
  702. pos = 0;
  703. txstate->residue = omap_dma_desc_size_pos(d, pos);
  704. } else {
  705. txstate->residue = 0;
  706. }
  707. if (ret == DMA_IN_PROGRESS && c->paused)
  708. ret = DMA_PAUSED;
  709. spin_unlock_irqrestore(&c->vc.lock, flags);
  710. return ret;
  711. }
  712. static void omap_dma_issue_pending(struct dma_chan *chan)
  713. {
  714. struct omap_chan *c = to_omap_dma_chan(chan);
  715. unsigned long flags;
  716. spin_lock_irqsave(&c->vc.lock, flags);
  717. if (vchan_issue_pending(&c->vc) && !c->desc)
  718. omap_dma_start_desc(c);
  719. spin_unlock_irqrestore(&c->vc.lock, flags);
  720. }
  721. static struct dma_async_tx_descriptor *omap_dma_prep_slave_sg(
  722. struct dma_chan *chan, struct scatterlist *sgl, unsigned sglen,
  723. enum dma_transfer_direction dir, unsigned long tx_flags, void *context)
  724. {
  725. struct omap_dmadev *od = to_omap_dma_dev(chan->device);
  726. struct omap_chan *c = to_omap_dma_chan(chan);
  727. enum dma_slave_buswidth dev_width;
  728. struct scatterlist *sgent;
  729. struct omap_desc *d;
  730. dma_addr_t dev_addr;
  731. unsigned i, es, en, frame_bytes;
  732. bool ll_failed = false;
  733. u32 burst;
  734. u32 port_window, port_window_bytes;
  735. if (dir == DMA_DEV_TO_MEM) {
  736. dev_addr = c->cfg.src_addr;
  737. dev_width = c->cfg.src_addr_width;
  738. burst = c->cfg.src_maxburst;
  739. port_window = c->cfg.src_port_window_size;
  740. } else if (dir == DMA_MEM_TO_DEV) {
  741. dev_addr = c->cfg.dst_addr;
  742. dev_width = c->cfg.dst_addr_width;
  743. burst = c->cfg.dst_maxburst;
  744. port_window = c->cfg.dst_port_window_size;
  745. } else {
  746. dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
  747. return NULL;
  748. }
  749. /* Bus width translates to the element size (ES) */
  750. switch (dev_width) {
  751. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  752. es = CSDP_DATA_TYPE_8;
  753. break;
  754. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  755. es = CSDP_DATA_TYPE_16;
  756. break;
  757. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  758. es = CSDP_DATA_TYPE_32;
  759. break;
  760. default: /* not reached */
  761. return NULL;
  762. }
  763. /* Now allocate and setup the descriptor. */
  764. d = kzalloc(sizeof(*d) + sglen * sizeof(d->sg[0]), GFP_ATOMIC);
  765. if (!d)
  766. return NULL;
  767. d->dir = dir;
  768. d->dev_addr = dev_addr;
  769. d->es = es;
  770. /* When the port_window is used, one frame must cover the window */
  771. if (port_window) {
  772. burst = port_window;
  773. port_window_bytes = port_window * es_bytes[es];
  774. d->ei = 1;
  775. /*
  776. * One frame covers the port_window and by configure
  777. * the source frame index to be -1 * (port_window - 1)
  778. * we instruct the sDMA that after a frame is processed
  779. * it should move back to the start of the window.
  780. */
  781. d->fi = -(port_window_bytes - 1);
  782. }
  783. d->ccr = c->ccr | CCR_SYNC_FRAME;
  784. if (dir == DMA_DEV_TO_MEM) {
  785. d->csdp = CSDP_DST_BURST_64 | CSDP_DST_PACKED;
  786. d->ccr |= CCR_DST_AMODE_POSTINC;
  787. if (port_window) {
  788. d->ccr |= CCR_SRC_AMODE_DBLIDX;
  789. if (port_window_bytes >= 64)
  790. d->csdp |= CSDP_SRC_BURST_64;
  791. else if (port_window_bytes >= 32)
  792. d->csdp |= CSDP_SRC_BURST_32;
  793. else if (port_window_bytes >= 16)
  794. d->csdp |= CSDP_SRC_BURST_16;
  795. } else {
  796. d->ccr |= CCR_SRC_AMODE_CONSTANT;
  797. }
  798. } else {
  799. d->csdp = CSDP_SRC_BURST_64 | CSDP_SRC_PACKED;
  800. d->ccr |= CCR_SRC_AMODE_POSTINC;
  801. if (port_window) {
  802. d->ccr |= CCR_DST_AMODE_DBLIDX;
  803. if (port_window_bytes >= 64)
  804. d->csdp |= CSDP_DST_BURST_64;
  805. else if (port_window_bytes >= 32)
  806. d->csdp |= CSDP_DST_BURST_32;
  807. else if (port_window_bytes >= 16)
  808. d->csdp |= CSDP_DST_BURST_16;
  809. } else {
  810. d->ccr |= CCR_DST_AMODE_CONSTANT;
  811. }
  812. }
  813. d->cicr = CICR_DROP_IE | CICR_BLOCK_IE;
  814. d->csdp |= es;
  815. if (dma_omap1()) {
  816. d->cicr |= CICR_TOUT_IE;
  817. if (dir == DMA_DEV_TO_MEM)
  818. d->csdp |= CSDP_DST_PORT_EMIFF | CSDP_SRC_PORT_TIPB;
  819. else
  820. d->csdp |= CSDP_DST_PORT_TIPB | CSDP_SRC_PORT_EMIFF;
  821. } else {
  822. if (dir == DMA_DEV_TO_MEM)
  823. d->ccr |= CCR_TRIGGER_SRC;
  824. d->cicr |= CICR_MISALIGNED_ERR_IE | CICR_TRANS_ERR_IE;
  825. if (port_window)
  826. d->csdp |= CSDP_WRITE_LAST_NON_POSTED;
  827. }
  828. if (od->plat->errata & DMA_ERRATA_PARALLEL_CHANNELS)
  829. d->clnk_ctrl = c->dma_ch;
  830. /*
  831. * Build our scatterlist entries: each contains the address,
  832. * the number of elements (EN) in each frame, and the number of
  833. * frames (FN). Number of bytes for this entry = ES * EN * FN.
  834. *
  835. * Burst size translates to number of elements with frame sync.
  836. * Note: DMA engine defines burst to be the number of dev-width
  837. * transfers.
  838. */
  839. en = burst;
  840. frame_bytes = es_bytes[es] * en;
  841. if (sglen >= 2)
  842. d->using_ll = od->ll123_supported;
  843. for_each_sg(sgl, sgent, sglen, i) {
  844. struct omap_sg *osg = &d->sg[i];
  845. osg->addr = sg_dma_address(sgent);
  846. osg->en = en;
  847. osg->fn = sg_dma_len(sgent) / frame_bytes;
  848. if (d->using_ll) {
  849. osg->t2_desc = dma_pool_alloc(od->desc_pool, GFP_ATOMIC,
  850. &osg->t2_desc_paddr);
  851. if (!osg->t2_desc) {
  852. dev_err(chan->device->dev,
  853. "t2_desc[%d] allocation failed\n", i);
  854. ll_failed = true;
  855. d->using_ll = false;
  856. continue;
  857. }
  858. omap_dma_fill_type2_desc(d, i, dir, (i == sglen - 1));
  859. }
  860. }
  861. d->sglen = sglen;
  862. /* Release the dma_pool entries if one allocation failed */
  863. if (ll_failed) {
  864. for (i = 0; i < d->sglen; i++) {
  865. struct omap_sg *osg = &d->sg[i];
  866. if (osg->t2_desc) {
  867. dma_pool_free(od->desc_pool, osg->t2_desc,
  868. osg->t2_desc_paddr);
  869. osg->t2_desc = NULL;
  870. }
  871. }
  872. }
  873. return vchan_tx_prep(&c->vc, &d->vd, tx_flags);
  874. }
  875. static struct dma_async_tx_descriptor *omap_dma_prep_dma_cyclic(
  876. struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  877. size_t period_len, enum dma_transfer_direction dir, unsigned long flags)
  878. {
  879. struct omap_dmadev *od = to_omap_dma_dev(chan->device);
  880. struct omap_chan *c = to_omap_dma_chan(chan);
  881. enum dma_slave_buswidth dev_width;
  882. struct omap_desc *d;
  883. dma_addr_t dev_addr;
  884. unsigned es;
  885. u32 burst;
  886. if (dir == DMA_DEV_TO_MEM) {
  887. dev_addr = c->cfg.src_addr;
  888. dev_width = c->cfg.src_addr_width;
  889. burst = c->cfg.src_maxburst;
  890. } else if (dir == DMA_MEM_TO_DEV) {
  891. dev_addr = c->cfg.dst_addr;
  892. dev_width = c->cfg.dst_addr_width;
  893. burst = c->cfg.dst_maxburst;
  894. } else {
  895. dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
  896. return NULL;
  897. }
  898. /* Bus width translates to the element size (ES) */
  899. switch (dev_width) {
  900. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  901. es = CSDP_DATA_TYPE_8;
  902. break;
  903. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  904. es = CSDP_DATA_TYPE_16;
  905. break;
  906. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  907. es = CSDP_DATA_TYPE_32;
  908. break;
  909. default: /* not reached */
  910. return NULL;
  911. }
  912. /* Now allocate and setup the descriptor. */
  913. d = kzalloc(sizeof(*d) + sizeof(d->sg[0]), GFP_ATOMIC);
  914. if (!d)
  915. return NULL;
  916. d->dir = dir;
  917. d->dev_addr = dev_addr;
  918. d->fi = burst;
  919. d->es = es;
  920. d->sg[0].addr = buf_addr;
  921. d->sg[0].en = period_len / es_bytes[es];
  922. d->sg[0].fn = buf_len / period_len;
  923. d->sglen = 1;
  924. d->ccr = c->ccr;
  925. if (dir == DMA_DEV_TO_MEM)
  926. d->ccr |= CCR_DST_AMODE_POSTINC | CCR_SRC_AMODE_CONSTANT;
  927. else
  928. d->ccr |= CCR_DST_AMODE_CONSTANT | CCR_SRC_AMODE_POSTINC;
  929. d->cicr = CICR_DROP_IE;
  930. if (flags & DMA_PREP_INTERRUPT)
  931. d->cicr |= CICR_FRAME_IE;
  932. d->csdp = es;
  933. if (dma_omap1()) {
  934. d->cicr |= CICR_TOUT_IE;
  935. if (dir == DMA_DEV_TO_MEM)
  936. d->csdp |= CSDP_DST_PORT_EMIFF | CSDP_SRC_PORT_MPUI;
  937. else
  938. d->csdp |= CSDP_DST_PORT_MPUI | CSDP_SRC_PORT_EMIFF;
  939. } else {
  940. if (burst)
  941. d->ccr |= CCR_SYNC_PACKET;
  942. else
  943. d->ccr |= CCR_SYNC_ELEMENT;
  944. if (dir == DMA_DEV_TO_MEM) {
  945. d->ccr |= CCR_TRIGGER_SRC;
  946. d->csdp |= CSDP_DST_PACKED;
  947. } else {
  948. d->csdp |= CSDP_SRC_PACKED;
  949. }
  950. d->cicr |= CICR_MISALIGNED_ERR_IE | CICR_TRANS_ERR_IE;
  951. d->csdp |= CSDP_DST_BURST_64 | CSDP_SRC_BURST_64;
  952. }
  953. if (__dma_omap15xx(od->plat->dma_attr))
  954. d->ccr |= CCR_AUTO_INIT | CCR_REPEAT;
  955. else
  956. d->clnk_ctrl = c->dma_ch | CLNK_CTRL_ENABLE_LNK;
  957. c->cyclic = true;
  958. return vchan_tx_prep(&c->vc, &d->vd, flags);
  959. }
  960. static struct dma_async_tx_descriptor *omap_dma_prep_dma_memcpy(
  961. struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  962. size_t len, unsigned long tx_flags)
  963. {
  964. struct omap_chan *c = to_omap_dma_chan(chan);
  965. struct omap_desc *d;
  966. uint8_t data_type;
  967. d = kzalloc(sizeof(*d) + sizeof(d->sg[0]), GFP_ATOMIC);
  968. if (!d)
  969. return NULL;
  970. data_type = __ffs((src | dest | len));
  971. if (data_type > CSDP_DATA_TYPE_32)
  972. data_type = CSDP_DATA_TYPE_32;
  973. d->dir = DMA_MEM_TO_MEM;
  974. d->dev_addr = src;
  975. d->fi = 0;
  976. d->es = data_type;
  977. d->sg[0].en = len / BIT(data_type);
  978. d->sg[0].fn = 1;
  979. d->sg[0].addr = dest;
  980. d->sglen = 1;
  981. d->ccr = c->ccr;
  982. d->ccr |= CCR_DST_AMODE_POSTINC | CCR_SRC_AMODE_POSTINC;
  983. d->cicr = CICR_DROP_IE | CICR_FRAME_IE;
  984. d->csdp = data_type;
  985. if (dma_omap1()) {
  986. d->cicr |= CICR_TOUT_IE;
  987. d->csdp |= CSDP_DST_PORT_EMIFF | CSDP_SRC_PORT_EMIFF;
  988. } else {
  989. d->csdp |= CSDP_DST_PACKED | CSDP_SRC_PACKED;
  990. d->cicr |= CICR_MISALIGNED_ERR_IE | CICR_TRANS_ERR_IE;
  991. d->csdp |= CSDP_DST_BURST_64 | CSDP_SRC_BURST_64;
  992. }
  993. return vchan_tx_prep(&c->vc, &d->vd, tx_flags);
  994. }
  995. static struct dma_async_tx_descriptor *omap_dma_prep_dma_interleaved(
  996. struct dma_chan *chan, struct dma_interleaved_template *xt,
  997. unsigned long flags)
  998. {
  999. struct omap_chan *c = to_omap_dma_chan(chan);
  1000. struct omap_desc *d;
  1001. struct omap_sg *sg;
  1002. uint8_t data_type;
  1003. size_t src_icg, dst_icg;
  1004. /* Slave mode is not supported */
  1005. if (is_slave_direction(xt->dir))
  1006. return NULL;
  1007. if (xt->frame_size != 1 || xt->numf == 0)
  1008. return NULL;
  1009. d = kzalloc(sizeof(*d) + sizeof(d->sg[0]), GFP_ATOMIC);
  1010. if (!d)
  1011. return NULL;
  1012. data_type = __ffs((xt->src_start | xt->dst_start | xt->sgl[0].size));
  1013. if (data_type > CSDP_DATA_TYPE_32)
  1014. data_type = CSDP_DATA_TYPE_32;
  1015. sg = &d->sg[0];
  1016. d->dir = DMA_MEM_TO_MEM;
  1017. d->dev_addr = xt->src_start;
  1018. d->es = data_type;
  1019. sg->en = xt->sgl[0].size / BIT(data_type);
  1020. sg->fn = xt->numf;
  1021. sg->addr = xt->dst_start;
  1022. d->sglen = 1;
  1023. d->ccr = c->ccr;
  1024. src_icg = dmaengine_get_src_icg(xt, &xt->sgl[0]);
  1025. dst_icg = dmaengine_get_dst_icg(xt, &xt->sgl[0]);
  1026. if (src_icg) {
  1027. d->ccr |= CCR_SRC_AMODE_DBLIDX;
  1028. d->ei = 1;
  1029. d->fi = src_icg;
  1030. } else if (xt->src_inc) {
  1031. d->ccr |= CCR_SRC_AMODE_POSTINC;
  1032. d->fi = 0;
  1033. } else {
  1034. dev_err(chan->device->dev,
  1035. "%s: SRC constant addressing is not supported\n",
  1036. __func__);
  1037. kfree(d);
  1038. return NULL;
  1039. }
  1040. if (dst_icg) {
  1041. d->ccr |= CCR_DST_AMODE_DBLIDX;
  1042. sg->ei = 1;
  1043. sg->fi = dst_icg;
  1044. } else if (xt->dst_inc) {
  1045. d->ccr |= CCR_DST_AMODE_POSTINC;
  1046. sg->fi = 0;
  1047. } else {
  1048. dev_err(chan->device->dev,
  1049. "%s: DST constant addressing is not supported\n",
  1050. __func__);
  1051. kfree(d);
  1052. return NULL;
  1053. }
  1054. d->cicr = CICR_DROP_IE | CICR_FRAME_IE;
  1055. d->csdp = data_type;
  1056. if (dma_omap1()) {
  1057. d->cicr |= CICR_TOUT_IE;
  1058. d->csdp |= CSDP_DST_PORT_EMIFF | CSDP_SRC_PORT_EMIFF;
  1059. } else {
  1060. d->csdp |= CSDP_DST_PACKED | CSDP_SRC_PACKED;
  1061. d->cicr |= CICR_MISALIGNED_ERR_IE | CICR_TRANS_ERR_IE;
  1062. d->csdp |= CSDP_DST_BURST_64 | CSDP_SRC_BURST_64;
  1063. }
  1064. return vchan_tx_prep(&c->vc, &d->vd, flags);
  1065. }
  1066. static int omap_dma_slave_config(struct dma_chan *chan, struct dma_slave_config *cfg)
  1067. {
  1068. struct omap_chan *c = to_omap_dma_chan(chan);
  1069. if (cfg->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
  1070. cfg->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
  1071. return -EINVAL;
  1072. memcpy(&c->cfg, cfg, sizeof(c->cfg));
  1073. return 0;
  1074. }
  1075. static int omap_dma_terminate_all(struct dma_chan *chan)
  1076. {
  1077. struct omap_chan *c = to_omap_dma_chan(chan);
  1078. unsigned long flags;
  1079. LIST_HEAD(head);
  1080. spin_lock_irqsave(&c->vc.lock, flags);
  1081. /*
  1082. * Stop DMA activity: we assume the callback will not be called
  1083. * after omap_dma_stop() returns (even if it does, it will see
  1084. * c->desc is NULL and exit.)
  1085. */
  1086. if (c->desc) {
  1087. omap_dma_desc_free(&c->desc->vd);
  1088. c->desc = NULL;
  1089. /* Avoid stopping the dma twice */
  1090. if (!c->paused)
  1091. omap_dma_stop(c);
  1092. }
  1093. c->cyclic = false;
  1094. c->paused = false;
  1095. vchan_get_all_descriptors(&c->vc, &head);
  1096. spin_unlock_irqrestore(&c->vc.lock, flags);
  1097. vchan_dma_desc_free_list(&c->vc, &head);
  1098. return 0;
  1099. }
  1100. static void omap_dma_synchronize(struct dma_chan *chan)
  1101. {
  1102. struct omap_chan *c = to_omap_dma_chan(chan);
  1103. vchan_synchronize(&c->vc);
  1104. }
  1105. static int omap_dma_pause(struct dma_chan *chan)
  1106. {
  1107. struct omap_chan *c = to_omap_dma_chan(chan);
  1108. struct omap_dmadev *od = to_omap_dma_dev(chan->device);
  1109. unsigned long flags;
  1110. int ret = -EINVAL;
  1111. bool can_pause = false;
  1112. spin_lock_irqsave(&od->irq_lock, flags);
  1113. if (!c->desc)
  1114. goto out;
  1115. if (c->cyclic)
  1116. can_pause = true;
  1117. /*
  1118. * We do not allow DMA_MEM_TO_DEV transfers to be paused.
  1119. * From the AM572x TRM, 16.1.4.18 Disabling a Channel During Transfer:
  1120. * "When a channel is disabled during a transfer, the channel undergoes
  1121. * an abort, unless it is hardware-source-synchronized …".
  1122. * A source-synchronised channel is one where the fetching of data is
  1123. * under control of the device. In other words, a device-to-memory
  1124. * transfer. So, a destination-synchronised channel (which would be a
  1125. * memory-to-device transfer) undergoes an abort if the the CCR_ENABLE
  1126. * bit is cleared.
  1127. * From 16.1.4.20.4.6.2 Abort: "If an abort trigger occurs, the channel
  1128. * aborts immediately after completion of current read/write
  1129. * transactions and then the FIFO is cleaned up." The term "cleaned up"
  1130. * is not defined. TI recommends to check that RD_ACTIVE and WR_ACTIVE
  1131. * are both clear _before_ disabling the channel, otherwise data loss
  1132. * will occur.
  1133. * The problem is that if the channel is active, then device activity
  1134. * can result in DMA activity starting between reading those as both
  1135. * clear and the write to DMA_CCR to clear the enable bit hitting the
  1136. * hardware. If the DMA hardware can't drain the data in its FIFO to the
  1137. * destination, then data loss "might" occur (say if we write to an UART
  1138. * and the UART is not accepting any further data).
  1139. */
  1140. else if (c->desc->dir == DMA_DEV_TO_MEM)
  1141. can_pause = true;
  1142. if (can_pause && !c->paused) {
  1143. ret = omap_dma_stop(c);
  1144. if (!ret)
  1145. c->paused = true;
  1146. }
  1147. out:
  1148. spin_unlock_irqrestore(&od->irq_lock, flags);
  1149. return ret;
  1150. }
  1151. static int omap_dma_resume(struct dma_chan *chan)
  1152. {
  1153. struct omap_chan *c = to_omap_dma_chan(chan);
  1154. struct omap_dmadev *od = to_omap_dma_dev(chan->device);
  1155. unsigned long flags;
  1156. int ret = -EINVAL;
  1157. spin_lock_irqsave(&od->irq_lock, flags);
  1158. if (c->paused && c->desc) {
  1159. mb();
  1160. /* Restore channel link register */
  1161. omap_dma_chan_write(c, CLNK_CTRL, c->desc->clnk_ctrl);
  1162. omap_dma_start(c, c->desc);
  1163. c->paused = false;
  1164. ret = 0;
  1165. }
  1166. spin_unlock_irqrestore(&od->irq_lock, flags);
  1167. return ret;
  1168. }
  1169. static int omap_dma_chan_init(struct omap_dmadev *od)
  1170. {
  1171. struct omap_chan *c;
  1172. c = kzalloc(sizeof(*c), GFP_KERNEL);
  1173. if (!c)
  1174. return -ENOMEM;
  1175. c->reg_map = od->reg_map;
  1176. c->vc.desc_free = omap_dma_desc_free;
  1177. vchan_init(&c->vc, &od->ddev);
  1178. return 0;
  1179. }
  1180. static void omap_dma_free(struct omap_dmadev *od)
  1181. {
  1182. while (!list_empty(&od->ddev.channels)) {
  1183. struct omap_chan *c = list_first_entry(&od->ddev.channels,
  1184. struct omap_chan, vc.chan.device_node);
  1185. list_del(&c->vc.chan.device_node);
  1186. tasklet_kill(&c->vc.task);
  1187. kfree(c);
  1188. }
  1189. }
  1190. #define OMAP_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
  1191. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
  1192. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
  1193. static int omap_dma_probe(struct platform_device *pdev)
  1194. {
  1195. struct omap_dmadev *od;
  1196. struct resource *res;
  1197. int rc, i, irq;
  1198. u32 lch_count;
  1199. od = devm_kzalloc(&pdev->dev, sizeof(*od), GFP_KERNEL);
  1200. if (!od)
  1201. return -ENOMEM;
  1202. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1203. od->base = devm_ioremap_resource(&pdev->dev, res);
  1204. if (IS_ERR(od->base))
  1205. return PTR_ERR(od->base);
  1206. od->plat = omap_get_plat_info();
  1207. if (!od->plat)
  1208. return -EPROBE_DEFER;
  1209. od->reg_map = od->plat->reg_map;
  1210. dma_cap_set(DMA_SLAVE, od->ddev.cap_mask);
  1211. dma_cap_set(DMA_CYCLIC, od->ddev.cap_mask);
  1212. dma_cap_set(DMA_MEMCPY, od->ddev.cap_mask);
  1213. dma_cap_set(DMA_INTERLEAVE, od->ddev.cap_mask);
  1214. od->ddev.device_alloc_chan_resources = omap_dma_alloc_chan_resources;
  1215. od->ddev.device_free_chan_resources = omap_dma_free_chan_resources;
  1216. od->ddev.device_tx_status = omap_dma_tx_status;
  1217. od->ddev.device_issue_pending = omap_dma_issue_pending;
  1218. od->ddev.device_prep_slave_sg = omap_dma_prep_slave_sg;
  1219. od->ddev.device_prep_dma_cyclic = omap_dma_prep_dma_cyclic;
  1220. od->ddev.device_prep_dma_memcpy = omap_dma_prep_dma_memcpy;
  1221. od->ddev.device_prep_interleaved_dma = omap_dma_prep_dma_interleaved;
  1222. od->ddev.device_config = omap_dma_slave_config;
  1223. od->ddev.device_pause = omap_dma_pause;
  1224. od->ddev.device_resume = omap_dma_resume;
  1225. od->ddev.device_terminate_all = omap_dma_terminate_all;
  1226. od->ddev.device_synchronize = omap_dma_synchronize;
  1227. od->ddev.src_addr_widths = OMAP_DMA_BUSWIDTHS;
  1228. od->ddev.dst_addr_widths = OMAP_DMA_BUSWIDTHS;
  1229. od->ddev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  1230. od->ddev.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  1231. od->ddev.dev = &pdev->dev;
  1232. INIT_LIST_HEAD(&od->ddev.channels);
  1233. spin_lock_init(&od->lock);
  1234. spin_lock_init(&od->irq_lock);
  1235. /* Number of DMA requests */
  1236. od->dma_requests = OMAP_SDMA_REQUESTS;
  1237. if (pdev->dev.of_node && of_property_read_u32(pdev->dev.of_node,
  1238. "dma-requests",
  1239. &od->dma_requests)) {
  1240. dev_info(&pdev->dev,
  1241. "Missing dma-requests property, using %u.\n",
  1242. OMAP_SDMA_REQUESTS);
  1243. }
  1244. /* Number of available logical channels */
  1245. if (!pdev->dev.of_node) {
  1246. lch_count = od->plat->dma_attr->lch_count;
  1247. if (unlikely(!lch_count))
  1248. lch_count = OMAP_SDMA_CHANNELS;
  1249. } else if (of_property_read_u32(pdev->dev.of_node, "dma-channels",
  1250. &lch_count)) {
  1251. dev_info(&pdev->dev,
  1252. "Missing dma-channels property, using %u.\n",
  1253. OMAP_SDMA_CHANNELS);
  1254. lch_count = OMAP_SDMA_CHANNELS;
  1255. }
  1256. od->lch_map = devm_kcalloc(&pdev->dev, lch_count, sizeof(*od->lch_map),
  1257. GFP_KERNEL);
  1258. if (!od->lch_map)
  1259. return -ENOMEM;
  1260. for (i = 0; i < od->dma_requests; i++) {
  1261. rc = omap_dma_chan_init(od);
  1262. if (rc) {
  1263. omap_dma_free(od);
  1264. return rc;
  1265. }
  1266. }
  1267. irq = platform_get_irq(pdev, 1);
  1268. if (irq <= 0) {
  1269. dev_info(&pdev->dev, "failed to get L1 IRQ: %d\n", irq);
  1270. od->legacy = true;
  1271. } else {
  1272. /* Disable all interrupts */
  1273. od->irq_enable_mask = 0;
  1274. omap_dma_glbl_write(od, IRQENABLE_L1, 0);
  1275. rc = devm_request_irq(&pdev->dev, irq, omap_dma_irq,
  1276. IRQF_SHARED, "omap-dma-engine", od);
  1277. if (rc) {
  1278. omap_dma_free(od);
  1279. return rc;
  1280. }
  1281. }
  1282. if (omap_dma_glbl_read(od, CAPS_0) & CAPS_0_SUPPORT_LL123)
  1283. od->ll123_supported = true;
  1284. od->ddev.filter.map = od->plat->slave_map;
  1285. od->ddev.filter.mapcnt = od->plat->slavecnt;
  1286. od->ddev.filter.fn = omap_dma_filter_fn;
  1287. if (od->ll123_supported) {
  1288. od->desc_pool = dma_pool_create(dev_name(&pdev->dev),
  1289. &pdev->dev,
  1290. sizeof(struct omap_type2_desc),
  1291. 4, 0);
  1292. if (!od->desc_pool) {
  1293. dev_err(&pdev->dev,
  1294. "unable to allocate descriptor pool\n");
  1295. od->ll123_supported = false;
  1296. }
  1297. }
  1298. rc = dma_async_device_register(&od->ddev);
  1299. if (rc) {
  1300. pr_warn("OMAP-DMA: failed to register slave DMA engine device: %d\n",
  1301. rc);
  1302. omap_dma_free(od);
  1303. return rc;
  1304. }
  1305. platform_set_drvdata(pdev, od);
  1306. if (pdev->dev.of_node) {
  1307. omap_dma_info.dma_cap = od->ddev.cap_mask;
  1308. /* Device-tree DMA controller registration */
  1309. rc = of_dma_controller_register(pdev->dev.of_node,
  1310. of_dma_simple_xlate, &omap_dma_info);
  1311. if (rc) {
  1312. pr_warn("OMAP-DMA: failed to register DMA controller\n");
  1313. dma_async_device_unregister(&od->ddev);
  1314. omap_dma_free(od);
  1315. }
  1316. }
  1317. dev_info(&pdev->dev, "OMAP DMA engine driver%s\n",
  1318. od->ll123_supported ? " (LinkedList1/2/3 supported)" : "");
  1319. return rc;
  1320. }
  1321. static int omap_dma_remove(struct platform_device *pdev)
  1322. {
  1323. struct omap_dmadev *od = platform_get_drvdata(pdev);
  1324. int irq;
  1325. if (pdev->dev.of_node)
  1326. of_dma_controller_free(pdev->dev.of_node);
  1327. irq = platform_get_irq(pdev, 1);
  1328. devm_free_irq(&pdev->dev, irq, od);
  1329. dma_async_device_unregister(&od->ddev);
  1330. if (!od->legacy) {
  1331. /* Disable all interrupts */
  1332. omap_dma_glbl_write(od, IRQENABLE_L0, 0);
  1333. }
  1334. if (od->ll123_supported)
  1335. dma_pool_destroy(od->desc_pool);
  1336. omap_dma_free(od);
  1337. return 0;
  1338. }
  1339. static const struct of_device_id omap_dma_match[] = {
  1340. { .compatible = "ti,omap2420-sdma", },
  1341. { .compatible = "ti,omap2430-sdma", },
  1342. { .compatible = "ti,omap3430-sdma", },
  1343. { .compatible = "ti,omap3630-sdma", },
  1344. { .compatible = "ti,omap4430-sdma", },
  1345. {},
  1346. };
  1347. MODULE_DEVICE_TABLE(of, omap_dma_match);
  1348. static struct platform_driver omap_dma_driver = {
  1349. .probe = omap_dma_probe,
  1350. .remove = omap_dma_remove,
  1351. .driver = {
  1352. .name = "omap-dma-engine",
  1353. .of_match_table = of_match_ptr(omap_dma_match),
  1354. },
  1355. };
  1356. bool omap_dma_filter_fn(struct dma_chan *chan, void *param)
  1357. {
  1358. if (chan->device->dev->driver == &omap_dma_driver.driver) {
  1359. struct omap_dmadev *od = to_omap_dma_dev(chan->device);
  1360. struct omap_chan *c = to_omap_dma_chan(chan);
  1361. unsigned req = *(unsigned *)param;
  1362. if (req <= od->dma_requests) {
  1363. c->dma_sig = req;
  1364. return true;
  1365. }
  1366. }
  1367. return false;
  1368. }
  1369. EXPORT_SYMBOL_GPL(omap_dma_filter_fn);
  1370. static int omap_dma_init(void)
  1371. {
  1372. return platform_driver_register(&omap_dma_driver);
  1373. }
  1374. subsys_initcall(omap_dma_init);
  1375. static void __exit omap_dma_exit(void)
  1376. {
  1377. platform_driver_unregister(&omap_dma_driver);
  1378. }
  1379. module_exit(omap_dma_exit);
  1380. MODULE_AUTHOR("Russell King");
  1381. MODULE_LICENSE("GPL");