davinci_nand.c 24 KB

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