Staging: strip: delete the driver
[linux/fpc-iii.git] / drivers / mtd / nand / bcm_umi_nand.c
blobc997f98eeb3de717730fc3aee2395a9c61e7f31b
1 /*****************************************************************************
2 * Copyright 2004 - 2009 Broadcom Corporation. All rights reserved.
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").
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 *****************************************************************************/
15 /* ---- Include Files ---------------------------------------------------- */
16 #include <linux/version.h>
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/slab.h>
22 #include <linux/string.h>
23 #include <linux/ioport.h>
24 #include <linux/device.h>
25 #include <linux/delay.h>
26 #include <linux/err.h>
27 #include <linux/io.h>
28 #include <linux/platform_device.h>
29 #include <linux/mtd/mtd.h>
30 #include <linux/mtd/nand.h>
31 #include <linux/mtd/nand_ecc.h>
32 #include <linux/mtd/partitions.h>
34 #include <asm/mach-types.h>
35 #include <asm/system.h>
37 #include <mach/reg_nand.h>
38 #include <mach/reg_umi.h>
40 #include "nand_bcm_umi.h"
42 #include <mach/memory_settings.h>
44 #define USE_DMA 1
45 #include <mach/dma.h>
46 #include <linux/dma-mapping.h>
47 #include <linux/completion.h>
49 /* ---- External Variable Declarations ----------------------------------- */
50 /* ---- External Function Prototypes ------------------------------------- */
51 /* ---- Public Variables ------------------------------------------------- */
52 /* ---- Private Constants and Types -------------------------------------- */
53 static const __devinitconst char gBanner[] = KERN_INFO \
54 "BCM UMI MTD NAND Driver: 1.00\n";
56 #ifdef CONFIG_MTD_PARTITIONS
57 const char *part_probes[] = { "cmdlinepart", NULL };
58 #endif
60 #if NAND_ECC_BCH
61 static uint8_t scan_ff_pattern[] = { 0xff };
63 static struct nand_bbt_descr largepage_bbt = {
64 .options = 0,
65 .offs = 0,
66 .len = 1,
67 .pattern = scan_ff_pattern
69 #endif
72 ** Preallocate a buffer to avoid having to do this every dma operation.
73 ** This is the size of the preallocated coherent DMA buffer.
75 #if USE_DMA
76 #define DMA_MIN_BUFLEN 512
77 #define DMA_MAX_BUFLEN PAGE_SIZE
78 #define USE_DIRECT_IO(len) (((len) < DMA_MIN_BUFLEN) || \
79 ((len) > DMA_MAX_BUFLEN))
82 * The current NAND data space goes from 0x80001900 to 0x80001FFF,
83 * which is only 0x700 = 1792 bytes long. This is too small for 2K, 4K page
84 * size NAND flash. Need to break the DMA down to multiple 1Ks.
86 * Need to make sure REG_NAND_DATA_PADDR + DMA_MAX_LEN < 0x80002000
88 #define DMA_MAX_LEN 1024
90 #else /* !USE_DMA */
91 #define DMA_MIN_BUFLEN 0
92 #define DMA_MAX_BUFLEN 0
93 #define USE_DIRECT_IO(len) 1
94 #endif
95 /* ---- Private Function Prototypes -------------------------------------- */
96 static void bcm_umi_nand_read_buf(struct mtd_info *mtd, u_char * buf, int len);
97 static void bcm_umi_nand_write_buf(struct mtd_info *mtd, const u_char * buf,
98 int len);
100 /* ---- Private Variables ------------------------------------------------ */
101 static struct mtd_info *board_mtd;
102 static void __iomem *bcm_umi_io_base;
103 static void *virtPtr;
104 static dma_addr_t physPtr;
105 static struct completion nand_comp;
107 /* ---- Private Functions ------------------------------------------------ */
108 #if NAND_ECC_BCH
109 #include "bcm_umi_bch.c"
110 #else
111 #include "bcm_umi_hamming.c"
112 #endif
114 #if USE_DMA
116 /* Handler called when the DMA finishes. */
117 static void nand_dma_handler(DMA_Device_t dev, int reason, void *userData)
119 complete(&nand_comp);
122 static int nand_dma_init(void)
124 int rc;
126 rc = dma_set_device_handler(DMA_DEVICE_NAND_MEM_TO_MEM,
127 nand_dma_handler, NULL);
128 if (rc != 0) {
129 printk(KERN_ERR "dma_set_device_handler failed: %d\n", rc);
130 return rc;
133 virtPtr =
134 dma_alloc_coherent(NULL, DMA_MAX_BUFLEN, &physPtr, GFP_KERNEL);
135 if (virtPtr == NULL) {
136 printk(KERN_ERR "NAND - Failed to allocate memory for DMA buffer\n");
137 return -ENOMEM;
140 return 0;
143 static void nand_dma_term(void)
145 if (virtPtr != NULL)
146 dma_free_coherent(NULL, DMA_MAX_BUFLEN, virtPtr, physPtr);
149 static void nand_dma_read(void *buf, int len)
151 int offset = 0;
152 int tmp_len = 0;
153 int len_left = len;
154 DMA_Handle_t hndl;
156 if (virtPtr == NULL)
157 panic("nand_dma_read: virtPtr == NULL\n");
159 if ((void *)physPtr == NULL)
160 panic("nand_dma_read: physPtr == NULL\n");
162 hndl = dma_request_channel(DMA_DEVICE_NAND_MEM_TO_MEM);
163 if (hndl < 0) {
164 printk(KERN_ERR
165 "nand_dma_read: unable to allocate dma channel: %d\n",
166 (int)hndl);
167 panic("\n");
170 while (len_left > 0) {
171 if (len_left > DMA_MAX_LEN) {
172 tmp_len = DMA_MAX_LEN;
173 len_left -= DMA_MAX_LEN;
174 } else {
175 tmp_len = len_left;
176 len_left = 0;
179 init_completion(&nand_comp);
180 dma_transfer_mem_to_mem(hndl, REG_NAND_DATA_PADDR,
181 physPtr + offset, tmp_len);
182 wait_for_completion(&nand_comp);
184 offset += tmp_len;
187 dma_free_channel(hndl);
189 if (buf != NULL)
190 memcpy(buf, virtPtr, len);
193 static void nand_dma_write(const void *buf, int len)
195 int offset = 0;
196 int tmp_len = 0;
197 int len_left = len;
198 DMA_Handle_t hndl;
200 if (buf == NULL)
201 panic("nand_dma_write: buf == NULL\n");
203 if (virtPtr == NULL)
204 panic("nand_dma_write: virtPtr == NULL\n");
206 if ((void *)physPtr == NULL)
207 panic("nand_dma_write: physPtr == NULL\n");
209 memcpy(virtPtr, buf, len);
212 hndl = dma_request_channel(DMA_DEVICE_NAND_MEM_TO_MEM);
213 if (hndl < 0) {
214 printk(KERN_ERR
215 "nand_dma_write: unable to allocate dma channel: %d\n",
216 (int)hndl);
217 panic("\n");
220 while (len_left > 0) {
221 if (len_left > DMA_MAX_LEN) {
222 tmp_len = DMA_MAX_LEN;
223 len_left -= DMA_MAX_LEN;
224 } else {
225 tmp_len = len_left;
226 len_left = 0;
229 init_completion(&nand_comp);
230 dma_transfer_mem_to_mem(hndl, physPtr + offset,
231 REG_NAND_DATA_PADDR, tmp_len);
232 wait_for_completion(&nand_comp);
234 offset += tmp_len;
237 dma_free_channel(hndl);
240 #endif
242 static int nand_dev_ready(struct mtd_info *mtd)
244 return nand_bcm_umi_dev_ready();
247 /****************************************************************************
249 * bcm_umi_nand_inithw
251 * This routine does the necessary hardware (board-specific)
252 * initializations. This includes setting up the timings, etc.
254 ***************************************************************************/
255 int bcm_umi_nand_inithw(void)
257 /* Configure nand timing parameters */
258 REG_UMI_NAND_TCR &= ~0x7ffff;
259 REG_UMI_NAND_TCR |= HW_CFG_NAND_TCR;
261 #if !defined(CONFIG_MTD_NAND_BCM_UMI_HWCS)
262 /* enable software control of CS */
263 REG_UMI_NAND_TCR |= REG_UMI_NAND_TCR_CS_SWCTRL;
264 #endif
266 /* keep NAND chip select asserted */
267 REG_UMI_NAND_RCSR |= REG_UMI_NAND_RCSR_CS_ASSERTED;
269 REG_UMI_NAND_TCR &= ~REG_UMI_NAND_TCR_WORD16;
270 /* enable writes to flash */
271 REG_UMI_MMD_ICR |= REG_UMI_MMD_ICR_FLASH_WP;
273 writel(NAND_CMD_RESET, bcm_umi_io_base + REG_NAND_CMD_OFFSET);
274 nand_bcm_umi_wait_till_ready();
276 #if NAND_ECC_BCH
277 nand_bcm_umi_bch_config_ecc(NAND_ECC_NUM_BYTES);
278 #endif
280 return 0;
283 /* Used to turn latch the proper register for access. */
284 static void bcm_umi_nand_hwcontrol(struct mtd_info *mtd, int cmd,
285 unsigned int ctrl)
287 /* send command to hardware */
288 struct nand_chip *chip = mtd->priv;
289 if (ctrl & NAND_CTRL_CHANGE) {
290 if (ctrl & NAND_CLE) {
291 chip->IO_ADDR_W = bcm_umi_io_base + REG_NAND_CMD_OFFSET;
292 goto CMD;
294 if (ctrl & NAND_ALE) {
295 chip->IO_ADDR_W =
296 bcm_umi_io_base + REG_NAND_ADDR_OFFSET;
297 goto CMD;
299 chip->IO_ADDR_W = bcm_umi_io_base + REG_NAND_DATA8_OFFSET;
302 CMD:
303 /* Send command to chip directly */
304 if (cmd != NAND_CMD_NONE)
305 writeb(cmd, chip->IO_ADDR_W);
308 static void bcm_umi_nand_write_buf(struct mtd_info *mtd, const u_char * buf,
309 int len)
311 if (USE_DIRECT_IO(len)) {
312 /* Do it the old way if the buffer is small or too large.
313 * Probably quicker than starting and checking dma. */
314 int i;
315 struct nand_chip *this = mtd->priv;
317 for (i = 0; i < len; i++)
318 writeb(buf[i], this->IO_ADDR_W);
320 #if USE_DMA
321 else
322 nand_dma_write(buf, len);
323 #endif
326 static void bcm_umi_nand_read_buf(struct mtd_info *mtd, u_char * buf, int len)
328 if (USE_DIRECT_IO(len)) {
329 int i;
330 struct nand_chip *this = mtd->priv;
332 for (i = 0; i < len; i++)
333 buf[i] = readb(this->IO_ADDR_R);
335 #if USE_DMA
336 else
337 nand_dma_read(buf, len);
338 #endif
341 static uint8_t readbackbuf[NAND_MAX_PAGESIZE];
342 static int bcm_umi_nand_verify_buf(struct mtd_info *mtd, const u_char * buf,
343 int len)
346 * Try to readback page with ECC correction. This is necessary
347 * for MLC parts which may have permanently stuck bits.
349 struct nand_chip *chip = mtd->priv;
350 int ret = chip->ecc.read_page(mtd, chip, readbackbuf, 0);
351 if (ret < 0)
352 return -EFAULT;
353 else {
354 if (memcmp(readbackbuf, buf, len) == 0)
355 return 0;
357 return -EFAULT;
359 return 0;
362 static int __devinit bcm_umi_nand_probe(struct platform_device *pdev)
364 struct nand_chip *this;
365 struct resource *r;
366 int err = 0;
368 printk(gBanner);
370 /* Allocate memory for MTD device structure and private data */
371 board_mtd =
372 kmalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip),
373 GFP_KERNEL);
374 if (!board_mtd) {
375 printk(KERN_WARNING
376 "Unable to allocate NAND MTD device structure.\n");
377 return -ENOMEM;
380 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
382 if (!r)
383 return -ENXIO;
385 /* map physical address */
386 bcm_umi_io_base = ioremap(r->start, r->end - r->start + 1);
388 if (!bcm_umi_io_base) {
389 printk(KERN_ERR "ioremap to access BCM UMI NAND chip failed\n");
390 kfree(board_mtd);
391 return -EIO;
394 /* Get pointer to private data */
395 this = (struct nand_chip *)(&board_mtd[1]);
397 /* Initialize structures */
398 memset((char *)board_mtd, 0, sizeof(struct mtd_info));
399 memset((char *)this, 0, sizeof(struct nand_chip));
401 /* Link the private data with the MTD structure */
402 board_mtd->priv = this;
404 /* Initialize the NAND hardware. */
405 if (bcm_umi_nand_inithw() < 0) {
406 printk(KERN_ERR "BCM UMI NAND chip could not be initialized\n");
407 iounmap(bcm_umi_io_base);
408 kfree(board_mtd);
409 return -EIO;
412 /* Set address of NAND IO lines */
413 this->IO_ADDR_W = bcm_umi_io_base + REG_NAND_DATA8_OFFSET;
414 this->IO_ADDR_R = bcm_umi_io_base + REG_NAND_DATA8_OFFSET;
416 /* Set command delay time, see datasheet for correct value */
417 this->chip_delay = 0;
418 /* Assign the device ready function, if available */
419 this->dev_ready = nand_dev_ready;
420 this->options = 0;
422 this->write_buf = bcm_umi_nand_write_buf;
423 this->read_buf = bcm_umi_nand_read_buf;
424 this->verify_buf = bcm_umi_nand_verify_buf;
426 this->cmd_ctrl = bcm_umi_nand_hwcontrol;
427 this->ecc.mode = NAND_ECC_HW;
428 this->ecc.size = 512;
429 this->ecc.bytes = NAND_ECC_NUM_BYTES;
430 #if NAND_ECC_BCH
431 this->ecc.read_page = bcm_umi_bch_read_page_hwecc;
432 this->ecc.write_page = bcm_umi_bch_write_page_hwecc;
433 #else
434 this->ecc.correct = nand_correct_data512;
435 this->ecc.calculate = bcm_umi_hamming_get_hw_ecc;
436 this->ecc.hwctl = bcm_umi_hamming_enable_hwecc;
437 #endif
439 #if USE_DMA
440 err = nand_dma_init();
441 if (err != 0)
442 return err;
443 #endif
445 /* Figure out the size of the device that we have.
446 * We need to do this to figure out which ECC
447 * layout we'll be using.
450 err = nand_scan_ident(board_mtd, 1);
451 if (err) {
452 printk(KERN_ERR "nand_scan failed: %d\n", err);
453 iounmap(bcm_umi_io_base);
454 kfree(board_mtd);
455 return err;
458 /* Now that we know the nand size, we can setup the ECC layout */
460 switch (board_mtd->writesize) { /* writesize is the pagesize */
461 case 4096:
462 this->ecc.layout = &nand_hw_eccoob_4096;
463 break;
464 case 2048:
465 this->ecc.layout = &nand_hw_eccoob_2048;
466 break;
467 case 512:
468 this->ecc.layout = &nand_hw_eccoob_512;
469 break;
470 default:
472 printk(KERN_ERR "NAND - Unrecognized pagesize: %d\n",
473 board_mtd->writesize);
474 return -EINVAL;
478 #if NAND_ECC_BCH
479 if (board_mtd->writesize > 512) {
480 if (this->options & NAND_USE_FLASH_BBT)
481 largepage_bbt.options = NAND_BBT_SCAN2NDPAGE;
482 this->badblock_pattern = &largepage_bbt;
484 #endif
486 /* Now finish off the scan, now that ecc.layout has been initialized. */
488 err = nand_scan_tail(board_mtd);
489 if (err) {
490 printk(KERN_ERR "nand_scan failed: %d\n", err);
491 iounmap(bcm_umi_io_base);
492 kfree(board_mtd);
493 return err;
496 /* Register the partitions */
498 int nr_partitions;
499 struct mtd_partition *partition_info;
501 board_mtd->name = "bcm_umi-nand";
502 nr_partitions =
503 parse_mtd_partitions(board_mtd, part_probes,
504 &partition_info, 0);
506 if (nr_partitions <= 0) {
507 printk(KERN_ERR "BCM UMI NAND: Too few partitions - %d\n",
508 nr_partitions);
509 iounmap(bcm_umi_io_base);
510 kfree(board_mtd);
511 return -EIO;
513 add_mtd_partitions(board_mtd, partition_info, nr_partitions);
516 /* Return happy */
517 return 0;
520 static int bcm_umi_nand_remove(struct platform_device *pdev)
522 #if USE_DMA
523 nand_dma_term();
524 #endif
526 /* Release resources, unregister device */
527 nand_release(board_mtd);
529 /* unmap physical address */
530 iounmap(bcm_umi_io_base);
532 /* Free the MTD device structure */
533 kfree(board_mtd);
535 return 0;
538 #ifdef CONFIG_PM
539 static int bcm_umi_nand_suspend(struct platform_device *pdev,
540 pm_message_t state)
542 printk(KERN_ERR "MTD NAND suspend is being called\n");
543 return 0;
546 static int bcm_umi_nand_resume(struct platform_device *pdev)
548 printk(KERN_ERR "MTD NAND resume is being called\n");
549 return 0;
551 #else
552 #define bcm_umi_nand_suspend NULL
553 #define bcm_umi_nand_resume NULL
554 #endif
556 static struct platform_driver nand_driver = {
557 .driver = {
558 .name = "bcm-nand",
559 .owner = THIS_MODULE,
561 .probe = bcm_umi_nand_probe,
562 .remove = bcm_umi_nand_remove,
563 .suspend = bcm_umi_nand_suspend,
564 .resume = bcm_umi_nand_resume,
567 static int __init nand_init(void)
569 return platform_driver_register(&nand_driver);
572 static void __exit nand_exit(void)
574 platform_driver_unregister(&nand_driver);
577 module_init(nand_init);
578 module_exit(nand_exit);
580 MODULE_LICENSE("GPL");
581 MODULE_AUTHOR("Broadcom");
582 MODULE_DESCRIPTION("BCM UMI MTD NAND driver");