1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2017 Free Electrons
4 * Copyright (C) 2017 NextThing Co
6 * Author: Boris Brezillon <boris.brezillon@free-electrons.com>
9 #include "linux/delay.h"
10 #include "internals.h"
12 #define MACRONIX_READ_RETRY_BIT BIT(0)
13 #define MACRONIX_NUM_READ_RETRY_MODES 6
15 #define ONFI_FEATURE_ADDR_MXIC_PROTECTION 0xA0
16 #define MXIC_BLOCK_PROTECTION_ALL_LOCK 0x38
17 #define MXIC_BLOCK_PROTECTION_ALL_UNLOCK 0x0
19 #define ONFI_FEATURE_ADDR_MXIC_RANDOMIZER 0xB0
20 #define MACRONIX_RANDOMIZER_BIT BIT(1)
21 #define MACRONIX_RANDOMIZER_ENPGM BIT(0)
22 #define MACRONIX_RANDOMIZER_RANDEN BIT(1)
23 #define MACRONIX_RANDOMIZER_RANDOPT BIT(2)
24 #define MACRONIX_RANDOMIZER_MODE_ENTER \
25 (MACRONIX_RANDOMIZER_ENPGM | \
26 MACRONIX_RANDOMIZER_RANDEN | \
27 MACRONIX_RANDOMIZER_RANDOPT)
28 #define MACRONIX_RANDOMIZER_MODE_EXIT \
29 (MACRONIX_RANDOMIZER_RANDEN | \
30 MACRONIX_RANDOMIZER_RANDOPT)
32 #define MXIC_CMD_POWER_DOWN 0xB9
34 struct nand_onfi_vendor_macronix
{
39 static int macronix_nand_setup_read_retry(struct nand_chip
*chip
, int mode
)
41 u8 feature
[ONFI_SUBFEATURE_PARAM_LEN
];
43 if (!chip
->parameters
.supports_set_get_features
||
44 !test_bit(ONFI_FEATURE_ADDR_READ_RETRY
,
45 chip
->parameters
.set_feature_list
))
49 return nand_set_features(chip
, ONFI_FEATURE_ADDR_READ_RETRY
, feature
);
52 static int macronix_nand_randomizer_check_enable(struct nand_chip
*chip
)
54 u8 feature
[ONFI_SUBFEATURE_PARAM_LEN
];
57 ret
= nand_get_features(chip
, ONFI_FEATURE_ADDR_MXIC_RANDOMIZER
,
65 feature
[0] = MACRONIX_RANDOMIZER_MODE_ENTER
;
66 ret
= nand_set_features(chip
, ONFI_FEATURE_ADDR_MXIC_RANDOMIZER
,
71 /* RANDEN and RANDOPT OTP bits are programmed */
73 ret
= nand_prog_page_op(chip
, 0, 0, feature
, 1);
77 ret
= nand_get_features(chip
, ONFI_FEATURE_ADDR_MXIC_RANDOMIZER
,
82 feature
[0] &= MACRONIX_RANDOMIZER_MODE_EXIT
;
83 ret
= nand_set_features(chip
, ONFI_FEATURE_ADDR_MXIC_RANDOMIZER
,
91 static void macronix_nand_onfi_init(struct nand_chip
*chip
)
93 struct nand_parameters
*p
= &chip
->parameters
;
94 struct nand_onfi_vendor_macronix
*mxic
;
95 struct device_node
*dn
= nand_get_flash_node(chip
);
102 if (of_find_property(dn
, "mxic,enable-randomizer-otp", NULL
))
105 mxic
= (struct nand_onfi_vendor_macronix
*)p
->onfi
->vendor
;
106 /* Subpage write is prohibited in randomizer operatoin */
107 if (rand_otp
&& chip
->options
& NAND_NO_SUBPAGE_WRITE
&&
108 mxic
->reliability_func
& MACRONIX_RANDOMIZER_BIT
) {
109 if (p
->supports_set_get_features
) {
110 bitmap_set(p
->set_feature_list
,
111 ONFI_FEATURE_ADDR_MXIC_RANDOMIZER
, 1);
112 bitmap_set(p
->get_feature_list
,
113 ONFI_FEATURE_ADDR_MXIC_RANDOMIZER
, 1);
114 ret
= macronix_nand_randomizer_check_enable(chip
);
116 bitmap_clear(p
->set_feature_list
,
117 ONFI_FEATURE_ADDR_MXIC_RANDOMIZER
,
119 bitmap_clear(p
->get_feature_list
,
120 ONFI_FEATURE_ADDR_MXIC_RANDOMIZER
,
122 pr_info("Macronix NAND randomizer failed\n");
124 pr_info("Macronix NAND randomizer enabled\n");
129 if ((mxic
->reliability_func
& MACRONIX_READ_RETRY_BIT
) == 0)
132 chip
->read_retries
= MACRONIX_NUM_READ_RETRY_MODES
;
133 chip
->ops
.setup_read_retry
= macronix_nand_setup_read_retry
;
135 if (p
->supports_set_get_features
) {
136 bitmap_set(p
->set_feature_list
,
137 ONFI_FEATURE_ADDR_READ_RETRY
, 1);
138 bitmap_set(p
->get_feature_list
,
139 ONFI_FEATURE_ADDR_READ_RETRY
, 1);
144 * Macronix AC series does not support using SET/GET_FEATURES to change
145 * the timings unlike what is declared in the parameter page. Unflag
146 * this feature to avoid unnecessary downturns.
148 static void macronix_nand_fix_broken_get_timings(struct nand_chip
*chip
)
151 static const char * const broken_get_timings
[] = {
168 if (!chip
->parameters
.supports_set_get_features
)
171 i
= match_string(broken_get_timings
, ARRAY_SIZE(broken_get_timings
),
172 chip
->parameters
.model
);
176 bitmap_clear(chip
->parameters
.get_feature_list
,
177 ONFI_FEATURE_ADDR_TIMING_MODE
, 1);
178 bitmap_clear(chip
->parameters
.set_feature_list
,
179 ONFI_FEATURE_ADDR_TIMING_MODE
, 1);
183 * Macronix NAND supports Block Protection by Protectoin(PT) pin;
184 * active high at power-on which protects the entire chip even the #WP is
185 * disabled. Lock/unlock protection area can be partition according to
186 * protection bits, i.e. upper 1/2 locked, upper 1/4 locked and so on.
188 static int mxic_nand_lock(struct nand_chip
*chip
, loff_t ofs
, uint64_t len
)
190 u8 feature
[ONFI_SUBFEATURE_PARAM_LEN
];
193 feature
[0] = MXIC_BLOCK_PROTECTION_ALL_LOCK
;
194 nand_select_target(chip
, 0);
195 ret
= nand_set_features(chip
, ONFI_FEATURE_ADDR_MXIC_PROTECTION
,
197 nand_deselect_target(chip
);
199 pr_err("%s all blocks failed\n", __func__
);
204 static int mxic_nand_unlock(struct nand_chip
*chip
, loff_t ofs
, uint64_t len
)
206 u8 feature
[ONFI_SUBFEATURE_PARAM_LEN
];
209 feature
[0] = MXIC_BLOCK_PROTECTION_ALL_UNLOCK
;
210 nand_select_target(chip
, 0);
211 ret
= nand_set_features(chip
, ONFI_FEATURE_ADDR_MXIC_PROTECTION
,
213 nand_deselect_target(chip
);
215 pr_err("%s all blocks failed\n", __func__
);
220 static void macronix_nand_block_protection_support(struct nand_chip
*chip
)
222 u8 feature
[ONFI_SUBFEATURE_PARAM_LEN
];
225 bitmap_set(chip
->parameters
.get_feature_list
,
226 ONFI_FEATURE_ADDR_MXIC_PROTECTION
, 1);
228 feature
[0] = MXIC_BLOCK_PROTECTION_ALL_UNLOCK
;
229 nand_select_target(chip
, 0);
230 ret
= nand_get_features(chip
, ONFI_FEATURE_ADDR_MXIC_PROTECTION
,
232 nand_deselect_target(chip
);
233 if (ret
|| feature
[0] != MXIC_BLOCK_PROTECTION_ALL_LOCK
) {
235 pr_err("Block protection check failed\n");
237 bitmap_clear(chip
->parameters
.get_feature_list
,
238 ONFI_FEATURE_ADDR_MXIC_PROTECTION
, 1);
242 bitmap_set(chip
->parameters
.set_feature_list
,
243 ONFI_FEATURE_ADDR_MXIC_PROTECTION
, 1);
245 chip
->ops
.lock_area
= mxic_nand_lock
;
246 chip
->ops
.unlock_area
= mxic_nand_unlock
;
249 static int nand_power_down_op(struct nand_chip
*chip
)
253 if (nand_has_exec_op(chip
)) {
254 struct nand_op_instr instrs
[] = {
255 NAND_OP_CMD(MXIC_CMD_POWER_DOWN
, 0),
258 struct nand_operation op
= NAND_OPERATION(chip
->cur_cs
, instrs
);
260 ret
= nand_exec_op(chip
, &op
);
265 chip
->legacy
.cmdfunc(chip
, MXIC_CMD_POWER_DOWN
, -1, -1);
271 static int mxic_nand_suspend(struct nand_chip
*chip
)
275 nand_select_target(chip
, 0);
276 ret
= nand_power_down_op(chip
);
278 pr_err("Suspending MXIC NAND chip failed (%d)\n", ret
);
279 nand_deselect_target(chip
);
284 static void mxic_nand_resume(struct nand_chip
*chip
)
287 * Toggle #CS pin to resume NAND device and don't care
288 * of the others CLE, #WE, #RE pins status.
289 * A NAND controller ensure it is able to assert/de-assert #CS
290 * by sending any byte over the NAND bus.
292 * NAND power down command or reset command w/o R/B# status checking.
294 nand_select_target(chip
, 0);
295 nand_power_down_op(chip
);
296 /* The minimum of a recovery time tRDP is 35 us */
297 usleep_range(35, 100);
298 nand_deselect_target(chip
);
301 static void macronix_nand_deep_power_down_support(struct nand_chip
*chip
)
304 static const char * const deep_power_down_dev
[] = {
310 i
= match_string(deep_power_down_dev
, ARRAY_SIZE(deep_power_down_dev
),
311 chip
->parameters
.model
);
315 chip
->ops
.suspend
= mxic_nand_suspend
;
316 chip
->ops
.resume
= mxic_nand_resume
;
319 static int macronix_nand_init(struct nand_chip
*chip
)
321 if (nand_is_slc(chip
))
322 chip
->options
|= NAND_BBM_FIRSTPAGE
| NAND_BBM_SECONDPAGE
;
324 macronix_nand_fix_broken_get_timings(chip
);
325 macronix_nand_onfi_init(chip
);
326 macronix_nand_block_protection_support(chip
);
327 macronix_nand_deep_power_down_support(chip
);
332 const struct nand_manufacturer_ops macronix_nand_manuf_ops
= {
333 .init
= macronix_nand_init
,