blob: 024e3fffd4bb49b6176af4701c07e344120a3fc2 [file] [log] [blame]
Anton Vorontsov5c249c52008-03-11 22:33:13 +03001/*
2 * Freescale UPM NAND driver.
3 *
4 * Copyright © 2007-2008 MontaVista Software, Inc.
5 *
6 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <linux/kernel.h>
15#include <linux/module.h>
Wolfgang Grandegger13f53692008-06-09 10:19:08 +020016#include <linux/delay.h>
Anton Vorontsov5c249c52008-03-11 22:33:13 +030017#include <linux/mtd/nand.h>
18#include <linux/mtd/nand_ecc.h>
19#include <linux/mtd/partitions.h>
20#include <linux/mtd/mtd.h>
21#include <linux/of_platform.h>
22#include <linux/of_gpio.h>
23#include <linux/io.h>
24#include <asm/fsl_lbc.h>
25
26struct fsl_upm_nand {
27 struct device *dev;
28 struct mtd_info mtd;
29 struct nand_chip chip;
30 int last_ctrl;
31#ifdef CONFIG_MTD_PARTITIONS
32 struct mtd_partition *parts;
33#endif
34
35 struct fsl_upm upm;
36 uint8_t upm_addr_offset;
37 uint8_t upm_cmd_offset;
38 void __iomem *io_base;
39 int rnb_gpio;
Wolfgang Grandegger13f53692008-06-09 10:19:08 +020040 int chip_delay;
Anton Vorontsov5c249c52008-03-11 22:33:13 +030041};
42
43#define to_fsl_upm_nand(mtd) container_of(mtd, struct fsl_upm_nand, mtd)
44
45static int fun_chip_ready(struct mtd_info *mtd)
46{
47 struct fsl_upm_nand *fun = to_fsl_upm_nand(mtd);
48
49 if (gpio_get_value(fun->rnb_gpio))
50 return 1;
51
52 dev_vdbg(fun->dev, "busy\n");
53 return 0;
54}
55
56static void fun_wait_rnb(struct fsl_upm_nand *fun)
57{
58 int cnt = 1000000;
59
60 if (fun->rnb_gpio >= 0) {
61 while (--cnt && !fun_chip_ready(&fun->mtd))
62 cpu_relax();
Wolfgang Grandegger13f53692008-06-09 10:19:08 +020063 if (!cnt)
64 dev_err(fun->dev, "tired waiting for RNB\n");
65 } else {
66 ndelay(100);
Anton Vorontsov5c249c52008-03-11 22:33:13 +030067 }
Anton Vorontsov5c249c52008-03-11 22:33:13 +030068}
69
70static void fun_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
71{
72 struct fsl_upm_nand *fun = to_fsl_upm_nand(mtd);
73
74 if (!(ctrl & fun->last_ctrl)) {
75 fsl_upm_end_pattern(&fun->upm);
76
77 if (cmd == NAND_CMD_NONE)
78 return;
79
80 fun->last_ctrl = ctrl & (NAND_ALE | NAND_CLE);
81 }
82
83 if (ctrl & NAND_CTRL_CHANGE) {
84 if (ctrl & NAND_ALE)
85 fsl_upm_start_pattern(&fun->upm, fun->upm_addr_offset);
86 else if (ctrl & NAND_CLE)
87 fsl_upm_start_pattern(&fun->upm, fun->upm_cmd_offset);
88 }
89
90 fsl_upm_run_pattern(&fun->upm, fun->io_base, cmd);
91
Anton Vorontsov95ebffd2008-09-18 20:50:26 +040092 fun_wait_rnb(fun);
Anton Vorontsov5c249c52008-03-11 22:33:13 +030093}
94
95static uint8_t fun_read_byte(struct mtd_info *mtd)
96{
97 struct fsl_upm_nand *fun = to_fsl_upm_nand(mtd);
98
99 return in_8(fun->chip.IO_ADDR_R);
100}
101
102static void fun_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
103{
104 struct fsl_upm_nand *fun = to_fsl_upm_nand(mtd);
105 int i;
106
107 for (i = 0; i < len; i++)
108 buf[i] = in_8(fun->chip.IO_ADDR_R);
109}
110
111static void fun_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
112{
113 struct fsl_upm_nand *fun = to_fsl_upm_nand(mtd);
114 int i;
115
116 for (i = 0; i < len; i++) {
117 out_8(fun->chip.IO_ADDR_W, buf[i]);
Anton Vorontsov95ebffd2008-09-18 20:50:26 +0400118 fun_wait_rnb(fun);
Anton Vorontsov5c249c52008-03-11 22:33:13 +0300119 }
120}
121
Anton Vorontsov95ebffd2008-09-18 20:50:26 +0400122static int __devinit fun_chip_init(struct fsl_upm_nand *fun,
123 const struct device_node *upm_np,
124 const struct resource *io_res)
Anton Vorontsov5c249c52008-03-11 22:33:13 +0300125{
126 int ret;
Anton Vorontsov95ebffd2008-09-18 20:50:26 +0400127 struct device_node *flash_np;
Anton Vorontsov5c249c52008-03-11 22:33:13 +0300128#ifdef CONFIG_MTD_PARTITIONS
129 static const char *part_types[] = { "cmdlinepart", NULL, };
130#endif
131
132 fun->chip.IO_ADDR_R = fun->io_base;
133 fun->chip.IO_ADDR_W = fun->io_base;
134 fun->chip.cmd_ctrl = fun_cmd_ctrl;
Wolfgang Grandegger13f53692008-06-09 10:19:08 +0200135 fun->chip.chip_delay = fun->chip_delay;
Anton Vorontsov5c249c52008-03-11 22:33:13 +0300136 fun->chip.read_byte = fun_read_byte;
137 fun->chip.read_buf = fun_read_buf;
138 fun->chip.write_buf = fun_write_buf;
139 fun->chip.ecc.mode = NAND_ECC_SOFT;
140
141 if (fun->rnb_gpio >= 0)
142 fun->chip.dev_ready = fun_chip_ready;
143
144 fun->mtd.priv = &fun->chip;
145 fun->mtd.owner = THIS_MODULE;
146
Anton Vorontsov95ebffd2008-09-18 20:50:26 +0400147 flash_np = of_get_next_child(upm_np, NULL);
148 if (!flash_np)
149 return -ENODEV;
150
151 fun->mtd.name = kasprintf(GFP_KERNEL, "%x.%s", io_res->start,
152 flash_np->name);
153 if (!fun->mtd.name) {
154 ret = -ENOMEM;
155 goto err;
156 }
157
Anton Vorontsov5c249c52008-03-11 22:33:13 +0300158 ret = nand_scan(&fun->mtd, 1);
159 if (ret)
Anton Vorontsov95ebffd2008-09-18 20:50:26 +0400160 goto err;
Anton Vorontsov5c249c52008-03-11 22:33:13 +0300161
162#ifdef CONFIG_MTD_PARTITIONS
163 ret = parse_mtd_partitions(&fun->mtd, part_types, &fun->parts, 0);
Anton Vorontsov95ebffd2008-09-18 20:50:26 +0400164
165#ifdef CONFIG_MTD_OF_PARTS
166 if (ret == 0)
167 ret = of_mtd_parse_partitions(fun->dev, &fun->mtd,
168 flash_np, &fun->parts);
Anton Vorontsov5c249c52008-03-11 22:33:13 +0300169#endif
Anton Vorontsov95ebffd2008-09-18 20:50:26 +0400170 if (ret > 0)
171 ret = add_mtd_partitions(&fun->mtd, fun->parts, ret);
172 else
173#endif
174 ret = add_mtd_device(&fun->mtd);
175err:
176 of_node_put(flash_np);
177 return ret;
Anton Vorontsov5c249c52008-03-11 22:33:13 +0300178}
179
180static int __devinit fun_probe(struct of_device *ofdev,
181 const struct of_device_id *ofid)
182{
183 struct fsl_upm_nand *fun;
184 struct resource io_res;
185 const uint32_t *prop;
186 int ret;
187 int size;
188
189 fun = kzalloc(sizeof(*fun), GFP_KERNEL);
190 if (!fun)
191 return -ENOMEM;
192
193 ret = of_address_to_resource(ofdev->node, 0, &io_res);
194 if (ret) {
195 dev_err(&ofdev->dev, "can't get IO base\n");
196 goto err1;
197 }
198
199 ret = fsl_upm_find(io_res.start, &fun->upm);
200 if (ret) {
201 dev_err(&ofdev->dev, "can't find UPM\n");
202 goto err1;
203 }
204
205 prop = of_get_property(ofdev->node, "fsl,upm-addr-offset", &size);
206 if (!prop || size != sizeof(uint32_t)) {
207 dev_err(&ofdev->dev, "can't get UPM address offset\n");
208 ret = -EINVAL;
209 goto err2;
210 }
211 fun->upm_addr_offset = *prop;
212
213 prop = of_get_property(ofdev->node, "fsl,upm-cmd-offset", &size);
214 if (!prop || size != sizeof(uint32_t)) {
215 dev_err(&ofdev->dev, "can't get UPM command offset\n");
216 ret = -EINVAL;
217 goto err2;
218 }
219 fun->upm_cmd_offset = *prop;
220
221 fun->rnb_gpio = of_get_gpio(ofdev->node, 0);
222 if (fun->rnb_gpio >= 0) {
223 ret = gpio_request(fun->rnb_gpio, ofdev->dev.bus_id);
224 if (ret) {
225 dev_err(&ofdev->dev, "can't request RNB gpio\n");
226 goto err2;
227 }
228 gpio_direction_input(fun->rnb_gpio);
229 } else if (fun->rnb_gpio == -EINVAL) {
230 dev_err(&ofdev->dev, "specified RNB gpio is invalid\n");
231 goto err2;
232 }
233
Wolfgang Grandegger13f53692008-06-09 10:19:08 +0200234 prop = of_get_property(ofdev->node, "chip-delay", NULL);
235 if (prop)
236 fun->chip_delay = *prop;
237 else
238 fun->chip_delay = 50;
239
Anton Vorontsov5c249c52008-03-11 22:33:13 +0300240 fun->io_base = devm_ioremap_nocache(&ofdev->dev, io_res.start,
241 io_res.end - io_res.start + 1);
242 if (!fun->io_base) {
243 ret = -ENOMEM;
244 goto err2;
245 }
246
247 fun->dev = &ofdev->dev;
248 fun->last_ctrl = NAND_CLE;
Anton Vorontsov5c249c52008-03-11 22:33:13 +0300249
Anton Vorontsov95ebffd2008-09-18 20:50:26 +0400250 ret = fun_chip_init(fun, ofdev->node, &io_res);
Anton Vorontsov5c249c52008-03-11 22:33:13 +0300251 if (ret)
252 goto err2;
253
254 dev_set_drvdata(&ofdev->dev, fun);
255
256 return 0;
257err2:
258 if (fun->rnb_gpio >= 0)
259 gpio_free(fun->rnb_gpio);
260err1:
261 kfree(fun);
262
263 return ret;
264}
265
266static int __devexit fun_remove(struct of_device *ofdev)
267{
268 struct fsl_upm_nand *fun = dev_get_drvdata(&ofdev->dev);
269
270 nand_release(&fun->mtd);
Anton Vorontsov95ebffd2008-09-18 20:50:26 +0400271 kfree(fun->mtd.name);
Anton Vorontsov5c249c52008-03-11 22:33:13 +0300272
273 if (fun->rnb_gpio >= 0)
274 gpio_free(fun->rnb_gpio);
275
276 kfree(fun);
277
278 return 0;
279}
280
281static struct of_device_id of_fun_match[] = {
282 { .compatible = "fsl,upm-nand" },
283 {},
284};
285MODULE_DEVICE_TABLE(of, of_fun_match);
286
287static struct of_platform_driver of_fun_driver = {
288 .name = "fsl,upm-nand",
289 .match_table = of_fun_match,
290 .probe = fun_probe,
291 .remove = __devexit_p(fun_remove),
292};
293
294static int __init fun_module_init(void)
295{
296 return of_register_platform_driver(&of_fun_driver);
297}
298module_init(fun_module_init);
299
300static void __exit fun_module_exit(void)
301{
302 of_unregister_platform_driver(&of_fun_driver);
303}
304module_exit(fun_module_exit);
305
306MODULE_LICENSE("GPL");
307MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
308MODULE_DESCRIPTION("Driver for NAND chips working through Freescale "
309 "LocalBus User-Programmable Machine");