2 * Oxford Semiconductor OXNAS NAND driver
4 * Copyright (C) 2016 Neil Armstrong <narmstrong@baylibre.com>
5 * Heavily based on plat_nand.c :
6 * Author: Vitaly Wool <vitalywool@gmail.com>
7 * Copyright (C) 2013 Ma Haijun <mahaijuns@gmail.com>
8 * Copyright (C) 2012 John Crispin <blogic@openwrt.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
16 #include <linux/err.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/slab.h>
21 #include <linux/clk.h>
22 #include <linux/reset.h>
23 #include <linux/mtd/mtd.h>
24 #include <linux/mtd/nand.h>
25 #include <linux/mtd/partitions.h>
29 #define OXNAS_NAND_CMD_ALE BIT(18)
30 #define OXNAS_NAND_CMD_CLE BIT(19)
32 #define OXNAS_NAND_MAX_CHIPS 1
34 struct oxnas_nand_ctrl {
35 struct nand_hw_control base;
36 void __iomem *io_base;
38 struct nand_chip *chips[OXNAS_NAND_MAX_CHIPS];
41 static uint8_t oxnas_nand_read_byte(struct mtd_info *mtd)
43 struct nand_chip *chip = mtd_to_nand(mtd);
44 struct oxnas_nand_ctrl *oxnas = nand_get_controller_data(chip);
46 return readb(oxnas->io_base);
49 static void oxnas_nand_read_buf(struct mtd_info *mtd, u8 *buf, int len)
51 struct nand_chip *chip = mtd_to_nand(mtd);
52 struct oxnas_nand_ctrl *oxnas = nand_get_controller_data(chip);
54 ioread8_rep(oxnas->io_base, buf, len);
57 static void oxnas_nand_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
59 struct nand_chip *chip = mtd_to_nand(mtd);
60 struct oxnas_nand_ctrl *oxnas = nand_get_controller_data(chip);
62 iowrite8_rep(oxnas->io_base, buf, len);
65 /* Single CS command control */
66 static void oxnas_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
69 struct nand_chip *chip = mtd_to_nand(mtd);
70 struct oxnas_nand_ctrl *oxnas = nand_get_controller_data(chip);
73 writeb(cmd, oxnas->io_base + OXNAS_NAND_CMD_CLE);
74 else if (ctrl & NAND_ALE)
75 writeb(cmd, oxnas->io_base + OXNAS_NAND_CMD_ALE);
79 * Probe for the NAND device.
81 static int oxnas_nand_probe(struct platform_device *pdev)
83 struct device_node *np = pdev->dev.of_node;
84 struct device_node *nand_np;
85 struct oxnas_nand_ctrl *oxnas;
86 struct nand_chip *chip;
93 /* Allocate memory for the device structure (and zero it) */
94 oxnas = devm_kzalloc(&pdev->dev, sizeof(*oxnas),
99 nand_hw_control_init(&oxnas->base);
101 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
102 oxnas->io_base = devm_ioremap_resource(&pdev->dev, res);
103 if (IS_ERR(oxnas->io_base))
104 return PTR_ERR(oxnas->io_base);
106 oxnas->clk = devm_clk_get(&pdev->dev, NULL);
107 if (IS_ERR(oxnas->clk))
110 /* Only a single chip node is supported */
111 count = of_get_child_count(np);
115 clk_prepare_enable(oxnas->clk);
116 device_reset_optional(&pdev->dev);
118 for_each_child_of_node(np, nand_np) {
119 chip = devm_kzalloc(&pdev->dev, sizeof(struct nand_chip),
124 chip->controller = &oxnas->base;
126 nand_set_flash_node(chip, nand_np);
127 nand_set_controller_data(chip, oxnas);
129 mtd = nand_to_mtd(chip);
130 mtd->dev.parent = &pdev->dev;
133 chip->cmd_ctrl = oxnas_nand_cmd_ctrl;
134 chip->read_buf = oxnas_nand_read_buf;
135 chip->read_byte = oxnas_nand_read_byte;
136 chip->write_buf = oxnas_nand_write_buf;
137 chip->chip_delay = 30;
139 /* Scan to find existence of the device */
140 err = nand_scan(mtd, 1);
144 err = mtd_device_register(mtd, NULL, 0);
150 oxnas->chips[nchips] = chip;
154 /* Exit if no chips found */
158 platform_set_drvdata(pdev, oxnas);
163 static int oxnas_nand_remove(struct platform_device *pdev)
165 struct oxnas_nand_ctrl *oxnas = platform_get_drvdata(pdev);
168 nand_release(nand_to_mtd(oxnas->chips[0]));
170 clk_disable_unprepare(oxnas->clk);
175 static const struct of_device_id oxnas_nand_match[] = {
176 { .compatible = "oxsemi,ox820-nand" },
179 MODULE_DEVICE_TABLE(of, oxnas_nand_match);
181 static struct platform_driver oxnas_nand_driver = {
182 .probe = oxnas_nand_probe,
183 .remove = oxnas_nand_remove,
185 .name = "oxnas_nand",
186 .of_match_table = oxnas_nand_match,
190 module_platform_driver(oxnas_nand_driver);
192 MODULE_LICENSE("GPL");
193 MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
194 MODULE_DESCRIPTION("Oxnas NAND driver");
195 MODULE_ALIAS("platform:oxnas_nand");