blob: 0e54b5f69389549fbd951f81d6cbe667dd95270c [file] [log] [blame]
Zhengxun Li01551712021-09-14 13:43:51 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2021 Macronix International Co., Ltd.
4 *
5 * Author:
6 * Zhengxun Li <zhengxunli@mxic.com.tw>
7 */
8
Zhengxun Li01551712021-09-14 13:43:51 +08009#include <clk.h>
10#include <dm.h>
11#include <malloc.h>
12#include <nand.h>
13#include <asm/io.h>
14#include <asm/arch/hardware.h>
15#include <dm/device_compat.h>
16#include <linux/bug.h>
17#include <linux/errno.h>
18#include <linux/iopoll.h>
19#include <linux/mtd/mtd.h>
20#include <linux/mtd/rawnand.h>
21#include <linux/mtd/partitions.h>
22#include <linux/mtd/nand_ecc.h>
23#include <linux/delay.h>
24
25#define HC_CFG 0x0
26#define HC_CFG_IF_CFG(x) ((x) << 27)
27#define HC_CFG_DUAL_SLAVE BIT(31)
28#define HC_CFG_INDIVIDUAL BIT(30)
29#define HC_CFG_NIO(x) (((x) / 4) << 27)
30#define HC_CFG_TYPE(s, t) ((t) << (23 + ((s) * 2)))
31#define HC_CFG_TYPE_SPI_NOR 0
32#define HC_CFG_TYPE_SPI_NAND 1
33#define HC_CFG_TYPE_SPI_RAM 2
34#define HC_CFG_TYPE_RAW_NAND 3
35#define HC_CFG_SLV_ACT(x) ((x) << 21)
36#define HC_CFG_CLK_PH_EN BIT(20)
37#define HC_CFG_CLK_POL_INV BIT(19)
38#define HC_CFG_BIG_ENDIAN BIT(18)
39#define HC_CFG_DATA_PASS BIT(17)
40#define HC_CFG_IDLE_SIO_LVL(x) ((x) << 16)
41#define HC_CFG_MAN_START_EN BIT(3)
42#define HC_CFG_MAN_START BIT(2)
43#define HC_CFG_MAN_CS_EN BIT(1)
44#define HC_CFG_MAN_CS_ASSERT BIT(0)
45
46#define INT_STS 0x4
47#define INT_STS_EN 0x8
48#define INT_SIG_EN 0xc
49#define INT_STS_ALL GENMASK(31, 0)
50#define INT_RDY_PIN BIT(26)
51#define INT_RDY_SR BIT(25)
52#define INT_LNR_SUSP BIT(24)
53#define INT_ECC_ERR BIT(17)
54#define INT_CRC_ERR BIT(16)
55#define INT_LWR_DIS BIT(12)
56#define INT_LRD_DIS BIT(11)
57#define INT_SDMA_INT BIT(10)
58#define INT_DMA_FINISH BIT(9)
59#define INT_RX_NOT_FULL BIT(3)
60#define INT_RX_NOT_EMPTY BIT(2)
61#define INT_TX_NOT_FULL BIT(1)
62#define INT_TX_EMPTY BIT(0)
63
64#define HC_EN 0x10
65#define HC_EN_BIT BIT(0)
66
67#define TXD(x) (0x14 + ((x) * 4))
68#define RXD 0x24
69
70#define SS_CTRL(s) (0x30 + ((s) * 4))
71#define LRD_CFG 0x44
72#define LWR_CFG 0x80
73#define RWW_CFG 0x70
74#define OP_READ BIT(23)
75#define OP_DUMMY_CYC(x) ((x) << 17)
76#define OP_ADDR_BYTES(x) ((x) << 14)
77#define OP_CMD_BYTES(x) (((x) - 1) << 13)
78#define OP_OCTA_CRC_EN BIT(12)
79#define OP_DQS_EN BIT(11)
80#define OP_ENHC_EN BIT(10)
81#define OP_PREAMBLE_EN BIT(9)
82#define OP_DATA_DDR BIT(8)
83#define OP_DATA_BUSW(x) ((x) << 6)
84#define OP_ADDR_DDR BIT(5)
85#define OP_ADDR_BUSW(x) ((x) << 3)
86#define OP_CMD_DDR BIT(2)
87#define OP_CMD_BUSW(x) (x)
88#define OP_BUSW_1 0
89#define OP_BUSW_2 1
90#define OP_BUSW_4 2
91#define OP_BUSW_8 3
92
93#define OCTA_CRC 0x38
94#define OCTA_CRC_IN_EN(s) BIT(3 + ((s) * 16))
95#define OCTA_CRC_CHUNK(s, x) ((fls((x) / 32)) << (1 + ((s) * 16)))
96#define OCTA_CRC_OUT_EN(s) BIT(0 + ((s) * 16))
97
98#define ONFI_DIN_CNT(s) (0x3c + (s))
99
100#define LRD_CTRL 0x48
101#define RWW_CTRL 0x74
102#define LWR_CTRL 0x84
103#define LMODE_EN BIT(31)
104#define LMODE_SLV_ACT(x) ((x) << 21)
105#define LMODE_CMD1(x) ((x) << 8)
106#define LMODE_CMD0(x) (x)
107
108#define LRD_ADDR 0x4c
109#define LWR_ADDR 0x88
110#define LRD_RANGE 0x50
111#define LWR_RANGE 0x8c
112
113#define AXI_SLV_ADDR 0x54
114
115#define DMAC_RD_CFG 0x58
116#define DMAC_WR_CFG 0x94
117#define DMAC_CFG_PERIPH_EN BIT(31)
118#define DMAC_CFG_ALLFLUSH_EN BIT(30)
119#define DMAC_CFG_LASTFLUSH_EN BIT(29)
120#define DMAC_CFG_QE(x) (((x) + 1) << 16)
121#define DMAC_CFG_BURST_LEN(x) (((x) + 1) << 12)
122#define DMAC_CFG_BURST_SZ(x) ((x) << 8)
123#define DMAC_CFG_DIR_READ BIT(1)
124#define DMAC_CFG_START BIT(0)
125
126#define DMAC_RD_CNT 0x5c
127#define DMAC_WR_CNT 0x98
128
129#define SDMA_ADDR 0x60
130
131#define DMAM_CFG 0x64
132#define DMAM_CFG_START BIT(31)
133#define DMAM_CFG_CONT BIT(30)
134#define DMAM_CFG_SDMA_GAP(x) (fls((x) / 8192) << 2)
135#define DMAM_CFG_DIR_READ BIT(1)
136#define DMAM_CFG_EN BIT(0)
137
138#define DMAM_CNT 0x68
139
140#define LNR_TIMER_TH 0x6c
141
142#define RDM_CFG0 0x78
143#define RDM_CFG0_POLY(x) (x)
144
145#define RDM_CFG1 0x7c
146#define RDM_CFG1_RDM_EN BIT(31)
147#define RDM_CFG1_SEED(x) (x)
148
149#define LWR_SUSP_CTRL 0x90
150#define LWR_SUSP_CTRL_EN BIT(31)
151
152#define DMAS_CTRL 0x9c
153#define DMAS_CTRL_EN BIT(31)
154#define DMAS_CTRL_DIR_READ BIT(30)
155
156#define DATA_STROB 0xa0
157#define DATA_STROB_EDO_EN BIT(2)
158#define DATA_STROB_INV_POL BIT(1)
159#define DATA_STROB_DELAY_2CYC BIT(0)
160
161#define IDLY_CODE(x) (0xa4 + ((x) * 4))
162#define IDLY_CODE_VAL(x, v) ((v) << (((x) % 4) * 8))
163
164#define GPIO 0xc4
165#define GPIO_PT(x) BIT(3 + ((x) * 16))
166#define GPIO_RESET(x) BIT(2 + ((x) * 16))
167#define GPIO_HOLDB(x) BIT(1 + ((x) * 16))
168#define GPIO_WPB(x) BIT((x) * 16)
169
170#define HC_VER 0xd0
171
172#define HW_TEST(x) (0xe0 + ((x) * 4))
173
174#define MXIC_NFC_MAX_CLK_HZ 50000000
175#define IRQ_TIMEOUT 1000
176
177struct mxic_nand_ctrl {
178 struct clk *send_clk;
179 struct clk *send_dly_clk;
180 void __iomem *regs;
181 struct nand_chip nand_chip;
182};
183
184/*
185 * struct mxic_nfc_command_format - Defines NAND flash command format
186 * @start_cmd: First cycle command (Start command)
187 * @end_cmd: Second cycle command (Last command)
188 * @addr_len: Number of address cycles required to send the address
189 * @read: Direction of command
190 */
191
192struct mxic_nfc_command_format {
193 int start_cmd;
194 int end_cmd;
195 u8 addr_len;
196 bool read;
197};
198
199/* The NAND flash operations command format */
200static const struct mxic_nfc_command_format mxic_nand_commands[] = {
201 {NAND_CMD_READ0, NAND_CMD_READSTART, 5, 1 },
202 {NAND_CMD_RNDOUT, NAND_CMD_RNDOUTSTART, 2, 1 },
203 {NAND_CMD_READID, NAND_CMD_NONE, 1, 1 },
204 {NAND_CMD_STATUS, NAND_CMD_NONE, 0, 1 },
205 {NAND_CMD_SEQIN, NAND_CMD_NONE, 5, 0 },
206 {NAND_CMD_PAGEPROG, NAND_CMD_NONE, 0, 0 },
207 {NAND_CMD_CACHEDPROG, NAND_CMD_NONE, 0, 0 },
208 {NAND_CMD_RNDIN, NAND_CMD_NONE, 2, 0 },
209 {NAND_CMD_ERASE1, NAND_CMD_NONE, 3, 0 },
210 {NAND_CMD_ERASE2, NAND_CMD_NONE, 0, 0 },
211 {NAND_CMD_RESET, NAND_CMD_NONE, 0, 0 },
212 {NAND_CMD_PARAM, NAND_CMD_NONE, 1, 1 },
213 {NAND_CMD_GET_FEATURES, NAND_CMD_NONE, 1, 1 },
214 {NAND_CMD_SET_FEATURES, NAND_CMD_NONE, 1, 0 },
215 {NAND_CMD_NONE, NAND_CMD_NONE, 0, 0 },
216};
217
218static int mxic_nfc_clk_enable(struct mxic_nand_ctrl *nfc)
219{
220 int ret;
221
222 ret = clk_prepare_enable(nfc->send_clk);
223 if (ret)
224 return ret;
225
226 ret = clk_prepare_enable(nfc->send_dly_clk);
227 if (ret)
228 goto err_send_dly_clk;
229
230 return ret;
231
232err_send_dly_clk:
233 clk_disable_unprepare(nfc->send_clk);
234
235 return ret;
236}
237
238static void mxic_nfc_clk_disable(struct mxic_nand_ctrl *nfc)
239{
240 clk_disable_unprepare(nfc->send_clk);
241 clk_disable_unprepare(nfc->send_dly_clk);
242}
243
244static void mxic_nfc_set_input_delay(struct mxic_nand_ctrl *nfc, u8 idly_code)
245{
246 writel(IDLY_CODE_VAL(0, idly_code) |
247 IDLY_CODE_VAL(1, idly_code) |
248 IDLY_CODE_VAL(2, idly_code) |
249 IDLY_CODE_VAL(3, idly_code),
250 nfc->regs + IDLY_CODE(0));
251 writel(IDLY_CODE_VAL(4, idly_code) |
252 IDLY_CODE_VAL(5, idly_code) |
253 IDLY_CODE_VAL(6, idly_code) |
254 IDLY_CODE_VAL(7, idly_code),
255 nfc->regs + IDLY_CODE(1));
256}
257
258static int mxic_nfc_clk_setup(struct mxic_nand_ctrl *nfc, unsigned long freq)
259{
260 int ret;
261
262 ret = clk_set_rate(nfc->send_clk, freq);
263 if (ret)
264 return ret;
265
266 ret = clk_set_rate(nfc->send_dly_clk, freq);
267 if (ret)
268 return ret;
269
270 /*
271 * A constant delay range from 0x0 ~ 0x1F for input delay,
272 * the unit is 78 ps, the max input delay is 2.418 ns.
273 */
274 mxic_nfc_set_input_delay(nfc, 0xf);
275
276 return 0;
277}
278
279static int mxic_nfc_set_freq(struct mxic_nand_ctrl *nfc, unsigned long freq)
280{
281 int ret;
282
283 if (freq > MXIC_NFC_MAX_CLK_HZ)
284 freq = MXIC_NFC_MAX_CLK_HZ;
285
286 mxic_nfc_clk_disable(nfc);
287 ret = mxic_nfc_clk_setup(nfc, freq);
288 if (ret)
289 return ret;
290
291 ret = mxic_nfc_clk_enable(nfc);
292 if (ret)
293 return ret;
294
295 return 0;
296}
297
298static void mxic_nfc_hw_init(struct mxic_nand_ctrl *nfc)
299{
300 writel(HC_CFG_NIO(8) | HC_CFG_TYPE(1, HC_CFG_TYPE_RAW_NAND) |
301 HC_CFG_SLV_ACT(0) | HC_CFG_MAN_CS_EN |
302 HC_CFG_IDLE_SIO_LVL(1), nfc->regs + HC_CFG);
303 writel(INT_STS_ALL, nfc->regs + INT_STS_EN);
304 writel(INT_RDY_PIN, nfc->regs + INT_SIG_EN);
305 writel(0x0, nfc->regs + ONFI_DIN_CNT(0));
306 writel(0, nfc->regs + LRD_CFG);
307 writel(0, nfc->regs + LRD_CTRL);
308 writel(0x0, nfc->regs + HC_EN);
309}
310
311static void mxic_nfc_cs_enable(struct mxic_nand_ctrl *nfc)
312{
313 writel(readl(nfc->regs + HC_CFG) | HC_CFG_MAN_CS_EN,
314 nfc->regs + HC_CFG);
315 writel(HC_CFG_MAN_CS_ASSERT | readl(nfc->regs + HC_CFG),
316 nfc->regs + HC_CFG);
317}
318
319static void mxic_nfc_cs_disable(struct mxic_nand_ctrl *nfc)
320{
321 writel(~HC_CFG_MAN_CS_ASSERT & readl(nfc->regs + HC_CFG),
322 nfc->regs + HC_CFG);
323}
324
325static int mxic_nfc_data_xfer(struct mxic_nand_ctrl *nfc, const void *txbuf,
326 void *rxbuf, unsigned int len)
327{
328 unsigned int pos = 0;
329
330 while (pos < len) {
331 unsigned int nbytes = len - pos;
332 u32 data = 0xffffffff;
333 u32 sts;
334 int ret;
335
336 if (nbytes > 4)
337 nbytes = 4;
338
339 if (txbuf)
340 memcpy(&data, txbuf + pos, nbytes);
341
342 ret = readl_poll_timeout(nfc->regs + INT_STS, sts,
343 sts & INT_TX_EMPTY, 1000000);
344 if (ret)
345 return ret;
346
347 writel(data, nfc->regs + TXD(nbytes % 4));
348
349 ret = readl_poll_timeout(nfc->regs + INT_STS, sts,
350 sts & INT_TX_EMPTY, 1000000);
351 if (ret)
352 return ret;
353
354 ret = readl_poll_timeout(nfc->regs + INT_STS, sts,
355 sts & INT_RX_NOT_EMPTY, 1000000);
356 if (ret)
357 return ret;
358
359 data = readl(nfc->regs + RXD);
360 if (rxbuf) {
361 data >>= (8 * (4 - nbytes));
362 memcpy(rxbuf + pos, &data, nbytes);
363 }
364
365 WARN_ON(readl(nfc->regs + INT_STS) & INT_RX_NOT_EMPTY);
366
367 pos += nbytes;
368 }
369
370 return 0;
371}
372
373static uint8_t mxic_nfc_read_byte(struct mtd_info *mtd)
374{
375 struct nand_chip *chip = mtd_to_nand(mtd);
376 struct mxic_nand_ctrl *nfc = nand_get_controller_data(chip);
377 u8 data;
378
379 writel(0x0, nfc->regs + ONFI_DIN_CNT(0));
380 writel(OP_DATA_BUSW(OP_BUSW_8) | OP_DUMMY_CYC(0x3F) |
381 OP_READ, nfc->regs + SS_CTRL(0));
382
383 mxic_nfc_data_xfer(nfc, NULL, &data, 1);
384
385 return data;
386}
387
388static void mxic_nfc_read_buf(struct mtd_info *mtd, uint8_t *rxbuf, int rlen)
389{
390 struct nand_chip *chip = mtd_to_nand(mtd);
391 struct mxic_nand_ctrl *nfc = nand_get_controller_data(chip);
392
393 writel(0x0, nfc->regs + ONFI_DIN_CNT(0));
394 writel(OP_DATA_BUSW(OP_BUSW_8) | OP_DUMMY_CYC(0x3F) |
395 OP_READ, nfc->regs + SS_CTRL(0));
396
397 mxic_nfc_data_xfer(nfc, NULL, rxbuf, rlen);
398}
399
400static void mxic_nfc_write_buf(struct mtd_info *mtd, const uint8_t *txbuf,
401 int wlen)
402{
403 struct nand_chip *chip = mtd_to_nand(mtd);
404 struct mxic_nand_ctrl *nfc = nand_get_controller_data(chip);
405
406 writel(wlen, nfc->regs + ONFI_DIN_CNT(0));
407 writel(OP_DATA_BUSW(OP_BUSW_8) | OP_DUMMY_CYC(0x3F),
408 nfc->regs + SS_CTRL(0));
409
410 mxic_nfc_data_xfer(nfc, txbuf, NULL, wlen);
411}
412
413static void mxic_nfc_cmd_function(struct mtd_info *mtd, unsigned int command,
414 int column, int page_addr)
415{
416 struct nand_chip *chip = mtd_to_nand(mtd);
417 struct mxic_nand_ctrl *nfc = nand_get_controller_data(chip);
418 const struct mxic_nfc_command_format *cmd = NULL;
419 u32 sts;
420 u8 index, addr[5];
421
422 /* Emulate NAND_CMD_READOOB */
423 if (command == NAND_CMD_READOOB) {
424 column += mtd->writesize;
425 command = NAND_CMD_READ0;
426 }
427
428 /* Get the command format */
429 for (index = 0; index < ARRAY_SIZE(mxic_nand_commands); index++)
430 if (command == mxic_nand_commands[index].start_cmd)
431 break;
432
433 cmd = &mxic_nand_commands[index];
434
435 if (!(command == NAND_CMD_PAGEPROG ||
436 command == NAND_CMD_CACHEDPROG ||
437 command == NAND_CMD_ERASE2))
438 mxic_nfc_cs_disable(nfc);
439
440 mxic_nfc_cs_enable(nfc);
441
442 if (column != -1) {
443 addr[0] = column;
444 addr[1] = column >> 8;
445
446 if (page_addr != -1) {
447 addr[2] = page_addr;
448 addr[3] = page_addr >> 8;
449 addr[4] = page_addr >> 16;
450 }
451 } else if (page_addr != -1) {
452 addr[0] = page_addr;
453 addr[1] = page_addr >> 8;
454 addr[2] = page_addr >> 16;
455 }
456
457 writel(0, nfc->regs + HC_EN);
458 writel(HC_EN_BIT, nfc->regs + HC_EN);
459 writel(OP_CMD_BUSW(OP_BUSW_8) | OP_DUMMY_CYC(0x3F) | OP_CMD_BYTES(0),
460 nfc->regs + SS_CTRL(0));
461
462 mxic_nfc_data_xfer(nfc, &cmd->start_cmd, NULL, 1);
463
464 if (cmd->addr_len) {
465 writel(OP_ADDR_BUSW(OP_BUSW_8) | OP_DUMMY_CYC(0x3F) |
466 OP_ADDR_BYTES(cmd->addr_len), nfc->regs + SS_CTRL(0));
467
468 mxic_nfc_data_xfer(nfc, &addr, NULL, cmd->addr_len);
469 }
470
471 if (cmd->end_cmd != NAND_CMD_NONE) {
472 writel(0, nfc->regs + HC_EN);
473 writel(HC_EN_BIT, nfc->regs + HC_EN);
474 writel(OP_CMD_BUSW(OP_BUSW_8) | OP_DUMMY_CYC(0x3F) |
475 OP_CMD_BYTES(0), nfc->regs + SS_CTRL(0));
476
477 mxic_nfc_data_xfer(nfc, &cmd->end_cmd, NULL, 1);
478 }
479
480 readl_poll_timeout(nfc->regs + INT_STS, sts, sts & INT_RDY_PIN,
481 1000000);
482
483 if (command == NAND_CMD_PAGEPROG ||
484 command == NAND_CMD_CACHEDPROG ||
485 command == NAND_CMD_ERASE2 ||
486 command == NAND_CMD_RESET) {
487 mxic_nfc_cs_disable(nfc);
488 }
489}
490
491static int mxic_nfc_setup_data_interface(struct mtd_info *mtd, int chipnr,
492 const struct nand_data_interface *conf)
493{
494 struct nand_chip *chip = mtd_to_nand(mtd);
495 struct mxic_nand_ctrl *nfc = nand_get_controller_data(chip);
496 const struct nand_sdr_timings *sdr;
497 unsigned long freq;
498 int ret;
499
500 sdr = nand_get_sdr_timings(conf);
501 if (IS_ERR(sdr))
502 return PTR_ERR(sdr);
503
504 if (chipnr == NAND_DATA_IFACE_CHECK_ONLY)
505 return 0;
506
507 freq = 1000000000 / (sdr->tRC_min / 1000);
508
509 ret = mxic_nfc_set_freq(nfc, freq);
510 if (ret)
511 WARN_ON("Set freq failed\n");
512
513 if (sdr->tRC_min < 30000)
514 writel(DATA_STROB_EDO_EN, nfc->regs + DATA_STROB);
515
516 return 0;
517}
518
519/* Dummy implementation: we don't support multiple chips */
520static void mxic_nfc_select_chip(struct mtd_info *mtd, int chipnr)
521{
522 switch (chipnr) {
523 case -1:
524 case 0:
525 break;
526
527 default:
528 BUG();
529 }
530}
531
532static int mxic_nfc_probe(struct udevice *dev)
533{
534 struct mxic_nand_ctrl *nfc = dev_get_priv(dev);
535 struct nand_chip *nand_chip = &nfc->nand_chip;
536 struct mtd_info *mtd;
537 ofnode child;
538 int err;
539
Johan Jonker8d5d8e02023-03-13 01:32:04 +0100540 nfc->regs = dev_read_addr_ptr(dev);
Zhengxun Li01551712021-09-14 13:43:51 +0800541
542 nfc->send_clk = devm_clk_get(dev, "send");
543 if (IS_ERR(nfc->send_clk))
544 return PTR_ERR(nfc->send_clk);
545
546 nfc->send_dly_clk = devm_clk_get(dev, "send_dly");
547 if (IS_ERR(nfc->send_dly_clk))
548 return PTR_ERR(nfc->send_dly_clk);
549
550 mtd = nand_to_mtd(nand_chip);
551
552 ofnode_for_each_subnode(child, dev_ofnode(dev))
553 nand_set_flash_node(nand_chip, child);
554
555 nand_set_controller_data(nand_chip, nfc);
556
557 nand_chip->select_chip = mxic_nfc_select_chip;
558 nand_chip->setup_data_interface = mxic_nfc_setup_data_interface;
559 nand_chip->cmdfunc = mxic_nfc_cmd_function;
560 nand_chip->read_byte = mxic_nfc_read_byte;
561 nand_chip->read_buf = mxic_nfc_read_buf;
562 nand_chip->write_buf = mxic_nfc_write_buf;
563
564 mxic_nfc_hw_init(nfc);
565
566 err = nand_scan(mtd, 1);
567 if (err)
568 return err;
569
570 err = nand_register(0, mtd);
571 if (err) {
572 dev_err(dev, "Failed to register MTD: %d\n", err);
573 return err;
574 }
575
576 return 0;
577}
578
579static const struct udevice_id mxic_nfc_of_ids[] = {
580 { .compatible = "mxic,multi-itfc-v009-nand-controller" },
581 { /* Sentinel */ }
582};
583
584U_BOOT_DRIVER(mxic_nfc) = {
585 .name = "mxic_nfc",
586 .id = UCLASS_MTD,
587 .of_match = mxic_nfc_of_ids,
588 .probe = mxic_nfc_probe,
589 .priv_auto = sizeof(struct mxic_nand_ctrl),
590};
591
592void board_nand_init(void)
593{
594 struct udevice *dev;
595 int ret;
596
597 ret = uclass_get_device_by_driver(UCLASS_MTD,
598 DM_DRIVER_GET(mxic_nfc), &dev);
599 if (ret && ret != -ENODEV)
600 pr_err("Failed to initialize %s. (error %d)\n", dev->name,
601 ret);
602}