fsmc_nand.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271
  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(pc, FSMC_NAND_REG(regs, bank, PC));
  362. }
  363. mb();
  364. if (cmd != NAND_CMD_NONE)
  365. writeb(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(value | FSMC_DEVWID_16, FSMC_NAND_REG(regs, bank, PC));
  399. else
  400. writel(value | FSMC_DEVWID_8, FSMC_NAND_REG(regs, bank, PC));
  401. writel(readl(FSMC_NAND_REG(regs, bank, PC)) | tclr | tar,
  402. FSMC_NAND_REG(regs, bank, PC));
  403. writel(thiz | thold | twait | tset, FSMC_NAND_REG(regs, bank, COMM));
  404. writel(thiz | thold | twait | tset, FSMC_NAND_REG(regs, bank, ATTRIB));
  405. }
  406. /*
  407. * fsmc_enable_hwecc - Enables Hardware ECC through FSMC registers
  408. */
  409. static void fsmc_enable_hwecc(struct mtd_info *mtd, int mode)
  410. {
  411. struct fsmc_nand_data *host = container_of(mtd,
  412. struct fsmc_nand_data, mtd);
  413. void __iomem *regs = host->regs_va;
  414. uint32_t bank = host->bank;
  415. writel(readl(FSMC_NAND_REG(regs, bank, PC)) & ~FSMC_ECCPLEN_256,
  416. FSMC_NAND_REG(regs, bank, PC));
  417. writel(readl(FSMC_NAND_REG(regs, bank, PC)) & ~FSMC_ECCEN,
  418. FSMC_NAND_REG(regs, bank, PC));
  419. writel(readl(FSMC_NAND_REG(regs, bank, PC)) | FSMC_ECCEN,
  420. FSMC_NAND_REG(regs, bank, PC));
  421. }
  422. /*
  423. * fsmc_read_hwecc_ecc4 - Hardware ECC calculator for ecc4 option supported by
  424. * FSMC. ECC is 13 bytes for 512 bytes of data (supports error correction up to
  425. * max of 8-bits)
  426. */
  427. static int fsmc_read_hwecc_ecc4(struct mtd_info *mtd, const uint8_t *data,
  428. uint8_t *ecc)
  429. {
  430. struct fsmc_nand_data *host = container_of(mtd,
  431. struct fsmc_nand_data, mtd);
  432. void __iomem *regs = host->regs_va;
  433. uint32_t bank = host->bank;
  434. uint32_t ecc_tmp;
  435. unsigned long deadline = jiffies + FSMC_BUSY_WAIT_TIMEOUT;
  436. do {
  437. if (readl(FSMC_NAND_REG(regs, bank, STS)) & FSMC_CODE_RDY)
  438. break;
  439. else
  440. cond_resched();
  441. } while (!time_after_eq(jiffies, deadline));
  442. if (time_after_eq(jiffies, deadline)) {
  443. dev_err(host->dev, "calculate ecc timed out\n");
  444. return -ETIMEDOUT;
  445. }
  446. ecc_tmp = readl(FSMC_NAND_REG(regs, bank, ECC1));
  447. ecc[0] = (uint8_t) (ecc_tmp >> 0);
  448. ecc[1] = (uint8_t) (ecc_tmp >> 8);
  449. ecc[2] = (uint8_t) (ecc_tmp >> 16);
  450. ecc[3] = (uint8_t) (ecc_tmp >> 24);
  451. ecc_tmp = readl(FSMC_NAND_REG(regs, bank, ECC2));
  452. ecc[4] = (uint8_t) (ecc_tmp >> 0);
  453. ecc[5] = (uint8_t) (ecc_tmp >> 8);
  454. ecc[6] = (uint8_t) (ecc_tmp >> 16);
  455. ecc[7] = (uint8_t) (ecc_tmp >> 24);
  456. ecc_tmp = readl(FSMC_NAND_REG(regs, bank, ECC3));
  457. ecc[8] = (uint8_t) (ecc_tmp >> 0);
  458. ecc[9] = (uint8_t) (ecc_tmp >> 8);
  459. ecc[10] = (uint8_t) (ecc_tmp >> 16);
  460. ecc[11] = (uint8_t) (ecc_tmp >> 24);
  461. ecc_tmp = readl(FSMC_NAND_REG(regs, bank, STS));
  462. ecc[12] = (uint8_t) (ecc_tmp >> 16);
  463. return 0;
  464. }
  465. /*
  466. * fsmc_read_hwecc_ecc1 - Hardware ECC calculator for ecc1 option supported by
  467. * FSMC. ECC is 3 bytes for 512 bytes of data (supports error correction up to
  468. * max of 1-bit)
  469. */
  470. static int fsmc_read_hwecc_ecc1(struct mtd_info *mtd, const uint8_t *data,
  471. uint8_t *ecc)
  472. {
  473. struct fsmc_nand_data *host = container_of(mtd,
  474. struct fsmc_nand_data, mtd);
  475. void __iomem *regs = host->regs_va;
  476. uint32_t bank = host->bank;
  477. uint32_t ecc_tmp;
  478. ecc_tmp = readl(FSMC_NAND_REG(regs, bank, ECC1));
  479. ecc[0] = (uint8_t) (ecc_tmp >> 0);
  480. ecc[1] = (uint8_t) (ecc_tmp >> 8);
  481. ecc[2] = (uint8_t) (ecc_tmp >> 16);
  482. return 0;
  483. }
  484. /* Count the number of 0's in buff upto a max of max_bits */
  485. static int count_written_bits(uint8_t *buff, int size, int max_bits)
  486. {
  487. int k, written_bits = 0;
  488. for (k = 0; k < size; k++) {
  489. written_bits += hweight8(~buff[k]);
  490. if (written_bits > max_bits)
  491. break;
  492. }
  493. return written_bits;
  494. }
  495. static void dma_complete(void *param)
  496. {
  497. struct fsmc_nand_data *host = param;
  498. complete(&host->dma_access_complete);
  499. }
  500. static int dma_xfer(struct fsmc_nand_data *host, void *buffer, int len,
  501. enum dma_data_direction direction)
  502. {
  503. struct dma_chan *chan;
  504. struct dma_device *dma_dev;
  505. struct dma_async_tx_descriptor *tx;
  506. dma_addr_t dma_dst, dma_src, dma_addr;
  507. dma_cookie_t cookie;
  508. unsigned long flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
  509. int ret;
  510. if (direction == DMA_TO_DEVICE)
  511. chan = host->write_dma_chan;
  512. else if (direction == DMA_FROM_DEVICE)
  513. chan = host->read_dma_chan;
  514. else
  515. return -EINVAL;
  516. dma_dev = chan->device;
  517. dma_addr = dma_map_single(dma_dev->dev, buffer, len, direction);
  518. if (direction == DMA_TO_DEVICE) {
  519. dma_src = dma_addr;
  520. dma_dst = host->data_pa;
  521. flags |= DMA_COMPL_SRC_UNMAP_SINGLE | DMA_COMPL_SKIP_DEST_UNMAP;
  522. } else {
  523. dma_src = host->data_pa;
  524. dma_dst = dma_addr;
  525. flags |= DMA_COMPL_DEST_UNMAP_SINGLE | DMA_COMPL_SKIP_SRC_UNMAP;
  526. }
  527. tx = dma_dev->device_prep_dma_memcpy(chan, dma_dst, dma_src,
  528. len, flags);
  529. if (!tx) {
  530. dev_err(host->dev, "device_prep_dma_memcpy error\n");
  531. dma_unmap_single(dma_dev->dev, dma_addr, len, direction);
  532. return -EIO;
  533. }
  534. tx->callback = dma_complete;
  535. tx->callback_param = host;
  536. cookie = tx->tx_submit(tx);
  537. ret = dma_submit_error(cookie);
  538. if (ret) {
  539. dev_err(host->dev, "dma_submit_error %d\n", cookie);
  540. return ret;
  541. }
  542. dma_async_issue_pending(chan);
  543. ret =
  544. wait_for_completion_interruptible_timeout(&host->dma_access_complete,
  545. msecs_to_jiffies(3000));
  546. if (ret <= 0) {
  547. chan->device->device_control(chan, DMA_TERMINATE_ALL, 0);
  548. dev_err(host->dev, "wait_for_completion_timeout\n");
  549. return ret ? ret : -ETIMEDOUT;
  550. }
  551. return 0;
  552. }
  553. /*
  554. * fsmc_write_buf - write buffer to chip
  555. * @mtd: MTD device structure
  556. * @buf: data buffer
  557. * @len: number of bytes to write
  558. */
  559. static void fsmc_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  560. {
  561. int i;
  562. struct nand_chip *chip = mtd->priv;
  563. if (IS_ALIGNED((uint32_t)buf, sizeof(uint32_t)) &&
  564. IS_ALIGNED(len, sizeof(uint32_t))) {
  565. uint32_t *p = (uint32_t *)buf;
  566. len = len >> 2;
  567. for (i = 0; i < len; i++)
  568. writel(p[i], chip->IO_ADDR_W);
  569. } else {
  570. for (i = 0; i < len; i++)
  571. writeb(buf[i], chip->IO_ADDR_W);
  572. }
  573. }
  574. /*
  575. * fsmc_read_buf - read chip data into buffer
  576. * @mtd: MTD device structure
  577. * @buf: buffer to store date
  578. * @len: number of bytes to read
  579. */
  580. static void fsmc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  581. {
  582. int i;
  583. struct nand_chip *chip = mtd->priv;
  584. if (IS_ALIGNED((uint32_t)buf, sizeof(uint32_t)) &&
  585. IS_ALIGNED(len, sizeof(uint32_t))) {
  586. uint32_t *p = (uint32_t *)buf;
  587. len = len >> 2;
  588. for (i = 0; i < len; i++)
  589. p[i] = readl(chip->IO_ADDR_R);
  590. } else {
  591. for (i = 0; i < len; i++)
  592. buf[i] = readb(chip->IO_ADDR_R);
  593. }
  594. }
  595. /*
  596. * fsmc_read_buf_dma - read chip data into buffer
  597. * @mtd: MTD device structure
  598. * @buf: buffer to store date
  599. * @len: number of bytes to read
  600. */
  601. static void fsmc_read_buf_dma(struct mtd_info *mtd, uint8_t *buf, int len)
  602. {
  603. struct fsmc_nand_data *host;
  604. host = container_of(mtd, struct fsmc_nand_data, mtd);
  605. dma_xfer(host, buf, len, DMA_FROM_DEVICE);
  606. }
  607. /*
  608. * fsmc_write_buf_dma - write buffer to chip
  609. * @mtd: MTD device structure
  610. * @buf: data buffer
  611. * @len: number of bytes to write
  612. */
  613. static void fsmc_write_buf_dma(struct mtd_info *mtd, const uint8_t *buf,
  614. int len)
  615. {
  616. struct fsmc_nand_data *host;
  617. host = container_of(mtd, struct fsmc_nand_data, mtd);
  618. dma_xfer(host, (void *)buf, len, DMA_TO_DEVICE);
  619. }
  620. /*
  621. * fsmc_read_page_hwecc
  622. * @mtd: mtd info structure
  623. * @chip: nand chip info structure
  624. * @buf: buffer to store read data
  625. * @oob_required: caller expects OOB data read to chip->oob_poi
  626. * @page: page number to read
  627. *
  628. * This routine is needed for fsmc version 8 as reading from NAND chip has to be
  629. * performed in a strict sequence as follows:
  630. * data(512 byte) -> ecc(13 byte)
  631. * After this read, fsmc hardware generates and reports error data bits(up to a
  632. * max of 8 bits)
  633. */
  634. static int fsmc_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  635. uint8_t *buf, int oob_required, int page)
  636. {
  637. struct fsmc_nand_data *host = container_of(mtd,
  638. struct fsmc_nand_data, mtd);
  639. struct fsmc_eccplace *ecc_place = host->ecc_place;
  640. int i, j, s, stat, eccsize = chip->ecc.size;
  641. int eccbytes = chip->ecc.bytes;
  642. int eccsteps = chip->ecc.steps;
  643. uint8_t *p = buf;
  644. uint8_t *ecc_calc = chip->buffers->ecccalc;
  645. uint8_t *ecc_code = chip->buffers->ecccode;
  646. int off, len, group = 0;
  647. /*
  648. * ecc_oob is intentionally taken as uint16_t. In 16bit devices, we
  649. * end up reading 14 bytes (7 words) from oob. The local array is
  650. * to maintain word alignment
  651. */
  652. uint16_t ecc_oob[7];
  653. uint8_t *oob = (uint8_t *)&ecc_oob[0];
  654. unsigned int max_bitflips = 0;
  655. for (i = 0, s = 0; s < eccsteps; s++, i += eccbytes, p += eccsize) {
  656. chip->cmdfunc(mtd, NAND_CMD_READ0, s * eccsize, page);
  657. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  658. chip->read_buf(mtd, p, eccsize);
  659. for (j = 0; j < eccbytes;) {
  660. off = ecc_place->eccplace[group].offset;
  661. len = ecc_place->eccplace[group].length;
  662. group++;
  663. /*
  664. * length is intentionally kept a higher multiple of 2
  665. * to read at least 13 bytes even in case of 16 bit NAND
  666. * devices
  667. */
  668. if (chip->options & NAND_BUSWIDTH_16)
  669. len = roundup(len, 2);
  670. chip->cmdfunc(mtd, NAND_CMD_READOOB, off, page);
  671. chip->read_buf(mtd, oob + j, len);
  672. j += len;
  673. }
  674. memcpy(&ecc_code[i], oob, chip->ecc.bytes);
  675. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  676. stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  677. if (stat < 0) {
  678. mtd->ecc_stats.failed++;
  679. } else {
  680. mtd->ecc_stats.corrected += stat;
  681. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  682. }
  683. }
  684. return max_bitflips;
  685. }
  686. /*
  687. * fsmc_bch8_correct_data
  688. * @mtd: mtd info structure
  689. * @dat: buffer of read data
  690. * @read_ecc: ecc read from device spare area
  691. * @calc_ecc: ecc calculated from read data
  692. *
  693. * calc_ecc is a 104 bit information containing maximum of 8 error
  694. * offset informations of 13 bits each in 512 bytes of read data.
  695. */
  696. static int fsmc_bch8_correct_data(struct mtd_info *mtd, uint8_t *dat,
  697. uint8_t *read_ecc, uint8_t *calc_ecc)
  698. {
  699. struct fsmc_nand_data *host = container_of(mtd,
  700. struct fsmc_nand_data, mtd);
  701. struct nand_chip *chip = mtd->priv;
  702. void __iomem *regs = host->regs_va;
  703. unsigned int bank = host->bank;
  704. uint32_t err_idx[8];
  705. uint32_t num_err, i;
  706. uint32_t ecc1, ecc2, ecc3, ecc4;
  707. num_err = (readl(FSMC_NAND_REG(regs, bank, STS)) >> 10) & 0xF;
  708. /* no bit flipping */
  709. if (likely(num_err == 0))
  710. return 0;
  711. /* too many errors */
  712. if (unlikely(num_err > 8)) {
  713. /*
  714. * This is a temporary erase check. A newly erased page read
  715. * would result in an ecc error because the oob data is also
  716. * erased to FF and the calculated ecc for an FF data is not
  717. * FF..FF.
  718. * This is a workaround to skip performing correction in case
  719. * data is FF..FF
  720. *
  721. * Logic:
  722. * For every page, each bit written as 0 is counted until these
  723. * number of bits are greater than 8 (the maximum correction
  724. * capability of FSMC for each 512 + 13 bytes)
  725. */
  726. int bits_ecc = count_written_bits(read_ecc, chip->ecc.bytes, 8);
  727. int bits_data = count_written_bits(dat, chip->ecc.size, 8);
  728. if ((bits_ecc + bits_data) <= 8) {
  729. if (bits_data)
  730. memset(dat, 0xff, chip->ecc.size);
  731. return bits_data;
  732. }
  733. return -EBADMSG;
  734. }
  735. /*
  736. * ------------------- calc_ecc[] bit wise -----------|--13 bits--|
  737. * |---idx[7]--|--.....-----|---idx[2]--||---idx[1]--||---idx[0]--|
  738. *
  739. * calc_ecc is a 104 bit information containing maximum of 8 error
  740. * offset informations of 13 bits each. calc_ecc is copied into a
  741. * uint64_t array and error offset indexes are populated in err_idx
  742. * array
  743. */
  744. ecc1 = readl(FSMC_NAND_REG(regs, bank, ECC1));
  745. ecc2 = readl(FSMC_NAND_REG(regs, bank, ECC2));
  746. ecc3 = readl(FSMC_NAND_REG(regs, bank, ECC3));
  747. ecc4 = readl(FSMC_NAND_REG(regs, bank, STS));
  748. err_idx[0] = (ecc1 >> 0) & 0x1FFF;
  749. err_idx[1] = (ecc1 >> 13) & 0x1FFF;
  750. err_idx[2] = (((ecc2 >> 0) & 0x7F) << 6) | ((ecc1 >> 26) & 0x3F);
  751. err_idx[3] = (ecc2 >> 7) & 0x1FFF;
  752. err_idx[4] = (((ecc3 >> 0) & 0x1) << 12) | ((ecc2 >> 20) & 0xFFF);
  753. err_idx[5] = (ecc3 >> 1) & 0x1FFF;
  754. err_idx[6] = (ecc3 >> 14) & 0x1FFF;
  755. err_idx[7] = (((ecc4 >> 16) & 0xFF) << 5) | ((ecc3 >> 27) & 0x1F);
  756. i = 0;
  757. while (num_err--) {
  758. change_bit(0, (unsigned long *)&err_idx[i]);
  759. change_bit(1, (unsigned long *)&err_idx[i]);
  760. if (err_idx[i] < chip->ecc.size * 8) {
  761. change_bit(err_idx[i], (unsigned long *)dat);
  762. i++;
  763. }
  764. }
  765. return i;
  766. }
  767. static bool filter(struct dma_chan *chan, void *slave)
  768. {
  769. chan->private = slave;
  770. return true;
  771. }
  772. #ifdef CONFIG_OF
  773. static int __devinit fsmc_nand_probe_config_dt(struct platform_device *pdev,
  774. struct device_node *np)
  775. {
  776. struct fsmc_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
  777. u32 val;
  778. /* Set default NAND width to 8 bits */
  779. pdata->width = 8;
  780. if (!of_property_read_u32(np, "bank-width", &val)) {
  781. if (val == 2) {
  782. pdata->width = 16;
  783. } else if (val != 1) {
  784. dev_err(&pdev->dev, "invalid bank-width %u\n", val);
  785. return -EINVAL;
  786. }
  787. }
  788. of_property_read_u32(np, "st,ale-off", &pdata->ale_off);
  789. of_property_read_u32(np, "st,cle-off", &pdata->cle_off);
  790. if (of_get_property(np, "nand-skip-bbtscan", NULL))
  791. pdata->options = NAND_SKIP_BBTSCAN;
  792. return 0;
  793. }
  794. #else
  795. static int __devinit fsmc_nand_probe_config_dt(struct platform_device *pdev,
  796. struct device_node *np)
  797. {
  798. return -ENOSYS;
  799. }
  800. #endif
  801. /*
  802. * fsmc_nand_probe - Probe function
  803. * @pdev: platform device structure
  804. */
  805. static int __init fsmc_nand_probe(struct platform_device *pdev)
  806. {
  807. struct fsmc_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
  808. struct device_node __maybe_unused *np = pdev->dev.of_node;
  809. struct mtd_part_parser_data ppdata = {};
  810. struct fsmc_nand_data *host;
  811. struct mtd_info *mtd;
  812. struct nand_chip *nand;
  813. struct resource *res;
  814. dma_cap_mask_t mask;
  815. int ret = 0;
  816. u32 pid;
  817. int i;
  818. if (np) {
  819. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  820. pdev->dev.platform_data = pdata;
  821. ret = fsmc_nand_probe_config_dt(pdev, np);
  822. if (ret) {
  823. dev_err(&pdev->dev, "no platform data\n");
  824. return -ENODEV;
  825. }
  826. }
  827. if (!pdata) {
  828. dev_err(&pdev->dev, "platform data is NULL\n");
  829. return -EINVAL;
  830. }
  831. /* Allocate memory for the device structure (and zero it) */
  832. host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
  833. if (!host) {
  834. dev_err(&pdev->dev, "failed to allocate device structure\n");
  835. return -ENOMEM;
  836. }
  837. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_data");
  838. if (!res)
  839. return -EINVAL;
  840. if (!devm_request_mem_region(&pdev->dev, res->start, resource_size(res),
  841. pdev->name)) {
  842. dev_err(&pdev->dev, "Failed to get memory data resourse\n");
  843. return -ENOENT;
  844. }
  845. host->data_pa = (dma_addr_t)res->start;
  846. host->data_va = devm_ioremap(&pdev->dev, res->start,
  847. resource_size(res));
  848. if (!host->data_va) {
  849. dev_err(&pdev->dev, "data ioremap failed\n");
  850. return -ENOMEM;
  851. }
  852. if (!devm_request_mem_region(&pdev->dev, res->start + pdata->ale_off,
  853. resource_size(res), pdev->name)) {
  854. dev_err(&pdev->dev, "Failed to get memory ale resourse\n");
  855. return -ENOENT;
  856. }
  857. host->addr_va = devm_ioremap(&pdev->dev, res->start + pdata->ale_off,
  858. resource_size(res));
  859. if (!host->addr_va) {
  860. dev_err(&pdev->dev, "ale ioremap failed\n");
  861. return -ENOMEM;
  862. }
  863. if (!devm_request_mem_region(&pdev->dev, res->start + pdata->cle_off,
  864. resource_size(res), pdev->name)) {
  865. dev_err(&pdev->dev, "Failed to get memory cle resourse\n");
  866. return -ENOENT;
  867. }
  868. host->cmd_va = devm_ioremap(&pdev->dev, res->start + pdata->cle_off,
  869. resource_size(res));
  870. if (!host->cmd_va) {
  871. dev_err(&pdev->dev, "ale ioremap failed\n");
  872. return -ENOMEM;
  873. }
  874. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "fsmc_regs");
  875. if (!res)
  876. return -EINVAL;
  877. if (!devm_request_mem_region(&pdev->dev, res->start, resource_size(res),
  878. pdev->name)) {
  879. dev_err(&pdev->dev, "Failed to get memory regs resourse\n");
  880. return -ENOENT;
  881. }
  882. host->regs_va = devm_ioremap(&pdev->dev, res->start,
  883. resource_size(res));
  884. if (!host->regs_va) {
  885. dev_err(&pdev->dev, "regs ioremap failed\n");
  886. return -ENOMEM;
  887. }
  888. host->clk = clk_get(&pdev->dev, NULL);
  889. if (IS_ERR(host->clk)) {
  890. dev_err(&pdev->dev, "failed to fetch block clock\n");
  891. return PTR_ERR(host->clk);
  892. }
  893. ret = clk_prepare_enable(host->clk);
  894. if (ret)
  895. goto err_clk_prepare_enable;
  896. /*
  897. * This device ID is actually a common AMBA ID as used on the
  898. * AMBA PrimeCell bus. However it is not a PrimeCell.
  899. */
  900. for (pid = 0, i = 0; i < 4; i++)
  901. pid |= (readl(host->regs_va + resource_size(res) - 0x20 + 4 * i) & 255) << (i * 8);
  902. host->pid = pid;
  903. dev_info(&pdev->dev, "FSMC device partno %03x, manufacturer %02x, "
  904. "revision %02x, config %02x\n",
  905. AMBA_PART_BITS(pid), AMBA_MANF_BITS(pid),
  906. AMBA_REV_BITS(pid), AMBA_CONFIG_BITS(pid));
  907. host->bank = pdata->bank;
  908. host->select_chip = pdata->select_bank;
  909. host->partitions = pdata->partitions;
  910. host->nr_partitions = pdata->nr_partitions;
  911. host->dev = &pdev->dev;
  912. host->dev_timings = pdata->nand_timings;
  913. host->mode = pdata->mode;
  914. if (host->mode == USE_DMA_ACCESS)
  915. init_completion(&host->dma_access_complete);
  916. /* Link all private pointers */
  917. mtd = &host->mtd;
  918. nand = &host->nand;
  919. mtd->priv = nand;
  920. nand->priv = host;
  921. host->mtd.owner = THIS_MODULE;
  922. nand->IO_ADDR_R = host->data_va;
  923. nand->IO_ADDR_W = host->data_va;
  924. nand->cmd_ctrl = fsmc_cmd_ctrl;
  925. nand->chip_delay = 30;
  926. nand->ecc.mode = NAND_ECC_HW;
  927. nand->ecc.hwctl = fsmc_enable_hwecc;
  928. nand->ecc.size = 512;
  929. nand->options = pdata->options;
  930. nand->select_chip = fsmc_select_chip;
  931. nand->badblockbits = 7;
  932. if (pdata->width == FSMC_NAND_BW16)
  933. nand->options |= NAND_BUSWIDTH_16;
  934. switch (host->mode) {
  935. case USE_DMA_ACCESS:
  936. dma_cap_zero(mask);
  937. dma_cap_set(DMA_MEMCPY, mask);
  938. host->read_dma_chan = dma_request_channel(mask, filter,
  939. pdata->read_dma_priv);
  940. if (!host->read_dma_chan) {
  941. dev_err(&pdev->dev, "Unable to get read dma channel\n");
  942. goto err_req_read_chnl;
  943. }
  944. host->write_dma_chan = dma_request_channel(mask, filter,
  945. pdata->write_dma_priv);
  946. if (!host->write_dma_chan) {
  947. dev_err(&pdev->dev, "Unable to get write dma channel\n");
  948. goto err_req_write_chnl;
  949. }
  950. nand->read_buf = fsmc_read_buf_dma;
  951. nand->write_buf = fsmc_write_buf_dma;
  952. break;
  953. default:
  954. case USE_WORD_ACCESS:
  955. nand->read_buf = fsmc_read_buf;
  956. nand->write_buf = fsmc_write_buf;
  957. break;
  958. }
  959. fsmc_nand_setup(host->regs_va, host->bank,
  960. nand->options & NAND_BUSWIDTH_16,
  961. host->dev_timings);
  962. if (AMBA_REV_BITS(host->pid) >= 8) {
  963. nand->ecc.read_page = fsmc_read_page_hwecc;
  964. nand->ecc.calculate = fsmc_read_hwecc_ecc4;
  965. nand->ecc.correct = fsmc_bch8_correct_data;
  966. nand->ecc.bytes = 13;
  967. nand->ecc.strength = 8;
  968. } else {
  969. nand->ecc.calculate = fsmc_read_hwecc_ecc1;
  970. nand->ecc.correct = nand_correct_data;
  971. nand->ecc.bytes = 3;
  972. nand->ecc.strength = 1;
  973. }
  974. /*
  975. * Scan to find existence of the device
  976. */
  977. if (nand_scan_ident(&host->mtd, 1, NULL)) {
  978. ret = -ENXIO;
  979. dev_err(&pdev->dev, "No NAND Device found!\n");
  980. goto err_scan_ident;
  981. }
  982. if (AMBA_REV_BITS(host->pid) >= 8) {
  983. switch (host->mtd.oobsize) {
  984. case 16:
  985. nand->ecc.layout = &fsmc_ecc4_16_layout;
  986. host->ecc_place = &fsmc_ecc4_sp_place;
  987. break;
  988. case 64:
  989. nand->ecc.layout = &fsmc_ecc4_64_layout;
  990. host->ecc_place = &fsmc_ecc4_lp_place;
  991. break;
  992. case 128:
  993. nand->ecc.layout = &fsmc_ecc4_128_layout;
  994. host->ecc_place = &fsmc_ecc4_lp_place;
  995. break;
  996. case 224:
  997. nand->ecc.layout = &fsmc_ecc4_224_layout;
  998. host->ecc_place = &fsmc_ecc4_lp_place;
  999. break;
  1000. case 256:
  1001. nand->ecc.layout = &fsmc_ecc4_256_layout;
  1002. host->ecc_place = &fsmc_ecc4_lp_place;
  1003. break;
  1004. default:
  1005. printk(KERN_WARNING "No oob scheme defined for "
  1006. "oobsize %d\n", mtd->oobsize);
  1007. BUG();
  1008. }
  1009. } else {
  1010. switch (host->mtd.oobsize) {
  1011. case 16:
  1012. nand->ecc.layout = &fsmc_ecc1_16_layout;
  1013. break;
  1014. case 64:
  1015. nand->ecc.layout = &fsmc_ecc1_64_layout;
  1016. break;
  1017. case 128:
  1018. nand->ecc.layout = &fsmc_ecc1_128_layout;
  1019. break;
  1020. default:
  1021. printk(KERN_WARNING "No oob scheme defined for "
  1022. "oobsize %d\n", mtd->oobsize);
  1023. BUG();
  1024. }
  1025. }
  1026. /* Second stage of scan to fill MTD data-structures */
  1027. if (nand_scan_tail(&host->mtd)) {
  1028. ret = -ENXIO;
  1029. goto err_probe;
  1030. }
  1031. /*
  1032. * The partition information can is accessed by (in the same precedence)
  1033. *
  1034. * command line through Bootloader,
  1035. * platform data,
  1036. * default partition information present in driver.
  1037. */
  1038. /*
  1039. * Check for partition info passed
  1040. */
  1041. host->mtd.name = "nand";
  1042. ppdata.of_node = np;
  1043. ret = mtd_device_parse_register(&host->mtd, NULL, &ppdata,
  1044. host->partitions, host->nr_partitions);
  1045. if (ret)
  1046. goto err_probe;
  1047. platform_set_drvdata(pdev, host);
  1048. dev_info(&pdev->dev, "FSMC NAND driver registration successful\n");
  1049. return 0;
  1050. err_probe:
  1051. err_scan_ident:
  1052. if (host->mode == USE_DMA_ACCESS)
  1053. dma_release_channel(host->write_dma_chan);
  1054. err_req_write_chnl:
  1055. if (host->mode == USE_DMA_ACCESS)
  1056. dma_release_channel(host->read_dma_chan);
  1057. err_req_read_chnl:
  1058. clk_disable_unprepare(host->clk);
  1059. err_clk_prepare_enable:
  1060. clk_put(host->clk);
  1061. return ret;
  1062. }
  1063. /*
  1064. * Clean up routine
  1065. */
  1066. static int fsmc_nand_remove(struct platform_device *pdev)
  1067. {
  1068. struct fsmc_nand_data *host = platform_get_drvdata(pdev);
  1069. platform_set_drvdata(pdev, NULL);
  1070. if (host) {
  1071. nand_release(&host->mtd);
  1072. if (host->mode == USE_DMA_ACCESS) {
  1073. dma_release_channel(host->write_dma_chan);
  1074. dma_release_channel(host->read_dma_chan);
  1075. }
  1076. clk_disable_unprepare(host->clk);
  1077. clk_put(host->clk);
  1078. }
  1079. return 0;
  1080. }
  1081. #ifdef CONFIG_PM
  1082. static int fsmc_nand_suspend(struct device *dev)
  1083. {
  1084. struct fsmc_nand_data *host = dev_get_drvdata(dev);
  1085. if (host)
  1086. clk_disable_unprepare(host->clk);
  1087. return 0;
  1088. }
  1089. static int fsmc_nand_resume(struct device *dev)
  1090. {
  1091. struct fsmc_nand_data *host = dev_get_drvdata(dev);
  1092. if (host) {
  1093. clk_prepare_enable(host->clk);
  1094. fsmc_nand_setup(host->regs_va, host->bank,
  1095. host->nand.options & NAND_BUSWIDTH_16,
  1096. host->dev_timings);
  1097. }
  1098. return 0;
  1099. }
  1100. static SIMPLE_DEV_PM_OPS(fsmc_nand_pm_ops, fsmc_nand_suspend, fsmc_nand_resume);
  1101. #endif
  1102. #ifdef CONFIG_OF
  1103. static const struct of_device_id fsmc_nand_id_table[] = {
  1104. { .compatible = "st,spear600-fsmc-nand" },
  1105. {}
  1106. };
  1107. MODULE_DEVICE_TABLE(of, fsmc_nand_id_table);
  1108. #endif
  1109. static struct platform_driver fsmc_nand_driver = {
  1110. .remove = fsmc_nand_remove,
  1111. .driver = {
  1112. .owner = THIS_MODULE,
  1113. .name = "fsmc-nand",
  1114. .of_match_table = of_match_ptr(fsmc_nand_id_table),
  1115. #ifdef CONFIG_PM
  1116. .pm = &fsmc_nand_pm_ops,
  1117. #endif
  1118. },
  1119. };
  1120. static int __init fsmc_nand_init(void)
  1121. {
  1122. return platform_driver_probe(&fsmc_nand_driver,
  1123. fsmc_nand_probe);
  1124. }
  1125. module_init(fsmc_nand_init);
  1126. static void __exit fsmc_nand_exit(void)
  1127. {
  1128. platform_driver_unregister(&fsmc_nand_driver);
  1129. }
  1130. module_exit(fsmc_nand_exit);
  1131. MODULE_LICENSE("GPL");
  1132. MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>, Ashish Priyadarshi");
  1133. MODULE_DESCRIPTION("NAND driver for SPEAr Platforms");