18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2017 Free Electrons
48c2ecf20Sopenharmony_ci * Copyright (C) 2017 NextThing Co
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Author: Boris Brezillon <boris.brezillon@free-electrons.com>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/sizes.h>
108c2ecf20Sopenharmony_ci#include <linux/slab.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include "internals.h"
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#define NAND_HYNIX_CMD_SET_PARAMS	0x36
158c2ecf20Sopenharmony_ci#define NAND_HYNIX_CMD_APPLY_PARAMS	0x16
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define NAND_HYNIX_1XNM_RR_REPEAT	8
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/**
208c2ecf20Sopenharmony_ci * struct hynix_read_retry - read-retry data
218c2ecf20Sopenharmony_ci * @nregs: number of register to set when applying a new read-retry mode
228c2ecf20Sopenharmony_ci * @regs: register offsets (NAND chip dependent)
238c2ecf20Sopenharmony_ci * @values: array of values to set in registers. The array size is equal to
248c2ecf20Sopenharmony_ci *	    (nregs * nmodes)
258c2ecf20Sopenharmony_ci */
268c2ecf20Sopenharmony_cistruct hynix_read_retry {
278c2ecf20Sopenharmony_ci	int nregs;
288c2ecf20Sopenharmony_ci	const u8 *regs;
298c2ecf20Sopenharmony_ci	u8 values[];
308c2ecf20Sopenharmony_ci};
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci/**
338c2ecf20Sopenharmony_ci * struct hynix_nand - private Hynix NAND struct
348c2ecf20Sopenharmony_ci * @nand_technology: manufacturing process expressed in picometer
358c2ecf20Sopenharmony_ci * @read_retry: read-retry information
368c2ecf20Sopenharmony_ci */
378c2ecf20Sopenharmony_cistruct hynix_nand {
388c2ecf20Sopenharmony_ci	const struct hynix_read_retry *read_retry;
398c2ecf20Sopenharmony_ci};
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci/**
428c2ecf20Sopenharmony_ci * struct hynix_read_retry_otp - structure describing how the read-retry OTP
438c2ecf20Sopenharmony_ci *				 area
448c2ecf20Sopenharmony_ci * @nregs: number of hynix private registers to set before reading the reading
458c2ecf20Sopenharmony_ci *	   the OTP area
468c2ecf20Sopenharmony_ci * @regs: registers that should be configured
478c2ecf20Sopenharmony_ci * @values: values that should be set in regs
488c2ecf20Sopenharmony_ci * @page: the address to pass to the READ_PAGE command. Depends on the NAND
498c2ecf20Sopenharmony_ci *	  chip
508c2ecf20Sopenharmony_ci * @size: size of the read-retry OTP section
518c2ecf20Sopenharmony_ci */
528c2ecf20Sopenharmony_cistruct hynix_read_retry_otp {
538c2ecf20Sopenharmony_ci	int nregs;
548c2ecf20Sopenharmony_ci	const u8 *regs;
558c2ecf20Sopenharmony_ci	const u8 *values;
568c2ecf20Sopenharmony_ci	int page;
578c2ecf20Sopenharmony_ci	int size;
588c2ecf20Sopenharmony_ci};
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic bool hynix_nand_has_valid_jedecid(struct nand_chip *chip)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	u8 jedecid[5] = { };
638c2ecf20Sopenharmony_ci	int ret;
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	ret = nand_readid_op(chip, 0x40, jedecid, sizeof(jedecid));
668c2ecf20Sopenharmony_ci	if (ret)
678c2ecf20Sopenharmony_ci		return false;
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	return !strncmp("JEDEC", jedecid, sizeof(jedecid));
708c2ecf20Sopenharmony_ci}
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_cistatic int hynix_nand_cmd_op(struct nand_chip *chip, u8 cmd)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	if (nand_has_exec_op(chip)) {
758c2ecf20Sopenharmony_ci		struct nand_op_instr instrs[] = {
768c2ecf20Sopenharmony_ci			NAND_OP_CMD(cmd, 0),
778c2ecf20Sopenharmony_ci		};
788c2ecf20Sopenharmony_ci		struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci		return nand_exec_op(chip, &op);
818c2ecf20Sopenharmony_ci	}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	chip->legacy.cmdfunc(chip, cmd, -1, -1);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	return 0;
868c2ecf20Sopenharmony_ci}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic int hynix_nand_reg_write_op(struct nand_chip *chip, u8 addr, u8 val)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	u16 column = ((u16)addr << 8) | addr;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	if (nand_has_exec_op(chip)) {
938c2ecf20Sopenharmony_ci		struct nand_op_instr instrs[] = {
948c2ecf20Sopenharmony_ci			NAND_OP_ADDR(1, &addr, 0),
958c2ecf20Sopenharmony_ci			NAND_OP_8BIT_DATA_OUT(1, &val, 0),
968c2ecf20Sopenharmony_ci		};
978c2ecf20Sopenharmony_ci		struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci		return nand_exec_op(chip, &op);
1008c2ecf20Sopenharmony_ci	}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	chip->legacy.cmdfunc(chip, NAND_CMD_NONE, column, -1);
1038c2ecf20Sopenharmony_ci	chip->legacy.write_byte(chip, val);
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	return 0;
1068c2ecf20Sopenharmony_ci}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic int hynix_nand_setup_read_retry(struct nand_chip *chip, int retry_mode)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	struct hynix_nand *hynix = nand_get_manufacturer_data(chip);
1118c2ecf20Sopenharmony_ci	const u8 *values;
1128c2ecf20Sopenharmony_ci	int i, ret;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	values = hynix->read_retry->values +
1158c2ecf20Sopenharmony_ci		 (retry_mode * hynix->read_retry->nregs);
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	/* Enter 'Set Hynix Parameters' mode */
1188c2ecf20Sopenharmony_ci	ret = hynix_nand_cmd_op(chip, NAND_HYNIX_CMD_SET_PARAMS);
1198c2ecf20Sopenharmony_ci	if (ret)
1208c2ecf20Sopenharmony_ci		return ret;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	/*
1238c2ecf20Sopenharmony_ci	 * Configure the NAND in the requested read-retry mode.
1248c2ecf20Sopenharmony_ci	 * This is done by setting pre-defined values in internal NAND
1258c2ecf20Sopenharmony_ci	 * registers.
1268c2ecf20Sopenharmony_ci	 *
1278c2ecf20Sopenharmony_ci	 * The set of registers is NAND specific, and the values are either
1288c2ecf20Sopenharmony_ci	 * predefined or extracted from an OTP area on the NAND (values are
1298c2ecf20Sopenharmony_ci	 * probably tweaked at production in this case).
1308c2ecf20Sopenharmony_ci	 */
1318c2ecf20Sopenharmony_ci	for (i = 0; i < hynix->read_retry->nregs; i++) {
1328c2ecf20Sopenharmony_ci		ret = hynix_nand_reg_write_op(chip, hynix->read_retry->regs[i],
1338c2ecf20Sopenharmony_ci					      values[i]);
1348c2ecf20Sopenharmony_ci		if (ret)
1358c2ecf20Sopenharmony_ci			return ret;
1368c2ecf20Sopenharmony_ci	}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	/* Apply the new settings. */
1398c2ecf20Sopenharmony_ci	return hynix_nand_cmd_op(chip, NAND_HYNIX_CMD_APPLY_PARAMS);
1408c2ecf20Sopenharmony_ci}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci/**
1438c2ecf20Sopenharmony_ci * hynix_get_majority - get the value that is occurring the most in a given
1448c2ecf20Sopenharmony_ci *			set of values
1458c2ecf20Sopenharmony_ci * @in: the array of values to test
1468c2ecf20Sopenharmony_ci * @repeat: the size of the in array
1478c2ecf20Sopenharmony_ci * @out: pointer used to store the output value
1488c2ecf20Sopenharmony_ci *
1498c2ecf20Sopenharmony_ci * This function implements the 'majority check' logic that is supposed to
1508c2ecf20Sopenharmony_ci * overcome the unreliability of MLC NANDs when reading the OTP area storing
1518c2ecf20Sopenharmony_ci * the read-retry parameters.
1528c2ecf20Sopenharmony_ci *
1538c2ecf20Sopenharmony_ci * It's based on a pretty simple assumption: if we repeat the same value
1548c2ecf20Sopenharmony_ci * several times and then take the one that is occurring the most, we should
1558c2ecf20Sopenharmony_ci * find the correct value.
1568c2ecf20Sopenharmony_ci * Let's hope this dummy algorithm prevents us from losing the read-retry
1578c2ecf20Sopenharmony_ci * parameters.
1588c2ecf20Sopenharmony_ci */
1598c2ecf20Sopenharmony_cistatic int hynix_get_majority(const u8 *in, int repeat, u8 *out)
1608c2ecf20Sopenharmony_ci{
1618c2ecf20Sopenharmony_ci	int i, j, half = repeat / 2;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	/*
1648c2ecf20Sopenharmony_ci	 * We only test the first half of the in array because we must ensure
1658c2ecf20Sopenharmony_ci	 * that the value is at least occurring repeat / 2 times.
1668c2ecf20Sopenharmony_ci	 *
1678c2ecf20Sopenharmony_ci	 * This loop is suboptimal since we may count the occurrences of the
1688c2ecf20Sopenharmony_ci	 * same value several time, but we are doing that on small sets, which
1698c2ecf20Sopenharmony_ci	 * makes it acceptable.
1708c2ecf20Sopenharmony_ci	 */
1718c2ecf20Sopenharmony_ci	for (i = 0; i < half; i++) {
1728c2ecf20Sopenharmony_ci		int cnt = 0;
1738c2ecf20Sopenharmony_ci		u8 val = in[i];
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci		/* Count all values that are matching the one at index i. */
1768c2ecf20Sopenharmony_ci		for (j = i + 1; j < repeat; j++) {
1778c2ecf20Sopenharmony_ci			if (in[j] == val)
1788c2ecf20Sopenharmony_ci				cnt++;
1798c2ecf20Sopenharmony_ci		}
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci		/* We found a value occurring more than repeat / 2. */
1828c2ecf20Sopenharmony_ci		if (cnt > half) {
1838c2ecf20Sopenharmony_ci			*out = val;
1848c2ecf20Sopenharmony_ci			return 0;
1858c2ecf20Sopenharmony_ci		}
1868c2ecf20Sopenharmony_ci	}
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	return -EIO;
1898c2ecf20Sopenharmony_ci}
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_cistatic int hynix_read_rr_otp(struct nand_chip *chip,
1928c2ecf20Sopenharmony_ci			     const struct hynix_read_retry_otp *info,
1938c2ecf20Sopenharmony_ci			     void *buf)
1948c2ecf20Sopenharmony_ci{
1958c2ecf20Sopenharmony_ci	int i, ret;
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	ret = nand_reset_op(chip);
1988c2ecf20Sopenharmony_ci	if (ret)
1998c2ecf20Sopenharmony_ci		return ret;
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	ret = hynix_nand_cmd_op(chip, NAND_HYNIX_CMD_SET_PARAMS);
2028c2ecf20Sopenharmony_ci	if (ret)
2038c2ecf20Sopenharmony_ci		return ret;
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	for (i = 0; i < info->nregs; i++) {
2068c2ecf20Sopenharmony_ci		ret = hynix_nand_reg_write_op(chip, info->regs[i],
2078c2ecf20Sopenharmony_ci					      info->values[i]);
2088c2ecf20Sopenharmony_ci		if (ret)
2098c2ecf20Sopenharmony_ci			return ret;
2108c2ecf20Sopenharmony_ci	}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	ret = hynix_nand_cmd_op(chip, NAND_HYNIX_CMD_APPLY_PARAMS);
2138c2ecf20Sopenharmony_ci	if (ret)
2148c2ecf20Sopenharmony_ci		return ret;
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	/* Sequence to enter OTP mode? */
2178c2ecf20Sopenharmony_ci	ret = hynix_nand_cmd_op(chip, 0x17);
2188c2ecf20Sopenharmony_ci	if (ret)
2198c2ecf20Sopenharmony_ci		return ret;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	ret = hynix_nand_cmd_op(chip, 0x4);
2228c2ecf20Sopenharmony_ci	if (ret)
2238c2ecf20Sopenharmony_ci		return ret;
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	ret = hynix_nand_cmd_op(chip, 0x19);
2268c2ecf20Sopenharmony_ci	if (ret)
2278c2ecf20Sopenharmony_ci		return ret;
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	/* Now read the page */
2308c2ecf20Sopenharmony_ci	ret = nand_read_page_op(chip, info->page, 0, buf, info->size);
2318c2ecf20Sopenharmony_ci	if (ret)
2328c2ecf20Sopenharmony_ci		return ret;
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	/* Put everything back to normal */
2358c2ecf20Sopenharmony_ci	ret = nand_reset_op(chip);
2368c2ecf20Sopenharmony_ci	if (ret)
2378c2ecf20Sopenharmony_ci		return ret;
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	ret = hynix_nand_cmd_op(chip, NAND_HYNIX_CMD_SET_PARAMS);
2408c2ecf20Sopenharmony_ci	if (ret)
2418c2ecf20Sopenharmony_ci		return ret;
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	ret = hynix_nand_reg_write_op(chip, 0x38, 0);
2448c2ecf20Sopenharmony_ci	if (ret)
2458c2ecf20Sopenharmony_ci		return ret;
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	ret = hynix_nand_cmd_op(chip, NAND_HYNIX_CMD_APPLY_PARAMS);
2488c2ecf20Sopenharmony_ci	if (ret)
2498c2ecf20Sopenharmony_ci		return ret;
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	return nand_read_page_op(chip, 0, 0, NULL, 0);
2528c2ecf20Sopenharmony_ci}
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci#define NAND_HYNIX_1XNM_RR_COUNT_OFFS				0
2558c2ecf20Sopenharmony_ci#define NAND_HYNIX_1XNM_RR_REG_COUNT_OFFS			8
2568c2ecf20Sopenharmony_ci#define NAND_HYNIX_1XNM_RR_SET_OFFS(x, setsize, inv)		\
2578c2ecf20Sopenharmony_ci	(16 + ((((x) * 2) + ((inv) ? 1 : 0)) * (setsize)))
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_cistatic int hynix_mlc_1xnm_rr_value(const u8 *buf, int nmodes, int nregs,
2608c2ecf20Sopenharmony_ci				   int mode, int reg, bool inv, u8 *val)
2618c2ecf20Sopenharmony_ci{
2628c2ecf20Sopenharmony_ci	u8 tmp[NAND_HYNIX_1XNM_RR_REPEAT];
2638c2ecf20Sopenharmony_ci	int val_offs = (mode * nregs) + reg;
2648c2ecf20Sopenharmony_ci	int set_size = nmodes * nregs;
2658c2ecf20Sopenharmony_ci	int i, ret;
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	for (i = 0; i < NAND_HYNIX_1XNM_RR_REPEAT; i++) {
2688c2ecf20Sopenharmony_ci		int set_offs = NAND_HYNIX_1XNM_RR_SET_OFFS(i, set_size, inv);
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci		tmp[i] = buf[val_offs + set_offs];
2718c2ecf20Sopenharmony_ci	}
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	ret = hynix_get_majority(tmp, NAND_HYNIX_1XNM_RR_REPEAT, val);
2748c2ecf20Sopenharmony_ci	if (ret)
2758c2ecf20Sopenharmony_ci		return ret;
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	if (inv)
2788c2ecf20Sopenharmony_ci		*val = ~*val;
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	return 0;
2818c2ecf20Sopenharmony_ci}
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_cistatic u8 hynix_1xnm_mlc_read_retry_regs[] = {
2848c2ecf20Sopenharmony_ci	0xcc, 0xbf, 0xaa, 0xab, 0xcd, 0xad, 0xae, 0xaf
2858c2ecf20Sopenharmony_ci};
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_cistatic int hynix_mlc_1xnm_rr_init(struct nand_chip *chip,
2888c2ecf20Sopenharmony_ci				  const struct hynix_read_retry_otp *info)
2898c2ecf20Sopenharmony_ci{
2908c2ecf20Sopenharmony_ci	struct hynix_nand *hynix = nand_get_manufacturer_data(chip);
2918c2ecf20Sopenharmony_ci	struct hynix_read_retry *rr = NULL;
2928c2ecf20Sopenharmony_ci	int ret, i, j;
2938c2ecf20Sopenharmony_ci	u8 nregs, nmodes;
2948c2ecf20Sopenharmony_ci	u8 *buf;
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	buf = kmalloc(info->size, GFP_KERNEL);
2978c2ecf20Sopenharmony_ci	if (!buf)
2988c2ecf20Sopenharmony_ci		return -ENOMEM;
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	ret = hynix_read_rr_otp(chip, info, buf);
3018c2ecf20Sopenharmony_ci	if (ret)
3028c2ecf20Sopenharmony_ci		goto out;
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	ret = hynix_get_majority(buf, NAND_HYNIX_1XNM_RR_REPEAT,
3058c2ecf20Sopenharmony_ci				 &nmodes);
3068c2ecf20Sopenharmony_ci	if (ret)
3078c2ecf20Sopenharmony_ci		goto out;
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	ret = hynix_get_majority(buf + NAND_HYNIX_1XNM_RR_REPEAT,
3108c2ecf20Sopenharmony_ci				 NAND_HYNIX_1XNM_RR_REPEAT,
3118c2ecf20Sopenharmony_ci				 &nregs);
3128c2ecf20Sopenharmony_ci	if (ret)
3138c2ecf20Sopenharmony_ci		goto out;
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci	rr = kzalloc(sizeof(*rr) + (nregs * nmodes), GFP_KERNEL);
3168c2ecf20Sopenharmony_ci	if (!rr) {
3178c2ecf20Sopenharmony_ci		ret = -ENOMEM;
3188c2ecf20Sopenharmony_ci		goto out;
3198c2ecf20Sopenharmony_ci	}
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	for (i = 0; i < nmodes; i++) {
3228c2ecf20Sopenharmony_ci		for (j = 0; j < nregs; j++) {
3238c2ecf20Sopenharmony_ci			u8 *val = rr->values + (i * nregs);
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci			ret = hynix_mlc_1xnm_rr_value(buf, nmodes, nregs, i, j,
3268c2ecf20Sopenharmony_ci						      false, val);
3278c2ecf20Sopenharmony_ci			if (!ret)
3288c2ecf20Sopenharmony_ci				continue;
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci			ret = hynix_mlc_1xnm_rr_value(buf, nmodes, nregs, i, j,
3318c2ecf20Sopenharmony_ci						      true, val);
3328c2ecf20Sopenharmony_ci			if (ret)
3338c2ecf20Sopenharmony_ci				goto out;
3348c2ecf20Sopenharmony_ci		}
3358c2ecf20Sopenharmony_ci	}
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	rr->nregs = nregs;
3388c2ecf20Sopenharmony_ci	rr->regs = hynix_1xnm_mlc_read_retry_regs;
3398c2ecf20Sopenharmony_ci	hynix->read_retry = rr;
3408c2ecf20Sopenharmony_ci	chip->ops.setup_read_retry = hynix_nand_setup_read_retry;
3418c2ecf20Sopenharmony_ci	chip->read_retries = nmodes;
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ciout:
3448c2ecf20Sopenharmony_ci	kfree(buf);
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	if (ret)
3478c2ecf20Sopenharmony_ci		kfree(rr);
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	return ret;
3508c2ecf20Sopenharmony_ci}
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_cistatic const u8 hynix_mlc_1xnm_rr_otp_regs[] = { 0x38 };
3538c2ecf20Sopenharmony_cistatic const u8 hynix_mlc_1xnm_rr_otp_values[] = { 0x52 };
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_cistatic const struct hynix_read_retry_otp hynix_mlc_1xnm_rr_otps[] = {
3568c2ecf20Sopenharmony_ci	{
3578c2ecf20Sopenharmony_ci		.nregs = ARRAY_SIZE(hynix_mlc_1xnm_rr_otp_regs),
3588c2ecf20Sopenharmony_ci		.regs = hynix_mlc_1xnm_rr_otp_regs,
3598c2ecf20Sopenharmony_ci		.values = hynix_mlc_1xnm_rr_otp_values,
3608c2ecf20Sopenharmony_ci		.page = 0x21f,
3618c2ecf20Sopenharmony_ci		.size = 784
3628c2ecf20Sopenharmony_ci	},
3638c2ecf20Sopenharmony_ci	{
3648c2ecf20Sopenharmony_ci		.nregs = ARRAY_SIZE(hynix_mlc_1xnm_rr_otp_regs),
3658c2ecf20Sopenharmony_ci		.regs = hynix_mlc_1xnm_rr_otp_regs,
3668c2ecf20Sopenharmony_ci		.values = hynix_mlc_1xnm_rr_otp_values,
3678c2ecf20Sopenharmony_ci		.page = 0x200,
3688c2ecf20Sopenharmony_ci		.size = 528,
3698c2ecf20Sopenharmony_ci	},
3708c2ecf20Sopenharmony_ci};
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_cistatic int hynix_nand_rr_init(struct nand_chip *chip)
3738c2ecf20Sopenharmony_ci{
3748c2ecf20Sopenharmony_ci	int i, ret = 0;
3758c2ecf20Sopenharmony_ci	bool valid_jedecid;
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	valid_jedecid = hynix_nand_has_valid_jedecid(chip);
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	/*
3808c2ecf20Sopenharmony_ci	 * We only support read-retry for 1xnm NANDs, and those NANDs all
3818c2ecf20Sopenharmony_ci	 * expose a valid JEDEC ID.
3828c2ecf20Sopenharmony_ci	 */
3838c2ecf20Sopenharmony_ci	if (valid_jedecid) {
3848c2ecf20Sopenharmony_ci		u8 nand_tech = chip->id.data[5] >> 4;
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci		/* 1xnm technology */
3878c2ecf20Sopenharmony_ci		if (nand_tech == 4) {
3888c2ecf20Sopenharmony_ci			for (i = 0; i < ARRAY_SIZE(hynix_mlc_1xnm_rr_otps);
3898c2ecf20Sopenharmony_ci			     i++) {
3908c2ecf20Sopenharmony_ci				/*
3918c2ecf20Sopenharmony_ci				 * FIXME: Hynix recommend to copy the
3928c2ecf20Sopenharmony_ci				 * read-retry OTP area into a normal page.
3938c2ecf20Sopenharmony_ci				 */
3948c2ecf20Sopenharmony_ci				ret = hynix_mlc_1xnm_rr_init(chip,
3958c2ecf20Sopenharmony_ci						hynix_mlc_1xnm_rr_otps);
3968c2ecf20Sopenharmony_ci				if (!ret)
3978c2ecf20Sopenharmony_ci					break;
3988c2ecf20Sopenharmony_ci			}
3998c2ecf20Sopenharmony_ci		}
4008c2ecf20Sopenharmony_ci	}
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	if (ret)
4038c2ecf20Sopenharmony_ci		pr_warn("failed to initialize read-retry infrastructure");
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	return 0;
4068c2ecf20Sopenharmony_ci}
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_cistatic void hynix_nand_extract_oobsize(struct nand_chip *chip,
4098c2ecf20Sopenharmony_ci				       bool valid_jedecid)
4108c2ecf20Sopenharmony_ci{
4118c2ecf20Sopenharmony_ci	struct mtd_info *mtd = nand_to_mtd(chip);
4128c2ecf20Sopenharmony_ci	struct nand_memory_organization *memorg;
4138c2ecf20Sopenharmony_ci	u8 oobsize;
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	memorg = nanddev_get_memorg(&chip->base);
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci	oobsize = ((chip->id.data[3] >> 2) & 0x3) |
4188c2ecf20Sopenharmony_ci		  ((chip->id.data[3] >> 4) & 0x4);
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	if (valid_jedecid) {
4218c2ecf20Sopenharmony_ci		switch (oobsize) {
4228c2ecf20Sopenharmony_ci		case 0:
4238c2ecf20Sopenharmony_ci			memorg->oobsize = 2048;
4248c2ecf20Sopenharmony_ci			break;
4258c2ecf20Sopenharmony_ci		case 1:
4268c2ecf20Sopenharmony_ci			memorg->oobsize = 1664;
4278c2ecf20Sopenharmony_ci			break;
4288c2ecf20Sopenharmony_ci		case 2:
4298c2ecf20Sopenharmony_ci			memorg->oobsize = 1024;
4308c2ecf20Sopenharmony_ci			break;
4318c2ecf20Sopenharmony_ci		case 3:
4328c2ecf20Sopenharmony_ci			memorg->oobsize = 640;
4338c2ecf20Sopenharmony_ci			break;
4348c2ecf20Sopenharmony_ci		default:
4358c2ecf20Sopenharmony_ci			/*
4368c2ecf20Sopenharmony_ci			 * We should never reach this case, but if that
4378c2ecf20Sopenharmony_ci			 * happens, this probably means Hynix decided to use
4388c2ecf20Sopenharmony_ci			 * a different extended ID format, and we should find
4398c2ecf20Sopenharmony_ci			 * a way to support it.
4408c2ecf20Sopenharmony_ci			 */
4418c2ecf20Sopenharmony_ci			WARN(1, "Invalid OOB size");
4428c2ecf20Sopenharmony_ci			break;
4438c2ecf20Sopenharmony_ci		}
4448c2ecf20Sopenharmony_ci	} else {
4458c2ecf20Sopenharmony_ci		switch (oobsize) {
4468c2ecf20Sopenharmony_ci		case 0:
4478c2ecf20Sopenharmony_ci			memorg->oobsize = 128;
4488c2ecf20Sopenharmony_ci			break;
4498c2ecf20Sopenharmony_ci		case 1:
4508c2ecf20Sopenharmony_ci			memorg->oobsize = 224;
4518c2ecf20Sopenharmony_ci			break;
4528c2ecf20Sopenharmony_ci		case 2:
4538c2ecf20Sopenharmony_ci			memorg->oobsize = 448;
4548c2ecf20Sopenharmony_ci			break;
4558c2ecf20Sopenharmony_ci		case 3:
4568c2ecf20Sopenharmony_ci			memorg->oobsize = 64;
4578c2ecf20Sopenharmony_ci			break;
4588c2ecf20Sopenharmony_ci		case 4:
4598c2ecf20Sopenharmony_ci			memorg->oobsize = 32;
4608c2ecf20Sopenharmony_ci			break;
4618c2ecf20Sopenharmony_ci		case 5:
4628c2ecf20Sopenharmony_ci			memorg->oobsize = 16;
4638c2ecf20Sopenharmony_ci			break;
4648c2ecf20Sopenharmony_ci		case 6:
4658c2ecf20Sopenharmony_ci			memorg->oobsize = 640;
4668c2ecf20Sopenharmony_ci			break;
4678c2ecf20Sopenharmony_ci		default:
4688c2ecf20Sopenharmony_ci			/*
4698c2ecf20Sopenharmony_ci			 * We should never reach this case, but if that
4708c2ecf20Sopenharmony_ci			 * happens, this probably means Hynix decided to use
4718c2ecf20Sopenharmony_ci			 * a different extended ID format, and we should find
4728c2ecf20Sopenharmony_ci			 * a way to support it.
4738c2ecf20Sopenharmony_ci			 */
4748c2ecf20Sopenharmony_ci			WARN(1, "Invalid OOB size");
4758c2ecf20Sopenharmony_ci			break;
4768c2ecf20Sopenharmony_ci		}
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci		/*
4798c2ecf20Sopenharmony_ci		 * The datasheet of H27UCG8T2BTR mentions that the "Redundant
4808c2ecf20Sopenharmony_ci		 * Area Size" is encoded "per 8KB" (page size). This chip uses
4818c2ecf20Sopenharmony_ci		 * a page size of 16KiB. The datasheet mentions an OOB size of
4828c2ecf20Sopenharmony_ci		 * 1.280 bytes, but the OOB size encoded in the ID bytes (using
4838c2ecf20Sopenharmony_ci		 * the existing logic above) is 640 bytes.
4848c2ecf20Sopenharmony_ci		 * Update the OOB size for this chip by taking the value
4858c2ecf20Sopenharmony_ci		 * determined above and scaling it to the actual page size (so
4868c2ecf20Sopenharmony_ci		 * the actual OOB size for this chip is: 640 * 16k / 8k).
4878c2ecf20Sopenharmony_ci		 */
4888c2ecf20Sopenharmony_ci		if (chip->id.data[1] == 0xde)
4898c2ecf20Sopenharmony_ci			memorg->oobsize *= memorg->pagesize / SZ_8K;
4908c2ecf20Sopenharmony_ci	}
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	mtd->oobsize = memorg->oobsize;
4938c2ecf20Sopenharmony_ci}
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_cistatic void hynix_nand_extract_ecc_requirements(struct nand_chip *chip,
4968c2ecf20Sopenharmony_ci						bool valid_jedecid)
4978c2ecf20Sopenharmony_ci{
4988c2ecf20Sopenharmony_ci	struct nand_device *base = &chip->base;
4998c2ecf20Sopenharmony_ci	struct nand_ecc_props requirements = {};
5008c2ecf20Sopenharmony_ci	u8 ecc_level = (chip->id.data[4] >> 4) & 0x7;
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci	if (valid_jedecid) {
5038c2ecf20Sopenharmony_ci		/* Reference: H27UCG8T2E datasheet */
5048c2ecf20Sopenharmony_ci		requirements.step_size = 1024;
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci		switch (ecc_level) {
5078c2ecf20Sopenharmony_ci		case 0:
5088c2ecf20Sopenharmony_ci			requirements.step_size = 0;
5098c2ecf20Sopenharmony_ci			requirements.strength = 0;
5108c2ecf20Sopenharmony_ci			break;
5118c2ecf20Sopenharmony_ci		case 1:
5128c2ecf20Sopenharmony_ci			requirements.strength = 4;
5138c2ecf20Sopenharmony_ci			break;
5148c2ecf20Sopenharmony_ci		case 2:
5158c2ecf20Sopenharmony_ci			requirements.strength = 24;
5168c2ecf20Sopenharmony_ci			break;
5178c2ecf20Sopenharmony_ci		case 3:
5188c2ecf20Sopenharmony_ci			requirements.strength = 32;
5198c2ecf20Sopenharmony_ci			break;
5208c2ecf20Sopenharmony_ci		case 4:
5218c2ecf20Sopenharmony_ci			requirements.strength = 40;
5228c2ecf20Sopenharmony_ci			break;
5238c2ecf20Sopenharmony_ci		case 5:
5248c2ecf20Sopenharmony_ci			requirements.strength = 50;
5258c2ecf20Sopenharmony_ci			break;
5268c2ecf20Sopenharmony_ci		case 6:
5278c2ecf20Sopenharmony_ci			requirements.strength = 60;
5288c2ecf20Sopenharmony_ci			break;
5298c2ecf20Sopenharmony_ci		default:
5308c2ecf20Sopenharmony_ci			/*
5318c2ecf20Sopenharmony_ci			 * We should never reach this case, but if that
5328c2ecf20Sopenharmony_ci			 * happens, this probably means Hynix decided to use
5338c2ecf20Sopenharmony_ci			 * a different extended ID format, and we should find
5348c2ecf20Sopenharmony_ci			 * a way to support it.
5358c2ecf20Sopenharmony_ci			 */
5368c2ecf20Sopenharmony_ci			WARN(1, "Invalid ECC requirements");
5378c2ecf20Sopenharmony_ci		}
5388c2ecf20Sopenharmony_ci	} else {
5398c2ecf20Sopenharmony_ci		/*
5408c2ecf20Sopenharmony_ci		 * The ECC requirements field meaning depends on the
5418c2ecf20Sopenharmony_ci		 * NAND technology.
5428c2ecf20Sopenharmony_ci		 */
5438c2ecf20Sopenharmony_ci		u8 nand_tech = chip->id.data[5] & 0x7;
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci		if (nand_tech < 3) {
5468c2ecf20Sopenharmony_ci			/* > 26nm, reference: H27UBG8T2A datasheet */
5478c2ecf20Sopenharmony_ci			if (ecc_level < 5) {
5488c2ecf20Sopenharmony_ci				requirements.step_size = 512;
5498c2ecf20Sopenharmony_ci				requirements.strength = 1 << ecc_level;
5508c2ecf20Sopenharmony_ci			} else if (ecc_level < 7) {
5518c2ecf20Sopenharmony_ci				if (ecc_level == 5)
5528c2ecf20Sopenharmony_ci					requirements.step_size = 2048;
5538c2ecf20Sopenharmony_ci				else
5548c2ecf20Sopenharmony_ci					requirements.step_size = 1024;
5558c2ecf20Sopenharmony_ci				requirements.strength = 24;
5568c2ecf20Sopenharmony_ci			} else {
5578c2ecf20Sopenharmony_ci				/*
5588c2ecf20Sopenharmony_ci				 * We should never reach this case, but if that
5598c2ecf20Sopenharmony_ci				 * happens, this probably means Hynix decided
5608c2ecf20Sopenharmony_ci				 * to use a different extended ID format, and
5618c2ecf20Sopenharmony_ci				 * we should find a way to support it.
5628c2ecf20Sopenharmony_ci				 */
5638c2ecf20Sopenharmony_ci				WARN(1, "Invalid ECC requirements");
5648c2ecf20Sopenharmony_ci			}
5658c2ecf20Sopenharmony_ci		} else {
5668c2ecf20Sopenharmony_ci			/* <= 26nm, reference: H27UBG8T2B datasheet */
5678c2ecf20Sopenharmony_ci			if (!ecc_level) {
5688c2ecf20Sopenharmony_ci				requirements.step_size = 0;
5698c2ecf20Sopenharmony_ci				requirements.strength = 0;
5708c2ecf20Sopenharmony_ci			} else if (ecc_level < 5) {
5718c2ecf20Sopenharmony_ci				requirements.step_size = 512;
5728c2ecf20Sopenharmony_ci				requirements.strength = 1 << (ecc_level - 1);
5738c2ecf20Sopenharmony_ci			} else {
5748c2ecf20Sopenharmony_ci				requirements.step_size = 1024;
5758c2ecf20Sopenharmony_ci				requirements.strength = 24 +
5768c2ecf20Sopenharmony_ci							(8 * (ecc_level - 5));
5778c2ecf20Sopenharmony_ci			}
5788c2ecf20Sopenharmony_ci		}
5798c2ecf20Sopenharmony_ci	}
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci	nanddev_set_ecc_requirements(base, &requirements);
5828c2ecf20Sopenharmony_ci}
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_cistatic void hynix_nand_extract_scrambling_requirements(struct nand_chip *chip,
5858c2ecf20Sopenharmony_ci						       bool valid_jedecid)
5868c2ecf20Sopenharmony_ci{
5878c2ecf20Sopenharmony_ci	u8 nand_tech;
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci	/* We need scrambling on all TLC NANDs*/
5908c2ecf20Sopenharmony_ci	if (nanddev_bits_per_cell(&chip->base) > 2)
5918c2ecf20Sopenharmony_ci		chip->options |= NAND_NEED_SCRAMBLING;
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci	/* And on MLC NANDs with sub-3xnm process */
5948c2ecf20Sopenharmony_ci	if (valid_jedecid) {
5958c2ecf20Sopenharmony_ci		nand_tech = chip->id.data[5] >> 4;
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci		/* < 3xnm */
5988c2ecf20Sopenharmony_ci		if (nand_tech > 0)
5998c2ecf20Sopenharmony_ci			chip->options |= NAND_NEED_SCRAMBLING;
6008c2ecf20Sopenharmony_ci	} else {
6018c2ecf20Sopenharmony_ci		nand_tech = chip->id.data[5] & 0x7;
6028c2ecf20Sopenharmony_ci
6038c2ecf20Sopenharmony_ci		/* < 32nm */
6048c2ecf20Sopenharmony_ci		if (nand_tech > 2)
6058c2ecf20Sopenharmony_ci			chip->options |= NAND_NEED_SCRAMBLING;
6068c2ecf20Sopenharmony_ci	}
6078c2ecf20Sopenharmony_ci}
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_cistatic void hynix_nand_decode_id(struct nand_chip *chip)
6108c2ecf20Sopenharmony_ci{
6118c2ecf20Sopenharmony_ci	struct mtd_info *mtd = nand_to_mtd(chip);
6128c2ecf20Sopenharmony_ci	struct nand_memory_organization *memorg;
6138c2ecf20Sopenharmony_ci	bool valid_jedecid;
6148c2ecf20Sopenharmony_ci	u8 tmp;
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_ci	memorg = nanddev_get_memorg(&chip->base);
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci	/*
6198c2ecf20Sopenharmony_ci	 * Exclude all SLC NANDs from this advanced detection scheme.
6208c2ecf20Sopenharmony_ci	 * According to the ranges defined in several datasheets, it might
6218c2ecf20Sopenharmony_ci	 * appear that even SLC NANDs could fall in this extended ID scheme.
6228c2ecf20Sopenharmony_ci	 * If that the case rework the test to let SLC NANDs go through the
6238c2ecf20Sopenharmony_ci	 * detection process.
6248c2ecf20Sopenharmony_ci	 */
6258c2ecf20Sopenharmony_ci	if (chip->id.len < 6 || nand_is_slc(chip)) {
6268c2ecf20Sopenharmony_ci		nand_decode_ext_id(chip);
6278c2ecf20Sopenharmony_ci		return;
6288c2ecf20Sopenharmony_ci	}
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci	/* Extract pagesize */
6318c2ecf20Sopenharmony_ci	memorg->pagesize = 2048 << (chip->id.data[3] & 0x03);
6328c2ecf20Sopenharmony_ci	mtd->writesize = memorg->pagesize;
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci	tmp = (chip->id.data[3] >> 4) & 0x3;
6358c2ecf20Sopenharmony_ci	/*
6368c2ecf20Sopenharmony_ci	 * When bit7 is set that means we start counting at 1MiB, otherwise
6378c2ecf20Sopenharmony_ci	 * we start counting at 128KiB and shift this value the content of
6388c2ecf20Sopenharmony_ci	 * ID[3][4:5].
6398c2ecf20Sopenharmony_ci	 * The only exception is when ID[3][4:5] == 3 and ID[3][7] == 0, in
6408c2ecf20Sopenharmony_ci	 * this case the erasesize is set to 768KiB.
6418c2ecf20Sopenharmony_ci	 */
6428c2ecf20Sopenharmony_ci	if (chip->id.data[3] & 0x80) {
6438c2ecf20Sopenharmony_ci		memorg->pages_per_eraseblock = (SZ_1M << tmp) /
6448c2ecf20Sopenharmony_ci					       memorg->pagesize;
6458c2ecf20Sopenharmony_ci		mtd->erasesize = SZ_1M << tmp;
6468c2ecf20Sopenharmony_ci	} else if (tmp == 3) {
6478c2ecf20Sopenharmony_ci		memorg->pages_per_eraseblock = (SZ_512K + SZ_256K) /
6488c2ecf20Sopenharmony_ci					       memorg->pagesize;
6498c2ecf20Sopenharmony_ci		mtd->erasesize = SZ_512K + SZ_256K;
6508c2ecf20Sopenharmony_ci	} else {
6518c2ecf20Sopenharmony_ci		memorg->pages_per_eraseblock = (SZ_128K << tmp) /
6528c2ecf20Sopenharmony_ci					       memorg->pagesize;
6538c2ecf20Sopenharmony_ci		mtd->erasesize = SZ_128K << tmp;
6548c2ecf20Sopenharmony_ci	}
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ci	/*
6578c2ecf20Sopenharmony_ci	 * Modern Toggle DDR NANDs have a valid JEDECID even though they are
6588c2ecf20Sopenharmony_ci	 * not exposing a valid JEDEC parameter table.
6598c2ecf20Sopenharmony_ci	 * These NANDs use a different NAND ID scheme.
6608c2ecf20Sopenharmony_ci	 */
6618c2ecf20Sopenharmony_ci	valid_jedecid = hynix_nand_has_valid_jedecid(chip);
6628c2ecf20Sopenharmony_ci
6638c2ecf20Sopenharmony_ci	hynix_nand_extract_oobsize(chip, valid_jedecid);
6648c2ecf20Sopenharmony_ci	hynix_nand_extract_ecc_requirements(chip, valid_jedecid);
6658c2ecf20Sopenharmony_ci	hynix_nand_extract_scrambling_requirements(chip, valid_jedecid);
6668c2ecf20Sopenharmony_ci}
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_cistatic void hynix_nand_cleanup(struct nand_chip *chip)
6698c2ecf20Sopenharmony_ci{
6708c2ecf20Sopenharmony_ci	struct hynix_nand *hynix = nand_get_manufacturer_data(chip);
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_ci	if (!hynix)
6738c2ecf20Sopenharmony_ci		return;
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_ci	kfree(hynix->read_retry);
6768c2ecf20Sopenharmony_ci	kfree(hynix);
6778c2ecf20Sopenharmony_ci	nand_set_manufacturer_data(chip, NULL);
6788c2ecf20Sopenharmony_ci}
6798c2ecf20Sopenharmony_ci
6808c2ecf20Sopenharmony_cistatic int
6818c2ecf20Sopenharmony_cih27ucg8t2atrbc_choose_interface_config(struct nand_chip *chip,
6828c2ecf20Sopenharmony_ci				       struct nand_interface_config *iface)
6838c2ecf20Sopenharmony_ci{
6848c2ecf20Sopenharmony_ci	onfi_fill_interface_config(chip, iface, NAND_SDR_IFACE, 4);
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci	return nand_choose_best_sdr_timings(chip, iface, NULL);
6878c2ecf20Sopenharmony_ci}
6888c2ecf20Sopenharmony_ci
6898c2ecf20Sopenharmony_cistatic int hynix_nand_init(struct nand_chip *chip)
6908c2ecf20Sopenharmony_ci{
6918c2ecf20Sopenharmony_ci	struct hynix_nand *hynix;
6928c2ecf20Sopenharmony_ci	int ret;
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci	if (!nand_is_slc(chip))
6958c2ecf20Sopenharmony_ci		chip->options |= NAND_BBM_LASTPAGE;
6968c2ecf20Sopenharmony_ci	else
6978c2ecf20Sopenharmony_ci		chip->options |= NAND_BBM_FIRSTPAGE | NAND_BBM_SECONDPAGE;
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_ci	hynix = kzalloc(sizeof(*hynix), GFP_KERNEL);
7008c2ecf20Sopenharmony_ci	if (!hynix)
7018c2ecf20Sopenharmony_ci		return -ENOMEM;
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_ci	nand_set_manufacturer_data(chip, hynix);
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci	if (!strncmp("H27UCG8T2ATR-BC", chip->parameters.model,
7068c2ecf20Sopenharmony_ci		     sizeof("H27UCG8T2ATR-BC") - 1))
7078c2ecf20Sopenharmony_ci		chip->ops.choose_interface_config =
7088c2ecf20Sopenharmony_ci			h27ucg8t2atrbc_choose_interface_config;
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci	ret = hynix_nand_rr_init(chip);
7118c2ecf20Sopenharmony_ci	if (ret)
7128c2ecf20Sopenharmony_ci		hynix_nand_cleanup(chip);
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci	return ret;
7158c2ecf20Sopenharmony_ci}
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_ciconst struct nand_manufacturer_ops hynix_nand_manuf_ops = {
7188c2ecf20Sopenharmony_ci	.detect = hynix_nand_decode_id,
7198c2ecf20Sopenharmony_ci	.init = hynix_nand_init,
7208c2ecf20Sopenharmony_ci	.cleanup = hynix_nand_cleanup,
7218c2ecf20Sopenharmony_ci};
722