mxc_nand.c 51 KB

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