blob: 211f894171aef847a6541dd615787bc38ba65388 [file] [log] [blame]
Deepa Dinamanie4573be2012-08-03 16:32:29 -07001/*
2 * Copyright (c) 2008, Google Inc.
3 * All rights reserved.
Tanya Brokhman72b44dc2015-01-07 10:20:05 +02004 * Copyright (c) 2009-2015, The Linux Foundation. All rights reserved.
Deepa Dinamanie4573be2012-08-03 16:32:29 -07005 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
23 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#ifndef __QPIC_NAND_H
31#define __QPIC_NAND_H
32
33#include <debug.h>
34#include <dev/flash.h>
35#include <platform/iomap.h>
36
37#define NAND_REG(off) (nand_base + (off))
38
39#define NAND_FLASH_CMD NAND_REG(0x0000)
40#define NAND_ADDR0 NAND_REG(0x0004)
41#define NAND_ADDR1 NAND_REG(0x0008)
42#define NAND_FLASH_CHIP_SELECT NAND_REG(0x000C)
43#define NAND_EXEC_CMD NAND_REG(0x0010)
44#define NAND_FLASH_STATUS NAND_REG(0x0014)
45#define NAND_BUFFER_STATUS NAND_REG(0x0018)
46#define NAND_DEV0_CFG0 NAND_REG(0x0020)
47#define NAND_DEV0_CFG1 NAND_REG(0x0024)
48#define NAND_DEV0_ECC_CFG NAND_REG(0x0028)
49#define NAND_DEV1_CFG0 NAND_REG(0x0030)
50#define NAND_DEV1_CFG1 NAND_REG(0x0034)
51#define NAND_SFLASHC_CMD NAND_REG(0x0038)
52#define NAND_SFLASHC_EXEC_CMD NAND_REG(0x003C)
53#define NAND_READ_ID NAND_REG(0x0040)
54#define NAND_READ_STATUS NAND_REG(0x0044)
Sridhar Parasuramfb0d9c82015-02-02 15:23:13 -080055#define NAND_READ_ID2 NAND_REG(0x0048)
Deepa Dinamanie4573be2012-08-03 16:32:29 -070056#define NAND_CONFIG_DATA NAND_REG(0x0050)
57#define NAND_CONFIG NAND_REG(0x0054)
58#define NAND_CONFIG_MODE NAND_REG(0x0058)
59#define NAND_CONFIG_STATUS NAND_REG(0x0060)
60#define NAND_MACRO1_REG NAND_REG(0x0064)
61#define NAND_XFR_STEP1 NAND_REG(0x0070)
62#define NAND_XFR_STEP2 NAND_REG(0x0074)
63#define NAND_XFR_STEP3 NAND_REG(0x0078)
64#define NAND_XFR_STEP4 NAND_REG(0x007C)
65#define NAND_XFR_STEP5 NAND_REG(0x0080)
66#define NAND_XFR_STEP6 NAND_REG(0x0084)
67#define NAND_XFR_STEP7 NAND_REG(0x0088)
68#define NAND_GENP_REG0 NAND_REG(0x0090)
69#define NAND_GENP_REG1 NAND_REG(0x0094)
70#define NAND_GENP_REG2 NAND_REG(0x0098)
71#define NAND_GENP_REG3 NAND_REG(0x009C)
72#define NAND_SFLASHC_STATUS NAND_REG(0x001C)
73#define NAND_DEV_CMD0 NAND_REG(0x00A0)
74#define NAND_DEV_CMD1 NAND_REG(0x00A4)
75#define NAND_DEV_CMD2 NAND_REG(0x00A8)
76#define NAND_DEV_CMD_VLD NAND_REG(0x00AC)
77#define NAND_EBI2_MISR_SIG_REG NAND_REG(0x00B0)
78#define NAND_ADDR2 NAND_REG(0x00C0)
79#define NAND_ADDR3 NAND_REG(0x00C4)
80#define NAND_ADDR4 NAND_REG(0x00C8)
81#define NAND_ADDR5 NAND_REG(0x00CC)
82#define NAND_DEV_CMD3 NAND_REG(0x00D0)
83#define NAND_DEV_CMD4 NAND_REG(0x00D4)
84#define NAND_DEV_CMD5 NAND_REG(0x00D8)
85#define NAND_DEV_CMD6 NAND_REG(0x00DC)
86#define NAND_SFLASHC_BURST_CFG NAND_REG(0x00E0)
87#define NAND_ADDR6 NAND_REG(0x00E4)
Deepa Dinamani19530062012-10-03 14:43:05 -070088#define NAND_ERASED_CW_DETECT_CFG NAND_REG(0x00E8)
89#define NAND_ERASED_CW_DETECT_STATUS NAND_REG(0x00EC)
Deepa Dinamanie4573be2012-08-03 16:32:29 -070090#define NAND_EBI2_ECC_BUF_CFG NAND_REG(0x00F0)
Deepa Dinamanie4573be2012-08-03 16:32:29 -070091#define NAND_HW_INFO NAND_REG(0x00FC)
Deepa Dinamani19530062012-10-03 14:43:05 -070092#define NAND_FLASH_BUFFER NAND_REG(0x0100)
Deepa Dinamanie4573be2012-08-03 16:32:29 -070093
94/* NANDc registers used during BAM transfer */
95#define NAND_READ_LOCATION_n(n) (NAND_REG(0xF20) + 4 * (n))
96#define NAND_RD_LOC_LAST_BIT(x) ((x) << 31)
97#define NAND_RD_LOC_SIZE(x) ((x) << 16)
98#define NAND_RD_LOC_OFFSET(x) ((x) << 0)
99
100/* Shift Values */
101#define NAND_DEV0_CFG1_WIDE_BUS_SHIFT 1
102#define NAND_DEV0_CFG0_DIS_STS_AFTER_WR_SHIFT 4
103#define NAND_DEV0_CFG0_CW_PER_PAGE_SHIFT 6
104#define NAND_DEV0_CFG0_UD_SIZE_BYTES_SHIFT 9
105#define NAND_DEV0_CFG0_ADDR_CYCLE_SHIFT 27
106#define NAND_DEV0_CFG0_SPARE_SZ_BYTES_SHIFT 23
107
108#define NAND_DEV0_CFG1_RECOVERY_CYCLES_SHIFT 2
109#define NAND_DEV0_CFG1_CS_ACTIVE_BSY_SHIFT 5
110#define NAND_DEV0_CFG1_BAD_BLK_BYTE_NUM_SHIFT 6
111#define NAND_DEV0_CFG1_BAD_BLK_IN_SPARE_SHIFT 16
112#define NAND_DEV0_CFG1_WR_RD_BSY_GAP_SHIFT 17
113#define NAND_DEV0_ECC_DISABLE_SHIFT 0
114#define NAND_DEV0_ECC_SW_RESET_SHIFT 1
115#define NAND_DEV0_ECC_MODE_SHIFT 4
116#define NAND_DEV0_ECC_DISABLE_SHIFT 0
117#define NAND_DEV0_ECC_PARITY_SZ_BYTES_SHIFT 8
118#define NAND_DEV0_ECC_NUM_DATA_BYTES 16
119#define NAND_DEV0_ECC_FORCE_CLK_OPEN_SHIFT 30
120
Deepa Dinamani19530062012-10-03 14:43:05 -0700121#define NAND_ERASED_CW_DETECT_STATUS_PAGE_ALL_ERASED 7
122#define NAND_ERASED_CW_DETECT_STATUS_CODEWORD_ALL_ERASED 6
123#define NAND_ERASED_CW_DETECT_STATUS_CODEWORD_ERASED 4
124
Channagoud Kadabib1dc47c2015-05-21 17:02:44 -0700125#define NAND_ERASED_CW (BIT(NAND_ERASED_CW_DETECT_STATUS_CODEWORD_ERASED) | \
126 BIT(NAND_ERASED_CW_DETECT_STATUS_CODEWORD_ALL_ERASED))
Deepa Dinamani19530062012-10-03 14:43:05 -0700127#define NAND_ERASED_CW_DETECT_CFG_RESET_CTRL 1
128#define NAND_ERASED_CW_DETECT_CFG_ACTIVATE_CTRL 0
129#define NAND_ERASED_CW_DETECT_ERASED_CW_ECC_MASK (1 << 1)
130#define NAND_ERASED_CW_DETECT_ERASED_CW_ECC_NO_MASK (0 << 1)
131
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700132/* device commands */
133#define NAND_CMD_SOFT_RESET 0x01
134#define NAND_CMD_PAGE_READ 0x32
135#define NAND_CMD_PAGE_READ_ECC 0x33
136#define NAND_CMD_PAGE_READ_ALL 0x34
137#define NAND_CMD_SEQ_PAGE_READ 0x15
138#define NAND_CMD_PRG_PAGE 0x36
139#define NAND_CMD_PRG_PAGE_ECC 0x37
140#define NAND_CMD_PRG_PAGE_ALL 0x39
141#define NAND_CMD_BLOCK_ERASE 0x3A
142#define NAND_CMD_FETCH_ID 0x0B
143#define NAND_CMD_STATUS 0x0C
144#define NAND_CMD_RESET 0x0D
145
146/* NAND Status errors */
147#define NAND_FLASH_MPU_ERR (1 << 8)
148#define NAND_FLASH_TIMEOUT_ERR (1 << 6)
149#define NAND_FLASH_OP_ERR (1 << 4)
150
151#define NAND_FLASH_ERR (NAND_FLASH_MPU_ERR | \
Deepa Dinamani19530062012-10-03 14:43:05 -0700152 NAND_FLASH_TIMEOUT_ERR | \
153 NAND_FLASH_OP_ERR)
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700154
155#define PROG_ERASE_OP_RESULT (1 << 7)
156
157#define DATA_CONSUMER_PIPE_INDEX 0
158#define DATA_PRODUCER_PIPE_INDEX 1
159#define CMD_PIPE_INDEX 2
160
161/* Define BAM pipe lock groups for NANDc*/
162#define P_LOCK_GROUP_0 0
163
164/* Define BAM pipe lock super groups for NANDc
165 * Note: This is configured by TZ.
166 */
167#define P_LOCK_SUPERGROUP_0 0
168#define P_LOCK_SUPERGROUP_1 1
169
170#define ONFI_SIGNATURE 0x49464E4F
171
172#define ONFI_CRC_POLYNOMIAL 0x8005
173#define ONFI_CRC_INIT_VALUE 0x4F4E
174
175#define ONFI_READ_PARAM_PAGE_ADDR_CYCLES 1
176#define ONFI_READ_ID_ADDR_CYCLES 1
177
178#define ONFI_READ_ID_CMD 0x90
179#define ONFI_READ_PARAM_PAGE_CMD 0xEC
180#define ONFI_READ_ID_ADDR 0x20
181#define ONFI_READ_PARAM_PAGE_ADDR 0x00
182
183#define NAND_CFG0_RAW_ONFI_ID 0x88000800
184#define NAND_CFG0_RAW_ONFI_PARAM_PAGE 0x88040000
185#define NAND_CFG1_RAW_ONFI_ID 0x0005045D
186#define NAND_CFG1_RAW_ONFI_PARAM_PAGE 0x0005045D
187#define NAND_CFG0 0x290409c0
188#define NAND_CFG1 0x08045d5c
189#define NAND_ECC_BCH_CFG 0x42040d10
190#define NAND_Bad_Block 0x00000175
191#define NAND_ECC_BUF_CFG 0x00000203
192
193#define ONFI_READ_ID_BUFFER_SIZE 0x4
194#define ONFI_READ_PARAM_PAGE_BUFFER_SIZE 0x200
195#define ONFI_PARAM_PAGE_SIZE 0x100
196
197#define NAND_8BIT_DEVICE 0x01
198#define NAND_16BIT_DEVICE 0x02
199
200#define NAND_CW_SIZE_4_BIT_ECC 528
201#define NAND_CW_SIZE_8_BIT_ECC 532
202
203/* Indicates the data bytes in the user data portion of the code word. */
204#define USER_DATA_BYTES_PER_CW 512
205
206/* Indicates the number of bytes covered by BCH ECC logic when
207 * a codeword is written to a NAND flash device.
208 * This is also the number of bytes that are part of the image in CW.
209 * 516 bytes = (512 bytes of user data and 4 bytes of spare data)
210 */
211#define DATA_BYTES_IN_IMG_PER_CW 516
212
213#define NAND_CW_DIV_RIGHT_SHIFT 9
214
Deepa Dinamani16663a62013-02-07 16:25:59 -0800215/* Number of max cw's the driver allows to flash. */
216#define QPIC_NAND_MAX_CWS_IN_PAGE 10
217
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700218/* Reset Values for Status registers */
219#define NAND_FLASH_STATUS_RESET 0x00000020
220#define NAND_READ_STATUS_RESET 0x000000C0
221
222/* result type */
223typedef enum {
224 NANDC_RESULT_SUCCESS = 0,
225 NANDC_RESULT_FAILURE = 1,
226 NANDC_RESULT_TIMEOUT = 2,
227 NANDC_RESULT_PARAM_INVALID = 3,
228 NANDC_RESULT_DEV_NOT_SUPPORTED = 4,
229 NANDC_RESULT_BAD_PAGE = 5,
230 NANDC_RESULT_BAD_BLOCK = 6,
231} nand_result_t;
232
233enum nand_bad_block_value
234{
235 NAND_BAD_BLK_VALUE_NOT_READ,
236 NAND_BAD_BLK_VALUE_IS_BAD,
237 NAND_BAD_BLK_VALUE_IS_GOOD,
238};
239
240enum nand_cfg_value
241{
242 NAND_CFG_RAW,
243 NAND_CFG,
244};
245
246struct onfi_param_page
247{
248 uint32_t signature;
249 uint16_t rev;
250 uint16_t feature_supported;
251 uint16_t opt_cmd_supported;
252 uint8_t reserved_1[22];
253 uint8_t mib[12];
254 uint8_t device_model[20];
255 uint8_t manufacturer_id;
256 uint16_t date_code;
257 uint8_t reserved_2[13];
258 uint32_t data_per_pg;
259 uint16_t spare_per_pg;
260 uint32_t data_per_partial_pg;
261 uint16_t spare_per_partial_pg;
262 uint32_t pgs_per_blk;
263 uint32_t blks_per_LUN;
264 uint8_t num_LUN;
265 uint8_t num_addr_cycles;
266 uint8_t num_bits_per_cell;
267 uint16_t bad_blks_max_per_LUN;
268 uint16_t blk_endurance;
269 uint8_t guaranteed_vld_blks_at_start;
270 uint16_t blk_endurance_for_garunteed_vld_blks;
271 uint8_t num_prg_per_pg;
272 uint8_t partial_prog_attr;
273 uint8_t num_bits_ecc_correctability;
274 uint8_t num_interleaved_addr_bits;
275 uint8_t interleaved_op_attr;
276 uint8_t reserved_3[13];
277 uint8_t io_pin_capcacitance;
278 uint16_t timing_mode_support;
279 uint16_t prog_cache_timing_mode_support;
280 uint16_t max_pg_prog_time_us;
281 uint16_t max_blk_erase_time_us;
282 uint16_t max_pr_rd_time_us;
283 uint16_t min_chg_col_setup_time_us;
284 uint8_t reserved_4[23];
285 uint16_t vendor_rev;
286 uint8_t vendor_specific[88];
287 uint16_t interity_crc;
288}__PACKED;
289
290struct cfg_params
291{
292 uint32_t addr0;
293 uint32_t addr1;
294 uint32_t cfg0;
295 uint32_t cfg1;
296 uint32_t cmd;
297 uint32_t ecc_cfg;
298 uint32_t addr_loc_0;
299 uint32_t exec;
300};
301
302struct onfi_probe_params
303{
304 uint32_t vld;
305 uint32_t dev_cmd1;
306 struct cfg_params cfg;
307};
308
309/* This stucture is used to create a static list of devices we support.
310 * This include a subset of values defined in the flash_info struct as
311 * other values can be derived.
312 */
313struct flash_id
314{
315 unsigned flash_id;
Sridhar Parasuramfb0d9c82015-02-02 15:23:13 -0800316 unsigned flash_id2;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700317 unsigned mask;
Sridhar Parasuramfb0d9c82015-02-02 15:23:13 -0800318 unsigned mask2;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700319 unsigned density;
320 unsigned widebus;
321 unsigned pagesize;
322 unsigned blksize;
323 unsigned oobsize;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700324 unsigned ecc_8_bits;
325};
326
327/* Structure to hold the pipe numbers */
328struct qpic_nand_bam_pipes
329{
330 unsigned read_pipe;
331 unsigned write_pipe;
332 unsigned cmd_pipe;
Deepa Dinamani536d3f82013-07-09 13:05:56 -0700333 uint8_t read_pipe_grp;
334 uint8_t write_pipe_grp;
335 uint8_t cmd_pipe_grp;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700336};
337
338/* Structure to define the initial nand config */
339struct qpic_nand_init_config
340{
341 uint32_t nand_base;
342 uint32_t bam_base;
Deepa Dinamanie9ded132012-11-27 15:03:38 -0800343 uint32_t ee;
344 uint32_t max_desc_len;
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700345 struct qpic_nand_bam_pipes pipes;
346};
347
348void
349qpic_nand_init(struct qpic_nand_init_config *config);
Sridhar Parasuramc9abcdd2014-12-29 13:43:28 -0800350unsigned flash_num_blocks(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700351unsigned
352flash_block_size(void);
Deepa Dinamani87feab82012-10-04 14:28:05 -0700353void
354qpic_nand_uninit();
Smita Ghoshf5431c62014-09-18 14:11:14 -0700355/* Api to return the nand base */
356uint32_t nand_device_base();
Tanya Brokhman72b44dc2015-01-07 10:20:05 +0200357nand_result_t qpic_nand_read(uint32_t start_page, uint32_t num_pages,
358 unsigned char* buffer, unsigned char* spareaddr);
359nand_result_t qpic_nand_write(uint32_t start_page, uint32_t num_pages,
360 unsigned char* buffer, unsigned write_extra_bytes);
361nand_result_t qpic_nand_block_isbad(unsigned page);
362nand_result_t qpic_nand_blk_erase(uint32_t page);
Deepa Dinamanie4573be2012-08-03 16:32:29 -0700363
364#endif