blob: 1343315b37bad2027c3db52d94eb7ec2a716612f [file] [log] [blame]
Yoshihiro Shimoda6028aa02008-10-14 21:23:26 +09001/*
2 * SuperH FLCTL nand controller
3 *
Magnus Dammb79c7ad2010-02-02 13:01:25 +09004 * Copyright (c) 2008 Renesas Solutions Corp.
5 * Copyright (c) 2008 Atom Create Engineering Co., Ltd.
Yoshihiro Shimoda6028aa02008-10-14 21:23:26 +09006 *
Magnus Dammb79c7ad2010-02-02 13:01:25 +09007 * Based on fsl_elbc_nand.c, Copyright (c) 2006-2007 Freescale Semiconductor
Yoshihiro Shimoda6028aa02008-10-14 21:23:26 +09008 *
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 Hecht3c7ea4e2012-05-14 14:14:41 +020027#include <linux/interrupt.h>
Yoshihiro Shimoda6028aa02008-10-14 21:23:26 +090028#include <linux/io.h>
29#include <linux/platform_device.h>
Bastian Hechtcfe78192012-03-18 15:13:20 +010030#include <linux/pm_runtime.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Bastian Hechtd76236f2012-07-05 12:41:01 +020032#include <linux/string.h>
Yoshihiro Shimoda6028aa02008-10-14 21:23:26 +090033
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
39static 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
47static struct nand_ecclayout flctl_4secc_oob_64 = {
Bastian Hechtaa32d1f2012-05-14 14:14:42 +020048 .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 Shimoda6028aa02008-10-14 21:23:26 +090054 .oobfree = {
Bastian Hechtaa32d1f2012-05-14 14:14:42 +020055 {.offset = 2, .length = 4},
56 {.offset = 16, .length = 6},
57 {.offset = 32, .length = 6},
58 {.offset = 48, .length = 6} },
Yoshihiro Shimoda6028aa02008-10-14 21:23:26 +090059};
60
61static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
62
63static 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
70static struct nand_bbt_descr flctl_4secc_largepage = {
Yoshihiro Shimodac0e66162009-03-24 18:27:24 +090071 .options = NAND_BBT_SCAN2NDPAGE,
Bastian Hechtaa32d1f2012-05-14 14:14:42 +020072 .offs = 0,
Yoshihiro Shimoda6028aa02008-10-14 21:23:26 +090073 .len = 2,
74 .pattern = scan_ff_pattern,
75};
76
77static void empty_fifo(struct sh_flctl *flctl)
78{
Bastian Hecht3c7ea4e2012-05-14 14:14:41 +020079 writel(flctl->flintdmacr_base | AC1CLR | AC0CLR, FLINTDMACR(flctl));
80 writel(flctl->flintdmacr_base, FLINTDMACR(flctl));
Yoshihiro Shimoda6028aa02008-10-14 21:23:26 +090081}
82
83static void start_translation(struct sh_flctl *flctl)
84{
85 writeb(TRSTRT, FLTRCR(flctl));
86}
87
Magnus Dammb79c7ad2010-02-02 13:01:25 +090088static void timeout_error(struct sh_flctl *flctl, const char *str)
89{
Lucas De Marchi25985ed2011-03-30 22:57:33 -030090 dev_err(&flctl->pdev->dev, "Timeout occurred in %s\n", str);
Magnus Dammb79c7ad2010-02-02 13:01:25 +090091}
92
Yoshihiro Shimoda6028aa02008-10-14 21:23:26 +090093static 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 Dammb79c7ad2010-02-02 13:01:25 +0900105 timeout_error(flctl, __func__);
Yoshihiro Shimoda6028aa02008-10-14 21:23:26 +0900106 writeb(0x0, FLTRCR(flctl));
107}
108
109static 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 Damm010ab822010-01-27 09:17:21 +0000118 if (flctl->chip.options & NAND_BUSWIDTH_16)
119 column >>= 1;
Yoshihiro Shimoda6028aa02008-10-14 21:23:26 +0900120 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
140static 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 Dammb79c7ad2010-02-02 13:01:25 +0900152 timeout_error(flctl, __func__);
Yoshihiro Shimoda6028aa02008-10-14 21:23:26 +0900153}
154
155static 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 Dammb79c7ad2010-02-02 13:01:25 +0900166 timeout_error(flctl, __func__);
Yoshihiro Shimoda6028aa02008-10-14 21:23:26 +0900167}
168
Bastian Hecht6667a6d52012-05-14 14:14:46 +0200169static enum flctl_ecc_res_t wait_recfifo_ready
170 (struct sh_flctl *flctl, int sector_number)
Yoshihiro Shimoda6028aa02008-10-14 21:23:26 +0900171{
172 uint32_t timeout = LOOP_TIMEOUT_MAX;
Yoshihiro Shimoda6028aa02008-10-14 21:23:26 +0900173 void __iomem *ecc_reg[4];
174 int i;
Bastian Hecht6667a6d52012-05-14 14:14:46 +0200175 int state = FL_SUCCESS;
Yoshihiro Shimoda6028aa02008-10-14 21:23:26 +0900176 uint32_t data, size;
177
Bastian Hecht6667a6d52012-05-14 14:14:46 +0200178 /*
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 Shimoda6028aa02008-10-14 21:23:26 +0900185 while (timeout--) {
Bastian Hecht6667a6d52012-05-14 14:14:46 +0200186 /* check if all is ok and we can read out the OOB */
Yoshihiro Shimoda6028aa02008-10-14 21:23:26 +0900187 size = readl(FLDTCNTR(flctl)) >> 24;
Bastian Hecht6667a6d52012-05-14 14:14:46 +0200188 if ((size & 0xFF) == 4)
189 return state;
Yoshihiro Shimoda6028aa02008-10-14 21:23:26 +0900190
Bastian Hecht6667a6d52012-05-14 14:14:46 +0200191 /* 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 Shimoda6028aa02008-10-14 21:23:26 +0900198 continue;
Bastian Hecht6667a6d52012-05-14 14:14:46 +0200199 }
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 Shimoda6028aa02008-10-14 21:23:26 +0900219
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 Hecht6667a6d52012-05-14 14:14:46 +0200227 uint8_t org;
228 int index;
229
Yoshihiro Shimoda6028aa02008-10-14 21:23:26 +0900230 data = readl(ecc_reg[i]);
Yoshihiro Shimoda6028aa02008-10-14 21:23:26 +0900231
Bastian Hecht6667a6d52012-05-14 14:14:46 +0200232 if (flctl->page_size)
233 index = (512 * sector_number) +
234 (data >> 16);
235 else
236 index = data >> 16;
Yoshihiro Shimodac0e66162009-03-24 18:27:24 +0900237
Bastian Hecht6667a6d52012-05-14 14:14:46 +0200238 org = flctl->done_buff[index];
239 flctl->done_buff[index] = org ^ (data & 0xFF);
Yoshihiro Shimoda6028aa02008-10-14 21:23:26 +0900240 }
Bastian Hecht6667a6d52012-05-14 14:14:46 +0200241 state = FL_REPAIRABLE;
Yoshihiro Shimoda6028aa02008-10-14 21:23:26 +0900242 writel(0, FL4ECCCR(flctl));
243 }
244
Magnus Dammb79c7ad2010-02-02 13:01:25 +0900245 timeout_error(flctl, __func__);
Bastian Hecht6667a6d52012-05-14 14:14:46 +0200246 return FL_TIMEOUT; /* timeout */
Yoshihiro Shimoda6028aa02008-10-14 21:23:26 +0900247}
248
249static 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 Dammb79c7ad2010-02-02 13:01:25 +0900261 timeout_error(flctl, __func__);
Yoshihiro Shimoda6028aa02008-10-14 21:23:26 +0900262}
263
264static 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
275static 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 Shimoda6028aa02008-10-14 21:23:26 +0900279
280 len_4align = (rlen + 3) / 4;
281
282 for (i = 0; i < len_4align; i++) {
283 wait_rfifo_ready(flctl);
Bastian Hecht3166df02012-05-14 14:14:47 +0200284 buf[i] = readl(FLDTFIFO(flctl));
Yoshihiro Shimoda6028aa02008-10-14 21:23:26 +0900285 buf[i] = be32_to_cpu(buf[i]);
286 }
287}
288
Bastian Hecht6667a6d52012-05-14 14:14:46 +0200289static enum flctl_ecc_res_t read_ecfiforeg
290 (struct sh_flctl *flctl, uint8_t *buff, int sector)
Yoshihiro Shimoda6028aa02008-10-14 21:23:26 +0900291{
292 int i;
Bastian Hecht6667a6d52012-05-14 14:14:46 +0200293 enum flctl_ecc_res_t res;
Yoshihiro Shimoda6028aa02008-10-14 21:23:26 +0900294 unsigned long *ecc_buf = (unsigned long *)buff;
Yoshihiro Shimoda6028aa02008-10-14 21:23:26 +0900295
Bastian Hecht6667a6d52012-05-14 14:14:46 +0200296 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 Shimoda6028aa02008-10-14 21:23:26 +0900303 }
304
Bastian Hecht6667a6d52012-05-14 14:14:46 +0200305 return res;
Yoshihiro Shimoda6028aa02008-10-14 21:23:26 +0900306}
307
308static void write_fiforeg(struct sh_flctl *flctl, int rlen, int offset)
309{
310 int i, len_4align;
311 unsigned long *data = (unsigned long *)&flctl->done_buff[offset];
312 void *fifo_addr = (void *)FLDTFIFO(flctl);
313
314 len_4align = (rlen + 3) / 4;
315 for (i = 0; i < len_4align; i++) {
316 wait_wfifo_ready(flctl);
317 writel(cpu_to_be32(data[i]), fifo_addr);
318 }
319}
320
Bastian Hecht3166df02012-05-14 14:14:47 +0200321static void write_ec_fiforeg(struct sh_flctl *flctl, int rlen, int offset)
322{
323 int i, len_4align;
324 unsigned long *data = (unsigned long *)&flctl->done_buff[offset];
325
326 len_4align = (rlen + 3) / 4;
327 for (i = 0; i < len_4align; i++) {
328 wait_wecfifo_ready(flctl);
329 writel(cpu_to_be32(data[i]), FLECFIFO(flctl));
330 }
331}
332
Yoshihiro Shimoda6028aa02008-10-14 21:23:26 +0900333static void set_cmd_regs(struct mtd_info *mtd, uint32_t cmd, uint32_t flcmcdr_val)
334{
335 struct sh_flctl *flctl = mtd_to_flctl(mtd);
Bastian Hecht0b3f0d12012-03-01 10:48:39 +0100336 uint32_t flcmncr_val = flctl->flcmncr_base & ~SEL_16BIT;
Yoshihiro Shimoda6028aa02008-10-14 21:23:26 +0900337 uint32_t flcmdcr_val, addr_len_bytes = 0;
338
339 /* Set SNAND bit if page size is 2048byte */
340 if (flctl->page_size)
341 flcmncr_val |= SNAND_E;
342 else
343 flcmncr_val &= ~SNAND_E;
344
345 /* default FLCMDCR val */
346 flcmdcr_val = DOCMD1_E | DOADR_E;
347
348 /* Set for FLCMDCR */
349 switch (cmd) {
350 case NAND_CMD_ERASE1:
351 addr_len_bytes = flctl->erase_ADRCNT;
352 flcmdcr_val |= DOCMD2_E;
353 break;
354 case NAND_CMD_READ0:
355 case NAND_CMD_READOOB:
Bastian Hechtdd5ab242012-03-01 10:48:38 +0100356 case NAND_CMD_RNDOUT:
Yoshihiro Shimoda6028aa02008-10-14 21:23:26 +0900357 addr_len_bytes = flctl->rw_ADRCNT;
358 flcmdcr_val |= CDSRC_E;
Magnus Damm010ab822010-01-27 09:17:21 +0000359 if (flctl->chip.options & NAND_BUSWIDTH_16)
360 flcmncr_val |= SEL_16BIT;
Yoshihiro Shimoda6028aa02008-10-14 21:23:26 +0900361 break;
362 case NAND_CMD_SEQIN:
363 /* This case is that cmd is READ0 or READ1 or READ00 */
364 flcmdcr_val &= ~DOADR_E; /* ONLY execute 1st cmd */
365 break;
366 case NAND_CMD_PAGEPROG:
367 addr_len_bytes = flctl->rw_ADRCNT;
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900368 flcmdcr_val |= DOCMD2_E | CDSRC_E | SELRW;
Magnus Damm010ab822010-01-27 09:17:21 +0000369 if (flctl->chip.options & NAND_BUSWIDTH_16)
370 flcmncr_val |= SEL_16BIT;
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900371 break;
372 case NAND_CMD_READID:
373 flcmncr_val &= ~SNAND_E;
Bastian Hecht7b6b2302012-03-01 10:48:37 +0100374 flcmdcr_val |= CDSRC_E;
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900375 addr_len_bytes = ADRCNT_1;
376 break;
377 case NAND_CMD_STATUS:
378 case NAND_CMD_RESET:
379 flcmncr_val &= ~SNAND_E;
380 flcmdcr_val &= ~(DOADR_E | DOSR_E);
381 break;
382 default:
383 break;
384 }
385
386 /* Set address bytes parameter */
387 flcmdcr_val |= addr_len_bytes;
388
389 /* Now actually write */
390 writel(flcmncr_val, FLCMNCR(flctl));
391 writel(flcmdcr_val, FLCMDCR(flctl));
392 writel(flcmcdr_val, FLCMCDR(flctl));
393}
394
395static int flctl_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -0700396 uint8_t *buf, int oob_required, int page)
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900397{
Bastian Hecht50ed3992012-05-14 14:14:44 +0200398 chip->read_buf(mtd, buf, mtd->writesize);
Bastian Hecht3166df02012-05-14 14:14:47 +0200399 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900400 return 0;
401}
402
Josh Wufdbad98d2012-06-25 18:07:45 +0800403static int flctl_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
Brian Norris1fbb9382012-05-02 10:14:55 -0700404 const uint8_t *buf, int oob_required)
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900405{
Bastian Hecht50ed3992012-05-14 14:14:44 +0200406 chip->write_buf(mtd, buf, mtd->writesize);
Bastian Hecht3166df02012-05-14 14:14:47 +0200407 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +0800408 return 0;
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900409}
410
411static void execmd_read_page_sector(struct mtd_info *mtd, int page_addr)
412{
413 struct sh_flctl *flctl = mtd_to_flctl(mtd);
414 int sector, page_sectors;
Bastian Hecht6667a6d52012-05-14 14:14:46 +0200415 enum flctl_ecc_res_t ecc_result;
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900416
Bastian Hecht623c55c2012-05-14 14:14:45 +0200417 page_sectors = flctl->page_size ? 4 : 1;
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900418
419 set_cmd_regs(mtd, NAND_CMD_READ0,
420 (NAND_CMD_READSTART << 8) | NAND_CMD_READ0);
421
Bastian Hecht623c55c2012-05-14 14:14:45 +0200422 writel(readl(FLCMNCR(flctl)) | ACM_SACCES_MODE | _4ECCCORRECT,
423 FLCMNCR(flctl));
424 writel(readl(FLCMDCR(flctl)) | page_sectors, FLCMDCR(flctl));
425 writel(page_addr << 2, FLADR(flctl));
426
427 empty_fifo(flctl);
428 start_translation(flctl);
429
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900430 for (sector = 0; sector < page_sectors; sector++) {
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900431 read_fiforeg(flctl, 512, 512 * sector);
432
Bastian Hecht6667a6d52012-05-14 14:14:46 +0200433 ecc_result = read_ecfiforeg(flctl,
Yoshihiro Shimodac0e66162009-03-24 18:27:24 +0900434 &flctl->done_buff[mtd->writesize + 16 * sector],
435 sector);
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900436
Bastian Hecht6667a6d52012-05-14 14:14:46 +0200437 switch (ecc_result) {
438 case FL_REPAIRABLE:
439 dev_info(&flctl->pdev->dev,
440 "applied ecc on page 0x%x", page_addr);
441 flctl->mtd.ecc_stats.corrected++;
442 break;
443 case FL_ERROR:
444 dev_warn(&flctl->pdev->dev,
445 "page 0x%x contains corrupted data\n",
446 page_addr);
447 flctl->mtd.ecc_stats.failed++;
448 break;
449 default:
450 ;
451 }
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900452 }
Bastian Hecht623c55c2012-05-14 14:14:45 +0200453
454 wait_completion(flctl);
455
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900456 writel(readl(FLCMNCR(flctl)) & ~(ACM_SACCES_MODE | _4ECCCORRECT),
457 FLCMNCR(flctl));
458}
459
460static void execmd_read_oob(struct mtd_info *mtd, int page_addr)
461{
462 struct sh_flctl *flctl = mtd_to_flctl(mtd);
Bastian Hechtef4ce0b2012-05-14 14:14:43 +0200463 int page_sectors = flctl->page_size ? 4 : 1;
464 int i;
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900465
466 set_cmd_regs(mtd, NAND_CMD_READ0,
467 (NAND_CMD_READSTART << 8) | NAND_CMD_READ0);
468
469 empty_fifo(flctl);
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900470
Bastian Hechtef4ce0b2012-05-14 14:14:43 +0200471 for (i = 0; i < page_sectors; i++) {
472 set_addr(mtd, (512 + 16) * i + 512 , page_addr);
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900473 writel(16, FLDTCNTR(flctl));
474
475 start_translation(flctl);
Bastian Hechtef4ce0b2012-05-14 14:14:43 +0200476 read_fiforeg(flctl, 16, 16 * i);
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900477 wait_completion(flctl);
478 }
479}
480
481static void execmd_write_page_sector(struct mtd_info *mtd)
482{
483 struct sh_flctl *flctl = mtd_to_flctl(mtd);
Bastian Hecht3166df02012-05-14 14:14:47 +0200484 int page_addr = flctl->seqin_page_addr;
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900485 int sector, page_sectors;
486
Bastian Hecht623c55c2012-05-14 14:14:45 +0200487 page_sectors = flctl->page_size ? 4 : 1;
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900488
489 set_cmd_regs(mtd, NAND_CMD_PAGEPROG,
490 (NAND_CMD_PAGEPROG << 8) | NAND_CMD_SEQIN);
491
Bastian Hecht623c55c2012-05-14 14:14:45 +0200492 empty_fifo(flctl);
493 writel(readl(FLCMNCR(flctl)) | ACM_SACCES_MODE, FLCMNCR(flctl));
494 writel(readl(FLCMDCR(flctl)) | page_sectors, FLCMDCR(flctl));
495 writel(page_addr << 2, FLADR(flctl));
496 start_translation(flctl);
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900497
Bastian Hecht623c55c2012-05-14 14:14:45 +0200498 for (sector = 0; sector < page_sectors; sector++) {
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900499 write_fiforeg(flctl, 512, 512 * sector);
Bastian Hecht3166df02012-05-14 14:14:47 +0200500 write_ec_fiforeg(flctl, 16, mtd->writesize + 16 * sector);
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900501 }
502
Bastian Hecht623c55c2012-05-14 14:14:45 +0200503 wait_completion(flctl);
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900504 writel(readl(FLCMNCR(flctl)) & ~ACM_SACCES_MODE, FLCMNCR(flctl));
505}
506
507static void execmd_write_oob(struct mtd_info *mtd)
508{
509 struct sh_flctl *flctl = mtd_to_flctl(mtd);
510 int page_addr = flctl->seqin_page_addr;
511 int sector, page_sectors;
512
Bastian Hechtef4ce0b2012-05-14 14:14:43 +0200513 page_sectors = flctl->page_size ? 4 : 1;
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900514
515 set_cmd_regs(mtd, NAND_CMD_PAGEPROG,
516 (NAND_CMD_PAGEPROG << 8) | NAND_CMD_SEQIN);
517
Bastian Hechtef4ce0b2012-05-14 14:14:43 +0200518 for (sector = 0; sector < page_sectors; sector++) {
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900519 empty_fifo(flctl);
520 set_addr(mtd, sector * 528 + 512, page_addr);
521 writel(16, FLDTCNTR(flctl)); /* set read size */
522
523 start_translation(flctl);
524 write_fiforeg(flctl, 16, 16 * sector);
525 wait_completion(flctl);
526 }
527}
528
529static void flctl_cmdfunc(struct mtd_info *mtd, unsigned int command,
530 int column, int page_addr)
531{
532 struct sh_flctl *flctl = mtd_to_flctl(mtd);
533 uint32_t read_cmd = 0;
534
Bastian Hechtcfe78192012-03-18 15:13:20 +0100535 pm_runtime_get_sync(&flctl->pdev->dev);
536
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900537 flctl->read_bytes = 0;
538 if (command != NAND_CMD_PAGEPROG)
539 flctl->index = 0;
540
541 switch (command) {
542 case NAND_CMD_READ1:
543 case NAND_CMD_READ0:
544 if (flctl->hwecc) {
545 /* read page with hwecc */
546 execmd_read_page_sector(mtd, page_addr);
547 break;
548 }
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900549 if (flctl->page_size)
550 set_cmd_regs(mtd, command, (NAND_CMD_READSTART << 8)
551 | command);
552 else
553 set_cmd_regs(mtd, command, command);
554
555 set_addr(mtd, 0, page_addr);
556
557 flctl->read_bytes = mtd->writesize + mtd->oobsize;
Magnus Damm010ab822010-01-27 09:17:21 +0000558 if (flctl->chip.options & NAND_BUSWIDTH_16)
559 column >>= 1;
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900560 flctl->index += column;
561 goto read_normal_exit;
562
563 case NAND_CMD_READOOB:
564 if (flctl->hwecc) {
565 /* read page with hwecc */
566 execmd_read_oob(mtd, page_addr);
567 break;
568 }
569
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900570 if (flctl->page_size) {
571 set_cmd_regs(mtd, command, (NAND_CMD_READSTART << 8)
572 | NAND_CMD_READ0);
573 set_addr(mtd, mtd->writesize, page_addr);
574 } else {
575 set_cmd_regs(mtd, command, command);
576 set_addr(mtd, 0, page_addr);
577 }
578 flctl->read_bytes = mtd->oobsize;
579 goto read_normal_exit;
580
Bastian Hechtdd5ab242012-03-01 10:48:38 +0100581 case NAND_CMD_RNDOUT:
582 if (flctl->hwecc)
583 break;
584
585 if (flctl->page_size)
586 set_cmd_regs(mtd, command, (NAND_CMD_RNDOUTSTART << 8)
587 | command);
588 else
589 set_cmd_regs(mtd, command, command);
590
591 set_addr(mtd, column, 0);
592
593 flctl->read_bytes = mtd->writesize + mtd->oobsize - column;
594 goto read_normal_exit;
595
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900596 case NAND_CMD_READID:
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900597 set_cmd_regs(mtd, command, command);
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900598
Bastian Hecht7b6b2302012-03-01 10:48:37 +0100599 /* READID is always performed using an 8-bit bus */
600 if (flctl->chip.options & NAND_BUSWIDTH_16)
601 column <<= 1;
602 set_addr(mtd, column, 0);
603
604 flctl->read_bytes = 8;
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900605 writel(flctl->read_bytes, FLDTCNTR(flctl)); /* set read size */
Bastian Hechtabb59ef2012-03-01 10:48:36 +0100606 empty_fifo(flctl);
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900607 start_translation(flctl);
Bastian Hecht7b6b2302012-03-01 10:48:37 +0100608 read_fiforeg(flctl, flctl->read_bytes, 0);
609 wait_completion(flctl);
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900610 break;
611
612 case NAND_CMD_ERASE1:
613 flctl->erase1_page_addr = page_addr;
614 break;
615
616 case NAND_CMD_ERASE2:
617 set_cmd_regs(mtd, NAND_CMD_ERASE1,
618 (command << 8) | NAND_CMD_ERASE1);
619 set_addr(mtd, -1, flctl->erase1_page_addr);
620 start_translation(flctl);
621 wait_completion(flctl);
622 break;
623
624 case NAND_CMD_SEQIN:
625 if (!flctl->page_size) {
626 /* output read command */
627 if (column >= mtd->writesize) {
628 column -= mtd->writesize;
629 read_cmd = NAND_CMD_READOOB;
630 } else if (column < 256) {
631 read_cmd = NAND_CMD_READ0;
632 } else {
633 column -= 256;
634 read_cmd = NAND_CMD_READ1;
635 }
636 }
637 flctl->seqin_column = column;
638 flctl->seqin_page_addr = page_addr;
639 flctl->seqin_read_cmd = read_cmd;
640 break;
641
642 case NAND_CMD_PAGEPROG:
643 empty_fifo(flctl);
644 if (!flctl->page_size) {
645 set_cmd_regs(mtd, NAND_CMD_SEQIN,
646 flctl->seqin_read_cmd);
647 set_addr(mtd, -1, -1);
648 writel(0, FLDTCNTR(flctl)); /* set 0 size */
649 start_translation(flctl);
650 wait_completion(flctl);
651 }
652 if (flctl->hwecc) {
653 /* write page with hwecc */
654 if (flctl->seqin_column == mtd->writesize)
655 execmd_write_oob(mtd);
656 else if (!flctl->seqin_column)
657 execmd_write_page_sector(mtd);
658 else
659 printk(KERN_ERR "Invalid address !?\n");
660 break;
661 }
662 set_cmd_regs(mtd, command, (command << 8) | NAND_CMD_SEQIN);
663 set_addr(mtd, flctl->seqin_column, flctl->seqin_page_addr);
664 writel(flctl->index, FLDTCNTR(flctl)); /* set write size */
665 start_translation(flctl);
666 write_fiforeg(flctl, flctl->index, 0);
667 wait_completion(flctl);
668 break;
669
670 case NAND_CMD_STATUS:
671 set_cmd_regs(mtd, command, command);
672 set_addr(mtd, -1, -1);
673
674 flctl->read_bytes = 1;
675 writel(flctl->read_bytes, FLDTCNTR(flctl)); /* set read size */
676 start_translation(flctl);
677 read_datareg(flctl, 0); /* read and end */
678 break;
679
680 case NAND_CMD_RESET:
681 set_cmd_regs(mtd, command, command);
682 set_addr(mtd, -1, -1);
683
684 writel(0, FLDTCNTR(flctl)); /* set 0 size */
685 start_translation(flctl);
686 wait_completion(flctl);
687 break;
688
689 default:
690 break;
691 }
Bastian Hechtcfe78192012-03-18 15:13:20 +0100692 goto runtime_exit;
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900693
694read_normal_exit:
695 writel(flctl->read_bytes, FLDTCNTR(flctl)); /* set read size */
Bastian Hechtabb59ef2012-03-01 10:48:36 +0100696 empty_fifo(flctl);
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900697 start_translation(flctl);
698 read_fiforeg(flctl, flctl->read_bytes, 0);
699 wait_completion(flctl);
Bastian Hechtcfe78192012-03-18 15:13:20 +0100700runtime_exit:
701 pm_runtime_put_sync(&flctl->pdev->dev);
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900702 return;
703}
704
705static void flctl_select_chip(struct mtd_info *mtd, int chipnr)
706{
707 struct sh_flctl *flctl = mtd_to_flctl(mtd);
Bastian Hechtcfe78192012-03-18 15:13:20 +0100708 int ret;
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900709
710 switch (chipnr) {
711 case -1:
Bastian Hecht0b3f0d12012-03-01 10:48:39 +0100712 flctl->flcmncr_base &= ~CE0_ENABLE;
Bastian Hechtcfe78192012-03-18 15:13:20 +0100713
714 pm_runtime_get_sync(&flctl->pdev->dev);
Bastian Hecht0b3f0d12012-03-01 10:48:39 +0100715 writel(flctl->flcmncr_base, FLCMNCR(flctl));
Bastian Hechtcfe78192012-03-18 15:13:20 +0100716
717 if (flctl->qos_request) {
718 dev_pm_qos_remove_request(&flctl->pm_qos);
719 flctl->qos_request = 0;
720 }
721
722 pm_runtime_put_sync(&flctl->pdev->dev);
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900723 break;
724 case 0:
Bastian Hecht0b3f0d12012-03-01 10:48:39 +0100725 flctl->flcmncr_base |= CE0_ENABLE;
Bastian Hechtcfe78192012-03-18 15:13:20 +0100726
727 if (!flctl->qos_request) {
728 ret = dev_pm_qos_add_request(&flctl->pdev->dev,
729 &flctl->pm_qos, 100);
730 if (ret < 0)
731 dev_err(&flctl->pdev->dev,
732 "PM QoS request failed: %d\n", ret);
733 flctl->qos_request = 1;
734 }
735
736 if (flctl->holden) {
737 pm_runtime_get_sync(&flctl->pdev->dev);
Bastian Hecht3f2e9242012-03-01 10:48:40 +0100738 writel(HOLDEN, FLHOLDCR(flctl));
Bastian Hechtcfe78192012-03-18 15:13:20 +0100739 pm_runtime_put_sync(&flctl->pdev->dev);
740 }
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900741 break;
742 default:
743 BUG();
744 }
745}
746
747static void flctl_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
748{
749 struct sh_flctl *flctl = mtd_to_flctl(mtd);
Bastian Hechtd76236f2012-07-05 12:41:01 +0200750 int index = flctl->index;
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900751
Bastian Hechtd76236f2012-07-05 12:41:01 +0200752 memcpy(&flctl->done_buff[index], buf, len);
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900753 flctl->index += len;
754}
755
756static uint8_t flctl_read_byte(struct mtd_info *mtd)
757{
758 struct sh_flctl *flctl = mtd_to_flctl(mtd);
759 int index = flctl->index;
760 uint8_t data;
761
762 data = flctl->done_buff[index];
763 flctl->index++;
764 return data;
765}
766
Magnus Damm010ab822010-01-27 09:17:21 +0000767static uint16_t flctl_read_word(struct mtd_info *mtd)
768{
769 struct sh_flctl *flctl = mtd_to_flctl(mtd);
770 int index = flctl->index;
771 uint16_t data;
772 uint16_t *buf = (uint16_t *)&flctl->done_buff[index];
773
774 data = *buf;
775 flctl->index += 2;
776 return data;
777}
778
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900779static void flctl_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
780{
Bastian Hechtd76236f2012-07-05 12:41:01 +0200781 struct sh_flctl *flctl = mtd_to_flctl(mtd);
782 int index = flctl->index;
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900783
Bastian Hechtd76236f2012-07-05 12:41:01 +0200784 memcpy(buf, &flctl->done_buff[index], len);
785 flctl->index += len;
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900786}
787
788static int flctl_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
789{
790 int i;
791
792 for (i = 0; i < len; i++)
793 if (buf[i] != flctl_read_byte(mtd))
794 return -EFAULT;
795 return 0;
796}
797
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900798static int flctl_chip_init_tail(struct mtd_info *mtd)
799{
800 struct sh_flctl *flctl = mtd_to_flctl(mtd);
801 struct nand_chip *chip = &flctl->chip;
802
803 if (mtd->writesize == 512) {
804 flctl->page_size = 0;
805 if (chip->chipsize > (32 << 20)) {
806 /* big than 32MB */
807 flctl->rw_ADRCNT = ADRCNT_4;
808 flctl->erase_ADRCNT = ADRCNT_3;
809 } else if (chip->chipsize > (2 << 16)) {
810 /* big than 128KB */
811 flctl->rw_ADRCNT = ADRCNT_3;
812 flctl->erase_ADRCNT = ADRCNT_2;
813 } else {
814 flctl->rw_ADRCNT = ADRCNT_2;
815 flctl->erase_ADRCNT = ADRCNT_1;
816 }
817 } else {
818 flctl->page_size = 1;
819 if (chip->chipsize > (128 << 20)) {
820 /* big than 128MB */
821 flctl->rw_ADRCNT = ADRCNT2_E;
822 flctl->erase_ADRCNT = ADRCNT_3;
823 } else if (chip->chipsize > (8 << 16)) {
824 /* big than 512KB */
825 flctl->rw_ADRCNT = ADRCNT_4;
826 flctl->erase_ADRCNT = ADRCNT_2;
827 } else {
828 flctl->rw_ADRCNT = ADRCNT_3;
829 flctl->erase_ADRCNT = ADRCNT_1;
830 }
831 }
832
833 if (flctl->hwecc) {
834 if (mtd->writesize == 512) {
835 chip->ecc.layout = &flctl_4secc_oob_16;
836 chip->badblock_pattern = &flctl_4secc_smallpage;
837 } else {
838 chip->ecc.layout = &flctl_4secc_oob_64;
839 chip->badblock_pattern = &flctl_4secc_largepage;
840 }
841
842 chip->ecc.size = 512;
843 chip->ecc.bytes = 10;
Mike Dunn6a918ba2012-03-11 14:21:11 -0700844 chip->ecc.strength = 4;
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900845 chip->ecc.read_page = flctl_read_page_hwecc;
846 chip->ecc.write_page = flctl_write_page_hwecc;
847 chip->ecc.mode = NAND_ECC_HW;
848
849 /* 4 symbols ECC enabled */
Bastian Hechtaa32d1f2012-05-14 14:14:42 +0200850 flctl->flcmncr_base |= _4ECCEN;
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900851 } else {
852 chip->ecc.mode = NAND_ECC_SOFT;
853 }
854
855 return 0;
856}
857
Bastian Hecht3c7ea4e2012-05-14 14:14:41 +0200858static irqreturn_t flctl_handle_flste(int irq, void *dev_id)
859{
860 struct sh_flctl *flctl = dev_id;
861
862 dev_err(&flctl->pdev->dev, "flste irq: %x\n", readl(FLINTDMACR(flctl)));
863 writel(flctl->flintdmacr_base, FLINTDMACR(flctl));
864
865 return IRQ_HANDLED;
866}
867
Magnus Dammb79c7ad2010-02-02 13:01:25 +0900868static int __devinit flctl_probe(struct platform_device *pdev)
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900869{
870 struct resource *res;
871 struct sh_flctl *flctl;
872 struct mtd_info *flctl_mtd;
873 struct nand_chip *nand;
874 struct sh_flctl_platform_data *pdata;
Magnus Dammb79c7ad2010-02-02 13:01:25 +0900875 int ret = -ENXIO;
Bastian Hecht3c7ea4e2012-05-14 14:14:41 +0200876 int irq;
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900877
878 pdata = pdev->dev.platform_data;
879 if (pdata == NULL) {
Magnus Dammb79c7ad2010-02-02 13:01:25 +0900880 dev_err(&pdev->dev, "no platform data defined\n");
881 return -EINVAL;
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900882 }
883
884 flctl = kzalloc(sizeof(struct sh_flctl), GFP_KERNEL);
885 if (!flctl) {
Magnus Dammb79c7ad2010-02-02 13:01:25 +0900886 dev_err(&pdev->dev, "failed to allocate driver data\n");
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900887 return -ENOMEM;
888 }
889
890 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
891 if (!res) {
Magnus Dammb79c7ad2010-02-02 13:01:25 +0900892 dev_err(&pdev->dev, "failed to get I/O memory\n");
Bastian Hechtcfe78192012-03-18 15:13:20 +0100893 goto err_iomap;
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900894 }
895
H Hartley Sweetencbd38a82009-12-14 16:59:27 -0500896 flctl->reg = ioremap(res->start, resource_size(res));
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900897 if (flctl->reg == NULL) {
Magnus Dammb79c7ad2010-02-02 13:01:25 +0900898 dev_err(&pdev->dev, "failed to remap I/O memory\n");
Bastian Hechtcfe78192012-03-18 15:13:20 +0100899 goto err_iomap;
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900900 }
901
Bastian Hecht3c7ea4e2012-05-14 14:14:41 +0200902 irq = platform_get_irq(pdev, 0);
903 if (irq < 0) {
904 dev_err(&pdev->dev, "failed to get flste irq data\n");
905 goto err_flste;
906 }
907
908 ret = request_irq(irq, flctl_handle_flste, IRQF_SHARED, "flste", flctl);
909 if (ret) {
910 dev_err(&pdev->dev, "request interrupt failed.\n");
911 goto err_flste;
912 }
913
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900914 platform_set_drvdata(pdev, flctl);
915 flctl_mtd = &flctl->mtd;
916 nand = &flctl->chip;
917 flctl_mtd->priv = nand;
Magnus Dammb79c7ad2010-02-02 13:01:25 +0900918 flctl->pdev = pdev;
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900919 flctl->hwecc = pdata->has_hwecc;
Bastian Hecht3f2e9242012-03-01 10:48:40 +0100920 flctl->holden = pdata->use_holden;
Bastian Hecht3c7ea4e2012-05-14 14:14:41 +0200921 flctl->flcmncr_base = pdata->flcmncr_val;
922 flctl->flintdmacr_base = flctl->hwecc ? (STERINTE | ECERB) : STERINTE;
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900923
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900924 /* Set address of hardware control function */
925 /* 20 us command delay time */
926 nand->chip_delay = 20;
927
928 nand->read_byte = flctl_read_byte;
929 nand->write_buf = flctl_write_buf;
930 nand->read_buf = flctl_read_buf;
931 nand->verify_buf = flctl_verify_buf;
932 nand->select_chip = flctl_select_chip;
933 nand->cmdfunc = flctl_cmdfunc;
934
Magnus Damm010ab822010-01-27 09:17:21 +0000935 if (pdata->flcmncr_val & SEL_16BIT) {
936 nand->options |= NAND_BUSWIDTH_16;
937 nand->read_word = flctl_read_word;
938 }
939
Bastian Hechtcfe78192012-03-18 15:13:20 +0100940 pm_runtime_enable(&pdev->dev);
941 pm_runtime_resume(&pdev->dev);
942
David Woodhouse5e81e882010-02-26 18:32:56 +0000943 ret = nand_scan_ident(flctl_mtd, 1, NULL);
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900944 if (ret)
Bastian Hechtcfe78192012-03-18 15:13:20 +0100945 goto err_chip;
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900946
947 ret = flctl_chip_init_tail(flctl_mtd);
948 if (ret)
Bastian Hechtcfe78192012-03-18 15:13:20 +0100949 goto err_chip;
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900950
951 ret = nand_scan_tail(flctl_mtd);
952 if (ret)
Bastian Hechtcfe78192012-03-18 15:13:20 +0100953 goto err_chip;
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900954
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100955 mtd_device_register(flctl_mtd, pdata->parts, pdata->nr_parts);
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900956
957 return 0;
958
Bastian Hechtcfe78192012-03-18 15:13:20 +0100959err_chip:
960 pm_runtime_disable(&pdev->dev);
Bastian Hecht3c7ea4e2012-05-14 14:14:41 +0200961 free_irq(irq, flctl);
962err_flste:
Bastian Hechtcb54751d2012-05-14 14:14:40 +0200963 iounmap(flctl->reg);
Bastian Hechtcfe78192012-03-18 15:13:20 +0100964err_iomap:
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900965 kfree(flctl);
966 return ret;
967}
968
Magnus Dammb79c7ad2010-02-02 13:01:25 +0900969static int __devexit flctl_remove(struct platform_device *pdev)
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900970{
971 struct sh_flctl *flctl = platform_get_drvdata(pdev);
972
973 nand_release(&flctl->mtd);
Bastian Hechtcfe78192012-03-18 15:13:20 +0100974 pm_runtime_disable(&pdev->dev);
Bastian Hecht3c7ea4e2012-05-14 14:14:41 +0200975 free_irq(platform_get_irq(pdev, 0), flctl);
Bastian Hechtcb54751d2012-05-14 14:14:40 +0200976 iounmap(flctl->reg);
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900977 kfree(flctl);
978
979 return 0;
980}
981
982static struct platform_driver flctl_driver = {
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900983 .remove = flctl_remove,
984 .driver = {
985 .name = "sh_flctl",
986 .owner = THIS_MODULE,
987 },
988};
989
990static int __init flctl_nand_init(void)
991{
David Woodhouse894572a2009-09-19 16:07:34 -0700992 return platform_driver_probe(&flctl_driver, flctl_probe);
Yoshihiro Shimoda35a34792008-10-20 17:17:44 +0900993}
994
995static void __exit flctl_nand_cleanup(void)
996{
997 platform_driver_unregister(&flctl_driver);
998}
999
1000module_init(flctl_nand_init);
1001module_exit(flctl_nand_cleanup);
1002
1003MODULE_LICENSE("GPL");
1004MODULE_AUTHOR("Yoshihiro Shimoda");
1005MODULE_DESCRIPTION("SuperH FLCTL driver");
1006MODULE_ALIAS("platform:sh_flctl");