Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2005 |
| 3 | * 2N Telekomunikace, a.s. <www.2n.cz> |
| 4 | * Ladislav Michl <michl@2n.cz> |
| 5 | * |
Tom Rini | e237880 | 2016-01-14 22:05:13 -0500 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0 |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 10 | #include <nand.h> |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 11 | #include <errno.h> |
Heiko Schocher | adb71df | 2016-06-07 08:55:41 +0200 | [diff] [blame] | 12 | #include <linux/mtd/concat.h> |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 13 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 14 | #ifndef CONFIG_SYS_NAND_BASE_LIST |
| 15 | #define CONFIG_SYS_NAND_BASE_LIST { CONFIG_SYS_NAND_BASE } |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 16 | #endif |
| 17 | |
Valeriy Glushkov | 1d01207 | 2009-01-19 16:32:59 +0200 | [diff] [blame] | 18 | DECLARE_GLOBAL_DATA_PTR; |
| 19 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 20 | int nand_curr_device = -1; |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 21 | |
Grygorii Strashko | aa5528b | 2017-06-26 19:13:08 -0500 | [diff] [blame] | 22 | static struct mtd_info *nand_info[CONFIG_SYS_MAX_NAND_DEVICE]; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 23 | |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 24 | #ifndef CONFIG_SYS_NAND_SELF_INIT |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 25 | static struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE]; |
| 26 | static ulong base_address[CONFIG_SYS_MAX_NAND_DEVICE] = CONFIG_SYS_NAND_BASE_LIST; |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 27 | #endif |
| 28 | |
| 29 | static char dev_name[CONFIG_SYS_MAX_NAND_DEVICE][8]; |
| 30 | |
| 31 | static unsigned long total_nand_size; /* in kiB */ |
| 32 | |
Mugunthan V N | 7b670cc | 2017-06-26 19:12:51 -0500 | [diff] [blame] | 33 | struct mtd_info *get_nand_dev_by_index(int dev) |
| 34 | { |
| 35 | if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[dev] || |
| 36 | !nand_info[dev]->name) |
| 37 | return NULL; |
| 38 | |
| 39 | return nand_info[dev]; |
| 40 | } |
| 41 | |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 42 | int nand_mtd_to_devnum(struct mtd_info *mtd) |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 43 | { |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 44 | int i; |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 45 | |
Mugunthan V N | 7b670cc | 2017-06-26 19:12:51 -0500 | [diff] [blame] | 46 | for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) { |
| 47 | if (mtd && get_nand_dev_by_index(i) == mtd) |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 48 | return i; |
| 49 | } |
| 50 | |
| 51 | return -ENODEV; |
| 52 | } |
| 53 | |
| 54 | /* Register an initialized NAND mtd device with the U-Boot NAND command. */ |
| 55 | int nand_register(int devnum, struct mtd_info *mtd) |
| 56 | { |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 57 | if (devnum >= CONFIG_SYS_MAX_NAND_DEVICE) |
| 58 | return -EINVAL; |
| 59 | |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 60 | nand_info[devnum] = mtd; |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 61 | |
| 62 | sprintf(dev_name[devnum], "nand%d", devnum); |
| 63 | mtd->name = dev_name[devnum]; |
| 64 | |
| 65 | #ifdef CONFIG_MTD_DEVICE |
| 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); |
| 71 | #endif |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 72 | |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 73 | total_nand_size += mtd->size / 1024; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 74 | |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 75 | if (nand_curr_device == -1) |
| 76 | nand_curr_device = devnum; |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | #ifndef CONFIG_SYS_NAND_SELF_INIT |
| 82 | static void nand_init_chip(int i) |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 83 | { |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 84 | struct nand_chip *nand = &nand_chip[i]; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 85 | struct mtd_info *mtd = nand_to_mtd(nand); |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 86 | ulong base_addr = base_address[i]; |
Wolfgang Grandegger | b325d7e | 2009-02-11 18:38:20 +0100 | [diff] [blame] | 87 | int maxchips = CONFIG_SYS_NAND_MAX_CHIPS; |
| 88 | |
| 89 | if (maxchips < 1) |
| 90 | maxchips = 1; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 91 | |
Bartlomiej Sieka | 6eed2ab | 2006-02-24 09:37:22 +0100 | [diff] [blame] | 92 | nand->IO_ADDR_R = nand->IO_ADDR_W = (void __iomem *)base_addr; |
Stefan Roese | 4157c63 | 2009-04-24 15:59:35 +0200 | [diff] [blame] | 93 | |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 94 | if (board_nand_init(nand)) |
| 95 | return; |
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 | if (nand_scan(mtd, maxchips)) |
| 98 | return; |
| 99 | |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 100 | nand_register(i, mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 101 | } |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 102 | #endif |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 103 | |
Heiko Schocher | adb71df | 2016-06-07 08:55:41 +0200 | [diff] [blame] | 104 | #ifdef CONFIG_MTD_CONCAT |
| 105 | static void create_mtd_concat(void) |
| 106 | { |
| 107 | struct mtd_info *nand_info_list[CONFIG_SYS_MAX_NAND_DEVICE]; |
| 108 | int nand_devices_found = 0; |
| 109 | int i; |
| 110 | |
| 111 | for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) { |
Mugunthan V N | 7b670cc | 2017-06-26 19:12:51 -0500 | [diff] [blame] | 112 | struct mtd_info *mtd = get_nand_dev_by_index(i); |
| 113 | if (mtd != NULL) { |
| 114 | nand_info_list[nand_devices_found] = mtd; |
Heiko Schocher | adb71df | 2016-06-07 08:55:41 +0200 | [diff] [blame] | 115 | nand_devices_found++; |
| 116 | } |
| 117 | } |
| 118 | if (nand_devices_found > 1) { |
| 119 | struct mtd_info *mtd; |
| 120 | char c_mtd_name[16]; |
| 121 | |
| 122 | /* |
| 123 | * We detected multiple devices. Concatenate them together. |
| 124 | */ |
| 125 | sprintf(c_mtd_name, "nand%d", nand_devices_found); |
| 126 | mtd = mtd_concat_create(nand_info_list, nand_devices_found, |
| 127 | c_mtd_name); |
| 128 | |
| 129 | if (mtd == NULL) |
| 130 | return; |
| 131 | |
| 132 | nand_register(nand_devices_found, mtd); |
| 133 | } |
| 134 | |
| 135 | return; |
| 136 | } |
| 137 | #else |
| 138 | static void create_mtd_concat(void) |
| 139 | { |
| 140 | } |
| 141 | #endif |
| 142 | |
Hou Zhiqiang | 9c8b3c7 | 2017-03-17 16:12:31 +0800 | [diff] [blame] | 143 | unsigned long nand_size(void) |
| 144 | { |
| 145 | return total_nand_size; |
| 146 | } |
| 147 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 148 | void nand_init(void) |
| 149 | { |
Hou Zhiqiang | 5dac9d8 | 2017-03-17 16:12:30 +0800 | [diff] [blame] | 150 | static int initialized; |
| 151 | |
| 152 | /* |
| 153 | * Avoid initializing NAND Flash multiple times, |
| 154 | * otherwise it will calculate a wrong total size. |
| 155 | */ |
| 156 | if (initialized) |
| 157 | return; |
| 158 | initialized = 1; |
| 159 | |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 160 | #ifdef CONFIG_SYS_NAND_SELF_INIT |
| 161 | board_nand_init(); |
| 162 | #else |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 163 | int i; |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 164 | |
| 165 | for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) |
| 166 | nand_init_chip(i); |
| 167 | #endif |
| 168 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 169 | #ifdef CONFIG_SYS_NAND_SELECT_DEVICE |
Stefan Roese | 3cdd3fd | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 170 | /* |
| 171 | * Select the chip in the board/cpu specific driver |
| 172 | */ |
Mugunthan V N | 7b670cc | 2017-06-26 19:12:51 -0500 | [diff] [blame] | 173 | 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] | 174 | nand_curr_device); |
Stefan Roese | 3cdd3fd | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 175 | #endif |
Heiko Schocher | adb71df | 2016-06-07 08:55:41 +0200 | [diff] [blame] | 176 | |
| 177 | create_mtd_concat(); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 178 | } |