mxc_nand.c 41 KB

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