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