18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Copyright © 2012 John Crispin <john@phrozen.org> 58c2ecf20Sopenharmony_ci * Copyright © 2016 Hauke Mehrtens <hauke@hauke-m.de> 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/mtd/rawnand.h> 98c2ecf20Sopenharmony_ci#include <linux/of_gpio.h> 108c2ecf20Sopenharmony_ci#include <linux/of_platform.h> 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#include <lantiq_soc.h> 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci/* nand registers */ 158c2ecf20Sopenharmony_ci#define EBU_ADDSEL1 0x24 168c2ecf20Sopenharmony_ci#define EBU_NAND_CON 0xB0 178c2ecf20Sopenharmony_ci#define EBU_NAND_WAIT 0xB4 188c2ecf20Sopenharmony_ci#define NAND_WAIT_RD BIT(0) /* NAND flash status output */ 198c2ecf20Sopenharmony_ci#define NAND_WAIT_WR_C BIT(3) /* NAND Write/Read complete */ 208c2ecf20Sopenharmony_ci#define EBU_NAND_ECC0 0xB8 218c2ecf20Sopenharmony_ci#define EBU_NAND_ECC_AC 0xBC 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci/* 248c2ecf20Sopenharmony_ci * nand commands 258c2ecf20Sopenharmony_ci * The pins of the NAND chip are selected based on the address bits of the 268c2ecf20Sopenharmony_ci * "register" read and write. There are no special registers, but an 278c2ecf20Sopenharmony_ci * address range and the lower address bits are used to activate the 288c2ecf20Sopenharmony_ci * correct line. For example when the bit (1 << 2) is set in the address 298c2ecf20Sopenharmony_ci * the ALE pin will be activated. 308c2ecf20Sopenharmony_ci */ 318c2ecf20Sopenharmony_ci#define NAND_CMD_ALE BIT(2) /* address latch enable */ 328c2ecf20Sopenharmony_ci#define NAND_CMD_CLE BIT(3) /* command latch enable */ 338c2ecf20Sopenharmony_ci#define NAND_CMD_CS BIT(4) /* chip select */ 348c2ecf20Sopenharmony_ci#define NAND_CMD_SE BIT(5) /* spare area access latch */ 358c2ecf20Sopenharmony_ci#define NAND_CMD_WP BIT(6) /* write protect */ 368c2ecf20Sopenharmony_ci#define NAND_WRITE_CMD (NAND_CMD_CS | NAND_CMD_CLE) 378c2ecf20Sopenharmony_ci#define NAND_WRITE_ADDR (NAND_CMD_CS | NAND_CMD_ALE) 388c2ecf20Sopenharmony_ci#define NAND_WRITE_DATA (NAND_CMD_CS) 398c2ecf20Sopenharmony_ci#define NAND_READ_DATA (NAND_CMD_CS) 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci/* we need to tel the ebu which addr we mapped the nand to */ 428c2ecf20Sopenharmony_ci#define ADDSEL1_MASK(x) (x << 4) 438c2ecf20Sopenharmony_ci#define ADDSEL1_REGEN 1 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci/* we need to tell the EBU that we have nand attached and set it up properly */ 468c2ecf20Sopenharmony_ci#define BUSCON1_SETUP (1 << 22) 478c2ecf20Sopenharmony_ci#define BUSCON1_BCGEN_RES (0x3 << 12) 488c2ecf20Sopenharmony_ci#define BUSCON1_WAITWRC2 (2 << 8) 498c2ecf20Sopenharmony_ci#define BUSCON1_WAITRDC2 (2 << 6) 508c2ecf20Sopenharmony_ci#define BUSCON1_HOLDC1 (1 << 4) 518c2ecf20Sopenharmony_ci#define BUSCON1_RECOVC1 (1 << 2) 528c2ecf20Sopenharmony_ci#define BUSCON1_CMULT4 1 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci#define NAND_CON_CE (1 << 20) 558c2ecf20Sopenharmony_ci#define NAND_CON_OUT_CS1 (1 << 10) 568c2ecf20Sopenharmony_ci#define NAND_CON_IN_CS1 (1 << 8) 578c2ecf20Sopenharmony_ci#define NAND_CON_PRE_P (1 << 7) 588c2ecf20Sopenharmony_ci#define NAND_CON_WP_P (1 << 6) 598c2ecf20Sopenharmony_ci#define NAND_CON_SE_P (1 << 5) 608c2ecf20Sopenharmony_ci#define NAND_CON_CS_P (1 << 4) 618c2ecf20Sopenharmony_ci#define NAND_CON_CSMUX (1 << 1) 628c2ecf20Sopenharmony_ci#define NAND_CON_NANDM 1 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_cistruct xway_nand_data { 658c2ecf20Sopenharmony_ci struct nand_controller controller; 668c2ecf20Sopenharmony_ci struct nand_chip chip; 678c2ecf20Sopenharmony_ci unsigned long csflags; 688c2ecf20Sopenharmony_ci void __iomem *nandaddr; 698c2ecf20Sopenharmony_ci}; 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_cistatic u8 xway_readb(struct mtd_info *mtd, int op) 728c2ecf20Sopenharmony_ci{ 738c2ecf20Sopenharmony_ci struct nand_chip *chip = mtd_to_nand(mtd); 748c2ecf20Sopenharmony_ci struct xway_nand_data *data = nand_get_controller_data(chip); 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci return readb(data->nandaddr + op); 778c2ecf20Sopenharmony_ci} 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_cistatic void xway_writeb(struct mtd_info *mtd, int op, u8 value) 808c2ecf20Sopenharmony_ci{ 818c2ecf20Sopenharmony_ci struct nand_chip *chip = mtd_to_nand(mtd); 828c2ecf20Sopenharmony_ci struct xway_nand_data *data = nand_get_controller_data(chip); 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci writeb(value, data->nandaddr + op); 858c2ecf20Sopenharmony_ci} 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_cistatic void xway_select_chip(struct nand_chip *chip, int select) 888c2ecf20Sopenharmony_ci{ 898c2ecf20Sopenharmony_ci struct xway_nand_data *data = nand_get_controller_data(chip); 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci switch (select) { 928c2ecf20Sopenharmony_ci case -1: 938c2ecf20Sopenharmony_ci ltq_ebu_w32_mask(NAND_CON_CE, 0, EBU_NAND_CON); 948c2ecf20Sopenharmony_ci ltq_ebu_w32_mask(NAND_CON_NANDM, 0, EBU_NAND_CON); 958c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&ebu_lock, data->csflags); 968c2ecf20Sopenharmony_ci break; 978c2ecf20Sopenharmony_ci case 0: 988c2ecf20Sopenharmony_ci spin_lock_irqsave(&ebu_lock, data->csflags); 998c2ecf20Sopenharmony_ci ltq_ebu_w32_mask(0, NAND_CON_NANDM, EBU_NAND_CON); 1008c2ecf20Sopenharmony_ci ltq_ebu_w32_mask(0, NAND_CON_CE, EBU_NAND_CON); 1018c2ecf20Sopenharmony_ci break; 1028c2ecf20Sopenharmony_ci default: 1038c2ecf20Sopenharmony_ci BUG(); 1048c2ecf20Sopenharmony_ci } 1058c2ecf20Sopenharmony_ci} 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_cistatic void xway_cmd_ctrl(struct nand_chip *chip, int cmd, unsigned int ctrl) 1088c2ecf20Sopenharmony_ci{ 1098c2ecf20Sopenharmony_ci struct mtd_info *mtd = nand_to_mtd(chip); 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci if (cmd == NAND_CMD_NONE) 1128c2ecf20Sopenharmony_ci return; 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci if (ctrl & NAND_CLE) 1158c2ecf20Sopenharmony_ci xway_writeb(mtd, NAND_WRITE_CMD, cmd); 1168c2ecf20Sopenharmony_ci else if (ctrl & NAND_ALE) 1178c2ecf20Sopenharmony_ci xway_writeb(mtd, NAND_WRITE_ADDR, cmd); 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci while ((ltq_ebu_r32(EBU_NAND_WAIT) & NAND_WAIT_WR_C) == 0) 1208c2ecf20Sopenharmony_ci ; 1218c2ecf20Sopenharmony_ci} 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_cistatic int xway_dev_ready(struct nand_chip *chip) 1248c2ecf20Sopenharmony_ci{ 1258c2ecf20Sopenharmony_ci return ltq_ebu_r32(EBU_NAND_WAIT) & NAND_WAIT_RD; 1268c2ecf20Sopenharmony_ci} 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_cistatic unsigned char xway_read_byte(struct nand_chip *chip) 1298c2ecf20Sopenharmony_ci{ 1308c2ecf20Sopenharmony_ci return xway_readb(nand_to_mtd(chip), NAND_READ_DATA); 1318c2ecf20Sopenharmony_ci} 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_cistatic void xway_read_buf(struct nand_chip *chip, u_char *buf, int len) 1348c2ecf20Sopenharmony_ci{ 1358c2ecf20Sopenharmony_ci int i; 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci for (i = 0; i < len; i++) 1388c2ecf20Sopenharmony_ci buf[i] = xway_readb(nand_to_mtd(chip), NAND_WRITE_DATA); 1398c2ecf20Sopenharmony_ci} 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_cistatic void xway_write_buf(struct nand_chip *chip, const u_char *buf, int len) 1428c2ecf20Sopenharmony_ci{ 1438c2ecf20Sopenharmony_ci int i; 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci for (i = 0; i < len; i++) 1468c2ecf20Sopenharmony_ci xway_writeb(nand_to_mtd(chip), NAND_WRITE_DATA, buf[i]); 1478c2ecf20Sopenharmony_ci} 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_cistatic int xway_attach_chip(struct nand_chip *chip) 1508c2ecf20Sopenharmony_ci{ 1518c2ecf20Sopenharmony_ci if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_SOFT && 1528c2ecf20Sopenharmony_ci chip->ecc.algo == NAND_ECC_ALGO_UNKNOWN) 1538c2ecf20Sopenharmony_ci chip->ecc.algo = NAND_ECC_ALGO_HAMMING; 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci return 0; 1568c2ecf20Sopenharmony_ci} 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_cistatic const struct nand_controller_ops xway_nand_ops = { 1598c2ecf20Sopenharmony_ci .attach_chip = xway_attach_chip, 1608c2ecf20Sopenharmony_ci}; 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci/* 1638c2ecf20Sopenharmony_ci * Probe for the NAND device. 1648c2ecf20Sopenharmony_ci */ 1658c2ecf20Sopenharmony_cistatic int xway_nand_probe(struct platform_device *pdev) 1668c2ecf20Sopenharmony_ci{ 1678c2ecf20Sopenharmony_ci struct xway_nand_data *data; 1688c2ecf20Sopenharmony_ci struct mtd_info *mtd; 1698c2ecf20Sopenharmony_ci struct resource *res; 1708c2ecf20Sopenharmony_ci int err; 1718c2ecf20Sopenharmony_ci u32 cs; 1728c2ecf20Sopenharmony_ci u32 cs_flag = 0; 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci /* Allocate memory for the device structure (and zero it) */ 1758c2ecf20Sopenharmony_ci data = devm_kzalloc(&pdev->dev, sizeof(struct xway_nand_data), 1768c2ecf20Sopenharmony_ci GFP_KERNEL); 1778c2ecf20Sopenharmony_ci if (!data) 1788c2ecf20Sopenharmony_ci return -ENOMEM; 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1818c2ecf20Sopenharmony_ci data->nandaddr = devm_ioremap_resource(&pdev->dev, res); 1828c2ecf20Sopenharmony_ci if (IS_ERR(data->nandaddr)) 1838c2ecf20Sopenharmony_ci return PTR_ERR(data->nandaddr); 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci nand_set_flash_node(&data->chip, pdev->dev.of_node); 1868c2ecf20Sopenharmony_ci mtd = nand_to_mtd(&data->chip); 1878c2ecf20Sopenharmony_ci mtd->dev.parent = &pdev->dev; 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci data->chip.legacy.cmd_ctrl = xway_cmd_ctrl; 1908c2ecf20Sopenharmony_ci data->chip.legacy.dev_ready = xway_dev_ready; 1918c2ecf20Sopenharmony_ci data->chip.legacy.select_chip = xway_select_chip; 1928c2ecf20Sopenharmony_ci data->chip.legacy.write_buf = xway_write_buf; 1938c2ecf20Sopenharmony_ci data->chip.legacy.read_buf = xway_read_buf; 1948c2ecf20Sopenharmony_ci data->chip.legacy.read_byte = xway_read_byte; 1958c2ecf20Sopenharmony_ci data->chip.legacy.chip_delay = 30; 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci nand_controller_init(&data->controller); 1988c2ecf20Sopenharmony_ci data->controller.ops = &xway_nand_ops; 1998c2ecf20Sopenharmony_ci data->chip.controller = &data->controller; 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, data); 2028c2ecf20Sopenharmony_ci nand_set_controller_data(&data->chip, data); 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci /* load our CS from the DT. Either we find a valid 1 or default to 0 */ 2058c2ecf20Sopenharmony_ci err = of_property_read_u32(pdev->dev.of_node, "lantiq,cs", &cs); 2068c2ecf20Sopenharmony_ci if (!err && cs == 1) 2078c2ecf20Sopenharmony_ci cs_flag = NAND_CON_IN_CS1 | NAND_CON_OUT_CS1; 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_ci /* setup the EBU to run in NAND mode on our base addr */ 2108c2ecf20Sopenharmony_ci ltq_ebu_w32(CPHYSADDR(data->nandaddr) 2118c2ecf20Sopenharmony_ci | ADDSEL1_MASK(3) | ADDSEL1_REGEN, EBU_ADDSEL1); 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ci ltq_ebu_w32(BUSCON1_SETUP | BUSCON1_BCGEN_RES | BUSCON1_WAITWRC2 2148c2ecf20Sopenharmony_ci | BUSCON1_WAITRDC2 | BUSCON1_HOLDC1 | BUSCON1_RECOVC1 2158c2ecf20Sopenharmony_ci | BUSCON1_CMULT4, LTQ_EBU_BUSCON1); 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ci ltq_ebu_w32(NAND_CON_NANDM | NAND_CON_CSMUX | NAND_CON_CS_P 2188c2ecf20Sopenharmony_ci | NAND_CON_SE_P | NAND_CON_WP_P | NAND_CON_PRE_P 2198c2ecf20Sopenharmony_ci | cs_flag, EBU_NAND_CON); 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci /* 2228c2ecf20Sopenharmony_ci * This driver assumes that the default ECC engine should be TYPE_SOFT. 2238c2ecf20Sopenharmony_ci * Set ->engine_type before registering the NAND devices in order to 2248c2ecf20Sopenharmony_ci * provide a driver specific default value. 2258c2ecf20Sopenharmony_ci */ 2268c2ecf20Sopenharmony_ci data->chip.ecc.engine_type = NAND_ECC_ENGINE_TYPE_SOFT; 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_ci /* Scan to find existence of the device */ 2298c2ecf20Sopenharmony_ci err = nand_scan(&data->chip, 1); 2308c2ecf20Sopenharmony_ci if (err) 2318c2ecf20Sopenharmony_ci return err; 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_ci err = mtd_device_register(mtd, NULL, 0); 2348c2ecf20Sopenharmony_ci if (err) 2358c2ecf20Sopenharmony_ci nand_cleanup(&data->chip); 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_ci return err; 2388c2ecf20Sopenharmony_ci} 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_ci/* 2418c2ecf20Sopenharmony_ci * Remove a NAND device. 2428c2ecf20Sopenharmony_ci */ 2438c2ecf20Sopenharmony_cistatic int xway_nand_remove(struct platform_device *pdev) 2448c2ecf20Sopenharmony_ci{ 2458c2ecf20Sopenharmony_ci struct xway_nand_data *data = platform_get_drvdata(pdev); 2468c2ecf20Sopenharmony_ci struct nand_chip *chip = &data->chip; 2478c2ecf20Sopenharmony_ci int ret; 2488c2ecf20Sopenharmony_ci 2498c2ecf20Sopenharmony_ci ret = mtd_device_unregister(nand_to_mtd(chip)); 2508c2ecf20Sopenharmony_ci WARN_ON(ret); 2518c2ecf20Sopenharmony_ci nand_cleanup(chip); 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_ci return 0; 2548c2ecf20Sopenharmony_ci} 2558c2ecf20Sopenharmony_ci 2568c2ecf20Sopenharmony_cistatic const struct of_device_id xway_nand_match[] = { 2578c2ecf20Sopenharmony_ci { .compatible = "lantiq,nand-xway" }, 2588c2ecf20Sopenharmony_ci {}, 2598c2ecf20Sopenharmony_ci}; 2608c2ecf20Sopenharmony_ci 2618c2ecf20Sopenharmony_cistatic struct platform_driver xway_nand_driver = { 2628c2ecf20Sopenharmony_ci .probe = xway_nand_probe, 2638c2ecf20Sopenharmony_ci .remove = xway_nand_remove, 2648c2ecf20Sopenharmony_ci .driver = { 2658c2ecf20Sopenharmony_ci .name = "lantiq,nand-xway", 2668c2ecf20Sopenharmony_ci .of_match_table = xway_nand_match, 2678c2ecf20Sopenharmony_ci }, 2688c2ecf20Sopenharmony_ci}; 2698c2ecf20Sopenharmony_ci 2708c2ecf20Sopenharmony_cibuiltin_platform_driver(xway_nand_driver); 271