tango_nand.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. /*
  2. * Copyright (C) 2016 Sigma Designs
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * version 2 as published by the Free Software Foundation.
  7. */
  8. #include <linux/io.h>
  9. #include <linux/of.h>
  10. #include <linux/clk.h>
  11. #include <linux/iopoll.h>
  12. #include <linux/module.h>
  13. #include <linux/mtd/rawnand.h>
  14. #include <linux/dmaengine.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/platform_device.h>
  17. /* Offsets relative to chip->base */
  18. #define PBUS_CMD 0
  19. #define PBUS_ADDR 4
  20. #define PBUS_DATA 8
  21. /* Offsets relative to reg_base */
  22. #define NFC_STATUS 0x00
  23. #define NFC_FLASH_CMD 0x04
  24. #define NFC_DEVICE_CFG 0x08
  25. #define NFC_TIMING1 0x0c
  26. #define NFC_TIMING2 0x10
  27. #define NFC_XFER_CFG 0x14
  28. #define NFC_PKT_0_CFG 0x18
  29. #define NFC_PKT_N_CFG 0x1c
  30. #define NFC_BB_CFG 0x20
  31. #define NFC_ADDR_PAGE 0x24
  32. #define NFC_ADDR_OFFSET 0x28
  33. #define NFC_XFER_STATUS 0x2c
  34. /* NFC_STATUS values */
  35. #define CMD_READY BIT(31)
  36. /* NFC_FLASH_CMD values */
  37. #define NFC_READ 1
  38. #define NFC_WRITE 2
  39. /* NFC_XFER_STATUS values */
  40. #define PAGE_IS_EMPTY BIT(16)
  41. /* Offsets relative to mem_base */
  42. #define METADATA 0x000
  43. #define ERROR_REPORT 0x1c0
  44. /*
  45. * Error reports are split in two bytes:
  46. * byte 0 for the first packet in the page (PKT_0)
  47. * byte 1 for other packets in the page (PKT_N, for N > 0)
  48. * ERR_COUNT_PKT_N is the max error count over all but the first packet.
  49. */
  50. #define ERR_COUNT_PKT_0(v) (((v) >> 0) & 0x3f)
  51. #define ERR_COUNT_PKT_N(v) (((v) >> 8) & 0x3f)
  52. #define DECODE_FAIL_PKT_0(v) (((v) & BIT(7)) == 0)
  53. #define DECODE_FAIL_PKT_N(v) (((v) & BIT(15)) == 0)
  54. /* Offsets relative to pbus_base */
  55. #define PBUS_CS_CTRL 0x83c
  56. #define PBUS_PAD_MODE 0x8f0
  57. /* PBUS_CS_CTRL values */
  58. #define PBUS_IORDY BIT(31)
  59. /*
  60. * PBUS_PAD_MODE values
  61. * In raw mode, the driver communicates directly with the NAND chips.
  62. * In NFC mode, the NAND Flash controller manages the communication.
  63. * We use NFC mode for read and write; raw mode for everything else.
  64. */
  65. #define MODE_RAW 0
  66. #define MODE_NFC BIT(31)
  67. #define METADATA_SIZE 4
  68. #define BBM_SIZE 6
  69. #define FIELD_ORDER 15
  70. #define MAX_CS 4
  71. struct tango_nfc {
  72. struct nand_hw_control hw;
  73. void __iomem *reg_base;
  74. void __iomem *mem_base;
  75. void __iomem *pbus_base;
  76. struct tango_chip *chips[MAX_CS];
  77. struct dma_chan *chan;
  78. int freq_kHz;
  79. };
  80. #define to_tango_nfc(ptr) container_of(ptr, struct tango_nfc, hw)
  81. struct tango_chip {
  82. struct nand_chip nand_chip;
  83. void __iomem *base;
  84. u32 timing1;
  85. u32 timing2;
  86. u32 xfer_cfg;
  87. u32 pkt_0_cfg;
  88. u32 pkt_n_cfg;
  89. u32 bb_cfg;
  90. };
  91. #define to_tango_chip(ptr) container_of(ptr, struct tango_chip, nand_chip)
  92. #define XFER_CFG(cs, page_count, steps, metadata_size) \
  93. ((cs) << 24 | (page_count) << 16 | (steps) << 8 | (metadata_size))
  94. #define PKT_CFG(size, strength) ((size) << 16 | (strength))
  95. #define BB_CFG(bb_offset, bb_size) ((bb_offset) << 16 | (bb_size))
  96. #define TIMING(t0, t1, t2, t3) ((t0) << 24 | (t1) << 16 | (t2) << 8 | (t3))
  97. static void tango_cmd_ctrl(struct mtd_info *mtd, int dat, unsigned int ctrl)
  98. {
  99. struct tango_chip *tchip = to_tango_chip(mtd_to_nand(mtd));
  100. if (ctrl & NAND_CLE)
  101. writeb_relaxed(dat, tchip->base + PBUS_CMD);
  102. if (ctrl & NAND_ALE)
  103. writeb_relaxed(dat, tchip->base + PBUS_ADDR);
  104. }
  105. static int tango_dev_ready(struct mtd_info *mtd)
  106. {
  107. struct nand_chip *chip = mtd_to_nand(mtd);
  108. struct tango_nfc *nfc = to_tango_nfc(chip->controller);
  109. return readl_relaxed(nfc->pbus_base + PBUS_CS_CTRL) & PBUS_IORDY;
  110. }
  111. static u8 tango_read_byte(struct mtd_info *mtd)
  112. {
  113. struct tango_chip *tchip = to_tango_chip(mtd_to_nand(mtd));
  114. return readb_relaxed(tchip->base + PBUS_DATA);
  115. }
  116. static void tango_read_buf(struct mtd_info *mtd, u8 *buf, int len)
  117. {
  118. struct tango_chip *tchip = to_tango_chip(mtd_to_nand(mtd));
  119. ioread8_rep(tchip->base + PBUS_DATA, buf, len);
  120. }
  121. static void tango_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
  122. {
  123. struct tango_chip *tchip = to_tango_chip(mtd_to_nand(mtd));
  124. iowrite8_rep(tchip->base + PBUS_DATA, buf, len);
  125. }
  126. static void tango_select_chip(struct mtd_info *mtd, int idx)
  127. {
  128. struct nand_chip *chip = mtd_to_nand(mtd);
  129. struct tango_nfc *nfc = to_tango_nfc(chip->controller);
  130. struct tango_chip *tchip = to_tango_chip(chip);
  131. if (idx < 0)
  132. return; /* No "chip unselect" function */
  133. writel_relaxed(tchip->timing1, nfc->reg_base + NFC_TIMING1);
  134. writel_relaxed(tchip->timing2, nfc->reg_base + NFC_TIMING2);
  135. writel_relaxed(tchip->xfer_cfg, nfc->reg_base + NFC_XFER_CFG);
  136. writel_relaxed(tchip->pkt_0_cfg, nfc->reg_base + NFC_PKT_0_CFG);
  137. writel_relaxed(tchip->pkt_n_cfg, nfc->reg_base + NFC_PKT_N_CFG);
  138. writel_relaxed(tchip->bb_cfg, nfc->reg_base + NFC_BB_CFG);
  139. }
  140. /*
  141. * The controller does not check for bitflips in erased pages,
  142. * therefore software must check instead.
  143. */
  144. static int check_erased_page(struct nand_chip *chip, u8 *buf)
  145. {
  146. struct mtd_info *mtd = nand_to_mtd(chip);
  147. u8 *meta = chip->oob_poi + BBM_SIZE;
  148. u8 *ecc = chip->oob_poi + BBM_SIZE + METADATA_SIZE;
  149. const int ecc_size = chip->ecc.bytes;
  150. const int pkt_size = chip->ecc.size;
  151. int i, res, meta_len, bitflips = 0;
  152. for (i = 0; i < chip->ecc.steps; ++i) {
  153. meta_len = i ? 0 : METADATA_SIZE;
  154. res = nand_check_erased_ecc_chunk(buf, pkt_size, ecc, ecc_size,
  155. meta, meta_len,
  156. chip->ecc.strength);
  157. if (res < 0)
  158. mtd->ecc_stats.failed++;
  159. else
  160. mtd->ecc_stats.corrected += res;
  161. bitflips = max(res, bitflips);
  162. buf += pkt_size;
  163. ecc += ecc_size;
  164. }
  165. return bitflips;
  166. }
  167. static int decode_error_report(struct nand_chip *chip)
  168. {
  169. u32 status, res;
  170. struct mtd_info *mtd = nand_to_mtd(chip);
  171. struct tango_nfc *nfc = to_tango_nfc(chip->controller);
  172. status = readl_relaxed(nfc->reg_base + NFC_XFER_STATUS);
  173. if (status & PAGE_IS_EMPTY)
  174. return 0;
  175. res = readl_relaxed(nfc->mem_base + ERROR_REPORT);
  176. if (DECODE_FAIL_PKT_0(res) || DECODE_FAIL_PKT_N(res))
  177. return -EBADMSG;
  178. /* ERR_COUNT_PKT_N is max, not sum, but that's all we have */
  179. mtd->ecc_stats.corrected +=
  180. ERR_COUNT_PKT_0(res) + ERR_COUNT_PKT_N(res);
  181. return max(ERR_COUNT_PKT_0(res), ERR_COUNT_PKT_N(res));
  182. }
  183. static void tango_dma_callback(void *arg)
  184. {
  185. complete(arg);
  186. }
  187. static int do_dma(struct tango_nfc *nfc, enum dma_data_direction dir, int cmd,
  188. const void *buf, int len, int page)
  189. {
  190. void __iomem *addr = nfc->reg_base + NFC_STATUS;
  191. struct dma_chan *chan = nfc->chan;
  192. struct dma_async_tx_descriptor *desc;
  193. enum dma_transfer_direction tdir;
  194. struct scatterlist sg;
  195. struct completion tx_done;
  196. int err = -EIO;
  197. u32 res, val;
  198. sg_init_one(&sg, buf, len);
  199. if (dma_map_sg(chan->device->dev, &sg, 1, dir) != 1)
  200. return -EIO;
  201. tdir = dir == DMA_TO_DEVICE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
  202. desc = dmaengine_prep_slave_sg(chan, &sg, 1, tdir, DMA_PREP_INTERRUPT);
  203. if (!desc)
  204. goto dma_unmap;
  205. desc->callback = tango_dma_callback;
  206. desc->callback_param = &tx_done;
  207. init_completion(&tx_done);
  208. writel_relaxed(MODE_NFC, nfc->pbus_base + PBUS_PAD_MODE);
  209. writel_relaxed(page, nfc->reg_base + NFC_ADDR_PAGE);
  210. writel_relaxed(0, nfc->reg_base + NFC_ADDR_OFFSET);
  211. writel_relaxed(cmd, nfc->reg_base + NFC_FLASH_CMD);
  212. dmaengine_submit(desc);
  213. dma_async_issue_pending(chan);
  214. res = wait_for_completion_timeout(&tx_done, HZ);
  215. if (res > 0)
  216. err = readl_poll_timeout(addr, val, val & CMD_READY, 0, 1000);
  217. writel_relaxed(MODE_RAW, nfc->pbus_base + PBUS_PAD_MODE);
  218. dma_unmap:
  219. dma_unmap_sg(chan->device->dev, &sg, 1, dir);
  220. return err;
  221. }
  222. static int tango_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  223. u8 *buf, int oob_required, int page)
  224. {
  225. struct tango_nfc *nfc = to_tango_nfc(chip->controller);
  226. int err, res, len = mtd->writesize;
  227. if (oob_required)
  228. chip->ecc.read_oob(mtd, chip, page);
  229. err = do_dma(nfc, DMA_FROM_DEVICE, NFC_READ, buf, len, page);
  230. if (err)
  231. return err;
  232. res = decode_error_report(chip);
  233. if (res < 0) {
  234. chip->ecc.read_oob_raw(mtd, chip, page);
  235. res = check_erased_page(chip, buf);
  236. }
  237. return res;
  238. }
  239. static int tango_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  240. const u8 *buf, int oob_required, int page)
  241. {
  242. struct tango_nfc *nfc = to_tango_nfc(chip->controller);
  243. int err, status, len = mtd->writesize;
  244. /* Calling tango_write_oob() would send PAGEPROG twice */
  245. if (oob_required)
  246. return -ENOTSUPP;
  247. writel_relaxed(0xffffffff, nfc->mem_base + METADATA);
  248. err = do_dma(nfc, DMA_TO_DEVICE, NFC_WRITE, buf, len, page);
  249. if (err)
  250. return err;
  251. status = chip->waitfunc(mtd, chip);
  252. if (status & NAND_STATUS_FAIL)
  253. return -EIO;
  254. return 0;
  255. }
  256. static void aux_read(struct nand_chip *chip, u8 **buf, int len, int *pos)
  257. {
  258. struct mtd_info *mtd = nand_to_mtd(chip);
  259. *pos += len;
  260. if (!*buf) {
  261. /* skip over "len" bytes */
  262. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, *pos, -1);
  263. } else {
  264. tango_read_buf(mtd, *buf, len);
  265. *buf += len;
  266. }
  267. }
  268. static void aux_write(struct nand_chip *chip, const u8 **buf, int len, int *pos)
  269. {
  270. struct mtd_info *mtd = nand_to_mtd(chip);
  271. *pos += len;
  272. if (!*buf) {
  273. /* skip over "len" bytes */
  274. chip->cmdfunc(mtd, NAND_CMD_RNDIN, *pos, -1);
  275. } else {
  276. tango_write_buf(mtd, *buf, len);
  277. *buf += len;
  278. }
  279. }
  280. /*
  281. * Physical page layout (not drawn to scale)
  282. *
  283. * NB: Bad Block Marker area splits PKT_N in two (N1, N2).
  284. *
  285. * +---+-----------------+-------+-----+-----------+-----+----+-------+
  286. * | M | PKT_0 | ECC_0 | ... | N1 | BBM | N2 | ECC_N |
  287. * +---+-----------------+-------+-----+-----------+-----+----+-------+
  288. *
  289. * Logical page layout:
  290. *
  291. * +-----+---+-------+-----+-------+
  292. * oob = | BBM | M | ECC_0 | ... | ECC_N |
  293. * +-----+---+-------+-----+-------+
  294. *
  295. * +-----------------+-----+-----------------+
  296. * buf = | PKT_0 | ... | PKT_N |
  297. * +-----------------+-----+-----------------+
  298. */
  299. static void raw_read(struct nand_chip *chip, u8 *buf, u8 *oob)
  300. {
  301. struct mtd_info *mtd = nand_to_mtd(chip);
  302. u8 *oob_orig = oob;
  303. const int page_size = mtd->writesize;
  304. const int ecc_size = chip->ecc.bytes;
  305. const int pkt_size = chip->ecc.size;
  306. int pos = 0; /* position within physical page */
  307. int rem = page_size; /* bytes remaining until BBM area */
  308. if (oob)
  309. oob += BBM_SIZE;
  310. aux_read(chip, &oob, METADATA_SIZE, &pos);
  311. while (rem > pkt_size) {
  312. aux_read(chip, &buf, pkt_size, &pos);
  313. aux_read(chip, &oob, ecc_size, &pos);
  314. rem = page_size - pos;
  315. }
  316. aux_read(chip, &buf, rem, &pos);
  317. aux_read(chip, &oob_orig, BBM_SIZE, &pos);
  318. aux_read(chip, &buf, pkt_size - rem, &pos);
  319. aux_read(chip, &oob, ecc_size, &pos);
  320. }
  321. static void raw_write(struct nand_chip *chip, const u8 *buf, const u8 *oob)
  322. {
  323. struct mtd_info *mtd = nand_to_mtd(chip);
  324. const u8 *oob_orig = oob;
  325. const int page_size = mtd->writesize;
  326. const int ecc_size = chip->ecc.bytes;
  327. const int pkt_size = chip->ecc.size;
  328. int pos = 0; /* position within physical page */
  329. int rem = page_size; /* bytes remaining until BBM area */
  330. if (oob)
  331. oob += BBM_SIZE;
  332. aux_write(chip, &oob, METADATA_SIZE, &pos);
  333. while (rem > pkt_size) {
  334. aux_write(chip, &buf, pkt_size, &pos);
  335. aux_write(chip, &oob, ecc_size, &pos);
  336. rem = page_size - pos;
  337. }
  338. aux_write(chip, &buf, rem, &pos);
  339. aux_write(chip, &oob_orig, BBM_SIZE, &pos);
  340. aux_write(chip, &buf, pkt_size - rem, &pos);
  341. aux_write(chip, &oob, ecc_size, &pos);
  342. }
  343. static int tango_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  344. u8 *buf, int oob_required, int page)
  345. {
  346. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  347. raw_read(chip, buf, chip->oob_poi);
  348. return 0;
  349. }
  350. static int tango_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  351. const u8 *buf, int oob_required, int page)
  352. {
  353. int status;
  354. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0, page);
  355. raw_write(chip, buf, chip->oob_poi);
  356. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  357. status = chip->waitfunc(mtd, chip);
  358. if (status & NAND_STATUS_FAIL)
  359. return -EIO;
  360. return 0;
  361. }
  362. static int tango_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
  363. int page)
  364. {
  365. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  366. raw_read(chip, NULL, chip->oob_poi);
  367. return 0;
  368. }
  369. static int tango_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
  370. int page)
  371. {
  372. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0, page);
  373. raw_write(chip, NULL, chip->oob_poi);
  374. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  375. chip->waitfunc(mtd, chip);
  376. return 0;
  377. }
  378. static int oob_ecc(struct mtd_info *mtd, int idx, struct mtd_oob_region *res)
  379. {
  380. struct nand_chip *chip = mtd_to_nand(mtd);
  381. struct nand_ecc_ctrl *ecc = &chip->ecc;
  382. if (idx >= ecc->steps)
  383. return -ERANGE;
  384. res->offset = BBM_SIZE + METADATA_SIZE + ecc->bytes * idx;
  385. res->length = ecc->bytes;
  386. return 0;
  387. }
  388. static int oob_free(struct mtd_info *mtd, int idx, struct mtd_oob_region *res)
  389. {
  390. return -ERANGE; /* no free space in spare area */
  391. }
  392. static const struct mtd_ooblayout_ops tango_nand_ooblayout_ops = {
  393. .ecc = oob_ecc,
  394. .free = oob_free,
  395. };
  396. static u32 to_ticks(int kHz, int ps)
  397. {
  398. return DIV_ROUND_UP_ULL((u64)kHz * ps, NSEC_PER_SEC);
  399. }
  400. static int tango_set_timings(struct mtd_info *mtd, int csline,
  401. const struct nand_data_interface *conf)
  402. {
  403. const struct nand_sdr_timings *sdr = nand_get_sdr_timings(conf);
  404. struct nand_chip *chip = mtd_to_nand(mtd);
  405. struct tango_nfc *nfc = to_tango_nfc(chip->controller);
  406. struct tango_chip *tchip = to_tango_chip(chip);
  407. u32 Trdy, Textw, Twc, Twpw, Tacc, Thold, Trpw, Textr;
  408. int kHz = nfc->freq_kHz;
  409. if (IS_ERR(sdr))
  410. return PTR_ERR(sdr);
  411. if (csline == NAND_DATA_IFACE_CHECK_ONLY)
  412. return 0;
  413. Trdy = to_ticks(kHz, sdr->tCEA_max - sdr->tREA_max);
  414. Textw = to_ticks(kHz, sdr->tWB_max);
  415. Twc = to_ticks(kHz, sdr->tWC_min);
  416. Twpw = to_ticks(kHz, sdr->tWC_min - sdr->tWP_min);
  417. Tacc = to_ticks(kHz, sdr->tREA_max);
  418. Thold = to_ticks(kHz, sdr->tREH_min);
  419. Trpw = to_ticks(kHz, sdr->tRC_min - sdr->tREH_min);
  420. Textr = to_ticks(kHz, sdr->tRHZ_max);
  421. tchip->timing1 = TIMING(Trdy, Textw, Twc, Twpw);
  422. tchip->timing2 = TIMING(Tacc, Thold, Trpw, Textr);
  423. return 0;
  424. }
  425. static int chip_init(struct device *dev, struct device_node *np)
  426. {
  427. u32 cs;
  428. int err, res;
  429. struct mtd_info *mtd;
  430. struct nand_chip *chip;
  431. struct tango_chip *tchip;
  432. struct nand_ecc_ctrl *ecc;
  433. struct tango_nfc *nfc = dev_get_drvdata(dev);
  434. tchip = devm_kzalloc(dev, sizeof(*tchip), GFP_KERNEL);
  435. if (!tchip)
  436. return -ENOMEM;
  437. res = of_property_count_u32_elems(np, "reg");
  438. if (res < 0)
  439. return res;
  440. if (res != 1)
  441. return -ENOTSUPP; /* Multi-CS chips are not supported */
  442. err = of_property_read_u32_index(np, "reg", 0, &cs);
  443. if (err)
  444. return err;
  445. if (cs >= MAX_CS)
  446. return -EINVAL;
  447. chip = &tchip->nand_chip;
  448. ecc = &chip->ecc;
  449. mtd = nand_to_mtd(chip);
  450. chip->read_byte = tango_read_byte;
  451. chip->write_buf = tango_write_buf;
  452. chip->read_buf = tango_read_buf;
  453. chip->select_chip = tango_select_chip;
  454. chip->cmd_ctrl = tango_cmd_ctrl;
  455. chip->dev_ready = tango_dev_ready;
  456. chip->setup_data_interface = tango_set_timings;
  457. chip->options = NAND_USE_BOUNCE_BUFFER |
  458. NAND_NO_SUBPAGE_WRITE |
  459. NAND_WAIT_TCCS;
  460. chip->controller = &nfc->hw;
  461. tchip->base = nfc->pbus_base + (cs * 256);
  462. nand_set_flash_node(chip, np);
  463. mtd_set_ooblayout(mtd, &tango_nand_ooblayout_ops);
  464. mtd->dev.parent = dev;
  465. err = nand_scan_ident(mtd, 1, NULL);
  466. if (err)
  467. return err;
  468. ecc->mode = NAND_ECC_HW;
  469. ecc->algo = NAND_ECC_BCH;
  470. ecc->bytes = DIV_ROUND_UP(ecc->strength * FIELD_ORDER, BITS_PER_BYTE);
  471. ecc->read_page_raw = tango_read_page_raw;
  472. ecc->write_page_raw = tango_write_page_raw;
  473. ecc->read_page = tango_read_page;
  474. ecc->write_page = tango_write_page;
  475. ecc->read_oob = tango_read_oob;
  476. ecc->write_oob = tango_write_oob;
  477. ecc->options = NAND_ECC_CUSTOM_PAGE_ACCESS;
  478. err = nand_scan_tail(mtd);
  479. if (err)
  480. return err;
  481. tchip->xfer_cfg = XFER_CFG(cs, 1, ecc->steps, METADATA_SIZE);
  482. tchip->pkt_0_cfg = PKT_CFG(ecc->size + METADATA_SIZE, ecc->strength);
  483. tchip->pkt_n_cfg = PKT_CFG(ecc->size, ecc->strength);
  484. tchip->bb_cfg = BB_CFG(mtd->writesize, BBM_SIZE);
  485. err = mtd_device_register(mtd, NULL, 0);
  486. if (err)
  487. return err;
  488. nfc->chips[cs] = tchip;
  489. return 0;
  490. }
  491. static int tango_nand_remove(struct platform_device *pdev)
  492. {
  493. int cs;
  494. struct tango_nfc *nfc = platform_get_drvdata(pdev);
  495. dma_release_channel(nfc->chan);
  496. for (cs = 0; cs < MAX_CS; ++cs) {
  497. if (nfc->chips[cs])
  498. nand_release(&nfc->chips[cs]->nand_chip);
  499. }
  500. return 0;
  501. }
  502. static int tango_nand_probe(struct platform_device *pdev)
  503. {
  504. int err;
  505. struct clk *clk;
  506. struct resource *res;
  507. struct tango_nfc *nfc;
  508. struct device_node *np;
  509. nfc = devm_kzalloc(&pdev->dev, sizeof(*nfc), GFP_KERNEL);
  510. if (!nfc)
  511. return -ENOMEM;
  512. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  513. nfc->reg_base = devm_ioremap_resource(&pdev->dev, res);
  514. if (IS_ERR(nfc->reg_base))
  515. return PTR_ERR(nfc->reg_base);
  516. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  517. nfc->mem_base = devm_ioremap_resource(&pdev->dev, res);
  518. if (IS_ERR(nfc->mem_base))
  519. return PTR_ERR(nfc->mem_base);
  520. res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  521. nfc->pbus_base = devm_ioremap_resource(&pdev->dev, res);
  522. if (IS_ERR(nfc->pbus_base))
  523. return PTR_ERR(nfc->pbus_base);
  524. writel_relaxed(MODE_RAW, nfc->pbus_base + PBUS_PAD_MODE);
  525. clk = devm_clk_get(&pdev->dev, NULL);
  526. if (IS_ERR(clk))
  527. return PTR_ERR(clk);
  528. nfc->chan = dma_request_chan(&pdev->dev, "rxtx");
  529. if (IS_ERR(nfc->chan))
  530. return PTR_ERR(nfc->chan);
  531. platform_set_drvdata(pdev, nfc);
  532. nand_hw_control_init(&nfc->hw);
  533. nfc->freq_kHz = clk_get_rate(clk) / 1000;
  534. for_each_child_of_node(pdev->dev.of_node, np) {
  535. err = chip_init(&pdev->dev, np);
  536. if (err) {
  537. tango_nand_remove(pdev);
  538. return err;
  539. }
  540. }
  541. return 0;
  542. }
  543. static const struct of_device_id tango_nand_ids[] = {
  544. { .compatible = "sigma,smp8758-nand" },
  545. { /* sentinel */ }
  546. };
  547. MODULE_DEVICE_TABLE(of, tango_nand_ids);
  548. static struct platform_driver tango_nand_driver = {
  549. .probe = tango_nand_probe,
  550. .remove = tango_nand_remove,
  551. .driver = {
  552. .name = "tango-nand",
  553. .of_match_table = tango_nand_ids,
  554. },
  555. };
  556. module_platform_driver(tango_nand_driver);
  557. MODULE_LICENSE("GPL");
  558. MODULE_AUTHOR("Sigma Designs");
  559. MODULE_DESCRIPTION("Tango4 NAND Flash controller driver");