mxc_nand.c 32 KB

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