xref: /openbmc/linux/drivers/mtd/nand/raw/internals.h (revision 82df5b73)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (c) 2018 - Bootlin
4  *
5  * Author: Boris Brezillon <boris.brezillon@bootlin.com>
6  *
7  * Header containing internal definitions to be used only by core files.
8  * NAND controller drivers should not include this file.
9  */
10 
11 #ifndef __LINUX_RAWNAND_INTERNALS
12 #define __LINUX_RAWNAND_INTERNALS
13 
14 #include <linux/mtd/rawnand.h>
15 
16 /*
17  * NAND Flash Manufacturer ID Codes
18  */
19 #define NAND_MFR_AMD		0x01
20 #define NAND_MFR_ATO		0x9b
21 #define NAND_MFR_EON		0x92
22 #define NAND_MFR_ESMT		0xc8
23 #define NAND_MFR_FUJITSU	0x04
24 #define NAND_MFR_HYNIX		0xad
25 #define NAND_MFR_INTEL		0x89
26 #define NAND_MFR_MACRONIX	0xc2
27 #define NAND_MFR_MICRON		0x2c
28 #define NAND_MFR_NATIONAL	0x8f
29 #define NAND_MFR_RENESAS	0x07
30 #define NAND_MFR_SAMSUNG	0xec
31 #define NAND_MFR_SANDISK	0x45
32 #define NAND_MFR_STMICRO	0x20
33 /* Kioxia is new name of Toshiba memory. */
34 #define NAND_MFR_TOSHIBA	0x98
35 #define NAND_MFR_WINBOND	0xef
36 
37 /**
38  * struct nand_manufacturer_ops - NAND Manufacturer operations
39  * @detect: detect the NAND memory organization and capabilities
40  * @init: initialize all vendor specific fields (like the ->read_retry()
41  *	  implementation) if any.
42  * @cleanup: the ->init() function may have allocated resources, ->cleanup()
43  *	     is here to let vendor specific code release those resources.
44  * @fixup_onfi_param_page: apply vendor specific fixups to the ONFI parameter
45  *			   page. This is called after the checksum is verified.
46  */
47 struct nand_manufacturer_ops {
48 	void (*detect)(struct nand_chip *chip);
49 	int (*init)(struct nand_chip *chip);
50 	void (*cleanup)(struct nand_chip *chip);
51 	void (*fixup_onfi_param_page)(struct nand_chip *chip,
52 				      struct nand_onfi_params *p);
53 };
54 
55 /**
56  * struct nand_manufacturer - NAND Flash Manufacturer structure
57  * @name: Manufacturer name
58  * @id: manufacturer ID code of device.
59  * @ops: manufacturer operations
60  */
61 struct nand_manufacturer {
62 	int id;
63 	char *name;
64 	const struct nand_manufacturer_ops *ops;
65 };
66 
67 
68 extern struct nand_flash_dev nand_flash_ids[];
69 
70 extern const struct nand_manufacturer_ops amd_nand_manuf_ops;
71 extern const struct nand_manufacturer_ops esmt_nand_manuf_ops;
72 extern const struct nand_manufacturer_ops hynix_nand_manuf_ops;
73 extern const struct nand_manufacturer_ops macronix_nand_manuf_ops;
74 extern const struct nand_manufacturer_ops micron_nand_manuf_ops;
75 extern const struct nand_manufacturer_ops samsung_nand_manuf_ops;
76 extern const struct nand_manufacturer_ops toshiba_nand_manuf_ops;
77 
78 /* MLC pairing schemes */
79 extern const struct mtd_pairing_scheme dist3_pairing_scheme;
80 
81 /* Core functions */
82 const struct nand_manufacturer *nand_get_manufacturer(u8 id);
83 int nand_bbm_get_next_page(struct nand_chip *chip, int page);
84 int nand_markbad_bbm(struct nand_chip *chip, loff_t ofs);
85 int nand_erase_nand(struct nand_chip *chip, struct erase_info *instr,
86 		    int allowbbt);
87 int onfi_fill_data_interface(struct nand_chip *chip,
88 			     enum nand_data_interface_type type,
89 			     int timing_mode);
90 int nand_get_features(struct nand_chip *chip, int addr, u8 *subfeature_param);
91 int nand_set_features(struct nand_chip *chip, int addr, u8 *subfeature_param);
92 int nand_read_page_raw_notsupp(struct nand_chip *chip, u8 *buf,
93 			       int oob_required, int page);
94 int nand_write_page_raw_notsupp(struct nand_chip *chip, const u8 *buf,
95 				int oob_required, int page);
96 int nand_exit_status_op(struct nand_chip *chip);
97 int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf,
98 			    unsigned int len);
99 void nand_decode_ext_id(struct nand_chip *chip);
100 void panic_nand_wait(struct nand_chip *chip, unsigned long timeo);
101 void sanitize_string(uint8_t *s, size_t len);
102 
103 static inline bool nand_has_exec_op(struct nand_chip *chip)
104 {
105 	if (!chip->controller || !chip->controller->ops ||
106 	    !chip->controller->ops->exec_op)
107 		return false;
108 
109 	return true;
110 }
111 
112 static inline int nand_check_op(struct nand_chip *chip,
113 				const struct nand_operation *op)
114 {
115 	if (!nand_has_exec_op(chip))
116 		return 0;
117 
118 	return chip->controller->ops->exec_op(chip, op, true);
119 }
120 
121 static inline int nand_exec_op(struct nand_chip *chip,
122 			       const struct nand_operation *op)
123 {
124 	if (!nand_has_exec_op(chip))
125 		return -ENOTSUPP;
126 
127 	if (WARN_ON(op->cs >= nanddev_ntargets(&chip->base)))
128 		return -EINVAL;
129 
130 	return chip->controller->ops->exec_op(chip, op, false);
131 }
132 
133 static inline bool nand_has_setup_data_iface(struct nand_chip *chip)
134 {
135 	if (!chip->controller || !chip->controller->ops ||
136 	    !chip->controller->ops->setup_data_interface)
137 		return false;
138 
139 	if (chip->options & NAND_KEEP_TIMINGS)
140 		return false;
141 
142 	return true;
143 }
144 
145 /* BBT functions */
146 int nand_markbad_bbt(struct nand_chip *chip, loff_t offs);
147 int nand_isreserved_bbt(struct nand_chip *chip, loff_t offs);
148 int nand_isbad_bbt(struct nand_chip *chip, loff_t offs, int allowbbt);
149 
150 /* Legacy */
151 void nand_legacy_set_defaults(struct nand_chip *chip);
152 void nand_legacy_adjust_cmdfunc(struct nand_chip *chip);
153 int nand_legacy_check_hooks(struct nand_chip *chip);
154 
155 /* ONFI functions */
156 u16 onfi_crc16(u16 crc, u8 const *p, size_t len);
157 int nand_onfi_detect(struct nand_chip *chip);
158 
159 /* JEDEC functions */
160 int nand_jedec_detect(struct nand_chip *chip);
161 
162 #endif /* __LINUX_RAWNAND_INTERNALS */
163