xref: /openbmc/linux/drivers/mtd/nand/raw/xway_nand.c (revision abac656349cb9f081bc3b0a4c75d98486ade77f0)
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>
1093db446aSBoris Brezillon #include <linux/of_platform.h>
1193db446aSBoris Brezillon 
1293db446aSBoris Brezillon #include <lantiq_soc.h>
1393db446aSBoris Brezillon 
1493db446aSBoris Brezillon /* nand registers */
1593db446aSBoris Brezillon #define EBU_ADDSEL1		0x24
1693db446aSBoris Brezillon #define EBU_NAND_CON		0xB0
1793db446aSBoris Brezillon #define EBU_NAND_WAIT		0xB4
1893db446aSBoris Brezillon #define  NAND_WAIT_RD		BIT(0) /* NAND flash status output */
1993db446aSBoris Brezillon #define  NAND_WAIT_WR_C		BIT(3) /* NAND Write/Read complete */
2093db446aSBoris Brezillon #define EBU_NAND_ECC0		0xB8
2193db446aSBoris Brezillon #define EBU_NAND_ECC_AC		0xBC
2293db446aSBoris Brezillon 
2393db446aSBoris Brezillon /*
2493db446aSBoris Brezillon  * nand commands
2593db446aSBoris Brezillon  * The pins of the NAND chip are selected based on the address bits of the
2693db446aSBoris Brezillon  * "register" read and write. There are no special registers, but an
2793db446aSBoris Brezillon  * address range and the lower address bits are used to activate the
2893db446aSBoris Brezillon  * correct line. For example when the bit (1 << 2) is set in the address
2993db446aSBoris Brezillon  * the ALE pin will be activated.
3093db446aSBoris Brezillon  */
3193db446aSBoris Brezillon #define NAND_CMD_ALE		BIT(2) /* address latch enable */
3293db446aSBoris Brezillon #define NAND_CMD_CLE		BIT(3) /* command latch enable */
3393db446aSBoris Brezillon #define NAND_CMD_CS		BIT(4) /* chip select */
3493db446aSBoris Brezillon #define NAND_CMD_SE		BIT(5) /* spare area access latch */
3593db446aSBoris Brezillon #define NAND_CMD_WP		BIT(6) /* write protect */
3693db446aSBoris Brezillon #define NAND_WRITE_CMD		(NAND_CMD_CS | NAND_CMD_CLE)
3793db446aSBoris Brezillon #define NAND_WRITE_ADDR		(NAND_CMD_CS | NAND_CMD_ALE)
3893db446aSBoris Brezillon #define NAND_WRITE_DATA		(NAND_CMD_CS)
3993db446aSBoris Brezillon #define NAND_READ_DATA		(NAND_CMD_CS)
4093db446aSBoris Brezillon 
4193db446aSBoris Brezillon /* we need to tel the ebu which addr we mapped the nand to */
4293db446aSBoris Brezillon #define ADDSEL1_MASK(x)		(x << 4)
4393db446aSBoris Brezillon #define ADDSEL1_REGEN		1
4493db446aSBoris Brezillon 
4593db446aSBoris Brezillon /* we need to tell the EBU that we have nand attached and set it up properly */
4693db446aSBoris Brezillon #define BUSCON1_SETUP		(1 << 22)
4793db446aSBoris Brezillon #define BUSCON1_BCGEN_RES	(0x3 << 12)
4893db446aSBoris Brezillon #define BUSCON1_WAITWRC2	(2 << 8)
4993db446aSBoris Brezillon #define BUSCON1_WAITRDC2	(2 << 6)
5093db446aSBoris Brezillon #define BUSCON1_HOLDC1		(1 << 4)
5193db446aSBoris Brezillon #define BUSCON1_RECOVC1		(1 << 2)
5293db446aSBoris Brezillon #define BUSCON1_CMULT4		1
5393db446aSBoris Brezillon 
5493db446aSBoris Brezillon #define NAND_CON_CE		(1 << 20)
5593db446aSBoris Brezillon #define NAND_CON_OUT_CS1	(1 << 10)
5693db446aSBoris Brezillon #define NAND_CON_IN_CS1		(1 << 8)
5793db446aSBoris Brezillon #define NAND_CON_PRE_P		(1 << 7)
5893db446aSBoris Brezillon #define NAND_CON_WP_P		(1 << 6)
5993db446aSBoris Brezillon #define NAND_CON_SE_P		(1 << 5)
6093db446aSBoris Brezillon #define NAND_CON_CS_P		(1 << 4)
6193db446aSBoris Brezillon #define NAND_CON_CSMUX		(1 << 1)
6293db446aSBoris Brezillon #define NAND_CON_NANDM		1
6393db446aSBoris Brezillon 
6493db446aSBoris Brezillon struct xway_nand_data {
65d525914bSMiquel Raynal 	struct nand_controller	controller;
6693db446aSBoris Brezillon 	struct nand_chip	chip;
6793db446aSBoris Brezillon 	unsigned long		csflags;
6893db446aSBoris Brezillon 	void __iomem		*nandaddr;
6993db446aSBoris Brezillon };
7093db446aSBoris Brezillon 
7193db446aSBoris Brezillon static u8 xway_readb(struct mtd_info *mtd, int op)
7293db446aSBoris Brezillon {
7393db446aSBoris Brezillon 	struct nand_chip *chip = mtd_to_nand(mtd);
7493db446aSBoris Brezillon 	struct xway_nand_data *data = nand_get_controller_data(chip);
7593db446aSBoris Brezillon 
7693db446aSBoris Brezillon 	return readb(data->nandaddr + op);
7793db446aSBoris Brezillon }
7893db446aSBoris Brezillon 
7993db446aSBoris Brezillon static void xway_writeb(struct mtd_info *mtd, int op, u8 value)
8093db446aSBoris Brezillon {
8193db446aSBoris Brezillon 	struct nand_chip *chip = mtd_to_nand(mtd);
8293db446aSBoris Brezillon 	struct xway_nand_data *data = nand_get_controller_data(chip);
8393db446aSBoris Brezillon 
8493db446aSBoris Brezillon 	writeb(value, data->nandaddr + op);
8593db446aSBoris Brezillon }
8693db446aSBoris Brezillon 
87758b56f5SBoris Brezillon static void xway_select_chip(struct nand_chip *chip, int select)
8893db446aSBoris Brezillon {
8993db446aSBoris Brezillon 	struct xway_nand_data *data = nand_get_controller_data(chip);
9093db446aSBoris Brezillon 
9193db446aSBoris Brezillon 	switch (select) {
9293db446aSBoris Brezillon 	case -1:
9393db446aSBoris Brezillon 		ltq_ebu_w32_mask(NAND_CON_CE, 0, EBU_NAND_CON);
9493db446aSBoris Brezillon 		ltq_ebu_w32_mask(NAND_CON_NANDM, 0, EBU_NAND_CON);
9593db446aSBoris Brezillon 		spin_unlock_irqrestore(&ebu_lock, data->csflags);
9693db446aSBoris Brezillon 		break;
9793db446aSBoris Brezillon 	case 0:
9893db446aSBoris Brezillon 		spin_lock_irqsave(&ebu_lock, data->csflags);
9993db446aSBoris Brezillon 		ltq_ebu_w32_mask(0, NAND_CON_NANDM, EBU_NAND_CON);
10093db446aSBoris Brezillon 		ltq_ebu_w32_mask(0, NAND_CON_CE, EBU_NAND_CON);
10193db446aSBoris Brezillon 		break;
10293db446aSBoris Brezillon 	default:
10393db446aSBoris Brezillon 		BUG();
10493db446aSBoris Brezillon 	}
10593db446aSBoris Brezillon }
10693db446aSBoris Brezillon 
1070f808c16SBoris Brezillon static void xway_cmd_ctrl(struct nand_chip *chip, int cmd, unsigned int ctrl)
10893db446aSBoris Brezillon {
1090f808c16SBoris Brezillon 	struct mtd_info *mtd = nand_to_mtd(chip);
1100f808c16SBoris Brezillon 
11193db446aSBoris Brezillon 	if (cmd == NAND_CMD_NONE)
11293db446aSBoris Brezillon 		return;
11393db446aSBoris Brezillon 
11493db446aSBoris Brezillon 	if (ctrl & NAND_CLE)
11593db446aSBoris Brezillon 		xway_writeb(mtd, NAND_WRITE_CMD, cmd);
11693db446aSBoris Brezillon 	else if (ctrl & NAND_ALE)
11793db446aSBoris Brezillon 		xway_writeb(mtd, NAND_WRITE_ADDR, cmd);
11893db446aSBoris Brezillon 
11993db446aSBoris Brezillon 	while ((ltq_ebu_r32(EBU_NAND_WAIT) & NAND_WAIT_WR_C) == 0)
12093db446aSBoris Brezillon 		;
12193db446aSBoris Brezillon }
12293db446aSBoris Brezillon 
12350a487e7SBoris Brezillon static int xway_dev_ready(struct nand_chip *chip)
12493db446aSBoris Brezillon {
12593db446aSBoris Brezillon 	return ltq_ebu_r32(EBU_NAND_WAIT) & NAND_WAIT_RD;
12693db446aSBoris Brezillon }
12793db446aSBoris Brezillon 
1287e534323SBoris Brezillon static unsigned char xway_read_byte(struct nand_chip *chip)
12993db446aSBoris Brezillon {
1307e534323SBoris Brezillon 	return xway_readb(nand_to_mtd(chip), NAND_READ_DATA);
13193db446aSBoris Brezillon }
13293db446aSBoris Brezillon 
1337e534323SBoris Brezillon static void xway_read_buf(struct nand_chip *chip, u_char *buf, int len)
13493db446aSBoris Brezillon {
13593db446aSBoris Brezillon 	int i;
13693db446aSBoris Brezillon 
13793db446aSBoris Brezillon 	for (i = 0; i < len; i++)
1387e534323SBoris Brezillon 		buf[i] = xway_readb(nand_to_mtd(chip), NAND_WRITE_DATA);
13993db446aSBoris Brezillon }
14093db446aSBoris Brezillon 
141c0739d85SBoris Brezillon static void xway_write_buf(struct nand_chip *chip, const u_char *buf, int len)
14293db446aSBoris Brezillon {
14393db446aSBoris Brezillon 	int i;
14493db446aSBoris Brezillon 
14593db446aSBoris Brezillon 	for (i = 0; i < len; i++)
146c0739d85SBoris Brezillon 		xway_writeb(nand_to_mtd(chip), NAND_WRITE_DATA, buf[i]);
14793db446aSBoris Brezillon }
14893db446aSBoris Brezillon 
149d525914bSMiquel Raynal static int xway_attach_chip(struct nand_chip *chip)
150d525914bSMiquel Raynal {
151d525914bSMiquel Raynal 	chip->ecc.engine_type = NAND_ECC_ENGINE_TYPE_SOFT;
15233d974e7SMiquel Raynal 
15333d974e7SMiquel Raynal 	if (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  */
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 
180*abac6563SCai 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 
22093db446aSBoris Brezillon 	/* Scan to find existence of the device */
22100ad378fSBoris Brezillon 	err = nand_scan(&data->chip, 1);
22293db446aSBoris Brezillon 	if (err)
22393db446aSBoris Brezillon 		return err;
22493db446aSBoris Brezillon 
22593db446aSBoris Brezillon 	err = mtd_device_register(mtd, NULL, 0);
22693db446aSBoris Brezillon 	if (err)
22734531be5SMiquel Raynal 		nand_cleanup(&data->chip);
22893db446aSBoris Brezillon 
22993db446aSBoris Brezillon 	return err;
23093db446aSBoris Brezillon }
23193db446aSBoris Brezillon 
23293db446aSBoris Brezillon /*
23393db446aSBoris Brezillon  * Remove a NAND device.
23493db446aSBoris Brezillon  */
23593db446aSBoris Brezillon static int xway_nand_remove(struct platform_device *pdev)
23693db446aSBoris Brezillon {
23793db446aSBoris Brezillon 	struct xway_nand_data *data = platform_get_drvdata(pdev);
2389fdd78f7SMiquel Raynal 	struct nand_chip *chip = &data->chip;
2399fdd78f7SMiquel Raynal 	int ret;
24093db446aSBoris Brezillon 
241880bc529SMiquel Raynal 	ret = mtd_device_unregister(nand_to_mtd(chip));
2429fdd78f7SMiquel Raynal 	WARN_ON(ret);
2439fdd78f7SMiquel Raynal 	nand_cleanup(chip);
24493db446aSBoris Brezillon 
24593db446aSBoris Brezillon 	return 0;
24693db446aSBoris Brezillon }
24793db446aSBoris Brezillon 
24893db446aSBoris Brezillon static const struct of_device_id xway_nand_match[] = {
24993db446aSBoris Brezillon 	{ .compatible = "lantiq,nand-xway" },
25093db446aSBoris Brezillon 	{},
25193db446aSBoris Brezillon };
25293db446aSBoris Brezillon 
25393db446aSBoris Brezillon static struct platform_driver xway_nand_driver = {
25493db446aSBoris Brezillon 	.probe	= xway_nand_probe,
25593db446aSBoris Brezillon 	.remove	= xway_nand_remove,
25693db446aSBoris Brezillon 	.driver	= {
25793db446aSBoris Brezillon 		.name		= "lantiq,nand-xway",
25893db446aSBoris Brezillon 		.of_match_table = xway_nand_match,
25993db446aSBoris Brezillon 	},
26093db446aSBoris Brezillon };
26193db446aSBoris Brezillon 
26293db446aSBoris Brezillon builtin_platform_driver(xway_nand_driver);
263