mxc_nand.c 40 KB

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