Tudor Ambarus | cae417b | 2019-02-05 17:33:22 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Driver for Atmel QSPI Controller |
| 4 | * |
| 5 | * Copyright (C) 2015 Atmel Corporation |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 6 | * Copyright (C) 2018 Cryptera A/S |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 7 | * |
| 8 | * Author: Cyrille Pitchen <cyrille.pitchen@atmel.com> |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 9 | * Author: Piotr Bugalski <bugalski.piotr@gmail.com> |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 10 | * |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 11 | * This driver is based on drivers/mtd/spi-nor/fsl-quadspi.c from Freescale. |
| 12 | */ |
| 13 | |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 14 | #include <linux/clk.h> |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 15 | #include <linux/delay.h> |
| 16 | #include <linux/err.h> |
| 17 | #include <linux/interrupt.h> |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 18 | #include <linux/io.h> |
Tudor Ambarus | 3ae012e | 2019-02-05 17:33:08 +0000 | [diff] [blame] | 19 | #include <linux/kernel.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/of.h> |
| 22 | #include <linux/platform_device.h> |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 23 | #include <linux/spi/spi-mem.h> |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 24 | |
| 25 | /* QSPI register offsets */ |
| 26 | #define QSPI_CR 0x0000 /* Control Register */ |
| 27 | #define QSPI_MR 0x0004 /* Mode Register */ |
| 28 | #define QSPI_RD 0x0008 /* Receive Data Register */ |
| 29 | #define QSPI_TD 0x000c /* Transmit Data Register */ |
| 30 | #define QSPI_SR 0x0010 /* Status Register */ |
| 31 | #define QSPI_IER 0x0014 /* Interrupt Enable Register */ |
| 32 | #define QSPI_IDR 0x0018 /* Interrupt Disable Register */ |
| 33 | #define QSPI_IMR 0x001c /* Interrupt Mask Register */ |
| 34 | #define QSPI_SCR 0x0020 /* Serial Clock Register */ |
| 35 | |
| 36 | #define QSPI_IAR 0x0030 /* Instruction Address Register */ |
| 37 | #define QSPI_ICR 0x0034 /* Instruction Code Register */ |
| 38 | #define QSPI_IFR 0x0038 /* Instruction Frame Register */ |
| 39 | |
| 40 | #define QSPI_SMR 0x0040 /* Scrambling Mode Register */ |
| 41 | #define QSPI_SKR 0x0044 /* Scrambling Key Register */ |
| 42 | |
| 43 | #define QSPI_WPMR 0x00E4 /* Write Protection Mode Register */ |
| 44 | #define QSPI_WPSR 0x00E8 /* Write Protection Status Register */ |
| 45 | |
| 46 | #define QSPI_VERSION 0x00FC /* Version Register */ |
| 47 | |
| 48 | |
| 49 | /* Bitfields in QSPI_CR (Control Register) */ |
| 50 | #define QSPI_CR_QSPIEN BIT(0) |
| 51 | #define QSPI_CR_QSPIDIS BIT(1) |
| 52 | #define QSPI_CR_SWRST BIT(7) |
| 53 | #define QSPI_CR_LASTXFER BIT(24) |
| 54 | |
| 55 | /* Bitfields in QSPI_MR (Mode Register) */ |
Piotr Bugalski | b82ab1c | 2018-11-05 11:36:20 +0100 | [diff] [blame] | 56 | #define QSPI_MR_SMM BIT(0) |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 57 | #define QSPI_MR_LLB BIT(1) |
| 58 | #define QSPI_MR_WDRBT BIT(2) |
| 59 | #define QSPI_MR_SMRM BIT(3) |
| 60 | #define QSPI_MR_CSMODE_MASK GENMASK(5, 4) |
| 61 | #define QSPI_MR_CSMODE_NOT_RELOADED (0 << 4) |
| 62 | #define QSPI_MR_CSMODE_LASTXFER (1 << 4) |
| 63 | #define QSPI_MR_CSMODE_SYSTEMATICALLY (2 << 4) |
| 64 | #define QSPI_MR_NBBITS_MASK GENMASK(11, 8) |
| 65 | #define QSPI_MR_NBBITS(n) ((((n) - 8) << 8) & QSPI_MR_NBBITS_MASK) |
| 66 | #define QSPI_MR_DLYBCT_MASK GENMASK(23, 16) |
| 67 | #define QSPI_MR_DLYBCT(n) (((n) << 16) & QSPI_MR_DLYBCT_MASK) |
| 68 | #define QSPI_MR_DLYCS_MASK GENMASK(31, 24) |
| 69 | #define QSPI_MR_DLYCS(n) (((n) << 24) & QSPI_MR_DLYCS_MASK) |
| 70 | |
| 71 | /* Bitfields in QSPI_SR/QSPI_IER/QSPI_IDR/QSPI_IMR */ |
| 72 | #define QSPI_SR_RDRF BIT(0) |
| 73 | #define QSPI_SR_TDRE BIT(1) |
| 74 | #define QSPI_SR_TXEMPTY BIT(2) |
| 75 | #define QSPI_SR_OVRES BIT(3) |
| 76 | #define QSPI_SR_CSR BIT(8) |
| 77 | #define QSPI_SR_CSS BIT(9) |
| 78 | #define QSPI_SR_INSTRE BIT(10) |
| 79 | #define QSPI_SR_QSPIENS BIT(24) |
| 80 | |
| 81 | #define QSPI_SR_CMD_COMPLETED (QSPI_SR_INSTRE | QSPI_SR_CSR) |
| 82 | |
| 83 | /* Bitfields in QSPI_SCR (Serial Clock Register) */ |
| 84 | #define QSPI_SCR_CPOL BIT(0) |
| 85 | #define QSPI_SCR_CPHA BIT(1) |
| 86 | #define QSPI_SCR_SCBR_MASK GENMASK(15, 8) |
| 87 | #define QSPI_SCR_SCBR(n) (((n) << 8) & QSPI_SCR_SCBR_MASK) |
| 88 | #define QSPI_SCR_DLYBS_MASK GENMASK(23, 16) |
| 89 | #define QSPI_SCR_DLYBS(n) (((n) << 16) & QSPI_SCR_DLYBS_MASK) |
| 90 | |
| 91 | /* Bitfields in QSPI_ICR (Instruction Code Register) */ |
| 92 | #define QSPI_ICR_INST_MASK GENMASK(7, 0) |
| 93 | #define QSPI_ICR_INST(inst) (((inst) << 0) & QSPI_ICR_INST_MASK) |
| 94 | #define QSPI_ICR_OPT_MASK GENMASK(23, 16) |
| 95 | #define QSPI_ICR_OPT(opt) (((opt) << 16) & QSPI_ICR_OPT_MASK) |
| 96 | |
| 97 | /* Bitfields in QSPI_IFR (Instruction Frame Register) */ |
| 98 | #define QSPI_IFR_WIDTH_MASK GENMASK(2, 0) |
| 99 | #define QSPI_IFR_WIDTH_SINGLE_BIT_SPI (0 << 0) |
| 100 | #define QSPI_IFR_WIDTH_DUAL_OUTPUT (1 << 0) |
| 101 | #define QSPI_IFR_WIDTH_QUAD_OUTPUT (2 << 0) |
| 102 | #define QSPI_IFR_WIDTH_DUAL_IO (3 << 0) |
| 103 | #define QSPI_IFR_WIDTH_QUAD_IO (4 << 0) |
| 104 | #define QSPI_IFR_WIDTH_DUAL_CMD (5 << 0) |
| 105 | #define QSPI_IFR_WIDTH_QUAD_CMD (6 << 0) |
| 106 | #define QSPI_IFR_INSTEN BIT(4) |
| 107 | #define QSPI_IFR_ADDREN BIT(5) |
| 108 | #define QSPI_IFR_OPTEN BIT(6) |
| 109 | #define QSPI_IFR_DATAEN BIT(7) |
| 110 | #define QSPI_IFR_OPTL_MASK GENMASK(9, 8) |
| 111 | #define QSPI_IFR_OPTL_1BIT (0 << 8) |
| 112 | #define QSPI_IFR_OPTL_2BIT (1 << 8) |
| 113 | #define QSPI_IFR_OPTL_4BIT (2 << 8) |
| 114 | #define QSPI_IFR_OPTL_8BIT (3 << 8) |
| 115 | #define QSPI_IFR_ADDRL BIT(10) |
Tudor Ambarus | b456fd1 | 2019-02-05 17:33:25 +0000 | [diff] [blame] | 116 | #define QSPI_IFR_TFRTYP_MEM BIT(12) |
| 117 | #define QSPI_IFR_SAMA5D2_WRITE_TRSFR BIT(13) |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 118 | #define QSPI_IFR_CRM BIT(14) |
| 119 | #define QSPI_IFR_NBDUM_MASK GENMASK(20, 16) |
| 120 | #define QSPI_IFR_NBDUM(n) (((n) << 16) & QSPI_IFR_NBDUM_MASK) |
| 121 | |
| 122 | /* Bitfields in QSPI_SMR (Scrambling Mode Register) */ |
| 123 | #define QSPI_SMR_SCREN BIT(0) |
| 124 | #define QSPI_SMR_RVDIS BIT(1) |
| 125 | |
| 126 | /* Bitfields in QSPI_WPMR (Write Protection Mode Register) */ |
| 127 | #define QSPI_WPMR_WPEN BIT(0) |
| 128 | #define QSPI_WPMR_WPKEY_MASK GENMASK(31, 8) |
| 129 | #define QSPI_WPMR_WPKEY(wpkey) (((wpkey) << 8) & QSPI_WPMR_WPKEY_MASK) |
| 130 | |
| 131 | /* Bitfields in QSPI_WPSR (Write Protection Status Register) */ |
| 132 | #define QSPI_WPSR_WPVS BIT(0) |
| 133 | #define QSPI_WPSR_WPVSRC_MASK GENMASK(15, 8) |
| 134 | #define QSPI_WPSR_WPVSRC(src) (((src) << 8) & QSPI_WPSR_WPVSRC) |
| 135 | |
| 136 | |
| 137 | struct atmel_qspi { |
| 138 | void __iomem *regs; |
| 139 | void __iomem *mem; |
Tudor Ambarus | bd7905e | 2019-02-05 17:33:33 +0000 | [diff] [blame^] | 140 | struct clk *pclk; |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 141 | struct platform_device *pdev; |
| 142 | u32 pending; |
Tudor Ambarus | 9958c8c | 2019-02-05 17:33:06 +0000 | [diff] [blame] | 143 | u32 mr; |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 144 | struct completion cmd_completion; |
| 145 | }; |
| 146 | |
Tudor Ambarus | 1db6de2 | 2019-02-05 17:33:14 +0000 | [diff] [blame] | 147 | struct atmel_qspi_mode { |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 148 | u8 cmd_buswidth; |
| 149 | u8 addr_buswidth; |
| 150 | u8 data_buswidth; |
| 151 | u32 config; |
| 152 | }; |
| 153 | |
Tudor Ambarus | 1db6de2 | 2019-02-05 17:33:14 +0000 | [diff] [blame] | 154 | static const struct atmel_qspi_mode sama5d2_qspi_modes[] = { |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 155 | { 1, 1, 1, QSPI_IFR_WIDTH_SINGLE_BIT_SPI }, |
| 156 | { 1, 1, 2, QSPI_IFR_WIDTH_DUAL_OUTPUT }, |
| 157 | { 1, 1, 4, QSPI_IFR_WIDTH_QUAD_OUTPUT }, |
| 158 | { 1, 2, 2, QSPI_IFR_WIDTH_DUAL_IO }, |
| 159 | { 1, 4, 4, QSPI_IFR_WIDTH_QUAD_IO }, |
| 160 | { 2, 2, 2, QSPI_IFR_WIDTH_DUAL_CMD }, |
| 161 | { 4, 4, 4, QSPI_IFR_WIDTH_QUAD_CMD }, |
| 162 | }; |
| 163 | |
Tudor Ambarus | 1db6de2 | 2019-02-05 17:33:14 +0000 | [diff] [blame] | 164 | static inline bool atmel_qspi_is_compatible(const struct spi_mem_op *op, |
| 165 | const struct atmel_qspi_mode *mode) |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 166 | { |
| 167 | if (op->cmd.buswidth != mode->cmd_buswidth) |
| 168 | return false; |
| 169 | |
| 170 | if (op->addr.nbytes && op->addr.buswidth != mode->addr_buswidth) |
| 171 | return false; |
| 172 | |
| 173 | if (op->data.nbytes && op->data.buswidth != mode->data_buswidth) |
| 174 | return false; |
| 175 | |
| 176 | return true; |
| 177 | } |
| 178 | |
Tudor Ambarus | 1db6de2 | 2019-02-05 17:33:14 +0000 | [diff] [blame] | 179 | static int atmel_qspi_find_mode(const struct spi_mem_op *op) |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 180 | { |
| 181 | u32 i; |
| 182 | |
| 183 | for (i = 0; i < ARRAY_SIZE(sama5d2_qspi_modes); i++) |
Tudor Ambarus | 1db6de2 | 2019-02-05 17:33:14 +0000 | [diff] [blame] | 184 | if (atmel_qspi_is_compatible(op, &sama5d2_qspi_modes[i])) |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 185 | return i; |
| 186 | |
Tudor Ambarus | 2aaa8dd | 2019-02-05 17:33:19 +0000 | [diff] [blame] | 187 | return -ENOTSUPP; |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | static bool atmel_qspi_supports_op(struct spi_mem *mem, |
| 191 | const struct spi_mem_op *op) |
| 192 | { |
Tudor Ambarus | 1db6de2 | 2019-02-05 17:33:14 +0000 | [diff] [blame] | 193 | if (atmel_qspi_find_mode(op) < 0) |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 194 | return false; |
| 195 | |
| 196 | /* special case not supported by hardware */ |
| 197 | if (op->addr.nbytes == 2 && op->cmd.buswidth != op->addr.buswidth && |
| 198 | op->dummy.nbytes == 0) |
| 199 | return false; |
| 200 | |
| 201 | return true; |
| 202 | } |
| 203 | |
| 204 | static int atmel_qspi_exec_op(struct spi_mem *mem, const struct spi_mem_op *op) |
| 205 | { |
| 206 | struct atmel_qspi *aq = spi_controller_get_devdata(mem->spi->master); |
| 207 | int mode; |
| 208 | u32 dummy_cycles = 0; |
| 209 | u32 iar, icr, ifr, sr; |
| 210 | int err = 0; |
| 211 | |
| 212 | iar = 0; |
| 213 | icr = QSPI_ICR_INST(op->cmd.opcode); |
| 214 | ifr = QSPI_IFR_INSTEN; |
| 215 | |
Tudor Ambarus | 9958c8c | 2019-02-05 17:33:06 +0000 | [diff] [blame] | 216 | /* |
| 217 | * If the QSPI controller is set in regular SPI mode, set it in |
| 218 | * Serial Memory Mode (SMM). |
| 219 | */ |
| 220 | if (aq->mr != QSPI_MR_SMM) { |
Tudor Ambarus | 18b6f6e | 2019-02-05 17:33:11 +0000 | [diff] [blame] | 221 | writel_relaxed(QSPI_MR_SMM, aq->regs + QSPI_MR); |
Tudor Ambarus | 9958c8c | 2019-02-05 17:33:06 +0000 | [diff] [blame] | 222 | aq->mr = QSPI_MR_SMM; |
| 223 | } |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 224 | |
Tudor Ambarus | 1db6de2 | 2019-02-05 17:33:14 +0000 | [diff] [blame] | 225 | mode = atmel_qspi_find_mode(op); |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 226 | if (mode < 0) |
Tudor Ambarus | 2aaa8dd | 2019-02-05 17:33:19 +0000 | [diff] [blame] | 227 | return mode; |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 228 | |
| 229 | ifr |= sama5d2_qspi_modes[mode].config; |
| 230 | |
| 231 | if (op->dummy.buswidth && op->dummy.nbytes) |
| 232 | dummy_cycles = op->dummy.nbytes * 8 / op->dummy.buswidth; |
| 233 | |
| 234 | if (op->addr.buswidth) { |
| 235 | switch (op->addr.nbytes) { |
| 236 | case 0: |
| 237 | break; |
| 238 | case 1: |
| 239 | ifr |= QSPI_IFR_OPTEN | QSPI_IFR_OPTL_8BIT; |
| 240 | icr |= QSPI_ICR_OPT(op->addr.val & 0xff); |
| 241 | break; |
| 242 | case 2: |
| 243 | if (dummy_cycles < 8 / op->addr.buswidth) { |
| 244 | ifr &= ~QSPI_IFR_INSTEN; |
| 245 | ifr |= QSPI_IFR_ADDREN; |
| 246 | iar = (op->cmd.opcode << 16) | |
| 247 | (op->addr.val & 0xffff); |
| 248 | } else { |
| 249 | ifr |= QSPI_IFR_ADDREN; |
| 250 | iar = (op->addr.val << 8) & 0xffffff; |
| 251 | dummy_cycles -= 8 / op->addr.buswidth; |
| 252 | } |
| 253 | break; |
| 254 | case 3: |
| 255 | ifr |= QSPI_IFR_ADDREN; |
| 256 | iar = op->addr.val & 0xffffff; |
| 257 | break; |
| 258 | case 4: |
| 259 | ifr |= QSPI_IFR_ADDREN | QSPI_IFR_ADDRL; |
| 260 | iar = op->addr.val & 0x7ffffff; |
| 261 | break; |
| 262 | default: |
| 263 | return -ENOTSUPP; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /* Set number of dummy cycles */ |
| 268 | if (dummy_cycles) |
| 269 | ifr |= QSPI_IFR_NBDUM(dummy_cycles); |
| 270 | |
| 271 | /* Set data enable */ |
| 272 | if (op->data.nbytes) |
| 273 | ifr |= QSPI_IFR_DATAEN; |
| 274 | |
Tudor Ambarus | b456fd1 | 2019-02-05 17:33:25 +0000 | [diff] [blame] | 275 | if (op->data.dir == SPI_MEM_DATA_OUT) |
| 276 | ifr |= QSPI_IFR_SAMA5D2_WRITE_TRSFR; |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 277 | |
| 278 | /* Clear pending interrupts */ |
Tudor Ambarus | 18b6f6e | 2019-02-05 17:33:11 +0000 | [diff] [blame] | 279 | (void)readl_relaxed(aq->regs + QSPI_SR); |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 280 | |
| 281 | /* Set QSPI Instruction Frame registers */ |
Tudor Ambarus | 18b6f6e | 2019-02-05 17:33:11 +0000 | [diff] [blame] | 282 | writel_relaxed(iar, aq->regs + QSPI_IAR); |
| 283 | writel_relaxed(icr, aq->regs + QSPI_ICR); |
| 284 | writel_relaxed(ifr, aq->regs + QSPI_IFR); |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 285 | |
| 286 | /* Skip to the final steps if there is no data */ |
| 287 | if (op->data.nbytes) { |
| 288 | /* Dummy read of QSPI_IFR to synchronize APB and AHB accesses */ |
Tudor Ambarus | 18b6f6e | 2019-02-05 17:33:11 +0000 | [diff] [blame] | 289 | (void)readl_relaxed(aq->regs + QSPI_IFR); |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 290 | |
| 291 | /* Send/Receive data */ |
| 292 | if (op->data.dir == SPI_MEM_DATA_IN) |
| 293 | _memcpy_fromio(op->data.buf.in, |
| 294 | aq->mem + iar, op->data.nbytes); |
| 295 | else |
| 296 | _memcpy_toio(aq->mem + iar, |
| 297 | op->data.buf.out, op->data.nbytes); |
| 298 | |
| 299 | /* Release the chip-select */ |
Tudor Ambarus | 18b6f6e | 2019-02-05 17:33:11 +0000 | [diff] [blame] | 300 | writel_relaxed(QSPI_CR_LASTXFER, aq->regs + QSPI_CR); |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | /* Poll INSTRuction End status */ |
Tudor Ambarus | 18b6f6e | 2019-02-05 17:33:11 +0000 | [diff] [blame] | 304 | sr = readl_relaxed(aq->regs + QSPI_SR); |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 305 | if ((sr & QSPI_SR_CMD_COMPLETED) == QSPI_SR_CMD_COMPLETED) |
| 306 | return err; |
| 307 | |
| 308 | /* Wait for INSTRuction End interrupt */ |
| 309 | reinit_completion(&aq->cmd_completion); |
| 310 | aq->pending = sr & QSPI_SR_CMD_COMPLETED; |
Tudor Ambarus | 18b6f6e | 2019-02-05 17:33:11 +0000 | [diff] [blame] | 311 | writel_relaxed(QSPI_SR_CMD_COMPLETED, aq->regs + QSPI_IER); |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 312 | if (!wait_for_completion_timeout(&aq->cmd_completion, |
| 313 | msecs_to_jiffies(1000))) |
| 314 | err = -ETIMEDOUT; |
Tudor Ambarus | 18b6f6e | 2019-02-05 17:33:11 +0000 | [diff] [blame] | 315 | writel_relaxed(QSPI_SR_CMD_COMPLETED, aq->regs + QSPI_IDR); |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 316 | |
| 317 | return err; |
| 318 | } |
| 319 | |
| 320 | const char *atmel_qspi_get_name(struct spi_mem *spimem) |
| 321 | { |
| 322 | return dev_name(spimem->spi->dev.parent); |
| 323 | } |
| 324 | |
| 325 | static const struct spi_controller_mem_ops atmel_qspi_mem_ops = { |
| 326 | .supports_op = atmel_qspi_supports_op, |
| 327 | .exec_op = atmel_qspi_exec_op, |
| 328 | .get_name = atmel_qspi_get_name |
| 329 | }; |
| 330 | |
| 331 | static int atmel_qspi_setup(struct spi_device *spi) |
| 332 | { |
| 333 | struct spi_controller *ctrl = spi->master; |
| 334 | struct atmel_qspi *aq = spi_controller_get_devdata(ctrl); |
| 335 | unsigned long src_rate; |
| 336 | u32 scr, scbr; |
| 337 | |
| 338 | if (ctrl->busy) |
| 339 | return -EBUSY; |
| 340 | |
| 341 | if (!spi->max_speed_hz) |
| 342 | return -EINVAL; |
| 343 | |
Tudor Ambarus | bd7905e | 2019-02-05 17:33:33 +0000 | [diff] [blame^] | 344 | src_rate = clk_get_rate(aq->pclk); |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 345 | if (!src_rate) |
| 346 | return -EINVAL; |
| 347 | |
| 348 | /* Compute the QSPI baudrate */ |
| 349 | scbr = DIV_ROUND_UP(src_rate, spi->max_speed_hz); |
| 350 | if (scbr > 0) |
| 351 | scbr--; |
| 352 | |
| 353 | scr = QSPI_SCR_SCBR(scbr); |
Tudor Ambarus | 18b6f6e | 2019-02-05 17:33:11 +0000 | [diff] [blame] | 354 | writel_relaxed(scr, aq->regs + QSPI_SCR); |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 355 | |
| 356 | return 0; |
| 357 | } |
| 358 | |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 359 | static int atmel_qspi_init(struct atmel_qspi *aq) |
| 360 | { |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 361 | /* Reset the QSPI controller */ |
Tudor Ambarus | 18b6f6e | 2019-02-05 17:33:11 +0000 | [diff] [blame] | 362 | writel_relaxed(QSPI_CR_SWRST, aq->regs + QSPI_CR); |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 363 | |
Tudor Ambarus | 9958c8c | 2019-02-05 17:33:06 +0000 | [diff] [blame] | 364 | /* Set the QSPI controller by default in Serial Memory Mode */ |
Tudor Ambarus | 18b6f6e | 2019-02-05 17:33:11 +0000 | [diff] [blame] | 365 | writel_relaxed(QSPI_MR_SMM, aq->regs + QSPI_MR); |
Tudor Ambarus | 9958c8c | 2019-02-05 17:33:06 +0000 | [diff] [blame] | 366 | aq->mr = QSPI_MR_SMM; |
| 367 | |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 368 | /* Enable the QSPI controller */ |
Tudor Ambarus | 18b6f6e | 2019-02-05 17:33:11 +0000 | [diff] [blame] | 369 | writel_relaxed(QSPI_CR_QSPIEN, aq->regs + QSPI_CR); |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 370 | |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | static irqreturn_t atmel_qspi_interrupt(int irq, void *dev_id) |
| 375 | { |
Tudor Ambarus | 9ce4c51 | 2019-02-05 17:33:17 +0000 | [diff] [blame] | 376 | struct atmel_qspi *aq = dev_id; |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 377 | u32 status, mask, pending; |
| 378 | |
Tudor Ambarus | 18b6f6e | 2019-02-05 17:33:11 +0000 | [diff] [blame] | 379 | status = readl_relaxed(aq->regs + QSPI_SR); |
| 380 | mask = readl_relaxed(aq->regs + QSPI_IMR); |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 381 | pending = status & mask; |
| 382 | |
| 383 | if (!pending) |
| 384 | return IRQ_NONE; |
| 385 | |
| 386 | aq->pending |= pending; |
| 387 | if ((aq->pending & QSPI_SR_CMD_COMPLETED) == QSPI_SR_CMD_COMPLETED) |
| 388 | complete(&aq->cmd_completion); |
| 389 | |
| 390 | return IRQ_HANDLED; |
| 391 | } |
| 392 | |
| 393 | static int atmel_qspi_probe(struct platform_device *pdev) |
| 394 | { |
Piotr Bugalski | 2d30ac5 | 2018-11-05 11:36:22 +0100 | [diff] [blame] | 395 | struct spi_controller *ctrl; |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 396 | struct atmel_qspi *aq; |
| 397 | struct resource *res; |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 398 | int irq, err = 0; |
| 399 | |
Piotr Bugalski | 2d30ac5 | 2018-11-05 11:36:22 +0100 | [diff] [blame] | 400 | ctrl = spi_alloc_master(&pdev->dev, sizeof(*aq)); |
| 401 | if (!ctrl) |
| 402 | return -ENOMEM; |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 403 | |
Piotr Bugalski | 2d30ac5 | 2018-11-05 11:36:22 +0100 | [diff] [blame] | 404 | ctrl->mode_bits = SPI_RX_DUAL | SPI_RX_QUAD | SPI_TX_DUAL | SPI_TX_QUAD; |
| 405 | ctrl->setup = atmel_qspi_setup; |
| 406 | ctrl->bus_num = -1; |
| 407 | ctrl->mem_ops = &atmel_qspi_mem_ops; |
| 408 | ctrl->num_chipselect = 1; |
| 409 | ctrl->dev.of_node = pdev->dev.of_node; |
| 410 | platform_set_drvdata(pdev, ctrl); |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 411 | |
Piotr Bugalski | 2d30ac5 | 2018-11-05 11:36:22 +0100 | [diff] [blame] | 412 | aq = spi_controller_get_devdata(ctrl); |
| 413 | |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 414 | init_completion(&aq->cmd_completion); |
| 415 | aq->pdev = pdev; |
| 416 | |
| 417 | /* Map the registers */ |
| 418 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "qspi_base"); |
| 419 | aq->regs = devm_ioremap_resource(&pdev->dev, res); |
| 420 | if (IS_ERR(aq->regs)) { |
| 421 | dev_err(&pdev->dev, "missing registers\n"); |
| 422 | err = PTR_ERR(aq->regs); |
| 423 | goto exit; |
| 424 | } |
| 425 | |
| 426 | /* Map the AHB memory */ |
| 427 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "qspi_mmap"); |
| 428 | aq->mem = devm_ioremap_resource(&pdev->dev, res); |
| 429 | if (IS_ERR(aq->mem)) { |
| 430 | dev_err(&pdev->dev, "missing AHB memory\n"); |
| 431 | err = PTR_ERR(aq->mem); |
| 432 | goto exit; |
| 433 | } |
| 434 | |
| 435 | /* Get the peripheral clock */ |
Tudor Ambarus | bd7905e | 2019-02-05 17:33:33 +0000 | [diff] [blame^] | 436 | aq->pclk = devm_clk_get(&pdev->dev, "pclk"); |
| 437 | if (IS_ERR(aq->pclk)) |
| 438 | aq->pclk = devm_clk_get(&pdev->dev, NULL); |
| 439 | |
| 440 | if (IS_ERR(aq->pclk)) { |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 441 | dev_err(&pdev->dev, "missing peripheral clock\n"); |
Tudor Ambarus | bd7905e | 2019-02-05 17:33:33 +0000 | [diff] [blame^] | 442 | err = PTR_ERR(aq->pclk); |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 443 | goto exit; |
| 444 | } |
| 445 | |
| 446 | /* Enable the peripheral clock */ |
Tudor Ambarus | bd7905e | 2019-02-05 17:33:33 +0000 | [diff] [blame^] | 447 | err = clk_prepare_enable(aq->pclk); |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 448 | if (err) { |
| 449 | dev_err(&pdev->dev, "failed to enable the peripheral clock\n"); |
| 450 | goto exit; |
| 451 | } |
| 452 | |
| 453 | /* Request the IRQ */ |
| 454 | irq = platform_get_irq(pdev, 0); |
| 455 | if (irq < 0) { |
| 456 | dev_err(&pdev->dev, "missing IRQ\n"); |
| 457 | err = irq; |
Tudor Ambarus | bd7905e | 2019-02-05 17:33:33 +0000 | [diff] [blame^] | 458 | goto disable_pclk; |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 459 | } |
| 460 | err = devm_request_irq(&pdev->dev, irq, atmel_qspi_interrupt, |
| 461 | 0, dev_name(&pdev->dev), aq); |
| 462 | if (err) |
Tudor Ambarus | bd7905e | 2019-02-05 17:33:33 +0000 | [diff] [blame^] | 463 | goto disable_pclk; |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 464 | |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 465 | err = atmel_qspi_init(aq); |
| 466 | if (err) |
Tudor Ambarus | bd7905e | 2019-02-05 17:33:33 +0000 | [diff] [blame^] | 467 | goto disable_pclk; |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 468 | |
Piotr Bugalski | 2d30ac5 | 2018-11-05 11:36:22 +0100 | [diff] [blame] | 469 | err = spi_register_controller(ctrl); |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 470 | if (err) |
Tudor Ambarus | bd7905e | 2019-02-05 17:33:33 +0000 | [diff] [blame^] | 471 | goto disable_pclk; |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 472 | |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 473 | return 0; |
| 474 | |
Tudor Ambarus | bd7905e | 2019-02-05 17:33:33 +0000 | [diff] [blame^] | 475 | disable_pclk: |
| 476 | clk_disable_unprepare(aq->pclk); |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 477 | exit: |
Piotr Bugalski | 2d30ac5 | 2018-11-05 11:36:22 +0100 | [diff] [blame] | 478 | spi_controller_put(ctrl); |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 479 | |
| 480 | return err; |
| 481 | } |
| 482 | |
| 483 | static int atmel_qspi_remove(struct platform_device *pdev) |
| 484 | { |
Piotr Bugalski | 2d30ac5 | 2018-11-05 11:36:22 +0100 | [diff] [blame] | 485 | struct spi_controller *ctrl = platform_get_drvdata(pdev); |
| 486 | struct atmel_qspi *aq = spi_controller_get_devdata(ctrl); |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 487 | |
Piotr Bugalski | 2d30ac5 | 2018-11-05 11:36:22 +0100 | [diff] [blame] | 488 | spi_unregister_controller(ctrl); |
Tudor Ambarus | 18b6f6e | 2019-02-05 17:33:11 +0000 | [diff] [blame] | 489 | writel_relaxed(QSPI_CR_QSPIDIS, aq->regs + QSPI_CR); |
Tudor Ambarus | bd7905e | 2019-02-05 17:33:33 +0000 | [diff] [blame^] | 490 | clk_disable_unprepare(aq->pclk); |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 491 | return 0; |
| 492 | } |
| 493 | |
Claudiu Beznea | de217c1 | 2018-06-04 11:46:33 +0300 | [diff] [blame] | 494 | static int __maybe_unused atmel_qspi_suspend(struct device *dev) |
| 495 | { |
| 496 | struct atmel_qspi *aq = dev_get_drvdata(dev); |
| 497 | |
Tudor Ambarus | bd7905e | 2019-02-05 17:33:33 +0000 | [diff] [blame^] | 498 | clk_disable_unprepare(aq->pclk); |
Claudiu Beznea | de217c1 | 2018-06-04 11:46:33 +0300 | [diff] [blame] | 499 | |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | static int __maybe_unused atmel_qspi_resume(struct device *dev) |
| 504 | { |
| 505 | struct atmel_qspi *aq = dev_get_drvdata(dev); |
| 506 | |
Tudor Ambarus | bd7905e | 2019-02-05 17:33:33 +0000 | [diff] [blame^] | 507 | clk_prepare_enable(aq->pclk); |
Claudiu Beznea | de217c1 | 2018-06-04 11:46:33 +0300 | [diff] [blame] | 508 | |
| 509 | return atmel_qspi_init(aq); |
| 510 | } |
| 511 | |
| 512 | static SIMPLE_DEV_PM_OPS(atmel_qspi_pm_ops, atmel_qspi_suspend, |
| 513 | atmel_qspi_resume); |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 514 | |
| 515 | static const struct of_device_id atmel_qspi_dt_ids[] = { |
| 516 | { .compatible = "atmel,sama5d2-qspi" }, |
| 517 | { /* sentinel */ } |
| 518 | }; |
| 519 | |
| 520 | MODULE_DEVICE_TABLE(of, atmel_qspi_dt_ids); |
| 521 | |
| 522 | static struct platform_driver atmel_qspi_driver = { |
| 523 | .driver = { |
| 524 | .name = "atmel_qspi", |
| 525 | .of_match_table = atmel_qspi_dt_ids, |
Claudiu Beznea | de217c1 | 2018-06-04 11:46:33 +0300 | [diff] [blame] | 526 | .pm = &atmel_qspi_pm_ops, |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 527 | }, |
| 528 | .probe = atmel_qspi_probe, |
| 529 | .remove = atmel_qspi_remove, |
| 530 | }; |
| 531 | module_platform_driver(atmel_qspi_driver); |
| 532 | |
| 533 | MODULE_AUTHOR("Cyrille Pitchen <cyrille.pitchen@atmel.com>"); |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 534 | MODULE_AUTHOR("Piotr Bugalski <bugalski.piotr@gmail.com"); |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 535 | MODULE_DESCRIPTION("Atmel QSPI Controller driver"); |
| 536 | MODULE_LICENSE("GPL v2"); |