1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
293db446aSBoris Brezillon /*
393db446aSBoris Brezillon *
493db446aSBoris Brezillon * Copyright © 2012 John Crispin <john@phrozen.org>
593db446aSBoris Brezillon * Copyright © 2016 Hauke Mehrtens <hauke@hauke-m.de>
693db446aSBoris Brezillon */
793db446aSBoris Brezillon
893db446aSBoris Brezillon #include <linux/mtd/rawnand.h>
993db446aSBoris Brezillon #include <linux/of_gpio.h>
10*c2fc6b69SRob Herring #include <linux/of.h>
11*c2fc6b69SRob Herring #include <linux/platform_device.h>
1293db446aSBoris Brezillon
1393db446aSBoris Brezillon #include <lantiq_soc.h>
1493db446aSBoris Brezillon
1593db446aSBoris Brezillon /* nand registers */
1693db446aSBoris Brezillon #define EBU_ADDSEL1 0x24
1793db446aSBoris Brezillon #define EBU_NAND_CON 0xB0
1893db446aSBoris Brezillon #define EBU_NAND_WAIT 0xB4
1993db446aSBoris Brezillon #define NAND_WAIT_RD BIT(0) /* NAND flash status output */
2093db446aSBoris Brezillon #define NAND_WAIT_WR_C BIT(3) /* NAND Write/Read complete */
2193db446aSBoris Brezillon #define EBU_NAND_ECC0 0xB8
2293db446aSBoris Brezillon #define EBU_NAND_ECC_AC 0xBC
2393db446aSBoris Brezillon
2493db446aSBoris Brezillon /*
2593db446aSBoris Brezillon * nand commands
2693db446aSBoris Brezillon * The pins of the NAND chip are selected based on the address bits of the
2793db446aSBoris Brezillon * "register" read and write. There are no special registers, but an
2893db446aSBoris Brezillon * address range and the lower address bits are used to activate the
2993db446aSBoris Brezillon * correct line. For example when the bit (1 << 2) is set in the address
3093db446aSBoris Brezillon * the ALE pin will be activated.
3193db446aSBoris Brezillon */
3293db446aSBoris Brezillon #define NAND_CMD_ALE BIT(2) /* address latch enable */
3393db446aSBoris Brezillon #define NAND_CMD_CLE BIT(3) /* command latch enable */
3493db446aSBoris Brezillon #define NAND_CMD_CS BIT(4) /* chip select */
3593db446aSBoris Brezillon #define NAND_CMD_SE BIT(5) /* spare area access latch */
3693db446aSBoris Brezillon #define NAND_CMD_WP BIT(6) /* write protect */
3793db446aSBoris Brezillon #define NAND_WRITE_CMD (NAND_CMD_CS | NAND_CMD_CLE)
3893db446aSBoris Brezillon #define NAND_WRITE_ADDR (NAND_CMD_CS | NAND_CMD_ALE)
3993db446aSBoris Brezillon #define NAND_WRITE_DATA (NAND_CMD_CS)
4093db446aSBoris Brezillon #define NAND_READ_DATA (NAND_CMD_CS)
4193db446aSBoris Brezillon
4293db446aSBoris Brezillon /* we need to tel the ebu which addr we mapped the nand to */
4393db446aSBoris Brezillon #define ADDSEL1_MASK(x) (x << 4)
4493db446aSBoris Brezillon #define ADDSEL1_REGEN 1
4593db446aSBoris Brezillon
4693db446aSBoris Brezillon /* we need to tell the EBU that we have nand attached and set it up properly */
4793db446aSBoris Brezillon #define BUSCON1_SETUP (1 << 22)
4893db446aSBoris Brezillon #define BUSCON1_BCGEN_RES (0x3 << 12)
4993db446aSBoris Brezillon #define BUSCON1_WAITWRC2 (2 << 8)
5093db446aSBoris Brezillon #define BUSCON1_WAITRDC2 (2 << 6)
5193db446aSBoris Brezillon #define BUSCON1_HOLDC1 (1 << 4)
5293db446aSBoris Brezillon #define BUSCON1_RECOVC1 (1 << 2)
5393db446aSBoris Brezillon #define BUSCON1_CMULT4 1
5493db446aSBoris Brezillon
5593db446aSBoris Brezillon #define NAND_CON_CE (1 << 20)
5693db446aSBoris Brezillon #define NAND_CON_OUT_CS1 (1 << 10)
5793db446aSBoris Brezillon #define NAND_CON_IN_CS1 (1 << 8)
5893db446aSBoris Brezillon #define NAND_CON_PRE_P (1 << 7)
5993db446aSBoris Brezillon #define NAND_CON_WP_P (1 << 6)
6093db446aSBoris Brezillon #define NAND_CON_SE_P (1 << 5)
6193db446aSBoris Brezillon #define NAND_CON_CS_P (1 << 4)
6293db446aSBoris Brezillon #define NAND_CON_CSMUX (1 << 1)
6393db446aSBoris Brezillon #define NAND_CON_NANDM 1
6493db446aSBoris Brezillon
6593db446aSBoris Brezillon struct xway_nand_data {
66d525914bSMiquel Raynal struct nand_controller controller;
6793db446aSBoris Brezillon struct nand_chip chip;
6893db446aSBoris Brezillon unsigned long csflags;
6993db446aSBoris Brezillon void __iomem *nandaddr;
7093db446aSBoris Brezillon };
7193db446aSBoris Brezillon
xway_readb(struct mtd_info * mtd,int op)7293db446aSBoris Brezillon static u8 xway_readb(struct mtd_info *mtd, int op)
7393db446aSBoris Brezillon {
7493db446aSBoris Brezillon struct nand_chip *chip = mtd_to_nand(mtd);
7593db446aSBoris Brezillon struct xway_nand_data *data = nand_get_controller_data(chip);
7693db446aSBoris Brezillon
7793db446aSBoris Brezillon return readb(data->nandaddr + op);
7893db446aSBoris Brezillon }
7993db446aSBoris Brezillon
xway_writeb(struct mtd_info * mtd,int op,u8 value)8093db446aSBoris Brezillon static void xway_writeb(struct mtd_info *mtd, int op, u8 value)
8193db446aSBoris Brezillon {
8293db446aSBoris Brezillon struct nand_chip *chip = mtd_to_nand(mtd);
8393db446aSBoris Brezillon struct xway_nand_data *data = nand_get_controller_data(chip);
8493db446aSBoris Brezillon
8593db446aSBoris Brezillon writeb(value, data->nandaddr + op);
8693db446aSBoris Brezillon }
8793db446aSBoris Brezillon
xway_select_chip(struct nand_chip * chip,int select)88758b56f5SBoris Brezillon static void xway_select_chip(struct nand_chip *chip, int select)
8993db446aSBoris Brezillon {
9093db446aSBoris Brezillon struct xway_nand_data *data = nand_get_controller_data(chip);
9193db446aSBoris Brezillon
9293db446aSBoris Brezillon switch (select) {
9393db446aSBoris Brezillon case -1:
9493db446aSBoris Brezillon ltq_ebu_w32_mask(NAND_CON_CE, 0, EBU_NAND_CON);
9593db446aSBoris Brezillon ltq_ebu_w32_mask(NAND_CON_NANDM, 0, EBU_NAND_CON);
9693db446aSBoris Brezillon spin_unlock_irqrestore(&ebu_lock, data->csflags);
9793db446aSBoris Brezillon break;
9893db446aSBoris Brezillon case 0:
9993db446aSBoris Brezillon spin_lock_irqsave(&ebu_lock, data->csflags);
10093db446aSBoris Brezillon ltq_ebu_w32_mask(0, NAND_CON_NANDM, EBU_NAND_CON);
10193db446aSBoris Brezillon ltq_ebu_w32_mask(0, NAND_CON_CE, EBU_NAND_CON);
10293db446aSBoris Brezillon break;
10393db446aSBoris Brezillon default:
10493db446aSBoris Brezillon BUG();
10593db446aSBoris Brezillon }
10693db446aSBoris Brezillon }
10793db446aSBoris Brezillon
xway_cmd_ctrl(struct nand_chip * chip,int cmd,unsigned int ctrl)1080f808c16SBoris Brezillon static void xway_cmd_ctrl(struct nand_chip *chip, int cmd, unsigned int ctrl)
10993db446aSBoris Brezillon {
1100f808c16SBoris Brezillon struct mtd_info *mtd = nand_to_mtd(chip);
1110f808c16SBoris Brezillon
11293db446aSBoris Brezillon if (cmd == NAND_CMD_NONE)
11393db446aSBoris Brezillon return;
11493db446aSBoris Brezillon
11593db446aSBoris Brezillon if (ctrl & NAND_CLE)
11693db446aSBoris Brezillon xway_writeb(mtd, NAND_WRITE_CMD, cmd);
11793db446aSBoris Brezillon else if (ctrl & NAND_ALE)
11893db446aSBoris Brezillon xway_writeb(mtd, NAND_WRITE_ADDR, cmd);
11993db446aSBoris Brezillon
12093db446aSBoris Brezillon while ((ltq_ebu_r32(EBU_NAND_WAIT) & NAND_WAIT_WR_C) == 0)
12193db446aSBoris Brezillon ;
12293db446aSBoris Brezillon }
12393db446aSBoris Brezillon
xway_dev_ready(struct nand_chip * chip)12450a487e7SBoris Brezillon static int xway_dev_ready(struct nand_chip *chip)
12593db446aSBoris Brezillon {
12693db446aSBoris Brezillon return ltq_ebu_r32(EBU_NAND_WAIT) & NAND_WAIT_RD;
12793db446aSBoris Brezillon }
12893db446aSBoris Brezillon
xway_read_byte(struct nand_chip * chip)1297e534323SBoris Brezillon static unsigned char xway_read_byte(struct nand_chip *chip)
13093db446aSBoris Brezillon {
1317e534323SBoris Brezillon return xway_readb(nand_to_mtd(chip), NAND_READ_DATA);
13293db446aSBoris Brezillon }
13393db446aSBoris Brezillon
xway_read_buf(struct nand_chip * chip,u_char * buf,int len)1347e534323SBoris Brezillon static void xway_read_buf(struct nand_chip *chip, u_char *buf, int len)
13593db446aSBoris Brezillon {
13693db446aSBoris Brezillon int i;
13793db446aSBoris Brezillon
13893db446aSBoris Brezillon for (i = 0; i < len; i++)
1397e534323SBoris Brezillon buf[i] = xway_readb(nand_to_mtd(chip), NAND_WRITE_DATA);
14093db446aSBoris Brezillon }
14193db446aSBoris Brezillon
xway_write_buf(struct nand_chip * chip,const u_char * buf,int len)142c0739d85SBoris Brezillon static void xway_write_buf(struct nand_chip *chip, const u_char *buf, int len)
14393db446aSBoris Brezillon {
14493db446aSBoris Brezillon int i;
14593db446aSBoris Brezillon
14693db446aSBoris Brezillon for (i = 0; i < len; i++)
147c0739d85SBoris Brezillon xway_writeb(nand_to_mtd(chip), NAND_WRITE_DATA, buf[i]);
14893db446aSBoris Brezillon }
14993db446aSBoris Brezillon
xway_attach_chip(struct nand_chip * chip)150d525914bSMiquel Raynal static int xway_attach_chip(struct nand_chip *chip)
151d525914bSMiquel Raynal {
1526bcd2960SMiquel Raynal if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_SOFT &&
1536bcd2960SMiquel Raynal chip->ecc.algo == NAND_ECC_ALGO_UNKNOWN)
154d525914bSMiquel Raynal chip->ecc.algo = NAND_ECC_ALGO_HAMMING;
155d525914bSMiquel Raynal
156d525914bSMiquel Raynal return 0;
157d525914bSMiquel Raynal }
158d525914bSMiquel Raynal
159d525914bSMiquel Raynal static const struct nand_controller_ops xway_nand_ops = {
160d525914bSMiquel Raynal .attach_chip = xway_attach_chip,
161d525914bSMiquel Raynal };
162d525914bSMiquel Raynal
16393db446aSBoris Brezillon /*
16493db446aSBoris Brezillon * Probe for the NAND device.
16593db446aSBoris Brezillon */
xway_nand_probe(struct platform_device * pdev)16693db446aSBoris Brezillon static int xway_nand_probe(struct platform_device *pdev)
16793db446aSBoris Brezillon {
16893db446aSBoris Brezillon struct xway_nand_data *data;
16993db446aSBoris Brezillon struct mtd_info *mtd;
17093db446aSBoris Brezillon int err;
17193db446aSBoris Brezillon u32 cs;
17293db446aSBoris Brezillon u32 cs_flag = 0;
17393db446aSBoris Brezillon
17493db446aSBoris Brezillon /* Allocate memory for the device structure (and zero it) */
17593db446aSBoris Brezillon data = devm_kzalloc(&pdev->dev, sizeof(struct xway_nand_data),
17693db446aSBoris Brezillon GFP_KERNEL);
17793db446aSBoris Brezillon if (!data)
17893db446aSBoris Brezillon return -ENOMEM;
17993db446aSBoris Brezillon
180abac6563SCai Huoqing data->nandaddr = devm_platform_ioremap_resource(pdev, 0);
18193db446aSBoris Brezillon if (IS_ERR(data->nandaddr))
18293db446aSBoris Brezillon return PTR_ERR(data->nandaddr);
18393db446aSBoris Brezillon
18493db446aSBoris Brezillon nand_set_flash_node(&data->chip, pdev->dev.of_node);
18593db446aSBoris Brezillon mtd = nand_to_mtd(&data->chip);
18693db446aSBoris Brezillon mtd->dev.parent = &pdev->dev;
18793db446aSBoris Brezillon
188bf6065c6SBoris Brezillon data->chip.legacy.cmd_ctrl = xway_cmd_ctrl;
1898395b753SBoris Brezillon data->chip.legacy.dev_ready = xway_dev_ready;
1907d6c37e9SBoris Brezillon data->chip.legacy.select_chip = xway_select_chip;
191716bbbabSBoris Brezillon data->chip.legacy.write_buf = xway_write_buf;
192716bbbabSBoris Brezillon data->chip.legacy.read_buf = xway_read_buf;
193716bbbabSBoris Brezillon data->chip.legacy.read_byte = xway_read_byte;
1943cece3abSBoris Brezillon data->chip.legacy.chip_delay = 30;
19593db446aSBoris Brezillon
196d525914bSMiquel Raynal nand_controller_init(&data->controller);
197d525914bSMiquel Raynal data->controller.ops = &xway_nand_ops;
198d525914bSMiquel Raynal data->chip.controller = &data->controller;
19993db446aSBoris Brezillon
20093db446aSBoris Brezillon platform_set_drvdata(pdev, data);
20193db446aSBoris Brezillon nand_set_controller_data(&data->chip, data);
20293db446aSBoris Brezillon
20393db446aSBoris Brezillon /* load our CS from the DT. Either we find a valid 1 or default to 0 */
20493db446aSBoris Brezillon err = of_property_read_u32(pdev->dev.of_node, "lantiq,cs", &cs);
20593db446aSBoris Brezillon if (!err && cs == 1)
20693db446aSBoris Brezillon cs_flag = NAND_CON_IN_CS1 | NAND_CON_OUT_CS1;
20793db446aSBoris Brezillon
20893db446aSBoris Brezillon /* setup the EBU to run in NAND mode on our base addr */
20993db446aSBoris Brezillon ltq_ebu_w32(CPHYSADDR(data->nandaddr)
21093db446aSBoris Brezillon | ADDSEL1_MASK(3) | ADDSEL1_REGEN, EBU_ADDSEL1);
21193db446aSBoris Brezillon
21293db446aSBoris Brezillon ltq_ebu_w32(BUSCON1_SETUP | BUSCON1_BCGEN_RES | BUSCON1_WAITWRC2
21393db446aSBoris Brezillon | BUSCON1_WAITRDC2 | BUSCON1_HOLDC1 | BUSCON1_RECOVC1
21493db446aSBoris Brezillon | BUSCON1_CMULT4, LTQ_EBU_BUSCON1);
21593db446aSBoris Brezillon
21693db446aSBoris Brezillon ltq_ebu_w32(NAND_CON_NANDM | NAND_CON_CSMUX | NAND_CON_CS_P
21793db446aSBoris Brezillon | NAND_CON_SE_P | NAND_CON_WP_P | NAND_CON_PRE_P
21893db446aSBoris Brezillon | cs_flag, EBU_NAND_CON);
21993db446aSBoris Brezillon
2206bcd2960SMiquel Raynal /*
2216bcd2960SMiquel Raynal * This driver assumes that the default ECC engine should be TYPE_SOFT.
2226bcd2960SMiquel Raynal * Set ->engine_type before registering the NAND devices in order to
2236bcd2960SMiquel Raynal * provide a driver specific default value.
2246bcd2960SMiquel Raynal */
2256bcd2960SMiquel Raynal data->chip.ecc.engine_type = NAND_ECC_ENGINE_TYPE_SOFT;
2266bcd2960SMiquel Raynal
22793db446aSBoris Brezillon /* Scan to find existence of the device */
22800ad378fSBoris Brezillon err = nand_scan(&data->chip, 1);
22993db446aSBoris Brezillon if (err)
23093db446aSBoris Brezillon return err;
23193db446aSBoris Brezillon
23293db446aSBoris Brezillon err = mtd_device_register(mtd, NULL, 0);
23393db446aSBoris Brezillon if (err)
23434531be5SMiquel Raynal nand_cleanup(&data->chip);
23593db446aSBoris Brezillon
23693db446aSBoris Brezillon return err;
23793db446aSBoris Brezillon }
23893db446aSBoris Brezillon
23993db446aSBoris Brezillon /*
24093db446aSBoris Brezillon * Remove a NAND device.
24193db446aSBoris Brezillon */
xway_nand_remove(struct platform_device * pdev)242ec185b18SUwe Kleine-König static void xway_nand_remove(struct platform_device *pdev)
24393db446aSBoris Brezillon {
24493db446aSBoris Brezillon struct xway_nand_data *data = platform_get_drvdata(pdev);
2459fdd78f7SMiquel Raynal struct nand_chip *chip = &data->chip;
2469fdd78f7SMiquel Raynal int ret;
24793db446aSBoris Brezillon
248880bc529SMiquel Raynal ret = mtd_device_unregister(nand_to_mtd(chip));
2499fdd78f7SMiquel Raynal WARN_ON(ret);
2509fdd78f7SMiquel Raynal nand_cleanup(chip);
25193db446aSBoris Brezillon }
25293db446aSBoris Brezillon
25393db446aSBoris Brezillon static const struct of_device_id xway_nand_match[] = {
25493db446aSBoris Brezillon { .compatible = "lantiq,nand-xway" },
25593db446aSBoris Brezillon {},
25693db446aSBoris Brezillon };
25793db446aSBoris Brezillon
25893db446aSBoris Brezillon static struct platform_driver xway_nand_driver = {
25993db446aSBoris Brezillon .probe = xway_nand_probe,
260ec185b18SUwe Kleine-König .remove_new = xway_nand_remove,
26193db446aSBoris Brezillon .driver = {
26293db446aSBoris Brezillon .name = "lantiq,nand-xway",
26393db446aSBoris Brezillon .of_match_table = xway_nand_match,
26493db446aSBoris Brezillon },
26593db446aSBoris Brezillon };
26693db446aSBoris Brezillon
26793db446aSBoris Brezillon builtin_platform_driver(xway_nand_driver);
268