2 * Copyright (C) 2010 Dirk Behme <dirk.behme@googlemail.com>
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
12 * Modified by Ruslan Araslanov <ruslan.araslanov@vitecmm.com>
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;
90 ds->regs = (struct mcspi *)OMAP3_MCSPI2_BASE;
93 ds->regs = (struct mcspi *)OMAP3_MCSPI3_BASE;
96 ds->regs = (struct mcspi *)OMAP3_MCSPI4_BASE;
99 printf("SPI error: unsupported bus %i. \
100 Supported busses 0 - 3\n", bus);
105 if (((bus == 0) && (cs > 3)) ||
106 ((bus == 1) && (cs > 1)) ||
107 ((bus == 2) && (cs > 1)) ||
108 ((bus == 3) && (cs > 0))) {
109 printf("SPI error: unsupported chip select %i \
110 on bus %i\n", cs, bus);
115 if (max_hz > OMAP3_MCSPI_MAX_FREQ) {
116 printf("SPI error: unsupported frequency %i Hz. \
117 Max frequency is 48 Mhz\n", max_hz);
122 if (mode > SPI_MODE_3) {
123 printf("SPI error: unsupported SPI mode %i\n", mode);
131 void spi_free_slave(struct spi_slave *slave)
133 struct omap3_spi_slave *ds = to_omap3_spi(slave);
138 int spi_claim_bus(struct spi_slave *slave)
140 struct omap3_spi_slave *ds = to_omap3_spi(slave);
141 unsigned int conf, div = 0;
143 /* McSPI global module configuration */
146 * setup when switching from (reset default) slave mode
147 * to single-channel master mode
150 conf = readl(&ds->regs->modulctrl);
151 conf &= ~(OMAP3_MCSPI_MODULCTRL_STEST | OMAP3_MCSPI_MODULCTRL_MS);
152 conf |= OMAP3_MCSPI_MODULCTRL_SINGLE;
153 writel(conf, &ds->regs->modulctrl);
155 /* McSPI individual channel configuration */
157 /* Calculate clock divisor. Valid range: 0x0 - 0xC ( /1 - /4096 ) */
159 while (div <= 0xC && (OMAP3_MCSPI_MAX_FREQ / (1 << div))
165 conf = readl(&ds->regs->channel[ds->slave.cs].chconf);
167 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
168 * REVISIT: this controller could support SPI_3WIRE mode.
170 conf &= ~(OMAP3_MCSPI_CHCONF_IS|OMAP3_MCSPI_CHCONF_DPE1);
171 conf |= OMAP3_MCSPI_CHCONF_DPE0;
174 conf &= ~OMAP3_MCSPI_CHCONF_WL_MASK;
175 conf |= (WORD_LEN - 1) << 7;
177 /* set chipselect polarity; manage with FORCE */
178 if (!(ds->mode & SPI_CS_HIGH))
179 conf |= OMAP3_MCSPI_CHCONF_EPOL; /* active-low; normal */
181 conf &= ~OMAP3_MCSPI_CHCONF_EPOL;
183 /* set clock divisor */
184 conf &= ~OMAP3_MCSPI_CHCONF_CLKD_MASK;
187 /* set SPI mode 0..3 */
188 if (ds->mode & SPI_CPOL)
189 conf |= OMAP3_MCSPI_CHCONF_POL;
191 conf &= ~OMAP3_MCSPI_CHCONF_POL;
192 if (ds->mode & SPI_CPHA)
193 conf |= OMAP3_MCSPI_CHCONF_PHA;
195 conf &= ~OMAP3_MCSPI_CHCONF_PHA;
197 /* Transmit & receive mode */
198 conf &= ~OMAP3_MCSPI_CHCONF_TRM_MASK;
200 writel(conf, &ds->regs->channel[ds->slave.cs].chconf);
205 void spi_release_bus(struct spi_slave *slave)
207 struct omap3_spi_slave *ds = to_omap3_spi(slave);
209 /* Reset the SPI hardware */
213 int omap3_spi_write(struct spi_slave *slave, unsigned int len, const u8 *txp,
216 struct omap3_spi_slave *ds = to_omap3_spi(slave);
218 int timeout = SPI_WAIT_TIMEOUT;
219 int chconf = readl(&ds->regs->channel[ds->slave.cs].chconf);
221 if (flags & SPI_XFER_BEGIN)
222 writel(OMAP3_MCSPI_CHCTRL_EN,
223 &ds->regs->channel[ds->slave.cs].chctrl);
225 chconf &= ~OMAP3_MCSPI_CHCONF_TRM_MASK;
226 chconf |= OMAP3_MCSPI_CHCONF_TRM_TX_ONLY;
227 chconf |= OMAP3_MCSPI_CHCONF_FORCE;
228 writel(chconf, &ds->regs->channel[ds->slave.cs].chconf);
230 for (i = 0; i < len; i++) {
231 /* wait till TX register is empty (TXS == 1) */
232 while (!(readl(&ds->regs->channel[ds->slave.cs].chstat) &
233 OMAP3_MCSPI_CHSTAT_TXS)) {
234 if (--timeout <= 0) {
235 printf("SPI TXS timed out, status=0x%08x\n",
236 readl(&ds->regs->channel[ds->slave.cs].chstat));
241 writel(txp[i], &ds->regs->channel[ds->slave.cs].tx);
244 if (flags & SPI_XFER_END) {
245 /* wait to finish of transfer */
246 while (!(readl(&ds->regs->channel[ds->slave.cs].chstat) &
247 OMAP3_MCSPI_CHSTAT_EOT));
249 chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
250 writel(chconf, &ds->regs->channel[ds->slave.cs].chconf);
252 writel(0, &ds->regs->channel[ds->slave.cs].chctrl);
257 int omap3_spi_read(struct spi_slave *slave, unsigned int len, u8 *rxp,
260 struct omap3_spi_slave *ds = to_omap3_spi(slave);
262 int timeout = SPI_WAIT_TIMEOUT;
263 int chconf = readl(&ds->regs->channel[ds->slave.cs].chconf);
265 if (flags & SPI_XFER_BEGIN)
266 writel(OMAP3_MCSPI_CHCTRL_EN,
267 &ds->regs->channel[ds->slave.cs].chctrl);
269 chconf &= ~OMAP3_MCSPI_CHCONF_TRM_MASK;
270 chconf |= OMAP3_MCSPI_CHCONF_TRM_RX_ONLY;
271 chconf |= OMAP3_MCSPI_CHCONF_FORCE;
272 writel(chconf, &ds->regs->channel[ds->slave.cs].chconf);
274 writel(0, &ds->regs->channel[ds->slave.cs].tx);
276 for (i = 0; i < len; i++) {
277 /* Wait till RX register contains data (RXS == 1) */
278 while (!(readl(&ds->regs->channel[ds->slave.cs].chstat) &
279 OMAP3_MCSPI_CHSTAT_RXS)) {
280 if (--timeout <= 0) {
281 printf("SPI RXS timed out, status=0x%08x\n",
282 readl(&ds->regs->channel[ds->slave.cs].chstat));
287 rxp[i] = readl(&ds->regs->channel[ds->slave.cs].rx);
290 if (flags & SPI_XFER_END) {
291 chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
292 writel(chconf, &ds->regs->channel[ds->slave.cs].chconf);
294 writel(0, &ds->regs->channel[ds->slave.cs].chctrl);
300 /*McSPI Transmit Receive Mode*/
301 int omap3_spi_txrx(struct spi_slave *slave,
302 unsigned int len, const u8 *txp, u8 *rxp, unsigned long flags)
304 struct omap3_spi_slave *ds = to_omap3_spi(slave);
305 int timeout = SPI_WAIT_TIMEOUT;
306 int chconf = readl(&ds->regs->channel[ds->slave.cs].chconf);
307 int irqstatus = readl(&ds->regs->irqstatus);
310 /*Enable SPI channel*/
311 if (flags & SPI_XFER_BEGIN)
312 writel(OMAP3_MCSPI_CHCTRL_EN,
313 &ds->regs->channel[ds->slave.cs].chctrl);
315 /*set TRANSMIT-RECEIVE Mode*/
316 chconf &= ~OMAP3_MCSPI_CHCONF_TRM_MASK;
317 chconf |= OMAP3_MCSPI_CHCONF_FORCE;
318 writel(chconf, &ds->regs->channel[ds->slave.cs].chconf);
320 /*Shift in and out 1 byte at time*/
321 for (i=0; i < len; i++){
322 /* Write: wait for TX empty (TXS == 1)*/
323 irqstatus |= (1<< (4*(ds->slave.bus)));
324 while (!(readl(&ds->regs->channel[ds->slave.cs].chstat) &
325 OMAP3_MCSPI_CHSTAT_TXS)) {
326 if (--timeout <= 0) {
327 printf("SPI TXS timed out, status=0x%08x\n",
328 readl(&ds->regs->channel[ds->slave.cs].chstat));
333 writel(txp[i], &ds->regs->channel[ds->slave.cs].tx);
335 /*Read: wait for RX containing data (RXS == 1)*/
336 while (!(readl(&ds->regs->channel[ds->slave.cs].chstat) &
337 OMAP3_MCSPI_CHSTAT_RXS)) {
338 if (--timeout <= 0) {
339 printf("SPI RXS timed out, status=0x%08x\n",
340 readl(&ds->regs->channel[ds->slave.cs].chstat));
345 rxp[i] = readl(&ds->regs->channel[ds->slave.cs].rx);
348 /*if transfer must be terminated disable the channel*/
349 if (flags & SPI_XFER_END) {
350 chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
351 writel(chconf, &ds->regs->channel[ds->slave.cs].chconf);
353 writel(0, &ds->regs->channel[ds->slave.cs].chctrl);
359 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
360 const void *dout, void *din, unsigned long flags)
362 struct omap3_spi_slave *ds = to_omap3_spi(slave);
364 const u8 *txp = dout;
373 if (bitlen == 0) { /* only change CS */
374 int chconf = readl(&ds->regs->channel[ds->slave.cs].chconf);
376 if (flags & SPI_XFER_BEGIN) {
377 writel(OMAP3_MCSPI_CHCTRL_EN,
378 &ds->regs->channel[ds->slave.cs].chctrl);
379 chconf |= OMAP3_MCSPI_CHCONF_FORCE;
381 &ds->regs->channel[ds->slave.cs].chconf);
383 if (flags & SPI_XFER_END) {
384 chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
386 &ds->regs->channel[ds->slave.cs].chconf);
387 writel(0, &ds->regs->channel[ds->slave.cs].chctrl);
391 if (dout != NULL && din != NULL)
392 ret = omap3_spi_txrx(slave, len, txp, rxp, flags);
393 else if (dout != NULL)
394 ret = omap3_spi_write(slave, len, txp, flags);
395 else if (din != NULL)
396 ret = omap3_spi_read(slave, len, rxp, flags);
401 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
406 void spi_cs_activate(struct spi_slave *slave)
410 void spi_cs_deactivate(struct spi_slave *slave)