davinci_nand.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  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 <linux/of_device.h>
  36. #include <linux/of.h>
  37. #include <linux/platform_data/mtd-davinci.h>
  38. #include <linux/platform_data/mtd-davinci-aemif.h>
  39. /*
  40. * This is a device driver for the NAND flash controller found on the
  41. * various DaVinci family chips. It handles up to four SoC chipselects,
  42. * and some flavors of secondary chipselect (e.g. based on A12) as used
  43. * with multichip packages.
  44. *
  45. * The 1-bit ECC hardware is supported, as well as the newer 4-bit ECC
  46. * available on chips like the DM355 and OMAP-L137 and needed with the
  47. * more error-prone MLC NAND chips.
  48. *
  49. * This driver assumes EM_WAIT connects all the NAND devices' RDY/nBUSY
  50. * outputs in a "wire-AND" configuration, with no per-chip signals.
  51. */
  52. struct davinci_nand_info {
  53. struct mtd_info mtd;
  54. struct nand_chip chip;
  55. struct nand_ecclayout ecclayout;
  56. struct device *dev;
  57. struct clk *clk;
  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. #if defined(CONFIG_OF)
  444. static const struct of_device_id davinci_nand_of_match[] = {
  445. {.compatible = "ti,davinci-nand", },
  446. {},
  447. };
  448. MODULE_DEVICE_TABLE(of, davinci_nand_of_match);
  449. static struct davinci_nand_pdata
  450. *nand_davinci_get_pdata(struct platform_device *pdev)
  451. {
  452. if (!dev_get_platdata(&pdev->dev) && pdev->dev.of_node) {
  453. struct davinci_nand_pdata *pdata;
  454. const char *mode;
  455. u32 prop;
  456. int len;
  457. pdata = devm_kzalloc(&pdev->dev,
  458. sizeof(struct davinci_nand_pdata),
  459. GFP_KERNEL);
  460. pdev->dev.platform_data = pdata;
  461. if (!pdata)
  462. return NULL;
  463. if (!of_property_read_u32(pdev->dev.of_node,
  464. "ti,davinci-chipselect", &prop))
  465. pdev->id = prop;
  466. if (!of_property_read_u32(pdev->dev.of_node,
  467. "ti,davinci-mask-ale", &prop))
  468. pdata->mask_ale = prop;
  469. if (!of_property_read_u32(pdev->dev.of_node,
  470. "ti,davinci-mask-cle", &prop))
  471. pdata->mask_cle = prop;
  472. if (!of_property_read_u32(pdev->dev.of_node,
  473. "ti,davinci-mask-chipsel", &prop))
  474. pdata->mask_chipsel = prop;
  475. if (!of_property_read_string(pdev->dev.of_node,
  476. "ti,davinci-ecc-mode", &mode)) {
  477. if (!strncmp("none", mode, 4))
  478. pdata->ecc_mode = NAND_ECC_NONE;
  479. if (!strncmp("soft", mode, 4))
  480. pdata->ecc_mode = NAND_ECC_SOFT;
  481. if (!strncmp("hw", mode, 2))
  482. pdata->ecc_mode = NAND_ECC_HW;
  483. }
  484. if (!of_property_read_u32(pdev->dev.of_node,
  485. "ti,davinci-ecc-bits", &prop))
  486. pdata->ecc_bits = prop;
  487. if (!of_property_read_u32(pdev->dev.of_node,
  488. "ti,davinci-nand-buswidth", &prop))
  489. if (prop == 16)
  490. pdata->options |= NAND_BUSWIDTH_16;
  491. if (of_find_property(pdev->dev.of_node,
  492. "ti,davinci-nand-use-bbt", &len))
  493. pdata->bbt_options = NAND_BBT_USE_FLASH;
  494. }
  495. return dev_get_platdata(&pdev->dev);
  496. }
  497. #else
  498. static struct davinci_nand_pdata
  499. *nand_davinci_get_pdata(struct platform_device *pdev)
  500. {
  501. return dev_get_platdata(&pdev->dev);
  502. }
  503. #endif
  504. static int __init nand_davinci_probe(struct platform_device *pdev)
  505. {
  506. struct davinci_nand_pdata *pdata;
  507. struct davinci_nand_info *info;
  508. struct resource *res1;
  509. struct resource *res2;
  510. void __iomem *vaddr;
  511. void __iomem *base;
  512. int ret;
  513. uint32_t val;
  514. nand_ecc_modes_t ecc_mode;
  515. pdata = nand_davinci_get_pdata(pdev);
  516. /* insist on board-specific configuration */
  517. if (!pdata)
  518. return -ENODEV;
  519. /* which external chipselect will we be managing? */
  520. if (pdev->id < 0 || pdev->id > 3)
  521. return -ENODEV;
  522. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  523. if (!info) {
  524. dev_err(&pdev->dev, "unable to allocate memory\n");
  525. ret = -ENOMEM;
  526. goto err_nomem;
  527. }
  528. platform_set_drvdata(pdev, info);
  529. res1 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  530. res2 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  531. if (!res1 || !res2) {
  532. dev_err(&pdev->dev, "resource missing\n");
  533. ret = -EINVAL;
  534. goto err_nomem;
  535. }
  536. vaddr = devm_ioremap_resource(&pdev->dev, res1);
  537. if (IS_ERR(vaddr)) {
  538. ret = PTR_ERR(vaddr);
  539. goto err_ioremap;
  540. }
  541. base = devm_ioremap_resource(&pdev->dev, res2);
  542. if (IS_ERR(base)) {
  543. ret = PTR_ERR(base);
  544. goto err_ioremap;
  545. }
  546. info->dev = &pdev->dev;
  547. info->base = base;
  548. info->vaddr = vaddr;
  549. info->mtd.priv = &info->chip;
  550. info->mtd.name = dev_name(&pdev->dev);
  551. info->mtd.owner = THIS_MODULE;
  552. info->mtd.dev.parent = &pdev->dev;
  553. info->chip.IO_ADDR_R = vaddr;
  554. info->chip.IO_ADDR_W = vaddr;
  555. info->chip.chip_delay = 0;
  556. info->chip.select_chip = nand_davinci_select_chip;
  557. /* options such as NAND_BBT_USE_FLASH */
  558. info->chip.bbt_options = pdata->bbt_options;
  559. /* options such as 16-bit widths */
  560. info->chip.options = pdata->options;
  561. info->chip.bbt_td = pdata->bbt_td;
  562. info->chip.bbt_md = pdata->bbt_md;
  563. info->timing = pdata->timing;
  564. info->ioaddr = (uint32_t __force) vaddr;
  565. info->current_cs = info->ioaddr;
  566. info->core_chipsel = pdev->id;
  567. info->mask_chipsel = pdata->mask_chipsel;
  568. /* use nandboot-capable ALE/CLE masks by default */
  569. info->mask_ale = pdata->mask_ale ? : MASK_ALE;
  570. info->mask_cle = pdata->mask_cle ? : MASK_CLE;
  571. /* Set address of hardware control function */
  572. info->chip.cmd_ctrl = nand_davinci_hwcontrol;
  573. info->chip.dev_ready = nand_davinci_dev_ready;
  574. /* Speed up buffer I/O */
  575. info->chip.read_buf = nand_davinci_read_buf;
  576. info->chip.write_buf = nand_davinci_write_buf;
  577. /* Use board-specific ECC config */
  578. ecc_mode = pdata->ecc_mode;
  579. ret = -EINVAL;
  580. switch (ecc_mode) {
  581. case NAND_ECC_NONE:
  582. case NAND_ECC_SOFT:
  583. pdata->ecc_bits = 0;
  584. break;
  585. case NAND_ECC_HW:
  586. if (pdata->ecc_bits == 4) {
  587. /* No sanity checks: CPUs must support this,
  588. * and the chips may not use NAND_BUSWIDTH_16.
  589. */
  590. /* No sharing 4-bit hardware between chipselects yet */
  591. spin_lock_irq(&davinci_nand_lock);
  592. if (ecc4_busy)
  593. ret = -EBUSY;
  594. else
  595. ecc4_busy = true;
  596. spin_unlock_irq(&davinci_nand_lock);
  597. if (ret == -EBUSY)
  598. goto err_ecc;
  599. info->chip.ecc.calculate = nand_davinci_calculate_4bit;
  600. info->chip.ecc.correct = nand_davinci_correct_4bit;
  601. info->chip.ecc.hwctl = nand_davinci_hwctl_4bit;
  602. info->chip.ecc.bytes = 10;
  603. } else {
  604. info->chip.ecc.calculate = nand_davinci_calculate_1bit;
  605. info->chip.ecc.correct = nand_davinci_correct_1bit;
  606. info->chip.ecc.hwctl = nand_davinci_hwctl_1bit;
  607. info->chip.ecc.bytes = 3;
  608. }
  609. info->chip.ecc.size = 512;
  610. info->chip.ecc.strength = pdata->ecc_bits;
  611. break;
  612. default:
  613. ret = -EINVAL;
  614. goto err_ecc;
  615. }
  616. info->chip.ecc.mode = ecc_mode;
  617. info->clk = devm_clk_get(&pdev->dev, "aemif");
  618. if (IS_ERR(info->clk)) {
  619. ret = PTR_ERR(info->clk);
  620. dev_dbg(&pdev->dev, "unable to get AEMIF clock, err %d\n", ret);
  621. goto err_clk;
  622. }
  623. ret = clk_prepare_enable(info->clk);
  624. if (ret < 0) {
  625. dev_dbg(&pdev->dev, "unable to enable AEMIF clock, err %d\n",
  626. ret);
  627. goto err_clk_enable;
  628. }
  629. /*
  630. * Setup Async configuration register in case we did not boot from
  631. * NAND and so bootloader did not bother to set it up.
  632. */
  633. val = davinci_nand_readl(info, A1CR_OFFSET + info->core_chipsel * 4);
  634. /* Extended Wait is not valid and Select Strobe mode is not used */
  635. val &= ~(ACR_ASIZE_MASK | ACR_EW_MASK | ACR_SS_MASK);
  636. if (info->chip.options & NAND_BUSWIDTH_16)
  637. val |= 0x1;
  638. davinci_nand_writel(info, A1CR_OFFSET + info->core_chipsel * 4, val);
  639. ret = 0;
  640. if (info->timing)
  641. ret = davinci_aemif_setup_timing(info->timing, info->base,
  642. info->core_chipsel);
  643. if (ret < 0) {
  644. dev_dbg(&pdev->dev, "NAND timing values setup fail\n");
  645. goto err_timing;
  646. }
  647. spin_lock_irq(&davinci_nand_lock);
  648. /* put CSxNAND into NAND mode */
  649. val = davinci_nand_readl(info, NANDFCR_OFFSET);
  650. val |= BIT(info->core_chipsel);
  651. davinci_nand_writel(info, NANDFCR_OFFSET, val);
  652. spin_unlock_irq(&davinci_nand_lock);
  653. /* Scan to find existence of the device(s) */
  654. ret = nand_scan_ident(&info->mtd, pdata->mask_chipsel ? 2 : 1, NULL);
  655. if (ret < 0) {
  656. dev_dbg(&pdev->dev, "no NAND chip(s) found\n");
  657. goto err_scan;
  658. }
  659. /* Update ECC layout if needed ... for 1-bit HW ECC, the default
  660. * is OK, but it allocates 6 bytes when only 3 are needed (for
  661. * each 512 bytes). For the 4-bit HW ECC, that default is not
  662. * usable: 10 bytes are needed, not 6.
  663. */
  664. if (pdata->ecc_bits == 4) {
  665. int chunks = info->mtd.writesize / 512;
  666. if (!chunks || info->mtd.oobsize < 16) {
  667. dev_dbg(&pdev->dev, "too small\n");
  668. ret = -EINVAL;
  669. goto err_scan;
  670. }
  671. /* For small page chips, preserve the manufacturer's
  672. * badblock marking data ... and make sure a flash BBT
  673. * table marker fits in the free bytes.
  674. */
  675. if (chunks == 1) {
  676. info->ecclayout = hwecc4_small;
  677. info->ecclayout.oobfree[1].length =
  678. info->mtd.oobsize - 16;
  679. goto syndrome_done;
  680. }
  681. if (chunks == 4) {
  682. info->ecclayout = hwecc4_2048;
  683. info->chip.ecc.mode = NAND_ECC_HW_OOB_FIRST;
  684. goto syndrome_done;
  685. }
  686. /* 4KiB page chips are not yet supported. The eccpos from
  687. * nand_ecclayout cannot hold 80 bytes and change to eccpos[]
  688. * breaks userspace ioctl interface with mtd-utils. Once we
  689. * resolve this issue, NAND_ECC_HW_OOB_FIRST mode can be used
  690. * for the 4KiB page chips.
  691. *
  692. * TODO: Note that nand_ecclayout has now been expanded and can
  693. * hold plenty of OOB entries.
  694. */
  695. dev_warn(&pdev->dev, "no 4-bit ECC support yet "
  696. "for 4KiB-page NAND\n");
  697. ret = -EIO;
  698. goto err_scan;
  699. syndrome_done:
  700. info->chip.ecc.layout = &info->ecclayout;
  701. }
  702. ret = nand_scan_tail(&info->mtd);
  703. if (ret < 0)
  704. goto err_scan;
  705. if (pdata->parts)
  706. ret = mtd_device_parse_register(&info->mtd, NULL, NULL,
  707. pdata->parts, pdata->nr_parts);
  708. else {
  709. struct mtd_part_parser_data ppdata;
  710. ppdata.of_node = pdev->dev.of_node;
  711. ret = mtd_device_parse_register(&info->mtd, NULL, &ppdata,
  712. NULL, 0);
  713. }
  714. if (ret < 0)
  715. goto err_scan;
  716. val = davinci_nand_readl(info, NRCSR_OFFSET);
  717. dev_info(&pdev->dev, "controller rev. %d.%d\n",
  718. (val >> 8) & 0xff, val & 0xff);
  719. return 0;
  720. err_scan:
  721. err_timing:
  722. clk_disable_unprepare(info->clk);
  723. err_clk_enable:
  724. spin_lock_irq(&davinci_nand_lock);
  725. if (ecc_mode == NAND_ECC_HW_SYNDROME)
  726. ecc4_busy = false;
  727. spin_unlock_irq(&davinci_nand_lock);
  728. err_ecc:
  729. err_clk:
  730. err_ioremap:
  731. err_nomem:
  732. return ret;
  733. }
  734. static int __exit nand_davinci_remove(struct platform_device *pdev)
  735. {
  736. struct davinci_nand_info *info = platform_get_drvdata(pdev);
  737. spin_lock_irq(&davinci_nand_lock);
  738. if (info->chip.ecc.mode == NAND_ECC_HW_SYNDROME)
  739. ecc4_busy = false;
  740. spin_unlock_irq(&davinci_nand_lock);
  741. nand_release(&info->mtd);
  742. clk_disable_unprepare(info->clk);
  743. return 0;
  744. }
  745. static struct platform_driver nand_davinci_driver = {
  746. .remove = __exit_p(nand_davinci_remove),
  747. .driver = {
  748. .name = "davinci_nand",
  749. .owner = THIS_MODULE,
  750. .of_match_table = of_match_ptr(davinci_nand_of_match),
  751. },
  752. };
  753. MODULE_ALIAS("platform:davinci_nand");
  754. module_platform_driver_probe(nand_davinci_driver, nand_davinci_probe);
  755. MODULE_LICENSE("GPL");
  756. MODULE_AUTHOR("Texas Instruments");
  757. MODULE_DESCRIPTION("Davinci NAND flash driver");