omap-dma.c 37 KB

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