edma.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529
  1. /*
  2. * EDMA3 support for DaVinci
  3. *
  4. * Copyright (C) 2006-2009 Texas Instruments.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/err.h>
  21. #include <linux/kernel.h>
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/io.h>
  27. #include <linux/slab.h>
  28. #include <linux/platform_data/edma.h>
  29. /* Offsets matching "struct edmacc_param" */
  30. #define PARM_OPT 0x00
  31. #define PARM_SRC 0x04
  32. #define PARM_A_B_CNT 0x08
  33. #define PARM_DST 0x0c
  34. #define PARM_SRC_DST_BIDX 0x10
  35. #define PARM_LINK_BCNTRLD 0x14
  36. #define PARM_SRC_DST_CIDX 0x18
  37. #define PARM_CCNT 0x1c
  38. #define PARM_SIZE 0x20
  39. /* Offsets for EDMA CC global channel registers and their shadows */
  40. #define SH_ER 0x00 /* 64 bits */
  41. #define SH_ECR 0x08 /* 64 bits */
  42. #define SH_ESR 0x10 /* 64 bits */
  43. #define SH_CER 0x18 /* 64 bits */
  44. #define SH_EER 0x20 /* 64 bits */
  45. #define SH_EECR 0x28 /* 64 bits */
  46. #define SH_EESR 0x30 /* 64 bits */
  47. #define SH_SER 0x38 /* 64 bits */
  48. #define SH_SECR 0x40 /* 64 bits */
  49. #define SH_IER 0x50 /* 64 bits */
  50. #define SH_IECR 0x58 /* 64 bits */
  51. #define SH_IESR 0x60 /* 64 bits */
  52. #define SH_IPR 0x68 /* 64 bits */
  53. #define SH_ICR 0x70 /* 64 bits */
  54. #define SH_IEVAL 0x78
  55. #define SH_QER 0x80
  56. #define SH_QEER 0x84
  57. #define SH_QEECR 0x88
  58. #define SH_QEESR 0x8c
  59. #define SH_QSER 0x90
  60. #define SH_QSECR 0x94
  61. #define SH_SIZE 0x200
  62. /* Offsets for EDMA CC global registers */
  63. #define EDMA_REV 0x0000
  64. #define EDMA_CCCFG 0x0004
  65. #define EDMA_QCHMAP 0x0200 /* 8 registers */
  66. #define EDMA_DMAQNUM 0x0240 /* 8 registers (4 on OMAP-L1xx) */
  67. #define EDMA_QDMAQNUM 0x0260
  68. #define EDMA_QUETCMAP 0x0280
  69. #define EDMA_QUEPRI 0x0284
  70. #define EDMA_EMR 0x0300 /* 64 bits */
  71. #define EDMA_EMCR 0x0308 /* 64 bits */
  72. #define EDMA_QEMR 0x0310
  73. #define EDMA_QEMCR 0x0314
  74. #define EDMA_CCERR 0x0318
  75. #define EDMA_CCERRCLR 0x031c
  76. #define EDMA_EEVAL 0x0320
  77. #define EDMA_DRAE 0x0340 /* 4 x 64 bits*/
  78. #define EDMA_QRAE 0x0380 /* 4 registers */
  79. #define EDMA_QUEEVTENTRY 0x0400 /* 2 x 16 registers */
  80. #define EDMA_QSTAT 0x0600 /* 2 registers */
  81. #define EDMA_QWMTHRA 0x0620
  82. #define EDMA_QWMTHRB 0x0624
  83. #define EDMA_CCSTAT 0x0640
  84. #define EDMA_M 0x1000 /* global channel registers */
  85. #define EDMA_ECR 0x1008
  86. #define EDMA_ECRH 0x100C
  87. #define EDMA_SHADOW0 0x2000 /* 4 regions shadowing global channels */
  88. #define EDMA_PARM 0x4000 /* 128 param entries */
  89. #define PARM_OFFSET(param_no) (EDMA_PARM + ((param_no) << 5))
  90. #define EDMA_DCHMAP 0x0100 /* 64 registers */
  91. #define CHMAP_EXIST BIT(24)
  92. #define EDMA_MAX_DMACH 64
  93. #define EDMA_MAX_PARAMENTRY 512
  94. /*****************************************************************************/
  95. static void __iomem *edmacc_regs_base[EDMA_MAX_CC];
  96. static inline unsigned int edma_read(unsigned ctlr, int offset)
  97. {
  98. return (unsigned int)__raw_readl(edmacc_regs_base[ctlr] + offset);
  99. }
  100. static inline void edma_write(unsigned ctlr, int offset, int val)
  101. {
  102. __raw_writel(val, edmacc_regs_base[ctlr] + offset);
  103. }
  104. static inline void edma_modify(unsigned ctlr, int offset, unsigned and,
  105. unsigned or)
  106. {
  107. unsigned val = edma_read(ctlr, offset);
  108. val &= and;
  109. val |= or;
  110. edma_write(ctlr, offset, val);
  111. }
  112. static inline void edma_and(unsigned ctlr, int offset, unsigned and)
  113. {
  114. unsigned val = edma_read(ctlr, offset);
  115. val &= and;
  116. edma_write(ctlr, offset, val);
  117. }
  118. static inline void edma_or(unsigned ctlr, int offset, unsigned or)
  119. {
  120. unsigned val = edma_read(ctlr, offset);
  121. val |= or;
  122. edma_write(ctlr, offset, val);
  123. }
  124. static inline unsigned int edma_read_array(unsigned ctlr, int offset, int i)
  125. {
  126. return edma_read(ctlr, offset + (i << 2));
  127. }
  128. static inline void edma_write_array(unsigned ctlr, int offset, int i,
  129. unsigned val)
  130. {
  131. edma_write(ctlr, offset + (i << 2), val);
  132. }
  133. static inline void edma_modify_array(unsigned ctlr, int offset, int i,
  134. unsigned and, unsigned or)
  135. {
  136. edma_modify(ctlr, offset + (i << 2), and, or);
  137. }
  138. static inline void edma_or_array(unsigned ctlr, int offset, int i, unsigned or)
  139. {
  140. edma_or(ctlr, offset + (i << 2), or);
  141. }
  142. static inline void edma_or_array2(unsigned ctlr, int offset, int i, int j,
  143. unsigned or)
  144. {
  145. edma_or(ctlr, offset + ((i*2 + j) << 2), or);
  146. }
  147. static inline void edma_write_array2(unsigned ctlr, int offset, int i, int j,
  148. unsigned val)
  149. {
  150. edma_write(ctlr, offset + ((i*2 + j) << 2), val);
  151. }
  152. static inline unsigned int edma_shadow0_read(unsigned ctlr, int offset)
  153. {
  154. return edma_read(ctlr, EDMA_SHADOW0 + offset);
  155. }
  156. static inline unsigned int edma_shadow0_read_array(unsigned ctlr, int offset,
  157. int i)
  158. {
  159. return edma_read(ctlr, EDMA_SHADOW0 + offset + (i << 2));
  160. }
  161. static inline void edma_shadow0_write(unsigned ctlr, int offset, unsigned val)
  162. {
  163. edma_write(ctlr, EDMA_SHADOW0 + offset, val);
  164. }
  165. static inline void edma_shadow0_write_array(unsigned ctlr, int offset, int i,
  166. unsigned val)
  167. {
  168. edma_write(ctlr, EDMA_SHADOW0 + offset + (i << 2), val);
  169. }
  170. static inline unsigned int edma_parm_read(unsigned ctlr, int offset,
  171. int param_no)
  172. {
  173. return edma_read(ctlr, EDMA_PARM + offset + (param_no << 5));
  174. }
  175. static inline void edma_parm_write(unsigned ctlr, int offset, int param_no,
  176. unsigned val)
  177. {
  178. edma_write(ctlr, EDMA_PARM + offset + (param_no << 5), val);
  179. }
  180. static inline void edma_parm_modify(unsigned ctlr, int offset, int param_no,
  181. unsigned and, unsigned or)
  182. {
  183. edma_modify(ctlr, EDMA_PARM + offset + (param_no << 5), and, or);
  184. }
  185. static inline void edma_parm_and(unsigned ctlr, int offset, int param_no,
  186. unsigned and)
  187. {
  188. edma_and(ctlr, EDMA_PARM + offset + (param_no << 5), and);
  189. }
  190. static inline void edma_parm_or(unsigned ctlr, int offset, int param_no,
  191. unsigned or)
  192. {
  193. edma_or(ctlr, EDMA_PARM + offset + (param_no << 5), or);
  194. }
  195. static inline void set_bits(int offset, int len, unsigned long *p)
  196. {
  197. for (; len > 0; len--)
  198. set_bit(offset + (len - 1), p);
  199. }
  200. static inline void clear_bits(int offset, int len, unsigned long *p)
  201. {
  202. for (; len > 0; len--)
  203. clear_bit(offset + (len - 1), p);
  204. }
  205. /*****************************************************************************/
  206. /* actual number of DMA channels and slots on this silicon */
  207. struct edma {
  208. /* how many dma resources of each type */
  209. unsigned num_channels;
  210. unsigned num_region;
  211. unsigned num_slots;
  212. unsigned num_tc;
  213. unsigned num_cc;
  214. enum dma_event_q default_queue;
  215. /* list of channels with no even trigger; terminated by "-1" */
  216. const s8 *noevent;
  217. /* The edma_inuse bit for each PaRAM slot is clear unless the
  218. * channel is in use ... by ARM or DSP, for QDMA, or whatever.
  219. */
  220. DECLARE_BITMAP(edma_inuse, EDMA_MAX_PARAMENTRY);
  221. /* The edma_unused bit for each channel is clear unless
  222. * it is not being used on this platform. It uses a bit
  223. * of SOC-specific initialization code.
  224. */
  225. DECLARE_BITMAP(edma_unused, EDMA_MAX_DMACH);
  226. unsigned irq_res_start;
  227. unsigned irq_res_end;
  228. struct dma_interrupt_data {
  229. void (*callback)(unsigned channel, unsigned short ch_status,
  230. void *data);
  231. void *data;
  232. } intr_data[EDMA_MAX_DMACH];
  233. };
  234. static struct edma *edma_cc[EDMA_MAX_CC];
  235. static int arch_num_cc;
  236. /* dummy param set used to (re)initialize parameter RAM slots */
  237. static const struct edmacc_param dummy_paramset = {
  238. .link_bcntrld = 0xffff,
  239. .ccnt = 1,
  240. };
  241. /*****************************************************************************/
  242. static void map_dmach_queue(unsigned ctlr, unsigned ch_no,
  243. enum dma_event_q queue_no)
  244. {
  245. int bit = (ch_no & 0x7) * 4;
  246. /* default to low priority queue */
  247. if (queue_no == EVENTQ_DEFAULT)
  248. queue_no = edma_cc[ctlr]->default_queue;
  249. queue_no &= 7;
  250. edma_modify_array(ctlr, EDMA_DMAQNUM, (ch_no >> 3),
  251. ~(0x7 << bit), queue_no << bit);
  252. }
  253. static void __init map_queue_tc(unsigned ctlr, int queue_no, int tc_no)
  254. {
  255. int bit = queue_no * 4;
  256. edma_modify(ctlr, EDMA_QUETCMAP, ~(0x7 << bit), ((tc_no & 0x7) << bit));
  257. }
  258. static void __init assign_priority_to_queue(unsigned ctlr, int queue_no,
  259. int priority)
  260. {
  261. int bit = queue_no * 4;
  262. edma_modify(ctlr, EDMA_QUEPRI, ~(0x7 << bit),
  263. ((priority & 0x7) << bit));
  264. }
  265. /**
  266. * map_dmach_param - Maps channel number to param entry number
  267. *
  268. * This maps the dma channel number to param entry numberter. In
  269. * other words using the DMA channel mapping registers a param entry
  270. * can be mapped to any channel
  271. *
  272. * Callers are responsible for ensuring the channel mapping logic is
  273. * included in that particular EDMA variant (Eg : dm646x)
  274. *
  275. */
  276. static void __init map_dmach_param(unsigned ctlr)
  277. {
  278. int i;
  279. for (i = 0; i < EDMA_MAX_DMACH; i++)
  280. edma_write_array(ctlr, EDMA_DCHMAP , i , (i << 5));
  281. }
  282. static inline void
  283. setup_dma_interrupt(unsigned lch,
  284. void (*callback)(unsigned channel, u16 ch_status, void *data),
  285. void *data)
  286. {
  287. unsigned ctlr;
  288. ctlr = EDMA_CTLR(lch);
  289. lch = EDMA_CHAN_SLOT(lch);
  290. if (!callback)
  291. edma_shadow0_write_array(ctlr, SH_IECR, lch >> 5,
  292. BIT(lch & 0x1f));
  293. edma_cc[ctlr]->intr_data[lch].callback = callback;
  294. edma_cc[ctlr]->intr_data[lch].data = data;
  295. if (callback) {
  296. edma_shadow0_write_array(ctlr, SH_ICR, lch >> 5,
  297. BIT(lch & 0x1f));
  298. edma_shadow0_write_array(ctlr, SH_IESR, lch >> 5,
  299. BIT(lch & 0x1f));
  300. }
  301. }
  302. static int irq2ctlr(int irq)
  303. {
  304. if (irq >= edma_cc[0]->irq_res_start && irq <= edma_cc[0]->irq_res_end)
  305. return 0;
  306. else if (irq >= edma_cc[1]->irq_res_start &&
  307. irq <= edma_cc[1]->irq_res_end)
  308. return 1;
  309. return -1;
  310. }
  311. /******************************************************************************
  312. *
  313. * DMA interrupt handler
  314. *
  315. *****************************************************************************/
  316. static irqreturn_t dma_irq_handler(int irq, void *data)
  317. {
  318. int ctlr;
  319. u32 sh_ier;
  320. u32 sh_ipr;
  321. u32 bank;
  322. ctlr = irq2ctlr(irq);
  323. if (ctlr < 0)
  324. return IRQ_NONE;
  325. dev_dbg(data, "dma_irq_handler\n");
  326. sh_ipr = edma_shadow0_read_array(ctlr, SH_IPR, 0);
  327. if (!sh_ipr) {
  328. sh_ipr = edma_shadow0_read_array(ctlr, SH_IPR, 1);
  329. if (!sh_ipr)
  330. return IRQ_NONE;
  331. sh_ier = edma_shadow0_read_array(ctlr, SH_IER, 1);
  332. bank = 1;
  333. } else {
  334. sh_ier = edma_shadow0_read_array(ctlr, SH_IER, 0);
  335. bank = 0;
  336. }
  337. do {
  338. u32 slot;
  339. u32 channel;
  340. dev_dbg(data, "IPR%d %08x\n", bank, sh_ipr);
  341. slot = __ffs(sh_ipr);
  342. sh_ipr &= ~(BIT(slot));
  343. if (sh_ier & BIT(slot)) {
  344. channel = (bank << 5) | slot;
  345. /* Clear the corresponding IPR bits */
  346. edma_shadow0_write_array(ctlr, SH_ICR, bank,
  347. BIT(slot));
  348. if (edma_cc[ctlr]->intr_data[channel].callback)
  349. edma_cc[ctlr]->intr_data[channel].callback(
  350. channel, DMA_COMPLETE,
  351. edma_cc[ctlr]->intr_data[channel].data);
  352. }
  353. } while (sh_ipr);
  354. edma_shadow0_write(ctlr, SH_IEVAL, 1);
  355. return IRQ_HANDLED;
  356. }
  357. /******************************************************************************
  358. *
  359. * DMA error interrupt handler
  360. *
  361. *****************************************************************************/
  362. static irqreturn_t dma_ccerr_handler(int irq, void *data)
  363. {
  364. int i;
  365. int ctlr;
  366. unsigned int cnt = 0;
  367. ctlr = irq2ctlr(irq);
  368. if (ctlr < 0)
  369. return IRQ_NONE;
  370. dev_dbg(data, "dma_ccerr_handler\n");
  371. if ((edma_read_array(ctlr, EDMA_EMR, 0) == 0) &&
  372. (edma_read_array(ctlr, EDMA_EMR, 1) == 0) &&
  373. (edma_read(ctlr, EDMA_QEMR) == 0) &&
  374. (edma_read(ctlr, EDMA_CCERR) == 0))
  375. return IRQ_NONE;
  376. while (1) {
  377. int j = -1;
  378. if (edma_read_array(ctlr, EDMA_EMR, 0))
  379. j = 0;
  380. else if (edma_read_array(ctlr, EDMA_EMR, 1))
  381. j = 1;
  382. if (j >= 0) {
  383. dev_dbg(data, "EMR%d %08x\n", j,
  384. edma_read_array(ctlr, EDMA_EMR, j));
  385. for (i = 0; i < 32; i++) {
  386. int k = (j << 5) + i;
  387. if (edma_read_array(ctlr, EDMA_EMR, j) &
  388. BIT(i)) {
  389. /* Clear the corresponding EMR bits */
  390. edma_write_array(ctlr, EDMA_EMCR, j,
  391. BIT(i));
  392. /* Clear any SER */
  393. edma_shadow0_write_array(ctlr, SH_SECR,
  394. j, BIT(i));
  395. if (edma_cc[ctlr]->intr_data[k].
  396. callback) {
  397. edma_cc[ctlr]->intr_data[k].
  398. callback(k,
  399. DMA_CC_ERROR,
  400. edma_cc[ctlr]->intr_data
  401. [k].data);
  402. }
  403. }
  404. }
  405. } else if (edma_read(ctlr, EDMA_QEMR)) {
  406. dev_dbg(data, "QEMR %02x\n",
  407. edma_read(ctlr, EDMA_QEMR));
  408. for (i = 0; i < 8; i++) {
  409. if (edma_read(ctlr, EDMA_QEMR) & BIT(i)) {
  410. /* Clear the corresponding IPR bits */
  411. edma_write(ctlr, EDMA_QEMCR, BIT(i));
  412. edma_shadow0_write(ctlr, SH_QSECR,
  413. BIT(i));
  414. /* NOTE: not reported!! */
  415. }
  416. }
  417. } else if (edma_read(ctlr, EDMA_CCERR)) {
  418. dev_dbg(data, "CCERR %08x\n",
  419. edma_read(ctlr, EDMA_CCERR));
  420. /* FIXME: CCERR.BIT(16) ignored! much better
  421. * to just write CCERRCLR with CCERR value...
  422. */
  423. for (i = 0; i < 8; i++) {
  424. if (edma_read(ctlr, EDMA_CCERR) & BIT(i)) {
  425. /* Clear the corresponding IPR bits */
  426. edma_write(ctlr, EDMA_CCERRCLR, BIT(i));
  427. /* NOTE: not reported!! */
  428. }
  429. }
  430. }
  431. if ((edma_read_array(ctlr, EDMA_EMR, 0) == 0) &&
  432. (edma_read_array(ctlr, EDMA_EMR, 1) == 0) &&
  433. (edma_read(ctlr, EDMA_QEMR) == 0) &&
  434. (edma_read(ctlr, EDMA_CCERR) == 0))
  435. break;
  436. cnt++;
  437. if (cnt > 10)
  438. break;
  439. }
  440. edma_write(ctlr, EDMA_EEVAL, 1);
  441. return IRQ_HANDLED;
  442. }
  443. static int reserve_contiguous_slots(int ctlr, unsigned int id,
  444. unsigned int num_slots,
  445. unsigned int start_slot)
  446. {
  447. int i, j;
  448. unsigned int count = num_slots;
  449. int stop_slot = start_slot;
  450. DECLARE_BITMAP(tmp_inuse, EDMA_MAX_PARAMENTRY);
  451. for (i = start_slot; i < edma_cc[ctlr]->num_slots; ++i) {
  452. j = EDMA_CHAN_SLOT(i);
  453. if (!test_and_set_bit(j, edma_cc[ctlr]->edma_inuse)) {
  454. /* Record our current beginning slot */
  455. if (count == num_slots)
  456. stop_slot = i;
  457. count--;
  458. set_bit(j, tmp_inuse);
  459. if (count == 0)
  460. break;
  461. } else {
  462. clear_bit(j, tmp_inuse);
  463. if (id == EDMA_CONT_PARAMS_FIXED_EXACT) {
  464. stop_slot = i;
  465. break;
  466. } else {
  467. count = num_slots;
  468. }
  469. }
  470. }
  471. /*
  472. * We have to clear any bits that we set
  473. * if we run out parameter RAM slots, i.e we do find a set
  474. * of contiguous parameter RAM slots but do not find the exact number
  475. * requested as we may reach the total number of parameter RAM slots
  476. */
  477. if (i == edma_cc[ctlr]->num_slots)
  478. stop_slot = i;
  479. j = start_slot;
  480. for_each_set_bit_from(j, tmp_inuse, stop_slot)
  481. clear_bit(j, edma_cc[ctlr]->edma_inuse);
  482. if (count)
  483. return -EBUSY;
  484. for (j = i - num_slots + 1; j <= i; ++j)
  485. memcpy_toio(edmacc_regs_base[ctlr] + PARM_OFFSET(j),
  486. &dummy_paramset, PARM_SIZE);
  487. return EDMA_CTLR_CHAN(ctlr, i - num_slots + 1);
  488. }
  489. static int prepare_unused_channel_list(struct device *dev, void *data)
  490. {
  491. struct platform_device *pdev = to_platform_device(dev);
  492. int i, ctlr;
  493. for (i = 0; i < pdev->num_resources; i++) {
  494. if ((pdev->resource[i].flags & IORESOURCE_DMA) &&
  495. (int)pdev->resource[i].start >= 0) {
  496. ctlr = EDMA_CTLR(pdev->resource[i].start);
  497. clear_bit(EDMA_CHAN_SLOT(pdev->resource[i].start),
  498. edma_cc[ctlr]->edma_unused);
  499. }
  500. }
  501. return 0;
  502. }
  503. /*-----------------------------------------------------------------------*/
  504. static bool unused_chan_list_done;
  505. /* Resource alloc/free: dma channels, parameter RAM slots */
  506. /**
  507. * edma_alloc_channel - allocate DMA channel and paired parameter RAM
  508. * @channel: specific channel to allocate; negative for "any unmapped channel"
  509. * @callback: optional; to be issued on DMA completion or errors
  510. * @data: passed to callback
  511. * @eventq_no: an EVENTQ_* constant, used to choose which Transfer
  512. * Controller (TC) executes requests using this channel. Use
  513. * EVENTQ_DEFAULT unless you really need a high priority queue.
  514. *
  515. * This allocates a DMA channel and its associated parameter RAM slot.
  516. * The parameter RAM is initialized to hold a dummy transfer.
  517. *
  518. * Normal use is to pass a specific channel number as @channel, to make
  519. * use of hardware events mapped to that channel. When the channel will
  520. * be used only for software triggering or event chaining, channels not
  521. * mapped to hardware events (or mapped to unused events) are preferable.
  522. *
  523. * DMA transfers start from a channel using edma_start(), or by
  524. * chaining. When the transfer described in that channel's parameter RAM
  525. * slot completes, that slot's data may be reloaded through a link.
  526. *
  527. * DMA errors are only reported to the @callback associated with the
  528. * channel driving that transfer, but transfer completion callbacks can
  529. * be sent to another channel under control of the TCC field in
  530. * the option word of the transfer's parameter RAM set. Drivers must not
  531. * use DMA transfer completion callbacks for channels they did not allocate.
  532. * (The same applies to TCC codes used in transfer chaining.)
  533. *
  534. * Returns the number of the channel, else negative errno.
  535. */
  536. int edma_alloc_channel(int channel,
  537. void (*callback)(unsigned channel, u16 ch_status, void *data),
  538. void *data,
  539. enum dma_event_q eventq_no)
  540. {
  541. unsigned i, done = 0, ctlr = 0;
  542. int ret = 0;
  543. if (!unused_chan_list_done) {
  544. /*
  545. * Scan all the platform devices to find out the EDMA channels
  546. * used and clear them in the unused list, making the rest
  547. * available for ARM usage.
  548. */
  549. ret = bus_for_each_dev(&platform_bus_type, NULL, NULL,
  550. prepare_unused_channel_list);
  551. if (ret < 0)
  552. return ret;
  553. unused_chan_list_done = true;
  554. }
  555. if (channel >= 0) {
  556. ctlr = EDMA_CTLR(channel);
  557. channel = EDMA_CHAN_SLOT(channel);
  558. }
  559. if (channel < 0) {
  560. for (i = 0; i < arch_num_cc; i++) {
  561. channel = 0;
  562. for (;;) {
  563. channel = find_next_bit(edma_cc[i]->edma_unused,
  564. edma_cc[i]->num_channels,
  565. channel);
  566. if (channel == edma_cc[i]->num_channels)
  567. break;
  568. if (!test_and_set_bit(channel,
  569. edma_cc[i]->edma_inuse)) {
  570. done = 1;
  571. ctlr = i;
  572. break;
  573. }
  574. channel++;
  575. }
  576. if (done)
  577. break;
  578. }
  579. if (!done)
  580. return -ENOMEM;
  581. } else if (channel >= edma_cc[ctlr]->num_channels) {
  582. return -EINVAL;
  583. } else if (test_and_set_bit(channel, edma_cc[ctlr]->edma_inuse)) {
  584. return -EBUSY;
  585. }
  586. /* ensure access through shadow region 0 */
  587. edma_or_array2(ctlr, EDMA_DRAE, 0, channel >> 5, BIT(channel & 0x1f));
  588. /* ensure no events are pending */
  589. edma_stop(EDMA_CTLR_CHAN(ctlr, channel));
  590. memcpy_toio(edmacc_regs_base[ctlr] + PARM_OFFSET(channel),
  591. &dummy_paramset, PARM_SIZE);
  592. if (callback)
  593. setup_dma_interrupt(EDMA_CTLR_CHAN(ctlr, channel),
  594. callback, data);
  595. map_dmach_queue(ctlr, channel, eventq_no);
  596. return EDMA_CTLR_CHAN(ctlr, channel);
  597. }
  598. EXPORT_SYMBOL(edma_alloc_channel);
  599. /**
  600. * edma_free_channel - deallocate DMA channel
  601. * @channel: dma channel returned from edma_alloc_channel()
  602. *
  603. * This deallocates the DMA channel and associated parameter RAM slot
  604. * allocated by edma_alloc_channel().
  605. *
  606. * Callers are responsible for ensuring the channel is inactive, and
  607. * will not be reactivated by linking, chaining, or software calls to
  608. * edma_start().
  609. */
  610. void edma_free_channel(unsigned channel)
  611. {
  612. unsigned ctlr;
  613. ctlr = EDMA_CTLR(channel);
  614. channel = EDMA_CHAN_SLOT(channel);
  615. if (channel >= edma_cc[ctlr]->num_channels)
  616. return;
  617. setup_dma_interrupt(channel, NULL, NULL);
  618. /* REVISIT should probably take out of shadow region 0 */
  619. memcpy_toio(edmacc_regs_base[ctlr] + PARM_OFFSET(channel),
  620. &dummy_paramset, PARM_SIZE);
  621. clear_bit(channel, edma_cc[ctlr]->edma_inuse);
  622. }
  623. EXPORT_SYMBOL(edma_free_channel);
  624. /**
  625. * edma_alloc_slot - allocate DMA parameter RAM
  626. * @slot: specific slot to allocate; negative for "any unused slot"
  627. *
  628. * This allocates a parameter RAM slot, initializing it to hold a
  629. * dummy transfer. Slots allocated using this routine have not been
  630. * mapped to a hardware DMA channel, and will normally be used by
  631. * linking to them from a slot associated with a DMA channel.
  632. *
  633. * Normal use is to pass EDMA_SLOT_ANY as the @slot, but specific
  634. * slots may be allocated on behalf of DSP firmware.
  635. *
  636. * Returns the number of the slot, else negative errno.
  637. */
  638. int edma_alloc_slot(unsigned ctlr, int slot)
  639. {
  640. if (!edma_cc[ctlr])
  641. return -EINVAL;
  642. if (slot >= 0)
  643. slot = EDMA_CHAN_SLOT(slot);
  644. if (slot < 0) {
  645. slot = edma_cc[ctlr]->num_channels;
  646. for (;;) {
  647. slot = find_next_zero_bit(edma_cc[ctlr]->edma_inuse,
  648. edma_cc[ctlr]->num_slots, slot);
  649. if (slot == edma_cc[ctlr]->num_slots)
  650. return -ENOMEM;
  651. if (!test_and_set_bit(slot, edma_cc[ctlr]->edma_inuse))
  652. break;
  653. }
  654. } else if (slot < edma_cc[ctlr]->num_channels ||
  655. slot >= edma_cc[ctlr]->num_slots) {
  656. return -EINVAL;
  657. } else if (test_and_set_bit(slot, edma_cc[ctlr]->edma_inuse)) {
  658. return -EBUSY;
  659. }
  660. memcpy_toio(edmacc_regs_base[ctlr] + PARM_OFFSET(slot),
  661. &dummy_paramset, PARM_SIZE);
  662. return EDMA_CTLR_CHAN(ctlr, slot);
  663. }
  664. EXPORT_SYMBOL(edma_alloc_slot);
  665. /**
  666. * edma_free_slot - deallocate DMA parameter RAM
  667. * @slot: parameter RAM slot returned from edma_alloc_slot()
  668. *
  669. * This deallocates the parameter RAM slot allocated by edma_alloc_slot().
  670. * Callers are responsible for ensuring the slot is inactive, and will
  671. * not be activated.
  672. */
  673. void edma_free_slot(unsigned slot)
  674. {
  675. unsigned ctlr;
  676. ctlr = EDMA_CTLR(slot);
  677. slot = EDMA_CHAN_SLOT(slot);
  678. if (slot < edma_cc[ctlr]->num_channels ||
  679. slot >= edma_cc[ctlr]->num_slots)
  680. return;
  681. memcpy_toio(edmacc_regs_base[ctlr] + PARM_OFFSET(slot),
  682. &dummy_paramset, PARM_SIZE);
  683. clear_bit(slot, edma_cc[ctlr]->edma_inuse);
  684. }
  685. EXPORT_SYMBOL(edma_free_slot);
  686. /**
  687. * edma_alloc_cont_slots- alloc contiguous parameter RAM slots
  688. * The API will return the starting point of a set of
  689. * contiguous parameter RAM slots that have been requested
  690. *
  691. * @id: can only be EDMA_CONT_PARAMS_ANY or EDMA_CONT_PARAMS_FIXED_EXACT
  692. * or EDMA_CONT_PARAMS_FIXED_NOT_EXACT
  693. * @count: number of contiguous Paramter RAM slots
  694. * @slot - the start value of Parameter RAM slot that should be passed if id
  695. * is EDMA_CONT_PARAMS_FIXED_EXACT or EDMA_CONT_PARAMS_FIXED_NOT_EXACT
  696. *
  697. * If id is EDMA_CONT_PARAMS_ANY then the API starts looking for a set of
  698. * contiguous Parameter RAM slots from parameter RAM 64 in the case of
  699. * DaVinci SOCs and 32 in the case of DA8xx SOCs.
  700. *
  701. * If id is EDMA_CONT_PARAMS_FIXED_EXACT then the API starts looking for a
  702. * set of contiguous parameter RAM slots from the "slot" that is passed as an
  703. * argument to the API.
  704. *
  705. * If id is EDMA_CONT_PARAMS_FIXED_NOT_EXACT then the API initially tries
  706. * starts looking for a set of contiguous parameter RAMs from the "slot"
  707. * that is passed as an argument to the API. On failure the API will try to
  708. * find a set of contiguous Parameter RAM slots from the remaining Parameter
  709. * RAM slots
  710. */
  711. int edma_alloc_cont_slots(unsigned ctlr, unsigned int id, int slot, int count)
  712. {
  713. /*
  714. * The start slot requested should be greater than
  715. * the number of channels and lesser than the total number
  716. * of slots
  717. */
  718. if ((id != EDMA_CONT_PARAMS_ANY) &&
  719. (slot < edma_cc[ctlr]->num_channels ||
  720. slot >= edma_cc[ctlr]->num_slots))
  721. return -EINVAL;
  722. /*
  723. * The number of parameter RAM slots requested cannot be less than 1
  724. * and cannot be more than the number of slots minus the number of
  725. * channels
  726. */
  727. if (count < 1 || count >
  728. (edma_cc[ctlr]->num_slots - edma_cc[ctlr]->num_channels))
  729. return -EINVAL;
  730. switch (id) {
  731. case EDMA_CONT_PARAMS_ANY:
  732. return reserve_contiguous_slots(ctlr, id, count,
  733. edma_cc[ctlr]->num_channels);
  734. case EDMA_CONT_PARAMS_FIXED_EXACT:
  735. case EDMA_CONT_PARAMS_FIXED_NOT_EXACT:
  736. return reserve_contiguous_slots(ctlr, id, count, slot);
  737. default:
  738. return -EINVAL;
  739. }
  740. }
  741. EXPORT_SYMBOL(edma_alloc_cont_slots);
  742. /**
  743. * edma_free_cont_slots - deallocate DMA parameter RAM slots
  744. * @slot: first parameter RAM of a set of parameter RAM slots to be freed
  745. * @count: the number of contiguous parameter RAM slots to be freed
  746. *
  747. * This deallocates the parameter RAM slots allocated by
  748. * edma_alloc_cont_slots.
  749. * Callers/applications need to keep track of sets of contiguous
  750. * parameter RAM slots that have been allocated using the edma_alloc_cont_slots
  751. * API.
  752. * Callers are responsible for ensuring the slots are inactive, and will
  753. * not be activated.
  754. */
  755. int edma_free_cont_slots(unsigned slot, int count)
  756. {
  757. unsigned ctlr, slot_to_free;
  758. int i;
  759. ctlr = EDMA_CTLR(slot);
  760. slot = EDMA_CHAN_SLOT(slot);
  761. if (slot < edma_cc[ctlr]->num_channels ||
  762. slot >= edma_cc[ctlr]->num_slots ||
  763. count < 1)
  764. return -EINVAL;
  765. for (i = slot; i < slot + count; ++i) {
  766. ctlr = EDMA_CTLR(i);
  767. slot_to_free = EDMA_CHAN_SLOT(i);
  768. memcpy_toio(edmacc_regs_base[ctlr] + PARM_OFFSET(slot_to_free),
  769. &dummy_paramset, PARM_SIZE);
  770. clear_bit(slot_to_free, edma_cc[ctlr]->edma_inuse);
  771. }
  772. return 0;
  773. }
  774. EXPORT_SYMBOL(edma_free_cont_slots);
  775. /*-----------------------------------------------------------------------*/
  776. /* Parameter RAM operations (i) -- read/write partial slots */
  777. /**
  778. * edma_set_src - set initial DMA source address in parameter RAM slot
  779. * @slot: parameter RAM slot being configured
  780. * @src_port: physical address of source (memory, controller FIFO, etc)
  781. * @addressMode: INCR, except in very rare cases
  782. * @fifoWidth: ignored unless @addressMode is FIFO, else specifies the
  783. * width to use when addressing the fifo (e.g. W8BIT, W32BIT)
  784. *
  785. * Note that the source address is modified during the DMA transfer
  786. * according to edma_set_src_index().
  787. */
  788. void edma_set_src(unsigned slot, dma_addr_t src_port,
  789. enum address_mode mode, enum fifo_width width)
  790. {
  791. unsigned ctlr;
  792. ctlr = EDMA_CTLR(slot);
  793. slot = EDMA_CHAN_SLOT(slot);
  794. if (slot < edma_cc[ctlr]->num_slots) {
  795. unsigned int i = edma_parm_read(ctlr, PARM_OPT, slot);
  796. if (mode) {
  797. /* set SAM and program FWID */
  798. i = (i & ~(EDMA_FWID)) | (SAM | ((width & 0x7) << 8));
  799. } else {
  800. /* clear SAM */
  801. i &= ~SAM;
  802. }
  803. edma_parm_write(ctlr, PARM_OPT, slot, i);
  804. /* set the source port address
  805. in source register of param structure */
  806. edma_parm_write(ctlr, PARM_SRC, slot, src_port);
  807. }
  808. }
  809. EXPORT_SYMBOL(edma_set_src);
  810. /**
  811. * edma_set_dest - set initial DMA destination address in parameter RAM slot
  812. * @slot: parameter RAM slot being configured
  813. * @dest_port: physical address of destination (memory, controller FIFO, etc)
  814. * @addressMode: INCR, except in very rare cases
  815. * @fifoWidth: ignored unless @addressMode is FIFO, else specifies the
  816. * width to use when addressing the fifo (e.g. W8BIT, W32BIT)
  817. *
  818. * Note that the destination address is modified during the DMA transfer
  819. * according to edma_set_dest_index().
  820. */
  821. void edma_set_dest(unsigned slot, dma_addr_t dest_port,
  822. enum address_mode mode, enum fifo_width width)
  823. {
  824. unsigned ctlr;
  825. ctlr = EDMA_CTLR(slot);
  826. slot = EDMA_CHAN_SLOT(slot);
  827. if (slot < edma_cc[ctlr]->num_slots) {
  828. unsigned int i = edma_parm_read(ctlr, PARM_OPT, slot);
  829. if (mode) {
  830. /* set DAM and program FWID */
  831. i = (i & ~(EDMA_FWID)) | (DAM | ((width & 0x7) << 8));
  832. } else {
  833. /* clear DAM */
  834. i &= ~DAM;
  835. }
  836. edma_parm_write(ctlr, PARM_OPT, slot, i);
  837. /* set the destination port address
  838. in dest register of param structure */
  839. edma_parm_write(ctlr, PARM_DST, slot, dest_port);
  840. }
  841. }
  842. EXPORT_SYMBOL(edma_set_dest);
  843. /**
  844. * edma_get_position - returns the current transfer points
  845. * @slot: parameter RAM slot being examined
  846. * @src: pointer to source port position
  847. * @dst: pointer to destination port position
  848. *
  849. * Returns current source and destination addresses for a particular
  850. * parameter RAM slot. Its channel should not be active when this is called.
  851. */
  852. void edma_get_position(unsigned slot, dma_addr_t *src, dma_addr_t *dst)
  853. {
  854. struct edmacc_param temp;
  855. unsigned ctlr;
  856. ctlr = EDMA_CTLR(slot);
  857. slot = EDMA_CHAN_SLOT(slot);
  858. edma_read_slot(EDMA_CTLR_CHAN(ctlr, slot), &temp);
  859. if (src != NULL)
  860. *src = temp.src;
  861. if (dst != NULL)
  862. *dst = temp.dst;
  863. }
  864. EXPORT_SYMBOL(edma_get_position);
  865. /**
  866. * edma_set_src_index - configure DMA source address indexing
  867. * @slot: parameter RAM slot being configured
  868. * @src_bidx: byte offset between source arrays in a frame
  869. * @src_cidx: byte offset between source frames in a block
  870. *
  871. * Offsets are specified to support either contiguous or discontiguous
  872. * memory transfers, or repeated access to a hardware register, as needed.
  873. * When accessing hardware registers, both offsets are normally zero.
  874. */
  875. void edma_set_src_index(unsigned slot, s16 src_bidx, s16 src_cidx)
  876. {
  877. unsigned ctlr;
  878. ctlr = EDMA_CTLR(slot);
  879. slot = EDMA_CHAN_SLOT(slot);
  880. if (slot < edma_cc[ctlr]->num_slots) {
  881. edma_parm_modify(ctlr, PARM_SRC_DST_BIDX, slot,
  882. 0xffff0000, src_bidx);
  883. edma_parm_modify(ctlr, PARM_SRC_DST_CIDX, slot,
  884. 0xffff0000, src_cidx);
  885. }
  886. }
  887. EXPORT_SYMBOL(edma_set_src_index);
  888. /**
  889. * edma_set_dest_index - configure DMA destination address indexing
  890. * @slot: parameter RAM slot being configured
  891. * @dest_bidx: byte offset between destination arrays in a frame
  892. * @dest_cidx: byte offset between destination frames in a block
  893. *
  894. * Offsets are specified to support either contiguous or discontiguous
  895. * memory transfers, or repeated access to a hardware register, as needed.
  896. * When accessing hardware registers, both offsets are normally zero.
  897. */
  898. void edma_set_dest_index(unsigned slot, s16 dest_bidx, s16 dest_cidx)
  899. {
  900. unsigned ctlr;
  901. ctlr = EDMA_CTLR(slot);
  902. slot = EDMA_CHAN_SLOT(slot);
  903. if (slot < edma_cc[ctlr]->num_slots) {
  904. edma_parm_modify(ctlr, PARM_SRC_DST_BIDX, slot,
  905. 0x0000ffff, dest_bidx << 16);
  906. edma_parm_modify(ctlr, PARM_SRC_DST_CIDX, slot,
  907. 0x0000ffff, dest_cidx << 16);
  908. }
  909. }
  910. EXPORT_SYMBOL(edma_set_dest_index);
  911. /**
  912. * edma_set_transfer_params - configure DMA transfer parameters
  913. * @slot: parameter RAM slot being configured
  914. * @acnt: how many bytes per array (at least one)
  915. * @bcnt: how many arrays per frame (at least one)
  916. * @ccnt: how many frames per block (at least one)
  917. * @bcnt_rld: used only for A-Synchronized transfers; this specifies
  918. * the value to reload into bcnt when it decrements to zero
  919. * @sync_mode: ASYNC or ABSYNC
  920. *
  921. * See the EDMA3 documentation to understand how to configure and link
  922. * transfers using the fields in PaRAM slots. If you are not doing it
  923. * all at once with edma_write_slot(), you will use this routine
  924. * plus two calls each for source and destination, setting the initial
  925. * address and saying how to index that address.
  926. *
  927. * An example of an A-Synchronized transfer is a serial link using a
  928. * single word shift register. In that case, @acnt would be equal to
  929. * that word size; the serial controller issues a DMA synchronization
  930. * event to transfer each word, and memory access by the DMA transfer
  931. * controller will be word-at-a-time.
  932. *
  933. * An example of an AB-Synchronized transfer is a device using a FIFO.
  934. * In that case, @acnt equals the FIFO width and @bcnt equals its depth.
  935. * The controller with the FIFO issues DMA synchronization events when
  936. * the FIFO threshold is reached, and the DMA transfer controller will
  937. * transfer one frame to (or from) the FIFO. It will probably use
  938. * efficient burst modes to access memory.
  939. */
  940. void edma_set_transfer_params(unsigned slot,
  941. u16 acnt, u16 bcnt, u16 ccnt,
  942. u16 bcnt_rld, enum sync_dimension sync_mode)
  943. {
  944. unsigned ctlr;
  945. ctlr = EDMA_CTLR(slot);
  946. slot = EDMA_CHAN_SLOT(slot);
  947. if (slot < edma_cc[ctlr]->num_slots) {
  948. edma_parm_modify(ctlr, PARM_LINK_BCNTRLD, slot,
  949. 0x0000ffff, bcnt_rld << 16);
  950. if (sync_mode == ASYNC)
  951. edma_parm_and(ctlr, PARM_OPT, slot, ~SYNCDIM);
  952. else
  953. edma_parm_or(ctlr, PARM_OPT, slot, SYNCDIM);
  954. /* Set the acount, bcount, ccount registers */
  955. edma_parm_write(ctlr, PARM_A_B_CNT, slot, (bcnt << 16) | acnt);
  956. edma_parm_write(ctlr, PARM_CCNT, slot, ccnt);
  957. }
  958. }
  959. EXPORT_SYMBOL(edma_set_transfer_params);
  960. /**
  961. * edma_link - link one parameter RAM slot to another
  962. * @from: parameter RAM slot originating the link
  963. * @to: parameter RAM slot which is the link target
  964. *
  965. * The originating slot should not be part of any active DMA transfer.
  966. */
  967. void edma_link(unsigned from, unsigned to)
  968. {
  969. unsigned ctlr_from, ctlr_to;
  970. ctlr_from = EDMA_CTLR(from);
  971. from = EDMA_CHAN_SLOT(from);
  972. ctlr_to = EDMA_CTLR(to);
  973. to = EDMA_CHAN_SLOT(to);
  974. if (from >= edma_cc[ctlr_from]->num_slots)
  975. return;
  976. if (to >= edma_cc[ctlr_to]->num_slots)
  977. return;
  978. edma_parm_modify(ctlr_from, PARM_LINK_BCNTRLD, from, 0xffff0000,
  979. PARM_OFFSET(to));
  980. }
  981. EXPORT_SYMBOL(edma_link);
  982. /**
  983. * edma_unlink - cut link from one parameter RAM slot
  984. * @from: parameter RAM slot originating the link
  985. *
  986. * The originating slot should not be part of any active DMA transfer.
  987. * Its link is set to 0xffff.
  988. */
  989. void edma_unlink(unsigned from)
  990. {
  991. unsigned ctlr;
  992. ctlr = EDMA_CTLR(from);
  993. from = EDMA_CHAN_SLOT(from);
  994. if (from >= edma_cc[ctlr]->num_slots)
  995. return;
  996. edma_parm_or(ctlr, PARM_LINK_BCNTRLD, from, 0xffff);
  997. }
  998. EXPORT_SYMBOL(edma_unlink);
  999. /*-----------------------------------------------------------------------*/
  1000. /* Parameter RAM operations (ii) -- read/write whole parameter sets */
  1001. /**
  1002. * edma_write_slot - write parameter RAM data for slot
  1003. * @slot: number of parameter RAM slot being modified
  1004. * @param: data to be written into parameter RAM slot
  1005. *
  1006. * Use this to assign all parameters of a transfer at once. This
  1007. * allows more efficient setup of transfers than issuing multiple
  1008. * calls to set up those parameters in small pieces, and provides
  1009. * complete control over all transfer options.
  1010. */
  1011. void edma_write_slot(unsigned slot, const struct edmacc_param *param)
  1012. {
  1013. unsigned ctlr;
  1014. ctlr = EDMA_CTLR(slot);
  1015. slot = EDMA_CHAN_SLOT(slot);
  1016. if (slot >= edma_cc[ctlr]->num_slots)
  1017. return;
  1018. memcpy_toio(edmacc_regs_base[ctlr] + PARM_OFFSET(slot), param,
  1019. PARM_SIZE);
  1020. }
  1021. EXPORT_SYMBOL(edma_write_slot);
  1022. /**
  1023. * edma_read_slot - read parameter RAM data from slot
  1024. * @slot: number of parameter RAM slot being copied
  1025. * @param: where to store copy of parameter RAM data
  1026. *
  1027. * Use this to read data from a parameter RAM slot, perhaps to
  1028. * save them as a template for later reuse.
  1029. */
  1030. void edma_read_slot(unsigned slot, struct edmacc_param *param)
  1031. {
  1032. unsigned ctlr;
  1033. ctlr = EDMA_CTLR(slot);
  1034. slot = EDMA_CHAN_SLOT(slot);
  1035. if (slot >= edma_cc[ctlr]->num_slots)
  1036. return;
  1037. memcpy_fromio(param, edmacc_regs_base[ctlr] + PARM_OFFSET(slot),
  1038. PARM_SIZE);
  1039. }
  1040. EXPORT_SYMBOL(edma_read_slot);
  1041. /*-----------------------------------------------------------------------*/
  1042. /* Various EDMA channel control operations */
  1043. /**
  1044. * edma_pause - pause dma on a channel
  1045. * @channel: on which edma_start() has been called
  1046. *
  1047. * This temporarily disables EDMA hardware events on the specified channel,
  1048. * preventing them from triggering new transfers on its behalf
  1049. */
  1050. void edma_pause(unsigned channel)
  1051. {
  1052. unsigned ctlr;
  1053. ctlr = EDMA_CTLR(channel);
  1054. channel = EDMA_CHAN_SLOT(channel);
  1055. if (channel < edma_cc[ctlr]->num_channels) {
  1056. unsigned int mask = BIT(channel & 0x1f);
  1057. edma_shadow0_write_array(ctlr, SH_EECR, channel >> 5, mask);
  1058. }
  1059. }
  1060. EXPORT_SYMBOL(edma_pause);
  1061. /**
  1062. * edma_resume - resumes dma on a paused channel
  1063. * @channel: on which edma_pause() has been called
  1064. *
  1065. * This re-enables EDMA hardware events on the specified channel.
  1066. */
  1067. void edma_resume(unsigned channel)
  1068. {
  1069. unsigned ctlr;
  1070. ctlr = EDMA_CTLR(channel);
  1071. channel = EDMA_CHAN_SLOT(channel);
  1072. if (channel < edma_cc[ctlr]->num_channels) {
  1073. unsigned int mask = BIT(channel & 0x1f);
  1074. edma_shadow0_write_array(ctlr, SH_EESR, channel >> 5, mask);
  1075. }
  1076. }
  1077. EXPORT_SYMBOL(edma_resume);
  1078. /**
  1079. * edma_start - start dma on a channel
  1080. * @channel: channel being activated
  1081. *
  1082. * Channels with event associations will be triggered by their hardware
  1083. * events, and channels without such associations will be triggered by
  1084. * software. (At this writing there is no interface for using software
  1085. * triggers except with channels that don't support hardware triggers.)
  1086. *
  1087. * Returns zero on success, else negative errno.
  1088. */
  1089. int edma_start(unsigned channel)
  1090. {
  1091. unsigned ctlr;
  1092. ctlr = EDMA_CTLR(channel);
  1093. channel = EDMA_CHAN_SLOT(channel);
  1094. if (channel < edma_cc[ctlr]->num_channels) {
  1095. int j = channel >> 5;
  1096. unsigned int mask = BIT(channel & 0x1f);
  1097. /* EDMA channels without event association */
  1098. if (test_bit(channel, edma_cc[ctlr]->edma_unused)) {
  1099. pr_debug("EDMA: ESR%d %08x\n", j,
  1100. edma_shadow0_read_array(ctlr, SH_ESR, j));
  1101. edma_shadow0_write_array(ctlr, SH_ESR, j, mask);
  1102. return 0;
  1103. }
  1104. /* EDMA channel with event association */
  1105. pr_debug("EDMA: ER%d %08x\n", j,
  1106. edma_shadow0_read_array(ctlr, SH_ER, j));
  1107. /* Clear any pending event or error */
  1108. edma_write_array(ctlr, EDMA_ECR, j, mask);
  1109. edma_write_array(ctlr, EDMA_EMCR, j, mask);
  1110. /* Clear any SER */
  1111. edma_shadow0_write_array(ctlr, SH_SECR, j, mask);
  1112. edma_shadow0_write_array(ctlr, SH_EESR, j, mask);
  1113. pr_debug("EDMA: EER%d %08x\n", j,
  1114. edma_shadow0_read_array(ctlr, SH_EER, j));
  1115. return 0;
  1116. }
  1117. return -EINVAL;
  1118. }
  1119. EXPORT_SYMBOL(edma_start);
  1120. /**
  1121. * edma_stop - stops dma on the channel passed
  1122. * @channel: channel being deactivated
  1123. *
  1124. * When @lch is a channel, any active transfer is paused and
  1125. * all pending hardware events are cleared. The current transfer
  1126. * may not be resumed, and the channel's Parameter RAM should be
  1127. * reinitialized before being reused.
  1128. */
  1129. void edma_stop(unsigned channel)
  1130. {
  1131. unsigned ctlr;
  1132. ctlr = EDMA_CTLR(channel);
  1133. channel = EDMA_CHAN_SLOT(channel);
  1134. if (channel < edma_cc[ctlr]->num_channels) {
  1135. int j = channel >> 5;
  1136. unsigned int mask = BIT(channel & 0x1f);
  1137. edma_shadow0_write_array(ctlr, SH_EECR, j, mask);
  1138. edma_shadow0_write_array(ctlr, SH_ECR, j, mask);
  1139. edma_shadow0_write_array(ctlr, SH_SECR, j, mask);
  1140. edma_write_array(ctlr, EDMA_EMCR, j, mask);
  1141. pr_debug("EDMA: EER%d %08x\n", j,
  1142. edma_shadow0_read_array(ctlr, SH_EER, j));
  1143. /* REVISIT: consider guarding against inappropriate event
  1144. * chaining by overwriting with dummy_paramset.
  1145. */
  1146. }
  1147. }
  1148. EXPORT_SYMBOL(edma_stop);
  1149. /******************************************************************************
  1150. *
  1151. * It cleans ParamEntry qand bring back EDMA to initial state if media has
  1152. * been removed before EDMA has finished.It is usedful for removable media.
  1153. * Arguments:
  1154. * ch_no - channel no
  1155. *
  1156. * Return: zero on success, or corresponding error no on failure
  1157. *
  1158. * FIXME this should not be needed ... edma_stop() should suffice.
  1159. *
  1160. *****************************************************************************/
  1161. void edma_clean_channel(unsigned channel)
  1162. {
  1163. unsigned ctlr;
  1164. ctlr = EDMA_CTLR(channel);
  1165. channel = EDMA_CHAN_SLOT(channel);
  1166. if (channel < edma_cc[ctlr]->num_channels) {
  1167. int j = (channel >> 5);
  1168. unsigned int mask = BIT(channel & 0x1f);
  1169. pr_debug("EDMA: EMR%d %08x\n", j,
  1170. edma_read_array(ctlr, EDMA_EMR, j));
  1171. edma_shadow0_write_array(ctlr, SH_ECR, j, mask);
  1172. /* Clear the corresponding EMR bits */
  1173. edma_write_array(ctlr, EDMA_EMCR, j, mask);
  1174. /* Clear any SER */
  1175. edma_shadow0_write_array(ctlr, SH_SECR, j, mask);
  1176. edma_write(ctlr, EDMA_CCERRCLR, BIT(16) | BIT(1) | BIT(0));
  1177. }
  1178. }
  1179. EXPORT_SYMBOL(edma_clean_channel);
  1180. /*
  1181. * edma_clear_event - clear an outstanding event on the DMA channel
  1182. * Arguments:
  1183. * channel - channel number
  1184. */
  1185. void edma_clear_event(unsigned channel)
  1186. {
  1187. unsigned ctlr;
  1188. ctlr = EDMA_CTLR(channel);
  1189. channel = EDMA_CHAN_SLOT(channel);
  1190. if (channel >= edma_cc[ctlr]->num_channels)
  1191. return;
  1192. if (channel < 32)
  1193. edma_write(ctlr, EDMA_ECR, BIT(channel));
  1194. else
  1195. edma_write(ctlr, EDMA_ECRH, BIT(channel - 32));
  1196. }
  1197. EXPORT_SYMBOL(edma_clear_event);
  1198. /*-----------------------------------------------------------------------*/
  1199. static int __init edma_probe(struct platform_device *pdev)
  1200. {
  1201. struct edma_soc_info **info = pdev->dev.platform_data;
  1202. const s8 (*queue_priority_mapping)[2];
  1203. const s8 (*queue_tc_mapping)[2];
  1204. int i, j, off, ln, found = 0;
  1205. int status = -1;
  1206. const s16 (*rsv_chans)[2];
  1207. const s16 (*rsv_slots)[2];
  1208. int irq[EDMA_MAX_CC] = {0, 0};
  1209. int err_irq[EDMA_MAX_CC] = {0, 0};
  1210. struct resource *r[EDMA_MAX_CC] = {NULL};
  1211. char res_name[10];
  1212. char irq_name[10];
  1213. if (!info)
  1214. return -ENODEV;
  1215. for (j = 0; j < EDMA_MAX_CC; j++) {
  1216. sprintf(res_name, "edma_cc%d", j);
  1217. r[j] = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1218. res_name);
  1219. if (!r[j] || !info[j]) {
  1220. if (found)
  1221. break;
  1222. else
  1223. return -ENODEV;
  1224. } else {
  1225. found = 1;
  1226. }
  1227. edmacc_regs_base[j] = devm_ioremap_resource(&pdev->dev, r[j]);
  1228. if (IS_ERR(edmacc_regs_base[j]))
  1229. return PTR_ERR(edmacc_regs_base[j]);
  1230. edma_cc[j] = devm_kzalloc(&pdev->dev, sizeof(struct edma),
  1231. GFP_KERNEL);
  1232. if (!edma_cc[j])
  1233. return -ENOMEM;
  1234. edma_cc[j]->num_channels = min_t(unsigned, info[j]->n_channel,
  1235. EDMA_MAX_DMACH);
  1236. edma_cc[j]->num_slots = min_t(unsigned, info[j]->n_slot,
  1237. EDMA_MAX_PARAMENTRY);
  1238. edma_cc[j]->num_cc = min_t(unsigned, info[j]->n_cc,
  1239. EDMA_MAX_CC);
  1240. edma_cc[j]->default_queue = info[j]->default_queue;
  1241. dev_dbg(&pdev->dev, "DMA REG BASE ADDR=%p\n",
  1242. edmacc_regs_base[j]);
  1243. for (i = 0; i < edma_cc[j]->num_slots; i++)
  1244. memcpy_toio(edmacc_regs_base[j] + PARM_OFFSET(i),
  1245. &dummy_paramset, PARM_SIZE);
  1246. /* Mark all channels as unused */
  1247. memset(edma_cc[j]->edma_unused, 0xff,
  1248. sizeof(edma_cc[j]->edma_unused));
  1249. if (info[j]->rsv) {
  1250. /* Clear the reserved channels in unused list */
  1251. rsv_chans = info[j]->rsv->rsv_chans;
  1252. if (rsv_chans) {
  1253. for (i = 0; rsv_chans[i][0] != -1; i++) {
  1254. off = rsv_chans[i][0];
  1255. ln = rsv_chans[i][1];
  1256. clear_bits(off, ln,
  1257. edma_cc[j]->edma_unused);
  1258. }
  1259. }
  1260. /* Set the reserved slots in inuse list */
  1261. rsv_slots = info[j]->rsv->rsv_slots;
  1262. if (rsv_slots) {
  1263. for (i = 0; rsv_slots[i][0] != -1; i++) {
  1264. off = rsv_slots[i][0];
  1265. ln = rsv_slots[i][1];
  1266. set_bits(off, ln,
  1267. edma_cc[j]->edma_inuse);
  1268. }
  1269. }
  1270. }
  1271. sprintf(irq_name, "edma%d", j);
  1272. irq[j] = platform_get_irq_byname(pdev, irq_name);
  1273. edma_cc[j]->irq_res_start = irq[j];
  1274. status = devm_request_irq(&pdev->dev, irq[j],
  1275. dma_irq_handler, 0, "edma",
  1276. &pdev->dev);
  1277. if (status < 0) {
  1278. dev_dbg(&pdev->dev,
  1279. "devm_request_irq %d failed --> %d\n",
  1280. irq[j], status);
  1281. return status;
  1282. }
  1283. sprintf(irq_name, "edma%d_err", j);
  1284. err_irq[j] = platform_get_irq_byname(pdev, irq_name);
  1285. edma_cc[j]->irq_res_end = err_irq[j];
  1286. status = devm_request_irq(&pdev->dev, err_irq[j],
  1287. dma_ccerr_handler, 0,
  1288. "edma_error", &pdev->dev);
  1289. if (status < 0) {
  1290. dev_dbg(&pdev->dev,
  1291. "devm_request_irq %d failed --> %d\n",
  1292. err_irq[j], status);
  1293. return status;
  1294. }
  1295. for (i = 0; i < edma_cc[j]->num_channels; i++)
  1296. map_dmach_queue(j, i, info[j]->default_queue);
  1297. queue_tc_mapping = info[j]->queue_tc_mapping;
  1298. queue_priority_mapping = info[j]->queue_priority_mapping;
  1299. /* Event queue to TC mapping */
  1300. for (i = 0; queue_tc_mapping[i][0] != -1; i++)
  1301. map_queue_tc(j, queue_tc_mapping[i][0],
  1302. queue_tc_mapping[i][1]);
  1303. /* Event queue priority mapping */
  1304. for (i = 0; queue_priority_mapping[i][0] != -1; i++)
  1305. assign_priority_to_queue(j,
  1306. queue_priority_mapping[i][0],
  1307. queue_priority_mapping[i][1]);
  1308. /* Map the channel to param entry if channel mapping logic
  1309. * exist
  1310. */
  1311. if (edma_read(j, EDMA_CCCFG) & CHMAP_EXIST)
  1312. map_dmach_param(j);
  1313. for (i = 0; i < info[j]->n_region; i++) {
  1314. edma_write_array2(j, EDMA_DRAE, i, 0, 0x0);
  1315. edma_write_array2(j, EDMA_DRAE, i, 1, 0x0);
  1316. edma_write_array(j, EDMA_QRAE, i, 0x0);
  1317. }
  1318. arch_num_cc++;
  1319. }
  1320. return 0;
  1321. }
  1322. static struct platform_driver edma_driver = {
  1323. .driver.name = "edma",
  1324. };
  1325. static int __init edma_init(void)
  1326. {
  1327. return platform_driver_probe(&edma_driver, edma_probe);
  1328. }
  1329. arch_initcall(edma_init);