blob: 95e856c213f0a7e163dea0aaa7548acd3e57f804 [file] [log] [blame]
Mike Dunn54baebe2013-04-12 11:59:18 -07001/*
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 */
34static inline void write_nop(void __iomem *docptr);
35static int poll_status(void __iomem *docptr);
36static void write_addr(void __iomem *docptr, uint32_t docg4_addr);
37static void address_sequence(unsigned int g4_page, unsigned int g4_index,
38 void __iomem *docptr);
39static int docg4_load_block_reliable(uint32_t flash_offset, void *dest_addr);
40
41int 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
60static inline void write_nop(void __iomem *docptr)
61{
62 writew(0, docptr + DOC_NOP);
63}
64
65static 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
85static 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
98static 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
108static 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 uint16_t discard, magic_high, magic_low;
117
118 /* flash_offset must be aligned to the start of a block */
119 if (flash_offset & 0x3ffff)
120 return -1;
121
122 writew(DOC_SEQ_RESET, docptr + DOC_FLASHSEQUENCE);
123 writew(DOC_CMD_RESET, docptr + DOC_FLASHCOMMAND);
124 write_nop(docptr);
125 write_nop(docptr);
126 poll_status(docptr);
127 write_nop(docptr);
128 writew(0x45, docptr + DOC_FLASHSEQUENCE);
129 writew(0xa3, docptr + DOC_FLASHCOMMAND);
130 write_nop(docptr);
131 writew(0x22, docptr + DOC_FLASHCOMMAND);
132 write_nop(docptr);
133
134 /* read 1st 4 oob bytes of first subpage of block */
135 address_sequence(g4_page, 0x0100, docptr); /* index at oob */
136 write_nop(docptr);
137 flash_status = readw(docptr + DOC_FLASHCONTROL);
138 flash_status = readw(docptr + DOC_FLASHCONTROL);
139 if (flash_status & 0x06) /* sequence or protection errors */
140 return -1;
141 writew(DOCG4_CMD_READ2, docptr + DOC_FLASHCOMMAND);
142 write_nop(docptr);
143 write_nop(docptr);
144 poll_status(docptr);
145 writew(DOC_ECCCONF0_READ_MODE | 4, docptr + DOC_ECCCONF0);
146 write_nop(docptr);
147 write_nop(docptr);
148 write_nop(docptr);
149 write_nop(docptr);
150 write_nop(docptr);
151
152 /*
153 * Here we read the first four oob bytes of the first page of the block.
154 * The IPL on the palmtreo680 requires that this contain a 32 bit magic
155 * number, or the load aborts. We'll ignore it.
156 */
157 discard = readw(docptr + 0x103c); /* hw quirk; 1st read discarded */
158 magic_low = readw(docptr + 0x103c);
159 magic_high = readw(docptr + DOCG4_MYSTERY_REG);
160 writew(0, docptr + DOC_DATAEND);
161 write_nop(docptr);
162 write_nop(docptr);
163
164 /* load contents of block to memory */
165 buf = (uint16_t *)dest_addr;
166 do {
167 int i;
168
169 address_sequence(g4_page, g4_index, docptr);
170 writew(DOCG4_CMD_READ2,
171 docptr + DOC_FLASHCOMMAND);
172 write_nop(docptr);
173 write_nop(docptr);
174 poll_status(docptr);
175 writew(DOC_ECCCONF0_READ_MODE |
176 DOC_ECCCONF0_ECC_ENABLE |
177 DOCG4_BCH_SIZE,
178 docptr + DOC_ECCCONF0);
179 write_nop(docptr);
180 write_nop(docptr);
181 write_nop(docptr);
182 write_nop(docptr);
183 write_nop(docptr);
184
185 /* read the 512 bytes of page data, 2 bytes at a time */
186 discard = readw(docptr + 0x103c);
187 for (i = 0; i < 256; i++)
188 *buf++ = readw(docptr + 0x103c);
189
190 /* read oob, but discard it */
191 for (i = 0; i < 7; i++)
192 discard = readw(docptr + 0x103c);
193 discard = readw(docptr + DOCG4_OOB_6_7);
194 discard = readw(docptr + DOCG4_OOB_6_7);
195
196 writew(0, docptr + DOC_DATAEND);
197 write_nop(docptr);
198 write_nop(docptr);
199
200 if (!(g4_index & 0x100)) {
201 /* not redundant subpage read; check for ecc error */
202 write_nop(docptr);
203 flash_status = readw(docptr + DOC_ECCCONF1);
204 flash_status = readw(docptr + DOC_ECCCONF1);
205 if (flash_status & 0x80) { /* ecc error */
206 g4_index += 0x108; /* read redundant subpage */
207 buf -= 256; /* back up ram ptr */
208 continue;
209 } else /* no ecc error */
210 g4_index += 0x210; /* skip redundant subpage */
211 } else /* redundant page was just read; skip ecc error check */
212 g4_index += 0x108;
213
214 if (g4_index == 0x420) { /* finished with 2k page */
215 g4_index = 0;
216 g4_page += 2; /* odd-numbered 2k pages skipped */
217 }
218
219 } while (g4_page != last_g4_page); /* while still on same block */
220
221 return 0;
222}