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> |
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 | |
Valeriy Glushkov | 1d01207 | 2009-01-19 16:32:59 +0200 | [diff] [blame] | 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 19 | int nand_curr_device = -1; |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 20 | |
| 21 | |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 22 | 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 | |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 33 | int nand_mtd_to_devnum(struct mtd_info *mtd) |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 34 | { |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 35 | int i; |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 36 | |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 37 | for (i = 0; i < ARRAY_SIZE(nand_info); i++) { |
| 38 | if (mtd && nand_info[i] == mtd) |
| 39 | return i; |
| 40 | } |
| 41 | |
| 42 | return -ENODEV; |
| 43 | } |
| 44 | |
| 45 | /* Register an initialized NAND mtd device with the U-Boot NAND command. */ |
| 46 | int nand_register(int devnum, struct mtd_info *mtd) |
| 47 | { |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 48 | if (devnum >= CONFIG_SYS_MAX_NAND_DEVICE) |
| 49 | return -EINVAL; |
| 50 | |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 51 | nand_info[devnum] = mtd; |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 52 | |
| 53 | sprintf(dev_name[devnum], "nand%d", devnum); |
| 54 | mtd->name = dev_name[devnum]; |
| 55 | |
| 56 | #ifdef CONFIG_MTD_DEVICE |
| 57 | /* |
| 58 | * Add MTD device so that we can reference it later |
| 59 | * via the mtdcore infrastructure (e.g. ubi). |
| 60 | */ |
| 61 | add_mtd_device(mtd); |
| 62 | #endif |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 63 | |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 64 | total_nand_size += mtd->size / 1024; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 65 | |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 66 | if (nand_curr_device == -1) |
| 67 | nand_curr_device = devnum; |
| 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | #ifndef CONFIG_SYS_NAND_SELF_INIT |
| 73 | static void nand_init_chip(int i) |
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 | struct nand_chip *nand = &nand_chip[i]; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 76 | struct mtd_info *mtd = nand_to_mtd(nand); |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 77 | ulong base_addr = base_address[i]; |
Wolfgang Grandegger | b325d7e | 2009-02-11 18:38:20 +0100 | [diff] [blame] | 78 | int maxchips = CONFIG_SYS_NAND_MAX_CHIPS; |
| 79 | |
| 80 | if (maxchips < 1) |
| 81 | maxchips = 1; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 82 | |
Bartlomiej Sieka | 6eed2ab | 2006-02-24 09:37:22 +0100 | [diff] [blame] | 83 | nand->IO_ADDR_R = nand->IO_ADDR_W = (void __iomem *)base_addr; |
Stefan Roese | 4157c63 | 2009-04-24 15:59:35 +0200 | [diff] [blame] | 84 | |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 85 | if (board_nand_init(nand)) |
| 86 | return; |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 87 | |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 88 | if (nand_scan(mtd, maxchips)) |
| 89 | return; |
| 90 | |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 91 | nand_register(i, mtd); |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 92 | } |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 93 | #endif |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 94 | |
| 95 | void nand_init(void) |
| 96 | { |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 97 | #ifdef CONFIG_SYS_NAND_SELF_INIT |
| 98 | board_nand_init(); |
| 99 | #else |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 100 | int i; |
Scott Wood | 193a0f5 | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 101 | |
| 102 | for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) |
| 103 | nand_init_chip(i); |
| 104 | #endif |
| 105 | |
| 106 | printf("%lu MiB\n", total_nand_size / 1024); |
Stefan Roese | 3cdd3fd | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 107 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 108 | #ifdef CONFIG_SYS_NAND_SELECT_DEVICE |
Stefan Roese | 3cdd3fd | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 109 | /* |
| 110 | * Select the chip in the board/cpu specific driver |
| 111 | */ |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 112 | board_nand_select_device(mtd_to_nand(nand_info[nand_curr_device]), |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 113 | nand_curr_device); |
Stefan Roese | 3cdd3fd | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 114 | #endif |
Wolfgang Denk | 2f9b7e4 | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 115 | } |