tango_nand.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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 DECODE_OK_PKT_0(v) ((v) & BIT(7))
  51. #define DECODE_OK_PKT_N(v) ((v) & BIT(15))
  52. #define ERR_COUNT_PKT_0(v) (((v) >> 0) & 0x3f)
  53. #define ERR_COUNT_PKT_N(v) (((v) >> 8) & 0x3f)
  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. bitflips = max(res, bitflips);
  160. buf += pkt_size;
  161. ecc += ecc_size;
  162. }
  163. return bitflips;
  164. }
  165. static int decode_error_report(struct tango_nfc *nfc)
  166. {
  167. u32 status, res;
  168. status = readl_relaxed(nfc->reg_base + NFC_XFER_STATUS);
  169. if (status & PAGE_IS_EMPTY)
  170. return 0;
  171. res = readl_relaxed(nfc->mem_base + ERROR_REPORT);
  172. if (DECODE_OK_PKT_0(res) && DECODE_OK_PKT_N(res))
  173. return max(ERR_COUNT_PKT_0(res), ERR_COUNT_PKT_N(res));
  174. return -EBADMSG;
  175. }
  176. static void tango_dma_callback(void *arg)
  177. {
  178. complete(arg);
  179. }
  180. static int do_dma(struct tango_nfc *nfc, int dir, int cmd, const void *buf,
  181. int len, int page)
  182. {
  183. void __iomem *addr = nfc->reg_base + NFC_STATUS;
  184. struct dma_chan *chan = nfc->chan;
  185. struct dma_async_tx_descriptor *desc;
  186. struct scatterlist sg;
  187. struct completion tx_done;
  188. int err = -EIO;
  189. u32 res, val;
  190. sg_init_one(&sg, buf, len);
  191. if (dma_map_sg(chan->device->dev, &sg, 1, dir) != 1)
  192. return -EIO;
  193. desc = dmaengine_prep_slave_sg(chan, &sg, 1, dir, DMA_PREP_INTERRUPT);
  194. if (!desc)
  195. goto dma_unmap;
  196. desc->callback = tango_dma_callback;
  197. desc->callback_param = &tx_done;
  198. init_completion(&tx_done);
  199. writel_relaxed(MODE_NFC, nfc->pbus_base + PBUS_PAD_MODE);
  200. writel_relaxed(page, nfc->reg_base + NFC_ADDR_PAGE);
  201. writel_relaxed(0, nfc->reg_base + NFC_ADDR_OFFSET);
  202. writel_relaxed(cmd, nfc->reg_base + NFC_FLASH_CMD);
  203. dmaengine_submit(desc);
  204. dma_async_issue_pending(chan);
  205. res = wait_for_completion_timeout(&tx_done, HZ);
  206. if (res > 0)
  207. err = readl_poll_timeout(addr, val, val & CMD_READY, 0, 1000);
  208. writel_relaxed(MODE_RAW, nfc->pbus_base + PBUS_PAD_MODE);
  209. dma_unmap:
  210. dma_unmap_sg(chan->device->dev, &sg, 1, dir);
  211. return err;
  212. }
  213. static int tango_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  214. u8 *buf, int oob_required, int page)
  215. {
  216. struct tango_nfc *nfc = to_tango_nfc(chip->controller);
  217. int err, res, len = mtd->writesize;
  218. if (oob_required)
  219. chip->ecc.read_oob(mtd, chip, page);
  220. err = do_dma(nfc, DMA_FROM_DEVICE, NFC_READ, buf, len, page);
  221. if (err)
  222. return err;
  223. res = decode_error_report(nfc);
  224. if (res < 0) {
  225. chip->ecc.read_oob_raw(mtd, chip, page);
  226. res = check_erased_page(chip, buf);
  227. }
  228. return res;
  229. }
  230. static int tango_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  231. const u8 *buf, int oob_required, int page)
  232. {
  233. struct tango_nfc *nfc = to_tango_nfc(chip->controller);
  234. int err, len = mtd->writesize;
  235. /* Calling tango_write_oob() would send PAGEPROG twice */
  236. if (oob_required)
  237. return -ENOTSUPP;
  238. writel_relaxed(0xffffffff, nfc->mem_base + METADATA);
  239. err = do_dma(nfc, DMA_TO_DEVICE, NFC_WRITE, buf, len, page);
  240. if (err)
  241. return err;
  242. return 0;
  243. }
  244. static void aux_read(struct nand_chip *chip, u8 **buf, int len, int *pos)
  245. {
  246. struct mtd_info *mtd = nand_to_mtd(chip);
  247. *pos += len;
  248. if (!*buf) {
  249. /* skip over "len" bytes */
  250. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, *pos, -1);
  251. } else {
  252. tango_read_buf(mtd, *buf, len);
  253. *buf += len;
  254. }
  255. }
  256. static void aux_write(struct nand_chip *chip, const 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_SEQIN, *pos, -1);
  263. } else {
  264. tango_write_buf(mtd, *buf, len);
  265. *buf += len;
  266. }
  267. }
  268. /*
  269. * Physical page layout (not drawn to scale)
  270. *
  271. * NB: Bad Block Marker area splits PKT_N in two (N1, N2).
  272. *
  273. * +---+-----------------+-------+-----+-----------+-----+----+-------+
  274. * | M | PKT_0 | ECC_0 | ... | N1 | BBM | N2 | ECC_N |
  275. * +---+-----------------+-------+-----+-----------+-----+----+-------+
  276. *
  277. * Logical page layout:
  278. *
  279. * +-----+---+-------+-----+-------+
  280. * oob = | BBM | M | ECC_0 | ... | ECC_N |
  281. * +-----+---+-------+-----+-------+
  282. *
  283. * +-----------------+-----+-----------------+
  284. * buf = | PKT_0 | ... | PKT_N |
  285. * +-----------------+-----+-----------------+
  286. */
  287. static void raw_read(struct nand_chip *chip, u8 *buf, u8 *oob)
  288. {
  289. struct mtd_info *mtd = nand_to_mtd(chip);
  290. u8 *oob_orig = oob;
  291. const int page_size = mtd->writesize;
  292. const int ecc_size = chip->ecc.bytes;
  293. const int pkt_size = chip->ecc.size;
  294. int pos = 0; /* position within physical page */
  295. int rem = page_size; /* bytes remaining until BBM area */
  296. if (oob)
  297. oob += BBM_SIZE;
  298. aux_read(chip, &oob, METADATA_SIZE, &pos);
  299. while (rem > pkt_size) {
  300. aux_read(chip, &buf, pkt_size, &pos);
  301. aux_read(chip, &oob, ecc_size, &pos);
  302. rem = page_size - pos;
  303. }
  304. aux_read(chip, &buf, rem, &pos);
  305. aux_read(chip, &oob_orig, BBM_SIZE, &pos);
  306. aux_read(chip, &buf, pkt_size - rem, &pos);
  307. aux_read(chip, &oob, ecc_size, &pos);
  308. }
  309. static void raw_write(struct nand_chip *chip, const u8 *buf, const u8 *oob)
  310. {
  311. struct mtd_info *mtd = nand_to_mtd(chip);
  312. const u8 *oob_orig = oob;
  313. const int page_size = mtd->writesize;
  314. const int ecc_size = chip->ecc.bytes;
  315. const int pkt_size = chip->ecc.size;
  316. int pos = 0; /* position within physical page */
  317. int rem = page_size; /* bytes remaining until BBM area */
  318. if (oob)
  319. oob += BBM_SIZE;
  320. aux_write(chip, &oob, METADATA_SIZE, &pos);
  321. while (rem > pkt_size) {
  322. aux_write(chip, &buf, pkt_size, &pos);
  323. aux_write(chip, &oob, ecc_size, &pos);
  324. rem = page_size - pos;
  325. }
  326. aux_write(chip, &buf, rem, &pos);
  327. aux_write(chip, &oob_orig, BBM_SIZE, &pos);
  328. aux_write(chip, &buf, pkt_size - rem, &pos);
  329. aux_write(chip, &oob, ecc_size, &pos);
  330. }
  331. static int tango_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  332. u8 *buf, int oob_required, int page)
  333. {
  334. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  335. raw_read(chip, buf, chip->oob_poi);
  336. return 0;
  337. }
  338. static int tango_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  339. const u8 *buf, int oob_required, int page)
  340. {
  341. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0, page);
  342. raw_write(chip, buf, chip->oob_poi);
  343. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  344. return 0;
  345. }
  346. static int tango_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
  347. int page)
  348. {
  349. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  350. raw_read(chip, NULL, chip->oob_poi);
  351. return 0;
  352. }
  353. static int tango_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
  354. int page)
  355. {
  356. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0, page);
  357. raw_write(chip, NULL, chip->oob_poi);
  358. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  359. chip->waitfunc(mtd, chip);
  360. return 0;
  361. }
  362. static int oob_ecc(struct mtd_info *mtd, int idx, struct mtd_oob_region *res)
  363. {
  364. struct nand_chip *chip = mtd_to_nand(mtd);
  365. struct nand_ecc_ctrl *ecc = &chip->ecc;
  366. if (idx >= ecc->steps)
  367. return -ERANGE;
  368. res->offset = BBM_SIZE + METADATA_SIZE + ecc->bytes * idx;
  369. res->length = ecc->bytes;
  370. return 0;
  371. }
  372. static int oob_free(struct mtd_info *mtd, int idx, struct mtd_oob_region *res)
  373. {
  374. return -ERANGE; /* no free space in spare area */
  375. }
  376. static const struct mtd_ooblayout_ops tango_nand_ooblayout_ops = {
  377. .ecc = oob_ecc,
  378. .free = oob_free,
  379. };
  380. static u32 to_ticks(int kHz, int ps)
  381. {
  382. return DIV_ROUND_UP_ULL((u64)kHz * ps, NSEC_PER_SEC);
  383. }
  384. static int tango_set_timings(struct mtd_info *mtd,
  385. const struct nand_data_interface *conf,
  386. bool check_only)
  387. {
  388. const struct nand_sdr_timings *sdr = nand_get_sdr_timings(conf);
  389. struct nand_chip *chip = mtd_to_nand(mtd);
  390. struct tango_nfc *nfc = to_tango_nfc(chip->controller);
  391. struct tango_chip *tchip = to_tango_chip(chip);
  392. u32 Trdy, Textw, Twc, Twpw, Tacc, Thold, Trpw, Textr;
  393. int kHz = nfc->freq_kHz;
  394. if (IS_ERR(sdr))
  395. return PTR_ERR(sdr);
  396. if (check_only)
  397. return 0;
  398. Trdy = to_ticks(kHz, sdr->tCEA_max - sdr->tREA_max);
  399. Textw = to_ticks(kHz, sdr->tWB_max);
  400. Twc = to_ticks(kHz, sdr->tWC_min);
  401. Twpw = to_ticks(kHz, sdr->tWC_min - sdr->tWP_min);
  402. Tacc = to_ticks(kHz, sdr->tREA_max);
  403. Thold = to_ticks(kHz, sdr->tREH_min);
  404. Trpw = to_ticks(kHz, sdr->tRC_min - sdr->tREH_min);
  405. Textr = to_ticks(kHz, sdr->tRHZ_max);
  406. tchip->timing1 = TIMING(Trdy, Textw, Twc, Twpw);
  407. tchip->timing2 = TIMING(Tacc, Thold, Trpw, Textr);
  408. return 0;
  409. }
  410. static int chip_init(struct device *dev, struct device_node *np)
  411. {
  412. u32 cs;
  413. int err, res;
  414. struct mtd_info *mtd;
  415. struct nand_chip *chip;
  416. struct tango_chip *tchip;
  417. struct nand_ecc_ctrl *ecc;
  418. struct tango_nfc *nfc = dev_get_drvdata(dev);
  419. tchip = devm_kzalloc(dev, sizeof(*tchip), GFP_KERNEL);
  420. if (!tchip)
  421. return -ENOMEM;
  422. res = of_property_count_u32_elems(np, "reg");
  423. if (res < 0)
  424. return res;
  425. if (res != 1)
  426. return -ENOTSUPP; /* Multi-CS chips are not supported */
  427. err = of_property_read_u32_index(np, "reg", 0, &cs);
  428. if (err)
  429. return err;
  430. if (cs >= MAX_CS)
  431. return -EINVAL;
  432. chip = &tchip->nand_chip;
  433. ecc = &chip->ecc;
  434. mtd = nand_to_mtd(chip);
  435. chip->read_byte = tango_read_byte;
  436. chip->write_buf = tango_write_buf;
  437. chip->read_buf = tango_read_buf;
  438. chip->select_chip = tango_select_chip;
  439. chip->cmd_ctrl = tango_cmd_ctrl;
  440. chip->dev_ready = tango_dev_ready;
  441. chip->setup_data_interface = tango_set_timings;
  442. chip->options = NAND_USE_BOUNCE_BUFFER |
  443. NAND_NO_SUBPAGE_WRITE |
  444. NAND_WAIT_TCCS;
  445. chip->controller = &nfc->hw;
  446. tchip->base = nfc->pbus_base + (cs * 256);
  447. nand_set_flash_node(chip, np);
  448. mtd_set_ooblayout(mtd, &tango_nand_ooblayout_ops);
  449. mtd->dev.parent = dev;
  450. err = nand_scan_ident(mtd, 1, NULL);
  451. if (err)
  452. return err;
  453. ecc->mode = NAND_ECC_HW;
  454. ecc->algo = NAND_ECC_BCH;
  455. ecc->bytes = DIV_ROUND_UP(ecc->strength * FIELD_ORDER, BITS_PER_BYTE);
  456. ecc->read_page_raw = tango_read_page_raw;
  457. ecc->write_page_raw = tango_write_page_raw;
  458. ecc->read_page = tango_read_page;
  459. ecc->write_page = tango_write_page;
  460. ecc->read_oob = tango_read_oob;
  461. ecc->write_oob = tango_write_oob;
  462. ecc->options = NAND_ECC_CUSTOM_PAGE_ACCESS;
  463. err = nand_scan_tail(mtd);
  464. if (err)
  465. return err;
  466. tchip->xfer_cfg = XFER_CFG(cs, 1, ecc->steps, METADATA_SIZE);
  467. tchip->pkt_0_cfg = PKT_CFG(ecc->size + METADATA_SIZE, ecc->strength);
  468. tchip->pkt_n_cfg = PKT_CFG(ecc->size, ecc->strength);
  469. tchip->bb_cfg = BB_CFG(mtd->writesize, BBM_SIZE);
  470. err = mtd_device_register(mtd, NULL, 0);
  471. if (err)
  472. return err;
  473. nfc->chips[cs] = tchip;
  474. return 0;
  475. }
  476. static int tango_nand_remove(struct platform_device *pdev)
  477. {
  478. int cs;
  479. struct tango_nfc *nfc = platform_get_drvdata(pdev);
  480. dma_release_channel(nfc->chan);
  481. for (cs = 0; cs < MAX_CS; ++cs) {
  482. if (nfc->chips[cs])
  483. nand_release(nand_to_mtd(&nfc->chips[cs]->nand_chip));
  484. }
  485. return 0;
  486. }
  487. static int tango_nand_probe(struct platform_device *pdev)
  488. {
  489. int err;
  490. struct clk *clk;
  491. struct resource *res;
  492. struct tango_nfc *nfc;
  493. struct device_node *np;
  494. nfc = devm_kzalloc(&pdev->dev, sizeof(*nfc), GFP_KERNEL);
  495. if (!nfc)
  496. return -ENOMEM;
  497. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  498. nfc->reg_base = devm_ioremap_resource(&pdev->dev, res);
  499. if (IS_ERR(nfc->reg_base))
  500. return PTR_ERR(nfc->reg_base);
  501. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  502. nfc->mem_base = devm_ioremap_resource(&pdev->dev, res);
  503. if (IS_ERR(nfc->mem_base))
  504. return PTR_ERR(nfc->mem_base);
  505. res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  506. nfc->pbus_base = devm_ioremap_resource(&pdev->dev, res);
  507. if (IS_ERR(nfc->pbus_base))
  508. return PTR_ERR(nfc->pbus_base);
  509. writel_relaxed(MODE_RAW, nfc->pbus_base + PBUS_PAD_MODE);
  510. clk = clk_get(&pdev->dev, NULL);
  511. if (IS_ERR(clk))
  512. return PTR_ERR(clk);
  513. nfc->chan = dma_request_chan(&pdev->dev, "rxtx");
  514. if (IS_ERR(nfc->chan))
  515. return PTR_ERR(nfc->chan);
  516. platform_set_drvdata(pdev, nfc);
  517. nand_hw_control_init(&nfc->hw);
  518. nfc->freq_kHz = clk_get_rate(clk) / 1000;
  519. for_each_child_of_node(pdev->dev.of_node, np) {
  520. err = chip_init(&pdev->dev, np);
  521. if (err) {
  522. tango_nand_remove(pdev);
  523. return err;
  524. }
  525. }
  526. return 0;
  527. }
  528. static const struct of_device_id tango_nand_ids[] = {
  529. { .compatible = "sigma,smp8758-nand" },
  530. { /* sentinel */ }
  531. };
  532. static struct platform_driver tango_nand_driver = {
  533. .probe = tango_nand_probe,
  534. .remove = tango_nand_remove,
  535. .driver = {
  536. .name = "tango-nand",
  537. .of_match_table = tango_nand_ids,
  538. },
  539. };
  540. module_platform_driver(tango_nand_driver);
  541. MODULE_LICENSE("GPL");
  542. MODULE_AUTHOR("Sigma Designs");
  543. MODULE_DESCRIPTION("Tango4 NAND Flash controller driver");