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 "internals.h"
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci/* Bit for detecting BENAND */
128c2ecf20Sopenharmony_ci#define TOSHIBA_NAND_ID4_IS_BENAND		BIT(7)
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci/* Recommended to rewrite for BENAND */
158c2ecf20Sopenharmony_ci#define TOSHIBA_NAND_STATUS_REWRITE_RECOMMENDED	BIT(3)
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci/* ECC Status Read Command for BENAND */
188c2ecf20Sopenharmony_ci#define TOSHIBA_NAND_CMD_ECC_STATUS_READ	0x7A
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/* ECC Status Mask for BENAND */
218c2ecf20Sopenharmony_ci#define TOSHIBA_NAND_ECC_STATUS_MASK		0x0F
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci/* Uncorrectable Error for BENAND */
248c2ecf20Sopenharmony_ci#define TOSHIBA_NAND_ECC_STATUS_UNCORR		0x0F
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci/* Max ECC Steps for BENAND */
278c2ecf20Sopenharmony_ci#define TOSHIBA_NAND_MAX_ECC_STEPS		8
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cistatic int toshiba_nand_benand_read_eccstatus_op(struct nand_chip *chip,
308c2ecf20Sopenharmony_ci						 u8 *buf)
318c2ecf20Sopenharmony_ci{
328c2ecf20Sopenharmony_ci	u8 *ecc_status = buf;
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	if (nand_has_exec_op(chip)) {
358c2ecf20Sopenharmony_ci		const struct nand_sdr_timings *sdr =
368c2ecf20Sopenharmony_ci			nand_get_sdr_timings(nand_get_interface_config(chip));
378c2ecf20Sopenharmony_ci		struct nand_op_instr instrs[] = {
388c2ecf20Sopenharmony_ci			NAND_OP_CMD(TOSHIBA_NAND_CMD_ECC_STATUS_READ,
398c2ecf20Sopenharmony_ci				    PSEC_TO_NSEC(sdr->tADL_min)),
408c2ecf20Sopenharmony_ci			NAND_OP_8BIT_DATA_IN(chip->ecc.steps, ecc_status, 0),
418c2ecf20Sopenharmony_ci		};
428c2ecf20Sopenharmony_ci		struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci		return nand_exec_op(chip, &op);
458c2ecf20Sopenharmony_ci	}
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	return -ENOTSUPP;
488c2ecf20Sopenharmony_ci}
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistatic int toshiba_nand_benand_eccstatus(struct nand_chip *chip)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	struct mtd_info *mtd = nand_to_mtd(chip);
538c2ecf20Sopenharmony_ci	int ret;
548c2ecf20Sopenharmony_ci	unsigned int max_bitflips = 0;
558c2ecf20Sopenharmony_ci	u8 status, ecc_status[TOSHIBA_NAND_MAX_ECC_STEPS];
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	/* Check Status */
588c2ecf20Sopenharmony_ci	ret = toshiba_nand_benand_read_eccstatus_op(chip, ecc_status);
598c2ecf20Sopenharmony_ci	if (!ret) {
608c2ecf20Sopenharmony_ci		unsigned int i, bitflips = 0;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci		for (i = 0; i < chip->ecc.steps; i++) {
638c2ecf20Sopenharmony_ci			bitflips = ecc_status[i] & TOSHIBA_NAND_ECC_STATUS_MASK;
648c2ecf20Sopenharmony_ci			if (bitflips == TOSHIBA_NAND_ECC_STATUS_UNCORR) {
658c2ecf20Sopenharmony_ci				mtd->ecc_stats.failed++;
668c2ecf20Sopenharmony_ci			} else {
678c2ecf20Sopenharmony_ci				mtd->ecc_stats.corrected += bitflips;
688c2ecf20Sopenharmony_ci				max_bitflips = max(max_bitflips, bitflips);
698c2ecf20Sopenharmony_ci			}
708c2ecf20Sopenharmony_ci		}
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci		return max_bitflips;
738c2ecf20Sopenharmony_ci	}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	/*
768c2ecf20Sopenharmony_ci	 * Fallback to regular status check if
778c2ecf20Sopenharmony_ci	 * toshiba_nand_benand_read_eccstatus_op() failed.
788c2ecf20Sopenharmony_ci	 */
798c2ecf20Sopenharmony_ci	ret = nand_status_op(chip, &status);
808c2ecf20Sopenharmony_ci	if (ret)
818c2ecf20Sopenharmony_ci		return ret;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	if (status & NAND_STATUS_FAIL) {
848c2ecf20Sopenharmony_ci		/* uncorrected */
858c2ecf20Sopenharmony_ci		mtd->ecc_stats.failed++;
868c2ecf20Sopenharmony_ci	} else if (status & TOSHIBA_NAND_STATUS_REWRITE_RECOMMENDED) {
878c2ecf20Sopenharmony_ci		/* corrected */
888c2ecf20Sopenharmony_ci		max_bitflips = mtd->bitflip_threshold;
898c2ecf20Sopenharmony_ci		mtd->ecc_stats.corrected += max_bitflips;
908c2ecf20Sopenharmony_ci	}
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	return max_bitflips;
938c2ecf20Sopenharmony_ci}
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_cistatic int
968c2ecf20Sopenharmony_citoshiba_nand_read_page_benand(struct nand_chip *chip, uint8_t *buf,
978c2ecf20Sopenharmony_ci			      int oob_required, int page)
988c2ecf20Sopenharmony_ci{
998c2ecf20Sopenharmony_ci	int ret;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	ret = nand_read_page_raw(chip, buf, oob_required, page);
1028c2ecf20Sopenharmony_ci	if (ret)
1038c2ecf20Sopenharmony_ci		return ret;
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	return toshiba_nand_benand_eccstatus(chip);
1068c2ecf20Sopenharmony_ci}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic int
1098c2ecf20Sopenharmony_citoshiba_nand_read_subpage_benand(struct nand_chip *chip, uint32_t data_offs,
1108c2ecf20Sopenharmony_ci				 uint32_t readlen, uint8_t *bufpoi, int page)
1118c2ecf20Sopenharmony_ci{
1128c2ecf20Sopenharmony_ci	int ret;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	ret = nand_read_page_op(chip, page, data_offs,
1158c2ecf20Sopenharmony_ci				bufpoi + data_offs, readlen);
1168c2ecf20Sopenharmony_ci	if (ret)
1178c2ecf20Sopenharmony_ci		return ret;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	return toshiba_nand_benand_eccstatus(chip);
1208c2ecf20Sopenharmony_ci}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistatic void toshiba_nand_benand_init(struct nand_chip *chip)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	struct mtd_info *mtd = nand_to_mtd(chip);
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	/*
1278c2ecf20Sopenharmony_ci	 * On BENAND, the entire OOB region can be used by the MTD user.
1288c2ecf20Sopenharmony_ci	 * The calculated ECC bytes are stored into other isolated
1298c2ecf20Sopenharmony_ci	 * area which is not accessible to users.
1308c2ecf20Sopenharmony_ci	 * This is why chip->ecc.bytes = 0.
1318c2ecf20Sopenharmony_ci	 */
1328c2ecf20Sopenharmony_ci	chip->ecc.bytes = 0;
1338c2ecf20Sopenharmony_ci	chip->ecc.size = 512;
1348c2ecf20Sopenharmony_ci	chip->ecc.strength = 8;
1358c2ecf20Sopenharmony_ci	chip->ecc.read_page = toshiba_nand_read_page_benand;
1368c2ecf20Sopenharmony_ci	chip->ecc.read_subpage = toshiba_nand_read_subpage_benand;
1378c2ecf20Sopenharmony_ci	chip->ecc.write_page = nand_write_page_raw;
1388c2ecf20Sopenharmony_ci	chip->ecc.read_page_raw = nand_read_page_raw_notsupp;
1398c2ecf20Sopenharmony_ci	chip->ecc.write_page_raw = nand_write_page_raw_notsupp;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	chip->options |= NAND_SUBPAGE_READ;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	mtd_set_ooblayout(mtd, nand_get_large_page_ooblayout());
1448c2ecf20Sopenharmony_ci}
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_cistatic void toshiba_nand_decode_id(struct nand_chip *chip)
1478c2ecf20Sopenharmony_ci{
1488c2ecf20Sopenharmony_ci	struct nand_device *base = &chip->base;
1498c2ecf20Sopenharmony_ci	struct nand_ecc_props requirements = {};
1508c2ecf20Sopenharmony_ci	struct mtd_info *mtd = nand_to_mtd(chip);
1518c2ecf20Sopenharmony_ci	struct nand_memory_organization *memorg;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	memorg = nanddev_get_memorg(&chip->base);
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	nand_decode_ext_id(chip);
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	/*
1588c2ecf20Sopenharmony_ci	 * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per
1598c2ecf20Sopenharmony_ci	 * 512B page. For Toshiba SLC, we decode the 5th/6th byte as
1608c2ecf20Sopenharmony_ci	 * follows:
1618c2ecf20Sopenharmony_ci	 * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm,
1628c2ecf20Sopenharmony_ci	 *                         110b -> 24nm
1638c2ecf20Sopenharmony_ci	 * - ID byte 5, bit[7]:    1 -> BENAND, 0 -> raw SLC
1648c2ecf20Sopenharmony_ci	 */
1658c2ecf20Sopenharmony_ci	if (chip->id.len >= 6 && nand_is_slc(chip) &&
1668c2ecf20Sopenharmony_ci	    (chip->id.data[5] & 0x7) == 0x6 /* 24nm */ &&
1678c2ecf20Sopenharmony_ci	    !(chip->id.data[4] & TOSHIBA_NAND_ID4_IS_BENAND) /* !BENAND */) {
1688c2ecf20Sopenharmony_ci		memorg->oobsize = 32 * memorg->pagesize >> 9;
1698c2ecf20Sopenharmony_ci		mtd->oobsize = memorg->oobsize;
1708c2ecf20Sopenharmony_ci	}
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	/*
1738c2ecf20Sopenharmony_ci	 * Extract ECC requirements from 6th id byte.
1748c2ecf20Sopenharmony_ci	 * For Toshiba SLC, ecc requrements are as follows:
1758c2ecf20Sopenharmony_ci	 *  - 43nm: 1 bit ECC for each 512Byte is required.
1768c2ecf20Sopenharmony_ci	 *  - 32nm: 4 bit ECC for each 512Byte is required.
1778c2ecf20Sopenharmony_ci	 *  - 24nm: 8 bit ECC for each 512Byte is required.
1788c2ecf20Sopenharmony_ci	 */
1798c2ecf20Sopenharmony_ci	if (chip->id.len >= 6 && nand_is_slc(chip)) {
1808c2ecf20Sopenharmony_ci		requirements.step_size = 512;
1818c2ecf20Sopenharmony_ci		switch (chip->id.data[5] & 0x7) {
1828c2ecf20Sopenharmony_ci		case 0x4:
1838c2ecf20Sopenharmony_ci			requirements.strength = 1;
1848c2ecf20Sopenharmony_ci			break;
1858c2ecf20Sopenharmony_ci		case 0x5:
1868c2ecf20Sopenharmony_ci			requirements.strength = 4;
1878c2ecf20Sopenharmony_ci			break;
1888c2ecf20Sopenharmony_ci		case 0x6:
1898c2ecf20Sopenharmony_ci			requirements.strength = 8;
1908c2ecf20Sopenharmony_ci			break;
1918c2ecf20Sopenharmony_ci		default:
1928c2ecf20Sopenharmony_ci			WARN(1, "Could not get ECC info");
1938c2ecf20Sopenharmony_ci			requirements.step_size = 0;
1948c2ecf20Sopenharmony_ci			break;
1958c2ecf20Sopenharmony_ci		}
1968c2ecf20Sopenharmony_ci	}
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	nanddev_set_ecc_requirements(base, &requirements);
1998c2ecf20Sopenharmony_ci}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_cistatic int
2028c2ecf20Sopenharmony_citc58teg5dclta00_choose_interface_config(struct nand_chip *chip,
2038c2ecf20Sopenharmony_ci					struct nand_interface_config *iface)
2048c2ecf20Sopenharmony_ci{
2058c2ecf20Sopenharmony_ci	onfi_fill_interface_config(chip, iface, NAND_SDR_IFACE, 5);
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	return nand_choose_best_sdr_timings(chip, iface, NULL);
2088c2ecf20Sopenharmony_ci}
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_cistatic int
2118c2ecf20Sopenharmony_citc58nvg0s3e_choose_interface_config(struct nand_chip *chip,
2128c2ecf20Sopenharmony_ci				    struct nand_interface_config *iface)
2138c2ecf20Sopenharmony_ci{
2148c2ecf20Sopenharmony_ci	onfi_fill_interface_config(chip, iface, NAND_SDR_IFACE, 2);
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	return nand_choose_best_sdr_timings(chip, iface, NULL);
2178c2ecf20Sopenharmony_ci}
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_cistatic int
2208c2ecf20Sopenharmony_cith58nvg2s3hbai4_choose_interface_config(struct nand_chip *chip,
2218c2ecf20Sopenharmony_ci					struct nand_interface_config *iface)
2228c2ecf20Sopenharmony_ci{
2238c2ecf20Sopenharmony_ci	struct nand_sdr_timings *sdr = &iface->timings.sdr;
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	/* Start with timings from the closest timing mode, mode 4. */
2268c2ecf20Sopenharmony_ci	onfi_fill_interface_config(chip, iface, NAND_SDR_IFACE, 4);
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	/* Patch timings that differ from mode 4. */
2298c2ecf20Sopenharmony_ci	sdr->tALS_min = 12000;
2308c2ecf20Sopenharmony_ci	sdr->tCHZ_max = 20000;
2318c2ecf20Sopenharmony_ci	sdr->tCLS_min = 12000;
2328c2ecf20Sopenharmony_ci	sdr->tCOH_min = 0;
2338c2ecf20Sopenharmony_ci	sdr->tDS_min = 12000;
2348c2ecf20Sopenharmony_ci	sdr->tRHOH_min = 25000;
2358c2ecf20Sopenharmony_ci	sdr->tRHW_min = 30000;
2368c2ecf20Sopenharmony_ci	sdr->tRHZ_max = 60000;
2378c2ecf20Sopenharmony_ci	sdr->tWHR_min = 60000;
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	/* Patch timings not part of onfi timing mode. */
2408c2ecf20Sopenharmony_ci	sdr->tPROG_max = 700000000;
2418c2ecf20Sopenharmony_ci	sdr->tBERS_max = 5000000000;
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	return nand_choose_best_sdr_timings(chip, iface, sdr);
2448c2ecf20Sopenharmony_ci}
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_cistatic int tc58teg5dclta00_init(struct nand_chip *chip)
2478c2ecf20Sopenharmony_ci{
2488c2ecf20Sopenharmony_ci	struct mtd_info *mtd = nand_to_mtd(chip);
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	chip->ops.choose_interface_config =
2518c2ecf20Sopenharmony_ci		&tc58teg5dclta00_choose_interface_config;
2528c2ecf20Sopenharmony_ci	chip->options |= NAND_NEED_SCRAMBLING;
2538c2ecf20Sopenharmony_ci	mtd_set_pairing_scheme(mtd, &dist3_pairing_scheme);
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	return 0;
2568c2ecf20Sopenharmony_ci}
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_cistatic int tc58nvg0s3e_init(struct nand_chip *chip)
2598c2ecf20Sopenharmony_ci{
2608c2ecf20Sopenharmony_ci	chip->ops.choose_interface_config =
2618c2ecf20Sopenharmony_ci		&tc58nvg0s3e_choose_interface_config;
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	return 0;
2648c2ecf20Sopenharmony_ci}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_cistatic int th58nvg2s3hbai4_init(struct nand_chip *chip)
2678c2ecf20Sopenharmony_ci{
2688c2ecf20Sopenharmony_ci	chip->ops.choose_interface_config =
2698c2ecf20Sopenharmony_ci		&th58nvg2s3hbai4_choose_interface_config;
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci	return 0;
2728c2ecf20Sopenharmony_ci}
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_cistatic int toshiba_nand_init(struct nand_chip *chip)
2758c2ecf20Sopenharmony_ci{
2768c2ecf20Sopenharmony_ci	if (nand_is_slc(chip))
2778c2ecf20Sopenharmony_ci		chip->options |= NAND_BBM_FIRSTPAGE | NAND_BBM_SECONDPAGE;
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	/* Check that chip is BENAND and ECC mode is on-die */
2808c2ecf20Sopenharmony_ci	if (nand_is_slc(chip) &&
2818c2ecf20Sopenharmony_ci	    chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_ON_DIE &&
2828c2ecf20Sopenharmony_ci	    chip->id.data[4] & TOSHIBA_NAND_ID4_IS_BENAND)
2838c2ecf20Sopenharmony_ci		toshiba_nand_benand_init(chip);
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	if (!strcmp("TC58TEG5DCLTA00", chip->parameters.model))
2868c2ecf20Sopenharmony_ci		tc58teg5dclta00_init(chip);
2878c2ecf20Sopenharmony_ci	if (!strncmp("TC58NVG0S3E", chip->parameters.model,
2888c2ecf20Sopenharmony_ci		     sizeof("TC58NVG0S3E") - 1))
2898c2ecf20Sopenharmony_ci		tc58nvg0s3e_init(chip);
2908c2ecf20Sopenharmony_ci	if (!strncmp("TH58NVG2S3HBAI4", chip->parameters.model,
2918c2ecf20Sopenharmony_ci		     sizeof("TH58NVG2S3HBAI4") - 1))
2928c2ecf20Sopenharmony_ci		th58nvg2s3hbai4_init(chip);
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	return 0;
2958c2ecf20Sopenharmony_ci}
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ciconst struct nand_manufacturer_ops toshiba_nand_manuf_ops = {
2988c2ecf20Sopenharmony_ci	.detect = toshiba_nand_decode_id,
2998c2ecf20Sopenharmony_ci	.init = toshiba_nand_init,
3008c2ecf20Sopenharmony_ci};
301