OpenWrt – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /* |
2 | * NAND flash driver for the MikroTik RouterBoard 4xx series |
||
3 | * |
||
4 | * Copyright (C) 2008-2011 Gabor Juhos <juhosg@openwrt.org> |
||
5 | * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org> |
||
6 | * |
||
7 | * This file was based on the driver for Linux 2.6.22 published by |
||
8 | * MikroTik for their RouterBoard 4xx series devices. |
||
9 | * |
||
10 | * This program is free software; you can redistribute it and/or modify it |
||
11 | * under the terms of the GNU General Public License version 2 as published |
||
12 | * by the Free Software Foundation. |
||
13 | */ |
||
14 | |||
15 | #include <linux/version.h> |
||
16 | #include <linux/kernel.h> |
||
17 | #include <linux/module.h> |
||
18 | #include <linux/init.h> |
||
19 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0) |
||
20 | #include <linux/mtd/nand.h> |
||
21 | #else |
||
22 | #include <linux/mtd/rawnand.h> |
||
23 | #endif |
||
24 | #include <linux/mtd/mtd.h> |
||
25 | #include <linux/mtd/partitions.h> |
||
26 | #include <linux/platform_device.h> |
||
27 | #include <linux/delay.h> |
||
28 | #include <linux/io.h> |
||
29 | #include <linux/gpio.h> |
||
30 | #include <linux/slab.h> |
||
31 | |||
32 | #include <asm/mach-ath79/ath79.h> |
||
33 | #include <asm/mach-ath79/rb4xx_cpld.h> |
||
34 | |||
35 | #define DRV_NAME "rb4xx-nand" |
||
36 | #define DRV_VERSION "0.2.0" |
||
37 | #define DRV_DESC "NAND flash driver for RouterBoard 4xx series" |
||
38 | |||
39 | #define RB4XX_NAND_GPIO_READY 5 |
||
40 | #define RB4XX_NAND_GPIO_ALE 37 |
||
41 | #define RB4XX_NAND_GPIO_CLE 38 |
||
42 | #define RB4XX_NAND_GPIO_NCE 39 |
||
43 | |||
44 | struct rb4xx_nand_info { |
||
45 | struct nand_chip chip; |
||
46 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0) |
||
47 | struct mtd_info mtd; |
||
48 | #endif |
||
49 | }; |
||
50 | |||
51 | static inline struct rb4xx_nand_info *mtd_to_rbinfo(struct mtd_info *mtd) |
||
52 | { |
||
53 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0) |
||
54 | return container_of(mtd, struct rb4xx_nand_info, mtd); |
||
55 | #else |
||
56 | struct nand_chip *chip = mtd_to_nand(mtd); |
||
57 | |||
58 | return container_of(chip, struct rb4xx_nand_info, chip); |
||
59 | #endif |
||
60 | } |
||
61 | |||
62 | static struct mtd_info *rbinfo_to_mtd(struct rb4xx_nand_info *nfc) |
||
63 | { |
||
64 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0) |
||
65 | return &nfc->mtd; |
||
66 | #else |
||
67 | return nand_to_mtd(&nfc->chip); |
||
68 | #endif |
||
69 | } |
||
70 | |||
71 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0) |
||
72 | /* |
||
73 | * We need to use the OLD Yaffs-1 OOB layout, otherwise the RB bootloader |
||
74 | * will not be able to find the kernel that we load. |
||
75 | */ |
||
76 | static struct nand_ecclayout rb4xx_nand_ecclayout = { |
||
77 | .eccbytes = 6, |
||
78 | .eccpos = { 8, 9, 10, 13, 14, 15 }, |
||
79 | .oobavail = 9, |
||
80 | .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } } |
||
81 | }; |
||
82 | |||
83 | #else |
||
84 | |||
85 | static int rb4xx_ooblayout_ecc(struct mtd_info *mtd, int section, |
||
86 | struct mtd_oob_region *oobregion) |
||
87 | { |
||
88 | switch (section) { |
||
89 | case 0: |
||
90 | oobregion->offset = 8; |
||
91 | oobregion->length = 3; |
||
92 | return 0; |
||
93 | case 1: |
||
94 | oobregion->offset = 13; |
||
95 | oobregion->length = 3; |
||
96 | return 0; |
||
97 | default: |
||
98 | return -ERANGE; |
||
99 | } |
||
100 | } |
||
101 | |||
102 | static int rb4xx_ooblayout_free(struct mtd_info *mtd, int section, |
||
103 | struct mtd_oob_region *oobregion) |
||
104 | { |
||
105 | switch (section) { |
||
106 | case 0: |
||
107 | oobregion->offset = 0; |
||
108 | oobregion->length = 4; |
||
109 | return 0; |
||
110 | case 1: |
||
111 | oobregion->offset = 4; |
||
112 | oobregion->length = 1; |
||
113 | return 0; |
||
114 | case 2: |
||
115 | oobregion->offset = 6; |
||
116 | oobregion->length = 2; |
||
117 | return 0; |
||
118 | case 3: |
||
119 | oobregion->offset = 11; |
||
120 | oobregion->length = 2; |
||
121 | return 0; |
||
122 | default: |
||
123 | return -ERANGE; |
||
124 | } |
||
125 | } |
||
126 | |||
127 | static const struct mtd_ooblayout_ops rb4xx_nand_ecclayout_ops = { |
||
128 | .ecc = rb4xx_ooblayout_ecc, |
||
129 | .free = rb4xx_ooblayout_free, |
||
130 | }; |
||
131 | #endif /* < 4.6 */ |
||
132 | |||
133 | static struct mtd_partition rb4xx_nand_partitions[] = { |
||
134 | { |
||
135 | .name = "booter", |
||
136 | .offset = 0, |
||
137 | .size = (256 * 1024), |
||
138 | .mask_flags = MTD_WRITEABLE, |
||
139 | }, |
||
140 | { |
||
141 | .name = "kernel", |
||
142 | .offset = (256 * 1024), |
||
143 | .size = (4 * 1024 * 1024) - (256 * 1024), |
||
144 | }, |
||
145 | { |
||
146 | .name = "ubi", |
||
147 | .offset = MTDPART_OFS_NXTBLK, |
||
148 | .size = MTDPART_SIZ_FULL, |
||
149 | }, |
||
150 | }; |
||
151 | |||
152 | static int rb4xx_nand_dev_ready(struct mtd_info *mtd) |
||
153 | { |
||
154 | return gpio_get_value_cansleep(RB4XX_NAND_GPIO_READY); |
||
155 | } |
||
156 | |||
157 | static void rb4xx_nand_write_cmd(unsigned char cmd) |
||
158 | { |
||
159 | unsigned char data = cmd; |
||
160 | int err; |
||
161 | |||
162 | err = rb4xx_cpld_write(&data, 1); |
||
163 | if (err) |
||
164 | pr_err("rb4xx_nand: write cmd failed, err=%d\n", err); |
||
165 | } |
||
166 | |||
167 | static void rb4xx_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, |
||
168 | unsigned int ctrl) |
||
169 | { |
||
170 | if (ctrl & NAND_CTRL_CHANGE) { |
||
171 | gpio_set_value_cansleep(RB4XX_NAND_GPIO_CLE, |
||
172 | (ctrl & NAND_CLE) ? 1 : 0); |
||
173 | gpio_set_value_cansleep(RB4XX_NAND_GPIO_ALE, |
||
174 | (ctrl & NAND_ALE) ? 1 : 0); |
||
175 | gpio_set_value_cansleep(RB4XX_NAND_GPIO_NCE, |
||
176 | (ctrl & NAND_NCE) ? 0 : 1); |
||
177 | } |
||
178 | |||
179 | if (cmd != NAND_CMD_NONE) |
||
180 | rb4xx_nand_write_cmd(cmd); |
||
181 | } |
||
182 | |||
183 | static unsigned char rb4xx_nand_read_byte(struct mtd_info *mtd) |
||
184 | { |
||
185 | unsigned char data = 0; |
||
186 | int err; |
||
187 | |||
188 | err = rb4xx_cpld_read(&data, 1); |
||
189 | if (err) { |
||
190 | pr_err("rb4xx_nand: read data failed, err=%d\n", err); |
||
191 | data = 0xff; |
||
192 | } |
||
193 | |||
194 | return data; |
||
195 | } |
||
196 | |||
197 | static void rb4xx_nand_write_buf(struct mtd_info *mtd, const unsigned char *buf, |
||
198 | int len) |
||
199 | { |
||
200 | int err; |
||
201 | |||
202 | err = rb4xx_cpld_write(buf, len); |
||
203 | if (err) |
||
204 | pr_err("rb4xx_nand: write buf failed, err=%d\n", err); |
||
205 | } |
||
206 | |||
207 | static void rb4xx_nand_read_buf(struct mtd_info *mtd, unsigned char *buf, |
||
208 | int len) |
||
209 | { |
||
210 | int err; |
||
211 | |||
212 | err = rb4xx_cpld_read(buf, len); |
||
213 | if (err) |
||
214 | pr_err("rb4xx_nand: read buf failed, err=%d\n", err); |
||
215 | } |
||
216 | |||
217 | static int rb4xx_nand_probe(struct platform_device *pdev) |
||
218 | { |
||
219 | struct rb4xx_nand_info *info; |
||
220 | struct mtd_info *mtd; |
||
221 | int ret; |
||
222 | |||
223 | printk(KERN_INFO DRV_DESC " version " DRV_VERSION "\n"); |
||
224 | |||
225 | ret = gpio_request(RB4XX_NAND_GPIO_READY, "NAND RDY"); |
||
226 | if (ret) { |
||
227 | dev_err(&pdev->dev, "unable to request gpio %d\n", |
||
228 | RB4XX_NAND_GPIO_READY); |
||
229 | goto err; |
||
230 | } |
||
231 | |||
232 | ret = gpio_direction_input(RB4XX_NAND_GPIO_READY); |
||
233 | if (ret) { |
||
234 | dev_err(&pdev->dev, "unable to set input mode on gpio %d\n", |
||
235 | RB4XX_NAND_GPIO_READY); |
||
236 | goto err_free_gpio_ready; |
||
237 | } |
||
238 | |||
239 | ret = gpio_request(RB4XX_NAND_GPIO_ALE, "NAND ALE"); |
||
240 | if (ret) { |
||
241 | dev_err(&pdev->dev, "unable to request gpio %d\n", |
||
242 | RB4XX_NAND_GPIO_ALE); |
||
243 | goto err_free_gpio_ready; |
||
244 | } |
||
245 | |||
246 | ret = gpio_direction_output(RB4XX_NAND_GPIO_ALE, 0); |
||
247 | if (ret) { |
||
248 | dev_err(&pdev->dev, "unable to set output mode on gpio %d\n", |
||
249 | RB4XX_NAND_GPIO_ALE); |
||
250 | goto err_free_gpio_ale; |
||
251 | } |
||
252 | |||
253 | ret = gpio_request(RB4XX_NAND_GPIO_CLE, "NAND CLE"); |
||
254 | if (ret) { |
||
255 | dev_err(&pdev->dev, "unable to request gpio %d\n", |
||
256 | RB4XX_NAND_GPIO_CLE); |
||
257 | goto err_free_gpio_ale; |
||
258 | } |
||
259 | |||
260 | ret = gpio_direction_output(RB4XX_NAND_GPIO_CLE, 0); |
||
261 | if (ret) { |
||
262 | dev_err(&pdev->dev, "unable to set output mode on gpio %d\n", |
||
263 | RB4XX_NAND_GPIO_CLE); |
||
264 | goto err_free_gpio_cle; |
||
265 | } |
||
266 | |||
267 | ret = gpio_request(RB4XX_NAND_GPIO_NCE, "NAND NCE"); |
||
268 | if (ret) { |
||
269 | dev_err(&pdev->dev, "unable to request gpio %d\n", |
||
270 | RB4XX_NAND_GPIO_NCE); |
||
271 | goto err_free_gpio_cle; |
||
272 | } |
||
273 | |||
274 | ret = gpio_direction_output(RB4XX_NAND_GPIO_NCE, 1); |
||
275 | if (ret) { |
||
276 | dev_err(&pdev->dev, "unable to set output mode on gpio %d\n", |
||
277 | RB4XX_NAND_GPIO_ALE); |
||
278 | goto err_free_gpio_nce; |
||
279 | } |
||
280 | |||
281 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
||
282 | if (!info) { |
||
283 | dev_err(&pdev->dev, "rb4xx-nand: no memory for private data\n"); |
||
284 | ret = -ENOMEM; |
||
285 | goto err_free_gpio_nce; |
||
286 | } |
||
287 | |||
288 | info->chip.priv = &info; |
||
289 | mtd = rbinfo_to_mtd(info); |
||
290 | |||
291 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0) |
||
292 | mtd->priv = &info->chip; |
||
293 | #endif |
||
294 | mtd->owner = THIS_MODULE; |
||
295 | |||
296 | info->chip.cmd_ctrl = rb4xx_nand_cmd_ctrl; |
||
297 | info->chip.dev_ready = rb4xx_nand_dev_ready; |
||
298 | info->chip.read_byte = rb4xx_nand_read_byte; |
||
299 | info->chip.write_buf = rb4xx_nand_write_buf; |
||
300 | info->chip.read_buf = rb4xx_nand_read_buf; |
||
301 | |||
302 | info->chip.chip_delay = 25; |
||
303 | info->chip.ecc.mode = NAND_ECC_SOFT; |
||
304 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4,6,0) |
||
305 | info->chip.ecc.algo = NAND_ECC_HAMMING; |
||
306 | #endif |
||
307 | info->chip.options = NAND_NO_SUBPAGE_WRITE; |
||
308 | |||
309 | platform_set_drvdata(pdev, info); |
||
310 | |||
311 | ret = nand_scan_ident(mtd, 1, NULL); |
||
312 | if (ret) { |
||
313 | ret = -ENXIO; |
||
314 | goto err_free_info; |
||
315 | } |
||
316 | |||
317 | if (mtd->writesize == 512) |
||
318 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0) |
||
319 | info->chip.ecc.layout = &rb4xx_nand_ecclayout; |
||
320 | #else |
||
321 | mtd_set_ooblayout(mtd, &rb4xx_nand_ecclayout_ops); |
||
322 | #endif |
||
323 | |||
324 | ret = nand_scan_tail(mtd); |
||
325 | if (ret) { |
||
326 | return -ENXIO; |
||
327 | goto err_set_drvdata; |
||
328 | } |
||
329 | |||
330 | mtd_device_register(mtd, rb4xx_nand_partitions, |
||
331 | ARRAY_SIZE(rb4xx_nand_partitions)); |
||
332 | if (ret) |
||
333 | goto err_release_nand; |
||
334 | |||
335 | return 0; |
||
336 | |||
337 | err_release_nand: |
||
338 | nand_release(mtd); |
||
339 | err_set_drvdata: |
||
340 | platform_set_drvdata(pdev, NULL); |
||
341 | err_free_info: |
||
342 | kfree(info); |
||
343 | err_free_gpio_nce: |
||
344 | gpio_free(RB4XX_NAND_GPIO_NCE); |
||
345 | err_free_gpio_cle: |
||
346 | gpio_free(RB4XX_NAND_GPIO_CLE); |
||
347 | err_free_gpio_ale: |
||
348 | gpio_free(RB4XX_NAND_GPIO_ALE); |
||
349 | err_free_gpio_ready: |
||
350 | gpio_free(RB4XX_NAND_GPIO_READY); |
||
351 | err: |
||
352 | return ret; |
||
353 | } |
||
354 | |||
355 | static int rb4xx_nand_remove(struct platform_device *pdev) |
||
356 | { |
||
357 | struct rb4xx_nand_info *info = platform_get_drvdata(pdev); |
||
358 | |||
359 | nand_release(rbinfo_to_mtd(info)); |
||
360 | platform_set_drvdata(pdev, NULL); |
||
361 | kfree(info); |
||
362 | gpio_free(RB4XX_NAND_GPIO_NCE); |
||
363 | gpio_free(RB4XX_NAND_GPIO_CLE); |
||
364 | gpio_free(RB4XX_NAND_GPIO_ALE); |
||
365 | gpio_free(RB4XX_NAND_GPIO_READY); |
||
366 | |||
367 | return 0; |
||
368 | } |
||
369 | |||
370 | static struct platform_driver rb4xx_nand_driver = { |
||
371 | .probe = rb4xx_nand_probe, |
||
372 | .remove = rb4xx_nand_remove, |
||
373 | .driver = { |
||
374 | .name = DRV_NAME, |
||
375 | .owner = THIS_MODULE, |
||
376 | }, |
||
377 | }; |
||
378 | |||
379 | static int __init rb4xx_nand_init(void) |
||
380 | { |
||
381 | return platform_driver_register(&rb4xx_nand_driver); |
||
382 | } |
||
383 | |||
384 | static void __exit rb4xx_nand_exit(void) |
||
385 | { |
||
386 | platform_driver_unregister(&rb4xx_nand_driver); |
||
387 | } |
||
388 | |||
389 | module_init(rb4xx_nand_init); |
||
390 | module_exit(rb4xx_nand_exit); |
||
391 | |||
392 | MODULE_DESCRIPTION(DRV_DESC); |
||
393 | MODULE_VERSION(DRV_VERSION); |
||
394 | MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>"); |
||
395 | MODULE_AUTHOR("Imre Kaloz <kaloz@openwrt.org>"); |
||
396 | MODULE_LICENSE("GPL v2"); |