1 // SPDX-License-Identifier: GPL-2.0+
8 * Driver for McSPI controller on OMAP3. Based on davinci_spi.c
9 * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
11 * Copyright (C) 2007 Atmel Corporation
13 * Parts taken from linux/drivers/spi/omap2_mcspi.c
14 * Copyright (C) 2005, 2006 Nokia Corporation
24 #include <linux/bitops.h>
25 #include <omap3_spi.h>
27 DECLARE_GLOBAL_DATA_PTR;
29 struct omap2_mcspi_platform_config {
30 unsigned int regs_offset;
33 struct omap3_spi_priv {
39 unsigned int pin_dir:1;
42 static void omap3_spi_write_chconf(struct omap3_spi_priv *priv, int val)
44 writel(val, &priv->regs->channel[priv->cs].chconf);
45 /* Flash post writes to make immediate effect */
46 readl(&priv->regs->channel[priv->cs].chconf);
49 static void omap3_spi_set_enable(struct omap3_spi_priv *priv, int enable)
51 writel(enable, &priv->regs->channel[priv->cs].chctrl);
52 /* Flash post writes to make immediate effect */
53 readl(&priv->regs->channel[priv->cs].chctrl);
56 static int omap3_spi_write(struct omap3_spi_priv *priv, unsigned int len,
57 const void *txp, unsigned long flags)
62 chconf = readl(&priv->regs->channel[priv->cs].chconf);
64 /* Enable the channel */
65 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
67 chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK);
68 chconf |= (priv->wordlen - 1) << 7;
69 chconf |= OMAP3_MCSPI_CHCONF_TRM_TX_ONLY;
70 chconf |= OMAP3_MCSPI_CHCONF_FORCE;
71 omap3_spi_write_chconf(priv, chconf);
73 for (i = 0; i < len; i++) {
74 /* wait till TX register is empty (TXS == 1) */
76 while (!(readl(&priv->regs->channel[priv->cs].chstat) &
77 OMAP3_MCSPI_CHSTAT_TXS)) {
78 if (get_timer(start) > SPI_WAIT_TIMEOUT) {
79 printf("SPI TXS timed out, status=0x%08x\n",
80 readl(&priv->regs->channel[priv->cs].chstat));
85 unsigned int *tx = &priv->regs->channel[priv->cs].tx;
86 if (priv->wordlen > 16)
87 writel(((u32 *)txp)[i], tx);
88 else if (priv->wordlen > 8)
89 writel(((u16 *)txp)[i], tx);
91 writel(((u8 *)txp)[i], tx);
94 /* wait to finish of transfer */
95 while ((readl(&priv->regs->channel[priv->cs].chstat) &
96 (OMAP3_MCSPI_CHSTAT_EOT | OMAP3_MCSPI_CHSTAT_TXS)) !=
97 (OMAP3_MCSPI_CHSTAT_EOT | OMAP3_MCSPI_CHSTAT_TXS))
100 /* Disable the channel otherwise the next immediate RX will get affected */
101 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
103 if (flags & SPI_XFER_END) {
105 chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
106 omap3_spi_write_chconf(priv, chconf);
111 static int omap3_spi_read(struct omap3_spi_priv *priv, unsigned int len,
112 void *rxp, unsigned long flags)
117 chconf = readl(&priv->regs->channel[priv->cs].chconf);
119 /* Enable the channel */
120 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
122 chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK);
123 chconf |= (priv->wordlen - 1) << 7;
124 chconf |= OMAP3_MCSPI_CHCONF_TRM_RX_ONLY;
125 chconf |= OMAP3_MCSPI_CHCONF_FORCE;
126 omap3_spi_write_chconf(priv, chconf);
128 writel(0, &priv->regs->channel[priv->cs].tx);
130 for (i = 0; i < len; i++) {
131 start = get_timer(0);
132 /* Wait till RX register contains data (RXS == 1) */
133 while (!(readl(&priv->regs->channel[priv->cs].chstat) &
134 OMAP3_MCSPI_CHSTAT_RXS)) {
135 if (get_timer(start) > SPI_WAIT_TIMEOUT) {
136 printf("SPI RXS timed out, status=0x%08x\n",
137 readl(&priv->regs->channel[priv->cs].chstat));
142 /* Disable the channel to prevent furher receiving */
144 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
147 unsigned int *rx = &priv->regs->channel[priv->cs].rx;
148 if (priv->wordlen > 16)
149 ((u32 *)rxp)[i] = readl(rx);
150 else if (priv->wordlen > 8)
151 ((u16 *)rxp)[i] = (u16)readl(rx);
153 ((u8 *)rxp)[i] = (u8)readl(rx);
156 if (flags & SPI_XFER_END) {
157 chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
158 omap3_spi_write_chconf(priv, chconf);
164 /*McSPI Transmit Receive Mode*/
165 static int omap3_spi_txrx(struct omap3_spi_priv *priv, unsigned int len,
166 const void *txp, void *rxp, unsigned long flags)
171 chconf = readl(&priv->regs->channel[priv->cs].chconf);
173 /*Enable SPI channel*/
174 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
176 /*set TRANSMIT-RECEIVE Mode*/
177 chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK);
178 chconf |= (priv->wordlen - 1) << 7;
179 chconf |= OMAP3_MCSPI_CHCONF_FORCE;
180 omap3_spi_write_chconf(priv, chconf);
182 /*Shift in and out 1 byte at time*/
183 for (i=0; i < len; i++){
184 /* Write: wait for TX empty (TXS == 1)*/
185 start = get_timer(0);
186 while (!(readl(&priv->regs->channel[priv->cs].chstat) &
187 OMAP3_MCSPI_CHSTAT_TXS)) {
188 if (get_timer(start) > SPI_WAIT_TIMEOUT) {
189 printf("SPI TXS timed out, status=0x%08x\n",
190 readl(&priv->regs->channel[priv->cs].chstat));
195 unsigned int *tx = &priv->regs->channel[priv->cs].tx;
196 if (priv->wordlen > 16)
197 writel(((u32 *)txp)[i], tx);
198 else if (priv->wordlen > 8)
199 writel(((u16 *)txp)[i], tx);
201 writel(((u8 *)txp)[i], tx);
203 /*Read: wait for RX containing data (RXS == 1)*/
204 start = get_timer(0);
205 while (!(readl(&priv->regs->channel[priv->cs].chstat) &
206 OMAP3_MCSPI_CHSTAT_RXS)) {
207 if (get_timer(start) > SPI_WAIT_TIMEOUT) {
208 printf("SPI RXS timed out, status=0x%08x\n",
209 readl(&priv->regs->channel[priv->cs].chstat));
214 unsigned int *rx = &priv->regs->channel[priv->cs].rx;
215 if (priv->wordlen > 16)
216 ((u32 *)rxp)[i] = readl(rx);
217 else if (priv->wordlen > 8)
218 ((u16 *)rxp)[i] = (u16)readl(rx);
220 ((u8 *)rxp)[i] = (u8)readl(rx);
222 /* Disable the channel */
223 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
225 /*if transfer must be terminated disable the channel*/
226 if (flags & SPI_XFER_END) {
227 chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
228 omap3_spi_write_chconf(priv, chconf);
234 static int _spi_xfer(struct omap3_spi_priv *priv, unsigned int bitlen,
235 const void *dout, void *din, unsigned long flags)
240 if (priv->wordlen < 4 || priv->wordlen > 32) {
241 printf("omap3_spi: invalid wordlen %d\n", priv->wordlen);
245 if (bitlen % priv->wordlen)
248 len = bitlen / priv->wordlen;
250 if (bitlen == 0) { /* only change CS */
251 int chconf = readl(&priv->regs->channel[priv->cs].chconf);
253 if (flags & SPI_XFER_BEGIN) {
254 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
255 chconf |= OMAP3_MCSPI_CHCONF_FORCE;
256 omap3_spi_write_chconf(priv, chconf);
258 if (flags & SPI_XFER_END) {
259 chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
260 omap3_spi_write_chconf(priv, chconf);
261 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
265 if (dout != NULL && din != NULL)
266 ret = omap3_spi_txrx(priv, len, dout, din, flags);
267 else if (dout != NULL)
268 ret = omap3_spi_write(priv, len, dout, flags);
269 else if (din != NULL)
270 ret = omap3_spi_read(priv, len, din, flags);
275 static void _omap3_spi_set_speed(struct omap3_spi_priv *priv)
277 uint32_t confr, div = 0;
279 confr = readl(&priv->regs->channel[priv->cs].chconf);
281 /* Calculate clock divisor. Valid range: 0x0 - 0xC ( /1 - /4096 ) */
283 while (div <= 0xC && (OMAP3_MCSPI_MAX_FREQ / (1 << div))
290 /* set clock divisor */
291 confr &= ~OMAP3_MCSPI_CHCONF_CLKD_MASK;
294 omap3_spi_write_chconf(priv, confr);
297 static void _omap3_spi_set_mode(struct omap3_spi_priv *priv)
301 confr = readl(&priv->regs->channel[priv->cs].chconf);
303 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
304 * REVISIT: this controller could support SPI_3WIRE mode.
306 if (priv->pin_dir == MCSPI_PINDIR_D0_IN_D1_OUT) {
307 confr &= ~(OMAP3_MCSPI_CHCONF_IS|OMAP3_MCSPI_CHCONF_DPE1);
308 confr |= OMAP3_MCSPI_CHCONF_DPE0;
310 confr &= ~OMAP3_MCSPI_CHCONF_DPE0;
311 confr |= OMAP3_MCSPI_CHCONF_IS|OMAP3_MCSPI_CHCONF_DPE1;
314 /* set SPI mode 0..3 */
315 confr &= ~(OMAP3_MCSPI_CHCONF_POL | OMAP3_MCSPI_CHCONF_PHA);
316 if (priv->mode & SPI_CPHA)
317 confr |= OMAP3_MCSPI_CHCONF_PHA;
318 if (priv->mode & SPI_CPOL)
319 confr |= OMAP3_MCSPI_CHCONF_POL;
321 /* set chipselect polarity; manage with FORCE */
322 if (!(priv->mode & SPI_CS_HIGH))
323 confr |= OMAP3_MCSPI_CHCONF_EPOL; /* active-low; normal */
325 confr &= ~OMAP3_MCSPI_CHCONF_EPOL;
327 /* Transmit & receive mode */
328 confr &= ~OMAP3_MCSPI_CHCONF_TRM_MASK;
330 omap3_spi_write_chconf(priv, confr);
333 static void _omap3_spi_set_wordlen(struct omap3_spi_priv *priv)
337 /* McSPI individual channel configuration */
338 confr = readl(&priv->regs->channel[priv->cs].chconf);
341 confr &= ~OMAP3_MCSPI_CHCONF_WL_MASK;
342 confr |= (priv->wordlen - 1) << 7;
344 omap3_spi_write_chconf(priv, confr);
347 static void spi_reset(struct mcspi *regs)
351 writel(OMAP3_MCSPI_SYSCONFIG_SOFTRESET, ®s->sysconfig);
353 tmp = readl(®s->sysstatus);
354 } while (!(tmp & OMAP3_MCSPI_SYSSTATUS_RESETDONE));
356 writel(OMAP3_MCSPI_SYSCONFIG_AUTOIDLE |
357 OMAP3_MCSPI_SYSCONFIG_ENAWAKEUP |
358 OMAP3_MCSPI_SYSCONFIG_SMARTIDLE, ®s->sysconfig);
360 writel(OMAP3_MCSPI_WAKEUPENABLE_WKEN, ®s->wakeupenable);
363 static void _omap3_spi_claim_bus(struct omap3_spi_priv *priv)
367 * setup when switching from (reset default) slave mode
368 * to single-channel master mode
370 conf = readl(&priv->regs->modulctrl);
371 conf &= ~(OMAP3_MCSPI_MODULCTRL_STEST | OMAP3_MCSPI_MODULCTRL_MS);
372 conf |= OMAP3_MCSPI_MODULCTRL_SINGLE;
374 writel(conf, &priv->regs->modulctrl);
377 static int omap3_spi_claim_bus(struct udevice *dev)
379 struct udevice *bus = dev->parent;
380 struct omap3_spi_priv *priv = dev_get_priv(bus);
381 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_plat(dev);
383 priv->cs = slave_plat->cs;
384 priv->freq = slave_plat->max_hz;
386 _omap3_spi_claim_bus(priv);
391 static int omap3_spi_release_bus(struct udevice *dev)
393 struct udevice *bus = dev->parent;
394 struct omap3_spi_priv *priv = dev_get_priv(bus);
396 writel(OMAP3_MCSPI_MODULCTRL_MS, &priv->regs->modulctrl);
401 static int omap3_spi_set_wordlen(struct udevice *dev, unsigned int wordlen)
403 struct udevice *bus = dev->parent;
404 struct omap3_spi_priv *priv = dev_get_priv(bus);
405 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_plat(dev);
407 priv->cs = slave_plat->cs;
408 priv->wordlen = wordlen;
409 _omap3_spi_set_wordlen(priv);
414 static int omap3_spi_probe(struct udevice *dev)
416 struct omap3_spi_priv *priv = dev_get_priv(dev);
417 struct omap3_spi_plat *plat = dev_get_platdata(dev);
419 priv->regs = plat->regs;
420 priv->pin_dir = plat->pin_dir;
421 priv->wordlen = SPI_DEFAULT_WORDLEN;
423 spi_reset(priv->regs);
428 static int omap3_spi_xfer(struct udevice *dev, unsigned int bitlen,
429 const void *dout, void *din, unsigned long flags)
431 struct udevice *bus = dev->parent;
432 struct omap3_spi_priv *priv = dev_get_priv(bus);
434 return _spi_xfer(priv, bitlen, dout, din, flags);
437 static int omap3_spi_set_speed(struct udevice *dev, unsigned int speed)
440 struct omap3_spi_priv *priv = dev_get_priv(dev);
443 _omap3_spi_set_speed(priv);
448 static int omap3_spi_set_mode(struct udevice *dev, uint mode)
450 struct omap3_spi_priv *priv = dev_get_priv(dev);
454 _omap3_spi_set_mode(priv);
459 static const struct dm_spi_ops omap3_spi_ops = {
460 .claim_bus = omap3_spi_claim_bus,
461 .release_bus = omap3_spi_release_bus,
462 .set_wordlen = omap3_spi_set_wordlen,
463 .xfer = omap3_spi_xfer,
464 .set_speed = omap3_spi_set_speed,
465 .set_mode = omap3_spi_set_mode,
467 * cs_info is not needed, since we require all chip selects to be
468 * in the device tree explicitly
472 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
473 static struct omap2_mcspi_platform_config omap2_pdata = {
477 static struct omap2_mcspi_platform_config omap4_pdata = {
478 .regs_offset = OMAP4_MCSPI_REG_OFFSET,
481 static int omap3_spi_ofdata_to_platdata(struct udevice *dev)
483 struct omap2_mcspi_platform_config *data =
484 (struct omap2_mcspi_platform_config *)dev_get_driver_data(dev);
485 struct omap3_spi_plat *plat = dev_get_platdata(dev);
487 plat->regs = (struct mcspi *)(dev_read_addr(dev) + data->regs_offset);
489 if (dev_read_bool(dev, "ti,pindir-d0-out-d1-in"))
490 plat->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN;
492 plat->pin_dir = MCSPI_PINDIR_D0_IN_D1_OUT;
497 static const struct udevice_id omap3_spi_ids[] = {
498 { .compatible = "ti,omap2-mcspi", .data = (ulong)&omap2_pdata },
499 { .compatible = "ti,omap4-mcspi", .data = (ulong)&omap4_pdata },
503 U_BOOT_DRIVER(omap3_spi) = {
506 .flags = DM_FLAG_PRE_RELOC,
507 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
508 .of_match = omap3_spi_ids,
509 .ofdata_to_platdata = omap3_spi_ofdata_to_platdata,
510 .plat_auto = sizeof(struct omap3_spi_plat),
512 .probe = omap3_spi_probe,
513 .ops = &omap3_spi_ops,
514 .priv_auto = sizeof(struct omap3_spi_priv),