nand.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #include <common.h>
  24. #include <nand.h>
  25. #ifndef CONFIG_SYS_NAND_BASE_LIST
  26. #define CONFIG_SYS_NAND_BASE_LIST { CONFIG_SYS_NAND_BASE }
  27. #endif
  28. DECLARE_GLOBAL_DATA_PTR;
  29. int nand_curr_device = -1;
  30. nand_info_t nand_info[CONFIG_SYS_MAX_NAND_DEVICE];
  31. static struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE];
  32. static ulong base_address[CONFIG_SYS_MAX_NAND_DEVICE] = CONFIG_SYS_NAND_BASE_LIST;
  33. static const char default_nand_name[] = "nand";
  34. static __attribute__((unused)) char dev_name[CONFIG_SYS_MAX_NAND_DEVICE][8];
  35. static void nand_init_chip(struct mtd_info *mtd, struct nand_chip *nand,
  36. ulong base_addr)
  37. {
  38. int maxchips = CONFIG_SYS_NAND_MAX_CHIPS;
  39. int __attribute__((unused)) i = 0;
  40. if (maxchips < 1)
  41. maxchips = 1;
  42. mtd->priv = nand;
  43. nand->IO_ADDR_R = nand->IO_ADDR_W = (void __iomem *)base_addr;
  44. if (board_nand_init(nand) == 0) {
  45. if (nand_scan(mtd, maxchips) == 0) {
  46. if (!mtd->name)
  47. mtd->name = (char *)default_nand_name;
  48. #ifndef CONFIG_RELOC_FIXUP_WORKS
  49. else
  50. mtd->name += gd->reloc_off;
  51. #endif
  52. #ifdef CONFIG_MTD_DEVICE
  53. /*
  54. * Add MTD device so that we can reference it later
  55. * via the mtdcore infrastructure (e.g. ubi).
  56. */
  57. sprintf(dev_name[i], "nand%d", i);
  58. mtd->name = dev_name[i++];
  59. add_mtd_device(mtd);
  60. #endif
  61. } else
  62. mtd->name = NULL;
  63. } else {
  64. mtd->name = NULL;
  65. mtd->size = 0;
  66. }
  67. }
  68. void nand_init(void)
  69. {
  70. int i;
  71. unsigned int size = 0;
  72. for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
  73. nand_init_chip(&nand_info[i], &nand_chip[i], base_address[i]);
  74. size += nand_info[i].size / 1024;
  75. if (nand_curr_device == -1)
  76. nand_curr_device = i;
  77. }
  78. printf("%u MiB\n", size / 1024);
  79. #ifdef CONFIG_SYS_NAND_SELECT_DEVICE
  80. /*
  81. * Select the chip in the board/cpu specific driver
  82. */
  83. board_nand_select_device(nand_info[nand_curr_device].priv, nand_curr_device);
  84. #endif
  85. }