davinci_nand.c 25 KB

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