blob: 16df274f4cd656753e66c09a44de973a4f01eb6d [file] [log] [blame]
Neil Armstrong66859242016-10-20 10:49:01 +02001/*
2 * Oxford Semiconductor OXNAS NAND driver
3
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>
9 *
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.
13 *
14 */
15
16#include <linux/err.h>
17#include <linux/io.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>
Boris Brezillond4092d72017-08-04 17:29:10 +020024#include <linux/mtd/rawnand.h>
Neil Armstrong66859242016-10-20 10:49:01 +020025#include <linux/mtd/partitions.h>
26#include <linux/of.h>
27
28/* Nand commands */
29#define OXNAS_NAND_CMD_ALE BIT(18)
30#define OXNAS_NAND_CMD_CLE BIT(19)
31
32#define OXNAS_NAND_MAX_CHIPS 1
33
34struct oxnas_nand_ctrl {
Miquel Raynal7da45132018-07-17 09:08:02 +020035 struct nand_controller base;
Neil Armstrong66859242016-10-20 10:49:01 +020036 void __iomem *io_base;
37 struct clk *clk;
38 struct nand_chip *chips[OXNAS_NAND_MAX_CHIPS];
39};
40
Boris Brezillon7e534322018-09-06 14:05:22 +020041static uint8_t oxnas_nand_read_byte(struct nand_chip *chip)
Neil Armstrong66859242016-10-20 10:49:01 +020042{
Neil Armstrong66859242016-10-20 10:49:01 +020043 struct oxnas_nand_ctrl *oxnas = nand_get_controller_data(chip);
44
45 return readb(oxnas->io_base);
46}
47
Boris Brezillon7e534322018-09-06 14:05:22 +020048static void oxnas_nand_read_buf(struct nand_chip *chip, u8 *buf, int len)
Neil Armstrong66859242016-10-20 10:49:01 +020049{
Neil Armstrong66859242016-10-20 10:49:01 +020050 struct oxnas_nand_ctrl *oxnas = nand_get_controller_data(chip);
51
52 ioread8_rep(oxnas->io_base, buf, len);
53}
54
Boris Brezillonc0739d82018-09-06 14:05:23 +020055static void oxnas_nand_write_buf(struct nand_chip *chip, const u8 *buf,
56 int len)
Neil Armstrong66859242016-10-20 10:49:01 +020057{
Neil Armstrong66859242016-10-20 10:49:01 +020058 struct oxnas_nand_ctrl *oxnas = nand_get_controller_data(chip);
59
60 iowrite8_rep(oxnas->io_base, buf, len);
61}
62
63/* Single CS command control */
Boris Brezillon0f808c12018-09-06 14:05:26 +020064static void oxnas_nand_cmd_ctrl(struct nand_chip *chip, int cmd,
Neil Armstrong66859242016-10-20 10:49:01 +020065 unsigned int ctrl)
66{
Neil Armstrong66859242016-10-20 10:49:01 +020067 struct oxnas_nand_ctrl *oxnas = nand_get_controller_data(chip);
68
69 if (ctrl & NAND_CLE)
70 writeb(cmd, oxnas->io_base + OXNAS_NAND_CMD_CLE);
71 else if (ctrl & NAND_ALE)
72 writeb(cmd, oxnas->io_base + OXNAS_NAND_CMD_ALE);
73}
74
75/*
76 * Probe for the NAND device.
77 */
78static int oxnas_nand_probe(struct platform_device *pdev)
79{
80 struct device_node *np = pdev->dev.of_node;
81 struct device_node *nand_np;
82 struct oxnas_nand_ctrl *oxnas;
83 struct nand_chip *chip;
84 struct mtd_info *mtd;
85 struct resource *res;
86 int nchips = 0;
87 int count = 0;
88 int err = 0;
89
90 /* Allocate memory for the device structure (and zero it) */
Dan Carpenterb98e1992017-04-25 12:19:49 +030091 oxnas = devm_kzalloc(&pdev->dev, sizeof(*oxnas),
Neil Armstrong66859242016-10-20 10:49:01 +020092 GFP_KERNEL);
93 if (!oxnas)
94 return -ENOMEM;
95
Miquel Raynal7da45132018-07-17 09:08:02 +020096 nand_controller_init(&oxnas->base);
Neil Armstrong66859242016-10-20 10:49:01 +020097
98 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
99 oxnas->io_base = devm_ioremap_resource(&pdev->dev, res);
100 if (IS_ERR(oxnas->io_base))
101 return PTR_ERR(oxnas->io_base);
102
103 oxnas->clk = devm_clk_get(&pdev->dev, NULL);
104 if (IS_ERR(oxnas->clk))
105 oxnas->clk = NULL;
106
107 /* Only a single chip node is supported */
108 count = of_get_child_count(np);
109 if (count > 1)
110 return -EINVAL;
111
Arvind Yadav24c9cd82017-08-01 17:07:27 +0530112 err = clk_prepare_enable(oxnas->clk);
113 if (err)
114 return err;
115
Neil Armstrong66859242016-10-20 10:49:01 +0200116 device_reset_optional(&pdev->dev);
117
118 for_each_child_of_node(np, nand_np) {
119 chip = devm_kzalloc(&pdev->dev, sizeof(struct nand_chip),
120 GFP_KERNEL);
Arvind Yadav24c9cd82017-08-01 17:07:27 +0530121 if (!chip) {
122 err = -ENOMEM;
123 goto err_clk_unprepare;
124 }
Neil Armstrong66859242016-10-20 10:49:01 +0200125
126 chip->controller = &oxnas->base;
127
128 nand_set_flash_node(chip, nand_np);
129 nand_set_controller_data(chip, oxnas);
130
131 mtd = nand_to_mtd(chip);
132 mtd->dev.parent = &pdev->dev;
133 mtd->priv = chip;
134
135 chip->cmd_ctrl = oxnas_nand_cmd_ctrl;
Boris Brezillon716bbba2018-09-07 00:38:35 +0200136 chip->legacy.read_buf = oxnas_nand_read_buf;
137 chip->legacy.read_byte = oxnas_nand_read_byte;
138 chip->legacy.write_buf = oxnas_nand_write_buf;
Neil Armstrong66859242016-10-20 10:49:01 +0200139 chip->chip_delay = 30;
140
141 /* Scan to find existence of the device */
Boris Brezillon00ad3782018-09-06 14:05:14 +0200142 err = nand_scan(chip, 1);
Neil Armstrong66859242016-10-20 10:49:01 +0200143 if (err)
Arvind Yadav24c9cd82017-08-01 17:07:27 +0530144 goto err_clk_unprepare;
Neil Armstrong66859242016-10-20 10:49:01 +0200145
146 err = mtd_device_register(mtd, NULL, 0);
147 if (err) {
Boris Brezillon59ac2762018-09-06 14:05:15 +0200148 nand_release(chip);
Arvind Yadav24c9cd82017-08-01 17:07:27 +0530149 goto err_clk_unprepare;
Neil Armstrong66859242016-10-20 10:49:01 +0200150 }
151
152 oxnas->chips[nchips] = chip;
153 ++nchips;
154 }
155
156 /* Exit if no chips found */
Arvind Yadav24c9cd82017-08-01 17:07:27 +0530157 if (!nchips) {
158 err = -ENODEV;
159 goto err_clk_unprepare;
160 }
Neil Armstrong66859242016-10-20 10:49:01 +0200161
162 platform_set_drvdata(pdev, oxnas);
163
164 return 0;
Arvind Yadav24c9cd82017-08-01 17:07:27 +0530165
166err_clk_unprepare:
167 clk_disable_unprepare(oxnas->clk);
168 return err;
Neil Armstrong66859242016-10-20 10:49:01 +0200169}
170
171static int oxnas_nand_remove(struct platform_device *pdev)
172{
173 struct oxnas_nand_ctrl *oxnas = platform_get_drvdata(pdev);
174
175 if (oxnas->chips[0])
Boris Brezillon59ac2762018-09-06 14:05:15 +0200176 nand_release(oxnas->chips[0]);
Neil Armstrong66859242016-10-20 10:49:01 +0200177
178 clk_disable_unprepare(oxnas->clk);
179
180 return 0;
181}
182
183static const struct of_device_id oxnas_nand_match[] = {
184 { .compatible = "oxsemi,ox820-nand" },
185 {},
186};
187MODULE_DEVICE_TABLE(of, oxnas_nand_match);
188
189static struct platform_driver oxnas_nand_driver = {
190 .probe = oxnas_nand_probe,
191 .remove = oxnas_nand_remove,
192 .driver = {
193 .name = "oxnas_nand",
194 .of_match_table = oxnas_nand_match,
195 },
196};
197
198module_platform_driver(oxnas_nand_driver);
199
200MODULE_LICENSE("GPL");
201MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
202MODULE_DESCRIPTION("Oxnas NAND driver");
203MODULE_ALIAS("platform:oxnas_nand");