blob: fa352f341f4d465203fea4be928486b844f12444 [file] [log] [blame]
Lionel Debieve402a46b2019-11-04 12:28:15 +01001/*
2 * Copyright (c) 2019, STMicroelectronics - All Rights Reserved
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <errno.h>
8
9#include <drivers/nand.h>
10#include <lib/utils.h>
11#include <plat/common/platform.h>
12
13#define SZ_512 0x200U
14
Lionel Debieve186b0462019-09-24 18:30:12 +020015#if STM32MP_RAW_NAND || STM32MP_SPI_NAND
16static int get_data_from_otp(struct nand_device *nand_dev, bool is_slc)
Lionel Debieve402a46b2019-11-04 12:28:15 +010017{
18 int result;
19 uint32_t nand_param;
20
21 /* Check if NAND parameters are stored in OTP */
22 result = bsec_shadow_read_otp(&nand_param, NAND_OTP);
23 if (result != BSEC_OK) {
24 ERROR("BSEC: NAND_OTP Error %i\n", result);
25 return -EACCES;
26 }
27
28 if (nand_param == 0U) {
29 return 0;
30 }
31
32 if ((nand_param & NAND_PARAM_STORED_IN_OTP) == 0U) {
33 goto ecc;
34 }
35
36 /* NAND parameter shall be read from OTP */
37 if ((nand_param & NAND_WIDTH_MASK) != 0U) {
38 nand_dev->buswidth = NAND_BUS_WIDTH_16;
39 } else {
40 nand_dev->buswidth = NAND_BUS_WIDTH_8;
41 }
42
43 switch ((nand_param & NAND_PAGE_SIZE_MASK) >> NAND_PAGE_SIZE_SHIFT) {
44 case NAND_PAGE_SIZE_2K:
45 nand_dev->page_size = 0x800U;
46 break;
47
48 case NAND_PAGE_SIZE_4K:
49 nand_dev->page_size = 0x1000U;
50 break;
51
52 case NAND_PAGE_SIZE_8K:
53 nand_dev->page_size = 0x2000U;
54 break;
55
56 default:
57 ERROR("Cannot read NAND page size\n");
58 return -EINVAL;
59 }
60
61 switch ((nand_param & NAND_BLOCK_SIZE_MASK) >> NAND_BLOCK_SIZE_SHIFT) {
62 case NAND_BLOCK_SIZE_64_PAGES:
63 nand_dev->block_size = 64U * nand_dev->page_size;
64 break;
65
66 case NAND_BLOCK_SIZE_128_PAGES:
67 nand_dev->block_size = 128U * nand_dev->page_size;
68 break;
69
70 case NAND_BLOCK_SIZE_256_PAGES:
71 nand_dev->block_size = 256U * nand_dev->page_size;
72 break;
73
74 default:
75 ERROR("Cannot read NAND block size\n");
76 return -EINVAL;
77 }
78
79 nand_dev->size = ((nand_param & NAND_BLOCK_NB_MASK) >>
80 NAND_BLOCK_NB_SHIFT) *
81 NAND_BLOCK_NB_UNIT * nand_dev->block_size;
82
83ecc:
Lionel Debieve186b0462019-09-24 18:30:12 +020084 if (is_slc) {
85 switch ((nand_param & NAND_ECC_BIT_NB_MASK) >>
86 NAND_ECC_BIT_NB_SHIFT) {
87 case NAND_ECC_BIT_NB_1_BITS:
88 nand_dev->ecc.max_bit_corr = 1U;
89 break;
Lionel Debieve402a46b2019-11-04 12:28:15 +010090
Lionel Debieve186b0462019-09-24 18:30:12 +020091 case NAND_ECC_BIT_NB_4_BITS:
92 nand_dev->ecc.max_bit_corr = 4U;
93 break;
Lionel Debieve402a46b2019-11-04 12:28:15 +010094
Lionel Debieve186b0462019-09-24 18:30:12 +020095 case NAND_ECC_BIT_NB_8_BITS:
96 nand_dev->ecc.max_bit_corr = 8U;
97 break;
Lionel Debieve402a46b2019-11-04 12:28:15 +010098
Lionel Debieve186b0462019-09-24 18:30:12 +020099 case NAND_ECC_ON_DIE:
100 nand_dev->ecc.mode = NAND_ECC_ONDIE;
101 break;
Lionel Debieve402a46b2019-11-04 12:28:15 +0100102
Lionel Debieve186b0462019-09-24 18:30:12 +0200103 default:
104 if (nand_dev->ecc.max_bit_corr == 0U) {
105 ERROR("No valid eccbit number\n");
106 return -EINVAL;
107 }
108 }
109 } else {
110 /* Selected multiple plane NAND */
111 if ((nand_param & NAND_PLANE_BIT_NB_MASK) != 0U) {
112 nand_dev->nb_planes = 2U;
113 } else {
114 nand_dev->nb_planes = 1U;
Lionel Debieve402a46b2019-11-04 12:28:15 +0100115 }
116 }
117
118 VERBOSE("OTP: Block %i Page %i Size %lli\n", nand_dev->block_size,
119 nand_dev->page_size, nand_dev->size);
120
121 return 0;
122}
Lionel Debieve186b0462019-09-24 18:30:12 +0200123#endif /* STM32MP_RAW_NAND || STM32MP_SPI_NAND */
Lionel Debieve402a46b2019-11-04 12:28:15 +0100124
125#if STM32MP_RAW_NAND
126int plat_get_raw_nand_data(struct rawnand_device *device)
127{
128 device->nand_dev->ecc.mode = NAND_ECC_HW;
129 device->nand_dev->ecc.size = SZ_512;
130
Lionel Debieve186b0462019-09-24 18:30:12 +0200131 return get_data_from_otp(device->nand_dev, true);
132}
133#endif
134
135#if STM32MP_SPI_NAND
136int plat_get_spi_nand_data(struct spinand_device *device)
137{
138 zeromem(&device->spi_read_cache_op, sizeof(struct spi_mem_op));
139 device->spi_read_cache_op.cmd.opcode = SPI_NAND_OP_READ_FROM_CACHE_4X;
140 device->spi_read_cache_op.cmd.buswidth = SPI_MEM_BUSWIDTH_1_LINE;
141 device->spi_read_cache_op.addr.nbytes = 2U;
142 device->spi_read_cache_op.addr.buswidth = SPI_MEM_BUSWIDTH_1_LINE;
143 device->spi_read_cache_op.dummy.nbytes = 1U;
144 device->spi_read_cache_op.dummy.buswidth = SPI_MEM_BUSWIDTH_1_LINE;
145 device->spi_read_cache_op.data.buswidth = SPI_MEM_BUSWIDTH_4_LINE;
146 device->spi_read_cache_op.data.dir = SPI_MEM_DATA_IN;
147
148 return get_data_from_otp(device->nand_dev, false);
Lionel Debieve402a46b2019-11-04 12:28:15 +0100149}
150#endif
151