Merge tag 'trace-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux/fpc-iii.git] / drivers / mtd / nand / raw / oxnas_nand.c
blobf44947043e5a2995c255423964bc31a0cf522b27
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Oxford Semiconductor OXNAS NAND driver
5 * Copyright (C) 2016 Neil Armstrong <narmstrong@baylibre.com>
6 * Heavily based on plat_nand.c :
7 * Author: Vitaly Wool <vitalywool@gmail.com>
8 * Copyright (C) 2013 Ma Haijun <mahaijuns@gmail.com>
9 * Copyright (C) 2012 John Crispin <blogic@openwrt.org>
12 #include <linux/err.h>
13 #include <linux/io.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/slab.h>
17 #include <linux/clk.h>
18 #include <linux/reset.h>
19 #include <linux/mtd/mtd.h>
20 #include <linux/mtd/rawnand.h>
21 #include <linux/mtd/partitions.h>
22 #include <linux/of.h>
24 /* Nand commands */
25 #define OXNAS_NAND_CMD_ALE BIT(18)
26 #define OXNAS_NAND_CMD_CLE BIT(19)
28 #define OXNAS_NAND_MAX_CHIPS 1
30 struct oxnas_nand_ctrl {
31 struct nand_controller base;
32 void __iomem *io_base;
33 struct clk *clk;
34 struct nand_chip *chips[OXNAS_NAND_MAX_CHIPS];
35 unsigned int nchips;
38 static uint8_t oxnas_nand_read_byte(struct nand_chip *chip)
40 struct oxnas_nand_ctrl *oxnas = nand_get_controller_data(chip);
42 return readb(oxnas->io_base);
45 static void oxnas_nand_read_buf(struct nand_chip *chip, u8 *buf, int len)
47 struct oxnas_nand_ctrl *oxnas = nand_get_controller_data(chip);
49 ioread8_rep(oxnas->io_base, buf, len);
52 static void oxnas_nand_write_buf(struct nand_chip *chip, const u8 *buf,
53 int len)
55 struct oxnas_nand_ctrl *oxnas = nand_get_controller_data(chip);
57 iowrite8_rep(oxnas->io_base, buf, len);
60 /* Single CS command control */
61 static void oxnas_nand_cmd_ctrl(struct nand_chip *chip, int cmd,
62 unsigned int ctrl)
64 struct oxnas_nand_ctrl *oxnas = nand_get_controller_data(chip);
66 if (ctrl & NAND_CLE)
67 writeb(cmd, oxnas->io_base + OXNAS_NAND_CMD_CLE);
68 else if (ctrl & NAND_ALE)
69 writeb(cmd, oxnas->io_base + OXNAS_NAND_CMD_ALE);
73 * Probe for the NAND device.
75 static int oxnas_nand_probe(struct platform_device *pdev)
77 struct device_node *np = pdev->dev.of_node;
78 struct device_node *nand_np;
79 struct oxnas_nand_ctrl *oxnas;
80 struct nand_chip *chip;
81 struct mtd_info *mtd;
82 struct resource *res;
83 int count = 0;
84 int err = 0;
85 int i;
87 /* Allocate memory for the device structure (and zero it) */
88 oxnas = devm_kzalloc(&pdev->dev, sizeof(*oxnas),
89 GFP_KERNEL);
90 if (!oxnas)
91 return -ENOMEM;
93 nand_controller_init(&oxnas->base);
95 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
96 oxnas->io_base = devm_ioremap_resource(&pdev->dev, res);
97 if (IS_ERR(oxnas->io_base))
98 return PTR_ERR(oxnas->io_base);
100 oxnas->clk = devm_clk_get(&pdev->dev, NULL);
101 if (IS_ERR(oxnas->clk))
102 oxnas->clk = NULL;
104 /* Only a single chip node is supported */
105 count = of_get_child_count(np);
106 if (count > 1)
107 return -EINVAL;
109 err = clk_prepare_enable(oxnas->clk);
110 if (err)
111 return err;
113 device_reset_optional(&pdev->dev);
115 for_each_child_of_node(np, nand_np) {
116 chip = devm_kzalloc(&pdev->dev, sizeof(struct nand_chip),
117 GFP_KERNEL);
118 if (!chip) {
119 err = -ENOMEM;
120 goto err_release_child;
123 chip->controller = &oxnas->base;
125 nand_set_flash_node(chip, nand_np);
126 nand_set_controller_data(chip, oxnas);
128 mtd = nand_to_mtd(chip);
129 mtd->dev.parent = &pdev->dev;
130 mtd->priv = chip;
132 chip->legacy.cmd_ctrl = oxnas_nand_cmd_ctrl;
133 chip->legacy.read_buf = oxnas_nand_read_buf;
134 chip->legacy.read_byte = oxnas_nand_read_byte;
135 chip->legacy.write_buf = oxnas_nand_write_buf;
136 chip->legacy.chip_delay = 30;
138 /* Scan to find existence of the device */
139 err = nand_scan(chip, 1);
140 if (err)
141 goto err_release_child;
143 err = mtd_device_register(mtd, NULL, 0);
144 if (err)
145 goto err_cleanup_nand;
147 oxnas->chips[oxnas->nchips++] = chip;
150 /* Exit if no chips found */
151 if (!oxnas->nchips) {
152 err = -ENODEV;
153 goto err_clk_unprepare;
156 platform_set_drvdata(pdev, oxnas);
158 return 0;
160 err_cleanup_nand:
161 nand_cleanup(chip);
162 err_release_child:
163 of_node_put(nand_np);
165 for (i = 0; i < oxnas->nchips; i++) {
166 chip = oxnas->chips[i];
167 WARN_ON(mtd_device_unregister(nand_to_mtd(chip)));
168 nand_cleanup(chip);
171 err_clk_unprepare:
172 clk_disable_unprepare(oxnas->clk);
173 return err;
176 static int oxnas_nand_remove(struct platform_device *pdev)
178 struct oxnas_nand_ctrl *oxnas = platform_get_drvdata(pdev);
179 struct nand_chip *chip;
180 int i;
182 for (i = 0; i < oxnas->nchips; i++) {
183 chip = oxnas->chips[i];
184 WARN_ON(mtd_device_unregister(nand_to_mtd(chip)));
185 nand_cleanup(chip);
188 clk_disable_unprepare(oxnas->clk);
190 return 0;
193 static const struct of_device_id oxnas_nand_match[] = {
194 { .compatible = "oxsemi,ox820-nand" },
197 MODULE_DEVICE_TABLE(of, oxnas_nand_match);
199 static struct platform_driver oxnas_nand_driver = {
200 .probe = oxnas_nand_probe,
201 .remove = oxnas_nand_remove,
202 .driver = {
203 .name = "oxnas_nand",
204 .of_match_table = oxnas_nand_match,
208 module_platform_driver(oxnas_nand_driver);
210 MODULE_LICENSE("GPL");
211 MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
212 MODULE_DESCRIPTION("Oxnas NAND driver");
213 MODULE_ALIAS("platform:oxnas_nand");