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