mxc_nand.c 22 KB

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