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