1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2018 MediaTek, Inc.
4 * Author : Guochun.Mao@mediatek.com
12 #include <linux/iopoll.h>
13 #include <linux/ioport.h>
16 struct mtk_qspi_regs {
49 struct mtk_qspi_platdata {
54 struct mtk_qspi_priv {
55 struct mtk_qspi_regs *regs;
56 unsigned long *mem_base;
58 u8 tx[3]; /* only record max 3 bytes paras, when it's address. */
59 u32 txlen; /* dout buffer length - op code length */
64 #define MTK_QSPI_CMD_POLLINGREG_US 500000
65 #define MTK_QSPI_WRBUF_SIZE 256
66 #define MTK_QSPI_COMMAND_ENABLE 0x30
68 /* NOR flash controller commands */
69 #define MTK_QSPI_RD_TRIGGER BIT(0)
70 #define MTK_QSPI_READSTATUS BIT(1)
71 #define MTK_QSPI_PRG_CMD BIT(2)
72 #define MTK_QSPI_WR_TRIGGER BIT(4)
73 #define MTK_QSPI_WRITESTATUS BIT(5)
74 #define MTK_QSPI_AUTOINC BIT(7)
76 #define MTK_QSPI_MAX_RX_TX_SHIFT 0x6
77 #define MTK_QSPI_MAX_SHIFT 0x8
79 #define MTK_QSPI_WR_BUF_ENABLE 0x1
80 #define MTK_QSPI_WR_BUF_DISABLE 0x0
82 static int mtk_qspi_execute_cmd(struct mtk_qspi_priv *priv, u8 cmd)
85 u8 val = cmd & ~MTK_QSPI_AUTOINC;
87 writeb(cmd, &priv->regs->cmd);
89 return readb_poll_timeout(&priv->regs->cmd, tmp, !(val & tmp),
90 MTK_QSPI_CMD_POLLINGREG_US);
93 static int mtk_qspi_tx_rx(struct mtk_qspi_priv *priv)
95 int len = 1 + priv->txlen + priv->rxlen;
98 if (len > MTK_QSPI_MAX_SHIFT)
101 writeb(len * 8, &priv->regs->cnt);
103 /* start at PRGDATA5, go down to PRGDATA0 */
104 idx = MTK_QSPI_MAX_RX_TX_SHIFT - 1;
107 writeb(priv->op, &priv->regs->prgdata[idx]);
110 /* program TX data */
111 for (i = 0; i < priv->txlen; i++, idx--)
112 writeb(priv->tx[i], &priv->regs->prgdata[idx]);
114 /* clear out rest of TX registers */
116 writeb(0, &priv->regs->prgdata[idx]);
120 ret = mtk_qspi_execute_cmd(priv, MTK_QSPI_PRG_CMD);
124 /* restart at first RX byte */
125 idx = priv->rxlen - 1;
127 /* read out RX data */
128 for (i = 0; i < priv->rxlen; i++, idx--)
129 priv->rx[i] = readb(&priv->regs->shreg[idx]);
134 static int mtk_qspi_read(struct mtk_qspi_priv *priv,
135 u32 addr, u8 *buf, u32 len)
137 memcpy(buf, (u8 *)priv->mem_base + addr, len);
141 static void mtk_qspi_set_addr(struct mtk_qspi_priv *priv, u32 addr)
145 for (i = 0; i < 3; i++) {
146 writeb(addr & 0xff, &priv->regs->radr[i]);
151 static int mtk_qspi_write_single_byte(struct mtk_qspi_priv *priv,
152 u32 addr, u32 length, const u8 *data)
156 mtk_qspi_set_addr(priv, addr);
158 for (i = 0; i < length; i++) {
159 writeb(*data++, &priv->regs->wdata);
160 ret = mtk_qspi_execute_cmd(priv, MTK_QSPI_WR_TRIGGER);
167 static int mtk_qspi_write_buffer(struct mtk_qspi_priv *priv, u32 addr,
172 mtk_qspi_set_addr(priv, addr);
174 for (i = 0; i < MTK_QSPI_WRBUF_SIZE; i += 4) {
175 data = buf[i + 3] << 24 | buf[i + 2] << 16 |
176 buf[i + 1] << 8 | buf[i];
177 writel(data, &priv->regs->pp_dw_data);
180 return mtk_qspi_execute_cmd(priv, MTK_QSPI_WR_TRIGGER);
183 static int mtk_qspi_write(struct mtk_qspi_priv *priv,
184 u32 addr, const u8 *buf, u32 len)
188 /* setting pre-fetch buffer for page program */
189 writel(MTK_QSPI_WR_BUF_ENABLE, &priv->regs->cfg[1]);
190 while (len >= MTK_QSPI_WRBUF_SIZE) {
191 ret = mtk_qspi_write_buffer(priv, addr, buf);
195 len -= MTK_QSPI_WRBUF_SIZE;
196 addr += MTK_QSPI_WRBUF_SIZE;
197 buf += MTK_QSPI_WRBUF_SIZE;
199 /* disable pre-fetch buffer for page program */
200 writel(MTK_QSPI_WR_BUF_DISABLE, &priv->regs->cfg[1]);
203 return mtk_qspi_write_single_byte(priv, addr, len, buf);
208 static int mtk_qspi_claim_bus(struct udevice *dev)
214 static int mtk_qspi_release_bus(struct udevice *dev)
220 static int mtk_qspi_transfer(struct mtk_qspi_priv *priv, unsigned int bitlen,
221 const void *dout, void *din, unsigned long flags)
223 u32 bytes = DIV_ROUND_UP(bitlen, 8);
230 if (flags & SPI_XFER_BEGIN) {
231 /* parse op code and potential paras first */
232 priv->op = *(u8 *)dout;
234 memcpy(priv->tx, (u8 *)dout + 1,
235 bytes <= 4 ? bytes - 1 : 3);
236 priv->txlen = bytes - 1;
239 if (flags == SPI_XFER_ONCE) {
240 /* operations without receiving or sending data.
241 * for example: erase, write flash register or write
246 return mtk_qspi_tx_rx(priv);
249 if (flags & SPI_XFER_END) {
250 /* here, dout should be data to be written.
251 * and priv->tx should be filled 3Bytes address.
253 addr = priv->tx[0] << 16 | priv->tx[1] << 8 |
255 return mtk_qspi_write(priv, addr, (u8 *)dout, bytes);
260 if (priv->txlen >= 3) {
261 /* if run to here, priv->tx[] should be the address
262 * where read data from,
263 * and, din is the buf to receive data.
265 addr = priv->tx[0] << 16 | priv->tx[1] << 8 |
267 return mtk_qspi_read(priv, addr, (u8 *)din, bytes);
270 /* should be reading flash's register */
271 priv->rx = (u8 *)din;
273 return mtk_qspi_tx_rx(priv);
279 static int mtk_qspi_xfer(struct udevice *dev, unsigned int bitlen,
280 const void *dout, void *din, unsigned long flags)
282 struct udevice *bus = dev->parent;
283 struct mtk_qspi_priv *priv = dev_get_priv(bus);
285 return mtk_qspi_transfer(priv, bitlen, dout, din, flags);
288 static int mtk_qspi_set_speed(struct udevice *bus, uint speed)
294 static int mtk_qspi_set_mode(struct udevice *bus, uint mode)
300 static int mtk_qspi_ofdata_to_platdata(struct udevice *bus)
302 struct resource res_reg, res_mem;
303 struct mtk_qspi_platdata *plat = bus->platdata;
306 ret = dev_read_resource_byname(bus, "reg_base", &res_reg);
308 debug("can't get reg_base resource(ret = %d)\n", ret);
312 ret = dev_read_resource_byname(bus, "mem_base", &res_mem);
314 debug("can't get map_base resource(ret = %d)\n", ret);
318 plat->mem_base = res_mem.start;
319 plat->reg_base = res_reg.start;
324 static int mtk_qspi_probe(struct udevice *bus)
326 struct mtk_qspi_platdata *plat = dev_get_platdata(bus);
327 struct mtk_qspi_priv *priv = dev_get_priv(bus);
329 priv->regs = (struct mtk_qspi_regs *)plat->reg_base;
330 priv->mem_base = (unsigned long *)plat->mem_base;
332 writel(MTK_QSPI_COMMAND_ENABLE, &priv->regs->wrprot);
337 static const struct dm_spi_ops mtk_qspi_ops = {
338 .claim_bus = mtk_qspi_claim_bus,
339 .release_bus = mtk_qspi_release_bus,
340 .xfer = mtk_qspi_xfer,
341 .set_speed = mtk_qspi_set_speed,
342 .set_mode = mtk_qspi_set_mode,
345 static const struct udevice_id mtk_qspi_ids[] = {
346 { .compatible = "mediatek,mt7629-qspi" },
350 U_BOOT_DRIVER(mtk_qspi) = {
353 .of_match = mtk_qspi_ids,
354 .ops = &mtk_qspi_ops,
355 .ofdata_to_platdata = mtk_qspi_ofdata_to_platdata,
356 .platdata_auto_alloc_size = sizeof(struct mtk_qspi_platdata),
357 .priv_auto_alloc_size = sizeof(struct mtk_qspi_priv),
358 .probe = mtk_qspi_probe,