mcbsp.c 27 KB

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