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