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 { 1953d4af7c1SBoris Brezillon unsigned long timeo = 400; 1963d4af7c1SBoris Brezillon 1973d4af7c1SBoris Brezillon if (in_interrupt() || oops_in_progress) 1980813621bSBoris Brezillon return panic_nand_wait_ready(chip, timeo); 1993d4af7c1SBoris Brezillon 2003d4af7c1SBoris Brezillon /* Wait until command is processed or timeout occurs */ 2013d4af7c1SBoris Brezillon timeo = jiffies + msecs_to_jiffies(timeo); 2023d4af7c1SBoris Brezillon do { 2033d4af7c1SBoris Brezillon if (chip->legacy.dev_ready(chip)) 2043d4af7c1SBoris Brezillon return; 2053d4af7c1SBoris Brezillon cond_resched(); 2063d4af7c1SBoris Brezillon } while (time_before(jiffies, timeo)); 2073d4af7c1SBoris Brezillon 2083d4af7c1SBoris Brezillon if (!chip->legacy.dev_ready(chip)) 2093d4af7c1SBoris Brezillon pr_warn_ratelimited("timeout while waiting for chip to become ready\n"); 2103d4af7c1SBoris Brezillon } 2113d4af7c1SBoris Brezillon EXPORT_SYMBOL_GPL(nand_wait_ready); 2123d4af7c1SBoris Brezillon 2133d4af7c1SBoris Brezillon /** 2143d4af7c1SBoris Brezillon * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands. 2150813621bSBoris Brezillon * @chip: NAND chip object 2163d4af7c1SBoris Brezillon * @timeo: Timeout in ms 2173d4af7c1SBoris Brezillon * 2183d4af7c1SBoris Brezillon * Wait for status ready (i.e. command done) or timeout. 2193d4af7c1SBoris Brezillon */ 2200813621bSBoris Brezillon static void nand_wait_status_ready(struct nand_chip *chip, unsigned long timeo) 2213d4af7c1SBoris Brezillon { 2223d4af7c1SBoris Brezillon int ret; 2233d4af7c1SBoris Brezillon 2243d4af7c1SBoris Brezillon timeo = jiffies + msecs_to_jiffies(timeo); 2253d4af7c1SBoris Brezillon do { 2263d4af7c1SBoris Brezillon u8 status; 2273d4af7c1SBoris Brezillon 2283d4af7c1SBoris Brezillon ret = nand_read_data_op(chip, &status, sizeof(status), true); 2293d4af7c1SBoris Brezillon if (ret) 2303d4af7c1SBoris Brezillon return; 2313d4af7c1SBoris Brezillon 2323d4af7c1SBoris Brezillon if (status & NAND_STATUS_READY) 2333d4af7c1SBoris Brezillon break; 2343d4af7c1SBoris Brezillon touch_softlockup_watchdog(); 2353d4af7c1SBoris Brezillon } while (time_before(jiffies, timeo)); 2363d4af7c1SBoris Brezillon }; 2373d4af7c1SBoris Brezillon 2383d4af7c1SBoris Brezillon /** 2393d4af7c1SBoris Brezillon * nand_command - [DEFAULT] Send command to NAND device 2403d4af7c1SBoris Brezillon * @chip: NAND chip object 2413d4af7c1SBoris Brezillon * @command: the command to be sent 2423d4af7c1SBoris Brezillon * @column: the column address for this command, -1 if none 2433d4af7c1SBoris Brezillon * @page_addr: the page address for this command, -1 if none 2443d4af7c1SBoris Brezillon * 2453d4af7c1SBoris Brezillon * Send command to NAND device. This function is used for small page devices 2463d4af7c1SBoris Brezillon * (512 Bytes per page). 2473d4af7c1SBoris Brezillon */ 2483d4af7c1SBoris Brezillon static void nand_command(struct nand_chip *chip, unsigned int command, 2493d4af7c1SBoris Brezillon int column, int page_addr) 2503d4af7c1SBoris Brezillon { 2513d4af7c1SBoris Brezillon struct mtd_info *mtd = nand_to_mtd(chip); 2523d4af7c1SBoris Brezillon int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE; 2533d4af7c1SBoris Brezillon 2543d4af7c1SBoris Brezillon /* Write out the command to the device */ 2553d4af7c1SBoris Brezillon if (command == NAND_CMD_SEQIN) { 2563d4af7c1SBoris Brezillon int readcmd; 2573d4af7c1SBoris Brezillon 2583d4af7c1SBoris Brezillon if (column >= mtd->writesize) { 2593d4af7c1SBoris Brezillon /* OOB area */ 2603d4af7c1SBoris Brezillon column -= mtd->writesize; 2613d4af7c1SBoris Brezillon readcmd = NAND_CMD_READOOB; 2623d4af7c1SBoris Brezillon } else if (column < 256) { 2633d4af7c1SBoris Brezillon /* First 256 bytes --> READ0 */ 2643d4af7c1SBoris Brezillon readcmd = NAND_CMD_READ0; 2653d4af7c1SBoris Brezillon } else { 2663d4af7c1SBoris Brezillon column -= 256; 2673d4af7c1SBoris Brezillon readcmd = NAND_CMD_READ1; 2683d4af7c1SBoris Brezillon } 2693d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, readcmd, ctrl); 2703d4af7c1SBoris Brezillon ctrl &= ~NAND_CTRL_CHANGE; 2713d4af7c1SBoris Brezillon } 2723d4af7c1SBoris Brezillon if (command != NAND_CMD_NONE) 2733d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, command, ctrl); 2743d4af7c1SBoris Brezillon 2753d4af7c1SBoris Brezillon /* Address cycle, when necessary */ 2763d4af7c1SBoris Brezillon ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE; 2773d4af7c1SBoris Brezillon /* Serially input address */ 2783d4af7c1SBoris Brezillon if (column != -1) { 2793d4af7c1SBoris Brezillon /* Adjust columns for 16 bit buswidth */ 2803d4af7c1SBoris Brezillon if (chip->options & NAND_BUSWIDTH_16 && 2813d4af7c1SBoris Brezillon !nand_opcode_8bits(command)) 2823d4af7c1SBoris Brezillon column >>= 1; 2833d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, column, ctrl); 2843d4af7c1SBoris Brezillon ctrl &= ~NAND_CTRL_CHANGE; 2853d4af7c1SBoris Brezillon } 2863d4af7c1SBoris Brezillon if (page_addr != -1) { 2873d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, page_addr, ctrl); 2883d4af7c1SBoris Brezillon ctrl &= ~NAND_CTRL_CHANGE; 2893d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, page_addr >> 8, ctrl); 2903d4af7c1SBoris Brezillon if (chip->options & NAND_ROW_ADDR_3) 2913d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, page_addr >> 16, ctrl); 2923d4af7c1SBoris Brezillon } 2933d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE, 2943d4af7c1SBoris Brezillon NAND_NCE | NAND_CTRL_CHANGE); 2953d4af7c1SBoris Brezillon 2963d4af7c1SBoris Brezillon /* 2973d4af7c1SBoris Brezillon * Program and erase have their own busy handlers status and sequential 2983d4af7c1SBoris Brezillon * in needs no delay 2993d4af7c1SBoris Brezillon */ 3003d4af7c1SBoris Brezillon switch (command) { 3013d4af7c1SBoris Brezillon 3023d4af7c1SBoris Brezillon case NAND_CMD_NONE: 3033d4af7c1SBoris Brezillon case NAND_CMD_PAGEPROG: 3043d4af7c1SBoris Brezillon case NAND_CMD_ERASE1: 3053d4af7c1SBoris Brezillon case NAND_CMD_ERASE2: 3063d4af7c1SBoris Brezillon case NAND_CMD_SEQIN: 3073d4af7c1SBoris Brezillon case NAND_CMD_STATUS: 3083d4af7c1SBoris Brezillon case NAND_CMD_READID: 3093d4af7c1SBoris Brezillon case NAND_CMD_SET_FEATURES: 3103d4af7c1SBoris Brezillon return; 3113d4af7c1SBoris Brezillon 3123d4af7c1SBoris Brezillon case NAND_CMD_RESET: 3133d4af7c1SBoris Brezillon if (chip->legacy.dev_ready) 3143d4af7c1SBoris Brezillon break; 3153d4af7c1SBoris Brezillon udelay(chip->legacy.chip_delay); 3163d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, NAND_CMD_STATUS, 3173d4af7c1SBoris Brezillon NAND_CTRL_CLE | NAND_CTRL_CHANGE); 3183d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE, 3193d4af7c1SBoris Brezillon NAND_NCE | NAND_CTRL_CHANGE); 3203d4af7c1SBoris Brezillon /* EZ-NAND can take upto 250ms as per ONFi v4.0 */ 3210813621bSBoris Brezillon nand_wait_status_ready(chip, 250); 3223d4af7c1SBoris Brezillon return; 3233d4af7c1SBoris Brezillon 3243d4af7c1SBoris Brezillon /* This applies to read commands */ 3253d4af7c1SBoris Brezillon case NAND_CMD_READ0: 3263d4af7c1SBoris Brezillon /* 3273d4af7c1SBoris Brezillon * READ0 is sometimes used to exit GET STATUS mode. When this 3283d4af7c1SBoris Brezillon * is the case no address cycles are requested, and we can use 3293d4af7c1SBoris Brezillon * this information to detect that we should not wait for the 3303d4af7c1SBoris Brezillon * device to be ready. 3313d4af7c1SBoris Brezillon */ 3323d4af7c1SBoris Brezillon if (column == -1 && page_addr == -1) 3333d4af7c1SBoris Brezillon return; 3343d4af7c1SBoris Brezillon 3353d4af7c1SBoris Brezillon default: 3363d4af7c1SBoris Brezillon /* 3373d4af7c1SBoris Brezillon * If we don't have access to the busy pin, we apply the given 3383d4af7c1SBoris Brezillon * command delay 3393d4af7c1SBoris Brezillon */ 3403d4af7c1SBoris Brezillon if (!chip->legacy.dev_ready) { 3413d4af7c1SBoris Brezillon udelay(chip->legacy.chip_delay); 3423d4af7c1SBoris Brezillon return; 3433d4af7c1SBoris Brezillon } 3443d4af7c1SBoris Brezillon } 3453d4af7c1SBoris Brezillon /* 3463d4af7c1SBoris Brezillon * Apply this short delay always to ensure that we do wait tWB in 3473d4af7c1SBoris Brezillon * any case on any machine. 3483d4af7c1SBoris Brezillon */ 3493d4af7c1SBoris Brezillon ndelay(100); 3503d4af7c1SBoris Brezillon 3513d4af7c1SBoris Brezillon nand_wait_ready(chip); 3523d4af7c1SBoris Brezillon } 3533d4af7c1SBoris Brezillon 3543d4af7c1SBoris Brezillon static void nand_ccs_delay(struct nand_chip *chip) 3553d4af7c1SBoris Brezillon { 3563d4af7c1SBoris Brezillon /* 3573d4af7c1SBoris Brezillon * The controller already takes care of waiting for tCCS when the RNDIN 3583d4af7c1SBoris Brezillon * or RNDOUT command is sent, return directly. 3593d4af7c1SBoris Brezillon */ 3603d4af7c1SBoris Brezillon if (!(chip->options & NAND_WAIT_TCCS)) 3613d4af7c1SBoris Brezillon return; 3623d4af7c1SBoris Brezillon 3633d4af7c1SBoris Brezillon /* 3643d4af7c1SBoris Brezillon * Wait tCCS_min if it is correctly defined, otherwise wait 500ns 3653d4af7c1SBoris Brezillon * (which should be safe for all NANDs). 3663d4af7c1SBoris Brezillon */ 3673d4af7c1SBoris Brezillon if (chip->setup_data_interface) 3683d4af7c1SBoris Brezillon ndelay(chip->data_interface.timings.sdr.tCCS_min / 1000); 3693d4af7c1SBoris Brezillon else 3703d4af7c1SBoris Brezillon ndelay(500); 3713d4af7c1SBoris Brezillon } 3723d4af7c1SBoris Brezillon 3733d4af7c1SBoris Brezillon /** 3743d4af7c1SBoris Brezillon * nand_command_lp - [DEFAULT] Send command to NAND large page device 3753d4af7c1SBoris Brezillon * @chip: NAND chip object 3763d4af7c1SBoris Brezillon * @command: the command to be sent 3773d4af7c1SBoris Brezillon * @column: the column address for this command, -1 if none 3783d4af7c1SBoris Brezillon * @page_addr: the page address for this command, -1 if none 3793d4af7c1SBoris Brezillon * 3803d4af7c1SBoris Brezillon * Send command to NAND device. This is the version for the new large page 3813d4af7c1SBoris Brezillon * devices. We don't have the separate regions as we have in the small page 3823d4af7c1SBoris Brezillon * devices. We must emulate NAND_CMD_READOOB to keep the code compatible. 3833d4af7c1SBoris Brezillon */ 3843d4af7c1SBoris Brezillon static void nand_command_lp(struct nand_chip *chip, unsigned int command, 3853d4af7c1SBoris Brezillon int column, int page_addr) 3863d4af7c1SBoris Brezillon { 3873d4af7c1SBoris Brezillon struct mtd_info *mtd = nand_to_mtd(chip); 3883d4af7c1SBoris Brezillon 3893d4af7c1SBoris Brezillon /* Emulate NAND_CMD_READOOB */ 3903d4af7c1SBoris Brezillon if (command == NAND_CMD_READOOB) { 3913d4af7c1SBoris Brezillon column += mtd->writesize; 3923d4af7c1SBoris Brezillon command = NAND_CMD_READ0; 3933d4af7c1SBoris Brezillon } 3943d4af7c1SBoris Brezillon 3953d4af7c1SBoris Brezillon /* Command latch cycle */ 3963d4af7c1SBoris Brezillon if (command != NAND_CMD_NONE) 3973d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, command, 3983d4af7c1SBoris Brezillon NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); 3993d4af7c1SBoris Brezillon 4003d4af7c1SBoris Brezillon if (column != -1 || page_addr != -1) { 4013d4af7c1SBoris Brezillon int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE; 4023d4af7c1SBoris Brezillon 4033d4af7c1SBoris Brezillon /* Serially input address */ 4043d4af7c1SBoris Brezillon if (column != -1) { 4053d4af7c1SBoris Brezillon /* Adjust columns for 16 bit buswidth */ 4063d4af7c1SBoris Brezillon if (chip->options & NAND_BUSWIDTH_16 && 4073d4af7c1SBoris Brezillon !nand_opcode_8bits(command)) 4083d4af7c1SBoris Brezillon column >>= 1; 4093d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, column, ctrl); 4103d4af7c1SBoris Brezillon ctrl &= ~NAND_CTRL_CHANGE; 4113d4af7c1SBoris Brezillon 4123d4af7c1SBoris Brezillon /* Only output a single addr cycle for 8bits opcodes. */ 4133d4af7c1SBoris Brezillon if (!nand_opcode_8bits(command)) 4143d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, column >> 8, ctrl); 4153d4af7c1SBoris Brezillon } 4163d4af7c1SBoris Brezillon if (page_addr != -1) { 4173d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, page_addr, ctrl); 4183d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, page_addr >> 8, 4193d4af7c1SBoris Brezillon NAND_NCE | NAND_ALE); 4203d4af7c1SBoris Brezillon if (chip->options & NAND_ROW_ADDR_3) 4213d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, page_addr >> 16, 4223d4af7c1SBoris Brezillon NAND_NCE | NAND_ALE); 4233d4af7c1SBoris Brezillon } 4243d4af7c1SBoris Brezillon } 4253d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE, 4263d4af7c1SBoris Brezillon NAND_NCE | NAND_CTRL_CHANGE); 4273d4af7c1SBoris Brezillon 4283d4af7c1SBoris Brezillon /* 4293d4af7c1SBoris Brezillon * Program and erase have their own busy handlers status, sequential 4303d4af7c1SBoris Brezillon * in and status need no delay. 4313d4af7c1SBoris Brezillon */ 4323d4af7c1SBoris Brezillon switch (command) { 4333d4af7c1SBoris Brezillon 4343d4af7c1SBoris Brezillon case NAND_CMD_NONE: 4353d4af7c1SBoris Brezillon case NAND_CMD_CACHEDPROG: 4363d4af7c1SBoris Brezillon case NAND_CMD_PAGEPROG: 4373d4af7c1SBoris Brezillon case NAND_CMD_ERASE1: 4383d4af7c1SBoris Brezillon case NAND_CMD_ERASE2: 4393d4af7c1SBoris Brezillon case NAND_CMD_SEQIN: 4403d4af7c1SBoris Brezillon case NAND_CMD_STATUS: 4413d4af7c1SBoris Brezillon case NAND_CMD_READID: 4423d4af7c1SBoris Brezillon case NAND_CMD_SET_FEATURES: 4433d4af7c1SBoris Brezillon return; 4443d4af7c1SBoris Brezillon 4453d4af7c1SBoris Brezillon case NAND_CMD_RNDIN: 4463d4af7c1SBoris Brezillon nand_ccs_delay(chip); 4473d4af7c1SBoris Brezillon return; 4483d4af7c1SBoris Brezillon 4493d4af7c1SBoris Brezillon case NAND_CMD_RESET: 4503d4af7c1SBoris Brezillon if (chip->legacy.dev_ready) 4513d4af7c1SBoris Brezillon break; 4523d4af7c1SBoris Brezillon udelay(chip->legacy.chip_delay); 4533d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, NAND_CMD_STATUS, 4543d4af7c1SBoris Brezillon NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); 4553d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE, 4563d4af7c1SBoris Brezillon NAND_NCE | NAND_CTRL_CHANGE); 4573d4af7c1SBoris Brezillon /* EZ-NAND can take upto 250ms as per ONFi v4.0 */ 4580813621bSBoris Brezillon nand_wait_status_ready(chip, 250); 4593d4af7c1SBoris Brezillon return; 4603d4af7c1SBoris Brezillon 4613d4af7c1SBoris Brezillon case NAND_CMD_RNDOUT: 4623d4af7c1SBoris Brezillon /* No ready / busy check necessary */ 4633d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, NAND_CMD_RNDOUTSTART, 4643d4af7c1SBoris Brezillon NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); 4653d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE, 4663d4af7c1SBoris Brezillon NAND_NCE | NAND_CTRL_CHANGE); 4673d4af7c1SBoris Brezillon 4683d4af7c1SBoris Brezillon nand_ccs_delay(chip); 4693d4af7c1SBoris Brezillon return; 4703d4af7c1SBoris Brezillon 4713d4af7c1SBoris Brezillon case NAND_CMD_READ0: 4723d4af7c1SBoris Brezillon /* 4733d4af7c1SBoris Brezillon * READ0 is sometimes used to exit GET STATUS mode. When this 4743d4af7c1SBoris Brezillon * is the case no address cycles are requested, and we can use 4753d4af7c1SBoris Brezillon * this information to detect that READSTART should not be 4763d4af7c1SBoris Brezillon * issued. 4773d4af7c1SBoris Brezillon */ 4783d4af7c1SBoris Brezillon if (column == -1 && page_addr == -1) 4793d4af7c1SBoris Brezillon return; 4803d4af7c1SBoris Brezillon 4813d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, NAND_CMD_READSTART, 4823d4af7c1SBoris Brezillon NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); 4833d4af7c1SBoris Brezillon chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE, 4843d4af7c1SBoris Brezillon NAND_NCE | NAND_CTRL_CHANGE); 4853d4af7c1SBoris Brezillon 4863d4af7c1SBoris Brezillon /* This applies to read commands */ 4873d4af7c1SBoris Brezillon default: 4883d4af7c1SBoris Brezillon /* 4893d4af7c1SBoris Brezillon * If we don't have access to the busy pin, we apply the given 4903d4af7c1SBoris Brezillon * command delay. 4913d4af7c1SBoris Brezillon */ 4923d4af7c1SBoris Brezillon if (!chip->legacy.dev_ready) { 4933d4af7c1SBoris Brezillon udelay(chip->legacy.chip_delay); 4943d4af7c1SBoris Brezillon return; 4953d4af7c1SBoris Brezillon } 4963d4af7c1SBoris Brezillon } 4973d4af7c1SBoris Brezillon 4983d4af7c1SBoris Brezillon /* 4993d4af7c1SBoris Brezillon * Apply this short delay always to ensure that we do wait tWB in 5003d4af7c1SBoris Brezillon * any case on any machine. 5013d4af7c1SBoris Brezillon */ 5023d4af7c1SBoris Brezillon ndelay(100); 5033d4af7c1SBoris Brezillon 5043d4af7c1SBoris Brezillon nand_wait_ready(chip); 5053d4af7c1SBoris Brezillon } 5063d4af7c1SBoris Brezillon 5073d4af7c1SBoris Brezillon /** 5083d4af7c1SBoris Brezillon * nand_get_set_features_notsupp - set/get features stub returning -ENOTSUPP 5093d4af7c1SBoris Brezillon * @chip: nand chip info structure 5103d4af7c1SBoris Brezillon * @addr: feature address. 5113d4af7c1SBoris Brezillon * @subfeature_param: the subfeature parameters, a four bytes array. 5123d4af7c1SBoris Brezillon * 5133d4af7c1SBoris Brezillon * Should be used by NAND controller drivers that do not support the SET/GET 5143d4af7c1SBoris Brezillon * FEATURES operations. 5153d4af7c1SBoris Brezillon */ 5163d4af7c1SBoris Brezillon int nand_get_set_features_notsupp(struct nand_chip *chip, int addr, 5173d4af7c1SBoris Brezillon u8 *subfeature_param) 5183d4af7c1SBoris Brezillon { 5193d4af7c1SBoris Brezillon return -ENOTSUPP; 5203d4af7c1SBoris Brezillon } 5213d4af7c1SBoris Brezillon EXPORT_SYMBOL(nand_get_set_features_notsupp); 5223d4af7c1SBoris Brezillon 5233d4af7c1SBoris Brezillon /** 5243d4af7c1SBoris Brezillon * nand_wait - [DEFAULT] wait until the command is done 5253d4af7c1SBoris Brezillon * @chip: NAND chip structure 5263d4af7c1SBoris Brezillon * 5273d4af7c1SBoris Brezillon * Wait for command done. This applies to erase and program only. 5283d4af7c1SBoris Brezillon */ 5293d4af7c1SBoris Brezillon static int nand_wait(struct nand_chip *chip) 5303d4af7c1SBoris Brezillon { 5313d4af7c1SBoris Brezillon 5323d4af7c1SBoris Brezillon unsigned long timeo = 400; 5333d4af7c1SBoris Brezillon u8 status; 5343d4af7c1SBoris Brezillon int ret; 5353d4af7c1SBoris Brezillon 5363d4af7c1SBoris Brezillon /* 5373d4af7c1SBoris Brezillon * Apply this short delay always to ensure that we do wait tWB in any 5383d4af7c1SBoris Brezillon * case on any machine. 5393d4af7c1SBoris Brezillon */ 5403d4af7c1SBoris Brezillon ndelay(100); 5413d4af7c1SBoris Brezillon 5423d4af7c1SBoris Brezillon ret = nand_status_op(chip, NULL); 5433d4af7c1SBoris Brezillon if (ret) 5443d4af7c1SBoris Brezillon return ret; 5453d4af7c1SBoris Brezillon 5463d4af7c1SBoris Brezillon if (in_interrupt() || oops_in_progress) 5473d4af7c1SBoris Brezillon panic_nand_wait(chip, timeo); 5483d4af7c1SBoris Brezillon else { 5493d4af7c1SBoris Brezillon timeo = jiffies + msecs_to_jiffies(timeo); 5503d4af7c1SBoris Brezillon do { 5513d4af7c1SBoris Brezillon if (chip->legacy.dev_ready) { 5523d4af7c1SBoris Brezillon if (chip->legacy.dev_ready(chip)) 5533d4af7c1SBoris Brezillon break; 5543d4af7c1SBoris Brezillon } else { 5553d4af7c1SBoris Brezillon ret = nand_read_data_op(chip, &status, 5563d4af7c1SBoris Brezillon sizeof(status), true); 5573d4af7c1SBoris Brezillon if (ret) 5583d4af7c1SBoris Brezillon return ret; 5593d4af7c1SBoris Brezillon 5603d4af7c1SBoris Brezillon if (status & NAND_STATUS_READY) 5613d4af7c1SBoris Brezillon break; 5623d4af7c1SBoris Brezillon } 5633d4af7c1SBoris Brezillon cond_resched(); 5643d4af7c1SBoris Brezillon } while (time_before(jiffies, timeo)); 5653d4af7c1SBoris Brezillon } 5663d4af7c1SBoris Brezillon 5673d4af7c1SBoris Brezillon ret = nand_read_data_op(chip, &status, sizeof(status), true); 5683d4af7c1SBoris Brezillon if (ret) 5693d4af7c1SBoris Brezillon return ret; 5703d4af7c1SBoris Brezillon 5713d4af7c1SBoris Brezillon /* This can happen if in case of timeout or buggy dev_ready */ 5723d4af7c1SBoris Brezillon WARN_ON(!(status & NAND_STATUS_READY)); 5733d4af7c1SBoris Brezillon return status; 5743d4af7c1SBoris Brezillon } 5753d4af7c1SBoris Brezillon 5763d4af7c1SBoris Brezillon void nand_legacy_set_defaults(struct nand_chip *chip) 5773d4af7c1SBoris Brezillon { 5783d4af7c1SBoris Brezillon unsigned int busw = chip->options & NAND_BUSWIDTH_16; 5793d4af7c1SBoris Brezillon 580*f2abfeb2SBoris Brezillon if (nand_has_exec_op(chip)) 5813d4af7c1SBoris Brezillon return; 5823d4af7c1SBoris Brezillon 5833d4af7c1SBoris Brezillon /* check for proper chip_delay setup, set 20us if not */ 5843d4af7c1SBoris Brezillon if (!chip->legacy.chip_delay) 5853d4af7c1SBoris Brezillon chip->legacy.chip_delay = 20; 5863d4af7c1SBoris Brezillon 5873d4af7c1SBoris Brezillon /* check, if a user supplied command function given */ 588996852a9SBoris Brezillon if (!chip->legacy.cmdfunc) 5893d4af7c1SBoris Brezillon chip->legacy.cmdfunc = nand_command; 5903d4af7c1SBoris Brezillon 5913d4af7c1SBoris Brezillon /* check, if a user supplied wait function given */ 5923d4af7c1SBoris Brezillon if (chip->legacy.waitfunc == NULL) 5933d4af7c1SBoris Brezillon chip->legacy.waitfunc = nand_wait; 5943d4af7c1SBoris Brezillon 5957d6c37e9SBoris Brezillon if (!chip->legacy.select_chip) 5967d6c37e9SBoris Brezillon chip->legacy.select_chip = nand_select_chip; 5973d4af7c1SBoris Brezillon 5983d4af7c1SBoris Brezillon /* If called twice, pointers that depend on busw may need to be reset */ 5993d4af7c1SBoris Brezillon if (!chip->legacy.read_byte || chip->legacy.read_byte == nand_read_byte) 6003d4af7c1SBoris Brezillon chip->legacy.read_byte = busw ? nand_read_byte16 : nand_read_byte; 6013d4af7c1SBoris Brezillon if (!chip->legacy.write_buf || chip->legacy.write_buf == nand_write_buf) 6023d4af7c1SBoris Brezillon chip->legacy.write_buf = busw ? nand_write_buf16 : nand_write_buf; 6033d4af7c1SBoris Brezillon if (!chip->legacy.write_byte || chip->legacy.write_byte == nand_write_byte) 6043d4af7c1SBoris Brezillon chip->legacy.write_byte = busw ? nand_write_byte16 : nand_write_byte; 6053d4af7c1SBoris Brezillon if (!chip->legacy.read_buf || chip->legacy.read_buf == nand_read_buf) 6063d4af7c1SBoris Brezillon chip->legacy.read_buf = busw ? nand_read_buf16 : nand_read_buf; 6073d4af7c1SBoris Brezillon } 6083d4af7c1SBoris Brezillon 6093d4af7c1SBoris Brezillon void nand_legacy_adjust_cmdfunc(struct nand_chip *chip) 6103d4af7c1SBoris Brezillon { 6113d4af7c1SBoris Brezillon struct mtd_info *mtd = nand_to_mtd(chip); 6123d4af7c1SBoris Brezillon 6133d4af7c1SBoris Brezillon /* Do not replace user supplied command function! */ 6143d4af7c1SBoris Brezillon if (mtd->writesize > 512 && chip->legacy.cmdfunc == nand_command) 6153d4af7c1SBoris Brezillon chip->legacy.cmdfunc = nand_command_lp; 6163d4af7c1SBoris Brezillon } 6173d4af7c1SBoris Brezillon 6183d4af7c1SBoris Brezillon int nand_legacy_check_hooks(struct nand_chip *chip) 6193d4af7c1SBoris Brezillon { 6203d4af7c1SBoris Brezillon /* 6213d4af7c1SBoris Brezillon * ->legacy.cmdfunc() is legacy and will only be used if ->exec_op() is 6223d4af7c1SBoris Brezillon * not populated. 6233d4af7c1SBoris Brezillon */ 624*f2abfeb2SBoris Brezillon if (nand_has_exec_op(chip)) 6253d4af7c1SBoris Brezillon return 0; 6263d4af7c1SBoris Brezillon 6273d4af7c1SBoris Brezillon /* 6283d4af7c1SBoris Brezillon * Default functions assigned for ->legacy.cmdfunc() and 6297d6c37e9SBoris Brezillon * ->legacy.select_chip() both expect ->legacy.cmd_ctrl() to be 6307d6c37e9SBoris Brezillon * populated. 6313d4af7c1SBoris Brezillon */ 6327d6c37e9SBoris Brezillon if ((!chip->legacy.cmdfunc || !chip->legacy.select_chip) && 6333d4af7c1SBoris Brezillon !chip->legacy.cmd_ctrl) { 6343d4af7c1SBoris Brezillon pr_err("->legacy.cmd_ctrl() should be provided\n"); 6353d4af7c1SBoris Brezillon return -EINVAL; 6363d4af7c1SBoris Brezillon } 6373d4af7c1SBoris Brezillon 6383d4af7c1SBoris Brezillon return 0; 6393d4af7c1SBoris Brezillon } 640