2 * linux/drivers/mtd/onenand/omap2.c
4 * OneNAND driver for OMAP2 / OMAP3
6 * Copyright © 2005-2006 Nokia Corporation
8 * Author: Jarkko Lavinen <jarkko.lavinen@nokia.com> and Juha Yrjölä
9 * IRQ and DMA support written by Timo Teras
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 2 as published by
13 * the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20 * You should have received a copy of the GNU General Public License along with
21 * this program; see the file COPYING. If not, write to the Free Software
22 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include <linux/device.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/mtd/mtd.h>
30 #include <linux/mtd/onenand.h>
31 #include <linux/mtd/partitions.h>
32 #include <linux/platform_device.h>
33 #include <linux/interrupt.h>
34 #include <linux/delay.h>
37 #include <asm/mach/flash.h>
38 #include <asm/arch/gpmc.h>
39 #include <asm/arch/onenand.h>
40 #include <asm/arch/gpio.h>
41 #include <asm/arch/gpmc.h>
42 #include <asm/arch/pm.h>
44 #include <linux/dma-mapping.h>
45 #include <asm/dma-mapping.h>
46 #include <asm/arch/dma.h>
48 #include <asm/arch/board.h>
50 #define DRIVER_NAME "omap2-onenand"
52 #define ONENAND_IO_SIZE SZ_128K
53 #define ONENAND_BUFRAM_SIZE (1024 * 5)
55 struct omap2_onenand {
56 struct platform_device *pdev;
58 unsigned long phys_base;
61 struct mtd_partition *parts;
62 struct onenand_chip onenand;
63 struct completion irq_done;
64 struct completion dma_done;
67 int (*setup)(void __iomem *base, int freq);
70 static void omap2_onenand_dma_cb(int lch, u16 ch_status, void *data)
72 struct omap2_onenand *c = data;
74 complete(&c->dma_done);
77 static irqreturn_t omap2_onenand_interrupt(int irq, void *dev_id)
79 struct omap2_onenand *c = dev_id;
81 complete(&c->irq_done);
86 static inline unsigned short read_reg(struct omap2_onenand *c, int reg)
88 return readw(c->onenand.base + reg);
91 static inline void write_reg(struct omap2_onenand *c, unsigned short value,
94 writew(value, c->onenand.base + reg);
97 static void wait_err(char *msg, int state, unsigned int ctrl, unsigned int intr)
99 printk(KERN_ERR "onenand_wait: %s! state %d ctrl 0x%04x intr 0x%04x\n",
100 msg, state, ctrl, intr);
103 static void wait_warn(char *msg, int state, unsigned int ctrl,
106 printk(KERN_WARNING "onenand_wait: %s! state %d ctrl 0x%04x "
107 "intr 0x%04x\n", msg, state, ctrl, intr);
110 static int omap2_onenand_wait(struct mtd_info *mtd, int state)
112 struct omap2_onenand *c = container_of(mtd, struct omap2_onenand, mtd);
113 unsigned int intr = 0;
115 unsigned long timeout;
118 if (state == FL_RESETING) {
121 for (i = 0; i < 20; i++) {
123 intr = read_reg(c, ONENAND_REG_INTERRUPT);
124 if (intr & ONENAND_INT_MASTER)
127 ctrl = read_reg(c, ONENAND_REG_CTRL_STATUS);
128 if (ctrl & ONENAND_CTRL_ERROR) {
129 wait_err("controller error", state, ctrl, intr);
132 if (!(intr & ONENAND_INT_RESET)) {
133 wait_err("timeout", state, ctrl, intr);
139 if (state != FL_READING) {
142 /* Turn interrupts on */
143 syscfg = read_reg(c, ONENAND_REG_SYS_CFG1);
144 if (!(syscfg & ONENAND_SYS_CFG1_IOBE)) {
145 syscfg |= ONENAND_SYS_CFG1_IOBE;
146 write_reg(c, syscfg, ONENAND_REG_SYS_CFG1);
147 if (cpu_is_omap34xx())
148 /* Add a delay to let GPIO settle */
149 syscfg = read_reg(c, ONENAND_REG_SYS_CFG1);
152 INIT_COMPLETION(c->irq_done);
154 result = omap_get_gpio_datain(c->gpio_irq);
156 ctrl = read_reg(c, ONENAND_REG_CTRL_STATUS);
157 intr = read_reg(c, ONENAND_REG_INTERRUPT);
158 wait_err("gpio error", state, ctrl, intr);
166 result = wait_for_completion_timeout(&c->irq_done,
167 msecs_to_jiffies(20));
169 /* Timeout after 20ms */
170 ctrl = read_reg(c, ONENAND_REG_CTRL_STATUS);
171 if (ctrl & ONENAND_CTRL_ONGO) {
173 * The operation seems to be still going
174 * so give it some more time.
180 ONENAND_REG_INTERRUPT);
181 wait_err("timeout", state, ctrl, intr);
184 intr = read_reg(c, ONENAND_REG_INTERRUPT);
185 if ((intr & ONENAND_INT_MASTER) == 0)
186 wait_warn("timeout", state, ctrl, intr);
192 /* Turn interrupts off */
193 syscfg = read_reg(c, ONENAND_REG_SYS_CFG1);
194 syscfg &= ~ONENAND_SYS_CFG1_IOBE;
195 write_reg(c, syscfg, ONENAND_REG_SYS_CFG1);
197 timeout = jiffies + msecs_to_jiffies(20);
199 if (time_before(jiffies, timeout)) {
200 intr = read_reg(c, ONENAND_REG_INTERRUPT);
201 if (intr & ONENAND_INT_MASTER)
204 /* Timeout after 20ms */
205 ctrl = read_reg(c, ONENAND_REG_CTRL_STATUS);
206 if (ctrl & ONENAND_CTRL_ONGO) {
208 * The operation seems to be still going
209 * so give it some more time.
214 msecs_to_jiffies(20);
223 intr = read_reg(c, ONENAND_REG_INTERRUPT);
224 ctrl = read_reg(c, ONENAND_REG_CTRL_STATUS);
226 if (intr & ONENAND_INT_READ) {
227 int ecc = read_reg(c, ONENAND_REG_ECC_STATUS);
230 unsigned int addr1, addr8;
232 addr1 = read_reg(c, ONENAND_REG_START_ADDRESS1);
233 addr8 = read_reg(c, ONENAND_REG_START_ADDRESS8);
234 if (ecc & ONENAND_ECC_2BIT_ALL) {
235 printk(KERN_ERR "onenand_wait: ECC error = "
236 "0x%04x, addr1 %#x, addr8 %#x\n",
238 mtd->ecc_stats.failed++;
240 } else if (ecc & ONENAND_ECC_1BIT_ALL) {
241 printk(KERN_NOTICE "onenand_wait: correctable "
242 "ECC error = 0x%04x, addr1 %#x, "
243 "addr8 %#x\n", ecc, addr1, addr8);
244 mtd->ecc_stats.corrected++;
247 } else if (state == FL_READING) {
248 wait_err("timeout", state, ctrl, intr);
252 if (ctrl & ONENAND_CTRL_ERROR) {
253 wait_err("controller error", state, ctrl, intr);
254 if (ctrl & ONENAND_CTRL_LOCK)
255 printk(KERN_ERR "onenand_wait: "
256 "Device is write protected!!!\n");
261 wait_warn("unexpected controller status", state, ctrl, intr);
266 static inline int omap2_onenand_bufferram_offset(struct mtd_info *mtd, int area)
268 struct onenand_chip *this = mtd->priv;
270 if (ONENAND_CURRENT_BUFFERRAM(this)) {
271 if (area == ONENAND_DATARAM)
272 return mtd->writesize;
273 if (area == ONENAND_SPARERAM)
280 #if defined(CONFIG_ARCH_OMAP3) || defined(MULTI_OMAP2)
282 static int omap3_onenand_read_bufferram(struct mtd_info *mtd, int area,
283 unsigned char *buffer, int offset,
286 struct omap2_onenand *c = container_of(mtd, struct omap2_onenand, mtd);
287 struct onenand_chip *this = mtd->priv;
288 dma_addr_t dma_src, dma_dst;
290 unsigned long timeout;
291 void *buf = (void *)buffer;
293 volatile unsigned *done;
295 bram_offset = omap2_onenand_bufferram_offset(mtd, area) + area + offset;
296 if (bram_offset & 3 || (size_t)buf & 3 || count < 384)
299 if (buf >= high_memory) {
302 if (((size_t)buf & PAGE_MASK) !=
303 ((size_t)(buf + count - 1) & PAGE_MASK))
305 p1 = vmalloc_to_page(buf);
308 buf = page_address(p1) + ((size_t)buf & ~PAGE_MASK);
314 memcpy(buf + count, this->base + bram_offset + count, xtra);
317 dma_src = c->phys_base + bram_offset;
318 dma_dst = dma_map_single(&c->pdev->dev, buf, count, DMA_FROM_DEVICE);
319 if (dma_mapping_error(&c->pdev->dev, dma_dst)) {
320 dev_err(&c->pdev->dev,
321 "Couldn't DMA map a %d byte buffer\n",
326 omap_set_dma_transfer_params(c->dma_channel, OMAP_DMA_DATA_TYPE_S32,
327 count >> 2, 1, 0, 0, 0);
328 omap_set_dma_src_params(c->dma_channel, 0, OMAP_DMA_AMODE_POST_INC,
330 omap_set_dma_dest_params(c->dma_channel, 0, OMAP_DMA_AMODE_POST_INC,
333 INIT_COMPLETION(c->dma_done);
334 omap_start_dma(c->dma_channel);
336 timeout = jiffies + msecs_to_jiffies(20);
337 done = &c->dma_done.done;
338 while (time_before(jiffies, timeout))
342 dma_unmap_single(&c->pdev->dev, dma_dst, count, DMA_FROM_DEVICE);
345 dev_err(&c->pdev->dev, "timeout waiting for DMA\n");
352 memcpy(buf, this->base + bram_offset, count);
356 static int omap3_onenand_write_bufferram(struct mtd_info *mtd, int area,
357 const unsigned char *buffer,
358 int offset, size_t count)
360 struct omap2_onenand *c = container_of(mtd, struct omap2_onenand, mtd);
361 struct onenand_chip *this = mtd->priv;
362 dma_addr_t dma_src, dma_dst;
364 unsigned long timeout;
365 void *buf = (void *)buffer;
366 volatile unsigned *done;
368 bram_offset = omap2_onenand_bufferram_offset(mtd, area) + area + offset;
369 if (bram_offset & 3 || (size_t)buf & 3 || count < 384)
372 /* panic_write() may be in an interrupt context */
376 if (buf >= high_memory) {
379 if (((size_t)buf & PAGE_MASK) !=
380 ((size_t)(buf + count - 1) & PAGE_MASK))
382 p1 = vmalloc_to_page(buf);
385 buf = page_address(p1) + ((size_t)buf & ~PAGE_MASK);
388 dma_src = dma_map_single(&c->pdev->dev, buf, count, DMA_TO_DEVICE);
389 dma_dst = c->phys_base + bram_offset;
390 if (dma_mapping_error(&c->pdev->dev, dma_dst)) {
391 dev_err(&c->pdev->dev,
392 "Couldn't DMA map a %d byte buffer\n",
397 omap_set_dma_transfer_params(c->dma_channel, OMAP_DMA_DATA_TYPE_S32,
398 count >> 2, 1, 0, 0, 0);
399 omap_set_dma_src_params(c->dma_channel, 0, OMAP_DMA_AMODE_POST_INC,
401 omap_set_dma_dest_params(c->dma_channel, 0, OMAP_DMA_AMODE_POST_INC,
404 INIT_COMPLETION(c->dma_done);
405 omap_start_dma(c->dma_channel);
407 timeout = jiffies + msecs_to_jiffies(20);
408 done = &c->dma_done.done;
409 while (time_before(jiffies, timeout))
413 dma_unmap_single(&c->pdev->dev, dma_dst, count, DMA_TO_DEVICE);
416 dev_err(&c->pdev->dev, "timeout waiting for DMA\n");
423 memcpy(this->base + bram_offset, buf, count);
429 int omap3_onenand_read_bufferram(struct mtd_info *mtd, int area,
430 unsigned char *buffer, int offset,
433 int omap3_onenand_write_bufferram(struct mtd_info *mtd, int area,
434 const unsigned char *buffer,
435 int offset, size_t count);
439 #if defined(CONFIG_ARCH_OMAP2) || defined(MULTI_OMAP2)
441 static int omap2_onenand_read_bufferram(struct mtd_info *mtd, int area,
442 unsigned char *buffer, int offset,
445 struct omap2_onenand *c = container_of(mtd, struct omap2_onenand, mtd);
446 struct onenand_chip *this = mtd->priv;
447 dma_addr_t dma_src, dma_dst;
450 bram_offset = omap2_onenand_bufferram_offset(mtd, area) + area + offset;
451 /* DMA is not used. Revisit PM requirements before enabling it. */
452 if (1 || (c->dma_channel < 0) ||
453 ((void *) buffer >= (void *) high_memory) || (bram_offset & 3) ||
454 (((unsigned int) buffer) & 3) || (count < 1024) || (count & 3)) {
455 memcpy(buffer, (__force void *)(this->base + bram_offset),
460 dma_src = c->phys_base + bram_offset;
461 dma_dst = dma_map_single(&c->pdev->dev, buffer, count,
463 if (dma_mapping_error(&c->pdev->dev, dma_dst)) {
464 dev_err(&c->pdev->dev,
465 "Couldn't DMA map a %d byte buffer\n",
470 omap_set_dma_transfer_params(c->dma_channel, OMAP_DMA_DATA_TYPE_S32,
471 count / 4, 1, 0, 0, 0);
472 omap_set_dma_src_params(c->dma_channel, 0, OMAP_DMA_AMODE_POST_INC,
474 omap_set_dma_dest_params(c->dma_channel, 0, OMAP_DMA_AMODE_POST_INC,
477 INIT_COMPLETION(c->dma_done);
478 omap_start_dma(c->dma_channel);
479 wait_for_completion(&c->dma_done);
481 dma_unmap_single(&c->pdev->dev, dma_dst, count, DMA_FROM_DEVICE);
486 static int omap2_onenand_write_bufferram(struct mtd_info *mtd, int area,
487 const unsigned char *buffer,
488 int offset, size_t count)
490 struct omap2_onenand *c = container_of(mtd, struct omap2_onenand, mtd);
491 struct onenand_chip *this = mtd->priv;
492 dma_addr_t dma_src, dma_dst;
495 bram_offset = omap2_onenand_bufferram_offset(mtd, area) + area + offset;
496 /* DMA is not used. Revisit PM requirements before enabling it. */
497 if (1 || (c->dma_channel < 0) ||
498 ((void *) buffer >= (void *) high_memory) || (bram_offset & 3) ||
499 (((unsigned int) buffer) & 3) || (count < 1024) || (count & 3)) {
500 memcpy((__force void *)(this->base + bram_offset), buffer,
505 dma_src = dma_map_single(&c->pdev->dev, (void *) buffer, count,
507 dma_dst = c->phys_base + bram_offset;
508 if (dma_mapping_error(&c->pdev->dev, dma_dst)) {
509 dev_err(&c->pdev->dev,
510 "Couldn't DMA map a %d byte buffer\n",
515 omap_set_dma_transfer_params(c->dma_channel, OMAP_DMA_DATA_TYPE_S16,
516 count / 2, 1, 0, 0, 0);
517 omap_set_dma_src_params(c->dma_channel, 0, OMAP_DMA_AMODE_POST_INC,
519 omap_set_dma_dest_params(c->dma_channel, 0, OMAP_DMA_AMODE_POST_INC,
522 INIT_COMPLETION(c->dma_done);
523 omap_start_dma(c->dma_channel);
524 wait_for_completion(&c->dma_done);
526 dma_unmap_single(&c->pdev->dev, dma_dst, count, DMA_TO_DEVICE);
533 int omap2_onenand_read_bufferram(struct mtd_info *mtd, int area,
534 unsigned char *buffer, int offset,
537 int omap2_onenand_write_bufferram(struct mtd_info *mtd, int area,
538 const unsigned char *buffer,
539 int offset, size_t count);
543 static struct platform_driver omap2_onenand_driver;
545 static int __adjust_timing(struct device *dev, void *data)
548 struct omap2_onenand *c;
550 c = dev_get_drvdata(dev);
552 BUG_ON(c->setup == NULL);
554 /* DMA is not in use so this is all that is needed */
555 /* Revisit for OMAP3! */
556 ret = c->setup(c->onenand.base, c->freq);
561 int omap2_onenand_rephase(void)
563 return driver_for_each_device(&omap2_onenand_driver.driver, NULL,
564 NULL, __adjust_timing);
567 static void __devexit omap2_onenand_shutdown(struct platform_device *pdev)
569 struct omap2_onenand *c = dev_get_drvdata(&pdev->dev);
571 /* With certain content in the buffer RAM, the OMAP boot ROM code
572 * can recognize the flash chip incorrectly. Zero it out before
575 memset((__force void *)c->onenand.base, 0, ONENAND_BUFRAM_SIZE);
578 static int __devinit omap2_onenand_probe(struct platform_device *pdev)
580 struct omap_onenand_platform_data *pdata;
581 struct omap2_onenand *c;
584 pdata = pdev->dev.platform_data;
586 dev_err(&pdev->dev, "platform data missing\n");
590 c = kzalloc(sizeof(struct omap2_onenand), GFP_KERNEL);
594 init_completion(&c->irq_done);
595 init_completion(&c->dma_done);
596 c->gpmc_cs = pdata->cs;
597 c->gpio_irq = pdata->gpio_irq;
598 c->dma_channel = pdata->dma_channel;
599 if (c->dma_channel < 0) {
600 /* if -1, don't use DMA */
604 r = gpmc_cs_request(c->gpmc_cs, ONENAND_IO_SIZE, &c->phys_base);
606 dev_err(&pdev->dev, "Cannot request GPMC CS\n");
610 if (request_mem_region(c->phys_base, ONENAND_IO_SIZE,
611 pdev->dev.driver->name) == NULL) {
612 dev_err(&pdev->dev, "Cannot reserve memory region at 0x%08lx, "
613 "size: 0x%x\n", c->phys_base, ONENAND_IO_SIZE);
617 c->onenand.base = ioremap(c->phys_base, ONENAND_IO_SIZE);
618 if (c->onenand.base == NULL) {
620 goto err_release_mem_region;
623 if (pdata->onenand_setup != NULL) {
624 r = pdata->onenand_setup(c->onenand.base, c->freq);
626 dev_err(&pdev->dev, "Onenand platform setup failed: "
630 c->setup = pdata->onenand_setup;
634 if ((r = omap_request_gpio(c->gpio_irq)) < 0) {
635 dev_err(&pdev->dev, "Failed to request GPIO%d for "
636 "OneNAND\n", c->gpio_irq);
639 omap_set_gpio_direction(c->gpio_irq, 1);
641 if ((r = request_irq(OMAP_GPIO_IRQ(c->gpio_irq),
642 omap2_onenand_interrupt, IRQF_TRIGGER_RISING,
643 pdev->dev.driver->name, c)) < 0)
644 goto err_release_gpio;
647 if (c->dma_channel >= 0) {
648 r = omap_request_dma(0, pdev->dev.driver->name,
649 omap2_onenand_dma_cb, (void *) c,
652 omap_set_dma_write_mode(c->dma_channel,
653 OMAP_DMA_WRITE_NON_POSTED);
654 omap_set_dma_src_data_pack(c->dma_channel, 1);
655 omap_set_dma_src_burst_mode(c->dma_channel,
656 OMAP_DMA_DATA_BURST_8);
657 omap_set_dma_dest_data_pack(c->dma_channel, 1);
658 omap_set_dma_dest_burst_mode(c->dma_channel,
659 OMAP_DMA_DATA_BURST_8);
662 "failed to allocate DMA for OneNAND, "
663 "using PIO instead\n");
668 dev_info(&pdev->dev, "initializing on CS%d, phys base 0x%08lx, virtual "
669 "base %p\n", c->gpmc_cs, c->phys_base,
673 c->mtd.name = pdev->dev.bus_id;
674 c->mtd.priv = &c->onenand;
675 c->mtd.owner = THIS_MODULE;
677 if (c->dma_channel >= 0) {
678 struct onenand_chip *this = &c->onenand;
680 this->wait = omap2_onenand_wait;
681 if (cpu_is_omap34xx()) {
682 this->read_bufferram = omap3_onenand_read_bufferram;
683 this->write_bufferram = omap3_onenand_write_bufferram;
685 this->read_bufferram = omap2_onenand_read_bufferram;
686 this->write_bufferram = omap2_onenand_write_bufferram;
690 if ((r = onenand_scan(&c->mtd, 1)) < 0)
691 goto err_release_dma;
693 switch ((c->onenand.version_id >> 4) & 0xf) {
708 #ifdef CONFIG_MTD_PARTITIONS
709 if (pdata->parts != NULL)
710 r = add_mtd_partitions(&c->mtd, pdata->parts,
714 r = add_mtd_device(&c->mtd);
716 goto err_release_onenand;
718 platform_set_drvdata(pdev, c);
723 onenand_release(&c->mtd);
725 if (c->dma_channel != -1)
726 omap_free_dma(c->dma_channel);
728 free_irq(OMAP_GPIO_IRQ(c->gpio_irq), c);
731 omap_free_gpio(c->gpio_irq);
733 iounmap(c->onenand.base);
734 err_release_mem_region:
735 release_mem_region(c->phys_base, ONENAND_IO_SIZE);
737 gpmc_cs_free(c->gpmc_cs);
744 static int __devexit omap2_onenand_remove(struct platform_device *pdev)
746 struct omap2_onenand *c = dev_get_drvdata(&pdev->dev);
750 #ifdef CONFIG_MTD_PARTITIONS
752 del_mtd_partitions(&c->mtd);
754 del_mtd_device(&c->mtd);
756 del_mtd_device(&c->mtd);
759 onenand_release(&c->mtd);
760 if (c->dma_channel != -1)
761 omap_free_dma(c->dma_channel);
762 omap2_onenand_shutdown(pdev);
763 platform_set_drvdata(pdev, NULL);
765 free_irq(OMAP_GPIO_IRQ(c->gpio_irq), c);
766 omap_free_gpio(c->gpio_irq);
768 iounmap(c->onenand.base);
769 release_mem_region(c->phys_base, ONENAND_IO_SIZE);
775 static struct platform_driver omap2_onenand_driver = {
776 .probe = omap2_onenand_probe,
777 .remove = omap2_onenand_remove,
778 .shutdown = omap2_onenand_shutdown,
781 .owner = THIS_MODULE,
785 static int __init omap2_onenand_init(void)
787 printk(KERN_INFO "OneNAND driver initializing\n");
788 return platform_driver_register(&omap2_onenand_driver);
791 static void __exit omap2_onenand_exit(void)
793 platform_driver_unregister(&omap2_onenand_driver);
796 module_init(omap2_onenand_init);
797 module_exit(omap2_onenand_exit);
799 MODULE_ALIAS(DRIVER_NAME);
800 MODULE_LICENSE("GPL");
801 MODULE_AUTHOR("Jarkko Lavinen <jarkko.lavinen@nokia.com>");
802 MODULE_DESCRIPTION("Glue layer for OneNAND flash on OMAP2 / OMAP3");