blob: c0018b1b66735e86e0550fb6b68b21427cff8f74 [file] [log] [blame]
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001/*
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
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/device.h>
18#include <linux/wait.h>
19#include <linux/completion.h>
20#include <linux/interrupt.h>
21#include <linux/err.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000022#include <linux/clk.h>
Tony Lindgren04fbf6a2007-02-12 10:50:53 -080023#include <linux/delay.h>
Eduardo Valentinfb78d802008-07-03 12:24:39 +030024#include <linux/io.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010025
26#include <asm/arch/dma.h>
27#include <asm/arch/mux.h>
28#include <asm/arch/irqs.h>
Tony Lindgren92105bb2005-09-07 17:20:26 +010029#include <asm/arch/dsp_common.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010030#include <asm/arch/mcbsp.h>
31
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010032#ifdef CONFIG_MCBSP_DEBUG
33#define DBG(x...) printk(x)
34#else
Tony Lindgren120db2c2006-04-02 17:46:27 +010035#define DBG(x...) do { } while (0)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010036#endif
37
38struct omap_mcbsp {
39 u32 io_base;
40 u8 id;
41 u8 free;
42 omap_mcbsp_word_length rx_word_length;
43 omap_mcbsp_word_length tx_word_length;
44
Tony Lindgren120db2c2006-04-02 17:46:27 +010045 omap_mcbsp_io_type_t io_type; /* IRQ or poll */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010046 /* IRQ based TX/RX */
47 int rx_irq;
48 int tx_irq;
49
50 /* DMA stuff */
51 u8 dma_rx_sync;
52 short dma_rx_lch;
53 u8 dma_tx_sync;
54 short dma_tx_lch;
55
56 /* Completion queues */
57 struct completion tx_irq_completion;
58 struct completion rx_irq_completion;
59 struct completion tx_dma_completion;
60 struct completion rx_dma_completion;
61
Eduardo Valentinfb78d802008-07-03 12:24:39 +030062 /* Protect the field .free, while checking if the mcbsp is in use */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010063 spinlock_t lock;
64};
65
66static struct omap_mcbsp mcbsp[OMAP_MAX_MCBSP_COUNT];
Tony Lindgren120db2c2006-04-02 17:46:27 +010067#ifdef CONFIG_ARCH_OMAP1
Eduardo Valentinfb78d802008-07-03 12:24:39 +030068static struct clk *mcbsp_dsp_ck;
69static struct clk *mcbsp_api_ck;
70static struct clk *mcbsp_dspxor_ck;
Tony Lindgren120db2c2006-04-02 17:46:27 +010071#endif
72#ifdef CONFIG_ARCH_OMAP2
Eduardo Valentinfb78d802008-07-03 12:24:39 +030073static struct clk *mcbsp1_ick;
74static struct clk *mcbsp1_fck;
75static struct clk *mcbsp2_ick;
76static struct clk *mcbsp2_fck;
Tony Lindgren120db2c2006-04-02 17:46:27 +010077#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010078
79static void omap_mcbsp_dump_reg(u8 id)
80{
81 DBG("**** MCBSP%d regs ****\n", mcbsp[id].id);
82 DBG("DRR2: 0x%04x\n", OMAP_MCBSP_READ(mcbsp[id].io_base, DRR2));
83 DBG("DRR1: 0x%04x\n", OMAP_MCBSP_READ(mcbsp[id].io_base, DRR1));
84 DBG("DXR2: 0x%04x\n", OMAP_MCBSP_READ(mcbsp[id].io_base, DXR2));
85 DBG("DXR1: 0x%04x\n", OMAP_MCBSP_READ(mcbsp[id].io_base, DXR1));
86 DBG("SPCR2: 0x%04x\n", OMAP_MCBSP_READ(mcbsp[id].io_base, SPCR2));
87 DBG("SPCR1: 0x%04x\n", OMAP_MCBSP_READ(mcbsp[id].io_base, SPCR1));
88 DBG("RCR2: 0x%04x\n", OMAP_MCBSP_READ(mcbsp[id].io_base, RCR2));
89 DBG("RCR1: 0x%04x\n", OMAP_MCBSP_READ(mcbsp[id].io_base, RCR1));
90 DBG("XCR2: 0x%04x\n", OMAP_MCBSP_READ(mcbsp[id].io_base, XCR2));
91 DBG("XCR1: 0x%04x\n", OMAP_MCBSP_READ(mcbsp[id].io_base, XCR1));
92 DBG("SRGR2: 0x%04x\n", OMAP_MCBSP_READ(mcbsp[id].io_base, SRGR2));
93 DBG("SRGR1: 0x%04x\n", OMAP_MCBSP_READ(mcbsp[id].io_base, SRGR1));
94 DBG("PCR0: 0x%04x\n", OMAP_MCBSP_READ(mcbsp[id].io_base, PCR0));
95 DBG("***********************\n");
96}
97
Linus Torvalds0cd61b62006-10-06 10:53:39 -070098static irqreturn_t omap_mcbsp_tx_irq_handler(int irq, void *dev_id)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010099{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400100 struct omap_mcbsp *mcbsp_tx = dev_id;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100101
Jeff Garzike8f2af12007-10-26 05:40:25 -0400102 DBG("TX IRQ callback : 0x%x\n",
103 OMAP_MCBSP_READ(mcbsp_tx->io_base, SPCR2));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100104
105 complete(&mcbsp_tx->tx_irq_completion);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300106
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100107 return IRQ_HANDLED;
108}
109
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700110static irqreturn_t omap_mcbsp_rx_irq_handler(int irq, void *dev_id)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100111{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400112 struct omap_mcbsp *mcbsp_rx = dev_id;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100113
Jeff Garzike8f2af12007-10-26 05:40:25 -0400114 DBG("RX IRQ callback : 0x%x\n",
115 OMAP_MCBSP_READ(mcbsp_rx->io_base, SPCR2));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100116
117 complete(&mcbsp_rx->rx_irq_completion);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300118
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100119 return IRQ_HANDLED;
120}
121
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100122static void omap_mcbsp_tx_dma_callback(int lch, u16 ch_status, void *data)
123{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400124 struct omap_mcbsp *mcbsp_dma_tx = data;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100125
Jeff Garzike8f2af12007-10-26 05:40:25 -0400126 DBG("TX DMA callback : 0x%x\n",
127 OMAP_MCBSP_READ(mcbsp_dma_tx->io_base, SPCR2));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100128
129 /* We can free the channels */
130 omap_free_dma(mcbsp_dma_tx->dma_tx_lch);
131 mcbsp_dma_tx->dma_tx_lch = -1;
132
133 complete(&mcbsp_dma_tx->tx_dma_completion);
134}
135
136static void omap_mcbsp_rx_dma_callback(int lch, u16 ch_status, void *data)
137{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400138 struct omap_mcbsp *mcbsp_dma_rx = data;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100139
Jeff Garzike8f2af12007-10-26 05:40:25 -0400140 DBG("RX DMA callback : 0x%x\n",
141 OMAP_MCBSP_READ(mcbsp_dma_rx->io_base, SPCR2));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100142
143 /* We can free the channels */
144 omap_free_dma(mcbsp_dma_rx->dma_rx_lch);
145 mcbsp_dma_rx->dma_rx_lch = -1;
146
147 complete(&mcbsp_dma_rx->rx_dma_completion);
148}
149
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100150/*
151 * omap_mcbsp_config simply write a config to the
152 * appropriate McBSP.
153 * You either call this function or set the McBSP registers
154 * by yourself before calling omap_mcbsp_start().
155 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300156void omap_mcbsp_config(unsigned int id, const struct omap_mcbsp_reg_cfg *config)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100157{
158 u32 io_base = mcbsp[id].io_base;
159
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300160 DBG("OMAP-McBSP: McBSP%d io_base: 0x%8x\n", id + 1, io_base);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100161
162 /* We write the given config */
163 OMAP_MCBSP_WRITE(io_base, SPCR2, config->spcr2);
164 OMAP_MCBSP_WRITE(io_base, SPCR1, config->spcr1);
165 OMAP_MCBSP_WRITE(io_base, RCR2, config->rcr2);
166 OMAP_MCBSP_WRITE(io_base, RCR1, config->rcr1);
167 OMAP_MCBSP_WRITE(io_base, XCR2, config->xcr2);
168 OMAP_MCBSP_WRITE(io_base, XCR1, config->xcr1);
169 OMAP_MCBSP_WRITE(io_base, SRGR2, config->srgr2);
170 OMAP_MCBSP_WRITE(io_base, SRGR1, config->srgr1);
171 OMAP_MCBSP_WRITE(io_base, MCR2, config->mcr2);
172 OMAP_MCBSP_WRITE(io_base, MCR1, config->mcr1);
173 OMAP_MCBSP_WRITE(io_base, PCR0, config->pcr0);
174}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300175EXPORT_SYMBOL(omap_mcbsp_config);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100176
177static int omap_mcbsp_check(unsigned int id)
178{
179 if (cpu_is_omap730()) {
180 if (id > OMAP_MAX_MCBSP_COUNT - 1) {
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300181 printk(KERN_ERR "OMAP-McBSP: McBSP%d doesn't exist\n",
182 id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100183 return -1;
184 }
185 return 0;
186 }
187
Tony Lindgren120db2c2006-04-02 17:46:27 +0100188 if (cpu_is_omap15xx() || cpu_is_omap16xx() || cpu_is_omap24xx()) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100189 if (id > OMAP_MAX_MCBSP_COUNT) {
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300190 printk(KERN_ERR "OMAP-McBSP: McBSP%d doesn't exist\n",
191 id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100192 return -1;
193 }
194 return 0;
195 }
196
197 return -1;
198}
199
Tony Lindgren120db2c2006-04-02 17:46:27 +0100200#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100201static void omap_mcbsp_dsp_request(void)
202{
Tony Lindgren120db2c2006-04-02 17:46:27 +0100203 if (cpu_is_omap15xx() || cpu_is_omap16xx()) {
Tony Lindgren1cccd2a2007-11-29 15:38:16 -0800204 int ret;
205
206 ret = omap_dsp_request_mem();
207 if (ret < 0) {
208 printk(KERN_ERR "Could not get dsp memory: %i\n", ret);
209 return;
210 }
211
Tony Lindgren30ff7202006-01-17 15:33:51 -0800212 clk_enable(mcbsp_dsp_ck);
213 clk_enable(mcbsp_api_ck);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100214
215 /* enable 12MHz clock to mcbsp 1 & 3 */
Tony Lindgren30ff7202006-01-17 15:33:51 -0800216 clk_enable(mcbsp_dspxor_ck);
Tony Lindgren92105bb2005-09-07 17:20:26 +0100217
218 /*
219 * DSP external peripheral reset
220 * FIXME: This should be moved to dsp code
221 */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100222 __raw_writew(__raw_readw(DSP_RSTCT2) | 1 | 1 << 1,
223 DSP_RSTCT2);
224 }
225}
226
227static void omap_mcbsp_dsp_free(void)
228{
Tony Lindgren120db2c2006-04-02 17:46:27 +0100229 if (cpu_is_omap15xx() || cpu_is_omap16xx()) {
Tony Lindgren1cccd2a2007-11-29 15:38:16 -0800230 omap_dsp_release_mem();
Tony Lindgren30ff7202006-01-17 15:33:51 -0800231 clk_disable(mcbsp_dspxor_ck);
232 clk_disable(mcbsp_dsp_ck);
233 clk_disable(mcbsp_api_ck);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100234 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100235}
Tony Lindgren120db2c2006-04-02 17:46:27 +0100236#endif
237
238#ifdef CONFIG_ARCH_OMAP2
239static void omap2_mcbsp2_mux_setup(void)
240{
Syed Mohammed Khasim56a25642006-12-06 17:14:08 -0800241 if (cpu_is_omap2420()) {
242 omap_cfg_reg(Y15_24XX_MCBSP2_CLKX);
243 omap_cfg_reg(R14_24XX_MCBSP2_FSX);
244 omap_cfg_reg(W15_24XX_MCBSP2_DR);
245 omap_cfg_reg(V15_24XX_MCBSP2_DX);
246 omap_cfg_reg(V14_24XX_GPIO117);
247 }
248 /*
249 * Need to add MUX settings for OMAP 2430 SDP
250 */
Tony Lindgren120db2c2006-04-02 17:46:27 +0100251}
252#endif
253
254/*
255 * We can choose between IRQ based or polled IO.
256 * This needs to be called before omap_mcbsp_request().
257 */
258int omap_mcbsp_set_io_type(unsigned int id, omap_mcbsp_io_type_t io_type)
259{
260 if (omap_mcbsp_check(id) < 0)
261 return -EINVAL;
262
263 spin_lock(&mcbsp[id].lock);
264
265 if (!mcbsp[id].free) {
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300266 printk(KERN_ERR "OMAP-McBSP: McBSP%d is currently in use\n",
267 id + 1);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100268 spin_unlock(&mcbsp[id].lock);
269 return -EINVAL;
270 }
271
272 mcbsp[id].io_type = io_type;
273
274 spin_unlock(&mcbsp[id].lock);
275
276 return 0;
277}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300278EXPORT_SYMBOL(omap_mcbsp_set_io_type);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100279
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100280int omap_mcbsp_request(unsigned int id)
281{
282 int err;
283
284 if (omap_mcbsp_check(id) < 0)
285 return -EINVAL;
286
Tony Lindgren120db2c2006-04-02 17:46:27 +0100287#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100288 /*
289 * On 1510, 1610 and 1710, McBSP1 and McBSP3
290 * are DSP public peripherals.
291 */
292 if (id == OMAP_MCBSP1 || id == OMAP_MCBSP3)
293 omap_mcbsp_dsp_request();
Tony Lindgren120db2c2006-04-02 17:46:27 +0100294#endif
295
296#ifdef CONFIG_ARCH_OMAP2
297 if (cpu_is_omap24xx()) {
298 if (id == OMAP_MCBSP1) {
299 clk_enable(mcbsp1_ick);
300 clk_enable(mcbsp1_fck);
301 } else {
302 clk_enable(mcbsp2_ick);
303 clk_enable(mcbsp2_fck);
304 }
305 }
306#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100307
308 spin_lock(&mcbsp[id].lock);
309 if (!mcbsp[id].free) {
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300310 printk(KERN_ERR "OMAP-McBSP: McBSP%d is currently in use\n",
311 id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100312 spin_unlock(&mcbsp[id].lock);
313 return -1;
314 }
315
316 mcbsp[id].free = 0;
317 spin_unlock(&mcbsp[id].lock);
318
Tony Lindgren120db2c2006-04-02 17:46:27 +0100319 if (mcbsp[id].io_type == OMAP_MCBSP_IRQ_IO) {
320 /* We need to get IRQs here */
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300321 err = request_irq(mcbsp[id].tx_irq, omap_mcbsp_tx_irq_handler,
322 0, "McBSP", (void *) (&mcbsp[id]));
Tony Lindgren120db2c2006-04-02 17:46:27 +0100323 if (err != 0) {
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300324 printk(KERN_ERR "OMAP-McBSP: Unable to "
325 "request TX IRQ %d for McBSP%d\n",
326 mcbsp[id].tx_irq, mcbsp[id].id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100327 return err;
328 }
329
330 init_completion(&(mcbsp[id].tx_irq_completion));
331
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300332 err = request_irq(mcbsp[id].rx_irq, omap_mcbsp_rx_irq_handler,
333 0, "McBSP", (void *) (&mcbsp[id]));
Tony Lindgren120db2c2006-04-02 17:46:27 +0100334 if (err != 0) {
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300335 printk(KERN_ERR "OMAP-McBSP: Unable to "
336 "request RX IRQ %d for McBSP%d\n",
337 mcbsp[id].rx_irq, mcbsp[id].id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100338 free_irq(mcbsp[id].tx_irq, (void *) (&mcbsp[id]));
339 return err;
340 }
341
342 init_completion(&(mcbsp[id].rx_irq_completion));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100343 }
344
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100345 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100346}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300347EXPORT_SYMBOL(omap_mcbsp_request);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100348
349void omap_mcbsp_free(unsigned int id)
350{
351 if (omap_mcbsp_check(id) < 0)
352 return;
353
Tony Lindgren120db2c2006-04-02 17:46:27 +0100354#ifdef CONFIG_ARCH_OMAP1
355 if (cpu_class_is_omap1()) {
356 if (id == OMAP_MCBSP1 || id == OMAP_MCBSP3)
357 omap_mcbsp_dsp_free();
358 }
359#endif
360
361#ifdef CONFIG_ARCH_OMAP2
362 if (cpu_is_omap24xx()) {
363 if (id == OMAP_MCBSP1) {
364 clk_disable(mcbsp1_ick);
365 clk_disable(mcbsp1_fck);
366 } else {
367 clk_disable(mcbsp2_ick);
368 clk_disable(mcbsp2_fck);
369 }
370 }
371#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100372
373 spin_lock(&mcbsp[id].lock);
374 if (mcbsp[id].free) {
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300375 printk(KERN_ERR "OMAP-McBSP: McBSP%d was not reserved\n",
376 id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100377 spin_unlock(&mcbsp[id].lock);
378 return;
379 }
380
381 mcbsp[id].free = 1;
382 spin_unlock(&mcbsp[id].lock);
383
Tony Lindgren120db2c2006-04-02 17:46:27 +0100384 if (mcbsp[id].io_type == OMAP_MCBSP_IRQ_IO) {
385 /* Free IRQs */
386 free_irq(mcbsp[id].rx_irq, (void *) (&mcbsp[id]));
387 free_irq(mcbsp[id].tx_irq, (void *) (&mcbsp[id]));
388 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100389}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300390EXPORT_SYMBOL(omap_mcbsp_free);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100391
392/*
393 * Here we start the McBSP, by enabling the sample
394 * generator, both transmitter and receivers,
395 * and the frame sync.
396 */
397void omap_mcbsp_start(unsigned int id)
398{
399 u32 io_base;
400 u16 w;
401
402 if (omap_mcbsp_check(id) < 0)
403 return;
404
405 io_base = mcbsp[id].io_base;
406
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300407 mcbsp[id].rx_word_length = (OMAP_MCBSP_READ(io_base, RCR1) >> 5) & 0x7;
408 mcbsp[id].tx_word_length = (OMAP_MCBSP_READ(io_base, XCR1) >> 5) & 0x7;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100409
410 /* Start the sample generator */
411 w = OMAP_MCBSP_READ(io_base, SPCR2);
412 OMAP_MCBSP_WRITE(io_base, SPCR2, w | (1 << 6));
413
414 /* Enable transmitter and receiver */
415 w = OMAP_MCBSP_READ(io_base, SPCR2);
416 OMAP_MCBSP_WRITE(io_base, SPCR2, w | 1);
417
418 w = OMAP_MCBSP_READ(io_base, SPCR1);
419 OMAP_MCBSP_WRITE(io_base, SPCR1, w | 1);
420
421 udelay(100);
422
423 /* Start frame sync */
424 w = OMAP_MCBSP_READ(io_base, SPCR2);
425 OMAP_MCBSP_WRITE(io_base, SPCR2, w | (1 << 7));
426
427 /* Dump McBSP Regs */
428 omap_mcbsp_dump_reg(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100429}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300430EXPORT_SYMBOL(omap_mcbsp_start);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100431
432void omap_mcbsp_stop(unsigned int id)
433{
434 u32 io_base;
435 u16 w;
436
437 if (omap_mcbsp_check(id) < 0)
438 return;
439
440 io_base = mcbsp[id].io_base;
441
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300442 /* Reset transmitter */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100443 w = OMAP_MCBSP_READ(io_base, SPCR2);
444 OMAP_MCBSP_WRITE(io_base, SPCR2, w & ~(1));
445
446 /* Reset receiver */
447 w = OMAP_MCBSP_READ(io_base, SPCR1);
448 OMAP_MCBSP_WRITE(io_base, SPCR1, w & ~(1));
449
450 /* Reset the sample rate generator */
451 w = OMAP_MCBSP_READ(io_base, SPCR2);
452 OMAP_MCBSP_WRITE(io_base, SPCR2, w & ~(1 << 6));
453}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300454EXPORT_SYMBOL(omap_mcbsp_stop);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100455
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100456/* polled mcbsp i/o operations */
457int omap_mcbsp_pollwrite(unsigned int id, u16 buf)
458{
459 u32 base = mcbsp[id].io_base;
460 writew(buf, base + OMAP_MCBSP_REG_DXR1);
461 /* if frame sync error - clear the error */
462 if (readw(base + OMAP_MCBSP_REG_SPCR2) & XSYNC_ERR) {
463 /* clear error */
464 writew(readw(base + OMAP_MCBSP_REG_SPCR2) & (~XSYNC_ERR),
465 base + OMAP_MCBSP_REG_SPCR2);
466 /* resend */
467 return -1;
468 } else {
469 /* wait for transmit confirmation */
470 int attemps = 0;
471 while (!(readw(base + OMAP_MCBSP_REG_SPCR2) & XRDY)) {
472 if (attemps++ > 1000) {
473 writew(readw(base + OMAP_MCBSP_REG_SPCR2) &
474 (~XRST),
475 base + OMAP_MCBSP_REG_SPCR2);
476 udelay(10);
477 writew(readw(base + OMAP_MCBSP_REG_SPCR2) |
478 (XRST),
479 base + OMAP_MCBSP_REG_SPCR2);
480 udelay(10);
481 printk(KERN_ERR
482 " Could not write to McBSP Register\n");
483 return -2;
484 }
485 }
486 }
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300487
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100488 return 0;
489}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300490EXPORT_SYMBOL(omap_mcbsp_pollwrite);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100491
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300492int omap_mcbsp_pollread(unsigned int id, u16 *buf)
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100493{
494 u32 base = mcbsp[id].io_base;
495 /* if frame sync error - clear the error */
496 if (readw(base + OMAP_MCBSP_REG_SPCR1) & RSYNC_ERR) {
497 /* clear error */
498 writew(readw(base + OMAP_MCBSP_REG_SPCR1) & (~RSYNC_ERR),
499 base + OMAP_MCBSP_REG_SPCR1);
500 /* resend */
501 return -1;
502 } else {
503 /* wait for recieve confirmation */
504 int attemps = 0;
505 while (!(readw(base + OMAP_MCBSP_REG_SPCR1) & RRDY)) {
506 if (attemps++ > 1000) {
507 writew(readw(base + OMAP_MCBSP_REG_SPCR1) &
508 (~RRST),
509 base + OMAP_MCBSP_REG_SPCR1);
510 udelay(10);
511 writew(readw(base + OMAP_MCBSP_REG_SPCR1) |
512 (RRST),
513 base + OMAP_MCBSP_REG_SPCR1);
514 udelay(10);
515 printk(KERN_ERR
516 " Could not read from McBSP Register\n");
517 return -2;
518 }
519 }
520 }
521 *buf = readw(base + OMAP_MCBSP_REG_DRR1);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300522
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100523 return 0;
524}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300525EXPORT_SYMBOL(omap_mcbsp_pollread);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100526
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100527/*
528 * IRQ based word transmission.
529 */
530void omap_mcbsp_xmit_word(unsigned int id, u32 word)
531{
532 u32 io_base;
533 omap_mcbsp_word_length word_length = mcbsp[id].tx_word_length;
534
535 if (omap_mcbsp_check(id) < 0)
536 return;
537
538 io_base = mcbsp[id].io_base;
539
540 wait_for_completion(&(mcbsp[id].tx_irq_completion));
541
542 if (word_length > OMAP_MCBSP_WORD_16)
543 OMAP_MCBSP_WRITE(io_base, DXR2, word >> 16);
544 OMAP_MCBSP_WRITE(io_base, DXR1, word & 0xffff);
545}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300546EXPORT_SYMBOL(omap_mcbsp_xmit_word);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100547
548u32 omap_mcbsp_recv_word(unsigned int id)
549{
550 u32 io_base;
551 u16 word_lsb, word_msb = 0;
552 omap_mcbsp_word_length word_length = mcbsp[id].rx_word_length;
553
554 if (omap_mcbsp_check(id) < 0)
555 return -EINVAL;
556
557 io_base = mcbsp[id].io_base;
558
559 wait_for_completion(&(mcbsp[id].rx_irq_completion));
560
561 if (word_length > OMAP_MCBSP_WORD_16)
562 word_msb = OMAP_MCBSP_READ(io_base, DRR2);
563 word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
564
565 return (word_lsb | (word_msb << 16));
566}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300567EXPORT_SYMBOL(omap_mcbsp_recv_word);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100568
Tony Lindgren120db2c2006-04-02 17:46:27 +0100569int omap_mcbsp_spi_master_xmit_word_poll(unsigned int id, u32 word)
570{
571 u32 io_base = mcbsp[id].io_base;
572 omap_mcbsp_word_length tx_word_length = mcbsp[id].tx_word_length;
573 omap_mcbsp_word_length rx_word_length = mcbsp[id].rx_word_length;
574 u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
575
576 if (tx_word_length != rx_word_length)
577 return -EINVAL;
578
579 /* First we wait for the transmitter to be ready */
580 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
581 while (!(spcr2 & XRDY)) {
582 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
583 if (attempts++ > 1000) {
584 /* We must reset the transmitter */
585 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 & (~XRST));
586 udelay(10);
587 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 | XRST);
588 udelay(10);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300589 printk(KERN_ERR "McBSP transmitter not ready\n");
Tony Lindgren120db2c2006-04-02 17:46:27 +0100590 return -EAGAIN;
591 }
592 }
593
594 /* Now we can push the data */
595 if (tx_word_length > OMAP_MCBSP_WORD_16)
596 OMAP_MCBSP_WRITE(io_base, DXR2, word >> 16);
597 OMAP_MCBSP_WRITE(io_base, DXR1, word & 0xffff);
598
599 /* We wait for the receiver to be ready */
600 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
601 while (!(spcr1 & RRDY)) {
602 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
603 if (attempts++ > 1000) {
604 /* We must reset the receiver */
605 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 & (~RRST));
606 udelay(10);
607 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 | RRST);
608 udelay(10);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300609 printk(KERN_ERR "McBSP receiver not ready\n");
Tony Lindgren120db2c2006-04-02 17:46:27 +0100610 return -EAGAIN;
611 }
612 }
613
614 /* Receiver is ready, let's read the dummy data */
615 if (rx_word_length > OMAP_MCBSP_WORD_16)
616 word_msb = OMAP_MCBSP_READ(io_base, DRR2);
617 word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
618
619 return 0;
620}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300621EXPORT_SYMBOL(omap_mcbsp_spi_master_xmit_word_poll);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100622
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300623int omap_mcbsp_spi_master_recv_word_poll(unsigned int id, u32 *word)
Tony Lindgren120db2c2006-04-02 17:46:27 +0100624{
625 u32 io_base = mcbsp[id].io_base, clock_word = 0;
626 omap_mcbsp_word_length tx_word_length = mcbsp[id].tx_word_length;
627 omap_mcbsp_word_length rx_word_length = mcbsp[id].rx_word_length;
628 u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
629
630 if (tx_word_length != rx_word_length)
631 return -EINVAL;
632
633 /* First we wait for the transmitter to be ready */
634 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
635 while (!(spcr2 & XRDY)) {
636 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
637 if (attempts++ > 1000) {
638 /* We must reset the transmitter */
639 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 & (~XRST));
640 udelay(10);
641 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 | XRST);
642 udelay(10);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300643 printk(KERN_ERR "McBSP transmitter not ready\n");
Tony Lindgren120db2c2006-04-02 17:46:27 +0100644 return -EAGAIN;
645 }
646 }
647
648 /* We first need to enable the bus clock */
649 if (tx_word_length > OMAP_MCBSP_WORD_16)
650 OMAP_MCBSP_WRITE(io_base, DXR2, clock_word >> 16);
651 OMAP_MCBSP_WRITE(io_base, DXR1, clock_word & 0xffff);
652
653 /* We wait for the receiver to be ready */
654 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
655 while (!(spcr1 & RRDY)) {
656 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
657 if (attempts++ > 1000) {
658 /* We must reset the receiver */
659 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 & (~RRST));
660 udelay(10);
661 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 | RRST);
662 udelay(10);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300663 printk(KERN_ERR "McBSP receiver not ready\n");
Tony Lindgren120db2c2006-04-02 17:46:27 +0100664 return -EAGAIN;
665 }
666 }
667
668 /* Receiver is ready, there is something for us */
669 if (rx_word_length > OMAP_MCBSP_WORD_16)
670 word_msb = OMAP_MCBSP_READ(io_base, DRR2);
671 word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
672
673 word[0] = (word_lsb | (word_msb << 16));
674
675 return 0;
676}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300677EXPORT_SYMBOL(omap_mcbsp_spi_master_recv_word_poll);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100678
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100679/*
680 * Simple DMA based buffer rx/tx routines.
681 * Nothing fancy, just a single buffer tx/rx through DMA.
682 * The DMA resources are released once the transfer is done.
683 * For anything fancier, you should use your own customized DMA
684 * routines and callbacks.
685 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300686int omap_mcbsp_xmit_buffer(unsigned int id, dma_addr_t buffer,
687 unsigned int length)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100688{
689 int dma_tx_ch;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100690 int src_port = 0;
691 int dest_port = 0;
692 int sync_dev = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100693
694 if (omap_mcbsp_check(id) < 0)
695 return -EINVAL;
696
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300697 if (omap_request_dma(mcbsp[id].dma_tx_sync, "McBSP TX",
698 omap_mcbsp_tx_dma_callback,
699 &mcbsp[id],
700 &dma_tx_ch)) {
701 printk(KERN_ERR "OMAP-McBSP: Unable to request DMA channel for"
702 " McBSP%d TX. Trying IRQ based TX\n", id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100703 return -EAGAIN;
704 }
705 mcbsp[id].dma_tx_lch = dma_tx_ch;
706
707 DBG("TX DMA on channel %d\n", dma_tx_ch);
708
709 init_completion(&(mcbsp[id].tx_dma_completion));
710
Tony Lindgren120db2c2006-04-02 17:46:27 +0100711 if (cpu_class_is_omap1()) {
712 src_port = OMAP_DMA_PORT_TIPB;
713 dest_port = OMAP_DMA_PORT_EMIFF;
714 }
715 if (cpu_is_omap24xx())
716 sync_dev = mcbsp[id].dma_tx_sync;
717
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100718 omap_set_dma_transfer_params(mcbsp[id].dma_tx_lch,
719 OMAP_DMA_DATA_TYPE_S16,
720 length >> 1, 1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000721 OMAP_DMA_SYNC_ELEMENT,
Tony Lindgren120db2c2006-04-02 17:46:27 +0100722 sync_dev, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100723
724 omap_set_dma_dest_params(mcbsp[id].dma_tx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +0100725 src_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100726 OMAP_DMA_AMODE_CONSTANT,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000727 mcbsp[id].io_base + OMAP_MCBSP_REG_DXR1,
728 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100729
730 omap_set_dma_src_params(mcbsp[id].dma_tx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +0100731 dest_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100732 OMAP_DMA_AMODE_POST_INC,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000733 buffer,
734 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100735
736 omap_start_dma(mcbsp[id].dma_tx_lch);
737 wait_for_completion(&(mcbsp[id].tx_dma_completion));
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300738
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100739 return 0;
740}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300741EXPORT_SYMBOL(omap_mcbsp_xmit_buffer);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100742
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300743int omap_mcbsp_recv_buffer(unsigned int id, dma_addr_t buffer,
744 unsigned int length)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100745{
746 int dma_rx_ch;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100747 int src_port = 0;
748 int dest_port = 0;
749 int sync_dev = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100750
751 if (omap_mcbsp_check(id) < 0)
752 return -EINVAL;
753
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300754 if (omap_request_dma(mcbsp[id].dma_rx_sync, "McBSP RX",
755 omap_mcbsp_rx_dma_callback,
756 &mcbsp[id],
757 &dma_rx_ch)) {
758 printk(KERN_ERR "Unable to request DMA channel for McBSP%d RX."
759 " Trying IRQ based RX\n", id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100760 return -EAGAIN;
761 }
762 mcbsp[id].dma_rx_lch = dma_rx_ch;
763
764 DBG("RX DMA on channel %d\n", dma_rx_ch);
765
766 init_completion(&(mcbsp[id].rx_dma_completion));
767
Tony Lindgren120db2c2006-04-02 17:46:27 +0100768 if (cpu_class_is_omap1()) {
769 src_port = OMAP_DMA_PORT_TIPB;
770 dest_port = OMAP_DMA_PORT_EMIFF;
771 }
772 if (cpu_is_omap24xx())
773 sync_dev = mcbsp[id].dma_rx_sync;
774
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100775 omap_set_dma_transfer_params(mcbsp[id].dma_rx_lch,
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300776 OMAP_DMA_DATA_TYPE_S16,
777 length >> 1, 1,
778 OMAP_DMA_SYNC_ELEMENT,
779 sync_dev, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100780
781 omap_set_dma_src_params(mcbsp[id].dma_rx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +0100782 src_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100783 OMAP_DMA_AMODE_CONSTANT,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000784 mcbsp[id].io_base + OMAP_MCBSP_REG_DRR1,
785 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100786
787 omap_set_dma_dest_params(mcbsp[id].dma_rx_lch,
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300788 dest_port,
789 OMAP_DMA_AMODE_POST_INC,
790 buffer,
791 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100792
793 omap_start_dma(mcbsp[id].dma_rx_lch);
794 wait_for_completion(&(mcbsp[id].rx_dma_completion));
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300795
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100796 return 0;
797}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300798EXPORT_SYMBOL(omap_mcbsp_recv_buffer);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100799
800/*
801 * SPI wrapper.
802 * Since SPI setup is much simpler than the generic McBSP one,
803 * this wrapper just need an omap_mcbsp_spi_cfg structure as an input.
804 * Once this is done, you can call omap_mcbsp_start().
805 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300806void omap_mcbsp_set_spi_mode(unsigned int id,
807 const struct omap_mcbsp_spi_cfg *spi_cfg)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100808{
809 struct omap_mcbsp_reg_cfg mcbsp_cfg;
810
811 if (omap_mcbsp_check(id) < 0)
812 return;
813
814 memset(&mcbsp_cfg, 0, sizeof(struct omap_mcbsp_reg_cfg));
815
816 /* SPI has only one frame */
817 mcbsp_cfg.rcr1 |= (RWDLEN1(spi_cfg->word_length) | RFRLEN1(0));
818 mcbsp_cfg.xcr1 |= (XWDLEN1(spi_cfg->word_length) | XFRLEN1(0));
819
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300820 /* Clock stop mode */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100821 if (spi_cfg->clk_stp_mode == OMAP_MCBSP_CLK_STP_MODE_NO_DELAY)
822 mcbsp_cfg.spcr1 |= (1 << 12);
823 else
824 mcbsp_cfg.spcr1 |= (3 << 11);
825
826 /* Set clock parities */
827 if (spi_cfg->rx_clock_polarity == OMAP_MCBSP_CLK_RISING)
828 mcbsp_cfg.pcr0 |= CLKRP;
829 else
830 mcbsp_cfg.pcr0 &= ~CLKRP;
831
832 if (spi_cfg->tx_clock_polarity == OMAP_MCBSP_CLK_RISING)
833 mcbsp_cfg.pcr0 &= ~CLKXP;
834 else
835 mcbsp_cfg.pcr0 |= CLKXP;
836
837 /* Set SCLKME to 0 and CLKSM to 1 */
838 mcbsp_cfg.pcr0 &= ~SCLKME;
839 mcbsp_cfg.srgr2 |= CLKSM;
840
841 /* Set FSXP */
842 if (spi_cfg->fsx_polarity == OMAP_MCBSP_FS_ACTIVE_HIGH)
843 mcbsp_cfg.pcr0 &= ~FSXP;
844 else
845 mcbsp_cfg.pcr0 |= FSXP;
846
847 if (spi_cfg->spi_mode == OMAP_MCBSP_SPI_MASTER) {
848 mcbsp_cfg.pcr0 |= CLKXM;
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300849 mcbsp_cfg.srgr1 |= CLKGDV(spi_cfg->clk_div - 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100850 mcbsp_cfg.pcr0 |= FSXM;
851 mcbsp_cfg.srgr2 &= ~FSGM;
852 mcbsp_cfg.xcr2 |= XDATDLY(1);
853 mcbsp_cfg.rcr2 |= RDATDLY(1);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300854 } else {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100855 mcbsp_cfg.pcr0 &= ~CLKXM;
856 mcbsp_cfg.srgr1 |= CLKGDV(1);
857 mcbsp_cfg.pcr0 &= ~FSXM;
858 mcbsp_cfg.xcr2 &= ~XDATDLY(3);
859 mcbsp_cfg.rcr2 &= ~RDATDLY(3);
860 }
861
862 mcbsp_cfg.xcr2 &= ~XPHASE;
863 mcbsp_cfg.rcr2 &= ~RPHASE;
864
865 omap_mcbsp_config(id, &mcbsp_cfg);
866}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300867EXPORT_SYMBOL(omap_mcbsp_set_spi_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100868
869/*
870 * McBSP1 and McBSP3 are directly mapped on 1610 and 1510.
871 * 730 has only 2 McBSP, and both of them are MPU peripherals.
872 */
873struct omap_mcbsp_info {
874 u32 virt_base;
875 u8 dma_rx_sync, dma_tx_sync;
876 u16 rx_irq, tx_irq;
877};
878
879#ifdef CONFIG_ARCH_OMAP730
880static const struct omap_mcbsp_info mcbsp_730[] = {
881 [0] = { .virt_base = io_p2v(OMAP730_MCBSP1_BASE),
882 .dma_rx_sync = OMAP_DMA_MCBSP1_RX,
883 .dma_tx_sync = OMAP_DMA_MCBSP1_TX,
884 .rx_irq = INT_730_McBSP1RX,
885 .tx_irq = INT_730_McBSP1TX },
886 [1] = { .virt_base = io_p2v(OMAP730_MCBSP2_BASE),
887 .dma_rx_sync = OMAP_DMA_MCBSP3_RX,
888 .dma_tx_sync = OMAP_DMA_MCBSP3_TX,
889 .rx_irq = INT_730_McBSP2RX,
890 .tx_irq = INT_730_McBSP2TX },
891};
892#endif
893
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000894#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100895static const struct omap_mcbsp_info mcbsp_1510[] = {
896 [0] = { .virt_base = OMAP1510_MCBSP1_BASE,
897 .dma_rx_sync = OMAP_DMA_MCBSP1_RX,
898 .dma_tx_sync = OMAP_DMA_MCBSP1_TX,
899 .rx_irq = INT_McBSP1RX,
900 .tx_irq = INT_McBSP1TX },
901 [1] = { .virt_base = io_p2v(OMAP1510_MCBSP2_BASE),
902 .dma_rx_sync = OMAP_DMA_MCBSP2_RX,
903 .dma_tx_sync = OMAP_DMA_MCBSP2_TX,
904 .rx_irq = INT_1510_SPI_RX,
905 .tx_irq = INT_1510_SPI_TX },
906 [2] = { .virt_base = OMAP1510_MCBSP3_BASE,
907 .dma_rx_sync = OMAP_DMA_MCBSP3_RX,
908 .dma_tx_sync = OMAP_DMA_MCBSP3_TX,
909 .rx_irq = INT_McBSP3RX,
910 .tx_irq = INT_McBSP3TX },
911};
912#endif
913
914#if defined(CONFIG_ARCH_OMAP16XX)
915static const struct omap_mcbsp_info mcbsp_1610[] = {
916 [0] = { .virt_base = OMAP1610_MCBSP1_BASE,
917 .dma_rx_sync = OMAP_DMA_MCBSP1_RX,
918 .dma_tx_sync = OMAP_DMA_MCBSP1_TX,
919 .rx_irq = INT_McBSP1RX,
920 .tx_irq = INT_McBSP1TX },
921 [1] = { .virt_base = io_p2v(OMAP1610_MCBSP2_BASE),
922 .dma_rx_sync = OMAP_DMA_MCBSP2_RX,
923 .dma_tx_sync = OMAP_DMA_MCBSP2_TX,
924 .rx_irq = INT_1610_McBSP2_RX,
925 .tx_irq = INT_1610_McBSP2_TX },
926 [2] = { .virt_base = OMAP1610_MCBSP3_BASE,
927 .dma_rx_sync = OMAP_DMA_MCBSP3_RX,
928 .dma_tx_sync = OMAP_DMA_MCBSP3_TX,
929 .rx_irq = INT_McBSP3RX,
930 .tx_irq = INT_McBSP3TX },
931};
932#endif
933
Tony Lindgren120db2c2006-04-02 17:46:27 +0100934#if defined(CONFIG_ARCH_OMAP24XX)
935static const struct omap_mcbsp_info mcbsp_24xx[] = {
936 [0] = { .virt_base = IO_ADDRESS(OMAP24XX_MCBSP1_BASE),
937 .dma_rx_sync = OMAP24XX_DMA_MCBSP1_RX,
938 .dma_tx_sync = OMAP24XX_DMA_MCBSP1_TX,
939 .rx_irq = INT_24XX_MCBSP1_IRQ_RX,
940 .tx_irq = INT_24XX_MCBSP1_IRQ_TX,
941 },
942 [1] = { .virt_base = IO_ADDRESS(OMAP24XX_MCBSP2_BASE),
943 .dma_rx_sync = OMAP24XX_DMA_MCBSP2_RX,
944 .dma_tx_sync = OMAP24XX_DMA_MCBSP2_TX,
945 .rx_irq = INT_24XX_MCBSP2_IRQ_RX,
946 .tx_irq = INT_24XX_MCBSP2_IRQ_TX,
947 },
948};
949#endif
950
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100951static int __init omap_mcbsp_init(void)
952{
953 int mcbsp_count = 0, i;
954 static const struct omap_mcbsp_info *mcbsp_info;
955
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300956 printk(KERN_INFO "Initializing OMAP McBSP system\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100957
Tony Lindgren120db2c2006-04-02 17:46:27 +0100958#ifdef CONFIG_ARCH_OMAP1
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100959 mcbsp_dsp_ck = clk_get(0, "dsp_ck");
960 if (IS_ERR(mcbsp_dsp_ck)) {
961 printk(KERN_ERR "mcbsp: could not acquire dsp_ck handle.\n");
962 return PTR_ERR(mcbsp_dsp_ck);
963 }
964 mcbsp_api_ck = clk_get(0, "api_ck");
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100965 if (IS_ERR(mcbsp_api_ck)) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100966 printk(KERN_ERR "mcbsp: could not acquire api_ck handle.\n");
967 return PTR_ERR(mcbsp_api_ck);
968 }
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100969 mcbsp_dspxor_ck = clk_get(0, "dspxor_ck");
970 if (IS_ERR(mcbsp_dspxor_ck)) {
971 printk(KERN_ERR "mcbsp: could not acquire dspxor_ck handle.\n");
972 return PTR_ERR(mcbsp_dspxor_ck);
973 }
Tony Lindgren120db2c2006-04-02 17:46:27 +0100974#endif
975#ifdef CONFIG_ARCH_OMAP2
976 mcbsp1_ick = clk_get(0, "mcbsp1_ick");
977 if (IS_ERR(mcbsp1_ick)) {
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300978 printk(KERN_ERR "mcbsp: could not acquire "
979 "mcbsp1_ick handle.\n");
Tony Lindgren120db2c2006-04-02 17:46:27 +0100980 return PTR_ERR(mcbsp1_ick);
981 }
982 mcbsp1_fck = clk_get(0, "mcbsp1_fck");
983 if (IS_ERR(mcbsp1_fck)) {
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300984 printk(KERN_ERR "mcbsp: could not acquire "
985 "mcbsp1_fck handle.\n");
Tony Lindgren120db2c2006-04-02 17:46:27 +0100986 return PTR_ERR(mcbsp1_fck);
987 }
988 mcbsp2_ick = clk_get(0, "mcbsp2_ick");
989 if (IS_ERR(mcbsp2_ick)) {
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300990 printk(KERN_ERR "mcbsp: could not acquire "
991 "mcbsp2_ick handle.\n");
Tony Lindgren120db2c2006-04-02 17:46:27 +0100992 return PTR_ERR(mcbsp2_ick);
993 }
994 mcbsp2_fck = clk_get(0, "mcbsp2_fck");
995 if (IS_ERR(mcbsp2_fck)) {
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300996 printk(KERN_ERR "mcbsp: could not acquire "
997 "mcbsp2_fck handle.\n");
Tony Lindgren120db2c2006-04-02 17:46:27 +0100998 return PTR_ERR(mcbsp2_fck);
999 }
1000#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001001
1002#ifdef CONFIG_ARCH_OMAP730
1003 if (cpu_is_omap730()) {
1004 mcbsp_info = mcbsp_730;
1005 mcbsp_count = ARRAY_SIZE(mcbsp_730);
1006 }
1007#endif
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00001008#ifdef CONFIG_ARCH_OMAP15XX
Tony Lindgren120db2c2006-04-02 17:46:27 +01001009 if (cpu_is_omap15xx()) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001010 mcbsp_info = mcbsp_1510;
1011 mcbsp_count = ARRAY_SIZE(mcbsp_1510);
1012 }
1013#endif
1014#if defined(CONFIG_ARCH_OMAP16XX)
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +01001015 if (cpu_is_omap16xx()) {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001016 mcbsp_info = mcbsp_1610;
1017 mcbsp_count = ARRAY_SIZE(mcbsp_1610);
1018 }
1019#endif
Tony Lindgren120db2c2006-04-02 17:46:27 +01001020#if defined(CONFIG_ARCH_OMAP24XX)
1021 if (cpu_is_omap24xx()) {
1022 mcbsp_info = mcbsp_24xx;
1023 mcbsp_count = ARRAY_SIZE(mcbsp_24xx);
Tony Lindgren120db2c2006-04-02 17:46:27 +01001024 omap2_mcbsp2_mux_setup();
Tony Lindgren120db2c2006-04-02 17:46:27 +01001025 }
1026#endif
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001027 for (i = 0; i < OMAP_MAX_MCBSP_COUNT ; i++) {
1028 if (i >= mcbsp_count) {
1029 mcbsp[i].io_base = 0;
1030 mcbsp[i].free = 0;
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001031 continue;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001032 }
1033 mcbsp[i].id = i + 1;
1034 mcbsp[i].free = 1;
1035 mcbsp[i].dma_tx_lch = -1;
1036 mcbsp[i].dma_rx_lch = -1;
1037
1038 mcbsp[i].io_base = mcbsp_info[i].virt_base;
Eduardo Valentinfb78d802008-07-03 12:24:39 +03001039 /* Default I/O is IRQ based */
1040 mcbsp[i].io_type = OMAP_MCBSP_IRQ_IO;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001041 mcbsp[i].tx_irq = mcbsp_info[i].tx_irq;
1042 mcbsp[i].rx_irq = mcbsp_info[i].rx_irq;
1043 mcbsp[i].dma_rx_sync = mcbsp_info[i].dma_rx_sync;
1044 mcbsp[i].dma_tx_sync = mcbsp_info[i].dma_tx_sync;
1045 spin_lock_init(&mcbsp[i].lock);
1046 }
1047
1048 return 0;
1049}
1050
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001051arch_initcall(omap_mcbsp_init);