diff options
author | Christopher Hill <ch6574@gmail.com> | 2020-05-25 21:05:00 -0400 |
---|---|---|
committer | Adrian Schmutzler <freifunk@adrianschmutzler.de> | 2020-06-15 21:16:18 +0200 |
commit | fa70b3a4bbc65d372d735004d9df894bd1c89c81 (patch) | |
tree | a56a9e308de8c1a657cbd4df1d5cd834b167203a /target/linux/ath79/files/drivers/mtd | |
parent | 344e75eb2b5653ed350121b4a7beb292049c24c0 (diff) | |
download | upstream-fa70b3a4bbc65d372d735004d9df894bd1c89c81.tar.gz upstream-fa70b3a4bbc65d372d735004d9df894bd1c89c81.tar.bz2 upstream-fa70b3a4bbc65d372d735004d9df894bd1c89c81.zip |
ath79: add Mikrotik rb4xx series drivers
This adds 3 Mikrotik rb4xx series drivers as follows:
rb4xx-cpld: This is in the mfd subsystem, and is the parent CPLD device
that interfaces between the SoC SPI bus and its two children below.
rb4xx-gpio: This is the GPIO expander.
rb4xx-nand: This is the NAND driver.
The history of this code comes in three phases.
1. The first is a May 2015 attempt to push the equivalient ar71xx rb4xx
drivers upstream. See https://lore.kernel.org/patchwork/patch/940880/.
Module-author: Gabor Juhos <juhosg@openwrt.org>
Module-author: Imre Kaloz <kaloz@openwrt.org>
Module-author: Bert Vermeulen <bert@biot.com>
2. Next several ar71xx patches were applied bringing the code current.
commit 7bbf4117c6fe4b764d9d7c62fb2bcf6dd93bff2c
Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
commit af79fdbe4af32a287798b579141204bda056b8aa
commit 889272d92db689fd9c910243635e44c9d8323095
commit e21cb649a235180563363b8af5ba8296b9ac0baa
commit 7c09fa4a7492ca436f2c94bd9a465b7c5bbeed6f
Signed-off-by: Felix Fietkau <nbd@nbd.name>
3. Finally a heavy refactor to split the driver into the three new
subsystems, and updated to work with the device tree configuration, plus
updates and review feedback incorporated
Reviewed-by: Thibaut VARĂˆNE <hacks@slashdirt.org>
Signed-off-by: Christopher Hill <ch6574@gmail.com>
Diffstat (limited to 'target/linux/ath79/files/drivers/mtd')
-rw-r--r-- | target/linux/ath79/files/drivers/mtd/nand/raw/nand_rb4xx.c | 297 |
1 files changed, 297 insertions, 0 deletions
diff --git a/target/linux/ath79/files/drivers/mtd/nand/raw/nand_rb4xx.c b/target/linux/ath79/files/drivers/mtd/nand/raw/nand_rb4xx.c new file mode 100644 index 0000000000..922d7095cb --- /dev/null +++ b/target/linux/ath79/files/drivers/mtd/nand/raw/nand_rb4xx.c @@ -0,0 +1,297 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * NAND driver for the MikroTik RouterBoard 4xx series + * + * Copyright (C) 2008-2011 Gabor Juhos <juhosg@openwrt.org> + * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org> + * Copyright (C) 2015 Bert Vermeulen <bert@biot.com> + * Copyright (C) 2020 Christopher Hill <ch6574@gmail.com> + * + * This file was based on the driver for Linux 2.6.22 published by + * MikroTik for their RouterBoard 4xx series devices. + * + * N.B. driver probe reports "DMA mask not set" warnings which are + * an artifact of using a platform_driver as an MFD device child. + * See conversation here https://lkml.org/lkml/2020/4/28/675 + */ +#include <linux/platform_device.h> +#include <linux/mtd/rawnand.h> +#include <linux/gpio/consumer.h> +#include <linux/module.h> +#include <linux/of_device.h> +#include <linux/version.h> + +#include <mfd/rb4xx-cpld.h> + +struct rb4xx_nand { + struct rb4xx_cpld *cpld; + struct device *dev; + + struct nand_chip chip; + struct gpio_desc *ale; + struct gpio_desc *cle; + struct gpio_desc *nce; + struct gpio_desc *rdy; +}; + +static int rb4xx_ooblayout_ecc(struct mtd_info *mtd, int section, + struct mtd_oob_region *oobregion) +{ + switch (section) { + case 0: + oobregion->offset = 8; + oobregion->length = 3; + return 0; + case 1: + oobregion->offset = 13; + oobregion->length = 3; + return 0; + default: + return -ERANGE; + } +} + +static int rb4xx_ooblayout_free(struct mtd_info *mtd, int section, + struct mtd_oob_region *oobregion) +{ + switch (section) { + case 0: + oobregion->offset = 0; + oobregion->length = 4; + return 0; + case 1: + oobregion->offset = 4; + oobregion->length = 1; + return 0; + case 2: + oobregion->offset = 6; + oobregion->length = 2; + return 0; + case 3: + oobregion->offset = 11; + oobregion->length = 2; + return 0; + default: + return -ERANGE; + } +} + +static const struct mtd_ooblayout_ops rb4xx_nand_ecclayout_ops = { + .ecc = rb4xx_ooblayout_ecc, + .free = rb4xx_ooblayout_free, +}; + +#if LINUX_VERSION_CODE < KERNEL_VERSION(4,20,0) +static uint8_t rb4xx_nand_read_byte(struct mtd_info *mtd) +{ + struct rb4xx_nand *nand = mtd->priv; +#else +static u8 rb4xx_nand_read_byte(struct nand_chip *chip) +{ + struct rb4xx_nand *nand = chip->priv; +#endif + struct rb4xx_cpld *cpld = nand->cpld; + u8 data; + int ret; + + ret = cpld->read_nand(cpld, &data, 1); + if (unlikely(ret)) + return 0xff; + + return data; +} + +#if LINUX_VERSION_CODE < KERNEL_VERSION(4,20,0) +static void rb4xx_nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, + int len) +{ + struct rb4xx_nand *nand = mtd->priv; +#else +static void rb4xx_nand_write_buf(struct nand_chip *chip, const u8 *buf, int len) +{ + struct rb4xx_nand *nand = chip->priv; +#endif + struct rb4xx_cpld *cpld = nand->cpld; + + cpld->write_nand(cpld, buf, len); +} + +#if LINUX_VERSION_CODE < KERNEL_VERSION(4,20,0) +static void rb4xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) +{ + struct rb4xx_nand *nand = mtd->priv; +#else +static void rb4xx_nand_read_buf(struct nand_chip *chip, u8 *buf, int len) +{ + struct rb4xx_nand *nand = chip->priv; +#endif + struct rb4xx_cpld *cpld = nand->cpld; + + cpld->read_nand(cpld, buf, len); +} + +#if LINUX_VERSION_CODE < KERNEL_VERSION(4,20,0) +static void rb4xx_nand_cmd_ctrl(struct mtd_info *mtd, int dat, unsigned int ctrl) +{ + struct rb4xx_nand *nand = mtd->priv; +#else +static void rb4xx_nand_cmd_ctrl(struct nand_chip *chip, int dat, + unsigned int ctrl) +{ + struct rb4xx_nand *nand = chip->priv; +#endif + struct rb4xx_cpld *cpld = nand->cpld; + u8 data = dat; + + if (ctrl & NAND_CTRL_CHANGE) { + gpiod_set_value_cansleep(nand->cle, !!(ctrl & NAND_CLE)); + gpiod_set_value_cansleep(nand->ale, !!(ctrl & NAND_ALE)); + gpiod_set_value_cansleep(nand->nce, !(ctrl & NAND_NCE)); + } + + if (dat != NAND_CMD_NONE) + cpld->write_nand(cpld, &data, 1); +} + +#if LINUX_VERSION_CODE < KERNEL_VERSION(4,20,0) +static int rb4xx_nand_dev_ready(struct mtd_info *mtd) +{ + struct rb4xx_nand *nand = mtd->priv; +#else +static int rb4xx_nand_dev_ready(struct nand_chip *chip) +{ + struct rb4xx_nand *nand = chip->priv; +#endif + + return gpiod_get_value_cansleep(nand->rdy); +} + +static int rb4xx_nand_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct device *parent = dev->parent; + struct rb4xx_nand *nand; + struct mtd_info *mtd; + int ret; + + if (!parent) + return -ENODEV; + + nand = devm_kzalloc(dev, sizeof(*nand), GFP_KERNEL); + if (!nand) + return -ENOMEM; + + platform_set_drvdata(pdev, nand); + nand->cpld = dev_get_drvdata(parent); + nand->dev = dev; + + nand->ale = devm_gpiod_get_index(dev, NULL, 0, GPIOD_OUT_LOW); + if (IS_ERR(nand->ale)) + dev_err(dev, "missing gpio ALE: %ld\n", PTR_ERR(nand->ale)); + + nand->cle = devm_gpiod_get_index(dev, NULL, 1, GPIOD_OUT_LOW); + if (IS_ERR(nand->cle)) + dev_err(dev, "missing gpio CLE: %ld\n", PTR_ERR(nand->cle)); + + nand->nce = devm_gpiod_get_index(dev, NULL, 2, GPIOD_OUT_LOW); + if (IS_ERR(nand->nce)) + dev_err(dev, "missing gpio nCE: %ld\n", PTR_ERR(nand->nce)); + + nand->rdy = devm_gpiod_get_index(dev, NULL, 3, GPIOD_IN); + if (IS_ERR(nand->rdy)) + dev_err(dev, "missing gpio RDY: %ld\n", PTR_ERR(nand->rdy)); + + if (IS_ERR(nand->ale) || IS_ERR(nand->cle) || + IS_ERR(nand->nce) || IS_ERR(nand->rdy)) + return -ENOENT; + + gpiod_set_consumer_name(nand->ale, "mikrotik:nand:ALE"); + gpiod_set_consumer_name(nand->cle, "mikrotik:nand:CLE"); + gpiod_set_consumer_name(nand->nce, "mikrotik:nand:nCE"); + gpiod_set_consumer_name(nand->rdy, "mikrotik:nand:RDY"); + + mtd = nand_to_mtd(&nand->chip); + mtd->priv = nand; + mtd->owner = THIS_MODULE; + mtd->dev.parent = dev; + mtd_set_of_node(mtd, dev->of_node); + + if (mtd->writesize == 512) + mtd_set_ooblayout(mtd, &rb4xx_nand_ecclayout_ops); + + nand->chip.ecc.mode = NAND_ECC_SOFT; + nand->chip.ecc.algo = NAND_ECC_HAMMING; + nand->chip.options = NAND_NO_SUBPAGE_WRITE; + nand->chip.priv = nand; + +#if LINUX_VERSION_CODE < KERNEL_VERSION(4,20,0) + nand->chip.read_byte = rb4xx_nand_read_byte; + nand->chip.write_buf = rb4xx_nand_write_buf; + nand->chip.read_buf = rb4xx_nand_read_buf; + nand->chip.cmd_ctrl = rb4xx_nand_cmd_ctrl; + nand->chip.dev_ready = rb4xx_nand_dev_ready; + nand->chip.chip_delay = 25; + + ret = nand_scan(mtd, 1); +#else + nand->chip.legacy.read_byte = rb4xx_nand_read_byte; + nand->chip.legacy.write_buf = rb4xx_nand_write_buf; + nand->chip.legacy.read_buf = rb4xx_nand_read_buf; + nand->chip.legacy.cmd_ctrl = rb4xx_nand_cmd_ctrl; + nand->chip.legacy.dev_ready = rb4xx_nand_dev_ready; + nand->chip.legacy.chip_delay = 25; + + ret = nand_scan(&nand->chip, 1); +#endif + if (ret) + return -ENXIO; + + ret = mtd_device_register(mtd, NULL, 0); + if (ret) { +#if LINUX_VERSION_CODE < KERNEL_VERSION(4,20,0) + nand_release(mtd); +#else + nand_release(&nand->chip); +#endif + return ret; + } + + return 0; +} + +static int rb4xx_nand_remove(struct platform_device *pdev) +{ + struct rb4xx_nand *nand = platform_get_drvdata(pdev); + +#if LINUX_VERSION_CODE < KERNEL_VERSION(4,20,0) + nand_release(nand_to_mtd(&nand->chip)); +#else + nand_release(&nand->chip); +#endif + + return 0; +} + +static const struct platform_device_id rb4xx_nand_id_table[] = { + { "mikrotik,rb4xx-nand", }, + { }, +}; +MODULE_DEVICE_TABLE(platform, rb4xx_nand_id_table); + +static struct platform_driver rb4xx_nand_driver = { + .probe = rb4xx_nand_probe, + .remove = rb4xx_nand_remove, + .id_table = rb4xx_nand_id_table, + .driver = { + .name = "rb4xx-nand", + }, +}; + +module_platform_driver(rb4xx_nand_driver); + +MODULE_DESCRIPTION("Mikrotik RB4xx NAND driver"); +MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>"); +MODULE_AUTHOR("Imre Kaloz <kaloz@openwrt.org>"); +MODULE_AUTHOR("Bert Vermeulen <bert@biot.com>"); +MODULE_AUTHOR("Christopher Hill <ch6574@gmail.com"); +MODULE_LICENSE("GPL v2"); |