nand.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * (C) Copyright 2005
  4. * 2N Telekomunikace, a.s. <www.2n.cz>
  5. * Ladislav Michl <michl@2n.cz>
  6. */
  7. #include <common.h>
  8. #include <nand.h>
  9. #include <errno.h>
  10. #include <linux/mtd/concat.h>
  11. #ifndef CONFIG_SYS_NAND_BASE_LIST
  12. #define CONFIG_SYS_NAND_BASE_LIST { CONFIG_SYS_NAND_BASE }
  13. #endif
  14. int nand_curr_device = -1;
  15. static struct mtd_info *nand_info[CONFIG_SYS_MAX_NAND_DEVICE];
  16. #ifndef CONFIG_SYS_NAND_SELF_INIT
  17. static struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE];
  18. static ulong base_address[CONFIG_SYS_MAX_NAND_DEVICE] = CONFIG_SYS_NAND_BASE_LIST;
  19. #endif
  20. static char dev_name[CONFIG_SYS_MAX_NAND_DEVICE][8];
  21. static unsigned long total_nand_size; /* in kiB */
  22. struct mtd_info *get_nand_dev_by_index(int dev)
  23. {
  24. if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[dev] ||
  25. !nand_info[dev]->name)
  26. return NULL;
  27. return nand_info[dev];
  28. }
  29. int nand_mtd_to_devnum(struct mtd_info *mtd)
  30. {
  31. int i;
  32. for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
  33. if (mtd && get_nand_dev_by_index(i) == mtd)
  34. return i;
  35. }
  36. return -ENODEV;
  37. }
  38. /* Register an initialized NAND mtd device with the U-Boot NAND command. */
  39. int nand_register(int devnum, struct mtd_info *mtd)
  40. {
  41. if (devnum >= CONFIG_SYS_MAX_NAND_DEVICE)
  42. return -EINVAL;
  43. nand_info[devnum] = mtd;
  44. sprintf(dev_name[devnum], "nand%d", devnum);
  45. mtd->name = dev_name[devnum];
  46. #ifdef CONFIG_MTD_DEVICE
  47. /*
  48. * Add MTD device so that we can reference it later
  49. * via the mtdcore infrastructure (e.g. ubi).
  50. */
  51. add_mtd_device(mtd);
  52. #endif
  53. total_nand_size += mtd->size / 1024;
  54. if (nand_curr_device == -1)
  55. nand_curr_device = devnum;
  56. return 0;
  57. }
  58. #ifndef CONFIG_SYS_NAND_SELF_INIT
  59. static void nand_init_chip(int i)
  60. {
  61. struct nand_chip *nand = &nand_chip[i];
  62. struct mtd_info *mtd = nand_to_mtd(nand);
  63. ulong base_addr = base_address[i];
  64. int maxchips = CONFIG_SYS_NAND_MAX_CHIPS;
  65. if (maxchips < 1)
  66. maxchips = 1;
  67. nand->IO_ADDR_R = nand->IO_ADDR_W = (void __iomem *)base_addr;
  68. if (board_nand_init(nand))
  69. return;
  70. if (nand_scan(mtd, maxchips))
  71. return;
  72. nand_register(i, mtd);
  73. }
  74. #endif
  75. #ifdef CONFIG_MTD_CONCAT
  76. static void create_mtd_concat(void)
  77. {
  78. struct mtd_info *nand_info_list[CONFIG_SYS_MAX_NAND_DEVICE];
  79. int nand_devices_found = 0;
  80. int i;
  81. for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
  82. struct mtd_info *mtd = get_nand_dev_by_index(i);
  83. if (mtd != NULL) {
  84. nand_info_list[nand_devices_found] = mtd;
  85. nand_devices_found++;
  86. }
  87. }
  88. if (nand_devices_found > 1) {
  89. struct mtd_info *mtd;
  90. char c_mtd_name[16];
  91. /*
  92. * We detected multiple devices. Concatenate them together.
  93. */
  94. sprintf(c_mtd_name, "nand%d", nand_devices_found);
  95. mtd = mtd_concat_create(nand_info_list, nand_devices_found,
  96. c_mtd_name);
  97. if (mtd == NULL)
  98. return;
  99. nand_register(nand_devices_found, mtd);
  100. }
  101. return;
  102. }
  103. #else
  104. static void create_mtd_concat(void)
  105. {
  106. }
  107. #endif
  108. unsigned long nand_size(void)
  109. {
  110. return total_nand_size;
  111. }
  112. void nand_init(void)
  113. {
  114. static int initialized;
  115. /*
  116. * Avoid initializing NAND Flash multiple times,
  117. * otherwise it will calculate a wrong total size.
  118. */
  119. if (initialized)
  120. return;
  121. initialized = 1;
  122. #ifdef CONFIG_SYS_NAND_SELF_INIT
  123. board_nand_init();
  124. #else
  125. int i;
  126. for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
  127. nand_init_chip(i);
  128. #endif
  129. #ifdef CONFIG_SYS_NAND_SELECT_DEVICE
  130. /*
  131. * Select the chip in the board/cpu specific driver
  132. */
  133. board_nand_select_device(mtd_to_nand(get_nand_dev_by_index(nand_curr_device)),
  134. nand_curr_device);
  135. #endif
  136. create_mtd_concat();
  137. }