mxc_nand.c 33 KB

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