Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Freescale QuadSPI driver. |
| 3 | * |
| 4 | * Copyright (C) 2013 Freescale Semiconductor, Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | */ |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/errno.h> |
| 15 | #include <linux/platform_device.h> |
| 16 | #include <linux/sched.h> |
| 17 | #include <linux/delay.h> |
| 18 | #include <linux/io.h> |
| 19 | #include <linux/clk.h> |
| 20 | #include <linux/err.h> |
| 21 | #include <linux/of.h> |
| 22 | #include <linux/of_device.h> |
| 23 | #include <linux/timer.h> |
| 24 | #include <linux/jiffies.h> |
| 25 | #include <linux/completion.h> |
| 26 | #include <linux/mtd/mtd.h> |
| 27 | #include <linux/mtd/partitions.h> |
| 28 | #include <linux/mtd/spi-nor.h> |
Han Xu | 392d39c | 2015-05-13 14:40:57 -0500 | [diff] [blame] | 29 | #include <linux/mutex.h> |
Frank Li | 5cc66cb | 2015-08-04 10:26:04 -0500 | [diff] [blame] | 30 | #include <linux/pm_qos.h> |
Fabio Estevam | 01a3c62 | 2015-10-14 00:39:44 -0300 | [diff] [blame] | 31 | #include <linux/sizes.h> |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 32 | |
Han Xu | 80d3772 | 2015-08-04 10:25:29 -0500 | [diff] [blame] | 33 | /* Controller needs driver to swap endian */ |
| 34 | #define QUADSPI_QUIRK_SWAP_ENDIAN (1 << 0) |
| 35 | /* Controller needs 4x internal clock */ |
| 36 | #define QUADSPI_QUIRK_4X_INT_CLK (1 << 1) |
Frank Li | d371cbf | 2015-08-04 10:25:35 -0500 | [diff] [blame] | 37 | /* |
| 38 | * TKT253890, Controller needs driver to fill txfifo till 16 byte to |
| 39 | * trigger data transfer even though extern data will not transferred. |
| 40 | */ |
| 41 | #define QUADSPI_QUIRK_TKT253890 (1 << 2) |
Frank Li | 5cc66cb | 2015-08-04 10:26:04 -0500 | [diff] [blame] | 42 | /* Controller cannot wake up from wait mode, TKT245618 */ |
| 43 | #define QUADSPI_QUIRK_TKT245618 (1 << 3) |
Han Xu | 80d3772 | 2015-08-04 10:25:29 -0500 | [diff] [blame] | 44 | |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 45 | /* The registers */ |
| 46 | #define QUADSPI_MCR 0x00 |
| 47 | #define QUADSPI_MCR_RESERVED_SHIFT 16 |
| 48 | #define QUADSPI_MCR_RESERVED_MASK (0xF << QUADSPI_MCR_RESERVED_SHIFT) |
| 49 | #define QUADSPI_MCR_MDIS_SHIFT 14 |
| 50 | #define QUADSPI_MCR_MDIS_MASK (1 << QUADSPI_MCR_MDIS_SHIFT) |
| 51 | #define QUADSPI_MCR_CLR_TXF_SHIFT 11 |
| 52 | #define QUADSPI_MCR_CLR_TXF_MASK (1 << QUADSPI_MCR_CLR_TXF_SHIFT) |
| 53 | #define QUADSPI_MCR_CLR_RXF_SHIFT 10 |
| 54 | #define QUADSPI_MCR_CLR_RXF_MASK (1 << QUADSPI_MCR_CLR_RXF_SHIFT) |
| 55 | #define QUADSPI_MCR_DDR_EN_SHIFT 7 |
| 56 | #define QUADSPI_MCR_DDR_EN_MASK (1 << QUADSPI_MCR_DDR_EN_SHIFT) |
| 57 | #define QUADSPI_MCR_END_CFG_SHIFT 2 |
| 58 | #define QUADSPI_MCR_END_CFG_MASK (3 << QUADSPI_MCR_END_CFG_SHIFT) |
| 59 | #define QUADSPI_MCR_SWRSTHD_SHIFT 1 |
| 60 | #define QUADSPI_MCR_SWRSTHD_MASK (1 << QUADSPI_MCR_SWRSTHD_SHIFT) |
| 61 | #define QUADSPI_MCR_SWRSTSD_SHIFT 0 |
| 62 | #define QUADSPI_MCR_SWRSTSD_MASK (1 << QUADSPI_MCR_SWRSTSD_SHIFT) |
| 63 | |
| 64 | #define QUADSPI_IPCR 0x08 |
| 65 | #define QUADSPI_IPCR_SEQID_SHIFT 24 |
| 66 | #define QUADSPI_IPCR_SEQID_MASK (0xF << QUADSPI_IPCR_SEQID_SHIFT) |
| 67 | |
| 68 | #define QUADSPI_BUF0CR 0x10 |
| 69 | #define QUADSPI_BUF1CR 0x14 |
| 70 | #define QUADSPI_BUF2CR 0x18 |
| 71 | #define QUADSPI_BUFXCR_INVALID_MSTRID 0xe |
| 72 | |
| 73 | #define QUADSPI_BUF3CR 0x1c |
| 74 | #define QUADSPI_BUF3CR_ALLMST_SHIFT 31 |
Allen Xu | 4e898ce | 2015-01-14 00:28:56 +0800 | [diff] [blame] | 75 | #define QUADSPI_BUF3CR_ALLMST_MASK (1 << QUADSPI_BUF3CR_ALLMST_SHIFT) |
| 76 | #define QUADSPI_BUF3CR_ADATSZ_SHIFT 8 |
| 77 | #define QUADSPI_BUF3CR_ADATSZ_MASK (0xFF << QUADSPI_BUF3CR_ADATSZ_SHIFT) |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 78 | |
| 79 | #define QUADSPI_BFGENCR 0x20 |
| 80 | #define QUADSPI_BFGENCR_PAR_EN_SHIFT 16 |
| 81 | #define QUADSPI_BFGENCR_PAR_EN_MASK (1 << (QUADSPI_BFGENCR_PAR_EN_SHIFT)) |
| 82 | #define QUADSPI_BFGENCR_SEQID_SHIFT 12 |
| 83 | #define QUADSPI_BFGENCR_SEQID_MASK (0xF << QUADSPI_BFGENCR_SEQID_SHIFT) |
| 84 | |
| 85 | #define QUADSPI_BUF0IND 0x30 |
| 86 | #define QUADSPI_BUF1IND 0x34 |
| 87 | #define QUADSPI_BUF2IND 0x38 |
| 88 | #define QUADSPI_SFAR 0x100 |
| 89 | |
| 90 | #define QUADSPI_SMPR 0x108 |
| 91 | #define QUADSPI_SMPR_DDRSMP_SHIFT 16 |
| 92 | #define QUADSPI_SMPR_DDRSMP_MASK (7 << QUADSPI_SMPR_DDRSMP_SHIFT) |
| 93 | #define QUADSPI_SMPR_FSDLY_SHIFT 6 |
| 94 | #define QUADSPI_SMPR_FSDLY_MASK (1 << QUADSPI_SMPR_FSDLY_SHIFT) |
| 95 | #define QUADSPI_SMPR_FSPHS_SHIFT 5 |
| 96 | #define QUADSPI_SMPR_FSPHS_MASK (1 << QUADSPI_SMPR_FSPHS_SHIFT) |
| 97 | #define QUADSPI_SMPR_HSENA_SHIFT 0 |
| 98 | #define QUADSPI_SMPR_HSENA_MASK (1 << QUADSPI_SMPR_HSENA_SHIFT) |
| 99 | |
| 100 | #define QUADSPI_RBSR 0x10c |
| 101 | #define QUADSPI_RBSR_RDBFL_SHIFT 8 |
| 102 | #define QUADSPI_RBSR_RDBFL_MASK (0x3F << QUADSPI_RBSR_RDBFL_SHIFT) |
| 103 | |
| 104 | #define QUADSPI_RBCT 0x110 |
| 105 | #define QUADSPI_RBCT_WMRK_MASK 0x1F |
| 106 | #define QUADSPI_RBCT_RXBRD_SHIFT 8 |
| 107 | #define QUADSPI_RBCT_RXBRD_USEIPS (0x1 << QUADSPI_RBCT_RXBRD_SHIFT) |
| 108 | |
| 109 | #define QUADSPI_TBSR 0x150 |
| 110 | #define QUADSPI_TBDR 0x154 |
| 111 | #define QUADSPI_SR 0x15c |
| 112 | #define QUADSPI_SR_IP_ACC_SHIFT 1 |
| 113 | #define QUADSPI_SR_IP_ACC_MASK (0x1 << QUADSPI_SR_IP_ACC_SHIFT) |
| 114 | #define QUADSPI_SR_AHB_ACC_SHIFT 2 |
| 115 | #define QUADSPI_SR_AHB_ACC_MASK (0x1 << QUADSPI_SR_AHB_ACC_SHIFT) |
| 116 | |
| 117 | #define QUADSPI_FR 0x160 |
| 118 | #define QUADSPI_FR_TFF_MASK 0x1 |
| 119 | |
| 120 | #define QUADSPI_SFA1AD 0x180 |
| 121 | #define QUADSPI_SFA2AD 0x184 |
| 122 | #define QUADSPI_SFB1AD 0x188 |
| 123 | #define QUADSPI_SFB2AD 0x18c |
| 124 | #define QUADSPI_RBDR 0x200 |
| 125 | |
| 126 | #define QUADSPI_LUTKEY 0x300 |
| 127 | #define QUADSPI_LUTKEY_VALUE 0x5AF05AF0 |
| 128 | |
| 129 | #define QUADSPI_LCKCR 0x304 |
| 130 | #define QUADSPI_LCKER_LOCK 0x1 |
| 131 | #define QUADSPI_LCKER_UNLOCK 0x2 |
| 132 | |
| 133 | #define QUADSPI_RSER 0x164 |
| 134 | #define QUADSPI_RSER_TFIE (0x1 << 0) |
| 135 | |
| 136 | #define QUADSPI_LUT_BASE 0x310 |
| 137 | |
| 138 | /* |
| 139 | * The definition of the LUT register shows below: |
| 140 | * |
| 141 | * --------------------------------------------------- |
| 142 | * | INSTR1 | PAD1 | OPRND1 | INSTR0 | PAD0 | OPRND0 | |
| 143 | * --------------------------------------------------- |
| 144 | */ |
| 145 | #define OPRND0_SHIFT 0 |
| 146 | #define PAD0_SHIFT 8 |
| 147 | #define INSTR0_SHIFT 10 |
| 148 | #define OPRND1_SHIFT 16 |
| 149 | |
| 150 | /* Instruction set for the LUT register. */ |
| 151 | #define LUT_STOP 0 |
| 152 | #define LUT_CMD 1 |
| 153 | #define LUT_ADDR 2 |
| 154 | #define LUT_DUMMY 3 |
| 155 | #define LUT_MODE 4 |
| 156 | #define LUT_MODE2 5 |
| 157 | #define LUT_MODE4 6 |
Han Xu | 04850c4 | 2015-10-23 13:18:28 -0500 | [diff] [blame] | 158 | #define LUT_FSL_READ 7 |
| 159 | #define LUT_FSL_WRITE 8 |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 160 | #define LUT_JMP_ON_CS 9 |
| 161 | #define LUT_ADDR_DDR 10 |
| 162 | #define LUT_MODE_DDR 11 |
| 163 | #define LUT_MODE2_DDR 12 |
| 164 | #define LUT_MODE4_DDR 13 |
Han Xu | 04850c4 | 2015-10-23 13:18:28 -0500 | [diff] [blame] | 165 | #define LUT_FSL_READ_DDR 14 |
| 166 | #define LUT_FSL_WRITE_DDR 15 |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 167 | #define LUT_DATA_LEARN 16 |
| 168 | |
| 169 | /* |
| 170 | * The PAD definitions for LUT register. |
| 171 | * |
| 172 | * The pad stands for the lines number of IO[0:3]. |
| 173 | * For example, the Quad read need four IO lines, so you should |
| 174 | * set LUT_PAD4 which means we use four IO lines. |
| 175 | */ |
| 176 | #define LUT_PAD1 0 |
| 177 | #define LUT_PAD2 1 |
| 178 | #define LUT_PAD4 2 |
| 179 | |
| 180 | /* Oprands for the LUT register. */ |
| 181 | #define ADDR24BIT 0x18 |
| 182 | #define ADDR32BIT 0x20 |
| 183 | |
| 184 | /* Macros for constructing the LUT register. */ |
| 185 | #define LUT0(ins, pad, opr) \ |
| 186 | (((opr) << OPRND0_SHIFT) | ((LUT_##pad) << PAD0_SHIFT) | \ |
| 187 | ((LUT_##ins) << INSTR0_SHIFT)) |
| 188 | |
| 189 | #define LUT1(ins, pad, opr) (LUT0(ins, pad, opr) << OPRND1_SHIFT) |
| 190 | |
| 191 | /* other macros for LUT register. */ |
| 192 | #define QUADSPI_LUT(x) (QUADSPI_LUT_BASE + (x) * 4) |
| 193 | #define QUADSPI_LUT_NUM 64 |
| 194 | |
| 195 | /* SEQID -- we can have 16 seqids at most. */ |
| 196 | #define SEQID_QUAD_READ 0 |
| 197 | #define SEQID_WREN 1 |
| 198 | #define SEQID_WRDI 2 |
| 199 | #define SEQID_RDSR 3 |
| 200 | #define SEQID_SE 4 |
| 201 | #define SEQID_CHIP_ERASE 5 |
| 202 | #define SEQID_PP 6 |
| 203 | #define SEQID_RDID 7 |
| 204 | #define SEQID_WRSR 8 |
| 205 | #define SEQID_RDCR 9 |
| 206 | #define SEQID_EN4B 10 |
| 207 | #define SEQID_BRWR 11 |
| 208 | |
Han Xu | 49bd706 | 2015-08-04 10:25:22 -0500 | [diff] [blame] | 209 | #define QUADSPI_MIN_IOMAP SZ_4M |
| 210 | |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 211 | enum fsl_qspi_devtype { |
| 212 | FSL_QUADSPI_VYBRID, |
| 213 | FSL_QUADSPI_IMX6SX, |
Frank Li | d371cbf | 2015-08-04 10:25:35 -0500 | [diff] [blame] | 214 | FSL_QUADSPI_IMX7D, |
Frank Li | 74a081d1 | 2015-08-04 10:25:47 -0500 | [diff] [blame] | 215 | FSL_QUADSPI_IMX6UL, |
Yao Yuan | e8c034b | 2016-01-26 15:23:56 +0800 | [diff] [blame] | 216 | FSL_QUADSPI_LS1021A, |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 217 | }; |
| 218 | |
| 219 | struct fsl_qspi_devtype_data { |
| 220 | enum fsl_qspi_devtype devtype; |
| 221 | int rxfifo; |
| 222 | int txfifo; |
Allen Xu | 4e898ce | 2015-01-14 00:28:56 +0800 | [diff] [blame] | 223 | int ahb_buf_size; |
Han Xu | 80d3772 | 2015-08-04 10:25:29 -0500 | [diff] [blame] | 224 | int driver_data; |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 225 | }; |
| 226 | |
LABBE Corentin | dfce0cd | 2016-08-16 14:56:38 +0200 | [diff] [blame] | 227 | static const struct fsl_qspi_devtype_data vybrid_data = { |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 228 | .devtype = FSL_QUADSPI_VYBRID, |
| 229 | .rxfifo = 128, |
Allen Xu | 4e898ce | 2015-01-14 00:28:56 +0800 | [diff] [blame] | 230 | .txfifo = 64, |
Han Xu | 80d3772 | 2015-08-04 10:25:29 -0500 | [diff] [blame] | 231 | .ahb_buf_size = 1024, |
| 232 | .driver_data = QUADSPI_QUIRK_SWAP_ENDIAN, |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 233 | }; |
| 234 | |
LABBE Corentin | dfce0cd | 2016-08-16 14:56:38 +0200 | [diff] [blame] | 235 | static const struct fsl_qspi_devtype_data imx6sx_data = { |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 236 | .devtype = FSL_QUADSPI_IMX6SX, |
| 237 | .rxfifo = 128, |
Allen Xu | 4e898ce | 2015-01-14 00:28:56 +0800 | [diff] [blame] | 238 | .txfifo = 512, |
Han Xu | 80d3772 | 2015-08-04 10:25:29 -0500 | [diff] [blame] | 239 | .ahb_buf_size = 1024, |
Frank Li | 5cc66cb | 2015-08-04 10:26:04 -0500 | [diff] [blame] | 240 | .driver_data = QUADSPI_QUIRK_4X_INT_CLK |
| 241 | | QUADSPI_QUIRK_TKT245618, |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 242 | }; |
| 243 | |
LABBE Corentin | dfce0cd | 2016-08-16 14:56:38 +0200 | [diff] [blame] | 244 | static const struct fsl_qspi_devtype_data imx7d_data = { |
Frank Li | d371cbf | 2015-08-04 10:25:35 -0500 | [diff] [blame] | 245 | .devtype = FSL_QUADSPI_IMX7D, |
| 246 | .rxfifo = 512, |
| 247 | .txfifo = 512, |
| 248 | .ahb_buf_size = 1024, |
| 249 | .driver_data = QUADSPI_QUIRK_TKT253890 |
| 250 | | QUADSPI_QUIRK_4X_INT_CLK, |
| 251 | }; |
| 252 | |
LABBE Corentin | dfce0cd | 2016-08-16 14:56:38 +0200 | [diff] [blame] | 253 | static const struct fsl_qspi_devtype_data imx6ul_data = { |
Frank Li | 74a081d1 | 2015-08-04 10:25:47 -0500 | [diff] [blame] | 254 | .devtype = FSL_QUADSPI_IMX6UL, |
| 255 | .rxfifo = 128, |
| 256 | .txfifo = 512, |
| 257 | .ahb_buf_size = 1024, |
| 258 | .driver_data = QUADSPI_QUIRK_TKT253890 |
| 259 | | QUADSPI_QUIRK_4X_INT_CLK, |
| 260 | }; |
| 261 | |
Yao Yuan | e8c034b | 2016-01-26 15:23:56 +0800 | [diff] [blame] | 262 | static struct fsl_qspi_devtype_data ls1021a_data = { |
| 263 | .devtype = FSL_QUADSPI_LS1021A, |
| 264 | .rxfifo = 128, |
| 265 | .txfifo = 64, |
| 266 | .ahb_buf_size = 1024, |
| 267 | .driver_data = 0, |
| 268 | }; |
| 269 | |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 270 | #define FSL_QSPI_MAX_CHIP 4 |
| 271 | struct fsl_qspi { |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 272 | struct spi_nor nor[FSL_QSPI_MAX_CHIP]; |
| 273 | void __iomem *iobase; |
Han Xu | 49bd706 | 2015-08-04 10:25:22 -0500 | [diff] [blame] | 274 | void __iomem *ahb_addr; |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 275 | u32 memmap_phy; |
Han Xu | 49bd706 | 2015-08-04 10:25:22 -0500 | [diff] [blame] | 276 | u32 memmap_offs; |
| 277 | u32 memmap_len; |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 278 | struct clk *clk, *clk_en; |
| 279 | struct device *dev; |
| 280 | struct completion c; |
Brian Norris | c98f71d | 2015-11-16 10:45:30 -0800 | [diff] [blame] | 281 | const struct fsl_qspi_devtype_data *devtype_data; |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 282 | u32 nor_size; |
| 283 | u32 nor_num; |
| 284 | u32 clk_rate; |
| 285 | unsigned int chip_base_addr; /* We may support two chips. */ |
Fabio Estevam | cfe4af3 | 2015-01-13 20:14:15 -0200 | [diff] [blame] | 286 | bool has_second_chip; |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 287 | bool big_endian; |
Han Xu | 392d39c | 2015-05-13 14:40:57 -0500 | [diff] [blame] | 288 | struct mutex lock; |
Frank Li | 5cc66cb | 2015-08-04 10:26:04 -0500 | [diff] [blame] | 289 | struct pm_qos_request pm_qos_req; |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 290 | }; |
| 291 | |
Han Xu | 80d3772 | 2015-08-04 10:25:29 -0500 | [diff] [blame] | 292 | static inline int needs_swap_endian(struct fsl_qspi *q) |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 293 | { |
Han Xu | 80d3772 | 2015-08-04 10:25:29 -0500 | [diff] [blame] | 294 | return q->devtype_data->driver_data & QUADSPI_QUIRK_SWAP_ENDIAN; |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 295 | } |
| 296 | |
Han Xu | 80d3772 | 2015-08-04 10:25:29 -0500 | [diff] [blame] | 297 | static inline int needs_4x_clock(struct fsl_qspi *q) |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 298 | { |
Han Xu | 80d3772 | 2015-08-04 10:25:29 -0500 | [diff] [blame] | 299 | return q->devtype_data->driver_data & QUADSPI_QUIRK_4X_INT_CLK; |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 300 | } |
| 301 | |
Frank Li | d371cbf | 2015-08-04 10:25:35 -0500 | [diff] [blame] | 302 | static inline int needs_fill_txfifo(struct fsl_qspi *q) |
| 303 | { |
| 304 | return q->devtype_data->driver_data & QUADSPI_QUIRK_TKT253890; |
| 305 | } |
| 306 | |
Frank Li | 5cc66cb | 2015-08-04 10:26:04 -0500 | [diff] [blame] | 307 | static inline int needs_wakeup_wait_mode(struct fsl_qspi *q) |
| 308 | { |
| 309 | return q->devtype_data->driver_data & QUADSPI_QUIRK_TKT245618; |
| 310 | } |
| 311 | |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 312 | /* |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 313 | * R/W functions for big- or little-endian registers: |
| 314 | * The qSPI controller's endian is independent of the CPU core's endian. |
| 315 | * So far, although the CPU core is little-endian but the qSPI have two |
| 316 | * versions for big-endian and little-endian. |
| 317 | */ |
| 318 | static void qspi_writel(struct fsl_qspi *q, u32 val, void __iomem *addr) |
| 319 | { |
| 320 | if (q->big_endian) |
| 321 | iowrite32be(val, addr); |
| 322 | else |
| 323 | iowrite32(val, addr); |
| 324 | } |
| 325 | |
| 326 | static u32 qspi_readl(struct fsl_qspi *q, void __iomem *addr) |
| 327 | { |
| 328 | if (q->big_endian) |
| 329 | return ioread32be(addr); |
| 330 | else |
| 331 | return ioread32(addr); |
| 332 | } |
| 333 | |
| 334 | /* |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 335 | * An IC bug makes us to re-arrange the 32-bit data. |
| 336 | * The following chips, such as IMX6SLX, have fixed this bug. |
| 337 | */ |
| 338 | static inline u32 fsl_qspi_endian_xchg(struct fsl_qspi *q, u32 a) |
| 339 | { |
Han Xu | 80d3772 | 2015-08-04 10:25:29 -0500 | [diff] [blame] | 340 | return needs_swap_endian(q) ? __swab32(a) : a; |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | static inline void fsl_qspi_unlock_lut(struct fsl_qspi *q) |
| 344 | { |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 345 | qspi_writel(q, QUADSPI_LUTKEY_VALUE, q->iobase + QUADSPI_LUTKEY); |
| 346 | qspi_writel(q, QUADSPI_LCKER_UNLOCK, q->iobase + QUADSPI_LCKCR); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | static inline void fsl_qspi_lock_lut(struct fsl_qspi *q) |
| 350 | { |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 351 | qspi_writel(q, QUADSPI_LUTKEY_VALUE, q->iobase + QUADSPI_LUTKEY); |
| 352 | qspi_writel(q, QUADSPI_LCKER_LOCK, q->iobase + QUADSPI_LCKCR); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | static irqreturn_t fsl_qspi_irq_handler(int irq, void *dev_id) |
| 356 | { |
| 357 | struct fsl_qspi *q = dev_id; |
| 358 | u32 reg; |
| 359 | |
| 360 | /* clear interrupt */ |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 361 | reg = qspi_readl(q, q->iobase + QUADSPI_FR); |
| 362 | qspi_writel(q, reg, q->iobase + QUADSPI_FR); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 363 | |
| 364 | if (reg & QUADSPI_FR_TFF_MASK) |
| 365 | complete(&q->c); |
| 366 | |
| 367 | dev_dbg(q->dev, "QUADSPI_FR : 0x%.8x:0x%.8x\n", q->chip_base_addr, reg); |
| 368 | return IRQ_HANDLED; |
| 369 | } |
| 370 | |
| 371 | static void fsl_qspi_init_lut(struct fsl_qspi *q) |
| 372 | { |
Brian Norris | a965d04 | 2014-04-10 15:49:38 -0700 | [diff] [blame] | 373 | void __iomem *base = q->iobase; |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 374 | int rxfifo = q->devtype_data->rxfifo; |
| 375 | u32 lut_base; |
| 376 | u8 cmd, addrlen, dummy; |
| 377 | int i; |
| 378 | |
| 379 | fsl_qspi_unlock_lut(q); |
| 380 | |
| 381 | /* Clear all the LUT table */ |
| 382 | for (i = 0; i < QUADSPI_LUT_NUM; i++) |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 383 | qspi_writel(q, 0, base + QUADSPI_LUT_BASE + i * 4); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 384 | |
| 385 | /* Quad Read */ |
| 386 | lut_base = SEQID_QUAD_READ * 4; |
| 387 | |
| 388 | if (q->nor_size <= SZ_16M) { |
Brian Norris | 58b89a1 | 2014-04-08 19:16:49 -0700 | [diff] [blame] | 389 | cmd = SPINOR_OP_READ_1_1_4; |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 390 | addrlen = ADDR24BIT; |
| 391 | dummy = 8; |
| 392 | } else { |
| 393 | /* use the 4-byte address */ |
Brian Norris | 58b89a1 | 2014-04-08 19:16:49 -0700 | [diff] [blame] | 394 | cmd = SPINOR_OP_READ_1_1_4; |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 395 | addrlen = ADDR32BIT; |
| 396 | dummy = 8; |
| 397 | } |
| 398 | |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 399 | qspi_writel(q, LUT0(CMD, PAD1, cmd) | LUT1(ADDR, PAD1, addrlen), |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 400 | base + QUADSPI_LUT(lut_base)); |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 401 | qspi_writel(q, LUT0(DUMMY, PAD1, dummy) | LUT1(FSL_READ, PAD4, rxfifo), |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 402 | base + QUADSPI_LUT(lut_base + 1)); |
| 403 | |
| 404 | /* Write enable */ |
| 405 | lut_base = SEQID_WREN * 4; |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 406 | qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_WREN), |
| 407 | base + QUADSPI_LUT(lut_base)); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 408 | |
| 409 | /* Page Program */ |
| 410 | lut_base = SEQID_PP * 4; |
| 411 | |
| 412 | if (q->nor_size <= SZ_16M) { |
Brian Norris | b02e7f3 | 2014-04-08 18:15:31 -0700 | [diff] [blame] | 413 | cmd = SPINOR_OP_PP; |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 414 | addrlen = ADDR24BIT; |
| 415 | } else { |
| 416 | /* use the 4-byte address */ |
Brian Norris | b02e7f3 | 2014-04-08 18:15:31 -0700 | [diff] [blame] | 417 | cmd = SPINOR_OP_PP; |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 418 | addrlen = ADDR32BIT; |
| 419 | } |
| 420 | |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 421 | qspi_writel(q, LUT0(CMD, PAD1, cmd) | LUT1(ADDR, PAD1, addrlen), |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 422 | base + QUADSPI_LUT(lut_base)); |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 423 | qspi_writel(q, LUT0(FSL_WRITE, PAD1, 0), |
| 424 | base + QUADSPI_LUT(lut_base + 1)); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 425 | |
| 426 | /* Read Status */ |
| 427 | lut_base = SEQID_RDSR * 4; |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 428 | qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_RDSR) | |
| 429 | LUT1(FSL_READ, PAD1, 0x1), |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 430 | base + QUADSPI_LUT(lut_base)); |
| 431 | |
| 432 | /* Erase a sector */ |
| 433 | lut_base = SEQID_SE * 4; |
| 434 | |
Frank Li | 788a6cd | 2015-08-04 10:26:16 -0500 | [diff] [blame] | 435 | cmd = q->nor[0].erase_opcode; |
| 436 | addrlen = q->nor_size <= SZ_16M ? ADDR24BIT : ADDR32BIT; |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 437 | |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 438 | qspi_writel(q, LUT0(CMD, PAD1, cmd) | LUT1(ADDR, PAD1, addrlen), |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 439 | base + QUADSPI_LUT(lut_base)); |
| 440 | |
| 441 | /* Erase the whole chip */ |
| 442 | lut_base = SEQID_CHIP_ERASE * 4; |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 443 | qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_CHIP_ERASE), |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 444 | base + QUADSPI_LUT(lut_base)); |
| 445 | |
| 446 | /* READ ID */ |
| 447 | lut_base = SEQID_RDID * 4; |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 448 | qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_RDID) | |
| 449 | LUT1(FSL_READ, PAD1, 0x8), |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 450 | base + QUADSPI_LUT(lut_base)); |
| 451 | |
| 452 | /* Write Register */ |
| 453 | lut_base = SEQID_WRSR * 4; |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 454 | qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_WRSR) | |
| 455 | LUT1(FSL_WRITE, PAD1, 0x2), |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 456 | base + QUADSPI_LUT(lut_base)); |
| 457 | |
| 458 | /* Read Configuration Register */ |
| 459 | lut_base = SEQID_RDCR * 4; |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 460 | qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_RDCR) | |
| 461 | LUT1(FSL_READ, PAD1, 0x1), |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 462 | base + QUADSPI_LUT(lut_base)); |
| 463 | |
| 464 | /* Write disable */ |
| 465 | lut_base = SEQID_WRDI * 4; |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 466 | qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_WRDI), |
| 467 | base + QUADSPI_LUT(lut_base)); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 468 | |
| 469 | /* Enter 4 Byte Mode (Micron) */ |
| 470 | lut_base = SEQID_EN4B * 4; |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 471 | qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_EN4B), |
| 472 | base + QUADSPI_LUT(lut_base)); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 473 | |
| 474 | /* Enter 4 Byte Mode (Spansion) */ |
| 475 | lut_base = SEQID_BRWR * 4; |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 476 | qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_BRWR), |
| 477 | base + QUADSPI_LUT(lut_base)); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 478 | |
| 479 | fsl_qspi_lock_lut(q); |
| 480 | } |
| 481 | |
| 482 | /* Get the SEQID for the command */ |
| 483 | static int fsl_qspi_get_seqid(struct fsl_qspi *q, u8 cmd) |
| 484 | { |
| 485 | switch (cmd) { |
Brian Norris | 58b89a1 | 2014-04-08 19:16:49 -0700 | [diff] [blame] | 486 | case SPINOR_OP_READ_1_1_4: |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 487 | return SEQID_QUAD_READ; |
Brian Norris | b02e7f3 | 2014-04-08 18:15:31 -0700 | [diff] [blame] | 488 | case SPINOR_OP_WREN: |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 489 | return SEQID_WREN; |
Brian Norris | b02e7f3 | 2014-04-08 18:15:31 -0700 | [diff] [blame] | 490 | case SPINOR_OP_WRDI: |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 491 | return SEQID_WRDI; |
Brian Norris | b02e7f3 | 2014-04-08 18:15:31 -0700 | [diff] [blame] | 492 | case SPINOR_OP_RDSR: |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 493 | return SEQID_RDSR; |
Brian Norris | b02e7f3 | 2014-04-08 18:15:31 -0700 | [diff] [blame] | 494 | case SPINOR_OP_SE: |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 495 | return SEQID_SE; |
Brian Norris | b02e7f3 | 2014-04-08 18:15:31 -0700 | [diff] [blame] | 496 | case SPINOR_OP_CHIP_ERASE: |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 497 | return SEQID_CHIP_ERASE; |
Brian Norris | b02e7f3 | 2014-04-08 18:15:31 -0700 | [diff] [blame] | 498 | case SPINOR_OP_PP: |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 499 | return SEQID_PP; |
Brian Norris | b02e7f3 | 2014-04-08 18:15:31 -0700 | [diff] [blame] | 500 | case SPINOR_OP_RDID: |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 501 | return SEQID_RDID; |
Brian Norris | b02e7f3 | 2014-04-08 18:15:31 -0700 | [diff] [blame] | 502 | case SPINOR_OP_WRSR: |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 503 | return SEQID_WRSR; |
Brian Norris | b02e7f3 | 2014-04-08 18:15:31 -0700 | [diff] [blame] | 504 | case SPINOR_OP_RDCR: |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 505 | return SEQID_RDCR; |
Brian Norris | b02e7f3 | 2014-04-08 18:15:31 -0700 | [diff] [blame] | 506 | case SPINOR_OP_EN4B: |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 507 | return SEQID_EN4B; |
Brian Norris | b02e7f3 | 2014-04-08 18:15:31 -0700 | [diff] [blame] | 508 | case SPINOR_OP_BRWR: |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 509 | return SEQID_BRWR; |
| 510 | default: |
Frank Li | 788a6cd | 2015-08-04 10:26:16 -0500 | [diff] [blame] | 511 | if (cmd == q->nor[0].erase_opcode) |
| 512 | return SEQID_SE; |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 513 | dev_err(q->dev, "Unsupported cmd 0x%.2x\n", cmd); |
| 514 | break; |
| 515 | } |
| 516 | return -EINVAL; |
| 517 | } |
| 518 | |
| 519 | static int |
| 520 | fsl_qspi_runcmd(struct fsl_qspi *q, u8 cmd, unsigned int addr, int len) |
| 521 | { |
Brian Norris | a965d04 | 2014-04-10 15:49:38 -0700 | [diff] [blame] | 522 | void __iomem *base = q->iobase; |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 523 | int seqid; |
| 524 | u32 reg, reg2; |
| 525 | int err; |
| 526 | |
| 527 | init_completion(&q->c); |
| 528 | dev_dbg(q->dev, "to 0x%.8x:0x%.8x, len:%d, cmd:%.2x\n", |
| 529 | q->chip_base_addr, addr, len, cmd); |
| 530 | |
| 531 | /* save the reg */ |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 532 | reg = qspi_readl(q, base + QUADSPI_MCR); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 533 | |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 534 | qspi_writel(q, q->memmap_phy + q->chip_base_addr + addr, |
| 535 | base + QUADSPI_SFAR); |
| 536 | qspi_writel(q, QUADSPI_RBCT_WMRK_MASK | QUADSPI_RBCT_RXBRD_USEIPS, |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 537 | base + QUADSPI_RBCT); |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 538 | qspi_writel(q, reg | QUADSPI_MCR_CLR_RXF_MASK, base + QUADSPI_MCR); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 539 | |
| 540 | do { |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 541 | reg2 = qspi_readl(q, base + QUADSPI_SR); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 542 | if (reg2 & (QUADSPI_SR_IP_ACC_MASK | QUADSPI_SR_AHB_ACC_MASK)) { |
| 543 | udelay(1); |
| 544 | dev_dbg(q->dev, "The controller is busy, 0x%x\n", reg2); |
| 545 | continue; |
| 546 | } |
| 547 | break; |
| 548 | } while (1); |
| 549 | |
| 550 | /* trigger the LUT now */ |
| 551 | seqid = fsl_qspi_get_seqid(q, cmd); |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 552 | qspi_writel(q, (seqid << QUADSPI_IPCR_SEQID_SHIFT) | len, |
| 553 | base + QUADSPI_IPCR); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 554 | |
| 555 | /* Wait for the interrupt. */ |
Nicholas Mc Guire | 219a8d1 | 2015-02-01 06:15:46 -0500 | [diff] [blame] | 556 | if (!wait_for_completion_timeout(&q->c, msecs_to_jiffies(1000))) { |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 557 | dev_err(q->dev, |
| 558 | "cmd 0x%.2x timeout, addr@%.8x, FR:0x%.8x, SR:0x%.8x\n", |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 559 | cmd, addr, qspi_readl(q, base + QUADSPI_FR), |
| 560 | qspi_readl(q, base + QUADSPI_SR)); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 561 | err = -ETIMEDOUT; |
| 562 | } else { |
| 563 | err = 0; |
| 564 | } |
| 565 | |
| 566 | /* restore the MCR */ |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 567 | qspi_writel(q, reg, base + QUADSPI_MCR); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 568 | |
| 569 | return err; |
| 570 | } |
| 571 | |
| 572 | /* Read out the data from the QUADSPI_RBDR buffer registers. */ |
| 573 | static void fsl_qspi_read_data(struct fsl_qspi *q, int len, u8 *rxbuf) |
| 574 | { |
| 575 | u32 tmp; |
| 576 | int i = 0; |
| 577 | |
| 578 | while (len > 0) { |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 579 | tmp = qspi_readl(q, q->iobase + QUADSPI_RBDR + i * 4); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 580 | tmp = fsl_qspi_endian_xchg(q, tmp); |
| 581 | dev_dbg(q->dev, "chip addr:0x%.8x, rcv:0x%.8x\n", |
| 582 | q->chip_base_addr, tmp); |
| 583 | |
| 584 | if (len >= 4) { |
| 585 | *((u32 *)rxbuf) = tmp; |
| 586 | rxbuf += 4; |
| 587 | } else { |
| 588 | memcpy(rxbuf, &tmp, len); |
| 589 | break; |
| 590 | } |
| 591 | |
| 592 | len -= 4; |
| 593 | i++; |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | /* |
| 598 | * If we have changed the content of the flash by writing or erasing, |
| 599 | * we need to invalidate the AHB buffer. If we do not do so, we may read out |
| 600 | * the wrong data. The spec tells us reset the AHB domain and Serial Flash |
| 601 | * domain at the same time. |
| 602 | */ |
| 603 | static inline void fsl_qspi_invalid(struct fsl_qspi *q) |
| 604 | { |
| 605 | u32 reg; |
| 606 | |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 607 | reg = qspi_readl(q, q->iobase + QUADSPI_MCR); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 608 | reg |= QUADSPI_MCR_SWRSTHD_MASK | QUADSPI_MCR_SWRSTSD_MASK; |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 609 | qspi_writel(q, reg, q->iobase + QUADSPI_MCR); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 610 | |
| 611 | /* |
| 612 | * The minimum delay : 1 AHB + 2 SFCK clocks. |
| 613 | * Delay 1 us is enough. |
| 614 | */ |
| 615 | udelay(1); |
| 616 | |
| 617 | reg &= ~(QUADSPI_MCR_SWRSTHD_MASK | QUADSPI_MCR_SWRSTSD_MASK); |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 618 | qspi_writel(q, reg, q->iobase + QUADSPI_MCR); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 619 | } |
| 620 | |
Michal Suchanek | fc0d7e5 | 2016-05-05 17:31:49 -0700 | [diff] [blame] | 621 | static ssize_t fsl_qspi_nor_write(struct fsl_qspi *q, struct spi_nor *nor, |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 622 | u8 opcode, unsigned int to, u32 *txbuf, |
Michal Suchanek | 2dd087b | 2016-05-05 17:31:53 -0700 | [diff] [blame] | 623 | unsigned count) |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 624 | { |
| 625 | int ret, i, j; |
| 626 | u32 tmp; |
| 627 | |
| 628 | dev_dbg(q->dev, "to 0x%.8x:0x%.8x, len : %d\n", |
| 629 | q->chip_base_addr, to, count); |
| 630 | |
| 631 | /* clear the TX FIFO. */ |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 632 | tmp = qspi_readl(q, q->iobase + QUADSPI_MCR); |
| 633 | qspi_writel(q, tmp | QUADSPI_MCR_CLR_TXF_MASK, q->iobase + QUADSPI_MCR); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 634 | |
| 635 | /* fill the TX data to the FIFO */ |
| 636 | for (j = 0, i = ((count + 3) / 4); j < i; j++) { |
| 637 | tmp = fsl_qspi_endian_xchg(q, *txbuf); |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 638 | qspi_writel(q, tmp, q->iobase + QUADSPI_TBDR); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 639 | txbuf++; |
| 640 | } |
| 641 | |
Frank Li | d371cbf | 2015-08-04 10:25:35 -0500 | [diff] [blame] | 642 | /* fill the TXFIFO upto 16 bytes for i.MX7d */ |
| 643 | if (needs_fill_txfifo(q)) |
| 644 | for (; i < 4; i++) |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 645 | qspi_writel(q, tmp, q->iobase + QUADSPI_TBDR); |
Frank Li | d371cbf | 2015-08-04 10:25:35 -0500 | [diff] [blame] | 646 | |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 647 | /* Trigger it */ |
| 648 | ret = fsl_qspi_runcmd(q, opcode, to, count); |
| 649 | |
Michal Suchanek | 2dd087b | 2016-05-05 17:31:53 -0700 | [diff] [blame] | 650 | if (ret == 0) |
Michal Suchanek | fc0d7e5 | 2016-05-05 17:31:49 -0700 | [diff] [blame] | 651 | return count; |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 652 | |
| 653 | return ret; |
| 654 | } |
| 655 | |
| 656 | static void fsl_qspi_set_map_addr(struct fsl_qspi *q) |
| 657 | { |
| 658 | int nor_size = q->nor_size; |
| 659 | void __iomem *base = q->iobase; |
| 660 | |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 661 | qspi_writel(q, nor_size + q->memmap_phy, base + QUADSPI_SFA1AD); |
| 662 | qspi_writel(q, nor_size * 2 + q->memmap_phy, base + QUADSPI_SFA2AD); |
| 663 | qspi_writel(q, nor_size * 3 + q->memmap_phy, base + QUADSPI_SFB1AD); |
| 664 | qspi_writel(q, nor_size * 4 + q->memmap_phy, base + QUADSPI_SFB2AD); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | /* |
| 668 | * There are two different ways to read out the data from the flash: |
| 669 | * the "IP Command Read" and the "AHB Command Read". |
| 670 | * |
| 671 | * The IC guy suggests we use the "AHB Command Read" which is faster |
| 672 | * then the "IP Command Read". (What's more is that there is a bug in |
| 673 | * the "IP Command Read" in the Vybrid.) |
| 674 | * |
| 675 | * After we set up the registers for the "AHB Command Read", we can use |
| 676 | * the memcpy to read the data directly. A "missed" access to the buffer |
| 677 | * causes the controller to clear the buffer, and use the sequence pointed |
| 678 | * by the QUADSPI_BFGENCR[SEQID] to initiate a read from the flash. |
| 679 | */ |
| 680 | static void fsl_qspi_init_abh_read(struct fsl_qspi *q) |
| 681 | { |
| 682 | void __iomem *base = q->iobase; |
| 683 | int seqid; |
| 684 | |
| 685 | /* AHB configuration for access buffer 0/1/2 .*/ |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 686 | qspi_writel(q, QUADSPI_BUFXCR_INVALID_MSTRID, base + QUADSPI_BUF0CR); |
| 687 | qspi_writel(q, QUADSPI_BUFXCR_INVALID_MSTRID, base + QUADSPI_BUF1CR); |
| 688 | qspi_writel(q, QUADSPI_BUFXCR_INVALID_MSTRID, base + QUADSPI_BUF2CR); |
Allen Xu | 4e898ce | 2015-01-14 00:28:56 +0800 | [diff] [blame] | 689 | /* |
| 690 | * Set ADATSZ with the maximum AHB buffer size to improve the |
| 691 | * read performance. |
| 692 | */ |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 693 | qspi_writel(q, QUADSPI_BUF3CR_ALLMST_MASK | |
| 694 | ((q->devtype_data->ahb_buf_size / 8) |
| 695 | << QUADSPI_BUF3CR_ADATSZ_SHIFT), |
| 696 | base + QUADSPI_BUF3CR); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 697 | |
| 698 | /* We only use the buffer3 */ |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 699 | qspi_writel(q, 0, base + QUADSPI_BUF0IND); |
| 700 | qspi_writel(q, 0, base + QUADSPI_BUF1IND); |
| 701 | qspi_writel(q, 0, base + QUADSPI_BUF2IND); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 702 | |
| 703 | /* Set the default lut sequence for AHB Read. */ |
| 704 | seqid = fsl_qspi_get_seqid(q, q->nor[0].read_opcode); |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 705 | qspi_writel(q, seqid << QUADSPI_BFGENCR_SEQID_SHIFT, |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 706 | q->iobase + QUADSPI_BFGENCR); |
| 707 | } |
| 708 | |
Allen Xu | cacbef4 | 2015-08-04 10:25:58 -0500 | [diff] [blame] | 709 | /* This function was used to prepare and enable QSPI clock */ |
| 710 | static int fsl_qspi_clk_prep_enable(struct fsl_qspi *q) |
| 711 | { |
| 712 | int ret; |
| 713 | |
| 714 | ret = clk_prepare_enable(q->clk_en); |
| 715 | if (ret) |
| 716 | return ret; |
| 717 | |
| 718 | ret = clk_prepare_enable(q->clk); |
| 719 | if (ret) { |
| 720 | clk_disable_unprepare(q->clk_en); |
| 721 | return ret; |
| 722 | } |
| 723 | |
Frank Li | 5cc66cb | 2015-08-04 10:26:04 -0500 | [diff] [blame] | 724 | if (needs_wakeup_wait_mode(q)) |
| 725 | pm_qos_add_request(&q->pm_qos_req, PM_QOS_CPU_DMA_LATENCY, 0); |
| 726 | |
Allen Xu | cacbef4 | 2015-08-04 10:25:58 -0500 | [diff] [blame] | 727 | return 0; |
| 728 | } |
| 729 | |
| 730 | /* This function was used to disable and unprepare QSPI clock */ |
| 731 | static void fsl_qspi_clk_disable_unprep(struct fsl_qspi *q) |
| 732 | { |
Frank Li | 5cc66cb | 2015-08-04 10:26:04 -0500 | [diff] [blame] | 733 | if (needs_wakeup_wait_mode(q)) |
| 734 | pm_qos_remove_request(&q->pm_qos_req); |
| 735 | |
Allen Xu | cacbef4 | 2015-08-04 10:25:58 -0500 | [diff] [blame] | 736 | clk_disable_unprepare(q->clk); |
| 737 | clk_disable_unprepare(q->clk_en); |
| 738 | |
| 739 | } |
| 740 | |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 741 | /* We use this function to do some basic init for spi_nor_scan(). */ |
| 742 | static int fsl_qspi_nor_setup(struct fsl_qspi *q) |
| 743 | { |
| 744 | void __iomem *base = q->iobase; |
| 745 | u32 reg; |
| 746 | int ret; |
| 747 | |
Allen Xu | cacbef4 | 2015-08-04 10:25:58 -0500 | [diff] [blame] | 748 | /* disable and unprepare clock to avoid glitch pass to controller */ |
| 749 | fsl_qspi_clk_disable_unprep(q); |
| 750 | |
| 751 | /* the default frequency, we will change it in the future. */ |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 752 | ret = clk_set_rate(q->clk, 66000000); |
| 753 | if (ret) |
| 754 | return ret; |
| 755 | |
Allen Xu | cacbef4 | 2015-08-04 10:25:58 -0500 | [diff] [blame] | 756 | ret = fsl_qspi_clk_prep_enable(q); |
| 757 | if (ret) |
| 758 | return ret; |
| 759 | |
Frank Li | 8b8319c | 2015-08-04 10:26:10 -0500 | [diff] [blame] | 760 | /* Reset the module */ |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 761 | qspi_writel(q, QUADSPI_MCR_SWRSTSD_MASK | QUADSPI_MCR_SWRSTHD_MASK, |
Frank Li | 8b8319c | 2015-08-04 10:26:10 -0500 | [diff] [blame] | 762 | base + QUADSPI_MCR); |
| 763 | udelay(1); |
| 764 | |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 765 | /* Init the LUT table. */ |
| 766 | fsl_qspi_init_lut(q); |
| 767 | |
| 768 | /* Disable the module */ |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 769 | qspi_writel(q, QUADSPI_MCR_MDIS_MASK | QUADSPI_MCR_RESERVED_MASK, |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 770 | base + QUADSPI_MCR); |
| 771 | |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 772 | reg = qspi_readl(q, base + QUADSPI_SMPR); |
| 773 | qspi_writel(q, reg & ~(QUADSPI_SMPR_FSDLY_MASK |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 774 | | QUADSPI_SMPR_FSPHS_MASK |
| 775 | | QUADSPI_SMPR_HSENA_MASK |
| 776 | | QUADSPI_SMPR_DDRSMP_MASK), base + QUADSPI_SMPR); |
| 777 | |
| 778 | /* Enable the module */ |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 779 | qspi_writel(q, QUADSPI_MCR_RESERVED_MASK | QUADSPI_MCR_END_CFG_MASK, |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 780 | base + QUADSPI_MCR); |
| 781 | |
Frank Li | 8b8319c | 2015-08-04 10:26:10 -0500 | [diff] [blame] | 782 | /* clear all interrupt status */ |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 783 | qspi_writel(q, 0xffffffff, q->iobase + QUADSPI_FR); |
Frank Li | 8b8319c | 2015-08-04 10:26:10 -0500 | [diff] [blame] | 784 | |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 785 | /* enable the interrupt */ |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 786 | qspi_writel(q, QUADSPI_RSER_TFIE, q->iobase + QUADSPI_RSER); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 787 | |
| 788 | return 0; |
| 789 | } |
| 790 | |
| 791 | static int fsl_qspi_nor_setup_last(struct fsl_qspi *q) |
| 792 | { |
| 793 | unsigned long rate = q->clk_rate; |
| 794 | int ret; |
| 795 | |
Han Xu | 80d3772 | 2015-08-04 10:25:29 -0500 | [diff] [blame] | 796 | if (needs_4x_clock(q)) |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 797 | rate *= 4; |
| 798 | |
Allen Xu | cacbef4 | 2015-08-04 10:25:58 -0500 | [diff] [blame] | 799 | /* disable and unprepare clock to avoid glitch pass to controller */ |
| 800 | fsl_qspi_clk_disable_unprep(q); |
| 801 | |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 802 | ret = clk_set_rate(q->clk, rate); |
| 803 | if (ret) |
| 804 | return ret; |
| 805 | |
Allen Xu | cacbef4 | 2015-08-04 10:25:58 -0500 | [diff] [blame] | 806 | ret = fsl_qspi_clk_prep_enable(q); |
| 807 | if (ret) |
| 808 | return ret; |
| 809 | |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 810 | /* Init the LUT table again. */ |
| 811 | fsl_qspi_init_lut(q); |
| 812 | |
| 813 | /* Init for AHB read */ |
| 814 | fsl_qspi_init_abh_read(q); |
| 815 | |
| 816 | return 0; |
| 817 | } |
| 818 | |
Fabian Frederick | 6661044 | 2015-03-16 20:20:28 +0100 | [diff] [blame] | 819 | static const struct of_device_id fsl_qspi_dt_ids[] = { |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 820 | { .compatible = "fsl,vf610-qspi", .data = (void *)&vybrid_data, }, |
| 821 | { .compatible = "fsl,imx6sx-qspi", .data = (void *)&imx6sx_data, }, |
Frank Li | d371cbf | 2015-08-04 10:25:35 -0500 | [diff] [blame] | 822 | { .compatible = "fsl,imx7d-qspi", .data = (void *)&imx7d_data, }, |
Frank Li | 74a081d1 | 2015-08-04 10:25:47 -0500 | [diff] [blame] | 823 | { .compatible = "fsl,imx6ul-qspi", .data = (void *)&imx6ul_data, }, |
Yao Yuan | e8c034b | 2016-01-26 15:23:56 +0800 | [diff] [blame] | 824 | { .compatible = "fsl,ls1021a-qspi", .data = (void *)&ls1021a_data, }, |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 825 | { /* sentinel */ } |
| 826 | }; |
| 827 | MODULE_DEVICE_TABLE(of, fsl_qspi_dt_ids); |
| 828 | |
| 829 | static void fsl_qspi_set_base_addr(struct fsl_qspi *q, struct spi_nor *nor) |
| 830 | { |
| 831 | q->chip_base_addr = q->nor_size * (nor - q->nor); |
| 832 | } |
| 833 | |
| 834 | static int fsl_qspi_read_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len) |
| 835 | { |
| 836 | int ret; |
| 837 | struct fsl_qspi *q = nor->priv; |
| 838 | |
| 839 | ret = fsl_qspi_runcmd(q, opcode, 0, len); |
| 840 | if (ret) |
| 841 | return ret; |
| 842 | |
| 843 | fsl_qspi_read_data(q, len, buf); |
| 844 | return 0; |
| 845 | } |
| 846 | |
Jagan Teki | f9f3ce8 | 2015-08-19 15:26:44 +0530 | [diff] [blame] | 847 | static int fsl_qspi_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len) |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 848 | { |
| 849 | struct fsl_qspi *q = nor->priv; |
| 850 | int ret; |
| 851 | |
| 852 | if (!buf) { |
| 853 | ret = fsl_qspi_runcmd(q, opcode, 0, 1); |
| 854 | if (ret) |
| 855 | return ret; |
| 856 | |
Brian Norris | b02e7f3 | 2014-04-08 18:15:31 -0700 | [diff] [blame] | 857 | if (opcode == SPINOR_OP_CHIP_ERASE) |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 858 | fsl_qspi_invalid(q); |
| 859 | |
| 860 | } else if (len > 0) { |
| 861 | ret = fsl_qspi_nor_write(q, nor, opcode, 0, |
Michal Suchanek | 2dd087b | 2016-05-05 17:31:53 -0700 | [diff] [blame] | 862 | (u32 *)buf, len); |
Michal Suchanek | fc0d7e5 | 2016-05-05 17:31:49 -0700 | [diff] [blame] | 863 | if (ret > 0) |
| 864 | return 0; |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 865 | } else { |
| 866 | dev_err(q->dev, "invalid cmd %d\n", opcode); |
| 867 | ret = -EINVAL; |
| 868 | } |
| 869 | |
| 870 | return ret; |
| 871 | } |
| 872 | |
Michal Suchanek | 59451e1 | 2016-05-05 17:31:47 -0700 | [diff] [blame] | 873 | static ssize_t fsl_qspi_write(struct spi_nor *nor, loff_t to, |
Michal Suchanek | 2dd087b | 2016-05-05 17:31:53 -0700 | [diff] [blame] | 874 | size_t len, const u_char *buf) |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 875 | { |
| 876 | struct fsl_qspi *q = nor->priv; |
Michal Suchanek | fc0d7e5 | 2016-05-05 17:31:49 -0700 | [diff] [blame] | 877 | ssize_t ret = fsl_qspi_nor_write(q, nor, nor->program_opcode, to, |
Michal Suchanek | 2dd087b | 2016-05-05 17:31:53 -0700 | [diff] [blame] | 878 | (u32 *)buf, len); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 879 | |
| 880 | /* invalid the data in the AHB buffer. */ |
| 881 | fsl_qspi_invalid(q); |
Michal Suchanek | fc0d7e5 | 2016-05-05 17:31:49 -0700 | [diff] [blame] | 882 | return ret; |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 883 | } |
| 884 | |
Michal Suchanek | 59451e1 | 2016-05-05 17:31:47 -0700 | [diff] [blame] | 885 | static ssize_t fsl_qspi_read(struct spi_nor *nor, loff_t from, |
Michal Suchanek | 2dd087b | 2016-05-05 17:31:53 -0700 | [diff] [blame] | 886 | size_t len, u_char *buf) |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 887 | { |
| 888 | struct fsl_qspi *q = nor->priv; |
| 889 | u8 cmd = nor->read_opcode; |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 890 | |
Han Xu | 49bd706 | 2015-08-04 10:25:22 -0500 | [diff] [blame] | 891 | /* if necessary,ioremap buffer before AHB read, */ |
| 892 | if (!q->ahb_addr) { |
| 893 | q->memmap_offs = q->chip_base_addr + from; |
| 894 | q->memmap_len = len > QUADSPI_MIN_IOMAP ? len : QUADSPI_MIN_IOMAP; |
| 895 | |
| 896 | q->ahb_addr = ioremap_nocache( |
| 897 | q->memmap_phy + q->memmap_offs, |
| 898 | q->memmap_len); |
| 899 | if (!q->ahb_addr) { |
| 900 | dev_err(q->dev, "ioremap failed\n"); |
| 901 | return -ENOMEM; |
| 902 | } |
| 903 | /* ioremap if the data requested is out of range */ |
| 904 | } else if (q->chip_base_addr + from < q->memmap_offs |
| 905 | || q->chip_base_addr + from + len > |
| 906 | q->memmap_offs + q->memmap_len) { |
| 907 | iounmap(q->ahb_addr); |
| 908 | |
| 909 | q->memmap_offs = q->chip_base_addr + from; |
| 910 | q->memmap_len = len > QUADSPI_MIN_IOMAP ? len : QUADSPI_MIN_IOMAP; |
| 911 | q->ahb_addr = ioremap_nocache( |
| 912 | q->memmap_phy + q->memmap_offs, |
| 913 | q->memmap_len); |
| 914 | if (!q->ahb_addr) { |
| 915 | dev_err(q->dev, "ioremap failed\n"); |
| 916 | return -ENOMEM; |
| 917 | } |
| 918 | } |
| 919 | |
Brian Norris | a5c603a | 2015-10-12 13:35:15 -0700 | [diff] [blame] | 920 | dev_dbg(q->dev, "cmd [%x],read from %p, len:%zd\n", |
Han Xu | 49bd706 | 2015-08-04 10:25:22 -0500 | [diff] [blame] | 921 | cmd, q->ahb_addr + q->chip_base_addr + from - q->memmap_offs, |
| 922 | len); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 923 | |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 924 | /* Read out the data directly from the AHB buffer.*/ |
Han Xu | 49bd706 | 2015-08-04 10:25:22 -0500 | [diff] [blame] | 925 | memcpy(buf, q->ahb_addr + q->chip_base_addr + from - q->memmap_offs, |
| 926 | len); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 927 | |
Michal Suchanek | fc0d7e5 | 2016-05-05 17:31:49 -0700 | [diff] [blame] | 928 | return len; |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | static int fsl_qspi_erase(struct spi_nor *nor, loff_t offs) |
| 932 | { |
| 933 | struct fsl_qspi *q = nor->priv; |
| 934 | int ret; |
| 935 | |
| 936 | dev_dbg(nor->dev, "%dKiB at 0x%08x:0x%08x\n", |
Brian Norris | 1976367 | 2015-08-13 15:46:05 -0700 | [diff] [blame] | 937 | nor->mtd.erasesize / 1024, q->chip_base_addr, (u32)offs); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 938 | |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 939 | ret = fsl_qspi_runcmd(q, nor->erase_opcode, offs, 0); |
| 940 | if (ret) |
| 941 | return ret; |
| 942 | |
| 943 | fsl_qspi_invalid(q); |
| 944 | return 0; |
| 945 | } |
| 946 | |
| 947 | static int fsl_qspi_prep(struct spi_nor *nor, enum spi_nor_ops ops) |
| 948 | { |
| 949 | struct fsl_qspi *q = nor->priv; |
| 950 | int ret; |
| 951 | |
Han Xu | 392d39c | 2015-05-13 14:40:57 -0500 | [diff] [blame] | 952 | mutex_lock(&q->lock); |
Allen Xu | cacbef4 | 2015-08-04 10:25:58 -0500 | [diff] [blame] | 953 | |
| 954 | ret = fsl_qspi_clk_prep_enable(q); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 955 | if (ret) |
Han Xu | 392d39c | 2015-05-13 14:40:57 -0500 | [diff] [blame] | 956 | goto err_mutex; |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 957 | |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 958 | fsl_qspi_set_base_addr(q, nor); |
| 959 | return 0; |
Han Xu | 392d39c | 2015-05-13 14:40:57 -0500 | [diff] [blame] | 960 | |
Han Xu | 392d39c | 2015-05-13 14:40:57 -0500 | [diff] [blame] | 961 | err_mutex: |
| 962 | mutex_unlock(&q->lock); |
Han Xu | 392d39c | 2015-05-13 14:40:57 -0500 | [diff] [blame] | 963 | return ret; |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | static void fsl_qspi_unprep(struct spi_nor *nor, enum spi_nor_ops ops) |
| 967 | { |
| 968 | struct fsl_qspi *q = nor->priv; |
| 969 | |
Allen Xu | cacbef4 | 2015-08-04 10:25:58 -0500 | [diff] [blame] | 970 | fsl_qspi_clk_disable_unprep(q); |
Han Xu | 392d39c | 2015-05-13 14:40:57 -0500 | [diff] [blame] | 971 | mutex_unlock(&q->lock); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | static int fsl_qspi_probe(struct platform_device *pdev) |
| 975 | { |
| 976 | struct device_node *np = pdev->dev.of_node; |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 977 | struct device *dev = &pdev->dev; |
| 978 | struct fsl_qspi *q; |
| 979 | struct resource *res; |
| 980 | struct spi_nor *nor; |
| 981 | struct mtd_info *mtd; |
| 982 | int ret, i = 0; |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 983 | |
| 984 | q = devm_kzalloc(dev, sizeof(*q), GFP_KERNEL); |
| 985 | if (!q) |
| 986 | return -ENOMEM; |
| 987 | |
| 988 | q->nor_num = of_get_child_count(dev->of_node); |
| 989 | if (!q->nor_num || q->nor_num > FSL_QSPI_MAX_CHIP) |
| 990 | return -ENODEV; |
| 991 | |
Frank Li | 5cc66cb | 2015-08-04 10:26:04 -0500 | [diff] [blame] | 992 | q->dev = dev; |
Brian Norris | c98f71d | 2015-11-16 10:45:30 -0800 | [diff] [blame] | 993 | q->devtype_data = of_device_get_match_data(dev); |
| 994 | if (!q->devtype_data) |
| 995 | return -ENODEV; |
Frank Li | 5cc66cb | 2015-08-04 10:26:04 -0500 | [diff] [blame] | 996 | platform_set_drvdata(pdev, q); |
| 997 | |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 998 | /* find the resources */ |
| 999 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "QuadSPI"); |
| 1000 | q->iobase = devm_ioremap_resource(dev, res); |
Fabio Estevam | b1ab474 | 2015-01-22 22:43:07 -0200 | [diff] [blame] | 1001 | if (IS_ERR(q->iobase)) |
| 1002 | return PTR_ERR(q->iobase); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 1003 | |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 1004 | q->big_endian = of_property_read_bool(np, "big-endian"); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 1005 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, |
| 1006 | "QuadSPI-memory"); |
Han Xu | 49bd706 | 2015-08-04 10:25:22 -0500 | [diff] [blame] | 1007 | if (!devm_request_mem_region(dev, res->start, resource_size(res), |
| 1008 | res->name)) { |
| 1009 | dev_err(dev, "can't request region for resource %pR\n", res); |
| 1010 | return -EBUSY; |
| 1011 | } |
Fabio Estevam | b1ab474 | 2015-01-22 22:43:07 -0200 | [diff] [blame] | 1012 | |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 1013 | q->memmap_phy = res->start; |
| 1014 | |
| 1015 | /* find the clocks */ |
| 1016 | q->clk_en = devm_clk_get(dev, "qspi_en"); |
Fabio Estevam | b1ab474 | 2015-01-22 22:43:07 -0200 | [diff] [blame] | 1017 | if (IS_ERR(q->clk_en)) |
| 1018 | return PTR_ERR(q->clk_en); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 1019 | |
| 1020 | q->clk = devm_clk_get(dev, "qspi"); |
Fabio Estevam | b1ab474 | 2015-01-22 22:43:07 -0200 | [diff] [blame] | 1021 | if (IS_ERR(q->clk)) |
| 1022 | return PTR_ERR(q->clk); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 1023 | |
Allen Xu | cacbef4 | 2015-08-04 10:25:58 -0500 | [diff] [blame] | 1024 | ret = fsl_qspi_clk_prep_enable(q); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 1025 | if (ret) { |
Allen Xu | cacbef4 | 2015-08-04 10:25:58 -0500 | [diff] [blame] | 1026 | dev_err(dev, "can not enable the clock\n"); |
Fabio Estevam | 77adc08 | 2014-10-17 17:14:01 -0300 | [diff] [blame] | 1027 | goto clk_failed; |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | /* find the irq */ |
| 1031 | ret = platform_get_irq(pdev, 0); |
| 1032 | if (ret < 0) { |
Fabio Estevam | dc6525c | 2015-02-09 10:07:19 -0200 | [diff] [blame] | 1033 | dev_err(dev, "failed to get the irq: %d\n", ret); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 1034 | goto irq_failed; |
| 1035 | } |
| 1036 | |
| 1037 | ret = devm_request_irq(dev, ret, |
| 1038 | fsl_qspi_irq_handler, 0, pdev->name, q); |
| 1039 | if (ret) { |
Fabio Estevam | dc6525c | 2015-02-09 10:07:19 -0200 | [diff] [blame] | 1040 | dev_err(dev, "failed to request irq: %d\n", ret); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 1041 | goto irq_failed; |
| 1042 | } |
| 1043 | |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 1044 | ret = fsl_qspi_nor_setup(q); |
| 1045 | if (ret) |
| 1046 | goto irq_failed; |
| 1047 | |
| 1048 | if (of_get_property(np, "fsl,qspi-has-second-chip", NULL)) |
Fabio Estevam | cfe4af3 | 2015-01-13 20:14:15 -0200 | [diff] [blame] | 1049 | q->has_second_chip = true; |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 1050 | |
Han Xu | 392d39c | 2015-05-13 14:40:57 -0500 | [diff] [blame] | 1051 | mutex_init(&q->lock); |
| 1052 | |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 1053 | /* iterate the subnodes. */ |
| 1054 | for_each_available_child_of_node(dev->of_node, np) { |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 1055 | /* skip the holes */ |
Fabio Estevam | cfe4af3 | 2015-01-13 20:14:15 -0200 | [diff] [blame] | 1056 | if (!q->has_second_chip) |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 1057 | i *= 2; |
| 1058 | |
| 1059 | nor = &q->nor[i]; |
Brian Norris | 1976367 | 2015-08-13 15:46:05 -0700 | [diff] [blame] | 1060 | mtd = &nor->mtd; |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 1061 | |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 1062 | nor->dev = dev; |
Brian Norris | 9c7d787 | 2015-10-30 20:33:24 -0700 | [diff] [blame] | 1063 | spi_nor_set_flash_node(nor, np); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 1064 | nor->priv = q; |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 1065 | |
| 1066 | /* fill the hooks */ |
| 1067 | nor->read_reg = fsl_qspi_read_reg; |
| 1068 | nor->write_reg = fsl_qspi_write_reg; |
| 1069 | nor->read = fsl_qspi_read; |
| 1070 | nor->write = fsl_qspi_write; |
| 1071 | nor->erase = fsl_qspi_erase; |
| 1072 | |
| 1073 | nor->prepare = fsl_qspi_prep; |
| 1074 | nor->unprepare = fsl_qspi_unprep; |
| 1075 | |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 1076 | ret = of_property_read_u32(np, "spi-max-frequency", |
| 1077 | &q->clk_rate); |
| 1078 | if (ret < 0) |
Han Xu | 392d39c | 2015-05-13 14:40:57 -0500 | [diff] [blame] | 1079 | goto mutex_failed; |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 1080 | |
| 1081 | /* set the chip address for READID */ |
| 1082 | fsl_qspi_set_base_addr(q, nor); |
| 1083 | |
Brian Norris | e747dbe | 2015-08-13 15:46:02 -0700 | [diff] [blame] | 1084 | ret = spi_nor_scan(nor, NULL, SPI_NOR_QUAD); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 1085 | if (ret) |
Han Xu | 392d39c | 2015-05-13 14:40:57 -0500 | [diff] [blame] | 1086 | goto mutex_failed; |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 1087 | |
Brian Norris | df02c88 | 2015-10-30 20:33:26 -0700 | [diff] [blame] | 1088 | ret = mtd_device_register(mtd, NULL, 0); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 1089 | if (ret) |
Han Xu | 392d39c | 2015-05-13 14:40:57 -0500 | [diff] [blame] | 1090 | goto mutex_failed; |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 1091 | |
| 1092 | /* Set the correct NOR size now. */ |
| 1093 | if (q->nor_size == 0) { |
| 1094 | q->nor_size = mtd->size; |
| 1095 | |
| 1096 | /* Map the SPI NOR to accessiable address */ |
| 1097 | fsl_qspi_set_map_addr(q); |
| 1098 | } |
| 1099 | |
| 1100 | /* |
| 1101 | * The TX FIFO is 64 bytes in the Vybrid, but the Page Program |
| 1102 | * may writes 265 bytes per time. The write is working in the |
| 1103 | * unit of the TX FIFO, not in the unit of the SPI NOR's page |
| 1104 | * size. |
| 1105 | * |
| 1106 | * So shrink the spi_nor->page_size if it is larger then the |
| 1107 | * TX FIFO. |
| 1108 | */ |
| 1109 | if (nor->page_size > q->devtype_data->txfifo) |
| 1110 | nor->page_size = q->devtype_data->txfifo; |
| 1111 | |
| 1112 | i++; |
| 1113 | } |
| 1114 | |
| 1115 | /* finish the rest init. */ |
| 1116 | ret = fsl_qspi_nor_setup_last(q); |
| 1117 | if (ret) |
| 1118 | goto last_init_failed; |
| 1119 | |
Allen Xu | cacbef4 | 2015-08-04 10:25:58 -0500 | [diff] [blame] | 1120 | fsl_qspi_clk_disable_unprep(q); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 1121 | return 0; |
| 1122 | |
| 1123 | last_init_failed: |
Fabio Estevam | cfe4af3 | 2015-01-13 20:14:15 -0200 | [diff] [blame] | 1124 | for (i = 0; i < q->nor_num; i++) { |
| 1125 | /* skip the holes */ |
| 1126 | if (!q->has_second_chip) |
| 1127 | i *= 2; |
Brian Norris | 1976367 | 2015-08-13 15:46:05 -0700 | [diff] [blame] | 1128 | mtd_device_unregister(&q->nor[i].mtd); |
Fabio Estevam | cfe4af3 | 2015-01-13 20:14:15 -0200 | [diff] [blame] | 1129 | } |
Han Xu | 392d39c | 2015-05-13 14:40:57 -0500 | [diff] [blame] | 1130 | mutex_failed: |
| 1131 | mutex_destroy(&q->lock); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 1132 | irq_failed: |
Allen Xu | cacbef4 | 2015-08-04 10:25:58 -0500 | [diff] [blame] | 1133 | fsl_qspi_clk_disable_unprep(q); |
Fabio Estevam | 77adc08 | 2014-10-17 17:14:01 -0300 | [diff] [blame] | 1134 | clk_failed: |
Allen Xu | cacbef4 | 2015-08-04 10:25:58 -0500 | [diff] [blame] | 1135 | dev_err(dev, "Freescale QuadSPI probe failed\n"); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 1136 | return ret; |
| 1137 | } |
| 1138 | |
| 1139 | static int fsl_qspi_remove(struct platform_device *pdev) |
| 1140 | { |
| 1141 | struct fsl_qspi *q = platform_get_drvdata(pdev); |
| 1142 | int i; |
| 1143 | |
Fabio Estevam | cfe4af3 | 2015-01-13 20:14:15 -0200 | [diff] [blame] | 1144 | for (i = 0; i < q->nor_num; i++) { |
| 1145 | /* skip the holes */ |
| 1146 | if (!q->has_second_chip) |
| 1147 | i *= 2; |
Brian Norris | 1976367 | 2015-08-13 15:46:05 -0700 | [diff] [blame] | 1148 | mtd_device_unregister(&q->nor[i].mtd); |
Fabio Estevam | cfe4af3 | 2015-01-13 20:14:15 -0200 | [diff] [blame] | 1149 | } |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 1150 | |
| 1151 | /* disable the hardware */ |
Yao Yuan | 2012850 | 2016-01-26 15:23:55 +0800 | [diff] [blame] | 1152 | qspi_writel(q, QUADSPI_MCR_MDIS_MASK, q->iobase + QUADSPI_MCR); |
| 1153 | qspi_writel(q, 0x0, q->iobase + QUADSPI_RSER); |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 1154 | |
Han Xu | 392d39c | 2015-05-13 14:40:57 -0500 | [diff] [blame] | 1155 | mutex_destroy(&q->lock); |
Han Xu | 49bd706 | 2015-08-04 10:25:22 -0500 | [diff] [blame] | 1156 | |
| 1157 | if (q->ahb_addr) |
| 1158 | iounmap(q->ahb_addr); |
| 1159 | |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 1160 | return 0; |
| 1161 | } |
| 1162 | |
Allen Xu | 45c6a0c | 2015-01-13 04:56:40 +0800 | [diff] [blame] | 1163 | static int fsl_qspi_suspend(struct platform_device *pdev, pm_message_t state) |
| 1164 | { |
| 1165 | return 0; |
| 1166 | } |
| 1167 | |
| 1168 | static int fsl_qspi_resume(struct platform_device *pdev) |
| 1169 | { |
Allen Xu | cacbef4 | 2015-08-04 10:25:58 -0500 | [diff] [blame] | 1170 | int ret; |
Allen Xu | 45c6a0c | 2015-01-13 04:56:40 +0800 | [diff] [blame] | 1171 | struct fsl_qspi *q = platform_get_drvdata(pdev); |
| 1172 | |
Allen Xu | cacbef4 | 2015-08-04 10:25:58 -0500 | [diff] [blame] | 1173 | ret = fsl_qspi_clk_prep_enable(q); |
| 1174 | if (ret) |
| 1175 | return ret; |
| 1176 | |
Allen Xu | 45c6a0c | 2015-01-13 04:56:40 +0800 | [diff] [blame] | 1177 | fsl_qspi_nor_setup(q); |
| 1178 | fsl_qspi_set_map_addr(q); |
| 1179 | fsl_qspi_nor_setup_last(q); |
| 1180 | |
Allen Xu | cacbef4 | 2015-08-04 10:25:58 -0500 | [diff] [blame] | 1181 | fsl_qspi_clk_disable_unprep(q); |
| 1182 | |
Allen Xu | 45c6a0c | 2015-01-13 04:56:40 +0800 | [diff] [blame] | 1183 | return 0; |
| 1184 | } |
| 1185 | |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 1186 | static struct platform_driver fsl_qspi_driver = { |
| 1187 | .driver = { |
| 1188 | .name = "fsl-quadspi", |
| 1189 | .bus = &platform_bus_type, |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 1190 | .of_match_table = fsl_qspi_dt_ids, |
| 1191 | }, |
| 1192 | .probe = fsl_qspi_probe, |
| 1193 | .remove = fsl_qspi_remove, |
Allen Xu | 45c6a0c | 2015-01-13 04:56:40 +0800 | [diff] [blame] | 1194 | .suspend = fsl_qspi_suspend, |
| 1195 | .resume = fsl_qspi_resume, |
Huang Shijie | e46ecda | 2014-02-24 18:37:42 +0800 | [diff] [blame] | 1196 | }; |
| 1197 | module_platform_driver(fsl_qspi_driver); |
| 1198 | |
| 1199 | MODULE_DESCRIPTION("Freescale QuadSPI Controller Driver"); |
| 1200 | MODULE_AUTHOR("Freescale Semiconductor Inc."); |
| 1201 | MODULE_LICENSE("GPL v2"); |