davinci_nand.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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. /* Start address calculation, and wait for it to complete.
  291. * We _could_ start reading more data while this is working,
  292. * to speed up the overall page read.
  293. */
  294. davinci_nand_writel(info, NANDFCR_OFFSET,
  295. davinci_nand_readl(info, NANDFCR_OFFSET) | BIT(13));
  296. for (;;) {
  297. u32 fsr = davinci_nand_readl(info, NANDFSR_OFFSET);
  298. switch ((fsr >> 8) & 0x0f) {
  299. case 0: /* no error, should not happen */
  300. return 0;
  301. case 1: /* five or more errors detected */
  302. return -EIO;
  303. case 2: /* error addresses computed */
  304. case 3:
  305. num_errors = 1 + ((fsr >> 16) & 0x03);
  306. goto correct;
  307. default: /* still working on it */
  308. cpu_relax();
  309. continue;
  310. }
  311. }
  312. correct:
  313. /* correct each error */
  314. for (i = 0, corrected = 0; i < num_errors; i++) {
  315. int error_address, error_value;
  316. if (i > 1) {
  317. error_address = davinci_nand_readl(info,
  318. NAND_ERR_ADD2_OFFSET);
  319. error_value = davinci_nand_readl(info,
  320. NAND_ERR_ERRVAL2_OFFSET);
  321. } else {
  322. error_address = davinci_nand_readl(info,
  323. NAND_ERR_ADD1_OFFSET);
  324. error_value = davinci_nand_readl(info,
  325. NAND_ERR_ERRVAL1_OFFSET);
  326. }
  327. if (i & 1) {
  328. error_address >>= 16;
  329. error_value >>= 16;
  330. }
  331. error_address &= 0x3ff;
  332. error_address = (512 + 7) - error_address;
  333. if (error_address < 512) {
  334. data[error_address] ^= error_value;
  335. corrected++;
  336. }
  337. }
  338. return corrected;
  339. }
  340. /*----------------------------------------------------------------------*/
  341. /*
  342. * NOTE: NAND boot requires ALE == EM_A[1], CLE == EM_A[2], so that's
  343. * how these chips are normally wired. This translates to both 8 and 16
  344. * bit busses using ALE == BIT(3) in byte addresses, and CLE == BIT(4).
  345. *
  346. * For now we assume that configuration, or any other one which ignores
  347. * the two LSBs for NAND access ... so we can issue 32-bit reads/writes
  348. * and have that transparently morphed into multiple NAND operations.
  349. */
  350. static void nand_davinci_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  351. {
  352. struct nand_chip *chip = mtd->priv;
  353. if ((0x03 & ((unsigned)buf)) == 0 && (0x03 & len) == 0)
  354. ioread32_rep(chip->IO_ADDR_R, buf, len >> 2);
  355. else if ((0x01 & ((unsigned)buf)) == 0 && (0x01 & len) == 0)
  356. ioread16_rep(chip->IO_ADDR_R, buf, len >> 1);
  357. else
  358. ioread8_rep(chip->IO_ADDR_R, buf, len);
  359. }
  360. static void nand_davinci_write_buf(struct mtd_info *mtd,
  361. const uint8_t *buf, int len)
  362. {
  363. struct nand_chip *chip = mtd->priv;
  364. if ((0x03 & ((unsigned)buf)) == 0 && (0x03 & len) == 0)
  365. iowrite32_rep(chip->IO_ADDR_R, buf, len >> 2);
  366. else if ((0x01 & ((unsigned)buf)) == 0 && (0x01 & len) == 0)
  367. iowrite16_rep(chip->IO_ADDR_R, buf, len >> 1);
  368. else
  369. iowrite8_rep(chip->IO_ADDR_R, buf, len);
  370. }
  371. /*
  372. * Check hardware register for wait status. Returns 1 if device is ready,
  373. * 0 if it is still busy.
  374. */
  375. static int nand_davinci_dev_ready(struct mtd_info *mtd)
  376. {
  377. struct davinci_nand_info *info = to_davinci_nand(mtd);
  378. return davinci_nand_readl(info, NANDFSR_OFFSET) & BIT(0);
  379. }
  380. static void __init nand_dm6446evm_flash_init(struct davinci_nand_info *info)
  381. {
  382. uint32_t regval, a1cr;
  383. /*
  384. * NAND FLASH timings @ PLL1 == 459 MHz
  385. * - AEMIF.CLK freq = PLL1/6 = 459/6 = 76.5 MHz
  386. * - AEMIF.CLK period = 1/76.5 MHz = 13.1 ns
  387. */
  388. regval = 0
  389. | (0 << 31) /* selectStrobe */
  390. | (0 << 30) /* extWait (never with NAND) */
  391. | (1 << 26) /* writeSetup 10 ns */
  392. | (3 << 20) /* writeStrobe 40 ns */
  393. | (1 << 17) /* writeHold 10 ns */
  394. | (0 << 13) /* readSetup 10 ns */
  395. | (3 << 7) /* readStrobe 60 ns */
  396. | (0 << 4) /* readHold 10 ns */
  397. | (3 << 2) /* turnAround ?? ns */
  398. | (0 << 0) /* asyncSize 8-bit bus */
  399. ;
  400. a1cr = davinci_nand_readl(info, A1CR_OFFSET);
  401. if (a1cr != regval) {
  402. dev_dbg(info->dev, "Warning: NAND config: Set A1CR " \
  403. "reg to 0x%08x, was 0x%08x, should be done by " \
  404. "bootloader.\n", regval, a1cr);
  405. davinci_nand_writel(info, A1CR_OFFSET, regval);
  406. }
  407. }
  408. /*----------------------------------------------------------------------*/
  409. /* An ECC layout for using 4-bit ECC with small-page flash, storing
  410. * ten ECC bytes plus the manufacturer's bad block marker byte, and
  411. * and not overlapping the default BBT markers.
  412. */
  413. static struct nand_ecclayout hwecc4_small __initconst = {
  414. .eccbytes = 10,
  415. .eccpos = { 0, 1, 2, 3, 4,
  416. /* offset 5 holds the badblock marker */
  417. 6, 7,
  418. 13, 14, 15, },
  419. .oobfree = {
  420. {.offset = 8, .length = 5, },
  421. {.offset = 16, },
  422. },
  423. };
  424. static int __init nand_davinci_probe(struct platform_device *pdev)
  425. {
  426. struct davinci_nand_pdata *pdata = pdev->dev.platform_data;
  427. struct davinci_nand_info *info;
  428. struct resource *res1;
  429. struct resource *res2;
  430. void __iomem *vaddr;
  431. void __iomem *base;
  432. int ret;
  433. uint32_t val;
  434. nand_ecc_modes_t ecc_mode;
  435. /* insist on board-specific configuration */
  436. if (!pdata)
  437. return -ENODEV;
  438. /* which external chipselect will we be managing? */
  439. if (pdev->id < 0 || pdev->id > 3)
  440. return -ENODEV;
  441. info = kzalloc(sizeof(*info), GFP_KERNEL);
  442. if (!info) {
  443. dev_err(&pdev->dev, "unable to allocate memory\n");
  444. ret = -ENOMEM;
  445. goto err_nomem;
  446. }
  447. platform_set_drvdata(pdev, info);
  448. res1 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  449. res2 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  450. if (!res1 || !res2) {
  451. dev_err(&pdev->dev, "resource missing\n");
  452. ret = -EINVAL;
  453. goto err_nomem;
  454. }
  455. vaddr = ioremap(res1->start, res1->end - res1->start);
  456. base = ioremap(res2->start, res2->end - res2->start);
  457. if (!vaddr || !base) {
  458. dev_err(&pdev->dev, "ioremap failed\n");
  459. ret = -EINVAL;
  460. goto err_ioremap;
  461. }
  462. info->dev = &pdev->dev;
  463. info->base = base;
  464. info->vaddr = vaddr;
  465. info->mtd.priv = &info->chip;
  466. info->mtd.name = dev_name(&pdev->dev);
  467. info->mtd.owner = THIS_MODULE;
  468. info->mtd.dev.parent = &pdev->dev;
  469. info->chip.IO_ADDR_R = vaddr;
  470. info->chip.IO_ADDR_W = vaddr;
  471. info->chip.chip_delay = 0;
  472. info->chip.select_chip = nand_davinci_select_chip;
  473. /* options such as NAND_USE_FLASH_BBT or 16-bit widths */
  474. info->chip.options = pdata->options;
  475. info->ioaddr = (uint32_t __force) vaddr;
  476. info->current_cs = info->ioaddr;
  477. info->core_chipsel = pdev->id;
  478. info->mask_chipsel = pdata->mask_chipsel;
  479. /* use nandboot-capable ALE/CLE masks by default */
  480. info->mask_ale = pdata->mask_cle ? : MASK_ALE;
  481. info->mask_cle = pdata->mask_cle ? : MASK_CLE;
  482. /* Set address of hardware control function */
  483. info->chip.cmd_ctrl = nand_davinci_hwcontrol;
  484. info->chip.dev_ready = nand_davinci_dev_ready;
  485. /* Speed up buffer I/O */
  486. info->chip.read_buf = nand_davinci_read_buf;
  487. info->chip.write_buf = nand_davinci_write_buf;
  488. /* Use board-specific ECC config */
  489. ecc_mode = pdata->ecc_mode;
  490. ret = -EINVAL;
  491. switch (ecc_mode) {
  492. case NAND_ECC_NONE:
  493. case NAND_ECC_SOFT:
  494. pdata->ecc_bits = 0;
  495. break;
  496. case NAND_ECC_HW:
  497. if (pdata->ecc_bits == 4) {
  498. /* No sanity checks: CPUs must support this,
  499. * and the chips may not use NAND_BUSWIDTH_16.
  500. */
  501. /* No sharing 4-bit hardware between chipselects yet */
  502. spin_lock_irq(&davinci_nand_lock);
  503. if (ecc4_busy)
  504. ret = -EBUSY;
  505. else
  506. ecc4_busy = true;
  507. spin_unlock_irq(&davinci_nand_lock);
  508. if (ret == -EBUSY)
  509. goto err_ecc;
  510. info->chip.ecc.calculate = nand_davinci_calculate_4bit;
  511. info->chip.ecc.correct = nand_davinci_correct_4bit;
  512. info->chip.ecc.hwctl = nand_davinci_hwctl_4bit;
  513. info->chip.ecc.bytes = 10;
  514. } else {
  515. info->chip.ecc.calculate = nand_davinci_calculate_1bit;
  516. info->chip.ecc.correct = nand_davinci_correct_1bit;
  517. info->chip.ecc.hwctl = nand_davinci_hwctl_1bit;
  518. info->chip.ecc.bytes = 3;
  519. }
  520. info->chip.ecc.size = 512;
  521. break;
  522. default:
  523. ret = -EINVAL;
  524. goto err_ecc;
  525. }
  526. info->chip.ecc.mode = ecc_mode;
  527. info->clk = clk_get(&pdev->dev, "aemif");
  528. if (IS_ERR(info->clk)) {
  529. ret = PTR_ERR(info->clk);
  530. dev_dbg(&pdev->dev, "unable to get AEMIF clock, err %d\n", ret);
  531. goto err_clk;
  532. }
  533. ret = clk_enable(info->clk);
  534. if (ret < 0) {
  535. dev_dbg(&pdev->dev, "unable to enable AEMIF clock, err %d\n",
  536. ret);
  537. goto err_clk_enable;
  538. }
  539. /* EMIF timings should normally be set by the boot loader,
  540. * especially after boot-from-NAND. The *only* reason to
  541. * have this special casing for the DM6446 EVM is to work
  542. * with boot-from-NOR ... with CS0 manually re-jumpered
  543. * (after startup) so it addresses the NAND flash, not NOR.
  544. * Even for dev boards, that's unusually rude...
  545. */
  546. if (machine_is_davinci_evm())
  547. nand_dm6446evm_flash_init(info);
  548. spin_lock_irq(&davinci_nand_lock);
  549. /* put CSxNAND into NAND mode */
  550. val = davinci_nand_readl(info, NANDFCR_OFFSET);
  551. val |= BIT(info->core_chipsel);
  552. davinci_nand_writel(info, NANDFCR_OFFSET, val);
  553. spin_unlock_irq(&davinci_nand_lock);
  554. /* Scan to find existence of the device(s) */
  555. ret = nand_scan_ident(&info->mtd, pdata->mask_chipsel ? 2 : 1);
  556. if (ret < 0) {
  557. dev_dbg(&pdev->dev, "no NAND chip(s) found\n");
  558. goto err_scan;
  559. }
  560. /* Update ECC layout if needed ... for 1-bit HW ECC, the default
  561. * is OK, but it allocates 6 bytes when only 3 are needed (for
  562. * each 512 bytes). For the 4-bit HW ECC, that default is not
  563. * usable: 10 bytes are needed, not 6.
  564. */
  565. if (pdata->ecc_bits == 4) {
  566. int chunks = info->mtd.writesize / 512;
  567. if (!chunks || info->mtd.oobsize < 16) {
  568. dev_dbg(&pdev->dev, "too small\n");
  569. ret = -EINVAL;
  570. goto err_scan;
  571. }
  572. /* For small page chips, preserve the manufacturer's
  573. * badblock marking data ... and make sure a flash BBT
  574. * table marker fits in the free bytes.
  575. */
  576. if (chunks == 1) {
  577. info->ecclayout = hwecc4_small;
  578. info->ecclayout.oobfree[1].length =
  579. info->mtd.oobsize - 16;
  580. goto syndrome_done;
  581. }
  582. /* For large page chips we'll be wanting to use a
  583. * not-yet-implemented mode that reads OOB data
  584. * before reading the body of the page, to avoid
  585. * the "infix OOB" model of NAND_ECC_HW_SYNDROME
  586. * (and preserve manufacturer badblock markings).
  587. */
  588. dev_warn(&pdev->dev, "no 4-bit ECC support yet "
  589. "for large page NAND\n");
  590. ret = -EIO;
  591. goto err_scan;
  592. syndrome_done:
  593. info->chip.ecc.layout = &info->ecclayout;
  594. }
  595. ret = nand_scan_tail(&info->mtd);
  596. if (ret < 0)
  597. goto err_scan;
  598. if (mtd_has_partitions()) {
  599. struct mtd_partition *mtd_parts = NULL;
  600. int mtd_parts_nb = 0;
  601. if (mtd_has_cmdlinepart()) {
  602. static const char *probes[] __initconst =
  603. { "cmdlinepart", NULL };
  604. mtd_parts_nb = parse_mtd_partitions(&info->mtd, probes,
  605. &mtd_parts, 0);
  606. }
  607. if (mtd_parts_nb <= 0) {
  608. mtd_parts = pdata->parts;
  609. mtd_parts_nb = pdata->nr_parts;
  610. }
  611. /* Register any partitions */
  612. if (mtd_parts_nb > 0) {
  613. ret = add_mtd_partitions(&info->mtd,
  614. mtd_parts, mtd_parts_nb);
  615. if (ret == 0)
  616. info->partitioned = true;
  617. }
  618. } else if (pdata->nr_parts) {
  619. dev_warn(&pdev->dev, "ignoring %d default partitions on %s\n",
  620. pdata->nr_parts, info->mtd.name);
  621. }
  622. /* If there's no partition info, just package the whole chip
  623. * as a single MTD device.
  624. */
  625. if (!info->partitioned)
  626. ret = add_mtd_device(&info->mtd) ? -ENODEV : 0;
  627. if (ret < 0)
  628. goto err_scan;
  629. val = davinci_nand_readl(info, NRCSR_OFFSET);
  630. dev_info(&pdev->dev, "controller rev. %d.%d\n",
  631. (val >> 8) & 0xff, val & 0xff);
  632. return 0;
  633. err_scan:
  634. clk_disable(info->clk);
  635. err_clk_enable:
  636. clk_put(info->clk);
  637. spin_lock_irq(&davinci_nand_lock);
  638. if (ecc_mode == NAND_ECC_HW_SYNDROME)
  639. ecc4_busy = false;
  640. spin_unlock_irq(&davinci_nand_lock);
  641. err_ecc:
  642. err_clk:
  643. err_ioremap:
  644. if (base)
  645. iounmap(base);
  646. if (vaddr)
  647. iounmap(vaddr);
  648. err_nomem:
  649. kfree(info);
  650. return ret;
  651. }
  652. static int __exit nand_davinci_remove(struct platform_device *pdev)
  653. {
  654. struct davinci_nand_info *info = platform_get_drvdata(pdev);
  655. int status;
  656. if (mtd_has_partitions() && info->partitioned)
  657. status = del_mtd_partitions(&info->mtd);
  658. else
  659. status = del_mtd_device(&info->mtd);
  660. spin_lock_irq(&davinci_nand_lock);
  661. if (info->chip.ecc.mode == NAND_ECC_HW_SYNDROME)
  662. ecc4_busy = false;
  663. spin_unlock_irq(&davinci_nand_lock);
  664. iounmap(info->base);
  665. iounmap(info->vaddr);
  666. nand_release(&info->mtd);
  667. clk_disable(info->clk);
  668. clk_put(info->clk);
  669. kfree(info);
  670. return 0;
  671. }
  672. static struct platform_driver nand_davinci_driver = {
  673. .remove = __exit_p(nand_davinci_remove),
  674. .driver = {
  675. .name = "davinci_nand",
  676. },
  677. };
  678. MODULE_ALIAS("platform:davinci_nand");
  679. static int __init nand_davinci_init(void)
  680. {
  681. return platform_driver_probe(&nand_davinci_driver, nand_davinci_probe);
  682. }
  683. module_init(nand_davinci_init);
  684. static void __exit nand_davinci_exit(void)
  685. {
  686. platform_driver_unregister(&nand_davinci_driver);
  687. }
  688. module_exit(nand_davinci_exit);
  689. MODULE_LICENSE("GPL");
  690. MODULE_AUTHOR("Texas Instruments");
  691. MODULE_DESCRIPTION("Davinci NAND flash driver");