Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 1 | /* |
| 2 | * spi driver for rockchip |
| 3 | * |
| 4 | * (C) Copyright 2015 Google, Inc |
| 5 | * |
| 6 | * (C) Copyright 2008-2013 Rockchip Electronics |
| 7 | * Peter, Software Engineering, <superpeter.cai@gmail.com>. |
| 8 | * |
| 9 | * SPDX-License-Identifier: GPL-2.0+ |
| 10 | */ |
| 11 | |
| 12 | #include <common.h> |
| 13 | #include <clk.h> |
| 14 | #include <dm.h> |
Simon Glass | 6e019c4 | 2016-11-13 14:22:02 -0700 | [diff] [blame] | 15 | #include <dt-structs.h> |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 16 | #include <errno.h> |
| 17 | #include <spi.h> |
Masahiro Yamada | 1221ce4 | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 18 | #include <linux/errno.h> |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 19 | #include <asm/io.h> |
| 20 | #include <asm/arch/clock.h> |
| 21 | #include <asm/arch/periph.h> |
| 22 | #include <dm/pinctrl.h> |
| 23 | #include "rk_spi.h" |
| 24 | |
| 25 | DECLARE_GLOBAL_DATA_PTR; |
| 26 | |
| 27 | /* Change to 1 to output registers at the start of each transaction */ |
| 28 | #define DEBUG_RK_SPI 0 |
| 29 | |
| 30 | struct rockchip_spi_platdata { |
Simon Glass | 6e019c4 | 2016-11-13 14:22:02 -0700 | [diff] [blame] | 31 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 32 | struct dtd_rockchip_rk3288_spi of_plat; |
| 33 | #endif |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 34 | s32 frequency; /* Default clock frequency, -1 for none */ |
| 35 | fdt_addr_t base; |
| 36 | uint deactivate_delay_us; /* Delay to wait after deactivate */ |
Simon Glass | 183a3a0 | 2016-01-21 19:44:10 -0700 | [diff] [blame] | 37 | uint activate_delay_us; /* Delay to wait after activate */ |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | struct rockchip_spi_priv { |
| 41 | struct rockchip_spi *regs; |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 42 | struct clk clk; |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 43 | unsigned int max_freq; |
| 44 | unsigned int mode; |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 45 | ulong last_transaction_us; /* Time of last transaction end */ |
| 46 | u8 bits_per_word; /* max 16 bits per word */ |
| 47 | u8 n_bytes; |
| 48 | unsigned int speed_hz; |
Simon Glass | 28a943c | 2016-01-21 19:44:03 -0700 | [diff] [blame] | 49 | unsigned int last_speed_hz; |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 50 | unsigned int tmode; |
| 51 | uint input_rate; |
| 52 | }; |
| 53 | |
| 54 | #define SPI_FIFO_DEPTH 32 |
| 55 | |
| 56 | static void rkspi_dump_regs(struct rockchip_spi *regs) |
| 57 | { |
| 58 | debug("ctrl0: \t\t0x%08x\n", readl(®s->ctrlr0)); |
| 59 | debug("ctrl1: \t\t0x%08x\n", readl(®s->ctrlr1)); |
| 60 | debug("ssienr: \t\t0x%08x\n", readl(®s->enr)); |
| 61 | debug("ser: \t\t0x%08x\n", readl(®s->ser)); |
| 62 | debug("baudr: \t\t0x%08x\n", readl(®s->baudr)); |
| 63 | debug("txftlr: \t\t0x%08x\n", readl(®s->txftlr)); |
| 64 | debug("rxftlr: \t\t0x%08x\n", readl(®s->rxftlr)); |
| 65 | debug("txflr: \t\t0x%08x\n", readl(®s->txflr)); |
| 66 | debug("rxflr: \t\t0x%08x\n", readl(®s->rxflr)); |
| 67 | debug("sr: \t\t0x%08x\n", readl(®s->sr)); |
| 68 | debug("imr: \t\t0x%08x\n", readl(®s->imr)); |
| 69 | debug("isr: \t\t0x%08x\n", readl(®s->isr)); |
| 70 | debug("dmacr: \t\t0x%08x\n", readl(®s->dmacr)); |
| 71 | debug("dmatdlr: \t0x%08x\n", readl(®s->dmatdlr)); |
| 72 | debug("dmardlr: \t0x%08x\n", readl(®s->dmardlr)); |
| 73 | } |
| 74 | |
| 75 | static void rkspi_enable_chip(struct rockchip_spi *regs, bool enable) |
| 76 | { |
| 77 | writel(enable ? 1 : 0, ®s->enr); |
| 78 | } |
| 79 | |
| 80 | static void rkspi_set_clk(struct rockchip_spi_priv *priv, uint speed) |
| 81 | { |
Philipp Tomsich | 9fc354e | 2017-04-20 22:05:52 +0200 | [diff] [blame] | 82 | /* |
| 83 | * We should try not to exceed the speed requested by the caller: |
| 84 | * when selecting a divider, we need to make sure we round up. |
| 85 | */ |
| 86 | uint clk_div = DIV_ROUND_UP(priv->input_rate, speed); |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 87 | |
Philipp Tomsich | 9fc354e | 2017-04-20 22:05:52 +0200 | [diff] [blame] | 88 | /* The baudrate register (BAUDR) is defined as a 32bit register where |
| 89 | * the upper 16bit are reserved and having 'Fsclk_out' in the lower |
| 90 | * 16bits with 'Fsclk_out' defined as follows: |
| 91 | * |
| 92 | * Fsclk_out = Fspi_clk/ SCKDV |
| 93 | * Where SCKDV is any even value between 2 and 65534. |
| 94 | */ |
| 95 | if (clk_div > 0xfffe) { |
| 96 | clk_div = 0xfffe; |
| 97 | debug("%s: can't divide down to %d hz (actual will be %d hz)\n", |
| 98 | __func__, speed, priv->input_rate / clk_div); |
| 99 | } |
| 100 | |
| 101 | /* Round up to the next even 16bit number */ |
| 102 | clk_div = (clk_div + 1) & 0xfffe; |
| 103 | |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 104 | debug("spi speed %u, div %u\n", speed, clk_div); |
| 105 | |
Philipp Tomsich | 9fc354e | 2017-04-20 22:05:52 +0200 | [diff] [blame] | 106 | clrsetbits_le32(&priv->regs->baudr, 0xffff, clk_div); |
Simon Glass | 28a943c | 2016-01-21 19:44:03 -0700 | [diff] [blame] | 107 | priv->last_speed_hz = speed; |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static int rkspi_wait_till_not_busy(struct rockchip_spi *regs) |
| 111 | { |
| 112 | unsigned long start; |
| 113 | |
| 114 | start = get_timer(0); |
| 115 | while (readl(®s->sr) & SR_BUSY) { |
| 116 | if (get_timer(start) > ROCKCHIP_SPI_TIMEOUT_MS) { |
| 117 | debug("RK SPI: Status keeps busy for 1000us after a read/write!\n"); |
| 118 | return -ETIMEDOUT; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
Simon Glass | 183a3a0 | 2016-01-21 19:44:10 -0700 | [diff] [blame] | 125 | static void spi_cs_activate(struct udevice *dev, uint cs) |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 126 | { |
Simon Glass | 183a3a0 | 2016-01-21 19:44:10 -0700 | [diff] [blame] | 127 | struct udevice *bus = dev->parent; |
| 128 | struct rockchip_spi_platdata *plat = bus->platdata; |
| 129 | struct rockchip_spi_priv *priv = dev_get_priv(bus); |
| 130 | struct rockchip_spi *regs = priv->regs; |
| 131 | |
Simon Glass | b425247 | 2016-11-13 14:22:03 -0700 | [diff] [blame] | 132 | /* If it's too soon to do another transaction, wait */ |
| 133 | if (plat->deactivate_delay_us && priv->last_transaction_us) { |
| 134 | ulong delay_us; /* The delay completed so far */ |
| 135 | delay_us = timer_get_us() - priv->last_transaction_us; |
| 136 | if (delay_us < plat->deactivate_delay_us) |
| 137 | udelay(plat->deactivate_delay_us - delay_us); |
| 138 | } |
| 139 | |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 140 | debug("activate cs%u\n", cs); |
| 141 | writel(1 << cs, ®s->ser); |
Simon Glass | 183a3a0 | 2016-01-21 19:44:10 -0700 | [diff] [blame] | 142 | if (plat->activate_delay_us) |
| 143 | udelay(plat->activate_delay_us); |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 144 | } |
| 145 | |
Simon Glass | 183a3a0 | 2016-01-21 19:44:10 -0700 | [diff] [blame] | 146 | static void spi_cs_deactivate(struct udevice *dev, uint cs) |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 147 | { |
Simon Glass | 183a3a0 | 2016-01-21 19:44:10 -0700 | [diff] [blame] | 148 | struct udevice *bus = dev->parent; |
| 149 | struct rockchip_spi_platdata *plat = bus->platdata; |
| 150 | struct rockchip_spi_priv *priv = dev_get_priv(bus); |
| 151 | struct rockchip_spi *regs = priv->regs; |
| 152 | |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 153 | debug("deactivate cs%u\n", cs); |
| 154 | writel(0, ®s->ser); |
Simon Glass | 183a3a0 | 2016-01-21 19:44:10 -0700 | [diff] [blame] | 155 | |
| 156 | /* Remember time of this transaction so we can honour the bus delay */ |
| 157 | if (plat->deactivate_delay_us) |
| 158 | priv->last_transaction_us = timer_get_us(); |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 159 | } |
| 160 | |
Simon Glass | 6e019c4 | 2016-11-13 14:22:02 -0700 | [diff] [blame] | 161 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 162 | static int conv_of_platdata(struct udevice *dev) |
| 163 | { |
| 164 | struct rockchip_spi_platdata *plat = dev->platdata; |
| 165 | struct dtd_rockchip_rk3288_spi *dtplat = &plat->of_plat; |
| 166 | struct rockchip_spi_priv *priv = dev_get_priv(dev); |
| 167 | int ret; |
| 168 | |
| 169 | plat->base = dtplat->reg[0]; |
| 170 | plat->frequency = 20000000; |
| 171 | ret = clk_get_by_index_platdata(dev, 0, dtplat->clocks, &priv->clk); |
| 172 | if (ret < 0) |
| 173 | return ret; |
| 174 | dev->req_seq = 0; |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | #endif |
| 179 | |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 180 | static int rockchip_spi_ofdata_to_platdata(struct udevice *bus) |
| 181 | { |
Simon Glass | 6e019c4 | 2016-11-13 14:22:02 -0700 | [diff] [blame] | 182 | #if !CONFIG_IS_ENABLED(OF_PLATDATA) |
| 183 | struct rockchip_spi_platdata *plat = dev_get_platdata(bus); |
Simon Glass | 71037d1 | 2016-01-21 19:43:43 -0700 | [diff] [blame] | 184 | struct rockchip_spi_priv *priv = dev_get_priv(bus); |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 185 | const void *blob = gd->fdt_blob; |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 186 | int node = dev_of_offset(bus); |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 187 | int ret; |
| 188 | |
Simon Glass | a821c4a | 2017-05-17 17:18:05 -0600 | [diff] [blame^] | 189 | plat->base = devfdt_get_addr(bus); |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 190 | |
Simon Glass | 71037d1 | 2016-01-21 19:43:43 -0700 | [diff] [blame] | 191 | ret = clk_get_by_index(bus, 0, &priv->clk); |
| 192 | if (ret < 0) { |
| 193 | debug("%s: Could not get clock for %s: %d\n", __func__, |
| 194 | bus->name, ret); |
| 195 | return ret; |
| 196 | } |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 197 | |
| 198 | plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency", |
Simon Glass | 71037d1 | 2016-01-21 19:43:43 -0700 | [diff] [blame] | 199 | 50000000); |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 200 | plat->deactivate_delay_us = fdtdec_get_int(blob, node, |
| 201 | "spi-deactivate-delay", 0); |
Simon Glass | 183a3a0 | 2016-01-21 19:44:10 -0700 | [diff] [blame] | 202 | plat->activate_delay_us = fdtdec_get_int(blob, node, |
| 203 | "spi-activate-delay", 0); |
Simon Glass | 90a2847 | 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 | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 206 | plat->deactivate_delay_us); |
Simon Glass | 6e019c4 | 2016-11-13 14:22:02 -0700 | [diff] [blame] | 207 | #endif |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
Philipp Tomsich | bd37671 | 2017-04-20 22:05:51 +0200 | [diff] [blame] | 212 | static int rockchip_spi_calc_modclk(ulong max_freq) |
| 213 | { |
| 214 | unsigned div; |
| 215 | const unsigned long gpll_hz = 594000000UL; |
| 216 | |
| 217 | /* |
| 218 | * We need to find an input clock that provides at least twice |
| 219 | * the maximum frequency and can be generated from the assumed |
| 220 | * speed of GPLL (594MHz) using an integer divider. |
| 221 | * |
| 222 | * To give us more achievable bitrates at higher speeds (these |
| 223 | * are generated by dividing by an even 16-bit integer from |
| 224 | * this frequency), we try to have an input frequency of at |
| 225 | * least 4x our max_freq. |
| 226 | */ |
| 227 | |
| 228 | div = DIV_ROUND_UP(gpll_hz, max_freq * 4); |
| 229 | return gpll_hz / div; |
| 230 | } |
| 231 | |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 232 | static int rockchip_spi_probe(struct udevice *bus) |
| 233 | { |
| 234 | struct rockchip_spi_platdata *plat = dev_get_platdata(bus); |
| 235 | struct rockchip_spi_priv *priv = dev_get_priv(bus); |
| 236 | int ret; |
| 237 | |
| 238 | debug("%s: probe\n", __func__); |
Simon Glass | 6e019c4 | 2016-11-13 14:22:02 -0700 | [diff] [blame] | 239 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 240 | ret = conv_of_platdata(bus); |
| 241 | if (ret) |
| 242 | return ret; |
| 243 | #endif |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 244 | priv->regs = (struct rockchip_spi *)plat->base; |
| 245 | |
| 246 | priv->last_transaction_us = timer_get_us(); |
| 247 | priv->max_freq = plat->frequency; |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 248 | |
Philipp Tomsich | bd37671 | 2017-04-20 22:05:51 +0200 | [diff] [blame] | 249 | /* Clamp the value from the DTS against any hardware limits */ |
| 250 | if (priv->max_freq > ROCKCHIP_SPI_MAX_RATE) |
| 251 | priv->max_freq = ROCKCHIP_SPI_MAX_RATE; |
| 252 | |
| 253 | /* Find a module-input clock that fits with the max_freq setting */ |
| 254 | ret = clk_set_rate(&priv->clk, |
| 255 | rockchip_spi_calc_modclk(priv->max_freq)); |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 256 | if (ret < 0) { |
| 257 | debug("%s: Failed to set clock: %d\n", __func__, ret); |
| 258 | return ret; |
| 259 | } |
| 260 | priv->input_rate = ret; |
| 261 | debug("%s: rate = %u\n", __func__, priv->input_rate); |
| 262 | priv->bits_per_word = 8; |
| 263 | priv->tmode = TMOD_TR; /* Tx & Rx */ |
| 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | static int rockchip_spi_claim_bus(struct udevice *dev) |
| 269 | { |
| 270 | struct udevice *bus = dev->parent; |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 271 | struct rockchip_spi_priv *priv = dev_get_priv(bus); |
| 272 | struct rockchip_spi *regs = priv->regs; |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 273 | u8 spi_dfs, spi_tf; |
| 274 | uint ctrlr0; |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 275 | |
| 276 | /* Disable the SPI hardware */ |
| 277 | rkspi_enable_chip(regs, 0); |
| 278 | |
| 279 | switch (priv->bits_per_word) { |
| 280 | case 8: |
| 281 | priv->n_bytes = 1; |
| 282 | spi_dfs = DFS_8BIT; |
| 283 | spi_tf = HALF_WORD_OFF; |
| 284 | break; |
| 285 | case 16: |
| 286 | priv->n_bytes = 2; |
| 287 | spi_dfs = DFS_16BIT; |
| 288 | spi_tf = HALF_WORD_ON; |
| 289 | break; |
| 290 | default: |
| 291 | debug("%s: unsupported bits: %dbits\n", __func__, |
| 292 | priv->bits_per_word); |
| 293 | return -EPROTONOSUPPORT; |
| 294 | } |
| 295 | |
Simon Glass | 28a943c | 2016-01-21 19:44:03 -0700 | [diff] [blame] | 296 | if (priv->speed_hz != priv->last_speed_hz) |
| 297 | rkspi_set_clk(priv, priv->speed_hz); |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 298 | |
| 299 | /* Operation Mode */ |
| 300 | ctrlr0 = OMOD_MASTER << OMOD_SHIFT; |
| 301 | |
| 302 | /* Data Frame Size */ |
Simon Glass | 2b9fe11 | 2016-01-21 19:44:04 -0700 | [diff] [blame] | 303 | ctrlr0 |= spi_dfs << DFS_SHIFT; |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 304 | |
| 305 | /* set SPI mode 0..3 */ |
| 306 | if (priv->mode & SPI_CPOL) |
| 307 | ctrlr0 |= SCOL_HIGH << SCOL_SHIFT; |
| 308 | if (priv->mode & SPI_CPHA) |
| 309 | ctrlr0 |= SCPH_TOGSTA << SCPH_SHIFT; |
| 310 | |
| 311 | /* Chip Select Mode */ |
| 312 | ctrlr0 |= CSM_KEEP << CSM_SHIFT; |
| 313 | |
| 314 | /* SSN to Sclk_out delay */ |
| 315 | ctrlr0 |= SSN_DELAY_ONE << SSN_DELAY_SHIFT; |
| 316 | |
| 317 | /* Serial Endian Mode */ |
| 318 | ctrlr0 |= SEM_LITTLE << SEM_SHIFT; |
| 319 | |
| 320 | /* First Bit Mode */ |
| 321 | ctrlr0 |= FBM_MSB << FBM_SHIFT; |
| 322 | |
| 323 | /* Byte and Halfword Transform */ |
Simon Glass | 2b9fe11 | 2016-01-21 19:44:04 -0700 | [diff] [blame] | 324 | ctrlr0 |= spi_tf << HALF_WORD_TX_SHIFT; |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 325 | |
| 326 | /* Rxd Sample Delay */ |
| 327 | ctrlr0 |= 0 << RXDSD_SHIFT; |
| 328 | |
| 329 | /* Frame Format */ |
| 330 | ctrlr0 |= FRF_SPI << FRF_SHIFT; |
| 331 | |
| 332 | /* Tx and Rx mode */ |
| 333 | ctrlr0 |= (priv->tmode & TMOD_MASK) << TMOD_SHIFT; |
| 334 | |
| 335 | writel(ctrlr0, ®s->ctrlr0); |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | static int rockchip_spi_release_bus(struct udevice *dev) |
| 341 | { |
Simon Glass | e15af8e | 2016-01-21 19:44:11 -0700 | [diff] [blame] | 342 | struct udevice *bus = dev->parent; |
| 343 | struct rockchip_spi_priv *priv = dev_get_priv(bus); |
| 344 | |
| 345 | rkspi_enable_chip(priv->regs, false); |
| 346 | |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | static int rockchip_spi_xfer(struct udevice *dev, unsigned int bitlen, |
| 351 | const void *dout, void *din, unsigned long flags) |
| 352 | { |
| 353 | struct udevice *bus = dev->parent; |
| 354 | struct rockchip_spi_priv *priv = dev_get_priv(bus); |
| 355 | struct rockchip_spi *regs = priv->regs; |
| 356 | struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev); |
| 357 | int len = bitlen >> 3; |
| 358 | const u8 *out = dout; |
| 359 | u8 *in = din; |
| 360 | int toread, towrite; |
| 361 | int ret; |
| 362 | |
| 363 | debug("%s: dout=%p, din=%p, len=%x, flags=%lx\n", __func__, dout, din, |
| 364 | len, flags); |
| 365 | if (DEBUG_RK_SPI) |
| 366 | rkspi_dump_regs(regs); |
| 367 | |
| 368 | /* Assert CS before transfer */ |
| 369 | if (flags & SPI_XFER_BEGIN) |
Simon Glass | 183a3a0 | 2016-01-21 19:44:10 -0700 | [diff] [blame] | 370 | spi_cs_activate(dev, slave_plat->cs); |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 371 | |
| 372 | while (len > 0) { |
| 373 | int todo = min(len, 0xffff); |
| 374 | |
Simon Glass | e15af8e | 2016-01-21 19:44:11 -0700 | [diff] [blame] | 375 | rkspi_enable_chip(regs, false); |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 376 | writel(todo - 1, ®s->ctrlr1); |
| 377 | rkspi_enable_chip(regs, true); |
| 378 | |
| 379 | toread = todo; |
| 380 | towrite = todo; |
| 381 | while (toread || towrite) { |
| 382 | u32 status = readl(®s->sr); |
| 383 | |
| 384 | if (towrite && !(status & SR_TF_FULL)) { |
| 385 | writel(out ? *out++ : 0, regs->txdr); |
| 386 | towrite--; |
| 387 | } |
| 388 | if (toread && !(status & SR_RF_EMPT)) { |
| 389 | u32 byte = readl(regs->rxdr); |
| 390 | |
| 391 | if (in) |
| 392 | *in++ = byte; |
| 393 | toread--; |
| 394 | } |
| 395 | } |
| 396 | ret = rkspi_wait_till_not_busy(regs); |
| 397 | if (ret) |
| 398 | break; |
| 399 | len -= todo; |
| 400 | } |
| 401 | |
| 402 | /* Deassert CS after transfer */ |
| 403 | if (flags & SPI_XFER_END) |
Simon Glass | 183a3a0 | 2016-01-21 19:44:10 -0700 | [diff] [blame] | 404 | spi_cs_deactivate(dev, slave_plat->cs); |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 405 | |
| 406 | rkspi_enable_chip(regs, false); |
| 407 | |
| 408 | return ret; |
| 409 | } |
| 410 | |
| 411 | static int rockchip_spi_set_speed(struct udevice *bus, uint speed) |
| 412 | { |
| 413 | struct rockchip_spi_priv *priv = dev_get_priv(bus); |
| 414 | |
Philipp Tomsich | bd37671 | 2017-04-20 22:05:51 +0200 | [diff] [blame] | 415 | /* Clamp to the maximum frequency specified in the DTS */ |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 416 | if (speed > priv->max_freq) |
| 417 | speed = priv->max_freq; |
Philipp Tomsich | bd37671 | 2017-04-20 22:05:51 +0200 | [diff] [blame] | 418 | |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 419 | priv->speed_hz = speed; |
| 420 | |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | static int rockchip_spi_set_mode(struct udevice *bus, uint mode) |
| 425 | { |
| 426 | struct rockchip_spi_priv *priv = dev_get_priv(bus); |
| 427 | |
| 428 | priv->mode = mode; |
| 429 | |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | static const struct dm_spi_ops rockchip_spi_ops = { |
| 434 | .claim_bus = rockchip_spi_claim_bus, |
| 435 | .release_bus = rockchip_spi_release_bus, |
| 436 | .xfer = rockchip_spi_xfer, |
| 437 | .set_speed = rockchip_spi_set_speed, |
| 438 | .set_mode = rockchip_spi_set_mode, |
| 439 | /* |
| 440 | * cs_info is not needed, since we require all chip selects to be |
| 441 | * in the device tree explicitly |
| 442 | */ |
| 443 | }; |
| 444 | |
| 445 | static const struct udevice_id rockchip_spi_ids[] = { |
| 446 | { .compatible = "rockchip,rk3288-spi" }, |
Jakob Unterwurzacher | cdeb4d7 | 2017-04-20 22:05:54 +0200 | [diff] [blame] | 447 | { .compatible = "rockchip,rk3399-spi" }, |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 448 | { } |
| 449 | }; |
| 450 | |
| 451 | U_BOOT_DRIVER(rockchip_spi) = { |
Simon Glass | 6e019c4 | 2016-11-13 14:22:02 -0700 | [diff] [blame] | 452 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 453 | .name = "rockchip_rk3288_spi", |
| 454 | #else |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 455 | .name = "rockchip_spi", |
Simon Glass | 6e019c4 | 2016-11-13 14:22:02 -0700 | [diff] [blame] | 456 | #endif |
Simon Glass | 1b2fd5b | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 457 | .id = UCLASS_SPI, |
| 458 | .of_match = rockchip_spi_ids, |
| 459 | .ops = &rockchip_spi_ops, |
| 460 | .ofdata_to_platdata = rockchip_spi_ofdata_to_platdata, |
| 461 | .platdata_auto_alloc_size = sizeof(struct rockchip_spi_platdata), |
| 462 | .priv_auto_alloc_size = sizeof(struct rockchip_spi_priv), |
| 463 | .probe = rockchip_spi_probe, |
| 464 | }; |