[openfirmware] r1300 - cpu/x86/pc/olpc cpu/x86/pc/olpc/via dev/olpc/kb3700 dev/olpc/spiflash dev/via dev/via/spi

svn at openfirmware.info svn at openfirmware.info
Wed Aug 19 05:40:15 CEST 2009


Author: wmb
Date: 2009-08-19 05:40:15 +0200 (Wed, 19 Aug 2009)
New Revision: 1300

Added:
   cpu/x86/pc/olpc/via/recover.fth
   dev/olpc/spiflash/flashif.fth
   dev/olpc/spiflash/memflash.fth
   dev/olpc/spiflash/spiif.fth
   dev/via/spi/
   dev/via/spi/bbspi.fth
   dev/via/spi/spi.fth
Modified:
   cpu/x86/pc/olpc/devices.fth
   cpu/x86/pc/olpc/via/addrs.fth
   cpu/x86/pc/olpc/via/devices.fth
   dev/olpc/kb3700/ecio.fth
   dev/olpc/kb3700/ecserial.fth
   dev/olpc/kb3700/ecspi.fth
   dev/olpc/spiflash/spiflash.fth
   dev/olpc/spiflash/spiui.fth
Log:
Via - Tethered FLASH recovery feature.



Modified: cpu/x86/pc/olpc/devices.fth
===================================================================
--- cpu/x86/pc/olpc/devices.fth	2009-08-18 16:31:24 UTC (rev 1299)
+++ cpu/x86/pc/olpc/devices.fth	2009-08-19 03:40:15 UTC (rev 1300)
@@ -248,14 +248,18 @@
 
 fload ${BP}/dev/geode/acpi.fth           \ Power management
 
-fload ${BP}/dev/olpc/kb3700/ecspi.fth      \ EC chip SPI FLASH access
+fload ${BP}/dev/olpc/spiflash/flashif.fth  \ Generic FLASH interface
+fload ${BP}/dev/olpc/spiflash/memflash.fth \ Memory-mapped FLASH access
 
 warning @ warning off
 : stand-init-io  stand-init-io  h# fff0.0000 to flash-base  ;
 warning !
 
+fload ${BP}/dev/olpc/spiflash/spiif.fth    \ Generic low-level SPI bus access
+fload ${BP}/dev/olpc/spiflash/spiflash.fth \ SPI FLASH programming
+
+fload ${BP}/dev/olpc/kb3700/ecspi.fth      \ EC chip SPI FLASH access
 fload ${BP}/dev/olpc/kb3700/ecserial.fth   \ Serial access to EC chip
-
 fload ${BP}/dev/olpc/kb3700/ecio.fth       \ I/O space access to EC chip
 
 fload ${BP}/cpu/x86/pc/olpc/boardrev.fth   \ Board revision decoding
@@ -297,7 +301,6 @@
 
 fload ${BP}/dev/olpc/kb3700/battery.fth      \ Battery status reports
 
-fload ${BP}/dev/olpc/spiflash/spiflash.fth   \ SPI FLASH programming
 fload ${BP}/dev/olpc/spiflash/spiui.fth      \ User interface for SPI FLASH programming
 fload ${BP}/dev/olpc/spiflash/recover.fth    \ XO-to-XO SPI FLASH recovery
 : ofw-fw-filename$  " disk:\boot\olpc.rom"  ;

Modified: cpu/x86/pc/olpc/via/addrs.fth
===================================================================
--- cpu/x86/pc/olpc/via/addrs.fth	2009-08-18 16:31:24 UTC (rev 1299)
+++ cpu/x86/pc/olpc/via/addrs.fth	2009-08-19 03:40:15 UTC (rev 1300)
@@ -56,7 +56,8 @@
 \ h# fe02.8000 constant camera-pci-base
 h# fed0.0000 constant hpet-mmio-base
 h# fed3.0000 constant spi-mmio-base
-\ h# fed4.0000 constant wdt-mmio-base
+h# fed4.0000 constant spi0-mmio-base
+h# fed5.0000 constant wdt-mmio-base
 h# fec0.0000 constant io-apic-mmio-base
 h# fee0.0000 constant apic-mmio-base
 

Modified: cpu/x86/pc/olpc/via/devices.fth
===================================================================
--- cpu/x86/pc/olpc/via/devices.fth	2009-08-18 16:31:24 UTC (rev 1299)
+++ cpu/x86/pc/olpc/via/devices.fth	2009-08-19 03:40:15 UTC (rev 1300)
@@ -45,7 +45,7 @@
 : stand-init-io  ( -- )
    stand-init-io
    acpi-calibrate-tsc
-   d# 800 to us-factor  d# 800000 to ms-factor
+   d# 1000 to us-factor  d# 1000000 to ms-factor
 ;
 warning !
 
@@ -242,18 +242,21 @@
 
 \needs md5init  fload ${BP}/ofw/ppp/md5.fth                \ MD5 hash
 
-fload ${BP}/dev/olpc/kb3700/ecspi.fth      \ EC chip SPI FLASH access
+fload ${BP}/dev/olpc/spiflash/flashif.fth   \ Generic FLASH interface
+fload ${BP}/dev/olpc/spiflash/memflash.fth  \ Memory-mapped FLASH read access
 
 warning @ warning off
 : stand-init-io  stand-init-io  h# fff0.0000 to flash-base  ;
 warning !
 
-fload ${BP}/dev/olpc/kb3700/ecserial.fth   \ Serial access to EC chip
+fload ${BP}/dev/olpc/spiflash/spiif.fth     \ Generic low-level SPI bus access
+fload ${BP}/dev/olpc/spiflash/spiflash.fth  \ SPI FLASH programming
+fload ${BP}/dev/olpc/kb3700/ecspi.fth       \ EC chip SPI FLASH access
+.( XXX Implement ignore-power-button) cr  : ignore-power-button ( -- ) ;
+fload ${BP}/dev/olpc/kb3700/ecio.fth        \ I/O space access to EC chip
+fload ${BP}/dev/via/spi/spi.fth             \ Driver for Via SPI controller
+fload ${BP}/dev/via/spi/bbspi.fth           \ Tethered SPI FLASH programming
 
-.( XXX Implement ignore-power-button) cr
-: ignore-power-button ( -- ) ;
-fload ${BP}/dev/olpc/kb3700/ecio.fth       \ I/O space access to EC chip
-
 fload ${BP}/cpu/x86/pc/olpc/via/boardrev.fth   \ Board revision decoding
 
 : cpu-mhz  ( -- n )
@@ -279,15 +282,13 @@
 
 fload ${BP}/cpu/x86/pc/olpc/mfgdata.fth      \ Manufacturing data
 fload ${BP}/cpu/x86/pc/olpc/mfgtree.fth      \ Manufacturing data in device tree
-.( XXX Reinstate kbdtype.fth) cr
-\ fload ${BP}/cpu/x86/pc/olpc/kbdtype.fth      \ Export keyboard type
+fload ${BP}/cpu/x86/pc/olpc/kbdtype.fth      \ Export keyboard type
 
 fload ${BP}/dev/olpc/kb3700/battery.fth      \ Battery status reports
    
-fload ${BP}/dev/olpc/spiflash/spiflash.fth   \ SPI FLASH programming
 fload ${BP}/dev/olpc/spiflash/spiui.fth      \ User interface for SPI FLASH programming
 h# 2c to crc-offset
-\ fload ${BP}/dev/olpc/spiflash/recover.fth    \ XO-to-XO SPI FLASH recovery
+fload ${BP}/cpu/x86/pc/olpc/via/recover.fth  \ XO-to-XO SPI FLASH recovery
 
 : ofw-fw-filename$  " disk:\boot\olpc.rom"  ;
 ' ofw-fw-filename$ to fw-filename$

Added: cpu/x86/pc/olpc/via/recover.fth
===================================================================
--- cpu/x86/pc/olpc/via/recover.fth	                        (rev 0)
+++ cpu/x86/pc/olpc/via/recover.fth	2009-08-19 03:40:15 UTC (rev 1300)
@@ -0,0 +1,25 @@
+purpose: Reprogram SPI FLASH from another XO-1.5
+
+\ Use a working OLPC XO-1.5 board to recover one whose firmware is bad,
+\ using a 6-pin ribbon cable connected from J3 on the good machine to J2
+\ on the bad one.
+\
+\ The commands are:
+\    ok clone                   \ Copies this machine's FLASH to the dead one
+\    ok recover disk:\file.rom  \ Copies a ROM file to the dead one
+
+: (tethered-flash)  ( -- )
+   use-bb-spi
+   reflash
+   use-local-ec
+;
+
+: clone  ( -- )
+   ." Getting a copy of this machine's FLASH" cr
+   slow-flash-read
+   true to file-loaded?
+
+   (tethered-flash)
+;
+
+: recover  ( "filename" -- )  get-file (tethered-flash)  ;

Modified: dev/olpc/kb3700/ecio.fth
===================================================================
--- dev/olpc/kb3700/ecio.fth	2009-08-18 16:31:24 UTC (rev 1299)
+++ dev/olpc/kb3700/ecio.fth	2009-08-19 03:40:15 UTC (rev 1300)
@@ -304,11 +304,13 @@
 ;
 
 : io-spi-start  ( -- )
-   ['] io-spi@  to spi@
-   ['] io-spi!  to spi!
+   ['] io-spi@    to spi@
+   ['] io-spi!    to spi!
    ['] io-spi-out to spi-out
+   use-ec-spi     \ spi-in, spi-cs-on, spi-cs-off via EC commands
+
    ['] io-spi-reprogrammed to spi-reprogrammed
-   h# fff0.0000 to flash-base
+   use-mem-flash-read
 
    7 to spi-us   \ Measured time for "1 fea9 ec!" is 7.9 uS
 

Modified: dev/olpc/kb3700/ecserial.fth
===================================================================
--- dev/olpc/kb3700/ecserial.fth	2009-08-18 16:31:24 UTC (rev 1299)
+++ dev/olpc/kb3700/ecserial.fth	2009-08-19 03:40:15 UTC (rev 1300)
@@ -12,6 +12,7 @@
    ['] serial-spi@  to spi@
    ['] serial-spi!  to spi!
    ['] spicmd!      to spi-out
+   use-ec-spi       \ spi-in, spi-cs-on, spi-cs-off via EC commands
 
    d# 200 to spi-us  \ Approximate time to do serial-spi!
    ['] noop to spi-reprogrammed
@@ -32,11 +33,10 @@
    d# 115200 baud
 
    d# 50 ms         \ Settling time
-
-   -1 to flash-base
 ;
 : use-serial-ec  ( -- )  ['] serial-spi-start to spi-start  ;
 use-serial-ec  \ Install this as the start-spi implementation
+
 \ LICENSE_BEGIN
 \ Copyright (c) 2006 FirmWorks
 \ 

Modified: dev/olpc/kb3700/ecspi.fth
===================================================================
--- dev/olpc/kb3700/ecspi.fth	2009-08-18 16:31:24 UTC (rev 1299)
+++ dev/olpc/kb3700/ecspi.fth	2009-08-19 03:40:15 UTC (rev 1300)
@@ -7,15 +7,8 @@
 \ The implementations of these access methods are defined in other files
 \ because they depend on details of the host system.
 
--1 value flash-base            \ If not -1, memory mapped FLASH address
-defer spi-start ( -- )         \ Init routine for the access path to the EC
 defer spi@      ( reg# -- b )  \ Read an EC SPI register
 defer spi!      ( b reg# -- )  \ Write an EC SPI register
-defer spi-out   ( b -- )       \ Write SPI command and wait if necessary
-1 value spi-us  ( -- n )       \ Approximate time in uS to do spi!
-                               \ Used to optimize some routines
-defer spi-reprogrammed  ( -- ) \ What to do when done reprogramming
-' noop to spi-reprogrammed
 
 \ Symbolic names for the registers that control SPI access from the EC
 
@@ -38,22 +31,6 @@
 : enable-flash  ( -- )  spicfg@  8 or  spicfg!  ;  \ SPICMDWE
 [then]
 
-\ I'm not sure this delay is necessary, but the EnE code did it, so
-\ I'm being safe.  The EnE code did 4 PCI reads of the base address
-\ which should be around 800nS.  2 uS should cover it in case I'm wrong
-: short-delay  ( -- )  2 us  ;
-
-\ Some chips (e.g. Spansion) don't work in hardware mode, so we do
-\ everything in "firmware mode", where we have control over the SPI bus.
-\ Every spicmd! clocks out 8 bits.  To read, you have to do a dummy
-\ write of the value 0, then you can read the data from the spidata register.
-
-\ Turning on the firmware mode bit asserts SPICS#
-\ Turning off the firmware mode bit deasserts SPICS#
-
-: spi-cs-on   ( -- )  h# 18 spicfg!  short-delay  ;  \ 10 is the firmware mode bit
-: spi-cs-off  ( -- )  h# 08 spicfg!  ;
-
 \ Poll the spicfg register waiting for the busy bit to go away
 \ This tells you when the hardware has finished sending the byte
 \ out serially over the SPI lines.
@@ -68,12 +45,31 @@
    again
 ;
 
+\ Some chips (e.g. Spansion) don't work in hardware mode, so we do
+\ everything in "firmware mode", where we have control over the SPI bus.
+\ Every spicmd! clocks out 8 bits.  To read, you have to do a dummy
+\ write of the value 0, then you can read the data from the spidata register.
+
+\ Turning on the firmware mode bit asserts SPICS#
+\ Turning off the firmware mode bit deasserts SPICS#
+
+: ec-spi-cs-on   ( -- )  h# 18 spicfg!  short-delay  ;  \ 10 is the firmware mode bit
+: ec-spi-cs-off  ( -- )  h# 08 spicfg!  ;
+
 \ To read a byte from SPI, the EC has to pulse the clock while the
 \ SPI flash device drives DO.  The "0 spi-out" is a "dummy write"
 \ that pulses the clock 8 times.  The data can then be read from the
 \ spidata register.
 
-: spi-in  ( -- b )  0 spi-out  spidata@  ;
+: ec-spi-in  ( -- b )  0 spi-out  spidata@  ;
+
+: use-ec-spi  ( -- )
+   ['] ec-spi-in     to spi-in
+   ['] ec-spi-cs-on  to spi-cs-on
+   ['] ec-spi-cs-off to spi-cs-off
+;
+use-ec-spi
+
 \ LICENSE_BEGIN
 \ Copyright (c) 2006 FirmWorks
 \ 

Added: dev/olpc/spiflash/flashif.fth
===================================================================
--- dev/olpc/spiflash/flashif.fth	                        (rev 0)
+++ dev/olpc/spiflash/flashif.fth	2009-08-19 03:40:15 UTC (rev 1300)
@@ -0,0 +1,35 @@
+\ See license at end of file
+purpose: Generic interface for FLASH programming operations
+
+defer flash-write-enable   ( -- )
+defer flash-write-disable  ( -- )
+defer flash-write          ( adr len offset -- )
+defer flash-read           ( adr len offset -- )
+defer flash-verify         ( adr len offset -- mismatch? )
+defer flash-erase-block    ( offset -- )
+h# 10.0000 value /flash-block
+h# 10000 value /flash-block
+
+\ LICENSE_BEGIN
+\ Copyright (c) 2009 FirmWorks
+\ 
+\ Permission is hereby granted, free of charge, to any person obtaining
+\ a copy of this software and associated documentation files (the
+\ "Software"), to deal in the Software without restriction, including
+\ without limitation the rights to use, copy, modify, merge, publish,
+\ distribute, sublicense, and/or sell copies of the Software, and to
+\ permit persons to whom the Software is furnished to do so, subject to
+\ the following conditions:
+\ 
+\ The above copyright notice and this permission notice shall be
+\ included in all copies or substantial portions of the Software.
+\ 
+\ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+\ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+\ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+\ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+\ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+\ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+\ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+\
+\ LICENSE_END

Added: dev/olpc/spiflash/memflash.fth
===================================================================
--- dev/olpc/spiflash/memflash.fth	                        (rev 0)
+++ dev/olpc/spiflash/memflash.fth	2009-08-19 03:40:15 UTC (rev 1300)
@@ -0,0 +1,34 @@
+\ See license at end of file
+purpose: FLASH read for memory-mapped FLASH
+
+-1 value flash-base            \ If not -1, memory mapped FLASH address
+
+: mem-read-flash  ( adr len offset -- )   flash-base + -rot move  ;
+
+: tethered?  ( -- flag )  ['] flash-read behavior  ['] mem-read-flash <>  ;
+
+: use-mem-flash-read  ( -- )  ['] mem-read-flash to flash-read  ;
+
+\ LICENSE_BEGIN
+\ Copyright (c) 2009 FirmWorks
+\ 
+\ Permission is hereby granted, free of charge, to any person obtaining
+\ a copy of this software and associated documentation files (the
+\ "Software"), to deal in the Software without restriction, including
+\ without limitation the rights to use, copy, modify, merge, publish,
+\ distribute, sublicense, and/or sell copies of the Software, and to
+\ permit persons to whom the Software is furnished to do so, subject to
+\ the following conditions:
+\ 
+\ The above copyright notice and this permission notice shall be
+\ included in all copies or substantial portions of the Software.
+\ 
+\ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+\ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+\ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+\ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+\ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+\ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+\ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+\
+\ LICENSE_END

Modified: dev/olpc/spiflash/spiflash.fth
===================================================================
--- dev/olpc/spiflash/spiflash.fth	2009-08-18 16:31:24 UTC (rev 1299)
+++ dev/olpc/spiflash/spiflash.fth	2009-08-19 03:40:15 UTC (rev 1300)
@@ -48,6 +48,11 @@
 
 : spi-read-status  ( -- b )  5 spi-cmd  spi-in  spi-cs-off  ; \ READSTATUS
 
+\ I'm not sure this delay is necessary, but the EnE code did it, so
+\ I'm being safe.  The EnE code did 4 PCI reads of the base address
+\ which should be around 800nS.  2 uS should cover it in case I'm wrong
+: short-delay  ( -- )  2 us  ;
+
 \ You have to write-enable before any command that modifes stuff
 \ inside the part - writes, erases, status register writes
 \ The write-enabled mode is mostly self-clearing; the exception
@@ -212,52 +217,31 @@
    spi-write-disable
 ;
 
+defer spi-reprogrammed  ( -- ) \ What to do when done reprogramming
+' noop to spi-reprogrammed
+
 defer write-spi-flash  ( adr len offset -- )
 
-\ Common start sequence for read and verify
-
-: setup-spi-read  ( offset -- )
-   \ Fast read command - no point since host access is the bottleneck
-   \ h# b spi-cmd spi-adr  0 spi-out    ( adr len )
-
-   3 spi-cmd  spi-adr
-;
-
 \ Read len bytes of data from the SPI FLASH beginning at offset
 \ into the memory buffer at adr
 
 : read-spi-flash  ( adr len offset -- )
-   flash-base -1 <>  if               ( adr len offset )
-      flash-base + -rot move          ( )
-   else                               ( adr len offset )
-      setup-spi-read                  ( adr len )
-      bounds  ?do  spi-in i c!  loop  ( )
-      spi-cs-off                      ( )
-   then                               ( )
+   \ Fast read command - no point since host access is the bottleneck
+   \ h# b spi-cmd spi-adr  0 spi-out    ( adr len )
+   3 spi-cmd  spi-adr              ( adr len )
+   bounds  ?do  spi-in i c!  loop  ( )
+   spi-cs-off                      ( )
 ;
 
-\ Implementation factor for verify.  Throws an error on mismatch.
-
-: (verify-spi-flash)  ( adr len -- )
-   bounds  ?do  spi-in  i c@  <>  throw  loop  ( )
-;
-
 \ Verify the contents of SPI FLASH starting at offset against
 \ the memory range adr,len .  Aborts with a message on mismatch.
 
-: verify-spi-flash  ( adr len offset -- )
-   flash-base -1 <>  if               ( adr len offset )
-      \ If the FLASH is memory mapped we can just read it directly
-      flash-base + swap comp          ( mismatch? )
-   else                               ( adr len offset )
-      setup-spi-read
-      ['] (verify-spi-flash)  catch   ( error? )
-
-      \ Be sure to turn off the chip select even on a mismatch
-      spi-cs-off                      ( error? )
-   then                               ( error? )
-
-   abort" Verify failed"
+: verify-spi-flash  ( adr len offset -- mismatch? )
+   over alloc-mem >r                  ( adr len offset r: temp-adr )
+   r@  2 pick  rot                    ( adr len temp-adr len offset r: temp-adr )
+   flash-read                         ( adr len r: temp-adr )
+   tuck  r@ swap comp                 ( len mismatch? r: temp-adr )
+   r> rot free-mem                    ( mismatch? )
 ;
 
 : jedec-id  ( -- b3b2b1)
@@ -312,26 +296,16 @@
 
 : spi-flash-write-enable  ( -- )  spi-start spi-identify  .spi-id cr  ;
 
-\ Create defer words for generic FLASH writing routines if necessary
-[ifndef] flash-write-enable
-defer flash-write-enable   ( -- )
-defer flash-write-disable  ( -- )
-defer flash-write          ( adr len offset -- )
-defer flash-read           ( adr len offset -- )
-defer flash-verify         ( adr len offset -- )
-defer flash-erase-block    ( offset -- )
-h# 10.0000 value /flash-block
-h# 10000 value /flash-block
-[then]
+: use-spi-flash-read  ( -- )  ['] read-spi-flash to flash-read  ;
 
 \ Install the SPI FLASH versions as their implementations.
 : use-spi-flash  ( -- )
    ['] spi-flash-write-enable  to flash-write-enable
    ['] spi-reprogrammed        to flash-write-disable
    ['] write-spi-flash         to flash-write
-   ['] read-spi-flash          to flash-read
    ['] verify-spi-flash        to flash-verify
    ['] erase-spi-block         to flash-erase-block
+   use-spi-flash-read          \ Might be overridden
    h# 10.0000  to /flash
    /spi-eblock to /flash-block
 ;

Added: dev/olpc/spiflash/spiif.fth
===================================================================
--- dev/olpc/spiflash/spiif.fth	                        (rev 0)
+++ dev/olpc/spiflash/spiif.fth	2009-08-19 03:40:15 UTC (rev 1300)
@@ -0,0 +1,35 @@
+\ See license at end of file
+purpose: Generic low-level SPI interface
+
+defer spi-start  ( -- )      \ Init the SPI controller
+defer spi-cs-on  ( -- )      \ Begin SPI transaction
+defer spi-cs-off ( -- )      \ End SPI transaction
+defer spi-out    ( b -- )    \ Write a byte to the SPI bus
+defer spi-in     ( -- b )    \ Read a byte from the SPI bus
+
+1 value spi-us   ( -- n )    \ Approximate time in uS to do spi-out
+                             \ Used to optimize some routines
+
+\ LICENSE_BEGIN
+\ Copyright (c) 2009 FirmWorks
+\ 
+\ Permission is hereby granted, free of charge, to any person obtaining
+\ a copy of this software and associated documentation files (the
+\ "Software"), to deal in the Software without restriction, including
+\ without limitation the rights to use, copy, modify, merge, publish,
+\ distribute, sublicense, and/or sell copies of the Software, and to
+\ permit persons to whom the Software is furnished to do so, subject to
+\ the following conditions:
+\ 
+\ The above copyright notice and this permission notice shall be
+\ included in all copies or substantial portions of the Software.
+\ 
+\ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+\ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+\ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+\ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+\ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+\ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+\ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+\
+\ LICENSE_END

Modified: dev/olpc/spiflash/spiui.fth
===================================================================
--- dev/olpc/spiflash/spiui.fth	2009-08-18 16:31:24 UTC (rev 1299)
+++ dev/olpc/spiflash/spiui.fth	2009-08-19 03:40:15 UTC (rev 1300)
@@ -1,5 +1,5 @@
 \ See license at end of file
-\ User interface for reflashing SPI FLASH parts
+purpose: User interface for reflashing SPI FLASH parts
 
 \ This code is concerned with the user interface for getting
 \ a new firmware image into memory and using it to program
@@ -20,14 +20,14 @@
    ?do                ( adr )
       \ Save time - don't write if the data is the same
       i .x (cr                              ( adr )
-      flash-base -1 =  if                   ( adr )
+      spi-us d# 20 >=  if                   ( adr )
+         \ Just write if reading is slow
          true                               ( adr must-write? )
       else                                  ( adr )
-         dup  flash-base i +  /flash-block comp   ( adr must-write? )
+         dup  /flash-block  i  flash-verify   ( adr must-write? )
       then                                  ( adr must-write? )
 
       if
-\         i /flash-block mod 0=  if  i flash-erase-block  then
          i flash-erase-block
          dup  /flash-block  i  flash-write  ( adr )
       then
@@ -40,7 +40,7 @@
    ." Verifying" cr
    ?do                ( adr )
       i .x (cr
-      dup   /flash-block  i  flash-verify
+      dup   /flash-block  i  flash-verify   abort" Verify failed"
       /flash-block +  ( adr' )
    /flash-block +loop ( adr )
    cr  drop           ( )
@@ -186,7 +186,7 @@
 : ?move-mfg-data  ( -- )
    ." Merging existing manufacturing data" cr
 
-   flash-base -1 =  if
+   tethered?  if
       \ Read the manufacturing data from the other FLASH
       \ First try the new location in the e.0000 block
       flash-buf mfg-data-offset +  /flash-block  mfg-data-offset  flash-read
@@ -338,7 +338,7 @@
    /flash-block  /string      ( adr+ len- )   \ Remove EC ucode from the beginning
    ." Erasing ..." cr erase-spi-firmware  ( adr len )
    ." Programming..."  2dup /flash-block  flash-write  cr    ( adr len )
-   ." Verifying..."  /flash-block flash-verify  cr  ( )
+   ." Verifying..."  /flash-block flash-verify  abort" Verify failed"  cr  ( )
 ;
 
 : flash-bios  ( -- )

Added: dev/via/spi/bbspi.fth
===================================================================
--- dev/via/spi/bbspi.fth	                        (rev 0)
+++ dev/via/spi/bbspi.fth	2009-08-19 03:40:15 UTC (rev 1300)
@@ -0,0 +1,111 @@
+\ See license at end of file
+purpose: SPI driver for tethered SPI FLASH programming
+
+\ It is tempting to use the Via's SPI hardware instead of
+\ bitbanging, but that turns out to be slower because the
+\ special FLASH mode to make 256-byte write cycles possible
+\ only work if the host system boots from SPI FLASH.
+\ Without that mode, you can only send 12 data bytes per
+\ "program page" transaction.  That ends up being slower
+\ overall than bitbanging.  Bitbanging loses on the per-bit
+\ time, but lets you do an efficient 256-byte page program.
+
+: gpo-port  ( -- port# )  acpi-io-base h# 4c +  ;
+: gpi-port  ( -- port# )  acpi-io-base h# 48 +  ;
+
+h# 20000 constant clk-mask
+h#  0008 constant cs-mask
+h#  0002 constant do-mask
+h#  0001 constant di-mask
+
+\ DI:GPI0, CLK:GPIO6, DO:GPO1, SS0#:GPO3
+: bb-spi-cs-on   ( -- )  gpo-port dup pc@ 8 invert and  swap pc!  ;
+: bb-spi-cs-off  ( -- )  gpo-port dup pc@ 8 or          swap pc!  ;
+
+code bb-spi-out  ( b -- )
+   bx pop
+
+   gpo-port # dx mov          \ Base value for register
+   dx ax in
+
+   8 # cx mov
+   begin
+      clk-mask do-mask +  invert #  ax  and   \ Clk low for first half-cycle
+      h# 80 # bl test  0<>  if
+         do-mask # ax or         \ Possibly set DO
+      then
+      ax dx out                  \ CLK low period
+      clk-mask #  ax  or         \ CLK high period
+      ax dx out
+      bl bl add                  \ Shift left
+   loopa
+c;
+
+code bb-spi-in  ( -- b )
+   bp push
+
+   gpo-port # dx mov          \ Out port
+   dx ax in                   \ Base value for out register
+   clk-mask do-mask +  invert #  ax  and   \ Clk low for first half-cycle
+   ax bp mov                  \ Save base value in BP
+
+   bx bx xor
+
+   8 # cx mov
+   begin
+      bp ax mov                  \ Base value
+      gpo-port # dx mov          \ Out port
+      ax dx out                  \ CLK low
+      clk-mask #  ax  or         \ CLK high
+      ax dx out                  \ Push it out
+
+      gpi-port # dx mov          \ In port
+      dx ax in
+      bx bx  add                 \ Shift left
+      di-mask #  ax  test  0<>  if
+         bx inc
+      then
+   loopa
+   bp pop
+   bx push
+c;
+
+: bb-spi-start  ( -- )
+   ['] bb-spi-in     to spi-in
+   ['] bb-spi-out    to spi-out
+   ['] bb-spi-cs-on  to spi-cs-on
+   ['] bb-spi-cs-off to spi-cs-off
+   d# 12 to spi-us
+   ['] noop to spi-reprogrammed
+\  use-spi-flash-read           \ Readback with SPI commands, not memory ops
+   use-hw-spi-flash-read
+
+   gpo-port dup pc@  clk-mask invert and  do-mask invert and  cs-mask or  swap pc!     \ CLK, DO low, CS# high
+   h# 88e4 config-b@ 2 or  h# 88e4 config-b!
+;
+
+: use-bb-spi  ( -- )  ['] bb-spi-start  to spi-start  ;
+
+\ LICENSE_BEGIN
+\ Copyright (c) 2009 FirmWorks
+\ 
+\ Permission is hereby granted, free of charge, to any person obtaining
+\ a copy of this software and associated documentation files (the
+\ "Software"), to deal in the Software without restriction, including
+\ without limitation the rights to use, copy, modify, merge, publish,
+\ distribute, sublicense, and/or sell copies of the Software, and to
+\ permit persons to whom the Software is furnished to do so, subject to
+\ the following conditions:
+\ 
+\ The above copyright notice and this permission notice shall be
+\ included in all copies or substantial portions of the Software.
+\ 
+\ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+\ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+\ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+\ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+\ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+\ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+\ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+\
+\ LICENSE_END

Added: dev/via/spi/spi.fth
===================================================================
--- dev/via/spi/spi.fth	                        (rev 0)
+++ dev/via/spi/spi.fth	2009-08-19 03:40:15 UTC (rev 1300)
@@ -0,0 +1,262 @@
+\ See license at end of file
+purpose: Driver for Via VX8xx SPI controller, for FLASH reprogramming
+
+[ifndef] spi0-mmio-base h# fed4.0000 constant spi0-mmio-base  [then]
+
+0 0 " " " /" begin-package
+
+" spi" device-name
+
+0 value spi-base
+0 value spi0-base
+: spi-b@  ( offset -- b )  spi0-base + c@  ;
+: spi-b!  ( b offset -- )  spi0-base + c!  ;
+: spi-w@  ( offset -- w )  spi0-base + w@  ;
+: spi-w!  ( w offset -- )  spi0-base + w!  ;
+: spi-l@  ( offset -- l )  spi0-base + l@  ;
+: spi-l!  ( l offset -- )  spi0-base + l!  ;
+
+: set-spi-base  ( adr -- )
+   dup  lbsplit  h# 88be config-b!  h# 88bd config-b!  h# 88bc config-b!  drop  ( adr )
+   h# 1000 " map-in" $call-parent to spi-base
+;
+: set-spi0-base  ( adr -- )
+   dup 1 or spi-base l!
+   h# 1000 " map-in" $call-parent to spi0-base
+;
+: grab-spi  ( -- )
+   h# 88e4 config-b@ 2 invert and  h# 88e4 config-b!  \ SPI mode for pins
+;
+: release-spi  ( -- )
+   0 spi-base l!  \ Disable controller
+   h# 88e4 config-b@ 2 or  h# 88e4 config-b!  \ GPIO mode for pins
+;
+
+: pio33mhz  ( -- )   \ Fastest PIO mode
+   h# 00 h# 6c spi-b!    \ 33 MHz
+   h# 04 h# 6e spi-b!    \ Sample on falling edge
+   h# 08 h# 6d spi-b!    \ Dynamic clock, PIO mode, command posted write off   
+;
+: dma16mhz  ( -- )   \ Fastest DMA mode
+   h# 01 h# 6c spi-b!    \ 33/(2*1) MHz
+   h# 03 h# 6e spi-b!    \ Delay mode 3
+   h# 48 h# 6d spi-b!    \ Dynamic clock, DMA mode, command posted write off   
+;
+: pio8mhz  ( -- )
+   h# 02 h# 6c spi-b!    \ 33/(2*2) MHz
+   h# 00 h# 6e spi-b!    \ No special clocking
+   h# 08 h# 6d spi-b!    \ Dynamic clock, PIO mode, command posted write off   
+;
+: dma8mhz  ( -- )
+   h# 02 h# 6c spi-b!    \ 33/(2*2) MHz
+   h# 00 h# 6e spi-b!    \ No special clocking
+   h# 48 h# 6d spi-b!    \ Dynamic clock, DMA mode, command posted write off   
+;
+: init-spi  ( -- )
+   grab-spi
+   spi-mmio-base set-spi-base
+   spi0-mmio-base set-spi0-base
+   h# 0c h# 00 spi-b!    \ Clear RW1C status bits
+   h# 00 h# 70 spi-b!    \ All interrupts off
+   h# ff h# 73 spi-b!    \ Clear DMA buffer interrupts
+   pio8mhz
+;
+
+: rb spi-b@ . ;  : wb spi-b! ;
+: rw spi-w@ . ;  : ww spi-w! ;
+: rl spi-l@ . ;  : wl spi-l! ;
+
+: open ( -- okay? )  init-spi true  ;
+: close  ( -- )  release-spi  ;
+
+: wait-ready  ( -- )  begin  0 spi-w@ 1 and 0=  until  ;
+: wait-done  ( -- )  begin  0 spi-w@ 4 and  until  ;
+: clear-done  ( -- )   4 0 spi-w!  ;
+: spi-cmd!  ( bits -- )  2 spi-w!  ;
+
+: spi-go  ( -- )
+   2 spi-cmd!    ( )  \ Set go bit
+   wait-done     ( )  \ Wait for done bit
+   0 spi-cmd!    ( )  \ Clear go bit
+   \ The go bit must be cleared before clear-done, otherwise clear-done will not work
+   clear-done    ( )  \ Clear done bit
+;
+
+: dma-setup
+   h#   200000 h# 74 spi-l!  \ Buffer to read from
+   h#   100000 h# 78 spi-l!  \ Buffer to write to
+   h# 10001000 h# 7c spi-l!  \ Buffer wraparound boundaries
+;
+
+h#  20 buffer: (out-buf)
+h# 400 buffer:  (in-buf)
+
+\ Prevent wrap-around; the DMA address register has issues with boundaries.
+: out-buf  ( -- adr )  (out-buf) h# 10 round-up  ;
+: in-buf   ( -- adr )  (in-buf) h# 200 round-up  ;
+
+\ Return the largest length that does not fall in one of the
+\ (undocumented) gaps where the DMA counter doesn't work right
+
+: best-length  ( len -- len' )
+   dup h# 1fc >  if  drop h# 1fc exit  then  ( len )
+   dup h# 17d h# 183 between  if  drop h# 17c exit  then   ( len )
+   dup h# 0fd h# 103 between  if  drop h# 0fc exit  then   ( len )
+   dup h# 07d h# 083 between  if  drop h# 07c exit  then   ( len )
+;
+
+\ Read as many as possible up to len, accounting for unavailable lengths
+: dma-read-some  ( adr len offset -- #read )
+   swap best-length swap                 ( adr len' offset )
+   dma16mhz                              ( adr len offset )
+   wait-ready                            ( adr len offset )
+
+   \ Setup command (3) and FLASH address
+   3 out-buf c!                          ( adr len offset )
+   lbsplit drop  out-buf 1+ c!  out-buf 2+ c! out-buf 3 + c!  ( adr len )
+   
+   \ Setup cycle control register with indicated read length (-1)
+   \ and write length for 1 command byte plus 3 data bytes
+   dup 1- 4 lshift  h# c003 or  h# 71 spi-w!    ( adr len )
+
+   in-buf  >physical h# 78 spi-l!  \ Buffer to write to
+   out-buf >physical h# 74 spi-l!  \ Buffer to read from
+   h# 0200.0010 h# 7c spi-l!       \ Buffer wraparound boundaries
+
+   spi-go                                ( adr len )
+   tuck  in-buf -rot move                ( len )
+;
+: dma-read  ( adr len offset -- )
+   begin  over  while                    ( adr len offset )
+      3dup dma-read-some                 ( adr len offset #read )
+      tuck +  >r                         ( adr len #read r: offset' )
+      /string r>                         ( adr' len' offset' )   
+   repeat                                ( adr len offset )
+   3drop
+;
+
+[ifdef] notdef
+: dma-out  ( len -- )
+   h# 40 h# 6d spi-b!  0 2 spi-w!        ( len )
+   wait-ready                            ( len )
+   dma-setup
+
+   \ Write to SPI mode
+   h# 2000 h# 71 spi-w!                  ( len )
+   
+   spi-go
+   2 2 spi-w!
+
+   
+   h# 200000 +                           ( endadr )
+   begin  dup  h# 80 spi-l@  u<= until   ( endadr )
+   drop                                  ( )
+   0 2 spi-w!                            ( )
+;
+[then]
+
+[ifdef] notdef
+
+\ The following FLASH-mode commands don't work unless the Via chip
+\ is hardware-strapped to use SPI FLASH as the boot ROM.  Without
+\ that strapping, the SPI controller only works in device mode,
+\ ignoring the atomic cycle bits, the address register, etc.
+\ That fact is undocumented, but confirmed by Via.
+: cmd-setup  ( -- )
+   h# 06 h# 54 spi-b!  \ Write enable prefix   
+   h# 06 h# 55 spi-b!  \ Write enable prefix (2)
+
+   h# b9 h# 58 spi-b!  \ 0 Deep power down
+   h# c7 h# 59 spi-b!  \ 1 Erase all
+   h# d8 h# 5a spi-b!  \ 2 Erase 64K
+   h# 02 h# 5b spi-b!  \ 3 Page Program
+   h# 0b h# 5c spi-b!  \ 4 Fast Read
+   h# 01 h# 5d spi-b!  \ 5 Write status register
+   h# 05 h# 5e spi-b!  \ 6 Read status register
+   h# ab h# 5f spi-b!  \ 7 Release power down
+
+   h# f4 h# 56 spi-b!  \ 3WriteAddr, 2WriteAddr, 1Write, 0Read
+   h# 46 h# 57 spi-b!  \ 7Write, 6Read, W5rite, 4ReadAddr
+;
+
+: spi-addr!  ( addr -- )  4 spi-l!  ;
+
+: sector-erase  ( offset -- )
+   wait-ready  spi-addr!  h# 0026 spi-cmd!
+;
+: erase-all  ( -- )  wait-ready  h# 0016 spi-cmd!  ;
+: inject-bytes  ( adr len -- )
+   0  ?do            ( adr )
+      dup i + c@     ( adr byte )
+      8 i + spi-b!   ( adr )
+   loop              ( adr )
+   drop              ( )
+;
+: program-bytes  ( adr len offset -- )
+   wait-ready  spi-addr!  tuck   ( len adr len )
+   inject-bytes
+   1- 8 lshift h# 4036 or spi-cmd!
+;
+: program-page   ( adr len offset -- )
+   wait-ready  spi-addr!                ( adr len )
+   begin  dup  while                    ( adr len )
+      2dup d# 16 min tuck               ( adr len thislen adr thislen )
+      inject-bytes                      ( adr len thislen )
+      dup 1- 8 lshift h# 4036 or        ( adr len thislen cmd )
+      over d# 16 =  if  h# 80 or  then  ( adr len thislen cmd' )
+      spi-cmd!                          ( adr len thislen )
+      /string                           ( adr' len' )
+   repeat                               ( adr' len' )
+   2drop
+;
+: fast-read  ( adr len offset -- )
+   wait-ready  spi-addr!                  ( adr len )
+\  dup 1- 8 lshift  h# 6042 or  spi-cmd!  ( adr len )
+   dup 1- 8 lshift  h# 4043 or  spi-cmd!  ( adr len )
+   wait-done                              ( adr len )
+   0  ?do                                 ( adr )
+      8 i + spi-b@  dup i + c!            ( adr )
+   loop  drop                             ( )
+;
+: power-down  ( -- )  wait-ready  h# 0006 spi-cmd!  ;
+: power-up  ( -- )  wait-ready  h# 0072 spi-cmd!  ;
+: sr!  ( value -- )  wait-ready  8 spi-b!  h# 4056 spi-cmd!  ;
+: sr@  ( -- value )
+   wait-ready h# 4062 spi-cmd!  wait-done  8 spi-b@
+;
+[then]
+
+end-package
+
+[ifdef] flash-read
+: hw-flash-read  ( adr len offset -- )
+   " /spi" open-dev >r          ( adr len offset )
+   " dma-read" r@ $call-method  ( )
+   r> close-dev                 ( )
+;
+: use-hw-spi-flash-read  ( -- )  ['] hw-flash-read to flash-read  ;
+[then]
+
+\ LICENSE_BEGIN
+\ Copyright (c) 2009 FirmWorks
+\ 
+\ Permission is hereby granted, free of charge, to any person obtaining
+\ a copy of this software and associated documentation files (the
+\ "Software"), to deal in the Software without restriction, including
+\ without limitation the rights to use, copy, modify, merge, publish,
+\ distribute, sublicense, and/or sell copies of the Software, and to
+\ permit persons to whom the Software is furnished to do so, subject to
+\ the following conditions:
+\ 
+\ The above copyright notice and this permission notice shall be
+\ included in all copies or substantial portions of the Software.
+\ 
+\ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+\ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+\ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+\ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+\ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+\ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+\ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+\
+\ LICENSE_END




More information about the openfirmware mailing list