Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 
0003 /*
0004  * NXP FlexSPI(FSPI) controller driver.
0005  *
0006  * Copyright 2019-2020 NXP
0007  * Copyright 2020 Puresoftware Ltd.
0008  *
0009  * FlexSPI is a flexsible SPI host controller which supports two SPI
0010  * channels and up to 4 external devices. Each channel supports
0011  * Single/Dual/Quad/Octal mode data transfer (1/2/4/8 bidirectional
0012  * data lines).
0013  *
0014  * FlexSPI controller is driven by the LUT(Look-up Table) registers
0015  * LUT registers are a look-up-table for sequences of instructions.
0016  * A valid sequence consists of four LUT registers.
0017  * Maximum 32 LUT sequences can be programmed simultaneously.
0018  *
0019  * LUTs are being created at run-time based on the commands passed
0020  * from the spi-mem framework, thus using single LUT index.
0021  *
0022  * Software triggered Flash read/write access by IP Bus.
0023  *
0024  * Memory mapped read access by AHB Bus.
0025  *
0026  * Based on SPI MEM interface and spi-fsl-qspi.c driver.
0027  *
0028  * Author:
0029  *     Yogesh Narayan Gaur <yogeshnarayan.gaur@nxp.com>
0030  *     Boris Brezillon <bbrezillon@kernel.org>
0031  *     Frieder Schrempf <frieder.schrempf@kontron.de>
0032  */
0033 
0034 #include <linux/acpi.h>
0035 #include <linux/bitops.h>
0036 #include <linux/bitfield.h>
0037 #include <linux/clk.h>
0038 #include <linux/completion.h>
0039 #include <linux/delay.h>
0040 #include <linux/err.h>
0041 #include <linux/errno.h>
0042 #include <linux/interrupt.h>
0043 #include <linux/io.h>
0044 #include <linux/iopoll.h>
0045 #include <linux/jiffies.h>
0046 #include <linux/kernel.h>
0047 #include <linux/module.h>
0048 #include <linux/mutex.h>
0049 #include <linux/of.h>
0050 #include <linux/of_device.h>
0051 #include <linux/platform_device.h>
0052 #include <linux/pm_qos.h>
0053 #include <linux/regmap.h>
0054 #include <linux/sizes.h>
0055 #include <linux/sys_soc.h>
0056 
0057 #include <linux/mfd/syscon.h>
0058 #include <linux/spi/spi.h>
0059 #include <linux/spi/spi-mem.h>
0060 
0061 /*
0062  * The driver only uses one single LUT entry, that is updated on
0063  * each call of exec_op(). Index 0 is preset at boot with a basic
0064  * read operation, so let's use the last entry (31).
0065  */
0066 #define SEQID_LUT           31
0067 
0068 /* Registers used by the driver */
0069 #define FSPI_MCR0           0x00
0070 #define FSPI_MCR0_AHB_TIMEOUT(x)    ((x) << 24)
0071 #define FSPI_MCR0_IP_TIMEOUT(x)     ((x) << 16)
0072 #define FSPI_MCR0_LEARN_EN      BIT(15)
0073 #define FSPI_MCR0_SCRFRUN_EN        BIT(14)
0074 #define FSPI_MCR0_OCTCOMB_EN        BIT(13)
0075 #define FSPI_MCR0_DOZE_EN       BIT(12)
0076 #define FSPI_MCR0_HSEN          BIT(11)
0077 #define FSPI_MCR0_SERCLKDIV     BIT(8)
0078 #define FSPI_MCR0_ATDF_EN       BIT(7)
0079 #define FSPI_MCR0_ARDF_EN       BIT(6)
0080 #define FSPI_MCR0_RXCLKSRC(x)       ((x) << 4)
0081 #define FSPI_MCR0_END_CFG(x)        ((x) << 2)
0082 #define FSPI_MCR0_MDIS          BIT(1)
0083 #define FSPI_MCR0_SWRST         BIT(0)
0084 
0085 #define FSPI_MCR1           0x04
0086 #define FSPI_MCR1_SEQ_TIMEOUT(x)    ((x) << 16)
0087 #define FSPI_MCR1_AHB_TIMEOUT(x)    (x)
0088 
0089 #define FSPI_MCR2           0x08
0090 #define FSPI_MCR2_IDLE_WAIT(x)      ((x) << 24)
0091 #define FSPI_MCR2_SAMEDEVICEEN      BIT(15)
0092 #define FSPI_MCR2_CLRLRPHS      BIT(14)
0093 #define FSPI_MCR2_ABRDATSZ      BIT(8)
0094 #define FSPI_MCR2_ABRLEARN      BIT(7)
0095 #define FSPI_MCR2_ABR_READ      BIT(6)
0096 #define FSPI_MCR2_ABRWRITE      BIT(5)
0097 #define FSPI_MCR2_ABRDUMMY      BIT(4)
0098 #define FSPI_MCR2_ABR_MODE      BIT(3)
0099 #define FSPI_MCR2_ABRCADDR      BIT(2)
0100 #define FSPI_MCR2_ABRRADDR      BIT(1)
0101 #define FSPI_MCR2_ABR_CMD       BIT(0)
0102 
0103 #define FSPI_AHBCR          0x0c
0104 #define FSPI_AHBCR_RDADDROPT        BIT(6)
0105 #define FSPI_AHBCR_PREF_EN      BIT(5)
0106 #define FSPI_AHBCR_BUFF_EN      BIT(4)
0107 #define FSPI_AHBCR_CACH_EN      BIT(3)
0108 #define FSPI_AHBCR_CLRTXBUF     BIT(2)
0109 #define FSPI_AHBCR_CLRRXBUF     BIT(1)
0110 #define FSPI_AHBCR_PAR_EN       BIT(0)
0111 
0112 #define FSPI_INTEN          0x10
0113 #define FSPI_INTEN_SCLKSBWR     BIT(9)
0114 #define FSPI_INTEN_SCLKSBRD     BIT(8)
0115 #define FSPI_INTEN_DATALRNFL        BIT(7)
0116 #define FSPI_INTEN_IPTXWE       BIT(6)
0117 #define FSPI_INTEN_IPRXWA       BIT(5)
0118 #define FSPI_INTEN_AHBCMDERR        BIT(4)
0119 #define FSPI_INTEN_IPCMDERR     BIT(3)
0120 #define FSPI_INTEN_AHBCMDGE     BIT(2)
0121 #define FSPI_INTEN_IPCMDGE      BIT(1)
0122 #define FSPI_INTEN_IPCMDDONE        BIT(0)
0123 
0124 #define FSPI_INTR           0x14
0125 #define FSPI_INTR_SCLKSBWR      BIT(9)
0126 #define FSPI_INTR_SCLKSBRD      BIT(8)
0127 #define FSPI_INTR_DATALRNFL     BIT(7)
0128 #define FSPI_INTR_IPTXWE        BIT(6)
0129 #define FSPI_INTR_IPRXWA        BIT(5)
0130 #define FSPI_INTR_AHBCMDERR     BIT(4)
0131 #define FSPI_INTR_IPCMDERR      BIT(3)
0132 #define FSPI_INTR_AHBCMDGE      BIT(2)
0133 #define FSPI_INTR_IPCMDGE       BIT(1)
0134 #define FSPI_INTR_IPCMDDONE     BIT(0)
0135 
0136 #define FSPI_LUTKEY         0x18
0137 #define FSPI_LUTKEY_VALUE       0x5AF05AF0
0138 
0139 #define FSPI_LCKCR          0x1C
0140 
0141 #define FSPI_LCKER_LOCK         0x1
0142 #define FSPI_LCKER_UNLOCK       0x2
0143 
0144 #define FSPI_BUFXCR_INVALID_MSTRID  0xE
0145 #define FSPI_AHBRX_BUF0CR0      0x20
0146 #define FSPI_AHBRX_BUF1CR0      0x24
0147 #define FSPI_AHBRX_BUF2CR0      0x28
0148 #define FSPI_AHBRX_BUF3CR0      0x2C
0149 #define FSPI_AHBRX_BUF4CR0      0x30
0150 #define FSPI_AHBRX_BUF5CR0      0x34
0151 #define FSPI_AHBRX_BUF6CR0      0x38
0152 #define FSPI_AHBRX_BUF7CR0      0x3C
0153 #define FSPI_AHBRXBUF0CR7_PREF      BIT(31)
0154 
0155 #define FSPI_AHBRX_BUF0CR1      0x40
0156 #define FSPI_AHBRX_BUF1CR1      0x44
0157 #define FSPI_AHBRX_BUF2CR1      0x48
0158 #define FSPI_AHBRX_BUF3CR1      0x4C
0159 #define FSPI_AHBRX_BUF4CR1      0x50
0160 #define FSPI_AHBRX_BUF5CR1      0x54
0161 #define FSPI_AHBRX_BUF6CR1      0x58
0162 #define FSPI_AHBRX_BUF7CR1      0x5C
0163 
0164 #define FSPI_FLSHA1CR0          0x60
0165 #define FSPI_FLSHA2CR0          0x64
0166 #define FSPI_FLSHB1CR0          0x68
0167 #define FSPI_FLSHB2CR0          0x6C
0168 #define FSPI_FLSHXCR0_SZ_KB     10
0169 #define FSPI_FLSHXCR0_SZ(x)     ((x) >> FSPI_FLSHXCR0_SZ_KB)
0170 
0171 #define FSPI_FLSHA1CR1          0x70
0172 #define FSPI_FLSHA2CR1          0x74
0173 #define FSPI_FLSHB1CR1          0x78
0174 #define FSPI_FLSHB2CR1          0x7C
0175 #define FSPI_FLSHXCR1_CSINTR(x)     ((x) << 16)
0176 #define FSPI_FLSHXCR1_CAS(x)        ((x) << 11)
0177 #define FSPI_FLSHXCR1_WA        BIT(10)
0178 #define FSPI_FLSHXCR1_TCSH(x)       ((x) << 5)
0179 #define FSPI_FLSHXCR1_TCSS(x)       (x)
0180 
0181 #define FSPI_FLSHA1CR2          0x80
0182 #define FSPI_FLSHA2CR2          0x84
0183 #define FSPI_FLSHB1CR2          0x88
0184 #define FSPI_FLSHB2CR2          0x8C
0185 #define FSPI_FLSHXCR2_CLRINSP       BIT(24)
0186 #define FSPI_FLSHXCR2_AWRWAIT       BIT(16)
0187 #define FSPI_FLSHXCR2_AWRSEQN_SHIFT 13
0188 #define FSPI_FLSHXCR2_AWRSEQI_SHIFT 8
0189 #define FSPI_FLSHXCR2_ARDSEQN_SHIFT 5
0190 #define FSPI_FLSHXCR2_ARDSEQI_SHIFT 0
0191 
0192 #define FSPI_IPCR0          0xA0
0193 
0194 #define FSPI_IPCR1          0xA4
0195 #define FSPI_IPCR1_IPAREN       BIT(31)
0196 #define FSPI_IPCR1_SEQNUM_SHIFT     24
0197 #define FSPI_IPCR1_SEQID_SHIFT      16
0198 #define FSPI_IPCR1_IDATSZ(x)        (x)
0199 
0200 #define FSPI_IPCMD          0xB0
0201 #define FSPI_IPCMD_TRG          BIT(0)
0202 
0203 #define FSPI_DLPR           0xB4
0204 
0205 #define FSPI_IPRXFCR            0xB8
0206 #define FSPI_IPRXFCR_CLR        BIT(0)
0207 #define FSPI_IPRXFCR_DMA_EN     BIT(1)
0208 #define FSPI_IPRXFCR_WMRK(x)        ((x) << 2)
0209 
0210 #define FSPI_IPTXFCR            0xBC
0211 #define FSPI_IPTXFCR_CLR        BIT(0)
0212 #define FSPI_IPTXFCR_DMA_EN     BIT(1)
0213 #define FSPI_IPTXFCR_WMRK(x)        ((x) << 2)
0214 
0215 #define FSPI_DLLACR         0xC0
0216 #define FSPI_DLLACR_OVRDEN      BIT(8)
0217 
0218 #define FSPI_DLLBCR         0xC4
0219 #define FSPI_DLLBCR_OVRDEN      BIT(8)
0220 
0221 #define FSPI_STS0           0xE0
0222 #define FSPI_STS0_DLPHB(x)      ((x) << 8)
0223 #define FSPI_STS0_DLPHA(x)      ((x) << 4)
0224 #define FSPI_STS0_CMD_SRC(x)        ((x) << 2)
0225 #define FSPI_STS0_ARB_IDLE      BIT(1)
0226 #define FSPI_STS0_SEQ_IDLE      BIT(0)
0227 
0228 #define FSPI_STS1           0xE4
0229 #define FSPI_STS1_IP_ERRCD(x)       ((x) << 24)
0230 #define FSPI_STS1_IP_ERRID(x)       ((x) << 16)
0231 #define FSPI_STS1_AHB_ERRCD(x)      ((x) << 8)
0232 #define FSPI_STS1_AHB_ERRID(x)      (x)
0233 
0234 #define FSPI_AHBSPNST           0xEC
0235 #define FSPI_AHBSPNST_DATLFT(x)     ((x) << 16)
0236 #define FSPI_AHBSPNST_BUFID(x)      ((x) << 1)
0237 #define FSPI_AHBSPNST_ACTIVE        BIT(0)
0238 
0239 #define FSPI_IPRXFSTS           0xF0
0240 #define FSPI_IPRXFSTS_RDCNTR(x)     ((x) << 16)
0241 #define FSPI_IPRXFSTS_FILL(x)       (x)
0242 
0243 #define FSPI_IPTXFSTS           0xF4
0244 #define FSPI_IPTXFSTS_WRCNTR(x)     ((x) << 16)
0245 #define FSPI_IPTXFSTS_FILL(x)       (x)
0246 
0247 #define FSPI_RFDR           0x100
0248 #define FSPI_TFDR           0x180
0249 
0250 #define FSPI_LUT_BASE           0x200
0251 #define FSPI_LUT_OFFSET         (SEQID_LUT * 4 * 4)
0252 #define FSPI_LUT_REG(idx) \
0253     (FSPI_LUT_BASE + FSPI_LUT_OFFSET + (idx) * 4)
0254 
0255 /* register map end */
0256 
0257 /* Instruction set for the LUT register. */
0258 #define LUT_STOP            0x00
0259 #define LUT_CMD             0x01
0260 #define LUT_ADDR            0x02
0261 #define LUT_CADDR_SDR           0x03
0262 #define LUT_MODE            0x04
0263 #define LUT_MODE2           0x05
0264 #define LUT_MODE4           0x06
0265 #define LUT_MODE8           0x07
0266 #define LUT_NXP_WRITE           0x08
0267 #define LUT_NXP_READ            0x09
0268 #define LUT_LEARN_SDR           0x0A
0269 #define LUT_DATSZ_SDR           0x0B
0270 #define LUT_DUMMY           0x0C
0271 #define LUT_DUMMY_RWDS_SDR      0x0D
0272 #define LUT_JMP_ON_CS           0x1F
0273 #define LUT_CMD_DDR         0x21
0274 #define LUT_ADDR_DDR            0x22
0275 #define LUT_CADDR_DDR           0x23
0276 #define LUT_MODE_DDR            0x24
0277 #define LUT_MODE2_DDR           0x25
0278 #define LUT_MODE4_DDR           0x26
0279 #define LUT_MODE8_DDR           0x27
0280 #define LUT_WRITE_DDR           0x28
0281 #define LUT_READ_DDR            0x29
0282 #define LUT_LEARN_DDR           0x2A
0283 #define LUT_DATSZ_DDR           0x2B
0284 #define LUT_DUMMY_DDR           0x2C
0285 #define LUT_DUMMY_RWDS_DDR      0x2D
0286 
0287 /*
0288  * Calculate number of required PAD bits for LUT register.
0289  *
0290  * The pad stands for the number of IO lines [0:7].
0291  * For example, the octal read needs eight IO lines,
0292  * so you should use LUT_PAD(8). This macro
0293  * returns 3 i.e. use eight (2^3) IP lines for read.
0294  */
0295 #define LUT_PAD(x) (fls(x) - 1)
0296 
0297 /*
0298  * Macro for constructing the LUT entries with the following
0299  * register layout:
0300  *
0301  *  ---------------------------------------------------
0302  *  | INSTR1 | PAD1 | OPRND1 | INSTR0 | PAD0 | OPRND0 |
0303  *  ---------------------------------------------------
0304  */
0305 #define PAD_SHIFT       8
0306 #define INSTR_SHIFT     10
0307 #define OPRND_SHIFT     16
0308 
0309 /* Macros for constructing the LUT register. */
0310 #define LUT_DEF(idx, ins, pad, opr)           \
0311     ((((ins) << INSTR_SHIFT) | ((pad) << PAD_SHIFT) | \
0312     (opr)) << (((idx) % 2) * OPRND_SHIFT))
0313 
0314 #define POLL_TOUT       5000
0315 #define NXP_FSPI_MAX_CHIPSELECT     4
0316 #define NXP_FSPI_MIN_IOMAP  SZ_4M
0317 
0318 #define DCFG_RCWSR1     0x100
0319 #define SYS_PLL_RAT     GENMASK(6, 2)
0320 
0321 /* Access flash memory using IP bus only */
0322 #define FSPI_QUIRK_USE_IP_ONLY  BIT(0)
0323 
0324 struct nxp_fspi_devtype_data {
0325     unsigned int rxfifo;
0326     unsigned int txfifo;
0327     unsigned int ahb_buf_size;
0328     unsigned int quirks;
0329     bool little_endian;
0330 };
0331 
0332 static struct nxp_fspi_devtype_data lx2160a_data = {
0333     .rxfifo = SZ_512,       /* (64  * 64 bits)  */
0334     .txfifo = SZ_1K,        /* (128 * 64 bits)  */
0335     .ahb_buf_size = SZ_2K,  /* (256 * 64 bits)  */
0336     .quirks = 0,
0337     .little_endian = true,  /* little-endian    */
0338 };
0339 
0340 static struct nxp_fspi_devtype_data imx8mm_data = {
0341     .rxfifo = SZ_512,       /* (64  * 64 bits)  */
0342     .txfifo = SZ_1K,        /* (128 * 64 bits)  */
0343     .ahb_buf_size = SZ_2K,  /* (256 * 64 bits)  */
0344     .quirks = 0,
0345     .little_endian = true,  /* little-endian    */
0346 };
0347 
0348 static struct nxp_fspi_devtype_data imx8qxp_data = {
0349     .rxfifo = SZ_512,       /* (64  * 64 bits)  */
0350     .txfifo = SZ_1K,        /* (128 * 64 bits)  */
0351     .ahb_buf_size = SZ_2K,  /* (256 * 64 bits)  */
0352     .quirks = 0,
0353     .little_endian = true,  /* little-endian    */
0354 };
0355 
0356 static struct nxp_fspi_devtype_data imx8dxl_data = {
0357     .rxfifo = SZ_512,       /* (64  * 64 bits)  */
0358     .txfifo = SZ_1K,        /* (128 * 64 bits)  */
0359     .ahb_buf_size = SZ_2K,  /* (256 * 64 bits)  */
0360     .quirks = FSPI_QUIRK_USE_IP_ONLY,
0361     .little_endian = true,  /* little-endian    */
0362 };
0363 
0364 struct nxp_fspi {
0365     void __iomem *iobase;
0366     void __iomem *ahb_addr;
0367     u32 memmap_phy;
0368     u32 memmap_phy_size;
0369     u32 memmap_start;
0370     u32 memmap_len;
0371     struct clk *clk, *clk_en;
0372     struct device *dev;
0373     struct completion c;
0374     struct nxp_fspi_devtype_data *devtype_data;
0375     struct mutex lock;
0376     struct pm_qos_request pm_qos_req;
0377     int selected;
0378 };
0379 
0380 static inline int needs_ip_only(struct nxp_fspi *f)
0381 {
0382     return f->devtype_data->quirks & FSPI_QUIRK_USE_IP_ONLY;
0383 }
0384 
0385 /*
0386  * R/W functions for big- or little-endian registers:
0387  * The FSPI controller's endianness is independent of
0388  * the CPU core's endianness. So far, although the CPU
0389  * core is little-endian the FSPI controller can use
0390  * big-endian or little-endian.
0391  */
0392 static void fspi_writel(struct nxp_fspi *f, u32 val, void __iomem *addr)
0393 {
0394     if (f->devtype_data->little_endian)
0395         iowrite32(val, addr);
0396     else
0397         iowrite32be(val, addr);
0398 }
0399 
0400 static u32 fspi_readl(struct nxp_fspi *f, void __iomem *addr)
0401 {
0402     if (f->devtype_data->little_endian)
0403         return ioread32(addr);
0404     else
0405         return ioread32be(addr);
0406 }
0407 
0408 static irqreturn_t nxp_fspi_irq_handler(int irq, void *dev_id)
0409 {
0410     struct nxp_fspi *f = dev_id;
0411     u32 reg;
0412 
0413     /* clear interrupt */
0414     reg = fspi_readl(f, f->iobase + FSPI_INTR);
0415     fspi_writel(f, FSPI_INTR_IPCMDDONE, f->iobase + FSPI_INTR);
0416 
0417     if (reg & FSPI_INTR_IPCMDDONE)
0418         complete(&f->c);
0419 
0420     return IRQ_HANDLED;
0421 }
0422 
0423 static int nxp_fspi_check_buswidth(struct nxp_fspi *f, u8 width)
0424 {
0425     switch (width) {
0426     case 1:
0427     case 2:
0428     case 4:
0429     case 8:
0430         return 0;
0431     }
0432 
0433     return -ENOTSUPP;
0434 }
0435 
0436 static bool nxp_fspi_supports_op(struct spi_mem *mem,
0437                  const struct spi_mem_op *op)
0438 {
0439     struct nxp_fspi *f = spi_controller_get_devdata(mem->spi->master);
0440     int ret;
0441 
0442     ret = nxp_fspi_check_buswidth(f, op->cmd.buswidth);
0443 
0444     if (op->addr.nbytes)
0445         ret |= nxp_fspi_check_buswidth(f, op->addr.buswidth);
0446 
0447     if (op->dummy.nbytes)
0448         ret |= nxp_fspi_check_buswidth(f, op->dummy.buswidth);
0449 
0450     if (op->data.nbytes)
0451         ret |= nxp_fspi_check_buswidth(f, op->data.buswidth);
0452 
0453     if (ret)
0454         return false;
0455 
0456     /*
0457      * The number of address bytes should be equal to or less than 4 bytes.
0458      */
0459     if (op->addr.nbytes > 4)
0460         return false;
0461 
0462     /*
0463      * If requested address value is greater than controller assigned
0464      * memory mapped space, return error as it didn't fit in the range
0465      * of assigned address space.
0466      */
0467     if (op->addr.val >= f->memmap_phy_size)
0468         return false;
0469 
0470     /* Max 64 dummy clock cycles supported */
0471     if (op->dummy.buswidth &&
0472         (op->dummy.nbytes * 8 / op->dummy.buswidth > 64))
0473         return false;
0474 
0475     /* Max data length, check controller limits and alignment */
0476     if (op->data.dir == SPI_MEM_DATA_IN &&
0477         (op->data.nbytes > f->devtype_data->ahb_buf_size ||
0478          (op->data.nbytes > f->devtype_data->rxfifo - 4 &&
0479           !IS_ALIGNED(op->data.nbytes, 8))))
0480         return false;
0481 
0482     if (op->data.dir == SPI_MEM_DATA_OUT &&
0483         op->data.nbytes > f->devtype_data->txfifo)
0484         return false;
0485 
0486     return spi_mem_default_supports_op(mem, op);
0487 }
0488 
0489 /* Instead of busy looping invoke readl_poll_timeout functionality. */
0490 static int fspi_readl_poll_tout(struct nxp_fspi *f, void __iomem *base,
0491                 u32 mask, u32 delay_us,
0492                 u32 timeout_us, bool c)
0493 {
0494     u32 reg;
0495 
0496     if (!f->devtype_data->little_endian)
0497         mask = (u32)cpu_to_be32(mask);
0498 
0499     if (c)
0500         return readl_poll_timeout(base, reg, (reg & mask),
0501                       delay_us, timeout_us);
0502     else
0503         return readl_poll_timeout(base, reg, !(reg & mask),
0504                       delay_us, timeout_us);
0505 }
0506 
0507 /*
0508  * If the slave device content being changed by Write/Erase, need to
0509  * invalidate the AHB buffer. This can be achieved by doing the reset
0510  * of controller after setting MCR0[SWRESET] bit.
0511  */
0512 static inline void nxp_fspi_invalid(struct nxp_fspi *f)
0513 {
0514     u32 reg;
0515     int ret;
0516 
0517     reg = fspi_readl(f, f->iobase + FSPI_MCR0);
0518     fspi_writel(f, reg | FSPI_MCR0_SWRST, f->iobase + FSPI_MCR0);
0519 
0520     /* w1c register, wait unit clear */
0521     ret = fspi_readl_poll_tout(f, f->iobase + FSPI_MCR0,
0522                    FSPI_MCR0_SWRST, 0, POLL_TOUT, false);
0523     WARN_ON(ret);
0524 }
0525 
0526 static void nxp_fspi_prepare_lut(struct nxp_fspi *f,
0527                  const struct spi_mem_op *op)
0528 {
0529     void __iomem *base = f->iobase;
0530     u32 lutval[4] = {};
0531     int lutidx = 1, i;
0532 
0533     /* cmd */
0534     lutval[0] |= LUT_DEF(0, LUT_CMD, LUT_PAD(op->cmd.buswidth),
0535                  op->cmd.opcode);
0536 
0537     /* addr bytes */
0538     if (op->addr.nbytes) {
0539         lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_ADDR,
0540                           LUT_PAD(op->addr.buswidth),
0541                           op->addr.nbytes * 8);
0542         lutidx++;
0543     }
0544 
0545     /* dummy bytes, if needed */
0546     if (op->dummy.nbytes) {
0547         lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_DUMMY,
0548         /*
0549          * Due to FlexSPI controller limitation number of PAD for dummy
0550          * buswidth needs to be programmed as equal to data buswidth.
0551          */
0552                           LUT_PAD(op->data.buswidth),
0553                           op->dummy.nbytes * 8 /
0554                           op->dummy.buswidth);
0555         lutidx++;
0556     }
0557 
0558     /* read/write data bytes */
0559     if (op->data.nbytes) {
0560         lutval[lutidx / 2] |= LUT_DEF(lutidx,
0561                           op->data.dir == SPI_MEM_DATA_IN ?
0562                           LUT_NXP_READ : LUT_NXP_WRITE,
0563                           LUT_PAD(op->data.buswidth),
0564                           0);
0565         lutidx++;
0566     }
0567 
0568     /* stop condition. */
0569     lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_STOP, 0, 0);
0570 
0571     /* unlock LUT */
0572     fspi_writel(f, FSPI_LUTKEY_VALUE, f->iobase + FSPI_LUTKEY);
0573     fspi_writel(f, FSPI_LCKER_UNLOCK, f->iobase + FSPI_LCKCR);
0574 
0575     /* fill LUT */
0576     for (i = 0; i < ARRAY_SIZE(lutval); i++)
0577         fspi_writel(f, lutval[i], base + FSPI_LUT_REG(i));
0578 
0579     dev_dbg(f->dev, "CMD[%x] lutval[0:%x \t 1:%x \t 2:%x \t 3:%x], size: 0x%08x\n",
0580         op->cmd.opcode, lutval[0], lutval[1], lutval[2], lutval[3], op->data.nbytes);
0581 
0582     /* lock LUT */
0583     fspi_writel(f, FSPI_LUTKEY_VALUE, f->iobase + FSPI_LUTKEY);
0584     fspi_writel(f, FSPI_LCKER_LOCK, f->iobase + FSPI_LCKCR);
0585 }
0586 
0587 static int nxp_fspi_clk_prep_enable(struct nxp_fspi *f)
0588 {
0589     int ret;
0590 
0591     if (is_acpi_node(f->dev->fwnode))
0592         return 0;
0593 
0594     ret = clk_prepare_enable(f->clk_en);
0595     if (ret)
0596         return ret;
0597 
0598     ret = clk_prepare_enable(f->clk);
0599     if (ret) {
0600         clk_disable_unprepare(f->clk_en);
0601         return ret;
0602     }
0603 
0604     return 0;
0605 }
0606 
0607 static int nxp_fspi_clk_disable_unprep(struct nxp_fspi *f)
0608 {
0609     if (is_acpi_node(f->dev->fwnode))
0610         return 0;
0611 
0612     clk_disable_unprepare(f->clk);
0613     clk_disable_unprepare(f->clk_en);
0614 
0615     return 0;
0616 }
0617 
0618 /*
0619  * In FlexSPI controller, flash access is based on value of FSPI_FLSHXXCR0
0620  * register and start base address of the slave device.
0621  *
0622  *                              (Higher address)
0623  *              --------    <-- FLSHB2CR0
0624  *              |  B2  |
0625  *              |      |
0626  *  B2 start address -->    --------    <-- FLSHB1CR0
0627  *              |  B1  |
0628  *              |      |
0629  *  B1 start address -->    --------    <-- FLSHA2CR0
0630  *              |  A2  |
0631  *              |      |
0632  *  A2 start address -->    --------    <-- FLSHA1CR0
0633  *              |  A1  |
0634  *              |      |
0635  *  A1 start address -->    --------            (Lower address)
0636  *
0637  *
0638  * Start base address defines the starting address range for given CS and
0639  * FSPI_FLSHXXCR0 defines the size of the slave device connected at given CS.
0640  *
0641  * But, different targets are having different combinations of number of CS,
0642  * some targets only have single CS or two CS covering controller's full
0643  * memory mapped space area.
0644  * Thus, implementation is being done as independent of the size and number
0645  * of the connected slave device.
0646  * Assign controller memory mapped space size as the size to the connected
0647  * slave device.
0648  * Mark FLSHxxCR0 as zero initially and then assign value only to the selected
0649  * chip-select Flash configuration register.
0650  *
0651  * For e.g. to access CS2 (B1), FLSHB1CR0 register would be equal to the
0652  * memory mapped size of the controller.
0653  * Value for rest of the CS FLSHxxCR0 register would be zero.
0654  *
0655  */
0656 static void nxp_fspi_select_mem(struct nxp_fspi *f, struct spi_device *spi)
0657 {
0658     unsigned long rate = spi->max_speed_hz;
0659     int ret;
0660     uint64_t size_kb;
0661 
0662     /*
0663      * Return, if previously selected slave device is same as current
0664      * requested slave device.
0665      */
0666     if (f->selected == spi->chip_select)
0667         return;
0668 
0669     /* Reset FLSHxxCR0 registers */
0670     fspi_writel(f, 0, f->iobase + FSPI_FLSHA1CR0);
0671     fspi_writel(f, 0, f->iobase + FSPI_FLSHA2CR0);
0672     fspi_writel(f, 0, f->iobase + FSPI_FLSHB1CR0);
0673     fspi_writel(f, 0, f->iobase + FSPI_FLSHB2CR0);
0674 
0675     /* Assign controller memory mapped space as size, KBytes, of flash. */
0676     size_kb = FSPI_FLSHXCR0_SZ(f->memmap_phy_size);
0677 
0678     fspi_writel(f, size_kb, f->iobase + FSPI_FLSHA1CR0 +
0679             4 * spi->chip_select);
0680 
0681     dev_dbg(f->dev, "Slave device [CS:%x] selected\n", spi->chip_select);
0682 
0683     nxp_fspi_clk_disable_unprep(f);
0684 
0685     ret = clk_set_rate(f->clk, rate);
0686     if (ret)
0687         return;
0688 
0689     ret = nxp_fspi_clk_prep_enable(f);
0690     if (ret)
0691         return;
0692 
0693     f->selected = spi->chip_select;
0694 }
0695 
0696 static int nxp_fspi_read_ahb(struct nxp_fspi *f, const struct spi_mem_op *op)
0697 {
0698     u32 start = op->addr.val;
0699     u32 len = op->data.nbytes;
0700 
0701     /* if necessary, ioremap before AHB read */
0702     if ((!f->ahb_addr) || start < f->memmap_start ||
0703          start + len > f->memmap_start + f->memmap_len) {
0704         if (f->ahb_addr)
0705             iounmap(f->ahb_addr);
0706 
0707         f->memmap_start = start;
0708         f->memmap_len = len > NXP_FSPI_MIN_IOMAP ?
0709                 len : NXP_FSPI_MIN_IOMAP;
0710 
0711         f->ahb_addr = ioremap_wc(f->memmap_phy + f->memmap_start,
0712                      f->memmap_len);
0713 
0714         if (!f->ahb_addr) {
0715             dev_err(f->dev, "failed to alloc memory\n");
0716             return -ENOMEM;
0717         }
0718     }
0719 
0720     /* Read out the data directly from the AHB buffer. */
0721     memcpy_fromio(op->data.buf.in,
0722               f->ahb_addr + start - f->memmap_start, len);
0723 
0724     return 0;
0725 }
0726 
0727 static void nxp_fspi_fill_txfifo(struct nxp_fspi *f,
0728                  const struct spi_mem_op *op)
0729 {
0730     void __iomem *base = f->iobase;
0731     int i, ret;
0732     u8 *buf = (u8 *) op->data.buf.out;
0733 
0734     /* clear the TX FIFO. */
0735     fspi_writel(f, FSPI_IPTXFCR_CLR, base + FSPI_IPTXFCR);
0736 
0737     /*
0738      * Default value of water mark level is 8 bytes, hence in single
0739      * write request controller can write max 8 bytes of data.
0740      */
0741 
0742     for (i = 0; i < ALIGN_DOWN(op->data.nbytes, 8); i += 8) {
0743         /* Wait for TXFIFO empty */
0744         ret = fspi_readl_poll_tout(f, f->iobase + FSPI_INTR,
0745                        FSPI_INTR_IPTXWE, 0,
0746                        POLL_TOUT, true);
0747         WARN_ON(ret);
0748 
0749         fspi_writel(f, *(u32 *) (buf + i), base + FSPI_TFDR);
0750         fspi_writel(f, *(u32 *) (buf + i + 4), base + FSPI_TFDR + 4);
0751         fspi_writel(f, FSPI_INTR_IPTXWE, base + FSPI_INTR);
0752     }
0753 
0754     if (i < op->data.nbytes) {
0755         u32 data = 0;
0756         int j;
0757         /* Wait for TXFIFO empty */
0758         ret = fspi_readl_poll_tout(f, f->iobase + FSPI_INTR,
0759                        FSPI_INTR_IPTXWE, 0,
0760                        POLL_TOUT, true);
0761         WARN_ON(ret);
0762 
0763         for (j = 0; j < ALIGN(op->data.nbytes - i, 4); j += 4) {
0764             memcpy(&data, buf + i + j, 4);
0765             fspi_writel(f, data, base + FSPI_TFDR + j);
0766         }
0767         fspi_writel(f, FSPI_INTR_IPTXWE, base + FSPI_INTR);
0768     }
0769 }
0770 
0771 static void nxp_fspi_read_rxfifo(struct nxp_fspi *f,
0772               const struct spi_mem_op *op)
0773 {
0774     void __iomem *base = f->iobase;
0775     int i, ret;
0776     int len = op->data.nbytes;
0777     u8 *buf = (u8 *) op->data.buf.in;
0778 
0779     /*
0780      * Default value of water mark level is 8 bytes, hence in single
0781      * read request controller can read max 8 bytes of data.
0782      */
0783     for (i = 0; i < ALIGN_DOWN(len, 8); i += 8) {
0784         /* Wait for RXFIFO available */
0785         ret = fspi_readl_poll_tout(f, f->iobase + FSPI_INTR,
0786                        FSPI_INTR_IPRXWA, 0,
0787                        POLL_TOUT, true);
0788         WARN_ON(ret);
0789 
0790         *(u32 *)(buf + i) = fspi_readl(f, base + FSPI_RFDR);
0791         *(u32 *)(buf + i + 4) = fspi_readl(f, base + FSPI_RFDR + 4);
0792         /* move the FIFO pointer */
0793         fspi_writel(f, FSPI_INTR_IPRXWA, base + FSPI_INTR);
0794     }
0795 
0796     if (i < len) {
0797         u32 tmp;
0798         int size, j;
0799 
0800         buf = op->data.buf.in + i;
0801         /* Wait for RXFIFO available */
0802         ret = fspi_readl_poll_tout(f, f->iobase + FSPI_INTR,
0803                        FSPI_INTR_IPRXWA, 0,
0804                        POLL_TOUT, true);
0805         WARN_ON(ret);
0806 
0807         len = op->data.nbytes - i;
0808         for (j = 0; j < op->data.nbytes - i; j += 4) {
0809             tmp = fspi_readl(f, base + FSPI_RFDR + j);
0810             size = min(len, 4);
0811             memcpy(buf + j, &tmp, size);
0812             len -= size;
0813         }
0814     }
0815 
0816     /* invalid the RXFIFO */
0817     fspi_writel(f, FSPI_IPRXFCR_CLR, base + FSPI_IPRXFCR);
0818     /* move the FIFO pointer */
0819     fspi_writel(f, FSPI_INTR_IPRXWA, base + FSPI_INTR);
0820 }
0821 
0822 static int nxp_fspi_do_op(struct nxp_fspi *f, const struct spi_mem_op *op)
0823 {
0824     void __iomem *base = f->iobase;
0825     int seqnum = 0;
0826     int err = 0;
0827     u32 reg;
0828 
0829     reg = fspi_readl(f, base + FSPI_IPRXFCR);
0830     /* invalid RXFIFO first */
0831     reg &= ~FSPI_IPRXFCR_DMA_EN;
0832     reg = reg | FSPI_IPRXFCR_CLR;
0833     fspi_writel(f, reg, base + FSPI_IPRXFCR);
0834 
0835     init_completion(&f->c);
0836 
0837     fspi_writel(f, op->addr.val, base + FSPI_IPCR0);
0838     /*
0839      * Always start the sequence at the same index since we update
0840      * the LUT at each exec_op() call. And also specify the DATA
0841      * length, since it's has not been specified in the LUT.
0842      */
0843     fspi_writel(f, op->data.nbytes |
0844          (SEQID_LUT << FSPI_IPCR1_SEQID_SHIFT) |
0845          (seqnum << FSPI_IPCR1_SEQNUM_SHIFT),
0846          base + FSPI_IPCR1);
0847 
0848     /* Trigger the LUT now. */
0849     fspi_writel(f, FSPI_IPCMD_TRG, base + FSPI_IPCMD);
0850 
0851     /* Wait for the interrupt. */
0852     if (!wait_for_completion_timeout(&f->c, msecs_to_jiffies(1000)))
0853         err = -ETIMEDOUT;
0854 
0855     /* Invoke IP data read, if request is of data read. */
0856     if (!err && op->data.nbytes && op->data.dir == SPI_MEM_DATA_IN)
0857         nxp_fspi_read_rxfifo(f, op);
0858 
0859     return err;
0860 }
0861 
0862 static int nxp_fspi_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
0863 {
0864     struct nxp_fspi *f = spi_controller_get_devdata(mem->spi->master);
0865     int err = 0;
0866 
0867     mutex_lock(&f->lock);
0868 
0869     /* Wait for controller being ready. */
0870     err = fspi_readl_poll_tout(f, f->iobase + FSPI_STS0,
0871                    FSPI_STS0_ARB_IDLE, 1, POLL_TOUT, true);
0872     WARN_ON(err);
0873 
0874     nxp_fspi_select_mem(f, mem->spi);
0875 
0876     nxp_fspi_prepare_lut(f, op);
0877     /*
0878      * If we have large chunks of data, we read them through the AHB bus by
0879      * accessing the mapped memory. In all other cases we use IP commands
0880      * to access the flash. Read via AHB bus may be corrupted due to
0881      * existence of an errata and therefore discard AHB read in such cases.
0882      */
0883     if (op->data.nbytes > (f->devtype_data->rxfifo - 4) &&
0884         op->data.dir == SPI_MEM_DATA_IN &&
0885         !needs_ip_only(f)) {
0886         err = nxp_fspi_read_ahb(f, op);
0887     } else {
0888         if (op->data.nbytes && op->data.dir == SPI_MEM_DATA_OUT)
0889             nxp_fspi_fill_txfifo(f, op);
0890 
0891         err = nxp_fspi_do_op(f, op);
0892     }
0893 
0894     /* Invalidate the data in the AHB buffer. */
0895     nxp_fspi_invalid(f);
0896 
0897     mutex_unlock(&f->lock);
0898 
0899     return err;
0900 }
0901 
0902 static int nxp_fspi_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op)
0903 {
0904     struct nxp_fspi *f = spi_controller_get_devdata(mem->spi->master);
0905 
0906     if (op->data.dir == SPI_MEM_DATA_OUT) {
0907         if (op->data.nbytes > f->devtype_data->txfifo)
0908             op->data.nbytes = f->devtype_data->txfifo;
0909     } else {
0910         if (op->data.nbytes > f->devtype_data->ahb_buf_size)
0911             op->data.nbytes = f->devtype_data->ahb_buf_size;
0912         else if (op->data.nbytes > (f->devtype_data->rxfifo - 4))
0913             op->data.nbytes = ALIGN_DOWN(op->data.nbytes, 8);
0914     }
0915 
0916     /* Limit data bytes to RX FIFO in case of IP read only */
0917     if (op->data.dir == SPI_MEM_DATA_IN &&
0918         needs_ip_only(f) &&
0919         op->data.nbytes > f->devtype_data->rxfifo)
0920         op->data.nbytes = f->devtype_data->rxfifo;
0921 
0922     return 0;
0923 }
0924 
0925 static void erratum_err050568(struct nxp_fspi *f)
0926 {
0927     const struct soc_device_attribute ls1028a_soc_attr[] = {
0928         { .family = "QorIQ LS1028A" },
0929         { /* sentinel */ }
0930     };
0931     struct regmap *map;
0932     u32 val, sys_pll_ratio;
0933     int ret;
0934 
0935     /* Check for LS1028A family */
0936     if (!soc_device_match(ls1028a_soc_attr)) {
0937         dev_dbg(f->dev, "Errata applicable only for LS1028A\n");
0938         return;
0939     }
0940 
0941     map = syscon_regmap_lookup_by_compatible("fsl,ls1028a-dcfg");
0942     if (IS_ERR(map)) {
0943         dev_err(f->dev, "No syscon regmap\n");
0944         goto err;
0945     }
0946 
0947     ret = regmap_read(map, DCFG_RCWSR1, &val);
0948     if (ret < 0)
0949         goto err;
0950 
0951     sys_pll_ratio = FIELD_GET(SYS_PLL_RAT, val);
0952     dev_dbg(f->dev, "val: 0x%08x, sys_pll_ratio: %d\n", val, sys_pll_ratio);
0953 
0954     /* Use IP bus only if platform clock is 300MHz */
0955     if (sys_pll_ratio == 3)
0956         f->devtype_data->quirks |= FSPI_QUIRK_USE_IP_ONLY;
0957 
0958     return;
0959 
0960 err:
0961     dev_err(f->dev, "Errata cannot be executed. Read via IP bus may not work\n");
0962 }
0963 
0964 static int nxp_fspi_default_setup(struct nxp_fspi *f)
0965 {
0966     void __iomem *base = f->iobase;
0967     int ret, i;
0968     u32 reg;
0969 
0970     /* disable and unprepare clock to avoid glitch pass to controller */
0971     nxp_fspi_clk_disable_unprep(f);
0972 
0973     /* the default frequency, we will change it later if necessary. */
0974     ret = clk_set_rate(f->clk, 20000000);
0975     if (ret)
0976         return ret;
0977 
0978     ret = nxp_fspi_clk_prep_enable(f);
0979     if (ret)
0980         return ret;
0981 
0982     /*
0983      * ERR050568: Flash access by FlexSPI AHB command may not work with
0984      * platform frequency equal to 300 MHz on LS1028A.
0985      * LS1028A reuses LX2160A compatible entry. Make errata applicable for
0986      * Layerscape LS1028A platform.
0987      */
0988     if (of_device_is_compatible(f->dev->of_node, "nxp,lx2160a-fspi"))
0989         erratum_err050568(f);
0990 
0991     /* Reset the module */
0992     /* w1c register, wait unit clear */
0993     ret = fspi_readl_poll_tout(f, f->iobase + FSPI_MCR0,
0994                    FSPI_MCR0_SWRST, 0, POLL_TOUT, false);
0995     WARN_ON(ret);
0996 
0997     /* Disable the module */
0998     fspi_writel(f, FSPI_MCR0_MDIS, base + FSPI_MCR0);
0999 
1000     /* Reset the DLL register to default value */
1001     fspi_writel(f, FSPI_DLLACR_OVRDEN, base + FSPI_DLLACR);
1002     fspi_writel(f, FSPI_DLLBCR_OVRDEN, base + FSPI_DLLBCR);
1003 
1004     /* enable module */
1005     fspi_writel(f, FSPI_MCR0_AHB_TIMEOUT(0xFF) |
1006             FSPI_MCR0_IP_TIMEOUT(0xFF) | (u32) FSPI_MCR0_OCTCOMB_EN,
1007             base + FSPI_MCR0);
1008 
1009     /*
1010      * Disable same device enable bit and configure all slave devices
1011      * independently.
1012      */
1013     reg = fspi_readl(f, f->iobase + FSPI_MCR2);
1014     reg = reg & ~(FSPI_MCR2_SAMEDEVICEEN);
1015     fspi_writel(f, reg, base + FSPI_MCR2);
1016 
1017     /* AHB configuration for access buffer 0~7. */
1018     for (i = 0; i < 7; i++)
1019         fspi_writel(f, 0, base + FSPI_AHBRX_BUF0CR0 + 4 * i);
1020 
1021     /*
1022      * Set ADATSZ with the maximum AHB buffer size to improve the read
1023      * performance.
1024      */
1025     fspi_writel(f, (f->devtype_data->ahb_buf_size / 8 |
1026           FSPI_AHBRXBUF0CR7_PREF), base + FSPI_AHBRX_BUF7CR0);
1027 
1028     /* prefetch and no start address alignment limitation */
1029     fspi_writel(f, FSPI_AHBCR_PREF_EN | FSPI_AHBCR_RDADDROPT,
1030          base + FSPI_AHBCR);
1031 
1032     /* AHB Read - Set lut sequence ID for all CS. */
1033     fspi_writel(f, SEQID_LUT, base + FSPI_FLSHA1CR2);
1034     fspi_writel(f, SEQID_LUT, base + FSPI_FLSHA2CR2);
1035     fspi_writel(f, SEQID_LUT, base + FSPI_FLSHB1CR2);
1036     fspi_writel(f, SEQID_LUT, base + FSPI_FLSHB2CR2);
1037 
1038     f->selected = -1;
1039 
1040     /* enable the interrupt */
1041     fspi_writel(f, FSPI_INTEN_IPCMDDONE, base + FSPI_INTEN);
1042 
1043     return 0;
1044 }
1045 
1046 static const char *nxp_fspi_get_name(struct spi_mem *mem)
1047 {
1048     struct nxp_fspi *f = spi_controller_get_devdata(mem->spi->master);
1049     struct device *dev = &mem->spi->dev;
1050     const char *name;
1051 
1052     // Set custom name derived from the platform_device of the controller.
1053     if (of_get_available_child_count(f->dev->of_node) == 1)
1054         return dev_name(f->dev);
1055 
1056     name = devm_kasprintf(dev, GFP_KERNEL,
1057                   "%s-%d", dev_name(f->dev),
1058                   mem->spi->chip_select);
1059 
1060     if (!name) {
1061         dev_err(dev, "failed to get memory for custom flash name\n");
1062         return ERR_PTR(-ENOMEM);
1063     }
1064 
1065     return name;
1066 }
1067 
1068 static const struct spi_controller_mem_ops nxp_fspi_mem_ops = {
1069     .adjust_op_size = nxp_fspi_adjust_op_size,
1070     .supports_op = nxp_fspi_supports_op,
1071     .exec_op = nxp_fspi_exec_op,
1072     .get_name = nxp_fspi_get_name,
1073 };
1074 
1075 static int nxp_fspi_probe(struct platform_device *pdev)
1076 {
1077     struct spi_controller *ctlr;
1078     struct device *dev = &pdev->dev;
1079     struct device_node *np = dev->of_node;
1080     struct resource *res;
1081     struct nxp_fspi *f;
1082     int ret;
1083     u32 reg;
1084 
1085     ctlr = spi_alloc_master(&pdev->dev, sizeof(*f));
1086     if (!ctlr)
1087         return -ENOMEM;
1088 
1089     ctlr->mode_bits = SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL |
1090               SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL;
1091 
1092     f = spi_controller_get_devdata(ctlr);
1093     f->dev = dev;
1094     f->devtype_data = (struct nxp_fspi_devtype_data *)device_get_match_data(dev);
1095     if (!f->devtype_data) {
1096         ret = -ENODEV;
1097         goto err_put_ctrl;
1098     }
1099 
1100     platform_set_drvdata(pdev, f);
1101 
1102     /* find the resources - configuration register address space */
1103     if (is_acpi_node(f->dev->fwnode))
1104         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1105     else
1106         res = platform_get_resource_byname(pdev,
1107                 IORESOURCE_MEM, "fspi_base");
1108 
1109     f->iobase = devm_ioremap_resource(dev, res);
1110     if (IS_ERR(f->iobase)) {
1111         ret = PTR_ERR(f->iobase);
1112         goto err_put_ctrl;
1113     }
1114 
1115     /* find the resources - controller memory mapped space */
1116     if (is_acpi_node(f->dev->fwnode))
1117         res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1118     else
1119         res = platform_get_resource_byname(pdev,
1120                 IORESOURCE_MEM, "fspi_mmap");
1121 
1122     if (!res) {
1123         ret = -ENODEV;
1124         goto err_put_ctrl;
1125     }
1126 
1127     /* assign memory mapped starting address and mapped size. */
1128     f->memmap_phy = res->start;
1129     f->memmap_phy_size = resource_size(res);
1130 
1131     /* find the clocks */
1132     if (dev_of_node(&pdev->dev)) {
1133         f->clk_en = devm_clk_get(dev, "fspi_en");
1134         if (IS_ERR(f->clk_en)) {
1135             ret = PTR_ERR(f->clk_en);
1136             goto err_put_ctrl;
1137         }
1138 
1139         f->clk = devm_clk_get(dev, "fspi");
1140         if (IS_ERR(f->clk)) {
1141             ret = PTR_ERR(f->clk);
1142             goto err_put_ctrl;
1143         }
1144 
1145         ret = nxp_fspi_clk_prep_enable(f);
1146         if (ret) {
1147             dev_err(dev, "can not enable the clock\n");
1148             goto err_put_ctrl;
1149         }
1150     }
1151 
1152     /* Clear potential interrupts */
1153     reg = fspi_readl(f, f->iobase + FSPI_INTR);
1154     if (reg)
1155         fspi_writel(f, reg, f->iobase + FSPI_INTR);
1156 
1157     /* find the irq */
1158     ret = platform_get_irq(pdev, 0);
1159     if (ret < 0)
1160         goto err_disable_clk;
1161 
1162     ret = devm_request_irq(dev, ret,
1163             nxp_fspi_irq_handler, 0, pdev->name, f);
1164     if (ret) {
1165         dev_err(dev, "failed to request irq: %d\n", ret);
1166         goto err_disable_clk;
1167     }
1168 
1169     mutex_init(&f->lock);
1170 
1171     ctlr->bus_num = -1;
1172     ctlr->num_chipselect = NXP_FSPI_MAX_CHIPSELECT;
1173     ctlr->mem_ops = &nxp_fspi_mem_ops;
1174 
1175     nxp_fspi_default_setup(f);
1176 
1177     ctlr->dev.of_node = np;
1178 
1179     ret = devm_spi_register_controller(&pdev->dev, ctlr);
1180     if (ret)
1181         goto err_destroy_mutex;
1182 
1183     return 0;
1184 
1185 err_destroy_mutex:
1186     mutex_destroy(&f->lock);
1187 
1188 err_disable_clk:
1189     nxp_fspi_clk_disable_unprep(f);
1190 
1191 err_put_ctrl:
1192     spi_controller_put(ctlr);
1193 
1194     dev_err(dev, "NXP FSPI probe failed\n");
1195     return ret;
1196 }
1197 
1198 static int nxp_fspi_remove(struct platform_device *pdev)
1199 {
1200     struct nxp_fspi *f = platform_get_drvdata(pdev);
1201 
1202     /* disable the hardware */
1203     fspi_writel(f, FSPI_MCR0_MDIS, f->iobase + FSPI_MCR0);
1204 
1205     nxp_fspi_clk_disable_unprep(f);
1206 
1207     mutex_destroy(&f->lock);
1208 
1209     if (f->ahb_addr)
1210         iounmap(f->ahb_addr);
1211 
1212     return 0;
1213 }
1214 
1215 static int nxp_fspi_suspend(struct device *dev)
1216 {
1217     return 0;
1218 }
1219 
1220 static int nxp_fspi_resume(struct device *dev)
1221 {
1222     struct nxp_fspi *f = dev_get_drvdata(dev);
1223 
1224     nxp_fspi_default_setup(f);
1225 
1226     return 0;
1227 }
1228 
1229 static const struct of_device_id nxp_fspi_dt_ids[] = {
1230     { .compatible = "nxp,lx2160a-fspi", .data = (void *)&lx2160a_data, },
1231     { .compatible = "nxp,imx8mm-fspi", .data = (void *)&imx8mm_data, },
1232     { .compatible = "nxp,imx8mp-fspi", .data = (void *)&imx8mm_data, },
1233     { .compatible = "nxp,imx8qxp-fspi", .data = (void *)&imx8qxp_data, },
1234     { .compatible = "nxp,imx8dxl-fspi", .data = (void *)&imx8dxl_data, },
1235     { /* sentinel */ }
1236 };
1237 MODULE_DEVICE_TABLE(of, nxp_fspi_dt_ids);
1238 
1239 #ifdef CONFIG_ACPI
1240 static const struct acpi_device_id nxp_fspi_acpi_ids[] = {
1241     { "NXP0009", .driver_data = (kernel_ulong_t)&lx2160a_data, },
1242     {}
1243 };
1244 MODULE_DEVICE_TABLE(acpi, nxp_fspi_acpi_ids);
1245 #endif
1246 
1247 static const struct dev_pm_ops nxp_fspi_pm_ops = {
1248     .suspend    = nxp_fspi_suspend,
1249     .resume     = nxp_fspi_resume,
1250 };
1251 
1252 static struct platform_driver nxp_fspi_driver = {
1253     .driver = {
1254         .name   = "nxp-fspi",
1255         .of_match_table = nxp_fspi_dt_ids,
1256         .acpi_match_table = ACPI_PTR(nxp_fspi_acpi_ids),
1257         .pm =   &nxp_fspi_pm_ops,
1258     },
1259     .probe          = nxp_fspi_probe,
1260     .remove     = nxp_fspi_remove,
1261 };
1262 module_platform_driver(nxp_fspi_driver);
1263 
1264 MODULE_DESCRIPTION("NXP FSPI Controller Driver");
1265 MODULE_AUTHOR("NXP Semiconductor");
1266 MODULE_AUTHOR("Yogesh Narayan Gaur <yogeshnarayan.gaur@nxp.com>");
1267 MODULE_AUTHOR("Boris Brezillon <bbrezillon@kernel.org>");
1268 MODULE_AUTHOR("Frieder Schrempf <frieder.schrempf@kontron.de>");
1269 MODULE_LICENSE("GPL v2");