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 | |
| 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 | |
Mugunthan V N | 7b670cc | 2017-06-26 19:12:51 -0500 | [diff] [blame] | 44 | for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) { |
| 45 | if (mtd && get_nand_dev_by_index(i) == mtd) |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 46 | return i; |
| 47 | } |
| 48 | |
| 49 | return -ENODEV; |
| 50 | } |
| 51 | |
| 52 | /* Register an initialized NAND mtd device with the U-Boot NAND command. */ |
| 53 | int nand_register(int devnum, struct mtd_info *mtd) |
| 54 | { |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 55 | if (devnum >= CONFIG_SYS_MAX_NAND_DEVICE) |
| 56 | return -EINVAL; |
| 57 | |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 58 | nand_info[devnum] = mtd; |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 59 | |
| 60 | sprintf(dev_name[devnum], "nand%d", devnum); |
| 61 | mtd->name = dev_name[devnum]; |
| 62 | |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 63 | /* |
| 64 | * Add MTD device so that we can reference it later |
| 65 | * via the mtdcore infrastructure (e.g. ubi). |
| 66 | */ |
| 67 | add_mtd_device(mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 68 | |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 69 | total_nand_size += mtd->size / 1024; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 70 | |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 71 | if (nand_curr_device == -1) |
| 72 | nand_curr_device = devnum; |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
Sean Anderson | 96fc205 | 2023-11-04 16:37:49 -0400 | [diff] [blame^] | 77 | void nand_unregister(struct mtd_info *mtd) |
| 78 | { |
| 79 | int devnum = nand_mtd_to_devnum(mtd); |
| 80 | |
| 81 | if (devnum < 0) |
| 82 | return; |
| 83 | |
| 84 | if (nand_curr_device == devnum) |
| 85 | nand_curr_device = -1; |
| 86 | |
| 87 | total_nand_size -= mtd->size / 1024; |
| 88 | |
| 89 | del_mtd_device(nand_info[devnum]); |
| 90 | |
| 91 | nand_info[devnum] = NULL; |
| 92 | } |
| 93 | |
Tom Rini | 9837245 | 2021-12-12 22:12:36 -0500 | [diff] [blame] | 94 | #if !CONFIG_IS_ENABLED(SYS_NAND_SELF_INIT) |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 95 | static void nand_init_chip(int i) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 96 | { |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 97 | struct nand_chip *nand = &nand_chip[i]; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 98 | struct mtd_info *mtd = nand_to_mtd(nand); |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 99 | ulong base_addr = base_address[i]; |
Wolfgang Grandegger | b325d7e | 2009-02-11 18:38:20 +0100 | [diff] [blame] | 100 | int maxchips = CONFIG_SYS_NAND_MAX_CHIPS; |
| 101 | |
| 102 | if (maxchips < 1) |
| 103 | maxchips = 1; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 104 | |
Bartlomiej Sieka | 6eed2ab | 2006-02-24 09:37:22 +0100 | [diff] [blame] | 105 | nand->IO_ADDR_R = nand->IO_ADDR_W = (void __iomem *)base_addr; |
Stefan Roese | 4157c63 | 2009-04-24 15:59:35 +0200 | [diff] [blame] | 106 | |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 107 | if (board_nand_init(nand)) |
| 108 | return; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 109 | |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 110 | if (nand_scan(mtd, maxchips)) |
| 111 | return; |
| 112 | |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 113 | nand_register(i, mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 114 | } |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 115 | #endif |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 116 | |
Heiko Schocher | adb71df | 2016-06-07 08:55:41 +0200 | [diff] [blame] | 117 | #ifdef CONFIG_MTD_CONCAT |
| 118 | static void create_mtd_concat(void) |
| 119 | { |
| 120 | struct mtd_info *nand_info_list[CONFIG_SYS_MAX_NAND_DEVICE]; |
| 121 | int nand_devices_found = 0; |
| 122 | int i; |
| 123 | |
| 124 | for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) { |
Mugunthan V N | 7b670cc | 2017-06-26 19:12:51 -0500 | [diff] [blame] | 125 | struct mtd_info *mtd = get_nand_dev_by_index(i); |
| 126 | if (mtd != NULL) { |
| 127 | nand_info_list[nand_devices_found] = mtd; |
Heiko Schocher | adb71df | 2016-06-07 08:55:41 +0200 | [diff] [blame] | 128 | nand_devices_found++; |
| 129 | } |
| 130 | } |
| 131 | if (nand_devices_found > 1) { |
| 132 | struct mtd_info *mtd; |
| 133 | char c_mtd_name[16]; |
| 134 | |
| 135 | /* |
| 136 | * We detected multiple devices. Concatenate them together. |
| 137 | */ |
| 138 | sprintf(c_mtd_name, "nand%d", nand_devices_found); |
| 139 | mtd = mtd_concat_create(nand_info_list, nand_devices_found, |
| 140 | c_mtd_name); |
| 141 | |
| 142 | if (mtd == NULL) |
| 143 | return; |
| 144 | |
| 145 | nand_register(nand_devices_found, mtd); |
| 146 | } |
| 147 | |
| 148 | return; |
| 149 | } |
| 150 | #else |
| 151 | static void create_mtd_concat(void) |
| 152 | { |
| 153 | } |
| 154 | #endif |
| 155 | |
Hou Zhiqiang | 9c8b3c7 | 2017-03-17 16:12:31 +0800 | [diff] [blame] | 156 | unsigned long nand_size(void) |
| 157 | { |
| 158 | return total_nand_size; |
| 159 | } |
| 160 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 161 | void nand_init(void) |
| 162 | { |
Hou Zhiqiang | 5dac9d8 | 2017-03-17 16:12:30 +0800 | [diff] [blame] | 163 | static int initialized; |
| 164 | |
| 165 | /* |
| 166 | * Avoid initializing NAND Flash multiple times, |
| 167 | * otherwise it will calculate a wrong total size. |
| 168 | */ |
| 169 | if (initialized) |
| 170 | return; |
| 171 | initialized = 1; |
| 172 | |
Tom Rini | 9837245 | 2021-12-12 22:12:36 -0500 | [diff] [blame] | 173 | #if CONFIG_IS_ENABLED(SYS_NAND_SELF_INIT) |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 174 | board_nand_init(); |
| 175 | #else |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 176 | int i; |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 177 | |
| 178 | for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) |
| 179 | nand_init_chip(i); |
| 180 | #endif |
| 181 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 182 | #ifdef CONFIG_SYS_NAND_SELECT_DEVICE |
Stefan Roese | 3cdd3fd | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 183 | /* |
| 184 | * Select the chip in the board/cpu specific driver |
| 185 | */ |
Mugunthan V N | 7b670cc | 2017-06-26 19:12:51 -0500 | [diff] [blame] | 186 | 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] | 187 | nand_curr_device); |
Stefan Roese | 3cdd3fd | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 188 | #endif |
Heiko Schocher | adb71df | 2016-06-07 08:55:41 +0200 | [diff] [blame] | 189 | |
| 190 | create_mtd_concat(); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 191 | } |
Sean Anderson | 8805f9d | 2023-11-04 16:37:44 -0400 | [diff] [blame] | 192 | |
| 193 | unsigned int nand_page_size(void) |
| 194 | { |
| 195 | struct mtd_info *mtd = get_nand_dev_by_index(nand_curr_device); |
| 196 | |
| 197 | return mtd ? mtd->writesize : 1; |
| 198 | } |