mxc_nand.c 23 KB

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