Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Anton Vorontsov | 6ab97e9 | 2008-01-14 23:09:32 +0300 | [diff] [blame] | 2 | /* |
| 3 | * FSL UPM NAND driver |
| 4 | * |
| 5 | * Copyright (C) 2007 MontaVista Software, Inc. |
| 6 | * Anton Vorontsov <avorontsov@ru.mvista.com> |
Anton Vorontsov | 6ab97e9 | 2008-01-14 23:09:32 +0300 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <config.h> |
Anton Vorontsov | 6ab97e9 | 2008-01-14 23:09:32 +0300 | [diff] [blame] | 10 | #include <common.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Anton Vorontsov | 6ab97e9 | 2008-01-14 23:09:32 +0300 | [diff] [blame] | 12 | #include <asm/io.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 13 | #include <linux/delay.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 14 | #include <linux/errno.h> |
Anton Vorontsov | 6ab97e9 | 2008-01-14 23:09:32 +0300 | [diff] [blame] | 15 | #include <linux/mtd/mtd.h> |
| 16 | #include <linux/mtd/fsl_upm.h> |
| 17 | #include <nand.h> |
| 18 | |
Anton Vorontsov | 6ab97e9 | 2008-01-14 23:09:32 +0300 | [diff] [blame] | 19 | static void fsl_upm_start_pattern(struct fsl_upm *upm, u32 pat_offset) |
| 20 | { |
Wolfgang Grandegger | 0d3058a | 2008-06-05 13:02:29 +0200 | [diff] [blame] | 21 | clrsetbits_be32(upm->mxmr, MxMR_MAD_MSK, MxMR_OP_RUNP | pat_offset); |
John Schmoller | a5fb403 | 2010-12-02 11:43:10 -0600 | [diff] [blame] | 22 | (void)in_be32(upm->mxmr); |
Anton Vorontsov | 6ab97e9 | 2008-01-14 23:09:32 +0300 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | static void fsl_upm_end_pattern(struct fsl_upm *upm) |
| 26 | { |
Wolfgang Grandegger | 0d3058a | 2008-06-05 13:02:29 +0200 | [diff] [blame] | 27 | clrbits_be32(upm->mxmr, MxMR_OP_RUNP); |
| 28 | |
| 29 | while (in_be32(upm->mxmr) & MxMR_OP_RUNP) |
Anton Vorontsov | 6ab97e9 | 2008-01-14 23:09:32 +0300 | [diff] [blame] | 30 | eieio(); |
| 31 | } |
| 32 | |
Wolfgang Grandegger | f154044 | 2009-02-11 18:38:21 +0100 | [diff] [blame] | 33 | static void fsl_upm_run_pattern(struct fsl_upm *upm, int width, |
| 34 | void __iomem *io_addr, u32 mar) |
Anton Vorontsov | 6ab97e9 | 2008-01-14 23:09:32 +0300 | [diff] [blame] | 35 | { |
Wolfgang Grandegger | f154044 | 2009-02-11 18:38:21 +0100 | [diff] [blame] | 36 | out_be32(upm->mar, mar); |
John Schmoller | a5fb403 | 2010-12-02 11:43:10 -0600 | [diff] [blame] | 37 | (void)in_be32(upm->mar); |
Wolfgang Grandegger | 0d3058a | 2008-06-05 13:02:29 +0200 | [diff] [blame] | 38 | switch (width) { |
| 39 | case 8: |
Wolfgang Grandegger | f154044 | 2009-02-11 18:38:21 +0100 | [diff] [blame] | 40 | out_8(io_addr, 0x0); |
Wolfgang Grandegger | 0d3058a | 2008-06-05 13:02:29 +0200 | [diff] [blame] | 41 | break; |
| 42 | case 16: |
Wolfgang Grandegger | f154044 | 2009-02-11 18:38:21 +0100 | [diff] [blame] | 43 | out_be16(io_addr, 0x0); |
Wolfgang Grandegger | 0d3058a | 2008-06-05 13:02:29 +0200 | [diff] [blame] | 44 | break; |
| 45 | case 32: |
Wolfgang Grandegger | f154044 | 2009-02-11 18:38:21 +0100 | [diff] [blame] | 46 | out_be32(io_addr, 0x0); |
Wolfgang Grandegger | 0d3058a | 2008-06-05 13:02:29 +0200 | [diff] [blame] | 47 | break; |
Anton Vorontsov | 6ab97e9 | 2008-01-14 23:09:32 +0300 | [diff] [blame] | 48 | } |
Anton Vorontsov | 6ab97e9 | 2008-01-14 23:09:32 +0300 | [diff] [blame] | 49 | } |
| 50 | |
Wolfgang Grandegger | 64de8db | 2009-02-11 18:38:23 +0100 | [diff] [blame] | 51 | static void fun_wait(struct fsl_upm_nand *fun) |
| 52 | { |
| 53 | if (fun->dev_ready) { |
| 54 | while (!fun->dev_ready(fun->chip_nr)) |
| 55 | debug("unexpected busy state\n"); |
| 56 | } else { |
| 57 | /* |
Wolfgang Denk | 178d49c | 2012-10-03 23:36:18 +0000 | [diff] [blame] | 58 | * If the R/B pin is not connected, |
Wolfgang Grandegger | 64de8db | 2009-02-11 18:38:23 +0100 | [diff] [blame] | 59 | * a short delay is necessary. |
| 60 | */ |
| 61 | udelay(1); |
| 62 | } |
| 63 | } |
| 64 | |
Wolfgang Grandegger | f154044 | 2009-02-11 18:38:21 +0100 | [diff] [blame] | 65 | #if CONFIG_SYS_NAND_MAX_CHIPS > 1 |
| 66 | static void fun_select_chip(struct mtd_info *mtd, int chip_nr) |
| 67 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 68 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 69 | struct fsl_upm_nand *fun = nand_get_controller_data(chip); |
Wolfgang Grandegger | f154044 | 2009-02-11 18:38:21 +0100 | [diff] [blame] | 70 | |
| 71 | if (chip_nr >= 0) { |
| 72 | fun->chip_nr = chip_nr; |
| 73 | chip->IO_ADDR_R = chip->IO_ADDR_W = |
| 74 | fun->upm.io_addr + fun->chip_offset * chip_nr; |
| 75 | } else if (chip_nr == -1) { |
| 76 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE); |
| 77 | } |
| 78 | } |
| 79 | #endif |
| 80 | |
Anton Vorontsov | 6716603 | 2008-06-12 11:10:21 -0500 | [diff] [blame] | 81 | static void fun_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl) |
Anton Vorontsov | 6ab97e9 | 2008-01-14 23:09:32 +0300 | [diff] [blame] | 82 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 83 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 84 | struct fsl_upm_nand *fun = nand_get_controller_data(chip); |
Wolfgang Grandegger | f154044 | 2009-02-11 18:38:21 +0100 | [diff] [blame] | 85 | void __iomem *io_addr; |
| 86 | u32 mar; |
Anton Vorontsov | 6ab97e9 | 2008-01-14 23:09:32 +0300 | [diff] [blame] | 87 | |
Anton Vorontsov | 6716603 | 2008-06-12 11:10:21 -0500 | [diff] [blame] | 88 | if (!(ctrl & fun->last_ctrl)) { |
Wolfgang Grandegger | 0d3058a | 2008-06-05 13:02:29 +0200 | [diff] [blame] | 89 | fsl_upm_end_pattern(&fun->upm); |
Anton Vorontsov | 6ab97e9 | 2008-01-14 23:09:32 +0300 | [diff] [blame] | 90 | |
Anton Vorontsov | 6716603 | 2008-06-12 11:10:21 -0500 | [diff] [blame] | 91 | if (cmd == NAND_CMD_NONE) |
| 92 | return; |
| 93 | |
| 94 | fun->last_ctrl = ctrl & (NAND_ALE | NAND_CLE); |
| 95 | } |
Anton Vorontsov | 6ab97e9 | 2008-01-14 23:09:32 +0300 | [diff] [blame] | 96 | |
Anton Vorontsov | 6716603 | 2008-06-12 11:10:21 -0500 | [diff] [blame] | 97 | if (ctrl & NAND_CTRL_CHANGE) { |
| 98 | if (ctrl & NAND_ALE) |
| 99 | fsl_upm_start_pattern(&fun->upm, fun->upm_addr_offset); |
| 100 | else if (ctrl & NAND_CLE) |
| 101 | fsl_upm_start_pattern(&fun->upm, fun->upm_cmd_offset); |
| 102 | } |
Anton Vorontsov | 6ab97e9 | 2008-01-14 23:09:32 +0300 | [diff] [blame] | 103 | |
Wolfgang Grandegger | f154044 | 2009-02-11 18:38:21 +0100 | [diff] [blame] | 104 | mar = cmd << (32 - fun->width); |
| 105 | io_addr = fun->upm.io_addr; |
| 106 | #if CONFIG_SYS_NAND_MAX_CHIPS > 1 |
Wolfgang Grandegger | ac20533 | 2009-02-11 18:38:22 +0100 | [diff] [blame] | 107 | if (fun->chip_nr > 0) { |
Wolfgang Grandegger | f154044 | 2009-02-11 18:38:21 +0100 | [diff] [blame] | 108 | io_addr += fun->chip_offset * fun->chip_nr; |
Wolfgang Grandegger | ac20533 | 2009-02-11 18:38:22 +0100 | [diff] [blame] | 109 | if (fun->upm_mar_chip_offset) |
| 110 | mar |= fun->upm_mar_chip_offset * fun->chip_nr; |
| 111 | } |
Wolfgang Grandegger | f154044 | 2009-02-11 18:38:21 +0100 | [diff] [blame] | 112 | #endif |
| 113 | fsl_upm_run_pattern(&fun->upm, fun->width, io_addr, mar); |
Anton Vorontsov | 6ab97e9 | 2008-01-14 23:09:32 +0300 | [diff] [blame] | 114 | |
Anton Vorontsov | 6716603 | 2008-06-12 11:10:21 -0500 | [diff] [blame] | 115 | /* |
Wolfgang Denk | ec7fbf5 | 2013-10-04 17:43:24 +0200 | [diff] [blame] | 116 | * Some boards/chips needs this. At least the MPC8360E-RDK |
| 117 | * needs it. Probably weird chip, because I don't see any |
| 118 | * need for this on MPC8555E + Samsung K9F1G08U0A. Usually |
| 119 | * here are 0-2 unexpected busy states per block read. |
Anton Vorontsov | 6716603 | 2008-06-12 11:10:21 -0500 | [diff] [blame] | 120 | */ |
Wolfgang Grandegger | 64de8db | 2009-02-11 18:38:23 +0100 | [diff] [blame] | 121 | if (fun->wait_flags & FSL_UPM_WAIT_RUN_PATTERN) |
| 122 | fun_wait(fun); |
Anton Vorontsov | 6ab97e9 | 2008-01-14 23:09:32 +0300 | [diff] [blame] | 123 | } |
| 124 | |
Marek Vasut | 3b8756f | 2011-10-04 00:56:07 +0200 | [diff] [blame] | 125 | static u8 upm_nand_read_byte(struct mtd_info *mtd) |
Anton Vorontsov | 6ab97e9 | 2008-01-14 23:09:32 +0300 | [diff] [blame] | 126 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 127 | struct nand_chip *chip = mtd_to_nand(mtd); |
Anton Vorontsov | 6ab97e9 | 2008-01-14 23:09:32 +0300 | [diff] [blame] | 128 | |
| 129 | return in_8(chip->IO_ADDR_R); |
| 130 | } |
| 131 | |
Marek Vasut | 3b8756f | 2011-10-04 00:56:07 +0200 | [diff] [blame] | 132 | static void upm_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len) |
Anton Vorontsov | 6ab97e9 | 2008-01-14 23:09:32 +0300 | [diff] [blame] | 133 | { |
| 134 | int i; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 135 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 136 | struct fsl_upm_nand *fun = nand_get_controller_data(chip); |
Anton Vorontsov | 6ab97e9 | 2008-01-14 23:09:32 +0300 | [diff] [blame] | 137 | |
Wolfgang Grandegger | 64de8db | 2009-02-11 18:38:23 +0100 | [diff] [blame] | 138 | for (i = 0; i < len; i++) { |
Anton Vorontsov | 6ab97e9 | 2008-01-14 23:09:32 +0300 | [diff] [blame] | 139 | out_8(chip->IO_ADDR_W, buf[i]); |
Wolfgang Grandegger | 64de8db | 2009-02-11 18:38:23 +0100 | [diff] [blame] | 140 | if (fun->wait_flags & FSL_UPM_WAIT_WRITE_BYTE) |
| 141 | fun_wait(fun); |
| 142 | } |
| 143 | |
| 144 | if (fun->wait_flags & FSL_UPM_WAIT_WRITE_BUFFER) |
| 145 | fun_wait(fun); |
Anton Vorontsov | 6ab97e9 | 2008-01-14 23:09:32 +0300 | [diff] [blame] | 146 | } |
| 147 | |
Marek Vasut | 3b8756f | 2011-10-04 00:56:07 +0200 | [diff] [blame] | 148 | static void upm_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len) |
Anton Vorontsov | 6ab97e9 | 2008-01-14 23:09:32 +0300 | [diff] [blame] | 149 | { |
| 150 | int i; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 151 | struct nand_chip *chip = mtd_to_nand(mtd); |
Anton Vorontsov | 6ab97e9 | 2008-01-14 23:09:32 +0300 | [diff] [blame] | 152 | |
| 153 | for (i = 0; i < len; i++) |
| 154 | buf[i] = in_8(chip->IO_ADDR_R); |
| 155 | } |
| 156 | |
Anton Vorontsov | 6ab97e9 | 2008-01-14 23:09:32 +0300 | [diff] [blame] | 157 | static int nand_dev_ready(struct mtd_info *mtd) |
| 158 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 159 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 160 | struct fsl_upm_nand *fun = nand_get_controller_data(chip); |
Anton Vorontsov | 6ab97e9 | 2008-01-14 23:09:32 +0300 | [diff] [blame] | 161 | |
Wolfgang Grandegger | f154044 | 2009-02-11 18:38:21 +0100 | [diff] [blame] | 162 | return fun->dev_ready(fun->chip_nr); |
Anton Vorontsov | 6ab97e9 | 2008-01-14 23:09:32 +0300 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | int fsl_upm_nand_init(struct nand_chip *chip, struct fsl_upm_nand *fun) |
| 166 | { |
Wolfgang Grandegger | 0d3058a | 2008-06-05 13:02:29 +0200 | [diff] [blame] | 167 | if (fun->width != 8 && fun->width != 16 && fun->width != 32) |
Anton Vorontsov | 6ab97e9 | 2008-01-14 23:09:32 +0300 | [diff] [blame] | 168 | return -ENOSYS; |
| 169 | |
Anton Vorontsov | 6716603 | 2008-06-12 11:10:21 -0500 | [diff] [blame] | 170 | fun->last_ctrl = NAND_CLE; |
| 171 | |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 172 | nand_set_controller_data(chip, fun); |
Anton Vorontsov | 6ab97e9 | 2008-01-14 23:09:32 +0300 | [diff] [blame] | 173 | chip->chip_delay = fun->chip_delay; |
Anton Vorontsov | 6716603 | 2008-06-12 11:10:21 -0500 | [diff] [blame] | 174 | chip->ecc.mode = NAND_ECC_SOFT; |
| 175 | chip->cmd_ctrl = fun_cmd_ctrl; |
Wolfgang Grandegger | f154044 | 2009-02-11 18:38:21 +0100 | [diff] [blame] | 176 | #if CONFIG_SYS_NAND_MAX_CHIPS > 1 |
| 177 | chip->select_chip = fun_select_chip; |
| 178 | #endif |
Marek Vasut | 3b8756f | 2011-10-04 00:56:07 +0200 | [diff] [blame] | 179 | chip->read_byte = upm_nand_read_byte; |
| 180 | chip->read_buf = upm_nand_read_buf; |
| 181 | chip->write_buf = upm_nand_write_buf; |
Wolfgang Grandegger | 0d3058a | 2008-06-05 13:02:29 +0200 | [diff] [blame] | 182 | if (fun->dev_ready) |
| 183 | chip->dev_ready = nand_dev_ready; |
Anton Vorontsov | 6ab97e9 | 2008-01-14 23:09:32 +0300 | [diff] [blame] | 184 | |
| 185 | return 0; |
| 186 | } |