Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Nobuhiro Iwamatsu | 16f47c9 | 2013-12-18 15:31:55 +0900 | [diff] [blame] | 2 | /* |
| 3 | * SH QSPI (Quad SPI) driver |
| 4 | * |
| 5 | * Copyright (C) 2013 Renesas Electronics Corporation |
| 6 | * Copyright (C) 2013 Nobuhiro Iwamatsu <nobuhiro.iwamatsu.yj@renesas.com> |
Nobuhiro Iwamatsu | 16f47c9 | 2013-12-18 15:31:55 +0900 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Simon Glass | 24b852a | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 10 | #include <console.h> |
Nobuhiro Iwamatsu | 16f47c9 | 2013-12-18 15:31:55 +0900 | [diff] [blame] | 11 | #include <malloc.h> |
| 12 | #include <spi.h> |
Marek Vasut | 9573db6 | 2018-04-10 16:58:46 +0200 | [diff] [blame] | 13 | #include <wait_bit.h> |
Nobuhiro Iwamatsu | 22e75d6 | 2014-01-08 10:16:25 +0900 | [diff] [blame] | 14 | #include <asm/arch/rmobile.h> |
Nobuhiro Iwamatsu | 16f47c9 | 2013-12-18 15:31:55 +0900 | [diff] [blame] | 15 | #include <asm/io.h> |
| 16 | |
| 17 | /* SH QSPI register bit masks <REG>_<BIT> */ |
| 18 | #define SPCR_MSTR 0x08 |
| 19 | #define SPCR_SPE 0x40 |
| 20 | #define SPSR_SPRFF 0x80 |
| 21 | #define SPSR_SPTEF 0x20 |
| 22 | #define SPPCR_IO3FV 0x04 |
| 23 | #define SPPCR_IO2FV 0x02 |
| 24 | #define SPPCR_IO1FV 0x01 |
Jagan Teki | ccaa948 | 2015-10-23 01:38:47 +0530 | [diff] [blame] | 25 | #define SPBDCR_RXBC0 BIT(0) |
| 26 | #define SPCMD_SCKDEN BIT(15) |
| 27 | #define SPCMD_SLNDEN BIT(14) |
| 28 | #define SPCMD_SPNDEN BIT(13) |
| 29 | #define SPCMD_SSLKP BIT(7) |
| 30 | #define SPCMD_BRDV0 BIT(2) |
Nobuhiro Iwamatsu | 16f47c9 | 2013-12-18 15:31:55 +0900 | [diff] [blame] | 31 | #define SPCMD_INIT1 SPCMD_SCKDEN | SPCMD_SLNDEN | \ |
| 32 | SPCMD_SPNDEN | SPCMD_SSLKP | \ |
| 33 | SPCMD_BRDV0 |
| 34 | #define SPCMD_INIT2 SPCMD_SPNDEN | SPCMD_SSLKP | \ |
| 35 | SPCMD_BRDV0 |
Jagan Teki | ccaa948 | 2015-10-23 01:38:47 +0530 | [diff] [blame] | 36 | #define SPBFCR_TXRST BIT(7) |
| 37 | #define SPBFCR_RXRST BIT(6) |
Marek Vasut | ea5512e | 2018-04-10 16:43:47 +0200 | [diff] [blame] | 38 | #define SPBFCR_TXTRG 0x30 |
| 39 | #define SPBFCR_RXTRG 0x07 |
Nobuhiro Iwamatsu | 16f47c9 | 2013-12-18 15:31:55 +0900 | [diff] [blame] | 40 | |
| 41 | /* SH QSPI register set */ |
| 42 | struct sh_qspi_regs { |
Marek Vasut | 0e6fa20 | 2018-04-10 16:47:38 +0200 | [diff] [blame] | 43 | u8 spcr; |
| 44 | u8 sslp; |
| 45 | u8 sppcr; |
| 46 | u8 spsr; |
| 47 | u32 spdr; |
| 48 | u8 spscr; |
| 49 | u8 spssr; |
| 50 | u8 spbr; |
| 51 | u8 spdcr; |
| 52 | u8 spckd; |
| 53 | u8 sslnd; |
| 54 | u8 spnd; |
| 55 | u8 dummy0; |
| 56 | u16 spcmd0; |
| 57 | u16 spcmd1; |
| 58 | u16 spcmd2; |
| 59 | u16 spcmd3; |
| 60 | u8 spbfcr; |
| 61 | u8 dummy1; |
| 62 | u16 spbdcr; |
| 63 | u32 spbmul0; |
| 64 | u32 spbmul1; |
| 65 | u32 spbmul2; |
| 66 | u32 spbmul3; |
Nobuhiro Iwamatsu | 16f47c9 | 2013-12-18 15:31:55 +0900 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | struct sh_qspi_slave { |
Marek Vasut | b3bec25 | 2018-08-25 19:34:24 +0200 | [diff] [blame^] | 70 | #ifndef CONFIG_DM_SPI |
Nobuhiro Iwamatsu | 16f47c9 | 2013-12-18 15:31:55 +0900 | [diff] [blame] | 71 | struct spi_slave slave; |
Marek Vasut | b3bec25 | 2018-08-25 19:34:24 +0200 | [diff] [blame^] | 72 | #endif |
Nobuhiro Iwamatsu | 16f47c9 | 2013-12-18 15:31:55 +0900 | [diff] [blame] | 73 | struct sh_qspi_regs *regs; |
| 74 | }; |
| 75 | |
Nobuhiro Iwamatsu | 16f47c9 | 2013-12-18 15:31:55 +0900 | [diff] [blame] | 76 | static void sh_qspi_init(struct sh_qspi_slave *ss) |
| 77 | { |
| 78 | /* QSPI initialize */ |
| 79 | /* Set master mode only */ |
| 80 | writeb(SPCR_MSTR, &ss->regs->spcr); |
| 81 | |
| 82 | /* Set SSL signal level */ |
| 83 | writeb(0x00, &ss->regs->sslp); |
| 84 | |
| 85 | /* Set MOSI signal value when transfer is in idle state */ |
| 86 | writeb(SPPCR_IO3FV|SPPCR_IO2FV, &ss->regs->sppcr); |
| 87 | |
| 88 | /* Set bit rate. See 58.3.8 Quad Serial Peripheral Interface */ |
| 89 | writeb(0x01, &ss->regs->spbr); |
| 90 | |
| 91 | /* Disable Dummy Data Transmission */ |
| 92 | writeb(0x00, &ss->regs->spdcr); |
| 93 | |
| 94 | /* Set clock delay value */ |
| 95 | writeb(0x00, &ss->regs->spckd); |
| 96 | |
| 97 | /* Set SSL negation delay value */ |
| 98 | writeb(0x00, &ss->regs->sslnd); |
| 99 | |
| 100 | /* Set next-access delay value */ |
| 101 | writeb(0x00, &ss->regs->spnd); |
| 102 | |
| 103 | /* Set equence command */ |
| 104 | writew(SPCMD_INIT2, &ss->regs->spcmd0); |
| 105 | |
| 106 | /* Reset transfer and receive Buffer */ |
| 107 | setbits_8(&ss->regs->spbfcr, SPBFCR_TXRST|SPBFCR_RXRST); |
| 108 | |
| 109 | /* Clear transfer and receive Buffer control bit */ |
| 110 | clrbits_8(&ss->regs->spbfcr, SPBFCR_TXRST|SPBFCR_RXRST); |
| 111 | |
| 112 | /* Set equence control method. Use equence0 only */ |
| 113 | writeb(0x00, &ss->regs->spscr); |
| 114 | |
| 115 | /* Enable SPI function */ |
| 116 | setbits_8(&ss->regs->spcr, SPCR_SPE); |
| 117 | } |
| 118 | |
Marek Vasut | b3bec25 | 2018-08-25 19:34:24 +0200 | [diff] [blame^] | 119 | static void sh_qspi_cs_activate(struct sh_qspi_slave *ss) |
Nobuhiro Iwamatsu | 16f47c9 | 2013-12-18 15:31:55 +0900 | [diff] [blame] | 120 | { |
Nobuhiro Iwamatsu | 16f47c9 | 2013-12-18 15:31:55 +0900 | [diff] [blame] | 121 | /* Set master mode only */ |
| 122 | writeb(SPCR_MSTR, &ss->regs->spcr); |
| 123 | |
| 124 | /* Set command */ |
| 125 | writew(SPCMD_INIT1, &ss->regs->spcmd0); |
| 126 | |
| 127 | /* Reset transfer and receive Buffer */ |
| 128 | setbits_8(&ss->regs->spbfcr, SPBFCR_TXRST|SPBFCR_RXRST); |
| 129 | |
| 130 | /* Clear transfer and receive Buffer control bit */ |
| 131 | clrbits_8(&ss->regs->spbfcr, SPBFCR_TXRST|SPBFCR_RXRST); |
| 132 | |
| 133 | /* Set equence control method. Use equence0 only */ |
| 134 | writeb(0x00, &ss->regs->spscr); |
| 135 | |
| 136 | /* Enable SPI function */ |
| 137 | setbits_8(&ss->regs->spcr, SPCR_SPE); |
| 138 | } |
| 139 | |
Marek Vasut | b3bec25 | 2018-08-25 19:34:24 +0200 | [diff] [blame^] | 140 | static void sh_qspi_cs_deactivate(struct sh_qspi_slave *ss) |
Nobuhiro Iwamatsu | 16f47c9 | 2013-12-18 15:31:55 +0900 | [diff] [blame] | 141 | { |
Nobuhiro Iwamatsu | 16f47c9 | 2013-12-18 15:31:55 +0900 | [diff] [blame] | 142 | /* Disable SPI Function */ |
| 143 | clrbits_8(&ss->regs->spcr, SPCR_SPE); |
| 144 | } |
| 145 | |
Marek Vasut | b3bec25 | 2018-08-25 19:34:24 +0200 | [diff] [blame^] | 146 | static int sh_qspi_xfer_common(struct sh_qspi_slave *ss, unsigned int bitlen, |
| 147 | const void *dout, void *din, unsigned long flags) |
Nobuhiro Iwamatsu | 16f47c9 | 2013-12-18 15:31:55 +0900 | [diff] [blame] | 148 | { |
Marek Vasut | ea5512e | 2018-04-10 16:43:47 +0200 | [diff] [blame] | 149 | u32 nbyte, chunk; |
| 150 | int i, ret = 0; |
Marek Vasut | 0e6fa20 | 2018-04-10 16:47:38 +0200 | [diff] [blame] | 151 | u8 dtdata = 0, drdata; |
| 152 | u8 *tdata = &dtdata, *rdata = &drdata; |
| 153 | u32 *spbmul0 = &ss->regs->spbmul0; |
Nobuhiro Iwamatsu | 16f47c9 | 2013-12-18 15:31:55 +0900 | [diff] [blame] | 154 | |
| 155 | if (dout == NULL && din == NULL) { |
| 156 | if (flags & SPI_XFER_END) |
Marek Vasut | b3bec25 | 2018-08-25 19:34:24 +0200 | [diff] [blame^] | 157 | sh_qspi_cs_deactivate(ss); |
Nobuhiro Iwamatsu | 16f47c9 | 2013-12-18 15:31:55 +0900 | [diff] [blame] | 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | if (bitlen % 8) { |
| 162 | printf("%s: bitlen is not 8bit alined %d", __func__, bitlen); |
| 163 | return 1; |
| 164 | } |
| 165 | |
| 166 | nbyte = bitlen / 8; |
| 167 | |
| 168 | if (flags & SPI_XFER_BEGIN) { |
Marek Vasut | b3bec25 | 2018-08-25 19:34:24 +0200 | [diff] [blame^] | 169 | sh_qspi_cs_activate(ss); |
Nobuhiro Iwamatsu | 16f47c9 | 2013-12-18 15:31:55 +0900 | [diff] [blame] | 170 | |
| 171 | /* Set 1048576 byte */ |
| 172 | writel(0x100000, spbmul0); |
| 173 | } |
| 174 | |
| 175 | if (flags & SPI_XFER_END) |
| 176 | writel(nbyte, spbmul0); |
| 177 | |
| 178 | if (dout != NULL) |
Marek Vasut | 0e6fa20 | 2018-04-10 16:47:38 +0200 | [diff] [blame] | 179 | tdata = (u8 *)dout; |
Nobuhiro Iwamatsu | 16f47c9 | 2013-12-18 15:31:55 +0900 | [diff] [blame] | 180 | |
| 181 | if (din != NULL) |
| 182 | rdata = din; |
| 183 | |
| 184 | while (nbyte > 0) { |
Marek Vasut | ea5512e | 2018-04-10 16:43:47 +0200 | [diff] [blame] | 185 | /* |
| 186 | * Check if there is 32 Byte chunk and if there is, transfer |
| 187 | * it in one burst, otherwise transfer on byte-by-byte basis. |
| 188 | */ |
| 189 | chunk = (nbyte >= 32) ? 32 : 1; |
| 190 | |
| 191 | clrsetbits_8(&ss->regs->spbfcr, SPBFCR_TXTRG | SPBFCR_RXTRG, |
| 192 | chunk == 32 ? SPBFCR_TXTRG | SPBFCR_RXTRG : 0); |
| 193 | |
Marek Vasut | 9573db6 | 2018-04-10 16:58:46 +0200 | [diff] [blame] | 194 | ret = wait_for_bit_8(&ss->regs->spsr, SPSR_SPTEF, |
| 195 | true, 1000, true); |
| 196 | if (ret) |
| 197 | return ret; |
Nobuhiro Iwamatsu | 16f47c9 | 2013-12-18 15:31:55 +0900 | [diff] [blame] | 198 | |
Marek Vasut | ea5512e | 2018-04-10 16:43:47 +0200 | [diff] [blame] | 199 | for (i = 0; i < chunk; i++) { |
| 200 | writeb(*tdata, &ss->regs->spdr); |
| 201 | if (dout != NULL) |
| 202 | tdata++; |
| 203 | } |
Nobuhiro Iwamatsu | 16f47c9 | 2013-12-18 15:31:55 +0900 | [diff] [blame] | 204 | |
Marek Vasut | 9573db6 | 2018-04-10 16:58:46 +0200 | [diff] [blame] | 205 | ret = wait_for_bit_8(&ss->regs->spsr, SPSR_SPRFF, |
| 206 | true, 1000, true); |
| 207 | if (ret) |
| 208 | return ret; |
Nobuhiro Iwamatsu | 16f47c9 | 2013-12-18 15:31:55 +0900 | [diff] [blame] | 209 | |
Marek Vasut | ea5512e | 2018-04-10 16:43:47 +0200 | [diff] [blame] | 210 | for (i = 0; i < chunk; i++) { |
| 211 | *rdata = readb(&ss->regs->spdr); |
| 212 | if (din != NULL) |
| 213 | rdata++; |
| 214 | } |
Nobuhiro Iwamatsu | 16f47c9 | 2013-12-18 15:31:55 +0900 | [diff] [blame] | 215 | |
Marek Vasut | ea5512e | 2018-04-10 16:43:47 +0200 | [diff] [blame] | 216 | nbyte -= chunk; |
Nobuhiro Iwamatsu | 16f47c9 | 2013-12-18 15:31:55 +0900 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | if (flags & SPI_XFER_END) |
Marek Vasut | b3bec25 | 2018-08-25 19:34:24 +0200 | [diff] [blame^] | 220 | sh_qspi_cs_deactivate(ss); |
Nobuhiro Iwamatsu | 16f47c9 | 2013-12-18 15:31:55 +0900 | [diff] [blame] | 221 | |
| 222 | return ret; |
| 223 | } |
Marek Vasut | b3bec25 | 2018-08-25 19:34:24 +0200 | [diff] [blame^] | 224 | |
| 225 | #ifndef CONFIG_DM_SPI |
| 226 | static inline struct sh_qspi_slave *to_sh_qspi(struct spi_slave *slave) |
| 227 | { |
| 228 | return container_of(slave, struct sh_qspi_slave, slave); |
| 229 | } |
| 230 | |
| 231 | int spi_cs_is_valid(unsigned int bus, unsigned int cs) |
| 232 | { |
| 233 | return 1; |
| 234 | } |
| 235 | |
| 236 | void spi_cs_activate(struct spi_slave *slave) |
| 237 | { |
| 238 | struct sh_qspi_slave *ss = to_sh_qspi(slave); |
| 239 | |
| 240 | sh_qspi_cs_activate(ss); |
| 241 | } |
| 242 | |
| 243 | void spi_cs_deactivate(struct spi_slave *slave) |
| 244 | { |
| 245 | struct sh_qspi_slave *ss = to_sh_qspi(slave); |
| 246 | |
| 247 | sh_qspi_cs_deactivate(ss); |
| 248 | } |
| 249 | |
| 250 | void spi_init(void) |
| 251 | { |
| 252 | /* nothing to do */ |
| 253 | } |
| 254 | |
| 255 | struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs, |
| 256 | unsigned int max_hz, unsigned int mode) |
| 257 | { |
| 258 | struct sh_qspi_slave *ss; |
| 259 | |
| 260 | if (!spi_cs_is_valid(bus, cs)) |
| 261 | return NULL; |
| 262 | |
| 263 | ss = spi_alloc_slave(struct sh_qspi_slave, bus, cs); |
| 264 | if (!ss) { |
| 265 | printf("SPI_error: Fail to allocate sh_qspi_slave\n"); |
| 266 | return NULL; |
| 267 | } |
| 268 | |
| 269 | ss->regs = (struct sh_qspi_regs *)SH_QSPI_BASE; |
| 270 | |
| 271 | /* Init SH QSPI */ |
| 272 | sh_qspi_init(ss); |
| 273 | |
| 274 | return &ss->slave; |
| 275 | } |
| 276 | |
| 277 | void spi_free_slave(struct spi_slave *slave) |
| 278 | { |
| 279 | struct sh_qspi_slave *spi = to_sh_qspi(slave); |
| 280 | |
| 281 | free(spi); |
| 282 | } |
| 283 | |
| 284 | int spi_claim_bus(struct spi_slave *slave) |
| 285 | { |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | void spi_release_bus(struct spi_slave *slave) |
| 290 | { |
| 291 | } |
| 292 | |
| 293 | int spi_xfer(struct spi_slave *slave, unsigned int bitlen, |
| 294 | const void *dout, void *din, unsigned long flags) |
| 295 | { |
| 296 | struct sh_qspi_slave *ss = to_sh_qspi(slave); |
| 297 | |
| 298 | return sh_qspi_xfer_common(ss, bitlen, dout, din, flags); |
| 299 | } |
| 300 | |
| 301 | #else |
| 302 | |
| 303 | #include <dm.h> |
| 304 | |
| 305 | static int sh_qspi_xfer(struct udevice *dev, unsigned int bitlen, |
| 306 | const void *dout, void *din, unsigned long flags) |
| 307 | { |
| 308 | struct udevice *bus = dev->parent; |
| 309 | struct sh_qspi_slave *ss = dev_get_platdata(bus); |
| 310 | |
| 311 | return sh_qspi_xfer_common(ss, bitlen, dout, din, flags); |
| 312 | } |
| 313 | |
| 314 | static int sh_qspi_set_speed(struct udevice *dev, uint speed) |
| 315 | { |
| 316 | /* This is a SPI NOR controller, do nothing. */ |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | static int sh_qspi_set_mode(struct udevice *dev, uint mode) |
| 321 | { |
| 322 | /* This is a SPI NOR controller, do nothing. */ |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | static int sh_qspi_probe(struct udevice *dev) |
| 327 | { |
| 328 | struct sh_qspi_slave *ss = dev_get_platdata(dev); |
| 329 | |
| 330 | sh_qspi_init(ss); |
| 331 | |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | static int sh_qspi_ofdata_to_platdata(struct udevice *dev) |
| 336 | { |
| 337 | struct sh_qspi_slave *plat = dev_get_platdata(dev); |
| 338 | |
| 339 | plat->regs = (struct sh_qspi_regs *)dev_read_addr(dev); |
| 340 | |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | static const struct dm_spi_ops sh_qspi_ops = { |
| 345 | .xfer = sh_qspi_xfer, |
| 346 | .set_speed = sh_qspi_set_speed, |
| 347 | .set_mode = sh_qspi_set_mode, |
| 348 | }; |
| 349 | |
| 350 | static const struct udevice_id sh_qspi_ids[] = { |
| 351 | { .compatible = "renesas,qspi" }, |
| 352 | { } |
| 353 | }; |
| 354 | |
| 355 | U_BOOT_DRIVER(sh_qspi) = { |
| 356 | .name = "sh_qspi", |
| 357 | .id = UCLASS_SPI, |
| 358 | .of_match = sh_qspi_ids, |
| 359 | .ops = &sh_qspi_ops, |
| 360 | .ofdata_to_platdata = sh_qspi_ofdata_to_platdata, |
| 361 | .platdata_auto_alloc_size = sizeof(struct sh_qspi_slave), |
| 362 | .probe = sh_qspi_probe, |
| 363 | }; |
| 364 | #endif |