1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <linux/delay.h>
21#include <linux/kernel.h>
22#include <linux/init.h>
23#include <linux/slab.h>
24#include <linux/rslib.h>
25#include <linux/bitrev.h>
26#include <linux/module.h>
27#include <linux/mtd/compatmac.h>
28#include <linux/mtd/mtd.h>
29#include <linux/mtd/nand.h>
30#include <linux/mtd/partitions.h>
31#include <asm/io.h>
32
33
34
35
36static struct mtd_info *rtc_from4_mtd = NULL;
37
38#define RTC_FROM4_MAX_CHIPS 2
39
40
41#define SH77X9_BCR1 ((volatile unsigned short *)(0xFFFFFF60))
42#define SH77X9_BCR2 ((volatile unsigned short *)(0xFFFFFF62))
43#define SH77X9_WCR1 ((volatile unsigned short *)(0xFFFFFF64))
44#define SH77X9_WCR2 ((volatile unsigned short *)(0xFFFFFF66))
45#define SH77X9_MCR ((volatile unsigned short *)(0xFFFFFF68))
46#define SH77X9_PCR ((volatile unsigned short *)(0xFFFFFF6C))
47#define SH77X9_FRQCR ((volatile unsigned short *)(0xFFFFFF80))
48
49
50
51
52
53#define RTC_FROM4_FIO_BASE 0x14000000
54
55
56#define RTC_FROM4_CLE (1 << 5)
57#define RTC_FROM4_ALE (1 << 4)
58
59
60#define RTC_FROM4_NAND_ADDR_SLOT3 (0x00800000)
61#define RTC_FROM4_NAND_ADDR_SLOT4 (0x00C00000)
62#define RTC_FROM4_NAND_ADDR_FPGA (0x01000000)
63
64#define RTC_FROM4_NAND_ADDR_MASK (RTC_FROM4_NAND_ADDR_SLOT3 | RTC_FROM4_NAND_ADDR_SLOT4 | RTC_FROM4_NAND_ADDR_FPGA)
65
66
67#define RTC_FROM4_FPGA_SR (RTC_FROM4_NAND_ADDR_FPGA | 0x00000002)
68#define RTC_FROM4_DEVICE_READY 0x0001
69
70
71
72#define RTC_FROM4_RS_ECC_CTL (RTC_FROM4_NAND_ADDR_FPGA | 0x00000050)
73#define RTC_FROM4_RS_ECC_CTL_CLR (1 << 7)
74#define RTC_FROM4_RS_ECC_CTL_GEN (1 << 6)
75#define RTC_FROM4_RS_ECC_CTL_FD_E (1 << 5)
76
77
78#define RTC_FROM4_RS_ECC (RTC_FROM4_NAND_ADDR_FPGA | 0x00000060)
79#define RTC_FROM4_RS_ECCN (RTC_FROM4_NAND_ADDR_FPGA | 0x00000080)
80
81
82#define RTC_FROM4_RS_ECC_CHK (RTC_FROM4_NAND_ADDR_FPGA | 0x00000070)
83#define RTC_FROM4_RS_ECC_CHK_ERROR (1 << 7)
84
85#define ERR_STAT_ECC_AVAILABLE 0x20
86
87
88#define RTC_FROM4_HWECC 1
89
90
91#define RTC_FROM4_NO_VIRTBLOCKS 0
92
93
94
95
96static void __iomem *rtc_from4_fio_base = (void *)P2SEGADDR(RTC_FROM4_FIO_BASE);
97
98static const struct mtd_partition partition_info[] = {
99 {
100 .name = "Renesas flash partition 1",
101 .offset = 0,
102 .size = MTDPART_SIZ_FULL},
103};
104
105#define NUM_PARTITIONS 1
106
107
108
109
110
111
112
113static uint8_t bbt_pattern[] = { 'B', 'b', 't', '0' };
114static uint8_t mirror_pattern[] = { '1', 't', 'b', 'B' };
115
116static struct nand_bbt_descr rtc_from4_bbt_main_descr = {
117 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
118 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
119 .offs = 40,
120 .len = 4,
121 .veroffs = 44,
122 .maxblocks = 4,
123 .pattern = bbt_pattern
124};
125
126static struct nand_bbt_descr rtc_from4_bbt_mirror_descr = {
127 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
128 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
129 .offs = 40,
130 .len = 4,
131 .veroffs = 44,
132 .maxblocks = 4,
133 .pattern = mirror_pattern
134};
135
136#ifdef RTC_FROM4_HWECC
137
138
139static struct rs_control *rs_decoder;
140
141
142
143
144static struct nand_ecclayout rtc_from4_nand_oobinfo = {
145 .eccbytes = 32,
146 .eccpos = {
147 0, 1, 2, 3, 4, 5, 6, 7,
148 8, 9, 10, 11, 12, 13, 14, 15,
149 16, 17, 18, 19, 20, 21, 22, 23,
150 24, 25, 26, 27, 28, 29, 30, 31},
151 .oobfree = {{32, 32}}
152};
153
154#endif
155
156
157
158
159
160
161
162
163
164
165
166
167
168static void rtc_from4_hwcontrol(struct mtd_info *mtd, int cmd,
169 unsigned int ctrl)
170{
171 struct nand_chip *chip = (mtd->priv);
172
173 if (cmd == NAND_CMD_NONE)
174 return;
175
176 if (ctrl & NAND_CLE)
177 writeb(cmd, chip->IO_ADDR_W | RTC_FROM4_CLE);
178 else
179 writeb(cmd, chip->IO_ADDR_W | RTC_FROM4_ALE);
180}
181
182
183
184
185
186
187
188
189
190
191static void rtc_from4_nand_select_chip(struct mtd_info *mtd, int chip)
192{
193 struct nand_chip *this = mtd->priv;
194
195 this->IO_ADDR_R = (void __iomem *)((unsigned long)this->IO_ADDR_R & ~RTC_FROM4_NAND_ADDR_MASK);
196 this->IO_ADDR_W = (void __iomem *)((unsigned long)this->IO_ADDR_W & ~RTC_FROM4_NAND_ADDR_MASK);
197
198 switch (chip) {
199
200 case 0:
201 this->IO_ADDR_R = (void __iomem *)((unsigned long)this->IO_ADDR_R | RTC_FROM4_NAND_ADDR_SLOT3);
202 this->IO_ADDR_W = (void __iomem *)((unsigned long)this->IO_ADDR_W | RTC_FROM4_NAND_ADDR_SLOT3);
203 break;
204 case 1:
205 this->IO_ADDR_R = (void __iomem *)((unsigned long)this->IO_ADDR_R | RTC_FROM4_NAND_ADDR_SLOT4);
206 this->IO_ADDR_W = (void __iomem *)((unsigned long)this->IO_ADDR_W | RTC_FROM4_NAND_ADDR_SLOT4);
207 break;
208
209 }
210}
211
212
213
214
215
216
217
218
219
220static int rtc_from4_nand_device_ready(struct mtd_info *mtd)
221{
222 unsigned short status;
223
224 status = *((volatile unsigned short *)(rtc_from4_fio_base + RTC_FROM4_FPGA_SR));
225
226 return (status & RTC_FROM4_DEVICE_READY);
227
228}
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243static void deplete(struct mtd_info *mtd, int chip)
244{
245 struct nand_chip *this = mtd->priv;
246
247
248 while (!this->dev_ready(mtd)) ;
249
250 this->select_chip(mtd, chip);
251
252
253 this->cmdfunc(mtd, NAND_CMD_DEPLETE1, 0x0000, 0x0000);
254 this->cmdfunc(mtd, NAND_CMD_DEPLETE2, -1, -1);
255
256
257 this->cmdfunc(mtd, NAND_CMD_DEPLETE1, 0x0000, 0x0004);
258 this->cmdfunc(mtd, NAND_CMD_DEPLETE2, -1, -1);
259
260}
261
262#ifdef RTC_FROM4_HWECC
263
264
265
266
267
268
269
270
271static void rtc_from4_enable_hwecc(struct mtd_info *mtd, int mode)
272{
273 volatile unsigned short *rs_ecc_ctl = (volatile unsigned short *)(rtc_from4_fio_base + RTC_FROM4_RS_ECC_CTL);
274 unsigned short status;
275
276 switch (mode) {
277 case NAND_ECC_READ:
278 status = RTC_FROM4_RS_ECC_CTL_CLR | RTC_FROM4_RS_ECC_CTL_FD_E;
279
280 *rs_ecc_ctl = status;
281 break;
282
283 case NAND_ECC_READSYN:
284 status = 0x00;
285
286 *rs_ecc_ctl = status;
287 break;
288
289 case NAND_ECC_WRITE:
290 status = RTC_FROM4_RS_ECC_CTL_CLR | RTC_FROM4_RS_ECC_CTL_GEN | RTC_FROM4_RS_ECC_CTL_FD_E;
291
292 *rs_ecc_ctl = status;
293 break;
294
295 default:
296 BUG();
297 break;
298 }
299
300}
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315static void rtc_from4_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
316{
317 volatile unsigned short *rs_eccn = (volatile unsigned short *)(rtc_from4_fio_base + RTC_FROM4_RS_ECCN);
318 unsigned short value;
319 int i;
320
321 for (i = 0; i < 8; i++) {
322 value = *rs_eccn;
323 ecc_code[i] = (unsigned char)value;
324 rs_eccn++;
325 }
326 ecc_code[7] |= 0x0f;
327}
328
329
330
331
332
333
334
335
336
337
338
339
340
341static int rtc_from4_correct_data(struct mtd_info *mtd, const u_char *buf, u_char *ecc1, u_char *ecc2)
342{
343 int i, j, res;
344 unsigned short status;
345 uint16_t par[6], syn[6];
346 uint8_t ecc[8];
347 volatile unsigned short *rs_ecc;
348
349 status = *((volatile unsigned short *)(rtc_from4_fio_base + RTC_FROM4_RS_ECC_CHK));
350
351 if (!(status & RTC_FROM4_RS_ECC_CHK_ERROR)) {
352 return 0;
353 }
354
355
356 rs_ecc = (volatile unsigned short *)(rtc_from4_fio_base + RTC_FROM4_RS_ECC);
357 for (i = 0; i < 8; i++) {
358 ecc[i] = bitrev8(*rs_ecc);
359 rs_ecc++;
360 }
361
362
363 par[5] = rs_decoder->index_of[(((uint16_t) ecc[0] >> 0) & 0x0ff) | (((uint16_t) ecc[1] << 8) & 0x300)];
364 par[4] = rs_decoder->index_of[(((uint16_t) ecc[1] >> 2) & 0x03f) | (((uint16_t) ecc[2] << 6) & 0x3c0)];
365 par[3] = rs_decoder->index_of[(((uint16_t) ecc[2] >> 4) & 0x00f) | (((uint16_t) ecc[3] << 4) & 0x3f0)];
366 par[2] = rs_decoder->index_of[(((uint16_t) ecc[3] >> 6) & 0x003) | (((uint16_t) ecc[4] << 2) & 0x3fc)];
367 par[1] = rs_decoder->index_of[(((uint16_t) ecc[5] >> 0) & 0x0ff) | (((uint16_t) ecc[6] << 8) & 0x300)];
368 par[0] = (((uint16_t) ecc[6] >> 2) & 0x03f) | (((uint16_t) ecc[7] << 6) & 0x3c0);
369
370
371 for (i = 0; i < 6; i++) {
372 syn[i] = par[0];
373 for (j = 1; j < 6; j++)
374 if (par[j] != rs_decoder->nn)
375 syn[i] ^= rs_decoder->alpha_to[rs_modnn(rs_decoder, par[j] + i * j)];
376
377
378 syn[i] = rs_decoder->index_of[syn[i]];
379 }
380
381
382 res = decode_rs8(rs_decoder, (uint8_t *) buf, par, 512, syn, 0, NULL, 0xff, NULL);
383 if (res > 0) {
384 DEBUG(MTD_DEBUG_LEVEL0, "rtc_from4_correct_data: " "ECC corrected %d errors on read\n", res);
385 }
386 return res;
387}
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404static int rtc_from4_errstat(struct mtd_info *mtd, struct nand_chip *this,
405 int state, int status, int page)
406{
407 int er_stat = 0;
408 int rtn, retlen;
409 size_t len;
410 uint8_t *buf;
411 int i;
412
413 this->cmdfunc(mtd, NAND_CMD_STATUS_CLEAR, -1, -1);
414
415 if (state == FL_ERASING) {
416
417 for (i = 0; i < 4; i++) {
418 if (!(status & 1 << (i + 1)))
419 continue;
420 this->cmdfunc(mtd, (NAND_CMD_STATUS_ERROR + i + 1),
421 -1, -1);
422 rtn = this->read_byte(mtd);
423 this->cmdfunc(mtd, NAND_CMD_STATUS_RESET, -1, -1);
424
425
426 if (!(rtn & ERR_STAT_ECC_AVAILABLE))
427 er_stat |= 1 << (i + 1);
428 }
429
430 } else if (state == FL_WRITING) {
431
432 unsigned long corrected = mtd->ecc_stats.corrected;
433
434
435 this->cmdfunc(mtd, NAND_CMD_STATUS_ERROR, -1, -1);
436 rtn = this->read_byte(mtd);
437 this->cmdfunc(mtd, NAND_CMD_STATUS_RESET, -1, -1);
438
439 if (!(rtn & ERR_STAT_ECC_AVAILABLE)) {
440
441 er_stat |= 1 << 1;
442 goto out;
443 }
444
445 len = mtd->writesize;
446 buf = kmalloc(len, GFP_KERNEL);
447 if (!buf) {
448 printk(KERN_ERR "rtc_from4_errstat: Out of memory!\n");
449 er_stat = 1;
450 goto out;
451 }
452
453
454 rtn = nand_do_read(mtd, page, len, &retlen, buf);
455
456
457 if (rtn || (mtd->ecc_stats.corrected - corrected) > 1)
458 er_stat |= 1 << 1;
459 kfree(buf);
460 }
461out:
462 rtn = status;
463 if (er_stat == 0) {
464 rtn = (status & ~NAND_STATUS_FAIL);
465 }
466
467 return rtn;
468}
469#endif
470
471
472
473
474static int __init rtc_from4_init(void)
475{
476 struct nand_chip *this;
477 unsigned short bcr1, bcr2, wcr2;
478 int i;
479 int ret;
480
481
482 rtc_from4_mtd = kmalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip), GFP_KERNEL);
483 if (!rtc_from4_mtd) {
484 printk("Unable to allocate Renesas NAND MTD device structure.\n");
485 return -ENOMEM;
486 }
487
488
489 this = (struct nand_chip *)(&rtc_from4_mtd[1]);
490
491
492 memset(rtc_from4_mtd, 0, sizeof(struct mtd_info));
493 memset(this, 0, sizeof(struct nand_chip));
494
495
496 rtc_from4_mtd->priv = this;
497 rtc_from4_mtd->owner = THIS_MODULE;
498
499
500 bcr1 = *SH77X9_BCR1 & ~0x0002;
501 bcr1 |= 0x0002;
502 *SH77X9_BCR1 = bcr1;
503
504
505 bcr2 = *SH77X9_BCR2 & ~0x0c00;
506 bcr2 |= 0x0800;
507 *SH77X9_BCR2 = bcr2;
508
509
510 wcr2 = *SH77X9_WCR2 & ~0x1c00;
511 wcr2 |= 0x1c00;
512 *SH77X9_WCR2 = wcr2;
513
514
515 this->IO_ADDR_R = rtc_from4_fio_base;
516 this->IO_ADDR_W = rtc_from4_fio_base;
517
518 this->cmd_ctrl = rtc_from4_hwcontrol;
519
520 this->select_chip = rtc_from4_nand_select_chip;
521
522 this->chip_delay = 100;
523
524 this->dev_ready = rtc_from4_nand_device_ready;
525
526#ifdef RTC_FROM4_HWECC
527 printk(KERN_INFO "rtc_from4_init: using hardware ECC detection.\n");
528
529 this->ecc.mode = NAND_ECC_HW_SYNDROME;
530 this->ecc.size = 512;
531 this->ecc.bytes = 8;
532
533 this->errstat = rtc_from4_errstat;
534
535 this->ecc.layout = &rtc_from4_nand_oobinfo;
536 this->ecc.hwctl = rtc_from4_enable_hwecc;
537 this->ecc.calculate = rtc_from4_calculate_ecc;
538 this->ecc.correct = rtc_from4_correct_data;
539
540
541
542
543
544
545
546
547
548
549 rs_decoder = init_rs(10, 0x409, 0, 1, 6);
550 if (!rs_decoder) {
551 printk(KERN_ERR "Could not create a RS decoder\n");
552 ret = -ENOMEM;
553 goto err_1;
554 }
555#else
556 printk(KERN_INFO "rtc_from4_init: using software ECC detection.\n");
557
558 this->ecc.mode = NAND_ECC_SOFT;
559#endif
560
561
562 this->bbt_td = &rtc_from4_bbt_main_descr;
563 this->bbt_md = &rtc_from4_bbt_mirror_descr;
564
565
566 if (nand_scan(rtc_from4_mtd, RTC_FROM4_MAX_CHIPS)) {
567 ret = -ENXIO;
568 goto err_2;
569 }
570
571
572 for (i = 0; i < this->numchips; i++) {
573 deplete(rtc_from4_mtd, i);
574 }
575
576#if RTC_FROM4_NO_VIRTBLOCKS
577
578
579
580 rtc_from4_mtd->flags |= MTD_NO_VIRTBLOCKS;
581#endif
582
583
584 ret = add_mtd_partitions(rtc_from4_mtd, partition_info, NUM_PARTITIONS);
585 if (ret)
586 goto err_3;
587
588
589 return 0;
590err_3:
591 nand_release(rtc_from4_mtd);
592err_2:
593 free_rs(rs_decoder);
594err_1:
595 kfree(rtc_from4_mtd);
596 return ret;
597}
598
599module_init(rtc_from4_init);
600
601
602
603
604static void __exit rtc_from4_cleanup(void)
605{
606
607 nand_release(rtc_from4_mtd);
608
609
610 kfree(rtc_from4_mtd);
611
612#ifdef RTC_FROM4_HWECC
613
614 if (rs_decoder) {
615 free_rs(rs_decoder);
616 }
617#endif
618}
619
620module_exit(rtc_from4_cleanup);
621
622MODULE_LICENSE("GPL");
623MODULE_AUTHOR("d.marlin <dmarlin@redhat.com");
624MODULE_DESCRIPTION("Board-specific glue layer for AG-AND flash on Renesas FROM_BOARD4");
625