2 * SPL driver for Diskonchip G4 nand flash
4 * Copyright (C) 2013 Mike Dunn <mikedunn@newsguy.com>
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.
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.
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
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.
31 #include <linux/mtd/docg4.h>
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);
41 int nand_spl_load_image(uint32_t offs, unsigned int size, void *dst)
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;
50 for (i = 0; i < block_count; i++) {
51 int ret = docg4_load_block_reliable(flash_offset, load_addr);
54 load_addr += DOCG4_BLOCK_CAPACITY_SPL;
55 flash_offset += DOCG4_BLOCK_SIZE;
60 static inline void write_nop(void __iomem *docptr)
62 writew(0, docptr + DOC_NOP);
65 static int poll_status(void __iomem *docptr)
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.
75 /* hardware quirk requires reading twice initially */
76 flash_status = readb(docptr + DOC_FLASHCONTROL);
79 flash_status = readb(docptr + DOC_FLASHCONTROL);
80 } while (!(flash_status & DOC_CTRL_FLASHREADY));
85 static void write_addr(void __iomem *docptr, uint32_t docg4_addr)
87 /* write the four address bytes packed in docg4_addr to the device */
89 writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
91 writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
93 writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
95 writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
98 static void address_sequence(unsigned int g4_page, unsigned int g4_index,
101 writew(DOCG4_SEQ_PAGE_READ, docptr + DOC_FLASHSEQUENCE);
102 writew(DOCG4_CMD_PAGE_READ, docptr + DOC_FLASHCOMMAND);
104 write_addr(docptr, ((uint32_t)g4_page << 16) | g4_index);
108 static int docg4_load_block_reliable(uint32_t flash_offset, void *dest_addr)
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 */
114 uint16_t flash_status;
117 /* flash_offset must be aligned to the start of a block */
118 if (flash_offset & 0x3ffff)
121 writew(DOC_SEQ_RESET, docptr + DOC_FLASHSEQUENCE);
122 writew(DOC_CMD_RESET, docptr + DOC_FLASHCOMMAND);
127 writew(0x45, docptr + DOC_FLASHSEQUENCE);
128 writew(0xa3, docptr + DOC_FLASHCOMMAND);
130 writew(0x22, docptr + DOC_FLASHCOMMAND);
133 /* read 1st 4 oob bytes of first subpage of block */
134 address_sequence(g4_page, 0x0100, docptr); /* index at oob */
136 flash_status = readw(docptr + DOC_FLASHCONTROL);
137 flash_status = readw(docptr + DOC_FLASHCONTROL);
138 if (flash_status & 0x06) /* sequence or protection errors */
140 writew(DOCG4_CMD_READ2, docptr + DOC_FLASHCOMMAND);
144 writew(DOC_ECCCONF0_READ_MODE | 4, docptr + DOC_ECCCONF0);
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.
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);
163 /* load contents of block to memory */
164 buf = (uint16_t *)dest_addr;
168 address_sequence(g4_page, g4_index, docptr);
169 writew(DOCG4_CMD_READ2,
170 docptr + DOC_FLASHCOMMAND);
174 writew(DOC_ECCCONF0_READ_MODE |
175 DOC_ECCCONF0_ECC_ENABLE |
177 docptr + DOC_ECCCONF0);
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);
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);
195 writew(0, docptr + DOC_DATAEND);
199 if (!(g4_index & 0x100)) {
200 /* not redundant subpage read; check for ecc error */
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 */
208 } else /* no ecc error */
209 g4_index += 0x210; /* skip redundant subpage */
210 } else /* redundant page was just read; skip ecc error check */
213 if (g4_index == 0x420) { /* finished with 2k page */
215 g4_page += 2; /* odd-numbered 2k pages skipped */
218 } while (g4_page != last_g4_page); /* while still on same block */