fsmc_nand.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  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. dev_err(host->dev, "unsupported chip-select %d\n", chipnr);
  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. unsigned long time_left;
  515. if (direction == DMA_TO_DEVICE)
  516. chan = host->write_dma_chan;
  517. else if (direction == DMA_FROM_DEVICE)
  518. chan = host->read_dma_chan;
  519. else
  520. return -EINVAL;
  521. dma_dev = chan->device;
  522. dma_addr = dma_map_single(dma_dev->dev, buffer, len, direction);
  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. time_left =
  547. wait_for_completion_timeout(&host->dma_access_complete,
  548. msecs_to_jiffies(3000));
  549. if (time_left == 0) {
  550. dmaengine_terminate_all(chan);
  551. dev_err(host->dev, "wait_for_completion_timeout\n");
  552. ret = -ETIMEDOUT;
  553. goto unmap_dma;
  554. }
  555. ret = 0;
  556. unmap_dma:
  557. dma_unmap_single(dma_dev->dev, dma_addr, len, direction);
  558. return ret;
  559. }
  560. /*
  561. * fsmc_write_buf - write buffer to chip
  562. * @mtd: MTD device structure
  563. * @buf: data buffer
  564. * @len: number of bytes to write
  565. */
  566. static void fsmc_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  567. {
  568. int i;
  569. struct nand_chip *chip = mtd->priv;
  570. if (IS_ALIGNED((uint32_t)buf, sizeof(uint32_t)) &&
  571. IS_ALIGNED(len, sizeof(uint32_t))) {
  572. uint32_t *p = (uint32_t *)buf;
  573. len = len >> 2;
  574. for (i = 0; i < len; i++)
  575. writel_relaxed(p[i], chip->IO_ADDR_W);
  576. } else {
  577. for (i = 0; i < len; i++)
  578. writeb_relaxed(buf[i], chip->IO_ADDR_W);
  579. }
  580. }
  581. /*
  582. * fsmc_read_buf - read chip data into buffer
  583. * @mtd: MTD device structure
  584. * @buf: buffer to store date
  585. * @len: number of bytes to read
  586. */
  587. static void fsmc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  588. {
  589. int i;
  590. struct nand_chip *chip = mtd->priv;
  591. if (IS_ALIGNED((uint32_t)buf, sizeof(uint32_t)) &&
  592. IS_ALIGNED(len, sizeof(uint32_t))) {
  593. uint32_t *p = (uint32_t *)buf;
  594. len = len >> 2;
  595. for (i = 0; i < len; i++)
  596. p[i] = readl_relaxed(chip->IO_ADDR_R);
  597. } else {
  598. for (i = 0; i < len; i++)
  599. buf[i] = readb_relaxed(chip->IO_ADDR_R);
  600. }
  601. }
  602. /*
  603. * fsmc_read_buf_dma - read chip data into buffer
  604. * @mtd: MTD device structure
  605. * @buf: buffer to store date
  606. * @len: number of bytes to read
  607. */
  608. static void fsmc_read_buf_dma(struct mtd_info *mtd, uint8_t *buf, int len)
  609. {
  610. struct fsmc_nand_data *host;
  611. host = container_of(mtd, struct fsmc_nand_data, mtd);
  612. dma_xfer(host, buf, len, DMA_FROM_DEVICE);
  613. }
  614. /*
  615. * fsmc_write_buf_dma - write buffer to chip
  616. * @mtd: MTD device structure
  617. * @buf: data buffer
  618. * @len: number of bytes to write
  619. */
  620. static void fsmc_write_buf_dma(struct mtd_info *mtd, const uint8_t *buf,
  621. int len)
  622. {
  623. struct fsmc_nand_data *host;
  624. host = container_of(mtd, struct fsmc_nand_data, mtd);
  625. dma_xfer(host, (void *)buf, len, DMA_TO_DEVICE);
  626. }
  627. /*
  628. * fsmc_read_page_hwecc
  629. * @mtd: mtd info structure
  630. * @chip: nand chip info structure
  631. * @buf: buffer to store read data
  632. * @oob_required: caller expects OOB data read to chip->oob_poi
  633. * @page: page number to read
  634. *
  635. * This routine is needed for fsmc version 8 as reading from NAND chip has to be
  636. * performed in a strict sequence as follows:
  637. * data(512 byte) -> ecc(13 byte)
  638. * After this read, fsmc hardware generates and reports error data bits(up to a
  639. * max of 8 bits)
  640. */
  641. static int fsmc_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  642. uint8_t *buf, int oob_required, int page)
  643. {
  644. struct fsmc_nand_data *host = container_of(mtd,
  645. struct fsmc_nand_data, mtd);
  646. struct fsmc_eccplace *ecc_place = host->ecc_place;
  647. int i, j, s, stat, eccsize = chip->ecc.size;
  648. int eccbytes = chip->ecc.bytes;
  649. int eccsteps = chip->ecc.steps;
  650. uint8_t *p = buf;
  651. uint8_t *ecc_calc = chip->buffers->ecccalc;
  652. uint8_t *ecc_code = chip->buffers->ecccode;
  653. int off, len, group = 0;
  654. /*
  655. * ecc_oob is intentionally taken as uint16_t. In 16bit devices, we
  656. * end up reading 14 bytes (7 words) from oob. The local array is
  657. * to maintain word alignment
  658. */
  659. uint16_t ecc_oob[7];
  660. uint8_t *oob = (uint8_t *)&ecc_oob[0];
  661. unsigned int max_bitflips = 0;
  662. for (i = 0, s = 0; s < eccsteps; s++, i += eccbytes, p += eccsize) {
  663. chip->cmdfunc(mtd, NAND_CMD_READ0, s * eccsize, page);
  664. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  665. chip->read_buf(mtd, p, eccsize);
  666. for (j = 0; j < eccbytes;) {
  667. off = ecc_place->eccplace[group].offset;
  668. len = ecc_place->eccplace[group].length;
  669. group++;
  670. /*
  671. * length is intentionally kept a higher multiple of 2
  672. * to read at least 13 bytes even in case of 16 bit NAND
  673. * devices
  674. */
  675. if (chip->options & NAND_BUSWIDTH_16)
  676. len = roundup(len, 2);
  677. chip->cmdfunc(mtd, NAND_CMD_READOOB, off, page);
  678. chip->read_buf(mtd, oob + j, len);
  679. j += len;
  680. }
  681. memcpy(&ecc_code[i], oob, chip->ecc.bytes);
  682. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  683. stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  684. if (stat < 0) {
  685. mtd->ecc_stats.failed++;
  686. } else {
  687. mtd->ecc_stats.corrected += stat;
  688. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  689. }
  690. }
  691. return max_bitflips;
  692. }
  693. /*
  694. * fsmc_bch8_correct_data
  695. * @mtd: mtd info structure
  696. * @dat: buffer of read data
  697. * @read_ecc: ecc read from device spare area
  698. * @calc_ecc: ecc calculated from read data
  699. *
  700. * calc_ecc is a 104 bit information containing maximum of 8 error
  701. * offset informations of 13 bits each in 512 bytes of read data.
  702. */
  703. static int fsmc_bch8_correct_data(struct mtd_info *mtd, uint8_t *dat,
  704. uint8_t *read_ecc, uint8_t *calc_ecc)
  705. {
  706. struct fsmc_nand_data *host = container_of(mtd,
  707. struct fsmc_nand_data, mtd);
  708. struct nand_chip *chip = mtd->priv;
  709. void __iomem *regs = host->regs_va;
  710. unsigned int bank = host->bank;
  711. uint32_t err_idx[8];
  712. uint32_t num_err, i;
  713. uint32_t ecc1, ecc2, ecc3, ecc4;
  714. num_err = (readl_relaxed(FSMC_NAND_REG(regs, bank, STS)) >> 10) & 0xF;
  715. /* no bit flipping */
  716. if (likely(num_err == 0))
  717. return 0;
  718. /* too many errors */
  719. if (unlikely(num_err > 8)) {
  720. /*
  721. * This is a temporary erase check. A newly erased page read
  722. * would result in an ecc error because the oob data is also
  723. * erased to FF and the calculated ecc for an FF data is not
  724. * FF..FF.
  725. * This is a workaround to skip performing correction in case
  726. * data is FF..FF
  727. *
  728. * Logic:
  729. * For every page, each bit written as 0 is counted until these
  730. * number of bits are greater than 8 (the maximum correction
  731. * capability of FSMC for each 512 + 13 bytes)
  732. */
  733. int bits_ecc = count_written_bits(read_ecc, chip->ecc.bytes, 8);
  734. int bits_data = count_written_bits(dat, chip->ecc.size, 8);
  735. if ((bits_ecc + bits_data) <= 8) {
  736. if (bits_data)
  737. memset(dat, 0xff, chip->ecc.size);
  738. return bits_data;
  739. }
  740. return -EBADMSG;
  741. }
  742. /*
  743. * ------------------- calc_ecc[] bit wise -----------|--13 bits--|
  744. * |---idx[7]--|--.....-----|---idx[2]--||---idx[1]--||---idx[0]--|
  745. *
  746. * calc_ecc is a 104 bit information containing maximum of 8 error
  747. * offset informations of 13 bits each. calc_ecc is copied into a
  748. * uint64_t array and error offset indexes are populated in err_idx
  749. * array
  750. */
  751. ecc1 = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC1));
  752. ecc2 = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC2));
  753. ecc3 = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC3));
  754. ecc4 = readl_relaxed(FSMC_NAND_REG(regs, bank, STS));
  755. err_idx[0] = (ecc1 >> 0) & 0x1FFF;
  756. err_idx[1] = (ecc1 >> 13) & 0x1FFF;
  757. err_idx[2] = (((ecc2 >> 0) & 0x7F) << 6) | ((ecc1 >> 26) & 0x3F);
  758. err_idx[3] = (ecc2 >> 7) & 0x1FFF;
  759. err_idx[4] = (((ecc3 >> 0) & 0x1) << 12) | ((ecc2 >> 20) & 0xFFF);
  760. err_idx[5] = (ecc3 >> 1) & 0x1FFF;
  761. err_idx[6] = (ecc3 >> 14) & 0x1FFF;
  762. err_idx[7] = (((ecc4 >> 16) & 0xFF) << 5) | ((ecc3 >> 27) & 0x1F);
  763. i = 0;
  764. while (num_err--) {
  765. change_bit(0, (unsigned long *)&err_idx[i]);
  766. change_bit(1, (unsigned long *)&err_idx[i]);
  767. if (err_idx[i] < chip->ecc.size * 8) {
  768. change_bit(err_idx[i], (unsigned long *)dat);
  769. i++;
  770. }
  771. }
  772. return i;
  773. }
  774. static bool filter(struct dma_chan *chan, void *slave)
  775. {
  776. chan->private = slave;
  777. return true;
  778. }
  779. #ifdef CONFIG_OF
  780. static int fsmc_nand_probe_config_dt(struct platform_device *pdev,
  781. struct device_node *np)
  782. {
  783. struct fsmc_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
  784. u32 val;
  785. int ret;
  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. return -ENOMEM;
  802. ret = of_property_read_u8_array(np, "timings", (u8 *)pdata->nand_timings,
  803. sizeof(*pdata->nand_timings));
  804. if (ret) {
  805. dev_info(&pdev->dev, "No timings in dts specified, using default timings!\n");
  806. pdata->nand_timings = NULL;
  807. }
  808. /* Set default NAND bank to 0 */
  809. pdata->bank = 0;
  810. if (!of_property_read_u32(np, "bank", &val)) {
  811. if (val > 3) {
  812. dev_err(&pdev->dev, "invalid bank %u\n", val);
  813. return -EINVAL;
  814. }
  815. pdata->bank = val;
  816. }
  817. return 0;
  818. }
  819. #else
  820. static int fsmc_nand_probe_config_dt(struct platform_device *pdev,
  821. struct device_node *np)
  822. {
  823. return -ENOSYS;
  824. }
  825. #endif
  826. /*
  827. * fsmc_nand_probe - Probe function
  828. * @pdev: platform device structure
  829. */
  830. static int __init fsmc_nand_probe(struct platform_device *pdev)
  831. {
  832. struct fsmc_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
  833. struct device_node __maybe_unused *np = pdev->dev.of_node;
  834. struct mtd_part_parser_data ppdata = {};
  835. struct fsmc_nand_data *host;
  836. struct mtd_info *mtd;
  837. struct nand_chip *nand;
  838. struct resource *res;
  839. dma_cap_mask_t mask;
  840. int ret = 0;
  841. u32 pid;
  842. int i;
  843. if (np) {
  844. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  845. pdev->dev.platform_data = pdata;
  846. ret = fsmc_nand_probe_config_dt(pdev, np);
  847. if (ret) {
  848. dev_err(&pdev->dev, "no platform data\n");
  849. return -ENODEV;
  850. }
  851. }
  852. if (!pdata) {
  853. dev_err(&pdev->dev, "platform data is NULL\n");
  854. return -EINVAL;
  855. }
  856. /* Allocate memory for the device structure (and zero it) */
  857. host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
  858. if (!host)
  859. return -ENOMEM;
  860. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_data");
  861. host->data_va = devm_ioremap_resource(&pdev->dev, res);
  862. if (IS_ERR(host->data_va))
  863. return PTR_ERR(host->data_va);
  864. host->data_pa = (dma_addr_t)res->start;
  865. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_addr");
  866. host->addr_va = devm_ioremap_resource(&pdev->dev, res);
  867. if (IS_ERR(host->addr_va))
  868. return PTR_ERR(host->addr_va);
  869. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_cmd");
  870. host->cmd_va = devm_ioremap_resource(&pdev->dev, res);
  871. if (IS_ERR(host->cmd_va))
  872. return PTR_ERR(host->cmd_va);
  873. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "fsmc_regs");
  874. host->regs_va = devm_ioremap_resource(&pdev->dev, res);
  875. if (IS_ERR(host->regs_va))
  876. return PTR_ERR(host->regs_va);
  877. host->clk = clk_get(&pdev->dev, NULL);
  878. if (IS_ERR(host->clk)) {
  879. dev_err(&pdev->dev, "failed to fetch block clock\n");
  880. return PTR_ERR(host->clk);
  881. }
  882. ret = clk_prepare_enable(host->clk);
  883. if (ret)
  884. goto err_clk_prepare_enable;
  885. /*
  886. * This device ID is actually a common AMBA ID as used on the
  887. * AMBA PrimeCell bus. However it is not a PrimeCell.
  888. */
  889. for (pid = 0, i = 0; i < 4; i++)
  890. pid |= (readl(host->regs_va + resource_size(res) - 0x20 + 4 * i) & 255) << (i * 8);
  891. host->pid = pid;
  892. dev_info(&pdev->dev, "FSMC device partno %03x, manufacturer %02x, "
  893. "revision %02x, config %02x\n",
  894. AMBA_PART_BITS(pid), AMBA_MANF_BITS(pid),
  895. AMBA_REV_BITS(pid), AMBA_CONFIG_BITS(pid));
  896. host->bank = pdata->bank;
  897. host->select_chip = pdata->select_bank;
  898. host->partitions = pdata->partitions;
  899. host->nr_partitions = pdata->nr_partitions;
  900. host->dev = &pdev->dev;
  901. host->dev_timings = pdata->nand_timings;
  902. host->mode = pdata->mode;
  903. if (host->mode == USE_DMA_ACCESS)
  904. init_completion(&host->dma_access_complete);
  905. /* Link all private pointers */
  906. mtd = &host->mtd;
  907. nand = &host->nand;
  908. mtd->priv = nand;
  909. nand->priv = host;
  910. host->mtd.dev.parent = &pdev->dev;
  911. nand->IO_ADDR_R = host->data_va;
  912. nand->IO_ADDR_W = host->data_va;
  913. nand->cmd_ctrl = fsmc_cmd_ctrl;
  914. nand->chip_delay = 30;
  915. /*
  916. * Setup default ECC mode. nand_dt_init() called from nand_scan_ident()
  917. * can overwrite this value if the DT provides a different value.
  918. */
  919. nand->ecc.mode = NAND_ECC_HW;
  920. nand->ecc.hwctl = fsmc_enable_hwecc;
  921. nand->ecc.size = 512;
  922. nand->options = pdata->options;
  923. nand->select_chip = fsmc_select_chip;
  924. nand->badblockbits = 7;
  925. nand->flash_node = np;
  926. if (pdata->width == FSMC_NAND_BW16)
  927. nand->options |= NAND_BUSWIDTH_16;
  928. switch (host->mode) {
  929. case USE_DMA_ACCESS:
  930. dma_cap_zero(mask);
  931. dma_cap_set(DMA_MEMCPY, mask);
  932. host->read_dma_chan = dma_request_channel(mask, filter,
  933. pdata->read_dma_priv);
  934. if (!host->read_dma_chan) {
  935. dev_err(&pdev->dev, "Unable to get read dma channel\n");
  936. goto err_req_read_chnl;
  937. }
  938. host->write_dma_chan = dma_request_channel(mask, filter,
  939. pdata->write_dma_priv);
  940. if (!host->write_dma_chan) {
  941. dev_err(&pdev->dev, "Unable to get write dma channel\n");
  942. goto err_req_write_chnl;
  943. }
  944. nand->read_buf = fsmc_read_buf_dma;
  945. nand->write_buf = fsmc_write_buf_dma;
  946. break;
  947. default:
  948. case USE_WORD_ACCESS:
  949. nand->read_buf = fsmc_read_buf;
  950. nand->write_buf = fsmc_write_buf;
  951. break;
  952. }
  953. fsmc_nand_setup(host->regs_va, host->bank,
  954. nand->options & NAND_BUSWIDTH_16,
  955. host->dev_timings);
  956. if (AMBA_REV_BITS(host->pid) >= 8) {
  957. nand->ecc.read_page = fsmc_read_page_hwecc;
  958. nand->ecc.calculate = fsmc_read_hwecc_ecc4;
  959. nand->ecc.correct = fsmc_bch8_correct_data;
  960. nand->ecc.bytes = 13;
  961. nand->ecc.strength = 8;
  962. }
  963. /*
  964. * Scan to find existence of the device
  965. */
  966. if (nand_scan_ident(&host->mtd, 1, NULL)) {
  967. ret = -ENXIO;
  968. dev_err(&pdev->dev, "No NAND Device found!\n");
  969. goto err_scan_ident;
  970. }
  971. if (AMBA_REV_BITS(host->pid) >= 8) {
  972. switch (host->mtd.oobsize) {
  973. case 16:
  974. nand->ecc.layout = &fsmc_ecc4_16_layout;
  975. host->ecc_place = &fsmc_ecc4_sp_place;
  976. break;
  977. case 64:
  978. nand->ecc.layout = &fsmc_ecc4_64_layout;
  979. host->ecc_place = &fsmc_ecc4_lp_place;
  980. break;
  981. case 128:
  982. nand->ecc.layout = &fsmc_ecc4_128_layout;
  983. host->ecc_place = &fsmc_ecc4_lp_place;
  984. break;
  985. case 224:
  986. nand->ecc.layout = &fsmc_ecc4_224_layout;
  987. host->ecc_place = &fsmc_ecc4_lp_place;
  988. break;
  989. case 256:
  990. nand->ecc.layout = &fsmc_ecc4_256_layout;
  991. host->ecc_place = &fsmc_ecc4_lp_place;
  992. break;
  993. default:
  994. dev_warn(&pdev->dev, "No oob scheme defined for oobsize %d\n",
  995. mtd->oobsize);
  996. ret = -EINVAL;
  997. goto err_probe;
  998. }
  999. } else {
  1000. switch (nand->ecc.mode) {
  1001. case NAND_ECC_HW:
  1002. dev_info(&pdev->dev, "Using 1-bit HW ECC scheme\n");
  1003. nand->ecc.calculate = fsmc_read_hwecc_ecc1;
  1004. nand->ecc.correct = nand_correct_data;
  1005. nand->ecc.bytes = 3;
  1006. nand->ecc.strength = 1;
  1007. break;
  1008. case NAND_ECC_SOFT_BCH:
  1009. dev_info(&pdev->dev, "Using 4-bit SW BCH ECC scheme\n");
  1010. break;
  1011. default:
  1012. dev_err(&pdev->dev, "Unsupported ECC mode!\n");
  1013. goto err_probe;
  1014. }
  1015. /*
  1016. * Don't set layout for BCH4 SW ECC. This will be
  1017. * generated later in nand_bch_init() later.
  1018. */
  1019. if (nand->ecc.mode != NAND_ECC_SOFT_BCH) {
  1020. switch (host->mtd.oobsize) {
  1021. case 16:
  1022. nand->ecc.layout = &fsmc_ecc1_16_layout;
  1023. break;
  1024. case 64:
  1025. nand->ecc.layout = &fsmc_ecc1_64_layout;
  1026. break;
  1027. case 128:
  1028. nand->ecc.layout = &fsmc_ecc1_128_layout;
  1029. break;
  1030. default:
  1031. dev_warn(&pdev->dev,
  1032. "No oob scheme defined for oobsize %d\n",
  1033. mtd->oobsize);
  1034. ret = -EINVAL;
  1035. goto err_probe;
  1036. }
  1037. }
  1038. }
  1039. /* Second stage of scan to fill MTD data-structures */
  1040. if (nand_scan_tail(&host->mtd)) {
  1041. ret = -ENXIO;
  1042. goto err_probe;
  1043. }
  1044. /*
  1045. * The partition information can is accessed by (in the same precedence)
  1046. *
  1047. * command line through Bootloader,
  1048. * platform data,
  1049. * default partition information present in driver.
  1050. */
  1051. /*
  1052. * Check for partition info passed
  1053. */
  1054. host->mtd.name = "nand";
  1055. ppdata.of_node = np;
  1056. ret = mtd_device_parse_register(&host->mtd, NULL, &ppdata,
  1057. host->partitions, host->nr_partitions);
  1058. if (ret)
  1059. goto err_probe;
  1060. platform_set_drvdata(pdev, host);
  1061. dev_info(&pdev->dev, "FSMC NAND driver registration successful\n");
  1062. return 0;
  1063. err_probe:
  1064. err_scan_ident:
  1065. if (host->mode == USE_DMA_ACCESS)
  1066. dma_release_channel(host->write_dma_chan);
  1067. err_req_write_chnl:
  1068. if (host->mode == USE_DMA_ACCESS)
  1069. dma_release_channel(host->read_dma_chan);
  1070. err_req_read_chnl:
  1071. clk_disable_unprepare(host->clk);
  1072. err_clk_prepare_enable:
  1073. clk_put(host->clk);
  1074. return ret;
  1075. }
  1076. /*
  1077. * Clean up routine
  1078. */
  1079. static int fsmc_nand_remove(struct platform_device *pdev)
  1080. {
  1081. struct fsmc_nand_data *host = platform_get_drvdata(pdev);
  1082. if (host) {
  1083. nand_release(&host->mtd);
  1084. if (host->mode == USE_DMA_ACCESS) {
  1085. dma_release_channel(host->write_dma_chan);
  1086. dma_release_channel(host->read_dma_chan);
  1087. }
  1088. clk_disable_unprepare(host->clk);
  1089. clk_put(host->clk);
  1090. }
  1091. return 0;
  1092. }
  1093. #ifdef CONFIG_PM_SLEEP
  1094. static int fsmc_nand_suspend(struct device *dev)
  1095. {
  1096. struct fsmc_nand_data *host = dev_get_drvdata(dev);
  1097. if (host)
  1098. clk_disable_unprepare(host->clk);
  1099. return 0;
  1100. }
  1101. static int fsmc_nand_resume(struct device *dev)
  1102. {
  1103. struct fsmc_nand_data *host = dev_get_drvdata(dev);
  1104. if (host) {
  1105. clk_prepare_enable(host->clk);
  1106. fsmc_nand_setup(host->regs_va, host->bank,
  1107. host->nand.options & NAND_BUSWIDTH_16,
  1108. host->dev_timings);
  1109. }
  1110. return 0;
  1111. }
  1112. #endif
  1113. static SIMPLE_DEV_PM_OPS(fsmc_nand_pm_ops, fsmc_nand_suspend, fsmc_nand_resume);
  1114. #ifdef CONFIG_OF
  1115. static const struct of_device_id fsmc_nand_id_table[] = {
  1116. { .compatible = "st,spear600-fsmc-nand" },
  1117. { .compatible = "stericsson,fsmc-nand" },
  1118. {}
  1119. };
  1120. MODULE_DEVICE_TABLE(of, fsmc_nand_id_table);
  1121. #endif
  1122. static struct platform_driver fsmc_nand_driver = {
  1123. .remove = fsmc_nand_remove,
  1124. .driver = {
  1125. .name = "fsmc-nand",
  1126. .of_match_table = of_match_ptr(fsmc_nand_id_table),
  1127. .pm = &fsmc_nand_pm_ops,
  1128. },
  1129. };
  1130. module_platform_driver_probe(fsmc_nand_driver, fsmc_nand_probe);
  1131. MODULE_LICENSE("GPL");
  1132. MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>, Ashish Priyadarshi");
  1133. MODULE_DESCRIPTION("NAND driver for SPEAr Platforms");