blob: 90589171261056cf98d02b51a48a9f16f8331e7a [file] [log] [blame]
Feng Chenee8403d2024-01-03 14:13:40 +08001/* SPDX-License-Identifier: (GPL-2.0+ OR MIT) */
2/*
3 * Copyright (c) 2019 Amlogic, Inc. All rights reserved.
4 */
5
6#ifndef __MESON_NAND_H_
7#define __MESON_NAND_H_
8
9#include <linux/mtd/mtd.h>
10#include <linux/mtd/rawnand.h>
11#include <linux/mtd/nand_ecc.h>
12#include <nand.h>
13#include "partition_table.h"
14#include <linux/mtd/partitions.h>
15#include "aml_nand.h"
16#include "aml_hwctrl.h"
17#include "aml_rsv.h"
18#include "aml_mtd.h"
19
20#define NAND_MAX_DEVICE 4
21/*MAX page list cnt for usrdef mode*/
22#define NAND_PAGELIST_CNT 16
23
24/*nand read retry info,max equal to zero,
25*that means no need retry*/
26struct nand_retry_t {
27 unsigned id;
28 unsigned max;
29 unsigned no_rb;
30};
31
32typedef struct _nand_cmd {
33 unsigned char type;
34 unsigned char val;
35} nand_cmd_t;
36
37struct meson_slcnand_platdata {
38 u32 reg_base;
39 u8 nand_user_mode;
40 u8 nand_ran_mode;
41};
42
43union sc2_cmdinfo {
44 uint32_t d32;
45 struct {
46 unsigned cmd:22; //20-21b' code0/1 17b' WE_n(r/w->1/0b)
47 unsigned page_list:1;
48 unsigned reserved:1;
49 unsigned new_type:8;
50 } b;
51};
52
53typedef struct nand_setup_sc2 {
54 union sc2_cmdinfo cfg;
55 uint16_t id;
56 uint16_t max; // id:0x100 user, max:0 disable.
57} nand_setup_sc2_t;
58
59union cmdinfo {
60 uint32_t d32;
61 struct {
62 unsigned cmd:22;
63 unsigned large_page:1; // 22
64 unsigned no_rb:1; // 23 from efuse
65 unsigned a2:1; // 24
66 unsigned reserved25:1; // 25
67 unsigned page_list:1; // 26
68 unsigned sync_mode:2; // 27 from efuse
69 unsigned size:2; // 29 from efuse
70 unsigned active:1; // 31
71 } b;
72};
73
74typedef struct nand_setup {
75 union cmdinfo cfg;
76 uint16_t id;
77 uint16_t max; // id:0x100 user, max:0 disable.
78} nand_setup_t;
79
80typedef struct _ext_info{
81 uint32_t read_info; //nand_read_info;
82 uint32_t new_type; //new_nand_type;
83 uint32_t page_per_blk; //pages_in_block;
84 uint32_t xlc; //slc=1,mlc=2,tlc=3;
85 uint32_t ce_mask;
86 /*copact mode: boot means whole uboot
87 it's easy to understand that copies off_type
88 bl2 and fip are the same.
89 * discrete mode,boot means the fip only*/
90 uint32_t boot_num;
91 uint32_t each_boot_pages;
92 /*for compatible reason*/
93 uint32_t bbt_occupy_pages;
94 uint32_t bbt_start_block;
95} ext_info_t;
96
97#define NAND_FIPMODE_COMPACT (0)
98#define NAND_FIPMODE_DISCRETE (1)
99
100/* if you don't need skip the bad blocks when address
101 * partitions,please enable this macro.
102 * #define CONFIG_NOT_SKIP_BAD_BLOCK
103 */
104typedef struct _fip_info {
105 uint16_t version; //version
106 uint16_t mode; //compact or discrete
107 uint32_t fip_start; //fip start,pages
108} fip_info_t;
109
110typedef struct _nand_page0_sc2 {
111 nand_setup_sc2_t nand_setup;
112 unsigned char page_list[32];
113 nand_cmd_t retry_usr[32];
114 ext_info_t ext_info;
115 fip_info_t fip_info;
116} nand_page0_sc2_t;
117
118 typedef struct _nand_page0 {
119 nand_setup_t nand_setup;
120 unsigned char page_list[16];
121 nand_cmd_t retry_usr[32];
122 ext_info_t ext_info;
123 fip_info_t fip_info;
124 uint32_t ddrp_start_page;
125 } nand_page0_t;
126
127typedef union nand_core_clk {
128 /*raw register data*/
129 uint32_t d32;
130 struct {
131 unsigned clk_div:7;
132 unsigned reserved0:1;
133 unsigned clk_en:1;
134 unsigned clk_sel:3;
135 unsigned not_used:20;
136 } b;
137} nand_core_clk_t;
138
139/***************ERROR CODING*******************/
140#define NAND_CHIP_ID_ERR 1
141#define NAND_SHIP_BAD_BLOCK_ERR 2
142#define NAND_CHIP_REVB_HY_ERR 3
143
144/** Register defination **/
145#define NAND_BOOT_NAME "bootloader"
146#define NAND_NORMAL_NAME "nandnormal"
147#define NAND_RESERVED_NAME "nandreserved"
148
Feng Chenee8403d2024-01-03 14:13:40 +0800149#define AML_CHIP_NONE_RB 4
150#define AML_INTERLEAVING_MODE 8
151
152#define AML_NAND_CE0 0xe
153#define AML_NAND_CE1 0xd
154#define AML_NAND_CE2 0xb
155#define AML_NAND_CE3 0x7
156
157#define AML_BADBLK_POS 0
158
159#define NAND_ECC_OPTIONS_MASK 0x0000000f
160#define NAND_PLANE_OPTIONS_MASK 0x000000f0
161#define NAND_TIMING_OPTIONS_MASK 0x00000f00
162#define NAND_BUSW_OPTIONS_MASK 0x0000f000
163#define NAND_INTERLEAVING_OPTIONS_MASK 0x000f0000
164
165#define NAND_TWO_PLANE_MODE 0x00000010
166#define NAND_TIMING_MODE0 0x00000000
167#define NAND_TIMING_MODE1 0x00000100
168#define NAND_TIMING_MODE2 0x00000200
169#define NAND_TIMING_MODE3 0x00000300
170#define NAND_TIMING_MODE4 0x00000400
171#define NAND_TIMING_MODE5 0x00000500
172#define NAND_INTERLEAVING_MODE 0x00010000
173
174#define DEFAULT_T_REA 40
175#define DEFAULT_T_RHOH 0
176#define NAND_DEFAULT_OPTIONS (NAND_TIMING_MODE5 | NAND_ECC_BCH8_MODE)
177
178#define AML_NAND_BUSY_TIMEOUT 0x40000
179#define AML_DMA_BUSY_TIMEOUT 0x100000
180#define MAX_ID_LEN 8
181
182#define NAND_CMD_PLANE2_READ_START 0x06
183#define NAND_CMD_TWOPLANE_PREVIOS_READ 0x60
184#define NAND_CMD_TWOPLANE_READ1 0x5a
185#define NAND_CMD_TWOPLANE_READ2 0xa5
186#define NAND_CMD_TWOPLANE_WRITE2_MICRO 0x80
187#define NAND_CMD_TWOPLANE_WRITE2 0x81
188#define NAND_CMD_DUMMY_PROGRAM 0x11
189#define NAND_CMD_ERASE1_END 0xd1
190#define NAND_CMD_MULTI_CHIP_STATUS 0x78
191
192#define ONFI_TIMING_ADDR 0x01
193
194#define NAND_STATUS_READY_MULTI 0x20
195
196#define NAND_BLOCK_GOOD 0
197#define NAND_BLOCK_BAD 1
198#define NAND_FACTORY_BAD 2
199#define BAD_BLK_LEVEL 2
200#define FACTORY_BAD_BLOCK_ERROR 159
201#define MINI_PART_SIZE 0x100000
202#define NAND_MINI_PART_NUM 4
203#define MAX_BAD_BLK_NUM 2000
204#define MAX_MTD_PART_NUM 16
205#define MAX_MTD_PART_NAME_LEN 24
206
207#define NAND_SYS_PART_SIZE 0x8000000
208
209struct aml_nand_flash_dev {
210 char *name;
211 u8 id[MAX_ID_LEN];
212 unsigned pagesize;
213 unsigned chipsize;
214 unsigned erasesize;
215 unsigned oobsize;
216 unsigned internal_chipnr;
217 unsigned T_REA;
218 unsigned T_RHOH;
219 u8 onfi_mode;
220 unsigned options;
221};
222
223struct aml_nand_part_info {
224 char mtd_part_magic[4];
225 char mtd_part_name[MAX_MTD_PART_NAME_LEN];
226 uint64_t size;
227 uint64_t offset;
228 u_int32_t mask_flags;
229};
230
231struct aml_nand_bch_desc {
232 char *name;
233 unsigned bch_mode;
234 unsigned bch_unit_size;
235 unsigned bch_bytes;
236 unsigned user_byte_mode;
237};
238
239struct aml_nand_chip {
240 struct nand_chip chip;
241 struct hw_controller *controller;
242
243 /* mtd info */
244 u8 mfr_type;
245 unsigned onfi_mode;
246 unsigned T_REA;
247 unsigned T_RHOH;
248 unsigned options;
249 unsigned page_size;
250 unsigned block_size;
251 unsigned oob_size;
252 unsigned virtual_page_size;
253 unsigned virtual_block_size;
254 u8 plane_num;
255 u8 internal_chipnr;
256 unsigned internal_page_nums;
257
258 unsigned internal_chip_shift;
259 unsigned int ran_mode;
260 unsigned int rbpin_mode;
261 unsigned int rbpin_detect;
262 unsigned int short_pgsz;
263 /* bch for infopage on short mode */
264 unsigned int bch_info;
265
266 unsigned bch_mode;
267 u8 user_byte_mode;
268 u8 ops_mode;
269 u8 cached_prog_status;
270 u8 max_bch_mode;
271 unsigned valid_chip[MAX_CHIP_NUM];
272 unsigned page_addr;
273 unsigned char *aml_nand_data_buf;
274 unsigned int *user_info_buf;
275 int8_t *block_status;
276 unsigned int toggle_mode;
277 u8 ecc_cnt_limit;
278 u8 ecc_cnt_cur;
279 u8 ecc_max;
280 unsigned zero_cnt;
281 unsigned oob_fill_cnt;
282 unsigned boot_oob_fill_cnt;
283 /*add property field for key private data*/
284 int dtbsize;
285 int keysize;
286 uint32_t boot_copy_num; /*tell how many bootloader copies*/
287
288 u8 key_protect;
289 unsigned char *rsv_data_buf;
290
291 struct meson_rsv_handler_t *rsv;
292
293 struct aml_nand_bch_desc *bch_desc;
294 /* platform info */
295 struct aml_nand_platform *platform;
296
297 /* device info */
298 struct device *device;
299
300 unsigned max_ecc;
301 struct ecc_desc_s *ecc;
302 // unsigned onfi_mode;
303 unsigned err_sts;
304 /* plateform operation function*/
305 void (*aml_nand_hw_init)(struct aml_nand_chip *aml_chip);
306 void (*aml_nand_adjust_timing)(struct aml_nand_chip *aml_chip);
307 int (*aml_nand_options_confirm)(struct aml_nand_chip *aml_chip);
308 void (*aml_nand_cmd_ctrl)(struct aml_nand_chip *aml_chip,
309 int cmd, unsigned int ctrl);
310 void (*aml_nand_select_chip)(struct aml_nand_chip *aml_chip,
311 int chipnr);
312 void (*aml_nand_write_byte)(struct aml_nand_chip *aml_chip,
313 u8 data);
314 void (*aml_nand_get_user_byte)(struct aml_nand_chip *aml_chip,
315 unsigned char *oob_buf, int byte_num);
316 void (*aml_nand_set_user_byte)(struct aml_nand_chip *aml_chip,
317 unsigned char *oob_buf, int byte_num);
318 void (*aml_nand_command)(struct aml_nand_chip *aml_chip,
319 unsigned command, int column, int page_addr, int chipnr);
320 int (*aml_nand_wait_devready)(struct aml_nand_chip *aml_chip,
321 int chipnr);
322 int (*aml_nand_dma_read)(struct aml_nand_chip *aml_chip,
323 unsigned char *buf, int len, unsigned bch_mode);
324 int (*aml_nand_dma_write)(struct aml_nand_chip *aml_chip,
325 unsigned char *buf, int len, unsigned bch_mode);
326 int (*aml_nand_hwecc_correct)(struct aml_nand_chip *aml_chip,
327 unsigned char *buf, unsigned size, unsigned char *oob_buf);
328 int (*aml_nand_block_bad_scrub)(struct mtd_info *mtd);
329};
330
331struct aml_pre_scan {
332 u8 pre_scan_flag;
333 u8 is_nand;
334};
335
336
337struct aml_nand_platform {
338 struct aml_nand_flash_dev *nand_flash_dev;
339 char *name;
340 unsigned chip_enable_pad;
341 unsigned ready_busy_pad;
342
343 /* DMA RD/WR delay loop timing */
344 unsigned int T_REA; /* for dma wating delay */
345 /* not equal of (nandchip->delay, which is for dev ready func)*/
346 unsigned int T_RHOH;
347 unsigned int ran_mode; /*def close, for all part*/
348 unsigned int rbpin_mode; /*may get from romboot*/
349 unsigned int rbpin_detect;
350 unsigned int short_pgsz; /*zero means no short*/
351
352 struct aml_nand_chip *aml_chip;
353 struct platform_nand_data platform_nand_data;
354};
355
356struct aml_nand_device {
357 struct aml_nand_platform *aml_nand_platform;
358 u8 dev_num;
359};
360
361static inline struct aml_nand_chip *mtd_to_nand_chip(struct mtd_info *mtd)
362{
363 struct nand_chip *chip = mtd->priv;
364 return container_of(chip, struct aml_nand_chip, chip);
365}
366
367#ifdef CONFIG_PARAMETER_PAGE
368struct parameter_page {
369 /*0~31 byte: Revision information and features block*/
370 unsigned char signature[4];
371 unsigned short ver;
372 unsigned short feature;
373 unsigned short opt_commd;
374 unsigned short reserve0;
375 unsigned short ex_para_page_len;
376 unsigned char num_para_page;
377 unsigned char reserve1[17];
378 /*32~79 byte: Manufacturer information block*/
379 unsigned char dev_manu[12];
380 unsigned char dev_model[20];
381 unsigned char JEDEC_manu_ID;
382 unsigned short date_code;
383 unsigned char reserve2[13];
384 /*80~127 byte: Memory organization block*/
385 unsigned int data_bytes_perpage;
386 unsigned short spare_bytes_perpage;
387 unsigned int data_bytes_perpartial;
388 unsigned short spare_bytes_perpartial;
389 unsigned int pages_perblk;
390 unsigned int blks_perLUN;
391 unsigned char num_LUN;
392 /* 4-7: column addr cycles; 0-3: row addr cycles*/
393 unsigned char num_addr_cycle;
394 unsigned char bits_percell;
395 unsigned short max_badblk_perLUN;
396 unsigned short blk_edurce;
397 /*Guaranteed valid blocks at beginning of target*/
398 unsigned char g_v_blk_begin;
399 unsigned short blk_edurce_g_v_blk;
400 unsigned char progm_perpage;
401 unsigned char prt_prog_att;//obsolete
402 unsigned char bits_ECC_correctable;
403 /*0-3: number of interleaved address bits*/
404 unsigned char bits_intleav_addr;
405 /*6-7 Reserved (0)
406 5 1 = lower bit XNOR block address restriction
407 4 1 = read cache supported
408 3 Address restrictions for cache operations
409 2 1 = program cache supported
410 1 1 = no block address restrictions
411 0 Overlapped / concurrent interleaving support
412 */
413 unsigned char intleav_op_attr;
414 unsigned char reserve3[13];
415 /*128~163 byte: Electrical parameters block*/
416 unsigned char max_io_pin;
417 /*6-15 Reserved (0)
418 5 1 = supports timing mode 5
419 4 1 = supports timing mode 4
420 3 1 = supports timing mode 3
421 2 1 = supports timing mode 2
422 1 1 = supports timing mode 1
423 0 1 = supports timing mode 0, shall be 1
424 */
425 unsigned short asy_time_mode;
426 unsigned short asy_prog_cach_time_mode; /*obsolete*/
427 unsigned short Tprog; /*Maximum page program time (Ts)*/
428 unsigned short Tbers; /*Maximum block erase time (Ts)*/
429 unsigned short Tr; /*Maximum page read time (Ts)*/
430 unsigned short Tccs; /*Minimum change column setup time (ns)*/
431 /* 6-15 Reserved (0)
432 5 1 = supports timing mode 5
433 4 1 = supports timing mode 4
434 3 1 = supports timing mode 3
435 2 1 = supports timing mode 2
436 1 1 = supports timing mode 1
437 0 1 = supports timing mode 0
438 */
439 unsigned short src_syn_time_mode;
440 /*3-7 Reserved (0)
441 2 1 = device supports CLK stopped for data input
442 1 1 = typical capacitance values present
443 0 tCAD value to use
444 */
445 unsigned char src_syn_feature;
446 unsigned short CLK_input_pin;
447 unsigned short IO_pin;
448 unsigned short input_pin;
449 unsigned char max_input_pin;
450 unsigned char dr_strgth;
451 /*Maximum interleaved page read time (Ts)*/
452 unsigned short Tir;
453 /*Program page register clear enhancement tADL value (ns)*/
454 unsigned short Tadl;
455 unsigned char reserve4[8];
456 /*164~255 byte: Vendor block*/
457 unsigned short vd_ver;
458 unsigned char vd_spec[88];
459 unsigned short int_CRC;
460 /*256~ byte: Redundant Parameter Pages*/
461
462} __attribute__ ((__packed__));
463#endif
464
465extern unsigned char pagelist_1ynm_hynix256_mtd[128];
466extern struct hw_controller *controller;
467
468int meson_nfc_probe(struct udevice *dev);
469
470int aml_nand_init(struct aml_nand_chip *aml_chip);
471
472int aml_nand_read_page_raw(struct mtd_info *mtd,
473 struct nand_chip *chip, u8 *buf, int oob_required, int page);
474
475int aml_nand_write_page_raw(struct mtd_info *mtd,
476 struct nand_chip *chip, const u8 *buf, int oob_required,
477 int page);
478
479int aml_nand_read_page_hwecc(struct mtd_info *mtd,
480 struct nand_chip *chip, u8 *buf, int oob_required, int page);
481
482int aml_nand_write_page_hwecc(struct mtd_info *mtd,
483 struct nand_chip *chip, const u8 *buf, int oob_required,
484 int page);
485
486int aml_nand_read_oob(struct mtd_info *mtd,
487 struct nand_chip *chip, int page);
488
489int aml_nand_write_oob(struct mtd_info *mtd, struct nand_chip *chip, int page);
490
491int aml_nand_block_bad(struct mtd_info *mtd, loff_t ofs);
492
493int aml_nand_block_markbad(struct mtd_info *mtd, loff_t ofs);
494
495void aml_nand_dma_read_buf(struct mtd_info *mtd, u8 *buf, int len);
496
497void aml_nand_dma_write_buf(struct mtd_info *mtd, const u8 *buf, int len);
498
499void aml_platform_cmd_ctrl(struct aml_nand_chip *aml_chip,
500 int cmd, unsigned int ctrl);
501
502void aml_platform_write_byte(struct aml_nand_chip *aml_chip, u8 data);
503
504int aml_platform_wait_devready(struct aml_nand_chip *aml_chip, int chipnr);
505
506void aml_platform_get_user_byte(struct aml_nand_chip *aml_chip,
507 unsigned char *oob_buf, int byte_num);
508
509void aml_platform_set_user_byte(struct aml_nand_chip *aml_chip,
510 unsigned char *oob_buf, int byte_num);
511
512void aml_nand_base_command(struct aml_nand_chip *aml_chip,
513 unsigned command, int column, int page_addr, int chipnr);
514
515int aml_nand_block_bad_scrub_update_bbt(struct mtd_info *mtd);
516
517int aml_ubootenv_init(struct aml_nand_chip *aml_chip);
518
519int amlnf_dtb_init(struct aml_nand_chip *aml_chip);
520
521int aml_key_init(struct aml_nand_chip *aml_chip);
522
523int aml_nand_erase_key(struct mtd_info *mtd);
524
525int aml_nand_bbt_check(struct mtd_info *mtd);/*fixed by liuxianjun*/
526
527int aml_nand_scan(struct mtd_info *mtd, int maxchips);
528
529int aml_nand_write_page_raw(struct mtd_info *mtd,
530 struct nand_chip *chip, const u8 *buf, int oob_required,
531 int page);
532
533int aml_nand_write_page(struct mtd_info *mtd,
534 struct nand_chip *chip, uint32_t offset,
535 int data_len,
536 const u8 *buf,
537 int oob_required, int page, int raw);
538
539void aml_nand_base_command(struct aml_nand_chip *aml_chip,
540 unsigned command, int column, int page_addr, int chipnr);
541
542void aml_nand_command(struct mtd_info *mtd,
543 unsigned command, int column, int page_addr);
544
545int aml_nand_wait(struct mtd_info *mtd, struct nand_chip *chip);
546
zhikui.cui82a063e2024-04-15 11:46:30 +0000547int aml_nand_erase(struct mtd_info *mtd, int page);
Feng Chenee8403d2024-01-03 14:13:40 +0800548
549int m3_nand_boot_erase_cmd(struct mtd_info *mtd, int page);
550
551int m3_nand_boot_read_page_hwecc(struct mtd_info *mtd,
552 struct nand_chip *chip, u8 *buf, int oob_required, int page);
553
554int m3_nand_boot_write_page_hwecc(struct mtd_info *mtd,
555 struct nand_chip *chip, const u8 *buf, int oob_required,
556 int page);
557
558int m3_nand_boot_write_page(struct mtd_info *mtd, struct nand_chip *chip,
559 uint32_t offset, int data_len, const u8 *buf,
560 int oob_required, int page, int raw);
561
562int aml_get_samsung_fbbt_flag(void);
563int aml_nand_get_fbb_issue(void);
564
565void aml_nand_check_fbb_issue(u8 *dev_id);
566
567void aml_nand_set_onfi_features(struct aml_nand_chip *aml_chip,
568 u8 *buf, int addr);
569u8 aml_nand_get_onfi_features(struct aml_nand_chip *aml_chip,
570 u8 *buf, int addr);
zhikui.cui3b92f382024-08-20 05:56:50 +0000571struct mtd_partition *get_aml_mtd_partition(void);
572int get_aml_partition_count(void);
Feng Chenee8403d2024-01-03 14:13:40 +0800573#endif