Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 2 | |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 3 | /* |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 4 | * Freescale QuadSPI driver. |
| 5 | * |
| 6 | * Copyright (C) 2013 Freescale Semiconductor, Inc. |
| 7 | * Copyright (C) 2018 Bootlin |
| 8 | * Copyright (C) 2018 exceet electronics GmbH |
| 9 | * Copyright (C) 2018 Kontron Electronics GmbH |
| 10 | * Copyright 2019-2020 NXP |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 11 | * |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 12 | * This driver is a ported version of Linux Freescale QSPI driver taken from |
| 13 | * v5.5-rc1 tag having following information. |
| 14 | * |
| 15 | * Transition to SPI MEM interface: |
| 16 | * Authors: |
| 17 | * Boris Brezillon <bbrezillon@kernel.org> |
| 18 | * Frieder Schrempf <frieder.schrempf@kontron.de> |
| 19 | * Yogesh Gaur <yogeshnarayan.gaur@nxp.com> |
| 20 | * Suresh Gupta <suresh.gupta@nxp.com> |
| 21 | * |
| 22 | * Based on the original fsl-quadspi.c spi-nor driver. |
| 23 | * Transition to spi-mem in spi-fsl-qspi.c |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 24 | */ |
| 25 | |
Sean Anderson | a89b943 | 2020-10-04 21:39:50 -0400 | [diff] [blame] | 26 | #include <dm.h> |
| 27 | #include <dm/device_compat.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 28 | #include <log.h> |
Sean Anderson | a89b943 | 2020-10-04 21:39:50 -0400 | [diff] [blame] | 29 | #include <spi.h> |
| 30 | #include <spi-mem.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 31 | #include <asm/global_data.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 32 | #include <linux/bitops.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 33 | #include <linux/delay.h> |
Simon Glass | 2dc9c34 | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 34 | #include <linux/libfdt.h> |
| 35 | #include <linux/sizes.h> |
| 36 | #include <linux/iopoll.h> |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 37 | #include <linux/iopoll.h> |
| 38 | #include <linux/sizes.h> |
| 39 | #include <linux/err.h> |
Sean Anderson | a89b943 | 2020-10-04 21:39:50 -0400 | [diff] [blame] | 40 | #include <asm/io.h> |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 41 | |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 42 | DECLARE_GLOBAL_DATA_PTR; |
| 43 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 44 | /* |
| 45 | * The driver only uses one single LUT entry, that is updated on |
| 46 | * each call of exec_op(). Index 0 is preset at boot with a basic |
| 47 | * read operation, so let's use the last entry (15). |
| 48 | */ |
| 49 | #define SEQID_LUT 15 |
Ye Li | d7e3c9a | 2020-06-09 00:59:06 -0700 | [diff] [blame] | 50 | #define SEQID_LUT_AHB 14 |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 51 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 52 | /* Registers used by the driver */ |
| 53 | #define QUADSPI_MCR 0x00 |
| 54 | #define QUADSPI_MCR_RESERVED_MASK GENMASK(19, 16) |
| 55 | #define QUADSPI_MCR_MDIS_MASK BIT(14) |
| 56 | #define QUADSPI_MCR_CLR_TXF_MASK BIT(11) |
| 57 | #define QUADSPI_MCR_CLR_RXF_MASK BIT(10) |
| 58 | #define QUADSPI_MCR_DDR_EN_MASK BIT(7) |
| 59 | #define QUADSPI_MCR_END_CFG_MASK GENMASK(3, 2) |
| 60 | #define QUADSPI_MCR_SWRSTHD_MASK BIT(1) |
| 61 | #define QUADSPI_MCR_SWRSTSD_MASK BIT(0) |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 62 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 63 | #define QUADSPI_IPCR 0x08 |
| 64 | #define QUADSPI_IPCR_SEQID(x) ((x) << 24) |
| 65 | #define QUADSPI_FLSHCR 0x0c |
| 66 | #define QUADSPI_FLSHCR_TCSS_MASK GENMASK(3, 0) |
| 67 | #define QUADSPI_FLSHCR_TCSH_MASK GENMASK(11, 8) |
| 68 | #define QUADSPI_FLSHCR_TDH_MASK GENMASK(17, 16) |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 69 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 70 | #define QUADSPI_BUF3CR 0x1c |
| 71 | #define QUADSPI_BUF3CR_ALLMST_MASK BIT(31) |
| 72 | #define QUADSPI_BUF3CR_ADATSZ(x) ((x) << 8) |
| 73 | #define QUADSPI_BUF3CR_ADATSZ_MASK GENMASK(15, 8) |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 74 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 75 | #define QUADSPI_BFGENCR 0x20 |
| 76 | #define QUADSPI_BFGENCR_SEQID(x) ((x) << 12) |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 77 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 78 | #define QUADSPI_BUF0IND 0x30 |
| 79 | #define QUADSPI_BUF1IND 0x34 |
| 80 | #define QUADSPI_BUF2IND 0x38 |
| 81 | #define QUADSPI_SFAR 0x100 |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 82 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 83 | #define QUADSPI_SMPR 0x108 |
| 84 | #define QUADSPI_SMPR_DDRSMP_MASK GENMASK(18, 16) |
| 85 | #define QUADSPI_SMPR_FSDLY_MASK BIT(6) |
| 86 | #define QUADSPI_SMPR_FSPHS_MASK BIT(5) |
| 87 | #define QUADSPI_SMPR_HSENA_MASK BIT(0) |
Yuan Yao | d719326 | 2016-03-15 14:36:42 +0800 | [diff] [blame] | 88 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 89 | #define QUADSPI_RBCT 0x110 |
| 90 | #define QUADSPI_RBCT_WMRK_MASK GENMASK(4, 0) |
| 91 | #define QUADSPI_RBCT_RXBRD_USEIPS BIT(8) |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 92 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 93 | #define QUADSPI_TBDR 0x154 |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 94 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 95 | #define QUADSPI_SR 0x15c |
| 96 | #define QUADSPI_SR_IP_ACC_MASK BIT(1) |
| 97 | #define QUADSPI_SR_AHB_ACC_MASK BIT(2) |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 98 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 99 | #define QUADSPI_FR 0x160 |
| 100 | #define QUADSPI_FR_TFF_MASK BIT(0) |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 101 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 102 | #define QUADSPI_RSER 0x164 |
| 103 | #define QUADSPI_RSER_TFIE BIT(0) |
Ye Li | 007b604 | 2019-08-14 11:31:36 +0000 | [diff] [blame] | 104 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 105 | #define QUADSPI_SPTRCLR 0x16c |
| 106 | #define QUADSPI_SPTRCLR_IPPTRC BIT(8) |
| 107 | #define QUADSPI_SPTRCLR_BFPTRC BIT(0) |
Ye Li | 007b604 | 2019-08-14 11:31:36 +0000 | [diff] [blame] | 108 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 109 | #define QUADSPI_SFA1AD 0x180 |
| 110 | #define QUADSPI_SFA2AD 0x184 |
| 111 | #define QUADSPI_SFB1AD 0x188 |
| 112 | #define QUADSPI_SFB2AD 0x18c |
| 113 | #define QUADSPI_RBDR(x) (0x200 + ((x) * 4)) |
Ye Li | 007b604 | 2019-08-14 11:31:36 +0000 | [diff] [blame] | 114 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 115 | #define QUADSPI_LUTKEY 0x300 |
| 116 | #define QUADSPI_LUTKEY_VALUE 0x5AF05AF0 |
| 117 | |
| 118 | #define QUADSPI_LCKCR 0x304 |
| 119 | #define QUADSPI_LCKER_LOCK BIT(0) |
| 120 | #define QUADSPI_LCKER_UNLOCK BIT(1) |
| 121 | |
| 122 | #define QUADSPI_LUT_BASE 0x310 |
| 123 | #define QUADSPI_LUT_OFFSET (SEQID_LUT * 4 * 4) |
| 124 | #define QUADSPI_LUT_REG(idx) \ |
| 125 | (QUADSPI_LUT_BASE + QUADSPI_LUT_OFFSET + (idx) * 4) |
| 126 | |
Ye Li | d7e3c9a | 2020-06-09 00:59:06 -0700 | [diff] [blame] | 127 | #define QUADSPI_AHB_LUT_OFFSET (SEQID_LUT_AHB * 4 * 4) |
| 128 | #define QUADSPI_AHB_LUT_REG(idx) \ |
| 129 | (QUADSPI_LUT_BASE + QUADSPI_AHB_LUT_OFFSET + (idx) * 4) |
| 130 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 131 | /* Instruction set for the LUT register */ |
| 132 | #define LUT_STOP 0 |
| 133 | #define LUT_CMD 1 |
| 134 | #define LUT_ADDR 2 |
| 135 | #define LUT_DUMMY 3 |
| 136 | #define LUT_MODE 4 |
| 137 | #define LUT_MODE2 5 |
| 138 | #define LUT_MODE4 6 |
| 139 | #define LUT_FSL_READ 7 |
| 140 | #define LUT_FSL_WRITE 8 |
| 141 | #define LUT_JMP_ON_CS 9 |
| 142 | #define LUT_ADDR_DDR 10 |
| 143 | #define LUT_MODE_DDR 11 |
| 144 | #define LUT_MODE2_DDR 12 |
| 145 | #define LUT_MODE4_DDR 13 |
| 146 | #define LUT_FSL_READ_DDR 14 |
| 147 | #define LUT_FSL_WRITE_DDR 15 |
| 148 | #define LUT_DATA_LEARN 16 |
| 149 | |
| 150 | /* |
| 151 | * The PAD definitions for LUT register. |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 152 | * |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 153 | * The pad stands for the number of IO lines [0:3]. |
| 154 | * For example, the quad read needs four IO lines, |
| 155 | * so you should use LUT_PAD(4). |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 156 | */ |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 157 | #define LUT_PAD(x) (fls(x) - 1) |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 158 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 159 | /* |
| 160 | * Macro for constructing the LUT entries with the following |
| 161 | * register layout: |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 162 | * |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 163 | * --------------------------------------------------- |
| 164 | * | INSTR1 | PAD1 | OPRND1 | INSTR0 | PAD0 | OPRND0 | |
| 165 | * --------------------------------------------------- |
| 166 | */ |
| 167 | #define LUT_DEF(idx, ins, pad, opr) \ |
| 168 | ((((ins) << 10) | ((pad) << 8) | (opr)) << (((idx) % 2) * 16)) |
| 169 | |
| 170 | /* Controller needs driver to swap endianness */ |
| 171 | #define QUADSPI_QUIRK_SWAP_ENDIAN BIT(0) |
| 172 | |
| 173 | /* Controller needs 4x internal clock */ |
| 174 | #define QUADSPI_QUIRK_4X_INT_CLK BIT(1) |
| 175 | |
| 176 | /* |
| 177 | * TKT253890, the controller needs the driver to fill the txfifo with |
| 178 | * 16 bytes at least to trigger a data transfer, even though the extra |
| 179 | * data won't be transferred. |
| 180 | */ |
| 181 | #define QUADSPI_QUIRK_TKT253890 BIT(2) |
| 182 | |
| 183 | /* TKT245618, the controller cannot wake up from wait mode */ |
| 184 | #define QUADSPI_QUIRK_TKT245618 BIT(3) |
| 185 | |
| 186 | /* |
| 187 | * Controller adds QSPI_AMBA_BASE (base address of the mapped memory) |
| 188 | * internally. No need to add it when setting SFXXAD and SFAR registers |
| 189 | */ |
| 190 | #define QUADSPI_QUIRK_BASE_INTERNAL BIT(4) |
| 191 | |
| 192 | /* |
| 193 | * Controller uses TDH bits in register QUADSPI_FLSHCR. |
| 194 | * They need to be set in accordance with the DDR/SDR mode. |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 195 | */ |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 196 | #define QUADSPI_QUIRK_USE_TDH_SETTING BIT(5) |
| 197 | |
Ye Li | d7e3c9a | 2020-06-09 00:59:06 -0700 | [diff] [blame] | 198 | /* |
| 199 | * Controller only has Two CS on flash A, no flash B port |
| 200 | */ |
| 201 | #define QUADSPI_QUIRK_SINGLE_BUS BIT(6) |
| 202 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 203 | struct fsl_qspi_devtype_data { |
| 204 | unsigned int rxfifo; |
| 205 | unsigned int txfifo; |
| 206 | unsigned int ahb_buf_size; |
| 207 | unsigned int quirks; |
| 208 | bool little_endian; |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 209 | }; |
| 210 | |
Ye Li | 007b604 | 2019-08-14 11:31:36 +0000 | [diff] [blame] | 211 | static const struct fsl_qspi_devtype_data vybrid_data = { |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 212 | .rxfifo = SZ_128, |
| 213 | .txfifo = SZ_64, |
| 214 | .ahb_buf_size = SZ_1K, |
| 215 | .quirks = QUADSPI_QUIRK_SWAP_ENDIAN, |
| 216 | .little_endian = true, |
Ye Li | 007b604 | 2019-08-14 11:31:36 +0000 | [diff] [blame] | 217 | }; |
| 218 | |
| 219 | static const struct fsl_qspi_devtype_data imx6sx_data = { |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 220 | .rxfifo = SZ_128, |
| 221 | .txfifo = SZ_512, |
| 222 | .ahb_buf_size = SZ_1K, |
| 223 | .quirks = QUADSPI_QUIRK_4X_INT_CLK | QUADSPI_QUIRK_TKT245618, |
| 224 | .little_endian = true, |
Ye Li | 007b604 | 2019-08-14 11:31:36 +0000 | [diff] [blame] | 225 | }; |
| 226 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 227 | static const struct fsl_qspi_devtype_data imx7d_data = { |
| 228 | .rxfifo = SZ_128, |
| 229 | .txfifo = SZ_512, |
| 230 | .ahb_buf_size = SZ_1K, |
| 231 | .quirks = QUADSPI_QUIRK_TKT253890 | QUADSPI_QUIRK_4X_INT_CLK | |
| 232 | QUADSPI_QUIRK_USE_TDH_SETTING, |
| 233 | .little_endian = true, |
Ye Li | 007b604 | 2019-08-14 11:31:36 +0000 | [diff] [blame] | 234 | }; |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 235 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 236 | static const struct fsl_qspi_devtype_data imx6ul_data = { |
| 237 | .rxfifo = SZ_128, |
| 238 | .txfifo = SZ_512, |
| 239 | .ahb_buf_size = SZ_1K, |
| 240 | .quirks = QUADSPI_QUIRK_TKT253890 | QUADSPI_QUIRK_4X_INT_CLK | |
| 241 | QUADSPI_QUIRK_USE_TDH_SETTING, |
| 242 | .little_endian = true, |
Ye Li | 57f6775 | 2019-08-14 11:31:40 +0000 | [diff] [blame] | 243 | }; |
| 244 | |
Ye Li | e4d39a0 | 2020-06-09 00:59:05 -0700 | [diff] [blame] | 245 | static const struct fsl_qspi_devtype_data imx7ulp_data = { |
| 246 | .rxfifo = SZ_64, |
| 247 | .txfifo = SZ_64, |
| 248 | .ahb_buf_size = SZ_128, |
| 249 | .quirks = QUADSPI_QUIRK_TKT253890 | QUADSPI_QUIRK_4X_INT_CLK | |
Ye Li | d7e3c9a | 2020-06-09 00:59:06 -0700 | [diff] [blame] | 250 | QUADSPI_QUIRK_USE_TDH_SETTING | QUADSPI_QUIRK_SINGLE_BUS, |
Ye Li | e4d39a0 | 2020-06-09 00:59:05 -0700 | [diff] [blame] | 251 | .little_endian = true, |
| 252 | }; |
| 253 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 254 | static const struct fsl_qspi_devtype_data ls1021a_data = { |
| 255 | .rxfifo = SZ_128, |
| 256 | .txfifo = SZ_64, |
| 257 | .ahb_buf_size = SZ_1K, |
| 258 | .quirks = 0, |
| 259 | .little_endian = false, |
| 260 | }; |
| 261 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 262 | static const struct fsl_qspi_devtype_data ls2080a_data = { |
| 263 | .rxfifo = SZ_128, |
| 264 | .txfifo = SZ_64, |
| 265 | .ahb_buf_size = SZ_1K, |
| 266 | .quirks = QUADSPI_QUIRK_TKT253890 | QUADSPI_QUIRK_BASE_INTERNAL, |
| 267 | .little_endian = true, |
| 268 | }; |
| 269 | |
| 270 | struct fsl_qspi { |
| 271 | struct udevice *dev; |
| 272 | void __iomem *iobase; |
| 273 | void __iomem *ahb_addr; |
| 274 | u32 memmap_phy; |
Ye Li | d7e3c9a | 2020-06-09 00:59:06 -0700 | [diff] [blame] | 275 | u32 memmap_size; |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 276 | const struct fsl_qspi_devtype_data *devtype_data; |
| 277 | int selected; |
| 278 | }; |
| 279 | |
| 280 | static inline int needs_swap_endian(struct fsl_qspi *q) |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 281 | { |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 282 | return q->devtype_data->quirks & QUADSPI_QUIRK_SWAP_ENDIAN; |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 283 | } |
| 284 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 285 | static inline int needs_4x_clock(struct fsl_qspi *q) |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 286 | { |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 287 | return q->devtype_data->quirks & QUADSPI_QUIRK_4X_INT_CLK; |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 288 | } |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 289 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 290 | static inline int needs_fill_txfifo(struct fsl_qspi *q) |
Rajat Srivastava | 234daec | 2018-03-22 13:30:55 +0530 | [diff] [blame] | 291 | { |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 292 | return q->devtype_data->quirks & QUADSPI_QUIRK_TKT253890; |
Rajat Srivastava | 234daec | 2018-03-22 13:30:55 +0530 | [diff] [blame] | 293 | } |
| 294 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 295 | static inline int needs_wakeup_wait_mode(struct fsl_qspi *q) |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 296 | { |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 297 | return q->devtype_data->quirks & QUADSPI_QUIRK_TKT245618; |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 298 | } |
| 299 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 300 | static inline int needs_amba_base_offset(struct fsl_qspi *q) |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 301 | { |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 302 | return !(q->devtype_data->quirks & QUADSPI_QUIRK_BASE_INTERNAL); |
| 303 | } |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 304 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 305 | static inline int needs_tdh_setting(struct fsl_qspi *q) |
| 306 | { |
| 307 | return q->devtype_data->quirks & QUADSPI_QUIRK_USE_TDH_SETTING; |
| 308 | } |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 309 | |
Ye Li | d7e3c9a | 2020-06-09 00:59:06 -0700 | [diff] [blame] | 310 | static inline int needs_single_bus(struct fsl_qspi *q) |
| 311 | { |
| 312 | return q->devtype_data->quirks & QUADSPI_QUIRK_SINGLE_BUS; |
| 313 | } |
| 314 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 315 | /* |
| 316 | * An IC bug makes it necessary to rearrange the 32-bit data. |
| 317 | * Later chips, such as IMX6SLX, have fixed this bug. |
| 318 | */ |
| 319 | static inline u32 fsl_qspi_endian_xchg(struct fsl_qspi *q, u32 a) |
| 320 | { |
| 321 | return needs_swap_endian(q) ? __swab32(a) : a; |
| 322 | } |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 323 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 324 | /* |
| 325 | * R/W functions for big- or little-endian registers: |
| 326 | * The QSPI controller's endianness is independent of |
| 327 | * the CPU core's endianness. So far, although the CPU |
| 328 | * core is little-endian the QSPI controller can use |
| 329 | * big-endian or little-endian. |
| 330 | */ |
| 331 | static void qspi_writel(struct fsl_qspi *q, u32 val, void __iomem *addr) |
| 332 | { |
| 333 | if (q->devtype_data->little_endian) |
| 334 | out_le32(addr, val); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 335 | else |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 336 | out_be32(addr, val); |
| 337 | } |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 338 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 339 | static u32 qspi_readl(struct fsl_qspi *q, void __iomem *addr) |
| 340 | { |
| 341 | if (q->devtype_data->little_endian) |
| 342 | return in_le32(addr); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 343 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 344 | return in_be32(addr); |
| 345 | } |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 346 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 347 | static int fsl_qspi_check_buswidth(struct fsl_qspi *q, u8 width) |
| 348 | { |
| 349 | switch (width) { |
| 350 | case 1: |
| 351 | case 2: |
| 352 | case 4: |
| 353 | return 0; |
| 354 | } |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 355 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 356 | return -ENOTSUPP; |
| 357 | } |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 358 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 359 | static bool fsl_qspi_supports_op(struct spi_slave *slave, |
| 360 | const struct spi_mem_op *op) |
| 361 | { |
| 362 | struct fsl_qspi *q = dev_get_priv(slave->dev->parent); |
| 363 | int ret; |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 364 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 365 | ret = fsl_qspi_check_buswidth(q, op->cmd.buswidth); |
Peng Fan | 3642a87 | 2014-12-31 11:01:39 +0800 | [diff] [blame] | 366 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 367 | if (op->addr.nbytes) |
| 368 | ret |= fsl_qspi_check_buswidth(q, op->addr.buswidth); |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 369 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 370 | if (op->dummy.nbytes) |
| 371 | ret |= fsl_qspi_check_buswidth(q, op->dummy.buswidth); |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 372 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 373 | if (op->data.nbytes) |
| 374 | ret |= fsl_qspi_check_buswidth(q, op->data.buswidth); |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 375 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 376 | if (ret) |
| 377 | return false; |
Yuan Yao | d719326 | 2016-03-15 14:36:42 +0800 | [diff] [blame] | 378 | |
| 379 | /* |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 380 | * The number of instructions needed for the op, needs |
| 381 | * to fit into a single LUT entry. |
Yuan Yao | d719326 | 2016-03-15 14:36:42 +0800 | [diff] [blame] | 382 | */ |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 383 | if (op->addr.nbytes + |
| 384 | (op->dummy.nbytes ? 1 : 0) + |
| 385 | (op->data.nbytes ? 1 : 0) > 6) |
| 386 | return false; |
Yuan Yao | d719326 | 2016-03-15 14:36:42 +0800 | [diff] [blame] | 387 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 388 | /* Max 64 dummy clock cycles supported */ |
| 389 | if (op->dummy.nbytes && |
| 390 | (op->dummy.nbytes * 8 / op->dummy.buswidth > 64)) |
| 391 | return false; |
Yuan Yao | d719326 | 2016-03-15 14:36:42 +0800 | [diff] [blame] | 392 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 393 | /* Max data length, check controller limits and alignment */ |
| 394 | if (op->data.dir == SPI_MEM_DATA_IN && |
| 395 | (op->data.nbytes > q->devtype_data->ahb_buf_size || |
| 396 | (op->data.nbytes > q->devtype_data->rxfifo - 4 && |
| 397 | !IS_ALIGNED(op->data.nbytes, 8)))) |
| 398 | return false; |
| 399 | |
| 400 | if (op->data.dir == SPI_MEM_DATA_OUT && |
| 401 | op->data.nbytes > q->devtype_data->txfifo) |
| 402 | return false; |
| 403 | |
Mathew McBride | 148dba4 | 2021-01-25 03:55:21 +0000 | [diff] [blame] | 404 | return spi_mem_default_supports_op(slave, op); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 405 | } |
| 406 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 407 | static void fsl_qspi_prepare_lut(struct fsl_qspi *q, |
| 408 | const struct spi_mem_op *op) |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 409 | { |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 410 | void __iomem *base = q->iobase; |
| 411 | u32 lutval[4] = {}; |
| 412 | int lutidx = 1, i; |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 413 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 414 | lutval[0] |= LUT_DEF(0, LUT_CMD, LUT_PAD(op->cmd.buswidth), |
| 415 | op->cmd.opcode); |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 416 | |
Ye Li | d7e3c9a | 2020-06-09 00:59:06 -0700 | [diff] [blame] | 417 | if (IS_ENABLED(CONFIG_FSL_QSPI_AHB_FULL_MAP)) { |
| 418 | if (op->addr.nbytes) { |
| 419 | lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_ADDR, |
| 420 | LUT_PAD(op->addr.buswidth), |
| 421 | (op->addr.nbytes == 4) ? 0x20 : 0x18); |
| 422 | lutidx++; |
| 423 | } |
| 424 | } else { |
| 425 | /* |
| 426 | * For some unknown reason, using LUT_ADDR doesn't work in some |
| 427 | * cases (at least with only one byte long addresses), so |
| 428 | * let's use LUT_MODE to write the address bytes one by one |
| 429 | */ |
| 430 | for (i = 0; i < op->addr.nbytes; i++) { |
| 431 | u8 addrbyte = op->addr.val >> (8 * (op->addr.nbytes - i - 1)); |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 432 | |
Ye Li | d7e3c9a | 2020-06-09 00:59:06 -0700 | [diff] [blame] | 433 | lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_MODE, |
| 434 | LUT_PAD(op->addr.buswidth), |
| 435 | addrbyte); |
| 436 | lutidx++; |
| 437 | } |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 438 | } |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 439 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 440 | if (op->dummy.nbytes) { |
| 441 | lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_DUMMY, |
| 442 | LUT_PAD(op->dummy.buswidth), |
| 443 | op->dummy.nbytes * 8 / |
| 444 | op->dummy.buswidth); |
| 445 | lutidx++; |
| 446 | } |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 447 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 448 | if (op->data.nbytes) { |
| 449 | lutval[lutidx / 2] |= LUT_DEF(lutidx, |
| 450 | op->data.dir == SPI_MEM_DATA_IN ? |
| 451 | LUT_FSL_READ : LUT_FSL_WRITE, |
| 452 | LUT_PAD(op->data.buswidth), |
| 453 | 0); |
| 454 | lutidx++; |
| 455 | } |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 456 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 457 | lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_STOP, 0, 0); |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 458 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 459 | /* unlock LUT */ |
| 460 | qspi_writel(q, QUADSPI_LUTKEY_VALUE, q->iobase + QUADSPI_LUTKEY); |
| 461 | qspi_writel(q, QUADSPI_LCKER_UNLOCK, q->iobase + QUADSPI_LCKCR); |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 462 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 463 | dev_dbg(q->dev, "CMD[%x] lutval[0:%x \t 1:%x \t 2:%x \t 3:%x]\n", |
| 464 | op->cmd.opcode, lutval[0], lutval[1], lutval[2], lutval[3]); |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 465 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 466 | /* fill LUT */ |
| 467 | for (i = 0; i < ARRAY_SIZE(lutval); i++) |
| 468 | qspi_writel(q, lutval[i], base + QUADSPI_LUT_REG(i)); |
Ye Li | 416d2ec | 2019-08-14 11:31:27 +0000 | [diff] [blame] | 469 | |
Ye Li | d7e3c9a | 2020-06-09 00:59:06 -0700 | [diff] [blame] | 470 | if (IS_ENABLED(CONFIG_FSL_QSPI_AHB_FULL_MAP)) { |
| 471 | if (op->data.nbytes && op->data.dir == SPI_MEM_DATA_IN && |
| 472 | op->addr.nbytes) { |
| 473 | for (i = 0; i < ARRAY_SIZE(lutval); i++) |
| 474 | qspi_writel(q, lutval[i], base + QUADSPI_AHB_LUT_REG(i)); |
| 475 | } |
| 476 | } |
| 477 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 478 | /* lock LUT */ |
| 479 | qspi_writel(q, QUADSPI_LUTKEY_VALUE, q->iobase + QUADSPI_LUTKEY); |
| 480 | qspi_writel(q, QUADSPI_LCKER_LOCK, q->iobase + QUADSPI_LCKCR); |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | /* |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 484 | * If we have changed the content of the flash by writing or erasing, or if we |
| 485 | * read from flash with a different offset into the page buffer, we need to |
| 486 | * invalidate the AHB buffer. If we do not do so, we may read out the wrong |
| 487 | * data. The spec tells us reset the AHB domain and Serial Flash domain at |
| 488 | * the same time. |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 489 | */ |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 490 | static void fsl_qspi_invalidate(struct fsl_qspi *q) |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 491 | { |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 492 | u32 reg; |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 493 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 494 | reg = qspi_readl(q, q->iobase + QUADSPI_MCR); |
| 495 | reg |= QUADSPI_MCR_SWRSTHD_MASK | QUADSPI_MCR_SWRSTSD_MASK; |
| 496 | qspi_writel(q, reg, q->iobase + QUADSPI_MCR); |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 497 | |
| 498 | /* |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 499 | * The minimum delay : 1 AHB + 2 SFCK clocks. |
| 500 | * Delay 1 us is enough. |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 501 | */ |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 502 | udelay(1); |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 503 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 504 | reg &= ~(QUADSPI_MCR_SWRSTHD_MASK | QUADSPI_MCR_SWRSTSD_MASK); |
| 505 | qspi_writel(q, reg, q->iobase + QUADSPI_MCR); |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 506 | } |
Peng Fan | 1c5f966 | 2015-01-08 10:40:20 +0800 | [diff] [blame] | 507 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 508 | static void fsl_qspi_select_mem(struct fsl_qspi *q, struct spi_slave *slave) |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 509 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 510 | struct dm_spi_slave_plat *plat = |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 511 | dev_get_parent_plat(slave->dev); |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 512 | |
Venkatesh Yadav Abbarapu | 91b9e37 | 2024-09-26 10:25:05 +0530 | [diff] [blame] | 513 | if (q->selected == plat->cs[0]) |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 514 | return; |
Alexander Stein | 283eb4a | 2017-06-01 09:32:19 +0200 | [diff] [blame] | 515 | |
Venkatesh Yadav Abbarapu | 91b9e37 | 2024-09-26 10:25:05 +0530 | [diff] [blame] | 516 | q->selected = plat->cs[0]; |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 517 | fsl_qspi_invalidate(q); |
Peng Fan | 3a34448 | 2015-01-04 17:07:14 +0800 | [diff] [blame] | 518 | } |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 519 | |
Ye Li | d7e3c9a | 2020-06-09 00:59:06 -0700 | [diff] [blame] | 520 | static u32 fsl_qspi_memsize_per_cs(struct fsl_qspi *q) |
| 521 | { |
| 522 | if (IS_ENABLED(CONFIG_FSL_QSPI_AHB_FULL_MAP)) { |
| 523 | if (needs_single_bus(q)) |
| 524 | return q->memmap_size / 2; |
| 525 | else |
| 526 | return q->memmap_size / 4; |
| 527 | } else { |
| 528 | return ALIGN(q->devtype_data->ahb_buf_size, 0x400); |
| 529 | } |
| 530 | } |
| 531 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 532 | static void fsl_qspi_read_ahb(struct fsl_qspi *q, const struct spi_mem_op *op) |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 533 | { |
Ye Li | d7e3c9a | 2020-06-09 00:59:06 -0700 | [diff] [blame] | 534 | void __iomem *ahb_read_addr = q->ahb_addr; |
| 535 | |
| 536 | if (IS_ENABLED(CONFIG_FSL_QSPI_AHB_FULL_MAP)) { |
| 537 | if (op->addr.nbytes) |
| 538 | ahb_read_addr += op->addr.val; |
| 539 | } |
| 540 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 541 | memcpy_fromio(op->data.buf.in, |
Ye Li | d7e3c9a | 2020-06-09 00:59:06 -0700 | [diff] [blame] | 542 | ahb_read_addr + q->selected * fsl_qspi_memsize_per_cs(q), |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 543 | op->data.nbytes); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 544 | } |
| 545 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 546 | static void fsl_qspi_fill_txfifo(struct fsl_qspi *q, |
| 547 | const struct spi_mem_op *op) |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 548 | { |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 549 | void __iomem *base = q->iobase; |
| 550 | int i; |
| 551 | u32 val; |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 552 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 553 | for (i = 0; i < ALIGN_DOWN(op->data.nbytes, 4); i += 4) { |
| 554 | memcpy(&val, op->data.buf.out + i, 4); |
| 555 | val = fsl_qspi_endian_xchg(q, val); |
| 556 | qspi_writel(q, val, base + QUADSPI_TBDR); |
| 557 | } |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 558 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 559 | if (i < op->data.nbytes) { |
| 560 | memcpy(&val, op->data.buf.out + i, op->data.nbytes - i); |
| 561 | val = fsl_qspi_endian_xchg(q, val); |
| 562 | qspi_writel(q, val, base + QUADSPI_TBDR); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 563 | } |
| 564 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 565 | if (needs_fill_txfifo(q)) { |
| 566 | for (i = op->data.nbytes; i < 16; i += 4) |
| 567 | qspi_writel(q, 0, base + QUADSPI_TBDR); |
| 568 | } |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 569 | } |
| 570 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 571 | static void fsl_qspi_read_rxfifo(struct fsl_qspi *q, |
| 572 | const struct spi_mem_op *op) |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 573 | { |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 574 | void __iomem *base = q->iobase; |
| 575 | int i; |
| 576 | u8 *buf = op->data.buf.in; |
| 577 | u32 val; |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 578 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 579 | for (i = 0; i < ALIGN_DOWN(op->data.nbytes, 4); i += 4) { |
| 580 | val = qspi_readl(q, base + QUADSPI_RBDR(i / 4)); |
| 581 | val = fsl_qspi_endian_xchg(q, val); |
| 582 | memcpy(buf + i, &val, 4); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 583 | } |
| 584 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 585 | if (i < op->data.nbytes) { |
| 586 | val = qspi_readl(q, base + QUADSPI_RBDR(i / 4)); |
| 587 | val = fsl_qspi_endian_xchg(q, val); |
| 588 | memcpy(buf + i, &val, op->data.nbytes - i); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 589 | } |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 590 | } |
| 591 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 592 | static int fsl_qspi_readl_poll_tout(struct fsl_qspi *q, void __iomem *base, |
| 593 | u32 mask, u32 delay_us, u32 timeout_us) |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 594 | { |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 595 | u32 reg; |
Alexander Stein | 283eb4a | 2017-06-01 09:32:19 +0200 | [diff] [blame] | 596 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 597 | if (!q->devtype_data->little_endian) |
| 598 | mask = (u32)cpu_to_be32(mask); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 599 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 600 | return readl_poll_timeout(base, reg, !(reg & mask), timeout_us); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 601 | } |
| 602 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 603 | static int fsl_qspi_do_op(struct fsl_qspi *q, const struct spi_mem_op *op) |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 604 | { |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 605 | void __iomem *base = q->iobase; |
| 606 | int err = 0; |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 607 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 608 | /* |
| 609 | * Always start the sequence at the same index since we update |
| 610 | * the LUT at each exec_op() call. And also specify the DATA |
| 611 | * length, since it's has not been specified in the LUT. |
| 612 | */ |
| 613 | qspi_writel(q, op->data.nbytes | QUADSPI_IPCR_SEQID(SEQID_LUT), |
| 614 | base + QUADSPI_IPCR); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 615 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 616 | /* wait for the controller being ready */ |
| 617 | err = fsl_qspi_readl_poll_tout(q, base + QUADSPI_SR, |
| 618 | (QUADSPI_SR_IP_ACC_MASK | |
| 619 | QUADSPI_SR_AHB_ACC_MASK), |
| 620 | 10, 1000); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 621 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 622 | if (!err && op->data.nbytes && op->data.dir == SPI_MEM_DATA_IN) |
| 623 | fsl_qspi_read_rxfifo(q, op); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 624 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 625 | return err; |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 626 | } |
| 627 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 628 | static int fsl_qspi_exec_op(struct spi_slave *slave, |
| 629 | const struct spi_mem_op *op) |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 630 | { |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 631 | struct fsl_qspi *q = dev_get_priv(slave->dev->parent); |
| 632 | void __iomem *base = q->iobase; |
| 633 | u32 addr_offset = 0; |
| 634 | int err = 0; |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 635 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 636 | /* wait for the controller being ready */ |
| 637 | fsl_qspi_readl_poll_tout(q, base + QUADSPI_SR, (QUADSPI_SR_IP_ACC_MASK | |
| 638 | QUADSPI_SR_AHB_ACC_MASK), 10, 1000); |
Alexander Stein | 283eb4a | 2017-06-01 09:32:19 +0200 | [diff] [blame] | 639 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 640 | fsl_qspi_select_mem(q, slave); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 641 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 642 | if (needs_amba_base_offset(q)) |
| 643 | addr_offset = q->memmap_phy; |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 644 | |
Ye Li | d7e3c9a | 2020-06-09 00:59:06 -0700 | [diff] [blame] | 645 | if (IS_ENABLED(CONFIG_FSL_QSPI_AHB_FULL_MAP)) { |
| 646 | if (op->addr.nbytes) |
| 647 | addr_offset += op->addr.val; |
| 648 | } |
| 649 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 650 | qspi_writel(q, |
Ye Li | d7e3c9a | 2020-06-09 00:59:06 -0700 | [diff] [blame] | 651 | q->selected * fsl_qspi_memsize_per_cs(q) + addr_offset, |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 652 | base + QUADSPI_SFAR); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 653 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 654 | qspi_writel(q, qspi_readl(q, base + QUADSPI_MCR) | |
| 655 | QUADSPI_MCR_CLR_RXF_MASK | QUADSPI_MCR_CLR_TXF_MASK, |
| 656 | base + QUADSPI_MCR); |
Alison Wang | c7410e3 | 2014-05-06 09:13:01 +0800 | [diff] [blame] | 657 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 658 | qspi_writel(q, QUADSPI_SPTRCLR_BFPTRC | QUADSPI_SPTRCLR_IPPTRC, |
| 659 | base + QUADSPI_SPTRCLR); |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 660 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 661 | fsl_qspi_prepare_lut(q, op); |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 662 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 663 | /* |
| 664 | * If we have large chunks of data, we read them through the AHB bus |
| 665 | * by accessing the mapped memory. In all other cases we use |
| 666 | * IP commands to access the flash. |
| 667 | */ |
| 668 | if (op->data.nbytes > (q->devtype_data->rxfifo - 4) && |
| 669 | op->data.dir == SPI_MEM_DATA_IN) { |
| 670 | fsl_qspi_read_ahb(q, op); |
| 671 | } else { |
| 672 | qspi_writel(q, QUADSPI_RBCT_WMRK_MASK | |
| 673 | QUADSPI_RBCT_RXBRD_USEIPS, base + QUADSPI_RBCT); |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 674 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 675 | if (op->data.nbytes && op->data.dir == SPI_MEM_DATA_OUT) |
| 676 | fsl_qspi_fill_txfifo(q, op); |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 677 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 678 | err = fsl_qspi_do_op(q, op); |
| 679 | } |
| 680 | |
| 681 | /* Invalidate the data in the AHB buffer. */ |
| 682 | fsl_qspi_invalidate(q); |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 683 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 684 | return err; |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 685 | } |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 686 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 687 | static int fsl_qspi_adjust_op_size(struct spi_slave *slave, |
| 688 | struct spi_mem_op *op) |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 689 | { |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 690 | struct fsl_qspi *q = dev_get_priv(slave->dev->parent); |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 691 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 692 | if (op->data.dir == SPI_MEM_DATA_OUT) { |
| 693 | if (op->data.nbytes > q->devtype_data->txfifo) |
| 694 | op->data.nbytes = q->devtype_data->txfifo; |
| 695 | } else { |
| 696 | if (op->data.nbytes > q->devtype_data->ahb_buf_size) |
| 697 | op->data.nbytes = q->devtype_data->ahb_buf_size; |
| 698 | else if (op->data.nbytes > (q->devtype_data->rxfifo - 4)) |
| 699 | op->data.nbytes = ALIGN_DOWN(op->data.nbytes, 8); |
| 700 | } |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 701 | |
| 702 | return 0; |
| 703 | } |
| 704 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 705 | static int fsl_qspi_default_setup(struct fsl_qspi *q) |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 706 | { |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 707 | void __iomem *base = q->iobase; |
Ye Li | d7e3c9a | 2020-06-09 00:59:06 -0700 | [diff] [blame] | 708 | u32 reg, addr_offset = 0, memsize_cs; |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 709 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 710 | /* Reset the module */ |
| 711 | qspi_writel(q, QUADSPI_MCR_SWRSTSD_MASK | QUADSPI_MCR_SWRSTHD_MASK, |
| 712 | base + QUADSPI_MCR); |
| 713 | udelay(1); |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 714 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 715 | /* Disable the module */ |
| 716 | qspi_writel(q, QUADSPI_MCR_MDIS_MASK | QUADSPI_MCR_RESERVED_MASK, |
| 717 | base + QUADSPI_MCR); |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 718 | |
Yuan Yao | ae41239 | 2016-03-15 14:36:40 +0800 | [diff] [blame] | 719 | /* |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 720 | * Previous boot stages (BootROM, bootloader) might have used DDR |
| 721 | * mode and did not clear the TDH bits. As we currently use SDR mode |
| 722 | * only, clear the TDH bits if necessary. |
Yuan Yao | ae41239 | 2016-03-15 14:36:40 +0800 | [diff] [blame] | 723 | */ |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 724 | if (needs_tdh_setting(q)) |
| 725 | qspi_writel(q, qspi_readl(q, base + QUADSPI_FLSHCR) & |
| 726 | ~QUADSPI_FLSHCR_TDH_MASK, |
| 727 | base + QUADSPI_FLSHCR); |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 728 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 729 | reg = qspi_readl(q, base + QUADSPI_SMPR); |
| 730 | qspi_writel(q, reg & ~(QUADSPI_SMPR_FSDLY_MASK |
| 731 | | QUADSPI_SMPR_FSPHS_MASK |
| 732 | | QUADSPI_SMPR_HSENA_MASK |
| 733 | | QUADSPI_SMPR_DDRSMP_MASK), base + QUADSPI_SMPR); |
Ye Li | 007b604 | 2019-08-14 11:31:36 +0000 | [diff] [blame] | 734 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 735 | /* We only use the buffer3 for AHB read */ |
| 736 | qspi_writel(q, 0, base + QUADSPI_BUF0IND); |
| 737 | qspi_writel(q, 0, base + QUADSPI_BUF1IND); |
| 738 | qspi_writel(q, 0, base + QUADSPI_BUF2IND); |
Suresh Gupta | 4945b87 | 2017-08-30 20:06:33 +0530 | [diff] [blame] | 739 | |
Ye Li | d7e3c9a | 2020-06-09 00:59:06 -0700 | [diff] [blame] | 740 | if (IS_ENABLED(CONFIG_FSL_QSPI_AHB_FULL_MAP)) |
| 741 | qspi_writel(q, QUADSPI_BFGENCR_SEQID(SEQID_LUT_AHB), |
| 742 | q->iobase + QUADSPI_BFGENCR); |
| 743 | else |
| 744 | qspi_writel(q, QUADSPI_BFGENCR_SEQID(SEQID_LUT), |
| 745 | q->iobase + QUADSPI_BFGENCR); |
| 746 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 747 | qspi_writel(q, QUADSPI_RBCT_WMRK_MASK, base + QUADSPI_RBCT); |
| 748 | qspi_writel(q, QUADSPI_BUF3CR_ALLMST_MASK | |
| 749 | QUADSPI_BUF3CR_ADATSZ(q->devtype_data->ahb_buf_size / 8), |
| 750 | base + QUADSPI_BUF3CR); |
Suresh Gupta | 4945b87 | 2017-08-30 20:06:33 +0530 | [diff] [blame] | 751 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 752 | if (needs_amba_base_offset(q)) |
| 753 | addr_offset = q->memmap_phy; |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 754 | |
Yuan Yao | b4bfe10 | 2016-03-15 14:36:41 +0800 | [diff] [blame] | 755 | /* |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 756 | * In HW there can be a maximum of four chips on two buses with |
| 757 | * two chip selects on each bus. We use four chip selects in SW |
| 758 | * to differentiate between the four chips. |
| 759 | * We use ahb_buf_size for each chip and set SFA1AD, SFA2AD, SFB1AD, |
| 760 | * SFB2AD accordingly. |
Yuan Yao | b4bfe10 | 2016-03-15 14:36:41 +0800 | [diff] [blame] | 761 | */ |
Ye Li | d7e3c9a | 2020-06-09 00:59:06 -0700 | [diff] [blame] | 762 | memsize_cs = fsl_qspi_memsize_per_cs(q); |
| 763 | qspi_writel(q, memsize_cs + addr_offset, |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 764 | base + QUADSPI_SFA1AD); |
Ye Li | d7e3c9a | 2020-06-09 00:59:06 -0700 | [diff] [blame] | 765 | qspi_writel(q, memsize_cs * 2 + addr_offset, |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 766 | base + QUADSPI_SFA2AD); |
Ye Li | d7e3c9a | 2020-06-09 00:59:06 -0700 | [diff] [blame] | 767 | if (!needs_single_bus(q)) { |
| 768 | qspi_writel(q, memsize_cs * 3 + addr_offset, |
| 769 | base + QUADSPI_SFB1AD); |
| 770 | qspi_writel(q, memsize_cs * 4 + addr_offset, |
| 771 | base + QUADSPI_SFB2AD); |
| 772 | } |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 773 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 774 | q->selected = -1; |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 775 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 776 | /* Enable the module */ |
| 777 | qspi_writel(q, QUADSPI_MCR_RESERVED_MASK | QUADSPI_MCR_END_CFG_MASK, |
| 778 | base + QUADSPI_MCR); |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 779 | return 0; |
| 780 | } |
| 781 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 782 | static const struct spi_controller_mem_ops fsl_qspi_mem_ops = { |
| 783 | .adjust_op_size = fsl_qspi_adjust_op_size, |
| 784 | .supports_op = fsl_qspi_supports_op, |
| 785 | .exec_op = fsl_qspi_exec_op, |
| 786 | }; |
| 787 | |
| 788 | static int fsl_qspi_probe(struct udevice *bus) |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 789 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 790 | struct dm_spi_bus *dm_bus = dev_get_uclass_priv(bus); |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 791 | struct fsl_qspi *q = dev_get_priv(bus); |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 792 | const void *blob = gd->fdt_blob; |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 793 | int node = dev_of_offset(bus); |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 794 | struct fdt_resource res; |
| 795 | int ret; |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 796 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 797 | q->dev = bus; |
| 798 | q->devtype_data = (struct fsl_qspi_devtype_data *) |
| 799 | dev_get_driver_data(bus); |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 800 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 801 | /* find the resources */ |
| 802 | ret = fdt_get_named_resource(blob, node, "reg", "reg-names", "QuadSPI", |
| 803 | &res); |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 804 | if (ret) { |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 805 | dev_err(bus, "Can't get regs base addresses(ret = %d)!\n", ret); |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 806 | return -ENOMEM; |
| 807 | } |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 808 | |
| 809 | q->iobase = map_physmem(res.start, res.end - res.start, MAP_NOCACHE); |
| 810 | |
Yuan Yao | ae41239 | 2016-03-15 14:36:40 +0800 | [diff] [blame] | 811 | ret = fdt_get_named_resource(blob, node, "reg", "reg-names", |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 812 | "QuadSPI-memory", &res); |
Yuan Yao | ae41239 | 2016-03-15 14:36:40 +0800 | [diff] [blame] | 813 | if (ret) { |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 814 | dev_err(bus, "Can't get AMBA base addresses(ret = %d)!\n", ret); |
Yuan Yao | ae41239 | 2016-03-15 14:36:40 +0800 | [diff] [blame] | 815 | return -ENOMEM; |
| 816 | } |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 817 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 818 | q->ahb_addr = map_physmem(res.start, res.end - res.start, MAP_NOCACHE); |
| 819 | q->memmap_phy = res.start; |
Ye Li | d7e3c9a | 2020-06-09 00:59:06 -0700 | [diff] [blame] | 820 | q->memmap_size = res.end - res.start; |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 821 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 822 | dm_bus->max_hz = fdtdec_get_int(blob, node, "spi-max-frequency", |
| 823 | 66000000); |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 824 | |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 825 | fsl_qspi_default_setup(q); |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 826 | |
| 827 | return 0; |
| 828 | } |
| 829 | |
| 830 | static int fsl_qspi_xfer(struct udevice *dev, unsigned int bitlen, |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 831 | const void *dout, void *din, unsigned long flags) |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 832 | { |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 833 | return 0; |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | static int fsl_qspi_claim_bus(struct udevice *dev) |
| 837 | { |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 838 | return 0; |
| 839 | } |
| 840 | |
| 841 | static int fsl_qspi_release_bus(struct udevice *dev) |
| 842 | { |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 843 | return 0; |
| 844 | } |
| 845 | |
| 846 | static int fsl_qspi_set_speed(struct udevice *bus, uint speed) |
| 847 | { |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 848 | return 0; |
| 849 | } |
| 850 | |
| 851 | static int fsl_qspi_set_mode(struct udevice *bus, uint mode) |
| 852 | { |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 853 | return 0; |
| 854 | } |
| 855 | |
| 856 | static const struct dm_spi_ops fsl_qspi_ops = { |
| 857 | .claim_bus = fsl_qspi_claim_bus, |
| 858 | .release_bus = fsl_qspi_release_bus, |
| 859 | .xfer = fsl_qspi_xfer, |
| 860 | .set_speed = fsl_qspi_set_speed, |
| 861 | .set_mode = fsl_qspi_set_mode, |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 862 | .mem_ops = &fsl_qspi_mem_ops, |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 863 | }; |
| 864 | |
| 865 | static const struct udevice_id fsl_qspi_ids[] = { |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 866 | { .compatible = "fsl,vf610-qspi", .data = (ulong)&vybrid_data, }, |
| 867 | { .compatible = "fsl,imx6sx-qspi", .data = (ulong)&imx6sx_data, }, |
| 868 | { .compatible = "fsl,imx6ul-qspi", .data = (ulong)&imx6ul_data, }, |
| 869 | { .compatible = "fsl,imx7d-qspi", .data = (ulong)&imx7d_data, }, |
Ye Li | e4d39a0 | 2020-06-09 00:59:05 -0700 | [diff] [blame] | 870 | { .compatible = "fsl,imx7ulp-qspi", .data = (ulong)&imx7ulp_data, }, |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 871 | { .compatible = "fsl,ls1021a-qspi", .data = (ulong)&ls1021a_data, }, |
Mathew McBride | b4c5396 | 2021-01-25 03:55:22 +0000 | [diff] [blame] | 872 | { .compatible = "fsl,ls1088a-qspi", .data = (ulong)&ls2080a_data, }, |
Kuldeep Singh | d8429a1 | 2020-02-20 22:57:52 +0530 | [diff] [blame] | 873 | { .compatible = "fsl,ls2080a-qspi", .data = (ulong)&ls2080a_data, }, |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 874 | { } |
| 875 | }; |
| 876 | |
| 877 | U_BOOT_DRIVER(fsl_qspi) = { |
| 878 | .name = "fsl_qspi", |
| 879 | .id = UCLASS_SPI, |
| 880 | .of_match = fsl_qspi_ids, |
| 881 | .ops = &fsl_qspi_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 882 | .priv_auto = sizeof(struct fsl_qspi), |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 883 | .probe = fsl_qspi_probe, |
Haikun.Wang@freescale.com | 221f2e1 | 2015-04-01 11:10:40 +0800 | [diff] [blame] | 884 | }; |