blob: 9c159f0dd9a61183892bb067c41dac17386c56e6 [file] [log] [blame]
Adrian Hunter36cd4fb2008-08-06 10:08:46 +03001/*
Adrian Hunter36cd4fb2008-08-06 10:08:46 +03002 * OneNAND driver for OMAP2 / OMAP3
3 *
4 * Copyright © 2005-2006 Nokia Corporation
5 *
6 * Author: Jarkko Lavinen <jarkko.lavinen@nokia.com> and Juha Yrjölä
7 * IRQ and DMA support written by Timo Teras
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License version 2 as published by
11 * the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * this program; see the file COPYING. If not, write to the Free Software
20 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 *
22 */
23
24#include <linux/device.h>
25#include <linux/module.h>
Adrian Hunter36cd4fb2008-08-06 10:08:46 +030026#include <linux/mtd/mtd.h>
27#include <linux/mtd/onenand.h>
28#include <linux/mtd/partitions.h>
Ladislav Michla758f502018-01-12 14:17:25 +010029#include <linux/of_device.h>
30#include <linux/omap-gpmc.h>
Adrian Hunter36cd4fb2008-08-06 10:08:46 +030031#include <linux/platform_device.h>
32#include <linux/interrupt.h>
33#include <linux/delay.h>
Adrian Huntercbbd6952008-11-24 14:44:36 +020034#include <linux/dma-mapping.h>
Peter Ujfalusi3ed6a4d2018-01-12 14:16:08 +010035#include <linux/dmaengine.h>
Adrian Huntercbbd6952008-11-24 14:44:36 +020036#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Ladislav Michla758f502018-01-12 14:17:25 +010038#include <linux/gpio/consumer.h>
Adrian Hunter36cd4fb2008-08-06 10:08:46 +030039
Adrian Hunter36cd4fb2008-08-06 10:08:46 +030040#include <asm/mach/flash.h>
Adrian Hunter36cd4fb2008-08-06 10:08:46 +030041
Adrian Hunter36cd4fb2008-08-06 10:08:46 +030042#define DRIVER_NAME "omap2-onenand"
43
Adrian Hunter36cd4fb2008-08-06 10:08:46 +030044#define ONENAND_BUFRAM_SIZE (1024 * 5)
45
46struct omap2_onenand {
47 struct platform_device *pdev;
48 int gpmc_cs;
49 unsigned long phys_base;
Ladislav Michla758f502018-01-12 14:17:25 +010050 struct gpio_desc *int_gpiod;
Adrian Hunter36cd4fb2008-08-06 10:08:46 +030051 struct mtd_info mtd;
Adrian Hunter36cd4fb2008-08-06 10:08:46 +030052 struct onenand_chip onenand;
53 struct completion irq_done;
54 struct completion dma_done;
Peter Ujfalusi3ed6a4d2018-01-12 14:16:08 +010055 struct dma_chan *dma_chan;
Adrian Hunter36cd4fb2008-08-06 10:08:46 +030056};
57
Peter Ujfalusi3ed6a4d2018-01-12 14:16:08 +010058static void omap2_onenand_dma_complete_func(void *completion)
Adrian Hunter36cd4fb2008-08-06 10:08:46 +030059{
Peter Ujfalusi3ed6a4d2018-01-12 14:16:08 +010060 complete(completion);
Adrian Hunter36cd4fb2008-08-06 10:08:46 +030061}
62
63static irqreturn_t omap2_onenand_interrupt(int irq, void *dev_id)
64{
65 struct omap2_onenand *c = dev_id;
66
67 complete(&c->irq_done);
68
69 return IRQ_HANDLED;
70}
71
72static inline unsigned short read_reg(struct omap2_onenand *c, int reg)
73{
74 return readw(c->onenand.base + reg);
75}
76
77static inline void write_reg(struct omap2_onenand *c, unsigned short value,
78 int reg)
79{
80 writew(value, c->onenand.base + reg);
81}
82
Ladislav Michla758f502018-01-12 14:17:25 +010083static int omap2_onenand_set_cfg(struct omap2_onenand *c,
84 bool sr, bool sw,
85 int latency, int burst_len)
86{
87 unsigned short reg = ONENAND_SYS_CFG1_RDY | ONENAND_SYS_CFG1_INT;
88
89 reg |= latency << ONENAND_SYS_CFG1_BRL_SHIFT;
90
91 switch (burst_len) {
92 case 0: /* continuous */
93 break;
94 case 4:
95 reg |= ONENAND_SYS_CFG1_BL_4;
96 break;
97 case 8:
98 reg |= ONENAND_SYS_CFG1_BL_8;
99 break;
100 case 16:
101 reg |= ONENAND_SYS_CFG1_BL_16;
102 break;
103 case 32:
104 reg |= ONENAND_SYS_CFG1_BL_32;
105 break;
106 default:
107 return -EINVAL;
108 }
109
110 if (latency > 5)
111 reg |= ONENAND_SYS_CFG1_HF;
112 if (latency > 7)
113 reg |= ONENAND_SYS_CFG1_VHF;
114 if (sr)
115 reg |= ONENAND_SYS_CFG1_SYNC_READ;
116 if (sw)
117 reg |= ONENAND_SYS_CFG1_SYNC_WRITE;
118
119 write_reg(c, reg, ONENAND_REG_SYS_CFG1);
120
121 return 0;
122}
123
124static int omap2_onenand_get_freq(int ver)
125{
126 switch ((ver >> 4) & 0xf) {
127 case 0:
128 return 40;
129 case 1:
130 return 54;
131 case 2:
132 return 66;
133 case 3:
134 return 83;
135 case 4:
136 return 104;
137 }
138
139 return -EINVAL;
140}
141
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300142static void wait_err(char *msg, int state, unsigned int ctrl, unsigned int intr)
143{
144 printk(KERN_ERR "onenand_wait: %s! state %d ctrl 0x%04x intr 0x%04x\n",
145 msg, state, ctrl, intr);
146}
147
148static void wait_warn(char *msg, int state, unsigned int ctrl,
149 unsigned int intr)
150{
151 printk(KERN_WARNING "onenand_wait: %s! state %d ctrl 0x%04x "
152 "intr 0x%04x\n", msg, state, ctrl, intr);
153}
154
155static int omap2_onenand_wait(struct mtd_info *mtd, int state)
156{
157 struct omap2_onenand *c = container_of(mtd, struct omap2_onenand, mtd);
Roman Tereshonkovd19d7b42010-11-03 12:55:20 +0200158 struct onenand_chip *this = mtd->priv;
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300159 unsigned int intr = 0;
Roman Tereshonkovd19d7b42010-11-03 12:55:20 +0200160 unsigned int ctrl, ctrl_mask;
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300161 unsigned long timeout;
162 u32 syscfg;
163
Mika Korhonen72073022009-10-23 07:50:43 +0200164 if (state == FL_RESETING || state == FL_PREPARING_ERASE ||
165 state == FL_VERIFYING_ERASE) {
166 int i = 21;
167 unsigned int intr_flags = ONENAND_INT_MASTER;
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300168
Mika Korhonen72073022009-10-23 07:50:43 +0200169 switch (state) {
170 case FL_RESETING:
171 intr_flags |= ONENAND_INT_RESET;
172 break;
173 case FL_PREPARING_ERASE:
174 intr_flags |= ONENAND_INT_ERASE;
175 break;
176 case FL_VERIFYING_ERASE:
177 i = 101;
178 break;
179 }
180
181 while (--i) {
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300182 udelay(1);
183 intr = read_reg(c, ONENAND_REG_INTERRUPT);
184 if (intr & ONENAND_INT_MASTER)
185 break;
186 }
187 ctrl = read_reg(c, ONENAND_REG_CTRL_STATUS);
188 if (ctrl & ONENAND_CTRL_ERROR) {
189 wait_err("controller error", state, ctrl, intr);
190 return -EIO;
191 }
Roman Tereshonkovc497dd52011-02-07 10:47:01 +0200192 if ((intr & intr_flags) == intr_flags)
193 return 0;
194 /* Continue in wait for interrupt branch */
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300195 }
196
197 if (state != FL_READING) {
198 int result;
199
200 /* Turn interrupts on */
201 syscfg = read_reg(c, ONENAND_REG_SYS_CFG1);
Adrian Hunter782b7a32008-08-14 14:00:12 +0300202 if (!(syscfg & ONENAND_SYS_CFG1_IOBE)) {
203 syscfg |= ONENAND_SYS_CFG1_IOBE;
204 write_reg(c, syscfg, ONENAND_REG_SYS_CFG1);
Ladislav Michlf5229332018-01-12 14:16:28 +0100205 /* Add a delay to let GPIO settle */
206 syscfg = read_reg(c, ONENAND_REG_SYS_CFG1);
Adrian Hunter782b7a32008-08-14 14:00:12 +0300207 }
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300208
Wolfram Sang16735d02013-11-14 14:32:02 -0800209 reinit_completion(&c->irq_done);
Ladislav Michla758f502018-01-12 14:17:25 +0100210 result = gpiod_get_value(c->int_gpiod);
Ladislav Michlbdaca932018-01-12 14:16:57 +0100211 if (result < 0) {
212 ctrl = read_reg(c, ONENAND_REG_CTRL_STATUS);
213 intr = read_reg(c, ONENAND_REG_INTERRUPT);
214 wait_err("gpio error", state, ctrl, intr);
Ladislav Michla758f502018-01-12 14:17:25 +0100215 return result;
Ladislav Michlbdaca932018-01-12 14:16:57 +0100216 } else if (result == 0) {
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300217 int retry_cnt = 0;
218retry:
Ladislav Michld1205682018-01-12 14:14:54 +0100219 if (!wait_for_completion_io_timeout(&c->irq_done,
220 msecs_to_jiffies(20))) {
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300221 /* Timeout after 20ms */
222 ctrl = read_reg(c, ONENAND_REG_CTRL_STATUS);
Roman Tereshonkovd19d7b42010-11-03 12:55:20 +0200223 if (ctrl & ONENAND_CTRL_ONGO &&
224 !this->ongoing) {
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300225 /*
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 goto retry;
232 intr = read_reg(c,
233 ONENAND_REG_INTERRUPT);
234 wait_err("timeout", state, ctrl, intr);
235 return -EIO;
236 }
237 intr = read_reg(c, ONENAND_REG_INTERRUPT);
238 if ((intr & ONENAND_INT_MASTER) == 0)
239 wait_warn("timeout", state, ctrl, intr);
240 }
241 }
242 } else {
Adrian Hunter8afbc112008-08-25 12:01:31 +0300243 int retry_cnt = 0;
244
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300245 /* Turn interrupts off */
246 syscfg = read_reg(c, ONENAND_REG_SYS_CFG1);
247 syscfg &= ~ONENAND_SYS_CFG1_IOBE;
248 write_reg(c, syscfg, ONENAND_REG_SYS_CFG1);
249
250 timeout = jiffies + msecs_to_jiffies(20);
Adrian Hunter8afbc112008-08-25 12:01:31 +0300251 while (1) {
252 if (time_before(jiffies, timeout)) {
253 intr = read_reg(c, ONENAND_REG_INTERRUPT);
254 if (intr & ONENAND_INT_MASTER)
255 break;
256 } else {
257 /* Timeout after 20ms */
258 ctrl = read_reg(c, ONENAND_REG_CTRL_STATUS);
259 if (ctrl & ONENAND_CTRL_ONGO) {
260 /*
261 * The operation seems to be still going
262 * so give it some more time.
263 */
264 retry_cnt += 1;
265 if (retry_cnt < 3) {
266 timeout = jiffies +
267 msecs_to_jiffies(20);
268 continue;
269 }
270 }
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300271 break;
Adrian Hunter8afbc112008-08-25 12:01:31 +0300272 }
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300273 }
274 }
275
276 intr = read_reg(c, ONENAND_REG_INTERRUPT);
277 ctrl = read_reg(c, ONENAND_REG_CTRL_STATUS);
278
279 if (intr & ONENAND_INT_READ) {
280 int ecc = read_reg(c, ONENAND_REG_ECC_STATUS);
281
282 if (ecc) {
283 unsigned int addr1, addr8;
284
285 addr1 = read_reg(c, ONENAND_REG_START_ADDRESS1);
286 addr8 = read_reg(c, ONENAND_REG_START_ADDRESS8);
287 if (ecc & ONENAND_ECC_2BIT_ALL) {
288 printk(KERN_ERR "onenand_wait: ECC error = "
289 "0x%04x, addr1 %#x, addr8 %#x\n",
290 ecc, addr1, addr8);
291 mtd->ecc_stats.failed++;
292 return -EBADMSG;
293 } else if (ecc & ONENAND_ECC_1BIT_ALL) {
294 printk(KERN_NOTICE "onenand_wait: correctable "
295 "ECC error = 0x%04x, addr1 %#x, "
296 "addr8 %#x\n", ecc, addr1, addr8);
297 mtd->ecc_stats.corrected++;
298 }
299 }
300 } else if (state == FL_READING) {
301 wait_err("timeout", state, ctrl, intr);
302 return -EIO;
303 }
304
305 if (ctrl & ONENAND_CTRL_ERROR) {
306 wait_err("controller error", state, ctrl, intr);
307 if (ctrl & ONENAND_CTRL_LOCK)
308 printk(KERN_ERR "onenand_wait: "
309 "Device is write protected!!!\n");
310 return -EIO;
311 }
312
Roman Tereshonkovd19d7b42010-11-03 12:55:20 +0200313 ctrl_mask = 0xFE9F;
314 if (this->ongoing)
315 ctrl_mask &= ~0x8000;
316
317 if (ctrl & ctrl_mask)
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300318 wait_warn("unexpected controller status", state, ctrl, intr);
319
320 return 0;
321}
322
323static inline int omap2_onenand_bufferram_offset(struct mtd_info *mtd, int area)
324{
325 struct onenand_chip *this = mtd->priv;
326
327 if (ONENAND_CURRENT_BUFFERRAM(this)) {
328 if (area == ONENAND_DATARAM)
Mika Korhonen00acf4a2009-06-11 14:05:07 +0300329 return this->writesize;
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300330 if (area == ONENAND_SPARERAM)
331 return mtd->oobsize;
332 }
333
334 return 0;
335}
336
Peter Ujfalusi36213112018-01-12 14:15:25 +0100337static inline int omap2_onenand_dma_transfer(struct omap2_onenand *c,
338 dma_addr_t src, dma_addr_t dst,
339 size_t count)
340{
Peter Ujfalusi3ed6a4d2018-01-12 14:16:08 +0100341 struct dma_async_tx_descriptor *tx;
342 dma_cookie_t cookie;
Peter Ujfalusi36213112018-01-12 14:15:25 +0100343
Peter Ujfalusi3ed6a4d2018-01-12 14:16:08 +0100344 tx = dmaengine_prep_dma_memcpy(c->dma_chan, dst, src, count, 0);
345 if (!tx) {
346 dev_err(&c->pdev->dev, "Failed to prepare DMA memcpy\n");
347 return -EIO;
348 }
Peter Ujfalusi36213112018-01-12 14:15:25 +0100349
350 reinit_completion(&c->dma_done);
Peter Ujfalusi3ed6a4d2018-01-12 14:16:08 +0100351
352 tx->callback = omap2_onenand_dma_complete_func;
353 tx->callback_param = &c->dma_done;
354
355 cookie = tx->tx_submit(tx);
356 if (dma_submit_error(cookie)) {
357 dev_err(&c->pdev->dev, "Failed to do DMA tx_submit\n");
358 return -EIO;
359 }
360
361 dma_async_issue_pending(c->dma_chan);
362
Peter Ujfalusi36213112018-01-12 14:15:25 +0100363 if (!wait_for_completion_io_timeout(&c->dma_done,
364 msecs_to_jiffies(20))) {
Peter Ujfalusi3ed6a4d2018-01-12 14:16:08 +0100365 dmaengine_terminate_sync(c->dma_chan);
Peter Ujfalusi36213112018-01-12 14:15:25 +0100366 return -ETIMEDOUT;
367 }
368
369 return 0;
370}
371
Ladislav Michlfb250702018-01-12 14:15:45 +0100372static int omap2_onenand_read_bufferram(struct mtd_info *mtd, int area,
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300373 unsigned char *buffer, int offset,
374 size_t count)
375{
376 struct omap2_onenand *c = container_of(mtd, struct omap2_onenand, mtd);
377 struct onenand_chip *this = mtd->priv;
378 dma_addr_t dma_src, dma_dst;
379 int bram_offset;
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300380 void *buf = (void *)buffer;
381 size_t xtra;
Peter Ujfalusi36213112018-01-12 14:15:25 +0100382 int ret;
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300383
384 bram_offset = omap2_onenand_bufferram_offset(mtd, area) + area + offset;
385 if (bram_offset & 3 || (size_t)buf & 3 || count < 384)
386 goto out_copy;
387
Adrian Huntera29f2802009-03-23 14:57:38 +0200388 /* panic_write() may be in an interrupt context */
Aaro Koskinen932f5d22010-02-10 19:03:19 +0200389 if (in_interrupt() || oops_in_progress)
Adrian Huntera29f2802009-03-23 14:57:38 +0200390 goto out_copy;
391
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300392 if (buf >= high_memory) {
393 struct page *p1;
394
395 if (((size_t)buf & PAGE_MASK) !=
396 ((size_t)(buf + count - 1) & PAGE_MASK))
397 goto out_copy;
398 p1 = vmalloc_to_page(buf);
399 if (!p1)
400 goto out_copy;
401 buf = page_address(p1) + ((size_t)buf & ~PAGE_MASK);
402 }
403
404 xtra = count & 3;
405 if (xtra) {
406 count -= xtra;
407 memcpy(buf + count, this->base + bram_offset + count, xtra);
408 }
409
410 dma_src = c->phys_base + bram_offset;
411 dma_dst = dma_map_single(&c->pdev->dev, buf, count, DMA_FROM_DEVICE);
412 if (dma_mapping_error(&c->pdev->dev, dma_dst)) {
413 dev_err(&c->pdev->dev,
414 "Couldn't DMA map a %d byte buffer\n",
415 count);
416 goto out_copy;
417 }
418
Peter Ujfalusi36213112018-01-12 14:15:25 +0100419 ret = omap2_onenand_dma_transfer(c, dma_src, dma_dst, count);
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300420 dma_unmap_single(&c->pdev->dev, dma_dst, count, DMA_FROM_DEVICE);
421
Peter Ujfalusi36213112018-01-12 14:15:25 +0100422 if (ret) {
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300423 dev_err(&c->pdev->dev, "timeout waiting for DMA\n");
424 goto out_copy;
425 }
426
427 return 0;
428
429out_copy:
430 memcpy(buf, this->base + bram_offset, count);
431 return 0;
432}
433
Ladislav Michlfb250702018-01-12 14:15:45 +0100434static int omap2_onenand_write_bufferram(struct mtd_info *mtd, int area,
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300435 const unsigned char *buffer,
436 int offset, size_t count)
437{
438 struct omap2_onenand *c = container_of(mtd, struct omap2_onenand, mtd);
439 struct onenand_chip *this = mtd->priv;
440 dma_addr_t dma_src, dma_dst;
441 int bram_offset;
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300442 void *buf = (void *)buffer;
Peter Ujfalusi36213112018-01-12 14:15:25 +0100443 int ret;
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300444
445 bram_offset = omap2_onenand_bufferram_offset(mtd, area) + area + offset;
446 if (bram_offset & 3 || (size_t)buf & 3 || count < 384)
447 goto out_copy;
448
449 /* panic_write() may be in an interrupt context */
Aaro Koskinen932f5d22010-02-10 19:03:19 +0200450 if (in_interrupt() || oops_in_progress)
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300451 goto out_copy;
452
453 if (buf >= high_memory) {
454 struct page *p1;
455
456 if (((size_t)buf & PAGE_MASK) !=
457 ((size_t)(buf + count - 1) & PAGE_MASK))
458 goto out_copy;
459 p1 = vmalloc_to_page(buf);
460 if (!p1)
461 goto out_copy;
462 buf = page_address(p1) + ((size_t)buf & ~PAGE_MASK);
463 }
464
465 dma_src = dma_map_single(&c->pdev->dev, buf, count, DMA_TO_DEVICE);
466 dma_dst = c->phys_base + bram_offset;
Mika Westerberg4a70b7d2010-03-24 12:10:48 +0200467 if (dma_mapping_error(&c->pdev->dev, dma_src)) {
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300468 dev_err(&c->pdev->dev,
469 "Couldn't DMA map a %d byte buffer\n",
470 count);
471 return -1;
472 }
473
Peter Ujfalusi36213112018-01-12 14:15:25 +0100474 ret = omap2_onenand_dma_transfer(c, dma_src, dma_dst, count);
Mika Westerberg4a70b7d2010-03-24 12:10:48 +0200475 dma_unmap_single(&c->pdev->dev, dma_src, count, DMA_TO_DEVICE);
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300476
Peter Ujfalusi36213112018-01-12 14:15:25 +0100477 if (ret) {
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300478 dev_err(&c->pdev->dev, "timeout waiting for DMA\n");
479 goto out_copy;
480 }
481
482 return 0;
483
484out_copy:
485 memcpy(this->base + bram_offset, buf, count);
486 return 0;
487}
488
Mika Korhonend3412db2009-05-21 23:09:42 +0300489static void omap2_onenand_shutdown(struct platform_device *pdev)
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300490{
491 struct omap2_onenand *c = dev_get_drvdata(&pdev->dev);
492
493 /* With certain content in the buffer RAM, the OMAP boot ROM code
494 * can recognize the flash chip incorrectly. Zero it out before
495 * soft reset.
496 */
497 memset((__force void *)c->onenand.base, 0, ONENAND_BUFRAM_SIZE);
498}
499
Bill Pemberton06f25512012-11-19 13:23:07 -0500500static int omap2_onenand_probe(struct platform_device *pdev)
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300501{
Ladislav Michla758f502018-01-12 14:17:25 +0100502 u32 val;
Ladislav Michlbdaca932018-01-12 14:16:57 +0100503 dma_cap_mask_t mask;
Ladislav Michla758f502018-01-12 14:17:25 +0100504 int freq, latency, r;
Afzal Mohammedd65ccb62012-08-30 12:53:23 -0700505 struct resource *res;
Ladislav Michla758f502018-01-12 14:17:25 +0100506 struct omap2_onenand *c;
507 struct gpmc_onenand_info info;
508 struct device *dev = &pdev->dev;
509 struct device_node *np = dev->of_node;
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300510
Ladislav Michla758f502018-01-12 14:17:25 +0100511 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
512 if (!res) {
513 dev_err(dev, "error getting memory resource\n");
514 return -EINVAL;
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300515 }
516
Ladislav Michla758f502018-01-12 14:17:25 +0100517 r = of_property_read_u32(np, "reg", &val);
518 if (r) {
519 dev_err(dev, "reg not found in DT\n");
520 return r;
521 }
522
523 c = devm_kzalloc(dev, sizeof(struct omap2_onenand), GFP_KERNEL);
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300524 if (!c)
525 return -ENOMEM;
526
527 init_completion(&c->irq_done);
528 init_completion(&c->dma_done);
Ladislav Michla758f502018-01-12 14:17:25 +0100529 c->gpmc_cs = val;
Afzal Mohammedd65ccb62012-08-30 12:53:23 -0700530 c->phys_base = res->start;
Afzal Mohammedd65ccb62012-08-30 12:53:23 -0700531
Ladislav Michla758f502018-01-12 14:17:25 +0100532 c->onenand.base = devm_ioremap_resource(dev, res);
Wei Yongjune6b90db82018-01-17 11:25:33 +0000533 if (IS_ERR(c->onenand.base))
Ladislav Michla758f502018-01-12 14:17:25 +0100534 return PTR_ERR(c->onenand.base);
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300535
Ladislav Michla758f502018-01-12 14:17:25 +0100536 c->int_gpiod = devm_gpiod_get_optional(dev, "int", GPIOD_IN);
537 if (IS_ERR(c->int_gpiod)) {
538 r = PTR_ERR(c->int_gpiod);
539 /* Just try again if this happens */
540 if (r != -EPROBE_DEFER)
541 dev_err(dev, "error getting gpio: %d\n", r);
542 return r;
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300543 }
544
Ladislav Michla758f502018-01-12 14:17:25 +0100545 if (c->int_gpiod) {
546 r = devm_request_irq(dev, gpiod_to_irq(c->int_gpiod),
547 omap2_onenand_interrupt,
548 IRQF_TRIGGER_RISING, "onenand", c);
549 if (r)
550 return r;
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300551
Ladislav Michla758f502018-01-12 14:17:25 +0100552 c->onenand.wait = omap2_onenand_wait;
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300553 }
554
Ladislav Michlbdaca932018-01-12 14:16:57 +0100555 dma_cap_zero(mask);
556 dma_cap_set(DMA_MEMCPY, mask);
Peter Ujfalusi3ed6a4d2018-01-12 14:16:08 +0100557
Ladislav Michlbdaca932018-01-12 14:16:57 +0100558 c->dma_chan = dma_request_channel(mask, NULL, NULL);
Ladislav Michla758f502018-01-12 14:17:25 +0100559 if (c->dma_chan) {
560 c->onenand.read_bufferram = omap2_onenand_read_bufferram;
561 c->onenand.write_bufferram = omap2_onenand_write_bufferram;
562 }
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300563
564 c->pdev = pdev;
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300565 c->mtd.priv = &c->onenand;
Ladislav Michla758f502018-01-12 14:17:25 +0100566 c->mtd.dev.parent = dev;
567 mtd_set_of_node(&c->mtd, dev->of_node);
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300568
Ladislav Michla758f502018-01-12 14:17:25 +0100569 dev_info(dev, "initializing on CS%d (0x%08lx), va %p, %s mode\n",
570 c->gpmc_cs, c->phys_base, c->onenand.base,
571 c->dma_chan ? "DMA" : "PIO");
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300572
573 if ((r = onenand_scan(&c->mtd, 1)) < 0)
Ladislav Michle6854e02018-01-12 14:13:36 +0100574 goto err_release_dma;
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300575
Ladislav Michla758f502018-01-12 14:17:25 +0100576 freq = omap2_onenand_get_freq(c->onenand.version_id);
577 if (freq > 0) {
578 switch (freq) {
579 case 104:
580 latency = 7;
581 break;
582 case 83:
583 latency = 6;
584 break;
585 case 66:
586 latency = 5;
587 break;
588 case 56:
589 latency = 4;
590 break;
591 default: /* 40 MHz or lower */
592 latency = 3;
593 break;
594 }
595
596 r = gpmc_omap_onenand_set_timings(dev, c->gpmc_cs,
597 freq, latency, &info);
598 if (r)
599 goto err_release_onenand;
600
601 r = omap2_onenand_set_cfg(c, info.sync_read, info.sync_write,
602 latency, info.burst_len);
603 if (r)
604 goto err_release_onenand;
605
606 if (info.sync_read || info.sync_write)
607 dev_info(dev, "optimized timings for %d MHz\n", freq);
608 }
609
Ladislav Michlfafc0b32018-01-12 14:14:17 +0100610 r = mtd_device_register(&c->mtd, NULL, 0);
Adrian Hunter263a8c82009-12-30 07:40:16 +0100611 if (r)
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300612 goto err_release_onenand;
613
614 platform_set_drvdata(pdev, c);
615
616 return 0;
617
618err_release_onenand:
619 onenand_release(&c->mtd);
620err_release_dma:
Peter Ujfalusi3ed6a4d2018-01-12 14:16:08 +0100621 if (c->dma_chan)
622 dma_release_channel(c->dma_chan);
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300623
624 return r;
625}
626
Bill Pemberton810b7e02012-11-19 13:26:04 -0500627static int omap2_onenand_remove(struct platform_device *pdev)
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300628{
629 struct omap2_onenand *c = dev_get_drvdata(&pdev->dev);
630
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300631 onenand_release(&c->mtd);
Peter Ujfalusi3ed6a4d2018-01-12 14:16:08 +0100632 if (c->dma_chan)
633 dma_release_channel(c->dma_chan);
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300634 omap2_onenand_shutdown(pdev);
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300635
636 return 0;
637}
638
Ladislav Michla758f502018-01-12 14:17:25 +0100639static const struct of_device_id omap2_onenand_id_table[] = {
640 { .compatible = "ti,omap2-onenand", },
641 {},
642};
643MODULE_DEVICE_TABLE(of, omap2_onenand_id_table);
644
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300645static struct platform_driver omap2_onenand_driver = {
646 .probe = omap2_onenand_probe,
Bill Pemberton5153b882012-11-19 13:21:24 -0500647 .remove = omap2_onenand_remove,
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300648 .shutdown = omap2_onenand_shutdown,
649 .driver = {
650 .name = DRIVER_NAME,
Ladislav Michla758f502018-01-12 14:17:25 +0100651 .of_match_table = omap2_onenand_id_table,
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300652 },
653};
654
Sachin Kamatcdb64042013-03-18 16:46:50 +0530655module_platform_driver(omap2_onenand_driver);
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300656
Axel Linc804c732011-03-07 11:04:24 +0800657MODULE_ALIAS("platform:" DRIVER_NAME);
Adrian Hunter36cd4fb2008-08-06 10:08:46 +0300658MODULE_LICENSE("GPL");
659MODULE_AUTHOR("Jarkko Lavinen <jarkko.lavinen@nokia.com>");
660MODULE_DESCRIPTION("Glue layer for OneNAND flash on OMAP2 / OMAP3");