@@ -160,30 +160,28 @@ static int sf_program(u32 addr, const u8 *buf, unsigned int words)
160160#define GK_BOOT_MAGIC 0x474b3731
161161
162162/*
163- * The boot ROM reads the boot image as raw chip bytes, while everything
164- * read or written through the controller is packed MSB first: a 32-bit
165- * word goes to the chip byte-swapped and comes back swapped again. For the
166- * kernel, the rootfs and the environment that round trip is invisible.
167- * For the boot image it is not: a programmer writes it in the chip's
168- * order, and the vendor U-Boot's flash write recognises such an image by
169- * its byte-swapped magic and swaps it back (check_magic_number() in its
170- * gk7101_sflash.c).
163+ * The controller packs bytes MSB first: every 32-bit word goes to the chip
164+ * byte-swapped and comes back swapped again, so through this driver the
165+ * flash reads and writes in CPU order, the order of every published file
166+ * (U-Boot, kernel, rootfs) and of what tftp, mmc or usb load. The chip
167+ * itself, which the boot ROM and a programmer see, holds every word
168+ * swapped; a programmer image is the files laid out and every word
169+ * swapped, and a dump swapped back is in CPU order.
171170 *
172- * This driver does the same, both ways, for every caller: from offset 0
173- * to sf_swap_end -- header table plus the U-Boot length the header gives
174- * -- data is swapped on write and on read, so the boot image reads back
175- * as the file a programmer writes, byte for byte. The window is the image
176- * on the flash at probe, and a write at 0 moves it: one that starts with
177- * the swapped magic opens it as long as that image, however the caller
178- * splits the rest; any other write at 0 closes it.
179- *
180- * A write at 0 that starts with the swapped magic and runs past the boot
181- * image -- a programmer's whole-chip dump in one sf write, or recovery
182- * image -- is chip order throughout, and is swapped over its whole
183- * length. Split into sectors (sf update), only its first sector is seen
184- * at 0, so only the boot image is swapped back.
171+ * A programmer image may still be written through here (sf write, SD
172+ * recovery). A write at 0 that starts with the swapped boot magic is
173+ * taken for one and swapped back: over sf_swap_end -- header table plus
174+ * the U-Boot length the header gives -- however the caller splits the
175+ * rest, and over its whole length when that one write is longer (a
176+ * whole-chip image). Any other write at 0 closes the window, and so does
177+ * a write elsewhere that does not start where the last one ended: the
178+ * window carries over only the pieces of one image written in a row, so a
179+ * later sf update whose unchanged first sector is skipped does not inherit
180+ * it. The vendor U-Boot's flash write does the same swap
181+ * (check_magic_number() in its gk7101_sflash.c). Reads are never swapped.
185182 */
186183static u32 sf_swap_end ;
184+ static loff_t sf_swap_next ; /* where the last write ended */
187185
188186/* The length of a chip-order boot image starting at buf, or 0 */
189187static u32 sf_boot_len (const u_char * buf , size_t len )
@@ -213,8 +211,11 @@ static u32 sf_boot_len(const u_char *buf, size_t len)
213211
214212static void sf_swap_detect (loff_t to , size_t len , const u_char * buf )
215213{
216- if (to )
214+ if (to ) {
215+ if (to != sf_swap_next )
216+ sf_swap_end = 0 ;
217217 return ;
218+ }
218219 sf_swap_end = sf_boot_len (buf , len );
219220 if (sf_swap_end )
220221 printf ("SF: boot image in the chip's byte order, swapping it back\n" );
@@ -239,10 +240,6 @@ static int gk_sf_read(struct mtd_info *mtd, loff_t from, size_t len,
239240 u32 w = readl (SF_DATA );
240241 unsigned int n = min_t (size_t , len , 4 - skip );
241242
242- if (addr < sf_swap_end )
243- w = swab32 (w );
244- addr += 4 ;
245-
246243 if (!skip && n == 4 ) {
247244 put_unaligned_le32 (w , buf );
248245 } else {
@@ -275,6 +272,7 @@ static int gk_sf_write(struct mtd_info *mtd, loff_t to, size_t len,
275272 if (to + len > mtd -> size )
276273 return - EINVAL ;
277274 sf_swap_detect (to , len , buf );
275+ sf_swap_next = to + len ;
278276 end = !to && sf_swap_end ? max_t (u32 , sf_swap_end , len ) : sf_swap_end ;
279277 * retlen = 0 ;
280278 while (len ) {
@@ -320,21 +318,6 @@ static int gk_sf_erase(struct mtd_info *mtd, struct erase_info *instr)
320318static struct spi_flash gk_flash ;
321319static int gk_announced ;
322320
323- /* The swap window over the boot image already on the flash */
324- static void sf_swap_probe (u32 size )
325- {
326- u8 hdr [SZ_2K ];
327- size_t n ;
328- unsigned int i ;
329-
330- sf_swap_end = 0 ;
331- gk_sf_read (& gk_flash .mtd , 0 , sizeof (hdr ), & n , hdr );
332- for (i = 0 ; i < sizeof (hdr ); i += 4 )
333- put_unaligned_le32 (swab32 (get_unaligned_le32 (hdr + i )),
334- hdr + i );
335- sf_swap_end = min (sf_boot_len (hdr , sizeof (hdr )), size );
336- }
337-
338321struct spi_flash * spi_flash_probe (unsigned int bus , unsigned int cs ,
339322 unsigned int max_hz , unsigned int spi_mode )
340323{
@@ -376,9 +359,6 @@ struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
376359 mtd -> _write = gk_sf_write ;
377360 mtd -> _erase = gk_sf_erase ;
378361 mtd -> priv = flash ;
379- /* Once: later writes at 0 keep the window up to date */
380- if (!gk_announced )
381- sf_swap_probe (size );
382362
383363 /* Once: the environment and common/openipc.c probe again and again */
384364 if (!gk_announced ++ )
0 commit comments