mxc_nand.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296
  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 <linux/irq.h>
  33. #include <linux/completion.h>
  34. #include <asm/mach/flash.h>
  35. #include <mach/mxc_nand.h>
  36. #include <mach/hardware.h>
  37. #define DRIVER_NAME "mxc_nand"
  38. #define nfc_is_v21() (cpu_is_mx25() || cpu_is_mx35())
  39. #define nfc_is_v1() (cpu_is_mx31() || cpu_is_mx27() || cpu_is_mx21())
  40. #define nfc_is_v3_2() cpu_is_mx51()
  41. #define nfc_is_v3() nfc_is_v3_2()
  42. /* Addresses for NFC registers */
  43. #define NFC_V1_V2_BUF_SIZE (host->regs + 0x00)
  44. #define NFC_V1_V2_BUF_ADDR (host->regs + 0x04)
  45. #define NFC_V1_V2_FLASH_ADDR (host->regs + 0x06)
  46. #define NFC_V1_V2_FLASH_CMD (host->regs + 0x08)
  47. #define NFC_V1_V2_CONFIG (host->regs + 0x0a)
  48. #define NFC_V1_V2_ECC_STATUS_RESULT (host->regs + 0x0c)
  49. #define NFC_V1_V2_RSLTMAIN_AREA (host->regs + 0x0e)
  50. #define NFC_V1_V2_RSLTSPARE_AREA (host->regs + 0x10)
  51. #define NFC_V1_V2_WRPROT (host->regs + 0x12)
  52. #define NFC_V1_UNLOCKSTART_BLKADDR (host->regs + 0x14)
  53. #define NFC_V1_UNLOCKEND_BLKADDR (host->regs + 0x16)
  54. #define NFC_V21_UNLOCKSTART_BLKADDR (host->regs + 0x20)
  55. #define NFC_V21_UNLOCKEND_BLKADDR (host->regs + 0x22)
  56. #define NFC_V1_V2_NF_WRPRST (host->regs + 0x18)
  57. #define NFC_V1_V2_CONFIG1 (host->regs + 0x1a)
  58. #define NFC_V1_V2_CONFIG2 (host->regs + 0x1c)
  59. #define NFC_V2_CONFIG1_ECC_MODE_4 (1 << 0)
  60. #define NFC_V1_V2_CONFIG1_SP_EN (1 << 2)
  61. #define NFC_V1_V2_CONFIG1_ECC_EN (1 << 3)
  62. #define NFC_V1_V2_CONFIG1_INT_MSK (1 << 4)
  63. #define NFC_V1_V2_CONFIG1_BIG (1 << 5)
  64. #define NFC_V1_V2_CONFIG1_RST (1 << 6)
  65. #define NFC_V1_V2_CONFIG1_CE (1 << 7)
  66. #define NFC_V2_CONFIG1_ONE_CYCLE (1 << 8)
  67. #define NFC_V2_CONFIG1_PPB(x) (((x) & 0x3) << 9)
  68. #define NFC_V2_CONFIG1_FP_INT (1 << 11)
  69. #define NFC_V1_V2_CONFIG2_INT (1 << 15)
  70. /*
  71. * Operation modes for the NFC. Valid for v1, v2 and v3
  72. * type controllers.
  73. */
  74. #define NFC_CMD (1 << 0)
  75. #define NFC_ADDR (1 << 1)
  76. #define NFC_INPUT (1 << 2)
  77. #define NFC_OUTPUT (1 << 3)
  78. #define NFC_ID (1 << 4)
  79. #define NFC_STATUS (1 << 5)
  80. #define NFC_V3_FLASH_CMD (host->regs_axi + 0x00)
  81. #define NFC_V3_FLASH_ADDR0 (host->regs_axi + 0x04)
  82. #define NFC_V3_CONFIG1 (host->regs_axi + 0x34)
  83. #define NFC_V3_CONFIG1_SP_EN (1 << 0)
  84. #define NFC_V3_CONFIG1_RBA(x) (((x) & 0x7 ) << 4)
  85. #define NFC_V3_ECC_STATUS_RESULT (host->regs_axi + 0x38)
  86. #define NFC_V3_LAUNCH (host->regs_axi + 0x40)
  87. #define NFC_V3_WRPROT (host->regs_ip + 0x0)
  88. #define NFC_V3_WRPROT_LOCK_TIGHT (1 << 0)
  89. #define NFC_V3_WRPROT_LOCK (1 << 1)
  90. #define NFC_V3_WRPROT_UNLOCK (1 << 2)
  91. #define NFC_V3_WRPROT_BLS_UNLOCK (2 << 6)
  92. #define NFC_V3_WRPROT_UNLOCK_BLK_ADD0 (host->regs_ip + 0x04)
  93. #define NFC_V3_CONFIG2 (host->regs_ip + 0x24)
  94. #define NFC_V3_CONFIG2_PS_512 (0 << 0)
  95. #define NFC_V3_CONFIG2_PS_2048 (1 << 0)
  96. #define NFC_V3_CONFIG2_PS_4096 (2 << 0)
  97. #define NFC_V3_CONFIG2_ONE_CYCLE (1 << 2)
  98. #define NFC_V3_CONFIG2_ECC_EN (1 << 3)
  99. #define NFC_V3_CONFIG2_2CMD_PHASES (1 << 4)
  100. #define NFC_V3_CONFIG2_NUM_ADDR_PHASE0 (1 << 5)
  101. #define NFC_V3_CONFIG2_ECC_MODE_8 (1 << 6)
  102. #define NFC_V3_CONFIG2_PPB(x) (((x) & 0x3) << 7)
  103. #define NFC_V3_CONFIG2_NUM_ADDR_PHASE1(x) (((x) & 0x3) << 12)
  104. #define NFC_V3_CONFIG2_INT_MSK (1 << 15)
  105. #define NFC_V3_CONFIG2_ST_CMD(x) (((x) & 0xff) << 24)
  106. #define NFC_V3_CONFIG2_SPAS(x) (((x) & 0xff) << 16)
  107. #define NFC_V3_CONFIG3 (host->regs_ip + 0x28)
  108. #define NFC_V3_CONFIG3_ADD_OP(x) (((x) & 0x3) << 0)
  109. #define NFC_V3_CONFIG3_FW8 (1 << 3)
  110. #define NFC_V3_CONFIG3_SBB(x) (((x) & 0x7) << 8)
  111. #define NFC_V3_CONFIG3_NUM_OF_DEVICES(x) (((x) & 0x7) << 12)
  112. #define NFC_V3_CONFIG3_RBB_MODE (1 << 15)
  113. #define NFC_V3_CONFIG3_NO_SDMA (1 << 20)
  114. #define NFC_V3_IPC (host->regs_ip + 0x2C)
  115. #define NFC_V3_IPC_CREQ (1 << 0)
  116. #define NFC_V3_IPC_INT (1 << 31)
  117. #define NFC_V3_DELAY_LINE (host->regs_ip + 0x34)
  118. struct mxc_nand_host {
  119. struct mtd_info mtd;
  120. struct nand_chip nand;
  121. struct mtd_partition *parts;
  122. struct device *dev;
  123. void *spare0;
  124. void *main_area0;
  125. void __iomem *base;
  126. void __iomem *regs;
  127. void __iomem *regs_axi;
  128. void __iomem *regs_ip;
  129. int status_request;
  130. struct clk *clk;
  131. int clk_act;
  132. int irq;
  133. int eccsize;
  134. struct completion op_completion;
  135. uint8_t *data_buf;
  136. unsigned int buf_start;
  137. int spare_len;
  138. void (*preset)(struct mtd_info *);
  139. void (*send_cmd)(struct mxc_nand_host *, uint16_t, int);
  140. void (*send_addr)(struct mxc_nand_host *, uint16_t, int);
  141. void (*send_page)(struct mtd_info *, unsigned int);
  142. void (*send_read_id)(struct mxc_nand_host *);
  143. uint16_t (*get_dev_status)(struct mxc_nand_host *);
  144. int (*check_int)(struct mxc_nand_host *);
  145. void (*irq_control)(struct mxc_nand_host *, int);
  146. };
  147. /* OOB placement block for use with hardware ecc generation */
  148. static struct nand_ecclayout nandv1_hw_eccoob_smallpage = {
  149. .eccbytes = 5,
  150. .eccpos = {6, 7, 8, 9, 10},
  151. .oobfree = {{0, 5}, {12, 4}, }
  152. };
  153. static struct nand_ecclayout nandv1_hw_eccoob_largepage = {
  154. .eccbytes = 20,
  155. .eccpos = {6, 7, 8, 9, 10, 22, 23, 24, 25, 26,
  156. 38, 39, 40, 41, 42, 54, 55, 56, 57, 58},
  157. .oobfree = {{2, 4}, {11, 10}, {27, 10}, {43, 10}, {59, 5}, }
  158. };
  159. /* OOB description for 512 byte pages with 16 byte OOB */
  160. static struct nand_ecclayout nandv2_hw_eccoob_smallpage = {
  161. .eccbytes = 1 * 9,
  162. .eccpos = {
  163. 7, 8, 9, 10, 11, 12, 13, 14, 15
  164. },
  165. .oobfree = {
  166. {.offset = 0, .length = 5}
  167. }
  168. };
  169. /* OOB description for 2048 byte pages with 64 byte OOB */
  170. static struct nand_ecclayout nandv2_hw_eccoob_largepage = {
  171. .eccbytes = 4 * 9,
  172. .eccpos = {
  173. 7, 8, 9, 10, 11, 12, 13, 14, 15,
  174. 23, 24, 25, 26, 27, 28, 29, 30, 31,
  175. 39, 40, 41, 42, 43, 44, 45, 46, 47,
  176. 55, 56, 57, 58, 59, 60, 61, 62, 63
  177. },
  178. .oobfree = {
  179. {.offset = 2, .length = 4},
  180. {.offset = 16, .length = 7},
  181. {.offset = 32, .length = 7},
  182. {.offset = 48, .length = 7}
  183. }
  184. };
  185. /* OOB description for 4096 byte pages with 128 byte OOB */
  186. static struct nand_ecclayout nandv2_hw_eccoob_4k = {
  187. .eccbytes = 8 * 9,
  188. .eccpos = {
  189. 7, 8, 9, 10, 11, 12, 13, 14, 15,
  190. 23, 24, 25, 26, 27, 28, 29, 30, 31,
  191. 39, 40, 41, 42, 43, 44, 45, 46, 47,
  192. 55, 56, 57, 58, 59, 60, 61, 62, 63,
  193. 71, 72, 73, 74, 75, 76, 77, 78, 79,
  194. 87, 88, 89, 90, 91, 92, 93, 94, 95,
  195. 103, 104, 105, 106, 107, 108, 109, 110, 111,
  196. 119, 120, 121, 122, 123, 124, 125, 126, 127,
  197. },
  198. .oobfree = {
  199. {.offset = 2, .length = 4},
  200. {.offset = 16, .length = 7},
  201. {.offset = 32, .length = 7},
  202. {.offset = 48, .length = 7},
  203. {.offset = 64, .length = 7},
  204. {.offset = 80, .length = 7},
  205. {.offset = 96, .length = 7},
  206. {.offset = 112, .length = 7},
  207. }
  208. };
  209. #ifdef CONFIG_MTD_PARTITIONS
  210. static const char *part_probes[] = { "RedBoot", "cmdlinepart", NULL };
  211. #endif
  212. static irqreturn_t mxc_nfc_irq(int irq, void *dev_id)
  213. {
  214. struct mxc_nand_host *host = dev_id;
  215. if (!host->check_int(host))
  216. return IRQ_NONE;
  217. host->irq_control(host, 0);
  218. complete(&host->op_completion);
  219. return IRQ_HANDLED;
  220. }
  221. static int check_int_v3(struct mxc_nand_host *host)
  222. {
  223. uint32_t tmp;
  224. tmp = readl(NFC_V3_IPC);
  225. if (!(tmp & NFC_V3_IPC_INT))
  226. return 0;
  227. tmp &= ~NFC_V3_IPC_INT;
  228. writel(tmp, NFC_V3_IPC);
  229. return 1;
  230. }
  231. static int check_int_v1_v2(struct mxc_nand_host *host)
  232. {
  233. uint32_t tmp;
  234. tmp = readw(NFC_V1_V2_CONFIG2);
  235. if (!(tmp & NFC_V1_V2_CONFIG2_INT))
  236. return 0;
  237. if (!cpu_is_mx21())
  238. writew(tmp & ~NFC_V1_V2_CONFIG2_INT, NFC_V1_V2_CONFIG2);
  239. return 1;
  240. }
  241. /*
  242. * It has been observed that the i.MX21 cannot read the CONFIG2:INT bit
  243. * if interrupts are masked (CONFIG1:INT_MSK is set). To handle this, the
  244. * driver can enable/disable the irq line rather than simply masking the
  245. * interrupts.
  246. */
  247. static void irq_control_mx21(struct mxc_nand_host *host, int activate)
  248. {
  249. if (activate)
  250. enable_irq(host->irq);
  251. else
  252. disable_irq_nosync(host->irq);
  253. }
  254. static void irq_control_v1_v2(struct mxc_nand_host *host, int activate)
  255. {
  256. uint16_t tmp;
  257. tmp = readw(NFC_V1_V2_CONFIG1);
  258. if (activate)
  259. tmp &= ~NFC_V1_V2_CONFIG1_INT_MSK;
  260. else
  261. tmp |= NFC_V1_V2_CONFIG1_INT_MSK;
  262. writew(tmp, NFC_V1_V2_CONFIG1);
  263. }
  264. static void irq_control_v3(struct mxc_nand_host *host, int activate)
  265. {
  266. uint32_t tmp;
  267. tmp = readl(NFC_V3_CONFIG2);
  268. if (activate)
  269. tmp &= ~NFC_V3_CONFIG2_INT_MSK;
  270. else
  271. tmp |= NFC_V3_CONFIG2_INT_MSK;
  272. writel(tmp, NFC_V3_CONFIG2);
  273. }
  274. /* This function polls the NANDFC to wait for the basic operation to
  275. * complete by checking the INT bit of config2 register.
  276. */
  277. static void wait_op_done(struct mxc_nand_host *host, int useirq)
  278. {
  279. int max_retries = 8000;
  280. if (useirq) {
  281. if (!host->check_int(host)) {
  282. INIT_COMPLETION(host->op_completion);
  283. host->irq_control(host, 1);
  284. wait_for_completion(&host->op_completion);
  285. }
  286. } else {
  287. while (max_retries-- > 0) {
  288. if (host->check_int(host))
  289. break;
  290. udelay(1);
  291. }
  292. if (max_retries < 0)
  293. DEBUG(MTD_DEBUG_LEVEL0, "%s: INT not set\n",
  294. __func__);
  295. }
  296. }
  297. static void send_cmd_v3(struct mxc_nand_host *host, uint16_t cmd, int useirq)
  298. {
  299. /* fill command */
  300. writel(cmd, NFC_V3_FLASH_CMD);
  301. /* send out command */
  302. writel(NFC_CMD, NFC_V3_LAUNCH);
  303. /* Wait for operation to complete */
  304. wait_op_done(host, useirq);
  305. }
  306. /* This function issues the specified command to the NAND device and
  307. * waits for completion. */
  308. static void send_cmd_v1_v2(struct mxc_nand_host *host, uint16_t cmd, int useirq)
  309. {
  310. DEBUG(MTD_DEBUG_LEVEL3, "send_cmd(host, 0x%x, %d)\n", cmd, useirq);
  311. writew(cmd, NFC_V1_V2_FLASH_CMD);
  312. writew(NFC_CMD, NFC_V1_V2_CONFIG2);
  313. if (cpu_is_mx21() && (cmd == NAND_CMD_RESET)) {
  314. int max_retries = 100;
  315. /* Reset completion is indicated by NFC_CONFIG2 */
  316. /* being set to 0 */
  317. while (max_retries-- > 0) {
  318. if (readw(NFC_V1_V2_CONFIG2) == 0) {
  319. break;
  320. }
  321. udelay(1);
  322. }
  323. if (max_retries < 0)
  324. DEBUG(MTD_DEBUG_LEVEL0, "%s: RESET failed\n",
  325. __func__);
  326. } else {
  327. /* Wait for operation to complete */
  328. wait_op_done(host, useirq);
  329. }
  330. }
  331. static void send_addr_v3(struct mxc_nand_host *host, uint16_t addr, int islast)
  332. {
  333. /* fill address */
  334. writel(addr, NFC_V3_FLASH_ADDR0);
  335. /* send out address */
  336. writel(NFC_ADDR, NFC_V3_LAUNCH);
  337. wait_op_done(host, 0);
  338. }
  339. /* This function sends an address (or partial address) to the
  340. * NAND device. The address is used to select the source/destination for
  341. * a NAND command. */
  342. static void send_addr_v1_v2(struct mxc_nand_host *host, uint16_t addr, int islast)
  343. {
  344. DEBUG(MTD_DEBUG_LEVEL3, "send_addr(host, 0x%x %d)\n", addr, islast);
  345. writew(addr, NFC_V1_V2_FLASH_ADDR);
  346. writew(NFC_ADDR, NFC_V1_V2_CONFIG2);
  347. /* Wait for operation to complete */
  348. wait_op_done(host, islast);
  349. }
  350. static void send_page_v3(struct mtd_info *mtd, unsigned int ops)
  351. {
  352. struct nand_chip *nand_chip = mtd->priv;
  353. struct mxc_nand_host *host = nand_chip->priv;
  354. uint32_t tmp;
  355. tmp = readl(NFC_V3_CONFIG1);
  356. tmp &= ~(7 << 4);
  357. writel(tmp, NFC_V3_CONFIG1);
  358. /* transfer data from NFC ram to nand */
  359. writel(ops, NFC_V3_LAUNCH);
  360. wait_op_done(host, false);
  361. }
  362. static void send_page_v1_v2(struct mtd_info *mtd, unsigned int ops)
  363. {
  364. struct nand_chip *nand_chip = mtd->priv;
  365. struct mxc_nand_host *host = nand_chip->priv;
  366. int bufs, i;
  367. if (nfc_is_v1() && mtd->writesize > 512)
  368. bufs = 4;
  369. else
  370. bufs = 1;
  371. for (i = 0; i < bufs; i++) {
  372. /* NANDFC buffer 0 is used for page read/write */
  373. writew(i, NFC_V1_V2_BUF_ADDR);
  374. writew(ops, NFC_V1_V2_CONFIG2);
  375. /* Wait for operation to complete */
  376. wait_op_done(host, true);
  377. }
  378. }
  379. static void send_read_id_v3(struct mxc_nand_host *host)
  380. {
  381. /* Read ID into main buffer */
  382. writel(NFC_ID, NFC_V3_LAUNCH);
  383. wait_op_done(host, true);
  384. memcpy(host->data_buf, host->main_area0, 16);
  385. }
  386. /* Request the NANDFC to perform a read of the NAND device ID. */
  387. static void send_read_id_v1_v2(struct mxc_nand_host *host)
  388. {
  389. struct nand_chip *this = &host->nand;
  390. /* NANDFC buffer 0 is used for device ID output */
  391. writew(0x0, NFC_V1_V2_BUF_ADDR);
  392. writew(NFC_ID, NFC_V1_V2_CONFIG2);
  393. /* Wait for operation to complete */
  394. wait_op_done(host, true);
  395. memcpy(host->data_buf, host->main_area0, 16);
  396. if (this->options & NAND_BUSWIDTH_16) {
  397. /* compress the ID info */
  398. host->data_buf[1] = host->data_buf[2];
  399. host->data_buf[2] = host->data_buf[4];
  400. host->data_buf[3] = host->data_buf[6];
  401. host->data_buf[4] = host->data_buf[8];
  402. host->data_buf[5] = host->data_buf[10];
  403. }
  404. }
  405. static uint16_t get_dev_status_v3(struct mxc_nand_host *host)
  406. {
  407. writew(NFC_STATUS, NFC_V3_LAUNCH);
  408. wait_op_done(host, true);
  409. return readl(NFC_V3_CONFIG1) >> 16;
  410. }
  411. /* This function requests the NANDFC to perform a read of the
  412. * NAND device status and returns the current status. */
  413. static uint16_t get_dev_status_v1_v2(struct mxc_nand_host *host)
  414. {
  415. void __iomem *main_buf = host->main_area0;
  416. uint32_t store;
  417. uint16_t ret;
  418. writew(0x0, NFC_V1_V2_BUF_ADDR);
  419. /*
  420. * The device status is stored in main_area0. To
  421. * prevent corruption of the buffer save the value
  422. * and restore it afterwards.
  423. */
  424. store = readl(main_buf);
  425. writew(NFC_STATUS, NFC_V1_V2_CONFIG2);
  426. wait_op_done(host, true);
  427. ret = readw(main_buf);
  428. writel(store, main_buf);
  429. return ret;
  430. }
  431. /* This functions is used by upper layer to checks if device is ready */
  432. static int mxc_nand_dev_ready(struct mtd_info *mtd)
  433. {
  434. /*
  435. * NFC handles R/B internally. Therefore, this function
  436. * always returns status as ready.
  437. */
  438. return 1;
  439. }
  440. static void mxc_nand_enable_hwecc(struct mtd_info *mtd, int mode)
  441. {
  442. /*
  443. * If HW ECC is enabled, we turn it on during init. There is
  444. * no need to enable again here.
  445. */
  446. }
  447. static int mxc_nand_correct_data_v1(struct mtd_info *mtd, u_char *dat,
  448. u_char *read_ecc, u_char *calc_ecc)
  449. {
  450. struct nand_chip *nand_chip = mtd->priv;
  451. struct mxc_nand_host *host = nand_chip->priv;
  452. /*
  453. * 1-Bit errors are automatically corrected in HW. No need for
  454. * additional correction. 2-Bit errors cannot be corrected by
  455. * HW ECC, so we need to return failure
  456. */
  457. uint16_t ecc_status = readw(NFC_V1_V2_ECC_STATUS_RESULT);
  458. if (((ecc_status & 0x3) == 2) || ((ecc_status >> 2) == 2)) {
  459. DEBUG(MTD_DEBUG_LEVEL0,
  460. "MXC_NAND: HWECC uncorrectable 2-bit ECC error\n");
  461. return -1;
  462. }
  463. return 0;
  464. }
  465. static int mxc_nand_correct_data_v2_v3(struct mtd_info *mtd, u_char *dat,
  466. u_char *read_ecc, u_char *calc_ecc)
  467. {
  468. struct nand_chip *nand_chip = mtd->priv;
  469. struct mxc_nand_host *host = nand_chip->priv;
  470. u32 ecc_stat, err;
  471. int no_subpages = 1;
  472. int ret = 0;
  473. u8 ecc_bit_mask, err_limit;
  474. ecc_bit_mask = (host->eccsize == 4) ? 0x7 : 0xf;
  475. err_limit = (host->eccsize == 4) ? 0x4 : 0x8;
  476. no_subpages = mtd->writesize >> 9;
  477. if (nfc_is_v21())
  478. ecc_stat = readl(NFC_V1_V2_ECC_STATUS_RESULT);
  479. else
  480. ecc_stat = readl(NFC_V3_ECC_STATUS_RESULT);
  481. do {
  482. err = ecc_stat & ecc_bit_mask;
  483. if (err > err_limit) {
  484. printk(KERN_WARNING "UnCorrectable RS-ECC Error\n");
  485. return -1;
  486. } else {
  487. ret += err;
  488. }
  489. ecc_stat >>= 4;
  490. } while (--no_subpages);
  491. mtd->ecc_stats.corrected += ret;
  492. pr_debug("%d Symbol Correctable RS-ECC Error\n", ret);
  493. return ret;
  494. }
  495. static int mxc_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
  496. u_char *ecc_code)
  497. {
  498. return 0;
  499. }
  500. static u_char mxc_nand_read_byte(struct mtd_info *mtd)
  501. {
  502. struct nand_chip *nand_chip = mtd->priv;
  503. struct mxc_nand_host *host = nand_chip->priv;
  504. uint8_t ret;
  505. /* Check for status request */
  506. if (host->status_request)
  507. return host->get_dev_status(host) & 0xFF;
  508. ret = *(uint8_t *)(host->data_buf + host->buf_start);
  509. host->buf_start++;
  510. return ret;
  511. }
  512. static uint16_t mxc_nand_read_word(struct mtd_info *mtd)
  513. {
  514. struct nand_chip *nand_chip = mtd->priv;
  515. struct mxc_nand_host *host = nand_chip->priv;
  516. uint16_t ret;
  517. ret = *(uint16_t *)(host->data_buf + host->buf_start);
  518. host->buf_start += 2;
  519. return ret;
  520. }
  521. /* Write data of length len to buffer buf. The data to be
  522. * written on NAND Flash is first copied to RAMbuffer. After the Data Input
  523. * Operation by the NFC, the data is written to NAND Flash */
  524. static void mxc_nand_write_buf(struct mtd_info *mtd,
  525. const u_char *buf, int len)
  526. {
  527. struct nand_chip *nand_chip = mtd->priv;
  528. struct mxc_nand_host *host = nand_chip->priv;
  529. u16 col = host->buf_start;
  530. int n = mtd->oobsize + mtd->writesize - col;
  531. n = min(n, len);
  532. memcpy(host->data_buf + col, buf, n);
  533. host->buf_start += n;
  534. }
  535. /* Read the data buffer from the NAND Flash. To read the data from NAND
  536. * Flash first the data output cycle is initiated by the NFC, which copies
  537. * the data to RAMbuffer. This data of length len is then copied to buffer buf.
  538. */
  539. static void mxc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  540. {
  541. struct nand_chip *nand_chip = mtd->priv;
  542. struct mxc_nand_host *host = nand_chip->priv;
  543. u16 col = host->buf_start;
  544. int n = mtd->oobsize + mtd->writesize - col;
  545. n = min(n, len);
  546. memcpy(buf, host->data_buf + col, n);
  547. host->buf_start += n;
  548. }
  549. /* Used by the upper layer to verify the data in NAND Flash
  550. * with the data in the buf. */
  551. static int mxc_nand_verify_buf(struct mtd_info *mtd,
  552. const u_char *buf, int len)
  553. {
  554. return -EFAULT;
  555. }
  556. /* This function is used by upper layer for select and
  557. * deselect of the NAND chip */
  558. static void mxc_nand_select_chip(struct mtd_info *mtd, int chip)
  559. {
  560. struct nand_chip *nand_chip = mtd->priv;
  561. struct mxc_nand_host *host = nand_chip->priv;
  562. switch (chip) {
  563. case -1:
  564. /* Disable the NFC clock */
  565. if (host->clk_act) {
  566. clk_disable(host->clk);
  567. host->clk_act = 0;
  568. }
  569. break;
  570. case 0:
  571. /* Enable the NFC clock */
  572. if (!host->clk_act) {
  573. clk_enable(host->clk);
  574. host->clk_act = 1;
  575. }
  576. break;
  577. default:
  578. break;
  579. }
  580. }
  581. /*
  582. * Function to transfer data to/from spare area.
  583. */
  584. static void copy_spare(struct mtd_info *mtd, bool bfrom)
  585. {
  586. struct nand_chip *this = mtd->priv;
  587. struct mxc_nand_host *host = this->priv;
  588. u16 i, j;
  589. u16 n = mtd->writesize >> 9;
  590. u8 *d = host->data_buf + mtd->writesize;
  591. u8 *s = host->spare0;
  592. u16 t = host->spare_len;
  593. j = (mtd->oobsize / n >> 1) << 1;
  594. if (bfrom) {
  595. for (i = 0; i < n - 1; i++)
  596. memcpy(d + i * j, s + i * t, j);
  597. /* the last section */
  598. memcpy(d + i * j, s + i * t, mtd->oobsize - i * j);
  599. } else {
  600. for (i = 0; i < n - 1; i++)
  601. memcpy(&s[i * t], &d[i * j], j);
  602. /* the last section */
  603. memcpy(&s[i * t], &d[i * j], mtd->oobsize - i * j);
  604. }
  605. }
  606. static void mxc_do_addr_cycle(struct mtd_info *mtd, int column, int page_addr)
  607. {
  608. struct nand_chip *nand_chip = mtd->priv;
  609. struct mxc_nand_host *host = nand_chip->priv;
  610. /* Write out column address, if necessary */
  611. if (column != -1) {
  612. /*
  613. * MXC NANDFC can only perform full page+spare or
  614. * spare-only read/write. When the upper layers
  615. * perform a read/write buf operation, the saved column
  616. * address is used to index into the full page.
  617. */
  618. host->send_addr(host, 0, page_addr == -1);
  619. if (mtd->writesize > 512)
  620. /* another col addr cycle for 2k page */
  621. host->send_addr(host, 0, false);
  622. }
  623. /* Write out page address, if necessary */
  624. if (page_addr != -1) {
  625. /* paddr_0 - p_addr_7 */
  626. host->send_addr(host, (page_addr & 0xff), false);
  627. if (mtd->writesize > 512) {
  628. if (mtd->size >= 0x10000000) {
  629. /* paddr_8 - paddr_15 */
  630. host->send_addr(host, (page_addr >> 8) & 0xff, false);
  631. host->send_addr(host, (page_addr >> 16) & 0xff, true);
  632. } else
  633. /* paddr_8 - paddr_15 */
  634. host->send_addr(host, (page_addr >> 8) & 0xff, true);
  635. } else {
  636. /* One more address cycle for higher density devices */
  637. if (mtd->size >= 0x4000000) {
  638. /* paddr_8 - paddr_15 */
  639. host->send_addr(host, (page_addr >> 8) & 0xff, false);
  640. host->send_addr(host, (page_addr >> 16) & 0xff, true);
  641. } else
  642. /* paddr_8 - paddr_15 */
  643. host->send_addr(host, (page_addr >> 8) & 0xff, true);
  644. }
  645. }
  646. }
  647. /*
  648. * v2 and v3 type controllers can do 4bit or 8bit ecc depending
  649. * on how much oob the nand chip has. For 8bit ecc we need at least
  650. * 26 bytes of oob data per 512 byte block.
  651. */
  652. static int get_eccsize(struct mtd_info *mtd)
  653. {
  654. int oobbytes_per_512 = 0;
  655. oobbytes_per_512 = mtd->oobsize * 512 / mtd->writesize;
  656. if (oobbytes_per_512 < 26)
  657. return 4;
  658. else
  659. return 8;
  660. }
  661. static void preset_v1_v2(struct mtd_info *mtd)
  662. {
  663. struct nand_chip *nand_chip = mtd->priv;
  664. struct mxc_nand_host *host = nand_chip->priv;
  665. uint16_t config1 = 0;
  666. if (nand_chip->ecc.mode == NAND_ECC_HW)
  667. config1 |= NFC_V1_V2_CONFIG1_ECC_EN;
  668. if (nfc_is_v21())
  669. config1 |= NFC_V2_CONFIG1_FP_INT;
  670. if (!cpu_is_mx21())
  671. config1 |= NFC_V1_V2_CONFIG1_INT_MSK;
  672. if (nfc_is_v21() && mtd->writesize) {
  673. uint16_t pages_per_block = mtd->erasesize / mtd->writesize;
  674. host->eccsize = get_eccsize(mtd);
  675. if (host->eccsize == 4)
  676. config1 |= NFC_V2_CONFIG1_ECC_MODE_4;
  677. config1 |= NFC_V2_CONFIG1_PPB(ffs(pages_per_block) - 6);
  678. } else {
  679. host->eccsize = 1;
  680. }
  681. writew(config1, NFC_V1_V2_CONFIG1);
  682. /* preset operation */
  683. /* Unlock the internal RAM Buffer */
  684. writew(0x2, NFC_V1_V2_CONFIG);
  685. /* Blocks to be unlocked */
  686. if (nfc_is_v21()) {
  687. writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR);
  688. writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR);
  689. } else if (nfc_is_v1()) {
  690. writew(0x0, NFC_V1_UNLOCKSTART_BLKADDR);
  691. writew(0x4000, NFC_V1_UNLOCKEND_BLKADDR);
  692. } else
  693. BUG();
  694. /* Unlock Block Command for given address range */
  695. writew(0x4, NFC_V1_V2_WRPROT);
  696. }
  697. static void preset_v3(struct mtd_info *mtd)
  698. {
  699. struct nand_chip *chip = mtd->priv;
  700. struct mxc_nand_host *host = chip->priv;
  701. uint32_t config2, config3;
  702. int i, addr_phases;
  703. writel(NFC_V3_CONFIG1_RBA(0), NFC_V3_CONFIG1);
  704. writel(NFC_V3_IPC_CREQ, NFC_V3_IPC);
  705. /* Unlock the internal RAM Buffer */
  706. writel(NFC_V3_WRPROT_BLS_UNLOCK | NFC_V3_WRPROT_UNLOCK,
  707. NFC_V3_WRPROT);
  708. /* Blocks to be unlocked */
  709. for (i = 0; i < NAND_MAX_CHIPS; i++)
  710. writel(0x0 | (0xffff << 16),
  711. NFC_V3_WRPROT_UNLOCK_BLK_ADD0 + (i << 2));
  712. writel(0, NFC_V3_IPC);
  713. config2 = NFC_V3_CONFIG2_ONE_CYCLE |
  714. NFC_V3_CONFIG2_2CMD_PHASES |
  715. NFC_V3_CONFIG2_SPAS(mtd->oobsize >> 1) |
  716. NFC_V3_CONFIG2_ST_CMD(0x70) |
  717. NFC_V3_CONFIG2_INT_MSK |
  718. NFC_V3_CONFIG2_NUM_ADDR_PHASE0;
  719. if (chip->ecc.mode == NAND_ECC_HW)
  720. config2 |= NFC_V3_CONFIG2_ECC_EN;
  721. addr_phases = fls(chip->pagemask) >> 3;
  722. if (mtd->writesize == 2048) {
  723. config2 |= NFC_V3_CONFIG2_PS_2048;
  724. config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases);
  725. } else if (mtd->writesize == 4096) {
  726. config2 |= NFC_V3_CONFIG2_PS_4096;
  727. config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases);
  728. } else {
  729. config2 |= NFC_V3_CONFIG2_PS_512;
  730. config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases - 1);
  731. }
  732. if (mtd->writesize) {
  733. config2 |= NFC_V3_CONFIG2_PPB(ffs(mtd->erasesize / mtd->writesize) - 6);
  734. host->eccsize = get_eccsize(mtd);
  735. if (host->eccsize == 8)
  736. config2 |= NFC_V3_CONFIG2_ECC_MODE_8;
  737. }
  738. writel(config2, NFC_V3_CONFIG2);
  739. config3 = NFC_V3_CONFIG3_NUM_OF_DEVICES(0) |
  740. NFC_V3_CONFIG3_NO_SDMA |
  741. NFC_V3_CONFIG3_RBB_MODE |
  742. NFC_V3_CONFIG3_SBB(6) | /* Reset default */
  743. NFC_V3_CONFIG3_ADD_OP(0);
  744. if (!(chip->options & NAND_BUSWIDTH_16))
  745. config3 |= NFC_V3_CONFIG3_FW8;
  746. writel(config3, NFC_V3_CONFIG3);
  747. writel(0, NFC_V3_DELAY_LINE);
  748. }
  749. /* Used by the upper layer to write command to NAND Flash for
  750. * different operations to be carried out on NAND Flash */
  751. static void mxc_nand_command(struct mtd_info *mtd, unsigned command,
  752. int column, int page_addr)
  753. {
  754. struct nand_chip *nand_chip = mtd->priv;
  755. struct mxc_nand_host *host = nand_chip->priv;
  756. DEBUG(MTD_DEBUG_LEVEL3,
  757. "mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n",
  758. command, column, page_addr);
  759. /* Reset command state information */
  760. host->status_request = false;
  761. /* Command pre-processing step */
  762. switch (command) {
  763. case NAND_CMD_RESET:
  764. host->preset(mtd);
  765. host->send_cmd(host, command, false);
  766. break;
  767. case NAND_CMD_STATUS:
  768. host->buf_start = 0;
  769. host->status_request = true;
  770. host->send_cmd(host, command, true);
  771. mxc_do_addr_cycle(mtd, column, page_addr);
  772. break;
  773. case NAND_CMD_READ0:
  774. case NAND_CMD_READOOB:
  775. if (command == NAND_CMD_READ0)
  776. host->buf_start = column;
  777. else
  778. host->buf_start = column + mtd->writesize;
  779. command = NAND_CMD_READ0; /* only READ0 is valid */
  780. host->send_cmd(host, command, false);
  781. mxc_do_addr_cycle(mtd, column, page_addr);
  782. if (mtd->writesize > 512)
  783. host->send_cmd(host, NAND_CMD_READSTART, true);
  784. host->send_page(mtd, NFC_OUTPUT);
  785. memcpy(host->data_buf, host->main_area0, mtd->writesize);
  786. copy_spare(mtd, true);
  787. break;
  788. case NAND_CMD_SEQIN:
  789. if (column >= mtd->writesize)
  790. /* call ourself to read a page */
  791. mxc_nand_command(mtd, NAND_CMD_READ0, 0, page_addr);
  792. host->buf_start = column;
  793. host->send_cmd(host, command, false);
  794. mxc_do_addr_cycle(mtd, column, page_addr);
  795. break;
  796. case NAND_CMD_PAGEPROG:
  797. memcpy(host->main_area0, host->data_buf, mtd->writesize);
  798. copy_spare(mtd, false);
  799. host->send_page(mtd, NFC_INPUT);
  800. host->send_cmd(host, command, true);
  801. mxc_do_addr_cycle(mtd, column, page_addr);
  802. break;
  803. case NAND_CMD_READID:
  804. host->send_cmd(host, command, true);
  805. mxc_do_addr_cycle(mtd, column, page_addr);
  806. host->send_read_id(host);
  807. host->buf_start = column;
  808. break;
  809. case NAND_CMD_ERASE1:
  810. case NAND_CMD_ERASE2:
  811. host->send_cmd(host, command, false);
  812. mxc_do_addr_cycle(mtd, column, page_addr);
  813. break;
  814. }
  815. }
  816. /*
  817. * The generic flash bbt decriptors overlap with our ecc
  818. * hardware, so define some i.MX specific ones.
  819. */
  820. static uint8_t bbt_pattern[] = { 'B', 'b', 't', '0' };
  821. static uint8_t mirror_pattern[] = { '1', 't', 'b', 'B' };
  822. static struct nand_bbt_descr bbt_main_descr = {
  823. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  824. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  825. .offs = 0,
  826. .len = 4,
  827. .veroffs = 4,
  828. .maxblocks = 4,
  829. .pattern = bbt_pattern,
  830. };
  831. static struct nand_bbt_descr bbt_mirror_descr = {
  832. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  833. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  834. .offs = 0,
  835. .len = 4,
  836. .veroffs = 4,
  837. .maxblocks = 4,
  838. .pattern = mirror_pattern,
  839. };
  840. static int __init mxcnd_probe(struct platform_device *pdev)
  841. {
  842. struct nand_chip *this;
  843. struct mtd_info *mtd;
  844. struct mxc_nand_platform_data *pdata = pdev->dev.platform_data;
  845. struct mxc_nand_host *host;
  846. struct resource *res;
  847. int err = 0, __maybe_unused nr_parts = 0;
  848. struct nand_ecclayout *oob_smallpage, *oob_largepage;
  849. /* Allocate memory for MTD device structure and private data */
  850. host = kzalloc(sizeof(struct mxc_nand_host) + NAND_MAX_PAGESIZE +
  851. NAND_MAX_OOBSIZE, GFP_KERNEL);
  852. if (!host)
  853. return -ENOMEM;
  854. host->data_buf = (uint8_t *)(host + 1);
  855. host->dev = &pdev->dev;
  856. /* structures must be linked */
  857. this = &host->nand;
  858. mtd = &host->mtd;
  859. mtd->priv = this;
  860. mtd->owner = THIS_MODULE;
  861. mtd->dev.parent = &pdev->dev;
  862. mtd->name = DRIVER_NAME;
  863. /* 50 us command delay time */
  864. this->chip_delay = 5;
  865. this->priv = host;
  866. this->dev_ready = mxc_nand_dev_ready;
  867. this->cmdfunc = mxc_nand_command;
  868. this->select_chip = mxc_nand_select_chip;
  869. this->read_byte = mxc_nand_read_byte;
  870. this->read_word = mxc_nand_read_word;
  871. this->write_buf = mxc_nand_write_buf;
  872. this->read_buf = mxc_nand_read_buf;
  873. this->verify_buf = mxc_nand_verify_buf;
  874. host->clk = clk_get(&pdev->dev, "nfc");
  875. if (IS_ERR(host->clk)) {
  876. err = PTR_ERR(host->clk);
  877. goto eclk;
  878. }
  879. clk_enable(host->clk);
  880. host->clk_act = 1;
  881. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  882. if (!res) {
  883. err = -ENODEV;
  884. goto eres;
  885. }
  886. host->base = ioremap(res->start, resource_size(res));
  887. if (!host->base) {
  888. err = -ENOMEM;
  889. goto eres;
  890. }
  891. host->main_area0 = host->base;
  892. if (nfc_is_v1() || nfc_is_v21()) {
  893. host->preset = preset_v1_v2;
  894. host->send_cmd = send_cmd_v1_v2;
  895. host->send_addr = send_addr_v1_v2;
  896. host->send_page = send_page_v1_v2;
  897. host->send_read_id = send_read_id_v1_v2;
  898. host->get_dev_status = get_dev_status_v1_v2;
  899. host->check_int = check_int_v1_v2;
  900. if (cpu_is_mx21())
  901. host->irq_control = irq_control_mx21;
  902. else
  903. host->irq_control = irq_control_v1_v2;
  904. }
  905. if (nfc_is_v21()) {
  906. host->regs = host->base + 0x1e00;
  907. host->spare0 = host->base + 0x1000;
  908. host->spare_len = 64;
  909. oob_smallpage = &nandv2_hw_eccoob_smallpage;
  910. oob_largepage = &nandv2_hw_eccoob_largepage;
  911. this->ecc.bytes = 9;
  912. } else if (nfc_is_v1()) {
  913. host->regs = host->base + 0xe00;
  914. host->spare0 = host->base + 0x800;
  915. host->spare_len = 16;
  916. oob_smallpage = &nandv1_hw_eccoob_smallpage;
  917. oob_largepage = &nandv1_hw_eccoob_largepage;
  918. this->ecc.bytes = 3;
  919. host->eccsize = 1;
  920. } else if (nfc_is_v3_2()) {
  921. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  922. if (!res) {
  923. err = -ENODEV;
  924. goto eirq;
  925. }
  926. host->regs_ip = ioremap(res->start, resource_size(res));
  927. if (!host->regs_ip) {
  928. err = -ENOMEM;
  929. goto eirq;
  930. }
  931. host->regs_axi = host->base + 0x1e00;
  932. host->spare0 = host->base + 0x1000;
  933. host->spare_len = 64;
  934. host->preset = preset_v3;
  935. host->send_cmd = send_cmd_v3;
  936. host->send_addr = send_addr_v3;
  937. host->send_page = send_page_v3;
  938. host->send_read_id = send_read_id_v3;
  939. host->check_int = check_int_v3;
  940. host->get_dev_status = get_dev_status_v3;
  941. host->irq_control = irq_control_v3;
  942. oob_smallpage = &nandv2_hw_eccoob_smallpage;
  943. oob_largepage = &nandv2_hw_eccoob_largepage;
  944. } else
  945. BUG();
  946. this->ecc.size = 512;
  947. this->ecc.layout = oob_smallpage;
  948. if (pdata->hw_ecc) {
  949. this->ecc.calculate = mxc_nand_calculate_ecc;
  950. this->ecc.hwctl = mxc_nand_enable_hwecc;
  951. if (nfc_is_v1())
  952. this->ecc.correct = mxc_nand_correct_data_v1;
  953. else
  954. this->ecc.correct = mxc_nand_correct_data_v2_v3;
  955. this->ecc.mode = NAND_ECC_HW;
  956. } else {
  957. this->ecc.mode = NAND_ECC_SOFT;
  958. }
  959. /* NAND bus width determines access funtions used by upper layer */
  960. if (pdata->width == 2)
  961. this->options |= NAND_BUSWIDTH_16;
  962. if (pdata->flash_bbt) {
  963. this->bbt_td = &bbt_main_descr;
  964. this->bbt_md = &bbt_mirror_descr;
  965. /* update flash based bbt */
  966. this->options |= NAND_USE_FLASH_BBT;
  967. }
  968. init_completion(&host->op_completion);
  969. host->irq = platform_get_irq(pdev, 0);
  970. /*
  971. * mask the interrupt. For i.MX21 explicitely call
  972. * irq_control_v1_v2 to use the mask bit. We can't call
  973. * disable_irq_nosync() for an interrupt we do not own yet.
  974. */
  975. if (cpu_is_mx21())
  976. irq_control_v1_v2(host, 0);
  977. else
  978. host->irq_control(host, 0);
  979. err = request_irq(host->irq, mxc_nfc_irq, IRQF_DISABLED, DRIVER_NAME, host);
  980. if (err)
  981. goto eirq;
  982. host->irq_control(host, 0);
  983. /*
  984. * Now that the interrupt is disabled make sure the interrupt
  985. * mask bit is cleared on i.MX21. Otherwise we can't read
  986. * the interrupt status bit on this machine.
  987. */
  988. if (cpu_is_mx21())
  989. irq_control_v1_v2(host, 1);
  990. /* first scan to find the device and get the page size */
  991. if (nand_scan_ident(mtd, 1, NULL)) {
  992. err = -ENXIO;
  993. goto escan;
  994. }
  995. /* Call preset again, with correct writesize this time */
  996. host->preset(mtd);
  997. if (mtd->writesize == 2048)
  998. this->ecc.layout = oob_largepage;
  999. if (nfc_is_v21() && mtd->writesize == 4096)
  1000. this->ecc.layout = &nandv2_hw_eccoob_4k;
  1001. /* second phase scan */
  1002. if (nand_scan_tail(mtd)) {
  1003. err = -ENXIO;
  1004. goto escan;
  1005. }
  1006. /* Register the partitions */
  1007. #ifdef CONFIG_MTD_PARTITIONS
  1008. nr_parts =
  1009. parse_mtd_partitions(mtd, part_probes, &host->parts, 0);
  1010. if (nr_parts > 0)
  1011. add_mtd_partitions(mtd, host->parts, nr_parts);
  1012. else if (pdata->parts)
  1013. add_mtd_partitions(mtd, pdata->parts, pdata->nr_parts);
  1014. else
  1015. #endif
  1016. {
  1017. pr_info("Registering %s as whole device\n", mtd->name);
  1018. add_mtd_device(mtd);
  1019. }
  1020. platform_set_drvdata(pdev, host);
  1021. return 0;
  1022. escan:
  1023. free_irq(host->irq, host);
  1024. eirq:
  1025. if (host->regs_ip)
  1026. iounmap(host->regs_ip);
  1027. iounmap(host->base);
  1028. eres:
  1029. clk_put(host->clk);
  1030. eclk:
  1031. kfree(host);
  1032. return err;
  1033. }
  1034. static int __devexit mxcnd_remove(struct platform_device *pdev)
  1035. {
  1036. struct mxc_nand_host *host = platform_get_drvdata(pdev);
  1037. clk_put(host->clk);
  1038. platform_set_drvdata(pdev, NULL);
  1039. nand_release(&host->mtd);
  1040. free_irq(host->irq, host);
  1041. if (host->regs_ip)
  1042. iounmap(host->regs_ip);
  1043. iounmap(host->base);
  1044. kfree(host);
  1045. return 0;
  1046. }
  1047. static struct platform_driver mxcnd_driver = {
  1048. .driver = {
  1049. .name = DRIVER_NAME,
  1050. },
  1051. .remove = __devexit_p(mxcnd_remove),
  1052. };
  1053. static int __init mxc_nd_init(void)
  1054. {
  1055. return platform_driver_probe(&mxcnd_driver, mxcnd_probe);
  1056. }
  1057. static void __exit mxc_nd_cleanup(void)
  1058. {
  1059. /* Unregister the device structure */
  1060. platform_driver_unregister(&mxcnd_driver);
  1061. }
  1062. module_init(mxc_nd_init);
  1063. module_exit(mxc_nd_cleanup);
  1064. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  1065. MODULE_DESCRIPTION("MXC NAND MTD driver");
  1066. MODULE_LICENSE("GPL");