mxc_nand.c 23 KB

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