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