davinci_nand.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. /*
  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. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/err.h>
  29. #include <linux/io.h>
  30. #include <linux/mtd/rawnand.h>
  31. #include <linux/mtd/partitions.h>
  32. #include <linux/slab.h>
  33. #include <linux/of_device.h>
  34. #include <linux/of.h>
  35. #include <linux/platform_data/mtd-davinci.h>
  36. #include <linux/platform_data/mtd-davinci-aemif.h>
  37. /*
  38. * This is a device driver for the NAND flash controller found on the
  39. * various DaVinci family chips. It handles up to four SoC chipselects,
  40. * and some flavors of secondary chipselect (e.g. based on A12) as used
  41. * with multichip packages.
  42. *
  43. * The 1-bit ECC hardware is supported, as well as the newer 4-bit ECC
  44. * available on chips like the DM355 and OMAP-L137 and needed with the
  45. * more error-prone MLC NAND chips.
  46. *
  47. * This driver assumes EM_WAIT connects all the NAND devices' RDY/nBUSY
  48. * outputs in a "wire-AND" configuration, with no per-chip signals.
  49. */
  50. struct davinci_nand_info {
  51. struct nand_chip chip;
  52. struct platform_device *pdev;
  53. bool is_readmode;
  54. void __iomem *base;
  55. void __iomem *vaddr;
  56. void __iomem *current_cs;
  57. uint32_t mask_chipsel;
  58. uint32_t mask_ale;
  59. uint32_t mask_cle;
  60. uint32_t core_chipsel;
  61. struct davinci_aemif_timing *timing;
  62. };
  63. static DEFINE_SPINLOCK(davinci_nand_lock);
  64. static bool ecc4_busy;
  65. static inline struct davinci_nand_info *to_davinci_nand(struct mtd_info *mtd)
  66. {
  67. return container_of(mtd_to_nand(mtd), struct davinci_nand_info, chip);
  68. }
  69. static inline unsigned int davinci_nand_readl(struct davinci_nand_info *info,
  70. int offset)
  71. {
  72. return __raw_readl(info->base + offset);
  73. }
  74. static inline void davinci_nand_writel(struct davinci_nand_info *info,
  75. int offset, unsigned long value)
  76. {
  77. __raw_writel(value, info->base + offset);
  78. }
  79. /*----------------------------------------------------------------------*/
  80. /*
  81. * Access to hardware control lines: ALE, CLE, secondary chipselect.
  82. */
  83. static void nand_davinci_hwcontrol(struct mtd_info *mtd, int cmd,
  84. unsigned int ctrl)
  85. {
  86. struct davinci_nand_info *info = to_davinci_nand(mtd);
  87. void __iomem *addr = info->current_cs;
  88. struct nand_chip *nand = mtd_to_nand(mtd);
  89. /* Did the control lines change? */
  90. if (ctrl & NAND_CTRL_CHANGE) {
  91. if ((ctrl & NAND_CTRL_CLE) == NAND_CTRL_CLE)
  92. addr += info->mask_cle;
  93. else if ((ctrl & NAND_CTRL_ALE) == NAND_CTRL_ALE)
  94. addr += info->mask_ale;
  95. nand->IO_ADDR_W = addr;
  96. }
  97. if (cmd != NAND_CMD_NONE)
  98. iowrite8(cmd, nand->IO_ADDR_W);
  99. }
  100. static void nand_davinci_select_chip(struct mtd_info *mtd, int chip)
  101. {
  102. struct davinci_nand_info *info = to_davinci_nand(mtd);
  103. info->current_cs = info->vaddr;
  104. /* maybe kick in a second chipselect */
  105. if (chip > 0)
  106. info->current_cs += info->mask_chipsel;
  107. info->chip.IO_ADDR_W = info->current_cs;
  108. info->chip.IO_ADDR_R = info->chip.IO_ADDR_W;
  109. }
  110. /*----------------------------------------------------------------------*/
  111. /*
  112. * 1-bit hardware ECC ... context maintained for each core chipselect
  113. */
  114. static inline uint32_t nand_davinci_readecc_1bit(struct mtd_info *mtd)
  115. {
  116. struct davinci_nand_info *info = to_davinci_nand(mtd);
  117. return davinci_nand_readl(info, NANDF1ECC_OFFSET
  118. + 4 * info->core_chipsel);
  119. }
  120. static void nand_davinci_hwctl_1bit(struct nand_chip *chip, int mode)
  121. {
  122. struct davinci_nand_info *info;
  123. uint32_t nandcfr;
  124. unsigned long flags;
  125. info = to_davinci_nand(nand_to_mtd(chip));
  126. /* Reset ECC hardware */
  127. nand_davinci_readecc_1bit(nand_to_mtd(chip));
  128. spin_lock_irqsave(&davinci_nand_lock, flags);
  129. /* Restart ECC hardware */
  130. nandcfr = davinci_nand_readl(info, NANDFCR_OFFSET);
  131. nandcfr |= BIT(8 + info->core_chipsel);
  132. davinci_nand_writel(info, NANDFCR_OFFSET, nandcfr);
  133. spin_unlock_irqrestore(&davinci_nand_lock, flags);
  134. }
  135. /*
  136. * Read hardware ECC value and pack into three bytes
  137. */
  138. static int nand_davinci_calculate_1bit(struct nand_chip *chip,
  139. const u_char *dat, u_char *ecc_code)
  140. {
  141. unsigned int ecc_val = nand_davinci_readecc_1bit(nand_to_mtd(chip));
  142. unsigned int ecc24 = (ecc_val & 0x0fff) | ((ecc_val & 0x0fff0000) >> 4);
  143. /* invert so that erased block ecc is correct */
  144. ecc24 = ~ecc24;
  145. ecc_code[0] = (u_char)(ecc24);
  146. ecc_code[1] = (u_char)(ecc24 >> 8);
  147. ecc_code[2] = (u_char)(ecc24 >> 16);
  148. return 0;
  149. }
  150. static int nand_davinci_correct_1bit(struct nand_chip *chip, u_char *dat,
  151. u_char *read_ecc, u_char *calc_ecc)
  152. {
  153. uint32_t eccNand = read_ecc[0] | (read_ecc[1] << 8) |
  154. (read_ecc[2] << 16);
  155. uint32_t eccCalc = calc_ecc[0] | (calc_ecc[1] << 8) |
  156. (calc_ecc[2] << 16);
  157. uint32_t diff = eccCalc ^ eccNand;
  158. if (diff) {
  159. if ((((diff >> 12) ^ diff) & 0xfff) == 0xfff) {
  160. /* Correctable error */
  161. if ((diff >> (12 + 3)) < chip->ecc.size) {
  162. dat[diff >> (12 + 3)] ^= BIT((diff >> 12) & 7);
  163. return 1;
  164. } else {
  165. return -EBADMSG;
  166. }
  167. } else if (!(diff & (diff - 1))) {
  168. /* Single bit ECC error in the ECC itself,
  169. * nothing to fix */
  170. return 1;
  171. } else {
  172. /* Uncorrectable error */
  173. return -EBADMSG;
  174. }
  175. }
  176. return 0;
  177. }
  178. /*----------------------------------------------------------------------*/
  179. /*
  180. * 4-bit hardware ECC ... context maintained over entire AEMIF
  181. *
  182. * This is a syndrome engine, but we avoid NAND_ECC_HW_SYNDROME
  183. * since that forces use of a problematic "infix OOB" layout.
  184. * Among other things, it trashes manufacturer bad block markers.
  185. * Also, and specific to this hardware, it ECC-protects the "prepad"
  186. * in the OOB ... while having ECC protection for parts of OOB would
  187. * seem useful, the current MTD stack sometimes wants to update the
  188. * OOB without recomputing ECC.
  189. */
  190. static void nand_davinci_hwctl_4bit(struct nand_chip *chip, int mode)
  191. {
  192. struct davinci_nand_info *info = to_davinci_nand(nand_to_mtd(chip));
  193. unsigned long flags;
  194. u32 val;
  195. /* Reset ECC hardware */
  196. davinci_nand_readl(info, NAND_4BIT_ECC1_OFFSET);
  197. spin_lock_irqsave(&davinci_nand_lock, flags);
  198. /* Start 4-bit ECC calculation for read/write */
  199. val = davinci_nand_readl(info, NANDFCR_OFFSET);
  200. val &= ~(0x03 << 4);
  201. val |= (info->core_chipsel << 4) | BIT(12);
  202. davinci_nand_writel(info, NANDFCR_OFFSET, val);
  203. info->is_readmode = (mode == NAND_ECC_READ);
  204. spin_unlock_irqrestore(&davinci_nand_lock, flags);
  205. }
  206. /* Read raw ECC code after writing to NAND. */
  207. static void
  208. nand_davinci_readecc_4bit(struct davinci_nand_info *info, u32 code[4])
  209. {
  210. const u32 mask = 0x03ff03ff;
  211. code[0] = davinci_nand_readl(info, NAND_4BIT_ECC1_OFFSET) & mask;
  212. code[1] = davinci_nand_readl(info, NAND_4BIT_ECC2_OFFSET) & mask;
  213. code[2] = davinci_nand_readl(info, NAND_4BIT_ECC3_OFFSET) & mask;
  214. code[3] = davinci_nand_readl(info, NAND_4BIT_ECC4_OFFSET) & mask;
  215. }
  216. /* Terminate read ECC; or return ECC (as bytes) of data written to NAND. */
  217. static int nand_davinci_calculate_4bit(struct nand_chip *chip,
  218. const u_char *dat, u_char *ecc_code)
  219. {
  220. struct davinci_nand_info *info = to_davinci_nand(nand_to_mtd(chip));
  221. u32 raw_ecc[4], *p;
  222. unsigned i;
  223. /* After a read, terminate ECC calculation by a dummy read
  224. * of some 4-bit ECC register. ECC covers everything that
  225. * was read; correct() just uses the hardware state, so
  226. * ecc_code is not needed.
  227. */
  228. if (info->is_readmode) {
  229. davinci_nand_readl(info, NAND_4BIT_ECC1_OFFSET);
  230. return 0;
  231. }
  232. /* Pack eight raw 10-bit ecc values into ten bytes, making
  233. * two passes which each convert four values (in upper and
  234. * lower halves of two 32-bit words) into five bytes. The
  235. * ROM boot loader uses this same packing scheme.
  236. */
  237. nand_davinci_readecc_4bit(info, raw_ecc);
  238. for (i = 0, p = raw_ecc; i < 2; i++, p += 2) {
  239. *ecc_code++ = p[0] & 0xff;
  240. *ecc_code++ = ((p[0] >> 8) & 0x03) | ((p[0] >> 14) & 0xfc);
  241. *ecc_code++ = ((p[0] >> 22) & 0x0f) | ((p[1] << 4) & 0xf0);
  242. *ecc_code++ = ((p[1] >> 4) & 0x3f) | ((p[1] >> 10) & 0xc0);
  243. *ecc_code++ = (p[1] >> 18) & 0xff;
  244. }
  245. return 0;
  246. }
  247. /* Correct up to 4 bits in data we just read, using state left in the
  248. * hardware plus the ecc_code computed when it was first written.
  249. */
  250. static int nand_davinci_correct_4bit(struct nand_chip *chip, u_char *data,
  251. u_char *ecc_code, u_char *null)
  252. {
  253. int i;
  254. struct davinci_nand_info *info = to_davinci_nand(nand_to_mtd(chip));
  255. unsigned short ecc10[8];
  256. unsigned short *ecc16;
  257. u32 syndrome[4];
  258. u32 ecc_state;
  259. unsigned num_errors, corrected;
  260. unsigned long timeo;
  261. /* Unpack ten bytes into eight 10 bit values. We know we're
  262. * little-endian, and use type punning for less shifting/masking.
  263. */
  264. if (WARN_ON(0x01 & (uintptr_t)ecc_code))
  265. return -EINVAL;
  266. ecc16 = (unsigned short *)ecc_code;
  267. ecc10[0] = (ecc16[0] >> 0) & 0x3ff;
  268. ecc10[1] = ((ecc16[0] >> 10) & 0x3f) | ((ecc16[1] << 6) & 0x3c0);
  269. ecc10[2] = (ecc16[1] >> 4) & 0x3ff;
  270. ecc10[3] = ((ecc16[1] >> 14) & 0x3) | ((ecc16[2] << 2) & 0x3fc);
  271. ecc10[4] = (ecc16[2] >> 8) | ((ecc16[3] << 8) & 0x300);
  272. ecc10[5] = (ecc16[3] >> 2) & 0x3ff;
  273. ecc10[6] = ((ecc16[3] >> 12) & 0xf) | ((ecc16[4] << 4) & 0x3f0);
  274. ecc10[7] = (ecc16[4] >> 6) & 0x3ff;
  275. /* Tell ECC controller about the expected ECC codes. */
  276. for (i = 7; i >= 0; i--)
  277. davinci_nand_writel(info, NAND_4BIT_ECC_LOAD_OFFSET, ecc10[i]);
  278. /* Allow time for syndrome calculation ... then read it.
  279. * A syndrome of all zeroes 0 means no detected errors.
  280. */
  281. davinci_nand_readl(info, NANDFSR_OFFSET);
  282. nand_davinci_readecc_4bit(info, syndrome);
  283. if (!(syndrome[0] | syndrome[1] | syndrome[2] | syndrome[3]))
  284. return 0;
  285. /*
  286. * Clear any previous address calculation by doing a dummy read of an
  287. * error address register.
  288. */
  289. davinci_nand_readl(info, NAND_ERR_ADD1_OFFSET);
  290. /* Start address calculation, and wait for it to complete.
  291. * We _could_ start reading more data while this is working,
  292. * to speed up the overall page read.
  293. */
  294. davinci_nand_writel(info, NANDFCR_OFFSET,
  295. davinci_nand_readl(info, NANDFCR_OFFSET) | BIT(13));
  296. /*
  297. * ECC_STATE field reads 0x3 (Error correction complete) immediately
  298. * after setting the 4BITECC_ADD_CALC_START bit. So if you immediately
  299. * begin trying to poll for the state, you may fall right out of your
  300. * loop without any of the correction calculations having taken place.
  301. * The recommendation from the hardware team is to initially delay as
  302. * long as ECC_STATE reads less than 4. After that, ECC HW has entered
  303. * correction state.
  304. */
  305. timeo = jiffies + usecs_to_jiffies(100);
  306. do {
  307. ecc_state = (davinci_nand_readl(info,
  308. NANDFSR_OFFSET) >> 8) & 0x0f;
  309. cpu_relax();
  310. } while ((ecc_state < 4) && time_before(jiffies, timeo));
  311. for (;;) {
  312. u32 fsr = davinci_nand_readl(info, NANDFSR_OFFSET);
  313. switch ((fsr >> 8) & 0x0f) {
  314. case 0: /* no error, should not happen */
  315. davinci_nand_readl(info, NAND_ERR_ERRVAL1_OFFSET);
  316. return 0;
  317. case 1: /* five or more errors detected */
  318. davinci_nand_readl(info, NAND_ERR_ERRVAL1_OFFSET);
  319. return -EBADMSG;
  320. case 2: /* error addresses computed */
  321. case 3:
  322. num_errors = 1 + ((fsr >> 16) & 0x03);
  323. goto correct;
  324. default: /* still working on it */
  325. cpu_relax();
  326. continue;
  327. }
  328. }
  329. correct:
  330. /* correct each error */
  331. for (i = 0, corrected = 0; i < num_errors; i++) {
  332. int error_address, error_value;
  333. if (i > 1) {
  334. error_address = davinci_nand_readl(info,
  335. NAND_ERR_ADD2_OFFSET);
  336. error_value = davinci_nand_readl(info,
  337. NAND_ERR_ERRVAL2_OFFSET);
  338. } else {
  339. error_address = davinci_nand_readl(info,
  340. NAND_ERR_ADD1_OFFSET);
  341. error_value = davinci_nand_readl(info,
  342. NAND_ERR_ERRVAL1_OFFSET);
  343. }
  344. if (i & 1) {
  345. error_address >>= 16;
  346. error_value >>= 16;
  347. }
  348. error_address &= 0x3ff;
  349. error_address = (512 + 7) - error_address;
  350. if (error_address < 512) {
  351. data[error_address] ^= error_value;
  352. corrected++;
  353. }
  354. }
  355. return corrected;
  356. }
  357. /*----------------------------------------------------------------------*/
  358. /*
  359. * NOTE: NAND boot requires ALE == EM_A[1], CLE == EM_A[2], so that's
  360. * how these chips are normally wired. This translates to both 8 and 16
  361. * bit busses using ALE == BIT(3) in byte addresses, and CLE == BIT(4).
  362. *
  363. * For now we assume that configuration, or any other one which ignores
  364. * the two LSBs for NAND access ... so we can issue 32-bit reads/writes
  365. * and have that transparently morphed into multiple NAND operations.
  366. */
  367. static void nand_davinci_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  368. {
  369. struct nand_chip *chip = mtd_to_nand(mtd);
  370. if ((0x03 & ((uintptr_t)buf)) == 0 && (0x03 & len) == 0)
  371. ioread32_rep(chip->IO_ADDR_R, buf, len >> 2);
  372. else if ((0x01 & ((uintptr_t)buf)) == 0 && (0x01 & len) == 0)
  373. ioread16_rep(chip->IO_ADDR_R, buf, len >> 1);
  374. else
  375. ioread8_rep(chip->IO_ADDR_R, buf, len);
  376. }
  377. static void nand_davinci_write_buf(struct mtd_info *mtd,
  378. const uint8_t *buf, int len)
  379. {
  380. struct nand_chip *chip = mtd_to_nand(mtd);
  381. if ((0x03 & ((uintptr_t)buf)) == 0 && (0x03 & len) == 0)
  382. iowrite32_rep(chip->IO_ADDR_R, buf, len >> 2);
  383. else if ((0x01 & ((uintptr_t)buf)) == 0 && (0x01 & len) == 0)
  384. iowrite16_rep(chip->IO_ADDR_R, buf, len >> 1);
  385. else
  386. iowrite8_rep(chip->IO_ADDR_R, buf, len);
  387. }
  388. /*
  389. * Check hardware register for wait status. Returns 1 if device is ready,
  390. * 0 if it is still busy.
  391. */
  392. static int nand_davinci_dev_ready(struct mtd_info *mtd)
  393. {
  394. struct davinci_nand_info *info = to_davinci_nand(mtd);
  395. return davinci_nand_readl(info, NANDFSR_OFFSET) & BIT(0);
  396. }
  397. /*----------------------------------------------------------------------*/
  398. /* An ECC layout for using 4-bit ECC with small-page flash, storing
  399. * ten ECC bytes plus the manufacturer's bad block marker byte, and
  400. * and not overlapping the default BBT markers.
  401. */
  402. static int hwecc4_ooblayout_small_ecc(struct mtd_info *mtd, int section,
  403. struct mtd_oob_region *oobregion)
  404. {
  405. if (section > 2)
  406. return -ERANGE;
  407. if (!section) {
  408. oobregion->offset = 0;
  409. oobregion->length = 5;
  410. } else if (section == 1) {
  411. oobregion->offset = 6;
  412. oobregion->length = 2;
  413. } else {
  414. oobregion->offset = 13;
  415. oobregion->length = 3;
  416. }
  417. return 0;
  418. }
  419. static int hwecc4_ooblayout_small_free(struct mtd_info *mtd, int section,
  420. struct mtd_oob_region *oobregion)
  421. {
  422. if (section > 1)
  423. return -ERANGE;
  424. if (!section) {
  425. oobregion->offset = 8;
  426. oobregion->length = 5;
  427. } else {
  428. oobregion->offset = 16;
  429. oobregion->length = mtd->oobsize - 16;
  430. }
  431. return 0;
  432. }
  433. static const struct mtd_ooblayout_ops hwecc4_small_ooblayout_ops = {
  434. .ecc = hwecc4_ooblayout_small_ecc,
  435. .free = hwecc4_ooblayout_small_free,
  436. };
  437. #if defined(CONFIG_OF)
  438. static const struct of_device_id davinci_nand_of_match[] = {
  439. {.compatible = "ti,davinci-nand", },
  440. {.compatible = "ti,keystone-nand", },
  441. {},
  442. };
  443. MODULE_DEVICE_TABLE(of, davinci_nand_of_match);
  444. static struct davinci_nand_pdata
  445. *nand_davinci_get_pdata(struct platform_device *pdev)
  446. {
  447. if (!dev_get_platdata(&pdev->dev) && pdev->dev.of_node) {
  448. struct davinci_nand_pdata *pdata;
  449. const char *mode;
  450. u32 prop;
  451. pdata = devm_kzalloc(&pdev->dev,
  452. sizeof(struct davinci_nand_pdata),
  453. GFP_KERNEL);
  454. pdev->dev.platform_data = pdata;
  455. if (!pdata)
  456. return ERR_PTR(-ENOMEM);
  457. if (!of_property_read_u32(pdev->dev.of_node,
  458. "ti,davinci-chipselect", &prop))
  459. pdata->core_chipsel = prop;
  460. else
  461. return ERR_PTR(-EINVAL);
  462. if (!of_property_read_u32(pdev->dev.of_node,
  463. "ti,davinci-mask-ale", &prop))
  464. pdata->mask_ale = prop;
  465. if (!of_property_read_u32(pdev->dev.of_node,
  466. "ti,davinci-mask-cle", &prop))
  467. pdata->mask_cle = prop;
  468. if (!of_property_read_u32(pdev->dev.of_node,
  469. "ti,davinci-mask-chipsel", &prop))
  470. pdata->mask_chipsel = prop;
  471. if (!of_property_read_string(pdev->dev.of_node,
  472. "ti,davinci-ecc-mode", &mode)) {
  473. if (!strncmp("none", mode, 4))
  474. pdata->ecc_mode = NAND_ECC_NONE;
  475. if (!strncmp("soft", mode, 4))
  476. pdata->ecc_mode = NAND_ECC_SOFT;
  477. if (!strncmp("hw", mode, 2))
  478. pdata->ecc_mode = NAND_ECC_HW;
  479. }
  480. if (!of_property_read_u32(pdev->dev.of_node,
  481. "ti,davinci-ecc-bits", &prop))
  482. pdata->ecc_bits = prop;
  483. if (!of_property_read_u32(pdev->dev.of_node,
  484. "ti,davinci-nand-buswidth", &prop) && prop == 16)
  485. pdata->options |= NAND_BUSWIDTH_16;
  486. if (of_property_read_bool(pdev->dev.of_node,
  487. "ti,davinci-nand-use-bbt"))
  488. pdata->bbt_options = NAND_BBT_USE_FLASH;
  489. /*
  490. * Since kernel v4.8, this driver has been fixed to enable
  491. * use of 4-bit hardware ECC with subpages and verified on
  492. * TI's keystone EVMs (K2L, K2HK and K2E).
  493. * However, in the interest of not breaking systems using
  494. * existing UBI partitions, sub-page writes are not being
  495. * (re)enabled. If you want to use subpage writes on Keystone
  496. * platforms (i.e. do not have any existing UBI partitions),
  497. * then use "ti,davinci-nand" as the compatible in your
  498. * device-tree file.
  499. */
  500. if (of_device_is_compatible(pdev->dev.of_node,
  501. "ti,keystone-nand")) {
  502. pdata->options |= NAND_NO_SUBPAGE_WRITE;
  503. }
  504. }
  505. return dev_get_platdata(&pdev->dev);
  506. }
  507. #else
  508. static struct davinci_nand_pdata
  509. *nand_davinci_get_pdata(struct platform_device *pdev)
  510. {
  511. return dev_get_platdata(&pdev->dev);
  512. }
  513. #endif
  514. static int davinci_nand_attach_chip(struct nand_chip *chip)
  515. {
  516. struct mtd_info *mtd = nand_to_mtd(chip);
  517. struct davinci_nand_info *info = to_davinci_nand(mtd);
  518. struct davinci_nand_pdata *pdata = nand_davinci_get_pdata(info->pdev);
  519. int ret = 0;
  520. if (IS_ERR(pdata))
  521. return PTR_ERR(pdata);
  522. switch (info->chip.ecc.mode) {
  523. case NAND_ECC_NONE:
  524. pdata->ecc_bits = 0;
  525. break;
  526. case NAND_ECC_SOFT:
  527. pdata->ecc_bits = 0;
  528. /*
  529. * This driver expects Hamming based ECC when ecc_mode is set
  530. * to NAND_ECC_SOFT. Force ecc.algo to NAND_ECC_HAMMING to
  531. * avoid adding an extra ->ecc_algo field to
  532. * davinci_nand_pdata.
  533. */
  534. info->chip.ecc.algo = NAND_ECC_HAMMING;
  535. break;
  536. case NAND_ECC_HW:
  537. if (pdata->ecc_bits == 4) {
  538. /*
  539. * No sanity checks: CPUs must support this,
  540. * and the chips may not use NAND_BUSWIDTH_16.
  541. */
  542. /* No sharing 4-bit hardware between chipselects yet */
  543. spin_lock_irq(&davinci_nand_lock);
  544. if (ecc4_busy)
  545. ret = -EBUSY;
  546. else
  547. ecc4_busy = true;
  548. spin_unlock_irq(&davinci_nand_lock);
  549. if (ret == -EBUSY)
  550. return ret;
  551. info->chip.ecc.calculate = nand_davinci_calculate_4bit;
  552. info->chip.ecc.correct = nand_davinci_correct_4bit;
  553. info->chip.ecc.hwctl = nand_davinci_hwctl_4bit;
  554. info->chip.ecc.bytes = 10;
  555. info->chip.ecc.options = NAND_ECC_GENERIC_ERASED_CHECK;
  556. info->chip.ecc.algo = NAND_ECC_BCH;
  557. } else {
  558. /* 1bit ecc hamming */
  559. info->chip.ecc.calculate = nand_davinci_calculate_1bit;
  560. info->chip.ecc.correct = nand_davinci_correct_1bit;
  561. info->chip.ecc.hwctl = nand_davinci_hwctl_1bit;
  562. info->chip.ecc.bytes = 3;
  563. info->chip.ecc.algo = NAND_ECC_HAMMING;
  564. }
  565. info->chip.ecc.size = 512;
  566. info->chip.ecc.strength = pdata->ecc_bits;
  567. break;
  568. default:
  569. return -EINVAL;
  570. }
  571. /*
  572. * Update ECC layout if needed ... for 1-bit HW ECC, the default
  573. * is OK, but it allocates 6 bytes when only 3 are needed (for
  574. * each 512 bytes). For the 4-bit HW ECC, that default is not
  575. * usable: 10 bytes are needed, not 6.
  576. */
  577. if (pdata->ecc_bits == 4) {
  578. int chunks = mtd->writesize / 512;
  579. if (!chunks || mtd->oobsize < 16) {
  580. dev_dbg(&info->pdev->dev, "too small\n");
  581. return -EINVAL;
  582. }
  583. /* For small page chips, preserve the manufacturer's
  584. * badblock marking data ... and make sure a flash BBT
  585. * table marker fits in the free bytes.
  586. */
  587. if (chunks == 1) {
  588. mtd_set_ooblayout(mtd, &hwecc4_small_ooblayout_ops);
  589. } else if (chunks == 4 || chunks == 8) {
  590. mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
  591. info->chip.ecc.mode = NAND_ECC_HW_OOB_FIRST;
  592. } else {
  593. return -EIO;
  594. }
  595. }
  596. return ret;
  597. }
  598. static const struct nand_controller_ops davinci_nand_controller_ops = {
  599. .attach_chip = davinci_nand_attach_chip,
  600. };
  601. static int nand_davinci_probe(struct platform_device *pdev)
  602. {
  603. struct davinci_nand_pdata *pdata;
  604. struct davinci_nand_info *info;
  605. struct resource *res1;
  606. struct resource *res2;
  607. void __iomem *vaddr;
  608. void __iomem *base;
  609. int ret;
  610. uint32_t val;
  611. struct mtd_info *mtd;
  612. pdata = nand_davinci_get_pdata(pdev);
  613. if (IS_ERR(pdata))
  614. return PTR_ERR(pdata);
  615. /* insist on board-specific configuration */
  616. if (!pdata)
  617. return -ENODEV;
  618. /* which external chipselect will we be managing? */
  619. if (pdata->core_chipsel < 0 || pdata->core_chipsel > 3)
  620. return -ENODEV;
  621. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  622. if (!info)
  623. return -ENOMEM;
  624. platform_set_drvdata(pdev, info);
  625. res1 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  626. res2 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  627. if (!res1 || !res2) {
  628. dev_err(&pdev->dev, "resource missing\n");
  629. return -EINVAL;
  630. }
  631. vaddr = devm_ioremap_resource(&pdev->dev, res1);
  632. if (IS_ERR(vaddr))
  633. return PTR_ERR(vaddr);
  634. /*
  635. * This registers range is used to setup NAND settings. In case with
  636. * TI AEMIF driver, the same memory address range is requested already
  637. * by AEMIF, so we cannot request it twice, just ioremap.
  638. * The AEMIF and NAND drivers not use the same registers in this range.
  639. */
  640. base = devm_ioremap(&pdev->dev, res2->start, resource_size(res2));
  641. if (!base) {
  642. dev_err(&pdev->dev, "ioremap failed for resource %pR\n", res2);
  643. return -EADDRNOTAVAIL;
  644. }
  645. info->pdev = pdev;
  646. info->base = base;
  647. info->vaddr = vaddr;
  648. mtd = nand_to_mtd(&info->chip);
  649. mtd->dev.parent = &pdev->dev;
  650. nand_set_flash_node(&info->chip, pdev->dev.of_node);
  651. info->chip.IO_ADDR_R = vaddr;
  652. info->chip.IO_ADDR_W = vaddr;
  653. info->chip.chip_delay = 0;
  654. info->chip.select_chip = nand_davinci_select_chip;
  655. /* options such as NAND_BBT_USE_FLASH */
  656. info->chip.bbt_options = pdata->bbt_options;
  657. /* options such as 16-bit widths */
  658. info->chip.options = pdata->options;
  659. info->chip.bbt_td = pdata->bbt_td;
  660. info->chip.bbt_md = pdata->bbt_md;
  661. info->timing = pdata->timing;
  662. info->current_cs = info->vaddr;
  663. info->core_chipsel = pdata->core_chipsel;
  664. info->mask_chipsel = pdata->mask_chipsel;
  665. /* use nandboot-capable ALE/CLE masks by default */
  666. info->mask_ale = pdata->mask_ale ? : MASK_ALE;
  667. info->mask_cle = pdata->mask_cle ? : MASK_CLE;
  668. /* Set address of hardware control function */
  669. info->chip.cmd_ctrl = nand_davinci_hwcontrol;
  670. info->chip.dev_ready = nand_davinci_dev_ready;
  671. /* Speed up buffer I/O */
  672. info->chip.read_buf = nand_davinci_read_buf;
  673. info->chip.write_buf = nand_davinci_write_buf;
  674. /* Use board-specific ECC config */
  675. info->chip.ecc.mode = pdata->ecc_mode;
  676. spin_lock_irq(&davinci_nand_lock);
  677. /* put CSxNAND into NAND mode */
  678. val = davinci_nand_readl(info, NANDFCR_OFFSET);
  679. val |= BIT(info->core_chipsel);
  680. davinci_nand_writel(info, NANDFCR_OFFSET, val);
  681. spin_unlock_irq(&davinci_nand_lock);
  682. /* Scan to find existence of the device(s) */
  683. info->chip.dummy_controller.ops = &davinci_nand_controller_ops;
  684. ret = nand_scan(&info->chip, pdata->mask_chipsel ? 2 : 1);
  685. if (ret < 0) {
  686. dev_dbg(&pdev->dev, "no NAND chip(s) found\n");
  687. return ret;
  688. }
  689. if (pdata->parts)
  690. ret = mtd_device_register(mtd, pdata->parts, pdata->nr_parts);
  691. else
  692. ret = mtd_device_register(mtd, NULL, 0);
  693. if (ret < 0)
  694. goto err_cleanup_nand;
  695. val = davinci_nand_readl(info, NRCSR_OFFSET);
  696. dev_info(&pdev->dev, "controller rev. %d.%d\n",
  697. (val >> 8) & 0xff, val & 0xff);
  698. return 0;
  699. err_cleanup_nand:
  700. nand_cleanup(&info->chip);
  701. return ret;
  702. }
  703. static int nand_davinci_remove(struct platform_device *pdev)
  704. {
  705. struct davinci_nand_info *info = platform_get_drvdata(pdev);
  706. spin_lock_irq(&davinci_nand_lock);
  707. if (info->chip.ecc.mode == NAND_ECC_HW_SYNDROME)
  708. ecc4_busy = false;
  709. spin_unlock_irq(&davinci_nand_lock);
  710. nand_release(&info->chip);
  711. return 0;
  712. }
  713. static struct platform_driver nand_davinci_driver = {
  714. .probe = nand_davinci_probe,
  715. .remove = nand_davinci_remove,
  716. .driver = {
  717. .name = "davinci_nand",
  718. .of_match_table = of_match_ptr(davinci_nand_of_match),
  719. },
  720. };
  721. MODULE_ALIAS("platform:davinci_nand");
  722. module_platform_driver(nand_davinci_driver);
  723. MODULE_LICENSE("GPL");
  724. MODULE_AUTHOR("Texas Instruments");
  725. MODULE_DESCRIPTION("Davinci NAND flash driver");