xref: /openbmc/linux/drivers/mtd/nand/raw/xway_nand.c (revision 3cece3abebda068e55e19302a6f0fa60cf553737)
193db446aSBoris Brezillon /*
293db446aSBoris Brezillon  *  This program is free software; you can redistribute it and/or modify it
393db446aSBoris Brezillon  *  under the terms of the GNU General Public License version 2 as published
493db446aSBoris Brezillon  *  by the Free Software Foundation.
593db446aSBoris Brezillon  *
693db446aSBoris Brezillon  *  Copyright © 2012 John Crispin <john@phrozen.org>
793db446aSBoris Brezillon  *  Copyright © 2016 Hauke Mehrtens <hauke@hauke-m.de>
893db446aSBoris Brezillon  */
993db446aSBoris Brezillon 
1093db446aSBoris Brezillon #include <linux/mtd/rawnand.h>
1193db446aSBoris Brezillon #include <linux/of_gpio.h>
1293db446aSBoris Brezillon #include <linux/of_platform.h>
1393db446aSBoris Brezillon 
1493db446aSBoris Brezillon #include <lantiq_soc.h>
1593db446aSBoris Brezillon 
1693db446aSBoris Brezillon /* nand registers */
1793db446aSBoris Brezillon #define EBU_ADDSEL1		0x24
1893db446aSBoris Brezillon #define EBU_NAND_CON		0xB0
1993db446aSBoris Brezillon #define EBU_NAND_WAIT		0xB4
2093db446aSBoris Brezillon #define  NAND_WAIT_RD		BIT(0) /* NAND flash status output */
2193db446aSBoris Brezillon #define  NAND_WAIT_WR_C		BIT(3) /* NAND Write/Read complete */
2293db446aSBoris Brezillon #define EBU_NAND_ECC0		0xB8
2393db446aSBoris Brezillon #define EBU_NAND_ECC_AC		0xBC
2493db446aSBoris Brezillon 
2593db446aSBoris Brezillon /*
2693db446aSBoris Brezillon  * nand commands
2793db446aSBoris Brezillon  * The pins of the NAND chip are selected based on the address bits of the
2893db446aSBoris Brezillon  * "register" read and write. There are no special registers, but an
2993db446aSBoris Brezillon  * address range and the lower address bits are used to activate the
3093db446aSBoris Brezillon  * correct line. For example when the bit (1 << 2) is set in the address
3193db446aSBoris Brezillon  * the ALE pin will be activated.
3293db446aSBoris Brezillon  */
3393db446aSBoris Brezillon #define NAND_CMD_ALE		BIT(2) /* address latch enable */
3493db446aSBoris Brezillon #define NAND_CMD_CLE		BIT(3) /* command latch enable */
3593db446aSBoris Brezillon #define NAND_CMD_CS		BIT(4) /* chip select */
3693db446aSBoris Brezillon #define NAND_CMD_SE		BIT(5) /* spare area access latch */
3793db446aSBoris Brezillon #define NAND_CMD_WP		BIT(6) /* write protect */
3893db446aSBoris Brezillon #define NAND_WRITE_CMD		(NAND_CMD_CS | NAND_CMD_CLE)
3993db446aSBoris Brezillon #define NAND_WRITE_ADDR		(NAND_CMD_CS | NAND_CMD_ALE)
4093db446aSBoris Brezillon #define NAND_WRITE_DATA		(NAND_CMD_CS)
4193db446aSBoris Brezillon #define NAND_READ_DATA		(NAND_CMD_CS)
4293db446aSBoris Brezillon 
4393db446aSBoris Brezillon /* we need to tel the ebu which addr we mapped the nand to */
4493db446aSBoris Brezillon #define ADDSEL1_MASK(x)		(x << 4)
4593db446aSBoris Brezillon #define ADDSEL1_REGEN		1
4693db446aSBoris Brezillon 
4793db446aSBoris Brezillon /* we need to tell the EBU that we have nand attached and set it up properly */
4893db446aSBoris Brezillon #define BUSCON1_SETUP		(1 << 22)
4993db446aSBoris Brezillon #define BUSCON1_BCGEN_RES	(0x3 << 12)
5093db446aSBoris Brezillon #define BUSCON1_WAITWRC2	(2 << 8)
5193db446aSBoris Brezillon #define BUSCON1_WAITRDC2	(2 << 6)
5293db446aSBoris Brezillon #define BUSCON1_HOLDC1		(1 << 4)
5393db446aSBoris Brezillon #define BUSCON1_RECOVC1		(1 << 2)
5493db446aSBoris Brezillon #define BUSCON1_CMULT4		1
5593db446aSBoris Brezillon 
5693db446aSBoris Brezillon #define NAND_CON_CE		(1 << 20)
5793db446aSBoris Brezillon #define NAND_CON_OUT_CS1	(1 << 10)
5893db446aSBoris Brezillon #define NAND_CON_IN_CS1		(1 << 8)
5993db446aSBoris Brezillon #define NAND_CON_PRE_P		(1 << 7)
6093db446aSBoris Brezillon #define NAND_CON_WP_P		(1 << 6)
6193db446aSBoris Brezillon #define NAND_CON_SE_P		(1 << 5)
6293db446aSBoris Brezillon #define NAND_CON_CS_P		(1 << 4)
6393db446aSBoris Brezillon #define NAND_CON_CSMUX		(1 << 1)
6493db446aSBoris Brezillon #define NAND_CON_NANDM		1
6593db446aSBoris Brezillon 
6693db446aSBoris Brezillon struct xway_nand_data {
6793db446aSBoris Brezillon 	struct nand_chip	chip;
6893db446aSBoris Brezillon 	unsigned long		csflags;
6993db446aSBoris Brezillon 	void __iomem		*nandaddr;
7093db446aSBoris Brezillon };
7193db446aSBoris Brezillon 
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 
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 
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 
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 
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 
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 
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 
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 
15093db446aSBoris Brezillon /*
15193db446aSBoris Brezillon  * Probe for the NAND device.
15293db446aSBoris Brezillon  */
15393db446aSBoris Brezillon static int xway_nand_probe(struct platform_device *pdev)
15493db446aSBoris Brezillon {
15593db446aSBoris Brezillon 	struct xway_nand_data *data;
15693db446aSBoris Brezillon 	struct mtd_info *mtd;
15793db446aSBoris Brezillon 	struct resource *res;
15893db446aSBoris Brezillon 	int err;
15993db446aSBoris Brezillon 	u32 cs;
16093db446aSBoris Brezillon 	u32 cs_flag = 0;
16193db446aSBoris Brezillon 
16293db446aSBoris Brezillon 	/* Allocate memory for the device structure (and zero it) */
16393db446aSBoris Brezillon 	data = devm_kzalloc(&pdev->dev, sizeof(struct xway_nand_data),
16493db446aSBoris Brezillon 			    GFP_KERNEL);
16593db446aSBoris Brezillon 	if (!data)
16693db446aSBoris Brezillon 		return -ENOMEM;
16793db446aSBoris Brezillon 
16893db446aSBoris Brezillon 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
16993db446aSBoris Brezillon 	data->nandaddr = devm_ioremap_resource(&pdev->dev, res);
17093db446aSBoris Brezillon 	if (IS_ERR(data->nandaddr))
17193db446aSBoris Brezillon 		return PTR_ERR(data->nandaddr);
17293db446aSBoris Brezillon 
17393db446aSBoris Brezillon 	nand_set_flash_node(&data->chip, pdev->dev.of_node);
17493db446aSBoris Brezillon 	mtd = nand_to_mtd(&data->chip);
17593db446aSBoris Brezillon 	mtd->dev.parent = &pdev->dev;
17693db446aSBoris Brezillon 
177bf6065c6SBoris Brezillon 	data->chip.legacy.cmd_ctrl = xway_cmd_ctrl;
1788395b753SBoris Brezillon 	data->chip.legacy.dev_ready = xway_dev_ready;
17993db446aSBoris Brezillon 	data->chip.select_chip = xway_select_chip;
180716bbbabSBoris Brezillon 	data->chip.legacy.write_buf = xway_write_buf;
181716bbbabSBoris Brezillon 	data->chip.legacy.read_buf = xway_read_buf;
182716bbbabSBoris Brezillon 	data->chip.legacy.read_byte = xway_read_byte;
183*3cece3abSBoris Brezillon 	data->chip.legacy.chip_delay = 30;
18493db446aSBoris Brezillon 
18593db446aSBoris Brezillon 	data->chip.ecc.mode = NAND_ECC_SOFT;
18693db446aSBoris Brezillon 	data->chip.ecc.algo = NAND_ECC_HAMMING;
18793db446aSBoris Brezillon 
18893db446aSBoris Brezillon 	platform_set_drvdata(pdev, data);
18993db446aSBoris Brezillon 	nand_set_controller_data(&data->chip, data);
19093db446aSBoris Brezillon 
19193db446aSBoris Brezillon 	/* load our CS from the DT. Either we find a valid 1 or default to 0 */
19293db446aSBoris Brezillon 	err = of_property_read_u32(pdev->dev.of_node, "lantiq,cs", &cs);
19393db446aSBoris Brezillon 	if (!err && cs == 1)
19493db446aSBoris Brezillon 		cs_flag = NAND_CON_IN_CS1 | NAND_CON_OUT_CS1;
19593db446aSBoris Brezillon 
19693db446aSBoris Brezillon 	/* setup the EBU to run in NAND mode on our base addr */
19793db446aSBoris Brezillon 	ltq_ebu_w32(CPHYSADDR(data->nandaddr)
19893db446aSBoris Brezillon 		    | ADDSEL1_MASK(3) | ADDSEL1_REGEN, EBU_ADDSEL1);
19993db446aSBoris Brezillon 
20093db446aSBoris Brezillon 	ltq_ebu_w32(BUSCON1_SETUP | BUSCON1_BCGEN_RES | BUSCON1_WAITWRC2
20193db446aSBoris Brezillon 		    | BUSCON1_WAITRDC2 | BUSCON1_HOLDC1 | BUSCON1_RECOVC1
20293db446aSBoris Brezillon 		    | BUSCON1_CMULT4, LTQ_EBU_BUSCON1);
20393db446aSBoris Brezillon 
20493db446aSBoris Brezillon 	ltq_ebu_w32(NAND_CON_NANDM | NAND_CON_CSMUX | NAND_CON_CS_P
20593db446aSBoris Brezillon 		    | NAND_CON_SE_P | NAND_CON_WP_P | NAND_CON_PRE_P
20693db446aSBoris Brezillon 		    | cs_flag, EBU_NAND_CON);
20793db446aSBoris Brezillon 
20893db446aSBoris Brezillon 	/* Scan to find existence of the device */
20900ad378fSBoris Brezillon 	err = nand_scan(&data->chip, 1);
21093db446aSBoris Brezillon 	if (err)
21193db446aSBoris Brezillon 		return err;
21293db446aSBoris Brezillon 
21393db446aSBoris Brezillon 	err = mtd_device_register(mtd, NULL, 0);
21493db446aSBoris Brezillon 	if (err)
21559ac276fSBoris Brezillon 		nand_release(&data->chip);
21693db446aSBoris Brezillon 
21793db446aSBoris Brezillon 	return err;
21893db446aSBoris Brezillon }
21993db446aSBoris Brezillon 
22093db446aSBoris Brezillon /*
22193db446aSBoris Brezillon  * Remove a NAND device.
22293db446aSBoris Brezillon  */
22393db446aSBoris Brezillon static int xway_nand_remove(struct platform_device *pdev)
22493db446aSBoris Brezillon {
22593db446aSBoris Brezillon 	struct xway_nand_data *data = platform_get_drvdata(pdev);
22693db446aSBoris Brezillon 
22759ac276fSBoris Brezillon 	nand_release(&data->chip);
22893db446aSBoris Brezillon 
22993db446aSBoris Brezillon 	return 0;
23093db446aSBoris Brezillon }
23193db446aSBoris Brezillon 
23293db446aSBoris Brezillon static const struct of_device_id xway_nand_match[] = {
23393db446aSBoris Brezillon 	{ .compatible = "lantiq,nand-xway" },
23493db446aSBoris Brezillon 	{},
23593db446aSBoris Brezillon };
23693db446aSBoris Brezillon 
23793db446aSBoris Brezillon static struct platform_driver xway_nand_driver = {
23893db446aSBoris Brezillon 	.probe	= xway_nand_probe,
23993db446aSBoris Brezillon 	.remove	= xway_nand_remove,
24093db446aSBoris Brezillon 	.driver	= {
24193db446aSBoris Brezillon 		.name		= "lantiq,nand-xway",
24293db446aSBoris Brezillon 		.of_match_table = xway_nand_match,
24393db446aSBoris Brezillon 	},
24493db446aSBoris Brezillon };
24593db446aSBoris Brezillon 
24693db446aSBoris Brezillon builtin_platform_driver(xway_nand_driver);
247