mxc_nand.c 39 KB

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