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