blob: 324402fcdedc1867155d8a4d89effab754291c6e [file] [log] [blame]
Adrian Hunter36cd4fb2008-08-06 10:08:46 +03001/*
2 * linux/drivers/mtd/onenand/omap2.c
3 *
4 * OneNAND driver for OMAP2 / OMAP3
5 *
6 * Copyright © 2005-2006 Nokia Corporation
7 *
8 * Author: Jarkko Lavinen <jarkko.lavinen@nokia.com> and Juha Yrjölä
9 * IRQ and DMA support written by Timo Teras
10 *
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.
14 *
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
18 * more details.
19 *
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.
23 *
24 */
25
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>
Adrian Huntercbbd6952008-11-24 14:44:36 +020035#include <linux/dma-mapping.h>
36#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Adrian Hunter36cd4fb2008-08-06 10:08:46 +030038
Adrian Hunter36cd4fb2008-08-06 10:08:46 +030039#include <asm/mach/flash.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070040#include <plat/gpmc.h>
41#include <plat/onenand.h>
Adrian Hunterfe875352008-11-24 13:37:05 +020042#include <mach/gpio.h>
Adrian Hunter36cd4fb2008-08-06 10:08:46 +030043
Tony Lindgrence491cf2009-10-20 09:40:47 -070044#include <plat/dma.h>
Adrian Hunter36cd4fb2008-08-06 10:08:46 +030045
Tony Lindgrence491cf2009-10-20 09:40:47 -070046#include <plat/board.h>
Adrian Hunter36cd4fb2008-08-06 10:08:46 +030047
48#define DRIVER_NAME "omap2-onenand"
49
50#define ONENAND_IO_SIZE SZ_128K
51#define ONENAND_BUFRAM_SIZE (1024 * 5)
52
53struct omap2_onenand {
54 struct platform_device *pdev;
55 int gpmc_cs;
56 unsigned long phys_base;
57 int gpio_irq;
58 struct mtd_info mtd;
59 struct mtd_partition *parts;
60 struct onenand_chip onenand;
61 struct completion irq_done;
62 struct completion dma_done;
63 int dma_channel;
64 int freq;
65 int (*setup)(void __iomem *base, int freq);
66};
67
Adrian Hunter263a8c82009-12-30 07:40:16 +010068#ifdef CONFIG_MTD_PARTITIONS
69static const char *part_probes[] = { "cmdlinepart", NULL, };
70#endif
71
Adrian Hunter36cd4fb2008-08-06 10:08:46 +030072static void omap2_onenand_dma_cb(int lch, u16 ch_status, void *data)
73{
74 struct omap2_onenand *c = data;
75
76 complete(&c->dma_done);
77}
78
79static irqreturn_t omap2_onenand_interrupt(int irq, void *dev_id)
80{
81 struct omap2_onenand *c = dev_id;
82
83 complete(&c->irq_done);
84
85 return IRQ_HANDLED;
86}
87
88static inline unsigned short read_reg(struct omap2_onenand *c, int reg)
89{
90 return readw(c->onenand.base + reg);
91}
92
93static inline void write_reg(struct omap2_onenand *c, unsigned short value,
94 int reg)
95{
96 writew(value, c->onenand.base + reg);
97}
98
99static void wait_err(char *msg, int state, unsigned int ctrl, unsigned int intr)
100{
101 printk(KERN_ERR "onenand_wait: %s! state %d ctrl 0x%04x intr 0x%04x\n",
102 msg, state, ctrl, intr);
103}
104
105static void wait_warn(char *msg, int state, unsigned int ctrl,
106 unsigned int intr)
107{
108 printk(KERN_WARNING "onenand_wait: %s! state %d ctrl 0x%04x "
109 "intr 0x%04x\n", msg, state, ctrl, intr);
110}
111
112static int omap2_onenand_wait(struct mtd_info *mtd, int state)
113{
114 struct omap2_onenand *c = container_of(mtd, struct omap2_onenand, mtd);
Roman Tereshonkovd19d7b42010-11-03 12:55:20 +0200115 struct onenand_chip *this = mtd->priv;
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300116 unsigned int intr = 0;
Roman Tereshonkovd19d7b42010-11-03 12:55:20 +0200117 unsigned int ctrl, ctrl_mask;
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300118 unsigned long timeout;
119 u32 syscfg;
120
Mika Korhonen72073022009-10-23 07:50:43 +0200121 if (state == FL_RESETING || state == FL_PREPARING_ERASE ||
122 state == FL_VERIFYING_ERASE) {
123 int i = 21;
124 unsigned int intr_flags = ONENAND_INT_MASTER;
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300125
Mika Korhonen72073022009-10-23 07:50:43 +0200126 switch (state) {
127 case FL_RESETING:
128 intr_flags |= ONENAND_INT_RESET;
129 break;
130 case FL_PREPARING_ERASE:
131 intr_flags |= ONENAND_INT_ERASE;
132 break;
133 case FL_VERIFYING_ERASE:
134 i = 101;
135 break;
136 }
137
138 while (--i) {
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300139 udelay(1);
140 intr = read_reg(c, ONENAND_REG_INTERRUPT);
141 if (intr & ONENAND_INT_MASTER)
142 break;
143 }
144 ctrl = read_reg(c, ONENAND_REG_CTRL_STATUS);
145 if (ctrl & ONENAND_CTRL_ERROR) {
146 wait_err("controller error", state, ctrl, intr);
147 return -EIO;
148 }
Mika Korhonen72073022009-10-23 07:50:43 +0200149 if ((intr & intr_flags) != intr_flags) {
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300150 wait_err("timeout", state, ctrl, intr);
151 return -EIO;
152 }
153 return 0;
154 }
155
156 if (state != FL_READING) {
157 int result;
158
159 /* Turn interrupts on */
160 syscfg = read_reg(c, ONENAND_REG_SYS_CFG1);
Adrian Hunter782b7a32008-08-14 14:00:12 +0300161 if (!(syscfg & ONENAND_SYS_CFG1_IOBE)) {
162 syscfg |= ONENAND_SYS_CFG1_IOBE;
163 write_reg(c, syscfg, ONENAND_REG_SYS_CFG1);
164 if (cpu_is_omap34xx())
165 /* Add a delay to let GPIO settle */
166 syscfg = read_reg(c, ONENAND_REG_SYS_CFG1);
167 }
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300168
169 INIT_COMPLETION(c->irq_done);
170 if (c->gpio_irq) {
David Brownell0b84b5c2008-12-10 17:35:25 -0800171 result = gpio_get_value(c->gpio_irq);
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300172 if (result == -1) {
173 ctrl = read_reg(c, ONENAND_REG_CTRL_STATUS);
174 intr = read_reg(c, ONENAND_REG_INTERRUPT);
175 wait_err("gpio error", state, ctrl, intr);
176 return -EIO;
177 }
178 } else
179 result = 0;
180 if (result == 0) {
181 int retry_cnt = 0;
182retry:
183 result = wait_for_completion_timeout(&c->irq_done,
184 msecs_to_jiffies(20));
185 if (result == 0) {
186 /* Timeout after 20ms */
187 ctrl = read_reg(c, ONENAND_REG_CTRL_STATUS);
Roman Tereshonkovd19d7b42010-11-03 12:55:20 +0200188 if (ctrl & ONENAND_CTRL_ONGO &&
189 !this->ongoing) {
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300190 /*
191 * The operation seems to be still going
192 * so give it some more time.
193 */
194 retry_cnt += 1;
195 if (retry_cnt < 3)
196 goto retry;
197 intr = read_reg(c,
198 ONENAND_REG_INTERRUPT);
199 wait_err("timeout", state, ctrl, intr);
200 return -EIO;
201 }
202 intr = read_reg(c, ONENAND_REG_INTERRUPT);
203 if ((intr & ONENAND_INT_MASTER) == 0)
204 wait_warn("timeout", state, ctrl, intr);
205 }
206 }
207 } else {
Adrian Hunter8afbc112008-08-25 12:01:31 +0300208 int retry_cnt = 0;
209
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300210 /* Turn interrupts off */
211 syscfg = read_reg(c, ONENAND_REG_SYS_CFG1);
212 syscfg &= ~ONENAND_SYS_CFG1_IOBE;
213 write_reg(c, syscfg, ONENAND_REG_SYS_CFG1);
214
215 timeout = jiffies + msecs_to_jiffies(20);
Adrian Hunter8afbc112008-08-25 12:01:31 +0300216 while (1) {
217 if (time_before(jiffies, timeout)) {
218 intr = read_reg(c, ONENAND_REG_INTERRUPT);
219 if (intr & ONENAND_INT_MASTER)
220 break;
221 } else {
222 /* Timeout after 20ms */
223 ctrl = read_reg(c, ONENAND_REG_CTRL_STATUS);
224 if (ctrl & ONENAND_CTRL_ONGO) {
225 /*
226 * The operation seems to be still going
227 * so give it some more time.
228 */
229 retry_cnt += 1;
230 if (retry_cnt < 3) {
231 timeout = jiffies +
232 msecs_to_jiffies(20);
233 continue;
234 }
235 }
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300236 break;
Adrian Hunter8afbc112008-08-25 12:01:31 +0300237 }
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300238 }
239 }
240
241 intr = read_reg(c, ONENAND_REG_INTERRUPT);
242 ctrl = read_reg(c, ONENAND_REG_CTRL_STATUS);
243
244 if (intr & ONENAND_INT_READ) {
245 int ecc = read_reg(c, ONENAND_REG_ECC_STATUS);
246
247 if (ecc) {
248 unsigned int addr1, addr8;
249
250 addr1 = read_reg(c, ONENAND_REG_START_ADDRESS1);
251 addr8 = read_reg(c, ONENAND_REG_START_ADDRESS8);
252 if (ecc & ONENAND_ECC_2BIT_ALL) {
253 printk(KERN_ERR "onenand_wait: ECC error = "
254 "0x%04x, addr1 %#x, addr8 %#x\n",
255 ecc, addr1, addr8);
256 mtd->ecc_stats.failed++;
257 return -EBADMSG;
258 } else if (ecc & ONENAND_ECC_1BIT_ALL) {
259 printk(KERN_NOTICE "onenand_wait: correctable "
260 "ECC error = 0x%04x, addr1 %#x, "
261 "addr8 %#x\n", ecc, addr1, addr8);
262 mtd->ecc_stats.corrected++;
263 }
264 }
265 } else if (state == FL_READING) {
266 wait_err("timeout", state, ctrl, intr);
267 return -EIO;
268 }
269
270 if (ctrl & ONENAND_CTRL_ERROR) {
271 wait_err("controller error", state, ctrl, intr);
272 if (ctrl & ONENAND_CTRL_LOCK)
273 printk(KERN_ERR "onenand_wait: "
274 "Device is write protected!!!\n");
275 return -EIO;
276 }
277
Roman Tereshonkovd19d7b42010-11-03 12:55:20 +0200278 ctrl_mask = 0xFE9F;
279 if (this->ongoing)
280 ctrl_mask &= ~0x8000;
281
282 if (ctrl & ctrl_mask)
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300283 wait_warn("unexpected controller status", state, ctrl, intr);
284
285 return 0;
286}
287
288static inline int omap2_onenand_bufferram_offset(struct mtd_info *mtd, int area)
289{
290 struct onenand_chip *this = mtd->priv;
291
292 if (ONENAND_CURRENT_BUFFERRAM(this)) {
293 if (area == ONENAND_DATARAM)
Mika Korhonen00acf4a2009-06-11 14:05:07 +0300294 return this->writesize;
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300295 if (area == ONENAND_SPARERAM)
296 return mtd->oobsize;
297 }
298
299 return 0;
300}
301
302#if defined(CONFIG_ARCH_OMAP3) || defined(MULTI_OMAP2)
303
304static int omap3_onenand_read_bufferram(struct mtd_info *mtd, int area,
305 unsigned char *buffer, int offset,
306 size_t count)
307{
308 struct omap2_onenand *c = container_of(mtd, struct omap2_onenand, mtd);
309 struct onenand_chip *this = mtd->priv;
310 dma_addr_t dma_src, dma_dst;
311 int bram_offset;
312 unsigned long timeout;
313 void *buf = (void *)buffer;
314 size_t xtra;
315 volatile unsigned *done;
316
317 bram_offset = omap2_onenand_bufferram_offset(mtd, area) + area + offset;
318 if (bram_offset & 3 || (size_t)buf & 3 || count < 384)
319 goto out_copy;
320
Adrian Huntera29f2802009-03-23 14:57:38 +0200321 /* panic_write() may be in an interrupt context */
Aaro Koskinen932f5d22010-02-10 19:03:19 +0200322 if (in_interrupt() || oops_in_progress)
Adrian Huntera29f2802009-03-23 14:57:38 +0200323 goto out_copy;
324
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300325 if (buf >= high_memory) {
326 struct page *p1;
327
328 if (((size_t)buf & PAGE_MASK) !=
329 ((size_t)(buf + count - 1) & PAGE_MASK))
330 goto out_copy;
331 p1 = vmalloc_to_page(buf);
332 if (!p1)
333 goto out_copy;
334 buf = page_address(p1) + ((size_t)buf & ~PAGE_MASK);
335 }
336
337 xtra = count & 3;
338 if (xtra) {
339 count -= xtra;
340 memcpy(buf + count, this->base + bram_offset + count, xtra);
341 }
342
343 dma_src = c->phys_base + bram_offset;
344 dma_dst = dma_map_single(&c->pdev->dev, buf, count, DMA_FROM_DEVICE);
345 if (dma_mapping_error(&c->pdev->dev, dma_dst)) {
346 dev_err(&c->pdev->dev,
347 "Couldn't DMA map a %d byte buffer\n",
348 count);
349 goto out_copy;
350 }
351
352 omap_set_dma_transfer_params(c->dma_channel, OMAP_DMA_DATA_TYPE_S32,
353 count >> 2, 1, 0, 0, 0);
354 omap_set_dma_src_params(c->dma_channel, 0, OMAP_DMA_AMODE_POST_INC,
355 dma_src, 0, 0);
356 omap_set_dma_dest_params(c->dma_channel, 0, OMAP_DMA_AMODE_POST_INC,
357 dma_dst, 0, 0);
358
359 INIT_COMPLETION(c->dma_done);
360 omap_start_dma(c->dma_channel);
361
362 timeout = jiffies + msecs_to_jiffies(20);
363 done = &c->dma_done.done;
364 while (time_before(jiffies, timeout))
365 if (*done)
366 break;
367
368 dma_unmap_single(&c->pdev->dev, dma_dst, count, DMA_FROM_DEVICE);
369
370 if (!*done) {
371 dev_err(&c->pdev->dev, "timeout waiting for DMA\n");
372 goto out_copy;
373 }
374
375 return 0;
376
377out_copy:
378 memcpy(buf, this->base + bram_offset, count);
379 return 0;
380}
381
382static int omap3_onenand_write_bufferram(struct mtd_info *mtd, int area,
383 const unsigned char *buffer,
384 int offset, size_t count)
385{
386 struct omap2_onenand *c = container_of(mtd, struct omap2_onenand, mtd);
387 struct onenand_chip *this = mtd->priv;
388 dma_addr_t dma_src, dma_dst;
389 int bram_offset;
390 unsigned long timeout;
391 void *buf = (void *)buffer;
392 volatile unsigned *done;
393
394 bram_offset = omap2_onenand_bufferram_offset(mtd, area) + area + offset;
395 if (bram_offset & 3 || (size_t)buf & 3 || count < 384)
396 goto out_copy;
397
398 /* panic_write() may be in an interrupt context */
Aaro Koskinen932f5d22010-02-10 19:03:19 +0200399 if (in_interrupt() || oops_in_progress)
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300400 goto out_copy;
401
402 if (buf >= high_memory) {
403 struct page *p1;
404
405 if (((size_t)buf & PAGE_MASK) !=
406 ((size_t)(buf + count - 1) & PAGE_MASK))
407 goto out_copy;
408 p1 = vmalloc_to_page(buf);
409 if (!p1)
410 goto out_copy;
411 buf = page_address(p1) + ((size_t)buf & ~PAGE_MASK);
412 }
413
414 dma_src = dma_map_single(&c->pdev->dev, buf, count, DMA_TO_DEVICE);
415 dma_dst = c->phys_base + bram_offset;
Mika Westerberg4a70b7d2010-03-24 12:10:48 +0200416 if (dma_mapping_error(&c->pdev->dev, dma_src)) {
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300417 dev_err(&c->pdev->dev,
418 "Couldn't DMA map a %d byte buffer\n",
419 count);
420 return -1;
421 }
422
423 omap_set_dma_transfer_params(c->dma_channel, OMAP_DMA_DATA_TYPE_S32,
424 count >> 2, 1, 0, 0, 0);
425 omap_set_dma_src_params(c->dma_channel, 0, OMAP_DMA_AMODE_POST_INC,
426 dma_src, 0, 0);
427 omap_set_dma_dest_params(c->dma_channel, 0, OMAP_DMA_AMODE_POST_INC,
428 dma_dst, 0, 0);
429
430 INIT_COMPLETION(c->dma_done);
431 omap_start_dma(c->dma_channel);
432
433 timeout = jiffies + msecs_to_jiffies(20);
434 done = &c->dma_done.done;
435 while (time_before(jiffies, timeout))
436 if (*done)
437 break;
438
Mika Westerberg4a70b7d2010-03-24 12:10:48 +0200439 dma_unmap_single(&c->pdev->dev, dma_src, count, DMA_TO_DEVICE);
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300440
441 if (!*done) {
442 dev_err(&c->pdev->dev, "timeout waiting for DMA\n");
443 goto out_copy;
444 }
445
446 return 0;
447
448out_copy:
449 memcpy(this->base + bram_offset, buf, count);
450 return 0;
451}
452
453#else
454
455int omap3_onenand_read_bufferram(struct mtd_info *mtd, int area,
456 unsigned char *buffer, int offset,
457 size_t count);
458
459int omap3_onenand_write_bufferram(struct mtd_info *mtd, int area,
460 const unsigned char *buffer,
461 int offset, size_t count);
462
463#endif
464
465#if defined(CONFIG_ARCH_OMAP2) || defined(MULTI_OMAP2)
466
467static int omap2_onenand_read_bufferram(struct mtd_info *mtd, int area,
468 unsigned char *buffer, int offset,
469 size_t count)
470{
471 struct omap2_onenand *c = container_of(mtd, struct omap2_onenand, mtd);
472 struct onenand_chip *this = mtd->priv;
473 dma_addr_t dma_src, dma_dst;
474 int bram_offset;
475
476 bram_offset = omap2_onenand_bufferram_offset(mtd, area) + area + offset;
477 /* DMA is not used. Revisit PM requirements before enabling it. */
478 if (1 || (c->dma_channel < 0) ||
479 ((void *) buffer >= (void *) high_memory) || (bram_offset & 3) ||
480 (((unsigned int) buffer) & 3) || (count < 1024) || (count & 3)) {
481 memcpy(buffer, (__force void *)(this->base + bram_offset),
482 count);
483 return 0;
484 }
485
486 dma_src = c->phys_base + bram_offset;
487 dma_dst = dma_map_single(&c->pdev->dev, buffer, count,
488 DMA_FROM_DEVICE);
489 if (dma_mapping_error(&c->pdev->dev, dma_dst)) {
490 dev_err(&c->pdev->dev,
491 "Couldn't DMA map a %d byte buffer\n",
492 count);
493 return -1;
494 }
495
496 omap_set_dma_transfer_params(c->dma_channel, OMAP_DMA_DATA_TYPE_S32,
497 count / 4, 1, 0, 0, 0);
498 omap_set_dma_src_params(c->dma_channel, 0, OMAP_DMA_AMODE_POST_INC,
499 dma_src, 0, 0);
500 omap_set_dma_dest_params(c->dma_channel, 0, OMAP_DMA_AMODE_POST_INC,
501 dma_dst, 0, 0);
502
503 INIT_COMPLETION(c->dma_done);
504 omap_start_dma(c->dma_channel);
505 wait_for_completion(&c->dma_done);
506
507 dma_unmap_single(&c->pdev->dev, dma_dst, count, DMA_FROM_DEVICE);
508
509 return 0;
510}
511
512static int omap2_onenand_write_bufferram(struct mtd_info *mtd, int area,
513 const unsigned char *buffer,
514 int offset, size_t count)
515{
516 struct omap2_onenand *c = container_of(mtd, struct omap2_onenand, mtd);
517 struct onenand_chip *this = mtd->priv;
518 dma_addr_t dma_src, dma_dst;
519 int bram_offset;
520
521 bram_offset = omap2_onenand_bufferram_offset(mtd, area) + area + offset;
522 /* DMA is not used. Revisit PM requirements before enabling it. */
523 if (1 || (c->dma_channel < 0) ||
524 ((void *) buffer >= (void *) high_memory) || (bram_offset & 3) ||
525 (((unsigned int) buffer) & 3) || (count < 1024) || (count & 3)) {
526 memcpy((__force void *)(this->base + bram_offset), buffer,
527 count);
528 return 0;
529 }
530
531 dma_src = dma_map_single(&c->pdev->dev, (void *) buffer, count,
532 DMA_TO_DEVICE);
533 dma_dst = c->phys_base + bram_offset;
Mika Westerberg4a70b7d2010-03-24 12:10:48 +0200534 if (dma_mapping_error(&c->pdev->dev, dma_src)) {
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300535 dev_err(&c->pdev->dev,
536 "Couldn't DMA map a %d byte buffer\n",
537 count);
538 return -1;
539 }
540
541 omap_set_dma_transfer_params(c->dma_channel, OMAP_DMA_DATA_TYPE_S16,
542 count / 2, 1, 0, 0, 0);
543 omap_set_dma_src_params(c->dma_channel, 0, OMAP_DMA_AMODE_POST_INC,
544 dma_src, 0, 0);
545 omap_set_dma_dest_params(c->dma_channel, 0, OMAP_DMA_AMODE_POST_INC,
546 dma_dst, 0, 0);
547
548 INIT_COMPLETION(c->dma_done);
549 omap_start_dma(c->dma_channel);
550 wait_for_completion(&c->dma_done);
551
Mika Westerberg4a70b7d2010-03-24 12:10:48 +0200552 dma_unmap_single(&c->pdev->dev, dma_src, count, DMA_TO_DEVICE);
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300553
554 return 0;
555}
556
557#else
558
559int omap2_onenand_read_bufferram(struct mtd_info *mtd, int area,
560 unsigned char *buffer, int offset,
561 size_t count);
562
563int omap2_onenand_write_bufferram(struct mtd_info *mtd, int area,
564 const unsigned char *buffer,
565 int offset, size_t count);
566
567#endif
568
569static struct platform_driver omap2_onenand_driver;
570
571static int __adjust_timing(struct device *dev, void *data)
572{
573 int ret = 0;
574 struct omap2_onenand *c;
575
576 c = dev_get_drvdata(dev);
577
578 BUG_ON(c->setup == NULL);
579
580 /* DMA is not in use so this is all that is needed */
581 /* Revisit for OMAP3! */
582 ret = c->setup(c->onenand.base, c->freq);
583
584 return ret;
585}
586
587int omap2_onenand_rephase(void)
588{
589 return driver_for_each_device(&omap2_onenand_driver.driver, NULL,
590 NULL, __adjust_timing);
591}
592
Mika Korhonend3412db2009-05-21 23:09:42 +0300593static void omap2_onenand_shutdown(struct platform_device *pdev)
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300594{
595 struct omap2_onenand *c = dev_get_drvdata(&pdev->dev);
596
597 /* With certain content in the buffer RAM, the OMAP boot ROM code
598 * can recognize the flash chip incorrectly. Zero it out before
599 * soft reset.
600 */
601 memset((__force void *)c->onenand.base, 0, ONENAND_BUFRAM_SIZE);
602}
603
604static int __devinit omap2_onenand_probe(struct platform_device *pdev)
605{
606 struct omap_onenand_platform_data *pdata;
607 struct omap2_onenand *c;
608 int r;
609
610 pdata = pdev->dev.platform_data;
611 if (pdata == NULL) {
612 dev_err(&pdev->dev, "platform data missing\n");
613 return -ENODEV;
614 }
615
616 c = kzalloc(sizeof(struct omap2_onenand), GFP_KERNEL);
617 if (!c)
618 return -ENOMEM;
619
620 init_completion(&c->irq_done);
621 init_completion(&c->dma_done);
622 c->gpmc_cs = pdata->cs;
623 c->gpio_irq = pdata->gpio_irq;
624 c->dma_channel = pdata->dma_channel;
625 if (c->dma_channel < 0) {
626 /* if -1, don't use DMA */
627 c->gpio_irq = 0;
628 }
629
630 r = gpmc_cs_request(c->gpmc_cs, ONENAND_IO_SIZE, &c->phys_base);
631 if (r < 0) {
632 dev_err(&pdev->dev, "Cannot request GPMC CS\n");
633 goto err_kfree;
634 }
635
636 if (request_mem_region(c->phys_base, ONENAND_IO_SIZE,
637 pdev->dev.driver->name) == NULL) {
638 dev_err(&pdev->dev, "Cannot reserve memory region at 0x%08lx, "
639 "size: 0x%x\n", c->phys_base, ONENAND_IO_SIZE);
640 r = -EBUSY;
641 goto err_free_cs;
642 }
643 c->onenand.base = ioremap(c->phys_base, ONENAND_IO_SIZE);
644 if (c->onenand.base == NULL) {
645 r = -ENOMEM;
646 goto err_release_mem_region;
647 }
648
649 if (pdata->onenand_setup != NULL) {
650 r = pdata->onenand_setup(c->onenand.base, c->freq);
651 if (r < 0) {
652 dev_err(&pdev->dev, "Onenand platform setup failed: "
653 "%d\n", r);
654 goto err_iounmap;
655 }
656 c->setup = pdata->onenand_setup;
657 }
658
659 if (c->gpio_irq) {
Jarkko Nikula73069e32009-01-15 13:09:52 +0200660 if ((r = gpio_request(c->gpio_irq, "OneNAND irq")) < 0) {
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300661 dev_err(&pdev->dev, "Failed to request GPIO%d for "
662 "OneNAND\n", c->gpio_irq);
663 goto err_iounmap;
664 }
David Brownell40e39252008-12-10 17:35:26 -0800665 gpio_direction_input(c->gpio_irq);
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300666
David Brownell15f74b02008-12-10 17:35:26 -0800667 if ((r = request_irq(gpio_to_irq(c->gpio_irq),
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300668 omap2_onenand_interrupt, IRQF_TRIGGER_RISING,
669 pdev->dev.driver->name, c)) < 0)
670 goto err_release_gpio;
671 }
672
673 if (c->dma_channel >= 0) {
674 r = omap_request_dma(0, pdev->dev.driver->name,
675 omap2_onenand_dma_cb, (void *) c,
676 &c->dma_channel);
677 if (r == 0) {
678 omap_set_dma_write_mode(c->dma_channel,
679 OMAP_DMA_WRITE_NON_POSTED);
680 omap_set_dma_src_data_pack(c->dma_channel, 1);
681 omap_set_dma_src_burst_mode(c->dma_channel,
682 OMAP_DMA_DATA_BURST_8);
683 omap_set_dma_dest_data_pack(c->dma_channel, 1);
684 omap_set_dma_dest_burst_mode(c->dma_channel,
685 OMAP_DMA_DATA_BURST_8);
686 } else {
687 dev_info(&pdev->dev,
688 "failed to allocate DMA for OneNAND, "
689 "using PIO instead\n");
690 c->dma_channel = -1;
691 }
692 }
693
694 dev_info(&pdev->dev, "initializing on CS%d, phys base 0x%08lx, virtual "
695 "base %p\n", c->gpmc_cs, c->phys_base,
696 c->onenand.base);
697
698 c->pdev = pdev;
Kay Sievers475b44c2009-01-06 10:44:38 -0800699 c->mtd.name = dev_name(&pdev->dev);
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300700 c->mtd.priv = &c->onenand;
701 c->mtd.owner = THIS_MODULE;
702
David Brownell87f39f02009-03-26 00:42:50 -0700703 c->mtd.dev.parent = &pdev->dev;
704
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300705 if (c->dma_channel >= 0) {
706 struct onenand_chip *this = &c->onenand;
707
708 this->wait = omap2_onenand_wait;
709 if (cpu_is_omap34xx()) {
710 this->read_bufferram = omap3_onenand_read_bufferram;
711 this->write_bufferram = omap3_onenand_write_bufferram;
712 } else {
713 this->read_bufferram = omap2_onenand_read_bufferram;
714 this->write_bufferram = omap2_onenand_write_bufferram;
715 }
716 }
717
718 if ((r = onenand_scan(&c->mtd, 1)) < 0)
719 goto err_release_dma;
720
721 switch ((c->onenand.version_id >> 4) & 0xf) {
722 case 0:
723 c->freq = 40;
724 break;
725 case 1:
726 c->freq = 54;
727 break;
728 case 2:
729 c->freq = 66;
730 break;
731 case 3:
732 c->freq = 83;
733 break;
734 }
735
736#ifdef CONFIG_MTD_PARTITIONS
Adrian Hunter263a8c82009-12-30 07:40:16 +0100737 r = parse_mtd_partitions(&c->mtd, part_probes, &c->parts, 0);
738 if (r > 0)
739 r = add_mtd_partitions(&c->mtd, c->parts, r);
740 else if (pdata->parts != NULL)
741 r = add_mtd_partitions(&c->mtd, pdata->parts, pdata->nr_parts);
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300742 else
743#endif
744 r = add_mtd_device(&c->mtd);
Adrian Hunter263a8c82009-12-30 07:40:16 +0100745 if (r)
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300746 goto err_release_onenand;
747
748 platform_set_drvdata(pdev, c);
749
750 return 0;
751
752err_release_onenand:
753 onenand_release(&c->mtd);
754err_release_dma:
755 if (c->dma_channel != -1)
756 omap_free_dma(c->dma_channel);
757 if (c->gpio_irq)
David Brownell15f74b02008-12-10 17:35:26 -0800758 free_irq(gpio_to_irq(c->gpio_irq), c);
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300759err_release_gpio:
760 if (c->gpio_irq)
Jarkko Nikula73069e32009-01-15 13:09:52 +0200761 gpio_free(c->gpio_irq);
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300762err_iounmap:
763 iounmap(c->onenand.base);
764err_release_mem_region:
765 release_mem_region(c->phys_base, ONENAND_IO_SIZE);
766err_free_cs:
767 gpmc_cs_free(c->gpmc_cs);
768err_kfree:
Adrian Hunter263a8c82009-12-30 07:40:16 +0100769 kfree(c->parts);
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300770 kfree(c);
771
772 return r;
773}
774
775static int __devexit omap2_onenand_remove(struct platform_device *pdev)
776{
777 struct omap2_onenand *c = dev_get_drvdata(&pdev->dev);
778
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300779 onenand_release(&c->mtd);
780 if (c->dma_channel != -1)
781 omap_free_dma(c->dma_channel);
782 omap2_onenand_shutdown(pdev);
783 platform_set_drvdata(pdev, NULL);
784 if (c->gpio_irq) {
David Brownell15f74b02008-12-10 17:35:26 -0800785 free_irq(gpio_to_irq(c->gpio_irq), c);
Jarkko Nikula73069e32009-01-15 13:09:52 +0200786 gpio_free(c->gpio_irq);
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300787 }
788 iounmap(c->onenand.base);
789 release_mem_region(c->phys_base, ONENAND_IO_SIZE);
Mika Korhonen3cae1cc2009-06-25 15:32:19 +0300790 gpmc_cs_free(c->gpmc_cs);
Adrian Hunter263a8c82009-12-30 07:40:16 +0100791 kfree(c->parts);
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300792 kfree(c);
793
794 return 0;
795}
796
797static struct platform_driver omap2_onenand_driver = {
798 .probe = omap2_onenand_probe,
Mika Korhonend3412db2009-05-21 23:09:42 +0300799 .remove = __devexit_p(omap2_onenand_remove),
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300800 .shutdown = omap2_onenand_shutdown,
801 .driver = {
802 .name = DRIVER_NAME,
803 .owner = THIS_MODULE,
804 },
805};
806
807static int __init omap2_onenand_init(void)
808{
809 printk(KERN_INFO "OneNAND driver initializing\n");
810 return platform_driver_register(&omap2_onenand_driver);
811}
812
813static void __exit omap2_onenand_exit(void)
814{
815 platform_driver_unregister(&omap2_onenand_driver);
816}
817
818module_init(omap2_onenand_init);
819module_exit(omap2_onenand_exit);
820
821MODULE_ALIAS(DRIVER_NAME);
822MODULE_LICENSE("GPL");
823MODULE_AUTHOR("Jarkko Lavinen <jarkko.lavinen@nokia.com>");
824MODULE_DESCRIPTION("Glue layer for OneNAND flash on OMAP2 / OMAP3");