tango_nand.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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_controller 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 nand_chip *chip, int dat, unsigned int ctrl)
  98. {
  99. struct tango_chip *tchip = to_tango_chip(chip);
  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 nand_chip *chip)
  106. {
  107. struct tango_nfc *nfc = to_tango_nfc(chip->controller);
  108. return readl_relaxed(nfc->pbus_base + PBUS_CS_CTRL) & PBUS_IORDY;
  109. }
  110. static u8 tango_read_byte(struct nand_chip *chip)
  111. {
  112. struct tango_chip *tchip = to_tango_chip(chip);
  113. return readb_relaxed(tchip->base + PBUS_DATA);
  114. }
  115. static void tango_read_buf(struct nand_chip *chip, u8 *buf, int len)
  116. {
  117. struct tango_chip *tchip = to_tango_chip(chip);
  118. ioread8_rep(tchip->base + PBUS_DATA, buf, len);
  119. }
  120. static void tango_write_buf(struct nand_chip *chip, const u8 *buf, int len)
  121. {
  122. struct tango_chip *tchip = to_tango_chip(chip);
  123. iowrite8_rep(tchip->base + PBUS_DATA, buf, len);
  124. }
  125. static void tango_select_chip(struct nand_chip *chip, int idx)
  126. {
  127. struct tango_nfc *nfc = to_tango_nfc(chip->controller);
  128. struct tango_chip *tchip = to_tango_chip(chip);
  129. if (idx < 0)
  130. return; /* No "chip unselect" function */
  131. writel_relaxed(tchip->timing1, nfc->reg_base + NFC_TIMING1);
  132. writel_relaxed(tchip->timing2, nfc->reg_base + NFC_TIMING2);
  133. writel_relaxed(tchip->xfer_cfg, nfc->reg_base + NFC_XFER_CFG);
  134. writel_relaxed(tchip->pkt_0_cfg, nfc->reg_base + NFC_PKT_0_CFG);
  135. writel_relaxed(tchip->pkt_n_cfg, nfc->reg_base + NFC_PKT_N_CFG);
  136. writel_relaxed(tchip->bb_cfg, nfc->reg_base + NFC_BB_CFG);
  137. }
  138. /*
  139. * The controller does not check for bitflips in erased pages,
  140. * therefore software must check instead.
  141. */
  142. static int check_erased_page(struct nand_chip *chip, u8 *buf)
  143. {
  144. struct mtd_info *mtd = nand_to_mtd(chip);
  145. u8 *meta = chip->oob_poi + BBM_SIZE;
  146. u8 *ecc = chip->oob_poi + BBM_SIZE + METADATA_SIZE;
  147. const int ecc_size = chip->ecc.bytes;
  148. const int pkt_size = chip->ecc.size;
  149. int i, res, meta_len, bitflips = 0;
  150. for (i = 0; i < chip->ecc.steps; ++i) {
  151. meta_len = i ? 0 : METADATA_SIZE;
  152. res = nand_check_erased_ecc_chunk(buf, pkt_size, ecc, ecc_size,
  153. meta, meta_len,
  154. chip->ecc.strength);
  155. if (res < 0)
  156. mtd->ecc_stats.failed++;
  157. else
  158. mtd->ecc_stats.corrected += res;
  159. bitflips = max(res, bitflips);
  160. buf += pkt_size;
  161. ecc += ecc_size;
  162. }
  163. return bitflips;
  164. }
  165. static int decode_error_report(struct nand_chip *chip)
  166. {
  167. u32 status, res;
  168. struct mtd_info *mtd = nand_to_mtd(chip);
  169. struct tango_nfc *nfc = to_tango_nfc(chip->controller);
  170. status = readl_relaxed(nfc->reg_base + NFC_XFER_STATUS);
  171. if (status & PAGE_IS_EMPTY)
  172. return 0;
  173. res = readl_relaxed(nfc->mem_base + ERROR_REPORT);
  174. if (DECODE_FAIL_PKT_0(res) || DECODE_FAIL_PKT_N(res))
  175. return -EBADMSG;
  176. /* ERR_COUNT_PKT_N is max, not sum, but that's all we have */
  177. mtd->ecc_stats.corrected +=
  178. ERR_COUNT_PKT_0(res) + ERR_COUNT_PKT_N(res);
  179. return max(ERR_COUNT_PKT_0(res), ERR_COUNT_PKT_N(res));
  180. }
  181. static void tango_dma_callback(void *arg)
  182. {
  183. complete(arg);
  184. }
  185. static int do_dma(struct tango_nfc *nfc, enum dma_data_direction dir, int cmd,
  186. const void *buf, int len, int page)
  187. {
  188. void __iomem *addr = nfc->reg_base + NFC_STATUS;
  189. struct dma_chan *chan = nfc->chan;
  190. struct dma_async_tx_descriptor *desc;
  191. enum dma_transfer_direction tdir;
  192. struct scatterlist sg;
  193. struct completion tx_done;
  194. int err = -EIO;
  195. u32 res, val;
  196. sg_init_one(&sg, buf, len);
  197. if (dma_map_sg(chan->device->dev, &sg, 1, dir) != 1)
  198. return -EIO;
  199. tdir = dir == DMA_TO_DEVICE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
  200. desc = dmaengine_prep_slave_sg(chan, &sg, 1, tdir, DMA_PREP_INTERRUPT);
  201. if (!desc)
  202. goto dma_unmap;
  203. desc->callback = tango_dma_callback;
  204. desc->callback_param = &tx_done;
  205. init_completion(&tx_done);
  206. writel_relaxed(MODE_NFC, nfc->pbus_base + PBUS_PAD_MODE);
  207. writel_relaxed(page, nfc->reg_base + NFC_ADDR_PAGE);
  208. writel_relaxed(0, nfc->reg_base + NFC_ADDR_OFFSET);
  209. writel_relaxed(cmd, nfc->reg_base + NFC_FLASH_CMD);
  210. dmaengine_submit(desc);
  211. dma_async_issue_pending(chan);
  212. res = wait_for_completion_timeout(&tx_done, HZ);
  213. if (res > 0)
  214. err = readl_poll_timeout(addr, val, val & CMD_READY, 0, 1000);
  215. writel_relaxed(MODE_RAW, nfc->pbus_base + PBUS_PAD_MODE);
  216. dma_unmap:
  217. dma_unmap_sg(chan->device->dev, &sg, 1, dir);
  218. return err;
  219. }
  220. static int tango_read_page(struct nand_chip *chip, u8 *buf,
  221. int oob_required, int page)
  222. {
  223. struct mtd_info *mtd = nand_to_mtd(chip);
  224. struct tango_nfc *nfc = to_tango_nfc(chip->controller);
  225. int err, res, len = mtd->writesize;
  226. if (oob_required)
  227. chip->ecc.read_oob(chip, page);
  228. err = do_dma(nfc, DMA_FROM_DEVICE, NFC_READ, buf, len, page);
  229. if (err)
  230. return err;
  231. res = decode_error_report(chip);
  232. if (res < 0) {
  233. chip->ecc.read_oob_raw(chip, page);
  234. res = check_erased_page(chip, buf);
  235. }
  236. return res;
  237. }
  238. static int tango_write_page(struct nand_chip *chip, const u8 *buf,
  239. int oob_required, int page)
  240. {
  241. struct mtd_info *mtd = nand_to_mtd(chip);
  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->legacy.waitfunc(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. *pos += len;
  259. if (!*buf) {
  260. /* skip over "len" bytes */
  261. nand_change_read_column_op(chip, *pos, NULL, 0, false);
  262. } else {
  263. tango_read_buf(chip, *buf, len);
  264. *buf += len;
  265. }
  266. }
  267. static void aux_write(struct nand_chip *chip, const u8 **buf, int len, int *pos)
  268. {
  269. *pos += len;
  270. if (!*buf) {
  271. /* skip over "len" bytes */
  272. nand_change_write_column_op(chip, *pos, NULL, 0, false);
  273. } else {
  274. tango_write_buf(chip, *buf, len);
  275. *buf += len;
  276. }
  277. }
  278. /*
  279. * Physical page layout (not drawn to scale)
  280. *
  281. * NB: Bad Block Marker area splits PKT_N in two (N1, N2).
  282. *
  283. * +---+-----------------+-------+-----+-----------+-----+----+-------+
  284. * | M | PKT_0 | ECC_0 | ... | N1 | BBM | N2 | ECC_N |
  285. * +---+-----------------+-------+-----+-----------+-----+----+-------+
  286. *
  287. * Logical page layout:
  288. *
  289. * +-----+---+-------+-----+-------+
  290. * oob = | BBM | M | ECC_0 | ... | ECC_N |
  291. * +-----+---+-------+-----+-------+
  292. *
  293. * +-----------------+-----+-----------------+
  294. * buf = | PKT_0 | ... | PKT_N |
  295. * +-----------------+-----+-----------------+
  296. */
  297. static void raw_read(struct nand_chip *chip, u8 *buf, u8 *oob)
  298. {
  299. struct mtd_info *mtd = nand_to_mtd(chip);
  300. u8 *oob_orig = oob;
  301. const int page_size = mtd->writesize;
  302. const int ecc_size = chip->ecc.bytes;
  303. const int pkt_size = chip->ecc.size;
  304. int pos = 0; /* position within physical page */
  305. int rem = page_size; /* bytes remaining until BBM area */
  306. if (oob)
  307. oob += BBM_SIZE;
  308. aux_read(chip, &oob, METADATA_SIZE, &pos);
  309. while (rem > pkt_size) {
  310. aux_read(chip, &buf, pkt_size, &pos);
  311. aux_read(chip, &oob, ecc_size, &pos);
  312. rem = page_size - pos;
  313. }
  314. aux_read(chip, &buf, rem, &pos);
  315. aux_read(chip, &oob_orig, BBM_SIZE, &pos);
  316. aux_read(chip, &buf, pkt_size - rem, &pos);
  317. aux_read(chip, &oob, ecc_size, &pos);
  318. }
  319. static void raw_write(struct nand_chip *chip, const u8 *buf, const u8 *oob)
  320. {
  321. struct mtd_info *mtd = nand_to_mtd(chip);
  322. const u8 *oob_orig = oob;
  323. const int page_size = mtd->writesize;
  324. const int ecc_size = chip->ecc.bytes;
  325. const int pkt_size = chip->ecc.size;
  326. int pos = 0; /* position within physical page */
  327. int rem = page_size; /* bytes remaining until BBM area */
  328. if (oob)
  329. oob += BBM_SIZE;
  330. aux_write(chip, &oob, METADATA_SIZE, &pos);
  331. while (rem > pkt_size) {
  332. aux_write(chip, &buf, pkt_size, &pos);
  333. aux_write(chip, &oob, ecc_size, &pos);
  334. rem = page_size - pos;
  335. }
  336. aux_write(chip, &buf, rem, &pos);
  337. aux_write(chip, &oob_orig, BBM_SIZE, &pos);
  338. aux_write(chip, &buf, pkt_size - rem, &pos);
  339. aux_write(chip, &oob, ecc_size, &pos);
  340. }
  341. static int tango_read_page_raw(struct nand_chip *chip, u8 *buf,
  342. int oob_required, int page)
  343. {
  344. nand_read_page_op(chip, page, 0, NULL, 0);
  345. raw_read(chip, buf, chip->oob_poi);
  346. return 0;
  347. }
  348. static int tango_write_page_raw(struct nand_chip *chip, const u8 *buf,
  349. int oob_required, int page)
  350. {
  351. nand_prog_page_begin_op(chip, page, 0, NULL, 0);
  352. raw_write(chip, buf, chip->oob_poi);
  353. return nand_prog_page_end_op(chip);
  354. }
  355. static int tango_read_oob(struct nand_chip *chip, int page)
  356. {
  357. nand_read_page_op(chip, page, 0, NULL, 0);
  358. raw_read(chip, NULL, chip->oob_poi);
  359. return 0;
  360. }
  361. static int tango_write_oob(struct nand_chip *chip, int page)
  362. {
  363. nand_prog_page_begin_op(chip, page, 0, NULL, 0);
  364. raw_write(chip, NULL, chip->oob_poi);
  365. return nand_prog_page_end_op(chip);
  366. }
  367. static int oob_ecc(struct mtd_info *mtd, int idx, struct mtd_oob_region *res)
  368. {
  369. struct nand_chip *chip = mtd_to_nand(mtd);
  370. struct nand_ecc_ctrl *ecc = &chip->ecc;
  371. if (idx >= ecc->steps)
  372. return -ERANGE;
  373. res->offset = BBM_SIZE + METADATA_SIZE + ecc->bytes * idx;
  374. res->length = ecc->bytes;
  375. return 0;
  376. }
  377. static int oob_free(struct mtd_info *mtd, int idx, struct mtd_oob_region *res)
  378. {
  379. return -ERANGE; /* no free space in spare area */
  380. }
  381. static const struct mtd_ooblayout_ops tango_nand_ooblayout_ops = {
  382. .ecc = oob_ecc,
  383. .free = oob_free,
  384. };
  385. static u32 to_ticks(int kHz, int ps)
  386. {
  387. return DIV_ROUND_UP_ULL((u64)kHz * ps, NSEC_PER_SEC);
  388. }
  389. static int tango_set_timings(struct nand_chip *chip, int csline,
  390. const struct nand_data_interface *conf)
  391. {
  392. const struct nand_sdr_timings *sdr = nand_get_sdr_timings(conf);
  393. struct tango_nfc *nfc = to_tango_nfc(chip->controller);
  394. struct tango_chip *tchip = to_tango_chip(chip);
  395. u32 Trdy, Textw, Twc, Twpw, Tacc, Thold, Trpw, Textr;
  396. int kHz = nfc->freq_kHz;
  397. if (IS_ERR(sdr))
  398. return PTR_ERR(sdr);
  399. if (csline == NAND_DATA_IFACE_CHECK_ONLY)
  400. return 0;
  401. Trdy = to_ticks(kHz, sdr->tCEA_max - sdr->tREA_max);
  402. Textw = to_ticks(kHz, sdr->tWB_max);
  403. Twc = to_ticks(kHz, sdr->tWC_min);
  404. Twpw = to_ticks(kHz, sdr->tWC_min - sdr->tWP_min);
  405. Tacc = to_ticks(kHz, sdr->tREA_max);
  406. Thold = to_ticks(kHz, sdr->tREH_min);
  407. Trpw = to_ticks(kHz, sdr->tRC_min - sdr->tREH_min);
  408. Textr = to_ticks(kHz, sdr->tRHZ_max);
  409. tchip->timing1 = TIMING(Trdy, Textw, Twc, Twpw);
  410. tchip->timing2 = TIMING(Tacc, Thold, Trpw, Textr);
  411. return 0;
  412. }
  413. static int tango_attach_chip(struct nand_chip *chip)
  414. {
  415. struct nand_ecc_ctrl *ecc = &chip->ecc;
  416. ecc->mode = NAND_ECC_HW;
  417. ecc->algo = NAND_ECC_BCH;
  418. ecc->bytes = DIV_ROUND_UP(ecc->strength * FIELD_ORDER, BITS_PER_BYTE);
  419. ecc->read_page_raw = tango_read_page_raw;
  420. ecc->write_page_raw = tango_write_page_raw;
  421. ecc->read_page = tango_read_page;
  422. ecc->write_page = tango_write_page;
  423. ecc->read_oob = tango_read_oob;
  424. ecc->write_oob = tango_write_oob;
  425. return 0;
  426. }
  427. static const struct nand_controller_ops tango_controller_ops = {
  428. .attach_chip = tango_attach_chip,
  429. };
  430. static int chip_init(struct device *dev, struct device_node *np)
  431. {
  432. u32 cs;
  433. int err, res;
  434. struct mtd_info *mtd;
  435. struct nand_chip *chip;
  436. struct tango_chip *tchip;
  437. struct nand_ecc_ctrl *ecc;
  438. struct tango_nfc *nfc = dev_get_drvdata(dev);
  439. tchip = devm_kzalloc(dev, sizeof(*tchip), GFP_KERNEL);
  440. if (!tchip)
  441. return -ENOMEM;
  442. res = of_property_count_u32_elems(np, "reg");
  443. if (res < 0)
  444. return res;
  445. if (res != 1)
  446. return -ENOTSUPP; /* Multi-CS chips are not supported */
  447. err = of_property_read_u32_index(np, "reg", 0, &cs);
  448. if (err)
  449. return err;
  450. if (cs >= MAX_CS)
  451. return -EINVAL;
  452. chip = &tchip->nand_chip;
  453. ecc = &chip->ecc;
  454. mtd = nand_to_mtd(chip);
  455. chip->legacy.read_byte = tango_read_byte;
  456. chip->legacy.write_buf = tango_write_buf;
  457. chip->legacy.read_buf = tango_read_buf;
  458. chip->select_chip = tango_select_chip;
  459. chip->legacy.cmd_ctrl = tango_cmd_ctrl;
  460. chip->legacy.dev_ready = tango_dev_ready;
  461. chip->setup_data_interface = tango_set_timings;
  462. chip->options = NAND_USE_BOUNCE_BUFFER |
  463. NAND_NO_SUBPAGE_WRITE |
  464. NAND_WAIT_TCCS;
  465. chip->controller = &nfc->hw;
  466. tchip->base = nfc->pbus_base + (cs * 256);
  467. nand_set_flash_node(chip, np);
  468. mtd_set_ooblayout(mtd, &tango_nand_ooblayout_ops);
  469. mtd->dev.parent = dev;
  470. err = nand_scan(chip, 1);
  471. if (err)
  472. return err;
  473. tchip->xfer_cfg = XFER_CFG(cs, 1, ecc->steps, METADATA_SIZE);
  474. tchip->pkt_0_cfg = PKT_CFG(ecc->size + METADATA_SIZE, ecc->strength);
  475. tchip->pkt_n_cfg = PKT_CFG(ecc->size, ecc->strength);
  476. tchip->bb_cfg = BB_CFG(mtd->writesize, BBM_SIZE);
  477. err = mtd_device_register(mtd, NULL, 0);
  478. if (err) {
  479. nand_cleanup(chip);
  480. return err;
  481. }
  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(&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 = devm_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_controller_init(&nfc->hw);
  527. nfc->hw.ops = &tango_controller_ops;
  528. nfc->freq_kHz = clk_get_rate(clk) / 1000;
  529. for_each_child_of_node(pdev->dev.of_node, np) {
  530. err = chip_init(&pdev->dev, np);
  531. if (err) {
  532. tango_nand_remove(pdev);
  533. return err;
  534. }
  535. }
  536. return 0;
  537. }
  538. static const struct of_device_id tango_nand_ids[] = {
  539. { .compatible = "sigma,smp8758-nand" },
  540. { /* sentinel */ }
  541. };
  542. MODULE_DEVICE_TABLE(of, tango_nand_ids);
  543. static struct platform_driver tango_nand_driver = {
  544. .probe = tango_nand_probe,
  545. .remove = tango_nand_remove,
  546. .driver = {
  547. .name = "tango-nand",
  548. .of_match_table = tango_nand_ids,
  549. },
  550. };
  551. module_platform_driver(tango_nand_driver);
  552. MODULE_LICENSE("GPL");
  553. MODULE_AUTHOR("Sigma Designs");
  554. MODULE_DESCRIPTION("Tango4 NAND Flash controller driver");