fsmc_nand.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  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. return 0;
  799. }
  800. #else
  801. static int fsmc_nand_probe_config_dt(struct platform_device *pdev,
  802. struct device_node *np)
  803. {
  804. return -ENOSYS;
  805. }
  806. #endif
  807. /*
  808. * fsmc_nand_probe - Probe function
  809. * @pdev: platform device structure
  810. */
  811. static int __init fsmc_nand_probe(struct platform_device *pdev)
  812. {
  813. struct fsmc_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
  814. struct device_node __maybe_unused *np = pdev->dev.of_node;
  815. struct mtd_part_parser_data ppdata = {};
  816. struct fsmc_nand_data *host;
  817. struct mtd_info *mtd;
  818. struct nand_chip *nand;
  819. struct resource *res;
  820. dma_cap_mask_t mask;
  821. int ret = 0;
  822. u32 pid;
  823. int i;
  824. if (np) {
  825. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  826. pdev->dev.platform_data = pdata;
  827. ret = fsmc_nand_probe_config_dt(pdev, np);
  828. if (ret) {
  829. dev_err(&pdev->dev, "no platform data\n");
  830. return -ENODEV;
  831. }
  832. }
  833. if (!pdata) {
  834. dev_err(&pdev->dev, "platform data is NULL\n");
  835. return -EINVAL;
  836. }
  837. /* Allocate memory for the device structure (and zero it) */
  838. host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
  839. if (!host) {
  840. dev_err(&pdev->dev, "failed to allocate device structure\n");
  841. return -ENOMEM;
  842. }
  843. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_data");
  844. if (!res)
  845. return -EINVAL;
  846. host->data_va = devm_ioremap_resource(&pdev->dev, res);
  847. if (IS_ERR(host->data_va))
  848. return PTR_ERR(host->data_va);
  849. host->data_pa = (dma_addr_t)res->start;
  850. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_addr");
  851. if (!res)
  852. return -EINVAL;
  853. host->addr_va = devm_ioremap_resource(&pdev->dev, res);
  854. if (IS_ERR(host->addr_va))
  855. return PTR_ERR(host->addr_va);
  856. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_cmd");
  857. if (!res)
  858. return -EINVAL;
  859. host->cmd_va = devm_ioremap_resource(&pdev->dev, res);
  860. if (IS_ERR(host->cmd_va))
  861. return PTR_ERR(host->cmd_va);
  862. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "fsmc_regs");
  863. if (!res)
  864. return -EINVAL;
  865. host->regs_va = devm_ioremap_resource(&pdev->dev, res);
  866. if (IS_ERR(host->regs_va))
  867. return PTR_ERR(host->regs_va);
  868. host->clk = clk_get(&pdev->dev, NULL);
  869. if (IS_ERR(host->clk)) {
  870. dev_err(&pdev->dev, "failed to fetch block clock\n");
  871. return PTR_ERR(host->clk);
  872. }
  873. ret = clk_prepare_enable(host->clk);
  874. if (ret)
  875. goto err_clk_prepare_enable;
  876. /*
  877. * This device ID is actually a common AMBA ID as used on the
  878. * AMBA PrimeCell bus. However it is not a PrimeCell.
  879. */
  880. for (pid = 0, i = 0; i < 4; i++)
  881. pid |= (readl(host->regs_va + resource_size(res) - 0x20 + 4 * i) & 255) << (i * 8);
  882. host->pid = pid;
  883. dev_info(&pdev->dev, "FSMC device partno %03x, manufacturer %02x, "
  884. "revision %02x, config %02x\n",
  885. AMBA_PART_BITS(pid), AMBA_MANF_BITS(pid),
  886. AMBA_REV_BITS(pid), AMBA_CONFIG_BITS(pid));
  887. host->bank = pdata->bank;
  888. host->select_chip = pdata->select_bank;
  889. host->partitions = pdata->partitions;
  890. host->nr_partitions = pdata->nr_partitions;
  891. host->dev = &pdev->dev;
  892. host->dev_timings = pdata->nand_timings;
  893. host->mode = pdata->mode;
  894. if (host->mode == USE_DMA_ACCESS)
  895. init_completion(&host->dma_access_complete);
  896. /* Link all private pointers */
  897. mtd = &host->mtd;
  898. nand = &host->nand;
  899. mtd->priv = nand;
  900. nand->priv = host;
  901. host->mtd.owner = THIS_MODULE;
  902. nand->IO_ADDR_R = host->data_va;
  903. nand->IO_ADDR_W = host->data_va;
  904. nand->cmd_ctrl = fsmc_cmd_ctrl;
  905. nand->chip_delay = 30;
  906. nand->ecc.mode = NAND_ECC_HW;
  907. nand->ecc.hwctl = fsmc_enable_hwecc;
  908. nand->ecc.size = 512;
  909. nand->options = pdata->options;
  910. nand->select_chip = fsmc_select_chip;
  911. nand->badblockbits = 7;
  912. if (pdata->width == FSMC_NAND_BW16)
  913. nand->options |= NAND_BUSWIDTH_16;
  914. switch (host->mode) {
  915. case USE_DMA_ACCESS:
  916. dma_cap_zero(mask);
  917. dma_cap_set(DMA_MEMCPY, mask);
  918. host->read_dma_chan = dma_request_channel(mask, filter,
  919. pdata->read_dma_priv);
  920. if (!host->read_dma_chan) {
  921. dev_err(&pdev->dev, "Unable to get read dma channel\n");
  922. goto err_req_read_chnl;
  923. }
  924. host->write_dma_chan = dma_request_channel(mask, filter,
  925. pdata->write_dma_priv);
  926. if (!host->write_dma_chan) {
  927. dev_err(&pdev->dev, "Unable to get write dma channel\n");
  928. goto err_req_write_chnl;
  929. }
  930. nand->read_buf = fsmc_read_buf_dma;
  931. nand->write_buf = fsmc_write_buf_dma;
  932. break;
  933. default:
  934. case USE_WORD_ACCESS:
  935. nand->read_buf = fsmc_read_buf;
  936. nand->write_buf = fsmc_write_buf;
  937. break;
  938. }
  939. fsmc_nand_setup(host->regs_va, host->bank,
  940. nand->options & NAND_BUSWIDTH_16,
  941. host->dev_timings);
  942. if (AMBA_REV_BITS(host->pid) >= 8) {
  943. nand->ecc.read_page = fsmc_read_page_hwecc;
  944. nand->ecc.calculate = fsmc_read_hwecc_ecc4;
  945. nand->ecc.correct = fsmc_bch8_correct_data;
  946. nand->ecc.bytes = 13;
  947. nand->ecc.strength = 8;
  948. } else {
  949. nand->ecc.calculate = fsmc_read_hwecc_ecc1;
  950. nand->ecc.correct = nand_correct_data;
  951. nand->ecc.bytes = 3;
  952. nand->ecc.strength = 1;
  953. }
  954. /*
  955. * Scan to find existence of the device
  956. */
  957. if (nand_scan_ident(&host->mtd, 1, NULL)) {
  958. ret = -ENXIO;
  959. dev_err(&pdev->dev, "No NAND Device found!\n");
  960. goto err_scan_ident;
  961. }
  962. if (AMBA_REV_BITS(host->pid) >= 8) {
  963. switch (host->mtd.oobsize) {
  964. case 16:
  965. nand->ecc.layout = &fsmc_ecc4_16_layout;
  966. host->ecc_place = &fsmc_ecc4_sp_place;
  967. break;
  968. case 64:
  969. nand->ecc.layout = &fsmc_ecc4_64_layout;
  970. host->ecc_place = &fsmc_ecc4_lp_place;
  971. break;
  972. case 128:
  973. nand->ecc.layout = &fsmc_ecc4_128_layout;
  974. host->ecc_place = &fsmc_ecc4_lp_place;
  975. break;
  976. case 224:
  977. nand->ecc.layout = &fsmc_ecc4_224_layout;
  978. host->ecc_place = &fsmc_ecc4_lp_place;
  979. break;
  980. case 256:
  981. nand->ecc.layout = &fsmc_ecc4_256_layout;
  982. host->ecc_place = &fsmc_ecc4_lp_place;
  983. break;
  984. default:
  985. printk(KERN_WARNING "No oob scheme defined for "
  986. "oobsize %d\n", mtd->oobsize);
  987. BUG();
  988. }
  989. } else {
  990. switch (host->mtd.oobsize) {
  991. case 16:
  992. nand->ecc.layout = &fsmc_ecc1_16_layout;
  993. break;
  994. case 64:
  995. nand->ecc.layout = &fsmc_ecc1_64_layout;
  996. break;
  997. case 128:
  998. nand->ecc.layout = &fsmc_ecc1_128_layout;
  999. break;
  1000. default:
  1001. printk(KERN_WARNING "No oob scheme defined for "
  1002. "oobsize %d\n", mtd->oobsize);
  1003. BUG();
  1004. }
  1005. }
  1006. /* Second stage of scan to fill MTD data-structures */
  1007. if (nand_scan_tail(&host->mtd)) {
  1008. ret = -ENXIO;
  1009. goto err_probe;
  1010. }
  1011. /*
  1012. * The partition information can is accessed by (in the same precedence)
  1013. *
  1014. * command line through Bootloader,
  1015. * platform data,
  1016. * default partition information present in driver.
  1017. */
  1018. /*
  1019. * Check for partition info passed
  1020. */
  1021. host->mtd.name = "nand";
  1022. ppdata.of_node = np;
  1023. ret = mtd_device_parse_register(&host->mtd, NULL, &ppdata,
  1024. host->partitions, host->nr_partitions);
  1025. if (ret)
  1026. goto err_probe;
  1027. platform_set_drvdata(pdev, host);
  1028. dev_info(&pdev->dev, "FSMC NAND driver registration successful\n");
  1029. return 0;
  1030. err_probe:
  1031. err_scan_ident:
  1032. if (host->mode == USE_DMA_ACCESS)
  1033. dma_release_channel(host->write_dma_chan);
  1034. err_req_write_chnl:
  1035. if (host->mode == USE_DMA_ACCESS)
  1036. dma_release_channel(host->read_dma_chan);
  1037. err_req_read_chnl:
  1038. clk_disable_unprepare(host->clk);
  1039. err_clk_prepare_enable:
  1040. clk_put(host->clk);
  1041. return ret;
  1042. }
  1043. /*
  1044. * Clean up routine
  1045. */
  1046. static int fsmc_nand_remove(struct platform_device *pdev)
  1047. {
  1048. struct fsmc_nand_data *host = platform_get_drvdata(pdev);
  1049. platform_set_drvdata(pdev, NULL);
  1050. if (host) {
  1051. nand_release(&host->mtd);
  1052. if (host->mode == USE_DMA_ACCESS) {
  1053. dma_release_channel(host->write_dma_chan);
  1054. dma_release_channel(host->read_dma_chan);
  1055. }
  1056. clk_disable_unprepare(host->clk);
  1057. clk_put(host->clk);
  1058. }
  1059. return 0;
  1060. }
  1061. #ifdef CONFIG_PM
  1062. static int fsmc_nand_suspend(struct device *dev)
  1063. {
  1064. struct fsmc_nand_data *host = dev_get_drvdata(dev);
  1065. if (host)
  1066. clk_disable_unprepare(host->clk);
  1067. return 0;
  1068. }
  1069. static int fsmc_nand_resume(struct device *dev)
  1070. {
  1071. struct fsmc_nand_data *host = dev_get_drvdata(dev);
  1072. if (host) {
  1073. clk_prepare_enable(host->clk);
  1074. fsmc_nand_setup(host->regs_va, host->bank,
  1075. host->nand.options & NAND_BUSWIDTH_16,
  1076. host->dev_timings);
  1077. }
  1078. return 0;
  1079. }
  1080. static SIMPLE_DEV_PM_OPS(fsmc_nand_pm_ops, fsmc_nand_suspend, fsmc_nand_resume);
  1081. #endif
  1082. #ifdef CONFIG_OF
  1083. static const struct of_device_id fsmc_nand_id_table[] = {
  1084. { .compatible = "st,spear600-fsmc-nand" },
  1085. { .compatible = "stericsson,fsmc-nand" },
  1086. {}
  1087. };
  1088. MODULE_DEVICE_TABLE(of, fsmc_nand_id_table);
  1089. #endif
  1090. static struct platform_driver fsmc_nand_driver = {
  1091. .remove = fsmc_nand_remove,
  1092. .driver = {
  1093. .owner = THIS_MODULE,
  1094. .name = "fsmc-nand",
  1095. .of_match_table = of_match_ptr(fsmc_nand_id_table),
  1096. #ifdef CONFIG_PM
  1097. .pm = &fsmc_nand_pm_ops,
  1098. #endif
  1099. },
  1100. };
  1101. module_platform_driver_probe(fsmc_nand_driver, fsmc_nand_probe);
  1102. MODULE_LICENSE("GPL");
  1103. MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>, Ashish Priyadarshi");
  1104. MODULE_DESCRIPTION("NAND driver for SPEAr Platforms");