mxc_nand.c 43 KB

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