mxc_nand.c 33 KB

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