mxc_nand.c 22 KB

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