fsmc_nand.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  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/completion.h>
  20. #include <linux/dmaengine.h>
  21. #include <linux/dma-direction.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/err.h>
  24. #include <linux/init.h>
  25. #include <linux/module.h>
  26. #include <linux/resource.h>
  27. #include <linux/sched.h>
  28. #include <linux/types.h>
  29. #include <linux/mtd/mtd.h>
  30. #include <linux/mtd/nand.h>
  31. #include <linux/mtd/nand_ecc.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/of.h>
  34. #include <linux/mtd/partitions.h>
  35. #include <linux/io.h>
  36. #include <linux/slab.h>
  37. #include <linux/mtd/fsmc.h>
  38. #include <linux/amba/bus.h>
  39. #include <mtd/mtd-abi.h>
  40. static struct nand_ecclayout fsmc_ecc1_128_layout = {
  41. .eccbytes = 24,
  42. .eccpos = {2, 3, 4, 18, 19, 20, 34, 35, 36, 50, 51, 52,
  43. 66, 67, 68, 82, 83, 84, 98, 99, 100, 114, 115, 116},
  44. .oobfree = {
  45. {.offset = 8, .length = 8},
  46. {.offset = 24, .length = 8},
  47. {.offset = 40, .length = 8},
  48. {.offset = 56, .length = 8},
  49. {.offset = 72, .length = 8},
  50. {.offset = 88, .length = 8},
  51. {.offset = 104, .length = 8},
  52. {.offset = 120, .length = 8}
  53. }
  54. };
  55. static struct nand_ecclayout fsmc_ecc1_64_layout = {
  56. .eccbytes = 12,
  57. .eccpos = {2, 3, 4, 18, 19, 20, 34, 35, 36, 50, 51, 52},
  58. .oobfree = {
  59. {.offset = 8, .length = 8},
  60. {.offset = 24, .length = 8},
  61. {.offset = 40, .length = 8},
  62. {.offset = 56, .length = 8},
  63. }
  64. };
  65. static struct nand_ecclayout fsmc_ecc1_16_layout = {
  66. .eccbytes = 3,
  67. .eccpos = {2, 3, 4},
  68. .oobfree = {
  69. {.offset = 8, .length = 8},
  70. }
  71. };
  72. /*
  73. * ECC4 layout for NAND of pagesize 8192 bytes & OOBsize 256 bytes. 13*16 bytes
  74. * of OB size is reserved for ECC, Byte no. 0 & 1 reserved for bad block and 46
  75. * bytes are free for use.
  76. */
  77. static struct nand_ecclayout fsmc_ecc4_256_layout = {
  78. .eccbytes = 208,
  79. .eccpos = { 2, 3, 4, 5, 6, 7, 8,
  80. 9, 10, 11, 12, 13, 14,
  81. 18, 19, 20, 21, 22, 23, 24,
  82. 25, 26, 27, 28, 29, 30,
  83. 34, 35, 36, 37, 38, 39, 40,
  84. 41, 42, 43, 44, 45, 46,
  85. 50, 51, 52, 53, 54, 55, 56,
  86. 57, 58, 59, 60, 61, 62,
  87. 66, 67, 68, 69, 70, 71, 72,
  88. 73, 74, 75, 76, 77, 78,
  89. 82, 83, 84, 85, 86, 87, 88,
  90. 89, 90, 91, 92, 93, 94,
  91. 98, 99, 100, 101, 102, 103, 104,
  92. 105, 106, 107, 108, 109, 110,
  93. 114, 115, 116, 117, 118, 119, 120,
  94. 121, 122, 123, 124, 125, 126,
  95. 130, 131, 132, 133, 134, 135, 136,
  96. 137, 138, 139, 140, 141, 142,
  97. 146, 147, 148, 149, 150, 151, 152,
  98. 153, 154, 155, 156, 157, 158,
  99. 162, 163, 164, 165, 166, 167, 168,
  100. 169, 170, 171, 172, 173, 174,
  101. 178, 179, 180, 181, 182, 183, 184,
  102. 185, 186, 187, 188, 189, 190,
  103. 194, 195, 196, 197, 198, 199, 200,
  104. 201, 202, 203, 204, 205, 206,
  105. 210, 211, 212, 213, 214, 215, 216,
  106. 217, 218, 219, 220, 221, 222,
  107. 226, 227, 228, 229, 230, 231, 232,
  108. 233, 234, 235, 236, 237, 238,
  109. 242, 243, 244, 245, 246, 247, 248,
  110. 249, 250, 251, 252, 253, 254
  111. },
  112. .oobfree = {
  113. {.offset = 15, .length = 3},
  114. {.offset = 31, .length = 3},
  115. {.offset = 47, .length = 3},
  116. {.offset = 63, .length = 3},
  117. {.offset = 79, .length = 3},
  118. {.offset = 95, .length = 3},
  119. {.offset = 111, .length = 3},
  120. {.offset = 127, .length = 3},
  121. {.offset = 143, .length = 3},
  122. {.offset = 159, .length = 3},
  123. {.offset = 175, .length = 3},
  124. {.offset = 191, .length = 3},
  125. {.offset = 207, .length = 3},
  126. {.offset = 223, .length = 3},
  127. {.offset = 239, .length = 3},
  128. {.offset = 255, .length = 1}
  129. }
  130. };
  131. /*
  132. * ECC4 layout for NAND of pagesize 4096 bytes & OOBsize 224 bytes. 13*8 bytes
  133. * of OOB size is reserved for ECC, Byte no. 0 & 1 reserved for bad block & 118
  134. * bytes are free for use.
  135. */
  136. static struct nand_ecclayout fsmc_ecc4_224_layout = {
  137. .eccbytes = 104,
  138. .eccpos = { 2, 3, 4, 5, 6, 7, 8,
  139. 9, 10, 11, 12, 13, 14,
  140. 18, 19, 20, 21, 22, 23, 24,
  141. 25, 26, 27, 28, 29, 30,
  142. 34, 35, 36, 37, 38, 39, 40,
  143. 41, 42, 43, 44, 45, 46,
  144. 50, 51, 52, 53, 54, 55, 56,
  145. 57, 58, 59, 60, 61, 62,
  146. 66, 67, 68, 69, 70, 71, 72,
  147. 73, 74, 75, 76, 77, 78,
  148. 82, 83, 84, 85, 86, 87, 88,
  149. 89, 90, 91, 92, 93, 94,
  150. 98, 99, 100, 101, 102, 103, 104,
  151. 105, 106, 107, 108, 109, 110,
  152. 114, 115, 116, 117, 118, 119, 120,
  153. 121, 122, 123, 124, 125, 126
  154. },
  155. .oobfree = {
  156. {.offset = 15, .length = 3},
  157. {.offset = 31, .length = 3},
  158. {.offset = 47, .length = 3},
  159. {.offset = 63, .length = 3},
  160. {.offset = 79, .length = 3},
  161. {.offset = 95, .length = 3},
  162. {.offset = 111, .length = 3},
  163. {.offset = 127, .length = 97}
  164. }
  165. };
  166. /*
  167. * ECC4 layout for NAND of pagesize 4096 bytes & OOBsize 128 bytes. 13*8 bytes
  168. * of OOB size is reserved for ECC, Byte no. 0 & 1 reserved for bad block & 22
  169. * bytes are free for use.
  170. */
  171. static struct nand_ecclayout fsmc_ecc4_128_layout = {
  172. .eccbytes = 104,
  173. .eccpos = { 2, 3, 4, 5, 6, 7, 8,
  174. 9, 10, 11, 12, 13, 14,
  175. 18, 19, 20, 21, 22, 23, 24,
  176. 25, 26, 27, 28, 29, 30,
  177. 34, 35, 36, 37, 38, 39, 40,
  178. 41, 42, 43, 44, 45, 46,
  179. 50, 51, 52, 53, 54, 55, 56,
  180. 57, 58, 59, 60, 61, 62,
  181. 66, 67, 68, 69, 70, 71, 72,
  182. 73, 74, 75, 76, 77, 78,
  183. 82, 83, 84, 85, 86, 87, 88,
  184. 89, 90, 91, 92, 93, 94,
  185. 98, 99, 100, 101, 102, 103, 104,
  186. 105, 106, 107, 108, 109, 110,
  187. 114, 115, 116, 117, 118, 119, 120,
  188. 121, 122, 123, 124, 125, 126
  189. },
  190. .oobfree = {
  191. {.offset = 15, .length = 3},
  192. {.offset = 31, .length = 3},
  193. {.offset = 47, .length = 3},
  194. {.offset = 63, .length = 3},
  195. {.offset = 79, .length = 3},
  196. {.offset = 95, .length = 3},
  197. {.offset = 111, .length = 3},
  198. {.offset = 127, .length = 1}
  199. }
  200. };
  201. /*
  202. * ECC4 layout for NAND of pagesize 2048 bytes & OOBsize 64 bytes. 13*4 bytes of
  203. * OOB size is reserved for ECC, Byte no. 0 & 1 reserved for bad block and 10
  204. * bytes are free for use.
  205. */
  206. static struct nand_ecclayout fsmc_ecc4_64_layout = {
  207. .eccbytes = 52,
  208. .eccpos = { 2, 3, 4, 5, 6, 7, 8,
  209. 9, 10, 11, 12, 13, 14,
  210. 18, 19, 20, 21, 22, 23, 24,
  211. 25, 26, 27, 28, 29, 30,
  212. 34, 35, 36, 37, 38, 39, 40,
  213. 41, 42, 43, 44, 45, 46,
  214. 50, 51, 52, 53, 54, 55, 56,
  215. 57, 58, 59, 60, 61, 62,
  216. },
  217. .oobfree = {
  218. {.offset = 15, .length = 3},
  219. {.offset = 31, .length = 3},
  220. {.offset = 47, .length = 3},
  221. {.offset = 63, .length = 1},
  222. }
  223. };
  224. /*
  225. * ECC4 layout for NAND of pagesize 512 bytes & OOBsize 16 bytes. 13 bytes of
  226. * OOB size is reserved for ECC, Byte no. 4 & 5 reserved for bad block and One
  227. * byte is free for use.
  228. */
  229. static struct nand_ecclayout fsmc_ecc4_16_layout = {
  230. .eccbytes = 13,
  231. .eccpos = { 0, 1, 2, 3, 6, 7, 8,
  232. 9, 10, 11, 12, 13, 14
  233. },
  234. .oobfree = {
  235. {.offset = 15, .length = 1},
  236. }
  237. };
  238. /*
  239. * ECC placement definitions in oobfree type format.
  240. * There are 13 bytes of ecc for every 512 byte block and it has to be read
  241. * consecutively and immediately after the 512 byte data block for hardware to
  242. * generate the error bit offsets in 512 byte data.
  243. * Managing the ecc bytes in the following way makes it easier for software to
  244. * read ecc bytes consecutive to data bytes. This way is similar to
  245. * oobfree structure maintained already in generic nand driver
  246. */
  247. static struct fsmc_eccplace fsmc_ecc4_lp_place = {
  248. .eccplace = {
  249. {.offset = 2, .length = 13},
  250. {.offset = 18, .length = 13},
  251. {.offset = 34, .length = 13},
  252. {.offset = 50, .length = 13},
  253. {.offset = 66, .length = 13},
  254. {.offset = 82, .length = 13},
  255. {.offset = 98, .length = 13},
  256. {.offset = 114, .length = 13}
  257. }
  258. };
  259. static struct fsmc_eccplace fsmc_ecc4_sp_place = {
  260. .eccplace = {
  261. {.offset = 0, .length = 4},
  262. {.offset = 6, .length = 9}
  263. }
  264. };
  265. /**
  266. * struct fsmc_nand_data - structure for FSMC NAND device state
  267. *
  268. * @pid: Part ID on the AMBA PrimeCell format
  269. * @mtd: MTD info for a NAND flash.
  270. * @nand: Chip related info for a NAND flash.
  271. * @partitions: Partition info for a NAND Flash.
  272. * @nr_partitions: Total number of partition of a NAND flash.
  273. *
  274. * @ecc_place: ECC placing locations in oobfree type format.
  275. * @bank: Bank number for probed device.
  276. * @clk: Clock structure for FSMC.
  277. *
  278. * @read_dma_chan: DMA channel for read access
  279. * @write_dma_chan: DMA channel for write access to NAND
  280. * @dma_access_complete: Completion structure
  281. *
  282. * @data_pa: NAND Physical port for Data.
  283. * @data_va: NAND port for Data.
  284. * @cmd_va: NAND port for Command.
  285. * @addr_va: NAND port for Address.
  286. * @regs_va: FSMC regs base address.
  287. */
  288. struct fsmc_nand_data {
  289. u32 pid;
  290. struct mtd_info mtd;
  291. struct nand_chip nand;
  292. struct mtd_partition *partitions;
  293. unsigned int nr_partitions;
  294. struct fsmc_eccplace *ecc_place;
  295. unsigned int bank;
  296. struct device *dev;
  297. enum access_mode mode;
  298. struct clk *clk;
  299. /* DMA related objects */
  300. struct dma_chan *read_dma_chan;
  301. struct dma_chan *write_dma_chan;
  302. struct completion dma_access_complete;
  303. struct fsmc_nand_timings *dev_timings;
  304. dma_addr_t data_pa;
  305. void __iomem *data_va;
  306. void __iomem *cmd_va;
  307. void __iomem *addr_va;
  308. void __iomem *regs_va;
  309. void (*select_chip)(uint32_t bank, uint32_t busw);
  310. };
  311. /* Assert CS signal based on chipnr */
  312. static void fsmc_select_chip(struct mtd_info *mtd, int chipnr)
  313. {
  314. struct nand_chip *chip = mtd->priv;
  315. struct fsmc_nand_data *host;
  316. host = container_of(mtd, struct fsmc_nand_data, mtd);
  317. switch (chipnr) {
  318. case -1:
  319. chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
  320. break;
  321. case 0:
  322. case 1:
  323. case 2:
  324. case 3:
  325. if (host->select_chip)
  326. host->select_chip(chipnr,
  327. chip->options & NAND_BUSWIDTH_16);
  328. break;
  329. default:
  330. BUG();
  331. }
  332. }
  333. /*
  334. * fsmc_cmd_ctrl - For facilitaing Hardware access
  335. * This routine allows hardware specific access to control-lines(ALE,CLE)
  336. */
  337. static void fsmc_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  338. {
  339. struct nand_chip *this = mtd->priv;
  340. struct fsmc_nand_data *host = container_of(mtd,
  341. struct fsmc_nand_data, mtd);
  342. void __iomem *regs = host->regs_va;
  343. unsigned int bank = host->bank;
  344. if (ctrl & NAND_CTRL_CHANGE) {
  345. u32 pc;
  346. if (ctrl & NAND_CLE) {
  347. this->IO_ADDR_R = host->cmd_va;
  348. this->IO_ADDR_W = host->cmd_va;
  349. } else if (ctrl & NAND_ALE) {
  350. this->IO_ADDR_R = host->addr_va;
  351. this->IO_ADDR_W = host->addr_va;
  352. } else {
  353. this->IO_ADDR_R = host->data_va;
  354. this->IO_ADDR_W = host->data_va;
  355. }
  356. pc = readl(FSMC_NAND_REG(regs, bank, PC));
  357. if (ctrl & NAND_NCE)
  358. pc |= FSMC_ENABLE;
  359. else
  360. pc &= ~FSMC_ENABLE;
  361. writel_relaxed(pc, FSMC_NAND_REG(regs, bank, PC));
  362. }
  363. mb();
  364. if (cmd != NAND_CMD_NONE)
  365. writeb_relaxed(cmd, this->IO_ADDR_W);
  366. }
  367. /*
  368. * fsmc_nand_setup - FSMC (Flexible Static Memory Controller) init routine
  369. *
  370. * This routine initializes timing parameters related to NAND memory access in
  371. * FSMC registers
  372. */
  373. static void fsmc_nand_setup(void __iomem *regs, uint32_t bank,
  374. uint32_t busw, struct fsmc_nand_timings *timings)
  375. {
  376. uint32_t value = FSMC_DEVTYPE_NAND | FSMC_ENABLE | FSMC_WAITON;
  377. uint32_t tclr, tar, thiz, thold, twait, tset;
  378. struct fsmc_nand_timings *tims;
  379. struct fsmc_nand_timings default_timings = {
  380. .tclr = FSMC_TCLR_1,
  381. .tar = FSMC_TAR_1,
  382. .thiz = FSMC_THIZ_1,
  383. .thold = FSMC_THOLD_4,
  384. .twait = FSMC_TWAIT_6,
  385. .tset = FSMC_TSET_0,
  386. };
  387. if (timings)
  388. tims = timings;
  389. else
  390. tims = &default_timings;
  391. tclr = (tims->tclr & FSMC_TCLR_MASK) << FSMC_TCLR_SHIFT;
  392. tar = (tims->tar & FSMC_TAR_MASK) << FSMC_TAR_SHIFT;
  393. thiz = (tims->thiz & FSMC_THIZ_MASK) << FSMC_THIZ_SHIFT;
  394. thold = (tims->thold & FSMC_THOLD_MASK) << FSMC_THOLD_SHIFT;
  395. twait = (tims->twait & FSMC_TWAIT_MASK) << FSMC_TWAIT_SHIFT;
  396. tset = (tims->tset & FSMC_TSET_MASK) << FSMC_TSET_SHIFT;
  397. if (busw)
  398. writel_relaxed(value | FSMC_DEVWID_16,
  399. FSMC_NAND_REG(regs, bank, PC));
  400. else
  401. writel_relaxed(value | FSMC_DEVWID_8,
  402. FSMC_NAND_REG(regs, bank, PC));
  403. writel_relaxed(readl(FSMC_NAND_REG(regs, bank, PC)) | tclr | tar,
  404. FSMC_NAND_REG(regs, bank, PC));
  405. writel_relaxed(thiz | thold | twait | tset,
  406. FSMC_NAND_REG(regs, bank, COMM));
  407. writel_relaxed(thiz | thold | twait | tset,
  408. FSMC_NAND_REG(regs, bank, ATTRIB));
  409. }
  410. /*
  411. * fsmc_enable_hwecc - Enables Hardware ECC through FSMC registers
  412. */
  413. static void fsmc_enable_hwecc(struct mtd_info *mtd, int mode)
  414. {
  415. struct fsmc_nand_data *host = container_of(mtd,
  416. struct fsmc_nand_data, mtd);
  417. void __iomem *regs = host->regs_va;
  418. uint32_t bank = host->bank;
  419. writel_relaxed(readl(FSMC_NAND_REG(regs, bank, PC)) & ~FSMC_ECCPLEN_256,
  420. FSMC_NAND_REG(regs, bank, PC));
  421. writel_relaxed(readl(FSMC_NAND_REG(regs, bank, PC)) & ~FSMC_ECCEN,
  422. FSMC_NAND_REG(regs, bank, PC));
  423. writel_relaxed(readl(FSMC_NAND_REG(regs, bank, PC)) | FSMC_ECCEN,
  424. FSMC_NAND_REG(regs, bank, PC));
  425. }
  426. /*
  427. * fsmc_read_hwecc_ecc4 - Hardware ECC calculator for ecc4 option supported by
  428. * FSMC. ECC is 13 bytes for 512 bytes of data (supports error correction up to
  429. * max of 8-bits)
  430. */
  431. static int fsmc_read_hwecc_ecc4(struct mtd_info *mtd, const uint8_t *data,
  432. uint8_t *ecc)
  433. {
  434. struct fsmc_nand_data *host = container_of(mtd,
  435. struct fsmc_nand_data, mtd);
  436. void __iomem *regs = host->regs_va;
  437. uint32_t bank = host->bank;
  438. uint32_t ecc_tmp;
  439. unsigned long deadline = jiffies + FSMC_BUSY_WAIT_TIMEOUT;
  440. do {
  441. if (readl_relaxed(FSMC_NAND_REG(regs, bank, STS)) & FSMC_CODE_RDY)
  442. break;
  443. else
  444. cond_resched();
  445. } while (!time_after_eq(jiffies, deadline));
  446. if (time_after_eq(jiffies, deadline)) {
  447. dev_err(host->dev, "calculate ecc timed out\n");
  448. return -ETIMEDOUT;
  449. }
  450. ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC1));
  451. ecc[0] = (uint8_t) (ecc_tmp >> 0);
  452. ecc[1] = (uint8_t) (ecc_tmp >> 8);
  453. ecc[2] = (uint8_t) (ecc_tmp >> 16);
  454. ecc[3] = (uint8_t) (ecc_tmp >> 24);
  455. ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC2));
  456. ecc[4] = (uint8_t) (ecc_tmp >> 0);
  457. ecc[5] = (uint8_t) (ecc_tmp >> 8);
  458. ecc[6] = (uint8_t) (ecc_tmp >> 16);
  459. ecc[7] = (uint8_t) (ecc_tmp >> 24);
  460. ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC3));
  461. ecc[8] = (uint8_t) (ecc_tmp >> 0);
  462. ecc[9] = (uint8_t) (ecc_tmp >> 8);
  463. ecc[10] = (uint8_t) (ecc_tmp >> 16);
  464. ecc[11] = (uint8_t) (ecc_tmp >> 24);
  465. ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, STS));
  466. ecc[12] = (uint8_t) (ecc_tmp >> 16);
  467. return 0;
  468. }
  469. /*
  470. * fsmc_read_hwecc_ecc1 - Hardware ECC calculator for ecc1 option supported by
  471. * FSMC. ECC is 3 bytes for 512 bytes of data (supports error correction up to
  472. * max of 1-bit)
  473. */
  474. static int fsmc_read_hwecc_ecc1(struct mtd_info *mtd, const uint8_t *data,
  475. uint8_t *ecc)
  476. {
  477. struct fsmc_nand_data *host = container_of(mtd,
  478. struct fsmc_nand_data, mtd);
  479. void __iomem *regs = host->regs_va;
  480. uint32_t bank = host->bank;
  481. uint32_t ecc_tmp;
  482. ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC1));
  483. ecc[0] = (uint8_t) (ecc_tmp >> 0);
  484. ecc[1] = (uint8_t) (ecc_tmp >> 8);
  485. ecc[2] = (uint8_t) (ecc_tmp >> 16);
  486. return 0;
  487. }
  488. /* Count the number of 0's in buff upto a max of max_bits */
  489. static int count_written_bits(uint8_t *buff, int size, int max_bits)
  490. {
  491. int k, written_bits = 0;
  492. for (k = 0; k < size; k++) {
  493. written_bits += hweight8(~buff[k]);
  494. if (written_bits > max_bits)
  495. break;
  496. }
  497. return written_bits;
  498. }
  499. static void dma_complete(void *param)
  500. {
  501. struct fsmc_nand_data *host = param;
  502. complete(&host->dma_access_complete);
  503. }
  504. static int dma_xfer(struct fsmc_nand_data *host, void *buffer, int len,
  505. enum dma_data_direction direction)
  506. {
  507. struct dma_chan *chan;
  508. struct dma_device *dma_dev;
  509. struct dma_async_tx_descriptor *tx;
  510. dma_addr_t dma_dst, dma_src, dma_addr;
  511. dma_cookie_t cookie;
  512. unsigned long flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
  513. int ret;
  514. if (direction == DMA_TO_DEVICE)
  515. chan = host->write_dma_chan;
  516. else if (direction == DMA_FROM_DEVICE)
  517. chan = host->read_dma_chan;
  518. else
  519. return -EINVAL;
  520. dma_dev = chan->device;
  521. dma_addr = dma_map_single(dma_dev->dev, buffer, len, direction);
  522. flags |= DMA_COMPL_SKIP_SRC_UNMAP | DMA_COMPL_SKIP_DEST_UNMAP;
  523. if (direction == DMA_TO_DEVICE) {
  524. dma_src = dma_addr;
  525. dma_dst = host->data_pa;
  526. } else {
  527. dma_src = host->data_pa;
  528. dma_dst = dma_addr;
  529. }
  530. tx = dma_dev->device_prep_dma_memcpy(chan, dma_dst, dma_src,
  531. len, flags);
  532. if (!tx) {
  533. dev_err(host->dev, "device_prep_dma_memcpy error\n");
  534. ret = -EIO;
  535. goto unmap_dma;
  536. }
  537. tx->callback = dma_complete;
  538. tx->callback_param = host;
  539. cookie = tx->tx_submit(tx);
  540. ret = dma_submit_error(cookie);
  541. if (ret) {
  542. dev_err(host->dev, "dma_submit_error %d\n", cookie);
  543. goto unmap_dma;
  544. }
  545. dma_async_issue_pending(chan);
  546. ret =
  547. wait_for_completion_timeout(&host->dma_access_complete,
  548. msecs_to_jiffies(3000));
  549. if (ret <= 0) {
  550. chan->device->device_control(chan, DMA_TERMINATE_ALL, 0);
  551. dev_err(host->dev, "wait_for_completion_timeout\n");
  552. if (!ret)
  553. ret = -ETIMEDOUT;
  554. goto unmap_dma;
  555. }
  556. ret = 0;
  557. unmap_dma:
  558. dma_unmap_single(dma_dev->dev, dma_addr, len, direction);
  559. return ret;
  560. }
  561. /*
  562. * fsmc_write_buf - write buffer to chip
  563. * @mtd: MTD device structure
  564. * @buf: data buffer
  565. * @len: number of bytes to write
  566. */
  567. static void fsmc_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  568. {
  569. int i;
  570. struct nand_chip *chip = mtd->priv;
  571. if (IS_ALIGNED((uint32_t)buf, sizeof(uint32_t)) &&
  572. IS_ALIGNED(len, sizeof(uint32_t))) {
  573. uint32_t *p = (uint32_t *)buf;
  574. len = len >> 2;
  575. for (i = 0; i < len; i++)
  576. writel_relaxed(p[i], chip->IO_ADDR_W);
  577. } else {
  578. for (i = 0; i < len; i++)
  579. writeb_relaxed(buf[i], chip->IO_ADDR_W);
  580. }
  581. }
  582. /*
  583. * fsmc_read_buf - read chip data into buffer
  584. * @mtd: MTD device structure
  585. * @buf: buffer to store date
  586. * @len: number of bytes to read
  587. */
  588. static void fsmc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  589. {
  590. int i;
  591. struct nand_chip *chip = mtd->priv;
  592. if (IS_ALIGNED((uint32_t)buf, sizeof(uint32_t)) &&
  593. IS_ALIGNED(len, sizeof(uint32_t))) {
  594. uint32_t *p = (uint32_t *)buf;
  595. len = len >> 2;
  596. for (i = 0; i < len; i++)
  597. p[i] = readl_relaxed(chip->IO_ADDR_R);
  598. } else {
  599. for (i = 0; i < len; i++)
  600. buf[i] = readb_relaxed(chip->IO_ADDR_R);
  601. }
  602. }
  603. /*
  604. * fsmc_read_buf_dma - read chip data into buffer
  605. * @mtd: MTD device structure
  606. * @buf: buffer to store date
  607. * @len: number of bytes to read
  608. */
  609. static void fsmc_read_buf_dma(struct mtd_info *mtd, uint8_t *buf, int len)
  610. {
  611. struct fsmc_nand_data *host;
  612. host = container_of(mtd, struct fsmc_nand_data, mtd);
  613. dma_xfer(host, buf, len, DMA_FROM_DEVICE);
  614. }
  615. /*
  616. * fsmc_write_buf_dma - write buffer to chip
  617. * @mtd: MTD device structure
  618. * @buf: data buffer
  619. * @len: number of bytes to write
  620. */
  621. static void fsmc_write_buf_dma(struct mtd_info *mtd, const uint8_t *buf,
  622. int len)
  623. {
  624. struct fsmc_nand_data *host;
  625. host = container_of(mtd, struct fsmc_nand_data, mtd);
  626. dma_xfer(host, (void *)buf, len, DMA_TO_DEVICE);
  627. }
  628. /*
  629. * fsmc_read_page_hwecc
  630. * @mtd: mtd info structure
  631. * @chip: nand chip info structure
  632. * @buf: buffer to store read data
  633. * @oob_required: caller expects OOB data read to chip->oob_poi
  634. * @page: page number to read
  635. *
  636. * This routine is needed for fsmc version 8 as reading from NAND chip has to be
  637. * performed in a strict sequence as follows:
  638. * data(512 byte) -> ecc(13 byte)
  639. * After this read, fsmc hardware generates and reports error data bits(up to a
  640. * max of 8 bits)
  641. */
  642. static int fsmc_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  643. uint8_t *buf, int oob_required, int page)
  644. {
  645. struct fsmc_nand_data *host = container_of(mtd,
  646. struct fsmc_nand_data, mtd);
  647. struct fsmc_eccplace *ecc_place = host->ecc_place;
  648. int i, j, s, stat, eccsize = chip->ecc.size;
  649. int eccbytes = chip->ecc.bytes;
  650. int eccsteps = chip->ecc.steps;
  651. uint8_t *p = buf;
  652. uint8_t *ecc_calc = chip->buffers->ecccalc;
  653. uint8_t *ecc_code = chip->buffers->ecccode;
  654. int off, len, group = 0;
  655. /*
  656. * ecc_oob is intentionally taken as uint16_t. In 16bit devices, we
  657. * end up reading 14 bytes (7 words) from oob. The local array is
  658. * to maintain word alignment
  659. */
  660. uint16_t ecc_oob[7];
  661. uint8_t *oob = (uint8_t *)&ecc_oob[0];
  662. unsigned int max_bitflips = 0;
  663. for (i = 0, s = 0; s < eccsteps; s++, i += eccbytes, p += eccsize) {
  664. chip->cmdfunc(mtd, NAND_CMD_READ0, s * eccsize, page);
  665. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  666. chip->read_buf(mtd, p, eccsize);
  667. for (j = 0; j < eccbytes;) {
  668. off = ecc_place->eccplace[group].offset;
  669. len = ecc_place->eccplace[group].length;
  670. group++;
  671. /*
  672. * length is intentionally kept a higher multiple of 2
  673. * to read at least 13 bytes even in case of 16 bit NAND
  674. * devices
  675. */
  676. if (chip->options & NAND_BUSWIDTH_16)
  677. len = roundup(len, 2);
  678. chip->cmdfunc(mtd, NAND_CMD_READOOB, off, page);
  679. chip->read_buf(mtd, oob + j, len);
  680. j += len;
  681. }
  682. memcpy(&ecc_code[i], oob, chip->ecc.bytes);
  683. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  684. stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  685. if (stat < 0) {
  686. mtd->ecc_stats.failed++;
  687. } else {
  688. mtd->ecc_stats.corrected += stat;
  689. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  690. }
  691. }
  692. return max_bitflips;
  693. }
  694. /*
  695. * fsmc_bch8_correct_data
  696. * @mtd: mtd info structure
  697. * @dat: buffer of read data
  698. * @read_ecc: ecc read from device spare area
  699. * @calc_ecc: ecc calculated from read data
  700. *
  701. * calc_ecc is a 104 bit information containing maximum of 8 error
  702. * offset informations of 13 bits each in 512 bytes of read data.
  703. */
  704. static int fsmc_bch8_correct_data(struct mtd_info *mtd, uint8_t *dat,
  705. uint8_t *read_ecc, uint8_t *calc_ecc)
  706. {
  707. struct fsmc_nand_data *host = container_of(mtd,
  708. struct fsmc_nand_data, mtd);
  709. struct nand_chip *chip = mtd->priv;
  710. void __iomem *regs = host->regs_va;
  711. unsigned int bank = host->bank;
  712. uint32_t err_idx[8];
  713. uint32_t num_err, i;
  714. uint32_t ecc1, ecc2, ecc3, ecc4;
  715. num_err = (readl_relaxed(FSMC_NAND_REG(regs, bank, STS)) >> 10) & 0xF;
  716. /* no bit flipping */
  717. if (likely(num_err == 0))
  718. return 0;
  719. /* too many errors */
  720. if (unlikely(num_err > 8)) {
  721. /*
  722. * This is a temporary erase check. A newly erased page read
  723. * would result in an ecc error because the oob data is also
  724. * erased to FF and the calculated ecc for an FF data is not
  725. * FF..FF.
  726. * This is a workaround to skip performing correction in case
  727. * data is FF..FF
  728. *
  729. * Logic:
  730. * For every page, each bit written as 0 is counted until these
  731. * number of bits are greater than 8 (the maximum correction
  732. * capability of FSMC for each 512 + 13 bytes)
  733. */
  734. int bits_ecc = count_written_bits(read_ecc, chip->ecc.bytes, 8);
  735. int bits_data = count_written_bits(dat, chip->ecc.size, 8);
  736. if ((bits_ecc + bits_data) <= 8) {
  737. if (bits_data)
  738. memset(dat, 0xff, chip->ecc.size);
  739. return bits_data;
  740. }
  741. return -EBADMSG;
  742. }
  743. /*
  744. * ------------------- calc_ecc[] bit wise -----------|--13 bits--|
  745. * |---idx[7]--|--.....-----|---idx[2]--||---idx[1]--||---idx[0]--|
  746. *
  747. * calc_ecc is a 104 bit information containing maximum of 8 error
  748. * offset informations of 13 bits each. calc_ecc is copied into a
  749. * uint64_t array and error offset indexes are populated in err_idx
  750. * array
  751. */
  752. ecc1 = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC1));
  753. ecc2 = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC2));
  754. ecc3 = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC3));
  755. ecc4 = readl_relaxed(FSMC_NAND_REG(regs, bank, STS));
  756. err_idx[0] = (ecc1 >> 0) & 0x1FFF;
  757. err_idx[1] = (ecc1 >> 13) & 0x1FFF;
  758. err_idx[2] = (((ecc2 >> 0) & 0x7F) << 6) | ((ecc1 >> 26) & 0x3F);
  759. err_idx[3] = (ecc2 >> 7) & 0x1FFF;
  760. err_idx[4] = (((ecc3 >> 0) & 0x1) << 12) | ((ecc2 >> 20) & 0xFFF);
  761. err_idx[5] = (ecc3 >> 1) & 0x1FFF;
  762. err_idx[6] = (ecc3 >> 14) & 0x1FFF;
  763. err_idx[7] = (((ecc4 >> 16) & 0xFF) << 5) | ((ecc3 >> 27) & 0x1F);
  764. i = 0;
  765. while (num_err--) {
  766. change_bit(0, (unsigned long *)&err_idx[i]);
  767. change_bit(1, (unsigned long *)&err_idx[i]);
  768. if (err_idx[i] < chip->ecc.size * 8) {
  769. change_bit(err_idx[i], (unsigned long *)dat);
  770. i++;
  771. }
  772. }
  773. return i;
  774. }
  775. static bool filter(struct dma_chan *chan, void *slave)
  776. {
  777. chan->private = slave;
  778. return true;
  779. }
  780. #ifdef CONFIG_OF
  781. static int fsmc_nand_probe_config_dt(struct platform_device *pdev,
  782. struct device_node *np)
  783. {
  784. struct fsmc_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
  785. u32 val;
  786. /* Set default NAND width to 8 bits */
  787. pdata->width = 8;
  788. if (!of_property_read_u32(np, "bank-width", &val)) {
  789. if (val == 2) {
  790. pdata->width = 16;
  791. } else if (val != 1) {
  792. dev_err(&pdev->dev, "invalid bank-width %u\n", val);
  793. return -EINVAL;
  794. }
  795. }
  796. if (of_get_property(np, "nand-skip-bbtscan", NULL))
  797. pdata->options = NAND_SKIP_BBTSCAN;
  798. pdata->nand_timings = devm_kzalloc(&pdev->dev,
  799. sizeof(*pdata->nand_timings), GFP_KERNEL);
  800. if (!pdata->nand_timings) {
  801. dev_err(&pdev->dev, "no memory for nand_timing\n");
  802. return -ENOMEM;
  803. }
  804. of_property_read_u8_array(np, "timings", (u8 *)pdata->nand_timings,
  805. sizeof(*pdata->nand_timings));
  806. /* Set default NAND bank to 0 */
  807. pdata->bank = 0;
  808. if (!of_property_read_u32(np, "bank", &val)) {
  809. if (val > 3) {
  810. dev_err(&pdev->dev, "invalid bank %u\n", val);
  811. return -EINVAL;
  812. }
  813. pdata->bank = val;
  814. }
  815. return 0;
  816. }
  817. #else
  818. static int fsmc_nand_probe_config_dt(struct platform_device *pdev,
  819. struct device_node *np)
  820. {
  821. return -ENOSYS;
  822. }
  823. #endif
  824. /*
  825. * fsmc_nand_probe - Probe function
  826. * @pdev: platform device structure
  827. */
  828. static int __init fsmc_nand_probe(struct platform_device *pdev)
  829. {
  830. struct fsmc_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
  831. struct device_node __maybe_unused *np = pdev->dev.of_node;
  832. struct mtd_part_parser_data ppdata = {};
  833. struct fsmc_nand_data *host;
  834. struct mtd_info *mtd;
  835. struct nand_chip *nand;
  836. struct resource *res;
  837. dma_cap_mask_t mask;
  838. int ret = 0;
  839. u32 pid;
  840. int i;
  841. if (np) {
  842. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  843. pdev->dev.platform_data = pdata;
  844. ret = fsmc_nand_probe_config_dt(pdev, np);
  845. if (ret) {
  846. dev_err(&pdev->dev, "no platform data\n");
  847. return -ENODEV;
  848. }
  849. }
  850. if (!pdata) {
  851. dev_err(&pdev->dev, "platform data is NULL\n");
  852. return -EINVAL;
  853. }
  854. /* Allocate memory for the device structure (and zero it) */
  855. host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
  856. if (!host) {
  857. dev_err(&pdev->dev, "failed to allocate device structure\n");
  858. return -ENOMEM;
  859. }
  860. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_data");
  861. if (!res)
  862. return -EINVAL;
  863. host->data_va = devm_ioremap_resource(&pdev->dev, res);
  864. if (IS_ERR(host->data_va))
  865. return PTR_ERR(host->data_va);
  866. host->data_pa = (dma_addr_t)res->start;
  867. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_addr");
  868. if (!res)
  869. return -EINVAL;
  870. host->addr_va = devm_ioremap_resource(&pdev->dev, res);
  871. if (IS_ERR(host->addr_va))
  872. return PTR_ERR(host->addr_va);
  873. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_cmd");
  874. if (!res)
  875. return -EINVAL;
  876. host->cmd_va = devm_ioremap_resource(&pdev->dev, res);
  877. if (IS_ERR(host->cmd_va))
  878. return PTR_ERR(host->cmd_va);
  879. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "fsmc_regs");
  880. if (!res)
  881. return -EINVAL;
  882. host->regs_va = devm_ioremap_resource(&pdev->dev, res);
  883. if (IS_ERR(host->regs_va))
  884. return PTR_ERR(host->regs_va);
  885. host->clk = clk_get(&pdev->dev, NULL);
  886. if (IS_ERR(host->clk)) {
  887. dev_err(&pdev->dev, "failed to fetch block clock\n");
  888. return PTR_ERR(host->clk);
  889. }
  890. ret = clk_prepare_enable(host->clk);
  891. if (ret)
  892. goto err_clk_prepare_enable;
  893. /*
  894. * This device ID is actually a common AMBA ID as used on the
  895. * AMBA PrimeCell bus. However it is not a PrimeCell.
  896. */
  897. for (pid = 0, i = 0; i < 4; i++)
  898. pid |= (readl(host->regs_va + resource_size(res) - 0x20 + 4 * i) & 255) << (i * 8);
  899. host->pid = pid;
  900. dev_info(&pdev->dev, "FSMC device partno %03x, manufacturer %02x, "
  901. "revision %02x, config %02x\n",
  902. AMBA_PART_BITS(pid), AMBA_MANF_BITS(pid),
  903. AMBA_REV_BITS(pid), AMBA_CONFIG_BITS(pid));
  904. host->bank = pdata->bank;
  905. host->select_chip = pdata->select_bank;
  906. host->partitions = pdata->partitions;
  907. host->nr_partitions = pdata->nr_partitions;
  908. host->dev = &pdev->dev;
  909. host->dev_timings = pdata->nand_timings;
  910. host->mode = pdata->mode;
  911. if (host->mode == USE_DMA_ACCESS)
  912. init_completion(&host->dma_access_complete);
  913. /* Link all private pointers */
  914. mtd = &host->mtd;
  915. nand = &host->nand;
  916. mtd->priv = nand;
  917. nand->priv = host;
  918. host->mtd.owner = THIS_MODULE;
  919. nand->IO_ADDR_R = host->data_va;
  920. nand->IO_ADDR_W = host->data_va;
  921. nand->cmd_ctrl = fsmc_cmd_ctrl;
  922. nand->chip_delay = 30;
  923. nand->ecc.mode = NAND_ECC_HW;
  924. nand->ecc.hwctl = fsmc_enable_hwecc;
  925. nand->ecc.size = 512;
  926. nand->options = pdata->options;
  927. nand->select_chip = fsmc_select_chip;
  928. nand->badblockbits = 7;
  929. if (pdata->width == FSMC_NAND_BW16)
  930. nand->options |= NAND_BUSWIDTH_16;
  931. switch (host->mode) {
  932. case USE_DMA_ACCESS:
  933. dma_cap_zero(mask);
  934. dma_cap_set(DMA_MEMCPY, mask);
  935. host->read_dma_chan = dma_request_channel(mask, filter,
  936. pdata->read_dma_priv);
  937. if (!host->read_dma_chan) {
  938. dev_err(&pdev->dev, "Unable to get read dma channel\n");
  939. goto err_req_read_chnl;
  940. }
  941. host->write_dma_chan = dma_request_channel(mask, filter,
  942. pdata->write_dma_priv);
  943. if (!host->write_dma_chan) {
  944. dev_err(&pdev->dev, "Unable to get write dma channel\n");
  945. goto err_req_write_chnl;
  946. }
  947. nand->read_buf = fsmc_read_buf_dma;
  948. nand->write_buf = fsmc_write_buf_dma;
  949. break;
  950. default:
  951. case USE_WORD_ACCESS:
  952. nand->read_buf = fsmc_read_buf;
  953. nand->write_buf = fsmc_write_buf;
  954. break;
  955. }
  956. fsmc_nand_setup(host->regs_va, host->bank,
  957. nand->options & NAND_BUSWIDTH_16,
  958. host->dev_timings);
  959. if (AMBA_REV_BITS(host->pid) >= 8) {
  960. nand->ecc.read_page = fsmc_read_page_hwecc;
  961. nand->ecc.calculate = fsmc_read_hwecc_ecc4;
  962. nand->ecc.correct = fsmc_bch8_correct_data;
  963. nand->ecc.bytes = 13;
  964. nand->ecc.strength = 8;
  965. } else {
  966. nand->ecc.calculate = fsmc_read_hwecc_ecc1;
  967. nand->ecc.correct = nand_correct_data;
  968. nand->ecc.bytes = 3;
  969. nand->ecc.strength = 1;
  970. }
  971. /*
  972. * Scan to find existence of the device
  973. */
  974. if (nand_scan_ident(&host->mtd, 1, NULL)) {
  975. ret = -ENXIO;
  976. dev_err(&pdev->dev, "No NAND Device found!\n");
  977. goto err_scan_ident;
  978. }
  979. if (AMBA_REV_BITS(host->pid) >= 8) {
  980. switch (host->mtd.oobsize) {
  981. case 16:
  982. nand->ecc.layout = &fsmc_ecc4_16_layout;
  983. host->ecc_place = &fsmc_ecc4_sp_place;
  984. break;
  985. case 64:
  986. nand->ecc.layout = &fsmc_ecc4_64_layout;
  987. host->ecc_place = &fsmc_ecc4_lp_place;
  988. break;
  989. case 128:
  990. nand->ecc.layout = &fsmc_ecc4_128_layout;
  991. host->ecc_place = &fsmc_ecc4_lp_place;
  992. break;
  993. case 224:
  994. nand->ecc.layout = &fsmc_ecc4_224_layout;
  995. host->ecc_place = &fsmc_ecc4_lp_place;
  996. break;
  997. case 256:
  998. nand->ecc.layout = &fsmc_ecc4_256_layout;
  999. host->ecc_place = &fsmc_ecc4_lp_place;
  1000. break;
  1001. default:
  1002. printk(KERN_WARNING "No oob scheme defined for "
  1003. "oobsize %d\n", mtd->oobsize);
  1004. BUG();
  1005. }
  1006. } else {
  1007. switch (host->mtd.oobsize) {
  1008. case 16:
  1009. nand->ecc.layout = &fsmc_ecc1_16_layout;
  1010. break;
  1011. case 64:
  1012. nand->ecc.layout = &fsmc_ecc1_64_layout;
  1013. break;
  1014. case 128:
  1015. nand->ecc.layout = &fsmc_ecc1_128_layout;
  1016. break;
  1017. default:
  1018. printk(KERN_WARNING "No oob scheme defined for "
  1019. "oobsize %d\n", mtd->oobsize);
  1020. BUG();
  1021. }
  1022. }
  1023. /* Second stage of scan to fill MTD data-structures */
  1024. if (nand_scan_tail(&host->mtd)) {
  1025. ret = -ENXIO;
  1026. goto err_probe;
  1027. }
  1028. /*
  1029. * The partition information can is accessed by (in the same precedence)
  1030. *
  1031. * command line through Bootloader,
  1032. * platform data,
  1033. * default partition information present in driver.
  1034. */
  1035. /*
  1036. * Check for partition info passed
  1037. */
  1038. host->mtd.name = "nand";
  1039. ppdata.of_node = np;
  1040. ret = mtd_device_parse_register(&host->mtd, NULL, &ppdata,
  1041. host->partitions, host->nr_partitions);
  1042. if (ret)
  1043. goto err_probe;
  1044. platform_set_drvdata(pdev, host);
  1045. dev_info(&pdev->dev, "FSMC NAND driver registration successful\n");
  1046. return 0;
  1047. err_probe:
  1048. err_scan_ident:
  1049. if (host->mode == USE_DMA_ACCESS)
  1050. dma_release_channel(host->write_dma_chan);
  1051. err_req_write_chnl:
  1052. if (host->mode == USE_DMA_ACCESS)
  1053. dma_release_channel(host->read_dma_chan);
  1054. err_req_read_chnl:
  1055. clk_disable_unprepare(host->clk);
  1056. err_clk_prepare_enable:
  1057. clk_put(host->clk);
  1058. return ret;
  1059. }
  1060. /*
  1061. * Clean up routine
  1062. */
  1063. static int fsmc_nand_remove(struct platform_device *pdev)
  1064. {
  1065. struct fsmc_nand_data *host = platform_get_drvdata(pdev);
  1066. platform_set_drvdata(pdev, NULL);
  1067. if (host) {
  1068. nand_release(&host->mtd);
  1069. if (host->mode == USE_DMA_ACCESS) {
  1070. dma_release_channel(host->write_dma_chan);
  1071. dma_release_channel(host->read_dma_chan);
  1072. }
  1073. clk_disable_unprepare(host->clk);
  1074. clk_put(host->clk);
  1075. }
  1076. return 0;
  1077. }
  1078. #ifdef CONFIG_PM_SLEEP
  1079. static int fsmc_nand_suspend(struct device *dev)
  1080. {
  1081. struct fsmc_nand_data *host = dev_get_drvdata(dev);
  1082. if (host)
  1083. clk_disable_unprepare(host->clk);
  1084. return 0;
  1085. }
  1086. static int fsmc_nand_resume(struct device *dev)
  1087. {
  1088. struct fsmc_nand_data *host = dev_get_drvdata(dev);
  1089. if (host) {
  1090. clk_prepare_enable(host->clk);
  1091. fsmc_nand_setup(host->regs_va, host->bank,
  1092. host->nand.options & NAND_BUSWIDTH_16,
  1093. host->dev_timings);
  1094. }
  1095. return 0;
  1096. }
  1097. #endif
  1098. static SIMPLE_DEV_PM_OPS(fsmc_nand_pm_ops, fsmc_nand_suspend, fsmc_nand_resume);
  1099. #ifdef CONFIG_OF
  1100. static const struct of_device_id fsmc_nand_id_table[] = {
  1101. { .compatible = "st,spear600-fsmc-nand" },
  1102. { .compatible = "stericsson,fsmc-nand" },
  1103. {}
  1104. };
  1105. MODULE_DEVICE_TABLE(of, fsmc_nand_id_table);
  1106. #endif
  1107. static struct platform_driver fsmc_nand_driver = {
  1108. .remove = fsmc_nand_remove,
  1109. .driver = {
  1110. .owner = THIS_MODULE,
  1111. .name = "fsmc-nand",
  1112. .of_match_table = of_match_ptr(fsmc_nand_id_table),
  1113. .pm = &fsmc_nand_pm_ops,
  1114. },
  1115. };
  1116. module_platform_driver_probe(fsmc_nand_driver, fsmc_nand_probe);
  1117. MODULE_LICENSE("GPL");
  1118. MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>, Ashish Priyadarshi");
  1119. MODULE_DESCRIPTION("NAND driver for SPEAr Platforms");