fsmc_nand.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262
  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 nand_chip nand;
  291. struct mtd_partition *partitions;
  292. unsigned int nr_partitions;
  293. struct fsmc_eccplace *ecc_place;
  294. unsigned int bank;
  295. struct device *dev;
  296. enum access_mode mode;
  297. struct clk *clk;
  298. /* DMA related objects */
  299. struct dma_chan *read_dma_chan;
  300. struct dma_chan *write_dma_chan;
  301. struct completion dma_access_complete;
  302. struct fsmc_nand_timings *dev_timings;
  303. dma_addr_t data_pa;
  304. void __iomem *data_va;
  305. void __iomem *cmd_va;
  306. void __iomem *addr_va;
  307. void __iomem *regs_va;
  308. void (*select_chip)(uint32_t bank, uint32_t busw);
  309. };
  310. static inline struct fsmc_nand_data *mtd_to_fsmc(struct mtd_info *mtd)
  311. {
  312. return container_of(mtd_to_nand(mtd), struct fsmc_nand_data, nand);
  313. }
  314. /* Assert CS signal based on chipnr */
  315. static void fsmc_select_chip(struct mtd_info *mtd, int chipnr)
  316. {
  317. struct nand_chip *chip = mtd_to_nand(mtd);
  318. struct fsmc_nand_data *host;
  319. host = mtd_to_fsmc(mtd);
  320. switch (chipnr) {
  321. case -1:
  322. chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
  323. break;
  324. case 0:
  325. case 1:
  326. case 2:
  327. case 3:
  328. if (host->select_chip)
  329. host->select_chip(chipnr,
  330. chip->options & NAND_BUSWIDTH_16);
  331. break;
  332. default:
  333. dev_err(host->dev, "unsupported chip-select %d\n", chipnr);
  334. }
  335. }
  336. /*
  337. * fsmc_cmd_ctrl - For facilitaing Hardware access
  338. * This routine allows hardware specific access to control-lines(ALE,CLE)
  339. */
  340. static void fsmc_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  341. {
  342. struct nand_chip *this = mtd_to_nand(mtd);
  343. struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
  344. void __iomem *regs = host->regs_va;
  345. unsigned int bank = host->bank;
  346. if (ctrl & NAND_CTRL_CHANGE) {
  347. u32 pc;
  348. if (ctrl & NAND_CLE) {
  349. this->IO_ADDR_R = host->cmd_va;
  350. this->IO_ADDR_W = host->cmd_va;
  351. } else if (ctrl & NAND_ALE) {
  352. this->IO_ADDR_R = host->addr_va;
  353. this->IO_ADDR_W = host->addr_va;
  354. } else {
  355. this->IO_ADDR_R = host->data_va;
  356. this->IO_ADDR_W = host->data_va;
  357. }
  358. pc = readl(FSMC_NAND_REG(regs, bank, PC));
  359. if (ctrl & NAND_NCE)
  360. pc |= FSMC_ENABLE;
  361. else
  362. pc &= ~FSMC_ENABLE;
  363. writel_relaxed(pc, FSMC_NAND_REG(regs, bank, PC));
  364. }
  365. mb();
  366. if (cmd != NAND_CMD_NONE)
  367. writeb_relaxed(cmd, this->IO_ADDR_W);
  368. }
  369. /*
  370. * fsmc_nand_setup - FSMC (Flexible Static Memory Controller) init routine
  371. *
  372. * This routine initializes timing parameters related to NAND memory access in
  373. * FSMC registers
  374. */
  375. static void fsmc_nand_setup(void __iomem *regs, uint32_t bank,
  376. uint32_t busw, struct fsmc_nand_timings *timings)
  377. {
  378. uint32_t value = FSMC_DEVTYPE_NAND | FSMC_ENABLE | FSMC_WAITON;
  379. uint32_t tclr, tar, thiz, thold, twait, tset;
  380. struct fsmc_nand_timings *tims;
  381. struct fsmc_nand_timings default_timings = {
  382. .tclr = FSMC_TCLR_1,
  383. .tar = FSMC_TAR_1,
  384. .thiz = FSMC_THIZ_1,
  385. .thold = FSMC_THOLD_4,
  386. .twait = FSMC_TWAIT_6,
  387. .tset = FSMC_TSET_0,
  388. };
  389. if (timings)
  390. tims = timings;
  391. else
  392. tims = &default_timings;
  393. tclr = (tims->tclr & FSMC_TCLR_MASK) << FSMC_TCLR_SHIFT;
  394. tar = (tims->tar & FSMC_TAR_MASK) << FSMC_TAR_SHIFT;
  395. thiz = (tims->thiz & FSMC_THIZ_MASK) << FSMC_THIZ_SHIFT;
  396. thold = (tims->thold & FSMC_THOLD_MASK) << FSMC_THOLD_SHIFT;
  397. twait = (tims->twait & FSMC_TWAIT_MASK) << FSMC_TWAIT_SHIFT;
  398. tset = (tims->tset & FSMC_TSET_MASK) << FSMC_TSET_SHIFT;
  399. if (busw)
  400. writel_relaxed(value | FSMC_DEVWID_16,
  401. FSMC_NAND_REG(regs, bank, PC));
  402. else
  403. writel_relaxed(value | FSMC_DEVWID_8,
  404. FSMC_NAND_REG(regs, bank, PC));
  405. writel_relaxed(readl(FSMC_NAND_REG(regs, bank, PC)) | tclr | tar,
  406. FSMC_NAND_REG(regs, bank, PC));
  407. writel_relaxed(thiz | thold | twait | tset,
  408. FSMC_NAND_REG(regs, bank, COMM));
  409. writel_relaxed(thiz | thold | twait | tset,
  410. FSMC_NAND_REG(regs, bank, ATTRIB));
  411. }
  412. /*
  413. * fsmc_enable_hwecc - Enables Hardware ECC through FSMC registers
  414. */
  415. static void fsmc_enable_hwecc(struct mtd_info *mtd, int mode)
  416. {
  417. struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
  418. void __iomem *regs = host->regs_va;
  419. uint32_t bank = host->bank;
  420. writel_relaxed(readl(FSMC_NAND_REG(regs, bank, PC)) & ~FSMC_ECCPLEN_256,
  421. FSMC_NAND_REG(regs, bank, PC));
  422. writel_relaxed(readl(FSMC_NAND_REG(regs, bank, PC)) & ~FSMC_ECCEN,
  423. FSMC_NAND_REG(regs, bank, PC));
  424. writel_relaxed(readl(FSMC_NAND_REG(regs, bank, PC)) | FSMC_ECCEN,
  425. FSMC_NAND_REG(regs, bank, PC));
  426. }
  427. /*
  428. * fsmc_read_hwecc_ecc4 - Hardware ECC calculator for ecc4 option supported by
  429. * FSMC. ECC is 13 bytes for 512 bytes of data (supports error correction up to
  430. * max of 8-bits)
  431. */
  432. static int fsmc_read_hwecc_ecc4(struct mtd_info *mtd, const uint8_t *data,
  433. uint8_t *ecc)
  434. {
  435. struct fsmc_nand_data *host = mtd_to_fsmc(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 = mtd_to_fsmc(mtd);
  478. void __iomem *regs = host->regs_va;
  479. uint32_t bank = host->bank;
  480. uint32_t ecc_tmp;
  481. ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC1));
  482. ecc[0] = (uint8_t) (ecc_tmp >> 0);
  483. ecc[1] = (uint8_t) (ecc_tmp >> 8);
  484. ecc[2] = (uint8_t) (ecc_tmp >> 16);
  485. return 0;
  486. }
  487. /* Count the number of 0's in buff upto a max of max_bits */
  488. static int count_written_bits(uint8_t *buff, int size, int max_bits)
  489. {
  490. int k, written_bits = 0;
  491. for (k = 0; k < size; k++) {
  492. written_bits += hweight8(~buff[k]);
  493. if (written_bits > max_bits)
  494. break;
  495. }
  496. return written_bits;
  497. }
  498. static void dma_complete(void *param)
  499. {
  500. struct fsmc_nand_data *host = param;
  501. complete(&host->dma_access_complete);
  502. }
  503. static int dma_xfer(struct fsmc_nand_data *host, void *buffer, int len,
  504. enum dma_data_direction direction)
  505. {
  506. struct dma_chan *chan;
  507. struct dma_device *dma_dev;
  508. struct dma_async_tx_descriptor *tx;
  509. dma_addr_t dma_dst, dma_src, dma_addr;
  510. dma_cookie_t cookie;
  511. unsigned long flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
  512. int ret;
  513. unsigned long time_left;
  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. if (direction == DMA_TO_DEVICE) {
  523. dma_src = dma_addr;
  524. dma_dst = host->data_pa;
  525. } else {
  526. dma_src = host->data_pa;
  527. dma_dst = dma_addr;
  528. }
  529. tx = dma_dev->device_prep_dma_memcpy(chan, dma_dst, dma_src,
  530. len, flags);
  531. if (!tx) {
  532. dev_err(host->dev, "device_prep_dma_memcpy error\n");
  533. ret = -EIO;
  534. goto unmap_dma;
  535. }
  536. tx->callback = dma_complete;
  537. tx->callback_param = host;
  538. cookie = tx->tx_submit(tx);
  539. ret = dma_submit_error(cookie);
  540. if (ret) {
  541. dev_err(host->dev, "dma_submit_error %d\n", cookie);
  542. goto unmap_dma;
  543. }
  544. dma_async_issue_pending(chan);
  545. time_left =
  546. wait_for_completion_timeout(&host->dma_access_complete,
  547. msecs_to_jiffies(3000));
  548. if (time_left == 0) {
  549. dmaengine_terminate_all(chan);
  550. dev_err(host->dev, "wait_for_completion_timeout\n");
  551. ret = -ETIMEDOUT;
  552. goto unmap_dma;
  553. }
  554. ret = 0;
  555. unmap_dma:
  556. dma_unmap_single(dma_dev->dev, dma_addr, len, direction);
  557. return ret;
  558. }
  559. /*
  560. * fsmc_write_buf - write buffer to chip
  561. * @mtd: MTD device structure
  562. * @buf: data buffer
  563. * @len: number of bytes to write
  564. */
  565. static void fsmc_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  566. {
  567. int i;
  568. struct nand_chip *chip = mtd_to_nand(mtd);
  569. if (IS_ALIGNED((uint32_t)buf, sizeof(uint32_t)) &&
  570. IS_ALIGNED(len, sizeof(uint32_t))) {
  571. uint32_t *p = (uint32_t *)buf;
  572. len = len >> 2;
  573. for (i = 0; i < len; i++)
  574. writel_relaxed(p[i], chip->IO_ADDR_W);
  575. } else {
  576. for (i = 0; i < len; i++)
  577. writeb_relaxed(buf[i], chip->IO_ADDR_W);
  578. }
  579. }
  580. /*
  581. * fsmc_read_buf - read chip data into buffer
  582. * @mtd: MTD device structure
  583. * @buf: buffer to store date
  584. * @len: number of bytes to read
  585. */
  586. static void fsmc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  587. {
  588. int i;
  589. struct nand_chip *chip = mtd_to_nand(mtd);
  590. if (IS_ALIGNED((uint32_t)buf, sizeof(uint32_t)) &&
  591. IS_ALIGNED(len, sizeof(uint32_t))) {
  592. uint32_t *p = (uint32_t *)buf;
  593. len = len >> 2;
  594. for (i = 0; i < len; i++)
  595. p[i] = readl_relaxed(chip->IO_ADDR_R);
  596. } else {
  597. for (i = 0; i < len; i++)
  598. buf[i] = readb_relaxed(chip->IO_ADDR_R);
  599. }
  600. }
  601. /*
  602. * fsmc_read_buf_dma - read chip data into buffer
  603. * @mtd: MTD device structure
  604. * @buf: buffer to store date
  605. * @len: number of bytes to read
  606. */
  607. static void fsmc_read_buf_dma(struct mtd_info *mtd, uint8_t *buf, int len)
  608. {
  609. struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
  610. dma_xfer(host, buf, len, DMA_FROM_DEVICE);
  611. }
  612. /*
  613. * fsmc_write_buf_dma - write buffer to chip
  614. * @mtd: MTD device structure
  615. * @buf: data buffer
  616. * @len: number of bytes to write
  617. */
  618. static void fsmc_write_buf_dma(struct mtd_info *mtd, const uint8_t *buf,
  619. int len)
  620. {
  621. struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
  622. dma_xfer(host, (void *)buf, len, DMA_TO_DEVICE);
  623. }
  624. /*
  625. * fsmc_read_page_hwecc
  626. * @mtd: mtd info structure
  627. * @chip: nand chip info structure
  628. * @buf: buffer to store read data
  629. * @oob_required: caller expects OOB data read to chip->oob_poi
  630. * @page: page number to read
  631. *
  632. * This routine is needed for fsmc version 8 as reading from NAND chip has to be
  633. * performed in a strict sequence as follows:
  634. * data(512 byte) -> ecc(13 byte)
  635. * After this read, fsmc hardware generates and reports error data bits(up to a
  636. * max of 8 bits)
  637. */
  638. static int fsmc_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  639. uint8_t *buf, int oob_required, int page)
  640. {
  641. struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
  642. struct fsmc_eccplace *ecc_place = host->ecc_place;
  643. int i, j, s, stat, eccsize = chip->ecc.size;
  644. int eccbytes = chip->ecc.bytes;
  645. int eccsteps = chip->ecc.steps;
  646. uint8_t *p = buf;
  647. uint8_t *ecc_calc = chip->buffers->ecccalc;
  648. uint8_t *ecc_code = chip->buffers->ecccode;
  649. int off, len, group = 0;
  650. /*
  651. * ecc_oob is intentionally taken as uint16_t. In 16bit devices, we
  652. * end up reading 14 bytes (7 words) from oob. The local array is
  653. * to maintain word alignment
  654. */
  655. uint16_t ecc_oob[7];
  656. uint8_t *oob = (uint8_t *)&ecc_oob[0];
  657. unsigned int max_bitflips = 0;
  658. for (i = 0, s = 0; s < eccsteps; s++, i += eccbytes, p += eccsize) {
  659. chip->cmdfunc(mtd, NAND_CMD_READ0, s * eccsize, page);
  660. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  661. chip->read_buf(mtd, p, eccsize);
  662. for (j = 0; j < eccbytes;) {
  663. off = ecc_place->eccplace[group].offset;
  664. len = ecc_place->eccplace[group].length;
  665. group++;
  666. /*
  667. * length is intentionally kept a higher multiple of 2
  668. * to read at least 13 bytes even in case of 16 bit NAND
  669. * devices
  670. */
  671. if (chip->options & NAND_BUSWIDTH_16)
  672. len = roundup(len, 2);
  673. chip->cmdfunc(mtd, NAND_CMD_READOOB, off, page);
  674. chip->read_buf(mtd, oob + j, len);
  675. j += len;
  676. }
  677. memcpy(&ecc_code[i], oob, chip->ecc.bytes);
  678. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  679. stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  680. if (stat < 0) {
  681. mtd->ecc_stats.failed++;
  682. } else {
  683. mtd->ecc_stats.corrected += stat;
  684. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  685. }
  686. }
  687. return max_bitflips;
  688. }
  689. /*
  690. * fsmc_bch8_correct_data
  691. * @mtd: mtd info structure
  692. * @dat: buffer of read data
  693. * @read_ecc: ecc read from device spare area
  694. * @calc_ecc: ecc calculated from read data
  695. *
  696. * calc_ecc is a 104 bit information containing maximum of 8 error
  697. * offset informations of 13 bits each in 512 bytes of read data.
  698. */
  699. static int fsmc_bch8_correct_data(struct mtd_info *mtd, uint8_t *dat,
  700. uint8_t *read_ecc, uint8_t *calc_ecc)
  701. {
  702. struct nand_chip *chip = mtd_to_nand(mtd);
  703. struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
  704. void __iomem *regs = host->regs_va;
  705. unsigned int bank = host->bank;
  706. uint32_t err_idx[8];
  707. uint32_t num_err, i;
  708. uint32_t ecc1, ecc2, ecc3, ecc4;
  709. num_err = (readl_relaxed(FSMC_NAND_REG(regs, bank, STS)) >> 10) & 0xF;
  710. /* no bit flipping */
  711. if (likely(num_err == 0))
  712. return 0;
  713. /* too many errors */
  714. if (unlikely(num_err > 8)) {
  715. /*
  716. * This is a temporary erase check. A newly erased page read
  717. * would result in an ecc error because the oob data is also
  718. * erased to FF and the calculated ecc for an FF data is not
  719. * FF..FF.
  720. * This is a workaround to skip performing correction in case
  721. * data is FF..FF
  722. *
  723. * Logic:
  724. * For every page, each bit written as 0 is counted until these
  725. * number of bits are greater than 8 (the maximum correction
  726. * capability of FSMC for each 512 + 13 bytes)
  727. */
  728. int bits_ecc = count_written_bits(read_ecc, chip->ecc.bytes, 8);
  729. int bits_data = count_written_bits(dat, chip->ecc.size, 8);
  730. if ((bits_ecc + bits_data) <= 8) {
  731. if (bits_data)
  732. memset(dat, 0xff, chip->ecc.size);
  733. return bits_data;
  734. }
  735. return -EBADMSG;
  736. }
  737. /*
  738. * ------------------- calc_ecc[] bit wise -----------|--13 bits--|
  739. * |---idx[7]--|--.....-----|---idx[2]--||---idx[1]--||---idx[0]--|
  740. *
  741. * calc_ecc is a 104 bit information containing maximum of 8 error
  742. * offset informations of 13 bits each. calc_ecc is copied into a
  743. * uint64_t array and error offset indexes are populated in err_idx
  744. * array
  745. */
  746. ecc1 = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC1));
  747. ecc2 = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC2));
  748. ecc3 = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC3));
  749. ecc4 = readl_relaxed(FSMC_NAND_REG(regs, bank, STS));
  750. err_idx[0] = (ecc1 >> 0) & 0x1FFF;
  751. err_idx[1] = (ecc1 >> 13) & 0x1FFF;
  752. err_idx[2] = (((ecc2 >> 0) & 0x7F) << 6) | ((ecc1 >> 26) & 0x3F);
  753. err_idx[3] = (ecc2 >> 7) & 0x1FFF;
  754. err_idx[4] = (((ecc3 >> 0) & 0x1) << 12) | ((ecc2 >> 20) & 0xFFF);
  755. err_idx[5] = (ecc3 >> 1) & 0x1FFF;
  756. err_idx[6] = (ecc3 >> 14) & 0x1FFF;
  757. err_idx[7] = (((ecc4 >> 16) & 0xFF) << 5) | ((ecc3 >> 27) & 0x1F);
  758. i = 0;
  759. while (num_err--) {
  760. change_bit(0, (unsigned long *)&err_idx[i]);
  761. change_bit(1, (unsigned long *)&err_idx[i]);
  762. if (err_idx[i] < chip->ecc.size * 8) {
  763. change_bit(err_idx[i], (unsigned long *)dat);
  764. i++;
  765. }
  766. }
  767. return i;
  768. }
  769. static bool filter(struct dma_chan *chan, void *slave)
  770. {
  771. chan->private = slave;
  772. return true;
  773. }
  774. #ifdef CONFIG_OF
  775. static int fsmc_nand_probe_config_dt(struct platform_device *pdev,
  776. struct device_node *np)
  777. {
  778. struct fsmc_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
  779. u32 val;
  780. int ret;
  781. /* Set default NAND width to 8 bits */
  782. pdata->width = 8;
  783. if (!of_property_read_u32(np, "bank-width", &val)) {
  784. if (val == 2) {
  785. pdata->width = 16;
  786. } else if (val != 1) {
  787. dev_err(&pdev->dev, "invalid bank-width %u\n", val);
  788. return -EINVAL;
  789. }
  790. }
  791. if (of_get_property(np, "nand-skip-bbtscan", NULL))
  792. pdata->options = NAND_SKIP_BBTSCAN;
  793. pdata->nand_timings = devm_kzalloc(&pdev->dev,
  794. sizeof(*pdata->nand_timings), GFP_KERNEL);
  795. if (!pdata->nand_timings)
  796. return -ENOMEM;
  797. ret = of_property_read_u8_array(np, "timings", (u8 *)pdata->nand_timings,
  798. sizeof(*pdata->nand_timings));
  799. if (ret) {
  800. dev_info(&pdev->dev, "No timings in dts specified, using default timings!\n");
  801. pdata->nand_timings = NULL;
  802. }
  803. /* Set default NAND bank to 0 */
  804. pdata->bank = 0;
  805. if (!of_property_read_u32(np, "bank", &val)) {
  806. if (val > 3) {
  807. dev_err(&pdev->dev, "invalid bank %u\n", val);
  808. return -EINVAL;
  809. }
  810. pdata->bank = val;
  811. }
  812. return 0;
  813. }
  814. #else
  815. static int fsmc_nand_probe_config_dt(struct platform_device *pdev,
  816. struct device_node *np)
  817. {
  818. return -ENOSYS;
  819. }
  820. #endif
  821. /*
  822. * fsmc_nand_probe - Probe function
  823. * @pdev: platform device structure
  824. */
  825. static int __init fsmc_nand_probe(struct platform_device *pdev)
  826. {
  827. struct fsmc_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
  828. struct device_node __maybe_unused *np = pdev->dev.of_node;
  829. struct fsmc_nand_data *host;
  830. struct mtd_info *mtd;
  831. struct nand_chip *nand;
  832. struct resource *res;
  833. dma_cap_mask_t mask;
  834. int ret = 0;
  835. u32 pid;
  836. int i;
  837. if (np) {
  838. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  839. pdev->dev.platform_data = pdata;
  840. ret = fsmc_nand_probe_config_dt(pdev, np);
  841. if (ret) {
  842. dev_err(&pdev->dev, "no platform data\n");
  843. return -ENODEV;
  844. }
  845. }
  846. if (!pdata) {
  847. dev_err(&pdev->dev, "platform data is NULL\n");
  848. return -EINVAL;
  849. }
  850. /* Allocate memory for the device structure (and zero it) */
  851. host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
  852. if (!host)
  853. return -ENOMEM;
  854. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_data");
  855. host->data_va = devm_ioremap_resource(&pdev->dev, res);
  856. if (IS_ERR(host->data_va))
  857. return PTR_ERR(host->data_va);
  858. host->data_pa = (dma_addr_t)res->start;
  859. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_addr");
  860. host->addr_va = devm_ioremap_resource(&pdev->dev, res);
  861. if (IS_ERR(host->addr_va))
  862. return PTR_ERR(host->addr_va);
  863. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_cmd");
  864. host->cmd_va = devm_ioremap_resource(&pdev->dev, res);
  865. if (IS_ERR(host->cmd_va))
  866. return PTR_ERR(host->cmd_va);
  867. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "fsmc_regs");
  868. host->regs_va = devm_ioremap_resource(&pdev->dev, res);
  869. if (IS_ERR(host->regs_va))
  870. return PTR_ERR(host->regs_va);
  871. host->clk = clk_get(&pdev->dev, NULL);
  872. if (IS_ERR(host->clk)) {
  873. dev_err(&pdev->dev, "failed to fetch block clock\n");
  874. return PTR_ERR(host->clk);
  875. }
  876. ret = clk_prepare_enable(host->clk);
  877. if (ret)
  878. goto err_clk_prepare_enable;
  879. /*
  880. * This device ID is actually a common AMBA ID as used on the
  881. * AMBA PrimeCell bus. However it is not a PrimeCell.
  882. */
  883. for (pid = 0, i = 0; i < 4; i++)
  884. pid |= (readl(host->regs_va + resource_size(res) - 0x20 + 4 * i) & 255) << (i * 8);
  885. host->pid = pid;
  886. dev_info(&pdev->dev, "FSMC device partno %03x, manufacturer %02x, "
  887. "revision %02x, config %02x\n",
  888. AMBA_PART_BITS(pid), AMBA_MANF_BITS(pid),
  889. AMBA_REV_BITS(pid), AMBA_CONFIG_BITS(pid));
  890. host->bank = pdata->bank;
  891. host->select_chip = pdata->select_bank;
  892. host->partitions = pdata->partitions;
  893. host->nr_partitions = pdata->nr_partitions;
  894. host->dev = &pdev->dev;
  895. host->dev_timings = pdata->nand_timings;
  896. host->mode = pdata->mode;
  897. if (host->mode == USE_DMA_ACCESS)
  898. init_completion(&host->dma_access_complete);
  899. /* Link all private pointers */
  900. mtd = nand_to_mtd(&host->nand);
  901. nand = &host->nand;
  902. nand_set_controller_data(nand, host);
  903. nand_set_flash_node(nand, np);
  904. mtd->dev.parent = &pdev->dev;
  905. nand->IO_ADDR_R = host->data_va;
  906. nand->IO_ADDR_W = host->data_va;
  907. nand->cmd_ctrl = fsmc_cmd_ctrl;
  908. nand->chip_delay = 30;
  909. /*
  910. * Setup default ECC mode. nand_dt_init() called from nand_scan_ident()
  911. * can overwrite this value if the DT provides a different value.
  912. */
  913. nand->ecc.mode = NAND_ECC_HW;
  914. nand->ecc.hwctl = fsmc_enable_hwecc;
  915. nand->ecc.size = 512;
  916. nand->options = pdata->options;
  917. nand->select_chip = fsmc_select_chip;
  918. nand->badblockbits = 7;
  919. nand_set_flash_node(nand, np);
  920. if (pdata->width == FSMC_NAND_BW16)
  921. nand->options |= NAND_BUSWIDTH_16;
  922. switch (host->mode) {
  923. case USE_DMA_ACCESS:
  924. dma_cap_zero(mask);
  925. dma_cap_set(DMA_MEMCPY, mask);
  926. host->read_dma_chan = dma_request_channel(mask, filter,
  927. pdata->read_dma_priv);
  928. if (!host->read_dma_chan) {
  929. dev_err(&pdev->dev, "Unable to get read dma channel\n");
  930. goto err_req_read_chnl;
  931. }
  932. host->write_dma_chan = dma_request_channel(mask, filter,
  933. pdata->write_dma_priv);
  934. if (!host->write_dma_chan) {
  935. dev_err(&pdev->dev, "Unable to get write dma channel\n");
  936. goto err_req_write_chnl;
  937. }
  938. nand->read_buf = fsmc_read_buf_dma;
  939. nand->write_buf = fsmc_write_buf_dma;
  940. break;
  941. default:
  942. case USE_WORD_ACCESS:
  943. nand->read_buf = fsmc_read_buf;
  944. nand->write_buf = fsmc_write_buf;
  945. break;
  946. }
  947. fsmc_nand_setup(host->regs_va, host->bank,
  948. nand->options & NAND_BUSWIDTH_16,
  949. host->dev_timings);
  950. if (AMBA_REV_BITS(host->pid) >= 8) {
  951. nand->ecc.read_page = fsmc_read_page_hwecc;
  952. nand->ecc.calculate = fsmc_read_hwecc_ecc4;
  953. nand->ecc.correct = fsmc_bch8_correct_data;
  954. nand->ecc.bytes = 13;
  955. nand->ecc.strength = 8;
  956. }
  957. /*
  958. * Scan to find existence of the device
  959. */
  960. if (nand_scan_ident(mtd, 1, NULL)) {
  961. ret = -ENXIO;
  962. dev_err(&pdev->dev, "No NAND Device found!\n");
  963. goto err_scan_ident;
  964. }
  965. if (AMBA_REV_BITS(host->pid) >= 8) {
  966. switch (mtd->oobsize) {
  967. case 16:
  968. nand->ecc.layout = &fsmc_ecc4_16_layout;
  969. host->ecc_place = &fsmc_ecc4_sp_place;
  970. break;
  971. case 64:
  972. nand->ecc.layout = &fsmc_ecc4_64_layout;
  973. host->ecc_place = &fsmc_ecc4_lp_place;
  974. break;
  975. case 128:
  976. nand->ecc.layout = &fsmc_ecc4_128_layout;
  977. host->ecc_place = &fsmc_ecc4_lp_place;
  978. break;
  979. case 224:
  980. nand->ecc.layout = &fsmc_ecc4_224_layout;
  981. host->ecc_place = &fsmc_ecc4_lp_place;
  982. break;
  983. case 256:
  984. nand->ecc.layout = &fsmc_ecc4_256_layout;
  985. host->ecc_place = &fsmc_ecc4_lp_place;
  986. break;
  987. default:
  988. dev_warn(&pdev->dev, "No oob scheme defined for oobsize %d\n",
  989. mtd->oobsize);
  990. ret = -EINVAL;
  991. goto err_probe;
  992. }
  993. } else {
  994. switch (nand->ecc.mode) {
  995. case NAND_ECC_HW:
  996. dev_info(&pdev->dev, "Using 1-bit HW ECC scheme\n");
  997. nand->ecc.calculate = fsmc_read_hwecc_ecc1;
  998. nand->ecc.correct = nand_correct_data;
  999. nand->ecc.bytes = 3;
  1000. nand->ecc.strength = 1;
  1001. break;
  1002. case NAND_ECC_SOFT_BCH:
  1003. dev_info(&pdev->dev, "Using 4-bit SW BCH ECC scheme\n");
  1004. break;
  1005. default:
  1006. dev_err(&pdev->dev, "Unsupported ECC mode!\n");
  1007. goto err_probe;
  1008. }
  1009. /*
  1010. * Don't set layout for BCH4 SW ECC. This will be
  1011. * generated later in nand_bch_init() later.
  1012. */
  1013. if (nand->ecc.mode != NAND_ECC_SOFT_BCH) {
  1014. switch (mtd->oobsize) {
  1015. case 16:
  1016. nand->ecc.layout = &fsmc_ecc1_16_layout;
  1017. break;
  1018. case 64:
  1019. nand->ecc.layout = &fsmc_ecc1_64_layout;
  1020. break;
  1021. case 128:
  1022. nand->ecc.layout = &fsmc_ecc1_128_layout;
  1023. break;
  1024. default:
  1025. dev_warn(&pdev->dev,
  1026. "No oob scheme defined for oobsize %d\n",
  1027. mtd->oobsize);
  1028. ret = -EINVAL;
  1029. goto err_probe;
  1030. }
  1031. }
  1032. }
  1033. /* Second stage of scan to fill MTD data-structures */
  1034. if (nand_scan_tail(mtd)) {
  1035. ret = -ENXIO;
  1036. goto err_probe;
  1037. }
  1038. /*
  1039. * The partition information can is accessed by (in the same precedence)
  1040. *
  1041. * command line through Bootloader,
  1042. * platform data,
  1043. * default partition information present in driver.
  1044. */
  1045. /*
  1046. * Check for partition info passed
  1047. */
  1048. mtd->name = "nand";
  1049. ret = mtd_device_register(mtd, host->partitions, host->nr_partitions);
  1050. if (ret)
  1051. goto err_probe;
  1052. platform_set_drvdata(pdev, host);
  1053. dev_info(&pdev->dev, "FSMC NAND driver registration successful\n");
  1054. return 0;
  1055. err_probe:
  1056. err_scan_ident:
  1057. if (host->mode == USE_DMA_ACCESS)
  1058. dma_release_channel(host->write_dma_chan);
  1059. err_req_write_chnl:
  1060. if (host->mode == USE_DMA_ACCESS)
  1061. dma_release_channel(host->read_dma_chan);
  1062. err_req_read_chnl:
  1063. clk_disable_unprepare(host->clk);
  1064. err_clk_prepare_enable:
  1065. clk_put(host->clk);
  1066. return ret;
  1067. }
  1068. /*
  1069. * Clean up routine
  1070. */
  1071. static int fsmc_nand_remove(struct platform_device *pdev)
  1072. {
  1073. struct fsmc_nand_data *host = platform_get_drvdata(pdev);
  1074. if (host) {
  1075. nand_release(nand_to_mtd(&host->nand));
  1076. if (host->mode == USE_DMA_ACCESS) {
  1077. dma_release_channel(host->write_dma_chan);
  1078. dma_release_channel(host->read_dma_chan);
  1079. }
  1080. clk_disable_unprepare(host->clk);
  1081. clk_put(host->clk);
  1082. }
  1083. return 0;
  1084. }
  1085. #ifdef CONFIG_PM_SLEEP
  1086. static int fsmc_nand_suspend(struct device *dev)
  1087. {
  1088. struct fsmc_nand_data *host = dev_get_drvdata(dev);
  1089. if (host)
  1090. clk_disable_unprepare(host->clk);
  1091. return 0;
  1092. }
  1093. static int fsmc_nand_resume(struct device *dev)
  1094. {
  1095. struct fsmc_nand_data *host = dev_get_drvdata(dev);
  1096. if (host) {
  1097. clk_prepare_enable(host->clk);
  1098. fsmc_nand_setup(host->regs_va, host->bank,
  1099. host->nand.options & NAND_BUSWIDTH_16,
  1100. host->dev_timings);
  1101. }
  1102. return 0;
  1103. }
  1104. #endif
  1105. static SIMPLE_DEV_PM_OPS(fsmc_nand_pm_ops, fsmc_nand_suspend, fsmc_nand_resume);
  1106. #ifdef CONFIG_OF
  1107. static const struct of_device_id fsmc_nand_id_table[] = {
  1108. { .compatible = "st,spear600-fsmc-nand" },
  1109. { .compatible = "stericsson,fsmc-nand" },
  1110. {}
  1111. };
  1112. MODULE_DEVICE_TABLE(of, fsmc_nand_id_table);
  1113. #endif
  1114. static struct platform_driver fsmc_nand_driver = {
  1115. .remove = fsmc_nand_remove,
  1116. .driver = {
  1117. .name = "fsmc-nand",
  1118. .of_match_table = of_match_ptr(fsmc_nand_id_table),
  1119. .pm = &fsmc_nand_pm_ops,
  1120. },
  1121. };
  1122. module_platform_driver_probe(fsmc_nand_driver, fsmc_nand_probe);
  1123. MODULE_LICENSE("GPL");
  1124. MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>, Ashish Priyadarshi");
  1125. MODULE_DESCRIPTION("NAND driver for SPEAr Platforms");