4 * Driver for McSPI controller on OMAP3. Based on davinci_spi.c
5 * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
7 * Copyright (C) 2007 Atmel Corporation
9 * Parts taken from linux/drivers/spi/omap2_mcspi.c
10 * Copyright (C) 2005, 2006 Nokia Corporation
14 * See file CREDITS for list of people who contributed to this
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License as
19 * published by the Free Software Foundation; either version 2 of
20 * the License, or (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
38 #include "omap3_spi.h"
41 #define SPI_WAIT_TIMEOUT 3000000;
43 static void spi_reset(struct omap3_spi_slave *ds)
47 writel(OMAP3_MCSPI_SYSCONFIG_SOFTRESET, &ds->regs->sysconfig);
49 tmp = readl(&ds->regs->sysstatus);
50 } while (!(tmp & OMAP3_MCSPI_SYSSTATUS_RESETDONE));
52 writel(OMAP3_MCSPI_SYSCONFIG_AUTOIDLE |
53 OMAP3_MCSPI_SYSCONFIG_ENAWAKEUP |
54 OMAP3_MCSPI_SYSCONFIG_SMARTIDLE,
55 &ds->regs->sysconfig);
57 writel(OMAP3_MCSPI_WAKEUPENABLE_WKEN, &ds->regs->wakeupenable);
65 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
66 unsigned int max_hz, unsigned int mode)
68 struct omap3_spi_slave *ds;
70 ds = malloc(sizeof(struct omap3_spi_slave));
72 printf("SPI error: malloc of SPI structure failed\n");
77 * OMAP3 McSPI (MultiChannel SPI) has 4 busses (modules)
78 * with different number of chip selects (CS, channels):
79 * McSPI1 has 4 CS (bus 0, cs 0 - 3)
80 * McSPI2 has 2 CS (bus 1, cs 0 - 1)
81 * McSPI3 has 2 CS (bus 2, cs 0 - 1)
82 * McSPI4 has 1 CS (bus 3, cs 0)
87 ds->regs = (struct mcspi *)OMAP3_MCSPI1_BASE;
89 #ifdef OMAP3_MCSPI2_BASE
91 ds->regs = (struct mcspi *)OMAP3_MCSPI2_BASE;
94 #ifdef OMAP3_MCSPI3_BASE
96 ds->regs = (struct mcspi *)OMAP3_MCSPI3_BASE;
99 #ifdef OMAP3_MCSPI4_BASE
101 ds->regs = (struct mcspi *)OMAP3_MCSPI4_BASE;
105 printf("SPI error: unsupported bus %i. \
106 Supported busses 0 - 3\n", bus);
111 if (((bus == 0) && (cs > 3)) ||
112 ((bus == 1) && (cs > 1)) ||
113 ((bus == 2) && (cs > 1)) ||
114 ((bus == 3) && (cs > 0))) {
115 printf("SPI error: unsupported chip select %i \
116 on bus %i\n", cs, bus);
121 if (max_hz > OMAP3_MCSPI_MAX_FREQ) {
122 printf("SPI error: unsupported frequency %i Hz. \
123 Max frequency is 48 Mhz\n", max_hz);
128 if (mode > SPI_MODE_3) {
129 printf("SPI error: unsupported SPI mode %i\n", mode);
137 void spi_free_slave(struct spi_slave *slave)
139 struct omap3_spi_slave *ds = to_omap3_spi(slave);
144 int spi_claim_bus(struct spi_slave *slave)
146 struct omap3_spi_slave *ds = to_omap3_spi(slave);
147 unsigned int conf, div = 0;
149 /* McSPI global module configuration */
152 * setup when switching from (reset default) slave mode
153 * to single-channel master mode
156 conf = readl(&ds->regs->modulctrl);
157 conf &= ~(OMAP3_MCSPI_MODULCTRL_STEST | OMAP3_MCSPI_MODULCTRL_MS);
158 conf |= OMAP3_MCSPI_MODULCTRL_SINGLE;
159 writel(conf, &ds->regs->modulctrl);
161 /* McSPI individual channel configuration */
163 /* Calculate clock divisor. Valid range: 0x0 - 0xC ( /1 - /4096 ) */
165 while (div <= 0xC && (OMAP3_MCSPI_MAX_FREQ / (1 << div))
171 conf = readl(&ds->regs->channel[ds->slave.cs].chconf);
173 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
174 * REVISIT: this controller could support SPI_3WIRE mode.
178 * The reference design on AM33xx has D0 and D1 wired up opposite
179 * of how it has been done on previous platforms. We assume that
180 * custom hardware will also follow this convention.
182 conf &= OMAP3_MCSPI_CHCONF_DPE0;
183 conf |= ~(OMAP3_MCSPI_CHCONF_IS|OMAP3_MCSPI_CHCONF_DPE1);
185 conf &= ~(OMAP3_MCSPI_CHCONF_IS|OMAP3_MCSPI_CHCONF_DPE1);
186 conf |= OMAP3_MCSPI_CHCONF_DPE0;
190 conf &= ~OMAP3_MCSPI_CHCONF_WL_MASK;
191 conf |= (WORD_LEN - 1) << 7;
193 /* set chipselect polarity; manage with FORCE */
194 if (!(ds->mode & SPI_CS_HIGH))
195 conf |= OMAP3_MCSPI_CHCONF_EPOL; /* active-low; normal */
197 conf &= ~OMAP3_MCSPI_CHCONF_EPOL;
199 /* set clock divisor */
200 conf &= ~OMAP3_MCSPI_CHCONF_CLKD_MASK;
203 /* set SPI mode 0..3 */
204 if (ds->mode & SPI_CPOL)
205 conf |= OMAP3_MCSPI_CHCONF_POL;
207 conf &= ~OMAP3_MCSPI_CHCONF_POL;
208 if (ds->mode & SPI_CPHA)
209 conf |= OMAP3_MCSPI_CHCONF_PHA;
211 conf &= ~OMAP3_MCSPI_CHCONF_PHA;
213 /* Transmit & receive mode */
214 conf &= ~OMAP3_MCSPI_CHCONF_TRM_MASK;
216 writel(conf, &ds->regs->channel[ds->slave.cs].chconf);
221 void spi_release_bus(struct spi_slave *slave)
223 struct omap3_spi_slave *ds = to_omap3_spi(slave);
225 /* Reset the SPI hardware */
229 int omap3_spi_write(struct spi_slave *slave, unsigned int len, const u8 *txp,
232 struct omap3_spi_slave *ds = to_omap3_spi(slave);
234 int timeout = SPI_WAIT_TIMEOUT;
235 int chconf = readl(&ds->regs->channel[ds->slave.cs].chconf);
237 if (flags & SPI_XFER_BEGIN)
238 writel(OMAP3_MCSPI_CHCTRL_EN,
239 &ds->regs->channel[ds->slave.cs].chctrl);
241 chconf &= ~OMAP3_MCSPI_CHCONF_TRM_MASK;
242 chconf |= OMAP3_MCSPI_CHCONF_TRM_TX_ONLY;
243 chconf |= OMAP3_MCSPI_CHCONF_FORCE;
244 writel(chconf, &ds->regs->channel[ds->slave.cs].chconf);
246 for (i = 0; i < len; i++) {
247 /* wait till TX register is empty (TXS == 1) */
248 while (!(readl(&ds->regs->channel[ds->slave.cs].chstat) &
249 OMAP3_MCSPI_CHSTAT_TXS)) {
250 if (--timeout <= 0) {
251 printf("SPI TXS timed out, status=0x%08x\n",
252 readl(&ds->regs->channel[ds->slave.cs].chstat));
257 writel(txp[i], &ds->regs->channel[ds->slave.cs].tx);
260 if (flags & SPI_XFER_END) {
261 /* wait to finish of transfer */
262 while (!(readl(&ds->regs->channel[ds->slave.cs].chstat) &
263 OMAP3_MCSPI_CHSTAT_EOT));
265 chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
266 writel(chconf, &ds->regs->channel[ds->slave.cs].chconf);
268 writel(0, &ds->regs->channel[ds->slave.cs].chctrl);
273 int omap3_spi_read(struct spi_slave *slave, unsigned int len, u8 *rxp,
276 struct omap3_spi_slave *ds = to_omap3_spi(slave);
278 int timeout = SPI_WAIT_TIMEOUT;
279 int chconf = readl(&ds->regs->channel[ds->slave.cs].chconf);
281 if (flags & SPI_XFER_BEGIN)
282 writel(OMAP3_MCSPI_CHCTRL_EN,
283 &ds->regs->channel[ds->slave.cs].chctrl);
285 chconf &= ~OMAP3_MCSPI_CHCONF_TRM_MASK;
286 chconf |= OMAP3_MCSPI_CHCONF_TRM_RX_ONLY;
287 chconf |= OMAP3_MCSPI_CHCONF_FORCE;
288 writel(chconf, &ds->regs->channel[ds->slave.cs].chconf);
290 writel(0, &ds->regs->channel[ds->slave.cs].tx);
292 for (i = 0; i < len; i++) {
293 /* Wait till RX register contains data (RXS == 1) */
294 while (!(readl(&ds->regs->channel[ds->slave.cs].chstat) &
295 OMAP3_MCSPI_CHSTAT_RXS)) {
296 if (--timeout <= 0) {
297 printf("SPI RXS timed out, status=0x%08x\n",
298 readl(&ds->regs->channel[ds->slave.cs].chstat));
303 rxp[i] = readl(&ds->regs->channel[ds->slave.cs].rx);
306 if (flags & SPI_XFER_END) {
307 chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
308 writel(chconf, &ds->regs->channel[ds->slave.cs].chconf);
310 writel(0, &ds->regs->channel[ds->slave.cs].chctrl);
316 /*McSPI Transmit Receive Mode*/
317 int omap3_spi_txrx(struct spi_slave *slave,
318 unsigned int len, const u8 *txp, u8 *rxp, unsigned long flags)
320 struct omap3_spi_slave *ds = to_omap3_spi(slave);
321 int timeout = SPI_WAIT_TIMEOUT;
322 int chconf = readl(&ds->regs->channel[ds->slave.cs].chconf);
323 int irqstatus = readl(&ds->regs->irqstatus);
326 /*Enable SPI channel*/
327 if (flags & SPI_XFER_BEGIN)
328 writel(OMAP3_MCSPI_CHCTRL_EN,
329 &ds->regs->channel[ds->slave.cs].chctrl);
331 /*set TRANSMIT-RECEIVE Mode*/
332 chconf &= ~OMAP3_MCSPI_CHCONF_TRM_MASK;
333 chconf |= OMAP3_MCSPI_CHCONF_FORCE;
334 writel(chconf, &ds->regs->channel[ds->slave.cs].chconf);
336 /*Shift in and out 1 byte at time*/
337 for (i=0; i < len; i++){
338 /* Write: wait for TX empty (TXS == 1)*/
339 irqstatus |= (1<< (4*(ds->slave.bus)));
340 while (!(readl(&ds->regs->channel[ds->slave.cs].chstat) &
341 OMAP3_MCSPI_CHSTAT_TXS)) {
342 if (--timeout <= 0) {
343 printf("SPI TXS timed out, status=0x%08x\n",
344 readl(&ds->regs->channel[ds->slave.cs].chstat));
349 writel(txp[i], &ds->regs->channel[ds->slave.cs].tx);
351 /*Read: wait for RX containing data (RXS == 1)*/
352 while (!(readl(&ds->regs->channel[ds->slave.cs].chstat) &
353 OMAP3_MCSPI_CHSTAT_RXS)) {
354 if (--timeout <= 0) {
355 printf("SPI RXS timed out, status=0x%08x\n",
356 readl(&ds->regs->channel[ds->slave.cs].chstat));
361 rxp[i] = readl(&ds->regs->channel[ds->slave.cs].rx);
364 /*if transfer must be terminated disable the channel*/
365 if (flags & SPI_XFER_END) {
366 chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
367 writel(chconf, &ds->regs->channel[ds->slave.cs].chconf);
369 writel(0, &ds->regs->channel[ds->slave.cs].chctrl);
375 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
376 const void *dout, void *din, unsigned long flags)
378 struct omap3_spi_slave *ds = to_omap3_spi(slave);
380 const u8 *txp = dout;
389 if (bitlen == 0) { /* only change CS */
390 int chconf = readl(&ds->regs->channel[ds->slave.cs].chconf);
392 if (flags & SPI_XFER_BEGIN) {
393 writel(OMAP3_MCSPI_CHCTRL_EN,
394 &ds->regs->channel[ds->slave.cs].chctrl);
395 chconf |= OMAP3_MCSPI_CHCONF_FORCE;
397 &ds->regs->channel[ds->slave.cs].chconf);
399 if (flags & SPI_XFER_END) {
400 chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
402 &ds->regs->channel[ds->slave.cs].chconf);
403 writel(0, &ds->regs->channel[ds->slave.cs].chctrl);
407 if (dout != NULL && din != NULL)
408 ret = omap3_spi_txrx(slave, len, txp, rxp, flags);
409 else if (dout != NULL)
410 ret = omap3_spi_write(slave, len, txp, flags);
411 else if (din != NULL)
412 ret = omap3_spi_read(slave, len, rxp, flags);
417 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
422 void spi_cs_activate(struct spi_slave *slave)
426 void spi_cs_deactivate(struct spi_slave *slave)