mxc_nand.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  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. * layers perform a read/write buf operation,
  592. * we will used the saved column address to index into
  593. * the full page.
  594. */
  595. host->send_addr(host, 0, page_addr == -1);
  596. if (mtd->writesize > 512)
  597. /* another col addr cycle for 2k page */
  598. host->send_addr(host, 0, false);
  599. }
  600. /* Write out page address, if necessary */
  601. if (page_addr != -1) {
  602. /* paddr_0 - p_addr_7 */
  603. host->send_addr(host, (page_addr & 0xff), false);
  604. if (mtd->writesize > 512) {
  605. if (mtd->size >= 0x10000000) {
  606. /* paddr_8 - paddr_15 */
  607. host->send_addr(host, (page_addr >> 8) & 0xff, false);
  608. host->send_addr(host, (page_addr >> 16) & 0xff, true);
  609. } else
  610. /* paddr_8 - paddr_15 */
  611. host->send_addr(host, (page_addr >> 8) & 0xff, true);
  612. } else {
  613. /* One more address cycle for higher density devices */
  614. if (mtd->size >= 0x4000000) {
  615. /* paddr_8 - paddr_15 */
  616. host->send_addr(host, (page_addr >> 8) & 0xff, false);
  617. host->send_addr(host, (page_addr >> 16) & 0xff, true);
  618. } else
  619. /* paddr_8 - paddr_15 */
  620. host->send_addr(host, (page_addr >> 8) & 0xff, true);
  621. }
  622. }
  623. }
  624. /*
  625. * v2 and v3 type controllers can do 4bit or 8bit ecc depending
  626. * on how much oob the nand chip has. For 8bit ecc we need at least
  627. * 26 bytes of oob data per 512 byte block.
  628. */
  629. static int get_eccsize(struct mtd_info *mtd)
  630. {
  631. int oobbytes_per_512 = 0;
  632. oobbytes_per_512 = mtd->oobsize * 512 / mtd->writesize;
  633. if (oobbytes_per_512 < 26)
  634. return 4;
  635. else
  636. return 8;
  637. }
  638. static void preset_v1_v2(struct mtd_info *mtd)
  639. {
  640. struct nand_chip *nand_chip = mtd->priv;
  641. struct mxc_nand_host *host = nand_chip->priv;
  642. uint16_t config1 = 0;
  643. if (nand_chip->ecc.mode == NAND_ECC_HW)
  644. config1 |= NFC_V1_V2_CONFIG1_ECC_EN;
  645. if (nfc_is_v21())
  646. config1 |= NFC_V2_CONFIG1_FP_INT;
  647. if (!cpu_is_mx21())
  648. config1 |= NFC_V1_V2_CONFIG1_INT_MSK;
  649. if (nfc_is_v21() && mtd->writesize) {
  650. uint16_t pages_per_block = mtd->erasesize / mtd->writesize;
  651. host->eccsize = get_eccsize(mtd);
  652. if (host->eccsize == 4)
  653. config1 |= NFC_V2_CONFIG1_ECC_MODE_4;
  654. config1 |= NFC_V2_CONFIG1_PPB(ffs(pages_per_block) - 6);
  655. } else {
  656. host->eccsize = 1;
  657. }
  658. writew(config1, NFC_V1_V2_CONFIG1);
  659. /* preset operation */
  660. /* Unlock the internal RAM Buffer */
  661. writew(0x2, NFC_V1_V2_CONFIG);
  662. /* Blocks to be unlocked */
  663. if (nfc_is_v21()) {
  664. writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR);
  665. writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR);
  666. } else if (nfc_is_v1()) {
  667. writew(0x0, NFC_V1_UNLOCKSTART_BLKADDR);
  668. writew(0x4000, NFC_V1_UNLOCKEND_BLKADDR);
  669. } else
  670. BUG();
  671. /* Unlock Block Command for given address range */
  672. writew(0x4, NFC_V1_V2_WRPROT);
  673. }
  674. static void preset_v3(struct mtd_info *mtd)
  675. {
  676. struct nand_chip *chip = mtd->priv;
  677. struct mxc_nand_host *host = chip->priv;
  678. uint32_t config2, config3;
  679. int i, addr_phases;
  680. writel(NFC_V3_CONFIG1_RBA(0), NFC_V3_CONFIG1);
  681. writel(NFC_V3_IPC_CREQ, NFC_V3_IPC);
  682. /* Unlock the internal RAM Buffer */
  683. writel(NFC_V3_WRPROT_BLS_UNLOCK | NFC_V3_WRPROT_UNLOCK,
  684. NFC_V3_WRPROT);
  685. /* Blocks to be unlocked */
  686. for (i = 0; i < NAND_MAX_CHIPS; i++)
  687. writel(0x0 | (0xffff << 16),
  688. NFC_V3_WRPROT_UNLOCK_BLK_ADD0 + (i << 2));
  689. writel(0, NFC_V3_IPC);
  690. config2 = NFC_V3_CONFIG2_ONE_CYCLE |
  691. NFC_V3_CONFIG2_2CMD_PHASES |
  692. NFC_V3_CONFIG2_SPAS(mtd->oobsize >> 1) |
  693. NFC_V3_CONFIG2_ST_CMD(0x70) |
  694. NFC_V3_CONFIG2_INT_MSK |
  695. NFC_V3_CONFIG2_NUM_ADDR_PHASE0;
  696. if (chip->ecc.mode == NAND_ECC_HW)
  697. config2 |= NFC_V3_CONFIG2_ECC_EN;
  698. addr_phases = fls(chip->pagemask) >> 3;
  699. if (mtd->writesize == 2048) {
  700. config2 |= NFC_V3_CONFIG2_PS_2048;
  701. config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases);
  702. } else if (mtd->writesize == 4096) {
  703. config2 |= NFC_V3_CONFIG2_PS_4096;
  704. config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases);
  705. } else {
  706. config2 |= NFC_V3_CONFIG2_PS_512;
  707. config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases - 1);
  708. }
  709. if (mtd->writesize) {
  710. config2 |= NFC_V3_CONFIG2_PPB(ffs(mtd->erasesize / mtd->writesize) - 6);
  711. host->eccsize = get_eccsize(mtd);
  712. if (host->eccsize == 8)
  713. config2 |= NFC_V3_CONFIG2_ECC_MODE_8;
  714. }
  715. writel(config2, NFC_V3_CONFIG2);
  716. config3 = NFC_V3_CONFIG3_NUM_OF_DEVICES(0) |
  717. NFC_V3_CONFIG3_NO_SDMA |
  718. NFC_V3_CONFIG3_RBB_MODE |
  719. NFC_V3_CONFIG3_SBB(6) | /* Reset default */
  720. NFC_V3_CONFIG3_ADD_OP(0);
  721. if (!(chip->options & NAND_BUSWIDTH_16))
  722. config3 |= NFC_V3_CONFIG3_FW8;
  723. writel(config3, NFC_V3_CONFIG3);
  724. writel(0, NFC_V3_DELAY_LINE);
  725. }
  726. /* Used by the upper layer to write command to NAND Flash for
  727. * different operations to be carried out on NAND Flash */
  728. static void mxc_nand_command(struct mtd_info *mtd, unsigned command,
  729. int column, int page_addr)
  730. {
  731. struct nand_chip *nand_chip = mtd->priv;
  732. struct mxc_nand_host *host = nand_chip->priv;
  733. DEBUG(MTD_DEBUG_LEVEL3,
  734. "mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n",
  735. command, column, page_addr);
  736. /* Reset command state information */
  737. host->status_request = false;
  738. /* Command pre-processing step */
  739. switch (command) {
  740. case NAND_CMD_RESET:
  741. host->preset(mtd);
  742. host->send_cmd(host, command, false);
  743. break;
  744. case NAND_CMD_STATUS:
  745. host->buf_start = 0;
  746. host->status_request = true;
  747. host->send_cmd(host, command, true);
  748. mxc_do_addr_cycle(mtd, column, page_addr);
  749. break;
  750. case NAND_CMD_READ0:
  751. case NAND_CMD_READOOB:
  752. if (command == NAND_CMD_READ0)
  753. host->buf_start = column;
  754. else
  755. host->buf_start = column + mtd->writesize;
  756. command = NAND_CMD_READ0; /* only READ0 is valid */
  757. host->send_cmd(host, command, false);
  758. mxc_do_addr_cycle(mtd, column, page_addr);
  759. if (mtd->writesize > 512)
  760. host->send_cmd(host, NAND_CMD_READSTART, true);
  761. host->send_page(mtd, NFC_OUTPUT);
  762. memcpy(host->data_buf, host->main_area0, mtd->writesize);
  763. copy_spare(mtd, true);
  764. break;
  765. case NAND_CMD_SEQIN:
  766. if (column >= mtd->writesize)
  767. /* call ourself to read a page */
  768. mxc_nand_command(mtd, NAND_CMD_READ0, 0, page_addr);
  769. host->buf_start = column;
  770. host->send_cmd(host, command, false);
  771. mxc_do_addr_cycle(mtd, column, page_addr);
  772. break;
  773. case NAND_CMD_PAGEPROG:
  774. memcpy(host->main_area0, host->data_buf, mtd->writesize);
  775. copy_spare(mtd, false);
  776. host->send_page(mtd, NFC_INPUT);
  777. host->send_cmd(host, command, true);
  778. mxc_do_addr_cycle(mtd, column, page_addr);
  779. break;
  780. case NAND_CMD_READID:
  781. host->send_cmd(host, command, true);
  782. mxc_do_addr_cycle(mtd, column, page_addr);
  783. host->send_read_id(host);
  784. host->buf_start = column;
  785. break;
  786. case NAND_CMD_ERASE1:
  787. case NAND_CMD_ERASE2:
  788. host->send_cmd(host, command, false);
  789. mxc_do_addr_cycle(mtd, column, page_addr);
  790. break;
  791. }
  792. }
  793. /*
  794. * The generic flash bbt decriptors overlap with our ecc
  795. * hardware, so define some i.MX specific ones.
  796. */
  797. static uint8_t bbt_pattern[] = { 'B', 'b', 't', '0' };
  798. static uint8_t mirror_pattern[] = { '1', 't', 'b', 'B' };
  799. static struct nand_bbt_descr bbt_main_descr = {
  800. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  801. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  802. .offs = 0,
  803. .len = 4,
  804. .veroffs = 4,
  805. .maxblocks = 4,
  806. .pattern = bbt_pattern,
  807. };
  808. static struct nand_bbt_descr bbt_mirror_descr = {
  809. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  810. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  811. .offs = 0,
  812. .len = 4,
  813. .veroffs = 4,
  814. .maxblocks = 4,
  815. .pattern = mirror_pattern,
  816. };
  817. static int __init mxcnd_probe(struct platform_device *pdev)
  818. {
  819. struct nand_chip *this;
  820. struct mtd_info *mtd;
  821. struct mxc_nand_platform_data *pdata = pdev->dev.platform_data;
  822. struct mxc_nand_host *host;
  823. struct resource *res;
  824. int err = 0, nr_parts = 0;
  825. struct nand_ecclayout *oob_smallpage, *oob_largepage;
  826. /* Allocate memory for MTD device structure and private data */
  827. host = kzalloc(sizeof(struct mxc_nand_host) + NAND_MAX_PAGESIZE +
  828. NAND_MAX_OOBSIZE, GFP_KERNEL);
  829. if (!host)
  830. return -ENOMEM;
  831. host->data_buf = (uint8_t *)(host + 1);
  832. host->dev = &pdev->dev;
  833. /* structures must be linked */
  834. this = &host->nand;
  835. mtd = &host->mtd;
  836. mtd->priv = this;
  837. mtd->owner = THIS_MODULE;
  838. mtd->dev.parent = &pdev->dev;
  839. mtd->name = DRIVER_NAME;
  840. /* 50 us command delay time */
  841. this->chip_delay = 5;
  842. this->priv = host;
  843. this->dev_ready = mxc_nand_dev_ready;
  844. this->cmdfunc = mxc_nand_command;
  845. this->select_chip = mxc_nand_select_chip;
  846. this->read_byte = mxc_nand_read_byte;
  847. this->read_word = mxc_nand_read_word;
  848. this->write_buf = mxc_nand_write_buf;
  849. this->read_buf = mxc_nand_read_buf;
  850. this->verify_buf = mxc_nand_verify_buf;
  851. host->clk = clk_get(&pdev->dev, "nfc");
  852. if (IS_ERR(host->clk)) {
  853. err = PTR_ERR(host->clk);
  854. goto eclk;
  855. }
  856. clk_enable(host->clk);
  857. host->clk_act = 1;
  858. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  859. if (!res) {
  860. err = -ENODEV;
  861. goto eres;
  862. }
  863. host->base = ioremap(res->start, resource_size(res));
  864. if (!host->base) {
  865. err = -ENOMEM;
  866. goto eres;
  867. }
  868. host->main_area0 = host->base;
  869. if (nfc_is_v1() || nfc_is_v21()) {
  870. host->preset = preset_v1_v2;
  871. host->send_cmd = send_cmd_v1_v2;
  872. host->send_addr = send_addr_v1_v2;
  873. host->send_page = send_page_v1_v2;
  874. host->send_read_id = send_read_id_v1_v2;
  875. host->get_dev_status = get_dev_status_v1_v2;
  876. host->check_int = check_int_v1_v2;
  877. if (cpu_is_mx21())
  878. host->irq_control = irq_control_mx21;
  879. else
  880. host->irq_control = irq_control_v1_v2;
  881. }
  882. if (nfc_is_v21()) {
  883. host->regs = host->base + 0x1e00;
  884. host->spare0 = host->base + 0x1000;
  885. host->spare_len = 64;
  886. oob_smallpage = &nandv2_hw_eccoob_smallpage;
  887. oob_largepage = &nandv2_hw_eccoob_largepage;
  888. this->ecc.bytes = 9;
  889. } else if (nfc_is_v1()) {
  890. host->regs = host->base + 0xe00;
  891. host->spare0 = host->base + 0x800;
  892. host->spare_len = 16;
  893. oob_smallpage = &nandv1_hw_eccoob_smallpage;
  894. oob_largepage = &nandv1_hw_eccoob_largepage;
  895. this->ecc.bytes = 3;
  896. host->eccsize = 1;
  897. } else if (nfc_is_v3_2()) {
  898. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  899. if (!res) {
  900. err = -ENODEV;
  901. goto eirq;
  902. }
  903. host->regs_ip = ioremap(res->start, resource_size(res));
  904. if (!host->regs_ip) {
  905. err = -ENOMEM;
  906. goto eirq;
  907. }
  908. host->regs_axi = host->base + 0x1e00;
  909. host->spare0 = host->base + 0x1000;
  910. host->spare_len = 64;
  911. host->preset = preset_v3;
  912. host->send_cmd = send_cmd_v3;
  913. host->send_addr = send_addr_v3;
  914. host->send_page = send_page_v3;
  915. host->send_read_id = send_read_id_v3;
  916. host->check_int = check_int_v3;
  917. host->get_dev_status = get_dev_status_v3;
  918. host->irq_control = irq_control_v3;
  919. oob_smallpage = &nandv2_hw_eccoob_smallpage;
  920. oob_largepage = &nandv2_hw_eccoob_largepage;
  921. } else
  922. BUG();
  923. this->ecc.size = 512;
  924. this->ecc.layout = oob_smallpage;
  925. if (pdata->hw_ecc) {
  926. this->ecc.calculate = mxc_nand_calculate_ecc;
  927. this->ecc.hwctl = mxc_nand_enable_hwecc;
  928. if (nfc_is_v1())
  929. this->ecc.correct = mxc_nand_correct_data_v1;
  930. else
  931. this->ecc.correct = mxc_nand_correct_data_v2_v3;
  932. this->ecc.mode = NAND_ECC_HW;
  933. } else {
  934. this->ecc.mode = NAND_ECC_SOFT;
  935. }
  936. /* NAND bus width determines access funtions used by upper layer */
  937. if (pdata->width == 2)
  938. this->options |= NAND_BUSWIDTH_16;
  939. if (pdata->flash_bbt) {
  940. this->bbt_td = &bbt_main_descr;
  941. this->bbt_md = &bbt_mirror_descr;
  942. /* update flash based bbt */
  943. this->options |= NAND_USE_FLASH_BBT;
  944. }
  945. init_completion(&host->op_completion);
  946. host->irq = platform_get_irq(pdev, 0);
  947. /*
  948. * mask the interrupt. For i.MX21 explicitely call
  949. * irq_control_v1_v2 to use the mask bit. We can't call
  950. * disable_irq_nosync() for an interrupt we do not own yet.
  951. */
  952. if (cpu_is_mx21())
  953. irq_control_v1_v2(host, 0);
  954. else
  955. host->irq_control(host, 0);
  956. err = request_irq(host->irq, mxc_nfc_irq, IRQF_DISABLED, DRIVER_NAME, host);
  957. if (err)
  958. goto eirq;
  959. host->irq_control(host, 0);
  960. /*
  961. * Now that the interrupt is disabled make sure the interrupt
  962. * mask bit is cleared on i.MX21. Otherwise we can't read
  963. * the interrupt status bit on this machine.
  964. */
  965. if (cpu_is_mx21())
  966. irq_control_v1_v2(host, 1);
  967. /* first scan to find the device and get the page size */
  968. if (nand_scan_ident(mtd, 1, NULL)) {
  969. err = -ENXIO;
  970. goto escan;
  971. }
  972. /* Call preset again, with correct writesize this time */
  973. host->preset(mtd);
  974. if (mtd->writesize == 2048)
  975. this->ecc.layout = oob_largepage;
  976. /* second phase scan */
  977. if (nand_scan_tail(mtd)) {
  978. err = -ENXIO;
  979. goto escan;
  980. }
  981. /* Register the partitions */
  982. #ifdef CONFIG_MTD_PARTITIONS
  983. nr_parts =
  984. parse_mtd_partitions(mtd, part_probes, &host->parts, 0);
  985. if (nr_parts > 0)
  986. add_mtd_partitions(mtd, host->parts, nr_parts);
  987. else if (pdata->parts)
  988. add_mtd_partitions(mtd, pdata->parts, pdata->nr_parts);
  989. else
  990. #endif
  991. {
  992. pr_info("Registering %s as whole device\n", mtd->name);
  993. add_mtd_device(mtd);
  994. }
  995. platform_set_drvdata(pdev, host);
  996. return 0;
  997. escan:
  998. free_irq(host->irq, host);
  999. eirq:
  1000. if (host->regs_ip)
  1001. iounmap(host->regs_ip);
  1002. iounmap(host->base);
  1003. eres:
  1004. clk_put(host->clk);
  1005. eclk:
  1006. kfree(host);
  1007. return err;
  1008. }
  1009. static int __devexit mxcnd_remove(struct platform_device *pdev)
  1010. {
  1011. struct mxc_nand_host *host = platform_get_drvdata(pdev);
  1012. clk_put(host->clk);
  1013. platform_set_drvdata(pdev, NULL);
  1014. nand_release(&host->mtd);
  1015. free_irq(host->irq, host);
  1016. if (host->regs_ip)
  1017. iounmap(host->regs_ip);
  1018. iounmap(host->base);
  1019. kfree(host);
  1020. return 0;
  1021. }
  1022. static struct platform_driver mxcnd_driver = {
  1023. .driver = {
  1024. .name = DRIVER_NAME,
  1025. },
  1026. .remove = __devexit_p(mxcnd_remove),
  1027. };
  1028. static int __init mxc_nd_init(void)
  1029. {
  1030. return platform_driver_probe(&mxcnd_driver, mxcnd_probe);
  1031. }
  1032. static void __exit mxc_nd_cleanup(void)
  1033. {
  1034. /* Unregister the device structure */
  1035. platform_driver_unregister(&mxcnd_driver);
  1036. }
  1037. module_init(mxc_nd_init);
  1038. module_exit(mxc_nd_cleanup);
  1039. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  1040. MODULE_DESCRIPTION("MXC NAND MTD driver");
  1041. MODULE_LICENSE("GPL");