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> |
Tudor Ambarus | 2e5c888 | 2019-02-05 17:33:38 +0000 | [diff] [blame] | 22 | #include <linux/of_platform.h> |
Tudor Ambarus | 3ae012e | 2019-02-05 17:33:08 +0000 | [diff] [blame] | 23 | #include <linux/platform_device.h> |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 24 | #include <linux/spi/spi-mem.h> |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 25 | |
| 26 | /* QSPI register offsets */ |
| 27 | #define QSPI_CR 0x0000 /* Control Register */ |
| 28 | #define QSPI_MR 0x0004 /* Mode Register */ |
| 29 | #define QSPI_RD 0x0008 /* Receive Data Register */ |
| 30 | #define QSPI_TD 0x000c /* Transmit Data Register */ |
| 31 | #define QSPI_SR 0x0010 /* Status Register */ |
| 32 | #define QSPI_IER 0x0014 /* Interrupt Enable Register */ |
| 33 | #define QSPI_IDR 0x0018 /* Interrupt Disable Register */ |
| 34 | #define QSPI_IMR 0x001c /* Interrupt Mask Register */ |
| 35 | #define QSPI_SCR 0x0020 /* Serial Clock Register */ |
| 36 | |
| 37 | #define QSPI_IAR 0x0030 /* Instruction Address Register */ |
| 38 | #define QSPI_ICR 0x0034 /* Instruction Code Register */ |
Tudor Ambarus | 2e5c888 | 2019-02-05 17:33:38 +0000 | [diff] [blame] | 39 | #define QSPI_WICR 0x0034 /* Write Instruction Code Register */ |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 40 | #define QSPI_IFR 0x0038 /* Instruction Frame Register */ |
Tudor Ambarus | 2e5c888 | 2019-02-05 17:33:38 +0000 | [diff] [blame] | 41 | #define QSPI_RICR 0x003C /* Read Instruction Code Register */ |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 42 | |
| 43 | #define QSPI_SMR 0x0040 /* Scrambling Mode Register */ |
| 44 | #define QSPI_SKR 0x0044 /* Scrambling Key Register */ |
| 45 | |
| 46 | #define QSPI_WPMR 0x00E4 /* Write Protection Mode Register */ |
| 47 | #define QSPI_WPSR 0x00E8 /* Write Protection Status Register */ |
| 48 | |
| 49 | #define QSPI_VERSION 0x00FC /* Version Register */ |
| 50 | |
| 51 | |
| 52 | /* Bitfields in QSPI_CR (Control Register) */ |
| 53 | #define QSPI_CR_QSPIEN BIT(0) |
| 54 | #define QSPI_CR_QSPIDIS BIT(1) |
| 55 | #define QSPI_CR_SWRST BIT(7) |
| 56 | #define QSPI_CR_LASTXFER BIT(24) |
| 57 | |
| 58 | /* Bitfields in QSPI_MR (Mode Register) */ |
Piotr Bugalski | b82ab1c | 2018-11-05 11:36:20 +0100 | [diff] [blame] | 59 | #define QSPI_MR_SMM BIT(0) |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 60 | #define QSPI_MR_LLB BIT(1) |
| 61 | #define QSPI_MR_WDRBT BIT(2) |
| 62 | #define QSPI_MR_SMRM BIT(3) |
| 63 | #define QSPI_MR_CSMODE_MASK GENMASK(5, 4) |
| 64 | #define QSPI_MR_CSMODE_NOT_RELOADED (0 << 4) |
| 65 | #define QSPI_MR_CSMODE_LASTXFER (1 << 4) |
| 66 | #define QSPI_MR_CSMODE_SYSTEMATICALLY (2 << 4) |
| 67 | #define QSPI_MR_NBBITS_MASK GENMASK(11, 8) |
| 68 | #define QSPI_MR_NBBITS(n) ((((n) - 8) << 8) & QSPI_MR_NBBITS_MASK) |
| 69 | #define QSPI_MR_DLYBCT_MASK GENMASK(23, 16) |
| 70 | #define QSPI_MR_DLYBCT(n) (((n) << 16) & QSPI_MR_DLYBCT_MASK) |
| 71 | #define QSPI_MR_DLYCS_MASK GENMASK(31, 24) |
| 72 | #define QSPI_MR_DLYCS(n) (((n) << 24) & QSPI_MR_DLYCS_MASK) |
| 73 | |
| 74 | /* Bitfields in QSPI_SR/QSPI_IER/QSPI_IDR/QSPI_IMR */ |
| 75 | #define QSPI_SR_RDRF BIT(0) |
| 76 | #define QSPI_SR_TDRE BIT(1) |
| 77 | #define QSPI_SR_TXEMPTY BIT(2) |
| 78 | #define QSPI_SR_OVRES BIT(3) |
| 79 | #define QSPI_SR_CSR BIT(8) |
| 80 | #define QSPI_SR_CSS BIT(9) |
| 81 | #define QSPI_SR_INSTRE BIT(10) |
| 82 | #define QSPI_SR_QSPIENS BIT(24) |
| 83 | |
| 84 | #define QSPI_SR_CMD_COMPLETED (QSPI_SR_INSTRE | QSPI_SR_CSR) |
| 85 | |
| 86 | /* Bitfields in QSPI_SCR (Serial Clock Register) */ |
| 87 | #define QSPI_SCR_CPOL BIT(0) |
| 88 | #define QSPI_SCR_CPHA BIT(1) |
| 89 | #define QSPI_SCR_SCBR_MASK GENMASK(15, 8) |
| 90 | #define QSPI_SCR_SCBR(n) (((n) << 8) & QSPI_SCR_SCBR_MASK) |
| 91 | #define QSPI_SCR_DLYBS_MASK GENMASK(23, 16) |
| 92 | #define QSPI_SCR_DLYBS(n) (((n) << 16) & QSPI_SCR_DLYBS_MASK) |
| 93 | |
Tudor Ambarus | 2e5c888 | 2019-02-05 17:33:38 +0000 | [diff] [blame] | 94 | /* Bitfields in QSPI_ICR (Read/Write Instruction Code Register) */ |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 95 | #define QSPI_ICR_INST_MASK GENMASK(7, 0) |
| 96 | #define QSPI_ICR_INST(inst) (((inst) << 0) & QSPI_ICR_INST_MASK) |
| 97 | #define QSPI_ICR_OPT_MASK GENMASK(23, 16) |
| 98 | #define QSPI_ICR_OPT(opt) (((opt) << 16) & QSPI_ICR_OPT_MASK) |
| 99 | |
| 100 | /* Bitfields in QSPI_IFR (Instruction Frame Register) */ |
| 101 | #define QSPI_IFR_WIDTH_MASK GENMASK(2, 0) |
| 102 | #define QSPI_IFR_WIDTH_SINGLE_BIT_SPI (0 << 0) |
| 103 | #define QSPI_IFR_WIDTH_DUAL_OUTPUT (1 << 0) |
| 104 | #define QSPI_IFR_WIDTH_QUAD_OUTPUT (2 << 0) |
| 105 | #define QSPI_IFR_WIDTH_DUAL_IO (3 << 0) |
| 106 | #define QSPI_IFR_WIDTH_QUAD_IO (4 << 0) |
| 107 | #define QSPI_IFR_WIDTH_DUAL_CMD (5 << 0) |
| 108 | #define QSPI_IFR_WIDTH_QUAD_CMD (6 << 0) |
| 109 | #define QSPI_IFR_INSTEN BIT(4) |
| 110 | #define QSPI_IFR_ADDREN BIT(5) |
| 111 | #define QSPI_IFR_OPTEN BIT(6) |
| 112 | #define QSPI_IFR_DATAEN BIT(7) |
| 113 | #define QSPI_IFR_OPTL_MASK GENMASK(9, 8) |
| 114 | #define QSPI_IFR_OPTL_1BIT (0 << 8) |
| 115 | #define QSPI_IFR_OPTL_2BIT (1 << 8) |
| 116 | #define QSPI_IFR_OPTL_4BIT (2 << 8) |
| 117 | #define QSPI_IFR_OPTL_8BIT (3 << 8) |
| 118 | #define QSPI_IFR_ADDRL BIT(10) |
Tudor Ambarus | b456fd1 | 2019-02-05 17:33:25 +0000 | [diff] [blame] | 119 | #define QSPI_IFR_TFRTYP_MEM BIT(12) |
| 120 | #define QSPI_IFR_SAMA5D2_WRITE_TRSFR BIT(13) |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 121 | #define QSPI_IFR_CRM BIT(14) |
| 122 | #define QSPI_IFR_NBDUM_MASK GENMASK(20, 16) |
| 123 | #define QSPI_IFR_NBDUM(n) (((n) << 16) & QSPI_IFR_NBDUM_MASK) |
Tudor Ambarus | 2e5c888 | 2019-02-05 17:33:38 +0000 | [diff] [blame] | 124 | #define QSPI_IFR_APBTFRTYP_READ BIT(24) /* Defined in SAM9X60 */ |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 125 | |
| 126 | /* Bitfields in QSPI_SMR (Scrambling Mode Register) */ |
| 127 | #define QSPI_SMR_SCREN BIT(0) |
| 128 | #define QSPI_SMR_RVDIS BIT(1) |
| 129 | |
| 130 | /* Bitfields in QSPI_WPMR (Write Protection Mode Register) */ |
| 131 | #define QSPI_WPMR_WPEN BIT(0) |
| 132 | #define QSPI_WPMR_WPKEY_MASK GENMASK(31, 8) |
| 133 | #define QSPI_WPMR_WPKEY(wpkey) (((wpkey) << 8) & QSPI_WPMR_WPKEY_MASK) |
| 134 | |
| 135 | /* Bitfields in QSPI_WPSR (Write Protection Status Register) */ |
| 136 | #define QSPI_WPSR_WPVS BIT(0) |
| 137 | #define QSPI_WPSR_WPVSRC_MASK GENMASK(15, 8) |
| 138 | #define QSPI_WPSR_WPVSRC(src) (((src) << 8) & QSPI_WPSR_WPVSRC) |
| 139 | |
Tudor Ambarus | 2e5c888 | 2019-02-05 17:33:38 +0000 | [diff] [blame] | 140 | struct atmel_qspi_caps { |
| 141 | bool has_qspick; |
| 142 | bool has_ricr; |
| 143 | }; |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 144 | |
| 145 | struct atmel_qspi { |
| 146 | void __iomem *regs; |
| 147 | void __iomem *mem; |
Tudor Ambarus | bd7905e | 2019-02-05 17:33:33 +0000 | [diff] [blame] | 148 | struct clk *pclk; |
Tudor Ambarus | 2e5c888 | 2019-02-05 17:33:38 +0000 | [diff] [blame] | 149 | struct clk *qspick; |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 150 | struct platform_device *pdev; |
Tudor Ambarus | 2e5c888 | 2019-02-05 17:33:38 +0000 | [diff] [blame] | 151 | const struct atmel_qspi_caps *caps; |
Tudor Ambarus | 8e093ea | 2020-02-28 15:55:32 +0000 | [diff] [blame] | 152 | resource_size_t mmap_size; |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 153 | u32 pending; |
Tudor Ambarus | 9958c8c | 2019-02-05 17:33:06 +0000 | [diff] [blame] | 154 | u32 mr; |
Tudor Ambarus | ab73561 | 2019-06-28 15:30:34 +0000 | [diff] [blame] | 155 | u32 scr; |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 156 | struct completion cmd_completion; |
| 157 | }; |
| 158 | |
Tudor Ambarus | 1db6de2 | 2019-02-05 17:33:14 +0000 | [diff] [blame] | 159 | struct atmel_qspi_mode { |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 160 | u8 cmd_buswidth; |
| 161 | u8 addr_buswidth; |
| 162 | u8 data_buswidth; |
| 163 | u32 config; |
| 164 | }; |
| 165 | |
Tudor Ambarus | 2e5c888 | 2019-02-05 17:33:38 +0000 | [diff] [blame] | 166 | static const struct atmel_qspi_mode atmel_qspi_modes[] = { |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 167 | { 1, 1, 1, QSPI_IFR_WIDTH_SINGLE_BIT_SPI }, |
| 168 | { 1, 1, 2, QSPI_IFR_WIDTH_DUAL_OUTPUT }, |
| 169 | { 1, 1, 4, QSPI_IFR_WIDTH_QUAD_OUTPUT }, |
| 170 | { 1, 2, 2, QSPI_IFR_WIDTH_DUAL_IO }, |
| 171 | { 1, 4, 4, QSPI_IFR_WIDTH_QUAD_IO }, |
| 172 | { 2, 2, 2, QSPI_IFR_WIDTH_DUAL_CMD }, |
| 173 | { 4, 4, 4, QSPI_IFR_WIDTH_QUAD_CMD }, |
| 174 | }; |
| 175 | |
Tudor Ambarus | c528ecf | 2020-03-20 06:51:01 +0000 | [diff] [blame] | 176 | #ifdef VERBOSE_DEBUG |
| 177 | static const char *atmel_qspi_reg_name(u32 offset, char *tmp, size_t sz) |
| 178 | { |
| 179 | switch (offset) { |
| 180 | case QSPI_CR: |
| 181 | return "CR"; |
| 182 | case QSPI_MR: |
| 183 | return "MR"; |
| 184 | case QSPI_RD: |
| 185 | return "MR"; |
| 186 | case QSPI_TD: |
| 187 | return "TD"; |
| 188 | case QSPI_SR: |
| 189 | return "SR"; |
| 190 | case QSPI_IER: |
| 191 | return "IER"; |
| 192 | case QSPI_IDR: |
| 193 | return "IDR"; |
| 194 | case QSPI_IMR: |
| 195 | return "IMR"; |
| 196 | case QSPI_SCR: |
| 197 | return "SCR"; |
| 198 | case QSPI_IAR: |
| 199 | return "IAR"; |
| 200 | case QSPI_ICR: |
| 201 | return "ICR/WICR"; |
| 202 | case QSPI_IFR: |
| 203 | return "IFR"; |
| 204 | case QSPI_RICR: |
| 205 | return "RICR"; |
| 206 | case QSPI_SMR: |
| 207 | return "SMR"; |
| 208 | case QSPI_SKR: |
| 209 | return "SKR"; |
| 210 | case QSPI_WPMR: |
| 211 | return "WPMR"; |
| 212 | case QSPI_WPSR: |
| 213 | return "WPSR"; |
| 214 | case QSPI_VERSION: |
| 215 | return "VERSION"; |
| 216 | default: |
| 217 | snprintf(tmp, sz, "0x%02x", offset); |
| 218 | break; |
| 219 | } |
| 220 | |
| 221 | return tmp; |
| 222 | } |
| 223 | #endif /* VERBOSE_DEBUG */ |
| 224 | |
| 225 | static u32 atmel_qspi_read(struct atmel_qspi *aq, u32 offset) |
| 226 | { |
| 227 | u32 value = readl_relaxed(aq->regs + offset); |
| 228 | |
| 229 | #ifdef VERBOSE_DEBUG |
| 230 | char tmp[8]; |
| 231 | |
| 232 | dev_vdbg(&aq->pdev->dev, "read 0x%08x from %s\n", value, |
| 233 | atmel_qspi_reg_name(offset, tmp, sizeof(tmp))); |
| 234 | #endif /* VERBOSE_DEBUG */ |
| 235 | |
| 236 | return value; |
| 237 | } |
| 238 | |
| 239 | static void atmel_qspi_write(u32 value, struct atmel_qspi *aq, u32 offset) |
| 240 | { |
| 241 | #ifdef VERBOSE_DEBUG |
| 242 | char tmp[8]; |
| 243 | |
| 244 | dev_vdbg(&aq->pdev->dev, "write 0x%08x into %s\n", value, |
| 245 | atmel_qspi_reg_name(offset, tmp, sizeof(tmp))); |
| 246 | #endif /* VERBOSE_DEBUG */ |
| 247 | |
| 248 | writel_relaxed(value, aq->regs + offset); |
| 249 | } |
| 250 | |
Tudor Ambarus | 1db6de2 | 2019-02-05 17:33:14 +0000 | [diff] [blame] | 251 | static inline bool atmel_qspi_is_compatible(const struct spi_mem_op *op, |
| 252 | const struct atmel_qspi_mode *mode) |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 253 | { |
| 254 | if (op->cmd.buswidth != mode->cmd_buswidth) |
| 255 | return false; |
| 256 | |
| 257 | if (op->addr.nbytes && op->addr.buswidth != mode->addr_buswidth) |
| 258 | return false; |
| 259 | |
| 260 | if (op->data.nbytes && op->data.buswidth != mode->data_buswidth) |
| 261 | return false; |
| 262 | |
| 263 | return true; |
| 264 | } |
| 265 | |
Tudor Ambarus | 1db6de2 | 2019-02-05 17:33:14 +0000 | [diff] [blame] | 266 | static int atmel_qspi_find_mode(const struct spi_mem_op *op) |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 267 | { |
| 268 | u32 i; |
| 269 | |
Tudor Ambarus | 2e5c888 | 2019-02-05 17:33:38 +0000 | [diff] [blame] | 270 | for (i = 0; i < ARRAY_SIZE(atmel_qspi_modes); i++) |
| 271 | if (atmel_qspi_is_compatible(op, &atmel_qspi_modes[i])) |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 272 | return i; |
| 273 | |
Tudor Ambarus | 2aaa8dd | 2019-02-05 17:33:19 +0000 | [diff] [blame] | 274 | return -ENOTSUPP; |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | static bool atmel_qspi_supports_op(struct spi_mem *mem, |
| 278 | const struct spi_mem_op *op) |
| 279 | { |
Tudor Ambarus | 1db6de2 | 2019-02-05 17:33:14 +0000 | [diff] [blame] | 280 | if (atmel_qspi_find_mode(op) < 0) |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 281 | return false; |
| 282 | |
| 283 | /* special case not supported by hardware */ |
| 284 | if (op->addr.nbytes == 2 && op->cmd.buswidth != op->addr.buswidth && |
| 285 | op->dummy.nbytes == 0) |
| 286 | return false; |
| 287 | |
Pratyush Yadav | 5c81c27 | 2020-06-24 00:00:16 +0530 | [diff] [blame^] | 288 | /* DTR ops not supported. */ |
| 289 | if (op->cmd.dtr || op->addr.dtr || op->dummy.dtr || op->data.dtr) |
| 290 | return false; |
| 291 | if (op->cmd.nbytes != 1) |
| 292 | return false; |
| 293 | |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 294 | return true; |
| 295 | } |
| 296 | |
Tudor Ambarus | 2e5c888 | 2019-02-05 17:33:38 +0000 | [diff] [blame] | 297 | static int atmel_qspi_set_cfg(struct atmel_qspi *aq, |
| 298 | const struct spi_mem_op *op, u32 *offset) |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 299 | { |
Tudor Ambarus | 2e5c888 | 2019-02-05 17:33:38 +0000 | [diff] [blame] | 300 | u32 iar, icr, ifr; |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 301 | u32 dummy_cycles = 0; |
Tudor Ambarus | 2e5c888 | 2019-02-05 17:33:38 +0000 | [diff] [blame] | 302 | int mode; |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 303 | |
| 304 | iar = 0; |
| 305 | icr = QSPI_ICR_INST(op->cmd.opcode); |
| 306 | ifr = QSPI_IFR_INSTEN; |
| 307 | |
Tudor Ambarus | 1db6de2 | 2019-02-05 17:33:14 +0000 | [diff] [blame] | 308 | mode = atmel_qspi_find_mode(op); |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 309 | if (mode < 0) |
Tudor Ambarus | 2aaa8dd | 2019-02-05 17:33:19 +0000 | [diff] [blame] | 310 | return mode; |
Tudor Ambarus | 2e5c888 | 2019-02-05 17:33:38 +0000 | [diff] [blame] | 311 | ifr |= atmel_qspi_modes[mode].config; |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 312 | |
| 313 | if (op->dummy.buswidth && op->dummy.nbytes) |
| 314 | dummy_cycles = op->dummy.nbytes * 8 / op->dummy.buswidth; |
| 315 | |
Tudor Ambarus | 2e5c888 | 2019-02-05 17:33:38 +0000 | [diff] [blame] | 316 | /* |
| 317 | * The controller allows 24 and 32-bit addressing while NAND-flash |
| 318 | * requires 16-bit long. Handling 8-bit long addresses is done using |
| 319 | * the option field. For the 16-bit addresses, the workaround depends |
| 320 | * of the number of requested dummy bits. If there are 8 or more dummy |
| 321 | * cycles, the address is shifted and sent with the first dummy byte. |
| 322 | * Otherwise opcode is disabled and the first byte of the address |
| 323 | * contains the command opcode (works only if the opcode and address |
| 324 | * use the same buswidth). The limitation is when the 16-bit address is |
| 325 | * used without enough dummy cycles and the opcode is using a different |
| 326 | * buswidth than the address. |
| 327 | */ |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 328 | if (op->addr.buswidth) { |
| 329 | switch (op->addr.nbytes) { |
| 330 | case 0: |
| 331 | break; |
| 332 | case 1: |
| 333 | ifr |= QSPI_IFR_OPTEN | QSPI_IFR_OPTL_8BIT; |
| 334 | icr |= QSPI_ICR_OPT(op->addr.val & 0xff); |
| 335 | break; |
| 336 | case 2: |
| 337 | if (dummy_cycles < 8 / op->addr.buswidth) { |
| 338 | ifr &= ~QSPI_IFR_INSTEN; |
| 339 | ifr |= QSPI_IFR_ADDREN; |
| 340 | iar = (op->cmd.opcode << 16) | |
| 341 | (op->addr.val & 0xffff); |
| 342 | } else { |
| 343 | ifr |= QSPI_IFR_ADDREN; |
| 344 | iar = (op->addr.val << 8) & 0xffffff; |
| 345 | dummy_cycles -= 8 / op->addr.buswidth; |
| 346 | } |
| 347 | break; |
| 348 | case 3: |
| 349 | ifr |= QSPI_IFR_ADDREN; |
| 350 | iar = op->addr.val & 0xffffff; |
| 351 | break; |
| 352 | case 4: |
| 353 | ifr |= QSPI_IFR_ADDREN | QSPI_IFR_ADDRL; |
| 354 | iar = op->addr.val & 0x7ffffff; |
| 355 | break; |
| 356 | default: |
| 357 | return -ENOTSUPP; |
| 358 | } |
| 359 | } |
| 360 | |
Tudor Ambarus | 2e5c888 | 2019-02-05 17:33:38 +0000 | [diff] [blame] | 361 | /* offset of the data access in the QSPI memory space */ |
| 362 | *offset = iar; |
| 363 | |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 364 | /* Set number of dummy cycles */ |
| 365 | if (dummy_cycles) |
| 366 | ifr |= QSPI_IFR_NBDUM(dummy_cycles); |
| 367 | |
| 368 | /* Set data enable */ |
| 369 | if (op->data.nbytes) |
| 370 | ifr |= QSPI_IFR_DATAEN; |
| 371 | |
Tudor Ambarus | 2e5c888 | 2019-02-05 17:33:38 +0000 | [diff] [blame] | 372 | /* |
| 373 | * If the QSPI controller is set in regular SPI mode, set it in |
| 374 | * Serial Memory Mode (SMM). |
| 375 | */ |
| 376 | if (aq->mr != QSPI_MR_SMM) { |
Tudor Ambarus | c528ecf | 2020-03-20 06:51:01 +0000 | [diff] [blame] | 377 | atmel_qspi_write(QSPI_MR_SMM, aq, QSPI_MR); |
Tudor Ambarus | 2e5c888 | 2019-02-05 17:33:38 +0000 | [diff] [blame] | 378 | aq->mr = QSPI_MR_SMM; |
| 379 | } |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 380 | |
| 381 | /* Clear pending interrupts */ |
Tudor Ambarus | c528ecf | 2020-03-20 06:51:01 +0000 | [diff] [blame] | 382 | (void)atmel_qspi_read(aq, QSPI_SR); |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 383 | |
Tudor Ambarus | 2e5c888 | 2019-02-05 17:33:38 +0000 | [diff] [blame] | 384 | if (aq->caps->has_ricr) { |
| 385 | if (!op->addr.nbytes && op->data.dir == SPI_MEM_DATA_IN) |
| 386 | ifr |= QSPI_IFR_APBTFRTYP_READ; |
| 387 | |
| 388 | /* Set QSPI Instruction Frame registers */ |
Tudor Ambarus | c528ecf | 2020-03-20 06:51:01 +0000 | [diff] [blame] | 389 | atmel_qspi_write(iar, aq, QSPI_IAR); |
Tudor Ambarus | 2e5c888 | 2019-02-05 17:33:38 +0000 | [diff] [blame] | 390 | if (op->data.dir == SPI_MEM_DATA_IN) |
Tudor Ambarus | c528ecf | 2020-03-20 06:51:01 +0000 | [diff] [blame] | 391 | atmel_qspi_write(icr, aq, QSPI_RICR); |
Tudor Ambarus | 2e5c888 | 2019-02-05 17:33:38 +0000 | [diff] [blame] | 392 | else |
Tudor Ambarus | c528ecf | 2020-03-20 06:51:01 +0000 | [diff] [blame] | 393 | atmel_qspi_write(icr, aq, QSPI_WICR); |
| 394 | atmel_qspi_write(ifr, aq, QSPI_IFR); |
Tudor Ambarus | 2e5c888 | 2019-02-05 17:33:38 +0000 | [diff] [blame] | 395 | } else { |
| 396 | if (op->data.dir == SPI_MEM_DATA_OUT) |
| 397 | ifr |= QSPI_IFR_SAMA5D2_WRITE_TRSFR; |
| 398 | |
| 399 | /* Set QSPI Instruction Frame registers */ |
Tudor Ambarus | c528ecf | 2020-03-20 06:51:01 +0000 | [diff] [blame] | 400 | atmel_qspi_write(iar, aq, QSPI_IAR); |
| 401 | atmel_qspi_write(icr, aq, QSPI_ICR); |
| 402 | atmel_qspi_write(ifr, aq, QSPI_IFR); |
Tudor Ambarus | 2e5c888 | 2019-02-05 17:33:38 +0000 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | static int atmel_qspi_exec_op(struct spi_mem *mem, const struct spi_mem_op *op) |
| 409 | { |
| 410 | struct atmel_qspi *aq = spi_controller_get_devdata(mem->spi->master); |
| 411 | u32 sr, offset; |
| 412 | int err; |
| 413 | |
Tudor Ambarus | 8e093ea | 2020-02-28 15:55:32 +0000 | [diff] [blame] | 414 | /* |
| 415 | * Check if the address exceeds the MMIO window size. An improvement |
| 416 | * would be to add support for regular SPI mode and fall back to it |
| 417 | * when the flash memories overrun the controller's memory space. |
| 418 | */ |
| 419 | if (op->addr.val + op->data.nbytes > aq->mmap_size) |
| 420 | return -ENOTSUPP; |
| 421 | |
Tudor Ambarus | 2e5c888 | 2019-02-05 17:33:38 +0000 | [diff] [blame] | 422 | err = atmel_qspi_set_cfg(aq, op, &offset); |
| 423 | if (err) |
| 424 | return err; |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 425 | |
| 426 | /* Skip to the final steps if there is no data */ |
| 427 | if (op->data.nbytes) { |
| 428 | /* Dummy read of QSPI_IFR to synchronize APB and AHB accesses */ |
Tudor Ambarus | c528ecf | 2020-03-20 06:51:01 +0000 | [diff] [blame] | 429 | (void)atmel_qspi_read(aq, QSPI_IFR); |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 430 | |
| 431 | /* Send/Receive data */ |
| 432 | if (op->data.dir == SPI_MEM_DATA_IN) |
Tudor Ambarus | 2e5c888 | 2019-02-05 17:33:38 +0000 | [diff] [blame] | 433 | _memcpy_fromio(op->data.buf.in, aq->mem + offset, |
| 434 | op->data.nbytes); |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 435 | else |
Tudor Ambarus | 2e5c888 | 2019-02-05 17:33:38 +0000 | [diff] [blame] | 436 | _memcpy_toio(aq->mem + offset, op->data.buf.out, |
| 437 | op->data.nbytes); |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 438 | |
| 439 | /* Release the chip-select */ |
Tudor Ambarus | c528ecf | 2020-03-20 06:51:01 +0000 | [diff] [blame] | 440 | atmel_qspi_write(QSPI_CR_LASTXFER, aq, QSPI_CR); |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | /* Poll INSTRuction End status */ |
Tudor Ambarus | c528ecf | 2020-03-20 06:51:01 +0000 | [diff] [blame] | 444 | sr = atmel_qspi_read(aq, QSPI_SR); |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 445 | if ((sr & QSPI_SR_CMD_COMPLETED) == QSPI_SR_CMD_COMPLETED) |
| 446 | return err; |
| 447 | |
| 448 | /* Wait for INSTRuction End interrupt */ |
| 449 | reinit_completion(&aq->cmd_completion); |
| 450 | aq->pending = sr & QSPI_SR_CMD_COMPLETED; |
Tudor Ambarus | c528ecf | 2020-03-20 06:51:01 +0000 | [diff] [blame] | 451 | atmel_qspi_write(QSPI_SR_CMD_COMPLETED, aq, QSPI_IER); |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 452 | if (!wait_for_completion_timeout(&aq->cmd_completion, |
| 453 | msecs_to_jiffies(1000))) |
| 454 | err = -ETIMEDOUT; |
Tudor Ambarus | c528ecf | 2020-03-20 06:51:01 +0000 | [diff] [blame] | 455 | atmel_qspi_write(QSPI_SR_CMD_COMPLETED, aq, QSPI_IDR); |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 456 | |
| 457 | return err; |
| 458 | } |
| 459 | |
YueHaibing | 55e3dac | 2019-03-21 23:16:56 +0800 | [diff] [blame] | 460 | static const char *atmel_qspi_get_name(struct spi_mem *spimem) |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 461 | { |
| 462 | return dev_name(spimem->spi->dev.parent); |
| 463 | } |
| 464 | |
| 465 | static const struct spi_controller_mem_ops atmel_qspi_mem_ops = { |
| 466 | .supports_op = atmel_qspi_supports_op, |
| 467 | .exec_op = atmel_qspi_exec_op, |
| 468 | .get_name = atmel_qspi_get_name |
| 469 | }; |
| 470 | |
| 471 | static int atmel_qspi_setup(struct spi_device *spi) |
| 472 | { |
| 473 | struct spi_controller *ctrl = spi->master; |
| 474 | struct atmel_qspi *aq = spi_controller_get_devdata(ctrl); |
| 475 | unsigned long src_rate; |
Tudor Ambarus | ab73561 | 2019-06-28 15:30:34 +0000 | [diff] [blame] | 476 | u32 scbr; |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 477 | |
| 478 | if (ctrl->busy) |
| 479 | return -EBUSY; |
| 480 | |
| 481 | if (!spi->max_speed_hz) |
| 482 | return -EINVAL; |
| 483 | |
Tudor Ambarus | bd7905e | 2019-02-05 17:33:33 +0000 | [diff] [blame] | 484 | src_rate = clk_get_rate(aq->pclk); |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 485 | if (!src_rate) |
| 486 | return -EINVAL; |
| 487 | |
| 488 | /* Compute the QSPI baudrate */ |
| 489 | scbr = DIV_ROUND_UP(src_rate, spi->max_speed_hz); |
| 490 | if (scbr > 0) |
| 491 | scbr--; |
| 492 | |
Tudor Ambarus | ab73561 | 2019-06-28 15:30:34 +0000 | [diff] [blame] | 493 | aq->scr = QSPI_SCR_SCBR(scbr); |
Tudor Ambarus | c528ecf | 2020-03-20 06:51:01 +0000 | [diff] [blame] | 494 | atmel_qspi_write(aq->scr, aq, QSPI_SCR); |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 495 | |
| 496 | return 0; |
| 497 | } |
| 498 | |
Tudor Ambarus | 5b74e9a | 2019-06-28 15:30:32 +0000 | [diff] [blame] | 499 | static void atmel_qspi_init(struct atmel_qspi *aq) |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 500 | { |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 501 | /* Reset the QSPI controller */ |
Tudor Ambarus | c528ecf | 2020-03-20 06:51:01 +0000 | [diff] [blame] | 502 | atmel_qspi_write(QSPI_CR_SWRST, aq, QSPI_CR); |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 503 | |
Tudor Ambarus | 9958c8c | 2019-02-05 17:33:06 +0000 | [diff] [blame] | 504 | /* Set the QSPI controller by default in Serial Memory Mode */ |
Tudor Ambarus | c528ecf | 2020-03-20 06:51:01 +0000 | [diff] [blame] | 505 | atmel_qspi_write(QSPI_MR_SMM, aq, QSPI_MR); |
Tudor Ambarus | 9958c8c | 2019-02-05 17:33:06 +0000 | [diff] [blame] | 506 | aq->mr = QSPI_MR_SMM; |
| 507 | |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 508 | /* Enable the QSPI controller */ |
Tudor Ambarus | c528ecf | 2020-03-20 06:51:01 +0000 | [diff] [blame] | 509 | atmel_qspi_write(QSPI_CR_QSPIEN, aq, QSPI_CR); |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | static irqreturn_t atmel_qspi_interrupt(int irq, void *dev_id) |
| 513 | { |
Tudor Ambarus | 9ce4c51 | 2019-02-05 17:33:17 +0000 | [diff] [blame] | 514 | struct atmel_qspi *aq = dev_id; |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 515 | u32 status, mask, pending; |
| 516 | |
Tudor Ambarus | c528ecf | 2020-03-20 06:51:01 +0000 | [diff] [blame] | 517 | status = atmel_qspi_read(aq, QSPI_SR); |
| 518 | mask = atmel_qspi_read(aq, QSPI_IMR); |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 519 | pending = status & mask; |
| 520 | |
| 521 | if (!pending) |
| 522 | return IRQ_NONE; |
| 523 | |
| 524 | aq->pending |= pending; |
| 525 | if ((aq->pending & QSPI_SR_CMD_COMPLETED) == QSPI_SR_CMD_COMPLETED) |
| 526 | complete(&aq->cmd_completion); |
| 527 | |
| 528 | return IRQ_HANDLED; |
| 529 | } |
| 530 | |
| 531 | static int atmel_qspi_probe(struct platform_device *pdev) |
| 532 | { |
Piotr Bugalski | 2d30ac5 | 2018-11-05 11:36:22 +0100 | [diff] [blame] | 533 | struct spi_controller *ctrl; |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 534 | struct atmel_qspi *aq; |
| 535 | struct resource *res; |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 536 | int irq, err = 0; |
| 537 | |
Piotr Bugalski | 2d30ac5 | 2018-11-05 11:36:22 +0100 | [diff] [blame] | 538 | ctrl = spi_alloc_master(&pdev->dev, sizeof(*aq)); |
| 539 | if (!ctrl) |
| 540 | return -ENOMEM; |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 541 | |
Piotr Bugalski | 2d30ac5 | 2018-11-05 11:36:22 +0100 | [diff] [blame] | 542 | ctrl->mode_bits = SPI_RX_DUAL | SPI_RX_QUAD | SPI_TX_DUAL | SPI_TX_QUAD; |
| 543 | ctrl->setup = atmel_qspi_setup; |
| 544 | ctrl->bus_num = -1; |
| 545 | ctrl->mem_ops = &atmel_qspi_mem_ops; |
| 546 | ctrl->num_chipselect = 1; |
| 547 | ctrl->dev.of_node = pdev->dev.of_node; |
| 548 | platform_set_drvdata(pdev, ctrl); |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 549 | |
Piotr Bugalski | 2d30ac5 | 2018-11-05 11:36:22 +0100 | [diff] [blame] | 550 | aq = spi_controller_get_devdata(ctrl); |
| 551 | |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 552 | init_completion(&aq->cmd_completion); |
| 553 | aq->pdev = pdev; |
| 554 | |
| 555 | /* Map the registers */ |
| 556 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "qspi_base"); |
| 557 | aq->regs = devm_ioremap_resource(&pdev->dev, res); |
| 558 | if (IS_ERR(aq->regs)) { |
| 559 | dev_err(&pdev->dev, "missing registers\n"); |
| 560 | err = PTR_ERR(aq->regs); |
| 561 | goto exit; |
| 562 | } |
| 563 | |
| 564 | /* Map the AHB memory */ |
| 565 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "qspi_mmap"); |
| 566 | aq->mem = devm_ioremap_resource(&pdev->dev, res); |
| 567 | if (IS_ERR(aq->mem)) { |
| 568 | dev_err(&pdev->dev, "missing AHB memory\n"); |
| 569 | err = PTR_ERR(aq->mem); |
| 570 | goto exit; |
| 571 | } |
| 572 | |
Tudor Ambarus | 8e093ea | 2020-02-28 15:55:32 +0000 | [diff] [blame] | 573 | aq->mmap_size = resource_size(res); |
| 574 | |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 575 | /* Get the peripheral clock */ |
Tudor Ambarus | bd7905e | 2019-02-05 17:33:33 +0000 | [diff] [blame] | 576 | aq->pclk = devm_clk_get(&pdev->dev, "pclk"); |
| 577 | if (IS_ERR(aq->pclk)) |
| 578 | aq->pclk = devm_clk_get(&pdev->dev, NULL); |
| 579 | |
| 580 | if (IS_ERR(aq->pclk)) { |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 581 | dev_err(&pdev->dev, "missing peripheral clock\n"); |
Tudor Ambarus | bd7905e | 2019-02-05 17:33:33 +0000 | [diff] [blame] | 582 | err = PTR_ERR(aq->pclk); |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 583 | goto exit; |
| 584 | } |
| 585 | |
| 586 | /* Enable the peripheral clock */ |
Tudor Ambarus | bd7905e | 2019-02-05 17:33:33 +0000 | [diff] [blame] | 587 | err = clk_prepare_enable(aq->pclk); |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 588 | if (err) { |
| 589 | dev_err(&pdev->dev, "failed to enable the peripheral clock\n"); |
| 590 | goto exit; |
| 591 | } |
| 592 | |
Tudor Ambarus | 2e5c888 | 2019-02-05 17:33:38 +0000 | [diff] [blame] | 593 | aq->caps = of_device_get_match_data(&pdev->dev); |
| 594 | if (!aq->caps) { |
| 595 | dev_err(&pdev->dev, "Could not retrieve QSPI caps\n"); |
| 596 | err = -EINVAL; |
| 597 | goto exit; |
| 598 | } |
| 599 | |
| 600 | if (aq->caps->has_qspick) { |
| 601 | /* Get the QSPI system clock */ |
| 602 | aq->qspick = devm_clk_get(&pdev->dev, "qspick"); |
| 603 | if (IS_ERR(aq->qspick)) { |
| 604 | dev_err(&pdev->dev, "missing system clock\n"); |
| 605 | err = PTR_ERR(aq->qspick); |
| 606 | goto disable_pclk; |
| 607 | } |
| 608 | |
| 609 | /* Enable the QSPI system clock */ |
| 610 | err = clk_prepare_enable(aq->qspick); |
| 611 | if (err) { |
| 612 | dev_err(&pdev->dev, |
| 613 | "failed to enable the QSPI system clock\n"); |
| 614 | goto disable_pclk; |
| 615 | } |
| 616 | } |
| 617 | |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 618 | /* Request the IRQ */ |
| 619 | irq = platform_get_irq(pdev, 0); |
| 620 | if (irq < 0) { |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 621 | err = irq; |
Tudor Ambarus | 2e5c888 | 2019-02-05 17:33:38 +0000 | [diff] [blame] | 622 | goto disable_qspick; |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 623 | } |
| 624 | err = devm_request_irq(&pdev->dev, irq, atmel_qspi_interrupt, |
| 625 | 0, dev_name(&pdev->dev), aq); |
| 626 | if (err) |
Tudor Ambarus | 2e5c888 | 2019-02-05 17:33:38 +0000 | [diff] [blame] | 627 | goto disable_qspick; |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 628 | |
Tudor Ambarus | 5b74e9a | 2019-06-28 15:30:32 +0000 | [diff] [blame] | 629 | atmel_qspi_init(aq); |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 630 | |
Piotr Bugalski | 2d30ac5 | 2018-11-05 11:36:22 +0100 | [diff] [blame] | 631 | err = spi_register_controller(ctrl); |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 632 | if (err) |
Tudor Ambarus | 2e5c888 | 2019-02-05 17:33:38 +0000 | [diff] [blame] | 633 | goto disable_qspick; |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 634 | |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 635 | return 0; |
| 636 | |
Tudor Ambarus | 2e5c888 | 2019-02-05 17:33:38 +0000 | [diff] [blame] | 637 | disable_qspick: |
| 638 | clk_disable_unprepare(aq->qspick); |
Tudor Ambarus | bd7905e | 2019-02-05 17:33:33 +0000 | [diff] [blame] | 639 | disable_pclk: |
| 640 | clk_disable_unprepare(aq->pclk); |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 641 | exit: |
Piotr Bugalski | 2d30ac5 | 2018-11-05 11:36:22 +0100 | [diff] [blame] | 642 | spi_controller_put(ctrl); |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 643 | |
| 644 | return err; |
| 645 | } |
| 646 | |
| 647 | static int atmel_qspi_remove(struct platform_device *pdev) |
| 648 | { |
Piotr Bugalski | 2d30ac5 | 2018-11-05 11:36:22 +0100 | [diff] [blame] | 649 | struct spi_controller *ctrl = platform_get_drvdata(pdev); |
| 650 | struct atmel_qspi *aq = spi_controller_get_devdata(ctrl); |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 651 | |
Piotr Bugalski | 2d30ac5 | 2018-11-05 11:36:22 +0100 | [diff] [blame] | 652 | spi_unregister_controller(ctrl); |
Tudor Ambarus | c528ecf | 2020-03-20 06:51:01 +0000 | [diff] [blame] | 653 | atmel_qspi_write(QSPI_CR_QSPIDIS, aq, QSPI_CR); |
Tudor Ambarus | 2e5c888 | 2019-02-05 17:33:38 +0000 | [diff] [blame] | 654 | clk_disable_unprepare(aq->qspick); |
Tudor Ambarus | bd7905e | 2019-02-05 17:33:33 +0000 | [diff] [blame] | 655 | clk_disable_unprepare(aq->pclk); |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 656 | return 0; |
| 657 | } |
| 658 | |
Claudiu Beznea | de217c1 | 2018-06-04 11:46:33 +0300 | [diff] [blame] | 659 | static int __maybe_unused atmel_qspi_suspend(struct device *dev) |
| 660 | { |
Claudiu Beznea | e5c2749 | 2019-04-24 09:17:59 +0000 | [diff] [blame] | 661 | struct spi_controller *ctrl = dev_get_drvdata(dev); |
| 662 | struct atmel_qspi *aq = spi_controller_get_devdata(ctrl); |
Claudiu Beznea | de217c1 | 2018-06-04 11:46:33 +0300 | [diff] [blame] | 663 | |
Tudor Ambarus | 2e5c888 | 2019-02-05 17:33:38 +0000 | [diff] [blame] | 664 | clk_disable_unprepare(aq->qspick); |
Tudor Ambarus | bd7905e | 2019-02-05 17:33:33 +0000 | [diff] [blame] | 665 | clk_disable_unprepare(aq->pclk); |
Claudiu Beznea | de217c1 | 2018-06-04 11:46:33 +0300 | [diff] [blame] | 666 | |
| 667 | return 0; |
| 668 | } |
| 669 | |
| 670 | static int __maybe_unused atmel_qspi_resume(struct device *dev) |
| 671 | { |
Claudiu Beznea | e5c2749 | 2019-04-24 09:17:59 +0000 | [diff] [blame] | 672 | struct spi_controller *ctrl = dev_get_drvdata(dev); |
| 673 | struct atmel_qspi *aq = spi_controller_get_devdata(ctrl); |
Claudiu Beznea | de217c1 | 2018-06-04 11:46:33 +0300 | [diff] [blame] | 674 | |
Tudor Ambarus | bd7905e | 2019-02-05 17:33:33 +0000 | [diff] [blame] | 675 | clk_prepare_enable(aq->pclk); |
Tudor Ambarus | 2e5c888 | 2019-02-05 17:33:38 +0000 | [diff] [blame] | 676 | clk_prepare_enable(aq->qspick); |
Claudiu Beznea | de217c1 | 2018-06-04 11:46:33 +0300 | [diff] [blame] | 677 | |
Tudor Ambarus | 5b74e9a | 2019-06-28 15:30:32 +0000 | [diff] [blame] | 678 | atmel_qspi_init(aq); |
Tudor Ambarus | ab73561 | 2019-06-28 15:30:34 +0000 | [diff] [blame] | 679 | |
Tudor Ambarus | c528ecf | 2020-03-20 06:51:01 +0000 | [diff] [blame] | 680 | atmel_qspi_write(aq->scr, aq, QSPI_SCR); |
Tudor Ambarus | ab73561 | 2019-06-28 15:30:34 +0000 | [diff] [blame] | 681 | |
Tudor Ambarus | 5b74e9a | 2019-06-28 15:30:32 +0000 | [diff] [blame] | 682 | return 0; |
Claudiu Beznea | de217c1 | 2018-06-04 11:46:33 +0300 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | static SIMPLE_DEV_PM_OPS(atmel_qspi_pm_ops, atmel_qspi_suspend, |
| 686 | atmel_qspi_resume); |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 687 | |
Tudor Ambarus | 2e5c888 | 2019-02-05 17:33:38 +0000 | [diff] [blame] | 688 | static const struct atmel_qspi_caps atmel_sama5d2_qspi_caps = {}; |
| 689 | |
| 690 | static const struct atmel_qspi_caps atmel_sam9x60_qspi_caps = { |
| 691 | .has_qspick = true, |
| 692 | .has_ricr = true, |
| 693 | }; |
| 694 | |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 695 | static const struct of_device_id atmel_qspi_dt_ids[] = { |
Tudor Ambarus | 2e5c888 | 2019-02-05 17:33:38 +0000 | [diff] [blame] | 696 | { |
| 697 | .compatible = "atmel,sama5d2-qspi", |
| 698 | .data = &atmel_sama5d2_qspi_caps, |
| 699 | }, |
| 700 | { |
| 701 | .compatible = "microchip,sam9x60-qspi", |
| 702 | .data = &atmel_sam9x60_qspi_caps, |
| 703 | }, |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 704 | { /* sentinel */ } |
| 705 | }; |
| 706 | |
| 707 | MODULE_DEVICE_TABLE(of, atmel_qspi_dt_ids); |
| 708 | |
| 709 | static struct platform_driver atmel_qspi_driver = { |
| 710 | .driver = { |
| 711 | .name = "atmel_qspi", |
| 712 | .of_match_table = atmel_qspi_dt_ids, |
Claudiu Beznea | de217c1 | 2018-06-04 11:46:33 +0300 | [diff] [blame] | 713 | .pm = &atmel_qspi_pm_ops, |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 714 | }, |
| 715 | .probe = atmel_qspi_probe, |
| 716 | .remove = atmel_qspi_remove, |
| 717 | }; |
| 718 | module_platform_driver(atmel_qspi_driver); |
| 719 | |
| 720 | MODULE_AUTHOR("Cyrille Pitchen <cyrille.pitchen@atmel.com>"); |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame] | 721 | MODULE_AUTHOR("Piotr Bugalski <bugalski.piotr@gmail.com"); |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 722 | MODULE_DESCRIPTION("Atmel QSPI Controller driver"); |
| 723 | MODULE_LICENSE("GPL v2"); |