fsmc_nand.c 26 KB

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