stm32mp1_boot_device.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright (c) 2019-2022, STMicroelectronics - All Rights Reserved
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <common/debug.h>
  9. #include <drivers/nand.h>
  10. #include <drivers/raw_nand.h>
  11. #include <drivers/spi_nand.h>
  12. #include <drivers/spi_nor.h>
  13. #include <lib/utils.h>
  14. #include <plat/common/platform.h>
  15. #if STM32MP_RAW_NAND || STM32MP_SPI_NAND
  16. #if STM32MP13
  17. void plat_get_scratch_buffer(void **buffer_addr, size_t *buf_size)
  18. {
  19. assert(buffer_addr != NULL);
  20. assert(buf_size != NULL);
  21. *buffer_addr = (void *)STM32MP_MTD_BUFFER;
  22. *buf_size = PLATFORM_MTD_MAX_PAGE_SIZE;
  23. }
  24. #endif
  25. static int get_data_from_otp(struct nand_device *nand_dev, bool is_slc)
  26. {
  27. uint32_t nand_param;
  28. uint32_t nand2_param __maybe_unused;
  29. /* Check if NAND parameters are stored in OTP */
  30. if (stm32_get_otp_value(NAND_OTP, &nand_param) != 0) {
  31. ERROR("BSEC: NAND_OTP Error\n");
  32. return -EACCES;
  33. }
  34. if (nand_param == 0U) {
  35. #if STM32MP13
  36. if (is_slc) {
  37. return 0;
  38. }
  39. #endif
  40. #if STM32MP15
  41. return 0;
  42. #endif
  43. }
  44. if ((nand_param & NAND_PARAM_STORED_IN_OTP) == 0U) {
  45. #if STM32MP13
  46. if (is_slc) {
  47. goto ecc;
  48. }
  49. #endif
  50. #if STM32MP15
  51. goto ecc;
  52. #endif
  53. }
  54. #if STM32MP13
  55. if (stm32_get_otp_value(NAND2_OTP, &nand2_param) != 0) {
  56. ERROR("BSEC: NAND_OTP Error\n");
  57. return -EACCES;
  58. }
  59. /* Check OTP configuration for this device */
  60. if ((((nand2_param & NAND2_CONFIG_DISTRIB) == NAND2_PNAND_NAND1_SNAND_NAND2) && !is_slc) ||
  61. (((nand2_param & NAND2_CONFIG_DISTRIB) == NAND2_PNAND_NAND2_SNAND_NAND1) && is_slc)) {
  62. nand_param = nand2_param << (NAND_PAGE_SIZE_SHIFT - NAND2_PAGE_SIZE_SHIFT);
  63. }
  64. #endif
  65. /* NAND parameter shall be read from OTP */
  66. if ((nand_param & NAND_WIDTH_MASK) != 0U) {
  67. nand_dev->buswidth = NAND_BUS_WIDTH_16;
  68. } else {
  69. nand_dev->buswidth = NAND_BUS_WIDTH_8;
  70. }
  71. switch ((nand_param & NAND_PAGE_SIZE_MASK) >> NAND_PAGE_SIZE_SHIFT) {
  72. case NAND_PAGE_SIZE_2K:
  73. nand_dev->page_size = 0x800U;
  74. break;
  75. case NAND_PAGE_SIZE_4K:
  76. nand_dev->page_size = 0x1000U;
  77. break;
  78. case NAND_PAGE_SIZE_8K:
  79. nand_dev->page_size = 0x2000U;
  80. break;
  81. default:
  82. ERROR("Cannot read NAND page size\n");
  83. return -EINVAL;
  84. }
  85. switch ((nand_param & NAND_BLOCK_SIZE_MASK) >> NAND_BLOCK_SIZE_SHIFT) {
  86. case NAND_BLOCK_SIZE_64_PAGES:
  87. nand_dev->block_size = 64U * nand_dev->page_size;
  88. break;
  89. case NAND_BLOCK_SIZE_128_PAGES:
  90. nand_dev->block_size = 128U * nand_dev->page_size;
  91. break;
  92. case NAND_BLOCK_SIZE_256_PAGES:
  93. nand_dev->block_size = 256U * nand_dev->page_size;
  94. break;
  95. default:
  96. ERROR("Cannot read NAND block size\n");
  97. return -EINVAL;
  98. }
  99. nand_dev->size = ((nand_param & NAND_BLOCK_NB_MASK) >>
  100. NAND_BLOCK_NB_SHIFT) *
  101. NAND_BLOCK_NB_UNIT * nand_dev->block_size;
  102. ecc:
  103. if (is_slc) {
  104. switch ((nand_param & NAND_ECC_BIT_NB_MASK) >>
  105. NAND_ECC_BIT_NB_SHIFT) {
  106. case NAND_ECC_BIT_NB_1_BITS:
  107. nand_dev->ecc.max_bit_corr = 1U;
  108. break;
  109. case NAND_ECC_BIT_NB_4_BITS:
  110. nand_dev->ecc.max_bit_corr = 4U;
  111. break;
  112. case NAND_ECC_BIT_NB_8_BITS:
  113. nand_dev->ecc.max_bit_corr = 8U;
  114. break;
  115. case NAND_ECC_ON_DIE:
  116. nand_dev->ecc.mode = NAND_ECC_ONDIE;
  117. break;
  118. default:
  119. if (nand_dev->ecc.max_bit_corr == 0U) {
  120. ERROR("No valid eccbit number\n");
  121. return -EINVAL;
  122. }
  123. }
  124. } else {
  125. /* Selected multiple plane NAND */
  126. if ((nand_param & NAND_PLANE_BIT_NB_MASK) != 0U) {
  127. nand_dev->nb_planes = 2U;
  128. } else {
  129. nand_dev->nb_planes = 1U;
  130. }
  131. }
  132. VERBOSE("OTP: Block %u Page %u Size %llu\n", nand_dev->block_size,
  133. nand_dev->page_size, nand_dev->size);
  134. return 0;
  135. }
  136. #endif /* STM32MP_RAW_NAND || STM32MP_SPI_NAND */
  137. #if STM32MP_RAW_NAND
  138. int plat_get_raw_nand_data(struct rawnand_device *device)
  139. {
  140. device->nand_dev->ecc.mode = NAND_ECC_HW;
  141. device->nand_dev->ecc.size = SZ_512;
  142. return get_data_from_otp(device->nand_dev, true);
  143. }
  144. #endif
  145. #if STM32MP_SPI_NAND
  146. int plat_get_spi_nand_data(struct spinand_device *device)
  147. {
  148. zeromem(&device->spi_read_cache_op, sizeof(struct spi_mem_op));
  149. device->spi_read_cache_op.cmd.opcode = SPI_NAND_OP_READ_FROM_CACHE_4X;
  150. device->spi_read_cache_op.cmd.buswidth = SPI_MEM_BUSWIDTH_1_LINE;
  151. device->spi_read_cache_op.addr.nbytes = 2U;
  152. device->spi_read_cache_op.addr.buswidth = SPI_MEM_BUSWIDTH_1_LINE;
  153. device->spi_read_cache_op.dummy.nbytes = 1U;
  154. device->spi_read_cache_op.dummy.buswidth = SPI_MEM_BUSWIDTH_1_LINE;
  155. device->spi_read_cache_op.data.buswidth = SPI_MEM_BUSWIDTH_4_LINE;
  156. device->spi_read_cache_op.data.dir = SPI_MEM_DATA_IN;
  157. return get_data_from_otp(device->nand_dev, false);
  158. }
  159. #endif
  160. #if STM32MP_SPI_NOR
  161. int plat_get_nor_data(struct nor_device *device)
  162. {
  163. device->size = SZ_64M;
  164. zeromem(&device->read_op, sizeof(struct spi_mem_op));
  165. device->read_op.cmd.opcode = SPI_NOR_OP_READ_1_1_4;
  166. device->read_op.cmd.buswidth = SPI_MEM_BUSWIDTH_1_LINE;
  167. device->read_op.addr.nbytes = 3U;
  168. device->read_op.addr.buswidth = SPI_MEM_BUSWIDTH_1_LINE;
  169. device->read_op.dummy.nbytes = 1U;
  170. device->read_op.dummy.buswidth = SPI_MEM_BUSWIDTH_1_LINE;
  171. device->read_op.data.buswidth = SPI_MEM_BUSWIDTH_4_LINE;
  172. device->read_op.data.dir = SPI_MEM_DATA_IN;
  173. return 0;
  174. }
  175. #endif