Simon Glass | d1c1377 | 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 | 32308d2 | 2016-11-13 14:22:02 -0700 | [diff] [blame] | 15 | #include <dt-structs.h> |
Simon Glass | d1c1377 | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 16 | #include <errno.h> |
| 17 | #include <spi.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 18 | #include <linux/errno.h> |
Simon Glass | d1c1377 | 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 | 32308d2 | 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 | d1c1377 | 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 | 58a52e8 | 2016-01-21 19:44:10 -0700 | [diff] [blame] | 37 | uint activate_delay_us; /* Delay to wait after activate */ |
Simon Glass | d1c1377 | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | struct rockchip_spi_priv { |
| 41 | struct rockchip_spi *regs; |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 42 | struct clk clk; |
Simon Glass | d1c1377 | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 43 | unsigned int max_freq; |
| 44 | unsigned int mode; |
Simon Glass | d1c1377 | 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 | 30508e9 | 2016-01-21 19:44:03 -0700 | [diff] [blame] | 49 | unsigned int last_speed_hz; |
Simon Glass | d1c1377 | 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 | { |
| 82 | uint clk_div; |
| 83 | |
| 84 | clk_div = clk_get_divisor(priv->input_rate, speed); |
| 85 | debug("spi speed %u, div %u\n", speed, clk_div); |
| 86 | |
| 87 | writel(clk_div, &priv->regs->baudr); |
Simon Glass | 30508e9 | 2016-01-21 19:44:03 -0700 | [diff] [blame] | 88 | priv->last_speed_hz = speed; |
Simon Glass | d1c1377 | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static int rkspi_wait_till_not_busy(struct rockchip_spi *regs) |
| 92 | { |
| 93 | unsigned long start; |
| 94 | |
| 95 | start = get_timer(0); |
| 96 | while (readl(®s->sr) & SR_BUSY) { |
| 97 | if (get_timer(start) > ROCKCHIP_SPI_TIMEOUT_MS) { |
| 98 | debug("RK SPI: Status keeps busy for 1000us after a read/write!\n"); |
| 99 | return -ETIMEDOUT; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
Simon Glass | 58a52e8 | 2016-01-21 19:44:10 -0700 | [diff] [blame] | 106 | static void spi_cs_activate(struct udevice *dev, uint cs) |
Simon Glass | d1c1377 | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 107 | { |
Simon Glass | 58a52e8 | 2016-01-21 19:44:10 -0700 | [diff] [blame] | 108 | struct udevice *bus = dev->parent; |
| 109 | struct rockchip_spi_platdata *plat = bus->platdata; |
| 110 | struct rockchip_spi_priv *priv = dev_get_priv(bus); |
| 111 | struct rockchip_spi *regs = priv->regs; |
| 112 | |
Simon Glass | cf65b00 | 2016-11-13 14:22:03 -0700 | [diff] [blame] | 113 | /* If it's too soon to do another transaction, wait */ |
| 114 | if (plat->deactivate_delay_us && priv->last_transaction_us) { |
| 115 | ulong delay_us; /* The delay completed so far */ |
| 116 | delay_us = timer_get_us() - priv->last_transaction_us; |
| 117 | if (delay_us < plat->deactivate_delay_us) |
| 118 | udelay(plat->deactivate_delay_us - delay_us); |
| 119 | } |
| 120 | |
Simon Glass | d1c1377 | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 121 | debug("activate cs%u\n", cs); |
| 122 | writel(1 << cs, ®s->ser); |
Simon Glass | 58a52e8 | 2016-01-21 19:44:10 -0700 | [diff] [blame] | 123 | if (plat->activate_delay_us) |
| 124 | udelay(plat->activate_delay_us); |
Simon Glass | d1c1377 | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 125 | } |
| 126 | |
Simon Glass | 58a52e8 | 2016-01-21 19:44:10 -0700 | [diff] [blame] | 127 | static void spi_cs_deactivate(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 | d1c1377 | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 134 | debug("deactivate cs%u\n", cs); |
| 135 | writel(0, ®s->ser); |
Simon Glass | 58a52e8 | 2016-01-21 19:44:10 -0700 | [diff] [blame] | 136 | |
| 137 | /* Remember time of this transaction so we can honour the bus delay */ |
| 138 | if (plat->deactivate_delay_us) |
| 139 | priv->last_transaction_us = timer_get_us(); |
Simon Glass | d1c1377 | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 140 | } |
| 141 | |
Simon Glass | 32308d2 | 2016-11-13 14:22:02 -0700 | [diff] [blame] | 142 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 143 | static int conv_of_platdata(struct udevice *dev) |
| 144 | { |
| 145 | struct rockchip_spi_platdata *plat = dev->platdata; |
| 146 | struct dtd_rockchip_rk3288_spi *dtplat = &plat->of_plat; |
| 147 | struct rockchip_spi_priv *priv = dev_get_priv(dev); |
| 148 | int ret; |
| 149 | |
| 150 | plat->base = dtplat->reg[0]; |
| 151 | plat->frequency = 20000000; |
| 152 | ret = clk_get_by_index_platdata(dev, 0, dtplat->clocks, &priv->clk); |
| 153 | if (ret < 0) |
| 154 | return ret; |
| 155 | dev->req_seq = 0; |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | #endif |
| 160 | |
Simon Glass | d1c1377 | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 161 | static int rockchip_spi_ofdata_to_platdata(struct udevice *bus) |
| 162 | { |
Simon Glass | 32308d2 | 2016-11-13 14:22:02 -0700 | [diff] [blame] | 163 | #if !CONFIG_IS_ENABLED(OF_PLATDATA) |
| 164 | struct rockchip_spi_platdata *plat = dev_get_platdata(bus); |
Simon Glass | a95049e | 2016-01-21 19:43:43 -0700 | [diff] [blame] | 165 | struct rockchip_spi_priv *priv = dev_get_priv(bus); |
Simon Glass | d1c1377 | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 166 | const void *blob = gd->fdt_blob; |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 167 | int node = dev_of_offset(bus); |
Simon Glass | d1c1377 | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 168 | int ret; |
| 169 | |
| 170 | plat->base = dev_get_addr(bus); |
Simon Glass | d1c1377 | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 171 | |
Simon Glass | a95049e | 2016-01-21 19:43:43 -0700 | [diff] [blame] | 172 | ret = clk_get_by_index(bus, 0, &priv->clk); |
| 173 | if (ret < 0) { |
| 174 | debug("%s: Could not get clock for %s: %d\n", __func__, |
| 175 | bus->name, ret); |
| 176 | return ret; |
| 177 | } |
Simon Glass | d1c1377 | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 178 | |
| 179 | plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency", |
Simon Glass | a95049e | 2016-01-21 19:43:43 -0700 | [diff] [blame] | 180 | 50000000); |
Simon Glass | d1c1377 | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 181 | plat->deactivate_delay_us = fdtdec_get_int(blob, node, |
| 182 | "spi-deactivate-delay", 0); |
Simon Glass | 58a52e8 | 2016-01-21 19:44:10 -0700 | [diff] [blame] | 183 | plat->activate_delay_us = fdtdec_get_int(blob, node, |
| 184 | "spi-activate-delay", 0); |
Simon Glass | c45aec9 | 2016-01-21 19:44:12 -0700 | [diff] [blame] | 185 | debug("%s: base=%x, max-frequency=%d, deactivate_delay=%d\n", |
| 186 | __func__, (uint)plat->base, plat->frequency, |
Simon Glass | d1c1377 | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 187 | plat->deactivate_delay_us); |
Simon Glass | 32308d2 | 2016-11-13 14:22:02 -0700 | [diff] [blame] | 188 | #endif |
Simon Glass | d1c1377 | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
Philipp Tomsich | 4544788 | 2017-04-20 22:05:51 +0200 | [diff] [blame^] | 193 | static int rockchip_spi_calc_modclk(ulong max_freq) |
| 194 | { |
| 195 | unsigned div; |
| 196 | const unsigned long gpll_hz = 594000000UL; |
| 197 | |
| 198 | /* |
| 199 | * We need to find an input clock that provides at least twice |
| 200 | * the maximum frequency and can be generated from the assumed |
| 201 | * speed of GPLL (594MHz) using an integer divider. |
| 202 | * |
| 203 | * To give us more achievable bitrates at higher speeds (these |
| 204 | * are generated by dividing by an even 16-bit integer from |
| 205 | * this frequency), we try to have an input frequency of at |
| 206 | * least 4x our max_freq. |
| 207 | */ |
| 208 | |
| 209 | div = DIV_ROUND_UP(gpll_hz, max_freq * 4); |
| 210 | return gpll_hz / div; |
| 211 | } |
| 212 | |
Simon Glass | d1c1377 | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 213 | static int rockchip_spi_probe(struct udevice *bus) |
| 214 | { |
| 215 | struct rockchip_spi_platdata *plat = dev_get_platdata(bus); |
| 216 | struct rockchip_spi_priv *priv = dev_get_priv(bus); |
| 217 | int ret; |
| 218 | |
| 219 | debug("%s: probe\n", __func__); |
Simon Glass | 32308d2 | 2016-11-13 14:22:02 -0700 | [diff] [blame] | 220 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 221 | ret = conv_of_platdata(bus); |
| 222 | if (ret) |
| 223 | return ret; |
| 224 | #endif |
Simon Glass | d1c1377 | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 225 | priv->regs = (struct rockchip_spi *)plat->base; |
| 226 | |
| 227 | priv->last_transaction_us = timer_get_us(); |
| 228 | priv->max_freq = plat->frequency; |
Simon Glass | d1c1377 | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 229 | |
Philipp Tomsich | 4544788 | 2017-04-20 22:05:51 +0200 | [diff] [blame^] | 230 | /* Clamp the value from the DTS against any hardware limits */ |
| 231 | if (priv->max_freq > ROCKCHIP_SPI_MAX_RATE) |
| 232 | priv->max_freq = ROCKCHIP_SPI_MAX_RATE; |
| 233 | |
| 234 | /* Find a module-input clock that fits with the max_freq setting */ |
| 235 | ret = clk_set_rate(&priv->clk, |
| 236 | rockchip_spi_calc_modclk(priv->max_freq)); |
Simon Glass | d1c1377 | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 237 | if (ret < 0) { |
| 238 | debug("%s: Failed to set clock: %d\n", __func__, ret); |
| 239 | return ret; |
| 240 | } |
| 241 | priv->input_rate = ret; |
| 242 | debug("%s: rate = %u\n", __func__, priv->input_rate); |
| 243 | priv->bits_per_word = 8; |
| 244 | priv->tmode = TMOD_TR; /* Tx & Rx */ |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | static int rockchip_spi_claim_bus(struct udevice *dev) |
| 250 | { |
| 251 | struct udevice *bus = dev->parent; |
Simon Glass | d1c1377 | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 252 | struct rockchip_spi_priv *priv = dev_get_priv(bus); |
| 253 | struct rockchip_spi *regs = priv->regs; |
Simon Glass | d1c1377 | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 254 | u8 spi_dfs, spi_tf; |
| 255 | uint ctrlr0; |
Simon Glass | d1c1377 | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 256 | |
| 257 | /* Disable the SPI hardware */ |
| 258 | rkspi_enable_chip(regs, 0); |
| 259 | |
| 260 | switch (priv->bits_per_word) { |
| 261 | case 8: |
| 262 | priv->n_bytes = 1; |
| 263 | spi_dfs = DFS_8BIT; |
| 264 | spi_tf = HALF_WORD_OFF; |
| 265 | break; |
| 266 | case 16: |
| 267 | priv->n_bytes = 2; |
| 268 | spi_dfs = DFS_16BIT; |
| 269 | spi_tf = HALF_WORD_ON; |
| 270 | break; |
| 271 | default: |
| 272 | debug("%s: unsupported bits: %dbits\n", __func__, |
| 273 | priv->bits_per_word); |
| 274 | return -EPROTONOSUPPORT; |
| 275 | } |
| 276 | |
Simon Glass | 30508e9 | 2016-01-21 19:44:03 -0700 | [diff] [blame] | 277 | if (priv->speed_hz != priv->last_speed_hz) |
| 278 | rkspi_set_clk(priv, priv->speed_hz); |
Simon Glass | d1c1377 | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 279 | |
| 280 | /* Operation Mode */ |
| 281 | ctrlr0 = OMOD_MASTER << OMOD_SHIFT; |
| 282 | |
| 283 | /* Data Frame Size */ |
Simon Glass | f9ed818 | 2016-01-21 19:44:04 -0700 | [diff] [blame] | 284 | ctrlr0 |= spi_dfs << DFS_SHIFT; |
Simon Glass | d1c1377 | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 285 | |
| 286 | /* set SPI mode 0..3 */ |
| 287 | if (priv->mode & SPI_CPOL) |
| 288 | ctrlr0 |= SCOL_HIGH << SCOL_SHIFT; |
| 289 | if (priv->mode & SPI_CPHA) |
| 290 | ctrlr0 |= SCPH_TOGSTA << SCPH_SHIFT; |
| 291 | |
| 292 | /* Chip Select Mode */ |
| 293 | ctrlr0 |= CSM_KEEP << CSM_SHIFT; |
| 294 | |
| 295 | /* SSN to Sclk_out delay */ |
| 296 | ctrlr0 |= SSN_DELAY_ONE << SSN_DELAY_SHIFT; |
| 297 | |
| 298 | /* Serial Endian Mode */ |
| 299 | ctrlr0 |= SEM_LITTLE << SEM_SHIFT; |
| 300 | |
| 301 | /* First Bit Mode */ |
| 302 | ctrlr0 |= FBM_MSB << FBM_SHIFT; |
| 303 | |
| 304 | /* Byte and Halfword Transform */ |
Simon Glass | f9ed818 | 2016-01-21 19:44:04 -0700 | [diff] [blame] | 305 | ctrlr0 |= spi_tf << HALF_WORD_TX_SHIFT; |
Simon Glass | d1c1377 | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 306 | |
| 307 | /* Rxd Sample Delay */ |
| 308 | ctrlr0 |= 0 << RXDSD_SHIFT; |
| 309 | |
| 310 | /* Frame Format */ |
| 311 | ctrlr0 |= FRF_SPI << FRF_SHIFT; |
| 312 | |
| 313 | /* Tx and Rx mode */ |
| 314 | ctrlr0 |= (priv->tmode & TMOD_MASK) << TMOD_SHIFT; |
| 315 | |
| 316 | writel(ctrlr0, ®s->ctrlr0); |
Simon Glass | d1c1377 | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 317 | |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | static int rockchip_spi_release_bus(struct udevice *dev) |
| 322 | { |
Simon Glass | 2c9693c | 2016-01-21 19:44:11 -0700 | [diff] [blame] | 323 | struct udevice *bus = dev->parent; |
| 324 | struct rockchip_spi_priv *priv = dev_get_priv(bus); |
| 325 | |
| 326 | rkspi_enable_chip(priv->regs, false); |
| 327 | |
Simon Glass | d1c1377 | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | static int rockchip_spi_xfer(struct udevice *dev, unsigned int bitlen, |
| 332 | const void *dout, void *din, unsigned long flags) |
| 333 | { |
| 334 | struct udevice *bus = dev->parent; |
| 335 | struct rockchip_spi_priv *priv = dev_get_priv(bus); |
| 336 | struct rockchip_spi *regs = priv->regs; |
| 337 | struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev); |
| 338 | int len = bitlen >> 3; |
| 339 | const u8 *out = dout; |
| 340 | u8 *in = din; |
| 341 | int toread, towrite; |
| 342 | int ret; |
| 343 | |
| 344 | debug("%s: dout=%p, din=%p, len=%x, flags=%lx\n", __func__, dout, din, |
| 345 | len, flags); |
| 346 | if (DEBUG_RK_SPI) |
| 347 | rkspi_dump_regs(regs); |
| 348 | |
| 349 | /* Assert CS before transfer */ |
| 350 | if (flags & SPI_XFER_BEGIN) |
Simon Glass | 58a52e8 | 2016-01-21 19:44:10 -0700 | [diff] [blame] | 351 | spi_cs_activate(dev, slave_plat->cs); |
Simon Glass | d1c1377 | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 352 | |
| 353 | while (len > 0) { |
| 354 | int todo = min(len, 0xffff); |
| 355 | |
Simon Glass | 2c9693c | 2016-01-21 19:44:11 -0700 | [diff] [blame] | 356 | rkspi_enable_chip(regs, false); |
Simon Glass | d1c1377 | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 357 | writel(todo - 1, ®s->ctrlr1); |
| 358 | rkspi_enable_chip(regs, true); |
| 359 | |
| 360 | toread = todo; |
| 361 | towrite = todo; |
| 362 | while (toread || towrite) { |
| 363 | u32 status = readl(®s->sr); |
| 364 | |
| 365 | if (towrite && !(status & SR_TF_FULL)) { |
| 366 | writel(out ? *out++ : 0, regs->txdr); |
| 367 | towrite--; |
| 368 | } |
| 369 | if (toread && !(status & SR_RF_EMPT)) { |
| 370 | u32 byte = readl(regs->rxdr); |
| 371 | |
| 372 | if (in) |
| 373 | *in++ = byte; |
| 374 | toread--; |
| 375 | } |
| 376 | } |
| 377 | ret = rkspi_wait_till_not_busy(regs); |
| 378 | if (ret) |
| 379 | break; |
| 380 | len -= todo; |
| 381 | } |
| 382 | |
| 383 | /* Deassert CS after transfer */ |
| 384 | if (flags & SPI_XFER_END) |
Simon Glass | 58a52e8 | 2016-01-21 19:44:10 -0700 | [diff] [blame] | 385 | spi_cs_deactivate(dev, slave_plat->cs); |
Simon Glass | d1c1377 | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 386 | |
| 387 | rkspi_enable_chip(regs, false); |
| 388 | |
| 389 | return ret; |
| 390 | } |
| 391 | |
| 392 | static int rockchip_spi_set_speed(struct udevice *bus, uint speed) |
| 393 | { |
| 394 | struct rockchip_spi_priv *priv = dev_get_priv(bus); |
| 395 | |
Philipp Tomsich | 4544788 | 2017-04-20 22:05:51 +0200 | [diff] [blame^] | 396 | /* Clamp to the maximum frequency specified in the DTS */ |
Simon Glass | d1c1377 | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 397 | if (speed > priv->max_freq) |
| 398 | speed = priv->max_freq; |
Philipp Tomsich | 4544788 | 2017-04-20 22:05:51 +0200 | [diff] [blame^] | 399 | |
Simon Glass | d1c1377 | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 400 | priv->speed_hz = speed; |
| 401 | |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | static int rockchip_spi_set_mode(struct udevice *bus, uint mode) |
| 406 | { |
| 407 | struct rockchip_spi_priv *priv = dev_get_priv(bus); |
| 408 | |
| 409 | priv->mode = mode; |
| 410 | |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | static const struct dm_spi_ops rockchip_spi_ops = { |
| 415 | .claim_bus = rockchip_spi_claim_bus, |
| 416 | .release_bus = rockchip_spi_release_bus, |
| 417 | .xfer = rockchip_spi_xfer, |
| 418 | .set_speed = rockchip_spi_set_speed, |
| 419 | .set_mode = rockchip_spi_set_mode, |
| 420 | /* |
| 421 | * cs_info is not needed, since we require all chip selects to be |
| 422 | * in the device tree explicitly |
| 423 | */ |
| 424 | }; |
| 425 | |
| 426 | static const struct udevice_id rockchip_spi_ids[] = { |
| 427 | { .compatible = "rockchip,rk3288-spi" }, |
| 428 | { } |
| 429 | }; |
| 430 | |
| 431 | U_BOOT_DRIVER(rockchip_spi) = { |
Simon Glass | 32308d2 | 2016-11-13 14:22:02 -0700 | [diff] [blame] | 432 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 433 | .name = "rockchip_rk3288_spi", |
| 434 | #else |
Simon Glass | d1c1377 | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 435 | .name = "rockchip_spi", |
Simon Glass | 32308d2 | 2016-11-13 14:22:02 -0700 | [diff] [blame] | 436 | #endif |
Simon Glass | d1c1377 | 2015-09-01 19:19:37 -0600 | [diff] [blame] | 437 | .id = UCLASS_SPI, |
| 438 | .of_match = rockchip_spi_ids, |
| 439 | .ops = &rockchip_spi_ops, |
| 440 | .ofdata_to_platdata = rockchip_spi_ofdata_to_platdata, |
| 441 | .platdata_auto_alloc_size = sizeof(struct rockchip_spi_platdata), |
| 442 | .priv_auto_alloc_size = sizeof(struct rockchip_spi_priv), |
| 443 | .probe = rockchip_spi_probe, |
| 444 | }; |