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
13 *****************************************************************************/
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>
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>
33 #include <asm/mach-types.h>
34 #include <asm/system.h>
36 #include <mach/reg_nand.h>
37 #include <mach/reg_umi.h>
39 #include "nand_bcm_umi.h"
41 #include <mach/memory_settings.h>
45 #include <linux/dma-mapping.h>
46 #include <linux/completion.h>
48 /* ---- External Variable Declarations ----------------------------------- */
49 /* ---- External Function Prototypes ------------------------------------- */
50 /* ---- Public Variables ------------------------------------------------- */
51 /* ---- Private Constants and Types -------------------------------------- */
52 static const __devinitconst
char gBanner
[] = KERN_INFO \
53 "BCM UMI MTD NAND Driver: 1.00\n";
55 #ifdef CONFIG_MTD_PARTITIONS
56 const char *part_probes
[] = { "cmdlinepart", NULL
};
60 static uint8_t scan_ff_pattern
[] = { 0xff };
62 static struct nand_bbt_descr largepage_bbt
= {
66 .pattern
= scan_ff_pattern
71 ** Preallocate a buffer to avoid having to do this every dma operation.
72 ** This is the size of the preallocated coherent DMA buffer.
75 #define DMA_MIN_BUFLEN 512
76 #define DMA_MAX_BUFLEN PAGE_SIZE
77 #define USE_DIRECT_IO(len) (((len) < DMA_MIN_BUFLEN) || \
78 ((len) > DMA_MAX_BUFLEN))
81 * The current NAND data space goes from 0x80001900 to 0x80001FFF,
82 * which is only 0x700 = 1792 bytes long. This is too small for 2K, 4K page
83 * size NAND flash. Need to break the DMA down to multiple 1Ks.
85 * Need to make sure REG_NAND_DATA_PADDR + DMA_MAX_LEN < 0x80002000
87 #define DMA_MAX_LEN 1024
90 #define DMA_MIN_BUFLEN 0
91 #define DMA_MAX_BUFLEN 0
92 #define USE_DIRECT_IO(len) 1
94 /* ---- Private Function Prototypes -------------------------------------- */
95 static void bcm_umi_nand_read_buf(struct mtd_info
*mtd
, u_char
* buf
, int len
);
96 static void bcm_umi_nand_write_buf(struct mtd_info
*mtd
, const u_char
* buf
,
99 /* ---- Private Variables ------------------------------------------------ */
100 static struct mtd_info
*board_mtd
;
101 static void __iomem
*bcm_umi_io_base
;
102 static void *virtPtr
;
103 static dma_addr_t physPtr
;
104 static struct completion nand_comp
;
106 /* ---- Private Functions ------------------------------------------------ */
108 #include "bcm_umi_bch.c"
110 #include "bcm_umi_hamming.c"
115 /* Handler called when the DMA finishes. */
116 static void nand_dma_handler(DMA_Device_t dev
, int reason
, void *userData
)
118 complete(&nand_comp
);
121 static int nand_dma_init(void)
125 rc
= dma_set_device_handler(DMA_DEVICE_NAND_MEM_TO_MEM
,
126 nand_dma_handler
, NULL
);
128 printk(KERN_ERR
"dma_set_device_handler failed: %d\n", rc
);
133 dma_alloc_coherent(NULL
, DMA_MAX_BUFLEN
, &physPtr
, GFP_KERNEL
);
134 if (virtPtr
== NULL
) {
135 printk(KERN_ERR
"NAND - Failed to allocate memory for DMA buffer\n");
142 static void nand_dma_term(void)
145 dma_free_coherent(NULL
, DMA_MAX_BUFLEN
, virtPtr
, physPtr
);
148 static void nand_dma_read(void *buf
, int len
)
156 panic("nand_dma_read: virtPtr == NULL\n");
158 if ((void *)physPtr
== NULL
)
159 panic("nand_dma_read: physPtr == NULL\n");
161 hndl
= dma_request_channel(DMA_DEVICE_NAND_MEM_TO_MEM
);
164 "nand_dma_read: unable to allocate dma channel: %d\n",
169 while (len_left
> 0) {
170 if (len_left
> DMA_MAX_LEN
) {
171 tmp_len
= DMA_MAX_LEN
;
172 len_left
-= DMA_MAX_LEN
;
178 init_completion(&nand_comp
);
179 dma_transfer_mem_to_mem(hndl
, REG_NAND_DATA_PADDR
,
180 physPtr
+ offset
, tmp_len
);
181 wait_for_completion(&nand_comp
);
186 dma_free_channel(hndl
);
189 memcpy(buf
, virtPtr
, len
);
192 static void nand_dma_write(const void *buf
, int len
)
200 panic("nand_dma_write: buf == NULL\n");
203 panic("nand_dma_write: virtPtr == NULL\n");
205 if ((void *)physPtr
== NULL
)
206 panic("nand_dma_write: physPtr == NULL\n");
208 memcpy(virtPtr
, buf
, len
);
211 hndl
= dma_request_channel(DMA_DEVICE_NAND_MEM_TO_MEM
);
214 "nand_dma_write: unable to allocate dma channel: %d\n",
219 while (len_left
> 0) {
220 if (len_left
> DMA_MAX_LEN
) {
221 tmp_len
= DMA_MAX_LEN
;
222 len_left
-= DMA_MAX_LEN
;
228 init_completion(&nand_comp
);
229 dma_transfer_mem_to_mem(hndl
, physPtr
+ offset
,
230 REG_NAND_DATA_PADDR
, tmp_len
);
231 wait_for_completion(&nand_comp
);
236 dma_free_channel(hndl
);
241 static int nand_dev_ready(struct mtd_info
*mtd
)
243 return nand_bcm_umi_dev_ready();
246 /****************************************************************************
248 * bcm_umi_nand_inithw
250 * This routine does the necessary hardware (board-specific)
251 * initializations. This includes setting up the timings, etc.
253 ***************************************************************************/
254 int bcm_umi_nand_inithw(void)
256 /* Configure nand timing parameters */
257 REG_UMI_NAND_TCR
&= ~0x7ffff;
258 REG_UMI_NAND_TCR
|= HW_CFG_NAND_TCR
;
260 #if !defined(CONFIG_MTD_NAND_BCM_UMI_HWCS)
261 /* enable software control of CS */
262 REG_UMI_NAND_TCR
|= REG_UMI_NAND_TCR_CS_SWCTRL
;
265 /* keep NAND chip select asserted */
266 REG_UMI_NAND_RCSR
|= REG_UMI_NAND_RCSR_CS_ASSERTED
;
268 REG_UMI_NAND_TCR
&= ~REG_UMI_NAND_TCR_WORD16
;
269 /* enable writes to flash */
270 REG_UMI_MMD_ICR
|= REG_UMI_MMD_ICR_FLASH_WP
;
272 writel(NAND_CMD_RESET
, bcm_umi_io_base
+ REG_NAND_CMD_OFFSET
);
273 nand_bcm_umi_wait_till_ready();
276 nand_bcm_umi_bch_config_ecc(NAND_ECC_NUM_BYTES
);
282 /* Used to turn latch the proper register for access. */
283 static void bcm_umi_nand_hwcontrol(struct mtd_info
*mtd
, int cmd
,
286 /* send command to hardware */
287 struct nand_chip
*chip
= mtd
->priv
;
288 if (ctrl
& NAND_CTRL_CHANGE
) {
289 if (ctrl
& NAND_CLE
) {
290 chip
->IO_ADDR_W
= bcm_umi_io_base
+ REG_NAND_CMD_OFFSET
;
293 if (ctrl
& NAND_ALE
) {
295 bcm_umi_io_base
+ REG_NAND_ADDR_OFFSET
;
298 chip
->IO_ADDR_W
= bcm_umi_io_base
+ REG_NAND_DATA8_OFFSET
;
302 /* Send command to chip directly */
303 if (cmd
!= NAND_CMD_NONE
)
304 writeb(cmd
, chip
->IO_ADDR_W
);
307 static void bcm_umi_nand_write_buf(struct mtd_info
*mtd
, const u_char
* buf
,
310 if (USE_DIRECT_IO(len
)) {
311 /* Do it the old way if the buffer is small or too large.
312 * Probably quicker than starting and checking dma. */
314 struct nand_chip
*this = mtd
->priv
;
316 for (i
= 0; i
< len
; i
++)
317 writeb(buf
[i
], this->IO_ADDR_W
);
321 nand_dma_write(buf
, len
);
325 static void bcm_umi_nand_read_buf(struct mtd_info
*mtd
, u_char
* buf
, int len
)
327 if (USE_DIRECT_IO(len
)) {
329 struct nand_chip
*this = mtd
->priv
;
331 for (i
= 0; i
< len
; i
++)
332 buf
[i
] = readb(this->IO_ADDR_R
);
336 nand_dma_read(buf
, len
);
340 static uint8_t readbackbuf
[NAND_MAX_PAGESIZE
];
341 static int bcm_umi_nand_verify_buf(struct mtd_info
*mtd
, const u_char
* buf
,
345 * Try to readback page with ECC correction. This is necessary
346 * for MLC parts which may have permanently stuck bits.
348 struct nand_chip
*chip
= mtd
->priv
;
349 int ret
= chip
->ecc
.read_page(mtd
, chip
, readbackbuf
, 0);
353 if (memcmp(readbackbuf
, buf
, len
) == 0)
361 static int __devinit
bcm_umi_nand_probe(struct platform_device
*pdev
)
363 struct nand_chip
*this;
369 /* Allocate memory for MTD device structure and private data */
371 kmalloc(sizeof(struct mtd_info
) + sizeof(struct nand_chip
),
375 "Unable to allocate NAND MTD device structure.\n");
379 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
384 /* map physical address */
385 bcm_umi_io_base
= ioremap(r
->start
, r
->end
- r
->start
+ 1);
387 if (!bcm_umi_io_base
) {
388 printk(KERN_ERR
"ioremap to access BCM UMI NAND chip failed\n");
393 /* Get pointer to private data */
394 this = (struct nand_chip
*)(&board_mtd
[1]);
396 /* Initialize structures */
397 memset((char *)board_mtd
, 0, sizeof(struct mtd_info
));
398 memset((char *)this, 0, sizeof(struct nand_chip
));
400 /* Link the private data with the MTD structure */
401 board_mtd
->priv
= this;
403 /* Initialize the NAND hardware. */
404 if (bcm_umi_nand_inithw() < 0) {
405 printk(KERN_ERR
"BCM UMI NAND chip could not be initialized\n");
406 iounmap(bcm_umi_io_base
);
411 /* Set address of NAND IO lines */
412 this->IO_ADDR_W
= bcm_umi_io_base
+ REG_NAND_DATA8_OFFSET
;
413 this->IO_ADDR_R
= bcm_umi_io_base
+ REG_NAND_DATA8_OFFSET
;
415 /* Set command delay time, see datasheet for correct value */
416 this->chip_delay
= 0;
417 /* Assign the device ready function, if available */
418 this->dev_ready
= nand_dev_ready
;
421 this->write_buf
= bcm_umi_nand_write_buf
;
422 this->read_buf
= bcm_umi_nand_read_buf
;
423 this->verify_buf
= bcm_umi_nand_verify_buf
;
425 this->cmd_ctrl
= bcm_umi_nand_hwcontrol
;
426 this->ecc
.mode
= NAND_ECC_HW
;
427 this->ecc
.size
= 512;
428 this->ecc
.bytes
= NAND_ECC_NUM_BYTES
;
430 this->ecc
.read_page
= bcm_umi_bch_read_page_hwecc
;
431 this->ecc
.write_page
= bcm_umi_bch_write_page_hwecc
;
433 this->ecc
.correct
= nand_correct_data512
;
434 this->ecc
.calculate
= bcm_umi_hamming_get_hw_ecc
;
435 this->ecc
.hwctl
= bcm_umi_hamming_enable_hwecc
;
439 err
= nand_dma_init();
444 /* Figure out the size of the device that we have.
445 * We need to do this to figure out which ECC
446 * layout we'll be using.
449 err
= nand_scan_ident(board_mtd
, 1, NULL
);
451 printk(KERN_ERR
"nand_scan failed: %d\n", err
);
452 iounmap(bcm_umi_io_base
);
457 /* Now that we know the nand size, we can setup the ECC layout */
459 switch (board_mtd
->writesize
) { /* writesize is the pagesize */
461 this->ecc
.layout
= &nand_hw_eccoob_4096
;
464 this->ecc
.layout
= &nand_hw_eccoob_2048
;
467 this->ecc
.layout
= &nand_hw_eccoob_512
;
471 printk(KERN_ERR
"NAND - Unrecognized pagesize: %d\n",
472 board_mtd
->writesize
);
478 if (board_mtd
->writesize
> 512) {
479 if (this->options
& NAND_USE_FLASH_BBT
)
480 largepage_bbt
.options
= NAND_BBT_SCAN2NDPAGE
;
481 this->badblock_pattern
= &largepage_bbt
;
485 /* Now finish off the scan, now that ecc.layout has been initialized. */
487 err
= nand_scan_tail(board_mtd
);
489 printk(KERN_ERR
"nand_scan failed: %d\n", err
);
490 iounmap(bcm_umi_io_base
);
495 /* Register the partitions */
498 struct mtd_partition
*partition_info
;
500 board_mtd
->name
= "bcm_umi-nand";
502 parse_mtd_partitions(board_mtd
, part_probes
,
505 if (nr_partitions
<= 0) {
506 printk(KERN_ERR
"BCM UMI NAND: Too few partitions - %d\n",
508 iounmap(bcm_umi_io_base
);
512 add_mtd_partitions(board_mtd
, partition_info
, nr_partitions
);
519 static int bcm_umi_nand_remove(struct platform_device
*pdev
)
525 /* Release resources, unregister device */
526 nand_release(board_mtd
);
528 /* unmap physical address */
529 iounmap(bcm_umi_io_base
);
531 /* Free the MTD device structure */
538 static int bcm_umi_nand_suspend(struct platform_device
*pdev
,
541 printk(KERN_ERR
"MTD NAND suspend is being called\n");
545 static int bcm_umi_nand_resume(struct platform_device
*pdev
)
547 printk(KERN_ERR
"MTD NAND resume is being called\n");
551 #define bcm_umi_nand_suspend NULL
552 #define bcm_umi_nand_resume NULL
555 static struct platform_driver nand_driver
= {
558 .owner
= THIS_MODULE
,
560 .probe
= bcm_umi_nand_probe
,
561 .remove
= bcm_umi_nand_remove
,
562 .suspend
= bcm_umi_nand_suspend
,
563 .resume
= bcm_umi_nand_resume
,
566 static int __init
nand_init(void)
568 return platform_driver_register(&nand_driver
);
571 static void __exit
nand_exit(void)
573 platform_driver_unregister(&nand_driver
);
576 module_init(nand_init
);
577 module_exit(nand_exit
);
579 MODULE_LICENSE("GPL");
580 MODULE_AUTHOR("Broadcom");
581 MODULE_DESCRIPTION("BCM UMI MTD NAND driver");