1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
4 * 2002-2006 Thomas Gleixner (tglx@linutronix.de)
7 * David Woodhouse for adding multichip support
9 * Aleph One Ltd. and Toby Churchill Ltd. for supporting the
10 * rework for 2K page size chips
12 * This file contains all legacy helpers/code that should be removed
16 #include <linux/delay.h>
18 #include <linux/nmi.h>
20 #include "internals.h"
23 * nand_read_byte - [DEFAULT] read one byte from the chip
24 * @chip: NAND chip object
26 * Default read function for 8bit buswidth
28 static uint8_t nand_read_byte(struct nand_chip *chip)
30 return readb(chip->legacy.IO_ADDR_R);
34 * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip
35 * @chip: NAND chip object
37 * Default read function for 16bit buswidth with endianness conversion.
40 static uint8_t nand_read_byte16(struct nand_chip *chip)
42 return (uint8_t) cpu_to_le16(readw(chip->legacy.IO_ADDR_R));
46 * nand_select_chip - [DEFAULT] control CE line
47 * @chip: NAND chip object
48 * @chipnr: chipnumber to select, -1 for deselect
50 * Default select function for 1 chip devices.
52 static void nand_select_chip(struct nand_chip *chip, int chipnr)
56 chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
57 0 | NAND_CTRL_CHANGE);
68 * nand_write_byte - [DEFAULT] write single byte to chip
69 * @chip: NAND chip object
70 * @byte: value to write
72 * Default function to write a byte to I/O[7:0]
74 static void nand_write_byte(struct nand_chip *chip, uint8_t byte)
76 chip->legacy.write_buf(chip, &byte, 1);
80 * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16
81 * @chip: NAND chip object
82 * @byte: value to write
84 * Default function to write a byte to I/O[7:0] on a 16-bit wide chip.
86 static void nand_write_byte16(struct nand_chip *chip, uint8_t byte)
91 * It's not entirely clear what should happen to I/O[15:8] when writing
92 * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads:
94 * When the host supports a 16-bit bus width, only data is
95 * transferred at the 16-bit width. All address and command line
96 * transfers shall use only the lower 8-bits of the data bus. During
97 * command transfers, the host may place any value on the upper
98 * 8-bits of the data bus. During address transfers, the host shall
99 * set the upper 8-bits of the data bus to 00h.
101 * One user of the write_byte callback is nand_set_features. The
102 * four parameters are specified to be written to I/O[7:0], but this is
103 * neither an address nor a command transfer. Let's assume a 0 on the
104 * upper I/O lines is OK.
106 chip->legacy.write_buf(chip, (uint8_t *)&word, 2);
110 * nand_write_buf - [DEFAULT] write buffer to chip
111 * @chip: NAND chip object
113 * @len: number of bytes to write
115 * Default write function for 8bit buswidth.
117 static void nand_write_buf(struct nand_chip *chip, const uint8_t *buf, int len)
119 iowrite8_rep(chip->legacy.IO_ADDR_W, buf, len);
123 * nand_read_buf - [DEFAULT] read chip data into buffer
124 * @chip: NAND chip object
125 * @buf: buffer to store date
126 * @len: number of bytes to read
128 * Default read function for 8bit buswidth.
130 static void nand_read_buf(struct nand_chip *chip, uint8_t *buf, int len)
132 ioread8_rep(chip->legacy.IO_ADDR_R, buf, len);
136 * nand_write_buf16 - [DEFAULT] write buffer to chip
137 * @chip: NAND chip object
139 * @len: number of bytes to write
141 * Default write function for 16bit buswidth.
143 static void nand_write_buf16(struct nand_chip *chip, const uint8_t *buf,
146 u16 *p = (u16 *) buf;
148 iowrite16_rep(chip->legacy.IO_ADDR_W, p, len >> 1);
152 * nand_read_buf16 - [DEFAULT] read chip data into buffer
153 * @chip: NAND chip object
154 * @buf: buffer to store date
155 * @len: number of bytes to read
157 * Default read function for 16bit buswidth.
159 static void nand_read_buf16(struct nand_chip *chip, uint8_t *buf, int len)
161 u16 *p = (u16 *) buf;
163 ioread16_rep(chip->legacy.IO_ADDR_R, p, len >> 1);
167 * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
168 * @chip: NAND chip object
171 * Helper function for nand_wait_ready used when needing to wait in interrupt
174 static void panic_nand_wait_ready(struct nand_chip *chip, unsigned long timeo)
178 /* Wait for the device to get ready */
179 for (i = 0; i < timeo; i++) {
180 if (chip->legacy.dev_ready(chip))
182 touch_softlockup_watchdog();
188 * nand_wait_ready - [GENERIC] Wait for the ready pin after commands.
189 * @chip: NAND chip object
191 * Wait for the ready pin after a command, and warn if a timeout occurs.
193 void nand_wait_ready(struct nand_chip *chip)
195 unsigned long timeo = 400;
197 if (in_interrupt() || oops_in_progress)
198 return panic_nand_wait_ready(chip, timeo);
200 /* Wait until command is processed or timeout occurs */
201 timeo = jiffies + msecs_to_jiffies(timeo);
203 if (chip->legacy.dev_ready(chip))
206 } while (time_before(jiffies, timeo));
208 if (!chip->legacy.dev_ready(chip))
209 pr_warn_ratelimited("timeout while waiting for chip to become ready\n");
211 EXPORT_SYMBOL_GPL(nand_wait_ready);
214 * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands.
215 * @chip: NAND chip object
216 * @timeo: Timeout in ms
218 * Wait for status ready (i.e. command done) or timeout.
220 static void nand_wait_status_ready(struct nand_chip *chip, unsigned long timeo)
224 timeo = jiffies + msecs_to_jiffies(timeo);
228 ret = nand_read_data_op(chip, &status, sizeof(status), true,
233 if (status & NAND_STATUS_READY)
235 touch_softlockup_watchdog();
236 } while (time_before(jiffies, timeo));
240 * nand_command - [DEFAULT] Send command to NAND device
241 * @chip: NAND chip object
242 * @command: the command to be sent
243 * @column: the column address for this command, -1 if none
244 * @page_addr: the page address for this command, -1 if none
246 * Send command to NAND device. This function is used for small page devices
247 * (512 Bytes per page).
249 static void nand_command(struct nand_chip *chip, unsigned int command,
250 int column, int page_addr)
252 struct mtd_info *mtd = nand_to_mtd(chip);
253 int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE;
255 /* Write out the command to the device */
256 if (command == NAND_CMD_SEQIN) {
259 if (column >= mtd->writesize) {
261 column -= mtd->writesize;
262 readcmd = NAND_CMD_READOOB;
263 } else if (column < 256) {
264 /* First 256 bytes --> READ0 */
265 readcmd = NAND_CMD_READ0;
268 readcmd = NAND_CMD_READ1;
270 chip->legacy.cmd_ctrl(chip, readcmd, ctrl);
271 ctrl &= ~NAND_CTRL_CHANGE;
273 if (command != NAND_CMD_NONE)
274 chip->legacy.cmd_ctrl(chip, command, ctrl);
276 /* Address cycle, when necessary */
277 ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE;
278 /* Serially input address */
280 /* Adjust columns for 16 bit buswidth */
281 if (chip->options & NAND_BUSWIDTH_16 &&
282 !nand_opcode_8bits(command))
284 chip->legacy.cmd_ctrl(chip, column, ctrl);
285 ctrl &= ~NAND_CTRL_CHANGE;
287 if (page_addr != -1) {
288 chip->legacy.cmd_ctrl(chip, page_addr, ctrl);
289 ctrl &= ~NAND_CTRL_CHANGE;
290 chip->legacy.cmd_ctrl(chip, page_addr >> 8, ctrl);
291 if (chip->options & NAND_ROW_ADDR_3)
292 chip->legacy.cmd_ctrl(chip, page_addr >> 16, ctrl);
294 chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
295 NAND_NCE | NAND_CTRL_CHANGE);
298 * Program and erase have their own busy handlers status and sequential
304 case NAND_CMD_PAGEPROG:
305 case NAND_CMD_ERASE1:
306 case NAND_CMD_ERASE2:
308 case NAND_CMD_STATUS:
309 case NAND_CMD_READID:
310 case NAND_CMD_SET_FEATURES:
314 if (chip->legacy.dev_ready)
316 udelay(chip->legacy.chip_delay);
317 chip->legacy.cmd_ctrl(chip, NAND_CMD_STATUS,
318 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
319 chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
320 NAND_NCE | NAND_CTRL_CHANGE);
321 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
322 nand_wait_status_ready(chip, 250);
325 /* This applies to read commands */
328 * READ0 is sometimes used to exit GET STATUS mode. When this
329 * is the case no address cycles are requested, and we can use
330 * this information to detect that we should not wait for the
331 * device to be ready.
333 if (column == -1 && page_addr == -1)
338 * If we don't have access to the busy pin, we apply the given
341 if (!chip->legacy.dev_ready) {
342 udelay(chip->legacy.chip_delay);
347 * Apply this short delay always to ensure that we do wait tWB in
348 * any case on any machine.
352 nand_wait_ready(chip);
355 static void nand_ccs_delay(struct nand_chip *chip)
357 const struct nand_sdr_timings *sdr =
358 nand_get_sdr_timings(nand_get_interface_config(chip));
361 * The controller already takes care of waiting for tCCS when the RNDIN
362 * or RNDOUT command is sent, return directly.
364 if (!(chip->options & NAND_WAIT_TCCS))
368 * Wait tCCS_min if it is correctly defined, otherwise wait 500ns
369 * (which should be safe for all NANDs).
371 if (nand_controller_can_setup_interface(chip))
372 ndelay(sdr->tCCS_min / 1000);
378 * nand_command_lp - [DEFAULT] Send command to NAND large page device
379 * @chip: NAND chip object
380 * @command: the command to be sent
381 * @column: the column address for this command, -1 if none
382 * @page_addr: the page address for this command, -1 if none
384 * Send command to NAND device. This is the version for the new large page
385 * devices. We don't have the separate regions as we have in the small page
386 * devices. We must emulate NAND_CMD_READOOB to keep the code compatible.
388 static void nand_command_lp(struct nand_chip *chip, unsigned int command,
389 int column, int page_addr)
391 struct mtd_info *mtd = nand_to_mtd(chip);
393 /* Emulate NAND_CMD_READOOB */
394 if (command == NAND_CMD_READOOB) {
395 column += mtd->writesize;
396 command = NAND_CMD_READ0;
399 /* Command latch cycle */
400 if (command != NAND_CMD_NONE)
401 chip->legacy.cmd_ctrl(chip, command,
402 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
404 if (column != -1 || page_addr != -1) {
405 int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE;
407 /* Serially input address */
409 /* Adjust columns for 16 bit buswidth */
410 if (chip->options & NAND_BUSWIDTH_16 &&
411 !nand_opcode_8bits(command))
413 chip->legacy.cmd_ctrl(chip, column, ctrl);
414 ctrl &= ~NAND_CTRL_CHANGE;
416 /* Only output a single addr cycle for 8bits opcodes. */
417 if (!nand_opcode_8bits(command))
418 chip->legacy.cmd_ctrl(chip, column >> 8, ctrl);
420 if (page_addr != -1) {
421 chip->legacy.cmd_ctrl(chip, page_addr, ctrl);
422 chip->legacy.cmd_ctrl(chip, page_addr >> 8,
423 NAND_NCE | NAND_ALE);
424 if (chip->options & NAND_ROW_ADDR_3)
425 chip->legacy.cmd_ctrl(chip, page_addr >> 16,
426 NAND_NCE | NAND_ALE);
429 chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
430 NAND_NCE | NAND_CTRL_CHANGE);
433 * Program and erase have their own busy handlers status, sequential
434 * in and status need no delay.
439 case NAND_CMD_CACHEDPROG:
440 case NAND_CMD_PAGEPROG:
441 case NAND_CMD_ERASE1:
442 case NAND_CMD_ERASE2:
444 case NAND_CMD_STATUS:
445 case NAND_CMD_READID:
446 case NAND_CMD_SET_FEATURES:
450 nand_ccs_delay(chip);
454 if (chip->legacy.dev_ready)
456 udelay(chip->legacy.chip_delay);
457 chip->legacy.cmd_ctrl(chip, NAND_CMD_STATUS,
458 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
459 chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
460 NAND_NCE | NAND_CTRL_CHANGE);
461 /* EZ-NAND can take upto 250ms as per ONFi v4.0 */
462 nand_wait_status_ready(chip, 250);
465 case NAND_CMD_RNDOUT:
466 /* No ready / busy check necessary */
467 chip->legacy.cmd_ctrl(chip, NAND_CMD_RNDOUTSTART,
468 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
469 chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
470 NAND_NCE | NAND_CTRL_CHANGE);
472 nand_ccs_delay(chip);
477 * READ0 is sometimes used to exit GET STATUS mode. When this
478 * is the case no address cycles are requested, and we can use
479 * this information to detect that READSTART should not be
482 if (column == -1 && page_addr == -1)
485 chip->legacy.cmd_ctrl(chip, NAND_CMD_READSTART,
486 NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE);
487 chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE,
488 NAND_NCE | NAND_CTRL_CHANGE);
489 fallthrough; /* This applies to read commands */
492 * If we don't have access to the busy pin, we apply the given
495 if (!chip->legacy.dev_ready) {
496 udelay(chip->legacy.chip_delay);
502 * Apply this short delay always to ensure that we do wait tWB in
503 * any case on any machine.
507 nand_wait_ready(chip);
511 * nand_get_set_features_notsupp - set/get features stub returning -ENOTSUPP
512 * @chip: nand chip info structure
513 * @addr: feature address.
514 * @subfeature_param: the subfeature parameters, a four bytes array.
516 * Should be used by NAND controller drivers that do not support the SET/GET
517 * FEATURES operations.
519 int nand_get_set_features_notsupp(struct nand_chip *chip, int addr,
520 u8 *subfeature_param)
524 EXPORT_SYMBOL(nand_get_set_features_notsupp);
527 * nand_wait - [DEFAULT] wait until the command is done
528 * @chip: NAND chip structure
530 * Wait for command done. This applies to erase and program only.
532 static int nand_wait(struct nand_chip *chip)
535 unsigned long timeo = 400;
540 * Apply this short delay always to ensure that we do wait tWB in any
541 * case on any machine.
545 ret = nand_status_op(chip, NULL);
549 if (in_interrupt() || oops_in_progress)
550 panic_nand_wait(chip, timeo);
552 timeo = jiffies + msecs_to_jiffies(timeo);
554 if (chip->legacy.dev_ready) {
555 if (chip->legacy.dev_ready(chip))
558 ret = nand_read_data_op(chip, &status,
559 sizeof(status), true,
564 if (status & NAND_STATUS_READY)
568 } while (time_before(jiffies, timeo));
571 ret = nand_read_data_op(chip, &status, sizeof(status), true, false);
575 /* This can happen if in case of timeout or buggy dev_ready */
576 WARN_ON(!(status & NAND_STATUS_READY));
580 void nand_legacy_set_defaults(struct nand_chip *chip)
582 unsigned int busw = chip->options & NAND_BUSWIDTH_16;
584 if (nand_has_exec_op(chip))
587 /* check for proper chip_delay setup, set 20us if not */
588 if (!chip->legacy.chip_delay)
589 chip->legacy.chip_delay = 20;
591 /* check, if a user supplied command function given */
592 if (!chip->legacy.cmdfunc)
593 chip->legacy.cmdfunc = nand_command;
595 /* check, if a user supplied wait function given */
596 if (chip->legacy.waitfunc == NULL)
597 chip->legacy.waitfunc = nand_wait;
599 if (!chip->legacy.select_chip)
600 chip->legacy.select_chip = nand_select_chip;
602 /* If called twice, pointers that depend on busw may need to be reset */
603 if (!chip->legacy.read_byte || chip->legacy.read_byte == nand_read_byte)
604 chip->legacy.read_byte = busw ? nand_read_byte16 : nand_read_byte;
605 if (!chip->legacy.write_buf || chip->legacy.write_buf == nand_write_buf)
606 chip->legacy.write_buf = busw ? nand_write_buf16 : nand_write_buf;
607 if (!chip->legacy.write_byte || chip->legacy.write_byte == nand_write_byte)
608 chip->legacy.write_byte = busw ? nand_write_byte16 : nand_write_byte;
609 if (!chip->legacy.read_buf || chip->legacy.read_buf == nand_read_buf)
610 chip->legacy.read_buf = busw ? nand_read_buf16 : nand_read_buf;
613 void nand_legacy_adjust_cmdfunc(struct nand_chip *chip)
615 struct mtd_info *mtd = nand_to_mtd(chip);
617 /* Do not replace user supplied command function! */
618 if (mtd->writesize > 512 && chip->legacy.cmdfunc == nand_command)
619 chip->legacy.cmdfunc = nand_command_lp;
622 int nand_legacy_check_hooks(struct nand_chip *chip)
625 * ->legacy.cmdfunc() is legacy and will only be used if ->exec_op() is
628 if (nand_has_exec_op(chip))
632 * Default functions assigned for ->legacy.cmdfunc() and
633 * ->legacy.select_chip() both expect ->legacy.cmd_ctrl() to be
636 if ((!chip->legacy.cmdfunc || !chip->legacy.select_chip) &&
637 !chip->legacy.cmd_ctrl) {
638 pr_err("->legacy.cmd_ctrl() should be provided\n");