mtd: kill MTD_NAND_VERIFY_WRITE
[profile/ivi/kernel-adaptation-intel-automotive.git] / drivers / mtd / nand / bcm_umi_nand.c
1 /*****************************************************************************
2 * Copyright 2004 - 2009 Broadcom Corporation.  All rights reserved.
3 *
4 * Unless you and Broadcom execute a separate written software license
5 * agreement governing use of this software, this software is licensed to you
6 * under the terms of the GNU General Public License version 2, available at
7 * http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
8 *
9 * Notwithstanding the above, under no circumstances may you combine this
10 * software in any way with any other Broadcom software provided under a
11 * license other than the GPL, without Broadcom's express prior written
12 * consent.
13 *****************************************************************************/
14
15 /* ---- Include Files ---------------------------------------------------- */
16 #include <linux/module.h>
17 #include <linux/types.h>
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/slab.h>
21 #include <linux/string.h>
22 #include <linux/ioport.h>
23 #include <linux/device.h>
24 #include <linux/delay.h>
25 #include <linux/err.h>
26 #include <linux/io.h>
27 #include <linux/platform_device.h>
28 #include <linux/mtd/mtd.h>
29 #include <linux/mtd/nand.h>
30 #include <linux/mtd/nand_ecc.h>
31 #include <linux/mtd/partitions.h>
32
33 #include <asm/mach-types.h>
34
35 #include <mach/reg_nand.h>
36 #include <mach/reg_umi.h>
37
38 #include "nand_bcm_umi.h"
39
40 #include <mach/memory_settings.h>
41
42 #define USE_DMA 1
43 #include <mach/dma.h>
44 #include <linux/dma-mapping.h>
45 #include <linux/completion.h>
46
47 /* ---- External Variable Declarations ----------------------------------- */
48 /* ---- External Function Prototypes ------------------------------------- */
49 /* ---- Public Variables ------------------------------------------------- */
50 /* ---- Private Constants and Types -------------------------------------- */
51 static const __devinitconst char gBanner[] = KERN_INFO \
52         "BCM UMI MTD NAND Driver: 1.00\n";
53
54 #if NAND_ECC_BCH
55 static uint8_t scan_ff_pattern[] = { 0xff };
56
57 static struct nand_bbt_descr largepage_bbt = {
58         .options = 0,
59         .offs = 0,
60         .len = 1,
61         .pattern = scan_ff_pattern
62 };
63 #endif
64
65 /*
66 ** Preallocate a buffer to avoid having to do this every dma operation.
67 ** This is the size of the preallocated coherent DMA buffer.
68 */
69 #if USE_DMA
70 #define DMA_MIN_BUFLEN  512
71 #define DMA_MAX_BUFLEN  PAGE_SIZE
72 #define USE_DIRECT_IO(len)      (((len) < DMA_MIN_BUFLEN) || \
73         ((len) > DMA_MAX_BUFLEN))
74
75 /*
76  * The current NAND data space goes from 0x80001900 to 0x80001FFF,
77  * which is only 0x700 = 1792 bytes long. This is too small for 2K, 4K page
78  * size NAND flash. Need to break the DMA down to multiple 1Ks.
79  *
80  * Need to make sure REG_NAND_DATA_PADDR + DMA_MAX_LEN < 0x80002000
81  */
82 #define DMA_MAX_LEN             1024
83
84 #else /* !USE_DMA */
85 #define DMA_MIN_BUFLEN          0
86 #define DMA_MAX_BUFLEN          0
87 #define USE_DIRECT_IO(len)      1
88 #endif
89 /* ---- Private Function Prototypes -------------------------------------- */
90 static void bcm_umi_nand_read_buf(struct mtd_info *mtd, u_char * buf, int len);
91 static void bcm_umi_nand_write_buf(struct mtd_info *mtd, const u_char * buf,
92                                    int len);
93
94 /* ---- Private Variables ------------------------------------------------ */
95 static struct mtd_info *board_mtd;
96 static void __iomem *bcm_umi_io_base;
97 static void *virtPtr;
98 static dma_addr_t physPtr;
99 static struct completion nand_comp;
100
101 /* ---- Private Functions ------------------------------------------------ */
102 #if NAND_ECC_BCH
103 #include "bcm_umi_bch.c"
104 #else
105 #include "bcm_umi_hamming.c"
106 #endif
107
108 #if USE_DMA
109
110 /* Handler called when the DMA finishes. */
111 static void nand_dma_handler(DMA_Device_t dev, int reason, void *userData)
112 {
113         complete(&nand_comp);
114 }
115
116 static int nand_dma_init(void)
117 {
118         int rc;
119
120         rc = dma_set_device_handler(DMA_DEVICE_NAND_MEM_TO_MEM,
121                 nand_dma_handler, NULL);
122         if (rc != 0) {
123                 printk(KERN_ERR "dma_set_device_handler failed: %d\n", rc);
124                 return rc;
125         }
126
127         virtPtr =
128             dma_alloc_coherent(NULL, DMA_MAX_BUFLEN, &physPtr, GFP_KERNEL);
129         if (virtPtr == NULL) {
130                 printk(KERN_ERR "NAND - Failed to allocate memory for DMA buffer\n");
131                 return -ENOMEM;
132         }
133
134         return 0;
135 }
136
137 static void nand_dma_term(void)
138 {
139         if (virtPtr != NULL)
140                 dma_free_coherent(NULL, DMA_MAX_BUFLEN, virtPtr, physPtr);
141 }
142
143 static void nand_dma_read(void *buf, int len)
144 {
145         int offset = 0;
146         int tmp_len = 0;
147         int len_left = len;
148         DMA_Handle_t hndl;
149
150         if (virtPtr == NULL)
151                 panic("nand_dma_read: virtPtr == NULL\n");
152
153         if ((void *)physPtr == NULL)
154                 panic("nand_dma_read: physPtr == NULL\n");
155
156         hndl = dma_request_channel(DMA_DEVICE_NAND_MEM_TO_MEM);
157         if (hndl < 0) {
158                 printk(KERN_ERR
159                        "nand_dma_read: unable to allocate dma channel: %d\n",
160                        (int)hndl);
161                 panic("\n");
162         }
163
164         while (len_left > 0) {
165                 if (len_left > DMA_MAX_LEN) {
166                         tmp_len = DMA_MAX_LEN;
167                         len_left -= DMA_MAX_LEN;
168                 } else {
169                         tmp_len = len_left;
170                         len_left = 0;
171                 }
172
173                 init_completion(&nand_comp);
174                 dma_transfer_mem_to_mem(hndl, REG_NAND_DATA_PADDR,
175                                         physPtr + offset, tmp_len);
176                 wait_for_completion(&nand_comp);
177
178                 offset += tmp_len;
179         }
180
181         dma_free_channel(hndl);
182
183         if (buf != NULL)
184                 memcpy(buf, virtPtr, len);
185 }
186
187 static void nand_dma_write(const void *buf, int len)
188 {
189         int offset = 0;
190         int tmp_len = 0;
191         int len_left = len;
192         DMA_Handle_t hndl;
193
194         if (buf == NULL)
195                 panic("nand_dma_write: buf == NULL\n");
196
197         if (virtPtr == NULL)
198                 panic("nand_dma_write: virtPtr == NULL\n");
199
200         if ((void *)physPtr == NULL)
201                 panic("nand_dma_write: physPtr == NULL\n");
202
203         memcpy(virtPtr, buf, len);
204
205
206         hndl = dma_request_channel(DMA_DEVICE_NAND_MEM_TO_MEM);
207         if (hndl < 0) {
208                 printk(KERN_ERR
209                        "nand_dma_write: unable to allocate dma channel: %d\n",
210                        (int)hndl);
211                 panic("\n");
212         }
213
214         while (len_left > 0) {
215                 if (len_left > DMA_MAX_LEN) {
216                         tmp_len = DMA_MAX_LEN;
217                         len_left -= DMA_MAX_LEN;
218                 } else {
219                         tmp_len = len_left;
220                         len_left = 0;
221                 }
222
223                 init_completion(&nand_comp);
224                 dma_transfer_mem_to_mem(hndl, physPtr + offset,
225                                         REG_NAND_DATA_PADDR, tmp_len);
226                 wait_for_completion(&nand_comp);
227
228                 offset += tmp_len;
229         }
230
231         dma_free_channel(hndl);
232 }
233
234 #endif
235
236 static int nand_dev_ready(struct mtd_info *mtd)
237 {
238         return nand_bcm_umi_dev_ready();
239 }
240
241 /****************************************************************************
242 *
243 *  bcm_umi_nand_inithw
244 *
245 *   This routine does the necessary hardware (board-specific)
246 *   initializations.  This includes setting up the timings, etc.
247 *
248 ***************************************************************************/
249 int bcm_umi_nand_inithw(void)
250 {
251         /* Configure nand timing parameters */
252         REG_UMI_NAND_TCR &= ~0x7ffff;
253         REG_UMI_NAND_TCR |= HW_CFG_NAND_TCR;
254
255 #if !defined(CONFIG_MTD_NAND_BCM_UMI_HWCS)
256         /* enable software control of CS */
257         REG_UMI_NAND_TCR |= REG_UMI_NAND_TCR_CS_SWCTRL;
258 #endif
259
260         /* keep NAND chip select asserted */
261         REG_UMI_NAND_RCSR |= REG_UMI_NAND_RCSR_CS_ASSERTED;
262
263         REG_UMI_NAND_TCR &= ~REG_UMI_NAND_TCR_WORD16;
264         /* enable writes to flash */
265         REG_UMI_MMD_ICR |= REG_UMI_MMD_ICR_FLASH_WP;
266
267         writel(NAND_CMD_RESET, bcm_umi_io_base + REG_NAND_CMD_OFFSET);
268         nand_bcm_umi_wait_till_ready();
269
270 #if NAND_ECC_BCH
271         nand_bcm_umi_bch_config_ecc(NAND_ECC_NUM_BYTES);
272 #endif
273
274         return 0;
275 }
276
277 /* Used to turn latch the proper register for access. */
278 static void bcm_umi_nand_hwcontrol(struct mtd_info *mtd, int cmd,
279                                    unsigned int ctrl)
280 {
281         /* send command to hardware */
282         struct nand_chip *chip = mtd->priv;
283         if (ctrl & NAND_CTRL_CHANGE) {
284                 if (ctrl & NAND_CLE) {
285                         chip->IO_ADDR_W = bcm_umi_io_base + REG_NAND_CMD_OFFSET;
286                         goto CMD;
287                 }
288                 if (ctrl & NAND_ALE) {
289                         chip->IO_ADDR_W =
290                             bcm_umi_io_base + REG_NAND_ADDR_OFFSET;
291                         goto CMD;
292                 }
293                 chip->IO_ADDR_W = bcm_umi_io_base + REG_NAND_DATA8_OFFSET;
294         }
295
296 CMD:
297         /* Send command to chip directly */
298         if (cmd != NAND_CMD_NONE)
299                 writeb(cmd, chip->IO_ADDR_W);
300 }
301
302 static void bcm_umi_nand_write_buf(struct mtd_info *mtd, const u_char * buf,
303                                    int len)
304 {
305         if (USE_DIRECT_IO(len)) {
306                 /* Do it the old way if the buffer is small or too large.
307                  * Probably quicker than starting and checking dma. */
308                 int i;
309                 struct nand_chip *this = mtd->priv;
310
311                 for (i = 0; i < len; i++)
312                         writeb(buf[i], this->IO_ADDR_W);
313         }
314 #if USE_DMA
315         else
316                 nand_dma_write(buf, len);
317 #endif
318 }
319
320 static void bcm_umi_nand_read_buf(struct mtd_info *mtd, u_char * buf, int len)
321 {
322         if (USE_DIRECT_IO(len)) {
323                 int i;
324                 struct nand_chip *this = mtd->priv;
325
326                 for (i = 0; i < len; i++)
327                         buf[i] = readb(this->IO_ADDR_R);
328         }
329 #if USE_DMA
330         else
331                 nand_dma_read(buf, len);
332 #endif
333 }
334
335 static int __devinit bcm_umi_nand_probe(struct platform_device *pdev)
336 {
337         struct nand_chip *this;
338         struct resource *r;
339         int err = 0;
340
341         printk(gBanner);
342
343         /* Allocate memory for MTD device structure and private data */
344         board_mtd =
345             kmalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip),
346                     GFP_KERNEL);
347         if (!board_mtd) {
348                 printk(KERN_WARNING
349                        "Unable to allocate NAND MTD device structure.\n");
350                 return -ENOMEM;
351         }
352
353         r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
354
355         if (!r) {
356                 err = -ENXIO;
357                 goto out_free;
358         }
359
360         /* map physical address */
361         bcm_umi_io_base = ioremap(r->start, resource_size(r));
362
363         if (!bcm_umi_io_base) {
364                 printk(KERN_ERR "ioremap to access BCM UMI NAND chip failed\n");
365                 err = -EIO;
366                 goto out_free;
367         }
368
369         /* Get pointer to private data */
370         this = (struct nand_chip *)(&board_mtd[1]);
371
372         /* Initialize structures */
373         memset((char *)board_mtd, 0, sizeof(struct mtd_info));
374         memset((char *)this, 0, sizeof(struct nand_chip));
375
376         /* Link the private data with the MTD structure */
377         board_mtd->priv = this;
378
379         /* Initialize the NAND hardware.  */
380         if (bcm_umi_nand_inithw() < 0) {
381                 printk(KERN_ERR "BCM UMI NAND chip could not be initialized\n");
382                 err = -EIO;
383                 goto out_unmap;
384         }
385
386         /* Set address of NAND IO lines */
387         this->IO_ADDR_W = bcm_umi_io_base + REG_NAND_DATA8_OFFSET;
388         this->IO_ADDR_R = bcm_umi_io_base + REG_NAND_DATA8_OFFSET;
389
390         /* Set command delay time, see datasheet for correct value */
391         this->chip_delay = 0;
392         /* Assign the device ready function, if available */
393         this->dev_ready = nand_dev_ready;
394         this->options = 0;
395
396         this->write_buf = bcm_umi_nand_write_buf;
397         this->read_buf = bcm_umi_nand_read_buf;
398
399         this->cmd_ctrl = bcm_umi_nand_hwcontrol;
400         this->ecc.mode = NAND_ECC_HW;
401         this->ecc.size = 512;
402         this->ecc.bytes = NAND_ECC_NUM_BYTES;
403 #if NAND_ECC_BCH
404         this->ecc.read_page = bcm_umi_bch_read_page_hwecc;
405         this->ecc.write_page = bcm_umi_bch_write_page_hwecc;
406 #else
407         this->ecc.correct = nand_correct_data512;
408         this->ecc.calculate = bcm_umi_hamming_get_hw_ecc;
409         this->ecc.hwctl = bcm_umi_hamming_enable_hwecc;
410 #endif
411
412 #if USE_DMA
413         err = nand_dma_init();
414         if (err != 0)
415                 goto out_unmap;
416 #endif
417
418         /* Figure out the size of the device that we have.
419          * We need to do this to figure out which ECC
420          * layout we'll be using.
421          */
422
423         err = nand_scan_ident(board_mtd, 1, NULL);
424         if (err) {
425                 printk(KERN_ERR "nand_scan failed: %d\n", err);
426                 goto out_unmap;
427         }
428
429         /* Now that we know the nand size, we can setup the ECC layout */
430
431         switch (board_mtd->writesize) { /* writesize is the pagesize */
432         case 4096:
433                 this->ecc.layout = &nand_hw_eccoob_4096;
434                 break;
435         case 2048:
436                 this->ecc.layout = &nand_hw_eccoob_2048;
437                 break;
438         case 512:
439                 this->ecc.layout = &nand_hw_eccoob_512;
440                 break;
441         default:
442                 {
443                         printk(KERN_ERR "NAND - Unrecognized pagesize: %d\n",
444                                          board_mtd->writesize);
445                         err = -EINVAL;
446                         goto out_unmap;
447                 }
448         }
449
450 #if NAND_ECC_BCH
451         if (board_mtd->writesize > 512) {
452                 if (this->bbt_options & NAND_BBT_USE_FLASH)
453                         largepage_bbt.options = NAND_BBT_SCAN2NDPAGE;
454                 this->badblock_pattern = &largepage_bbt;
455         }
456
457         this->ecc.strength = 8;
458
459 #endif
460
461         /* Now finish off the scan, now that ecc.layout has been initialized. */
462
463         err = nand_scan_tail(board_mtd);
464         if (err) {
465                 printk(KERN_ERR "nand_scan failed: %d\n", err);
466                 goto out_unmap;
467         }
468
469         /* Register the partitions */
470         board_mtd->name = "bcm_umi-nand";
471         mtd_device_parse_register(board_mtd, NULL, NULL, NULL, 0);
472
473         /* Return happy */
474         return 0;
475 out_unmap:
476         iounmap(bcm_umi_io_base);
477 out_free:
478         kfree(board_mtd);
479         return err;
480 }
481
482 static int bcm_umi_nand_remove(struct platform_device *pdev)
483 {
484 #if USE_DMA
485         nand_dma_term();
486 #endif
487
488         /* Release resources, unregister device */
489         nand_release(board_mtd);
490
491         /* unmap physical address */
492         iounmap(bcm_umi_io_base);
493
494         /* Free the MTD device structure */
495         kfree(board_mtd);
496
497         return 0;
498 }
499
500 #ifdef CONFIG_PM
501 static int bcm_umi_nand_suspend(struct platform_device *pdev,
502                                 pm_message_t state)
503 {
504         printk(KERN_ERR "MTD NAND suspend is being called\n");
505         return 0;
506 }
507
508 static int bcm_umi_nand_resume(struct platform_device *pdev)
509 {
510         printk(KERN_ERR "MTD NAND resume is being called\n");
511         return 0;
512 }
513 #else
514 #define bcm_umi_nand_suspend   NULL
515 #define bcm_umi_nand_resume    NULL
516 #endif
517
518 static struct platform_driver nand_driver = {
519         .driver = {
520                    .name = "bcm-nand",
521                    .owner = THIS_MODULE,
522                    },
523         .probe = bcm_umi_nand_probe,
524         .remove = bcm_umi_nand_remove,
525         .suspend = bcm_umi_nand_suspend,
526         .resume = bcm_umi_nand_resume,
527 };
528
529 module_platform_driver(nand_driver);
530
531 MODULE_LICENSE("GPL");
532 MODULE_AUTHOR("Broadcom");
533 MODULE_DESCRIPTION("BCM UMI MTD NAND driver");