nand.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. #include <linux/mtd/concat.h>
  12. #ifndef CONFIG_SYS_NAND_BASE_LIST
  13. #define CONFIG_SYS_NAND_BASE_LIST { CONFIG_SYS_NAND_BASE }
  14. #endif
  15. DECLARE_GLOBAL_DATA_PTR;
  16. int nand_curr_device = -1;
  17. struct mtd_info *nand_info[CONFIG_SYS_MAX_NAND_DEVICE];
  18. #if !defined(CONFIG_SYS_NAND_SELF_INIT) && !defined(CONFIG_DM_NAND)
  19. static struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE];
  20. static ulong base_address[CONFIG_SYS_MAX_NAND_DEVICE] = CONFIG_SYS_NAND_BASE_LIST;
  21. #endif
  22. static char dev_name[CONFIG_SYS_MAX_NAND_DEVICE][8];
  23. static unsigned long total_nand_size; /* in kiB */
  24. #ifndef CONFIG_DM_NAND
  25. struct mtd_info *get_nand_dev_by_index(int dev)
  26. {
  27. if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[dev] ||
  28. !nand_info[dev]->name)
  29. return NULL;
  30. return nand_info[dev];
  31. }
  32. #endif
  33. int nand_mtd_to_devnum(struct mtd_info *mtd)
  34. {
  35. int i;
  36. for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
  37. if (mtd && get_nand_dev_by_index(i) == mtd)
  38. return i;
  39. }
  40. return -ENODEV;
  41. }
  42. /* Register an initialized NAND mtd device with the U-Boot NAND command. */
  43. int nand_register(int devnum, struct mtd_info *mtd)
  44. {
  45. if (devnum >= CONFIG_SYS_MAX_NAND_DEVICE)
  46. return -EINVAL;
  47. #if !defined(CONFIG_SYS_NAND_SELF_INIT) && !defined(CONFIG_DM_NAND)
  48. nand_info[devnum] = mtd;
  49. #endif
  50. sprintf(dev_name[devnum], "nand%d", devnum);
  51. mtd->name = dev_name[devnum];
  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. add_mtd_device(mtd);
  58. #endif
  59. total_nand_size += mtd->size / 1024;
  60. if (nand_curr_device == -1)
  61. nand_curr_device = devnum;
  62. return 0;
  63. }
  64. #ifndef CONFIG_SYS_NAND_SELF_INIT
  65. static void nand_init_chip(int i)
  66. {
  67. #ifndef CONFIG_DM_NAND
  68. struct nand_chip *nand = &nand_chip[i];
  69. struct mtd_info *mtd = nand_to_mtd(nand);
  70. ulong base_addr = base_address[i];
  71. #else
  72. struct mtd_info *mtd;
  73. #endif
  74. int maxchips = CONFIG_SYS_NAND_MAX_CHIPS;
  75. if (maxchips < 1)
  76. maxchips = 1;
  77. #ifdef CONFIG_DM_NAND
  78. mtd = get_nand_dev_by_index(i);
  79. if (!mtd)
  80. return;
  81. #else
  82. nand->IO_ADDR_R = nand->IO_ADDR_W = (void __iomem *)base_addr;
  83. if (board_nand_init(nand))
  84. return;
  85. #endif
  86. if (nand_scan(mtd, maxchips))
  87. return;
  88. nand_register(i, mtd);
  89. }
  90. #endif
  91. #ifdef CONFIG_MTD_CONCAT
  92. static void create_mtd_concat(void)
  93. {
  94. struct mtd_info *nand_info_list[CONFIG_SYS_MAX_NAND_DEVICE];
  95. int nand_devices_found = 0;
  96. int i;
  97. for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
  98. struct mtd_info *mtd = get_nand_dev_by_index(i);
  99. if (mtd != NULL) {
  100. nand_info_list[nand_devices_found] = mtd;
  101. nand_devices_found++;
  102. }
  103. }
  104. if (nand_devices_found > 1) {
  105. struct mtd_info *mtd;
  106. char c_mtd_name[16];
  107. /*
  108. * We detected multiple devices. Concatenate them together.
  109. */
  110. sprintf(c_mtd_name, "nand%d", nand_devices_found);
  111. mtd = mtd_concat_create(nand_info_list, nand_devices_found,
  112. c_mtd_name);
  113. if (mtd == NULL)
  114. return;
  115. nand_register(nand_devices_found, mtd);
  116. }
  117. return;
  118. }
  119. #else
  120. static void create_mtd_concat(void)
  121. {
  122. }
  123. #endif
  124. void nand_init(void)
  125. {
  126. #ifdef CONFIG_SYS_NAND_SELF_INIT
  127. board_nand_init();
  128. #else
  129. int i;
  130. for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
  131. nand_init_chip(i);
  132. #endif
  133. printf("%lu MiB\n", total_nand_size / 1024);
  134. #ifdef CONFIG_SYS_NAND_SELECT_DEVICE
  135. /*
  136. * Select the chip in the board/cpu specific driver
  137. */
  138. board_nand_select_device(mtd_to_nand(get_nand_dev_by_index(nand_curr_device)),
  139. nand_curr_device);
  140. #endif
  141. create_mtd_concat();
  142. }