mxc_nand.c 49 KB

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