1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Freescale UPM NAND driver.
5 * Copyright © 2007-2008 MontaVista Software, Inc.
7 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/delay.h>
13 #include <linux/mtd/rawnand.h>
14 #include <linux/mtd/nand_ecc.h>
15 #include <linux/mtd/partitions.h>
16 #include <linux/mtd/mtd.h>
17 #include <linux/of_address.h>
18 #include <linux/of_platform.h>
19 #include <linux/of_gpio.h>
21 #include <linux/slab.h>
22 #include <asm/fsl_lbc.h>
24 #define FSL_UPM_WAIT_RUN_PATTERN 0x1
25 #define FSL_UPM_WAIT_WRITE_BYTE 0x2
26 #define FSL_UPM_WAIT_WRITE_BUFFER 0x4
30 struct nand_chip chip
;
32 struct mtd_partition
*parts
;
34 uint8_t upm_addr_offset
;
35 uint8_t upm_cmd_offset
;
36 void __iomem
*io_base
;
37 int rnb_gpio
[NAND_MAX_CHIPS
];
38 uint32_t mchip_offsets
[NAND_MAX_CHIPS
];
40 uint32_t mchip_number
;
45 static inline struct fsl_upm_nand
*to_fsl_upm_nand(struct mtd_info
*mtdinfo
)
47 return container_of(mtd_to_nand(mtdinfo
), struct fsl_upm_nand
,
51 static int fun_chip_ready(struct nand_chip
*chip
)
53 struct fsl_upm_nand
*fun
= to_fsl_upm_nand(nand_to_mtd(chip
));
55 if (gpio_get_value(fun
->rnb_gpio
[fun
->mchip_number
]))
58 dev_vdbg(fun
->dev
, "busy\n");
62 static void fun_wait_rnb(struct fsl_upm_nand
*fun
)
64 if (fun
->rnb_gpio
[fun
->mchip_number
] >= 0) {
65 struct mtd_info
*mtd
= nand_to_mtd(&fun
->chip
);
68 while (--cnt
&& !fun_chip_ready(&fun
->chip
))
71 dev_err(fun
->dev
, "tired waiting for RNB\n");
77 static void fun_cmd_ctrl(struct nand_chip
*chip
, int cmd
, unsigned int ctrl
)
79 struct fsl_upm_nand
*fun
= to_fsl_upm_nand(nand_to_mtd(chip
));
82 if (!(ctrl
& fun
->last_ctrl
)) {
83 fsl_upm_end_pattern(&fun
->upm
);
85 if (cmd
== NAND_CMD_NONE
)
88 fun
->last_ctrl
= ctrl
& (NAND_ALE
| NAND_CLE
);
91 if (ctrl
& NAND_CTRL_CHANGE
) {
93 fsl_upm_start_pattern(&fun
->upm
, fun
->upm_addr_offset
);
94 else if (ctrl
& NAND_CLE
)
95 fsl_upm_start_pattern(&fun
->upm
, fun
->upm_cmd_offset
);
98 mar
= (cmd
<< (32 - fun
->upm
.width
)) |
99 fun
->mchip_offsets
[fun
->mchip_number
];
100 fsl_upm_run_pattern(&fun
->upm
, chip
->legacy
.IO_ADDR_R
, mar
);
102 if (fun
->wait_flags
& FSL_UPM_WAIT_RUN_PATTERN
)
106 static void fun_select_chip(struct nand_chip
*chip
, int mchip_nr
)
108 struct fsl_upm_nand
*fun
= to_fsl_upm_nand(nand_to_mtd(chip
));
110 if (mchip_nr
== -1) {
111 chip
->legacy
.cmd_ctrl(chip
, NAND_CMD_NONE
, 0 | NAND_CTRL_CHANGE
);
112 } else if (mchip_nr
>= 0 && mchip_nr
< NAND_MAX_CHIPS
) {
113 fun
->mchip_number
= mchip_nr
;
114 chip
->legacy
.IO_ADDR_R
= fun
->io_base
+ fun
->mchip_offsets
[mchip_nr
];
115 chip
->legacy
.IO_ADDR_W
= chip
->legacy
.IO_ADDR_R
;
121 static uint8_t fun_read_byte(struct nand_chip
*chip
)
123 struct fsl_upm_nand
*fun
= to_fsl_upm_nand(nand_to_mtd(chip
));
125 return in_8(fun
->chip
.legacy
.IO_ADDR_R
);
128 static void fun_read_buf(struct nand_chip
*chip
, uint8_t *buf
, int len
)
130 struct fsl_upm_nand
*fun
= to_fsl_upm_nand(nand_to_mtd(chip
));
133 for (i
= 0; i
< len
; i
++)
134 buf
[i
] = in_8(fun
->chip
.legacy
.IO_ADDR_R
);
137 static void fun_write_buf(struct nand_chip
*chip
, const uint8_t *buf
, int len
)
139 struct fsl_upm_nand
*fun
= to_fsl_upm_nand(nand_to_mtd(chip
));
142 for (i
= 0; i
< len
; i
++) {
143 out_8(fun
->chip
.legacy
.IO_ADDR_W
, buf
[i
]);
144 if (fun
->wait_flags
& FSL_UPM_WAIT_WRITE_BYTE
)
147 if (fun
->wait_flags
& FSL_UPM_WAIT_WRITE_BUFFER
)
151 static int fun_chip_init(struct fsl_upm_nand
*fun
,
152 const struct device_node
*upm_np
,
153 const struct resource
*io_res
)
155 struct mtd_info
*mtd
= nand_to_mtd(&fun
->chip
);
157 struct device_node
*flash_np
;
159 fun
->chip
.legacy
.IO_ADDR_R
= fun
->io_base
;
160 fun
->chip
.legacy
.IO_ADDR_W
= fun
->io_base
;
161 fun
->chip
.legacy
.cmd_ctrl
= fun_cmd_ctrl
;
162 fun
->chip
.legacy
.chip_delay
= fun
->chip_delay
;
163 fun
->chip
.legacy
.read_byte
= fun_read_byte
;
164 fun
->chip
.legacy
.read_buf
= fun_read_buf
;
165 fun
->chip
.legacy
.write_buf
= fun_write_buf
;
166 fun
->chip
.ecc
.mode
= NAND_ECC_SOFT
;
167 fun
->chip
.ecc
.algo
= NAND_ECC_HAMMING
;
168 if (fun
->mchip_count
> 1)
169 fun
->chip
.legacy
.select_chip
= fun_select_chip
;
171 if (fun
->rnb_gpio
[0] >= 0)
172 fun
->chip
.legacy
.dev_ready
= fun_chip_ready
;
174 mtd
->dev
.parent
= fun
->dev
;
176 flash_np
= of_get_next_child(upm_np
, NULL
);
180 nand_set_flash_node(&fun
->chip
, flash_np
);
181 mtd
->name
= kasprintf(GFP_KERNEL
, "0x%llx.%pOFn", (u64
)io_res
->start
,
188 ret
= nand_scan(&fun
->chip
, fun
->mchip_count
);
192 ret
= mtd_device_register(mtd
, NULL
, 0);
194 of_node_put(flash_np
);
200 static int fun_probe(struct platform_device
*ofdev
)
202 struct fsl_upm_nand
*fun
;
203 struct resource io_res
;
210 fun
= kzalloc(sizeof(*fun
), GFP_KERNEL
);
214 ret
= of_address_to_resource(ofdev
->dev
.of_node
, 0, &io_res
);
216 dev_err(&ofdev
->dev
, "can't get IO base\n");
220 ret
= fsl_upm_find(io_res
.start
, &fun
->upm
);
222 dev_err(&ofdev
->dev
, "can't find UPM\n");
226 prop
= of_get_property(ofdev
->dev
.of_node
, "fsl,upm-addr-offset",
228 if (!prop
|| size
!= sizeof(uint32_t)) {
229 dev_err(&ofdev
->dev
, "can't get UPM address offset\n");
233 fun
->upm_addr_offset
= *prop
;
235 prop
= of_get_property(ofdev
->dev
.of_node
, "fsl,upm-cmd-offset", &size
);
236 if (!prop
|| size
!= sizeof(uint32_t)) {
237 dev_err(&ofdev
->dev
, "can't get UPM command offset\n");
241 fun
->upm_cmd_offset
= *prop
;
243 prop
= of_get_property(ofdev
->dev
.of_node
,
244 "fsl,upm-addr-line-cs-offsets", &size
);
245 if (prop
&& (size
/ sizeof(uint32_t)) > 0) {
246 fun
->mchip_count
= size
/ sizeof(uint32_t);
247 if (fun
->mchip_count
>= NAND_MAX_CHIPS
) {
248 dev_err(&ofdev
->dev
, "too much multiple chips\n");
251 for (i
= 0; i
< fun
->mchip_count
; i
++)
252 fun
->mchip_offsets
[i
] = be32_to_cpu(prop
[i
]);
254 fun
->mchip_count
= 1;
257 for (i
= 0; i
< fun
->mchip_count
; i
++) {
258 fun
->rnb_gpio
[i
] = -1;
259 rnb_gpio
= of_get_gpio(ofdev
->dev
.of_node
, i
);
261 ret
= gpio_request(rnb_gpio
, dev_name(&ofdev
->dev
));
264 "can't request RNB gpio #%d\n", i
);
267 gpio_direction_input(rnb_gpio
);
268 fun
->rnb_gpio
[i
] = rnb_gpio
;
269 } else if (rnb_gpio
== -EINVAL
) {
270 dev_err(&ofdev
->dev
, "RNB gpio #%d is invalid\n", i
);
275 prop
= of_get_property(ofdev
->dev
.of_node
, "chip-delay", NULL
);
277 fun
->chip_delay
= be32_to_cpup(prop
);
279 fun
->chip_delay
= 50;
281 prop
= of_get_property(ofdev
->dev
.of_node
, "fsl,upm-wait-flags", &size
);
282 if (prop
&& size
== sizeof(uint32_t))
283 fun
->wait_flags
= be32_to_cpup(prop
);
285 fun
->wait_flags
= FSL_UPM_WAIT_RUN_PATTERN
|
286 FSL_UPM_WAIT_WRITE_BYTE
;
288 fun
->io_base
= devm_ioremap(&ofdev
->dev
, io_res
.start
,
289 resource_size(&io_res
));
295 fun
->dev
= &ofdev
->dev
;
296 fun
->last_ctrl
= NAND_CLE
;
298 ret
= fun_chip_init(fun
, ofdev
->dev
.of_node
, &io_res
);
302 dev_set_drvdata(&ofdev
->dev
, fun
);
306 for (i
= 0; i
< fun
->mchip_count
; i
++) {
307 if (fun
->rnb_gpio
[i
] < 0)
309 gpio_free(fun
->rnb_gpio
[i
]);
317 static int fun_remove(struct platform_device
*ofdev
)
319 struct fsl_upm_nand
*fun
= dev_get_drvdata(&ofdev
->dev
);
320 struct mtd_info
*mtd
= nand_to_mtd(&fun
->chip
);
323 nand_release(&fun
->chip
);
326 for (i
= 0; i
< fun
->mchip_count
; i
++) {
327 if (fun
->rnb_gpio
[i
] < 0)
329 gpio_free(fun
->rnb_gpio
[i
]);
337 static const struct of_device_id of_fun_match
[] = {
338 { .compatible
= "fsl,upm-nand" },
341 MODULE_DEVICE_TABLE(of
, of_fun_match
);
343 static struct platform_driver of_fun_driver
= {
345 .name
= "fsl,upm-nand",
346 .of_match_table
= of_fun_match
,
349 .remove
= fun_remove
,
352 module_platform_driver(of_fun_driver
);
354 MODULE_LICENSE("GPL");
355 MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
356 MODULE_DESCRIPTION("Driver for NAND chips working through Freescale "
357 "LocalBus User-Programmable Machine");