mxc_nand.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853
  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/rawnand.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 <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. const struct mtd_ooblayout_ops *ooblayout;
  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. int (*setup_data_interface)(struct mtd_info *mtd, int csline,
  137. const struct nand_data_interface *conf);
  138. /*
  139. * On i.MX21 the CONFIG2:INT bit cannot be read if interrupts are masked
  140. * (CONFIG1:INT_MSK is set). To handle this the driver uses
  141. * enable_irq/disable_irq_nosync instead of CONFIG1:INT_MSK
  142. */
  143. int irqpending_quirk;
  144. int needs_ip;
  145. size_t regs_offset;
  146. size_t spare0_offset;
  147. size_t axi_offset;
  148. int spare_len;
  149. int eccbytes;
  150. int eccsize;
  151. int ppb_shift;
  152. };
  153. struct mxc_nand_host {
  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 used_oobsize;
  168. int active_cs;
  169. struct completion op_completion;
  170. uint8_t *data_buf;
  171. unsigned int buf_start;
  172. const struct mxc_nand_devtype_data *devtype_data;
  173. struct mxc_nand_platform_data pdata;
  174. };
  175. static const char * const part_probes[] = {
  176. "cmdlinepart", "RedBoot", "ofpart", NULL };
  177. static void memcpy32_fromio(void *trg, const void __iomem *src, size_t size)
  178. {
  179. int i;
  180. u32 *t = trg;
  181. const __iomem u32 *s = src;
  182. for (i = 0; i < (size >> 2); i++)
  183. *t++ = __raw_readl(s++);
  184. }
  185. static void memcpy16_fromio(void *trg, const void __iomem *src, size_t size)
  186. {
  187. int i;
  188. u16 *t = trg;
  189. const __iomem u16 *s = src;
  190. /* We assume that src (IO) is always 32bit aligned */
  191. if (PTR_ALIGN(trg, 4) == trg && IS_ALIGNED(size, 4)) {
  192. memcpy32_fromio(trg, src, size);
  193. return;
  194. }
  195. for (i = 0; i < (size >> 1); i++)
  196. *t++ = __raw_readw(s++);
  197. }
  198. static inline void memcpy32_toio(void __iomem *trg, const void *src, int size)
  199. {
  200. /* __iowrite32_copy use 32bit size values so divide by 4 */
  201. __iowrite32_copy(trg, src, size / 4);
  202. }
  203. static void memcpy16_toio(void __iomem *trg, const void *src, int size)
  204. {
  205. int i;
  206. __iomem u16 *t = trg;
  207. const u16 *s = src;
  208. /* We assume that trg (IO) is always 32bit aligned */
  209. if (PTR_ALIGN(src, 4) == src && IS_ALIGNED(size, 4)) {
  210. memcpy32_toio(trg, src, size);
  211. return;
  212. }
  213. for (i = 0; i < (size >> 1); i++)
  214. __raw_writew(*s++, t++);
  215. }
  216. static int check_int_v3(struct mxc_nand_host *host)
  217. {
  218. uint32_t tmp;
  219. tmp = readl(NFC_V3_IPC);
  220. if (!(tmp & NFC_V3_IPC_INT))
  221. return 0;
  222. tmp &= ~NFC_V3_IPC_INT;
  223. writel(tmp, NFC_V3_IPC);
  224. return 1;
  225. }
  226. static int check_int_v1_v2(struct mxc_nand_host *host)
  227. {
  228. uint32_t tmp;
  229. tmp = readw(NFC_V1_V2_CONFIG2);
  230. if (!(tmp & NFC_V1_V2_CONFIG2_INT))
  231. return 0;
  232. if (!host->devtype_data->irqpending_quirk)
  233. writew(tmp & ~NFC_V1_V2_CONFIG2_INT, NFC_V1_V2_CONFIG2);
  234. return 1;
  235. }
  236. static void irq_control_v1_v2(struct mxc_nand_host *host, int activate)
  237. {
  238. uint16_t tmp;
  239. tmp = readw(NFC_V1_V2_CONFIG1);
  240. if (activate)
  241. tmp &= ~NFC_V1_V2_CONFIG1_INT_MSK;
  242. else
  243. tmp |= NFC_V1_V2_CONFIG1_INT_MSK;
  244. writew(tmp, NFC_V1_V2_CONFIG1);
  245. }
  246. static void irq_control_v3(struct mxc_nand_host *host, int activate)
  247. {
  248. uint32_t tmp;
  249. tmp = readl(NFC_V3_CONFIG2);
  250. if (activate)
  251. tmp &= ~NFC_V3_CONFIG2_INT_MSK;
  252. else
  253. tmp |= NFC_V3_CONFIG2_INT_MSK;
  254. writel(tmp, NFC_V3_CONFIG2);
  255. }
  256. static void irq_control(struct mxc_nand_host *host, int activate)
  257. {
  258. if (host->devtype_data->irqpending_quirk) {
  259. if (activate)
  260. enable_irq(host->irq);
  261. else
  262. disable_irq_nosync(host->irq);
  263. } else {
  264. host->devtype_data->irq_control(host, activate);
  265. }
  266. }
  267. static u32 get_ecc_status_v1(struct mxc_nand_host *host)
  268. {
  269. return readw(NFC_V1_V2_ECC_STATUS_RESULT);
  270. }
  271. static u32 get_ecc_status_v2(struct mxc_nand_host *host)
  272. {
  273. return readl(NFC_V1_V2_ECC_STATUS_RESULT);
  274. }
  275. static u32 get_ecc_status_v3(struct mxc_nand_host *host)
  276. {
  277. return readl(NFC_V3_ECC_STATUS_RESULT);
  278. }
  279. static irqreturn_t mxc_nfc_irq(int irq, void *dev_id)
  280. {
  281. struct mxc_nand_host *host = dev_id;
  282. if (!host->devtype_data->check_int(host))
  283. return IRQ_NONE;
  284. irq_control(host, 0);
  285. complete(&host->op_completion);
  286. return IRQ_HANDLED;
  287. }
  288. /* This function polls the NANDFC to wait for the basic operation to
  289. * complete by checking the INT bit of config2 register.
  290. */
  291. static int wait_op_done(struct mxc_nand_host *host, int useirq)
  292. {
  293. int ret = 0;
  294. /*
  295. * If operation is already complete, don't bother to setup an irq or a
  296. * loop.
  297. */
  298. if (host->devtype_data->check_int(host))
  299. return 0;
  300. if (useirq) {
  301. unsigned long timeout;
  302. reinit_completion(&host->op_completion);
  303. irq_control(host, 1);
  304. timeout = wait_for_completion_timeout(&host->op_completion, HZ);
  305. if (!timeout && !host->devtype_data->check_int(host)) {
  306. dev_dbg(host->dev, "timeout waiting for irq\n");
  307. ret = -ETIMEDOUT;
  308. }
  309. } else {
  310. int max_retries = 8000;
  311. int done;
  312. do {
  313. udelay(1);
  314. done = host->devtype_data->check_int(host);
  315. if (done)
  316. break;
  317. } while (--max_retries);
  318. if (!done) {
  319. dev_dbg(host->dev, "timeout polling for completion\n");
  320. ret = -ETIMEDOUT;
  321. }
  322. }
  323. WARN_ONCE(ret < 0, "timeout! useirq=%d\n", useirq);
  324. return ret;
  325. }
  326. static void send_cmd_v3(struct mxc_nand_host *host, uint16_t cmd, int useirq)
  327. {
  328. /* fill command */
  329. writel(cmd, NFC_V3_FLASH_CMD);
  330. /* send out command */
  331. writel(NFC_CMD, NFC_V3_LAUNCH);
  332. /* Wait for operation to complete */
  333. wait_op_done(host, useirq);
  334. }
  335. /* This function issues the specified command to the NAND device and
  336. * waits for completion. */
  337. static void send_cmd_v1_v2(struct mxc_nand_host *host, uint16_t cmd, int useirq)
  338. {
  339. pr_debug("send_cmd(host, 0x%x, %d)\n", cmd, useirq);
  340. writew(cmd, NFC_V1_V2_FLASH_CMD);
  341. writew(NFC_CMD, NFC_V1_V2_CONFIG2);
  342. if (host->devtype_data->irqpending_quirk && (cmd == NAND_CMD_RESET)) {
  343. int max_retries = 100;
  344. /* Reset completion is indicated by NFC_CONFIG2 */
  345. /* being set to 0 */
  346. while (max_retries-- > 0) {
  347. if (readw(NFC_V1_V2_CONFIG2) == 0) {
  348. break;
  349. }
  350. udelay(1);
  351. }
  352. if (max_retries < 0)
  353. pr_debug("%s: RESET failed\n", __func__);
  354. } else {
  355. /* Wait for operation to complete */
  356. wait_op_done(host, useirq);
  357. }
  358. }
  359. static void send_addr_v3(struct mxc_nand_host *host, uint16_t addr, int islast)
  360. {
  361. /* fill address */
  362. writel(addr, NFC_V3_FLASH_ADDR0);
  363. /* send out address */
  364. writel(NFC_ADDR, NFC_V3_LAUNCH);
  365. wait_op_done(host, 0);
  366. }
  367. /* This function sends an address (or partial address) to the
  368. * NAND device. The address is used to select the source/destination for
  369. * a NAND command. */
  370. static void send_addr_v1_v2(struct mxc_nand_host *host, uint16_t addr, int islast)
  371. {
  372. pr_debug("send_addr(host, 0x%x %d)\n", addr, islast);
  373. writew(addr, NFC_V1_V2_FLASH_ADDR);
  374. writew(NFC_ADDR, NFC_V1_V2_CONFIG2);
  375. /* Wait for operation to complete */
  376. wait_op_done(host, islast);
  377. }
  378. static void send_page_v3(struct mtd_info *mtd, unsigned int ops)
  379. {
  380. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  381. struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
  382. uint32_t tmp;
  383. tmp = readl(NFC_V3_CONFIG1);
  384. tmp &= ~(7 << 4);
  385. writel(tmp, NFC_V3_CONFIG1);
  386. /* transfer data from NFC ram to nand */
  387. writel(ops, NFC_V3_LAUNCH);
  388. wait_op_done(host, false);
  389. }
  390. static void send_page_v2(struct mtd_info *mtd, unsigned int ops)
  391. {
  392. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  393. struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
  394. /* NANDFC buffer 0 is used for page read/write */
  395. writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR);
  396. writew(ops, NFC_V1_V2_CONFIG2);
  397. /* Wait for operation to complete */
  398. wait_op_done(host, true);
  399. }
  400. static void send_page_v1(struct mtd_info *mtd, unsigned int ops)
  401. {
  402. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  403. struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
  404. int bufs, i;
  405. if (mtd->writesize > 512)
  406. bufs = 4;
  407. else
  408. bufs = 1;
  409. for (i = 0; i < bufs; i++) {
  410. /* NANDFC buffer 0 is used for page read/write */
  411. writew((host->active_cs << 4) | i, NFC_V1_V2_BUF_ADDR);
  412. writew(ops, NFC_V1_V2_CONFIG2);
  413. /* Wait for operation to complete */
  414. wait_op_done(host, true);
  415. }
  416. }
  417. static void send_read_id_v3(struct mxc_nand_host *host)
  418. {
  419. /* Read ID into main buffer */
  420. writel(NFC_ID, NFC_V3_LAUNCH);
  421. wait_op_done(host, true);
  422. memcpy32_fromio(host->data_buf, host->main_area0, 16);
  423. }
  424. /* Request the NANDFC to perform a read of the NAND device ID. */
  425. static void send_read_id_v1_v2(struct mxc_nand_host *host)
  426. {
  427. /* NANDFC buffer 0 is used for device ID output */
  428. writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR);
  429. writew(NFC_ID, NFC_V1_V2_CONFIG2);
  430. /* Wait for operation to complete */
  431. wait_op_done(host, true);
  432. memcpy32_fromio(host->data_buf, host->main_area0, 16);
  433. }
  434. static uint16_t get_dev_status_v3(struct mxc_nand_host *host)
  435. {
  436. writew(NFC_STATUS, NFC_V3_LAUNCH);
  437. wait_op_done(host, true);
  438. return readl(NFC_V3_CONFIG1) >> 16;
  439. }
  440. /* This function requests the NANDFC to perform a read of the
  441. * NAND device status and returns the current status. */
  442. static uint16_t get_dev_status_v1_v2(struct mxc_nand_host *host)
  443. {
  444. void __iomem *main_buf = host->main_area0;
  445. uint32_t store;
  446. uint16_t ret;
  447. writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR);
  448. /*
  449. * The device status is stored in main_area0. To
  450. * prevent corruption of the buffer save the value
  451. * and restore it afterwards.
  452. */
  453. store = readl(main_buf);
  454. writew(NFC_STATUS, NFC_V1_V2_CONFIG2);
  455. wait_op_done(host, true);
  456. ret = readw(main_buf);
  457. writel(store, main_buf);
  458. return ret;
  459. }
  460. /* This functions is used by upper layer to checks if device is ready */
  461. static int mxc_nand_dev_ready(struct mtd_info *mtd)
  462. {
  463. /*
  464. * NFC handles R/B internally. Therefore, this function
  465. * always returns status as ready.
  466. */
  467. return 1;
  468. }
  469. static void mxc_nand_enable_hwecc(struct mtd_info *mtd, int mode)
  470. {
  471. /*
  472. * If HW ECC is enabled, we turn it on during init. There is
  473. * no need to enable again here.
  474. */
  475. }
  476. static int mxc_nand_correct_data_v1(struct mtd_info *mtd, u_char *dat,
  477. u_char *read_ecc, u_char *calc_ecc)
  478. {
  479. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  480. struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
  481. /*
  482. * 1-Bit errors are automatically corrected in HW. No need for
  483. * additional correction. 2-Bit errors cannot be corrected by
  484. * HW ECC, so we need to return failure
  485. */
  486. uint16_t ecc_status = get_ecc_status_v1(host);
  487. if (((ecc_status & 0x3) == 2) || ((ecc_status >> 2) == 2)) {
  488. pr_debug("MXC_NAND: HWECC uncorrectable 2-bit ECC error\n");
  489. return -EBADMSG;
  490. }
  491. return 0;
  492. }
  493. static int mxc_nand_correct_data_v2_v3(struct mtd_info *mtd, u_char *dat,
  494. u_char *read_ecc, u_char *calc_ecc)
  495. {
  496. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  497. struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
  498. u32 ecc_stat, err;
  499. int no_subpages = 1;
  500. int ret = 0;
  501. u8 ecc_bit_mask, err_limit;
  502. ecc_bit_mask = (host->eccsize == 4) ? 0x7 : 0xf;
  503. err_limit = (host->eccsize == 4) ? 0x4 : 0x8;
  504. no_subpages = mtd->writesize >> 9;
  505. ecc_stat = host->devtype_data->get_ecc_status(host);
  506. do {
  507. err = ecc_stat & ecc_bit_mask;
  508. if (err > err_limit) {
  509. printk(KERN_WARNING "UnCorrectable RS-ECC Error\n");
  510. return -EBADMSG;
  511. } else {
  512. ret += err;
  513. }
  514. ecc_stat >>= 4;
  515. } while (--no_subpages);
  516. pr_debug("%d Symbol Correctable RS-ECC Error\n", ret);
  517. return ret;
  518. }
  519. static int mxc_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
  520. u_char *ecc_code)
  521. {
  522. return 0;
  523. }
  524. static u_char mxc_nand_read_byte(struct mtd_info *mtd)
  525. {
  526. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  527. struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
  528. uint8_t ret;
  529. /* Check for status request */
  530. if (host->status_request)
  531. return host->devtype_data->get_dev_status(host) & 0xFF;
  532. if (nand_chip->options & NAND_BUSWIDTH_16) {
  533. /* only take the lower byte of each word */
  534. ret = *(uint16_t *)(host->data_buf + host->buf_start);
  535. host->buf_start += 2;
  536. } else {
  537. ret = *(uint8_t *)(host->data_buf + host->buf_start);
  538. host->buf_start++;
  539. }
  540. pr_debug("%s: ret=0x%hhx (start=%u)\n", __func__, ret, host->buf_start);
  541. return ret;
  542. }
  543. static uint16_t mxc_nand_read_word(struct mtd_info *mtd)
  544. {
  545. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  546. struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
  547. uint16_t ret;
  548. ret = *(uint16_t *)(host->data_buf + host->buf_start);
  549. host->buf_start += 2;
  550. return ret;
  551. }
  552. /* Write data of length len to buffer buf. The data to be
  553. * written on NAND Flash is first copied to RAMbuffer. After the Data Input
  554. * Operation by the NFC, the data is written to NAND Flash */
  555. static void mxc_nand_write_buf(struct mtd_info *mtd,
  556. const u_char *buf, int len)
  557. {
  558. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  559. struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
  560. u16 col = host->buf_start;
  561. int n = mtd->oobsize + mtd->writesize - col;
  562. n = min(n, len);
  563. memcpy(host->data_buf + col, buf, n);
  564. host->buf_start += n;
  565. }
  566. /* Read the data buffer from the NAND Flash. To read the data from NAND
  567. * Flash first the data output cycle is initiated by the NFC, which copies
  568. * the data to RAMbuffer. This data of length len is then copied to buffer buf.
  569. */
  570. static void mxc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  571. {
  572. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  573. struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
  574. u16 col = host->buf_start;
  575. int n = mtd->oobsize + mtd->writesize - col;
  576. n = min(n, len);
  577. memcpy(buf, host->data_buf + col, n);
  578. host->buf_start += n;
  579. }
  580. /* This function is used by upper layer for select and
  581. * deselect of the NAND chip */
  582. static void mxc_nand_select_chip_v1_v3(struct mtd_info *mtd, int chip)
  583. {
  584. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  585. struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
  586. if (chip == -1) {
  587. /* Disable the NFC clock */
  588. if (host->clk_act) {
  589. clk_disable_unprepare(host->clk);
  590. host->clk_act = 0;
  591. }
  592. return;
  593. }
  594. if (!host->clk_act) {
  595. /* Enable the NFC clock */
  596. clk_prepare_enable(host->clk);
  597. host->clk_act = 1;
  598. }
  599. }
  600. static void mxc_nand_select_chip_v2(struct mtd_info *mtd, int chip)
  601. {
  602. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  603. struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
  604. if (chip == -1) {
  605. /* Disable the NFC clock */
  606. if (host->clk_act) {
  607. clk_disable_unprepare(host->clk);
  608. host->clk_act = 0;
  609. }
  610. return;
  611. }
  612. if (!host->clk_act) {
  613. /* Enable the NFC clock */
  614. clk_prepare_enable(host->clk);
  615. host->clk_act = 1;
  616. }
  617. host->active_cs = chip;
  618. writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR);
  619. }
  620. /*
  621. * The controller splits a page into data chunks of 512 bytes + partial oob.
  622. * There are writesize / 512 such chunks, the size of the partial oob parts is
  623. * oobsize / #chunks rounded down to a multiple of 2. The last oob chunk then
  624. * contains additionally the byte lost by rounding (if any).
  625. * This function handles the needed shuffling between host->data_buf (which
  626. * holds a page in natural order, i.e. writesize bytes data + oobsize bytes
  627. * spare) and the NFC buffer.
  628. */
  629. static void copy_spare(struct mtd_info *mtd, bool bfrom)
  630. {
  631. struct nand_chip *this = mtd_to_nand(mtd);
  632. struct mxc_nand_host *host = nand_get_controller_data(this);
  633. u16 i, oob_chunk_size;
  634. u16 num_chunks = mtd->writesize / 512;
  635. u8 *d = host->data_buf + mtd->writesize;
  636. u8 __iomem *s = host->spare0;
  637. u16 sparebuf_size = host->devtype_data->spare_len;
  638. /* size of oob chunk for all but possibly the last one */
  639. oob_chunk_size = (host->used_oobsize / num_chunks) & ~1;
  640. if (bfrom) {
  641. for (i = 0; i < num_chunks - 1; i++)
  642. memcpy16_fromio(d + i * oob_chunk_size,
  643. s + i * sparebuf_size,
  644. oob_chunk_size);
  645. /* the last chunk */
  646. memcpy16_fromio(d + i * oob_chunk_size,
  647. s + i * sparebuf_size,
  648. host->used_oobsize - i * oob_chunk_size);
  649. } else {
  650. for (i = 0; i < num_chunks - 1; i++)
  651. memcpy16_toio(&s[i * sparebuf_size],
  652. &d[i * oob_chunk_size],
  653. oob_chunk_size);
  654. /* the last chunk */
  655. memcpy16_toio(&s[i * sparebuf_size],
  656. &d[i * oob_chunk_size],
  657. host->used_oobsize - i * oob_chunk_size);
  658. }
  659. }
  660. /*
  661. * MXC NANDFC can only perform full page+spare or spare-only read/write. When
  662. * the upper layers perform a read/write buf operation, the saved column address
  663. * is used to index into the full page. So usually this function is called with
  664. * column == 0 (unless no column cycle is needed indicated by column == -1)
  665. */
  666. static void mxc_do_addr_cycle(struct mtd_info *mtd, int column, int page_addr)
  667. {
  668. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  669. struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
  670. /* Write out column address, if necessary */
  671. if (column != -1) {
  672. host->devtype_data->send_addr(host, column & 0xff,
  673. page_addr == -1);
  674. if (mtd->writesize > 512)
  675. /* another col addr cycle for 2k page */
  676. host->devtype_data->send_addr(host,
  677. (column >> 8) & 0xff,
  678. false);
  679. }
  680. /* Write out page address, if necessary */
  681. if (page_addr != -1) {
  682. /* paddr_0 - p_addr_7 */
  683. host->devtype_data->send_addr(host, (page_addr & 0xff), false);
  684. if (mtd->writesize > 512) {
  685. if (mtd->size >= 0x10000000) {
  686. /* paddr_8 - paddr_15 */
  687. host->devtype_data->send_addr(host,
  688. (page_addr >> 8) & 0xff,
  689. false);
  690. host->devtype_data->send_addr(host,
  691. (page_addr >> 16) & 0xff,
  692. true);
  693. } else
  694. /* paddr_8 - paddr_15 */
  695. host->devtype_data->send_addr(host,
  696. (page_addr >> 8) & 0xff, true);
  697. } else {
  698. /* One more address cycle for higher density devices */
  699. if (mtd->size >= 0x4000000) {
  700. /* paddr_8 - paddr_15 */
  701. host->devtype_data->send_addr(host,
  702. (page_addr >> 8) & 0xff,
  703. false);
  704. host->devtype_data->send_addr(host,
  705. (page_addr >> 16) & 0xff,
  706. true);
  707. } else
  708. /* paddr_8 - paddr_15 */
  709. host->devtype_data->send_addr(host,
  710. (page_addr >> 8) & 0xff, true);
  711. }
  712. }
  713. }
  714. static int mxc_v1_ooblayout_ecc(struct mtd_info *mtd, int section,
  715. struct mtd_oob_region *oobregion)
  716. {
  717. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  718. if (section >= nand_chip->ecc.steps)
  719. return -ERANGE;
  720. oobregion->offset = (section * 16) + 6;
  721. oobregion->length = nand_chip->ecc.bytes;
  722. return 0;
  723. }
  724. static int mxc_v1_ooblayout_free(struct mtd_info *mtd, int section,
  725. struct mtd_oob_region *oobregion)
  726. {
  727. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  728. if (section > nand_chip->ecc.steps)
  729. return -ERANGE;
  730. if (!section) {
  731. if (mtd->writesize <= 512) {
  732. oobregion->offset = 0;
  733. oobregion->length = 5;
  734. } else {
  735. oobregion->offset = 2;
  736. oobregion->length = 4;
  737. }
  738. } else {
  739. oobregion->offset = ((section - 1) * 16) +
  740. nand_chip->ecc.bytes + 6;
  741. if (section < nand_chip->ecc.steps)
  742. oobregion->length = (section * 16) + 6 -
  743. oobregion->offset;
  744. else
  745. oobregion->length = mtd->oobsize - oobregion->offset;
  746. }
  747. return 0;
  748. }
  749. static const struct mtd_ooblayout_ops mxc_v1_ooblayout_ops = {
  750. .ecc = mxc_v1_ooblayout_ecc,
  751. .free = mxc_v1_ooblayout_free,
  752. };
  753. static int mxc_v2_ooblayout_ecc(struct mtd_info *mtd, int section,
  754. struct mtd_oob_region *oobregion)
  755. {
  756. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  757. int stepsize = nand_chip->ecc.bytes == 9 ? 16 : 26;
  758. if (section >= nand_chip->ecc.steps)
  759. return -ERANGE;
  760. oobregion->offset = (section * stepsize) + 7;
  761. oobregion->length = nand_chip->ecc.bytes;
  762. return 0;
  763. }
  764. static int mxc_v2_ooblayout_free(struct mtd_info *mtd, int section,
  765. struct mtd_oob_region *oobregion)
  766. {
  767. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  768. int stepsize = nand_chip->ecc.bytes == 9 ? 16 : 26;
  769. if (section >= nand_chip->ecc.steps)
  770. return -ERANGE;
  771. if (!section) {
  772. if (mtd->writesize <= 512) {
  773. oobregion->offset = 0;
  774. oobregion->length = 5;
  775. } else {
  776. oobregion->offset = 2;
  777. oobregion->length = 4;
  778. }
  779. } else {
  780. oobregion->offset = section * stepsize;
  781. oobregion->length = 7;
  782. }
  783. return 0;
  784. }
  785. static const struct mtd_ooblayout_ops mxc_v2_ooblayout_ops = {
  786. .ecc = mxc_v2_ooblayout_ecc,
  787. .free = mxc_v2_ooblayout_free,
  788. };
  789. /*
  790. * v2 and v3 type controllers can do 4bit or 8bit ecc depending
  791. * on how much oob the nand chip has. For 8bit ecc we need at least
  792. * 26 bytes of oob data per 512 byte block.
  793. */
  794. static int get_eccsize(struct mtd_info *mtd)
  795. {
  796. int oobbytes_per_512 = 0;
  797. oobbytes_per_512 = mtd->oobsize * 512 / mtd->writesize;
  798. if (oobbytes_per_512 < 26)
  799. return 4;
  800. else
  801. return 8;
  802. }
  803. static void preset_v1(struct mtd_info *mtd)
  804. {
  805. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  806. struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
  807. uint16_t config1 = 0;
  808. if (nand_chip->ecc.mode == NAND_ECC_HW && mtd->writesize)
  809. config1 |= NFC_V1_V2_CONFIG1_ECC_EN;
  810. if (!host->devtype_data->irqpending_quirk)
  811. config1 |= NFC_V1_V2_CONFIG1_INT_MSK;
  812. host->eccsize = 1;
  813. writew(config1, NFC_V1_V2_CONFIG1);
  814. /* preset operation */
  815. /* Unlock the internal RAM Buffer */
  816. writew(0x2, NFC_V1_V2_CONFIG);
  817. /* Blocks to be unlocked */
  818. writew(0x0, NFC_V1_UNLOCKSTART_BLKADDR);
  819. writew(0xffff, NFC_V1_UNLOCKEND_BLKADDR);
  820. /* Unlock Block Command for given address range */
  821. writew(0x4, NFC_V1_V2_WRPROT);
  822. }
  823. static int mxc_nand_v2_setup_data_interface(struct mtd_info *mtd, int csline,
  824. const struct nand_data_interface *conf)
  825. {
  826. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  827. struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
  828. int tRC_min_ns, tRC_ps, ret;
  829. unsigned long rate, rate_round;
  830. const struct nand_sdr_timings *timings;
  831. u16 config1;
  832. timings = nand_get_sdr_timings(conf);
  833. if (IS_ERR(timings))
  834. return -ENOTSUPP;
  835. config1 = readw(NFC_V1_V2_CONFIG1);
  836. tRC_min_ns = timings->tRC_min / 1000;
  837. rate = 1000000000 / tRC_min_ns;
  838. /*
  839. * For tRC < 30ns we have to use EDO mode. In this case the controller
  840. * does one access per clock cycle. Otherwise the controller does one
  841. * access in two clock cycles, thus we have to double the rate to the
  842. * controller.
  843. */
  844. if (tRC_min_ns < 30) {
  845. rate_round = clk_round_rate(host->clk, rate);
  846. config1 |= NFC_V2_CONFIG1_ONE_CYCLE;
  847. tRC_ps = 1000000000 / (rate_round / 1000);
  848. } else {
  849. rate *= 2;
  850. rate_round = clk_round_rate(host->clk, rate);
  851. config1 &= ~NFC_V2_CONFIG1_ONE_CYCLE;
  852. tRC_ps = 1000000000 / (rate_round / 1000 / 2);
  853. }
  854. /*
  855. * The timing values compared against are from the i.MX25 Automotive
  856. * datasheet, Table 50. NFC Timing Parameters
  857. */
  858. if (timings->tCLS_min > tRC_ps - 1000 ||
  859. timings->tCLH_min > tRC_ps - 2000 ||
  860. timings->tCS_min > tRC_ps - 1000 ||
  861. timings->tCH_min > tRC_ps - 2000 ||
  862. timings->tWP_min > tRC_ps - 1500 ||
  863. timings->tALS_min > tRC_ps ||
  864. timings->tALH_min > tRC_ps - 3000 ||
  865. timings->tDS_min > tRC_ps ||
  866. timings->tDH_min > tRC_ps - 5000 ||
  867. timings->tWC_min > 2 * tRC_ps ||
  868. timings->tWH_min > tRC_ps - 2500 ||
  869. timings->tRR_min > 6 * tRC_ps ||
  870. timings->tRP_min > 3 * tRC_ps / 2 ||
  871. timings->tRC_min > 2 * tRC_ps ||
  872. timings->tREH_min > (tRC_ps / 2) - 2500) {
  873. dev_dbg(host->dev, "Timing out of bounds\n");
  874. return -EINVAL;
  875. }
  876. if (csline == NAND_DATA_IFACE_CHECK_ONLY)
  877. return 0;
  878. ret = clk_set_rate(host->clk, rate);
  879. if (ret)
  880. return ret;
  881. writew(config1, NFC_V1_V2_CONFIG1);
  882. dev_dbg(host->dev, "Setting rate to %ldHz, %s mode\n", rate_round,
  883. config1 & NFC_V2_CONFIG1_ONE_CYCLE ? "One cycle (EDO)" :
  884. "normal");
  885. return 0;
  886. }
  887. static void preset_v2(struct mtd_info *mtd)
  888. {
  889. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  890. struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
  891. uint16_t config1 = 0;
  892. config1 |= NFC_V2_CONFIG1_FP_INT;
  893. if (!host->devtype_data->irqpending_quirk)
  894. config1 |= NFC_V1_V2_CONFIG1_INT_MSK;
  895. if (mtd->writesize) {
  896. uint16_t pages_per_block = mtd->erasesize / mtd->writesize;
  897. if (nand_chip->ecc.mode == NAND_ECC_HW)
  898. config1 |= NFC_V1_V2_CONFIG1_ECC_EN;
  899. host->eccsize = get_eccsize(mtd);
  900. if (host->eccsize == 4)
  901. config1 |= NFC_V2_CONFIG1_ECC_MODE_4;
  902. config1 |= NFC_V2_CONFIG1_PPB(ffs(pages_per_block) - 6);
  903. } else {
  904. host->eccsize = 1;
  905. }
  906. writew(config1, NFC_V1_V2_CONFIG1);
  907. /* preset operation */
  908. /* Unlock the internal RAM Buffer */
  909. writew(0x2, NFC_V1_V2_CONFIG);
  910. /* Blocks to be unlocked */
  911. writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR0);
  912. writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR1);
  913. writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR2);
  914. writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR3);
  915. writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR0);
  916. writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR1);
  917. writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR2);
  918. writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR3);
  919. /* Unlock Block Command for given address range */
  920. writew(0x4, NFC_V1_V2_WRPROT);
  921. }
  922. static void preset_v3(struct mtd_info *mtd)
  923. {
  924. struct nand_chip *chip = mtd_to_nand(mtd);
  925. struct mxc_nand_host *host = nand_get_controller_data(chip);
  926. uint32_t config2, config3;
  927. int i, addr_phases;
  928. writel(NFC_V3_CONFIG1_RBA(0), NFC_V3_CONFIG1);
  929. writel(NFC_V3_IPC_CREQ, NFC_V3_IPC);
  930. /* Unlock the internal RAM Buffer */
  931. writel(NFC_V3_WRPROT_BLS_UNLOCK | NFC_V3_WRPROT_UNLOCK,
  932. NFC_V3_WRPROT);
  933. /* Blocks to be unlocked */
  934. for (i = 0; i < NAND_MAX_CHIPS; i++)
  935. writel(0xffff << 16, NFC_V3_WRPROT_UNLOCK_BLK_ADD0 + (i << 2));
  936. writel(0, NFC_V3_IPC);
  937. config2 = NFC_V3_CONFIG2_ONE_CYCLE |
  938. NFC_V3_CONFIG2_2CMD_PHASES |
  939. NFC_V3_CONFIG2_SPAS(mtd->oobsize >> 1) |
  940. NFC_V3_CONFIG2_ST_CMD(0x70) |
  941. NFC_V3_CONFIG2_INT_MSK |
  942. NFC_V3_CONFIG2_NUM_ADDR_PHASE0;
  943. addr_phases = fls(chip->pagemask) >> 3;
  944. if (mtd->writesize == 2048) {
  945. config2 |= NFC_V3_CONFIG2_PS_2048;
  946. config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases);
  947. } else if (mtd->writesize == 4096) {
  948. config2 |= NFC_V3_CONFIG2_PS_4096;
  949. config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases);
  950. } else {
  951. config2 |= NFC_V3_CONFIG2_PS_512;
  952. config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases - 1);
  953. }
  954. if (mtd->writesize) {
  955. if (chip->ecc.mode == NAND_ECC_HW)
  956. config2 |= NFC_V3_CONFIG2_ECC_EN;
  957. config2 |= NFC_V3_CONFIG2_PPB(
  958. ffs(mtd->erasesize / mtd->writesize) - 6,
  959. host->devtype_data->ppb_shift);
  960. host->eccsize = get_eccsize(mtd);
  961. if (host->eccsize == 8)
  962. config2 |= NFC_V3_CONFIG2_ECC_MODE_8;
  963. }
  964. writel(config2, NFC_V3_CONFIG2);
  965. config3 = NFC_V3_CONFIG3_NUM_OF_DEVICES(0) |
  966. NFC_V3_CONFIG3_NO_SDMA |
  967. NFC_V3_CONFIG3_RBB_MODE |
  968. NFC_V3_CONFIG3_SBB(6) | /* Reset default */
  969. NFC_V3_CONFIG3_ADD_OP(0);
  970. if (!(chip->options & NAND_BUSWIDTH_16))
  971. config3 |= NFC_V3_CONFIG3_FW8;
  972. writel(config3, NFC_V3_CONFIG3);
  973. writel(0, NFC_V3_DELAY_LINE);
  974. }
  975. /* Used by the upper layer to write command to NAND Flash for
  976. * different operations to be carried out on NAND Flash */
  977. static void mxc_nand_command(struct mtd_info *mtd, unsigned command,
  978. int column, int page_addr)
  979. {
  980. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  981. struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
  982. pr_debug("mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n",
  983. command, column, page_addr);
  984. /* Reset command state information */
  985. host->status_request = false;
  986. /* Command pre-processing step */
  987. switch (command) {
  988. case NAND_CMD_RESET:
  989. host->devtype_data->preset(mtd);
  990. host->devtype_data->send_cmd(host, command, false);
  991. break;
  992. case NAND_CMD_STATUS:
  993. host->buf_start = 0;
  994. host->status_request = true;
  995. host->devtype_data->send_cmd(host, command, true);
  996. WARN_ONCE(column != -1 || page_addr != -1,
  997. "Unexpected column/row value (cmd=%u, col=%d, row=%d)\n",
  998. command, column, page_addr);
  999. mxc_do_addr_cycle(mtd, column, page_addr);
  1000. break;
  1001. case NAND_CMD_READ0:
  1002. case NAND_CMD_READOOB:
  1003. if (command == NAND_CMD_READ0)
  1004. host->buf_start = column;
  1005. else
  1006. host->buf_start = column + mtd->writesize;
  1007. command = NAND_CMD_READ0; /* only READ0 is valid */
  1008. host->devtype_data->send_cmd(host, command, false);
  1009. WARN_ONCE(column < 0,
  1010. "Unexpected column/row value (cmd=%u, col=%d, row=%d)\n",
  1011. command, column, page_addr);
  1012. mxc_do_addr_cycle(mtd, 0, page_addr);
  1013. if (mtd->writesize > 512)
  1014. host->devtype_data->send_cmd(host,
  1015. NAND_CMD_READSTART, true);
  1016. host->devtype_data->send_page(mtd, NFC_OUTPUT);
  1017. memcpy32_fromio(host->data_buf, host->main_area0,
  1018. mtd->writesize);
  1019. copy_spare(mtd, true);
  1020. break;
  1021. case NAND_CMD_SEQIN:
  1022. if (column >= mtd->writesize)
  1023. /* call ourself to read a page */
  1024. mxc_nand_command(mtd, NAND_CMD_READ0, 0, page_addr);
  1025. host->buf_start = column;
  1026. host->devtype_data->send_cmd(host, command, false);
  1027. WARN_ONCE(column < -1,
  1028. "Unexpected column/row value (cmd=%u, col=%d, row=%d)\n",
  1029. command, column, page_addr);
  1030. mxc_do_addr_cycle(mtd, 0, page_addr);
  1031. break;
  1032. case NAND_CMD_PAGEPROG:
  1033. memcpy32_toio(host->main_area0, host->data_buf, mtd->writesize);
  1034. copy_spare(mtd, false);
  1035. host->devtype_data->send_page(mtd, NFC_INPUT);
  1036. host->devtype_data->send_cmd(host, command, true);
  1037. WARN_ONCE(column != -1 || page_addr != -1,
  1038. "Unexpected column/row value (cmd=%u, col=%d, row=%d)\n",
  1039. command, column, page_addr);
  1040. mxc_do_addr_cycle(mtd, column, page_addr);
  1041. break;
  1042. case NAND_CMD_READID:
  1043. host->devtype_data->send_cmd(host, command, true);
  1044. mxc_do_addr_cycle(mtd, column, page_addr);
  1045. host->devtype_data->send_read_id(host);
  1046. host->buf_start = 0;
  1047. break;
  1048. case NAND_CMD_ERASE1:
  1049. case NAND_CMD_ERASE2:
  1050. host->devtype_data->send_cmd(host, command, false);
  1051. WARN_ONCE(column != -1,
  1052. "Unexpected column value (cmd=%u, col=%d)\n",
  1053. command, column);
  1054. mxc_do_addr_cycle(mtd, column, page_addr);
  1055. break;
  1056. case NAND_CMD_PARAM:
  1057. host->devtype_data->send_cmd(host, command, false);
  1058. mxc_do_addr_cycle(mtd, column, page_addr);
  1059. host->devtype_data->send_page(mtd, NFC_OUTPUT);
  1060. memcpy32_fromio(host->data_buf, host->main_area0, 512);
  1061. host->buf_start = 0;
  1062. break;
  1063. default:
  1064. WARN_ONCE(1, "Unimplemented command (cmd=%u)\n",
  1065. command);
  1066. break;
  1067. }
  1068. }
  1069. static int mxc_nand_onfi_set_features(struct mtd_info *mtd,
  1070. struct nand_chip *chip, int addr,
  1071. u8 *subfeature_param)
  1072. {
  1073. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  1074. struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
  1075. int i;
  1076. if (!chip->onfi_version ||
  1077. !(le16_to_cpu(chip->onfi_params.opt_cmd)
  1078. & ONFI_OPT_CMD_SET_GET_FEATURES))
  1079. return -EINVAL;
  1080. host->buf_start = 0;
  1081. for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
  1082. chip->write_byte(mtd, subfeature_param[i]);
  1083. memcpy32_toio(host->main_area0, host->data_buf, mtd->writesize);
  1084. host->devtype_data->send_cmd(host, NAND_CMD_SET_FEATURES, false);
  1085. mxc_do_addr_cycle(mtd, addr, -1);
  1086. host->devtype_data->send_page(mtd, NFC_INPUT);
  1087. return 0;
  1088. }
  1089. static int mxc_nand_onfi_get_features(struct mtd_info *mtd,
  1090. struct nand_chip *chip, int addr,
  1091. u8 *subfeature_param)
  1092. {
  1093. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  1094. struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
  1095. int i;
  1096. if (!chip->onfi_version ||
  1097. !(le16_to_cpu(chip->onfi_params.opt_cmd)
  1098. & ONFI_OPT_CMD_SET_GET_FEATURES))
  1099. return -EINVAL;
  1100. host->devtype_data->send_cmd(host, NAND_CMD_GET_FEATURES, false);
  1101. mxc_do_addr_cycle(mtd, addr, -1);
  1102. host->devtype_data->send_page(mtd, NFC_OUTPUT);
  1103. memcpy32_fromio(host->data_buf, host->main_area0, 512);
  1104. host->buf_start = 0;
  1105. for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
  1106. *subfeature_param++ = chip->read_byte(mtd);
  1107. return 0;
  1108. }
  1109. /*
  1110. * The generic flash bbt decriptors overlap with our ecc
  1111. * hardware, so define some i.MX specific ones.
  1112. */
  1113. static uint8_t bbt_pattern[] = { 'B', 'b', 't', '0' };
  1114. static uint8_t mirror_pattern[] = { '1', 't', 'b', 'B' };
  1115. static struct nand_bbt_descr bbt_main_descr = {
  1116. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1117. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  1118. .offs = 0,
  1119. .len = 4,
  1120. .veroffs = 4,
  1121. .maxblocks = 4,
  1122. .pattern = bbt_pattern,
  1123. };
  1124. static struct nand_bbt_descr bbt_mirror_descr = {
  1125. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1126. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  1127. .offs = 0,
  1128. .len = 4,
  1129. .veroffs = 4,
  1130. .maxblocks = 4,
  1131. .pattern = mirror_pattern,
  1132. };
  1133. /* v1 + irqpending_quirk: i.MX21 */
  1134. static const struct mxc_nand_devtype_data imx21_nand_devtype_data = {
  1135. .preset = preset_v1,
  1136. .send_cmd = send_cmd_v1_v2,
  1137. .send_addr = send_addr_v1_v2,
  1138. .send_page = send_page_v1,
  1139. .send_read_id = send_read_id_v1_v2,
  1140. .get_dev_status = get_dev_status_v1_v2,
  1141. .check_int = check_int_v1_v2,
  1142. .irq_control = irq_control_v1_v2,
  1143. .get_ecc_status = get_ecc_status_v1,
  1144. .ooblayout = &mxc_v1_ooblayout_ops,
  1145. .select_chip = mxc_nand_select_chip_v1_v3,
  1146. .correct_data = mxc_nand_correct_data_v1,
  1147. .irqpending_quirk = 1,
  1148. .needs_ip = 0,
  1149. .regs_offset = 0xe00,
  1150. .spare0_offset = 0x800,
  1151. .spare_len = 16,
  1152. .eccbytes = 3,
  1153. .eccsize = 1,
  1154. };
  1155. /* v1 + !irqpending_quirk: i.MX27, i.MX31 */
  1156. static const struct mxc_nand_devtype_data imx27_nand_devtype_data = {
  1157. .preset = preset_v1,
  1158. .send_cmd = send_cmd_v1_v2,
  1159. .send_addr = send_addr_v1_v2,
  1160. .send_page = send_page_v1,
  1161. .send_read_id = send_read_id_v1_v2,
  1162. .get_dev_status = get_dev_status_v1_v2,
  1163. .check_int = check_int_v1_v2,
  1164. .irq_control = irq_control_v1_v2,
  1165. .get_ecc_status = get_ecc_status_v1,
  1166. .ooblayout = &mxc_v1_ooblayout_ops,
  1167. .select_chip = mxc_nand_select_chip_v1_v3,
  1168. .correct_data = mxc_nand_correct_data_v1,
  1169. .irqpending_quirk = 0,
  1170. .needs_ip = 0,
  1171. .regs_offset = 0xe00,
  1172. .spare0_offset = 0x800,
  1173. .axi_offset = 0,
  1174. .spare_len = 16,
  1175. .eccbytes = 3,
  1176. .eccsize = 1,
  1177. };
  1178. /* v21: i.MX25, i.MX35 */
  1179. static const struct mxc_nand_devtype_data imx25_nand_devtype_data = {
  1180. .preset = preset_v2,
  1181. .send_cmd = send_cmd_v1_v2,
  1182. .send_addr = send_addr_v1_v2,
  1183. .send_page = send_page_v2,
  1184. .send_read_id = send_read_id_v1_v2,
  1185. .get_dev_status = get_dev_status_v1_v2,
  1186. .check_int = check_int_v1_v2,
  1187. .irq_control = irq_control_v1_v2,
  1188. .get_ecc_status = get_ecc_status_v2,
  1189. .ooblayout = &mxc_v2_ooblayout_ops,
  1190. .select_chip = mxc_nand_select_chip_v2,
  1191. .correct_data = mxc_nand_correct_data_v2_v3,
  1192. .setup_data_interface = mxc_nand_v2_setup_data_interface,
  1193. .irqpending_quirk = 0,
  1194. .needs_ip = 0,
  1195. .regs_offset = 0x1e00,
  1196. .spare0_offset = 0x1000,
  1197. .axi_offset = 0,
  1198. .spare_len = 64,
  1199. .eccbytes = 9,
  1200. .eccsize = 0,
  1201. };
  1202. /* v3.2a: i.MX51 */
  1203. static const struct mxc_nand_devtype_data imx51_nand_devtype_data = {
  1204. .preset = preset_v3,
  1205. .send_cmd = send_cmd_v3,
  1206. .send_addr = send_addr_v3,
  1207. .send_page = send_page_v3,
  1208. .send_read_id = send_read_id_v3,
  1209. .get_dev_status = get_dev_status_v3,
  1210. .check_int = check_int_v3,
  1211. .irq_control = irq_control_v3,
  1212. .get_ecc_status = get_ecc_status_v3,
  1213. .ooblayout = &mxc_v2_ooblayout_ops,
  1214. .select_chip = mxc_nand_select_chip_v1_v3,
  1215. .correct_data = mxc_nand_correct_data_v2_v3,
  1216. .irqpending_quirk = 0,
  1217. .needs_ip = 1,
  1218. .regs_offset = 0,
  1219. .spare0_offset = 0x1000,
  1220. .axi_offset = 0x1e00,
  1221. .spare_len = 64,
  1222. .eccbytes = 0,
  1223. .eccsize = 0,
  1224. .ppb_shift = 7,
  1225. };
  1226. /* v3.2b: i.MX53 */
  1227. static const struct mxc_nand_devtype_data imx53_nand_devtype_data = {
  1228. .preset = preset_v3,
  1229. .send_cmd = send_cmd_v3,
  1230. .send_addr = send_addr_v3,
  1231. .send_page = send_page_v3,
  1232. .send_read_id = send_read_id_v3,
  1233. .get_dev_status = get_dev_status_v3,
  1234. .check_int = check_int_v3,
  1235. .irq_control = irq_control_v3,
  1236. .get_ecc_status = get_ecc_status_v3,
  1237. .ooblayout = &mxc_v2_ooblayout_ops,
  1238. .select_chip = mxc_nand_select_chip_v1_v3,
  1239. .correct_data = mxc_nand_correct_data_v2_v3,
  1240. .irqpending_quirk = 0,
  1241. .needs_ip = 1,
  1242. .regs_offset = 0,
  1243. .spare0_offset = 0x1000,
  1244. .axi_offset = 0x1e00,
  1245. .spare_len = 64,
  1246. .eccbytes = 0,
  1247. .eccsize = 0,
  1248. .ppb_shift = 8,
  1249. };
  1250. static inline int is_imx21_nfc(struct mxc_nand_host *host)
  1251. {
  1252. return host->devtype_data == &imx21_nand_devtype_data;
  1253. }
  1254. static inline int is_imx27_nfc(struct mxc_nand_host *host)
  1255. {
  1256. return host->devtype_data == &imx27_nand_devtype_data;
  1257. }
  1258. static inline int is_imx25_nfc(struct mxc_nand_host *host)
  1259. {
  1260. return host->devtype_data == &imx25_nand_devtype_data;
  1261. }
  1262. static inline int is_imx51_nfc(struct mxc_nand_host *host)
  1263. {
  1264. return host->devtype_data == &imx51_nand_devtype_data;
  1265. }
  1266. static inline int is_imx53_nfc(struct mxc_nand_host *host)
  1267. {
  1268. return host->devtype_data == &imx53_nand_devtype_data;
  1269. }
  1270. static const struct platform_device_id mxcnd_devtype[] = {
  1271. {
  1272. .name = "imx21-nand",
  1273. .driver_data = (kernel_ulong_t) &imx21_nand_devtype_data,
  1274. }, {
  1275. .name = "imx27-nand",
  1276. .driver_data = (kernel_ulong_t) &imx27_nand_devtype_data,
  1277. }, {
  1278. .name = "imx25-nand",
  1279. .driver_data = (kernel_ulong_t) &imx25_nand_devtype_data,
  1280. }, {
  1281. .name = "imx51-nand",
  1282. .driver_data = (kernel_ulong_t) &imx51_nand_devtype_data,
  1283. }, {
  1284. .name = "imx53-nand",
  1285. .driver_data = (kernel_ulong_t) &imx53_nand_devtype_data,
  1286. }, {
  1287. /* sentinel */
  1288. }
  1289. };
  1290. MODULE_DEVICE_TABLE(platform, mxcnd_devtype);
  1291. #ifdef CONFIG_OF
  1292. static const struct of_device_id mxcnd_dt_ids[] = {
  1293. {
  1294. .compatible = "fsl,imx21-nand",
  1295. .data = &imx21_nand_devtype_data,
  1296. }, {
  1297. .compatible = "fsl,imx27-nand",
  1298. .data = &imx27_nand_devtype_data,
  1299. }, {
  1300. .compatible = "fsl,imx25-nand",
  1301. .data = &imx25_nand_devtype_data,
  1302. }, {
  1303. .compatible = "fsl,imx51-nand",
  1304. .data = &imx51_nand_devtype_data,
  1305. }, {
  1306. .compatible = "fsl,imx53-nand",
  1307. .data = &imx53_nand_devtype_data,
  1308. },
  1309. { /* sentinel */ }
  1310. };
  1311. MODULE_DEVICE_TABLE(of, mxcnd_dt_ids);
  1312. static int __init mxcnd_probe_dt(struct mxc_nand_host *host)
  1313. {
  1314. struct device_node *np = host->dev->of_node;
  1315. const struct of_device_id *of_id =
  1316. of_match_device(mxcnd_dt_ids, host->dev);
  1317. if (!np)
  1318. return 1;
  1319. host->devtype_data = of_id->data;
  1320. return 0;
  1321. }
  1322. #else
  1323. static int __init mxcnd_probe_dt(struct mxc_nand_host *host)
  1324. {
  1325. return 1;
  1326. }
  1327. #endif
  1328. static int mxcnd_probe(struct platform_device *pdev)
  1329. {
  1330. struct nand_chip *this;
  1331. struct mtd_info *mtd;
  1332. struct mxc_nand_host *host;
  1333. struct resource *res;
  1334. int err = 0;
  1335. /* Allocate memory for MTD device structure and private data */
  1336. host = devm_kzalloc(&pdev->dev, sizeof(struct mxc_nand_host),
  1337. GFP_KERNEL);
  1338. if (!host)
  1339. return -ENOMEM;
  1340. /* allocate a temporary buffer for the nand_scan_ident() */
  1341. host->data_buf = devm_kzalloc(&pdev->dev, PAGE_SIZE, GFP_KERNEL);
  1342. if (!host->data_buf)
  1343. return -ENOMEM;
  1344. host->dev = &pdev->dev;
  1345. /* structures must be linked */
  1346. this = &host->nand;
  1347. mtd = nand_to_mtd(this);
  1348. mtd->dev.parent = &pdev->dev;
  1349. mtd->name = DRIVER_NAME;
  1350. /* 50 us command delay time */
  1351. this->chip_delay = 5;
  1352. nand_set_controller_data(this, host);
  1353. nand_set_flash_node(this, pdev->dev.of_node),
  1354. this->dev_ready = mxc_nand_dev_ready;
  1355. this->cmdfunc = mxc_nand_command;
  1356. this->read_byte = mxc_nand_read_byte;
  1357. this->read_word = mxc_nand_read_word;
  1358. this->write_buf = mxc_nand_write_buf;
  1359. this->read_buf = mxc_nand_read_buf;
  1360. this->onfi_set_features = mxc_nand_onfi_set_features;
  1361. this->onfi_get_features = mxc_nand_onfi_get_features;
  1362. host->clk = devm_clk_get(&pdev->dev, NULL);
  1363. if (IS_ERR(host->clk))
  1364. return PTR_ERR(host->clk);
  1365. err = mxcnd_probe_dt(host);
  1366. if (err > 0) {
  1367. struct mxc_nand_platform_data *pdata =
  1368. dev_get_platdata(&pdev->dev);
  1369. if (pdata) {
  1370. host->pdata = *pdata;
  1371. host->devtype_data = (struct mxc_nand_devtype_data *)
  1372. pdev->id_entry->driver_data;
  1373. } else {
  1374. err = -ENODEV;
  1375. }
  1376. }
  1377. if (err < 0)
  1378. return err;
  1379. this->setup_data_interface = host->devtype_data->setup_data_interface;
  1380. if (host->devtype_data->needs_ip) {
  1381. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1382. host->regs_ip = devm_ioremap_resource(&pdev->dev, res);
  1383. if (IS_ERR(host->regs_ip))
  1384. return PTR_ERR(host->regs_ip);
  1385. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1386. } else {
  1387. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1388. }
  1389. host->base = devm_ioremap_resource(&pdev->dev, res);
  1390. if (IS_ERR(host->base))
  1391. return PTR_ERR(host->base);
  1392. host->main_area0 = host->base;
  1393. if (host->devtype_data->regs_offset)
  1394. host->regs = host->base + host->devtype_data->regs_offset;
  1395. host->spare0 = host->base + host->devtype_data->spare0_offset;
  1396. if (host->devtype_data->axi_offset)
  1397. host->regs_axi = host->base + host->devtype_data->axi_offset;
  1398. this->ecc.bytes = host->devtype_data->eccbytes;
  1399. host->eccsize = host->devtype_data->eccsize;
  1400. this->select_chip = host->devtype_data->select_chip;
  1401. this->ecc.size = 512;
  1402. mtd_set_ooblayout(mtd, host->devtype_data->ooblayout);
  1403. if (host->pdata.hw_ecc) {
  1404. this->ecc.mode = NAND_ECC_HW;
  1405. } else {
  1406. this->ecc.mode = NAND_ECC_SOFT;
  1407. this->ecc.algo = NAND_ECC_HAMMING;
  1408. }
  1409. /* NAND bus width determines access functions used by upper layer */
  1410. if (host->pdata.width == 2)
  1411. this->options |= NAND_BUSWIDTH_16;
  1412. /* update flash based bbt */
  1413. if (host->pdata.flash_bbt)
  1414. this->bbt_options |= NAND_BBT_USE_FLASH;
  1415. init_completion(&host->op_completion);
  1416. host->irq = platform_get_irq(pdev, 0);
  1417. if (host->irq < 0)
  1418. return host->irq;
  1419. /*
  1420. * Use host->devtype_data->irq_control() here instead of irq_control()
  1421. * because we must not disable_irq_nosync without having requested the
  1422. * irq.
  1423. */
  1424. host->devtype_data->irq_control(host, 0);
  1425. err = devm_request_irq(&pdev->dev, host->irq, mxc_nfc_irq,
  1426. 0, DRIVER_NAME, host);
  1427. if (err)
  1428. return err;
  1429. err = clk_prepare_enable(host->clk);
  1430. if (err)
  1431. return err;
  1432. host->clk_act = 1;
  1433. /*
  1434. * Now that we "own" the interrupt make sure the interrupt mask bit is
  1435. * cleared on i.MX21. Otherwise we can't read the interrupt status bit
  1436. * on this machine.
  1437. */
  1438. if (host->devtype_data->irqpending_quirk) {
  1439. disable_irq_nosync(host->irq);
  1440. host->devtype_data->irq_control(host, 1);
  1441. }
  1442. /* first scan to find the device and get the page size */
  1443. err = nand_scan_ident(mtd, is_imx25_nfc(host) ? 4 : 1, NULL);
  1444. if (err)
  1445. goto escan;
  1446. switch (this->ecc.mode) {
  1447. case NAND_ECC_HW:
  1448. this->ecc.calculate = mxc_nand_calculate_ecc;
  1449. this->ecc.hwctl = mxc_nand_enable_hwecc;
  1450. this->ecc.correct = host->devtype_data->correct_data;
  1451. break;
  1452. case NAND_ECC_SOFT:
  1453. break;
  1454. default:
  1455. err = -EINVAL;
  1456. goto escan;
  1457. }
  1458. if (this->bbt_options & NAND_BBT_USE_FLASH) {
  1459. this->bbt_td = &bbt_main_descr;
  1460. this->bbt_md = &bbt_mirror_descr;
  1461. }
  1462. /* allocate the right size buffer now */
  1463. devm_kfree(&pdev->dev, (void *)host->data_buf);
  1464. host->data_buf = devm_kzalloc(&pdev->dev, mtd->writesize + mtd->oobsize,
  1465. GFP_KERNEL);
  1466. if (!host->data_buf) {
  1467. err = -ENOMEM;
  1468. goto escan;
  1469. }
  1470. /* Call preset again, with correct writesize this time */
  1471. host->devtype_data->preset(mtd);
  1472. if (!this->ecc.bytes) {
  1473. if (host->eccsize == 8)
  1474. this->ecc.bytes = 18;
  1475. else if (host->eccsize == 4)
  1476. this->ecc.bytes = 9;
  1477. }
  1478. /*
  1479. * Experimentation shows that i.MX NFC can only handle up to 218 oob
  1480. * bytes. Limit used_oobsize to 218 so as to not confuse copy_spare()
  1481. * into copying invalid data to/from the spare IO buffer, as this
  1482. * might cause ECC data corruption when doing sub-page write to a
  1483. * partially written page.
  1484. */
  1485. host->used_oobsize = min(mtd->oobsize, 218U);
  1486. if (this->ecc.mode == NAND_ECC_HW) {
  1487. if (is_imx21_nfc(host) || is_imx27_nfc(host))
  1488. this->ecc.strength = 1;
  1489. else
  1490. this->ecc.strength = (host->eccsize == 4) ? 4 : 8;
  1491. }
  1492. /* second phase scan */
  1493. err = nand_scan_tail(mtd);
  1494. if (err)
  1495. goto escan;
  1496. /* Register the partitions */
  1497. mtd_device_parse_register(mtd, part_probes,
  1498. NULL,
  1499. host->pdata.parts,
  1500. host->pdata.nr_parts);
  1501. platform_set_drvdata(pdev, host);
  1502. return 0;
  1503. escan:
  1504. if (host->clk_act)
  1505. clk_disable_unprepare(host->clk);
  1506. return err;
  1507. }
  1508. static int mxcnd_remove(struct platform_device *pdev)
  1509. {
  1510. struct mxc_nand_host *host = platform_get_drvdata(pdev);
  1511. nand_release(nand_to_mtd(&host->nand));
  1512. if (host->clk_act)
  1513. clk_disable_unprepare(host->clk);
  1514. return 0;
  1515. }
  1516. static struct platform_driver mxcnd_driver = {
  1517. .driver = {
  1518. .name = DRIVER_NAME,
  1519. .of_match_table = of_match_ptr(mxcnd_dt_ids),
  1520. },
  1521. .id_table = mxcnd_devtype,
  1522. .probe = mxcnd_probe,
  1523. .remove = mxcnd_remove,
  1524. };
  1525. module_platform_driver(mxcnd_driver);
  1526. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  1527. MODULE_DESCRIPTION("MXC NAND MTD driver");
  1528. MODULE_LICENSE("GPL");