Michael Walle | d3967f3 | 2019-12-18 00:09:58 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * NXP FlexSPI(FSPI) controller driver. |
| 4 | * |
| 5 | * Copyright (c) 2019 Michael Walle <michael@walle.cc> |
| 6 | * Copyright (c) 2019 NXP |
| 7 | * |
| 8 | * This driver was originally ported from the linux kernel v5.4-rc3, which had |
| 9 | * the following notes: |
| 10 | * |
| 11 | * FlexSPI is a flexsible SPI host controller which supports two SPI |
| 12 | * channels and up to 4 external devices. Each channel supports |
| 13 | * Single/Dual/Quad/Octal mode data transfer (1/2/4/8 bidirectional |
| 14 | * data lines). |
| 15 | * |
| 16 | * FlexSPI controller is driven by the LUT(Look-up Table) registers |
| 17 | * LUT registers are a look-up-table for sequences of instructions. |
| 18 | * A valid sequence consists of four LUT registers. |
| 19 | * Maximum 32 LUT sequences can be programmed simultaneously. |
| 20 | * |
| 21 | * LUTs are being created at run-time based on the commands passed |
| 22 | * from the spi-mem framework, thus using single LUT index. |
| 23 | * |
| 24 | * Software triggered Flash read/write access by IP Bus. |
| 25 | * |
| 26 | * Memory mapped read access by AHB Bus. |
| 27 | * |
| 28 | * Based on SPI MEM interface and spi-fsl-qspi.c driver. |
| 29 | * |
| 30 | * Author: |
| 31 | * Yogesh Narayan Gaur <yogeshnarayan.gaur@nxp.com> |
| 32 | * Boris Brezillon <bbrezillon@kernel.org> |
| 33 | * Frieder Schrempf <frieder.schrempf@kontron.de> |
| 34 | */ |
| 35 | |
Sean Anderson | acccaca | 2020-10-04 21:39:49 -0400 | [diff] [blame] | 36 | #include <clk.h> |
| 37 | #include <dm.h> |
| 38 | #include <dm/device_compat.h> |
Michael Walle | d3967f3 | 2019-12-18 00:09:58 +0100 | [diff] [blame] | 39 | #include <malloc.h> |
| 40 | #include <spi.h> |
| 41 | #include <spi-mem.h> |
Sean Anderson | acccaca | 2020-10-04 21:39:49 -0400 | [diff] [blame] | 42 | #include <asm/io.h> |
Kuldeep Singh | 19e38b2 | 2021-08-03 14:32:58 +0530 | [diff] [blame] | 43 | #ifdef CONFIG_FSL_LAYERSCAPE |
| 44 | #include <asm/arch/clock.h> |
| 45 | #include <asm/arch/soc.h> |
| 46 | #include <asm/arch/speed.h> |
| 47 | #endif |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 48 | #include <linux/bitops.h> |
Michael Walle | d3967f3 | 2019-12-18 00:09:58 +0100 | [diff] [blame] | 49 | #include <linux/kernel.h> |
| 50 | #include <linux/sizes.h> |
| 51 | #include <linux/iopoll.h> |
| 52 | #include <linux/bug.h> |
Simon Glass | fb6f482 | 2020-02-03 07:36:17 -0700 | [diff] [blame] | 53 | #include <linux/err.h> |
Michael Walle | d3967f3 | 2019-12-18 00:09:58 +0100 | [diff] [blame] | 54 | |
| 55 | /* |
| 56 | * The driver only uses one single LUT entry, that is updated on |
| 57 | * each call of exec_op(). Index 0 is preset at boot with a basic |
| 58 | * read operation, so let's use the last entry (31). |
| 59 | */ |
| 60 | #define SEQID_LUT 31 |
| 61 | |
| 62 | /* Registers used by the driver */ |
| 63 | #define FSPI_MCR0 0x00 |
| 64 | #define FSPI_MCR0_AHB_TIMEOUT(x) ((x) << 24) |
| 65 | #define FSPI_MCR0_IP_TIMEOUT(x) ((x) << 16) |
| 66 | #define FSPI_MCR0_LEARN_EN BIT(15) |
| 67 | #define FSPI_MCR0_SCRFRUN_EN BIT(14) |
| 68 | #define FSPI_MCR0_OCTCOMB_EN BIT(13) |
| 69 | #define FSPI_MCR0_DOZE_EN BIT(12) |
| 70 | #define FSPI_MCR0_HSEN BIT(11) |
| 71 | #define FSPI_MCR0_SERCLKDIV BIT(8) |
| 72 | #define FSPI_MCR0_ATDF_EN BIT(7) |
| 73 | #define FSPI_MCR0_ARDF_EN BIT(6) |
| 74 | #define FSPI_MCR0_RXCLKSRC(x) ((x) << 4) |
| 75 | #define FSPI_MCR0_END_CFG(x) ((x) << 2) |
| 76 | #define FSPI_MCR0_MDIS BIT(1) |
| 77 | #define FSPI_MCR0_SWRST BIT(0) |
| 78 | |
| 79 | #define FSPI_MCR1 0x04 |
| 80 | #define FSPI_MCR1_SEQ_TIMEOUT(x) ((x) << 16) |
| 81 | #define FSPI_MCR1_AHB_TIMEOUT(x) (x) |
| 82 | |
| 83 | #define FSPI_MCR2 0x08 |
| 84 | #define FSPI_MCR2_IDLE_WAIT(x) ((x) << 24) |
| 85 | #define FSPI_MCR2_SAMEDEVICEEN BIT(15) |
| 86 | #define FSPI_MCR2_CLRLRPHS BIT(14) |
| 87 | #define FSPI_MCR2_ABRDATSZ BIT(8) |
| 88 | #define FSPI_MCR2_ABRLEARN BIT(7) |
| 89 | #define FSPI_MCR2_ABR_READ BIT(6) |
| 90 | #define FSPI_MCR2_ABRWRITE BIT(5) |
| 91 | #define FSPI_MCR2_ABRDUMMY BIT(4) |
| 92 | #define FSPI_MCR2_ABR_MODE BIT(3) |
| 93 | #define FSPI_MCR2_ABRCADDR BIT(2) |
| 94 | #define FSPI_MCR2_ABRRADDR BIT(1) |
| 95 | #define FSPI_MCR2_ABR_CMD BIT(0) |
| 96 | |
| 97 | #define FSPI_AHBCR 0x0c |
| 98 | #define FSPI_AHBCR_RDADDROPT BIT(6) |
| 99 | #define FSPI_AHBCR_PREF_EN BIT(5) |
| 100 | #define FSPI_AHBCR_BUFF_EN BIT(4) |
| 101 | #define FSPI_AHBCR_CACH_EN BIT(3) |
| 102 | #define FSPI_AHBCR_CLRTXBUF BIT(2) |
| 103 | #define FSPI_AHBCR_CLRRXBUF BIT(1) |
| 104 | #define FSPI_AHBCR_PAR_EN BIT(0) |
| 105 | |
| 106 | #define FSPI_INTEN 0x10 |
| 107 | #define FSPI_INTEN_SCLKSBWR BIT(9) |
| 108 | #define FSPI_INTEN_SCLKSBRD BIT(8) |
| 109 | #define FSPI_INTEN_DATALRNFL BIT(7) |
| 110 | #define FSPI_INTEN_IPTXWE BIT(6) |
| 111 | #define FSPI_INTEN_IPRXWA BIT(5) |
| 112 | #define FSPI_INTEN_AHBCMDERR BIT(4) |
| 113 | #define FSPI_INTEN_IPCMDERR BIT(3) |
| 114 | #define FSPI_INTEN_AHBCMDGE BIT(2) |
| 115 | #define FSPI_INTEN_IPCMDGE BIT(1) |
| 116 | #define FSPI_INTEN_IPCMDDONE BIT(0) |
| 117 | |
| 118 | #define FSPI_INTR 0x14 |
| 119 | #define FSPI_INTR_SCLKSBWR BIT(9) |
| 120 | #define FSPI_INTR_SCLKSBRD BIT(8) |
| 121 | #define FSPI_INTR_DATALRNFL BIT(7) |
| 122 | #define FSPI_INTR_IPTXWE BIT(6) |
| 123 | #define FSPI_INTR_IPRXWA BIT(5) |
| 124 | #define FSPI_INTR_AHBCMDERR BIT(4) |
| 125 | #define FSPI_INTR_IPCMDERR BIT(3) |
| 126 | #define FSPI_INTR_AHBCMDGE BIT(2) |
| 127 | #define FSPI_INTR_IPCMDGE BIT(1) |
| 128 | #define FSPI_INTR_IPCMDDONE BIT(0) |
| 129 | |
| 130 | #define FSPI_LUTKEY 0x18 |
| 131 | #define FSPI_LUTKEY_VALUE 0x5AF05AF0 |
| 132 | |
| 133 | #define FSPI_LCKCR 0x1C |
| 134 | |
| 135 | #define FSPI_LCKER_LOCK 0x1 |
| 136 | #define FSPI_LCKER_UNLOCK 0x2 |
| 137 | |
| 138 | #define FSPI_BUFXCR_INVALID_MSTRID 0xE |
| 139 | #define FSPI_AHBRX_BUF0CR0 0x20 |
| 140 | #define FSPI_AHBRX_BUF1CR0 0x24 |
| 141 | #define FSPI_AHBRX_BUF2CR0 0x28 |
| 142 | #define FSPI_AHBRX_BUF3CR0 0x2C |
| 143 | #define FSPI_AHBRX_BUF4CR0 0x30 |
| 144 | #define FSPI_AHBRX_BUF5CR0 0x34 |
| 145 | #define FSPI_AHBRX_BUF6CR0 0x38 |
| 146 | #define FSPI_AHBRX_BUF7CR0 0x3C |
| 147 | #define FSPI_AHBRXBUF0CR7_PREF BIT(31) |
| 148 | |
| 149 | #define FSPI_AHBRX_BUF0CR1 0x40 |
| 150 | #define FSPI_AHBRX_BUF1CR1 0x44 |
| 151 | #define FSPI_AHBRX_BUF2CR1 0x48 |
| 152 | #define FSPI_AHBRX_BUF3CR1 0x4C |
| 153 | #define FSPI_AHBRX_BUF4CR1 0x50 |
| 154 | #define FSPI_AHBRX_BUF5CR1 0x54 |
| 155 | #define FSPI_AHBRX_BUF6CR1 0x58 |
| 156 | #define FSPI_AHBRX_BUF7CR1 0x5C |
| 157 | |
| 158 | #define FSPI_FLSHA1CR0 0x60 |
| 159 | #define FSPI_FLSHA2CR0 0x64 |
| 160 | #define FSPI_FLSHB1CR0 0x68 |
| 161 | #define FSPI_FLSHB2CR0 0x6C |
| 162 | #define FSPI_FLSHXCR0_SZ_KB 10 |
| 163 | #define FSPI_FLSHXCR0_SZ(x) ((x) >> FSPI_FLSHXCR0_SZ_KB) |
| 164 | |
| 165 | #define FSPI_FLSHA1CR1 0x70 |
| 166 | #define FSPI_FLSHA2CR1 0x74 |
| 167 | #define FSPI_FLSHB1CR1 0x78 |
| 168 | #define FSPI_FLSHB2CR1 0x7C |
| 169 | #define FSPI_FLSHXCR1_CSINTR(x) ((x) << 16) |
| 170 | #define FSPI_FLSHXCR1_CAS(x) ((x) << 11) |
| 171 | #define FSPI_FLSHXCR1_WA BIT(10) |
| 172 | #define FSPI_FLSHXCR1_TCSH(x) ((x) << 5) |
| 173 | #define FSPI_FLSHXCR1_TCSS(x) (x) |
| 174 | |
| 175 | #define FSPI_FLSHA1CR2 0x80 |
| 176 | #define FSPI_FLSHA2CR2 0x84 |
| 177 | #define FSPI_FLSHB1CR2 0x88 |
| 178 | #define FSPI_FLSHB2CR2 0x8C |
| 179 | #define FSPI_FLSHXCR2_CLRINSP BIT(24) |
| 180 | #define FSPI_FLSHXCR2_AWRWAIT BIT(16) |
| 181 | #define FSPI_FLSHXCR2_AWRSEQN_SHIFT 13 |
| 182 | #define FSPI_FLSHXCR2_AWRSEQI_SHIFT 8 |
| 183 | #define FSPI_FLSHXCR2_ARDSEQN_SHIFT 5 |
| 184 | #define FSPI_FLSHXCR2_ARDSEQI_SHIFT 0 |
| 185 | |
| 186 | #define FSPI_IPCR0 0xA0 |
| 187 | |
| 188 | #define FSPI_IPCR1 0xA4 |
| 189 | #define FSPI_IPCR1_IPAREN BIT(31) |
| 190 | #define FSPI_IPCR1_SEQNUM_SHIFT 24 |
| 191 | #define FSPI_IPCR1_SEQID_SHIFT 16 |
| 192 | #define FSPI_IPCR1_IDATSZ(x) (x) |
| 193 | |
| 194 | #define FSPI_IPCMD 0xB0 |
| 195 | #define FSPI_IPCMD_TRG BIT(0) |
| 196 | |
| 197 | #define FSPI_DLPR 0xB4 |
| 198 | |
| 199 | #define FSPI_IPRXFCR 0xB8 |
| 200 | #define FSPI_IPRXFCR_CLR BIT(0) |
| 201 | #define FSPI_IPRXFCR_DMA_EN BIT(1) |
| 202 | #define FSPI_IPRXFCR_WMRK(x) ((x) << 2) |
| 203 | |
| 204 | #define FSPI_IPTXFCR 0xBC |
| 205 | #define FSPI_IPTXFCR_CLR BIT(0) |
| 206 | #define FSPI_IPTXFCR_DMA_EN BIT(1) |
| 207 | #define FSPI_IPTXFCR_WMRK(x) ((x) << 2) |
| 208 | |
| 209 | #define FSPI_DLLACR 0xC0 |
| 210 | #define FSPI_DLLACR_OVRDEN BIT(8) |
| 211 | |
| 212 | #define FSPI_DLLBCR 0xC4 |
| 213 | #define FSPI_DLLBCR_OVRDEN BIT(8) |
| 214 | |
| 215 | #define FSPI_STS0 0xE0 |
| 216 | #define FSPI_STS0_DLPHB(x) ((x) << 8) |
| 217 | #define FSPI_STS0_DLPHA(x) ((x) << 4) |
| 218 | #define FSPI_STS0_CMD_SRC(x) ((x) << 2) |
| 219 | #define FSPI_STS0_ARB_IDLE BIT(1) |
| 220 | #define FSPI_STS0_SEQ_IDLE BIT(0) |
| 221 | |
| 222 | #define FSPI_STS1 0xE4 |
| 223 | #define FSPI_STS1_IP_ERRCD(x) ((x) << 24) |
| 224 | #define FSPI_STS1_IP_ERRID(x) ((x) << 16) |
| 225 | #define FSPI_STS1_AHB_ERRCD(x) ((x) << 8) |
| 226 | #define FSPI_STS1_AHB_ERRID(x) (x) |
| 227 | |
| 228 | #define FSPI_AHBSPNST 0xEC |
| 229 | #define FSPI_AHBSPNST_DATLFT(x) ((x) << 16) |
| 230 | #define FSPI_AHBSPNST_BUFID(x) ((x) << 1) |
| 231 | #define FSPI_AHBSPNST_ACTIVE BIT(0) |
| 232 | |
| 233 | #define FSPI_IPRXFSTS 0xF0 |
| 234 | #define FSPI_IPRXFSTS_RDCNTR(x) ((x) << 16) |
| 235 | #define FSPI_IPRXFSTS_FILL(x) (x) |
| 236 | |
| 237 | #define FSPI_IPTXFSTS 0xF4 |
| 238 | #define FSPI_IPTXFSTS_WRCNTR(x) ((x) << 16) |
| 239 | #define FSPI_IPTXFSTS_FILL(x) (x) |
| 240 | |
| 241 | #define FSPI_RFDR 0x100 |
| 242 | #define FSPI_TFDR 0x180 |
| 243 | |
| 244 | #define FSPI_LUT_BASE 0x200 |
| 245 | #define FSPI_LUT_OFFSET (SEQID_LUT * 4 * 4) |
| 246 | #define FSPI_LUT_REG(idx) \ |
| 247 | (FSPI_LUT_BASE + FSPI_LUT_OFFSET + (idx) * 4) |
| 248 | |
| 249 | /* register map end */ |
| 250 | |
| 251 | /* Instruction set for the LUT register. */ |
| 252 | #define LUT_STOP 0x00 |
| 253 | #define LUT_CMD 0x01 |
| 254 | #define LUT_ADDR 0x02 |
| 255 | #define LUT_CADDR_SDR 0x03 |
| 256 | #define LUT_MODE 0x04 |
| 257 | #define LUT_MODE2 0x05 |
| 258 | #define LUT_MODE4 0x06 |
| 259 | #define LUT_MODE8 0x07 |
| 260 | #define LUT_NXP_WRITE 0x08 |
| 261 | #define LUT_NXP_READ 0x09 |
| 262 | #define LUT_LEARN_SDR 0x0A |
| 263 | #define LUT_DATSZ_SDR 0x0B |
| 264 | #define LUT_DUMMY 0x0C |
| 265 | #define LUT_DUMMY_RWDS_SDR 0x0D |
| 266 | #define LUT_JMP_ON_CS 0x1F |
| 267 | #define LUT_CMD_DDR 0x21 |
| 268 | #define LUT_ADDR_DDR 0x22 |
| 269 | #define LUT_CADDR_DDR 0x23 |
| 270 | #define LUT_MODE_DDR 0x24 |
| 271 | #define LUT_MODE2_DDR 0x25 |
| 272 | #define LUT_MODE4_DDR 0x26 |
| 273 | #define LUT_MODE8_DDR 0x27 |
| 274 | #define LUT_WRITE_DDR 0x28 |
| 275 | #define LUT_READ_DDR 0x29 |
| 276 | #define LUT_LEARN_DDR 0x2A |
| 277 | #define LUT_DATSZ_DDR 0x2B |
| 278 | #define LUT_DUMMY_DDR 0x2C |
| 279 | #define LUT_DUMMY_RWDS_DDR 0x2D |
| 280 | |
| 281 | /* |
| 282 | * Calculate number of required PAD bits for LUT register. |
| 283 | * |
| 284 | * The pad stands for the number of IO lines [0:7]. |
| 285 | * For example, the octal read needs eight IO lines, |
| 286 | * so you should use LUT_PAD(8). This macro |
| 287 | * returns 3 i.e. use eight (2^3) IP lines for read. |
| 288 | */ |
| 289 | #define LUT_PAD(x) (fls(x) - 1) |
| 290 | |
| 291 | /* |
| 292 | * Macro for constructing the LUT entries with the following |
| 293 | * register layout: |
| 294 | * |
| 295 | * --------------------------------------------------- |
| 296 | * | INSTR1 | PAD1 | OPRND1 | INSTR0 | PAD0 | OPRND0 | |
| 297 | * --------------------------------------------------- |
| 298 | */ |
| 299 | #define PAD_SHIFT 8 |
| 300 | #define INSTR_SHIFT 10 |
| 301 | #define OPRND_SHIFT 16 |
| 302 | |
| 303 | /* Macros for constructing the LUT register. */ |
| 304 | #define LUT_DEF(idx, ins, pad, opr) \ |
| 305 | ((((ins) << INSTR_SHIFT) | ((pad) << PAD_SHIFT) | \ |
| 306 | (opr)) << (((idx) % 2) * OPRND_SHIFT)) |
| 307 | |
| 308 | #define POLL_TOUT 5000 |
| 309 | #define NXP_FSPI_MAX_CHIPSELECT 4 |
| 310 | |
Kuldeep Singh | 66a813e | 2021-08-03 14:32:57 +0530 | [diff] [blame] | 311 | /* Access flash memory using IP bus only */ |
| 312 | #define FSPI_QUIRK_USE_IP_ONLY BIT(0) |
| 313 | |
Michael Walle | d3967f3 | 2019-12-18 00:09:58 +0100 | [diff] [blame] | 314 | struct nxp_fspi_devtype_data { |
| 315 | unsigned int rxfifo; |
| 316 | unsigned int txfifo; |
| 317 | unsigned int ahb_buf_size; |
| 318 | unsigned int quirks; |
| 319 | bool little_endian; |
| 320 | }; |
| 321 | |
Kuldeep Singh | 19e38b2 | 2021-08-03 14:32:58 +0530 | [diff] [blame] | 322 | static struct nxp_fspi_devtype_data lx2160a_data = { |
Michael Walle | d3967f3 | 2019-12-18 00:09:58 +0100 | [diff] [blame] | 323 | .rxfifo = SZ_512, /* (64 * 64 bits) */ |
| 324 | .txfifo = SZ_1K, /* (128 * 64 bits) */ |
| 325 | .ahb_buf_size = SZ_2K, /* (256 * 64 bits) */ |
| 326 | .quirks = 0, |
| 327 | .little_endian = true, /* little-endian */ |
| 328 | }; |
| 329 | |
Kuldeep Singh | 19e38b2 | 2021-08-03 14:32:58 +0530 | [diff] [blame] | 330 | static struct nxp_fspi_devtype_data imx8mm_data = { |
Adam Ford | cf559bf | 2021-01-18 15:32:50 -0600 | [diff] [blame] | 331 | .rxfifo = SZ_512, /* (64 * 64 bits) */ |
| 332 | .txfifo = SZ_1K, /* (128 * 64 bits) */ |
| 333 | .ahb_buf_size = SZ_2K, /* (256 * 64 bits) */ |
| 334 | .quirks = 0, |
| 335 | .little_endian = true, /* little-endian */ |
| 336 | }; |
| 337 | |
Michael Walle | d3967f3 | 2019-12-18 00:09:58 +0100 | [diff] [blame] | 338 | struct nxp_fspi { |
| 339 | struct udevice *dev; |
| 340 | void __iomem *iobase; |
| 341 | void __iomem *ahb_addr; |
| 342 | u32 memmap_phy; |
| 343 | u32 memmap_phy_size; |
| 344 | struct clk clk, clk_en; |
Kuldeep Singh | 19e38b2 | 2021-08-03 14:32:58 +0530 | [diff] [blame] | 345 | struct nxp_fspi_devtype_data *devtype_data; |
Michael Walle | d3967f3 | 2019-12-18 00:09:58 +0100 | [diff] [blame] | 346 | }; |
| 347 | |
Kuldeep Singh | 66a813e | 2021-08-03 14:32:57 +0530 | [diff] [blame] | 348 | static inline int needs_ip_only(struct nxp_fspi *f) |
| 349 | { |
| 350 | return f->devtype_data->quirks & FSPI_QUIRK_USE_IP_ONLY; |
| 351 | } |
| 352 | |
Michael Walle | d3967f3 | 2019-12-18 00:09:58 +0100 | [diff] [blame] | 353 | /* |
| 354 | * R/W functions for big- or little-endian registers: |
| 355 | * The FSPI controller's endianness is independent of |
| 356 | * the CPU core's endianness. So far, although the CPU |
| 357 | * core is little-endian the FSPI controller can use |
| 358 | * big-endian or little-endian. |
| 359 | */ |
| 360 | static void fspi_writel(struct nxp_fspi *f, u32 val, void __iomem *addr) |
| 361 | { |
| 362 | if (f->devtype_data->little_endian) |
| 363 | out_le32(addr, val); |
| 364 | else |
| 365 | out_be32(addr, val); |
| 366 | } |
| 367 | |
| 368 | static u32 fspi_readl(struct nxp_fspi *f, void __iomem *addr) |
| 369 | { |
| 370 | if (f->devtype_data->little_endian) |
| 371 | return in_le32(addr); |
| 372 | else |
| 373 | return in_be32(addr); |
| 374 | } |
| 375 | |
| 376 | static int nxp_fspi_check_buswidth(struct nxp_fspi *f, u8 width) |
| 377 | { |
| 378 | switch (width) { |
| 379 | case 1: |
| 380 | case 2: |
| 381 | case 4: |
| 382 | case 8: |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | return -ENOTSUPP; |
| 387 | } |
| 388 | |
| 389 | static bool nxp_fspi_supports_op(struct spi_slave *slave, |
| 390 | const struct spi_mem_op *op) |
| 391 | { |
| 392 | struct nxp_fspi *f; |
| 393 | struct udevice *bus; |
| 394 | int ret; |
| 395 | |
| 396 | bus = slave->dev->parent; |
| 397 | f = dev_get_priv(bus); |
| 398 | |
| 399 | ret = nxp_fspi_check_buswidth(f, op->cmd.buswidth); |
| 400 | |
| 401 | if (op->addr.nbytes) |
| 402 | ret |= nxp_fspi_check_buswidth(f, op->addr.buswidth); |
| 403 | |
| 404 | if (op->dummy.nbytes) |
| 405 | ret |= nxp_fspi_check_buswidth(f, op->dummy.buswidth); |
| 406 | |
| 407 | if (op->data.nbytes) |
| 408 | ret |= nxp_fspi_check_buswidth(f, op->data.buswidth); |
| 409 | |
| 410 | if (ret) |
| 411 | return false; |
| 412 | |
| 413 | /* |
| 414 | * The number of address bytes should be equal to or less than 4 bytes. |
| 415 | */ |
| 416 | if (op->addr.nbytes > 4) |
| 417 | return false; |
| 418 | |
| 419 | /* |
| 420 | * If requested address value is greater than controller assigned |
| 421 | * memory mapped space, return error as it didn't fit in the range |
| 422 | * of assigned address space. |
| 423 | */ |
| 424 | if (op->addr.val >= f->memmap_phy_size) |
| 425 | return false; |
| 426 | |
| 427 | /* Max 64 dummy clock cycles supported */ |
| 428 | if (op->dummy.buswidth && |
| 429 | (op->dummy.nbytes * 8 / op->dummy.buswidth > 64)) |
| 430 | return false; |
| 431 | |
| 432 | /* Max data length, check controller limits and alignment */ |
| 433 | if (op->data.dir == SPI_MEM_DATA_IN && |
| 434 | (op->data.nbytes > f->devtype_data->ahb_buf_size || |
| 435 | (op->data.nbytes > f->devtype_data->rxfifo - 4 && |
| 436 | !IS_ALIGNED(op->data.nbytes, 8)))) |
| 437 | return false; |
| 438 | |
| 439 | if (op->data.dir == SPI_MEM_DATA_OUT && |
| 440 | op->data.nbytes > f->devtype_data->txfifo) |
| 441 | return false; |
| 442 | |
Michael Walle | d9d5733 | 2021-07-26 21:35:28 +0200 | [diff] [blame] | 443 | return spi_mem_default_supports_op(slave, op); |
Michael Walle | d3967f3 | 2019-12-18 00:09:58 +0100 | [diff] [blame] | 444 | } |
| 445 | |
Kuldeep Singh | cab5651 | 2020-04-27 12:38:51 +0530 | [diff] [blame] | 446 | /* Instead of busy looping invoke readl_poll_sleep_timeout functionality. */ |
Michael Walle | d3967f3 | 2019-12-18 00:09:58 +0100 | [diff] [blame] | 447 | static int fspi_readl_poll_tout(struct nxp_fspi *f, void __iomem *base, |
| 448 | u32 mask, u32 delay_us, |
| 449 | u32 timeout_us, bool c) |
| 450 | { |
| 451 | u32 reg; |
| 452 | |
| 453 | if (!f->devtype_data->little_endian) |
| 454 | mask = (u32)cpu_to_be32(mask); |
| 455 | |
| 456 | if (c) |
Kuldeep Singh | cab5651 | 2020-04-27 12:38:51 +0530 | [diff] [blame] | 457 | return readl_poll_sleep_timeout(base, reg, (reg & mask), |
| 458 | delay_us, timeout_us); |
Michael Walle | d3967f3 | 2019-12-18 00:09:58 +0100 | [diff] [blame] | 459 | else |
Kuldeep Singh | cab5651 | 2020-04-27 12:38:51 +0530 | [diff] [blame] | 460 | return readl_poll_sleep_timeout(base, reg, !(reg & mask), |
| 461 | delay_us, timeout_us); |
Michael Walle | d3967f3 | 2019-12-18 00:09:58 +0100 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | /* |
| 465 | * If the slave device content being changed by Write/Erase, need to |
| 466 | * invalidate the AHB buffer. This can be achieved by doing the reset |
| 467 | * of controller after setting MCR0[SWRESET] bit. |
| 468 | */ |
| 469 | static inline void nxp_fspi_invalid(struct nxp_fspi *f) |
| 470 | { |
| 471 | u32 reg; |
| 472 | int ret; |
| 473 | |
| 474 | reg = fspi_readl(f, f->iobase + FSPI_MCR0); |
| 475 | fspi_writel(f, reg | FSPI_MCR0_SWRST, f->iobase + FSPI_MCR0); |
| 476 | |
| 477 | /* w1c register, wait unit clear */ |
| 478 | ret = fspi_readl_poll_tout(f, f->iobase + FSPI_MCR0, |
| 479 | FSPI_MCR0_SWRST, 0, POLL_TOUT, false); |
| 480 | WARN_ON(ret); |
| 481 | } |
| 482 | |
| 483 | static void nxp_fspi_prepare_lut(struct nxp_fspi *f, |
| 484 | const struct spi_mem_op *op) |
| 485 | { |
| 486 | void __iomem *base = f->iobase; |
| 487 | u32 lutval[4] = {}; |
| 488 | int lutidx = 1, i; |
| 489 | |
| 490 | /* cmd */ |
| 491 | lutval[0] |= LUT_DEF(0, LUT_CMD, LUT_PAD(op->cmd.buswidth), |
| 492 | op->cmd.opcode); |
| 493 | |
| 494 | /* addr bytes */ |
| 495 | if (op->addr.nbytes) { |
| 496 | lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_ADDR, |
| 497 | LUT_PAD(op->addr.buswidth), |
| 498 | op->addr.nbytes * 8); |
| 499 | lutidx++; |
| 500 | } |
| 501 | |
| 502 | /* dummy bytes, if needed */ |
| 503 | if (op->dummy.nbytes) { |
| 504 | lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_DUMMY, |
| 505 | /* |
| 506 | * Due to FlexSPI controller limitation number of PAD for dummy |
| 507 | * buswidth needs to be programmed as equal to data buswidth. |
| 508 | */ |
| 509 | LUT_PAD(op->data.buswidth), |
| 510 | op->dummy.nbytes * 8 / |
| 511 | op->dummy.buswidth); |
| 512 | lutidx++; |
| 513 | } |
| 514 | |
| 515 | /* read/write data bytes */ |
| 516 | if (op->data.nbytes) { |
| 517 | lutval[lutidx / 2] |= LUT_DEF(lutidx, |
| 518 | op->data.dir == SPI_MEM_DATA_IN ? |
| 519 | LUT_NXP_READ : LUT_NXP_WRITE, |
| 520 | LUT_PAD(op->data.buswidth), |
| 521 | 0); |
| 522 | lutidx++; |
| 523 | } |
| 524 | |
| 525 | /* stop condition. */ |
| 526 | lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_STOP, 0, 0); |
| 527 | |
| 528 | /* unlock LUT */ |
| 529 | fspi_writel(f, FSPI_LUTKEY_VALUE, f->iobase + FSPI_LUTKEY); |
| 530 | fspi_writel(f, FSPI_LCKER_UNLOCK, f->iobase + FSPI_LCKCR); |
| 531 | |
| 532 | /* fill LUT */ |
| 533 | for (i = 0; i < ARRAY_SIZE(lutval); i++) |
| 534 | fspi_writel(f, lutval[i], base + FSPI_LUT_REG(i)); |
| 535 | |
Kuldeep Singh | 19e38b2 | 2021-08-03 14:32:58 +0530 | [diff] [blame] | 536 | dev_dbg(f->dev, "CMD[%x] lutval[0:%x \t 1:%x \t 2:%x \t 3:%x], size: 0x%08x\n", |
| 537 | op->cmd.opcode, lutval[0], lutval[1], lutval[2], lutval[3], op->data.nbytes); |
Michael Walle | d3967f3 | 2019-12-18 00:09:58 +0100 | [diff] [blame] | 538 | |
| 539 | /* lock LUT */ |
| 540 | fspi_writel(f, FSPI_LUTKEY_VALUE, f->iobase + FSPI_LUTKEY); |
| 541 | fspi_writel(f, FSPI_LCKER_LOCK, f->iobase + FSPI_LCKCR); |
| 542 | } |
| 543 | |
Alper Nebi Yasak | 5de2f33 | 2020-10-05 09:57:29 +0300 | [diff] [blame] | 544 | #if CONFIG_IS_ENABLED(CLK) |
Michael Walle | d3967f3 | 2019-12-18 00:09:58 +0100 | [diff] [blame] | 545 | static int nxp_fspi_clk_prep_enable(struct nxp_fspi *f) |
| 546 | { |
| 547 | int ret; |
| 548 | |
| 549 | ret = clk_enable(&f->clk_en); |
| 550 | if (ret) |
| 551 | return ret; |
| 552 | |
| 553 | ret = clk_enable(&f->clk); |
| 554 | if (ret) { |
| 555 | clk_disable(&f->clk_en); |
| 556 | return ret; |
| 557 | } |
| 558 | |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | static void nxp_fspi_clk_disable_unprep(struct nxp_fspi *f) |
| 563 | { |
| 564 | clk_disable(&f->clk); |
| 565 | clk_disable(&f->clk_en); |
| 566 | } |
| 567 | #endif |
| 568 | |
| 569 | /* |
| 570 | * In FlexSPI controller, flash access is based on value of FSPI_FLSHXXCR0 |
| 571 | * register and start base address of the slave device. |
| 572 | * |
| 573 | * (Higher address) |
| 574 | * -------- <-- FLSHB2CR0 |
| 575 | * | B2 | |
| 576 | * | | |
| 577 | * B2 start address --> -------- <-- FLSHB1CR0 |
| 578 | * | B1 | |
| 579 | * | | |
| 580 | * B1 start address --> -------- <-- FLSHA2CR0 |
| 581 | * | A2 | |
| 582 | * | | |
| 583 | * A2 start address --> -------- <-- FLSHA1CR0 |
| 584 | * | A1 | |
| 585 | * | | |
| 586 | * A1 start address --> -------- (Lower address) |
| 587 | * |
| 588 | * |
| 589 | * Start base address defines the starting address range for given CS and |
| 590 | * FSPI_FLSHXXCR0 defines the size of the slave device connected at given CS. |
| 591 | * |
| 592 | * But, different targets are having different combinations of number of CS, |
| 593 | * some targets only have single CS or two CS covering controller's full |
| 594 | * memory mapped space area. |
| 595 | * Thus, implementation is being done as independent of the size and number |
| 596 | * of the connected slave device. |
| 597 | * Assign controller memory mapped space size as the size to the connected |
| 598 | * slave device. |
| 599 | * Mark FLSHxxCR0 as zero initially and then assign value only to the selected |
| 600 | * chip-select Flash configuration register. |
| 601 | * |
| 602 | * For e.g. to access CS2 (B1), FLSHB1CR0 register would be equal to the |
| 603 | * memory mapped size of the controller. |
| 604 | * Value for rest of the CS FLSHxxCR0 register would be zero. |
| 605 | * |
| 606 | */ |
| 607 | static void nxp_fspi_select_mem(struct nxp_fspi *f, int chip_select) |
| 608 | { |
| 609 | u64 size_kb; |
| 610 | |
| 611 | /* Reset FLSHxxCR0 registers */ |
| 612 | fspi_writel(f, 0, f->iobase + FSPI_FLSHA1CR0); |
| 613 | fspi_writel(f, 0, f->iobase + FSPI_FLSHA2CR0); |
| 614 | fspi_writel(f, 0, f->iobase + FSPI_FLSHB1CR0); |
| 615 | fspi_writel(f, 0, f->iobase + FSPI_FLSHB2CR0); |
| 616 | |
| 617 | /* Assign controller memory mapped space as size, KBytes, of flash. */ |
| 618 | size_kb = FSPI_FLSHXCR0_SZ(f->memmap_phy_size); |
| 619 | |
| 620 | fspi_writel(f, size_kb, f->iobase + FSPI_FLSHA1CR0 + |
| 621 | 4 * chip_select); |
| 622 | |
| 623 | dev_dbg(f->dev, "Slave device [CS:%x] selected\n", chip_select); |
| 624 | } |
| 625 | |
| 626 | static void nxp_fspi_read_ahb(struct nxp_fspi *f, const struct spi_mem_op *op) |
| 627 | { |
| 628 | u32 len = op->data.nbytes; |
| 629 | |
| 630 | /* Read out the data directly from the AHB buffer. */ |
| 631 | memcpy_fromio(op->data.buf.in, (f->ahb_addr + op->addr.val), len); |
| 632 | } |
| 633 | |
| 634 | static void nxp_fspi_fill_txfifo(struct nxp_fspi *f, |
| 635 | const struct spi_mem_op *op) |
| 636 | { |
| 637 | void __iomem *base = f->iobase; |
| 638 | int i, ret; |
| 639 | u8 *buf = (u8 *)op->data.buf.out; |
| 640 | |
| 641 | /* clear the TX FIFO. */ |
| 642 | fspi_writel(f, FSPI_IPTXFCR_CLR, base + FSPI_IPTXFCR); |
| 643 | |
| 644 | /* |
| 645 | * Default value of water mark level is 8 bytes, hence in single |
| 646 | * write request controller can write max 8 bytes of data. |
| 647 | */ |
| 648 | |
| 649 | for (i = 0; i < ALIGN_DOWN(op->data.nbytes, 8); i += 8) { |
| 650 | /* Wait for TXFIFO empty */ |
| 651 | ret = fspi_readl_poll_tout(f, f->iobase + FSPI_INTR, |
| 652 | FSPI_INTR_IPTXWE, 0, |
| 653 | POLL_TOUT, true); |
| 654 | WARN_ON(ret); |
| 655 | |
| 656 | fspi_writel(f, *(u32 *)(buf + i), base + FSPI_TFDR); |
| 657 | fspi_writel(f, *(u32 *)(buf + i + 4), base + FSPI_TFDR + 4); |
| 658 | fspi_writel(f, FSPI_INTR_IPTXWE, base + FSPI_INTR); |
| 659 | } |
| 660 | |
| 661 | if (i < op->data.nbytes) { |
| 662 | u32 data = 0; |
| 663 | int j; |
| 664 | /* Wait for TXFIFO empty */ |
| 665 | ret = fspi_readl_poll_tout(f, f->iobase + FSPI_INTR, |
| 666 | FSPI_INTR_IPTXWE, 0, |
| 667 | POLL_TOUT, true); |
| 668 | WARN_ON(ret); |
| 669 | |
| 670 | for (j = 0; j < ALIGN(op->data.nbytes - i, 4); j += 4) { |
| 671 | memcpy(&data, buf + i + j, 4); |
| 672 | fspi_writel(f, data, base + FSPI_TFDR + j); |
| 673 | } |
| 674 | fspi_writel(f, FSPI_INTR_IPTXWE, base + FSPI_INTR); |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | static void nxp_fspi_read_rxfifo(struct nxp_fspi *f, |
| 679 | const struct spi_mem_op *op) |
| 680 | { |
| 681 | void __iomem *base = f->iobase; |
| 682 | int i, ret; |
| 683 | int len = op->data.nbytes; |
| 684 | u8 *buf = (u8 *)op->data.buf.in; |
| 685 | |
| 686 | /* |
| 687 | * Default value of water mark level is 8 bytes, hence in single |
| 688 | * read request controller can read max 8 bytes of data. |
| 689 | */ |
| 690 | for (i = 0; i < ALIGN_DOWN(len, 8); i += 8) { |
| 691 | /* Wait for RXFIFO available */ |
| 692 | ret = fspi_readl_poll_tout(f, f->iobase + FSPI_INTR, |
| 693 | FSPI_INTR_IPRXWA, 0, |
| 694 | POLL_TOUT, true); |
| 695 | WARN_ON(ret); |
| 696 | |
| 697 | *(u32 *)(buf + i) = fspi_readl(f, base + FSPI_RFDR); |
| 698 | *(u32 *)(buf + i + 4) = fspi_readl(f, base + FSPI_RFDR + 4); |
| 699 | /* move the FIFO pointer */ |
| 700 | fspi_writel(f, FSPI_INTR_IPRXWA, base + FSPI_INTR); |
| 701 | } |
| 702 | |
| 703 | if (i < len) { |
| 704 | u32 tmp; |
| 705 | int size, j; |
| 706 | |
| 707 | buf = op->data.buf.in + i; |
| 708 | /* Wait for RXFIFO available */ |
| 709 | ret = fspi_readl_poll_tout(f, f->iobase + FSPI_INTR, |
| 710 | FSPI_INTR_IPRXWA, 0, |
| 711 | POLL_TOUT, true); |
| 712 | WARN_ON(ret); |
| 713 | |
| 714 | len = op->data.nbytes - i; |
| 715 | for (j = 0; j < op->data.nbytes - i; j += 4) { |
| 716 | tmp = fspi_readl(f, base + FSPI_RFDR + j); |
| 717 | size = min(len, 4); |
| 718 | memcpy(buf + j, &tmp, size); |
| 719 | len -= size; |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | /* invalid the RXFIFO */ |
| 724 | fspi_writel(f, FSPI_IPRXFCR_CLR, base + FSPI_IPRXFCR); |
| 725 | /* move the FIFO pointer */ |
| 726 | fspi_writel(f, FSPI_INTR_IPRXWA, base + FSPI_INTR); |
| 727 | } |
| 728 | |
| 729 | static int nxp_fspi_do_op(struct nxp_fspi *f, const struct spi_mem_op *op) |
| 730 | { |
| 731 | void __iomem *base = f->iobase; |
| 732 | int seqnum = 0; |
| 733 | int err = 0; |
| 734 | u32 reg; |
| 735 | |
| 736 | reg = fspi_readl(f, base + FSPI_IPRXFCR); |
| 737 | /* invalid RXFIFO first */ |
| 738 | reg &= ~FSPI_IPRXFCR_DMA_EN; |
| 739 | reg = reg | FSPI_IPRXFCR_CLR; |
| 740 | fspi_writel(f, reg, base + FSPI_IPRXFCR); |
| 741 | |
| 742 | fspi_writel(f, op->addr.val, base + FSPI_IPCR0); |
| 743 | /* |
| 744 | * Always start the sequence at the same index since we update |
| 745 | * the LUT at each exec_op() call. And also specify the DATA |
| 746 | * length, since it's has not been specified in the LUT. |
| 747 | */ |
| 748 | fspi_writel(f, op->data.nbytes | |
| 749 | (SEQID_LUT << FSPI_IPCR1_SEQID_SHIFT) | |
| 750 | (seqnum << FSPI_IPCR1_SEQNUM_SHIFT), |
| 751 | base + FSPI_IPCR1); |
| 752 | |
| 753 | /* Trigger the LUT now. */ |
| 754 | fspi_writel(f, FSPI_IPCMD_TRG, base + FSPI_IPCMD); |
| 755 | |
| 756 | /* Wait for the completion. */ |
| 757 | err = fspi_readl_poll_tout(f, f->iobase + FSPI_STS0, |
| 758 | FSPI_STS0_ARB_IDLE, 1, 1000 * 1000, true); |
| 759 | |
| 760 | /* Invoke IP data read, if request is of data read. */ |
| 761 | if (!err && op->data.nbytes && op->data.dir == SPI_MEM_DATA_IN) |
| 762 | nxp_fspi_read_rxfifo(f, op); |
| 763 | |
| 764 | return err; |
| 765 | } |
| 766 | |
| 767 | static int nxp_fspi_exec_op(struct spi_slave *slave, |
| 768 | const struct spi_mem_op *op) |
| 769 | { |
| 770 | struct nxp_fspi *f; |
| 771 | struct udevice *bus; |
| 772 | int err = 0; |
| 773 | |
| 774 | bus = slave->dev->parent; |
| 775 | f = dev_get_priv(bus); |
| 776 | |
| 777 | /* Wait for controller being ready. */ |
| 778 | err = fspi_readl_poll_tout(f, f->iobase + FSPI_STS0, |
| 779 | FSPI_STS0_ARB_IDLE, 1, POLL_TOUT, true); |
| 780 | WARN_ON(err); |
| 781 | |
| 782 | nxp_fspi_prepare_lut(f, op); |
| 783 | /* |
Kuldeep Singh | 66a813e | 2021-08-03 14:32:57 +0530 | [diff] [blame] | 784 | * If we have large chunks of data, we read them through the AHB bus by |
| 785 | * accessing the mapped memory. In all other cases we use IP commands |
| 786 | * to access the flash. Read via AHB bus may be corrupted due to |
| 787 | * existence of an errata and therefore discard AHB read in such cases. |
Michael Walle | d3967f3 | 2019-12-18 00:09:58 +0100 | [diff] [blame] | 788 | */ |
| 789 | if (op->data.nbytes > (f->devtype_data->rxfifo - 4) && |
Kuldeep Singh | 66a813e | 2021-08-03 14:32:57 +0530 | [diff] [blame] | 790 | op->data.dir == SPI_MEM_DATA_IN && |
| 791 | !needs_ip_only(f)) { |
Michael Walle | d3967f3 | 2019-12-18 00:09:58 +0100 | [diff] [blame] | 792 | nxp_fspi_read_ahb(f, op); |
| 793 | } else { |
| 794 | if (op->data.nbytes && op->data.dir == SPI_MEM_DATA_OUT) |
| 795 | nxp_fspi_fill_txfifo(f, op); |
| 796 | |
| 797 | err = nxp_fspi_do_op(f, op); |
| 798 | } |
| 799 | |
| 800 | /* Invalidate the data in the AHB buffer. */ |
| 801 | nxp_fspi_invalid(f); |
| 802 | |
| 803 | return err; |
| 804 | } |
| 805 | |
| 806 | static int nxp_fspi_adjust_op_size(struct spi_slave *slave, |
| 807 | struct spi_mem_op *op) |
| 808 | { |
| 809 | struct nxp_fspi *f; |
| 810 | struct udevice *bus; |
| 811 | |
| 812 | bus = slave->dev->parent; |
| 813 | f = dev_get_priv(bus); |
| 814 | |
| 815 | if (op->data.dir == SPI_MEM_DATA_OUT) { |
| 816 | if (op->data.nbytes > f->devtype_data->txfifo) |
| 817 | op->data.nbytes = f->devtype_data->txfifo; |
| 818 | } else { |
| 819 | if (op->data.nbytes > f->devtype_data->ahb_buf_size) |
| 820 | op->data.nbytes = f->devtype_data->ahb_buf_size; |
| 821 | else if (op->data.nbytes > (f->devtype_data->rxfifo - 4)) |
| 822 | op->data.nbytes = ALIGN_DOWN(op->data.nbytes, 8); |
| 823 | } |
| 824 | |
Kuldeep Singh | 66a813e | 2021-08-03 14:32:57 +0530 | [diff] [blame] | 825 | /* Limit data bytes to RX FIFO in case of IP read only */ |
| 826 | if (needs_ip_only(f) && |
| 827 | op->data.dir == SPI_MEM_DATA_IN && |
| 828 | op->data.nbytes > f->devtype_data->rxfifo) |
| 829 | op->data.nbytes = f->devtype_data->rxfifo; |
| 830 | |
Michael Walle | d3967f3 | 2019-12-18 00:09:58 +0100 | [diff] [blame] | 831 | return 0; |
| 832 | } |
| 833 | |
Kuldeep Singh | 19e38b2 | 2021-08-03 14:32:58 +0530 | [diff] [blame] | 834 | #ifdef CONFIG_FSL_LAYERSCAPE |
| 835 | static void erratum_err050568(struct nxp_fspi *f) |
| 836 | { |
| 837 | struct sys_info sysinfo; |
| 838 | u32 svr = 0, freq = 0; |
| 839 | |
| 840 | /* Check for LS1028A variants */ |
| 841 | svr = SVR_SOC_VER(get_svr()); |
| 842 | if (svr != SVR_LS1017A || |
| 843 | svr != SVR_LS1018A || |
| 844 | svr != SVR_LS1027A || |
| 845 | svr != SVR_LS1028A) { |
| 846 | dev_dbg(f->dev, "Errata applicable only for LS1028A variants\n"); |
| 847 | return; |
| 848 | } |
| 849 | |
| 850 | /* Read PLL frequency */ |
| 851 | get_sys_info(&sysinfo); |
| 852 | freq = sysinfo.freq_systembus / 1000000; /* Convert to MHz */ |
| 853 | dev_dbg(f->dev, "svr: %08x, Frequency: %dMhz\n", svr, freq); |
| 854 | |
| 855 | /* Use IP bus only if PLL is 300MHz */ |
| 856 | if (freq == 300) |
| 857 | f->devtype_data->quirks |= FSPI_QUIRK_USE_IP_ONLY; |
| 858 | } |
| 859 | #endif |
| 860 | |
Michael Walle | d3967f3 | 2019-12-18 00:09:58 +0100 | [diff] [blame] | 861 | static int nxp_fspi_default_setup(struct nxp_fspi *f) |
| 862 | { |
| 863 | void __iomem *base = f->iobase; |
| 864 | int ret, i; |
| 865 | u32 reg; |
| 866 | |
Alper Nebi Yasak | 5de2f33 | 2020-10-05 09:57:29 +0300 | [diff] [blame] | 867 | #if CONFIG_IS_ENABLED(CLK) |
Michael Walle | d3967f3 | 2019-12-18 00:09:58 +0100 | [diff] [blame] | 868 | /* the default frequency, we will change it later if necessary. */ |
| 869 | ret = clk_set_rate(&f->clk, 20000000); |
Adam Ford | c9ad048 | 2021-01-18 15:32:49 -0600 | [diff] [blame] | 870 | if (ret < 0) |
Michael Walle | d3967f3 | 2019-12-18 00:09:58 +0100 | [diff] [blame] | 871 | return ret; |
| 872 | |
| 873 | ret = nxp_fspi_clk_prep_enable(f); |
| 874 | if (ret) |
| 875 | return ret; |
| 876 | #endif |
| 877 | |
Kuldeep Singh | 19e38b2 | 2021-08-03 14:32:58 +0530 | [diff] [blame] | 878 | #ifdef CONFIG_FSL_LAYERSCAPE |
| 879 | /* |
| 880 | * ERR050568: Flash access by FlexSPI AHB command may not work with |
| 881 | * platform frequency equal to 300 MHz on LS1028A. |
| 882 | * LS1028A reuses LX2160A compatible entry. Make errata applicable for |
| 883 | * Layerscape LS1028A platform family. |
| 884 | */ |
| 885 | if (device_is_compatible(f->dev, "nxp,lx2160a-fspi")) |
| 886 | erratum_err050568(f); |
| 887 | #endif |
| 888 | |
Michael Walle | d3967f3 | 2019-12-18 00:09:58 +0100 | [diff] [blame] | 889 | /* Reset the module */ |
| 890 | /* w1c register, wait unit clear */ |
| 891 | ret = fspi_readl_poll_tout(f, f->iobase + FSPI_MCR0, |
| 892 | FSPI_MCR0_SWRST, 0, POLL_TOUT, false); |
| 893 | WARN_ON(ret); |
| 894 | |
| 895 | /* Disable the module */ |
| 896 | fspi_writel(f, FSPI_MCR0_MDIS, base + FSPI_MCR0); |
| 897 | |
| 898 | /* Reset the DLL register to default value */ |
| 899 | fspi_writel(f, FSPI_DLLACR_OVRDEN, base + FSPI_DLLACR); |
| 900 | fspi_writel(f, FSPI_DLLBCR_OVRDEN, base + FSPI_DLLBCR); |
| 901 | |
| 902 | /* enable module */ |
| 903 | fspi_writel(f, FSPI_MCR0_AHB_TIMEOUT(0xFF) | FSPI_MCR0_IP_TIMEOUT(0xFF), |
| 904 | base + FSPI_MCR0); |
| 905 | |
| 906 | /* |
| 907 | * Disable same device enable bit and configure all slave devices |
| 908 | * independently. |
| 909 | */ |
| 910 | reg = fspi_readl(f, f->iobase + FSPI_MCR2); |
| 911 | reg = reg & ~(FSPI_MCR2_SAMEDEVICEEN); |
| 912 | fspi_writel(f, reg, base + FSPI_MCR2); |
| 913 | |
| 914 | /* AHB configuration for access buffer 0~7. */ |
| 915 | for (i = 0; i < 7; i++) |
| 916 | fspi_writel(f, 0, base + FSPI_AHBRX_BUF0CR0 + 4 * i); |
| 917 | |
| 918 | /* |
| 919 | * Set ADATSZ with the maximum AHB buffer size to improve the read |
| 920 | * performance. |
| 921 | */ |
| 922 | fspi_writel(f, (f->devtype_data->ahb_buf_size / 8 | |
| 923 | FSPI_AHBRXBUF0CR7_PREF), base + FSPI_AHBRX_BUF7CR0); |
| 924 | |
| 925 | /* prefetch and no start address alignment limitation */ |
| 926 | fspi_writel(f, FSPI_AHBCR_PREF_EN | FSPI_AHBCR_RDADDROPT, |
| 927 | base + FSPI_AHBCR); |
| 928 | |
Han Xu | d274488 | 2023-09-13 16:15:35 -0500 | [diff] [blame] | 929 | /* Reset the flashx control1 registers */ |
| 930 | reg = FSPI_FLSHXCR1_TCSH(0x3) | FSPI_FLSHXCR1_TCSS(0x3); |
| 931 | fspi_writel(f, reg, base + FSPI_FLSHA1CR1); |
| 932 | fspi_writel(f, reg, base + FSPI_FLSHA2CR1); |
| 933 | fspi_writel(f, reg, base + FSPI_FLSHB1CR1); |
| 934 | fspi_writel(f, reg, base + FSPI_FLSHB2CR1); |
| 935 | |
Michael Walle | d3967f3 | 2019-12-18 00:09:58 +0100 | [diff] [blame] | 936 | /* AHB Read - Set lut sequence ID for all CS. */ |
| 937 | fspi_writel(f, SEQID_LUT, base + FSPI_FLSHA1CR2); |
| 938 | fspi_writel(f, SEQID_LUT, base + FSPI_FLSHA2CR2); |
| 939 | fspi_writel(f, SEQID_LUT, base + FSPI_FLSHB1CR2); |
| 940 | fspi_writel(f, SEQID_LUT, base + FSPI_FLSHB2CR2); |
| 941 | |
| 942 | return 0; |
| 943 | } |
| 944 | |
| 945 | static int nxp_fspi_probe(struct udevice *bus) |
| 946 | { |
| 947 | struct nxp_fspi *f = dev_get_priv(bus); |
| 948 | |
| 949 | f->devtype_data = |
| 950 | (struct nxp_fspi_devtype_data *)dev_get_driver_data(bus); |
| 951 | nxp_fspi_default_setup(f); |
| 952 | |
| 953 | return 0; |
| 954 | } |
| 955 | |
| 956 | static int nxp_fspi_claim_bus(struct udevice *dev) |
| 957 | { |
| 958 | struct nxp_fspi *f; |
| 959 | struct udevice *bus; |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 960 | struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev); |
Michael Walle | d3967f3 | 2019-12-18 00:09:58 +0100 | [diff] [blame] | 961 | |
| 962 | bus = dev->parent; |
| 963 | f = dev_get_priv(bus); |
| 964 | |
Venkatesh Yadav Abbarapu | 91b9e37 | 2024-09-26 10:25:05 +0530 | [diff] [blame] | 965 | nxp_fspi_select_mem(f, slave_plat->cs[0]); |
Michael Walle | d3967f3 | 2019-12-18 00:09:58 +0100 | [diff] [blame] | 966 | |
| 967 | return 0; |
| 968 | } |
| 969 | |
| 970 | static int nxp_fspi_set_speed(struct udevice *bus, uint speed) |
| 971 | { |
Alper Nebi Yasak | 5de2f33 | 2020-10-05 09:57:29 +0300 | [diff] [blame] | 972 | #if CONFIG_IS_ENABLED(CLK) |
Michael Walle | d3967f3 | 2019-12-18 00:09:58 +0100 | [diff] [blame] | 973 | struct nxp_fspi *f = dev_get_priv(bus); |
| 974 | int ret; |
| 975 | |
| 976 | nxp_fspi_clk_disable_unprep(f); |
| 977 | |
| 978 | ret = clk_set_rate(&f->clk, speed); |
Adam Ford | c9ad048 | 2021-01-18 15:32:49 -0600 | [diff] [blame] | 979 | if (ret < 0) |
Michael Walle | d3967f3 | 2019-12-18 00:09:58 +0100 | [diff] [blame] | 980 | return ret; |
| 981 | |
| 982 | ret = nxp_fspi_clk_prep_enable(f); |
| 983 | if (ret) |
| 984 | return ret; |
| 985 | #endif |
| 986 | return 0; |
| 987 | } |
| 988 | |
| 989 | static int nxp_fspi_set_mode(struct udevice *bus, uint mode) |
| 990 | { |
| 991 | /* Nothing to do */ |
| 992 | return 0; |
| 993 | } |
| 994 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 995 | static int nxp_fspi_of_to_plat(struct udevice *bus) |
Michael Walle | d3967f3 | 2019-12-18 00:09:58 +0100 | [diff] [blame] | 996 | { |
| 997 | struct nxp_fspi *f = dev_get_priv(bus); |
Alper Nebi Yasak | 5de2f33 | 2020-10-05 09:57:29 +0300 | [diff] [blame] | 998 | #if CONFIG_IS_ENABLED(CLK) |
Michael Walle | d3967f3 | 2019-12-18 00:09:58 +0100 | [diff] [blame] | 999 | int ret; |
| 1000 | #endif |
| 1001 | |
| 1002 | fdt_addr_t iobase; |
| 1003 | fdt_addr_t iobase_size; |
| 1004 | fdt_addr_t ahb_addr; |
| 1005 | fdt_addr_t ahb_size; |
| 1006 | |
| 1007 | f->dev = bus; |
| 1008 | |
| 1009 | iobase = devfdt_get_addr_size_name(bus, "fspi_base", &iobase_size); |
| 1010 | if (iobase == FDT_ADDR_T_NONE) { |
| 1011 | dev_err(bus, "fspi_base regs missing\n"); |
| 1012 | return -ENODEV; |
| 1013 | } |
| 1014 | f->iobase = map_physmem(iobase, iobase_size, MAP_NOCACHE); |
| 1015 | |
| 1016 | ahb_addr = devfdt_get_addr_size_name(bus, "fspi_mmap", &ahb_size); |
| 1017 | if (ahb_addr == FDT_ADDR_T_NONE) { |
| 1018 | dev_err(bus, "fspi_mmap regs missing\n"); |
| 1019 | return -ENODEV; |
| 1020 | } |
| 1021 | f->ahb_addr = map_physmem(ahb_addr, ahb_size, MAP_NOCACHE); |
| 1022 | f->memmap_phy_size = ahb_size; |
| 1023 | |
Alper Nebi Yasak | 5de2f33 | 2020-10-05 09:57:29 +0300 | [diff] [blame] | 1024 | #if CONFIG_IS_ENABLED(CLK) |
Michael Walle | d3967f3 | 2019-12-18 00:09:58 +0100 | [diff] [blame] | 1025 | ret = clk_get_by_name(bus, "fspi_en", &f->clk_en); |
| 1026 | if (ret) { |
| 1027 | dev_err(bus, "failed to get fspi_en clock\n"); |
| 1028 | return ret; |
| 1029 | } |
| 1030 | |
| 1031 | ret = clk_get_by_name(bus, "fspi", &f->clk); |
| 1032 | if (ret) { |
| 1033 | dev_err(bus, "failed to get fspi clock\n"); |
| 1034 | return ret; |
| 1035 | } |
| 1036 | #endif |
| 1037 | |
| 1038 | dev_dbg(bus, "iobase=<0x%llx>, ahb_addr=<0x%llx>\n", iobase, ahb_addr); |
| 1039 | |
| 1040 | return 0; |
| 1041 | } |
| 1042 | |
| 1043 | static const struct spi_controller_mem_ops nxp_fspi_mem_ops = { |
| 1044 | .adjust_op_size = nxp_fspi_adjust_op_size, |
| 1045 | .supports_op = nxp_fspi_supports_op, |
| 1046 | .exec_op = nxp_fspi_exec_op, |
| 1047 | }; |
| 1048 | |
| 1049 | static const struct dm_spi_ops nxp_fspi_ops = { |
| 1050 | .claim_bus = nxp_fspi_claim_bus, |
| 1051 | .set_speed = nxp_fspi_set_speed, |
| 1052 | .set_mode = nxp_fspi_set_mode, |
| 1053 | .mem_ops = &nxp_fspi_mem_ops, |
| 1054 | }; |
| 1055 | |
| 1056 | static const struct udevice_id nxp_fspi_ids[] = { |
| 1057 | { .compatible = "nxp,lx2160a-fspi", .data = (ulong)&lx2160a_data, }, |
Adam Ford | cf559bf | 2021-01-18 15:32:50 -0600 | [diff] [blame] | 1058 | { .compatible = "nxp,imx8mm-fspi", .data = (ulong)&imx8mm_data, }, |
Marek Vasut | 2e4b63b | 2022-03-09 04:18:57 +0100 | [diff] [blame] | 1059 | { .compatible = "nxp,imx8mp-fspi", .data = (ulong)&imx8mm_data, }, |
Michael Walle | d3967f3 | 2019-12-18 00:09:58 +0100 | [diff] [blame] | 1060 | { } |
| 1061 | }; |
| 1062 | |
| 1063 | U_BOOT_DRIVER(nxp_fspi) = { |
| 1064 | .name = "nxp_fspi", |
| 1065 | .id = UCLASS_SPI, |
| 1066 | .of_match = nxp_fspi_ids, |
| 1067 | .ops = &nxp_fspi_ops, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 1068 | .of_to_plat = nxp_fspi_of_to_plat, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 1069 | .priv_auto = sizeof(struct nxp_fspi), |
Michael Walle | d3967f3 | 2019-12-18 00:09:58 +0100 | [diff] [blame] | 1070 | .probe = nxp_fspi_probe, |
| 1071 | }; |