dma.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  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/sched.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/spinlock.h>
  27. #include <linux/compiler.h>
  28. #include <linux/io.h>
  29. #include <mach/cputype.h>
  30. #include <mach/memory.h>
  31. #include <mach/hardware.h>
  32. #include <mach/irqs.h>
  33. #include <mach/edma.h>
  34. #include <mach/mux.h>
  35. /* Offsets matching "struct edmacc_param" */
  36. #define PARM_OPT 0x00
  37. #define PARM_SRC 0x04
  38. #define PARM_A_B_CNT 0x08
  39. #define PARM_DST 0x0c
  40. #define PARM_SRC_DST_BIDX 0x10
  41. #define PARM_LINK_BCNTRLD 0x14
  42. #define PARM_SRC_DST_CIDX 0x18
  43. #define PARM_CCNT 0x1c
  44. #define PARM_SIZE 0x20
  45. /* Offsets for EDMA CC global channel registers and their shadows */
  46. #define SH_ER 0x00 /* 64 bits */
  47. #define SH_ECR 0x08 /* 64 bits */
  48. #define SH_ESR 0x10 /* 64 bits */
  49. #define SH_CER 0x18 /* 64 bits */
  50. #define SH_EER 0x20 /* 64 bits */
  51. #define SH_EECR 0x28 /* 64 bits */
  52. #define SH_EESR 0x30 /* 64 bits */
  53. #define SH_SER 0x38 /* 64 bits */
  54. #define SH_SECR 0x40 /* 64 bits */
  55. #define SH_IER 0x50 /* 64 bits */
  56. #define SH_IECR 0x58 /* 64 bits */
  57. #define SH_IESR 0x60 /* 64 bits */
  58. #define SH_IPR 0x68 /* 64 bits */
  59. #define SH_ICR 0x70 /* 64 bits */
  60. #define SH_IEVAL 0x78
  61. #define SH_QER 0x80
  62. #define SH_QEER 0x84
  63. #define SH_QEECR 0x88
  64. #define SH_QEESR 0x8c
  65. #define SH_QSER 0x90
  66. #define SH_QSECR 0x94
  67. #define SH_SIZE 0x200
  68. /* Offsets for EDMA CC global registers */
  69. #define EDMA_REV 0x0000
  70. #define EDMA_CCCFG 0x0004
  71. #define EDMA_QCHMAP 0x0200 /* 8 registers */
  72. #define EDMA_DMAQNUM 0x0240 /* 8 registers (4 on OMAP-L1xx) */
  73. #define EDMA_QDMAQNUM 0x0260
  74. #define EDMA_QUETCMAP 0x0280
  75. #define EDMA_QUEPRI 0x0284
  76. #define EDMA_EMR 0x0300 /* 64 bits */
  77. #define EDMA_EMCR 0x0308 /* 64 bits */
  78. #define EDMA_QEMR 0x0310
  79. #define EDMA_QEMCR 0x0314
  80. #define EDMA_CCERR 0x0318
  81. #define EDMA_CCERRCLR 0x031c
  82. #define EDMA_EEVAL 0x0320
  83. #define EDMA_DRAE 0x0340 /* 4 x 64 bits*/
  84. #define EDMA_QRAE 0x0380 /* 4 registers */
  85. #define EDMA_QUEEVTENTRY 0x0400 /* 2 x 16 registers */
  86. #define EDMA_QSTAT 0x0600 /* 2 registers */
  87. #define EDMA_QWMTHRA 0x0620
  88. #define EDMA_QWMTHRB 0x0624
  89. #define EDMA_CCSTAT 0x0640
  90. #define EDMA_M 0x1000 /* global channel registers */
  91. #define EDMA_ECR 0x1008
  92. #define EDMA_ECRH 0x100C
  93. #define EDMA_SHADOW0 0x2000 /* 4 regions shadowing global channels */
  94. #define EDMA_PARM 0x4000 /* 128 param entries */
  95. #define DAVINCI_DMA_3PCC_BASE 0x01C00000
  96. #define PARM_OFFSET(param_no) (EDMA_PARM + ((param_no) << 5))
  97. #define EDMA_MAX_DMACH 64
  98. #define EDMA_MAX_PARAMENTRY 512
  99. #define EDMA_MAX_EVQUE 2 /* FIXME too small */
  100. /*****************************************************************************/
  101. static void __iomem *edmacc_regs_base;
  102. static inline unsigned int edma_read(int offset)
  103. {
  104. return (unsigned int)__raw_readl(edmacc_regs_base + offset);
  105. }
  106. static inline void edma_write(int offset, int val)
  107. {
  108. __raw_writel(val, edmacc_regs_base + offset);
  109. }
  110. static inline void edma_modify(int offset, unsigned and, unsigned or)
  111. {
  112. unsigned val = edma_read(offset);
  113. val &= and;
  114. val |= or;
  115. edma_write(offset, val);
  116. }
  117. static inline void edma_and(int offset, unsigned and)
  118. {
  119. unsigned val = edma_read(offset);
  120. val &= and;
  121. edma_write(offset, val);
  122. }
  123. static inline void edma_or(int offset, unsigned or)
  124. {
  125. unsigned val = edma_read(offset);
  126. val |= or;
  127. edma_write(offset, val);
  128. }
  129. static inline unsigned int edma_read_array(int offset, int i)
  130. {
  131. return edma_read(offset + (i << 2));
  132. }
  133. static inline void edma_write_array(int offset, int i, unsigned val)
  134. {
  135. edma_write(offset + (i << 2), val);
  136. }
  137. static inline void edma_modify_array(int offset, int i,
  138. unsigned and, unsigned or)
  139. {
  140. edma_modify(offset + (i << 2), and, or);
  141. }
  142. static inline void edma_or_array(int offset, int i, unsigned or)
  143. {
  144. edma_or(offset + (i << 2), or);
  145. }
  146. static inline void edma_or_array2(int offset, int i, int j, unsigned or)
  147. {
  148. edma_or(offset + ((i*2 + j) << 2), or);
  149. }
  150. static inline void edma_write_array2(int offset, int i, int j, unsigned val)
  151. {
  152. edma_write(offset + ((i*2 + j) << 2), val);
  153. }
  154. static inline unsigned int edma_shadow0_read(int offset)
  155. {
  156. return edma_read(EDMA_SHADOW0 + offset);
  157. }
  158. static inline unsigned int edma_shadow0_read_array(int offset, int i)
  159. {
  160. return edma_read(EDMA_SHADOW0 + offset + (i << 2));
  161. }
  162. static inline void edma_shadow0_write(int offset, unsigned val)
  163. {
  164. edma_write(EDMA_SHADOW0 + offset, val);
  165. }
  166. static inline void edma_shadow0_write_array(int offset, int i, unsigned val)
  167. {
  168. edma_write(EDMA_SHADOW0 + offset + (i << 2), val);
  169. }
  170. static inline unsigned int edma_parm_read(int offset, int param_no)
  171. {
  172. return edma_read(EDMA_PARM + offset + (param_no << 5));
  173. }
  174. static inline void edma_parm_write(int offset, int param_no, unsigned val)
  175. {
  176. edma_write(EDMA_PARM + offset + (param_no << 5), val);
  177. }
  178. static inline void edma_parm_modify(int offset, int param_no,
  179. unsigned and, unsigned or)
  180. {
  181. edma_modify(EDMA_PARM + offset + (param_no << 5), and, or);
  182. }
  183. static inline void edma_parm_and(int offset, int param_no, unsigned and)
  184. {
  185. edma_and(EDMA_PARM + offset + (param_no << 5), and);
  186. }
  187. static inline void edma_parm_or(int offset, int param_no, unsigned or)
  188. {
  189. edma_or(EDMA_PARM + offset + (param_no << 5), or);
  190. }
  191. /*****************************************************************************/
  192. /* actual number of DMA channels and slots on this silicon */
  193. static unsigned num_channels;
  194. static unsigned num_slots;
  195. static struct dma_interrupt_data {
  196. void (*callback)(unsigned channel, unsigned short ch_status,
  197. void *data);
  198. void *data;
  199. } intr_data[EDMA_MAX_DMACH];
  200. /* The edma_inuse bit for each PaRAM slot is clear unless the
  201. * channel is in use ... by ARM or DSP, for QDMA, or whatever.
  202. */
  203. static DECLARE_BITMAP(edma_inuse, EDMA_MAX_PARAMENTRY);
  204. /* The edma_noevent bit for each channel is clear unless
  205. * it doesn't trigger DMA events on this platform. It uses a
  206. * bit of SOC-specific initialization code.
  207. */
  208. static DECLARE_BITMAP(edma_noevent, EDMA_MAX_DMACH);
  209. /* dummy param set used to (re)initialize parameter RAM slots */
  210. static const struct edmacc_param dummy_paramset = {
  211. .link_bcntrld = 0xffff,
  212. .ccnt = 1,
  213. };
  214. static const int __initconst
  215. queue_tc_mapping[EDMA_MAX_EVQUE + 1][2] = {
  216. /* {event queue no, TC no} */
  217. {0, 0},
  218. {1, 1},
  219. {-1, -1}
  220. };
  221. static const int __initconst
  222. queue_priority_mapping[EDMA_MAX_EVQUE + 1][2] = {
  223. /* {event queue no, Priority} */
  224. {0, 3},
  225. {1, 7},
  226. {-1, -1}
  227. };
  228. /*****************************************************************************/
  229. static void map_dmach_queue(unsigned ch_no, enum dma_event_q queue_no)
  230. {
  231. int bit = (ch_no & 0x7) * 4;
  232. /* default to low priority queue */
  233. if (queue_no == EVENTQ_DEFAULT)
  234. queue_no = EVENTQ_1;
  235. queue_no &= 7;
  236. edma_modify_array(EDMA_DMAQNUM, (ch_no >> 3),
  237. ~(0x7 << bit), queue_no << bit);
  238. }
  239. static void __init map_queue_tc(int queue_no, int tc_no)
  240. {
  241. int bit = queue_no * 4;
  242. edma_modify(EDMA_QUETCMAP, ~(0x7 << bit), ((tc_no & 0x7) << bit));
  243. }
  244. static void __init assign_priority_to_queue(int queue_no, int priority)
  245. {
  246. int bit = queue_no * 4;
  247. edma_modify(EDMA_QUEPRI, ~(0x7 << bit), ((priority & 0x7) << bit));
  248. }
  249. static inline void
  250. setup_dma_interrupt(unsigned lch,
  251. void (*callback)(unsigned channel, u16 ch_status, void *data),
  252. void *data)
  253. {
  254. if (!callback) {
  255. edma_shadow0_write_array(SH_IECR, lch >> 5,
  256. (1 << (lch & 0x1f)));
  257. }
  258. intr_data[lch].callback = callback;
  259. intr_data[lch].data = data;
  260. if (callback) {
  261. edma_shadow0_write_array(SH_ICR, lch >> 5,
  262. (1 << (lch & 0x1f)));
  263. edma_shadow0_write_array(SH_IESR, lch >> 5,
  264. (1 << (lch & 0x1f)));
  265. }
  266. }
  267. /******************************************************************************
  268. *
  269. * DMA interrupt handler
  270. *
  271. *****************************************************************************/
  272. static irqreturn_t dma_irq_handler(int irq, void *data)
  273. {
  274. int i;
  275. unsigned int cnt = 0;
  276. dev_dbg(data, "dma_irq_handler\n");
  277. if ((edma_shadow0_read_array(SH_IPR, 0) == 0)
  278. && (edma_shadow0_read_array(SH_IPR, 1) == 0))
  279. return IRQ_NONE;
  280. while (1) {
  281. int j;
  282. if (edma_shadow0_read_array(SH_IPR, 0))
  283. j = 0;
  284. else if (edma_shadow0_read_array(SH_IPR, 1))
  285. j = 1;
  286. else
  287. break;
  288. dev_dbg(data, "IPR%d %08x\n", j,
  289. edma_shadow0_read_array(SH_IPR, j));
  290. for (i = 0; i < 32; i++) {
  291. int k = (j << 5) + i;
  292. if (edma_shadow0_read_array(SH_IPR, j) & (1 << i)) {
  293. /* Clear the corresponding IPR bits */
  294. edma_shadow0_write_array(SH_ICR, j, (1 << i));
  295. if (intr_data[k].callback) {
  296. intr_data[k].callback(k, DMA_COMPLETE,
  297. intr_data[k].data);
  298. }
  299. }
  300. }
  301. cnt++;
  302. if (cnt > 10)
  303. break;
  304. }
  305. edma_shadow0_write(SH_IEVAL, 1);
  306. return IRQ_HANDLED;
  307. }
  308. /******************************************************************************
  309. *
  310. * DMA error interrupt handler
  311. *
  312. *****************************************************************************/
  313. static irqreturn_t dma_ccerr_handler(int irq, void *data)
  314. {
  315. int i;
  316. unsigned int cnt = 0;
  317. dev_dbg(data, "dma_ccerr_handler\n");
  318. if ((edma_read_array(EDMA_EMR, 0) == 0) &&
  319. (edma_read_array(EDMA_EMR, 1) == 0) &&
  320. (edma_read(EDMA_QEMR) == 0) && (edma_read(EDMA_CCERR) == 0))
  321. return IRQ_NONE;
  322. while (1) {
  323. int j = -1;
  324. if (edma_read_array(EDMA_EMR, 0))
  325. j = 0;
  326. else if (edma_read_array(EDMA_EMR, 1))
  327. j = 1;
  328. if (j >= 0) {
  329. dev_dbg(data, "EMR%d %08x\n", j,
  330. edma_read_array(EDMA_EMR, j));
  331. for (i = 0; i < 32; i++) {
  332. int k = (j << 5) + i;
  333. if (edma_read_array(EDMA_EMR, j) & (1 << i)) {
  334. /* Clear the corresponding EMR bits */
  335. edma_write_array(EDMA_EMCR, j, 1 << i);
  336. /* Clear any SER */
  337. edma_shadow0_write_array(SH_SECR, j,
  338. (1 << i));
  339. if (intr_data[k].callback) {
  340. intr_data[k].callback(k,
  341. DMA_CC_ERROR,
  342. intr_data
  343. [k].data);
  344. }
  345. }
  346. }
  347. } else if (edma_read(EDMA_QEMR)) {
  348. dev_dbg(data, "QEMR %02x\n",
  349. edma_read(EDMA_QEMR));
  350. for (i = 0; i < 8; i++) {
  351. if (edma_read(EDMA_QEMR) & (1 << i)) {
  352. /* Clear the corresponding IPR bits */
  353. edma_write(EDMA_QEMCR, 1 << i);
  354. edma_shadow0_write(SH_QSECR, (1 << i));
  355. /* NOTE: not reported!! */
  356. }
  357. }
  358. } else if (edma_read(EDMA_CCERR)) {
  359. dev_dbg(data, "CCERR %08x\n",
  360. edma_read(EDMA_CCERR));
  361. /* FIXME: CCERR.BIT(16) ignored! much better
  362. * to just write CCERRCLR with CCERR value...
  363. */
  364. for (i = 0; i < 8; i++) {
  365. if (edma_read(EDMA_CCERR) & (1 << i)) {
  366. /* Clear the corresponding IPR bits */
  367. edma_write(EDMA_CCERRCLR, 1 << i);
  368. /* NOTE: not reported!! */
  369. }
  370. }
  371. }
  372. if ((edma_read_array(EDMA_EMR, 0) == 0)
  373. && (edma_read_array(EDMA_EMR, 1) == 0)
  374. && (edma_read(EDMA_QEMR) == 0)
  375. && (edma_read(EDMA_CCERR) == 0)) {
  376. break;
  377. }
  378. cnt++;
  379. if (cnt > 10)
  380. break;
  381. }
  382. edma_write(EDMA_EEVAL, 1);
  383. return IRQ_HANDLED;
  384. }
  385. /******************************************************************************
  386. *
  387. * Transfer controller error interrupt handlers
  388. *
  389. *****************************************************************************/
  390. #define tc_errs_handled false /* disabled as long as they're NOPs */
  391. static irqreturn_t dma_tc0err_handler(int irq, void *data)
  392. {
  393. dev_dbg(data, "dma_tc0err_handler\n");
  394. return IRQ_HANDLED;
  395. }
  396. static irqreturn_t dma_tc1err_handler(int irq, void *data)
  397. {
  398. dev_dbg(data, "dma_tc1err_handler\n");
  399. return IRQ_HANDLED;
  400. }
  401. /*-----------------------------------------------------------------------*/
  402. /* Resource alloc/free: dma channels, parameter RAM slots */
  403. /**
  404. * edma_alloc_channel - allocate DMA channel and paired parameter RAM
  405. * @channel: specific channel to allocate; negative for "any unmapped channel"
  406. * @callback: optional; to be issued on DMA completion or errors
  407. * @data: passed to callback
  408. * @eventq_no: an EVENTQ_* constant, used to choose which Transfer
  409. * Controller (TC) executes requests using this channel. Use
  410. * EVENTQ_DEFAULT unless you really need a high priority queue.
  411. *
  412. * This allocates a DMA channel and its associated parameter RAM slot.
  413. * The parameter RAM is initialized to hold a dummy transfer.
  414. *
  415. * Normal use is to pass a specific channel number as @channel, to make
  416. * use of hardware events mapped to that channel. When the channel will
  417. * be used only for software triggering or event chaining, channels not
  418. * mapped to hardware events (or mapped to unused events) are preferable.
  419. *
  420. * DMA transfers start from a channel using edma_start(), or by
  421. * chaining. When the transfer described in that channel's parameter RAM
  422. * slot completes, that slot's data may be reloaded through a link.
  423. *
  424. * DMA errors are only reported to the @callback associated with the
  425. * channel driving that transfer, but transfer completion callbacks can
  426. * be sent to another channel under control of the TCC field in
  427. * the option word of the transfer's parameter RAM set. Drivers must not
  428. * use DMA transfer completion callbacks for channels they did not allocate.
  429. * (The same applies to TCC codes used in transfer chaining.)
  430. *
  431. * Returns the number of the channel, else negative errno.
  432. */
  433. int edma_alloc_channel(int channel,
  434. void (*callback)(unsigned channel, u16 ch_status, void *data),
  435. void *data,
  436. enum dma_event_q eventq_no)
  437. {
  438. if (channel < 0) {
  439. channel = 0;
  440. for (;;) {
  441. channel = find_next_bit(edma_noevent,
  442. num_channels, channel);
  443. if (channel == num_channels)
  444. return -ENOMEM;
  445. if (!test_and_set_bit(channel, edma_inuse))
  446. break;
  447. channel++;
  448. }
  449. } else if (channel >= num_channels) {
  450. return -EINVAL;
  451. } else if (test_and_set_bit(channel, edma_inuse)) {
  452. return -EBUSY;
  453. }
  454. /* ensure access through shadow region 0 */
  455. edma_or_array2(EDMA_DRAE, 0, channel >> 5, 1 << (channel & 0x1f));
  456. /* ensure no events are pending */
  457. edma_stop(channel);
  458. memcpy_toio(edmacc_regs_base + PARM_OFFSET(channel),
  459. &dummy_paramset, PARM_SIZE);
  460. if (callback)
  461. setup_dma_interrupt(channel, callback, data);
  462. map_dmach_queue(channel, eventq_no);
  463. return channel;
  464. }
  465. EXPORT_SYMBOL(edma_alloc_channel);
  466. /**
  467. * edma_free_channel - deallocate DMA channel
  468. * @channel: dma channel returned from edma_alloc_channel()
  469. *
  470. * This deallocates the DMA channel and associated parameter RAM slot
  471. * allocated by edma_alloc_channel().
  472. *
  473. * Callers are responsible for ensuring the channel is inactive, and
  474. * will not be reactivated by linking, chaining, or software calls to
  475. * edma_start().
  476. */
  477. void edma_free_channel(unsigned channel)
  478. {
  479. if (channel >= num_channels)
  480. return;
  481. setup_dma_interrupt(channel, NULL, NULL);
  482. /* REVISIT should probably take out of shadow region 0 */
  483. memcpy_toio(edmacc_regs_base + PARM_OFFSET(channel),
  484. &dummy_paramset, PARM_SIZE);
  485. clear_bit(channel, edma_inuse);
  486. }
  487. EXPORT_SYMBOL(edma_free_channel);
  488. /**
  489. * edma_alloc_slot - allocate DMA parameter RAM
  490. * @slot: specific slot to allocate; negative for "any unused slot"
  491. *
  492. * This allocates a parameter RAM slot, initializing it to hold a
  493. * dummy transfer. Slots allocated using this routine have not been
  494. * mapped to a hardware DMA channel, and will normally be used by
  495. * linking to them from a slot associated with a DMA channel.
  496. *
  497. * Normal use is to pass EDMA_SLOT_ANY as the @slot, but specific
  498. * slots may be allocated on behalf of DSP firmware.
  499. *
  500. * Returns the number of the slot, else negative errno.
  501. */
  502. int edma_alloc_slot(int slot)
  503. {
  504. if (slot < 0) {
  505. slot = num_channels;
  506. for (;;) {
  507. slot = find_next_zero_bit(edma_inuse,
  508. num_slots, slot);
  509. if (slot == num_slots)
  510. return -ENOMEM;
  511. if (!test_and_set_bit(slot, edma_inuse))
  512. break;
  513. }
  514. } else if (slot < num_channels || slot >= num_slots) {
  515. return -EINVAL;
  516. } else if (test_and_set_bit(slot, edma_inuse)) {
  517. return -EBUSY;
  518. }
  519. memcpy_toio(edmacc_regs_base + PARM_OFFSET(slot),
  520. &dummy_paramset, PARM_SIZE);
  521. return slot;
  522. }
  523. EXPORT_SYMBOL(edma_alloc_slot);
  524. /**
  525. * edma_free_slot - deallocate DMA parameter RAM
  526. * @slot: parameter RAM slot returned from edma_alloc_slot()
  527. *
  528. * This deallocates the parameter RAM slot allocated by edma_alloc_slot().
  529. * Callers are responsible for ensuring the slot is inactive, and will
  530. * not be activated.
  531. */
  532. void edma_free_slot(unsigned slot)
  533. {
  534. if (slot < num_channels || slot >= num_slots)
  535. return;
  536. memcpy_toio(edmacc_regs_base + PARM_OFFSET(slot),
  537. &dummy_paramset, PARM_SIZE);
  538. clear_bit(slot, edma_inuse);
  539. }
  540. EXPORT_SYMBOL(edma_free_slot);
  541. /*-----------------------------------------------------------------------*/
  542. /* Parameter RAM operations (i) -- read/write partial slots */
  543. /**
  544. * edma_set_src - set initial DMA source address in parameter RAM slot
  545. * @slot: parameter RAM slot being configured
  546. * @src_port: physical address of source (memory, controller FIFO, etc)
  547. * @addressMode: INCR, except in very rare cases
  548. * @fifoWidth: ignored unless @addressMode is FIFO, else specifies the
  549. * width to use when addressing the fifo (e.g. W8BIT, W32BIT)
  550. *
  551. * Note that the source address is modified during the DMA transfer
  552. * according to edma_set_src_index().
  553. */
  554. void edma_set_src(unsigned slot, dma_addr_t src_port,
  555. enum address_mode mode, enum fifo_width width)
  556. {
  557. if (slot < num_slots) {
  558. unsigned int i = edma_parm_read(PARM_OPT, slot);
  559. if (mode) {
  560. /* set SAM and program FWID */
  561. i = (i & ~(EDMA_FWID)) | (SAM | ((width & 0x7) << 8));
  562. } else {
  563. /* clear SAM */
  564. i &= ~SAM;
  565. }
  566. edma_parm_write(PARM_OPT, slot, i);
  567. /* set the source port address
  568. in source register of param structure */
  569. edma_parm_write(PARM_SRC, slot, src_port);
  570. }
  571. }
  572. EXPORT_SYMBOL(edma_set_src);
  573. /**
  574. * edma_set_dest - set initial DMA destination address in parameter RAM slot
  575. * @slot: parameter RAM slot being configured
  576. * @dest_port: physical address of destination (memory, controller FIFO, etc)
  577. * @addressMode: INCR, except in very rare cases
  578. * @fifoWidth: ignored unless @addressMode is FIFO, else specifies the
  579. * width to use when addressing the fifo (e.g. W8BIT, W32BIT)
  580. *
  581. * Note that the destination address is modified during the DMA transfer
  582. * according to edma_set_dest_index().
  583. */
  584. void edma_set_dest(unsigned slot, dma_addr_t dest_port,
  585. enum address_mode mode, enum fifo_width width)
  586. {
  587. if (slot < num_slots) {
  588. unsigned int i = edma_parm_read(PARM_OPT, slot);
  589. if (mode) {
  590. /* set DAM and program FWID */
  591. i = (i & ~(EDMA_FWID)) | (DAM | ((width & 0x7) << 8));
  592. } else {
  593. /* clear DAM */
  594. i &= ~DAM;
  595. }
  596. edma_parm_write(PARM_OPT, slot, i);
  597. /* set the destination port address
  598. in dest register of param structure */
  599. edma_parm_write(PARM_DST, slot, dest_port);
  600. }
  601. }
  602. EXPORT_SYMBOL(edma_set_dest);
  603. /**
  604. * edma_get_position - returns the current transfer points
  605. * @slot: parameter RAM slot being examined
  606. * @src: pointer to source port position
  607. * @dst: pointer to destination port position
  608. *
  609. * Returns current source and destination addresses for a particular
  610. * parameter RAM slot. Its channel should not be active when this is called.
  611. */
  612. void edma_get_position(unsigned slot, dma_addr_t *src, dma_addr_t *dst)
  613. {
  614. struct edmacc_param temp;
  615. edma_read_slot(slot, &temp);
  616. if (src != NULL)
  617. *src = temp.src;
  618. if (dst != NULL)
  619. *dst = temp.dst;
  620. }
  621. EXPORT_SYMBOL(edma_get_position);
  622. /**
  623. * edma_set_src_index - configure DMA source address indexing
  624. * @slot: parameter RAM slot being configured
  625. * @src_bidx: byte offset between source arrays in a frame
  626. * @src_cidx: byte offset between source frames in a block
  627. *
  628. * Offsets are specified to support either contiguous or discontiguous
  629. * memory transfers, or repeated access to a hardware register, as needed.
  630. * When accessing hardware registers, both offsets are normally zero.
  631. */
  632. void edma_set_src_index(unsigned slot, s16 src_bidx, s16 src_cidx)
  633. {
  634. if (slot < num_slots) {
  635. edma_parm_modify(PARM_SRC_DST_BIDX, slot,
  636. 0xffff0000, src_bidx);
  637. edma_parm_modify(PARM_SRC_DST_CIDX, slot,
  638. 0xffff0000, src_cidx);
  639. }
  640. }
  641. EXPORT_SYMBOL(edma_set_src_index);
  642. /**
  643. * edma_set_dest_index - configure DMA destination address indexing
  644. * @slot: parameter RAM slot being configured
  645. * @dest_bidx: byte offset between destination arrays in a frame
  646. * @dest_cidx: byte offset between destination frames in a block
  647. *
  648. * Offsets are specified to support either contiguous or discontiguous
  649. * memory transfers, or repeated access to a hardware register, as needed.
  650. * When accessing hardware registers, both offsets are normally zero.
  651. */
  652. void edma_set_dest_index(unsigned slot, s16 dest_bidx, s16 dest_cidx)
  653. {
  654. if (slot < num_slots) {
  655. edma_parm_modify(PARM_SRC_DST_BIDX, slot,
  656. 0x0000ffff, dest_bidx << 16);
  657. edma_parm_modify(PARM_SRC_DST_CIDX, slot,
  658. 0x0000ffff, dest_cidx << 16);
  659. }
  660. }
  661. EXPORT_SYMBOL(edma_set_dest_index);
  662. /**
  663. * edma_set_transfer_params - configure DMA transfer parameters
  664. * @slot: parameter RAM slot being configured
  665. * @acnt: how many bytes per array (at least one)
  666. * @bcnt: how many arrays per frame (at least one)
  667. * @ccnt: how many frames per block (at least one)
  668. * @bcnt_rld: used only for A-Synchronized transfers; this specifies
  669. * the value to reload into bcnt when it decrements to zero
  670. * @sync_mode: ASYNC or ABSYNC
  671. *
  672. * See the EDMA3 documentation to understand how to configure and link
  673. * transfers using the fields in PaRAM slots. If you are not doing it
  674. * all at once with edma_write_slot(), you will use this routine
  675. * plus two calls each for source and destination, setting the initial
  676. * address and saying how to index that address.
  677. *
  678. * An example of an A-Synchronized transfer is a serial link using a
  679. * single word shift register. In that case, @acnt would be equal to
  680. * that word size; the serial controller issues a DMA synchronization
  681. * event to transfer each word, and memory access by the DMA transfer
  682. * controller will be word-at-a-time.
  683. *
  684. * An example of an AB-Synchronized transfer is a device using a FIFO.
  685. * In that case, @acnt equals the FIFO width and @bcnt equals its depth.
  686. * The controller with the FIFO issues DMA synchronization events when
  687. * the FIFO threshold is reached, and the DMA transfer controller will
  688. * transfer one frame to (or from) the FIFO. It will probably use
  689. * efficient burst modes to access memory.
  690. */
  691. void edma_set_transfer_params(unsigned slot,
  692. u16 acnt, u16 bcnt, u16 ccnt,
  693. u16 bcnt_rld, enum sync_dimension sync_mode)
  694. {
  695. if (slot < num_slots) {
  696. edma_parm_modify(PARM_LINK_BCNTRLD, slot,
  697. 0x0000ffff, bcnt_rld << 16);
  698. if (sync_mode == ASYNC)
  699. edma_parm_and(PARM_OPT, slot, ~SYNCDIM);
  700. else
  701. edma_parm_or(PARM_OPT, slot, SYNCDIM);
  702. /* Set the acount, bcount, ccount registers */
  703. edma_parm_write(PARM_A_B_CNT, slot, (bcnt << 16) | acnt);
  704. edma_parm_write(PARM_CCNT, slot, ccnt);
  705. }
  706. }
  707. EXPORT_SYMBOL(edma_set_transfer_params);
  708. /**
  709. * edma_link - link one parameter RAM slot to another
  710. * @from: parameter RAM slot originating the link
  711. * @to: parameter RAM slot which is the link target
  712. *
  713. * The originating slot should not be part of any active DMA transfer.
  714. */
  715. void edma_link(unsigned from, unsigned to)
  716. {
  717. if (from >= num_slots)
  718. return;
  719. if (to >= num_slots)
  720. return;
  721. edma_parm_modify(PARM_LINK_BCNTRLD, from, 0xffff0000, PARM_OFFSET(to));
  722. }
  723. EXPORT_SYMBOL(edma_link);
  724. /**
  725. * edma_unlink - cut link from one parameter RAM slot
  726. * @from: parameter RAM slot originating the link
  727. *
  728. * The originating slot should not be part of any active DMA transfer.
  729. * Its link is set to 0xffff.
  730. */
  731. void edma_unlink(unsigned from)
  732. {
  733. if (from >= num_slots)
  734. return;
  735. edma_parm_or(PARM_LINK_BCNTRLD, from, 0xffff);
  736. }
  737. EXPORT_SYMBOL(edma_unlink);
  738. /*-----------------------------------------------------------------------*/
  739. /* Parameter RAM operations (ii) -- read/write whole parameter sets */
  740. /**
  741. * edma_write_slot - write parameter RAM data for slot
  742. * @slot: number of parameter RAM slot being modified
  743. * @param: data to be written into parameter RAM slot
  744. *
  745. * Use this to assign all parameters of a transfer at once. This
  746. * allows more efficient setup of transfers than issuing multiple
  747. * calls to set up those parameters in small pieces, and provides
  748. * complete control over all transfer options.
  749. */
  750. void edma_write_slot(unsigned slot, const struct edmacc_param *param)
  751. {
  752. if (slot >= num_slots)
  753. return;
  754. memcpy_toio(edmacc_regs_base + PARM_OFFSET(slot), param, PARM_SIZE);
  755. }
  756. EXPORT_SYMBOL(edma_write_slot);
  757. /**
  758. * edma_read_slot - read parameter RAM data from slot
  759. * @slot: number of parameter RAM slot being copied
  760. * @param: where to store copy of parameter RAM data
  761. *
  762. * Use this to read data from a parameter RAM slot, perhaps to
  763. * save them as a template for later reuse.
  764. */
  765. void edma_read_slot(unsigned slot, struct edmacc_param *param)
  766. {
  767. if (slot >= num_slots)
  768. return;
  769. memcpy_fromio(param, edmacc_regs_base + PARM_OFFSET(slot), PARM_SIZE);
  770. }
  771. EXPORT_SYMBOL(edma_read_slot);
  772. /*-----------------------------------------------------------------------*/
  773. /* Various EDMA channel control operations */
  774. /**
  775. * edma_pause - pause dma on a channel
  776. * @channel: on which edma_start() has been called
  777. *
  778. * This temporarily disables EDMA hardware events on the specified channel,
  779. * preventing them from triggering new transfers on its behalf
  780. */
  781. void edma_pause(unsigned channel)
  782. {
  783. if (channel < num_channels) {
  784. unsigned int mask = (1 << (channel & 0x1f));
  785. edma_shadow0_write_array(SH_EECR, channel >> 5, mask);
  786. }
  787. }
  788. EXPORT_SYMBOL(edma_pause);
  789. /**
  790. * edma_resume - resumes dma on a paused channel
  791. * @channel: on which edma_pause() has been called
  792. *
  793. * This re-enables EDMA hardware events on the specified channel.
  794. */
  795. void edma_resume(unsigned channel)
  796. {
  797. if (channel < num_channels) {
  798. unsigned int mask = (1 << (channel & 0x1f));
  799. edma_shadow0_write_array(SH_EESR, channel >> 5, mask);
  800. }
  801. }
  802. EXPORT_SYMBOL(edma_resume);
  803. /**
  804. * edma_start - start dma on a channel
  805. * @channel: channel being activated
  806. *
  807. * Channels with event associations will be triggered by their hardware
  808. * events, and channels without such associations will be triggered by
  809. * software. (At this writing there is no interface for using software
  810. * triggers except with channels that don't support hardware triggers.)
  811. *
  812. * Returns zero on success, else negative errno.
  813. */
  814. int edma_start(unsigned channel)
  815. {
  816. if (channel < num_channels) {
  817. int j = channel >> 5;
  818. unsigned int mask = (1 << (channel & 0x1f));
  819. /* EDMA channels without event association */
  820. if (test_bit(channel, edma_noevent)) {
  821. pr_debug("EDMA: ESR%d %08x\n", j,
  822. edma_shadow0_read_array(SH_ESR, j));
  823. edma_shadow0_write_array(SH_ESR, j, mask);
  824. return 0;
  825. }
  826. /* EDMA channel with event association */
  827. pr_debug("EDMA: ER%d %08x\n", j,
  828. edma_shadow0_read_array(SH_ER, j));
  829. /* Clear any pending error */
  830. edma_write_array(EDMA_EMCR, j, mask);
  831. /* Clear any SER */
  832. edma_shadow0_write_array(SH_SECR, j, mask);
  833. edma_shadow0_write_array(SH_EESR, j, mask);
  834. pr_debug("EDMA: EER%d %08x\n", j,
  835. edma_shadow0_read_array(SH_EER, j));
  836. return 0;
  837. }
  838. return -EINVAL;
  839. }
  840. EXPORT_SYMBOL(edma_start);
  841. /**
  842. * edma_stop - stops dma on the channel passed
  843. * @channel: channel being deactivated
  844. *
  845. * When @lch is a channel, any active transfer is paused and
  846. * all pending hardware events are cleared. The current transfer
  847. * may not be resumed, and the channel's Parameter RAM should be
  848. * reinitialized before being reused.
  849. */
  850. void edma_stop(unsigned channel)
  851. {
  852. if (channel < num_channels) {
  853. int j = channel >> 5;
  854. unsigned int mask = (1 << (channel & 0x1f));
  855. edma_shadow0_write_array(SH_EECR, j, mask);
  856. edma_shadow0_write_array(SH_ECR, j, mask);
  857. edma_shadow0_write_array(SH_SECR, j, mask);
  858. edma_write_array(EDMA_EMCR, j, mask);
  859. pr_debug("EDMA: EER%d %08x\n", j,
  860. edma_shadow0_read_array(SH_EER, j));
  861. /* REVISIT: consider guarding against inappropriate event
  862. * chaining by overwriting with dummy_paramset.
  863. */
  864. }
  865. }
  866. EXPORT_SYMBOL(edma_stop);
  867. /******************************************************************************
  868. *
  869. * It cleans ParamEntry qand bring back EDMA to initial state if media has
  870. * been removed before EDMA has finished.It is usedful for removable media.
  871. * Arguments:
  872. * ch_no - channel no
  873. *
  874. * Return: zero on success, or corresponding error no on failure
  875. *
  876. * FIXME this should not be needed ... edma_stop() should suffice.
  877. *
  878. *****************************************************************************/
  879. void edma_clean_channel(unsigned channel)
  880. {
  881. if (channel < num_channels) {
  882. int j = (channel >> 5);
  883. unsigned int mask = 1 << (channel & 0x1f);
  884. pr_debug("EDMA: EMR%d %08x\n", j,
  885. edma_read_array(EDMA_EMR, j));
  886. edma_shadow0_write_array(SH_ECR, j, mask);
  887. /* Clear the corresponding EMR bits */
  888. edma_write_array(EDMA_EMCR, j, mask);
  889. /* Clear any SER */
  890. edma_shadow0_write_array(SH_SECR, j, mask);
  891. edma_write(EDMA_CCERRCLR, (1 << 16) | 0x3);
  892. }
  893. }
  894. EXPORT_SYMBOL(edma_clean_channel);
  895. /*
  896. * edma_clear_event - clear an outstanding event on the DMA channel
  897. * Arguments:
  898. * channel - channel number
  899. */
  900. void edma_clear_event(unsigned channel)
  901. {
  902. if (channel >= num_channels)
  903. return;
  904. if (channel < 32)
  905. edma_write(EDMA_ECR, 1 << channel);
  906. else
  907. edma_write(EDMA_ECRH, 1 << (channel - 32));
  908. }
  909. EXPORT_SYMBOL(edma_clear_event);
  910. /*-----------------------------------------------------------------------*/
  911. static int __init edma_probe(struct platform_device *pdev)
  912. {
  913. struct edma_soc_info *info = pdev->dev.platform_data;
  914. int i;
  915. int status;
  916. const s8 *noevent;
  917. int irq = 0, err_irq = 0;
  918. struct resource *r;
  919. resource_size_t len;
  920. if (!info)
  921. return -ENODEV;
  922. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "edma_cc");
  923. if (!r)
  924. return -ENODEV;
  925. len = r->end - r->start + 1;
  926. r = request_mem_region(r->start, len, r->name);
  927. if (!r)
  928. return -EBUSY;
  929. edmacc_regs_base = ioremap(r->start, len);
  930. if (!edmacc_regs_base) {
  931. status = -EBUSY;
  932. goto fail1;
  933. }
  934. num_channels = min_t(unsigned, info->n_channel, EDMA_MAX_DMACH);
  935. num_slots = min_t(unsigned, info->n_slot, EDMA_MAX_PARAMENTRY);
  936. dev_dbg(&pdev->dev, "DMA REG BASE ADDR=%p\n", edmacc_regs_base);
  937. for (i = 0; i < num_slots; i++)
  938. memcpy_toio(edmacc_regs_base + PARM_OFFSET(i),
  939. &dummy_paramset, PARM_SIZE);
  940. noevent = info->noevent;
  941. if (noevent) {
  942. while (*noevent != -1)
  943. set_bit(*noevent++, edma_noevent);
  944. }
  945. irq = platform_get_irq(pdev, 0);
  946. status = request_irq(irq, dma_irq_handler, 0, "edma", &pdev->dev);
  947. if (status < 0) {
  948. dev_dbg(&pdev->dev, "request_irq %d failed --> %d\n",
  949. irq, status);
  950. goto fail;
  951. }
  952. err_irq = platform_get_irq(pdev, 1);
  953. status = request_irq(err_irq, dma_ccerr_handler, 0,
  954. "edma_error", &pdev->dev);
  955. if (status < 0) {
  956. dev_dbg(&pdev->dev, "request_irq %d failed --> %d\n",
  957. err_irq, status);
  958. goto fail;
  959. }
  960. if (tc_errs_handled) {
  961. status = request_irq(IRQ_TCERRINT0, dma_tc0err_handler, 0,
  962. "edma_tc0", &pdev->dev);
  963. if (status < 0) {
  964. dev_dbg(&pdev->dev, "request_irq %d failed --> %d\n",
  965. IRQ_TCERRINT0, status);
  966. return status;
  967. }
  968. status = request_irq(IRQ_TCERRINT, dma_tc1err_handler, 0,
  969. "edma_tc1", &pdev->dev);
  970. if (status < 0) {
  971. dev_dbg(&pdev->dev, "request_irq %d --> %d\n",
  972. IRQ_TCERRINT, status);
  973. return status;
  974. }
  975. }
  976. /* Everything lives on transfer controller 1 until otherwise specified.
  977. * This way, long transfers on the low priority queue
  978. * started by the codec engine will not cause audio defects.
  979. */
  980. for (i = 0; i < num_channels; i++)
  981. map_dmach_queue(i, EVENTQ_1);
  982. /* Event queue to TC mapping */
  983. for (i = 0; queue_tc_mapping[i][0] != -1; i++)
  984. map_queue_tc(queue_tc_mapping[i][0], queue_tc_mapping[i][1]);
  985. /* Event queue priority mapping */
  986. for (i = 0; queue_priority_mapping[i][0] != -1; i++)
  987. assign_priority_to_queue(queue_priority_mapping[i][0],
  988. queue_priority_mapping[i][1]);
  989. for (i = 0; i < info->n_region; i++) {
  990. edma_write_array2(EDMA_DRAE, i, 0, 0x0);
  991. edma_write_array2(EDMA_DRAE, i, 1, 0x0);
  992. edma_write_array(EDMA_QRAE, i, 0x0);
  993. }
  994. return 0;
  995. fail:
  996. if (err_irq)
  997. free_irq(err_irq, NULL);
  998. if (irq)
  999. free_irq(irq, NULL);
  1000. iounmap(edmacc_regs_base);
  1001. fail1:
  1002. release_mem_region(r->start, len);
  1003. return status;
  1004. }
  1005. static struct platform_driver edma_driver = {
  1006. .driver.name = "edma",
  1007. };
  1008. static int __init edma_init(void)
  1009. {
  1010. return platform_driver_probe(&edma_driver, edma_probe);
  1011. }
  1012. arch_initcall(edma_init);