dma.c 43 KB

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