nand.c 3.5 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. static struct mtd_info *nand_info[CONFIG_SYS_MAX_NAND_DEVICE];
  18. #ifndef CONFIG_SYS_NAND_SELF_INIT
  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. struct mtd_info *get_nand_dev_by_index(int dev)
  25. {
  26. if (dev < 0 || dev >= CONFIG_SYS_MAX_NAND_DEVICE || !nand_info[dev] ||
  27. !nand_info[dev]->name)
  28. return NULL;
  29. return nand_info[dev];
  30. }
  31. int nand_mtd_to_devnum(struct mtd_info *mtd)
  32. {
  33. int i;
  34. for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
  35. if (mtd && get_nand_dev_by_index(i) == mtd)
  36. return i;
  37. }
  38. return -ENODEV;
  39. }
  40. /* Register an initialized NAND mtd device with the U-Boot NAND command. */
  41. int nand_register(int devnum, struct mtd_info *mtd)
  42. {
  43. if (devnum >= CONFIG_SYS_MAX_NAND_DEVICE)
  44. return -EINVAL;
  45. nand_info[devnum] = mtd;
  46. sprintf(dev_name[devnum], "nand%d", devnum);
  47. mtd->name = dev_name[devnum];
  48. #ifdef CONFIG_MTD_DEVICE
  49. /*
  50. * Add MTD device so that we can reference it later
  51. * via the mtdcore infrastructure (e.g. ubi).
  52. */
  53. add_mtd_device(mtd);
  54. #endif
  55. total_nand_size += mtd->size / 1024;
  56. if (nand_curr_device == -1)
  57. nand_curr_device = devnum;
  58. return 0;
  59. }
  60. #ifndef CONFIG_SYS_NAND_SELF_INIT
  61. static void nand_init_chip(int i)
  62. {
  63. struct nand_chip *nand = &nand_chip[i];
  64. struct mtd_info *mtd = nand_to_mtd(nand);
  65. ulong base_addr = base_address[i];
  66. int maxchips = CONFIG_SYS_NAND_MAX_CHIPS;
  67. if (maxchips < 1)
  68. maxchips = 1;
  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, mtd);
  75. }
  76. #endif
  77. #ifdef CONFIG_MTD_CONCAT
  78. static void create_mtd_concat(void)
  79. {
  80. struct mtd_info *nand_info_list[CONFIG_SYS_MAX_NAND_DEVICE];
  81. int nand_devices_found = 0;
  82. int i;
  83. for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++) {
  84. struct mtd_info *mtd = get_nand_dev_by_index(i);
  85. if (mtd != NULL) {
  86. nand_info_list[nand_devices_found] = mtd;
  87. nand_devices_found++;
  88. }
  89. }
  90. if (nand_devices_found > 1) {
  91. struct mtd_info *mtd;
  92. char c_mtd_name[16];
  93. /*
  94. * We detected multiple devices. Concatenate them together.
  95. */
  96. sprintf(c_mtd_name, "nand%d", nand_devices_found);
  97. mtd = mtd_concat_create(nand_info_list, nand_devices_found,
  98. c_mtd_name);
  99. if (mtd == NULL)
  100. return;
  101. nand_register(nand_devices_found, mtd);
  102. }
  103. return;
  104. }
  105. #else
  106. static void create_mtd_concat(void)
  107. {
  108. }
  109. #endif
  110. unsigned long nand_size(void)
  111. {
  112. return total_nand_size;
  113. }
  114. void nand_init(void)
  115. {
  116. static int initialized;
  117. /*
  118. * Avoid initializing NAND Flash multiple times,
  119. * otherwise it will calculate a wrong total size.
  120. */
  121. if (initialized)
  122. return;
  123. initialized = 1;
  124. #ifdef CONFIG_SYS_NAND_SELF_INIT
  125. board_nand_init();
  126. #else
  127. int i;
  128. for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
  129. nand_init_chip(i);
  130. #endif
  131. #ifdef CONFIG_SYS_NAND_SELECT_DEVICE
  132. /*
  133. * Select the chip in the board/cpu specific driver
  134. */
  135. board_nand_select_device(mtd_to_nand(get_nand_dev_by_index(nand_curr_device)),
  136. nand_curr_device);
  137. #endif
  138. create_mtd_concat();
  139. }