mxc_nand.c 22 KB

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