2007-10-17 07:10:40 +08:00
|
|
|
/*
|
|
|
|
* drivers/mtd/nand/orion_nand.c
|
|
|
|
*
|
|
|
|
* NAND support for Marvell Orion SoC platforms
|
|
|
|
*
|
|
|
|
* Tzachi Perelstein <tzachi@marvell.com>
|
|
|
|
*
|
|
|
|
* This file is licensed under the terms of the GNU General Public
|
|
|
|
* License version 2. This program is licensed "as is" without any
|
|
|
|
* warranty of any kind, whether express or implied.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/platform_device.h>
|
2012-04-18 18:06:41 +08:00
|
|
|
#include <linux/of.h>
|
2007-10-17 07:10:40 +08:00
|
|
|
#include <linux/mtd/mtd.h>
|
|
|
|
#include <linux/mtd/nand.h>
|
|
|
|
#include <linux/mtd/partitions.h>
|
2012-02-19 18:01:22 +08:00
|
|
|
#include <linux/clk.h>
|
|
|
|
#include <linux/err.h>
|
2014-10-16 12:58:35 +08:00
|
|
|
#include <linux/io.h>
|
2007-10-17 07:10:40 +08:00
|
|
|
#include <asm/sizes.h>
|
2012-08-24 21:21:54 +08:00
|
|
|
#include <linux/platform_data/mtd-orion_nand.h>
|
2007-10-17 07:10:40 +08:00
|
|
|
|
|
|
|
static void orion_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
|
|
|
|
{
|
2015-12-01 19:03:04 +08:00
|
|
|
struct nand_chip *nc = mtd_to_nand(mtd);
|
2015-12-10 16:00:41 +08:00
|
|
|
struct orion_nand_data *board = nand_get_controller_data(nc);
|
2007-10-17 07:10:40 +08:00
|
|
|
u32 offs;
|
|
|
|
|
|
|
|
if (cmd == NAND_CMD_NONE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (ctrl & NAND_CLE)
|
|
|
|
offs = (1 << board->cle);
|
|
|
|
else if (ctrl & NAND_ALE)
|
|
|
|
offs = (1 << board->ale);
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (nc->options & NAND_BUSWIDTH_16)
|
|
|
|
offs <<= 1;
|
|
|
|
|
|
|
|
writeb(cmd, nc->IO_ADDR_W + offs);
|
|
|
|
}
|
|
|
|
|
2009-06-01 10:25:40 +08:00
|
|
|
static void orion_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
|
|
|
|
{
|
2015-12-01 19:03:04 +08:00
|
|
|
struct nand_chip *chip = mtd_to_nand(mtd);
|
2009-06-01 10:25:40 +08:00
|
|
|
void __iomem *io_base = chip->IO_ADDR_R;
|
|
|
|
uint64_t *buf64;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
while (len && (unsigned long)buf & 7) {
|
|
|
|
*buf++ = readb(io_base);
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
buf64 = (uint64_t *)buf;
|
|
|
|
while (i < len/8) {
|
2010-04-24 01:17:47 +08:00
|
|
|
/*
|
|
|
|
* Since GCC has no proper constraint (PR 43518)
|
|
|
|
* force x variable to r2/r3 registers as ldrd instruction
|
|
|
|
* requires first register to be even.
|
|
|
|
*/
|
|
|
|
register uint64_t x asm ("r2");
|
|
|
|
|
2009-08-20 15:19:53 +08:00
|
|
|
asm volatile ("ldrd\t%0, [%1]" : "=&r" (x) : "r" (io_base));
|
2009-06-01 10:25:40 +08:00
|
|
|
buf64[i++] = x;
|
|
|
|
}
|
|
|
|
i *= 8;
|
|
|
|
while (i < len)
|
|
|
|
buf[i++] = readb(io_base);
|
|
|
|
}
|
|
|
|
|
2007-10-17 07:10:40 +08:00
|
|
|
static int __init orion_nand_probe(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct mtd_info *mtd;
|
|
|
|
struct nand_chip *nc;
|
|
|
|
struct orion_nand_data *board;
|
2009-12-15 05:48:34 +08:00
|
|
|
struct resource *res;
|
2012-02-19 18:01:22 +08:00
|
|
|
struct clk *clk;
|
2007-10-17 07:10:40 +08:00
|
|
|
void __iomem *io_base;
|
|
|
|
int ret = 0;
|
2012-04-18 18:06:41 +08:00
|
|
|
u32 val = 0;
|
2007-10-17 07:10:40 +08:00
|
|
|
|
2014-10-16 12:58:35 +08:00
|
|
|
nc = devm_kzalloc(&pdev->dev,
|
2015-12-10 16:00:17 +08:00
|
|
|
sizeof(struct nand_chip),
|
2014-10-16 12:58:35 +08:00
|
|
|
GFP_KERNEL);
|
|
|
|
if (!nc)
|
|
|
|
return -ENOMEM;
|
2015-12-10 16:00:17 +08:00
|
|
|
mtd = nand_to_mtd(nc);
|
2007-10-17 07:10:40 +08:00
|
|
|
|
2009-12-15 05:48:34 +08:00
|
|
|
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
2014-10-16 12:58:35 +08:00
|
|
|
io_base = devm_ioremap_resource(&pdev->dev, res);
|
2009-12-15 05:48:34 +08:00
|
|
|
|
2014-10-16 12:58:35 +08:00
|
|
|
if (IS_ERR(io_base))
|
|
|
|
return PTR_ERR(io_base);
|
2007-10-17 07:10:40 +08:00
|
|
|
|
2012-04-18 18:06:41 +08:00
|
|
|
if (pdev->dev.of_node) {
|
|
|
|
board = devm_kzalloc(&pdev->dev, sizeof(struct orion_nand_data),
|
|
|
|
GFP_KERNEL);
|
2014-10-16 12:58:35 +08:00
|
|
|
if (!board)
|
|
|
|
return -ENOMEM;
|
2012-04-18 18:06:41 +08:00
|
|
|
if (!of_property_read_u32(pdev->dev.of_node, "cle", &val))
|
|
|
|
board->cle = (u8)val;
|
|
|
|
else
|
|
|
|
board->cle = 0;
|
|
|
|
if (!of_property_read_u32(pdev->dev.of_node, "ale", &val))
|
|
|
|
board->ale = (u8)val;
|
|
|
|
else
|
|
|
|
board->ale = 1;
|
|
|
|
if (!of_property_read_u32(pdev->dev.of_node,
|
|
|
|
"bank-width", &val))
|
|
|
|
board->width = (u8)val * 8;
|
|
|
|
else
|
|
|
|
board->width = 8;
|
|
|
|
if (!of_property_read_u32(pdev->dev.of_node,
|
|
|
|
"chip-delay", &val))
|
|
|
|
board->chip_delay = (u8)val;
|
2013-07-30 16:18:33 +08:00
|
|
|
} else {
|
|
|
|
board = dev_get_platdata(&pdev->dev);
|
|
|
|
}
|
2007-10-17 07:10:40 +08:00
|
|
|
|
2015-06-11 04:38:58 +08:00
|
|
|
mtd->dev.parent = &pdev->dev;
|
2007-10-17 07:10:40 +08:00
|
|
|
|
2015-12-10 16:00:41 +08:00
|
|
|
nand_set_controller_data(nc, board);
|
2015-10-31 11:33:25 +08:00
|
|
|
nand_set_flash_node(nc, pdev->dev.of_node);
|
2007-10-17 07:10:40 +08:00
|
|
|
nc->IO_ADDR_R = nc->IO_ADDR_W = io_base;
|
|
|
|
nc->cmd_ctrl = orion_nand_cmd_ctrl;
|
2009-06-01 10:25:40 +08:00
|
|
|
nc->read_buf = orion_nand_read_buf;
|
2007-10-17 07:10:40 +08:00
|
|
|
nc->ecc.mode = NAND_ECC_SOFT;
|
2016-04-08 18:23:48 +08:00
|
|
|
nc->ecc.algo = NAND_ECC_HAMMING;
|
2007-10-17 07:10:40 +08:00
|
|
|
|
2008-05-05 14:25:52 +08:00
|
|
|
if (board->chip_delay)
|
|
|
|
nc->chip_delay = board->chip_delay;
|
|
|
|
|
2012-04-18 18:06:41 +08:00
|
|
|
WARN(board->width > 16,
|
|
|
|
"%d bit bus width out of range",
|
|
|
|
board->width);
|
|
|
|
|
2007-10-17 07:10:40 +08:00
|
|
|
if (board->width == 16)
|
|
|
|
nc->options |= NAND_BUSWIDTH_16;
|
|
|
|
|
2010-04-20 17:26:18 +08:00
|
|
|
if (board->dev_ready)
|
|
|
|
nc->dev_ready = board->dev_ready;
|
|
|
|
|
2007-10-17 07:10:40 +08:00
|
|
|
platform_set_drvdata(pdev, mtd);
|
|
|
|
|
2012-02-19 18:01:22 +08:00
|
|
|
/* Not all platforms can gate the clock, so it is not
|
|
|
|
an error if the clock does not exists. */
|
|
|
|
clk = clk_get(&pdev->dev, NULL);
|
|
|
|
if (!IS_ERR(clk)) {
|
|
|
|
clk_prepare_enable(clk);
|
|
|
|
clk_put(clk);
|
|
|
|
}
|
|
|
|
|
2007-10-17 07:10:40 +08:00
|
|
|
if (nand_scan(mtd, 1)) {
|
|
|
|
ret = -ENXIO;
|
|
|
|
goto no_dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
mtd->name = "orion_nand";
|
2015-10-31 11:33:25 +08:00
|
|
|
ret = mtd_device_register(mtd, board->parts, board->nr_parts);
|
2007-10-17 07:10:40 +08:00
|
|
|
if (ret) {
|
|
|
|
nand_release(mtd);
|
|
|
|
goto no_dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
no_dev:
|
2012-07-19 06:04:09 +08:00
|
|
|
if (!IS_ERR(clk)) {
|
|
|
|
clk_disable_unprepare(clk);
|
|
|
|
clk_put(clk);
|
|
|
|
}
|
2007-10-17 07:10:40 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-11-20 02:26:04 +08:00
|
|
|
static int orion_nand_remove(struct platform_device *pdev)
|
2007-10-17 07:10:40 +08:00
|
|
|
{
|
|
|
|
struct mtd_info *mtd = platform_get_drvdata(pdev);
|
2012-02-19 18:01:22 +08:00
|
|
|
struct clk *clk;
|
2007-10-17 07:10:40 +08:00
|
|
|
|
|
|
|
nand_release(mtd);
|
|
|
|
|
2012-02-19 18:01:22 +08:00
|
|
|
clk = clk_get(&pdev->dev, NULL);
|
|
|
|
if (!IS_ERR(clk)) {
|
|
|
|
clk_disable_unprepare(clk);
|
|
|
|
clk_put(clk);
|
|
|
|
}
|
|
|
|
|
2007-10-17 07:10:40 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-18 18:06:41 +08:00
|
|
|
#ifdef CONFIG_OF
|
2014-05-07 16:50:23 +08:00
|
|
|
static const struct of_device_id orion_nand_of_match_table[] = {
|
2012-07-19 01:22:54 +08:00
|
|
|
{ .compatible = "marvell,orion-nand", },
|
2012-04-18 18:06:41 +08:00
|
|
|
{},
|
|
|
|
};
|
2015-09-18 06:14:02 +08:00
|
|
|
MODULE_DEVICE_TABLE(of, orion_nand_of_match_table);
|
2012-04-18 18:06:41 +08:00
|
|
|
#endif
|
|
|
|
|
2007-10-17 07:10:40 +08:00
|
|
|
static struct platform_driver orion_nand_driver = {
|
2012-11-20 02:21:24 +08:00
|
|
|
.remove = orion_nand_remove,
|
2007-10-17 07:10:40 +08:00
|
|
|
.driver = {
|
|
|
|
.name = "orion_nand",
|
2012-04-18 18:06:41 +08:00
|
|
|
.of_match_table = of_match_ptr(orion_nand_of_match_table),
|
2007-10-17 07:10:40 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2013-03-05 12:29:20 +08:00
|
|
|
module_platform_driver_probe(orion_nand_driver, orion_nand_probe);
|
2007-10-17 07:10:40 +08:00
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_AUTHOR("Tzachi Perelstein");
|
|
|
|
MODULE_DESCRIPTION("NAND glue for Orion platforms");
|
2008-04-19 04:44:27 +08:00
|
|
|
MODULE_ALIAS("platform:orion_nand");
|