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