blob: f202b2f49f587e765a02202ab8c56c6bf2051391 [file] [log] [blame]
developerf33cf3c2021-01-20 15:31:33 +08001// SPDX-License-Identifier: GPL-2.0
2//
3// Mediatek SPI-NOR controller driver
4//
5// Copyright (C) 2020 SkyLake Huang <SkyLake.Huang@mediatek.com>
6//
7// Some parts are based on drivers/spi/spi-mtk-nor.c of linux version
8
9#include <clk.h>
developerf33cf3c2021-01-20 15:31:33 +080010#include <cpu_func.h>
11#include <dm.h>
12#include <dm/device.h>
13#include <dm/device_compat.h>
14#include <dm/devres.h>
15#include <dm/pinctrl.h>
16#include <linux/bitops.h>
17#include <linux/completion.h>
18#include <linux/io.h>
19#include <linux/iopoll.h>
20#include <spi.h>
21#include <spi-mem.h>
22#include <stdbool.h>
23#include <watchdog.h>
24#include <linux/dma-mapping.h>
25
26#define DRIVER_NAME "mtk-spi-nor"
27
28#define MTK_NOR_REG_CMD 0x00
29#define MTK_NOR_CMD_WRSR BIT(5)
30#define MTK_NOR_CMD_WRITE BIT(4)
31#define MTK_NOR_CMD_PROGRAM BIT(2)
32#define MTK_NOR_CMD_RDSR BIT(1)
33#define MTK_NOR_CMD_READ BIT(0)
34#define MTK_NOR_CMD_MASK GENMASK(5, 0)
35
36#define MTK_NOR_REG_PRG_CNT 0x04
37#define MTK_NOR_REG_RDSR 0x08
38#define MTK_NOR_REG_RDATA 0x0c
39
40#define MTK_NOR_REG_RADR0 0x10
41#define MTK_NOR_REG_RADR(n) (MTK_NOR_REG_RADR0 + 4 * (n))
42#define MTK_NOR_REG_RADR3 0xc8
43
44#define MTK_NOR_REG_WDATA 0x1c
45
46#define MTK_NOR_REG_PRGDATA0 0x20
47#define MTK_NOR_REG_PRGDATA(n) (MTK_NOR_REG_PRGDATA0 + 4 * (n))
48#define MTK_NOR_REG_PRGDATA_MAX 5
49
50#define MTK_NOR_REG_SHIFT0 0x38
51#define MTK_NOR_REG_SHIFT(n) (MTK_NOR_REG_SHIFT0 + 4 * (n))
52#define MTK_NOR_REG_SHIFT_MAX 9
53
54#define MTK_NOR_REG_CFG1 0x60
55#define MTK_NOR_FAST_READ BIT(0)
56
57#define MTK_NOR_REG_CFG2 0x64
58#define MTK_NOR_WR_CUSTOM_OP_EN BIT(4)
59#define MTK_NOR_WR_BUF_EN BIT(0)
60
61#define MTK_NOR_REG_PP_DATA 0x98
62
63#define MTK_NOR_REG_IRQ_STAT 0xa8
64#define MTK_NOR_REG_IRQ_EN 0xac
65#define MTK_NOR_IRQ_DMA BIT(7)
66#define MTK_NOR_IRQ_WRSR BIT(5)
67#define MTK_NOR_IRQ_MASK GENMASK(7, 0)
68
69#define MTK_NOR_REG_CFG3 0xb4
70#define MTK_NOR_DISABLE_WREN BIT(7)
71#define MTK_NOR_DISABLE_SR_POLL BIT(5)
72
73#define MTK_NOR_REG_WP 0xc4
74#define MTK_NOR_ENABLE_SF_CMD 0x30
75
76#define MTK_NOR_REG_BUSCFG 0xcc
77#define MTK_NOR_4B_ADDR BIT(4)
78#define MTK_NOR_QUAD_ADDR BIT(3)
79#define MTK_NOR_QUAD_READ BIT(2)
80#define MTK_NOR_DUAL_ADDR BIT(1)
81#define MTK_NOR_DUAL_READ BIT(0)
82#define MTK_NOR_BUS_MODE_MASK GENMASK(4, 0)
83
84#define MTK_NOR_REG_DMA_CTL 0x718
85#define MTK_NOR_DMA_START BIT(0)
86
87#define MTK_NOR_REG_DMA_FADR 0x71c
88#define MTK_NOR_REG_DMA_DADR 0x720
89#define MTK_NOR_REG_DMA_END_DADR 0x724
90
91#define MTK_NOR_PRG_MAX_SIZE 6
92// Reading DMA src/dst addresses have to be 16-byte aligned
93#define MTK_NOR_DMA_ALIGN 16
94#define MTK_NOR_DMA_ALIGN_MASK (MTK_NOR_DMA_ALIGN - 1)
95// and we allocate a bounce buffer if destination address isn't aligned.
96#define MTK_NOR_BOUNCE_BUF_SIZE PAGE_SIZE
97
98// Buffered page program can do one 128-byte transfer
99#define MTK_NOR_PP_SIZE 128
100
101#define CLK_TO_US(priv, clkcnt) DIV_ROUND_UP(clkcnt, (priv)->spi_freq / 1000000)
102
103#define MTK_NOR_UNLOCK_ALL 0x0
104
105struct mtk_snor_priv {
106 struct device *dev;
107 void __iomem *base;
108 u8 *buffer;
109 struct clk spi_clk;
110 struct clk ctlr_clk;
111 unsigned int spi_freq;
112 bool wbuf_en;
113};
114
115static inline void mtk_snor_rmw(struct mtk_snor_priv *priv, u32 reg, u32 set,
116 u32 clr)
117{
118 u32 val = readl(priv->base + reg);
119
120 val &= ~clr;
121 val |= set;
122 writel(val, priv->base + reg);
123}
124
125static inline int mtk_snor_cmd_exec(struct mtk_snor_priv *priv, u32 cmd,
126 ulong clk)
127{
128 unsigned long long delay = CLK_TO_US(priv, clk);
129 u32 reg;
130 int ret;
131
132 writel(cmd, priv->base + MTK_NOR_REG_CMD);
133 delay = (delay + 1) * 200;
134 ret = readl_poll_timeout(priv->base + MTK_NOR_REG_CMD, reg,
135 !(reg & cmd), delay);
136 if (ret < 0)
137 dev_err(priv->dev, "command %u timeout.\n", cmd);
138 return ret;
139}
140
141static void mtk_snor_set_addr(struct mtk_snor_priv *priv,
142 const struct spi_mem_op *op)
143{
144 u32 addr = op->addr.val;
145 int i;
146
147 for (i = 0; i < 3; i++) {
148 writeb(addr & 0xff, priv->base + MTK_NOR_REG_RADR(i));
149 addr >>= 8;
150 }
151 if (op->addr.nbytes == 4) {
152 writeb(addr & 0xff, priv->base + MTK_NOR_REG_RADR3);
153 mtk_snor_rmw(priv, MTK_NOR_REG_BUSCFG, MTK_NOR_4B_ADDR, 0);
154 } else {
155 mtk_snor_rmw(priv, MTK_NOR_REG_BUSCFG, 0, MTK_NOR_4B_ADDR);
156 }
157}
158
159static bool need_bounce(const struct spi_mem_op *op)
160{
161 return ((uintptr_t)op->data.buf.in & MTK_NOR_DMA_ALIGN_MASK);
162}
163
164static int mtk_snor_adjust_op_size(struct spi_slave *slave,
165 struct spi_mem_op *op)
166{
167 if (!op->data.nbytes)
168 return 0;
169
170 if (op->addr.nbytes == 3 || op->addr.nbytes == 4) {
171 if (op->data.dir == SPI_MEM_DATA_IN) { //&&
172 // limit size to prevent timeout calculation overflow
173 if (op->data.nbytes > 0x400000)
174 op->data.nbytes = 0x400000;
175 if (op->addr.val & MTK_NOR_DMA_ALIGN_MASK ||
176 op->data.nbytes < MTK_NOR_DMA_ALIGN)
177 op->data.nbytes = 1;
178 else if (!need_bounce(op))
179 op->data.nbytes &= ~MTK_NOR_DMA_ALIGN_MASK;
180 else if (op->data.nbytes > MTK_NOR_BOUNCE_BUF_SIZE)
181 op->data.nbytes = MTK_NOR_BOUNCE_BUF_SIZE;
182 return 0;
183 } else if (op->data.dir == SPI_MEM_DATA_OUT) {
184 if (op->data.nbytes >= MTK_NOR_PP_SIZE)
185 op->data.nbytes = MTK_NOR_PP_SIZE;
186 else
187 op->data.nbytes = 1;
188 return 0;
189 }
190 }
191
192 return 0;
193}
194
195static bool mtk_snor_supports_op(struct spi_slave *slave,
196 const struct spi_mem_op *op)
197{
198 /* This controller only supports 1-1-1 write mode */
199 if (op->data.dir == SPI_MEM_DATA_OUT &&
200 (op->cmd.buswidth != 1 || op->data.buswidth != 1))
201 return false;
202
203 return true;
204}
205
206static void mtk_snor_setup_bus(struct mtk_snor_priv *priv,
207 const struct spi_mem_op *op)
208{
209 u32 reg = 0;
210
211 if (op->addr.nbytes == 4)
212 reg |= MTK_NOR_4B_ADDR;
213
214 if (op->data.buswidth == 4) {
215 reg |= MTK_NOR_QUAD_READ;
216 writeb(op->cmd.opcode, priv->base + MTK_NOR_REG_PRGDATA(4));
217 if (op->addr.buswidth == 4)
218 reg |= MTK_NOR_QUAD_ADDR;
219 } else if (op->data.buswidth == 2) {
220 reg |= MTK_NOR_DUAL_READ;
221 writeb(op->cmd.opcode, priv->base + MTK_NOR_REG_PRGDATA(3));
222 if (op->addr.buswidth == 2)
223 reg |= MTK_NOR_DUAL_ADDR;
224 } else {
225 if (op->cmd.opcode == 0x0b)
226 mtk_snor_rmw(priv, MTK_NOR_REG_CFG1, MTK_NOR_FAST_READ,
227 0);
228 else
229 mtk_snor_rmw(priv, MTK_NOR_REG_CFG1, 0,
230 MTK_NOR_FAST_READ);
231 }
232 mtk_snor_rmw(priv, MTK_NOR_REG_BUSCFG, reg, MTK_NOR_BUS_MODE_MASK);
233}
234
235static int mtk_snor_dma_exec(struct mtk_snor_priv *priv, u32 from,
236 unsigned int length, dma_addr_t dma_addr)
237{
238 int ret = 0;
239 ulong delay;
240 u32 reg;
241
242 writel(from, priv->base + MTK_NOR_REG_DMA_FADR);
243 writel(dma_addr, priv->base + MTK_NOR_REG_DMA_DADR);
244 writel(dma_addr + length, priv->base + MTK_NOR_REG_DMA_END_DADR);
245
246 mtk_snor_rmw(priv, MTK_NOR_REG_DMA_CTL, MTK_NOR_DMA_START, 0);
247
248 delay = CLK_TO_US(priv, (length + 5) * BITS_PER_BYTE);
249
250 delay = (delay + 1) * 100;
251 ret = readl_poll_timeout(priv->base + MTK_NOR_REG_DMA_CTL, reg,
252 !(reg & MTK_NOR_DMA_START), delay);
253
254 if (ret < 0)
255 dev_err(priv->dev, "dma read timeout.\n");
256
257 return ret;
258}
259
260static int mtk_snor_read_bounce(struct mtk_snor_priv *priv,
261 const struct spi_mem_op *op)
262{
263 unsigned int rdlen;
264 int ret;
265
266 if (op->data.nbytes & MTK_NOR_DMA_ALIGN_MASK)
267 rdlen = (op->data.nbytes + MTK_NOR_DMA_ALIGN) &
268 ~MTK_NOR_DMA_ALIGN_MASK;
269 else
270 rdlen = op->data.nbytes;
271
272 ret = mtk_snor_dma_exec(priv, op->addr.val, rdlen,
273 (dma_addr_t)priv->buffer);
274
275 if (!ret)
276 memcpy(op->data.buf.in, priv->buffer, op->data.nbytes);
277
278 return ret;
279}
280
281static int mtk_snor_read_dma(struct mtk_snor_priv *priv,
282 const struct spi_mem_op *op)
283{
284 int ret;
285 dma_addr_t dma_addr;
286
287 if (need_bounce(op))
288 return mtk_snor_read_bounce(priv, op);
289
290 dma_addr = dma_map_single(op->data.buf.in, op->data.nbytes,
291 DMA_FROM_DEVICE);
292
293 if (dma_mapping_error(priv->dev, dma_addr))
294 return -EINVAL;
295
296 ret = mtk_snor_dma_exec(priv, op->addr.val, op->data.nbytes, dma_addr);
297
298 dma_unmap_single(dma_addr, op->data.nbytes, DMA_FROM_DEVICE);
299
300 return ret;
301}
302
303static int mtk_snor_read_pio(struct mtk_snor_priv *priv,
304 const struct spi_mem_op *op)
305{
306 u8 *buf = op->data.buf.in;
307 int ret;
308
309 ret = mtk_snor_cmd_exec(priv, MTK_NOR_CMD_READ, 6 * BITS_PER_BYTE);
310 if (!ret)
311 buf[0] = readb(priv->base + MTK_NOR_REG_RDATA);
312 return ret;
313}
314
315static int mtk_snor_write_buffer_enable(struct mtk_snor_priv *priv)
316{
317 int ret;
318 u32 val;
319
320 if (priv->wbuf_en)
321 return 0;
322
323 val = readl(priv->base + MTK_NOR_REG_CFG2);
324 writel(val | MTK_NOR_WR_BUF_EN, priv->base + MTK_NOR_REG_CFG2);
325 ret = readl_poll_timeout(priv->base + MTK_NOR_REG_CFG2, val,
326 val & MTK_NOR_WR_BUF_EN, 10000);
327 if (!ret)
328 priv->wbuf_en = true;
329 return ret;
330}
331
332static int mtk_snor_write_buffer_disable(struct mtk_snor_priv *priv)
333{
334 int ret;
335 u32 val;
336
337 if (!priv->wbuf_en)
338 return 0;
339 val = readl(priv->base + MTK_NOR_REG_CFG2);
340 writel(val & ~MTK_NOR_WR_BUF_EN, priv->base + MTK_NOR_REG_CFG2);
341 ret = readl_poll_timeout(priv->base + MTK_NOR_REG_CFG2, val,
342 !(val & MTK_NOR_WR_BUF_EN), 10000);
343 if (!ret)
344 priv->wbuf_en = false;
345 return ret;
346}
347
348static int mtk_snor_pp_buffered(struct mtk_snor_priv *priv,
349 const struct spi_mem_op *op)
350{
351 const u8 *buf = op->data.buf.out;
352 u32 val;
353 int ret, i;
354
355 ret = mtk_snor_write_buffer_enable(priv);
356 if (ret < 0)
357 return ret;
358
359 for (i = 0; i < op->data.nbytes; i += 4) {
360 val = buf[i + 3] << 24 | buf[i + 2] << 16 | buf[i + 1] << 8 |
361 buf[i];
362 writel(val, priv->base + MTK_NOR_REG_PP_DATA);
363 }
364 mtk_snor_cmd_exec(priv, MTK_NOR_CMD_WRITE,
365 (op->data.nbytes + 5) * BITS_PER_BYTE);
366 return mtk_snor_write_buffer_disable(priv);
367}
368
369static int mtk_snor_pp_unbuffered(struct mtk_snor_priv *priv,
370 const struct spi_mem_op *op)
371{
372 const u8 *buf = op->data.buf.out;
373 int ret;
374
375 ret = mtk_snor_write_buffer_disable(priv);
376 if (ret < 0)
377 return ret;
378 writeb(buf[0], priv->base + MTK_NOR_REG_WDATA);
379 return mtk_snor_cmd_exec(priv, MTK_NOR_CMD_WRITE, 6 * BITS_PER_BYTE);
380}
381
382static int mtk_snor_cmd_program(struct mtk_snor_priv *priv,
383 const struct spi_mem_op *op)
384{
385 u32 tx_len = 0;
386 u32 trx_len = 0;
387 int reg_offset = MTK_NOR_REG_PRGDATA_MAX;
388 void __iomem *reg;
389 u8 *txbuf;
390 int tx_cnt = 0;
391 u8 *rxbuf = op->data.buf.in;
392 int i = 0;
393
394 tx_len = 1 + op->addr.nbytes + op->dummy.nbytes;
395 trx_len = tx_len + op->data.nbytes;
396 if (op->data.dir == SPI_MEM_DATA_OUT)
397 tx_len += op->data.nbytes;
398
399 txbuf = kmalloc_array(tx_len, sizeof(u8), GFP_KERNEL);
400 memset(txbuf, 0x0, tx_len * sizeof(u8));
401
402 /* Join all bytes to be transferred */
403 txbuf[tx_cnt] = op->cmd.opcode;
404 tx_cnt++;
405 for (i = op->addr.nbytes; i > 0; i--, tx_cnt++)
406 txbuf[tx_cnt] = ((u8 *)&op->addr.val)[i - 1];
407 for (i = op->dummy.nbytes; i > 0; i--, tx_cnt++)
408 txbuf[tx_cnt] = 0x0;
409 if (op->data.dir == SPI_MEM_DATA_OUT)
410 for (i = op->data.nbytes; i > 0; i--, tx_cnt++)
411 txbuf[tx_cnt] = ((u8 *)op->data.buf.out)[i - 1];
412
413 for (i = MTK_NOR_REG_PRGDATA_MAX; i >= 0; i--)
414 writeb(0, priv->base + MTK_NOR_REG_PRGDATA(i));
415
416 for (i = 0; i < tx_len; i++, reg_offset--)
417 writeb(txbuf[i], priv->base + MTK_NOR_REG_PRGDATA(reg_offset));
418
419 kfree(txbuf);
420
421 writel(trx_len * BITS_PER_BYTE, priv->base + MTK_NOR_REG_PRG_CNT);
422
423 mtk_snor_cmd_exec(priv, MTK_NOR_CMD_PROGRAM, trx_len * BITS_PER_BYTE);
424
425 reg_offset = op->data.nbytes - 1;
426 for (i = 0; i < op->data.nbytes; i++, reg_offset--) {
427 reg = priv->base + MTK_NOR_REG_SHIFT(reg_offset);
428 rxbuf[i] = readb(reg);
429 }
430
431 return 0;
432}
433
434static int mtk_snor_exec_op(struct spi_slave *slave,
435 const struct spi_mem_op *op)
436{
437 struct udevice *bus = dev_get_parent(slave->dev);
438 struct mtk_snor_priv *priv = dev_get_priv(bus);
439 int ret;
440
441 if (op->data.dir == SPI_MEM_NO_DATA || op->addr.nbytes == 0) {
442 return mtk_snor_cmd_program(priv, op);
443 } else if (op->data.dir == SPI_MEM_DATA_OUT) {
444 mtk_snor_set_addr(priv, op);
445 writeb(op->cmd.opcode, priv->base + MTK_NOR_REG_PRGDATA0);
446 if (op->data.nbytes == MTK_NOR_PP_SIZE)
447 return mtk_snor_pp_buffered(priv, op);
448 return mtk_snor_pp_unbuffered(priv, op);
449 } else if (op->data.dir == SPI_MEM_DATA_IN) {
450 ret = mtk_snor_write_buffer_disable(priv);
451 if (ret < 0)
452 return ret;
453 mtk_snor_setup_bus(priv, op);
454 if (op->data.nbytes == 1) {
455 mtk_snor_set_addr(priv, op);
456 return mtk_snor_read_pio(priv, op);
457 } else {
458 return mtk_snor_read_dma(priv, op);
459 }
460 }
461
462 return -ENOTSUPP;
463}
464
465static int mtk_snor_probe(struct udevice *bus)
466{
467 struct mtk_snor_priv *priv = dev_get_priv(bus);
468 u8 *buffer;
469 int ret;
470 u32 reg;
471
Johan Jonker2f9f7752023-03-13 01:32:44 +0100472 priv->base = devfdt_get_addr_ptr(bus);
developerf33cf3c2021-01-20 15:31:33 +0800473 if (!priv->base)
474 return -EINVAL;
475
476 ret = clk_get_by_name(bus, "spi", &priv->spi_clk);
477 if (ret < 0)
478 return ret;
479
480 ret = clk_get_by_name(bus, "sf", &priv->ctlr_clk);
481 if (ret < 0)
482 return ret;
483
484 buffer = devm_kmalloc(bus, MTK_NOR_BOUNCE_BUF_SIZE + MTK_NOR_DMA_ALIGN,
485 GFP_KERNEL);
486 if (!buffer)
487 return -ENOMEM;
488 if ((ulong)buffer & MTK_NOR_DMA_ALIGN_MASK)
489 buffer = (u8 *)(((ulong)buffer + MTK_NOR_DMA_ALIGN) &
490 ~MTK_NOR_DMA_ALIGN_MASK);
491 priv->buffer = buffer;
492
493 clk_enable(&priv->spi_clk);
494 clk_enable(&priv->ctlr_clk);
495
496 priv->spi_freq = clk_get_rate(&priv->spi_clk);
497 printf("spi frequency: %d Hz\n", priv->spi_freq);
498
499 /* With this setting, we issue one command at a time to
500 * accommodate to SPI-mem framework.
501 */
502 writel(MTK_NOR_ENABLE_SF_CMD, priv->base + MTK_NOR_REG_WP);
503 mtk_snor_rmw(priv, MTK_NOR_REG_CFG2, MTK_NOR_WR_CUSTOM_OP_EN, 0);
504 mtk_snor_rmw(priv, MTK_NOR_REG_CFG3,
505 MTK_NOR_DISABLE_WREN | MTK_NOR_DISABLE_SR_POLL, 0);
506
507 /* Unlock all blocks using write status command.
508 * SPI-MEM hasn't implemented unlock procedure on MXIC devices.
509 * We may remove this later.
510 */
511 writel(2 * BITS_PER_BYTE, priv->base + MTK_NOR_REG_PRG_CNT);
512 writel(MTK_NOR_UNLOCK_ALL, priv->base + MTK_NOR_REG_PRGDATA(5));
513 writel(MTK_NOR_IRQ_WRSR, priv->base + MTK_NOR_REG_IRQ_EN);
514 writel(MTK_NOR_CMD_WRSR, priv->base + MTK_NOR_REG_CMD);
515 ret = readl_poll_timeout(priv->base + MTK_NOR_REG_IRQ_STAT, reg,
516 !(reg & MTK_NOR_IRQ_WRSR),
517 ((3 * BITS_PER_BYTE) + 1) * 200);
518
519 return 0;
520}
521
522static int mtk_snor_set_speed(struct udevice *bus, uint speed)
523{
524 /* MTK's SNOR controller does not have a bus clock divider.
525 * We setup maximum bus clock in dts.
526 */
527
528 return 0;
529}
530
531static int mtk_snor_set_mode(struct udevice *bus, uint mode)
532{
533 /* We set up mode later for each transmission.
534 */
535 return 0;
536}
537
538static const struct spi_controller_mem_ops mtk_snor_mem_ops = {
539 .adjust_op_size = mtk_snor_adjust_op_size,
540 .supports_op = mtk_snor_supports_op,
541 .exec_op = mtk_snor_exec_op
542};
543
544static const struct dm_spi_ops mtk_snor_ops = {
545 .mem_ops = &mtk_snor_mem_ops,
546 .set_speed = mtk_snor_set_speed,
547 .set_mode = mtk_snor_set_mode,
548};
549
550static const struct udevice_id mtk_snor_ids[] = {
551 { .compatible = "mediatek,mtk-snor" },
552 {}
553};
554
555U_BOOT_DRIVER(mtk_snor) = {
556 .name = "mtk_snor",
557 .id = UCLASS_SPI,
558 .of_match = mtk_snor_ids,
559 .ops = &mtk_snor_ops,
560 .priv_auto = sizeof(struct mtk_snor_priv),
561 .probe = mtk_snor_probe,
562};