blob: cb3beda88789a77844a15175d0cd4834572a5f96 [file] [log] [blame]
Marc Gonzalez59dbc862016-12-01 11:22:14 +01001/*
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
Marc Gonzalez6956e232016-10-25 18:10:47 +02009#include <linux/io.h>
10#include <linux/of.h>
11#include <linux/clk.h>
12#include <linux/iopoll.h>
13#include <linux/module.h>
Boris Brezillond4092d72017-08-04 17:29:10 +020014#include <linux/mtd/rawnand.h>
Marc Gonzalez6956e232016-10-25 18:10:47 +020015#include <linux/dmaengine.h>
16#include <linux/dma-mapping.h>
17#include <linux/platform_device.h>
18
19/* Offsets relative to chip->base */
20#define PBUS_CMD 0
21#define PBUS_ADDR 4
22#define PBUS_DATA 8
23
24/* Offsets relative to reg_base */
25#define NFC_STATUS 0x00
26#define NFC_FLASH_CMD 0x04
27#define NFC_DEVICE_CFG 0x08
28#define NFC_TIMING1 0x0c
29#define NFC_TIMING2 0x10
30#define NFC_XFER_CFG 0x14
31#define NFC_PKT_0_CFG 0x18
32#define NFC_PKT_N_CFG 0x1c
33#define NFC_BB_CFG 0x20
34#define NFC_ADDR_PAGE 0x24
35#define NFC_ADDR_OFFSET 0x28
36#define NFC_XFER_STATUS 0x2c
37
38/* NFC_STATUS values */
39#define CMD_READY BIT(31)
40
41/* NFC_FLASH_CMD values */
42#define NFC_READ 1
43#define NFC_WRITE 2
44
45/* NFC_XFER_STATUS values */
46#define PAGE_IS_EMPTY BIT(16)
47
48/* Offsets relative to mem_base */
49#define METADATA 0x000
50#define ERROR_REPORT 0x1c0
51
52/*
53 * Error reports are split in two bytes:
54 * byte 0 for the first packet in the page (PKT_0)
55 * byte 1 for other packets in the page (PKT_N, for N > 0)
56 * ERR_COUNT_PKT_N is the max error count over all but the first packet.
57 */
Marc Gonzalez6956e232016-10-25 18:10:47 +020058#define ERR_COUNT_PKT_0(v) (((v) >> 0) & 0x3f)
59#define ERR_COUNT_PKT_N(v) (((v) >> 8) & 0x3f)
Marc Gonzalez60cf0ce2017-05-12 17:34:01 +020060#define DECODE_FAIL_PKT_0(v) (((v) & BIT(7)) == 0)
61#define DECODE_FAIL_PKT_N(v) (((v) & BIT(15)) == 0)
Marc Gonzalez6956e232016-10-25 18:10:47 +020062
63/* Offsets relative to pbus_base */
64#define PBUS_CS_CTRL 0x83c
65#define PBUS_PAD_MODE 0x8f0
66
67/* PBUS_CS_CTRL values */
68#define PBUS_IORDY BIT(31)
69
70/*
71 * PBUS_PAD_MODE values
72 * In raw mode, the driver communicates directly with the NAND chips.
73 * In NFC mode, the NAND Flash controller manages the communication.
74 * We use NFC mode for read and write; raw mode for everything else.
75 */
76#define MODE_RAW 0
77#define MODE_NFC BIT(31)
78
79#define METADATA_SIZE 4
80#define BBM_SIZE 6
81#define FIELD_ORDER 15
82
83#define MAX_CS 4
84
85struct tango_nfc {
Miquel Raynal7da45132018-07-17 09:08:02 +020086 struct nand_controller hw;
Marc Gonzalez6956e232016-10-25 18:10:47 +020087 void __iomem *reg_base;
88 void __iomem *mem_base;
89 void __iomem *pbus_base;
90 struct tango_chip *chips[MAX_CS];
91 struct dma_chan *chan;
92 int freq_kHz;
93};
94
95#define to_tango_nfc(ptr) container_of(ptr, struct tango_nfc, hw)
96
97struct tango_chip {
98 struct nand_chip nand_chip;
99 void __iomem *base;
100 u32 timing1;
101 u32 timing2;
102 u32 xfer_cfg;
103 u32 pkt_0_cfg;
104 u32 pkt_n_cfg;
105 u32 bb_cfg;
106};
107
108#define to_tango_chip(ptr) container_of(ptr, struct tango_chip, nand_chip)
109
110#define XFER_CFG(cs, page_count, steps, metadata_size) \
111 ((cs) << 24 | (page_count) << 16 | (steps) << 8 | (metadata_size))
112
113#define PKT_CFG(size, strength) ((size) << 16 | (strength))
114
115#define BB_CFG(bb_offset, bb_size) ((bb_offset) << 16 | (bb_size))
116
117#define TIMING(t0, t1, t2, t3) ((t0) << 24 | (t1) << 16 | (t2) << 8 | (t3))
118
Boris Brezillon0f808c12018-09-06 14:05:26 +0200119static void tango_cmd_ctrl(struct nand_chip *chip, int dat, unsigned int ctrl)
Marc Gonzalez6956e232016-10-25 18:10:47 +0200120{
Boris Brezillon0f808c12018-09-06 14:05:26 +0200121 struct tango_chip *tchip = to_tango_chip(chip);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200122
123 if (ctrl & NAND_CLE)
124 writeb_relaxed(dat, tchip->base + PBUS_CMD);
125
126 if (ctrl & NAND_ALE)
127 writeb_relaxed(dat, tchip->base + PBUS_ADDR);
128}
129
Boris Brezillon50a487e2018-09-06 14:05:27 +0200130static int tango_dev_ready(struct nand_chip *chip)
Marc Gonzalez6956e232016-10-25 18:10:47 +0200131{
Marc Gonzalez6956e232016-10-25 18:10:47 +0200132 struct tango_nfc *nfc = to_tango_nfc(chip->controller);
133
134 return readl_relaxed(nfc->pbus_base + PBUS_CS_CTRL) & PBUS_IORDY;
135}
136
Boris Brezillon7e534322018-09-06 14:05:22 +0200137static u8 tango_read_byte(struct nand_chip *chip)
Marc Gonzalez6956e232016-10-25 18:10:47 +0200138{
Boris Brezillon7e534322018-09-06 14:05:22 +0200139 struct tango_chip *tchip = to_tango_chip(chip);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200140
141 return readb_relaxed(tchip->base + PBUS_DATA);
142}
143
Boris Brezillon7e534322018-09-06 14:05:22 +0200144static void tango_read_buf(struct nand_chip *chip, u8 *buf, int len)
Marc Gonzalez6956e232016-10-25 18:10:47 +0200145{
Boris Brezillon7e534322018-09-06 14:05:22 +0200146 struct tango_chip *tchip = to_tango_chip(chip);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200147
148 ioread8_rep(tchip->base + PBUS_DATA, buf, len);
149}
150
Boris Brezillonc0739d82018-09-06 14:05:23 +0200151static void tango_write_buf(struct nand_chip *chip, const u8 *buf, int len)
Marc Gonzalez6956e232016-10-25 18:10:47 +0200152{
Boris Brezillonc0739d82018-09-06 14:05:23 +0200153 struct tango_chip *tchip = to_tango_chip(chip);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200154
155 iowrite8_rep(tchip->base + PBUS_DATA, buf, len);
156}
157
Boris Brezillon758b56f2018-09-06 14:05:24 +0200158static void tango_select_chip(struct nand_chip *chip, int idx)
Marc Gonzalez6956e232016-10-25 18:10:47 +0200159{
Marc Gonzalez6956e232016-10-25 18:10:47 +0200160 struct tango_nfc *nfc = to_tango_nfc(chip->controller);
161 struct tango_chip *tchip = to_tango_chip(chip);
162
163 if (idx < 0)
164 return; /* No "chip unselect" function */
165
166 writel_relaxed(tchip->timing1, nfc->reg_base + NFC_TIMING1);
167 writel_relaxed(tchip->timing2, nfc->reg_base + NFC_TIMING2);
168 writel_relaxed(tchip->xfer_cfg, nfc->reg_base + NFC_XFER_CFG);
169 writel_relaxed(tchip->pkt_0_cfg, nfc->reg_base + NFC_PKT_0_CFG);
170 writel_relaxed(tchip->pkt_n_cfg, nfc->reg_base + NFC_PKT_N_CFG);
171 writel_relaxed(tchip->bb_cfg, nfc->reg_base + NFC_BB_CFG);
172}
173
174/*
175 * The controller does not check for bitflips in erased pages,
176 * therefore software must check instead.
177 */
178static int check_erased_page(struct nand_chip *chip, u8 *buf)
179{
Boris Brezillon8fcfba02016-11-21 10:03:04 +0100180 struct mtd_info *mtd = nand_to_mtd(chip);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200181 u8 *meta = chip->oob_poi + BBM_SIZE;
182 u8 *ecc = chip->oob_poi + BBM_SIZE + METADATA_SIZE;
183 const int ecc_size = chip->ecc.bytes;
184 const int pkt_size = chip->ecc.size;
185 int i, res, meta_len, bitflips = 0;
186
187 for (i = 0; i < chip->ecc.steps; ++i) {
188 meta_len = i ? 0 : METADATA_SIZE;
189 res = nand_check_erased_ecc_chunk(buf, pkt_size, ecc, ecc_size,
190 meta, meta_len,
191 chip->ecc.strength);
192 if (res < 0)
Boris Brezillon8fcfba02016-11-21 10:03:04 +0100193 mtd->ecc_stats.failed++;
Marc Gonzalez60cf0ce2017-05-12 17:34:01 +0200194 else
195 mtd->ecc_stats.corrected += res;
Marc Gonzalez6956e232016-10-25 18:10:47 +0200196
197 bitflips = max(res, bitflips);
198 buf += pkt_size;
199 ecc += ecc_size;
200 }
201
202 return bitflips;
203}
204
Marc Gonzalez60cf0ce2017-05-12 17:34:01 +0200205static int decode_error_report(struct nand_chip *chip)
Marc Gonzalez6956e232016-10-25 18:10:47 +0200206{
207 u32 status, res;
Marc Gonzalez60cf0ce2017-05-12 17:34:01 +0200208 struct mtd_info *mtd = nand_to_mtd(chip);
209 struct tango_nfc *nfc = to_tango_nfc(chip->controller);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200210
211 status = readl_relaxed(nfc->reg_base + NFC_XFER_STATUS);
212 if (status & PAGE_IS_EMPTY)
213 return 0;
214
215 res = readl_relaxed(nfc->mem_base + ERROR_REPORT);
216
Marc Gonzalez60cf0ce2017-05-12 17:34:01 +0200217 if (DECODE_FAIL_PKT_0(res) || DECODE_FAIL_PKT_N(res))
218 return -EBADMSG;
Marc Gonzalez6956e232016-10-25 18:10:47 +0200219
Marc Gonzalez60cf0ce2017-05-12 17:34:01 +0200220 /* ERR_COUNT_PKT_N is max, not sum, but that's all we have */
221 mtd->ecc_stats.corrected +=
222 ERR_COUNT_PKT_0(res) + ERR_COUNT_PKT_N(res);
223
224 return max(ERR_COUNT_PKT_0(res), ERR_COUNT_PKT_N(res));
Marc Gonzalez6956e232016-10-25 18:10:47 +0200225}
226
227static void tango_dma_callback(void *arg)
228{
229 complete(arg);
230}
231
Boris Brezillon1932a962017-02-20 14:10:07 +0100232static int do_dma(struct tango_nfc *nfc, enum dma_data_direction dir, int cmd,
233 const void *buf, int len, int page)
Marc Gonzalez6956e232016-10-25 18:10:47 +0200234{
235 void __iomem *addr = nfc->reg_base + NFC_STATUS;
236 struct dma_chan *chan = nfc->chan;
237 struct dma_async_tx_descriptor *desc;
Boris Brezillon1932a962017-02-20 14:10:07 +0100238 enum dma_transfer_direction tdir;
Marc Gonzalez6956e232016-10-25 18:10:47 +0200239 struct scatterlist sg;
240 struct completion tx_done;
241 int err = -EIO;
242 u32 res, val;
243
244 sg_init_one(&sg, buf, len);
245 if (dma_map_sg(chan->device->dev, &sg, 1, dir) != 1)
246 return -EIO;
247
Boris Brezillon1932a962017-02-20 14:10:07 +0100248 tdir = dir == DMA_TO_DEVICE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
249 desc = dmaengine_prep_slave_sg(chan, &sg, 1, tdir, DMA_PREP_INTERRUPT);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200250 if (!desc)
251 goto dma_unmap;
252
253 desc->callback = tango_dma_callback;
254 desc->callback_param = &tx_done;
255 init_completion(&tx_done);
256
257 writel_relaxed(MODE_NFC, nfc->pbus_base + PBUS_PAD_MODE);
258
259 writel_relaxed(page, nfc->reg_base + NFC_ADDR_PAGE);
260 writel_relaxed(0, nfc->reg_base + NFC_ADDR_OFFSET);
261 writel_relaxed(cmd, nfc->reg_base + NFC_FLASH_CMD);
262
263 dmaengine_submit(desc);
264 dma_async_issue_pending(chan);
265
266 res = wait_for_completion_timeout(&tx_done, HZ);
267 if (res > 0)
268 err = readl_poll_timeout(addr, val, val & CMD_READY, 0, 1000);
269
270 writel_relaxed(MODE_RAW, nfc->pbus_base + PBUS_PAD_MODE);
271
272dma_unmap:
273 dma_unmap_sg(chan->device->dev, &sg, 1, dir);
274
275 return err;
276}
277
Boris Brezillonb9761682018-09-06 14:05:20 +0200278static int tango_read_page(struct nand_chip *chip, u8 *buf,
279 int oob_required, int page)
Marc Gonzalez6956e232016-10-25 18:10:47 +0200280{
Boris Brezillonb9761682018-09-06 14:05:20 +0200281 struct mtd_info *mtd = nand_to_mtd(chip);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200282 struct tango_nfc *nfc = to_tango_nfc(chip->controller);
283 int err, res, len = mtd->writesize;
284
285 if (oob_required)
Boris Brezillonb9761682018-09-06 14:05:20 +0200286 chip->ecc.read_oob(chip, page);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200287
288 err = do_dma(nfc, DMA_FROM_DEVICE, NFC_READ, buf, len, page);
289 if (err)
290 return err;
291
Marc Gonzalez60cf0ce2017-05-12 17:34:01 +0200292 res = decode_error_report(chip);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200293 if (res < 0) {
Boris Brezillonb9761682018-09-06 14:05:20 +0200294 chip->ecc.read_oob_raw(chip, page);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200295 res = check_erased_page(chip, buf);
296 }
297
298 return res;
299}
300
Boris Brezillon767eb6f2018-09-06 14:05:21 +0200301static int tango_write_page(struct nand_chip *chip, const u8 *buf,
302 int oob_required, int page)
Marc Gonzalez6956e232016-10-25 18:10:47 +0200303{
Boris Brezillon767eb6f2018-09-06 14:05:21 +0200304 struct mtd_info *mtd = nand_to_mtd(chip);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200305 struct tango_nfc *nfc = to_tango_nfc(chip->controller);
Boris Brezillon41145642017-05-16 18:27:49 +0200306 int err, status, len = mtd->writesize;
Marc Gonzalez6956e232016-10-25 18:10:47 +0200307
308 /* Calling tango_write_oob() would send PAGEPROG twice */
309 if (oob_required)
310 return -ENOTSUPP;
311
312 writel_relaxed(0xffffffff, nfc->mem_base + METADATA);
313 err = do_dma(nfc, DMA_TO_DEVICE, NFC_WRITE, buf, len, page);
314 if (err)
315 return err;
316
Boris Brezillon8395b752018-09-07 00:38:37 +0200317 status = chip->legacy.waitfunc(chip);
Boris Brezillon41145642017-05-16 18:27:49 +0200318 if (status & NAND_STATUS_FAIL)
319 return -EIO;
320
Marc Gonzalez6956e232016-10-25 18:10:47 +0200321 return 0;
322}
323
324static void aux_read(struct nand_chip *chip, u8 **buf, int len, int *pos)
325{
326 *pos += len;
327
328 if (!*buf) {
329 /* skip over "len" bytes */
Boris Brezillon97d90da2017-11-30 18:01:29 +0100330 nand_change_read_column_op(chip, *pos, NULL, 0, false);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200331 } else {
Boris Brezillon7e534322018-09-06 14:05:22 +0200332 tango_read_buf(chip, *buf, len);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200333 *buf += len;
334 }
335}
336
337static void aux_write(struct nand_chip *chip, const u8 **buf, int len, int *pos)
338{
339 *pos += len;
340
341 if (!*buf) {
342 /* skip over "len" bytes */
Boris Brezillon97d90da2017-11-30 18:01:29 +0100343 nand_change_write_column_op(chip, *pos, NULL, 0, false);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200344 } else {
Boris Brezillonc0739d82018-09-06 14:05:23 +0200345 tango_write_buf(chip, *buf, len);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200346 *buf += len;
347 }
348}
349
350/*
351 * Physical page layout (not drawn to scale)
352 *
353 * NB: Bad Block Marker area splits PKT_N in two (N1, N2).
354 *
355 * +---+-----------------+-------+-----+-----------+-----+----+-------+
356 * | M | PKT_0 | ECC_0 | ... | N1 | BBM | N2 | ECC_N |
357 * +---+-----------------+-------+-----+-----------+-----+----+-------+
358 *
359 * Logical page layout:
360 *
361 * +-----+---+-------+-----+-------+
362 * oob = | BBM | M | ECC_0 | ... | ECC_N |
363 * +-----+---+-------+-----+-------+
364 *
365 * +-----------------+-----+-----------------+
366 * buf = | PKT_0 | ... | PKT_N |
367 * +-----------------+-----+-----------------+
368 */
Marc Gonzalez37871ab2016-11-15 11:08:19 +0100369static void raw_read(struct nand_chip *chip, u8 *buf, u8 *oob)
Marc Gonzalez6956e232016-10-25 18:10:47 +0200370{
Boris Brezillon8fcfba02016-11-21 10:03:04 +0100371 struct mtd_info *mtd = nand_to_mtd(chip);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200372 u8 *oob_orig = oob;
Boris Brezillon8fcfba02016-11-21 10:03:04 +0100373 const int page_size = mtd->writesize;
Marc Gonzalez6956e232016-10-25 18:10:47 +0200374 const int ecc_size = chip->ecc.bytes;
375 const int pkt_size = chip->ecc.size;
376 int pos = 0; /* position within physical page */
377 int rem = page_size; /* bytes remaining until BBM area */
378
379 if (oob)
380 oob += BBM_SIZE;
381
382 aux_read(chip, &oob, METADATA_SIZE, &pos);
383
384 while (rem > pkt_size) {
385 aux_read(chip, &buf, pkt_size, &pos);
386 aux_read(chip, &oob, ecc_size, &pos);
387 rem = page_size - pos;
388 }
389
390 aux_read(chip, &buf, rem, &pos);
391 aux_read(chip, &oob_orig, BBM_SIZE, &pos);
392 aux_read(chip, &buf, pkt_size - rem, &pos);
393 aux_read(chip, &oob, ecc_size, &pos);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200394}
395
Marc Gonzalez37871ab2016-11-15 11:08:19 +0100396static void raw_write(struct nand_chip *chip, const u8 *buf, const u8 *oob)
Marc Gonzalez6956e232016-10-25 18:10:47 +0200397{
Boris Brezillon8fcfba02016-11-21 10:03:04 +0100398 struct mtd_info *mtd = nand_to_mtd(chip);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200399 const u8 *oob_orig = oob;
Boris Brezillon8fcfba02016-11-21 10:03:04 +0100400 const int page_size = mtd->writesize;
Marc Gonzalez6956e232016-10-25 18:10:47 +0200401 const int ecc_size = chip->ecc.bytes;
402 const int pkt_size = chip->ecc.size;
403 int pos = 0; /* position within physical page */
404 int rem = page_size; /* bytes remaining until BBM area */
405
406 if (oob)
407 oob += BBM_SIZE;
408
409 aux_write(chip, &oob, METADATA_SIZE, &pos);
410
411 while (rem > pkt_size) {
412 aux_write(chip, &buf, pkt_size, &pos);
413 aux_write(chip, &oob, ecc_size, &pos);
414 rem = page_size - pos;
415 }
416
417 aux_write(chip, &buf, rem, &pos);
418 aux_write(chip, &oob_orig, BBM_SIZE, &pos);
419 aux_write(chip, &buf, pkt_size - rem, &pos);
420 aux_write(chip, &oob, ecc_size, &pos);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200421}
422
Boris Brezillonb9761682018-09-06 14:05:20 +0200423static int tango_read_page_raw(struct nand_chip *chip, u8 *buf,
424 int oob_required, int page)
Marc Gonzalez6956e232016-10-25 18:10:47 +0200425{
Boris Brezillon97d90da2017-11-30 18:01:29 +0100426 nand_read_page_op(chip, page, 0, NULL, 0);
Marc Gonzalez37871ab2016-11-15 11:08:19 +0100427 raw_read(chip, buf, chip->oob_poi);
428 return 0;
Marc Gonzalez6956e232016-10-25 18:10:47 +0200429}
430
Boris Brezillon767eb6f2018-09-06 14:05:21 +0200431static int tango_write_page_raw(struct nand_chip *chip, const u8 *buf,
432 int oob_required, int page)
Marc Gonzalez6956e232016-10-25 18:10:47 +0200433{
Boris Brezillon97d90da2017-11-30 18:01:29 +0100434 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
Marc Gonzalezff9e9ea2016-11-15 11:05:39 +0100435 raw_write(chip, buf, chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100436 return nand_prog_page_end_op(chip);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200437}
438
Boris Brezillonb9761682018-09-06 14:05:20 +0200439static int tango_read_oob(struct nand_chip *chip, int page)
Marc Gonzalez6956e232016-10-25 18:10:47 +0200440{
Boris Brezillon97d90da2017-11-30 18:01:29 +0100441 nand_read_page_op(chip, page, 0, NULL, 0);
Marc Gonzalez37871ab2016-11-15 11:08:19 +0100442 raw_read(chip, NULL, chip->oob_poi);
443 return 0;
Marc Gonzalez6956e232016-10-25 18:10:47 +0200444}
445
Boris Brezillon767eb6f2018-09-06 14:05:21 +0200446static int tango_write_oob(struct nand_chip *chip, int page)
Marc Gonzalez6956e232016-10-25 18:10:47 +0200447{
Boris Brezillon97d90da2017-11-30 18:01:29 +0100448 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200449 raw_write(chip, NULL, chip->oob_poi);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100450 return nand_prog_page_end_op(chip);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200451}
452
453static int oob_ecc(struct mtd_info *mtd, int idx, struct mtd_oob_region *res)
454{
455 struct nand_chip *chip = mtd_to_nand(mtd);
456 struct nand_ecc_ctrl *ecc = &chip->ecc;
457
458 if (idx >= ecc->steps)
459 return -ERANGE;
460
461 res->offset = BBM_SIZE + METADATA_SIZE + ecc->bytes * idx;
462 res->length = ecc->bytes;
463
464 return 0;
465}
466
467static int oob_free(struct mtd_info *mtd, int idx, struct mtd_oob_region *res)
468{
469 return -ERANGE; /* no free space in spare area */
470}
471
472static const struct mtd_ooblayout_ops tango_nand_ooblayout_ops = {
473 .ecc = oob_ecc,
474 .free = oob_free,
475};
476
477static u32 to_ticks(int kHz, int ps)
478{
479 return DIV_ROUND_UP_ULL((u64)kHz * ps, NSEC_PER_SEC);
480}
481
Boris Brezillon858838b2018-09-06 14:05:33 +0200482static int tango_set_timings(struct nand_chip *chip, int csline,
Boris Brezillon104e4422017-03-16 09:35:58 +0100483 const struct nand_data_interface *conf)
Marc Gonzalez6956e232016-10-25 18:10:47 +0200484{
485 const struct nand_sdr_timings *sdr = nand_get_sdr_timings(conf);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200486 struct tango_nfc *nfc = to_tango_nfc(chip->controller);
487 struct tango_chip *tchip = to_tango_chip(chip);
488 u32 Trdy, Textw, Twc, Twpw, Tacc, Thold, Trpw, Textr;
489 int kHz = nfc->freq_kHz;
490
491 if (IS_ERR(sdr))
492 return PTR_ERR(sdr);
493
Boris Brezillon104e4422017-03-16 09:35:58 +0100494 if (csline == NAND_DATA_IFACE_CHECK_ONLY)
Marc Gonzalez6956e232016-10-25 18:10:47 +0200495 return 0;
496
497 Trdy = to_ticks(kHz, sdr->tCEA_max - sdr->tREA_max);
498 Textw = to_ticks(kHz, sdr->tWB_max);
499 Twc = to_ticks(kHz, sdr->tWC_min);
500 Twpw = to_ticks(kHz, sdr->tWC_min - sdr->tWP_min);
501
502 Tacc = to_ticks(kHz, sdr->tREA_max);
503 Thold = to_ticks(kHz, sdr->tREH_min);
504 Trpw = to_ticks(kHz, sdr->tRC_min - sdr->tREH_min);
505 Textr = to_ticks(kHz, sdr->tRHZ_max);
506
507 tchip->timing1 = TIMING(Trdy, Textw, Twc, Twpw);
508 tchip->timing2 = TIMING(Tacc, Thold, Trpw, Textr);
509
510 return 0;
511}
512
Miquel Raynal6a9035c2018-07-20 17:15:14 +0200513static int tango_attach_chip(struct nand_chip *chip)
514{
515 struct nand_ecc_ctrl *ecc = &chip->ecc;
516
517 ecc->mode = NAND_ECC_HW;
518 ecc->algo = NAND_ECC_BCH;
519 ecc->bytes = DIV_ROUND_UP(ecc->strength * FIELD_ORDER, BITS_PER_BYTE);
520
521 ecc->read_page_raw = tango_read_page_raw;
522 ecc->write_page_raw = tango_write_page_raw;
523 ecc->read_page = tango_read_page;
524 ecc->write_page = tango_write_page;
525 ecc->read_oob = tango_read_oob;
526 ecc->write_oob = tango_write_oob;
527
528 return 0;
529}
530
531static const struct nand_controller_ops tango_controller_ops = {
532 .attach_chip = tango_attach_chip,
Boris Brezillon7a08dba2018-11-11 08:55:24 +0100533 .setup_data_interface = tango_set_timings,
Miquel Raynal6a9035c2018-07-20 17:15:14 +0200534};
535
Marc Gonzalez6956e232016-10-25 18:10:47 +0200536static int chip_init(struct device *dev, struct device_node *np)
537{
538 u32 cs;
539 int err, res;
540 struct mtd_info *mtd;
541 struct nand_chip *chip;
542 struct tango_chip *tchip;
543 struct nand_ecc_ctrl *ecc;
544 struct tango_nfc *nfc = dev_get_drvdata(dev);
545
546 tchip = devm_kzalloc(dev, sizeof(*tchip), GFP_KERNEL);
547 if (!tchip)
548 return -ENOMEM;
549
550 res = of_property_count_u32_elems(np, "reg");
551 if (res < 0)
552 return res;
553
554 if (res != 1)
555 return -ENOTSUPP; /* Multi-CS chips are not supported */
556
557 err = of_property_read_u32_index(np, "reg", 0, &cs);
558 if (err)
559 return err;
560
561 if (cs >= MAX_CS)
562 return -EINVAL;
563
564 chip = &tchip->nand_chip;
565 ecc = &chip->ecc;
Boris Brezillon8fcfba02016-11-21 10:03:04 +0100566 mtd = nand_to_mtd(chip);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200567
Boris Brezillon716bbba2018-09-07 00:38:35 +0200568 chip->legacy.read_byte = tango_read_byte;
569 chip->legacy.write_buf = tango_write_buf;
570 chip->legacy.read_buf = tango_read_buf;
Boris Brezillon7d6c37e2018-11-11 08:55:22 +0100571 chip->legacy.select_chip = tango_select_chip;
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200572 chip->legacy.cmd_ctrl = tango_cmd_ctrl;
Boris Brezillon8395b752018-09-07 00:38:37 +0200573 chip->legacy.dev_ready = tango_dev_ready;
Marc Gonzalez6956e232016-10-25 18:10:47 +0200574 chip->options = NAND_USE_BOUNCE_BUFFER |
575 NAND_NO_SUBPAGE_WRITE |
576 NAND_WAIT_TCCS;
577 chip->controller = &nfc->hw;
578 tchip->base = nfc->pbus_base + (cs * 256);
579
580 nand_set_flash_node(chip, np);
581 mtd_set_ooblayout(mtd, &tango_nand_ooblayout_ops);
582 mtd->dev.parent = dev;
583
Boris Brezillon00ad3782018-09-06 14:05:14 +0200584 err = nand_scan(chip, 1);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200585 if (err)
586 return err;
587
588 tchip->xfer_cfg = XFER_CFG(cs, 1, ecc->steps, METADATA_SIZE);
589 tchip->pkt_0_cfg = PKT_CFG(ecc->size + METADATA_SIZE, ecc->strength);
590 tchip->pkt_n_cfg = PKT_CFG(ecc->size, ecc->strength);
591 tchip->bb_cfg = BB_CFG(mtd->writesize, BBM_SIZE);
592
593 err = mtd_device_register(mtd, NULL, 0);
Miquel Raynal0eaa8792018-03-21 14:01:53 +0100594 if (err) {
595 nand_cleanup(chip);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200596 return err;
Miquel Raynal0eaa8792018-03-21 14:01:53 +0100597 }
Marc Gonzalez6956e232016-10-25 18:10:47 +0200598
599 nfc->chips[cs] = tchip;
600
601 return 0;
602}
603
604static int tango_nand_remove(struct platform_device *pdev)
605{
606 int cs;
607 struct tango_nfc *nfc = platform_get_drvdata(pdev);
608
609 dma_release_channel(nfc->chan);
610
611 for (cs = 0; cs < MAX_CS; ++cs) {
612 if (nfc->chips[cs])
Boris Brezillon59ac2762018-09-06 14:05:15 +0200613 nand_release(&nfc->chips[cs]->nand_chip);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200614 }
615
616 return 0;
617}
618
619static int tango_nand_probe(struct platform_device *pdev)
620{
621 int err;
622 struct clk *clk;
623 struct resource *res;
624 struct tango_nfc *nfc;
625 struct device_node *np;
626
627 nfc = devm_kzalloc(&pdev->dev, sizeof(*nfc), GFP_KERNEL);
628 if (!nfc)
629 return -ENOMEM;
630
631 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
632 nfc->reg_base = devm_ioremap_resource(&pdev->dev, res);
633 if (IS_ERR(nfc->reg_base))
634 return PTR_ERR(nfc->reg_base);
635
636 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
637 nfc->mem_base = devm_ioremap_resource(&pdev->dev, res);
638 if (IS_ERR(nfc->mem_base))
639 return PTR_ERR(nfc->mem_base);
640
641 res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
642 nfc->pbus_base = devm_ioremap_resource(&pdev->dev, res);
643 if (IS_ERR(nfc->pbus_base))
644 return PTR_ERR(nfc->pbus_base);
645
Marc Gonzalez8043d252017-01-03 11:01:14 +0100646 writel_relaxed(MODE_RAW, nfc->pbus_base + PBUS_PAD_MODE);
647
Marc Gonzalez007b4e82018-04-05 14:57:59 +0200648 clk = devm_clk_get(&pdev->dev, NULL);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200649 if (IS_ERR(clk))
650 return PTR_ERR(clk);
651
Marc Gonzalez7165b8a2016-12-19 15:30:12 +0100652 nfc->chan = dma_request_chan(&pdev->dev, "rxtx");
Marc Gonzalez6956e232016-10-25 18:10:47 +0200653 if (IS_ERR(nfc->chan))
654 return PTR_ERR(nfc->chan);
655
656 platform_set_drvdata(pdev, nfc);
Miquel Raynal7da45132018-07-17 09:08:02 +0200657 nand_controller_init(&nfc->hw);
Miquel Raynal6a9035c2018-07-20 17:15:14 +0200658 nfc->hw.ops = &tango_controller_ops;
Marc Gonzalez6956e232016-10-25 18:10:47 +0200659 nfc->freq_kHz = clk_get_rate(clk) / 1000;
660
661 for_each_child_of_node(pdev->dev.of_node, np) {
662 err = chip_init(&pdev->dev, np);
663 if (err) {
664 tango_nand_remove(pdev);
665 return err;
666 }
667 }
668
669 return 0;
670}
671
672static const struct of_device_id tango_nand_ids[] = {
673 { .compatible = "sigma,smp8758-nand" },
674 { /* sentinel */ }
675};
Andres Galacho2761b4f2017-05-01 16:30:15 -0400676MODULE_DEVICE_TABLE(of, tango_nand_ids);
Marc Gonzalez6956e232016-10-25 18:10:47 +0200677
678static struct platform_driver tango_nand_driver = {
679 .probe = tango_nand_probe,
680 .remove = tango_nand_remove,
681 .driver = {
682 .name = "tango-nand",
683 .of_match_table = tango_nand_ids,
684 },
685};
686
687module_platform_driver(tango_nand_driver);
688
689MODULE_LICENSE("GPL");
690MODULE_AUTHOR("Sigma Designs");
691MODULE_DESCRIPTION("Tango4 NAND Flash controller driver");