18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
48c2ecf20Sopenharmony_ci *		  2002-2006 Thomas Gleixner (tglx@linutronix.de)
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci *  Credits:
78c2ecf20Sopenharmony_ci *	David Woodhouse for adding multichip support
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci *	Aleph One Ltd. and Toby Churchill Ltd. for supporting the
108c2ecf20Sopenharmony_ci *	rework for 2K page size chips
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * This file contains all legacy helpers/code that should be removed
138c2ecf20Sopenharmony_ci * at some point.
148c2ecf20Sopenharmony_ci */
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <linux/delay.h>
178c2ecf20Sopenharmony_ci#include <linux/io.h>
188c2ecf20Sopenharmony_ci#include <linux/nmi.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#include "internals.h"
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci/**
238c2ecf20Sopenharmony_ci * nand_read_byte - [DEFAULT] read one byte from the chip
248c2ecf20Sopenharmony_ci * @chip: NAND chip object
258c2ecf20Sopenharmony_ci *
268c2ecf20Sopenharmony_ci * Default read function for 8bit buswidth
278c2ecf20Sopenharmony_ci */
288c2ecf20Sopenharmony_cistatic uint8_t nand_read_byte(struct nand_chip *chip)
298c2ecf20Sopenharmony_ci{
308c2ecf20Sopenharmony_ci	return readb(chip->legacy.IO_ADDR_R);
318c2ecf20Sopenharmony_ci}
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/**
348c2ecf20Sopenharmony_ci * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
358c2ecf20Sopenharmony_ci * @chip: NAND chip object
368c2ecf20Sopenharmony_ci *
378c2ecf20Sopenharmony_ci * Default read function for 16bit buswidth with endianness conversion.
388c2ecf20Sopenharmony_ci *
398c2ecf20Sopenharmony_ci */
408c2ecf20Sopenharmony_cistatic uint8_t nand_read_byte16(struct nand_chip *chip)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci	return (uint8_t) cpu_to_le16(readw(chip->legacy.IO_ADDR_R));
438c2ecf20Sopenharmony_ci}
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci/**
468c2ecf20Sopenharmony_ci * nand_select_chip - [DEFAULT] control CE line
478c2ecf20Sopenharmony_ci * @chip: NAND chip object
488c2ecf20Sopenharmony_ci * @chipnr: chipnumber to select, -1 for deselect
498c2ecf20Sopenharmony_ci *
508c2ecf20Sopenharmony_ci * Default select function for 1 chip devices.
518c2ecf20Sopenharmony_ci */
528c2ecf20Sopenharmony_cistatic void nand_select_chip(struct nand_chip *chip, int chipnr)
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	switch (chipnr) {
558c2ecf20Sopenharmony_ci	case -1:
568c2ecf20Sopenharmony_ci		chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
578c2ecf20Sopenharmony_ci				      0 | NAND_CTRL_CHANGE);
588c2ecf20Sopenharmony_ci		break;
598c2ecf20Sopenharmony_ci	case 0:
608c2ecf20Sopenharmony_ci		break;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	default:
638c2ecf20Sopenharmony_ci		BUG();
648c2ecf20Sopenharmony_ci	}
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci/**
688c2ecf20Sopenharmony_ci * nand_write_byte - [DEFAULT] write single byte to chip
698c2ecf20Sopenharmony_ci * @chip: NAND chip object
708c2ecf20Sopenharmony_ci * @byte: value to write
718c2ecf20Sopenharmony_ci *
728c2ecf20Sopenharmony_ci * Default function to write a byte to I/O[7:0]
738c2ecf20Sopenharmony_ci */
748c2ecf20Sopenharmony_cistatic void nand_write_byte(struct nand_chip *chip, uint8_t byte)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	chip->legacy.write_buf(chip, &byte, 1);
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci/**
808c2ecf20Sopenharmony_ci * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
818c2ecf20Sopenharmony_ci * @chip: NAND chip object
828c2ecf20Sopenharmony_ci * @byte: value to write
838c2ecf20Sopenharmony_ci *
848c2ecf20Sopenharmony_ci * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
858c2ecf20Sopenharmony_ci */
868c2ecf20Sopenharmony_cistatic void nand_write_byte16(struct nand_chip *chip, uint8_t byte)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	uint16_t word = byte;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	/*
918c2ecf20Sopenharmony_ci	 * It's not entirely clear what should happen to I/O[15:8] when writing
928c2ecf20Sopenharmony_ci	 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
938c2ecf20Sopenharmony_ci	 *
948c2ecf20Sopenharmony_ci	 *    When the host supports a 16-bit bus width, only data is
958c2ecf20Sopenharmony_ci	 *    transferred at the 16-bit width. All address and command line
968c2ecf20Sopenharmony_ci	 *    transfers shall use only the lower 8-bits of the data bus. During
978c2ecf20Sopenharmony_ci	 *    command transfers, the host may place any value on the upper
988c2ecf20Sopenharmony_ci	 *    8-bits of the data bus. During address transfers, the host shall
998c2ecf20Sopenharmony_ci	 *    set the upper 8-bits of the data bus to 00h.
1008c2ecf20Sopenharmony_ci	 *
1018c2ecf20Sopenharmony_ci	 * One user of the write_byte callback is nand_set_features. The
1028c2ecf20Sopenharmony_ci	 * four parameters are specified to be written to I/O[7:0], but this is
1038c2ecf20Sopenharmony_ci	 * neither an address nor a command transfer. Let's assume a 0 on the
1048c2ecf20Sopenharmony_ci	 * upper I/O lines is OK.
1058c2ecf20Sopenharmony_ci	 */
1068c2ecf20Sopenharmony_ci	chip->legacy.write_buf(chip, (uint8_t *)&word, 2);
1078c2ecf20Sopenharmony_ci}
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci/**
1108c2ecf20Sopenharmony_ci * nand_write_buf - [DEFAULT] write buffer to chip
1118c2ecf20Sopenharmony_ci * @chip: NAND chip object
1128c2ecf20Sopenharmony_ci * @buf: data buffer
1138c2ecf20Sopenharmony_ci * @len: number of bytes to write
1148c2ecf20Sopenharmony_ci *
1158c2ecf20Sopenharmony_ci * Default write function for 8bit buswidth.
1168c2ecf20Sopenharmony_ci */
1178c2ecf20Sopenharmony_cistatic void nand_write_buf(struct nand_chip *chip, const uint8_t *buf, int len)
1188c2ecf20Sopenharmony_ci{
1198c2ecf20Sopenharmony_ci	iowrite8_rep(chip->legacy.IO_ADDR_W, buf, len);
1208c2ecf20Sopenharmony_ci}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci/**
1238c2ecf20Sopenharmony_ci * nand_read_buf - [DEFAULT] read chip data into buffer
1248c2ecf20Sopenharmony_ci * @chip: NAND chip object
1258c2ecf20Sopenharmony_ci * @buf: buffer to store date
1268c2ecf20Sopenharmony_ci * @len: number of bytes to read
1278c2ecf20Sopenharmony_ci *
1288c2ecf20Sopenharmony_ci * Default read function for 8bit buswidth.
1298c2ecf20Sopenharmony_ci */
1308c2ecf20Sopenharmony_cistatic void nand_read_buf(struct nand_chip *chip, uint8_t *buf, int len)
1318c2ecf20Sopenharmony_ci{
1328c2ecf20Sopenharmony_ci	ioread8_rep(chip->legacy.IO_ADDR_R, buf, len);
1338c2ecf20Sopenharmony_ci}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci/**
1368c2ecf20Sopenharmony_ci * nand_write_buf16 - [DEFAULT] write buffer to chip
1378c2ecf20Sopenharmony_ci * @chip: NAND chip object
1388c2ecf20Sopenharmony_ci * @buf: data buffer
1398c2ecf20Sopenharmony_ci * @len: number of bytes to write
1408c2ecf20Sopenharmony_ci *
1418c2ecf20Sopenharmony_ci * Default write function for 16bit buswidth.
1428c2ecf20Sopenharmony_ci */
1438c2ecf20Sopenharmony_cistatic void nand_write_buf16(struct nand_chip *chip, const uint8_t *buf,
1448c2ecf20Sopenharmony_ci			     int len)
1458c2ecf20Sopenharmony_ci{
1468c2ecf20Sopenharmony_ci	u16 *p = (u16 *) buf;
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	iowrite16_rep(chip->legacy.IO_ADDR_W, p, len >> 1);
1498c2ecf20Sopenharmony_ci}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci/**
1528c2ecf20Sopenharmony_ci * nand_read_buf16 - [DEFAULT] read chip data into buffer
1538c2ecf20Sopenharmony_ci * @chip: NAND chip object
1548c2ecf20Sopenharmony_ci * @buf: buffer to store date
1558c2ecf20Sopenharmony_ci * @len: number of bytes to read
1568c2ecf20Sopenharmony_ci *
1578c2ecf20Sopenharmony_ci * Default read function for 16bit buswidth.
1588c2ecf20Sopenharmony_ci */
1598c2ecf20Sopenharmony_cistatic void nand_read_buf16(struct nand_chip *chip, uint8_t *buf, int len)
1608c2ecf20Sopenharmony_ci{
1618c2ecf20Sopenharmony_ci	u16 *p = (u16 *) buf;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	ioread16_rep(chip->legacy.IO_ADDR_R, p, len >> 1);
1648c2ecf20Sopenharmony_ci}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci/**
1678c2ecf20Sopenharmony_ci * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
1688c2ecf20Sopenharmony_ci * @chip: NAND chip object
1698c2ecf20Sopenharmony_ci * @timeo: Timeout
1708c2ecf20Sopenharmony_ci *
1718c2ecf20Sopenharmony_ci * Helper function for nand_wait_ready used when needing to wait in interrupt
1728c2ecf20Sopenharmony_ci * context.
1738c2ecf20Sopenharmony_ci */
1748c2ecf20Sopenharmony_cistatic void panic_nand_wait_ready(struct nand_chip *chip, unsigned long timeo)
1758c2ecf20Sopenharmony_ci{
1768c2ecf20Sopenharmony_ci	int i;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	/* Wait for the device to get ready */
1798c2ecf20Sopenharmony_ci	for (i = 0; i < timeo; i++) {
1808c2ecf20Sopenharmony_ci		if (chip->legacy.dev_ready(chip))
1818c2ecf20Sopenharmony_ci			break;
1828c2ecf20Sopenharmony_ci		touch_softlockup_watchdog();
1838c2ecf20Sopenharmony_ci		mdelay(1);
1848c2ecf20Sopenharmony_ci	}
1858c2ecf20Sopenharmony_ci}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci/**
1888c2ecf20Sopenharmony_ci * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
1898c2ecf20Sopenharmony_ci * @chip: NAND chip object
1908c2ecf20Sopenharmony_ci *
1918c2ecf20Sopenharmony_ci * Wait for the ready pin after a command, and warn if a timeout occurs.
1928c2ecf20Sopenharmony_ci */
1938c2ecf20Sopenharmony_civoid nand_wait_ready(struct nand_chip *chip)
1948c2ecf20Sopenharmony_ci{
1958c2ecf20Sopenharmony_ci	unsigned long timeo = 400;
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	if (in_interrupt() || oops_in_progress)
1988c2ecf20Sopenharmony_ci		return panic_nand_wait_ready(chip, timeo);
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	/* Wait until command is processed or timeout occurs */
2018c2ecf20Sopenharmony_ci	timeo = jiffies + msecs_to_jiffies(timeo);
2028c2ecf20Sopenharmony_ci	do {
2038c2ecf20Sopenharmony_ci		if (chip->legacy.dev_ready(chip))
2048c2ecf20Sopenharmony_ci			return;
2058c2ecf20Sopenharmony_ci		cond_resched();
2068c2ecf20Sopenharmony_ci	} while (time_before(jiffies, timeo));
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	if (!chip->legacy.dev_ready(chip))
2098c2ecf20Sopenharmony_ci		pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
2108c2ecf20Sopenharmony_ci}
2118c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(nand_wait_ready);
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci/**
2148c2ecf20Sopenharmony_ci * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
2158c2ecf20Sopenharmony_ci * @chip: NAND chip object
2168c2ecf20Sopenharmony_ci * @timeo: Timeout in ms
2178c2ecf20Sopenharmony_ci *
2188c2ecf20Sopenharmony_ci * Wait for status ready (i.e. command done) or timeout.
2198c2ecf20Sopenharmony_ci */
2208c2ecf20Sopenharmony_cistatic void nand_wait_status_ready(struct nand_chip *chip, unsigned long timeo)
2218c2ecf20Sopenharmony_ci{
2228c2ecf20Sopenharmony_ci	int ret;
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	timeo = jiffies + msecs_to_jiffies(timeo);
2258c2ecf20Sopenharmony_ci	do {
2268c2ecf20Sopenharmony_ci		u8 status;
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci		ret = nand_read_data_op(chip, &status, sizeof(status), true,
2298c2ecf20Sopenharmony_ci					false);
2308c2ecf20Sopenharmony_ci		if (ret)
2318c2ecf20Sopenharmony_ci			return;
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci		if (status & NAND_STATUS_READY)
2348c2ecf20Sopenharmony_ci			break;
2358c2ecf20Sopenharmony_ci		touch_softlockup_watchdog();
2368c2ecf20Sopenharmony_ci	} while (time_before(jiffies, timeo));
2378c2ecf20Sopenharmony_ci};
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci/**
2408c2ecf20Sopenharmony_ci * nand_command - [DEFAULT] Send command to NAND device
2418c2ecf20Sopenharmony_ci * @chip: NAND chip object
2428c2ecf20Sopenharmony_ci * @command: the command to be sent
2438c2ecf20Sopenharmony_ci * @column: the column address for this command, -1 if none
2448c2ecf20Sopenharmony_ci * @page_addr: the page address for this command, -1 if none
2458c2ecf20Sopenharmony_ci *
2468c2ecf20Sopenharmony_ci * Send command to NAND device. This function is used for small page devices
2478c2ecf20Sopenharmony_ci * (512 Bytes per page).
2488c2ecf20Sopenharmony_ci */
2498c2ecf20Sopenharmony_cistatic void nand_command(struct nand_chip *chip, unsigned int command,
2508c2ecf20Sopenharmony_ci			 int column, int page_addr)
2518c2ecf20Sopenharmony_ci{
2528c2ecf20Sopenharmony_ci	struct mtd_info *mtd = nand_to_mtd(chip);
2538c2ecf20Sopenharmony_ci	int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	/* Write out the command to the device */
2568c2ecf20Sopenharmony_ci	if (command == NAND_CMD_SEQIN) {
2578c2ecf20Sopenharmony_ci		int readcmd;
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci		if (column >= mtd->writesize) {
2608c2ecf20Sopenharmony_ci			/* OOB area */
2618c2ecf20Sopenharmony_ci			column -= mtd->writesize;
2628c2ecf20Sopenharmony_ci			readcmd = NAND_CMD_READOOB;
2638c2ecf20Sopenharmony_ci		} else if (column < 256) {
2648c2ecf20Sopenharmony_ci			/* First 256 bytes --> READ0 */
2658c2ecf20Sopenharmony_ci			readcmd = NAND_CMD_READ0;
2668c2ecf20Sopenharmony_ci		} else {
2678c2ecf20Sopenharmony_ci			column -= 256;
2688c2ecf20Sopenharmony_ci			readcmd = NAND_CMD_READ1;
2698c2ecf20Sopenharmony_ci		}
2708c2ecf20Sopenharmony_ci		chip->legacy.cmd_ctrl(chip, readcmd, ctrl);
2718c2ecf20Sopenharmony_ci		ctrl &= ~NAND_CTRL_CHANGE;
2728c2ecf20Sopenharmony_ci	}
2738c2ecf20Sopenharmony_ci	if (command != NAND_CMD_NONE)
2748c2ecf20Sopenharmony_ci		chip->legacy.cmd_ctrl(chip, command, ctrl);
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	/* Address cycle, when necessary */
2778c2ecf20Sopenharmony_ci	ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
2788c2ecf20Sopenharmony_ci	/* Serially input address */
2798c2ecf20Sopenharmony_ci	if (column != -1) {
2808c2ecf20Sopenharmony_ci		/* Adjust columns for 16 bit buswidth */
2818c2ecf20Sopenharmony_ci		if (chip->options & NAND_BUSWIDTH_16 &&
2828c2ecf20Sopenharmony_ci				!nand_opcode_8bits(command))
2838c2ecf20Sopenharmony_ci			column >>= 1;
2848c2ecf20Sopenharmony_ci		chip->legacy.cmd_ctrl(chip, column, ctrl);
2858c2ecf20Sopenharmony_ci		ctrl &= ~NAND_CTRL_CHANGE;
2868c2ecf20Sopenharmony_ci	}
2878c2ecf20Sopenharmony_ci	if (page_addr != -1) {
2888c2ecf20Sopenharmony_ci		chip->legacy.cmd_ctrl(chip, page_addr, ctrl);
2898c2ecf20Sopenharmony_ci		ctrl &= ~NAND_CTRL_CHANGE;
2908c2ecf20Sopenharmony_ci		chip->legacy.cmd_ctrl(chip, page_addr >> 8, ctrl);
2918c2ecf20Sopenharmony_ci		if (chip->options & NAND_ROW_ADDR_3)
2928c2ecf20Sopenharmony_ci			chip->legacy.cmd_ctrl(chip, page_addr >> 16, ctrl);
2938c2ecf20Sopenharmony_ci	}
2948c2ecf20Sopenharmony_ci	chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
2958c2ecf20Sopenharmony_ci			      NAND_NCE | NAND_CTRL_CHANGE);
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	/*
2988c2ecf20Sopenharmony_ci	 * Program and erase have their own busy handlers status and sequential
2998c2ecf20Sopenharmony_ci	 * in needs no delay
3008c2ecf20Sopenharmony_ci	 */
3018c2ecf20Sopenharmony_ci	switch (command) {
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	case NAND_CMD_NONE:
3048c2ecf20Sopenharmony_ci	case NAND_CMD_PAGEPROG:
3058c2ecf20Sopenharmony_ci	case NAND_CMD_ERASE1:
3068c2ecf20Sopenharmony_ci	case NAND_CMD_ERASE2:
3078c2ecf20Sopenharmony_ci	case NAND_CMD_SEQIN:
3088c2ecf20Sopenharmony_ci	case NAND_CMD_STATUS:
3098c2ecf20Sopenharmony_ci	case NAND_CMD_READID:
3108c2ecf20Sopenharmony_ci	case NAND_CMD_SET_FEATURES:
3118c2ecf20Sopenharmony_ci		return;
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	case NAND_CMD_RESET:
3148c2ecf20Sopenharmony_ci		if (chip->legacy.dev_ready)
3158c2ecf20Sopenharmony_ci			break;
3168c2ecf20Sopenharmony_ci		udelay(chip->legacy.chip_delay);
3178c2ecf20Sopenharmony_ci		chip->legacy.cmd_ctrl(chip, NAND_CMD_STATUS,
3188c2ecf20Sopenharmony_ci				      NAND_CTRL_CLE | NAND_CTRL_CHANGE);
3198c2ecf20Sopenharmony_ci		chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
3208c2ecf20Sopenharmony_ci				      NAND_NCE | NAND_CTRL_CHANGE);
3218c2ecf20Sopenharmony_ci		/* EZ-NAND can take upto 250ms as per ONFi v4.0 */
3228c2ecf20Sopenharmony_ci		nand_wait_status_ready(chip, 250);
3238c2ecf20Sopenharmony_ci		return;
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci		/* This applies to read commands */
3268c2ecf20Sopenharmony_ci	case NAND_CMD_READ0:
3278c2ecf20Sopenharmony_ci		/*
3288c2ecf20Sopenharmony_ci		 * READ0 is sometimes used to exit GET STATUS mode. When this
3298c2ecf20Sopenharmony_ci		 * is the case no address cycles are requested, and we can use
3308c2ecf20Sopenharmony_ci		 * this information to detect that we should not wait for the
3318c2ecf20Sopenharmony_ci		 * device to be ready.
3328c2ecf20Sopenharmony_ci		 */
3338c2ecf20Sopenharmony_ci		if (column == -1 && page_addr == -1)
3348c2ecf20Sopenharmony_ci			return;
3358c2ecf20Sopenharmony_ci		fallthrough;
3368c2ecf20Sopenharmony_ci	default:
3378c2ecf20Sopenharmony_ci		/*
3388c2ecf20Sopenharmony_ci		 * If we don't have access to the busy pin, we apply the given
3398c2ecf20Sopenharmony_ci		 * command delay
3408c2ecf20Sopenharmony_ci		 */
3418c2ecf20Sopenharmony_ci		if (!chip->legacy.dev_ready) {
3428c2ecf20Sopenharmony_ci			udelay(chip->legacy.chip_delay);
3438c2ecf20Sopenharmony_ci			return;
3448c2ecf20Sopenharmony_ci		}
3458c2ecf20Sopenharmony_ci	}
3468c2ecf20Sopenharmony_ci	/*
3478c2ecf20Sopenharmony_ci	 * Apply this short delay always to ensure that we do wait tWB in
3488c2ecf20Sopenharmony_ci	 * any case on any machine.
3498c2ecf20Sopenharmony_ci	 */
3508c2ecf20Sopenharmony_ci	ndelay(100);
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci	nand_wait_ready(chip);
3538c2ecf20Sopenharmony_ci}
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_cistatic void nand_ccs_delay(struct nand_chip *chip)
3568c2ecf20Sopenharmony_ci{
3578c2ecf20Sopenharmony_ci	const struct nand_sdr_timings *sdr =
3588c2ecf20Sopenharmony_ci		nand_get_sdr_timings(nand_get_interface_config(chip));
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	/*
3618c2ecf20Sopenharmony_ci	 * The controller already takes care of waiting for tCCS when the RNDIN
3628c2ecf20Sopenharmony_ci	 * or RNDOUT command is sent, return directly.
3638c2ecf20Sopenharmony_ci	 */
3648c2ecf20Sopenharmony_ci	if (!(chip->options & NAND_WAIT_TCCS))
3658c2ecf20Sopenharmony_ci		return;
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	/*
3688c2ecf20Sopenharmony_ci	 * Wait tCCS_min if it is correctly defined, otherwise wait 500ns
3698c2ecf20Sopenharmony_ci	 * (which should be safe for all NANDs).
3708c2ecf20Sopenharmony_ci	 */
3718c2ecf20Sopenharmony_ci	if (nand_controller_can_setup_interface(chip))
3728c2ecf20Sopenharmony_ci		ndelay(sdr->tCCS_min / 1000);
3738c2ecf20Sopenharmony_ci	else
3748c2ecf20Sopenharmony_ci		ndelay(500);
3758c2ecf20Sopenharmony_ci}
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci/**
3788c2ecf20Sopenharmony_ci * nand_command_lp - [DEFAULT] Send command to NAND large page device
3798c2ecf20Sopenharmony_ci * @chip: NAND chip object
3808c2ecf20Sopenharmony_ci * @command: the command to be sent
3818c2ecf20Sopenharmony_ci * @column: the column address for this command, -1 if none
3828c2ecf20Sopenharmony_ci * @page_addr: the page address for this command, -1 if none
3838c2ecf20Sopenharmony_ci *
3848c2ecf20Sopenharmony_ci * Send command to NAND device. This is the version for the new large page
3858c2ecf20Sopenharmony_ci * devices. We don't have the separate regions as we have in the small page
3868c2ecf20Sopenharmony_ci * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
3878c2ecf20Sopenharmony_ci */
3888c2ecf20Sopenharmony_cistatic void nand_command_lp(struct nand_chip *chip, unsigned int command,
3898c2ecf20Sopenharmony_ci			    int column, int page_addr)
3908c2ecf20Sopenharmony_ci{
3918c2ecf20Sopenharmony_ci	struct mtd_info *mtd = nand_to_mtd(chip);
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	/* Emulate NAND_CMD_READOOB */
3948c2ecf20Sopenharmony_ci	if (command == NAND_CMD_READOOB) {
3958c2ecf20Sopenharmony_ci		column += mtd->writesize;
3968c2ecf20Sopenharmony_ci		command = NAND_CMD_READ0;
3978c2ecf20Sopenharmony_ci	}
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	/* Command latch cycle */
4008c2ecf20Sopenharmony_ci	if (command != NAND_CMD_NONE)
4018c2ecf20Sopenharmony_ci		chip->legacy.cmd_ctrl(chip, command,
4028c2ecf20Sopenharmony_ci				      NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci	if (column != -1 || page_addr != -1) {
4058c2ecf20Sopenharmony_ci		int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci		/* Serially input address */
4088c2ecf20Sopenharmony_ci		if (column != -1) {
4098c2ecf20Sopenharmony_ci			/* Adjust columns for 16 bit buswidth */
4108c2ecf20Sopenharmony_ci			if (chip->options & NAND_BUSWIDTH_16 &&
4118c2ecf20Sopenharmony_ci					!nand_opcode_8bits(command))
4128c2ecf20Sopenharmony_ci				column >>= 1;
4138c2ecf20Sopenharmony_ci			chip->legacy.cmd_ctrl(chip, column, ctrl);
4148c2ecf20Sopenharmony_ci			ctrl &= ~NAND_CTRL_CHANGE;
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci			/* Only output a single addr cycle for 8bits opcodes. */
4178c2ecf20Sopenharmony_ci			if (!nand_opcode_8bits(command))
4188c2ecf20Sopenharmony_ci				chip->legacy.cmd_ctrl(chip, column >> 8, ctrl);
4198c2ecf20Sopenharmony_ci		}
4208c2ecf20Sopenharmony_ci		if (page_addr != -1) {
4218c2ecf20Sopenharmony_ci			chip->legacy.cmd_ctrl(chip, page_addr, ctrl);
4228c2ecf20Sopenharmony_ci			chip->legacy.cmd_ctrl(chip, page_addr >> 8,
4238c2ecf20Sopenharmony_ci					     NAND_NCE | NAND_ALE);
4248c2ecf20Sopenharmony_ci			if (chip->options & NAND_ROW_ADDR_3)
4258c2ecf20Sopenharmony_ci				chip->legacy.cmd_ctrl(chip, page_addr >> 16,
4268c2ecf20Sopenharmony_ci						      NAND_NCE | NAND_ALE);
4278c2ecf20Sopenharmony_ci		}
4288c2ecf20Sopenharmony_ci	}
4298c2ecf20Sopenharmony_ci	chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
4308c2ecf20Sopenharmony_ci			      NAND_NCE | NAND_CTRL_CHANGE);
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci	/*
4338c2ecf20Sopenharmony_ci	 * Program and erase have their own busy handlers status, sequential
4348c2ecf20Sopenharmony_ci	 * in and status need no delay.
4358c2ecf20Sopenharmony_ci	 */
4368c2ecf20Sopenharmony_ci	switch (command) {
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ci	case NAND_CMD_NONE:
4398c2ecf20Sopenharmony_ci	case NAND_CMD_CACHEDPROG:
4408c2ecf20Sopenharmony_ci	case NAND_CMD_PAGEPROG:
4418c2ecf20Sopenharmony_ci	case NAND_CMD_ERASE1:
4428c2ecf20Sopenharmony_ci	case NAND_CMD_ERASE2:
4438c2ecf20Sopenharmony_ci	case NAND_CMD_SEQIN:
4448c2ecf20Sopenharmony_ci	case NAND_CMD_STATUS:
4458c2ecf20Sopenharmony_ci	case NAND_CMD_READID:
4468c2ecf20Sopenharmony_ci	case NAND_CMD_SET_FEATURES:
4478c2ecf20Sopenharmony_ci		return;
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci	case NAND_CMD_RNDIN:
4508c2ecf20Sopenharmony_ci		nand_ccs_delay(chip);
4518c2ecf20Sopenharmony_ci		return;
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci	case NAND_CMD_RESET:
4548c2ecf20Sopenharmony_ci		if (chip->legacy.dev_ready)
4558c2ecf20Sopenharmony_ci			break;
4568c2ecf20Sopenharmony_ci		udelay(chip->legacy.chip_delay);
4578c2ecf20Sopenharmony_ci		chip->legacy.cmd_ctrl(chip, NAND_CMD_STATUS,
4588c2ecf20Sopenharmony_ci				      NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
4598c2ecf20Sopenharmony_ci		chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
4608c2ecf20Sopenharmony_ci				      NAND_NCE | NAND_CTRL_CHANGE);
4618c2ecf20Sopenharmony_ci		/* EZ-NAND can take upto 250ms as per ONFi v4.0 */
4628c2ecf20Sopenharmony_ci		nand_wait_status_ready(chip, 250);
4638c2ecf20Sopenharmony_ci		return;
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci	case NAND_CMD_RNDOUT:
4668c2ecf20Sopenharmony_ci		/* No ready / busy check necessary */
4678c2ecf20Sopenharmony_ci		chip->legacy.cmd_ctrl(chip, NAND_CMD_RNDOUTSTART,
4688c2ecf20Sopenharmony_ci				      NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
4698c2ecf20Sopenharmony_ci		chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
4708c2ecf20Sopenharmony_ci				      NAND_NCE | NAND_CTRL_CHANGE);
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci		nand_ccs_delay(chip);
4738c2ecf20Sopenharmony_ci		return;
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	case NAND_CMD_READ0:
4768c2ecf20Sopenharmony_ci		/*
4778c2ecf20Sopenharmony_ci		 * READ0 is sometimes used to exit GET STATUS mode. When this
4788c2ecf20Sopenharmony_ci		 * is the case no address cycles are requested, and we can use
4798c2ecf20Sopenharmony_ci		 * this information to detect that READSTART should not be
4808c2ecf20Sopenharmony_ci		 * issued.
4818c2ecf20Sopenharmony_ci		 */
4828c2ecf20Sopenharmony_ci		if (column == -1 && page_addr == -1)
4838c2ecf20Sopenharmony_ci			return;
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci		chip->legacy.cmd_ctrl(chip, NAND_CMD_READSTART,
4868c2ecf20Sopenharmony_ci				      NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
4878c2ecf20Sopenharmony_ci		chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
4888c2ecf20Sopenharmony_ci				      NAND_NCE | NAND_CTRL_CHANGE);
4898c2ecf20Sopenharmony_ci		fallthrough;	/* This applies to read commands */
4908c2ecf20Sopenharmony_ci	default:
4918c2ecf20Sopenharmony_ci		/*
4928c2ecf20Sopenharmony_ci		 * If we don't have access to the busy pin, we apply the given
4938c2ecf20Sopenharmony_ci		 * command delay.
4948c2ecf20Sopenharmony_ci		 */
4958c2ecf20Sopenharmony_ci		if (!chip->legacy.dev_ready) {
4968c2ecf20Sopenharmony_ci			udelay(chip->legacy.chip_delay);
4978c2ecf20Sopenharmony_ci			return;
4988c2ecf20Sopenharmony_ci		}
4998c2ecf20Sopenharmony_ci	}
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	/*
5028c2ecf20Sopenharmony_ci	 * Apply this short delay always to ensure that we do wait tWB in
5038c2ecf20Sopenharmony_ci	 * any case on any machine.
5048c2ecf20Sopenharmony_ci	 */
5058c2ecf20Sopenharmony_ci	ndelay(100);
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci	nand_wait_ready(chip);
5088c2ecf20Sopenharmony_ci}
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci/**
5118c2ecf20Sopenharmony_ci * nand_get_set_features_notsupp - set/get features stub returning -ENOTSUPP
5128c2ecf20Sopenharmony_ci * @chip: nand chip info structure
5138c2ecf20Sopenharmony_ci * @addr: feature address.
5148c2ecf20Sopenharmony_ci * @subfeature_param: the subfeature parameters, a four bytes array.
5158c2ecf20Sopenharmony_ci *
5168c2ecf20Sopenharmony_ci * Should be used by NAND controller drivers that do not support the SET/GET
5178c2ecf20Sopenharmony_ci * FEATURES operations.
5188c2ecf20Sopenharmony_ci */
5198c2ecf20Sopenharmony_ciint nand_get_set_features_notsupp(struct nand_chip *chip, int addr,
5208c2ecf20Sopenharmony_ci				  u8 *subfeature_param)
5218c2ecf20Sopenharmony_ci{
5228c2ecf20Sopenharmony_ci	return -ENOTSUPP;
5238c2ecf20Sopenharmony_ci}
5248c2ecf20Sopenharmony_ciEXPORT_SYMBOL(nand_get_set_features_notsupp);
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci/**
5278c2ecf20Sopenharmony_ci * nand_wait - [DEFAULT] wait until the command is done
5288c2ecf20Sopenharmony_ci * @chip: NAND chip structure
5298c2ecf20Sopenharmony_ci *
5308c2ecf20Sopenharmony_ci * Wait for command done. This applies to erase and program only.
5318c2ecf20Sopenharmony_ci */
5328c2ecf20Sopenharmony_cistatic int nand_wait(struct nand_chip *chip)
5338c2ecf20Sopenharmony_ci{
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	unsigned long timeo = 400;
5368c2ecf20Sopenharmony_ci	u8 status;
5378c2ecf20Sopenharmony_ci	int ret;
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci	/*
5408c2ecf20Sopenharmony_ci	 * Apply this short delay always to ensure that we do wait tWB in any
5418c2ecf20Sopenharmony_ci	 * case on any machine.
5428c2ecf20Sopenharmony_ci	 */
5438c2ecf20Sopenharmony_ci	ndelay(100);
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci	ret = nand_status_op(chip, NULL);
5468c2ecf20Sopenharmony_ci	if (ret)
5478c2ecf20Sopenharmony_ci		return ret;
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci	if (in_interrupt() || oops_in_progress)
5508c2ecf20Sopenharmony_ci		panic_nand_wait(chip, timeo);
5518c2ecf20Sopenharmony_ci	else {
5528c2ecf20Sopenharmony_ci		timeo = jiffies + msecs_to_jiffies(timeo);
5538c2ecf20Sopenharmony_ci		do {
5548c2ecf20Sopenharmony_ci			if (chip->legacy.dev_ready) {
5558c2ecf20Sopenharmony_ci				if (chip->legacy.dev_ready(chip))
5568c2ecf20Sopenharmony_ci					break;
5578c2ecf20Sopenharmony_ci			} else {
5588c2ecf20Sopenharmony_ci				ret = nand_read_data_op(chip, &status,
5598c2ecf20Sopenharmony_ci							sizeof(status), true,
5608c2ecf20Sopenharmony_ci							false);
5618c2ecf20Sopenharmony_ci				if (ret)
5628c2ecf20Sopenharmony_ci					return ret;
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci				if (status & NAND_STATUS_READY)
5658c2ecf20Sopenharmony_ci					break;
5668c2ecf20Sopenharmony_ci			}
5678c2ecf20Sopenharmony_ci			cond_resched();
5688c2ecf20Sopenharmony_ci		} while (time_before(jiffies, timeo));
5698c2ecf20Sopenharmony_ci	}
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci	ret = nand_read_data_op(chip, &status, sizeof(status), true, false);
5728c2ecf20Sopenharmony_ci	if (ret)
5738c2ecf20Sopenharmony_ci		return ret;
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_ci	/* This can happen if in case of timeout or buggy dev_ready */
5768c2ecf20Sopenharmony_ci	WARN_ON(!(status & NAND_STATUS_READY));
5778c2ecf20Sopenharmony_ci	return status;
5788c2ecf20Sopenharmony_ci}
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_civoid nand_legacy_set_defaults(struct nand_chip *chip)
5818c2ecf20Sopenharmony_ci{
5828c2ecf20Sopenharmony_ci	unsigned int busw = chip->options & NAND_BUSWIDTH_16;
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_ci	if (nand_has_exec_op(chip))
5858c2ecf20Sopenharmony_ci		return;
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci	/* check for proper chip_delay setup, set 20us if not */
5888c2ecf20Sopenharmony_ci	if (!chip->legacy.chip_delay)
5898c2ecf20Sopenharmony_ci		chip->legacy.chip_delay = 20;
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	/* check, if a user supplied command function given */
5928c2ecf20Sopenharmony_ci	if (!chip->legacy.cmdfunc)
5938c2ecf20Sopenharmony_ci		chip->legacy.cmdfunc = nand_command;
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci	/* check, if a user supplied wait function given */
5968c2ecf20Sopenharmony_ci	if (chip->legacy.waitfunc == NULL)
5978c2ecf20Sopenharmony_ci		chip->legacy.waitfunc = nand_wait;
5988c2ecf20Sopenharmony_ci
5998c2ecf20Sopenharmony_ci	if (!chip->legacy.select_chip)
6008c2ecf20Sopenharmony_ci		chip->legacy.select_chip = nand_select_chip;
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci	/* If called twice, pointers that depend on busw may need to be reset */
6038c2ecf20Sopenharmony_ci	if (!chip->legacy.read_byte || chip->legacy.read_byte == nand_read_byte)
6048c2ecf20Sopenharmony_ci		chip->legacy.read_byte = busw ? nand_read_byte16 : nand_read_byte;
6058c2ecf20Sopenharmony_ci	if (!chip->legacy.write_buf || chip->legacy.write_buf == nand_write_buf)
6068c2ecf20Sopenharmony_ci		chip->legacy.write_buf = busw ? nand_write_buf16 : nand_write_buf;
6078c2ecf20Sopenharmony_ci	if (!chip->legacy.write_byte || chip->legacy.write_byte == nand_write_byte)
6088c2ecf20Sopenharmony_ci		chip->legacy.write_byte = busw ? nand_write_byte16 : nand_write_byte;
6098c2ecf20Sopenharmony_ci	if (!chip->legacy.read_buf || chip->legacy.read_buf == nand_read_buf)
6108c2ecf20Sopenharmony_ci		chip->legacy.read_buf = busw ? nand_read_buf16 : nand_read_buf;
6118c2ecf20Sopenharmony_ci}
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_civoid nand_legacy_adjust_cmdfunc(struct nand_chip *chip)
6148c2ecf20Sopenharmony_ci{
6158c2ecf20Sopenharmony_ci	struct mtd_info *mtd = nand_to_mtd(chip);
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	/* Do not replace user supplied command function! */
6188c2ecf20Sopenharmony_ci	if (mtd->writesize > 512 && chip->legacy.cmdfunc == nand_command)
6198c2ecf20Sopenharmony_ci		chip->legacy.cmdfunc = nand_command_lp;
6208c2ecf20Sopenharmony_ci}
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ciint nand_legacy_check_hooks(struct nand_chip *chip)
6238c2ecf20Sopenharmony_ci{
6248c2ecf20Sopenharmony_ci	/*
6258c2ecf20Sopenharmony_ci	 * ->legacy.cmdfunc() is legacy and will only be used if ->exec_op() is
6268c2ecf20Sopenharmony_ci	 * not populated.
6278c2ecf20Sopenharmony_ci	 */
6288c2ecf20Sopenharmony_ci	if (nand_has_exec_op(chip))
6298c2ecf20Sopenharmony_ci		return 0;
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci	/*
6328c2ecf20Sopenharmony_ci	 * Default functions assigned for ->legacy.cmdfunc() and
6338c2ecf20Sopenharmony_ci	 * ->legacy.select_chip() both expect ->legacy.cmd_ctrl() to be
6348c2ecf20Sopenharmony_ci	 *  populated.
6358c2ecf20Sopenharmony_ci	 */
6368c2ecf20Sopenharmony_ci	if ((!chip->legacy.cmdfunc || !chip->legacy.select_chip) &&
6378c2ecf20Sopenharmony_ci	    !chip->legacy.cmd_ctrl) {
6388c2ecf20Sopenharmony_ci		pr_err("->legacy.cmd_ctrl() should be provided\n");
6398c2ecf20Sopenharmony_ci		return -EINVAL;
6408c2ecf20Sopenharmony_ci	}
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_ci	return 0;
6438c2ecf20Sopenharmony_ci}
644