fsmc_nand.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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/err.h>
  20. #include <linux/init.h>
  21. #include <linux/module.h>
  22. #include <linux/resource.h>
  23. #include <linux/sched.h>
  24. #include <linux/types.h>
  25. #include <linux/mtd/mtd.h>
  26. #include <linux/mtd/nand.h>
  27. #include <linux/mtd/nand_ecc.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/mtd/partitions.h>
  30. #include <linux/io.h>
  31. #include <linux/slab.h>
  32. #include <linux/mtd/fsmc.h>
  33. #include <linux/amba/bus.h>
  34. #include <mtd/mtd-abi.h>
  35. static struct nand_ecclayout fsmc_ecc1_128_layout = {
  36. .eccbytes = 24,
  37. .eccpos = {2, 3, 4, 18, 19, 20, 34, 35, 36, 50, 51, 52,
  38. 66, 67, 68, 82, 83, 84, 98, 99, 100, 114, 115, 116},
  39. .oobfree = {
  40. {.offset = 8, .length = 8},
  41. {.offset = 24, .length = 8},
  42. {.offset = 40, .length = 8},
  43. {.offset = 56, .length = 8},
  44. {.offset = 72, .length = 8},
  45. {.offset = 88, .length = 8},
  46. {.offset = 104, .length = 8},
  47. {.offset = 120, .length = 8}
  48. }
  49. };
  50. static struct nand_ecclayout fsmc_ecc1_64_layout = {
  51. .eccbytes = 12,
  52. .eccpos = {2, 3, 4, 18, 19, 20, 34, 35, 36, 50, 51, 52},
  53. .oobfree = {
  54. {.offset = 8, .length = 8},
  55. {.offset = 24, .length = 8},
  56. {.offset = 40, .length = 8},
  57. {.offset = 56, .length = 8},
  58. }
  59. };
  60. static struct nand_ecclayout fsmc_ecc1_16_layout = {
  61. .eccbytes = 3,
  62. .eccpos = {2, 3, 4},
  63. .oobfree = {
  64. {.offset = 8, .length = 8},
  65. }
  66. };
  67. /*
  68. * ECC4 layout for NAND of pagesize 8192 bytes & OOBsize 256 bytes. 13*16 bytes
  69. * of OB size is reserved for ECC, Byte no. 0 & 1 reserved for bad block and 46
  70. * bytes are free for use.
  71. */
  72. static struct nand_ecclayout fsmc_ecc4_256_layout = {
  73. .eccbytes = 208,
  74. .eccpos = { 2, 3, 4, 5, 6, 7, 8,
  75. 9, 10, 11, 12, 13, 14,
  76. 18, 19, 20, 21, 22, 23, 24,
  77. 25, 26, 27, 28, 29, 30,
  78. 34, 35, 36, 37, 38, 39, 40,
  79. 41, 42, 43, 44, 45, 46,
  80. 50, 51, 52, 53, 54, 55, 56,
  81. 57, 58, 59, 60, 61, 62,
  82. 66, 67, 68, 69, 70, 71, 72,
  83. 73, 74, 75, 76, 77, 78,
  84. 82, 83, 84, 85, 86, 87, 88,
  85. 89, 90, 91, 92, 93, 94,
  86. 98, 99, 100, 101, 102, 103, 104,
  87. 105, 106, 107, 108, 109, 110,
  88. 114, 115, 116, 117, 118, 119, 120,
  89. 121, 122, 123, 124, 125, 126,
  90. 130, 131, 132, 133, 134, 135, 136,
  91. 137, 138, 139, 140, 141, 142,
  92. 146, 147, 148, 149, 150, 151, 152,
  93. 153, 154, 155, 156, 157, 158,
  94. 162, 163, 164, 165, 166, 167, 168,
  95. 169, 170, 171, 172, 173, 174,
  96. 178, 179, 180, 181, 182, 183, 184,
  97. 185, 186, 187, 188, 189, 190,
  98. 194, 195, 196, 197, 198, 199, 200,
  99. 201, 202, 203, 204, 205, 206,
  100. 210, 211, 212, 213, 214, 215, 216,
  101. 217, 218, 219, 220, 221, 222,
  102. 226, 227, 228, 229, 230, 231, 232,
  103. 233, 234, 235, 236, 237, 238,
  104. 242, 243, 244, 245, 246, 247, 248,
  105. 249, 250, 251, 252, 253, 254
  106. },
  107. .oobfree = {
  108. {.offset = 15, .length = 3},
  109. {.offset = 31, .length = 3},
  110. {.offset = 47, .length = 3},
  111. {.offset = 63, .length = 3},
  112. {.offset = 79, .length = 3},
  113. {.offset = 95, .length = 3},
  114. {.offset = 111, .length = 3},
  115. {.offset = 127, .length = 3},
  116. {.offset = 143, .length = 3},
  117. {.offset = 159, .length = 3},
  118. {.offset = 175, .length = 3},
  119. {.offset = 191, .length = 3},
  120. {.offset = 207, .length = 3},
  121. {.offset = 223, .length = 3},
  122. {.offset = 239, .length = 3},
  123. {.offset = 255, .length = 1}
  124. }
  125. };
  126. /*
  127. * ECC4 layout for NAND of pagesize 4096 bytes & OOBsize 224 bytes. 13*8 bytes
  128. * of OOB size is reserved for ECC, Byte no. 0 & 1 reserved for bad block & 118
  129. * bytes are free for use.
  130. */
  131. static struct nand_ecclayout fsmc_ecc4_224_layout = {
  132. .eccbytes = 104,
  133. .eccpos = { 2, 3, 4, 5, 6, 7, 8,
  134. 9, 10, 11, 12, 13, 14,
  135. 18, 19, 20, 21, 22, 23, 24,
  136. 25, 26, 27, 28, 29, 30,
  137. 34, 35, 36, 37, 38, 39, 40,
  138. 41, 42, 43, 44, 45, 46,
  139. 50, 51, 52, 53, 54, 55, 56,
  140. 57, 58, 59, 60, 61, 62,
  141. 66, 67, 68, 69, 70, 71, 72,
  142. 73, 74, 75, 76, 77, 78,
  143. 82, 83, 84, 85, 86, 87, 88,
  144. 89, 90, 91, 92, 93, 94,
  145. 98, 99, 100, 101, 102, 103, 104,
  146. 105, 106, 107, 108, 109, 110,
  147. 114, 115, 116, 117, 118, 119, 120,
  148. 121, 122, 123, 124, 125, 126
  149. },
  150. .oobfree = {
  151. {.offset = 15, .length = 3},
  152. {.offset = 31, .length = 3},
  153. {.offset = 47, .length = 3},
  154. {.offset = 63, .length = 3},
  155. {.offset = 79, .length = 3},
  156. {.offset = 95, .length = 3},
  157. {.offset = 111, .length = 3},
  158. {.offset = 127, .length = 97}
  159. }
  160. };
  161. /*
  162. * ECC4 layout for NAND of pagesize 4096 bytes & OOBsize 128 bytes. 13*8 bytes
  163. * of OOB size is reserved for ECC, Byte no. 0 & 1 reserved for bad block & 22
  164. * bytes are free for use.
  165. */
  166. static struct nand_ecclayout fsmc_ecc4_128_layout = {
  167. .eccbytes = 104,
  168. .eccpos = { 2, 3, 4, 5, 6, 7, 8,
  169. 9, 10, 11, 12, 13, 14,
  170. 18, 19, 20, 21, 22, 23, 24,
  171. 25, 26, 27, 28, 29, 30,
  172. 34, 35, 36, 37, 38, 39, 40,
  173. 41, 42, 43, 44, 45, 46,
  174. 50, 51, 52, 53, 54, 55, 56,
  175. 57, 58, 59, 60, 61, 62,
  176. 66, 67, 68, 69, 70, 71, 72,
  177. 73, 74, 75, 76, 77, 78,
  178. 82, 83, 84, 85, 86, 87, 88,
  179. 89, 90, 91, 92, 93, 94,
  180. 98, 99, 100, 101, 102, 103, 104,
  181. 105, 106, 107, 108, 109, 110,
  182. 114, 115, 116, 117, 118, 119, 120,
  183. 121, 122, 123, 124, 125, 126
  184. },
  185. .oobfree = {
  186. {.offset = 15, .length = 3},
  187. {.offset = 31, .length = 3},
  188. {.offset = 47, .length = 3},
  189. {.offset = 63, .length = 3},
  190. {.offset = 79, .length = 3},
  191. {.offset = 95, .length = 3},
  192. {.offset = 111, .length = 3},
  193. {.offset = 127, .length = 1}
  194. }
  195. };
  196. /*
  197. * ECC4 layout for NAND of pagesize 2048 bytes & OOBsize 64 bytes. 13*4 bytes of
  198. * OOB size is reserved for ECC, Byte no. 0 & 1 reserved for bad block and 10
  199. * bytes are free for use.
  200. */
  201. static struct nand_ecclayout fsmc_ecc4_64_layout = {
  202. .eccbytes = 52,
  203. .eccpos = { 2, 3, 4, 5, 6, 7, 8,
  204. 9, 10, 11, 12, 13, 14,
  205. 18, 19, 20, 21, 22, 23, 24,
  206. 25, 26, 27, 28, 29, 30,
  207. 34, 35, 36, 37, 38, 39, 40,
  208. 41, 42, 43, 44, 45, 46,
  209. 50, 51, 52, 53, 54, 55, 56,
  210. 57, 58, 59, 60, 61, 62,
  211. },
  212. .oobfree = {
  213. {.offset = 15, .length = 3},
  214. {.offset = 31, .length = 3},
  215. {.offset = 47, .length = 3},
  216. {.offset = 63, .length = 1},
  217. }
  218. };
  219. /*
  220. * ECC4 layout for NAND of pagesize 512 bytes & OOBsize 16 bytes. 13 bytes of
  221. * OOB size is reserved for ECC, Byte no. 4 & 5 reserved for bad block and One
  222. * byte is free for use.
  223. */
  224. static struct nand_ecclayout fsmc_ecc4_16_layout = {
  225. .eccbytes = 13,
  226. .eccpos = { 0, 1, 2, 3, 6, 7, 8,
  227. 9, 10, 11, 12, 13, 14
  228. },
  229. .oobfree = {
  230. {.offset = 15, .length = 1},
  231. }
  232. };
  233. /*
  234. * ECC placement definitions in oobfree type format.
  235. * There are 13 bytes of ecc for every 512 byte block and it has to be read
  236. * consecutively and immediately after the 512 byte data block for hardware to
  237. * generate the error bit offsets in 512 byte data.
  238. * Managing the ecc bytes in the following way makes it easier for software to
  239. * read ecc bytes consecutive to data bytes. This way is similar to
  240. * oobfree structure maintained already in generic nand driver
  241. */
  242. static struct fsmc_eccplace fsmc_ecc4_lp_place = {
  243. .eccplace = {
  244. {.offset = 2, .length = 13},
  245. {.offset = 18, .length = 13},
  246. {.offset = 34, .length = 13},
  247. {.offset = 50, .length = 13},
  248. {.offset = 66, .length = 13},
  249. {.offset = 82, .length = 13},
  250. {.offset = 98, .length = 13},
  251. {.offset = 114, .length = 13}
  252. }
  253. };
  254. static struct fsmc_eccplace fsmc_ecc4_sp_place = {
  255. .eccplace = {
  256. {.offset = 0, .length = 4},
  257. {.offset = 6, .length = 9}
  258. }
  259. };
  260. /**
  261. * struct fsmc_nand_data - structure for FSMC NAND device state
  262. *
  263. * @pid: Part ID on the AMBA PrimeCell format
  264. * @mtd: MTD info for a NAND flash.
  265. * @nand: Chip related info for a NAND flash.
  266. * @partitions: Partition info for a NAND Flash.
  267. * @nr_partitions: Total number of partition of a NAND flash.
  268. *
  269. * @ecc_place: ECC placing locations in oobfree type format.
  270. * @bank: Bank number for probed device.
  271. * @clk: Clock structure for FSMC.
  272. *
  273. * @data_va: NAND port for Data.
  274. * @cmd_va: NAND port for Command.
  275. * @addr_va: NAND port for Address.
  276. * @regs_va: FSMC regs base address.
  277. */
  278. struct fsmc_nand_data {
  279. u32 pid;
  280. struct mtd_info mtd;
  281. struct nand_chip nand;
  282. struct mtd_partition *partitions;
  283. unsigned int nr_partitions;
  284. struct fsmc_eccplace *ecc_place;
  285. unsigned int bank;
  286. struct clk *clk;
  287. struct resource *resregs;
  288. struct resource *rescmd;
  289. struct resource *resaddr;
  290. struct resource *resdata;
  291. void __iomem *data_va;
  292. void __iomem *cmd_va;
  293. void __iomem *addr_va;
  294. void __iomem *regs_va;
  295. void (*select_chip)(uint32_t bank, uint32_t busw);
  296. };
  297. /* Assert CS signal based on chipnr */
  298. static void fsmc_select_chip(struct mtd_info *mtd, int chipnr)
  299. {
  300. struct nand_chip *chip = mtd->priv;
  301. struct fsmc_nand_data *host;
  302. host = container_of(mtd, struct fsmc_nand_data, mtd);
  303. switch (chipnr) {
  304. case -1:
  305. chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
  306. break;
  307. case 0:
  308. case 1:
  309. case 2:
  310. case 3:
  311. if (host->select_chip)
  312. host->select_chip(chipnr,
  313. chip->options & NAND_BUSWIDTH_16);
  314. break;
  315. default:
  316. BUG();
  317. }
  318. }
  319. /*
  320. * fsmc_cmd_ctrl - For facilitaing Hardware access
  321. * This routine allows hardware specific access to control-lines(ALE,CLE)
  322. */
  323. static void fsmc_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  324. {
  325. struct nand_chip *this = mtd->priv;
  326. struct fsmc_nand_data *host = container_of(mtd,
  327. struct fsmc_nand_data, mtd);
  328. struct fsmc_regs *regs = host->regs_va;
  329. unsigned int bank = host->bank;
  330. if (ctrl & NAND_CTRL_CHANGE) {
  331. if (ctrl & NAND_CLE) {
  332. this->IO_ADDR_R = (void __iomem *)host->cmd_va;
  333. this->IO_ADDR_W = (void __iomem *)host->cmd_va;
  334. } else if (ctrl & NAND_ALE) {
  335. this->IO_ADDR_R = (void __iomem *)host->addr_va;
  336. this->IO_ADDR_W = (void __iomem *)host->addr_va;
  337. } else {
  338. this->IO_ADDR_R = (void __iomem *)host->data_va;
  339. this->IO_ADDR_W = (void __iomem *)host->data_va;
  340. }
  341. if (ctrl & NAND_NCE) {
  342. writel(readl(&regs->bank_regs[bank].pc) | FSMC_ENABLE,
  343. &regs->bank_regs[bank].pc);
  344. } else {
  345. writel(readl(&regs->bank_regs[bank].pc) & ~FSMC_ENABLE,
  346. &regs->bank_regs[bank].pc);
  347. }
  348. }
  349. mb();
  350. if (cmd != NAND_CMD_NONE)
  351. writeb(cmd, this->IO_ADDR_W);
  352. }
  353. /*
  354. * fsmc_nand_setup - FSMC (Flexible Static Memory Controller) init routine
  355. *
  356. * This routine initializes timing parameters related to NAND memory access in
  357. * FSMC registers
  358. */
  359. static void __init fsmc_nand_setup(struct fsmc_regs *regs, uint32_t bank,
  360. uint32_t busw)
  361. {
  362. uint32_t value = FSMC_DEVTYPE_NAND | FSMC_ENABLE | FSMC_WAITON;
  363. if (busw)
  364. writel(value | FSMC_DEVWID_16, &regs->bank_regs[bank].pc);
  365. else
  366. writel(value | FSMC_DEVWID_8, &regs->bank_regs[bank].pc);
  367. writel(readl(&regs->bank_regs[bank].pc) | FSMC_TCLR_1 | FSMC_TAR_1,
  368. &regs->bank_regs[bank].pc);
  369. writel(FSMC_THIZ_1 | FSMC_THOLD_4 | FSMC_TWAIT_6 | FSMC_TSET_0,
  370. &regs->bank_regs[bank].comm);
  371. writel(FSMC_THIZ_1 | FSMC_THOLD_4 | FSMC_TWAIT_6 | FSMC_TSET_0,
  372. &regs->bank_regs[bank].attrib);
  373. }
  374. /*
  375. * fsmc_enable_hwecc - Enables Hardware ECC through FSMC registers
  376. */
  377. static void fsmc_enable_hwecc(struct mtd_info *mtd, int mode)
  378. {
  379. struct fsmc_nand_data *host = container_of(mtd,
  380. struct fsmc_nand_data, mtd);
  381. struct fsmc_regs *regs = host->regs_va;
  382. uint32_t bank = host->bank;
  383. writel(readl(&regs->bank_regs[bank].pc) & ~FSMC_ECCPLEN_256,
  384. &regs->bank_regs[bank].pc);
  385. writel(readl(&regs->bank_regs[bank].pc) & ~FSMC_ECCEN,
  386. &regs->bank_regs[bank].pc);
  387. writel(readl(&regs->bank_regs[bank].pc) | FSMC_ECCEN,
  388. &regs->bank_regs[bank].pc);
  389. }
  390. /*
  391. * fsmc_read_hwecc_ecc4 - Hardware ECC calculator for ecc4 option supported by
  392. * FSMC. ECC is 13 bytes for 512 bytes of data (supports error correction up to
  393. * max of 8-bits)
  394. */
  395. static int fsmc_read_hwecc_ecc4(struct mtd_info *mtd, const uint8_t *data,
  396. uint8_t *ecc)
  397. {
  398. struct fsmc_nand_data *host = container_of(mtd,
  399. struct fsmc_nand_data, mtd);
  400. struct fsmc_regs *regs = host->regs_va;
  401. uint32_t bank = host->bank;
  402. uint32_t ecc_tmp;
  403. unsigned long deadline = jiffies + FSMC_BUSY_WAIT_TIMEOUT;
  404. do {
  405. if (readl(&regs->bank_regs[bank].sts) & FSMC_CODE_RDY)
  406. break;
  407. else
  408. cond_resched();
  409. } while (!time_after_eq(jiffies, deadline));
  410. ecc_tmp = readl(&regs->bank_regs[bank].ecc1);
  411. ecc[0] = (uint8_t) (ecc_tmp >> 0);
  412. ecc[1] = (uint8_t) (ecc_tmp >> 8);
  413. ecc[2] = (uint8_t) (ecc_tmp >> 16);
  414. ecc[3] = (uint8_t) (ecc_tmp >> 24);
  415. ecc_tmp = readl(&regs->bank_regs[bank].ecc2);
  416. ecc[4] = (uint8_t) (ecc_tmp >> 0);
  417. ecc[5] = (uint8_t) (ecc_tmp >> 8);
  418. ecc[6] = (uint8_t) (ecc_tmp >> 16);
  419. ecc[7] = (uint8_t) (ecc_tmp >> 24);
  420. ecc_tmp = readl(&regs->bank_regs[bank].ecc3);
  421. ecc[8] = (uint8_t) (ecc_tmp >> 0);
  422. ecc[9] = (uint8_t) (ecc_tmp >> 8);
  423. ecc[10] = (uint8_t) (ecc_tmp >> 16);
  424. ecc[11] = (uint8_t) (ecc_tmp >> 24);
  425. ecc_tmp = readl(&regs->bank_regs[bank].sts);
  426. ecc[12] = (uint8_t) (ecc_tmp >> 16);
  427. return 0;
  428. }
  429. /*
  430. * fsmc_read_hwecc_ecc1 - Hardware ECC calculator for ecc1 option supported by
  431. * FSMC. ECC is 3 bytes for 512 bytes of data (supports error correction up to
  432. * max of 1-bit)
  433. */
  434. static int fsmc_read_hwecc_ecc1(struct mtd_info *mtd, const uint8_t *data,
  435. uint8_t *ecc)
  436. {
  437. struct fsmc_nand_data *host = container_of(mtd,
  438. struct fsmc_nand_data, mtd);
  439. struct fsmc_regs *regs = host->regs_va;
  440. uint32_t bank = host->bank;
  441. uint32_t ecc_tmp;
  442. ecc_tmp = readl(&regs->bank_regs[bank].ecc1);
  443. ecc[0] = (uint8_t) (ecc_tmp >> 0);
  444. ecc[1] = (uint8_t) (ecc_tmp >> 8);
  445. ecc[2] = (uint8_t) (ecc_tmp >> 16);
  446. return 0;
  447. }
  448. /* Count the number of 0's in buff upto a max of max_bits */
  449. static int count_written_bits(uint8_t *buff, int size, int max_bits)
  450. {
  451. int k, written_bits = 0;
  452. for (k = 0; k < size; k++) {
  453. written_bits += hweight8(~buff[k]);
  454. if (written_bits > max_bits)
  455. break;
  456. }
  457. return written_bits;
  458. }
  459. /*
  460. * fsmc_read_page_hwecc
  461. * @mtd: mtd info structure
  462. * @chip: nand chip info structure
  463. * @buf: buffer to store read data
  464. * @page: page number to read
  465. *
  466. * This routine is needed for fsmc version 8 as reading from NAND chip has to be
  467. * performed in a strict sequence as follows:
  468. * data(512 byte) -> ecc(13 byte)
  469. * After this read, fsmc hardware generates and reports error data bits(up to a
  470. * max of 8 bits)
  471. */
  472. static int fsmc_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  473. uint8_t *buf, int page)
  474. {
  475. struct fsmc_nand_data *host = container_of(mtd,
  476. struct fsmc_nand_data, mtd);
  477. struct fsmc_eccplace *ecc_place = host->ecc_place;
  478. int i, j, s, stat, eccsize = chip->ecc.size;
  479. int eccbytes = chip->ecc.bytes;
  480. int eccsteps = chip->ecc.steps;
  481. uint8_t *p = buf;
  482. uint8_t *ecc_calc = chip->buffers->ecccalc;
  483. uint8_t *ecc_code = chip->buffers->ecccode;
  484. int off, len, group = 0;
  485. /*
  486. * ecc_oob is intentionally taken as uint16_t. In 16bit devices, we
  487. * end up reading 14 bytes (7 words) from oob. The local array is
  488. * to maintain word alignment
  489. */
  490. uint16_t ecc_oob[7];
  491. uint8_t *oob = (uint8_t *)&ecc_oob[0];
  492. for (i = 0, s = 0; s < eccsteps; s++, i += eccbytes, p += eccsize) {
  493. chip->cmdfunc(mtd, NAND_CMD_READ0, s * eccsize, page);
  494. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  495. chip->read_buf(mtd, p, eccsize);
  496. for (j = 0; j < eccbytes;) {
  497. off = ecc_place->eccplace[group].offset;
  498. len = ecc_place->eccplace[group].length;
  499. group++;
  500. /*
  501. * length is intentionally kept a higher multiple of 2
  502. * to read at least 13 bytes even in case of 16 bit NAND
  503. * devices
  504. */
  505. len = roundup(len, 2);
  506. chip->cmdfunc(mtd, NAND_CMD_READOOB, off, page);
  507. chip->read_buf(mtd, oob + j, len);
  508. j += len;
  509. }
  510. memcpy(&ecc_code[i], oob, chip->ecc.bytes);
  511. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  512. stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  513. if (stat < 0)
  514. mtd->ecc_stats.failed++;
  515. else
  516. mtd->ecc_stats.corrected += stat;
  517. }
  518. return 0;
  519. }
  520. /*
  521. * fsmc_bch8_correct_data
  522. * @mtd: mtd info structure
  523. * @dat: buffer of read data
  524. * @read_ecc: ecc read from device spare area
  525. * @calc_ecc: ecc calculated from read data
  526. *
  527. * calc_ecc is a 104 bit information containing maximum of 8 error
  528. * offset informations of 13 bits each in 512 bytes of read data.
  529. */
  530. static int fsmc_bch8_correct_data(struct mtd_info *mtd, uint8_t *dat,
  531. uint8_t *read_ecc, uint8_t *calc_ecc)
  532. {
  533. struct fsmc_nand_data *host = container_of(mtd,
  534. struct fsmc_nand_data, mtd);
  535. struct nand_chip *chip = mtd->priv;
  536. struct fsmc_regs *regs = host->regs_va;
  537. unsigned int bank = host->bank;
  538. uint32_t err_idx[8];
  539. uint32_t num_err, i;
  540. uint32_t ecc1, ecc2, ecc3, ecc4;
  541. num_err = (readl(&regs->bank_regs[bank].sts) >> 10) & 0xF;
  542. /* no bit flipping */
  543. if (likely(num_err == 0))
  544. return 0;
  545. /* too many errors */
  546. if (unlikely(num_err > 8)) {
  547. /*
  548. * This is a temporary erase check. A newly erased page read
  549. * would result in an ecc error because the oob data is also
  550. * erased to FF and the calculated ecc for an FF data is not
  551. * FF..FF.
  552. * This is a workaround to skip performing correction in case
  553. * data is FF..FF
  554. *
  555. * Logic:
  556. * For every page, each bit written as 0 is counted until these
  557. * number of bits are greater than 8 (the maximum correction
  558. * capability of FSMC for each 512 + 13 bytes)
  559. */
  560. int bits_ecc = count_written_bits(read_ecc, chip->ecc.bytes, 8);
  561. int bits_data = count_written_bits(dat, chip->ecc.size, 8);
  562. if ((bits_ecc + bits_data) <= 8) {
  563. if (bits_data)
  564. memset(dat, 0xff, chip->ecc.size);
  565. return bits_data;
  566. }
  567. return -EBADMSG;
  568. }
  569. /*
  570. * ------------------- calc_ecc[] bit wise -----------|--13 bits--|
  571. * |---idx[7]--|--.....-----|---idx[2]--||---idx[1]--||---idx[0]--|
  572. *
  573. * calc_ecc is a 104 bit information containing maximum of 8 error
  574. * offset informations of 13 bits each. calc_ecc is copied into a
  575. * uint64_t array and error offset indexes are populated in err_idx
  576. * array
  577. */
  578. ecc1 = readl(&regs->bank_regs[bank].ecc1);
  579. ecc2 = readl(&regs->bank_regs[bank].ecc2);
  580. ecc3 = readl(&regs->bank_regs[bank].ecc3);
  581. ecc4 = readl(&regs->bank_regs[bank].sts);
  582. err_idx[0] = (ecc1 >> 0) & 0x1FFF;
  583. err_idx[1] = (ecc1 >> 13) & 0x1FFF;
  584. err_idx[2] = (((ecc2 >> 0) & 0x7F) << 6) | ((ecc1 >> 26) & 0x3F);
  585. err_idx[3] = (ecc2 >> 7) & 0x1FFF;
  586. err_idx[4] = (((ecc3 >> 0) & 0x1) << 12) | ((ecc2 >> 20) & 0xFFF);
  587. err_idx[5] = (ecc3 >> 1) & 0x1FFF;
  588. err_idx[6] = (ecc3 >> 14) & 0x1FFF;
  589. err_idx[7] = (((ecc4 >> 16) & 0xFF) << 5) | ((ecc3 >> 27) & 0x1F);
  590. i = 0;
  591. while (num_err--) {
  592. change_bit(0, (unsigned long *)&err_idx[i]);
  593. change_bit(1, (unsigned long *)&err_idx[i]);
  594. if (err_idx[i] <= chip->ecc.size * 8) {
  595. change_bit(err_idx[i], (unsigned long *)dat);
  596. i++;
  597. }
  598. }
  599. return i;
  600. }
  601. /*
  602. * fsmc_nand_probe - Probe function
  603. * @pdev: platform device structure
  604. */
  605. static int __init fsmc_nand_probe(struct platform_device *pdev)
  606. {
  607. struct fsmc_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
  608. struct fsmc_nand_data *host;
  609. struct mtd_info *mtd;
  610. struct nand_chip *nand;
  611. struct fsmc_regs *regs;
  612. struct resource *res;
  613. int ret = 0;
  614. u32 pid;
  615. int i;
  616. if (!pdata) {
  617. dev_err(&pdev->dev, "platform data is NULL\n");
  618. return -EINVAL;
  619. }
  620. /* Allocate memory for the device structure (and zero it) */
  621. host = kzalloc(sizeof(*host), GFP_KERNEL);
  622. if (!host) {
  623. dev_err(&pdev->dev, "failed to allocate device structure\n");
  624. return -ENOMEM;
  625. }
  626. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_data");
  627. if (!res) {
  628. ret = -EIO;
  629. goto err_probe1;
  630. }
  631. host->resdata = request_mem_region(res->start, resource_size(res),
  632. pdev->name);
  633. if (!host->resdata) {
  634. ret = -EIO;
  635. goto err_probe1;
  636. }
  637. host->data_va = ioremap(res->start, resource_size(res));
  638. if (!host->data_va) {
  639. ret = -EIO;
  640. goto err_probe1;
  641. }
  642. host->resaddr = request_mem_region(res->start + pdata->ale_off,
  643. resource_size(res), pdev->name);
  644. if (!host->resaddr) {
  645. ret = -EIO;
  646. goto err_probe1;
  647. }
  648. host->addr_va = ioremap(res->start + pdata->ale_off,
  649. resource_size(res));
  650. if (!host->addr_va) {
  651. ret = -EIO;
  652. goto err_probe1;
  653. }
  654. host->rescmd = request_mem_region(res->start + pdata->cle_off,
  655. resource_size(res), pdev->name);
  656. if (!host->rescmd) {
  657. ret = -EIO;
  658. goto err_probe1;
  659. }
  660. host->cmd_va = ioremap(res->start + pdata->cle_off, resource_size(res));
  661. if (!host->cmd_va) {
  662. ret = -EIO;
  663. goto err_probe1;
  664. }
  665. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "fsmc_regs");
  666. if (!res) {
  667. ret = -EIO;
  668. goto err_probe1;
  669. }
  670. host->resregs = request_mem_region(res->start, resource_size(res),
  671. pdev->name);
  672. if (!host->resregs) {
  673. ret = -EIO;
  674. goto err_probe1;
  675. }
  676. host->regs_va = ioremap(res->start, resource_size(res));
  677. if (!host->regs_va) {
  678. ret = -EIO;
  679. goto err_probe1;
  680. }
  681. host->clk = clk_get(&pdev->dev, NULL);
  682. if (IS_ERR(host->clk)) {
  683. dev_err(&pdev->dev, "failed to fetch block clock\n");
  684. ret = PTR_ERR(host->clk);
  685. host->clk = NULL;
  686. goto err_probe1;
  687. }
  688. ret = clk_enable(host->clk);
  689. if (ret)
  690. goto err_probe1;
  691. /*
  692. * This device ID is actually a common AMBA ID as used on the
  693. * AMBA PrimeCell bus. However it is not a PrimeCell.
  694. */
  695. for (pid = 0, i = 0; i < 4; i++)
  696. pid |= (readl(host->regs_va + resource_size(res) - 0x20 + 4 * i) & 255) << (i * 8);
  697. host->pid = pid;
  698. dev_info(&pdev->dev, "FSMC device partno %03x, manufacturer %02x, "
  699. "revision %02x, config %02x\n",
  700. AMBA_PART_BITS(pid), AMBA_MANF_BITS(pid),
  701. AMBA_REV_BITS(pid), AMBA_CONFIG_BITS(pid));
  702. host->bank = pdata->bank;
  703. host->select_chip = pdata->select_bank;
  704. host->partitions = pdata->partitions;
  705. host->nr_partitions = pdata->nr_partitions;
  706. regs = host->regs_va;
  707. /* Link all private pointers */
  708. mtd = &host->mtd;
  709. nand = &host->nand;
  710. mtd->priv = nand;
  711. nand->priv = host;
  712. host->mtd.owner = THIS_MODULE;
  713. nand->IO_ADDR_R = host->data_va;
  714. nand->IO_ADDR_W = host->data_va;
  715. nand->cmd_ctrl = fsmc_cmd_ctrl;
  716. nand->chip_delay = 30;
  717. nand->ecc.mode = NAND_ECC_HW;
  718. nand->ecc.hwctl = fsmc_enable_hwecc;
  719. nand->ecc.size = 512;
  720. nand->options = pdata->options;
  721. nand->select_chip = fsmc_select_chip;
  722. if (pdata->width == FSMC_NAND_BW16)
  723. nand->options |= NAND_BUSWIDTH_16;
  724. fsmc_nand_setup(regs, host->bank, nand->options & NAND_BUSWIDTH_16);
  725. if (AMBA_REV_BITS(host->pid) >= 8) {
  726. nand->ecc.read_page = fsmc_read_page_hwecc;
  727. nand->ecc.calculate = fsmc_read_hwecc_ecc4;
  728. nand->ecc.correct = fsmc_bch8_correct_data;
  729. nand->ecc.bytes = 13;
  730. nand->ecc.strength = 8;
  731. } else {
  732. nand->ecc.calculate = fsmc_read_hwecc_ecc1;
  733. nand->ecc.correct = nand_correct_data;
  734. nand->ecc.bytes = 3;
  735. nand->ecc.strength = 1;
  736. }
  737. /*
  738. * Scan to find existence of the device
  739. */
  740. if (nand_scan_ident(&host->mtd, 1, NULL)) {
  741. ret = -ENXIO;
  742. dev_err(&pdev->dev, "No NAND Device found!\n");
  743. goto err_probe;
  744. }
  745. if (AMBA_REV_BITS(host->pid) >= 8) {
  746. switch (host->mtd.oobsize) {
  747. case 16:
  748. nand->ecc.layout = &fsmc_ecc4_16_layout;
  749. host->ecc_place = &fsmc_ecc4_sp_place;
  750. break;
  751. case 64:
  752. nand->ecc.layout = &fsmc_ecc4_64_layout;
  753. host->ecc_place = &fsmc_ecc4_lp_place;
  754. break;
  755. case 128:
  756. nand->ecc.layout = &fsmc_ecc4_128_layout;
  757. host->ecc_place = &fsmc_ecc4_lp_place;
  758. break;
  759. case 224:
  760. nand->ecc.layout = &fsmc_ecc4_224_layout;
  761. host->ecc_place = &fsmc_ecc4_lp_place;
  762. break;
  763. case 256:
  764. nand->ecc.layout = &fsmc_ecc4_256_layout;
  765. host->ecc_place = &fsmc_ecc4_lp_place;
  766. break;
  767. default:
  768. printk(KERN_WARNING "No oob scheme defined for "
  769. "oobsize %d\n", mtd->oobsize);
  770. BUG();
  771. }
  772. } else {
  773. switch (host->mtd.oobsize) {
  774. case 16:
  775. nand->ecc.layout = &fsmc_ecc1_16_layout;
  776. break;
  777. case 64:
  778. nand->ecc.layout = &fsmc_ecc1_64_layout;
  779. break;
  780. case 128:
  781. nand->ecc.layout = &fsmc_ecc1_128_layout;
  782. break;
  783. default:
  784. printk(KERN_WARNING "No oob scheme defined for "
  785. "oobsize %d\n", mtd->oobsize);
  786. BUG();
  787. }
  788. }
  789. /* Second stage of scan to fill MTD data-structures */
  790. if (nand_scan_tail(&host->mtd)) {
  791. ret = -ENXIO;
  792. goto err_probe;
  793. }
  794. /*
  795. * The partition information can is accessed by (in the same precedence)
  796. *
  797. * command line through Bootloader,
  798. * platform data,
  799. * default partition information present in driver.
  800. */
  801. /*
  802. * Check for partition info passed
  803. */
  804. host->mtd.name = "nand";
  805. ret = mtd_device_parse_register(&host->mtd, NULL, NULL,
  806. host->partitions, host->nr_partitions);
  807. if (ret)
  808. goto err_probe;
  809. platform_set_drvdata(pdev, host);
  810. dev_info(&pdev->dev, "FSMC NAND driver registration successful\n");
  811. return 0;
  812. err_probe:
  813. clk_disable(host->clk);
  814. err_probe1:
  815. if (host->clk)
  816. clk_put(host->clk);
  817. if (host->regs_va)
  818. iounmap(host->regs_va);
  819. if (host->resregs)
  820. release_mem_region(host->resregs->start,
  821. resource_size(host->resregs));
  822. if (host->cmd_va)
  823. iounmap(host->cmd_va);
  824. if (host->rescmd)
  825. release_mem_region(host->rescmd->start,
  826. resource_size(host->rescmd));
  827. if (host->addr_va)
  828. iounmap(host->addr_va);
  829. if (host->resaddr)
  830. release_mem_region(host->resaddr->start,
  831. resource_size(host->resaddr));
  832. if (host->data_va)
  833. iounmap(host->data_va);
  834. if (host->resdata)
  835. release_mem_region(host->resdata->start,
  836. resource_size(host->resdata));
  837. kfree(host);
  838. return ret;
  839. }
  840. /*
  841. * Clean up routine
  842. */
  843. static int fsmc_nand_remove(struct platform_device *pdev)
  844. {
  845. struct fsmc_nand_data *host = platform_get_drvdata(pdev);
  846. platform_set_drvdata(pdev, NULL);
  847. if (host) {
  848. nand_release(&host->mtd);
  849. clk_disable(host->clk);
  850. clk_put(host->clk);
  851. iounmap(host->regs_va);
  852. release_mem_region(host->resregs->start,
  853. resource_size(host->resregs));
  854. iounmap(host->cmd_va);
  855. release_mem_region(host->rescmd->start,
  856. resource_size(host->rescmd));
  857. iounmap(host->addr_va);
  858. release_mem_region(host->resaddr->start,
  859. resource_size(host->resaddr));
  860. iounmap(host->data_va);
  861. release_mem_region(host->resdata->start,
  862. resource_size(host->resdata));
  863. kfree(host);
  864. }
  865. return 0;
  866. }
  867. #ifdef CONFIG_PM
  868. static int fsmc_nand_suspend(struct device *dev)
  869. {
  870. struct fsmc_nand_data *host = dev_get_drvdata(dev);
  871. if (host)
  872. clk_disable(host->clk);
  873. return 0;
  874. }
  875. static int fsmc_nand_resume(struct device *dev)
  876. {
  877. struct fsmc_nand_data *host = dev_get_drvdata(dev);
  878. if (host)
  879. clk_enable(host->clk);
  880. return 0;
  881. }
  882. static const struct dev_pm_ops fsmc_nand_pm_ops = {
  883. .suspend = fsmc_nand_suspend,
  884. .resume = fsmc_nand_resume,
  885. };
  886. #endif
  887. static struct platform_driver fsmc_nand_driver = {
  888. .remove = fsmc_nand_remove,
  889. .driver = {
  890. .owner = THIS_MODULE,
  891. .name = "fsmc-nand",
  892. #ifdef CONFIG_PM
  893. .pm = &fsmc_nand_pm_ops,
  894. #endif
  895. },
  896. };
  897. static int __init fsmc_nand_init(void)
  898. {
  899. return platform_driver_probe(&fsmc_nand_driver,
  900. fsmc_nand_probe);
  901. }
  902. module_init(fsmc_nand_init);
  903. static void __exit fsmc_nand_exit(void)
  904. {
  905. platform_driver_unregister(&fsmc_nand_driver);
  906. }
  907. module_exit(fsmc_nand_exit);
  908. MODULE_LICENSE("GPL");
  909. MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>, Ashish Priyadarshi");
  910. MODULE_DESCRIPTION("NAND driver for SPEAr Platforms");