1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * ST Microelectronics
4 * Flexible Static Memory Controller (FSMC)
5 * Driver for NAND portions
6 *
7 * Copyright © 2010 ST Microelectronics
8 * Vipin Kumar <vipin.kumar@st.com>
9 * Ashish Priyadarshi
10 *
11 * Based on drivers/mtd/nand/nomadik_nand.c (removed in v3.8)
12 * Copyright © 2007 STMicroelectronics Pvt. Ltd.
13 * Copyright © 2009 Alessandro Rubini
14 */
15
16 #include <linux/clk.h>
17 #include <linux/completion.h>
18 #include <linux/delay.h>
19 #include <linux/dmaengine.h>
20 #include <linux/dma-direction.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/err.h>
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/resource.h>
26 #include <linux/sched.h>
27 #include <linux/types.h>
28 #include <linux/mtd/mtd.h>
29 #include <linux/mtd/nand-ecc-sw-hamming.h>
30 #include <linux/mtd/rawnand.h>
31 #include <linux/platform_device.h>
32 #include <linux/of.h>
33 #include <linux/mtd/partitions.h>
34 #include <linux/io.h>
35 #include <linux/slab.h>
36 #include <linux/amba/bus.h>
37 #include <mtd/mtd-abi.h>
38
39 /* fsmc controller registers for NOR flash */
40 #define CTRL 0x0
41 /* ctrl register definitions */
42 #define BANK_ENABLE BIT(0)
43 #define MUXED BIT(1)
44 #define NOR_DEV (2 << 2)
45 #define WIDTH_16 BIT(4)
46 #define RSTPWRDWN BIT(6)
47 #define WPROT BIT(7)
48 #define WRT_ENABLE BIT(12)
49 #define WAIT_ENB BIT(13)
50
51 #define CTRL_TIM 0x4
52 /* ctrl_tim register definitions */
53
54 #define FSMC_NOR_BANK_SZ 0x8
55 #define FSMC_NOR_REG_SIZE 0x40
56
57 #define FSMC_NOR_REG(base, bank, reg) ((base) + \
58 (FSMC_NOR_BANK_SZ * (bank)) + \
59 (reg))
60
61 /* fsmc controller registers for NAND flash */
62 #define FSMC_PC 0x00
63 /* pc register definitions */
64 #define FSMC_RESET BIT(0)
65 #define FSMC_WAITON BIT(1)
66 #define FSMC_ENABLE BIT(2)
67 #define FSMC_DEVTYPE_NAND BIT(3)
68 #define FSMC_DEVWID_16 BIT(4)
69 #define FSMC_ECCEN BIT(6)
70 #define FSMC_ECCPLEN_256 BIT(7)
71 #define FSMC_TCLR_SHIFT (9)
72 #define FSMC_TCLR_MASK (0xF)
73 #define FSMC_TAR_SHIFT (13)
74 #define FSMC_TAR_MASK (0xF)
75 #define STS 0x04
76 /* sts register definitions */
77 #define FSMC_CODE_RDY BIT(15)
78 #define COMM 0x08
79 /* comm register definitions */
80 #define FSMC_TSET_SHIFT 0
81 #define FSMC_TSET_MASK 0xFF
82 #define FSMC_TWAIT_SHIFT 8
83 #define FSMC_TWAIT_MASK 0xFF
84 #define FSMC_THOLD_SHIFT 16
85 #define FSMC_THOLD_MASK 0xFF
86 #define FSMC_THIZ_SHIFT 24
87 #define FSMC_THIZ_MASK 0xFF
88 #define ATTRIB 0x0C
89 #define IOATA 0x10
90 #define ECC1 0x14
91 #define ECC2 0x18
92 #define ECC3 0x1C
93 #define FSMC_NAND_BANK_SZ 0x20
94
95 #define FSMC_BUSY_WAIT_TIMEOUT (1 * HZ)
96
97 /*
98 * According to SPEAr300 Reference Manual (RM0082)
99 * TOUDEL = 7ns (Output delay from the flip-flops to the board)
100 * TINDEL = 5ns (Input delay from the board to the flipflop)
101 */
102 #define TOUTDEL 7000
103 #define TINDEL 5000
104
105 struct fsmc_nand_timings {
106 u8 tclr;
107 u8 tar;
108 u8 thiz;
109 u8 thold;
110 u8 twait;
111 u8 tset;
112 };
113
114 enum access_mode {
115 USE_DMA_ACCESS = 1,
116 USE_WORD_ACCESS,
117 };
118
119 /**
120 * struct fsmc_nand_data - structure for FSMC NAND device state
121 *
122 * @base: Inherit from the nand_controller struct
123 * @pid: Part ID on the AMBA PrimeCell format
124 * @nand: Chip related info for a NAND flash.
125 *
126 * @bank: Bank number for probed device.
127 * @dev: Parent device
128 * @mode: Access mode
129 * @clk: Clock structure for FSMC.
130 *
131 * @read_dma_chan: DMA channel for read access
132 * @write_dma_chan: DMA channel for write access to NAND
133 * @dma_access_complete: Completion structure
134 *
135 * @dev_timings: NAND timings
136 *
137 * @data_pa: NAND Physical port for Data.
138 * @data_va: NAND port for Data.
139 * @cmd_va: NAND port for Command.
140 * @addr_va: NAND port for Address.
141 * @regs_va: Registers base address for a given bank.
142 */
143 struct fsmc_nand_data {
144 struct nand_controller base;
145 u32 pid;
146 struct nand_chip nand;
147
148 unsigned int bank;
149 struct device *dev;
150 enum access_mode mode;
151 struct clk *clk;
152
153 /* DMA related objects */
154 struct dma_chan *read_dma_chan;
155 struct dma_chan *write_dma_chan;
156 struct completion dma_access_complete;
157
158 struct fsmc_nand_timings *dev_timings;
159
160 dma_addr_t data_pa;
161 void __iomem *data_va;
162 void __iomem *cmd_va;
163 void __iomem *addr_va;
164 void __iomem *regs_va;
165 };
166
fsmc_ecc1_ooblayout_ecc(struct mtd_info * mtd,int section,struct mtd_oob_region * oobregion)167 static int fsmc_ecc1_ooblayout_ecc(struct mtd_info *mtd, int section,
168 struct mtd_oob_region *oobregion)
169 {
170 struct nand_chip *chip = mtd_to_nand(mtd);
171
172 if (section >= chip->ecc.steps)
173 return -ERANGE;
174
175 oobregion->offset = (section * 16) + 2;
176 oobregion->length = 3;
177
178 return 0;
179 }
180
fsmc_ecc1_ooblayout_free(struct mtd_info * mtd,int section,struct mtd_oob_region * oobregion)181 static int fsmc_ecc1_ooblayout_free(struct mtd_info *mtd, int section,
182 struct mtd_oob_region *oobregion)
183 {
184 struct nand_chip *chip = mtd_to_nand(mtd);
185
186 if (section >= chip->ecc.steps)
187 return -ERANGE;
188
189 oobregion->offset = (section * 16) + 8;
190
191 if (section < chip->ecc.steps - 1)
192 oobregion->length = 8;
193 else
194 oobregion->length = mtd->oobsize - oobregion->offset;
195
196 return 0;
197 }
198
199 static const struct mtd_ooblayout_ops fsmc_ecc1_ooblayout_ops = {
200 .ecc = fsmc_ecc1_ooblayout_ecc,
201 .free = fsmc_ecc1_ooblayout_free,
202 };
203
204 /*
205 * ECC placement definitions in oobfree type format.
206 * There are 13 bytes of ecc for every 512 byte block and it has to be read
207 * consecutively and immediately after the 512 byte data block for hardware to
208 * generate the error bit offsets in 512 byte data.
209 */
fsmc_ecc4_ooblayout_ecc(struct mtd_info * mtd,int section,struct mtd_oob_region * oobregion)210 static int fsmc_ecc4_ooblayout_ecc(struct mtd_info *mtd, int section,
211 struct mtd_oob_region *oobregion)
212 {
213 struct nand_chip *chip = mtd_to_nand(mtd);
214
215 if (section >= chip->ecc.steps)
216 return -ERANGE;
217
218 oobregion->length = chip->ecc.bytes;
219
220 if (!section && mtd->writesize <= 512)
221 oobregion->offset = 0;
222 else
223 oobregion->offset = (section * 16) + 2;
224
225 return 0;
226 }
227
fsmc_ecc4_ooblayout_free(struct mtd_info * mtd,int section,struct mtd_oob_region * oobregion)228 static int fsmc_ecc4_ooblayout_free(struct mtd_info *mtd, int section,
229 struct mtd_oob_region *oobregion)
230 {
231 struct nand_chip *chip = mtd_to_nand(mtd);
232
233 if (section >= chip->ecc.steps)
234 return -ERANGE;
235
236 oobregion->offset = (section * 16) + 15;
237
238 if (section < chip->ecc.steps - 1)
239 oobregion->length = 3;
240 else
241 oobregion->length = mtd->oobsize - oobregion->offset;
242
243 return 0;
244 }
245
246 static const struct mtd_ooblayout_ops fsmc_ecc4_ooblayout_ops = {
247 .ecc = fsmc_ecc4_ooblayout_ecc,
248 .free = fsmc_ecc4_ooblayout_free,
249 };
250
nand_to_fsmc(struct nand_chip * chip)251 static inline struct fsmc_nand_data *nand_to_fsmc(struct nand_chip *chip)
252 {
253 return container_of(chip, struct fsmc_nand_data, nand);
254 }
255
256 /*
257 * fsmc_nand_setup - FSMC (Flexible Static Memory Controller) init routine
258 *
259 * This routine initializes timing parameters related to NAND memory access in
260 * FSMC registers
261 */
fsmc_nand_setup(struct fsmc_nand_data * host,struct fsmc_nand_timings * tims)262 static void fsmc_nand_setup(struct fsmc_nand_data *host,
263 struct fsmc_nand_timings *tims)
264 {
265 u32 value = FSMC_DEVTYPE_NAND | FSMC_ENABLE | FSMC_WAITON;
266 u32 tclr, tar, thiz, thold, twait, tset;
267
268 tclr = (tims->tclr & FSMC_TCLR_MASK) << FSMC_TCLR_SHIFT;
269 tar = (tims->tar & FSMC_TAR_MASK) << FSMC_TAR_SHIFT;
270 thiz = (tims->thiz & FSMC_THIZ_MASK) << FSMC_THIZ_SHIFT;
271 thold = (tims->thold & FSMC_THOLD_MASK) << FSMC_THOLD_SHIFT;
272 twait = (tims->twait & FSMC_TWAIT_MASK) << FSMC_TWAIT_SHIFT;
273 tset = (tims->tset & FSMC_TSET_MASK) << FSMC_TSET_SHIFT;
274
275 if (host->nand.options & NAND_BUSWIDTH_16)
276 value |= FSMC_DEVWID_16;
277
278 writel_relaxed(value | tclr | tar, host->regs_va + FSMC_PC);
279 writel_relaxed(thiz | thold | twait | tset, host->regs_va + COMM);
280 writel_relaxed(thiz | thold | twait | tset, host->regs_va + ATTRIB);
281 }
282
fsmc_calc_timings(struct fsmc_nand_data * host,const struct nand_sdr_timings * sdrt,struct fsmc_nand_timings * tims)283 static int fsmc_calc_timings(struct fsmc_nand_data *host,
284 const struct nand_sdr_timings *sdrt,
285 struct fsmc_nand_timings *tims)
286 {
287 unsigned long hclk = clk_get_rate(host->clk);
288 unsigned long hclkn = NSEC_PER_SEC / hclk;
289 u32 thiz, thold, twait, tset, twait_min;
290
291 if (sdrt->tRC_min < 30000)
292 return -EOPNOTSUPP;
293
294 tims->tar = DIV_ROUND_UP(sdrt->tAR_min / 1000, hclkn) - 1;
295 if (tims->tar > FSMC_TAR_MASK)
296 tims->tar = FSMC_TAR_MASK;
297 tims->tclr = DIV_ROUND_UP(sdrt->tCLR_min / 1000, hclkn) - 1;
298 if (tims->tclr > FSMC_TCLR_MASK)
299 tims->tclr = FSMC_TCLR_MASK;
300
301 thiz = sdrt->tCS_min - sdrt->tWP_min;
302 tims->thiz = DIV_ROUND_UP(thiz / 1000, hclkn);
303
304 thold = sdrt->tDH_min;
305 if (thold < sdrt->tCH_min)
306 thold = sdrt->tCH_min;
307 if (thold < sdrt->tCLH_min)
308 thold = sdrt->tCLH_min;
309 if (thold < sdrt->tWH_min)
310 thold = sdrt->tWH_min;
311 if (thold < sdrt->tALH_min)
312 thold = sdrt->tALH_min;
313 if (thold < sdrt->tREH_min)
314 thold = sdrt->tREH_min;
315 tims->thold = DIV_ROUND_UP(thold / 1000, hclkn);
316 if (tims->thold == 0)
317 tims->thold = 1;
318 else if (tims->thold > FSMC_THOLD_MASK)
319 tims->thold = FSMC_THOLD_MASK;
320
321 tset = max(sdrt->tCS_min - sdrt->tWP_min,
322 sdrt->tCEA_max - sdrt->tREA_max);
323 tims->tset = DIV_ROUND_UP(tset / 1000, hclkn) - 1;
324 if (tims->tset == 0)
325 tims->tset = 1;
326 else if (tims->tset > FSMC_TSET_MASK)
327 tims->tset = FSMC_TSET_MASK;
328
329 /*
330 * According to SPEAr300 Reference Manual (RM0082) which gives more
331 * information related to FSMSC timings than the SPEAr600 one (RM0305),
332 * twait >= tCEA - (tset * TCLK) + TOUTDEL + TINDEL
333 */
334 twait_min = sdrt->tCEA_max - ((tims->tset + 1) * hclkn * 1000)
335 + TOUTDEL + TINDEL;
336 twait = max3(sdrt->tRP_min, sdrt->tWP_min, twait_min);
337
338 tims->twait = DIV_ROUND_UP(twait / 1000, hclkn) - 1;
339 if (tims->twait == 0)
340 tims->twait = 1;
341 else if (tims->twait > FSMC_TWAIT_MASK)
342 tims->twait = FSMC_TWAIT_MASK;
343
344 return 0;
345 }
346
fsmc_setup_interface(struct nand_chip * nand,int csline,const struct nand_interface_config * conf)347 static int fsmc_setup_interface(struct nand_chip *nand, int csline,
348 const struct nand_interface_config *conf)
349 {
350 struct fsmc_nand_data *host = nand_to_fsmc(nand);
351 struct fsmc_nand_timings tims;
352 const struct nand_sdr_timings *sdrt;
353 int ret;
354
355 sdrt = nand_get_sdr_timings(conf);
356 if (IS_ERR(sdrt))
357 return PTR_ERR(sdrt);
358
359 ret = fsmc_calc_timings(host, sdrt, &tims);
360 if (ret)
361 return ret;
362
363 if (csline == NAND_DATA_IFACE_CHECK_ONLY)
364 return 0;
365
366 fsmc_nand_setup(host, &tims);
367
368 return 0;
369 }
370
371 /*
372 * fsmc_enable_hwecc - Enables Hardware ECC through FSMC registers
373 */
fsmc_enable_hwecc(struct nand_chip * chip,int mode)374 static void fsmc_enable_hwecc(struct nand_chip *chip, int mode)
375 {
376 struct fsmc_nand_data *host = nand_to_fsmc(chip);
377
378 writel_relaxed(readl(host->regs_va + FSMC_PC) & ~FSMC_ECCPLEN_256,
379 host->regs_va + FSMC_PC);
380 writel_relaxed(readl(host->regs_va + FSMC_PC) & ~FSMC_ECCEN,
381 host->regs_va + FSMC_PC);
382 writel_relaxed(readl(host->regs_va + FSMC_PC) | FSMC_ECCEN,
383 host->regs_va + FSMC_PC);
384 }
385
386 /*
387 * fsmc_read_hwecc_ecc4 - Hardware ECC calculator for ecc4 option supported by
388 * FSMC. ECC is 13 bytes for 512 bytes of data (supports error correction up to
389 * max of 8-bits)
390 */
fsmc_read_hwecc_ecc4(struct nand_chip * chip,const u8 * data,u8 * ecc)391 static int fsmc_read_hwecc_ecc4(struct nand_chip *chip, const u8 *data,
392 u8 *ecc)
393 {
394 struct fsmc_nand_data *host = nand_to_fsmc(chip);
395 u32 ecc_tmp;
396 unsigned long deadline = jiffies + FSMC_BUSY_WAIT_TIMEOUT;
397
398 do {
399 if (readl_relaxed(host->regs_va + STS) & FSMC_CODE_RDY)
400 break;
401
402 cond_resched();
403 } while (!time_after_eq(jiffies, deadline));
404
405 if (time_after_eq(jiffies, deadline)) {
406 dev_err(host->dev, "calculate ecc timed out\n");
407 return -ETIMEDOUT;
408 }
409
410 ecc_tmp = readl_relaxed(host->regs_va + ECC1);
411 ecc[0] = ecc_tmp;
412 ecc[1] = ecc_tmp >> 8;
413 ecc[2] = ecc_tmp >> 16;
414 ecc[3] = ecc_tmp >> 24;
415
416 ecc_tmp = readl_relaxed(host->regs_va + ECC2);
417 ecc[4] = ecc_tmp;
418 ecc[5] = ecc_tmp >> 8;
419 ecc[6] = ecc_tmp >> 16;
420 ecc[7] = ecc_tmp >> 24;
421
422 ecc_tmp = readl_relaxed(host->regs_va + ECC3);
423 ecc[8] = ecc_tmp;
424 ecc[9] = ecc_tmp >> 8;
425 ecc[10] = ecc_tmp >> 16;
426 ecc[11] = ecc_tmp >> 24;
427
428 ecc_tmp = readl_relaxed(host->regs_va + STS);
429 ecc[12] = ecc_tmp >> 16;
430
431 return 0;
432 }
433
434 /*
435 * fsmc_read_hwecc_ecc1 - Hardware ECC calculator for ecc1 option supported by
436 * FSMC. ECC is 3 bytes for 512 bytes of data (supports error correction up to
437 * max of 1-bit)
438 */
fsmc_read_hwecc_ecc1(struct nand_chip * chip,const u8 * data,u8 * ecc)439 static int fsmc_read_hwecc_ecc1(struct nand_chip *chip, const u8 *data,
440 u8 *ecc)
441 {
442 struct fsmc_nand_data *host = nand_to_fsmc(chip);
443 u32 ecc_tmp;
444
445 ecc_tmp = readl_relaxed(host->regs_va + ECC1);
446 ecc[0] = ecc_tmp;
447 ecc[1] = ecc_tmp >> 8;
448 ecc[2] = ecc_tmp >> 16;
449
450 return 0;
451 }
452
fsmc_correct_ecc1(struct nand_chip * chip,unsigned char * buf,unsigned char * read_ecc,unsigned char * calc_ecc)453 static int fsmc_correct_ecc1(struct nand_chip *chip,
454 unsigned char *buf,
455 unsigned char *read_ecc,
456 unsigned char *calc_ecc)
457 {
458 bool sm_order = chip->ecc.options & NAND_ECC_SOFT_HAMMING_SM_ORDER;
459
460 return ecc_sw_hamming_correct(buf, read_ecc, calc_ecc,
461 chip->ecc.size, sm_order);
462 }
463
464 /* Count the number of 0's in buff upto a max of max_bits */
count_written_bits(u8 * buff,int size,int max_bits)465 static int count_written_bits(u8 *buff, int size, int max_bits)
466 {
467 int k, written_bits = 0;
468
469 for (k = 0; k < size; k++) {
470 written_bits += hweight8(~buff[k]);
471 if (written_bits > max_bits)
472 break;
473 }
474
475 return written_bits;
476 }
477
dma_complete(void * param)478 static void dma_complete(void *param)
479 {
480 struct fsmc_nand_data *host = param;
481
482 complete(&host->dma_access_complete);
483 }
484
dma_xfer(struct fsmc_nand_data * host,void * buffer,int len,enum dma_data_direction direction)485 static int dma_xfer(struct fsmc_nand_data *host, void *buffer, int len,
486 enum dma_data_direction direction)
487 {
488 struct dma_chan *chan;
489 struct dma_device *dma_dev;
490 struct dma_async_tx_descriptor *tx;
491 dma_addr_t dma_dst, dma_src, dma_addr;
492 dma_cookie_t cookie;
493 unsigned long flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
494 int ret;
495 unsigned long time_left;
496
497 if (direction == DMA_TO_DEVICE)
498 chan = host->write_dma_chan;
499 else if (direction == DMA_FROM_DEVICE)
500 chan = host->read_dma_chan;
501 else
502 return -EINVAL;
503
504 dma_dev = chan->device;
505 dma_addr = dma_map_single(dma_dev->dev, buffer, len, direction);
506 if (dma_mapping_error(dma_dev->dev, dma_addr))
507 return -EINVAL;
508
509 if (direction == DMA_TO_DEVICE) {
510 dma_src = dma_addr;
511 dma_dst = host->data_pa;
512 } else {
513 dma_src = host->data_pa;
514 dma_dst = dma_addr;
515 }
516
517 tx = dma_dev->device_prep_dma_memcpy(chan, dma_dst, dma_src,
518 len, flags);
519 if (!tx) {
520 dev_err(host->dev, "device_prep_dma_memcpy error\n");
521 ret = -EIO;
522 goto unmap_dma;
523 }
524
525 tx->callback = dma_complete;
526 tx->callback_param = host;
527 cookie = tx->tx_submit(tx);
528
529 ret = dma_submit_error(cookie);
530 if (ret) {
531 dev_err(host->dev, "dma_submit_error %d\n", cookie);
532 goto unmap_dma;
533 }
534
535 dma_async_issue_pending(chan);
536
537 time_left =
538 wait_for_completion_timeout(&host->dma_access_complete,
539 msecs_to_jiffies(3000));
540 if (time_left == 0) {
541 dmaengine_terminate_all(chan);
542 dev_err(host->dev, "wait_for_completion_timeout\n");
543 ret = -ETIMEDOUT;
544 goto unmap_dma;
545 }
546
547 ret = 0;
548
549 unmap_dma:
550 dma_unmap_single(dma_dev->dev, dma_addr, len, direction);
551
552 return ret;
553 }
554
555 /*
556 * fsmc_write_buf - write buffer to chip
557 * @host: FSMC NAND controller
558 * @buf: data buffer
559 * @len: number of bytes to write
560 */
fsmc_write_buf(struct fsmc_nand_data * host,const u8 * buf,int len)561 static void fsmc_write_buf(struct fsmc_nand_data *host, const u8 *buf,
562 int len)
563 {
564 int i;
565
566 if (IS_ALIGNED((uintptr_t)buf, sizeof(u32)) &&
567 IS_ALIGNED(len, sizeof(u32))) {
568 u32 *p = (u32 *)buf;
569
570 len = len >> 2;
571 for (i = 0; i < len; i++)
572 writel_relaxed(p[i], host->data_va);
573 } else {
574 for (i = 0; i < len; i++)
575 writeb_relaxed(buf[i], host->data_va);
576 }
577 }
578
579 /*
580 * fsmc_read_buf - read chip data into buffer
581 * @host: FSMC NAND controller
582 * @buf: buffer to store date
583 * @len: number of bytes to read
584 */
fsmc_read_buf(struct fsmc_nand_data * host,u8 * buf,int len)585 static void fsmc_read_buf(struct fsmc_nand_data *host, u8 *buf, int len)
586 {
587 int i;
588
589 if (IS_ALIGNED((uintptr_t)buf, sizeof(u32)) &&
590 IS_ALIGNED(len, sizeof(u32))) {
591 u32 *p = (u32 *)buf;
592
593 len = len >> 2;
594 for (i = 0; i < len; i++)
595 p[i] = readl_relaxed(host->data_va);
596 } else {
597 for (i = 0; i < len; i++)
598 buf[i] = readb_relaxed(host->data_va);
599 }
600 }
601
602 /*
603 * fsmc_read_buf_dma - read chip data into buffer
604 * @host: FSMC NAND controller
605 * @buf: buffer to store date
606 * @len: number of bytes to read
607 */
fsmc_read_buf_dma(struct fsmc_nand_data * host,u8 * buf,int len)608 static void fsmc_read_buf_dma(struct fsmc_nand_data *host, u8 *buf,
609 int len)
610 {
611 dma_xfer(host, buf, len, DMA_FROM_DEVICE);
612 }
613
614 /*
615 * fsmc_write_buf_dma - write buffer to chip
616 * @host: FSMC NAND controller
617 * @buf: data buffer
618 * @len: number of bytes to write
619 */
fsmc_write_buf_dma(struct fsmc_nand_data * host,const u8 * buf,int len)620 static void fsmc_write_buf_dma(struct fsmc_nand_data *host, const u8 *buf,
621 int len)
622 {
623 dma_xfer(host, (void *)buf, len, DMA_TO_DEVICE);
624 }
625
626 /*
627 * fsmc_exec_op - hook called by the core to execute NAND operations
628 *
629 * This controller is simple enough and thus does not need to use the parser
630 * provided by the core, instead, handle every situation here.
631 */
fsmc_exec_op(struct nand_chip * chip,const struct nand_operation * op,bool check_only)632 static int fsmc_exec_op(struct nand_chip *chip, const struct nand_operation *op,
633 bool check_only)
634 {
635 struct fsmc_nand_data *host = nand_to_fsmc(chip);
636 const struct nand_op_instr *instr = NULL;
637 int ret = 0;
638 unsigned int op_id;
639 int i;
640
641 if (check_only)
642 return 0;
643
644 pr_debug("Executing operation [%d instructions]:\n", op->ninstrs);
645
646 for (op_id = 0; op_id < op->ninstrs; op_id++) {
647 instr = &op->instrs[op_id];
648
649 nand_op_trace(" ", instr);
650
651 switch (instr->type) {
652 case NAND_OP_CMD_INSTR:
653 writeb_relaxed(instr->ctx.cmd.opcode, host->cmd_va);
654 break;
655
656 case NAND_OP_ADDR_INSTR:
657 for (i = 0; i < instr->ctx.addr.naddrs; i++)
658 writeb_relaxed(instr->ctx.addr.addrs[i],
659 host->addr_va);
660 break;
661
662 case NAND_OP_DATA_IN_INSTR:
663 if (host->mode == USE_DMA_ACCESS)
664 fsmc_read_buf_dma(host, instr->ctx.data.buf.in,
665 instr->ctx.data.len);
666 else
667 fsmc_read_buf(host, instr->ctx.data.buf.in,
668 instr->ctx.data.len);
669 break;
670
671 case NAND_OP_DATA_OUT_INSTR:
672 if (host->mode == USE_DMA_ACCESS)
673 fsmc_write_buf_dma(host,
674 instr->ctx.data.buf.out,
675 instr->ctx.data.len);
676 else
677 fsmc_write_buf(host, instr->ctx.data.buf.out,
678 instr->ctx.data.len);
679 break;
680
681 case NAND_OP_WAITRDY_INSTR:
682 ret = nand_soft_waitrdy(chip,
683 instr->ctx.waitrdy.timeout_ms);
684 break;
685 }
686
687 if (instr->delay_ns)
688 ndelay(instr->delay_ns);
689 }
690
691 return ret;
692 }
693
694 /*
695 * fsmc_read_page_hwecc
696 * @chip: nand chip info structure
697 * @buf: buffer to store read data
698 * @oob_required: caller expects OOB data read to chip->oob_poi
699 * @page: page number to read
700 *
701 * This routine is needed for fsmc version 8 as reading from NAND chip has to be
702 * performed in a strict sequence as follows:
703 * data(512 byte) -> ecc(13 byte)
704 * After this read, fsmc hardware generates and reports error data bits(up to a
705 * max of 8 bits)
706 */
fsmc_read_page_hwecc(struct nand_chip * chip,u8 * buf,int oob_required,int page)707 static int fsmc_read_page_hwecc(struct nand_chip *chip, u8 *buf,
708 int oob_required, int page)
709 {
710 struct mtd_info *mtd = nand_to_mtd(chip);
711 int i, j, s, stat, eccsize = chip->ecc.size;
712 int eccbytes = chip->ecc.bytes;
713 int eccsteps = chip->ecc.steps;
714 u8 *p = buf;
715 u8 *ecc_calc = chip->ecc.calc_buf;
716 u8 *ecc_code = chip->ecc.code_buf;
717 int off, len, ret, group = 0;
718 /*
719 * ecc_oob is intentionally taken as u16. In 16bit devices, we
720 * end up reading 14 bytes (7 words) from oob. The local array is
721 * to maintain word alignment
722 */
723 u16 ecc_oob[7];
724 u8 *oob = (u8 *)&ecc_oob[0];
725 unsigned int max_bitflips = 0;
726
727 for (i = 0, s = 0; s < eccsteps; s++, i += eccbytes, p += eccsize) {
728 nand_read_page_op(chip, page, s * eccsize, NULL, 0);
729 chip->ecc.hwctl(chip, NAND_ECC_READ);
730 ret = nand_read_data_op(chip, p, eccsize, false, false);
731 if (ret)
732 return ret;
733
734 for (j = 0; j < eccbytes;) {
735 struct mtd_oob_region oobregion;
736
737 ret = mtd_ooblayout_ecc(mtd, group++, &oobregion);
738 if (ret)
739 return ret;
740
741 off = oobregion.offset;
742 len = oobregion.length;
743
744 /*
745 * length is intentionally kept a higher multiple of 2
746 * to read at least 13 bytes even in case of 16 bit NAND
747 * devices
748 */
749 if (chip->options & NAND_BUSWIDTH_16)
750 len = roundup(len, 2);
751
752 nand_read_oob_op(chip, page, off, oob + j, len);
753 j += len;
754 }
755
756 memcpy(&ecc_code[i], oob, chip->ecc.bytes);
757 chip->ecc.calculate(chip, p, &ecc_calc[i]);
758
759 stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]);
760 if (stat < 0) {
761 mtd->ecc_stats.failed++;
762 } else {
763 mtd->ecc_stats.corrected += stat;
764 max_bitflips = max_t(unsigned int, max_bitflips, stat);
765 }
766 }
767
768 return max_bitflips;
769 }
770
771 /*
772 * fsmc_bch8_correct_data
773 * @mtd: mtd info structure
774 * @dat: buffer of read data
775 * @read_ecc: ecc read from device spare area
776 * @calc_ecc: ecc calculated from read data
777 *
778 * calc_ecc is a 104 bit information containing maximum of 8 error
779 * offset information of 13 bits each in 512 bytes of read data.
780 */
fsmc_bch8_correct_data(struct nand_chip * chip,u8 * dat,u8 * read_ecc,u8 * calc_ecc)781 static int fsmc_bch8_correct_data(struct nand_chip *chip, u8 *dat,
782 u8 *read_ecc, u8 *calc_ecc)
783 {
784 struct fsmc_nand_data *host = nand_to_fsmc(chip);
785 u32 err_idx[8];
786 u32 num_err, i;
787 u32 ecc1, ecc2, ecc3, ecc4;
788
789 num_err = (readl_relaxed(host->regs_va + STS) >> 10) & 0xF;
790
791 /* no bit flipping */
792 if (likely(num_err == 0))
793 return 0;
794
795 /* too many errors */
796 if (unlikely(num_err > 8)) {
797 /*
798 * This is a temporary erase check. A newly erased page read
799 * would result in an ecc error because the oob data is also
800 * erased to FF and the calculated ecc for an FF data is not
801 * FF..FF.
802 * This is a workaround to skip performing correction in case
803 * data is FF..FF
804 *
805 * Logic:
806 * For every page, each bit written as 0 is counted until these
807 * number of bits are greater than 8 (the maximum correction
808 * capability of FSMC for each 512 + 13 bytes)
809 */
810
811 int bits_ecc = count_written_bits(read_ecc, chip->ecc.bytes, 8);
812 int bits_data = count_written_bits(dat, chip->ecc.size, 8);
813
814 if ((bits_ecc + bits_data) <= 8) {
815 if (bits_data)
816 memset(dat, 0xff, chip->ecc.size);
817 return bits_data;
818 }
819
820 return -EBADMSG;
821 }
822
823 /*
824 * ------------------- calc_ecc[] bit wise -----------|--13 bits--|
825 * |---idx[7]--|--.....-----|---idx[2]--||---idx[1]--||---idx[0]--|
826 *
827 * calc_ecc is a 104 bit information containing maximum of 8 error
828 * offset information of 13 bits each. calc_ecc is copied into a
829 * u64 array and error offset indexes are populated in err_idx
830 * array
831 */
832 ecc1 = readl_relaxed(host->regs_va + ECC1);
833 ecc2 = readl_relaxed(host->regs_va + ECC2);
834 ecc3 = readl_relaxed(host->regs_va + ECC3);
835 ecc4 = readl_relaxed(host->regs_va + STS);
836
837 err_idx[0] = (ecc1 >> 0) & 0x1FFF;
838 err_idx[1] = (ecc1 >> 13) & 0x1FFF;
839 err_idx[2] = (((ecc2 >> 0) & 0x7F) << 6) | ((ecc1 >> 26) & 0x3F);
840 err_idx[3] = (ecc2 >> 7) & 0x1FFF;
841 err_idx[4] = (((ecc3 >> 0) & 0x1) << 12) | ((ecc2 >> 20) & 0xFFF);
842 err_idx[5] = (ecc3 >> 1) & 0x1FFF;
843 err_idx[6] = (ecc3 >> 14) & 0x1FFF;
844 err_idx[7] = (((ecc4 >> 16) & 0xFF) << 5) | ((ecc3 >> 27) & 0x1F);
845
846 i = 0;
847 while (num_err--) {
848 err_idx[i] ^= 3;
849
850 if (err_idx[i] < chip->ecc.size * 8) {
851 int err = err_idx[i];
852
853 dat[err >> 3] ^= BIT(err & 7);
854 i++;
855 }
856 }
857 return i;
858 }
859
filter(struct dma_chan * chan,void * slave)860 static bool filter(struct dma_chan *chan, void *slave)
861 {
862 chan->private = slave;
863 return true;
864 }
865
fsmc_nand_probe_config_dt(struct platform_device * pdev,struct fsmc_nand_data * host,struct nand_chip * nand)866 static int fsmc_nand_probe_config_dt(struct platform_device *pdev,
867 struct fsmc_nand_data *host,
868 struct nand_chip *nand)
869 {
870 struct device_node *np = pdev->dev.of_node;
871 u32 val;
872 int ret;
873
874 nand->options = 0;
875
876 if (!of_property_read_u32(np, "bank-width", &val)) {
877 if (val == 2) {
878 nand->options |= NAND_BUSWIDTH_16;
879 } else if (val == 1) {
880 nand->options |= NAND_BUSWIDTH_AUTO;
881 } else {
882 dev_err(&pdev->dev, "invalid bank-width %u\n", val);
883 return -EINVAL;
884 }
885 } else {
886 nand->options |= NAND_BUSWIDTH_AUTO;
887 }
888
889 if (of_property_read_bool(np, "nand-skip-bbtscan"))
890 nand->options |= NAND_SKIP_BBTSCAN;
891
892 host->dev_timings = devm_kzalloc(&pdev->dev,
893 sizeof(*host->dev_timings),
894 GFP_KERNEL);
895 if (!host->dev_timings)
896 return -ENOMEM;
897
898 ret = of_property_read_u8_array(np, "timings", (u8 *)host->dev_timings,
899 sizeof(*host->dev_timings));
900 if (ret)
901 host->dev_timings = NULL;
902
903 /* Set default NAND bank to 0 */
904 host->bank = 0;
905 if (!of_property_read_u32(np, "bank", &val)) {
906 if (val > 3) {
907 dev_err(&pdev->dev, "invalid bank %u\n", val);
908 return -EINVAL;
909 }
910 host->bank = val;
911 }
912 return 0;
913 }
914
fsmc_nand_attach_chip(struct nand_chip * nand)915 static int fsmc_nand_attach_chip(struct nand_chip *nand)
916 {
917 struct mtd_info *mtd = nand_to_mtd(nand);
918 struct fsmc_nand_data *host = nand_to_fsmc(nand);
919
920 if (nand->ecc.engine_type == NAND_ECC_ENGINE_TYPE_INVALID)
921 nand->ecc.engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST;
922
923 if (!nand->ecc.size)
924 nand->ecc.size = 512;
925
926 if (AMBA_REV_BITS(host->pid) >= 8) {
927 nand->ecc.read_page = fsmc_read_page_hwecc;
928 nand->ecc.calculate = fsmc_read_hwecc_ecc4;
929 nand->ecc.correct = fsmc_bch8_correct_data;
930 nand->ecc.bytes = 13;
931 nand->ecc.strength = 8;
932 }
933
934 if (AMBA_REV_BITS(host->pid) >= 8) {
935 switch (mtd->oobsize) {
936 case 16:
937 case 64:
938 case 128:
939 case 224:
940 case 256:
941 break;
942 default:
943 dev_warn(host->dev,
944 "No oob scheme defined for oobsize %d\n",
945 mtd->oobsize);
946 return -EINVAL;
947 }
948
949 mtd_set_ooblayout(mtd, &fsmc_ecc4_ooblayout_ops);
950
951 return 0;
952 }
953
954 switch (nand->ecc.engine_type) {
955 case NAND_ECC_ENGINE_TYPE_ON_HOST:
956 dev_info(host->dev, "Using 1-bit HW ECC scheme\n");
957 nand->ecc.calculate = fsmc_read_hwecc_ecc1;
958 nand->ecc.correct = fsmc_correct_ecc1;
959 nand->ecc.hwctl = fsmc_enable_hwecc;
960 nand->ecc.bytes = 3;
961 nand->ecc.strength = 1;
962 nand->ecc.options |= NAND_ECC_SOFT_HAMMING_SM_ORDER;
963 break;
964
965 case NAND_ECC_ENGINE_TYPE_SOFT:
966 if (nand->ecc.algo == NAND_ECC_ALGO_BCH) {
967 dev_info(host->dev,
968 "Using 4-bit SW BCH ECC scheme\n");
969 break;
970 }
971 break;
972
973 case NAND_ECC_ENGINE_TYPE_ON_DIE:
974 break;
975
976 default:
977 dev_err(host->dev, "Unsupported ECC mode!\n");
978 return -ENOTSUPP;
979 }
980
981 /*
982 * Don't set layout for BCH4 SW ECC. This will be
983 * generated later during BCH initialization.
984 */
985 if (nand->ecc.engine_type == NAND_ECC_ENGINE_TYPE_ON_HOST) {
986 switch (mtd->oobsize) {
987 case 16:
988 case 64:
989 case 128:
990 mtd_set_ooblayout(mtd,
991 &fsmc_ecc1_ooblayout_ops);
992 break;
993 default:
994 dev_warn(host->dev,
995 "No oob scheme defined for oobsize %d\n",
996 mtd->oobsize);
997 return -EINVAL;
998 }
999 }
1000
1001 return 0;
1002 }
1003
1004 static const struct nand_controller_ops fsmc_nand_controller_ops = {
1005 .attach_chip = fsmc_nand_attach_chip,
1006 .exec_op = fsmc_exec_op,
1007 .setup_interface = fsmc_setup_interface,
1008 };
1009
1010 /**
1011 * fsmc_nand_disable() - Disables the NAND bank
1012 * @host: The instance to disable
1013 */
fsmc_nand_disable(struct fsmc_nand_data * host)1014 static void fsmc_nand_disable(struct fsmc_nand_data *host)
1015 {
1016 u32 val;
1017
1018 val = readl(host->regs_va + FSMC_PC);
1019 val &= ~FSMC_ENABLE;
1020 writel(val, host->regs_va + FSMC_PC);
1021 }
1022
1023 /*
1024 * fsmc_nand_probe - Probe function
1025 * @pdev: platform device structure
1026 */
fsmc_nand_probe(struct platform_device * pdev)1027 static int __init fsmc_nand_probe(struct platform_device *pdev)
1028 {
1029 struct fsmc_nand_data *host;
1030 struct mtd_info *mtd;
1031 struct nand_chip *nand;
1032 struct resource *res;
1033 void __iomem *base;
1034 dma_cap_mask_t mask;
1035 int ret = 0;
1036 u32 pid;
1037 int i;
1038
1039 /* Allocate memory for the device structure (and zero it) */
1040 host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
1041 if (!host)
1042 return -ENOMEM;
1043
1044 nand = &host->nand;
1045
1046 ret = fsmc_nand_probe_config_dt(pdev, host, nand);
1047 if (ret)
1048 return ret;
1049
1050 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_data");
1051 host->data_va = devm_ioremap_resource(&pdev->dev, res);
1052 if (IS_ERR(host->data_va))
1053 return PTR_ERR(host->data_va);
1054
1055 host->data_pa = (dma_addr_t)res->start;
1056
1057 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_addr");
1058 host->addr_va = devm_ioremap_resource(&pdev->dev, res);
1059 if (IS_ERR(host->addr_va))
1060 return PTR_ERR(host->addr_va);
1061
1062 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_cmd");
1063 host->cmd_va = devm_ioremap_resource(&pdev->dev, res);
1064 if (IS_ERR(host->cmd_va))
1065 return PTR_ERR(host->cmd_va);
1066
1067 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "fsmc_regs");
1068 base = devm_ioremap_resource(&pdev->dev, res);
1069 if (IS_ERR(base))
1070 return PTR_ERR(base);
1071
1072 host->regs_va = base + FSMC_NOR_REG_SIZE +
1073 (host->bank * FSMC_NAND_BANK_SZ);
1074
1075 host->clk = devm_clk_get_enabled(&pdev->dev, NULL);
1076 if (IS_ERR(host->clk)) {
1077 dev_err(&pdev->dev, "failed to fetch block clock\n");
1078 return PTR_ERR(host->clk);
1079 }
1080
1081 /*
1082 * This device ID is actually a common AMBA ID as used on the
1083 * AMBA PrimeCell bus. However it is not a PrimeCell.
1084 */
1085 for (pid = 0, i = 0; i < 4; i++)
1086 pid |= (readl(base + resource_size(res) - 0x20 + 4 * i) &
1087 255) << (i * 8);
1088
1089 host->pid = pid;
1090
1091 dev_info(&pdev->dev,
1092 "FSMC device partno %03x, manufacturer %02x, revision %02x, config %02x\n",
1093 AMBA_PART_BITS(pid), AMBA_MANF_BITS(pid),
1094 AMBA_REV_BITS(pid), AMBA_CONFIG_BITS(pid));
1095
1096 host->dev = &pdev->dev;
1097
1098 if (host->mode == USE_DMA_ACCESS)
1099 init_completion(&host->dma_access_complete);
1100
1101 /* Link all private pointers */
1102 mtd = nand_to_mtd(&host->nand);
1103 nand_set_flash_node(nand, pdev->dev.of_node);
1104
1105 mtd->dev.parent = &pdev->dev;
1106
1107 nand->badblockbits = 7;
1108
1109 if (host->mode == USE_DMA_ACCESS) {
1110 dma_cap_zero(mask);
1111 dma_cap_set(DMA_MEMCPY, mask);
1112 host->read_dma_chan = dma_request_channel(mask, filter, NULL);
1113 if (!host->read_dma_chan) {
1114 dev_err(&pdev->dev, "Unable to get read dma channel\n");
1115 ret = -ENODEV;
1116 goto disable_fsmc;
1117 }
1118 host->write_dma_chan = dma_request_channel(mask, filter, NULL);
1119 if (!host->write_dma_chan) {
1120 dev_err(&pdev->dev, "Unable to get write dma channel\n");
1121 ret = -ENODEV;
1122 goto release_dma_read_chan;
1123 }
1124 }
1125
1126 if (host->dev_timings) {
1127 fsmc_nand_setup(host, host->dev_timings);
1128 nand->options |= NAND_KEEP_TIMINGS;
1129 }
1130
1131 nand_controller_init(&host->base);
1132 host->base.ops = &fsmc_nand_controller_ops;
1133 nand->controller = &host->base;
1134
1135 /*
1136 * Scan to find existence of the device
1137 */
1138 ret = nand_scan(nand, 1);
1139 if (ret)
1140 goto release_dma_write_chan;
1141
1142 mtd->name = "nand";
1143 ret = mtd_device_register(mtd, NULL, 0);
1144 if (ret)
1145 goto cleanup_nand;
1146
1147 platform_set_drvdata(pdev, host);
1148 dev_info(&pdev->dev, "FSMC NAND driver registration successful\n");
1149
1150 return 0;
1151
1152 cleanup_nand:
1153 nand_cleanup(nand);
1154 release_dma_write_chan:
1155 if (host->mode == USE_DMA_ACCESS)
1156 dma_release_channel(host->write_dma_chan);
1157 release_dma_read_chan:
1158 if (host->mode == USE_DMA_ACCESS)
1159 dma_release_channel(host->read_dma_chan);
1160 disable_fsmc:
1161 fsmc_nand_disable(host);
1162
1163 return ret;
1164 }
1165
1166 /*
1167 * Clean up routine
1168 */
fsmc_nand_remove(struct platform_device * pdev)1169 static void fsmc_nand_remove(struct platform_device *pdev)
1170 {
1171 struct fsmc_nand_data *host = platform_get_drvdata(pdev);
1172
1173 if (host) {
1174 struct nand_chip *chip = &host->nand;
1175 int ret;
1176
1177 ret = mtd_device_unregister(nand_to_mtd(chip));
1178 WARN_ON(ret);
1179 nand_cleanup(chip);
1180 fsmc_nand_disable(host);
1181
1182 if (host->mode == USE_DMA_ACCESS) {
1183 dma_release_channel(host->write_dma_chan);
1184 dma_release_channel(host->read_dma_chan);
1185 }
1186 }
1187 }
1188
1189 #ifdef CONFIG_PM_SLEEP
fsmc_nand_suspend(struct device * dev)1190 static int fsmc_nand_suspend(struct device *dev)
1191 {
1192 struct fsmc_nand_data *host = dev_get_drvdata(dev);
1193
1194 if (host)
1195 clk_disable_unprepare(host->clk);
1196
1197 return 0;
1198 }
1199
fsmc_nand_resume(struct device * dev)1200 static int fsmc_nand_resume(struct device *dev)
1201 {
1202 struct fsmc_nand_data *host = dev_get_drvdata(dev);
1203 int ret;
1204
1205 if (host) {
1206 ret = clk_prepare_enable(host->clk);
1207 if (ret) {
1208 dev_err(dev, "failed to enable clk\n");
1209 return ret;
1210 }
1211 if (host->dev_timings)
1212 fsmc_nand_setup(host, host->dev_timings);
1213 nand_reset(&host->nand, 0);
1214 }
1215
1216 return 0;
1217 }
1218 #endif
1219
1220 static SIMPLE_DEV_PM_OPS(fsmc_nand_pm_ops, fsmc_nand_suspend, fsmc_nand_resume);
1221
1222 static const struct of_device_id fsmc_nand_id_table[] = {
1223 { .compatible = "st,spear600-fsmc-nand" },
1224 { .compatible = "stericsson,fsmc-nand" },
1225 {}
1226 };
1227 MODULE_DEVICE_TABLE(of, fsmc_nand_id_table);
1228
1229 static struct platform_driver fsmc_nand_driver = {
1230 .remove = fsmc_nand_remove,
1231 .driver = {
1232 .name = "fsmc-nand",
1233 .of_match_table = fsmc_nand_id_table,
1234 .pm = &fsmc_nand_pm_ops,
1235 },
1236 };
1237
1238 module_platform_driver_probe(fsmc_nand_driver, fsmc_nand_probe);
1239
1240 MODULE_LICENSE("GPL v2");
1241 MODULE_AUTHOR("Vipin Kumar <vipin.kumar@st.com>, Ashish Priyadarshi");
1242 MODULE_DESCRIPTION("NAND driver for SPEAr Platforms");
1243