blob: 3a8a27ac912c08a99efa59c07e5adf26efa21f4a [file] [log] [blame]
Lionel Debieve402a46b2019-11-04 12:28:15 +01001/*
Yann Gautier4359c9a2022-01-27 13:55:18 +01002 * Copyright (c) 2019-2022, STMicroelectronics - All Rights Reserved
Lionel Debieve402a46b2019-11-04 12:28:15 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Lionel Debieve129f6512021-04-13 17:11:00 +02007#include <assert.h>
Lionel Debieve402a46b2019-11-04 12:28:15 +01008#include <errno.h>
9
Yann Gautier4359c9a2022-01-27 13:55:18 +010010#include <common/debug.h>
Lionel Debieve402a46b2019-11-04 12:28:15 +010011#include <drivers/nand.h>
Madhukar Pappireddyfc9b4112019-12-23 14:49:52 -060012#include <drivers/raw_nand.h>
13#include <drivers/spi_nand.h>
14#include <drivers/spi_nor.h>
Lionel Debieve402a46b2019-11-04 12:28:15 +010015#include <lib/utils.h>
16#include <plat/common/platform.h>
17
Lionel Debieve186b0462019-09-24 18:30:12 +020018#if STM32MP_RAW_NAND || STM32MP_SPI_NAND
Lionel Debieve129f6512021-04-13 17:11:00 +020019#if STM32MP13
20void plat_get_scratch_buffer(void **buffer_addr, size_t *buf_size)
21{
22 assert(buffer_addr != NULL);
23 assert(buf_size != NULL);
24
25 *buffer_addr = (void *)STM32MP_MTD_BUFFER;
26 *buf_size = PLATFORM_MTD_MAX_PAGE_SIZE;
27}
28#endif
29
Lionel Debieve186b0462019-09-24 18:30:12 +020030static int get_data_from_otp(struct nand_device *nand_dev, bool is_slc)
Lionel Debieve402a46b2019-11-04 12:28:15 +010031{
Lionel Debieve402a46b2019-11-04 12:28:15 +010032 uint32_t nand_param;
Yann Gautierbde43da2021-08-18 15:03:40 +020033 uint32_t nand2_param __maybe_unused;
Lionel Debieve402a46b2019-11-04 12:28:15 +010034
35 /* Check if NAND parameters are stored in OTP */
Lionel Debievebc2d88d2019-11-04 14:31:38 +010036 if (stm32_get_otp_value(NAND_OTP, &nand_param) != 0) {
37 ERROR("BSEC: NAND_OTP Error\n");
Lionel Debieve402a46b2019-11-04 12:28:15 +010038 return -EACCES;
39 }
40
41 if (nand_param == 0U) {
Yann Gautierbde43da2021-08-18 15:03:40 +020042#if STM32MP13
43 if (is_slc) {
44 return 0;
45 }
46#endif
47#if STM32MP15
Lionel Debieve402a46b2019-11-04 12:28:15 +010048 return 0;
Yann Gautierbde43da2021-08-18 15:03:40 +020049#endif
Lionel Debieve402a46b2019-11-04 12:28:15 +010050 }
51
52 if ((nand_param & NAND_PARAM_STORED_IN_OTP) == 0U) {
Yann Gautierbde43da2021-08-18 15:03:40 +020053#if STM32MP13
54 if (is_slc) {
55 goto ecc;
56 }
57#endif
58#if STM32MP15
Lionel Debieve402a46b2019-11-04 12:28:15 +010059 goto ecc;
Yann Gautierbde43da2021-08-18 15:03:40 +020060#endif
61 }
62
63#if STM32MP13
64 if (stm32_get_otp_value(NAND2_OTP, &nand2_param) != 0) {
65 ERROR("BSEC: NAND_OTP Error\n");
66 return -EACCES;
67 }
68
69 /* Check OTP configuration for this device */
70 if ((((nand2_param & NAND2_CONFIG_DISTRIB) == NAND2_PNAND_NAND1_SNAND_NAND2) && !is_slc) ||
71 (((nand2_param & NAND2_CONFIG_DISTRIB) == NAND2_PNAND_NAND2_SNAND_NAND1) && is_slc)) {
72 nand_param = nand2_param << (NAND_PAGE_SIZE_SHIFT - NAND2_PAGE_SIZE_SHIFT);
Lionel Debieve402a46b2019-11-04 12:28:15 +010073 }
Yann Gautierbde43da2021-08-18 15:03:40 +020074#endif
Lionel Debieve402a46b2019-11-04 12:28:15 +010075
76 /* NAND parameter shall be read from OTP */
77 if ((nand_param & NAND_WIDTH_MASK) != 0U) {
78 nand_dev->buswidth = NAND_BUS_WIDTH_16;
79 } else {
80 nand_dev->buswidth = NAND_BUS_WIDTH_8;
81 }
82
83 switch ((nand_param & NAND_PAGE_SIZE_MASK) >> NAND_PAGE_SIZE_SHIFT) {
84 case NAND_PAGE_SIZE_2K:
85 nand_dev->page_size = 0x800U;
86 break;
87
88 case NAND_PAGE_SIZE_4K:
89 nand_dev->page_size = 0x1000U;
90 break;
91
92 case NAND_PAGE_SIZE_8K:
93 nand_dev->page_size = 0x2000U;
94 break;
95
96 default:
97 ERROR("Cannot read NAND page size\n");
98 return -EINVAL;
99 }
100
101 switch ((nand_param & NAND_BLOCK_SIZE_MASK) >> NAND_BLOCK_SIZE_SHIFT) {
102 case NAND_BLOCK_SIZE_64_PAGES:
103 nand_dev->block_size = 64U * nand_dev->page_size;
104 break;
105
106 case NAND_BLOCK_SIZE_128_PAGES:
107 nand_dev->block_size = 128U * nand_dev->page_size;
108 break;
109
110 case NAND_BLOCK_SIZE_256_PAGES:
111 nand_dev->block_size = 256U * nand_dev->page_size;
112 break;
113
114 default:
115 ERROR("Cannot read NAND block size\n");
116 return -EINVAL;
117 }
118
119 nand_dev->size = ((nand_param & NAND_BLOCK_NB_MASK) >>
120 NAND_BLOCK_NB_SHIFT) *
121 NAND_BLOCK_NB_UNIT * nand_dev->block_size;
122
123ecc:
Lionel Debieve186b0462019-09-24 18:30:12 +0200124 if (is_slc) {
125 switch ((nand_param & NAND_ECC_BIT_NB_MASK) >>
126 NAND_ECC_BIT_NB_SHIFT) {
127 case NAND_ECC_BIT_NB_1_BITS:
128 nand_dev->ecc.max_bit_corr = 1U;
129 break;
Lionel Debieve402a46b2019-11-04 12:28:15 +0100130
Lionel Debieve186b0462019-09-24 18:30:12 +0200131 case NAND_ECC_BIT_NB_4_BITS:
132 nand_dev->ecc.max_bit_corr = 4U;
133 break;
Lionel Debieve402a46b2019-11-04 12:28:15 +0100134
Lionel Debieve186b0462019-09-24 18:30:12 +0200135 case NAND_ECC_BIT_NB_8_BITS:
136 nand_dev->ecc.max_bit_corr = 8U;
137 break;
Lionel Debieve402a46b2019-11-04 12:28:15 +0100138
Lionel Debieve186b0462019-09-24 18:30:12 +0200139 case NAND_ECC_ON_DIE:
140 nand_dev->ecc.mode = NAND_ECC_ONDIE;
141 break;
Lionel Debieve402a46b2019-11-04 12:28:15 +0100142
Lionel Debieve186b0462019-09-24 18:30:12 +0200143 default:
144 if (nand_dev->ecc.max_bit_corr == 0U) {
145 ERROR("No valid eccbit number\n");
146 return -EINVAL;
147 }
148 }
149 } else {
150 /* Selected multiple plane NAND */
151 if ((nand_param & NAND_PLANE_BIT_NB_MASK) != 0U) {
152 nand_dev->nb_planes = 2U;
153 } else {
154 nand_dev->nb_planes = 1U;
Lionel Debieve402a46b2019-11-04 12:28:15 +0100155 }
156 }
157
Yann Gautier37bea082022-02-14 11:10:59 +0100158 VERBOSE("OTP: Block %u Page %u Size %llu\n", nand_dev->block_size,
159 nand_dev->page_size, nand_dev->size);
Lionel Debieve402a46b2019-11-04 12:28:15 +0100160
161 return 0;
162}
Lionel Debieve186b0462019-09-24 18:30:12 +0200163#endif /* STM32MP_RAW_NAND || STM32MP_SPI_NAND */
Lionel Debieve402a46b2019-11-04 12:28:15 +0100164
165#if STM32MP_RAW_NAND
166int plat_get_raw_nand_data(struct rawnand_device *device)
167{
168 device->nand_dev->ecc.mode = NAND_ECC_HW;
169 device->nand_dev->ecc.size = SZ_512;
170
Lionel Debieve186b0462019-09-24 18:30:12 +0200171 return get_data_from_otp(device->nand_dev, true);
172}
173#endif
174
175#if STM32MP_SPI_NAND
176int plat_get_spi_nand_data(struct spinand_device *device)
177{
178 zeromem(&device->spi_read_cache_op, sizeof(struct spi_mem_op));
179 device->spi_read_cache_op.cmd.opcode = SPI_NAND_OP_READ_FROM_CACHE_4X;
180 device->spi_read_cache_op.cmd.buswidth = SPI_MEM_BUSWIDTH_1_LINE;
181 device->spi_read_cache_op.addr.nbytes = 2U;
182 device->spi_read_cache_op.addr.buswidth = SPI_MEM_BUSWIDTH_1_LINE;
183 device->spi_read_cache_op.dummy.nbytes = 1U;
184 device->spi_read_cache_op.dummy.buswidth = SPI_MEM_BUSWIDTH_1_LINE;
185 device->spi_read_cache_op.data.buswidth = SPI_MEM_BUSWIDTH_4_LINE;
186 device->spi_read_cache_op.data.dir = SPI_MEM_DATA_IN;
187
188 return get_data_from_otp(device->nand_dev, false);
Lionel Debieve402a46b2019-11-04 12:28:15 +0100189}
190#endif
191
Lionel Debievecb0dbc42019-09-25 09:11:31 +0200192#if STM32MP_SPI_NOR
193int plat_get_nor_data(struct nor_device *device)
194{
195 device->size = SZ_64M;
196
197 zeromem(&device->read_op, sizeof(struct spi_mem_op));
198 device->read_op.cmd.opcode = SPI_NOR_OP_READ_1_1_4;
199 device->read_op.cmd.buswidth = SPI_MEM_BUSWIDTH_1_LINE;
200 device->read_op.addr.nbytes = 3U;
201 device->read_op.addr.buswidth = SPI_MEM_BUSWIDTH_1_LINE;
202 device->read_op.dummy.nbytes = 1U;
203 device->read_op.dummy.buswidth = SPI_MEM_BUSWIDTH_1_LINE;
204 device->read_op.data.buswidth = SPI_MEM_BUSWIDTH_4_LINE;
205 device->read_op.data.dir = SPI_MEM_DATA_IN;
206
207 return 0;
208}
209#endif