fsmc_nand.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  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/amba/bus.h>
  38. #include <mtd/mtd-abi.h>
  39. /* fsmc controller registers for NOR flash */
  40. #define CTRL 0x0
  41. /* ctrl register definitions */
  42. #define BANK_ENABLE (1 << 0)
  43. #define MUXED (1 << 1)
  44. #define NOR_DEV (2 << 2)
  45. #define WIDTH_8 (0 << 4)
  46. #define WIDTH_16 (1 << 4)
  47. #define RSTPWRDWN (1 << 6)
  48. #define WPROT (1 << 7)
  49. #define WRT_ENABLE (1 << 12)
  50. #define WAIT_ENB (1 << 13)
  51. #define CTRL_TIM 0x4
  52. /* ctrl_tim register definitions */
  53. #define FSMC_NOR_BANK_SZ 0x8
  54. #define FSMC_NOR_REG_SIZE 0x40
  55. #define FSMC_NOR_REG(base, bank, reg) (base + \
  56. FSMC_NOR_BANK_SZ * (bank) + \
  57. reg)
  58. /* fsmc controller registers for NAND flash */
  59. #define PC 0x00
  60. /* pc register definitions */
  61. #define FSMC_RESET (1 << 0)
  62. #define FSMC_WAITON (1 << 1)
  63. #define FSMC_ENABLE (1 << 2)
  64. #define FSMC_DEVTYPE_NAND (1 << 3)
  65. #define FSMC_DEVWID_8 (0 << 4)
  66. #define FSMC_DEVWID_16 (1 << 4)
  67. #define FSMC_ECCEN (1 << 6)
  68. #define FSMC_ECCPLEN_512 (0 << 7)
  69. #define FSMC_ECCPLEN_256 (1 << 7)
  70. #define FSMC_TCLR_1 (1)
  71. #define FSMC_TCLR_SHIFT (9)
  72. #define FSMC_TCLR_MASK (0xF)
  73. #define FSMC_TAR_1 (1)
  74. #define FSMC_TAR_SHIFT (13)
  75. #define FSMC_TAR_MASK (0xF)
  76. #define STS 0x04
  77. /* sts register definitions */
  78. #define FSMC_CODE_RDY (1 << 15)
  79. #define COMM 0x08
  80. /* comm register definitions */
  81. #define FSMC_TSET_0 0
  82. #define FSMC_TSET_SHIFT 0
  83. #define FSMC_TSET_MASK 0xFF
  84. #define FSMC_TWAIT_6 6
  85. #define FSMC_TWAIT_SHIFT 8
  86. #define FSMC_TWAIT_MASK 0xFF
  87. #define FSMC_THOLD_4 4
  88. #define FSMC_THOLD_SHIFT 16
  89. #define FSMC_THOLD_MASK 0xFF
  90. #define FSMC_THIZ_1 1
  91. #define FSMC_THIZ_SHIFT 24
  92. #define FSMC_THIZ_MASK 0xFF
  93. #define ATTRIB 0x0C
  94. #define IOATA 0x10
  95. #define ECC1 0x14
  96. #define ECC2 0x18
  97. #define ECC3 0x1C
  98. #define FSMC_NAND_BANK_SZ 0x20
  99. #define FSMC_NAND_REG(base, bank, reg) (base + FSMC_NOR_REG_SIZE + \
  100. (FSMC_NAND_BANK_SZ * (bank)) + \
  101. reg)
  102. #define FSMC_BUSY_WAIT_TIMEOUT (1 * HZ)
  103. struct fsmc_nand_timings {
  104. uint8_t tclr;
  105. uint8_t tar;
  106. uint8_t thiz;
  107. uint8_t thold;
  108. uint8_t twait;
  109. uint8_t tset;
  110. };
  111. enum access_mode {
  112. USE_DMA_ACCESS = 1,
  113. USE_WORD_ACCESS,
  114. };
  115. /**
  116. * struct fsmc_nand_data - structure for FSMC NAND device state
  117. *
  118. * @pid: Part ID on the AMBA PrimeCell format
  119. * @mtd: MTD info for a NAND flash.
  120. * @nand: Chip related info for a NAND flash.
  121. * @partitions: Partition info for a NAND Flash.
  122. * @nr_partitions: Total number of partition of a NAND flash.
  123. *
  124. * @bank: Bank number for probed device.
  125. * @clk: Clock structure for FSMC.
  126. *
  127. * @read_dma_chan: DMA channel for read access
  128. * @write_dma_chan: DMA channel for write access to NAND
  129. * @dma_access_complete: Completion structure
  130. *
  131. * @data_pa: NAND Physical port for Data.
  132. * @data_va: NAND port for Data.
  133. * @cmd_va: NAND port for Command.
  134. * @addr_va: NAND port for Address.
  135. * @regs_va: FSMC regs base address.
  136. */
  137. struct fsmc_nand_data {
  138. u32 pid;
  139. struct nand_chip nand;
  140. unsigned int bank;
  141. struct device *dev;
  142. enum access_mode mode;
  143. struct clk *clk;
  144. /* DMA related objects */
  145. struct dma_chan *read_dma_chan;
  146. struct dma_chan *write_dma_chan;
  147. struct completion dma_access_complete;
  148. struct fsmc_nand_timings *dev_timings;
  149. dma_addr_t data_pa;
  150. void __iomem *data_va;
  151. void __iomem *cmd_va;
  152. void __iomem *addr_va;
  153. void __iomem *regs_va;
  154. };
  155. static int fsmc_ecc1_ooblayout_ecc(struct mtd_info *mtd, int section,
  156. struct mtd_oob_region *oobregion)
  157. {
  158. struct nand_chip *chip = mtd_to_nand(mtd);
  159. if (section >= chip->ecc.steps)
  160. return -ERANGE;
  161. oobregion->offset = (section * 16) + 2;
  162. oobregion->length = 3;
  163. return 0;
  164. }
  165. static int fsmc_ecc1_ooblayout_free(struct mtd_info *mtd, int section,
  166. struct mtd_oob_region *oobregion)
  167. {
  168. struct nand_chip *chip = mtd_to_nand(mtd);
  169. if (section >= chip->ecc.steps)
  170. return -ERANGE;
  171. oobregion->offset = (section * 16) + 8;
  172. if (section < chip->ecc.steps - 1)
  173. oobregion->length = 8;
  174. else
  175. oobregion->length = mtd->oobsize - oobregion->offset;
  176. return 0;
  177. }
  178. static const struct mtd_ooblayout_ops fsmc_ecc1_ooblayout_ops = {
  179. .ecc = fsmc_ecc1_ooblayout_ecc,
  180. .free = fsmc_ecc1_ooblayout_free,
  181. };
  182. /*
  183. * ECC placement definitions in oobfree type format.
  184. * There are 13 bytes of ecc for every 512 byte block and it has to be read
  185. * consecutively and immediately after the 512 byte data block for hardware to
  186. * generate the error bit offsets in 512 byte data.
  187. */
  188. static int fsmc_ecc4_ooblayout_ecc(struct mtd_info *mtd, int section,
  189. struct mtd_oob_region *oobregion)
  190. {
  191. struct nand_chip *chip = mtd_to_nand(mtd);
  192. if (section >= chip->ecc.steps)
  193. return -ERANGE;
  194. oobregion->length = chip->ecc.bytes;
  195. if (!section && mtd->writesize <= 512)
  196. oobregion->offset = 0;
  197. else
  198. oobregion->offset = (section * 16) + 2;
  199. return 0;
  200. }
  201. static int fsmc_ecc4_ooblayout_free(struct mtd_info *mtd, int section,
  202. struct mtd_oob_region *oobregion)
  203. {
  204. struct nand_chip *chip = mtd_to_nand(mtd);
  205. if (section >= chip->ecc.steps)
  206. return -ERANGE;
  207. oobregion->offset = (section * 16) + 15;
  208. if (section < chip->ecc.steps - 1)
  209. oobregion->length = 3;
  210. else
  211. oobregion->length = mtd->oobsize - oobregion->offset;
  212. return 0;
  213. }
  214. static const struct mtd_ooblayout_ops fsmc_ecc4_ooblayout_ops = {
  215. .ecc = fsmc_ecc4_ooblayout_ecc,
  216. .free = fsmc_ecc4_ooblayout_free,
  217. };
  218. static inline struct fsmc_nand_data *mtd_to_fsmc(struct mtd_info *mtd)
  219. {
  220. return container_of(mtd_to_nand(mtd), struct fsmc_nand_data, nand);
  221. }
  222. /*
  223. * fsmc_cmd_ctrl - For facilitaing Hardware access
  224. * This routine allows hardware specific access to control-lines(ALE,CLE)
  225. */
  226. static void fsmc_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  227. {
  228. struct nand_chip *this = mtd_to_nand(mtd);
  229. struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
  230. void __iomem *regs = host->regs_va;
  231. unsigned int bank = host->bank;
  232. if (ctrl & NAND_CTRL_CHANGE) {
  233. u32 pc;
  234. if (ctrl & NAND_CLE) {
  235. this->IO_ADDR_R = host->cmd_va;
  236. this->IO_ADDR_W = host->cmd_va;
  237. } else if (ctrl & NAND_ALE) {
  238. this->IO_ADDR_R = host->addr_va;
  239. this->IO_ADDR_W = host->addr_va;
  240. } else {
  241. this->IO_ADDR_R = host->data_va;
  242. this->IO_ADDR_W = host->data_va;
  243. }
  244. pc = readl(FSMC_NAND_REG(regs, bank, PC));
  245. if (ctrl & NAND_NCE)
  246. pc |= FSMC_ENABLE;
  247. else
  248. pc &= ~FSMC_ENABLE;
  249. writel_relaxed(pc, FSMC_NAND_REG(regs, bank, PC));
  250. }
  251. mb();
  252. if (cmd != NAND_CMD_NONE)
  253. writeb_relaxed(cmd, this->IO_ADDR_W);
  254. }
  255. /*
  256. * fsmc_nand_setup - FSMC (Flexible Static Memory Controller) init routine
  257. *
  258. * This routine initializes timing parameters related to NAND memory access in
  259. * FSMC registers
  260. */
  261. static void fsmc_nand_setup(void __iomem *regs, uint32_t bank,
  262. uint32_t busw, struct fsmc_nand_timings *timings)
  263. {
  264. uint32_t value = FSMC_DEVTYPE_NAND | FSMC_ENABLE | FSMC_WAITON;
  265. uint32_t tclr, tar, thiz, thold, twait, tset;
  266. struct fsmc_nand_timings *tims;
  267. struct fsmc_nand_timings default_timings = {
  268. .tclr = FSMC_TCLR_1,
  269. .tar = FSMC_TAR_1,
  270. .thiz = FSMC_THIZ_1,
  271. .thold = FSMC_THOLD_4,
  272. .twait = FSMC_TWAIT_6,
  273. .tset = FSMC_TSET_0,
  274. };
  275. if (timings)
  276. tims = timings;
  277. else
  278. tims = &default_timings;
  279. tclr = (tims->tclr & FSMC_TCLR_MASK) << FSMC_TCLR_SHIFT;
  280. tar = (tims->tar & FSMC_TAR_MASK) << FSMC_TAR_SHIFT;
  281. thiz = (tims->thiz & FSMC_THIZ_MASK) << FSMC_THIZ_SHIFT;
  282. thold = (tims->thold & FSMC_THOLD_MASK) << FSMC_THOLD_SHIFT;
  283. twait = (tims->twait & FSMC_TWAIT_MASK) << FSMC_TWAIT_SHIFT;
  284. tset = (tims->tset & FSMC_TSET_MASK) << FSMC_TSET_SHIFT;
  285. if (busw)
  286. writel_relaxed(value | FSMC_DEVWID_16,
  287. FSMC_NAND_REG(regs, bank, PC));
  288. else
  289. writel_relaxed(value | FSMC_DEVWID_8,
  290. FSMC_NAND_REG(regs, bank, PC));
  291. writel_relaxed(readl(FSMC_NAND_REG(regs, bank, PC)) | tclr | tar,
  292. FSMC_NAND_REG(regs, bank, PC));
  293. writel_relaxed(thiz | thold | twait | tset,
  294. FSMC_NAND_REG(regs, bank, COMM));
  295. writel_relaxed(thiz | thold | twait | tset,
  296. FSMC_NAND_REG(regs, bank, ATTRIB));
  297. }
  298. /*
  299. * fsmc_enable_hwecc - Enables Hardware ECC through FSMC registers
  300. */
  301. static void fsmc_enable_hwecc(struct mtd_info *mtd, int mode)
  302. {
  303. struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
  304. void __iomem *regs = host->regs_va;
  305. uint32_t bank = host->bank;
  306. writel_relaxed(readl(FSMC_NAND_REG(regs, bank, PC)) & ~FSMC_ECCPLEN_256,
  307. FSMC_NAND_REG(regs, bank, PC));
  308. writel_relaxed(readl(FSMC_NAND_REG(regs, bank, PC)) & ~FSMC_ECCEN,
  309. FSMC_NAND_REG(regs, bank, PC));
  310. writel_relaxed(readl(FSMC_NAND_REG(regs, bank, PC)) | FSMC_ECCEN,
  311. FSMC_NAND_REG(regs, bank, PC));
  312. }
  313. /*
  314. * fsmc_read_hwecc_ecc4 - Hardware ECC calculator for ecc4 option supported by
  315. * FSMC. ECC is 13 bytes for 512 bytes of data (supports error correction up to
  316. * max of 8-bits)
  317. */
  318. static int fsmc_read_hwecc_ecc4(struct mtd_info *mtd, const uint8_t *data,
  319. uint8_t *ecc)
  320. {
  321. struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
  322. void __iomem *regs = host->regs_va;
  323. uint32_t bank = host->bank;
  324. uint32_t ecc_tmp;
  325. unsigned long deadline = jiffies + FSMC_BUSY_WAIT_TIMEOUT;
  326. do {
  327. if (readl_relaxed(FSMC_NAND_REG(regs, bank, STS)) & FSMC_CODE_RDY)
  328. break;
  329. else
  330. cond_resched();
  331. } while (!time_after_eq(jiffies, deadline));
  332. if (time_after_eq(jiffies, deadline)) {
  333. dev_err(host->dev, "calculate ecc timed out\n");
  334. return -ETIMEDOUT;
  335. }
  336. ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC1));
  337. ecc[0] = (uint8_t) (ecc_tmp >> 0);
  338. ecc[1] = (uint8_t) (ecc_tmp >> 8);
  339. ecc[2] = (uint8_t) (ecc_tmp >> 16);
  340. ecc[3] = (uint8_t) (ecc_tmp >> 24);
  341. ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC2));
  342. ecc[4] = (uint8_t) (ecc_tmp >> 0);
  343. ecc[5] = (uint8_t) (ecc_tmp >> 8);
  344. ecc[6] = (uint8_t) (ecc_tmp >> 16);
  345. ecc[7] = (uint8_t) (ecc_tmp >> 24);
  346. ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC3));
  347. ecc[8] = (uint8_t) (ecc_tmp >> 0);
  348. ecc[9] = (uint8_t) (ecc_tmp >> 8);
  349. ecc[10] = (uint8_t) (ecc_tmp >> 16);
  350. ecc[11] = (uint8_t) (ecc_tmp >> 24);
  351. ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, STS));
  352. ecc[12] = (uint8_t) (ecc_tmp >> 16);
  353. return 0;
  354. }
  355. /*
  356. * fsmc_read_hwecc_ecc1 - Hardware ECC calculator for ecc1 option supported by
  357. * FSMC. ECC is 3 bytes for 512 bytes of data (supports error correction up to
  358. * max of 1-bit)
  359. */
  360. static int fsmc_read_hwecc_ecc1(struct mtd_info *mtd, const uint8_t *data,
  361. uint8_t *ecc)
  362. {
  363. struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
  364. void __iomem *regs = host->regs_va;
  365. uint32_t bank = host->bank;
  366. uint32_t ecc_tmp;
  367. ecc_tmp = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC1));
  368. ecc[0] = (uint8_t) (ecc_tmp >> 0);
  369. ecc[1] = (uint8_t) (ecc_tmp >> 8);
  370. ecc[2] = (uint8_t) (ecc_tmp >> 16);
  371. return 0;
  372. }
  373. /* Count the number of 0's in buff upto a max of max_bits */
  374. static int count_written_bits(uint8_t *buff, int size, int max_bits)
  375. {
  376. int k, written_bits = 0;
  377. for (k = 0; k < size; k++) {
  378. written_bits += hweight8(~buff[k]);
  379. if (written_bits > max_bits)
  380. break;
  381. }
  382. return written_bits;
  383. }
  384. static void dma_complete(void *param)
  385. {
  386. struct fsmc_nand_data *host = param;
  387. complete(&host->dma_access_complete);
  388. }
  389. static int dma_xfer(struct fsmc_nand_data *host, void *buffer, int len,
  390. enum dma_data_direction direction)
  391. {
  392. struct dma_chan *chan;
  393. struct dma_device *dma_dev;
  394. struct dma_async_tx_descriptor *tx;
  395. dma_addr_t dma_dst, dma_src, dma_addr;
  396. dma_cookie_t cookie;
  397. unsigned long flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
  398. int ret;
  399. unsigned long time_left;
  400. if (direction == DMA_TO_DEVICE)
  401. chan = host->write_dma_chan;
  402. else if (direction == DMA_FROM_DEVICE)
  403. chan = host->read_dma_chan;
  404. else
  405. return -EINVAL;
  406. dma_dev = chan->device;
  407. dma_addr = dma_map_single(dma_dev->dev, buffer, len, direction);
  408. if (direction == DMA_TO_DEVICE) {
  409. dma_src = dma_addr;
  410. dma_dst = host->data_pa;
  411. } else {
  412. dma_src = host->data_pa;
  413. dma_dst = dma_addr;
  414. }
  415. tx = dma_dev->device_prep_dma_memcpy(chan, dma_dst, dma_src,
  416. len, flags);
  417. if (!tx) {
  418. dev_err(host->dev, "device_prep_dma_memcpy error\n");
  419. ret = -EIO;
  420. goto unmap_dma;
  421. }
  422. tx->callback = dma_complete;
  423. tx->callback_param = host;
  424. cookie = tx->tx_submit(tx);
  425. ret = dma_submit_error(cookie);
  426. if (ret) {
  427. dev_err(host->dev, "dma_submit_error %d\n", cookie);
  428. goto unmap_dma;
  429. }
  430. dma_async_issue_pending(chan);
  431. time_left =
  432. wait_for_completion_timeout(&host->dma_access_complete,
  433. msecs_to_jiffies(3000));
  434. if (time_left == 0) {
  435. dmaengine_terminate_all(chan);
  436. dev_err(host->dev, "wait_for_completion_timeout\n");
  437. ret = -ETIMEDOUT;
  438. goto unmap_dma;
  439. }
  440. ret = 0;
  441. unmap_dma:
  442. dma_unmap_single(dma_dev->dev, dma_addr, len, direction);
  443. return ret;
  444. }
  445. /*
  446. * fsmc_write_buf - write buffer to chip
  447. * @mtd: MTD device structure
  448. * @buf: data buffer
  449. * @len: number of bytes to write
  450. */
  451. static void fsmc_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  452. {
  453. int i;
  454. struct nand_chip *chip = mtd_to_nand(mtd);
  455. if (IS_ALIGNED((uint32_t)buf, sizeof(uint32_t)) &&
  456. IS_ALIGNED(len, sizeof(uint32_t))) {
  457. uint32_t *p = (uint32_t *)buf;
  458. len = len >> 2;
  459. for (i = 0; i < len; i++)
  460. writel_relaxed(p[i], chip->IO_ADDR_W);
  461. } else {
  462. for (i = 0; i < len; i++)
  463. writeb_relaxed(buf[i], chip->IO_ADDR_W);
  464. }
  465. }
  466. /*
  467. * fsmc_read_buf - read chip data into buffer
  468. * @mtd: MTD device structure
  469. * @buf: buffer to store date
  470. * @len: number of bytes to read
  471. */
  472. static void fsmc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  473. {
  474. int i;
  475. struct nand_chip *chip = mtd_to_nand(mtd);
  476. if (IS_ALIGNED((uint32_t)buf, sizeof(uint32_t)) &&
  477. IS_ALIGNED(len, sizeof(uint32_t))) {
  478. uint32_t *p = (uint32_t *)buf;
  479. len = len >> 2;
  480. for (i = 0; i < len; i++)
  481. p[i] = readl_relaxed(chip->IO_ADDR_R);
  482. } else {
  483. for (i = 0; i < len; i++)
  484. buf[i] = readb_relaxed(chip->IO_ADDR_R);
  485. }
  486. }
  487. /*
  488. * fsmc_read_buf_dma - read chip data into buffer
  489. * @mtd: MTD device structure
  490. * @buf: buffer to store date
  491. * @len: number of bytes to read
  492. */
  493. static void fsmc_read_buf_dma(struct mtd_info *mtd, uint8_t *buf, int len)
  494. {
  495. struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
  496. dma_xfer(host, buf, len, DMA_FROM_DEVICE);
  497. }
  498. /*
  499. * fsmc_write_buf_dma - write buffer to chip
  500. * @mtd: MTD device structure
  501. * @buf: data buffer
  502. * @len: number of bytes to write
  503. */
  504. static void fsmc_write_buf_dma(struct mtd_info *mtd, const uint8_t *buf,
  505. int len)
  506. {
  507. struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
  508. dma_xfer(host, (void *)buf, len, DMA_TO_DEVICE);
  509. }
  510. /*
  511. * fsmc_read_page_hwecc
  512. * @mtd: mtd info structure
  513. * @chip: nand chip info structure
  514. * @buf: buffer to store read data
  515. * @oob_required: caller expects OOB data read to chip->oob_poi
  516. * @page: page number to read
  517. *
  518. * This routine is needed for fsmc version 8 as reading from NAND chip has to be
  519. * performed in a strict sequence as follows:
  520. * data(512 byte) -> ecc(13 byte)
  521. * After this read, fsmc hardware generates and reports error data bits(up to a
  522. * max of 8 bits)
  523. */
  524. static int fsmc_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
  525. uint8_t *buf, int oob_required, int page)
  526. {
  527. int i, j, s, stat, eccsize = chip->ecc.size;
  528. int eccbytes = chip->ecc.bytes;
  529. int eccsteps = chip->ecc.steps;
  530. uint8_t *p = buf;
  531. uint8_t *ecc_calc = chip->buffers->ecccalc;
  532. uint8_t *ecc_code = chip->buffers->ecccode;
  533. int off, len, group = 0;
  534. /*
  535. * ecc_oob is intentionally taken as uint16_t. In 16bit devices, we
  536. * end up reading 14 bytes (7 words) from oob. The local array is
  537. * to maintain word alignment
  538. */
  539. uint16_t ecc_oob[7];
  540. uint8_t *oob = (uint8_t *)&ecc_oob[0];
  541. unsigned int max_bitflips = 0;
  542. for (i = 0, s = 0; s < eccsteps; s++, i += eccbytes, p += eccsize) {
  543. chip->cmdfunc(mtd, NAND_CMD_READ0, s * eccsize, page);
  544. chip->ecc.hwctl(mtd, NAND_ECC_READ);
  545. chip->read_buf(mtd, p, eccsize);
  546. for (j = 0; j < eccbytes;) {
  547. struct mtd_oob_region oobregion;
  548. int ret;
  549. ret = mtd_ooblayout_ecc(mtd, group++, &oobregion);
  550. if (ret)
  551. return ret;
  552. off = oobregion.offset;
  553. len = oobregion.length;
  554. /*
  555. * length is intentionally kept a higher multiple of 2
  556. * to read at least 13 bytes even in case of 16 bit NAND
  557. * devices
  558. */
  559. if (chip->options & NAND_BUSWIDTH_16)
  560. len = roundup(len, 2);
  561. chip->cmdfunc(mtd, NAND_CMD_READOOB, off, page);
  562. chip->read_buf(mtd, oob + j, len);
  563. j += len;
  564. }
  565. memcpy(&ecc_code[i], oob, chip->ecc.bytes);
  566. chip->ecc.calculate(mtd, p, &ecc_calc[i]);
  567. stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
  568. if (stat < 0) {
  569. mtd->ecc_stats.failed++;
  570. } else {
  571. mtd->ecc_stats.corrected += stat;
  572. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  573. }
  574. }
  575. return max_bitflips;
  576. }
  577. /*
  578. * fsmc_bch8_correct_data
  579. * @mtd: mtd info structure
  580. * @dat: buffer of read data
  581. * @read_ecc: ecc read from device spare area
  582. * @calc_ecc: ecc calculated from read data
  583. *
  584. * calc_ecc is a 104 bit information containing maximum of 8 error
  585. * offset informations of 13 bits each in 512 bytes of read data.
  586. */
  587. static int fsmc_bch8_correct_data(struct mtd_info *mtd, uint8_t *dat,
  588. uint8_t *read_ecc, uint8_t *calc_ecc)
  589. {
  590. struct nand_chip *chip = mtd_to_nand(mtd);
  591. struct fsmc_nand_data *host = mtd_to_fsmc(mtd);
  592. void __iomem *regs = host->regs_va;
  593. unsigned int bank = host->bank;
  594. uint32_t err_idx[8];
  595. uint32_t num_err, i;
  596. uint32_t ecc1, ecc2, ecc3, ecc4;
  597. num_err = (readl_relaxed(FSMC_NAND_REG(regs, bank, STS)) >> 10) & 0xF;
  598. /* no bit flipping */
  599. if (likely(num_err == 0))
  600. return 0;
  601. /* too many errors */
  602. if (unlikely(num_err > 8)) {
  603. /*
  604. * This is a temporary erase check. A newly erased page read
  605. * would result in an ecc error because the oob data is also
  606. * erased to FF and the calculated ecc for an FF data is not
  607. * FF..FF.
  608. * This is a workaround to skip performing correction in case
  609. * data is FF..FF
  610. *
  611. * Logic:
  612. * For every page, each bit written as 0 is counted until these
  613. * number of bits are greater than 8 (the maximum correction
  614. * capability of FSMC for each 512 + 13 bytes)
  615. */
  616. int bits_ecc = count_written_bits(read_ecc, chip->ecc.bytes, 8);
  617. int bits_data = count_written_bits(dat, chip->ecc.size, 8);
  618. if ((bits_ecc + bits_data) <= 8) {
  619. if (bits_data)
  620. memset(dat, 0xff, chip->ecc.size);
  621. return bits_data;
  622. }
  623. return -EBADMSG;
  624. }
  625. /*
  626. * ------------------- calc_ecc[] bit wise -----------|--13 bits--|
  627. * |---idx[7]--|--.....-----|---idx[2]--||---idx[1]--||---idx[0]--|
  628. *
  629. * calc_ecc is a 104 bit information containing maximum of 8 error
  630. * offset informations of 13 bits each. calc_ecc is copied into a
  631. * uint64_t array and error offset indexes are populated in err_idx
  632. * array
  633. */
  634. ecc1 = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC1));
  635. ecc2 = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC2));
  636. ecc3 = readl_relaxed(FSMC_NAND_REG(regs, bank, ECC3));
  637. ecc4 = readl_relaxed(FSMC_NAND_REG(regs, bank, STS));
  638. err_idx[0] = (ecc1 >> 0) & 0x1FFF;
  639. err_idx[1] = (ecc1 >> 13) & 0x1FFF;
  640. err_idx[2] = (((ecc2 >> 0) & 0x7F) << 6) | ((ecc1 >> 26) & 0x3F);
  641. err_idx[3] = (ecc2 >> 7) & 0x1FFF;
  642. err_idx[4] = (((ecc3 >> 0) & 0x1) << 12) | ((ecc2 >> 20) & 0xFFF);
  643. err_idx[5] = (ecc3 >> 1) & 0x1FFF;
  644. err_idx[6] = (ecc3 >> 14) & 0x1FFF;
  645. err_idx[7] = (((ecc4 >> 16) & 0xFF) << 5) | ((ecc3 >> 27) & 0x1F);
  646. i = 0;
  647. while (num_err--) {
  648. change_bit(0, (unsigned long *)&err_idx[i]);
  649. change_bit(1, (unsigned long *)&err_idx[i]);
  650. if (err_idx[i] < chip->ecc.size * 8) {
  651. change_bit(err_idx[i], (unsigned long *)dat);
  652. i++;
  653. }
  654. }
  655. return i;
  656. }
  657. static bool filter(struct dma_chan *chan, void *slave)
  658. {
  659. chan->private = slave;
  660. return true;
  661. }
  662. static int fsmc_nand_probe_config_dt(struct platform_device *pdev,
  663. struct fsmc_nand_data *host,
  664. struct nand_chip *nand)
  665. {
  666. struct device_node *np = pdev->dev.of_node;
  667. u32 val;
  668. int ret;
  669. nand->options = 0;
  670. if (!of_property_read_u32(np, "bank-width", &val)) {
  671. if (val == 2) {
  672. nand->options |= NAND_BUSWIDTH_16;
  673. } else if (val != 1) {
  674. dev_err(&pdev->dev, "invalid bank-width %u\n", val);
  675. return -EINVAL;
  676. }
  677. }
  678. if (of_get_property(np, "nand-skip-bbtscan", NULL))
  679. nand->options |= NAND_SKIP_BBTSCAN;
  680. host->dev_timings = devm_kzalloc(&pdev->dev,
  681. sizeof(*host->dev_timings), GFP_KERNEL);
  682. if (!host->dev_timings)
  683. return -ENOMEM;
  684. ret = of_property_read_u8_array(np, "timings", (u8 *)host->dev_timings,
  685. sizeof(*host->dev_timings));
  686. if (ret) {
  687. dev_info(&pdev->dev, "No timings in dts specified, using default timings!\n");
  688. host->dev_timings = NULL;
  689. }
  690. /* Set default NAND bank to 0 */
  691. host->bank = 0;
  692. if (!of_property_read_u32(np, "bank", &val)) {
  693. if (val > 3) {
  694. dev_err(&pdev->dev, "invalid bank %u\n", val);
  695. return -EINVAL;
  696. }
  697. host->bank = val;
  698. }
  699. return 0;
  700. }
  701. /*
  702. * fsmc_nand_probe - Probe function
  703. * @pdev: platform device structure
  704. */
  705. static int __init fsmc_nand_probe(struct platform_device *pdev)
  706. {
  707. struct fsmc_nand_data *host;
  708. struct mtd_info *mtd;
  709. struct nand_chip *nand;
  710. struct resource *res;
  711. dma_cap_mask_t mask;
  712. int ret = 0;
  713. u32 pid;
  714. int i;
  715. /* Allocate memory for the device structure (and zero it) */
  716. host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
  717. if (!host)
  718. return -ENOMEM;
  719. nand = &host->nand;
  720. ret = fsmc_nand_probe_config_dt(pdev, host, nand);
  721. if (ret)
  722. return ret;
  723. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_data");
  724. host->data_va = devm_ioremap_resource(&pdev->dev, res);
  725. if (IS_ERR(host->data_va))
  726. return PTR_ERR(host->data_va);
  727. host->data_pa = (dma_addr_t)res->start;
  728. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_addr");
  729. host->addr_va = devm_ioremap_resource(&pdev->dev, res);
  730. if (IS_ERR(host->addr_va))
  731. return PTR_ERR(host->addr_va);
  732. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_cmd");
  733. host->cmd_va = devm_ioremap_resource(&pdev->dev, res);
  734. if (IS_ERR(host->cmd_va))
  735. return PTR_ERR(host->cmd_va);
  736. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "fsmc_regs");
  737. host->regs_va = devm_ioremap_resource(&pdev->dev, res);
  738. if (IS_ERR(host->regs_va))
  739. return PTR_ERR(host->regs_va);
  740. host->clk = devm_clk_get(&pdev->dev, NULL);
  741. if (IS_ERR(host->clk)) {
  742. dev_err(&pdev->dev, "failed to fetch block clock\n");
  743. return PTR_ERR(host->clk);
  744. }
  745. ret = clk_prepare_enable(host->clk);
  746. if (ret)
  747. return ret;
  748. /*
  749. * This device ID is actually a common AMBA ID as used on the
  750. * AMBA PrimeCell bus. However it is not a PrimeCell.
  751. */
  752. for (pid = 0, i = 0; i < 4; i++)
  753. pid |= (readl(host->regs_va + resource_size(res) - 0x20 + 4 * i) & 255) << (i * 8);
  754. host->pid = pid;
  755. dev_info(&pdev->dev, "FSMC device partno %03x, manufacturer %02x, "
  756. "revision %02x, config %02x\n",
  757. AMBA_PART_BITS(pid), AMBA_MANF_BITS(pid),
  758. AMBA_REV_BITS(pid), AMBA_CONFIG_BITS(pid));
  759. host->dev = &pdev->dev;
  760. if (host->mode == USE_DMA_ACCESS)
  761. init_completion(&host->dma_access_complete);
  762. /* Link all private pointers */
  763. mtd = nand_to_mtd(&host->nand);
  764. nand_set_controller_data(nand, host);
  765. nand_set_flash_node(nand, pdev->dev.of_node);
  766. mtd->dev.parent = &pdev->dev;
  767. nand->IO_ADDR_R = host->data_va;
  768. nand->IO_ADDR_W = host->data_va;
  769. nand->cmd_ctrl = fsmc_cmd_ctrl;
  770. nand->chip_delay = 30;
  771. /*
  772. * Setup default ECC mode. nand_dt_init() called from nand_scan_ident()
  773. * can overwrite this value if the DT provides a different value.
  774. */
  775. nand->ecc.mode = NAND_ECC_HW;
  776. nand->ecc.hwctl = fsmc_enable_hwecc;
  777. nand->ecc.size = 512;
  778. nand->badblockbits = 7;
  779. switch (host->mode) {
  780. case USE_DMA_ACCESS:
  781. dma_cap_zero(mask);
  782. dma_cap_set(DMA_MEMCPY, mask);
  783. host->read_dma_chan = dma_request_channel(mask, filter, NULL);
  784. if (!host->read_dma_chan) {
  785. dev_err(&pdev->dev, "Unable to get read dma channel\n");
  786. goto err_req_read_chnl;
  787. }
  788. host->write_dma_chan = dma_request_channel(mask, filter, NULL);
  789. if (!host->write_dma_chan) {
  790. dev_err(&pdev->dev, "Unable to get write dma channel\n");
  791. goto err_req_write_chnl;
  792. }
  793. nand->read_buf = fsmc_read_buf_dma;
  794. nand->write_buf = fsmc_write_buf_dma;
  795. break;
  796. default:
  797. case USE_WORD_ACCESS:
  798. nand->read_buf = fsmc_read_buf;
  799. nand->write_buf = fsmc_write_buf;
  800. break;
  801. }
  802. fsmc_nand_setup(host->regs_va, host->bank,
  803. nand->options & NAND_BUSWIDTH_16,
  804. host->dev_timings);
  805. if (AMBA_REV_BITS(host->pid) >= 8) {
  806. nand->ecc.read_page = fsmc_read_page_hwecc;
  807. nand->ecc.calculate = fsmc_read_hwecc_ecc4;
  808. nand->ecc.correct = fsmc_bch8_correct_data;
  809. nand->ecc.bytes = 13;
  810. nand->ecc.strength = 8;
  811. }
  812. /*
  813. * Scan to find existence of the device
  814. */
  815. ret = nand_scan_ident(mtd, 1, NULL);
  816. if (ret) {
  817. dev_err(&pdev->dev, "No NAND Device found!\n");
  818. goto err_scan_ident;
  819. }
  820. if (AMBA_REV_BITS(host->pid) >= 8) {
  821. switch (mtd->oobsize) {
  822. case 16:
  823. case 64:
  824. case 128:
  825. case 224:
  826. case 256:
  827. break;
  828. default:
  829. dev_warn(&pdev->dev, "No oob scheme defined for oobsize %d\n",
  830. mtd->oobsize);
  831. ret = -EINVAL;
  832. goto err_probe;
  833. }
  834. mtd_set_ooblayout(mtd, &fsmc_ecc4_ooblayout_ops);
  835. } else {
  836. switch (nand->ecc.mode) {
  837. case NAND_ECC_HW:
  838. dev_info(&pdev->dev, "Using 1-bit HW ECC scheme\n");
  839. nand->ecc.calculate = fsmc_read_hwecc_ecc1;
  840. nand->ecc.correct = nand_correct_data;
  841. nand->ecc.bytes = 3;
  842. nand->ecc.strength = 1;
  843. break;
  844. case NAND_ECC_SOFT:
  845. if (nand->ecc.algo == NAND_ECC_BCH) {
  846. dev_info(&pdev->dev, "Using 4-bit SW BCH ECC scheme\n");
  847. break;
  848. }
  849. default:
  850. dev_err(&pdev->dev, "Unsupported ECC mode!\n");
  851. goto err_probe;
  852. }
  853. /*
  854. * Don't set layout for BCH4 SW ECC. This will be
  855. * generated later in nand_bch_init() later.
  856. */
  857. if (nand->ecc.mode == NAND_ECC_HW) {
  858. switch (mtd->oobsize) {
  859. case 16:
  860. case 64:
  861. case 128:
  862. mtd_set_ooblayout(mtd,
  863. &fsmc_ecc1_ooblayout_ops);
  864. break;
  865. default:
  866. dev_warn(&pdev->dev,
  867. "No oob scheme defined for oobsize %d\n",
  868. mtd->oobsize);
  869. ret = -EINVAL;
  870. goto err_probe;
  871. }
  872. }
  873. }
  874. /* Second stage of scan to fill MTD data-structures */
  875. ret = nand_scan_tail(mtd);
  876. if (ret)
  877. goto err_probe;
  878. mtd->name = "nand";
  879. ret = mtd_device_register(mtd, NULL, 0);
  880. if (ret)
  881. goto err_probe;
  882. platform_set_drvdata(pdev, host);
  883. dev_info(&pdev->dev, "FSMC NAND driver registration successful\n");
  884. return 0;
  885. err_probe:
  886. err_scan_ident:
  887. if (host->mode == USE_DMA_ACCESS)
  888. dma_release_channel(host->write_dma_chan);
  889. err_req_write_chnl:
  890. if (host->mode == USE_DMA_ACCESS)
  891. dma_release_channel(host->read_dma_chan);
  892. err_req_read_chnl:
  893. clk_disable_unprepare(host->clk);
  894. return ret;
  895. }
  896. /*
  897. * Clean up routine
  898. */
  899. static int fsmc_nand_remove(struct platform_device *pdev)
  900. {
  901. struct fsmc_nand_data *host = platform_get_drvdata(pdev);
  902. if (host) {
  903. nand_release(nand_to_mtd(&host->nand));
  904. if (host->mode == USE_DMA_ACCESS) {
  905. dma_release_channel(host->write_dma_chan);
  906. dma_release_channel(host->read_dma_chan);
  907. }
  908. clk_disable_unprepare(host->clk);
  909. }
  910. return 0;
  911. }
  912. #ifdef CONFIG_PM_SLEEP
  913. static int fsmc_nand_suspend(struct device *dev)
  914. {
  915. struct fsmc_nand_data *host = dev_get_drvdata(dev);
  916. if (host)
  917. clk_disable_unprepare(host->clk);
  918. return 0;
  919. }
  920. static int fsmc_nand_resume(struct device *dev)
  921. {
  922. struct fsmc_nand_data *host = dev_get_drvdata(dev);
  923. if (host) {
  924. clk_prepare_enable(host->clk);
  925. fsmc_nand_setup(host->regs_va, host->bank,
  926. host->nand.options & NAND_BUSWIDTH_16,
  927. host->dev_timings);
  928. }
  929. return 0;
  930. }
  931. #endif
  932. static SIMPLE_DEV_PM_OPS(fsmc_nand_pm_ops, fsmc_nand_suspend, fsmc_nand_resume);
  933. static const struct of_device_id fsmc_nand_id_table[] = {
  934. { .compatible = "st,spear600-fsmc-nand" },
  935. { .compatible = "stericsson,fsmc-nand" },
  936. {}
  937. };
  938. MODULE_DEVICE_TABLE(of, fsmc_nand_id_table);
  939. static struct platform_driver fsmc_nand_driver = {
  940. .remove = fsmc_nand_remove,
  941. .driver = {
  942. .name = "fsmc-nand",
  943. .of_match_table = fsmc_nand_id_table,
  944. .pm = &fsmc_nand_pm_ops,
  945. },
  946. };
  947. module_platform_driver_probe(fsmc_nand_driver, fsmc_nand_probe);
  948. MODULE_LICENSE("GPL");
  949. MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>, Ashish Priyadarshi");
  950. MODULE_DESCRIPTION("NAND driver for SPEAr Platforms");