davinci_nand.c 25 KB

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