Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2012 SAMSUNG Electronics |
| 4 | * Padmavathi Venna <padma.v@samsung.com> |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 8 | #include <dm.h> |
| 9 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 11 | #include <malloc.h> |
| 12 | #include <spi.h> |
Rajeshwari Shinde | 64ef8a3 | 2012-12-26 20:03:23 +0000 | [diff] [blame] | 13 | #include <fdtdec.h> |
Simon Glass | 495a5dc | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 14 | #include <time.h> |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 15 | #include <asm/arch/clk.h> |
| 16 | #include <asm/arch/clock.h> |
| 17 | #include <asm/arch/cpu.h> |
| 18 | #include <asm/arch/gpio.h> |
| 19 | #include <asm/arch/pinmux.h> |
Thomas Abraham | 74f8486 | 2015-08-03 17:58:00 +0530 | [diff] [blame] | 20 | #include <asm/arch/spi.h> |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 21 | #include <asm/io.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 22 | #include <linux/delay.h> |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 23 | |
Rajeshwari Shinde | 64ef8a3 | 2012-12-26 20:03:23 +0000 | [diff] [blame] | 24 | DECLARE_GLOBAL_DATA_PTR; |
| 25 | |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 26 | struct exynos_spi_platdata { |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 27 | enum periph_id periph_id; |
| 28 | s32 frequency; /* Default clock frequency, -1 for none */ |
| 29 | struct exynos_spi *regs; |
Rajeshwari Shinde | ab46adb | 2013-10-08 16:20:04 +0530 | [diff] [blame] | 30 | uint deactivate_delay_us; /* Delay to wait after deactivate */ |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 33 | struct exynos_spi_priv { |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 34 | struct exynos_spi *regs; |
| 35 | unsigned int freq; /* Default frequency */ |
| 36 | unsigned int mode; |
| 37 | enum periph_id periph_id; /* Peripheral ID for this device */ |
| 38 | unsigned int fifo_size; |
Rajeshwari Shinde | 813637c | 2013-05-28 20:10:38 +0000 | [diff] [blame] | 39 | int skip_preamble; |
Rajeshwari Shinde | ab46adb | 2013-10-08 16:20:04 +0530 | [diff] [blame] | 40 | ulong last_transaction_us; /* Time of last transaction end */ |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 43 | /** |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 44 | * Flush spi tx, rx fifos and reset the SPI controller |
| 45 | * |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 46 | * @param regs Pointer to SPI registers |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 47 | */ |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 48 | static void spi_flush_fifo(struct exynos_spi *regs) |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 49 | { |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 50 | clrsetbits_le32(®s->ch_cfg, SPI_CH_HS_EN, SPI_CH_RST); |
| 51 | clrbits_le32(®s->ch_cfg, SPI_CH_RST); |
| 52 | setbits_le32(®s->ch_cfg, SPI_TX_CH_ON | SPI_RX_CH_ON); |
| 53 | } |
| 54 | |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 55 | static void spi_get_fifo_levels(struct exynos_spi *regs, |
| 56 | int *rx_lvl, int *tx_lvl) |
| 57 | { |
| 58 | uint32_t spi_sts = readl(®s->spi_sts); |
| 59 | |
| 60 | *rx_lvl = (spi_sts >> SPI_RX_LVL_OFFSET) & SPI_FIFO_LVL_MASK; |
| 61 | *tx_lvl = (spi_sts >> SPI_TX_LVL_OFFSET) & SPI_FIFO_LVL_MASK; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * If there's something to transfer, do a software reset and set a |
| 66 | * transaction size. |
| 67 | * |
| 68 | * @param regs SPI peripheral registers |
| 69 | * @param count Number of bytes to transfer |
Rajeshwari Shinde | ffc7461 | 2013-10-08 16:20:06 +0530 | [diff] [blame] | 70 | * @param step Number of bytes to transfer in each packet (1 or 4) |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 71 | */ |
Rajeshwari Shinde | ffc7461 | 2013-10-08 16:20:06 +0530 | [diff] [blame] | 72 | static void spi_request_bytes(struct exynos_spi *regs, int count, int step) |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 73 | { |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 74 | debug("%s: regs=%p, count=%d, step=%d\n", __func__, regs, count, step); |
| 75 | |
Rajeshwari Shinde | ffc7461 | 2013-10-08 16:20:06 +0530 | [diff] [blame] | 76 | /* For word address we need to swap bytes */ |
| 77 | if (step == 4) { |
| 78 | setbits_le32(®s->mode_cfg, |
| 79 | SPI_MODE_CH_WIDTH_WORD | SPI_MODE_BUS_WIDTH_WORD); |
| 80 | count /= 4; |
| 81 | setbits_le32(®s->swap_cfg, SPI_TX_SWAP_EN | SPI_RX_SWAP_EN | |
| 82 | SPI_TX_BYTE_SWAP | SPI_RX_BYTE_SWAP | |
| 83 | SPI_TX_HWORD_SWAP | SPI_RX_HWORD_SWAP); |
| 84 | } else { |
| 85 | /* Select byte access and clear the swap configuration */ |
| 86 | clrbits_le32(®s->mode_cfg, |
| 87 | SPI_MODE_CH_WIDTH_WORD | SPI_MODE_BUS_WIDTH_WORD); |
| 88 | writel(0, ®s->swap_cfg); |
| 89 | } |
| 90 | |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 91 | assert(count && count < (1 << 16)); |
| 92 | setbits_le32(®s->ch_cfg, SPI_CH_RST); |
| 93 | clrbits_le32(®s->ch_cfg, SPI_CH_RST); |
Rajeshwari Shinde | ffc7461 | 2013-10-08 16:20:06 +0530 | [diff] [blame] | 94 | |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 95 | writel(count | SPI_PACKET_CNT_EN, ®s->pkt_cnt); |
| 96 | } |
| 97 | |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 98 | static int spi_rx_tx(struct exynos_spi_priv *priv, int todo, |
Rajeshwari Shinde | 813637c | 2013-05-28 20:10:38 +0000 | [diff] [blame] | 99 | void **dinp, void const **doutp, unsigned long flags) |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 100 | { |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 101 | struct exynos_spi *regs = priv->regs; |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 102 | uchar *rxp = *dinp; |
| 103 | const uchar *txp = *doutp; |
| 104 | int rx_lvl, tx_lvl; |
| 105 | uint out_bytes, in_bytes; |
Rajeshwari Shinde | 813637c | 2013-05-28 20:10:38 +0000 | [diff] [blame] | 106 | int toread; |
| 107 | unsigned start = get_timer(0); |
| 108 | int stopping; |
Rajeshwari Shinde | ffc7461 | 2013-10-08 16:20:06 +0530 | [diff] [blame] | 109 | int step; |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 110 | |
| 111 | out_bytes = in_bytes = todo; |
| 112 | |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 113 | stopping = priv->skip_preamble && (flags & SPI_XFER_END) && |
| 114 | !(priv->mode & SPI_SLAVE); |
Rajeshwari Shinde | 813637c | 2013-05-28 20:10:38 +0000 | [diff] [blame] | 115 | |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 116 | /* |
Rajeshwari Shinde | ffc7461 | 2013-10-08 16:20:06 +0530 | [diff] [blame] | 117 | * Try to transfer words if we can. This helps read performance at |
| 118 | * SPI clock speeds above about 20MHz. |
| 119 | */ |
| 120 | step = 1; |
| 121 | if (!((todo | (uintptr_t)rxp | (uintptr_t)txp) & 3) && |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 122 | !priv->skip_preamble) |
Rajeshwari Shinde | ffc7461 | 2013-10-08 16:20:06 +0530 | [diff] [blame] | 123 | step = 4; |
| 124 | |
| 125 | /* |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 126 | * If there's something to send, do a software reset and set a |
| 127 | * transaction size. |
| 128 | */ |
Rajeshwari Shinde | ffc7461 | 2013-10-08 16:20:06 +0530 | [diff] [blame] | 129 | spi_request_bytes(regs, todo, step); |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 130 | |
| 131 | /* |
| 132 | * Bytes are transmitted/received in pairs. Wait to receive all the |
| 133 | * data because then transmission will be done as well. |
| 134 | */ |
Rajeshwari Shinde | 813637c | 2013-05-28 20:10:38 +0000 | [diff] [blame] | 135 | toread = in_bytes; |
| 136 | |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 137 | while (in_bytes) { |
| 138 | int temp; |
| 139 | |
| 140 | /* Keep the fifos full/empty. */ |
| 141 | spi_get_fifo_levels(regs, &rx_lvl, &tx_lvl); |
Rajeshwari Shinde | ffc7461 | 2013-10-08 16:20:06 +0530 | [diff] [blame] | 142 | |
| 143 | /* |
| 144 | * Don't completely fill the txfifo, since we don't want our |
| 145 | * rxfifo to overflow, and it may already contain data. |
| 146 | */ |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 147 | while (tx_lvl < priv->fifo_size/2 && out_bytes) { |
Rajeshwari Shinde | ffc7461 | 2013-10-08 16:20:06 +0530 | [diff] [blame] | 148 | if (!txp) |
| 149 | temp = -1; |
| 150 | else if (step == 4) |
| 151 | temp = *(uint32_t *)txp; |
| 152 | else |
| 153 | temp = *txp; |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 154 | writel(temp, ®s->tx_data); |
Rajeshwari Shinde | ffc7461 | 2013-10-08 16:20:06 +0530 | [diff] [blame] | 155 | out_bytes -= step; |
| 156 | if (txp) |
| 157 | txp += step; |
| 158 | tx_lvl += step; |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 159 | } |
Rajeshwari Shinde | ffc7461 | 2013-10-08 16:20:06 +0530 | [diff] [blame] | 160 | if (rx_lvl >= step) { |
| 161 | while (rx_lvl >= step) { |
Rajeshwari Shinde | 0c0faef | 2013-10-08 16:20:05 +0530 | [diff] [blame] | 162 | temp = readl(®s->rx_data); |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 163 | if (priv->skip_preamble) { |
Rajeshwari Shinde | 0c0faef | 2013-10-08 16:20:05 +0530 | [diff] [blame] | 164 | if (temp == SPI_PREAMBLE_END_BYTE) { |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 165 | priv->skip_preamble = 0; |
Rajeshwari Shinde | 0c0faef | 2013-10-08 16:20:05 +0530 | [diff] [blame] | 166 | stopping = 0; |
| 167 | } |
| 168 | } else { |
Rajeshwari Shinde | ffc7461 | 2013-10-08 16:20:06 +0530 | [diff] [blame] | 169 | if (rxp || stopping) { |
Akshay Saraswat | 8613bed | 2014-06-18 17:52:41 +0530 | [diff] [blame] | 170 | if (step == 4) |
| 171 | *(uint32_t *)rxp = temp; |
| 172 | else |
| 173 | *rxp = temp; |
Rajeshwari Shinde | ffc7461 | 2013-10-08 16:20:06 +0530 | [diff] [blame] | 174 | rxp += step; |
| 175 | } |
| 176 | in_bytes -= step; |
Rajeshwari Shinde | 813637c | 2013-05-28 20:10:38 +0000 | [diff] [blame] | 177 | } |
Rajeshwari Shinde | ffc7461 | 2013-10-08 16:20:06 +0530 | [diff] [blame] | 178 | toread -= step; |
| 179 | rx_lvl -= step; |
| 180 | } |
Rajeshwari Shinde | 813637c | 2013-05-28 20:10:38 +0000 | [diff] [blame] | 181 | } else if (!toread) { |
| 182 | /* |
| 183 | * We have run out of input data, but haven't read |
| 184 | * enough bytes after the preamble yet. Read some more, |
| 185 | * and make sure that we transmit dummy bytes too, to |
| 186 | * keep things going. |
| 187 | */ |
| 188 | assert(!out_bytes); |
| 189 | out_bytes = in_bytes; |
| 190 | toread = in_bytes; |
| 191 | txp = NULL; |
Rajeshwari Shinde | ffc7461 | 2013-10-08 16:20:06 +0530 | [diff] [blame] | 192 | spi_request_bytes(regs, toread, step); |
Rajeshwari Shinde | 813637c | 2013-05-28 20:10:38 +0000 | [diff] [blame] | 193 | } |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 194 | if (priv->skip_preamble && get_timer(start) > 100) { |
Simon Glass | 905ed0b | 2015-07-02 18:16:11 -0600 | [diff] [blame] | 195 | debug("SPI timeout: in_bytes=%d, out_bytes=%d, ", |
| 196 | in_bytes, out_bytes); |
| 197 | return -ETIMEDOUT; |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 198 | } |
| 199 | } |
Rajeshwari Shinde | 813637c | 2013-05-28 20:10:38 +0000 | [diff] [blame] | 200 | |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 201 | *dinp = rxp; |
| 202 | *doutp = txp; |
Rajeshwari Shinde | 813637c | 2013-05-28 20:10:38 +0000 | [diff] [blame] | 203 | |
| 204 | return 0; |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /** |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 208 | * Activate the CS by driving it LOW |
| 209 | * |
| 210 | * @param slave Pointer to spi_slave to which controller has to |
| 211 | * communicate with |
| 212 | */ |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 213 | static void spi_cs_activate(struct udevice *dev) |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 214 | { |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 215 | struct udevice *bus = dev->parent; |
| 216 | struct exynos_spi_platdata *pdata = dev_get_platdata(bus); |
| 217 | struct exynos_spi_priv *priv = dev_get_priv(bus); |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 218 | |
Rajeshwari Shinde | ab46adb | 2013-10-08 16:20:04 +0530 | [diff] [blame] | 219 | /* If it's too soon to do another transaction, wait */ |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 220 | if (pdata->deactivate_delay_us && |
| 221 | priv->last_transaction_us) { |
Rajeshwari Shinde | ab46adb | 2013-10-08 16:20:04 +0530 | [diff] [blame] | 222 | ulong delay_us; /* The delay completed so far */ |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 223 | delay_us = timer_get_us() - priv->last_transaction_us; |
| 224 | if (delay_us < pdata->deactivate_delay_us) |
| 225 | udelay(pdata->deactivate_delay_us - delay_us); |
Rajeshwari Shinde | ab46adb | 2013-10-08 16:20:04 +0530 | [diff] [blame] | 226 | } |
| 227 | |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 228 | clrbits_le32(&priv->regs->cs_reg, SPI_SLAVE_SIG_INACT); |
| 229 | debug("Activate CS, bus '%s'\n", bus->name); |
| 230 | priv->skip_preamble = priv->mode & SPI_PREAMBLE; |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Deactivate the CS by driving it HIGH |
| 235 | * |
| 236 | * @param slave Pointer to spi_slave to which controller has to |
| 237 | * communicate with |
| 238 | */ |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 239 | static void spi_cs_deactivate(struct udevice *dev) |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 240 | { |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 241 | struct udevice *bus = dev->parent; |
| 242 | struct exynos_spi_platdata *pdata = dev_get_platdata(bus); |
| 243 | struct exynos_spi_priv *priv = dev_get_priv(bus); |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 244 | |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 245 | setbits_le32(&priv->regs->cs_reg, SPI_SLAVE_SIG_INACT); |
Simon Glass | a193ed0 | 2014-07-07 10:16:38 -0600 | [diff] [blame] | 246 | |
| 247 | /* Remember time of this transaction so we can honour the bus delay */ |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 248 | if (pdata->deactivate_delay_us) |
| 249 | priv->last_transaction_us = timer_get_us(); |
Simon Glass | a193ed0 | 2014-07-07 10:16:38 -0600 | [diff] [blame] | 250 | |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 251 | debug("Deactivate CS, bus '%s'\n", bus->name); |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 254 | static int exynos_spi_ofdata_to_platdata(struct udevice *bus) |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 255 | { |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 256 | struct exynos_spi_platdata *plat = bus->platdata; |
| 257 | const void *blob = gd->fdt_blob; |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 258 | int node = dev_of_offset(bus); |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 259 | |
Tom Rini | 5a9ecb2 | 2020-07-24 08:42:06 -0400 | [diff] [blame] | 260 | plat->regs = (struct exynos_spi *)devfdt_get_addr(bus); |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 261 | plat->periph_id = pinmux_decode_periph_id(blob, node); |
Rajeshwari Shinde | 64ef8a3 | 2012-12-26 20:03:23 +0000 | [diff] [blame] | 262 | |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 263 | if (plat->periph_id == PERIPH_ID_NONE) { |
Rajeshwari Shinde | 64ef8a3 | 2012-12-26 20:03:23 +0000 | [diff] [blame] | 264 | debug("%s: Invalid peripheral ID %d\n", __func__, |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 265 | plat->periph_id); |
Rajeshwari Shinde | 64ef8a3 | 2012-12-26 20:03:23 +0000 | [diff] [blame] | 266 | return -FDT_ERR_NOTFOUND; |
| 267 | } |
| 268 | |
| 269 | /* Use 500KHz as a suitable default */ |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 270 | plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency", |
Rajeshwari Shinde | 64ef8a3 | 2012-12-26 20:03:23 +0000 | [diff] [blame] | 271 | 500000); |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 272 | plat->deactivate_delay_us = fdtdec_get_int(blob, node, |
Rajeshwari Shinde | ab46adb | 2013-10-08 16:20:04 +0530 | [diff] [blame] | 273 | "spi-deactivate-delay", 0); |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 274 | debug("%s: regs=%p, periph_id=%d, max-frequency=%d, deactivate_delay=%d\n", |
| 275 | __func__, plat->regs, plat->periph_id, plat->frequency, |
| 276 | plat->deactivate_delay_us); |
Rajeshwari Shinde | 64ef8a3 | 2012-12-26 20:03:23 +0000 | [diff] [blame] | 277 | |
| 278 | return 0; |
| 279 | } |
| 280 | |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 281 | static int exynos_spi_probe(struct udevice *bus) |
Rajeshwari Shinde | 64ef8a3 | 2012-12-26 20:03:23 +0000 | [diff] [blame] | 282 | { |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 283 | struct exynos_spi_platdata *plat = dev_get_platdata(bus); |
| 284 | struct exynos_spi_priv *priv = dev_get_priv(bus); |
Rajeshwari Shinde | 64ef8a3 | 2012-12-26 20:03:23 +0000 | [diff] [blame] | 285 | |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 286 | priv->regs = plat->regs; |
| 287 | if (plat->periph_id == PERIPH_ID_SPI1 || |
| 288 | plat->periph_id == PERIPH_ID_SPI2) |
| 289 | priv->fifo_size = 64; |
| 290 | else |
| 291 | priv->fifo_size = 256; |
Rajeshwari Shinde | 64ef8a3 | 2012-12-26 20:03:23 +0000 | [diff] [blame] | 292 | |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 293 | priv->skip_preamble = 0; |
| 294 | priv->last_transaction_us = timer_get_us(); |
| 295 | priv->freq = plat->frequency; |
| 296 | priv->periph_id = plat->periph_id; |
Rajeshwari Shinde | 64ef8a3 | 2012-12-26 20:03:23 +0000 | [diff] [blame] | 297 | |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 298 | return 0; |
| 299 | } |
Rajeshwari Shinde | 64ef8a3 | 2012-12-26 20:03:23 +0000 | [diff] [blame] | 300 | |
Simon Glass | 5c74fba | 2015-04-19 09:05:40 -0600 | [diff] [blame] | 301 | static int exynos_spi_claim_bus(struct udevice *dev) |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 302 | { |
Simon Glass | 5c74fba | 2015-04-19 09:05:40 -0600 | [diff] [blame] | 303 | struct udevice *bus = dev->parent; |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 304 | struct exynos_spi_priv *priv = dev_get_priv(bus); |
| 305 | |
| 306 | exynos_pinmux_config(priv->periph_id, PINMUX_FLAG_NONE); |
| 307 | spi_flush_fifo(priv->regs); |
| 308 | |
| 309 | writel(SPI_FB_DELAY_180, &priv->regs->fb_clk); |
Rajeshwari Shinde | 64ef8a3 | 2012-12-26 20:03:23 +0000 | [diff] [blame] | 310 | |
| 311 | return 0; |
| 312 | } |
| 313 | |
Simon Glass | 5c74fba | 2015-04-19 09:05:40 -0600 | [diff] [blame] | 314 | static int exynos_spi_release_bus(struct udevice *dev) |
Hung-ying Tyan | 0039123 | 2013-05-15 18:27:30 +0800 | [diff] [blame] | 315 | { |
Simon Glass | 5c74fba | 2015-04-19 09:05:40 -0600 | [diff] [blame] | 316 | struct udevice *bus = dev->parent; |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 317 | struct exynos_spi_priv *priv = dev_get_priv(bus); |
Hung-ying Tyan | 0039123 | 2013-05-15 18:27:30 +0800 | [diff] [blame] | 318 | |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 319 | spi_flush_fifo(priv->regs); |
| 320 | |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | static int exynos_spi_xfer(struct udevice *dev, unsigned int bitlen, |
| 325 | const void *dout, void *din, unsigned long flags) |
| 326 | { |
| 327 | struct udevice *bus = dev->parent; |
| 328 | struct exynos_spi_priv *priv = dev_get_priv(bus); |
| 329 | int upto, todo; |
| 330 | int bytelen; |
| 331 | int ret = 0; |
| 332 | |
| 333 | /* spi core configured to do 8 bit transfers */ |
| 334 | if (bitlen % 8) { |
| 335 | debug("Non byte aligned SPI transfer.\n"); |
| 336 | return -1; |
Hung-ying Tyan | 0039123 | 2013-05-15 18:27:30 +0800 | [diff] [blame] | 337 | } |
| 338 | |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 339 | /* Start the transaction, if necessary. */ |
| 340 | if ((flags & SPI_XFER_BEGIN)) |
| 341 | spi_cs_activate(dev); |
| 342 | |
| 343 | /* |
| 344 | * Exynos SPI limits each transfer to 65535 transfers. To keep |
| 345 | * things simple, allow a maximum of 65532 bytes. We could allow |
| 346 | * more in word mode, but the performance difference is small. |
| 347 | */ |
| 348 | bytelen = bitlen / 8; |
| 349 | for (upto = 0; !ret && upto < bytelen; upto += todo) { |
| 350 | todo = min(bytelen - upto, (1 << 16) - 4); |
| 351 | ret = spi_rx_tx(priv, todo, &din, &dout, flags); |
| 352 | if (ret) |
| 353 | break; |
| 354 | } |
| 355 | |
| 356 | /* Stop the transaction, if necessary. */ |
| 357 | if ((flags & SPI_XFER_END) && !(priv->mode & SPI_SLAVE)) { |
| 358 | spi_cs_deactivate(dev); |
| 359 | if (priv->skip_preamble) { |
| 360 | assert(!priv->skip_preamble); |
| 361 | debug("Failed to complete premable transaction\n"); |
| 362 | ret = -1; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | return ret; |
| 367 | } |
| 368 | |
| 369 | static int exynos_spi_set_speed(struct udevice *bus, uint speed) |
| 370 | { |
| 371 | struct exynos_spi_platdata *plat = bus->platdata; |
| 372 | struct exynos_spi_priv *priv = dev_get_priv(bus); |
| 373 | int ret; |
| 374 | |
| 375 | if (speed > plat->frequency) |
| 376 | speed = plat->frequency; |
| 377 | ret = set_spi_clk(priv->periph_id, speed); |
| 378 | if (ret) |
| 379 | return ret; |
| 380 | priv->freq = speed; |
| 381 | debug("%s: regs=%p, speed=%d\n", __func__, priv->regs, priv->freq); |
| 382 | |
| 383 | return 0; |
Hung-ying Tyan | 0039123 | 2013-05-15 18:27:30 +0800 | [diff] [blame] | 384 | } |
| 385 | |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 386 | static int exynos_spi_set_mode(struct udevice *bus, uint mode) |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 387 | { |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 388 | struct exynos_spi_priv *priv = dev_get_priv(bus); |
| 389 | uint32_t reg; |
Rajeshwari Shinde | 64ef8a3 | 2012-12-26 20:03:23 +0000 | [diff] [blame] | 390 | |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 391 | reg = readl(&priv->regs->ch_cfg); |
| 392 | reg &= ~(SPI_CH_CPHA_B | SPI_CH_CPOL_L); |
Rajeshwari Shinde | 64ef8a3 | 2012-12-26 20:03:23 +0000 | [diff] [blame] | 393 | |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 394 | if (mode & SPI_CPHA) |
| 395 | reg |= SPI_CH_CPHA_B; |
Rajeshwari Shinde | 64ef8a3 | 2012-12-26 20:03:23 +0000 | [diff] [blame] | 396 | |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 397 | if (mode & SPI_CPOL) |
| 398 | reg |= SPI_CH_CPOL_L; |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 399 | |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 400 | writel(reg, &priv->regs->ch_cfg); |
| 401 | priv->mode = mode; |
| 402 | debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode); |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 403 | |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 404 | return 0; |
Rajeshwari Shinde | ba3b893 | 2012-11-02 01:15:36 +0000 | [diff] [blame] | 405 | } |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 406 | |
| 407 | static const struct dm_spi_ops exynos_spi_ops = { |
| 408 | .claim_bus = exynos_spi_claim_bus, |
| 409 | .release_bus = exynos_spi_release_bus, |
| 410 | .xfer = exynos_spi_xfer, |
| 411 | .set_speed = exynos_spi_set_speed, |
| 412 | .set_mode = exynos_spi_set_mode, |
| 413 | /* |
| 414 | * cs_info is not needed, since we require all chip selects to be |
| 415 | * in the device tree explicitly |
| 416 | */ |
| 417 | }; |
| 418 | |
| 419 | static const struct udevice_id exynos_spi_ids[] = { |
| 420 | { .compatible = "samsung,exynos-spi" }, |
| 421 | { } |
| 422 | }; |
| 423 | |
| 424 | U_BOOT_DRIVER(exynos_spi) = { |
| 425 | .name = "exynos_spi", |
| 426 | .id = UCLASS_SPI, |
| 427 | .of_match = exynos_spi_ids, |
| 428 | .ops = &exynos_spi_ops, |
| 429 | .ofdata_to_platdata = exynos_spi_ofdata_to_platdata, |
| 430 | .platdata_auto_alloc_size = sizeof(struct exynos_spi_platdata), |
| 431 | .priv_auto_alloc_size = sizeof(struct exynos_spi_priv), |
Simon Glass | ca7eafe | 2014-10-13 23:42:01 -0600 | [diff] [blame] | 432 | .probe = exynos_spi_probe, |
| 433 | }; |