nand.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * (C) Copyright 2005
  3. * 2N Telekomunikace, a.s. <www.2n.cz>
  4. * Ladislav Michl <michl@2n.cz>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0
  7. */
  8. #include <common.h>
  9. #include <nand.h>
  10. #include <errno.h>
  11. #ifndef CONFIG_SYS_NAND_BASE_LIST
  12. #define CONFIG_SYS_NAND_BASE_LIST { CONFIG_SYS_NAND_BASE }
  13. #endif
  14. DECLARE_GLOBAL_DATA_PTR;
  15. int nand_curr_device = -1;
  16. struct mtd_info *nand_info[CONFIG_SYS_MAX_NAND_DEVICE];
  17. #ifndef CONFIG_SYS_NAND_SELF_INIT
  18. static struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE];
  19. static ulong base_address[CONFIG_SYS_MAX_NAND_DEVICE] = CONFIG_SYS_NAND_BASE_LIST;
  20. #endif
  21. static char dev_name[CONFIG_SYS_MAX_NAND_DEVICE][8];
  22. static unsigned long total_nand_size; /* in kiB */
  23. int nand_mtd_to_devnum(struct mtd_info *mtd)
  24. {
  25. int i;
  26. for (i = 0; i < ARRAY_SIZE(nand_info); i++) {
  27. if (mtd && nand_info[i] == mtd)
  28. return i;
  29. }
  30. return -ENODEV;
  31. }
  32. /* Register an initialized NAND mtd device with the U-Boot NAND command. */
  33. int nand_register(int devnum, struct mtd_info *mtd)
  34. {
  35. if (devnum >= CONFIG_SYS_MAX_NAND_DEVICE)
  36. return -EINVAL;
  37. nand_info[devnum] = mtd;
  38. sprintf(dev_name[devnum], "nand%d", devnum);
  39. mtd->name = dev_name[devnum];
  40. #ifdef CONFIG_MTD_DEVICE
  41. /*
  42. * Add MTD device so that we can reference it later
  43. * via the mtdcore infrastructure (e.g. ubi).
  44. */
  45. add_mtd_device(mtd);
  46. #endif
  47. total_nand_size += mtd->size / 1024;
  48. if (nand_curr_device == -1)
  49. nand_curr_device = devnum;
  50. return 0;
  51. }
  52. #ifndef CONFIG_SYS_NAND_SELF_INIT
  53. static void nand_init_chip(int i)
  54. {
  55. struct nand_chip *nand = &nand_chip[i];
  56. struct mtd_info *mtd = nand_to_mtd(nand);
  57. ulong base_addr = base_address[i];
  58. int maxchips = CONFIG_SYS_NAND_MAX_CHIPS;
  59. if (maxchips < 1)
  60. maxchips = 1;
  61. nand->IO_ADDR_R = nand->IO_ADDR_W = (void __iomem *)base_addr;
  62. if (board_nand_init(nand))
  63. return;
  64. if (nand_scan(mtd, maxchips))
  65. return;
  66. nand_register(i, mtd);
  67. }
  68. #endif
  69. void nand_init(void)
  70. {
  71. #ifdef CONFIG_SYS_NAND_SELF_INIT
  72. board_nand_init();
  73. #else
  74. int i;
  75. for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
  76. nand_init_chip(i);
  77. #endif
  78. printf("%lu MiB\n", total_nand_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(mtd_to_nand(nand_info[nand_curr_device]),
  84. nand_curr_device);
  85. #endif
  86. }