blob: efa0e0111f38815f4416634bc31427d786e8fced [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/*
331 * Here we start the McBSP, by enabling the sample
332 * generator, both transmitter and receivers,
333 * and the frame sync.
334 */
335void omap_mcbsp_start(unsigned int id)
336{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300337 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100338 void __iomem *io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100339 u16 w;
340
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300341 if (!omap_mcbsp_check_valid_id(id)) {
342 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100343 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300344 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300345 mcbsp = id_to_mcbsp_ptr(id);
346 io_base = mcbsp->io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100347
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300348 mcbsp->rx_word_length = (OMAP_MCBSP_READ(io_base, RCR1) >> 5) & 0x7;
349 mcbsp->tx_word_length = (OMAP_MCBSP_READ(io_base, XCR1) >> 5) & 0x7;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100350
351 /* Start the sample generator */
352 w = OMAP_MCBSP_READ(io_base, SPCR2);
353 OMAP_MCBSP_WRITE(io_base, SPCR2, w | (1 << 6));
354
355 /* Enable transmitter and receiver */
356 w = OMAP_MCBSP_READ(io_base, SPCR2);
357 OMAP_MCBSP_WRITE(io_base, SPCR2, w | 1);
358
359 w = OMAP_MCBSP_READ(io_base, SPCR1);
360 OMAP_MCBSP_WRITE(io_base, SPCR1, w | 1);
361
362 udelay(100);
363
364 /* Start frame sync */
365 w = OMAP_MCBSP_READ(io_base, SPCR2);
366 OMAP_MCBSP_WRITE(io_base, SPCR2, w | (1 << 7));
367
368 /* Dump McBSP Regs */
369 omap_mcbsp_dump_reg(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100370}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300371EXPORT_SYMBOL(omap_mcbsp_start);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100372
373void omap_mcbsp_stop(unsigned int id)
374{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300375 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100376 void __iomem *io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100377 u16 w;
378
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300379 if (!omap_mcbsp_check_valid_id(id)) {
380 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100381 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300382 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100383
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300384 mcbsp = id_to_mcbsp_ptr(id);
385 io_base = mcbsp->io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100386
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300387 /* Reset transmitter */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100388 w = OMAP_MCBSP_READ(io_base, SPCR2);
389 OMAP_MCBSP_WRITE(io_base, SPCR2, w & ~(1));
390
391 /* Reset receiver */
392 w = OMAP_MCBSP_READ(io_base, SPCR1);
393 OMAP_MCBSP_WRITE(io_base, SPCR1, w & ~(1));
394
395 /* Reset the sample rate generator */
396 w = OMAP_MCBSP_READ(io_base, SPCR2);
397 OMAP_MCBSP_WRITE(io_base, SPCR2, w & ~(1 << 6));
398}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300399EXPORT_SYMBOL(omap_mcbsp_stop);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100400
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100401/* polled mcbsp i/o operations */
402int omap_mcbsp_pollwrite(unsigned int id, u16 buf)
403{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300404 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100405 void __iomem *base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300406
407 if (!omap_mcbsp_check_valid_id(id)) {
408 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
409 return -ENODEV;
410 }
411
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300412 mcbsp = id_to_mcbsp_ptr(id);
413 base = mcbsp->io_base;
414
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100415 writew(buf, base + OMAP_MCBSP_REG_DXR1);
416 /* if frame sync error - clear the error */
417 if (readw(base + OMAP_MCBSP_REG_SPCR2) & XSYNC_ERR) {
418 /* clear error */
419 writew(readw(base + OMAP_MCBSP_REG_SPCR2) & (~XSYNC_ERR),
420 base + OMAP_MCBSP_REG_SPCR2);
421 /* resend */
422 return -1;
423 } else {
424 /* wait for transmit confirmation */
425 int attemps = 0;
426 while (!(readw(base + OMAP_MCBSP_REG_SPCR2) & XRDY)) {
427 if (attemps++ > 1000) {
428 writew(readw(base + OMAP_MCBSP_REG_SPCR2) &
429 (~XRST),
430 base + OMAP_MCBSP_REG_SPCR2);
431 udelay(10);
432 writew(readw(base + OMAP_MCBSP_REG_SPCR2) |
433 (XRST),
434 base + OMAP_MCBSP_REG_SPCR2);
435 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300436 dev_err(mcbsp->dev, "Could not write to"
437 " McBSP%d Register\n", mcbsp->id);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100438 return -2;
439 }
440 }
441 }
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300442
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100443 return 0;
444}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300445EXPORT_SYMBOL(omap_mcbsp_pollwrite);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100446
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300447int omap_mcbsp_pollread(unsigned int id, u16 *buf)
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100448{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300449 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100450 void __iomem *base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300451
452 if (!omap_mcbsp_check_valid_id(id)) {
453 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
454 return -ENODEV;
455 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300456 mcbsp = id_to_mcbsp_ptr(id);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300457
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300458 base = mcbsp->io_base;
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100459 /* if frame sync error - clear the error */
460 if (readw(base + OMAP_MCBSP_REG_SPCR1) & RSYNC_ERR) {
461 /* clear error */
462 writew(readw(base + OMAP_MCBSP_REG_SPCR1) & (~RSYNC_ERR),
463 base + OMAP_MCBSP_REG_SPCR1);
464 /* resend */
465 return -1;
466 } else {
467 /* wait for recieve confirmation */
468 int attemps = 0;
469 while (!(readw(base + OMAP_MCBSP_REG_SPCR1) & RRDY)) {
470 if (attemps++ > 1000) {
471 writew(readw(base + OMAP_MCBSP_REG_SPCR1) &
472 (~RRST),
473 base + OMAP_MCBSP_REG_SPCR1);
474 udelay(10);
475 writew(readw(base + OMAP_MCBSP_REG_SPCR1) |
476 (RRST),
477 base + OMAP_MCBSP_REG_SPCR1);
478 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300479 dev_err(mcbsp->dev, "Could not read from"
480 " McBSP%d Register\n", mcbsp->id);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100481 return -2;
482 }
483 }
484 }
485 *buf = readw(base + OMAP_MCBSP_REG_DRR1);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300486
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100487 return 0;
488}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300489EXPORT_SYMBOL(omap_mcbsp_pollread);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100490
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100491/*
492 * IRQ based word transmission.
493 */
494void omap_mcbsp_xmit_word(unsigned int id, u32 word)
495{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300496 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100497 void __iomem *io_base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300498 omap_mcbsp_word_length word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100499
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300500 if (!omap_mcbsp_check_valid_id(id)) {
501 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100502 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300503 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100504
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300505 mcbsp = id_to_mcbsp_ptr(id);
506 io_base = mcbsp->io_base;
507 word_length = mcbsp->tx_word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100508
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300509 wait_for_completion(&mcbsp->tx_irq_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100510
511 if (word_length > OMAP_MCBSP_WORD_16)
512 OMAP_MCBSP_WRITE(io_base, DXR2, word >> 16);
513 OMAP_MCBSP_WRITE(io_base, DXR1, word & 0xffff);
514}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300515EXPORT_SYMBOL(omap_mcbsp_xmit_word);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100516
517u32 omap_mcbsp_recv_word(unsigned int id)
518{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300519 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100520 void __iomem *io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100521 u16 word_lsb, word_msb = 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300522 omap_mcbsp_word_length word_length;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100523
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300524 if (!omap_mcbsp_check_valid_id(id)) {
525 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
526 return -ENODEV;
527 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300528 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100529
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300530 word_length = mcbsp->rx_word_length;
531 io_base = mcbsp->io_base;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100532
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300533 wait_for_completion(&mcbsp->rx_irq_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100534
535 if (word_length > OMAP_MCBSP_WORD_16)
536 word_msb = OMAP_MCBSP_READ(io_base, DRR2);
537 word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
538
539 return (word_lsb | (word_msb << 16));
540}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300541EXPORT_SYMBOL(omap_mcbsp_recv_word);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100542
Tony Lindgren120db2c2006-04-02 17:46:27 +0100543int omap_mcbsp_spi_master_xmit_word_poll(unsigned int id, u32 word)
544{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300545 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100546 void __iomem *io_base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300547 omap_mcbsp_word_length tx_word_length;
548 omap_mcbsp_word_length rx_word_length;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100549 u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
550
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300551 if (!omap_mcbsp_check_valid_id(id)) {
552 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
553 return -ENODEV;
554 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300555 mcbsp = id_to_mcbsp_ptr(id);
556 io_base = mcbsp->io_base;
557 tx_word_length = mcbsp->tx_word_length;
558 rx_word_length = mcbsp->rx_word_length;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300559
Tony Lindgren120db2c2006-04-02 17:46:27 +0100560 if (tx_word_length != rx_word_length)
561 return -EINVAL;
562
563 /* First we wait for the transmitter to be ready */
564 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
565 while (!(spcr2 & XRDY)) {
566 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
567 if (attempts++ > 1000) {
568 /* We must reset the transmitter */
569 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 & (~XRST));
570 udelay(10);
571 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 | XRST);
572 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300573 dev_err(mcbsp->dev, "McBSP%d transmitter not "
574 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100575 return -EAGAIN;
576 }
577 }
578
579 /* Now we can push the data */
580 if (tx_word_length > OMAP_MCBSP_WORD_16)
581 OMAP_MCBSP_WRITE(io_base, DXR2, word >> 16);
582 OMAP_MCBSP_WRITE(io_base, DXR1, word & 0xffff);
583
584 /* We wait for the receiver to be ready */
585 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
586 while (!(spcr1 & RRDY)) {
587 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
588 if (attempts++ > 1000) {
589 /* We must reset the receiver */
590 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 & (~RRST));
591 udelay(10);
592 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 | RRST);
593 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300594 dev_err(mcbsp->dev, "McBSP%d receiver not "
595 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100596 return -EAGAIN;
597 }
598 }
599
600 /* Receiver is ready, let's read the dummy data */
601 if (rx_word_length > OMAP_MCBSP_WORD_16)
602 word_msb = OMAP_MCBSP_READ(io_base, DRR2);
603 word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
604
605 return 0;
606}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300607EXPORT_SYMBOL(omap_mcbsp_spi_master_xmit_word_poll);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100608
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300609int omap_mcbsp_spi_master_recv_word_poll(unsigned int id, u32 *word)
Tony Lindgren120db2c2006-04-02 17:46:27 +0100610{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300611 struct omap_mcbsp *mcbsp;
Russell Kingd592dd12008-09-04 14:25:42 +0100612 u32 clock_word = 0;
613 void __iomem *io_base;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300614 omap_mcbsp_word_length tx_word_length;
615 omap_mcbsp_word_length rx_word_length;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100616 u16 spcr2, spcr1, attempts = 0, word_lsb, word_msb = 0;
617
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300618 if (!omap_mcbsp_check_valid_id(id)) {
619 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
620 return -ENODEV;
621 }
622
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300623 mcbsp = id_to_mcbsp_ptr(id);
624 io_base = mcbsp->io_base;
625
626 tx_word_length = mcbsp->tx_word_length;
627 rx_word_length = mcbsp->rx_word_length;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300628
Tony Lindgren120db2c2006-04-02 17:46:27 +0100629 if (tx_word_length != rx_word_length)
630 return -EINVAL;
631
632 /* First we wait for the transmitter to be ready */
633 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
634 while (!(spcr2 & XRDY)) {
635 spcr2 = OMAP_MCBSP_READ(io_base, SPCR2);
636 if (attempts++ > 1000) {
637 /* We must reset the transmitter */
638 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 & (~XRST));
639 udelay(10);
640 OMAP_MCBSP_WRITE(io_base, SPCR2, spcr2 | XRST);
641 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300642 dev_err(mcbsp->dev, "McBSP%d transmitter not "
643 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100644 return -EAGAIN;
645 }
646 }
647
648 /* We first need to enable the bus clock */
649 if (tx_word_length > OMAP_MCBSP_WORD_16)
650 OMAP_MCBSP_WRITE(io_base, DXR2, clock_word >> 16);
651 OMAP_MCBSP_WRITE(io_base, DXR1, clock_word & 0xffff);
652
653 /* We wait for the receiver to be ready */
654 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
655 while (!(spcr1 & RRDY)) {
656 spcr1 = OMAP_MCBSP_READ(io_base, SPCR1);
657 if (attempts++ > 1000) {
658 /* We must reset the receiver */
659 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 & (~RRST));
660 udelay(10);
661 OMAP_MCBSP_WRITE(io_base, SPCR1, spcr1 | RRST);
662 udelay(10);
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300663 dev_err(mcbsp->dev, "McBSP%d receiver not "
664 "ready\n", mcbsp->id);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100665 return -EAGAIN;
666 }
667 }
668
669 /* Receiver is ready, there is something for us */
670 if (rx_word_length > OMAP_MCBSP_WORD_16)
671 word_msb = OMAP_MCBSP_READ(io_base, DRR2);
672 word_lsb = OMAP_MCBSP_READ(io_base, DRR1);
673
674 word[0] = (word_lsb | (word_msb << 16));
675
676 return 0;
677}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300678EXPORT_SYMBOL(omap_mcbsp_spi_master_recv_word_poll);
Tony Lindgren120db2c2006-04-02 17:46:27 +0100679
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100680/*
681 * Simple DMA based buffer rx/tx routines.
682 * Nothing fancy, just a single buffer tx/rx through DMA.
683 * The DMA resources are released once the transfer is done.
684 * For anything fancier, you should use your own customized DMA
685 * routines and callbacks.
686 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300687int omap_mcbsp_xmit_buffer(unsigned int id, dma_addr_t buffer,
688 unsigned int length)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100689{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300690 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100691 int dma_tx_ch;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100692 int src_port = 0;
693 int dest_port = 0;
694 int sync_dev = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100695
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300696 if (!omap_mcbsp_check_valid_id(id)) {
697 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
698 return -ENODEV;
699 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300700 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100701
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300702 if (omap_request_dma(mcbsp->dma_tx_sync, "McBSP TX",
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300703 omap_mcbsp_tx_dma_callback,
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300704 mcbsp,
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300705 &dma_tx_ch)) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300706 dev_err(mcbsp->dev, " Unable to request DMA channel for "
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300707 "McBSP%d TX. Trying IRQ based TX\n",
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300708 mcbsp->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100709 return -EAGAIN;
710 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300711 mcbsp->dma_tx_lch = dma_tx_ch;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100712
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300713 dev_err(mcbsp->dev, "McBSP%d TX DMA on channel %d\n", mcbsp->id,
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300714 dma_tx_ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100715
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300716 init_completion(&mcbsp->tx_dma_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100717
Tony Lindgren120db2c2006-04-02 17:46:27 +0100718 if (cpu_class_is_omap1()) {
719 src_port = OMAP_DMA_PORT_TIPB;
720 dest_port = OMAP_DMA_PORT_EMIFF;
721 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300722 if (cpu_class_is_omap2())
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300723 sync_dev = mcbsp->dma_tx_sync;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100724
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300725 omap_set_dma_transfer_params(mcbsp->dma_tx_lch,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100726 OMAP_DMA_DATA_TYPE_S16,
727 length >> 1, 1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000728 OMAP_DMA_SYNC_ELEMENT,
Tony Lindgren120db2c2006-04-02 17:46:27 +0100729 sync_dev, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100730
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300731 omap_set_dma_dest_params(mcbsp->dma_tx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +0100732 src_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100733 OMAP_DMA_AMODE_CONSTANT,
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300734 mcbsp->phys_base + OMAP_MCBSP_REG_DXR1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000735 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100736
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300737 omap_set_dma_src_params(mcbsp->dma_tx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +0100738 dest_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100739 OMAP_DMA_AMODE_POST_INC,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000740 buffer,
741 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100742
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300743 omap_start_dma(mcbsp->dma_tx_lch);
744 wait_for_completion(&mcbsp->tx_dma_completion);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300745
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100746 return 0;
747}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300748EXPORT_SYMBOL(omap_mcbsp_xmit_buffer);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100749
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300750int omap_mcbsp_recv_buffer(unsigned int id, dma_addr_t buffer,
751 unsigned int length)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100752{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300753 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100754 int dma_rx_ch;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100755 int src_port = 0;
756 int dest_port = 0;
757 int sync_dev = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100758
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300759 if (!omap_mcbsp_check_valid_id(id)) {
760 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
761 return -ENODEV;
762 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300763 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100764
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300765 if (omap_request_dma(mcbsp->dma_rx_sync, "McBSP RX",
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300766 omap_mcbsp_rx_dma_callback,
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300767 mcbsp,
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300768 &dma_rx_ch)) {
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300769 dev_err(mcbsp->dev, "Unable to request DMA channel for "
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300770 "McBSP%d RX. Trying IRQ based RX\n",
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300771 mcbsp->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100772 return -EAGAIN;
773 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300774 mcbsp->dma_rx_lch = dma_rx_ch;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100775
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300776 dev_err(mcbsp->dev, "McBSP%d RX DMA on channel %d\n", mcbsp->id,
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300777 dma_rx_ch);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100778
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300779 init_completion(&mcbsp->rx_dma_completion);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100780
Tony Lindgren120db2c2006-04-02 17:46:27 +0100781 if (cpu_class_is_omap1()) {
782 src_port = OMAP_DMA_PORT_TIPB;
783 dest_port = OMAP_DMA_PORT_EMIFF;
784 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300785 if (cpu_class_is_omap2())
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300786 sync_dev = mcbsp->dma_rx_sync;
Tony Lindgren120db2c2006-04-02 17:46:27 +0100787
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300788 omap_set_dma_transfer_params(mcbsp->dma_rx_lch,
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300789 OMAP_DMA_DATA_TYPE_S16,
790 length >> 1, 1,
791 OMAP_DMA_SYNC_ELEMENT,
792 sync_dev, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100793
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300794 omap_set_dma_src_params(mcbsp->dma_rx_lch,
Tony Lindgren120db2c2006-04-02 17:46:27 +0100795 src_port,
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100796 OMAP_DMA_AMODE_CONSTANT,
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300797 mcbsp->phys_base + OMAP_MCBSP_REG_DRR1,
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000798 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100799
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300800 omap_set_dma_dest_params(mcbsp->dma_rx_lch,
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300801 dest_port,
802 OMAP_DMA_AMODE_POST_INC,
803 buffer,
804 0, 0);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100805
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300806 omap_start_dma(mcbsp->dma_rx_lch);
807 wait_for_completion(&mcbsp->rx_dma_completion);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300808
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100809 return 0;
810}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300811EXPORT_SYMBOL(omap_mcbsp_recv_buffer);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100812
813/*
814 * SPI wrapper.
815 * Since SPI setup is much simpler than the generic McBSP one,
816 * this wrapper just need an omap_mcbsp_spi_cfg structure as an input.
817 * Once this is done, you can call omap_mcbsp_start().
818 */
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300819void omap_mcbsp_set_spi_mode(unsigned int id,
820 const struct omap_mcbsp_spi_cfg *spi_cfg)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100821{
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300822 struct omap_mcbsp *mcbsp;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100823 struct omap_mcbsp_reg_cfg mcbsp_cfg;
824
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300825 if (!omap_mcbsp_check_valid_id(id)) {
826 printk(KERN_ERR "%s: Invalid id (%d)\n", __func__, id + 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100827 return;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300828 }
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300829 mcbsp = id_to_mcbsp_ptr(id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100830
831 memset(&mcbsp_cfg, 0, sizeof(struct omap_mcbsp_reg_cfg));
832
833 /* SPI has only one frame */
834 mcbsp_cfg.rcr1 |= (RWDLEN1(spi_cfg->word_length) | RFRLEN1(0));
835 mcbsp_cfg.xcr1 |= (XWDLEN1(spi_cfg->word_length) | XFRLEN1(0));
836
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300837 /* Clock stop mode */
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100838 if (spi_cfg->clk_stp_mode == OMAP_MCBSP_CLK_STP_MODE_NO_DELAY)
839 mcbsp_cfg.spcr1 |= (1 << 12);
840 else
841 mcbsp_cfg.spcr1 |= (3 << 11);
842
843 /* Set clock parities */
844 if (spi_cfg->rx_clock_polarity == OMAP_MCBSP_CLK_RISING)
845 mcbsp_cfg.pcr0 |= CLKRP;
846 else
847 mcbsp_cfg.pcr0 &= ~CLKRP;
848
849 if (spi_cfg->tx_clock_polarity == OMAP_MCBSP_CLK_RISING)
850 mcbsp_cfg.pcr0 &= ~CLKXP;
851 else
852 mcbsp_cfg.pcr0 |= CLKXP;
853
854 /* Set SCLKME to 0 and CLKSM to 1 */
855 mcbsp_cfg.pcr0 &= ~SCLKME;
856 mcbsp_cfg.srgr2 |= CLKSM;
857
858 /* Set FSXP */
859 if (spi_cfg->fsx_polarity == OMAP_MCBSP_FS_ACTIVE_HIGH)
860 mcbsp_cfg.pcr0 &= ~FSXP;
861 else
862 mcbsp_cfg.pcr0 |= FSXP;
863
864 if (spi_cfg->spi_mode == OMAP_MCBSP_SPI_MASTER) {
865 mcbsp_cfg.pcr0 |= CLKXM;
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300866 mcbsp_cfg.srgr1 |= CLKGDV(spi_cfg->clk_div - 1);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100867 mcbsp_cfg.pcr0 |= FSXM;
868 mcbsp_cfg.srgr2 &= ~FSGM;
869 mcbsp_cfg.xcr2 |= XDATDLY(1);
870 mcbsp_cfg.rcr2 |= RDATDLY(1);
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300871 } else {
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100872 mcbsp_cfg.pcr0 &= ~CLKXM;
873 mcbsp_cfg.srgr1 |= CLKGDV(1);
874 mcbsp_cfg.pcr0 &= ~FSXM;
875 mcbsp_cfg.xcr2 &= ~XDATDLY(3);
876 mcbsp_cfg.rcr2 &= ~RDATDLY(3);
877 }
878
879 mcbsp_cfg.xcr2 &= ~XPHASE;
880 mcbsp_cfg.rcr2 &= ~RPHASE;
881
882 omap_mcbsp_config(id, &mcbsp_cfg);
883}
Eduardo Valentinfb78d802008-07-03 12:24:39 +0300884EXPORT_SYMBOL(omap_mcbsp_set_spi_mode);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100885
886/*
887 * McBSP1 and McBSP3 are directly mapped on 1610 and 1510.
888 * 730 has only 2 McBSP, and both of them are MPU peripherals.
889 */
Uwe Kleine-König25cef222008-10-08 10:01:39 +0300890static int __devinit omap_mcbsp_probe(struct platform_device *pdev)
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100891{
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300892 struct omap_mcbsp_platform_data *pdata = pdev->dev.platform_data;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300893 struct omap_mcbsp *mcbsp;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300894 int id = pdev->id - 1;
895 int ret = 0;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100896
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300897 if (!pdata) {
898 dev_err(&pdev->dev, "McBSP device initialized without"
899 "platform data\n");
900 ret = -EINVAL;
901 goto exit;
902 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100903
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300904 dev_dbg(&pdev->dev, "Initializing OMAP McBSP (%d).\n", pdev->id);
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100905
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300906 if (id >= omap_mcbsp_count) {
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300907 dev_err(&pdev->dev, "Invalid McBSP device id (%d)\n", id);
908 ret = -EINVAL;
909 goto exit;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100910 }
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100911
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300912 mcbsp = kzalloc(sizeof(struct omap_mcbsp), GFP_KERNEL);
913 if (!mcbsp) {
914 ret = -ENOMEM;
915 goto exit;
916 }
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300917
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300918 spin_lock_init(&mcbsp->lock);
919 mcbsp->id = id + 1;
920 mcbsp->free = 1;
921 mcbsp->dma_tx_lch = -1;
922 mcbsp->dma_rx_lch = -1;
923
924 mcbsp->phys_base = pdata->phys_base;
925 mcbsp->io_base = ioremap(pdata->phys_base, SZ_4K);
926 if (!mcbsp->io_base) {
Russell Kingd592dd12008-09-04 14:25:42 +0100927 ret = -ENOMEM;
928 goto err_ioremap;
929 }
930
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300931 /* Default I/O is IRQ based */
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300932 mcbsp->io_type = OMAP_MCBSP_IRQ_IO;
933 mcbsp->tx_irq = pdata->tx_irq;
934 mcbsp->rx_irq = pdata->rx_irq;
935 mcbsp->dma_rx_sync = pdata->dma_rx_sync;
936 mcbsp->dma_tx_sync = pdata->dma_tx_sync;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300937
Russell Kingb820ce42009-01-23 10:26:46 +0000938 mcbsp->iclk = clk_get(&pdev->dev, "ick");
939 if (IS_ERR(mcbsp->iclk)) {
940 ret = PTR_ERR(mcbsp->iclk);
941 dev_err(&pdev->dev, "unable to get ick: %d\n", ret);
942 goto err_iclk;
943 }
Stanley.Miao06151152009-01-29 08:57:12 -0800944
Russell Kingb820ce42009-01-23 10:26:46 +0000945 mcbsp->fclk = clk_get(&pdev->dev, "fck");
946 if (IS_ERR(mcbsp->fclk)) {
947 ret = PTR_ERR(mcbsp->fclk);
948 dev_err(&pdev->dev, "unable to get fck: %d\n", ret);
949 goto err_fclk;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300950 }
951
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300952 mcbsp->pdata = pdata;
953 mcbsp->dev = &pdev->dev;
Russell Kingb820ce42009-01-23 10:26:46 +0000954 mcbsp_ptr[id] = mcbsp;
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300955 platform_set_drvdata(pdev, mcbsp);
Russell Kingd592dd12008-09-04 14:25:42 +0100956 return 0;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300957
Russell Kingb820ce42009-01-23 10:26:46 +0000958err_fclk:
959 clk_put(mcbsp->iclk);
960err_iclk:
Chandra Shekharb4b58f52008-10-08 10:01:39 +0300961 iounmap(mcbsp->io_base);
Russell Kingd592dd12008-09-04 14:25:42 +0100962err_ioremap:
Russell Kingb820ce42009-01-23 10:26:46 +0000963 kfree(mcbsp);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300964exit:
965 return ret;
966}
967
Uwe Kleine-König25cef222008-10-08 10:01:39 +0300968static int __devexit omap_mcbsp_remove(struct platform_device *pdev)
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300969{
970 struct omap_mcbsp *mcbsp = platform_get_drvdata(pdev);
971
972 platform_set_drvdata(pdev, NULL);
973 if (mcbsp) {
974
975 if (mcbsp->pdata && mcbsp->pdata->ops &&
976 mcbsp->pdata->ops->free)
977 mcbsp->pdata->ops->free(mcbsp->id);
978
Russell Kingb820ce42009-01-23 10:26:46 +0000979 clk_disable(mcbsp->fclk);
980 clk_disable(mcbsp->iclk);
981 clk_put(mcbsp->fclk);
982 clk_put(mcbsp->iclk);
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300983
Russell Kingd592dd12008-09-04 14:25:42 +0100984 iounmap(mcbsp->io_base);
985
Russell Kingb820ce42009-01-23 10:26:46 +0000986 mcbsp->fclk = NULL;
987 mcbsp->iclk = NULL;
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300988 mcbsp->free = 0;
989 mcbsp->dev = NULL;
Tony Lindgren5e1c5ff2005-07-10 19:58:15 +0100990 }
991
992 return 0;
993}
994
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300995static struct platform_driver omap_mcbsp_driver = {
996 .probe = omap_mcbsp_probe,
Uwe Kleine-König25cef222008-10-08 10:01:39 +0300997 .remove = __devexit_p(omap_mcbsp_remove),
Eduardo Valentinbc5d0c82008-07-03 12:24:39 +0300998 .driver = {
999 .name = "omap-mcbsp",
1000 },
1001};
1002
1003int __init omap_mcbsp_init(void)
1004{
1005 /* Register the McBSP driver */
1006 return platform_driver_register(&omap_mcbsp_driver);
1007}