tango_nand.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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/nand.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, 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. return 0;
  252. }
  253. static void aux_read(struct nand_chip *chip, u8 **buf, int len, int *pos)
  254. {
  255. struct mtd_info *mtd = nand_to_mtd(chip);
  256. *pos += len;
  257. if (!*buf) {
  258. /* skip over "len" bytes */
  259. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, *pos, -1);
  260. } else {
  261. tango_read_buf(mtd, *buf, len);
  262. *buf += len;
  263. }
  264. }
  265. static void aux_write(struct nand_chip *chip, const u8 **buf, int len, int *pos)
  266. {
  267. struct mtd_info *mtd = nand_to_mtd(chip);
  268. *pos += len;
  269. if (!*buf) {
  270. /* skip over "len" bytes */
  271. chip->cmdfunc(mtd, NAND_CMD_SEQIN, *pos, -1);
  272. } else {
  273. tango_write_buf(mtd, *buf, len);
  274. *buf += len;
  275. }
  276. }
  277. /*
  278. * Physical page layout (not drawn to scale)
  279. *
  280. * NB: Bad Block Marker area splits PKT_N in two (N1, N2).
  281. *
  282. * +---+-----------------+-------+-----+-----------+-----+----+-------+
  283. * | M | PKT_0 | ECC_0 | ... | N1 | BBM | N2 | ECC_N |
  284. * +---+-----------------+-------+-----+-----------+-----+----+-------+
  285. *
  286. * Logical page layout:
  287. *
  288. * +-----+---+-------+-----+-------+
  289. * oob = | BBM | M | ECC_0 | ... | ECC_N |
  290. * +-----+---+-------+-----+-------+
  291. *
  292. * +-----------------+-----+-----------------+
  293. * buf = | PKT_0 | ... | PKT_N |
  294. * +-----------------+-----+-----------------+
  295. */
  296. static void raw_read(struct nand_chip *chip, u8 *buf, u8 *oob)
  297. {
  298. struct mtd_info *mtd = nand_to_mtd(chip);
  299. u8 *oob_orig = oob;
  300. const int page_size = mtd->writesize;
  301. const int ecc_size = chip->ecc.bytes;
  302. const int pkt_size = chip->ecc.size;
  303. int pos = 0; /* position within physical page */
  304. int rem = page_size; /* bytes remaining until BBM area */
  305. if (oob)
  306. oob += BBM_SIZE;
  307. aux_read(chip, &oob, METADATA_SIZE, &pos);
  308. while (rem > pkt_size) {
  309. aux_read(chip, &buf, pkt_size, &pos);
  310. aux_read(chip, &oob, ecc_size, &pos);
  311. rem = page_size - pos;
  312. }
  313. aux_read(chip, &buf, rem, &pos);
  314. aux_read(chip, &oob_orig, BBM_SIZE, &pos);
  315. aux_read(chip, &buf, pkt_size - rem, &pos);
  316. aux_read(chip, &oob, ecc_size, &pos);
  317. }
  318. static void raw_write(struct nand_chip *chip, const u8 *buf, const u8 *oob)
  319. {
  320. struct mtd_info *mtd = nand_to_mtd(chip);
  321. const u8 *oob_orig = oob;
  322. const int page_size = mtd->writesize;
  323. const int ecc_size = chip->ecc.bytes;
  324. const int pkt_size = chip->ecc.size;
  325. int pos = 0; /* position within physical page */
  326. int rem = page_size; /* bytes remaining until BBM area */
  327. if (oob)
  328. oob += BBM_SIZE;
  329. aux_write(chip, &oob, METADATA_SIZE, &pos);
  330. while (rem > pkt_size) {
  331. aux_write(chip, &buf, pkt_size, &pos);
  332. aux_write(chip, &oob, ecc_size, &pos);
  333. rem = page_size - pos;
  334. }
  335. aux_write(chip, &buf, rem, &pos);
  336. aux_write(chip, &oob_orig, BBM_SIZE, &pos);
  337. aux_write(chip, &buf, pkt_size - rem, &pos);
  338. aux_write(chip, &oob, ecc_size, &pos);
  339. }
  340. static int tango_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  341. u8 *buf, int oob_required, int page)
  342. {
  343. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  344. raw_read(chip, buf, chip->oob_poi);
  345. return 0;
  346. }
  347. static int tango_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  348. const u8 *buf, int oob_required, int page)
  349. {
  350. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0, page);
  351. raw_write(chip, buf, chip->oob_poi);
  352. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  353. return 0;
  354. }
  355. static int tango_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
  356. int page)
  357. {
  358. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  359. raw_read(chip, NULL, chip->oob_poi);
  360. return 0;
  361. }
  362. static int tango_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
  363. int page)
  364. {
  365. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0, page);
  366. raw_write(chip, NULL, chip->oob_poi);
  367. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  368. chip->waitfunc(mtd, chip);
  369. return 0;
  370. }
  371. static int oob_ecc(struct mtd_info *mtd, int idx, struct mtd_oob_region *res)
  372. {
  373. struct nand_chip *chip = mtd_to_nand(mtd);
  374. struct nand_ecc_ctrl *ecc = &chip->ecc;
  375. if (idx >= ecc->steps)
  376. return -ERANGE;
  377. res->offset = BBM_SIZE + METADATA_SIZE + ecc->bytes * idx;
  378. res->length = ecc->bytes;
  379. return 0;
  380. }
  381. static int oob_free(struct mtd_info *mtd, int idx, struct mtd_oob_region *res)
  382. {
  383. return -ERANGE; /* no free space in spare area */
  384. }
  385. static const struct mtd_ooblayout_ops tango_nand_ooblayout_ops = {
  386. .ecc = oob_ecc,
  387. .free = oob_free,
  388. };
  389. static u32 to_ticks(int kHz, int ps)
  390. {
  391. return DIV_ROUND_UP_ULL((u64)kHz * ps, NSEC_PER_SEC);
  392. }
  393. static int tango_set_timings(struct mtd_info *mtd,
  394. const struct nand_data_interface *conf,
  395. bool check_only)
  396. {
  397. const struct nand_sdr_timings *sdr = nand_get_sdr_timings(conf);
  398. struct nand_chip *chip = mtd_to_nand(mtd);
  399. struct tango_nfc *nfc = to_tango_nfc(chip->controller);
  400. struct tango_chip *tchip = to_tango_chip(chip);
  401. u32 Trdy, Textw, Twc, Twpw, Tacc, Thold, Trpw, Textr;
  402. int kHz = nfc->freq_kHz;
  403. if (IS_ERR(sdr))
  404. return PTR_ERR(sdr);
  405. if (check_only)
  406. return 0;
  407. Trdy = to_ticks(kHz, sdr->tCEA_max - sdr->tREA_max);
  408. Textw = to_ticks(kHz, sdr->tWB_max);
  409. Twc = to_ticks(kHz, sdr->tWC_min);
  410. Twpw = to_ticks(kHz, sdr->tWC_min - sdr->tWP_min);
  411. Tacc = to_ticks(kHz, sdr->tREA_max);
  412. Thold = to_ticks(kHz, sdr->tREH_min);
  413. Trpw = to_ticks(kHz, sdr->tRC_min - sdr->tREH_min);
  414. Textr = to_ticks(kHz, sdr->tRHZ_max);
  415. tchip->timing1 = TIMING(Trdy, Textw, Twc, Twpw);
  416. tchip->timing2 = TIMING(Tacc, Thold, Trpw, Textr);
  417. return 0;
  418. }
  419. static int chip_init(struct device *dev, struct device_node *np)
  420. {
  421. u32 cs;
  422. int err, res;
  423. struct mtd_info *mtd;
  424. struct nand_chip *chip;
  425. struct tango_chip *tchip;
  426. struct nand_ecc_ctrl *ecc;
  427. struct tango_nfc *nfc = dev_get_drvdata(dev);
  428. tchip = devm_kzalloc(dev, sizeof(*tchip), GFP_KERNEL);
  429. if (!tchip)
  430. return -ENOMEM;
  431. res = of_property_count_u32_elems(np, "reg");
  432. if (res < 0)
  433. return res;
  434. if (res != 1)
  435. return -ENOTSUPP; /* Multi-CS chips are not supported */
  436. err = of_property_read_u32_index(np, "reg", 0, &cs);
  437. if (err)
  438. return err;
  439. if (cs >= MAX_CS)
  440. return -EINVAL;
  441. chip = &tchip->nand_chip;
  442. ecc = &chip->ecc;
  443. mtd = nand_to_mtd(chip);
  444. chip->read_byte = tango_read_byte;
  445. chip->write_buf = tango_write_buf;
  446. chip->read_buf = tango_read_buf;
  447. chip->select_chip = tango_select_chip;
  448. chip->cmd_ctrl = tango_cmd_ctrl;
  449. chip->dev_ready = tango_dev_ready;
  450. chip->setup_data_interface = tango_set_timings;
  451. chip->options = NAND_USE_BOUNCE_BUFFER |
  452. NAND_NO_SUBPAGE_WRITE |
  453. NAND_WAIT_TCCS;
  454. chip->controller = &nfc->hw;
  455. tchip->base = nfc->pbus_base + (cs * 256);
  456. nand_set_flash_node(chip, np);
  457. mtd_set_ooblayout(mtd, &tango_nand_ooblayout_ops);
  458. mtd->dev.parent = dev;
  459. err = nand_scan_ident(mtd, 1, NULL);
  460. if (err)
  461. return err;
  462. ecc->mode = NAND_ECC_HW;
  463. ecc->algo = NAND_ECC_BCH;
  464. ecc->bytes = DIV_ROUND_UP(ecc->strength * FIELD_ORDER, BITS_PER_BYTE);
  465. ecc->read_page_raw = tango_read_page_raw;
  466. ecc->write_page_raw = tango_write_page_raw;
  467. ecc->read_page = tango_read_page;
  468. ecc->write_page = tango_write_page;
  469. ecc->read_oob = tango_read_oob;
  470. ecc->write_oob = tango_write_oob;
  471. ecc->options = NAND_ECC_CUSTOM_PAGE_ACCESS;
  472. err = nand_scan_tail(mtd);
  473. if (err)
  474. return err;
  475. tchip->xfer_cfg = XFER_CFG(cs, 1, ecc->steps, METADATA_SIZE);
  476. tchip->pkt_0_cfg = PKT_CFG(ecc->size + METADATA_SIZE, ecc->strength);
  477. tchip->pkt_n_cfg = PKT_CFG(ecc->size, ecc->strength);
  478. tchip->bb_cfg = BB_CFG(mtd->writesize, BBM_SIZE);
  479. err = mtd_device_register(mtd, NULL, 0);
  480. if (err)
  481. return err;
  482. nfc->chips[cs] = tchip;
  483. return 0;
  484. }
  485. static int tango_nand_remove(struct platform_device *pdev)
  486. {
  487. int cs;
  488. struct tango_nfc *nfc = platform_get_drvdata(pdev);
  489. dma_release_channel(nfc->chan);
  490. for (cs = 0; cs < MAX_CS; ++cs) {
  491. if (nfc->chips[cs])
  492. nand_release(nand_to_mtd(&nfc->chips[cs]->nand_chip));
  493. }
  494. return 0;
  495. }
  496. static int tango_nand_probe(struct platform_device *pdev)
  497. {
  498. int err;
  499. struct clk *clk;
  500. struct resource *res;
  501. struct tango_nfc *nfc;
  502. struct device_node *np;
  503. nfc = devm_kzalloc(&pdev->dev, sizeof(*nfc), GFP_KERNEL);
  504. if (!nfc)
  505. return -ENOMEM;
  506. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  507. nfc->reg_base = devm_ioremap_resource(&pdev->dev, res);
  508. if (IS_ERR(nfc->reg_base))
  509. return PTR_ERR(nfc->reg_base);
  510. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  511. nfc->mem_base = devm_ioremap_resource(&pdev->dev, res);
  512. if (IS_ERR(nfc->mem_base))
  513. return PTR_ERR(nfc->mem_base);
  514. res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  515. nfc->pbus_base = devm_ioremap_resource(&pdev->dev, res);
  516. if (IS_ERR(nfc->pbus_base))
  517. return PTR_ERR(nfc->pbus_base);
  518. writel_relaxed(MODE_RAW, nfc->pbus_base + PBUS_PAD_MODE);
  519. clk = clk_get(&pdev->dev, NULL);
  520. if (IS_ERR(clk))
  521. return PTR_ERR(clk);
  522. nfc->chan = dma_request_chan(&pdev->dev, "rxtx");
  523. if (IS_ERR(nfc->chan))
  524. return PTR_ERR(nfc->chan);
  525. platform_set_drvdata(pdev, nfc);
  526. nand_hw_control_init(&nfc->hw);
  527. nfc->freq_kHz = clk_get_rate(clk) / 1000;
  528. for_each_child_of_node(pdev->dev.of_node, np) {
  529. err = chip_init(&pdev->dev, np);
  530. if (err) {
  531. tango_nand_remove(pdev);
  532. return err;
  533. }
  534. }
  535. return 0;
  536. }
  537. static const struct of_device_id tango_nand_ids[] = {
  538. { .compatible = "sigma,smp8758-nand" },
  539. { /* sentinel */ }
  540. };
  541. MODULE_DEVICE_TABLE(of, tango_nand_ids);
  542. static struct platform_driver tango_nand_driver = {
  543. .probe = tango_nand_probe,
  544. .remove = tango_nand_remove,
  545. .driver = {
  546. .name = "tango-nand",
  547. .of_match_table = tango_nand_ids,
  548. },
  549. };
  550. module_platform_driver(tango_nand_driver);
  551. MODULE_LICENSE("GPL");
  552. MODULE_AUTHOR("Sigma Designs");
  553. MODULE_DESCRIPTION("Tango4 NAND Flash controller driver");