Tudor Ambarus | 88151bb | 2019-06-18 08:51:50 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Driver for Atmel QSPI Controller |
| 4 | * |
| 5 | * Copyright (C) 2015 Atmel Corporation |
| 6 | * Copyright (C) 2018 Cryptera A/S |
| 7 | * |
| 8 | * Author: Cyrille Pitchen <cyrille.pitchen@atmel.com> |
| 9 | * Author: Piotr Bugalski <bugalski.piotr@gmail.com> |
| 10 | */ |
| 11 | |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 12 | #include <malloc.h> |
Tudor Ambarus | 88151bb | 2019-06-18 08:51:50 +0000 | [diff] [blame] | 13 | #include <asm/io.h> |
| 14 | #include <clk.h> |
| 15 | #include <common.h> |
| 16 | #include <dm.h> |
| 17 | #include <errno.h> |
| 18 | #include <fdtdec.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 19 | #include <dm/device_compat.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 20 | #include <linux/err.h> |
Tudor Ambarus | 88151bb | 2019-06-18 08:51:50 +0000 | [diff] [blame] | 21 | #include <linux/io.h> |
| 22 | #include <linux/iopoll.h> |
| 23 | #include <linux/ioport.h> |
| 24 | #include <mach/clk.h> |
| 25 | #include <spi.h> |
| 26 | #include <spi-mem.h> |
| 27 | |
| 28 | /* QSPI register offsets */ |
| 29 | #define QSPI_CR 0x0000 /* Control Register */ |
| 30 | #define QSPI_MR 0x0004 /* Mode Register */ |
| 31 | #define QSPI_RD 0x0008 /* Receive Data Register */ |
| 32 | #define QSPI_TD 0x000c /* Transmit Data Register */ |
| 33 | #define QSPI_SR 0x0010 /* Status Register */ |
| 34 | #define QSPI_IER 0x0014 /* Interrupt Enable Register */ |
| 35 | #define QSPI_IDR 0x0018 /* Interrupt Disable Register */ |
| 36 | #define QSPI_IMR 0x001c /* Interrupt Mask Register */ |
| 37 | #define QSPI_SCR 0x0020 /* Serial Clock Register */ |
| 38 | |
| 39 | #define QSPI_IAR 0x0030 /* Instruction Address Register */ |
| 40 | #define QSPI_ICR 0x0034 /* Instruction Code Register */ |
| 41 | #define QSPI_WICR 0x0034 /* Write Instruction Code Register */ |
| 42 | #define QSPI_IFR 0x0038 /* Instruction Frame Register */ |
| 43 | #define QSPI_RICR 0x003C /* Read Instruction Code Register */ |
| 44 | |
| 45 | #define QSPI_SMR 0x0040 /* Scrambling Mode Register */ |
| 46 | #define QSPI_SKR 0x0044 /* Scrambling Key Register */ |
| 47 | |
| 48 | #define QSPI_WPMR 0x00E4 /* Write Protection Mode Register */ |
| 49 | #define QSPI_WPSR 0x00E8 /* Write Protection Status Register */ |
| 50 | |
| 51 | #define QSPI_VERSION 0x00FC /* Version Register */ |
| 52 | |
| 53 | /* Bitfields in QSPI_CR (Control Register) */ |
| 54 | #define QSPI_CR_QSPIEN BIT(0) |
| 55 | #define QSPI_CR_QSPIDIS BIT(1) |
| 56 | #define QSPI_CR_SWRST BIT(7) |
| 57 | #define QSPI_CR_LASTXFER BIT(24) |
| 58 | |
| 59 | /* Bitfields in QSPI_MR (Mode Register) */ |
| 60 | #define QSPI_MR_SMM BIT(0) |
| 61 | #define QSPI_MR_LLB BIT(1) |
| 62 | #define QSPI_MR_WDRBT BIT(2) |
| 63 | #define QSPI_MR_SMRM BIT(3) |
| 64 | #define QSPI_MR_CSMODE_MASK GENMASK(5, 4) |
| 65 | #define QSPI_MR_CSMODE_NOT_RELOADED (0 << 4) |
| 66 | #define QSPI_MR_CSMODE_LASTXFER (1 << 4) |
| 67 | #define QSPI_MR_CSMODE_SYSTEMATICALLY (2 << 4) |
| 68 | #define QSPI_MR_NBBITS_MASK GENMASK(11, 8) |
| 69 | #define QSPI_MR_NBBITS(n) ((((n) - 8) << 8) & QSPI_MR_NBBITS_MASK) |
| 70 | #define QSPI_MR_DLYBCT_MASK GENMASK(23, 16) |
| 71 | #define QSPI_MR_DLYBCT(n) (((n) << 16) & QSPI_MR_DLYBCT_MASK) |
| 72 | #define QSPI_MR_DLYCS_MASK GENMASK(31, 24) |
| 73 | #define QSPI_MR_DLYCS(n) (((n) << 24) & QSPI_MR_DLYCS_MASK) |
| 74 | |
| 75 | /* Bitfields in QSPI_SR/QSPI_IER/QSPI_IDR/QSPI_IMR */ |
| 76 | #define QSPI_SR_RDRF BIT(0) |
| 77 | #define QSPI_SR_TDRE BIT(1) |
| 78 | #define QSPI_SR_TXEMPTY BIT(2) |
| 79 | #define QSPI_SR_OVRES BIT(3) |
| 80 | #define QSPI_SR_CSR BIT(8) |
| 81 | #define QSPI_SR_CSS BIT(9) |
| 82 | #define QSPI_SR_INSTRE BIT(10) |
| 83 | #define QSPI_SR_QSPIENS BIT(24) |
| 84 | |
| 85 | #define QSPI_SR_CMD_COMPLETED (QSPI_SR_INSTRE | QSPI_SR_CSR) |
| 86 | |
| 87 | /* Bitfields in QSPI_SCR (Serial Clock Register) */ |
| 88 | #define QSPI_SCR_CPOL BIT(0) |
| 89 | #define QSPI_SCR_CPHA BIT(1) |
| 90 | #define QSPI_SCR_SCBR_MASK GENMASK(15, 8) |
| 91 | #define QSPI_SCR_SCBR(n) (((n) << 8) & QSPI_SCR_SCBR_MASK) |
| 92 | #define QSPI_SCR_DLYBS_MASK GENMASK(23, 16) |
| 93 | #define QSPI_SCR_DLYBS(n) (((n) << 16) & QSPI_SCR_DLYBS_MASK) |
| 94 | |
| 95 | /* Bitfields in QSPI_ICR (Read/Write Instruction Code Register) */ |
| 96 | #define QSPI_ICR_INST_MASK GENMASK(7, 0) |
| 97 | #define QSPI_ICR_INST(inst) (((inst) << 0) & QSPI_ICR_INST_MASK) |
| 98 | #define QSPI_ICR_OPT_MASK GENMASK(23, 16) |
| 99 | #define QSPI_ICR_OPT(opt) (((opt) << 16) & QSPI_ICR_OPT_MASK) |
| 100 | |
| 101 | /* Bitfields in QSPI_IFR (Instruction Frame Register) */ |
| 102 | #define QSPI_IFR_WIDTH_MASK GENMASK(2, 0) |
| 103 | #define QSPI_IFR_WIDTH_SINGLE_BIT_SPI (0 << 0) |
| 104 | #define QSPI_IFR_WIDTH_DUAL_OUTPUT (1 << 0) |
| 105 | #define QSPI_IFR_WIDTH_QUAD_OUTPUT (2 << 0) |
| 106 | #define QSPI_IFR_WIDTH_DUAL_IO (3 << 0) |
| 107 | #define QSPI_IFR_WIDTH_QUAD_IO (4 << 0) |
| 108 | #define QSPI_IFR_WIDTH_DUAL_CMD (5 << 0) |
| 109 | #define QSPI_IFR_WIDTH_QUAD_CMD (6 << 0) |
| 110 | #define QSPI_IFR_INSTEN BIT(4) |
| 111 | #define QSPI_IFR_ADDREN BIT(5) |
| 112 | #define QSPI_IFR_OPTEN BIT(6) |
| 113 | #define QSPI_IFR_DATAEN BIT(7) |
| 114 | #define QSPI_IFR_OPTL_MASK GENMASK(9, 8) |
| 115 | #define QSPI_IFR_OPTL_1BIT (0 << 8) |
| 116 | #define QSPI_IFR_OPTL_2BIT (1 << 8) |
| 117 | #define QSPI_IFR_OPTL_4BIT (2 << 8) |
| 118 | #define QSPI_IFR_OPTL_8BIT (3 << 8) |
| 119 | #define QSPI_IFR_ADDRL BIT(10) |
| 120 | #define QSPI_IFR_TFRTYP_MEM BIT(12) |
| 121 | #define QSPI_IFR_SAMA5D2_WRITE_TRSFR BIT(13) |
| 122 | #define QSPI_IFR_CRM BIT(14) |
| 123 | #define QSPI_IFR_NBDUM_MASK GENMASK(20, 16) |
| 124 | #define QSPI_IFR_NBDUM(n) (((n) << 16) & QSPI_IFR_NBDUM_MASK) |
| 125 | #define QSPI_IFR_APBTFRTYP_READ BIT(24) /* Defined in SAM9X60 */ |
| 126 | |
| 127 | /* Bitfields in QSPI_SMR (Scrambling Mode Register) */ |
| 128 | #define QSPI_SMR_SCREN BIT(0) |
| 129 | #define QSPI_SMR_RVDIS BIT(1) |
| 130 | |
| 131 | /* Bitfields in QSPI_WPMR (Write Protection Mode Register) */ |
| 132 | #define QSPI_WPMR_WPEN BIT(0) |
| 133 | #define QSPI_WPMR_WPKEY_MASK GENMASK(31, 8) |
| 134 | #define QSPI_WPMR_WPKEY(wpkey) (((wpkey) << 8) & QSPI_WPMR_WPKEY_MASK) |
| 135 | |
| 136 | /* Bitfields in QSPI_WPSR (Write Protection Status Register) */ |
| 137 | #define QSPI_WPSR_WPVS BIT(0) |
| 138 | #define QSPI_WPSR_WPVSRC_MASK GENMASK(15, 8) |
| 139 | #define QSPI_WPSR_WPVSRC(src) (((src) << 8) & QSPI_WPSR_WPVSRC) |
| 140 | |
| 141 | struct atmel_qspi_caps { |
| 142 | bool has_qspick; |
| 143 | bool has_ricr; |
| 144 | }; |
| 145 | |
| 146 | struct atmel_qspi { |
| 147 | void __iomem *regs; |
| 148 | void __iomem *mem; |
Tudor Ambarus | 678b893 | 2020-03-20 09:37:59 +0000 | [diff] [blame^] | 149 | resource_size_t mmap_size; |
Tudor Ambarus | 88151bb | 2019-06-18 08:51:50 +0000 | [diff] [blame] | 150 | const struct atmel_qspi_caps *caps; |
| 151 | ulong bus_clk_rate; |
| 152 | u32 mr; |
| 153 | }; |
| 154 | |
| 155 | struct atmel_qspi_mode { |
| 156 | u8 cmd_buswidth; |
| 157 | u8 addr_buswidth; |
| 158 | u8 data_buswidth; |
| 159 | u32 config; |
| 160 | }; |
| 161 | |
| 162 | static const struct atmel_qspi_mode atmel_qspi_modes[] = { |
| 163 | { 1, 1, 1, QSPI_IFR_WIDTH_SINGLE_BIT_SPI }, |
| 164 | { 1, 1, 2, QSPI_IFR_WIDTH_DUAL_OUTPUT }, |
| 165 | { 1, 1, 4, QSPI_IFR_WIDTH_QUAD_OUTPUT }, |
| 166 | { 1, 2, 2, QSPI_IFR_WIDTH_DUAL_IO }, |
| 167 | { 1, 4, 4, QSPI_IFR_WIDTH_QUAD_IO }, |
| 168 | { 2, 2, 2, QSPI_IFR_WIDTH_DUAL_CMD }, |
| 169 | { 4, 4, 4, QSPI_IFR_WIDTH_QUAD_CMD }, |
| 170 | }; |
| 171 | |
| 172 | static inline bool atmel_qspi_is_compatible(const struct spi_mem_op *op, |
| 173 | const struct atmel_qspi_mode *mode) |
| 174 | { |
| 175 | if (op->cmd.buswidth != mode->cmd_buswidth) |
| 176 | return false; |
| 177 | |
| 178 | if (op->addr.nbytes && op->addr.buswidth != mode->addr_buswidth) |
| 179 | return false; |
| 180 | |
| 181 | if (op->data.nbytes && op->data.buswidth != mode->data_buswidth) |
| 182 | return false; |
| 183 | |
| 184 | return true; |
| 185 | } |
| 186 | |
| 187 | static int atmel_qspi_find_mode(const struct spi_mem_op *op) |
| 188 | { |
| 189 | u32 i; |
| 190 | |
| 191 | for (i = 0; i < ARRAY_SIZE(atmel_qspi_modes); i++) |
| 192 | if (atmel_qspi_is_compatible(op, &atmel_qspi_modes[i])) |
| 193 | return i; |
| 194 | |
| 195 | return -ENOTSUPP; |
| 196 | } |
| 197 | |
| 198 | static bool atmel_qspi_supports_op(struct spi_slave *slave, |
| 199 | const struct spi_mem_op *op) |
| 200 | { |
| 201 | if (atmel_qspi_find_mode(op) < 0) |
| 202 | return false; |
| 203 | |
| 204 | /* special case not supported by hardware */ |
| 205 | if (op->addr.nbytes == 2 && op->cmd.buswidth != op->addr.buswidth && |
| 206 | op->dummy.nbytes == 0) |
| 207 | return false; |
| 208 | |
| 209 | return true; |
| 210 | } |
| 211 | |
| 212 | static int atmel_qspi_set_cfg(struct atmel_qspi *aq, |
| 213 | const struct spi_mem_op *op, u32 *offset) |
| 214 | { |
| 215 | u32 iar, icr, ifr; |
| 216 | u32 dummy_cycles = 0; |
| 217 | int mode; |
| 218 | |
| 219 | iar = 0; |
| 220 | icr = QSPI_ICR_INST(op->cmd.opcode); |
| 221 | ifr = QSPI_IFR_INSTEN; |
| 222 | |
| 223 | mode = atmel_qspi_find_mode(op); |
| 224 | if (mode < 0) |
| 225 | return mode; |
| 226 | ifr |= atmel_qspi_modes[mode].config; |
| 227 | |
| 228 | if (op->dummy.buswidth && op->dummy.nbytes) |
| 229 | dummy_cycles = op->dummy.nbytes * 8 / op->dummy.buswidth; |
| 230 | |
| 231 | /* |
| 232 | * The controller allows 24 and 32-bit addressing while NAND-flash |
| 233 | * requires 16-bit long. Handling 8-bit long addresses is done using |
| 234 | * the option field. For the 16-bit addresses, the workaround depends |
| 235 | * of the number of requested dummy bits. If there are 8 or more dummy |
| 236 | * cycles, the address is shifted and sent with the first dummy byte. |
| 237 | * Otherwise opcode is disabled and the first byte of the address |
| 238 | * contains the command opcode (works only if the opcode and address |
| 239 | * use the same buswidth). The limitation is when the 16-bit address is |
| 240 | * used without enough dummy cycles and the opcode is using a different |
| 241 | * buswidth than the address. |
| 242 | */ |
| 243 | if (op->addr.buswidth) { |
| 244 | switch (op->addr.nbytes) { |
| 245 | case 0: |
| 246 | break; |
| 247 | case 1: |
| 248 | ifr |= QSPI_IFR_OPTEN | QSPI_IFR_OPTL_8BIT; |
| 249 | icr |= QSPI_ICR_OPT(op->addr.val & 0xff); |
| 250 | break; |
| 251 | case 2: |
| 252 | if (dummy_cycles < 8 / op->addr.buswidth) { |
| 253 | ifr &= ~QSPI_IFR_INSTEN; |
| 254 | ifr |= QSPI_IFR_ADDREN; |
| 255 | iar = (op->cmd.opcode << 16) | |
| 256 | (op->addr.val & 0xffff); |
| 257 | } else { |
| 258 | ifr |= QSPI_IFR_ADDREN; |
| 259 | iar = (op->addr.val << 8) & 0xffffff; |
| 260 | dummy_cycles -= 8 / op->addr.buswidth; |
| 261 | } |
| 262 | break; |
| 263 | case 3: |
| 264 | ifr |= QSPI_IFR_ADDREN; |
| 265 | iar = op->addr.val & 0xffffff; |
| 266 | break; |
| 267 | case 4: |
| 268 | ifr |= QSPI_IFR_ADDREN | QSPI_IFR_ADDRL; |
| 269 | iar = op->addr.val & 0x7ffffff; |
| 270 | break; |
| 271 | default: |
| 272 | return -ENOTSUPP; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | /* offset of the data access in the QSPI memory space */ |
| 277 | *offset = iar; |
| 278 | |
| 279 | /* Set number of dummy cycles */ |
| 280 | if (dummy_cycles) |
| 281 | ifr |= QSPI_IFR_NBDUM(dummy_cycles); |
| 282 | |
| 283 | /* Set data enable */ |
| 284 | if (op->data.nbytes) |
| 285 | ifr |= QSPI_IFR_DATAEN; |
| 286 | |
| 287 | /* |
| 288 | * If the QSPI controller is set in regular SPI mode, set it in |
| 289 | * Serial Memory Mode (SMM). |
| 290 | */ |
| 291 | if (aq->mr != QSPI_MR_SMM) { |
| 292 | writel(QSPI_MR_SMM, aq->regs + QSPI_MR); |
| 293 | aq->mr = QSPI_MR_SMM; |
| 294 | } |
| 295 | |
| 296 | /* Clear pending interrupts */ |
| 297 | (void)readl(aq->regs + QSPI_SR); |
| 298 | |
| 299 | if (aq->caps->has_ricr) { |
| 300 | if (!op->addr.nbytes && op->data.dir == SPI_MEM_DATA_IN) |
| 301 | ifr |= QSPI_IFR_APBTFRTYP_READ; |
| 302 | |
| 303 | /* Set QSPI Instruction Frame registers */ |
| 304 | writel(iar, aq->regs + QSPI_IAR); |
| 305 | if (op->data.dir == SPI_MEM_DATA_IN) |
| 306 | writel(icr, aq->regs + QSPI_RICR); |
| 307 | else |
| 308 | writel(icr, aq->regs + QSPI_WICR); |
| 309 | writel(ifr, aq->regs + QSPI_IFR); |
| 310 | } else { |
| 311 | if (op->data.dir == SPI_MEM_DATA_OUT) |
| 312 | ifr |= QSPI_IFR_SAMA5D2_WRITE_TRSFR; |
| 313 | |
| 314 | /* Set QSPI Instruction Frame registers */ |
| 315 | writel(iar, aq->regs + QSPI_IAR); |
| 316 | writel(icr, aq->regs + QSPI_ICR); |
| 317 | writel(ifr, aq->regs + QSPI_IFR); |
| 318 | } |
| 319 | |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | static int atmel_qspi_exec_op(struct spi_slave *slave, |
| 324 | const struct spi_mem_op *op) |
| 325 | { |
| 326 | struct atmel_qspi *aq = dev_get_priv(slave->dev->parent); |
| 327 | u32 sr, imr, offset; |
| 328 | int err; |
| 329 | |
Tudor Ambarus | 678b893 | 2020-03-20 09:37:59 +0000 | [diff] [blame^] | 330 | /* |
| 331 | * Check if the address exceeds the MMIO window size. An improvement |
| 332 | * would be to add support for regular SPI mode and fall back to it |
| 333 | * when the flash memories overrun the controller's memory space. |
| 334 | */ |
| 335 | if (op->addr.val + op->data.nbytes > aq->mmap_size) |
| 336 | return -ENOTSUPP; |
| 337 | |
Tudor Ambarus | 88151bb | 2019-06-18 08:51:50 +0000 | [diff] [blame] | 338 | err = atmel_qspi_set_cfg(aq, op, &offset); |
| 339 | if (err) |
| 340 | return err; |
| 341 | |
| 342 | /* Skip to the final steps if there is no data */ |
| 343 | if (op->data.nbytes) { |
| 344 | /* Dummy read of QSPI_IFR to synchronize APB and AHB accesses */ |
| 345 | (void)readl(aq->regs + QSPI_IFR); |
| 346 | |
| 347 | /* Send/Receive data */ |
| 348 | if (op->data.dir == SPI_MEM_DATA_IN) |
| 349 | memcpy_fromio(op->data.buf.in, aq->mem + offset, |
| 350 | op->data.nbytes); |
| 351 | else |
| 352 | memcpy_toio(aq->mem + offset, op->data.buf.out, |
| 353 | op->data.nbytes); |
| 354 | |
| 355 | /* Release the chip-select */ |
| 356 | writel(QSPI_CR_LASTXFER, aq->regs + QSPI_CR); |
| 357 | } |
| 358 | |
| 359 | /* Poll INSTruction End and Chip Select Rise flags. */ |
| 360 | imr = QSPI_SR_INSTRE | QSPI_SR_CSR; |
| 361 | return readl_poll_timeout(aq->regs + QSPI_SR, sr, (sr & imr) == imr, |
| 362 | 1000000); |
| 363 | } |
| 364 | |
| 365 | static int atmel_qspi_set_speed(struct udevice *bus, uint hz) |
| 366 | { |
| 367 | struct atmel_qspi *aq = dev_get_priv(bus); |
| 368 | u32 scr, scbr, mask, new_value; |
| 369 | |
| 370 | /* Compute the QSPI baudrate */ |
| 371 | scbr = DIV_ROUND_UP(aq->bus_clk_rate, hz); |
| 372 | if (scbr > 0) |
| 373 | scbr--; |
| 374 | |
| 375 | new_value = QSPI_SCR_SCBR(scbr); |
| 376 | mask = QSPI_SCR_SCBR_MASK; |
| 377 | |
| 378 | scr = readl(aq->regs + QSPI_SCR); |
| 379 | if ((scr & mask) == new_value) |
| 380 | return 0; |
| 381 | |
| 382 | scr = (scr & ~mask) | new_value; |
| 383 | writel(scr, aq->regs + QSPI_SCR); |
| 384 | |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | static int atmel_qspi_set_mode(struct udevice *bus, uint mode) |
| 389 | { |
| 390 | struct atmel_qspi *aq = dev_get_priv(bus); |
| 391 | u32 scr, mask, new_value = 0; |
| 392 | |
| 393 | if (mode & SPI_CPOL) |
| 394 | new_value = QSPI_SCR_CPOL; |
| 395 | if (mode & SPI_CPHA) |
| 396 | new_value = QSPI_SCR_CPHA; |
| 397 | |
| 398 | mask = QSPI_SCR_CPOL | QSPI_SCR_CPHA; |
| 399 | |
| 400 | scr = readl(aq->regs + QSPI_SCR); |
| 401 | if ((scr & mask) == new_value) |
| 402 | return 0; |
| 403 | |
| 404 | scr = (scr & ~mask) | new_value; |
| 405 | writel(scr, aq->regs + QSPI_SCR); |
| 406 | |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | static int atmel_qspi_enable_clk(struct udevice *dev) |
| 411 | { |
| 412 | struct atmel_qspi *aq = dev_get_priv(dev); |
| 413 | struct clk pclk, qspick; |
| 414 | int ret; |
| 415 | |
| 416 | ret = clk_get_by_name(dev, "pclk", &pclk); |
| 417 | if (ret) |
| 418 | ret = clk_get_by_index(dev, 0, &pclk); |
| 419 | |
| 420 | if (ret) { |
| 421 | dev_err(dev, "Missing QSPI peripheral clock\n"); |
| 422 | return ret; |
| 423 | } |
| 424 | |
| 425 | ret = clk_enable(&pclk); |
| 426 | if (ret) { |
| 427 | dev_err(dev, "Failed to enable QSPI peripheral clock\n"); |
| 428 | goto free_pclk; |
| 429 | } |
| 430 | |
| 431 | if (aq->caps->has_qspick) { |
| 432 | /* Get the QSPI system clock */ |
| 433 | ret = clk_get_by_name(dev, "qspick", &qspick); |
| 434 | if (ret) { |
| 435 | dev_err(dev, "Missing QSPI peripheral clock\n"); |
| 436 | goto free_pclk; |
| 437 | } |
| 438 | |
| 439 | ret = clk_enable(&qspick); |
| 440 | if (ret) |
| 441 | dev_err(dev, "Failed to enable QSPI system clock\n"); |
| 442 | clk_free(&qspick); |
| 443 | } |
| 444 | |
| 445 | aq->bus_clk_rate = clk_get_rate(&pclk); |
| 446 | if (!aq->bus_clk_rate) |
| 447 | ret = -EINVAL; |
| 448 | |
| 449 | free_pclk: |
| 450 | clk_free(&pclk); |
| 451 | |
| 452 | return ret; |
| 453 | } |
| 454 | |
| 455 | static void atmel_qspi_init(struct atmel_qspi *aq) |
| 456 | { |
| 457 | /* Reset the QSPI controller */ |
| 458 | writel(QSPI_CR_SWRST, aq->regs + QSPI_CR); |
| 459 | |
| 460 | /* Set the QSPI controller by default in Serial Memory Mode */ |
| 461 | writel(QSPI_MR_SMM, aq->regs + QSPI_MR); |
| 462 | aq->mr = QSPI_MR_SMM; |
| 463 | |
| 464 | /* Enable the QSPI controller */ |
| 465 | writel(QSPI_CR_QSPIEN, aq->regs + QSPI_CR); |
| 466 | } |
| 467 | |
| 468 | static int atmel_qspi_probe(struct udevice *dev) |
| 469 | { |
| 470 | struct atmel_qspi *aq = dev_get_priv(dev); |
| 471 | struct resource res; |
| 472 | int ret; |
| 473 | |
| 474 | aq->caps = (struct atmel_qspi_caps *)dev_get_driver_data(dev); |
| 475 | if (!aq->caps) { |
| 476 | dev_err(dev, "Could not retrieve QSPI caps\n"); |
| 477 | return -EINVAL; |
| 478 | }; |
| 479 | |
| 480 | /* Map the registers */ |
| 481 | ret = dev_read_resource_byname(dev, "qspi_base", &res); |
| 482 | if (ret) { |
| 483 | dev_err(dev, "missing registers\n"); |
| 484 | return ret; |
| 485 | } |
| 486 | |
| 487 | aq->regs = devm_ioremap(dev, res.start, resource_size(&res)); |
| 488 | if (IS_ERR(aq->regs)) |
| 489 | return PTR_ERR(aq->regs); |
| 490 | |
| 491 | /* Map the AHB memory */ |
| 492 | ret = dev_read_resource_byname(dev, "qspi_mmap", &res); |
| 493 | if (ret) { |
| 494 | dev_err(dev, "missing AHB memory\n"); |
| 495 | return ret; |
| 496 | } |
| 497 | |
| 498 | aq->mem = devm_ioremap(dev, res.start, resource_size(&res)); |
| 499 | if (IS_ERR(aq->mem)) |
| 500 | return PTR_ERR(aq->mem); |
| 501 | |
Tudor Ambarus | 678b893 | 2020-03-20 09:37:59 +0000 | [diff] [blame^] | 502 | aq->mmap_size = resource_size(&res); |
| 503 | |
Tudor Ambarus | 88151bb | 2019-06-18 08:51:50 +0000 | [diff] [blame] | 504 | ret = atmel_qspi_enable_clk(dev); |
| 505 | if (ret) |
| 506 | return ret; |
| 507 | |
| 508 | atmel_qspi_init(aq); |
| 509 | |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | static const struct spi_controller_mem_ops atmel_qspi_mem_ops = { |
| 514 | .supports_op = atmel_qspi_supports_op, |
| 515 | .exec_op = atmel_qspi_exec_op, |
| 516 | }; |
| 517 | |
| 518 | static const struct dm_spi_ops atmel_qspi_ops = { |
| 519 | .set_speed = atmel_qspi_set_speed, |
| 520 | .set_mode = atmel_qspi_set_mode, |
| 521 | .mem_ops = &atmel_qspi_mem_ops, |
| 522 | }; |
| 523 | |
| 524 | static const struct atmel_qspi_caps atmel_sama5d2_qspi_caps = {}; |
| 525 | |
| 526 | static const struct atmel_qspi_caps atmel_sam9x60_qspi_caps = { |
| 527 | .has_qspick = true, |
| 528 | .has_ricr = true, |
| 529 | }; |
| 530 | |
| 531 | static const struct udevice_id atmel_qspi_ids[] = { |
| 532 | { |
| 533 | .compatible = "atmel,sama5d2-qspi", |
| 534 | .data = (ulong)&atmel_sama5d2_qspi_caps, |
| 535 | }, |
| 536 | { |
| 537 | .compatible = "microchip,sam9x60-qspi", |
| 538 | .data = (ulong)&atmel_sam9x60_qspi_caps, |
| 539 | }, |
| 540 | { /* sentinel */ } |
| 541 | }; |
| 542 | |
| 543 | U_BOOT_DRIVER(atmel_qspi) = { |
| 544 | .name = "atmel_qspi", |
| 545 | .id = UCLASS_SPI, |
| 546 | .of_match = atmel_qspi_ids, |
| 547 | .ops = &atmel_qspi_ops, |
| 548 | .priv_auto_alloc_size = sizeof(struct atmel_qspi), |
| 549 | .probe = atmel_qspi_probe, |
| 550 | }; |