Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Driver for Atmel QSPI Controller |
| 3 | * |
| 4 | * Copyright (C) 2015 Atmel Corporation |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame^] | 5 | * Copyright (C) 2018 Cryptera A/S |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 6 | * |
| 7 | * Author: Cyrille Pitchen <cyrille.pitchen@atmel.com> |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame^] | 8 | * Author: Piotr Bugalski <bugalski.piotr@gmail.com> |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 15 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 17 | * more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along with |
| 20 | * this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | * |
| 22 | * This driver is based on drivers/mtd/spi-nor/fsl-quadspi.c from Freescale. |
| 23 | */ |
| 24 | |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/clk.h> |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/platform_device.h> |
| 29 | #include <linux/delay.h> |
| 30 | #include <linux/err.h> |
| 31 | #include <linux/interrupt.h> |
| 32 | #include <linux/mtd/mtd.h> |
| 33 | #include <linux/mtd/partitions.h> |
| 34 | #include <linux/mtd/spi-nor.h> |
| 35 | #include <linux/platform_data/atmel.h> |
| 36 | #include <linux/of.h> |
| 37 | |
| 38 | #include <linux/io.h> |
Boris Brezillon | 261b354 | 2018-07-20 11:57:41 +0200 | [diff] [blame] | 39 | #include <linux/gpio/consumer.h> |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame^] | 40 | #include <linux/spi/spi-mem.h> |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 41 | |
| 42 | /* QSPI register offsets */ |
| 43 | #define QSPI_CR 0x0000 /* Control Register */ |
| 44 | #define QSPI_MR 0x0004 /* Mode Register */ |
| 45 | #define QSPI_RD 0x0008 /* Receive Data Register */ |
| 46 | #define QSPI_TD 0x000c /* Transmit Data Register */ |
| 47 | #define QSPI_SR 0x0010 /* Status Register */ |
| 48 | #define QSPI_IER 0x0014 /* Interrupt Enable Register */ |
| 49 | #define QSPI_IDR 0x0018 /* Interrupt Disable Register */ |
| 50 | #define QSPI_IMR 0x001c /* Interrupt Mask Register */ |
| 51 | #define QSPI_SCR 0x0020 /* Serial Clock Register */ |
| 52 | |
| 53 | #define QSPI_IAR 0x0030 /* Instruction Address Register */ |
| 54 | #define QSPI_ICR 0x0034 /* Instruction Code Register */ |
| 55 | #define QSPI_IFR 0x0038 /* Instruction Frame Register */ |
| 56 | |
| 57 | #define QSPI_SMR 0x0040 /* Scrambling Mode Register */ |
| 58 | #define QSPI_SKR 0x0044 /* Scrambling Key Register */ |
| 59 | |
| 60 | #define QSPI_WPMR 0x00E4 /* Write Protection Mode Register */ |
| 61 | #define QSPI_WPSR 0x00E8 /* Write Protection Status Register */ |
| 62 | |
| 63 | #define QSPI_VERSION 0x00FC /* Version Register */ |
| 64 | |
| 65 | |
| 66 | /* Bitfields in QSPI_CR (Control Register) */ |
| 67 | #define QSPI_CR_QSPIEN BIT(0) |
| 68 | #define QSPI_CR_QSPIDIS BIT(1) |
| 69 | #define QSPI_CR_SWRST BIT(7) |
| 70 | #define QSPI_CR_LASTXFER BIT(24) |
| 71 | |
| 72 | /* Bitfields in QSPI_MR (Mode Register) */ |
Piotr Bugalski | b82ab1c | 2018-11-05 11:36:20 +0100 | [diff] [blame] | 73 | #define QSPI_MR_SMM BIT(0) |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 74 | #define QSPI_MR_LLB BIT(1) |
| 75 | #define QSPI_MR_WDRBT BIT(2) |
| 76 | #define QSPI_MR_SMRM BIT(3) |
| 77 | #define QSPI_MR_CSMODE_MASK GENMASK(5, 4) |
| 78 | #define QSPI_MR_CSMODE_NOT_RELOADED (0 << 4) |
| 79 | #define QSPI_MR_CSMODE_LASTXFER (1 << 4) |
| 80 | #define QSPI_MR_CSMODE_SYSTEMATICALLY (2 << 4) |
| 81 | #define QSPI_MR_NBBITS_MASK GENMASK(11, 8) |
| 82 | #define QSPI_MR_NBBITS(n) ((((n) - 8) << 8) & QSPI_MR_NBBITS_MASK) |
| 83 | #define QSPI_MR_DLYBCT_MASK GENMASK(23, 16) |
| 84 | #define QSPI_MR_DLYBCT(n) (((n) << 16) & QSPI_MR_DLYBCT_MASK) |
| 85 | #define QSPI_MR_DLYCS_MASK GENMASK(31, 24) |
| 86 | #define QSPI_MR_DLYCS(n) (((n) << 24) & QSPI_MR_DLYCS_MASK) |
| 87 | |
| 88 | /* Bitfields in QSPI_SR/QSPI_IER/QSPI_IDR/QSPI_IMR */ |
| 89 | #define QSPI_SR_RDRF BIT(0) |
| 90 | #define QSPI_SR_TDRE BIT(1) |
| 91 | #define QSPI_SR_TXEMPTY BIT(2) |
| 92 | #define QSPI_SR_OVRES BIT(3) |
| 93 | #define QSPI_SR_CSR BIT(8) |
| 94 | #define QSPI_SR_CSS BIT(9) |
| 95 | #define QSPI_SR_INSTRE BIT(10) |
| 96 | #define QSPI_SR_QSPIENS BIT(24) |
| 97 | |
| 98 | #define QSPI_SR_CMD_COMPLETED (QSPI_SR_INSTRE | QSPI_SR_CSR) |
| 99 | |
| 100 | /* Bitfields in QSPI_SCR (Serial Clock Register) */ |
| 101 | #define QSPI_SCR_CPOL BIT(0) |
| 102 | #define QSPI_SCR_CPHA BIT(1) |
| 103 | #define QSPI_SCR_SCBR_MASK GENMASK(15, 8) |
| 104 | #define QSPI_SCR_SCBR(n) (((n) << 8) & QSPI_SCR_SCBR_MASK) |
| 105 | #define QSPI_SCR_DLYBS_MASK GENMASK(23, 16) |
| 106 | #define QSPI_SCR_DLYBS(n) (((n) << 16) & QSPI_SCR_DLYBS_MASK) |
| 107 | |
| 108 | /* Bitfields in QSPI_ICR (Instruction Code Register) */ |
| 109 | #define QSPI_ICR_INST_MASK GENMASK(7, 0) |
| 110 | #define QSPI_ICR_INST(inst) (((inst) << 0) & QSPI_ICR_INST_MASK) |
| 111 | #define QSPI_ICR_OPT_MASK GENMASK(23, 16) |
| 112 | #define QSPI_ICR_OPT(opt) (((opt) << 16) & QSPI_ICR_OPT_MASK) |
| 113 | |
| 114 | /* Bitfields in QSPI_IFR (Instruction Frame Register) */ |
| 115 | #define QSPI_IFR_WIDTH_MASK GENMASK(2, 0) |
| 116 | #define QSPI_IFR_WIDTH_SINGLE_BIT_SPI (0 << 0) |
| 117 | #define QSPI_IFR_WIDTH_DUAL_OUTPUT (1 << 0) |
| 118 | #define QSPI_IFR_WIDTH_QUAD_OUTPUT (2 << 0) |
| 119 | #define QSPI_IFR_WIDTH_DUAL_IO (3 << 0) |
| 120 | #define QSPI_IFR_WIDTH_QUAD_IO (4 << 0) |
| 121 | #define QSPI_IFR_WIDTH_DUAL_CMD (5 << 0) |
| 122 | #define QSPI_IFR_WIDTH_QUAD_CMD (6 << 0) |
| 123 | #define QSPI_IFR_INSTEN BIT(4) |
| 124 | #define QSPI_IFR_ADDREN BIT(5) |
| 125 | #define QSPI_IFR_OPTEN BIT(6) |
| 126 | #define QSPI_IFR_DATAEN BIT(7) |
| 127 | #define QSPI_IFR_OPTL_MASK GENMASK(9, 8) |
| 128 | #define QSPI_IFR_OPTL_1BIT (0 << 8) |
| 129 | #define QSPI_IFR_OPTL_2BIT (1 << 8) |
| 130 | #define QSPI_IFR_OPTL_4BIT (2 << 8) |
| 131 | #define QSPI_IFR_OPTL_8BIT (3 << 8) |
| 132 | #define QSPI_IFR_ADDRL BIT(10) |
| 133 | #define QSPI_IFR_TFRTYP_MASK GENMASK(13, 12) |
| 134 | #define QSPI_IFR_TFRTYP_TRSFR_READ (0 << 12) |
| 135 | #define QSPI_IFR_TFRTYP_TRSFR_READ_MEM (1 << 12) |
| 136 | #define QSPI_IFR_TFRTYP_TRSFR_WRITE (2 << 12) |
| 137 | #define QSPI_IFR_TFRTYP_TRSFR_WRITE_MEM (3 << 13) |
| 138 | #define QSPI_IFR_CRM BIT(14) |
| 139 | #define QSPI_IFR_NBDUM_MASK GENMASK(20, 16) |
| 140 | #define QSPI_IFR_NBDUM(n) (((n) << 16) & QSPI_IFR_NBDUM_MASK) |
| 141 | |
| 142 | /* Bitfields in QSPI_SMR (Scrambling Mode Register) */ |
| 143 | #define QSPI_SMR_SCREN BIT(0) |
| 144 | #define QSPI_SMR_RVDIS BIT(1) |
| 145 | |
| 146 | /* Bitfields in QSPI_WPMR (Write Protection Mode Register) */ |
| 147 | #define QSPI_WPMR_WPEN BIT(0) |
| 148 | #define QSPI_WPMR_WPKEY_MASK GENMASK(31, 8) |
| 149 | #define QSPI_WPMR_WPKEY(wpkey) (((wpkey) << 8) & QSPI_WPMR_WPKEY_MASK) |
| 150 | |
| 151 | /* Bitfields in QSPI_WPSR (Write Protection Status Register) */ |
| 152 | #define QSPI_WPSR_WPVS BIT(0) |
| 153 | #define QSPI_WPSR_WPVSRC_MASK GENMASK(15, 8) |
| 154 | #define QSPI_WPSR_WPVSRC(src) (((src) << 8) & QSPI_WPSR_WPVSRC) |
| 155 | |
| 156 | |
| 157 | struct atmel_qspi { |
| 158 | void __iomem *regs; |
| 159 | void __iomem *mem; |
| 160 | struct clk *clk; |
| 161 | struct platform_device *pdev; |
| 162 | u32 pending; |
| 163 | |
| 164 | struct spi_nor nor; |
| 165 | u32 clk_rate; |
| 166 | struct completion cmd_completion; |
| 167 | }; |
| 168 | |
| 169 | struct atmel_qspi_command { |
| 170 | union { |
| 171 | struct { |
| 172 | u32 instruction:1; |
| 173 | u32 address:3; |
| 174 | u32 mode:1; |
| 175 | u32 dummy:1; |
| 176 | u32 data:1; |
| 177 | u32 reserved:25; |
| 178 | } bits; |
| 179 | u32 word; |
| 180 | } enable; |
| 181 | u8 instruction; |
| 182 | u8 mode; |
| 183 | u8 num_mode_cycles; |
| 184 | u8 num_dummy_cycles; |
| 185 | u32 address; |
| 186 | |
| 187 | size_t buf_len; |
| 188 | const void *tx_buf; |
| 189 | void *rx_buf; |
| 190 | }; |
| 191 | |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame^] | 192 | struct qspi_mode { |
| 193 | u8 cmd_buswidth; |
| 194 | u8 addr_buswidth; |
| 195 | u8 data_buswidth; |
| 196 | u32 config; |
| 197 | }; |
| 198 | |
| 199 | static const struct qspi_mode sama5d2_qspi_modes[] = { |
| 200 | { 1, 1, 1, QSPI_IFR_WIDTH_SINGLE_BIT_SPI }, |
| 201 | { 1, 1, 2, QSPI_IFR_WIDTH_DUAL_OUTPUT }, |
| 202 | { 1, 1, 4, QSPI_IFR_WIDTH_QUAD_OUTPUT }, |
| 203 | { 1, 2, 2, QSPI_IFR_WIDTH_DUAL_IO }, |
| 204 | { 1, 4, 4, QSPI_IFR_WIDTH_QUAD_IO }, |
| 205 | { 2, 2, 2, QSPI_IFR_WIDTH_DUAL_CMD }, |
| 206 | { 4, 4, 4, QSPI_IFR_WIDTH_QUAD_CMD }, |
| 207 | }; |
| 208 | |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 209 | /* Register access functions */ |
| 210 | static inline u32 qspi_readl(struct atmel_qspi *aq, u32 reg) |
| 211 | { |
| 212 | return readl_relaxed(aq->regs + reg); |
| 213 | } |
| 214 | |
| 215 | static inline void qspi_writel(struct atmel_qspi *aq, u32 reg, u32 value) |
| 216 | { |
| 217 | writel_relaxed(value, aq->regs + reg); |
| 218 | } |
| 219 | |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame^] | 220 | static inline bool is_compatible(const struct spi_mem_op *op, |
| 221 | const struct qspi_mode *mode) |
| 222 | { |
| 223 | if (op->cmd.buswidth != mode->cmd_buswidth) |
| 224 | return false; |
| 225 | |
| 226 | if (op->addr.nbytes && op->addr.buswidth != mode->addr_buswidth) |
| 227 | return false; |
| 228 | |
| 229 | if (op->data.nbytes && op->data.buswidth != mode->data_buswidth) |
| 230 | return false; |
| 231 | |
| 232 | return true; |
| 233 | } |
| 234 | |
| 235 | static int find_mode(const struct spi_mem_op *op) |
| 236 | { |
| 237 | u32 i; |
| 238 | |
| 239 | for (i = 0; i < ARRAY_SIZE(sama5d2_qspi_modes); i++) |
| 240 | if (is_compatible(op, &sama5d2_qspi_modes[i])) |
| 241 | return i; |
| 242 | |
| 243 | return -1; |
| 244 | } |
| 245 | |
| 246 | static bool atmel_qspi_supports_op(struct spi_mem *mem, |
| 247 | const struct spi_mem_op *op) |
| 248 | { |
| 249 | if (find_mode(op) < 0) |
| 250 | return false; |
| 251 | |
| 252 | /* special case not supported by hardware */ |
| 253 | if (op->addr.nbytes == 2 && op->cmd.buswidth != op->addr.buswidth && |
| 254 | op->dummy.nbytes == 0) |
| 255 | return false; |
| 256 | |
| 257 | return true; |
| 258 | } |
| 259 | |
| 260 | static int atmel_qspi_exec_op(struct spi_mem *mem, const struct spi_mem_op *op) |
| 261 | { |
| 262 | struct atmel_qspi *aq = spi_controller_get_devdata(mem->spi->master); |
| 263 | int mode; |
| 264 | u32 dummy_cycles = 0; |
| 265 | u32 iar, icr, ifr, sr; |
| 266 | int err = 0; |
| 267 | |
| 268 | iar = 0; |
| 269 | icr = QSPI_ICR_INST(op->cmd.opcode); |
| 270 | ifr = QSPI_IFR_INSTEN; |
| 271 | |
| 272 | qspi_writel(aq, QSPI_MR, QSPI_MR_SMM); |
| 273 | |
| 274 | mode = find_mode(op); |
| 275 | if (mode < 0) |
| 276 | return -ENOTSUPP; |
| 277 | |
| 278 | ifr |= sama5d2_qspi_modes[mode].config; |
| 279 | |
| 280 | if (op->dummy.buswidth && op->dummy.nbytes) |
| 281 | dummy_cycles = op->dummy.nbytes * 8 / op->dummy.buswidth; |
| 282 | |
| 283 | if (op->addr.buswidth) { |
| 284 | switch (op->addr.nbytes) { |
| 285 | case 0: |
| 286 | break; |
| 287 | case 1: |
| 288 | ifr |= QSPI_IFR_OPTEN | QSPI_IFR_OPTL_8BIT; |
| 289 | icr |= QSPI_ICR_OPT(op->addr.val & 0xff); |
| 290 | break; |
| 291 | case 2: |
| 292 | if (dummy_cycles < 8 / op->addr.buswidth) { |
| 293 | ifr &= ~QSPI_IFR_INSTEN; |
| 294 | ifr |= QSPI_IFR_ADDREN; |
| 295 | iar = (op->cmd.opcode << 16) | |
| 296 | (op->addr.val & 0xffff); |
| 297 | } else { |
| 298 | ifr |= QSPI_IFR_ADDREN; |
| 299 | iar = (op->addr.val << 8) & 0xffffff; |
| 300 | dummy_cycles -= 8 / op->addr.buswidth; |
| 301 | } |
| 302 | break; |
| 303 | case 3: |
| 304 | ifr |= QSPI_IFR_ADDREN; |
| 305 | iar = op->addr.val & 0xffffff; |
| 306 | break; |
| 307 | case 4: |
| 308 | ifr |= QSPI_IFR_ADDREN | QSPI_IFR_ADDRL; |
| 309 | iar = op->addr.val & 0x7ffffff; |
| 310 | break; |
| 311 | default: |
| 312 | return -ENOTSUPP; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | /* Set number of dummy cycles */ |
| 317 | if (dummy_cycles) |
| 318 | ifr |= QSPI_IFR_NBDUM(dummy_cycles); |
| 319 | |
| 320 | /* Set data enable */ |
| 321 | if (op->data.nbytes) |
| 322 | ifr |= QSPI_IFR_DATAEN; |
| 323 | |
| 324 | if (op->data.dir == SPI_MEM_DATA_IN && op->data.nbytes) |
| 325 | ifr |= QSPI_IFR_TFRTYP_TRSFR_READ; |
| 326 | else |
| 327 | ifr |= QSPI_IFR_TFRTYP_TRSFR_WRITE; |
| 328 | |
| 329 | /* Clear pending interrupts */ |
| 330 | (void)qspi_readl(aq, QSPI_SR); |
| 331 | |
| 332 | /* Set QSPI Instruction Frame registers */ |
| 333 | qspi_writel(aq, QSPI_IAR, iar); |
| 334 | qspi_writel(aq, QSPI_ICR, icr); |
| 335 | qspi_writel(aq, QSPI_IFR, ifr); |
| 336 | |
| 337 | /* Skip to the final steps if there is no data */ |
| 338 | if (op->data.nbytes) { |
| 339 | /* Dummy read of QSPI_IFR to synchronize APB and AHB accesses */ |
| 340 | (void)qspi_readl(aq, QSPI_IFR); |
| 341 | |
| 342 | /* Send/Receive data */ |
| 343 | if (op->data.dir == SPI_MEM_DATA_IN) |
| 344 | _memcpy_fromio(op->data.buf.in, |
| 345 | aq->mem + iar, op->data.nbytes); |
| 346 | else |
| 347 | _memcpy_toio(aq->mem + iar, |
| 348 | op->data.buf.out, op->data.nbytes); |
| 349 | |
| 350 | /* Release the chip-select */ |
| 351 | qspi_writel(aq, QSPI_CR, QSPI_CR_LASTXFER); |
| 352 | } |
| 353 | |
| 354 | /* Poll INSTRuction End status */ |
| 355 | sr = qspi_readl(aq, QSPI_SR); |
| 356 | if ((sr & QSPI_SR_CMD_COMPLETED) == QSPI_SR_CMD_COMPLETED) |
| 357 | return err; |
| 358 | |
| 359 | /* Wait for INSTRuction End interrupt */ |
| 360 | reinit_completion(&aq->cmd_completion); |
| 361 | aq->pending = sr & QSPI_SR_CMD_COMPLETED; |
| 362 | qspi_writel(aq, QSPI_IER, QSPI_SR_CMD_COMPLETED); |
| 363 | if (!wait_for_completion_timeout(&aq->cmd_completion, |
| 364 | msecs_to_jiffies(1000))) |
| 365 | err = -ETIMEDOUT; |
| 366 | qspi_writel(aq, QSPI_IDR, QSPI_SR_CMD_COMPLETED); |
| 367 | |
| 368 | return err; |
| 369 | } |
| 370 | |
| 371 | const char *atmel_qspi_get_name(struct spi_mem *spimem) |
| 372 | { |
| 373 | return dev_name(spimem->spi->dev.parent); |
| 374 | } |
| 375 | |
| 376 | static const struct spi_controller_mem_ops atmel_qspi_mem_ops = { |
| 377 | .supports_op = atmel_qspi_supports_op, |
| 378 | .exec_op = atmel_qspi_exec_op, |
| 379 | .get_name = atmel_qspi_get_name |
| 380 | }; |
| 381 | |
| 382 | static int atmel_qspi_setup(struct spi_device *spi) |
| 383 | { |
| 384 | struct spi_controller *ctrl = spi->master; |
| 385 | struct atmel_qspi *aq = spi_controller_get_devdata(ctrl); |
| 386 | unsigned long src_rate; |
| 387 | u32 scr, scbr; |
| 388 | |
| 389 | if (ctrl->busy) |
| 390 | return -EBUSY; |
| 391 | |
| 392 | if (!spi->max_speed_hz) |
| 393 | return -EINVAL; |
| 394 | |
| 395 | src_rate = clk_get_rate(aq->clk); |
| 396 | if (!src_rate) |
| 397 | return -EINVAL; |
| 398 | |
| 399 | /* Compute the QSPI baudrate */ |
| 400 | scbr = DIV_ROUND_UP(src_rate, spi->max_speed_hz); |
| 401 | if (scbr > 0) |
| 402 | scbr--; |
| 403 | |
| 404 | scr = QSPI_SCR_SCBR(scbr); |
| 405 | qspi_writel(aq, QSPI_SCR, scr); |
| 406 | |
| 407 | return 0; |
| 408 | } |
| 409 | |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 410 | static int atmel_qspi_run_transfer(struct atmel_qspi *aq, |
| 411 | const struct atmel_qspi_command *cmd) |
| 412 | { |
| 413 | void __iomem *ahb_mem; |
| 414 | |
| 415 | /* Then fallback to a PIO transfer (memcpy() DOES NOT work!) */ |
| 416 | ahb_mem = aq->mem; |
| 417 | if (cmd->enable.bits.address) |
| 418 | ahb_mem += cmd->address; |
| 419 | if (cmd->tx_buf) |
| 420 | _memcpy_toio(ahb_mem, cmd->tx_buf, cmd->buf_len); |
| 421 | else |
| 422 | _memcpy_fromio(cmd->rx_buf, ahb_mem, cmd->buf_len); |
| 423 | |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | #ifdef DEBUG |
| 428 | static void atmel_qspi_debug_command(struct atmel_qspi *aq, |
| 429 | const struct atmel_qspi_command *cmd, |
| 430 | u32 ifr) |
| 431 | { |
| 432 | u8 cmd_buf[SPI_NOR_MAX_CMD_SIZE]; |
| 433 | size_t len = 0; |
| 434 | int i; |
| 435 | |
| 436 | if (cmd->enable.bits.instruction) |
| 437 | cmd_buf[len++] = cmd->instruction; |
| 438 | |
| 439 | for (i = cmd->enable.bits.address-1; i >= 0; --i) |
| 440 | cmd_buf[len++] = (cmd->address >> (i << 3)) & 0xff; |
| 441 | |
| 442 | if (cmd->enable.bits.mode) |
| 443 | cmd_buf[len++] = cmd->mode; |
| 444 | |
| 445 | if (cmd->enable.bits.dummy) { |
| 446 | int num = cmd->num_dummy_cycles; |
| 447 | |
| 448 | switch (ifr & QSPI_IFR_WIDTH_MASK) { |
| 449 | case QSPI_IFR_WIDTH_SINGLE_BIT_SPI: |
| 450 | case QSPI_IFR_WIDTH_DUAL_OUTPUT: |
| 451 | case QSPI_IFR_WIDTH_QUAD_OUTPUT: |
| 452 | num >>= 3; |
| 453 | break; |
| 454 | case QSPI_IFR_WIDTH_DUAL_IO: |
| 455 | case QSPI_IFR_WIDTH_DUAL_CMD: |
| 456 | num >>= 2; |
| 457 | break; |
| 458 | case QSPI_IFR_WIDTH_QUAD_IO: |
| 459 | case QSPI_IFR_WIDTH_QUAD_CMD: |
| 460 | num >>= 1; |
| 461 | break; |
| 462 | default: |
| 463 | return; |
| 464 | } |
| 465 | |
| 466 | for (i = 0; i < num; ++i) |
| 467 | cmd_buf[len++] = 0; |
| 468 | } |
| 469 | |
| 470 | /* Dump the SPI command */ |
| 471 | print_hex_dump(KERN_DEBUG, "qspi cmd: ", DUMP_PREFIX_NONE, |
| 472 | 32, 1, cmd_buf, len, false); |
| 473 | |
| 474 | #ifdef VERBOSE_DEBUG |
| 475 | /* If verbose debug is enabled, also dump the TX data */ |
| 476 | if (cmd->enable.bits.data && cmd->tx_buf) |
| 477 | print_hex_dump(KERN_DEBUG, "qspi tx : ", DUMP_PREFIX_NONE, |
| 478 | 32, 1, cmd->tx_buf, cmd->buf_len, false); |
| 479 | #endif |
| 480 | } |
| 481 | #else |
| 482 | #define atmel_qspi_debug_command(aq, cmd, ifr) |
| 483 | #endif |
| 484 | |
| 485 | static int atmel_qspi_run_command(struct atmel_qspi *aq, |
| 486 | const struct atmel_qspi_command *cmd, |
Cyrille Pitchen | cfc5604 | 2017-04-25 22:08:46 +0200 | [diff] [blame] | 487 | u32 ifr_tfrtyp, enum spi_nor_protocol proto) |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 488 | { |
| 489 | u32 iar, icr, ifr, sr; |
| 490 | int err = 0; |
| 491 | |
| 492 | iar = 0; |
| 493 | icr = 0; |
Cyrille Pitchen | cfc5604 | 2017-04-25 22:08:46 +0200 | [diff] [blame] | 494 | ifr = ifr_tfrtyp; |
| 495 | |
| 496 | /* Set the SPI protocol */ |
| 497 | switch (proto) { |
| 498 | case SNOR_PROTO_1_1_1: |
| 499 | ifr |= QSPI_IFR_WIDTH_SINGLE_BIT_SPI; |
| 500 | break; |
| 501 | |
| 502 | case SNOR_PROTO_1_1_2: |
| 503 | ifr |= QSPI_IFR_WIDTH_DUAL_OUTPUT; |
| 504 | break; |
| 505 | |
| 506 | case SNOR_PROTO_1_1_4: |
| 507 | ifr |= QSPI_IFR_WIDTH_QUAD_OUTPUT; |
| 508 | break; |
| 509 | |
| 510 | case SNOR_PROTO_1_2_2: |
| 511 | ifr |= QSPI_IFR_WIDTH_DUAL_IO; |
| 512 | break; |
| 513 | |
| 514 | case SNOR_PROTO_1_4_4: |
| 515 | ifr |= QSPI_IFR_WIDTH_QUAD_IO; |
| 516 | break; |
| 517 | |
| 518 | case SNOR_PROTO_2_2_2: |
| 519 | ifr |= QSPI_IFR_WIDTH_DUAL_CMD; |
| 520 | break; |
| 521 | |
| 522 | case SNOR_PROTO_4_4_4: |
| 523 | ifr |= QSPI_IFR_WIDTH_QUAD_CMD; |
| 524 | break; |
| 525 | |
| 526 | default: |
| 527 | return -EINVAL; |
| 528 | } |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 529 | |
| 530 | /* Compute instruction parameters */ |
| 531 | if (cmd->enable.bits.instruction) { |
| 532 | icr |= QSPI_ICR_INST(cmd->instruction); |
| 533 | ifr |= QSPI_IFR_INSTEN; |
| 534 | } |
| 535 | |
| 536 | /* Compute address parameters */ |
| 537 | switch (cmd->enable.bits.address) { |
| 538 | case 4: |
| 539 | ifr |= QSPI_IFR_ADDRL; |
| 540 | /* fall through to the 24bit (3 byte) address case. */ |
| 541 | case 3: |
| 542 | iar = (cmd->enable.bits.data) ? 0 : cmd->address; |
| 543 | ifr |= QSPI_IFR_ADDREN; |
| 544 | break; |
| 545 | case 0: |
| 546 | break; |
| 547 | default: |
| 548 | return -EINVAL; |
| 549 | } |
| 550 | |
| 551 | /* Compute option parameters */ |
| 552 | if (cmd->enable.bits.mode && cmd->num_mode_cycles) { |
| 553 | u32 mode_cycle_bits, mode_bits; |
| 554 | |
| 555 | icr |= QSPI_ICR_OPT(cmd->mode); |
| 556 | ifr |= QSPI_IFR_OPTEN; |
| 557 | |
| 558 | switch (ifr & QSPI_IFR_WIDTH_MASK) { |
| 559 | case QSPI_IFR_WIDTH_SINGLE_BIT_SPI: |
| 560 | case QSPI_IFR_WIDTH_DUAL_OUTPUT: |
| 561 | case QSPI_IFR_WIDTH_QUAD_OUTPUT: |
| 562 | mode_cycle_bits = 1; |
| 563 | break; |
| 564 | case QSPI_IFR_WIDTH_DUAL_IO: |
| 565 | case QSPI_IFR_WIDTH_DUAL_CMD: |
| 566 | mode_cycle_bits = 2; |
| 567 | break; |
| 568 | case QSPI_IFR_WIDTH_QUAD_IO: |
| 569 | case QSPI_IFR_WIDTH_QUAD_CMD: |
| 570 | mode_cycle_bits = 4; |
| 571 | break; |
| 572 | default: |
| 573 | return -EINVAL; |
| 574 | } |
| 575 | |
| 576 | mode_bits = cmd->num_mode_cycles * mode_cycle_bits; |
| 577 | switch (mode_bits) { |
| 578 | case 1: |
| 579 | ifr |= QSPI_IFR_OPTL_1BIT; |
| 580 | break; |
| 581 | |
| 582 | case 2: |
| 583 | ifr |= QSPI_IFR_OPTL_2BIT; |
| 584 | break; |
| 585 | |
| 586 | case 4: |
| 587 | ifr |= QSPI_IFR_OPTL_4BIT; |
| 588 | break; |
| 589 | |
| 590 | case 8: |
| 591 | ifr |= QSPI_IFR_OPTL_8BIT; |
| 592 | break; |
| 593 | |
| 594 | default: |
| 595 | return -EINVAL; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | /* Set number of dummy cycles */ |
| 600 | if (cmd->enable.bits.dummy) |
| 601 | ifr |= QSPI_IFR_NBDUM(cmd->num_dummy_cycles); |
| 602 | |
| 603 | /* Set data enable */ |
| 604 | if (cmd->enable.bits.data) { |
| 605 | ifr |= QSPI_IFR_DATAEN; |
| 606 | |
| 607 | /* Special case for Continuous Read Mode */ |
| 608 | if (!cmd->tx_buf && !cmd->rx_buf) |
| 609 | ifr |= QSPI_IFR_CRM; |
| 610 | } |
| 611 | |
| 612 | /* Clear pending interrupts */ |
| 613 | (void)qspi_readl(aq, QSPI_SR); |
| 614 | |
| 615 | /* Set QSPI Instruction Frame registers */ |
| 616 | atmel_qspi_debug_command(aq, cmd, ifr); |
| 617 | qspi_writel(aq, QSPI_IAR, iar); |
| 618 | qspi_writel(aq, QSPI_ICR, icr); |
| 619 | qspi_writel(aq, QSPI_IFR, ifr); |
| 620 | |
| 621 | /* Skip to the final steps if there is no data */ |
| 622 | if (!cmd->enable.bits.data) |
| 623 | goto no_data; |
| 624 | |
| 625 | /* Dummy read of QSPI_IFR to synchronize APB and AHB accesses */ |
| 626 | (void)qspi_readl(aq, QSPI_IFR); |
| 627 | |
| 628 | /* Stop here for continuous read */ |
| 629 | if (!cmd->tx_buf && !cmd->rx_buf) |
| 630 | return 0; |
| 631 | /* Send/Receive data */ |
| 632 | err = atmel_qspi_run_transfer(aq, cmd); |
| 633 | |
| 634 | /* Release the chip-select */ |
| 635 | qspi_writel(aq, QSPI_CR, QSPI_CR_LASTXFER); |
| 636 | |
| 637 | if (err) |
| 638 | return err; |
| 639 | |
| 640 | #if defined(DEBUG) && defined(VERBOSE_DEBUG) |
| 641 | /* |
| 642 | * If verbose debug is enabled, also dump the RX data in addition to |
| 643 | * the SPI command previously dumped by atmel_qspi_debug_command() |
| 644 | */ |
| 645 | if (cmd->rx_buf) |
| 646 | print_hex_dump(KERN_DEBUG, "qspi rx : ", DUMP_PREFIX_NONE, |
| 647 | 32, 1, cmd->rx_buf, cmd->buf_len, false); |
| 648 | #endif |
| 649 | no_data: |
| 650 | /* Poll INSTRuction End status */ |
| 651 | sr = qspi_readl(aq, QSPI_SR); |
| 652 | if ((sr & QSPI_SR_CMD_COMPLETED) == QSPI_SR_CMD_COMPLETED) |
| 653 | return err; |
| 654 | |
| 655 | /* Wait for INSTRuction End interrupt */ |
| 656 | reinit_completion(&aq->cmd_completion); |
| 657 | aq->pending = sr & QSPI_SR_CMD_COMPLETED; |
| 658 | qspi_writel(aq, QSPI_IER, QSPI_SR_CMD_COMPLETED); |
| 659 | if (!wait_for_completion_timeout(&aq->cmd_completion, |
| 660 | msecs_to_jiffies(1000))) |
| 661 | err = -ETIMEDOUT; |
| 662 | qspi_writel(aq, QSPI_IDR, QSPI_SR_CMD_COMPLETED); |
| 663 | |
| 664 | return err; |
| 665 | } |
| 666 | |
| 667 | static int atmel_qspi_read_reg(struct spi_nor *nor, u8 opcode, |
| 668 | u8 *buf, int len) |
| 669 | { |
| 670 | struct atmel_qspi *aq = nor->priv; |
| 671 | struct atmel_qspi_command cmd; |
| 672 | |
| 673 | memset(&cmd, 0, sizeof(cmd)); |
| 674 | cmd.enable.bits.instruction = 1; |
| 675 | cmd.enable.bits.data = 1; |
| 676 | cmd.instruction = opcode; |
| 677 | cmd.rx_buf = buf; |
| 678 | cmd.buf_len = len; |
| 679 | return atmel_qspi_run_command(aq, &cmd, QSPI_IFR_TFRTYP_TRSFR_READ, |
Cyrille Pitchen | cfc5604 | 2017-04-25 22:08:46 +0200 | [diff] [blame] | 680 | nor->reg_proto); |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | static int atmel_qspi_write_reg(struct spi_nor *nor, u8 opcode, |
| 684 | u8 *buf, int len) |
| 685 | { |
| 686 | struct atmel_qspi *aq = nor->priv; |
| 687 | struct atmel_qspi_command cmd; |
| 688 | |
| 689 | memset(&cmd, 0, sizeof(cmd)); |
| 690 | cmd.enable.bits.instruction = 1; |
| 691 | cmd.enable.bits.data = (buf != NULL && len > 0); |
| 692 | cmd.instruction = opcode; |
| 693 | cmd.tx_buf = buf; |
| 694 | cmd.buf_len = len; |
| 695 | return atmel_qspi_run_command(aq, &cmd, QSPI_IFR_TFRTYP_TRSFR_WRITE, |
Cyrille Pitchen | cfc5604 | 2017-04-25 22:08:46 +0200 | [diff] [blame] | 696 | nor->reg_proto); |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | static ssize_t atmel_qspi_write(struct spi_nor *nor, loff_t to, size_t len, |
| 700 | const u_char *write_buf) |
| 701 | { |
| 702 | struct atmel_qspi *aq = nor->priv; |
| 703 | struct atmel_qspi_command cmd; |
| 704 | ssize_t ret; |
| 705 | |
| 706 | memset(&cmd, 0, sizeof(cmd)); |
| 707 | cmd.enable.bits.instruction = 1; |
| 708 | cmd.enable.bits.address = nor->addr_width; |
| 709 | cmd.enable.bits.data = 1; |
| 710 | cmd.instruction = nor->program_opcode; |
| 711 | cmd.address = (u32)to; |
| 712 | cmd.tx_buf = write_buf; |
| 713 | cmd.buf_len = len; |
| 714 | ret = atmel_qspi_run_command(aq, &cmd, QSPI_IFR_TFRTYP_TRSFR_WRITE_MEM, |
Cyrille Pitchen | cfc5604 | 2017-04-25 22:08:46 +0200 | [diff] [blame] | 715 | nor->write_proto); |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 716 | return (ret < 0) ? ret : len; |
| 717 | } |
| 718 | |
| 719 | static int atmel_qspi_erase(struct spi_nor *nor, loff_t offs) |
| 720 | { |
| 721 | struct atmel_qspi *aq = nor->priv; |
| 722 | struct atmel_qspi_command cmd; |
| 723 | |
| 724 | memset(&cmd, 0, sizeof(cmd)); |
| 725 | cmd.enable.bits.instruction = 1; |
| 726 | cmd.enable.bits.address = nor->addr_width; |
| 727 | cmd.instruction = nor->erase_opcode; |
| 728 | cmd.address = (u32)offs; |
| 729 | return atmel_qspi_run_command(aq, &cmd, QSPI_IFR_TFRTYP_TRSFR_WRITE, |
Cyrille Pitchen | cfc5604 | 2017-04-25 22:08:46 +0200 | [diff] [blame] | 730 | nor->reg_proto); |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | static ssize_t atmel_qspi_read(struct spi_nor *nor, loff_t from, size_t len, |
| 734 | u_char *read_buf) |
| 735 | { |
| 736 | struct atmel_qspi *aq = nor->priv; |
| 737 | struct atmel_qspi_command cmd; |
| 738 | u8 num_mode_cycles, num_dummy_cycles; |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 739 | ssize_t ret; |
| 740 | |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 741 | if (nor->read_dummy >= 2) { |
| 742 | num_mode_cycles = 2; |
| 743 | num_dummy_cycles = nor->read_dummy - 2; |
| 744 | } else { |
| 745 | num_mode_cycles = nor->read_dummy; |
| 746 | num_dummy_cycles = 0; |
| 747 | } |
| 748 | |
| 749 | memset(&cmd, 0, sizeof(cmd)); |
| 750 | cmd.enable.bits.instruction = 1; |
| 751 | cmd.enable.bits.address = nor->addr_width; |
| 752 | cmd.enable.bits.mode = (num_mode_cycles > 0); |
| 753 | cmd.enable.bits.dummy = (num_dummy_cycles > 0); |
| 754 | cmd.enable.bits.data = 1; |
| 755 | cmd.instruction = nor->read_opcode; |
| 756 | cmd.address = (u32)from; |
| 757 | cmd.mode = 0xff; /* This value prevents from entering the 0-4-4 mode */ |
| 758 | cmd.num_mode_cycles = num_mode_cycles; |
| 759 | cmd.num_dummy_cycles = num_dummy_cycles; |
| 760 | cmd.rx_buf = read_buf; |
| 761 | cmd.buf_len = len; |
| 762 | ret = atmel_qspi_run_command(aq, &cmd, QSPI_IFR_TFRTYP_TRSFR_READ_MEM, |
Cyrille Pitchen | cfc5604 | 2017-04-25 22:08:46 +0200 | [diff] [blame] | 763 | nor->read_proto); |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 764 | return (ret < 0) ? ret : len; |
| 765 | } |
| 766 | |
| 767 | static int atmel_qspi_init(struct atmel_qspi *aq) |
| 768 | { |
| 769 | unsigned long src_rate; |
| 770 | u32 mr, scr, scbr; |
| 771 | |
| 772 | /* Reset the QSPI controller */ |
| 773 | qspi_writel(aq, QSPI_CR, QSPI_CR_SWRST); |
| 774 | |
| 775 | /* Set the QSPI controller in Serial Memory Mode */ |
Piotr Bugalski | b82ab1c | 2018-11-05 11:36:20 +0100 | [diff] [blame] | 776 | mr = QSPI_MR_NBBITS(8) | QSPI_MR_SMM; |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 777 | qspi_writel(aq, QSPI_MR, mr); |
| 778 | |
| 779 | src_rate = clk_get_rate(aq->clk); |
| 780 | if (!src_rate) |
| 781 | return -EINVAL; |
| 782 | |
| 783 | /* Compute the QSPI baudrate */ |
| 784 | scbr = DIV_ROUND_UP(src_rate, aq->clk_rate); |
| 785 | if (scbr > 0) |
| 786 | scbr--; |
| 787 | scr = QSPI_SCR_SCBR(scbr); |
| 788 | qspi_writel(aq, QSPI_SCR, scr); |
| 789 | |
| 790 | /* Enable the QSPI controller */ |
| 791 | qspi_writel(aq, QSPI_CR, QSPI_CR_QSPIEN); |
| 792 | |
| 793 | return 0; |
| 794 | } |
| 795 | |
| 796 | static irqreturn_t atmel_qspi_interrupt(int irq, void *dev_id) |
| 797 | { |
| 798 | struct atmel_qspi *aq = (struct atmel_qspi *)dev_id; |
| 799 | u32 status, mask, pending; |
| 800 | |
| 801 | status = qspi_readl(aq, QSPI_SR); |
| 802 | mask = qspi_readl(aq, QSPI_IMR); |
| 803 | pending = status & mask; |
| 804 | |
| 805 | if (!pending) |
| 806 | return IRQ_NONE; |
| 807 | |
| 808 | aq->pending |= pending; |
| 809 | if ((aq->pending & QSPI_SR_CMD_COMPLETED) == QSPI_SR_CMD_COMPLETED) |
| 810 | complete(&aq->cmd_completion); |
| 811 | |
| 812 | return IRQ_HANDLED; |
| 813 | } |
| 814 | |
| 815 | static int atmel_qspi_probe(struct platform_device *pdev) |
| 816 | { |
Cyrille Pitchen | cfc5604 | 2017-04-25 22:08:46 +0200 | [diff] [blame] | 817 | const struct spi_nor_hwcaps hwcaps = { |
| 818 | .mask = SNOR_HWCAPS_READ | |
| 819 | SNOR_HWCAPS_READ_FAST | |
| 820 | SNOR_HWCAPS_READ_1_1_2 | |
| 821 | SNOR_HWCAPS_READ_1_2_2 | |
| 822 | SNOR_HWCAPS_READ_2_2_2 | |
| 823 | SNOR_HWCAPS_READ_1_1_4 | |
| 824 | SNOR_HWCAPS_READ_1_4_4 | |
| 825 | SNOR_HWCAPS_READ_4_4_4 | |
| 826 | SNOR_HWCAPS_PP | |
| 827 | SNOR_HWCAPS_PP_1_1_4 | |
| 828 | SNOR_HWCAPS_PP_1_4_4 | |
| 829 | SNOR_HWCAPS_PP_4_4_4, |
| 830 | }; |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 831 | struct device_node *child, *np = pdev->dev.of_node; |
| 832 | struct atmel_qspi *aq; |
| 833 | struct resource *res; |
| 834 | struct spi_nor *nor; |
| 835 | struct mtd_info *mtd; |
| 836 | int irq, err = 0; |
| 837 | |
| 838 | if (of_get_child_count(np) != 1) |
| 839 | return -ENODEV; |
| 840 | child = of_get_next_child(np, NULL); |
| 841 | |
| 842 | aq = devm_kzalloc(&pdev->dev, sizeof(*aq), GFP_KERNEL); |
| 843 | if (!aq) { |
| 844 | err = -ENOMEM; |
| 845 | goto exit; |
| 846 | } |
| 847 | |
| 848 | platform_set_drvdata(pdev, aq); |
| 849 | init_completion(&aq->cmd_completion); |
| 850 | aq->pdev = pdev; |
| 851 | |
| 852 | /* Map the registers */ |
| 853 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "qspi_base"); |
| 854 | aq->regs = devm_ioremap_resource(&pdev->dev, res); |
| 855 | if (IS_ERR(aq->regs)) { |
| 856 | dev_err(&pdev->dev, "missing registers\n"); |
| 857 | err = PTR_ERR(aq->regs); |
| 858 | goto exit; |
| 859 | } |
| 860 | |
| 861 | /* Map the AHB memory */ |
| 862 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "qspi_mmap"); |
| 863 | aq->mem = devm_ioremap_resource(&pdev->dev, res); |
| 864 | if (IS_ERR(aq->mem)) { |
| 865 | dev_err(&pdev->dev, "missing AHB memory\n"); |
| 866 | err = PTR_ERR(aq->mem); |
| 867 | goto exit; |
| 868 | } |
| 869 | |
| 870 | /* Get the peripheral clock */ |
| 871 | aq->clk = devm_clk_get(&pdev->dev, NULL); |
| 872 | if (IS_ERR(aq->clk)) { |
| 873 | dev_err(&pdev->dev, "missing peripheral clock\n"); |
| 874 | err = PTR_ERR(aq->clk); |
| 875 | goto exit; |
| 876 | } |
| 877 | |
| 878 | /* Enable the peripheral clock */ |
| 879 | err = clk_prepare_enable(aq->clk); |
| 880 | if (err) { |
| 881 | dev_err(&pdev->dev, "failed to enable the peripheral clock\n"); |
| 882 | goto exit; |
| 883 | } |
| 884 | |
| 885 | /* Request the IRQ */ |
| 886 | irq = platform_get_irq(pdev, 0); |
| 887 | if (irq < 0) { |
| 888 | dev_err(&pdev->dev, "missing IRQ\n"); |
| 889 | err = irq; |
| 890 | goto disable_clk; |
| 891 | } |
| 892 | err = devm_request_irq(&pdev->dev, irq, atmel_qspi_interrupt, |
| 893 | 0, dev_name(&pdev->dev), aq); |
| 894 | if (err) |
| 895 | goto disable_clk; |
| 896 | |
| 897 | /* Setup the spi-nor */ |
| 898 | nor = &aq->nor; |
| 899 | mtd = &nor->mtd; |
| 900 | |
| 901 | nor->dev = &pdev->dev; |
| 902 | spi_nor_set_flash_node(nor, child); |
| 903 | nor->priv = aq; |
| 904 | mtd->priv = nor; |
| 905 | |
| 906 | nor->read_reg = atmel_qspi_read_reg; |
| 907 | nor->write_reg = atmel_qspi_write_reg; |
| 908 | nor->read = atmel_qspi_read; |
| 909 | nor->write = atmel_qspi_write; |
| 910 | nor->erase = atmel_qspi_erase; |
| 911 | |
| 912 | err = of_property_read_u32(child, "spi-max-frequency", &aq->clk_rate); |
| 913 | if (err < 0) |
| 914 | goto disable_clk; |
| 915 | |
| 916 | err = atmel_qspi_init(aq); |
| 917 | if (err) |
| 918 | goto disable_clk; |
| 919 | |
Cyrille Pitchen | cfc5604 | 2017-04-25 22:08:46 +0200 | [diff] [blame] | 920 | err = spi_nor_scan(nor, NULL, &hwcaps); |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 921 | if (err) |
| 922 | goto disable_clk; |
| 923 | |
| 924 | err = mtd_device_register(mtd, NULL, 0); |
| 925 | if (err) |
| 926 | goto disable_clk; |
| 927 | |
| 928 | of_node_put(child); |
| 929 | |
| 930 | return 0; |
| 931 | |
| 932 | disable_clk: |
| 933 | clk_disable_unprepare(aq->clk); |
| 934 | exit: |
| 935 | of_node_put(child); |
| 936 | |
| 937 | return err; |
| 938 | } |
| 939 | |
| 940 | static int atmel_qspi_remove(struct platform_device *pdev) |
| 941 | { |
| 942 | struct atmel_qspi *aq = platform_get_drvdata(pdev); |
| 943 | |
| 944 | mtd_device_unregister(&aq->nor.mtd); |
| 945 | qspi_writel(aq, QSPI_CR, QSPI_CR_QSPIDIS); |
| 946 | clk_disable_unprepare(aq->clk); |
| 947 | return 0; |
| 948 | } |
| 949 | |
Claudiu Beznea | de217c1 | 2018-06-04 11:46:33 +0300 | [diff] [blame] | 950 | static int __maybe_unused atmel_qspi_suspend(struct device *dev) |
| 951 | { |
| 952 | struct atmel_qspi *aq = dev_get_drvdata(dev); |
| 953 | |
| 954 | clk_disable_unprepare(aq->clk); |
| 955 | |
| 956 | return 0; |
| 957 | } |
| 958 | |
| 959 | static int __maybe_unused atmel_qspi_resume(struct device *dev) |
| 960 | { |
| 961 | struct atmel_qspi *aq = dev_get_drvdata(dev); |
| 962 | |
| 963 | clk_prepare_enable(aq->clk); |
| 964 | |
| 965 | return atmel_qspi_init(aq); |
| 966 | } |
| 967 | |
| 968 | static SIMPLE_DEV_PM_OPS(atmel_qspi_pm_ops, atmel_qspi_suspend, |
| 969 | atmel_qspi_resume); |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 970 | |
| 971 | static const struct of_device_id atmel_qspi_dt_ids[] = { |
| 972 | { .compatible = "atmel,sama5d2-qspi" }, |
| 973 | { /* sentinel */ } |
| 974 | }; |
| 975 | |
| 976 | MODULE_DEVICE_TABLE(of, atmel_qspi_dt_ids); |
| 977 | |
| 978 | static struct platform_driver atmel_qspi_driver = { |
| 979 | .driver = { |
| 980 | .name = "atmel_qspi", |
| 981 | .of_match_table = atmel_qspi_dt_ids, |
Claudiu Beznea | de217c1 | 2018-06-04 11:46:33 +0300 | [diff] [blame] | 982 | .pm = &atmel_qspi_pm_ops, |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 983 | }, |
| 984 | .probe = atmel_qspi_probe, |
| 985 | .remove = atmel_qspi_remove, |
| 986 | }; |
| 987 | module_platform_driver(atmel_qspi_driver); |
| 988 | |
| 989 | MODULE_AUTHOR("Cyrille Pitchen <cyrille.pitchen@atmel.com>"); |
Piotr Bugalski | d5433de | 2018-11-05 11:36:21 +0100 | [diff] [blame^] | 990 | MODULE_AUTHOR("Piotr Bugalski <bugalski.piotr@gmail.com"); |
Cyrille Pitchen | 161aaab | 2016-06-13 17:10:26 +0200 | [diff] [blame] | 991 | MODULE_DESCRIPTION("Atmel QSPI Controller driver"); |
| 992 | MODULE_LICENSE("GPL v2"); |