mxc_nand.c 50 KB

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