mxc_nand.c 49 KB

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