mcbsp.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. /*
  2. * linux/arch/arm/plat-omap/mcbsp.c
  3. *
  4. * Copyright (C) 2004 Nokia Corporation
  5. * Author: Samuel Ortiz <samuel.ortiz@nokia.com>
  6. *
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * Multichannel mode not supported.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/device.h>
  17. #include <linux/wait.h>
  18. #include <linux/completion.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/err.h>
  21. #include <linux/clk.h>
  22. #include <linux/delay.h>
  23. #include <asm/io.h>
  24. #include <asm/irq.h>
  25. #include <asm/arch/dma.h>
  26. #include <asm/arch/mux.h>
  27. #include <asm/arch/irqs.h>
  28. #include <asm/arch/dsp_common.h>
  29. #include <asm/arch/mcbsp.h>
  30. #ifdef CONFIG_MCBSP_DEBUG
  31. #define DBG(x...) printk(x)
  32. #else
  33. #define DBG(x...) do { } while (0)
  34. #endif
  35. struct omap_mcbsp {
  36. u32 io_base;
  37. u8 id;
  38. u8 free;
  39. omap_mcbsp_word_length rx_word_length;
  40. omap_mcbsp_word_length tx_word_length;
  41. omap_mcbsp_io_type_t io_type; /* IRQ or poll */
  42. /* IRQ based TX/RX */
  43. int rx_irq;
  44. int tx_irq;
  45. /* DMA stuff */
  46. u8 dma_rx_sync;
  47. short dma_rx_lch;
  48. u8 dma_tx_sync;
  49. short dma_tx_lch;
  50. /* Completion queues */
  51. struct completion tx_irq_completion;
  52. struct completion rx_irq_completion;
  53. struct completion tx_dma_completion;
  54. struct completion rx_dma_completion;
  55. spinlock_t lock;
  56. };
  57. static struct omap_mcbsp mcbsp[OMAP_MAX_MCBSP_COUNT];
  58. #ifdef CONFIG_ARCH_OMAP1
  59. static struct clk *mcbsp_dsp_ck = 0;
  60. static struct clk *mcbsp_api_ck = 0;
  61. static struct clk *mcbsp_dspxor_ck = 0;
  62. #endif
  63. #ifdef CONFIG_ARCH_OMAP2
  64. static struct clk *mcbsp1_ick = 0;
  65. static struct clk *mcbsp1_fck = 0;
  66. static struct clk *mcbsp2_ick = 0;
  67. static struct clk *mcbsp2_fck = 0;
  68. #endif
  69. static void omap_mcbsp_dump_reg(u8 id)
  70. {
  71. DBG("**** MCBSP%d regs ****\n", mcbsp[id].id);
  72. DBG("DRR2: 0x%04x\n", OMAP_MCBSP_READ(mcbsp[id].io_base, DRR2));
  73. DBG("DRR1: 0x%04x\n", OMAP_MCBSP_READ(mcbsp[id].io_base, DRR1));
  74. DBG("DXR2: 0x%04x\n", OMAP_MCBSP_READ(mcbsp[id].io_base, DXR2));
  75. DBG("DXR1: 0x%04x\n", OMAP_MCBSP_READ(mcbsp[id].io_base, DXR1));
  76. DBG("SPCR2: 0x%04x\n", OMAP_MCBSP_READ(mcbsp[id].io_base, SPCR2));
  77. DBG("SPCR1: 0x%04x\n", OMAP_MCBSP_READ(mcbsp[id].io_base, SPCR1));
  78. DBG("RCR2: 0x%04x\n", OMAP_MCBSP_READ(mcbsp[id].io_base, RCR2));
  79. DBG("RCR1: 0x%04x\n", OMAP_MCBSP_READ(mcbsp[id].io_base, RCR1));
  80. DBG("XCR2: 0x%04x\n", OMAP_MCBSP_READ(mcbsp[id].io_base, XCR2));
  81. DBG("XCR1: 0x%04x\n", OMAP_MCBSP_READ(mcbsp[id].io_base, XCR1));
  82. DBG("SRGR2: 0x%04x\n", OMAP_MCBSP_READ(mcbsp[id].io_base, SRGR2));
  83. DBG("SRGR1: 0x%04x\n", OMAP_MCBSP_READ(mcbsp[id].io_base, SRGR1));
  84. DBG("PCR0: 0x%04x\n", OMAP_MCBSP_READ(mcbsp[id].io_base, PCR0));
  85. DBG("***********************\n");
  86. }
  87. static irqreturn_t omap_mcbsp_tx_irq_handler(int irq, void *dev_id)
  88. {
  89. struct omap_mcbsp *mcbsp_tx = dev_id;
  90. DBG("TX IRQ callback : 0x%x\n",
  91. OMAP_MCBSP_READ(mcbsp_tx->io_base, SPCR2));
  92. complete(&mcbsp_tx->tx_irq_completion);
  93. return IRQ_HANDLED;
  94. }
  95. static irqreturn_t omap_mcbsp_rx_irq_handler(int irq, void *dev_id)
  96. {
  97. struct omap_mcbsp *mcbsp_rx = dev_id;
  98. DBG("RX IRQ callback : 0x%x\n",
  99. OMAP_MCBSP_READ(mcbsp_rx->io_base, SPCR2));
  100. complete(&mcbsp_rx->rx_irq_completion);
  101. return IRQ_HANDLED;
  102. }
  103. static void omap_mcbsp_tx_dma_callback(int lch, u16 ch_status, void *data)
  104. {
  105. struct omap_mcbsp *mcbsp_dma_tx = data;
  106. DBG("TX DMA callback : 0x%x\n",
  107. OMAP_MCBSP_READ(mcbsp_dma_tx->io_base, SPCR2));
  108. /* We can free the channels */
  109. omap_free_dma(mcbsp_dma_tx->dma_tx_lch);
  110. mcbsp_dma_tx->dma_tx_lch = -1;
  111. complete(&mcbsp_dma_tx->tx_dma_completion);
  112. }
  113. static void omap_mcbsp_rx_dma_callback(int lch, u16 ch_status, void *data)
  114. {
  115. struct omap_mcbsp *mcbsp_dma_rx = data;
  116. DBG("RX DMA callback : 0x%x\n",
  117. OMAP_MCBSP_READ(mcbsp_dma_rx->io_base, SPCR2));
  118. /* We can free the channels */
  119. omap_free_dma(mcbsp_dma_rx->dma_rx_lch);
  120. mcbsp_dma_rx->dma_rx_lch = -1;
  121. complete(&mcbsp_dma_rx->rx_dma_completion);
  122. }
  123. /*
  124. * omap_mcbsp_config simply write a config to the
  125. * appropriate McBSP.
  126. * You either call this function or set the McBSP registers
  127. * by yourself before calling omap_mcbsp_start().
  128. */
  129. void omap_mcbsp_config(unsigned int id, const struct omap_mcbsp_reg_cfg * config)
  130. {
  131. u32 io_base = mcbsp[id].io_base;
  132. DBG("OMAP-McBSP: McBSP%d io_base: 0x%8x\n", id+1, io_base);
  133. /* We write the given config */
  134. OMAP_MCBSP_WRITE(io_base, SPCR2, config->spcr2);
  135. OMAP_MCBSP_WRITE(io_base, SPCR1, config->spcr1);
  136. OMAP_MCBSP_WRITE(io_base, RCR2, config->rcr2);
  137. OMAP_MCBSP_WRITE(io_base, RCR1, config->rcr1);
  138. OMAP_MCBSP_WRITE(io_base, XCR2, config->xcr2);
  139. OMAP_MCBSP_WRITE(io_base, XCR1, config->xcr1);
  140. OMAP_MCBSP_WRITE(io_base, SRGR2, config->srgr2);
  141. OMAP_MCBSP_WRITE(io_base, SRGR1, config->srgr1);
  142. OMAP_MCBSP_WRITE(io_base, MCR2, config->mcr2);
  143. OMAP_MCBSP_WRITE(io_base, MCR1, config->mcr1);
  144. OMAP_MCBSP_WRITE(io_base, PCR0, config->pcr0);
  145. }
  146. static int omap_mcbsp_check(unsigned int id)
  147. {
  148. if (cpu_is_omap730()) {
  149. if (id > OMAP_MAX_MCBSP_COUNT - 1) {
  150. printk(KERN_ERR "OMAP-McBSP: McBSP%d doesn't exist\n", id + 1);
  151. return -1;
  152. }
  153. return 0;
  154. }
  155. if (cpu_is_omap15xx() || cpu_is_omap16xx() || cpu_is_omap24xx()) {
  156. if (id > OMAP_MAX_MCBSP_COUNT) {
  157. printk(KERN_ERR "OMAP-McBSP: McBSP%d doesn't exist\n", id + 1);
  158. return -1;
  159. }
  160. return 0;
  161. }
  162. return -1;
  163. }
  164. #ifdef CONFIG_ARCH_OMAP1
  165. static void omap_mcbsp_dsp_request(void)
  166. {
  167. if (cpu_is_omap15xx() || cpu_is_omap16xx()) {
  168. clk_enable(mcbsp_dsp_ck);
  169. clk_enable(mcbsp_api_ck);
  170. /* enable 12MHz clock to mcbsp 1 & 3 */
  171. clk_enable(mcbsp_dspxor_ck);
  172. /*
  173. * DSP external peripheral reset
  174. * FIXME: This should be moved to dsp code
  175. */
  176. __raw_writew(__raw_readw(DSP_RSTCT2) | 1 | 1 << 1,
  177. DSP_RSTCT2);
  178. }
  179. }
  180. static void omap_mcbsp_dsp_free(void)
  181. {
  182. if (cpu_is_omap15xx() || cpu_is_omap16xx()) {
  183. clk_disable(mcbsp_dspxor_ck);
  184. clk_disable(mcbsp_dsp_ck);
  185. clk_disable(mcbsp_api_ck);
  186. }
  187. }
  188. #endif
  189. #ifdef CONFIG_ARCH_OMAP2
  190. static void omap2_mcbsp2_mux_setup(void)
  191. {
  192. if (cpu_is_omap2420()) {
  193. omap_cfg_reg(Y15_24XX_MCBSP2_CLKX);
  194. omap_cfg_reg(R14_24XX_MCBSP2_FSX);
  195. omap_cfg_reg(W15_24XX_MCBSP2_DR);
  196. omap_cfg_reg(V15_24XX_MCBSP2_DX);
  197. omap_cfg_reg(V14_24XX_GPIO117);
  198. }
  199. /*
  200. * Need to add MUX settings for OMAP 2430 SDP
  201. */
  202. }
  203. #endif
  204. /*
  205. * We can choose between IRQ based or polled IO.
  206. * This needs to be called before omap_mcbsp_request().
  207. */
  208. int omap_mcbsp_set_io_type(unsigned int id, omap_mcbsp_io_type_t io_type)
  209. {
  210. if (omap_mcbsp_check(id) < 0)
  211. return -EINVAL;
  212. spin_lock(&mcbsp[id].lock);
  213. if (!mcbsp[id].free) {
  214. printk (KERN_ERR "OMAP-McBSP: McBSP%d is currently in use\n", id + 1);
  215. spin_unlock(&mcbsp[id].lock);
  216. return -EINVAL;
  217. }
  218. mcbsp[id].io_type = io_type;
  219. spin_unlock(&mcbsp[id].lock);
  220. return 0;
  221. }
  222. int omap_mcbsp_request(unsigned int id)
  223. {
  224. int err;
  225. if (omap_mcbsp_check(id) < 0)
  226. return -EINVAL;
  227. #ifdef CONFIG_ARCH_OMAP1
  228. /*
  229. * On 1510, 1610 and 1710, McBSP1 and McBSP3
  230. * are DSP public peripherals.
  231. */
  232. if (id == OMAP_MCBSP1 || id == OMAP_MCBSP3)
  233. omap_mcbsp_dsp_request();
  234. #endif
  235. #ifdef CONFIG_ARCH_OMAP2
  236. if (cpu_is_omap24xx()) {
  237. if (id == OMAP_MCBSP1) {
  238. clk_enable(mcbsp1_ick);
  239. clk_enable(mcbsp1_fck);
  240. } else {
  241. clk_enable(mcbsp2_ick);
  242. clk_enable(mcbsp2_fck);
  243. }
  244. }
  245. #endif
  246. spin_lock(&mcbsp[id].lock);
  247. if (!mcbsp[id].free) {
  248. printk (KERN_ERR "OMAP-McBSP: McBSP%d is currently in use\n", id + 1);
  249. spin_unlock(&mcbsp[id].lock);
  250. return -1;
  251. }
  252. mcbsp[id].free = 0;
  253. spin_unlock(&mcbsp[id].lock);
  254. if (mcbsp[id].io_type == OMAP_MCBSP_IRQ_IO) {
  255. /* We need to get IRQs here */
  256. err = request_irq(mcbsp[id].tx_irq, omap_mcbsp_tx_irq_handler, 0,
  257. "McBSP",
  258. (void *) (&mcbsp[id]));
  259. if (err != 0) {
  260. printk(KERN_ERR "OMAP-McBSP: Unable to request TX IRQ %d for McBSP%d\n",
  261. mcbsp[id].tx_irq, mcbsp[id].id);
  262. return err;
  263. }
  264. init_completion(&(mcbsp[id].tx_irq_completion));
  265. err = request_irq(mcbsp[id].rx_irq, omap_mcbsp_rx_irq_handler, 0,
  266. "McBSP",
  267. (void *) (&mcbsp[id]));
  268. if (err != 0) {
  269. printk(KERN_ERR "OMAP-McBSP: Unable to request RX IRQ %d for McBSP%d\n",
  270. mcbsp[id].rx_irq, mcbsp[id].id);
  271. free_irq(mcbsp[id].tx_irq, (void *) (&mcbsp[id]));
  272. return err;
  273. }
  274. init_completion(&(mcbsp[id].rx_irq_completion));
  275. }
  276. return 0;
  277. }
  278. void omap_mcbsp_free(unsigned int id)
  279. {
  280. if (omap_mcbsp_check(id) < 0)
  281. return;
  282. #ifdef CONFIG_ARCH_OMAP1
  283. if (cpu_class_is_omap1()) {
  284. if (id == OMAP_MCBSP1 || id == OMAP_MCBSP3)
  285. omap_mcbsp_dsp_free();
  286. }
  287. #endif
  288. #ifdef CONFIG_ARCH_OMAP2
  289. if (cpu_is_omap24xx()) {
  290. if (id == OMAP_MCBSP1) {
  291. clk_disable(mcbsp1_ick);
  292. clk_disable(mcbsp1_fck);
  293. } else {
  294. clk_disable(mcbsp2_ick);
  295. clk_disable(mcbsp2_fck);
  296. }
  297. }
  298. #endif
  299. spin_lock(&mcbsp[id].lock);
  300. if (mcbsp[id].free) {
  301. printk (KERN_ERR "OMAP-McBSP: McBSP%d was not reserved\n", id + 1);
  302. spin_unlock(&mcbsp[id].lock);
  303. return;
  304. }
  305. mcbsp[id].free = 1;
  306. spin_unlock(&mcbsp[id].lock);
  307. if (mcbsp[id].io_type == OMAP_MCBSP_IRQ_IO) {
  308. /* Free IRQs */
  309. free_irq(mcbsp[id].rx_irq, (void *) (&mcbsp[id]));
  310. free_irq(mcbsp[id].tx_irq, (void *) (&mcbsp[id]));
  311. }
  312. }
  313. /*
  314. * Here we start the McBSP, by enabling the sample
  315. * generator, both transmitter and receivers,
  316. * and the frame sync.
  317. */
  318. void omap_mcbsp_start(unsigned int id)
  319. {
  320. u32 io_base;
  321. u16 w;
  322. if (omap_mcbsp_check(id) < 0)
  323. return;
  324. io_base = mcbsp[id].io_base;
  325. mcbsp[id].rx_word_length = ((OMAP_MCBSP_READ(io_base, RCR1) >> 5) & 0x7);
  326. mcbsp[id].tx_word_length = ((OMAP_MCBSP_READ(io_base, XCR1) >> 5) & 0x7);
  327. /* Start the sample generator */
  328. w = OMAP_MCBSP_READ(io_base, SPCR2);
  329. OMAP_MCBSP_WRITE(io_base, SPCR2, w | (1 << 6));
  330. /* Enable transmitter and receiver */
  331. w = OMAP_MCBSP_READ(io_base, SPCR2);
  332. OMAP_MCBSP_WRITE(io_base, SPCR2, w | 1);
  333. w = OMAP_MCBSP_READ(io_base, SPCR1);
  334. OMAP_MCBSP_WRITE(io_base, SPCR1, w | 1);
  335. udelay(100);
  336. /* Start frame sync */
  337. w = OMAP_MCBSP_READ(io_base, SPCR2);
  338. OMAP_MCBSP_WRITE(io_base, SPCR2, w | (1 << 7));
  339. /* Dump McBSP Regs */
  340. omap_mcbsp_dump_reg(id);
  341. }
  342. void omap_mcbsp_stop(unsigned int id)
  343. {
  344. u32 io_base;
  345. u16 w;
  346. if (omap_mcbsp_check(id) < 0)
  347. return;
  348. io_base = mcbsp[id].io_base;
  349. /* Reset transmitter */
  350. w = OMAP_MCBSP_READ(io_base, SPCR2);
  351. OMAP_MCBSP_WRITE(io_base, SPCR2, w & ~(1));
  352. /* Reset receiver */
  353. w = OMAP_MCBSP_READ(io_base, SPCR1);
  354. OMAP_MCBSP_WRITE(io_base, SPCR1, w & ~(1));
  355. /* Reset the sample rate generator */
  356. w = OMAP_MCBSP_READ(io_base, SPCR2);
  357. OMAP_MCBSP_WRITE(io_base, SPCR2, w & ~(1 << 6));
  358. }
  359. /* polled mcbsp i/o operations */
  360. int omap_mcbsp_pollwrite(unsigned int id, u16 buf)
  361. {
  362. u32 base = mcbsp[id].io_base;
  363. writew(buf, base + OMAP_MCBSP_REG_DXR1);
  364. /* if frame sync error - clear the error */
  365. if (readw(base + OMAP_MCBSP_REG_SPCR2) & XSYNC_ERR) {
  366. /* clear error */
  367. writew(readw(base + OMAP_MCBSP_REG_SPCR2) & (~XSYNC_ERR),
  368. base + OMAP_MCBSP_REG_SPCR2);
  369. /* resend */
  370. return -1;
  371. } else {
  372. /* wait for transmit confirmation */
  373. int attemps = 0;
  374. while (!(readw(base + OMAP_MCBSP_REG_SPCR2) & XRDY)) {
  375. if (attemps++ > 1000) {
  376. writew(readw(base + OMAP_MCBSP_REG_SPCR2) &
  377. (~XRST),
  378. base + OMAP_MCBSP_REG_SPCR2);
  379. udelay(10);
  380. writew(readw(base + OMAP_MCBSP_REG_SPCR2) |
  381. (XRST),
  382. base + OMAP_MCBSP_REG_SPCR2);
  383. udelay(10);
  384. printk(KERN_ERR
  385. " Could not write to McBSP Register\n");
  386. return -2;
  387. }
  388. }
  389. }
  390. return 0;
  391. }
  392. int omap_mcbsp_pollread(unsigned int id, u16 * buf)
  393. {
  394. u32 base = mcbsp[id].io_base;
  395. /* if frame sync error - clear the error */
  396. if (readw(base + OMAP_MCBSP_REG_SPCR1) & RSYNC_ERR) {
  397. /* clear error */
  398. writew(readw(base + OMAP_MCBSP_REG_SPCR1) & (~RSYNC_ERR),
  399. base + OMAP_MCBSP_REG_SPCR1);
  400. /* resend */
  401. return -1;
  402. } else {
  403. /* wait for recieve confirmation */
  404. int attemps = 0;
  405. while (!(readw(base + OMAP_MCBSP_REG_SPCR1) & RRDY)) {
  406. if (attemps++ > 1000) {
  407. writew(readw(base + OMAP_MCBSP_REG_SPCR1) &
  408. (~RRST),
  409. base + OMAP_MCBSP_REG_SPCR1);
  410. udelay(10);
  411. writew(readw(base + OMAP_MCBSP_REG_SPCR1) |
  412. (RRST),
  413. base + OMAP_MCBSP_REG_SPCR1);
  414. udelay(10);
  415. printk(KERN_ERR
  416. " Could not read from McBSP Register\n");
  417. return -2;
  418. }
  419. }
  420. }
  421. *buf = readw(base + OMAP_MCBSP_REG_DRR1);
  422. return 0;
  423. }
  424. /*
  425. * IRQ based word transmission.
  426. */
  427. void omap_mcbsp_xmit_word(unsigned int id, u32 word)
  428. {
  429. u32 io_base;
  430. omap_mcbsp_word_length word_length = mcbsp[id].tx_word_length;
  431. if (omap_mcbsp_check(id) < 0)
  432. return;
  433. io_base = mcbsp[id].io_base;
  434. wait_for_completion(&(mcbsp[id].tx_irq_completion));
  435. if (word_length > OMAP_MCBSP_WORD_16)
  436. OMAP_MCBSP_WRITE(io_base, DXR2, word >> 16);
  437. OMAP_MCBSP_WRITE(io_base, DXR1, word & 0xffff);
  438. }
  439. u32 omap_mcbsp_recv_word(unsigned int id)
  440. {
  441. u32 io_base;
  442. u16 word_lsb, word_msb = 0;
  443. omap_mcbsp_word_length word_length = mcbsp[id].rx_word_length;
  444. if (omap_mcbsp_check(id) < 0)
  445. return -EINVAL;
  446. io_base = mcbsp[id].io_base;
  447. wait_for_completion(&(mcbsp[id].rx_irq_completion));
  448. if (word_length > OMAP_MCBSP_WORD_16)
  449. word_msb = OMAP_MCBSP_READ(io_base, DRR2);
  450. word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
  451. return (word_lsb | (word_msb << 16));
  452. }
  453. int omap_mcbsp_spi_master_xmit_word_poll(unsigned int id, u32 word)
  454. {
  455. u32 io_base = mcbsp[id].io_base;
  456. omap_mcbsp_word_length tx_word_length = mcbsp[id].tx_word_length;
  457. omap_mcbsp_word_length rx_word_length = mcbsp[id].rx_word_length;
  458. u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
  459. if (tx_word_length != rx_word_length)
  460. return -EINVAL;
  461. /* First we wait for the transmitter to be ready */
  462. spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
  463. while (!(spcr2 & XRDY)) {
  464. spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
  465. if (attempts++ > 1000) {
  466. /* We must reset the transmitter */
  467. OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 & (~XRST));
  468. udelay(10);
  469. OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 | XRST);
  470. udelay(10);
  471. printk("McBSP transmitter not ready\n");
  472. return -EAGAIN;
  473. }
  474. }
  475. /* Now we can push the data */
  476. if (tx_word_length > OMAP_MCBSP_WORD_16)
  477. OMAP_MCBSP_WRITE(io_base, DXR2, word >> 16);
  478. OMAP_MCBSP_WRITE(io_base, DXR1, word & 0xffff);
  479. /* We wait for the receiver to be ready */
  480. spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
  481. while (!(spcr1 & RRDY)) {
  482. spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
  483. if (attempts++ > 1000) {
  484. /* We must reset the receiver */
  485. OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 & (~RRST));
  486. udelay(10);
  487. OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 | RRST);
  488. udelay(10);
  489. printk("McBSP receiver not ready\n");
  490. return -EAGAIN;
  491. }
  492. }
  493. /* Receiver is ready, let's read the dummy data */
  494. if (rx_word_length > OMAP_MCBSP_WORD_16)
  495. word_msb = OMAP_MCBSP_READ(io_base, DRR2);
  496. word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
  497. return 0;
  498. }
  499. int omap_mcbsp_spi_master_recv_word_poll(unsigned int id, u32 * word)
  500. {
  501. u32 io_base = mcbsp[id].io_base, clock_word = 0;
  502. omap_mcbsp_word_length tx_word_length = mcbsp[id].tx_word_length;
  503. omap_mcbsp_word_length rx_word_length = mcbsp[id].rx_word_length;
  504. u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
  505. if (tx_word_length != rx_word_length)
  506. return -EINVAL;
  507. /* First we wait for the transmitter to be ready */
  508. spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
  509. while (!(spcr2 & XRDY)) {
  510. spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
  511. if (attempts++ > 1000) {
  512. /* We must reset the transmitter */
  513. OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 & (~XRST));
  514. udelay(10);
  515. OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 | XRST);
  516. udelay(10);
  517. printk("McBSP transmitter not ready\n");
  518. return -EAGAIN;
  519. }
  520. }
  521. /* We first need to enable the bus clock */
  522. if (tx_word_length > OMAP_MCBSP_WORD_16)
  523. OMAP_MCBSP_WRITE(io_base, DXR2, clock_word >> 16);
  524. OMAP_MCBSP_WRITE(io_base, DXR1, clock_word & 0xffff);
  525. /* We wait for the receiver to be ready */
  526. spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
  527. while (!(spcr1 & RRDY)) {
  528. spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
  529. if (attempts++ > 1000) {
  530. /* We must reset the receiver */
  531. OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 & (~RRST));
  532. udelay(10);
  533. OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 | RRST);
  534. udelay(10);
  535. printk("McBSP receiver not ready\n");
  536. return -EAGAIN;
  537. }
  538. }
  539. /* Receiver is ready, there is something for us */
  540. if (rx_word_length > OMAP_MCBSP_WORD_16)
  541. word_msb = OMAP_MCBSP_READ(io_base, DRR2);
  542. word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
  543. word[0] = (word_lsb | (word_msb << 16));
  544. return 0;
  545. }
  546. /*
  547. * Simple DMA based buffer rx/tx routines.
  548. * Nothing fancy, just a single buffer tx/rx through DMA.
  549. * The DMA resources are released once the transfer is done.
  550. * For anything fancier, you should use your own customized DMA
  551. * routines and callbacks.
  552. */
  553. int omap_mcbsp_xmit_buffer(unsigned int id, dma_addr_t buffer, unsigned int length)
  554. {
  555. int dma_tx_ch;
  556. int src_port = 0;
  557. int dest_port = 0;
  558. int sync_dev = 0;
  559. if (omap_mcbsp_check(id) < 0)
  560. return -EINVAL;
  561. if (omap_request_dma(mcbsp[id].dma_tx_sync, "McBSP TX", omap_mcbsp_tx_dma_callback,
  562. &mcbsp[id],
  563. &dma_tx_ch)) {
  564. printk("OMAP-McBSP: Unable to request DMA channel for McBSP%d TX. Trying IRQ based TX\n", id+1);
  565. return -EAGAIN;
  566. }
  567. mcbsp[id].dma_tx_lch = dma_tx_ch;
  568. DBG("TX DMA on channel %d\n", dma_tx_ch);
  569. init_completion(&(mcbsp[id].tx_dma_completion));
  570. if (cpu_class_is_omap1()) {
  571. src_port = OMAP_DMA_PORT_TIPB;
  572. dest_port = OMAP_DMA_PORT_EMIFF;
  573. }
  574. if (cpu_is_omap24xx())
  575. sync_dev = mcbsp[id].dma_tx_sync;
  576. omap_set_dma_transfer_params(mcbsp[id].dma_tx_lch,
  577. OMAP_DMA_DATA_TYPE_S16,
  578. length >> 1, 1,
  579. OMAP_DMA_SYNC_ELEMENT,
  580. sync_dev, 0);
  581. omap_set_dma_dest_params(mcbsp[id].dma_tx_lch,
  582. src_port,
  583. OMAP_DMA_AMODE_CONSTANT,
  584. mcbsp[id].io_base + OMAP_MCBSP_REG_DXR1,
  585. 0, 0);
  586. omap_set_dma_src_params(mcbsp[id].dma_tx_lch,
  587. dest_port,
  588. OMAP_DMA_AMODE_POST_INC,
  589. buffer,
  590. 0, 0);
  591. omap_start_dma(mcbsp[id].dma_tx_lch);
  592. wait_for_completion(&(mcbsp[id].tx_dma_completion));
  593. return 0;
  594. }
  595. int omap_mcbsp_recv_buffer(unsigned int id, dma_addr_t buffer, unsigned int length)
  596. {
  597. int dma_rx_ch;
  598. int src_port = 0;
  599. int dest_port = 0;
  600. int sync_dev = 0;
  601. if (omap_mcbsp_check(id) < 0)
  602. return -EINVAL;
  603. if (omap_request_dma(mcbsp[id].dma_rx_sync, "McBSP RX", omap_mcbsp_rx_dma_callback,
  604. &mcbsp[id],
  605. &dma_rx_ch)) {
  606. printk("Unable to request DMA channel for McBSP%d RX. Trying IRQ based RX\n", id+1);
  607. return -EAGAIN;
  608. }
  609. mcbsp[id].dma_rx_lch = dma_rx_ch;
  610. DBG("RX DMA on channel %d\n", dma_rx_ch);
  611. init_completion(&(mcbsp[id].rx_dma_completion));
  612. if (cpu_class_is_omap1()) {
  613. src_port = OMAP_DMA_PORT_TIPB;
  614. dest_port = OMAP_DMA_PORT_EMIFF;
  615. }
  616. if (cpu_is_omap24xx())
  617. sync_dev = mcbsp[id].dma_rx_sync;
  618. omap_set_dma_transfer_params(mcbsp[id].dma_rx_lch,
  619. OMAP_DMA_DATA_TYPE_S16,
  620. length >> 1, 1,
  621. OMAP_DMA_SYNC_ELEMENT,
  622. sync_dev, 0);
  623. omap_set_dma_src_params(mcbsp[id].dma_rx_lch,
  624. src_port,
  625. OMAP_DMA_AMODE_CONSTANT,
  626. mcbsp[id].io_base + OMAP_MCBSP_REG_DRR1,
  627. 0, 0);
  628. omap_set_dma_dest_params(mcbsp[id].dma_rx_lch,
  629. dest_port,
  630. OMAP_DMA_AMODE_POST_INC,
  631. buffer,
  632. 0, 0);
  633. omap_start_dma(mcbsp[id].dma_rx_lch);
  634. wait_for_completion(&(mcbsp[id].rx_dma_completion));
  635. return 0;
  636. }
  637. /*
  638. * SPI wrapper.
  639. * Since SPI setup is much simpler than the generic McBSP one,
  640. * this wrapper just need an omap_mcbsp_spi_cfg structure as an input.
  641. * Once this is done, you can call omap_mcbsp_start().
  642. */
  643. void omap_mcbsp_set_spi_mode(unsigned int id, const struct omap_mcbsp_spi_cfg * spi_cfg)
  644. {
  645. struct omap_mcbsp_reg_cfg mcbsp_cfg;
  646. if (omap_mcbsp_check(id) < 0)
  647. return;
  648. memset(&mcbsp_cfg, 0, sizeof(struct omap_mcbsp_reg_cfg));
  649. /* SPI has only one frame */
  650. mcbsp_cfg.rcr1 |= (RWDLEN1(spi_cfg->word_length) | RFRLEN1(0));
  651. mcbsp_cfg.xcr1 |= (XWDLEN1(spi_cfg->word_length) | XFRLEN1(0));
  652. /* Clock stop mode */
  653. if (spi_cfg->clk_stp_mode == OMAP_MCBSP_CLK_STP_MODE_NO_DELAY)
  654. mcbsp_cfg.spcr1 |= (1 << 12);
  655. else
  656. mcbsp_cfg.spcr1 |= (3 << 11);
  657. /* Set clock parities */
  658. if (spi_cfg->rx_clock_polarity == OMAP_MCBSP_CLK_RISING)
  659. mcbsp_cfg.pcr0 |= CLKRP;
  660. else
  661. mcbsp_cfg.pcr0 &= ~CLKRP;
  662. if (spi_cfg->tx_clock_polarity == OMAP_MCBSP_CLK_RISING)
  663. mcbsp_cfg.pcr0 &= ~CLKXP;
  664. else
  665. mcbsp_cfg.pcr0 |= CLKXP;
  666. /* Set SCLKME to 0 and CLKSM to 1 */
  667. mcbsp_cfg.pcr0 &= ~SCLKME;
  668. mcbsp_cfg.srgr2 |= CLKSM;
  669. /* Set FSXP */
  670. if (spi_cfg->fsx_polarity == OMAP_MCBSP_FS_ACTIVE_HIGH)
  671. mcbsp_cfg.pcr0 &= ~FSXP;
  672. else
  673. mcbsp_cfg.pcr0 |= FSXP;
  674. if (spi_cfg->spi_mode == OMAP_MCBSP_SPI_MASTER) {
  675. mcbsp_cfg.pcr0 |= CLKXM;
  676. mcbsp_cfg.srgr1 |= CLKGDV(spi_cfg->clk_div -1);
  677. mcbsp_cfg.pcr0 |= FSXM;
  678. mcbsp_cfg.srgr2 &= ~FSGM;
  679. mcbsp_cfg.xcr2 |= XDATDLY(1);
  680. mcbsp_cfg.rcr2 |= RDATDLY(1);
  681. }
  682. else {
  683. mcbsp_cfg.pcr0 &= ~CLKXM;
  684. mcbsp_cfg.srgr1 |= CLKGDV(1);
  685. mcbsp_cfg.pcr0 &= ~FSXM;
  686. mcbsp_cfg.xcr2 &= ~XDATDLY(3);
  687. mcbsp_cfg.rcr2 &= ~RDATDLY(3);
  688. }
  689. mcbsp_cfg.xcr2 &= ~XPHASE;
  690. mcbsp_cfg.rcr2 &= ~RPHASE;
  691. omap_mcbsp_config(id, &mcbsp_cfg);
  692. }
  693. /*
  694. * McBSP1 and McBSP3 are directly mapped on 1610 and 1510.
  695. * 730 has only 2 McBSP, and both of them are MPU peripherals.
  696. */
  697. struct omap_mcbsp_info {
  698. u32 virt_base;
  699. u8 dma_rx_sync, dma_tx_sync;
  700. u16 rx_irq, tx_irq;
  701. };
  702. #ifdef CONFIG_ARCH_OMAP730
  703. static const struct omap_mcbsp_info mcbsp_730[] = {
  704. [0] = { .virt_base = io_p2v(OMAP730_MCBSP1_BASE),
  705. .dma_rx_sync = OMAP_DMA_MCBSP1_RX,
  706. .dma_tx_sync = OMAP_DMA_MCBSP1_TX,
  707. .rx_irq = INT_730_McBSP1RX,
  708. .tx_irq = INT_730_McBSP1TX },
  709. [1] = { .virt_base = io_p2v(OMAP730_MCBSP2_BASE),
  710. .dma_rx_sync = OMAP_DMA_MCBSP3_RX,
  711. .dma_tx_sync = OMAP_DMA_MCBSP3_TX,
  712. .rx_irq = INT_730_McBSP2RX,
  713. .tx_irq = INT_730_McBSP2TX },
  714. };
  715. #endif
  716. #ifdef CONFIG_ARCH_OMAP15XX
  717. static const struct omap_mcbsp_info mcbsp_1510[] = {
  718. [0] = { .virt_base = OMAP1510_MCBSP1_BASE,
  719. .dma_rx_sync = OMAP_DMA_MCBSP1_RX,
  720. .dma_tx_sync = OMAP_DMA_MCBSP1_TX,
  721. .rx_irq = INT_McBSP1RX,
  722. .tx_irq = INT_McBSP1TX },
  723. [1] = { .virt_base = io_p2v(OMAP1510_MCBSP2_BASE),
  724. .dma_rx_sync = OMAP_DMA_MCBSP2_RX,
  725. .dma_tx_sync = OMAP_DMA_MCBSP2_TX,
  726. .rx_irq = INT_1510_SPI_RX,
  727. .tx_irq = INT_1510_SPI_TX },
  728. [2] = { .virt_base = OMAP1510_MCBSP3_BASE,
  729. .dma_rx_sync = OMAP_DMA_MCBSP3_RX,
  730. .dma_tx_sync = OMAP_DMA_MCBSP3_TX,
  731. .rx_irq = INT_McBSP3RX,
  732. .tx_irq = INT_McBSP3TX },
  733. };
  734. #endif
  735. #if defined(CONFIG_ARCH_OMAP16XX)
  736. static const struct omap_mcbsp_info mcbsp_1610[] = {
  737. [0] = { .virt_base = OMAP1610_MCBSP1_BASE,
  738. .dma_rx_sync = OMAP_DMA_MCBSP1_RX,
  739. .dma_tx_sync = OMAP_DMA_MCBSP1_TX,
  740. .rx_irq = INT_McBSP1RX,
  741. .tx_irq = INT_McBSP1TX },
  742. [1] = { .virt_base = io_p2v(OMAP1610_MCBSP2_BASE),
  743. .dma_rx_sync = OMAP_DMA_MCBSP2_RX,
  744. .dma_tx_sync = OMAP_DMA_MCBSP2_TX,
  745. .rx_irq = INT_1610_McBSP2_RX,
  746. .tx_irq = INT_1610_McBSP2_TX },
  747. [2] = { .virt_base = OMAP1610_MCBSP3_BASE,
  748. .dma_rx_sync = OMAP_DMA_MCBSP3_RX,
  749. .dma_tx_sync = OMAP_DMA_MCBSP3_TX,
  750. .rx_irq = INT_McBSP3RX,
  751. .tx_irq = INT_McBSP3TX },
  752. };
  753. #endif
  754. #if defined(CONFIG_ARCH_OMAP24XX)
  755. static const struct omap_mcbsp_info mcbsp_24xx[] = {
  756. [0] = { .virt_base = IO_ADDRESS(OMAP24XX_MCBSP1_BASE),
  757. .dma_rx_sync = OMAP24XX_DMA_MCBSP1_RX,
  758. .dma_tx_sync = OMAP24XX_DMA_MCBSP1_TX,
  759. .rx_irq = INT_24XX_MCBSP1_IRQ_RX,
  760. .tx_irq = INT_24XX_MCBSP1_IRQ_TX,
  761. },
  762. [1] = { .virt_base = IO_ADDRESS(OMAP24XX_MCBSP2_BASE),
  763. .dma_rx_sync = OMAP24XX_DMA_MCBSP2_RX,
  764. .dma_tx_sync = OMAP24XX_DMA_MCBSP2_TX,
  765. .rx_irq = INT_24XX_MCBSP2_IRQ_RX,
  766. .tx_irq = INT_24XX_MCBSP2_IRQ_TX,
  767. },
  768. };
  769. #endif
  770. static int __init omap_mcbsp_init(void)
  771. {
  772. int mcbsp_count = 0, i;
  773. static const struct omap_mcbsp_info *mcbsp_info;
  774. printk("Initializing OMAP McBSP system\n");
  775. #ifdef CONFIG_ARCH_OMAP1
  776. mcbsp_dsp_ck = clk_get(0, "dsp_ck");
  777. if (IS_ERR(mcbsp_dsp_ck)) {
  778. printk(KERN_ERR "mcbsp: could not acquire dsp_ck handle.\n");
  779. return PTR_ERR(mcbsp_dsp_ck);
  780. }
  781. mcbsp_api_ck = clk_get(0, "api_ck");
  782. if (IS_ERR(mcbsp_api_ck)) {
  783. printk(KERN_ERR "mcbsp: could not acquire api_ck handle.\n");
  784. return PTR_ERR(mcbsp_api_ck);
  785. }
  786. mcbsp_dspxor_ck = clk_get(0, "dspxor_ck");
  787. if (IS_ERR(mcbsp_dspxor_ck)) {
  788. printk(KERN_ERR "mcbsp: could not acquire dspxor_ck handle.\n");
  789. return PTR_ERR(mcbsp_dspxor_ck);
  790. }
  791. #endif
  792. #ifdef CONFIG_ARCH_OMAP2
  793. mcbsp1_ick = clk_get(0, "mcbsp1_ick");
  794. if (IS_ERR(mcbsp1_ick)) {
  795. printk(KERN_ERR "mcbsp: could not acquire mcbsp1_ick handle.\n");
  796. return PTR_ERR(mcbsp1_ick);
  797. }
  798. mcbsp1_fck = clk_get(0, "mcbsp1_fck");
  799. if (IS_ERR(mcbsp1_fck)) {
  800. printk(KERN_ERR "mcbsp: could not acquire mcbsp1_fck handle.\n");
  801. return PTR_ERR(mcbsp1_fck);
  802. }
  803. mcbsp2_ick = clk_get(0, "mcbsp2_ick");
  804. if (IS_ERR(mcbsp2_ick)) {
  805. printk(KERN_ERR "mcbsp: could not acquire mcbsp2_ick handle.\n");
  806. return PTR_ERR(mcbsp2_ick);
  807. }
  808. mcbsp2_fck = clk_get(0, "mcbsp2_fck");
  809. if (IS_ERR(mcbsp2_fck)) {
  810. printk(KERN_ERR "mcbsp: could not acquire mcbsp2_fck handle.\n");
  811. return PTR_ERR(mcbsp2_fck);
  812. }
  813. #endif
  814. #ifdef CONFIG_ARCH_OMAP730
  815. if (cpu_is_omap730()) {
  816. mcbsp_info = mcbsp_730;
  817. mcbsp_count = ARRAY_SIZE(mcbsp_730);
  818. }
  819. #endif
  820. #ifdef CONFIG_ARCH_OMAP15XX
  821. if (cpu_is_omap15xx()) {
  822. mcbsp_info = mcbsp_1510;
  823. mcbsp_count = ARRAY_SIZE(mcbsp_1510);
  824. }
  825. #endif
  826. #if defined(CONFIG_ARCH_OMAP16XX)
  827. if (cpu_is_omap16xx()) {
  828. mcbsp_info = mcbsp_1610;
  829. mcbsp_count = ARRAY_SIZE(mcbsp_1610);
  830. }
  831. #endif
  832. #if defined(CONFIG_ARCH_OMAP24XX)
  833. if (cpu_is_omap24xx()) {
  834. mcbsp_info = mcbsp_24xx;
  835. mcbsp_count = ARRAY_SIZE(mcbsp_24xx);
  836. omap2_mcbsp2_mux_setup();
  837. }
  838. #endif
  839. for (i = 0; i < OMAP_MAX_MCBSP_COUNT ; i++) {
  840. if (i >= mcbsp_count) {
  841. mcbsp[i].io_base = 0;
  842. mcbsp[i].free = 0;
  843. continue;
  844. }
  845. mcbsp[i].id = i + 1;
  846. mcbsp[i].free = 1;
  847. mcbsp[i].dma_tx_lch = -1;
  848. mcbsp[i].dma_rx_lch = -1;
  849. mcbsp[i].io_base = mcbsp_info[i].virt_base;
  850. mcbsp[i].io_type = OMAP_MCBSP_IRQ_IO; /* Default I/O is IRQ based */
  851. mcbsp[i].tx_irq = mcbsp_info[i].tx_irq;
  852. mcbsp[i].rx_irq = mcbsp_info[i].rx_irq;
  853. mcbsp[i].dma_rx_sync = mcbsp_info[i].dma_rx_sync;
  854. mcbsp[i].dma_tx_sync = mcbsp_info[i].dma_tx_sync;
  855. spin_lock_init(&mcbsp[i].lock);
  856. }
  857. return 0;
  858. }
  859. arch_initcall(omap_mcbsp_init);
  860. EXPORT_SYMBOL(omap_mcbsp_config);
  861. EXPORT_SYMBOL(omap_mcbsp_request);
  862. EXPORT_SYMBOL(omap_mcbsp_set_io_type);
  863. EXPORT_SYMBOL(omap_mcbsp_free);
  864. EXPORT_SYMBOL(omap_mcbsp_start);
  865. EXPORT_SYMBOL(omap_mcbsp_stop);
  866. EXPORT_SYMBOL(omap_mcbsp_xmit_word);
  867. EXPORT_SYMBOL(omap_mcbsp_recv_word);
  868. EXPORT_SYMBOL(omap_mcbsp_xmit_buffer);
  869. EXPORT_SYMBOL(omap_mcbsp_recv_buffer);
  870. EXPORT_SYMBOL(omap_mcbsp_spi_master_xmit_word_poll);
  871. EXPORT_SYMBOL(omap_mcbsp_spi_master_recv_word_poll);
  872. EXPORT_SYMBOL(omap_mcbsp_set_spi_mode);