1 /* SPDX-License-Identifier: GPL-2.0-only */
4 #include <console/console.h>
5 #include <device/mmio.h>
6 #include <soc/flash_controller_common.h>
7 #include <soc/symbols.h>
9 #include <spi-generic.h>
15 static struct mtk_nor_regs
*const mtk_nor
= (void *)SFLASH_REG_BASE
;
17 #define GET_NTH_BYTE(d, n) ((d >> (8 * n)) & 0xff)
19 static int polling_cmd(u32 val
)
23 stopwatch_init_usecs_expire(&sw
, SFLASH_POLLINGREG_US
);
25 while ((read32(&mtk_nor
->cmd
) & val
) != 0) {
26 if (stopwatch_expired(&sw
))
33 static int mtk_nor_execute_cmd(u8 cmdval
)
35 u8 val
= cmdval
& ~SFLASH_AUTOINC
;
37 write8(&mtk_nor
->cmd
, cmdval
);
38 return polling_cmd(val
);
41 static int sflashhw_read_flash_status(u8
*value
)
43 if (mtk_nor_execute_cmd(SFLASH_READSTATUS
))
46 *value
= read8(&mtk_nor
->rdsr
);
50 static int wait_for_write_done(void)
55 stopwatch_init_usecs_expire(&sw
, SFLASH_POLLINGREG_US
);
57 while (sflashhw_read_flash_status(®
) == 0) {
58 if (!(reg
& SFLASH_WRITE_IN_PROGRESS
))
60 if (stopwatch_expired(&sw
))
67 /* set serial flash program address */
68 static void set_sfpaddr(u32 addr
)
70 write8(&mtk_nor
->radr
[2], GET_NTH_BYTE(addr
, 2));
71 write8(&mtk_nor
->radr
[1], GET_NTH_BYTE(addr
, 1));
72 write8(&mtk_nor
->radr
[0], GET_NTH_BYTE(addr
, 0));
75 static int sector_erase(int offset
)
77 if (wait_for_write_done())
80 write8(&mtk_nor
->prgdata
[5], SFLASH_OP_WREN
);
81 write8(&mtk_nor
->cnt
, 8);
82 mtk_nor_execute_cmd(SFLASH_PRG_CMD
);
84 write8(&mtk_nor
->prgdata
[5], SECTOR_ERASE_CMD
);
85 write8(&mtk_nor
->prgdata
[4], GET_NTH_BYTE(offset
, 2));
86 write8(&mtk_nor
->prgdata
[3], GET_NTH_BYTE(offset
, 1));
87 write8(&mtk_nor
->prgdata
[2], GET_NTH_BYTE(offset
, 0));
88 write8(&mtk_nor
->cnt
, 32);
89 mtk_nor_execute_cmd(SFLASH_PRG_CMD
);
91 if (wait_for_write_done())
97 static int dma_read(u32 addr
, uintptr_t dma_buf
, u32 len
)
101 assert(IS_ALIGNED((uintptr_t)addr
, SFLASH_DMA_ALIGN
) &&
102 IS_ALIGNED(len
, SFLASH_DMA_ALIGN
));
105 write32(&mtk_nor
->fdma_ctl
, SFLASH_DMA_SW_RESET
);
106 write32(&mtk_nor
->fdma_ctl
, SFLASH_DMA_WDLE_EN
);
107 /* flash source address and dram dest address */
108 write32(&mtk_nor
->fdma_fadr
, addr
);
109 write32(&mtk_nor
->fdma_dadr
, dma_buf
);
110 write32(&mtk_nor
->fdma_end_dadr
, (dma_buf
+ len
));
112 write32(&mtk_nor
->fdma_ctl
, SFLASH_DMA_TRIGGER
| SFLASH_DMA_WDLE_EN
);
114 stopwatch_init_usecs_expire(&sw
, SFLASH_POLLINGREG_US
);
115 while ((read32(&mtk_nor
->fdma_ctl
) & SFLASH_DMA_TRIGGER
) != 0) {
116 if (stopwatch_expired(&sw
)) {
117 printk(BIOS_WARNING
, "dma read timeout!\n");
125 static int nor_read(const struct spi_flash
*flash
, u32 addr
, size_t len
,
128 uintptr_t dma_buf
= (uintptr_t)_dma_coherent
;
129 size_t dma_buf_len
= REGION_SIZE(dma_coherent
);
130 u32 start
= ALIGN_DOWN(addr
, SFLASH_DMA_ALIGN
);
131 u32 skip
= addr
- start
;
132 u32 total
= ALIGN_UP(skip
+ len
, SFLASH_DMA_ALIGN
);
133 u32 drop
= total
- skip
- len
;
134 u32 done
, read_len
, copy_len
;
135 uint8_t *dest
= (uint8_t *)buf
;
137 /* Refer to CB:13989 for the hardware limitation on mt8173. */
138 if (CONFIG(SOC_MEDIATEK_MT8173
)) {
139 if (!ENV_BOOTBLOCK
&& !ENV_SEPARATE_VERSTAGE
) {
140 dma_buf
= (uintptr_t)_dram_dma
;
141 dma_buf_len
= REGION_SIZE(dram_dma
);
145 if (CONFIG(FLASH_DUAL_IO_READ
)) {
146 setbits8(&mtk_nor
->read_dual
, SFLASH_READ_DUAL_EN
);
147 write8(&mtk_nor
->prgdata
[3], SFLASH_1_1_2_READ
);
150 /* DMA: start [ skip | len | drop ] = total end */
151 for (done
= 0; done
< total
; dest
+= copy_len
) {
152 read_len
= MIN(dma_buf_len
, total
- done
);
153 if (dma_read(start
+ done
, dma_buf
, read_len
))
157 /* decide the range to copy into buffer */
159 read_len
-= drop
; /* Only drop in last iteration */
161 copy_len
= read_len
- skip
;
162 memcpy(dest
, (uint8_t *)dma_buf
+ skip
, copy_len
);
164 skip
= 0; /* Only apply skip in first iteration. */
169 static int nor_write(const struct spi_flash
*flash
, u32 addr
, size_t len
,
172 const u8
*buffer
= (const u8
*)buf
;
176 write8(&mtk_nor
->wdata
, *buffer
);
177 if (mtk_nor_execute_cmd(SFLASH_WR_TRIGGER
| SFLASH_AUTOINC
))
180 if (wait_for_write_done())
188 static int nor_erase(const struct spi_flash
*flash
, u32 offset
, size_t len
)
190 int sector_start
= offset
;
191 int sector_num
= (u32
)len
/ flash
->sector_size
;
194 if (!sector_erase(sector_start
)) {
195 sector_start
+= flash
->sector_size
;
198 printk(BIOS_WARNING
, "Erase failed at %#x!\n",
206 const struct spi_flash_ops spi_flash_ops
= {
212 int mtk_spi_flash_probe(const struct spi_slave
*spi
,
213 struct spi_flash
*flash
)
215 write32(&mtk_nor
->wrprot
, SFLASH_COMMAND_ENABLE
);
216 memcpy(&flash
->spi
, spi
, sizeof(*spi
));
218 flash
->sector_size
= 0x1000;
219 flash
->erase_cmd
= SECTOR_ERASE_CMD
;
220 flash
->size
= CONFIG_ROM_SIZE
;
222 flash
->ops
= &spi_flash_ops
;