blob: 1476135e0d50176312c4534de0b1386296e79405 [file] [log] [blame]
Huang Shijiee46ecda2014-02-24 18:37:42 +08001/*
2 * Freescale QuadSPI driver.
3 *
4 * Copyright (C) 2013 Freescale Semiconductor, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/interrupt.h>
14#include <linux/errno.h>
15#include <linux/platform_device.h>
16#include <linux/sched.h>
17#include <linux/delay.h>
18#include <linux/io.h>
19#include <linux/clk.h>
20#include <linux/err.h>
21#include <linux/of.h>
22#include <linux/of_device.h>
23#include <linux/timer.h>
24#include <linux/jiffies.h>
25#include <linux/completion.h>
26#include <linux/mtd/mtd.h>
27#include <linux/mtd/partitions.h>
28#include <linux/mtd/spi-nor.h>
Han Xu392d39c2015-05-13 14:40:57 -050029#include <linux/mutex.h>
Frank Li5cc66cb2015-08-04 10:26:04 -050030#include <linux/pm_qos.h>
Fabio Estevam01a3c622015-10-14 00:39:44 -030031#include <linux/sizes.h>
Huang Shijiee46ecda2014-02-24 18:37:42 +080032
Han Xu80d37722015-08-04 10:25:29 -050033/* Controller needs driver to swap endian */
34#define QUADSPI_QUIRK_SWAP_ENDIAN (1 << 0)
35/* Controller needs 4x internal clock */
36#define QUADSPI_QUIRK_4X_INT_CLK (1 << 1)
Frank Lid371cbf2015-08-04 10:25:35 -050037/*
38 * TKT253890, Controller needs driver to fill txfifo till 16 byte to
39 * trigger data transfer even though extern data will not transferred.
40 */
41#define QUADSPI_QUIRK_TKT253890 (1 << 2)
Frank Li5cc66cb2015-08-04 10:26:04 -050042/* Controller cannot wake up from wait mode, TKT245618 */
43#define QUADSPI_QUIRK_TKT245618 (1 << 3)
Han Xu80d37722015-08-04 10:25:29 -050044
Huang Shijiee46ecda2014-02-24 18:37:42 +080045/* The registers */
46#define QUADSPI_MCR 0x00
47#define QUADSPI_MCR_RESERVED_SHIFT 16
48#define QUADSPI_MCR_RESERVED_MASK (0xF << QUADSPI_MCR_RESERVED_SHIFT)
49#define QUADSPI_MCR_MDIS_SHIFT 14
50#define QUADSPI_MCR_MDIS_MASK (1 << QUADSPI_MCR_MDIS_SHIFT)
51#define QUADSPI_MCR_CLR_TXF_SHIFT 11
52#define QUADSPI_MCR_CLR_TXF_MASK (1 << QUADSPI_MCR_CLR_TXF_SHIFT)
53#define QUADSPI_MCR_CLR_RXF_SHIFT 10
54#define QUADSPI_MCR_CLR_RXF_MASK (1 << QUADSPI_MCR_CLR_RXF_SHIFT)
55#define QUADSPI_MCR_DDR_EN_SHIFT 7
56#define QUADSPI_MCR_DDR_EN_MASK (1 << QUADSPI_MCR_DDR_EN_SHIFT)
57#define QUADSPI_MCR_END_CFG_SHIFT 2
58#define QUADSPI_MCR_END_CFG_MASK (3 << QUADSPI_MCR_END_CFG_SHIFT)
59#define QUADSPI_MCR_SWRSTHD_SHIFT 1
60#define QUADSPI_MCR_SWRSTHD_MASK (1 << QUADSPI_MCR_SWRSTHD_SHIFT)
61#define QUADSPI_MCR_SWRSTSD_SHIFT 0
62#define QUADSPI_MCR_SWRSTSD_MASK (1 << QUADSPI_MCR_SWRSTSD_SHIFT)
63
64#define QUADSPI_IPCR 0x08
65#define QUADSPI_IPCR_SEQID_SHIFT 24
66#define QUADSPI_IPCR_SEQID_MASK (0xF << QUADSPI_IPCR_SEQID_SHIFT)
67
68#define QUADSPI_BUF0CR 0x10
69#define QUADSPI_BUF1CR 0x14
70#define QUADSPI_BUF2CR 0x18
71#define QUADSPI_BUFXCR_INVALID_MSTRID 0xe
72
73#define QUADSPI_BUF3CR 0x1c
74#define QUADSPI_BUF3CR_ALLMST_SHIFT 31
Allen Xu4e898ce2015-01-14 00:28:56 +080075#define QUADSPI_BUF3CR_ALLMST_MASK (1 << QUADSPI_BUF3CR_ALLMST_SHIFT)
76#define QUADSPI_BUF3CR_ADATSZ_SHIFT 8
77#define QUADSPI_BUF3CR_ADATSZ_MASK (0xFF << QUADSPI_BUF3CR_ADATSZ_SHIFT)
Huang Shijiee46ecda2014-02-24 18:37:42 +080078
79#define QUADSPI_BFGENCR 0x20
80#define QUADSPI_BFGENCR_PAR_EN_SHIFT 16
81#define QUADSPI_BFGENCR_PAR_EN_MASK (1 << (QUADSPI_BFGENCR_PAR_EN_SHIFT))
82#define QUADSPI_BFGENCR_SEQID_SHIFT 12
83#define QUADSPI_BFGENCR_SEQID_MASK (0xF << QUADSPI_BFGENCR_SEQID_SHIFT)
84
85#define QUADSPI_BUF0IND 0x30
86#define QUADSPI_BUF1IND 0x34
87#define QUADSPI_BUF2IND 0x38
88#define QUADSPI_SFAR 0x100
89
90#define QUADSPI_SMPR 0x108
91#define QUADSPI_SMPR_DDRSMP_SHIFT 16
92#define QUADSPI_SMPR_DDRSMP_MASK (7 << QUADSPI_SMPR_DDRSMP_SHIFT)
93#define QUADSPI_SMPR_FSDLY_SHIFT 6
94#define QUADSPI_SMPR_FSDLY_MASK (1 << QUADSPI_SMPR_FSDLY_SHIFT)
95#define QUADSPI_SMPR_FSPHS_SHIFT 5
96#define QUADSPI_SMPR_FSPHS_MASK (1 << QUADSPI_SMPR_FSPHS_SHIFT)
97#define QUADSPI_SMPR_HSENA_SHIFT 0
98#define QUADSPI_SMPR_HSENA_MASK (1 << QUADSPI_SMPR_HSENA_SHIFT)
99
100#define QUADSPI_RBSR 0x10c
101#define QUADSPI_RBSR_RDBFL_SHIFT 8
102#define QUADSPI_RBSR_RDBFL_MASK (0x3F << QUADSPI_RBSR_RDBFL_SHIFT)
103
104#define QUADSPI_RBCT 0x110
105#define QUADSPI_RBCT_WMRK_MASK 0x1F
106#define QUADSPI_RBCT_RXBRD_SHIFT 8
107#define QUADSPI_RBCT_RXBRD_USEIPS (0x1 << QUADSPI_RBCT_RXBRD_SHIFT)
108
109#define QUADSPI_TBSR 0x150
110#define QUADSPI_TBDR 0x154
111#define QUADSPI_SR 0x15c
112#define QUADSPI_SR_IP_ACC_SHIFT 1
113#define QUADSPI_SR_IP_ACC_MASK (0x1 << QUADSPI_SR_IP_ACC_SHIFT)
114#define QUADSPI_SR_AHB_ACC_SHIFT 2
115#define QUADSPI_SR_AHB_ACC_MASK (0x1 << QUADSPI_SR_AHB_ACC_SHIFT)
116
117#define QUADSPI_FR 0x160
118#define QUADSPI_FR_TFF_MASK 0x1
119
120#define QUADSPI_SFA1AD 0x180
121#define QUADSPI_SFA2AD 0x184
122#define QUADSPI_SFB1AD 0x188
123#define QUADSPI_SFB2AD 0x18c
124#define QUADSPI_RBDR 0x200
125
126#define QUADSPI_LUTKEY 0x300
127#define QUADSPI_LUTKEY_VALUE 0x5AF05AF0
128
129#define QUADSPI_LCKCR 0x304
130#define QUADSPI_LCKER_LOCK 0x1
131#define QUADSPI_LCKER_UNLOCK 0x2
132
133#define QUADSPI_RSER 0x164
134#define QUADSPI_RSER_TFIE (0x1 << 0)
135
136#define QUADSPI_LUT_BASE 0x310
137
138/*
139 * The definition of the LUT register shows below:
140 *
141 * ---------------------------------------------------
142 * | INSTR1 | PAD1 | OPRND1 | INSTR0 | PAD0 | OPRND0 |
143 * ---------------------------------------------------
144 */
145#define OPRND0_SHIFT 0
146#define PAD0_SHIFT 8
147#define INSTR0_SHIFT 10
148#define OPRND1_SHIFT 16
149
150/* Instruction set for the LUT register. */
151#define LUT_STOP 0
152#define LUT_CMD 1
153#define LUT_ADDR 2
154#define LUT_DUMMY 3
155#define LUT_MODE 4
156#define LUT_MODE2 5
157#define LUT_MODE4 6
Han Xu04850c42015-10-23 13:18:28 -0500158#define LUT_FSL_READ 7
159#define LUT_FSL_WRITE 8
Huang Shijiee46ecda2014-02-24 18:37:42 +0800160#define LUT_JMP_ON_CS 9
161#define LUT_ADDR_DDR 10
162#define LUT_MODE_DDR 11
163#define LUT_MODE2_DDR 12
164#define LUT_MODE4_DDR 13
Han Xu04850c42015-10-23 13:18:28 -0500165#define LUT_FSL_READ_DDR 14
166#define LUT_FSL_WRITE_DDR 15
Huang Shijiee46ecda2014-02-24 18:37:42 +0800167#define LUT_DATA_LEARN 16
168
169/*
170 * The PAD definitions for LUT register.
171 *
172 * The pad stands for the lines number of IO[0:3].
173 * For example, the Quad read need four IO lines, so you should
174 * set LUT_PAD4 which means we use four IO lines.
175 */
176#define LUT_PAD1 0
177#define LUT_PAD2 1
178#define LUT_PAD4 2
179
180/* Oprands for the LUT register. */
181#define ADDR24BIT 0x18
182#define ADDR32BIT 0x20
183
184/* Macros for constructing the LUT register. */
185#define LUT0(ins, pad, opr) \
186 (((opr) << OPRND0_SHIFT) | ((LUT_##pad) << PAD0_SHIFT) | \
187 ((LUT_##ins) << INSTR0_SHIFT))
188
189#define LUT1(ins, pad, opr) (LUT0(ins, pad, opr) << OPRND1_SHIFT)
190
191/* other macros for LUT register. */
192#define QUADSPI_LUT(x) (QUADSPI_LUT_BASE + (x) * 4)
193#define QUADSPI_LUT_NUM 64
194
195/* SEQID -- we can have 16 seqids at most. */
Yunhui Cui9b2a3492016-08-18 15:37:57 +0800196#define SEQID_READ 0
Huang Shijiee46ecda2014-02-24 18:37:42 +0800197#define SEQID_WREN 1
198#define SEQID_WRDI 2
199#define SEQID_RDSR 3
200#define SEQID_SE 4
201#define SEQID_CHIP_ERASE 5
202#define SEQID_PP 6
203#define SEQID_RDID 7
204#define SEQID_WRSR 8
205#define SEQID_RDCR 9
206#define SEQID_EN4B 10
207#define SEQID_BRWR 11
208
Han Xu49bd7062015-08-04 10:25:22 -0500209#define QUADSPI_MIN_IOMAP SZ_4M
210
Huang Shijiee46ecda2014-02-24 18:37:42 +0800211enum fsl_qspi_devtype {
212 FSL_QUADSPI_VYBRID,
213 FSL_QUADSPI_IMX6SX,
Frank Lid371cbf2015-08-04 10:25:35 -0500214 FSL_QUADSPI_IMX7D,
Frank Li74a081d12015-08-04 10:25:47 -0500215 FSL_QUADSPI_IMX6UL,
Yao Yuane8c034b2016-01-26 15:23:56 +0800216 FSL_QUADSPI_LS1021A,
Huang Shijiee46ecda2014-02-24 18:37:42 +0800217};
218
219struct fsl_qspi_devtype_data {
220 enum fsl_qspi_devtype devtype;
221 int rxfifo;
222 int txfifo;
Allen Xu4e898ce2015-01-14 00:28:56 +0800223 int ahb_buf_size;
Han Xu80d37722015-08-04 10:25:29 -0500224 int driver_data;
Huang Shijiee46ecda2014-02-24 18:37:42 +0800225};
226
LABBE Corentindfce0cd2016-08-16 14:56:38 +0200227static const struct fsl_qspi_devtype_data vybrid_data = {
Huang Shijiee46ecda2014-02-24 18:37:42 +0800228 .devtype = FSL_QUADSPI_VYBRID,
229 .rxfifo = 128,
Allen Xu4e898ce2015-01-14 00:28:56 +0800230 .txfifo = 64,
Han Xu80d37722015-08-04 10:25:29 -0500231 .ahb_buf_size = 1024,
232 .driver_data = QUADSPI_QUIRK_SWAP_ENDIAN,
Huang Shijiee46ecda2014-02-24 18:37:42 +0800233};
234
LABBE Corentindfce0cd2016-08-16 14:56:38 +0200235static const struct fsl_qspi_devtype_data imx6sx_data = {
Huang Shijiee46ecda2014-02-24 18:37:42 +0800236 .devtype = FSL_QUADSPI_IMX6SX,
237 .rxfifo = 128,
Allen Xu4e898ce2015-01-14 00:28:56 +0800238 .txfifo = 512,
Han Xu80d37722015-08-04 10:25:29 -0500239 .ahb_buf_size = 1024,
Frank Li5cc66cb2015-08-04 10:26:04 -0500240 .driver_data = QUADSPI_QUIRK_4X_INT_CLK
241 | QUADSPI_QUIRK_TKT245618,
Huang Shijiee46ecda2014-02-24 18:37:42 +0800242};
243
LABBE Corentindfce0cd2016-08-16 14:56:38 +0200244static const struct fsl_qspi_devtype_data imx7d_data = {
Frank Lid371cbf2015-08-04 10:25:35 -0500245 .devtype = FSL_QUADSPI_IMX7D,
246 .rxfifo = 512,
247 .txfifo = 512,
248 .ahb_buf_size = 1024,
249 .driver_data = QUADSPI_QUIRK_TKT253890
250 | QUADSPI_QUIRK_4X_INT_CLK,
251};
252
LABBE Corentindfce0cd2016-08-16 14:56:38 +0200253static const struct fsl_qspi_devtype_data imx6ul_data = {
Frank Li74a081d12015-08-04 10:25:47 -0500254 .devtype = FSL_QUADSPI_IMX6UL,
255 .rxfifo = 128,
256 .txfifo = 512,
257 .ahb_buf_size = 1024,
258 .driver_data = QUADSPI_QUIRK_TKT253890
259 | QUADSPI_QUIRK_4X_INT_CLK,
260};
261
Yao Yuane8c034b2016-01-26 15:23:56 +0800262static struct fsl_qspi_devtype_data ls1021a_data = {
263 .devtype = FSL_QUADSPI_LS1021A,
264 .rxfifo = 128,
265 .txfifo = 64,
266 .ahb_buf_size = 1024,
267 .driver_data = 0,
268};
269
Huang Shijiee46ecda2014-02-24 18:37:42 +0800270#define FSL_QSPI_MAX_CHIP 4
271struct fsl_qspi {
Huang Shijiee46ecda2014-02-24 18:37:42 +0800272 struct spi_nor nor[FSL_QSPI_MAX_CHIP];
273 void __iomem *iobase;
Han Xu49bd7062015-08-04 10:25:22 -0500274 void __iomem *ahb_addr;
Huang Shijiee46ecda2014-02-24 18:37:42 +0800275 u32 memmap_phy;
Han Xu49bd7062015-08-04 10:25:22 -0500276 u32 memmap_offs;
277 u32 memmap_len;
Huang Shijiee46ecda2014-02-24 18:37:42 +0800278 struct clk *clk, *clk_en;
279 struct device *dev;
280 struct completion c;
Brian Norrisc98f71d2015-11-16 10:45:30 -0800281 const struct fsl_qspi_devtype_data *devtype_data;
Huang Shijiee46ecda2014-02-24 18:37:42 +0800282 u32 nor_size;
283 u32 nor_num;
284 u32 clk_rate;
285 unsigned int chip_base_addr; /* We may support two chips. */
Fabio Estevamcfe4af32015-01-13 20:14:15 -0200286 bool has_second_chip;
Yao Yuan20128502016-01-26 15:23:55 +0800287 bool big_endian;
Han Xu392d39c2015-05-13 14:40:57 -0500288 struct mutex lock;
Frank Li5cc66cb2015-08-04 10:26:04 -0500289 struct pm_qos_request pm_qos_req;
Huang Shijiee46ecda2014-02-24 18:37:42 +0800290};
291
Han Xu80d37722015-08-04 10:25:29 -0500292static inline int needs_swap_endian(struct fsl_qspi *q)
Huang Shijiee46ecda2014-02-24 18:37:42 +0800293{
Han Xu80d37722015-08-04 10:25:29 -0500294 return q->devtype_data->driver_data & QUADSPI_QUIRK_SWAP_ENDIAN;
Huang Shijiee46ecda2014-02-24 18:37:42 +0800295}
296
Han Xu80d37722015-08-04 10:25:29 -0500297static inline int needs_4x_clock(struct fsl_qspi *q)
Huang Shijiee46ecda2014-02-24 18:37:42 +0800298{
Han Xu80d37722015-08-04 10:25:29 -0500299 return q->devtype_data->driver_data & QUADSPI_QUIRK_4X_INT_CLK;
Huang Shijiee46ecda2014-02-24 18:37:42 +0800300}
301
Frank Lid371cbf2015-08-04 10:25:35 -0500302static inline int needs_fill_txfifo(struct fsl_qspi *q)
303{
304 return q->devtype_data->driver_data & QUADSPI_QUIRK_TKT253890;
305}
306
Frank Li5cc66cb2015-08-04 10:26:04 -0500307static inline int needs_wakeup_wait_mode(struct fsl_qspi *q)
308{
309 return q->devtype_data->driver_data & QUADSPI_QUIRK_TKT245618;
310}
311
Huang Shijiee46ecda2014-02-24 18:37:42 +0800312/*
Yao Yuan20128502016-01-26 15:23:55 +0800313 * R/W functions for big- or little-endian registers:
314 * The qSPI controller's endian is independent of the CPU core's endian.
315 * So far, although the CPU core is little-endian but the qSPI have two
316 * versions for big-endian and little-endian.
317 */
318static void qspi_writel(struct fsl_qspi *q, u32 val, void __iomem *addr)
319{
320 if (q->big_endian)
321 iowrite32be(val, addr);
322 else
323 iowrite32(val, addr);
324}
325
326static u32 qspi_readl(struct fsl_qspi *q, void __iomem *addr)
327{
328 if (q->big_endian)
329 return ioread32be(addr);
330 else
331 return ioread32(addr);
332}
333
334/*
Huang Shijiee46ecda2014-02-24 18:37:42 +0800335 * An IC bug makes us to re-arrange the 32-bit data.
336 * The following chips, such as IMX6SLX, have fixed this bug.
337 */
338static inline u32 fsl_qspi_endian_xchg(struct fsl_qspi *q, u32 a)
339{
Han Xu80d37722015-08-04 10:25:29 -0500340 return needs_swap_endian(q) ? __swab32(a) : a;
Huang Shijiee46ecda2014-02-24 18:37:42 +0800341}
342
343static inline void fsl_qspi_unlock_lut(struct fsl_qspi *q)
344{
Yao Yuan20128502016-01-26 15:23:55 +0800345 qspi_writel(q, QUADSPI_LUTKEY_VALUE, q->iobase + QUADSPI_LUTKEY);
346 qspi_writel(q, QUADSPI_LCKER_UNLOCK, q->iobase + QUADSPI_LCKCR);
Huang Shijiee46ecda2014-02-24 18:37:42 +0800347}
348
349static inline void fsl_qspi_lock_lut(struct fsl_qspi *q)
350{
Yao Yuan20128502016-01-26 15:23:55 +0800351 qspi_writel(q, QUADSPI_LUTKEY_VALUE, q->iobase + QUADSPI_LUTKEY);
352 qspi_writel(q, QUADSPI_LCKER_LOCK, q->iobase + QUADSPI_LCKCR);
Huang Shijiee46ecda2014-02-24 18:37:42 +0800353}
354
355static irqreturn_t fsl_qspi_irq_handler(int irq, void *dev_id)
356{
357 struct fsl_qspi *q = dev_id;
358 u32 reg;
359
360 /* clear interrupt */
Yao Yuan20128502016-01-26 15:23:55 +0800361 reg = qspi_readl(q, q->iobase + QUADSPI_FR);
362 qspi_writel(q, reg, q->iobase + QUADSPI_FR);
Huang Shijiee46ecda2014-02-24 18:37:42 +0800363
364 if (reg & QUADSPI_FR_TFF_MASK)
365 complete(&q->c);
366
367 dev_dbg(q->dev, "QUADSPI_FR : 0x%.8x:0x%.8x\n", q->chip_base_addr, reg);
368 return IRQ_HANDLED;
369}
370
371static void fsl_qspi_init_lut(struct fsl_qspi *q)
372{
Brian Norrisa965d042014-04-10 15:49:38 -0700373 void __iomem *base = q->iobase;
Huang Shijiee46ecda2014-02-24 18:37:42 +0800374 int rxfifo = q->devtype_data->rxfifo;
375 u32 lut_base;
Huang Shijiee46ecda2014-02-24 18:37:42 +0800376 int i;
377
Yunhui Cuidfdb3eb2016-08-18 15:37:56 +0800378 struct spi_nor *nor = &q->nor[0];
379 u8 addrlen = (nor->addr_width == 3) ? ADDR24BIT : ADDR32BIT;
380 u8 read_op = nor->read_opcode;
381 u8 read_dm = nor->read_dummy;
382
Huang Shijiee46ecda2014-02-24 18:37:42 +0800383 fsl_qspi_unlock_lut(q);
384
385 /* Clear all the LUT table */
386 for (i = 0; i < QUADSPI_LUT_NUM; i++)
Yao Yuan20128502016-01-26 15:23:55 +0800387 qspi_writel(q, 0, base + QUADSPI_LUT_BASE + i * 4);
Huang Shijiee46ecda2014-02-24 18:37:42 +0800388
Yunhui Cui9b2a3492016-08-18 15:37:57 +0800389 /* Read */
390 lut_base = SEQID_READ * 4;
Huang Shijiee46ecda2014-02-24 18:37:42 +0800391
Yunhui Cuidfdb3eb2016-08-18 15:37:56 +0800392 qspi_writel(q, LUT0(CMD, PAD1, read_op) | LUT1(ADDR, PAD1, addrlen),
Huang Shijiee46ecda2014-02-24 18:37:42 +0800393 base + QUADSPI_LUT(lut_base));
Yunhui Cuidfdb3eb2016-08-18 15:37:56 +0800394 qspi_writel(q, LUT0(DUMMY, PAD1, read_dm) |
395 LUT1(FSL_READ, PAD4, rxfifo),
Huang Shijiee46ecda2014-02-24 18:37:42 +0800396 base + QUADSPI_LUT(lut_base + 1));
397
398 /* Write enable */
399 lut_base = SEQID_WREN * 4;
Yao Yuan20128502016-01-26 15:23:55 +0800400 qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_WREN),
401 base + QUADSPI_LUT(lut_base));
Huang Shijiee46ecda2014-02-24 18:37:42 +0800402
403 /* Page Program */
404 lut_base = SEQID_PP * 4;
405
Yunhui Cuidfdb3eb2016-08-18 15:37:56 +0800406 qspi_writel(q, LUT0(CMD, PAD1, nor->program_opcode) |
407 LUT1(ADDR, PAD1, addrlen),
Huang Shijiee46ecda2014-02-24 18:37:42 +0800408 base + QUADSPI_LUT(lut_base));
Yao Yuan20128502016-01-26 15:23:55 +0800409 qspi_writel(q, LUT0(FSL_WRITE, PAD1, 0),
410 base + QUADSPI_LUT(lut_base + 1));
Huang Shijiee46ecda2014-02-24 18:37:42 +0800411
412 /* Read Status */
413 lut_base = SEQID_RDSR * 4;
Yao Yuan20128502016-01-26 15:23:55 +0800414 qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_RDSR) |
415 LUT1(FSL_READ, PAD1, 0x1),
Huang Shijiee46ecda2014-02-24 18:37:42 +0800416 base + QUADSPI_LUT(lut_base));
417
418 /* Erase a sector */
419 lut_base = SEQID_SE * 4;
420
Yunhui Cuidfdb3eb2016-08-18 15:37:56 +0800421 qspi_writel(q, LUT0(CMD, PAD1, nor->erase_opcode) |
422 LUT1(ADDR, PAD1, addrlen),
Huang Shijiee46ecda2014-02-24 18:37:42 +0800423 base + QUADSPI_LUT(lut_base));
424
425 /* Erase the whole chip */
426 lut_base = SEQID_CHIP_ERASE * 4;
Yao Yuan20128502016-01-26 15:23:55 +0800427 qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_CHIP_ERASE),
Huang Shijiee46ecda2014-02-24 18:37:42 +0800428 base + QUADSPI_LUT(lut_base));
429
430 /* READ ID */
431 lut_base = SEQID_RDID * 4;
Yao Yuan20128502016-01-26 15:23:55 +0800432 qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_RDID) |
433 LUT1(FSL_READ, PAD1, 0x8),
Huang Shijiee46ecda2014-02-24 18:37:42 +0800434 base + QUADSPI_LUT(lut_base));
435
436 /* Write Register */
437 lut_base = SEQID_WRSR * 4;
Yao Yuan20128502016-01-26 15:23:55 +0800438 qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_WRSR) |
439 LUT1(FSL_WRITE, PAD1, 0x2),
Huang Shijiee46ecda2014-02-24 18:37:42 +0800440 base + QUADSPI_LUT(lut_base));
441
442 /* Read Configuration Register */
443 lut_base = SEQID_RDCR * 4;
Yao Yuan20128502016-01-26 15:23:55 +0800444 qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_RDCR) |
445 LUT1(FSL_READ, PAD1, 0x1),
Huang Shijiee46ecda2014-02-24 18:37:42 +0800446 base + QUADSPI_LUT(lut_base));
447
448 /* Write disable */
449 lut_base = SEQID_WRDI * 4;
Yao Yuan20128502016-01-26 15:23:55 +0800450 qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_WRDI),
451 base + QUADSPI_LUT(lut_base));
Huang Shijiee46ecda2014-02-24 18:37:42 +0800452
453 /* Enter 4 Byte Mode (Micron) */
454 lut_base = SEQID_EN4B * 4;
Yao Yuan20128502016-01-26 15:23:55 +0800455 qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_EN4B),
456 base + QUADSPI_LUT(lut_base));
Huang Shijiee46ecda2014-02-24 18:37:42 +0800457
458 /* Enter 4 Byte Mode (Spansion) */
459 lut_base = SEQID_BRWR * 4;
Yao Yuan20128502016-01-26 15:23:55 +0800460 qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_BRWR),
461 base + QUADSPI_LUT(lut_base));
Huang Shijiee46ecda2014-02-24 18:37:42 +0800462
463 fsl_qspi_lock_lut(q);
464}
465
466/* Get the SEQID for the command */
467static int fsl_qspi_get_seqid(struct fsl_qspi *q, u8 cmd)
468{
469 switch (cmd) {
Brian Norris58b89a12014-04-08 19:16:49 -0700470 case SPINOR_OP_READ_1_1_4:
Yunhui Cui9b2a3492016-08-18 15:37:57 +0800471 return SEQID_READ;
Brian Norrisb02e7f32014-04-08 18:15:31 -0700472 case SPINOR_OP_WREN:
Huang Shijiee46ecda2014-02-24 18:37:42 +0800473 return SEQID_WREN;
Brian Norrisb02e7f32014-04-08 18:15:31 -0700474 case SPINOR_OP_WRDI:
Huang Shijiee46ecda2014-02-24 18:37:42 +0800475 return SEQID_WRDI;
Brian Norrisb02e7f32014-04-08 18:15:31 -0700476 case SPINOR_OP_RDSR:
Huang Shijiee46ecda2014-02-24 18:37:42 +0800477 return SEQID_RDSR;
Brian Norrisb02e7f32014-04-08 18:15:31 -0700478 case SPINOR_OP_SE:
Huang Shijiee46ecda2014-02-24 18:37:42 +0800479 return SEQID_SE;
Brian Norrisb02e7f32014-04-08 18:15:31 -0700480 case SPINOR_OP_CHIP_ERASE:
Huang Shijiee46ecda2014-02-24 18:37:42 +0800481 return SEQID_CHIP_ERASE;
Brian Norrisb02e7f32014-04-08 18:15:31 -0700482 case SPINOR_OP_PP:
Huang Shijiee46ecda2014-02-24 18:37:42 +0800483 return SEQID_PP;
Brian Norrisb02e7f32014-04-08 18:15:31 -0700484 case SPINOR_OP_RDID:
Huang Shijiee46ecda2014-02-24 18:37:42 +0800485 return SEQID_RDID;
Brian Norrisb02e7f32014-04-08 18:15:31 -0700486 case SPINOR_OP_WRSR:
Huang Shijiee46ecda2014-02-24 18:37:42 +0800487 return SEQID_WRSR;
Brian Norrisb02e7f32014-04-08 18:15:31 -0700488 case SPINOR_OP_RDCR:
Huang Shijiee46ecda2014-02-24 18:37:42 +0800489 return SEQID_RDCR;
Brian Norrisb02e7f32014-04-08 18:15:31 -0700490 case SPINOR_OP_EN4B:
Huang Shijiee46ecda2014-02-24 18:37:42 +0800491 return SEQID_EN4B;
Brian Norrisb02e7f32014-04-08 18:15:31 -0700492 case SPINOR_OP_BRWR:
Huang Shijiee46ecda2014-02-24 18:37:42 +0800493 return SEQID_BRWR;
494 default:
Frank Li788a6cd2015-08-04 10:26:16 -0500495 if (cmd == q->nor[0].erase_opcode)
496 return SEQID_SE;
Huang Shijiee46ecda2014-02-24 18:37:42 +0800497 dev_err(q->dev, "Unsupported cmd 0x%.2x\n", cmd);
498 break;
499 }
500 return -EINVAL;
501}
502
503static int
504fsl_qspi_runcmd(struct fsl_qspi *q, u8 cmd, unsigned int addr, int len)
505{
Brian Norrisa965d042014-04-10 15:49:38 -0700506 void __iomem *base = q->iobase;
Huang Shijiee46ecda2014-02-24 18:37:42 +0800507 int seqid;
508 u32 reg, reg2;
509 int err;
510
511 init_completion(&q->c);
512 dev_dbg(q->dev, "to 0x%.8x:0x%.8x, len:%d, cmd:%.2x\n",
513 q->chip_base_addr, addr, len, cmd);
514
515 /* save the reg */
Yao Yuan20128502016-01-26 15:23:55 +0800516 reg = qspi_readl(q, base + QUADSPI_MCR);
Huang Shijiee46ecda2014-02-24 18:37:42 +0800517
Yao Yuan20128502016-01-26 15:23:55 +0800518 qspi_writel(q, q->memmap_phy + q->chip_base_addr + addr,
519 base + QUADSPI_SFAR);
520 qspi_writel(q, QUADSPI_RBCT_WMRK_MASK | QUADSPI_RBCT_RXBRD_USEIPS,
Huang Shijiee46ecda2014-02-24 18:37:42 +0800521 base + QUADSPI_RBCT);
Yao Yuan20128502016-01-26 15:23:55 +0800522 qspi_writel(q, reg | QUADSPI_MCR_CLR_RXF_MASK, base + QUADSPI_MCR);
Huang Shijiee46ecda2014-02-24 18:37:42 +0800523
524 do {
Yao Yuan20128502016-01-26 15:23:55 +0800525 reg2 = qspi_readl(q, base + QUADSPI_SR);
Huang Shijiee46ecda2014-02-24 18:37:42 +0800526 if (reg2 & (QUADSPI_SR_IP_ACC_MASK | QUADSPI_SR_AHB_ACC_MASK)) {
527 udelay(1);
528 dev_dbg(q->dev, "The controller is busy, 0x%x\n", reg2);
529 continue;
530 }
531 break;
532 } while (1);
533
534 /* trigger the LUT now */
535 seqid = fsl_qspi_get_seqid(q, cmd);
Yao Yuan20128502016-01-26 15:23:55 +0800536 qspi_writel(q, (seqid << QUADSPI_IPCR_SEQID_SHIFT) | len,
537 base + QUADSPI_IPCR);
Huang Shijiee46ecda2014-02-24 18:37:42 +0800538
539 /* Wait for the interrupt. */
Nicholas Mc Guire219a8d12015-02-01 06:15:46 -0500540 if (!wait_for_completion_timeout(&q->c, msecs_to_jiffies(1000))) {
Huang Shijiee46ecda2014-02-24 18:37:42 +0800541 dev_err(q->dev,
542 "cmd 0x%.2x timeout, addr@%.8x, FR:0x%.8x, SR:0x%.8x\n",
Yao Yuan20128502016-01-26 15:23:55 +0800543 cmd, addr, qspi_readl(q, base + QUADSPI_FR),
544 qspi_readl(q, base + QUADSPI_SR));
Huang Shijiee46ecda2014-02-24 18:37:42 +0800545 err = -ETIMEDOUT;
546 } else {
547 err = 0;
548 }
549
550 /* restore the MCR */
Yao Yuan20128502016-01-26 15:23:55 +0800551 qspi_writel(q, reg, base + QUADSPI_MCR);
Huang Shijiee46ecda2014-02-24 18:37:42 +0800552
553 return err;
554}
555
556/* Read out the data from the QUADSPI_RBDR buffer registers. */
557static void fsl_qspi_read_data(struct fsl_qspi *q, int len, u8 *rxbuf)
558{
559 u32 tmp;
560 int i = 0;
561
562 while (len > 0) {
Yao Yuan20128502016-01-26 15:23:55 +0800563 tmp = qspi_readl(q, q->iobase + QUADSPI_RBDR + i * 4);
Huang Shijiee46ecda2014-02-24 18:37:42 +0800564 tmp = fsl_qspi_endian_xchg(q, tmp);
565 dev_dbg(q->dev, "chip addr:0x%.8x, rcv:0x%.8x\n",
566 q->chip_base_addr, tmp);
567
568 if (len >= 4) {
569 *((u32 *)rxbuf) = tmp;
570 rxbuf += 4;
571 } else {
572 memcpy(rxbuf, &tmp, len);
573 break;
574 }
575
576 len -= 4;
577 i++;
578 }
579}
580
581/*
582 * If we have changed the content of the flash by writing or erasing,
583 * we need to invalidate the AHB buffer. If we do not do so, we may read out
584 * the wrong data. The spec tells us reset the AHB domain and Serial Flash
585 * domain at the same time.
586 */
587static inline void fsl_qspi_invalid(struct fsl_qspi *q)
588{
589 u32 reg;
590
Yao Yuan20128502016-01-26 15:23:55 +0800591 reg = qspi_readl(q, q->iobase + QUADSPI_MCR);
Huang Shijiee46ecda2014-02-24 18:37:42 +0800592 reg |= QUADSPI_MCR_SWRSTHD_MASK | QUADSPI_MCR_SWRSTSD_MASK;
Yao Yuan20128502016-01-26 15:23:55 +0800593 qspi_writel(q, reg, q->iobase + QUADSPI_MCR);
Huang Shijiee46ecda2014-02-24 18:37:42 +0800594
595 /*
596 * The minimum delay : 1 AHB + 2 SFCK clocks.
597 * Delay 1 us is enough.
598 */
599 udelay(1);
600
601 reg &= ~(QUADSPI_MCR_SWRSTHD_MASK | QUADSPI_MCR_SWRSTSD_MASK);
Yao Yuan20128502016-01-26 15:23:55 +0800602 qspi_writel(q, reg, q->iobase + QUADSPI_MCR);
Huang Shijiee46ecda2014-02-24 18:37:42 +0800603}
604
Michal Suchanekfc0d7e52016-05-05 17:31:49 -0700605static ssize_t fsl_qspi_nor_write(struct fsl_qspi *q, struct spi_nor *nor,
Huang Shijiee46ecda2014-02-24 18:37:42 +0800606 u8 opcode, unsigned int to, u32 *txbuf,
Michal Suchanek2dd087b2016-05-05 17:31:53 -0700607 unsigned count)
Huang Shijiee46ecda2014-02-24 18:37:42 +0800608{
609 int ret, i, j;
610 u32 tmp;
611
612 dev_dbg(q->dev, "to 0x%.8x:0x%.8x, len : %d\n",
613 q->chip_base_addr, to, count);
614
615 /* clear the TX FIFO. */
Yao Yuan20128502016-01-26 15:23:55 +0800616 tmp = qspi_readl(q, q->iobase + QUADSPI_MCR);
617 qspi_writel(q, tmp | QUADSPI_MCR_CLR_TXF_MASK, q->iobase + QUADSPI_MCR);
Huang Shijiee46ecda2014-02-24 18:37:42 +0800618
619 /* fill the TX data to the FIFO */
620 for (j = 0, i = ((count + 3) / 4); j < i; j++) {
621 tmp = fsl_qspi_endian_xchg(q, *txbuf);
Yao Yuan20128502016-01-26 15:23:55 +0800622 qspi_writel(q, tmp, q->iobase + QUADSPI_TBDR);
Huang Shijiee46ecda2014-02-24 18:37:42 +0800623 txbuf++;
624 }
625
Frank Lid371cbf2015-08-04 10:25:35 -0500626 /* fill the TXFIFO upto 16 bytes for i.MX7d */
627 if (needs_fill_txfifo(q))
628 for (; i < 4; i++)
Yao Yuan20128502016-01-26 15:23:55 +0800629 qspi_writel(q, tmp, q->iobase + QUADSPI_TBDR);
Frank Lid371cbf2015-08-04 10:25:35 -0500630
Huang Shijiee46ecda2014-02-24 18:37:42 +0800631 /* Trigger it */
632 ret = fsl_qspi_runcmd(q, opcode, to, count);
633
Michal Suchanek2dd087b2016-05-05 17:31:53 -0700634 if (ret == 0)
Michal Suchanekfc0d7e52016-05-05 17:31:49 -0700635 return count;
Huang Shijiee46ecda2014-02-24 18:37:42 +0800636
637 return ret;
638}
639
640static void fsl_qspi_set_map_addr(struct fsl_qspi *q)
641{
642 int nor_size = q->nor_size;
643 void __iomem *base = q->iobase;
644
Yao Yuan20128502016-01-26 15:23:55 +0800645 qspi_writel(q, nor_size + q->memmap_phy, base + QUADSPI_SFA1AD);
646 qspi_writel(q, nor_size * 2 + q->memmap_phy, base + QUADSPI_SFA2AD);
647 qspi_writel(q, nor_size * 3 + q->memmap_phy, base + QUADSPI_SFB1AD);
648 qspi_writel(q, nor_size * 4 + q->memmap_phy, base + QUADSPI_SFB2AD);
Huang Shijiee46ecda2014-02-24 18:37:42 +0800649}
650
651/*
652 * There are two different ways to read out the data from the flash:
653 * the "IP Command Read" and the "AHB Command Read".
654 *
655 * The IC guy suggests we use the "AHB Command Read" which is faster
656 * then the "IP Command Read". (What's more is that there is a bug in
657 * the "IP Command Read" in the Vybrid.)
658 *
659 * After we set up the registers for the "AHB Command Read", we can use
660 * the memcpy to read the data directly. A "missed" access to the buffer
661 * causes the controller to clear the buffer, and use the sequence pointed
662 * by the QUADSPI_BFGENCR[SEQID] to initiate a read from the flash.
663 */
664static void fsl_qspi_init_abh_read(struct fsl_qspi *q)
665{
666 void __iomem *base = q->iobase;
667 int seqid;
668
669 /* AHB configuration for access buffer 0/1/2 .*/
Yao Yuan20128502016-01-26 15:23:55 +0800670 qspi_writel(q, QUADSPI_BUFXCR_INVALID_MSTRID, base + QUADSPI_BUF0CR);
671 qspi_writel(q, QUADSPI_BUFXCR_INVALID_MSTRID, base + QUADSPI_BUF1CR);
672 qspi_writel(q, QUADSPI_BUFXCR_INVALID_MSTRID, base + QUADSPI_BUF2CR);
Allen Xu4e898ce2015-01-14 00:28:56 +0800673 /*
674 * Set ADATSZ with the maximum AHB buffer size to improve the
675 * read performance.
676 */
Yao Yuan20128502016-01-26 15:23:55 +0800677 qspi_writel(q, QUADSPI_BUF3CR_ALLMST_MASK |
678 ((q->devtype_data->ahb_buf_size / 8)
679 << QUADSPI_BUF3CR_ADATSZ_SHIFT),
680 base + QUADSPI_BUF3CR);
Huang Shijiee46ecda2014-02-24 18:37:42 +0800681
682 /* We only use the buffer3 */
Yao Yuan20128502016-01-26 15:23:55 +0800683 qspi_writel(q, 0, base + QUADSPI_BUF0IND);
684 qspi_writel(q, 0, base + QUADSPI_BUF1IND);
685 qspi_writel(q, 0, base + QUADSPI_BUF2IND);
Huang Shijiee46ecda2014-02-24 18:37:42 +0800686
687 /* Set the default lut sequence for AHB Read. */
688 seqid = fsl_qspi_get_seqid(q, q->nor[0].read_opcode);
Yao Yuan20128502016-01-26 15:23:55 +0800689 qspi_writel(q, seqid << QUADSPI_BFGENCR_SEQID_SHIFT,
Huang Shijiee46ecda2014-02-24 18:37:42 +0800690 q->iobase + QUADSPI_BFGENCR);
691}
692
Allen Xucacbef42015-08-04 10:25:58 -0500693/* This function was used to prepare and enable QSPI clock */
694static int fsl_qspi_clk_prep_enable(struct fsl_qspi *q)
695{
696 int ret;
697
698 ret = clk_prepare_enable(q->clk_en);
699 if (ret)
700 return ret;
701
702 ret = clk_prepare_enable(q->clk);
703 if (ret) {
704 clk_disable_unprepare(q->clk_en);
705 return ret;
706 }
707
Frank Li5cc66cb2015-08-04 10:26:04 -0500708 if (needs_wakeup_wait_mode(q))
709 pm_qos_add_request(&q->pm_qos_req, PM_QOS_CPU_DMA_LATENCY, 0);
710
Allen Xucacbef42015-08-04 10:25:58 -0500711 return 0;
712}
713
714/* This function was used to disable and unprepare QSPI clock */
715static void fsl_qspi_clk_disable_unprep(struct fsl_qspi *q)
716{
Frank Li5cc66cb2015-08-04 10:26:04 -0500717 if (needs_wakeup_wait_mode(q))
718 pm_qos_remove_request(&q->pm_qos_req);
719
Allen Xucacbef42015-08-04 10:25:58 -0500720 clk_disable_unprepare(q->clk);
721 clk_disable_unprepare(q->clk_en);
722
723}
724
Huang Shijiee46ecda2014-02-24 18:37:42 +0800725/* We use this function to do some basic init for spi_nor_scan(). */
726static int fsl_qspi_nor_setup(struct fsl_qspi *q)
727{
728 void __iomem *base = q->iobase;
729 u32 reg;
730 int ret;
731
Allen Xucacbef42015-08-04 10:25:58 -0500732 /* disable and unprepare clock to avoid glitch pass to controller */
733 fsl_qspi_clk_disable_unprep(q);
734
735 /* the default frequency, we will change it in the future. */
Huang Shijiee46ecda2014-02-24 18:37:42 +0800736 ret = clk_set_rate(q->clk, 66000000);
737 if (ret)
738 return ret;
739
Allen Xucacbef42015-08-04 10:25:58 -0500740 ret = fsl_qspi_clk_prep_enable(q);
741 if (ret)
742 return ret;
743
Frank Li8b8319c2015-08-04 10:26:10 -0500744 /* Reset the module */
Yao Yuan20128502016-01-26 15:23:55 +0800745 qspi_writel(q, QUADSPI_MCR_SWRSTSD_MASK | QUADSPI_MCR_SWRSTHD_MASK,
Frank Li8b8319c2015-08-04 10:26:10 -0500746 base + QUADSPI_MCR);
747 udelay(1);
748
Huang Shijiee46ecda2014-02-24 18:37:42 +0800749 /* Init the LUT table. */
750 fsl_qspi_init_lut(q);
751
752 /* Disable the module */
Yao Yuan20128502016-01-26 15:23:55 +0800753 qspi_writel(q, QUADSPI_MCR_MDIS_MASK | QUADSPI_MCR_RESERVED_MASK,
Huang Shijiee46ecda2014-02-24 18:37:42 +0800754 base + QUADSPI_MCR);
755
Yao Yuan20128502016-01-26 15:23:55 +0800756 reg = qspi_readl(q, base + QUADSPI_SMPR);
757 qspi_writel(q, reg & ~(QUADSPI_SMPR_FSDLY_MASK
Huang Shijiee46ecda2014-02-24 18:37:42 +0800758 | QUADSPI_SMPR_FSPHS_MASK
759 | QUADSPI_SMPR_HSENA_MASK
760 | QUADSPI_SMPR_DDRSMP_MASK), base + QUADSPI_SMPR);
761
762 /* Enable the module */
Yao Yuan20128502016-01-26 15:23:55 +0800763 qspi_writel(q, QUADSPI_MCR_RESERVED_MASK | QUADSPI_MCR_END_CFG_MASK,
Huang Shijiee46ecda2014-02-24 18:37:42 +0800764 base + QUADSPI_MCR);
765
Frank Li8b8319c2015-08-04 10:26:10 -0500766 /* clear all interrupt status */
Yao Yuan20128502016-01-26 15:23:55 +0800767 qspi_writel(q, 0xffffffff, q->iobase + QUADSPI_FR);
Frank Li8b8319c2015-08-04 10:26:10 -0500768
Huang Shijiee46ecda2014-02-24 18:37:42 +0800769 /* enable the interrupt */
Yao Yuan20128502016-01-26 15:23:55 +0800770 qspi_writel(q, QUADSPI_RSER_TFIE, q->iobase + QUADSPI_RSER);
Huang Shijiee46ecda2014-02-24 18:37:42 +0800771
772 return 0;
773}
774
775static int fsl_qspi_nor_setup_last(struct fsl_qspi *q)
776{
777 unsigned long rate = q->clk_rate;
778 int ret;
779
Han Xu80d37722015-08-04 10:25:29 -0500780 if (needs_4x_clock(q))
Huang Shijiee46ecda2014-02-24 18:37:42 +0800781 rate *= 4;
782
Allen Xucacbef42015-08-04 10:25:58 -0500783 /* disable and unprepare clock to avoid glitch pass to controller */
784 fsl_qspi_clk_disable_unprep(q);
785
Huang Shijiee46ecda2014-02-24 18:37:42 +0800786 ret = clk_set_rate(q->clk, rate);
787 if (ret)
788 return ret;
789
Allen Xucacbef42015-08-04 10:25:58 -0500790 ret = fsl_qspi_clk_prep_enable(q);
791 if (ret)
792 return ret;
793
Huang Shijiee46ecda2014-02-24 18:37:42 +0800794 /* Init the LUT table again. */
795 fsl_qspi_init_lut(q);
796
797 /* Init for AHB read */
798 fsl_qspi_init_abh_read(q);
799
800 return 0;
801}
802
Fabian Frederick66610442015-03-16 20:20:28 +0100803static const struct of_device_id fsl_qspi_dt_ids[] = {
Huang Shijiee46ecda2014-02-24 18:37:42 +0800804 { .compatible = "fsl,vf610-qspi", .data = (void *)&vybrid_data, },
805 { .compatible = "fsl,imx6sx-qspi", .data = (void *)&imx6sx_data, },
Frank Lid371cbf2015-08-04 10:25:35 -0500806 { .compatible = "fsl,imx7d-qspi", .data = (void *)&imx7d_data, },
Frank Li74a081d12015-08-04 10:25:47 -0500807 { .compatible = "fsl,imx6ul-qspi", .data = (void *)&imx6ul_data, },
Yao Yuane8c034b2016-01-26 15:23:56 +0800808 { .compatible = "fsl,ls1021a-qspi", .data = (void *)&ls1021a_data, },
Huang Shijiee46ecda2014-02-24 18:37:42 +0800809 { /* sentinel */ }
810};
811MODULE_DEVICE_TABLE(of, fsl_qspi_dt_ids);
812
813static void fsl_qspi_set_base_addr(struct fsl_qspi *q, struct spi_nor *nor)
814{
815 q->chip_base_addr = q->nor_size * (nor - q->nor);
816}
817
818static int fsl_qspi_read_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
819{
820 int ret;
821 struct fsl_qspi *q = nor->priv;
822
823 ret = fsl_qspi_runcmd(q, opcode, 0, len);
824 if (ret)
825 return ret;
826
827 fsl_qspi_read_data(q, len, buf);
828 return 0;
829}
830
Jagan Tekif9f3ce82015-08-19 15:26:44 +0530831static int fsl_qspi_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
Huang Shijiee46ecda2014-02-24 18:37:42 +0800832{
833 struct fsl_qspi *q = nor->priv;
834 int ret;
835
836 if (!buf) {
837 ret = fsl_qspi_runcmd(q, opcode, 0, 1);
838 if (ret)
839 return ret;
840
Brian Norrisb02e7f32014-04-08 18:15:31 -0700841 if (opcode == SPINOR_OP_CHIP_ERASE)
Huang Shijiee46ecda2014-02-24 18:37:42 +0800842 fsl_qspi_invalid(q);
843
844 } else if (len > 0) {
845 ret = fsl_qspi_nor_write(q, nor, opcode, 0,
Michal Suchanek2dd087b2016-05-05 17:31:53 -0700846 (u32 *)buf, len);
Michal Suchanekfc0d7e52016-05-05 17:31:49 -0700847 if (ret > 0)
848 return 0;
Huang Shijiee46ecda2014-02-24 18:37:42 +0800849 } else {
850 dev_err(q->dev, "invalid cmd %d\n", opcode);
851 ret = -EINVAL;
852 }
853
854 return ret;
855}
856
Michal Suchanek59451e12016-05-05 17:31:47 -0700857static ssize_t fsl_qspi_write(struct spi_nor *nor, loff_t to,
Michal Suchanek2dd087b2016-05-05 17:31:53 -0700858 size_t len, const u_char *buf)
Huang Shijiee46ecda2014-02-24 18:37:42 +0800859{
860 struct fsl_qspi *q = nor->priv;
Michal Suchanekfc0d7e52016-05-05 17:31:49 -0700861 ssize_t ret = fsl_qspi_nor_write(q, nor, nor->program_opcode, to,
Michal Suchanek2dd087b2016-05-05 17:31:53 -0700862 (u32 *)buf, len);
Huang Shijiee46ecda2014-02-24 18:37:42 +0800863
864 /* invalid the data in the AHB buffer. */
865 fsl_qspi_invalid(q);
Michal Suchanekfc0d7e52016-05-05 17:31:49 -0700866 return ret;
Huang Shijiee46ecda2014-02-24 18:37:42 +0800867}
868
Michal Suchanek59451e12016-05-05 17:31:47 -0700869static ssize_t fsl_qspi_read(struct spi_nor *nor, loff_t from,
Michal Suchanek2dd087b2016-05-05 17:31:53 -0700870 size_t len, u_char *buf)
Huang Shijiee46ecda2014-02-24 18:37:42 +0800871{
872 struct fsl_qspi *q = nor->priv;
873 u8 cmd = nor->read_opcode;
Huang Shijiee46ecda2014-02-24 18:37:42 +0800874
Han Xu49bd7062015-08-04 10:25:22 -0500875 /* if necessary,ioremap buffer before AHB read, */
876 if (!q->ahb_addr) {
877 q->memmap_offs = q->chip_base_addr + from;
878 q->memmap_len = len > QUADSPI_MIN_IOMAP ? len : QUADSPI_MIN_IOMAP;
879
880 q->ahb_addr = ioremap_nocache(
881 q->memmap_phy + q->memmap_offs,
882 q->memmap_len);
883 if (!q->ahb_addr) {
884 dev_err(q->dev, "ioremap failed\n");
885 return -ENOMEM;
886 }
887 /* ioremap if the data requested is out of range */
888 } else if (q->chip_base_addr + from < q->memmap_offs
889 || q->chip_base_addr + from + len >
890 q->memmap_offs + q->memmap_len) {
891 iounmap(q->ahb_addr);
892
893 q->memmap_offs = q->chip_base_addr + from;
894 q->memmap_len = len > QUADSPI_MIN_IOMAP ? len : QUADSPI_MIN_IOMAP;
895 q->ahb_addr = ioremap_nocache(
896 q->memmap_phy + q->memmap_offs,
897 q->memmap_len);
898 if (!q->ahb_addr) {
899 dev_err(q->dev, "ioremap failed\n");
900 return -ENOMEM;
901 }
902 }
903
Brian Norrisa5c603a2015-10-12 13:35:15 -0700904 dev_dbg(q->dev, "cmd [%x],read from %p, len:%zd\n",
Han Xu49bd7062015-08-04 10:25:22 -0500905 cmd, q->ahb_addr + q->chip_base_addr + from - q->memmap_offs,
906 len);
Huang Shijiee46ecda2014-02-24 18:37:42 +0800907
Huang Shijiee46ecda2014-02-24 18:37:42 +0800908 /* Read out the data directly from the AHB buffer.*/
Han Xu49bd7062015-08-04 10:25:22 -0500909 memcpy(buf, q->ahb_addr + q->chip_base_addr + from - q->memmap_offs,
910 len);
Huang Shijiee46ecda2014-02-24 18:37:42 +0800911
Michal Suchanekfc0d7e52016-05-05 17:31:49 -0700912 return len;
Huang Shijiee46ecda2014-02-24 18:37:42 +0800913}
914
915static int fsl_qspi_erase(struct spi_nor *nor, loff_t offs)
916{
917 struct fsl_qspi *q = nor->priv;
918 int ret;
919
920 dev_dbg(nor->dev, "%dKiB at 0x%08x:0x%08x\n",
Brian Norris19763672015-08-13 15:46:05 -0700921 nor->mtd.erasesize / 1024, q->chip_base_addr, (u32)offs);
Huang Shijiee46ecda2014-02-24 18:37:42 +0800922
Huang Shijiee46ecda2014-02-24 18:37:42 +0800923 ret = fsl_qspi_runcmd(q, nor->erase_opcode, offs, 0);
924 if (ret)
925 return ret;
926
927 fsl_qspi_invalid(q);
928 return 0;
929}
930
931static int fsl_qspi_prep(struct spi_nor *nor, enum spi_nor_ops ops)
932{
933 struct fsl_qspi *q = nor->priv;
934 int ret;
935
Han Xu392d39c2015-05-13 14:40:57 -0500936 mutex_lock(&q->lock);
Allen Xucacbef42015-08-04 10:25:58 -0500937
938 ret = fsl_qspi_clk_prep_enable(q);
Huang Shijiee46ecda2014-02-24 18:37:42 +0800939 if (ret)
Han Xu392d39c2015-05-13 14:40:57 -0500940 goto err_mutex;
Huang Shijiee46ecda2014-02-24 18:37:42 +0800941
Huang Shijiee46ecda2014-02-24 18:37:42 +0800942 fsl_qspi_set_base_addr(q, nor);
943 return 0;
Han Xu392d39c2015-05-13 14:40:57 -0500944
Han Xu392d39c2015-05-13 14:40:57 -0500945err_mutex:
946 mutex_unlock(&q->lock);
Han Xu392d39c2015-05-13 14:40:57 -0500947 return ret;
Huang Shijiee46ecda2014-02-24 18:37:42 +0800948}
949
950static void fsl_qspi_unprep(struct spi_nor *nor, enum spi_nor_ops ops)
951{
952 struct fsl_qspi *q = nor->priv;
953
Allen Xucacbef42015-08-04 10:25:58 -0500954 fsl_qspi_clk_disable_unprep(q);
Han Xu392d39c2015-05-13 14:40:57 -0500955 mutex_unlock(&q->lock);
Huang Shijiee46ecda2014-02-24 18:37:42 +0800956}
957
958static int fsl_qspi_probe(struct platform_device *pdev)
959{
960 struct device_node *np = pdev->dev.of_node;
Huang Shijiee46ecda2014-02-24 18:37:42 +0800961 struct device *dev = &pdev->dev;
962 struct fsl_qspi *q;
963 struct resource *res;
964 struct spi_nor *nor;
965 struct mtd_info *mtd;
966 int ret, i = 0;
Huang Shijiee46ecda2014-02-24 18:37:42 +0800967
968 q = devm_kzalloc(dev, sizeof(*q), GFP_KERNEL);
969 if (!q)
970 return -ENOMEM;
971
972 q->nor_num = of_get_child_count(dev->of_node);
973 if (!q->nor_num || q->nor_num > FSL_QSPI_MAX_CHIP)
974 return -ENODEV;
975
Frank Li5cc66cb2015-08-04 10:26:04 -0500976 q->dev = dev;
Brian Norrisc98f71d2015-11-16 10:45:30 -0800977 q->devtype_data = of_device_get_match_data(dev);
978 if (!q->devtype_data)
979 return -ENODEV;
Frank Li5cc66cb2015-08-04 10:26:04 -0500980 platform_set_drvdata(pdev, q);
981
Huang Shijiee46ecda2014-02-24 18:37:42 +0800982 /* find the resources */
983 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "QuadSPI");
984 q->iobase = devm_ioremap_resource(dev, res);
Fabio Estevamb1ab4742015-01-22 22:43:07 -0200985 if (IS_ERR(q->iobase))
986 return PTR_ERR(q->iobase);
Huang Shijiee46ecda2014-02-24 18:37:42 +0800987
Yao Yuan20128502016-01-26 15:23:55 +0800988 q->big_endian = of_property_read_bool(np, "big-endian");
Huang Shijiee46ecda2014-02-24 18:37:42 +0800989 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
990 "QuadSPI-memory");
Han Xu49bd7062015-08-04 10:25:22 -0500991 if (!devm_request_mem_region(dev, res->start, resource_size(res),
992 res->name)) {
993 dev_err(dev, "can't request region for resource %pR\n", res);
994 return -EBUSY;
995 }
Fabio Estevamb1ab4742015-01-22 22:43:07 -0200996
Huang Shijiee46ecda2014-02-24 18:37:42 +0800997 q->memmap_phy = res->start;
998
999 /* find the clocks */
1000 q->clk_en = devm_clk_get(dev, "qspi_en");
Fabio Estevamb1ab4742015-01-22 22:43:07 -02001001 if (IS_ERR(q->clk_en))
1002 return PTR_ERR(q->clk_en);
Huang Shijiee46ecda2014-02-24 18:37:42 +08001003
1004 q->clk = devm_clk_get(dev, "qspi");
Fabio Estevamb1ab4742015-01-22 22:43:07 -02001005 if (IS_ERR(q->clk))
1006 return PTR_ERR(q->clk);
Huang Shijiee46ecda2014-02-24 18:37:42 +08001007
Allen Xucacbef42015-08-04 10:25:58 -05001008 ret = fsl_qspi_clk_prep_enable(q);
Huang Shijiee46ecda2014-02-24 18:37:42 +08001009 if (ret) {
Allen Xucacbef42015-08-04 10:25:58 -05001010 dev_err(dev, "can not enable the clock\n");
Fabio Estevam77adc082014-10-17 17:14:01 -03001011 goto clk_failed;
Huang Shijiee46ecda2014-02-24 18:37:42 +08001012 }
1013
1014 /* find the irq */
1015 ret = platform_get_irq(pdev, 0);
1016 if (ret < 0) {
Fabio Estevamdc6525c2015-02-09 10:07:19 -02001017 dev_err(dev, "failed to get the irq: %d\n", ret);
Huang Shijiee46ecda2014-02-24 18:37:42 +08001018 goto irq_failed;
1019 }
1020
1021 ret = devm_request_irq(dev, ret,
1022 fsl_qspi_irq_handler, 0, pdev->name, q);
1023 if (ret) {
Fabio Estevamdc6525c2015-02-09 10:07:19 -02001024 dev_err(dev, "failed to request irq: %d\n", ret);
Huang Shijiee46ecda2014-02-24 18:37:42 +08001025 goto irq_failed;
1026 }
1027
Huang Shijiee46ecda2014-02-24 18:37:42 +08001028 ret = fsl_qspi_nor_setup(q);
1029 if (ret)
1030 goto irq_failed;
1031
1032 if (of_get_property(np, "fsl,qspi-has-second-chip", NULL))
Fabio Estevamcfe4af32015-01-13 20:14:15 -02001033 q->has_second_chip = true;
Huang Shijiee46ecda2014-02-24 18:37:42 +08001034
Han Xu392d39c2015-05-13 14:40:57 -05001035 mutex_init(&q->lock);
1036
Huang Shijiee46ecda2014-02-24 18:37:42 +08001037 /* iterate the subnodes. */
1038 for_each_available_child_of_node(dev->of_node, np) {
Huang Shijiee46ecda2014-02-24 18:37:42 +08001039 /* skip the holes */
Fabio Estevamcfe4af32015-01-13 20:14:15 -02001040 if (!q->has_second_chip)
Huang Shijiee46ecda2014-02-24 18:37:42 +08001041 i *= 2;
1042
1043 nor = &q->nor[i];
Brian Norris19763672015-08-13 15:46:05 -07001044 mtd = &nor->mtd;
Huang Shijiee46ecda2014-02-24 18:37:42 +08001045
Huang Shijiee46ecda2014-02-24 18:37:42 +08001046 nor->dev = dev;
Brian Norris9c7d7872015-10-30 20:33:24 -07001047 spi_nor_set_flash_node(nor, np);
Huang Shijiee46ecda2014-02-24 18:37:42 +08001048 nor->priv = q;
Huang Shijiee46ecda2014-02-24 18:37:42 +08001049
1050 /* fill the hooks */
1051 nor->read_reg = fsl_qspi_read_reg;
1052 nor->write_reg = fsl_qspi_write_reg;
1053 nor->read = fsl_qspi_read;
1054 nor->write = fsl_qspi_write;
1055 nor->erase = fsl_qspi_erase;
1056
1057 nor->prepare = fsl_qspi_prep;
1058 nor->unprepare = fsl_qspi_unprep;
1059
Huang Shijiee46ecda2014-02-24 18:37:42 +08001060 ret = of_property_read_u32(np, "spi-max-frequency",
1061 &q->clk_rate);
1062 if (ret < 0)
Han Xu392d39c2015-05-13 14:40:57 -05001063 goto mutex_failed;
Huang Shijiee46ecda2014-02-24 18:37:42 +08001064
1065 /* set the chip address for READID */
1066 fsl_qspi_set_base_addr(q, nor);
1067
Brian Norrise747dbe2015-08-13 15:46:02 -07001068 ret = spi_nor_scan(nor, NULL, SPI_NOR_QUAD);
Huang Shijiee46ecda2014-02-24 18:37:42 +08001069 if (ret)
Han Xu392d39c2015-05-13 14:40:57 -05001070 goto mutex_failed;
Huang Shijiee46ecda2014-02-24 18:37:42 +08001071
Brian Norrisdf02c882015-10-30 20:33:26 -07001072 ret = mtd_device_register(mtd, NULL, 0);
Huang Shijiee46ecda2014-02-24 18:37:42 +08001073 if (ret)
Han Xu392d39c2015-05-13 14:40:57 -05001074 goto mutex_failed;
Huang Shijiee46ecda2014-02-24 18:37:42 +08001075
1076 /* Set the correct NOR size now. */
1077 if (q->nor_size == 0) {
1078 q->nor_size = mtd->size;
1079
1080 /* Map the SPI NOR to accessiable address */
1081 fsl_qspi_set_map_addr(q);
1082 }
1083
1084 /*
1085 * The TX FIFO is 64 bytes in the Vybrid, but the Page Program
1086 * may writes 265 bytes per time. The write is working in the
1087 * unit of the TX FIFO, not in the unit of the SPI NOR's page
1088 * size.
1089 *
1090 * So shrink the spi_nor->page_size if it is larger then the
1091 * TX FIFO.
1092 */
1093 if (nor->page_size > q->devtype_data->txfifo)
1094 nor->page_size = q->devtype_data->txfifo;
1095
1096 i++;
1097 }
1098
1099 /* finish the rest init. */
1100 ret = fsl_qspi_nor_setup_last(q);
1101 if (ret)
1102 goto last_init_failed;
1103
Allen Xucacbef42015-08-04 10:25:58 -05001104 fsl_qspi_clk_disable_unprep(q);
Huang Shijiee46ecda2014-02-24 18:37:42 +08001105 return 0;
1106
1107last_init_failed:
Fabio Estevamcfe4af32015-01-13 20:14:15 -02001108 for (i = 0; i < q->nor_num; i++) {
1109 /* skip the holes */
1110 if (!q->has_second_chip)
1111 i *= 2;
Brian Norris19763672015-08-13 15:46:05 -07001112 mtd_device_unregister(&q->nor[i].mtd);
Fabio Estevamcfe4af32015-01-13 20:14:15 -02001113 }
Han Xu392d39c2015-05-13 14:40:57 -05001114mutex_failed:
1115 mutex_destroy(&q->lock);
Huang Shijiee46ecda2014-02-24 18:37:42 +08001116irq_failed:
Allen Xucacbef42015-08-04 10:25:58 -05001117 fsl_qspi_clk_disable_unprep(q);
Fabio Estevam77adc082014-10-17 17:14:01 -03001118clk_failed:
Allen Xucacbef42015-08-04 10:25:58 -05001119 dev_err(dev, "Freescale QuadSPI probe failed\n");
Huang Shijiee46ecda2014-02-24 18:37:42 +08001120 return ret;
1121}
1122
1123static int fsl_qspi_remove(struct platform_device *pdev)
1124{
1125 struct fsl_qspi *q = platform_get_drvdata(pdev);
1126 int i;
1127
Fabio Estevamcfe4af32015-01-13 20:14:15 -02001128 for (i = 0; i < q->nor_num; i++) {
1129 /* skip the holes */
1130 if (!q->has_second_chip)
1131 i *= 2;
Brian Norris19763672015-08-13 15:46:05 -07001132 mtd_device_unregister(&q->nor[i].mtd);
Fabio Estevamcfe4af32015-01-13 20:14:15 -02001133 }
Huang Shijiee46ecda2014-02-24 18:37:42 +08001134
1135 /* disable the hardware */
Yao Yuan20128502016-01-26 15:23:55 +08001136 qspi_writel(q, QUADSPI_MCR_MDIS_MASK, q->iobase + QUADSPI_MCR);
1137 qspi_writel(q, 0x0, q->iobase + QUADSPI_RSER);
Huang Shijiee46ecda2014-02-24 18:37:42 +08001138
Han Xu392d39c2015-05-13 14:40:57 -05001139 mutex_destroy(&q->lock);
Han Xu49bd7062015-08-04 10:25:22 -05001140
1141 if (q->ahb_addr)
1142 iounmap(q->ahb_addr);
1143
Huang Shijiee46ecda2014-02-24 18:37:42 +08001144 return 0;
1145}
1146
Allen Xu45c6a0c2015-01-13 04:56:40 +08001147static int fsl_qspi_suspend(struct platform_device *pdev, pm_message_t state)
1148{
1149 return 0;
1150}
1151
1152static int fsl_qspi_resume(struct platform_device *pdev)
1153{
Allen Xucacbef42015-08-04 10:25:58 -05001154 int ret;
Allen Xu45c6a0c2015-01-13 04:56:40 +08001155 struct fsl_qspi *q = platform_get_drvdata(pdev);
1156
Allen Xucacbef42015-08-04 10:25:58 -05001157 ret = fsl_qspi_clk_prep_enable(q);
1158 if (ret)
1159 return ret;
1160
Allen Xu45c6a0c2015-01-13 04:56:40 +08001161 fsl_qspi_nor_setup(q);
1162 fsl_qspi_set_map_addr(q);
1163 fsl_qspi_nor_setup_last(q);
1164
Allen Xucacbef42015-08-04 10:25:58 -05001165 fsl_qspi_clk_disable_unprep(q);
1166
Allen Xu45c6a0c2015-01-13 04:56:40 +08001167 return 0;
1168}
1169
Huang Shijiee46ecda2014-02-24 18:37:42 +08001170static struct platform_driver fsl_qspi_driver = {
1171 .driver = {
1172 .name = "fsl-quadspi",
1173 .bus = &platform_bus_type,
Huang Shijiee46ecda2014-02-24 18:37:42 +08001174 .of_match_table = fsl_qspi_dt_ids,
1175 },
1176 .probe = fsl_qspi_probe,
1177 .remove = fsl_qspi_remove,
Allen Xu45c6a0c2015-01-13 04:56:40 +08001178 .suspend = fsl_qspi_suspend,
1179 .resume = fsl_qspi_resume,
Huang Shijiee46ecda2014-02-24 18:37:42 +08001180};
1181module_platform_driver(fsl_qspi_driver);
1182
1183MODULE_DESCRIPTION("Freescale QuadSPI Controller Driver");
1184MODULE_AUTHOR("Freescale Semiconductor Inc.");
1185MODULE_LICENSE("GPL v2");