fsmc_nand.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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. * struct fsmc_nand_data - structure for FSMC NAND device state
  262. *
  263. * @pid: Part ID on the AMBA PrimeCell format
  264. * @mtd: MTD info for a NAND flash.
  265. * @nand: Chip related info for a NAND flash.
  266. * @partitions: Partition info for a NAND Flash.
  267. * @nr_partitions: Total number of partition of a NAND flash.
  268. *
  269. * @ecc_place: ECC placing locations in oobfree type format.
  270. * @bank: Bank number for probed device.
  271. * @clk: Clock structure for FSMC.
  272. *
  273. * @data_va: NAND port for Data.
  274. * @cmd_va: NAND port for Command.
  275. * @addr_va: NAND port for Address.
  276. * @regs_va: FSMC regs base address.
  277. */
  278. struct fsmc_nand_data {
  279. u32 pid;
  280. struct mtd_info mtd;
  281. struct nand_chip nand;
  282. struct mtd_partition *partitions;
  283. unsigned int nr_partitions;
  284. struct fsmc_eccplace *ecc_place;
  285. unsigned int bank;
  286. struct device *dev;
  287. struct clk *clk;
  288. struct fsmc_nand_timings *dev_timings;
  289. void __iomem *data_va;
  290. void __iomem *cmd_va;
  291. void __iomem *addr_va;
  292. void __iomem *regs_va;
  293. void (*select_chip)(uint32_t bank, uint32_t busw);
  294. };
  295. /* Assert CS signal based on chipnr */
  296. static void fsmc_select_chip(struct mtd_info *mtd, int chipnr)
  297. {
  298. struct nand_chip *chip = mtd->priv;
  299. struct fsmc_nand_data *host;
  300. host = container_of(mtd, struct fsmc_nand_data, mtd);
  301. switch (chipnr) {
  302. case -1:
  303. chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
  304. break;
  305. case 0:
  306. case 1:
  307. case 2:
  308. case 3:
  309. if (host->select_chip)
  310. host->select_chip(chipnr,
  311. chip->options & NAND_BUSWIDTH_16);
  312. break;
  313. default:
  314. BUG();
  315. }
  316. }
  317. /*
  318. * fsmc_cmd_ctrl - For facilitaing Hardware access
  319. * This routine allows hardware specific access to control-lines(ALE,CLE)
  320. */
  321. static void fsmc_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  322. {
  323. struct nand_chip *this = mtd->priv;
  324. struct fsmc_nand_data *host = container_of(mtd,
  325. struct fsmc_nand_data, mtd);
  326. struct fsmc_regs *regs = host->regs_va;
  327. unsigned int bank = host->bank;
  328. if (ctrl & NAND_CTRL_CHANGE) {
  329. if (ctrl & NAND_CLE) {
  330. this->IO_ADDR_R = (void __iomem *)host->cmd_va;
  331. this->IO_ADDR_W = (void __iomem *)host->cmd_va;
  332. } else if (ctrl & NAND_ALE) {
  333. this->IO_ADDR_R = (void __iomem *)host->addr_va;
  334. this->IO_ADDR_W = (void __iomem *)host->addr_va;
  335. } else {
  336. this->IO_ADDR_R = (void __iomem *)host->data_va;
  337. this->IO_ADDR_W = (void __iomem *)host->data_va;
  338. }
  339. if (ctrl & NAND_NCE) {
  340. writel(readl(&regs->bank_regs[bank].pc) | FSMC_ENABLE,
  341. &regs->bank_regs[bank].pc);
  342. } else {
  343. writel(readl(&regs->bank_regs[bank].pc) & ~FSMC_ENABLE,
  344. &regs->bank_regs[bank].pc);
  345. }
  346. }
  347. mb();
  348. if (cmd != NAND_CMD_NONE)
  349. writeb(cmd, this->IO_ADDR_W);
  350. }
  351. /*
  352. * fsmc_nand_setup - FSMC (Flexible Static Memory Controller) init routine
  353. *
  354. * This routine initializes timing parameters related to NAND memory access in
  355. * FSMC registers
  356. */
  357. static void fsmc_nand_setup(struct fsmc_regs *regs, uint32_t bank,
  358. uint32_t busw, struct fsmc_nand_timings *timings)
  359. {
  360. uint32_t value = FSMC_DEVTYPE_NAND | FSMC_ENABLE | FSMC_WAITON;
  361. uint32_t tclr, tar, thiz, thold, twait, tset;
  362. struct fsmc_nand_timings *tims;
  363. struct fsmc_nand_timings default_timings = {
  364. .tclr = FSMC_TCLR_1,
  365. .tar = FSMC_TAR_1,
  366. .thiz = FSMC_THIZ_1,
  367. .thold = FSMC_THOLD_4,
  368. .twait = FSMC_TWAIT_6,
  369. .tset = FSMC_TSET_0,
  370. };
  371. if (timings)
  372. tims = timings;
  373. else
  374. tims = &default_timings;
  375. tclr = (tims->tclr & FSMC_TCLR_MASK) << FSMC_TCLR_SHIFT;
  376. tar = (tims->tar & FSMC_TAR_MASK) << FSMC_TAR_SHIFT;
  377. thiz = (tims->thiz & FSMC_THIZ_MASK) << FSMC_THIZ_SHIFT;
  378. thold = (tims->thold & FSMC_THOLD_MASK) << FSMC_THOLD_SHIFT;
  379. twait = (tims->twait & FSMC_TWAIT_MASK) << FSMC_TWAIT_SHIFT;
  380. tset = (tims->tset & FSMC_TSET_MASK) << FSMC_TSET_SHIFT;
  381. if (busw)
  382. writel(value | FSMC_DEVWID_16, &regs->bank_regs[bank].pc);
  383. else
  384. writel(value | FSMC_DEVWID_8, &regs->bank_regs[bank].pc);
  385. writel(readl(&regs->bank_regs[bank].pc) | tclr | tar,
  386. &regs->bank_regs[bank].pc);
  387. writel(thiz | thold | twait | tset, &regs->bank_regs[bank].comm);
  388. writel(thiz | thold | twait | tset, &regs->bank_regs[bank].attrib);
  389. }
  390. /*
  391. * fsmc_enable_hwecc - Enables Hardware ECC through FSMC registers
  392. */
  393. static void fsmc_enable_hwecc(struct mtd_info *mtd, int mode)
  394. {
  395. struct fsmc_nand_data *host = container_of(mtd,
  396. struct fsmc_nand_data, mtd);
  397. struct fsmc_regs *regs = host->regs_va;
  398. uint32_t bank = host->bank;
  399. writel(readl(&regs->bank_regs[bank].pc) & ~FSMC_ECCPLEN_256,
  400. &regs->bank_regs[bank].pc);
  401. writel(readl(&regs->bank_regs[bank].pc) & ~FSMC_ECCEN,
  402. &regs->bank_regs[bank].pc);
  403. writel(readl(&regs->bank_regs[bank].pc) | FSMC_ECCEN,
  404. &regs->bank_regs[bank].pc);
  405. }
  406. /*
  407. * fsmc_read_hwecc_ecc4 - Hardware ECC calculator for ecc4 option supported by
  408. * FSMC. ECC is 13 bytes for 512 bytes of data (supports error correction up to
  409. * max of 8-bits)
  410. */
  411. static int fsmc_read_hwecc_ecc4(struct mtd_info *mtd, const uint8_t *data,
  412. uint8_t *ecc)
  413. {
  414. struct fsmc_nand_data *host = container_of(mtd,
  415. struct fsmc_nand_data, mtd);
  416. struct fsmc_regs *regs = host->regs_va;
  417. uint32_t bank = host->bank;
  418. uint32_t ecc_tmp;
  419. unsigned long deadline = jiffies + FSMC_BUSY_WAIT_TIMEOUT;
  420. do {
  421. if (readl(&regs->bank_regs[bank].sts) & FSMC_CODE_RDY)
  422. break;
  423. else
  424. cond_resched();
  425. } while (!time_after_eq(jiffies, deadline));
  426. if (time_after_eq(jiffies, deadline)) {
  427. dev_err(host->dev, "calculate ecc timed out\n");
  428. return -ETIMEDOUT;
  429. }
  430. ecc_tmp = readl(&regs->bank_regs[bank].ecc1);
  431. ecc[0] = (uint8_t) (ecc_tmp >> 0);
  432. ecc[1] = (uint8_t) (ecc_tmp >> 8);
  433. ecc[2] = (uint8_t) (ecc_tmp >> 16);
  434. ecc[3] = (uint8_t) (ecc_tmp >> 24);
  435. ecc_tmp = readl(&regs->bank_regs[bank].ecc2);
  436. ecc[4] = (uint8_t) (ecc_tmp >> 0);
  437. ecc[5] = (uint8_t) (ecc_tmp >> 8);
  438. ecc[6] = (uint8_t) (ecc_tmp >> 16);
  439. ecc[7] = (uint8_t) (ecc_tmp >> 24);
  440. ecc_tmp = readl(&regs->bank_regs[bank].ecc3);
  441. ecc[8] = (uint8_t) (ecc_tmp >> 0);
  442. ecc[9] = (uint8_t) (ecc_tmp >> 8);
  443. ecc[10] = (uint8_t) (ecc_tmp >> 16);
  444. ecc[11] = (uint8_t) (ecc_tmp >> 24);
  445. ecc_tmp = readl(&regs->bank_regs[bank].sts);
  446. ecc[12] = (uint8_t) (ecc_tmp >> 16);
  447. return 0;
  448. }
  449. /*
  450. * fsmc_read_hwecc_ecc1 - Hardware ECC calculator for ecc1 option supported by
  451. * FSMC. ECC is 3 bytes for 512 bytes of data (supports error correction up to
  452. * max of 1-bit)
  453. */
  454. static int fsmc_read_hwecc_ecc1(struct mtd_info *mtd, const uint8_t *data,
  455. uint8_t *ecc)
  456. {
  457. struct fsmc_nand_data *host = container_of(mtd,
  458. struct fsmc_nand_data, mtd);
  459. struct fsmc_regs *regs = host->regs_va;
  460. uint32_t bank = host->bank;
  461. uint32_t ecc_tmp;
  462. ecc_tmp = readl(&regs->bank_regs[bank].ecc1);
  463. ecc[0] = (uint8_t) (ecc_tmp >> 0);
  464. ecc[1] = (uint8_t) (ecc_tmp >> 8);
  465. ecc[2] = (uint8_t) (ecc_tmp >> 16);
  466. return 0;
  467. }
  468. /* Count the number of 0's in buff upto a max of max_bits */
  469. static int count_written_bits(uint8_t *buff, int size, int max_bits)
  470. {
  471. int k, written_bits = 0;
  472. for (k = 0; k < size; k++) {
  473. written_bits += hweight8(~buff[k]);
  474. if (written_bits > max_bits)
  475. break;
  476. }
  477. return written_bits;
  478. }
  479. /*
  480. * fsmc_read_page_hwecc
  481. * @mtd: mtd info structure
  482. * @chip: nand chip info structure
  483. * @buf: buffer to store read data
  484. * @page: page number to read
  485. *
  486. * This routine is needed for fsmc version 8 as reading from NAND chip has to be
  487. * performed in a strict sequence as follows:
  488. * data(512 byte) -> ecc(13 byte)
  489. * After this read, fsmc hardware generates and reports error data bits(up to a
  490. * max of 8 bits)
  491. */
  492. static int fsmc_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  493. uint8_t *buf, int page)
  494. {
  495. struct fsmc_nand_data *host = container_of(mtd,
  496. struct fsmc_nand_data, mtd);
  497. struct fsmc_eccplace *ecc_place = host->ecc_place;
  498. int i, j, s, stat, eccsize = chip->ecc.size;
  499. int eccbytes = chip->ecc.bytes;
  500. int eccsteps = chip->ecc.steps;
  501. uint8_t *p = buf;
  502. uint8_t *ecc_calc = chip->buffers->ecccalc;
  503. uint8_t *ecc_code = chip->buffers->ecccode;
  504. int off, len, group = 0;
  505. /*
  506. * ecc_oob is intentionally taken as uint16_t. In 16bit devices, we
  507. * end up reading 14 bytes (7 words) from oob. The local array is
  508. * to maintain word alignment
  509. */
  510. uint16_t ecc_oob[7];
  511. uint8_t *oob = (uint8_t *)&ecc_oob[0];
  512. for (i = 0, s = 0; s < eccsteps; s++, i += eccbytes, p += eccsize) {
  513. chip->cmdfunc(mtd, NAND_CMD_READ0, s * eccsize, page);
  514. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  515. chip->read_buf(mtd, p, eccsize);
  516. for (j = 0; j < eccbytes;) {
  517. off = ecc_place->eccplace[group].offset;
  518. len = ecc_place->eccplace[group].length;
  519. group++;
  520. /*
  521. * length is intentionally kept a higher multiple of 2
  522. * to read at least 13 bytes even in case of 16 bit NAND
  523. * devices
  524. */
  525. if (chip->options & NAND_BUSWIDTH_16)
  526. len = roundup(len, 2);
  527. chip->cmdfunc(mtd, NAND_CMD_READOOB, off, page);
  528. chip->read_buf(mtd, oob + j, len);
  529. j += len;
  530. }
  531. memcpy(&ecc_code[i], oob, chip->ecc.bytes);
  532. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  533. stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  534. if (stat < 0)
  535. mtd->ecc_stats.failed++;
  536. else
  537. mtd->ecc_stats.corrected += stat;
  538. }
  539. return 0;
  540. }
  541. /*
  542. * fsmc_bch8_correct_data
  543. * @mtd: mtd info structure
  544. * @dat: buffer of read data
  545. * @read_ecc: ecc read from device spare area
  546. * @calc_ecc: ecc calculated from read data
  547. *
  548. * calc_ecc is a 104 bit information containing maximum of 8 error
  549. * offset informations of 13 bits each in 512 bytes of read data.
  550. */
  551. static int fsmc_bch8_correct_data(struct mtd_info *mtd, uint8_t *dat,
  552. uint8_t *read_ecc, uint8_t *calc_ecc)
  553. {
  554. struct fsmc_nand_data *host = container_of(mtd,
  555. struct fsmc_nand_data, mtd);
  556. struct nand_chip *chip = mtd->priv;
  557. struct fsmc_regs *regs = host->regs_va;
  558. unsigned int bank = host->bank;
  559. uint32_t err_idx[8];
  560. uint32_t num_err, i;
  561. uint32_t ecc1, ecc2, ecc3, ecc4;
  562. num_err = (readl(&regs->bank_regs[bank].sts) >> 10) & 0xF;
  563. /* no bit flipping */
  564. if (likely(num_err == 0))
  565. return 0;
  566. /* too many errors */
  567. if (unlikely(num_err > 8)) {
  568. /*
  569. * This is a temporary erase check. A newly erased page read
  570. * would result in an ecc error because the oob data is also
  571. * erased to FF and the calculated ecc for an FF data is not
  572. * FF..FF.
  573. * This is a workaround to skip performing correction in case
  574. * data is FF..FF
  575. *
  576. * Logic:
  577. * For every page, each bit written as 0 is counted until these
  578. * number of bits are greater than 8 (the maximum correction
  579. * capability of FSMC for each 512 + 13 bytes)
  580. */
  581. int bits_ecc = count_written_bits(read_ecc, chip->ecc.bytes, 8);
  582. int bits_data = count_written_bits(dat, chip->ecc.size, 8);
  583. if ((bits_ecc + bits_data) <= 8) {
  584. if (bits_data)
  585. memset(dat, 0xff, chip->ecc.size);
  586. return bits_data;
  587. }
  588. return -EBADMSG;
  589. }
  590. /*
  591. * ------------------- calc_ecc[] bit wise -----------|--13 bits--|
  592. * |---idx[7]--|--.....-----|---idx[2]--||---idx[1]--||---idx[0]--|
  593. *
  594. * calc_ecc is a 104 bit information containing maximum of 8 error
  595. * offset informations of 13 bits each. calc_ecc is copied into a
  596. * uint64_t array and error offset indexes are populated in err_idx
  597. * array
  598. */
  599. ecc1 = readl(&regs->bank_regs[bank].ecc1);
  600. ecc2 = readl(&regs->bank_regs[bank].ecc2);
  601. ecc3 = readl(&regs->bank_regs[bank].ecc3);
  602. ecc4 = readl(&regs->bank_regs[bank].sts);
  603. err_idx[0] = (ecc1 >> 0) & 0x1FFF;
  604. err_idx[1] = (ecc1 >> 13) & 0x1FFF;
  605. err_idx[2] = (((ecc2 >> 0) & 0x7F) << 6) | ((ecc1 >> 26) & 0x3F);
  606. err_idx[3] = (ecc2 >> 7) & 0x1FFF;
  607. err_idx[4] = (((ecc3 >> 0) & 0x1) << 12) | ((ecc2 >> 20) & 0xFFF);
  608. err_idx[5] = (ecc3 >> 1) & 0x1FFF;
  609. err_idx[6] = (ecc3 >> 14) & 0x1FFF;
  610. err_idx[7] = (((ecc4 >> 16) & 0xFF) << 5) | ((ecc3 >> 27) & 0x1F);
  611. i = 0;
  612. while (num_err--) {
  613. change_bit(0, (unsigned long *)&err_idx[i]);
  614. change_bit(1, (unsigned long *)&err_idx[i]);
  615. if (err_idx[i] < chip->ecc.size * 8) {
  616. change_bit(err_idx[i], (unsigned long *)dat);
  617. i++;
  618. }
  619. }
  620. return i;
  621. }
  622. /*
  623. * fsmc_nand_probe - Probe function
  624. * @pdev: platform device structure
  625. */
  626. static int __init fsmc_nand_probe(struct platform_device *pdev)
  627. {
  628. struct fsmc_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
  629. struct fsmc_nand_data *host;
  630. struct mtd_info *mtd;
  631. struct nand_chip *nand;
  632. struct fsmc_regs *regs;
  633. struct resource *res;
  634. int ret = 0;
  635. u32 pid;
  636. int i;
  637. if (!pdata) {
  638. dev_err(&pdev->dev, "platform data is NULL\n");
  639. return -EINVAL;
  640. }
  641. /* Allocate memory for the device structure (and zero it) */
  642. host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
  643. if (!host) {
  644. dev_err(&pdev->dev, "failed to allocate device structure\n");
  645. return -ENOMEM;
  646. }
  647. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_data");
  648. if (!res)
  649. return -EINVAL;
  650. if (!devm_request_mem_region(&pdev->dev, res->start, resource_size(res),
  651. pdev->name)) {
  652. dev_err(&pdev->dev, "Failed to get memory data resourse\n");
  653. return -ENOENT;
  654. }
  655. host->data_va = devm_ioremap(&pdev->dev, res->start,
  656. resource_size(res));
  657. if (!host->data_va) {
  658. dev_err(&pdev->dev, "data ioremap failed\n");
  659. return -ENOMEM;
  660. }
  661. if (!devm_request_mem_region(&pdev->dev, res->start + pdata->ale_off,
  662. resource_size(res), pdev->name)) {
  663. dev_err(&pdev->dev, "Failed to get memory ale resourse\n");
  664. return -ENOENT;
  665. }
  666. host->addr_va = devm_ioremap(&pdev->dev, res->start + pdata->ale_off,
  667. resource_size(res));
  668. if (!host->addr_va) {
  669. dev_err(&pdev->dev, "ale ioremap failed\n");
  670. return -ENOMEM;
  671. }
  672. if (!devm_request_mem_region(&pdev->dev, res->start + pdata->cle_off,
  673. resource_size(res), pdev->name)) {
  674. dev_err(&pdev->dev, "Failed to get memory cle resourse\n");
  675. return -ENOENT;
  676. }
  677. host->cmd_va = devm_ioremap(&pdev->dev, res->start + pdata->cle_off,
  678. resource_size(res));
  679. if (!host->cmd_va) {
  680. dev_err(&pdev->dev, "ale ioremap failed\n");
  681. return -ENOMEM;
  682. }
  683. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "fsmc_regs");
  684. if (!res)
  685. return -EINVAL;
  686. if (!devm_request_mem_region(&pdev->dev, res->start, resource_size(res),
  687. pdev->name)) {
  688. dev_err(&pdev->dev, "Failed to get memory regs resourse\n");
  689. return -ENOENT;
  690. }
  691. host->regs_va = devm_ioremap(&pdev->dev, res->start,
  692. resource_size(res));
  693. if (!host->regs_va) {
  694. dev_err(&pdev->dev, "regs ioremap failed\n");
  695. return -ENOMEM;
  696. }
  697. host->clk = clk_get(&pdev->dev, NULL);
  698. if (IS_ERR(host->clk)) {
  699. dev_err(&pdev->dev, "failed to fetch block clock\n");
  700. return PTR_ERR(host->clk);
  701. }
  702. ret = clk_enable(host->clk);
  703. if (ret)
  704. goto err_clk_enable;
  705. /*
  706. * This device ID is actually a common AMBA ID as used on the
  707. * AMBA PrimeCell bus. However it is not a PrimeCell.
  708. */
  709. for (pid = 0, i = 0; i < 4; i++)
  710. pid |= (readl(host->regs_va + resource_size(res) - 0x20 + 4 * i) & 255) << (i * 8);
  711. host->pid = pid;
  712. dev_info(&pdev->dev, "FSMC device partno %03x, manufacturer %02x, "
  713. "revision %02x, config %02x\n",
  714. AMBA_PART_BITS(pid), AMBA_MANF_BITS(pid),
  715. AMBA_REV_BITS(pid), AMBA_CONFIG_BITS(pid));
  716. host->bank = pdata->bank;
  717. host->select_chip = pdata->select_bank;
  718. host->partitions = pdata->partitions;
  719. host->nr_partitions = pdata->nr_partitions;
  720. host->dev = &pdev->dev;
  721. host->dev_timings = pdata->nand_timings;
  722. regs = host->regs_va;
  723. /* Link all private pointers */
  724. mtd = &host->mtd;
  725. nand = &host->nand;
  726. mtd->priv = nand;
  727. nand->priv = host;
  728. host->mtd.owner = THIS_MODULE;
  729. nand->IO_ADDR_R = host->data_va;
  730. nand->IO_ADDR_W = host->data_va;
  731. nand->cmd_ctrl = fsmc_cmd_ctrl;
  732. nand->chip_delay = 30;
  733. nand->ecc.mode = NAND_ECC_HW;
  734. nand->ecc.hwctl = fsmc_enable_hwecc;
  735. nand->ecc.size = 512;
  736. nand->options = pdata->options;
  737. nand->select_chip = fsmc_select_chip;
  738. nand->badblockbits = 7;
  739. if (pdata->width == FSMC_NAND_BW16)
  740. nand->options |= NAND_BUSWIDTH_16;
  741. fsmc_nand_setup(regs, host->bank, nand->options & NAND_BUSWIDTH_16,
  742. host->dev_timings);
  743. if (AMBA_REV_BITS(host->pid) >= 8) {
  744. nand->ecc.read_page = fsmc_read_page_hwecc;
  745. nand->ecc.calculate = fsmc_read_hwecc_ecc4;
  746. nand->ecc.correct = fsmc_bch8_correct_data;
  747. nand->ecc.bytes = 13;
  748. nand->ecc.strength = 8;
  749. } else {
  750. nand->ecc.calculate = fsmc_read_hwecc_ecc1;
  751. nand->ecc.correct = nand_correct_data;
  752. nand->ecc.bytes = 3;
  753. nand->ecc.strength = 1;
  754. }
  755. /*
  756. * Scan to find existence of the device
  757. */
  758. if (nand_scan_ident(&host->mtd, 1, NULL)) {
  759. ret = -ENXIO;
  760. dev_err(&pdev->dev, "No NAND Device found!\n");
  761. goto err_scan_ident;
  762. }
  763. if (AMBA_REV_BITS(host->pid) >= 8) {
  764. switch (host->mtd.oobsize) {
  765. case 16:
  766. nand->ecc.layout = &fsmc_ecc4_16_layout;
  767. host->ecc_place = &fsmc_ecc4_sp_place;
  768. break;
  769. case 64:
  770. nand->ecc.layout = &fsmc_ecc4_64_layout;
  771. host->ecc_place = &fsmc_ecc4_lp_place;
  772. break;
  773. case 128:
  774. nand->ecc.layout = &fsmc_ecc4_128_layout;
  775. host->ecc_place = &fsmc_ecc4_lp_place;
  776. break;
  777. case 224:
  778. nand->ecc.layout = &fsmc_ecc4_224_layout;
  779. host->ecc_place = &fsmc_ecc4_lp_place;
  780. break;
  781. case 256:
  782. nand->ecc.layout = &fsmc_ecc4_256_layout;
  783. host->ecc_place = &fsmc_ecc4_lp_place;
  784. break;
  785. default:
  786. printk(KERN_WARNING "No oob scheme defined for "
  787. "oobsize %d\n", mtd->oobsize);
  788. BUG();
  789. }
  790. } else {
  791. switch (host->mtd.oobsize) {
  792. case 16:
  793. nand->ecc.layout = &fsmc_ecc1_16_layout;
  794. break;
  795. case 64:
  796. nand->ecc.layout = &fsmc_ecc1_64_layout;
  797. break;
  798. case 128:
  799. nand->ecc.layout = &fsmc_ecc1_128_layout;
  800. break;
  801. default:
  802. printk(KERN_WARNING "No oob scheme defined for "
  803. "oobsize %d\n", mtd->oobsize);
  804. BUG();
  805. }
  806. }
  807. /* Second stage of scan to fill MTD data-structures */
  808. if (nand_scan_tail(&host->mtd)) {
  809. ret = -ENXIO;
  810. goto err_probe;
  811. }
  812. /*
  813. * The partition information can is accessed by (in the same precedence)
  814. *
  815. * command line through Bootloader,
  816. * platform data,
  817. * default partition information present in driver.
  818. */
  819. /*
  820. * Check for partition info passed
  821. */
  822. host->mtd.name = "nand";
  823. ret = mtd_device_parse_register(&host->mtd, NULL, NULL,
  824. host->partitions, host->nr_partitions);
  825. if (ret)
  826. goto err_probe;
  827. platform_set_drvdata(pdev, host);
  828. dev_info(&pdev->dev, "FSMC NAND driver registration successful\n");
  829. return 0;
  830. err_probe:
  831. err_scan_ident:
  832. clk_disable(host->clk);
  833. err_clk_enable:
  834. clk_put(host->clk);
  835. return ret;
  836. }
  837. /*
  838. * Clean up routine
  839. */
  840. static int fsmc_nand_remove(struct platform_device *pdev)
  841. {
  842. struct fsmc_nand_data *host = platform_get_drvdata(pdev);
  843. platform_set_drvdata(pdev, NULL);
  844. if (host) {
  845. nand_release(&host->mtd);
  846. clk_disable(host->clk);
  847. clk_put(host->clk);
  848. }
  849. return 0;
  850. }
  851. #ifdef CONFIG_PM
  852. static int fsmc_nand_suspend(struct device *dev)
  853. {
  854. struct fsmc_nand_data *host = dev_get_drvdata(dev);
  855. if (host)
  856. clk_disable(host->clk);
  857. return 0;
  858. }
  859. static int fsmc_nand_resume(struct device *dev)
  860. {
  861. struct fsmc_nand_data *host = dev_get_drvdata(dev);
  862. if (host) {
  863. clk_enable(host->clk);
  864. fsmc_nand_setup(host->regs_va, host->bank,
  865. host->nand.options & NAND_BUSWIDTH_16,
  866. host->dev_timings);
  867. }
  868. return 0;
  869. }
  870. static SIMPLE_DEV_PM_OPS(fsmc_nand_pm_ops, fsmc_nand_suspend, fsmc_nand_resume);
  871. #endif
  872. static struct platform_driver fsmc_nand_driver = {
  873. .remove = fsmc_nand_remove,
  874. .driver = {
  875. .owner = THIS_MODULE,
  876. .name = "fsmc-nand",
  877. #ifdef CONFIG_PM
  878. .pm = &fsmc_nand_pm_ops,
  879. #endif
  880. },
  881. };
  882. static int __init fsmc_nand_init(void)
  883. {
  884. return platform_driver_probe(&fsmc_nand_driver,
  885. fsmc_nand_probe);
  886. }
  887. module_init(fsmc_nand_init);
  888. static void __exit fsmc_nand_exit(void)
  889. {
  890. platform_driver_unregister(&fsmc_nand_driver);
  891. }
  892. module_exit(fsmc_nand_exit);
  893. MODULE_LICENSE("GPL");
  894. MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>, Ashish Priyadarshi");
  895. MODULE_DESCRIPTION("NAND driver for SPEAr Platforms");