mxc_nand.c 23 KB

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