fsmc_nand.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. /*
  2. * drivers/mtd/nand/fsmc_nand.c
  3. *
  4. * ST Microelectronics
  5. * Flexible Static Memory Controller (FSMC)
  6. * Driver for NAND portions
  7. *
  8. * Copyright © 2010 ST Microelectronics
  9. * Vipin Kumar <vipin.kumar@st.com>
  10. * Ashish Priyadarshi
  11. *
  12. * Based on drivers/mtd/nand/nomadik_nand.c
  13. *
  14. * This file is licensed under the terms of the GNU General Public
  15. * License version 2. This program is licensed "as is" without any
  16. * warranty of any kind, whether express or implied.
  17. */
  18. #include <linux/clk.h>
  19. #include <linux/err.h>
  20. #include <linux/init.h>
  21. #include <linux/module.h>
  22. #include <linux/resource.h>
  23. #include <linux/sched.h>
  24. #include <linux/types.h>
  25. #include <linux/mtd/mtd.h>
  26. #include <linux/mtd/nand.h>
  27. #include <linux/mtd/nand_ecc.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/mtd/partitions.h>
  30. #include <linux/io.h>
  31. #include <linux/slab.h>
  32. #include <linux/mtd/fsmc.h>
  33. #include <linux/amba/bus.h>
  34. #include <mtd/mtd-abi.h>
  35. static struct nand_ecclayout fsmc_ecc1_128_layout = {
  36. .eccbytes = 24,
  37. .eccpos = {2, 3, 4, 18, 19, 20, 34, 35, 36, 50, 51, 52,
  38. 66, 67, 68, 82, 83, 84, 98, 99, 100, 114, 115, 116},
  39. .oobfree = {
  40. {.offset = 8, .length = 8},
  41. {.offset = 24, .length = 8},
  42. {.offset = 40, .length = 8},
  43. {.offset = 56, .length = 8},
  44. {.offset = 72, .length = 8},
  45. {.offset = 88, .length = 8},
  46. {.offset = 104, .length = 8},
  47. {.offset = 120, .length = 8}
  48. }
  49. };
  50. static struct nand_ecclayout fsmc_ecc1_64_layout = {
  51. .eccbytes = 12,
  52. .eccpos = {2, 3, 4, 18, 19, 20, 34, 35, 36, 50, 51, 52},
  53. .oobfree = {
  54. {.offset = 8, .length = 8},
  55. {.offset = 24, .length = 8},
  56. {.offset = 40, .length = 8},
  57. {.offset = 56, .length = 8},
  58. }
  59. };
  60. static struct nand_ecclayout fsmc_ecc1_16_layout = {
  61. .eccbytes = 3,
  62. .eccpos = {2, 3, 4},
  63. .oobfree = {
  64. {.offset = 8, .length = 8},
  65. }
  66. };
  67. /*
  68. * ECC4 layout for NAND of pagesize 8192 bytes & OOBsize 256 bytes. 13*16 bytes
  69. * of OB size is reserved for ECC, Byte no. 0 & 1 reserved for bad block and 46
  70. * bytes are free for use.
  71. */
  72. static struct nand_ecclayout fsmc_ecc4_256_layout = {
  73. .eccbytes = 208,
  74. .eccpos = { 2, 3, 4, 5, 6, 7, 8,
  75. 9, 10, 11, 12, 13, 14,
  76. 18, 19, 20, 21, 22, 23, 24,
  77. 25, 26, 27, 28, 29, 30,
  78. 34, 35, 36, 37, 38, 39, 40,
  79. 41, 42, 43, 44, 45, 46,
  80. 50, 51, 52, 53, 54, 55, 56,
  81. 57, 58, 59, 60, 61, 62,
  82. 66, 67, 68, 69, 70, 71, 72,
  83. 73, 74, 75, 76, 77, 78,
  84. 82, 83, 84, 85, 86, 87, 88,
  85. 89, 90, 91, 92, 93, 94,
  86. 98, 99, 100, 101, 102, 103, 104,
  87. 105, 106, 107, 108, 109, 110,
  88. 114, 115, 116, 117, 118, 119, 120,
  89. 121, 122, 123, 124, 125, 126,
  90. 130, 131, 132, 133, 134, 135, 136,
  91. 137, 138, 139, 140, 141, 142,
  92. 146, 147, 148, 149, 150, 151, 152,
  93. 153, 154, 155, 156, 157, 158,
  94. 162, 163, 164, 165, 166, 167, 168,
  95. 169, 170, 171, 172, 173, 174,
  96. 178, 179, 180, 181, 182, 183, 184,
  97. 185, 186, 187, 188, 189, 190,
  98. 194, 195, 196, 197, 198, 199, 200,
  99. 201, 202, 203, 204, 205, 206,
  100. 210, 211, 212, 213, 214, 215, 216,
  101. 217, 218, 219, 220, 221, 222,
  102. 226, 227, 228, 229, 230, 231, 232,
  103. 233, 234, 235, 236, 237, 238,
  104. 242, 243, 244, 245, 246, 247, 248,
  105. 249, 250, 251, 252, 253, 254
  106. },
  107. .oobfree = {
  108. {.offset = 15, .length = 3},
  109. {.offset = 31, .length = 3},
  110. {.offset = 47, .length = 3},
  111. {.offset = 63, .length = 3},
  112. {.offset = 79, .length = 3},
  113. {.offset = 95, .length = 3},
  114. {.offset = 111, .length = 3},
  115. {.offset = 127, .length = 3},
  116. {.offset = 143, .length = 3},
  117. {.offset = 159, .length = 3},
  118. {.offset = 175, .length = 3},
  119. {.offset = 191, .length = 3},
  120. {.offset = 207, .length = 3},
  121. {.offset = 223, .length = 3},
  122. {.offset = 239, .length = 3},
  123. {.offset = 255, .length = 1}
  124. }
  125. };
  126. /*
  127. * ECC4 layout for NAND of pagesize 4096 bytes & OOBsize 224 bytes. 13*8 bytes
  128. * of OOB size is reserved for ECC, Byte no. 0 & 1 reserved for bad block & 118
  129. * bytes are free for use.
  130. */
  131. static struct nand_ecclayout fsmc_ecc4_224_layout = {
  132. .eccbytes = 104,
  133. .eccpos = { 2, 3, 4, 5, 6, 7, 8,
  134. 9, 10, 11, 12, 13, 14,
  135. 18, 19, 20, 21, 22, 23, 24,
  136. 25, 26, 27, 28, 29, 30,
  137. 34, 35, 36, 37, 38, 39, 40,
  138. 41, 42, 43, 44, 45, 46,
  139. 50, 51, 52, 53, 54, 55, 56,
  140. 57, 58, 59, 60, 61, 62,
  141. 66, 67, 68, 69, 70, 71, 72,
  142. 73, 74, 75, 76, 77, 78,
  143. 82, 83, 84, 85, 86, 87, 88,
  144. 89, 90, 91, 92, 93, 94,
  145. 98, 99, 100, 101, 102, 103, 104,
  146. 105, 106, 107, 108, 109, 110,
  147. 114, 115, 116, 117, 118, 119, 120,
  148. 121, 122, 123, 124, 125, 126
  149. },
  150. .oobfree = {
  151. {.offset = 15, .length = 3},
  152. {.offset = 31, .length = 3},
  153. {.offset = 47, .length = 3},
  154. {.offset = 63, .length = 3},
  155. {.offset = 79, .length = 3},
  156. {.offset = 95, .length = 3},
  157. {.offset = 111, .length = 3},
  158. {.offset = 127, .length = 97}
  159. }
  160. };
  161. /*
  162. * ECC4 layout for NAND of pagesize 4096 bytes & OOBsize 128 bytes. 13*8 bytes
  163. * of OOB size is reserved for ECC, Byte no. 0 & 1 reserved for bad block & 22
  164. * bytes are free for use.
  165. */
  166. static struct nand_ecclayout fsmc_ecc4_128_layout = {
  167. .eccbytes = 104,
  168. .eccpos = { 2, 3, 4, 5, 6, 7, 8,
  169. 9, 10, 11, 12, 13, 14,
  170. 18, 19, 20, 21, 22, 23, 24,
  171. 25, 26, 27, 28, 29, 30,
  172. 34, 35, 36, 37, 38, 39, 40,
  173. 41, 42, 43, 44, 45, 46,
  174. 50, 51, 52, 53, 54, 55, 56,
  175. 57, 58, 59, 60, 61, 62,
  176. 66, 67, 68, 69, 70, 71, 72,
  177. 73, 74, 75, 76, 77, 78,
  178. 82, 83, 84, 85, 86, 87, 88,
  179. 89, 90, 91, 92, 93, 94,
  180. 98, 99, 100, 101, 102, 103, 104,
  181. 105, 106, 107, 108, 109, 110,
  182. 114, 115, 116, 117, 118, 119, 120,
  183. 121, 122, 123, 124, 125, 126
  184. },
  185. .oobfree = {
  186. {.offset = 15, .length = 3},
  187. {.offset = 31, .length = 3},
  188. {.offset = 47, .length = 3},
  189. {.offset = 63, .length = 3},
  190. {.offset = 79, .length = 3},
  191. {.offset = 95, .length = 3},
  192. {.offset = 111, .length = 3},
  193. {.offset = 127, .length = 1}
  194. }
  195. };
  196. /*
  197. * ECC4 layout for NAND of pagesize 2048 bytes & OOBsize 64 bytes. 13*4 bytes of
  198. * OOB size is reserved for ECC, Byte no. 0 & 1 reserved for bad block and 10
  199. * bytes are free for use.
  200. */
  201. static struct nand_ecclayout fsmc_ecc4_64_layout = {
  202. .eccbytes = 52,
  203. .eccpos = { 2, 3, 4, 5, 6, 7, 8,
  204. 9, 10, 11, 12, 13, 14,
  205. 18, 19, 20, 21, 22, 23, 24,
  206. 25, 26, 27, 28, 29, 30,
  207. 34, 35, 36, 37, 38, 39, 40,
  208. 41, 42, 43, 44, 45, 46,
  209. 50, 51, 52, 53, 54, 55, 56,
  210. 57, 58, 59, 60, 61, 62,
  211. },
  212. .oobfree = {
  213. {.offset = 15, .length = 3},
  214. {.offset = 31, .length = 3},
  215. {.offset = 47, .length = 3},
  216. {.offset = 63, .length = 1},
  217. }
  218. };
  219. /*
  220. * ECC4 layout for NAND of pagesize 512 bytes & OOBsize 16 bytes. 13 bytes of
  221. * OOB size is reserved for ECC, Byte no. 4 & 5 reserved for bad block and One
  222. * byte is free for use.
  223. */
  224. static struct nand_ecclayout fsmc_ecc4_16_layout = {
  225. .eccbytes = 13,
  226. .eccpos = { 0, 1, 2, 3, 6, 7, 8,
  227. 9, 10, 11, 12, 13, 14
  228. },
  229. .oobfree = {
  230. {.offset = 15, .length = 1},
  231. }
  232. };
  233. /*
  234. * ECC placement definitions in oobfree type format.
  235. * There are 13 bytes of ecc for every 512 byte block and it has to be read
  236. * consecutively and immediately after the 512 byte data block for hardware to
  237. * generate the error bit offsets in 512 byte data.
  238. * Managing the ecc bytes in the following way makes it easier for software to
  239. * read ecc bytes consecutive to data bytes. This way is similar to
  240. * oobfree structure maintained already in generic nand driver
  241. */
  242. static struct fsmc_eccplace fsmc_ecc4_lp_place = {
  243. .eccplace = {
  244. {.offset = 2, .length = 13},
  245. {.offset = 18, .length = 13},
  246. {.offset = 34, .length = 13},
  247. {.offset = 50, .length = 13},
  248. {.offset = 66, .length = 13},
  249. {.offset = 82, .length = 13},
  250. {.offset = 98, .length = 13},
  251. {.offset = 114, .length = 13}
  252. }
  253. };
  254. static struct fsmc_eccplace fsmc_ecc4_sp_place = {
  255. .eccplace = {
  256. {.offset = 0, .length = 4},
  257. {.offset = 6, .length = 9}
  258. }
  259. };
  260. /*
  261. * Default partition tables to be used if the partition information not
  262. * provided through platform data.
  263. *
  264. * Default partition layout for small page(= 512 bytes) devices
  265. * Size for "Root file system" is updated in driver based on actual device size
  266. */
  267. static struct mtd_partition partition_info_16KB_blk[] = {
  268. {
  269. .name = "X-loader",
  270. .offset = 0,
  271. .size = 4*0x4000,
  272. },
  273. {
  274. .name = "U-Boot",
  275. .offset = 0x10000,
  276. .size = 20*0x4000,
  277. },
  278. {
  279. .name = "Kernel",
  280. .offset = 0x60000,
  281. .size = 256*0x4000,
  282. },
  283. {
  284. .name = "Root File System",
  285. .offset = 0x460000,
  286. .size = MTDPART_SIZ_FULL,
  287. },
  288. };
  289. /*
  290. * Default partition layout for large page(> 512 bytes) devices
  291. * Size for "Root file system" is updated in driver based on actual device size
  292. */
  293. static struct mtd_partition partition_info_128KB_blk[] = {
  294. {
  295. .name = "X-loader",
  296. .offset = 0,
  297. .size = 4*0x20000,
  298. },
  299. {
  300. .name = "U-Boot",
  301. .offset = 0x80000,
  302. .size = 12*0x20000,
  303. },
  304. {
  305. .name = "Kernel",
  306. .offset = 0x200000,
  307. .size = 48*0x20000,
  308. },
  309. {
  310. .name = "Root File System",
  311. .offset = 0x800000,
  312. .size = MTDPART_SIZ_FULL,
  313. },
  314. };
  315. /**
  316. * struct fsmc_nand_data - structure for FSMC NAND device state
  317. *
  318. * @pid: Part ID on the AMBA PrimeCell format
  319. * @mtd: MTD info for a NAND flash.
  320. * @nand: Chip related info for a NAND flash.
  321. *
  322. * @ecc_place: ECC placing locations in oobfree type format.
  323. * @bank: Bank number for probed device.
  324. * @clk: Clock structure for FSMC.
  325. *
  326. * @data_va: NAND port for Data.
  327. * @cmd_va: NAND port for Command.
  328. * @addr_va: NAND port for Address.
  329. * @regs_va: FSMC regs base address.
  330. */
  331. struct fsmc_nand_data {
  332. u32 pid;
  333. struct mtd_info mtd;
  334. struct nand_chip nand;
  335. struct fsmc_eccplace *ecc_place;
  336. unsigned int bank;
  337. struct clk *clk;
  338. struct resource *resregs;
  339. struct resource *rescmd;
  340. struct resource *resaddr;
  341. struct resource *resdata;
  342. void __iomem *data_va;
  343. void __iomem *cmd_va;
  344. void __iomem *addr_va;
  345. void __iomem *regs_va;
  346. void (*select_chip)(uint32_t bank, uint32_t busw);
  347. };
  348. /* Assert CS signal based on chipnr */
  349. static void fsmc_select_chip(struct mtd_info *mtd, int chipnr)
  350. {
  351. struct nand_chip *chip = mtd->priv;
  352. struct fsmc_nand_data *host;
  353. host = container_of(mtd, struct fsmc_nand_data, mtd);
  354. switch (chipnr) {
  355. case -1:
  356. chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
  357. break;
  358. case 0:
  359. case 1:
  360. case 2:
  361. case 3:
  362. if (host->select_chip)
  363. host->select_chip(chipnr,
  364. chip->options & NAND_BUSWIDTH_16);
  365. break;
  366. default:
  367. BUG();
  368. }
  369. }
  370. /*
  371. * fsmc_cmd_ctrl - For facilitaing Hardware access
  372. * This routine allows hardware specific access to control-lines(ALE,CLE)
  373. */
  374. static void fsmc_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  375. {
  376. struct nand_chip *this = mtd->priv;
  377. struct fsmc_nand_data *host = container_of(mtd,
  378. struct fsmc_nand_data, mtd);
  379. struct fsmc_regs *regs = host->regs_va;
  380. unsigned int bank = host->bank;
  381. if (ctrl & NAND_CTRL_CHANGE) {
  382. if (ctrl & NAND_CLE) {
  383. this->IO_ADDR_R = (void __iomem *)host->cmd_va;
  384. this->IO_ADDR_W = (void __iomem *)host->cmd_va;
  385. } else if (ctrl & NAND_ALE) {
  386. this->IO_ADDR_R = (void __iomem *)host->addr_va;
  387. this->IO_ADDR_W = (void __iomem *)host->addr_va;
  388. } else {
  389. this->IO_ADDR_R = (void __iomem *)host->data_va;
  390. this->IO_ADDR_W = (void __iomem *)host->data_va;
  391. }
  392. if (ctrl & NAND_NCE) {
  393. writel(readl(&regs->bank_regs[bank].pc) | FSMC_ENABLE,
  394. &regs->bank_regs[bank].pc);
  395. } else {
  396. writel(readl(&regs->bank_regs[bank].pc) & ~FSMC_ENABLE,
  397. &regs->bank_regs[bank].pc);
  398. }
  399. }
  400. mb();
  401. if (cmd != NAND_CMD_NONE)
  402. writeb(cmd, this->IO_ADDR_W);
  403. }
  404. /*
  405. * fsmc_nand_setup - FSMC (Flexible Static Memory Controller) init routine
  406. *
  407. * This routine initializes timing parameters related to NAND memory access in
  408. * FSMC registers
  409. */
  410. static void __init fsmc_nand_setup(struct fsmc_regs *regs, uint32_t bank,
  411. uint32_t busw)
  412. {
  413. uint32_t value = FSMC_DEVTYPE_NAND | FSMC_ENABLE | FSMC_WAITON;
  414. if (busw)
  415. writel(value | FSMC_DEVWID_16, &regs->bank_regs[bank].pc);
  416. else
  417. writel(value | FSMC_DEVWID_8, &regs->bank_regs[bank].pc);
  418. writel(readl(&regs->bank_regs[bank].pc) | FSMC_TCLR_1 | FSMC_TAR_1,
  419. &regs->bank_regs[bank].pc);
  420. writel(FSMC_THIZ_1 | FSMC_THOLD_4 | FSMC_TWAIT_6 | FSMC_TSET_0,
  421. &regs->bank_regs[bank].comm);
  422. writel(FSMC_THIZ_1 | FSMC_THOLD_4 | FSMC_TWAIT_6 | FSMC_TSET_0,
  423. &regs->bank_regs[bank].attrib);
  424. }
  425. /*
  426. * fsmc_enable_hwecc - Enables Hardware ECC through FSMC registers
  427. */
  428. static void fsmc_enable_hwecc(struct mtd_info *mtd, int mode)
  429. {
  430. struct fsmc_nand_data *host = container_of(mtd,
  431. struct fsmc_nand_data, mtd);
  432. struct fsmc_regs *regs = host->regs_va;
  433. uint32_t bank = host->bank;
  434. writel(readl(&regs->bank_regs[bank].pc) & ~FSMC_ECCPLEN_256,
  435. &regs->bank_regs[bank].pc);
  436. writel(readl(&regs->bank_regs[bank].pc) & ~FSMC_ECCEN,
  437. &regs->bank_regs[bank].pc);
  438. writel(readl(&regs->bank_regs[bank].pc) | FSMC_ECCEN,
  439. &regs->bank_regs[bank].pc);
  440. }
  441. /*
  442. * fsmc_read_hwecc_ecc4 - Hardware ECC calculator for ecc4 option supported by
  443. * FSMC. ECC is 13 bytes for 512 bytes of data (supports error correction up to
  444. * max of 8-bits)
  445. */
  446. static int fsmc_read_hwecc_ecc4(struct mtd_info *mtd, const uint8_t *data,
  447. uint8_t *ecc)
  448. {
  449. struct fsmc_nand_data *host = container_of(mtd,
  450. struct fsmc_nand_data, mtd);
  451. struct fsmc_regs *regs = host->regs_va;
  452. uint32_t bank = host->bank;
  453. uint32_t ecc_tmp;
  454. unsigned long deadline = jiffies + FSMC_BUSY_WAIT_TIMEOUT;
  455. do {
  456. if (readl(&regs->bank_regs[bank].sts) & FSMC_CODE_RDY)
  457. break;
  458. else
  459. cond_resched();
  460. } while (!time_after_eq(jiffies, deadline));
  461. ecc_tmp = readl(&regs->bank_regs[bank].ecc1);
  462. ecc[0] = (uint8_t) (ecc_tmp >> 0);
  463. ecc[1] = (uint8_t) (ecc_tmp >> 8);
  464. ecc[2] = (uint8_t) (ecc_tmp >> 16);
  465. ecc[3] = (uint8_t) (ecc_tmp >> 24);
  466. ecc_tmp = readl(&regs->bank_regs[bank].ecc2);
  467. ecc[4] = (uint8_t) (ecc_tmp >> 0);
  468. ecc[5] = (uint8_t) (ecc_tmp >> 8);
  469. ecc[6] = (uint8_t) (ecc_tmp >> 16);
  470. ecc[7] = (uint8_t) (ecc_tmp >> 24);
  471. ecc_tmp = readl(&regs->bank_regs[bank].ecc3);
  472. ecc[8] = (uint8_t) (ecc_tmp >> 0);
  473. ecc[9] = (uint8_t) (ecc_tmp >> 8);
  474. ecc[10] = (uint8_t) (ecc_tmp >> 16);
  475. ecc[11] = (uint8_t) (ecc_tmp >> 24);
  476. ecc_tmp = readl(&regs->bank_regs[bank].sts);
  477. ecc[12] = (uint8_t) (ecc_tmp >> 16);
  478. return 0;
  479. }
  480. /*
  481. * fsmc_read_hwecc_ecc1 - Hardware ECC calculator for ecc1 option supported by
  482. * FSMC. ECC is 3 bytes for 512 bytes of data (supports error correction up to
  483. * max of 1-bit)
  484. */
  485. static int fsmc_read_hwecc_ecc1(struct mtd_info *mtd, const uint8_t *data,
  486. uint8_t *ecc)
  487. {
  488. struct fsmc_nand_data *host = container_of(mtd,
  489. struct fsmc_nand_data, mtd);
  490. struct fsmc_regs *regs = host->regs_va;
  491. uint32_t bank = host->bank;
  492. uint32_t ecc_tmp;
  493. ecc_tmp = readl(&regs->bank_regs[bank].ecc1);
  494. ecc[0] = (uint8_t) (ecc_tmp >> 0);
  495. ecc[1] = (uint8_t) (ecc_tmp >> 8);
  496. ecc[2] = (uint8_t) (ecc_tmp >> 16);
  497. return 0;
  498. }
  499. /* Count the number of 0's in buff upto a max of max_bits */
  500. static int count_written_bits(uint8_t *buff, int size, int max_bits)
  501. {
  502. int k, written_bits = 0;
  503. for (k = 0; k < size; k++) {
  504. written_bits += hweight8(~buff[k]);
  505. if (written_bits > max_bits)
  506. break;
  507. }
  508. return written_bits;
  509. }
  510. /*
  511. * fsmc_read_page_hwecc
  512. * @mtd: mtd info structure
  513. * @chip: nand chip info structure
  514. * @buf: buffer to store read data
  515. * @page: page number to read
  516. *
  517. * This routine is needed for fsmc version 8 as reading from NAND chip has to be
  518. * performed in a strict sequence as follows:
  519. * data(512 byte) -> ecc(13 byte)
  520. * After this read, fsmc hardware generates and reports error data bits(up to a
  521. * max of 8 bits)
  522. */
  523. static int fsmc_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  524. uint8_t *buf, int page)
  525. {
  526. struct fsmc_nand_data *host = container_of(mtd,
  527. struct fsmc_nand_data, mtd);
  528. struct fsmc_eccplace *ecc_place = host->ecc_place;
  529. int i, j, s, stat, eccsize = chip->ecc.size;
  530. int eccbytes = chip->ecc.bytes;
  531. int eccsteps = chip->ecc.steps;
  532. uint8_t *p = buf;
  533. uint8_t *ecc_calc = chip->buffers->ecccalc;
  534. uint8_t *ecc_code = chip->buffers->ecccode;
  535. int off, len, group = 0;
  536. /*
  537. * ecc_oob is intentionally taken as uint16_t. In 16bit devices, we
  538. * end up reading 14 bytes (7 words) from oob. The local array is
  539. * to maintain word alignment
  540. */
  541. uint16_t ecc_oob[7];
  542. uint8_t *oob = (uint8_t *)&ecc_oob[0];
  543. for (i = 0, s = 0; s < eccsteps; s++, i += eccbytes, p += eccsize) {
  544. chip->cmdfunc(mtd, NAND_CMD_READ0, s * eccsize, page);
  545. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  546. chip->read_buf(mtd, p, eccsize);
  547. for (j = 0; j < eccbytes;) {
  548. off = ecc_place->eccplace[group].offset;
  549. len = ecc_place->eccplace[group].length;
  550. group++;
  551. /*
  552. * length is intentionally kept a higher multiple of 2
  553. * to read at least 13 bytes even in case of 16 bit NAND
  554. * devices
  555. */
  556. len = roundup(len, 2);
  557. chip->cmdfunc(mtd, NAND_CMD_READOOB, off, page);
  558. chip->read_buf(mtd, oob + j, len);
  559. j += len;
  560. }
  561. memcpy(&ecc_code[i], oob, chip->ecc.bytes);
  562. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  563. stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  564. if (stat < 0)
  565. mtd->ecc_stats.failed++;
  566. else
  567. mtd->ecc_stats.corrected += stat;
  568. }
  569. return 0;
  570. }
  571. /*
  572. * fsmc_bch8_correct_data
  573. * @mtd: mtd info structure
  574. * @dat: buffer of read data
  575. * @read_ecc: ecc read from device spare area
  576. * @calc_ecc: ecc calculated from read data
  577. *
  578. * calc_ecc is a 104 bit information containing maximum of 8 error
  579. * offset informations of 13 bits each in 512 bytes of read data.
  580. */
  581. static int fsmc_bch8_correct_data(struct mtd_info *mtd, uint8_t *dat,
  582. uint8_t *read_ecc, uint8_t *calc_ecc)
  583. {
  584. struct fsmc_nand_data *host = container_of(mtd,
  585. struct fsmc_nand_data, mtd);
  586. struct nand_chip *chip = mtd->priv;
  587. struct fsmc_regs *regs = host->regs_va;
  588. unsigned int bank = host->bank;
  589. uint32_t err_idx[8];
  590. uint32_t num_err, i;
  591. uint32_t ecc1, ecc2, ecc3, ecc4;
  592. num_err = (readl(&regs->bank_regs[bank].sts) >> 10) & 0xF;
  593. /* no bit flipping */
  594. if (likely(num_err == 0))
  595. return 0;
  596. /* too many errors */
  597. if (unlikely(num_err > 8)) {
  598. /*
  599. * This is a temporary erase check. A newly erased page read
  600. * would result in an ecc error because the oob data is also
  601. * erased to FF and the calculated ecc for an FF data is not
  602. * FF..FF.
  603. * This is a workaround to skip performing correction in case
  604. * data is FF..FF
  605. *
  606. * Logic:
  607. * For every page, each bit written as 0 is counted until these
  608. * number of bits are greater than 8 (the maximum correction
  609. * capability of FSMC for each 512 + 13 bytes)
  610. */
  611. int bits_ecc = count_written_bits(read_ecc, chip->ecc.bytes, 8);
  612. int bits_data = count_written_bits(dat, chip->ecc.size, 8);
  613. if ((bits_ecc + bits_data) <= 8) {
  614. if (bits_data)
  615. memset(dat, 0xff, chip->ecc.size);
  616. return bits_data;
  617. }
  618. return -EBADMSG;
  619. }
  620. /*
  621. * ------------------- calc_ecc[] bit wise -----------|--13 bits--|
  622. * |---idx[7]--|--.....-----|---idx[2]--||---idx[1]--||---idx[0]--|
  623. *
  624. * calc_ecc is a 104 bit information containing maximum of 8 error
  625. * offset informations of 13 bits each. calc_ecc is copied into a
  626. * uint64_t array and error offset indexes are populated in err_idx
  627. * array
  628. */
  629. ecc1 = readl(&regs->bank_regs[bank].ecc1);
  630. ecc2 = readl(&regs->bank_regs[bank].ecc2);
  631. ecc3 = readl(&regs->bank_regs[bank].ecc3);
  632. ecc4 = readl(&regs->bank_regs[bank].sts);
  633. err_idx[0] = (ecc1 >> 0) & 0x1FFF;
  634. err_idx[1] = (ecc1 >> 13) & 0x1FFF;
  635. err_idx[2] = (((ecc2 >> 0) & 0x7F) << 6) | ((ecc1 >> 26) & 0x3F);
  636. err_idx[3] = (ecc2 >> 7) & 0x1FFF;
  637. err_idx[4] = (((ecc3 >> 0) & 0x1) << 12) | ((ecc2 >> 20) & 0xFFF);
  638. err_idx[5] = (ecc3 >> 1) & 0x1FFF;
  639. err_idx[6] = (ecc3 >> 14) & 0x1FFF;
  640. err_idx[7] = (((ecc4 >> 16) & 0xFF) << 5) | ((ecc3 >> 27) & 0x1F);
  641. i = 0;
  642. while (num_err--) {
  643. change_bit(0, (unsigned long *)&err_idx[i]);
  644. change_bit(1, (unsigned long *)&err_idx[i]);
  645. if (err_idx[i] <= chip->ecc.size * 8) {
  646. change_bit(err_idx[i], (unsigned long *)dat);
  647. i++;
  648. }
  649. }
  650. return i;
  651. }
  652. /*
  653. * fsmc_nand_probe - Probe function
  654. * @pdev: platform device structure
  655. */
  656. static int __init fsmc_nand_probe(struct platform_device *pdev)
  657. {
  658. struct fsmc_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
  659. struct fsmc_nand_data *host;
  660. struct mtd_info *mtd;
  661. struct nand_chip *nand;
  662. struct fsmc_regs *regs;
  663. struct resource *res;
  664. int ret = 0;
  665. u32 pid;
  666. int i;
  667. if (!pdata) {
  668. dev_err(&pdev->dev, "platform data is NULL\n");
  669. return -EINVAL;
  670. }
  671. /* Allocate memory for the device structure (and zero it) */
  672. host = kzalloc(sizeof(*host), GFP_KERNEL);
  673. if (!host) {
  674. dev_err(&pdev->dev, "failed to allocate device structure\n");
  675. return -ENOMEM;
  676. }
  677. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_data");
  678. if (!res) {
  679. ret = -EIO;
  680. goto err_probe1;
  681. }
  682. host->resdata = request_mem_region(res->start, resource_size(res),
  683. pdev->name);
  684. if (!host->resdata) {
  685. ret = -EIO;
  686. goto err_probe1;
  687. }
  688. host->data_va = ioremap(res->start, resource_size(res));
  689. if (!host->data_va) {
  690. ret = -EIO;
  691. goto err_probe1;
  692. }
  693. host->resaddr = request_mem_region(res->start + pdata->ale_off,
  694. resource_size(res), pdev->name);
  695. if (!host->resaddr) {
  696. ret = -EIO;
  697. goto err_probe1;
  698. }
  699. host->addr_va = ioremap(res->start + pdata->ale_off,
  700. resource_size(res));
  701. if (!host->addr_va) {
  702. ret = -EIO;
  703. goto err_probe1;
  704. }
  705. host->rescmd = request_mem_region(res->start + pdata->cle_off,
  706. resource_size(res), pdev->name);
  707. if (!host->rescmd) {
  708. ret = -EIO;
  709. goto err_probe1;
  710. }
  711. host->cmd_va = ioremap(res->start + pdata->cle_off, resource_size(res));
  712. if (!host->cmd_va) {
  713. ret = -EIO;
  714. goto err_probe1;
  715. }
  716. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "fsmc_regs");
  717. if (!res) {
  718. ret = -EIO;
  719. goto err_probe1;
  720. }
  721. host->resregs = request_mem_region(res->start, resource_size(res),
  722. pdev->name);
  723. if (!host->resregs) {
  724. ret = -EIO;
  725. goto err_probe1;
  726. }
  727. host->regs_va = ioremap(res->start, resource_size(res));
  728. if (!host->regs_va) {
  729. ret = -EIO;
  730. goto err_probe1;
  731. }
  732. host->clk = clk_get(&pdev->dev, NULL);
  733. if (IS_ERR(host->clk)) {
  734. dev_err(&pdev->dev, "failed to fetch block clock\n");
  735. ret = PTR_ERR(host->clk);
  736. host->clk = NULL;
  737. goto err_probe1;
  738. }
  739. ret = clk_enable(host->clk);
  740. if (ret)
  741. goto err_probe1;
  742. /*
  743. * This device ID is actually a common AMBA ID as used on the
  744. * AMBA PrimeCell bus. However it is not a PrimeCell.
  745. */
  746. for (pid = 0, i = 0; i < 4; i++)
  747. pid |= (readl(host->regs_va + resource_size(res) - 0x20 + 4 * i) & 255) << (i * 8);
  748. host->pid = pid;
  749. dev_info(&pdev->dev, "FSMC device partno %03x, manufacturer %02x, "
  750. "revision %02x, config %02x\n",
  751. AMBA_PART_BITS(pid), AMBA_MANF_BITS(pid),
  752. AMBA_REV_BITS(pid), AMBA_CONFIG_BITS(pid));
  753. host->bank = pdata->bank;
  754. host->select_chip = pdata->select_bank;
  755. regs = host->regs_va;
  756. /* Link all private pointers */
  757. mtd = &host->mtd;
  758. nand = &host->nand;
  759. mtd->priv = nand;
  760. nand->priv = host;
  761. host->mtd.owner = THIS_MODULE;
  762. nand->IO_ADDR_R = host->data_va;
  763. nand->IO_ADDR_W = host->data_va;
  764. nand->cmd_ctrl = fsmc_cmd_ctrl;
  765. nand->chip_delay = 30;
  766. nand->ecc.mode = NAND_ECC_HW;
  767. nand->ecc.hwctl = fsmc_enable_hwecc;
  768. nand->ecc.size = 512;
  769. nand->options = pdata->options;
  770. nand->select_chip = fsmc_select_chip;
  771. if (pdata->width == FSMC_NAND_BW16)
  772. nand->options |= NAND_BUSWIDTH_16;
  773. fsmc_nand_setup(regs, host->bank, nand->options & NAND_BUSWIDTH_16);
  774. if (AMBA_REV_BITS(host->pid) >= 8) {
  775. nand->ecc.read_page = fsmc_read_page_hwecc;
  776. nand->ecc.calculate = fsmc_read_hwecc_ecc4;
  777. nand->ecc.correct = fsmc_bch8_correct_data;
  778. nand->ecc.bytes = 13;
  779. } else {
  780. nand->ecc.calculate = fsmc_read_hwecc_ecc1;
  781. nand->ecc.correct = nand_correct_data;
  782. nand->ecc.bytes = 3;
  783. }
  784. /*
  785. * Scan to find existence of the device
  786. */
  787. if (nand_scan_ident(&host->mtd, 1, NULL)) {
  788. ret = -ENXIO;
  789. dev_err(&pdev->dev, "No NAND Device found!\n");
  790. goto err_probe;
  791. }
  792. if (AMBA_REV_BITS(host->pid) >= 8) {
  793. switch (host->mtd.oobsize) {
  794. case 16:
  795. nand->ecc.layout = &fsmc_ecc4_16_layout;
  796. host->ecc_place = &fsmc_ecc4_sp_place;
  797. break;
  798. case 64:
  799. nand->ecc.layout = &fsmc_ecc4_64_layout;
  800. host->ecc_place = &fsmc_ecc4_lp_place;
  801. break;
  802. case 128:
  803. nand->ecc.layout = &fsmc_ecc4_128_layout;
  804. host->ecc_place = &fsmc_ecc4_lp_place;
  805. break;
  806. case 224:
  807. nand->ecc.layout = &fsmc_ecc4_224_layout;
  808. host->ecc_place = &fsmc_ecc4_lp_place;
  809. break;
  810. case 256:
  811. nand->ecc.layout = &fsmc_ecc4_256_layout;
  812. host->ecc_place = &fsmc_ecc4_lp_place;
  813. break;
  814. default:
  815. printk(KERN_WARNING "No oob scheme defined for "
  816. "oobsize %d\n", mtd->oobsize);
  817. BUG();
  818. }
  819. } else {
  820. switch (host->mtd.oobsize) {
  821. case 16:
  822. nand->ecc.layout = &fsmc_ecc1_16_layout;
  823. break;
  824. case 64:
  825. nand->ecc.layout = &fsmc_ecc1_64_layout;
  826. break;
  827. case 128:
  828. nand->ecc.layout = &fsmc_ecc1_128_layout;
  829. break;
  830. default:
  831. printk(KERN_WARNING "No oob scheme defined for "
  832. "oobsize %d\n", mtd->oobsize);
  833. BUG();
  834. }
  835. }
  836. /* Second stage of scan to fill MTD data-structures */
  837. if (nand_scan_tail(&host->mtd)) {
  838. ret = -ENXIO;
  839. goto err_probe;
  840. }
  841. /*
  842. * The partition information can is accessed by (in the same precedence)
  843. *
  844. * command line through Bootloader,
  845. * platform data,
  846. * default partition information present in driver.
  847. */
  848. /*
  849. * Check for partition info passed
  850. */
  851. host->mtd.name = "nand";
  852. ret = mtd_device_parse_register(&host->mtd, NULL, NULL,
  853. host->mtd.size <= 0x04000000 ?
  854. partition_info_16KB_blk :
  855. partition_info_128KB_blk,
  856. host->mtd.size <= 0x04000000 ?
  857. ARRAY_SIZE(partition_info_16KB_blk) :
  858. ARRAY_SIZE(partition_info_128KB_blk));
  859. if (ret)
  860. goto err_probe;
  861. platform_set_drvdata(pdev, host);
  862. dev_info(&pdev->dev, "FSMC NAND driver registration successful\n");
  863. return 0;
  864. err_probe:
  865. clk_disable(host->clk);
  866. err_probe1:
  867. if (host->clk)
  868. clk_put(host->clk);
  869. if (host->regs_va)
  870. iounmap(host->regs_va);
  871. if (host->resregs)
  872. release_mem_region(host->resregs->start,
  873. resource_size(host->resregs));
  874. if (host->cmd_va)
  875. iounmap(host->cmd_va);
  876. if (host->rescmd)
  877. release_mem_region(host->rescmd->start,
  878. resource_size(host->rescmd));
  879. if (host->addr_va)
  880. iounmap(host->addr_va);
  881. if (host->resaddr)
  882. release_mem_region(host->resaddr->start,
  883. resource_size(host->resaddr));
  884. if (host->data_va)
  885. iounmap(host->data_va);
  886. if (host->resdata)
  887. release_mem_region(host->resdata->start,
  888. resource_size(host->resdata));
  889. kfree(host);
  890. return ret;
  891. }
  892. /*
  893. * Clean up routine
  894. */
  895. static int fsmc_nand_remove(struct platform_device *pdev)
  896. {
  897. struct fsmc_nand_data *host = platform_get_drvdata(pdev);
  898. platform_set_drvdata(pdev, NULL);
  899. if (host) {
  900. nand_release(&host->mtd);
  901. clk_disable(host->clk);
  902. clk_put(host->clk);
  903. iounmap(host->regs_va);
  904. release_mem_region(host->resregs->start,
  905. resource_size(host->resregs));
  906. iounmap(host->cmd_va);
  907. release_mem_region(host->rescmd->start,
  908. resource_size(host->rescmd));
  909. iounmap(host->addr_va);
  910. release_mem_region(host->resaddr->start,
  911. resource_size(host->resaddr));
  912. iounmap(host->data_va);
  913. release_mem_region(host->resdata->start,
  914. resource_size(host->resdata));
  915. kfree(host);
  916. }
  917. return 0;
  918. }
  919. #ifdef CONFIG_PM
  920. static int fsmc_nand_suspend(struct device *dev)
  921. {
  922. struct fsmc_nand_data *host = dev_get_drvdata(dev);
  923. if (host)
  924. clk_disable(host->clk);
  925. return 0;
  926. }
  927. static int fsmc_nand_resume(struct device *dev)
  928. {
  929. struct fsmc_nand_data *host = dev_get_drvdata(dev);
  930. if (host)
  931. clk_enable(host->clk);
  932. return 0;
  933. }
  934. static const struct dev_pm_ops fsmc_nand_pm_ops = {
  935. .suspend = fsmc_nand_suspend,
  936. .resume = fsmc_nand_resume,
  937. };
  938. #endif
  939. static struct platform_driver fsmc_nand_driver = {
  940. .remove = fsmc_nand_remove,
  941. .driver = {
  942. .owner = THIS_MODULE,
  943. .name = "fsmc-nand",
  944. #ifdef CONFIG_PM
  945. .pm = &fsmc_nand_pm_ops,
  946. #endif
  947. },
  948. };
  949. static int __init fsmc_nand_init(void)
  950. {
  951. return platform_driver_probe(&fsmc_nand_driver,
  952. fsmc_nand_probe);
  953. }
  954. module_init(fsmc_nand_init);
  955. static void __exit fsmc_nand_exit(void)
  956. {
  957. platform_driver_unregister(&fsmc_nand_driver);
  958. }
  959. module_exit(fsmc_nand_exit);
  960. MODULE_LICENSE("GPL");
  961. MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>, Ashish Priyadarshi");
  962. MODULE_DESCRIPTION("NAND driver for SPEAr Platforms");