fsmc_nand.c 28 KB

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