1 // SPDX-License-Identifier: GPL-2.0
3 // Mediatek SPI NOR controller driver
5 // Copyright (C) 2020 Chuanhong Guo <gch981213@gmail.com>
7 #include <linux/bits.h>
9 #include <linux/completion.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/interrupt.h>
13 #include <linux/iopoll.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/of_device.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/spi/spi.h>
19 #include <linux/spi/spi-mem.h>
20 #include <linux/string.h>
22 #define DRIVER_NAME "mtk-spi-nor"
24 #define MTK_NOR_REG_CMD 0x00
25 #define MTK_NOR_CMD_WRITE BIT(4)
26 #define MTK_NOR_CMD_PROGRAM BIT(2)
27 #define MTK_NOR_CMD_READ BIT(0)
28 #define MTK_NOR_CMD_MASK GENMASK(5, 0)
30 #define MTK_NOR_REG_PRG_CNT 0x04
31 #define MTK_NOR_PRG_CNT_MAX 56
32 #define MTK_NOR_REG_RDATA 0x0c
34 #define MTK_NOR_REG_RADR0 0x10
35 #define MTK_NOR_REG_RADR(n) (MTK_NOR_REG_RADR0 + 4 * (n))
36 #define MTK_NOR_REG_RADR3 0xc8
38 #define MTK_NOR_REG_WDATA 0x1c
40 #define MTK_NOR_REG_PRGDATA0 0x20
41 #define MTK_NOR_REG_PRGDATA(n) (MTK_NOR_REG_PRGDATA0 + 4 * (n))
42 #define MTK_NOR_REG_PRGDATA_MAX 5
44 #define MTK_NOR_REG_SHIFT0 0x38
45 #define MTK_NOR_REG_SHIFT(n) (MTK_NOR_REG_SHIFT0 + 4 * (n))
46 #define MTK_NOR_REG_SHIFT_MAX 9
48 #define MTK_NOR_REG_CFG1 0x60
49 #define MTK_NOR_FAST_READ BIT(0)
51 #define MTK_NOR_REG_CFG2 0x64
52 #define MTK_NOR_WR_CUSTOM_OP_EN BIT(4)
53 #define MTK_NOR_WR_BUF_EN BIT(0)
55 #define MTK_NOR_REG_PP_DATA 0x98
57 #define MTK_NOR_REG_IRQ_STAT 0xa8
58 #define MTK_NOR_REG_IRQ_EN 0xac
59 #define MTK_NOR_IRQ_DMA BIT(7)
60 #define MTK_NOR_IRQ_MASK GENMASK(7, 0)
62 #define MTK_NOR_REG_CFG3 0xb4
63 #define MTK_NOR_DISABLE_WREN BIT(7)
64 #define MTK_NOR_DISABLE_SR_POLL BIT(5)
66 #define MTK_NOR_REG_WP 0xc4
67 #define MTK_NOR_ENABLE_SF_CMD 0x30
69 #define MTK_NOR_REG_BUSCFG 0xcc
70 #define MTK_NOR_4B_ADDR BIT(4)
71 #define MTK_NOR_QUAD_ADDR BIT(3)
72 #define MTK_NOR_QUAD_READ BIT(2)
73 #define MTK_NOR_DUAL_ADDR BIT(1)
74 #define MTK_NOR_DUAL_READ BIT(0)
75 #define MTK_NOR_BUS_MODE_MASK GENMASK(4, 0)
77 #define MTK_NOR_REG_DMA_CTL 0x718
78 #define MTK_NOR_DMA_START BIT(0)
80 #define MTK_NOR_REG_DMA_FADR 0x71c
81 #define MTK_NOR_REG_DMA_DADR 0x720
82 #define MTK_NOR_REG_DMA_END_DADR 0x724
83 #define MTK_NOR_REG_DMA_DADR_HB 0x738
84 #define MTK_NOR_REG_DMA_END_DADR_HB 0x73c
86 #define MTK_NOR_PRG_MAX_SIZE 6
87 // Reading DMA src/dst addresses have to be 16-byte aligned
88 #define MTK_NOR_DMA_ALIGN 16
89 #define MTK_NOR_DMA_ALIGN_MASK (MTK_NOR_DMA_ALIGN - 1)
90 // and we allocate a bounce buffer if destination address isn't aligned.
91 #define MTK_NOR_BOUNCE_BUF_SIZE PAGE_SIZE
93 // Buffered page program can do one 128-byte transfer
94 #define MTK_NOR_PP_SIZE 128
96 #define CLK_TO_US(sp, clkcnt) DIV_ROUND_UP(clkcnt, sp->spi_freq / 1000000)
101 /* extra_dummy_bit is adding for the IP of new SoCs.
102 * Some new SoCs modify the timing of fetching registers' values
103 * and IDs of nor flash, they need a extra_dummy_bit which can add
104 * more clock cycles for fetching data.
110 struct spi_controller *ctlr;
114 dma_addr_t buffer_dma;
116 struct clk *ctlr_clk;
118 struct clk *axi_s_clk;
119 unsigned int spi_freq;
123 struct completion op_done;
124 const struct mtk_nor_caps *caps;
127 static inline void mtk_nor_rmw(struct mtk_nor *sp, u32 reg, u32 set, u32 clr)
129 u32 val = readl(sp->base + reg);
133 writel(val, sp->base + reg);
136 static inline int mtk_nor_cmd_exec(struct mtk_nor *sp, u32 cmd, ulong clk)
138 ulong delay = CLK_TO_US(sp, clk);
142 writel(cmd, sp->base + MTK_NOR_REG_CMD);
143 ret = readl_poll_timeout(sp->base + MTK_NOR_REG_CMD, reg, !(reg & cmd),
144 delay / 3, (delay + 1) * 200);
146 dev_err(sp->dev, "command %u timeout.\n", cmd);
150 static void mtk_nor_set_addr(struct mtk_nor *sp, const struct spi_mem_op *op)
152 u32 addr = op->addr.val;
155 for (i = 0; i < 3; i++) {
156 writeb(addr & 0xff, sp->base + MTK_NOR_REG_RADR(i));
159 if (op->addr.nbytes == 4) {
160 writeb(addr & 0xff, sp->base + MTK_NOR_REG_RADR3);
161 mtk_nor_rmw(sp, MTK_NOR_REG_BUSCFG, MTK_NOR_4B_ADDR, 0);
163 mtk_nor_rmw(sp, MTK_NOR_REG_BUSCFG, 0, MTK_NOR_4B_ADDR);
167 static bool need_bounce(struct mtk_nor *sp, const struct spi_mem_op *op)
169 return ((uintptr_t)op->data.buf.in & MTK_NOR_DMA_ALIGN_MASK);
172 static bool mtk_nor_match_read(const struct spi_mem_op *op)
176 if (op->dummy.nbytes)
177 dummy = op->dummy.nbytes * BITS_PER_BYTE / op->dummy.buswidth;
179 if ((op->data.buswidth == 2) || (op->data.buswidth == 4)) {
180 if (op->addr.buswidth == 1)
182 else if (op->addr.buswidth == 2)
184 else if (op->addr.buswidth == 4)
186 } else if ((op->addr.buswidth == 1) && (op->data.buswidth == 1)) {
187 if (op->cmd.opcode == 0x03)
189 else if (op->cmd.opcode == 0x0b)
195 static bool mtk_nor_match_prg(const struct spi_mem_op *op)
197 int tx_len, rx_len, prg_len, prg_left;
199 // prg mode is spi-only.
200 if ((op->cmd.buswidth > 1) || (op->addr.buswidth > 1) ||
201 (op->dummy.buswidth > 1) || (op->data.buswidth > 1))
204 tx_len = op->cmd.nbytes + op->addr.nbytes;
206 if (op->data.dir == SPI_MEM_DATA_OUT) {
207 // count dummy bytes only if we need to write data after it
208 tx_len += op->dummy.nbytes;
210 // leave at least one byte for data
211 if (tx_len > MTK_NOR_REG_PRGDATA_MAX)
214 // if there's no addr, meaning adjust_op_size is impossible,
215 // check data length as well.
216 if ((!op->addr.nbytes) &&
217 (tx_len + op->data.nbytes > MTK_NOR_REG_PRGDATA_MAX + 1))
219 } else if (op->data.dir == SPI_MEM_DATA_IN) {
220 if (tx_len > MTK_NOR_REG_PRGDATA_MAX + 1)
223 rx_len = op->data.nbytes;
224 prg_left = MTK_NOR_PRG_CNT_MAX / 8 - tx_len - op->dummy.nbytes;
225 if (prg_left > MTK_NOR_REG_SHIFT_MAX + 1)
226 prg_left = MTK_NOR_REG_SHIFT_MAX + 1;
227 if (rx_len > prg_left) {
228 if (!op->addr.nbytes)
233 prg_len = tx_len + op->dummy.nbytes + rx_len;
234 if (prg_len > MTK_NOR_PRG_CNT_MAX / 8)
237 prg_len = tx_len + op->dummy.nbytes;
238 if (prg_len > MTK_NOR_PRG_CNT_MAX / 8)
244 static void mtk_nor_adj_prg_size(struct spi_mem_op *op)
246 int tx_len, tx_left, prg_left;
248 tx_len = op->cmd.nbytes + op->addr.nbytes;
249 if (op->data.dir == SPI_MEM_DATA_OUT) {
250 tx_len += op->dummy.nbytes;
251 tx_left = MTK_NOR_REG_PRGDATA_MAX + 1 - tx_len;
252 if (op->data.nbytes > tx_left)
253 op->data.nbytes = tx_left;
254 } else if (op->data.dir == SPI_MEM_DATA_IN) {
255 prg_left = MTK_NOR_PRG_CNT_MAX / 8 - tx_len - op->dummy.nbytes;
256 if (prg_left > MTK_NOR_REG_SHIFT_MAX + 1)
257 prg_left = MTK_NOR_REG_SHIFT_MAX + 1;
258 if (op->data.nbytes > prg_left)
259 op->data.nbytes = prg_left;
263 static int mtk_nor_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op)
265 struct mtk_nor *sp = spi_controller_get_devdata(mem->spi->master);
267 if (!op->data.nbytes)
270 if ((op->addr.nbytes == 3) || (op->addr.nbytes == 4)) {
271 if ((op->data.dir == SPI_MEM_DATA_IN) &&
272 mtk_nor_match_read(op)) {
273 // limit size to prevent timeout calculation overflow
274 if (op->data.nbytes > 0x400000)
275 op->data.nbytes = 0x400000;
277 if ((op->addr.val & MTK_NOR_DMA_ALIGN_MASK) ||
278 (op->data.nbytes < MTK_NOR_DMA_ALIGN))
280 else if (!need_bounce(sp, op))
281 op->data.nbytes &= ~MTK_NOR_DMA_ALIGN_MASK;
282 else if (op->data.nbytes > MTK_NOR_BOUNCE_BUF_SIZE)
283 op->data.nbytes = MTK_NOR_BOUNCE_BUF_SIZE;
285 } else if (op->data.dir == SPI_MEM_DATA_OUT) {
286 if (op->data.nbytes >= MTK_NOR_PP_SIZE)
287 op->data.nbytes = MTK_NOR_PP_SIZE;
294 mtk_nor_adj_prg_size(op);
298 static bool mtk_nor_supports_op(struct spi_mem *mem,
299 const struct spi_mem_op *op)
301 if (!spi_mem_default_supports_op(mem, op))
304 if (op->cmd.buswidth != 1)
307 if ((op->addr.nbytes == 3) || (op->addr.nbytes == 4)) {
308 switch (op->data.dir) {
309 case SPI_MEM_DATA_IN:
310 if (mtk_nor_match_read(op))
313 case SPI_MEM_DATA_OUT:
314 if ((op->addr.buswidth == 1) &&
315 (op->dummy.nbytes == 0) &&
316 (op->data.buswidth == 1))
324 return mtk_nor_match_prg(op);
327 static void mtk_nor_setup_bus(struct mtk_nor *sp, const struct spi_mem_op *op)
331 if (op->addr.nbytes == 4)
332 reg |= MTK_NOR_4B_ADDR;
334 if (op->data.buswidth == 4) {
335 reg |= MTK_NOR_QUAD_READ;
336 writeb(op->cmd.opcode, sp->base + MTK_NOR_REG_PRGDATA(4));
337 if (op->addr.buswidth == 4)
338 reg |= MTK_NOR_QUAD_ADDR;
339 } else if (op->data.buswidth == 2) {
340 reg |= MTK_NOR_DUAL_READ;
341 writeb(op->cmd.opcode, sp->base + MTK_NOR_REG_PRGDATA(3));
342 if (op->addr.buswidth == 2)
343 reg |= MTK_NOR_DUAL_ADDR;
345 if (op->cmd.opcode == 0x0b)
346 mtk_nor_rmw(sp, MTK_NOR_REG_CFG1, MTK_NOR_FAST_READ, 0);
348 mtk_nor_rmw(sp, MTK_NOR_REG_CFG1, 0, MTK_NOR_FAST_READ);
350 mtk_nor_rmw(sp, MTK_NOR_REG_BUSCFG, reg, MTK_NOR_BUS_MODE_MASK);
353 static int mtk_nor_dma_exec(struct mtk_nor *sp, u32 from, unsigned int length,
360 writel(from, sp->base + MTK_NOR_REG_DMA_FADR);
361 writel(dma_addr, sp->base + MTK_NOR_REG_DMA_DADR);
362 writel(dma_addr + length, sp->base + MTK_NOR_REG_DMA_END_DADR);
365 writel(upper_32_bits(dma_addr),
366 sp->base + MTK_NOR_REG_DMA_DADR_HB);
367 writel(upper_32_bits(dma_addr + length),
368 sp->base + MTK_NOR_REG_DMA_END_DADR_HB);
372 reinit_completion(&sp->op_done);
373 mtk_nor_rmw(sp, MTK_NOR_REG_IRQ_EN, MTK_NOR_IRQ_DMA, 0);
376 mtk_nor_rmw(sp, MTK_NOR_REG_DMA_CTL, MTK_NOR_DMA_START, 0);
378 delay = CLK_TO_US(sp, (length + 5) * BITS_PER_BYTE);
381 if (!wait_for_completion_timeout(&sp->op_done,
385 ret = readl_poll_timeout(sp->base + MTK_NOR_REG_DMA_CTL, reg,
386 !(reg & MTK_NOR_DMA_START), delay / 3,
391 dev_err(sp->dev, "dma read timeout.\n");
396 static int mtk_nor_read_bounce(struct mtk_nor *sp, const struct spi_mem_op *op)
401 if (op->data.nbytes & MTK_NOR_DMA_ALIGN_MASK)
402 rdlen = (op->data.nbytes + MTK_NOR_DMA_ALIGN) & ~MTK_NOR_DMA_ALIGN_MASK;
404 rdlen = op->data.nbytes;
406 ret = mtk_nor_dma_exec(sp, op->addr.val, rdlen, sp->buffer_dma);
409 memcpy(op->data.buf.in, sp->buffer, op->data.nbytes);
414 static int mtk_nor_read_dma(struct mtk_nor *sp, const struct spi_mem_op *op)
419 if (need_bounce(sp, op))
420 return mtk_nor_read_bounce(sp, op);
422 dma_addr = dma_map_single(sp->dev, op->data.buf.in,
423 op->data.nbytes, DMA_FROM_DEVICE);
425 if (dma_mapping_error(sp->dev, dma_addr))
428 ret = mtk_nor_dma_exec(sp, op->addr.val, op->data.nbytes, dma_addr);
430 dma_unmap_single(sp->dev, dma_addr, op->data.nbytes, DMA_FROM_DEVICE);
435 static int mtk_nor_read_pio(struct mtk_nor *sp, const struct spi_mem_op *op)
437 u8 *buf = op->data.buf.in;
440 ret = mtk_nor_cmd_exec(sp, MTK_NOR_CMD_READ, 6 * BITS_PER_BYTE);
442 buf[0] = readb(sp->base + MTK_NOR_REG_RDATA);
446 static int mtk_nor_write_buffer_enable(struct mtk_nor *sp)
454 val = readl(sp->base + MTK_NOR_REG_CFG2);
455 writel(val | MTK_NOR_WR_BUF_EN, sp->base + MTK_NOR_REG_CFG2);
456 ret = readl_poll_timeout(sp->base + MTK_NOR_REG_CFG2, val,
457 val & MTK_NOR_WR_BUF_EN, 0, 10000);
463 static int mtk_nor_write_buffer_disable(struct mtk_nor *sp)
470 val = readl(sp->base + MTK_NOR_REG_CFG2);
471 writel(val & ~MTK_NOR_WR_BUF_EN, sp->base + MTK_NOR_REG_CFG2);
472 ret = readl_poll_timeout(sp->base + MTK_NOR_REG_CFG2, val,
473 !(val & MTK_NOR_WR_BUF_EN), 0, 10000);
479 static int mtk_nor_pp_buffered(struct mtk_nor *sp, const struct spi_mem_op *op)
481 const u8 *buf = op->data.buf.out;
485 ret = mtk_nor_write_buffer_enable(sp);
489 for (i = 0; i < op->data.nbytes; i += 4) {
490 val = buf[i + 3] << 24 | buf[i + 2] << 16 | buf[i + 1] << 8 |
492 writel(val, sp->base + MTK_NOR_REG_PP_DATA);
494 return mtk_nor_cmd_exec(sp, MTK_NOR_CMD_WRITE,
495 (op->data.nbytes + 5) * BITS_PER_BYTE);
498 static int mtk_nor_pp_unbuffered(struct mtk_nor *sp,
499 const struct spi_mem_op *op)
501 const u8 *buf = op->data.buf.out;
504 ret = mtk_nor_write_buffer_disable(sp);
507 writeb(buf[0], sp->base + MTK_NOR_REG_WDATA);
508 return mtk_nor_cmd_exec(sp, MTK_NOR_CMD_WRITE, 6 * BITS_PER_BYTE);
511 static int mtk_nor_spi_mem_prg(struct mtk_nor *sp, const struct spi_mem_op *op)
514 int reg_offset = MTK_NOR_REG_PRGDATA_MAX;
520 tx_len = op->cmd.nbytes + op->addr.nbytes;
522 // count dummy bytes only if we need to write data after it
523 if (op->data.dir == SPI_MEM_DATA_OUT)
524 tx_len += op->dummy.nbytes + op->data.nbytes;
525 else if (op->data.dir == SPI_MEM_DATA_IN)
526 rx_len = op->data.nbytes;
528 prg_len = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes +
531 // an invalid op may reach here if the caller calls exec_op without
532 // adjust_op_size. return -EINVAL instead of -ENOTSUPP so that
533 // spi-mem won't try this op again with generic spi transfers.
534 if ((tx_len > MTK_NOR_REG_PRGDATA_MAX + 1) ||
535 (rx_len > MTK_NOR_REG_SHIFT_MAX + 1) ||
536 (prg_len > MTK_NOR_PRG_CNT_MAX / 8))
540 for (i = op->cmd.nbytes; i > 0; i--, reg_offset--) {
541 reg = sp->base + MTK_NOR_REG_PRGDATA(reg_offset);
542 bufbyte = (op->cmd.opcode >> ((i - 1) * BITS_PER_BYTE)) & 0xff;
543 writeb(bufbyte, reg);
546 for (i = op->addr.nbytes; i > 0; i--, reg_offset--) {
547 reg = sp->base + MTK_NOR_REG_PRGDATA(reg_offset);
548 bufbyte = (op->addr.val >> ((i - 1) * BITS_PER_BYTE)) & 0xff;
549 writeb(bufbyte, reg);
552 if (op->data.dir == SPI_MEM_DATA_OUT) {
553 for (i = 0; i < op->dummy.nbytes; i++, reg_offset--) {
554 reg = sp->base + MTK_NOR_REG_PRGDATA(reg_offset);
558 for (i = 0; i < op->data.nbytes; i++, reg_offset--) {
559 reg = sp->base + MTK_NOR_REG_PRGDATA(reg_offset);
560 writeb(((const u8 *)(op->data.buf.out))[i], reg);
564 for (; reg_offset >= 0; reg_offset--) {
565 reg = sp->base + MTK_NOR_REG_PRGDATA(reg_offset);
571 writel(prg_len * BITS_PER_BYTE + sp->caps->extra_dummy_bit,
572 sp->base + MTK_NOR_REG_PRG_CNT);
574 writel(prg_len * BITS_PER_BYTE, sp->base + MTK_NOR_REG_PRG_CNT);
576 ret = mtk_nor_cmd_exec(sp, MTK_NOR_CMD_PROGRAM,
577 prg_len * BITS_PER_BYTE);
583 if (op->data.dir == SPI_MEM_DATA_IN) {
584 for (i = op->data.nbytes - 1; i >= 0; i--, reg_offset++) {
585 reg = sp->base + MTK_NOR_REG_SHIFT(reg_offset);
586 ((u8 *)(op->data.buf.in))[i] = readb(reg);
593 static int mtk_nor_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
595 struct mtk_nor *sp = spi_controller_get_devdata(mem->spi->master);
598 if ((op->data.nbytes == 0) ||
599 ((op->addr.nbytes != 3) && (op->addr.nbytes != 4)))
600 return mtk_nor_spi_mem_prg(sp, op);
602 if (op->data.dir == SPI_MEM_DATA_OUT) {
603 mtk_nor_set_addr(sp, op);
604 writeb(op->cmd.opcode, sp->base + MTK_NOR_REG_PRGDATA0);
605 if (op->data.nbytes == MTK_NOR_PP_SIZE)
606 return mtk_nor_pp_buffered(sp, op);
607 return mtk_nor_pp_unbuffered(sp, op);
610 if ((op->data.dir == SPI_MEM_DATA_IN) && mtk_nor_match_read(op)) {
611 ret = mtk_nor_write_buffer_disable(sp);
614 mtk_nor_setup_bus(sp, op);
615 if (op->data.nbytes == 1) {
616 mtk_nor_set_addr(sp, op);
617 return mtk_nor_read_pio(sp, op);
619 return mtk_nor_read_dma(sp, op);
623 return mtk_nor_spi_mem_prg(sp, op);
626 static int mtk_nor_setup(struct spi_device *spi)
628 struct mtk_nor *sp = spi_controller_get_devdata(spi->master);
630 if (spi->max_speed_hz && (spi->max_speed_hz < sp->spi_freq)) {
631 dev_err(&spi->dev, "spi clock should be %u Hz.\n",
635 spi->max_speed_hz = sp->spi_freq;
640 static int mtk_nor_transfer_one_message(struct spi_controller *master,
641 struct spi_message *m)
643 struct mtk_nor *sp = spi_controller_get_devdata(master);
644 struct spi_transfer *t = NULL;
645 unsigned long trx_len = 0;
647 int reg_offset = MTK_NOR_REG_PRGDATA_MAX;
653 list_for_each_entry(t, &m->transfers, transfer_list) {
655 for (i = 0; i < t->len; i++, reg_offset--) {
656 reg = sp->base + MTK_NOR_REG_PRGDATA(reg_offset);
658 writeb(txbuf[i], reg);
665 writel(trx_len * BITS_PER_BYTE, sp->base + MTK_NOR_REG_PRG_CNT);
667 stat = mtk_nor_cmd_exec(sp, MTK_NOR_CMD_PROGRAM,
668 trx_len * BITS_PER_BYTE);
672 reg_offset = trx_len - 1;
673 list_for_each_entry(t, &m->transfers, transfer_list) {
675 for (i = 0; i < t->len; i++, reg_offset--) {
676 reg = sp->base + MTK_NOR_REG_SHIFT(reg_offset);
678 rxbuf[i] = readb(reg);
682 m->actual_length = trx_len;
685 spi_finalize_current_message(master);
690 static void mtk_nor_disable_clk(struct mtk_nor *sp)
692 clk_disable_unprepare(sp->spi_clk);
693 clk_disable_unprepare(sp->ctlr_clk);
694 clk_disable_unprepare(sp->axi_clk);
695 clk_disable_unprepare(sp->axi_s_clk);
698 static int mtk_nor_enable_clk(struct mtk_nor *sp)
702 ret = clk_prepare_enable(sp->spi_clk);
706 ret = clk_prepare_enable(sp->ctlr_clk);
708 clk_disable_unprepare(sp->spi_clk);
712 ret = clk_prepare_enable(sp->axi_clk);
714 clk_disable_unprepare(sp->spi_clk);
715 clk_disable_unprepare(sp->ctlr_clk);
719 ret = clk_prepare_enable(sp->axi_s_clk);
721 clk_disable_unprepare(sp->spi_clk);
722 clk_disable_unprepare(sp->ctlr_clk);
723 clk_disable_unprepare(sp->axi_clk);
730 static void mtk_nor_init(struct mtk_nor *sp)
732 writel(0, sp->base + MTK_NOR_REG_IRQ_EN);
733 writel(MTK_NOR_IRQ_MASK, sp->base + MTK_NOR_REG_IRQ_STAT);
735 writel(MTK_NOR_ENABLE_SF_CMD, sp->base + MTK_NOR_REG_WP);
736 mtk_nor_rmw(sp, MTK_NOR_REG_CFG2, MTK_NOR_WR_CUSTOM_OP_EN, 0);
737 mtk_nor_rmw(sp, MTK_NOR_REG_CFG3,
738 MTK_NOR_DISABLE_WREN | MTK_NOR_DISABLE_SR_POLL, 0);
741 static irqreturn_t mtk_nor_irq_handler(int irq, void *data)
743 struct mtk_nor *sp = data;
744 u32 irq_status, irq_enabled;
746 irq_status = readl(sp->base + MTK_NOR_REG_IRQ_STAT);
747 irq_enabled = readl(sp->base + MTK_NOR_REG_IRQ_EN);
748 // write status back to clear interrupt
749 writel(irq_status, sp->base + MTK_NOR_REG_IRQ_STAT);
751 if (!(irq_status & irq_enabled))
754 if (irq_status & MTK_NOR_IRQ_DMA) {
755 complete(&sp->op_done);
756 writel(0, sp->base + MTK_NOR_REG_IRQ_EN);
762 static size_t mtk_max_msg_size(struct spi_device *spi)
764 return MTK_NOR_PRG_MAX_SIZE;
767 static const struct spi_controller_mem_ops mtk_nor_mem_ops = {
768 .adjust_op_size = mtk_nor_adjust_op_size,
769 .supports_op = mtk_nor_supports_op,
770 .exec_op = mtk_nor_exec_op
773 static const struct mtk_nor_caps mtk_nor_caps_mt8173 = {
775 .extra_dummy_bit = 0,
778 static const struct mtk_nor_caps mtk_nor_caps_mt8186 = {
780 .extra_dummy_bit = 1,
783 static const struct mtk_nor_caps mtk_nor_caps_mt8192 = {
785 .extra_dummy_bit = 0,
788 static const struct of_device_id mtk_nor_match[] = {
789 { .compatible = "mediatek,mt8173-nor", .data = &mtk_nor_caps_mt8173 },
790 { .compatible = "mediatek,mt8186-nor", .data = &mtk_nor_caps_mt8186 },
791 { .compatible = "mediatek,mt8192-nor", .data = &mtk_nor_caps_mt8192 },
794 MODULE_DEVICE_TABLE(of, mtk_nor_match);
796 static int mtk_nor_probe(struct platform_device *pdev)
798 struct spi_controller *ctlr;
800 struct mtk_nor_caps *caps;
802 struct clk *spi_clk, *ctlr_clk, *axi_clk, *axi_s_clk;
805 base = devm_platform_ioremap_resource(pdev, 0);
807 return PTR_ERR(base);
809 spi_clk = devm_clk_get(&pdev->dev, "spi");
811 return PTR_ERR(spi_clk);
813 ctlr_clk = devm_clk_get(&pdev->dev, "sf");
814 if (IS_ERR(ctlr_clk))
815 return PTR_ERR(ctlr_clk);
817 axi_clk = devm_clk_get_optional(&pdev->dev, "axi");
819 return PTR_ERR(axi_clk);
821 axi_s_clk = devm_clk_get_optional(&pdev->dev, "axi_s");
822 if (IS_ERR(axi_s_clk))
823 return PTR_ERR(axi_s_clk);
825 caps = (struct mtk_nor_caps *)of_device_get_match_data(&pdev->dev);
827 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(caps->dma_bits));
829 dev_err(&pdev->dev, "failed to set dma mask(%u)\n", caps->dma_bits);
833 ctlr = devm_spi_alloc_master(&pdev->dev, sizeof(*sp));
835 dev_err(&pdev->dev, "failed to allocate spi controller\n");
839 ctlr->bits_per_word_mask = SPI_BPW_MASK(8);
840 ctlr->dev.of_node = pdev->dev.of_node;
841 ctlr->max_message_size = mtk_max_msg_size;
842 ctlr->mem_ops = &mtk_nor_mem_ops;
843 ctlr->mode_bits = SPI_RX_DUAL | SPI_RX_QUAD | SPI_TX_DUAL | SPI_TX_QUAD;
844 ctlr->num_chipselect = 1;
845 ctlr->setup = mtk_nor_setup;
846 ctlr->transfer_one_message = mtk_nor_transfer_one_message;
847 ctlr->auto_runtime_pm = true;
849 dev_set_drvdata(&pdev->dev, ctlr);
851 sp = spi_controller_get_devdata(ctlr);
856 sp->dev = &pdev->dev;
857 sp->spi_clk = spi_clk;
858 sp->ctlr_clk = ctlr_clk;
859 sp->axi_clk = axi_clk;
860 sp->axi_s_clk = axi_s_clk;
862 sp->high_dma = caps->dma_bits > 32;
863 sp->buffer = dmam_alloc_coherent(&pdev->dev,
864 MTK_NOR_BOUNCE_BUF_SIZE + MTK_NOR_DMA_ALIGN,
865 &sp->buffer_dma, GFP_KERNEL);
869 if ((uintptr_t)sp->buffer & MTK_NOR_DMA_ALIGN_MASK) {
870 dev_err(sp->dev, "misaligned allocation of internal buffer.\n");
874 ret = mtk_nor_enable_clk(sp);
878 sp->spi_freq = clk_get_rate(sp->spi_clk);
882 irq = platform_get_irq_optional(pdev, 0);
885 dev_warn(sp->dev, "IRQ not available.");
887 ret = devm_request_irq(sp->dev, irq, mtk_nor_irq_handler, 0,
890 dev_warn(sp->dev, "failed to request IRQ.");
892 init_completion(&sp->op_done);
897 pm_runtime_set_autosuspend_delay(&pdev->dev, -1);
898 pm_runtime_use_autosuspend(&pdev->dev);
899 pm_runtime_set_active(&pdev->dev);
900 pm_runtime_enable(&pdev->dev);
901 pm_runtime_get_noresume(&pdev->dev);
903 ret = devm_spi_register_controller(&pdev->dev, ctlr);
907 pm_runtime_mark_last_busy(&pdev->dev);
908 pm_runtime_put_autosuspend(&pdev->dev);
910 dev_info(&pdev->dev, "spi frequency: %d Hz\n", sp->spi_freq);
915 pm_runtime_disable(&pdev->dev);
916 pm_runtime_set_suspended(&pdev->dev);
917 pm_runtime_dont_use_autosuspend(&pdev->dev);
919 mtk_nor_disable_clk(sp);
924 static int mtk_nor_remove(struct platform_device *pdev)
926 struct spi_controller *ctlr = dev_get_drvdata(&pdev->dev);
927 struct mtk_nor *sp = spi_controller_get_devdata(ctlr);
929 pm_runtime_disable(&pdev->dev);
930 pm_runtime_set_suspended(&pdev->dev);
931 pm_runtime_dont_use_autosuspend(&pdev->dev);
933 mtk_nor_disable_clk(sp);
938 static int __maybe_unused mtk_nor_runtime_suspend(struct device *dev)
940 struct spi_controller *ctlr = dev_get_drvdata(dev);
941 struct mtk_nor *sp = spi_controller_get_devdata(ctlr);
943 mtk_nor_disable_clk(sp);
948 static int __maybe_unused mtk_nor_runtime_resume(struct device *dev)
950 struct spi_controller *ctlr = dev_get_drvdata(dev);
951 struct mtk_nor *sp = spi_controller_get_devdata(ctlr);
953 return mtk_nor_enable_clk(sp);
956 static int __maybe_unused mtk_nor_suspend(struct device *dev)
958 return pm_runtime_force_suspend(dev);
961 static int __maybe_unused mtk_nor_resume(struct device *dev)
963 struct spi_controller *ctlr = dev_get_drvdata(dev);
964 struct mtk_nor *sp = spi_controller_get_devdata(ctlr);
967 ret = pm_runtime_force_resume(dev);
976 static const struct dev_pm_ops mtk_nor_pm_ops = {
977 SET_RUNTIME_PM_OPS(mtk_nor_runtime_suspend,
978 mtk_nor_runtime_resume, NULL)
979 SET_SYSTEM_SLEEP_PM_OPS(mtk_nor_suspend, mtk_nor_resume)
982 static struct platform_driver mtk_nor_driver = {
985 .of_match_table = mtk_nor_match,
986 .pm = &mtk_nor_pm_ops,
988 .probe = mtk_nor_probe,
989 .remove = mtk_nor_remove,
992 module_platform_driver(mtk_nor_driver);
994 MODULE_DESCRIPTION("Mediatek SPI NOR controller driver");
995 MODULE_AUTHOR("Chuanhong Guo <gch981213@gmail.com>");
996 MODULE_LICENSE("GPL v2");
997 MODULE_ALIAS("platform:" DRIVER_NAME);