fsmc_nand.c 33 KB

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