mxc_nand.c 49 KB

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