mxc_nand.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. /*
  2. * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved.
  3. * Copyright 2008 Sascha Hauer, kernel@pengutronix.de
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. * MA 02110-1301, USA.
  18. */
  19. #include <linux/delay.h>
  20. #include <linux/slab.h>
  21. #include <linux/init.h>
  22. #include <linux/module.h>
  23. #include <linux/mtd/mtd.h>
  24. #include <linux/mtd/nand.h>
  25. #include <linux/mtd/partitions.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/device.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/clk.h>
  30. #include <linux/err.h>
  31. #include <linux/io.h>
  32. #include <asm/mach/flash.h>
  33. #include <mach/mxc_nand.h>
  34. #include <mach/hardware.h>
  35. #define DRIVER_NAME "mxc_nand"
  36. #define nfc_is_v21() (cpu_is_mx25() || cpu_is_mx35())
  37. #define nfc_is_v1() (cpu_is_mx31() || cpu_is_mx27() || cpu_is_mx21())
  38. /* Addresses for NFC registers */
  39. #define NFC_V1_V2_BUF_SIZE (host->regs + 0x00)
  40. #define NFC_V1_V2_BUF_ADDR (host->regs + 0x04)
  41. #define NFC_V1_V2_FLASH_ADDR (host->regs + 0x06)
  42. #define NFC_V1_V2_FLASH_CMD (host->regs + 0x08)
  43. #define NFC_V1_V2_CONFIG (host->regs + 0x0a)
  44. #define NFC_V1_V2_ECC_STATUS_RESULT (host->regs + 0x0c)
  45. #define NFC_V1_V2_RSLTMAIN_AREA (host->regs + 0x0e)
  46. #define NFC_V1_V2_RSLTSPARE_AREA (host->regs + 0x10)
  47. #define NFC_V1_V2_WRPROT (host->regs + 0x12)
  48. #define NFC_V1_UNLOCKSTART_BLKADDR (host->regs + 0x14)
  49. #define NFC_V1_UNLOCKEND_BLKADDR (host->regs + 0x16)
  50. #define NFC_V21_UNLOCKSTART_BLKADDR (host->regs + 0x20)
  51. #define NFC_V21_UNLOCKEND_BLKADDR (host->regs + 0x22)
  52. #define NFC_V1_V2_NF_WRPRST (host->regs + 0x18)
  53. #define NFC_V1_V2_CONFIG1 (host->regs + 0x1a)
  54. #define NFC_V1_V2_CONFIG2 (host->regs + 0x1c)
  55. #define NFC_V2_CONFIG1_ECC_MODE_4 (1 << 0)
  56. #define NFC_V1_V2_CONFIG1_SP_EN (1 << 2)
  57. #define NFC_V1_V2_CONFIG1_ECC_EN (1 << 3)
  58. #define NFC_V1_V2_CONFIG1_INT_MSK (1 << 4)
  59. #define NFC_V1_V2_CONFIG1_BIG (1 << 5)
  60. #define NFC_V1_V2_CONFIG1_RST (1 << 6)
  61. #define NFC_V1_V2_CONFIG1_CE (1 << 7)
  62. #define NFC_V1_V2_CONFIG1_ONE_CYCLE (1 << 8)
  63. #define NFC_V1_V2_CONFIG2_INT (1 << 15)
  64. /*
  65. * Operation modes for the NFC. Valid for v1, v2 and v3
  66. * type controllers.
  67. */
  68. #define NFC_CMD (1 << 0)
  69. #define NFC_ADDR (1 << 1)
  70. #define NFC_INPUT (1 << 2)
  71. #define NFC_OUTPUT (1 << 3)
  72. #define NFC_ID (1 << 4)
  73. #define NFC_STATUS (1 << 5)
  74. struct mxc_nand_host {
  75. struct mtd_info mtd;
  76. struct nand_chip nand;
  77. struct mtd_partition *parts;
  78. struct device *dev;
  79. void *spare0;
  80. void *main_area0;
  81. void __iomem *base;
  82. void __iomem *regs;
  83. int status_request;
  84. struct clk *clk;
  85. int clk_act;
  86. int irq;
  87. int eccsize;
  88. wait_queue_head_t irq_waitq;
  89. uint8_t *data_buf;
  90. unsigned int buf_start;
  91. int spare_len;
  92. void (*preset)(struct mtd_info *);
  93. void (*send_cmd)(struct mxc_nand_host *, uint16_t, int);
  94. void (*send_addr)(struct mxc_nand_host *, uint16_t, int);
  95. void (*send_page)(struct mtd_info *, unsigned int);
  96. void (*send_read_id)(struct mxc_nand_host *);
  97. uint16_t (*get_dev_status)(struct mxc_nand_host *);
  98. int (*check_int)(struct mxc_nand_host *);
  99. };
  100. /* OOB placement block for use with hardware ecc generation */
  101. static struct nand_ecclayout nandv1_hw_eccoob_smallpage = {
  102. .eccbytes = 5,
  103. .eccpos = {6, 7, 8, 9, 10},
  104. .oobfree = {{0, 5}, {12, 4}, }
  105. };
  106. static struct nand_ecclayout nandv1_hw_eccoob_largepage = {
  107. .eccbytes = 20,
  108. .eccpos = {6, 7, 8, 9, 10, 22, 23, 24, 25, 26,
  109. 38, 39, 40, 41, 42, 54, 55, 56, 57, 58},
  110. .oobfree = {{2, 4}, {11, 10}, {27, 10}, {43, 10}, {59, 5}, }
  111. };
  112. /* OOB description for 512 byte pages with 16 byte OOB */
  113. static struct nand_ecclayout nandv2_hw_eccoob_smallpage = {
  114. .eccbytes = 1 * 9,
  115. .eccpos = {
  116. 7, 8, 9, 10, 11, 12, 13, 14, 15
  117. },
  118. .oobfree = {
  119. {.offset = 0, .length = 5}
  120. }
  121. };
  122. /* OOB description for 2048 byte pages with 64 byte OOB */
  123. static struct nand_ecclayout nandv2_hw_eccoob_largepage = {
  124. .eccbytes = 4 * 9,
  125. .eccpos = {
  126. 7, 8, 9, 10, 11, 12, 13, 14, 15,
  127. 23, 24, 25, 26, 27, 28, 29, 30, 31,
  128. 39, 40, 41, 42, 43, 44, 45, 46, 47,
  129. 55, 56, 57, 58, 59, 60, 61, 62, 63
  130. },
  131. .oobfree = {
  132. {.offset = 2, .length = 4},
  133. {.offset = 16, .length = 7},
  134. {.offset = 32, .length = 7},
  135. {.offset = 48, .length = 7}
  136. }
  137. };
  138. #ifdef CONFIG_MTD_PARTITIONS
  139. static const char *part_probes[] = { "RedBoot", "cmdlinepart", NULL };
  140. #endif
  141. static irqreturn_t mxc_nfc_irq(int irq, void *dev_id)
  142. {
  143. struct mxc_nand_host *host = dev_id;
  144. disable_irq_nosync(irq);
  145. wake_up(&host->irq_waitq);
  146. return IRQ_HANDLED;
  147. }
  148. static int check_int_v1_v2(struct mxc_nand_host *host)
  149. {
  150. uint32_t tmp;
  151. tmp = readw(NFC_V1_V2_CONFIG2);
  152. if (!(tmp & NFC_V1_V2_CONFIG2_INT))
  153. return 0;
  154. writew(tmp & ~NFC_V1_V2_CONFIG2_INT, NFC_V1_V2_CONFIG2);
  155. return 1;
  156. }
  157. /* This function polls the NANDFC to wait for the basic operation to
  158. * complete by checking the INT bit of config2 register.
  159. */
  160. static void wait_op_done(struct mxc_nand_host *host, int useirq)
  161. {
  162. int max_retries = 8000;
  163. if (useirq) {
  164. if (!host->check_int(host)) {
  165. enable_irq(host->irq);
  166. wait_event(host->irq_waitq, host->check_int(host));
  167. }
  168. } else {
  169. while (max_retries-- > 0) {
  170. if (host->check_int(host))
  171. break;
  172. udelay(1);
  173. }
  174. if (max_retries < 0)
  175. DEBUG(MTD_DEBUG_LEVEL0, "%s: INT not set\n",
  176. __func__);
  177. }
  178. }
  179. /* This function issues the specified command to the NAND device and
  180. * waits for completion. */
  181. static void send_cmd_v1_v2(struct mxc_nand_host *host, uint16_t cmd, int useirq)
  182. {
  183. DEBUG(MTD_DEBUG_LEVEL3, "send_cmd(host, 0x%x, %d)\n", cmd, useirq);
  184. writew(cmd, NFC_V1_V2_FLASH_CMD);
  185. writew(NFC_CMD, NFC_V1_V2_CONFIG2);
  186. if (cpu_is_mx21() && (cmd == NAND_CMD_RESET)) {
  187. int max_retries = 100;
  188. /* Reset completion is indicated by NFC_CONFIG2 */
  189. /* being set to 0 */
  190. while (max_retries-- > 0) {
  191. if (readw(NFC_V1_V2_CONFIG2) == 0) {
  192. break;
  193. }
  194. udelay(1);
  195. }
  196. if (max_retries < 0)
  197. DEBUG(MTD_DEBUG_LEVEL0, "%s: RESET failed\n",
  198. __func__);
  199. } else {
  200. /* Wait for operation to complete */
  201. wait_op_done(host, useirq);
  202. }
  203. }
  204. /* This function sends an address (or partial address) to the
  205. * NAND device. The address is used to select the source/destination for
  206. * a NAND command. */
  207. static void send_addr_v1_v2(struct mxc_nand_host *host, uint16_t addr, int islast)
  208. {
  209. DEBUG(MTD_DEBUG_LEVEL3, "send_addr(host, 0x%x %d)\n", addr, islast);
  210. writew(addr, NFC_V1_V2_FLASH_ADDR);
  211. writew(NFC_ADDR, NFC_V1_V2_CONFIG2);
  212. /* Wait for operation to complete */
  213. wait_op_done(host, islast);
  214. }
  215. static void send_page_v1_v2(struct mtd_info *mtd, unsigned int ops)
  216. {
  217. struct nand_chip *nand_chip = mtd->priv;
  218. struct mxc_nand_host *host = nand_chip->priv;
  219. int bufs, i;
  220. if (nfc_is_v1() && mtd->writesize > 512)
  221. bufs = 4;
  222. else
  223. bufs = 1;
  224. for (i = 0; i < bufs; i++) {
  225. /* NANDFC buffer 0 is used for page read/write */
  226. writew(i, NFC_V1_V2_BUF_ADDR);
  227. writew(ops, NFC_V1_V2_CONFIG2);
  228. /* Wait for operation to complete */
  229. wait_op_done(host, true);
  230. }
  231. }
  232. /* Request the NANDFC to perform a read of the NAND device ID. */
  233. static void send_read_id_v1_v2(struct mxc_nand_host *host)
  234. {
  235. struct nand_chip *this = &host->nand;
  236. /* NANDFC buffer 0 is used for device ID output */
  237. writew(0x0, NFC_V1_V2_BUF_ADDR);
  238. writew(NFC_ID, NFC_V1_V2_CONFIG2);
  239. /* Wait for operation to complete */
  240. wait_op_done(host, true);
  241. if (this->options & NAND_BUSWIDTH_16) {
  242. void __iomem *main_buf = host->main_area0;
  243. /* compress the ID info */
  244. writeb(readb(main_buf + 2), main_buf + 1);
  245. writeb(readb(main_buf + 4), main_buf + 2);
  246. writeb(readb(main_buf + 6), main_buf + 3);
  247. writeb(readb(main_buf + 8), main_buf + 4);
  248. writeb(readb(main_buf + 10), main_buf + 5);
  249. }
  250. memcpy(host->data_buf, host->main_area0, 16);
  251. }
  252. /* This function requests the NANDFC to perform a read of the
  253. * NAND device status and returns the current status. */
  254. static uint16_t get_dev_status_v1_v2(struct mxc_nand_host *host)
  255. {
  256. void __iomem *main_buf = host->main_area0;
  257. uint32_t store;
  258. uint16_t ret;
  259. writew(0x0, NFC_V1_V2_BUF_ADDR);
  260. /*
  261. * The device status is stored in main_area0. To
  262. * prevent corruption of the buffer save the value
  263. * and restore it afterwards.
  264. */
  265. store = readl(main_buf);
  266. writew(NFC_STATUS, NFC_V1_V2_CONFIG2);
  267. wait_op_done(host, true);
  268. ret = readw(main_buf);
  269. writel(store, main_buf);
  270. return ret;
  271. }
  272. /* This functions is used by upper layer to checks if device is ready */
  273. static int mxc_nand_dev_ready(struct mtd_info *mtd)
  274. {
  275. /*
  276. * NFC handles R/B internally. Therefore, this function
  277. * always returns status as ready.
  278. */
  279. return 1;
  280. }
  281. static void mxc_nand_enable_hwecc(struct mtd_info *mtd, int mode)
  282. {
  283. /*
  284. * If HW ECC is enabled, we turn it on during init. There is
  285. * no need to enable again here.
  286. */
  287. }
  288. static int mxc_nand_correct_data_v1(struct mtd_info *mtd, u_char *dat,
  289. u_char *read_ecc, u_char *calc_ecc)
  290. {
  291. struct nand_chip *nand_chip = mtd->priv;
  292. struct mxc_nand_host *host = nand_chip->priv;
  293. /*
  294. * 1-Bit errors are automatically corrected in HW. No need for
  295. * additional correction. 2-Bit errors cannot be corrected by
  296. * HW ECC, so we need to return failure
  297. */
  298. uint16_t ecc_status = readw(NFC_V1_V2_ECC_STATUS_RESULT);
  299. if (((ecc_status & 0x3) == 2) || ((ecc_status >> 2) == 2)) {
  300. DEBUG(MTD_DEBUG_LEVEL0,
  301. "MXC_NAND: HWECC uncorrectable 2-bit ECC error\n");
  302. return -1;
  303. }
  304. return 0;
  305. }
  306. static int mxc_nand_correct_data_v2_v3(struct mtd_info *mtd, u_char *dat,
  307. u_char *read_ecc, u_char *calc_ecc)
  308. {
  309. struct nand_chip *nand_chip = mtd->priv;
  310. struct mxc_nand_host *host = nand_chip->priv;
  311. u32 ecc_stat, err;
  312. int no_subpages = 1;
  313. int ret = 0;
  314. u8 ecc_bit_mask, err_limit;
  315. ecc_bit_mask = (host->eccsize == 4) ? 0x7 : 0xf;
  316. err_limit = (host->eccsize == 4) ? 0x4 : 0x8;
  317. no_subpages = mtd->writesize >> 9;
  318. ecc_stat = readl(NFC_V1_V2_ECC_STATUS_RESULT);
  319. do {
  320. err = ecc_stat & ecc_bit_mask;
  321. if (err > err_limit) {
  322. printk(KERN_WARNING "UnCorrectable RS-ECC Error\n");
  323. return -1;
  324. } else {
  325. ret += err;
  326. }
  327. ecc_stat >>= 4;
  328. } while (--no_subpages);
  329. mtd->ecc_stats.corrected += ret;
  330. pr_debug("%d Symbol Correctable RS-ECC Error\n", ret);
  331. return ret;
  332. }
  333. static int mxc_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
  334. u_char *ecc_code)
  335. {
  336. return 0;
  337. }
  338. static u_char mxc_nand_read_byte(struct mtd_info *mtd)
  339. {
  340. struct nand_chip *nand_chip = mtd->priv;
  341. struct mxc_nand_host *host = nand_chip->priv;
  342. uint8_t ret;
  343. /* Check for status request */
  344. if (host->status_request)
  345. return host->get_dev_status(host) & 0xFF;
  346. ret = *(uint8_t *)(host->data_buf + host->buf_start);
  347. host->buf_start++;
  348. return ret;
  349. }
  350. static uint16_t mxc_nand_read_word(struct mtd_info *mtd)
  351. {
  352. struct nand_chip *nand_chip = mtd->priv;
  353. struct mxc_nand_host *host = nand_chip->priv;
  354. uint16_t ret;
  355. ret = *(uint16_t *)(host->data_buf + host->buf_start);
  356. host->buf_start += 2;
  357. return ret;
  358. }
  359. /* Write data of length len to buffer buf. The data to be
  360. * written on NAND Flash is first copied to RAMbuffer. After the Data Input
  361. * Operation by the NFC, the data is written to NAND Flash */
  362. static void mxc_nand_write_buf(struct mtd_info *mtd,
  363. const u_char *buf, int len)
  364. {
  365. struct nand_chip *nand_chip = mtd->priv;
  366. struct mxc_nand_host *host = nand_chip->priv;
  367. u16 col = host->buf_start;
  368. int n = mtd->oobsize + mtd->writesize - col;
  369. n = min(n, len);
  370. memcpy(host->data_buf + col, buf, n);
  371. host->buf_start += n;
  372. }
  373. /* Read the data buffer from the NAND Flash. To read the data from NAND
  374. * Flash first the data output cycle is initiated by the NFC, which copies
  375. * the data to RAMbuffer. This data of length len is then copied to buffer buf.
  376. */
  377. static void mxc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  378. {
  379. struct nand_chip *nand_chip = mtd->priv;
  380. struct mxc_nand_host *host = nand_chip->priv;
  381. u16 col = host->buf_start;
  382. int n = mtd->oobsize + mtd->writesize - col;
  383. n = min(n, len);
  384. memcpy(buf, host->data_buf + col, len);
  385. host->buf_start += len;
  386. }
  387. /* Used by the upper layer to verify the data in NAND Flash
  388. * with the data in the buf. */
  389. static int mxc_nand_verify_buf(struct mtd_info *mtd,
  390. const u_char *buf, int len)
  391. {
  392. return -EFAULT;
  393. }
  394. /* This function is used by upper layer for select and
  395. * deselect of the NAND chip */
  396. static void mxc_nand_select_chip(struct mtd_info *mtd, int chip)
  397. {
  398. struct nand_chip *nand_chip = mtd->priv;
  399. struct mxc_nand_host *host = nand_chip->priv;
  400. switch (chip) {
  401. case -1:
  402. /* Disable the NFC clock */
  403. if (host->clk_act) {
  404. clk_disable(host->clk);
  405. host->clk_act = 0;
  406. }
  407. break;
  408. case 0:
  409. /* Enable the NFC clock */
  410. if (!host->clk_act) {
  411. clk_enable(host->clk);
  412. host->clk_act = 1;
  413. }
  414. break;
  415. default:
  416. break;
  417. }
  418. }
  419. /*
  420. * Function to transfer data to/from spare area.
  421. */
  422. static void copy_spare(struct mtd_info *mtd, bool bfrom)
  423. {
  424. struct nand_chip *this = mtd->priv;
  425. struct mxc_nand_host *host = this->priv;
  426. u16 i, j;
  427. u16 n = mtd->writesize >> 9;
  428. u8 *d = host->data_buf + mtd->writesize;
  429. u8 *s = host->spare0;
  430. u16 t = host->spare_len;
  431. j = (mtd->oobsize / n >> 1) << 1;
  432. if (bfrom) {
  433. for (i = 0; i < n - 1; i++)
  434. memcpy(d + i * j, s + i * t, j);
  435. /* the last section */
  436. memcpy(d + i * j, s + i * t, mtd->oobsize - i * j);
  437. } else {
  438. for (i = 0; i < n - 1; i++)
  439. memcpy(&s[i * t], &d[i * j], j);
  440. /* the last section */
  441. memcpy(&s[i * t], &d[i * j], mtd->oobsize - i * j);
  442. }
  443. }
  444. static void mxc_do_addr_cycle(struct mtd_info *mtd, int column, int page_addr)
  445. {
  446. struct nand_chip *nand_chip = mtd->priv;
  447. struct mxc_nand_host *host = nand_chip->priv;
  448. /* Write out column address, if necessary */
  449. if (column != -1) {
  450. /*
  451. * MXC NANDFC can only perform full page+spare or
  452. * spare-only read/write. When the upper layers
  453. * layers perform a read/write buf operation,
  454. * we will used the saved column address to index into
  455. * the full page.
  456. */
  457. host->send_addr(host, 0, page_addr == -1);
  458. if (mtd->writesize > 512)
  459. /* another col addr cycle for 2k page */
  460. host->send_addr(host, 0, false);
  461. }
  462. /* Write out page address, if necessary */
  463. if (page_addr != -1) {
  464. /* paddr_0 - p_addr_7 */
  465. host->send_addr(host, (page_addr & 0xff), false);
  466. if (mtd->writesize > 512) {
  467. if (mtd->size >= 0x10000000) {
  468. /* paddr_8 - paddr_15 */
  469. host->send_addr(host, (page_addr >> 8) & 0xff, false);
  470. host->send_addr(host, (page_addr >> 16) & 0xff, true);
  471. } else
  472. /* paddr_8 - paddr_15 */
  473. host->send_addr(host, (page_addr >> 8) & 0xff, true);
  474. } else {
  475. /* One more address cycle for higher density devices */
  476. if (mtd->size >= 0x4000000) {
  477. /* paddr_8 - paddr_15 */
  478. host->send_addr(host, (page_addr >> 8) & 0xff, false);
  479. host->send_addr(host, (page_addr >> 16) & 0xff, true);
  480. } else
  481. /* paddr_8 - paddr_15 */
  482. host->send_addr(host, (page_addr >> 8) & 0xff, true);
  483. }
  484. }
  485. }
  486. /*
  487. * v2 and v3 type controllers can do 4bit or 8bit ecc depending
  488. * on how much oob the nand chip has. For 8bit ecc we need at least
  489. * 26 bytes of oob data per 512 byte block.
  490. */
  491. static int get_eccsize(struct mtd_info *mtd)
  492. {
  493. int oobbytes_per_512 = 0;
  494. oobbytes_per_512 = mtd->oobsize * 512 / mtd->writesize;
  495. if (oobbytes_per_512 < 26)
  496. return 4;
  497. else
  498. return 8;
  499. }
  500. static void preset_v1_v2(struct mtd_info *mtd)
  501. {
  502. struct nand_chip *nand_chip = mtd->priv;
  503. struct mxc_nand_host *host = nand_chip->priv;
  504. uint16_t tmp;
  505. /* enable interrupt, disable spare enable */
  506. tmp = readw(NFC_V1_V2_CONFIG1);
  507. tmp &= ~NFC_V1_V2_CONFIG1_INT_MSK;
  508. tmp &= ~NFC_V1_V2_CONFIG1_SP_EN;
  509. if (nand_chip->ecc.mode == NAND_ECC_HW) {
  510. tmp |= NFC_V1_V2_CONFIG1_ECC_EN;
  511. } else {
  512. tmp &= ~NFC_V1_V2_CONFIG1_ECC_EN;
  513. }
  514. if (nfc_is_v21() && mtd->writesize) {
  515. host->eccsize = get_eccsize(mtd);
  516. if (host->eccsize == 4)
  517. tmp |= NFC_V2_CONFIG1_ECC_MODE_4;
  518. } else {
  519. host->eccsize = 1;
  520. }
  521. writew(tmp, NFC_V1_V2_CONFIG1);
  522. /* preset operation */
  523. /* Unlock the internal RAM Buffer */
  524. writew(0x2, NFC_V1_V2_CONFIG);
  525. /* Blocks to be unlocked */
  526. if (nfc_is_v21()) {
  527. writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR);
  528. writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR);
  529. } else if (nfc_is_v1()) {
  530. writew(0x0, NFC_V1_UNLOCKSTART_BLKADDR);
  531. writew(0x4000, NFC_V1_UNLOCKEND_BLKADDR);
  532. } else
  533. BUG();
  534. /* Unlock Block Command for given address range */
  535. writew(0x4, NFC_V1_V2_WRPROT);
  536. }
  537. /* Used by the upper layer to write command to NAND Flash for
  538. * different operations to be carried out on NAND Flash */
  539. static void mxc_nand_command(struct mtd_info *mtd, unsigned command,
  540. int column, int page_addr)
  541. {
  542. struct nand_chip *nand_chip = mtd->priv;
  543. struct mxc_nand_host *host = nand_chip->priv;
  544. DEBUG(MTD_DEBUG_LEVEL3,
  545. "mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n",
  546. command, column, page_addr);
  547. /* Reset command state information */
  548. host->status_request = false;
  549. /* Command pre-processing step */
  550. switch (command) {
  551. case NAND_CMD_RESET:
  552. host->preset(mtd);
  553. host->send_cmd(host, command, false);
  554. break;
  555. case NAND_CMD_STATUS:
  556. host->buf_start = 0;
  557. host->status_request = true;
  558. host->send_cmd(host, command, true);
  559. mxc_do_addr_cycle(mtd, column, page_addr);
  560. break;
  561. case NAND_CMD_READ0:
  562. case NAND_CMD_READOOB:
  563. if (command == NAND_CMD_READ0)
  564. host->buf_start = column;
  565. else
  566. host->buf_start = column + mtd->writesize;
  567. command = NAND_CMD_READ0; /* only READ0 is valid */
  568. host->send_cmd(host, command, false);
  569. mxc_do_addr_cycle(mtd, column, page_addr);
  570. if (mtd->writesize > 512)
  571. host->send_cmd(host, NAND_CMD_READSTART, true);
  572. host->send_page(mtd, NFC_OUTPUT);
  573. memcpy(host->data_buf, host->main_area0, mtd->writesize);
  574. copy_spare(mtd, true);
  575. break;
  576. case NAND_CMD_SEQIN:
  577. if (column >= mtd->writesize)
  578. /* call ourself to read a page */
  579. mxc_nand_command(mtd, NAND_CMD_READ0, 0, page_addr);
  580. host->buf_start = column;
  581. host->send_cmd(host, command, false);
  582. mxc_do_addr_cycle(mtd, column, page_addr);
  583. break;
  584. case NAND_CMD_PAGEPROG:
  585. memcpy(host->main_area0, host->data_buf, mtd->writesize);
  586. copy_spare(mtd, false);
  587. host->send_page(mtd, NFC_INPUT);
  588. host->send_cmd(host, command, true);
  589. mxc_do_addr_cycle(mtd, column, page_addr);
  590. break;
  591. case NAND_CMD_READID:
  592. host->send_cmd(host, command, true);
  593. mxc_do_addr_cycle(mtd, column, page_addr);
  594. host->send_read_id(host);
  595. host->buf_start = column;
  596. break;
  597. case NAND_CMD_ERASE1:
  598. case NAND_CMD_ERASE2:
  599. host->send_cmd(host, command, false);
  600. mxc_do_addr_cycle(mtd, column, page_addr);
  601. break;
  602. }
  603. }
  604. /*
  605. * The generic flash bbt decriptors overlap with our ecc
  606. * hardware, so define some i.MX specific ones.
  607. */
  608. static uint8_t bbt_pattern[] = { 'B', 'b', 't', '0' };
  609. static uint8_t mirror_pattern[] = { '1', 't', 'b', 'B' };
  610. static struct nand_bbt_descr bbt_main_descr = {
  611. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  612. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  613. .offs = 0,
  614. .len = 4,
  615. .veroffs = 4,
  616. .maxblocks = 4,
  617. .pattern = bbt_pattern,
  618. };
  619. static struct nand_bbt_descr bbt_mirror_descr = {
  620. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  621. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  622. .offs = 0,
  623. .len = 4,
  624. .veroffs = 4,
  625. .maxblocks = 4,
  626. .pattern = mirror_pattern,
  627. };
  628. static int __init mxcnd_probe(struct platform_device *pdev)
  629. {
  630. struct nand_chip *this;
  631. struct mtd_info *mtd;
  632. struct mxc_nand_platform_data *pdata = pdev->dev.platform_data;
  633. struct mxc_nand_host *host;
  634. struct resource *res;
  635. int err = 0, nr_parts = 0;
  636. struct nand_ecclayout *oob_smallpage, *oob_largepage;
  637. /* Allocate memory for MTD device structure and private data */
  638. host = kzalloc(sizeof(struct mxc_nand_host) + NAND_MAX_PAGESIZE +
  639. NAND_MAX_OOBSIZE, GFP_KERNEL);
  640. if (!host)
  641. return -ENOMEM;
  642. host->data_buf = (uint8_t *)(host + 1);
  643. host->dev = &pdev->dev;
  644. /* structures must be linked */
  645. this = &host->nand;
  646. mtd = &host->mtd;
  647. mtd->priv = this;
  648. mtd->owner = THIS_MODULE;
  649. mtd->dev.parent = &pdev->dev;
  650. mtd->name = DRIVER_NAME;
  651. /* 50 us command delay time */
  652. this->chip_delay = 5;
  653. this->priv = host;
  654. this->dev_ready = mxc_nand_dev_ready;
  655. this->cmdfunc = mxc_nand_command;
  656. this->select_chip = mxc_nand_select_chip;
  657. this->read_byte = mxc_nand_read_byte;
  658. this->read_word = mxc_nand_read_word;
  659. this->write_buf = mxc_nand_write_buf;
  660. this->read_buf = mxc_nand_read_buf;
  661. this->verify_buf = mxc_nand_verify_buf;
  662. host->clk = clk_get(&pdev->dev, "nfc");
  663. if (IS_ERR(host->clk)) {
  664. err = PTR_ERR(host->clk);
  665. goto eclk;
  666. }
  667. clk_enable(host->clk);
  668. host->clk_act = 1;
  669. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  670. if (!res) {
  671. err = -ENODEV;
  672. goto eres;
  673. }
  674. host->base = ioremap(res->start, resource_size(res));
  675. if (!host->base) {
  676. err = -ENOMEM;
  677. goto eres;
  678. }
  679. host->main_area0 = host->base;
  680. if (nfc_is_v1() || nfc_is_v21()) {
  681. host->preset = preset_v1_v2;
  682. host->send_cmd = send_cmd_v1_v2;
  683. host->send_addr = send_addr_v1_v2;
  684. host->send_page = send_page_v1_v2;
  685. host->send_read_id = send_read_id_v1_v2;
  686. host->get_dev_status = get_dev_status_v1_v2;
  687. host->check_int = check_int_v1_v2;
  688. }
  689. if (nfc_is_v21()) {
  690. host->regs = host->base + 0x1e00;
  691. host->spare0 = host->base + 0x1000;
  692. host->spare_len = 64;
  693. oob_smallpage = &nandv2_hw_eccoob_smallpage;
  694. oob_largepage = &nandv2_hw_eccoob_largepage;
  695. this->ecc.bytes = 9;
  696. } else if (nfc_is_v1()) {
  697. host->regs = host->base + 0xe00;
  698. host->spare0 = host->base + 0x800;
  699. host->spare_len = 16;
  700. oob_smallpage = &nandv1_hw_eccoob_smallpage;
  701. oob_largepage = &nandv1_hw_eccoob_largepage;
  702. this->ecc.bytes = 3;
  703. } else
  704. BUG();
  705. this->ecc.size = 512;
  706. this->ecc.layout = oob_smallpage;
  707. if (pdata->hw_ecc) {
  708. this->ecc.calculate = mxc_nand_calculate_ecc;
  709. this->ecc.hwctl = mxc_nand_enable_hwecc;
  710. if (nfc_is_v1())
  711. this->ecc.correct = mxc_nand_correct_data_v1;
  712. else
  713. this->ecc.correct = mxc_nand_correct_data_v2_v3;
  714. this->ecc.mode = NAND_ECC_HW;
  715. } else {
  716. this->ecc.mode = NAND_ECC_SOFT;
  717. }
  718. /* NAND bus width determines access funtions used by upper layer */
  719. if (pdata->width == 2)
  720. this->options |= NAND_BUSWIDTH_16;
  721. if (pdata->flash_bbt) {
  722. this->bbt_td = &bbt_main_descr;
  723. this->bbt_md = &bbt_mirror_descr;
  724. /* update flash based bbt */
  725. this->options |= NAND_USE_FLASH_BBT;
  726. }
  727. init_waitqueue_head(&host->irq_waitq);
  728. host->irq = platform_get_irq(pdev, 0);
  729. err = request_irq(host->irq, mxc_nfc_irq, IRQF_DISABLED, DRIVER_NAME, host);
  730. if (err)
  731. goto eirq;
  732. /* first scan to find the device and get the page size */
  733. if (nand_scan_ident(mtd, 1, NULL)) {
  734. err = -ENXIO;
  735. goto escan;
  736. }
  737. /* Call preset again, with correct writesize this time */
  738. host->preset(mtd);
  739. if (mtd->writesize == 2048)
  740. this->ecc.layout = oob_largepage;
  741. /* second phase scan */
  742. if (nand_scan_tail(mtd)) {
  743. err = -ENXIO;
  744. goto escan;
  745. }
  746. /* Register the partitions */
  747. #ifdef CONFIG_MTD_PARTITIONS
  748. nr_parts =
  749. parse_mtd_partitions(mtd, part_probes, &host->parts, 0);
  750. if (nr_parts > 0)
  751. add_mtd_partitions(mtd, host->parts, nr_parts);
  752. else
  753. #endif
  754. {
  755. pr_info("Registering %s as whole device\n", mtd->name);
  756. add_mtd_device(mtd);
  757. }
  758. platform_set_drvdata(pdev, host);
  759. return 0;
  760. escan:
  761. free_irq(host->irq, host);
  762. eirq:
  763. iounmap(host->base);
  764. eres:
  765. clk_put(host->clk);
  766. eclk:
  767. kfree(host);
  768. return err;
  769. }
  770. static int __devexit mxcnd_remove(struct platform_device *pdev)
  771. {
  772. struct mxc_nand_host *host = platform_get_drvdata(pdev);
  773. clk_put(host->clk);
  774. platform_set_drvdata(pdev, NULL);
  775. nand_release(&host->mtd);
  776. free_irq(host->irq, host);
  777. iounmap(host->base);
  778. kfree(host);
  779. return 0;
  780. }
  781. static struct platform_driver mxcnd_driver = {
  782. .driver = {
  783. .name = DRIVER_NAME,
  784. },
  785. .remove = __devexit_p(mxcnd_remove),
  786. };
  787. static int __init mxc_nd_init(void)
  788. {
  789. return platform_driver_probe(&mxcnd_driver, mxcnd_probe);
  790. }
  791. static void __exit mxc_nd_cleanup(void)
  792. {
  793. /* Unregister the device structure */
  794. platform_driver_unregister(&mxcnd_driver);
  795. }
  796. module_init(mxc_nd_init);
  797. module_exit(mxc_nd_cleanup);
  798. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  799. MODULE_DESCRIPTION("MXC NAND MTD driver");
  800. MODULE_LICENSE("GPL");