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/mtd/mtd.h>
29 #include <linux/mtd/onenand.h>
30 #include <linux/mtd/partitions.h>
31 #include <linux/of_device.h>
32 #include <linux/omap-gpmc.h>
33 #include <linux/platform_device.h>
34 #include <linux/interrupt.h>
35 #include <linux/delay.h>
36 #include <linux/dma-mapping.h>
37 #include <linux/dmaengine.h>
39 #include <linux/slab.h>
40 #include <linux/gpio/consumer.h>
42 #include <asm/mach/flash.h>
44 #define DRIVER_NAME "omap2-onenand"
46 #define ONENAND_BUFRAM_SIZE (1024 * 5)
48 struct omap2_onenand
{
49 struct platform_device
*pdev
;
51 unsigned long phys_base
;
52 struct gpio_desc
*int_gpiod
;
54 struct onenand_chip onenand
;
55 struct completion irq_done
;
56 struct completion dma_done
;
57 struct dma_chan
*dma_chan
;
60 static void omap2_onenand_dma_complete_func(void *completion
)
65 static irqreturn_t
omap2_onenand_interrupt(int irq
, void *dev_id
)
67 struct omap2_onenand
*c
= dev_id
;
69 complete(&c
->irq_done
);
74 static inline unsigned short read_reg(struct omap2_onenand
*c
, int reg
)
76 return readw(c
->onenand
.base
+ reg
);
79 static inline void write_reg(struct omap2_onenand
*c
, unsigned short value
,
82 writew(value
, c
->onenand
.base
+ reg
);
85 static int omap2_onenand_set_cfg(struct omap2_onenand
*c
,
87 int latency
, int burst_len
)
89 unsigned short reg
= ONENAND_SYS_CFG1_RDY
| ONENAND_SYS_CFG1_INT
;
91 reg
|= latency
<< ONENAND_SYS_CFG1_BRL_SHIFT
;
94 case 0: /* continuous */
97 reg
|= ONENAND_SYS_CFG1_BL_4
;
100 reg
|= ONENAND_SYS_CFG1_BL_8
;
103 reg
|= ONENAND_SYS_CFG1_BL_16
;
106 reg
|= ONENAND_SYS_CFG1_BL_32
;
113 reg
|= ONENAND_SYS_CFG1_HF
;
115 reg
|= ONENAND_SYS_CFG1_VHF
;
117 reg
|= ONENAND_SYS_CFG1_SYNC_READ
;
119 reg
|= ONENAND_SYS_CFG1_SYNC_WRITE
;
121 write_reg(c
, reg
, ONENAND_REG_SYS_CFG1
);
126 static int omap2_onenand_get_freq(int ver
)
128 switch ((ver
>> 4) & 0xf) {
144 static void wait_err(char *msg
, int state
, unsigned int ctrl
, unsigned int intr
)
146 printk(KERN_ERR
"onenand_wait: %s! state %d ctrl 0x%04x intr 0x%04x\n",
147 msg
, state
, ctrl
, intr
);
150 static void wait_warn(char *msg
, int state
, unsigned int ctrl
,
153 printk(KERN_WARNING
"onenand_wait: %s! state %d ctrl 0x%04x "
154 "intr 0x%04x\n", msg
, state
, ctrl
, intr
);
157 static int omap2_onenand_wait(struct mtd_info
*mtd
, int state
)
159 struct omap2_onenand
*c
= container_of(mtd
, struct omap2_onenand
, mtd
);
160 struct onenand_chip
*this = mtd
->priv
;
161 unsigned int intr
= 0;
162 unsigned int ctrl
, ctrl_mask
;
163 unsigned long timeout
;
166 if (state
== FL_RESETING
|| state
== FL_PREPARING_ERASE
||
167 state
== FL_VERIFYING_ERASE
) {
169 unsigned int intr_flags
= ONENAND_INT_MASTER
;
173 intr_flags
|= ONENAND_INT_RESET
;
175 case FL_PREPARING_ERASE
:
176 intr_flags
|= ONENAND_INT_ERASE
;
178 case FL_VERIFYING_ERASE
:
185 intr
= read_reg(c
, ONENAND_REG_INTERRUPT
);
186 if (intr
& ONENAND_INT_MASTER
)
189 ctrl
= read_reg(c
, ONENAND_REG_CTRL_STATUS
);
190 if (ctrl
& ONENAND_CTRL_ERROR
) {
191 wait_err("controller error", state
, ctrl
, intr
);
194 if ((intr
& intr_flags
) == intr_flags
)
196 /* Continue in wait for interrupt branch */
199 if (state
!= FL_READING
) {
202 /* Turn interrupts on */
203 syscfg
= read_reg(c
, ONENAND_REG_SYS_CFG1
);
204 if (!(syscfg
& ONENAND_SYS_CFG1_IOBE
)) {
205 syscfg
|= ONENAND_SYS_CFG1_IOBE
;
206 write_reg(c
, syscfg
, ONENAND_REG_SYS_CFG1
);
207 /* Add a delay to let GPIO settle */
208 syscfg
= read_reg(c
, ONENAND_REG_SYS_CFG1
);
211 reinit_completion(&c
->irq_done
);
212 result
= gpiod_get_value(c
->int_gpiod
);
214 ctrl
= read_reg(c
, ONENAND_REG_CTRL_STATUS
);
215 intr
= read_reg(c
, ONENAND_REG_INTERRUPT
);
216 wait_err("gpio error", state
, ctrl
, intr
);
218 } else if (result
== 0) {
221 if (!wait_for_completion_io_timeout(&c
->irq_done
,
222 msecs_to_jiffies(20))) {
223 /* Timeout after 20ms */
224 ctrl
= read_reg(c
, ONENAND_REG_CTRL_STATUS
);
225 if (ctrl
& ONENAND_CTRL_ONGO
&&
228 * The operation seems to be still going
229 * so give it some more time.
235 ONENAND_REG_INTERRUPT
);
236 wait_err("timeout", state
, ctrl
, intr
);
239 intr
= read_reg(c
, ONENAND_REG_INTERRUPT
);
240 if ((intr
& ONENAND_INT_MASTER
) == 0)
241 wait_warn("timeout", state
, ctrl
, intr
);
247 /* Turn interrupts off */
248 syscfg
= read_reg(c
, ONENAND_REG_SYS_CFG1
);
249 syscfg
&= ~ONENAND_SYS_CFG1_IOBE
;
250 write_reg(c
, syscfg
, ONENAND_REG_SYS_CFG1
);
252 timeout
= jiffies
+ msecs_to_jiffies(20);
254 if (time_before(jiffies
, timeout
)) {
255 intr
= read_reg(c
, ONENAND_REG_INTERRUPT
);
256 if (intr
& ONENAND_INT_MASTER
)
259 /* Timeout after 20ms */
260 ctrl
= read_reg(c
, ONENAND_REG_CTRL_STATUS
);
261 if (ctrl
& ONENAND_CTRL_ONGO
) {
263 * The operation seems to be still going
264 * so give it some more time.
269 msecs_to_jiffies(20);
278 intr
= read_reg(c
, ONENAND_REG_INTERRUPT
);
279 ctrl
= read_reg(c
, ONENAND_REG_CTRL_STATUS
);
281 if (intr
& ONENAND_INT_READ
) {
282 int ecc
= read_reg(c
, ONENAND_REG_ECC_STATUS
);
285 unsigned int addr1
, addr8
;
287 addr1
= read_reg(c
, ONENAND_REG_START_ADDRESS1
);
288 addr8
= read_reg(c
, ONENAND_REG_START_ADDRESS8
);
289 if (ecc
& ONENAND_ECC_2BIT_ALL
) {
290 printk(KERN_ERR
"onenand_wait: ECC error = "
291 "0x%04x, addr1 %#x, addr8 %#x\n",
293 mtd
->ecc_stats
.failed
++;
295 } else if (ecc
& ONENAND_ECC_1BIT_ALL
) {
296 printk(KERN_NOTICE
"onenand_wait: correctable "
297 "ECC error = 0x%04x, addr1 %#x, "
298 "addr8 %#x\n", ecc
, addr1
, addr8
);
299 mtd
->ecc_stats
.corrected
++;
302 } else if (state
== FL_READING
) {
303 wait_err("timeout", state
, ctrl
, intr
);
307 if (ctrl
& ONENAND_CTRL_ERROR
) {
308 wait_err("controller error", state
, ctrl
, intr
);
309 if (ctrl
& ONENAND_CTRL_LOCK
)
310 printk(KERN_ERR
"onenand_wait: "
311 "Device is write protected!!!\n");
317 ctrl_mask
&= ~0x8000;
319 if (ctrl
& ctrl_mask
)
320 wait_warn("unexpected controller status", state
, ctrl
, intr
);
325 static inline int omap2_onenand_bufferram_offset(struct mtd_info
*mtd
, int area
)
327 struct onenand_chip
*this = mtd
->priv
;
329 if (ONENAND_CURRENT_BUFFERRAM(this)) {
330 if (area
== ONENAND_DATARAM
)
331 return this->writesize
;
332 if (area
== ONENAND_SPARERAM
)
339 static inline int omap2_onenand_dma_transfer(struct omap2_onenand
*c
,
340 dma_addr_t src
, dma_addr_t dst
,
343 struct dma_async_tx_descriptor
*tx
;
346 tx
= dmaengine_prep_dma_memcpy(c
->dma_chan
, dst
, src
, count
, 0);
348 dev_err(&c
->pdev
->dev
, "Failed to prepare DMA memcpy\n");
352 reinit_completion(&c
->dma_done
);
354 tx
->callback
= omap2_onenand_dma_complete_func
;
355 tx
->callback_param
= &c
->dma_done
;
357 cookie
= tx
->tx_submit(tx
);
358 if (dma_submit_error(cookie
)) {
359 dev_err(&c
->pdev
->dev
, "Failed to do DMA tx_submit\n");
363 dma_async_issue_pending(c
->dma_chan
);
365 if (!wait_for_completion_io_timeout(&c
->dma_done
,
366 msecs_to_jiffies(20))) {
367 dmaengine_terminate_sync(c
->dma_chan
);
374 static int omap2_onenand_read_bufferram(struct mtd_info
*mtd
, int area
,
375 unsigned char *buffer
, int offset
,
378 struct omap2_onenand
*c
= container_of(mtd
, struct omap2_onenand
, mtd
);
379 struct onenand_chip
*this = mtd
->priv
;
380 dma_addr_t dma_src
, dma_dst
;
382 void *buf
= (void *)buffer
;
386 bram_offset
= omap2_onenand_bufferram_offset(mtd
, area
) + area
+ offset
;
387 if (bram_offset
& 3 || (size_t)buf
& 3 || count
< 384)
390 /* panic_write() may be in an interrupt context */
391 if (in_interrupt() || oops_in_progress
)
394 if (buf
>= high_memory
) {
397 if (((size_t)buf
& PAGE_MASK
) !=
398 ((size_t)(buf
+ count
- 1) & PAGE_MASK
))
400 p1
= vmalloc_to_page(buf
);
403 buf
= page_address(p1
) + ((size_t)buf
& ~PAGE_MASK
);
409 memcpy(buf
+ count
, this->base
+ bram_offset
+ count
, xtra
);
412 dma_src
= c
->phys_base
+ bram_offset
;
413 dma_dst
= dma_map_single(&c
->pdev
->dev
, buf
, count
, DMA_FROM_DEVICE
);
414 if (dma_mapping_error(&c
->pdev
->dev
, dma_dst
)) {
415 dev_err(&c
->pdev
->dev
,
416 "Couldn't DMA map a %d byte buffer\n",
421 ret
= omap2_onenand_dma_transfer(c
, dma_src
, dma_dst
, count
);
422 dma_unmap_single(&c
->pdev
->dev
, dma_dst
, count
, DMA_FROM_DEVICE
);
425 dev_err(&c
->pdev
->dev
, "timeout waiting for DMA\n");
432 memcpy(buf
, this->base
+ bram_offset
, count
);
436 static int omap2_onenand_write_bufferram(struct mtd_info
*mtd
, int area
,
437 const unsigned char *buffer
,
438 int offset
, size_t count
)
440 struct omap2_onenand
*c
= container_of(mtd
, struct omap2_onenand
, mtd
);
441 struct onenand_chip
*this = mtd
->priv
;
442 dma_addr_t dma_src
, dma_dst
;
444 void *buf
= (void *)buffer
;
447 bram_offset
= omap2_onenand_bufferram_offset(mtd
, area
) + area
+ offset
;
448 if (bram_offset
& 3 || (size_t)buf
& 3 || count
< 384)
451 /* panic_write() may be in an interrupt context */
452 if (in_interrupt() || oops_in_progress
)
455 if (buf
>= high_memory
) {
458 if (((size_t)buf
& PAGE_MASK
) !=
459 ((size_t)(buf
+ count
- 1) & PAGE_MASK
))
461 p1
= vmalloc_to_page(buf
);
464 buf
= page_address(p1
) + ((size_t)buf
& ~PAGE_MASK
);
467 dma_src
= dma_map_single(&c
->pdev
->dev
, buf
, count
, DMA_TO_DEVICE
);
468 dma_dst
= c
->phys_base
+ bram_offset
;
469 if (dma_mapping_error(&c
->pdev
->dev
, dma_src
)) {
470 dev_err(&c
->pdev
->dev
,
471 "Couldn't DMA map a %d byte buffer\n",
476 ret
= omap2_onenand_dma_transfer(c
, dma_src
, dma_dst
, count
);
477 dma_unmap_single(&c
->pdev
->dev
, dma_src
, count
, DMA_TO_DEVICE
);
480 dev_err(&c
->pdev
->dev
, "timeout waiting for DMA\n");
487 memcpy(this->base
+ bram_offset
, buf
, count
);
491 static void omap2_onenand_shutdown(struct platform_device
*pdev
)
493 struct omap2_onenand
*c
= dev_get_drvdata(&pdev
->dev
);
495 /* With certain content in the buffer RAM, the OMAP boot ROM code
496 * can recognize the flash chip incorrectly. Zero it out before
499 memset((__force
void *)c
->onenand
.base
, 0, ONENAND_BUFRAM_SIZE
);
502 static int omap2_onenand_probe(struct platform_device
*pdev
)
506 int freq
, latency
, r
;
507 struct resource
*res
;
508 struct omap2_onenand
*c
;
509 struct gpmc_onenand_info info
;
510 struct device
*dev
= &pdev
->dev
;
511 struct device_node
*np
= dev
->of_node
;
513 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
515 dev_err(dev
, "error getting memory resource\n");
519 r
= of_property_read_u32(np
, "reg", &val
);
521 dev_err(dev
, "reg not found in DT\n");
525 c
= devm_kzalloc(dev
, sizeof(struct omap2_onenand
), GFP_KERNEL
);
529 init_completion(&c
->irq_done
);
530 init_completion(&c
->dma_done
);
532 c
->phys_base
= res
->start
;
534 c
->onenand
.base
= devm_ioremap_resource(dev
, res
);
535 if (IS_ERR(c
->onenand
.base
))
536 return PTR_ERR(c
->onenand
.base
);
538 c
->int_gpiod
= devm_gpiod_get_optional(dev
, "int", GPIOD_IN
);
539 if (IS_ERR(c
->int_gpiod
)) {
540 r
= PTR_ERR(c
->int_gpiod
);
541 /* Just try again if this happens */
542 if (r
!= -EPROBE_DEFER
)
543 dev_err(dev
, "error getting gpio: %d\n", r
);
548 r
= devm_request_irq(dev
, gpiod_to_irq(c
->int_gpiod
),
549 omap2_onenand_interrupt
,
550 IRQF_TRIGGER_RISING
, "onenand", c
);
554 c
->onenand
.wait
= omap2_onenand_wait
;
558 dma_cap_set(DMA_MEMCPY
, mask
);
560 c
->dma_chan
= dma_request_channel(mask
, NULL
, NULL
);
562 c
->onenand
.read_bufferram
= omap2_onenand_read_bufferram
;
563 c
->onenand
.write_bufferram
= omap2_onenand_write_bufferram
;
567 c
->mtd
.priv
= &c
->onenand
;
568 c
->mtd
.dev
.parent
= dev
;
569 mtd_set_of_node(&c
->mtd
, dev
->of_node
);
571 dev_info(dev
, "initializing on CS%d (0x%08lx), va %p, %s mode\n",
572 c
->gpmc_cs
, c
->phys_base
, c
->onenand
.base
,
573 c
->dma_chan
? "DMA" : "PIO");
575 if ((r
= onenand_scan(&c
->mtd
, 1)) < 0)
576 goto err_release_dma
;
578 freq
= omap2_onenand_get_freq(c
->onenand
.version_id
);
593 default: /* 40 MHz or lower */
598 r
= gpmc_omap_onenand_set_timings(dev
, c
->gpmc_cs
,
599 freq
, latency
, &info
);
601 goto err_release_onenand
;
603 r
= omap2_onenand_set_cfg(c
, info
.sync_read
, info
.sync_write
,
604 latency
, info
.burst_len
);
606 goto err_release_onenand
;
608 if (info
.sync_read
|| info
.sync_write
)
609 dev_info(dev
, "optimized timings for %d MHz\n", freq
);
612 r
= mtd_device_register(&c
->mtd
, NULL
, 0);
614 goto err_release_onenand
;
616 platform_set_drvdata(pdev
, c
);
621 onenand_release(&c
->mtd
);
624 dma_release_channel(c
->dma_chan
);
629 static int omap2_onenand_remove(struct platform_device
*pdev
)
631 struct omap2_onenand
*c
= dev_get_drvdata(&pdev
->dev
);
633 onenand_release(&c
->mtd
);
635 dma_release_channel(c
->dma_chan
);
636 omap2_onenand_shutdown(pdev
);
641 static const struct of_device_id omap2_onenand_id_table
[] = {
642 { .compatible
= "ti,omap2-onenand", },
645 MODULE_DEVICE_TABLE(of
, omap2_onenand_id_table
);
647 static struct platform_driver omap2_onenand_driver
= {
648 .probe
= omap2_onenand_probe
,
649 .remove
= omap2_onenand_remove
,
650 .shutdown
= omap2_onenand_shutdown
,
653 .of_match_table
= omap2_onenand_id_table
,
657 module_platform_driver(omap2_onenand_driver
);
659 MODULE_ALIAS("platform:" DRIVER_NAME
);
660 MODULE_LICENSE("GPL");
661 MODULE_AUTHOR("Jarkko Lavinen <jarkko.lavinen@nokia.com>");
662 MODULE_DESCRIPTION("Glue layer for OneNAND flash on OMAP2 / OMAP3");