Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 1 | /* |
| 2 | * SuperH FLCTL nand controller |
| 3 | * |
Magnus Damm | b79c7ad | 2010-02-02 13:01:25 +0900 | [diff] [blame] | 4 | * Copyright (c) 2008 Renesas Solutions Corp. |
| 5 | * Copyright (c) 2008 Atom Create Engineering Co., Ltd. |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 6 | * |
Magnus Damm | b79c7ad | 2010-02-02 13:01:25 +0900 | [diff] [blame] | 7 | * Based on fsl_elbc_nand.c, Copyright (c) 2006-2007 Freescale Semiconductor |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; version 2 of the License. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/delay.h> |
Bastian Hecht | 3c7ea4e | 2012-05-14 14:14:41 +0200 | [diff] [blame] | 27 | #include <linux/interrupt.h> |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 28 | #include <linux/io.h> |
| 29 | #include <linux/platform_device.h> |
Bastian Hecht | cfe7819 | 2012-03-18 15:13:20 +0100 | [diff] [blame] | 30 | #include <linux/pm_runtime.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 31 | #include <linux/slab.h> |
Bastian Hecht | d76236f | 2012-07-05 12:41:01 +0200 | [diff] [blame] | 32 | #include <linux/string.h> |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 33 | |
| 34 | #include <linux/mtd/mtd.h> |
| 35 | #include <linux/mtd/nand.h> |
| 36 | #include <linux/mtd/partitions.h> |
| 37 | #include <linux/mtd/sh_flctl.h> |
| 38 | |
| 39 | static struct nand_ecclayout flctl_4secc_oob_16 = { |
| 40 | .eccbytes = 10, |
| 41 | .eccpos = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, |
| 42 | .oobfree = { |
| 43 | {.offset = 12, |
| 44 | . length = 4} }, |
| 45 | }; |
| 46 | |
| 47 | static struct nand_ecclayout flctl_4secc_oob_64 = { |
Bastian Hecht | aa32d1f | 2012-05-14 14:14:42 +0200 | [diff] [blame] | 48 | .eccbytes = 4 * 10, |
| 49 | .eccpos = { |
| 50 | 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, |
| 51 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 52 | 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, |
| 53 | 54, 55, 56, 57, 58, 59, 60, 61, 62, 63 }, |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 54 | .oobfree = { |
Bastian Hecht | aa32d1f | 2012-05-14 14:14:42 +0200 | [diff] [blame] | 55 | {.offset = 2, .length = 4}, |
| 56 | {.offset = 16, .length = 6}, |
| 57 | {.offset = 32, .length = 6}, |
| 58 | {.offset = 48, .length = 6} }, |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | static uint8_t scan_ff_pattern[] = { 0xff, 0xff }; |
| 62 | |
| 63 | static struct nand_bbt_descr flctl_4secc_smallpage = { |
| 64 | .options = NAND_BBT_SCAN2NDPAGE, |
| 65 | .offs = 11, |
| 66 | .len = 1, |
| 67 | .pattern = scan_ff_pattern, |
| 68 | }; |
| 69 | |
| 70 | static struct nand_bbt_descr flctl_4secc_largepage = { |
Yoshihiro Shimoda | c0e6616 | 2009-03-24 18:27:24 +0900 | [diff] [blame] | 71 | .options = NAND_BBT_SCAN2NDPAGE, |
Bastian Hecht | aa32d1f | 2012-05-14 14:14:42 +0200 | [diff] [blame] | 72 | .offs = 0, |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 73 | .len = 2, |
| 74 | .pattern = scan_ff_pattern, |
| 75 | }; |
| 76 | |
| 77 | static void empty_fifo(struct sh_flctl *flctl) |
| 78 | { |
Bastian Hecht | 3c7ea4e | 2012-05-14 14:14:41 +0200 | [diff] [blame] | 79 | writel(flctl->flintdmacr_base | AC1CLR | AC0CLR, FLINTDMACR(flctl)); |
| 80 | writel(flctl->flintdmacr_base, FLINTDMACR(flctl)); |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static void start_translation(struct sh_flctl *flctl) |
| 84 | { |
| 85 | writeb(TRSTRT, FLTRCR(flctl)); |
| 86 | } |
| 87 | |
Magnus Damm | b79c7ad | 2010-02-02 13:01:25 +0900 | [diff] [blame] | 88 | static void timeout_error(struct sh_flctl *flctl, const char *str) |
| 89 | { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 90 | dev_err(&flctl->pdev->dev, "Timeout occurred in %s\n", str); |
Magnus Damm | b79c7ad | 2010-02-02 13:01:25 +0900 | [diff] [blame] | 91 | } |
| 92 | |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 93 | static void wait_completion(struct sh_flctl *flctl) |
| 94 | { |
| 95 | uint32_t timeout = LOOP_TIMEOUT_MAX; |
| 96 | |
| 97 | while (timeout--) { |
| 98 | if (readb(FLTRCR(flctl)) & TREND) { |
| 99 | writeb(0x0, FLTRCR(flctl)); |
| 100 | return; |
| 101 | } |
| 102 | udelay(1); |
| 103 | } |
| 104 | |
Magnus Damm | b79c7ad | 2010-02-02 13:01:25 +0900 | [diff] [blame] | 105 | timeout_error(flctl, __func__); |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 106 | writeb(0x0, FLTRCR(flctl)); |
| 107 | } |
| 108 | |
| 109 | static void set_addr(struct mtd_info *mtd, int column, int page_addr) |
| 110 | { |
| 111 | struct sh_flctl *flctl = mtd_to_flctl(mtd); |
| 112 | uint32_t addr = 0; |
| 113 | |
| 114 | if (column == -1) { |
| 115 | addr = page_addr; /* ERASE1 */ |
| 116 | } else if (page_addr != -1) { |
| 117 | /* SEQIN, READ0, etc.. */ |
Magnus Damm | 010ab82 | 2010-01-27 09:17:21 +0000 | [diff] [blame] | 118 | if (flctl->chip.options & NAND_BUSWIDTH_16) |
| 119 | column >>= 1; |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 120 | if (flctl->page_size) { |
| 121 | addr = column & 0x0FFF; |
| 122 | addr |= (page_addr & 0xff) << 16; |
| 123 | addr |= ((page_addr >> 8) & 0xff) << 24; |
| 124 | /* big than 128MB */ |
| 125 | if (flctl->rw_ADRCNT == ADRCNT2_E) { |
| 126 | uint32_t addr2; |
| 127 | addr2 = (page_addr >> 16) & 0xff; |
| 128 | writel(addr2, FLADR2(flctl)); |
| 129 | } |
| 130 | } else { |
| 131 | addr = column; |
| 132 | addr |= (page_addr & 0xff) << 8; |
| 133 | addr |= ((page_addr >> 8) & 0xff) << 16; |
| 134 | addr |= ((page_addr >> 16) & 0xff) << 24; |
| 135 | } |
| 136 | } |
| 137 | writel(addr, FLADR(flctl)); |
| 138 | } |
| 139 | |
| 140 | static void wait_rfifo_ready(struct sh_flctl *flctl) |
| 141 | { |
| 142 | uint32_t timeout = LOOP_TIMEOUT_MAX; |
| 143 | |
| 144 | while (timeout--) { |
| 145 | uint32_t val; |
| 146 | /* check FIFO */ |
| 147 | val = readl(FLDTCNTR(flctl)) >> 16; |
| 148 | if (val & 0xFF) |
| 149 | return; |
| 150 | udelay(1); |
| 151 | } |
Magnus Damm | b79c7ad | 2010-02-02 13:01:25 +0900 | [diff] [blame] | 152 | timeout_error(flctl, __func__); |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static void wait_wfifo_ready(struct sh_flctl *flctl) |
| 156 | { |
| 157 | uint32_t len, timeout = LOOP_TIMEOUT_MAX; |
| 158 | |
| 159 | while (timeout--) { |
| 160 | /* check FIFO */ |
| 161 | len = (readl(FLDTCNTR(flctl)) >> 16) & 0xFF; |
| 162 | if (len >= 4) |
| 163 | return; |
| 164 | udelay(1); |
| 165 | } |
Magnus Damm | b79c7ad | 2010-02-02 13:01:25 +0900 | [diff] [blame] | 166 | timeout_error(flctl, __func__); |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 167 | } |
| 168 | |
Bastian Hecht | 6667a6d5 | 2012-05-14 14:14:46 +0200 | [diff] [blame] | 169 | static enum flctl_ecc_res_t wait_recfifo_ready |
| 170 | (struct sh_flctl *flctl, int sector_number) |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 171 | { |
| 172 | uint32_t timeout = LOOP_TIMEOUT_MAX; |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 173 | void __iomem *ecc_reg[4]; |
| 174 | int i; |
Bastian Hecht | 6667a6d5 | 2012-05-14 14:14:46 +0200 | [diff] [blame] | 175 | int state = FL_SUCCESS; |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 176 | uint32_t data, size; |
| 177 | |
Bastian Hecht | 6667a6d5 | 2012-05-14 14:14:46 +0200 | [diff] [blame] | 178 | /* |
| 179 | * First this loops checks in FLDTCNTR if we are ready to read out the |
| 180 | * oob data. This is the case if either all went fine without errors or |
| 181 | * if the bottom part of the loop corrected the errors or marked them as |
| 182 | * uncorrectable and the controller is given time to push the data into |
| 183 | * the FIFO. |
| 184 | */ |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 185 | while (timeout--) { |
Bastian Hecht | 6667a6d5 | 2012-05-14 14:14:46 +0200 | [diff] [blame] | 186 | /* check if all is ok and we can read out the OOB */ |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 187 | size = readl(FLDTCNTR(flctl)) >> 24; |
Bastian Hecht | 6667a6d5 | 2012-05-14 14:14:46 +0200 | [diff] [blame] | 188 | if ((size & 0xFF) == 4) |
| 189 | return state; |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 190 | |
Bastian Hecht | 6667a6d5 | 2012-05-14 14:14:46 +0200 | [diff] [blame] | 191 | /* check if a correction code has been calculated */ |
| 192 | if (!(readl(FL4ECCCR(flctl)) & _4ECCEND)) { |
| 193 | /* |
| 194 | * either we wait for the fifo to be filled or a |
| 195 | * correction pattern is being generated |
| 196 | */ |
| 197 | udelay(1); |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 198 | continue; |
Bastian Hecht | 6667a6d5 | 2012-05-14 14:14:46 +0200 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /* check for an uncorrectable error */ |
| 202 | if (readl(FL4ECCCR(flctl)) & _4ECCFA) { |
| 203 | /* check if we face a non-empty page */ |
| 204 | for (i = 0; i < 512; i++) { |
| 205 | if (flctl->done_buff[i] != 0xff) { |
| 206 | state = FL_ERROR; /* can't correct */ |
| 207 | break; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | if (state == FL_SUCCESS) |
| 212 | dev_dbg(&flctl->pdev->dev, |
| 213 | "reading empty sector %d, ecc error ignored\n", |
| 214 | sector_number); |
| 215 | |
| 216 | writel(0, FL4ECCCR(flctl)); |
| 217 | continue; |
| 218 | } |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 219 | |
| 220 | /* start error correction */ |
| 221 | ecc_reg[0] = FL4ECCRESULT0(flctl); |
| 222 | ecc_reg[1] = FL4ECCRESULT1(flctl); |
| 223 | ecc_reg[2] = FL4ECCRESULT2(flctl); |
| 224 | ecc_reg[3] = FL4ECCRESULT3(flctl); |
| 225 | |
| 226 | for (i = 0; i < 3; i++) { |
Bastian Hecht | 6667a6d5 | 2012-05-14 14:14:46 +0200 | [diff] [blame] | 227 | uint8_t org; |
Bastian Hecht | e8a9d8f | 2012-10-19 12:15:34 +0200 | [diff] [blame^] | 228 | unsigned int index; |
Bastian Hecht | 6667a6d5 | 2012-05-14 14:14:46 +0200 | [diff] [blame] | 229 | |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 230 | data = readl(ecc_reg[i]); |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 231 | |
Bastian Hecht | 6667a6d5 | 2012-05-14 14:14:46 +0200 | [diff] [blame] | 232 | if (flctl->page_size) |
| 233 | index = (512 * sector_number) + |
| 234 | (data >> 16); |
| 235 | else |
| 236 | index = data >> 16; |
Yoshihiro Shimoda | c0e6616 | 2009-03-24 18:27:24 +0900 | [diff] [blame] | 237 | |
Bastian Hecht | 6667a6d5 | 2012-05-14 14:14:46 +0200 | [diff] [blame] | 238 | org = flctl->done_buff[index]; |
| 239 | flctl->done_buff[index] = org ^ (data & 0xFF); |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 240 | } |
Bastian Hecht | 6667a6d5 | 2012-05-14 14:14:46 +0200 | [diff] [blame] | 241 | state = FL_REPAIRABLE; |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 242 | writel(0, FL4ECCCR(flctl)); |
| 243 | } |
| 244 | |
Magnus Damm | b79c7ad | 2010-02-02 13:01:25 +0900 | [diff] [blame] | 245 | timeout_error(flctl, __func__); |
Bastian Hecht | 6667a6d5 | 2012-05-14 14:14:46 +0200 | [diff] [blame] | 246 | return FL_TIMEOUT; /* timeout */ |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | static void wait_wecfifo_ready(struct sh_flctl *flctl) |
| 250 | { |
| 251 | uint32_t timeout = LOOP_TIMEOUT_MAX; |
| 252 | uint32_t len; |
| 253 | |
| 254 | while (timeout--) { |
| 255 | /* check FLECFIFO */ |
| 256 | len = (readl(FLDTCNTR(flctl)) >> 24) & 0xFF; |
| 257 | if (len >= 4) |
| 258 | return; |
| 259 | udelay(1); |
| 260 | } |
Magnus Damm | b79c7ad | 2010-02-02 13:01:25 +0900 | [diff] [blame] | 261 | timeout_error(flctl, __func__); |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | static void read_datareg(struct sh_flctl *flctl, int offset) |
| 265 | { |
| 266 | unsigned long data; |
| 267 | unsigned long *buf = (unsigned long *)&flctl->done_buff[offset]; |
| 268 | |
| 269 | wait_completion(flctl); |
| 270 | |
| 271 | data = readl(FLDATAR(flctl)); |
| 272 | *buf = le32_to_cpu(data); |
| 273 | } |
| 274 | |
| 275 | static void read_fiforeg(struct sh_flctl *flctl, int rlen, int offset) |
| 276 | { |
| 277 | int i, len_4align; |
| 278 | unsigned long *buf = (unsigned long *)&flctl->done_buff[offset]; |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 279 | |
| 280 | len_4align = (rlen + 3) / 4; |
| 281 | |
| 282 | for (i = 0; i < len_4align; i++) { |
| 283 | wait_rfifo_ready(flctl); |
Bastian Hecht | 3166df0 | 2012-05-14 14:14:47 +0200 | [diff] [blame] | 284 | buf[i] = readl(FLDTFIFO(flctl)); |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 285 | buf[i] = be32_to_cpu(buf[i]); |
| 286 | } |
| 287 | } |
| 288 | |
Bastian Hecht | 6667a6d5 | 2012-05-14 14:14:46 +0200 | [diff] [blame] | 289 | static enum flctl_ecc_res_t read_ecfiforeg |
| 290 | (struct sh_flctl *flctl, uint8_t *buff, int sector) |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 291 | { |
| 292 | int i; |
Bastian Hecht | 6667a6d5 | 2012-05-14 14:14:46 +0200 | [diff] [blame] | 293 | enum flctl_ecc_res_t res; |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 294 | unsigned long *ecc_buf = (unsigned long *)buff; |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 295 | |
Bastian Hecht | 6667a6d5 | 2012-05-14 14:14:46 +0200 | [diff] [blame] | 296 | res = wait_recfifo_ready(flctl , sector); |
| 297 | |
| 298 | if (res != FL_ERROR) { |
| 299 | for (i = 0; i < 4; i++) { |
| 300 | ecc_buf[i] = readl(FLECFIFO(flctl)); |
| 301 | ecc_buf[i] = be32_to_cpu(ecc_buf[i]); |
| 302 | } |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 303 | } |
| 304 | |
Bastian Hecht | 6667a6d5 | 2012-05-14 14:14:46 +0200 | [diff] [blame] | 305 | return res; |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 306 | } |
| 307 | |
Bastian Hecht | e8a9d8f | 2012-10-19 12:15:34 +0200 | [diff] [blame^] | 308 | static void write_fiforeg(struct sh_flctl *flctl, int rlen, |
| 309 | unsigned int offset) |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 310 | { |
| 311 | int i, len_4align; |
Bastian Hecht | e8a9d8f | 2012-10-19 12:15:34 +0200 | [diff] [blame^] | 312 | unsigned long *buf = (unsigned long *)&flctl->done_buff[offset]; |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 313 | |
| 314 | len_4align = (rlen + 3) / 4; |
| 315 | for (i = 0; i < len_4align; i++) { |
| 316 | wait_wfifo_ready(flctl); |
Bastian Hecht | e8a9d8f | 2012-10-19 12:15:34 +0200 | [diff] [blame^] | 317 | writel(cpu_to_be32(buf[i]), FLDTFIFO(flctl)); |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
Bastian Hecht | e8a9d8f | 2012-10-19 12:15:34 +0200 | [diff] [blame^] | 321 | static void write_ec_fiforeg(struct sh_flctl *flctl, int rlen, |
| 322 | unsigned int offset) |
Bastian Hecht | 3166df0 | 2012-05-14 14:14:47 +0200 | [diff] [blame] | 323 | { |
| 324 | int i, len_4align; |
Bastian Hecht | e8a9d8f | 2012-10-19 12:15:34 +0200 | [diff] [blame^] | 325 | unsigned long *buf = (unsigned long *)&flctl->done_buff[offset]; |
Bastian Hecht | 3166df0 | 2012-05-14 14:14:47 +0200 | [diff] [blame] | 326 | |
| 327 | len_4align = (rlen + 3) / 4; |
| 328 | for (i = 0; i < len_4align; i++) { |
| 329 | wait_wecfifo_ready(flctl); |
Bastian Hecht | e8a9d8f | 2012-10-19 12:15:34 +0200 | [diff] [blame^] | 330 | writel(cpu_to_be32(buf[i]), FLECFIFO(flctl)); |
Bastian Hecht | 3166df0 | 2012-05-14 14:14:47 +0200 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 334 | static void set_cmd_regs(struct mtd_info *mtd, uint32_t cmd, uint32_t flcmcdr_val) |
| 335 | { |
| 336 | struct sh_flctl *flctl = mtd_to_flctl(mtd); |
Bastian Hecht | 0b3f0d1 | 2012-03-01 10:48:39 +0100 | [diff] [blame] | 337 | uint32_t flcmncr_val = flctl->flcmncr_base & ~SEL_16BIT; |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 338 | uint32_t flcmdcr_val, addr_len_bytes = 0; |
| 339 | |
| 340 | /* Set SNAND bit if page size is 2048byte */ |
| 341 | if (flctl->page_size) |
| 342 | flcmncr_val |= SNAND_E; |
| 343 | else |
| 344 | flcmncr_val &= ~SNAND_E; |
| 345 | |
| 346 | /* default FLCMDCR val */ |
| 347 | flcmdcr_val = DOCMD1_E | DOADR_E; |
| 348 | |
| 349 | /* Set for FLCMDCR */ |
| 350 | switch (cmd) { |
| 351 | case NAND_CMD_ERASE1: |
| 352 | addr_len_bytes = flctl->erase_ADRCNT; |
| 353 | flcmdcr_val |= DOCMD2_E; |
| 354 | break; |
| 355 | case NAND_CMD_READ0: |
| 356 | case NAND_CMD_READOOB: |
Bastian Hecht | dd5ab24 | 2012-03-01 10:48:38 +0100 | [diff] [blame] | 357 | case NAND_CMD_RNDOUT: |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 358 | addr_len_bytes = flctl->rw_ADRCNT; |
| 359 | flcmdcr_val |= CDSRC_E; |
Magnus Damm | 010ab82 | 2010-01-27 09:17:21 +0000 | [diff] [blame] | 360 | if (flctl->chip.options & NAND_BUSWIDTH_16) |
| 361 | flcmncr_val |= SEL_16BIT; |
Yoshihiro Shimoda | 6028aa0 | 2008-10-14 21:23:26 +0900 | [diff] [blame] | 362 | break; |
| 363 | case NAND_CMD_SEQIN: |
| 364 | /* This case is that cmd is READ0 or READ1 or READ00 */ |
| 365 | flcmdcr_val &= ~DOADR_E; /* ONLY execute 1st cmd */ |
| 366 | break; |
| 367 | case NAND_CMD_PAGEPROG: |
| 368 | addr_len_bytes = flctl->rw_ADRCNT; |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 369 | flcmdcr_val |= DOCMD2_E | CDSRC_E | SELRW; |
Magnus Damm | 010ab82 | 2010-01-27 09:17:21 +0000 | [diff] [blame] | 370 | if (flctl->chip.options & NAND_BUSWIDTH_16) |
| 371 | flcmncr_val |= SEL_16BIT; |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 372 | break; |
| 373 | case NAND_CMD_READID: |
| 374 | flcmncr_val &= ~SNAND_E; |
Bastian Hecht | 7b6b230 | 2012-03-01 10:48:37 +0100 | [diff] [blame] | 375 | flcmdcr_val |= CDSRC_E; |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 376 | addr_len_bytes = ADRCNT_1; |
| 377 | break; |
| 378 | case NAND_CMD_STATUS: |
| 379 | case NAND_CMD_RESET: |
| 380 | flcmncr_val &= ~SNAND_E; |
| 381 | flcmdcr_val &= ~(DOADR_E | DOSR_E); |
| 382 | break; |
| 383 | default: |
| 384 | break; |
| 385 | } |
| 386 | |
| 387 | /* Set address bytes parameter */ |
| 388 | flcmdcr_val |= addr_len_bytes; |
| 389 | |
| 390 | /* Now actually write */ |
| 391 | writel(flcmncr_val, FLCMNCR(flctl)); |
| 392 | writel(flcmdcr_val, FLCMDCR(flctl)); |
| 393 | writel(flcmcdr_val, FLCMCDR(flctl)); |
| 394 | } |
| 395 | |
| 396 | static int flctl_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip, |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 397 | uint8_t *buf, int oob_required, int page) |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 398 | { |
Bastian Hecht | 50ed399 | 2012-05-14 14:14:44 +0200 | [diff] [blame] | 399 | chip->read_buf(mtd, buf, mtd->writesize); |
Bastian Hecht | 894824f | 2012-07-05 12:41:02 +0200 | [diff] [blame] | 400 | if (oob_required) |
| 401 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 402 | return 0; |
| 403 | } |
| 404 | |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 405 | static int flctl_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip, |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 406 | const uint8_t *buf, int oob_required) |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 407 | { |
Bastian Hecht | 50ed399 | 2012-05-14 14:14:44 +0200 | [diff] [blame] | 408 | chip->write_buf(mtd, buf, mtd->writesize); |
Bastian Hecht | 3166df0 | 2012-05-14 14:14:47 +0200 | [diff] [blame] | 409 | chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 410 | return 0; |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | static void execmd_read_page_sector(struct mtd_info *mtd, int page_addr) |
| 414 | { |
| 415 | struct sh_flctl *flctl = mtd_to_flctl(mtd); |
| 416 | int sector, page_sectors; |
Bastian Hecht | 6667a6d5 | 2012-05-14 14:14:46 +0200 | [diff] [blame] | 417 | enum flctl_ecc_res_t ecc_result; |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 418 | |
Bastian Hecht | 623c55c | 2012-05-14 14:14:45 +0200 | [diff] [blame] | 419 | page_sectors = flctl->page_size ? 4 : 1; |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 420 | |
| 421 | set_cmd_regs(mtd, NAND_CMD_READ0, |
| 422 | (NAND_CMD_READSTART << 8) | NAND_CMD_READ0); |
| 423 | |
Bastian Hecht | 623c55c | 2012-05-14 14:14:45 +0200 | [diff] [blame] | 424 | writel(readl(FLCMNCR(flctl)) | ACM_SACCES_MODE | _4ECCCORRECT, |
| 425 | FLCMNCR(flctl)); |
| 426 | writel(readl(FLCMDCR(flctl)) | page_sectors, FLCMDCR(flctl)); |
| 427 | writel(page_addr << 2, FLADR(flctl)); |
| 428 | |
| 429 | empty_fifo(flctl); |
| 430 | start_translation(flctl); |
| 431 | |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 432 | for (sector = 0; sector < page_sectors; sector++) { |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 433 | read_fiforeg(flctl, 512, 512 * sector); |
| 434 | |
Bastian Hecht | 6667a6d5 | 2012-05-14 14:14:46 +0200 | [diff] [blame] | 435 | ecc_result = read_ecfiforeg(flctl, |
Yoshihiro Shimoda | c0e6616 | 2009-03-24 18:27:24 +0900 | [diff] [blame] | 436 | &flctl->done_buff[mtd->writesize + 16 * sector], |
| 437 | sector); |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 438 | |
Bastian Hecht | 6667a6d5 | 2012-05-14 14:14:46 +0200 | [diff] [blame] | 439 | switch (ecc_result) { |
| 440 | case FL_REPAIRABLE: |
| 441 | dev_info(&flctl->pdev->dev, |
| 442 | "applied ecc on page 0x%x", page_addr); |
| 443 | flctl->mtd.ecc_stats.corrected++; |
| 444 | break; |
| 445 | case FL_ERROR: |
| 446 | dev_warn(&flctl->pdev->dev, |
| 447 | "page 0x%x contains corrupted data\n", |
| 448 | page_addr); |
| 449 | flctl->mtd.ecc_stats.failed++; |
| 450 | break; |
| 451 | default: |
| 452 | ; |
| 453 | } |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 454 | } |
Bastian Hecht | 623c55c | 2012-05-14 14:14:45 +0200 | [diff] [blame] | 455 | |
| 456 | wait_completion(flctl); |
| 457 | |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 458 | writel(readl(FLCMNCR(flctl)) & ~(ACM_SACCES_MODE | _4ECCCORRECT), |
| 459 | FLCMNCR(flctl)); |
| 460 | } |
| 461 | |
| 462 | static void execmd_read_oob(struct mtd_info *mtd, int page_addr) |
| 463 | { |
| 464 | struct sh_flctl *flctl = mtd_to_flctl(mtd); |
Bastian Hecht | ef4ce0b | 2012-05-14 14:14:43 +0200 | [diff] [blame] | 465 | int page_sectors = flctl->page_size ? 4 : 1; |
| 466 | int i; |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 467 | |
| 468 | set_cmd_regs(mtd, NAND_CMD_READ0, |
| 469 | (NAND_CMD_READSTART << 8) | NAND_CMD_READ0); |
| 470 | |
| 471 | empty_fifo(flctl); |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 472 | |
Bastian Hecht | ef4ce0b | 2012-05-14 14:14:43 +0200 | [diff] [blame] | 473 | for (i = 0; i < page_sectors; i++) { |
| 474 | set_addr(mtd, (512 + 16) * i + 512 , page_addr); |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 475 | writel(16, FLDTCNTR(flctl)); |
| 476 | |
| 477 | start_translation(flctl); |
Bastian Hecht | ef4ce0b | 2012-05-14 14:14:43 +0200 | [diff] [blame] | 478 | read_fiforeg(flctl, 16, 16 * i); |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 479 | wait_completion(flctl); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | static void execmd_write_page_sector(struct mtd_info *mtd) |
| 484 | { |
| 485 | struct sh_flctl *flctl = mtd_to_flctl(mtd); |
Bastian Hecht | 3166df0 | 2012-05-14 14:14:47 +0200 | [diff] [blame] | 486 | int page_addr = flctl->seqin_page_addr; |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 487 | int sector, page_sectors; |
| 488 | |
Bastian Hecht | 623c55c | 2012-05-14 14:14:45 +0200 | [diff] [blame] | 489 | page_sectors = flctl->page_size ? 4 : 1; |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 490 | |
| 491 | set_cmd_regs(mtd, NAND_CMD_PAGEPROG, |
| 492 | (NAND_CMD_PAGEPROG << 8) | NAND_CMD_SEQIN); |
| 493 | |
Bastian Hecht | 623c55c | 2012-05-14 14:14:45 +0200 | [diff] [blame] | 494 | empty_fifo(flctl); |
| 495 | writel(readl(FLCMNCR(flctl)) | ACM_SACCES_MODE, FLCMNCR(flctl)); |
| 496 | writel(readl(FLCMDCR(flctl)) | page_sectors, FLCMDCR(flctl)); |
| 497 | writel(page_addr << 2, FLADR(flctl)); |
| 498 | start_translation(flctl); |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 499 | |
Bastian Hecht | 623c55c | 2012-05-14 14:14:45 +0200 | [diff] [blame] | 500 | for (sector = 0; sector < page_sectors; sector++) { |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 501 | write_fiforeg(flctl, 512, 512 * sector); |
Bastian Hecht | 3166df0 | 2012-05-14 14:14:47 +0200 | [diff] [blame] | 502 | write_ec_fiforeg(flctl, 16, mtd->writesize + 16 * sector); |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 503 | } |
| 504 | |
Bastian Hecht | 623c55c | 2012-05-14 14:14:45 +0200 | [diff] [blame] | 505 | wait_completion(flctl); |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 506 | writel(readl(FLCMNCR(flctl)) & ~ACM_SACCES_MODE, FLCMNCR(flctl)); |
| 507 | } |
| 508 | |
| 509 | static void execmd_write_oob(struct mtd_info *mtd) |
| 510 | { |
| 511 | struct sh_flctl *flctl = mtd_to_flctl(mtd); |
| 512 | int page_addr = flctl->seqin_page_addr; |
| 513 | int sector, page_sectors; |
| 514 | |
Bastian Hecht | ef4ce0b | 2012-05-14 14:14:43 +0200 | [diff] [blame] | 515 | page_sectors = flctl->page_size ? 4 : 1; |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 516 | |
| 517 | set_cmd_regs(mtd, NAND_CMD_PAGEPROG, |
| 518 | (NAND_CMD_PAGEPROG << 8) | NAND_CMD_SEQIN); |
| 519 | |
Bastian Hecht | ef4ce0b | 2012-05-14 14:14:43 +0200 | [diff] [blame] | 520 | for (sector = 0; sector < page_sectors; sector++) { |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 521 | empty_fifo(flctl); |
| 522 | set_addr(mtd, sector * 528 + 512, page_addr); |
| 523 | writel(16, FLDTCNTR(flctl)); /* set read size */ |
| 524 | |
| 525 | start_translation(flctl); |
| 526 | write_fiforeg(flctl, 16, 16 * sector); |
| 527 | wait_completion(flctl); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | static void flctl_cmdfunc(struct mtd_info *mtd, unsigned int command, |
| 532 | int column, int page_addr) |
| 533 | { |
| 534 | struct sh_flctl *flctl = mtd_to_flctl(mtd); |
| 535 | uint32_t read_cmd = 0; |
| 536 | |
Bastian Hecht | cfe7819 | 2012-03-18 15:13:20 +0100 | [diff] [blame] | 537 | pm_runtime_get_sync(&flctl->pdev->dev); |
| 538 | |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 539 | flctl->read_bytes = 0; |
| 540 | if (command != NAND_CMD_PAGEPROG) |
| 541 | flctl->index = 0; |
| 542 | |
| 543 | switch (command) { |
| 544 | case NAND_CMD_READ1: |
| 545 | case NAND_CMD_READ0: |
| 546 | if (flctl->hwecc) { |
| 547 | /* read page with hwecc */ |
| 548 | execmd_read_page_sector(mtd, page_addr); |
| 549 | break; |
| 550 | } |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 551 | if (flctl->page_size) |
| 552 | set_cmd_regs(mtd, command, (NAND_CMD_READSTART << 8) |
| 553 | | command); |
| 554 | else |
| 555 | set_cmd_regs(mtd, command, command); |
| 556 | |
| 557 | set_addr(mtd, 0, page_addr); |
| 558 | |
| 559 | flctl->read_bytes = mtd->writesize + mtd->oobsize; |
Magnus Damm | 010ab82 | 2010-01-27 09:17:21 +0000 | [diff] [blame] | 560 | if (flctl->chip.options & NAND_BUSWIDTH_16) |
| 561 | column >>= 1; |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 562 | flctl->index += column; |
| 563 | goto read_normal_exit; |
| 564 | |
| 565 | case NAND_CMD_READOOB: |
| 566 | if (flctl->hwecc) { |
| 567 | /* read page with hwecc */ |
| 568 | execmd_read_oob(mtd, page_addr); |
| 569 | break; |
| 570 | } |
| 571 | |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 572 | if (flctl->page_size) { |
| 573 | set_cmd_regs(mtd, command, (NAND_CMD_READSTART << 8) |
| 574 | | NAND_CMD_READ0); |
| 575 | set_addr(mtd, mtd->writesize, page_addr); |
| 576 | } else { |
| 577 | set_cmd_regs(mtd, command, command); |
| 578 | set_addr(mtd, 0, page_addr); |
| 579 | } |
| 580 | flctl->read_bytes = mtd->oobsize; |
| 581 | goto read_normal_exit; |
| 582 | |
Bastian Hecht | dd5ab24 | 2012-03-01 10:48:38 +0100 | [diff] [blame] | 583 | case NAND_CMD_RNDOUT: |
| 584 | if (flctl->hwecc) |
| 585 | break; |
| 586 | |
| 587 | if (flctl->page_size) |
| 588 | set_cmd_regs(mtd, command, (NAND_CMD_RNDOUTSTART << 8) |
| 589 | | command); |
| 590 | else |
| 591 | set_cmd_regs(mtd, command, command); |
| 592 | |
| 593 | set_addr(mtd, column, 0); |
| 594 | |
| 595 | flctl->read_bytes = mtd->writesize + mtd->oobsize - column; |
| 596 | goto read_normal_exit; |
| 597 | |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 598 | case NAND_CMD_READID: |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 599 | set_cmd_regs(mtd, command, command); |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 600 | |
Bastian Hecht | 7b6b230 | 2012-03-01 10:48:37 +0100 | [diff] [blame] | 601 | /* READID is always performed using an 8-bit bus */ |
| 602 | if (flctl->chip.options & NAND_BUSWIDTH_16) |
| 603 | column <<= 1; |
| 604 | set_addr(mtd, column, 0); |
| 605 | |
| 606 | flctl->read_bytes = 8; |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 607 | writel(flctl->read_bytes, FLDTCNTR(flctl)); /* set read size */ |
Bastian Hecht | abb59ef | 2012-03-01 10:48:36 +0100 | [diff] [blame] | 608 | empty_fifo(flctl); |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 609 | start_translation(flctl); |
Bastian Hecht | 7b6b230 | 2012-03-01 10:48:37 +0100 | [diff] [blame] | 610 | read_fiforeg(flctl, flctl->read_bytes, 0); |
| 611 | wait_completion(flctl); |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 612 | break; |
| 613 | |
| 614 | case NAND_CMD_ERASE1: |
| 615 | flctl->erase1_page_addr = page_addr; |
| 616 | break; |
| 617 | |
| 618 | case NAND_CMD_ERASE2: |
| 619 | set_cmd_regs(mtd, NAND_CMD_ERASE1, |
| 620 | (command << 8) | NAND_CMD_ERASE1); |
| 621 | set_addr(mtd, -1, flctl->erase1_page_addr); |
| 622 | start_translation(flctl); |
| 623 | wait_completion(flctl); |
| 624 | break; |
| 625 | |
| 626 | case NAND_CMD_SEQIN: |
| 627 | if (!flctl->page_size) { |
| 628 | /* output read command */ |
| 629 | if (column >= mtd->writesize) { |
| 630 | column -= mtd->writesize; |
| 631 | read_cmd = NAND_CMD_READOOB; |
| 632 | } else if (column < 256) { |
| 633 | read_cmd = NAND_CMD_READ0; |
| 634 | } else { |
| 635 | column -= 256; |
| 636 | read_cmd = NAND_CMD_READ1; |
| 637 | } |
| 638 | } |
| 639 | flctl->seqin_column = column; |
| 640 | flctl->seqin_page_addr = page_addr; |
| 641 | flctl->seqin_read_cmd = read_cmd; |
| 642 | break; |
| 643 | |
| 644 | case NAND_CMD_PAGEPROG: |
| 645 | empty_fifo(flctl); |
| 646 | if (!flctl->page_size) { |
| 647 | set_cmd_regs(mtd, NAND_CMD_SEQIN, |
| 648 | flctl->seqin_read_cmd); |
| 649 | set_addr(mtd, -1, -1); |
| 650 | writel(0, FLDTCNTR(flctl)); /* set 0 size */ |
| 651 | start_translation(flctl); |
| 652 | wait_completion(flctl); |
| 653 | } |
| 654 | if (flctl->hwecc) { |
| 655 | /* write page with hwecc */ |
| 656 | if (flctl->seqin_column == mtd->writesize) |
| 657 | execmd_write_oob(mtd); |
| 658 | else if (!flctl->seqin_column) |
| 659 | execmd_write_page_sector(mtd); |
| 660 | else |
| 661 | printk(KERN_ERR "Invalid address !?\n"); |
| 662 | break; |
| 663 | } |
| 664 | set_cmd_regs(mtd, command, (command << 8) | NAND_CMD_SEQIN); |
| 665 | set_addr(mtd, flctl->seqin_column, flctl->seqin_page_addr); |
| 666 | writel(flctl->index, FLDTCNTR(flctl)); /* set write size */ |
| 667 | start_translation(flctl); |
| 668 | write_fiforeg(flctl, flctl->index, 0); |
| 669 | wait_completion(flctl); |
| 670 | break; |
| 671 | |
| 672 | case NAND_CMD_STATUS: |
| 673 | set_cmd_regs(mtd, command, command); |
| 674 | set_addr(mtd, -1, -1); |
| 675 | |
| 676 | flctl->read_bytes = 1; |
| 677 | writel(flctl->read_bytes, FLDTCNTR(flctl)); /* set read size */ |
| 678 | start_translation(flctl); |
| 679 | read_datareg(flctl, 0); /* read and end */ |
| 680 | break; |
| 681 | |
| 682 | case NAND_CMD_RESET: |
| 683 | set_cmd_regs(mtd, command, command); |
| 684 | set_addr(mtd, -1, -1); |
| 685 | |
| 686 | writel(0, FLDTCNTR(flctl)); /* set 0 size */ |
| 687 | start_translation(flctl); |
| 688 | wait_completion(flctl); |
| 689 | break; |
| 690 | |
| 691 | default: |
| 692 | break; |
| 693 | } |
Bastian Hecht | cfe7819 | 2012-03-18 15:13:20 +0100 | [diff] [blame] | 694 | goto runtime_exit; |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 695 | |
| 696 | read_normal_exit: |
| 697 | writel(flctl->read_bytes, FLDTCNTR(flctl)); /* set read size */ |
Bastian Hecht | abb59ef | 2012-03-01 10:48:36 +0100 | [diff] [blame] | 698 | empty_fifo(flctl); |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 699 | start_translation(flctl); |
| 700 | read_fiforeg(flctl, flctl->read_bytes, 0); |
| 701 | wait_completion(flctl); |
Bastian Hecht | cfe7819 | 2012-03-18 15:13:20 +0100 | [diff] [blame] | 702 | runtime_exit: |
| 703 | pm_runtime_put_sync(&flctl->pdev->dev); |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 704 | return; |
| 705 | } |
| 706 | |
| 707 | static void flctl_select_chip(struct mtd_info *mtd, int chipnr) |
| 708 | { |
| 709 | struct sh_flctl *flctl = mtd_to_flctl(mtd); |
Bastian Hecht | cfe7819 | 2012-03-18 15:13:20 +0100 | [diff] [blame] | 710 | int ret; |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 711 | |
| 712 | switch (chipnr) { |
| 713 | case -1: |
Bastian Hecht | 0b3f0d1 | 2012-03-01 10:48:39 +0100 | [diff] [blame] | 714 | flctl->flcmncr_base &= ~CE0_ENABLE; |
Bastian Hecht | cfe7819 | 2012-03-18 15:13:20 +0100 | [diff] [blame] | 715 | |
| 716 | pm_runtime_get_sync(&flctl->pdev->dev); |
Bastian Hecht | 0b3f0d1 | 2012-03-01 10:48:39 +0100 | [diff] [blame] | 717 | writel(flctl->flcmncr_base, FLCMNCR(flctl)); |
Bastian Hecht | cfe7819 | 2012-03-18 15:13:20 +0100 | [diff] [blame] | 718 | |
| 719 | if (flctl->qos_request) { |
| 720 | dev_pm_qos_remove_request(&flctl->pm_qos); |
| 721 | flctl->qos_request = 0; |
| 722 | } |
| 723 | |
| 724 | pm_runtime_put_sync(&flctl->pdev->dev); |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 725 | break; |
| 726 | case 0: |
Bastian Hecht | 0b3f0d1 | 2012-03-01 10:48:39 +0100 | [diff] [blame] | 727 | flctl->flcmncr_base |= CE0_ENABLE; |
Bastian Hecht | cfe7819 | 2012-03-18 15:13:20 +0100 | [diff] [blame] | 728 | |
| 729 | if (!flctl->qos_request) { |
| 730 | ret = dev_pm_qos_add_request(&flctl->pdev->dev, |
| 731 | &flctl->pm_qos, 100); |
| 732 | if (ret < 0) |
| 733 | dev_err(&flctl->pdev->dev, |
| 734 | "PM QoS request failed: %d\n", ret); |
| 735 | flctl->qos_request = 1; |
| 736 | } |
| 737 | |
| 738 | if (flctl->holden) { |
| 739 | pm_runtime_get_sync(&flctl->pdev->dev); |
Bastian Hecht | 3f2e924 | 2012-03-01 10:48:40 +0100 | [diff] [blame] | 740 | writel(HOLDEN, FLHOLDCR(flctl)); |
Bastian Hecht | cfe7819 | 2012-03-18 15:13:20 +0100 | [diff] [blame] | 741 | pm_runtime_put_sync(&flctl->pdev->dev); |
| 742 | } |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 743 | break; |
| 744 | default: |
| 745 | BUG(); |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | static void flctl_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len) |
| 750 | { |
| 751 | struct sh_flctl *flctl = mtd_to_flctl(mtd); |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 752 | |
Bastian Hecht | e8a9d8f | 2012-10-19 12:15:34 +0200 | [diff] [blame^] | 753 | memcpy(&flctl->done_buff[flctl->index], buf, len); |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 754 | flctl->index += len; |
| 755 | } |
| 756 | |
| 757 | static uint8_t flctl_read_byte(struct mtd_info *mtd) |
| 758 | { |
| 759 | struct sh_flctl *flctl = mtd_to_flctl(mtd); |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 760 | uint8_t data; |
| 761 | |
Bastian Hecht | e8a9d8f | 2012-10-19 12:15:34 +0200 | [diff] [blame^] | 762 | data = flctl->done_buff[flctl->index]; |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 763 | flctl->index++; |
| 764 | return data; |
| 765 | } |
| 766 | |
Magnus Damm | 010ab82 | 2010-01-27 09:17:21 +0000 | [diff] [blame] | 767 | static uint16_t flctl_read_word(struct mtd_info *mtd) |
| 768 | { |
Bastian Hecht | e8a9d8f | 2012-10-19 12:15:34 +0200 | [diff] [blame^] | 769 | struct sh_flctl *flctl = mtd_to_flctl(mtd); |
| 770 | uint16_t *buf = (uint16_t *)&flctl->done_buff[flctl->index]; |
Magnus Damm | 010ab82 | 2010-01-27 09:17:21 +0000 | [diff] [blame] | 771 | |
Bastian Hecht | e8a9d8f | 2012-10-19 12:15:34 +0200 | [diff] [blame^] | 772 | flctl->index += 2; |
| 773 | return *buf; |
Magnus Damm | 010ab82 | 2010-01-27 09:17:21 +0000 | [diff] [blame] | 774 | } |
| 775 | |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 776 | static void flctl_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) |
| 777 | { |
Bastian Hecht | d76236f | 2012-07-05 12:41:01 +0200 | [diff] [blame] | 778 | struct sh_flctl *flctl = mtd_to_flctl(mtd); |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 779 | |
Bastian Hecht | e8a9d8f | 2012-10-19 12:15:34 +0200 | [diff] [blame^] | 780 | memcpy(buf, &flctl->done_buff[flctl->index], len); |
Bastian Hecht | d76236f | 2012-07-05 12:41:01 +0200 | [diff] [blame] | 781 | flctl->index += len; |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 782 | } |
| 783 | |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 784 | static int flctl_chip_init_tail(struct mtd_info *mtd) |
| 785 | { |
| 786 | struct sh_flctl *flctl = mtd_to_flctl(mtd); |
| 787 | struct nand_chip *chip = &flctl->chip; |
| 788 | |
| 789 | if (mtd->writesize == 512) { |
| 790 | flctl->page_size = 0; |
| 791 | if (chip->chipsize > (32 << 20)) { |
| 792 | /* big than 32MB */ |
| 793 | flctl->rw_ADRCNT = ADRCNT_4; |
| 794 | flctl->erase_ADRCNT = ADRCNT_3; |
| 795 | } else if (chip->chipsize > (2 << 16)) { |
| 796 | /* big than 128KB */ |
| 797 | flctl->rw_ADRCNT = ADRCNT_3; |
| 798 | flctl->erase_ADRCNT = ADRCNT_2; |
| 799 | } else { |
| 800 | flctl->rw_ADRCNT = ADRCNT_2; |
| 801 | flctl->erase_ADRCNT = ADRCNT_1; |
| 802 | } |
| 803 | } else { |
| 804 | flctl->page_size = 1; |
| 805 | if (chip->chipsize > (128 << 20)) { |
| 806 | /* big than 128MB */ |
| 807 | flctl->rw_ADRCNT = ADRCNT2_E; |
| 808 | flctl->erase_ADRCNT = ADRCNT_3; |
| 809 | } else if (chip->chipsize > (8 << 16)) { |
| 810 | /* big than 512KB */ |
| 811 | flctl->rw_ADRCNT = ADRCNT_4; |
| 812 | flctl->erase_ADRCNT = ADRCNT_2; |
| 813 | } else { |
| 814 | flctl->rw_ADRCNT = ADRCNT_3; |
| 815 | flctl->erase_ADRCNT = ADRCNT_1; |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | if (flctl->hwecc) { |
| 820 | if (mtd->writesize == 512) { |
| 821 | chip->ecc.layout = &flctl_4secc_oob_16; |
| 822 | chip->badblock_pattern = &flctl_4secc_smallpage; |
| 823 | } else { |
| 824 | chip->ecc.layout = &flctl_4secc_oob_64; |
| 825 | chip->badblock_pattern = &flctl_4secc_largepage; |
| 826 | } |
| 827 | |
| 828 | chip->ecc.size = 512; |
| 829 | chip->ecc.bytes = 10; |
Mike Dunn | 6a918ba | 2012-03-11 14:21:11 -0700 | [diff] [blame] | 830 | chip->ecc.strength = 4; |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 831 | chip->ecc.read_page = flctl_read_page_hwecc; |
| 832 | chip->ecc.write_page = flctl_write_page_hwecc; |
| 833 | chip->ecc.mode = NAND_ECC_HW; |
| 834 | |
| 835 | /* 4 symbols ECC enabled */ |
Bastian Hecht | aa32d1f | 2012-05-14 14:14:42 +0200 | [diff] [blame] | 836 | flctl->flcmncr_base |= _4ECCEN; |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 837 | } else { |
| 838 | chip->ecc.mode = NAND_ECC_SOFT; |
| 839 | } |
| 840 | |
| 841 | return 0; |
| 842 | } |
| 843 | |
Bastian Hecht | 3c7ea4e | 2012-05-14 14:14:41 +0200 | [diff] [blame] | 844 | static irqreturn_t flctl_handle_flste(int irq, void *dev_id) |
| 845 | { |
| 846 | struct sh_flctl *flctl = dev_id; |
| 847 | |
| 848 | dev_err(&flctl->pdev->dev, "flste irq: %x\n", readl(FLINTDMACR(flctl))); |
| 849 | writel(flctl->flintdmacr_base, FLINTDMACR(flctl)); |
| 850 | |
| 851 | return IRQ_HANDLED; |
| 852 | } |
| 853 | |
Magnus Damm | b79c7ad | 2010-02-02 13:01:25 +0900 | [diff] [blame] | 854 | static int __devinit flctl_probe(struct platform_device *pdev) |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 855 | { |
| 856 | struct resource *res; |
| 857 | struct sh_flctl *flctl; |
| 858 | struct mtd_info *flctl_mtd; |
| 859 | struct nand_chip *nand; |
| 860 | struct sh_flctl_platform_data *pdata; |
Magnus Damm | b79c7ad | 2010-02-02 13:01:25 +0900 | [diff] [blame] | 861 | int ret = -ENXIO; |
Bastian Hecht | 3c7ea4e | 2012-05-14 14:14:41 +0200 | [diff] [blame] | 862 | int irq; |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 863 | |
| 864 | pdata = pdev->dev.platform_data; |
| 865 | if (pdata == NULL) { |
Magnus Damm | b79c7ad | 2010-02-02 13:01:25 +0900 | [diff] [blame] | 866 | dev_err(&pdev->dev, "no platform data defined\n"); |
| 867 | return -EINVAL; |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | flctl = kzalloc(sizeof(struct sh_flctl), GFP_KERNEL); |
| 871 | if (!flctl) { |
Magnus Damm | b79c7ad | 2010-02-02 13:01:25 +0900 | [diff] [blame] | 872 | dev_err(&pdev->dev, "failed to allocate driver data\n"); |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 873 | return -ENOMEM; |
| 874 | } |
| 875 | |
| 876 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 877 | if (!res) { |
Magnus Damm | b79c7ad | 2010-02-02 13:01:25 +0900 | [diff] [blame] | 878 | dev_err(&pdev->dev, "failed to get I/O memory\n"); |
Bastian Hecht | cfe7819 | 2012-03-18 15:13:20 +0100 | [diff] [blame] | 879 | goto err_iomap; |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 880 | } |
| 881 | |
H Hartley Sweeten | cbd38a8 | 2009-12-14 16:59:27 -0500 | [diff] [blame] | 882 | flctl->reg = ioremap(res->start, resource_size(res)); |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 883 | if (flctl->reg == NULL) { |
Magnus Damm | b79c7ad | 2010-02-02 13:01:25 +0900 | [diff] [blame] | 884 | dev_err(&pdev->dev, "failed to remap I/O memory\n"); |
Bastian Hecht | cfe7819 | 2012-03-18 15:13:20 +0100 | [diff] [blame] | 885 | goto err_iomap; |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 886 | } |
| 887 | |
Bastian Hecht | 3c7ea4e | 2012-05-14 14:14:41 +0200 | [diff] [blame] | 888 | irq = platform_get_irq(pdev, 0); |
| 889 | if (irq < 0) { |
| 890 | dev_err(&pdev->dev, "failed to get flste irq data\n"); |
| 891 | goto err_flste; |
| 892 | } |
| 893 | |
| 894 | ret = request_irq(irq, flctl_handle_flste, IRQF_SHARED, "flste", flctl); |
| 895 | if (ret) { |
| 896 | dev_err(&pdev->dev, "request interrupt failed.\n"); |
| 897 | goto err_flste; |
| 898 | } |
| 899 | |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 900 | platform_set_drvdata(pdev, flctl); |
| 901 | flctl_mtd = &flctl->mtd; |
| 902 | nand = &flctl->chip; |
| 903 | flctl_mtd->priv = nand; |
Magnus Damm | b79c7ad | 2010-02-02 13:01:25 +0900 | [diff] [blame] | 904 | flctl->pdev = pdev; |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 905 | flctl->hwecc = pdata->has_hwecc; |
Bastian Hecht | 3f2e924 | 2012-03-01 10:48:40 +0100 | [diff] [blame] | 906 | flctl->holden = pdata->use_holden; |
Bastian Hecht | 3c7ea4e | 2012-05-14 14:14:41 +0200 | [diff] [blame] | 907 | flctl->flcmncr_base = pdata->flcmncr_val; |
| 908 | flctl->flintdmacr_base = flctl->hwecc ? (STERINTE | ECERB) : STERINTE; |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 909 | |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 910 | /* Set address of hardware control function */ |
| 911 | /* 20 us command delay time */ |
| 912 | nand->chip_delay = 20; |
| 913 | |
| 914 | nand->read_byte = flctl_read_byte; |
| 915 | nand->write_buf = flctl_write_buf; |
| 916 | nand->read_buf = flctl_read_buf; |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 917 | nand->select_chip = flctl_select_chip; |
| 918 | nand->cmdfunc = flctl_cmdfunc; |
| 919 | |
Magnus Damm | 010ab82 | 2010-01-27 09:17:21 +0000 | [diff] [blame] | 920 | if (pdata->flcmncr_val & SEL_16BIT) { |
| 921 | nand->options |= NAND_BUSWIDTH_16; |
| 922 | nand->read_word = flctl_read_word; |
| 923 | } |
| 924 | |
Bastian Hecht | cfe7819 | 2012-03-18 15:13:20 +0100 | [diff] [blame] | 925 | pm_runtime_enable(&pdev->dev); |
| 926 | pm_runtime_resume(&pdev->dev); |
| 927 | |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 928 | ret = nand_scan_ident(flctl_mtd, 1, NULL); |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 929 | if (ret) |
Bastian Hecht | cfe7819 | 2012-03-18 15:13:20 +0100 | [diff] [blame] | 930 | goto err_chip; |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 931 | |
| 932 | ret = flctl_chip_init_tail(flctl_mtd); |
| 933 | if (ret) |
Bastian Hecht | cfe7819 | 2012-03-18 15:13:20 +0100 | [diff] [blame] | 934 | goto err_chip; |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 935 | |
| 936 | ret = nand_scan_tail(flctl_mtd); |
| 937 | if (ret) |
Bastian Hecht | cfe7819 | 2012-03-18 15:13:20 +0100 | [diff] [blame] | 938 | goto err_chip; |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 939 | |
Jamie Iles | ee0e87b | 2011-05-23 10:23:40 +0100 | [diff] [blame] | 940 | mtd_device_register(flctl_mtd, pdata->parts, pdata->nr_parts); |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 941 | |
| 942 | return 0; |
| 943 | |
Bastian Hecht | cfe7819 | 2012-03-18 15:13:20 +0100 | [diff] [blame] | 944 | err_chip: |
| 945 | pm_runtime_disable(&pdev->dev); |
Bastian Hecht | 3c7ea4e | 2012-05-14 14:14:41 +0200 | [diff] [blame] | 946 | free_irq(irq, flctl); |
| 947 | err_flste: |
Bastian Hecht | cb54751 | 2012-05-14 14:14:40 +0200 | [diff] [blame] | 948 | iounmap(flctl->reg); |
Bastian Hecht | cfe7819 | 2012-03-18 15:13:20 +0100 | [diff] [blame] | 949 | err_iomap: |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 950 | kfree(flctl); |
| 951 | return ret; |
| 952 | } |
| 953 | |
Magnus Damm | b79c7ad | 2010-02-02 13:01:25 +0900 | [diff] [blame] | 954 | static int __devexit flctl_remove(struct platform_device *pdev) |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 955 | { |
| 956 | struct sh_flctl *flctl = platform_get_drvdata(pdev); |
| 957 | |
| 958 | nand_release(&flctl->mtd); |
Bastian Hecht | cfe7819 | 2012-03-18 15:13:20 +0100 | [diff] [blame] | 959 | pm_runtime_disable(&pdev->dev); |
Bastian Hecht | 3c7ea4e | 2012-05-14 14:14:41 +0200 | [diff] [blame] | 960 | free_irq(platform_get_irq(pdev, 0), flctl); |
Bastian Hecht | cb54751 | 2012-05-14 14:14:40 +0200 | [diff] [blame] | 961 | iounmap(flctl->reg); |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 962 | kfree(flctl); |
| 963 | |
| 964 | return 0; |
| 965 | } |
| 966 | |
| 967 | static struct platform_driver flctl_driver = { |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 968 | .remove = flctl_remove, |
| 969 | .driver = { |
| 970 | .name = "sh_flctl", |
| 971 | .owner = THIS_MODULE, |
| 972 | }, |
| 973 | }; |
| 974 | |
| 975 | static int __init flctl_nand_init(void) |
| 976 | { |
David Woodhouse | 894572a | 2009-09-19 16:07:34 -0700 | [diff] [blame] | 977 | return platform_driver_probe(&flctl_driver, flctl_probe); |
Yoshihiro Shimoda | 35a3479 | 2008-10-20 17:17:44 +0900 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | static void __exit flctl_nand_cleanup(void) |
| 981 | { |
| 982 | platform_driver_unregister(&flctl_driver); |
| 983 | } |
| 984 | |
| 985 | module_init(flctl_nand_init); |
| 986 | module_exit(flctl_nand_cleanup); |
| 987 | |
| 988 | MODULE_LICENSE("GPL"); |
| 989 | MODULE_AUTHOR("Yoshihiro Shimoda"); |
| 990 | MODULE_DESCRIPTION("SuperH FLCTL driver"); |
| 991 | MODULE_ALIAS("platform:sh_flctl"); |