1 // SPDX-License-Identifier: GPL-2.0-only
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>
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>
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
;
34 struct nand_chip
*chips
[OXNAS_NAND_MAX_CHIPS
];
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
,
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
,
64 struct oxnas_nand_ctrl
*oxnas
= nand_get_controller_data(chip
);
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
;
87 /* Allocate memory for the device structure (and zero it) */
88 oxnas
= devm_kzalloc(&pdev
->dev
, sizeof(*oxnas
),
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
))
104 /* Only a single chip node is supported */
105 count
= of_get_child_count(np
);
109 err
= clk_prepare_enable(oxnas
->clk
);
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
),
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
;
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);
141 goto err_release_child
;
143 err
= mtd_device_register(mtd
, NULL
, 0);
145 goto err_cleanup_nand
;
147 oxnas
->chips
[oxnas
->nchips
++] = chip
;
150 /* Exit if no chips found */
151 if (!oxnas
->nchips
) {
153 goto err_clk_unprepare
;
156 platform_set_drvdata(pdev
, oxnas
);
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
)));
172 clk_disable_unprepare(oxnas
->clk
);
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
;
182 for (i
= 0; i
< oxnas
->nchips
; i
++) {
183 chip
= oxnas
->chips
[i
];
184 WARN_ON(mtd_device_unregister(nand_to_mtd(chip
)));
188 clk_disable_unprepare(oxnas
->clk
);
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
,
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");