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