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