mxc_nand.c 24 KB

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