blob: e5842e30e534485b6a1c5faa11bbd9ce2de94272 [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);
Tony Lindgren3127f8f2009-01-15 13:09:54 +0200176 if (cpu_is_omap2430() || cpu_is_omap34xx()) {
177 OMAP_MCBSP_WRITE(io_base, XCCR, config->xccr);
178 OMAP_MCBSP_WRITE(io_base, RCCR, config->rccr);
179 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100180}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300181EXPORT_SYMBOL(omap_mcbsp_config);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100182
Tony Lindgren120db2c2006-04-02 17:46:27 +0100183/*
184 * We can choose between IRQ based or polled IO.
185 * This needs to be called before omap_mcbsp_request().
186 */
187int omap_mcbsp_set_io_type(unsigned int id, omap_mcbsp_io_type_t io_type)
188{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300189 struct omap_mcbsp *mcbsp;
190
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300191 if (!omap_mcbsp_check_valid_id(id)) {
192 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
193 return -ENODEV;
194 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300195 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100196
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300197 spin_lock(&mcbsp->lock);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100198
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300199 if (!mcbsp->free) {
200 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
201 mcbsp->id);
202 spin_unlock(&mcbsp->lock);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100203 return -EINVAL;
204 }
205
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300206 mcbsp->io_type = io_type;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100207
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300208 spin_unlock(&mcbsp->lock);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100209
210 return 0;
211}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300212EXPORT_SYMBOL(omap_mcbsp_set_io_type);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100213
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100214int omap_mcbsp_request(unsigned int id)
215{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300216 struct omap_mcbsp *mcbsp;
Stanley.Miao06151152009-01-29 08:57:12 -0800217 int i;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100218 int err;
219
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300220 if (!omap_mcbsp_check_valid_id(id)) {
221 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
222 return -ENODEV;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100223 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300224 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300225
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300226 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->request)
227 mcbsp->pdata->ops->request(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300228
Stanley.Miao06151152009-01-29 08:57:12 -0800229 for (i = 0; i < mcbsp->num_clks; i++)
230 clk_enable(mcbsp->clks[i]);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100231
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300232 spin_lock(&mcbsp->lock);
233 if (!mcbsp->free) {
234 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
235 mcbsp->id);
236 spin_unlock(&mcbsp->lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100237 return -1;
238 }
239
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300240 mcbsp->free = 0;
241 spin_unlock(&mcbsp->lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100242
Jarkko Nikula5a070552008-10-08 10:01:41 +0300243 /*
244 * Make sure that transmitter, receiver and sample-rate generator are
245 * not running before activating IRQs.
246 */
247 OMAP_MCBSP_WRITE(mcbsp->io_base, SPCR1, 0);
248 OMAP_MCBSP_WRITE(mcbsp->io_base, SPCR2, 0);
249
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300250 if (mcbsp->io_type == OMAP_MCBSP_IRQ_IO) {
Tony Lindgren120db2c2006-04-02 17:46:27 +0100251 /* We need to get IRQs here */
Jarkko Nikula5a070552008-10-08 10:01:41 +0300252 init_completion(&mcbsp->tx_irq_completion);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300253 err = request_irq(mcbsp->tx_irq, omap_mcbsp_tx_irq_handler,
254 0, "McBSP", (void *)mcbsp);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100255 if (err != 0) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300256 dev_err(mcbsp->dev, "Unable to request TX IRQ %d "
257 "for McBSP%d\n", mcbsp->tx_irq,
258 mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100259 return err;
260 }
261
Jarkko Nikula5a070552008-10-08 10:01:41 +0300262 init_completion(&mcbsp->rx_irq_completion);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300263 err = request_irq(mcbsp->rx_irq, omap_mcbsp_rx_irq_handler,
264 0, "McBSP", (void *)mcbsp);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100265 if (err != 0) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300266 dev_err(mcbsp->dev, "Unable to request RX IRQ %d "
267 "for McBSP%d\n", mcbsp->rx_irq,
268 mcbsp->id);
269 free_irq(mcbsp->tx_irq, (void *)mcbsp);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100270 return err;
271 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100272 }
273
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100274 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100275}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300276EXPORT_SYMBOL(omap_mcbsp_request);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100277
278void omap_mcbsp_free(unsigned int id)
279{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300280 struct omap_mcbsp *mcbsp;
Stanley.Miao06151152009-01-29 08:57:12 -0800281 int i;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300282
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300283 if (!omap_mcbsp_check_valid_id(id)) {
284 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100285 return;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100286 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300287 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100288
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300289 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
290 mcbsp->pdata->ops->free(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300291
Stanley.Miao06151152009-01-29 08:57:12 -0800292 for (i = mcbsp->num_clks - 1; i >= 0; i--)
293 clk_disable(mcbsp->clks[i]);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100294
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300295 spin_lock(&mcbsp->lock);
296 if (mcbsp->free) {
297 dev_err(mcbsp->dev, "McBSP%d was not reserved\n",
298 mcbsp->id);
299 spin_unlock(&mcbsp->lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100300 return;
301 }
302
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300303 mcbsp->free = 1;
304 spin_unlock(&mcbsp->lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100305
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300306 if (mcbsp->io_type == OMAP_MCBSP_IRQ_IO) {
Tony Lindgren120db2c2006-04-02 17:46:27 +0100307 /* Free IRQs */
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300308 free_irq(mcbsp->rx_irq, (void *)mcbsp);
309 free_irq(mcbsp->tx_irq, (void *)mcbsp);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100310 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100311}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300312EXPORT_SYMBOL(omap_mcbsp_free);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100313
314/*
315 * Here we start the McBSP, by enabling the sample
316 * generator, both transmitter and receivers,
317 * and the frame sync.
318 */
319void omap_mcbsp_start(unsigned int id)
320{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300321 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100322 void __iomem *io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100323 u16 w;
324
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300325 if (!omap_mcbsp_check_valid_id(id)) {
326 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100327 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300328 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300329 mcbsp = id_to_mcbsp_ptr(id);
330 io_base = mcbsp->io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100331
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300332 mcbsp->rx_word_length = (OMAP_MCBSP_READ(io_base, RCR1) >> 5) & 0x7;
333 mcbsp->tx_word_length = (OMAP_MCBSP_READ(io_base, XCR1) >> 5) & 0x7;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100334
335 /* Start the sample generator */
336 w = OMAP_MCBSP_READ(io_base, SPCR2);
337 OMAP_MCBSP_WRITE(io_base, SPCR2, w | (1 << 6));
338
339 /* Enable transmitter and receiver */
340 w = OMAP_MCBSP_READ(io_base, SPCR2);
341 OMAP_MCBSP_WRITE(io_base, SPCR2, w | 1);
342
343 w = OMAP_MCBSP_READ(io_base, SPCR1);
344 OMAP_MCBSP_WRITE(io_base, SPCR1, w | 1);
345
346 udelay(100);
347
348 /* Start frame sync */
349 w = OMAP_MCBSP_READ(io_base, SPCR2);
350 OMAP_MCBSP_WRITE(io_base, SPCR2, w | (1 << 7));
351
352 /* Dump McBSP Regs */
353 omap_mcbsp_dump_reg(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100354}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300355EXPORT_SYMBOL(omap_mcbsp_start);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100356
357void omap_mcbsp_stop(unsigned int id)
358{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300359 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100360 void __iomem *io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100361 u16 w;
362
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300363 if (!omap_mcbsp_check_valid_id(id)) {
364 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100365 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300366 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100367
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300368 mcbsp = id_to_mcbsp_ptr(id);
369 io_base = mcbsp->io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100370
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300371 /* Reset transmitter */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100372 w = OMAP_MCBSP_READ(io_base, SPCR2);
373 OMAP_MCBSP_WRITE(io_base, SPCR2, w & ~(1));
374
375 /* Reset receiver */
376 w = OMAP_MCBSP_READ(io_base, SPCR1);
377 OMAP_MCBSP_WRITE(io_base, SPCR1, w & ~(1));
378
379 /* Reset the sample rate generator */
380 w = OMAP_MCBSP_READ(io_base, SPCR2);
381 OMAP_MCBSP_WRITE(io_base, SPCR2, w & ~(1 << 6));
382}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300383EXPORT_SYMBOL(omap_mcbsp_stop);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100384
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100385/* polled mcbsp i/o operations */
386int omap_mcbsp_pollwrite(unsigned int id, u16 buf)
387{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300388 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100389 void __iomem *base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300390
391 if (!omap_mcbsp_check_valid_id(id)) {
392 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
393 return -ENODEV;
394 }
395
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300396 mcbsp = id_to_mcbsp_ptr(id);
397 base = mcbsp->io_base;
398
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100399 writew(buf, base + OMAP_MCBSP_REG_DXR1);
400 /* if frame sync error - clear the error */
401 if (readw(base + OMAP_MCBSP_REG_SPCR2) & XSYNC_ERR) {
402 /* clear error */
403 writew(readw(base + OMAP_MCBSP_REG_SPCR2) & (~XSYNC_ERR),
404 base + OMAP_MCBSP_REG_SPCR2);
405 /* resend */
406 return -1;
407 } else {
408 /* wait for transmit confirmation */
409 int attemps = 0;
410 while (!(readw(base + OMAP_MCBSP_REG_SPCR2) & XRDY)) {
411 if (attemps++ > 1000) {
412 writew(readw(base + OMAP_MCBSP_REG_SPCR2) &
413 (~XRST),
414 base + OMAP_MCBSP_REG_SPCR2);
415 udelay(10);
416 writew(readw(base + OMAP_MCBSP_REG_SPCR2) |
417 (XRST),
418 base + OMAP_MCBSP_REG_SPCR2);
419 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300420 dev_err(mcbsp->dev, "Could not write to"
421 " McBSP%d Register\n", mcbsp->id);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100422 return -2;
423 }
424 }
425 }
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300426
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100427 return 0;
428}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300429EXPORT_SYMBOL(omap_mcbsp_pollwrite);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100430
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300431int omap_mcbsp_pollread(unsigned int id, u16 *buf)
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100432{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300433 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100434 void __iomem *base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300435
436 if (!omap_mcbsp_check_valid_id(id)) {
437 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
438 return -ENODEV;
439 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300440 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300441
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300442 base = mcbsp->io_base;
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100443 /* if frame sync error - clear the error */
444 if (readw(base + OMAP_MCBSP_REG_SPCR1) & RSYNC_ERR) {
445 /* clear error */
446 writew(readw(base + OMAP_MCBSP_REG_SPCR1) & (~RSYNC_ERR),
447 base + OMAP_MCBSP_REG_SPCR1);
448 /* resend */
449 return -1;
450 } else {
451 /* wait for recieve confirmation */
452 int attemps = 0;
453 while (!(readw(base + OMAP_MCBSP_REG_SPCR1) & RRDY)) {
454 if (attemps++ > 1000) {
455 writew(readw(base + OMAP_MCBSP_REG_SPCR1) &
456 (~RRST),
457 base + OMAP_MCBSP_REG_SPCR1);
458 udelay(10);
459 writew(readw(base + OMAP_MCBSP_REG_SPCR1) |
460 (RRST),
461 base + OMAP_MCBSP_REG_SPCR1);
462 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300463 dev_err(mcbsp->dev, "Could not read from"
464 " McBSP%d Register\n", mcbsp->id);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100465 return -2;
466 }
467 }
468 }
469 *buf = readw(base + OMAP_MCBSP_REG_DRR1);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300470
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100471 return 0;
472}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300473EXPORT_SYMBOL(omap_mcbsp_pollread);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100474
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100475/*
476 * IRQ based word transmission.
477 */
478void omap_mcbsp_xmit_word(unsigned int id, u32 word)
479{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300480 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100481 void __iomem *io_base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300482 omap_mcbsp_word_length word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100483
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300484 if (!omap_mcbsp_check_valid_id(id)) {
485 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100486 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300487 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100488
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300489 mcbsp = id_to_mcbsp_ptr(id);
490 io_base = mcbsp->io_base;
491 word_length = mcbsp->tx_word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100492
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300493 wait_for_completion(&mcbsp->tx_irq_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100494
495 if (word_length > OMAP_MCBSP_WORD_16)
496 OMAP_MCBSP_WRITE(io_base, DXR2, word >> 16);
497 OMAP_MCBSP_WRITE(io_base, DXR1, word & 0xffff);
498}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300499EXPORT_SYMBOL(omap_mcbsp_xmit_word);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100500
501u32 omap_mcbsp_recv_word(unsigned int id)
502{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300503 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100504 void __iomem *io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100505 u16 word_lsb, word_msb = 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300506 omap_mcbsp_word_length word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100507
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300508 if (!omap_mcbsp_check_valid_id(id)) {
509 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
510 return -ENODEV;
511 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300512 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100513
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300514 word_length = mcbsp->rx_word_length;
515 io_base = mcbsp->io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100516
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300517 wait_for_completion(&mcbsp->rx_irq_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100518
519 if (word_length > OMAP_MCBSP_WORD_16)
520 word_msb = OMAP_MCBSP_READ(io_base, DRR2);
521 word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
522
523 return (word_lsb | (word_msb << 16));
524}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300525EXPORT_SYMBOL(omap_mcbsp_recv_word);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100526
Tony Lindgren120db2c2006-04-02 17:46:27 +0100527int omap_mcbsp_spi_master_xmit_word_poll(unsigned int id, u32 word)
528{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300529 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100530 void __iomem *io_base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300531 omap_mcbsp_word_length tx_word_length;
532 omap_mcbsp_word_length rx_word_length;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100533 u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
534
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300535 if (!omap_mcbsp_check_valid_id(id)) {
536 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
537 return -ENODEV;
538 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300539 mcbsp = id_to_mcbsp_ptr(id);
540 io_base = mcbsp->io_base;
541 tx_word_length = mcbsp->tx_word_length;
542 rx_word_length = mcbsp->rx_word_length;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300543
Tony Lindgren120db2c2006-04-02 17:46:27 +0100544 if (tx_word_length != rx_word_length)
545 return -EINVAL;
546
547 /* First we wait for the transmitter to be ready */
548 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
549 while (!(spcr2 & XRDY)) {
550 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
551 if (attempts++ > 1000) {
552 /* We must reset the transmitter */
553 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 & (~XRST));
554 udelay(10);
555 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 | XRST);
556 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300557 dev_err(mcbsp->dev, "McBSP%d transmitter not "
558 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100559 return -EAGAIN;
560 }
561 }
562
563 /* Now we can push the data */
564 if (tx_word_length > OMAP_MCBSP_WORD_16)
565 OMAP_MCBSP_WRITE(io_base, DXR2, word >> 16);
566 OMAP_MCBSP_WRITE(io_base, DXR1, word & 0xffff);
567
568 /* We wait for the receiver to be ready */
569 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
570 while (!(spcr1 & RRDY)) {
571 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
572 if (attempts++ > 1000) {
573 /* We must reset the receiver */
574 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 & (~RRST));
575 udelay(10);
576 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 | RRST);
577 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300578 dev_err(mcbsp->dev, "McBSP%d receiver not "
579 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100580 return -EAGAIN;
581 }
582 }
583
584 /* Receiver is ready, let's read the dummy data */
585 if (rx_word_length > OMAP_MCBSP_WORD_16)
586 word_msb = OMAP_MCBSP_READ(io_base, DRR2);
587 word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
588
589 return 0;
590}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300591EXPORT_SYMBOL(omap_mcbsp_spi_master_xmit_word_poll);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100592
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300593int omap_mcbsp_spi_master_recv_word_poll(unsigned int id, u32 *word)
Tony Lindgren120db2c2006-04-02 17:46:27 +0100594{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300595 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100596 u32 clock_word = 0;
597 void __iomem *io_base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300598 omap_mcbsp_word_length tx_word_length;
599 omap_mcbsp_word_length rx_word_length;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100600 u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
601
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300602 if (!omap_mcbsp_check_valid_id(id)) {
603 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
604 return -ENODEV;
605 }
606
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300607 mcbsp = id_to_mcbsp_ptr(id);
608 io_base = mcbsp->io_base;
609
610 tx_word_length = mcbsp->tx_word_length;
611 rx_word_length = mcbsp->rx_word_length;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300612
Tony Lindgren120db2c2006-04-02 17:46:27 +0100613 if (tx_word_length != rx_word_length)
614 return -EINVAL;
615
616 /* First we wait for the transmitter to be ready */
617 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
618 while (!(spcr2 & XRDY)) {
619 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
620 if (attempts++ > 1000) {
621 /* We must reset the transmitter */
622 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 & (~XRST));
623 udelay(10);
624 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 | XRST);
625 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300626 dev_err(mcbsp->dev, "McBSP%d transmitter not "
627 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100628 return -EAGAIN;
629 }
630 }
631
632 /* We first need to enable the bus clock */
633 if (tx_word_length > OMAP_MCBSP_WORD_16)
634 OMAP_MCBSP_WRITE(io_base, DXR2, clock_word >> 16);
635 OMAP_MCBSP_WRITE(io_base, DXR1, clock_word & 0xffff);
636
637 /* We wait for the receiver to be ready */
638 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
639 while (!(spcr1 & RRDY)) {
640 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
641 if (attempts++ > 1000) {
642 /* We must reset the receiver */
643 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 & (~RRST));
644 udelay(10);
645 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 | RRST);
646 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300647 dev_err(mcbsp->dev, "McBSP%d receiver not "
648 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100649 return -EAGAIN;
650 }
651 }
652
653 /* Receiver is ready, there is something for us */
654 if (rx_word_length > OMAP_MCBSP_WORD_16)
655 word_msb = OMAP_MCBSP_READ(io_base, DRR2);
656 word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
657
658 word[0] = (word_lsb | (word_msb << 16));
659
660 return 0;
661}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300662EXPORT_SYMBOL(omap_mcbsp_spi_master_recv_word_poll);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100663
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100664/*
665 * Simple DMA based buffer rx/tx routines.
666 * Nothing fancy, just a single buffer tx/rx through DMA.
667 * The DMA resources are released once the transfer is done.
668 * For anything fancier, you should use your own customized DMA
669 * routines and callbacks.
670 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300671int omap_mcbsp_xmit_buffer(unsigned int id, dma_addr_t buffer,
672 unsigned int length)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100673{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300674 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100675 int dma_tx_ch;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100676 int src_port = 0;
677 int dest_port = 0;
678 int sync_dev = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100679
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300680 if (!omap_mcbsp_check_valid_id(id)) {
681 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
682 return -ENODEV;
683 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300684 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100685
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300686 if (omap_request_dma(mcbsp->dma_tx_sync, "McBSP TX",
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300687 omap_mcbsp_tx_dma_callback,
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300688 mcbsp,
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300689 &dma_tx_ch)) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300690 dev_err(mcbsp->dev, " Unable to request DMA channel for "
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300691 "McBSP%d TX. Trying IRQ based TX\n",
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300692 mcbsp->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100693 return -EAGAIN;
694 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300695 mcbsp->dma_tx_lch = dma_tx_ch;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100696
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300697 dev_err(mcbsp->dev, "McBSP%d TX DMA on channel %d\n", mcbsp->id,
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300698 dma_tx_ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100699
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300700 init_completion(&mcbsp->tx_dma_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100701
Tony Lindgren120db2c2006-04-02 17:46:27 +0100702 if (cpu_class_is_omap1()) {
703 src_port = OMAP_DMA_PORT_TIPB;
704 dest_port = OMAP_DMA_PORT_EMIFF;
705 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300706 if (cpu_class_is_omap2())
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300707 sync_dev = mcbsp->dma_tx_sync;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100708
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300709 omap_set_dma_transfer_params(mcbsp->dma_tx_lch,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100710 OMAP_DMA_DATA_TYPE_S16,
711 length >> 1, 1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000712 OMAP_DMA_SYNC_ELEMENT,
Tony Lindgren120db2c2006-04-02 17:46:27 +0100713 sync_dev, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100714
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300715 omap_set_dma_dest_params(mcbsp->dma_tx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +0100716 src_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100717 OMAP_DMA_AMODE_CONSTANT,
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300718 mcbsp->phys_base + OMAP_MCBSP_REG_DXR1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000719 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100720
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300721 omap_set_dma_src_params(mcbsp->dma_tx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +0100722 dest_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100723 OMAP_DMA_AMODE_POST_INC,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000724 buffer,
725 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100726
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300727 omap_start_dma(mcbsp->dma_tx_lch);
728 wait_for_completion(&mcbsp->tx_dma_completion);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300729
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100730 return 0;
731}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300732EXPORT_SYMBOL(omap_mcbsp_xmit_buffer);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100733
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300734int omap_mcbsp_recv_buffer(unsigned int id, dma_addr_t buffer,
735 unsigned int length)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100736{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300737 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100738 int dma_rx_ch;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100739 int src_port = 0;
740 int dest_port = 0;
741 int sync_dev = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100742
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300743 if (!omap_mcbsp_check_valid_id(id)) {
744 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
745 return -ENODEV;
746 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300747 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100748
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300749 if (omap_request_dma(mcbsp->dma_rx_sync, "McBSP RX",
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300750 omap_mcbsp_rx_dma_callback,
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300751 mcbsp,
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300752 &dma_rx_ch)) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300753 dev_err(mcbsp->dev, "Unable to request DMA channel for "
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300754 "McBSP%d RX. Trying IRQ based RX\n",
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300755 mcbsp->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100756 return -EAGAIN;
757 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300758 mcbsp->dma_rx_lch = dma_rx_ch;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100759
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300760 dev_err(mcbsp->dev, "McBSP%d RX DMA on channel %d\n", mcbsp->id,
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300761 dma_rx_ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100762
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300763 init_completion(&mcbsp->rx_dma_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100764
Tony Lindgren120db2c2006-04-02 17:46:27 +0100765 if (cpu_class_is_omap1()) {
766 src_port = OMAP_DMA_PORT_TIPB;
767 dest_port = OMAP_DMA_PORT_EMIFF;
768 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300769 if (cpu_class_is_omap2())
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300770 sync_dev = mcbsp->dma_rx_sync;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100771
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300772 omap_set_dma_transfer_params(mcbsp->dma_rx_lch,
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300773 OMAP_DMA_DATA_TYPE_S16,
774 length >> 1, 1,
775 OMAP_DMA_SYNC_ELEMENT,
776 sync_dev, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100777
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300778 omap_set_dma_src_params(mcbsp->dma_rx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +0100779 src_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100780 OMAP_DMA_AMODE_CONSTANT,
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300781 mcbsp->phys_base + OMAP_MCBSP_REG_DRR1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000782 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100783
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300784 omap_set_dma_dest_params(mcbsp->dma_rx_lch,
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300785 dest_port,
786 OMAP_DMA_AMODE_POST_INC,
787 buffer,
788 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100789
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300790 omap_start_dma(mcbsp->dma_rx_lch);
791 wait_for_completion(&mcbsp->rx_dma_completion);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300792
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100793 return 0;
794}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300795EXPORT_SYMBOL(omap_mcbsp_recv_buffer);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100796
797/*
798 * SPI wrapper.
799 * Since SPI setup is much simpler than the generic McBSP one,
800 * this wrapper just need an omap_mcbsp_spi_cfg structure as an input.
801 * Once this is done, you can call omap_mcbsp_start().
802 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300803void omap_mcbsp_set_spi_mode(unsigned int id,
804 const struct omap_mcbsp_spi_cfg *spi_cfg)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100805{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300806 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100807 struct omap_mcbsp_reg_cfg mcbsp_cfg;
808
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300809 if (!omap_mcbsp_check_valid_id(id)) {
810 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100811 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300812 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300813 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100814
815 memset(&mcbsp_cfg, 0, sizeof(struct omap_mcbsp_reg_cfg));
816
817 /* SPI has only one frame */
818 mcbsp_cfg.rcr1 |= (RWDLEN1(spi_cfg->word_length) | RFRLEN1(0));
819 mcbsp_cfg.xcr1 |= (XWDLEN1(spi_cfg->word_length) | XFRLEN1(0));
820
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300821 /* Clock stop mode */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100822 if (spi_cfg->clk_stp_mode == OMAP_MCBSP_CLK_STP_MODE_NO_DELAY)
823 mcbsp_cfg.spcr1 |= (1 << 12);
824 else
825 mcbsp_cfg.spcr1 |= (3 << 11);
826
827 /* Set clock parities */
828 if (spi_cfg->rx_clock_polarity == OMAP_MCBSP_CLK_RISING)
829 mcbsp_cfg.pcr0 |= CLKRP;
830 else
831 mcbsp_cfg.pcr0 &= ~CLKRP;
832
833 if (spi_cfg->tx_clock_polarity == OMAP_MCBSP_CLK_RISING)
834 mcbsp_cfg.pcr0 &= ~CLKXP;
835 else
836 mcbsp_cfg.pcr0 |= CLKXP;
837
838 /* Set SCLKME to 0 and CLKSM to 1 */
839 mcbsp_cfg.pcr0 &= ~SCLKME;
840 mcbsp_cfg.srgr2 |= CLKSM;
841
842 /* Set FSXP */
843 if (spi_cfg->fsx_polarity == OMAP_MCBSP_FS_ACTIVE_HIGH)
844 mcbsp_cfg.pcr0 &= ~FSXP;
845 else
846 mcbsp_cfg.pcr0 |= FSXP;
847
848 if (spi_cfg->spi_mode == OMAP_MCBSP_SPI_MASTER) {
849 mcbsp_cfg.pcr0 |= CLKXM;
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300850 mcbsp_cfg.srgr1 |= CLKGDV(spi_cfg->clk_div - 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100851 mcbsp_cfg.pcr0 |= FSXM;
852 mcbsp_cfg.srgr2 &= ~FSGM;
853 mcbsp_cfg.xcr2 |= XDATDLY(1);
854 mcbsp_cfg.rcr2 |= RDATDLY(1);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300855 } else {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100856 mcbsp_cfg.pcr0 &= ~CLKXM;
857 mcbsp_cfg.srgr1 |= CLKGDV(1);
858 mcbsp_cfg.pcr0 &= ~FSXM;
859 mcbsp_cfg.xcr2 &= ~XDATDLY(3);
860 mcbsp_cfg.rcr2 &= ~RDATDLY(3);
861 }
862
863 mcbsp_cfg.xcr2 &= ~XPHASE;
864 mcbsp_cfg.rcr2 &= ~RPHASE;
865
866 omap_mcbsp_config(id, &mcbsp_cfg);
867}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300868EXPORT_SYMBOL(omap_mcbsp_set_spi_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100869
870/*
871 * McBSP1 and McBSP3 are directly mapped on 1610 and 1510.
872 * 730 has only 2 McBSP, and both of them are MPU peripherals.
873 */
Uwe Kleine-König25cef222008-10-08 10:01:39 +0300874static int __devinit omap_mcbsp_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100875{
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300876 struct omap_mcbsp_platform_data *pdata = pdev->dev.platform_data;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300877 struct omap_mcbsp *mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300878 int id = pdev->id - 1;
Stanley.Miao06151152009-01-29 08:57:12 -0800879 int i;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300880 int ret = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100881
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300882 if (!pdata) {
883 dev_err(&pdev->dev, "McBSP device initialized without"
884 "platform data\n");
885 ret = -EINVAL;
886 goto exit;
887 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100888
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300889 dev_dbg(&pdev->dev, "Initializing OMAP McBSP (%d).\n", pdev->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100890
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300891 if (id >= omap_mcbsp_count) {
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300892 dev_err(&pdev->dev, "Invalid McBSP device id (%d)\n", id);
893 ret = -EINVAL;
894 goto exit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100895 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100896
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300897 mcbsp = kzalloc(sizeof(struct omap_mcbsp), GFP_KERNEL);
898 if (!mcbsp) {
899 ret = -ENOMEM;
900 goto exit;
901 }
902 mcbsp_ptr[id] = mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300903
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300904 spin_lock_init(&mcbsp->lock);
905 mcbsp->id = id + 1;
906 mcbsp->free = 1;
907 mcbsp->dma_tx_lch = -1;
908 mcbsp->dma_rx_lch = -1;
909
910 mcbsp->phys_base = pdata->phys_base;
911 mcbsp->io_base = ioremap(pdata->phys_base, SZ_4K);
912 if (!mcbsp->io_base) {
Russell Kingd592dd12008-09-04 14:25:42 +0100913 ret = -ENOMEM;
914 goto err_ioremap;
915 }
916
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300917 /* Default I/O is IRQ based */
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300918 mcbsp->io_type = OMAP_MCBSP_IRQ_IO;
919 mcbsp->tx_irq = pdata->tx_irq;
920 mcbsp->rx_irq = pdata->rx_irq;
921 mcbsp->dma_rx_sync = pdata->dma_rx_sync;
922 mcbsp->dma_tx_sync = pdata->dma_tx_sync;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300923
Stanley.Miao06151152009-01-29 08:57:12 -0800924 if (pdata->num_clks) {
925 mcbsp->num_clks = pdata->num_clks;
926 mcbsp->clks = kzalloc(mcbsp->num_clks * sizeof(struct clk *),
927 GFP_KERNEL);
928 if (!mcbsp->clks) {
929 ret = -ENOMEM;
930 goto exit;
931 }
932 for (i = 0; i < mcbsp->num_clks; i++) {
933 mcbsp->clks[i] = clk_get(&pdev->dev, pdata->clk_names[i]);
934 if (IS_ERR(mcbsp->clks[i])) {
935 dev_err(&pdev->dev,
936 "Invalid %s configuration for McBSP%d.\n",
937 pdata->clk_names[i], mcbsp->id);
938 ret = PTR_ERR(mcbsp->clks[i]);
939 goto err_clk;
940 }
941 }
942
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300943 }
944
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300945 mcbsp->pdata = pdata;
946 mcbsp->dev = &pdev->dev;
947 platform_set_drvdata(pdev, mcbsp);
Russell Kingd592dd12008-09-04 14:25:42 +0100948 return 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300949
Russell Kingd592dd12008-09-04 14:25:42 +0100950err_clk:
Stanley.Miao06151152009-01-29 08:57:12 -0800951 while (i--)
952 clk_put(mcbsp->clks[i]);
953 kfree(mcbsp->clks);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300954 iounmap(mcbsp->io_base);
Russell Kingd592dd12008-09-04 14:25:42 +0100955err_ioremap:
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300956 mcbsp->free = 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300957exit:
958 return ret;
959}
960
Uwe Kleine-König25cef222008-10-08 10:01:39 +0300961static int __devexit omap_mcbsp_remove(struct platform_device *pdev)
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300962{
963 struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev);
Stanley.Miao06151152009-01-29 08:57:12 -0800964 int i;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300965
966 platform_set_drvdata(pdev, NULL);
967 if (mcbsp) {
968
969 if (mcbsp->pdata && mcbsp->pdata->ops &&
970 mcbsp->pdata->ops->free)
971 mcbsp->pdata->ops->free(mcbsp->id);
972
Stanley.Miao06151152009-01-29 08:57:12 -0800973 for (i = mcbsp->num_clks - 1; i >= 0; i--) {
974 clk_disable(mcbsp->clks[i]);
975 clk_put(mcbsp->clks[i]);
976 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300977
Russell Kingd592dd12008-09-04 14:25:42 +0100978 iounmap(mcbsp->io_base);
979
Stanley.Miao06151152009-01-29 08:57:12 -0800980 if (mcbsp->num_clks) {
981 kfree(mcbsp->clks);
982 mcbsp->clks = NULL;
983 mcbsp->num_clks = 0;
984 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300985 mcbsp->free = 0;
986 mcbsp->dev = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100987 }
988
989 return 0;
990}
991
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300992static struct platform_driver omap_mcbsp_driver = {
993 .probe = omap_mcbsp_probe,
Uwe Kleine-König25cef222008-10-08 10:01:39 +0300994 .remove = __devexit_p(omap_mcbsp_remove),
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300995 .driver = {
996 .name = "omap-mcbsp",
997 },
998};
999
1000int __init omap_mcbsp_init(void)
1001{
1002 /* Register the McBSP driver */
1003 return platform_driver_register(&omap_mcbsp_driver);
1004}
1005