mxc_nand.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951
  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 mtd_info *mtd, int chip);
  122. int (*setup_data_interface)(struct mtd_info *mtd, 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 mtd_info *mtd)
  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 mtd_info *mtd, struct nand_chip *chip,
  656. uint8_t *buf, 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 mtd_info *mtd, struct nand_chip *chip,
  667. uint8_t *buf, 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 mtd_info *mtd, struct nand_chip *chip,
  678. int page)
  679. {
  680. struct mxc_nand_host *host = nand_get_controller_data(chip);
  681. return host->devtype_data->read_page(chip, NULL, chip->oob_poi, 0,
  682. page);
  683. }
  684. static int mxc_nand_write_page(struct nand_chip *chip, const uint8_t *buf,
  685. bool ecc, int page)
  686. {
  687. struct mtd_info *mtd = nand_to_mtd(chip);
  688. struct mxc_nand_host *host = nand_get_controller_data(chip);
  689. host->devtype_data->enable_hwecc(chip, ecc);
  690. host->devtype_data->send_cmd(host, NAND_CMD_SEQIN, false);
  691. mxc_do_addr_cycle(mtd, 0, page);
  692. memcpy32_toio(host->main_area0, buf, mtd->writesize);
  693. copy_spare(mtd, false, chip->oob_poi);
  694. host->devtype_data->send_page(mtd, NFC_INPUT);
  695. host->devtype_data->send_cmd(host, NAND_CMD_PAGEPROG, true);
  696. mxc_do_addr_cycle(mtd, 0, page);
  697. return 0;
  698. }
  699. static int mxc_nand_write_page_ecc(struct mtd_info *mtd, struct nand_chip *chip,
  700. const uint8_t *buf, int oob_required,
  701. int page)
  702. {
  703. return mxc_nand_write_page(chip, buf, true, page);
  704. }
  705. static int mxc_nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  706. const uint8_t *buf, int oob_required, int page)
  707. {
  708. return mxc_nand_write_page(chip, buf, false, page);
  709. }
  710. static int mxc_nand_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
  711. int page)
  712. {
  713. struct mxc_nand_host *host = nand_get_controller_data(chip);
  714. memset(host->data_buf, 0xff, mtd->writesize);
  715. return mxc_nand_write_page(chip, host->data_buf, false, page);
  716. }
  717. static u_char mxc_nand_read_byte(struct mtd_info *mtd)
  718. {
  719. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  720. struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
  721. uint8_t ret;
  722. /* Check for status request */
  723. if (host->status_request)
  724. return host->devtype_data->get_dev_status(host) & 0xFF;
  725. if (nand_chip->options & NAND_BUSWIDTH_16) {
  726. /* only take the lower byte of each word */
  727. ret = *(uint16_t *)(host->data_buf + host->buf_start);
  728. host->buf_start += 2;
  729. } else {
  730. ret = *(uint8_t *)(host->data_buf + host->buf_start);
  731. host->buf_start++;
  732. }
  733. dev_dbg(host->dev, "%s: ret=0x%hhx (start=%u)\n", __func__, ret, host->buf_start);
  734. return ret;
  735. }
  736. static uint16_t mxc_nand_read_word(struct mtd_info *mtd)
  737. {
  738. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  739. struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
  740. uint16_t ret;
  741. ret = *(uint16_t *)(host->data_buf + host->buf_start);
  742. host->buf_start += 2;
  743. return ret;
  744. }
  745. /* Write data of length len to buffer buf. The data to be
  746. * written on NAND Flash is first copied to RAMbuffer. After the Data Input
  747. * Operation by the NFC, the data is written to NAND Flash */
  748. static void mxc_nand_write_buf(struct mtd_info *mtd,
  749. const u_char *buf, int len)
  750. {
  751. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  752. struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
  753. u16 col = host->buf_start;
  754. int n = mtd->oobsize + mtd->writesize - col;
  755. n = min(n, len);
  756. memcpy(host->data_buf + col, buf, n);
  757. host->buf_start += n;
  758. }
  759. /* Read the data buffer from the NAND Flash. To read the data from NAND
  760. * Flash first the data output cycle is initiated by the NFC, which copies
  761. * the data to RAMbuffer. This data of length len is then copied to buffer buf.
  762. */
  763. static void mxc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  764. {
  765. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  766. struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
  767. u16 col = host->buf_start;
  768. int n = mtd->oobsize + mtd->writesize - col;
  769. n = min(n, len);
  770. memcpy(buf, host->data_buf + col, n);
  771. host->buf_start += n;
  772. }
  773. /* This function is used by upper layer for select and
  774. * deselect of the NAND chip */
  775. static void mxc_nand_select_chip_v1_v3(struct mtd_info *mtd, int chip)
  776. {
  777. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  778. struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
  779. if (chip == -1) {
  780. /* Disable the NFC clock */
  781. if (host->clk_act) {
  782. clk_disable_unprepare(host->clk);
  783. host->clk_act = 0;
  784. }
  785. return;
  786. }
  787. if (!host->clk_act) {
  788. /* Enable the NFC clock */
  789. clk_prepare_enable(host->clk);
  790. host->clk_act = 1;
  791. }
  792. }
  793. static void mxc_nand_select_chip_v2(struct mtd_info *mtd, int chip)
  794. {
  795. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  796. struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
  797. if (chip == -1) {
  798. /* Disable the NFC clock */
  799. if (host->clk_act) {
  800. clk_disable_unprepare(host->clk);
  801. host->clk_act = 0;
  802. }
  803. return;
  804. }
  805. if (!host->clk_act) {
  806. /* Enable the NFC clock */
  807. clk_prepare_enable(host->clk);
  808. host->clk_act = 1;
  809. }
  810. host->active_cs = chip;
  811. writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR);
  812. }
  813. #define MXC_V1_ECCBYTES 5
  814. static int mxc_v1_ooblayout_ecc(struct mtd_info *mtd, int section,
  815. struct mtd_oob_region *oobregion)
  816. {
  817. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  818. if (section >= nand_chip->ecc.steps)
  819. return -ERANGE;
  820. oobregion->offset = (section * 16) + 6;
  821. oobregion->length = MXC_V1_ECCBYTES;
  822. return 0;
  823. }
  824. static int mxc_v1_ooblayout_free(struct mtd_info *mtd, int section,
  825. struct mtd_oob_region *oobregion)
  826. {
  827. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  828. if (section > nand_chip->ecc.steps)
  829. return -ERANGE;
  830. if (!section) {
  831. if (mtd->writesize <= 512) {
  832. oobregion->offset = 0;
  833. oobregion->length = 5;
  834. } else {
  835. oobregion->offset = 2;
  836. oobregion->length = 4;
  837. }
  838. } else {
  839. oobregion->offset = ((section - 1) * 16) + MXC_V1_ECCBYTES + 6;
  840. if (section < nand_chip->ecc.steps)
  841. oobregion->length = (section * 16) + 6 -
  842. oobregion->offset;
  843. else
  844. oobregion->length = mtd->oobsize - oobregion->offset;
  845. }
  846. return 0;
  847. }
  848. static const struct mtd_ooblayout_ops mxc_v1_ooblayout_ops = {
  849. .ecc = mxc_v1_ooblayout_ecc,
  850. .free = mxc_v1_ooblayout_free,
  851. };
  852. static int mxc_v2_ooblayout_ecc(struct mtd_info *mtd, int section,
  853. struct mtd_oob_region *oobregion)
  854. {
  855. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  856. int stepsize = nand_chip->ecc.bytes == 9 ? 16 : 26;
  857. if (section >= nand_chip->ecc.steps)
  858. return -ERANGE;
  859. oobregion->offset = (section * stepsize) + 7;
  860. oobregion->length = nand_chip->ecc.bytes;
  861. return 0;
  862. }
  863. static int mxc_v2_ooblayout_free(struct mtd_info *mtd, int section,
  864. struct mtd_oob_region *oobregion)
  865. {
  866. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  867. int stepsize = nand_chip->ecc.bytes == 9 ? 16 : 26;
  868. if (section >= nand_chip->ecc.steps)
  869. return -ERANGE;
  870. if (!section) {
  871. if (mtd->writesize <= 512) {
  872. oobregion->offset = 0;
  873. oobregion->length = 5;
  874. } else {
  875. oobregion->offset = 2;
  876. oobregion->length = 4;
  877. }
  878. } else {
  879. oobregion->offset = section * stepsize;
  880. oobregion->length = 7;
  881. }
  882. return 0;
  883. }
  884. static const struct mtd_ooblayout_ops mxc_v2_ooblayout_ops = {
  885. .ecc = mxc_v2_ooblayout_ecc,
  886. .free = mxc_v2_ooblayout_free,
  887. };
  888. /*
  889. * v2 and v3 type controllers can do 4bit or 8bit ecc depending
  890. * on how much oob the nand chip has. For 8bit ecc we need at least
  891. * 26 bytes of oob data per 512 byte block.
  892. */
  893. static int get_eccsize(struct mtd_info *mtd)
  894. {
  895. int oobbytes_per_512 = 0;
  896. oobbytes_per_512 = mtd->oobsize * 512 / mtd->writesize;
  897. if (oobbytes_per_512 < 26)
  898. return 4;
  899. else
  900. return 8;
  901. }
  902. static void preset_v1(struct mtd_info *mtd)
  903. {
  904. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  905. struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
  906. uint16_t config1 = 0;
  907. if (nand_chip->ecc.mode == NAND_ECC_HW && mtd->writesize)
  908. config1 |= NFC_V1_V2_CONFIG1_ECC_EN;
  909. if (!host->devtype_data->irqpending_quirk)
  910. config1 |= NFC_V1_V2_CONFIG1_INT_MSK;
  911. host->eccsize = 1;
  912. writew(config1, NFC_V1_V2_CONFIG1);
  913. /* preset operation */
  914. /* Unlock the internal RAM Buffer */
  915. writew(0x2, NFC_V1_V2_CONFIG);
  916. /* Blocks to be unlocked */
  917. writew(0x0, NFC_V1_UNLOCKSTART_BLKADDR);
  918. writew(0xffff, NFC_V1_UNLOCKEND_BLKADDR);
  919. /* Unlock Block Command for given address range */
  920. writew(0x4, NFC_V1_V2_WRPROT);
  921. }
  922. static int mxc_nand_v2_setup_data_interface(struct mtd_info *mtd, int csline,
  923. const struct nand_data_interface *conf)
  924. {
  925. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  926. struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
  927. int tRC_min_ns, tRC_ps, ret;
  928. unsigned long rate, rate_round;
  929. const struct nand_sdr_timings *timings;
  930. u16 config1;
  931. timings = nand_get_sdr_timings(conf);
  932. if (IS_ERR(timings))
  933. return -ENOTSUPP;
  934. config1 = readw(NFC_V1_V2_CONFIG1);
  935. tRC_min_ns = timings->tRC_min / 1000;
  936. rate = 1000000000 / tRC_min_ns;
  937. /*
  938. * For tRC < 30ns we have to use EDO mode. In this case the controller
  939. * does one access per clock cycle. Otherwise the controller does one
  940. * access in two clock cycles, thus we have to double the rate to the
  941. * controller.
  942. */
  943. if (tRC_min_ns < 30) {
  944. rate_round = clk_round_rate(host->clk, rate);
  945. config1 |= NFC_V2_CONFIG1_ONE_CYCLE;
  946. tRC_ps = 1000000000 / (rate_round / 1000);
  947. } else {
  948. rate *= 2;
  949. rate_round = clk_round_rate(host->clk, rate);
  950. config1 &= ~NFC_V2_CONFIG1_ONE_CYCLE;
  951. tRC_ps = 1000000000 / (rate_round / 1000 / 2);
  952. }
  953. /*
  954. * The timing values compared against are from the i.MX25 Automotive
  955. * datasheet, Table 50. NFC Timing Parameters
  956. */
  957. if (timings->tCLS_min > tRC_ps - 1000 ||
  958. timings->tCLH_min > tRC_ps - 2000 ||
  959. timings->tCS_min > tRC_ps - 1000 ||
  960. timings->tCH_min > tRC_ps - 2000 ||
  961. timings->tWP_min > tRC_ps - 1500 ||
  962. timings->tALS_min > tRC_ps ||
  963. timings->tALH_min > tRC_ps - 3000 ||
  964. timings->tDS_min > tRC_ps ||
  965. timings->tDH_min > tRC_ps - 5000 ||
  966. timings->tWC_min > 2 * tRC_ps ||
  967. timings->tWH_min > tRC_ps - 2500 ||
  968. timings->tRR_min > 6 * tRC_ps ||
  969. timings->tRP_min > 3 * tRC_ps / 2 ||
  970. timings->tRC_min > 2 * tRC_ps ||
  971. timings->tREH_min > (tRC_ps / 2) - 2500) {
  972. dev_dbg(host->dev, "Timing out of bounds\n");
  973. return -EINVAL;
  974. }
  975. if (csline == NAND_DATA_IFACE_CHECK_ONLY)
  976. return 0;
  977. ret = clk_set_rate(host->clk, rate);
  978. if (ret)
  979. return ret;
  980. writew(config1, NFC_V1_V2_CONFIG1);
  981. dev_dbg(host->dev, "Setting rate to %ldHz, %s mode\n", rate_round,
  982. config1 & NFC_V2_CONFIG1_ONE_CYCLE ? "One cycle (EDO)" :
  983. "normal");
  984. return 0;
  985. }
  986. static void preset_v2(struct mtd_info *mtd)
  987. {
  988. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  989. struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
  990. uint16_t config1 = 0;
  991. config1 |= NFC_V2_CONFIG1_FP_INT;
  992. if (!host->devtype_data->irqpending_quirk)
  993. config1 |= NFC_V1_V2_CONFIG1_INT_MSK;
  994. if (mtd->writesize) {
  995. uint16_t pages_per_block = mtd->erasesize / mtd->writesize;
  996. if (nand_chip->ecc.mode == NAND_ECC_HW)
  997. config1 |= NFC_V1_V2_CONFIG1_ECC_EN;
  998. host->eccsize = get_eccsize(mtd);
  999. if (host->eccsize == 4)
  1000. config1 |= NFC_V2_CONFIG1_ECC_MODE_4;
  1001. config1 |= NFC_V2_CONFIG1_PPB(ffs(pages_per_block) - 6);
  1002. } else {
  1003. host->eccsize = 1;
  1004. }
  1005. writew(config1, NFC_V1_V2_CONFIG1);
  1006. /* preset operation */
  1007. /* spare area size in 16-bit half-words */
  1008. writew(mtd->oobsize / 2, NFC_V21_RSLTSPARE_AREA);
  1009. /* Unlock the internal RAM Buffer */
  1010. writew(0x2, NFC_V1_V2_CONFIG);
  1011. /* Blocks to be unlocked */
  1012. writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR0);
  1013. writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR1);
  1014. writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR2);
  1015. writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR3);
  1016. writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR0);
  1017. writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR1);
  1018. writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR2);
  1019. writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR3);
  1020. /* Unlock Block Command for given address range */
  1021. writew(0x4, NFC_V1_V2_WRPROT);
  1022. }
  1023. static void preset_v3(struct mtd_info *mtd)
  1024. {
  1025. struct nand_chip *chip = mtd_to_nand(mtd);
  1026. struct mxc_nand_host *host = nand_get_controller_data(chip);
  1027. uint32_t config2, config3;
  1028. int i, addr_phases;
  1029. writel(NFC_V3_CONFIG1_RBA(0), NFC_V3_CONFIG1);
  1030. writel(NFC_V3_IPC_CREQ, NFC_V3_IPC);
  1031. /* Unlock the internal RAM Buffer */
  1032. writel(NFC_V3_WRPROT_BLS_UNLOCK | NFC_V3_WRPROT_UNLOCK,
  1033. NFC_V3_WRPROT);
  1034. /* Blocks to be unlocked */
  1035. for (i = 0; i < NAND_MAX_CHIPS; i++)
  1036. writel(0xffff << 16, NFC_V3_WRPROT_UNLOCK_BLK_ADD0 + (i << 2));
  1037. writel(0, NFC_V3_IPC);
  1038. config2 = NFC_V3_CONFIG2_ONE_CYCLE |
  1039. NFC_V3_CONFIG2_2CMD_PHASES |
  1040. NFC_V3_CONFIG2_SPAS(mtd->oobsize >> 1) |
  1041. NFC_V3_CONFIG2_ST_CMD(0x70) |
  1042. NFC_V3_CONFIG2_INT_MSK |
  1043. NFC_V3_CONFIG2_NUM_ADDR_PHASE0;
  1044. addr_phases = fls(chip->pagemask) >> 3;
  1045. if (mtd->writesize == 2048) {
  1046. config2 |= NFC_V3_CONFIG2_PS_2048;
  1047. config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases);
  1048. } else if (mtd->writesize == 4096) {
  1049. config2 |= NFC_V3_CONFIG2_PS_4096;
  1050. config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases);
  1051. } else {
  1052. config2 |= NFC_V3_CONFIG2_PS_512;
  1053. config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases - 1);
  1054. }
  1055. if (mtd->writesize) {
  1056. if (chip->ecc.mode == NAND_ECC_HW)
  1057. config2 |= NFC_V3_CONFIG2_ECC_EN;
  1058. config2 |= NFC_V3_CONFIG2_PPB(
  1059. ffs(mtd->erasesize / mtd->writesize) - 6,
  1060. host->devtype_data->ppb_shift);
  1061. host->eccsize = get_eccsize(mtd);
  1062. if (host->eccsize == 8)
  1063. config2 |= NFC_V3_CONFIG2_ECC_MODE_8;
  1064. }
  1065. writel(config2, NFC_V3_CONFIG2);
  1066. config3 = NFC_V3_CONFIG3_NUM_OF_DEVICES(0) |
  1067. NFC_V3_CONFIG3_NO_SDMA |
  1068. NFC_V3_CONFIG3_RBB_MODE |
  1069. NFC_V3_CONFIG3_SBB(6) | /* Reset default */
  1070. NFC_V3_CONFIG3_ADD_OP(0);
  1071. if (!(chip->options & NAND_BUSWIDTH_16))
  1072. config3 |= NFC_V3_CONFIG3_FW8;
  1073. writel(config3, NFC_V3_CONFIG3);
  1074. writel(0, NFC_V3_DELAY_LINE);
  1075. }
  1076. /* Used by the upper layer to write command to NAND Flash for
  1077. * different operations to be carried out on NAND Flash */
  1078. static void mxc_nand_command(struct mtd_info *mtd, unsigned command,
  1079. int column, int page_addr)
  1080. {
  1081. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  1082. struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
  1083. dev_dbg(host->dev, "mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n",
  1084. command, column, page_addr);
  1085. /* Reset command state information */
  1086. host->status_request = false;
  1087. /* Command pre-processing step */
  1088. switch (command) {
  1089. case NAND_CMD_RESET:
  1090. host->devtype_data->preset(mtd);
  1091. host->devtype_data->send_cmd(host, command, false);
  1092. break;
  1093. case NAND_CMD_STATUS:
  1094. host->buf_start = 0;
  1095. host->status_request = true;
  1096. host->devtype_data->send_cmd(host, command, true);
  1097. WARN_ONCE(column != -1 || page_addr != -1,
  1098. "Unexpected column/row value (cmd=%u, col=%d, row=%d)\n",
  1099. command, column, page_addr);
  1100. mxc_do_addr_cycle(mtd, column, page_addr);
  1101. break;
  1102. case NAND_CMD_READID:
  1103. host->devtype_data->send_cmd(host, command, true);
  1104. mxc_do_addr_cycle(mtd, column, page_addr);
  1105. host->devtype_data->send_read_id(host);
  1106. host->buf_start = 0;
  1107. break;
  1108. case NAND_CMD_ERASE1:
  1109. case NAND_CMD_ERASE2:
  1110. host->devtype_data->send_cmd(host, command, false);
  1111. WARN_ONCE(column != -1,
  1112. "Unexpected column value (cmd=%u, col=%d)\n",
  1113. command, column);
  1114. mxc_do_addr_cycle(mtd, column, page_addr);
  1115. break;
  1116. case NAND_CMD_PARAM:
  1117. host->devtype_data->send_cmd(host, command, false);
  1118. mxc_do_addr_cycle(mtd, column, page_addr);
  1119. host->devtype_data->send_page(mtd, NFC_OUTPUT);
  1120. memcpy32_fromio(host->data_buf, host->main_area0, 512);
  1121. host->buf_start = 0;
  1122. break;
  1123. default:
  1124. WARN_ONCE(1, "Unimplemented command (cmd=%u)\n",
  1125. command);
  1126. break;
  1127. }
  1128. }
  1129. static int mxc_nand_set_features(struct mtd_info *mtd, struct nand_chip *chip,
  1130. int addr, u8 *subfeature_param)
  1131. {
  1132. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  1133. struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
  1134. int i;
  1135. host->buf_start = 0;
  1136. for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
  1137. chip->write_byte(mtd, subfeature_param[i]);
  1138. memcpy32_toio(host->main_area0, host->data_buf, mtd->writesize);
  1139. host->devtype_data->send_cmd(host, NAND_CMD_SET_FEATURES, false);
  1140. mxc_do_addr_cycle(mtd, addr, -1);
  1141. host->devtype_data->send_page(mtd, NFC_INPUT);
  1142. return 0;
  1143. }
  1144. static int mxc_nand_get_features(struct mtd_info *mtd, struct nand_chip *chip,
  1145. int addr, u8 *subfeature_param)
  1146. {
  1147. struct nand_chip *nand_chip = mtd_to_nand(mtd);
  1148. struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
  1149. int i;
  1150. host->devtype_data->send_cmd(host, NAND_CMD_GET_FEATURES, false);
  1151. mxc_do_addr_cycle(mtd, addr, -1);
  1152. host->devtype_data->send_page(mtd, NFC_OUTPUT);
  1153. memcpy32_fromio(host->data_buf, host->main_area0, 512);
  1154. host->buf_start = 0;
  1155. for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
  1156. *subfeature_param++ = chip->read_byte(mtd);
  1157. return 0;
  1158. }
  1159. /*
  1160. * The generic flash bbt decriptors overlap with our ecc
  1161. * hardware, so define some i.MX specific ones.
  1162. */
  1163. static uint8_t bbt_pattern[] = { 'B', 'b', 't', '0' };
  1164. static uint8_t mirror_pattern[] = { '1', 't', 'b', 'B' };
  1165. static struct nand_bbt_descr bbt_main_descr = {
  1166. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1167. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  1168. .offs = 0,
  1169. .len = 4,
  1170. .veroffs = 4,
  1171. .maxblocks = 4,
  1172. .pattern = bbt_pattern,
  1173. };
  1174. static struct nand_bbt_descr bbt_mirror_descr = {
  1175. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  1176. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  1177. .offs = 0,
  1178. .len = 4,
  1179. .veroffs = 4,
  1180. .maxblocks = 4,
  1181. .pattern = mirror_pattern,
  1182. };
  1183. /* v1 + irqpending_quirk: i.MX21 */
  1184. static const struct mxc_nand_devtype_data imx21_nand_devtype_data = {
  1185. .preset = preset_v1,
  1186. .read_page = mxc_nand_read_page_v1,
  1187. .send_cmd = send_cmd_v1_v2,
  1188. .send_addr = send_addr_v1_v2,
  1189. .send_page = send_page_v1,
  1190. .send_read_id = send_read_id_v1_v2,
  1191. .get_dev_status = get_dev_status_v1_v2,
  1192. .check_int = check_int_v1_v2,
  1193. .irq_control = irq_control_v1_v2,
  1194. .get_ecc_status = get_ecc_status_v1,
  1195. .ooblayout = &mxc_v1_ooblayout_ops,
  1196. .select_chip = mxc_nand_select_chip_v1_v3,
  1197. .enable_hwecc = mxc_nand_enable_hwecc_v1_v2,
  1198. .irqpending_quirk = 1,
  1199. .needs_ip = 0,
  1200. .regs_offset = 0xe00,
  1201. .spare0_offset = 0x800,
  1202. .spare_len = 16,
  1203. .eccbytes = 3,
  1204. .eccsize = 1,
  1205. };
  1206. /* v1 + !irqpending_quirk: i.MX27, i.MX31 */
  1207. static const struct mxc_nand_devtype_data imx27_nand_devtype_data = {
  1208. .preset = preset_v1,
  1209. .read_page = mxc_nand_read_page_v1,
  1210. .send_cmd = send_cmd_v1_v2,
  1211. .send_addr = send_addr_v1_v2,
  1212. .send_page = send_page_v1,
  1213. .send_read_id = send_read_id_v1_v2,
  1214. .get_dev_status = get_dev_status_v1_v2,
  1215. .check_int = check_int_v1_v2,
  1216. .irq_control = irq_control_v1_v2,
  1217. .get_ecc_status = get_ecc_status_v1,
  1218. .ooblayout = &mxc_v1_ooblayout_ops,
  1219. .select_chip = mxc_nand_select_chip_v1_v3,
  1220. .enable_hwecc = mxc_nand_enable_hwecc_v1_v2,
  1221. .irqpending_quirk = 0,
  1222. .needs_ip = 0,
  1223. .regs_offset = 0xe00,
  1224. .spare0_offset = 0x800,
  1225. .axi_offset = 0,
  1226. .spare_len = 16,
  1227. .eccbytes = 3,
  1228. .eccsize = 1,
  1229. };
  1230. /* v21: i.MX25, i.MX35 */
  1231. static const struct mxc_nand_devtype_data imx25_nand_devtype_data = {
  1232. .preset = preset_v2,
  1233. .read_page = mxc_nand_read_page_v2_v3,
  1234. .send_cmd = send_cmd_v1_v2,
  1235. .send_addr = send_addr_v1_v2,
  1236. .send_page = send_page_v2,
  1237. .send_read_id = send_read_id_v1_v2,
  1238. .get_dev_status = get_dev_status_v1_v2,
  1239. .check_int = check_int_v1_v2,
  1240. .irq_control = irq_control_v1_v2,
  1241. .get_ecc_status = get_ecc_status_v2,
  1242. .ooblayout = &mxc_v2_ooblayout_ops,
  1243. .select_chip = mxc_nand_select_chip_v2,
  1244. .setup_data_interface = mxc_nand_v2_setup_data_interface,
  1245. .enable_hwecc = mxc_nand_enable_hwecc_v1_v2,
  1246. .irqpending_quirk = 0,
  1247. .needs_ip = 0,
  1248. .regs_offset = 0x1e00,
  1249. .spare0_offset = 0x1000,
  1250. .axi_offset = 0,
  1251. .spare_len = 64,
  1252. .eccbytes = 9,
  1253. .eccsize = 0,
  1254. };
  1255. /* v3.2a: i.MX51 */
  1256. static const struct mxc_nand_devtype_data imx51_nand_devtype_data = {
  1257. .preset = preset_v3,
  1258. .read_page = mxc_nand_read_page_v2_v3,
  1259. .send_cmd = send_cmd_v3,
  1260. .send_addr = send_addr_v3,
  1261. .send_page = send_page_v3,
  1262. .send_read_id = send_read_id_v3,
  1263. .get_dev_status = get_dev_status_v3,
  1264. .check_int = check_int_v3,
  1265. .irq_control = irq_control_v3,
  1266. .get_ecc_status = get_ecc_status_v3,
  1267. .ooblayout = &mxc_v2_ooblayout_ops,
  1268. .select_chip = mxc_nand_select_chip_v1_v3,
  1269. .enable_hwecc = mxc_nand_enable_hwecc_v3,
  1270. .irqpending_quirk = 0,
  1271. .needs_ip = 1,
  1272. .regs_offset = 0,
  1273. .spare0_offset = 0x1000,
  1274. .axi_offset = 0x1e00,
  1275. .spare_len = 64,
  1276. .eccbytes = 0,
  1277. .eccsize = 0,
  1278. .ppb_shift = 7,
  1279. };
  1280. /* v3.2b: i.MX53 */
  1281. static const struct mxc_nand_devtype_data imx53_nand_devtype_data = {
  1282. .preset = preset_v3,
  1283. .read_page = mxc_nand_read_page_v2_v3,
  1284. .send_cmd = send_cmd_v3,
  1285. .send_addr = send_addr_v3,
  1286. .send_page = send_page_v3,
  1287. .send_read_id = send_read_id_v3,
  1288. .get_dev_status = get_dev_status_v3,
  1289. .check_int = check_int_v3,
  1290. .irq_control = irq_control_v3,
  1291. .get_ecc_status = get_ecc_status_v3,
  1292. .ooblayout = &mxc_v2_ooblayout_ops,
  1293. .select_chip = mxc_nand_select_chip_v1_v3,
  1294. .enable_hwecc = mxc_nand_enable_hwecc_v3,
  1295. .irqpending_quirk = 0,
  1296. .needs_ip = 1,
  1297. .regs_offset = 0,
  1298. .spare0_offset = 0x1000,
  1299. .axi_offset = 0x1e00,
  1300. .spare_len = 64,
  1301. .eccbytes = 0,
  1302. .eccsize = 0,
  1303. .ppb_shift = 8,
  1304. };
  1305. static inline int is_imx21_nfc(struct mxc_nand_host *host)
  1306. {
  1307. return host->devtype_data == &imx21_nand_devtype_data;
  1308. }
  1309. static inline int is_imx27_nfc(struct mxc_nand_host *host)
  1310. {
  1311. return host->devtype_data == &imx27_nand_devtype_data;
  1312. }
  1313. static inline int is_imx25_nfc(struct mxc_nand_host *host)
  1314. {
  1315. return host->devtype_data == &imx25_nand_devtype_data;
  1316. }
  1317. static inline int is_imx51_nfc(struct mxc_nand_host *host)
  1318. {
  1319. return host->devtype_data == &imx51_nand_devtype_data;
  1320. }
  1321. static inline int is_imx53_nfc(struct mxc_nand_host *host)
  1322. {
  1323. return host->devtype_data == &imx53_nand_devtype_data;
  1324. }
  1325. static const struct platform_device_id mxcnd_devtype[] = {
  1326. {
  1327. .name = "imx21-nand",
  1328. .driver_data = (kernel_ulong_t) &imx21_nand_devtype_data,
  1329. }, {
  1330. .name = "imx27-nand",
  1331. .driver_data = (kernel_ulong_t) &imx27_nand_devtype_data,
  1332. }, {
  1333. .name = "imx25-nand",
  1334. .driver_data = (kernel_ulong_t) &imx25_nand_devtype_data,
  1335. }, {
  1336. .name = "imx51-nand",
  1337. .driver_data = (kernel_ulong_t) &imx51_nand_devtype_data,
  1338. }, {
  1339. .name = "imx53-nand",
  1340. .driver_data = (kernel_ulong_t) &imx53_nand_devtype_data,
  1341. }, {
  1342. /* sentinel */
  1343. }
  1344. };
  1345. MODULE_DEVICE_TABLE(platform, mxcnd_devtype);
  1346. #ifdef CONFIG_OF
  1347. static const struct of_device_id mxcnd_dt_ids[] = {
  1348. {
  1349. .compatible = "fsl,imx21-nand",
  1350. .data = &imx21_nand_devtype_data,
  1351. }, {
  1352. .compatible = "fsl,imx27-nand",
  1353. .data = &imx27_nand_devtype_data,
  1354. }, {
  1355. .compatible = "fsl,imx25-nand",
  1356. .data = &imx25_nand_devtype_data,
  1357. }, {
  1358. .compatible = "fsl,imx51-nand",
  1359. .data = &imx51_nand_devtype_data,
  1360. }, {
  1361. .compatible = "fsl,imx53-nand",
  1362. .data = &imx53_nand_devtype_data,
  1363. },
  1364. { /* sentinel */ }
  1365. };
  1366. MODULE_DEVICE_TABLE(of, mxcnd_dt_ids);
  1367. static int mxcnd_probe_dt(struct mxc_nand_host *host)
  1368. {
  1369. struct device_node *np = host->dev->of_node;
  1370. const struct of_device_id *of_id =
  1371. of_match_device(mxcnd_dt_ids, host->dev);
  1372. if (!np)
  1373. return 1;
  1374. host->devtype_data = of_id->data;
  1375. return 0;
  1376. }
  1377. #else
  1378. static int mxcnd_probe_dt(struct mxc_nand_host *host)
  1379. {
  1380. return 1;
  1381. }
  1382. #endif
  1383. static int mxcnd_attach_chip(struct nand_chip *chip)
  1384. {
  1385. struct mtd_info *mtd = nand_to_mtd(chip);
  1386. struct mxc_nand_host *host = nand_get_controller_data(chip);
  1387. struct device *dev = mtd->dev.parent;
  1388. switch (chip->ecc.mode) {
  1389. case NAND_ECC_HW:
  1390. chip->ecc.read_page = mxc_nand_read_page;
  1391. chip->ecc.read_page_raw = mxc_nand_read_page_raw;
  1392. chip->ecc.read_oob = mxc_nand_read_oob;
  1393. chip->ecc.write_page = mxc_nand_write_page_ecc;
  1394. chip->ecc.write_page_raw = mxc_nand_write_page_raw;
  1395. chip->ecc.write_oob = mxc_nand_write_oob;
  1396. break;
  1397. case NAND_ECC_SOFT:
  1398. break;
  1399. default:
  1400. return -EINVAL;
  1401. }
  1402. if (chip->bbt_options & NAND_BBT_USE_FLASH) {
  1403. chip->bbt_td = &bbt_main_descr;
  1404. chip->bbt_md = &bbt_mirror_descr;
  1405. }
  1406. /* Allocate the right size buffer now */
  1407. devm_kfree(dev, (void *)host->data_buf);
  1408. host->data_buf = devm_kzalloc(dev, mtd->writesize + mtd->oobsize,
  1409. GFP_KERNEL);
  1410. if (!host->data_buf)
  1411. return -ENOMEM;
  1412. /* Call preset again, with correct writesize chip time */
  1413. host->devtype_data->preset(mtd);
  1414. if (!chip->ecc.bytes) {
  1415. if (host->eccsize == 8)
  1416. chip->ecc.bytes = 18;
  1417. else if (host->eccsize == 4)
  1418. chip->ecc.bytes = 9;
  1419. }
  1420. /*
  1421. * Experimentation shows that i.MX NFC can only handle up to 218 oob
  1422. * bytes. Limit used_oobsize to 218 so as to not confuse copy_spare()
  1423. * into copying invalid data to/from the spare IO buffer, as this
  1424. * might cause ECC data corruption when doing sub-page write to a
  1425. * partially written page.
  1426. */
  1427. host->used_oobsize = min(mtd->oobsize, 218U);
  1428. if (chip->ecc.mode == NAND_ECC_HW) {
  1429. if (is_imx21_nfc(host) || is_imx27_nfc(host))
  1430. chip->ecc.strength = 1;
  1431. else
  1432. chip->ecc.strength = (host->eccsize == 4) ? 4 : 8;
  1433. }
  1434. return 0;
  1435. }
  1436. static const struct nand_controller_ops mxcnd_controller_ops = {
  1437. .attach_chip = mxcnd_attach_chip,
  1438. };
  1439. static int mxcnd_probe(struct platform_device *pdev)
  1440. {
  1441. struct nand_chip *this;
  1442. struct mtd_info *mtd;
  1443. struct mxc_nand_host *host;
  1444. struct resource *res;
  1445. int err = 0;
  1446. /* Allocate memory for MTD device structure and private data */
  1447. host = devm_kzalloc(&pdev->dev, sizeof(struct mxc_nand_host),
  1448. GFP_KERNEL);
  1449. if (!host)
  1450. return -ENOMEM;
  1451. /* allocate a temporary buffer for the nand_scan_ident() */
  1452. host->data_buf = devm_kzalloc(&pdev->dev, PAGE_SIZE, GFP_KERNEL);
  1453. if (!host->data_buf)
  1454. return -ENOMEM;
  1455. host->dev = &pdev->dev;
  1456. /* structures must be linked */
  1457. this = &host->nand;
  1458. mtd = nand_to_mtd(this);
  1459. mtd->dev.parent = &pdev->dev;
  1460. mtd->name = DRIVER_NAME;
  1461. /* 50 us command delay time */
  1462. this->chip_delay = 5;
  1463. nand_set_controller_data(this, host);
  1464. nand_set_flash_node(this, pdev->dev.of_node),
  1465. this->dev_ready = mxc_nand_dev_ready;
  1466. this->cmdfunc = mxc_nand_command;
  1467. this->read_byte = mxc_nand_read_byte;
  1468. this->read_word = mxc_nand_read_word;
  1469. this->write_buf = mxc_nand_write_buf;
  1470. this->read_buf = mxc_nand_read_buf;
  1471. this->set_features = mxc_nand_set_features;
  1472. this->get_features = mxc_nand_get_features;
  1473. host->clk = devm_clk_get(&pdev->dev, NULL);
  1474. if (IS_ERR(host->clk))
  1475. return PTR_ERR(host->clk);
  1476. err = mxcnd_probe_dt(host);
  1477. if (err > 0) {
  1478. struct mxc_nand_platform_data *pdata =
  1479. dev_get_platdata(&pdev->dev);
  1480. if (pdata) {
  1481. host->pdata = *pdata;
  1482. host->devtype_data = (struct mxc_nand_devtype_data *)
  1483. pdev->id_entry->driver_data;
  1484. } else {
  1485. err = -ENODEV;
  1486. }
  1487. }
  1488. if (err < 0)
  1489. return err;
  1490. this->setup_data_interface = host->devtype_data->setup_data_interface;
  1491. if (host->devtype_data->needs_ip) {
  1492. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1493. host->regs_ip = devm_ioremap_resource(&pdev->dev, res);
  1494. if (IS_ERR(host->regs_ip))
  1495. return PTR_ERR(host->regs_ip);
  1496. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1497. } else {
  1498. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1499. }
  1500. host->base = devm_ioremap_resource(&pdev->dev, res);
  1501. if (IS_ERR(host->base))
  1502. return PTR_ERR(host->base);
  1503. host->main_area0 = host->base;
  1504. if (host->devtype_data->regs_offset)
  1505. host->regs = host->base + host->devtype_data->regs_offset;
  1506. host->spare0 = host->base + host->devtype_data->spare0_offset;
  1507. if (host->devtype_data->axi_offset)
  1508. host->regs_axi = host->base + host->devtype_data->axi_offset;
  1509. this->ecc.bytes = host->devtype_data->eccbytes;
  1510. host->eccsize = host->devtype_data->eccsize;
  1511. this->select_chip = host->devtype_data->select_chip;
  1512. this->ecc.size = 512;
  1513. mtd_set_ooblayout(mtd, host->devtype_data->ooblayout);
  1514. if (host->pdata.hw_ecc) {
  1515. this->ecc.mode = NAND_ECC_HW;
  1516. } else {
  1517. this->ecc.mode = NAND_ECC_SOFT;
  1518. this->ecc.algo = NAND_ECC_HAMMING;
  1519. }
  1520. /* NAND bus width determines access functions used by upper layer */
  1521. if (host->pdata.width == 2)
  1522. this->options |= NAND_BUSWIDTH_16;
  1523. /* update flash based bbt */
  1524. if (host->pdata.flash_bbt)
  1525. this->bbt_options |= NAND_BBT_USE_FLASH;
  1526. init_completion(&host->op_completion);
  1527. host->irq = platform_get_irq(pdev, 0);
  1528. if (host->irq < 0)
  1529. return host->irq;
  1530. /*
  1531. * Use host->devtype_data->irq_control() here instead of irq_control()
  1532. * because we must not disable_irq_nosync without having requested the
  1533. * irq.
  1534. */
  1535. host->devtype_data->irq_control(host, 0);
  1536. err = devm_request_irq(&pdev->dev, host->irq, mxc_nfc_irq,
  1537. 0, DRIVER_NAME, host);
  1538. if (err)
  1539. return err;
  1540. err = clk_prepare_enable(host->clk);
  1541. if (err)
  1542. return err;
  1543. host->clk_act = 1;
  1544. /*
  1545. * Now that we "own" the interrupt make sure the interrupt mask bit is
  1546. * cleared on i.MX21. Otherwise we can't read the interrupt status bit
  1547. * on this machine.
  1548. */
  1549. if (host->devtype_data->irqpending_quirk) {
  1550. disable_irq_nosync(host->irq);
  1551. host->devtype_data->irq_control(host, 1);
  1552. }
  1553. /* Scan the NAND device */
  1554. this->dummy_controller.ops = &mxcnd_controller_ops;
  1555. err = nand_scan(mtd, is_imx25_nfc(host) ? 4 : 1);
  1556. if (err)
  1557. goto escan;
  1558. /* Register the partitions */
  1559. err = mtd_device_parse_register(mtd, part_probes, NULL,
  1560. host->pdata.parts,
  1561. host->pdata.nr_parts);
  1562. if (err)
  1563. goto cleanup_nand;
  1564. platform_set_drvdata(pdev, host);
  1565. return 0;
  1566. cleanup_nand:
  1567. nand_cleanup(this);
  1568. escan:
  1569. if (host->clk_act)
  1570. clk_disable_unprepare(host->clk);
  1571. return err;
  1572. }
  1573. static int mxcnd_remove(struct platform_device *pdev)
  1574. {
  1575. struct mxc_nand_host *host = platform_get_drvdata(pdev);
  1576. nand_release(nand_to_mtd(&host->nand));
  1577. if (host->clk_act)
  1578. clk_disable_unprepare(host->clk);
  1579. return 0;
  1580. }
  1581. static struct platform_driver mxcnd_driver = {
  1582. .driver = {
  1583. .name = DRIVER_NAME,
  1584. .of_match_table = of_match_ptr(mxcnd_dt_ids),
  1585. },
  1586. .id_table = mxcnd_devtype,
  1587. .probe = mxcnd_probe,
  1588. .remove = mxcnd_remove,
  1589. };
  1590. module_platform_driver(mxcnd_driver);
  1591. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  1592. MODULE_DESCRIPTION("MXC NAND MTD driver");
  1593. MODULE_LICENSE("GPL");