mxc_nand.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved.
  4. * Copyright 2008 Sascha Hauer, kernel@pengutronix.de
  5. */
  6. #include <linux/delay.h>
  7. #include <linux/slab.h>
  8. #include <linux/init.h>
  9. #include <linux/module.h>
  10. #include <linux/mtd/mtd.h>
  11. #include <linux/mtd/rawnand.h>
  12. #include <linux/mtd/partitions.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/device.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/clk.h>
  17. #include <linux/err.h>
  18. #include <linux/io.h>
  19. #include <linux/irq.h>
  20. #include <linux/completion.h>
  21. #include <linux/of.h>
  22. #include <linux/bitfield.h>
  23. #define DRIVER_NAME "mxc_nand"
  24. /* Addresses for NFC registers */
  25. #define NFC_V1_V2_BUF_SIZE (host->regs + 0x00)
  26. #define NFC_V1_V2_BUF_ADDR (host->regs + 0x04)
  27. #define NFC_V1_V2_FLASH_ADDR (host->regs + 0x06)
  28. #define NFC_V1_V2_FLASH_CMD (host->regs + 0x08)
  29. #define NFC_V1_V2_CONFIG (host->regs + 0x0a)
  30. #define NFC_V1_V2_ECC_STATUS_RESULT (host->regs + 0x0c)
  31. #define NFC_V1_V2_RSLTMAIN_AREA (host->regs + 0x0e)
  32. #define NFC_V21_RSLTSPARE_AREA (host->regs + 0x10)
  33. #define NFC_V1_V2_WRPROT (host->regs + 0x12)
  34. #define NFC_V1_UNLOCKSTART_BLKADDR (host->regs + 0x14)
  35. #define NFC_V1_UNLOCKEND_BLKADDR (host->regs + 0x16)
  36. #define NFC_V21_UNLOCKSTART_BLKADDR0 (host->regs + 0x20)
  37. #define NFC_V21_UNLOCKSTART_BLKADDR1 (host->regs + 0x24)
  38. #define NFC_V21_UNLOCKSTART_BLKADDR2 (host->regs + 0x28)
  39. #define NFC_V21_UNLOCKSTART_BLKADDR3 (host->regs + 0x2c)
  40. #define NFC_V21_UNLOCKEND_BLKADDR0 (host->regs + 0x22)
  41. #define NFC_V21_UNLOCKEND_BLKADDR1 (host->regs + 0x26)
  42. #define NFC_V21_UNLOCKEND_BLKADDR2 (host->regs + 0x2a)
  43. #define NFC_V21_UNLOCKEND_BLKADDR3 (host->regs + 0x2e)
  44. #define NFC_V1_V2_NF_WRPRST (host->regs + 0x18)
  45. #define NFC_V1_V2_CONFIG1 (host->regs + 0x1a)
  46. #define NFC_V1_V2_CONFIG2 (host->regs + 0x1c)
  47. #define NFC_V1_V2_ECC_STATUS_RESULT_ERM GENMASK(3, 2)
  48. #define NFC_V2_CONFIG1_ECC_MODE_4 (1 << 0)
  49. #define NFC_V1_V2_CONFIG1_SP_EN (1 << 2)
  50. #define NFC_V1_V2_CONFIG1_ECC_EN (1 << 3)
  51. #define NFC_V1_V2_CONFIG1_INT_MSK (1 << 4)
  52. #define NFC_V1_V2_CONFIG1_BIG (1 << 5)
  53. #define NFC_V1_V2_CONFIG1_RST (1 << 6)
  54. #define NFC_V1_V2_CONFIG1_CE (1 << 7)
  55. #define NFC_V2_CONFIG1_ONE_CYCLE (1 << 8)
  56. #define NFC_V2_CONFIG1_PPB(x) (((x) & 0x3) << 9)
  57. #define NFC_V2_CONFIG1_FP_INT (1 << 11)
  58. #define NFC_V1_V2_CONFIG2_INT (1 << 15)
  59. /*
  60. * Operation modes for the NFC. Valid for v1, v2 and v3
  61. * type controllers.
  62. */
  63. #define NFC_CMD (1 << 0)
  64. #define NFC_ADDR (1 << 1)
  65. #define NFC_INPUT (1 << 2)
  66. #define NFC_OUTPUT (1 << 3)
  67. #define NFC_ID (1 << 4)
  68. #define NFC_STATUS (1 << 5)
  69. #define NFC_V3_FLASH_CMD (host->regs_axi + 0x00)
  70. #define NFC_V3_FLASH_ADDR0 (host->regs_axi + 0x04)
  71. #define NFC_V3_CONFIG1 (host->regs_axi + 0x34)
  72. #define NFC_V3_CONFIG1_SP_EN (1 << 0)
  73. #define NFC_V3_CONFIG1_RBA(x) (((x) & 0x7 ) << 4)
  74. #define NFC_V3_ECC_STATUS_RESULT (host->regs_axi + 0x38)
  75. #define NFC_V3_LAUNCH (host->regs_axi + 0x40)
  76. #define NFC_V3_WRPROT (host->regs_ip + 0x0)
  77. #define NFC_V3_WRPROT_LOCK_TIGHT (1 << 0)
  78. #define NFC_V3_WRPROT_LOCK (1 << 1)
  79. #define NFC_V3_WRPROT_UNLOCK (1 << 2)
  80. #define NFC_V3_WRPROT_BLS_UNLOCK (2 << 6)
  81. #define NFC_V3_WRPROT_UNLOCK_BLK_ADD0 (host->regs_ip + 0x04)
  82. #define NFC_V3_CONFIG2 (host->regs_ip + 0x24)
  83. #define NFC_V3_CONFIG2_PS_512 (0 << 0)
  84. #define NFC_V3_CONFIG2_PS_2048 (1 << 0)
  85. #define NFC_V3_CONFIG2_PS_4096 (2 << 0)
  86. #define NFC_V3_CONFIG2_ONE_CYCLE (1 << 2)
  87. #define NFC_V3_CONFIG2_ECC_EN (1 << 3)
  88. #define NFC_V3_CONFIG2_2CMD_PHASES (1 << 4)
  89. #define NFC_V3_CONFIG2_NUM_ADDR_PHASE0 (1 << 5)
  90. #define NFC_V3_CONFIG2_ECC_MODE_8 (1 << 6)
  91. #define NFC_V3_CONFIG2_PPB(x, shift) (((x) & 0x3) << shift)
  92. #define NFC_V3_CONFIG2_NUM_ADDR_PHASE1(x) (((x) & 0x3) << 12)
  93. #define NFC_V3_CONFIG2_INT_MSK (1 << 15)
  94. #define NFC_V3_CONFIG2_ST_CMD(x) (((x) & 0xff) << 24)
  95. #define NFC_V3_CONFIG2_SPAS(x) (((x) & 0xff) << 16)
  96. #define NFC_V3_CONFIG3 (host->regs_ip + 0x28)
  97. #define NFC_V3_CONFIG3_ADD_OP(x) (((x) & 0x3) << 0)
  98. #define NFC_V3_CONFIG3_FW8 (1 << 3)
  99. #define NFC_V3_CONFIG3_SBB(x) (((x) & 0x7) << 8)
  100. #define NFC_V3_CONFIG3_NUM_OF_DEVICES(x) (((x) & 0x7) << 12)
  101. #define NFC_V3_CONFIG3_RBB_MODE (1 << 15)
  102. #define NFC_V3_CONFIG3_NO_SDMA (1 << 20)
  103. #define NFC_V3_IPC (host->regs_ip + 0x2C)
  104. #define NFC_V3_IPC_CREQ (1 << 0)
  105. #define NFC_V3_IPC_INT (1 << 31)
  106. #define NFC_V3_DELAY_LINE (host->regs_ip + 0x34)
  107. struct mxc_nand_host;
  108. struct mxc_nand_devtype_data {
  109. void (*preset)(struct mtd_info *);
  110. int (*read_page)(struct nand_chip *chip);
  111. void (*send_cmd)(struct mxc_nand_host *, uint16_t, int);
  112. void (*send_addr)(struct mxc_nand_host *, uint16_t, int);
  113. void (*send_page)(struct mtd_info *, unsigned int);
  114. void (*send_read_id)(struct mxc_nand_host *);
  115. uint16_t (*get_dev_status)(struct mxc_nand_host *);
  116. int (*check_int)(struct mxc_nand_host *);
  117. void (*irq_control)(struct mxc_nand_host *, int);
  118. u32 (*get_ecc_status)(struct nand_chip *);
  119. const struct mtd_ooblayout_ops *ooblayout;
  120. void (*select_chip)(struct nand_chip *chip, int cs);
  121. int (*setup_interface)(struct nand_chip *chip, int csline,
  122. const struct nand_interface_config *conf);
  123. void (*enable_hwecc)(struct nand_chip *chip, bool enable);
  124. /*
  125. * On i.MX21 the CONFIG2:INT bit cannot be read if interrupts are masked
  126. * (CONFIG1:INT_MSK is set). To handle this the driver uses
  127. * enable_irq/disable_irq_nosync instead of CONFIG1:INT_MSK
  128. */
  129. int irqpending_quirk;
  130. int needs_ip;
  131. size_t regs_offset;
  132. size_t spare0_offset;
  133. size_t axi_offset;
  134. int spare_len;
  135. int eccbytes;
  136. int eccsize;
  137. int ppb_shift;
  138. };
  139. struct mxc_nand_host {
  140. struct nand_chip nand;
  141. struct device *dev;
  142. void __iomem *spare0;
  143. void __iomem *main_area0;
  144. void __iomem *base;
  145. void __iomem *regs;
  146. void __iomem *regs_axi;
  147. void __iomem *regs_ip;
  148. int status_request;
  149. struct clk *clk;
  150. int clk_act;
  151. int irq;
  152. int eccsize;
  153. int used_oobsize;
  154. int active_cs;
  155. unsigned int ecc_stats_v1;
  156. struct completion op_completion;
  157. void *data_buf;
  158. const struct mxc_nand_devtype_data *devtype_data;
  159. };
  160. static const char * const part_probes[] = {
  161. "cmdlinepart", "RedBoot", "ofpart", NULL };
  162. static void memcpy32_fromio(void *trg, const void __iomem *src, size_t size)
  163. {
  164. int i;
  165. u32 *t = trg;
  166. const __iomem u32 *s = src;
  167. for (i = 0; i < (size >> 2); i++)
  168. *t++ = __raw_readl(s++);
  169. }
  170. static void memcpy16_fromio(void *trg, const void __iomem *src, size_t size)
  171. {
  172. int i;
  173. u16 *t = trg;
  174. const __iomem u16 *s = src;
  175. /* We assume that src (IO) is always 32bit aligned */
  176. if (PTR_ALIGN(trg, 4) == trg && IS_ALIGNED(size, 4)) {
  177. memcpy32_fromio(trg, src, size);
  178. return;
  179. }
  180. for (i = 0; i < (size >> 1); i++)
  181. *t++ = __raw_readw(s++);
  182. }
  183. static inline void memcpy32_toio(void __iomem *trg, const void *src, int size)
  184. {
  185. /* __iowrite32_copy use 32bit size values so divide by 4 */
  186. __iowrite32_copy(trg, src, size / 4);
  187. }
  188. static void memcpy16_toio(void __iomem *trg, const void *src, int size)
  189. {
  190. int i;
  191. __iomem u16 *t = trg;
  192. const u16 *s = src;
  193. /* We assume that trg (IO) is always 32bit aligned */
  194. if (PTR_ALIGN(src, 4) == src && IS_ALIGNED(size, 4)) {
  195. memcpy32_toio(trg, src, size);
  196. return;
  197. }
  198. for (i = 0; i < (size >> 1); i++)
  199. __raw_writew(*s++, t++);
  200. }
  201. /*
  202. * The controller splits a page into data chunks of 512 bytes + partial oob.
  203. * There are writesize / 512 such chunks, the size of the partial oob parts is
  204. * oobsize / #chunks rounded down to a multiple of 2. The last oob chunk then
  205. * contains additionally the byte lost by rounding (if any).
  206. * This function handles the needed shuffling between host->data_buf (which
  207. * holds a page in natural order, i.e. writesize bytes data + oobsize bytes
  208. * spare) and the NFC buffer.
  209. */
  210. static void copy_spare(struct mtd_info *mtd, bool bfrom, void *buf)
  211. {
  212. struct nand_chip *this = mtd_to_nand(mtd);
  213. struct mxc_nand_host *host = nand_get_controller_data(this);
  214. u16 i, oob_chunk_size;
  215. u16 num_chunks = mtd->writesize / 512;
  216. u8 *d = buf;
  217. u8 __iomem *s = host->spare0;
  218. u16 sparebuf_size = host->devtype_data->spare_len;
  219. /* size of oob chunk for all but possibly the last one */
  220. oob_chunk_size = (host->used_oobsize / num_chunks) & ~1;
  221. if (bfrom) {
  222. for (i = 0; i < num_chunks - 1; i++)
  223. memcpy16_fromio(d + i * oob_chunk_size,
  224. s + i * sparebuf_size,
  225. oob_chunk_size);
  226. /* the last chunk */
  227. memcpy16_fromio(d + i * oob_chunk_size,
  228. s + i * sparebuf_size,
  229. host->used_oobsize - i * oob_chunk_size);
  230. } else {
  231. for (i = 0; i < num_chunks - 1; i++)
  232. memcpy16_toio(&s[i * sparebuf_size],
  233. &d[i * oob_chunk_size],
  234. oob_chunk_size);
  235. /* the last chunk */
  236. memcpy16_toio(&s[i * sparebuf_size],
  237. &d[i * oob_chunk_size],
  238. host->used_oobsize - i * oob_chunk_size);
  239. }
  240. }
  241. static int check_int_v3(struct mxc_nand_host *host)
  242. {
  243. uint32_t tmp;
  244. tmp = readl(NFC_V3_IPC);
  245. if (!(tmp & NFC_V3_IPC_INT))
  246. return 0;
  247. tmp &= ~NFC_V3_IPC_INT;
  248. writel(tmp, NFC_V3_IPC);
  249. return 1;
  250. }
  251. static int check_int_v1_v2(struct mxc_nand_host *host)
  252. {
  253. uint32_t tmp;
  254. tmp = readw(NFC_V1_V2_CONFIG2);
  255. if (!(tmp & NFC_V1_V2_CONFIG2_INT))
  256. return 0;
  257. if (!host->devtype_data->irqpending_quirk)
  258. writew(tmp & ~NFC_V1_V2_CONFIG2_INT, NFC_V1_V2_CONFIG2);
  259. return 1;
  260. }
  261. static void irq_control_v1_v2(struct mxc_nand_host *host, int activate)
  262. {
  263. uint16_t tmp;
  264. tmp = readw(NFC_V1_V2_CONFIG1);
  265. if (activate)
  266. tmp &= ~NFC_V1_V2_CONFIG1_INT_MSK;
  267. else
  268. tmp |= NFC_V1_V2_CONFIG1_INT_MSK;
  269. writew(tmp, NFC_V1_V2_CONFIG1);
  270. }
  271. static void irq_control_v3(struct mxc_nand_host *host, int activate)
  272. {
  273. uint32_t tmp;
  274. tmp = readl(NFC_V3_CONFIG2);
  275. if (activate)
  276. tmp &= ~NFC_V3_CONFIG2_INT_MSK;
  277. else
  278. tmp |= NFC_V3_CONFIG2_INT_MSK;
  279. writel(tmp, NFC_V3_CONFIG2);
  280. }
  281. static void irq_control(struct mxc_nand_host *host, int activate)
  282. {
  283. if (host->devtype_data->irqpending_quirk) {
  284. if (activate)
  285. enable_irq(host->irq);
  286. else
  287. disable_irq_nosync(host->irq);
  288. } else {
  289. host->devtype_data->irq_control(host, activate);
  290. }
  291. }
  292. static u32 get_ecc_status_v1(struct nand_chip *chip)
  293. {
  294. struct mtd_info *mtd = nand_to_mtd(chip);
  295. struct mxc_nand_host *host = nand_get_controller_data(chip);
  296. unsigned int ecc_stats, max_bitflips = 0;
  297. int no_subpages, i;
  298. no_subpages = mtd->writesize >> 9;
  299. ecc_stats = host->ecc_stats_v1;
  300. for (i = 0; i < no_subpages; i++) {
  301. switch (ecc_stats & 0x3) {
  302. case 0:
  303. default:
  304. break;
  305. case 1:
  306. mtd->ecc_stats.corrected++;
  307. max_bitflips = 1;
  308. break;
  309. case 2:
  310. mtd->ecc_stats.failed++;
  311. break;
  312. }
  313. ecc_stats >>= 2;
  314. }
  315. return max_bitflips;
  316. }
  317. static u32 get_ecc_status_v2_v3(struct nand_chip *chip, unsigned int ecc_stat)
  318. {
  319. struct mtd_info *mtd = nand_to_mtd(chip);
  320. struct mxc_nand_host *host = nand_get_controller_data(chip);
  321. u8 ecc_bit_mask, err_limit;
  322. unsigned int max_bitflips = 0;
  323. int no_subpages, err;
  324. ecc_bit_mask = (host->eccsize == 4) ? 0x7 : 0xf;
  325. err_limit = (host->eccsize == 4) ? 0x4 : 0x8;
  326. no_subpages = mtd->writesize >> 9;
  327. do {
  328. err = ecc_stat & ecc_bit_mask;
  329. if (err > err_limit) {
  330. mtd->ecc_stats.failed++;
  331. } else {
  332. mtd->ecc_stats.corrected += err;
  333. max_bitflips = max_t(unsigned int, max_bitflips, err);
  334. }
  335. ecc_stat >>= 4;
  336. } while (--no_subpages);
  337. return max_bitflips;
  338. }
  339. static u32 get_ecc_status_v2(struct nand_chip *chip)
  340. {
  341. struct mxc_nand_host *host = nand_get_controller_data(chip);
  342. u32 ecc_stat = readl(NFC_V1_V2_ECC_STATUS_RESULT);
  343. return get_ecc_status_v2_v3(chip, ecc_stat);
  344. }
  345. static u32 get_ecc_status_v3(struct nand_chip *chip)
  346. {
  347. struct mxc_nand_host *host = nand_get_controller_data(chip);
  348. u32 ecc_stat = readl(NFC_V3_ECC_STATUS_RESULT);
  349. return get_ecc_status_v2_v3(chip, ecc_stat);
  350. }
  351. static irqreturn_t mxc_nfc_irq(int irq, void *dev_id)
  352. {
  353. struct mxc_nand_host *host = dev_id;
  354. if (!host->devtype_data->check_int(host))
  355. return IRQ_NONE;
  356. irq_control(host, 0);
  357. complete(&host->op_completion);
  358. return IRQ_HANDLED;
  359. }
  360. /* This function polls the NANDFC to wait for the basic operation to
  361. * complete by checking the INT bit of config2 register.
  362. */
  363. static int wait_op_done(struct mxc_nand_host *host, int useirq)
  364. {
  365. int ret = 0;
  366. /*
  367. * If operation is already complete, don't bother to setup an irq or a
  368. * loop.
  369. */
  370. if (host->devtype_data->check_int(host))
  371. return 0;
  372. if (useirq) {
  373. unsigned long time_left;
  374. reinit_completion(&host->op_completion);
  375. irq_control(host, 1);
  376. time_left = wait_for_completion_timeout(&host->op_completion, HZ);
  377. if (!time_left && !host->devtype_data->check_int(host)) {
  378. dev_dbg(host->dev, "timeout waiting for irq\n");
  379. ret = -ETIMEDOUT;
  380. }
  381. } else {
  382. int max_retries = 8000;
  383. int done;
  384. do {
  385. udelay(1);
  386. done = host->devtype_data->check_int(host);
  387. if (done)
  388. break;
  389. } while (--max_retries);
  390. if (!done) {
  391. dev_dbg(host->dev, "timeout polling for completion\n");
  392. ret = -ETIMEDOUT;
  393. }
  394. }
  395. WARN_ONCE(ret < 0, "timeout! useirq=%d\n", useirq);
  396. return ret;
  397. }
  398. static void send_cmd_v3(struct mxc_nand_host *host, uint16_t cmd, int useirq)
  399. {
  400. /* fill command */
  401. writel(cmd, NFC_V3_FLASH_CMD);
  402. /* send out command */
  403. writel(NFC_CMD, NFC_V3_LAUNCH);
  404. /* Wait for operation to complete */
  405. wait_op_done(host, useirq);
  406. }
  407. /* This function issues the specified command to the NAND device and
  408. * waits for completion. */
  409. static void send_cmd_v1_v2(struct mxc_nand_host *host, uint16_t cmd, int useirq)
  410. {
  411. dev_dbg(host->dev, "send_cmd(host, 0x%x, %d)\n", cmd, useirq);
  412. writew(cmd, NFC_V1_V2_FLASH_CMD);
  413. writew(NFC_CMD, NFC_V1_V2_CONFIG2);
  414. if (host->devtype_data->irqpending_quirk && (cmd == NAND_CMD_RESET)) {
  415. int max_retries = 100;
  416. /* Reset completion is indicated by NFC_CONFIG2 */
  417. /* being set to 0 */
  418. while (max_retries-- > 0) {
  419. if (readw(NFC_V1_V2_CONFIG2) == 0) {
  420. break;
  421. }
  422. udelay(1);
  423. }
  424. if (max_retries < 0)
  425. dev_dbg(host->dev, "%s: RESET failed\n", __func__);
  426. } else {
  427. /* Wait for operation to complete */
  428. wait_op_done(host, useirq);
  429. }
  430. }
  431. static void send_addr_v3(struct mxc_nand_host *host, uint16_t addr, int islast)
  432. {
  433. /* fill address */
  434. writel(addr, NFC_V3_FLASH_ADDR0);
  435. /* send out address */
  436. writel(NFC_ADDR, NFC_V3_LAUNCH);
  437. wait_op_done(host, 0);
  438. }
  439. /* This function sends an address (or partial address) to the
  440. * NAND device. The address is used to select the source/destination for
  441. * a NAND command. */
  442. static void send_addr_v1_v2(struct mxc_nand_host *host, uint16_t addr, int islast)
  443. {
  444. dev_dbg(host->dev, "send_addr(host, 0x%x %d)\n", addr, islast);
  445. writew(addr, NFC_V1_V2_FLASH_ADDR);
  446. writew(NFC_ADDR, NFC_V1_V2_CONFIG2);
  447. /* Wait for operation to complete */
  448. wait_op_done(host, islast);
  449. }
  450. static void send_page_v3(struct mtd_info *mtd, unsigned int ops)
  451. {
  452. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  453. struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
  454. uint32_t tmp;
  455. tmp = readl(NFC_V3_CONFIG1);
  456. tmp &= ~(7 << 4);
  457. writel(tmp, NFC_V3_CONFIG1);
  458. /* transfer data from NFC ram to nand */
  459. writel(ops, NFC_V3_LAUNCH);
  460. wait_op_done(host, false);
  461. }
  462. static void send_page_v2(struct mtd_info *mtd, unsigned int ops)
  463. {
  464. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  465. struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
  466. /* NANDFC buffer 0 is used for page read/write */
  467. writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR);
  468. writew(ops, NFC_V1_V2_CONFIG2);
  469. /* Wait for operation to complete */
  470. wait_op_done(host, true);
  471. }
  472. static void send_page_v1(struct mtd_info *mtd, unsigned int ops)
  473. {
  474. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  475. struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
  476. int bufs, i;
  477. if (mtd->writesize > 512)
  478. bufs = 4;
  479. else
  480. bufs = 1;
  481. for (i = 0; i < bufs; i++) {
  482. /* NANDFC buffer 0 is used for page read/write */
  483. writew((host->active_cs << 4) | i, NFC_V1_V2_BUF_ADDR);
  484. writew(ops, NFC_V1_V2_CONFIG2);
  485. /* Wait for operation to complete */
  486. wait_op_done(host, true);
  487. }
  488. }
  489. static void send_read_id_v3(struct mxc_nand_host *host)
  490. {
  491. /* Read ID into main buffer */
  492. writel(NFC_ID, NFC_V3_LAUNCH);
  493. wait_op_done(host, true);
  494. memcpy32_fromio(host->data_buf, host->main_area0, 16);
  495. }
  496. /* Request the NANDFC to perform a read of the NAND device ID. */
  497. static void send_read_id_v1_v2(struct mxc_nand_host *host)
  498. {
  499. /* NANDFC buffer 0 is used for device ID output */
  500. writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR);
  501. writew(NFC_ID, NFC_V1_V2_CONFIG2);
  502. /* Wait for operation to complete */
  503. wait_op_done(host, true);
  504. memcpy32_fromio(host->data_buf, host->main_area0, 16);
  505. }
  506. static uint16_t get_dev_status_v3(struct mxc_nand_host *host)
  507. {
  508. writew(NFC_STATUS, NFC_V3_LAUNCH);
  509. wait_op_done(host, true);
  510. return readl(NFC_V3_CONFIG1) >> 16;
  511. }
  512. /* This function requests the NANDFC to perform a read of the
  513. * NAND device status and returns the current status. */
  514. static uint16_t get_dev_status_v1_v2(struct mxc_nand_host *host)
  515. {
  516. void __iomem *main_buf = host->main_area0;
  517. uint32_t store;
  518. uint16_t ret;
  519. writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR);
  520. /*
  521. * The device status is stored in main_area0. To
  522. * prevent corruption of the buffer save the value
  523. * and restore it afterwards.
  524. */
  525. store = readl(main_buf);
  526. writew(NFC_STATUS, NFC_V1_V2_CONFIG2);
  527. wait_op_done(host, true);
  528. ret = readw(main_buf);
  529. writel(store, main_buf);
  530. return ret;
  531. }
  532. static void mxc_nand_enable_hwecc_v1_v2(struct nand_chip *chip, bool enable)
  533. {
  534. struct mxc_nand_host *host = nand_get_controller_data(chip);
  535. uint16_t config1;
  536. if (chip->ecc.engine_type != NAND_ECC_ENGINE_TYPE_ON_HOST)
  537. return;
  538. config1 = readw(NFC_V1_V2_CONFIG1);
  539. if (enable)
  540. config1 |= NFC_V1_V2_CONFIG1_ECC_EN;
  541. else
  542. config1 &= ~NFC_V1_V2_CONFIG1_ECC_EN;
  543. writew(config1, NFC_V1_V2_CONFIG1);
  544. }
  545. static void mxc_nand_enable_hwecc_v3(struct nand_chip *chip, bool enable)
  546. {
  547. struct mxc_nand_host *host = nand_get_controller_data(chip);
  548. uint32_t config2;
  549. if (chip->ecc.engine_type != NAND_ECC_ENGINE_TYPE_ON_HOST)
  550. return;
  551. config2 = readl(NFC_V3_CONFIG2);
  552. if (enable)
  553. config2 |= NFC_V3_CONFIG2_ECC_EN;
  554. else
  555. config2 &= ~NFC_V3_CONFIG2_ECC_EN;
  556. writel(config2, NFC_V3_CONFIG2);
  557. }
  558. static int mxc_nand_read_page_v1(struct nand_chip *chip)
  559. {
  560. struct mtd_info *mtd = nand_to_mtd(chip);
  561. struct mxc_nand_host *host = nand_get_controller_data(chip);
  562. int no_subpages;
  563. int i;
  564. unsigned int ecc_stats = 0;
  565. if (mtd->writesize)
  566. no_subpages = mtd->writesize >> 9;
  567. else
  568. /* READ PARAMETER PAGE is called when mtd->writesize is not yet set */
  569. no_subpages = 1;
  570. for (i = 0; i < no_subpages; i++) {
  571. /* NANDFC buffer 0 is used for page read/write */
  572. writew((host->active_cs << 4) | i, NFC_V1_V2_BUF_ADDR);
  573. writew(NFC_OUTPUT, NFC_V1_V2_CONFIG2);
  574. /* Wait for operation to complete */
  575. wait_op_done(host, true);
  576. ecc_stats |= FIELD_GET(NFC_V1_V2_ECC_STATUS_RESULT_ERM,
  577. readw(NFC_V1_V2_ECC_STATUS_RESULT)) << i * 2;
  578. }
  579. host->ecc_stats_v1 = ecc_stats;
  580. return 0;
  581. }
  582. static int mxc_nand_read_page_v2_v3(struct nand_chip *chip)
  583. {
  584. struct mtd_info *mtd = nand_to_mtd(chip);
  585. struct mxc_nand_host *host = nand_get_controller_data(chip);
  586. host->devtype_data->send_page(mtd, NFC_OUTPUT);
  587. return 0;
  588. }
  589. static int mxc_nand_read_page(struct nand_chip *chip, uint8_t *buf,
  590. int oob_required, int page)
  591. {
  592. struct mtd_info *mtd = nand_to_mtd(chip);
  593. struct mxc_nand_host *host = nand_get_controller_data(chip);
  594. int ret;
  595. host->devtype_data->enable_hwecc(chip, true);
  596. ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize);
  597. host->devtype_data->enable_hwecc(chip, false);
  598. if (ret)
  599. return ret;
  600. if (oob_required)
  601. copy_spare(mtd, true, chip->oob_poi);
  602. return host->devtype_data->get_ecc_status(chip);
  603. }
  604. static int mxc_nand_read_page_raw(struct nand_chip *chip, uint8_t *buf,
  605. int oob_required, int page)
  606. {
  607. struct mtd_info *mtd = nand_to_mtd(chip);
  608. int ret;
  609. ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize);
  610. if (ret)
  611. return ret;
  612. if (oob_required)
  613. copy_spare(mtd, true, chip->oob_poi);
  614. return 0;
  615. }
  616. static int mxc_nand_read_oob(struct nand_chip *chip, int page)
  617. {
  618. struct mtd_info *mtd = nand_to_mtd(chip);
  619. struct mxc_nand_host *host = nand_get_controller_data(chip);
  620. int ret;
  621. ret = nand_read_page_op(chip, page, 0, host->data_buf, mtd->writesize);
  622. if (ret)
  623. return ret;
  624. copy_spare(mtd, true, chip->oob_poi);
  625. return 0;
  626. }
  627. static int mxc_nand_write_page_ecc(struct nand_chip *chip, const uint8_t *buf,
  628. int oob_required, int page)
  629. {
  630. struct mtd_info *mtd = nand_to_mtd(chip);
  631. struct mxc_nand_host *host = nand_get_controller_data(chip);
  632. int ret;
  633. copy_spare(mtd, false, chip->oob_poi);
  634. host->devtype_data->enable_hwecc(chip, true);
  635. ret = nand_prog_page_op(chip, page, 0, buf, mtd->writesize);
  636. host->devtype_data->enable_hwecc(chip, false);
  637. return ret;
  638. }
  639. static int mxc_nand_write_page_raw(struct nand_chip *chip, const uint8_t *buf,
  640. int oob_required, int page)
  641. {
  642. struct mtd_info *mtd = nand_to_mtd(chip);
  643. copy_spare(mtd, false, chip->oob_poi);
  644. return nand_prog_page_op(chip, page, 0, buf, mtd->writesize);
  645. }
  646. static int mxc_nand_write_oob(struct nand_chip *chip, int page)
  647. {
  648. struct mtd_info *mtd = nand_to_mtd(chip);
  649. struct mxc_nand_host *host = nand_get_controller_data(chip);
  650. memset(host->data_buf, 0xff, mtd->writesize);
  651. copy_spare(mtd, false, chip->oob_poi);
  652. return nand_prog_page_op(chip, page, 0, host->data_buf, mtd->writesize);
  653. }
  654. /* This function is used by upper layer for select and
  655. * deselect of the NAND chip */
  656. static void mxc_nand_select_chip_v1_v3(struct nand_chip *nand_chip, int chip)
  657. {
  658. struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
  659. if (chip == -1) {
  660. /* Disable the NFC clock */
  661. if (host->clk_act) {
  662. clk_disable_unprepare(host->clk);
  663. host->clk_act = 0;
  664. }
  665. return;
  666. }
  667. if (!host->clk_act) {
  668. /* Enable the NFC clock */
  669. clk_prepare_enable(host->clk);
  670. host->clk_act = 1;
  671. }
  672. }
  673. static void mxc_nand_select_chip_v2(struct nand_chip *nand_chip, int chip)
  674. {
  675. struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
  676. if (chip == -1) {
  677. /* Disable the NFC clock */
  678. if (host->clk_act) {
  679. clk_disable_unprepare(host->clk);
  680. host->clk_act = 0;
  681. }
  682. return;
  683. }
  684. if (!host->clk_act) {
  685. /* Enable the NFC clock */
  686. clk_prepare_enable(host->clk);
  687. host->clk_act = 1;
  688. }
  689. host->active_cs = chip;
  690. writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR);
  691. }
  692. #define MXC_V1_ECCBYTES 5
  693. static int mxc_v1_ooblayout_ecc(struct mtd_info *mtd, int section,
  694. struct mtd_oob_region *oobregion)
  695. {
  696. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  697. if (section >= nand_chip->ecc.steps)
  698. return -ERANGE;
  699. oobregion->offset = (section * 16) + 6;
  700. oobregion->length = MXC_V1_ECCBYTES;
  701. return 0;
  702. }
  703. static int mxc_v1_ooblayout_free(struct mtd_info *mtd, int section,
  704. struct mtd_oob_region *oobregion)
  705. {
  706. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  707. if (section > nand_chip->ecc.steps)
  708. return -ERANGE;
  709. if (!section) {
  710. if (mtd->writesize <= 512) {
  711. oobregion->offset = 0;
  712. oobregion->length = 5;
  713. } else {
  714. oobregion->offset = 2;
  715. oobregion->length = 4;
  716. }
  717. } else {
  718. oobregion->offset = ((section - 1) * 16) + MXC_V1_ECCBYTES + 6;
  719. if (section < nand_chip->ecc.steps)
  720. oobregion->length = (section * 16) + 6 -
  721. oobregion->offset;
  722. else
  723. oobregion->length = mtd->oobsize - oobregion->offset;
  724. }
  725. return 0;
  726. }
  727. static const struct mtd_ooblayout_ops mxc_v1_ooblayout_ops = {
  728. .ecc = mxc_v1_ooblayout_ecc,
  729. .free = mxc_v1_ooblayout_free,
  730. };
  731. static int mxc_v2_ooblayout_ecc(struct mtd_info *mtd, int section,
  732. struct mtd_oob_region *oobregion)
  733. {
  734. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  735. int stepsize = nand_chip->ecc.bytes == 9 ? 16 : 26;
  736. if (section >= nand_chip->ecc.steps)
  737. return -ERANGE;
  738. oobregion->offset = (section * stepsize) + 7;
  739. oobregion->length = nand_chip->ecc.bytes;
  740. return 0;
  741. }
  742. static int mxc_v2_ooblayout_free(struct mtd_info *mtd, int section,
  743. struct mtd_oob_region *oobregion)
  744. {
  745. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  746. int stepsize = nand_chip->ecc.bytes == 9 ? 16 : 26;
  747. if (section >= nand_chip->ecc.steps)
  748. return -ERANGE;
  749. if (!section) {
  750. if (mtd->writesize <= 512) {
  751. oobregion->offset = 0;
  752. oobregion->length = 5;
  753. } else {
  754. oobregion->offset = 2;
  755. oobregion->length = 4;
  756. }
  757. } else {
  758. oobregion->offset = section * stepsize;
  759. oobregion->length = 7;
  760. }
  761. return 0;
  762. }
  763. static const struct mtd_ooblayout_ops mxc_v2_ooblayout_ops = {
  764. .ecc = mxc_v2_ooblayout_ecc,
  765. .free = mxc_v2_ooblayout_free,
  766. };
  767. /*
  768. * v2 and v3 type controllers can do 4bit or 8bit ecc depending
  769. * on how much oob the nand chip has. For 8bit ecc we need at least
  770. * 26 bytes of oob data per 512 byte block.
  771. */
  772. static int get_eccsize(struct mtd_info *mtd)
  773. {
  774. int oobbytes_per_512 = 0;
  775. oobbytes_per_512 = mtd->oobsize * 512 / mtd->writesize;
  776. if (oobbytes_per_512 < 26)
  777. return 4;
  778. else
  779. return 8;
  780. }
  781. static void preset_v1(struct mtd_info *mtd)
  782. {
  783. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  784. struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
  785. uint16_t config1 = 0;
  786. if (nand_chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_ON_HOST &&
  787. mtd->writesize)
  788. config1 |= NFC_V1_V2_CONFIG1_ECC_EN;
  789. if (!host->devtype_data->irqpending_quirk)
  790. config1 |= NFC_V1_V2_CONFIG1_INT_MSK;
  791. host->eccsize = 1;
  792. writew(config1, NFC_V1_V2_CONFIG1);
  793. /* preset operation */
  794. /* Unlock the internal RAM Buffer */
  795. writew(0x2, NFC_V1_V2_CONFIG);
  796. /* Blocks to be unlocked */
  797. writew(0x0, NFC_V1_UNLOCKSTART_BLKADDR);
  798. writew(0xffff, NFC_V1_UNLOCKEND_BLKADDR);
  799. /* Unlock Block Command for given address range */
  800. writew(0x4, NFC_V1_V2_WRPROT);
  801. }
  802. static int mxc_nand_v2_setup_interface(struct nand_chip *chip, int csline,
  803. const struct nand_interface_config *conf)
  804. {
  805. struct mxc_nand_host *host = nand_get_controller_data(chip);
  806. int tRC_min_ns, tRC_ps, ret;
  807. unsigned long rate, rate_round;
  808. const struct nand_sdr_timings *timings;
  809. u16 config1;
  810. timings = nand_get_sdr_timings(conf);
  811. if (IS_ERR(timings))
  812. return -ENOTSUPP;
  813. config1 = readw(NFC_V1_V2_CONFIG1);
  814. tRC_min_ns = timings->tRC_min / 1000;
  815. rate = 1000000000 / tRC_min_ns;
  816. /*
  817. * For tRC < 30ns we have to use EDO mode. In this case the controller
  818. * does one access per clock cycle. Otherwise the controller does one
  819. * access in two clock cycles, thus we have to double the rate to the
  820. * controller.
  821. */
  822. if (tRC_min_ns < 30) {
  823. rate_round = clk_round_rate(host->clk, rate);
  824. config1 |= NFC_V2_CONFIG1_ONE_CYCLE;
  825. tRC_ps = 1000000000 / (rate_round / 1000);
  826. } else {
  827. rate *= 2;
  828. rate_round = clk_round_rate(host->clk, rate);
  829. config1 &= ~NFC_V2_CONFIG1_ONE_CYCLE;
  830. tRC_ps = 1000000000 / (rate_round / 1000 / 2);
  831. }
  832. /*
  833. * The timing values compared against are from the i.MX25 Automotive
  834. * datasheet, Table 50. NFC Timing Parameters
  835. */
  836. if (timings->tCLS_min > tRC_ps - 1000 ||
  837. timings->tCLH_min > tRC_ps - 2000 ||
  838. timings->tCS_min > tRC_ps - 1000 ||
  839. timings->tCH_min > tRC_ps - 2000 ||
  840. timings->tWP_min > tRC_ps - 1500 ||
  841. timings->tALS_min > tRC_ps ||
  842. timings->tALH_min > tRC_ps - 3000 ||
  843. timings->tDS_min > tRC_ps ||
  844. timings->tDH_min > tRC_ps - 5000 ||
  845. timings->tWC_min > 2 * tRC_ps ||
  846. timings->tWH_min > tRC_ps - 2500 ||
  847. timings->tRR_min > 6 * tRC_ps ||
  848. timings->tRP_min > 3 * tRC_ps / 2 ||
  849. timings->tRC_min > 2 * tRC_ps ||
  850. timings->tREH_min > (tRC_ps / 2) - 2500) {
  851. dev_dbg(host->dev, "Timing out of bounds\n");
  852. return -EINVAL;
  853. }
  854. if (csline == NAND_DATA_IFACE_CHECK_ONLY)
  855. return 0;
  856. ret = clk_set_rate(host->clk, rate);
  857. if (ret)
  858. return ret;
  859. writew(config1, NFC_V1_V2_CONFIG1);
  860. dev_dbg(host->dev, "Setting rate to %ldHz, %s mode\n", rate_round,
  861. config1 & NFC_V2_CONFIG1_ONE_CYCLE ? "One cycle (EDO)" :
  862. "normal");
  863. return 0;
  864. }
  865. static void preset_v2(struct mtd_info *mtd)
  866. {
  867. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  868. struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
  869. uint16_t config1 = 0;
  870. config1 |= NFC_V2_CONFIG1_FP_INT;
  871. if (!host->devtype_data->irqpending_quirk)
  872. config1 |= NFC_V1_V2_CONFIG1_INT_MSK;
  873. if (mtd->writesize) {
  874. uint16_t pages_per_block = mtd->erasesize / mtd->writesize;
  875. if (nand_chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_ON_HOST)
  876. config1 |= NFC_V1_V2_CONFIG1_ECC_EN;
  877. host->eccsize = get_eccsize(mtd);
  878. if (host->eccsize == 4)
  879. config1 |= NFC_V2_CONFIG1_ECC_MODE_4;
  880. config1 |= NFC_V2_CONFIG1_PPB(ffs(pages_per_block) - 6);
  881. } else {
  882. host->eccsize = 1;
  883. }
  884. writew(config1, NFC_V1_V2_CONFIG1);
  885. /* preset operation */
  886. /* spare area size in 16-bit half-words */
  887. writew(mtd->oobsize / 2, NFC_V21_RSLTSPARE_AREA);
  888. /* Unlock the internal RAM Buffer */
  889. writew(0x2, NFC_V1_V2_CONFIG);
  890. /* Blocks to be unlocked */
  891. writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR0);
  892. writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR1);
  893. writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR2);
  894. writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR3);
  895. writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR0);
  896. writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR1);
  897. writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR2);
  898. writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR3);
  899. /* Unlock Block Command for given address range */
  900. writew(0x4, NFC_V1_V2_WRPROT);
  901. }
  902. static void preset_v3(struct mtd_info *mtd)
  903. {
  904. struct nand_chip *chip = mtd_to_nand(mtd);
  905. struct mxc_nand_host *host = nand_get_controller_data(chip);
  906. uint32_t config2, config3;
  907. int i, addr_phases;
  908. writel(NFC_V3_CONFIG1_RBA(0), NFC_V3_CONFIG1);
  909. writel(NFC_V3_IPC_CREQ, NFC_V3_IPC);
  910. /* Unlock the internal RAM Buffer */
  911. writel(NFC_V3_WRPROT_BLS_UNLOCK | NFC_V3_WRPROT_UNLOCK,
  912. NFC_V3_WRPROT);
  913. /* Blocks to be unlocked */
  914. for (i = 0; i < NAND_MAX_CHIPS; i++)
  915. writel(0xffff << 16, NFC_V3_WRPROT_UNLOCK_BLK_ADD0 + (i << 2));
  916. writel(0, NFC_V3_IPC);
  917. config2 = NFC_V3_CONFIG2_ONE_CYCLE |
  918. NFC_V3_CONFIG2_2CMD_PHASES |
  919. NFC_V3_CONFIG2_SPAS(mtd->oobsize >> 1) |
  920. NFC_V3_CONFIG2_ST_CMD(0x70) |
  921. NFC_V3_CONFIG2_INT_MSK |
  922. NFC_V3_CONFIG2_NUM_ADDR_PHASE0;
  923. addr_phases = fls(chip->pagemask) >> 3;
  924. if (mtd->writesize == 2048) {
  925. config2 |= NFC_V3_CONFIG2_PS_2048;
  926. config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases);
  927. } else if (mtd->writesize == 4096) {
  928. config2 |= NFC_V3_CONFIG2_PS_4096;
  929. config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases);
  930. } else {
  931. config2 |= NFC_V3_CONFIG2_PS_512;
  932. config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases - 1);
  933. }
  934. if (mtd->writesize) {
  935. if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_ON_HOST)
  936. config2 |= NFC_V3_CONFIG2_ECC_EN;
  937. config2 |= NFC_V3_CONFIG2_PPB(
  938. ffs(mtd->erasesize / mtd->writesize) - 6,
  939. host->devtype_data->ppb_shift);
  940. host->eccsize = get_eccsize(mtd);
  941. if (host->eccsize == 8)
  942. config2 |= NFC_V3_CONFIG2_ECC_MODE_8;
  943. }
  944. writel(config2, NFC_V3_CONFIG2);
  945. config3 = NFC_V3_CONFIG3_NUM_OF_DEVICES(0) |
  946. NFC_V3_CONFIG3_NO_SDMA |
  947. NFC_V3_CONFIG3_RBB_MODE |
  948. NFC_V3_CONFIG3_SBB(6) | /* Reset default */
  949. NFC_V3_CONFIG3_ADD_OP(0);
  950. if (!(chip->options & NAND_BUSWIDTH_16))
  951. config3 |= NFC_V3_CONFIG3_FW8;
  952. writel(config3, NFC_V3_CONFIG3);
  953. writel(0, NFC_V3_DELAY_LINE);
  954. }
  955. /*
  956. * The generic flash bbt descriptors overlap with our ecc
  957. * hardware, so define some i.MX specific ones.
  958. */
  959. static uint8_t bbt_pattern[] = { 'B', 'b', 't', '0' };
  960. static uint8_t mirror_pattern[] = { '1', 't', 'b', 'B' };
  961. static struct nand_bbt_descr bbt_main_descr = {
  962. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  963. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  964. .offs = 0,
  965. .len = 4,
  966. .veroffs = 4,
  967. .maxblocks = 4,
  968. .pattern = bbt_pattern,
  969. };
  970. static struct nand_bbt_descr bbt_mirror_descr = {
  971. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  972. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  973. .offs = 0,
  974. .len = 4,
  975. .veroffs = 4,
  976. .maxblocks = 4,
  977. .pattern = mirror_pattern,
  978. };
  979. /* v1 + irqpending_quirk: i.MX21 */
  980. static const struct mxc_nand_devtype_data imx21_nand_devtype_data = {
  981. .preset = preset_v1,
  982. .read_page = mxc_nand_read_page_v1,
  983. .send_cmd = send_cmd_v1_v2,
  984. .send_addr = send_addr_v1_v2,
  985. .send_page = send_page_v1,
  986. .send_read_id = send_read_id_v1_v2,
  987. .get_dev_status = get_dev_status_v1_v2,
  988. .check_int = check_int_v1_v2,
  989. .irq_control = irq_control_v1_v2,
  990. .get_ecc_status = get_ecc_status_v1,
  991. .ooblayout = &mxc_v1_ooblayout_ops,
  992. .select_chip = mxc_nand_select_chip_v1_v3,
  993. .enable_hwecc = mxc_nand_enable_hwecc_v1_v2,
  994. .irqpending_quirk = 1,
  995. .needs_ip = 0,
  996. .regs_offset = 0xe00,
  997. .spare0_offset = 0x800,
  998. .spare_len = 16,
  999. .eccbytes = 3,
  1000. .eccsize = 1,
  1001. };
  1002. /* v1 + !irqpending_quirk: i.MX27, i.MX31 */
  1003. static const struct mxc_nand_devtype_data imx27_nand_devtype_data = {
  1004. .preset = preset_v1,
  1005. .read_page = mxc_nand_read_page_v1,
  1006. .send_cmd = send_cmd_v1_v2,
  1007. .send_addr = send_addr_v1_v2,
  1008. .send_page = send_page_v1,
  1009. .send_read_id = send_read_id_v1_v2,
  1010. .get_dev_status = get_dev_status_v1_v2,
  1011. .check_int = check_int_v1_v2,
  1012. .irq_control = irq_control_v1_v2,
  1013. .get_ecc_status = get_ecc_status_v1,
  1014. .ooblayout = &mxc_v1_ooblayout_ops,
  1015. .select_chip = mxc_nand_select_chip_v1_v3,
  1016. .enable_hwecc = mxc_nand_enable_hwecc_v1_v2,
  1017. .irqpending_quirk = 0,
  1018. .needs_ip = 0,
  1019. .regs_offset = 0xe00,
  1020. .spare0_offset = 0x800,
  1021. .axi_offset = 0,
  1022. .spare_len = 16,
  1023. .eccbytes = 3,
  1024. .eccsize = 1,
  1025. };
  1026. /* v21: i.MX25, i.MX35 */
  1027. static const struct mxc_nand_devtype_data imx25_nand_devtype_data = {
  1028. .preset = preset_v2,
  1029. .read_page = mxc_nand_read_page_v2_v3,
  1030. .send_cmd = send_cmd_v1_v2,
  1031. .send_addr = send_addr_v1_v2,
  1032. .send_page = send_page_v2,
  1033. .send_read_id = send_read_id_v1_v2,
  1034. .get_dev_status = get_dev_status_v1_v2,
  1035. .check_int = check_int_v1_v2,
  1036. .irq_control = irq_control_v1_v2,
  1037. .get_ecc_status = get_ecc_status_v2,
  1038. .ooblayout = &mxc_v2_ooblayout_ops,
  1039. .select_chip = mxc_nand_select_chip_v2,
  1040. .setup_interface = mxc_nand_v2_setup_interface,
  1041. .enable_hwecc = mxc_nand_enable_hwecc_v1_v2,
  1042. .irqpending_quirk = 0,
  1043. .needs_ip = 0,
  1044. .regs_offset = 0x1e00,
  1045. .spare0_offset = 0x1000,
  1046. .axi_offset = 0,
  1047. .spare_len = 64,
  1048. .eccbytes = 9,
  1049. .eccsize = 0,
  1050. };
  1051. /* v3.2a: i.MX51 */
  1052. static const struct mxc_nand_devtype_data imx51_nand_devtype_data = {
  1053. .preset = preset_v3,
  1054. .read_page = mxc_nand_read_page_v2_v3,
  1055. .send_cmd = send_cmd_v3,
  1056. .send_addr = send_addr_v3,
  1057. .send_page = send_page_v3,
  1058. .send_read_id = send_read_id_v3,
  1059. .get_dev_status = get_dev_status_v3,
  1060. .check_int = check_int_v3,
  1061. .irq_control = irq_control_v3,
  1062. .get_ecc_status = get_ecc_status_v3,
  1063. .ooblayout = &mxc_v2_ooblayout_ops,
  1064. .select_chip = mxc_nand_select_chip_v1_v3,
  1065. .enable_hwecc = mxc_nand_enable_hwecc_v3,
  1066. .irqpending_quirk = 0,
  1067. .needs_ip = 1,
  1068. .regs_offset = 0,
  1069. .spare0_offset = 0x1000,
  1070. .axi_offset = 0x1e00,
  1071. .spare_len = 64,
  1072. .eccbytes = 0,
  1073. .eccsize = 0,
  1074. .ppb_shift = 7,
  1075. };
  1076. /* v3.2b: i.MX53 */
  1077. static const struct mxc_nand_devtype_data imx53_nand_devtype_data = {
  1078. .preset = preset_v3,
  1079. .read_page = mxc_nand_read_page_v2_v3,
  1080. .send_cmd = send_cmd_v3,
  1081. .send_addr = send_addr_v3,
  1082. .send_page = send_page_v3,
  1083. .send_read_id = send_read_id_v3,
  1084. .get_dev_status = get_dev_status_v3,
  1085. .check_int = check_int_v3,
  1086. .irq_control = irq_control_v3,
  1087. .get_ecc_status = get_ecc_status_v3,
  1088. .ooblayout = &mxc_v2_ooblayout_ops,
  1089. .select_chip = mxc_nand_select_chip_v1_v3,
  1090. .enable_hwecc = mxc_nand_enable_hwecc_v3,
  1091. .irqpending_quirk = 0,
  1092. .needs_ip = 1,
  1093. .regs_offset = 0,
  1094. .spare0_offset = 0x1000,
  1095. .axi_offset = 0x1e00,
  1096. .spare_len = 64,
  1097. .eccbytes = 0,
  1098. .eccsize = 0,
  1099. .ppb_shift = 8,
  1100. };
  1101. static inline int is_imx21_nfc(struct mxc_nand_host *host)
  1102. {
  1103. return host->devtype_data == &imx21_nand_devtype_data;
  1104. }
  1105. static inline int is_imx27_nfc(struct mxc_nand_host *host)
  1106. {
  1107. return host->devtype_data == &imx27_nand_devtype_data;
  1108. }
  1109. static inline int is_imx25_nfc(struct mxc_nand_host *host)
  1110. {
  1111. return host->devtype_data == &imx25_nand_devtype_data;
  1112. }
  1113. static const struct of_device_id mxcnd_dt_ids[] = {
  1114. { .compatible = "fsl,imx21-nand", .data = &imx21_nand_devtype_data, },
  1115. { .compatible = "fsl,imx27-nand", .data = &imx27_nand_devtype_data, },
  1116. { .compatible = "fsl,imx25-nand", .data = &imx25_nand_devtype_data, },
  1117. { .compatible = "fsl,imx51-nand", .data = &imx51_nand_devtype_data, },
  1118. { .compatible = "fsl,imx53-nand", .data = &imx53_nand_devtype_data, },
  1119. { /* sentinel */ }
  1120. };
  1121. MODULE_DEVICE_TABLE(of, mxcnd_dt_ids);
  1122. static int mxcnd_attach_chip(struct nand_chip *chip)
  1123. {
  1124. struct mtd_info *mtd = nand_to_mtd(chip);
  1125. struct mxc_nand_host *host = nand_get_controller_data(chip);
  1126. struct device *dev = mtd->dev.parent;
  1127. chip->ecc.bytes = host->devtype_data->eccbytes;
  1128. host->eccsize = host->devtype_data->eccsize;
  1129. chip->ecc.size = 512;
  1130. switch (chip->ecc.engine_type) {
  1131. case NAND_ECC_ENGINE_TYPE_ON_HOST:
  1132. mtd_set_ooblayout(mtd, host->devtype_data->ooblayout);
  1133. chip->ecc.read_page = mxc_nand_read_page;
  1134. chip->ecc.read_page_raw = mxc_nand_read_page_raw;
  1135. chip->ecc.read_oob = mxc_nand_read_oob;
  1136. chip->ecc.write_page = mxc_nand_write_page_ecc;
  1137. chip->ecc.write_page_raw = mxc_nand_write_page_raw;
  1138. chip->ecc.write_oob = mxc_nand_write_oob;
  1139. break;
  1140. case NAND_ECC_ENGINE_TYPE_SOFT:
  1141. chip->ecc.write_page_raw = nand_monolithic_write_page_raw;
  1142. chip->ecc.read_page_raw = nand_monolithic_read_page_raw;
  1143. break;
  1144. default:
  1145. return -EINVAL;
  1146. }
  1147. if (chip->bbt_options & NAND_BBT_USE_FLASH) {
  1148. chip->bbt_td = &bbt_main_descr;
  1149. chip->bbt_md = &bbt_mirror_descr;
  1150. }
  1151. /* Allocate the right size buffer now */
  1152. devm_kfree(dev, (void *)host->data_buf);
  1153. host->data_buf = devm_kzalloc(dev, mtd->writesize + mtd->oobsize,
  1154. GFP_KERNEL);
  1155. if (!host->data_buf)
  1156. return -ENOMEM;
  1157. /* Call preset again, with correct writesize chip time */
  1158. host->devtype_data->preset(mtd);
  1159. if (!chip->ecc.bytes) {
  1160. if (host->eccsize == 8)
  1161. chip->ecc.bytes = 18;
  1162. else if (host->eccsize == 4)
  1163. chip->ecc.bytes = 9;
  1164. }
  1165. /*
  1166. * Experimentation shows that i.MX NFC can only handle up to 218 oob
  1167. * bytes. Limit used_oobsize to 218 so as to not confuse copy_spare()
  1168. * into copying invalid data to/from the spare IO buffer, as this
  1169. * might cause ECC data corruption when doing sub-page write to a
  1170. * partially written page.
  1171. */
  1172. host->used_oobsize = min(mtd->oobsize, 218U);
  1173. if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_ON_HOST) {
  1174. if (is_imx21_nfc(host) || is_imx27_nfc(host))
  1175. chip->ecc.strength = 1;
  1176. else
  1177. chip->ecc.strength = (host->eccsize == 4) ? 4 : 8;
  1178. }
  1179. return 0;
  1180. }
  1181. static int mxcnd_setup_interface(struct nand_chip *chip, int chipnr,
  1182. const struct nand_interface_config *conf)
  1183. {
  1184. struct mxc_nand_host *host = nand_get_controller_data(chip);
  1185. return host->devtype_data->setup_interface(chip, chipnr, conf);
  1186. }
  1187. static void memff16_toio(void *buf, int n)
  1188. {
  1189. __iomem u16 *t = buf;
  1190. int i;
  1191. for (i = 0; i < (n >> 1); i++)
  1192. __raw_writew(0xffff, t++);
  1193. }
  1194. static void copy_page_to_sram(struct mtd_info *mtd, const void *buf, int buf_len)
  1195. {
  1196. struct nand_chip *this = mtd_to_nand(mtd);
  1197. struct mxc_nand_host *host = nand_get_controller_data(this);
  1198. unsigned int no_subpages = mtd->writesize / 512;
  1199. int oob_per_subpage, i;
  1200. oob_per_subpage = (mtd->oobsize / no_subpages) & ~1;
  1201. /*
  1202. * During a page write the i.MX NAND controller will read 512b from
  1203. * main_area0 SRAM, then oob_per_subpage bytes from spare0 SRAM, then
  1204. * 512b from main_area1 SRAM and so on until the full page is written.
  1205. * For software ECC we want to have a 1:1 mapping between the raw page
  1206. * data on the NAND chip and the view of the NAND core. This is
  1207. * necessary to make the NAND_CMD_RNDOUT read the data it expects.
  1208. * To accomplish this we have to write the data in the order the controller
  1209. * reads it. This is reversed in copy_page_from_sram() below.
  1210. *
  1211. * buf_len can either be the full page including the OOB or user data only.
  1212. * When it's user data only make sure that we fill up the rest of the
  1213. * SRAM with 0xff.
  1214. */
  1215. for (i = 0; i < no_subpages; i++) {
  1216. int now = min(buf_len, 512);
  1217. if (now)
  1218. memcpy16_toio(host->main_area0 + i * 512, buf, now);
  1219. if (now < 512)
  1220. memff16_toio(host->main_area0 + i * 512 + now, 512 - now);
  1221. buf += 512;
  1222. buf_len -= now;
  1223. now = min(buf_len, oob_per_subpage);
  1224. if (now)
  1225. memcpy16_toio(host->spare0 + i * host->devtype_data->spare_len,
  1226. buf, now);
  1227. if (now < oob_per_subpage)
  1228. memff16_toio(host->spare0 + i * host->devtype_data->spare_len + now,
  1229. oob_per_subpage - now);
  1230. buf += oob_per_subpage;
  1231. buf_len -= now;
  1232. }
  1233. }
  1234. static void copy_page_from_sram(struct mtd_info *mtd)
  1235. {
  1236. struct nand_chip *this = mtd_to_nand(mtd);
  1237. struct mxc_nand_host *host = nand_get_controller_data(this);
  1238. void *buf = host->data_buf;
  1239. unsigned int no_subpages = mtd->writesize / 512;
  1240. int oob_per_subpage, i;
  1241. /* mtd->writesize is not set during ident scanning */
  1242. if (!no_subpages)
  1243. no_subpages = 1;
  1244. oob_per_subpage = (mtd->oobsize / no_subpages) & ~1;
  1245. for (i = 0; i < no_subpages; i++) {
  1246. memcpy16_fromio(buf, host->main_area0 + i * 512, 512);
  1247. buf += 512;
  1248. memcpy16_fromio(buf, host->spare0 + i * host->devtype_data->spare_len,
  1249. oob_per_subpage);
  1250. buf += oob_per_subpage;
  1251. }
  1252. }
  1253. static int mxcnd_do_exec_op(struct nand_chip *chip,
  1254. const struct nand_subop *op)
  1255. {
  1256. struct mxc_nand_host *host = nand_get_controller_data(chip);
  1257. struct mtd_info *mtd = nand_to_mtd(chip);
  1258. int i, j, buf_len;
  1259. void *buf_read = NULL;
  1260. const void *buf_write = NULL;
  1261. const struct nand_op_instr *instr;
  1262. bool readid = false;
  1263. bool statusreq = false;
  1264. for (i = 0; i < op->ninstrs; i++) {
  1265. instr = &op->instrs[i];
  1266. switch (instr->type) {
  1267. case NAND_OP_WAITRDY_INSTR:
  1268. /* NFC handles R/B internally, nothing to do here */
  1269. break;
  1270. case NAND_OP_CMD_INSTR:
  1271. host->devtype_data->send_cmd(host, instr->ctx.cmd.opcode, true);
  1272. if (instr->ctx.cmd.opcode == NAND_CMD_READID)
  1273. readid = true;
  1274. if (instr->ctx.cmd.opcode == NAND_CMD_STATUS)
  1275. statusreq = true;
  1276. break;
  1277. case NAND_OP_ADDR_INSTR:
  1278. for (j = 0; j < instr->ctx.addr.naddrs; j++) {
  1279. bool islast = j == instr->ctx.addr.naddrs - 1;
  1280. host->devtype_data->send_addr(host, instr->ctx.addr.addrs[j], islast);
  1281. }
  1282. break;
  1283. case NAND_OP_DATA_OUT_INSTR:
  1284. buf_write = instr->ctx.data.buf.out;
  1285. buf_len = instr->ctx.data.len;
  1286. if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_ON_HOST)
  1287. memcpy32_toio(host->main_area0, buf_write, buf_len);
  1288. else
  1289. copy_page_to_sram(mtd, buf_write, buf_len);
  1290. host->devtype_data->send_page(mtd, NFC_INPUT);
  1291. break;
  1292. case NAND_OP_DATA_IN_INSTR:
  1293. buf_read = instr->ctx.data.buf.in;
  1294. buf_len = instr->ctx.data.len;
  1295. if (readid) {
  1296. host->devtype_data->send_read_id(host);
  1297. readid = false;
  1298. memcpy32_fromio(host->data_buf, host->main_area0, buf_len * 2);
  1299. if (chip->options & NAND_BUSWIDTH_16) {
  1300. u8 *bufr = buf_read;
  1301. u16 *bufw = host->data_buf;
  1302. for (j = 0; j < buf_len; j++)
  1303. bufr[j] = bufw[j];
  1304. } else {
  1305. memcpy(buf_read, host->data_buf, buf_len);
  1306. }
  1307. break;
  1308. }
  1309. if (statusreq) {
  1310. *(u8*)buf_read = host->devtype_data->get_dev_status(host);
  1311. statusreq = false;
  1312. break;
  1313. }
  1314. host->devtype_data->read_page(chip);
  1315. if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_ON_HOST) {
  1316. if (IS_ALIGNED(buf_len, 4)) {
  1317. memcpy32_fromio(buf_read, host->main_area0, buf_len);
  1318. } else {
  1319. memcpy32_fromio(host->data_buf, host->main_area0, mtd->writesize);
  1320. memcpy(buf_read, host->data_buf, buf_len);
  1321. }
  1322. } else {
  1323. copy_page_from_sram(mtd);
  1324. memcpy(buf_read, host->data_buf, buf_len);
  1325. }
  1326. break;
  1327. }
  1328. }
  1329. return 0;
  1330. }
  1331. #define MAX_DATA_SIZE (4096 + 512)
  1332. static const struct nand_op_parser mxcnd_op_parser = NAND_OP_PARSER(
  1333. NAND_OP_PARSER_PATTERN(mxcnd_do_exec_op,
  1334. NAND_OP_PARSER_PAT_CMD_ELEM(false),
  1335. NAND_OP_PARSER_PAT_ADDR_ELEM(true, 7),
  1336. NAND_OP_PARSER_PAT_CMD_ELEM(true),
  1337. NAND_OP_PARSER_PAT_WAITRDY_ELEM(true),
  1338. NAND_OP_PARSER_PAT_DATA_IN_ELEM(true, MAX_DATA_SIZE)),
  1339. NAND_OP_PARSER_PATTERN(mxcnd_do_exec_op,
  1340. NAND_OP_PARSER_PAT_CMD_ELEM(false),
  1341. NAND_OP_PARSER_PAT_ADDR_ELEM(false, 7),
  1342. NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, MAX_DATA_SIZE),
  1343. NAND_OP_PARSER_PAT_CMD_ELEM(false),
  1344. NAND_OP_PARSER_PAT_WAITRDY_ELEM(true)),
  1345. NAND_OP_PARSER_PATTERN(mxcnd_do_exec_op,
  1346. NAND_OP_PARSER_PAT_CMD_ELEM(false),
  1347. NAND_OP_PARSER_PAT_ADDR_ELEM(false, 7),
  1348. NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, MAX_DATA_SIZE),
  1349. NAND_OP_PARSER_PAT_CMD_ELEM(true),
  1350. NAND_OP_PARSER_PAT_WAITRDY_ELEM(true)),
  1351. );
  1352. static int mxcnd_exec_op(struct nand_chip *chip,
  1353. const struct nand_operation *op, bool check_only)
  1354. {
  1355. return nand_op_parser_exec_op(chip, &mxcnd_op_parser,
  1356. op, check_only);
  1357. }
  1358. static const struct nand_controller_ops mxcnd_controller_ops = {
  1359. .attach_chip = mxcnd_attach_chip,
  1360. .setup_interface = mxcnd_setup_interface,
  1361. .exec_op = mxcnd_exec_op,
  1362. };
  1363. static int mxcnd_probe(struct platform_device *pdev)
  1364. {
  1365. struct nand_chip *this;
  1366. struct mtd_info *mtd;
  1367. struct mxc_nand_host *host;
  1368. int err = 0;
  1369. /* Allocate memory for MTD device structure and private data */
  1370. host = devm_kzalloc(&pdev->dev, sizeof(struct mxc_nand_host),
  1371. GFP_KERNEL);
  1372. if (!host)
  1373. return -ENOMEM;
  1374. /* allocate a temporary buffer for the nand_scan_ident() */
  1375. host->data_buf = devm_kzalloc(&pdev->dev, PAGE_SIZE, GFP_KERNEL);
  1376. if (!host->data_buf)
  1377. return -ENOMEM;
  1378. host->dev = &pdev->dev;
  1379. /* structures must be linked */
  1380. this = &host->nand;
  1381. mtd = nand_to_mtd(this);
  1382. mtd->dev.parent = &pdev->dev;
  1383. mtd->name = DRIVER_NAME;
  1384. /* 50 us command delay time */
  1385. this->legacy.chip_delay = 5;
  1386. nand_set_controller_data(this, host);
  1387. nand_set_flash_node(this, pdev->dev.of_node);
  1388. host->clk = devm_clk_get(&pdev->dev, NULL);
  1389. if (IS_ERR(host->clk))
  1390. return PTR_ERR(host->clk);
  1391. host->devtype_data = device_get_match_data(&pdev->dev);
  1392. if (!host->devtype_data->setup_interface)
  1393. this->options |= NAND_KEEP_TIMINGS;
  1394. if (host->devtype_data->needs_ip) {
  1395. host->regs_ip = devm_platform_ioremap_resource(pdev, 0);
  1396. if (IS_ERR(host->regs_ip))
  1397. return PTR_ERR(host->regs_ip);
  1398. host->base = devm_platform_ioremap_resource(pdev, 1);
  1399. } else {
  1400. host->base = devm_platform_ioremap_resource(pdev, 0);
  1401. }
  1402. if (IS_ERR(host->base))
  1403. return PTR_ERR(host->base);
  1404. host->main_area0 = host->base;
  1405. if (host->devtype_data->regs_offset)
  1406. host->regs = host->base + host->devtype_data->regs_offset;
  1407. host->spare0 = host->base + host->devtype_data->spare0_offset;
  1408. if (host->devtype_data->axi_offset)
  1409. host->regs_axi = host->base + host->devtype_data->axi_offset;
  1410. this->legacy.select_chip = host->devtype_data->select_chip;
  1411. init_completion(&host->op_completion);
  1412. host->irq = platform_get_irq(pdev, 0);
  1413. if (host->irq < 0)
  1414. return host->irq;
  1415. /*
  1416. * Use host->devtype_data->irq_control() here instead of irq_control()
  1417. * because we must not disable_irq_nosync without having requested the
  1418. * irq.
  1419. */
  1420. host->devtype_data->irq_control(host, 0);
  1421. err = devm_request_irq(&pdev->dev, host->irq, mxc_nfc_irq,
  1422. 0, DRIVER_NAME, host);
  1423. if (err)
  1424. return err;
  1425. err = clk_prepare_enable(host->clk);
  1426. if (err)
  1427. return err;
  1428. host->clk_act = 1;
  1429. /*
  1430. * Now that we "own" the interrupt make sure the interrupt mask bit is
  1431. * cleared on i.MX21. Otherwise we can't read the interrupt status bit
  1432. * on this machine.
  1433. */
  1434. if (host->devtype_data->irqpending_quirk) {
  1435. disable_irq_nosync(host->irq);
  1436. host->devtype_data->irq_control(host, 1);
  1437. }
  1438. /* Scan the NAND device */
  1439. this->legacy.dummy_controller.ops = &mxcnd_controller_ops;
  1440. err = nand_scan(this, is_imx25_nfc(host) ? 4 : 1);
  1441. if (err)
  1442. goto escan;
  1443. /* Register the partitions */
  1444. err = mtd_device_parse_register(mtd, part_probes, NULL, NULL, 0);
  1445. if (err)
  1446. goto cleanup_nand;
  1447. platform_set_drvdata(pdev, host);
  1448. return 0;
  1449. cleanup_nand:
  1450. nand_cleanup(this);
  1451. escan:
  1452. if (host->clk_act)
  1453. clk_disable_unprepare(host->clk);
  1454. return err;
  1455. }
  1456. static void mxcnd_remove(struct platform_device *pdev)
  1457. {
  1458. struct mxc_nand_host *host = platform_get_drvdata(pdev);
  1459. struct nand_chip *chip = &host->nand;
  1460. int ret;
  1461. ret = mtd_device_unregister(nand_to_mtd(chip));
  1462. WARN_ON(ret);
  1463. nand_cleanup(chip);
  1464. if (host->clk_act)
  1465. clk_disable_unprepare(host->clk);
  1466. }
  1467. static struct platform_driver mxcnd_driver = {
  1468. .driver = {
  1469. .name = DRIVER_NAME,
  1470. .of_match_table = mxcnd_dt_ids,
  1471. },
  1472. .probe = mxcnd_probe,
  1473. .remove_new = mxcnd_remove,
  1474. };
  1475. module_platform_driver(mxcnd_driver);
  1476. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  1477. MODULE_DESCRIPTION("MXC NAND MTD driver");
  1478. MODULE_LICENSE("GPL");