]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/mtd/nand/docg4_spl.c
mtd/nand: docg4: fix compiler warnings
[karo-tx-uboot.git] / drivers / mtd / nand / docg4_spl.c
1 /*
2  * SPL driver for Diskonchip G4 nand flash
3  *
4  * Copyright (C) 2013 Mike Dunn <mikedunn@newsguy.com>
5  *
6  * This file is released under the terms of GPL v2 and any later version.
7  * See the file COPYING in the root directory of the source tree for details.
8  *
9  *
10  * This driver basically mimics the load functionality of a typical IPL (initial
11  * program loader) resident in the 2k NOR-like region of the docg4 that is
12  * mapped to the reset vector.  It allows the u-boot SPL to continue loading if
13  * the IPL loads a fixed number of flash blocks that is insufficient to contain
14  * the entire u-boot image.  In this case, a concatenated spl + u-boot image is
15  * written at the flash offset from which the IPL loads an image, and when the
16  * IPL jumps to the SPL, the SPL resumes loading where the IPL left off.  See
17  * the palmtreo680 for an example.
18  *
19  * This driver assumes that the data was written to the flash using the device's
20  * "reliable" mode, and also assumes that each 512 byte page is stored
21  * redundantly in the subsequent page.  This storage format is likely to be used
22  * by all boards that boot from the docg4.  The format compensates for the lack
23  * of ecc in the IPL.
24  *
25  * Reliable mode reduces the capacity of a block by half, and the redundant
26  * pages reduce it by half again.  As a result, the normal 256k capacity of a
27  * block is reduced to 64k for the purposes of the IPL/SPL.
28  */
29
30 #include <asm/io.h>
31 #include <linux/mtd/docg4.h>
32
33 /* forward declarations */
34 static inline void write_nop(void __iomem *docptr);
35 static int poll_status(void __iomem *docptr);
36 static void write_addr(void __iomem *docptr, uint32_t docg4_addr);
37 static void address_sequence(unsigned int g4_page, unsigned int g4_index,
38                              void __iomem *docptr);
39 static int docg4_load_block_reliable(uint32_t flash_offset, void *dest_addr);
40
41 int nand_spl_load_image(uint32_t offs, unsigned int size, void *dst)
42 {
43         void *load_addr = dst;
44         uint32_t flash_offset = offs;
45         const unsigned int block_count =
46                 (size + DOCG4_BLOCK_CAPACITY_SPL - 1)
47                 / DOCG4_BLOCK_CAPACITY_SPL;
48         int i;
49
50         for (i = 0; i < block_count; i++) {
51                 int ret = docg4_load_block_reliable(flash_offset, load_addr);
52                 if (ret)
53                         return ret;
54                 load_addr += DOCG4_BLOCK_CAPACITY_SPL;
55                 flash_offset += DOCG4_BLOCK_SIZE;
56         }
57         return 0;
58 }
59
60 static inline void write_nop(void __iomem *docptr)
61 {
62         writew(0, docptr + DOC_NOP);
63 }
64
65 static int poll_status(void __iomem *docptr)
66 {
67         /*
68          * Busy-wait for the FLASHREADY bit to be set in the FLASHCONTROL
69          * register.  Operations known to take a long time (e.g., block erase)
70          * should sleep for a while before calling this.
71          */
72
73         uint8_t flash_status;
74
75         /* hardware quirk requires reading twice initially */
76         flash_status = readb(docptr + DOC_FLASHCONTROL);
77
78         do {
79                 flash_status = readb(docptr + DOC_FLASHCONTROL);
80         } while (!(flash_status & DOC_CTRL_FLASHREADY));
81
82         return 0;
83 }
84
85 static void write_addr(void __iomem *docptr, uint32_t docg4_addr)
86 {
87         /* write the four address bytes packed in docg4_addr to the device */
88
89         writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
90         docg4_addr >>= 8;
91         writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
92         docg4_addr >>= 8;
93         writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
94         docg4_addr >>= 8;
95         writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
96 }
97
98 static void address_sequence(unsigned int g4_page, unsigned int g4_index,
99                              void __iomem *docptr)
100 {
101         writew(DOCG4_SEQ_PAGE_READ, docptr + DOC_FLASHSEQUENCE);
102         writew(DOCG4_CMD_PAGE_READ, docptr + DOC_FLASHCOMMAND);
103         write_nop(docptr);
104         write_addr(docptr, ((uint32_t)g4_page << 16) | g4_index);
105         write_nop(docptr);
106 }
107
108 static int docg4_load_block_reliable(uint32_t flash_offset, void *dest_addr)
109 {
110         void __iomem *docptr = (void *)CONFIG_SYS_NAND_BASE;
111         unsigned int g4_page = flash_offset >> 11; /* 2k page */
112         const unsigned int last_g4_page = g4_page + 0x80; /* last in block */
113         int g4_index = 0;
114         uint16_t flash_status;
115         uint16_t *buf;
116
117         /* flash_offset must be aligned to the start of a block */
118         if (flash_offset & 0x3ffff)
119                 return -1;
120
121         writew(DOC_SEQ_RESET, docptr + DOC_FLASHSEQUENCE);
122         writew(DOC_CMD_RESET, docptr + DOC_FLASHCOMMAND);
123         write_nop(docptr);
124         write_nop(docptr);
125         poll_status(docptr);
126         write_nop(docptr);
127         writew(0x45, docptr + DOC_FLASHSEQUENCE);
128         writew(0xa3, docptr + DOC_FLASHCOMMAND);
129         write_nop(docptr);
130         writew(0x22, docptr + DOC_FLASHCOMMAND);
131         write_nop(docptr);
132
133         /* read 1st 4 oob bytes of first subpage of block */
134         address_sequence(g4_page, 0x0100, docptr); /* index at oob */
135         write_nop(docptr);
136         flash_status = readw(docptr + DOC_FLASHCONTROL);
137         flash_status = readw(docptr + DOC_FLASHCONTROL);
138         if (flash_status & 0x06) /* sequence or protection errors */
139                 return -1;
140         writew(DOCG4_CMD_READ2, docptr + DOC_FLASHCOMMAND);
141         write_nop(docptr);
142         write_nop(docptr);
143         poll_status(docptr);
144         writew(DOC_ECCCONF0_READ_MODE | 4, docptr + DOC_ECCCONF0);
145         write_nop(docptr);
146         write_nop(docptr);
147         write_nop(docptr);
148         write_nop(docptr);
149         write_nop(docptr);
150
151         /*
152          * Here we read the first four oob bytes of the first page of the block.
153          * The IPL on the palmtreo680 requires that this contain a 32 bit magic
154          * number, or the load aborts.  We'll ignore it.
155          */
156         readw(docptr + 0x103c); /* hw quirk; 1st read discarded */
157         readw(docptr + 0x103c); /* lower 16 bits of magic number */
158         readw(docptr + DOCG4_MYSTERY_REG); /* upper 16 bits of magic number */
159         writew(0, docptr + DOC_DATAEND);
160         write_nop(docptr);
161         write_nop(docptr);
162
163         /* load contents of block to memory */
164         buf = (uint16_t *)dest_addr;
165         do {
166                 int i;
167
168                 address_sequence(g4_page, g4_index, docptr);
169                 writew(DOCG4_CMD_READ2,
170                        docptr + DOC_FLASHCOMMAND);
171                 write_nop(docptr);
172                 write_nop(docptr);
173                 poll_status(docptr);
174                 writew(DOC_ECCCONF0_READ_MODE |
175                        DOC_ECCCONF0_ECC_ENABLE |
176                        DOCG4_BCH_SIZE,
177                        docptr + DOC_ECCCONF0);
178                 write_nop(docptr);
179                 write_nop(docptr);
180                 write_nop(docptr);
181                 write_nop(docptr);
182                 write_nop(docptr);
183
184                 /* read the 512 bytes of page data, 2 bytes at a time */
185                 readw(docptr + 0x103c); /* hw quirk */
186                 for (i = 0; i < 256; i++)
187                         *buf++ = readw(docptr + 0x103c);
188
189                 /* read oob, but discard it */
190                 for (i = 0; i < 7; i++)
191                         readw(docptr + 0x103c);
192                 readw(docptr + DOCG4_OOB_6_7);
193                 readw(docptr + DOCG4_OOB_6_7);
194
195                 writew(0, docptr + DOC_DATAEND);
196                 write_nop(docptr);
197                 write_nop(docptr);
198
199                 if (!(g4_index & 0x100)) {
200                         /* not redundant subpage read; check for ecc error */
201                         write_nop(docptr);
202                         flash_status = readw(docptr + DOC_ECCCONF1);
203                         flash_status = readw(docptr + DOC_ECCCONF1);
204                         if (flash_status & 0x80) { /* ecc error */
205                                 g4_index += 0x108; /* read redundant subpage */
206                                 buf -= 256;        /* back up ram ptr */
207                                 continue;
208                         } else                       /* no ecc error */
209                                 g4_index += 0x210; /* skip redundant subpage */
210                 } else  /* redundant page was just read; skip ecc error check */
211                         g4_index += 0x108;
212
213                 if (g4_index == 0x420) { /* finished with 2k page */
214                         g4_index = 0;
215                         g4_page += 2; /* odd-numbered 2k pages skipped */
216                 }
217
218         } while (g4_page != last_g4_page); /* while still on same block */
219
220         return 0;
221 }