Merge remote branch 'nouveau/for-airlied' of /ssd/git/drm-nouveau-next into drm-fixes
[pandora-kernel.git] / drivers / mtd / nand / mxc_nand.c
1 /*
2  * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved.
3  * Copyright 2008 Sascha Hauer, kernel@pengutronix.de
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17  * MA 02110-1301, USA.
18  */
19
20 #include <linux/delay.h>
21 #include <linux/slab.h>
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/mtd/mtd.h>
25 #include <linux/mtd/nand.h>
26 #include <linux/mtd/partitions.h>
27 #include <linux/interrupt.h>
28 #include <linux/device.h>
29 #include <linux/platform_device.h>
30 #include <linux/clk.h>
31 #include <linux/err.h>
32 #include <linux/io.h>
33
34 #include <asm/mach/flash.h>
35 #include <mach/mxc_nand.h>
36 #include <mach/hardware.h>
37
38 #define DRIVER_NAME "mxc_nand"
39
40 #define nfc_is_v21()            (cpu_is_mx25() || cpu_is_mx35())
41 #define nfc_is_v1()             (cpu_is_mx31() || cpu_is_mx27() || cpu_is_mx21())
42 #define nfc_is_v3_2()           cpu_is_mx51()
43 #define nfc_is_v3()             nfc_is_v3_2()
44
45 /* Addresses for NFC registers */
46 #define NFC_V1_V2_BUF_SIZE              (host->regs + 0x00)
47 #define NFC_V1_V2_BUF_ADDR              (host->regs + 0x04)
48 #define NFC_V1_V2_FLASH_ADDR            (host->regs + 0x06)
49 #define NFC_V1_V2_FLASH_CMD             (host->regs + 0x08)
50 #define NFC_V1_V2_CONFIG                (host->regs + 0x0a)
51 #define NFC_V1_V2_ECC_STATUS_RESULT     (host->regs + 0x0c)
52 #define NFC_V1_V2_RSLTMAIN_AREA         (host->regs + 0x0e)
53 #define NFC_V1_V2_RSLTSPARE_AREA        (host->regs + 0x10)
54 #define NFC_V1_V2_WRPROT                (host->regs + 0x12)
55 #define NFC_V1_UNLOCKSTART_BLKADDR      (host->regs + 0x14)
56 #define NFC_V1_UNLOCKEND_BLKADDR        (host->regs + 0x16)
57 #define NFC_V21_UNLOCKSTART_BLKADDR     (host->regs + 0x20)
58 #define NFC_V21_UNLOCKEND_BLKADDR       (host->regs + 0x22)
59 #define NFC_V1_V2_NF_WRPRST             (host->regs + 0x18)
60 #define NFC_V1_V2_CONFIG1               (host->regs + 0x1a)
61 #define NFC_V1_V2_CONFIG2               (host->regs + 0x1c)
62
63 #define NFC_V2_CONFIG1_ECC_MODE_4       (1 << 0)
64 #define NFC_V1_V2_CONFIG1_SP_EN         (1 << 2)
65 #define NFC_V1_V2_CONFIG1_ECC_EN        (1 << 3)
66 #define NFC_V1_V2_CONFIG1_INT_MSK       (1 << 4)
67 #define NFC_V1_V2_CONFIG1_BIG           (1 << 5)
68 #define NFC_V1_V2_CONFIG1_RST           (1 << 6)
69 #define NFC_V1_V2_CONFIG1_CE            (1 << 7)
70 #define NFC_V1_V2_CONFIG1_ONE_CYCLE     (1 << 8)
71
72 #define NFC_V1_V2_CONFIG2_INT           (1 << 15)
73
74 /*
75  * Operation modes for the NFC. Valid for v1, v2 and v3
76  * type controllers.
77  */
78 #define NFC_CMD                         (1 << 0)
79 #define NFC_ADDR                        (1 << 1)
80 #define NFC_INPUT                       (1 << 2)
81 #define NFC_OUTPUT                      (1 << 3)
82 #define NFC_ID                          (1 << 4)
83 #define NFC_STATUS                      (1 << 5)
84
85 #define NFC_V3_FLASH_CMD                (host->regs_axi + 0x00)
86 #define NFC_V3_FLASH_ADDR0              (host->regs_axi + 0x04)
87
88 #define NFC_V3_CONFIG1                  (host->regs_axi + 0x34)
89 #define NFC_V3_CONFIG1_SP_EN            (1 << 0)
90 #define NFC_V3_CONFIG1_RBA(x)           (((x) & 0x7 ) << 4)
91
92 #define NFC_V3_ECC_STATUS_RESULT        (host->regs_axi + 0x38)
93
94 #define NFC_V3_LAUNCH                   (host->regs_axi + 0x40)
95
96 #define NFC_V3_WRPROT                   (host->regs_ip + 0x0)
97 #define NFC_V3_WRPROT_LOCK_TIGHT        (1 << 0)
98 #define NFC_V3_WRPROT_LOCK              (1 << 1)
99 #define NFC_V3_WRPROT_UNLOCK            (1 << 2)
100 #define NFC_V3_WRPROT_BLS_UNLOCK        (2 << 6)
101
102 #define NFC_V3_WRPROT_UNLOCK_BLK_ADD0   (host->regs_ip + 0x04)
103
104 #define NFC_V3_CONFIG2                  (host->regs_ip + 0x24)
105 #define NFC_V3_CONFIG2_PS_512                   (0 << 0)
106 #define NFC_V3_CONFIG2_PS_2048                  (1 << 0)
107 #define NFC_V3_CONFIG2_PS_4096                  (2 << 0)
108 #define NFC_V3_CONFIG2_ONE_CYCLE                (1 << 2)
109 #define NFC_V3_CONFIG2_ECC_EN                   (1 << 3)
110 #define NFC_V3_CONFIG2_2CMD_PHASES              (1 << 4)
111 #define NFC_V3_CONFIG2_NUM_ADDR_PHASE0          (1 << 5)
112 #define NFC_V3_CONFIG2_ECC_MODE_8               (1 << 6)
113 #define NFC_V3_CONFIG2_PPB(x)                   (((x) & 0x3) << 7)
114 #define NFC_V3_CONFIG2_NUM_ADDR_PHASE1(x)       (((x) & 0x3) << 12)
115 #define NFC_V3_CONFIG2_INT_MSK                  (1 << 15)
116 #define NFC_V3_CONFIG2_ST_CMD(x)                (((x) & 0xff) << 24)
117 #define NFC_V3_CONFIG2_SPAS(x)                  (((x) & 0xff) << 16)
118
119 #define NFC_V3_CONFIG3                          (host->regs_ip + 0x28)
120 #define NFC_V3_CONFIG3_ADD_OP(x)                (((x) & 0x3) << 0)
121 #define NFC_V3_CONFIG3_FW8                      (1 << 3)
122 #define NFC_V3_CONFIG3_SBB(x)                   (((x) & 0x7) << 8)
123 #define NFC_V3_CONFIG3_NUM_OF_DEVICES(x)        (((x) & 0x7) << 12)
124 #define NFC_V3_CONFIG3_RBB_MODE                 (1 << 15)
125 #define NFC_V3_CONFIG3_NO_SDMA                  (1 << 20)
126
127 #define NFC_V3_IPC                      (host->regs_ip + 0x2C)
128 #define NFC_V3_IPC_CREQ                 (1 << 0)
129 #define NFC_V3_IPC_INT                  (1 << 31)
130
131 #define NFC_V3_DELAY_LINE               (host->regs_ip + 0x34)
132
133 struct mxc_nand_host {
134         struct mtd_info         mtd;
135         struct nand_chip        nand;
136         struct mtd_partition    *parts;
137         struct device           *dev;
138
139         void                    *spare0;
140         void                    *main_area0;
141
142         void __iomem            *base;
143         void __iomem            *regs;
144         void __iomem            *regs_axi;
145         void __iomem            *regs_ip;
146         int                     status_request;
147         struct clk              *clk;
148         int                     clk_act;
149         int                     irq;
150         int                     eccsize;
151
152         wait_queue_head_t       irq_waitq;
153
154         uint8_t                 *data_buf;
155         unsigned int            buf_start;
156         int                     spare_len;
157
158         void                    (*preset)(struct mtd_info *);
159         void                    (*send_cmd)(struct mxc_nand_host *, uint16_t, int);
160         void                    (*send_addr)(struct mxc_nand_host *, uint16_t, int);
161         void                    (*send_page)(struct mtd_info *, unsigned int);
162         void                    (*send_read_id)(struct mxc_nand_host *);
163         uint16_t                (*get_dev_status)(struct mxc_nand_host *);
164         int                     (*check_int)(struct mxc_nand_host *);
165 };
166
167 /* OOB placement block for use with hardware ecc generation */
168 static struct nand_ecclayout nandv1_hw_eccoob_smallpage = {
169         .eccbytes = 5,
170         .eccpos = {6, 7, 8, 9, 10},
171         .oobfree = {{0, 5}, {12, 4}, }
172 };
173
174 static struct nand_ecclayout nandv1_hw_eccoob_largepage = {
175         .eccbytes = 20,
176         .eccpos = {6, 7, 8, 9, 10, 22, 23, 24, 25, 26,
177                    38, 39, 40, 41, 42, 54, 55, 56, 57, 58},
178         .oobfree = {{2, 4}, {11, 10}, {27, 10}, {43, 10}, {59, 5}, }
179 };
180
181 /* OOB description for 512 byte pages with 16 byte OOB */
182 static struct nand_ecclayout nandv2_hw_eccoob_smallpage = {
183         .eccbytes = 1 * 9,
184         .eccpos = {
185                  7,  8,  9, 10, 11, 12, 13, 14, 15
186         },
187         .oobfree = {
188                 {.offset = 0, .length = 5}
189         }
190 };
191
192 /* OOB description for 2048 byte pages with 64 byte OOB */
193 static struct nand_ecclayout nandv2_hw_eccoob_largepage = {
194         .eccbytes = 4 * 9,
195         .eccpos = {
196                  7,  8,  9, 10, 11, 12, 13, 14, 15,
197                 23, 24, 25, 26, 27, 28, 29, 30, 31,
198                 39, 40, 41, 42, 43, 44, 45, 46, 47,
199                 55, 56, 57, 58, 59, 60, 61, 62, 63
200         },
201         .oobfree = {
202                 {.offset = 2, .length = 4},
203                 {.offset = 16, .length = 7},
204                 {.offset = 32, .length = 7},
205                 {.offset = 48, .length = 7}
206         }
207 };
208
209 #ifdef CONFIG_MTD_PARTITIONS
210 static const char *part_probes[] = { "RedBoot", "cmdlinepart", NULL };
211 #endif
212
213 static irqreturn_t mxc_nfc_irq(int irq, void *dev_id)
214 {
215         struct mxc_nand_host *host = dev_id;
216
217         disable_irq_nosync(irq);
218
219         wake_up(&host->irq_waitq);
220
221         return IRQ_HANDLED;
222 }
223
224 static int check_int_v3(struct mxc_nand_host *host)
225 {
226         uint32_t tmp;
227
228         tmp = readl(NFC_V3_IPC);
229         if (!(tmp & NFC_V3_IPC_INT))
230                 return 0;
231
232         tmp &= ~NFC_V3_IPC_INT;
233         writel(tmp, NFC_V3_IPC);
234
235         return 1;
236 }
237
238 static int check_int_v1_v2(struct mxc_nand_host *host)
239 {
240         uint32_t tmp;
241
242         tmp = readw(NFC_V1_V2_CONFIG2);
243         if (!(tmp & NFC_V1_V2_CONFIG2_INT))
244                 return 0;
245
246         writew(tmp & ~NFC_V1_V2_CONFIG2_INT, NFC_V1_V2_CONFIG2);
247
248         return 1;
249 }
250
251 /* This function polls the NANDFC to wait for the basic operation to
252  * complete by checking the INT bit of config2 register.
253  */
254 static void wait_op_done(struct mxc_nand_host *host, int useirq)
255 {
256         int max_retries = 8000;
257
258         if (useirq) {
259                 if (!host->check_int(host)) {
260
261                         enable_irq(host->irq);
262
263                         wait_event(host->irq_waitq, host->check_int(host));
264                 }
265         } else {
266                 while (max_retries-- > 0) {
267                         if (host->check_int(host))
268                                 break;
269
270                         udelay(1);
271                 }
272                 if (max_retries < 0)
273                         DEBUG(MTD_DEBUG_LEVEL0, "%s: INT not set\n",
274                               __func__);
275         }
276 }
277
278 static void send_cmd_v3(struct mxc_nand_host *host, uint16_t cmd, int useirq)
279 {
280         /* fill command */
281         writel(cmd, NFC_V3_FLASH_CMD);
282
283         /* send out command */
284         writel(NFC_CMD, NFC_V3_LAUNCH);
285
286         /* Wait for operation to complete */
287         wait_op_done(host, useirq);
288 }
289
290 /* This function issues the specified command to the NAND device and
291  * waits for completion. */
292 static void send_cmd_v1_v2(struct mxc_nand_host *host, uint16_t cmd, int useirq)
293 {
294         DEBUG(MTD_DEBUG_LEVEL3, "send_cmd(host, 0x%x, %d)\n", cmd, useirq);
295
296         writew(cmd, NFC_V1_V2_FLASH_CMD);
297         writew(NFC_CMD, NFC_V1_V2_CONFIG2);
298
299         if (cpu_is_mx21() && (cmd == NAND_CMD_RESET)) {
300                 int max_retries = 100;
301                 /* Reset completion is indicated by NFC_CONFIG2 */
302                 /* being set to 0 */
303                 while (max_retries-- > 0) {
304                         if (readw(NFC_V1_V2_CONFIG2) == 0) {
305                                 break;
306                         }
307                         udelay(1);
308                 }
309                 if (max_retries < 0)
310                         DEBUG(MTD_DEBUG_LEVEL0, "%s: RESET failed\n",
311                               __func__);
312         } else {
313                 /* Wait for operation to complete */
314                 wait_op_done(host, useirq);
315         }
316 }
317
318 static void send_addr_v3(struct mxc_nand_host *host, uint16_t addr, int islast)
319 {
320         /* fill address */
321         writel(addr, NFC_V3_FLASH_ADDR0);
322
323         /* send out address */
324         writel(NFC_ADDR, NFC_V3_LAUNCH);
325
326         wait_op_done(host, 0);
327 }
328
329 /* This function sends an address (or partial address) to the
330  * NAND device. The address is used to select the source/destination for
331  * a NAND command. */
332 static void send_addr_v1_v2(struct mxc_nand_host *host, uint16_t addr, int islast)
333 {
334         DEBUG(MTD_DEBUG_LEVEL3, "send_addr(host, 0x%x %d)\n", addr, islast);
335
336         writew(addr, NFC_V1_V2_FLASH_ADDR);
337         writew(NFC_ADDR, NFC_V1_V2_CONFIG2);
338
339         /* Wait for operation to complete */
340         wait_op_done(host, islast);
341 }
342
343 static void send_page_v3(struct mtd_info *mtd, unsigned int ops)
344 {
345         struct nand_chip *nand_chip = mtd->priv;
346         struct mxc_nand_host *host = nand_chip->priv;
347         uint32_t tmp;
348
349         tmp = readl(NFC_V3_CONFIG1);
350         tmp &= ~(7 << 4);
351         writel(tmp, NFC_V3_CONFIG1);
352
353         /* transfer data from NFC ram to nand */
354         writel(ops, NFC_V3_LAUNCH);
355
356         wait_op_done(host, false);
357 }
358
359 static void send_page_v1_v2(struct mtd_info *mtd, unsigned int ops)
360 {
361         struct nand_chip *nand_chip = mtd->priv;
362         struct mxc_nand_host *host = nand_chip->priv;
363         int bufs, i;
364
365         if (nfc_is_v1() && mtd->writesize > 512)
366                 bufs = 4;
367         else
368                 bufs = 1;
369
370         for (i = 0; i < bufs; i++) {
371
372                 /* NANDFC buffer 0 is used for page read/write */
373                 writew(i, NFC_V1_V2_BUF_ADDR);
374
375                 writew(ops, NFC_V1_V2_CONFIG2);
376
377                 /* Wait for operation to complete */
378                 wait_op_done(host, true);
379         }
380 }
381
382 static void send_read_id_v3(struct mxc_nand_host *host)
383 {
384         /* Read ID into main buffer */
385         writel(NFC_ID, NFC_V3_LAUNCH);
386
387         wait_op_done(host, true);
388
389         memcpy(host->data_buf, host->main_area0, 16);
390 }
391
392 /* Request the NANDFC to perform a read of the NAND device ID. */
393 static void send_read_id_v1_v2(struct mxc_nand_host *host)
394 {
395         struct nand_chip *this = &host->nand;
396
397         /* NANDFC buffer 0 is used for device ID output */
398         writew(0x0, NFC_V1_V2_BUF_ADDR);
399
400         writew(NFC_ID, NFC_V1_V2_CONFIG2);
401
402         /* Wait for operation to complete */
403         wait_op_done(host, true);
404
405         if (this->options & NAND_BUSWIDTH_16) {
406                 void __iomem *main_buf = host->main_area0;
407                 /* compress the ID info */
408                 writeb(readb(main_buf + 2), main_buf + 1);
409                 writeb(readb(main_buf + 4), main_buf + 2);
410                 writeb(readb(main_buf + 6), main_buf + 3);
411                 writeb(readb(main_buf + 8), main_buf + 4);
412                 writeb(readb(main_buf + 10), main_buf + 5);
413         }
414         memcpy(host->data_buf, host->main_area0, 16);
415 }
416
417 static uint16_t get_dev_status_v3(struct mxc_nand_host *host)
418 {
419         writew(NFC_STATUS, NFC_V3_LAUNCH);
420         wait_op_done(host, true);
421
422         return readl(NFC_V3_CONFIG1) >> 16;
423 }
424
425 /* This function requests the NANDFC to perform a read of the
426  * NAND device status and returns the current status. */
427 static uint16_t get_dev_status_v1_v2(struct mxc_nand_host *host)
428 {
429         void __iomem *main_buf = host->main_area0;
430         uint32_t store;
431         uint16_t ret;
432
433         writew(0x0, NFC_V1_V2_BUF_ADDR);
434
435         /*
436          * The device status is stored in main_area0. To
437          * prevent corruption of the buffer save the value
438          * and restore it afterwards.
439          */
440         store = readl(main_buf);
441
442         writew(NFC_STATUS, NFC_V1_V2_CONFIG2);
443         wait_op_done(host, true);
444
445         ret = readw(main_buf);
446
447         writel(store, main_buf);
448
449         return ret;
450 }
451
452 /* This functions is used by upper layer to checks if device is ready */
453 static int mxc_nand_dev_ready(struct mtd_info *mtd)
454 {
455         /*
456          * NFC handles R/B internally. Therefore, this function
457          * always returns status as ready.
458          */
459         return 1;
460 }
461
462 static void mxc_nand_enable_hwecc(struct mtd_info *mtd, int mode)
463 {
464         /*
465          * If HW ECC is enabled, we turn it on during init. There is
466          * no need to enable again here.
467          */
468 }
469
470 static int mxc_nand_correct_data_v1(struct mtd_info *mtd, u_char *dat,
471                                  u_char *read_ecc, u_char *calc_ecc)
472 {
473         struct nand_chip *nand_chip = mtd->priv;
474         struct mxc_nand_host *host = nand_chip->priv;
475
476         /*
477          * 1-Bit errors are automatically corrected in HW.  No need for
478          * additional correction.  2-Bit errors cannot be corrected by
479          * HW ECC, so we need to return failure
480          */
481         uint16_t ecc_status = readw(NFC_V1_V2_ECC_STATUS_RESULT);
482
483         if (((ecc_status & 0x3) == 2) || ((ecc_status >> 2) == 2)) {
484                 DEBUG(MTD_DEBUG_LEVEL0,
485                       "MXC_NAND: HWECC uncorrectable 2-bit ECC error\n");
486                 return -1;
487         }
488
489         return 0;
490 }
491
492 static int mxc_nand_correct_data_v2_v3(struct mtd_info *mtd, u_char *dat,
493                                  u_char *read_ecc, u_char *calc_ecc)
494 {
495         struct nand_chip *nand_chip = mtd->priv;
496         struct mxc_nand_host *host = nand_chip->priv;
497         u32 ecc_stat, err;
498         int no_subpages = 1;
499         int ret = 0;
500         u8 ecc_bit_mask, err_limit;
501
502         ecc_bit_mask = (host->eccsize == 4) ? 0x7 : 0xf;
503         err_limit = (host->eccsize == 4) ? 0x4 : 0x8;
504
505         no_subpages = mtd->writesize >> 9;
506
507         if (nfc_is_v21())
508                 ecc_stat = readl(NFC_V1_V2_ECC_STATUS_RESULT);
509         else
510                 ecc_stat = readl(NFC_V3_ECC_STATUS_RESULT);
511
512         do {
513                 err = ecc_stat & ecc_bit_mask;
514                 if (err > err_limit) {
515                         printk(KERN_WARNING "UnCorrectable RS-ECC Error\n");
516                         return -1;
517                 } else {
518                         ret += err;
519                 }
520                 ecc_stat >>= 4;
521         } while (--no_subpages);
522
523         mtd->ecc_stats.corrected += ret;
524         pr_debug("%d Symbol Correctable RS-ECC Error\n", ret);
525
526         return ret;
527 }
528
529 static int mxc_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
530                                   u_char *ecc_code)
531 {
532         return 0;
533 }
534
535 static u_char mxc_nand_read_byte(struct mtd_info *mtd)
536 {
537         struct nand_chip *nand_chip = mtd->priv;
538         struct mxc_nand_host *host = nand_chip->priv;
539         uint8_t ret;
540
541         /* Check for status request */
542         if (host->status_request)
543                 return host->get_dev_status(host) & 0xFF;
544
545         ret = *(uint8_t *)(host->data_buf + host->buf_start);
546         host->buf_start++;
547
548         return ret;
549 }
550
551 static uint16_t mxc_nand_read_word(struct mtd_info *mtd)
552 {
553         struct nand_chip *nand_chip = mtd->priv;
554         struct mxc_nand_host *host = nand_chip->priv;
555         uint16_t ret;
556
557         ret = *(uint16_t *)(host->data_buf + host->buf_start);
558         host->buf_start += 2;
559
560         return ret;
561 }
562
563 /* Write data of length len to buffer buf. The data to be
564  * written on NAND Flash is first copied to RAMbuffer. After the Data Input
565  * Operation by the NFC, the data is written to NAND Flash */
566 static void mxc_nand_write_buf(struct mtd_info *mtd,
567                                 const u_char *buf, int len)
568 {
569         struct nand_chip *nand_chip = mtd->priv;
570         struct mxc_nand_host *host = nand_chip->priv;
571         u16 col = host->buf_start;
572         int n = mtd->oobsize + mtd->writesize - col;
573
574         n = min(n, len);
575
576         memcpy(host->data_buf + col, buf, n);
577
578         host->buf_start += n;
579 }
580
581 /* Read the data buffer from the NAND Flash. To read the data from NAND
582  * Flash first the data output cycle is initiated by the NFC, which copies
583  * the data to RAMbuffer. This data of length len is then copied to buffer buf.
584  */
585 static void mxc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
586 {
587         struct nand_chip *nand_chip = mtd->priv;
588         struct mxc_nand_host *host = nand_chip->priv;
589         u16 col = host->buf_start;
590         int n = mtd->oobsize + mtd->writesize - col;
591
592         n = min(n, len);
593
594         memcpy(buf, host->data_buf + col, len);
595
596         host->buf_start += len;
597 }
598
599 /* Used by the upper layer to verify the data in NAND Flash
600  * with the data in the buf. */
601 static int mxc_nand_verify_buf(struct mtd_info *mtd,
602                                 const u_char *buf, int len)
603 {
604         return -EFAULT;
605 }
606
607 /* This function is used by upper layer for select and
608  * deselect of the NAND chip */
609 static void mxc_nand_select_chip(struct mtd_info *mtd, int chip)
610 {
611         struct nand_chip *nand_chip = mtd->priv;
612         struct mxc_nand_host *host = nand_chip->priv;
613
614         switch (chip) {
615         case -1:
616                 /* Disable the NFC clock */
617                 if (host->clk_act) {
618                         clk_disable(host->clk);
619                         host->clk_act = 0;
620                 }
621                 break;
622         case 0:
623                 /* Enable the NFC clock */
624                 if (!host->clk_act) {
625                         clk_enable(host->clk);
626                         host->clk_act = 1;
627                 }
628                 break;
629
630         default:
631                 break;
632         }
633 }
634
635 /*
636  * Function to transfer data to/from spare area.
637  */
638 static void copy_spare(struct mtd_info *mtd, bool bfrom)
639 {
640         struct nand_chip *this = mtd->priv;
641         struct mxc_nand_host *host = this->priv;
642         u16 i, j;
643         u16 n = mtd->writesize >> 9;
644         u8 *d = host->data_buf + mtd->writesize;
645         u8 *s = host->spare0;
646         u16 t = host->spare_len;
647
648         j = (mtd->oobsize / n >> 1) << 1;
649
650         if (bfrom) {
651                 for (i = 0; i < n - 1; i++)
652                         memcpy(d + i * j, s + i * t, j);
653
654                 /* the last section */
655                 memcpy(d + i * j, s + i * t, mtd->oobsize - i * j);
656         } else {
657                 for (i = 0; i < n - 1; i++)
658                         memcpy(&s[i * t], &d[i * j], j);
659
660                 /* the last section */
661                 memcpy(&s[i * t], &d[i * j], mtd->oobsize - i * j);
662         }
663 }
664
665 static void mxc_do_addr_cycle(struct mtd_info *mtd, int column, int page_addr)
666 {
667         struct nand_chip *nand_chip = mtd->priv;
668         struct mxc_nand_host *host = nand_chip->priv;
669
670         /* Write out column address, if necessary */
671         if (column != -1) {
672                 /*
673                  * MXC NANDFC can only perform full page+spare or
674                  * spare-only read/write.  When the upper layers
675                  * layers perform a read/write buf operation,
676                  * we will used the saved column address to index into
677                  * the full page.
678                  */
679                 host->send_addr(host, 0, page_addr == -1);
680                 if (mtd->writesize > 512)
681                         /* another col addr cycle for 2k page */
682                         host->send_addr(host, 0, false);
683         }
684
685         /* Write out page address, if necessary */
686         if (page_addr != -1) {
687                 /* paddr_0 - p_addr_7 */
688                 host->send_addr(host, (page_addr & 0xff), false);
689
690                 if (mtd->writesize > 512) {
691                         if (mtd->size >= 0x10000000) {
692                                 /* paddr_8 - paddr_15 */
693                                 host->send_addr(host, (page_addr >> 8) & 0xff, false);
694                                 host->send_addr(host, (page_addr >> 16) & 0xff, true);
695                         } else
696                                 /* paddr_8 - paddr_15 */
697                                 host->send_addr(host, (page_addr >> 8) & 0xff, true);
698                 } else {
699                         /* One more address cycle for higher density devices */
700                         if (mtd->size >= 0x4000000) {
701                                 /* paddr_8 - paddr_15 */
702                                 host->send_addr(host, (page_addr >> 8) & 0xff, false);
703                                 host->send_addr(host, (page_addr >> 16) & 0xff, true);
704                         } else
705                                 /* paddr_8 - paddr_15 */
706                                 host->send_addr(host, (page_addr >> 8) & 0xff, true);
707                 }
708         }
709 }
710
711 /*
712  * v2 and v3 type controllers can do 4bit or 8bit ecc depending
713  * on how much oob the nand chip has. For 8bit ecc we need at least
714  * 26 bytes of oob data per 512 byte block.
715  */
716 static int get_eccsize(struct mtd_info *mtd)
717 {
718         int oobbytes_per_512 = 0;
719
720         oobbytes_per_512 = mtd->oobsize * 512 / mtd->writesize;
721
722         if (oobbytes_per_512 < 26)
723                 return 4;
724         else
725                 return 8;
726 }
727
728 static void preset_v1_v2(struct mtd_info *mtd)
729 {
730         struct nand_chip *nand_chip = mtd->priv;
731         struct mxc_nand_host *host = nand_chip->priv;
732         uint16_t tmp;
733
734         /* enable interrupt, disable spare enable */
735         tmp = readw(NFC_V1_V2_CONFIG1);
736         tmp &= ~NFC_V1_V2_CONFIG1_INT_MSK;
737         tmp &= ~NFC_V1_V2_CONFIG1_SP_EN;
738         if (nand_chip->ecc.mode == NAND_ECC_HW) {
739                 tmp |= NFC_V1_V2_CONFIG1_ECC_EN;
740         } else {
741                 tmp &= ~NFC_V1_V2_CONFIG1_ECC_EN;
742         }
743
744         if (nfc_is_v21() && mtd->writesize) {
745                 host->eccsize = get_eccsize(mtd);
746                 if (host->eccsize == 4)
747                         tmp |= NFC_V2_CONFIG1_ECC_MODE_4;
748         } else {
749                 host->eccsize = 1;
750         }
751
752         writew(tmp, NFC_V1_V2_CONFIG1);
753         /* preset operation */
754
755         /* Unlock the internal RAM Buffer */
756         writew(0x2, NFC_V1_V2_CONFIG);
757
758         /* Blocks to be unlocked */
759         if (nfc_is_v21()) {
760                 writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR);
761                 writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR);
762         } else if (nfc_is_v1()) {
763                 writew(0x0, NFC_V1_UNLOCKSTART_BLKADDR);
764                 writew(0x4000, NFC_V1_UNLOCKEND_BLKADDR);
765         } else
766                 BUG();
767
768         /* Unlock Block Command for given address range */
769         writew(0x4, NFC_V1_V2_WRPROT);
770 }
771
772 static void preset_v3(struct mtd_info *mtd)
773 {
774         struct nand_chip *chip = mtd->priv;
775         struct mxc_nand_host *host = chip->priv;
776         uint32_t config2, config3;
777         int i, addr_phases;
778
779         writel(NFC_V3_CONFIG1_RBA(0), NFC_V3_CONFIG1);
780         writel(NFC_V3_IPC_CREQ, NFC_V3_IPC);
781
782         /* Unlock the internal RAM Buffer */
783         writel(NFC_V3_WRPROT_BLS_UNLOCK | NFC_V3_WRPROT_UNLOCK,
784                         NFC_V3_WRPROT);
785
786         /* Blocks to be unlocked */
787         for (i = 0; i < NAND_MAX_CHIPS; i++)
788                 writel(0x0 |    (0xffff << 16),
789                                 NFC_V3_WRPROT_UNLOCK_BLK_ADD0 + (i << 2));
790
791         writel(0, NFC_V3_IPC);
792
793         config2 = NFC_V3_CONFIG2_ONE_CYCLE |
794                 NFC_V3_CONFIG2_2CMD_PHASES |
795                 NFC_V3_CONFIG2_SPAS(mtd->oobsize >> 1) |
796                 NFC_V3_CONFIG2_ST_CMD(0x70) |
797                 NFC_V3_CONFIG2_NUM_ADDR_PHASE0;
798
799         if (chip->ecc.mode == NAND_ECC_HW)
800                 config2 |= NFC_V3_CONFIG2_ECC_EN;
801
802         addr_phases = fls(chip->pagemask) >> 3;
803
804         if (mtd->writesize == 2048) {
805                 config2 |= NFC_V3_CONFIG2_PS_2048;
806                 config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases);
807         } else if (mtd->writesize == 4096) {
808                 config2 |= NFC_V3_CONFIG2_PS_4096;
809                 config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases);
810         } else {
811                 config2 |= NFC_V3_CONFIG2_PS_512;
812                 config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases - 1);
813         }
814
815         if (mtd->writesize) {
816                 config2 |= NFC_V3_CONFIG2_PPB(ffs(mtd->erasesize / mtd->writesize) - 6);
817                 host->eccsize = get_eccsize(mtd);
818                 if (host->eccsize == 8)
819                         config2 |= NFC_V3_CONFIG2_ECC_MODE_8;
820         }
821
822         writel(config2, NFC_V3_CONFIG2);
823
824         config3 = NFC_V3_CONFIG3_NUM_OF_DEVICES(0) |
825                         NFC_V3_CONFIG3_NO_SDMA |
826                         NFC_V3_CONFIG3_RBB_MODE |
827                         NFC_V3_CONFIG3_SBB(6) | /* Reset default */
828                         NFC_V3_CONFIG3_ADD_OP(0);
829
830         if (!(chip->options & NAND_BUSWIDTH_16))
831                 config3 |= NFC_V3_CONFIG3_FW8;
832
833         writel(config3, NFC_V3_CONFIG3);
834
835         writel(0, NFC_V3_DELAY_LINE);
836 }
837
838 /* Used by the upper layer to write command to NAND Flash for
839  * different operations to be carried out on NAND Flash */
840 static void mxc_nand_command(struct mtd_info *mtd, unsigned command,
841                                 int column, int page_addr)
842 {
843         struct nand_chip *nand_chip = mtd->priv;
844         struct mxc_nand_host *host = nand_chip->priv;
845
846         DEBUG(MTD_DEBUG_LEVEL3,
847               "mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n",
848               command, column, page_addr);
849
850         /* Reset command state information */
851         host->status_request = false;
852
853         /* Command pre-processing step */
854         switch (command) {
855         case NAND_CMD_RESET:
856                 host->preset(mtd);
857                 host->send_cmd(host, command, false);
858                 break;
859
860         case NAND_CMD_STATUS:
861                 host->buf_start = 0;
862                 host->status_request = true;
863
864                 host->send_cmd(host, command, true);
865                 mxc_do_addr_cycle(mtd, column, page_addr);
866                 break;
867
868         case NAND_CMD_READ0:
869         case NAND_CMD_READOOB:
870                 if (command == NAND_CMD_READ0)
871                         host->buf_start = column;
872                 else
873                         host->buf_start = column + mtd->writesize;
874
875                 command = NAND_CMD_READ0; /* only READ0 is valid */
876
877                 host->send_cmd(host, command, false);
878                 mxc_do_addr_cycle(mtd, column, page_addr);
879
880                 if (mtd->writesize > 512)
881                         host->send_cmd(host, NAND_CMD_READSTART, true);
882
883                 host->send_page(mtd, NFC_OUTPUT);
884
885                 memcpy(host->data_buf, host->main_area0, mtd->writesize);
886                 copy_spare(mtd, true);
887                 break;
888
889         case NAND_CMD_SEQIN:
890                 if (column >= mtd->writesize)
891                         /* call ourself to read a page */
892                         mxc_nand_command(mtd, NAND_CMD_READ0, 0, page_addr);
893
894                 host->buf_start = column;
895
896                 host->send_cmd(host, command, false);
897                 mxc_do_addr_cycle(mtd, column, page_addr);
898                 break;
899
900         case NAND_CMD_PAGEPROG:
901                 memcpy(host->main_area0, host->data_buf, mtd->writesize);
902                 copy_spare(mtd, false);
903                 host->send_page(mtd, NFC_INPUT);
904                 host->send_cmd(host, command, true);
905                 mxc_do_addr_cycle(mtd, column, page_addr);
906                 break;
907
908         case NAND_CMD_READID:
909                 host->send_cmd(host, command, true);
910                 mxc_do_addr_cycle(mtd, column, page_addr);
911                 host->send_read_id(host);
912                 host->buf_start = column;
913                 break;
914
915         case NAND_CMD_ERASE1:
916         case NAND_CMD_ERASE2:
917                 host->send_cmd(host, command, false);
918                 mxc_do_addr_cycle(mtd, column, page_addr);
919
920                 break;
921         }
922 }
923
924 /*
925  * The generic flash bbt decriptors overlap with our ecc
926  * hardware, so define some i.MX specific ones.
927  */
928 static uint8_t bbt_pattern[] = { 'B', 'b', 't', '0' };
929 static uint8_t mirror_pattern[] = { '1', 't', 'b', 'B' };
930
931 static struct nand_bbt_descr bbt_main_descr = {
932         .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
933             | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
934         .offs = 0,
935         .len = 4,
936         .veroffs = 4,
937         .maxblocks = 4,
938         .pattern = bbt_pattern,
939 };
940
941 static struct nand_bbt_descr bbt_mirror_descr = {
942         .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
943             | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
944         .offs = 0,
945         .len = 4,
946         .veroffs = 4,
947         .maxblocks = 4,
948         .pattern = mirror_pattern,
949 };
950
951 static int __init mxcnd_probe(struct platform_device *pdev)
952 {
953         struct nand_chip *this;
954         struct mtd_info *mtd;
955         struct mxc_nand_platform_data *pdata = pdev->dev.platform_data;
956         struct mxc_nand_host *host;
957         struct resource *res;
958         int err = 0, nr_parts = 0;
959         struct nand_ecclayout *oob_smallpage, *oob_largepage;
960
961         /* Allocate memory for MTD device structure and private data */
962         host = kzalloc(sizeof(struct mxc_nand_host) + NAND_MAX_PAGESIZE +
963                         NAND_MAX_OOBSIZE, GFP_KERNEL);
964         if (!host)
965                 return -ENOMEM;
966
967         host->data_buf = (uint8_t *)(host + 1);
968
969         host->dev = &pdev->dev;
970         /* structures must be linked */
971         this = &host->nand;
972         mtd = &host->mtd;
973         mtd->priv = this;
974         mtd->owner = THIS_MODULE;
975         mtd->dev.parent = &pdev->dev;
976         mtd->name = DRIVER_NAME;
977
978         /* 50 us command delay time */
979         this->chip_delay = 5;
980
981         this->priv = host;
982         this->dev_ready = mxc_nand_dev_ready;
983         this->cmdfunc = mxc_nand_command;
984         this->select_chip = mxc_nand_select_chip;
985         this->read_byte = mxc_nand_read_byte;
986         this->read_word = mxc_nand_read_word;
987         this->write_buf = mxc_nand_write_buf;
988         this->read_buf = mxc_nand_read_buf;
989         this->verify_buf = mxc_nand_verify_buf;
990
991         host->clk = clk_get(&pdev->dev, "nfc");
992         if (IS_ERR(host->clk)) {
993                 err = PTR_ERR(host->clk);
994                 goto eclk;
995         }
996
997         clk_enable(host->clk);
998         host->clk_act = 1;
999
1000         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1001         if (!res) {
1002                 err = -ENODEV;
1003                 goto eres;
1004         }
1005
1006         host->base = ioremap(res->start, resource_size(res));
1007         if (!host->base) {
1008                 err = -ENOMEM;
1009                 goto eres;
1010         }
1011
1012         host->main_area0 = host->base;
1013
1014         if (nfc_is_v1() || nfc_is_v21()) {
1015                 host->preset = preset_v1_v2;
1016                 host->send_cmd = send_cmd_v1_v2;
1017                 host->send_addr = send_addr_v1_v2;
1018                 host->send_page = send_page_v1_v2;
1019                 host->send_read_id = send_read_id_v1_v2;
1020                 host->get_dev_status = get_dev_status_v1_v2;
1021                 host->check_int = check_int_v1_v2;
1022         }
1023
1024         if (nfc_is_v21()) {
1025                 host->regs = host->base + 0x1e00;
1026                 host->spare0 = host->base + 0x1000;
1027                 host->spare_len = 64;
1028                 oob_smallpage = &nandv2_hw_eccoob_smallpage;
1029                 oob_largepage = &nandv2_hw_eccoob_largepage;
1030                 this->ecc.bytes = 9;
1031         } else if (nfc_is_v1()) {
1032                 host->regs = host->base + 0xe00;
1033                 host->spare0 = host->base + 0x800;
1034                 host->spare_len = 16;
1035                 oob_smallpage = &nandv1_hw_eccoob_smallpage;
1036                 oob_largepage = &nandv1_hw_eccoob_largepage;
1037                 this->ecc.bytes = 3;
1038                 host->eccsize = 1;
1039         } else if (nfc_is_v3_2()) {
1040                 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1041                 if (!res) {
1042                         err = -ENODEV;
1043                         goto eirq;
1044                 }
1045                 host->regs_ip = ioremap(res->start, resource_size(res));
1046                 if (!host->regs_ip) {
1047                         err = -ENOMEM;
1048                         goto eirq;
1049                 }
1050                 host->regs_axi = host->base + 0x1e00;
1051                 host->spare0 = host->base + 0x1000;
1052                 host->spare_len = 64;
1053                 host->preset = preset_v3;
1054                 host->send_cmd = send_cmd_v3;
1055                 host->send_addr = send_addr_v3;
1056                 host->send_page = send_page_v3;
1057                 host->send_read_id = send_read_id_v3;
1058                 host->check_int = check_int_v3;
1059                 host->get_dev_status = get_dev_status_v3;
1060                 oob_smallpage = &nandv2_hw_eccoob_smallpage;
1061                 oob_largepage = &nandv2_hw_eccoob_largepage;
1062         } else
1063                 BUG();
1064
1065         this->ecc.size = 512;
1066         this->ecc.layout = oob_smallpage;
1067
1068         if (pdata->hw_ecc) {
1069                 this->ecc.calculate = mxc_nand_calculate_ecc;
1070                 this->ecc.hwctl = mxc_nand_enable_hwecc;
1071                 if (nfc_is_v1())
1072                         this->ecc.correct = mxc_nand_correct_data_v1;
1073                 else
1074                         this->ecc.correct = mxc_nand_correct_data_v2_v3;
1075                 this->ecc.mode = NAND_ECC_HW;
1076         } else {
1077                 this->ecc.mode = NAND_ECC_SOFT;
1078         }
1079
1080         /* NAND bus width determines access funtions used by upper layer */
1081         if (pdata->width == 2)
1082                 this->options |= NAND_BUSWIDTH_16;
1083
1084         if (pdata->flash_bbt) {
1085                 this->bbt_td = &bbt_main_descr;
1086                 this->bbt_md = &bbt_mirror_descr;
1087                 /* update flash based bbt */
1088                 this->options |= NAND_USE_FLASH_BBT;
1089         }
1090
1091         init_waitqueue_head(&host->irq_waitq);
1092
1093         host->irq = platform_get_irq(pdev, 0);
1094
1095         err = request_irq(host->irq, mxc_nfc_irq, IRQF_DISABLED, DRIVER_NAME, host);
1096         if (err)
1097                 goto eirq;
1098
1099         /* first scan to find the device and get the page size */
1100         if (nand_scan_ident(mtd, 1, NULL)) {
1101                 err = -ENXIO;
1102                 goto escan;
1103         }
1104
1105         /* Call preset again, with correct writesize this time */
1106         host->preset(mtd);
1107
1108         if (mtd->writesize == 2048)
1109                 this->ecc.layout = oob_largepage;
1110
1111         /* second phase scan */
1112         if (nand_scan_tail(mtd)) {
1113                 err = -ENXIO;
1114                 goto escan;
1115         }
1116
1117         /* Register the partitions */
1118 #ifdef CONFIG_MTD_PARTITIONS
1119         nr_parts =
1120             parse_mtd_partitions(mtd, part_probes, &host->parts, 0);
1121         if (nr_parts > 0)
1122                 add_mtd_partitions(mtd, host->parts, nr_parts);
1123         else if (pdata->parts)
1124                 add_mtd_partitions(mtd, pdata->parts, pdata->nr_parts);
1125         else
1126 #endif
1127         {
1128                 pr_info("Registering %s as whole device\n", mtd->name);
1129                 add_mtd_device(mtd);
1130         }
1131
1132         platform_set_drvdata(pdev, host);
1133
1134         return 0;
1135
1136 escan:
1137         free_irq(host->irq, host);
1138 eirq:
1139         if (host->regs_ip)
1140                 iounmap(host->regs_ip);
1141         iounmap(host->base);
1142 eres:
1143         clk_put(host->clk);
1144 eclk:
1145         kfree(host);
1146
1147         return err;
1148 }
1149
1150 static int __devexit mxcnd_remove(struct platform_device *pdev)
1151 {
1152         struct mxc_nand_host *host = platform_get_drvdata(pdev);
1153
1154         clk_put(host->clk);
1155
1156         platform_set_drvdata(pdev, NULL);
1157
1158         nand_release(&host->mtd);
1159         free_irq(host->irq, host);
1160         if (host->regs_ip)
1161                 iounmap(host->regs_ip);
1162         iounmap(host->base);
1163         kfree(host);
1164
1165         return 0;
1166 }
1167
1168 static struct platform_driver mxcnd_driver = {
1169         .driver = {
1170                    .name = DRIVER_NAME,
1171         },
1172         .remove = __devexit_p(mxcnd_remove),
1173 };
1174
1175 static int __init mxc_nd_init(void)
1176 {
1177         return platform_driver_probe(&mxcnd_driver, mxcnd_probe);
1178 }
1179
1180 static void __exit mxc_nd_cleanup(void)
1181 {
1182         /* Unregister the device structure */
1183         platform_driver_unregister(&mxcnd_driver);
1184 }
1185
1186 module_init(mxc_nd_init);
1187 module_exit(mxc_nd_cleanup);
1188
1189 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
1190 MODULE_DESCRIPTION("MXC NAND MTD driver");
1191 MODULE_LICENSE("GPL");