Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * davinci_nand.c - NAND Flash Driver for DaVinci family chips
  4 *
  5 * Copyright © 2006 Texas Instruments.
  6 *
  7 * Port to 2.6.23 Copyright © 2008 by:
  8 *   Sander Huijsen <Shuijsen@optelecom-nkf.com>
  9 *   Troy Kisky <troy.kisky@boundarydevices.com>
 10 *   Dirk Behme <Dirk.Behme@gmail.com>
 11 */
 12
 13#include <linux/kernel.h>
 14#include <linux/module.h>
 15#include <linux/platform_device.h>
 16#include <linux/err.h>
 17#include <linux/iopoll.h>
 18#include <linux/mtd/rawnand.h>
 
 19#include <linux/mtd/partitions.h>
 
 
 
 20#include <linux/slab.h>
 21#include <linux/of.h>
 22
 23#include <linux/platform_data/mtd-davinci.h>
 24#include <linux/platform_data/mtd-davinci-aemif.h>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 25
 26/*
 27 * This is a device driver for the NAND flash controller found on the
 28 * various DaVinci family chips.  It handles up to four SoC chipselects,
 29 * and some flavors of secondary chipselect (e.g. based on A12) as used
 30 * with multichip packages.
 31 *
 32 * The 1-bit ECC hardware is supported, as well as the newer 4-bit ECC
 33 * available on chips like the DM355 and OMAP-L137 and needed with the
 34 * more error-prone MLC NAND chips.
 35 *
 36 * This driver assumes EM_WAIT connects all the NAND devices' RDY/nBUSY
 37 * outputs in a "wire-AND" configuration, with no per-chip signals.
 38 */
 39struct davinci_nand_info {
 40	struct nand_controller	controller;
 41	struct nand_chip	chip;
 42
 43	struct platform_device	*pdev;
 44
 45	bool			is_readmode;
 46
 47	void __iomem		*base;
 48	void __iomem		*vaddr;
 49
 50	void __iomem		*current_cs;
 51
 52	uint32_t		mask_chipsel;
 53	uint32_t		mask_ale;
 54	uint32_t		mask_cle;
 55
 56	uint32_t		core_chipsel;
 57
 58	struct davinci_aemif_timing	*timing;
 59};
 60
 61static DEFINE_SPINLOCK(davinci_nand_lock);
 62static bool ecc4_busy;
 63
 64static inline struct davinci_nand_info *to_davinci_nand(struct mtd_info *mtd)
 65{
 66	return container_of(mtd_to_nand(mtd), struct davinci_nand_info, chip);
 67}
 68
 69static inline unsigned int davinci_nand_readl(struct davinci_nand_info *info,
 70		int offset)
 71{
 72	return __raw_readl(info->base + offset);
 73}
 74
 75static inline void davinci_nand_writel(struct davinci_nand_info *info,
 76		int offset, unsigned long value)
 77{
 78	__raw_writel(value, info->base + offset);
 79}
 80
 81/*----------------------------------------------------------------------*/
 82
 83/*
 84 * 1-bit hardware ECC ... context maintained for each core chipselect
 85 */
 86
 87static inline uint32_t nand_davinci_readecc_1bit(struct mtd_info *mtd)
 88{
 89	struct davinci_nand_info *info = to_davinci_nand(mtd);
 90
 91	return davinci_nand_readl(info, NANDF1ECC_OFFSET
 92			+ 4 * info->core_chipsel);
 93}
 94
 95static void nand_davinci_hwctl_1bit(struct nand_chip *chip, int mode)
 96{
 97	struct davinci_nand_info *info;
 98	uint32_t nandcfr;
 99	unsigned long flags;
100
101	info = to_davinci_nand(nand_to_mtd(chip));
102
103	/* Reset ECC hardware */
104	nand_davinci_readecc_1bit(nand_to_mtd(chip));
105
106	spin_lock_irqsave(&davinci_nand_lock, flags);
107
108	/* Restart ECC hardware */
109	nandcfr = davinci_nand_readl(info, NANDFCR_OFFSET);
110	nandcfr |= BIT(8 + info->core_chipsel);
111	davinci_nand_writel(info, NANDFCR_OFFSET, nandcfr);
112
113	spin_unlock_irqrestore(&davinci_nand_lock, flags);
114}
115
116/*
117 * Read hardware ECC value and pack into three bytes
118 */
119static int nand_davinci_calculate_1bit(struct nand_chip *chip,
120				       const u_char *dat, u_char *ecc_code)
121{
122	unsigned int ecc_val = nand_davinci_readecc_1bit(nand_to_mtd(chip));
123	unsigned int ecc24 = (ecc_val & 0x0fff) | ((ecc_val & 0x0fff0000) >> 4);
124
125	/* invert so that erased block ecc is correct */
126	ecc24 = ~ecc24;
127	ecc_code[0] = (u_char)(ecc24);
128	ecc_code[1] = (u_char)(ecc24 >> 8);
129	ecc_code[2] = (u_char)(ecc24 >> 16);
130
131	return 0;
132}
133
134static int nand_davinci_correct_1bit(struct nand_chip *chip, u_char *dat,
135				     u_char *read_ecc, u_char *calc_ecc)
136{
137	uint32_t eccNand = read_ecc[0] | (read_ecc[1] << 8) |
138					  (read_ecc[2] << 16);
139	uint32_t eccCalc = calc_ecc[0] | (calc_ecc[1] << 8) |
140					  (calc_ecc[2] << 16);
141	uint32_t diff = eccCalc ^ eccNand;
142
143	if (diff) {
144		if ((((diff >> 12) ^ diff) & 0xfff) == 0xfff) {
145			/* Correctable error */
146			if ((diff >> (12 + 3)) < chip->ecc.size) {
147				dat[diff >> (12 + 3)] ^= BIT((diff >> 12) & 7);
148				return 1;
149			} else {
150				return -EBADMSG;
151			}
152		} else if (!(diff & (diff - 1))) {
153			/* Single bit ECC error in the ECC itself,
154			 * nothing to fix */
155			return 1;
156		} else {
157			/* Uncorrectable error */
158			return -EBADMSG;
159		}
160
161	}
162	return 0;
163}
164
165/*----------------------------------------------------------------------*/
166
167/*
168 * 4-bit hardware ECC ... context maintained over entire AEMIF
169 *
170 * This is a syndrome engine, but we avoid NAND_ECC_PLACEMENT_INTERLEAVED
171 * since that forces use of a problematic "infix OOB" layout.
172 * Among other things, it trashes manufacturer bad block markers.
173 * Also, and specific to this hardware, it ECC-protects the "prepad"
174 * in the OOB ... while having ECC protection for parts of OOB would
175 * seem useful, the current MTD stack sometimes wants to update the
176 * OOB without recomputing ECC.
177 */
178
179static void nand_davinci_hwctl_4bit(struct nand_chip *chip, int mode)
180{
181	struct davinci_nand_info *info = to_davinci_nand(nand_to_mtd(chip));
182	unsigned long flags;
183	u32 val;
184
185	/* Reset ECC hardware */
186	davinci_nand_readl(info, NAND_4BIT_ECC1_OFFSET);
187
188	spin_lock_irqsave(&davinci_nand_lock, flags);
189
190	/* Start 4-bit ECC calculation for read/write */
191	val = davinci_nand_readl(info, NANDFCR_OFFSET);
192	val &= ~(0x03 << 4);
193	val |= (info->core_chipsel << 4) | BIT(12);
194	davinci_nand_writel(info, NANDFCR_OFFSET, val);
195
196	info->is_readmode = (mode == NAND_ECC_READ);
197
198	spin_unlock_irqrestore(&davinci_nand_lock, flags);
199}
200
201/* Read raw ECC code after writing to NAND. */
202static void
203nand_davinci_readecc_4bit(struct davinci_nand_info *info, u32 code[4])
204{
205	const u32 mask = 0x03ff03ff;
206
207	code[0] = davinci_nand_readl(info, NAND_4BIT_ECC1_OFFSET) & mask;
208	code[1] = davinci_nand_readl(info, NAND_4BIT_ECC2_OFFSET) & mask;
209	code[2] = davinci_nand_readl(info, NAND_4BIT_ECC3_OFFSET) & mask;
210	code[3] = davinci_nand_readl(info, NAND_4BIT_ECC4_OFFSET) & mask;
211}
212
213/* Terminate read ECC; or return ECC (as bytes) of data written to NAND. */
214static int nand_davinci_calculate_4bit(struct nand_chip *chip,
215				       const u_char *dat, u_char *ecc_code)
216{
217	struct davinci_nand_info *info = to_davinci_nand(nand_to_mtd(chip));
218	u32 raw_ecc[4], *p;
219	unsigned i;
220
221	/* After a read, terminate ECC calculation by a dummy read
222	 * of some 4-bit ECC register.  ECC covers everything that
223	 * was read; correct() just uses the hardware state, so
224	 * ecc_code is not needed.
225	 */
226	if (info->is_readmode) {
227		davinci_nand_readl(info, NAND_4BIT_ECC1_OFFSET);
228		return 0;
229	}
230
231	/* Pack eight raw 10-bit ecc values into ten bytes, making
232	 * two passes which each convert four values (in upper and
233	 * lower halves of two 32-bit words) into five bytes.  The
234	 * ROM boot loader uses this same packing scheme.
235	 */
236	nand_davinci_readecc_4bit(info, raw_ecc);
237	for (i = 0, p = raw_ecc; i < 2; i++, p += 2) {
238		*ecc_code++ =   p[0]        & 0xff;
239		*ecc_code++ = ((p[0] >>  8) & 0x03) | ((p[0] >> 14) & 0xfc);
240		*ecc_code++ = ((p[0] >> 22) & 0x0f) | ((p[1] <<  4) & 0xf0);
241		*ecc_code++ = ((p[1] >>  4) & 0x3f) | ((p[1] >> 10) & 0xc0);
242		*ecc_code++ =  (p[1] >> 18) & 0xff;
243	}
244
245	return 0;
246}
247
248/* Correct up to 4 bits in data we just read, using state left in the
249 * hardware plus the ecc_code computed when it was first written.
250 */
251static int nand_davinci_correct_4bit(struct nand_chip *chip, u_char *data,
252				     u_char *ecc_code, u_char *null)
253{
254	int i;
255	struct davinci_nand_info *info = to_davinci_nand(nand_to_mtd(chip));
256	unsigned short ecc10[8];
257	unsigned short *ecc16;
258	u32 syndrome[4];
259	u32 ecc_state;
260	unsigned num_errors, corrected;
261	unsigned long timeo;
262
263	/* Unpack ten bytes into eight 10 bit values.  We know we're
264	 * little-endian, and use type punning for less shifting/masking.
265	 */
266	if (WARN_ON(0x01 & (uintptr_t)ecc_code))
267		return -EINVAL;
268	ecc16 = (unsigned short *)ecc_code;
269
270	ecc10[0] =  (ecc16[0] >>  0) & 0x3ff;
271	ecc10[1] = ((ecc16[0] >> 10) & 0x3f) | ((ecc16[1] << 6) & 0x3c0);
272	ecc10[2] =  (ecc16[1] >>  4) & 0x3ff;
273	ecc10[3] = ((ecc16[1] >> 14) & 0x3)  | ((ecc16[2] << 2) & 0x3fc);
274	ecc10[4] =  (ecc16[2] >>  8)         | ((ecc16[3] << 8) & 0x300);
275	ecc10[5] =  (ecc16[3] >>  2) & 0x3ff;
276	ecc10[6] = ((ecc16[3] >> 12) & 0xf)  | ((ecc16[4] << 4) & 0x3f0);
277	ecc10[7] =  (ecc16[4] >>  6) & 0x3ff;
278
279	/* Tell ECC controller about the expected ECC codes. */
280	for (i = 7; i >= 0; i--)
281		davinci_nand_writel(info, NAND_4BIT_ECC_LOAD_OFFSET, ecc10[i]);
282
283	/* Allow time for syndrome calculation ... then read it.
284	 * A syndrome of all zeroes 0 means no detected errors.
285	 */
286	davinci_nand_readl(info, NANDFSR_OFFSET);
287	nand_davinci_readecc_4bit(info, syndrome);
288	if (!(syndrome[0] | syndrome[1] | syndrome[2] | syndrome[3]))
289		return 0;
290
291	/*
292	 * Clear any previous address calculation by doing a dummy read of an
293	 * error address register.
294	 */
295	davinci_nand_readl(info, NAND_ERR_ADD1_OFFSET);
296
297	/* Start address calculation, and wait for it to complete.
298	 * We _could_ start reading more data while this is working,
299	 * to speed up the overall page read.
300	 */
301	davinci_nand_writel(info, NANDFCR_OFFSET,
302			davinci_nand_readl(info, NANDFCR_OFFSET) | BIT(13));
303
304	/*
305	 * ECC_STATE field reads 0x3 (Error correction complete) immediately
306	 * after setting the 4BITECC_ADD_CALC_START bit. So if you immediately
307	 * begin trying to poll for the state, you may fall right out of your
308	 * loop without any of the correction calculations having taken place.
309	 * The recommendation from the hardware team is to initially delay as
310	 * long as ECC_STATE reads less than 4. After that, ECC HW has entered
311	 * correction state.
312	 */
313	timeo = jiffies + usecs_to_jiffies(100);
314	do {
315		ecc_state = (davinci_nand_readl(info,
316				NANDFSR_OFFSET) >> 8) & 0x0f;
317		cpu_relax();
318	} while ((ecc_state < 4) && time_before(jiffies, timeo));
319
320	for (;;) {
321		u32	fsr = davinci_nand_readl(info, NANDFSR_OFFSET);
322
323		switch ((fsr >> 8) & 0x0f) {
324		case 0:		/* no error, should not happen */
325			davinci_nand_readl(info, NAND_ERR_ERRVAL1_OFFSET);
326			return 0;
327		case 1:		/* five or more errors detected */
328			davinci_nand_readl(info, NAND_ERR_ERRVAL1_OFFSET);
329			return -EBADMSG;
330		case 2:		/* error addresses computed */
331		case 3:
332			num_errors = 1 + ((fsr >> 16) & 0x03);
333			goto correct;
334		default:	/* still working on it */
335			cpu_relax();
336			continue;
337		}
338	}
339
340correct:
341	/* correct each error */
342	for (i = 0, corrected = 0; i < num_errors; i++) {
343		int error_address, error_value;
344
345		if (i > 1) {
346			error_address = davinci_nand_readl(info,
347						NAND_ERR_ADD2_OFFSET);
348			error_value = davinci_nand_readl(info,
349						NAND_ERR_ERRVAL2_OFFSET);
350		} else {
351			error_address = davinci_nand_readl(info,
352						NAND_ERR_ADD1_OFFSET);
353			error_value = davinci_nand_readl(info,
354						NAND_ERR_ERRVAL1_OFFSET);
355		}
356
357		if (i & 1) {
358			error_address >>= 16;
359			error_value >>= 16;
360		}
361		error_address &= 0x3ff;
362		error_address = (512 + 7) - error_address;
363
364		if (error_address < 512) {
365			data[error_address] ^= error_value;
366			corrected++;
367		}
368	}
369
370	return corrected;
371}
372
373/*----------------------------------------------------------------------*/
374
375/* An ECC layout for using 4-bit ECC with small-page flash, storing
376 * ten ECC bytes plus the manufacturer's bad block marker byte, and
377 * and not overlapping the default BBT markers.
378 */
379static int hwecc4_ooblayout_small_ecc(struct mtd_info *mtd, int section,
380				      struct mtd_oob_region *oobregion)
381{
382	if (section > 2)
383		return -ERANGE;
384
385	if (!section) {
386		oobregion->offset = 0;
387		oobregion->length = 5;
388	} else if (section == 1) {
389		oobregion->offset = 6;
390		oobregion->length = 2;
391	} else {
392		oobregion->offset = 13;
393		oobregion->length = 3;
394	}
395
396	return 0;
397}
398
399static int hwecc4_ooblayout_small_free(struct mtd_info *mtd, int section,
400				       struct mtd_oob_region *oobregion)
401{
402	if (section > 1)
403		return -ERANGE;
404
405	if (!section) {
406		oobregion->offset = 8;
407		oobregion->length = 5;
408	} else {
409		oobregion->offset = 16;
410		oobregion->length = mtd->oobsize - 16;
411	}
412
413	return 0;
414}
415
416static const struct mtd_ooblayout_ops hwecc4_small_ooblayout_ops = {
417	.ecc = hwecc4_ooblayout_small_ecc,
418	.free = hwecc4_ooblayout_small_free,
419};
420
421#if defined(CONFIG_OF)
422static const struct of_device_id davinci_nand_of_match[] = {
423	{.compatible = "ti,davinci-nand", },
424	{.compatible = "ti,keystone-nand", },
425	{},
426};
427MODULE_DEVICE_TABLE(of, davinci_nand_of_match);
428
429static struct davinci_nand_pdata
430	*nand_davinci_get_pdata(struct platform_device *pdev)
431{
432	if (!dev_get_platdata(&pdev->dev) && pdev->dev.of_node) {
433		struct davinci_nand_pdata *pdata;
434		const char *mode;
435		u32 prop;
436
437		pdata =  devm_kzalloc(&pdev->dev,
438				sizeof(struct davinci_nand_pdata),
439				GFP_KERNEL);
440		pdev->dev.platform_data = pdata;
441		if (!pdata)
442			return ERR_PTR(-ENOMEM);
443		if (!of_property_read_u32(pdev->dev.of_node,
444			"ti,davinci-chipselect", &prop))
445			pdata->core_chipsel = prop;
446		else
447			return ERR_PTR(-EINVAL);
448
449		if (!of_property_read_u32(pdev->dev.of_node,
450			"ti,davinci-mask-ale", &prop))
451			pdata->mask_ale = prop;
452		if (!of_property_read_u32(pdev->dev.of_node,
453			"ti,davinci-mask-cle", &prop))
454			pdata->mask_cle = prop;
455		if (!of_property_read_u32(pdev->dev.of_node,
456			"ti,davinci-mask-chipsel", &prop))
457			pdata->mask_chipsel = prop;
458		if (!of_property_read_string(pdev->dev.of_node,
459			"ti,davinci-ecc-mode", &mode)) {
 
460			if (!strncmp("none", mode, 4))
461				pdata->engine_type = NAND_ECC_ENGINE_TYPE_NONE;
462			if (!strncmp("soft", mode, 4))
463				pdata->engine_type = NAND_ECC_ENGINE_TYPE_SOFT;
464			if (!strncmp("hw", mode, 2))
465				pdata->engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST;
466		}
467		if (!of_property_read_u32(pdev->dev.of_node,
468			"ti,davinci-ecc-bits", &prop))
469			pdata->ecc_bits = prop;
470
471		if (!of_property_read_u32(pdev->dev.of_node,
472			"ti,davinci-nand-buswidth", &prop) && prop == 16)
 
473			pdata->options |= NAND_BUSWIDTH_16;
474
475		if (of_property_read_bool(pdev->dev.of_node,
476			"ti,davinci-nand-use-bbt"))
477			pdata->bbt_options = NAND_BBT_USE_FLASH;
478
479		/*
480		 * Since kernel v4.8, this driver has been fixed to enable
481		 * use of 4-bit hardware ECC with subpages and verified on
482		 * TI's keystone EVMs (K2L, K2HK and K2E).
483		 * However, in the interest of not breaking systems using
484		 * existing UBI partitions, sub-page writes are not being
485		 * (re)enabled. If you want to use subpage writes on Keystone
486		 * platforms (i.e. do not have any existing UBI partitions),
487		 * then use "ti,davinci-nand" as the compatible in your
488		 * device-tree file.
489		 */
490		if (of_device_is_compatible(pdev->dev.of_node,
491					    "ti,keystone-nand")) {
492			pdata->options |= NAND_NO_SUBPAGE_WRITE;
493		}
494	}
495
496	return dev_get_platdata(&pdev->dev);
497}
498#else
499static struct davinci_nand_pdata
500	*nand_davinci_get_pdata(struct platform_device *pdev)
501{
502	return dev_get_platdata(&pdev->dev);
503}
504#endif
505
506static int davinci_nand_attach_chip(struct nand_chip *chip)
507{
508	struct mtd_info *mtd = nand_to_mtd(chip);
509	struct davinci_nand_info *info = to_davinci_nand(mtd);
510	struct davinci_nand_pdata *pdata = nand_davinci_get_pdata(info->pdev);
511	int ret = 0;
512
513	if (IS_ERR(pdata))
514		return PTR_ERR(pdata);
515
516	/* Use board-specific ECC config */
517	chip->ecc.engine_type = pdata->engine_type;
518	chip->ecc.placement = pdata->ecc_placement;
519
520	switch (chip->ecc.engine_type) {
521	case NAND_ECC_ENGINE_TYPE_NONE:
522		pdata->ecc_bits = 0;
523		break;
524	case NAND_ECC_ENGINE_TYPE_SOFT:
525		pdata->ecc_bits = 0;
526		/*
527		 * This driver expects Hamming based ECC when engine_type is set
528		 * to NAND_ECC_ENGINE_TYPE_SOFT. Force ecc.algo to
529		 * NAND_ECC_ALGO_HAMMING to avoid adding an extra ->ecc_algo
530		 * field to davinci_nand_pdata.
531		 */
532		chip->ecc.algo = NAND_ECC_ALGO_HAMMING;
533		break;
534	case NAND_ECC_ENGINE_TYPE_ON_HOST:
535		if (pdata->ecc_bits == 4) {
536			int chunks = mtd->writesize / 512;
537
538			if (!chunks || mtd->oobsize < 16) {
539				dev_dbg(&info->pdev->dev, "too small\n");
540				return -EINVAL;
541			}
542
543			/*
544			 * No sanity checks:  CPUs must support this,
545			 * and the chips may not use NAND_BUSWIDTH_16.
546			 */
547
548			/* No sharing 4-bit hardware between chipselects yet */
549			spin_lock_irq(&davinci_nand_lock);
550			if (ecc4_busy)
551				ret = -EBUSY;
552			else
553				ecc4_busy = true;
554			spin_unlock_irq(&davinci_nand_lock);
555
556			if (ret == -EBUSY)
557				return ret;
558
559			chip->ecc.calculate = nand_davinci_calculate_4bit;
560			chip->ecc.correct = nand_davinci_correct_4bit;
561			chip->ecc.hwctl = nand_davinci_hwctl_4bit;
562			chip->ecc.bytes = 10;
563			chip->ecc.options = NAND_ECC_GENERIC_ERASED_CHECK;
564			chip->ecc.algo = NAND_ECC_ALGO_BCH;
565
566			/*
567			 * Update ECC layout if needed ... for 1-bit HW ECC, the
568			 * default is OK, but it allocates 6 bytes when only 3
569			 * are needed (for each 512 bytes). For 4-bit HW ECC,
570			 * the default is not usable: 10 bytes needed, not 6.
571			 *
572			 * For small page chips, preserve the manufacturer's
573			 * badblock marking data ... and make sure a flash BBT
574			 * table marker fits in the free bytes.
575			 */
576			if (chunks == 1) {
577				mtd_set_ooblayout(mtd,
578						  &hwecc4_small_ooblayout_ops);
579			} else if (chunks == 4 || chunks == 8) {
580				mtd_set_ooblayout(mtd,
581						  nand_get_large_page_ooblayout());
582				chip->ecc.read_page = nand_read_page_hwecc_oob_first;
583			} else {
584				return -EIO;
585			}
586		} else {
587			/* 1bit ecc hamming */
588			chip->ecc.calculate = nand_davinci_calculate_1bit;
589			chip->ecc.correct = nand_davinci_correct_1bit;
590			chip->ecc.hwctl = nand_davinci_hwctl_1bit;
591			chip->ecc.bytes = 3;
592			chip->ecc.algo = NAND_ECC_ALGO_HAMMING;
593		}
594		chip->ecc.size = 512;
595		chip->ecc.strength = pdata->ecc_bits;
596		break;
597	default:
598		return -EINVAL;
599	}
600
601	return ret;
602}
603
604static void nand_davinci_data_in(struct davinci_nand_info *info, void *buf,
605				 unsigned int len, bool force_8bit)
606{
607	u32 alignment = ((uintptr_t)buf | len) & 3;
608
609	if (force_8bit || (alignment & 1))
610		ioread8_rep(info->current_cs, buf, len);
611	else if (alignment & 3)
612		ioread16_rep(info->current_cs, buf, len >> 1);
613	else
614		ioread32_rep(info->current_cs, buf, len >> 2);
615}
616
617static void nand_davinci_data_out(struct davinci_nand_info *info,
618				  const void *buf, unsigned int len,
619				  bool force_8bit)
620{
621	u32 alignment = ((uintptr_t)buf | len) & 3;
622
623	if (force_8bit || (alignment & 1))
624		iowrite8_rep(info->current_cs, buf, len);
625	else if (alignment & 3)
626		iowrite16_rep(info->current_cs, buf, len >> 1);
627	else
628		iowrite32_rep(info->current_cs, buf, len >> 2);
629}
630
631static int davinci_nand_exec_instr(struct davinci_nand_info *info,
632				   const struct nand_op_instr *instr)
633{
634	unsigned int i, timeout_us;
635	u32 status;
636	int ret;
637
638	switch (instr->type) {
639	case NAND_OP_CMD_INSTR:
640		iowrite8(instr->ctx.cmd.opcode,
641			 info->current_cs + info->mask_cle);
642		break;
643
644	case NAND_OP_ADDR_INSTR:
645		for (i = 0; i < instr->ctx.addr.naddrs; i++) {
646			iowrite8(instr->ctx.addr.addrs[i],
647				 info->current_cs + info->mask_ale);
648		}
649		break;
650
651	case NAND_OP_DATA_IN_INSTR:
652		nand_davinci_data_in(info, instr->ctx.data.buf.in,
653				     instr->ctx.data.len,
654				     instr->ctx.data.force_8bit);
655		break;
656
657	case NAND_OP_DATA_OUT_INSTR:
658		nand_davinci_data_out(info, instr->ctx.data.buf.out,
659				      instr->ctx.data.len,
660				      instr->ctx.data.force_8bit);
661		break;
662
663	case NAND_OP_WAITRDY_INSTR:
664		timeout_us = instr->ctx.waitrdy.timeout_ms * 1000;
665		ret = readl_relaxed_poll_timeout(info->base + NANDFSR_OFFSET,
666						 status, status & BIT(0), 100,
667						 timeout_us);
668		if (ret)
669			return ret;
670
671		break;
672	}
673
674	if (instr->delay_ns)
 
 
675		ndelay(instr->delay_ns);
 
676
677	return 0;
678}
679
680static int davinci_nand_exec_op(struct nand_chip *chip,
681				const struct nand_operation *op,
682				bool check_only)
683{
684	struct davinci_nand_info *info = to_davinci_nand(nand_to_mtd(chip));
685	unsigned int i;
686
687	if (check_only)
688		return 0;
689
690	info->current_cs = info->vaddr + (op->cs * info->mask_chipsel);
691
692	for (i = 0; i < op->ninstrs; i++) {
693		int ret;
694
695		ret = davinci_nand_exec_instr(info, &op->instrs[i]);
696		if (ret)
697			return ret;
698	}
699
700	return 0;
701}
702
703static const struct nand_controller_ops davinci_nand_controller_ops = {
704	.attach_chip = davinci_nand_attach_chip,
705	.exec_op = davinci_nand_exec_op,
706};
707
708static int nand_davinci_probe(struct platform_device *pdev)
709{
710	struct davinci_nand_pdata	*pdata;
711	struct davinci_nand_info	*info;
712	struct resource			*res1;
713	struct resource			*res2;
714	void __iomem			*vaddr;
715	void __iomem			*base;
716	int				ret;
717	uint32_t			val;
718	struct mtd_info			*mtd;
719
720	pdata = nand_davinci_get_pdata(pdev);
721	if (IS_ERR(pdata))
722		return PTR_ERR(pdata);
723
724	/* insist on board-specific configuration */
725	if (!pdata)
726		return -ENODEV;
727
728	/* which external chipselect will we be managing? */
729	if (pdata->core_chipsel > 3)
730		return -ENODEV;
731
732	info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
733	if (!info)
734		return -ENOMEM;
735
736	platform_set_drvdata(pdev, info);
737
738	res1 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
739	res2 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
740	if (!res1 || !res2) {
741		dev_err(&pdev->dev, "resource missing\n");
742		return -EINVAL;
743	}
744
745	vaddr = devm_ioremap_resource(&pdev->dev, res1);
746	if (IS_ERR(vaddr))
747		return PTR_ERR(vaddr);
748
749	/*
750	 * This registers range is used to setup NAND settings. In case with
751	 * TI AEMIF driver, the same memory address range is requested already
752	 * by AEMIF, so we cannot request it twice, just ioremap.
753	 * The AEMIF and NAND drivers not use the same registers in this range.
754	 */
755	base = devm_ioremap(&pdev->dev, res2->start, resource_size(res2));
756	if (!base) {
757		dev_err(&pdev->dev, "ioremap failed for resource %pR\n", res2);
758		return -EADDRNOTAVAIL;
759	}
760
761	info->pdev		= pdev;
762	info->base		= base;
763	info->vaddr		= vaddr;
764
765	mtd			= nand_to_mtd(&info->chip);
766	mtd->dev.parent		= &pdev->dev;
767	nand_set_flash_node(&info->chip, pdev->dev.of_node);
768
769	/* options such as NAND_BBT_USE_FLASH */
770	info->chip.bbt_options	= pdata->bbt_options;
771	/* options such as 16-bit widths */
772	info->chip.options	= pdata->options;
773	info->chip.bbt_td	= pdata->bbt_td;
774	info->chip.bbt_md	= pdata->bbt_md;
775	info->timing		= pdata->timing;
776
777	info->current_cs	= info->vaddr;
778	info->core_chipsel	= pdata->core_chipsel;
779	info->mask_chipsel	= pdata->mask_chipsel;
780
781	/* use nandboot-capable ALE/CLE masks by default */
782	info->mask_ale		= pdata->mask_ale ? : MASK_ALE;
783	info->mask_cle		= pdata->mask_cle ? : MASK_CLE;
784
785	spin_lock_irq(&davinci_nand_lock);
786
787	/* put CSxNAND into NAND mode */
788	val = davinci_nand_readl(info, NANDFCR_OFFSET);
789	val |= BIT(info->core_chipsel);
790	davinci_nand_writel(info, NANDFCR_OFFSET, val);
791
792	spin_unlock_irq(&davinci_nand_lock);
793
794	/* Scan to find existence of the device(s) */
795	nand_controller_init(&info->controller);
796	info->controller.ops = &davinci_nand_controller_ops;
797	info->chip.controller = &info->controller;
798	ret = nand_scan(&info->chip, pdata->mask_chipsel ? 2 : 1);
799	if (ret < 0) {
800		dev_dbg(&pdev->dev, "no NAND chip(s) found\n");
801		return ret;
802	}
803
804	if (pdata->parts)
805		ret = mtd_device_register(mtd, pdata->parts, pdata->nr_parts);
806	else
807		ret = mtd_device_register(mtd, NULL, 0);
808	if (ret < 0)
809		goto err_cleanup_nand;
810
811	val = davinci_nand_readl(info, NRCSR_OFFSET);
812	dev_info(&pdev->dev, "controller rev. %d.%d\n",
813	       (val >> 8) & 0xff, val & 0xff);
814
815	return 0;
816
817err_cleanup_nand:
818	nand_cleanup(&info->chip);
819
820	return ret;
821}
822
823static void nand_davinci_remove(struct platform_device *pdev)
824{
825	struct davinci_nand_info *info = platform_get_drvdata(pdev);
826	struct nand_chip *chip = &info->chip;
827	int ret;
828
829	spin_lock_irq(&davinci_nand_lock);
830	if (chip->ecc.placement == NAND_ECC_PLACEMENT_INTERLEAVED)
831		ecc4_busy = false;
832	spin_unlock_irq(&davinci_nand_lock);
833
834	ret = mtd_device_unregister(nand_to_mtd(chip));
835	WARN_ON(ret);
836	nand_cleanup(chip);
837}
838
839static struct platform_driver nand_davinci_driver = {
840	.probe		= nand_davinci_probe,
841	.remove_new	= nand_davinci_remove,
842	.driver		= {
843		.name	= "davinci_nand",
844		.of_match_table = of_match_ptr(davinci_nand_of_match),
845	},
846};
847MODULE_ALIAS("platform:davinci_nand");
848
849module_platform_driver(nand_davinci_driver);
850
851MODULE_LICENSE("GPL");
852MODULE_AUTHOR("Texas Instruments");
853MODULE_DESCRIPTION("Davinci NAND flash driver");
854
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * davinci_nand.c - NAND Flash Driver for DaVinci family chips
  4 *
  5 * Copyright © 2006 Texas Instruments.
  6 *
  7 * Port to 2.6.23 Copyright © 2008 by:
  8 *   Sander Huijsen <Shuijsen@optelecom-nkf.com>
  9 *   Troy Kisky <troy.kisky@boundarydevices.com>
 10 *   Dirk Behme <Dirk.Behme@gmail.com>
 11 */
 12
 
 
 
 13#include <linux/err.h>
 14#include <linux/iopoll.h>
 15#include <linux/kernel.h>
 16#include <linux/module.h>
 17#include <linux/mtd/partitions.h>
 18#include <linux/mtd/rawnand.h>
 19#include <linux/platform_device.h>
 20#include <linux/property.h>
 21#include <linux/slab.h>
 
 22
 23#define NRCSR_OFFSET		0x00
 24#define NANDFCR_OFFSET		0x60
 25#define NANDFSR_OFFSET		0x64
 26#define NANDF1ECC_OFFSET	0x70
 27
 28/* 4-bit ECC syndrome registers */
 29#define NAND_4BIT_ECC_LOAD_OFFSET	0xbc
 30#define NAND_4BIT_ECC1_OFFSET		0xc0
 31#define NAND_4BIT_ECC2_OFFSET		0xc4
 32#define NAND_4BIT_ECC3_OFFSET		0xc8
 33#define NAND_4BIT_ECC4_OFFSET		0xcc
 34#define NAND_ERR_ADD1_OFFSET		0xd0
 35#define NAND_ERR_ADD2_OFFSET		0xd4
 36#define NAND_ERR_ERRVAL1_OFFSET		0xd8
 37#define NAND_ERR_ERRVAL2_OFFSET		0xdc
 38
 39/* NOTE:  boards don't need to use these address bits
 40 * for ALE/CLE unless they support booting from NAND.
 41 * They're used unless platform data overrides them.
 42 */
 43#define	MASK_ALE		0x08
 44#define	MASK_CLE		0x10
 45
 46struct davinci_nand_pdata {
 47	uint32_t		mask_ale;
 48	uint32_t		mask_cle;
 49
 50	/*
 51	 * 0-indexed chip-select number of the asynchronous
 52	 * interface to which the NAND device has been connected.
 53	 *
 54	 * So, if you have NAND connected to CS3 of DA850, you
 55	 * will pass '1' here. Since the asynchronous interface
 56	 * on DA850 starts from CS2.
 57	 */
 58	uint32_t		core_chipsel;
 59
 60	/* for packages using two chipselects */
 61	uint32_t		mask_chipsel;
 62
 63	/* board's default static partition info */
 64	struct mtd_partition	*parts;
 65	unsigned int		nr_parts;
 66
 67	/* none  == NAND_ECC_ENGINE_TYPE_NONE (strongly *not* advised!!)
 68	 * soft  == NAND_ECC_ENGINE_TYPE_SOFT
 69	 * else  == NAND_ECC_ENGINE_TYPE_ON_HOST, according to ecc_bits
 70	 *
 71	 * All DaVinci-family chips support 1-bit hardware ECC.
 72	 * Newer ones also support 4-bit ECC, but are awkward
 73	 * using it with large page chips.
 74	 */
 75	enum nand_ecc_engine_type engine_type;
 76	enum nand_ecc_placement ecc_placement;
 77	u8			ecc_bits;
 78
 79	/* e.g. NAND_BUSWIDTH_16 */
 80	unsigned int		options;
 81	/* e.g. NAND_BBT_USE_FLASH */
 82	unsigned int		bbt_options;
 83
 84	/* Main and mirror bbt descriptor overrides */
 85	struct nand_bbt_descr	*bbt_td;
 86	struct nand_bbt_descr	*bbt_md;
 87};
 88
 89/*
 90 * This is a device driver for the NAND flash controller found on the
 91 * various DaVinci family chips.  It handles up to four SoC chipselects,
 92 * and some flavors of secondary chipselect (e.g. based on A12) as used
 93 * with multichip packages.
 94 *
 95 * The 1-bit ECC hardware is supported, as well as the newer 4-bit ECC
 96 * available on chips like the DM355 and OMAP-L137 and needed with the
 97 * more error-prone MLC NAND chips.
 98 *
 99 * This driver assumes EM_WAIT connects all the NAND devices' RDY/nBUSY
100 * outputs in a "wire-AND" configuration, with no per-chip signals.
101 */
102struct davinci_nand_info {
103	struct nand_controller	controller;
104	struct nand_chip	chip;
105
106	struct platform_device	*pdev;
107
108	bool			is_readmode;
109
110	void __iomem		*base;
111	void __iomem		*vaddr;
112
113	void __iomem		*current_cs;
114
115	uint32_t		mask_chipsel;
116	uint32_t		mask_ale;
117	uint32_t		mask_cle;
118
119	uint32_t		core_chipsel;
 
 
120};
121
122static DEFINE_SPINLOCK(davinci_nand_lock);
123static bool ecc4_busy;
124
125static inline struct davinci_nand_info *to_davinci_nand(struct mtd_info *mtd)
126{
127	return container_of(mtd_to_nand(mtd), struct davinci_nand_info, chip);
128}
129
130static inline unsigned int davinci_nand_readl(struct davinci_nand_info *info,
131		int offset)
132{
133	return __raw_readl(info->base + offset);
134}
135
136static inline void davinci_nand_writel(struct davinci_nand_info *info,
137		int offset, unsigned long value)
138{
139	__raw_writel(value, info->base + offset);
140}
141
142/*----------------------------------------------------------------------*/
143
144/*
145 * 1-bit hardware ECC ... context maintained for each core chipselect
146 */
147
148static inline uint32_t nand_davinci_readecc_1bit(struct mtd_info *mtd)
149{
150	struct davinci_nand_info *info = to_davinci_nand(mtd);
151
152	return davinci_nand_readl(info, NANDF1ECC_OFFSET
153			+ 4 * info->core_chipsel);
154}
155
156static void nand_davinci_hwctl_1bit(struct nand_chip *chip, int mode)
157{
158	struct davinci_nand_info *info;
159	uint32_t nandcfr;
160	unsigned long flags;
161
162	info = to_davinci_nand(nand_to_mtd(chip));
163
164	/* Reset ECC hardware */
165	nand_davinci_readecc_1bit(nand_to_mtd(chip));
166
167	spin_lock_irqsave(&davinci_nand_lock, flags);
168
169	/* Restart ECC hardware */
170	nandcfr = davinci_nand_readl(info, NANDFCR_OFFSET);
171	nandcfr |= BIT(8 + info->core_chipsel);
172	davinci_nand_writel(info, NANDFCR_OFFSET, nandcfr);
173
174	spin_unlock_irqrestore(&davinci_nand_lock, flags);
175}
176
177/*
178 * Read hardware ECC value and pack into three bytes
179 */
180static int nand_davinci_calculate_1bit(struct nand_chip *chip,
181				       const u_char *dat, u_char *ecc_code)
182{
183	unsigned int ecc_val = nand_davinci_readecc_1bit(nand_to_mtd(chip));
184	unsigned int ecc24 = (ecc_val & 0x0fff) | ((ecc_val & 0x0fff0000) >> 4);
185
186	/* invert so that erased block ecc is correct */
187	ecc24 = ~ecc24;
188	ecc_code[0] = (u_char)(ecc24);
189	ecc_code[1] = (u_char)(ecc24 >> 8);
190	ecc_code[2] = (u_char)(ecc24 >> 16);
191
192	return 0;
193}
194
195static int nand_davinci_correct_1bit(struct nand_chip *chip, u_char *dat,
196				     u_char *read_ecc, u_char *calc_ecc)
197{
198	uint32_t eccNand = read_ecc[0] | (read_ecc[1] << 8) |
199					  (read_ecc[2] << 16);
200	uint32_t eccCalc = calc_ecc[0] | (calc_ecc[1] << 8) |
201					  (calc_ecc[2] << 16);
202	uint32_t diff = eccCalc ^ eccNand;
203
204	if (diff) {
205		if ((((diff >> 12) ^ diff) & 0xfff) == 0xfff) {
206			/* Correctable error */
207			if ((diff >> (12 + 3)) < chip->ecc.size) {
208				dat[diff >> (12 + 3)] ^= BIT((diff >> 12) & 7);
209				return 1;
210			} else {
211				return -EBADMSG;
212			}
213		} else if (!(diff & (diff - 1))) {
214			/* Single bit ECC error in the ECC itself,
215			 * nothing to fix */
216			return 1;
217		} else {
218			/* Uncorrectable error */
219			return -EBADMSG;
220		}
221
222	}
223	return 0;
224}
225
226/*----------------------------------------------------------------------*/
227
228/*
229 * 4-bit hardware ECC ... context maintained over entire AEMIF
230 *
231 * This is a syndrome engine, but we avoid NAND_ECC_PLACEMENT_INTERLEAVED
232 * since that forces use of a problematic "infix OOB" layout.
233 * Among other things, it trashes manufacturer bad block markers.
234 * Also, and specific to this hardware, it ECC-protects the "prepad"
235 * in the OOB ... while having ECC protection for parts of OOB would
236 * seem useful, the current MTD stack sometimes wants to update the
237 * OOB without recomputing ECC.
238 */
239
240static void nand_davinci_hwctl_4bit(struct nand_chip *chip, int mode)
241{
242	struct davinci_nand_info *info = to_davinci_nand(nand_to_mtd(chip));
243	unsigned long flags;
244	u32 val;
245
246	/* Reset ECC hardware */
247	davinci_nand_readl(info, NAND_4BIT_ECC1_OFFSET);
248
249	spin_lock_irqsave(&davinci_nand_lock, flags);
250
251	/* Start 4-bit ECC calculation for read/write */
252	val = davinci_nand_readl(info, NANDFCR_OFFSET);
253	val &= ~(0x03 << 4);
254	val |= (info->core_chipsel << 4) | BIT(12);
255	davinci_nand_writel(info, NANDFCR_OFFSET, val);
256
257	info->is_readmode = (mode == NAND_ECC_READ);
258
259	spin_unlock_irqrestore(&davinci_nand_lock, flags);
260}
261
262/* Read raw ECC code after writing to NAND. */
263static void
264nand_davinci_readecc_4bit(struct davinci_nand_info *info, u32 code[4])
265{
266	const u32 mask = 0x03ff03ff;
267
268	code[0] = davinci_nand_readl(info, NAND_4BIT_ECC1_OFFSET) & mask;
269	code[1] = davinci_nand_readl(info, NAND_4BIT_ECC2_OFFSET) & mask;
270	code[2] = davinci_nand_readl(info, NAND_4BIT_ECC3_OFFSET) & mask;
271	code[3] = davinci_nand_readl(info, NAND_4BIT_ECC4_OFFSET) & mask;
272}
273
274/* Terminate read ECC; or return ECC (as bytes) of data written to NAND. */
275static int nand_davinci_calculate_4bit(struct nand_chip *chip,
276				       const u_char *dat, u_char *ecc_code)
277{
278	struct davinci_nand_info *info = to_davinci_nand(nand_to_mtd(chip));
279	u32 raw_ecc[4], *p;
280	unsigned i;
281
282	/* After a read, terminate ECC calculation by a dummy read
283	 * of some 4-bit ECC register.  ECC covers everything that
284	 * was read; correct() just uses the hardware state, so
285	 * ecc_code is not needed.
286	 */
287	if (info->is_readmode) {
288		davinci_nand_readl(info, NAND_4BIT_ECC1_OFFSET);
289		return 0;
290	}
291
292	/* Pack eight raw 10-bit ecc values into ten bytes, making
293	 * two passes which each convert four values (in upper and
294	 * lower halves of two 32-bit words) into five bytes.  The
295	 * ROM boot loader uses this same packing scheme.
296	 */
297	nand_davinci_readecc_4bit(info, raw_ecc);
298	for (i = 0, p = raw_ecc; i < 2; i++, p += 2) {
299		*ecc_code++ =   p[0]        & 0xff;
300		*ecc_code++ = ((p[0] >>  8) & 0x03) | ((p[0] >> 14) & 0xfc);
301		*ecc_code++ = ((p[0] >> 22) & 0x0f) | ((p[1] <<  4) & 0xf0);
302		*ecc_code++ = ((p[1] >>  4) & 0x3f) | ((p[1] >> 10) & 0xc0);
303		*ecc_code++ =  (p[1] >> 18) & 0xff;
304	}
305
306	return 0;
307}
308
309/* Correct up to 4 bits in data we just read, using state left in the
310 * hardware plus the ecc_code computed when it was first written.
311 */
312static int nand_davinci_correct_4bit(struct nand_chip *chip, u_char *data,
313				     u_char *ecc_code, u_char *null)
314{
315	int i;
316	struct davinci_nand_info *info = to_davinci_nand(nand_to_mtd(chip));
317	unsigned short ecc10[8];
318	unsigned short *ecc16;
319	u32 syndrome[4];
320	u32 ecc_state;
321	unsigned num_errors, corrected;
322	unsigned long timeo;
323
324	/* Unpack ten bytes into eight 10 bit values.  We know we're
325	 * little-endian, and use type punning for less shifting/masking.
326	 */
327	if (WARN_ON(0x01 & (uintptr_t)ecc_code))
328		return -EINVAL;
329	ecc16 = (unsigned short *)ecc_code;
330
331	ecc10[0] =  (ecc16[0] >>  0) & 0x3ff;
332	ecc10[1] = ((ecc16[0] >> 10) & 0x3f) | ((ecc16[1] << 6) & 0x3c0);
333	ecc10[2] =  (ecc16[1] >>  4) & 0x3ff;
334	ecc10[3] = ((ecc16[1] >> 14) & 0x3)  | ((ecc16[2] << 2) & 0x3fc);
335	ecc10[4] =  (ecc16[2] >>  8)         | ((ecc16[3] << 8) & 0x300);
336	ecc10[5] =  (ecc16[3] >>  2) & 0x3ff;
337	ecc10[6] = ((ecc16[3] >> 12) & 0xf)  | ((ecc16[4] << 4) & 0x3f0);
338	ecc10[7] =  (ecc16[4] >>  6) & 0x3ff;
339
340	/* Tell ECC controller about the expected ECC codes. */
341	for (i = 7; i >= 0; i--)
342		davinci_nand_writel(info, NAND_4BIT_ECC_LOAD_OFFSET, ecc10[i]);
343
344	/* Allow time for syndrome calculation ... then read it.
345	 * A syndrome of all zeroes 0 means no detected errors.
346	 */
347	davinci_nand_readl(info, NANDFSR_OFFSET);
348	nand_davinci_readecc_4bit(info, syndrome);
349	if (!(syndrome[0] | syndrome[1] | syndrome[2] | syndrome[3]))
350		return 0;
351
352	/*
353	 * Clear any previous address calculation by doing a dummy read of an
354	 * error address register.
355	 */
356	davinci_nand_readl(info, NAND_ERR_ADD1_OFFSET);
357
358	/* Start address calculation, and wait for it to complete.
359	 * We _could_ start reading more data while this is working,
360	 * to speed up the overall page read.
361	 */
362	davinci_nand_writel(info, NANDFCR_OFFSET,
363			davinci_nand_readl(info, NANDFCR_OFFSET) | BIT(13));
364
365	/*
366	 * ECC_STATE field reads 0x3 (Error correction complete) immediately
367	 * after setting the 4BITECC_ADD_CALC_START bit. So if you immediately
368	 * begin trying to poll for the state, you may fall right out of your
369	 * loop without any of the correction calculations having taken place.
370	 * The recommendation from the hardware team is to initially delay as
371	 * long as ECC_STATE reads less than 4. After that, ECC HW has entered
372	 * correction state.
373	 */
374	timeo = jiffies + usecs_to_jiffies(100);
375	do {
376		ecc_state = (davinci_nand_readl(info,
377				NANDFSR_OFFSET) >> 8) & 0x0f;
378		cpu_relax();
379	} while ((ecc_state < 4) && time_before(jiffies, timeo));
380
381	for (;;) {
382		u32	fsr = davinci_nand_readl(info, NANDFSR_OFFSET);
383
384		switch ((fsr >> 8) & 0x0f) {
385		case 0:		/* no error, should not happen */
386			davinci_nand_readl(info, NAND_ERR_ERRVAL1_OFFSET);
387			return 0;
388		case 1:		/* five or more errors detected */
389			davinci_nand_readl(info, NAND_ERR_ERRVAL1_OFFSET);
390			return -EBADMSG;
391		case 2:		/* error addresses computed */
392		case 3:
393			num_errors = 1 + ((fsr >> 16) & 0x03);
394			goto correct;
395		default:	/* still working on it */
396			cpu_relax();
397			continue;
398		}
399	}
400
401correct:
402	/* correct each error */
403	for (i = 0, corrected = 0; i < num_errors; i++) {
404		int error_address, error_value;
405
406		if (i > 1) {
407			error_address = davinci_nand_readl(info,
408						NAND_ERR_ADD2_OFFSET);
409			error_value = davinci_nand_readl(info,
410						NAND_ERR_ERRVAL2_OFFSET);
411		} else {
412			error_address = davinci_nand_readl(info,
413						NAND_ERR_ADD1_OFFSET);
414			error_value = davinci_nand_readl(info,
415						NAND_ERR_ERRVAL1_OFFSET);
416		}
417
418		if (i & 1) {
419			error_address >>= 16;
420			error_value >>= 16;
421		}
422		error_address &= 0x3ff;
423		error_address = (512 + 7) - error_address;
424
425		if (error_address < 512) {
426			data[error_address] ^= error_value;
427			corrected++;
428		}
429	}
430
431	return corrected;
432}
433
434/*----------------------------------------------------------------------*/
435
436/* An ECC layout for using 4-bit ECC with small-page flash, storing
437 * ten ECC bytes plus the manufacturer's bad block marker byte, and
438 * and not overlapping the default BBT markers.
439 */
440static int hwecc4_ooblayout_small_ecc(struct mtd_info *mtd, int section,
441				      struct mtd_oob_region *oobregion)
442{
443	if (section > 2)
444		return -ERANGE;
445
446	if (!section) {
447		oobregion->offset = 0;
448		oobregion->length = 5;
449	} else if (section == 1) {
450		oobregion->offset = 6;
451		oobregion->length = 2;
452	} else {
453		oobregion->offset = 13;
454		oobregion->length = 3;
455	}
456
457	return 0;
458}
459
460static int hwecc4_ooblayout_small_free(struct mtd_info *mtd, int section,
461				       struct mtd_oob_region *oobregion)
462{
463	if (section > 1)
464		return -ERANGE;
465
466	if (!section) {
467		oobregion->offset = 8;
468		oobregion->length = 5;
469	} else {
470		oobregion->offset = 16;
471		oobregion->length = mtd->oobsize - 16;
472	}
473
474	return 0;
475}
476
477static const struct mtd_ooblayout_ops hwecc4_small_ooblayout_ops = {
478	.ecc = hwecc4_ooblayout_small_ecc,
479	.free = hwecc4_ooblayout_small_free,
480};
481
482#if defined(CONFIG_OF)
483static const struct of_device_id davinci_nand_of_match[] = {
484	{.compatible = "ti,davinci-nand", },
485	{.compatible = "ti,keystone-nand", },
486	{},
487};
488MODULE_DEVICE_TABLE(of, davinci_nand_of_match);
489
490static struct davinci_nand_pdata *
491nand_davinci_get_pdata(struct platform_device *pdev)
492{
493	if (!dev_get_platdata(&pdev->dev)) {
494		struct davinci_nand_pdata *pdata;
495		const char *mode;
496		u32 prop;
497
498		pdata =  devm_kzalloc(&pdev->dev,
499				sizeof(struct davinci_nand_pdata),
500				GFP_KERNEL);
501		pdev->dev.platform_data = pdata;
502		if (!pdata)
503			return ERR_PTR(-ENOMEM);
504		if (!device_property_read_u32(&pdev->dev,
505					      "ti,davinci-chipselect", &prop))
506			pdata->core_chipsel = prop;
507		else
508			return ERR_PTR(-EINVAL);
509
510		if (!device_property_read_u32(&pdev->dev,
511					      "ti,davinci-mask-ale", &prop))
512			pdata->mask_ale = prop;
513		if (!device_property_read_u32(&pdev->dev,
514					      "ti,davinci-mask-cle", &prop))
515			pdata->mask_cle = prop;
516		if (!device_property_read_u32(&pdev->dev,
517					      "ti,davinci-mask-chipsel", &prop))
518			pdata->mask_chipsel = prop;
519		if (!device_property_read_string(&pdev->dev,
520						 "ti,davinci-ecc-mode",
521						 &mode)) {
522			if (!strncmp("none", mode, 4))
523				pdata->engine_type = NAND_ECC_ENGINE_TYPE_NONE;
524			if (!strncmp("soft", mode, 4))
525				pdata->engine_type = NAND_ECC_ENGINE_TYPE_SOFT;
526			if (!strncmp("hw", mode, 2))
527				pdata->engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST;
528		}
529		if (!device_property_read_u32(&pdev->dev,
530					      "ti,davinci-ecc-bits", &prop))
531			pdata->ecc_bits = prop;
532
533		if (!device_property_read_u32(&pdev->dev,
534					      "ti,davinci-nand-buswidth",
535					      &prop) && prop == 16)
536			pdata->options |= NAND_BUSWIDTH_16;
537
538		if (device_property_read_bool(&pdev->dev,
539					      "ti,davinci-nand-use-bbt"))
540			pdata->bbt_options = NAND_BBT_USE_FLASH;
541
542		/*
543		 * Since kernel v4.8, this driver has been fixed to enable
544		 * use of 4-bit hardware ECC with subpages and verified on
545		 * TI's keystone EVMs (K2L, K2HK and K2E).
546		 * However, in the interest of not breaking systems using
547		 * existing UBI partitions, sub-page writes are not being
548		 * (re)enabled. If you want to use subpage writes on Keystone
549		 * platforms (i.e. do not have any existing UBI partitions),
550		 * then use "ti,davinci-nand" as the compatible in your
551		 * device-tree file.
552		 */
553		if (device_is_compatible(&pdev->dev, "ti,keystone-nand"))
 
554			pdata->options |= NAND_NO_SUBPAGE_WRITE;
 
555	}
556
557	return dev_get_platdata(&pdev->dev);
558}
559#else
560static struct davinci_nand_pdata *
561nand_davinci_get_pdata(struct platform_device *pdev)
562{
563	return dev_get_platdata(&pdev->dev);
564}
565#endif
566
567static int davinci_nand_attach_chip(struct nand_chip *chip)
568{
569	struct mtd_info *mtd = nand_to_mtd(chip);
570	struct davinci_nand_info *info = to_davinci_nand(mtd);
571	struct davinci_nand_pdata *pdata = nand_davinci_get_pdata(info->pdev);
572	int ret = 0;
573
574	if (IS_ERR(pdata))
575		return PTR_ERR(pdata);
576
577	/* Use board-specific ECC config */
578	chip->ecc.engine_type = pdata->engine_type;
579	chip->ecc.placement = pdata->ecc_placement;
580
581	switch (chip->ecc.engine_type) {
582	case NAND_ECC_ENGINE_TYPE_NONE:
583		pdata->ecc_bits = 0;
584		break;
585	case NAND_ECC_ENGINE_TYPE_SOFT:
586		pdata->ecc_bits = 0;
587		/*
588		 * This driver expects Hamming based ECC when engine_type is set
589		 * to NAND_ECC_ENGINE_TYPE_SOFT. Force ecc.algo to
590		 * NAND_ECC_ALGO_HAMMING to avoid adding an extra ->ecc_algo
591		 * field to davinci_nand_pdata.
592		 */
593		chip->ecc.algo = NAND_ECC_ALGO_HAMMING;
594		break;
595	case NAND_ECC_ENGINE_TYPE_ON_HOST:
596		if (pdata->ecc_bits == 4) {
597			int chunks = mtd->writesize / 512;
598
599			if (!chunks || mtd->oobsize < 16) {
600				dev_dbg(&info->pdev->dev, "too small\n");
601				return -EINVAL;
602			}
603
604			/*
605			 * No sanity checks:  CPUs must support this,
606			 * and the chips may not use NAND_BUSWIDTH_16.
607			 */
608
609			/* No sharing 4-bit hardware between chipselects yet */
610			spin_lock_irq(&davinci_nand_lock);
611			if (ecc4_busy)
612				ret = -EBUSY;
613			else
614				ecc4_busy = true;
615			spin_unlock_irq(&davinci_nand_lock);
616
617			if (ret == -EBUSY)
618				return ret;
619
620			chip->ecc.calculate = nand_davinci_calculate_4bit;
621			chip->ecc.correct = nand_davinci_correct_4bit;
622			chip->ecc.hwctl = nand_davinci_hwctl_4bit;
623			chip->ecc.bytes = 10;
624			chip->ecc.options = NAND_ECC_GENERIC_ERASED_CHECK;
625			chip->ecc.algo = NAND_ECC_ALGO_BCH;
626
627			/*
628			 * Update ECC layout if needed ... for 1-bit HW ECC, the
629			 * default is OK, but it allocates 6 bytes when only 3
630			 * are needed (for each 512 bytes). For 4-bit HW ECC,
631			 * the default is not usable: 10 bytes needed, not 6.
632			 *
633			 * For small page chips, preserve the manufacturer's
634			 * badblock marking data ... and make sure a flash BBT
635			 * table marker fits in the free bytes.
636			 */
637			if (chunks == 1) {
638				mtd_set_ooblayout(mtd,
639						  &hwecc4_small_ooblayout_ops);
640			} else if (chunks == 4 || chunks == 8) {
641				mtd_set_ooblayout(mtd,
642						  nand_get_large_page_ooblayout());
643				chip->ecc.read_page = nand_read_page_hwecc_oob_first;
644			} else {
645				return -EIO;
646			}
647		} else {
648			/* 1bit ecc hamming */
649			chip->ecc.calculate = nand_davinci_calculate_1bit;
650			chip->ecc.correct = nand_davinci_correct_1bit;
651			chip->ecc.hwctl = nand_davinci_hwctl_1bit;
652			chip->ecc.bytes = 3;
653			chip->ecc.algo = NAND_ECC_ALGO_HAMMING;
654		}
655		chip->ecc.size = 512;
656		chip->ecc.strength = pdata->ecc_bits;
657		break;
658	default:
659		return -EINVAL;
660	}
661
662	return ret;
663}
664
665static void nand_davinci_data_in(struct davinci_nand_info *info, void *buf,
666				 unsigned int len, bool force_8bit)
667{
668	u32 alignment = ((uintptr_t)buf | len) & 3;
669
670	if (force_8bit || (alignment & 1))
671		ioread8_rep(info->current_cs, buf, len);
672	else if (alignment & 3)
673		ioread16_rep(info->current_cs, buf, len >> 1);
674	else
675		ioread32_rep(info->current_cs, buf, len >> 2);
676}
677
678static void nand_davinci_data_out(struct davinci_nand_info *info,
679				  const void *buf, unsigned int len,
680				  bool force_8bit)
681{
682	u32 alignment = ((uintptr_t)buf | len) & 3;
683
684	if (force_8bit || (alignment & 1))
685		iowrite8_rep(info->current_cs, buf, len);
686	else if (alignment & 3)
687		iowrite16_rep(info->current_cs, buf, len >> 1);
688	else
689		iowrite32_rep(info->current_cs, buf, len >> 2);
690}
691
692static int davinci_nand_exec_instr(struct davinci_nand_info *info,
693				   const struct nand_op_instr *instr)
694{
695	unsigned int i, timeout_us;
696	u32 status;
697	int ret;
698
699	switch (instr->type) {
700	case NAND_OP_CMD_INSTR:
701		iowrite8(instr->ctx.cmd.opcode,
702			 info->current_cs + info->mask_cle);
703		break;
704
705	case NAND_OP_ADDR_INSTR:
706		for (i = 0; i < instr->ctx.addr.naddrs; i++) {
707			iowrite8(instr->ctx.addr.addrs[i],
708				 info->current_cs + info->mask_ale);
709		}
710		break;
711
712	case NAND_OP_DATA_IN_INSTR:
713		nand_davinci_data_in(info, instr->ctx.data.buf.in,
714				     instr->ctx.data.len,
715				     instr->ctx.data.force_8bit);
716		break;
717
718	case NAND_OP_DATA_OUT_INSTR:
719		nand_davinci_data_out(info, instr->ctx.data.buf.out,
720				      instr->ctx.data.len,
721				      instr->ctx.data.force_8bit);
722		break;
723
724	case NAND_OP_WAITRDY_INSTR:
725		timeout_us = instr->ctx.waitrdy.timeout_ms * 1000;
726		ret = readl_relaxed_poll_timeout(info->base + NANDFSR_OFFSET,
727						 status, status & BIT(0), 100,
728						 timeout_us);
729		if (ret)
730			return ret;
731
732		break;
733	}
734
735	if (instr->delay_ns) {
736		/* Dummy read to be sure that command is sent before ndelay starts */
737		davinci_nand_readl(info, 0);
738		ndelay(instr->delay_ns);
739	}
740
741	return 0;
742}
743
744static int davinci_nand_exec_op(struct nand_chip *chip,
745				const struct nand_operation *op,
746				bool check_only)
747{
748	struct davinci_nand_info *info = to_davinci_nand(nand_to_mtd(chip));
749	unsigned int i;
750
751	if (check_only)
752		return 0;
753
754	info->current_cs = info->vaddr + (op->cs * info->mask_chipsel);
755
756	for (i = 0; i < op->ninstrs; i++) {
757		int ret;
758
759		ret = davinci_nand_exec_instr(info, &op->instrs[i]);
760		if (ret)
761			return ret;
762	}
763
764	return 0;
765}
766
767static const struct nand_controller_ops davinci_nand_controller_ops = {
768	.attach_chip = davinci_nand_attach_chip,
769	.exec_op = davinci_nand_exec_op,
770};
771
772static int nand_davinci_probe(struct platform_device *pdev)
773{
774	struct davinci_nand_pdata	*pdata;
775	struct davinci_nand_info	*info;
776	struct resource			*res1;
777	struct resource			*res2;
778	void __iomem			*vaddr;
779	void __iomem			*base;
780	int				ret;
781	uint32_t			val;
782	struct mtd_info			*mtd;
783
784	pdata = nand_davinci_get_pdata(pdev);
785	if (IS_ERR(pdata))
786		return PTR_ERR(pdata);
787
788	/* insist on board-specific configuration */
789	if (!pdata)
790		return -ENODEV;
791
792	/* which external chipselect will we be managing? */
793	if (pdata->core_chipsel > 3)
794		return -ENODEV;
795
796	info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
797	if (!info)
798		return -ENOMEM;
799
800	platform_set_drvdata(pdev, info);
801
802	res1 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
803	res2 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
804	if (!res1 || !res2) {
805		dev_err(&pdev->dev, "resource missing\n");
806		return -EINVAL;
807	}
808
809	vaddr = devm_ioremap_resource(&pdev->dev, res1);
810	if (IS_ERR(vaddr))
811		return PTR_ERR(vaddr);
812
813	/*
814	 * This registers range is used to setup NAND settings. In case with
815	 * TI AEMIF driver, the same memory address range is requested already
816	 * by AEMIF, so we cannot request it twice, just ioremap.
817	 * The AEMIF and NAND drivers not use the same registers in this range.
818	 */
819	base = devm_ioremap(&pdev->dev, res2->start, resource_size(res2));
820	if (!base) {
821		dev_err(&pdev->dev, "ioremap failed for resource %pR\n", res2);
822		return -EADDRNOTAVAIL;
823	}
824
825	info->pdev		= pdev;
826	info->base		= base;
827	info->vaddr		= vaddr;
828
829	mtd			= nand_to_mtd(&info->chip);
830	mtd->dev.parent		= &pdev->dev;
831	nand_set_flash_node(&info->chip, pdev->dev.of_node);
832
833	/* options such as NAND_BBT_USE_FLASH */
834	info->chip.bbt_options	= pdata->bbt_options;
835	/* options such as 16-bit widths */
836	info->chip.options	= pdata->options;
837	info->chip.bbt_td	= pdata->bbt_td;
838	info->chip.bbt_md	= pdata->bbt_md;
 
839
840	info->current_cs	= info->vaddr;
841	info->core_chipsel	= pdata->core_chipsel;
842	info->mask_chipsel	= pdata->mask_chipsel;
843
844	/* use nandboot-capable ALE/CLE masks by default */
845	info->mask_ale		= pdata->mask_ale ? : MASK_ALE;
846	info->mask_cle		= pdata->mask_cle ? : MASK_CLE;
847
848	spin_lock_irq(&davinci_nand_lock);
849
850	/* put CSxNAND into NAND mode */
851	val = davinci_nand_readl(info, NANDFCR_OFFSET);
852	val |= BIT(info->core_chipsel);
853	davinci_nand_writel(info, NANDFCR_OFFSET, val);
854
855	spin_unlock_irq(&davinci_nand_lock);
856
857	/* Scan to find existence of the device(s) */
858	nand_controller_init(&info->controller);
859	info->controller.ops = &davinci_nand_controller_ops;
860	info->chip.controller = &info->controller;
861	ret = nand_scan(&info->chip, pdata->mask_chipsel ? 2 : 1);
862	if (ret < 0) {
863		dev_dbg(&pdev->dev, "no NAND chip(s) found\n");
864		return ret;
865	}
866
867	if (pdata->parts)
868		ret = mtd_device_register(mtd, pdata->parts, pdata->nr_parts);
869	else
870		ret = mtd_device_register(mtd, NULL, 0);
871	if (ret < 0)
872		goto err_cleanup_nand;
873
874	val = davinci_nand_readl(info, NRCSR_OFFSET);
875	dev_info(&pdev->dev, "controller rev. %d.%d\n",
876	       (val >> 8) & 0xff, val & 0xff);
877
878	return 0;
879
880err_cleanup_nand:
881	nand_cleanup(&info->chip);
882
883	return ret;
884}
885
886static void nand_davinci_remove(struct platform_device *pdev)
887{
888	struct davinci_nand_info *info = platform_get_drvdata(pdev);
889	struct nand_chip *chip = &info->chip;
890	int ret;
891
892	spin_lock_irq(&davinci_nand_lock);
893	if (chip->ecc.placement == NAND_ECC_PLACEMENT_INTERLEAVED)
894		ecc4_busy = false;
895	spin_unlock_irq(&davinci_nand_lock);
896
897	ret = mtd_device_unregister(nand_to_mtd(chip));
898	WARN_ON(ret);
899	nand_cleanup(chip);
900}
901
902static struct platform_driver nand_davinci_driver = {
903	.probe		= nand_davinci_probe,
904	.remove		= nand_davinci_remove,
905	.driver		= {
906		.name	= "davinci_nand",
907		.of_match_table = of_match_ptr(davinci_nand_of_match),
908	},
909};
910MODULE_ALIAS("platform:davinci_nand");
911
912module_platform_driver(nand_davinci_driver);
913
914MODULE_LICENSE("GPL");
915MODULE_AUTHOR("Texas Instruments");
916MODULE_DESCRIPTION("Davinci NAND flash driver");
917