1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015 MediaTek Inc.
4 * Author: Bayi Cheng <bayi.cheng@mediatek.com>
8 #include <linux/delay.h>
9 #include <linux/device.h>
10 #include <linux/init.h>
12 #include <linux/iopoll.h>
13 #include <linux/ioport.h>
14 #include <linux/math64.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
18 #include <linux/of_device.h>
19 #include <linux/platform_device.h>
20 #include <linux/slab.h>
21 #include <linux/mtd/mtd.h>
22 #include <linux/mtd/partitions.h>
23 #include <linux/mtd/spi-nor.h>
25 #define MTK_NOR_CMD_REG 0x00
26 #define MTK_NOR_CNT_REG 0x04
27 #define MTK_NOR_RDSR_REG 0x08
28 #define MTK_NOR_RDATA_REG 0x0c
29 #define MTK_NOR_RADR0_REG 0x10
30 #define MTK_NOR_RADR1_REG 0x14
31 #define MTK_NOR_RADR2_REG 0x18
32 #define MTK_NOR_WDATA_REG 0x1c
33 #define MTK_NOR_PRGDATA0_REG 0x20
34 #define MTK_NOR_PRGDATA1_REG 0x24
35 #define MTK_NOR_PRGDATA2_REG 0x28
36 #define MTK_NOR_PRGDATA3_REG 0x2c
37 #define MTK_NOR_PRGDATA4_REG 0x30
38 #define MTK_NOR_PRGDATA5_REG 0x34
39 #define MTK_NOR_SHREG0_REG 0x38
40 #define MTK_NOR_SHREG1_REG 0x3c
41 #define MTK_NOR_SHREG2_REG 0x40
42 #define MTK_NOR_SHREG3_REG 0x44
43 #define MTK_NOR_SHREG4_REG 0x48
44 #define MTK_NOR_SHREG5_REG 0x4c
45 #define MTK_NOR_SHREG6_REG 0x50
46 #define MTK_NOR_SHREG7_REG 0x54
47 #define MTK_NOR_SHREG8_REG 0x58
48 #define MTK_NOR_SHREG9_REG 0x5c
49 #define MTK_NOR_CFG1_REG 0x60
50 #define MTK_NOR_CFG2_REG 0x64
51 #define MTK_NOR_CFG3_REG 0x68
52 #define MTK_NOR_STATUS0_REG 0x70
53 #define MTK_NOR_STATUS1_REG 0x74
54 #define MTK_NOR_STATUS2_REG 0x78
55 #define MTK_NOR_STATUS3_REG 0x7c
56 #define MTK_NOR_FLHCFG_REG 0x84
57 #define MTK_NOR_TIME_REG 0x94
58 #define MTK_NOR_PP_DATA_REG 0x98
59 #define MTK_NOR_PREBUF_STUS_REG 0x9c
60 #define MTK_NOR_DELSEL0_REG 0xa0
61 #define MTK_NOR_DELSEL1_REG 0xa4
62 #define MTK_NOR_INTRSTUS_REG 0xa8
63 #define MTK_NOR_INTREN_REG 0xac
64 #define MTK_NOR_CHKSUM_CTL_REG 0xb8
65 #define MTK_NOR_CHKSUM_REG 0xbc
66 #define MTK_NOR_CMD2_REG 0xc0
67 #define MTK_NOR_WRPROT_REG 0xc4
68 #define MTK_NOR_RADR3_REG 0xc8
69 #define MTK_NOR_DUAL_REG 0xcc
70 #define MTK_NOR_DELSEL2_REG 0xd0
71 #define MTK_NOR_DELSEL3_REG 0xd4
72 #define MTK_NOR_DELSEL4_REG 0xd8
74 /* commands for mtk nor controller */
75 #define MTK_NOR_READ_CMD 0x0
76 #define MTK_NOR_RDSR_CMD 0x2
77 #define MTK_NOR_PRG_CMD 0x4
78 #define MTK_NOR_WR_CMD 0x10
79 #define MTK_NOR_PIO_WR_CMD 0x90
80 #define MTK_NOR_WRSR_CMD 0x20
81 #define MTK_NOR_PIO_READ_CMD 0x81
82 #define MTK_NOR_WR_BUF_ENABLE 0x1
83 #define MTK_NOR_WR_BUF_DISABLE 0x0
84 #define MTK_NOR_ENABLE_SF_CMD 0x30
85 #define MTK_NOR_DUAD_ADDR_EN 0x8
86 #define MTK_NOR_QUAD_READ_EN 0x4
87 #define MTK_NOR_DUAL_ADDR_EN 0x2
88 #define MTK_NOR_DUAL_READ_EN 0x1
89 #define MTK_NOR_DUAL_DISABLE 0x0
90 #define MTK_NOR_FAST_READ 0x1
92 #define SFLASH_WRBUF_SIZE 128
94 /* Can shift up to 48 bits (6 bytes) of TX/RX */
95 #define MTK_NOR_MAX_RX_TX_SHIFT 6
96 /* can shift up to 56 bits (7 bytes) transfer by MTK_NOR_PRG_CMD */
97 #define MTK_NOR_MAX_SHIFT 7
98 /* nor controller 4-byte address mode enable bit */
99 #define MTK_NOR_4B_ADDR_EN BIT(4)
101 /* Helpers for accessing the program data / shift data registers */
102 #define MTK_NOR_PRG_REG(n) (MTK_NOR_PRGDATA0_REG + 4 * (n))
103 #define MTK_NOR_SHREG(n) (MTK_NOR_SHREG0_REG + 4 * (n))
108 void __iomem
*base
; /* nor flash base address */
113 static void mtk_nor_set_read_mode(struct mtk_nor
*mtk_nor
)
115 struct spi_nor
*nor
= &mtk_nor
->nor
;
117 switch (nor
->read_proto
) {
118 case SNOR_PROTO_1_1_1
:
119 writeb(nor
->read_opcode
, mtk_nor
->base
+
120 MTK_NOR_PRGDATA3_REG
);
121 writeb(MTK_NOR_FAST_READ
, mtk_nor
->base
+
124 case SNOR_PROTO_1_1_2
:
125 writeb(nor
->read_opcode
, mtk_nor
->base
+
126 MTK_NOR_PRGDATA3_REG
);
127 writeb(MTK_NOR_DUAL_READ_EN
, mtk_nor
->base
+
130 case SNOR_PROTO_1_1_4
:
131 writeb(nor
->read_opcode
, mtk_nor
->base
+
132 MTK_NOR_PRGDATA4_REG
);
133 writeb(MTK_NOR_QUAD_READ_EN
, mtk_nor
->base
+
137 writeb(MTK_NOR_DUAL_DISABLE
, mtk_nor
->base
+
143 static int mtk_nor_execute_cmd(struct mtk_nor
*mtk_nor
, u8 cmdval
)
146 u8 val
= cmdval
& 0x1f;
148 writeb(cmdval
, mtk_nor
->base
+ MTK_NOR_CMD_REG
);
149 return readl_poll_timeout(mtk_nor
->base
+ MTK_NOR_CMD_REG
, reg
,
150 !(reg
& val
), 100, 10000);
153 static int mtk_nor_do_tx_rx(struct mtk_nor
*mtk_nor
, u8 op
,
154 u8
*tx
, int txlen
, u8
*rx
, int rxlen
)
156 int len
= 1 + txlen
+ rxlen
;
159 if (len
> MTK_NOR_MAX_SHIFT
)
162 writeb(len
* 8, mtk_nor
->base
+ MTK_NOR_CNT_REG
);
164 /* start at PRGDATA5, go down to PRGDATA0 */
165 idx
= MTK_NOR_MAX_RX_TX_SHIFT
- 1;
168 writeb(op
, mtk_nor
->base
+ MTK_NOR_PRG_REG(idx
));
171 /* program TX data */
172 for (i
= 0; i
< txlen
; i
++, idx
--)
173 writeb(tx
[i
], mtk_nor
->base
+ MTK_NOR_PRG_REG(idx
));
175 /* clear out rest of TX registers */
177 writeb(0, mtk_nor
->base
+ MTK_NOR_PRG_REG(idx
));
181 ret
= mtk_nor_execute_cmd(mtk_nor
, MTK_NOR_PRG_CMD
);
185 /* restart at first RX byte */
188 /* read out RX data */
189 for (i
= 0; i
< rxlen
; i
++, idx
--)
190 rx
[i
] = readb(mtk_nor
->base
+ MTK_NOR_SHREG(idx
));
195 /* Do a WRSR (Write Status Register) command */
196 static int mtk_nor_wr_sr(struct mtk_nor
*mtk_nor
, u8 sr
)
198 writeb(sr
, mtk_nor
->base
+ MTK_NOR_PRGDATA5_REG
);
199 writeb(8, mtk_nor
->base
+ MTK_NOR_CNT_REG
);
200 return mtk_nor_execute_cmd(mtk_nor
, MTK_NOR_WRSR_CMD
);
203 static int mtk_nor_write_buffer_enable(struct mtk_nor
*mtk_nor
)
207 /* the bit0 of MTK_NOR_CFG2_REG is pre-fetch buffer
208 * 0: pre-fetch buffer use for read
209 * 1: pre-fetch buffer use for page program
211 writel(MTK_NOR_WR_BUF_ENABLE
, mtk_nor
->base
+ MTK_NOR_CFG2_REG
);
212 return readb_poll_timeout(mtk_nor
->base
+ MTK_NOR_CFG2_REG
, reg
,
213 0x01 == (reg
& 0x01), 100, 10000);
216 static int mtk_nor_write_buffer_disable(struct mtk_nor
*mtk_nor
)
220 writel(MTK_NOR_WR_BUF_DISABLE
, mtk_nor
->base
+ MTK_NOR_CFG2_REG
);
221 return readb_poll_timeout(mtk_nor
->base
+ MTK_NOR_CFG2_REG
, reg
,
222 MTK_NOR_WR_BUF_DISABLE
== (reg
& 0x1), 100,
226 static void mtk_nor_set_addr_width(struct mtk_nor
*mtk_nor
)
229 struct spi_nor
*nor
= &mtk_nor
->nor
;
231 val
= readb(mtk_nor
->base
+ MTK_NOR_DUAL_REG
);
233 switch (nor
->addr_width
) {
235 val
&= ~MTK_NOR_4B_ADDR_EN
;
238 val
|= MTK_NOR_4B_ADDR_EN
;
241 dev_warn(mtk_nor
->dev
, "Unexpected address width %u.\n",
246 writeb(val
, mtk_nor
->base
+ MTK_NOR_DUAL_REG
);
249 static void mtk_nor_set_addr(struct mtk_nor
*mtk_nor
, u32 addr
)
253 mtk_nor_set_addr_width(mtk_nor
);
255 for (i
= 0; i
< 3; i
++) {
256 writeb(addr
& 0xff, mtk_nor
->base
+ MTK_NOR_RADR0_REG
+ i
* 4);
259 /* Last register is non-contiguous */
260 writeb(addr
& 0xff, mtk_nor
->base
+ MTK_NOR_RADR3_REG
);
263 static ssize_t
mtk_nor_read(struct spi_nor
*nor
, loff_t from
, size_t length
,
267 int addr
= (int)from
;
268 u8
*buf
= (u8
*)buffer
;
269 struct mtk_nor
*mtk_nor
= nor
->priv
;
271 /* set mode for fast read mode ,dual mode or quad mode */
272 mtk_nor_set_read_mode(mtk_nor
);
273 mtk_nor_set_addr(mtk_nor
, addr
);
275 for (i
= 0; i
< length
; i
++) {
276 ret
= mtk_nor_execute_cmd(mtk_nor
, MTK_NOR_PIO_READ_CMD
);
279 buf
[i
] = readb(mtk_nor
->base
+ MTK_NOR_RDATA_REG
);
284 static int mtk_nor_write_single_byte(struct mtk_nor
*mtk_nor
,
285 int addr
, int length
, u8
*data
)
289 mtk_nor_set_addr(mtk_nor
, addr
);
291 for (i
= 0; i
< length
; i
++) {
292 writeb(*data
++, mtk_nor
->base
+ MTK_NOR_WDATA_REG
);
293 ret
= mtk_nor_execute_cmd(mtk_nor
, MTK_NOR_PIO_WR_CMD
);
300 static int mtk_nor_write_buffer(struct mtk_nor
*mtk_nor
, int addr
,
305 mtk_nor_set_addr(mtk_nor
, addr
);
308 for (i
= 0; i
< SFLASH_WRBUF_SIZE
; i
+= 4) {
309 data
= buf
[bufidx
+ 3]<<24 | buf
[bufidx
+ 2]<<16 |
310 buf
[bufidx
+ 1]<<8 | buf
[bufidx
];
312 writel(data
, mtk_nor
->base
+ MTK_NOR_PP_DATA_REG
);
314 return mtk_nor_execute_cmd(mtk_nor
, MTK_NOR_WR_CMD
);
317 static ssize_t
mtk_nor_write(struct spi_nor
*nor
, loff_t to
, size_t len
,
321 struct mtk_nor
*mtk_nor
= nor
->priv
;
324 ret
= mtk_nor_write_buffer_enable(mtk_nor
);
326 dev_warn(mtk_nor
->dev
, "write buffer enable failed!\n");
330 for (i
= 0; i
+ SFLASH_WRBUF_SIZE
<= len
; i
+= SFLASH_WRBUF_SIZE
) {
331 ret
= mtk_nor_write_buffer(mtk_nor
, to
, buf
);
333 dev_err(mtk_nor
->dev
, "write buffer failed!\n");
336 to
+= SFLASH_WRBUF_SIZE
;
337 buf
+= SFLASH_WRBUF_SIZE
;
339 ret
= mtk_nor_write_buffer_disable(mtk_nor
);
341 dev_warn(mtk_nor
->dev
, "write buffer disable failed!\n");
346 ret
= mtk_nor_write_single_byte(mtk_nor
, to
,
347 (int)(len
- i
), (u8
*)buf
);
349 dev_err(mtk_nor
->dev
, "write single byte failed!\n");
357 static int mtk_nor_read_reg(struct spi_nor
*nor
, u8 opcode
, u8
*buf
, int len
)
360 struct mtk_nor
*mtk_nor
= nor
->priv
;
364 ret
= mtk_nor_execute_cmd(mtk_nor
, MTK_NOR_RDSR_CMD
);
368 *buf
= readb(mtk_nor
->base
+ MTK_NOR_RDSR_REG
);
370 dev_err(mtk_nor
->dev
, "len should be 1 for read status!\n");
373 ret
= mtk_nor_do_tx_rx(mtk_nor
, opcode
, NULL
, 0, buf
, len
);
379 static int mtk_nor_write_reg(struct spi_nor
*nor
, u8 opcode
, u8
*buf
,
383 struct mtk_nor
*mtk_nor
= nor
->priv
;
387 /* We only handle 1 byte */
388 ret
= mtk_nor_wr_sr(mtk_nor
, *buf
);
391 ret
= mtk_nor_do_tx_rx(mtk_nor
, opcode
, buf
, len
, NULL
, 0);
393 dev_warn(mtk_nor
->dev
, "write reg failure!\n");
399 static void mtk_nor_disable_clk(struct mtk_nor
*mtk_nor
)
401 clk_disable_unprepare(mtk_nor
->spi_clk
);
402 clk_disable_unprepare(mtk_nor
->nor_clk
);
405 static int mtk_nor_enable_clk(struct mtk_nor
*mtk_nor
)
409 ret
= clk_prepare_enable(mtk_nor
->spi_clk
);
413 ret
= clk_prepare_enable(mtk_nor
->nor_clk
);
415 clk_disable_unprepare(mtk_nor
->spi_clk
);
422 static int mtk_nor_init(struct mtk_nor
*mtk_nor
,
423 struct device_node
*flash_node
)
425 const struct spi_nor_hwcaps hwcaps
= {
426 .mask
= SNOR_HWCAPS_READ
|
427 SNOR_HWCAPS_READ_FAST
|
428 SNOR_HWCAPS_READ_1_1_2
|
434 /* initialize controller to accept commands */
435 writel(MTK_NOR_ENABLE_SF_CMD
, mtk_nor
->base
+ MTK_NOR_WRPROT_REG
);
438 nor
->dev
= mtk_nor
->dev
;
440 spi_nor_set_flash_node(nor
, flash_node
);
442 /* fill the hooks to spi nor */
443 nor
->read
= mtk_nor_read
;
444 nor
->read_reg
= mtk_nor_read_reg
;
445 nor
->write
= mtk_nor_write
;
446 nor
->write_reg
= mtk_nor_write_reg
;
447 nor
->mtd
.name
= "mtk_nor";
448 /* initialized with NULL */
449 ret
= spi_nor_scan(nor
, NULL
, &hwcaps
);
453 return mtd_device_register(&nor
->mtd
, NULL
, 0);
456 static int mtk_nor_drv_probe(struct platform_device
*pdev
)
458 struct device_node
*flash_np
;
459 struct resource
*res
;
461 struct mtk_nor
*mtk_nor
;
463 if (!pdev
->dev
.of_node
) {
464 dev_err(&pdev
->dev
, "No DT found\n");
468 mtk_nor
= devm_kzalloc(&pdev
->dev
, sizeof(*mtk_nor
), GFP_KERNEL
);
471 platform_set_drvdata(pdev
, mtk_nor
);
473 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
474 mtk_nor
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
475 if (IS_ERR(mtk_nor
->base
))
476 return PTR_ERR(mtk_nor
->base
);
478 mtk_nor
->spi_clk
= devm_clk_get(&pdev
->dev
, "spi");
479 if (IS_ERR(mtk_nor
->spi_clk
))
480 return PTR_ERR(mtk_nor
->spi_clk
);
482 mtk_nor
->nor_clk
= devm_clk_get(&pdev
->dev
, "sf");
483 if (IS_ERR(mtk_nor
->nor_clk
))
484 return PTR_ERR(mtk_nor
->nor_clk
);
486 mtk_nor
->dev
= &pdev
->dev
;
488 ret
= mtk_nor_enable_clk(mtk_nor
);
492 /* only support one attached flash */
493 flash_np
= of_get_next_available_child(pdev
->dev
.of_node
, NULL
);
495 dev_err(&pdev
->dev
, "no SPI flash device to configure\n");
499 ret
= mtk_nor_init(mtk_nor
, flash_np
);
503 mtk_nor_disable_clk(mtk_nor
);
508 static int mtk_nor_drv_remove(struct platform_device
*pdev
)
510 struct mtk_nor
*mtk_nor
= platform_get_drvdata(pdev
);
512 mtk_nor_disable_clk(mtk_nor
);
517 #ifdef CONFIG_PM_SLEEP
518 static int mtk_nor_suspend(struct device
*dev
)
520 struct mtk_nor
*mtk_nor
= dev_get_drvdata(dev
);
522 mtk_nor_disable_clk(mtk_nor
);
527 static int mtk_nor_resume(struct device
*dev
)
529 struct mtk_nor
*mtk_nor
= dev_get_drvdata(dev
);
531 return mtk_nor_enable_clk(mtk_nor
);
534 static const struct dev_pm_ops mtk_nor_dev_pm_ops
= {
535 .suspend
= mtk_nor_suspend
,
536 .resume
= mtk_nor_resume
,
539 #define MTK_NOR_DEV_PM_OPS (&mtk_nor_dev_pm_ops)
541 #define MTK_NOR_DEV_PM_OPS NULL
544 static const struct of_device_id mtk_nor_of_ids
[] = {
545 { .compatible
= "mediatek,mt8173-nor"},
548 MODULE_DEVICE_TABLE(of
, mtk_nor_of_ids
);
550 static struct platform_driver mtk_nor_driver
= {
551 .probe
= mtk_nor_drv_probe
,
552 .remove
= mtk_nor_drv_remove
,
555 .pm
= MTK_NOR_DEV_PM_OPS
,
556 .of_match_table
= mtk_nor_of_ids
,
560 module_platform_driver(mtk_nor_driver
);
561 MODULE_LICENSE("GPL v2");
562 MODULE_DESCRIPTION("MediaTek SPI NOR Flash Driver");