1881d1ee9SChuanhong Guo // SPDX-License-Identifier: GPL-2.0
2881d1ee9SChuanhong Guo //
3881d1ee9SChuanhong Guo // Mediatek SPI NOR controller driver
4881d1ee9SChuanhong Guo //
5881d1ee9SChuanhong Guo // Copyright (C) 2020 Chuanhong Guo <gch981213@gmail.com>
6881d1ee9SChuanhong Guo
7881d1ee9SChuanhong Guo #include <linux/bits.h>
8881d1ee9SChuanhong Guo #include <linux/clk.h>
9881d1ee9SChuanhong Guo #include <linux/completion.h>
10881d1ee9SChuanhong Guo #include <linux/dma-mapping.h>
11881d1ee9SChuanhong Guo #include <linux/interrupt.h>
12881d1ee9SChuanhong Guo #include <linux/io.h>
13881d1ee9SChuanhong Guo #include <linux/iopoll.h>
14881d1ee9SChuanhong Guo #include <linux/kernel.h>
15881d1ee9SChuanhong Guo #include <linux/module.h>
16749396cbSRob Herring #include <linux/of.h>
17749396cbSRob Herring #include <linux/platform_device.h>
183bfd9103SIkjoon Jang #include <linux/pm_runtime.h>
19881d1ee9SChuanhong Guo #include <linux/spi/spi.h>
20881d1ee9SChuanhong Guo #include <linux/spi/spi-mem.h>
21881d1ee9SChuanhong Guo #include <linux/string.h>
22881d1ee9SChuanhong Guo
23881d1ee9SChuanhong Guo #define DRIVER_NAME "mtk-spi-nor"
24881d1ee9SChuanhong Guo
25881d1ee9SChuanhong Guo #define MTK_NOR_REG_CMD 0x00
26881d1ee9SChuanhong Guo #define MTK_NOR_CMD_WRITE BIT(4)
27881d1ee9SChuanhong Guo #define MTK_NOR_CMD_PROGRAM BIT(2)
28881d1ee9SChuanhong Guo #define MTK_NOR_CMD_READ BIT(0)
29881d1ee9SChuanhong Guo #define MTK_NOR_CMD_MASK GENMASK(5, 0)
30881d1ee9SChuanhong Guo
31881d1ee9SChuanhong Guo #define MTK_NOR_REG_PRG_CNT 0x04
32e7edd2cfSChuanhong Guo #define MTK_NOR_PRG_CNT_MAX 56
33881d1ee9SChuanhong Guo #define MTK_NOR_REG_RDATA 0x0c
34881d1ee9SChuanhong Guo
35881d1ee9SChuanhong Guo #define MTK_NOR_REG_RADR0 0x10
36881d1ee9SChuanhong Guo #define MTK_NOR_REG_RADR(n) (MTK_NOR_REG_RADR0 + 4 * (n))
37881d1ee9SChuanhong Guo #define MTK_NOR_REG_RADR3 0xc8
38881d1ee9SChuanhong Guo
39881d1ee9SChuanhong Guo #define MTK_NOR_REG_WDATA 0x1c
40881d1ee9SChuanhong Guo
41881d1ee9SChuanhong Guo #define MTK_NOR_REG_PRGDATA0 0x20
42881d1ee9SChuanhong Guo #define MTK_NOR_REG_PRGDATA(n) (MTK_NOR_REG_PRGDATA0 + 4 * (n))
43881d1ee9SChuanhong Guo #define MTK_NOR_REG_PRGDATA_MAX 5
44881d1ee9SChuanhong Guo
45881d1ee9SChuanhong Guo #define MTK_NOR_REG_SHIFT0 0x38
46881d1ee9SChuanhong Guo #define MTK_NOR_REG_SHIFT(n) (MTK_NOR_REG_SHIFT0 + 4 * (n))
47881d1ee9SChuanhong Guo #define MTK_NOR_REG_SHIFT_MAX 9
48881d1ee9SChuanhong Guo
49881d1ee9SChuanhong Guo #define MTK_NOR_REG_CFG1 0x60
50881d1ee9SChuanhong Guo #define MTK_NOR_FAST_READ BIT(0)
51881d1ee9SChuanhong Guo
52881d1ee9SChuanhong Guo #define MTK_NOR_REG_CFG2 0x64
53881d1ee9SChuanhong Guo #define MTK_NOR_WR_CUSTOM_OP_EN BIT(4)
54881d1ee9SChuanhong Guo #define MTK_NOR_WR_BUF_EN BIT(0)
55881d1ee9SChuanhong Guo
56881d1ee9SChuanhong Guo #define MTK_NOR_REG_PP_DATA 0x98
57881d1ee9SChuanhong Guo
58881d1ee9SChuanhong Guo #define MTK_NOR_REG_IRQ_STAT 0xa8
59881d1ee9SChuanhong Guo #define MTK_NOR_REG_IRQ_EN 0xac
60881d1ee9SChuanhong Guo #define MTK_NOR_IRQ_DMA BIT(7)
61881d1ee9SChuanhong Guo #define MTK_NOR_IRQ_MASK GENMASK(7, 0)
62881d1ee9SChuanhong Guo
63881d1ee9SChuanhong Guo #define MTK_NOR_REG_CFG3 0xb4
64881d1ee9SChuanhong Guo #define MTK_NOR_DISABLE_WREN BIT(7)
65881d1ee9SChuanhong Guo #define MTK_NOR_DISABLE_SR_POLL BIT(5)
66881d1ee9SChuanhong Guo
67881d1ee9SChuanhong Guo #define MTK_NOR_REG_WP 0xc4
68881d1ee9SChuanhong Guo #define MTK_NOR_ENABLE_SF_CMD 0x30
69881d1ee9SChuanhong Guo
70881d1ee9SChuanhong Guo #define MTK_NOR_REG_BUSCFG 0xcc
71881d1ee9SChuanhong Guo #define MTK_NOR_4B_ADDR BIT(4)
72881d1ee9SChuanhong Guo #define MTK_NOR_QUAD_ADDR BIT(3)
73881d1ee9SChuanhong Guo #define MTK_NOR_QUAD_READ BIT(2)
74881d1ee9SChuanhong Guo #define MTK_NOR_DUAL_ADDR BIT(1)
75881d1ee9SChuanhong Guo #define MTK_NOR_DUAL_READ BIT(0)
76881d1ee9SChuanhong Guo #define MTK_NOR_BUS_MODE_MASK GENMASK(4, 0)
77881d1ee9SChuanhong Guo
78881d1ee9SChuanhong Guo #define MTK_NOR_REG_DMA_CTL 0x718
79881d1ee9SChuanhong Guo #define MTK_NOR_DMA_START BIT(0)
80881d1ee9SChuanhong Guo
81881d1ee9SChuanhong Guo #define MTK_NOR_REG_DMA_FADR 0x71c
82881d1ee9SChuanhong Guo #define MTK_NOR_REG_DMA_DADR 0x720
83881d1ee9SChuanhong Guo #define MTK_NOR_REG_DMA_END_DADR 0x724
848330e9e8Sbayi cheng #define MTK_NOR_REG_CG_DIS 0x728
858330e9e8Sbayi cheng #define MTK_NOR_SFC_SW_RST BIT(2)
868330e9e8Sbayi cheng
87e836d4cfSIkjoon Jang #define MTK_NOR_REG_DMA_DADR_HB 0x738
88e836d4cfSIkjoon Jang #define MTK_NOR_REG_DMA_END_DADR_HB 0x73c
89881d1ee9SChuanhong Guo
90881d1ee9SChuanhong Guo #define MTK_NOR_PRG_MAX_SIZE 6
91881d1ee9SChuanhong Guo // Reading DMA src/dst addresses have to be 16-byte aligned
92881d1ee9SChuanhong Guo #define MTK_NOR_DMA_ALIGN 16
93881d1ee9SChuanhong Guo #define MTK_NOR_DMA_ALIGN_MASK (MTK_NOR_DMA_ALIGN - 1)
94881d1ee9SChuanhong Guo // and we allocate a bounce buffer if destination address isn't aligned.
95881d1ee9SChuanhong Guo #define MTK_NOR_BOUNCE_BUF_SIZE PAGE_SIZE
96881d1ee9SChuanhong Guo
97881d1ee9SChuanhong Guo // Buffered page program can do one 128-byte transfer
98881d1ee9SChuanhong Guo #define MTK_NOR_PP_SIZE 128
99881d1ee9SChuanhong Guo
1004cafaddeSChuanhong Guo #define CLK_TO_US(sp, clkcnt) DIV_ROUND_UP(clkcnt, sp->spi_freq / 1000000)
101881d1ee9SChuanhong Guo
1025b177234SGuochun Mao struct mtk_nor_caps {
1035b177234SGuochun Mao u8 dma_bits;
1045b177234SGuochun Mao
1055b177234SGuochun Mao /* extra_dummy_bit is adding for the IP of new SoCs.
1065b177234SGuochun Mao * Some new SoCs modify the timing of fetching registers' values
1075b177234SGuochun Mao * and IDs of nor flash, they need a extra_dummy_bit which can add
1085b177234SGuochun Mao * more clock cycles for fetching data.
1095b177234SGuochun Mao */
1105b177234SGuochun Mao u8 extra_dummy_bit;
1115b177234SGuochun Mao };
1125b177234SGuochun Mao
113881d1ee9SChuanhong Guo struct mtk_nor {
114881d1ee9SChuanhong Guo struct spi_controller *ctlr;
115881d1ee9SChuanhong Guo struct device *dev;
116881d1ee9SChuanhong Guo void __iomem *base;
117881d1ee9SChuanhong Guo u8 *buffer;
118a1daaa99SIkjoon Jang dma_addr_t buffer_dma;
119881d1ee9SChuanhong Guo struct clk *spi_clk;
120881d1ee9SChuanhong Guo struct clk *ctlr_clk;
121f32cce84Sbayi cheng struct clk *axi_clk;
12258b0a653SGuochun Mao struct clk *axi_s_clk;
123881d1ee9SChuanhong Guo unsigned int spi_freq;
124881d1ee9SChuanhong Guo bool wbuf_en;
125881d1ee9SChuanhong Guo bool has_irq;
126e836d4cfSIkjoon Jang bool high_dma;
127881d1ee9SChuanhong Guo struct completion op_done;
1285b177234SGuochun Mao const struct mtk_nor_caps *caps;
129881d1ee9SChuanhong Guo };
130881d1ee9SChuanhong Guo
mtk_nor_rmw(struct mtk_nor * sp,u32 reg,u32 set,u32 clr)131881d1ee9SChuanhong Guo static inline void mtk_nor_rmw(struct mtk_nor *sp, u32 reg, u32 set, u32 clr)
132881d1ee9SChuanhong Guo {
133881d1ee9SChuanhong Guo u32 val = readl(sp->base + reg);
134881d1ee9SChuanhong Guo
135881d1ee9SChuanhong Guo val &= ~clr;
136881d1ee9SChuanhong Guo val |= set;
137881d1ee9SChuanhong Guo writel(val, sp->base + reg);
138881d1ee9SChuanhong Guo }
139881d1ee9SChuanhong Guo
mtk_nor_cmd_exec(struct mtk_nor * sp,u32 cmd,ulong clk)140881d1ee9SChuanhong Guo static inline int mtk_nor_cmd_exec(struct mtk_nor *sp, u32 cmd, ulong clk)
141881d1ee9SChuanhong Guo {
142881d1ee9SChuanhong Guo ulong delay = CLK_TO_US(sp, clk);
143881d1ee9SChuanhong Guo u32 reg;
144881d1ee9SChuanhong Guo int ret;
145881d1ee9SChuanhong Guo
146881d1ee9SChuanhong Guo writel(cmd, sp->base + MTK_NOR_REG_CMD);
147881d1ee9SChuanhong Guo ret = readl_poll_timeout(sp->base + MTK_NOR_REG_CMD, reg, !(reg & cmd),
148881d1ee9SChuanhong Guo delay / 3, (delay + 1) * 200);
149881d1ee9SChuanhong Guo if (ret < 0)
150881d1ee9SChuanhong Guo dev_err(sp->dev, "command %u timeout.\n", cmd);
151881d1ee9SChuanhong Guo return ret;
152881d1ee9SChuanhong Guo }
153881d1ee9SChuanhong Guo
mtk_nor_reset(struct mtk_nor * sp)1548330e9e8Sbayi cheng static void mtk_nor_reset(struct mtk_nor *sp)
1558330e9e8Sbayi cheng {
1568330e9e8Sbayi cheng mtk_nor_rmw(sp, MTK_NOR_REG_CG_DIS, 0, MTK_NOR_SFC_SW_RST);
1578330e9e8Sbayi cheng mb(); /* flush previous writes */
1588330e9e8Sbayi cheng mtk_nor_rmw(sp, MTK_NOR_REG_CG_DIS, MTK_NOR_SFC_SW_RST, 0);
1598330e9e8Sbayi cheng mb(); /* flush previous writes */
1608330e9e8Sbayi cheng writel(MTK_NOR_ENABLE_SF_CMD, sp->base + MTK_NOR_REG_WP);
1618330e9e8Sbayi cheng }
1628330e9e8Sbayi cheng
mtk_nor_set_addr(struct mtk_nor * sp,const struct spi_mem_op * op)163881d1ee9SChuanhong Guo static void mtk_nor_set_addr(struct mtk_nor *sp, const struct spi_mem_op *op)
164881d1ee9SChuanhong Guo {
165881d1ee9SChuanhong Guo u32 addr = op->addr.val;
166881d1ee9SChuanhong Guo int i;
167881d1ee9SChuanhong Guo
168881d1ee9SChuanhong Guo for (i = 0; i < 3; i++) {
169881d1ee9SChuanhong Guo writeb(addr & 0xff, sp->base + MTK_NOR_REG_RADR(i));
170881d1ee9SChuanhong Guo addr >>= 8;
171881d1ee9SChuanhong Guo }
172881d1ee9SChuanhong Guo if (op->addr.nbytes == 4) {
173881d1ee9SChuanhong Guo writeb(addr & 0xff, sp->base + MTK_NOR_REG_RADR3);
174881d1ee9SChuanhong Guo mtk_nor_rmw(sp, MTK_NOR_REG_BUSCFG, MTK_NOR_4B_ADDR, 0);
175881d1ee9SChuanhong Guo } else {
176881d1ee9SChuanhong Guo mtk_nor_rmw(sp, MTK_NOR_REG_BUSCFG, 0, MTK_NOR_4B_ADDR);
177881d1ee9SChuanhong Guo }
178881d1ee9SChuanhong Guo }
179881d1ee9SChuanhong Guo
need_bounce(struct mtk_nor * sp,const struct spi_mem_op * op)180a1daaa99SIkjoon Jang static bool need_bounce(struct mtk_nor *sp, const struct spi_mem_op *op)
181a1daaa99SIkjoon Jang {
182a1daaa99SIkjoon Jang return ((uintptr_t)op->data.buf.in & MTK_NOR_DMA_ALIGN_MASK);
183a1daaa99SIkjoon Jang }
184a1daaa99SIkjoon Jang
mtk_nor_match_read(const struct spi_mem_op * op)185881d1ee9SChuanhong Guo static bool mtk_nor_match_read(const struct spi_mem_op *op)
186881d1ee9SChuanhong Guo {
187881d1ee9SChuanhong Guo int dummy = 0;
188881d1ee9SChuanhong Guo
18909134c53SYoshitaka Ikeda if (op->dummy.nbytes)
190881d1ee9SChuanhong Guo dummy = op->dummy.nbytes * BITS_PER_BYTE / op->dummy.buswidth;
191881d1ee9SChuanhong Guo
192881d1ee9SChuanhong Guo if ((op->data.buswidth == 2) || (op->data.buswidth == 4)) {
193881d1ee9SChuanhong Guo if (op->addr.buswidth == 1)
194881d1ee9SChuanhong Guo return dummy == 8;
195881d1ee9SChuanhong Guo else if (op->addr.buswidth == 2)
196881d1ee9SChuanhong Guo return dummy == 4;
197881d1ee9SChuanhong Guo else if (op->addr.buswidth == 4)
198881d1ee9SChuanhong Guo return dummy == 6;
199881d1ee9SChuanhong Guo } else if ((op->addr.buswidth == 1) && (op->data.buswidth == 1)) {
200881d1ee9SChuanhong Guo if (op->cmd.opcode == 0x03)
201881d1ee9SChuanhong Guo return dummy == 0;
202881d1ee9SChuanhong Guo else if (op->cmd.opcode == 0x0b)
203881d1ee9SChuanhong Guo return dummy == 8;
204881d1ee9SChuanhong Guo }
205881d1ee9SChuanhong Guo return false;
206881d1ee9SChuanhong Guo }
207881d1ee9SChuanhong Guo
mtk_nor_match_prg(const struct spi_mem_op * op)208fd806575SChuanhong Guo static bool mtk_nor_match_prg(const struct spi_mem_op *op)
209fd806575SChuanhong Guo {
210fd806575SChuanhong Guo int tx_len, rx_len, prg_len, prg_left;
211fd806575SChuanhong Guo
212fd806575SChuanhong Guo // prg mode is spi-only.
213fd806575SChuanhong Guo if ((op->cmd.buswidth > 1) || (op->addr.buswidth > 1) ||
214fd806575SChuanhong Guo (op->dummy.buswidth > 1) || (op->data.buswidth > 1))
215fd806575SChuanhong Guo return false;
216fd806575SChuanhong Guo
217fd806575SChuanhong Guo tx_len = op->cmd.nbytes + op->addr.nbytes;
218fd806575SChuanhong Guo
219fd806575SChuanhong Guo if (op->data.dir == SPI_MEM_DATA_OUT) {
220fd806575SChuanhong Guo // count dummy bytes only if we need to write data after it
221fd806575SChuanhong Guo tx_len += op->dummy.nbytes;
222fd806575SChuanhong Guo
223fd806575SChuanhong Guo // leave at least one byte for data
224fd806575SChuanhong Guo if (tx_len > MTK_NOR_REG_PRGDATA_MAX)
225fd806575SChuanhong Guo return false;
226fd806575SChuanhong Guo
227fd806575SChuanhong Guo // if there's no addr, meaning adjust_op_size is impossible,
228fd806575SChuanhong Guo // check data length as well.
229fd806575SChuanhong Guo if ((!op->addr.nbytes) &&
230fd806575SChuanhong Guo (tx_len + op->data.nbytes > MTK_NOR_REG_PRGDATA_MAX + 1))
231fd806575SChuanhong Guo return false;
232fd806575SChuanhong Guo } else if (op->data.dir == SPI_MEM_DATA_IN) {
233fd806575SChuanhong Guo if (tx_len > MTK_NOR_REG_PRGDATA_MAX + 1)
234fd806575SChuanhong Guo return false;
235fd806575SChuanhong Guo
236fd806575SChuanhong Guo rx_len = op->data.nbytes;
237fd806575SChuanhong Guo prg_left = MTK_NOR_PRG_CNT_MAX / 8 - tx_len - op->dummy.nbytes;
238fd806575SChuanhong Guo if (prg_left > MTK_NOR_REG_SHIFT_MAX + 1)
239fd806575SChuanhong Guo prg_left = MTK_NOR_REG_SHIFT_MAX + 1;
240fd806575SChuanhong Guo if (rx_len > prg_left) {
241fd806575SChuanhong Guo if (!op->addr.nbytes)
242fd806575SChuanhong Guo return false;
243fd806575SChuanhong Guo rx_len = prg_left;
244fd806575SChuanhong Guo }
245fd806575SChuanhong Guo
246fd806575SChuanhong Guo prg_len = tx_len + op->dummy.nbytes + rx_len;
247fd806575SChuanhong Guo if (prg_len > MTK_NOR_PRG_CNT_MAX / 8)
248fd806575SChuanhong Guo return false;
249fd806575SChuanhong Guo } else {
250fd806575SChuanhong Guo prg_len = tx_len + op->dummy.nbytes;
251fd806575SChuanhong Guo if (prg_len > MTK_NOR_PRG_CNT_MAX / 8)
252fd806575SChuanhong Guo return false;
253fd806575SChuanhong Guo }
254fd806575SChuanhong Guo return true;
255fd806575SChuanhong Guo }
256fd806575SChuanhong Guo
mtk_nor_adj_prg_size(struct spi_mem_op * op)257fd806575SChuanhong Guo static void mtk_nor_adj_prg_size(struct spi_mem_op *op)
258fd806575SChuanhong Guo {
259fd806575SChuanhong Guo int tx_len, tx_left, prg_left;
260fd806575SChuanhong Guo
261fd806575SChuanhong Guo tx_len = op->cmd.nbytes + op->addr.nbytes;
262fd806575SChuanhong Guo if (op->data.dir == SPI_MEM_DATA_OUT) {
263fd806575SChuanhong Guo tx_len += op->dummy.nbytes;
264fd806575SChuanhong Guo tx_left = MTK_NOR_REG_PRGDATA_MAX + 1 - tx_len;
265fd806575SChuanhong Guo if (op->data.nbytes > tx_left)
266fd806575SChuanhong Guo op->data.nbytes = tx_left;
267fd806575SChuanhong Guo } else if (op->data.dir == SPI_MEM_DATA_IN) {
268fd806575SChuanhong Guo prg_left = MTK_NOR_PRG_CNT_MAX / 8 - tx_len - op->dummy.nbytes;
269fd806575SChuanhong Guo if (prg_left > MTK_NOR_REG_SHIFT_MAX + 1)
270fd806575SChuanhong Guo prg_left = MTK_NOR_REG_SHIFT_MAX + 1;
271fd806575SChuanhong Guo if (op->data.nbytes > prg_left)
272fd806575SChuanhong Guo op->data.nbytes = prg_left;
273fd806575SChuanhong Guo }
274fd806575SChuanhong Guo }
275fd806575SChuanhong Guo
mtk_nor_adjust_op_size(struct spi_mem * mem,struct spi_mem_op * op)276881d1ee9SChuanhong Guo static int mtk_nor_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op)
277881d1ee9SChuanhong Guo {
278*a3a77a42SYang Yingliang struct mtk_nor *sp = spi_controller_get_devdata(mem->spi->controller);
279a1daaa99SIkjoon Jang
280881d1ee9SChuanhong Guo if (!op->data.nbytes)
281881d1ee9SChuanhong Guo return 0;
282881d1ee9SChuanhong Guo
283881d1ee9SChuanhong Guo if ((op->addr.nbytes == 3) || (op->addr.nbytes == 4)) {
284881d1ee9SChuanhong Guo if ((op->data.dir == SPI_MEM_DATA_IN) &&
285881d1ee9SChuanhong Guo mtk_nor_match_read(op)) {
2864cafaddeSChuanhong Guo // limit size to prevent timeout calculation overflow
2874cafaddeSChuanhong Guo if (op->data.nbytes > 0x400000)
2884cafaddeSChuanhong Guo op->data.nbytes = 0x400000;
2894cafaddeSChuanhong Guo
290881d1ee9SChuanhong Guo if ((op->addr.val & MTK_NOR_DMA_ALIGN_MASK) ||
291881d1ee9SChuanhong Guo (op->data.nbytes < MTK_NOR_DMA_ALIGN))
292881d1ee9SChuanhong Guo op->data.nbytes = 1;
293a1daaa99SIkjoon Jang else if (!need_bounce(sp, op))
294881d1ee9SChuanhong Guo op->data.nbytes &= ~MTK_NOR_DMA_ALIGN_MASK;
295881d1ee9SChuanhong Guo else if (op->data.nbytes > MTK_NOR_BOUNCE_BUF_SIZE)
296881d1ee9SChuanhong Guo op->data.nbytes = MTK_NOR_BOUNCE_BUF_SIZE;
297881d1ee9SChuanhong Guo return 0;
298881d1ee9SChuanhong Guo } else if (op->data.dir == SPI_MEM_DATA_OUT) {
299881d1ee9SChuanhong Guo if (op->data.nbytes >= MTK_NOR_PP_SIZE)
300881d1ee9SChuanhong Guo op->data.nbytes = MTK_NOR_PP_SIZE;
301881d1ee9SChuanhong Guo else
302881d1ee9SChuanhong Guo op->data.nbytes = 1;
303881d1ee9SChuanhong Guo return 0;
304881d1ee9SChuanhong Guo }
305881d1ee9SChuanhong Guo }
306881d1ee9SChuanhong Guo
307fd806575SChuanhong Guo mtk_nor_adj_prg_size(op);
308881d1ee9SChuanhong Guo return 0;
309881d1ee9SChuanhong Guo }
310881d1ee9SChuanhong Guo
mtk_nor_supports_op(struct spi_mem * mem,const struct spi_mem_op * op)311881d1ee9SChuanhong Guo static bool mtk_nor_supports_op(struct spi_mem *mem,
312881d1ee9SChuanhong Guo const struct spi_mem_op *op)
313881d1ee9SChuanhong Guo {
31481f13f21SChuanhong Guo if (!spi_mem_default_supports_op(mem, op))
31581f13f21SChuanhong Guo return false;
316881d1ee9SChuanhong Guo
317881d1ee9SChuanhong Guo if (op->cmd.buswidth != 1)
318881d1ee9SChuanhong Guo return false;
319881d1ee9SChuanhong Guo
320881d1ee9SChuanhong Guo if ((op->addr.nbytes == 3) || (op->addr.nbytes == 4)) {
321a59b2c7cSIkjoon Jang switch (op->data.dir) {
322a59b2c7cSIkjoon Jang case SPI_MEM_DATA_IN:
32381f13f21SChuanhong Guo if (mtk_nor_match_read(op))
32481f13f21SChuanhong Guo return true;
325a59b2c7cSIkjoon Jang break;
326a59b2c7cSIkjoon Jang case SPI_MEM_DATA_OUT:
32781f13f21SChuanhong Guo if ((op->addr.buswidth == 1) &&
32881f13f21SChuanhong Guo (op->dummy.nbytes == 0) &&
32981f13f21SChuanhong Guo (op->data.buswidth == 1))
33081f13f21SChuanhong Guo return true;
331a59b2c7cSIkjoon Jang break;
332a59b2c7cSIkjoon Jang default:
333a59b2c7cSIkjoon Jang break;
334a59b2c7cSIkjoon Jang }
335881d1ee9SChuanhong Guo }
336a59b2c7cSIkjoon Jang
33781f13f21SChuanhong Guo return mtk_nor_match_prg(op);
338881d1ee9SChuanhong Guo }
339881d1ee9SChuanhong Guo
mtk_nor_setup_bus(struct mtk_nor * sp,const struct spi_mem_op * op)340881d1ee9SChuanhong Guo static void mtk_nor_setup_bus(struct mtk_nor *sp, const struct spi_mem_op *op)
341881d1ee9SChuanhong Guo {
342881d1ee9SChuanhong Guo u32 reg = 0;
343881d1ee9SChuanhong Guo
344881d1ee9SChuanhong Guo if (op->addr.nbytes == 4)
345881d1ee9SChuanhong Guo reg |= MTK_NOR_4B_ADDR;
346881d1ee9SChuanhong Guo
347881d1ee9SChuanhong Guo if (op->data.buswidth == 4) {
348881d1ee9SChuanhong Guo reg |= MTK_NOR_QUAD_READ;
349881d1ee9SChuanhong Guo writeb(op->cmd.opcode, sp->base + MTK_NOR_REG_PRGDATA(4));
350881d1ee9SChuanhong Guo if (op->addr.buswidth == 4)
351881d1ee9SChuanhong Guo reg |= MTK_NOR_QUAD_ADDR;
352881d1ee9SChuanhong Guo } else if (op->data.buswidth == 2) {
353881d1ee9SChuanhong Guo reg |= MTK_NOR_DUAL_READ;
354881d1ee9SChuanhong Guo writeb(op->cmd.opcode, sp->base + MTK_NOR_REG_PRGDATA(3));
355881d1ee9SChuanhong Guo if (op->addr.buswidth == 2)
356881d1ee9SChuanhong Guo reg |= MTK_NOR_DUAL_ADDR;
357881d1ee9SChuanhong Guo } else {
358881d1ee9SChuanhong Guo if (op->cmd.opcode == 0x0b)
359881d1ee9SChuanhong Guo mtk_nor_rmw(sp, MTK_NOR_REG_CFG1, MTK_NOR_FAST_READ, 0);
360881d1ee9SChuanhong Guo else
361881d1ee9SChuanhong Guo mtk_nor_rmw(sp, MTK_NOR_REG_CFG1, 0, MTK_NOR_FAST_READ);
362881d1ee9SChuanhong Guo }
363881d1ee9SChuanhong Guo mtk_nor_rmw(sp, MTK_NOR_REG_BUSCFG, reg, MTK_NOR_BUS_MODE_MASK);
364881d1ee9SChuanhong Guo }
365881d1ee9SChuanhong Guo
mtk_nor_dma_exec(struct mtk_nor * sp,u32 from,unsigned int length,dma_addr_t dma_addr)366a1daaa99SIkjoon Jang static int mtk_nor_dma_exec(struct mtk_nor *sp, u32 from, unsigned int length,
367a1daaa99SIkjoon Jang dma_addr_t dma_addr)
368881d1ee9SChuanhong Guo {
369881d1ee9SChuanhong Guo int ret = 0;
370d52a826bSbayi cheng u32 delay, timeout;
371881d1ee9SChuanhong Guo u32 reg;
372881d1ee9SChuanhong Guo
373881d1ee9SChuanhong Guo writel(from, sp->base + MTK_NOR_REG_DMA_FADR);
374881d1ee9SChuanhong Guo writel(dma_addr, sp->base + MTK_NOR_REG_DMA_DADR);
375881d1ee9SChuanhong Guo writel(dma_addr + length, sp->base + MTK_NOR_REG_DMA_END_DADR);
376881d1ee9SChuanhong Guo
377e836d4cfSIkjoon Jang if (sp->high_dma) {
378e836d4cfSIkjoon Jang writel(upper_32_bits(dma_addr),
379e836d4cfSIkjoon Jang sp->base + MTK_NOR_REG_DMA_DADR_HB);
380e836d4cfSIkjoon Jang writel(upper_32_bits(dma_addr + length),
381e836d4cfSIkjoon Jang sp->base + MTK_NOR_REG_DMA_END_DADR_HB);
382e836d4cfSIkjoon Jang }
383e836d4cfSIkjoon Jang
384881d1ee9SChuanhong Guo if (sp->has_irq) {
385881d1ee9SChuanhong Guo reinit_completion(&sp->op_done);
386881d1ee9SChuanhong Guo mtk_nor_rmw(sp, MTK_NOR_REG_IRQ_EN, MTK_NOR_IRQ_DMA, 0);
387881d1ee9SChuanhong Guo }
388881d1ee9SChuanhong Guo
389881d1ee9SChuanhong Guo mtk_nor_rmw(sp, MTK_NOR_REG_DMA_CTL, MTK_NOR_DMA_START, 0);
390881d1ee9SChuanhong Guo
391881d1ee9SChuanhong Guo delay = CLK_TO_US(sp, (length + 5) * BITS_PER_BYTE);
392d52a826bSbayi cheng timeout = (delay + 1) * 100;
393881d1ee9SChuanhong Guo
394881d1ee9SChuanhong Guo if (sp->has_irq) {
395881d1ee9SChuanhong Guo if (!wait_for_completion_timeout(&sp->op_done,
396d52a826bSbayi cheng usecs_to_jiffies(max(timeout, 10000U))))
397881d1ee9SChuanhong Guo ret = -ETIMEDOUT;
398881d1ee9SChuanhong Guo } else {
399881d1ee9SChuanhong Guo ret = readl_poll_timeout(sp->base + MTK_NOR_REG_DMA_CTL, reg,
400881d1ee9SChuanhong Guo !(reg & MTK_NOR_DMA_START), delay / 3,
401d52a826bSbayi cheng timeout);
402881d1ee9SChuanhong Guo }
403881d1ee9SChuanhong Guo
404881d1ee9SChuanhong Guo if (ret < 0)
405881d1ee9SChuanhong Guo dev_err(sp->dev, "dma read timeout.\n");
406881d1ee9SChuanhong Guo
407881d1ee9SChuanhong Guo return ret;
408881d1ee9SChuanhong Guo }
409881d1ee9SChuanhong Guo
mtk_nor_read_bounce(struct mtk_nor * sp,const struct spi_mem_op * op)410a1daaa99SIkjoon Jang static int mtk_nor_read_bounce(struct mtk_nor *sp, const struct spi_mem_op *op)
411881d1ee9SChuanhong Guo {
412881d1ee9SChuanhong Guo unsigned int rdlen;
413881d1ee9SChuanhong Guo int ret;
414881d1ee9SChuanhong Guo
415a1daaa99SIkjoon Jang if (op->data.nbytes & MTK_NOR_DMA_ALIGN_MASK)
416a1daaa99SIkjoon Jang rdlen = (op->data.nbytes + MTK_NOR_DMA_ALIGN) & ~MTK_NOR_DMA_ALIGN_MASK;
417881d1ee9SChuanhong Guo else
418a1daaa99SIkjoon Jang rdlen = op->data.nbytes;
419881d1ee9SChuanhong Guo
420a1daaa99SIkjoon Jang ret = mtk_nor_dma_exec(sp, op->addr.val, rdlen, sp->buffer_dma);
421a1daaa99SIkjoon Jang
422a1daaa99SIkjoon Jang if (!ret)
423a1daaa99SIkjoon Jang memcpy(op->data.buf.in, sp->buffer, op->data.nbytes);
424a1daaa99SIkjoon Jang
425881d1ee9SChuanhong Guo return ret;
426a1daaa99SIkjoon Jang }
427881d1ee9SChuanhong Guo
mtk_nor_read_dma(struct mtk_nor * sp,const struct spi_mem_op * op)428a1daaa99SIkjoon Jang static int mtk_nor_read_dma(struct mtk_nor *sp, const struct spi_mem_op *op)
429a1daaa99SIkjoon Jang {
430a1daaa99SIkjoon Jang int ret;
431a1daaa99SIkjoon Jang dma_addr_t dma_addr;
432a1daaa99SIkjoon Jang
433a1daaa99SIkjoon Jang if (need_bounce(sp, op))
434a1daaa99SIkjoon Jang return mtk_nor_read_bounce(sp, op);
435a1daaa99SIkjoon Jang
436a1daaa99SIkjoon Jang dma_addr = dma_map_single(sp->dev, op->data.buf.in,
437a1daaa99SIkjoon Jang op->data.nbytes, DMA_FROM_DEVICE);
438a1daaa99SIkjoon Jang
439a1daaa99SIkjoon Jang if (dma_mapping_error(sp->dev, dma_addr))
440a1daaa99SIkjoon Jang return -EINVAL;
441a1daaa99SIkjoon Jang
442a1daaa99SIkjoon Jang ret = mtk_nor_dma_exec(sp, op->addr.val, op->data.nbytes, dma_addr);
443a1daaa99SIkjoon Jang
444a1daaa99SIkjoon Jang dma_unmap_single(sp->dev, dma_addr, op->data.nbytes, DMA_FROM_DEVICE);
445a1daaa99SIkjoon Jang
446a1daaa99SIkjoon Jang return ret;
447881d1ee9SChuanhong Guo }
448881d1ee9SChuanhong Guo
mtk_nor_read_pio(struct mtk_nor * sp,const struct spi_mem_op * op)449881d1ee9SChuanhong Guo static int mtk_nor_read_pio(struct mtk_nor *sp, const struct spi_mem_op *op)
450881d1ee9SChuanhong Guo {
451881d1ee9SChuanhong Guo u8 *buf = op->data.buf.in;
452881d1ee9SChuanhong Guo int ret;
453881d1ee9SChuanhong Guo
454881d1ee9SChuanhong Guo ret = mtk_nor_cmd_exec(sp, MTK_NOR_CMD_READ, 6 * BITS_PER_BYTE);
455881d1ee9SChuanhong Guo if (!ret)
456881d1ee9SChuanhong Guo buf[0] = readb(sp->base + MTK_NOR_REG_RDATA);
457881d1ee9SChuanhong Guo return ret;
458881d1ee9SChuanhong Guo }
459881d1ee9SChuanhong Guo
mtk_nor_setup_write_buffer(struct mtk_nor * sp,bool on)46063d9a4d8Sbayi cheng static int mtk_nor_setup_write_buffer(struct mtk_nor *sp, bool on)
461881d1ee9SChuanhong Guo {
462881d1ee9SChuanhong Guo int ret;
463881d1ee9SChuanhong Guo u32 val;
464881d1ee9SChuanhong Guo
46563d9a4d8Sbayi cheng if (!(sp->wbuf_en ^ on))
466881d1ee9SChuanhong Guo return 0;
467881d1ee9SChuanhong Guo
468881d1ee9SChuanhong Guo val = readl(sp->base + MTK_NOR_REG_CFG2);
46963d9a4d8Sbayi cheng if (on) {
470881d1ee9SChuanhong Guo writel(val | MTK_NOR_WR_BUF_EN, sp->base + MTK_NOR_REG_CFG2);
471881d1ee9SChuanhong Guo ret = readl_poll_timeout(sp->base + MTK_NOR_REG_CFG2, val,
472881d1ee9SChuanhong Guo val & MTK_NOR_WR_BUF_EN, 0, 10000);
47363d9a4d8Sbayi cheng } else {
474881d1ee9SChuanhong Guo writel(val & ~MTK_NOR_WR_BUF_EN, sp->base + MTK_NOR_REG_CFG2);
475881d1ee9SChuanhong Guo ret = readl_poll_timeout(sp->base + MTK_NOR_REG_CFG2, val,
476881d1ee9SChuanhong Guo !(val & MTK_NOR_WR_BUF_EN), 0, 10000);
47763d9a4d8Sbayi cheng }
47863d9a4d8Sbayi cheng
479881d1ee9SChuanhong Guo if (!ret)
48063d9a4d8Sbayi cheng sp->wbuf_en = on;
48163d9a4d8Sbayi cheng
482881d1ee9SChuanhong Guo return ret;
483881d1ee9SChuanhong Guo }
484881d1ee9SChuanhong Guo
mtk_nor_pp_buffered(struct mtk_nor * sp,const struct spi_mem_op * op)485881d1ee9SChuanhong Guo static int mtk_nor_pp_buffered(struct mtk_nor *sp, const struct spi_mem_op *op)
486881d1ee9SChuanhong Guo {
487881d1ee9SChuanhong Guo const u8 *buf = op->data.buf.out;
488881d1ee9SChuanhong Guo u32 val;
489881d1ee9SChuanhong Guo int ret, i;
490881d1ee9SChuanhong Guo
49163d9a4d8Sbayi cheng ret = mtk_nor_setup_write_buffer(sp, true);
492881d1ee9SChuanhong Guo if (ret < 0)
493881d1ee9SChuanhong Guo return ret;
494881d1ee9SChuanhong Guo
495881d1ee9SChuanhong Guo for (i = 0; i < op->data.nbytes; i += 4) {
496881d1ee9SChuanhong Guo val = buf[i + 3] << 24 | buf[i + 2] << 16 | buf[i + 1] << 8 |
497881d1ee9SChuanhong Guo buf[i];
498881d1ee9SChuanhong Guo writel(val, sp->base + MTK_NOR_REG_PP_DATA);
499881d1ee9SChuanhong Guo }
500881d1ee9SChuanhong Guo return mtk_nor_cmd_exec(sp, MTK_NOR_CMD_WRITE,
501881d1ee9SChuanhong Guo (op->data.nbytes + 5) * BITS_PER_BYTE);
502881d1ee9SChuanhong Guo }
503881d1ee9SChuanhong Guo
mtk_nor_pp_unbuffered(struct mtk_nor * sp,const struct spi_mem_op * op)504881d1ee9SChuanhong Guo static int mtk_nor_pp_unbuffered(struct mtk_nor *sp,
505881d1ee9SChuanhong Guo const struct spi_mem_op *op)
506881d1ee9SChuanhong Guo {
507881d1ee9SChuanhong Guo const u8 *buf = op->data.buf.out;
508881d1ee9SChuanhong Guo int ret;
509881d1ee9SChuanhong Guo
51063d9a4d8Sbayi cheng ret = mtk_nor_setup_write_buffer(sp, false);
511881d1ee9SChuanhong Guo if (ret < 0)
512881d1ee9SChuanhong Guo return ret;
513881d1ee9SChuanhong Guo writeb(buf[0], sp->base + MTK_NOR_REG_WDATA);
514881d1ee9SChuanhong Guo return mtk_nor_cmd_exec(sp, MTK_NOR_CMD_WRITE, 6 * BITS_PER_BYTE);
515881d1ee9SChuanhong Guo }
516881d1ee9SChuanhong Guo
mtk_nor_spi_mem_prg(struct mtk_nor * sp,const struct spi_mem_op * op)517e7edd2cfSChuanhong Guo static int mtk_nor_spi_mem_prg(struct mtk_nor *sp, const struct spi_mem_op *op)
518e7edd2cfSChuanhong Guo {
519e7edd2cfSChuanhong Guo int rx_len = 0;
520e7edd2cfSChuanhong Guo int reg_offset = MTK_NOR_REG_PRGDATA_MAX;
521e7edd2cfSChuanhong Guo int tx_len, prg_len;
522e7edd2cfSChuanhong Guo int i, ret;
523e7edd2cfSChuanhong Guo void __iomem *reg;
524e7edd2cfSChuanhong Guo u8 bufbyte;
525e7edd2cfSChuanhong Guo
526e7edd2cfSChuanhong Guo tx_len = op->cmd.nbytes + op->addr.nbytes;
527e7edd2cfSChuanhong Guo
528e7edd2cfSChuanhong Guo // count dummy bytes only if we need to write data after it
529e7edd2cfSChuanhong Guo if (op->data.dir == SPI_MEM_DATA_OUT)
530e7edd2cfSChuanhong Guo tx_len += op->dummy.nbytes + op->data.nbytes;
531e7edd2cfSChuanhong Guo else if (op->data.dir == SPI_MEM_DATA_IN)
532e7edd2cfSChuanhong Guo rx_len = op->data.nbytes;
533e7edd2cfSChuanhong Guo
534e7edd2cfSChuanhong Guo prg_len = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes +
535e7edd2cfSChuanhong Guo op->data.nbytes;
536e7edd2cfSChuanhong Guo
537e7edd2cfSChuanhong Guo // an invalid op may reach here if the caller calls exec_op without
538e7edd2cfSChuanhong Guo // adjust_op_size. return -EINVAL instead of -ENOTSUPP so that
539e7edd2cfSChuanhong Guo // spi-mem won't try this op again with generic spi transfers.
540e7edd2cfSChuanhong Guo if ((tx_len > MTK_NOR_REG_PRGDATA_MAX + 1) ||
541e7edd2cfSChuanhong Guo (rx_len > MTK_NOR_REG_SHIFT_MAX + 1) ||
542e7edd2cfSChuanhong Guo (prg_len > MTK_NOR_PRG_CNT_MAX / 8))
543e7edd2cfSChuanhong Guo return -EINVAL;
544e7edd2cfSChuanhong Guo
545e7edd2cfSChuanhong Guo // fill tx data
546e7edd2cfSChuanhong Guo for (i = op->cmd.nbytes; i > 0; i--, reg_offset--) {
547e7edd2cfSChuanhong Guo reg = sp->base + MTK_NOR_REG_PRGDATA(reg_offset);
548e7edd2cfSChuanhong Guo bufbyte = (op->cmd.opcode >> ((i - 1) * BITS_PER_BYTE)) & 0xff;
549e7edd2cfSChuanhong Guo writeb(bufbyte, reg);
550e7edd2cfSChuanhong Guo }
551e7edd2cfSChuanhong Guo
552e7edd2cfSChuanhong Guo for (i = op->addr.nbytes; i > 0; i--, reg_offset--) {
553e7edd2cfSChuanhong Guo reg = sp->base + MTK_NOR_REG_PRGDATA(reg_offset);
554e7edd2cfSChuanhong Guo bufbyte = (op->addr.val >> ((i - 1) * BITS_PER_BYTE)) & 0xff;
555e7edd2cfSChuanhong Guo writeb(bufbyte, reg);
556e7edd2cfSChuanhong Guo }
557e7edd2cfSChuanhong Guo
558e7edd2cfSChuanhong Guo if (op->data.dir == SPI_MEM_DATA_OUT) {
559e7edd2cfSChuanhong Guo for (i = 0; i < op->dummy.nbytes; i++, reg_offset--) {
560e7edd2cfSChuanhong Guo reg = sp->base + MTK_NOR_REG_PRGDATA(reg_offset);
561e7edd2cfSChuanhong Guo writeb(0, reg);
562e7edd2cfSChuanhong Guo }
563e7edd2cfSChuanhong Guo
564e7edd2cfSChuanhong Guo for (i = 0; i < op->data.nbytes; i++, reg_offset--) {
565e7edd2cfSChuanhong Guo reg = sp->base + MTK_NOR_REG_PRGDATA(reg_offset);
566e7edd2cfSChuanhong Guo writeb(((const u8 *)(op->data.buf.out))[i], reg);
567e7edd2cfSChuanhong Guo }
568e7edd2cfSChuanhong Guo }
569e7edd2cfSChuanhong Guo
570e7edd2cfSChuanhong Guo for (; reg_offset >= 0; reg_offset--) {
571e7edd2cfSChuanhong Guo reg = sp->base + MTK_NOR_REG_PRGDATA(reg_offset);
572e7edd2cfSChuanhong Guo writeb(0, reg);
573e7edd2cfSChuanhong Guo }
574e7edd2cfSChuanhong Guo
575e7edd2cfSChuanhong Guo // trigger op
5765b177234SGuochun Mao if (rx_len)
5775b177234SGuochun Mao writel(prg_len * BITS_PER_BYTE + sp->caps->extra_dummy_bit,
5785b177234SGuochun Mao sp->base + MTK_NOR_REG_PRG_CNT);
5795b177234SGuochun Mao else
580e7edd2cfSChuanhong Guo writel(prg_len * BITS_PER_BYTE, sp->base + MTK_NOR_REG_PRG_CNT);
5815b177234SGuochun Mao
582e7edd2cfSChuanhong Guo ret = mtk_nor_cmd_exec(sp, MTK_NOR_CMD_PROGRAM,
583e7edd2cfSChuanhong Guo prg_len * BITS_PER_BYTE);
584e7edd2cfSChuanhong Guo if (ret)
585e7edd2cfSChuanhong Guo return ret;
586e7edd2cfSChuanhong Guo
587e7edd2cfSChuanhong Guo // fetch read data
588e7edd2cfSChuanhong Guo reg_offset = 0;
589e7edd2cfSChuanhong Guo if (op->data.dir == SPI_MEM_DATA_IN) {
590e7edd2cfSChuanhong Guo for (i = op->data.nbytes - 1; i >= 0; i--, reg_offset++) {
591e7edd2cfSChuanhong Guo reg = sp->base + MTK_NOR_REG_SHIFT(reg_offset);
592e7edd2cfSChuanhong Guo ((u8 *)(op->data.buf.in))[i] = readb(reg);
593e7edd2cfSChuanhong Guo }
594e7edd2cfSChuanhong Guo }
595e7edd2cfSChuanhong Guo
596e7edd2cfSChuanhong Guo return 0;
597e7edd2cfSChuanhong Guo }
598e7edd2cfSChuanhong Guo
mtk_nor_exec_op(struct spi_mem * mem,const struct spi_mem_op * op)599afedb4b7SJason Yan static int mtk_nor_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
600881d1ee9SChuanhong Guo {
601*a3a77a42SYang Yingliang struct mtk_nor *sp = spi_controller_get_devdata(mem->spi->controller);
602881d1ee9SChuanhong Guo int ret;
603881d1ee9SChuanhong Guo
604881d1ee9SChuanhong Guo if ((op->data.nbytes == 0) ||
605881d1ee9SChuanhong Guo ((op->addr.nbytes != 3) && (op->addr.nbytes != 4)))
606e7edd2cfSChuanhong Guo return mtk_nor_spi_mem_prg(sp, op);
607881d1ee9SChuanhong Guo
608881d1ee9SChuanhong Guo if (op->data.dir == SPI_MEM_DATA_OUT) {
609881d1ee9SChuanhong Guo mtk_nor_set_addr(sp, op);
610881d1ee9SChuanhong Guo writeb(op->cmd.opcode, sp->base + MTK_NOR_REG_PRGDATA0);
611881d1ee9SChuanhong Guo if (op->data.nbytes == MTK_NOR_PP_SIZE)
612881d1ee9SChuanhong Guo return mtk_nor_pp_buffered(sp, op);
613881d1ee9SChuanhong Guo return mtk_nor_pp_unbuffered(sp, op);
614881d1ee9SChuanhong Guo }
615881d1ee9SChuanhong Guo
616881d1ee9SChuanhong Guo if ((op->data.dir == SPI_MEM_DATA_IN) && mtk_nor_match_read(op)) {
61763d9a4d8Sbayi cheng ret = mtk_nor_setup_write_buffer(sp, false);
618881d1ee9SChuanhong Guo if (ret < 0)
619881d1ee9SChuanhong Guo return ret;
620881d1ee9SChuanhong Guo mtk_nor_setup_bus(sp, op);
621881d1ee9SChuanhong Guo if (op->data.nbytes == 1) {
622881d1ee9SChuanhong Guo mtk_nor_set_addr(sp, op);
623881d1ee9SChuanhong Guo return mtk_nor_read_pio(sp, op);
624881d1ee9SChuanhong Guo } else {
6258330e9e8Sbayi cheng ret = mtk_nor_read_dma(sp, op);
6268330e9e8Sbayi cheng if (unlikely(ret)) {
6278330e9e8Sbayi cheng /* Handle rare bus glitch */
6288330e9e8Sbayi cheng mtk_nor_reset(sp);
6298330e9e8Sbayi cheng mtk_nor_setup_bus(sp, op);
630a1daaa99SIkjoon Jang return mtk_nor_read_dma(sp, op);
631881d1ee9SChuanhong Guo }
6328330e9e8Sbayi cheng
6338330e9e8Sbayi cheng return ret;
6348330e9e8Sbayi cheng }
635881d1ee9SChuanhong Guo }
636881d1ee9SChuanhong Guo
637e7edd2cfSChuanhong Guo return mtk_nor_spi_mem_prg(sp, op);
638881d1ee9SChuanhong Guo }
639881d1ee9SChuanhong Guo
mtk_nor_setup(struct spi_device * spi)640881d1ee9SChuanhong Guo static int mtk_nor_setup(struct spi_device *spi)
641881d1ee9SChuanhong Guo {
642*a3a77a42SYang Yingliang struct mtk_nor *sp = spi_controller_get_devdata(spi->controller);
643881d1ee9SChuanhong Guo
644881d1ee9SChuanhong Guo if (spi->max_speed_hz && (spi->max_speed_hz < sp->spi_freq)) {
645881d1ee9SChuanhong Guo dev_err(&spi->dev, "spi clock should be %u Hz.\n",
646881d1ee9SChuanhong Guo sp->spi_freq);
647881d1ee9SChuanhong Guo return -EINVAL;
648881d1ee9SChuanhong Guo }
649881d1ee9SChuanhong Guo spi->max_speed_hz = sp->spi_freq;
650881d1ee9SChuanhong Guo
651881d1ee9SChuanhong Guo return 0;
652881d1ee9SChuanhong Guo }
653881d1ee9SChuanhong Guo
mtk_nor_transfer_one_message(struct spi_controller * host,struct spi_message * m)654*a3a77a42SYang Yingliang static int mtk_nor_transfer_one_message(struct spi_controller *host,
655881d1ee9SChuanhong Guo struct spi_message *m)
656881d1ee9SChuanhong Guo {
657*a3a77a42SYang Yingliang struct mtk_nor *sp = spi_controller_get_devdata(host);
658881d1ee9SChuanhong Guo struct spi_transfer *t = NULL;
659881d1ee9SChuanhong Guo unsigned long trx_len = 0;
660881d1ee9SChuanhong Guo int stat = 0;
661881d1ee9SChuanhong Guo int reg_offset = MTK_NOR_REG_PRGDATA_MAX;
662881d1ee9SChuanhong Guo void __iomem *reg;
663881d1ee9SChuanhong Guo const u8 *txbuf;
664881d1ee9SChuanhong Guo u8 *rxbuf;
665881d1ee9SChuanhong Guo int i;
666881d1ee9SChuanhong Guo
667881d1ee9SChuanhong Guo list_for_each_entry(t, &m->transfers, transfer_list) {
668881d1ee9SChuanhong Guo txbuf = t->tx_buf;
669881d1ee9SChuanhong Guo for (i = 0; i < t->len; i++, reg_offset--) {
670881d1ee9SChuanhong Guo reg = sp->base + MTK_NOR_REG_PRGDATA(reg_offset);
671881d1ee9SChuanhong Guo if (txbuf)
672881d1ee9SChuanhong Guo writeb(txbuf[i], reg);
673881d1ee9SChuanhong Guo else
674881d1ee9SChuanhong Guo writeb(0, reg);
675881d1ee9SChuanhong Guo }
676881d1ee9SChuanhong Guo trx_len += t->len;
677881d1ee9SChuanhong Guo }
678881d1ee9SChuanhong Guo
679881d1ee9SChuanhong Guo writel(trx_len * BITS_PER_BYTE, sp->base + MTK_NOR_REG_PRG_CNT);
680881d1ee9SChuanhong Guo
681881d1ee9SChuanhong Guo stat = mtk_nor_cmd_exec(sp, MTK_NOR_CMD_PROGRAM,
682881d1ee9SChuanhong Guo trx_len * BITS_PER_BYTE);
683881d1ee9SChuanhong Guo if (stat < 0)
684881d1ee9SChuanhong Guo goto msg_done;
685881d1ee9SChuanhong Guo
686881d1ee9SChuanhong Guo reg_offset = trx_len - 1;
687881d1ee9SChuanhong Guo list_for_each_entry(t, &m->transfers, transfer_list) {
688881d1ee9SChuanhong Guo rxbuf = t->rx_buf;
689881d1ee9SChuanhong Guo for (i = 0; i < t->len; i++, reg_offset--) {
690881d1ee9SChuanhong Guo reg = sp->base + MTK_NOR_REG_SHIFT(reg_offset);
691881d1ee9SChuanhong Guo if (rxbuf)
692881d1ee9SChuanhong Guo rxbuf[i] = readb(reg);
693881d1ee9SChuanhong Guo }
694881d1ee9SChuanhong Guo }
695881d1ee9SChuanhong Guo
696881d1ee9SChuanhong Guo m->actual_length = trx_len;
697881d1ee9SChuanhong Guo msg_done:
698881d1ee9SChuanhong Guo m->status = stat;
699*a3a77a42SYang Yingliang spi_finalize_current_message(host);
700881d1ee9SChuanhong Guo
701881d1ee9SChuanhong Guo return 0;
702881d1ee9SChuanhong Guo }
703881d1ee9SChuanhong Guo
mtk_nor_disable_clk(struct mtk_nor * sp)704881d1ee9SChuanhong Guo static void mtk_nor_disable_clk(struct mtk_nor *sp)
705881d1ee9SChuanhong Guo {
706881d1ee9SChuanhong Guo clk_disable_unprepare(sp->spi_clk);
707881d1ee9SChuanhong Guo clk_disable_unprepare(sp->ctlr_clk);
708f32cce84Sbayi cheng clk_disable_unprepare(sp->axi_clk);
70958b0a653SGuochun Mao clk_disable_unprepare(sp->axi_s_clk);
710881d1ee9SChuanhong Guo }
711881d1ee9SChuanhong Guo
mtk_nor_enable_clk(struct mtk_nor * sp)712881d1ee9SChuanhong Guo static int mtk_nor_enable_clk(struct mtk_nor *sp)
713881d1ee9SChuanhong Guo {
714881d1ee9SChuanhong Guo int ret;
715881d1ee9SChuanhong Guo
716881d1ee9SChuanhong Guo ret = clk_prepare_enable(sp->spi_clk);
717881d1ee9SChuanhong Guo if (ret)
718881d1ee9SChuanhong Guo return ret;
719881d1ee9SChuanhong Guo
720881d1ee9SChuanhong Guo ret = clk_prepare_enable(sp->ctlr_clk);
721881d1ee9SChuanhong Guo if (ret) {
722881d1ee9SChuanhong Guo clk_disable_unprepare(sp->spi_clk);
723881d1ee9SChuanhong Guo return ret;
724881d1ee9SChuanhong Guo }
725881d1ee9SChuanhong Guo
726f32cce84Sbayi cheng ret = clk_prepare_enable(sp->axi_clk);
727f32cce84Sbayi cheng if (ret) {
728f32cce84Sbayi cheng clk_disable_unprepare(sp->spi_clk);
729f32cce84Sbayi cheng clk_disable_unprepare(sp->ctlr_clk);
730f32cce84Sbayi cheng return ret;
731f32cce84Sbayi cheng }
732f32cce84Sbayi cheng
73358b0a653SGuochun Mao ret = clk_prepare_enable(sp->axi_s_clk);
73458b0a653SGuochun Mao if (ret) {
73558b0a653SGuochun Mao clk_disable_unprepare(sp->spi_clk);
73658b0a653SGuochun Mao clk_disable_unprepare(sp->ctlr_clk);
73758b0a653SGuochun Mao clk_disable_unprepare(sp->axi_clk);
73858b0a653SGuochun Mao return ret;
73958b0a653SGuochun Mao }
74058b0a653SGuochun Mao
741881d1ee9SChuanhong Guo return 0;
742881d1ee9SChuanhong Guo }
743881d1ee9SChuanhong Guo
mtk_nor_init(struct mtk_nor * sp)7443bfd9103SIkjoon Jang static void mtk_nor_init(struct mtk_nor *sp)
745881d1ee9SChuanhong Guo {
7463bfd9103SIkjoon Jang writel(0, sp->base + MTK_NOR_REG_IRQ_EN);
7473bfd9103SIkjoon Jang writel(MTK_NOR_IRQ_MASK, sp->base + MTK_NOR_REG_IRQ_STAT);
748881d1ee9SChuanhong Guo
749881d1ee9SChuanhong Guo writel(MTK_NOR_ENABLE_SF_CMD, sp->base + MTK_NOR_REG_WP);
750881d1ee9SChuanhong Guo mtk_nor_rmw(sp, MTK_NOR_REG_CFG2, MTK_NOR_WR_CUSTOM_OP_EN, 0);
751881d1ee9SChuanhong Guo mtk_nor_rmw(sp, MTK_NOR_REG_CFG3,
752881d1ee9SChuanhong Guo MTK_NOR_DISABLE_WREN | MTK_NOR_DISABLE_SR_POLL, 0);
753881d1ee9SChuanhong Guo }
754881d1ee9SChuanhong Guo
mtk_nor_irq_handler(int irq,void * data)755881d1ee9SChuanhong Guo static irqreturn_t mtk_nor_irq_handler(int irq, void *data)
756881d1ee9SChuanhong Guo {
757881d1ee9SChuanhong Guo struct mtk_nor *sp = data;
758881d1ee9SChuanhong Guo u32 irq_status, irq_enabled;
759881d1ee9SChuanhong Guo
760881d1ee9SChuanhong Guo irq_status = readl(sp->base + MTK_NOR_REG_IRQ_STAT);
761881d1ee9SChuanhong Guo irq_enabled = readl(sp->base + MTK_NOR_REG_IRQ_EN);
762881d1ee9SChuanhong Guo // write status back to clear interrupt
763881d1ee9SChuanhong Guo writel(irq_status, sp->base + MTK_NOR_REG_IRQ_STAT);
764881d1ee9SChuanhong Guo
765881d1ee9SChuanhong Guo if (!(irq_status & irq_enabled))
766881d1ee9SChuanhong Guo return IRQ_NONE;
767881d1ee9SChuanhong Guo
768881d1ee9SChuanhong Guo if (irq_status & MTK_NOR_IRQ_DMA) {
769881d1ee9SChuanhong Guo complete(&sp->op_done);
770881d1ee9SChuanhong Guo writel(0, sp->base + MTK_NOR_REG_IRQ_EN);
771881d1ee9SChuanhong Guo }
772881d1ee9SChuanhong Guo
773881d1ee9SChuanhong Guo return IRQ_HANDLED;
774881d1ee9SChuanhong Guo }
775881d1ee9SChuanhong Guo
mtk_max_msg_size(struct spi_device * spi)776881d1ee9SChuanhong Guo static size_t mtk_max_msg_size(struct spi_device *spi)
777881d1ee9SChuanhong Guo {
778881d1ee9SChuanhong Guo return MTK_NOR_PRG_MAX_SIZE;
779881d1ee9SChuanhong Guo }
780881d1ee9SChuanhong Guo
781881d1ee9SChuanhong Guo static const struct spi_controller_mem_ops mtk_nor_mem_ops = {
782881d1ee9SChuanhong Guo .adjust_op_size = mtk_nor_adjust_op_size,
783881d1ee9SChuanhong Guo .supports_op = mtk_nor_supports_op,
784881d1ee9SChuanhong Guo .exec_op = mtk_nor_exec_op
785881d1ee9SChuanhong Guo };
786881d1ee9SChuanhong Guo
787474fc2e6SGuochun Mao static const struct mtk_nor_caps mtk_nor_caps_mt8173 = {
7885b177234SGuochun Mao .dma_bits = 32,
7895b177234SGuochun Mao .extra_dummy_bit = 0,
7905b177234SGuochun Mao };
7915b177234SGuochun Mao
792474fc2e6SGuochun Mao static const struct mtk_nor_caps mtk_nor_caps_mt8186 = {
7934e8bfe5cSGuochun Mao .dma_bits = 32,
7944e8bfe5cSGuochun Mao .extra_dummy_bit = 1,
7954e8bfe5cSGuochun Mao };
7964e8bfe5cSGuochun Mao
797474fc2e6SGuochun Mao static const struct mtk_nor_caps mtk_nor_caps_mt8192 = {
7985b177234SGuochun Mao .dma_bits = 36,
7995b177234SGuochun Mao .extra_dummy_bit = 0,
8005b177234SGuochun Mao };
8015b177234SGuochun Mao
802881d1ee9SChuanhong Guo static const struct of_device_id mtk_nor_match[] = {
8035b177234SGuochun Mao { .compatible = "mediatek,mt8173-nor", .data = &mtk_nor_caps_mt8173 },
8044e8bfe5cSGuochun Mao { .compatible = "mediatek,mt8186-nor", .data = &mtk_nor_caps_mt8186 },
8055b177234SGuochun Mao { .compatible = "mediatek,mt8192-nor", .data = &mtk_nor_caps_mt8192 },
806881d1ee9SChuanhong Guo { /* sentinel */ }
807881d1ee9SChuanhong Guo };
808881d1ee9SChuanhong Guo MODULE_DEVICE_TABLE(of, mtk_nor_match);
809881d1ee9SChuanhong Guo
mtk_nor_probe(struct platform_device * pdev)810881d1ee9SChuanhong Guo static int mtk_nor_probe(struct platform_device *pdev)
811881d1ee9SChuanhong Guo {
812881d1ee9SChuanhong Guo struct spi_controller *ctlr;
813881d1ee9SChuanhong Guo struct mtk_nor *sp;
8145b177234SGuochun Mao struct mtk_nor_caps *caps;
815881d1ee9SChuanhong Guo void __iomem *base;
81658b0a653SGuochun Mao struct clk *spi_clk, *ctlr_clk, *axi_clk, *axi_s_clk;
817881d1ee9SChuanhong Guo int ret, irq;
818881d1ee9SChuanhong Guo
819881d1ee9SChuanhong Guo base = devm_platform_ioremap_resource(pdev, 0);
820881d1ee9SChuanhong Guo if (IS_ERR(base))
821881d1ee9SChuanhong Guo return PTR_ERR(base);
822881d1ee9SChuanhong Guo
823881d1ee9SChuanhong Guo spi_clk = devm_clk_get(&pdev->dev, "spi");
824881d1ee9SChuanhong Guo if (IS_ERR(spi_clk))
825881d1ee9SChuanhong Guo return PTR_ERR(spi_clk);
826881d1ee9SChuanhong Guo
827881d1ee9SChuanhong Guo ctlr_clk = devm_clk_get(&pdev->dev, "sf");
828881d1ee9SChuanhong Guo if (IS_ERR(ctlr_clk))
829881d1ee9SChuanhong Guo return PTR_ERR(ctlr_clk);
830881d1ee9SChuanhong Guo
831f32cce84Sbayi cheng axi_clk = devm_clk_get_optional(&pdev->dev, "axi");
832f32cce84Sbayi cheng if (IS_ERR(axi_clk))
833f32cce84Sbayi cheng return PTR_ERR(axi_clk);
834f32cce84Sbayi cheng
83558b0a653SGuochun Mao axi_s_clk = devm_clk_get_optional(&pdev->dev, "axi_s");
83658b0a653SGuochun Mao if (IS_ERR(axi_s_clk))
83758b0a653SGuochun Mao return PTR_ERR(axi_s_clk);
83858b0a653SGuochun Mao
8395b177234SGuochun Mao caps = (struct mtk_nor_caps *)of_device_get_match_data(&pdev->dev);
8405b177234SGuochun Mao
8415b177234SGuochun Mao ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(caps->dma_bits));
8425b177234SGuochun Mao if (ret) {
8435b177234SGuochun Mao dev_err(&pdev->dev, "failed to set dma mask(%u)\n", caps->dma_bits);
8445b177234SGuochun Mao return ret;
845e836d4cfSIkjoon Jang }
846e836d4cfSIkjoon Jang
847*a3a77a42SYang Yingliang ctlr = devm_spi_alloc_host(&pdev->dev, sizeof(*sp));
848881d1ee9SChuanhong Guo if (!ctlr) {
849881d1ee9SChuanhong Guo dev_err(&pdev->dev, "failed to allocate spi controller\n");
850881d1ee9SChuanhong Guo return -ENOMEM;
851881d1ee9SChuanhong Guo }
852881d1ee9SChuanhong Guo
853881d1ee9SChuanhong Guo ctlr->bits_per_word_mask = SPI_BPW_MASK(8);
854881d1ee9SChuanhong Guo ctlr->dev.of_node = pdev->dev.of_node;
855881d1ee9SChuanhong Guo ctlr->max_message_size = mtk_max_msg_size;
856881d1ee9SChuanhong Guo ctlr->mem_ops = &mtk_nor_mem_ops;
857881d1ee9SChuanhong Guo ctlr->mode_bits = SPI_RX_DUAL | SPI_RX_QUAD | SPI_TX_DUAL | SPI_TX_QUAD;
858881d1ee9SChuanhong Guo ctlr->num_chipselect = 1;
859881d1ee9SChuanhong Guo ctlr->setup = mtk_nor_setup;
860881d1ee9SChuanhong Guo ctlr->transfer_one_message = mtk_nor_transfer_one_message;
8613bfd9103SIkjoon Jang ctlr->auto_runtime_pm = true;
862881d1ee9SChuanhong Guo
863881d1ee9SChuanhong Guo dev_set_drvdata(&pdev->dev, ctlr);
864881d1ee9SChuanhong Guo
865881d1ee9SChuanhong Guo sp = spi_controller_get_devdata(ctlr);
866881d1ee9SChuanhong Guo sp->base = base;
867881d1ee9SChuanhong Guo sp->has_irq = false;
868881d1ee9SChuanhong Guo sp->wbuf_en = false;
869881d1ee9SChuanhong Guo sp->ctlr = ctlr;
870881d1ee9SChuanhong Guo sp->dev = &pdev->dev;
871881d1ee9SChuanhong Guo sp->spi_clk = spi_clk;
872881d1ee9SChuanhong Guo sp->ctlr_clk = ctlr_clk;
873f32cce84Sbayi cheng sp->axi_clk = axi_clk;
87458b0a653SGuochun Mao sp->axi_s_clk = axi_s_clk;
8755b177234SGuochun Mao sp->caps = caps;
8765b177234SGuochun Mao sp->high_dma = caps->dma_bits > 32;
877a1daaa99SIkjoon Jang sp->buffer = dmam_alloc_coherent(&pdev->dev,
878a1daaa99SIkjoon Jang MTK_NOR_BOUNCE_BUF_SIZE + MTK_NOR_DMA_ALIGN,
879a1daaa99SIkjoon Jang &sp->buffer_dma, GFP_KERNEL);
880a1daaa99SIkjoon Jang if (!sp->buffer)
881a1daaa99SIkjoon Jang return -ENOMEM;
882a1daaa99SIkjoon Jang
883a1daaa99SIkjoon Jang if ((uintptr_t)sp->buffer & MTK_NOR_DMA_ALIGN_MASK) {
884a1daaa99SIkjoon Jang dev_err(sp->dev, "misaligned allocation of internal buffer.\n");
885a1daaa99SIkjoon Jang return -ENOMEM;
886a1daaa99SIkjoon Jang }
887881d1ee9SChuanhong Guo
8883bfd9103SIkjoon Jang ret = mtk_nor_enable_clk(sp);
8893bfd9103SIkjoon Jang if (ret < 0)
8903bfd9103SIkjoon Jang return ret;
8913bfd9103SIkjoon Jang
8923bfd9103SIkjoon Jang sp->spi_freq = clk_get_rate(sp->spi_clk);
8933bfd9103SIkjoon Jang
8943bfd9103SIkjoon Jang mtk_nor_init(sp);
8953bfd9103SIkjoon Jang
896881d1ee9SChuanhong Guo irq = platform_get_irq_optional(pdev, 0);
8973bfd9103SIkjoon Jang
898881d1ee9SChuanhong Guo if (irq < 0) {
899881d1ee9SChuanhong Guo dev_warn(sp->dev, "IRQ not available.");
900881d1ee9SChuanhong Guo } else {
901881d1ee9SChuanhong Guo ret = devm_request_irq(sp->dev, irq, mtk_nor_irq_handler, 0,
902881d1ee9SChuanhong Guo pdev->name, sp);
903881d1ee9SChuanhong Guo if (ret < 0) {
904881d1ee9SChuanhong Guo dev_warn(sp->dev, "failed to request IRQ.");
905881d1ee9SChuanhong Guo } else {
906881d1ee9SChuanhong Guo init_completion(&sp->op_done);
907881d1ee9SChuanhong Guo sp->has_irq = true;
908881d1ee9SChuanhong Guo }
909881d1ee9SChuanhong Guo }
910881d1ee9SChuanhong Guo
9113bfd9103SIkjoon Jang pm_runtime_set_autosuspend_delay(&pdev->dev, -1);
9123bfd9103SIkjoon Jang pm_runtime_use_autosuspend(&pdev->dev);
9133bfd9103SIkjoon Jang pm_runtime_set_active(&pdev->dev);
9143bfd9103SIkjoon Jang pm_runtime_enable(&pdev->dev);
9153bfd9103SIkjoon Jang pm_runtime_get_noresume(&pdev->dev);
9163bfd9103SIkjoon Jang
9173bfd9103SIkjoon Jang ret = devm_spi_register_controller(&pdev->dev, ctlr);
9183bfd9103SIkjoon Jang if (ret < 0)
9193bfd9103SIkjoon Jang goto err_probe;
9203bfd9103SIkjoon Jang
9213bfd9103SIkjoon Jang pm_runtime_mark_last_busy(&pdev->dev);
9223bfd9103SIkjoon Jang pm_runtime_put_autosuspend(&pdev->dev);
923881d1ee9SChuanhong Guo
924881d1ee9SChuanhong Guo dev_info(&pdev->dev, "spi frequency: %d Hz\n", sp->spi_freq);
925881d1ee9SChuanhong Guo
9263bfd9103SIkjoon Jang return 0;
9273bfd9103SIkjoon Jang
9283bfd9103SIkjoon Jang err_probe:
9293bfd9103SIkjoon Jang pm_runtime_disable(&pdev->dev);
9303bfd9103SIkjoon Jang pm_runtime_set_suspended(&pdev->dev);
9313bfd9103SIkjoon Jang pm_runtime_dont_use_autosuspend(&pdev->dev);
9323bfd9103SIkjoon Jang
9333bfd9103SIkjoon Jang mtk_nor_disable_clk(sp);
9343bfd9103SIkjoon Jang
9353bfd9103SIkjoon Jang return ret;
936881d1ee9SChuanhong Guo }
937881d1ee9SChuanhong Guo
mtk_nor_remove(struct platform_device * pdev)938b5584358SUwe Kleine-König static void mtk_nor_remove(struct platform_device *pdev)
939881d1ee9SChuanhong Guo {
9403bfd9103SIkjoon Jang struct spi_controller *ctlr = dev_get_drvdata(&pdev->dev);
9413bfd9103SIkjoon Jang struct mtk_nor *sp = spi_controller_get_devdata(ctlr);
942881d1ee9SChuanhong Guo
9433bfd9103SIkjoon Jang pm_runtime_disable(&pdev->dev);
9443bfd9103SIkjoon Jang pm_runtime_set_suspended(&pdev->dev);
9453bfd9103SIkjoon Jang pm_runtime_dont_use_autosuspend(&pdev->dev);
946881d1ee9SChuanhong Guo
947881d1ee9SChuanhong Guo mtk_nor_disable_clk(sp);
948881d1ee9SChuanhong Guo }
949881d1ee9SChuanhong Guo
mtk_nor_runtime_suspend(struct device * dev)9503bfd9103SIkjoon Jang static int __maybe_unused mtk_nor_runtime_suspend(struct device *dev)
9513bfd9103SIkjoon Jang {
9523bfd9103SIkjoon Jang struct spi_controller *ctlr = dev_get_drvdata(dev);
9533bfd9103SIkjoon Jang struct mtk_nor *sp = spi_controller_get_devdata(ctlr);
9543bfd9103SIkjoon Jang
9553bfd9103SIkjoon Jang mtk_nor_disable_clk(sp);
9563bfd9103SIkjoon Jang
9573bfd9103SIkjoon Jang return 0;
9583bfd9103SIkjoon Jang }
9593bfd9103SIkjoon Jang
mtk_nor_runtime_resume(struct device * dev)9603bfd9103SIkjoon Jang static int __maybe_unused mtk_nor_runtime_resume(struct device *dev)
9613bfd9103SIkjoon Jang {
9623bfd9103SIkjoon Jang struct spi_controller *ctlr = dev_get_drvdata(dev);
9633bfd9103SIkjoon Jang struct mtk_nor *sp = spi_controller_get_devdata(ctlr);
9643bfd9103SIkjoon Jang
9653bfd9103SIkjoon Jang return mtk_nor_enable_clk(sp);
9663bfd9103SIkjoon Jang }
9673bfd9103SIkjoon Jang
mtk_nor_suspend(struct device * dev)9683bfd9103SIkjoon Jang static int __maybe_unused mtk_nor_suspend(struct device *dev)
9693bfd9103SIkjoon Jang {
9703bfd9103SIkjoon Jang return pm_runtime_force_suspend(dev);
9713bfd9103SIkjoon Jang }
9723bfd9103SIkjoon Jang
mtk_nor_resume(struct device * dev)9733bfd9103SIkjoon Jang static int __maybe_unused mtk_nor_resume(struct device *dev)
9743bfd9103SIkjoon Jang {
975317c2045SAllen-KH Cheng struct spi_controller *ctlr = dev_get_drvdata(dev);
976317c2045SAllen-KH Cheng struct mtk_nor *sp = spi_controller_get_devdata(ctlr);
977317c2045SAllen-KH Cheng int ret;
978317c2045SAllen-KH Cheng
979317c2045SAllen-KH Cheng ret = pm_runtime_force_resume(dev);
980317c2045SAllen-KH Cheng if (ret)
981317c2045SAllen-KH Cheng return ret;
982317c2045SAllen-KH Cheng
983317c2045SAllen-KH Cheng mtk_nor_init(sp);
984317c2045SAllen-KH Cheng
985317c2045SAllen-KH Cheng return 0;
9863bfd9103SIkjoon Jang }
9873bfd9103SIkjoon Jang
9883bfd9103SIkjoon Jang static const struct dev_pm_ops mtk_nor_pm_ops = {
9893bfd9103SIkjoon Jang SET_RUNTIME_PM_OPS(mtk_nor_runtime_suspend,
9903bfd9103SIkjoon Jang mtk_nor_runtime_resume, NULL)
9913bfd9103SIkjoon Jang SET_SYSTEM_SLEEP_PM_OPS(mtk_nor_suspend, mtk_nor_resume)
9923bfd9103SIkjoon Jang };
9933bfd9103SIkjoon Jang
994881d1ee9SChuanhong Guo static struct platform_driver mtk_nor_driver = {
995881d1ee9SChuanhong Guo .driver = {
996881d1ee9SChuanhong Guo .name = DRIVER_NAME,
997881d1ee9SChuanhong Guo .of_match_table = mtk_nor_match,
9983bfd9103SIkjoon Jang .pm = &mtk_nor_pm_ops,
999881d1ee9SChuanhong Guo },
1000881d1ee9SChuanhong Guo .probe = mtk_nor_probe,
1001b5584358SUwe Kleine-König .remove_new = mtk_nor_remove,
1002881d1ee9SChuanhong Guo };
1003881d1ee9SChuanhong Guo
1004881d1ee9SChuanhong Guo module_platform_driver(mtk_nor_driver);
1005881d1ee9SChuanhong Guo
1006881d1ee9SChuanhong Guo MODULE_DESCRIPTION("Mediatek SPI NOR controller driver");
1007881d1ee9SChuanhong Guo MODULE_AUTHOR("Chuanhong Guo <gch981213@gmail.com>");
1008881d1ee9SChuanhong Guo MODULE_LICENSE("GPL v2");
1009881d1ee9SChuanhong Guo MODULE_ALIAS("platform:" DRIVER_NAME);
1010