davinci_nand.c 24 KB

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