davinci_nand.c 25 KB

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