Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2005 |
| 4 | * 2N Telekomunikace, a.s. <www.2n.cz> |
| 5 | * Ladislav Michl <michl@2n.cz> |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
Tom Rini | abb9a04 | 2024-05-18 20:20:43 -0600 | [diff] [blame] | 8 | #include <common.h> |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 9 | #include <nand.h> |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 10 | #include <errno.h> |
Heiko Schocher | adb71df | 2016-06-07 08:55:41 +0200 | [diff] [blame] | 11 | #include <linux/mtd/concat.h> |
Tom Rini | 3bde7e2 | 2021-09-22 14:50:35 -0400 | [diff] [blame] | 12 | #include <linux/mtd/rawnand.h> |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 13 | |
Tom Rini | b421349 | 2022-11-12 17:36:51 -0500 | [diff] [blame] | 14 | #ifndef CFG_SYS_NAND_BASE_LIST |
| 15 | #define CFG_SYS_NAND_BASE_LIST { CFG_SYS_NAND_BASE } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 16 | #endif |
| 17 | |
| 18 | int nand_curr_device = -1; |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 19 | |
Grygorii Strashko | aa5528b | 2017-06-26 19:13:08 -0500 | [diff] [blame] | 20 | static struct mtd_info *nand_info[CONFIG_SYS_MAX_NAND_DEVICE]; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 21 | |
Roger Quadros | bdd58e2 | 2022-09-28 14:42:35 +0300 | [diff] [blame] | 22 | #if !CONFIG_IS_ENABLED(SYS_NAND_SELF_INIT) |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 23 | static struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE]; |
Tom Rini | b421349 | 2022-11-12 17:36:51 -0500 | [diff] [blame] | 24 | static ulong base_address[CONFIG_SYS_MAX_NAND_DEVICE] = CFG_SYS_NAND_BASE_LIST; |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 25 | #endif |
| 26 | |
| 27 | static char dev_name[CONFIG_SYS_MAX_NAND_DEVICE][8]; |
| 28 | |
| 29 | static unsigned long total_nand_size; /* in kiB */ |
| 30 | |
Mugunthan V N | 7b670cc | 2017-06-26 19:12:51 -0500 | [diff] [blame] | 31 | struct mtd_info *get_nand_dev_by_index(int dev) |
| 32 | { |
| 33 | if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[dev] || |
| 34 | !nand_info[dev]->name) |
| 35 | return NULL; |
| 36 | |
| 37 | return nand_info[dev]; |
| 38 | } |
| 39 | |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 40 | int nand_mtd_to_devnum(struct mtd_info *mtd) |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 41 | { |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 42 | int i; |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 43 | |
Dario Binacchi | 968d988 | 2023-11-02 12:38:22 +0100 | [diff] [blame] | 44 | if (!mtd) |
| 45 | return -ENODEV; |
| 46 | |
Mugunthan V N | 7b670cc | 2017-06-26 19:12:51 -0500 | [diff] [blame] | 47 | for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) { |
Dario Binacchi | 968d988 | 2023-11-02 12:38:22 +0100 | [diff] [blame] | 48 | if (get_nand_dev_by_index(i) == mtd) |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 49 | return i; |
| 50 | } |
| 51 | |
| 52 | return -ENODEV; |
| 53 | } |
| 54 | |
| 55 | /* Register an initialized NAND mtd device with the U-Boot NAND command. */ |
| 56 | int nand_register(int devnum, struct mtd_info *mtd) |
| 57 | { |
Dario Binacchi | b11dec5 | 2023-11-02 12:27:33 +0100 | [diff] [blame] | 58 | if (!mtd || devnum >= CONFIG_SYS_MAX_NAND_DEVICE) |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 59 | return -EINVAL; |
| 60 | |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 61 | nand_info[devnum] = mtd; |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 62 | |
| 63 | sprintf(dev_name[devnum], "nand%d", devnum); |
| 64 | mtd->name = dev_name[devnum]; |
| 65 | |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 66 | /* |
| 67 | * Add MTD device so that we can reference it later |
| 68 | * via the mtdcore infrastructure (e.g. ubi). |
| 69 | */ |
| 70 | add_mtd_device(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 71 | |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 72 | total_nand_size += mtd->size / 1024; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 73 | |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 74 | if (nand_curr_device == -1) |
| 75 | nand_curr_device = devnum; |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
Sean Anderson | 96fc205 | 2023-11-04 16:37:49 -0400 | [diff] [blame] | 80 | void nand_unregister(struct mtd_info *mtd) |
| 81 | { |
| 82 | int devnum = nand_mtd_to_devnum(mtd); |
| 83 | |
| 84 | if (devnum < 0) |
| 85 | return; |
| 86 | |
| 87 | if (nand_curr_device == devnum) |
| 88 | nand_curr_device = -1; |
| 89 | |
| 90 | total_nand_size -= mtd->size / 1024; |
| 91 | |
| 92 | del_mtd_device(nand_info[devnum]); |
| 93 | |
| 94 | nand_info[devnum] = NULL; |
| 95 | } |
| 96 | |
Tom Rini | 9837245 | 2021-12-12 22:12:36 -0500 | [diff] [blame] | 97 | #if !CONFIG_IS_ENABLED(SYS_NAND_SELF_INIT) |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 98 | static void nand_init_chip(int i) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 99 | { |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 100 | struct nand_chip *nand = &nand_chip[i]; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 101 | struct mtd_info *mtd = nand_to_mtd(nand); |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 102 | ulong base_addr = base_address[i]; |
Wolfgang Grandegger | b325d7e | 2009-02-11 18:38:20 +0100 | [diff] [blame] | 103 | int maxchips = CONFIG_SYS_NAND_MAX_CHIPS; |
| 104 | |
| 105 | if (maxchips < 1) |
| 106 | maxchips = 1; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 107 | |
Bartlomiej Sieka | 6eed2ab | 2006-02-24 09:37:22 +0100 | [diff] [blame] | 108 | nand->IO_ADDR_R = nand->IO_ADDR_W = (void __iomem *)base_addr; |
Stefan Roese | 4157c63 | 2009-04-24 15:59:35 +0200 | [diff] [blame] | 109 | |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 110 | if (board_nand_init(nand)) |
| 111 | return; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 112 | |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 113 | if (nand_scan(mtd, maxchips)) |
| 114 | return; |
| 115 | |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 116 | nand_register(i, mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 117 | } |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 118 | #endif |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 119 | |
Heiko Schocher | adb71df | 2016-06-07 08:55:41 +0200 | [diff] [blame] | 120 | #ifdef CONFIG_MTD_CONCAT |
Sean Anderson | 28e2eaf | 2023-11-04 16:37:50 -0400 | [diff] [blame] | 121 | struct mtd_info *concat_mtd; |
| 122 | |
Heiko Schocher | adb71df | 2016-06-07 08:55:41 +0200 | [diff] [blame] | 123 | static void create_mtd_concat(void) |
| 124 | { |
| 125 | struct mtd_info *nand_info_list[CONFIG_SYS_MAX_NAND_DEVICE]; |
| 126 | int nand_devices_found = 0; |
| 127 | int i; |
| 128 | |
| 129 | for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) { |
Mugunthan V N | 7b670cc | 2017-06-26 19:12:51 -0500 | [diff] [blame] | 130 | struct mtd_info *mtd = get_nand_dev_by_index(i); |
| 131 | if (mtd != NULL) { |
| 132 | nand_info_list[nand_devices_found] = mtd; |
Heiko Schocher | adb71df | 2016-06-07 08:55:41 +0200 | [diff] [blame] | 133 | nand_devices_found++; |
| 134 | } |
| 135 | } |
| 136 | if (nand_devices_found > 1) { |
Heiko Schocher | adb71df | 2016-06-07 08:55:41 +0200 | [diff] [blame] | 137 | char c_mtd_name[16]; |
| 138 | |
| 139 | /* |
| 140 | * We detected multiple devices. Concatenate them together. |
| 141 | */ |
| 142 | sprintf(c_mtd_name, "nand%d", nand_devices_found); |
Sean Anderson | 28e2eaf | 2023-11-04 16:37:50 -0400 | [diff] [blame] | 143 | concat_mtd = mtd_concat_create(nand_info_list, |
| 144 | nand_devices_found, c_mtd_name); |
Heiko Schocher | adb71df | 2016-06-07 08:55:41 +0200 | [diff] [blame] | 145 | |
Sean Anderson | 28e2eaf | 2023-11-04 16:37:50 -0400 | [diff] [blame] | 146 | if (!concat_mtd) |
Heiko Schocher | adb71df | 2016-06-07 08:55:41 +0200 | [diff] [blame] | 147 | return; |
| 148 | |
Sean Anderson | 28e2eaf | 2023-11-04 16:37:50 -0400 | [diff] [blame] | 149 | nand_register(nand_devices_found, concat_mtd); |
Heiko Schocher | adb71df | 2016-06-07 08:55:41 +0200 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | return; |
| 153 | } |
Sean Anderson | 28e2eaf | 2023-11-04 16:37:50 -0400 | [diff] [blame] | 154 | |
| 155 | static void destroy_mtd_concat(void) |
| 156 | { |
| 157 | if (!concat_mtd) |
| 158 | return; |
| 159 | |
| 160 | mtd_concat_destroy(concat_mtd); |
| 161 | concat_mtd = NULL; |
| 162 | } |
Heiko Schocher | adb71df | 2016-06-07 08:55:41 +0200 | [diff] [blame] | 163 | #else |
| 164 | static void create_mtd_concat(void) |
| 165 | { |
| 166 | } |
Sean Anderson | 28e2eaf | 2023-11-04 16:37:50 -0400 | [diff] [blame] | 167 | |
| 168 | static void destroy_mtd_concat(void) |
| 169 | { |
| 170 | } |
Heiko Schocher | adb71df | 2016-06-07 08:55:41 +0200 | [diff] [blame] | 171 | #endif |
| 172 | |
Hou Zhiqiang | 9c8b3c7 | 2017-03-17 16:12:31 +0800 | [diff] [blame] | 173 | unsigned long nand_size(void) |
| 174 | { |
| 175 | return total_nand_size; |
| 176 | } |
| 177 | |
Sean Anderson | 28e2eaf | 2023-11-04 16:37:50 -0400 | [diff] [blame] | 178 | static int initialized; |
| 179 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 180 | void nand_init(void) |
| 181 | { |
Hou Zhiqiang | 5dac9d8 | 2017-03-17 16:12:30 +0800 | [diff] [blame] | 182 | /* |
| 183 | * Avoid initializing NAND Flash multiple times, |
| 184 | * otherwise it will calculate a wrong total size. |
| 185 | */ |
| 186 | if (initialized) |
| 187 | return; |
| 188 | initialized = 1; |
| 189 | |
Tom Rini | 9837245 | 2021-12-12 22:12:36 -0500 | [diff] [blame] | 190 | #if CONFIG_IS_ENABLED(SYS_NAND_SELF_INIT) |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 191 | board_nand_init(); |
| 192 | #else |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 193 | int i; |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 194 | |
| 195 | for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) |
| 196 | nand_init_chip(i); |
| 197 | #endif |
| 198 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 199 | #ifdef CONFIG_SYS_NAND_SELECT_DEVICE |
Stefan Roese | 3cdd3fd | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 200 | /* |
| 201 | * Select the chip in the board/cpu specific driver |
| 202 | */ |
Mugunthan V N | 7b670cc | 2017-06-26 19:12:51 -0500 | [diff] [blame] | 203 | board_nand_select_device(mtd_to_nand(get_nand_dev_by_index(nand_curr_device)), |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 204 | nand_curr_device); |
Stefan Roese | 3cdd3fd | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 205 | #endif |
Heiko Schocher | adb71df | 2016-06-07 08:55:41 +0200 | [diff] [blame] | 206 | |
| 207 | create_mtd_concat(); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 208 | } |
Sean Anderson | 8805f9d | 2023-11-04 16:37:44 -0400 | [diff] [blame] | 209 | |
Sean Anderson | 28e2eaf | 2023-11-04 16:37:50 -0400 | [diff] [blame] | 210 | void nand_reinit(void) |
| 211 | { |
| 212 | int i; |
| 213 | |
| 214 | destroy_mtd_concat(); |
| 215 | for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) |
| 216 | assert(!nand_info[i]); |
| 217 | |
| 218 | initialized = 0; |
| 219 | nand_init(); |
| 220 | } |
| 221 | |
Sean Anderson | 8805f9d | 2023-11-04 16:37:44 -0400 | [diff] [blame] | 222 | unsigned int nand_page_size(void) |
| 223 | { |
| 224 | struct mtd_info *mtd = get_nand_dev_by_index(nand_curr_device); |
| 225 | |
| 226 | return mtd ? mtd->writesize : 1; |
| 227 | } |