blob: ec87516b87f5e0d88b464fcab51b4a060ba392a9 [file] [log] [blame]
Marc Gonzalez6956e232016-10-25 18:10:47 +02001#include <linux/io.h>
2#include <linux/of.h>
3#include <linux/clk.h>
4#include <linux/iopoll.h>
5#include <linux/module.h>
6#include <linux/mtd/nand.h>
7#include <linux/dmaengine.h>
8#include <linux/dma-mapping.h>
9#include <linux/platform_device.h>
10
11/* Offsets relative to chip->base */
12#define PBUS_CMD 0
13#define PBUS_ADDR 4
14#define PBUS_DATA 8
15
16/* Offsets relative to reg_base */
17#define NFC_STATUS 0x00
18#define NFC_FLASH_CMD 0x04
19#define NFC_DEVICE_CFG 0x08
20#define NFC_TIMING1 0x0c
21#define NFC_TIMING2 0x10
22#define NFC_XFER_CFG 0x14
23#define NFC_PKT_0_CFG 0x18
24#define NFC_PKT_N_CFG 0x1c
25#define NFC_BB_CFG 0x20
26#define NFC_ADDR_PAGE 0x24
27#define NFC_ADDR_OFFSET 0x28
28#define NFC_XFER_STATUS 0x2c
29
30/* NFC_STATUS values */
31#define CMD_READY BIT(31)
32
33/* NFC_FLASH_CMD values */
34#define NFC_READ 1
35#define NFC_WRITE 2
36
37/* NFC_XFER_STATUS values */
38#define PAGE_IS_EMPTY BIT(16)
39
40/* Offsets relative to mem_base */
41#define METADATA 0x000
42#define ERROR_REPORT 0x1c0
43
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
55/* Offsets relative to pbus_base */
56#define PBUS_CS_CTRL 0x83c
57#define PBUS_PAD_MODE 0x8f0
58
59/* PBUS_CS_CTRL values */
60#define PBUS_IORDY BIT(31)
61
62/*
63 * PBUS_PAD_MODE values
64 * In raw mode, the driver communicates directly with the NAND chips.
65 * In NFC mode, the NAND Flash controller manages the communication.
66 * We use NFC mode for read and write; raw mode for everything else.
67 */
68#define MODE_RAW 0
69#define MODE_NFC BIT(31)
70
71#define METADATA_SIZE 4
72#define BBM_SIZE 6
73#define FIELD_ORDER 15
74
75#define MAX_CS 4
76
77struct tango_nfc {
78 struct nand_hw_control hw;
79 void __iomem *reg_base;
80 void __iomem *mem_base;
81 void __iomem *pbus_base;
82 struct tango_chip *chips[MAX_CS];
83 struct dma_chan *chan;
84 int freq_kHz;
85};
86
87#define to_tango_nfc(ptr) container_of(ptr, struct tango_nfc, hw)
88
89struct tango_chip {
90 struct nand_chip nand_chip;
91 void __iomem *base;
92 u32 timing1;
93 u32 timing2;
94 u32 xfer_cfg;
95 u32 pkt_0_cfg;
96 u32 pkt_n_cfg;
97 u32 bb_cfg;
98};
99
100#define to_tango_chip(ptr) container_of(ptr, struct tango_chip, nand_chip)
101
102#define XFER_CFG(cs, page_count, steps, metadata_size) \
103 ((cs) << 24 | (page_count) << 16 | (steps) << 8 | (metadata_size))
104
105#define PKT_CFG(size, strength) ((size) << 16 | (strength))
106
107#define BB_CFG(bb_offset, bb_size) ((bb_offset) << 16 | (bb_size))
108
109#define TIMING(t0, t1, t2, t3) ((t0) << 24 | (t1) << 16 | (t2) << 8 | (t3))
110
111static void tango_cmd_ctrl(struct mtd_info *mtd, int dat, unsigned int ctrl)
112{
113 struct tango_chip *tchip = to_tango_chip(mtd_to_nand(mtd));
114
115 if (ctrl & NAND_CLE)
116 writeb_relaxed(dat, tchip->base + PBUS_CMD);
117
118 if (ctrl & NAND_ALE)
119 writeb_relaxed(dat, tchip->base + PBUS_ADDR);
120}
121
122static int tango_dev_ready(struct mtd_info *mtd)
123{
124 struct nand_chip *chip = mtd_to_nand(mtd);
125 struct tango_nfc *nfc = to_tango_nfc(chip->controller);
126
127 return readl_relaxed(nfc->pbus_base + PBUS_CS_CTRL) & PBUS_IORDY;
128}
129
130static u8 tango_read_byte(struct mtd_info *mtd)
131{
132 struct tango_chip *tchip = to_tango_chip(mtd_to_nand(mtd));
133
134 return readb_relaxed(tchip->base + PBUS_DATA);
135}
136
137static void tango_read_buf(struct mtd_info *mtd, u8 *buf, int len)
138{
139 struct tango_chip *tchip = to_tango_chip(mtd_to_nand(mtd));
140
141 ioread8_rep(tchip->base + PBUS_DATA, buf, len);
142}
143
144static void tango_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
145{
146 struct tango_chip *tchip = to_tango_chip(mtd_to_nand(mtd));
147
148 iowrite8_rep(tchip->base + PBUS_DATA, buf, len);
149}
150
151static void tango_select_chip(struct mtd_info *mtd, int idx)
152{
153 struct nand_chip *chip = mtd_to_nand(mtd);
154 struct tango_nfc *nfc = to_tango_nfc(chip->controller);
155 struct tango_chip *tchip = to_tango_chip(chip);
156
157 if (idx < 0)
158 return; /* No "chip unselect" function */
159
160 writel_relaxed(tchip->timing1, nfc->reg_base + NFC_TIMING1);
161 writel_relaxed(tchip->timing2, nfc->reg_base + NFC_TIMING2);
162 writel_relaxed(tchip->xfer_cfg, nfc->reg_base + NFC_XFER_CFG);
163 writel_relaxed(tchip->pkt_0_cfg, nfc->reg_base + NFC_PKT_0_CFG);
164 writel_relaxed(tchip->pkt_n_cfg, nfc->reg_base + NFC_PKT_N_CFG);
165 writel_relaxed(tchip->bb_cfg, nfc->reg_base + NFC_BB_CFG);
166}
167
168/*
169 * The controller does not check for bitflips in erased pages,
170 * therefore software must check instead.
171 */
172static int check_erased_page(struct nand_chip *chip, u8 *buf)
173{
Boris Brezillon8fcfba02016-11-21 10:03:04 +0100174 struct mtd_info *mtd = nand_to_mtd(chip);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200175 u8 *meta = chip->oob_poi + BBM_SIZE;
176 u8 *ecc = chip->oob_poi + BBM_SIZE + METADATA_SIZE;
177 const int ecc_size = chip->ecc.bytes;
178 const int pkt_size = chip->ecc.size;
179 int i, res, meta_len, bitflips = 0;
180
181 for (i = 0; i < chip->ecc.steps; ++i) {
182 meta_len = i ? 0 : METADATA_SIZE;
183 res = nand_check_erased_ecc_chunk(buf, pkt_size, ecc, ecc_size,
184 meta, meta_len,
185 chip->ecc.strength);
186 if (res < 0)
Boris Brezillon8fcfba02016-11-21 10:03:04 +0100187 mtd->ecc_stats.failed++;
Marc Gonzalez6956e232016-10-25 18:10:47 +0200188
189 bitflips = max(res, bitflips);
190 buf += pkt_size;
191 ecc += ecc_size;
192 }
193
194 return bitflips;
195}
196
197static int decode_error_report(struct tango_nfc *nfc)
198{
199 u32 status, res;
200
201 status = readl_relaxed(nfc->reg_base + NFC_XFER_STATUS);
202 if (status & PAGE_IS_EMPTY)
203 return 0;
204
205 res = readl_relaxed(nfc->mem_base + ERROR_REPORT);
206
207 if (DECODE_OK_PKT_0(res) && DECODE_OK_PKT_N(res))
208 return max(ERR_COUNT_PKT_0(res), ERR_COUNT_PKT_N(res));
209
210 return -EBADMSG;
211}
212
213static void tango_dma_callback(void *arg)
214{
215 complete(arg);
216}
217
218static int do_dma(struct tango_nfc *nfc, int dir, int cmd, const void *buf,
219 int len, int page)
220{
221 void __iomem *addr = nfc->reg_base + NFC_STATUS;
222 struct dma_chan *chan = nfc->chan;
223 struct dma_async_tx_descriptor *desc;
224 struct scatterlist sg;
225 struct completion tx_done;
226 int err = -EIO;
227 u32 res, val;
228
229 sg_init_one(&sg, buf, len);
230 if (dma_map_sg(chan->device->dev, &sg, 1, dir) != 1)
231 return -EIO;
232
233 desc = dmaengine_prep_slave_sg(chan, &sg, 1, dir, DMA_PREP_INTERRUPT);
234 if (!desc)
235 goto dma_unmap;
236
237 desc->callback = tango_dma_callback;
238 desc->callback_param = &tx_done;
239 init_completion(&tx_done);
240
241 writel_relaxed(MODE_NFC, nfc->pbus_base + PBUS_PAD_MODE);
242
243 writel_relaxed(page, nfc->reg_base + NFC_ADDR_PAGE);
244 writel_relaxed(0, nfc->reg_base + NFC_ADDR_OFFSET);
245 writel_relaxed(cmd, nfc->reg_base + NFC_FLASH_CMD);
246
247 dmaengine_submit(desc);
248 dma_async_issue_pending(chan);
249
250 res = wait_for_completion_timeout(&tx_done, HZ);
251 if (res > 0)
252 err = readl_poll_timeout(addr, val, val & CMD_READY, 0, 1000);
253
254 writel_relaxed(MODE_RAW, nfc->pbus_base + PBUS_PAD_MODE);
255
256dma_unmap:
257 dma_unmap_sg(chan->device->dev, &sg, 1, dir);
258
259 return err;
260}
261
262static int tango_read_page(struct mtd_info *mtd, struct nand_chip *chip,
263 u8 *buf, int oob_required, int page)
264{
265 struct tango_nfc *nfc = to_tango_nfc(chip->controller);
266 int err, res, len = mtd->writesize;
267
268 if (oob_required)
269 chip->ecc.read_oob(mtd, chip, page);
270
271 err = do_dma(nfc, DMA_FROM_DEVICE, NFC_READ, buf, len, page);
272 if (err)
273 return err;
274
275 res = decode_error_report(nfc);
276 if (res < 0) {
277 chip->ecc.read_oob_raw(mtd, chip, page);
278 res = check_erased_page(chip, buf);
279 }
280
281 return res;
282}
283
284static int tango_write_page(struct mtd_info *mtd, struct nand_chip *chip,
285 const u8 *buf, int oob_required, int page)
286{
287 struct tango_nfc *nfc = to_tango_nfc(chip->controller);
288 int err, len = mtd->writesize;
289
290 /* Calling tango_write_oob() would send PAGEPROG twice */
291 if (oob_required)
292 return -ENOTSUPP;
293
294 writel_relaxed(0xffffffff, nfc->mem_base + METADATA);
295 err = do_dma(nfc, DMA_TO_DEVICE, NFC_WRITE, buf, len, page);
296 if (err)
297 return err;
298
299 return 0;
300}
301
302static void aux_read(struct nand_chip *chip, u8 **buf, int len, int *pos)
303{
Boris Brezillon8fcfba02016-11-21 10:03:04 +0100304 struct mtd_info *mtd = nand_to_mtd(chip);
305
Marc Gonzalez6956e232016-10-25 18:10:47 +0200306 *pos += len;
307
308 if (!*buf) {
309 /* skip over "len" bytes */
Boris Brezillon8fcfba02016-11-21 10:03:04 +0100310 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, *pos, -1);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200311 } else {
Boris Brezillon8fcfba02016-11-21 10:03:04 +0100312 tango_read_buf(mtd, *buf, len);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200313 *buf += len;
314 }
315}
316
317static void aux_write(struct nand_chip *chip, const u8 **buf, int len, int *pos)
318{
Boris Brezillon8fcfba02016-11-21 10:03:04 +0100319 struct mtd_info *mtd = nand_to_mtd(chip);
320
Marc Gonzalez6956e232016-10-25 18:10:47 +0200321 *pos += len;
322
323 if (!*buf) {
324 /* skip over "len" bytes */
Boris Brezillon8fcfba02016-11-21 10:03:04 +0100325 chip->cmdfunc(mtd, NAND_CMD_SEQIN, *pos, -1);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200326 } else {
Boris Brezillon8fcfba02016-11-21 10:03:04 +0100327 tango_write_buf(mtd, *buf, len);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200328 *buf += len;
329 }
330}
331
332/*
333 * Physical page layout (not drawn to scale)
334 *
335 * NB: Bad Block Marker area splits PKT_N in two (N1, N2).
336 *
337 * +---+-----------------+-------+-----+-----------+-----+----+-------+
338 * | M | PKT_0 | ECC_0 | ... | N1 | BBM | N2 | ECC_N |
339 * +---+-----------------+-------+-----+-----------+-----+----+-------+
340 *
341 * Logical page layout:
342 *
343 * +-----+---+-------+-----+-------+
344 * oob = | BBM | M | ECC_0 | ... | ECC_N |
345 * +-----+---+-------+-----+-------+
346 *
347 * +-----------------+-----+-----------------+
348 * buf = | PKT_0 | ... | PKT_N |
349 * +-----------------+-----+-----------------+
350 */
Marc Gonzalez37871ab2016-11-15 11:08:19 +0100351static void raw_read(struct nand_chip *chip, u8 *buf, u8 *oob)
Marc Gonzalez6956e232016-10-25 18:10:47 +0200352{
Boris Brezillon8fcfba02016-11-21 10:03:04 +0100353 struct mtd_info *mtd = nand_to_mtd(chip);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200354 u8 *oob_orig = oob;
Boris Brezillon8fcfba02016-11-21 10:03:04 +0100355 const int page_size = mtd->writesize;
Marc Gonzalez6956e232016-10-25 18:10:47 +0200356 const int ecc_size = chip->ecc.bytes;
357 const int pkt_size = chip->ecc.size;
358 int pos = 0; /* position within physical page */
359 int rem = page_size; /* bytes remaining until BBM area */
360
361 if (oob)
362 oob += BBM_SIZE;
363
364 aux_read(chip, &oob, METADATA_SIZE, &pos);
365
366 while (rem > pkt_size) {
367 aux_read(chip, &buf, pkt_size, &pos);
368 aux_read(chip, &oob, ecc_size, &pos);
369 rem = page_size - pos;
370 }
371
372 aux_read(chip, &buf, rem, &pos);
373 aux_read(chip, &oob_orig, BBM_SIZE, &pos);
374 aux_read(chip, &buf, pkt_size - rem, &pos);
375 aux_read(chip, &oob, ecc_size, &pos);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200376}
377
Marc Gonzalez37871ab2016-11-15 11:08:19 +0100378static void raw_write(struct nand_chip *chip, const u8 *buf, const u8 *oob)
Marc Gonzalez6956e232016-10-25 18:10:47 +0200379{
Boris Brezillon8fcfba02016-11-21 10:03:04 +0100380 struct mtd_info *mtd = nand_to_mtd(chip);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200381 const u8 *oob_orig = oob;
Boris Brezillon8fcfba02016-11-21 10:03:04 +0100382 const int page_size = mtd->writesize;
Marc Gonzalez6956e232016-10-25 18:10:47 +0200383 const int ecc_size = chip->ecc.bytes;
384 const int pkt_size = chip->ecc.size;
385 int pos = 0; /* position within physical page */
386 int rem = page_size; /* bytes remaining until BBM area */
387
388 if (oob)
389 oob += BBM_SIZE;
390
391 aux_write(chip, &oob, METADATA_SIZE, &pos);
392
393 while (rem > pkt_size) {
394 aux_write(chip, &buf, pkt_size, &pos);
395 aux_write(chip, &oob, ecc_size, &pos);
396 rem = page_size - pos;
397 }
398
399 aux_write(chip, &buf, rem, &pos);
400 aux_write(chip, &oob_orig, BBM_SIZE, &pos);
401 aux_write(chip, &buf, pkt_size - rem, &pos);
402 aux_write(chip, &oob, ecc_size, &pos);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200403}
404
405static int tango_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
406 u8 *buf, int oob_required, int page)
407{
Marc Gonzalezff9e9ea2016-11-15 11:05:39 +0100408 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
Marc Gonzalez37871ab2016-11-15 11:08:19 +0100409 raw_read(chip, buf, chip->oob_poi);
410 return 0;
Marc Gonzalez6956e232016-10-25 18:10:47 +0200411}
412
413static int tango_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
414 const u8 *buf, int oob_required, int page)
415{
Marc Gonzalezff9e9ea2016-11-15 11:05:39 +0100416 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0, page);
417 raw_write(chip, buf, chip->oob_poi);
418 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
419 return 0;
Marc Gonzalez6956e232016-10-25 18:10:47 +0200420}
421
422static int tango_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
423 int page)
424{
425 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
Marc Gonzalez37871ab2016-11-15 11:08:19 +0100426 raw_read(chip, NULL, chip->oob_poi);
427 return 0;
Marc Gonzalez6956e232016-10-25 18:10:47 +0200428}
429
430static int tango_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
431 int page)
432{
433 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0, page);
434 raw_write(chip, NULL, chip->oob_poi);
435 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
436 chip->waitfunc(mtd, chip);
437 return 0;
438}
439
440static int oob_ecc(struct mtd_info *mtd, int idx, struct mtd_oob_region *res)
441{
442 struct nand_chip *chip = mtd_to_nand(mtd);
443 struct nand_ecc_ctrl *ecc = &chip->ecc;
444
445 if (idx >= ecc->steps)
446 return -ERANGE;
447
448 res->offset = BBM_SIZE + METADATA_SIZE + ecc->bytes * idx;
449 res->length = ecc->bytes;
450
451 return 0;
452}
453
454static int oob_free(struct mtd_info *mtd, int idx, struct mtd_oob_region *res)
455{
456 return -ERANGE; /* no free space in spare area */
457}
458
459static const struct mtd_ooblayout_ops tango_nand_ooblayout_ops = {
460 .ecc = oob_ecc,
461 .free = oob_free,
462};
463
464static u32 to_ticks(int kHz, int ps)
465{
466 return DIV_ROUND_UP_ULL((u64)kHz * ps, NSEC_PER_SEC);
467}
468
469static int tango_set_timings(struct mtd_info *mtd,
470 const struct nand_data_interface *conf,
471 bool check_only)
472{
473 const struct nand_sdr_timings *sdr = nand_get_sdr_timings(conf);
474 struct nand_chip *chip = mtd_to_nand(mtd);
475 struct tango_nfc *nfc = to_tango_nfc(chip->controller);
476 struct tango_chip *tchip = to_tango_chip(chip);
477 u32 Trdy, Textw, Twc, Twpw, Tacc, Thold, Trpw, Textr;
478 int kHz = nfc->freq_kHz;
479
480 if (IS_ERR(sdr))
481 return PTR_ERR(sdr);
482
483 if (check_only)
484 return 0;
485
486 Trdy = to_ticks(kHz, sdr->tCEA_max - sdr->tREA_max);
487 Textw = to_ticks(kHz, sdr->tWB_max);
488 Twc = to_ticks(kHz, sdr->tWC_min);
489 Twpw = to_ticks(kHz, sdr->tWC_min - sdr->tWP_min);
490
491 Tacc = to_ticks(kHz, sdr->tREA_max);
492 Thold = to_ticks(kHz, sdr->tREH_min);
493 Trpw = to_ticks(kHz, sdr->tRC_min - sdr->tREH_min);
494 Textr = to_ticks(kHz, sdr->tRHZ_max);
495
496 tchip->timing1 = TIMING(Trdy, Textw, Twc, Twpw);
497 tchip->timing2 = TIMING(Tacc, Thold, Trpw, Textr);
498
499 return 0;
500}
501
502static int chip_init(struct device *dev, struct device_node *np)
503{
504 u32 cs;
505 int err, res;
506 struct mtd_info *mtd;
507 struct nand_chip *chip;
508 struct tango_chip *tchip;
509 struct nand_ecc_ctrl *ecc;
510 struct tango_nfc *nfc = dev_get_drvdata(dev);
511
512 tchip = devm_kzalloc(dev, sizeof(*tchip), GFP_KERNEL);
513 if (!tchip)
514 return -ENOMEM;
515
516 res = of_property_count_u32_elems(np, "reg");
517 if (res < 0)
518 return res;
519
520 if (res != 1)
521 return -ENOTSUPP; /* Multi-CS chips are not supported */
522
523 err = of_property_read_u32_index(np, "reg", 0, &cs);
524 if (err)
525 return err;
526
527 if (cs >= MAX_CS)
528 return -EINVAL;
529
530 chip = &tchip->nand_chip;
531 ecc = &chip->ecc;
Boris Brezillon8fcfba02016-11-21 10:03:04 +0100532 mtd = nand_to_mtd(chip);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200533
534 chip->read_byte = tango_read_byte;
535 chip->write_buf = tango_write_buf;
536 chip->read_buf = tango_read_buf;
537 chip->select_chip = tango_select_chip;
538 chip->cmd_ctrl = tango_cmd_ctrl;
539 chip->dev_ready = tango_dev_ready;
540 chip->setup_data_interface = tango_set_timings;
541 chip->options = NAND_USE_BOUNCE_BUFFER |
542 NAND_NO_SUBPAGE_WRITE |
543 NAND_WAIT_TCCS;
544 chip->controller = &nfc->hw;
545 tchip->base = nfc->pbus_base + (cs * 256);
546
547 nand_set_flash_node(chip, np);
548 mtd_set_ooblayout(mtd, &tango_nand_ooblayout_ops);
549 mtd->dev.parent = dev;
550
551 err = nand_scan_ident(mtd, 1, NULL);
552 if (err)
553 return err;
554
555 ecc->mode = NAND_ECC_HW;
556 ecc->algo = NAND_ECC_BCH;
557 ecc->bytes = DIV_ROUND_UP(ecc->strength * FIELD_ORDER, BITS_PER_BYTE);
558
559 ecc->read_page_raw = tango_read_page_raw;
560 ecc->write_page_raw = tango_write_page_raw;
561 ecc->read_page = tango_read_page;
562 ecc->write_page = tango_write_page;
563 ecc->read_oob = tango_read_oob;
564 ecc->write_oob = tango_write_oob;
Marc Gonzalezff9e9ea2016-11-15 11:05:39 +0100565 ecc->options = NAND_ECC_CUSTOM_PAGE_ACCESS;
Marc Gonzalez6956e232016-10-25 18:10:47 +0200566
567 err = nand_scan_tail(mtd);
568 if (err)
569 return err;
570
571 tchip->xfer_cfg = XFER_CFG(cs, 1, ecc->steps, METADATA_SIZE);
572 tchip->pkt_0_cfg = PKT_CFG(ecc->size + METADATA_SIZE, ecc->strength);
573 tchip->pkt_n_cfg = PKT_CFG(ecc->size, ecc->strength);
574 tchip->bb_cfg = BB_CFG(mtd->writesize, BBM_SIZE);
575
576 err = mtd_device_register(mtd, NULL, 0);
577 if (err)
578 return err;
579
580 nfc->chips[cs] = tchip;
581
582 return 0;
583}
584
585static int tango_nand_remove(struct platform_device *pdev)
586{
587 int cs;
588 struct tango_nfc *nfc = platform_get_drvdata(pdev);
589
590 dma_release_channel(nfc->chan);
591
592 for (cs = 0; cs < MAX_CS; ++cs) {
593 if (nfc->chips[cs])
Boris Brezillon8fcfba02016-11-21 10:03:04 +0100594 nand_release(nand_to_mtd(&nfc->chips[cs]->nand_chip));
Marc Gonzalez6956e232016-10-25 18:10:47 +0200595 }
596
597 return 0;
598}
599
600static int tango_nand_probe(struct platform_device *pdev)
601{
602 int err;
603 struct clk *clk;
604 struct resource *res;
605 struct tango_nfc *nfc;
606 struct device_node *np;
607
608 nfc = devm_kzalloc(&pdev->dev, sizeof(*nfc), GFP_KERNEL);
609 if (!nfc)
610 return -ENOMEM;
611
612 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
613 nfc->reg_base = devm_ioremap_resource(&pdev->dev, res);
614 if (IS_ERR(nfc->reg_base))
615 return PTR_ERR(nfc->reg_base);
616
617 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
618 nfc->mem_base = devm_ioremap_resource(&pdev->dev, res);
619 if (IS_ERR(nfc->mem_base))
620 return PTR_ERR(nfc->mem_base);
621
622 res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
623 nfc->pbus_base = devm_ioremap_resource(&pdev->dev, res);
624 if (IS_ERR(nfc->pbus_base))
625 return PTR_ERR(nfc->pbus_base);
626
627 clk = clk_get(&pdev->dev, NULL);
628 if (IS_ERR(clk))
629 return PTR_ERR(clk);
630
631 nfc->chan = dma_request_chan(&pdev->dev, "nfc_sbox");
632 if (IS_ERR(nfc->chan))
633 return PTR_ERR(nfc->chan);
634
635 platform_set_drvdata(pdev, nfc);
636 nand_hw_control_init(&nfc->hw);
637 nfc->freq_kHz = clk_get_rate(clk) / 1000;
638
639 for_each_child_of_node(pdev->dev.of_node, np) {
640 err = chip_init(&pdev->dev, np);
641 if (err) {
642 tango_nand_remove(pdev);
643 return err;
644 }
645 }
646
647 return 0;
648}
649
650static const struct of_device_id tango_nand_ids[] = {
651 { .compatible = "sigma,smp8758-nand" },
652 { /* sentinel */ }
653};
654
655static struct platform_driver tango_nand_driver = {
656 .probe = tango_nand_probe,
657 .remove = tango_nand_remove,
658 .driver = {
659 .name = "tango-nand",
660 .of_match_table = tango_nand_ids,
661 },
662};
663
664module_platform_driver(tango_nand_driver);
665
666MODULE_LICENSE("GPL");
667MODULE_AUTHOR("Sigma Designs");
668MODULE_DESCRIPTION("Tango4 NAND Flash controller driver");