blob: af33fc713e1a325d7e5179d8fa20921ebc1e152d [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>
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +030018#include <linux/platform_device.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010019#include <linux/wait.h>
20#include <linux/completion.h>
21#include <linux/interrupt.h>
22#include <linux/err.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000023#include <linux/clk.h>
Tony Lindgren04fbf6a2007-02-12 10:50:53 -080024#include <linux/delay.h>
Eduardo Valentinfb78d802008-07-03 12:24:39 +030025#include <linux/io.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010026
Russell Kinga09e64f2008-08-05 16:14:15 +010027#include <mach/dma.h>
28#include <mach/mcbsp.h>
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010029
Chandra Shekharb4b58f52008-10-08 10:01:39 +030030struct omap_mcbsp **mcbsp_ptr;
31int omap_mcbsp_count;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +030032
Chandra Shekharb4b58f52008-10-08 10:01:39 +030033void omap_mcbsp_write(void __iomem *io_base, u16 reg, u32 val)
34{
35 if (cpu_class_is_omap1() || cpu_is_omap2420())
36 __raw_writew((u16)val, io_base + reg);
37 else
38 __raw_writel(val, io_base + reg);
39}
40
41int omap_mcbsp_read(void __iomem *io_base, u16 reg)
42{
43 if (cpu_class_is_omap1() || cpu_is_omap2420())
44 return __raw_readw(io_base + reg);
45 else
46 return __raw_readl(io_base + reg);
47}
48
49#define OMAP_MCBSP_READ(base, reg) \
50 omap_mcbsp_read(base, OMAP_MCBSP_REG_##reg)
51#define OMAP_MCBSP_WRITE(base, reg, val) \
52 omap_mcbsp_write(base, OMAP_MCBSP_REG_##reg, val)
53
54#define omap_mcbsp_check_valid_id(id) (id < omap_mcbsp_count)
55#define id_to_mcbsp_ptr(id) mcbsp_ptr[id];
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010056
57static void omap_mcbsp_dump_reg(u8 id)
58{
Chandra Shekharb4b58f52008-10-08 10:01:39 +030059 struct omap_mcbsp *mcbsp = id_to_mcbsp_ptr(id);
60
61 dev_dbg(mcbsp->dev, "**** McBSP%d regs ****\n", mcbsp->id);
62 dev_dbg(mcbsp->dev, "DRR2: 0x%04x\n",
63 OMAP_MCBSP_READ(mcbsp->io_base, DRR2));
64 dev_dbg(mcbsp->dev, "DRR1: 0x%04x\n",
65 OMAP_MCBSP_READ(mcbsp->io_base, DRR1));
66 dev_dbg(mcbsp->dev, "DXR2: 0x%04x\n",
67 OMAP_MCBSP_READ(mcbsp->io_base, DXR2));
68 dev_dbg(mcbsp->dev, "DXR1: 0x%04x\n",
69 OMAP_MCBSP_READ(mcbsp->io_base, DXR1));
70 dev_dbg(mcbsp->dev, "SPCR2: 0x%04x\n",
71 OMAP_MCBSP_READ(mcbsp->io_base, SPCR2));
72 dev_dbg(mcbsp->dev, "SPCR1: 0x%04x\n",
73 OMAP_MCBSP_READ(mcbsp->io_base, SPCR1));
74 dev_dbg(mcbsp->dev, "RCR2: 0x%04x\n",
75 OMAP_MCBSP_READ(mcbsp->io_base, RCR2));
76 dev_dbg(mcbsp->dev, "RCR1: 0x%04x\n",
77 OMAP_MCBSP_READ(mcbsp->io_base, RCR1));
78 dev_dbg(mcbsp->dev, "XCR2: 0x%04x\n",
79 OMAP_MCBSP_READ(mcbsp->io_base, XCR2));
80 dev_dbg(mcbsp->dev, "XCR1: 0x%04x\n",
81 OMAP_MCBSP_READ(mcbsp->io_base, XCR1));
82 dev_dbg(mcbsp->dev, "SRGR2: 0x%04x\n",
83 OMAP_MCBSP_READ(mcbsp->io_base, SRGR2));
84 dev_dbg(mcbsp->dev, "SRGR1: 0x%04x\n",
85 OMAP_MCBSP_READ(mcbsp->io_base, SRGR1));
86 dev_dbg(mcbsp->dev, "PCR0: 0x%04x\n",
87 OMAP_MCBSP_READ(mcbsp->io_base, PCR0));
88 dev_dbg(mcbsp->dev, "***********************\n");
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010089}
90
Linus Torvalds0cd61b62006-10-06 10:53:39 -070091static irqreturn_t omap_mcbsp_tx_irq_handler(int irq, void *dev_id)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010092{
Jeff Garzike8f2af12007-10-26 05:40:25 -040093 struct omap_mcbsp *mcbsp_tx = dev_id;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010094
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +030095 dev_dbg(mcbsp_tx->dev, "TX IRQ callback : 0x%x\n",
96 OMAP_MCBSP_READ(mcbsp_tx->io_base, SPCR2));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010097
98 complete(&mcbsp_tx->tx_irq_completion);
Eduardo Valentinfb78d802008-07-03 12:24:39 +030099
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100100 return IRQ_HANDLED;
101}
102
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700103static irqreturn_t omap_mcbsp_rx_irq_handler(int irq, void *dev_id)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100104{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400105 struct omap_mcbsp *mcbsp_rx = dev_id;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100106
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300107 dev_dbg(mcbsp_rx->dev, "RX IRQ callback : 0x%x\n",
108 OMAP_MCBSP_READ(mcbsp_rx->io_base, SPCR2));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100109
110 complete(&mcbsp_rx->rx_irq_completion);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300111
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100112 return IRQ_HANDLED;
113}
114
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100115static void omap_mcbsp_tx_dma_callback(int lch, u16 ch_status, void *data)
116{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400117 struct omap_mcbsp *mcbsp_dma_tx = data;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100118
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300119 dev_dbg(mcbsp_dma_tx->dev, "TX DMA callback : 0x%x\n",
120 OMAP_MCBSP_READ(mcbsp_dma_tx->io_base, SPCR2));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100121
122 /* We can free the channels */
123 omap_free_dma(mcbsp_dma_tx->dma_tx_lch);
124 mcbsp_dma_tx->dma_tx_lch = -1;
125
126 complete(&mcbsp_dma_tx->tx_dma_completion);
127}
128
129static void omap_mcbsp_rx_dma_callback(int lch, u16 ch_status, void *data)
130{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400131 struct omap_mcbsp *mcbsp_dma_rx = data;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100132
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300133 dev_dbg(mcbsp_dma_rx->dev, "RX DMA callback : 0x%x\n",
134 OMAP_MCBSP_READ(mcbsp_dma_rx->io_base, SPCR2));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100135
136 /* We can free the channels */
137 omap_free_dma(mcbsp_dma_rx->dma_rx_lch);
138 mcbsp_dma_rx->dma_rx_lch = -1;
139
140 complete(&mcbsp_dma_rx->rx_dma_completion);
141}
142
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100143/*
144 * omap_mcbsp_config simply write a config to the
145 * appropriate McBSP.
146 * You either call this function or set the McBSP registers
147 * by yourself before calling omap_mcbsp_start().
148 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300149void omap_mcbsp_config(unsigned int id, const struct omap_mcbsp_reg_cfg *config)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100150{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300151 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100152 void __iomem *io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100153
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300154 if (!omap_mcbsp_check_valid_id(id)) {
155 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
156 return;
157 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300158 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300159
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300160 io_base = mcbsp->io_base;
161 dev_dbg(mcbsp->dev, "Configuring McBSP%d phys_base: 0x%08lx\n",
162 mcbsp->id, mcbsp->phys_base);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100163
164 /* We write the given config */
165 OMAP_MCBSP_WRITE(io_base, SPCR2, config->spcr2);
166 OMAP_MCBSP_WRITE(io_base, SPCR1, config->spcr1);
167 OMAP_MCBSP_WRITE(io_base, RCR2, config->rcr2);
168 OMAP_MCBSP_WRITE(io_base, RCR1, config->rcr1);
169 OMAP_MCBSP_WRITE(io_base, XCR2, config->xcr2);
170 OMAP_MCBSP_WRITE(io_base, XCR1, config->xcr1);
171 OMAP_MCBSP_WRITE(io_base, SRGR2, config->srgr2);
172 OMAP_MCBSP_WRITE(io_base, SRGR1, config->srgr1);
173 OMAP_MCBSP_WRITE(io_base, MCR2, config->mcr2);
174 OMAP_MCBSP_WRITE(io_base, MCR1, config->mcr1);
175 OMAP_MCBSP_WRITE(io_base, PCR0, config->pcr0);
176}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300177EXPORT_SYMBOL(omap_mcbsp_config);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100178
Tony Lindgren120db2c2006-04-02 17:46:27 +0100179/*
180 * We can choose between IRQ based or polled IO.
181 * This needs to be called before omap_mcbsp_request().
182 */
183int omap_mcbsp_set_io_type(unsigned int id, omap_mcbsp_io_type_t io_type)
184{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300185 struct omap_mcbsp *mcbsp;
186
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300187 if (!omap_mcbsp_check_valid_id(id)) {
188 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
189 return -ENODEV;
190 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300191 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100192
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300193 spin_lock(&mcbsp->lock);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100194
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300195 if (!mcbsp->free) {
196 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
197 mcbsp->id);
198 spin_unlock(&mcbsp->lock);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100199 return -EINVAL;
200 }
201
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300202 mcbsp->io_type = io_type;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100203
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300204 spin_unlock(&mcbsp->lock);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100205
206 return 0;
207}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300208EXPORT_SYMBOL(omap_mcbsp_set_io_type);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100209
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100210int omap_mcbsp_request(unsigned int id)
211{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300212 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100213 int err;
214
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300215 if (!omap_mcbsp_check_valid_id(id)) {
216 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
217 return -ENODEV;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100218 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300219 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300220
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300221 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->request)
222 mcbsp->pdata->ops->request(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300223
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300224 clk_enable(mcbsp->clk);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100225
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300226 spin_lock(&mcbsp->lock);
227 if (!mcbsp->free) {
228 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
229 mcbsp->id);
230 spin_unlock(&mcbsp->lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100231 return -1;
232 }
233
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300234 mcbsp->free = 0;
235 spin_unlock(&mcbsp->lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100236
Jarkko Nikula5a070552008-10-08 10:01:41 +0300237 /*
238 * Make sure that transmitter, receiver and sample-rate generator are
239 * not running before activating IRQs.
240 */
241 OMAP_MCBSP_WRITE(mcbsp->io_base, SPCR1, 0);
242 OMAP_MCBSP_WRITE(mcbsp->io_base, SPCR2, 0);
243
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300244 if (mcbsp->io_type == OMAP_MCBSP_IRQ_IO) {
Tony Lindgren120db2c2006-04-02 17:46:27 +0100245 /* We need to get IRQs here */
Jarkko Nikula5a070552008-10-08 10:01:41 +0300246 init_completion(&mcbsp->tx_irq_completion);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300247 err = request_irq(mcbsp->tx_irq, omap_mcbsp_tx_irq_handler,
248 0, "McBSP", (void *)mcbsp);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100249 if (err != 0) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300250 dev_err(mcbsp->dev, "Unable to request TX IRQ %d "
251 "for McBSP%d\n", mcbsp->tx_irq,
252 mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100253 return err;
254 }
255
Jarkko Nikula5a070552008-10-08 10:01:41 +0300256 init_completion(&mcbsp->rx_irq_completion);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300257 err = request_irq(mcbsp->rx_irq, omap_mcbsp_rx_irq_handler,
258 0, "McBSP", (void *)mcbsp);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100259 if (err != 0) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300260 dev_err(mcbsp->dev, "Unable to request RX IRQ %d "
261 "for McBSP%d\n", mcbsp->rx_irq,
262 mcbsp->id);
263 free_irq(mcbsp->tx_irq, (void *)mcbsp);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100264 return err;
265 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100266 }
267
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100268 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100269}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300270EXPORT_SYMBOL(omap_mcbsp_request);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100271
272void omap_mcbsp_free(unsigned int id)
273{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300274 struct omap_mcbsp *mcbsp;
275
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300276 if (!omap_mcbsp_check_valid_id(id)) {
277 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100278 return;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100279 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300280 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100281
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300282 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
283 mcbsp->pdata->ops->free(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300284
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300285 clk_disable(mcbsp->clk);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100286
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300287 spin_lock(&mcbsp->lock);
288 if (mcbsp->free) {
289 dev_err(mcbsp->dev, "McBSP%d was not reserved\n",
290 mcbsp->id);
291 spin_unlock(&mcbsp->lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100292 return;
293 }
294
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300295 mcbsp->free = 1;
296 spin_unlock(&mcbsp->lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100297
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300298 if (mcbsp->io_type == OMAP_MCBSP_IRQ_IO) {
Tony Lindgren120db2c2006-04-02 17:46:27 +0100299 /* Free IRQs */
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300300 free_irq(mcbsp->rx_irq, (void *)mcbsp);
301 free_irq(mcbsp->tx_irq, (void *)mcbsp);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100302 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100303}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300304EXPORT_SYMBOL(omap_mcbsp_free);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100305
306/*
307 * Here we start the McBSP, by enabling the sample
308 * generator, both transmitter and receivers,
309 * and the frame sync.
310 */
311void omap_mcbsp_start(unsigned int id)
312{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300313 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100314 void __iomem *io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100315 u16 w;
316
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300317 if (!omap_mcbsp_check_valid_id(id)) {
318 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100319 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300320 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300321 mcbsp = id_to_mcbsp_ptr(id);
322 io_base = mcbsp->io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100323
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300324 mcbsp->rx_word_length = (OMAP_MCBSP_READ(io_base, RCR1) >> 5) & 0x7;
325 mcbsp->tx_word_length = (OMAP_MCBSP_READ(io_base, XCR1) >> 5) & 0x7;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100326
327 /* Start the sample generator */
328 w = OMAP_MCBSP_READ(io_base, SPCR2);
329 OMAP_MCBSP_WRITE(io_base, SPCR2, w | (1 << 6));
330
331 /* Enable transmitter and receiver */
332 w = OMAP_MCBSP_READ(io_base, SPCR2);
333 OMAP_MCBSP_WRITE(io_base, SPCR2, w | 1);
334
335 w = OMAP_MCBSP_READ(io_base, SPCR1);
336 OMAP_MCBSP_WRITE(io_base, SPCR1, w | 1);
337
338 udelay(100);
339
340 /* Start frame sync */
341 w = OMAP_MCBSP_READ(io_base, SPCR2);
342 OMAP_MCBSP_WRITE(io_base, SPCR2, w | (1 << 7));
343
344 /* Dump McBSP Regs */
345 omap_mcbsp_dump_reg(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100346}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300347EXPORT_SYMBOL(omap_mcbsp_start);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100348
349void omap_mcbsp_stop(unsigned int id)
350{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300351 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100352 void __iomem *io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100353 u16 w;
354
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300355 if (!omap_mcbsp_check_valid_id(id)) {
356 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100357 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300358 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100359
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300360 mcbsp = id_to_mcbsp_ptr(id);
361 io_base = mcbsp->io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100362
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300363 /* Reset transmitter */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100364 w = OMAP_MCBSP_READ(io_base, SPCR2);
365 OMAP_MCBSP_WRITE(io_base, SPCR2, w & ~(1));
366
367 /* Reset receiver */
368 w = OMAP_MCBSP_READ(io_base, SPCR1);
369 OMAP_MCBSP_WRITE(io_base, SPCR1, w & ~(1));
370
371 /* Reset the sample rate generator */
372 w = OMAP_MCBSP_READ(io_base, SPCR2);
373 OMAP_MCBSP_WRITE(io_base, SPCR2, w & ~(1 << 6));
374}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300375EXPORT_SYMBOL(omap_mcbsp_stop);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100376
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100377/* polled mcbsp i/o operations */
378int omap_mcbsp_pollwrite(unsigned int id, u16 buf)
379{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300380 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100381 void __iomem *base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300382
383 if (!omap_mcbsp_check_valid_id(id)) {
384 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
385 return -ENODEV;
386 }
387
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300388 mcbsp = id_to_mcbsp_ptr(id);
389 base = mcbsp->io_base;
390
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100391 writew(buf, base + OMAP_MCBSP_REG_DXR1);
392 /* if frame sync error - clear the error */
393 if (readw(base + OMAP_MCBSP_REG_SPCR2) & XSYNC_ERR) {
394 /* clear error */
395 writew(readw(base + OMAP_MCBSP_REG_SPCR2) & (~XSYNC_ERR),
396 base + OMAP_MCBSP_REG_SPCR2);
397 /* resend */
398 return -1;
399 } else {
400 /* wait for transmit confirmation */
401 int attemps = 0;
402 while (!(readw(base + OMAP_MCBSP_REG_SPCR2) & XRDY)) {
403 if (attemps++ > 1000) {
404 writew(readw(base + OMAP_MCBSP_REG_SPCR2) &
405 (~XRST),
406 base + OMAP_MCBSP_REG_SPCR2);
407 udelay(10);
408 writew(readw(base + OMAP_MCBSP_REG_SPCR2) |
409 (XRST),
410 base + OMAP_MCBSP_REG_SPCR2);
411 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300412 dev_err(mcbsp->dev, "Could not write to"
413 " McBSP%d Register\n", mcbsp->id);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100414 return -2;
415 }
416 }
417 }
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300418
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100419 return 0;
420}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300421EXPORT_SYMBOL(omap_mcbsp_pollwrite);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100422
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300423int omap_mcbsp_pollread(unsigned int id, u16 *buf)
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100424{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300425 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100426 void __iomem *base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300427
428 if (!omap_mcbsp_check_valid_id(id)) {
429 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
430 return -ENODEV;
431 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300432 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300433
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300434 base = mcbsp->io_base;
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100435 /* if frame sync error - clear the error */
436 if (readw(base + OMAP_MCBSP_REG_SPCR1) & RSYNC_ERR) {
437 /* clear error */
438 writew(readw(base + OMAP_MCBSP_REG_SPCR1) & (~RSYNC_ERR),
439 base + OMAP_MCBSP_REG_SPCR1);
440 /* resend */
441 return -1;
442 } else {
443 /* wait for recieve confirmation */
444 int attemps = 0;
445 while (!(readw(base + OMAP_MCBSP_REG_SPCR1) & RRDY)) {
446 if (attemps++ > 1000) {
447 writew(readw(base + OMAP_MCBSP_REG_SPCR1) &
448 (~RRST),
449 base + OMAP_MCBSP_REG_SPCR1);
450 udelay(10);
451 writew(readw(base + OMAP_MCBSP_REG_SPCR1) |
452 (RRST),
453 base + OMAP_MCBSP_REG_SPCR1);
454 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300455 dev_err(mcbsp->dev, "Could not read from"
456 " McBSP%d Register\n", mcbsp->id);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100457 return -2;
458 }
459 }
460 }
461 *buf = readw(base + OMAP_MCBSP_REG_DRR1);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300462
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100463 return 0;
464}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300465EXPORT_SYMBOL(omap_mcbsp_pollread);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100466
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100467/*
468 * IRQ based word transmission.
469 */
470void omap_mcbsp_xmit_word(unsigned int id, u32 word)
471{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300472 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100473 void __iomem *io_base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300474 omap_mcbsp_word_length word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100475
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300476 if (!omap_mcbsp_check_valid_id(id)) {
477 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100478 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300479 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100480
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300481 mcbsp = id_to_mcbsp_ptr(id);
482 io_base = mcbsp->io_base;
483 word_length = mcbsp->tx_word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100484
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300485 wait_for_completion(&mcbsp->tx_irq_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100486
487 if (word_length > OMAP_MCBSP_WORD_16)
488 OMAP_MCBSP_WRITE(io_base, DXR2, word >> 16);
489 OMAP_MCBSP_WRITE(io_base, DXR1, word & 0xffff);
490}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300491EXPORT_SYMBOL(omap_mcbsp_xmit_word);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100492
493u32 omap_mcbsp_recv_word(unsigned int id)
494{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300495 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100496 void __iomem *io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100497 u16 word_lsb, word_msb = 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300498 omap_mcbsp_word_length word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100499
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300500 if (!omap_mcbsp_check_valid_id(id)) {
501 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
502 return -ENODEV;
503 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300504 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100505
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300506 word_length = mcbsp->rx_word_length;
507 io_base = mcbsp->io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100508
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300509 wait_for_completion(&mcbsp->rx_irq_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100510
511 if (word_length > OMAP_MCBSP_WORD_16)
512 word_msb = OMAP_MCBSP_READ(io_base, DRR2);
513 word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
514
515 return (word_lsb | (word_msb << 16));
516}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300517EXPORT_SYMBOL(omap_mcbsp_recv_word);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100518
Tony Lindgren120db2c2006-04-02 17:46:27 +0100519int omap_mcbsp_spi_master_xmit_word_poll(unsigned int id, u32 word)
520{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300521 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100522 void __iomem *io_base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300523 omap_mcbsp_word_length tx_word_length;
524 omap_mcbsp_word_length rx_word_length;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100525 u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
526
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300527 if (!omap_mcbsp_check_valid_id(id)) {
528 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
529 return -ENODEV;
530 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300531 mcbsp = id_to_mcbsp_ptr(id);
532 io_base = mcbsp->io_base;
533 tx_word_length = mcbsp->tx_word_length;
534 rx_word_length = mcbsp->rx_word_length;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300535
Tony Lindgren120db2c2006-04-02 17:46:27 +0100536 if (tx_word_length != rx_word_length)
537 return -EINVAL;
538
539 /* First we wait for the transmitter to be ready */
540 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
541 while (!(spcr2 & XRDY)) {
542 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
543 if (attempts++ > 1000) {
544 /* We must reset the transmitter */
545 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 & (~XRST));
546 udelay(10);
547 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 | XRST);
548 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300549 dev_err(mcbsp->dev, "McBSP%d transmitter not "
550 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100551 return -EAGAIN;
552 }
553 }
554
555 /* Now we can push the data */
556 if (tx_word_length > OMAP_MCBSP_WORD_16)
557 OMAP_MCBSP_WRITE(io_base, DXR2, word >> 16);
558 OMAP_MCBSP_WRITE(io_base, DXR1, word & 0xffff);
559
560 /* We wait for the receiver to be ready */
561 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
562 while (!(spcr1 & RRDY)) {
563 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
564 if (attempts++ > 1000) {
565 /* We must reset the receiver */
566 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 & (~RRST));
567 udelay(10);
568 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 | RRST);
569 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300570 dev_err(mcbsp->dev, "McBSP%d receiver not "
571 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100572 return -EAGAIN;
573 }
574 }
575
576 /* Receiver is ready, let's read the dummy data */
577 if (rx_word_length > OMAP_MCBSP_WORD_16)
578 word_msb = OMAP_MCBSP_READ(io_base, DRR2);
579 word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
580
581 return 0;
582}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300583EXPORT_SYMBOL(omap_mcbsp_spi_master_xmit_word_poll);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100584
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300585int omap_mcbsp_spi_master_recv_word_poll(unsigned int id, u32 *word)
Tony Lindgren120db2c2006-04-02 17:46:27 +0100586{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300587 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100588 u32 clock_word = 0;
589 void __iomem *io_base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300590 omap_mcbsp_word_length tx_word_length;
591 omap_mcbsp_word_length rx_word_length;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100592 u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
593
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300594 if (!omap_mcbsp_check_valid_id(id)) {
595 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
596 return -ENODEV;
597 }
598
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300599 mcbsp = id_to_mcbsp_ptr(id);
600 io_base = mcbsp->io_base;
601
602 tx_word_length = mcbsp->tx_word_length;
603 rx_word_length = mcbsp->rx_word_length;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300604
Tony Lindgren120db2c2006-04-02 17:46:27 +0100605 if (tx_word_length != rx_word_length)
606 return -EINVAL;
607
608 /* First we wait for the transmitter to be ready */
609 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
610 while (!(spcr2 & XRDY)) {
611 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
612 if (attempts++ > 1000) {
613 /* We must reset the transmitter */
614 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 & (~XRST));
615 udelay(10);
616 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 | XRST);
617 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300618 dev_err(mcbsp->dev, "McBSP%d transmitter not "
619 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100620 return -EAGAIN;
621 }
622 }
623
624 /* We first need to enable the bus clock */
625 if (tx_word_length > OMAP_MCBSP_WORD_16)
626 OMAP_MCBSP_WRITE(io_base, DXR2, clock_word >> 16);
627 OMAP_MCBSP_WRITE(io_base, DXR1, clock_word & 0xffff);
628
629 /* We wait for the receiver to be ready */
630 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
631 while (!(spcr1 & RRDY)) {
632 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
633 if (attempts++ > 1000) {
634 /* We must reset the receiver */
635 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 & (~RRST));
636 udelay(10);
637 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 | RRST);
638 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300639 dev_err(mcbsp->dev, "McBSP%d receiver not "
640 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100641 return -EAGAIN;
642 }
643 }
644
645 /* Receiver is ready, there is something for us */
646 if (rx_word_length > OMAP_MCBSP_WORD_16)
647 word_msb = OMAP_MCBSP_READ(io_base, DRR2);
648 word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
649
650 word[0] = (word_lsb | (word_msb << 16));
651
652 return 0;
653}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300654EXPORT_SYMBOL(omap_mcbsp_spi_master_recv_word_poll);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100655
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100656/*
657 * Simple DMA based buffer rx/tx routines.
658 * Nothing fancy, just a single buffer tx/rx through DMA.
659 * The DMA resources are released once the transfer is done.
660 * For anything fancier, you should use your own customized DMA
661 * routines and callbacks.
662 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300663int omap_mcbsp_xmit_buffer(unsigned int id, dma_addr_t buffer,
664 unsigned int length)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100665{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300666 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100667 int dma_tx_ch;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100668 int src_port = 0;
669 int dest_port = 0;
670 int sync_dev = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100671
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300672 if (!omap_mcbsp_check_valid_id(id)) {
673 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
674 return -ENODEV;
675 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300676 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100677
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300678 if (omap_request_dma(mcbsp->dma_tx_sync, "McBSP TX",
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300679 omap_mcbsp_tx_dma_callback,
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300680 mcbsp,
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300681 &dma_tx_ch)) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300682 dev_err(mcbsp->dev, " Unable to request DMA channel for "
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300683 "McBSP%d TX. Trying IRQ based TX\n",
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300684 mcbsp->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100685 return -EAGAIN;
686 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300687 mcbsp->dma_tx_lch = dma_tx_ch;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100688
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300689 dev_err(mcbsp->dev, "McBSP%d TX DMA on channel %d\n", mcbsp->id,
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300690 dma_tx_ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100691
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300692 init_completion(&mcbsp->tx_dma_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100693
Tony Lindgren120db2c2006-04-02 17:46:27 +0100694 if (cpu_class_is_omap1()) {
695 src_port = OMAP_DMA_PORT_TIPB;
696 dest_port = OMAP_DMA_PORT_EMIFF;
697 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300698 if (cpu_class_is_omap2())
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300699 sync_dev = mcbsp->dma_tx_sync;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100700
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300701 omap_set_dma_transfer_params(mcbsp->dma_tx_lch,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100702 OMAP_DMA_DATA_TYPE_S16,
703 length >> 1, 1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000704 OMAP_DMA_SYNC_ELEMENT,
Tony Lindgren120db2c2006-04-02 17:46:27 +0100705 sync_dev, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100706
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300707 omap_set_dma_dest_params(mcbsp->dma_tx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +0100708 src_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100709 OMAP_DMA_AMODE_CONSTANT,
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300710 mcbsp->phys_base + OMAP_MCBSP_REG_DXR1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000711 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100712
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300713 omap_set_dma_src_params(mcbsp->dma_tx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +0100714 dest_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100715 OMAP_DMA_AMODE_POST_INC,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000716 buffer,
717 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100718
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300719 omap_start_dma(mcbsp->dma_tx_lch);
720 wait_for_completion(&mcbsp->tx_dma_completion);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300721
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100722 return 0;
723}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300724EXPORT_SYMBOL(omap_mcbsp_xmit_buffer);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100725
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300726int omap_mcbsp_recv_buffer(unsigned int id, dma_addr_t buffer,
727 unsigned int length)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100728{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300729 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100730 int dma_rx_ch;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100731 int src_port = 0;
732 int dest_port = 0;
733 int sync_dev = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100734
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300735 if (!omap_mcbsp_check_valid_id(id)) {
736 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
737 return -ENODEV;
738 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300739 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100740
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300741 if (omap_request_dma(mcbsp->dma_rx_sync, "McBSP RX",
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300742 omap_mcbsp_rx_dma_callback,
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300743 mcbsp,
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300744 &dma_rx_ch)) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300745 dev_err(mcbsp->dev, "Unable to request DMA channel for "
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300746 "McBSP%d RX. Trying IRQ based RX\n",
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300747 mcbsp->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100748 return -EAGAIN;
749 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300750 mcbsp->dma_rx_lch = dma_rx_ch;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100751
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300752 dev_err(mcbsp->dev, "McBSP%d RX DMA on channel %d\n", mcbsp->id,
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300753 dma_rx_ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100754
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300755 init_completion(&mcbsp->rx_dma_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100756
Tony Lindgren120db2c2006-04-02 17:46:27 +0100757 if (cpu_class_is_omap1()) {
758 src_port = OMAP_DMA_PORT_TIPB;
759 dest_port = OMAP_DMA_PORT_EMIFF;
760 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300761 if (cpu_class_is_omap2())
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300762 sync_dev = mcbsp->dma_rx_sync;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100763
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300764 omap_set_dma_transfer_params(mcbsp->dma_rx_lch,
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300765 OMAP_DMA_DATA_TYPE_S16,
766 length >> 1, 1,
767 OMAP_DMA_SYNC_ELEMENT,
768 sync_dev, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100769
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300770 omap_set_dma_src_params(mcbsp->dma_rx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +0100771 src_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100772 OMAP_DMA_AMODE_CONSTANT,
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300773 mcbsp->phys_base + OMAP_MCBSP_REG_DRR1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000774 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100775
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300776 omap_set_dma_dest_params(mcbsp->dma_rx_lch,
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300777 dest_port,
778 OMAP_DMA_AMODE_POST_INC,
779 buffer,
780 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100781
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300782 omap_start_dma(mcbsp->dma_rx_lch);
783 wait_for_completion(&mcbsp->rx_dma_completion);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300784
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100785 return 0;
786}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300787EXPORT_SYMBOL(omap_mcbsp_recv_buffer);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100788
789/*
790 * SPI wrapper.
791 * Since SPI setup is much simpler than the generic McBSP one,
792 * this wrapper just need an omap_mcbsp_spi_cfg structure as an input.
793 * Once this is done, you can call omap_mcbsp_start().
794 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300795void omap_mcbsp_set_spi_mode(unsigned int id,
796 const struct omap_mcbsp_spi_cfg *spi_cfg)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100797{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300798 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100799 struct omap_mcbsp_reg_cfg mcbsp_cfg;
800
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300801 if (!omap_mcbsp_check_valid_id(id)) {
802 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100803 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300804 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300805 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100806
807 memset(&mcbsp_cfg, 0, sizeof(struct omap_mcbsp_reg_cfg));
808
809 /* SPI has only one frame */
810 mcbsp_cfg.rcr1 |= (RWDLEN1(spi_cfg->word_length) | RFRLEN1(0));
811 mcbsp_cfg.xcr1 |= (XWDLEN1(spi_cfg->word_length) | XFRLEN1(0));
812
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300813 /* Clock stop mode */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100814 if (spi_cfg->clk_stp_mode == OMAP_MCBSP_CLK_STP_MODE_NO_DELAY)
815 mcbsp_cfg.spcr1 |= (1 << 12);
816 else
817 mcbsp_cfg.spcr1 |= (3 << 11);
818
819 /* Set clock parities */
820 if (spi_cfg->rx_clock_polarity == OMAP_MCBSP_CLK_RISING)
821 mcbsp_cfg.pcr0 |= CLKRP;
822 else
823 mcbsp_cfg.pcr0 &= ~CLKRP;
824
825 if (spi_cfg->tx_clock_polarity == OMAP_MCBSP_CLK_RISING)
826 mcbsp_cfg.pcr0 &= ~CLKXP;
827 else
828 mcbsp_cfg.pcr0 |= CLKXP;
829
830 /* Set SCLKME to 0 and CLKSM to 1 */
831 mcbsp_cfg.pcr0 &= ~SCLKME;
832 mcbsp_cfg.srgr2 |= CLKSM;
833
834 /* Set FSXP */
835 if (spi_cfg->fsx_polarity == OMAP_MCBSP_FS_ACTIVE_HIGH)
836 mcbsp_cfg.pcr0 &= ~FSXP;
837 else
838 mcbsp_cfg.pcr0 |= FSXP;
839
840 if (spi_cfg->spi_mode == OMAP_MCBSP_SPI_MASTER) {
841 mcbsp_cfg.pcr0 |= CLKXM;
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300842 mcbsp_cfg.srgr1 |= CLKGDV(spi_cfg->clk_div - 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100843 mcbsp_cfg.pcr0 |= FSXM;
844 mcbsp_cfg.srgr2 &= ~FSGM;
845 mcbsp_cfg.xcr2 |= XDATDLY(1);
846 mcbsp_cfg.rcr2 |= RDATDLY(1);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300847 } else {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100848 mcbsp_cfg.pcr0 &= ~CLKXM;
849 mcbsp_cfg.srgr1 |= CLKGDV(1);
850 mcbsp_cfg.pcr0 &= ~FSXM;
851 mcbsp_cfg.xcr2 &= ~XDATDLY(3);
852 mcbsp_cfg.rcr2 &= ~RDATDLY(3);
853 }
854
855 mcbsp_cfg.xcr2 &= ~XPHASE;
856 mcbsp_cfg.rcr2 &= ~RPHASE;
857
858 omap_mcbsp_config(id, &mcbsp_cfg);
859}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300860EXPORT_SYMBOL(omap_mcbsp_set_spi_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100861
862/*
863 * McBSP1 and McBSP3 are directly mapped on 1610 and 1510.
864 * 730 has only 2 McBSP, and both of them are MPU peripherals.
865 */
Uwe Kleine-König25cef222008-10-08 10:01:39 +0300866static int __devinit omap_mcbsp_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100867{
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300868 struct omap_mcbsp_platform_data *pdata = pdev->dev.platform_data;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300869 struct omap_mcbsp *mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300870 int id = pdev->id - 1;
871 int ret = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100872
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300873 if (!pdata) {
874 dev_err(&pdev->dev, "McBSP device initialized without"
875 "platform data\n");
876 ret = -EINVAL;
877 goto exit;
878 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100879
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300880 dev_dbg(&pdev->dev, "Initializing OMAP McBSP (%d).\n", pdev->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100881
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300882 if (id >= omap_mcbsp_count) {
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300883 dev_err(&pdev->dev, "Invalid McBSP device id (%d)\n", id);
884 ret = -EINVAL;
885 goto exit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100886 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100887
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300888 mcbsp = kzalloc(sizeof(struct omap_mcbsp), GFP_KERNEL);
889 if (!mcbsp) {
890 ret = -ENOMEM;
891 goto exit;
892 }
893 mcbsp_ptr[id] = mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300894
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300895 spin_lock_init(&mcbsp->lock);
896 mcbsp->id = id + 1;
897 mcbsp->free = 1;
898 mcbsp->dma_tx_lch = -1;
899 mcbsp->dma_rx_lch = -1;
900
901 mcbsp->phys_base = pdata->phys_base;
902 mcbsp->io_base = ioremap(pdata->phys_base, SZ_4K);
903 if (!mcbsp->io_base) {
Russell Kingd592dd12008-09-04 14:25:42 +0100904 ret = -ENOMEM;
905 goto err_ioremap;
906 }
907
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300908 /* Default I/O is IRQ based */
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300909 mcbsp->io_type = OMAP_MCBSP_IRQ_IO;
910 mcbsp->tx_irq = pdata->tx_irq;
911 mcbsp->rx_irq = pdata->rx_irq;
912 mcbsp->dma_rx_sync = pdata->dma_rx_sync;
913 mcbsp->dma_tx_sync = pdata->dma_tx_sync;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300914
915 if (pdata->clk_name)
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300916 mcbsp->clk = clk_get(&pdev->dev, pdata->clk_name);
917 if (IS_ERR(mcbsp->clk)) {
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300918 dev_err(&pdev->dev,
919 "Invalid clock configuration for McBSP%d.\n",
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300920 mcbsp->id);
921 ret = PTR_ERR(mcbsp->clk);
Russell Kingd592dd12008-09-04 14:25:42 +0100922 goto err_clk;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300923 }
924
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300925 mcbsp->pdata = pdata;
926 mcbsp->dev = &pdev->dev;
927 platform_set_drvdata(pdev, mcbsp);
Russell Kingd592dd12008-09-04 14:25:42 +0100928 return 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300929
Russell Kingd592dd12008-09-04 14:25:42 +0100930err_clk:
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300931 iounmap(mcbsp->io_base);
Russell Kingd592dd12008-09-04 14:25:42 +0100932err_ioremap:
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300933 mcbsp->free = 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300934exit:
935 return ret;
936}
937
Uwe Kleine-König25cef222008-10-08 10:01:39 +0300938static int __devexit omap_mcbsp_remove(struct platform_device *pdev)
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300939{
940 struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev);
941
942 platform_set_drvdata(pdev, NULL);
943 if (mcbsp) {
944
945 if (mcbsp->pdata && mcbsp->pdata->ops &&
946 mcbsp->pdata->ops->free)
947 mcbsp->pdata->ops->free(mcbsp->id);
948
949 clk_disable(mcbsp->clk);
950 clk_put(mcbsp->clk);
951
Russell Kingd592dd12008-09-04 14:25:42 +0100952 iounmap(mcbsp->io_base);
953
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300954 mcbsp->clk = NULL;
955 mcbsp->free = 0;
956 mcbsp->dev = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100957 }
958
959 return 0;
960}
961
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300962static struct platform_driver omap_mcbsp_driver = {
963 .probe = omap_mcbsp_probe,
Uwe Kleine-König25cef222008-10-08 10:01:39 +0300964 .remove = __devexit_p(omap_mcbsp_remove),
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300965 .driver = {
966 .name = "omap-mcbsp",
967 },
968};
969
970int __init omap_mcbsp_init(void)
971{
972 /* Register the McBSP driver */
973 return platform_driver_register(&omap_mcbsp_driver);
974}
975