nand.c 2.8 KB

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