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