mxc_nand.c 41 KB

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