Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 2 | /* |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 3 | * Copyright (C) 2016 Jagan Teki <jteki@openedev.com> |
| 4 | * Christophe Ricard <christophe.ricard@gmail.com> |
| 5 | * |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 6 | * Copyright (C) 2010 Dirk Behme <dirk.behme@googlemail.com> |
| 7 | * |
| 8 | * Driver for McSPI controller on OMAP3. Based on davinci_spi.c |
| 9 | * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/ |
| 10 | * |
| 11 | * Copyright (C) 2007 Atmel Corporation |
| 12 | * |
| 13 | * Parts taken from linux/drivers/spi/omap2_mcspi.c |
| 14 | * Copyright (C) 2005, 2006 Nokia Corporation |
| 15 | * |
| 16 | * Modified by Ruslan Araslanov <ruslan.araslanov@vitecmm.com> |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #include <common.h> |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 20 | #include <dm.h> |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 21 | #include <spi.h> |
| 22 | #include <malloc.h> |
| 23 | #include <asm/io.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 24 | #include <linux/bitops.h> |
Faiz Abbas | 618ba9f | 2020-09-14 12:11:15 +0530 | [diff] [blame] | 25 | #include <omap3_spi.h> |
Jagan Teki | c2cca6c | 2016-03-01 15:16:20 +0100 | [diff] [blame] | 26 | |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 27 | DECLARE_GLOBAL_DATA_PTR; |
| 28 | |
Martin Hejnfelt | d74ea96 | 2016-05-19 09:11:58 +0200 | [diff] [blame] | 29 | struct omap2_mcspi_platform_config { |
| 30 | unsigned int regs_offset; |
| 31 | }; |
| 32 | |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 33 | struct omap3_spi_priv { |
Jagan Teki | c2cca6c | 2016-03-01 15:16:20 +0100 | [diff] [blame] | 34 | struct mcspi *regs; |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 35 | unsigned int cs; |
Jagan Teki | c2cca6c | 2016-03-01 15:16:20 +0100 | [diff] [blame] | 36 | unsigned int freq; |
| 37 | unsigned int mode; |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 38 | unsigned int wordlen; |
| 39 | unsigned int pin_dir:1; |
Jagan Teki | c2cca6c | 2016-03-01 15:16:20 +0100 | [diff] [blame] | 40 | }; |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 41 | |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 42 | static void omap3_spi_write_chconf(struct omap3_spi_priv *priv, int val) |
Jagan Teki | c2cca6c | 2016-03-01 15:16:20 +0100 | [diff] [blame] | 43 | { |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 44 | writel(val, &priv->regs->channel[priv->cs].chconf); |
ajoy | 260ab20 | 2012-11-17 21:10:15 +0000 | [diff] [blame] | 45 | /* Flash post writes to make immediate effect */ |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 46 | readl(&priv->regs->channel[priv->cs].chconf); |
ajoy | 260ab20 | 2012-11-17 21:10:15 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 49 | static void omap3_spi_set_enable(struct omap3_spi_priv *priv, int enable) |
ajoy | 260ab20 | 2012-11-17 21:10:15 +0000 | [diff] [blame] | 50 | { |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 51 | writel(enable, &priv->regs->channel[priv->cs].chctrl); |
Wolfgang Denk | ec7fbf5 | 2013-10-04 17:43:24 +0200 | [diff] [blame] | 52 | /* Flash post writes to make immediate effect */ |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 53 | readl(&priv->regs->channel[priv->cs].chctrl); |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 54 | } |
| 55 | |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 56 | static int omap3_spi_write(struct omap3_spi_priv *priv, unsigned int len, |
Jagan Teki | 40a55df | 2016-03-01 15:16:21 +0100 | [diff] [blame] | 57 | const void *txp, unsigned long flags) |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 58 | { |
David Dueck | a11b2a6 | 2015-04-01 14:20:24 +0200 | [diff] [blame] | 59 | ulong start; |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 60 | int i, chconf; |
| 61 | |
| 62 | chconf = readl(&priv->regs->channel[priv->cs].chconf); |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 63 | |
ajoy | 260ab20 | 2012-11-17 21:10:15 +0000 | [diff] [blame] | 64 | /* Enable the channel */ |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 65 | omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN); |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 66 | |
Nikita Kiryanov | 18dd07c | 2013-10-16 17:23:25 +0300 | [diff] [blame] | 67 | chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK); |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 68 | chconf |= (priv->wordlen - 1) << 7; |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 69 | chconf |= OMAP3_MCSPI_CHCONF_TRM_TX_ONLY; |
| 70 | chconf |= OMAP3_MCSPI_CHCONF_FORCE; |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 71 | omap3_spi_write_chconf(priv, chconf); |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 72 | |
| 73 | for (i = 0; i < len; i++) { |
| 74 | /* wait till TX register is empty (TXS == 1) */ |
David Dueck | a11b2a6 | 2015-04-01 14:20:24 +0200 | [diff] [blame] | 75 | start = get_timer(0); |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 76 | while (!(readl(&priv->regs->channel[priv->cs].chstat) & |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 77 | OMAP3_MCSPI_CHSTAT_TXS)) { |
David Dueck | a11b2a6 | 2015-04-01 14:20:24 +0200 | [diff] [blame] | 78 | if (get_timer(start) > SPI_WAIT_TIMEOUT) { |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 79 | printf("SPI TXS timed out, status=0x%08x\n", |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 80 | readl(&priv->regs->channel[priv->cs].chstat)); |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 81 | return -1; |
| 82 | } |
| 83 | } |
| 84 | /* Write the data */ |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 85 | unsigned int *tx = &priv->regs->channel[priv->cs].tx; |
| 86 | if (priv->wordlen > 16) |
Nikita Kiryanov | 18dd07c | 2013-10-16 17:23:25 +0300 | [diff] [blame] | 87 | writel(((u32 *)txp)[i], tx); |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 88 | else if (priv->wordlen > 8) |
Nikita Kiryanov | 18dd07c | 2013-10-16 17:23:25 +0300 | [diff] [blame] | 89 | writel(((u16 *)txp)[i], tx); |
| 90 | else |
| 91 | writel(((u8 *)txp)[i], tx); |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 92 | } |
| 93 | |
Wolfgang Denk | ec7fbf5 | 2013-10-04 17:43:24 +0200 | [diff] [blame] | 94 | /* wait to finish of transfer */ |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 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)) |
| 98 | ; |
ajoy | 260ab20 | 2012-11-17 21:10:15 +0000 | [diff] [blame] | 99 | |
| 100 | /* Disable the channel otherwise the next immediate RX will get affected */ |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 101 | omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS); |
ajoy | 260ab20 | 2012-11-17 21:10:15 +0000 | [diff] [blame] | 102 | |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 103 | if (flags & SPI_XFER_END) { |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 104 | |
| 105 | chconf &= ~OMAP3_MCSPI_CHCONF_FORCE; |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 106 | omap3_spi_write_chconf(priv, chconf); |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 107 | } |
| 108 | return 0; |
| 109 | } |
| 110 | |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 111 | static int omap3_spi_read(struct omap3_spi_priv *priv, unsigned int len, |
Jagan Teki | 40a55df | 2016-03-01 15:16:21 +0100 | [diff] [blame] | 112 | void *rxp, unsigned long flags) |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 113 | { |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 114 | int i, chconf; |
David Dueck | a11b2a6 | 2015-04-01 14:20:24 +0200 | [diff] [blame] | 115 | ulong start; |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 116 | |
| 117 | chconf = readl(&priv->regs->channel[priv->cs].chconf); |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 118 | |
ajoy | 260ab20 | 2012-11-17 21:10:15 +0000 | [diff] [blame] | 119 | /* Enable the channel */ |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 120 | omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN); |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 121 | |
Nikita Kiryanov | 18dd07c | 2013-10-16 17:23:25 +0300 | [diff] [blame] | 122 | chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK); |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 123 | chconf |= (priv->wordlen - 1) << 7; |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 124 | chconf |= OMAP3_MCSPI_CHCONF_TRM_RX_ONLY; |
| 125 | chconf |= OMAP3_MCSPI_CHCONF_FORCE; |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 126 | omap3_spi_write_chconf(priv, chconf); |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 127 | |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 128 | writel(0, &priv->regs->channel[priv->cs].tx); |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 129 | |
| 130 | for (i = 0; i < len; i++) { |
David Dueck | a11b2a6 | 2015-04-01 14:20:24 +0200 | [diff] [blame] | 131 | start = get_timer(0); |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 132 | /* Wait till RX register contains data (RXS == 1) */ |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 133 | while (!(readl(&priv->regs->channel[priv->cs].chstat) & |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 134 | OMAP3_MCSPI_CHSTAT_RXS)) { |
David Dueck | a11b2a6 | 2015-04-01 14:20:24 +0200 | [diff] [blame] | 135 | if (get_timer(start) > SPI_WAIT_TIMEOUT) { |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 136 | printf("SPI RXS timed out, status=0x%08x\n", |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 137 | readl(&priv->regs->channel[priv->cs].chstat)); |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 138 | return -1; |
| 139 | } |
| 140 | } |
ajoy | 260ab20 | 2012-11-17 21:10:15 +0000 | [diff] [blame] | 141 | |
| 142 | /* Disable the channel to prevent furher receiving */ |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 143 | if (i == (len - 1)) |
| 144 | omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS); |
ajoy | 260ab20 | 2012-11-17 21:10:15 +0000 | [diff] [blame] | 145 | |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 146 | /* Read the data */ |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 147 | unsigned int *rx = &priv->regs->channel[priv->cs].rx; |
| 148 | if (priv->wordlen > 16) |
Nikita Kiryanov | 18dd07c | 2013-10-16 17:23:25 +0300 | [diff] [blame] | 149 | ((u32 *)rxp)[i] = readl(rx); |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 150 | else if (priv->wordlen > 8) |
Nikita Kiryanov | 18dd07c | 2013-10-16 17:23:25 +0300 | [diff] [blame] | 151 | ((u16 *)rxp)[i] = (u16)readl(rx); |
| 152 | else |
| 153 | ((u8 *)rxp)[i] = (u8)readl(rx); |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | if (flags & SPI_XFER_END) { |
| 157 | chconf &= ~OMAP3_MCSPI_CHCONF_FORCE; |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 158 | omap3_spi_write_chconf(priv, chconf); |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
jacopo mondi | fe8793e | 2011-03-02 05:13:22 +0000 | [diff] [blame] | 164 | /*McSPI Transmit Receive Mode*/ |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 165 | static int omap3_spi_txrx(struct omap3_spi_priv *priv, unsigned int len, |
Jagan Teki | 40a55df | 2016-03-01 15:16:21 +0100 | [diff] [blame] | 166 | const void *txp, void *rxp, unsigned long flags) |
jacopo mondi | fe8793e | 2011-03-02 05:13:22 +0000 | [diff] [blame] | 167 | { |
David Dueck | a11b2a6 | 2015-04-01 14:20:24 +0200 | [diff] [blame] | 168 | ulong start; |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 169 | int chconf, i = 0; |
| 170 | |
| 171 | chconf = readl(&priv->regs->channel[priv->cs].chconf); |
jacopo mondi | fe8793e | 2011-03-02 05:13:22 +0000 | [diff] [blame] | 172 | |
| 173 | /*Enable SPI channel*/ |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 174 | omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN); |
jacopo mondi | fe8793e | 2011-03-02 05:13:22 +0000 | [diff] [blame] | 175 | |
| 176 | /*set TRANSMIT-RECEIVE Mode*/ |
Nikita Kiryanov | 18dd07c | 2013-10-16 17:23:25 +0300 | [diff] [blame] | 177 | chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK); |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 178 | chconf |= (priv->wordlen - 1) << 7; |
jacopo mondi | fe8793e | 2011-03-02 05:13:22 +0000 | [diff] [blame] | 179 | chconf |= OMAP3_MCSPI_CHCONF_FORCE; |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 180 | omap3_spi_write_chconf(priv, chconf); |
jacopo mondi | fe8793e | 2011-03-02 05:13:22 +0000 | [diff] [blame] | 181 | |
| 182 | /*Shift in and out 1 byte at time*/ |
| 183 | for (i=0; i < len; i++){ |
| 184 | /* Write: wait for TX empty (TXS == 1)*/ |
David Dueck | a11b2a6 | 2015-04-01 14:20:24 +0200 | [diff] [blame] | 185 | start = get_timer(0); |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 186 | while (!(readl(&priv->regs->channel[priv->cs].chstat) & |
jacopo mondi | fe8793e | 2011-03-02 05:13:22 +0000 | [diff] [blame] | 187 | OMAP3_MCSPI_CHSTAT_TXS)) { |
David Dueck | a11b2a6 | 2015-04-01 14:20:24 +0200 | [diff] [blame] | 188 | if (get_timer(start) > SPI_WAIT_TIMEOUT) { |
jacopo mondi | fe8793e | 2011-03-02 05:13:22 +0000 | [diff] [blame] | 189 | printf("SPI TXS timed out, status=0x%08x\n", |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 190 | readl(&priv->regs->channel[priv->cs].chstat)); |
jacopo mondi | fe8793e | 2011-03-02 05:13:22 +0000 | [diff] [blame] | 191 | return -1; |
| 192 | } |
| 193 | } |
| 194 | /* Write the data */ |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 195 | unsigned int *tx = &priv->regs->channel[priv->cs].tx; |
| 196 | if (priv->wordlen > 16) |
Nikita Kiryanov | 18dd07c | 2013-10-16 17:23:25 +0300 | [diff] [blame] | 197 | writel(((u32 *)txp)[i], tx); |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 198 | else if (priv->wordlen > 8) |
Nikita Kiryanov | 18dd07c | 2013-10-16 17:23:25 +0300 | [diff] [blame] | 199 | writel(((u16 *)txp)[i], tx); |
| 200 | else |
| 201 | writel(((u8 *)txp)[i], tx); |
jacopo mondi | fe8793e | 2011-03-02 05:13:22 +0000 | [diff] [blame] | 202 | |
| 203 | /*Read: wait for RX containing data (RXS == 1)*/ |
David Dueck | a11b2a6 | 2015-04-01 14:20:24 +0200 | [diff] [blame] | 204 | start = get_timer(0); |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 205 | while (!(readl(&priv->regs->channel[priv->cs].chstat) & |
jacopo mondi | fe8793e | 2011-03-02 05:13:22 +0000 | [diff] [blame] | 206 | OMAP3_MCSPI_CHSTAT_RXS)) { |
David Dueck | a11b2a6 | 2015-04-01 14:20:24 +0200 | [diff] [blame] | 207 | if (get_timer(start) > SPI_WAIT_TIMEOUT) { |
jacopo mondi | fe8793e | 2011-03-02 05:13:22 +0000 | [diff] [blame] | 208 | printf("SPI RXS timed out, status=0x%08x\n", |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 209 | readl(&priv->regs->channel[priv->cs].chstat)); |
jacopo mondi | fe8793e | 2011-03-02 05:13:22 +0000 | [diff] [blame] | 210 | return -1; |
| 211 | } |
| 212 | } |
| 213 | /* Read the data */ |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 214 | unsigned int *rx = &priv->regs->channel[priv->cs].rx; |
| 215 | if (priv->wordlen > 16) |
Nikita Kiryanov | 18dd07c | 2013-10-16 17:23:25 +0300 | [diff] [blame] | 216 | ((u32 *)rxp)[i] = readl(rx); |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 217 | else if (priv->wordlen > 8) |
Nikita Kiryanov | 18dd07c | 2013-10-16 17:23:25 +0300 | [diff] [blame] | 218 | ((u16 *)rxp)[i] = (u16)readl(rx); |
| 219 | else |
| 220 | ((u8 *)rxp)[i] = (u8)readl(rx); |
jacopo mondi | fe8793e | 2011-03-02 05:13:22 +0000 | [diff] [blame] | 221 | } |
ajoy | 260ab20 | 2012-11-17 21:10:15 +0000 | [diff] [blame] | 222 | /* Disable the channel */ |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 223 | omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS); |
jacopo mondi | fe8793e | 2011-03-02 05:13:22 +0000 | [diff] [blame] | 224 | |
| 225 | /*if transfer must be terminated disable the channel*/ |
| 226 | if (flags & SPI_XFER_END) { |
| 227 | chconf &= ~OMAP3_MCSPI_CHCONF_FORCE; |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 228 | omap3_spi_write_chconf(priv, chconf); |
jacopo mondi | fe8793e | 2011-03-02 05:13:22 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 234 | static int _spi_xfer(struct omap3_spi_priv *priv, unsigned int bitlen, |
| 235 | const void *dout, void *din, unsigned long flags) |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 236 | { |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 237 | unsigned int len; |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 238 | int ret = -1; |
| 239 | |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 240 | if (priv->wordlen < 4 || priv->wordlen > 32) { |
| 241 | printf("omap3_spi: invalid wordlen %d\n", priv->wordlen); |
Nikita Kiryanov | 18dd07c | 2013-10-16 17:23:25 +0300 | [diff] [blame] | 242 | return -1; |
| 243 | } |
| 244 | |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 245 | if (bitlen % priv->wordlen) |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 246 | return -1; |
| 247 | |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 248 | len = bitlen / priv->wordlen; |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 249 | |
| 250 | if (bitlen == 0) { /* only change CS */ |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 251 | int chconf = readl(&priv->regs->channel[priv->cs].chconf); |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 252 | |
| 253 | if (flags & SPI_XFER_BEGIN) { |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 254 | omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN); |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 255 | chconf |= OMAP3_MCSPI_CHCONF_FORCE; |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 256 | omap3_spi_write_chconf(priv, chconf); |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 257 | } |
| 258 | if (flags & SPI_XFER_END) { |
| 259 | chconf &= ~OMAP3_MCSPI_CHCONF_FORCE; |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 260 | omap3_spi_write_chconf(priv, chconf); |
| 261 | omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS); |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 262 | } |
| 263 | ret = 0; |
| 264 | } else { |
jacopo mondi | fe8793e | 2011-03-02 05:13:22 +0000 | [diff] [blame] | 265 | if (dout != NULL && din != NULL) |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 266 | ret = omap3_spi_txrx(priv, len, dout, din, flags); |
jacopo mondi | fe8793e | 2011-03-02 05:13:22 +0000 | [diff] [blame] | 267 | else if (dout != NULL) |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 268 | ret = omap3_spi_write(priv, len, dout, flags); |
jacopo mondi | fe8793e | 2011-03-02 05:13:22 +0000 | [diff] [blame] | 269 | else if (din != NULL) |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 270 | ret = omap3_spi_read(priv, len, din, flags); |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 271 | } |
| 272 | return ret; |
| 273 | } |
| 274 | |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 275 | static void _omap3_spi_set_speed(struct omap3_spi_priv *priv) |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 276 | { |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 277 | uint32_t confr, div = 0; |
| 278 | |
| 279 | confr = readl(&priv->regs->channel[priv->cs].chconf); |
| 280 | |
| 281 | /* Calculate clock divisor. Valid range: 0x0 - 0xC ( /1 - /4096 ) */ |
| 282 | if (priv->freq) { |
| 283 | while (div <= 0xC && (OMAP3_MCSPI_MAX_FREQ / (1 << div)) |
| 284 | > priv->freq) |
| 285 | div++; |
| 286 | } else { |
| 287 | div = 0xC; |
| 288 | } |
| 289 | |
| 290 | /* set clock divisor */ |
| 291 | confr &= ~OMAP3_MCSPI_CHCONF_CLKD_MASK; |
| 292 | confr |= div << 2; |
| 293 | |
| 294 | omap3_spi_write_chconf(priv, confr); |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 295 | } |
| 296 | |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 297 | static void _omap3_spi_set_mode(struct omap3_spi_priv *priv) |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 298 | { |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 299 | uint32_t confr; |
| 300 | |
| 301 | confr = readl(&priv->regs->channel[priv->cs].chconf); |
| 302 | |
| 303 | /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS |
| 304 | * REVISIT: this controller could support SPI_3WIRE mode. |
| 305 | */ |
| 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; |
| 309 | } else { |
| 310 | confr &= ~OMAP3_MCSPI_CHCONF_DPE0; |
| 311 | confr |= OMAP3_MCSPI_CHCONF_IS|OMAP3_MCSPI_CHCONF_DPE1; |
| 312 | } |
| 313 | |
| 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; |
| 320 | |
| 321 | /* set chipselect polarity; manage with FORCE */ |
| 322 | if (!(priv->mode & SPI_CS_HIGH)) |
| 323 | confr |= OMAP3_MCSPI_CHCONF_EPOL; /* active-low; normal */ |
| 324 | else |
| 325 | confr &= ~OMAP3_MCSPI_CHCONF_EPOL; |
| 326 | |
| 327 | /* Transmit & receive mode */ |
| 328 | confr &= ~OMAP3_MCSPI_CHCONF_TRM_MASK; |
| 329 | |
| 330 | omap3_spi_write_chconf(priv, confr); |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 331 | } |
| 332 | |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 333 | static void _omap3_spi_set_wordlen(struct omap3_spi_priv *priv) |
Dirk Behme | 78cd9ac | 2010-12-11 11:01:00 -0500 | [diff] [blame] | 334 | { |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 335 | unsigned int confr; |
| 336 | |
| 337 | /* McSPI individual channel configuration */ |
David Rivshin | 7aacc91 | 2019-02-18 18:04:29 -0500 | [diff] [blame] | 338 | confr = readl(&priv->regs->channel[priv->cs].chconf); |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 339 | |
| 340 | /* wordlength */ |
| 341 | confr &= ~OMAP3_MCSPI_CHCONF_WL_MASK; |
| 342 | confr |= (priv->wordlen - 1) << 7; |
| 343 | |
| 344 | omap3_spi_write_chconf(priv, confr); |
| 345 | } |
| 346 | |
| 347 | static void spi_reset(struct mcspi *regs) |
| 348 | { |
| 349 | unsigned int tmp; |
| 350 | |
| 351 | writel(OMAP3_MCSPI_SYSCONFIG_SOFTRESET, ®s->sysconfig); |
| 352 | do { |
| 353 | tmp = readl(®s->sysstatus); |
| 354 | } while (!(tmp & OMAP3_MCSPI_SYSSTATUS_RESETDONE)); |
| 355 | |
| 356 | writel(OMAP3_MCSPI_SYSCONFIG_AUTOIDLE | |
| 357 | OMAP3_MCSPI_SYSCONFIG_ENAWAKEUP | |
| 358 | OMAP3_MCSPI_SYSCONFIG_SMARTIDLE, ®s->sysconfig); |
| 359 | |
| 360 | writel(OMAP3_MCSPI_WAKEUPENABLE_WKEN, ®s->wakeupenable); |
| 361 | } |
| 362 | |
| 363 | static void _omap3_spi_claim_bus(struct omap3_spi_priv *priv) |
| 364 | { |
| 365 | unsigned int conf; |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 366 | /* |
| 367 | * setup when switching from (reset default) slave mode |
| 368 | * to single-channel master mode |
| 369 | */ |
| 370 | conf = readl(&priv->regs->modulctrl); |
| 371 | conf &= ~(OMAP3_MCSPI_MODULCTRL_STEST | OMAP3_MCSPI_MODULCTRL_MS); |
| 372 | conf |= OMAP3_MCSPI_MODULCTRL_SINGLE; |
| 373 | |
| 374 | writel(conf, &priv->regs->modulctrl); |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 375 | } |
| 376 | |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 377 | static int omap3_spi_claim_bus(struct udevice *dev) |
| 378 | { |
| 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_platdata(dev); |
| 382 | |
| 383 | priv->cs = slave_plat->cs; |
Hannes Schmelzer | 9e77880 | 2018-06-02 08:06:47 +0200 | [diff] [blame] | 384 | priv->freq = slave_plat->max_hz; |
| 385 | |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 386 | _omap3_spi_claim_bus(priv); |
| 387 | |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | static int omap3_spi_release_bus(struct udevice *dev) |
| 392 | { |
| 393 | struct udevice *bus = dev->parent; |
| 394 | struct omap3_spi_priv *priv = dev_get_priv(bus); |
| 395 | |
Hannes Schmelzer | 202f9a4 | 2018-06-26 16:08:39 +0200 | [diff] [blame] | 396 | writel(OMAP3_MCSPI_MODULCTRL_MS, &priv->regs->modulctrl); |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 397 | |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | static int omap3_spi_set_wordlen(struct udevice *dev, unsigned int wordlen) |
| 402 | { |
| 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_platdata(dev); |
| 406 | |
| 407 | priv->cs = slave_plat->cs; |
| 408 | priv->wordlen = wordlen; |
| 409 | _omap3_spi_set_wordlen(priv); |
| 410 | |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | static int omap3_spi_probe(struct udevice *dev) |
| 415 | { |
| 416 | struct omap3_spi_priv *priv = dev_get_priv(dev); |
Faiz Abbas | 12b2fb2 | 2020-09-14 12:11:16 +0530 | [diff] [blame] | 417 | struct omap3_spi_plat *plat = dev_get_platdata(dev); |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 418 | |
Faiz Abbas | 12b2fb2 | 2020-09-14 12:11:16 +0530 | [diff] [blame] | 419 | priv->regs = plat->regs; |
| 420 | priv->pin_dir = plat->pin_dir; |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 421 | priv->wordlen = SPI_DEFAULT_WORDLEN; |
Hannes Schmelzer | 202f9a4 | 2018-06-26 16:08:39 +0200 | [diff] [blame] | 422 | |
| 423 | spi_reset(priv->regs); |
| 424 | |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | static int omap3_spi_xfer(struct udevice *dev, unsigned int bitlen, |
| 429 | const void *dout, void *din, unsigned long flags) |
| 430 | { |
| 431 | struct udevice *bus = dev->parent; |
| 432 | struct omap3_spi_priv *priv = dev_get_priv(bus); |
| 433 | |
| 434 | return _spi_xfer(priv, bitlen, dout, din, flags); |
| 435 | } |
| 436 | |
Jagan Teki | 55865a9 | 2018-03-14 23:07:31 +0530 | [diff] [blame] | 437 | static int omap3_spi_set_speed(struct udevice *dev, unsigned int speed) |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 438 | { |
Jagan Teki | 96a95ee | 2018-03-07 10:30:45 +0530 | [diff] [blame] | 439 | |
Hannes Schmelzer | 7e4f833 | 2018-06-02 08:06:48 +0200 | [diff] [blame] | 440 | struct omap3_spi_priv *priv = dev_get_priv(dev); |
| 441 | |
| 442 | priv->freq = speed; |
Jagan Teki | 96a95ee | 2018-03-07 10:30:45 +0530 | [diff] [blame] | 443 | _omap3_spi_set_speed(priv); |
| 444 | |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 445 | return 0; |
| 446 | } |
| 447 | |
Jagan Teki | 55865a9 | 2018-03-14 23:07:31 +0530 | [diff] [blame] | 448 | static int omap3_spi_set_mode(struct udevice *dev, uint mode) |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 449 | { |
Hannes Schmelzer | 7e4f833 | 2018-06-02 08:06:48 +0200 | [diff] [blame] | 450 | struct omap3_spi_priv *priv = dev_get_priv(dev); |
| 451 | |
| 452 | priv->mode = mode; |
Jagan Teki | 96a95ee | 2018-03-07 10:30:45 +0530 | [diff] [blame] | 453 | |
Jagan Teki | 96a95ee | 2018-03-07 10:30:45 +0530 | [diff] [blame] | 454 | _omap3_spi_set_mode(priv); |
| 455 | |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 456 | return 0; |
| 457 | } |
| 458 | |
| 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, |
| 466 | /* |
| 467 | * cs_info is not needed, since we require all chip selects to be |
| 468 | * in the device tree explicitly |
| 469 | */ |
| 470 | }; |
| 471 | |
Faiz Abbas | 12b2fb2 | 2020-09-14 12:11:16 +0530 | [diff] [blame] | 472 | #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA) |
Martin Hejnfelt | d74ea96 | 2016-05-19 09:11:58 +0200 | [diff] [blame] | 473 | static struct omap2_mcspi_platform_config omap2_pdata = { |
| 474 | .regs_offset = 0, |
| 475 | }; |
| 476 | |
| 477 | static struct omap2_mcspi_platform_config omap4_pdata = { |
| 478 | .regs_offset = OMAP4_MCSPI_REG_OFFSET, |
| 479 | }; |
| 480 | |
Faiz Abbas | 12b2fb2 | 2020-09-14 12:11:16 +0530 | [diff] [blame] | 481 | static int omap3_spi_ofdata_to_platdata(struct udevice *dev) |
| 482 | { |
| 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); |
| 486 | |
| 487 | plat->regs = (struct mcspi *)(dev_read_addr(dev) + data->regs_offset); |
| 488 | |
| 489 | if (dev_read_bool(dev, "ti,pindir-d0-out-d1-in")) |
| 490 | plat->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN; |
| 491 | else |
| 492 | plat->pin_dir = MCSPI_PINDIR_D0_IN_D1_OUT; |
| 493 | |
| 494 | return 0; |
| 495 | } |
| 496 | |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 497 | static const struct udevice_id omap3_spi_ids[] = { |
Martin Hejnfelt | d74ea96 | 2016-05-19 09:11:58 +0200 | [diff] [blame] | 498 | { .compatible = "ti,omap2-mcspi", .data = (ulong)&omap2_pdata }, |
| 499 | { .compatible = "ti,omap4-mcspi", .data = (ulong)&omap4_pdata }, |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 500 | { } |
| 501 | }; |
Faiz Abbas | 12b2fb2 | 2020-09-14 12:11:16 +0530 | [diff] [blame] | 502 | #endif |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 503 | U_BOOT_DRIVER(omap3_spi) = { |
| 504 | .name = "omap3_spi", |
| 505 | .id = UCLASS_SPI, |
Faiz Abbas | 12b2fb2 | 2020-09-14 12:11:16 +0530 | [diff] [blame] | 506 | .flags = DM_FLAG_PRE_RELOC, |
| 507 | #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA) |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 508 | .of_match = omap3_spi_ids, |
Faiz Abbas | 12b2fb2 | 2020-09-14 12:11:16 +0530 | [diff] [blame] | 509 | .ofdata_to_platdata = omap3_spi_ofdata_to_platdata, |
| 510 | .platdata_auto_alloc_size = sizeof(struct omap3_spi_plat), |
| 511 | #endif |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 512 | .probe = omap3_spi_probe, |
| 513 | .ops = &omap3_spi_ops, |
| 514 | .priv_auto_alloc_size = sizeof(struct omap3_spi_priv), |
Jagan Teki | ea17242 | 2016-03-14 22:41:24 +0530 | [diff] [blame] | 515 | }; |