blob: a3d2313460b3314441f2f1e852daf4816c3387d9 [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;
Eero Nurkkalad6d834b2009-05-25 11:08:42 -070094 u16 irqst_spcr2;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010095
Eero Nurkkalad6d834b2009-05-25 11:08:42 -070096 irqst_spcr2 = OMAP_MCBSP_READ(mcbsp_tx->io_base, SPCR2);
97 dev_dbg(mcbsp_tx->dev, "TX IRQ callback : 0x%x\n", irqst_spcr2);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +010098
Eero Nurkkalad6d834b2009-05-25 11:08:42 -070099 if (irqst_spcr2 & XSYNC_ERR) {
100 dev_err(mcbsp_tx->dev, "TX Frame Sync Error! : 0x%x\n",
101 irqst_spcr2);
102 /* Writing zero to XSYNC_ERR clears the IRQ */
103 OMAP_MCBSP_WRITE(mcbsp_tx->io_base, SPCR2,
104 irqst_spcr2 & ~(XSYNC_ERR));
105 } else {
106 complete(&mcbsp_tx->tx_irq_completion);
107 }
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300108
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100109 return IRQ_HANDLED;
110}
111
Linus Torvalds0cd61b62006-10-06 10:53:39 -0700112static irqreturn_t omap_mcbsp_rx_irq_handler(int irq, void *dev_id)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100113{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400114 struct omap_mcbsp *mcbsp_rx = dev_id;
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700115 u16 irqst_spcr1;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100116
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700117 irqst_spcr1 = OMAP_MCBSP_READ(mcbsp_rx->io_base, SPCR1);
118 dev_dbg(mcbsp_rx->dev, "RX IRQ callback : 0x%x\n", irqst_spcr1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100119
Eero Nurkkalad6d834b2009-05-25 11:08:42 -0700120 if (irqst_spcr1 & RSYNC_ERR) {
121 dev_err(mcbsp_rx->dev, "RX Frame Sync Error! : 0x%x\n",
122 irqst_spcr1);
123 /* Writing zero to RSYNC_ERR clears the IRQ */
124 OMAP_MCBSP_WRITE(mcbsp_rx->io_base, SPCR1,
125 irqst_spcr1 & ~(RSYNC_ERR));
126 } else {
127 complete(&mcbsp_rx->tx_irq_completion);
128 }
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300129
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100130 return IRQ_HANDLED;
131}
132
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100133static void omap_mcbsp_tx_dma_callback(int lch, u16 ch_status, void *data)
134{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400135 struct omap_mcbsp *mcbsp_dma_tx = data;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100136
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300137 dev_dbg(mcbsp_dma_tx->dev, "TX DMA callback : 0x%x\n",
138 OMAP_MCBSP_READ(mcbsp_dma_tx->io_base, SPCR2));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100139
140 /* We can free the channels */
141 omap_free_dma(mcbsp_dma_tx->dma_tx_lch);
142 mcbsp_dma_tx->dma_tx_lch = -1;
143
144 complete(&mcbsp_dma_tx->tx_dma_completion);
145}
146
147static void omap_mcbsp_rx_dma_callback(int lch, u16 ch_status, void *data)
148{
Jeff Garzike8f2af12007-10-26 05:40:25 -0400149 struct omap_mcbsp *mcbsp_dma_rx = data;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100150
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300151 dev_dbg(mcbsp_dma_rx->dev, "RX DMA callback : 0x%x\n",
152 OMAP_MCBSP_READ(mcbsp_dma_rx->io_base, SPCR2));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100153
154 /* We can free the channels */
155 omap_free_dma(mcbsp_dma_rx->dma_rx_lch);
156 mcbsp_dma_rx->dma_rx_lch = -1;
157
158 complete(&mcbsp_dma_rx->rx_dma_completion);
159}
160
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100161/*
162 * omap_mcbsp_config simply write a config to the
163 * appropriate McBSP.
164 * You either call this function or set the McBSP registers
165 * by yourself before calling omap_mcbsp_start().
166 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300167void omap_mcbsp_config(unsigned int id, const struct omap_mcbsp_reg_cfg *config)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100168{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300169 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100170 void __iomem *io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100171
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300172 if (!omap_mcbsp_check_valid_id(id)) {
173 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
174 return;
175 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300176 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300177
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300178 io_base = mcbsp->io_base;
179 dev_dbg(mcbsp->dev, "Configuring McBSP%d phys_base: 0x%08lx\n",
180 mcbsp->id, mcbsp->phys_base);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100181
182 /* We write the given config */
183 OMAP_MCBSP_WRITE(io_base, SPCR2, config->spcr2);
184 OMAP_MCBSP_WRITE(io_base, SPCR1, config->spcr1);
185 OMAP_MCBSP_WRITE(io_base, RCR2, config->rcr2);
186 OMAP_MCBSP_WRITE(io_base, RCR1, config->rcr1);
187 OMAP_MCBSP_WRITE(io_base, XCR2, config->xcr2);
188 OMAP_MCBSP_WRITE(io_base, XCR1, config->xcr1);
189 OMAP_MCBSP_WRITE(io_base, SRGR2, config->srgr2);
190 OMAP_MCBSP_WRITE(io_base, SRGR1, config->srgr1);
191 OMAP_MCBSP_WRITE(io_base, MCR2, config->mcr2);
192 OMAP_MCBSP_WRITE(io_base, MCR1, config->mcr1);
193 OMAP_MCBSP_WRITE(io_base, PCR0, config->pcr0);
Tony Lindgren3127f8f2009-01-15 13:09:54 +0200194 if (cpu_is_omap2430() || cpu_is_omap34xx()) {
195 OMAP_MCBSP_WRITE(io_base, XCCR, config->xccr);
196 OMAP_MCBSP_WRITE(io_base, RCCR, config->rccr);
197 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100198}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300199EXPORT_SYMBOL(omap_mcbsp_config);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100200
Tony Lindgren120db2c2006-04-02 17:46:27 +0100201/*
202 * We can choose between IRQ based or polled IO.
203 * This needs to be called before omap_mcbsp_request().
204 */
205int omap_mcbsp_set_io_type(unsigned int id, omap_mcbsp_io_type_t io_type)
206{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300207 struct omap_mcbsp *mcbsp;
208
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300209 if (!omap_mcbsp_check_valid_id(id)) {
210 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
211 return -ENODEV;
212 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300213 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100214
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300215 spin_lock(&mcbsp->lock);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100216
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300217 if (!mcbsp->free) {
218 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
219 mcbsp->id);
220 spin_unlock(&mcbsp->lock);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100221 return -EINVAL;
222 }
223
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300224 mcbsp->io_type = io_type;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100225
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300226 spin_unlock(&mcbsp->lock);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100227
228 return 0;
229}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300230EXPORT_SYMBOL(omap_mcbsp_set_io_type);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100231
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100232int omap_mcbsp_request(unsigned int id)
233{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300234 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100235 int err;
236
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300237 if (!omap_mcbsp_check_valid_id(id)) {
238 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
239 return -ENODEV;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100240 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300241 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300242
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300243 spin_lock(&mcbsp->lock);
244 if (!mcbsp->free) {
245 dev_err(mcbsp->dev, "McBSP%d is currently in use\n",
246 mcbsp->id);
247 spin_unlock(&mcbsp->lock);
Russell Kingb820ce42009-01-23 10:26:46 +0000248 return -EBUSY;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100249 }
250
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300251 mcbsp->free = 0;
252 spin_unlock(&mcbsp->lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100253
Russell Kingb820ce42009-01-23 10:26:46 +0000254 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->request)
255 mcbsp->pdata->ops->request(id);
256
257 clk_enable(mcbsp->iclk);
258 clk_enable(mcbsp->fclk);
259
Jarkko Nikula5a070552008-10-08 10:01:41 +0300260 /*
261 * Make sure that transmitter, receiver and sample-rate generator are
262 * not running before activating IRQs.
263 */
264 OMAP_MCBSP_WRITE(mcbsp->io_base, SPCR1, 0);
265 OMAP_MCBSP_WRITE(mcbsp->io_base, SPCR2, 0);
266
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300267 if (mcbsp->io_type == OMAP_MCBSP_IRQ_IO) {
Tony Lindgren120db2c2006-04-02 17:46:27 +0100268 /* We need to get IRQs here */
Jarkko Nikula5a070552008-10-08 10:01:41 +0300269 init_completion(&mcbsp->tx_irq_completion);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300270 err = request_irq(mcbsp->tx_irq, omap_mcbsp_tx_irq_handler,
271 0, "McBSP", (void *)mcbsp);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100272 if (err != 0) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300273 dev_err(mcbsp->dev, "Unable to request TX IRQ %d "
274 "for McBSP%d\n", mcbsp->tx_irq,
275 mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100276 return err;
277 }
278
Jarkko Nikula5a070552008-10-08 10:01:41 +0300279 init_completion(&mcbsp->rx_irq_completion);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300280 err = request_irq(mcbsp->rx_irq, omap_mcbsp_rx_irq_handler,
281 0, "McBSP", (void *)mcbsp);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100282 if (err != 0) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300283 dev_err(mcbsp->dev, "Unable to request RX IRQ %d "
284 "for McBSP%d\n", mcbsp->rx_irq,
285 mcbsp->id);
286 free_irq(mcbsp->tx_irq, (void *)mcbsp);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100287 return err;
288 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100289 }
290
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100291 return 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100292}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300293EXPORT_SYMBOL(omap_mcbsp_request);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100294
295void omap_mcbsp_free(unsigned int id)
296{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300297 struct omap_mcbsp *mcbsp;
298
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300299 if (!omap_mcbsp_check_valid_id(id)) {
300 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100301 return;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100302 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300303 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100304
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300305 if (mcbsp->pdata && mcbsp->pdata->ops && mcbsp->pdata->ops->free)
306 mcbsp->pdata->ops->free(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300307
Russell Kingb820ce42009-01-23 10:26:46 +0000308 clk_disable(mcbsp->fclk);
309 clk_disable(mcbsp->iclk);
310
311 if (mcbsp->io_type == OMAP_MCBSP_IRQ_IO) {
312 /* Free IRQs */
313 free_irq(mcbsp->rx_irq, (void *)mcbsp);
314 free_irq(mcbsp->tx_irq, (void *)mcbsp);
315 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100316
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300317 spin_lock(&mcbsp->lock);
318 if (mcbsp->free) {
319 dev_err(mcbsp->dev, "McBSP%d was not reserved\n",
320 mcbsp->id);
321 spin_unlock(&mcbsp->lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100322 return;
323 }
324
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300325 mcbsp->free = 1;
326 spin_unlock(&mcbsp->lock);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100327}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300328EXPORT_SYMBOL(omap_mcbsp_free);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100329
330/*
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300331 * Here we start the McBSP, by enabling transmitter, receiver or both.
332 * If no transmitter or receiver is active prior calling, then sample-rate
333 * generator and frame sync are started.
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100334 */
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300335void omap_mcbsp_start(unsigned int id, int tx, int rx)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100336{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300337 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100338 void __iomem *io_base;
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300339 int idle;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100340 u16 w;
341
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300342 if (!omap_mcbsp_check_valid_id(id)) {
343 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100344 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300345 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300346 mcbsp = id_to_mcbsp_ptr(id);
347 io_base = mcbsp->io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100348
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300349 mcbsp->rx_word_length = (OMAP_MCBSP_READ(io_base, RCR1) >> 5) & 0x7;
350 mcbsp->tx_word_length = (OMAP_MCBSP_READ(io_base, XCR1) >> 5) & 0x7;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100351
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300352 idle = !((OMAP_MCBSP_READ(io_base, SPCR2) |
353 OMAP_MCBSP_READ(io_base, SPCR1)) & 1);
354
355 if (idle) {
356 /* Start the sample generator */
357 w = OMAP_MCBSP_READ(io_base, SPCR2);
358 OMAP_MCBSP_WRITE(io_base, SPCR2, w | (1 << 6));
359 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100360
361 /* Enable transmitter and receiver */
362 w = OMAP_MCBSP_READ(io_base, SPCR2);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300363 OMAP_MCBSP_WRITE(io_base, SPCR2, w | (tx & 1));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100364
365 w = OMAP_MCBSP_READ(io_base, SPCR1);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300366 OMAP_MCBSP_WRITE(io_base, SPCR1, w | (rx & 1));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100367
368 udelay(100);
369
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300370 if (idle) {
371 /* Start frame sync */
372 w = OMAP_MCBSP_READ(io_base, SPCR2);
373 OMAP_MCBSP_WRITE(io_base, SPCR2, w | (1 << 7));
374 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100375
376 /* Dump McBSP Regs */
377 omap_mcbsp_dump_reg(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100378}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300379EXPORT_SYMBOL(omap_mcbsp_start);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100380
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300381void omap_mcbsp_stop(unsigned int id, int tx, int rx)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100382{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300383 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100384 void __iomem *io_base;
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300385 int idle;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100386 u16 w;
387
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300388 if (!omap_mcbsp_check_valid_id(id)) {
389 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100390 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300391 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100392
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300393 mcbsp = id_to_mcbsp_ptr(id);
394 io_base = mcbsp->io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100395
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300396 /* Reset transmitter */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100397 w = OMAP_MCBSP_READ(io_base, SPCR2);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300398 OMAP_MCBSP_WRITE(io_base, SPCR2, w & ~(tx & 1));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100399
400 /* Reset receiver */
401 w = OMAP_MCBSP_READ(io_base, SPCR1);
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300402 OMAP_MCBSP_WRITE(io_base, SPCR1, w & ~(rx & 1));
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100403
Jarkko Nikulac12abc02009-08-07 09:59:47 +0300404 idle = !((OMAP_MCBSP_READ(io_base, SPCR2) |
405 OMAP_MCBSP_READ(io_base, SPCR1)) & 1);
406
407 if (idle) {
408 /* Reset the sample rate generator */
409 w = OMAP_MCBSP_READ(io_base, SPCR2);
410 OMAP_MCBSP_WRITE(io_base, SPCR2, w & ~(1 << 6));
411 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100412}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300413EXPORT_SYMBOL(omap_mcbsp_stop);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100414
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100415/* polled mcbsp i/o operations */
416int omap_mcbsp_pollwrite(unsigned int id, u16 buf)
417{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300418 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100419 void __iomem *base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300420
421 if (!omap_mcbsp_check_valid_id(id)) {
422 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
423 return -ENODEV;
424 }
425
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300426 mcbsp = id_to_mcbsp_ptr(id);
427 base = mcbsp->io_base;
428
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100429 writew(buf, base + OMAP_MCBSP_REG_DXR1);
430 /* if frame sync error - clear the error */
431 if (readw(base + OMAP_MCBSP_REG_SPCR2) & XSYNC_ERR) {
432 /* clear error */
433 writew(readw(base + OMAP_MCBSP_REG_SPCR2) & (~XSYNC_ERR),
434 base + OMAP_MCBSP_REG_SPCR2);
435 /* resend */
436 return -1;
437 } else {
438 /* wait for transmit confirmation */
439 int attemps = 0;
440 while (!(readw(base + OMAP_MCBSP_REG_SPCR2) & XRDY)) {
441 if (attemps++ > 1000) {
442 writew(readw(base + OMAP_MCBSP_REG_SPCR2) &
443 (~XRST),
444 base + OMAP_MCBSP_REG_SPCR2);
445 udelay(10);
446 writew(readw(base + OMAP_MCBSP_REG_SPCR2) |
447 (XRST),
448 base + OMAP_MCBSP_REG_SPCR2);
449 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300450 dev_err(mcbsp->dev, "Could not write to"
451 " McBSP%d Register\n", mcbsp->id);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100452 return -2;
453 }
454 }
455 }
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300456
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100457 return 0;
458}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300459EXPORT_SYMBOL(omap_mcbsp_pollwrite);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100460
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300461int omap_mcbsp_pollread(unsigned int id, u16 *buf)
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100462{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300463 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100464 void __iomem *base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300465
466 if (!omap_mcbsp_check_valid_id(id)) {
467 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
468 return -ENODEV;
469 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300470 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300471
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300472 base = mcbsp->io_base;
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100473 /* if frame sync error - clear the error */
474 if (readw(base + OMAP_MCBSP_REG_SPCR1) & RSYNC_ERR) {
475 /* clear error */
476 writew(readw(base + OMAP_MCBSP_REG_SPCR1) & (~RSYNC_ERR),
477 base + OMAP_MCBSP_REG_SPCR1);
478 /* resend */
479 return -1;
480 } else {
481 /* wait for recieve confirmation */
482 int attemps = 0;
483 while (!(readw(base + OMAP_MCBSP_REG_SPCR1) & RRDY)) {
484 if (attemps++ > 1000) {
485 writew(readw(base + OMAP_MCBSP_REG_SPCR1) &
486 (~RRST),
487 base + OMAP_MCBSP_REG_SPCR1);
488 udelay(10);
489 writew(readw(base + OMAP_MCBSP_REG_SPCR1) |
490 (RRST),
491 base + OMAP_MCBSP_REG_SPCR1);
492 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300493 dev_err(mcbsp->dev, "Could not read from"
494 " McBSP%d Register\n", mcbsp->id);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100495 return -2;
496 }
497 }
498 }
499 *buf = readw(base + OMAP_MCBSP_REG_DRR1);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300500
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100501 return 0;
502}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300503EXPORT_SYMBOL(omap_mcbsp_pollread);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100504
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100505/*
506 * IRQ based word transmission.
507 */
508void omap_mcbsp_xmit_word(unsigned int id, u32 word)
509{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300510 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100511 void __iomem *io_base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300512 omap_mcbsp_word_length word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100513
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300514 if (!omap_mcbsp_check_valid_id(id)) {
515 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100516 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300517 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100518
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300519 mcbsp = id_to_mcbsp_ptr(id);
520 io_base = mcbsp->io_base;
521 word_length = mcbsp->tx_word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100522
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300523 wait_for_completion(&mcbsp->tx_irq_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100524
525 if (word_length > OMAP_MCBSP_WORD_16)
526 OMAP_MCBSP_WRITE(io_base, DXR2, word >> 16);
527 OMAP_MCBSP_WRITE(io_base, DXR1, word & 0xffff);
528}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300529EXPORT_SYMBOL(omap_mcbsp_xmit_word);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100530
531u32 omap_mcbsp_recv_word(unsigned int id)
532{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300533 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100534 void __iomem *io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100535 u16 word_lsb, word_msb = 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300536 omap_mcbsp_word_length word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100537
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300538 if (!omap_mcbsp_check_valid_id(id)) {
539 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
540 return -ENODEV;
541 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300542 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100543
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300544 word_length = mcbsp->rx_word_length;
545 io_base = mcbsp->io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100546
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300547 wait_for_completion(&mcbsp->rx_irq_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100548
549 if (word_length > OMAP_MCBSP_WORD_16)
550 word_msb = OMAP_MCBSP_READ(io_base, DRR2);
551 word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
552
553 return (word_lsb | (word_msb << 16));
554}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300555EXPORT_SYMBOL(omap_mcbsp_recv_word);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100556
Tony Lindgren120db2c2006-04-02 17:46:27 +0100557int omap_mcbsp_spi_master_xmit_word_poll(unsigned int id, u32 word)
558{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300559 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100560 void __iomem *io_base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300561 omap_mcbsp_word_length tx_word_length;
562 omap_mcbsp_word_length rx_word_length;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100563 u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
564
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300565 if (!omap_mcbsp_check_valid_id(id)) {
566 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
567 return -ENODEV;
568 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300569 mcbsp = id_to_mcbsp_ptr(id);
570 io_base = mcbsp->io_base;
571 tx_word_length = mcbsp->tx_word_length;
572 rx_word_length = mcbsp->rx_word_length;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300573
Tony Lindgren120db2c2006-04-02 17:46:27 +0100574 if (tx_word_length != rx_word_length)
575 return -EINVAL;
576
577 /* First we wait for the transmitter to be ready */
578 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
579 while (!(spcr2 & XRDY)) {
580 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
581 if (attempts++ > 1000) {
582 /* We must reset the transmitter */
583 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 & (~XRST));
584 udelay(10);
585 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 | XRST);
586 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300587 dev_err(mcbsp->dev, "McBSP%d transmitter not "
588 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100589 return -EAGAIN;
590 }
591 }
592
593 /* Now we can push the data */
594 if (tx_word_length > OMAP_MCBSP_WORD_16)
595 OMAP_MCBSP_WRITE(io_base, DXR2, word >> 16);
596 OMAP_MCBSP_WRITE(io_base, DXR1, word & 0xffff);
597
598 /* We wait for the receiver to be ready */
599 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
600 while (!(spcr1 & RRDY)) {
601 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
602 if (attempts++ > 1000) {
603 /* We must reset the receiver */
604 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 & (~RRST));
605 udelay(10);
606 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 | RRST);
607 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300608 dev_err(mcbsp->dev, "McBSP%d receiver not "
609 "ready\n", mcbsp->id);
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{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300625 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100626 u32 clock_word = 0;
627 void __iomem *io_base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300628 omap_mcbsp_word_length tx_word_length;
629 omap_mcbsp_word_length rx_word_length;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100630 u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
631
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300632 if (!omap_mcbsp_check_valid_id(id)) {
633 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
634 return -ENODEV;
635 }
636
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300637 mcbsp = id_to_mcbsp_ptr(id);
638 io_base = mcbsp->io_base;
639
640 tx_word_length = mcbsp->tx_word_length;
641 rx_word_length = mcbsp->rx_word_length;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300642
Tony Lindgren120db2c2006-04-02 17:46:27 +0100643 if (tx_word_length != rx_word_length)
644 return -EINVAL;
645
646 /* First we wait for the transmitter to be ready */
647 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
648 while (!(spcr2 & XRDY)) {
649 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
650 if (attempts++ > 1000) {
651 /* We must reset the transmitter */
652 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 & (~XRST));
653 udelay(10);
654 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 | XRST);
655 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300656 dev_err(mcbsp->dev, "McBSP%d transmitter not "
657 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100658 return -EAGAIN;
659 }
660 }
661
662 /* We first need to enable the bus clock */
663 if (tx_word_length > OMAP_MCBSP_WORD_16)
664 OMAP_MCBSP_WRITE(io_base, DXR2, clock_word >> 16);
665 OMAP_MCBSP_WRITE(io_base, DXR1, clock_word & 0xffff);
666
667 /* We wait for the receiver to be ready */
668 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
669 while (!(spcr1 & RRDY)) {
670 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
671 if (attempts++ > 1000) {
672 /* We must reset the receiver */
673 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 & (~RRST));
674 udelay(10);
675 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 | RRST);
676 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300677 dev_err(mcbsp->dev, "McBSP%d receiver not "
678 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100679 return -EAGAIN;
680 }
681 }
682
683 /* Receiver is ready, there is something for us */
684 if (rx_word_length > OMAP_MCBSP_WORD_16)
685 word_msb = OMAP_MCBSP_READ(io_base, DRR2);
686 word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
687
688 word[0] = (word_lsb | (word_msb << 16));
689
690 return 0;
691}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300692EXPORT_SYMBOL(omap_mcbsp_spi_master_recv_word_poll);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100693
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100694/*
695 * Simple DMA based buffer rx/tx routines.
696 * Nothing fancy, just a single buffer tx/rx through DMA.
697 * The DMA resources are released once the transfer is done.
698 * For anything fancier, you should use your own customized DMA
699 * routines and callbacks.
700 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300701int omap_mcbsp_xmit_buffer(unsigned int id, dma_addr_t buffer,
702 unsigned int length)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100703{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300704 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100705 int dma_tx_ch;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100706 int src_port = 0;
707 int dest_port = 0;
708 int sync_dev = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100709
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300710 if (!omap_mcbsp_check_valid_id(id)) {
711 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
712 return -ENODEV;
713 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300714 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100715
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300716 if (omap_request_dma(mcbsp->dma_tx_sync, "McBSP TX",
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300717 omap_mcbsp_tx_dma_callback,
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300718 mcbsp,
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300719 &dma_tx_ch)) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300720 dev_err(mcbsp->dev, " Unable to request DMA channel for "
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300721 "McBSP%d TX. Trying IRQ based TX\n",
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300722 mcbsp->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100723 return -EAGAIN;
724 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300725 mcbsp->dma_tx_lch = dma_tx_ch;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100726
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300727 dev_err(mcbsp->dev, "McBSP%d TX DMA on channel %d\n", mcbsp->id,
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300728 dma_tx_ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100729
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300730 init_completion(&mcbsp->tx_dma_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100731
Tony Lindgren120db2c2006-04-02 17:46:27 +0100732 if (cpu_class_is_omap1()) {
733 src_port = OMAP_DMA_PORT_TIPB;
734 dest_port = OMAP_DMA_PORT_EMIFF;
735 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300736 if (cpu_class_is_omap2())
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300737 sync_dev = mcbsp->dma_tx_sync;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100738
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300739 omap_set_dma_transfer_params(mcbsp->dma_tx_lch,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100740 OMAP_DMA_DATA_TYPE_S16,
741 length >> 1, 1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000742 OMAP_DMA_SYNC_ELEMENT,
Tony Lindgren120db2c2006-04-02 17:46:27 +0100743 sync_dev, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100744
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300745 omap_set_dma_dest_params(mcbsp->dma_tx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +0100746 src_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100747 OMAP_DMA_AMODE_CONSTANT,
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300748 mcbsp->phys_base + OMAP_MCBSP_REG_DXR1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000749 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100750
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300751 omap_set_dma_src_params(mcbsp->dma_tx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +0100752 dest_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100753 OMAP_DMA_AMODE_POST_INC,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000754 buffer,
755 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100756
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300757 omap_start_dma(mcbsp->dma_tx_lch);
758 wait_for_completion(&mcbsp->tx_dma_completion);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300759
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100760 return 0;
761}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300762EXPORT_SYMBOL(omap_mcbsp_xmit_buffer);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100763
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300764int omap_mcbsp_recv_buffer(unsigned int id, dma_addr_t buffer,
765 unsigned int length)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100766{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300767 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100768 int dma_rx_ch;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100769 int src_port = 0;
770 int dest_port = 0;
771 int sync_dev = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100772
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300773 if (!omap_mcbsp_check_valid_id(id)) {
774 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
775 return -ENODEV;
776 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300777 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100778
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300779 if (omap_request_dma(mcbsp->dma_rx_sync, "McBSP RX",
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300780 omap_mcbsp_rx_dma_callback,
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300781 mcbsp,
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300782 &dma_rx_ch)) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300783 dev_err(mcbsp->dev, "Unable to request DMA channel for "
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300784 "McBSP%d RX. Trying IRQ based RX\n",
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300785 mcbsp->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100786 return -EAGAIN;
787 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300788 mcbsp->dma_rx_lch = dma_rx_ch;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100789
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300790 dev_err(mcbsp->dev, "McBSP%d RX DMA on channel %d\n", mcbsp->id,
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300791 dma_rx_ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100792
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300793 init_completion(&mcbsp->rx_dma_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100794
Tony Lindgren120db2c2006-04-02 17:46:27 +0100795 if (cpu_class_is_omap1()) {
796 src_port = OMAP_DMA_PORT_TIPB;
797 dest_port = OMAP_DMA_PORT_EMIFF;
798 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300799 if (cpu_class_is_omap2())
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300800 sync_dev = mcbsp->dma_rx_sync;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100801
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300802 omap_set_dma_transfer_params(mcbsp->dma_rx_lch,
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300803 OMAP_DMA_DATA_TYPE_S16,
804 length >> 1, 1,
805 OMAP_DMA_SYNC_ELEMENT,
806 sync_dev, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100807
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300808 omap_set_dma_src_params(mcbsp->dma_rx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +0100809 src_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100810 OMAP_DMA_AMODE_CONSTANT,
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300811 mcbsp->phys_base + OMAP_MCBSP_REG_DRR1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000812 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100813
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300814 omap_set_dma_dest_params(mcbsp->dma_rx_lch,
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300815 dest_port,
816 OMAP_DMA_AMODE_POST_INC,
817 buffer,
818 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100819
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300820 omap_start_dma(mcbsp->dma_rx_lch);
821 wait_for_completion(&mcbsp->rx_dma_completion);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300822
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100823 return 0;
824}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300825EXPORT_SYMBOL(omap_mcbsp_recv_buffer);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100826
827/*
828 * SPI wrapper.
829 * Since SPI setup is much simpler than the generic McBSP one,
830 * this wrapper just need an omap_mcbsp_spi_cfg structure as an input.
831 * Once this is done, you can call omap_mcbsp_start().
832 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300833void omap_mcbsp_set_spi_mode(unsigned int id,
834 const struct omap_mcbsp_spi_cfg *spi_cfg)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100835{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300836 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100837 struct omap_mcbsp_reg_cfg mcbsp_cfg;
838
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300839 if (!omap_mcbsp_check_valid_id(id)) {
840 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100841 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300842 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300843 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100844
845 memset(&mcbsp_cfg, 0, sizeof(struct omap_mcbsp_reg_cfg));
846
847 /* SPI has only one frame */
848 mcbsp_cfg.rcr1 |= (RWDLEN1(spi_cfg->word_length) | RFRLEN1(0));
849 mcbsp_cfg.xcr1 |= (XWDLEN1(spi_cfg->word_length) | XFRLEN1(0));
850
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300851 /* Clock stop mode */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100852 if (spi_cfg->clk_stp_mode == OMAP_MCBSP_CLK_STP_MODE_NO_DELAY)
853 mcbsp_cfg.spcr1 |= (1 << 12);
854 else
855 mcbsp_cfg.spcr1 |= (3 << 11);
856
857 /* Set clock parities */
858 if (spi_cfg->rx_clock_polarity == OMAP_MCBSP_CLK_RISING)
859 mcbsp_cfg.pcr0 |= CLKRP;
860 else
861 mcbsp_cfg.pcr0 &= ~CLKRP;
862
863 if (spi_cfg->tx_clock_polarity == OMAP_MCBSP_CLK_RISING)
864 mcbsp_cfg.pcr0 &= ~CLKXP;
865 else
866 mcbsp_cfg.pcr0 |= CLKXP;
867
868 /* Set SCLKME to 0 and CLKSM to 1 */
869 mcbsp_cfg.pcr0 &= ~SCLKME;
870 mcbsp_cfg.srgr2 |= CLKSM;
871
872 /* Set FSXP */
873 if (spi_cfg->fsx_polarity == OMAP_MCBSP_FS_ACTIVE_HIGH)
874 mcbsp_cfg.pcr0 &= ~FSXP;
875 else
876 mcbsp_cfg.pcr0 |= FSXP;
877
878 if (spi_cfg->spi_mode == OMAP_MCBSP_SPI_MASTER) {
879 mcbsp_cfg.pcr0 |= CLKXM;
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300880 mcbsp_cfg.srgr1 |= CLKGDV(spi_cfg->clk_div - 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100881 mcbsp_cfg.pcr0 |= FSXM;
882 mcbsp_cfg.srgr2 &= ~FSGM;
883 mcbsp_cfg.xcr2 |= XDATDLY(1);
884 mcbsp_cfg.rcr2 |= RDATDLY(1);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300885 } else {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100886 mcbsp_cfg.pcr0 &= ~CLKXM;
887 mcbsp_cfg.srgr1 |= CLKGDV(1);
888 mcbsp_cfg.pcr0 &= ~FSXM;
889 mcbsp_cfg.xcr2 &= ~XDATDLY(3);
890 mcbsp_cfg.rcr2 &= ~RDATDLY(3);
891 }
892
893 mcbsp_cfg.xcr2 &= ~XPHASE;
894 mcbsp_cfg.rcr2 &= ~RPHASE;
895
896 omap_mcbsp_config(id, &mcbsp_cfg);
897}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300898EXPORT_SYMBOL(omap_mcbsp_set_spi_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100899
900/*
901 * McBSP1 and McBSP3 are directly mapped on 1610 and 1510.
902 * 730 has only 2 McBSP, and both of them are MPU peripherals.
903 */
Uwe Kleine-König25cef222008-10-08 10:01:39 +0300904static int __devinit omap_mcbsp_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100905{
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300906 struct omap_mcbsp_platform_data *pdata = pdev->dev.platform_data;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300907 struct omap_mcbsp *mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300908 int id = pdev->id - 1;
909 int ret = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100910
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300911 if (!pdata) {
912 dev_err(&pdev->dev, "McBSP device initialized without"
913 "platform data\n");
914 ret = -EINVAL;
915 goto exit;
916 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100917
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300918 dev_dbg(&pdev->dev, "Initializing OMAP McBSP (%d).\n", pdev->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100919
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300920 if (id >= omap_mcbsp_count) {
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300921 dev_err(&pdev->dev, "Invalid McBSP device id (%d)\n", id);
922 ret = -EINVAL;
923 goto exit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100924 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100925
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300926 mcbsp = kzalloc(sizeof(struct omap_mcbsp), GFP_KERNEL);
927 if (!mcbsp) {
928 ret = -ENOMEM;
929 goto exit;
930 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300931
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300932 spin_lock_init(&mcbsp->lock);
933 mcbsp->id = id + 1;
934 mcbsp->free = 1;
935 mcbsp->dma_tx_lch = -1;
936 mcbsp->dma_rx_lch = -1;
937
938 mcbsp->phys_base = pdata->phys_base;
939 mcbsp->io_base = ioremap(pdata->phys_base, SZ_4K);
940 if (!mcbsp->io_base) {
Russell Kingd592dd12008-09-04 14:25:42 +0100941 ret = -ENOMEM;
942 goto err_ioremap;
943 }
944
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300945 /* Default I/O is IRQ based */
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300946 mcbsp->io_type = OMAP_MCBSP_IRQ_IO;
947 mcbsp->tx_irq = pdata->tx_irq;
948 mcbsp->rx_irq = pdata->rx_irq;
949 mcbsp->dma_rx_sync = pdata->dma_rx_sync;
950 mcbsp->dma_tx_sync = pdata->dma_tx_sync;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300951
Russell Kingb820ce42009-01-23 10:26:46 +0000952 mcbsp->iclk = clk_get(&pdev->dev, "ick");
953 if (IS_ERR(mcbsp->iclk)) {
954 ret = PTR_ERR(mcbsp->iclk);
955 dev_err(&pdev->dev, "unable to get ick: %d\n", ret);
956 goto err_iclk;
957 }
Stanley.Miao06151152009-01-29 08:57:12 -0800958
Russell Kingb820ce42009-01-23 10:26:46 +0000959 mcbsp->fclk = clk_get(&pdev->dev, "fck");
960 if (IS_ERR(mcbsp->fclk)) {
961 ret = PTR_ERR(mcbsp->fclk);
962 dev_err(&pdev->dev, "unable to get fck: %d\n", ret);
963 goto err_fclk;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300964 }
965
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300966 mcbsp->pdata = pdata;
967 mcbsp->dev = &pdev->dev;
Russell Kingb820ce42009-01-23 10:26:46 +0000968 mcbsp_ptr[id] = mcbsp;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300969 platform_set_drvdata(pdev, mcbsp);
Russell Kingd592dd12008-09-04 14:25:42 +0100970 return 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300971
Russell Kingb820ce42009-01-23 10:26:46 +0000972err_fclk:
973 clk_put(mcbsp->iclk);
974err_iclk:
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300975 iounmap(mcbsp->io_base);
Russell Kingd592dd12008-09-04 14:25:42 +0100976err_ioremap:
Russell Kingb820ce42009-01-23 10:26:46 +0000977 kfree(mcbsp);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300978exit:
979 return ret;
980}
981
Uwe Kleine-König25cef222008-10-08 10:01:39 +0300982static int __devexit omap_mcbsp_remove(struct platform_device *pdev)
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300983{
984 struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev);
985
986 platform_set_drvdata(pdev, NULL);
987 if (mcbsp) {
988
989 if (mcbsp->pdata && mcbsp->pdata->ops &&
990 mcbsp->pdata->ops->free)
991 mcbsp->pdata->ops->free(mcbsp->id);
992
Russell Kingb820ce42009-01-23 10:26:46 +0000993 clk_disable(mcbsp->fclk);
994 clk_disable(mcbsp->iclk);
995 clk_put(mcbsp->fclk);
996 clk_put(mcbsp->iclk);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300997
Russell Kingd592dd12008-09-04 14:25:42 +0100998 iounmap(mcbsp->io_base);
999
Russell Kingb820ce42009-01-23 10:26:46 +00001000 mcbsp->fclk = NULL;
1001 mcbsp->iclk = NULL;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001002 mcbsp->free = 0;
1003 mcbsp->dev = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +01001004 }
1005
1006 return 0;
1007}
1008
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001009static struct platform_driver omap_mcbsp_driver = {
1010 .probe = omap_mcbsp_probe,
Uwe Kleine-König25cef222008-10-08 10:01:39 +03001011 .remove = __devexit_p(omap_mcbsp_remove),
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +03001012 .driver = {
1013 .name = "omap-mcbsp",
1014 },
1015};
1016
1017int __init omap_mcbsp_init(void)
1018{
1019 /* Register the McBSP driver */
1020 return platform_driver_register(&omap_mcbsp_driver);
1021}