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