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