1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2016 Jagan Teki <jteki@openedev.com>
4 * Christophe Ricard <christophe.ricard@gmail.com>
6 * Copyright (C) 2010 Dirk Behme <dirk.behme@googlemail.com>
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
16 * Modified by Ruslan Araslanov <ruslan.araslanov@vitecmm.com>
24 #include <linux/bitops.h>
26 DECLARE_GLOBAL_DATA_PTR;
28 #define OMAP4_MCSPI_REG_OFFSET 0x100
30 struct omap2_mcspi_platform_config {
31 unsigned int regs_offset;
34 /* per-register bitmasks */
35 #define OMAP3_MCSPI_SYSCONFIG_SMARTIDLE (2 << 3)
36 #define OMAP3_MCSPI_SYSCONFIG_ENAWAKEUP BIT(2)
37 #define OMAP3_MCSPI_SYSCONFIG_AUTOIDLE BIT(0)
38 #define OMAP3_MCSPI_SYSCONFIG_SOFTRESET BIT(1)
40 #define OMAP3_MCSPI_SYSSTATUS_RESETDONE BIT(0)
42 #define OMAP3_MCSPI_MODULCTRL_SINGLE BIT(0)
43 #define OMAP3_MCSPI_MODULCTRL_MS BIT(2)
44 #define OMAP3_MCSPI_MODULCTRL_STEST BIT(3)
46 #define OMAP3_MCSPI_CHCONF_PHA BIT(0)
47 #define OMAP3_MCSPI_CHCONF_POL BIT(1)
48 #define OMAP3_MCSPI_CHCONF_CLKD_MASK GENMASK(5, 2)
49 #define OMAP3_MCSPI_CHCONF_EPOL BIT(6)
50 #define OMAP3_MCSPI_CHCONF_WL_MASK GENMASK(11, 7)
51 #define OMAP3_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
52 #define OMAP3_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
53 #define OMAP3_MCSPI_CHCONF_TRM_MASK GENMASK(13, 12)
54 #define OMAP3_MCSPI_CHCONF_DMAW BIT(14)
55 #define OMAP3_MCSPI_CHCONF_DMAR BIT(15)
56 #define OMAP3_MCSPI_CHCONF_DPE0 BIT(16)
57 #define OMAP3_MCSPI_CHCONF_DPE1 BIT(17)
58 #define OMAP3_MCSPI_CHCONF_IS BIT(18)
59 #define OMAP3_MCSPI_CHCONF_TURBO BIT(19)
60 #define OMAP3_MCSPI_CHCONF_FORCE BIT(20)
62 #define OMAP3_MCSPI_CHSTAT_RXS BIT(0)
63 #define OMAP3_MCSPI_CHSTAT_TXS BIT(1)
64 #define OMAP3_MCSPI_CHSTAT_EOT BIT(2)
66 #define OMAP3_MCSPI_CHCTRL_EN BIT(0)
67 #define OMAP3_MCSPI_CHCTRL_DIS (0 << 0)
69 #define OMAP3_MCSPI_WAKEUPENABLE_WKEN BIT(0)
70 #define MCSPI_PINDIR_D0_IN_D1_OUT 0
71 #define MCSPI_PINDIR_D0_OUT_D1_IN 1
73 #define OMAP3_MCSPI_MAX_FREQ 48000000
74 #define SPI_WAIT_TIMEOUT 10
76 /* OMAP3 McSPI registers */
77 struct mcspi_channel {
78 unsigned int chconf; /* 0x2C, 0x40, 0x54, 0x68 */
79 unsigned int chstat; /* 0x30, 0x44, 0x58, 0x6C */
80 unsigned int chctrl; /* 0x34, 0x48, 0x5C, 0x70 */
81 unsigned int tx; /* 0x38, 0x4C, 0x60, 0x74 */
82 unsigned int rx; /* 0x3C, 0x50, 0x64, 0x78 */
86 unsigned char res1[0x10];
87 unsigned int sysconfig; /* 0x10 */
88 unsigned int sysstatus; /* 0x14 */
89 unsigned int irqstatus; /* 0x18 */
90 unsigned int irqenable; /* 0x1C */
91 unsigned int wakeupenable; /* 0x20 */
92 unsigned int syst; /* 0x24 */
93 unsigned int modulctrl; /* 0x28 */
94 struct mcspi_channel channel[4];
95 /* channel0: 0x2C - 0x3C, bus 0 & 1 & 2 & 3 */
96 /* channel1: 0x40 - 0x50, bus 0 & 1 */
97 /* channel2: 0x54 - 0x64, bus 0 & 1 */
98 /* channel3: 0x68 - 0x78, bus 0 */
101 struct omap3_spi_priv {
106 unsigned int wordlen;
107 unsigned int pin_dir:1;
110 static void omap3_spi_write_chconf(struct omap3_spi_priv *priv, int val)
112 writel(val, &priv->regs->channel[priv->cs].chconf);
113 /* Flash post writes to make immediate effect */
114 readl(&priv->regs->channel[priv->cs].chconf);
117 static void omap3_spi_set_enable(struct omap3_spi_priv *priv, int enable)
119 writel(enable, &priv->regs->channel[priv->cs].chctrl);
120 /* Flash post writes to make immediate effect */
121 readl(&priv->regs->channel[priv->cs].chctrl);
124 static int omap3_spi_write(struct omap3_spi_priv *priv, unsigned int len,
125 const void *txp, unsigned long flags)
130 chconf = readl(&priv->regs->channel[priv->cs].chconf);
132 /* Enable the channel */
133 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
135 chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK);
136 chconf |= (priv->wordlen - 1) << 7;
137 chconf |= OMAP3_MCSPI_CHCONF_TRM_TX_ONLY;
138 chconf |= OMAP3_MCSPI_CHCONF_FORCE;
139 omap3_spi_write_chconf(priv, chconf);
141 for (i = 0; i < len; i++) {
142 /* wait till TX register is empty (TXS == 1) */
143 start = get_timer(0);
144 while (!(readl(&priv->regs->channel[priv->cs].chstat) &
145 OMAP3_MCSPI_CHSTAT_TXS)) {
146 if (get_timer(start) > SPI_WAIT_TIMEOUT) {
147 printf("SPI TXS timed out, status=0x%08x\n",
148 readl(&priv->regs->channel[priv->cs].chstat));
153 unsigned int *tx = &priv->regs->channel[priv->cs].tx;
154 if (priv->wordlen > 16)
155 writel(((u32 *)txp)[i], tx);
156 else if (priv->wordlen > 8)
157 writel(((u16 *)txp)[i], tx);
159 writel(((u8 *)txp)[i], tx);
162 /* wait to finish of transfer */
163 while ((readl(&priv->regs->channel[priv->cs].chstat) &
164 (OMAP3_MCSPI_CHSTAT_EOT | OMAP3_MCSPI_CHSTAT_TXS)) !=
165 (OMAP3_MCSPI_CHSTAT_EOT | OMAP3_MCSPI_CHSTAT_TXS))
168 /* Disable the channel otherwise the next immediate RX will get affected */
169 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
171 if (flags & SPI_XFER_END) {
173 chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
174 omap3_spi_write_chconf(priv, chconf);
179 static int omap3_spi_read(struct omap3_spi_priv *priv, unsigned int len,
180 void *rxp, unsigned long flags)
185 chconf = readl(&priv->regs->channel[priv->cs].chconf);
187 /* Enable the channel */
188 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
190 chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK);
191 chconf |= (priv->wordlen - 1) << 7;
192 chconf |= OMAP3_MCSPI_CHCONF_TRM_RX_ONLY;
193 chconf |= OMAP3_MCSPI_CHCONF_FORCE;
194 omap3_spi_write_chconf(priv, chconf);
196 writel(0, &priv->regs->channel[priv->cs].tx);
198 for (i = 0; i < len; i++) {
199 start = get_timer(0);
200 /* Wait till RX register contains data (RXS == 1) */
201 while (!(readl(&priv->regs->channel[priv->cs].chstat) &
202 OMAP3_MCSPI_CHSTAT_RXS)) {
203 if (get_timer(start) > SPI_WAIT_TIMEOUT) {
204 printf("SPI RXS timed out, status=0x%08x\n",
205 readl(&priv->regs->channel[priv->cs].chstat));
210 /* Disable the channel to prevent furher receiving */
212 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
215 unsigned int *rx = &priv->regs->channel[priv->cs].rx;
216 if (priv->wordlen > 16)
217 ((u32 *)rxp)[i] = readl(rx);
218 else if (priv->wordlen > 8)
219 ((u16 *)rxp)[i] = (u16)readl(rx);
221 ((u8 *)rxp)[i] = (u8)readl(rx);
224 if (flags & SPI_XFER_END) {
225 chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
226 omap3_spi_write_chconf(priv, chconf);
232 /*McSPI Transmit Receive Mode*/
233 static int omap3_spi_txrx(struct omap3_spi_priv *priv, unsigned int len,
234 const void *txp, void *rxp, unsigned long flags)
239 chconf = readl(&priv->regs->channel[priv->cs].chconf);
241 /*Enable SPI channel*/
242 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
244 /*set TRANSMIT-RECEIVE Mode*/
245 chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK);
246 chconf |= (priv->wordlen - 1) << 7;
247 chconf |= OMAP3_MCSPI_CHCONF_FORCE;
248 omap3_spi_write_chconf(priv, chconf);
250 /*Shift in and out 1 byte at time*/
251 for (i=0; i < len; i++){
252 /* Write: wait for TX empty (TXS == 1)*/
253 start = get_timer(0);
254 while (!(readl(&priv->regs->channel[priv->cs].chstat) &
255 OMAP3_MCSPI_CHSTAT_TXS)) {
256 if (get_timer(start) > SPI_WAIT_TIMEOUT) {
257 printf("SPI TXS timed out, status=0x%08x\n",
258 readl(&priv->regs->channel[priv->cs].chstat));
263 unsigned int *tx = &priv->regs->channel[priv->cs].tx;
264 if (priv->wordlen > 16)
265 writel(((u32 *)txp)[i], tx);
266 else if (priv->wordlen > 8)
267 writel(((u16 *)txp)[i], tx);
269 writel(((u8 *)txp)[i], tx);
271 /*Read: wait for RX containing data (RXS == 1)*/
272 start = get_timer(0);
273 while (!(readl(&priv->regs->channel[priv->cs].chstat) &
274 OMAP3_MCSPI_CHSTAT_RXS)) {
275 if (get_timer(start) > SPI_WAIT_TIMEOUT) {
276 printf("SPI RXS timed out, status=0x%08x\n",
277 readl(&priv->regs->channel[priv->cs].chstat));
282 unsigned int *rx = &priv->regs->channel[priv->cs].rx;
283 if (priv->wordlen > 16)
284 ((u32 *)rxp)[i] = readl(rx);
285 else if (priv->wordlen > 8)
286 ((u16 *)rxp)[i] = (u16)readl(rx);
288 ((u8 *)rxp)[i] = (u8)readl(rx);
290 /* Disable the channel */
291 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
293 /*if transfer must be terminated disable the channel*/
294 if (flags & SPI_XFER_END) {
295 chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
296 omap3_spi_write_chconf(priv, chconf);
302 static int _spi_xfer(struct omap3_spi_priv *priv, unsigned int bitlen,
303 const void *dout, void *din, unsigned long flags)
308 if (priv->wordlen < 4 || priv->wordlen > 32) {
309 printf("omap3_spi: invalid wordlen %d\n", priv->wordlen);
313 if (bitlen % priv->wordlen)
316 len = bitlen / priv->wordlen;
318 if (bitlen == 0) { /* only change CS */
319 int chconf = readl(&priv->regs->channel[priv->cs].chconf);
321 if (flags & SPI_XFER_BEGIN) {
322 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
323 chconf |= OMAP3_MCSPI_CHCONF_FORCE;
324 omap3_spi_write_chconf(priv, chconf);
326 if (flags & SPI_XFER_END) {
327 chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
328 omap3_spi_write_chconf(priv, chconf);
329 omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
333 if (dout != NULL && din != NULL)
334 ret = omap3_spi_txrx(priv, len, dout, din, flags);
335 else if (dout != NULL)
336 ret = omap3_spi_write(priv, len, dout, flags);
337 else if (din != NULL)
338 ret = omap3_spi_read(priv, len, din, flags);
343 static void _omap3_spi_set_speed(struct omap3_spi_priv *priv)
345 uint32_t confr, div = 0;
347 confr = readl(&priv->regs->channel[priv->cs].chconf);
349 /* Calculate clock divisor. Valid range: 0x0 - 0xC ( /1 - /4096 ) */
351 while (div <= 0xC && (OMAP3_MCSPI_MAX_FREQ / (1 << div))
358 /* set clock divisor */
359 confr &= ~OMAP3_MCSPI_CHCONF_CLKD_MASK;
362 omap3_spi_write_chconf(priv, confr);
365 static void _omap3_spi_set_mode(struct omap3_spi_priv *priv)
369 confr = readl(&priv->regs->channel[priv->cs].chconf);
371 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
372 * REVISIT: this controller could support SPI_3WIRE mode.
374 if (priv->pin_dir == MCSPI_PINDIR_D0_IN_D1_OUT) {
375 confr &= ~(OMAP3_MCSPI_CHCONF_IS|OMAP3_MCSPI_CHCONF_DPE1);
376 confr |= OMAP3_MCSPI_CHCONF_DPE0;
378 confr &= ~OMAP3_MCSPI_CHCONF_DPE0;
379 confr |= OMAP3_MCSPI_CHCONF_IS|OMAP3_MCSPI_CHCONF_DPE1;
382 /* set SPI mode 0..3 */
383 confr &= ~(OMAP3_MCSPI_CHCONF_POL | OMAP3_MCSPI_CHCONF_PHA);
384 if (priv->mode & SPI_CPHA)
385 confr |= OMAP3_MCSPI_CHCONF_PHA;
386 if (priv->mode & SPI_CPOL)
387 confr |= OMAP3_MCSPI_CHCONF_POL;
389 /* set chipselect polarity; manage with FORCE */
390 if (!(priv->mode & SPI_CS_HIGH))
391 confr |= OMAP3_MCSPI_CHCONF_EPOL; /* active-low; normal */
393 confr &= ~OMAP3_MCSPI_CHCONF_EPOL;
395 /* Transmit & receive mode */
396 confr &= ~OMAP3_MCSPI_CHCONF_TRM_MASK;
398 omap3_spi_write_chconf(priv, confr);
401 static void _omap3_spi_set_wordlen(struct omap3_spi_priv *priv)
405 /* McSPI individual channel configuration */
406 confr = readl(&priv->regs->channel[priv->cs].chconf);
409 confr &= ~OMAP3_MCSPI_CHCONF_WL_MASK;
410 confr |= (priv->wordlen - 1) << 7;
412 omap3_spi_write_chconf(priv, confr);
415 static void spi_reset(struct mcspi *regs)
419 writel(OMAP3_MCSPI_SYSCONFIG_SOFTRESET, ®s->sysconfig);
421 tmp = readl(®s->sysstatus);
422 } while (!(tmp & OMAP3_MCSPI_SYSSTATUS_RESETDONE));
424 writel(OMAP3_MCSPI_SYSCONFIG_AUTOIDLE |
425 OMAP3_MCSPI_SYSCONFIG_ENAWAKEUP |
426 OMAP3_MCSPI_SYSCONFIG_SMARTIDLE, ®s->sysconfig);
428 writel(OMAP3_MCSPI_WAKEUPENABLE_WKEN, ®s->wakeupenable);
431 static void _omap3_spi_claim_bus(struct omap3_spi_priv *priv)
435 * setup when switching from (reset default) slave mode
436 * to single-channel master mode
438 conf = readl(&priv->regs->modulctrl);
439 conf &= ~(OMAP3_MCSPI_MODULCTRL_STEST | OMAP3_MCSPI_MODULCTRL_MS);
440 conf |= OMAP3_MCSPI_MODULCTRL_SINGLE;
442 writel(conf, &priv->regs->modulctrl);
445 static int omap3_spi_claim_bus(struct udevice *dev)
447 struct udevice *bus = dev->parent;
448 struct omap3_spi_priv *priv = dev_get_priv(bus);
449 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
451 priv->cs = slave_plat->cs;
452 priv->freq = slave_plat->max_hz;
454 _omap3_spi_claim_bus(priv);
459 static int omap3_spi_release_bus(struct udevice *dev)
461 struct udevice *bus = dev->parent;
462 struct omap3_spi_priv *priv = dev_get_priv(bus);
464 writel(OMAP3_MCSPI_MODULCTRL_MS, &priv->regs->modulctrl);
469 static int omap3_spi_set_wordlen(struct udevice *dev, unsigned int wordlen)
471 struct udevice *bus = dev->parent;
472 struct omap3_spi_priv *priv = dev_get_priv(bus);
473 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
475 priv->cs = slave_plat->cs;
476 priv->wordlen = wordlen;
477 _omap3_spi_set_wordlen(priv);
482 static int omap3_spi_probe(struct udevice *dev)
484 struct omap3_spi_priv *priv = dev_get_priv(dev);
485 const void *blob = gd->fdt_blob;
486 int node = dev_of_offset(dev);
488 struct omap2_mcspi_platform_config* data =
489 (struct omap2_mcspi_platform_config*)dev_get_driver_data(dev);
491 priv->regs = (struct mcspi *)(dev_read_addr(dev) + data->regs_offset);
492 if (fdtdec_get_bool(blob, node, "ti,pindir-d0-out-d1-in"))
493 priv->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN;
495 priv->pin_dir = MCSPI_PINDIR_D0_IN_D1_OUT;
496 priv->wordlen = SPI_DEFAULT_WORDLEN;
498 spi_reset(priv->regs);
503 static int omap3_spi_xfer(struct udevice *dev, unsigned int bitlen,
504 const void *dout, void *din, unsigned long flags)
506 struct udevice *bus = dev->parent;
507 struct omap3_spi_priv *priv = dev_get_priv(bus);
509 return _spi_xfer(priv, bitlen, dout, din, flags);
512 static int omap3_spi_set_speed(struct udevice *dev, unsigned int speed)
515 struct omap3_spi_priv *priv = dev_get_priv(dev);
518 _omap3_spi_set_speed(priv);
523 static int omap3_spi_set_mode(struct udevice *dev, uint mode)
525 struct omap3_spi_priv *priv = dev_get_priv(dev);
529 _omap3_spi_set_mode(priv);
534 static const struct dm_spi_ops omap3_spi_ops = {
535 .claim_bus = omap3_spi_claim_bus,
536 .release_bus = omap3_spi_release_bus,
537 .set_wordlen = omap3_spi_set_wordlen,
538 .xfer = omap3_spi_xfer,
539 .set_speed = omap3_spi_set_speed,
540 .set_mode = omap3_spi_set_mode,
542 * cs_info is not needed, since we require all chip selects to be
543 * in the device tree explicitly
547 static struct omap2_mcspi_platform_config omap2_pdata = {
551 static struct omap2_mcspi_platform_config omap4_pdata = {
552 .regs_offset = OMAP4_MCSPI_REG_OFFSET,
555 static const struct udevice_id omap3_spi_ids[] = {
556 { .compatible = "ti,omap2-mcspi", .data = (ulong)&omap2_pdata },
557 { .compatible = "ti,omap4-mcspi", .data = (ulong)&omap4_pdata },
561 U_BOOT_DRIVER(omap3_spi) = {
564 .of_match = omap3_spi_ids,
565 .probe = omap3_spi_probe,
566 .ops = &omap3_spi_ops,
567 .priv_auto_alloc_size = sizeof(struct omap3_spi_priv),