Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 2 | /* |
| 3 | * spi driver for rockchip |
| 4 | * |
Philipp Tomsich | 8aa6c92 | 2019-02-03 16:17:31 +0100 | [diff] [blame] | 5 | * (C) 2019 Theobroma Systems Design und Consulting GmbH |
| 6 | * |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 7 | * (C) Copyright 2015 Google, Inc |
| 8 | * |
| 9 | * (C) Copyright 2008-2013 Rockchip Electronics |
| 10 | * Peter, Software Engineering, <superpeter.cai@gmail.com>. |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <common.h> |
| 14 | #include <clk.h> |
| 15 | #include <dm.h> |
Simon Glass | 6e019c4 | 2016-11-13 14:22:02 -0700 | [diff] [blame] | 16 | #include <dt-structs.h> |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 17 | #include <errno.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 18 | #include <log.h> |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 19 | #include <spi.h> |
Simon Glass | 1045315 | 2019-11-14 12:57:30 -0700 | [diff] [blame] | 20 | #include <time.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 21 | #include <linux/delay.h> |
Masahiro Yamada | 1221ce4 | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 22 | #include <linux/errno.h> |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 23 | #include <asm/io.h> |
Kever Yang | 15f09a1 | 2019-03-28 11:01:23 +0800 | [diff] [blame] | 24 | #include <asm/arch-rockchip/clock.h> |
| 25 | #include <asm/arch-rockchip/periph.h> |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 26 | #include <dm/pinctrl.h> |
| 27 | #include "rk_spi.h" |
| 28 | |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 29 | /* Change to 1 to output registers at the start of each transaction */ |
| 30 | #define DEBUG_RK_SPI 0 |
| 31 | |
Jagan Teki | dbbdc81 | 2019-12-21 13:24:30 +0530 | [diff] [blame] | 32 | /* |
| 33 | * ctrlr1 is 16-bits, so we should support lengths of 0xffff + 1. However, |
| 34 | * the controller seems to hang when given 0x10000, so stick with this for now. |
| 35 | */ |
| 36 | #define ROCKCHIP_SPI_MAX_TRANLEN 0xffff |
| 37 | |
Philipp Tomsich | 51a644a | 2019-02-03 16:17:32 +0100 | [diff] [blame] | 38 | struct rockchip_spi_params { |
| 39 | /* RXFIFO overruns and TXFIFO underruns stop the master clock */ |
| 40 | bool master_manages_fifo; |
| 41 | }; |
| 42 | |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 43 | struct rockchip_spi_platdata { |
Simon Glass | 6e019c4 | 2016-11-13 14:22:02 -0700 | [diff] [blame] | 44 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 45 | struct dtd_rockchip_rk3288_spi of_plat; |
| 46 | #endif |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 47 | s32 frequency; /* Default clock frequency, -1 for none */ |
| 48 | fdt_addr_t base; |
| 49 | uint deactivate_delay_us; /* Delay to wait after deactivate */ |
Simon Glass | 183a3a0 | 2016-01-21 19:44:10 -0700 | [diff] [blame] | 50 | uint activate_delay_us; /* Delay to wait after activate */ |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | struct rockchip_spi_priv { |
| 54 | struct rockchip_spi *regs; |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 55 | struct clk clk; |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 56 | unsigned int max_freq; |
| 57 | unsigned int mode; |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 58 | ulong last_transaction_us; /* Time of last transaction end */ |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 59 | unsigned int speed_hz; |
Simon Glass | 28a943c | 2016-01-21 19:44:03 -0700 | [diff] [blame] | 60 | unsigned int last_speed_hz; |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 61 | uint input_rate; |
| 62 | }; |
| 63 | |
| 64 | #define SPI_FIFO_DEPTH 32 |
| 65 | |
| 66 | static void rkspi_dump_regs(struct rockchip_spi *regs) |
| 67 | { |
| 68 | debug("ctrl0: \t\t0x%08x\n", readl(®s->ctrlr0)); |
| 69 | debug("ctrl1: \t\t0x%08x\n", readl(®s->ctrlr1)); |
| 70 | debug("ssienr: \t\t0x%08x\n", readl(®s->enr)); |
| 71 | debug("ser: \t\t0x%08x\n", readl(®s->ser)); |
| 72 | debug("baudr: \t\t0x%08x\n", readl(®s->baudr)); |
| 73 | debug("txftlr: \t\t0x%08x\n", readl(®s->txftlr)); |
| 74 | debug("rxftlr: \t\t0x%08x\n", readl(®s->rxftlr)); |
| 75 | debug("txflr: \t\t0x%08x\n", readl(®s->txflr)); |
| 76 | debug("rxflr: \t\t0x%08x\n", readl(®s->rxflr)); |
| 77 | debug("sr: \t\t0x%08x\n", readl(®s->sr)); |
| 78 | debug("imr: \t\t0x%08x\n", readl(®s->imr)); |
| 79 | debug("isr: \t\t0x%08x\n", readl(®s->isr)); |
| 80 | debug("dmacr: \t\t0x%08x\n", readl(®s->dmacr)); |
| 81 | debug("dmatdlr: \t0x%08x\n", readl(®s->dmatdlr)); |
| 82 | debug("dmardlr: \t0x%08x\n", readl(®s->dmardlr)); |
| 83 | } |
| 84 | |
| 85 | static void rkspi_enable_chip(struct rockchip_spi *regs, bool enable) |
| 86 | { |
| 87 | writel(enable ? 1 : 0, ®s->enr); |
| 88 | } |
| 89 | |
| 90 | static void rkspi_set_clk(struct rockchip_spi_priv *priv, uint speed) |
| 91 | { |
Philipp Tomsich | 9fc354e | 2017-04-20 22:05:52 +0200 | [diff] [blame] | 92 | /* |
| 93 | * We should try not to exceed the speed requested by the caller: |
| 94 | * when selecting a divider, we need to make sure we round up. |
| 95 | */ |
| 96 | uint clk_div = DIV_ROUND_UP(priv->input_rate, speed); |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 97 | |
Philipp Tomsich | 9fc354e | 2017-04-20 22:05:52 +0200 | [diff] [blame] | 98 | /* The baudrate register (BAUDR) is defined as a 32bit register where |
| 99 | * the upper 16bit are reserved and having 'Fsclk_out' in the lower |
| 100 | * 16bits with 'Fsclk_out' defined as follows: |
| 101 | * |
| 102 | * Fsclk_out = Fspi_clk/ SCKDV |
| 103 | * Where SCKDV is any even value between 2 and 65534. |
| 104 | */ |
| 105 | if (clk_div > 0xfffe) { |
| 106 | clk_div = 0xfffe; |
Heinrich Schuchardt | 11f12c1 | 2017-11-12 20:59:44 +0100 | [diff] [blame] | 107 | debug("%s: can't divide down to %d Hz (actual will be %d Hz)\n", |
Philipp Tomsich | 9fc354e | 2017-04-20 22:05:52 +0200 | [diff] [blame] | 108 | __func__, speed, priv->input_rate / clk_div); |
| 109 | } |
| 110 | |
| 111 | /* Round up to the next even 16bit number */ |
| 112 | clk_div = (clk_div + 1) & 0xfffe; |
| 113 | |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 114 | debug("spi speed %u, div %u\n", speed, clk_div); |
| 115 | |
Philipp Tomsich | 9fc354e | 2017-04-20 22:05:52 +0200 | [diff] [blame] | 116 | clrsetbits_le32(&priv->regs->baudr, 0xffff, clk_div); |
Simon Glass | 28a943c | 2016-01-21 19:44:03 -0700 | [diff] [blame] | 117 | priv->last_speed_hz = speed; |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static int rkspi_wait_till_not_busy(struct rockchip_spi *regs) |
| 121 | { |
| 122 | unsigned long start; |
| 123 | |
| 124 | start = get_timer(0); |
| 125 | while (readl(®s->sr) & SR_BUSY) { |
| 126 | if (get_timer(start) > ROCKCHIP_SPI_TIMEOUT_MS) { |
| 127 | debug("RK SPI: Status keeps busy for 1000us after a read/write!\n"); |
| 128 | return -ETIMEDOUT; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
Simon Glass | 183a3a0 | 2016-01-21 19:44:10 -0700 | [diff] [blame] | 135 | static void spi_cs_activate(struct udevice *dev, uint cs) |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 136 | { |
Simon Glass | 183a3a0 | 2016-01-21 19:44:10 -0700 | [diff] [blame] | 137 | struct udevice *bus = dev->parent; |
| 138 | struct rockchip_spi_platdata *plat = bus->platdata; |
| 139 | struct rockchip_spi_priv *priv = dev_get_priv(bus); |
| 140 | struct rockchip_spi *regs = priv->regs; |
| 141 | |
Simon Glass | b425247 | 2016-11-13 14:22:03 -0700 | [diff] [blame] | 142 | /* If it's too soon to do another transaction, wait */ |
| 143 | if (plat->deactivate_delay_us && priv->last_transaction_us) { |
| 144 | ulong delay_us; /* The delay completed so far */ |
| 145 | delay_us = timer_get_us() - priv->last_transaction_us; |
Philipp Tomsich | f92cf0a | 2019-02-03 16:17:26 +0100 | [diff] [blame] | 146 | if (delay_us < plat->deactivate_delay_us) { |
| 147 | ulong additional_delay_us = |
| 148 | plat->deactivate_delay_us - delay_us; |
| 149 | debug("%s: delaying by %ld us\n", |
| 150 | __func__, additional_delay_us); |
| 151 | udelay(additional_delay_us); |
| 152 | } |
Simon Glass | b425247 | 2016-11-13 14:22:03 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 155 | debug("activate cs%u\n", cs); |
| 156 | writel(1 << cs, ®s->ser); |
Simon Glass | 183a3a0 | 2016-01-21 19:44:10 -0700 | [diff] [blame] | 157 | if (plat->activate_delay_us) |
| 158 | udelay(plat->activate_delay_us); |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 159 | } |
| 160 | |
Simon Glass | 183a3a0 | 2016-01-21 19:44:10 -0700 | [diff] [blame] | 161 | static void spi_cs_deactivate(struct udevice *dev, uint cs) |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 162 | { |
Simon Glass | 183a3a0 | 2016-01-21 19:44:10 -0700 | [diff] [blame] | 163 | struct udevice *bus = dev->parent; |
| 164 | struct rockchip_spi_platdata *plat = bus->platdata; |
| 165 | struct rockchip_spi_priv *priv = dev_get_priv(bus); |
| 166 | struct rockchip_spi *regs = priv->regs; |
| 167 | |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 168 | debug("deactivate cs%u\n", cs); |
| 169 | writel(0, ®s->ser); |
Simon Glass | 183a3a0 | 2016-01-21 19:44:10 -0700 | [diff] [blame] | 170 | |
| 171 | /* Remember time of this transaction so we can honour the bus delay */ |
| 172 | if (plat->deactivate_delay_us) |
| 173 | priv->last_transaction_us = timer_get_us(); |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 174 | } |
| 175 | |
Simon Glass | 6e019c4 | 2016-11-13 14:22:02 -0700 | [diff] [blame] | 176 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 177 | static int conv_of_platdata(struct udevice *dev) |
| 178 | { |
| 179 | struct rockchip_spi_platdata *plat = dev->platdata; |
| 180 | struct dtd_rockchip_rk3288_spi *dtplat = &plat->of_plat; |
| 181 | struct rockchip_spi_priv *priv = dev_get_priv(dev); |
| 182 | int ret; |
| 183 | |
| 184 | plat->base = dtplat->reg[0]; |
| 185 | plat->frequency = 20000000; |
Walter Lozano | 51f1263 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 186 | ret = clk_get_by_driver_info(dev, dtplat->clocks, &priv->clk); |
Simon Glass | 6e019c4 | 2016-11-13 14:22:02 -0700 | [diff] [blame] | 187 | if (ret < 0) |
| 188 | return ret; |
| 189 | dev->req_seq = 0; |
| 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | #endif |
| 194 | |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 195 | static int rockchip_spi_ofdata_to_platdata(struct udevice *bus) |
| 196 | { |
Simon Glass | 6e019c4 | 2016-11-13 14:22:02 -0700 | [diff] [blame] | 197 | #if !CONFIG_IS_ENABLED(OF_PLATDATA) |
| 198 | struct rockchip_spi_platdata *plat = dev_get_platdata(bus); |
Simon Glass | 71037d1 | 2016-01-21 19:43:43 -0700 | [diff] [blame] | 199 | struct rockchip_spi_priv *priv = dev_get_priv(bus); |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 200 | int ret; |
| 201 | |
Philipp Tomsich | d27c273 | 2017-09-11 22:04:20 +0200 | [diff] [blame] | 202 | plat->base = dev_read_addr(bus); |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 203 | |
Simon Glass | 71037d1 | 2016-01-21 19:43:43 -0700 | [diff] [blame] | 204 | ret = clk_get_by_index(bus, 0, &priv->clk); |
| 205 | if (ret < 0) { |
| 206 | debug("%s: Could not get clock for %s: %d\n", __func__, |
| 207 | bus->name, ret); |
| 208 | return ret; |
| 209 | } |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 210 | |
Philipp Tomsich | 6c65577c | 2017-06-07 18:45:58 +0200 | [diff] [blame] | 211 | plat->frequency = |
| 212 | dev_read_u32_default(bus, "spi-max-frequency", 50000000); |
| 213 | plat->deactivate_delay_us = |
| 214 | dev_read_u32_default(bus, "spi-deactivate-delay", 0); |
| 215 | plat->activate_delay_us = |
| 216 | dev_read_u32_default(bus, "spi-activate-delay", 0); |
| 217 | |
Simon Glass | 90a2847 | 2016-01-21 19:44:12 -0700 | [diff] [blame] | 218 | debug("%s: base=%x, max-frequency=%d, deactivate_delay=%d\n", |
| 219 | __func__, (uint)plat->base, plat->frequency, |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 220 | plat->deactivate_delay_us); |
Simon Glass | 6e019c4 | 2016-11-13 14:22:02 -0700 | [diff] [blame] | 221 | #endif |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
Philipp Tomsich | bd37671 | 2017-04-20 22:05:51 +0200 | [diff] [blame] | 226 | static int rockchip_spi_calc_modclk(ulong max_freq) |
| 227 | { |
Philipp Tomsich | d16120a | 2017-07-25 16:25:30 +0200 | [diff] [blame] | 228 | /* |
| 229 | * While this is not strictly correct for the RK3368, as the |
| 230 | * GPLL will be 576MHz, things will still work, as the |
| 231 | * clk_set_rate(...) implementation in our clock-driver will |
| 232 | * chose the next closest rate not exceeding what we request |
| 233 | * based on the output of this function. |
| 234 | */ |
| 235 | |
Philipp Tomsich | bd37671 | 2017-04-20 22:05:51 +0200 | [diff] [blame] | 236 | unsigned div; |
| 237 | const unsigned long gpll_hz = 594000000UL; |
| 238 | |
| 239 | /* |
| 240 | * We need to find an input clock that provides at least twice |
| 241 | * the maximum frequency and can be generated from the assumed |
| 242 | * speed of GPLL (594MHz) using an integer divider. |
| 243 | * |
| 244 | * To give us more achievable bitrates at higher speeds (these |
| 245 | * are generated by dividing by an even 16-bit integer from |
| 246 | * this frequency), we try to have an input frequency of at |
| 247 | * least 4x our max_freq. |
| 248 | */ |
| 249 | |
| 250 | div = DIV_ROUND_UP(gpll_hz, max_freq * 4); |
| 251 | return gpll_hz / div; |
| 252 | } |
| 253 | |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 254 | static int rockchip_spi_probe(struct udevice *bus) |
| 255 | { |
| 256 | struct rockchip_spi_platdata *plat = dev_get_platdata(bus); |
| 257 | struct rockchip_spi_priv *priv = dev_get_priv(bus); |
| 258 | int ret; |
| 259 | |
| 260 | debug("%s: probe\n", __func__); |
Simon Glass | 6e019c4 | 2016-11-13 14:22:02 -0700 | [diff] [blame] | 261 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 262 | ret = conv_of_platdata(bus); |
| 263 | if (ret) |
| 264 | return ret; |
| 265 | #endif |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 266 | priv->regs = (struct rockchip_spi *)plat->base; |
| 267 | |
| 268 | priv->last_transaction_us = timer_get_us(); |
| 269 | priv->max_freq = plat->frequency; |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 270 | |
Philipp Tomsich | bd37671 | 2017-04-20 22:05:51 +0200 | [diff] [blame] | 271 | /* Clamp the value from the DTS against any hardware limits */ |
| 272 | if (priv->max_freq > ROCKCHIP_SPI_MAX_RATE) |
| 273 | priv->max_freq = ROCKCHIP_SPI_MAX_RATE; |
| 274 | |
| 275 | /* Find a module-input clock that fits with the max_freq setting */ |
| 276 | ret = clk_set_rate(&priv->clk, |
| 277 | rockchip_spi_calc_modclk(priv->max_freq)); |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 278 | if (ret < 0) { |
| 279 | debug("%s: Failed to set clock: %d\n", __func__, ret); |
| 280 | return ret; |
| 281 | } |
| 282 | priv->input_rate = ret; |
| 283 | debug("%s: rate = %u\n", __func__, priv->input_rate); |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 284 | |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | static int rockchip_spi_claim_bus(struct udevice *dev) |
| 289 | { |
| 290 | struct udevice *bus = dev->parent; |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 291 | struct rockchip_spi_priv *priv = dev_get_priv(bus); |
| 292 | struct rockchip_spi *regs = priv->regs; |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 293 | uint ctrlr0; |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 294 | |
| 295 | /* Disable the SPI hardware */ |
Philipp Tomsich | b6101e9 | 2019-02-03 16:17:29 +0100 | [diff] [blame] | 296 | rkspi_enable_chip(regs, false); |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 297 | |
Simon Glass | 28a943c | 2016-01-21 19:44:03 -0700 | [diff] [blame] | 298 | if (priv->speed_hz != priv->last_speed_hz) |
| 299 | rkspi_set_clk(priv, priv->speed_hz); |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 300 | |
| 301 | /* Operation Mode */ |
| 302 | ctrlr0 = OMOD_MASTER << OMOD_SHIFT; |
| 303 | |
| 304 | /* Data Frame Size */ |
Philipp Tomsich | 0e661b6 | 2019-02-03 16:17:27 +0100 | [diff] [blame] | 305 | ctrlr0 |= DFS_8BIT << DFS_SHIFT; |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 306 | |
| 307 | /* set SPI mode 0..3 */ |
| 308 | if (priv->mode & SPI_CPOL) |
| 309 | ctrlr0 |= SCOL_HIGH << SCOL_SHIFT; |
| 310 | if (priv->mode & SPI_CPHA) |
| 311 | ctrlr0 |= SCPH_TOGSTA << SCPH_SHIFT; |
| 312 | |
| 313 | /* Chip Select Mode */ |
| 314 | ctrlr0 |= CSM_KEEP << CSM_SHIFT; |
| 315 | |
| 316 | /* SSN to Sclk_out delay */ |
| 317 | ctrlr0 |= SSN_DELAY_ONE << SSN_DELAY_SHIFT; |
| 318 | |
| 319 | /* Serial Endian Mode */ |
| 320 | ctrlr0 |= SEM_LITTLE << SEM_SHIFT; |
| 321 | |
| 322 | /* First Bit Mode */ |
| 323 | ctrlr0 |= FBM_MSB << FBM_SHIFT; |
| 324 | |
| 325 | /* Byte and Halfword Transform */ |
Philipp Tomsich | 0e661b6 | 2019-02-03 16:17:27 +0100 | [diff] [blame] | 326 | ctrlr0 |= HALF_WORD_OFF << HALF_WORD_TX_SHIFT; |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 327 | |
| 328 | /* Rxd Sample Delay */ |
| 329 | ctrlr0 |= 0 << RXDSD_SHIFT; |
| 330 | |
| 331 | /* Frame Format */ |
| 332 | ctrlr0 |= FRF_SPI << FRF_SHIFT; |
| 333 | |
| 334 | /* Tx and Rx mode */ |
Philipp Tomsich | 0e661b6 | 2019-02-03 16:17:27 +0100 | [diff] [blame] | 335 | ctrlr0 |= TMOD_TR << TMOD_SHIFT; |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 336 | |
| 337 | writel(ctrlr0, ®s->ctrlr0); |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 338 | |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | static int rockchip_spi_release_bus(struct udevice *dev) |
| 343 | { |
Simon Glass | e15af8e | 2016-01-21 19:44:11 -0700 | [diff] [blame] | 344 | struct udevice *bus = dev->parent; |
| 345 | struct rockchip_spi_priv *priv = dev_get_priv(bus); |
| 346 | |
| 347 | rkspi_enable_chip(priv->regs, false); |
| 348 | |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 349 | return 0; |
| 350 | } |
| 351 | |
Philipp Tomsich | 8aa6c92 | 2019-02-03 16:17:31 +0100 | [diff] [blame] | 352 | static inline int rockchip_spi_16bit_reader(struct udevice *dev, |
| 353 | u8 **din, int *len) |
| 354 | { |
| 355 | struct udevice *bus = dev->parent; |
| 356 | const struct rockchip_spi_params * const data = |
| 357 | (void *)dev_get_driver_data(bus); |
| 358 | struct rockchip_spi_priv *priv = dev_get_priv(bus); |
| 359 | struct rockchip_spi *regs = priv->regs; |
| 360 | const u32 saved_ctrlr0 = readl(®s->ctrlr0); |
| 361 | #if defined(DEBUG) |
| 362 | u32 statistics_rxlevels[33] = { }; |
| 363 | #endif |
| 364 | u32 frames = *len / 2; |
Philipp Tomsich | 2236149 | 2019-02-03 16:17:33 +0100 | [diff] [blame] | 365 | u8 *in = (u8 *)(*din); |
Philipp Tomsich | 8aa6c92 | 2019-02-03 16:17:31 +0100 | [diff] [blame] | 366 | u32 max_chunk_size = SPI_FIFO_DEPTH; |
| 367 | |
| 368 | if (!frames) |
| 369 | return 0; |
| 370 | |
| 371 | /* |
Philipp Tomsich | 51a644a | 2019-02-03 16:17:32 +0100 | [diff] [blame] | 372 | * If we know that the hardware will manage RXFIFO overruns |
| 373 | * (i.e. stop the SPI clock until there's space in the FIFO), |
| 374 | * we the allow largest possible chunk size that can be |
| 375 | * represented in CTRLR1. |
| 376 | */ |
| 377 | if (data && data->master_manages_fifo) |
Jagan Teki | dbbdc81 | 2019-12-21 13:24:30 +0530 | [diff] [blame] | 378 | max_chunk_size = ROCKCHIP_SPI_MAX_TRANLEN; |
Philipp Tomsich | 51a644a | 2019-02-03 16:17:32 +0100 | [diff] [blame] | 379 | |
Philipp Tomsich | 8aa6c92 | 2019-02-03 16:17:31 +0100 | [diff] [blame] | 380 | // rockchip_spi_configure(dev, mode, size) |
| 381 | rkspi_enable_chip(regs, false); |
| 382 | clrsetbits_le32(®s->ctrlr0, |
| 383 | TMOD_MASK << TMOD_SHIFT, |
| 384 | TMOD_RO << TMOD_SHIFT); |
| 385 | /* 16bit data frame size */ |
| 386 | clrsetbits_le32(®s->ctrlr0, DFS_MASK, DFS_16BIT); |
| 387 | |
| 388 | /* Update caller's context */ |
| 389 | const u32 bytes_to_process = 2 * frames; |
| 390 | *din += bytes_to_process; |
| 391 | *len -= bytes_to_process; |
| 392 | |
| 393 | /* Process our frames */ |
| 394 | while (frames) { |
| 395 | u32 chunk_size = min(frames, max_chunk_size); |
| 396 | |
| 397 | frames -= chunk_size; |
| 398 | |
| 399 | writew(chunk_size - 1, ®s->ctrlr1); |
| 400 | rkspi_enable_chip(regs, true); |
| 401 | |
| 402 | do { |
| 403 | u32 rx_level = readw(®s->rxflr); |
| 404 | #if defined(DEBUG) |
| 405 | statistics_rxlevels[rx_level]++; |
| 406 | #endif |
| 407 | chunk_size -= rx_level; |
Philipp Tomsich | 2236149 | 2019-02-03 16:17:33 +0100 | [diff] [blame] | 408 | while (rx_level--) { |
| 409 | u16 val = readw(regs->rxdr); |
| 410 | *in++ = val & 0xff; |
| 411 | *in++ = val >> 8; |
| 412 | } |
Philipp Tomsich | 8aa6c92 | 2019-02-03 16:17:31 +0100 | [diff] [blame] | 413 | } while (chunk_size); |
| 414 | |
| 415 | rkspi_enable_chip(regs, false); |
| 416 | } |
| 417 | |
| 418 | #if defined(DEBUG) |
| 419 | debug("%s: observed rx_level during processing:\n", __func__); |
| 420 | for (int i = 0; i <= 32; ++i) |
| 421 | if (statistics_rxlevels[i]) |
| 422 | debug("\t%2d: %d\n", i, statistics_rxlevels[i]); |
| 423 | #endif |
| 424 | /* Restore the original transfer setup and return error-free. */ |
| 425 | writel(saved_ctrlr0, ®s->ctrlr0); |
| 426 | return 0; |
| 427 | } |
| 428 | |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 429 | static int rockchip_spi_xfer(struct udevice *dev, unsigned int bitlen, |
| 430 | const void *dout, void *din, unsigned long flags) |
| 431 | { |
| 432 | struct udevice *bus = dev->parent; |
| 433 | struct rockchip_spi_priv *priv = dev_get_priv(bus); |
| 434 | struct rockchip_spi *regs = priv->regs; |
| 435 | struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev); |
| 436 | int len = bitlen >> 3; |
| 437 | const u8 *out = dout; |
| 438 | u8 *in = din; |
| 439 | int toread, towrite; |
Philipp Tomsich | 8aa6c92 | 2019-02-03 16:17:31 +0100 | [diff] [blame] | 440 | int ret = 0; |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 441 | |
| 442 | debug("%s: dout=%p, din=%p, len=%x, flags=%lx\n", __func__, dout, din, |
| 443 | len, flags); |
| 444 | if (DEBUG_RK_SPI) |
| 445 | rkspi_dump_regs(regs); |
| 446 | |
| 447 | /* Assert CS before transfer */ |
| 448 | if (flags & SPI_XFER_BEGIN) |
Simon Glass | 183a3a0 | 2016-01-21 19:44:10 -0700 | [diff] [blame] | 449 | spi_cs_activate(dev, slave_plat->cs); |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 450 | |
Philipp Tomsich | 8aa6c92 | 2019-02-03 16:17:31 +0100 | [diff] [blame] | 451 | /* |
| 452 | * To ensure fast loading of firmware images (e.g. full U-Boot |
| 453 | * stage, ATF, Linux kernel) from SPI flash, we optimise the |
| 454 | * case of read-only transfers by using the full 16bits of each |
| 455 | * FIFO element. |
| 456 | */ |
| 457 | if (!out) |
| 458 | ret = rockchip_spi_16bit_reader(dev, &in, &len); |
| 459 | |
| 460 | /* This is the original 8bit reader/writer code */ |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 461 | while (len > 0) { |
Jagan Teki | dbbdc81 | 2019-12-21 13:24:30 +0530 | [diff] [blame] | 462 | int todo = min(len, ROCKCHIP_SPI_MAX_TRANLEN); |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 463 | |
Simon Glass | e15af8e | 2016-01-21 19:44:11 -0700 | [diff] [blame] | 464 | rkspi_enable_chip(regs, false); |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 465 | writel(todo - 1, ®s->ctrlr1); |
| 466 | rkspi_enable_chip(regs, true); |
| 467 | |
| 468 | toread = todo; |
| 469 | towrite = todo; |
| 470 | while (toread || towrite) { |
| 471 | u32 status = readl(®s->sr); |
| 472 | |
| 473 | if (towrite && !(status & SR_TF_FULL)) { |
| 474 | writel(out ? *out++ : 0, regs->txdr); |
| 475 | towrite--; |
| 476 | } |
| 477 | if (toread && !(status & SR_RF_EMPT)) { |
| 478 | u32 byte = readl(regs->rxdr); |
| 479 | |
| 480 | if (in) |
| 481 | *in++ = byte; |
| 482 | toread--; |
| 483 | } |
| 484 | } |
Philipp Tomsich | 7e0e5c5 | 2019-02-03 16:17:30 +0100 | [diff] [blame] | 485 | |
| 486 | /* |
| 487 | * In case that there's a transmit-component, we need to wait |
| 488 | * until the control goes idle before we can disable the SPI |
| 489 | * control logic (as this will implictly flush the FIFOs). |
| 490 | */ |
| 491 | if (out) { |
| 492 | ret = rkspi_wait_till_not_busy(regs); |
| 493 | if (ret) |
| 494 | break; |
| 495 | } |
| 496 | |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 497 | len -= todo; |
| 498 | } |
| 499 | |
| 500 | /* Deassert CS after transfer */ |
| 501 | if (flags & SPI_XFER_END) |
Simon Glass | 183a3a0 | 2016-01-21 19:44:10 -0700 | [diff] [blame] | 502 | spi_cs_deactivate(dev, slave_plat->cs); |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 503 | |
| 504 | rkspi_enable_chip(regs, false); |
| 505 | |
| 506 | return ret; |
| 507 | } |
| 508 | |
| 509 | static int rockchip_spi_set_speed(struct udevice *bus, uint speed) |
| 510 | { |
| 511 | struct rockchip_spi_priv *priv = dev_get_priv(bus); |
| 512 | |
Philipp Tomsich | bd37671 | 2017-04-20 22:05:51 +0200 | [diff] [blame] | 513 | /* Clamp to the maximum frequency specified in the DTS */ |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 514 | if (speed > priv->max_freq) |
| 515 | speed = priv->max_freq; |
Philipp Tomsich | bd37671 | 2017-04-20 22:05:51 +0200 | [diff] [blame] | 516 | |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 517 | priv->speed_hz = speed; |
| 518 | |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | static int rockchip_spi_set_mode(struct udevice *bus, uint mode) |
| 523 | { |
| 524 | struct rockchip_spi_priv *priv = dev_get_priv(bus); |
| 525 | |
| 526 | priv->mode = mode; |
| 527 | |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | static const struct dm_spi_ops rockchip_spi_ops = { |
| 532 | .claim_bus = rockchip_spi_claim_bus, |
| 533 | .release_bus = rockchip_spi_release_bus, |
| 534 | .xfer = rockchip_spi_xfer, |
| 535 | .set_speed = rockchip_spi_set_speed, |
| 536 | .set_mode = rockchip_spi_set_mode, |
| 537 | /* |
| 538 | * cs_info is not needed, since we require all chip selects to be |
| 539 | * in the device tree explicitly |
| 540 | */ |
| 541 | }; |
| 542 | |
Philipp Tomsich | 51a644a | 2019-02-03 16:17:32 +0100 | [diff] [blame] | 543 | const struct rockchip_spi_params rk3399_spi_params = { |
| 544 | .master_manages_fifo = true, |
| 545 | }; |
| 546 | |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 547 | static const struct udevice_id rockchip_spi_ids[] = { |
Johannes Krottmayer | e93d2f4 | 2020-07-08 23:57:39 +0200 | [diff] [blame] | 548 | { .compatible = "rockchip,rk3066-spi" }, |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 549 | { .compatible = "rockchip,rk3288-spi" }, |
Johannes Krottmayer | e93d2f4 | 2020-07-08 23:57:39 +0200 | [diff] [blame] | 550 | { .compatible = "rockchip,rk3328-spi" }, |
Philipp Tomsich | 51a644a | 2019-02-03 16:17:32 +0100 | [diff] [blame] | 551 | { .compatible = "rockchip,rk3368-spi", |
| 552 | .data = (ulong)&rk3399_spi_params }, |
| 553 | { .compatible = "rockchip,rk3399-spi", |
| 554 | .data = (ulong)&rk3399_spi_params }, |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 555 | { } |
| 556 | }; |
| 557 | |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 558 | U_BOOT_DRIVER(rockchip_rk3288_spi) = { |
Simon Glass | 6e019c4 | 2016-11-13 14:22:02 -0700 | [diff] [blame] | 559 | .name = "rockchip_rk3288_spi", |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 560 | .id = UCLASS_SPI, |
| 561 | .of_match = rockchip_spi_ids, |
| 562 | .ops = &rockchip_spi_ops, |
| 563 | .ofdata_to_platdata = rockchip_spi_ofdata_to_platdata, |
| 564 | .platdata_auto_alloc_size = sizeof(struct rockchip_spi_platdata), |
| 565 | .priv_auto_alloc_size = sizeof(struct rockchip_spi_priv), |
| 566 | .probe = rockchip_spi_probe, |
| 567 | }; |
Walter Lozano | addf358 | 2020-06-25 01:10:06 -0300 | [diff] [blame] | 568 | |
| 569 | U_BOOT_DRIVER_ALIAS(rockchip_rk3288_spi, rockchip_rk3368_spi) |