davinci_nand.c 24 KB

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