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;
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 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
301 const void *dout, void *din, unsigned long flags)
303 struct omap3_spi_slave *ds = to_omap3_spi(slave);
305 const u8 *txp = dout;
314 if (bitlen == 0) { /* only change CS */
315 int chconf = readl(&ds->regs->channel[ds->slave.cs].chconf);
317 if (flags & SPI_XFER_BEGIN) {
318 writel(OMAP3_MCSPI_CHCTRL_EN,
319 &ds->regs->channel[ds->slave.cs].chctrl);
320 chconf |= OMAP3_MCSPI_CHCONF_FORCE;
322 &ds->regs->channel[ds->slave.cs].chconf);
324 if (flags & SPI_XFER_END) {
325 chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
327 &ds->regs->channel[ds->slave.cs].chconf);
328 writel(0, &ds->regs->channel[ds->slave.cs].chctrl);
333 ret = omap3_spi_write(slave, len, txp, flags);
336 ret = omap3_spi_read(slave, len, rxp, flags);
341 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
346 void spi_cs_activate(struct spi_slave *slave)
350 void spi_cs_deactivate(struct spi_slave *slave)