davinci_nand.c 24 KB

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