Wolfgang Denk | 932394a | 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 | * |
| 6 | * See file CREDITS for list of people who contributed to this |
| 7 | * project. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License |
| 11 | * version 2 as published by the Free Software Foundation. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include <common.h> |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 25 | #include <nand.h> |
Scott Wood | 578931b | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 26 | #include <errno.h> |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 27 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 28 | #ifndef CONFIG_SYS_NAND_BASE_LIST |
| 29 | #define CONFIG_SYS_NAND_BASE_LIST { CONFIG_SYS_NAND_BASE } |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 30 | #endif |
| 31 | |
Valeriy Glushkov | ad09ab2 | 2009-01-19 16:32:59 +0200 | [diff] [blame] | 32 | DECLARE_GLOBAL_DATA_PTR; |
| 33 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 34 | int nand_curr_device = -1; |
Scott Wood | 578931b | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 35 | |
| 36 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 37 | nand_info_t nand_info[CONFIG_SYS_MAX_NAND_DEVICE]; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 38 | |
Scott Wood | 578931b | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 39 | #ifndef CONFIG_SYS_NAND_SELF_INIT |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 40 | static struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE]; |
| 41 | static ulong base_address[CONFIG_SYS_MAX_NAND_DEVICE] = CONFIG_SYS_NAND_BASE_LIST; |
Scott Wood | 578931b | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 42 | #endif |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 43 | |
Scott Wood | 578931b | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 44 | static char dev_name[CONFIG_SYS_MAX_NAND_DEVICE][8]; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 45 | |
Scott Wood | 578931b | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 46 | static unsigned long total_nand_size; /* in kiB */ |
| 47 | |
| 48 | /* Register an initialized NAND mtd device with the U-Boot NAND command. */ |
| 49 | int nand_register(int devnum) |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 50 | { |
Scott Wood | 578931b | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 51 | struct mtd_info *mtd; |
| 52 | |
| 53 | if (devnum >= CONFIG_SYS_MAX_NAND_DEVICE) |
| 54 | return -EINVAL; |
| 55 | |
| 56 | mtd = &nand_info[devnum]; |
| 57 | |
| 58 | sprintf(dev_name[devnum], "nand%d", devnum); |
| 59 | mtd->name = dev_name[devnum]; |
| 60 | |
| 61 | #ifdef CONFIG_MTD_DEVICE |
| 62 | /* |
| 63 | * Add MTD device so that we can reference it later |
| 64 | * via the mtdcore infrastructure (e.g. ubi). |
| 65 | */ |
| 66 | add_mtd_device(mtd); |
| 67 | #endif |
| 68 | |
| 69 | total_nand_size += mtd->size / 1024; |
| 70 | |
| 71 | if (nand_curr_device == -1) |
| 72 | nand_curr_device = devnum; |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | #ifndef CONFIG_SYS_NAND_SELF_INIT |
| 78 | static void nand_init_chip(int i) |
| 79 | { |
| 80 | struct mtd_info *mtd = &nand_info[i]; |
| 81 | struct nand_chip *nand = &nand_chip[i]; |
| 82 | ulong base_addr = base_address[i]; |
Wolfgang Grandegger | 672ed2a | 2009-02-11 18:38:20 +0100 | [diff] [blame] | 83 | int maxchips = CONFIG_SYS_NAND_MAX_CHIPS; |
| 84 | |
| 85 | if (maxchips < 1) |
| 86 | maxchips = 1; |
Scott Wood | 578931b | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 87 | |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 88 | mtd->priv = nand; |
Bartlomiej Sieka | 038ccac | 2006-02-24 09:37:22 +0100 | [diff] [blame] | 89 | nand->IO_ADDR_R = nand->IO_ADDR_W = (void __iomem *)base_addr; |
Stefan Roese | dbe29e3 | 2009-04-24 15:59:35 +0200 | [diff] [blame] | 90 | |
Scott Wood | 578931b | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 91 | if (board_nand_init(nand)) |
| 92 | return; |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 93 | |
Scott Wood | 578931b | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 94 | if (nand_scan(mtd, maxchips)) |
| 95 | return; |
| 96 | |
| 97 | nand_register(i); |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 98 | } |
Scott Wood | 578931b | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 99 | #endif |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 100 | |
| 101 | void nand_init(void) |
| 102 | { |
Scott Wood | 578931b | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 103 | #ifdef CONFIG_SYS_NAND_SELF_INIT |
| 104 | board_nand_init(); |
| 105 | #else |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 106 | int i; |
Scott Wood | 578931b | 2012-01-12 19:07:23 -0600 | [diff] [blame] | 107 | |
| 108 | for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) |
| 109 | nand_init_chip(i); |
| 110 | #endif |
| 111 | |
| 112 | printf("%lu MiB\n", total_nand_size / 1024); |
Stefan Roese | 43a2b0e | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 113 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 114 | #ifdef CONFIG_SYS_NAND_SELECT_DEVICE |
Stefan Roese | 43a2b0e | 2006-10-20 14:28:52 +0200 | [diff] [blame] | 115 | /* |
| 116 | * Select the chip in the board/cpu specific driver |
| 117 | */ |
| 118 | board_nand_select_device(nand_info[nand_curr_device].priv, nand_curr_device); |
| 119 | #endif |
Wolfgang Denk | 932394a | 2005-08-17 12:55:25 +0200 | [diff] [blame] | 120 | } |