mxc_nand.c 23 KB

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