blob: e79ed0f60ade93a45ec21fd179b9b7ab01ee02ce [file] [log] [blame]
David Brownellff4569c2009-03-04 12:01:37 -08001/*
2 * davinci_nand.c - NAND Flash Driver for DaVinci family chips
3 *
4 * Copyright © 2006 Texas Instruments.
5 *
6 * Port to 2.6.23 Copyright © 2008 by:
7 * Sander Huijsen <Shuijsen@optelecom-nkf.com>
8 * Troy Kisky <troy.kisky@boundarydevices.com>
9 * Dirk Behme <Dirk.Behme@gmail.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26#include <linux/kernel.h>
David Brownellff4569c2009-03-04 12:01:37 -080027#include <linux/module.h>
28#include <linux/platform_device.h>
29#include <linux/err.h>
David Brownellff4569c2009-03-04 12:01:37 -080030#include <linux/io.h>
Boris Brezillond4092d72017-08-04 17:29:10 +020031#include <linux/mtd/rawnand.h>
David Brownellff4569c2009-03-04 12:01:37 -080032#include <linux/mtd/partitions.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Heiko Schochercdeadd72012-07-30 09:22:24 +020034#include <linux/of_device.h>
Sachin Kamatc4f8cde2013-03-14 15:37:01 +053035#include <linux/of.h>
David Brownellff4569c2009-03-04 12:01:37 -080036
Arnd Bergmannec2a0832012-08-24 15:11:34 +020037#include <linux/platform_data/mtd-davinci.h>
38#include <linux/platform_data/mtd-davinci-aemif.h>
David Brownellff4569c2009-03-04 12:01:37 -080039
David Brownellff4569c2009-03-04 12:01:37 -080040/*
41 * This is a device driver for the NAND flash controller found on the
42 * various DaVinci family chips. It handles up to four SoC chipselects,
43 * and some flavors of secondary chipselect (e.g. based on A12) as used
44 * with multichip packages.
45 *
David Brownell6a4123e2009-04-21 19:58:13 -070046 * The 1-bit ECC hardware is supported, as well as the newer 4-bit ECC
David Brownellff4569c2009-03-04 12:01:37 -080047 * available on chips like the DM355 and OMAP-L137 and needed with the
48 * more error-prone MLC NAND chips.
49 *
50 * This driver assumes EM_WAIT connects all the NAND devices' RDY/nBUSY
51 * outputs in a "wire-AND" configuration, with no per-chip signals.
52 */
53struct davinci_nand_info {
David Brownellff4569c2009-03-04 12:01:37 -080054 struct nand_chip chip;
55
56 struct device *dev;
David Brownellff4569c2009-03-04 12:01:37 -080057
David Brownell6a4123e2009-04-21 19:58:13 -070058 bool is_readmode;
59
David Brownellff4569c2009-03-04 12:01:37 -080060 void __iomem *base;
61 void __iomem *vaddr;
62
Boris Brezillonc5b76d82018-07-09 22:09:28 +020063 void __iomem *current_cs;
David Brownellff4569c2009-03-04 12:01:37 -080064
65 uint32_t mask_chipsel;
66 uint32_t mask_ale;
67 uint32_t mask_cle;
68
69 uint32_t core_chipsel;
Sekhar Noria88dbc52010-08-09 15:46:36 +053070
71 struct davinci_aemif_timing *timing;
David Brownellff4569c2009-03-04 12:01:37 -080072};
73
74static DEFINE_SPINLOCK(davinci_nand_lock);
David Brownell6a4123e2009-04-21 19:58:13 -070075static bool ecc4_busy;
David Brownellff4569c2009-03-04 12:01:37 -080076
Boris BREZILLONa5cfb4d2015-12-10 08:59:58 +010077static inline struct davinci_nand_info *to_davinci_nand(struct mtd_info *mtd)
78{
79 return container_of(mtd_to_nand(mtd), struct davinci_nand_info, chip);
80}
David Brownellff4569c2009-03-04 12:01:37 -080081
82static inline unsigned int davinci_nand_readl(struct davinci_nand_info *info,
83 int offset)
84{
85 return __raw_readl(info->base + offset);
86}
87
88static inline void davinci_nand_writel(struct davinci_nand_info *info,
89 int offset, unsigned long value)
90{
91 __raw_writel(value, info->base + offset);
92}
93
94/*----------------------------------------------------------------------*/
95
96/*
97 * Access to hardware control lines: ALE, CLE, secondary chipselect.
98 */
99
100static void nand_davinci_hwcontrol(struct mtd_info *mtd, int cmd,
101 unsigned int ctrl)
102{
103 struct davinci_nand_info *info = to_davinci_nand(mtd);
Boris Brezillonc5b76d82018-07-09 22:09:28 +0200104 void __iomem *addr = info->current_cs;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100105 struct nand_chip *nand = mtd_to_nand(mtd);
David Brownellff4569c2009-03-04 12:01:37 -0800106
107 /* Did the control lines change? */
108 if (ctrl & NAND_CTRL_CHANGE) {
109 if ((ctrl & NAND_CTRL_CLE) == NAND_CTRL_CLE)
Boris Brezillonc5b76d82018-07-09 22:09:28 +0200110 addr += info->mask_cle;
David Brownellff4569c2009-03-04 12:01:37 -0800111 else if ((ctrl & NAND_CTRL_ALE) == NAND_CTRL_ALE)
Boris Brezillonc5b76d82018-07-09 22:09:28 +0200112 addr += info->mask_ale;
David Brownellff4569c2009-03-04 12:01:37 -0800113
Boris Brezillonc5b76d82018-07-09 22:09:28 +0200114 nand->IO_ADDR_W = addr;
David Brownellff4569c2009-03-04 12:01:37 -0800115 }
116
117 if (cmd != NAND_CMD_NONE)
118 iowrite8(cmd, nand->IO_ADDR_W);
119}
120
121static void nand_davinci_select_chip(struct mtd_info *mtd, int chip)
122{
123 struct davinci_nand_info *info = to_davinci_nand(mtd);
Boris Brezillonc5b76d82018-07-09 22:09:28 +0200124
125 info->current_cs = info->vaddr;
David Brownellff4569c2009-03-04 12:01:37 -0800126
127 /* maybe kick in a second chipselect */
128 if (chip > 0)
Boris Brezillonc5b76d82018-07-09 22:09:28 +0200129 info->current_cs += info->mask_chipsel;
David Brownellff4569c2009-03-04 12:01:37 -0800130
Boris Brezillonc5b76d82018-07-09 22:09:28 +0200131 info->chip.IO_ADDR_W = info->current_cs;
David Brownellff4569c2009-03-04 12:01:37 -0800132 info->chip.IO_ADDR_R = info->chip.IO_ADDR_W;
133}
134
135/*----------------------------------------------------------------------*/
136
137/*
138 * 1-bit hardware ECC ... context maintained for each core chipselect
139 */
140
141static inline uint32_t nand_davinci_readecc_1bit(struct mtd_info *mtd)
142{
143 struct davinci_nand_info *info = to_davinci_nand(mtd);
144
145 return davinci_nand_readl(info, NANDF1ECC_OFFSET
146 + 4 * info->core_chipsel);
147}
148
149static void nand_davinci_hwctl_1bit(struct mtd_info *mtd, int mode)
150{
151 struct davinci_nand_info *info;
152 uint32_t nandcfr;
153 unsigned long flags;
154
155 info = to_davinci_nand(mtd);
156
157 /* Reset ECC hardware */
158 nand_davinci_readecc_1bit(mtd);
159
160 spin_lock_irqsave(&davinci_nand_lock, flags);
161
162 /* Restart ECC hardware */
163 nandcfr = davinci_nand_readl(info, NANDFCR_OFFSET);
164 nandcfr |= BIT(8 + info->core_chipsel);
165 davinci_nand_writel(info, NANDFCR_OFFSET, nandcfr);
166
167 spin_unlock_irqrestore(&davinci_nand_lock, flags);
168}
169
170/*
171 * Read hardware ECC value and pack into three bytes
172 */
173static int nand_davinci_calculate_1bit(struct mtd_info *mtd,
174 const u_char *dat, u_char *ecc_code)
175{
176 unsigned int ecc_val = nand_davinci_readecc_1bit(mtd);
177 unsigned int ecc24 = (ecc_val & 0x0fff) | ((ecc_val & 0x0fff0000) >> 4);
178
179 /* invert so that erased block ecc is correct */
180 ecc24 = ~ecc24;
181 ecc_code[0] = (u_char)(ecc24);
182 ecc_code[1] = (u_char)(ecc24 >> 8);
183 ecc_code[2] = (u_char)(ecc24 >> 16);
184
185 return 0;
186}
187
188static int nand_davinci_correct_1bit(struct mtd_info *mtd, u_char *dat,
189 u_char *read_ecc, u_char *calc_ecc)
190{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100191 struct nand_chip *chip = mtd_to_nand(mtd);
David Brownellff4569c2009-03-04 12:01:37 -0800192 uint32_t eccNand = read_ecc[0] | (read_ecc[1] << 8) |
193 (read_ecc[2] << 16);
194 uint32_t eccCalc = calc_ecc[0] | (calc_ecc[1] << 8) |
195 (calc_ecc[2] << 16);
196 uint32_t diff = eccCalc ^ eccNand;
197
198 if (diff) {
199 if ((((diff >> 12) ^ diff) & 0xfff) == 0xfff) {
200 /* Correctable error */
201 if ((diff >> (12 + 3)) < chip->ecc.size) {
202 dat[diff >> (12 + 3)] ^= BIT((diff >> 12) & 7);
203 return 1;
204 } else {
Boris BREZILLON6e941192015-12-30 20:32:03 +0100205 return -EBADMSG;
David Brownellff4569c2009-03-04 12:01:37 -0800206 }
207 } else if (!(diff & (diff - 1))) {
208 /* Single bit ECC error in the ECC itself,
209 * nothing to fix */
210 return 1;
211 } else {
212 /* Uncorrectable error */
Boris BREZILLON6e941192015-12-30 20:32:03 +0100213 return -EBADMSG;
David Brownellff4569c2009-03-04 12:01:37 -0800214 }
215
216 }
217 return 0;
218}
219
220/*----------------------------------------------------------------------*/
221
222/*
David Brownell6a4123e2009-04-21 19:58:13 -0700223 * 4-bit hardware ECC ... context maintained over entire AEMIF
224 *
225 * This is a syndrome engine, but we avoid NAND_ECC_HW_SYNDROME
226 * since that forces use of a problematic "infix OOB" layout.
227 * Among other things, it trashes manufacturer bad block markers.
228 * Also, and specific to this hardware, it ECC-protects the "prepad"
229 * in the OOB ... while having ECC protection for parts of OOB would
230 * seem useful, the current MTD stack sometimes wants to update the
231 * OOB without recomputing ECC.
232 */
233
234static void nand_davinci_hwctl_4bit(struct mtd_info *mtd, int mode)
235{
236 struct davinci_nand_info *info = to_davinci_nand(mtd);
237 unsigned long flags;
238 u32 val;
239
Karl Beldanf6d7c1b2016-08-29 07:45:49 +0000240 /* Reset ECC hardware */
241 davinci_nand_readl(info, NAND_4BIT_ECC1_OFFSET);
242
David Brownell6a4123e2009-04-21 19:58:13 -0700243 spin_lock_irqsave(&davinci_nand_lock, flags);
244
245 /* Start 4-bit ECC calculation for read/write */
246 val = davinci_nand_readl(info, NANDFCR_OFFSET);
247 val &= ~(0x03 << 4);
248 val |= (info->core_chipsel << 4) | BIT(12);
249 davinci_nand_writel(info, NANDFCR_OFFSET, val);
250
251 info->is_readmode = (mode == NAND_ECC_READ);
252
253 spin_unlock_irqrestore(&davinci_nand_lock, flags);
254}
255
256/* Read raw ECC code after writing to NAND. */
257static void
258nand_davinci_readecc_4bit(struct davinci_nand_info *info, u32 code[4])
259{
260 const u32 mask = 0x03ff03ff;
261
262 code[0] = davinci_nand_readl(info, NAND_4BIT_ECC1_OFFSET) & mask;
263 code[1] = davinci_nand_readl(info, NAND_4BIT_ECC2_OFFSET) & mask;
264 code[2] = davinci_nand_readl(info, NAND_4BIT_ECC3_OFFSET) & mask;
265 code[3] = davinci_nand_readl(info, NAND_4BIT_ECC4_OFFSET) & mask;
266}
267
268/* Terminate read ECC; or return ECC (as bytes) of data written to NAND. */
269static int nand_davinci_calculate_4bit(struct mtd_info *mtd,
270 const u_char *dat, u_char *ecc_code)
271{
272 struct davinci_nand_info *info = to_davinci_nand(mtd);
273 u32 raw_ecc[4], *p;
274 unsigned i;
275
276 /* After a read, terminate ECC calculation by a dummy read
277 * of some 4-bit ECC register. ECC covers everything that
278 * was read; correct() just uses the hardware state, so
279 * ecc_code is not needed.
280 */
281 if (info->is_readmode) {
282 davinci_nand_readl(info, NAND_4BIT_ECC1_OFFSET);
283 return 0;
284 }
285
286 /* Pack eight raw 10-bit ecc values into ten bytes, making
287 * two passes which each convert four values (in upper and
288 * lower halves of two 32-bit words) into five bytes. The
289 * ROM boot loader uses this same packing scheme.
290 */
291 nand_davinci_readecc_4bit(info, raw_ecc);
292 for (i = 0, p = raw_ecc; i < 2; i++, p += 2) {
293 *ecc_code++ = p[0] & 0xff;
294 *ecc_code++ = ((p[0] >> 8) & 0x03) | ((p[0] >> 14) & 0xfc);
295 *ecc_code++ = ((p[0] >> 22) & 0x0f) | ((p[1] << 4) & 0xf0);
296 *ecc_code++ = ((p[1] >> 4) & 0x3f) | ((p[1] >> 10) & 0xc0);
297 *ecc_code++ = (p[1] >> 18) & 0xff;
298 }
299
300 return 0;
301}
302
303/* Correct up to 4 bits in data we just read, using state left in the
304 * hardware plus the ecc_code computed when it was first written.
305 */
306static int nand_davinci_correct_4bit(struct mtd_info *mtd,
307 u_char *data, u_char *ecc_code, u_char *null)
308{
309 int i;
310 struct davinci_nand_info *info = to_davinci_nand(mtd);
311 unsigned short ecc10[8];
312 unsigned short *ecc16;
313 u32 syndrome[4];
Sudhakar Rajashekhara1c3275b2010-07-20 15:24:01 -0700314 u32 ecc_state;
David Brownell6a4123e2009-04-21 19:58:13 -0700315 unsigned num_errors, corrected;
Wolfram Sang2bdb0532010-09-03 12:35:37 +0200316 unsigned long timeo;
David Brownell6a4123e2009-04-21 19:58:13 -0700317
David Brownell6a4123e2009-04-21 19:58:13 -0700318 /* Unpack ten bytes into eight 10 bit values. We know we're
319 * little-endian, and use type punning for less shifting/masking.
320 */
Boris Brezilloncc53d5c2018-07-09 22:09:29 +0200321 if (WARN_ON(0x01 & (uintptr_t)ecc_code))
David Brownell6a4123e2009-04-21 19:58:13 -0700322 return -EINVAL;
323 ecc16 = (unsigned short *)ecc_code;
324
325 ecc10[0] = (ecc16[0] >> 0) & 0x3ff;
326 ecc10[1] = ((ecc16[0] >> 10) & 0x3f) | ((ecc16[1] << 6) & 0x3c0);
327 ecc10[2] = (ecc16[1] >> 4) & 0x3ff;
328 ecc10[3] = ((ecc16[1] >> 14) & 0x3) | ((ecc16[2] << 2) & 0x3fc);
329 ecc10[4] = (ecc16[2] >> 8) | ((ecc16[3] << 8) & 0x300);
330 ecc10[5] = (ecc16[3] >> 2) & 0x3ff;
331 ecc10[6] = ((ecc16[3] >> 12) & 0xf) | ((ecc16[4] << 4) & 0x3f0);
332 ecc10[7] = (ecc16[4] >> 6) & 0x3ff;
333
334 /* Tell ECC controller about the expected ECC codes. */
335 for (i = 7; i >= 0; i--)
336 davinci_nand_writel(info, NAND_4BIT_ECC_LOAD_OFFSET, ecc10[i]);
337
338 /* Allow time for syndrome calculation ... then read it.
339 * A syndrome of all zeroes 0 means no detected errors.
340 */
341 davinci_nand_readl(info, NANDFSR_OFFSET);
342 nand_davinci_readecc_4bit(info, syndrome);
343 if (!(syndrome[0] | syndrome[1] | syndrome[2] | syndrome[3]))
344 return 0;
345
Sneha Narnakajef12a9472009-09-18 12:51:48 -0700346 /*
347 * Clear any previous address calculation by doing a dummy read of an
348 * error address register.
349 */
350 davinci_nand_readl(info, NAND_ERR_ADD1_OFFSET);
351
David Brownell6a4123e2009-04-21 19:58:13 -0700352 /* Start address calculation, and wait for it to complete.
353 * We _could_ start reading more data while this is working,
354 * to speed up the overall page read.
355 */
356 davinci_nand_writel(info, NANDFCR_OFFSET,
357 davinci_nand_readl(info, NANDFCR_OFFSET) | BIT(13));
Sudhakar Rajashekhara1c3275b2010-07-20 15:24:01 -0700358
359 /*
360 * ECC_STATE field reads 0x3 (Error correction complete) immediately
361 * after setting the 4BITECC_ADD_CALC_START bit. So if you immediately
362 * begin trying to poll for the state, you may fall right out of your
363 * loop without any of the correction calculations having taken place.
Wolfram Sangeea116e2010-08-25 14:18:20 +0200364 * The recommendation from the hardware team is to initially delay as
365 * long as ECC_STATE reads less than 4. After that, ECC HW has entered
366 * correction state.
Sudhakar Rajashekhara1c3275b2010-07-20 15:24:01 -0700367 */
Wolfram Sang2bdb0532010-09-03 12:35:37 +0200368 timeo = jiffies + usecs_to_jiffies(100);
Sudhakar Rajashekhara1c3275b2010-07-20 15:24:01 -0700369 do {
370 ecc_state = (davinci_nand_readl(info,
371 NANDFSR_OFFSET) >> 8) & 0x0f;
372 cpu_relax();
373 } while ((ecc_state < 4) && time_before(jiffies, timeo));
374
David Brownell6a4123e2009-04-21 19:58:13 -0700375 for (;;) {
376 u32 fsr = davinci_nand_readl(info, NANDFSR_OFFSET);
377
378 switch ((fsr >> 8) & 0x0f) {
379 case 0: /* no error, should not happen */
Sneha Narnakajef12a9472009-09-18 12:51:48 -0700380 davinci_nand_readl(info, NAND_ERR_ERRVAL1_OFFSET);
David Brownell6a4123e2009-04-21 19:58:13 -0700381 return 0;
382 case 1: /* five or more errors detected */
Sneha Narnakajef12a9472009-09-18 12:51:48 -0700383 davinci_nand_readl(info, NAND_ERR_ERRVAL1_OFFSET);
Boris BREZILLON6e941192015-12-30 20:32:03 +0100384 return -EBADMSG;
David Brownell6a4123e2009-04-21 19:58:13 -0700385 case 2: /* error addresses computed */
386 case 3:
387 num_errors = 1 + ((fsr >> 16) & 0x03);
388 goto correct;
389 default: /* still working on it */
390 cpu_relax();
391 continue;
392 }
393 }
394
395correct:
396 /* correct each error */
397 for (i = 0, corrected = 0; i < num_errors; i++) {
398 int error_address, error_value;
399
400 if (i > 1) {
401 error_address = davinci_nand_readl(info,
402 NAND_ERR_ADD2_OFFSET);
403 error_value = davinci_nand_readl(info,
404 NAND_ERR_ERRVAL2_OFFSET);
405 } else {
406 error_address = davinci_nand_readl(info,
407 NAND_ERR_ADD1_OFFSET);
408 error_value = davinci_nand_readl(info,
409 NAND_ERR_ERRVAL1_OFFSET);
410 }
411
412 if (i & 1) {
413 error_address >>= 16;
414 error_value >>= 16;
415 }
416 error_address &= 0x3ff;
417 error_address = (512 + 7) - error_address;
418
419 if (error_address < 512) {
420 data[error_address] ^= error_value;
421 corrected++;
422 }
423 }
424
425 return corrected;
426}
427
428/*----------------------------------------------------------------------*/
429
430/*
David Brownellff4569c2009-03-04 12:01:37 -0800431 * NOTE: NAND boot requires ALE == EM_A[1], CLE == EM_A[2], so that's
432 * how these chips are normally wired. This translates to both 8 and 16
433 * bit busses using ALE == BIT(3) in byte addresses, and CLE == BIT(4).
434 *
435 * For now we assume that configuration, or any other one which ignores
436 * the two LSBs for NAND access ... so we can issue 32-bit reads/writes
437 * and have that transparently morphed into multiple NAND operations.
438 */
439static void nand_davinci_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
440{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100441 struct nand_chip *chip = mtd_to_nand(mtd);
David Brownellff4569c2009-03-04 12:01:37 -0800442
Boris Brezilloncc53d5c2018-07-09 22:09:29 +0200443 if ((0x03 & ((uintptr_t)buf)) == 0 && (0x03 & len) == 0)
David Brownellff4569c2009-03-04 12:01:37 -0800444 ioread32_rep(chip->IO_ADDR_R, buf, len >> 2);
Boris Brezilloncc53d5c2018-07-09 22:09:29 +0200445 else if ((0x01 & ((uintptr_t)buf)) == 0 && (0x01 & len) == 0)
David Brownellff4569c2009-03-04 12:01:37 -0800446 ioread16_rep(chip->IO_ADDR_R, buf, len >> 1);
447 else
448 ioread8_rep(chip->IO_ADDR_R, buf, len);
449}
450
451static void nand_davinci_write_buf(struct mtd_info *mtd,
452 const uint8_t *buf, int len)
453{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100454 struct nand_chip *chip = mtd_to_nand(mtd);
David Brownellff4569c2009-03-04 12:01:37 -0800455
Boris Brezilloncc53d5c2018-07-09 22:09:29 +0200456 if ((0x03 & ((uintptr_t)buf)) == 0 && (0x03 & len) == 0)
David Brownellff4569c2009-03-04 12:01:37 -0800457 iowrite32_rep(chip->IO_ADDR_R, buf, len >> 2);
Boris Brezilloncc53d5c2018-07-09 22:09:29 +0200458 else if ((0x01 & ((uintptr_t)buf)) == 0 && (0x01 & len) == 0)
David Brownellff4569c2009-03-04 12:01:37 -0800459 iowrite16_rep(chip->IO_ADDR_R, buf, len >> 1);
460 else
461 iowrite8_rep(chip->IO_ADDR_R, buf, len);
462}
463
464/*
465 * Check hardware register for wait status. Returns 1 if device is ready,
466 * 0 if it is still busy.
467 */
468static int nand_davinci_dev_ready(struct mtd_info *mtd)
469{
470 struct davinci_nand_info *info = to_davinci_nand(mtd);
471
472 return davinci_nand_readl(info, NANDFSR_OFFSET) & BIT(0);
473}
474
David Brownellff4569c2009-03-04 12:01:37 -0800475/*----------------------------------------------------------------------*/
476
David Brownell6a4123e2009-04-21 19:58:13 -0700477/* An ECC layout for using 4-bit ECC with small-page flash, storing
478 * ten ECC bytes plus the manufacturer's bad block marker byte, and
479 * and not overlapping the default BBT markers.
480 */
Boris Brezillone4aacaa2016-02-03 19:59:58 +0100481static int hwecc4_ooblayout_small_ecc(struct mtd_info *mtd, int section,
482 struct mtd_oob_region *oobregion)
483{
484 if (section > 2)
485 return -ERANGE;
David Brownell6a4123e2009-04-21 19:58:13 -0700486
Boris Brezillone4aacaa2016-02-03 19:59:58 +0100487 if (!section) {
488 oobregion->offset = 0;
489 oobregion->length = 5;
490 } else if (section == 1) {
491 oobregion->offset = 6;
492 oobregion->length = 2;
493 } else {
494 oobregion->offset = 13;
495 oobregion->length = 3;
496 }
David Brownell6a4123e2009-04-21 19:58:13 -0700497
Boris Brezillone4aacaa2016-02-03 19:59:58 +0100498 return 0;
499}
500
501static int hwecc4_ooblayout_small_free(struct mtd_info *mtd, int section,
502 struct mtd_oob_region *oobregion)
503{
504 if (section > 1)
505 return -ERANGE;
506
507 if (!section) {
508 oobregion->offset = 8;
509 oobregion->length = 5;
510 } else {
511 oobregion->offset = 16;
512 oobregion->length = mtd->oobsize - 16;
513 }
514
515 return 0;
516}
517
518static const struct mtd_ooblayout_ops hwecc4_small_ooblayout_ops = {
519 .ecc = hwecc4_ooblayout_small_ecc,
520 .free = hwecc4_ooblayout_small_free,
Sandeep Paulraja11244c2014-08-19 15:31:54 +0300521};
522
Heiko Schochercdeadd72012-07-30 09:22:24 +0200523#if defined(CONFIG_OF)
524static const struct of_device_id davinci_nand_of_match[] = {
525 {.compatible = "ti,davinci-nand", },
Murali Karicheri28c015a2014-03-20 22:08:32 +0200526 {.compatible = "ti,keystone-nand", },
Heiko Schochercdeadd72012-07-30 09:22:24 +0200527 {},
Sergei Shtylyov13daa222013-01-03 21:27:34 +0300528};
Heiko Schochercdeadd72012-07-30 09:22:24 +0200529MODULE_DEVICE_TABLE(of, davinci_nand_of_match);
530
531static struct davinci_nand_pdata
532 *nand_davinci_get_pdata(struct platform_device *pdev)
533{
Jingoo Han453810b2013-07-30 17:18:33 +0900534 if (!dev_get_platdata(&pdev->dev) && pdev->dev.of_node) {
Heiko Schochercdeadd72012-07-30 09:22:24 +0200535 struct davinci_nand_pdata *pdata;
536 const char *mode;
537 u32 prop;
Heiko Schochercdeadd72012-07-30 09:22:24 +0200538
539 pdata = devm_kzalloc(&pdev->dev,
540 sizeof(struct davinci_nand_pdata),
541 GFP_KERNEL);
542 pdev->dev.platform_data = pdata;
543 if (!pdata)
Ivan Khoronzhukf735a4d2013-12-17 15:36:05 +0200544 return ERR_PTR(-ENOMEM);
Heiko Schochercdeadd72012-07-30 09:22:24 +0200545 if (!of_property_read_u32(pdev->dev.of_node,
546 "ti,davinci-chipselect", &prop))
Bartosz Golaszewskifd065802018-04-30 10:24:52 +0200547 pdata->core_chipsel = prop;
Ivan Khoronzhuk05103822013-12-17 15:36:44 +0200548 else
549 return ERR_PTR(-EINVAL);
550
Heiko Schochercdeadd72012-07-30 09:22:24 +0200551 if (!of_property_read_u32(pdev->dev.of_node,
552 "ti,davinci-mask-ale", &prop))
553 pdata->mask_ale = prop;
554 if (!of_property_read_u32(pdev->dev.of_node,
555 "ti,davinci-mask-cle", &prop))
556 pdata->mask_cle = prop;
557 if (!of_property_read_u32(pdev->dev.of_node,
558 "ti,davinci-mask-chipsel", &prop))
559 pdata->mask_chipsel = prop;
560 if (!of_property_read_string(pdev->dev.of_node,
561 "ti,davinci-ecc-mode", &mode)) {
562 if (!strncmp("none", mode, 4))
563 pdata->ecc_mode = NAND_ECC_NONE;
564 if (!strncmp("soft", mode, 4))
565 pdata->ecc_mode = NAND_ECC_SOFT;
566 if (!strncmp("hw", mode, 2))
567 pdata->ecc_mode = NAND_ECC_HW;
568 }
569 if (!of_property_read_u32(pdev->dev.of_node,
570 "ti,davinci-ecc-bits", &prop))
571 pdata->ecc_bits = prop;
Ivan Khoronzhuk75be1ea2013-12-17 15:37:56 +0200572
Boris Brezillon363b5db2016-04-01 14:54:25 +0200573 if (!of_property_read_u32(pdev->dev.of_node,
574 "ti,davinci-nand-buswidth", &prop) && prop == 16)
575 pdata->options |= NAND_BUSWIDTH_16;
576
Ivan Khoronzhuk75be1ea2013-12-17 15:37:56 +0200577 if (of_property_read_bool(pdev->dev.of_node,
Ivan Khoronzhuk75be1ea2013-12-17 15:37:56 +0200578 "ti,davinci-nand-use-bbt"))
Heiko Schochercdeadd72012-07-30 09:22:24 +0200579 pdata->bbt_options = NAND_BBT_USE_FLASH;
Murali Karicheri28c015a2014-03-20 22:08:32 +0200580
Sekhar Nori65a2c1c2017-03-30 20:09:30 +0530581 /*
582 * Since kernel v4.8, this driver has been fixed to enable
583 * use of 4-bit hardware ECC with subpages and verified on
584 * TI's keystone EVMs (K2L, K2HK and K2E).
585 * However, in the interest of not breaking systems using
586 * existing UBI partitions, sub-page writes are not being
587 * (re)enabled. If you want to use subpage writes on Keystone
588 * platforms (i.e. do not have any existing UBI partitions),
589 * then use "ti,davinci-nand" as the compatible in your
590 * device-tree file.
591 */
Murali Karicheri28c015a2014-03-20 22:08:32 +0200592 if (of_device_is_compatible(pdev->dev.of_node,
593 "ti,keystone-nand")) {
594 pdata->options |= NAND_NO_SUBPAGE_WRITE;
595 }
Heiko Schochercdeadd72012-07-30 09:22:24 +0200596 }
597
Jingoo Han453810b2013-07-30 17:18:33 +0900598 return dev_get_platdata(&pdev->dev);
Heiko Schochercdeadd72012-07-30 09:22:24 +0200599}
600#else
Heiko Schochercdeadd72012-07-30 09:22:24 +0200601static struct davinci_nand_pdata
602 *nand_davinci_get_pdata(struct platform_device *pdev)
603{
Jingoo Han453810b2013-07-30 17:18:33 +0900604 return dev_get_platdata(&pdev->dev);
Heiko Schochercdeadd72012-07-30 09:22:24 +0200605}
606#endif
607
Ivan Khoronzhukeaaa4a92013-12-17 15:33:50 +0200608static int nand_davinci_probe(struct platform_device *pdev)
David Brownellff4569c2009-03-04 12:01:37 -0800609{
Heiko Schochercdeadd72012-07-30 09:22:24 +0200610 struct davinci_nand_pdata *pdata;
David Brownellff4569c2009-03-04 12:01:37 -0800611 struct davinci_nand_info *info;
612 struct resource *res1;
613 struct resource *res2;
614 void __iomem *vaddr;
615 void __iomem *base;
616 int ret;
617 uint32_t val;
Boris BREZILLONa5cfb4d2015-12-10 08:59:58 +0100618 struct mtd_info *mtd;
David Brownellff4569c2009-03-04 12:01:37 -0800619
Heiko Schochercdeadd72012-07-30 09:22:24 +0200620 pdata = nand_davinci_get_pdata(pdev);
Ivan Khoronzhukf735a4d2013-12-17 15:36:05 +0200621 if (IS_ERR(pdata))
622 return PTR_ERR(pdata);
623
David Brownell533a0142009-04-21 19:51:31 -0700624 /* insist on board-specific configuration */
625 if (!pdata)
626 return -ENODEV;
627
David Brownellff4569c2009-03-04 12:01:37 -0800628 /* which external chipselect will we be managing? */
Bartosz Golaszewskifd065802018-04-30 10:24:52 +0200629 if (pdata->core_chipsel < 0 || pdata->core_chipsel > 3)
David Brownellff4569c2009-03-04 12:01:37 -0800630 return -ENODEV;
631
Mrugesh Katepallewaref4e0c22013-02-07 16:03:15 +0530632 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
Jingoo Han00669232013-12-26 12:13:59 +0900633 if (!info)
Ivan Khoronzhuk30a39702013-12-17 15:37:00 +0200634 return -ENOMEM;
David Brownellff4569c2009-03-04 12:01:37 -0800635
636 platform_set_drvdata(pdev, info);
637
638 res1 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
639 res2 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
640 if (!res1 || !res2) {
641 dev_err(&pdev->dev, "resource missing\n");
Ivan Khoronzhuk30a39702013-12-17 15:37:00 +0200642 return -EINVAL;
David Brownellff4569c2009-03-04 12:01:37 -0800643 }
644
Laurent Navet59bff7f2013-05-02 15:56:10 +0200645 vaddr = devm_ioremap_resource(&pdev->dev, res1);
Ivan Khoronzhuk30a39702013-12-17 15:37:00 +0200646 if (IS_ERR(vaddr))
647 return PTR_ERR(vaddr);
648
Ivan Khoronzhuk0966a412013-12-17 15:38:31 +0200649 /*
650 * This registers range is used to setup NAND settings. In case with
651 * TI AEMIF driver, the same memory address range is requested already
652 * by AEMIF, so we cannot request it twice, just ioremap.
653 * The AEMIF and NAND drivers not use the same registers in this range.
654 */
655 base = devm_ioremap(&pdev->dev, res2->start, resource_size(res2));
656 if (!base) {
657 dev_err(&pdev->dev, "ioremap failed for resource %pR\n", res2);
658 return -EADDRNOTAVAIL;
659 }
David Brownellff4569c2009-03-04 12:01:37 -0800660
661 info->dev = &pdev->dev;
662 info->base = base;
663 info->vaddr = vaddr;
664
Boris BREZILLONa5cfb4d2015-12-10 08:59:58 +0100665 mtd = nand_to_mtd(&info->chip);
Boris BREZILLONa5cfb4d2015-12-10 08:59:58 +0100666 mtd->dev.parent = &pdev->dev;
Brian Norrisa61ae812015-10-30 20:33:25 -0700667 nand_set_flash_node(&info->chip, pdev->dev.of_node);
David Brownell87f39f02009-03-26 00:42:50 -0700668
David Brownellff4569c2009-03-04 12:01:37 -0800669 info->chip.IO_ADDR_R = vaddr;
670 info->chip.IO_ADDR_W = vaddr;
671 info->chip.chip_delay = 0;
672 info->chip.select_chip = nand_davinci_select_chip;
673
Brian Norrisbb9ebd4e2011-05-31 16:31:23 -0700674 /* options such as NAND_BBT_USE_FLASH */
Brian Norrisa40f7342011-05-31 16:31:22 -0700675 info->chip.bbt_options = pdata->bbt_options;
676 /* options such as 16-bit widths */
David Brownell533a0142009-04-21 19:51:31 -0700677 info->chip.options = pdata->options;
Mark A. Greerf611a792009-10-12 16:16:37 -0700678 info->chip.bbt_td = pdata->bbt_td;
679 info->chip.bbt_md = pdata->bbt_md;
Sekhar Noria88dbc52010-08-09 15:46:36 +0530680 info->timing = pdata->timing;
David Brownellff4569c2009-03-04 12:01:37 -0800681
Boris Brezillonc5b76d82018-07-09 22:09:28 +0200682 info->current_cs = info->vaddr;
Bartosz Golaszewskifd065802018-04-30 10:24:52 +0200683 info->core_chipsel = pdata->core_chipsel;
David Brownellff4569c2009-03-04 12:01:37 -0800684 info->mask_chipsel = pdata->mask_chipsel;
685
686 /* use nandboot-capable ALE/CLE masks by default */
Hemant Pedanekar5cd0be82009-10-01 19:55:06 +0530687 info->mask_ale = pdata->mask_ale ? : MASK_ALE;
David Brownell533a0142009-04-21 19:51:31 -0700688 info->mask_cle = pdata->mask_cle ? : MASK_CLE;
David Brownellff4569c2009-03-04 12:01:37 -0800689
690 /* Set address of hardware control function */
691 info->chip.cmd_ctrl = nand_davinci_hwcontrol;
692 info->chip.dev_ready = nand_davinci_dev_ready;
693
694 /* Speed up buffer I/O */
695 info->chip.read_buf = nand_davinci_read_buf;
696 info->chip.write_buf = nand_davinci_write_buf;
697
David Brownell533a0142009-04-21 19:51:31 -0700698 /* Use board-specific ECC config */
Boris Brezillon363b5db2016-04-01 14:54:25 +0200699 info->chip.ecc.mode = pdata->ecc_mode;
David Brownellff4569c2009-03-04 12:01:37 -0800700
Boris Brezillon363b5db2016-04-01 14:54:25 +0200701 spin_lock_irq(&davinci_nand_lock);
702
703 /* put CSxNAND into NAND mode */
704 val = davinci_nand_readl(info, NANDFCR_OFFSET);
705 val |= BIT(info->core_chipsel);
706 davinci_nand_writel(info, NANDFCR_OFFSET, val);
707
708 spin_unlock_irq(&davinci_nand_lock);
709
710 /* Scan to find existence of the device(s) */
711 ret = nand_scan_ident(mtd, pdata->mask_chipsel ? 2 : 1, NULL);
712 if (ret < 0) {
713 dev_dbg(&pdev->dev, "no NAND chip(s) found\n");
Sekhar Noria8e39232018-03-30 20:00:51 +0530714 return ret;
Boris Brezillon363b5db2016-04-01 14:54:25 +0200715 }
716
717 switch (info->chip.ecc.mode) {
David Brownellff4569c2009-03-04 12:01:37 -0800718 case NAND_ECC_NONE:
Rafał Miłecki867f9872016-04-17 22:52:58 +0200719 pdata->ecc_bits = 0;
720 break;
David Brownellff4569c2009-03-04 12:01:37 -0800721 case NAND_ECC_SOFT:
David Brownell6a4123e2009-04-21 19:58:13 -0700722 pdata->ecc_bits = 0;
Rafał Miłecki867f9872016-04-17 22:52:58 +0200723 /*
724 * This driver expects Hamming based ECC when ecc_mode is set
725 * to NAND_ECC_SOFT. Force ecc.algo to NAND_ECC_HAMMING to
726 * avoid adding an extra ->ecc_algo field to
727 * davinci_nand_pdata.
728 */
729 info->chip.ecc.algo = NAND_ECC_HAMMING;
David Brownellff4569c2009-03-04 12:01:37 -0800730 break;
731 case NAND_ECC_HW:
David Brownell6a4123e2009-04-21 19:58:13 -0700732 if (pdata->ecc_bits == 4) {
733 /* No sanity checks: CPUs must support this,
734 * and the chips may not use NAND_BUSWIDTH_16.
735 */
David Brownellff4569c2009-03-04 12:01:37 -0800736
David Brownell6a4123e2009-04-21 19:58:13 -0700737 /* No sharing 4-bit hardware between chipselects yet */
738 spin_lock_irq(&davinci_nand_lock);
739 if (ecc4_busy)
740 ret = -EBUSY;
741 else
742 ecc4_busy = true;
743 spin_unlock_irq(&davinci_nand_lock);
744
745 if (ret == -EBUSY)
Ivan Khoronzhuk30a39702013-12-17 15:37:00 +0200746 return ret;
David Brownell6a4123e2009-04-21 19:58:13 -0700747
748 info->chip.ecc.calculate = nand_davinci_calculate_4bit;
749 info->chip.ecc.correct = nand_davinci_correct_4bit;
750 info->chip.ecc.hwctl = nand_davinci_hwctl_4bit;
751 info->chip.ecc.bytes = 10;
Boris BREZILLONbc29c952015-12-30 20:32:05 +0100752 info->chip.ecc.options = NAND_ECC_GENERIC_ERASED_CHECK;
Alexander Couzens19d8ccc2017-05-02 11:47:36 +0200753 info->chip.ecc.algo = NAND_ECC_BCH;
David Brownell6a4123e2009-04-21 19:58:13 -0700754 } else {
Alexander Couzens19d8ccc2017-05-02 11:47:36 +0200755 /* 1bit ecc hamming */
David Brownell6a4123e2009-04-21 19:58:13 -0700756 info->chip.ecc.calculate = nand_davinci_calculate_1bit;
757 info->chip.ecc.correct = nand_davinci_correct_1bit;
758 info->chip.ecc.hwctl = nand_davinci_hwctl_1bit;
759 info->chip.ecc.bytes = 3;
Alexander Couzens19d8ccc2017-05-02 11:47:36 +0200760 info->chip.ecc.algo = NAND_ECC_HAMMING;
David Brownell6a4123e2009-04-21 19:58:13 -0700761 }
762 info->chip.ecc.size = 512;
Mike Dunn6a918ba2012-03-11 14:21:11 -0700763 info->chip.ecc.strength = pdata->ecc_bits;
David Brownell6a4123e2009-04-21 19:58:13 -0700764 break;
David Brownellff4569c2009-03-04 12:01:37 -0800765 default:
Ivan Khoronzhuk30a39702013-12-17 15:37:00 +0200766 return -EINVAL;
David Brownellff4569c2009-03-04 12:01:37 -0800767 }
David Brownellff4569c2009-03-04 12:01:37 -0800768
David Brownell6a4123e2009-04-21 19:58:13 -0700769 /* Update ECC layout if needed ... for 1-bit HW ECC, the default
770 * is OK, but it allocates 6 bytes when only 3 are needed (for
771 * each 512 bytes). For the 4-bit HW ECC, that default is not
772 * usable: 10 bytes are needed, not 6.
773 */
774 if (pdata->ecc_bits == 4) {
Boris BREZILLONa5cfb4d2015-12-10 08:59:58 +0100775 int chunks = mtd->writesize / 512;
David Brownell6a4123e2009-04-21 19:58:13 -0700776
Boris BREZILLONa5cfb4d2015-12-10 08:59:58 +0100777 if (!chunks || mtd->oobsize < 16) {
David Brownell6a4123e2009-04-21 19:58:13 -0700778 dev_dbg(&pdev->dev, "too small\n");
779 ret = -EINVAL;
Ivan Khoronzhuk30a39702013-12-17 15:37:00 +0200780 goto err;
David Brownell6a4123e2009-04-21 19:58:13 -0700781 }
782
783 /* For small page chips, preserve the manufacturer's
784 * badblock marking data ... and make sure a flash BBT
785 * table marker fits in the free bytes.
786 */
787 if (chunks == 1) {
Boris Brezillone4aacaa2016-02-03 19:59:58 +0100788 mtd_set_ooblayout(mtd, &hwecc4_small_ooblayout_ops);
789 } else if (chunks == 4 || chunks == 8) {
790 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
Sneha Narnakajef12a9472009-09-18 12:51:48 -0700791 info->chip.ecc.mode = NAND_ECC_HW_OOB_FIRST;
Boris Brezillone4aacaa2016-02-03 19:59:58 +0100792 } else {
793 ret = -EIO;
794 goto err;
Sneha Narnakajef12a9472009-09-18 12:51:48 -0700795 }
David Brownell6a4123e2009-04-21 19:58:13 -0700796 }
797
Boris BREZILLONa5cfb4d2015-12-10 08:59:58 +0100798 ret = nand_scan_tail(mtd);
David Brownell6a4123e2009-04-21 19:58:13 -0700799 if (ret < 0)
Ivan Khoronzhuk30a39702013-12-17 15:37:00 +0200800 goto err;
David Brownell6a4123e2009-04-21 19:58:13 -0700801
Murali Karicheri192afdb2012-11-02 10:22:41 -0400802 if (pdata->parts)
Boris BREZILLONa5cfb4d2015-12-10 08:59:58 +0100803 ret = mtd_device_parse_register(mtd, NULL, NULL,
Murali Karicheri192afdb2012-11-02 10:22:41 -0400804 pdata->parts, pdata->nr_parts);
Brian Norrisa61ae812015-10-30 20:33:25 -0700805 else
Boris BREZILLONa5cfb4d2015-12-10 08:59:58 +0100806 ret = mtd_device_register(mtd, NULL, 0);
David Brownellff4569c2009-03-04 12:01:37 -0800807 if (ret < 0)
Miquel Raynal4acc3042018-03-21 14:01:44 +0100808 goto err_cleanup_nand;
David Brownellff4569c2009-03-04 12:01:37 -0800809
810 val = davinci_nand_readl(info, NRCSR_OFFSET);
811 dev_info(&pdev->dev, "controller rev. %d.%d\n",
812 (val >> 8) & 0xff, val & 0xff);
813
814 return 0;
815
Miquel Raynal4acc3042018-03-21 14:01:44 +0100816err_cleanup_nand:
817 nand_cleanup(&info->chip);
818
Ivan Khoronzhuk30a39702013-12-17 15:37:00 +0200819err:
David Brownell6a4123e2009-04-21 19:58:13 -0700820 spin_lock_irq(&davinci_nand_lock);
Boris Brezillon363b5db2016-04-01 14:54:25 +0200821 if (info->chip.ecc.mode == NAND_ECC_HW_SYNDROME)
David Brownell6a4123e2009-04-21 19:58:13 -0700822 ecc4_busy = false;
823 spin_unlock_irq(&davinci_nand_lock);
David Brownellff4569c2009-03-04 12:01:37 -0800824 return ret;
825}
826
Ivan Khoronzhukeaaa4a92013-12-17 15:33:50 +0200827static int nand_davinci_remove(struct platform_device *pdev)
David Brownellff4569c2009-03-04 12:01:37 -0800828{
829 struct davinci_nand_info *info = platform_get_drvdata(pdev);
David Brownellff4569c2009-03-04 12:01:37 -0800830
David Brownell6a4123e2009-04-21 19:58:13 -0700831 spin_lock_irq(&davinci_nand_lock);
832 if (info->chip.ecc.mode == NAND_ECC_HW_SYNDROME)
833 ecc4_busy = false;
834 spin_unlock_irq(&davinci_nand_lock);
835
Boris BREZILLONa5cfb4d2015-12-10 08:59:58 +0100836 nand_release(nand_to_mtd(&info->chip));
David Brownellff4569c2009-03-04 12:01:37 -0800837
David Brownellff4569c2009-03-04 12:01:37 -0800838 return 0;
839}
840
841static struct platform_driver nand_davinci_driver = {
Ivan Khoronzhukeaaa4a92013-12-17 15:33:50 +0200842 .probe = nand_davinci_probe,
843 .remove = nand_davinci_remove,
David Brownellff4569c2009-03-04 12:01:37 -0800844 .driver = {
845 .name = "davinci_nand",
Sachin Kamatc4f8cde2013-03-14 15:37:01 +0530846 .of_match_table = of_match_ptr(davinci_nand_of_match),
David Brownellff4569c2009-03-04 12:01:37 -0800847 },
848};
849MODULE_ALIAS("platform:davinci_nand");
850
Ivan Khoronzhukeaaa4a92013-12-17 15:33:50 +0200851module_platform_driver(nand_davinci_driver);
David Brownellff4569c2009-03-04 12:01:37 -0800852
853MODULE_LICENSE("GPL");
854MODULE_AUTHOR("Texas Instruments");
855MODULE_DESCRIPTION("Davinci NAND flash driver");
856