mxc_nand.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602
  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/nand.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_device.h>
  35. #include <linux/of_mtd.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. 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. struct nand_ecclayout *ecclayout_512, *ecclayout_2k, *ecclayout_4k;
  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. /*
  137. * On i.MX21 the CONFIG2:INT bit cannot be read if interrupts are masked
  138. * (CONFIG1:INT_MSK is set). To handle this the driver uses
  139. * enable_irq/disable_irq_nosync instead of CONFIG1:INT_MSK
  140. */
  141. int irqpending_quirk;
  142. int needs_ip;
  143. size_t regs_offset;
  144. size_t spare0_offset;
  145. size_t axi_offset;
  146. int spare_len;
  147. int eccbytes;
  148. int eccsize;
  149. int ppb_shift;
  150. };
  151. struct mxc_nand_host {
  152. struct mtd_info mtd;
  153. struct nand_chip nand;
  154. struct device *dev;
  155. void __iomem *spare0;
  156. void __iomem *main_area0;
  157. void __iomem *base;
  158. void __iomem *regs;
  159. void __iomem *regs_axi;
  160. void __iomem *regs_ip;
  161. int status_request;
  162. struct clk *clk;
  163. int clk_act;
  164. int irq;
  165. int eccsize;
  166. int active_cs;
  167. struct completion op_completion;
  168. uint8_t *data_buf;
  169. unsigned int buf_start;
  170. const struct mxc_nand_devtype_data *devtype_data;
  171. struct mxc_nand_platform_data pdata;
  172. };
  173. /* OOB placement block for use with hardware ecc generation */
  174. static struct nand_ecclayout nandv1_hw_eccoob_smallpage = {
  175. .eccbytes = 5,
  176. .eccpos = {6, 7, 8, 9, 10},
  177. .oobfree = {{0, 5}, {12, 4}, }
  178. };
  179. static struct nand_ecclayout nandv1_hw_eccoob_largepage = {
  180. .eccbytes = 20,
  181. .eccpos = {6, 7, 8, 9, 10, 22, 23, 24, 25, 26,
  182. 38, 39, 40, 41, 42, 54, 55, 56, 57, 58},
  183. .oobfree = {{2, 4}, {11, 10}, {27, 10}, {43, 10}, {59, 5}, }
  184. };
  185. /* OOB description for 512 byte pages with 16 byte OOB */
  186. static struct nand_ecclayout nandv2_hw_eccoob_smallpage = {
  187. .eccbytes = 1 * 9,
  188. .eccpos = {
  189. 7, 8, 9, 10, 11, 12, 13, 14, 15
  190. },
  191. .oobfree = {
  192. {.offset = 0, .length = 5}
  193. }
  194. };
  195. /* OOB description for 2048 byte pages with 64 byte OOB */
  196. static struct nand_ecclayout nandv2_hw_eccoob_largepage = {
  197. .eccbytes = 4 * 9,
  198. .eccpos = {
  199. 7, 8, 9, 10, 11, 12, 13, 14, 15,
  200. 23, 24, 25, 26, 27, 28, 29, 30, 31,
  201. 39, 40, 41, 42, 43, 44, 45, 46, 47,
  202. 55, 56, 57, 58, 59, 60, 61, 62, 63
  203. },
  204. .oobfree = {
  205. {.offset = 2, .length = 4},
  206. {.offset = 16, .length = 7},
  207. {.offset = 32, .length = 7},
  208. {.offset = 48, .length = 7}
  209. }
  210. };
  211. /* OOB description for 4096 byte pages with 128 byte OOB */
  212. static struct nand_ecclayout nandv2_hw_eccoob_4k = {
  213. .eccbytes = 8 * 9,
  214. .eccpos = {
  215. 7, 8, 9, 10, 11, 12, 13, 14, 15,
  216. 23, 24, 25, 26, 27, 28, 29, 30, 31,
  217. 39, 40, 41, 42, 43, 44, 45, 46, 47,
  218. 55, 56, 57, 58, 59, 60, 61, 62, 63,
  219. 71, 72, 73, 74, 75, 76, 77, 78, 79,
  220. 87, 88, 89, 90, 91, 92, 93, 94, 95,
  221. 103, 104, 105, 106, 107, 108, 109, 110, 111,
  222. 119, 120, 121, 122, 123, 124, 125, 126, 127,
  223. },
  224. .oobfree = {
  225. {.offset = 2, .length = 4},
  226. {.offset = 16, .length = 7},
  227. {.offset = 32, .length = 7},
  228. {.offset = 48, .length = 7},
  229. {.offset = 64, .length = 7},
  230. {.offset = 80, .length = 7},
  231. {.offset = 96, .length = 7},
  232. {.offset = 112, .length = 7},
  233. }
  234. };
  235. static const char const *part_probes[] = {
  236. "cmdlinepart", "RedBoot", "ofpart", NULL };
  237. static void memcpy32_fromio(void *trg, const void __iomem *src, size_t size)
  238. {
  239. int i;
  240. u32 *t = trg;
  241. const __iomem u32 *s = src;
  242. for (i = 0; i < (size >> 2); i++)
  243. *t++ = __raw_readl(s++);
  244. }
  245. static void memcpy32_toio(void __iomem *trg, const void *src, int size)
  246. {
  247. int i;
  248. u32 __iomem *t = trg;
  249. const u32 *s = src;
  250. for (i = 0; i < (size >> 2); i++)
  251. __raw_writel(*s++, t++);
  252. }
  253. static int check_int_v3(struct mxc_nand_host *host)
  254. {
  255. uint32_t tmp;
  256. tmp = readl(NFC_V3_IPC);
  257. if (!(tmp & NFC_V3_IPC_INT))
  258. return 0;
  259. tmp &= ~NFC_V3_IPC_INT;
  260. writel(tmp, NFC_V3_IPC);
  261. return 1;
  262. }
  263. static int check_int_v1_v2(struct mxc_nand_host *host)
  264. {
  265. uint32_t tmp;
  266. tmp = readw(NFC_V1_V2_CONFIG2);
  267. if (!(tmp & NFC_V1_V2_CONFIG2_INT))
  268. return 0;
  269. if (!host->devtype_data->irqpending_quirk)
  270. writew(tmp & ~NFC_V1_V2_CONFIG2_INT, NFC_V1_V2_CONFIG2);
  271. return 1;
  272. }
  273. static void irq_control_v1_v2(struct mxc_nand_host *host, int activate)
  274. {
  275. uint16_t tmp;
  276. tmp = readw(NFC_V1_V2_CONFIG1);
  277. if (activate)
  278. tmp &= ~NFC_V1_V2_CONFIG1_INT_MSK;
  279. else
  280. tmp |= NFC_V1_V2_CONFIG1_INT_MSK;
  281. writew(tmp, NFC_V1_V2_CONFIG1);
  282. }
  283. static void irq_control_v3(struct mxc_nand_host *host, int activate)
  284. {
  285. uint32_t tmp;
  286. tmp = readl(NFC_V3_CONFIG2);
  287. if (activate)
  288. tmp &= ~NFC_V3_CONFIG2_INT_MSK;
  289. else
  290. tmp |= NFC_V3_CONFIG2_INT_MSK;
  291. writel(tmp, NFC_V3_CONFIG2);
  292. }
  293. static void irq_control(struct mxc_nand_host *host, int activate)
  294. {
  295. if (host->devtype_data->irqpending_quirk) {
  296. if (activate)
  297. enable_irq(host->irq);
  298. else
  299. disable_irq_nosync(host->irq);
  300. } else {
  301. host->devtype_data->irq_control(host, activate);
  302. }
  303. }
  304. static u32 get_ecc_status_v1(struct mxc_nand_host *host)
  305. {
  306. return readw(NFC_V1_V2_ECC_STATUS_RESULT);
  307. }
  308. static u32 get_ecc_status_v2(struct mxc_nand_host *host)
  309. {
  310. return readl(NFC_V1_V2_ECC_STATUS_RESULT);
  311. }
  312. static u32 get_ecc_status_v3(struct mxc_nand_host *host)
  313. {
  314. return readl(NFC_V3_ECC_STATUS_RESULT);
  315. }
  316. static irqreturn_t mxc_nfc_irq(int irq, void *dev_id)
  317. {
  318. struct mxc_nand_host *host = dev_id;
  319. if (!host->devtype_data->check_int(host))
  320. return IRQ_NONE;
  321. irq_control(host, 0);
  322. complete(&host->op_completion);
  323. return IRQ_HANDLED;
  324. }
  325. /* This function polls the NANDFC to wait for the basic operation to
  326. * complete by checking the INT bit of config2 register.
  327. */
  328. static void wait_op_done(struct mxc_nand_host *host, int useirq)
  329. {
  330. int max_retries = 8000;
  331. if (useirq) {
  332. if (!host->devtype_data->check_int(host)) {
  333. INIT_COMPLETION(host->op_completion);
  334. irq_control(host, 1);
  335. wait_for_completion(&host->op_completion);
  336. }
  337. } else {
  338. while (max_retries-- > 0) {
  339. if (host->devtype_data->check_int(host))
  340. break;
  341. udelay(1);
  342. }
  343. if (max_retries < 0)
  344. pr_debug("%s: INT not set\n", __func__);
  345. }
  346. }
  347. static void send_cmd_v3(struct mxc_nand_host *host, uint16_t cmd, int useirq)
  348. {
  349. /* fill command */
  350. writel(cmd, NFC_V3_FLASH_CMD);
  351. /* send out command */
  352. writel(NFC_CMD, NFC_V3_LAUNCH);
  353. /* Wait for operation to complete */
  354. wait_op_done(host, useirq);
  355. }
  356. /* This function issues the specified command to the NAND device and
  357. * waits for completion. */
  358. static void send_cmd_v1_v2(struct mxc_nand_host *host, uint16_t cmd, int useirq)
  359. {
  360. pr_debug("send_cmd(host, 0x%x, %d)\n", cmd, useirq);
  361. writew(cmd, NFC_V1_V2_FLASH_CMD);
  362. writew(NFC_CMD, NFC_V1_V2_CONFIG2);
  363. if (host->devtype_data->irqpending_quirk && (cmd == NAND_CMD_RESET)) {
  364. int max_retries = 100;
  365. /* Reset completion is indicated by NFC_CONFIG2 */
  366. /* being set to 0 */
  367. while (max_retries-- > 0) {
  368. if (readw(NFC_V1_V2_CONFIG2) == 0) {
  369. break;
  370. }
  371. udelay(1);
  372. }
  373. if (max_retries < 0)
  374. pr_debug("%s: RESET failed\n", __func__);
  375. } else {
  376. /* Wait for operation to complete */
  377. wait_op_done(host, useirq);
  378. }
  379. }
  380. static void send_addr_v3(struct mxc_nand_host *host, uint16_t addr, int islast)
  381. {
  382. /* fill address */
  383. writel(addr, NFC_V3_FLASH_ADDR0);
  384. /* send out address */
  385. writel(NFC_ADDR, NFC_V3_LAUNCH);
  386. wait_op_done(host, 0);
  387. }
  388. /* This function sends an address (or partial address) to the
  389. * NAND device. The address is used to select the source/destination for
  390. * a NAND command. */
  391. static void send_addr_v1_v2(struct mxc_nand_host *host, uint16_t addr, int islast)
  392. {
  393. pr_debug("send_addr(host, 0x%x %d)\n", addr, islast);
  394. writew(addr, NFC_V1_V2_FLASH_ADDR);
  395. writew(NFC_ADDR, NFC_V1_V2_CONFIG2);
  396. /* Wait for operation to complete */
  397. wait_op_done(host, islast);
  398. }
  399. static void send_page_v3(struct mtd_info *mtd, unsigned int ops)
  400. {
  401. struct nand_chip *nand_chip = mtd->priv;
  402. struct mxc_nand_host *host = nand_chip->priv;
  403. uint32_t tmp;
  404. tmp = readl(NFC_V3_CONFIG1);
  405. tmp &= ~(7 << 4);
  406. writel(tmp, NFC_V3_CONFIG1);
  407. /* transfer data from NFC ram to nand */
  408. writel(ops, NFC_V3_LAUNCH);
  409. wait_op_done(host, false);
  410. }
  411. static void send_page_v2(struct mtd_info *mtd, unsigned int ops)
  412. {
  413. struct nand_chip *nand_chip = mtd->priv;
  414. struct mxc_nand_host *host = nand_chip->priv;
  415. /* NANDFC buffer 0 is used for page read/write */
  416. writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR);
  417. writew(ops, NFC_V1_V2_CONFIG2);
  418. /* Wait for operation to complete */
  419. wait_op_done(host, true);
  420. }
  421. static void send_page_v1(struct mtd_info *mtd, unsigned int ops)
  422. {
  423. struct nand_chip *nand_chip = mtd->priv;
  424. struct mxc_nand_host *host = nand_chip->priv;
  425. int bufs, i;
  426. if (mtd->writesize > 512)
  427. bufs = 4;
  428. else
  429. bufs = 1;
  430. for (i = 0; i < bufs; i++) {
  431. /* NANDFC buffer 0 is used for page read/write */
  432. writew((host->active_cs << 4) | i, NFC_V1_V2_BUF_ADDR);
  433. writew(ops, NFC_V1_V2_CONFIG2);
  434. /* Wait for operation to complete */
  435. wait_op_done(host, true);
  436. }
  437. }
  438. static void send_read_id_v3(struct mxc_nand_host *host)
  439. {
  440. struct nand_chip *this = &host->nand;
  441. /* Read ID into main buffer */
  442. writel(NFC_ID, NFC_V3_LAUNCH);
  443. wait_op_done(host, true);
  444. memcpy32_fromio(host->data_buf, host->main_area0, 16);
  445. if (this->options & NAND_BUSWIDTH_16) {
  446. /* compress the ID info */
  447. host->data_buf[1] = host->data_buf[2];
  448. host->data_buf[2] = host->data_buf[4];
  449. host->data_buf[3] = host->data_buf[6];
  450. host->data_buf[4] = host->data_buf[8];
  451. host->data_buf[5] = host->data_buf[10];
  452. }
  453. }
  454. /* Request the NANDFC to perform a read of the NAND device ID. */
  455. static void send_read_id_v1_v2(struct mxc_nand_host *host)
  456. {
  457. struct nand_chip *this = &host->nand;
  458. /* NANDFC buffer 0 is used for device ID output */
  459. writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR);
  460. writew(NFC_ID, NFC_V1_V2_CONFIG2);
  461. /* Wait for operation to complete */
  462. wait_op_done(host, true);
  463. memcpy32_fromio(host->data_buf, host->main_area0, 16);
  464. if (this->options & NAND_BUSWIDTH_16) {
  465. /* compress the ID info */
  466. host->data_buf[1] = host->data_buf[2];
  467. host->data_buf[2] = host->data_buf[4];
  468. host->data_buf[3] = host->data_buf[6];
  469. host->data_buf[4] = host->data_buf[8];
  470. host->data_buf[5] = host->data_buf[10];
  471. }
  472. }
  473. static uint16_t get_dev_status_v3(struct mxc_nand_host *host)
  474. {
  475. writew(NFC_STATUS, NFC_V3_LAUNCH);
  476. wait_op_done(host, true);
  477. return readl(NFC_V3_CONFIG1) >> 16;
  478. }
  479. /* This function requests the NANDFC to perform a read of the
  480. * NAND device status and returns the current status. */
  481. static uint16_t get_dev_status_v1_v2(struct mxc_nand_host *host)
  482. {
  483. void __iomem *main_buf = host->main_area0;
  484. uint32_t store;
  485. uint16_t ret;
  486. writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR);
  487. /*
  488. * The device status is stored in main_area0. To
  489. * prevent corruption of the buffer save the value
  490. * and restore it afterwards.
  491. */
  492. store = readl(main_buf);
  493. writew(NFC_STATUS, NFC_V1_V2_CONFIG2);
  494. wait_op_done(host, true);
  495. ret = readw(main_buf);
  496. writel(store, main_buf);
  497. return ret;
  498. }
  499. /* This functions is used by upper layer to checks if device is ready */
  500. static int mxc_nand_dev_ready(struct mtd_info *mtd)
  501. {
  502. /*
  503. * NFC handles R/B internally. Therefore, this function
  504. * always returns status as ready.
  505. */
  506. return 1;
  507. }
  508. static void mxc_nand_enable_hwecc(struct mtd_info *mtd, int mode)
  509. {
  510. /*
  511. * If HW ECC is enabled, we turn it on during init. There is
  512. * no need to enable again here.
  513. */
  514. }
  515. static int mxc_nand_correct_data_v1(struct mtd_info *mtd, u_char *dat,
  516. u_char *read_ecc, u_char *calc_ecc)
  517. {
  518. struct nand_chip *nand_chip = mtd->priv;
  519. struct mxc_nand_host *host = nand_chip->priv;
  520. /*
  521. * 1-Bit errors are automatically corrected in HW. No need for
  522. * additional correction. 2-Bit errors cannot be corrected by
  523. * HW ECC, so we need to return failure
  524. */
  525. uint16_t ecc_status = get_ecc_status_v1(host);
  526. if (((ecc_status & 0x3) == 2) || ((ecc_status >> 2) == 2)) {
  527. pr_debug("MXC_NAND: HWECC uncorrectable 2-bit ECC error\n");
  528. return -1;
  529. }
  530. return 0;
  531. }
  532. static int mxc_nand_correct_data_v2_v3(struct mtd_info *mtd, u_char *dat,
  533. u_char *read_ecc, u_char *calc_ecc)
  534. {
  535. struct nand_chip *nand_chip = mtd->priv;
  536. struct mxc_nand_host *host = nand_chip->priv;
  537. u32 ecc_stat, err;
  538. int no_subpages = 1;
  539. int ret = 0;
  540. u8 ecc_bit_mask, err_limit;
  541. ecc_bit_mask = (host->eccsize == 4) ? 0x7 : 0xf;
  542. err_limit = (host->eccsize == 4) ? 0x4 : 0x8;
  543. no_subpages = mtd->writesize >> 9;
  544. ecc_stat = host->devtype_data->get_ecc_status(host);
  545. do {
  546. err = ecc_stat & ecc_bit_mask;
  547. if (err > err_limit) {
  548. printk(KERN_WARNING "UnCorrectable RS-ECC Error\n");
  549. return -1;
  550. } else {
  551. ret += err;
  552. }
  553. ecc_stat >>= 4;
  554. } while (--no_subpages);
  555. mtd->ecc_stats.corrected += ret;
  556. pr_debug("%d Symbol Correctable RS-ECC Error\n", ret);
  557. return ret;
  558. }
  559. static int mxc_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
  560. u_char *ecc_code)
  561. {
  562. return 0;
  563. }
  564. static u_char mxc_nand_read_byte(struct mtd_info *mtd)
  565. {
  566. struct nand_chip *nand_chip = mtd->priv;
  567. struct mxc_nand_host *host = nand_chip->priv;
  568. uint8_t ret;
  569. /* Check for status request */
  570. if (host->status_request)
  571. return host->devtype_data->get_dev_status(host) & 0xFF;
  572. ret = *(uint8_t *)(host->data_buf + host->buf_start);
  573. host->buf_start++;
  574. return ret;
  575. }
  576. static uint16_t mxc_nand_read_word(struct mtd_info *mtd)
  577. {
  578. struct nand_chip *nand_chip = mtd->priv;
  579. struct mxc_nand_host *host = nand_chip->priv;
  580. uint16_t ret;
  581. ret = *(uint16_t *)(host->data_buf + host->buf_start);
  582. host->buf_start += 2;
  583. return ret;
  584. }
  585. /* Write data of length len to buffer buf. The data to be
  586. * written on NAND Flash is first copied to RAMbuffer. After the Data Input
  587. * Operation by the NFC, the data is written to NAND Flash */
  588. static void mxc_nand_write_buf(struct mtd_info *mtd,
  589. const u_char *buf, int len)
  590. {
  591. struct nand_chip *nand_chip = mtd->priv;
  592. struct mxc_nand_host *host = nand_chip->priv;
  593. u16 col = host->buf_start;
  594. int n = mtd->oobsize + mtd->writesize - col;
  595. n = min(n, len);
  596. memcpy(host->data_buf + col, buf, n);
  597. host->buf_start += n;
  598. }
  599. /* Read the data buffer from the NAND Flash. To read the data from NAND
  600. * Flash first the data output cycle is initiated by the NFC, which copies
  601. * the data to RAMbuffer. This data of length len is then copied to buffer buf.
  602. */
  603. static void mxc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  604. {
  605. struct nand_chip *nand_chip = mtd->priv;
  606. struct mxc_nand_host *host = nand_chip->priv;
  607. u16 col = host->buf_start;
  608. int n = mtd->oobsize + mtd->writesize - col;
  609. n = min(n, len);
  610. memcpy(buf, host->data_buf + col, n);
  611. host->buf_start += n;
  612. }
  613. /* This function is used by upper layer for select and
  614. * deselect of the NAND chip */
  615. static void mxc_nand_select_chip_v1_v3(struct mtd_info *mtd, int chip)
  616. {
  617. struct nand_chip *nand_chip = mtd->priv;
  618. struct mxc_nand_host *host = nand_chip->priv;
  619. if (chip == -1) {
  620. /* Disable the NFC clock */
  621. if (host->clk_act) {
  622. clk_disable_unprepare(host->clk);
  623. host->clk_act = 0;
  624. }
  625. return;
  626. }
  627. if (!host->clk_act) {
  628. /* Enable the NFC clock */
  629. clk_prepare_enable(host->clk);
  630. host->clk_act = 1;
  631. }
  632. }
  633. static void mxc_nand_select_chip_v2(struct mtd_info *mtd, int chip)
  634. {
  635. struct nand_chip *nand_chip = mtd->priv;
  636. struct mxc_nand_host *host = nand_chip->priv;
  637. if (chip == -1) {
  638. /* Disable the NFC clock */
  639. if (host->clk_act) {
  640. clk_disable_unprepare(host->clk);
  641. host->clk_act = 0;
  642. }
  643. return;
  644. }
  645. if (!host->clk_act) {
  646. /* Enable the NFC clock */
  647. clk_prepare_enable(host->clk);
  648. host->clk_act = 1;
  649. }
  650. host->active_cs = chip;
  651. writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR);
  652. }
  653. /*
  654. * Function to transfer data to/from spare area.
  655. */
  656. static void copy_spare(struct mtd_info *mtd, bool bfrom)
  657. {
  658. struct nand_chip *this = mtd->priv;
  659. struct mxc_nand_host *host = this->priv;
  660. u16 i, j;
  661. u16 n = mtd->writesize >> 9;
  662. u8 *d = host->data_buf + mtd->writesize;
  663. u8 __iomem *s = host->spare0;
  664. u16 t = host->devtype_data->spare_len;
  665. j = (mtd->oobsize / n >> 1) << 1;
  666. if (bfrom) {
  667. for (i = 0; i < n - 1; i++)
  668. memcpy32_fromio(d + i * j, s + i * t, j);
  669. /* the last section */
  670. memcpy32_fromio(d + i * j, s + i * t, mtd->oobsize - i * j);
  671. } else {
  672. for (i = 0; i < n - 1; i++)
  673. memcpy32_toio(&s[i * t], &d[i * j], j);
  674. /* the last section */
  675. memcpy32_toio(&s[i * t], &d[i * j], mtd->oobsize - i * j);
  676. }
  677. }
  678. static void mxc_do_addr_cycle(struct mtd_info *mtd, int column, int page_addr)
  679. {
  680. struct nand_chip *nand_chip = mtd->priv;
  681. struct mxc_nand_host *host = nand_chip->priv;
  682. /* Write out column address, if necessary */
  683. if (column != -1) {
  684. /*
  685. * MXC NANDFC can only perform full page+spare or
  686. * spare-only read/write. When the upper layers
  687. * perform a read/write buf operation, the saved column
  688. * address is used to index into the full page.
  689. */
  690. host->devtype_data->send_addr(host, 0, page_addr == -1);
  691. if (mtd->writesize > 512)
  692. /* another col addr cycle for 2k page */
  693. host->devtype_data->send_addr(host, 0, false);
  694. }
  695. /* Write out page address, if necessary */
  696. if (page_addr != -1) {
  697. /* paddr_0 - p_addr_7 */
  698. host->devtype_data->send_addr(host, (page_addr & 0xff), false);
  699. if (mtd->writesize > 512) {
  700. if (mtd->size >= 0x10000000) {
  701. /* paddr_8 - paddr_15 */
  702. host->devtype_data->send_addr(host,
  703. (page_addr >> 8) & 0xff,
  704. false);
  705. host->devtype_data->send_addr(host,
  706. (page_addr >> 16) & 0xff,
  707. true);
  708. } else
  709. /* paddr_8 - paddr_15 */
  710. host->devtype_data->send_addr(host,
  711. (page_addr >> 8) & 0xff, true);
  712. } else {
  713. /* One more address cycle for higher density devices */
  714. if (mtd->size >= 0x4000000) {
  715. /* paddr_8 - paddr_15 */
  716. host->devtype_data->send_addr(host,
  717. (page_addr >> 8) & 0xff,
  718. false);
  719. host->devtype_data->send_addr(host,
  720. (page_addr >> 16) & 0xff,
  721. true);
  722. } else
  723. /* paddr_8 - paddr_15 */
  724. host->devtype_data->send_addr(host,
  725. (page_addr >> 8) & 0xff, true);
  726. }
  727. }
  728. }
  729. /*
  730. * v2 and v3 type controllers can do 4bit or 8bit ecc depending
  731. * on how much oob the nand chip has. For 8bit ecc we need at least
  732. * 26 bytes of oob data per 512 byte block.
  733. */
  734. static int get_eccsize(struct mtd_info *mtd)
  735. {
  736. int oobbytes_per_512 = 0;
  737. oobbytes_per_512 = mtd->oobsize * 512 / mtd->writesize;
  738. if (oobbytes_per_512 < 26)
  739. return 4;
  740. else
  741. return 8;
  742. }
  743. static void preset_v1(struct mtd_info *mtd)
  744. {
  745. struct nand_chip *nand_chip = mtd->priv;
  746. struct mxc_nand_host *host = nand_chip->priv;
  747. uint16_t config1 = 0;
  748. if (nand_chip->ecc.mode == NAND_ECC_HW)
  749. config1 |= NFC_V1_V2_CONFIG1_ECC_EN;
  750. if (!host->devtype_data->irqpending_quirk)
  751. config1 |= NFC_V1_V2_CONFIG1_INT_MSK;
  752. host->eccsize = 1;
  753. writew(config1, NFC_V1_V2_CONFIG1);
  754. /* preset operation */
  755. /* Unlock the internal RAM Buffer */
  756. writew(0x2, NFC_V1_V2_CONFIG);
  757. /* Blocks to be unlocked */
  758. writew(0x0, NFC_V1_UNLOCKSTART_BLKADDR);
  759. writew(0xffff, NFC_V1_UNLOCKEND_BLKADDR);
  760. /* Unlock Block Command for given address range */
  761. writew(0x4, NFC_V1_V2_WRPROT);
  762. }
  763. static void preset_v2(struct mtd_info *mtd)
  764. {
  765. struct nand_chip *nand_chip = mtd->priv;
  766. struct mxc_nand_host *host = nand_chip->priv;
  767. uint16_t config1 = 0;
  768. if (nand_chip->ecc.mode == NAND_ECC_HW)
  769. config1 |= NFC_V1_V2_CONFIG1_ECC_EN;
  770. config1 |= NFC_V2_CONFIG1_FP_INT;
  771. if (!host->devtype_data->irqpending_quirk)
  772. config1 |= NFC_V1_V2_CONFIG1_INT_MSK;
  773. if (mtd->writesize) {
  774. uint16_t pages_per_block = mtd->erasesize / mtd->writesize;
  775. host->eccsize = get_eccsize(mtd);
  776. if (host->eccsize == 4)
  777. config1 |= NFC_V2_CONFIG1_ECC_MODE_4;
  778. config1 |= NFC_V2_CONFIG1_PPB(ffs(pages_per_block) - 6);
  779. } else {
  780. host->eccsize = 1;
  781. }
  782. writew(config1, NFC_V1_V2_CONFIG1);
  783. /* preset operation */
  784. /* Unlock the internal RAM Buffer */
  785. writew(0x2, NFC_V1_V2_CONFIG);
  786. /* Blocks to be unlocked */
  787. writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR0);
  788. writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR1);
  789. writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR2);
  790. writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR3);
  791. writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR0);
  792. writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR1);
  793. writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR2);
  794. writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR3);
  795. /* Unlock Block Command for given address range */
  796. writew(0x4, NFC_V1_V2_WRPROT);
  797. }
  798. static void preset_v3(struct mtd_info *mtd)
  799. {
  800. struct nand_chip *chip = mtd->priv;
  801. struct mxc_nand_host *host = chip->priv;
  802. uint32_t config2, config3;
  803. int i, addr_phases;
  804. writel(NFC_V3_CONFIG1_RBA(0), NFC_V3_CONFIG1);
  805. writel(NFC_V3_IPC_CREQ, NFC_V3_IPC);
  806. /* Unlock the internal RAM Buffer */
  807. writel(NFC_V3_WRPROT_BLS_UNLOCK | NFC_V3_WRPROT_UNLOCK,
  808. NFC_V3_WRPROT);
  809. /* Blocks to be unlocked */
  810. for (i = 0; i < NAND_MAX_CHIPS; i++)
  811. writel(0x0 | (0xffff << 16),
  812. NFC_V3_WRPROT_UNLOCK_BLK_ADD0 + (i << 2));
  813. writel(0, NFC_V3_IPC);
  814. config2 = NFC_V3_CONFIG2_ONE_CYCLE |
  815. NFC_V3_CONFIG2_2CMD_PHASES |
  816. NFC_V3_CONFIG2_SPAS(mtd->oobsize >> 1) |
  817. NFC_V3_CONFIG2_ST_CMD(0x70) |
  818. NFC_V3_CONFIG2_INT_MSK |
  819. NFC_V3_CONFIG2_NUM_ADDR_PHASE0;
  820. if (chip->ecc.mode == NAND_ECC_HW)
  821. config2 |= NFC_V3_CONFIG2_ECC_EN;
  822. addr_phases = fls(chip->pagemask) >> 3;
  823. if (mtd->writesize == 2048) {
  824. config2 |= NFC_V3_CONFIG2_PS_2048;
  825. config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases);
  826. } else if (mtd->writesize == 4096) {
  827. config2 |= NFC_V3_CONFIG2_PS_4096;
  828. config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases);
  829. } else {
  830. config2 |= NFC_V3_CONFIG2_PS_512;
  831. config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases - 1);
  832. }
  833. if (mtd->writesize) {
  834. config2 |= NFC_V3_CONFIG2_PPB(
  835. ffs(mtd->erasesize / mtd->writesize) - 6,
  836. host->devtype_data->ppb_shift);
  837. host->eccsize = get_eccsize(mtd);
  838. if (host->eccsize == 8)
  839. config2 |= NFC_V3_CONFIG2_ECC_MODE_8;
  840. }
  841. writel(config2, NFC_V3_CONFIG2);
  842. config3 = NFC_V3_CONFIG3_NUM_OF_DEVICES(0) |
  843. NFC_V3_CONFIG3_NO_SDMA |
  844. NFC_V3_CONFIG3_RBB_MODE |
  845. NFC_V3_CONFIG3_SBB(6) | /* Reset default */
  846. NFC_V3_CONFIG3_ADD_OP(0);
  847. if (!(chip->options & NAND_BUSWIDTH_16))
  848. config3 |= NFC_V3_CONFIG3_FW8;
  849. writel(config3, NFC_V3_CONFIG3);
  850. writel(0, NFC_V3_DELAY_LINE);
  851. }
  852. /* Used by the upper layer to write command to NAND Flash for
  853. * different operations to be carried out on NAND Flash */
  854. static void mxc_nand_command(struct mtd_info *mtd, unsigned command,
  855. int column, int page_addr)
  856. {
  857. struct nand_chip *nand_chip = mtd->priv;
  858. struct mxc_nand_host *host = nand_chip->priv;
  859. pr_debug("mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n",
  860. command, column, page_addr);
  861. /* Reset command state information */
  862. host->status_request = false;
  863. /* Command pre-processing step */
  864. switch (command) {
  865. case NAND_CMD_RESET:
  866. host->devtype_data->preset(mtd);
  867. host->devtype_data->send_cmd(host, command, false);
  868. break;
  869. case NAND_CMD_STATUS:
  870. host->buf_start = 0;
  871. host->status_request = true;
  872. host->devtype_data->send_cmd(host, command, true);
  873. mxc_do_addr_cycle(mtd, column, page_addr);
  874. break;
  875. case NAND_CMD_READ0:
  876. case NAND_CMD_READOOB:
  877. if (command == NAND_CMD_READ0)
  878. host->buf_start = column;
  879. else
  880. host->buf_start = column + mtd->writesize;
  881. command = NAND_CMD_READ0; /* only READ0 is valid */
  882. host->devtype_data->send_cmd(host, command, false);
  883. mxc_do_addr_cycle(mtd, column, page_addr);
  884. if (mtd->writesize > 512)
  885. host->devtype_data->send_cmd(host,
  886. NAND_CMD_READSTART, true);
  887. host->devtype_data->send_page(mtd, NFC_OUTPUT);
  888. memcpy32_fromio(host->data_buf, host->main_area0,
  889. mtd->writesize);
  890. copy_spare(mtd, true);
  891. break;
  892. case NAND_CMD_SEQIN:
  893. if (column >= mtd->writesize)
  894. /* call ourself to read a page */
  895. mxc_nand_command(mtd, NAND_CMD_READ0, 0, page_addr);
  896. host->buf_start = column;
  897. host->devtype_data->send_cmd(host, command, false);
  898. mxc_do_addr_cycle(mtd, column, page_addr);
  899. break;
  900. case NAND_CMD_PAGEPROG:
  901. memcpy32_toio(host->main_area0, host->data_buf, mtd->writesize);
  902. copy_spare(mtd, false);
  903. host->devtype_data->send_page(mtd, NFC_INPUT);
  904. host->devtype_data->send_cmd(host, command, true);
  905. mxc_do_addr_cycle(mtd, column, page_addr);
  906. break;
  907. case NAND_CMD_READID:
  908. host->devtype_data->send_cmd(host, command, true);
  909. mxc_do_addr_cycle(mtd, column, page_addr);
  910. host->devtype_data->send_read_id(host);
  911. host->buf_start = column;
  912. break;
  913. case NAND_CMD_ERASE1:
  914. case NAND_CMD_ERASE2:
  915. host->devtype_data->send_cmd(host, command, false);
  916. mxc_do_addr_cycle(mtd, column, page_addr);
  917. break;
  918. }
  919. }
  920. /*
  921. * The generic flash bbt decriptors overlap with our ecc
  922. * hardware, so define some i.MX specific ones.
  923. */
  924. static uint8_t bbt_pattern[] = { 'B', 'b', 't', '0' };
  925. static uint8_t mirror_pattern[] = { '1', 't', 'b', 'B' };
  926. static struct nand_bbt_descr bbt_main_descr = {
  927. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  928. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  929. .offs = 0,
  930. .len = 4,
  931. .veroffs = 4,
  932. .maxblocks = 4,
  933. .pattern = bbt_pattern,
  934. };
  935. static struct nand_bbt_descr bbt_mirror_descr = {
  936. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  937. | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
  938. .offs = 0,
  939. .len = 4,
  940. .veroffs = 4,
  941. .maxblocks = 4,
  942. .pattern = mirror_pattern,
  943. };
  944. /* v1 + irqpending_quirk: i.MX21 */
  945. static const struct mxc_nand_devtype_data imx21_nand_devtype_data = {
  946. .preset = preset_v1,
  947. .send_cmd = send_cmd_v1_v2,
  948. .send_addr = send_addr_v1_v2,
  949. .send_page = send_page_v1,
  950. .send_read_id = send_read_id_v1_v2,
  951. .get_dev_status = get_dev_status_v1_v2,
  952. .check_int = check_int_v1_v2,
  953. .irq_control = irq_control_v1_v2,
  954. .get_ecc_status = get_ecc_status_v1,
  955. .ecclayout_512 = &nandv1_hw_eccoob_smallpage,
  956. .ecclayout_2k = &nandv1_hw_eccoob_largepage,
  957. .ecclayout_4k = &nandv1_hw_eccoob_smallpage, /* XXX: needs fix */
  958. .select_chip = mxc_nand_select_chip_v1_v3,
  959. .correct_data = mxc_nand_correct_data_v1,
  960. .irqpending_quirk = 1,
  961. .needs_ip = 0,
  962. .regs_offset = 0xe00,
  963. .spare0_offset = 0x800,
  964. .spare_len = 16,
  965. .eccbytes = 3,
  966. .eccsize = 1,
  967. };
  968. /* v1 + !irqpending_quirk: i.MX27, i.MX31 */
  969. static const struct mxc_nand_devtype_data imx27_nand_devtype_data = {
  970. .preset = preset_v1,
  971. .send_cmd = send_cmd_v1_v2,
  972. .send_addr = send_addr_v1_v2,
  973. .send_page = send_page_v1,
  974. .send_read_id = send_read_id_v1_v2,
  975. .get_dev_status = get_dev_status_v1_v2,
  976. .check_int = check_int_v1_v2,
  977. .irq_control = irq_control_v1_v2,
  978. .get_ecc_status = get_ecc_status_v1,
  979. .ecclayout_512 = &nandv1_hw_eccoob_smallpage,
  980. .ecclayout_2k = &nandv1_hw_eccoob_largepage,
  981. .ecclayout_4k = &nandv1_hw_eccoob_smallpage, /* XXX: needs fix */
  982. .select_chip = mxc_nand_select_chip_v1_v3,
  983. .correct_data = mxc_nand_correct_data_v1,
  984. .irqpending_quirk = 0,
  985. .needs_ip = 0,
  986. .regs_offset = 0xe00,
  987. .spare0_offset = 0x800,
  988. .axi_offset = 0,
  989. .spare_len = 16,
  990. .eccbytes = 3,
  991. .eccsize = 1,
  992. };
  993. /* v21: i.MX25, i.MX35 */
  994. static const struct mxc_nand_devtype_data imx25_nand_devtype_data = {
  995. .preset = preset_v2,
  996. .send_cmd = send_cmd_v1_v2,
  997. .send_addr = send_addr_v1_v2,
  998. .send_page = send_page_v2,
  999. .send_read_id = send_read_id_v1_v2,
  1000. .get_dev_status = get_dev_status_v1_v2,
  1001. .check_int = check_int_v1_v2,
  1002. .irq_control = irq_control_v1_v2,
  1003. .get_ecc_status = get_ecc_status_v2,
  1004. .ecclayout_512 = &nandv2_hw_eccoob_smallpage,
  1005. .ecclayout_2k = &nandv2_hw_eccoob_largepage,
  1006. .ecclayout_4k = &nandv2_hw_eccoob_4k,
  1007. .select_chip = mxc_nand_select_chip_v2,
  1008. .correct_data = mxc_nand_correct_data_v2_v3,
  1009. .irqpending_quirk = 0,
  1010. .needs_ip = 0,
  1011. .regs_offset = 0x1e00,
  1012. .spare0_offset = 0x1000,
  1013. .axi_offset = 0,
  1014. .spare_len = 64,
  1015. .eccbytes = 9,
  1016. .eccsize = 0,
  1017. };
  1018. /* v3.2a: i.MX51 */
  1019. static const struct mxc_nand_devtype_data imx51_nand_devtype_data = {
  1020. .preset = preset_v3,
  1021. .send_cmd = send_cmd_v3,
  1022. .send_addr = send_addr_v3,
  1023. .send_page = send_page_v3,
  1024. .send_read_id = send_read_id_v3,
  1025. .get_dev_status = get_dev_status_v3,
  1026. .check_int = check_int_v3,
  1027. .irq_control = irq_control_v3,
  1028. .get_ecc_status = get_ecc_status_v3,
  1029. .ecclayout_512 = &nandv2_hw_eccoob_smallpage,
  1030. .ecclayout_2k = &nandv2_hw_eccoob_largepage,
  1031. .ecclayout_4k = &nandv2_hw_eccoob_smallpage, /* XXX: needs fix */
  1032. .select_chip = mxc_nand_select_chip_v1_v3,
  1033. .correct_data = mxc_nand_correct_data_v2_v3,
  1034. .irqpending_quirk = 0,
  1035. .needs_ip = 1,
  1036. .regs_offset = 0,
  1037. .spare0_offset = 0x1000,
  1038. .axi_offset = 0x1e00,
  1039. .spare_len = 64,
  1040. .eccbytes = 0,
  1041. .eccsize = 0,
  1042. .ppb_shift = 7,
  1043. };
  1044. /* v3.2b: i.MX53 */
  1045. static const struct mxc_nand_devtype_data imx53_nand_devtype_data = {
  1046. .preset = preset_v3,
  1047. .send_cmd = send_cmd_v3,
  1048. .send_addr = send_addr_v3,
  1049. .send_page = send_page_v3,
  1050. .send_read_id = send_read_id_v3,
  1051. .get_dev_status = get_dev_status_v3,
  1052. .check_int = check_int_v3,
  1053. .irq_control = irq_control_v3,
  1054. .get_ecc_status = get_ecc_status_v3,
  1055. .ecclayout_512 = &nandv2_hw_eccoob_smallpage,
  1056. .ecclayout_2k = &nandv2_hw_eccoob_largepage,
  1057. .ecclayout_4k = &nandv2_hw_eccoob_smallpage, /* XXX: needs fix */
  1058. .select_chip = mxc_nand_select_chip_v1_v3,
  1059. .correct_data = mxc_nand_correct_data_v2_v3,
  1060. .irqpending_quirk = 0,
  1061. .needs_ip = 1,
  1062. .regs_offset = 0,
  1063. .spare0_offset = 0x1000,
  1064. .axi_offset = 0x1e00,
  1065. .spare_len = 64,
  1066. .eccbytes = 0,
  1067. .eccsize = 0,
  1068. .ppb_shift = 8,
  1069. };
  1070. static inline int is_imx21_nfc(struct mxc_nand_host *host)
  1071. {
  1072. return host->devtype_data == &imx21_nand_devtype_data;
  1073. }
  1074. static inline int is_imx27_nfc(struct mxc_nand_host *host)
  1075. {
  1076. return host->devtype_data == &imx27_nand_devtype_data;
  1077. }
  1078. static inline int is_imx25_nfc(struct mxc_nand_host *host)
  1079. {
  1080. return host->devtype_data == &imx25_nand_devtype_data;
  1081. }
  1082. static inline int is_imx51_nfc(struct mxc_nand_host *host)
  1083. {
  1084. return host->devtype_data == &imx51_nand_devtype_data;
  1085. }
  1086. static inline int is_imx53_nfc(struct mxc_nand_host *host)
  1087. {
  1088. return host->devtype_data == &imx53_nand_devtype_data;
  1089. }
  1090. static struct platform_device_id mxcnd_devtype[] = {
  1091. {
  1092. .name = "imx21-nand",
  1093. .driver_data = (kernel_ulong_t) &imx21_nand_devtype_data,
  1094. }, {
  1095. .name = "imx27-nand",
  1096. .driver_data = (kernel_ulong_t) &imx27_nand_devtype_data,
  1097. }, {
  1098. .name = "imx25-nand",
  1099. .driver_data = (kernel_ulong_t) &imx25_nand_devtype_data,
  1100. }, {
  1101. .name = "imx51-nand",
  1102. .driver_data = (kernel_ulong_t) &imx51_nand_devtype_data,
  1103. }, {
  1104. .name = "imx53-nand",
  1105. .driver_data = (kernel_ulong_t) &imx53_nand_devtype_data,
  1106. }, {
  1107. /* sentinel */
  1108. }
  1109. };
  1110. MODULE_DEVICE_TABLE(platform, mxcnd_devtype);
  1111. #ifdef CONFIG_OF_MTD
  1112. static const struct of_device_id mxcnd_dt_ids[] = {
  1113. {
  1114. .compatible = "fsl,imx21-nand",
  1115. .data = &imx21_nand_devtype_data,
  1116. }, {
  1117. .compatible = "fsl,imx27-nand",
  1118. .data = &imx27_nand_devtype_data,
  1119. }, {
  1120. .compatible = "fsl,imx25-nand",
  1121. .data = &imx25_nand_devtype_data,
  1122. }, {
  1123. .compatible = "fsl,imx51-nand",
  1124. .data = &imx51_nand_devtype_data,
  1125. }, {
  1126. .compatible = "fsl,imx53-nand",
  1127. .data = &imx53_nand_devtype_data,
  1128. },
  1129. { /* sentinel */ }
  1130. };
  1131. static int __init mxcnd_probe_dt(struct mxc_nand_host *host)
  1132. {
  1133. struct device_node *np = host->dev->of_node;
  1134. struct mxc_nand_platform_data *pdata = &host->pdata;
  1135. const struct of_device_id *of_id =
  1136. of_match_device(mxcnd_dt_ids, host->dev);
  1137. int buswidth;
  1138. if (!np)
  1139. return 1;
  1140. if (of_get_nand_ecc_mode(np) >= 0)
  1141. pdata->hw_ecc = 1;
  1142. pdata->flash_bbt = of_get_nand_on_flash_bbt(np);
  1143. buswidth = of_get_nand_bus_width(np);
  1144. if (buswidth < 0)
  1145. return buswidth;
  1146. pdata->width = buswidth / 8;
  1147. host->devtype_data = of_id->data;
  1148. return 0;
  1149. }
  1150. #else
  1151. static int __init mxcnd_probe_dt(struct mxc_nand_host *host)
  1152. {
  1153. return 1;
  1154. }
  1155. #endif
  1156. static int mxcnd_probe(struct platform_device *pdev)
  1157. {
  1158. struct nand_chip *this;
  1159. struct mtd_info *mtd;
  1160. struct mxc_nand_host *host;
  1161. struct resource *res;
  1162. int err = 0;
  1163. /* Allocate memory for MTD device structure and private data */
  1164. host = devm_kzalloc(&pdev->dev, sizeof(struct mxc_nand_host) +
  1165. NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE, GFP_KERNEL);
  1166. if (!host)
  1167. return -ENOMEM;
  1168. host->data_buf = (uint8_t *)(host + 1);
  1169. host->dev = &pdev->dev;
  1170. /* structures must be linked */
  1171. this = &host->nand;
  1172. mtd = &host->mtd;
  1173. mtd->priv = this;
  1174. mtd->owner = THIS_MODULE;
  1175. mtd->dev.parent = &pdev->dev;
  1176. mtd->name = DRIVER_NAME;
  1177. /* 50 us command delay time */
  1178. this->chip_delay = 5;
  1179. this->priv = host;
  1180. this->dev_ready = mxc_nand_dev_ready;
  1181. this->cmdfunc = mxc_nand_command;
  1182. this->read_byte = mxc_nand_read_byte;
  1183. this->read_word = mxc_nand_read_word;
  1184. this->write_buf = mxc_nand_write_buf;
  1185. this->read_buf = mxc_nand_read_buf;
  1186. host->clk = devm_clk_get(&pdev->dev, NULL);
  1187. if (IS_ERR(host->clk))
  1188. return PTR_ERR(host->clk);
  1189. err = mxcnd_probe_dt(host);
  1190. if (err > 0) {
  1191. struct mxc_nand_platform_data *pdata = pdev->dev.platform_data;
  1192. if (pdata) {
  1193. host->pdata = *pdata;
  1194. host->devtype_data = (struct mxc_nand_devtype_data *)
  1195. pdev->id_entry->driver_data;
  1196. } else {
  1197. err = -ENODEV;
  1198. }
  1199. }
  1200. if (err < 0)
  1201. return err;
  1202. if (host->devtype_data->needs_ip) {
  1203. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1204. if (!res)
  1205. return -ENODEV;
  1206. host->regs_ip = devm_ioremap_resource(&pdev->dev, res);
  1207. if (IS_ERR(host->regs_ip))
  1208. return PTR_ERR(host->regs_ip);
  1209. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1210. } else {
  1211. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1212. }
  1213. if (!res)
  1214. return -ENODEV;
  1215. host->base = devm_ioremap_resource(&pdev->dev, res);
  1216. if (IS_ERR(host->base))
  1217. return PTR_ERR(host->base);
  1218. host->main_area0 = host->base;
  1219. if (host->devtype_data->regs_offset)
  1220. host->regs = host->base + host->devtype_data->regs_offset;
  1221. host->spare0 = host->base + host->devtype_data->spare0_offset;
  1222. if (host->devtype_data->axi_offset)
  1223. host->regs_axi = host->base + host->devtype_data->axi_offset;
  1224. this->ecc.bytes = host->devtype_data->eccbytes;
  1225. host->eccsize = host->devtype_data->eccsize;
  1226. this->select_chip = host->devtype_data->select_chip;
  1227. this->ecc.size = 512;
  1228. this->ecc.layout = host->devtype_data->ecclayout_512;
  1229. if (host->pdata.hw_ecc) {
  1230. this->ecc.calculate = mxc_nand_calculate_ecc;
  1231. this->ecc.hwctl = mxc_nand_enable_hwecc;
  1232. this->ecc.correct = host->devtype_data->correct_data;
  1233. this->ecc.mode = NAND_ECC_HW;
  1234. } else {
  1235. this->ecc.mode = NAND_ECC_SOFT;
  1236. }
  1237. /* NAND bus width determines access functions used by upper layer */
  1238. if (host->pdata.width == 2)
  1239. this->options |= NAND_BUSWIDTH_16;
  1240. if (host->pdata.flash_bbt) {
  1241. this->bbt_td = &bbt_main_descr;
  1242. this->bbt_md = &bbt_mirror_descr;
  1243. /* update flash based bbt */
  1244. this->bbt_options |= NAND_BBT_USE_FLASH;
  1245. }
  1246. init_completion(&host->op_completion);
  1247. host->irq = platform_get_irq(pdev, 0);
  1248. /*
  1249. * Use host->devtype_data->irq_control() here instead of irq_control()
  1250. * because we must not disable_irq_nosync without having requested the
  1251. * irq.
  1252. */
  1253. host->devtype_data->irq_control(host, 0);
  1254. err = devm_request_irq(&pdev->dev, host->irq, mxc_nfc_irq,
  1255. IRQF_DISABLED, DRIVER_NAME, host);
  1256. if (err)
  1257. return err;
  1258. clk_prepare_enable(host->clk);
  1259. host->clk_act = 1;
  1260. /*
  1261. * Now that we "own" the interrupt make sure the interrupt mask bit is
  1262. * cleared on i.MX21. Otherwise we can't read the interrupt status bit
  1263. * on this machine.
  1264. */
  1265. if (host->devtype_data->irqpending_quirk) {
  1266. disable_irq_nosync(host->irq);
  1267. host->devtype_data->irq_control(host, 1);
  1268. }
  1269. /* first scan to find the device and get the page size */
  1270. if (nand_scan_ident(mtd, is_imx25_nfc(host) ? 4 : 1, NULL)) {
  1271. err = -ENXIO;
  1272. goto escan;
  1273. }
  1274. /* Call preset again, with correct writesize this time */
  1275. host->devtype_data->preset(mtd);
  1276. if (mtd->writesize == 2048)
  1277. this->ecc.layout = host->devtype_data->ecclayout_2k;
  1278. else if (mtd->writesize == 4096)
  1279. this->ecc.layout = host->devtype_data->ecclayout_4k;
  1280. if (this->ecc.mode == NAND_ECC_HW) {
  1281. if (is_imx21_nfc(host) || is_imx27_nfc(host))
  1282. this->ecc.strength = 1;
  1283. else
  1284. this->ecc.strength = (host->eccsize == 4) ? 4 : 8;
  1285. }
  1286. /* second phase scan */
  1287. if (nand_scan_tail(mtd)) {
  1288. err = -ENXIO;
  1289. goto escan;
  1290. }
  1291. /* Register the partitions */
  1292. mtd_device_parse_register(mtd, part_probes,
  1293. &(struct mtd_part_parser_data){
  1294. .of_node = pdev->dev.of_node,
  1295. },
  1296. host->pdata.parts,
  1297. host->pdata.nr_parts);
  1298. platform_set_drvdata(pdev, host);
  1299. return 0;
  1300. escan:
  1301. if (host->clk_act)
  1302. clk_disable_unprepare(host->clk);
  1303. return err;
  1304. }
  1305. static int mxcnd_remove(struct platform_device *pdev)
  1306. {
  1307. struct mxc_nand_host *host = platform_get_drvdata(pdev);
  1308. platform_set_drvdata(pdev, NULL);
  1309. nand_release(&host->mtd);
  1310. return 0;
  1311. }
  1312. static struct platform_driver mxcnd_driver = {
  1313. .driver = {
  1314. .name = DRIVER_NAME,
  1315. .owner = THIS_MODULE,
  1316. .of_match_table = of_match_ptr(mxcnd_dt_ids),
  1317. },
  1318. .id_table = mxcnd_devtype,
  1319. .probe = mxcnd_probe,
  1320. .remove = mxcnd_remove,
  1321. };
  1322. module_platform_driver(mxcnd_driver);
  1323. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  1324. MODULE_DESCRIPTION("MXC NAND MTD driver");
  1325. MODULE_LICENSE("GPL");