13d4af7c1SBoris Brezillon // SPDX-License-Identifier: GPL-2.0 23d4af7c1SBoris Brezillon /* 33d4af7c1SBoris Brezillon * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com) 43d4af7c1SBoris Brezillon * 2002-2006 Thomas Gleixner (tglx@linutronix.de) 53d4af7c1SBoris Brezillon * 63d4af7c1SBoris Brezillon * Credits: 73d4af7c1SBoris Brezillon * David Woodhouse for adding multichip support 83d4af7c1SBoris Brezillon * 93d4af7c1SBoris Brezillon * Aleph One Ltd. and Toby Churchill Ltd. for supporting the 103d4af7c1SBoris Brezillon * rework for 2K page size chips 113d4af7c1SBoris Brezillon * 123d4af7c1SBoris Brezillon * This file contains all legacy helpers/code that should be removed 133d4af7c1SBoris Brezillon * at some point. 143d4af7c1SBoris Brezillon */ 153d4af7c1SBoris Brezillon 163d4af7c1SBoris Brezillon #include <linux/delay.h> 173d4af7c1SBoris Brezillon #include <linux/io.h> 183d4af7c1SBoris Brezillon #include <linux/nmi.h> 193d4af7c1SBoris Brezillon 203d4af7c1SBoris Brezillon #include "internals.h" 213d4af7c1SBoris Brezillon 223d4af7c1SBoris Brezillon /** 233d4af7c1SBoris Brezillon * nand_read_byte - [DEFAULT] read one byte from the chip 243d4af7c1SBoris Brezillon * @chip: NAND chip object 253d4af7c1SBoris Brezillon * 263d4af7c1SBoris Brezillon * Default read function for 8bit buswidth 273d4af7c1SBoris Brezillon */ 283d4af7c1SBoris Brezillon static uint8_t nand_read_byte(struct nand_chip *chip) 293d4af7c1SBoris Brezillon { 303d4af7c1SBoris Brezillon return readb(chip->legacy.IO_ADDR_R); 313d4af7c1SBoris Brezillon } 323d4af7c1SBoris Brezillon 333d4af7c1SBoris Brezillon /** 343d4af7c1SBoris Brezillon * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip 353d4af7c1SBoris Brezillon * @chip: NAND chip object 363d4af7c1SBoris Brezillon * 373d4af7c1SBoris Brezillon * Default read function for 16bit buswidth with endianness conversion. 383d4af7c1SBoris Brezillon * 393d4af7c1SBoris Brezillon */ 403d4af7c1SBoris Brezillon static uint8_t nand_read_byte16(struct nand_chip *chip) 413d4af7c1SBoris Brezillon { 423d4af7c1SBoris Brezillon return (uint8_t) cpu_to_le16(readw(chip->legacy.IO_ADDR_R)); 433d4af7c1SBoris Brezillon } 443d4af7c1SBoris Brezillon 453d4af7c1SBoris Brezillon /** 463d4af7c1SBoris Brezillon * nand_select_chip - [DEFAULT] control CE line 473d4af7c1SBoris Brezillon * @chip: NAND chip object 483d4af7c1SBoris Brezillon * @chipnr: chipnumber to select, -1 for deselect 493d4af7c1SBoris Brezillon * 503d4af7c1SBoris Brezillon * Default select function for 1 chip devices. 513d4af7c1SBoris Brezillon */ 523d4af7c1SBoris Brezillon static void nand_select_chip(struct nand_chip *chip, int chipnr) 533d4af7c1SBoris Brezillon { 543d4af7c1SBoris Brezillon switch (chipnr) { 553d4af7c1SBoris Brezillon case -1: 563d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE, 573d4af7c1SBoris Brezillon 0 | NAND_CTRL_CHANGE); 583d4af7c1SBoris Brezillon break; 593d4af7c1SBoris Brezillon case 0: 603d4af7c1SBoris Brezillon break; 613d4af7c1SBoris Brezillon 623d4af7c1SBoris Brezillon default: 633d4af7c1SBoris Brezillon BUG(); 643d4af7c1SBoris Brezillon } 653d4af7c1SBoris Brezillon } 663d4af7c1SBoris Brezillon 673d4af7c1SBoris Brezillon /** 683d4af7c1SBoris Brezillon * nand_write_byte - [DEFAULT] write single byte to chip 693d4af7c1SBoris Brezillon * @chip: NAND chip object 703d4af7c1SBoris Brezillon * @byte: value to write 713d4af7c1SBoris Brezillon * 723d4af7c1SBoris Brezillon * Default function to write a byte to I/O[7:0] 733d4af7c1SBoris Brezillon */ 743d4af7c1SBoris Brezillon static void nand_write_byte(struct nand_chip *chip, uint8_t byte) 753d4af7c1SBoris Brezillon { 763d4af7c1SBoris Brezillon chip->legacy.write_buf(chip, &byte, 1); 773d4af7c1SBoris Brezillon } 783d4af7c1SBoris Brezillon 793d4af7c1SBoris Brezillon /** 803d4af7c1SBoris Brezillon * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16 813d4af7c1SBoris Brezillon * @chip: NAND chip object 823d4af7c1SBoris Brezillon * @byte: value to write 833d4af7c1SBoris Brezillon * 843d4af7c1SBoris Brezillon * Default function to write a byte to I/O[7:0] on a 16-bit wide chip. 853d4af7c1SBoris Brezillon */ 863d4af7c1SBoris Brezillon static void nand_write_byte16(struct nand_chip *chip, uint8_t byte) 873d4af7c1SBoris Brezillon { 883d4af7c1SBoris Brezillon uint16_t word = byte; 893d4af7c1SBoris Brezillon 903d4af7c1SBoris Brezillon /* 913d4af7c1SBoris Brezillon * It's not entirely clear what should happen to I/O[15:8] when writing 923d4af7c1SBoris Brezillon * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads: 933d4af7c1SBoris Brezillon * 943d4af7c1SBoris Brezillon * When the host supports a 16-bit bus width, only data is 953d4af7c1SBoris Brezillon * transferred at the 16-bit width. All address and command line 963d4af7c1SBoris Brezillon * transfers shall use only the lower 8-bits of the data bus. During 973d4af7c1SBoris Brezillon * command transfers, the host may place any value on the upper 983d4af7c1SBoris Brezillon * 8-bits of the data bus. During address transfers, the host shall 993d4af7c1SBoris Brezillon * set the upper 8-bits of the data bus to 00h. 1003d4af7c1SBoris Brezillon * 1013d4af7c1SBoris Brezillon * One user of the write_byte callback is nand_set_features. The 1023d4af7c1SBoris Brezillon * four parameters are specified to be written to I/O[7:0], but this is 1033d4af7c1SBoris Brezillon * neither an address nor a command transfer. Let's assume a 0 on the 1043d4af7c1SBoris Brezillon * upper I/O lines is OK. 1053d4af7c1SBoris Brezillon */ 1063d4af7c1SBoris Brezillon chip->legacy.write_buf(chip, (uint8_t *)&word, 2); 1073d4af7c1SBoris Brezillon } 1083d4af7c1SBoris Brezillon 1093d4af7c1SBoris Brezillon /** 1103d4af7c1SBoris Brezillon * nand_write_buf - [DEFAULT] write buffer to chip 1113d4af7c1SBoris Brezillon * @chip: NAND chip object 1123d4af7c1SBoris Brezillon * @buf: data buffer 1133d4af7c1SBoris Brezillon * @len: number of bytes to write 1143d4af7c1SBoris Brezillon * 1153d4af7c1SBoris Brezillon * Default write function for 8bit buswidth. 1163d4af7c1SBoris Brezillon */ 1173d4af7c1SBoris Brezillon static void nand_write_buf(struct nand_chip *chip, const uint8_t *buf, int len) 1183d4af7c1SBoris Brezillon { 1193d4af7c1SBoris Brezillon iowrite8_rep(chip->legacy.IO_ADDR_W, buf, len); 1203d4af7c1SBoris Brezillon } 1213d4af7c1SBoris Brezillon 1223d4af7c1SBoris Brezillon /** 1233d4af7c1SBoris Brezillon * nand_read_buf - [DEFAULT] read chip data into buffer 1243d4af7c1SBoris Brezillon * @chip: NAND chip object 1253d4af7c1SBoris Brezillon * @buf: buffer to store date 1263d4af7c1SBoris Brezillon * @len: number of bytes to read 1273d4af7c1SBoris Brezillon * 1283d4af7c1SBoris Brezillon * Default read function for 8bit buswidth. 1293d4af7c1SBoris Brezillon */ 1303d4af7c1SBoris Brezillon static void nand_read_buf(struct nand_chip *chip, uint8_t *buf, int len) 1313d4af7c1SBoris Brezillon { 1323d4af7c1SBoris Brezillon ioread8_rep(chip->legacy.IO_ADDR_R, buf, len); 1333d4af7c1SBoris Brezillon } 1343d4af7c1SBoris Brezillon 1353d4af7c1SBoris Brezillon /** 1363d4af7c1SBoris Brezillon * nand_write_buf16 - [DEFAULT] write buffer to chip 1373d4af7c1SBoris Brezillon * @chip: NAND chip object 1383d4af7c1SBoris Brezillon * @buf: data buffer 1393d4af7c1SBoris Brezillon * @len: number of bytes to write 1403d4af7c1SBoris Brezillon * 1413d4af7c1SBoris Brezillon * Default write function for 16bit buswidth. 1423d4af7c1SBoris Brezillon */ 1433d4af7c1SBoris Brezillon static void nand_write_buf16(struct nand_chip *chip, const uint8_t *buf, 1443d4af7c1SBoris Brezillon int len) 1453d4af7c1SBoris Brezillon { 1463d4af7c1SBoris Brezillon u16 *p = (u16 *) buf; 1473d4af7c1SBoris Brezillon 1483d4af7c1SBoris Brezillon iowrite16_rep(chip->legacy.IO_ADDR_W, p, len >> 1); 1493d4af7c1SBoris Brezillon } 1503d4af7c1SBoris Brezillon 1513d4af7c1SBoris Brezillon /** 1523d4af7c1SBoris Brezillon * nand_read_buf16 - [DEFAULT] read chip data into buffer 1533d4af7c1SBoris Brezillon * @chip: NAND chip object 1543d4af7c1SBoris Brezillon * @buf: buffer to store date 1553d4af7c1SBoris Brezillon * @len: number of bytes to read 1563d4af7c1SBoris Brezillon * 1573d4af7c1SBoris Brezillon * Default read function for 16bit buswidth. 1583d4af7c1SBoris Brezillon */ 1593d4af7c1SBoris Brezillon static void nand_read_buf16(struct nand_chip *chip, uint8_t *buf, int len) 1603d4af7c1SBoris Brezillon { 1613d4af7c1SBoris Brezillon u16 *p = (u16 *) buf; 1623d4af7c1SBoris Brezillon 1633d4af7c1SBoris Brezillon ioread16_rep(chip->legacy.IO_ADDR_R, p, len >> 1); 1643d4af7c1SBoris Brezillon } 1653d4af7c1SBoris Brezillon 1663d4af7c1SBoris Brezillon /** 1673d4af7c1SBoris Brezillon * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands. 1680813621bSBoris Brezillon * @chip: NAND chip object 1693d4af7c1SBoris Brezillon * @timeo: Timeout 1703d4af7c1SBoris Brezillon * 1713d4af7c1SBoris Brezillon * Helper function for nand_wait_ready used when needing to wait in interrupt 1723d4af7c1SBoris Brezillon * context. 1733d4af7c1SBoris Brezillon */ 1740813621bSBoris Brezillon static void panic_nand_wait_ready(struct nand_chip *chip, unsigned long timeo) 1753d4af7c1SBoris Brezillon { 1763d4af7c1SBoris Brezillon int i; 1773d4af7c1SBoris Brezillon 1783d4af7c1SBoris Brezillon /* Wait for the device to get ready */ 1793d4af7c1SBoris Brezillon for (i = 0; i < timeo; i++) { 1803d4af7c1SBoris Brezillon if (chip->legacy.dev_ready(chip)) 1813d4af7c1SBoris Brezillon break; 1823d4af7c1SBoris Brezillon touch_softlockup_watchdog(); 1833d4af7c1SBoris Brezillon mdelay(1); 1843d4af7c1SBoris Brezillon } 1853d4af7c1SBoris Brezillon } 1863d4af7c1SBoris Brezillon 1873d4af7c1SBoris Brezillon /** 1883d4af7c1SBoris Brezillon * nand_wait_ready - [GENERIC] Wait for the ready pin after commands. 1893d4af7c1SBoris Brezillon * @chip: NAND chip object 1903d4af7c1SBoris Brezillon * 1913d4af7c1SBoris Brezillon * Wait for the ready pin after a command, and warn if a timeout occurs. 1923d4af7c1SBoris Brezillon */ 1933d4af7c1SBoris Brezillon void nand_wait_ready(struct nand_chip *chip) 1943d4af7c1SBoris Brezillon { 195*875330f8SThomas Gleixner struct mtd_info *mtd = nand_to_mtd(chip); 1963d4af7c1SBoris Brezillon unsigned long timeo = 400; 1973d4af7c1SBoris Brezillon 198*875330f8SThomas Gleixner if (mtd->oops_panic_write) 1990813621bSBoris Brezillon return panic_nand_wait_ready(chip, timeo); 2003d4af7c1SBoris Brezillon 2013d4af7c1SBoris Brezillon /* Wait until command is processed or timeout occurs */ 2023d4af7c1SBoris Brezillon timeo = jiffies + msecs_to_jiffies(timeo); 2033d4af7c1SBoris Brezillon do { 2043d4af7c1SBoris Brezillon if (chip->legacy.dev_ready(chip)) 2053d4af7c1SBoris Brezillon return; 2063d4af7c1SBoris Brezillon cond_resched(); 2073d4af7c1SBoris Brezillon } while (time_before(jiffies, timeo)); 2083d4af7c1SBoris Brezillon 2093d4af7c1SBoris Brezillon if (!chip->legacy.dev_ready(chip)) 2103d4af7c1SBoris Brezillon pr_warn_ratelimited("timeout while waiting for chip to become ready\n"); 2113d4af7c1SBoris Brezillon } 2123d4af7c1SBoris Brezillon EXPORT_SYMBOL_GPL(nand_wait_ready); 2133d4af7c1SBoris Brezillon 2143d4af7c1SBoris Brezillon /** 2153d4af7c1SBoris Brezillon * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands. 2160813621bSBoris Brezillon * @chip: NAND chip object 2173d4af7c1SBoris Brezillon * @timeo: Timeout in ms 2183d4af7c1SBoris Brezillon * 2193d4af7c1SBoris Brezillon * Wait for status ready (i.e. command done) or timeout. 2203d4af7c1SBoris Brezillon */ 2210813621bSBoris Brezillon static void nand_wait_status_ready(struct nand_chip *chip, unsigned long timeo) 2223d4af7c1SBoris Brezillon { 2233d4af7c1SBoris Brezillon int ret; 2243d4af7c1SBoris Brezillon 2253d4af7c1SBoris Brezillon timeo = jiffies + msecs_to_jiffies(timeo); 2263d4af7c1SBoris Brezillon do { 2273d4af7c1SBoris Brezillon u8 status; 2283d4af7c1SBoris Brezillon 229b451f5beSMiquel Raynal ret = nand_read_data_op(chip, &status, sizeof(status), true, 230b451f5beSMiquel Raynal false); 2313d4af7c1SBoris Brezillon if (ret) 2323d4af7c1SBoris Brezillon return; 2333d4af7c1SBoris Brezillon 2343d4af7c1SBoris Brezillon if (status & NAND_STATUS_READY) 2353d4af7c1SBoris Brezillon break; 2363d4af7c1SBoris Brezillon touch_softlockup_watchdog(); 2373d4af7c1SBoris Brezillon } while (time_before(jiffies, timeo)); 2383d4af7c1SBoris Brezillon }; 2393d4af7c1SBoris Brezillon 2403d4af7c1SBoris Brezillon /** 2413d4af7c1SBoris Brezillon * nand_command - [DEFAULT] Send command to NAND device 2423d4af7c1SBoris Brezillon * @chip: NAND chip object 2433d4af7c1SBoris Brezillon * @command: the command to be sent 2443d4af7c1SBoris Brezillon * @column: the column address for this command, -1 if none 2453d4af7c1SBoris Brezillon * @page_addr: the page address for this command, -1 if none 2463d4af7c1SBoris Brezillon * 2473d4af7c1SBoris Brezillon * Send command to NAND device. This function is used for small page devices 2483d4af7c1SBoris Brezillon * (512 Bytes per page). 2493d4af7c1SBoris Brezillon */ 2503d4af7c1SBoris Brezillon static void nand_command(struct nand_chip *chip, unsigned int command, 2513d4af7c1SBoris Brezillon int column, int page_addr) 2523d4af7c1SBoris Brezillon { 2533d4af7c1SBoris Brezillon struct mtd_info *mtd = nand_to_mtd(chip); 2543d4af7c1SBoris Brezillon int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE; 2553d4af7c1SBoris Brezillon 2563d4af7c1SBoris Brezillon /* Write out the command to the device */ 2573d4af7c1SBoris Brezillon if (command == NAND_CMD_SEQIN) { 2583d4af7c1SBoris Brezillon int readcmd; 2593d4af7c1SBoris Brezillon 2603d4af7c1SBoris Brezillon if (column >= mtd->writesize) { 2613d4af7c1SBoris Brezillon /* OOB area */ 2623d4af7c1SBoris Brezillon column -= mtd->writesize; 2633d4af7c1SBoris Brezillon readcmd = NAND_CMD_READOOB; 2643d4af7c1SBoris Brezillon } else if (column < 256) { 2653d4af7c1SBoris Brezillon /* First 256 bytes --> READ0 */ 2663d4af7c1SBoris Brezillon readcmd = NAND_CMD_READ0; 2673d4af7c1SBoris Brezillon } else { 2683d4af7c1SBoris Brezillon column -= 256; 2693d4af7c1SBoris Brezillon readcmd = NAND_CMD_READ1; 2703d4af7c1SBoris Brezillon } 2713d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, readcmd, ctrl); 2723d4af7c1SBoris Brezillon ctrl &= ~NAND_CTRL_CHANGE; 2733d4af7c1SBoris Brezillon } 2743d4af7c1SBoris Brezillon if (command != NAND_CMD_NONE) 2753d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, command, ctrl); 2763d4af7c1SBoris Brezillon 2773d4af7c1SBoris Brezillon /* Address cycle, when necessary */ 2783d4af7c1SBoris Brezillon ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE; 2793d4af7c1SBoris Brezillon /* Serially input address */ 2803d4af7c1SBoris Brezillon if (column != -1) { 2813d4af7c1SBoris Brezillon /* Adjust columns for 16 bit buswidth */ 2823d4af7c1SBoris Brezillon if (chip->options & NAND_BUSWIDTH_16 && 2833d4af7c1SBoris Brezillon !nand_opcode_8bits(command)) 2843d4af7c1SBoris Brezillon column >>= 1; 2853d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, column, ctrl); 2863d4af7c1SBoris Brezillon ctrl &= ~NAND_CTRL_CHANGE; 2873d4af7c1SBoris Brezillon } 2883d4af7c1SBoris Brezillon if (page_addr != -1) { 2893d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, page_addr, ctrl); 2903d4af7c1SBoris Brezillon ctrl &= ~NAND_CTRL_CHANGE; 2913d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, page_addr >> 8, ctrl); 2923d4af7c1SBoris Brezillon if (chip->options & NAND_ROW_ADDR_3) 2933d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, page_addr >> 16, ctrl); 2943d4af7c1SBoris Brezillon } 2953d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE, 2963d4af7c1SBoris Brezillon NAND_NCE | NAND_CTRL_CHANGE); 2973d4af7c1SBoris Brezillon 2983d4af7c1SBoris Brezillon /* 2993d4af7c1SBoris Brezillon * Program and erase have their own busy handlers status and sequential 3003d4af7c1SBoris Brezillon * in needs no delay 3013d4af7c1SBoris Brezillon */ 3023d4af7c1SBoris Brezillon switch (command) { 3033d4af7c1SBoris Brezillon 3043d4af7c1SBoris Brezillon case NAND_CMD_NONE: 3053d4af7c1SBoris Brezillon case NAND_CMD_PAGEPROG: 3063d4af7c1SBoris Brezillon case NAND_CMD_ERASE1: 3073d4af7c1SBoris Brezillon case NAND_CMD_ERASE2: 3083d4af7c1SBoris Brezillon case NAND_CMD_SEQIN: 3093d4af7c1SBoris Brezillon case NAND_CMD_STATUS: 3103d4af7c1SBoris Brezillon case NAND_CMD_READID: 3113d4af7c1SBoris Brezillon case NAND_CMD_SET_FEATURES: 3123d4af7c1SBoris Brezillon return; 3133d4af7c1SBoris Brezillon 3143d4af7c1SBoris Brezillon case NAND_CMD_RESET: 3153d4af7c1SBoris Brezillon if (chip->legacy.dev_ready) 3163d4af7c1SBoris Brezillon break; 3173d4af7c1SBoris Brezillon udelay(chip->legacy.chip_delay); 3183d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, NAND_CMD_STATUS, 3193d4af7c1SBoris Brezillon NAND_CTRL_CLE | NAND_CTRL_CHANGE); 3203d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE, 3213d4af7c1SBoris Brezillon NAND_NCE | NAND_CTRL_CHANGE); 3223d4af7c1SBoris Brezillon /* EZ-NAND can take upto 250ms as per ONFi v4.0 */ 3230813621bSBoris Brezillon nand_wait_status_ready(chip, 250); 3243d4af7c1SBoris Brezillon return; 3253d4af7c1SBoris Brezillon 3263d4af7c1SBoris Brezillon /* This applies to read commands */ 3273d4af7c1SBoris Brezillon case NAND_CMD_READ0: 3283d4af7c1SBoris Brezillon /* 3293d4af7c1SBoris Brezillon * READ0 is sometimes used to exit GET STATUS mode. When this 3303d4af7c1SBoris Brezillon * is the case no address cycles are requested, and we can use 3313d4af7c1SBoris Brezillon * this information to detect that we should not wait for the 3323d4af7c1SBoris Brezillon * device to be ready. 3333d4af7c1SBoris Brezillon */ 3343d4af7c1SBoris Brezillon if (column == -1 && page_addr == -1) 3353d4af7c1SBoris Brezillon return; 336025a06c1SMiquel Raynal fallthrough; 3373d4af7c1SBoris Brezillon default: 3383d4af7c1SBoris Brezillon /* 3393d4af7c1SBoris Brezillon * If we don't have access to the busy pin, we apply the given 3403d4af7c1SBoris Brezillon * command delay 3413d4af7c1SBoris Brezillon */ 3423d4af7c1SBoris Brezillon if (!chip->legacy.dev_ready) { 3433d4af7c1SBoris Brezillon udelay(chip->legacy.chip_delay); 3443d4af7c1SBoris Brezillon return; 3453d4af7c1SBoris Brezillon } 3463d4af7c1SBoris Brezillon } 3473d4af7c1SBoris Brezillon /* 3483d4af7c1SBoris Brezillon * Apply this short delay always to ensure that we do wait tWB in 3493d4af7c1SBoris Brezillon * any case on any machine. 3503d4af7c1SBoris Brezillon */ 3513d4af7c1SBoris Brezillon ndelay(100); 3523d4af7c1SBoris Brezillon 3533d4af7c1SBoris Brezillon nand_wait_ready(chip); 3543d4af7c1SBoris Brezillon } 3553d4af7c1SBoris Brezillon 3563d4af7c1SBoris Brezillon static void nand_ccs_delay(struct nand_chip *chip) 3573d4af7c1SBoris Brezillon { 358e0160cd4SMiquel Raynal const struct nand_sdr_timings *sdr = 359e0160cd4SMiquel Raynal nand_get_sdr_timings(nand_get_interface_config(chip)); 360e0160cd4SMiquel Raynal 3613d4af7c1SBoris Brezillon /* 3623d4af7c1SBoris Brezillon * The controller already takes care of waiting for tCCS when the RNDIN 3633d4af7c1SBoris Brezillon * or RNDOUT command is sent, return directly. 3643d4af7c1SBoris Brezillon */ 3653d4af7c1SBoris Brezillon if (!(chip->options & NAND_WAIT_TCCS)) 3663d4af7c1SBoris Brezillon return; 3673d4af7c1SBoris Brezillon 3683d4af7c1SBoris Brezillon /* 3693d4af7c1SBoris Brezillon * Wait tCCS_min if it is correctly defined, otherwise wait 500ns 3703d4af7c1SBoris Brezillon * (which should be safe for all NANDs). 3713d4af7c1SBoris Brezillon */ 3724c46667bSMiquel Raynal if (nand_controller_can_setup_interface(chip)) 373e0160cd4SMiquel Raynal ndelay(sdr->tCCS_min / 1000); 3743d4af7c1SBoris Brezillon else 3753d4af7c1SBoris Brezillon ndelay(500); 3763d4af7c1SBoris Brezillon } 3773d4af7c1SBoris Brezillon 3783d4af7c1SBoris Brezillon /** 3793d4af7c1SBoris Brezillon * nand_command_lp - [DEFAULT] Send command to NAND large page device 3803d4af7c1SBoris Brezillon * @chip: NAND chip object 3813d4af7c1SBoris Brezillon * @command: the command to be sent 3823d4af7c1SBoris Brezillon * @column: the column address for this command, -1 if none 3833d4af7c1SBoris Brezillon * @page_addr: the page address for this command, -1 if none 3843d4af7c1SBoris Brezillon * 3853d4af7c1SBoris Brezillon * Send command to NAND device. This is the version for the new large page 3863d4af7c1SBoris Brezillon * devices. We don't have the separate regions as we have in the small page 3873d4af7c1SBoris Brezillon * devices. We must emulate NAND_CMD_READOOB to keep the code compatible. 3883d4af7c1SBoris Brezillon */ 3893d4af7c1SBoris Brezillon static void nand_command_lp(struct nand_chip *chip, unsigned int command, 3903d4af7c1SBoris Brezillon int column, int page_addr) 3913d4af7c1SBoris Brezillon { 3923d4af7c1SBoris Brezillon struct mtd_info *mtd = nand_to_mtd(chip); 3933d4af7c1SBoris Brezillon 3943d4af7c1SBoris Brezillon /* Emulate NAND_CMD_READOOB */ 3953d4af7c1SBoris Brezillon if (command == NAND_CMD_READOOB) { 3963d4af7c1SBoris Brezillon column += mtd->writesize; 3973d4af7c1SBoris Brezillon command = NAND_CMD_READ0; 3983d4af7c1SBoris Brezillon } 3993d4af7c1SBoris Brezillon 4003d4af7c1SBoris Brezillon /* Command latch cycle */ 4013d4af7c1SBoris Brezillon if (command != NAND_CMD_NONE) 4023d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, command, 4033d4af7c1SBoris Brezillon NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); 4043d4af7c1SBoris Brezillon 4053d4af7c1SBoris Brezillon if (column != -1 || page_addr != -1) { 4063d4af7c1SBoris Brezillon int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE; 4073d4af7c1SBoris Brezillon 4083d4af7c1SBoris Brezillon /* Serially input address */ 4093d4af7c1SBoris Brezillon if (column != -1) { 4103d4af7c1SBoris Brezillon /* Adjust columns for 16 bit buswidth */ 4113d4af7c1SBoris Brezillon if (chip->options & NAND_BUSWIDTH_16 && 4123d4af7c1SBoris Brezillon !nand_opcode_8bits(command)) 4133d4af7c1SBoris Brezillon column >>= 1; 4143d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, column, ctrl); 4153d4af7c1SBoris Brezillon ctrl &= ~NAND_CTRL_CHANGE; 4163d4af7c1SBoris Brezillon 4173d4af7c1SBoris Brezillon /* Only output a single addr cycle for 8bits opcodes. */ 4183d4af7c1SBoris Brezillon if (!nand_opcode_8bits(command)) 4193d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, column >> 8, ctrl); 4203d4af7c1SBoris Brezillon } 4213d4af7c1SBoris Brezillon if (page_addr != -1) { 4223d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, page_addr, ctrl); 4233d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, page_addr >> 8, 4243d4af7c1SBoris Brezillon NAND_NCE | NAND_ALE); 4253d4af7c1SBoris Brezillon if (chip->options & NAND_ROW_ADDR_3) 4263d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, page_addr >> 16, 4273d4af7c1SBoris Brezillon NAND_NCE | NAND_ALE); 4283d4af7c1SBoris Brezillon } 4293d4af7c1SBoris Brezillon } 4303d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE, 4313d4af7c1SBoris Brezillon NAND_NCE | NAND_CTRL_CHANGE); 4323d4af7c1SBoris Brezillon 4333d4af7c1SBoris Brezillon /* 4343d4af7c1SBoris Brezillon * Program and erase have their own busy handlers status, sequential 4353d4af7c1SBoris Brezillon * in and status need no delay. 4363d4af7c1SBoris Brezillon */ 4373d4af7c1SBoris Brezillon switch (command) { 4383d4af7c1SBoris Brezillon 4393d4af7c1SBoris Brezillon case NAND_CMD_NONE: 4403d4af7c1SBoris Brezillon case NAND_CMD_CACHEDPROG: 4413d4af7c1SBoris Brezillon case NAND_CMD_PAGEPROG: 4423d4af7c1SBoris Brezillon case NAND_CMD_ERASE1: 4433d4af7c1SBoris Brezillon case NAND_CMD_ERASE2: 4443d4af7c1SBoris Brezillon case NAND_CMD_SEQIN: 4453d4af7c1SBoris Brezillon case NAND_CMD_STATUS: 4463d4af7c1SBoris Brezillon case NAND_CMD_READID: 4473d4af7c1SBoris Brezillon case NAND_CMD_SET_FEATURES: 4483d4af7c1SBoris Brezillon return; 4493d4af7c1SBoris Brezillon 4503d4af7c1SBoris Brezillon case NAND_CMD_RNDIN: 4513d4af7c1SBoris Brezillon nand_ccs_delay(chip); 4523d4af7c1SBoris Brezillon return; 4533d4af7c1SBoris Brezillon 4543d4af7c1SBoris Brezillon case NAND_CMD_RESET: 4553d4af7c1SBoris Brezillon if (chip->legacy.dev_ready) 4563d4af7c1SBoris Brezillon break; 4573d4af7c1SBoris Brezillon udelay(chip->legacy.chip_delay); 4583d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, NAND_CMD_STATUS, 4593d4af7c1SBoris Brezillon NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); 4603d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE, 4613d4af7c1SBoris Brezillon NAND_NCE | NAND_CTRL_CHANGE); 4623d4af7c1SBoris Brezillon /* EZ-NAND can take upto 250ms as per ONFi v4.0 */ 4630813621bSBoris Brezillon nand_wait_status_ready(chip, 250); 4643d4af7c1SBoris Brezillon return; 4653d4af7c1SBoris Brezillon 4663d4af7c1SBoris Brezillon case NAND_CMD_RNDOUT: 4673d4af7c1SBoris Brezillon /* No ready / busy check necessary */ 4683d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, NAND_CMD_RNDOUTSTART, 4693d4af7c1SBoris Brezillon NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); 4703d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE, 4713d4af7c1SBoris Brezillon NAND_NCE | NAND_CTRL_CHANGE); 4723d4af7c1SBoris Brezillon 4733d4af7c1SBoris Brezillon nand_ccs_delay(chip); 4743d4af7c1SBoris Brezillon return; 4753d4af7c1SBoris Brezillon 4763d4af7c1SBoris Brezillon case NAND_CMD_READ0: 4773d4af7c1SBoris Brezillon /* 4783d4af7c1SBoris Brezillon * READ0 is sometimes used to exit GET STATUS mode. When this 4793d4af7c1SBoris Brezillon * is the case no address cycles are requested, and we can use 4803d4af7c1SBoris Brezillon * this information to detect that READSTART should not be 4813d4af7c1SBoris Brezillon * issued. 4823d4af7c1SBoris Brezillon */ 4833d4af7c1SBoris Brezillon if (column == -1 && page_addr == -1) 4843d4af7c1SBoris Brezillon return; 4853d4af7c1SBoris Brezillon 4863d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, NAND_CMD_READSTART, 4873d4af7c1SBoris Brezillon NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); 4883d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE, 4893d4af7c1SBoris Brezillon NAND_NCE | NAND_CTRL_CHANGE); 490025a06c1SMiquel Raynal fallthrough; /* This applies to read commands */ 4913d4af7c1SBoris Brezillon default: 4923d4af7c1SBoris Brezillon /* 4933d4af7c1SBoris Brezillon * If we don't have access to the busy pin, we apply the given 4943d4af7c1SBoris Brezillon * command delay. 4953d4af7c1SBoris Brezillon */ 4963d4af7c1SBoris Brezillon if (!chip->legacy.dev_ready) { 4973d4af7c1SBoris Brezillon udelay(chip->legacy.chip_delay); 4983d4af7c1SBoris Brezillon return; 4993d4af7c1SBoris Brezillon } 5003d4af7c1SBoris Brezillon } 5013d4af7c1SBoris Brezillon 5023d4af7c1SBoris Brezillon /* 5033d4af7c1SBoris Brezillon * Apply this short delay always to ensure that we do wait tWB in 5043d4af7c1SBoris Brezillon * any case on any machine. 5053d4af7c1SBoris Brezillon */ 5063d4af7c1SBoris Brezillon ndelay(100); 5073d4af7c1SBoris Brezillon 5083d4af7c1SBoris Brezillon nand_wait_ready(chip); 5093d4af7c1SBoris Brezillon } 5103d4af7c1SBoris Brezillon 5113d4af7c1SBoris Brezillon /** 5123d4af7c1SBoris Brezillon * nand_get_set_features_notsupp - set/get features stub returning -ENOTSUPP 5133d4af7c1SBoris Brezillon * @chip: nand chip info structure 5143d4af7c1SBoris Brezillon * @addr: feature address. 5153d4af7c1SBoris Brezillon * @subfeature_param: the subfeature parameters, a four bytes array. 5163d4af7c1SBoris Brezillon * 5173d4af7c1SBoris Brezillon * Should be used by NAND controller drivers that do not support the SET/GET 5183d4af7c1SBoris Brezillon * FEATURES operations. 5193d4af7c1SBoris Brezillon */ 5203d4af7c1SBoris Brezillon int nand_get_set_features_notsupp(struct nand_chip *chip, int addr, 5213d4af7c1SBoris Brezillon u8 *subfeature_param) 5223d4af7c1SBoris Brezillon { 5233d4af7c1SBoris Brezillon return -ENOTSUPP; 5243d4af7c1SBoris Brezillon } 5253d4af7c1SBoris Brezillon EXPORT_SYMBOL(nand_get_set_features_notsupp); 5263d4af7c1SBoris Brezillon 5273d4af7c1SBoris Brezillon /** 5283d4af7c1SBoris Brezillon * nand_wait - [DEFAULT] wait until the command is done 5293d4af7c1SBoris Brezillon * @chip: NAND chip structure 5303d4af7c1SBoris Brezillon * 5313d4af7c1SBoris Brezillon * Wait for command done. This applies to erase and program only. 5323d4af7c1SBoris Brezillon */ 5333d4af7c1SBoris Brezillon static int nand_wait(struct nand_chip *chip) 5343d4af7c1SBoris Brezillon { 535*875330f8SThomas Gleixner struct mtd_info *mtd = nand_to_mtd(chip); 5363d4af7c1SBoris Brezillon unsigned long timeo = 400; 5373d4af7c1SBoris Brezillon u8 status; 5383d4af7c1SBoris Brezillon int ret; 5393d4af7c1SBoris Brezillon 5403d4af7c1SBoris Brezillon /* 5413d4af7c1SBoris Brezillon * Apply this short delay always to ensure that we do wait tWB in any 5423d4af7c1SBoris Brezillon * case on any machine. 5433d4af7c1SBoris Brezillon */ 5443d4af7c1SBoris Brezillon ndelay(100); 5453d4af7c1SBoris Brezillon 5463d4af7c1SBoris Brezillon ret = nand_status_op(chip, NULL); 5473d4af7c1SBoris Brezillon if (ret) 5483d4af7c1SBoris Brezillon return ret; 5493d4af7c1SBoris Brezillon 550*875330f8SThomas Gleixner if (mtd->oops_panic_write) { 5513d4af7c1SBoris Brezillon panic_nand_wait(chip, timeo); 552*875330f8SThomas Gleixner } else { 5533d4af7c1SBoris Brezillon timeo = jiffies + msecs_to_jiffies(timeo); 5543d4af7c1SBoris Brezillon do { 5553d4af7c1SBoris Brezillon if (chip->legacy.dev_ready) { 5563d4af7c1SBoris Brezillon if (chip->legacy.dev_ready(chip)) 5573d4af7c1SBoris Brezillon break; 5583d4af7c1SBoris Brezillon } else { 5593d4af7c1SBoris Brezillon ret = nand_read_data_op(chip, &status, 560b451f5beSMiquel Raynal sizeof(status), true, 561b451f5beSMiquel Raynal false); 5623d4af7c1SBoris Brezillon if (ret) 5633d4af7c1SBoris Brezillon return ret; 5643d4af7c1SBoris Brezillon 5653d4af7c1SBoris Brezillon if (status & NAND_STATUS_READY) 5663d4af7c1SBoris Brezillon break; 5673d4af7c1SBoris Brezillon } 5683d4af7c1SBoris Brezillon cond_resched(); 5693d4af7c1SBoris Brezillon } while (time_before(jiffies, timeo)); 5703d4af7c1SBoris Brezillon } 5713d4af7c1SBoris Brezillon 572b451f5beSMiquel Raynal ret = nand_read_data_op(chip, &status, sizeof(status), true, false); 5733d4af7c1SBoris Brezillon if (ret) 5743d4af7c1SBoris Brezillon return ret; 5753d4af7c1SBoris Brezillon 5763d4af7c1SBoris Brezillon /* This can happen if in case of timeout or buggy dev_ready */ 5773d4af7c1SBoris Brezillon WARN_ON(!(status & NAND_STATUS_READY)); 5783d4af7c1SBoris Brezillon return status; 5793d4af7c1SBoris Brezillon } 5803d4af7c1SBoris Brezillon 5813d4af7c1SBoris Brezillon void nand_legacy_set_defaults(struct nand_chip *chip) 5823d4af7c1SBoris Brezillon { 5833d4af7c1SBoris Brezillon unsigned int busw = chip->options & NAND_BUSWIDTH_16; 5843d4af7c1SBoris Brezillon 585f2abfeb2SBoris Brezillon if (nand_has_exec_op(chip)) 5863d4af7c1SBoris Brezillon return; 5873d4af7c1SBoris Brezillon 5883d4af7c1SBoris Brezillon /* check for proper chip_delay setup, set 20us if not */ 5893d4af7c1SBoris Brezillon if (!chip->legacy.chip_delay) 5903d4af7c1SBoris Brezillon chip->legacy.chip_delay = 20; 5913d4af7c1SBoris Brezillon 5923d4af7c1SBoris Brezillon /* check, if a user supplied command function given */ 593996852a9SBoris Brezillon if (!chip->legacy.cmdfunc) 5943d4af7c1SBoris Brezillon chip->legacy.cmdfunc = nand_command; 5953d4af7c1SBoris Brezillon 5963d4af7c1SBoris Brezillon /* check, if a user supplied wait function given */ 5973d4af7c1SBoris Brezillon if (chip->legacy.waitfunc == NULL) 5983d4af7c1SBoris Brezillon chip->legacy.waitfunc = nand_wait; 5993d4af7c1SBoris Brezillon 6007d6c37e9SBoris Brezillon if (!chip->legacy.select_chip) 6017d6c37e9SBoris Brezillon chip->legacy.select_chip = nand_select_chip; 6023d4af7c1SBoris Brezillon 6033d4af7c1SBoris Brezillon /* If called twice, pointers that depend on busw may need to be reset */ 6043d4af7c1SBoris Brezillon if (!chip->legacy.read_byte || chip->legacy.read_byte == nand_read_byte) 6053d4af7c1SBoris Brezillon chip->legacy.read_byte = busw ? nand_read_byte16 : nand_read_byte; 6063d4af7c1SBoris Brezillon if (!chip->legacy.write_buf || chip->legacy.write_buf == nand_write_buf) 6073d4af7c1SBoris Brezillon chip->legacy.write_buf = busw ? nand_write_buf16 : nand_write_buf; 6083d4af7c1SBoris Brezillon if (!chip->legacy.write_byte || chip->legacy.write_byte == nand_write_byte) 6093d4af7c1SBoris Brezillon chip->legacy.write_byte = busw ? nand_write_byte16 : nand_write_byte; 6103d4af7c1SBoris Brezillon if (!chip->legacy.read_buf || chip->legacy.read_buf == nand_read_buf) 6113d4af7c1SBoris Brezillon chip->legacy.read_buf = busw ? nand_read_buf16 : nand_read_buf; 6123d4af7c1SBoris Brezillon } 6133d4af7c1SBoris Brezillon 6143d4af7c1SBoris Brezillon void nand_legacy_adjust_cmdfunc(struct nand_chip *chip) 6153d4af7c1SBoris Brezillon { 6163d4af7c1SBoris Brezillon struct mtd_info *mtd = nand_to_mtd(chip); 6173d4af7c1SBoris Brezillon 6183d4af7c1SBoris Brezillon /* Do not replace user supplied command function! */ 6193d4af7c1SBoris Brezillon if (mtd->writesize > 512 && chip->legacy.cmdfunc == nand_command) 6203d4af7c1SBoris Brezillon chip->legacy.cmdfunc = nand_command_lp; 6213d4af7c1SBoris Brezillon } 6223d4af7c1SBoris Brezillon 6233d4af7c1SBoris Brezillon int nand_legacy_check_hooks(struct nand_chip *chip) 6243d4af7c1SBoris Brezillon { 6253d4af7c1SBoris Brezillon /* 6263d4af7c1SBoris Brezillon * ->legacy.cmdfunc() is legacy and will only be used if ->exec_op() is 6273d4af7c1SBoris Brezillon * not populated. 6283d4af7c1SBoris Brezillon */ 629f2abfeb2SBoris Brezillon if (nand_has_exec_op(chip)) 6303d4af7c1SBoris Brezillon return 0; 6313d4af7c1SBoris Brezillon 6323d4af7c1SBoris Brezillon /* 6333d4af7c1SBoris Brezillon * Default functions assigned for ->legacy.cmdfunc() and 6347d6c37e9SBoris Brezillon * ->legacy.select_chip() both expect ->legacy.cmd_ctrl() to be 6357d6c37e9SBoris Brezillon * populated. 6363d4af7c1SBoris Brezillon */ 6377d6c37e9SBoris Brezillon if ((!chip->legacy.cmdfunc || !chip->legacy.select_chip) && 6383d4af7c1SBoris Brezillon !chip->legacy.cmd_ctrl) { 6393d4af7c1SBoris Brezillon pr_err("->legacy.cmd_ctrl() should be provided\n"); 6403d4af7c1SBoris Brezillon return -EINVAL; 6413d4af7c1SBoris Brezillon } 6423d4af7c1SBoris Brezillon 6433d4af7c1SBoris Brezillon return 0; 6443d4af7c1SBoris Brezillon } 645