mcbsp.c 26 KB

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