Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 2 | /* |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 3 | * Copyright 2013-2015 Freescale Semiconductor, Inc. |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 4 | * |
| 5 | * Freescale Quad Serial Peripheral Interface (QSPI) driver |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <malloc.h> |
| 10 | #include <spi.h> |
| 11 | #include <asm/io.h> |
| 12 | #include <linux/sizes.h> |
Thomas Schaefer | f751d85 | 2019-07-01 17:37:35 +0200 | [diff] [blame] | 13 | #include <linux/iopoll.h> |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 14 | #include <dm.h> |
| 15 | #include <errno.h> |
Alexander Stein | 9b2c8a9 | 2015-11-04 09:19:10 +0100 | [diff] [blame] | 16 | #include <watchdog.h> |
Suresh Gupta | 4945b87 | 2017-08-30 20:06:33 +0530 | [diff] [blame] | 17 | #include <wait_bit.h> |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 18 | #include "fsl_qspi.h" |
| 19 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 22 | #define RX_BUFFER_SIZE 0x80 |
Peng Fan | b3b1f5a | 2018-01-03 08:52:02 +0800 | [diff] [blame] | 23 | #if defined(CONFIG_MX6SX) || defined(CONFIG_MX6UL) || \ |
| 24 | defined(CONFIG_MX6ULL) || defined(CONFIG_MX7D) |
Peng Fan | 828e468 | 2014-12-31 11:01:38 +0800 | [diff] [blame] | 25 | #define TX_BUFFER_SIZE 0x200 |
| 26 | #else |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 27 | #define TX_BUFFER_SIZE 0x40 |
Peng Fan | 828e468 | 2014-12-31 11:01:38 +0800 | [diff] [blame] | 28 | #endif |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 29 | |
Gong Qianyu | 44f65f8 | 2015-12-14 18:26:25 +0800 | [diff] [blame] | 30 | #define OFFSET_BITS_MASK GENMASK(23, 0) |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 31 | |
| 32 | #define FLASH_STATUS_WEL 0x02 |
| 33 | |
| 34 | /* SEQID */ |
| 35 | #define SEQID_WREN 1 |
| 36 | #define SEQID_FAST_READ 2 |
| 37 | #define SEQID_RDSR 3 |
| 38 | #define SEQID_SE 4 |
| 39 | #define SEQID_CHIP_ERASE 5 |
| 40 | #define SEQID_PP 6 |
| 41 | #define SEQID_RDID 7 |
Peng Fan | 3642a87 | 2014-12-31 11:01:39 +0800 | [diff] [blame] | 42 | #define SEQID_BE_4K 8 |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 43 | #ifdef CONFIG_SPI_FLASH_BAR |
| 44 | #define SEQID_BRRD 9 |
| 45 | #define SEQID_BRWR 10 |
| 46 | #define SEQID_RDEAR 11 |
| 47 | #define SEQID_WREAR 12 |
| 48 | #endif |
Yuan Yao | d719326 | 2016-03-15 14:36:42 +0800 | [diff] [blame] | 49 | #define SEQID_WRAR 13 |
| 50 | #define SEQID_RDAR 14 |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 51 | |
Peng Fan | ff16234 | 2014-12-31 11:01:36 +0800 | [diff] [blame] | 52 | /* QSPI CMD */ |
| 53 | #define QSPI_CMD_PP 0x02 /* Page program (up to 256 bytes) */ |
| 54 | #define QSPI_CMD_RDSR 0x05 /* Read status register */ |
| 55 | #define QSPI_CMD_WREN 0x06 /* Write enable */ |
| 56 | #define QSPI_CMD_FAST_READ 0x0b /* Read data bytes (high frequency) */ |
Peng Fan | 3642a87 | 2014-12-31 11:01:39 +0800 | [diff] [blame] | 57 | #define QSPI_CMD_BE_4K 0x20 /* 4K erase */ |
Peng Fan | ff16234 | 2014-12-31 11:01:36 +0800 | [diff] [blame] | 58 | #define QSPI_CMD_CHIP_ERASE 0xc7 /* Erase whole flash chip */ |
| 59 | #define QSPI_CMD_SE 0xd8 /* Sector erase (usually 64KiB) */ |
| 60 | #define QSPI_CMD_RDID 0x9f /* Read JEDEC ID */ |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 61 | |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 62 | /* Used for Micron, winbond and Macronix flashes */ |
| 63 | #define QSPI_CMD_WREAR 0xc5 /* EAR register write */ |
| 64 | #define QSPI_CMD_RDEAR 0xc8 /* EAR reigster read */ |
| 65 | |
| 66 | /* Used for Spansion flashes only. */ |
| 67 | #define QSPI_CMD_BRRD 0x16 /* Bank register read */ |
| 68 | #define QSPI_CMD_BRWR 0x17 /* Bank register write */ |
| 69 | |
Yuan Yao | d719326 | 2016-03-15 14:36:42 +0800 | [diff] [blame] | 70 | /* Used for Spansion S25FS-S family flash only. */ |
| 71 | #define QSPI_CMD_RDAR 0x65 /* Read any device register */ |
| 72 | #define QSPI_CMD_WRAR 0x71 /* Write any device register */ |
| 73 | |
Peng Fan | ff16234 | 2014-12-31 11:01:36 +0800 | [diff] [blame] | 74 | /* 4-byte address QSPI CMD - used on Spansion and some Macronix flashes */ |
| 75 | #define QSPI_CMD_FAST_READ_4B 0x0c /* Read data bytes (high frequency) */ |
| 76 | #define QSPI_CMD_PP_4B 0x12 /* Page program (up to 256 bytes) */ |
| 77 | #define QSPI_CMD_SE_4B 0xdc /* Sector erase (usually 64KiB) */ |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 78 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 79 | /* fsl_qspi_platdata flags */ |
Jagan Teki | c97ca92 | 2015-10-23 01:37:18 +0530 | [diff] [blame] | 80 | #define QSPI_FLAG_REGMAP_ENDIAN_BIG BIT(0) |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 81 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 82 | /* default SCK frequency, unit: HZ */ |
| 83 | #define FSL_QSPI_DEFAULT_SCK_FREQ 50000000 |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 84 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 85 | /* QSPI max chipselect signals number */ |
| 86 | #define FSL_QSPI_MAX_CHIPSELECT_NUM 4 |
| 87 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 88 | /** |
| 89 | * struct fsl_qspi_platdata - platform data for Freescale QSPI |
| 90 | * |
| 91 | * @flags: Flags for QSPI QSPI_FLAG_... |
| 92 | * @speed_hz: Default SCK frequency |
| 93 | * @reg_base: Base address of QSPI registers |
| 94 | * @amba_base: Base address of QSPI memory mapping |
| 95 | * @amba_total_size: size of QSPI memory mapping |
| 96 | * @flash_num: Number of active slave devices |
| 97 | * @num_chipselect: Number of QSPI chipselect signals |
| 98 | */ |
| 99 | struct fsl_qspi_platdata { |
| 100 | u32 flags; |
| 101 | u32 speed_hz; |
Yuan Yao | ae41239 | 2016-03-15 14:36:40 +0800 | [diff] [blame] | 102 | fdt_addr_t reg_base; |
| 103 | fdt_addr_t amba_base; |
| 104 | fdt_size_t amba_total_size; |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 105 | u32 flash_num; |
| 106 | u32 num_chipselect; |
| 107 | }; |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * struct fsl_qspi_priv - private data for Freescale QSPI |
| 111 | * |
| 112 | * @flags: Flags for QSPI QSPI_FLAG_... |
| 113 | * @bus_clk: QSPI input clk frequency |
| 114 | * @speed_hz: Default SCK frequency |
| 115 | * @cur_seqid: current LUT table sequence id |
| 116 | * @sf_addr: flash access offset |
| 117 | * @amba_base: Base address of QSPI memory mapping of every CS |
| 118 | * @amba_total_size: size of QSPI memory mapping |
| 119 | * @cur_amba_base: Base address of QSPI memory mapping of current CS |
| 120 | * @flash_num: Number of active slave devices |
| 121 | * @num_chipselect: Number of QSPI chipselect signals |
| 122 | * @regs: Point to QSPI register structure for I/O access |
| 123 | */ |
| 124 | struct fsl_qspi_priv { |
| 125 | u32 flags; |
| 126 | u32 bus_clk; |
| 127 | u32 speed_hz; |
| 128 | u32 cur_seqid; |
| 129 | u32 sf_addr; |
| 130 | u32 amba_base[FSL_QSPI_MAX_CHIPSELECT_NUM]; |
| 131 | u32 amba_total_size; |
| 132 | u32 cur_amba_base; |
| 133 | u32 flash_num; |
| 134 | u32 num_chipselect; |
| 135 | struct fsl_qspi_regs *regs; |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 136 | }; |
| 137 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 138 | |
| 139 | static u32 qspi_read32(u32 flags, u32 *addr) |
| 140 | { |
| 141 | return flags & QSPI_FLAG_REGMAP_ENDIAN_BIG ? |
| 142 | in_be32(addr) : in_le32(addr); |
| 143 | } |
| 144 | |
| 145 | static void qspi_write32(u32 flags, u32 *addr, u32 val) |
| 146 | { |
| 147 | flags & QSPI_FLAG_REGMAP_ENDIAN_BIG ? |
| 148 | out_be32(addr, val) : out_le32(addr, val); |
| 149 | } |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 150 | |
Rajat Srivastava | 234daec | 2018-03-22 13:30:55 +0530 | [diff] [blame] | 151 | static inline int is_controller_busy(const struct fsl_qspi_priv *priv) |
| 152 | { |
| 153 | u32 val; |
Thomas Schaefer | f751d85 | 2019-07-01 17:37:35 +0200 | [diff] [blame] | 154 | u32 mask = QSPI_SR_BUSY_MASK | QSPI_SR_AHB_ACC_MASK | |
| 155 | QSPI_SR_IP_ACC_MASK; |
Rajat Srivastava | 234daec | 2018-03-22 13:30:55 +0530 | [diff] [blame] | 156 | |
Thomas Schaefer | f751d85 | 2019-07-01 17:37:35 +0200 | [diff] [blame] | 157 | if (priv->flags & QSPI_FLAG_REGMAP_ENDIAN_BIG) |
| 158 | mask = (u32)cpu_to_be32(mask); |
Rajat Srivastava | 234daec | 2018-03-22 13:30:55 +0530 | [diff] [blame] | 159 | |
Thomas Schaefer | f751d85 | 2019-07-01 17:37:35 +0200 | [diff] [blame] | 160 | return readl_poll_timeout(&priv->regs->sr, val, !(val & mask), 1000); |
Rajat Srivastava | 234daec | 2018-03-22 13:30:55 +0530 | [diff] [blame] | 161 | } |
| 162 | |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 163 | /* QSPI support swapping the flash read/write data |
| 164 | * in hardware for LS102xA, but not for VF610 */ |
| 165 | static inline u32 qspi_endian_xchg(u32 data) |
| 166 | { |
| 167 | #ifdef CONFIG_VF610 |
| 168 | return swab32(data); |
| 169 | #else |
| 170 | return data; |
| 171 | #endif |
| 172 | } |
| 173 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 174 | static void qspi_set_lut(struct fsl_qspi_priv *priv) |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 175 | { |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 176 | struct fsl_qspi_regs *regs = priv->regs; |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 177 | u32 lut_base; |
| 178 | |
| 179 | /* Unlock the LUT */ |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 180 | qspi_write32(priv->flags, ®s->lutkey, LUT_KEY_VALUE); |
| 181 | qspi_write32(priv->flags, ®s->lckcr, QSPI_LCKCR_UNLOCK); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 182 | |
| 183 | /* Write Enable */ |
| 184 | lut_base = SEQID_WREN * 4; |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 185 | qspi_write32(priv->flags, ®s->lut[lut_base], OPRND0(QSPI_CMD_WREN) | |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 186 | PAD0(LUT_PAD1) | INSTR0(LUT_CMD)); |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 187 | qspi_write32(priv->flags, ®s->lut[lut_base + 1], 0); |
| 188 | qspi_write32(priv->flags, ®s->lut[lut_base + 2], 0); |
| 189 | qspi_write32(priv->flags, ®s->lut[lut_base + 3], 0); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 190 | |
| 191 | /* Fast Read */ |
| 192 | lut_base = SEQID_FAST_READ * 4; |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 193 | #ifdef CONFIG_SPI_FLASH_BAR |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 194 | qspi_write32(priv->flags, ®s->lut[lut_base], |
| 195 | OPRND0(QSPI_CMD_FAST_READ) | PAD0(LUT_PAD1) | |
| 196 | INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) | |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 197 | PAD1(LUT_PAD1) | INSTR1(LUT_ADDR)); |
| 198 | #else |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 199 | if (FSL_QSPI_FLASH_SIZE <= SZ_16M) |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 200 | qspi_write32(priv->flags, ®s->lut[lut_base], |
| 201 | OPRND0(QSPI_CMD_FAST_READ) | PAD0(LUT_PAD1) | |
| 202 | INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) | |
| 203 | PAD1(LUT_PAD1) | INSTR1(LUT_ADDR)); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 204 | else |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 205 | qspi_write32(priv->flags, ®s->lut[lut_base], |
Peng Fan | ff16234 | 2014-12-31 11:01:36 +0800 | [diff] [blame] | 206 | OPRND0(QSPI_CMD_FAST_READ_4B) | |
| 207 | PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | |
| 208 | OPRND1(ADDR32BIT) | PAD1(LUT_PAD1) | |
| 209 | INSTR1(LUT_ADDR)); |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 210 | #endif |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 211 | qspi_write32(priv->flags, ®s->lut[lut_base + 1], |
| 212 | OPRND0(8) | PAD0(LUT_PAD1) | INSTR0(LUT_DUMMY) | |
| 213 | OPRND1(RX_BUFFER_SIZE) | PAD1(LUT_PAD1) | |
| 214 | INSTR1(LUT_READ)); |
| 215 | qspi_write32(priv->flags, ®s->lut[lut_base + 2], 0); |
| 216 | qspi_write32(priv->flags, ®s->lut[lut_base + 3], 0); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 217 | |
| 218 | /* Read Status */ |
| 219 | lut_base = SEQID_RDSR * 4; |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 220 | qspi_write32(priv->flags, ®s->lut[lut_base], OPRND0(QSPI_CMD_RDSR) | |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 221 | PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) | |
| 222 | PAD1(LUT_PAD1) | INSTR1(LUT_READ)); |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 223 | qspi_write32(priv->flags, ®s->lut[lut_base + 1], 0); |
| 224 | qspi_write32(priv->flags, ®s->lut[lut_base + 2], 0); |
| 225 | qspi_write32(priv->flags, ®s->lut[lut_base + 3], 0); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 226 | |
| 227 | /* Erase a sector */ |
| 228 | lut_base = SEQID_SE * 4; |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 229 | #ifdef CONFIG_SPI_FLASH_BAR |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 230 | qspi_write32(priv->flags, ®s->lut[lut_base], OPRND0(QSPI_CMD_SE) | |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 231 | PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) | |
| 232 | PAD1(LUT_PAD1) | INSTR1(LUT_ADDR)); |
| 233 | #else |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 234 | if (FSL_QSPI_FLASH_SIZE <= SZ_16M) |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 235 | qspi_write32(priv->flags, ®s->lut[lut_base], |
| 236 | OPRND0(QSPI_CMD_SE) | PAD0(LUT_PAD1) | |
| 237 | INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) | |
| 238 | PAD1(LUT_PAD1) | INSTR1(LUT_ADDR)); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 239 | else |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 240 | qspi_write32(priv->flags, ®s->lut[lut_base], |
| 241 | OPRND0(QSPI_CMD_SE_4B) | PAD0(LUT_PAD1) | |
| 242 | INSTR0(LUT_CMD) | OPRND1(ADDR32BIT) | |
| 243 | PAD1(LUT_PAD1) | INSTR1(LUT_ADDR)); |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 244 | #endif |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 245 | qspi_write32(priv->flags, ®s->lut[lut_base + 1], 0); |
| 246 | qspi_write32(priv->flags, ®s->lut[lut_base + 2], 0); |
| 247 | qspi_write32(priv->flags, ®s->lut[lut_base + 3], 0); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 248 | |
| 249 | /* Erase the whole chip */ |
| 250 | lut_base = SEQID_CHIP_ERASE * 4; |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 251 | qspi_write32(priv->flags, ®s->lut[lut_base], |
| 252 | OPRND0(QSPI_CMD_CHIP_ERASE) | |
| 253 | PAD0(LUT_PAD1) | INSTR0(LUT_CMD)); |
| 254 | qspi_write32(priv->flags, ®s->lut[lut_base + 1], 0); |
| 255 | qspi_write32(priv->flags, ®s->lut[lut_base + 2], 0); |
| 256 | qspi_write32(priv->flags, ®s->lut[lut_base + 3], 0); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 257 | |
| 258 | /* Page Program */ |
| 259 | lut_base = SEQID_PP * 4; |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 260 | #ifdef CONFIG_SPI_FLASH_BAR |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 261 | qspi_write32(priv->flags, ®s->lut[lut_base], OPRND0(QSPI_CMD_PP) | |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 262 | PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) | |
| 263 | PAD1(LUT_PAD1) | INSTR1(LUT_ADDR)); |
| 264 | #else |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 265 | if (FSL_QSPI_FLASH_SIZE <= SZ_16M) |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 266 | qspi_write32(priv->flags, ®s->lut[lut_base], |
| 267 | OPRND0(QSPI_CMD_PP) | PAD0(LUT_PAD1) | |
| 268 | INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) | |
| 269 | PAD1(LUT_PAD1) | INSTR1(LUT_ADDR)); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 270 | else |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 271 | qspi_write32(priv->flags, ®s->lut[lut_base], |
| 272 | OPRND0(QSPI_CMD_PP_4B) | PAD0(LUT_PAD1) | |
| 273 | INSTR0(LUT_CMD) | OPRND1(ADDR32BIT) | |
| 274 | PAD1(LUT_PAD1) | INSTR1(LUT_ADDR)); |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 275 | #endif |
Peng Fan | b3b1f5a | 2018-01-03 08:52:02 +0800 | [diff] [blame] | 276 | #if defined(CONFIG_MX6SX) || defined(CONFIG_MX6UL) || \ |
| 277 | defined(CONFIG_MX6ULL) || defined(CONFIG_MX7D) |
Peng Fan | 828e468 | 2014-12-31 11:01:38 +0800 | [diff] [blame] | 278 | /* |
| 279 | * To MX6SX, OPRND0(TX_BUFFER_SIZE) can not work correctly. |
| 280 | * So, Use IDATSZ in IPCR to determine the size and here set 0. |
| 281 | */ |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 282 | qspi_write32(priv->flags, ®s->lut[lut_base + 1], OPRND0(0) | |
Peng Fan | 828e468 | 2014-12-31 11:01:38 +0800 | [diff] [blame] | 283 | PAD0(LUT_PAD1) | INSTR0(LUT_WRITE)); |
| 284 | #else |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 285 | qspi_write32(priv->flags, ®s->lut[lut_base + 1], |
| 286 | OPRND0(TX_BUFFER_SIZE) | |
| 287 | PAD0(LUT_PAD1) | INSTR0(LUT_WRITE)); |
Peng Fan | 828e468 | 2014-12-31 11:01:38 +0800 | [diff] [blame] | 288 | #endif |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 289 | qspi_write32(priv->flags, ®s->lut[lut_base + 2], 0); |
| 290 | qspi_write32(priv->flags, ®s->lut[lut_base + 3], 0); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 291 | |
| 292 | /* READ ID */ |
| 293 | lut_base = SEQID_RDID * 4; |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 294 | qspi_write32(priv->flags, ®s->lut[lut_base], OPRND0(QSPI_CMD_RDID) | |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 295 | PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(8) | |
| 296 | PAD1(LUT_PAD1) | INSTR1(LUT_READ)); |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 297 | qspi_write32(priv->flags, ®s->lut[lut_base + 1], 0); |
| 298 | qspi_write32(priv->flags, ®s->lut[lut_base + 2], 0); |
| 299 | qspi_write32(priv->flags, ®s->lut[lut_base + 3], 0); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 300 | |
Peng Fan | 3642a87 | 2014-12-31 11:01:39 +0800 | [diff] [blame] | 301 | /* SUB SECTOR 4K ERASE */ |
| 302 | lut_base = SEQID_BE_4K * 4; |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 303 | qspi_write32(priv->flags, ®s->lut[lut_base], OPRND0(QSPI_CMD_BE_4K) | |
Peng Fan | 3642a87 | 2014-12-31 11:01:39 +0800 | [diff] [blame] | 304 | PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) | |
| 305 | PAD1(LUT_PAD1) | INSTR1(LUT_ADDR)); |
| 306 | |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 307 | #ifdef CONFIG_SPI_FLASH_BAR |
| 308 | /* |
| 309 | * BRRD BRWR RDEAR WREAR are all supported, because it is hard to |
| 310 | * dynamically check whether to set BRRD BRWR or RDEAR WREAR during |
| 311 | * initialization. |
| 312 | */ |
| 313 | lut_base = SEQID_BRRD * 4; |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 314 | qspi_write32(priv->flags, ®s->lut[lut_base], OPRND0(QSPI_CMD_BRRD) | |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 315 | PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) | |
| 316 | PAD1(LUT_PAD1) | INSTR1(LUT_READ)); |
| 317 | |
| 318 | lut_base = SEQID_BRWR * 4; |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 319 | qspi_write32(priv->flags, ®s->lut[lut_base], OPRND0(QSPI_CMD_BRWR) | |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 320 | PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) | |
| 321 | PAD1(LUT_PAD1) | INSTR1(LUT_WRITE)); |
| 322 | |
| 323 | lut_base = SEQID_RDEAR * 4; |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 324 | qspi_write32(priv->flags, ®s->lut[lut_base], OPRND0(QSPI_CMD_RDEAR) | |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 325 | PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) | |
| 326 | PAD1(LUT_PAD1) | INSTR1(LUT_READ)); |
| 327 | |
| 328 | lut_base = SEQID_WREAR * 4; |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 329 | qspi_write32(priv->flags, ®s->lut[lut_base], OPRND0(QSPI_CMD_WREAR) | |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 330 | PAD0(LUT_PAD1) | INSTR0(LUT_CMD) | OPRND1(1) | |
| 331 | PAD1(LUT_PAD1) | INSTR1(LUT_WRITE)); |
| 332 | #endif |
Yuan Yao | d719326 | 2016-03-15 14:36:42 +0800 | [diff] [blame] | 333 | |
| 334 | /* |
| 335 | * Read any device register. |
| 336 | * Used for Spansion S25FS-S family flash only. |
| 337 | */ |
| 338 | lut_base = SEQID_RDAR * 4; |
| 339 | qspi_write32(priv->flags, ®s->lut[lut_base], |
| 340 | OPRND0(QSPI_CMD_RDAR) | PAD0(LUT_PAD1) | |
| 341 | INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) | |
| 342 | PAD1(LUT_PAD1) | INSTR1(LUT_ADDR)); |
| 343 | qspi_write32(priv->flags, ®s->lut[lut_base + 1], |
| 344 | OPRND0(8) | PAD0(LUT_PAD1) | INSTR0(LUT_DUMMY) | |
| 345 | OPRND1(1) | PAD1(LUT_PAD1) | |
| 346 | INSTR1(LUT_READ)); |
| 347 | |
| 348 | /* |
| 349 | * Write any device register. |
| 350 | * Used for Spansion S25FS-S family flash only. |
| 351 | */ |
| 352 | lut_base = SEQID_WRAR * 4; |
| 353 | qspi_write32(priv->flags, ®s->lut[lut_base], |
| 354 | OPRND0(QSPI_CMD_WRAR) | PAD0(LUT_PAD1) | |
| 355 | INSTR0(LUT_CMD) | OPRND1(ADDR24BIT) | |
| 356 | PAD1(LUT_PAD1) | INSTR1(LUT_ADDR)); |
| 357 | qspi_write32(priv->flags, ®s->lut[lut_base + 1], |
| 358 | OPRND0(1) | PAD0(LUT_PAD1) | INSTR0(LUT_WRITE)); |
| 359 | |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 360 | /* Lock the LUT */ |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 361 | qspi_write32(priv->flags, ®s->lutkey, LUT_KEY_VALUE); |
| 362 | qspi_write32(priv->flags, ®s->lckcr, QSPI_LCKCR_LOCK); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 363 | } |
| 364 | |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 365 | #if defined(CONFIG_SYS_FSL_QSPI_AHB) |
| 366 | /* |
| 367 | * If we have changed the content of the flash by writing or erasing, |
| 368 | * we need to invalidate the AHB buffer. If we do not do so, we may read out |
| 369 | * the wrong data. The spec tells us reset the AHB domain and Serial Flash |
| 370 | * domain at the same time. |
| 371 | */ |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 372 | static inline void qspi_ahb_invalid(struct fsl_qspi_priv *priv) |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 373 | { |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 374 | struct fsl_qspi_regs *regs = priv->regs; |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 375 | u32 reg; |
| 376 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 377 | reg = qspi_read32(priv->flags, ®s->mcr); |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 378 | reg |= QSPI_MCR_SWRSTHD_MASK | QSPI_MCR_SWRSTSD_MASK; |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 379 | qspi_write32(priv->flags, ®s->mcr, reg); |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 380 | |
| 381 | /* |
| 382 | * The minimum delay : 1 AHB + 2 SFCK clocks. |
| 383 | * Delay 1 us is enough. |
| 384 | */ |
| 385 | udelay(1); |
| 386 | |
| 387 | reg &= ~(QSPI_MCR_SWRSTHD_MASK | QSPI_MCR_SWRSTSD_MASK); |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 388 | qspi_write32(priv->flags, ®s->mcr, reg); |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /* Read out the data from the AHB buffer. */ |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 392 | static inline void qspi_ahb_read(struct fsl_qspi_priv *priv, u8 *rxbuf, int len) |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 393 | { |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 394 | struct fsl_qspi_regs *regs = priv->regs; |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 395 | u32 mcr_reg; |
Heinrich Schuchardt | 950e75d | 2018-03-18 12:47:20 +0100 | [diff] [blame] | 396 | void *rx_addr; |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 397 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 398 | mcr_reg = qspi_read32(priv->flags, ®s->mcr); |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 399 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 400 | qspi_write32(priv->flags, ®s->mcr, |
| 401 | QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK | |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 402 | QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE); |
| 403 | |
Yunhui Cui | e4584cc | 2016-07-13 10:46:27 +0800 | [diff] [blame] | 404 | rx_addr = (void *)(uintptr_t)(priv->cur_amba_base + priv->sf_addr); |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 405 | /* Read out the data directly from the AHB buffer. */ |
Yunhui Cui | e4584cc | 2016-07-13 10:46:27 +0800 | [diff] [blame] | 406 | memcpy(rxbuf, rx_addr, len); |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 407 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 408 | qspi_write32(priv->flags, ®s->mcr, mcr_reg); |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 409 | } |
| 410 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 411 | static void qspi_enable_ddr_mode(struct fsl_qspi_priv *priv) |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 412 | { |
| 413 | u32 reg, reg2; |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 414 | struct fsl_qspi_regs *regs = priv->regs; |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 415 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 416 | reg = qspi_read32(priv->flags, ®s->mcr); |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 417 | /* Disable the module */ |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 418 | qspi_write32(priv->flags, ®s->mcr, reg | QSPI_MCR_MDIS_MASK); |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 419 | |
| 420 | /* Set the Sampling Register for DDR */ |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 421 | reg2 = qspi_read32(priv->flags, ®s->smpr); |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 422 | reg2 &= ~QSPI_SMPR_DDRSMP_MASK; |
| 423 | reg2 |= (2 << QSPI_SMPR_DDRSMP_SHIFT); |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 424 | qspi_write32(priv->flags, ®s->smpr, reg2); |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 425 | |
| 426 | /* Enable the module again (enable the DDR too) */ |
| 427 | reg |= QSPI_MCR_DDR_EN_MASK; |
| 428 | /* Enable bit 29 for imx6sx */ |
Jagan Teki | c97ca92 | 2015-10-23 01:37:18 +0530 | [diff] [blame] | 429 | reg |= BIT(29); |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 430 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 431 | qspi_write32(priv->flags, ®s->mcr, reg); |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | /* |
| 435 | * There are two different ways to read out the data from the flash: |
| 436 | * the "IP Command Read" and the "AHB Command Read". |
| 437 | * |
| 438 | * The IC guy suggests we use the "AHB Command Read" which is faster |
| 439 | * then the "IP Command Read". (What's more is that there is a bug in |
| 440 | * the "IP Command Read" in the Vybrid.) |
| 441 | * |
| 442 | * After we set up the registers for the "AHB Command Read", we can use |
| 443 | * the memcpy to read the data directly. A "missed" access to the buffer |
| 444 | * causes the controller to clear the buffer, and use the sequence pointed |
| 445 | * by the QUADSPI_BFGENCR[SEQID] to initiate a read from the flash. |
| 446 | */ |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 447 | static void qspi_init_ahb_read(struct fsl_qspi_priv *priv) |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 448 | { |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 449 | struct fsl_qspi_regs *regs = priv->regs; |
| 450 | |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 451 | /* AHB configuration for access buffer 0/1/2 .*/ |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 452 | qspi_write32(priv->flags, ®s->buf0cr, QSPI_BUFXCR_INVALID_MSTRID); |
| 453 | qspi_write32(priv->flags, ®s->buf1cr, QSPI_BUFXCR_INVALID_MSTRID); |
| 454 | qspi_write32(priv->flags, ®s->buf2cr, QSPI_BUFXCR_INVALID_MSTRID); |
| 455 | qspi_write32(priv->flags, ®s->buf3cr, QSPI_BUF3CR_ALLMST_MASK | |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 456 | (0x80 << QSPI_BUF3CR_ADATSZ_SHIFT)); |
| 457 | |
| 458 | /* We only use the buffer3 */ |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 459 | qspi_write32(priv->flags, ®s->buf0ind, 0); |
| 460 | qspi_write32(priv->flags, ®s->buf1ind, 0); |
| 461 | qspi_write32(priv->flags, ®s->buf2ind, 0); |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 462 | |
| 463 | /* |
| 464 | * Set the default lut sequence for AHB Read. |
| 465 | * Parallel mode is disabled. |
| 466 | */ |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 467 | qspi_write32(priv->flags, ®s->bfgencr, |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 468 | SEQID_FAST_READ << QSPI_BFGENCR_SEQID_SHIFT); |
| 469 | |
| 470 | /*Enable DDR Mode*/ |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 471 | qspi_enable_ddr_mode(priv); |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 472 | } |
| 473 | #endif |
| 474 | |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 475 | #ifdef CONFIG_SPI_FLASH_BAR |
| 476 | /* Bank register read/write, EAR register read/write */ |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 477 | static void qspi_op_rdbank(struct fsl_qspi_priv *priv, u8 *rxbuf, u32 len) |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 478 | { |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 479 | struct fsl_qspi_regs *regs = priv->regs; |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 480 | u32 reg, mcr_reg, data, seqid; |
| 481 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 482 | mcr_reg = qspi_read32(priv->flags, ®s->mcr); |
| 483 | qspi_write32(priv->flags, ®s->mcr, |
| 484 | QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK | |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 485 | QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE); |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 486 | qspi_write32(priv->flags, ®s->rbct, QSPI_RBCT_RXBRD_USEIPS); |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 487 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 488 | qspi_write32(priv->flags, ®s->sfar, priv->cur_amba_base); |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 489 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 490 | if (priv->cur_seqid == QSPI_CMD_BRRD) |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 491 | seqid = SEQID_BRRD; |
| 492 | else |
| 493 | seqid = SEQID_RDEAR; |
| 494 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 495 | qspi_write32(priv->flags, ®s->ipcr, |
| 496 | (seqid << QSPI_IPCR_SEQID_SHIFT) | len); |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 497 | |
| 498 | /* Wait previous command complete */ |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 499 | while (qspi_read32(priv->flags, ®s->sr) & QSPI_SR_BUSY_MASK) |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 500 | ; |
| 501 | |
| 502 | while (1) { |
Alexander Stein | 283eb4a | 2017-06-01 09:32:19 +0200 | [diff] [blame] | 503 | WATCHDOG_RESET(); |
| 504 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 505 | reg = qspi_read32(priv->flags, ®s->rbsr); |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 506 | if (reg & QSPI_RBSR_RDBFL_MASK) { |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 507 | data = qspi_read32(priv->flags, ®s->rbdr[0]); |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 508 | data = qspi_endian_xchg(data); |
| 509 | memcpy(rxbuf, &data, len); |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 510 | qspi_write32(priv->flags, ®s->mcr, |
| 511 | qspi_read32(priv->flags, ®s->mcr) | |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 512 | QSPI_MCR_CLR_RXF_MASK); |
| 513 | break; |
| 514 | } |
| 515 | } |
| 516 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 517 | qspi_write32(priv->flags, ®s->mcr, mcr_reg); |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 518 | } |
| 519 | #endif |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 520 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 521 | static void qspi_op_rdid(struct fsl_qspi_priv *priv, u32 *rxbuf, u32 len) |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 522 | { |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 523 | struct fsl_qspi_regs *regs = priv->regs; |
Gong Qianyu | a5357ca | 2016-01-26 15:06:40 +0800 | [diff] [blame] | 524 | u32 mcr_reg, rbsr_reg, data, size; |
| 525 | int i; |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 526 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 527 | mcr_reg = qspi_read32(priv->flags, ®s->mcr); |
| 528 | qspi_write32(priv->flags, ®s->mcr, |
| 529 | QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK | |
| 530 | QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE); |
| 531 | qspi_write32(priv->flags, ®s->rbct, QSPI_RBCT_RXBRD_USEIPS); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 532 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 533 | qspi_write32(priv->flags, ®s->sfar, priv->cur_amba_base); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 534 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 535 | qspi_write32(priv->flags, ®s->ipcr, |
| 536 | (SEQID_RDID << QSPI_IPCR_SEQID_SHIFT) | 0); |
| 537 | while (qspi_read32(priv->flags, ®s->sr) & QSPI_SR_BUSY_MASK) |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 538 | ; |
| 539 | |
| 540 | i = 0; |
Gong Qianyu | a5357ca | 2016-01-26 15:06:40 +0800 | [diff] [blame] | 541 | while ((RX_BUFFER_SIZE >= len) && (len > 0)) { |
Alexander Stein | 283eb4a | 2017-06-01 09:32:19 +0200 | [diff] [blame] | 542 | WATCHDOG_RESET(); |
| 543 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 544 | rbsr_reg = qspi_read32(priv->flags, ®s->rbsr); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 545 | if (rbsr_reg & QSPI_RBSR_RDBFL_MASK) { |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 546 | data = qspi_read32(priv->flags, ®s->rbdr[i]); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 547 | data = qspi_endian_xchg(data); |
Gong Qianyu | a5357ca | 2016-01-26 15:06:40 +0800 | [diff] [blame] | 548 | size = (len < 4) ? len : 4; |
| 549 | memcpy(rxbuf, &data, size); |
| 550 | len -= size; |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 551 | rxbuf++; |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 552 | i++; |
| 553 | } |
| 554 | } |
| 555 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 556 | qspi_write32(priv->flags, ®s->mcr, mcr_reg); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 557 | } |
| 558 | |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 559 | /* If not use AHB read, read data from ip interface */ |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 560 | static void qspi_op_read(struct fsl_qspi_priv *priv, u32 *rxbuf, u32 len) |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 561 | { |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 562 | struct fsl_qspi_regs *regs = priv->regs; |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 563 | u32 mcr_reg, data; |
| 564 | int i, size; |
| 565 | u32 to_or_from; |
Yuan Yao | d719326 | 2016-03-15 14:36:42 +0800 | [diff] [blame] | 566 | u32 seqid; |
| 567 | |
| 568 | if (priv->cur_seqid == QSPI_CMD_RDAR) |
| 569 | seqid = SEQID_RDAR; |
| 570 | else |
| 571 | seqid = SEQID_FAST_READ; |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 572 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 573 | mcr_reg = qspi_read32(priv->flags, ®s->mcr); |
| 574 | qspi_write32(priv->flags, ®s->mcr, |
| 575 | QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK | |
| 576 | QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE); |
| 577 | qspi_write32(priv->flags, ®s->rbct, QSPI_RBCT_RXBRD_USEIPS); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 578 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 579 | to_or_from = priv->sf_addr + priv->cur_amba_base; |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 580 | |
| 581 | while (len > 0) { |
Alexander Stein | 9b2c8a9 | 2015-11-04 09:19:10 +0100 | [diff] [blame] | 582 | WATCHDOG_RESET(); |
| 583 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 584 | qspi_write32(priv->flags, ®s->sfar, to_or_from); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 585 | |
| 586 | size = (len > RX_BUFFER_SIZE) ? |
| 587 | RX_BUFFER_SIZE : len; |
| 588 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 589 | qspi_write32(priv->flags, ®s->ipcr, |
Yuan Yao | d719326 | 2016-03-15 14:36:42 +0800 | [diff] [blame] | 590 | (seqid << QSPI_IPCR_SEQID_SHIFT) | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 591 | size); |
| 592 | while (qspi_read32(priv->flags, ®s->sr) & QSPI_SR_BUSY_MASK) |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 593 | ; |
| 594 | |
| 595 | to_or_from += size; |
| 596 | len -= size; |
| 597 | |
| 598 | i = 0; |
| 599 | while ((RX_BUFFER_SIZE >= size) && (size > 0)) { |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 600 | data = qspi_read32(priv->flags, ®s->rbdr[i]); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 601 | data = qspi_endian_xchg(data); |
Yuan Yao | d719326 | 2016-03-15 14:36:42 +0800 | [diff] [blame] | 602 | if (size < 4) |
| 603 | memcpy(rxbuf, &data, size); |
| 604 | else |
| 605 | memcpy(rxbuf, &data, 4); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 606 | rxbuf++; |
| 607 | size -= 4; |
| 608 | i++; |
| 609 | } |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 610 | qspi_write32(priv->flags, ®s->mcr, |
| 611 | qspi_read32(priv->flags, ®s->mcr) | |
| 612 | QSPI_MCR_CLR_RXF_MASK); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 613 | } |
| 614 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 615 | qspi_write32(priv->flags, ®s->mcr, mcr_reg); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 616 | } |
| 617 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 618 | static void qspi_op_write(struct fsl_qspi_priv *priv, u8 *txbuf, u32 len) |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 619 | { |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 620 | struct fsl_qspi_regs *regs = priv->regs; |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 621 | u32 mcr_reg, data, reg, status_reg, seqid; |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 622 | int i, size, tx_size; |
| 623 | u32 to_or_from = 0; |
| 624 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 625 | mcr_reg = qspi_read32(priv->flags, ®s->mcr); |
| 626 | qspi_write32(priv->flags, ®s->mcr, |
| 627 | QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK | |
| 628 | QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE); |
| 629 | qspi_write32(priv->flags, ®s->rbct, QSPI_RBCT_RXBRD_USEIPS); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 630 | |
| 631 | status_reg = 0; |
| 632 | while ((status_reg & FLASH_STATUS_WEL) != FLASH_STATUS_WEL) { |
Alexander Stein | 9b2c8a9 | 2015-11-04 09:19:10 +0100 | [diff] [blame] | 633 | WATCHDOG_RESET(); |
| 634 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 635 | qspi_write32(priv->flags, ®s->ipcr, |
| 636 | (SEQID_WREN << QSPI_IPCR_SEQID_SHIFT) | 0); |
| 637 | while (qspi_read32(priv->flags, ®s->sr) & QSPI_SR_BUSY_MASK) |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 638 | ; |
| 639 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 640 | qspi_write32(priv->flags, ®s->ipcr, |
| 641 | (SEQID_RDSR << QSPI_IPCR_SEQID_SHIFT) | 1); |
| 642 | while (qspi_read32(priv->flags, ®s->sr) & QSPI_SR_BUSY_MASK) |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 643 | ; |
| 644 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 645 | reg = qspi_read32(priv->flags, ®s->rbsr); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 646 | if (reg & QSPI_RBSR_RDBFL_MASK) { |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 647 | status_reg = qspi_read32(priv->flags, ®s->rbdr[0]); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 648 | status_reg = qspi_endian_xchg(status_reg); |
| 649 | } |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 650 | qspi_write32(priv->flags, ®s->mcr, |
| 651 | qspi_read32(priv->flags, ®s->mcr) | |
| 652 | QSPI_MCR_CLR_RXF_MASK); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 653 | } |
| 654 | |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 655 | /* Default is page programming */ |
| 656 | seqid = SEQID_PP; |
Yuan Yao | d719326 | 2016-03-15 14:36:42 +0800 | [diff] [blame] | 657 | if (priv->cur_seqid == QSPI_CMD_WRAR) |
| 658 | seqid = SEQID_WRAR; |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 659 | #ifdef CONFIG_SPI_FLASH_BAR |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 660 | if (priv->cur_seqid == QSPI_CMD_BRWR) |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 661 | seqid = SEQID_BRWR; |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 662 | else if (priv->cur_seqid == QSPI_CMD_WREAR) |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 663 | seqid = SEQID_WREAR; |
| 664 | #endif |
| 665 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 666 | to_or_from = priv->sf_addr + priv->cur_amba_base; |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 667 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 668 | qspi_write32(priv->flags, ®s->sfar, to_or_from); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 669 | |
| 670 | tx_size = (len > TX_BUFFER_SIZE) ? |
| 671 | TX_BUFFER_SIZE : len; |
| 672 | |
Suresh Gupta | 14ae0f9 | 2017-06-05 14:37:20 +0530 | [diff] [blame] | 673 | size = tx_size / 16; |
| 674 | /* |
| 675 | * There must be atleast 128bit data |
| 676 | * available in TX FIFO for any pop operation |
| 677 | */ |
| 678 | if (tx_size % 16) |
| 679 | size++; |
| 680 | for (i = 0; i < size * 4; i++) { |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 681 | memcpy(&data, txbuf, 4); |
| 682 | data = qspi_endian_xchg(data); |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 683 | qspi_write32(priv->flags, ®s->tbdr, data); |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 684 | txbuf += 4; |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 685 | } |
| 686 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 687 | qspi_write32(priv->flags, ®s->ipcr, |
| 688 | (seqid << QSPI_IPCR_SEQID_SHIFT) | tx_size); |
| 689 | while (qspi_read32(priv->flags, ®s->sr) & QSPI_SR_BUSY_MASK) |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 690 | ; |
| 691 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 692 | qspi_write32(priv->flags, ®s->mcr, mcr_reg); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 693 | } |
| 694 | |
Gong Qianyu | 4e8630b | 2016-01-26 15:06:41 +0800 | [diff] [blame] | 695 | static void qspi_op_rdsr(struct fsl_qspi_priv *priv, void *rxbuf, u32 len) |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 696 | { |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 697 | struct fsl_qspi_regs *regs = priv->regs; |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 698 | u32 mcr_reg, reg, data; |
| 699 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 700 | mcr_reg = qspi_read32(priv->flags, ®s->mcr); |
| 701 | qspi_write32(priv->flags, ®s->mcr, |
| 702 | QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK | |
| 703 | QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE); |
| 704 | qspi_write32(priv->flags, ®s->rbct, QSPI_RBCT_RXBRD_USEIPS); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 705 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 706 | qspi_write32(priv->flags, ®s->sfar, priv->cur_amba_base); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 707 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 708 | qspi_write32(priv->flags, ®s->ipcr, |
| 709 | (SEQID_RDSR << QSPI_IPCR_SEQID_SHIFT) | 0); |
| 710 | while (qspi_read32(priv->flags, ®s->sr) & QSPI_SR_BUSY_MASK) |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 711 | ; |
| 712 | |
| 713 | while (1) { |
Alexander Stein | 283eb4a | 2017-06-01 09:32:19 +0200 | [diff] [blame] | 714 | WATCHDOG_RESET(); |
| 715 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 716 | reg = qspi_read32(priv->flags, ®s->rbsr); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 717 | if (reg & QSPI_RBSR_RDBFL_MASK) { |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 718 | data = qspi_read32(priv->flags, ®s->rbdr[0]); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 719 | data = qspi_endian_xchg(data); |
Gong Qianyu | 4e8630b | 2016-01-26 15:06:41 +0800 | [diff] [blame] | 720 | memcpy(rxbuf, &data, len); |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 721 | qspi_write32(priv->flags, ®s->mcr, |
| 722 | qspi_read32(priv->flags, ®s->mcr) | |
| 723 | QSPI_MCR_CLR_RXF_MASK); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 724 | break; |
| 725 | } |
| 726 | } |
| 727 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 728 | qspi_write32(priv->flags, ®s->mcr, mcr_reg); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 729 | } |
| 730 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 731 | static void qspi_op_erase(struct fsl_qspi_priv *priv) |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 732 | { |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 733 | struct fsl_qspi_regs *regs = priv->regs; |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 734 | u32 mcr_reg; |
| 735 | u32 to_or_from = 0; |
| 736 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 737 | mcr_reg = qspi_read32(priv->flags, ®s->mcr); |
| 738 | qspi_write32(priv->flags, ®s->mcr, |
| 739 | QSPI_MCR_CLR_RXF_MASK | QSPI_MCR_CLR_TXF_MASK | |
| 740 | QSPI_MCR_RESERVED_MASK | QSPI_MCR_END_CFD_LE); |
| 741 | qspi_write32(priv->flags, ®s->rbct, QSPI_RBCT_RXBRD_USEIPS); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 742 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 743 | to_or_from = priv->sf_addr + priv->cur_amba_base; |
| 744 | qspi_write32(priv->flags, ®s->sfar, to_or_from); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 745 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 746 | qspi_write32(priv->flags, ®s->ipcr, |
| 747 | (SEQID_WREN << QSPI_IPCR_SEQID_SHIFT) | 0); |
| 748 | while (qspi_read32(priv->flags, ®s->sr) & QSPI_SR_BUSY_MASK) |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 749 | ; |
| 750 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 751 | if (priv->cur_seqid == QSPI_CMD_SE) { |
| 752 | qspi_write32(priv->flags, ®s->ipcr, |
Peng Fan | 3642a87 | 2014-12-31 11:01:39 +0800 | [diff] [blame] | 753 | (SEQID_SE << QSPI_IPCR_SEQID_SHIFT) | 0); |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 754 | } else if (priv->cur_seqid == QSPI_CMD_BE_4K) { |
| 755 | qspi_write32(priv->flags, ®s->ipcr, |
Peng Fan | 3642a87 | 2014-12-31 11:01:39 +0800 | [diff] [blame] | 756 | (SEQID_BE_4K << QSPI_IPCR_SEQID_SHIFT) | 0); |
| 757 | } |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 758 | while (qspi_read32(priv->flags, ®s->sr) & QSPI_SR_BUSY_MASK) |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 759 | ; |
| 760 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 761 | qspi_write32(priv->flags, ®s->mcr, mcr_reg); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 762 | } |
| 763 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 764 | int qspi_xfer(struct fsl_qspi_priv *priv, unsigned int bitlen, |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 765 | const void *dout, void *din, unsigned long flags) |
| 766 | { |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 767 | u32 bytes = DIV_ROUND_UP(bitlen, 8); |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 768 | static u32 wr_sfaddr; |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 769 | u32 txbuf; |
| 770 | |
Alexander Stein | 283eb4a | 2017-06-01 09:32:19 +0200 | [diff] [blame] | 771 | WATCHDOG_RESET(); |
| 772 | |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 773 | if (dout) { |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 774 | if (flags & SPI_XFER_BEGIN) { |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 775 | priv->cur_seqid = *(u8 *)dout; |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 776 | memcpy(&txbuf, dout, 4); |
| 777 | } |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 778 | |
| 779 | if (flags == SPI_XFER_END) { |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 780 | priv->sf_addr = wr_sfaddr; |
| 781 | qspi_op_write(priv, (u8 *)dout, bytes); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 782 | return 0; |
| 783 | } |
| 784 | |
Yuan Yao | d719326 | 2016-03-15 14:36:42 +0800 | [diff] [blame] | 785 | if (priv->cur_seqid == QSPI_CMD_FAST_READ || |
| 786 | priv->cur_seqid == QSPI_CMD_RDAR) { |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 787 | priv->sf_addr = swab32(txbuf) & OFFSET_BITS_MASK; |
| 788 | } else if ((priv->cur_seqid == QSPI_CMD_SE) || |
| 789 | (priv->cur_seqid == QSPI_CMD_BE_4K)) { |
| 790 | priv->sf_addr = swab32(txbuf) & OFFSET_BITS_MASK; |
| 791 | qspi_op_erase(priv); |
Yuan Yao | d719326 | 2016-03-15 14:36:42 +0800 | [diff] [blame] | 792 | } else if (priv->cur_seqid == QSPI_CMD_PP || |
| 793 | priv->cur_seqid == QSPI_CMD_WRAR) { |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 794 | wr_sfaddr = swab32(txbuf) & OFFSET_BITS_MASK; |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 795 | } else if ((priv->cur_seqid == QSPI_CMD_BRWR) || |
| 796 | (priv->cur_seqid == QSPI_CMD_WREAR)) { |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 797 | #ifdef CONFIG_SPI_FLASH_BAR |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 798 | wr_sfaddr = 0; |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 799 | #endif |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 800 | } |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | if (din) { |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 804 | if (priv->cur_seqid == QSPI_CMD_FAST_READ) { |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 805 | #ifdef CONFIG_SYS_FSL_QSPI_AHB |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 806 | qspi_ahb_read(priv, din, bytes); |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 807 | #else |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 808 | qspi_op_read(priv, din, bytes); |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 809 | #endif |
Yuan Yao | d719326 | 2016-03-15 14:36:42 +0800 | [diff] [blame] | 810 | } else if (priv->cur_seqid == QSPI_CMD_RDAR) { |
| 811 | qspi_op_read(priv, din, bytes); |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 812 | } else if (priv->cur_seqid == QSPI_CMD_RDID) |
| 813 | qspi_op_rdid(priv, din, bytes); |
| 814 | else if (priv->cur_seqid == QSPI_CMD_RDSR) |
Gong Qianyu | 4e8630b | 2016-01-26 15:06:41 +0800 | [diff] [blame] | 815 | qspi_op_rdsr(priv, din, bytes); |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 816 | #ifdef CONFIG_SPI_FLASH_BAR |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 817 | else if ((priv->cur_seqid == QSPI_CMD_BRRD) || |
| 818 | (priv->cur_seqid == QSPI_CMD_RDEAR)) { |
| 819 | priv->sf_addr = 0; |
| 820 | qspi_op_rdbank(priv, din, bytes); |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 821 | } |
| 822 | #endif |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 823 | } |
| 824 | |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 825 | #ifdef CONFIG_SYS_FSL_QSPI_AHB |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 826 | if ((priv->cur_seqid == QSPI_CMD_SE) || |
| 827 | (priv->cur_seqid == QSPI_CMD_PP) || |
| 828 | (priv->cur_seqid == QSPI_CMD_BE_4K) || |
| 829 | (priv->cur_seqid == QSPI_CMD_WREAR) || |
| 830 | (priv->cur_seqid == QSPI_CMD_BRWR)) |
| 831 | qspi_ahb_invalid(priv); |
| 832 | #endif |
| 833 | |
| 834 | return 0; |
| 835 | } |
| 836 | |
| 837 | void qspi_module_disable(struct fsl_qspi_priv *priv, u8 disable) |
| 838 | { |
| 839 | u32 mcr_val; |
| 840 | |
| 841 | mcr_val = qspi_read32(priv->flags, &priv->regs->mcr); |
| 842 | if (disable) |
| 843 | mcr_val |= QSPI_MCR_MDIS_MASK; |
| 844 | else |
| 845 | mcr_val &= ~QSPI_MCR_MDIS_MASK; |
| 846 | qspi_write32(priv->flags, &priv->regs->mcr, mcr_val); |
| 847 | } |
| 848 | |
| 849 | void qspi_cfg_smpr(struct fsl_qspi_priv *priv, u32 clear_bits, u32 set_bits) |
| 850 | { |
| 851 | u32 smpr_val; |
| 852 | |
| 853 | smpr_val = qspi_read32(priv->flags, &priv->regs->smpr); |
| 854 | smpr_val &= ~clear_bits; |
| 855 | smpr_val |= set_bits; |
| 856 | qspi_write32(priv->flags, &priv->regs->smpr, smpr_val); |
| 857 | } |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 858 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 859 | static int fsl_qspi_child_pre_probe(struct udevice *dev) |
| 860 | { |
Simon Glass | de44acf | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 861 | struct spi_slave *slave = dev_get_parent_priv(dev); |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 862 | |
| 863 | slave->max_write_size = TX_BUFFER_SIZE; |
| 864 | |
| 865 | return 0; |
| 866 | } |
| 867 | |
| 868 | static int fsl_qspi_probe(struct udevice *bus) |
| 869 | { |
York Sun | b4f57a9 | 2016-10-05 13:19:08 -0700 | [diff] [blame] | 870 | u32 mcr_val; |
Yuan Yao | b4bfe10 | 2016-03-15 14:36:41 +0800 | [diff] [blame] | 871 | u32 amba_size_per_chip; |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 872 | struct fsl_qspi_platdata *plat = dev_get_platdata(bus); |
| 873 | struct fsl_qspi_priv *priv = dev_get_priv(bus); |
| 874 | struct dm_spi_bus *dm_spi_bus; |
Suresh Gupta | 4945b87 | 2017-08-30 20:06:33 +0530 | [diff] [blame] | 875 | int i, ret; |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 876 | |
| 877 | dm_spi_bus = bus->uclass_priv; |
| 878 | |
| 879 | dm_spi_bus->max_hz = plat->speed_hz; |
| 880 | |
Gong Qianyu | 14b532e | 2016-01-26 15:06:39 +0800 | [diff] [blame] | 881 | priv->regs = (struct fsl_qspi_regs *)(uintptr_t)plat->reg_base; |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 882 | priv->flags = plat->flags; |
| 883 | |
| 884 | priv->speed_hz = plat->speed_hz; |
Yuan Yao | ae41239 | 2016-03-15 14:36:40 +0800 | [diff] [blame] | 885 | /* |
| 886 | * QSPI SFADR width is 32bits, the max dest addr is 4GB-1. |
| 887 | * AMBA memory zone should be located on the 0~4GB space |
| 888 | * even on a 64bits cpu. |
| 889 | */ |
| 890 | priv->amba_base[0] = (u32)plat->amba_base; |
| 891 | priv->amba_total_size = (u32)plat->amba_total_size; |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 892 | priv->flash_num = plat->flash_num; |
| 893 | priv->num_chipselect = plat->num_chipselect; |
| 894 | |
Suresh Gupta | 4945b87 | 2017-08-30 20:06:33 +0530 | [diff] [blame] | 895 | /* make sure controller is not busy anywhere */ |
Rajat Srivastava | 234daec | 2018-03-22 13:30:55 +0530 | [diff] [blame] | 896 | ret = is_controller_busy(priv); |
Suresh Gupta | 4945b87 | 2017-08-30 20:06:33 +0530 | [diff] [blame] | 897 | |
| 898 | if (ret) { |
| 899 | debug("ERROR : The controller is busy\n"); |
| 900 | return ret; |
| 901 | } |
| 902 | |
York Sun | b4f57a9 | 2016-10-05 13:19:08 -0700 | [diff] [blame] | 903 | mcr_val = qspi_read32(priv->flags, &priv->regs->mcr); |
Peng Fan | b3b1f5a | 2018-01-03 08:52:02 +0800 | [diff] [blame] | 904 | |
| 905 | /* Set endianness to LE for i.mx */ |
| 906 | if (IS_ENABLED(CONFIG_MX6) || IS_ENABLED(CONFIG_MX7)) |
| 907 | mcr_val = QSPI_MCR_END_CFD_LE; |
| 908 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 909 | qspi_write32(priv->flags, &priv->regs->mcr, |
York Sun | b4f57a9 | 2016-10-05 13:19:08 -0700 | [diff] [blame] | 910 | QSPI_MCR_RESERVED_MASK | QSPI_MCR_MDIS_MASK | |
| 911 | (mcr_val & QSPI_MCR_END_CFD_MASK)); |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 912 | |
| 913 | qspi_cfg_smpr(priv, ~(QSPI_SMPR_FSDLY_MASK | QSPI_SMPR_DDRSMP_MASK | |
| 914 | QSPI_SMPR_FSPHS_MASK | QSPI_SMPR_HSENA_MASK), 0); |
| 915 | |
Yuan Yao | b4bfe10 | 2016-03-15 14:36:41 +0800 | [diff] [blame] | 916 | /* |
| 917 | * Assign AMBA memory zone for every chipselect |
| 918 | * QuadSPI has two channels, every channel has two chipselects. |
| 919 | * If the property 'num-cs' in dts is 2, the AMBA memory will be divided |
| 920 | * into two parts and assign to every channel. This indicate that every |
| 921 | * channel only has one valid chipselect. |
| 922 | * If the property 'num-cs' in dts is 4, the AMBA memory will be divided |
| 923 | * into four parts and assign to every chipselect. |
| 924 | * Every channel will has two valid chipselects. |
| 925 | */ |
| 926 | amba_size_per_chip = priv->amba_total_size >> |
| 927 | (priv->num_chipselect >> 1); |
| 928 | for (i = 1 ; i < priv->num_chipselect ; i++) |
| 929 | priv->amba_base[i] = |
| 930 | amba_size_per_chip + priv->amba_base[i - 1]; |
| 931 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 932 | /* |
| 933 | * Any read access to non-implemented addresses will provide |
| 934 | * undefined results. |
| 935 | * |
| 936 | * In case single die flash devices, TOP_ADDR_MEMA2 and |
| 937 | * TOP_ADDR_MEMB2 should be initialized/programmed to |
| 938 | * TOP_ADDR_MEMA1 and TOP_ADDR_MEMB1 respectively - in effect, |
| 939 | * setting the size of these devices to 0. This would ensure |
| 940 | * that the complete memory map is assigned to only one flash device. |
| 941 | */ |
Suresh Gupta | 095f78e | 2017-02-21 14:26:46 +0530 | [diff] [blame] | 942 | qspi_write32(priv->flags, &priv->regs->sfa1ad, |
| 943 | priv->amba_base[0] + amba_size_per_chip); |
Yuan Yao | b4bfe10 | 2016-03-15 14:36:41 +0800 | [diff] [blame] | 944 | switch (priv->num_chipselect) { |
Suresh Gupta | 095f78e | 2017-02-21 14:26:46 +0530 | [diff] [blame] | 945 | case 1: |
| 946 | break; |
Yuan Yao | b4bfe10 | 2016-03-15 14:36:41 +0800 | [diff] [blame] | 947 | case 2: |
| 948 | qspi_write32(priv->flags, &priv->regs->sfa2ad, |
| 949 | priv->amba_base[1]); |
| 950 | qspi_write32(priv->flags, &priv->regs->sfb1ad, |
| 951 | priv->amba_base[1] + amba_size_per_chip); |
| 952 | qspi_write32(priv->flags, &priv->regs->sfb2ad, |
| 953 | priv->amba_base[1] + amba_size_per_chip); |
| 954 | break; |
| 955 | case 4: |
| 956 | qspi_write32(priv->flags, &priv->regs->sfa2ad, |
| 957 | priv->amba_base[2]); |
| 958 | qspi_write32(priv->flags, &priv->regs->sfb1ad, |
| 959 | priv->amba_base[3]); |
| 960 | qspi_write32(priv->flags, &priv->regs->sfb2ad, |
| 961 | priv->amba_base[3] + amba_size_per_chip); |
| 962 | break; |
| 963 | default: |
| 964 | debug("Error: Unsupported chipselect number %u!\n", |
| 965 | priv->num_chipselect); |
| 966 | qspi_module_disable(priv, 1); |
| 967 | return -EINVAL; |
| 968 | } |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 969 | |
| 970 | qspi_set_lut(priv); |
| 971 | |
| 972 | #ifdef CONFIG_SYS_FSL_QSPI_AHB |
| 973 | qspi_init_ahb_read(priv); |
| 974 | #endif |
| 975 | |
| 976 | qspi_module_disable(priv, 0); |
| 977 | |
| 978 | return 0; |
| 979 | } |
| 980 | |
| 981 | static int fsl_qspi_ofdata_to_platdata(struct udevice *bus) |
| 982 | { |
Yuan Yao | ae41239 | 2016-03-15 14:36:40 +0800 | [diff] [blame] | 983 | struct fdt_resource res_regs, res_mem; |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 984 | struct fsl_qspi_platdata *plat = bus->platdata; |
| 985 | const void *blob = gd->fdt_blob; |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 986 | int node = dev_of_offset(bus); |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 987 | int ret, flash_num = 0, subnode; |
| 988 | |
| 989 | if (fdtdec_get_bool(blob, node, "big-endian")) |
| 990 | plat->flags |= QSPI_FLAG_REGMAP_ENDIAN_BIG; |
| 991 | |
Yuan Yao | ae41239 | 2016-03-15 14:36:40 +0800 | [diff] [blame] | 992 | ret = fdt_get_named_resource(blob, node, "reg", "reg-names", |
| 993 | "QuadSPI", &res_regs); |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 994 | if (ret) { |
Yuan Yao | ae41239 | 2016-03-15 14:36:40 +0800 | [diff] [blame] | 995 | debug("Error: can't get regs base addresses(ret = %d)!\n", ret); |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 996 | return -ENOMEM; |
| 997 | } |
Yuan Yao | ae41239 | 2016-03-15 14:36:40 +0800 | [diff] [blame] | 998 | ret = fdt_get_named_resource(blob, node, "reg", "reg-names", |
| 999 | "QuadSPI-memory", &res_mem); |
| 1000 | if (ret) { |
| 1001 | debug("Error: can't get AMBA base addresses(ret = %d)!\n", ret); |
| 1002 | return -ENOMEM; |
| 1003 | } |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 1004 | |
| 1005 | /* Count flash numbers */ |
Simon Glass | 499c29e | 2016-10-02 17:59:29 -0600 | [diff] [blame] | 1006 | fdt_for_each_subnode(subnode, blob, node) |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 1007 | ++flash_num; |
| 1008 | |
| 1009 | if (flash_num == 0) { |
| 1010 | debug("Error: Missing flashes!\n"); |
| 1011 | return -ENODEV; |
| 1012 | } |
| 1013 | |
| 1014 | plat->speed_hz = fdtdec_get_int(blob, node, "spi-max-frequency", |
| 1015 | FSL_QSPI_DEFAULT_SCK_FREQ); |
| 1016 | plat->num_chipselect = fdtdec_get_int(blob, node, "num-cs", |
| 1017 | FSL_QSPI_MAX_CHIPSELECT_NUM); |
| 1018 | |
Yuan Yao | ae41239 | 2016-03-15 14:36:40 +0800 | [diff] [blame] | 1019 | plat->reg_base = res_regs.start; |
| 1020 | plat->amba_base = res_mem.start; |
| 1021 | plat->amba_total_size = res_mem.end - res_mem.start + 1; |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 1022 | plat->flash_num = flash_num; |
| 1023 | |
Yuan Yao | ae41239 | 2016-03-15 14:36:40 +0800 | [diff] [blame] | 1024 | debug("%s: regs=<0x%llx> <0x%llx, 0x%llx>, max-frequency=%d, endianess=%s\n", |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 1025 | __func__, |
Yuan Yao | ae41239 | 2016-03-15 14:36:40 +0800 | [diff] [blame] | 1026 | (u64)plat->reg_base, |
| 1027 | (u64)plat->amba_base, |
| 1028 | (u64)plat->amba_total_size, |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 1029 | plat->speed_hz, |
| 1030 | plat->flags & QSPI_FLAG_REGMAP_ENDIAN_BIG ? "be" : "le" |
| 1031 | ); |
| 1032 | |
| 1033 | return 0; |
| 1034 | } |
| 1035 | |
| 1036 | static int fsl_qspi_xfer(struct udevice *dev, unsigned int bitlen, |
| 1037 | const void *dout, void *din, unsigned long flags) |
| 1038 | { |
| 1039 | struct fsl_qspi_priv *priv; |
| 1040 | struct udevice *bus; |
| 1041 | |
| 1042 | bus = dev->parent; |
| 1043 | priv = dev_get_priv(bus); |
| 1044 | |
| 1045 | return qspi_xfer(priv, bitlen, dout, din, flags); |
| 1046 | } |
| 1047 | |
| 1048 | static int fsl_qspi_claim_bus(struct udevice *dev) |
| 1049 | { |
| 1050 | struct fsl_qspi_priv *priv; |
| 1051 | struct udevice *bus; |
| 1052 | struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev); |
Suresh Gupta | 4945b87 | 2017-08-30 20:06:33 +0530 | [diff] [blame] | 1053 | int ret; |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 1054 | |
| 1055 | bus = dev->parent; |
| 1056 | priv = dev_get_priv(bus); |
| 1057 | |
Suresh Gupta | 4945b87 | 2017-08-30 20:06:33 +0530 | [diff] [blame] | 1058 | /* make sure controller is not busy anywhere */ |
Rajat Srivastava | 234daec | 2018-03-22 13:30:55 +0530 | [diff] [blame] | 1059 | ret = is_controller_busy(priv); |
Suresh Gupta | 4945b87 | 2017-08-30 20:06:33 +0530 | [diff] [blame] | 1060 | |
| 1061 | if (ret) { |
| 1062 | debug("ERROR : The controller is busy\n"); |
| 1063 | return ret; |
| 1064 | } |
| 1065 | |
Yuan Yao | b4bfe10 | 2016-03-15 14:36:41 +0800 | [diff] [blame] | 1066 | priv->cur_amba_base = priv->amba_base[slave_plat->cs]; |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 1067 | |
| 1068 | qspi_module_disable(priv, 0); |
| 1069 | |
| 1070 | return 0; |
| 1071 | } |
| 1072 | |
| 1073 | static int fsl_qspi_release_bus(struct udevice *dev) |
| 1074 | { |
| 1075 | struct fsl_qspi_priv *priv; |
| 1076 | struct udevice *bus; |
| 1077 | |
| 1078 | bus = dev->parent; |
| 1079 | priv = dev_get_priv(bus); |
| 1080 | |
| 1081 | qspi_module_disable(priv, 1); |
| 1082 | |
| 1083 | return 0; |
| 1084 | } |
| 1085 | |
| 1086 | static int fsl_qspi_set_speed(struct udevice *bus, uint speed) |
| 1087 | { |
| 1088 | /* Nothing to do */ |
| 1089 | return 0; |
| 1090 | } |
| 1091 | |
| 1092 | static int fsl_qspi_set_mode(struct udevice *bus, uint mode) |
| 1093 | { |
| 1094 | /* Nothing to do */ |
| 1095 | return 0; |
| 1096 | } |
| 1097 | |
| 1098 | static const struct dm_spi_ops fsl_qspi_ops = { |
| 1099 | .claim_bus = fsl_qspi_claim_bus, |
| 1100 | .release_bus = fsl_qspi_release_bus, |
| 1101 | .xfer = fsl_qspi_xfer, |
| 1102 | .set_speed = fsl_qspi_set_speed, |
| 1103 | .set_mode = fsl_qspi_set_mode, |
| 1104 | }; |
| 1105 | |
| 1106 | static const struct udevice_id fsl_qspi_ids[] = { |
| 1107 | { .compatible = "fsl,vf610-qspi" }, |
| 1108 | { .compatible = "fsl,imx6sx-qspi" }, |
Peng Fan | b3b1f5a | 2018-01-03 08:52:02 +0800 | [diff] [blame] | 1109 | { .compatible = "fsl,imx6ul-qspi" }, |
| 1110 | { .compatible = "fsl,imx7d-qspi" }, |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 1111 | { } |
| 1112 | }; |
| 1113 | |
| 1114 | U_BOOT_DRIVER(fsl_qspi) = { |
| 1115 | .name = "fsl_qspi", |
| 1116 | .id = UCLASS_SPI, |
| 1117 | .of_match = fsl_qspi_ids, |
| 1118 | .ops = &fsl_qspi_ops, |
| 1119 | .ofdata_to_platdata = fsl_qspi_ofdata_to_platdata, |
| 1120 | .platdata_auto_alloc_size = sizeof(struct fsl_qspi_platdata), |
| 1121 | .priv_auto_alloc_size = sizeof(struct fsl_qspi_priv), |
| 1122 | .probe = fsl_qspi_probe, |
| 1123 | .child_pre_probe = fsl_qspi_child_pre_probe, |
| 1124 | }; |