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