davinci_nand.c 25 KB

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