[openfirmware] [commit] r1992 - cpu/arm cpu/arm/mmp2 cpu/arm/olpc/1.75 dev dev/olpc/dcon dev/olpc/kb3700 dev/olpc/mmp2camera dev/pci

repository service svn at openfirmware.info
Fri Oct 29 18:17:26 CEST 2010


Author: wmb
Date: Fri Oct 29 18:17:26 2010
New Revision: 1992
URL: http://tracker.coreboot.org/trac/openfirmware/changeset/1992

Log:
OLPC XO-1.75 - another checkpoint - more stuff working.

Added:
   cpu/arm/mmp2/spimaster.fth
   cpu/arm/olpc/1.75/dconsmb.fth
   cpu/arm/olpc/1.75/lcd.fth
   cpu/arm/olpc/1.75/prefw.bth
   dev/olpc/dcon/mmp2dcon.fth
   dev/olpc/mmp2camera/
   dev/olpc/mmp2camera/ccic.fth
   dev/olpc/mmp2camera/loadpkg.fth
   dev/olpc/mmp2camera/ov.fth
   dev/olpc/mmp2camera/platform.fth
   dev/olpc/mmp2camera/smbus.fth
Modified:
   cpu/arm/assem.fth
   cpu/arm/disassem.fth
   cpu/arm/mmp2/sspspi.fth
   cpu/arm/mmp2/watchdog.fth
   cpu/arm/olpc/1.75/addrs.fth
   cpu/arm/olpc/1.75/boardgpio.fth
   cpu/arm/olpc/1.75/config.fth
   cpu/arm/olpc/1.75/devices.fth
   cpu/arm/olpc/1.75/fw.bth
   cpu/arm/olpc/1.75/lcdcfg.fth
   cpu/arm/olpc/1.75/probemem.fth
   cpu/arm/olpc/1.75/sp.bth
   dev/olpc/kb3700/spicmd.fth
   dev/pci/isakbd.fth
   dev/pckbd.fth
   dev/ps2mouse.fth

Modified: cpu/arm/assem.fth
==============================================================================
--- cpu/arm/assem.fth	Mon Oct 25 14:42:14 2010	(r1991)
+++ cpu/arm/assem.fth	Fri Oct 29 18:17:26 2010	(r1992)
@@ -546,6 +546,7 @@
 : amode-rrop2  ( -- )  init-operands  get-r12 get-r16 get-opr2  !op  ;
 : amode-rnop2  ( -- )  init-operands  get-r16 get-opr2  !op  ;
 : amode-rdop2  ( -- )  init-operands  get-r12 get-opr2  !op  ;
+: amode-rev    ( -- )  init-operands  get-r12 get-r00   !op  ;
 
 : amode-lsm  ( -- )
    init-operands
@@ -951,6 +952,10 @@
 : ldr  ( -- )  0410.0000 {cond} {shbt}  ;
 : str  ( -- )  0400.0000 {cond} {hbt}   ;
 
+: rev    ( -- )  06bf0f30 {cond} amode-rev  ;
+: rev16  ( -- )  06bf0fb0 {cond} amode-rev  ;
+: revsh  ( -- )  06ff0f30 {cond} amode-rev  ;
+
 : rd-field  ( reg# -- )  d# 12 set-field  ;
 : rb-field  ( reg# -- )  d# 16 set-field  ;
 

Modified: cpu/arm/disassem.fth
==============================================================================
--- cpu/arm/disassem.fth	Mon Oct 25 14:42:14 2010	(r1991)
+++ cpu/arm/disassem.fth	Fri Oct 29 18:17:26 2010	(r1992)
@@ -251,9 +251,20 @@
       imm12  if  ., ." #" +/- imm12 u.h  then
    then
 ;
+: .rev  ( -- )  {<cond>}  op.rd, .rm  ;
+: .stuff  ( -- )
+   0 d# 28 bits  h# 0fff.0ff0 and  
+   case
+      h# 06bf0f30  of  ." rev"    .rev  endof
+      h# 06bf0fb0  of  ." rev16"  .rev  endof
+      h# 06ff0f30  of  ." revsh"  .rev  endof
+      ( default )
+      ." undefined" {<cond>}
+   endcase
+;
 : .ldr/str  ( -- )   \ d# 25 3 bits 2|3 =
    0 d# 28 bits  h# 0e00.0010 and  h# 0600.0010 =  if
-      ." undefined" {<cond>}
+      .stuff
       exit
    then
    .ld/st  ." r"  {<cond>} {b}  {t}

Added: cpu/arm/mmp2/spimaster.fth
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ cpu/arm/mmp2/spimaster.fth	Fri Oct 29 18:17:26 2010	(r1992)
@@ -0,0 +1,104 @@
+\ See license at end of file
+purpose: Driver for Marvell MMP2 SSP in SPI Master Mode
+
+0 0  " d4035000"  " /" begin-package   	\ SPI interface using SSP1
+
+headerless
+
+" spi"     device-name
+
+0 0 encode-bytes
+   " Marvell,ssp-spi"  encode-string encode+
+" compatible" property
+
+my-address      my-space  h# 1000  encode-reg
+" reg" property
+
+\ 1 " #address-cells"  integer-property
+\ 0 " #size-cells"     integer-property
+
+3 /n* buffer: port-data
+: init-queue  ( -- )  port-data  3 na+  bounds  ?do  -1 i !  /n +loop  ;
+
+my-space value ssp-base
+: ssp-sscr0  ( -- adr )  ssp-base  ;
+: ssp-sscr1  ( -- adr )  ssp-base  la1+  ;
+: ssp-sssr   ( -- adr )  ssp-base  2 la+  ;
+: ssp-ssdr   ( -- adr )  ssp-base  4 la+  ;
+
+: enable  ( -- )
+   h# 87 ssp-sscr0 rl!   \ Enable, 8-bit data, SPI normal mode
+;
+: disable  ( -- )
+   h# 07 ssp-sscr0 rl!   \ 8-bit data, SPI normal mode
+;
+\ Switch to master mode, for testing
+: master  ( -- )
+   disable
+   h# 0000.0000 ssp-sscr1 rl!  \ master mode
+   enable
+;
+
+: ssp1-clk-on  7 h# d4015050 l!   3 h# d4015050 l!  ;
+\ : ssp2-clk-on  7 h# d4015054 l!   3 h# d4015052 l!  ;
+\ : ssp3-clk-on  7 h# d4015058 l!   3 h# d4015058 l!  ;
+\ : ssp4-clk-on  7 h# d401505c l!   3 h# d401505c l!  ;
+
+: wb  ( byte -- )  ssp-ssdr rl!  ;
+: rb  ( -- byte )  ssp-ssdr rl@ .  ;
+
+: select-ssp1-pins  ( -- )  d# 47  d# 43  do  h# c3 i af!  loop  ;
+
+\ Choose alternate function 4 (SSP3) for the pins we use
+: init-ssp-in-master-mode  ( -- )
+   select-ssp1-pins
+   ssp1-clk-on
+   disable   \ 8-bit data, SPI normal mode
+   0 ssp-sscr1 rl!  \ master mode
+   \ The enable bit must be set last, after all configuration is done
+   enable   \ Enable, 8-bit data, SPI normal mode
+;
+
+: .ssr  ssp-sssr rl@  .  ;
+: ssp-#bytes  ( -- n )  ssp-sssr rl@ d# 12 rshift h# f and  ;
+
+0 value open-count
+: open  ( -- flag )
+   open-count  0=  if
+      my-address my-space  h# 1000  " map-in" $call-parent  is ssp-base
+      init-ssp-in-master-mode
+   then
+   open-count 1+ to open-count
+   true
+;
+: close  ( -- )
+   open-count 1 =  if
+      ssp-base h# 1000  " map-in" $call-parent  0 is ssp-base
+   then
+   open-count 1- 0 max to open-count
+;
+end-package
+
+\ LICENSE_BEGIN
+\ Copyright (c) 2010 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: cpu/arm/mmp2/sspspi.fth
==============================================================================
--- cpu/arm/mmp2/sspspi.fth	Mon Oct 25 14:42:14 2010	(r1991)
+++ cpu/arm/mmp2/sspspi.fth	Fri Oct 29 18:17:26 2010	(r1992)
@@ -38,6 +38,10 @@
    disable-interrupts
    ignore-power-button
    ssp-spi-start
+   \ The following clears out some glitches so the chip will respond
+   \ to the ab-id command.
+   0 spi-cmd spi-cs-off
+   0 spi-cmd spi-cs-off
 ;
 
 : use-ssp-spi  ( -- )

Modified: cpu/arm/mmp2/watchdog.fth
==============================================================================
--- cpu/arm/mmp2/watchdog.fth	Mon Oct 25 14:42:14 2010	(r1991)
+++ cpu/arm/mmp2/watchdog.fth	Fri Oct 29 18:17:26 2010	(r1992)
@@ -16,7 +16,7 @@
 : wdt@  ( offset -- value )  wdt-pa +  l@  ;
 : (reset-all)  ( -- )
    enable-wdt-clock
-   2 h# 68  wdt!  \ set match register
+   2 h# 68 wdt!   \ set match register
    3 h# 64 wdt!   \ match enable: just interrupt, no reset yet
    1 h# 98 wdt!   \ Reset counter
    begin  again

Modified: cpu/arm/olpc/1.75/addrs.fth
==============================================================================
--- cpu/arm/olpc/1.75/addrs.fth	Mon Oct 25 14:42:14 2010	(r1991)
+++ cpu/arm/olpc/1.75/addrs.fth	Fri Oct 29 18:17:26 2010	(r1992)
@@ -12,7 +12,7 @@
 
 \ OFW implementation choices
 \ h# 1fe0.0000 constant fw-pa
-0 constant fw-pa
+h# 1fa0.0000 constant fw-pa
 
 [ifdef] virtual-mode
 h# f700.0000 constant fw-virt-base

Modified: cpu/arm/olpc/1.75/boardgpio.fth
==============================================================================
--- cpu/arm/olpc/1.75/boardgpio.fth	Mon Oct 25 14:42:14 2010	(r1991)
+++ cpu/arm/olpc/1.75/boardgpio.fth	Fri Oct 29 18:17:26 2010	(r1992)
@@ -19,6 +19,7 @@
    d# 34 gpio-dir-out  \ EN_WLAN_PWR
    d# 35 gpio-dir-out  \ EN_SD_PWR
    d# 57 gpio-dir-out  \ WLAN_PD#
+   d# 58 gpio-set      \ WLAN_RESET#
    d# 58 gpio-dir-out  \ WLAN_RESET#
    d# 73 gpio-dir-out  \ CAM_RST
 
@@ -221,7 +222,7 @@
    0 af,      \ GPIO_160 - ND_RDY[1]
    1 af,      \ GPIO_161 - ND_IO[12] - Not connected (TP 44)
    1 af,      \ GPIO_162 - (ND_IO[11]) - DCON_SCL
-   1 af,      \ GPIO_163 - (ND_IO[10]) - DCON_SDA
+   1 pull-up, \ GPIO_163 - (ND_IO[10]) - DCON_SDA
    1 af,      \ GPIO_164 - (ND_IO[9]) - Not connected (TP106)
    0 af,      \ GPIO_165 - ND_IO[3]
    0 af,      \ GPIO_166 - ND_IO[2]

Modified: cpu/arm/olpc/1.75/config.fth
==============================================================================
--- cpu/arm/olpc/1.75/config.fth	Mon Oct 25 14:42:14 2010	(r1991)
+++ cpu/arm/olpc/1.75/config.fth	Fri Oct 29 18:17:26 2010	(r1992)
@@ -1,4 +1,6 @@
 create debug-startup
+create olpc
+create trust-ec-keyboard
 
 fload ${BP}/cpu/arm/olpc/1.75/addrs.fth
 fload ${BP}/cpu/arm/mmp2/hwaddrs.fth

Added: cpu/arm/olpc/1.75/dconsmb.fth
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ cpu/arm/olpc/1.75/dconsmb.fth	Fri Oct 29 18:17:26 2010	(r1992)
@@ -0,0 +1,122 @@
+: smb-dly  4 us  ;
+
+: smb-data-hi  ( -- )  d# 163 gpio-set  smb-dly  ;
+: smb-data-lo  ( -- )  d# 163 gpio-clr  smb-dly  ;
+: smb-clk-hi  ( -- )  d# 162 gpio-set  smb-dly  ;
+: smb-clk-lo  ( -- )  d# 162 gpio-clr  smb-dly  ;
+: smb-data@  ( -- flag )  d# 163 gpio-pin@  ;
+: smb-clk@  ( -- flag )  d# 162 gpio-pin@  ;
+: smb-off  ( -- )  d# 163 gpio-dir-in  ;
+: smb-on  ( -- )  d# 163 gpio-dir-out  ;
+: smb-data-dir-out  ( -- )  d# 163 gpio-dir-out  ;
+: smb-data-dir-in  ( -- )  d# 163 gpio-dir-in  ;
+
+h# 3500 constant smb-clk-timeout-us
+\ Slave can flow control by holding CLK low temporarily
+: smb-wait-clk-hi  ( -- )
+   smb-clk-timeout-us 0  do
+      smb-clk@  if  smb-dly  unloop exit  then  1 us
+   loop
+   true abort" I2C clock stuck low"
+;
+: smb-data-hi-w  ( -- )  smb-data-hi  smb-wait-clk-hi  ;
+
+h# 3500 constant smb-data-timeout-us
+: smb-wait-data-hi  ( -- )
+   smb-data-timeout-us 0  do
+      smb-data@  if  unloop exit  then  1 us
+   loop
+   true abort" I2C data stuck low"
+;
+
+: smb-restart  ( -- )
+   smb-clk-hi  smb-data-lo  smb-clk-lo
+;
+
+: smb-start ( -- )  smb-clk-hi  smb-data-hi  smb-data-lo smb-clk-lo  ;
+: smb-stop  ( -- )  smb-clk-lo  smb-data-lo  smb-clk-hi  smb-data-hi  ;
+
+: smb-get-ack  ( -- )
+   smb-data-dir-in
+   smb-data-hi
+   smb-clk-hi smb-wait-clk-hi  
+   smb-data@  \ drop		\ SCCB generates an don't care bit
+   if  smb-stop  true abort" I2c NAK" then
+   smb-clk-lo
+\   smb-wait-data-hi
+   smb-data-dir-out
+;
+: smb-bit  ( flag -- )
+   if  smb-data-hi  else  smb-data-lo  then
+   smb-clk-hi smb-wait-clk-hi  smb-clk-lo
+;
+
+: smb-byte  ( b -- )
+   8 0  do                     ( b )
+      dup h# 80 and  smb-bit   ( b )
+      2*                       ( b' )
+   loop                        ( b )
+   drop                        ( )
+   smb-get-ack
+;
+: smb-byte-in  ( ack=0/nak=1 -- b )
+   smb-data-dir-in
+   0
+   8 0  do             ( n )
+      smb-clk-hi       ( n )
+      2*  smb-data@  if  1 or  then  ( n' )
+      smb-clk-lo
+   loop
+   smb-data-dir-out
+   swap smb-bit  smb-data-hi  \ Send ACK or NAK
+;
+
+0 value smb-slave
+: smb-addr  ( lowbit -- )  smb-slave or  smb-byte  ;
+
+: smb-byte!  ( byte reg# -- )
+   smb-start
+   0 smb-addr          ( byte reg# )
+   smb-byte            ( byte )
+   smb-byte            ( )
+   smb-stop
+;
+
+: smb-byte@  ( reg# -- byte )
+   smb-start
+   0 smb-addr          ( reg# )
+   smb-byte            ( )
+   smb-stop smb-start	\ SCCB bus needs a stop and a start for the second phase
+   1 smb-addr
+   1 smb-byte-in       ( byte )
+   smb-stop
+;
+
+: smb-word!  ( word reg# -- )
+   smb-start
+   0 smb-addr          ( word reg# )
+   smb-byte            ( word )
+   wbsplit swap smb-byte smb-byte  ( )
+   smb-stop
+;
+
+: smb-word@  ( reg# -- word )
+   smb-start
+   0 smb-addr          ( reg# )
+   smb-byte            ( )
+   smb-restart
+   1 smb-addr          ( )
+   0 smb-byte-in   1 smb-byte-in  bwjoin  ( word )
+   smb-stop
+;
+
+\ This can useful for clearing out DCON SMB internal state
+: smb-pulses  ( -- )
+   d# 32 0  do  smb-clk-lo smb-clk-hi  loop
+;
+
+: set-dcon-slave  ( -- )  h# 1a to smb-slave  ;
+: smb-init    ( -- )  set-dcon-slave  smb-on  smb-pulses  ;
+
+: dcon@  ( reg# -- word )  set-dcon-slave  smb-word@  ;
+: dcon!  ( word reg# -- )  set-dcon-slave  smb-word!  ;

Modified: cpu/arm/olpc/1.75/devices.fth
==============================================================================
--- cpu/arm/olpc/1.75/devices.fth	Mon Oct 25 14:42:14 2010	(r1991)
+++ cpu/arm/olpc/1.75/devices.fth	Fri Oct 29 18:17:26 2010	(r1992)
@@ -86,6 +86,17 @@
 
 \needs md5init  fload ${BP}/ofw/ppp/md5.fth                \ MD5 hash
 
+[ifdef] notyet
+fload ${BP}/dev/olpc/confirm.fth             \ Selftest interaction modalities
+fload ${BP}/cpu/x86/pc/olpc/mfgdata.fth      \ Manufacturing data
+fload ${BP}/cpu/x86/pc/olpc/mfgtree.fth      \ Manufacturing data in device tree
+fload ${BP}/cpu/x86/pc/olpc/kbdtype.fth      \ Export keyboard type
+
+fload ${BP}/dev/olpc/kb3700/battery.fth      \ Battery status reports
+[else]
+: find-tag  ( adr len -- false | value$ true )  2drop false  ;
+[then]
+
 fload ${BP}/dev/olpc/spiflash/flashif.fth   \ Generic FLASH interface
 
 fload ${BP}/dev/olpc/spiflash/spiif.fth    \ Generic low-level SPI bus access
@@ -103,14 +114,16 @@
 : ofw-fw-filename$  " disk:\boot\olpc.rom"  ;
 ' ofw-fw-filename$ to fw-filename$
 
-0 [if]
 0 0  " d420b000"  " /" begin-package
    " display" name
    fload ${BP}/cpu/arm/olpc/1.75/lcdcfg.fth
-\   fload ${BP}/cpu/arm/mmp2/dsi.fth
 
-   fload ${BP}/cpu/arm/mmp2/lcd.fth
+   fload ${BP}/cpu/arm/olpc/1.75/lcd.fth
+   fload ${BP}/cpu/arm/olpc/1.75/dconsmb.fth     \ SMB access to DCON chip - bitbanged
+   fload ${BP}/dev/olpc/dcon/mmp2dcon.fth        \ DCON control
+
    : display-on
+      init-xo-display  \ Turns on DCON
       init-lcd
       fb-pa  hdisp vdisp * >bytes  h# ff fill
    ;
@@ -137,12 +150,15 @@
    ' display-remove   is-remove
    ' display-selftest is-selftest
 end-package
+
 devalias screen /display
    
 devalias keyboard /keyboard
 
-fload ${BP}/ofw/termemu/cp881-16.fth
-[then]
+create 15x30pc  " ${BP}/ofw/termemu/15x30pc.psf" $file,
+' 15x30pc to romfont
+
+\ fload ${BP}/ofw/termemu/cp881-16.fth
 
 fload ${BP}/cpu/arm/olpc/1.75/sdhci.fth
 
@@ -151,6 +167,8 @@
 
 fload ${BP}/dev/olpc/kb3700/spicmd.fth
 
+devalias keyboard /ec-spi/keyboard
+
 0 0  " d4208000"  " /" begin-package  \ USB Host Controller
    h# 200 constant /regs
    my-address my-space /regs reg
@@ -165,15 +183,26 @@
    fload ${BP}/dev/usb2/hcd/ehci/loadpkg.fth
 end-package
    
-: usb-power-on  ( -- )  d# 82 gpio-set  ;  \ 1 instead of 82 for XO
+: usb-power-on  ( -- )  1 gpio-set  ;
+: unreset-usb-hub  ( -- )  d# 146 gpio-set  ;
 
-0 [if]
 fload ${BP}/cpu/arm/marvell/utmiphy.fth
-stand-init: Init USB Phy
+
+: start-usb  ( -- )
+   h# 9 h# d428285c l!  \ Enable clock to USB block
+   unreset-usb-hub
    init-usb-phy
 ;
+
+0 [if]
+stand-init: Init USB Phy
+\  usb-power-on   \ The EC now controls the USB power
+   start-usb
+;
 [then]
 
+fload ${BP}/dev/olpc/mmp2camera/loadpkg.fth
+
 \ LICENSE_BEGIN
 \ Copyright (c) 2010 FirmWorks
 \ 

Modified: cpu/arm/olpc/1.75/fw.bth
==============================================================================
--- cpu/arm/olpc/1.75/fw.bth	Mon Oct 25 14:42:14 2010	(r1991)
+++ cpu/arm/olpc/1.75/fw.bth	Fri Oct 29 18:17:26 2010	(r1992)
@@ -1,168 +1,17 @@
 purpose: Build OFW Forth dictionary for OLPC XO-1.75
 \ See license at end of file
 
-dictionary: ${BP}/cpu/arm/build/basefw.dic
+dictionary: ${BP}/cpu/arm/olpc/1.75/build/prefw.dic
 command: &armforth &dictionary &this
 build-now
 
-" fw.tag" r/w create-file drop  tag-file !
+\ " fw.tag" r/w create-file drop  tag-file !
 
 hex
 \ ' $report-name is include-hook
-' noop is include-hook
+\ ' noop is include-hook
 
-fload ${BP}/cpu/arm/olpc/1.75/config.fth
-
-: headerless ;  : headers  ;  : headerless0 ;
-
-' (quit) to quit
-
-: \Tags [compile] \  ; immediate
-: \NotTags [compile] \  ; immediate
-
-: RAMbase  ( -- adr )  fw-virt-base  ;
-: RAMtop  ( -- adr )  RAMbase /fw-ram +  ;
-
-def-load-base ' load-base set-config-int-default
-
-\ use-movable-vector-base  \ Marvell CPU core has a movable vector base
-
-true ' fcode-debug? set-config-int-default
-\ false  ' auto-boot?    set-config-int-default
-
-
-
-[ifdef] serial-console
-" com1" ' output-device set-config-string-default
-" com1" ' input-device set-config-string-default
-[then]
-
-
-fload ${BP}/cpu/arm/mmp2/rootnode.fth	\ Root node mapping - physical mode
-dev /
-   " olpc,XO-1.75" model
-   " Marvell,Armada 610" encode-string  " architecture" property
-\ The clock frequency of the root bus may be irrelevant, since the bus is internal to the SOC
-\    d# 1,000,000,000 " clock-frequency" integer-property
-device-end
-
-: (cpu-arch  ( -- adr len )
-   " architecture" ['] root-node  get-package-property  drop
-   get-encoded-string
-;
-' (cpu-arch to cpu-arch
-
-\ Memory management services
-[ifdef] virtual-mode
-fload ${BP}/ofw/core/clntmem1.fth	\ client services for memory
-[else]
-fload ${BP}/ofw/core/clntphy1.fth	\ client services for memory
-: >physical  ( va -- pa )
-   dup fw-virt-base - fw-virt-size u<  if   ( va )
-      fw-virt-base -  fw-pa +
-   then
-;
-[then]
-fload ${BP}/ofw/core/memlist.fth	\ Resource list common routines
-fload ${BP}/ofw/core/showlist.fth	\ Linked list display tool
-fload ${BP}/ofw/core/allocph1.fth	\ S Physical memory allocator
-fload ${BP}/ofw/core/availpm.fth	\ Available memory list
-
-fload ${BP}/cpu/arm/olpc/1.75/probemem.fth	\ Memory probing
-
-stand-init: Probing memory
-   " probe" memory-node @ $call-method
-;
-
-[ifdef] virtual-mode
-fload ${BP}/cpu/arm/loadvmem.fth	\ /mmu node
-stand-init: MMU
-   " /mmu" open-dev mmu-node !
-;
-fload ${BP}/ofw/core/initdict.fth	\ Dynamic dictionary allocation
-fload ${BP}/arch/arm/loadarea.fth	\ Allocate and map program load area
-[else]
-fload ${BP}/cpu/arm/mmp2/mmuon.fth
-[then]
-
-\ XXX should be elsewhere
-dev /client-services
-: chain  ( len args entry size virt -- )
-   release                                       ( len args entry )
-   h# 8000 alloc-mem h# 8000 +  (init-program)   ( len args )
-   to r1  to r2
-   go
-;
-device-end
-
-fload ${BP}/cpu/arm/crc32.fth		\ Assembly language Zip CRC calculation
-fload ${BP}/forth/lib/crc32.fth		\ High-level portion of CRC calculation
-
-[ifdef] resident-packages
-
-\needs unix-seconds>  fload ${BP}/ofw/fs/unixtime.fth	\ Unix time calculation
-support-package: ext2-file-system
-   fload ${BP}/ofw/fs/ext2fs/ext2fs.fth	\ Linux file system
-end-support-package
-
-[ifdef] jffs2-support
-\needs unix-seconds>  fload ${BP}/ofw/fs/unixtime.fth	\ Unix time calculation
-support-package: jffs2-file-system
-   fload ${BP}/ofw/fs/jffs2/jffs2.fth	\ Journaling flash file system 2
-end-support-package
-[then]
-
-support-package: zip-file-system
-   fload ${BP}/ofw/fs/zipfs.fth		\ Zip file system
-end-support-package
-[then]
-
-fload ${BP}/ofw/core/osfile.fth		\ For testing
-
-\ Load file format handlers
-
-: call32 ;
-
-fload ${BP}/ofw/core/allocsym.fth    \ Allocate memory for symbol table
-fload ${BP}/ofw/core/symcif.fth
-fload ${BP}/ofw/core/symdebug.fth
-: release-load-area  ( boundary-adr -- )  drop  ;
-
-[ifdef] use-elf
-fload ${BP}/ofw/elf/elf.fth
-fload ${BP}/ofw/elf/elfdebug.fth
-[ifdef] virtual-mode
-\ Depends on the assumption that physical memory is mapped 1:1 already
-: (elf-map-in) ( va size -- )  0 mem-claim  drop  ;
-[else]
-: (elf-map-in)  ( va size -- )  2drop  ;
-[then]
-' (elf-map-in) is elf-map-in
-[then]
-
-\ Reboot and re-entry code
-fload ${BP}/ofw/core/reboot.fth		\ Restart the client program
-fload ${BP}/ofw/core/reenter.fth	\ Various entries into Forth
-
-headerless
-[ifdef] virtual-mode
-: (initial-heap)  ( -- adr len )  sp0 @ ps-size -  dict-limit  tuck -  ;
-[else]
-   \ : (initial-heap)  ( -- adr len )  RAMtop heap-size  ;
-: (initial-heap)  ( -- adr len )  limit heap-size  ;
-[then]
-' (initial-heap) is initial-heap
-headers
-
-" /openprom" find-device
-   " FirmWorks,3.0" encode-string " model" property
-device-end
-
-[ifdef] virtual-mode
-fload ${BP}/cpu/arm/mmusetup.fth	\ Initial values for MMU lists
-[then]
-
-: background-rgb  ( -- r g b )  h# ff h# ff h# ff  ;
+: confirm-selftest?  ( -- flag )  true  ;  \ XXX implement me
 
 fload ${BP}/cpu/arm/olpc/1.75/devices.fth
 
@@ -175,17 +24,7 @@
 warning on
 [then]
 
-true ' local-mac-address? set-config-int-default
-[ifdef] resident-packages
-support-package: nfs
-   fload ${BP}/ofw/fs/nfs/loadpkg.fth
-end-support-package
-
-[then]
-devalias nfs net//obp-tftp:last//nfs
-
 fload ${BP}/cpu/arm/linux.fth
-h# 20.0000 to linux-params  \ The Jasper Linux kernel fails unless the params are between 0x20.0000 and 0x20.4000
 d# 9999 to arm-linux-machine-type  \ Marvell Jasper
 
 \ Add a tag describing the linear frame buffer
@@ -207,7 +46,7 @@
    0     tag-b,       \ Rsvd size
    d# 24 tag-b,       \ Rsvd position
 ;
-\ ' mmp-fb-tag, to fb-tag,
+' mmp-fb-tag, to fb-tag,
 
 \ fload ${BP}/cpu/arm/mmp2/usb.fth
 
@@ -253,24 +92,6 @@
 ;
 ' (.firmware) to .firmware
 
-fload ${BP}/ofw/gui/bmptools.fth
-fload ${BP}/dev/null.fth
-fload ${BP}/ofw/core/bailout.fth
-
-\ GUI
-false value gui-safeboot?
-
-: 2tuck  ( d1 d2 -- d2 d1 d2 )  2swap 2over  ;
-: user-ok  "ok"  ;  \ This is supposed to check for authorization
-true value user-mode?
-
-fload ${BP}/ofw/gui/loadmenu.fth
-\ fload ${BP}/ofw/gui/insticon.fth
-
-\ Uninstall the diag menu from the general user interface vector
-\ so exiting from emacs doesn't invoke the diag menu.
-' quit to user-interface
-
 : screen-#lines  ( -- n )
    screen-ih 0=  if  default-#lines exit  then
    screen-ih  package( #lines )package
@@ -327,51 +148,6 @@
 
 fload ${BP}/cpu/arm/saverom.fth  \ Save the dictionary for standalone startup
 
-fload ${BP}/forth/lib/selstr.fth
-
-fload ${BP}/ofw/inet/loadtcp.fth
-
-support-package: http
-   fload ${BP}/ofw/inet/http.fth	\ HTTP client
-end-support-package
-
-[ifdef] notyet
-fload ${BP}/cpu/x86/pc/olpc/memtest.fth
-[then]
-
-[ifdef] notyet
-fload ${BP}/ofw/wifi/wifi-cfg.fth
-support-package: supplicant
-fload ${BP}/ofw/wifi/loadpkg.fth
-end-support-package
-
-: ofw-ssids  ( -- $ )  " OFWSSID"  ;
-' ofw-ssids to default-ssids
-[then]
-
-fload ${BP}/ofw/inet/sntp.fth
-: olpc-ntp-servers  ( -- )
-   " DHCP time 172.18.0.1 0.pool.ntp.org 1.pool.ntp.org 2.pool.ntp.org"
-;
-' olpc-ntp-servers to ntp-servers
-: ntp-time&date  ( -- s m h d m y )
-   ntp-timestamp  abort" Can't contact NTP server"
-   ntp>time&date
-;
-: .clock  ( -- )
-   time&date .date space .time  ."  UTC" cr
-;
-: ntp-set-clock  ( -- )
-   ntp-time&date  " set-time"  clock-node @ $call-method
-   .clock
-;
-
-[ifdef] use-ppp
-fload ${BP}/ofw/ppp/loadppp.fth
-[then]
-
-" dhcp" ' ip-address  set-config-string-default
-
 [ifdef] notyet
 : c1-idle  ( -- )  interrupts-enabled?  if  halt  then  ;
 ' c1-idle to stdin-idle
@@ -386,13 +162,19 @@
 
    " //null" open-dev to null-ih  \ For text-off state
 ;
-
+: keyboard-off  ( -- )
+   keyboard-ih  if
+      keyboard-ih remove-input
+      keyboard-ih close-dev
+      0 to keyboard-ih
+   then
+;
 : interpreter-init  ( -- )
    hex
    warning on
    only forth also definitions
 
-\   install-alarm
+   install-alarm
 
    page-mode
    #line off
@@ -478,14 +260,7 @@
    quit
 ;
 
-\ This helps with TeraTerm, which sends ESC-O as the arrow key prefix
-also hidden also keys-forth definitions
-warning @  warning off
-: esc-o  key lastchar !  [""] esc-[ do-command  ;
-warning !
-previous previous definitions
-
-tag-file @ fclose  tag-file off
+\ tag-file @ fclose  tag-file off
 
 .( --- Saving fw.dic ...)
 " fw.dic" $save-forth cr

Added: cpu/arm/olpc/1.75/lcd.fth
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ cpu/arm/olpc/1.75/lcd.fth	Fri Oct 29 18:17:26 2010	(r1992)
@@ -0,0 +1,30 @@
+
+: lcd@  ( offset -- l )  lcd-pa + l@  ;
+: lcd!  ( l offset -- )  lcd-pa + l!  ;
+
+: init-lcd  ( -- )
+   \ Turn on clocks
+   h# 08 pmua-disp-clk-sel + h# d428284c l!
+   h# 09 pmua-disp-clk-sel + h# d428284c l!
+   h# 19 pmua-disp-clk-sel + h# d428284c l!
+   h# 1b pmua-disp-clk-sel + h# d428284c l!
+
+   0      h# 190 lcd!   \ Disable LCD DMA controller
+   fb-pa               h# f4 lcd!  \ Frame buffer area 0
+   0                   h# f8 lcd!  \ Frame buffer area 1
+   hdisp bytes/pixel * h# fc lcd!  \ Pitch in bytes
+
+   hdisp vdisp wljoin  dup h# 104 lcd!  dup h# 108 lcd!  h# 118 lcd!  \ size, size after zoom, disp
+
+   htotal >chunks  vtotal wljoin  h# 114 lcd!  \ SPUT_V_H_TOTAL
+
+   htotal >chunks  hdisp -  hbp >chunks -  6 -  ( low )
+   hbp >chunks  wljoin  h# 11c lcd!
+   
+   vfp vbp wljoin  h# 120 lcd!
+   h# 2000FF00 h# 194 lcd!  \ DMA CTRL 1
+   h# 2000000d h# 1b8 lcd!  \ Dumb panel controller - 18 bit RGB666 on LDD[17:0]
+   h# 01330133 h# 13c lcd!  \ Panel VSYNC Pulse Pixel Edge Control
+   clkdiv      h# 1a8 lcd!  \ Clock divider
+   h# 00021100 h# 190 lcd!  \ DMA CTRL 0 - enable DMA, 24 bpp mode
+;

Modified: cpu/arm/olpc/1.75/lcdcfg.fth
==============================================================================
--- cpu/arm/olpc/1.75/lcdcfg.fth	Mon Oct 25 14:42:14 2010	(r1991)
+++ cpu/arm/olpc/1.75/lcdcfg.fth	Fri Oct 29 18:17:26 2010	(r1992)
@@ -1,17 +1,20 @@
-d#    4 value hsync
-d# 1200 value hdisp
-d# 1456 value htotal  .( HTOTAL ???) cr
-d#  212 value hbp
+h# 40001102 value clkdiv  \ Display Clock 1 / 2 -> 56.93 MHz
+h# 00000700 value pmua-disp-clk-sel  \ PLL1 / 7 -> 113.86 MHz 
 
-d#    4 value vsync
-d#  800 value vdisp
-d#  845 value vtotal  .( VTOTAL ???) cr
-d#   31 value vbp
+d#    8 value hsync  \ Sync width
+d# 1200 value hdisp  \ Display width
+d# 1256 value htotal \ Display + FP + Sync + BP
+d#   24 value hbp    \ Back porch
 
-: hfp  ( -- n )  htotal hdisp -  hsync -  hbp -  ;
-: vfp  ( -- n )  vtotal vdisp -  vsync -  vbp -  ;
+d#    3 value vsync  \ Sync width
+d#  900 value vdisp  \ Display width
+d#  912 value vtotal \ Display + FP + Sync + BP
+d#    5 value vbp    \ Back porch
 
-2 constant #lanes
+: hfp  ( -- n )  htotal hdisp -  hsync -  hbp -  ;  \ 24
+: vfp  ( -- n )  vtotal vdisp -  vsync -  vbp -  ;  \ 4
+
+3 constant #lanes
 3 constant bytes/pixel
 d# 24 constant bpp
 

Added: cpu/arm/olpc/1.75/prefw.bth
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ cpu/arm/olpc/1.75/prefw.bth	Fri Oct 29 18:17:26 2010	(r1992)
@@ -0,0 +1,249 @@
+purpose: Build OFW Forth dictionary for OLPC XO-1.75
+\ See license at end of file
+
+dictionary: ${BP}/cpu/arm/build/basefw.dic
+command: &armforth &dictionary &this
+build-now
+
+" prefw.tag" r/w create-file drop  tag-file !
+
+hex
+\ ' $report-name is include-hook
+' noop is include-hook
+
+fload ${BP}/cpu/arm/olpc/1.75/config.fth
+
+: headerless ;  : headers  ;  : headerless0 ;
+
+' (quit) to quit
+
+: \Tags [compile] \  ; immediate
+: \NotTags [compile] \  ; immediate
+
+: RAMbase  ( -- adr )  fw-virt-base  ;
+: RAMtop  ( -- adr )  RAMbase /fw-ram +  ;
+
+def-load-base ' load-base set-config-int-default
+
+\ use-movable-vector-base  \ Marvell CPU core has a movable vector base
+
+true ' fcode-debug? set-config-int-default
+\ false  ' auto-boot?    set-config-int-default
+
+
+
+[ifdef] serial-console
+" com1" ' output-device set-config-string-default
+" com1" ' input-device set-config-string-default
+[then]
+
+
+fload ${BP}/cpu/arm/mmp2/rootnode.fth	\ Root node mapping - physical mode
+dev /
+   " olpc,XO-1.75" model
+   " Marvell,Armada 610" encode-string  " architecture" property
+\ The clock frequency of the root bus may be irrelevant, since the bus is internal to the SOC
+\    d# 1,000,000,000 " clock-frequency" integer-property
+device-end
+
+: (cpu-arch  ( -- adr len )
+   " architecture" ['] root-node  get-package-property  drop
+   get-encoded-string
+;
+' (cpu-arch to cpu-arch
+
+\ Memory management services
+[ifdef] virtual-mode
+fload ${BP}/ofw/core/clntmem1.fth	\ client services for memory
+[else]
+fload ${BP}/ofw/core/clntphy1.fth	\ client services for memory
+: >physical  ( va -- pa )
+   dup fw-virt-base - fw-virt-size u<  if   ( va )
+      fw-virt-base -  fw-pa +
+   then
+;
+[then]
+fload ${BP}/ofw/core/memlist.fth	\ Resource list common routines
+fload ${BP}/ofw/core/showlist.fth	\ Linked list display tool
+fload ${BP}/ofw/core/allocph1.fth	\ S Physical memory allocator
+fload ${BP}/ofw/core/availpm.fth	\ Available memory list
+
+fload ${BP}/cpu/arm/olpc/1.75/probemem.fth	\ Memory probing
+
+stand-init: Probing memory
+   " probe" memory-node @ $call-method
+;
+
+[ifdef] virtual-mode
+fload ${BP}/cpu/arm/loadvmem.fth	\ /mmu node
+stand-init: MMU
+   " /mmu" open-dev mmu-node !
+;
+fload ${BP}/ofw/core/initdict.fth	\ Dynamic dictionary allocation
+fload ${BP}/arch/arm/loadarea.fth	\ Allocate and map program load area
+[else]
+fload ${BP}/cpu/arm/mmp2/mmuon.fth
+[then]
+
+\ XXX should be elsewhere
+dev /client-services
+: chain  ( len args entry size virt -- )
+   release                                       ( len args entry )
+   h# 8000 alloc-mem h# 8000 +  (init-program)   ( len args )
+   to r1  to r2
+   go
+;
+device-end
+
+fload ${BP}/cpu/arm/crc32.fth		\ Assembly language Zip CRC calculation
+fload ${BP}/forth/lib/crc32.fth		\ High-level portion of CRC calculation
+
+[ifdef] resident-packages
+
+\needs unix-seconds>  fload ${BP}/ofw/fs/unixtime.fth	\ Unix time calculation
+support-package: ext2-file-system
+   fload ${BP}/ofw/fs/ext2fs/ext2fs.fth	\ Linux file system
+end-support-package
+
+[ifdef] jffs2-support
+\needs unix-seconds>  fload ${BP}/ofw/fs/unixtime.fth	\ Unix time calculation
+support-package: jffs2-file-system
+   fload ${BP}/ofw/fs/jffs2/jffs2.fth	\ Journaling flash file system 2
+end-support-package
+[then]
+
+support-package: zip-file-system
+   fload ${BP}/ofw/fs/zipfs.fth		\ Zip file system
+end-support-package
+[then]
+
+fload ${BP}/ofw/core/osfile.fth		\ For testing
+
+\ Load file format handlers
+
+: call32 ;
+
+fload ${BP}/ofw/core/allocsym.fth    \ Allocate memory for symbol table
+fload ${BP}/ofw/core/symcif.fth
+fload ${BP}/ofw/core/symdebug.fth
+: release-load-area  ( boundary-adr -- )  drop  ;
+
+[ifdef] use-elf
+fload ${BP}/ofw/elf/elf.fth
+fload ${BP}/ofw/elf/elfdebug.fth
+[ifdef] virtual-mode
+\ Depends on the assumption that physical memory is mapped 1:1 already
+: (elf-map-in) ( va size -- )  0 mem-claim  drop  ;
+[else]
+: (elf-map-in)  ( va size -- )  2drop  ;
+[then]
+' (elf-map-in) is elf-map-in
+[then]
+
+\ Reboot and re-entry code
+fload ${BP}/ofw/core/reboot.fth		\ Restart the client program
+fload ${BP}/ofw/core/reenter.fth	\ Various entries into Forth
+
+headerless
+[ifdef] virtual-mode
+: (initial-heap)  ( -- adr len )  sp0 @ ps-size -  dict-limit  tuck -  ;
+[else]
+   \ : (initial-heap)  ( -- adr len )  RAMtop heap-size  ;
+: (initial-heap)  ( -- adr len )  limit heap-size  ;
+[then]
+' (initial-heap) is initial-heap
+headers
+
+" /openprom" find-device
+   " FirmWorks,3.0" encode-string " model" property
+device-end
+
+[ifdef] virtual-mode
+fload ${BP}/cpu/arm/mmusetup.fth	\ Initial values for MMU lists
+[then]
+
+: background-rgb  ( -- r g b )  h# ff h# ff h# ff  ;
+
+fload ${BP}/forth/lib/selstr.fth
+
+fload ${BP}/ofw/inet/loadtcp.fth
+
+support-package: http
+   fload ${BP}/ofw/inet/http.fth	\ HTTP client
+end-support-package
+
+[ifdef] notyet
+fload ${BP}/cpu/x86/pc/olpc/memtest.fth
+[then]
+
+[ifdef] notyet
+fload ${BP}/ofw/wifi/wifi-cfg.fth
+support-package: supplicant
+fload ${BP}/ofw/wifi/loadpkg.fth
+end-support-package
+
+: ofw-ssids  ( -- $ )  " OFWSSID"  ;
+' ofw-ssids to default-ssids
+[then]
+
+fload ${BP}/ofw/inet/sntp.fth
+: olpc-ntp-servers  ( -- )
+   " DHCP time 172.18.0.1 0.pool.ntp.org 1.pool.ntp.org 2.pool.ntp.org"
+;
+' olpc-ntp-servers to ntp-servers
+: ntp-time&date  ( -- s m h d m y )
+   ntp-timestamp  abort" Can't contact NTP server"
+   ntp>time&date
+;
+: .clock  ( -- )
+   time&date .date space .time  ."  UTC" cr
+;
+: ntp-set-clock  ( -- )
+   ntp-time&date  " set-time"  clock-node @ $call-method
+   .clock
+;
+
+[ifdef] use-ppp
+fload ${BP}/ofw/ppp/loadppp.fth
+[then]
+
+" dhcp" ' ip-address  set-config-string-default
+
+fload ${BP}/ofw/gui/bmptools.fth
+fload ${BP}/dev/null.fth
+fload ${BP}/ofw/core/bailout.fth
+
+true ' local-mac-address? set-config-int-default
+[ifdef] resident-packages
+support-package: nfs
+   fload ${BP}/ofw/fs/nfs/loadpkg.fth
+end-support-package
+
+[then]
+devalias nfs net//obp-tftp:last//nfs
+
+\ This helps with TeraTerm, which sends ESC-O as the arrow key prefix
+also hidden also keys-forth definitions
+warning @  warning off
+: esc-o  key lastchar !  [""] esc-[ do-command  ;
+warning !
+previous previous definitions
+
+\ GUI
+false value gui-safeboot?
+
+: 2tuck  ( d1 d2 -- d2 d1 d2 )  2swap 2over  ;
+: user-ok  "ok"  ;  \ This is supposed to check for authorization
+true value user-mode?
+
+fload ${BP}/ofw/gui/loadmenu.fth
+\ fload ${BP}/ofw/gui/insticon.fth
+
+\ Uninstall the diag menu from the general user interface vector
+\ so exiting from emacs doesn't invoke the diag menu.
+' quit to user-interface
+
+tag-file @ fclose  tag-file off
+
+.( --- Saving prefw.dic ...)
+" prefw.dic" $save-forth cr

Modified: cpu/arm/olpc/1.75/probemem.fth
==============================================================================
--- cpu/arm/olpc/1.75/probemem.fth	Mon Oct 25 14:42:14 2010	(r1991)
+++ cpu/arm/olpc/1.75/probemem.fth	Fri Oct 29 18:17:26 2010	(r1992)
@@ -24,6 +24,7 @@
    2drop  " reg" property
 
    \ Claim the memory used by OFW
+   fw-pa  /fw-ram    0 claim  drop
 \   high h# 10.0000 -  h# 10.0000    0 claim  drop
 ;
 

Modified: cpu/arm/olpc/1.75/sp.bth
==============================================================================
--- cpu/arm/olpc/1.75/sp.bth	Mon Oct 25 14:42:14 2010	(r1991)
+++ cpu/arm/olpc/1.75/sp.bth	Fri Oct 29 18:17:26 2010	(r1992)
@@ -355,6 +355,60 @@
 	setreg d4282860 b8    \ NAND FLASH PLL1/8 + ECC PLL1/4, AXI clock enabled
 	setreg d4282860 bf    \ plus ECC&controller&AXI released from reset
 
+   \ Read the SPI FLASH into DRAM
+   
+   setreg d4035000 0010000f  \ 32-bit, not enabled
+   setreg d4035000 0010008f  \ 32-bit, enabled
+
+   setreg d401901c 00004000  \ Drive GPIO46 (SPI CS#) high
+
+   setreg d4019058 00004000  \ Set GPIO46 to output direction
+
+   setreg d401e10c 000000c0  \ GPIO mode for FRM
+   delay 10              
+   setreg d4019028 00004000  \ Drive GPIO46 (SPI CS#) low
+
+   set r0,0xd4035000   \ Register base address
+   set r1,0x00100000   \ Remaining length
+   set r2,0x00000000   \ DRAM (and FLASH) address
+
+   orr   r3,r2,#0x03000000  \ SPI FLASH command - read (3) with address in low bytes
+   str   r3,[r0,#0x10]      \ Put command in FIFO
+
+   mov   r3,#0              \ Value to write (could be anything)
+   str   r3,[r0,#0x10]      \ Put dummy value in FIFO to force read
+
+   begin
+      ldr  r5,[r0,#0x08]    \ Read status
+      ands r5,#8            \ Test RxFIFO not empty bit
+   0<> until
+   ldr   r5,[r0,#0x10]      \ Read (and discard) first FIFO entry
+
+   begin
+      str   r3,[r0,#0x10]    \ Put dummy value in FIFO to keep the read going
+
+      begin
+         ldr  r5,[r0,#0x08]  \ Read status
+         ands r5,#8          \ Test RxFIFO not empty bit
+      0<> until
+      ldr   r5,[r0,#0x10]    \ Read FIFO entry
+
+      rev r5,r5              \ byte reverse because the FIFO is big-endian
+      str  r5,[r2],#4        \ Write out
+
+      cmp  r2,#0x100000
+   = until
+
+   \ Discard the extra entry just for cleanliness
+   begin
+      ldr  r5,[r0,#0x08]    \ Read status
+      ands r5,#8            \ Test RxFIFO not empty bit
+   0<> until
+   ldr   r5,[r0,#0x10]      \ Read (and discard) first FIFO entry
+
+   setreg d401901c 00004000  \ Drive GPIO46 (SPI CS#) high
+
+
 \ Finally, release the reset for the main CPU
 
    set r0,0xd4050020

Added: dev/olpc/dcon/mmp2dcon.fth
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ dev/olpc/dcon/mmp2dcon.fth	Fri Oct 29 18:17:26 2010	(r1992)
@@ -0,0 +1,212 @@
+\ See license at end of file
+\ " dcon" device-name
+
+\ DCON internal registers, accessed via I2C
+\ 0 constant DCON_ID
+\ 1 constant DCON_MODE
+\ 2 constant DCON_HRES
+\ 3 constant DCON_HTOTAL
+\ 4 constant DCON_HSYNC_WIDTH
+\ 5 constant DCON_VRES
+\ 6 constant DCON_VTOTAL
+\ 7 constant DCON_VSYNC_WIDTH
+\ 8 constant DCON_TIMEOUT
+\ 9 constant DCON_SCAN_INT
+\ d# 10 constant DCON_BRIGHT
+
+\ Mode register bits
+\ h#    1 constant DM_PASSTHRU
+\ h#    2 constant DM_SLEEP
+\ h#    4 constant DM_SLEEP_AUTO
+\ h#    8 constant DM_BL_ENABLE
+\ h#   10 constant DM_BLANK
+\ h#   20 constant DM_CSWIZZLE
+\ h#   40 constant DM_COL_AA
+\ h#   80 constant DM_MONO_LUMA
+\ h#  100 constant DM_SCAN_INT
+\ h#  200 constant DM_CLOCKDIV
+\ h# 4000 constant DM_DEBUG
+\ h# 8000 constant DM_SELFTEST
+
+: dcon-load  ( -- )  d# 151 gpio-set  ;
+: dcon-unload  ( -- )  d# 151 gpio-clr  ;
+\ : dcon-blnk?  ( -- flag )  ;  \ Not hooked up
+: dcon-stat@  ( -- n )  h# d4019100 l@ 4 rshift 3 and  ;
+: dcon-irq?  ( -- flag )  d# 124 gpio-pin@  0=  ;
+
+\ DCONSTAT values:  0 SCANINT  1 SCANINT_DCON  2 DISPLAYLOAD  3 MISSED
+
+1 value vga? \ VGA
+0 value color? \ COLOUR
+
+\ : gxfb!  ( l offset -- )  gxfb-dc-regs +  rl!  ;  \ Probably should be IO mapped
+
+d# 905 value resumeline  \ Configurable; should be set from args
+
+: wait-output  ( -- )  d# 40 ms  ;
+
+: mark-time  ( -- start-time )  get-msecs  ;
+: delta-ms  ( start-time -- elapsed-ms )  mark-time  swap -   ;
+: wait-dcon-mode  ( -- retry? )
+   mark-time                            ( start-time )
+   begin                                ( start-time )
+      dcon-irq?  if                     ( start-time )
+         dcon-stat@  2 =  if  \ DCONSTAT=10  ( start-time )
+            \ Sometimes the DCON ack's the UNLOAD command sooner than it
+            \ should.  When that happens, it doesn't really capture the
+            \ new frame data.  The workaround is to detect the case and
+            \ retry the sequence.
+            delta-ms  d# 20 <           ( retry? )
+            exit   
+         then                           ( start-time )
+      then                              ( start-time )
+      dup delta-ms  d# 100 >            ( start-time reached? )    \ 100 ms timeout
+   until                                ( start-time )
+   drop
+   ." Timeout entering DCON mode" cr
+   \ We say false here because we don't want to retry; it probably won't succeed
+   false
+;
+
+: set-source ( vga? -- )  \ true to unfreeze display, false to freeze it
+   dup vga? =  if  drop exit  then  ( source )
+   dup to vga?                      ( source )
+   if
+\      unblank-display
+      d# 50 ms
+      wait-output
+      dcon-load  \ Put the DCON in VGA-refreshed mode
+      d# 25 ms   \ Ensure that that DCON sees the DCONLOAD high
+\      display-on
+   else
+      begin                             ( )
+         dcon-unload  \ Put the DCON in self-refresh mode
+         lock[ wait-dcon-mode ]unlock   ( retry? )
+\        display-off                    ( retry? )
+      while                             ( )
+         \ We got a false ack from the DCON so start over from LOAD state
+         dcon-load  d# 25 ms            ( )
+      repeat                            ( )
+   then
+;
+
+\ gx_configure_tft(info);
+
+: try-dcon!  ( w reg# -- )
+   ['] dcon!  catch  if  2drop  smb-stop 1 ms  smb-off  1 ms  smb-on  then
+;
+
+: mode@    ( -- mode )    1 dcon@  ;
+: mode!    ( mode -- )    1 dcon!  ;
+: hres!    ( hres -- )    2 dcon!  ;  \ def: h#  458 d# 1200
+: htotal!  ( htotal -- )  3 dcon!  ;  \ def: h#  4e8 d# 1256
+: hsync!   ( sync -- )    4 dcon!  ;  \ def: h# 1808 d# 24,8
+: vres!    ( vres -- )    5 dcon!  ;  \ def: h#  340 d# 900
+: vtotal!  ( htotal -- )  6 dcon!  ;  \ def: h#  390 d# 912
+: vsync!   ( sync -- )    7 dcon!  ;  \ def: h#  403 d# 4,3
+: timeout! ( to -- )      8 dcon!  ;  \ def: h# ffff
+: scanint! ( si -- )      9 dcon!  ;  \ def: h# 0000
+: bright!  ( level -- ) d# 10 dcon! ; \ def: h# xxxF
+: bright@  ( -- level ) d# 10 dcon@ ;
+: brighter  ( -- )  bright@ 1+  h# f min  bright!  ;
+: dimmer    ( -- )  bright@ 1-  0 max  bright!  ;
+
+: backlight-off  ( -- )  mode@  8 invert and  mode!  ;
+: backlight-on   ( -- )  mode@  8 or  mode!  ;
+
+\ Color swizzle, AA, no passthrough, backlight
+: set-color ( color? -- )
+   dup to color?
+   if  h# 69  else  h# 89  then  mode!
+;
+
+\ Setup so it can be called by execute-device-method
+: dcon-off  ( -- )  smb-init  h# 12 ['] mode!  catch  if  drop  then  ;
+
+: dcon2?  ( -- flag )
+   0 ['] dcon@ catch  if  ( x )
+      drop   smb-init     ( )
+      0 ['] dcon@ catch  if  drop false exit  then
+   then
+   h# dc02 =
+;
+
+: dcon-setup  ( -- )
+   0 dcon@ drop  0 dcon@ drop
+
+[ifdef] notdef
+   d# 1200 2 dcon!  \ HResolution
+   d# 1240 3 dcon!  \ HTotal
+   h# 0608 4 dcon!  \ HSyncstart (6+900=906), HSyncwidth (8)
+   d#  900 5 dcon!  \ VResolution
+   d#  912 6 dcon!  \ VTotal
+   h# 0502 7 dcon!  \ VSyncstart (5+900=905), VSyncwidth (2)
+[then]
+   
+   \ Switch to OLPC mode
+   h# c040  h# 3a dcon!   \ SDRAM Setup/Hold time.  Default of e040 fails
+   h# 0000  h# 41 dcon!   \ Himax suggested this sequence (0 then 0101)
+
+   h# 0101  h# 41 dcon!
+   h# 0101  h# 42 dcon!
+
+   h# 12 mode!
+;
+: dcon-enable  ( -- )
+   dcon-setup
+   true set-color
+   h# f bright!
+;
+
+: video-save
+   0 set-source  \ Freeze image
+\  olpc-lcd-off
+;
+
+: video-restore
+   smb-init
+\  olpc-lcd-mode
+
+\   gp-setup
+   1 set-source  \ Unfreeze image
+;
+
+0 value dcon-found?
+
+: maybe-set-cmos  ( -- )  ;
+
+: init-xo-display  ( -- )
+   smb-init
+
+\   olpc-lcd-mode
+
+   dcon-load
+   dcon-enable  ( maybe-set-cmos )
+   \ dcon-enable leaves mode set to 69 - 40:antialias, 20:swizzle, 8:backlight on, 1:passthru off
+;
+
+\ ' init-xo-display to init-display
+
+\ LICENSE_BEGIN
+\ Copyright (c) 2010 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/kb3700/spicmd.fth
==============================================================================
--- dev/olpc/kb3700/spicmd.fth	Mon Oct 25 14:42:14 2010	(r1991)
+++ dev/olpc/kb3700/spicmd.fth	Fri Oct 29 18:17:26 2010	(r1992)
@@ -35,12 +35,31 @@
 1 " #address-cells"  integer-property
 0 " #size-cells"     integer-property
 
-3 /n* buffer: port-data
-: init-queue  ( -- )  port-data  3 na+  bounds  ?do  -1 i !  /n +loop  ;
-: enque  ( data port# -- )
+: encode-unit  ( phys -- adr len )  push-hex  (u.)  pop-base  ;
+: decode-unit  ( adr len -- phys )  push-hex  $number  if  0  then  pop-base  ;
+
+d# 155 constant cmd-gpio#
+d# 125 constant ack-gpio#
+
+\ Channel#(port#) Meaning
+\ 0              Invalid
+\ 1              Switch to Command Mode
+\ 2              Command response
+\ 3              Keyboard
+\ 4              Touchpad
+\ 5              Event
+\ 6              EC Debug
+
+5 constant #ports
+#ports /n* buffer: port-data
+: init-queue  ( -- )  port-data  #ports /n*  bounds  ?do  -1 i !  /n +loop  ;
+: enque  ( data channel# -- )
+   2-                      ( data queue# )
+   dup #ports >=  if  2drop exit  then
    port-data swap na+ !    ( data adr )
 ;
-: deque?  ( port# -- false | data true )
+: deque?  ( channel# -- false | data true )
+   2-                      ( queue# )
    port-data swap na+      ( adr )
    dup @                   ( adr data )
    dup -1 =  if            ( adr data )
@@ -70,54 +89,49 @@
    enable
 ;
 
-: ssp1-clk-on  7 h# d4015050 l!   3 h# d4015050 l!  ;
-: ssp2-clk-on  7 h# d4015054 l!   3 h# d4015052 l!  ;
+\ : ssp1-clk-on  7 h# d4015050 l!   3 h# d4015050 l!  ;
+\ : ssp2-clk-on  7 h# d4015054 l!   3 h# d4015052 l!  ;
 : ssp3-clk-on  7 h# d4015058 l!   3 h# d4015058 l!  ;
-: ssp4-clk-on  7 h# d401505c l!   3 h# d401505c l!  ;
+\ : ssp4-clk-on  7 h# d401505c l!   3 h# d401505c l!  ;
 
-: wb  ( byte -- )  ssp-ssdr rl!  ;
-: rb  ( -- byte )  ssp-ssdr rl@ .  ;
+: wb  ( byte -- )  ssp-ssdr rl!  ;  \ Debugging tool
+: rb  ( -- byte )  ssp-ssdr rl@ .  ;  \ Debugging tool
 
 \ Wait until the CSS (Clock Synchronization Status) bit is 0
 : wait-clk-sync  ( -- )
    begin  ssp-sssr rl@ h# 400.0000 and  0=  until
 ;
 
-\ Choose alternate function 4 (SSP3) for the pins we use
-: select-ssp3-pins
-   h# c4 h# d401e170 rl!  \ GPIO74
-   h# c4 h# d401e174 rl!  \ GPIO75
-   h# c4 h# d401e178 rl!  \ GPIO76
-   h# c4 h# d401e17c rl!  \ GPIO77
-;
 : init-ssp-in-slave-mode  ( -- )
-   select-ssp3-pins
    ssp3-clk-on
    h# 07 ssp-sscr0 rl!   \ 8-bit data, SPI normal mode
-   h# 1380.0010 ssp-sscr1 rl!  \ SCFR=1, slave mode, Rx w/o Tx, early phase
+   h# 1300.0010 ssp-sscr1 rl!  \ SCFR=1, slave mode, early phase
    \ The enable bit must be set last, after all configuration is done
    h# 87 ssp-sscr0 rl!   \ Enable, 8-bit data, SPI normal mode
    wait-clk-sync
 ;
-: set-ssp-receive-w/o-transmit  ( -- )
-   ssp-sscr1 rl@  h# 0080.0000 or  ssp-sscr1 rl!
-;
-: clr-ssp-receive-w/o-transmit  ( -- )
-   ssp-sscr1 rl@  h# 0080.0000 invert and  ssp-sscr1 rl!
-;
-0 value ssp-rx-threshold
+2 value ssp-rx-threshold
 : set-ssp-fifo-threshold  ( n -- )  to ssp-rx-threshold  ;
-\ tx fifo trigger threshold?
 
 : .ssr  ssp-sssr rl@  .  ;
-: ssp-ready?  ( -- flag )
-   ssp-sssr rl@ d# 12 rshift h# f and  ssp-rx-threshold  =
+: rxavail  ( -- n )
+   ssp-sssr rl@  dup 8 and  if   ( val )
+      d# 12 rshift h# f and  1+
+   else
+      drop 0
+   then
+;
+: prime-fifo  ( -- )
+   ssp-rx-threshold  0  ?do  0 ssp-ssdr l!  loop
 ;
+: rxflush  ( -- )
+   begin  ssp-sssr rl@  8 and  while  ssp-ssdr l@ drop  repeat
+;
+: ssp-ready?  ( -- flag )  rxavail  ssp-rx-threshold  >=  ;
 
+false value debug?
 \ Set the direction on the ACK and CMD GPIOs
-d# 151 constant cmd-gpio#
-d# 125 constant ack-gpio#
-: init-ec-spi-gpios  ( -- )
+: init-gpios  ( -- )
    cmd-gpio# gpio-dir-out
    ack-gpio# gpio-dir-out
 ;
@@ -125,21 +139,18 @@
 : set-cmd  ( -- )  cmd-gpio# gpio-set  ;
 : clr-ack  ( -- )  ack-gpio# gpio-clr  ;
 : set-ack  ( -- )  ack-gpio# gpio-set  ;
-: fast-ack  ( -- )  set-ack clr-ack  ;
+: fast-ack  ( -- )  set-ack clr-ack  debug?  if  ." ACK " cr  then  ;
 : slow-ack  ( -- )  d# 10 ms  set-ack d# 10 ms  clr-ack  ;
-defer pulse-ack  ' slow-ack to pulse-ack  \ FIXME !!!
-
-0 value ec-spi-cmd-done  \ 0 - still waiting, 1 - successful send, 2 - timeout
+defer pulse-ack  ' fast-ack to pulse-ack
 
-6 buffer: ec-cmdbuf
-d# 16 buffer: ec-respbuf
-: expected-response-length  ( -- n )  ec-cmdbuf 1+ c@ h# f and  ;
-
-: write-cmd-to-ssp-fifo  ( -- )
-   6 0  do
-      ec-cmdbuf i + c@  ssp-ssdr rl!
-   loop
-;
+0 value cmdbuf
+0 value cmdlen
+0 value sticky?
+
+0 value databuf
+0 value datalen
+0 value datain?
+0 value command-finished?
 
 0 value ec-cmd-time-limit
 : ec-cmd-timeout?   ( -- flag )
@@ -152,100 +163,124 @@
    ec-cmd-time-limit 0=  if  1 to ec-cmd-time-limit  then  \ Avoid reserved value
 ;
 
-defer ec-spi-state  ' noop to ec-spi-state
+defer do-state  ' noop to do-state
+defer upstream
 
-defer ec-spi-upstream
-: ec-spi-response  ( -- )
-   cancel-cmd-timeout
-   expected-response-length  0  ?do
-      ssp-ssdr rl@  ec-respbuf i + c!
-   loop
-   1 to ec-spi-cmd-done
+: enter-upstream-state  ( -- )
    2 set-ssp-fifo-threshold
-   clr-cmd
-   ['] ec-spi-upstream to ec-spi-state
+   ['] upstream to do-state
+;
+: command-done  ( -- )
+   cancel-cmd-timeout
+   true to command-finished?
+   sticky?  0=  if
+      enter-upstream-state
+      prime-fifo
+      pulse-ack
+   then
+   \ In sticky mode, we hold off on pulsing ACK until we have the
+   \ next command to send.
 ;
-: ec-spi-switched  ( -- )
-   set-ssp-receive-w/o-transmit
-   expected-response-length  if
-      expected-response-length set-ssp-fifo-threshold
-      ['] ec-spi-response to ec-spi-state
+
+\ Discard 'len' bytes from the Rx FIFO.  Used after a send
+\ operation to get rid of the bytes that were received as
+\ a side effect.
+: clean-fifo  ( len -- )  0  ?do  ssp-ssdr rl@ drop  loop  ;
+
+: response  ( -- )
+   datalen  if
+      \ XXX switch to 64-byte mode if necessary
+      datain?  if
+         debug?  if  ." Data from EC: "  then
+         datalen  0  ?do
+            ssp-ssdr rl@
+            debug?  if  dup .  then
+            databuf i + c!
+         loop
+      else
+         \ Unload the spurious (result of sending data) rx bytes from the FIFO
+         datalen clean-fifo
+      then
+      debug?  if  cr  then
+   then
+   command-done
+;
+: switched  ( -- )
+   \ Unload the spurious (result of sending command) rx bytes from the FIFO
+   cmdlen clean-fifo
+   datalen  if
+      datalen set-ssp-fifo-threshold
+      ['] response to do-state
+      \ XXX switch to 64-byte mode if necessary
+      datain?  if
+         prime-fifo
+      else
+         debug?  if  ." Data to EC: "  then
+         datalen  0  ?do
+            databuf i + c@
+            debug?  if  dup .  then
+            ssp-ssdr rl!
+         loop
+      then
+      pulse-ack
    else
-      ec-spi-response
+      command-done
    then
 ;
-: (ec-spi-upstream)  ( -- )
-   ssp-ssdr rl@  ssp-ssdr rl@             ( channel# data )
-   over 3 =  if  \ Switched               ( channel# data )
-      2drop                               ( )
-      write-cmd-to-ssp-fifo               ( )
-      clr-ssp-receive-w/o-transmit        ( )
-      ['] ec-spi-switched to ec-spi-state ( )
-   else                                   ( channel# data )
-      swap enque                          ( )
-   then
-;
-' (ec-spi-upstream) to ec-spi-upstream
-: init-ec-spi  ( -- )
-   init-ec-spi-gpios
+: handoff-command  ( -- )
+   debug?  if  ." CMD: "  then
+   cmdlen 0  do
+      cmdbuf i + c@
+      debug?  if  dup .  then
+      ssp-ssdr rl!
+   loop
+   debug?  if  cr  then
+   cmdlen set-ssp-fifo-threshold
+   sticky?  0=  if  clr-cmd  then
+   ['] switched to do-state            ( )
+   pulse-ack
+;
+: (upstream)  ( -- )
+   ssp-ssdr rl@  ssp-ssdr rl@              ( channel# data )
+   debug? if
+      ." UP: " over . dup . cr
+   then
+   over case                               ( channel# data )
+      0 of  2drop prime-fifo pulse-ack  endof  ( channel# data )  \ Invalid
+      1 of  2drop handoff-command   endof  ( channel# data )  \ Switched
+      ( default )                          ( channel# data channel# )
+         enque  prime-fifo pulse-ack       ( channel# )
+   endcase
+;
+' (upstream) to upstream
+: init  ( -- )
+   init-gpios
    init-ssp-in-slave-mode
-   set-ssp-receive-w/o-transmit
+   rxflush
+   init-queue
    clr-cmd
+   prime-fifo
    clr-ack  \ Tell EC that it is okay to send
-   ['] ec-spi-upstream to ec-spi-state
+   enter-upstream-state
 ;
 
-: ec-spi-handle-message  ( -- )
-   ec-spi-state
-   pulse-ack
+: poll  ( -- )
+   ssp-ready?  if  do-state  then
+   debug?  if  key?  if  key drop debug-me  then  then
 ;
-: poll-ec-spi  ( -- )
-   ssp-ready?  if
-      exit
-   then
-   ec-cmd-timeout?  if
-      clr-cmd
-      cancel-cmd-timeout
-      2 to ec-spi-cmd-done   \ Timeout
-      ['] ec-spi-upstream to ec-spi-state      
-      exit
-   then
-   ec-spi-handle-message
-;
-
-: ec-command  ( [ args ] #args #results cmd-code -- [ results ] error? )
-   ec-cmdbuf 6 erase       ( [ args ] #args #results cmd-code )
-   ec-cmdbuf c!            ( [ args ] #args #results )
-   over 4 lshift or        ( [ args ] #args #args|#results )
-   ec-cmdbuf 1+ c!         ( [ args ] #args )
-   dup 4 >  abort" Too many EC command arguments"
-   0  ?do                  ( ... arg )
-      ec-cmdbuf 2+ i + c!  ( ... )
-   loop                    ( )
-
-   set-cmd-timeout
-   set-cmd
-
-   0 to ec-spi-cmd-done
-   begin
-      poll-ec-spi
-      ec-spi-cmd-done
-   until
-
-   ec-spi-cmd-done  2 =  if  true  exit  then
-
-   ec-cmdbuf 1+ c@  0 ?do  \ XXX maybe this loop should go backwards?
-      ec-respbuf i + c@
-   loop
-   false
+: cancel-command  ( -- )  \ Called when the command child times out
+   clr-cmd
+   ['] upstream to do-state      
+   prime-fifo
+   pulse-ack
 ;
 
 0 instance value port#
 : set-port  ( port# -- )  to port#  ;
-: put-data  ( byte -- )  port# 2 0 d# 99 ec-command  ;  \ XXX
+\ : put-data  ( byte -- )  port# 2 0 d# 99 ec-command  ;  \ XXX
 : get-data?  ( -- false | data true )
    port# deque?     ( false | data true )
-   poll-ec-spi
+   poll
 ;
 : get-data  ( -- data | -1 )  \ Wait for data from our device
    d# 1000 0  do
@@ -254,7 +289,7 @@
    loop
    true \ abort" Timeout waiting for data from device" 
 ;
-: put-get-data  ( cmd -- data | -1 )  put-data get-data  ;
+\ : put-get-data  ( cmd -- data | -1 )  put-data get-data  ;
 \ Wait until the device stops sending data
 : clear-out-buf  ( -- )  begin  d# 120 ms  get-data?  while  drop  repeat  ;
 
@@ -263,17 +298,182 @@
    open-count  0=  if
       my-address my-space  h# 1000  " map-in" $call-parent  is ssp-base
 \     setup-pin-mux
-      init-ec-spi
+      init
    then
    open-count 1+ to open-count
    true
 ;
 : close  ( -- )
    open-count 1 =  if
-      ssp-base h# 1000  " map-in" $call-parent  0 is ssp-base
+      ssp-base h# 1000  " map-out" $call-parent  0 is ssp-base
    then
    open-count 1- 0 max to open-count
 ;
+
+: data-command  ( databuf datalen datain? cmdadr cmdlen more? -- )
+   to sticky?  to cmdlen   to cmdbuf
+   to datain?  to datalen  to databuf
+   false to command-finished?
+
+   ['] do-state behavior ['] upstream =  if
+      set-cmd-timeout
+      set-cmd
+   else
+      handoff-command
+   then
+   begin  poll  command-finished?  until
+;
+
+: no-data-command  ( adr len sticky? -- )
+   >r >r >r  0 0 0  r> r> r>  data-command
+;
+
+new-device
+" "  " 2" set-args
+" eccmd" name
+my-space " reg" integer-property
+: open  ( -- flag )
+   my-unit " set-port" $call-parent
+   true
+;
+: close  ( -- )
+;
+8 buffer: ec-cmdbuf
+d# 16 buffer: ec-respbuf
+: expected-response-length  ( -- n )  ec-cmdbuf 1+ c@ h# f and  ;
+
+0 value #results
+: set-cmdbuf  ( [ args ] #args #results cmd-code slen -- )
+   >r                      ( [ args ] #args #results cmd-code r: slen )
+   ec-cmdbuf 8 erase       ( [ args ] #args #results cmd-code )
+   ec-cmdbuf c!            ( [ args ] #args #results )
+   to #results             ( [ args ] #args )
+   dup ec-cmdbuf 1+ c!     ( [ args ] #args  r: slen )
+   r> ec-cmdbuf 2+ c!      ( [ args ] #args  r: )
+   h# f and                ( [ args ] #args' )
+   dup 5 >  abort" Too many EC command arguments"
+   ec-cmdbuf 3 +   swap  bounds  ?do  i c!  loop  ( )
+;
+: get-results  ( -- [ results ] )
+   ec-respbuf  #results  bounds  ?do
+      begin  " get-data?" $call-parent  until  ( byte )
+      i c!
+   loop
+
+   #results 0 ?do  \ XXX maybe this loop should go backwards?
+      ec-respbuf i + c@
+   loop
+;
+: ec-command  ( [ args ] #args #results cmd-code -- [ results ] error? )
+   0 set-cmdbuf
+
+   ec-cmdbuf 8 false " no-data-command" $call-parent
+
+   get-results
+   false
+;
+: enter-updater  ( -- )
+   0 0 h# 50 1 set-cmdbuf
+   
+   ec-respbuf 1 true  ec-cmdbuf 8 true " data-command" $call-parent
+;
+
+create pgm-cmd     h# 51 c, h# 84 c, d# 16 c, h# 02 c, h# 00 c, 0 c, 0 c, 0 c,
+create read-cmd    h# 51 c, h# 04 c, d# 16 c, h# 03 c, h# 00 c, 0 c, 0 c, 0 c,
+create rdstat-cmd  h# 51 c, h# 01 c, d# 01 c, h# 05 c, h# 80 c, 0 c, 0 c, 0 c,
+create wena-cmd    h# 51 c, h# 01 c, d# 00 c, h# 06 c, h# 80 c, 0 c, 0 c, 0 c,
+create erase-cmd   h# 51 c, h# 01 c, d# 00 c, h# 60 c, h# 80 c, 0 c, 0 c, 0 c,
+
+: set-offset&len  ( offset len template -- )
+   >r            ( offset len r: template )
+   r@ 2+ c!      ( offset r: template )
+   lbsplit drop  ( low mid hi r: template )
+   r@ 4 + c!  r@ 5 + c!  r> 6 + c!
+;
+: flash-command  ( datadr datlen in? template -- )
+   8 true  " data-command" $call-parent
+;
+: write-flash-chunk  ( adr len offset -- )  \ len limited to 16 bytes for now
+   over pgm-cmd set-offset&len   ( adr len )
+   false pgm-cmd flash-command   ( )
+;
+: read-flash-chunk  ( adr len offset -- )
+   over read-cmd set-offset&len
+   true read-cmd flash-command   ( )
+;
+: read-flash-status  ( -- stat )
+   ec-respbuf 1 true rdstat-cmd flash-command
+   ec-respbuf c@
+;
+: write-enable-flash  ( -- )
+   0 0 false wena-cmd flash-command
+;
+: erase-flash-all  ( -- )
+   0 0 false erase-cmd flash-command
+;
+: read-flash  ( adr len offset -- )
+   swap bounds  ?do        ( adr )
+      i . (cr              ( adr )
+      dup  h# 10  i  read-flash-chunk  ( adr )
+      h# 10 +              ( adr' )
+   h# 10 +loop             ( adr )
+   drop                    ( )
+;
+: wait-write-enabled  ( -- )
+   write-enable-flash   ( adr )
+   begin  read-flash-status 2 and  until
+;
+: wait-write-done  ( -- )
+   begin  read-flash-status 1 and 0=  until
+;
+
+: erase-flash  ( -- )
+   wait-write-enabled
+   erase-flash-all
+   wait-write-done
+;
+
+: write-flash  ( adr len offset -- )
+   swap bounds  ?do        ( adr )
+      i . (cr              ( adr )
+      wait-write-enabled
+      dup  h# 10  i  write-flash-chunk  ( adr )
+      wait-write-done      ( adr )
+      h# 10 +              ( adr' )
+   h# 10 +loop             ( adr )
+   drop                    ( )
+;
+h# 6000 value flash-size
+: get-flash  ( -- )
+   load-base flash-size  0 read-flash
+;
+: put-flash  ( -- )
+   ." Erasing" cr
+   erase-flash
+   ." Writing" cr
+   load-base flash-size  0 write-flash
+;
+: help  ( -- )
+   ." enter-updater" cr
+   ." h# 8000 to flash-size   ( default is 6000)" cr
+   ." get-flash"  cr
+   ." load-base 100 ldump" cr
+   ." put-flash"  cr
+;
+
+finish-device
+
+new-device
+   " "  " 3" set-args
+   fload ${BP}/dev/pckbd.fth
+finish-device
+
+new-device
+   " "  " 4" set-args
+   fload ${BP}/dev/ps2mouse.fth
+finish-device
+
+
 end-package
 
 \ LICENSE_BEGIN

Added: dev/olpc/mmp2camera/ccic.fth
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ dev/olpc/mmp2camera/ccic.fth	Fri Oct 29 18:17:26 2010	(r1992)
@@ -0,0 +1,301 @@
+\ ==========================  video capture operations ==========================
+
+d# 640 constant VGA_WIDTH
+d# 480 constant VGA_HEIGHT
+
+VGA_WIDTH VGA_HEIGHT * 2* constant /dma-buf
+3 constant #dma-bufs
+0 value dma-bufs
+0 value dma-bufs-phys
+0 value next-buf
+
+: 'dma-buf       ( i -- virt )  /dma-buf * dma-bufs      +  ;
+: 'dma-buf-phys  ( i -- phys )  /dma-buf * dma-bufs-phys +  ;
+
+: alloc-dma-bufs  ( -- )
+   dma-bufs 0=  if
+      /dma-buf #dma-bufs *  alloc-capture-buffer  to dma-bufs-phys  to dma-bufs
+   then
+;
+: free-dma-bufs  ( -- )
+   dma-bufs  dma-bufs-phys  /dma-buf #dma-bufs *  " free-capture-buffer" $call-parent
+   0 to dma-bufs 0 to dma-bufs-phys
+;
+
+: setup-dma  ( -- )
+   h# 0440.003c h# 40 cl!   \ posted writes, 3 buffers, 256 byte burst, reserved field
+
+   0 'dma-buf-phys  h# 00 cl!
+   1 'dma-buf-phys  h# 04 cl!
+   2 'dma-buf-phys  h# 08 cl!
+;
+
+\ c000.0000 = 0000.0000 for HSYNC/VSYNC format
+\ 0400.0000 for falling vclk
+\ 0200.0000 for VSYNC active low
+\ 0100.0000 for HSYNC active low
+\ 0080.0000 for VSYNC falling edge
+h# 0000.0000 constant polarities  
+h#        20 constant rgb-sensor
+h#       080 constant rgb-fb
+h#        00 constant rgb-endian  \ 0c bits
+
+\ VGA RGB565
+: setup-image  ( -- )
+   VGA_WIDTH 2*  h# 24 cl!	\ 640*2 stride, UV stride in high bits = 0
+
+   VGA_WIDTH 2*  VGA_HEIGHT wljoin  h# 34 cl!   \ Image size register
+   0             0          wljoin  h# 38 cl!	\ Image offset
+
+   polarities  rgb-fb or  rgb-sensor or  rgb-endian or  h# 3c cl!  \ CTRL0
+;
+
+: interrupts-off  ( -- )  0 h# 2c cl!  h# ffffffff h# 30 cl!  ;
+: interrupts-on  ( -- )  7 h# 2c cl!  h# ffffffff h# 30 cl!  ;
+
+: ctlr-config  ( -- )
+
+   interrupts-off
+   setup-dma
+   setup-image
+;
+
+: ctlr-start  ( -- )  h# 3c dup cl@  1 or          swap cl!  ;  \ Enable
+: ctlr-stop   ( -- )  h# 3c dup cl@  1 invert and  swap cl!  ;	\ Disable
+
+: read-setup  ( -- )
+   camera-config
+   ctlr-config
+     \ Clear all interrupts
+   interrupts-on          \ Enable frame done interrupts
+   ctlr-start
+   0 to next-buf
+;
+
+: power-on  ( -- )
+   \ Enable clocks
+   h# 3f h# d4282828 l!  \ Clock gating - AHB, Internal PIXCLK, AXI clock always on
+   h# 0003.805b h# d4282850 l!  \ PMUA clock config for CCIC - /1, PLL1/16, AXI arb, AXI, perip on
+
+\  h# 0000.0002 h# 88 cl!   \ Clock select - PIXMCLK, 797/2 (PLL1/16) / 2 -> 24.9 MHz
+\  h# 4000.0002 h# 88 cl!   \ Clock select -     AXI, 797/2 (PLL1/16) / 2 -> 24.9 MHz
+   h# 6000.0002 h# 88 cl!   \ Clock select -    core, 797/2 (PLL1/16) / 2 -> 24.9 MHz
+
+   sensor-power-on  1 ms
+   h# 40 cl@  h# 1000.0000 invert and  h# 40 cl!  \ Enable pads
+
+   reset-sensor
+   1 ms
+;
+
+: power-off  ( -- )
+   reset-sensor
+   h# 40 cl@  h# 1000.0000 or  h# 40 cl!  \ Disable pads
+   sensor-power-off
+;
+
+: init  ( -- )
+   power-on
+   ov-smb-setup smb-on
+   camera-init
+;
+
+
+\ =============================  read operation ==============================
+
+0 value buf-act
+: /string  ( adr len n -- adr' len' )  tuck - -rot + swap  ;
+: buf-done?  ( -- false | buf-adr true )
+   h# 30 cl@  dup 1 next-buf lshift  and   if  ( value )
+      h# 30 cl!                ( )
+      next-buf 'dma-buf        ( buf-adr )
+      true                     ( buf-adr true )
+   else                        ( value )
+      drop false               ( false )
+   then
+;
+
+
+: snap  ( timeout -- true | adr false )
+   0  do
+      buf-done?  if   ( buf-adr )
+         false  unloop exit  ( -- buf-adr false )
+      then
+      1 ms
+   loop
+   true
+;
+
+external
+
+: read   ( adr len -- actual )
+   buf-done?  if          ( adr len buf-adr )
+      -rot /dma-buf min   ( buf-adr adr actual )
+      dup >r  move  r>    ( actual )
+   else
+      2drop 0
+   then
+;
+
+: open  ( -- flag )
+   init
+   ov7670-detected? 0=  if  false exit  then
+   alloc-dma-bufs
+   read-setup
+   true
+;
+
+: close  ( -- )
+   ctlr-stop
+   interrupts-off
+   power-off
+   free-dma-bufs
+;
+
+
+\ ============================= selftest operation ===========================
+
+d# 5,000 constant movie-time
+0 constant test-x
+0 constant test-y
+
+\ Thanks to Cortland Setlow (AKA Blaketh) for the autobrightness code
+\ and the full-screen + mirrored display.
+
+: autobright  ( -- )
+   read-agc 3 + 3 rshift  h# f min  " bright!" $call-screen
+;
+: full-brightness  ( -- )  h# f " bright!" $call-screen  ;
+
+code copy16>24-line  ( src-adr dst-adr #pixels -- )
+   mov     r2,tos            \ #pixels in r2
+   ldmia   sp!,{r0,r1,tos}   \ r0: src, r1: dst, r2: #pixels
+   begin
+      ldrh  r3,[r1]
+      inc   r1,2
+
+      mov   r4,r3,lsr #8
+      and   r4,r4,#0xf8
+      strb  r4,[r0],#1
+
+      mov   r4,r3,lsr #3
+      and   r4,r4,#0xfc
+      strb  r4,[r0],#1
+
+      mov   r4,r3,lsl #3
+      and   r4,r4,#0xf8
+      strb  r4,[r0],#1
+
+      decs  r2,1
+   0= until
+c;
+
+VGA_WIDTH  value rect-w
+VGA_HEIGHT value rect-h
+
+d# 1200 3 *  value dst-pitch
+d# 1200 VGA_WIDTH  - 2/ value dst-x
+d#  800 VGA_HEIGHT - 2/ value dst-y
+
+: >dst-adr  ( adr -- adr' )  dst-y dst-pitch *  dst-x +  3 *  +  ;
+
+VGA_WIDTH 2* value src-pitch
+
+: copy16>24  ( src-adr dst-base -- )
+   >dst-adr             ( src-adr dst-adr )
+   rect-h 0  ?do        ( src-adr dst-adr )
+      2dup rect-w copy16>24-line          ( scr-adr dst-adr )
+      swap src-pitch +  swap dst-pitch +  ( scr-adr' dst-adr' )
+   loop                 ( src-adr dst-adr )
+   2drop                ( )
+;
+
+: display-frame  ( adr -- )
+   fb-pa copy16>24
+\   autobright
+;
+
+: timeout-read  ( adr len timeout -- actual )
+   >r 0 -rot r>  0  ?do			( actual adr len )
+      2dup read ?dup  if  3 roll drop -rot leave  then
+      1 ms
+   loop  2drop
+;
+
+: shoot-still  ( -- error? )
+   d# 1000 snap  if  true exit  then   ( adr )
+   display-frame
+   false
+;
+
+: shoot-movie  ( -- error? )
+   get-msecs movie-time +			( timeout )
+   begin                 			( timeout )
+      shoot-still  if  drop true exit  then 	( timeout )
+      dup get-msecs - 0<=                       ( timeout reached )
+   until					( timeout )
+   drop false
+;
+
+: mirrored  ( -- )  h# 1e ov@  h# 20 or  h# 1e ov!  ;
+: unmirrored  ( -- )  h# 1e ov@  h# 20 invert and  h# 1e ov!  ;
+
+: selftest  ( -- error? )
+   open 0=  if  true exit  then
+   d# 300 ms
+   unmirrored  shoot-still  ?dup  if  close exit  then	( error? )
+   d# 1,000 ms
+   mirrored   shoot-movie  full-brightness		( error? )
+   close						( error? )
+   ?dup  0=  if  confirm-selftest?  then		( error? )
+;
+
+: dump-regs  ( run# -- )
+   0 d# 16 " at-xy" eval
+   ." Pass " .d
+   key upc  h# 47 =  if ." Good" else  ." Bad" then cr  \ 47 is G
+
+   ."        0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f" cr
+   ."       -----------------------------------------------" cr
+   h# ca 0  do
+      i 2 u.r ." :  "
+      i h# 10 bounds  do
+         i h# ca <  if  i ov@ 3 u.r   then
+      loop
+      cr
+   h# 10 +loop
+;
+
+: xselftest  ( -- error? )
+   open 0=  if  true exit  then
+
+   h# 10 0 do
+      shoot-still  drop  d# 500 ms  camera-config  config-check
+      i dump-regs
+   loop
+   0 close					( error? )
+;
+
+\ LICENSE_BEGIN
+\ Copyright (c) 2010 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/mmp2camera/loadpkg.fth
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ dev/olpc/mmp2camera/loadpkg.fth	Fri Oct 29 18:17:26 2010	(r1992)
@@ -0,0 +1,31 @@
+" "  " d420a000" " /"  begin-package
+   " camera" device-name
+   0 0 reg  \ A reg property makes "test-all" consider this device
+
+0 [if]
+   : alloc-capture-buffer  ( len -- vadr padr )
+      \ XXX need map-in if we should use virtual mode
+      dup " dma-alloc" $call-parent        ( len vadr )
+      tuck swap                            ( vadr vadr len )
+      false  " dma-map-in" $call-parent    ( vadr padr )
+   ;
+   : free-capture-buffer  ( vadr padr len -- )
+      3dup " dma-map-out" $call-parent  ( vadr padr len )
+      nip  " dma-free" $call-parent
+   ;
+[else]
+   : alloc-capture-buffer  ( len -- vadr padr )
+      drop load-base dup
+   ;
+   : free-capture-buffer  ( vadr padr len -- )
+      3drop
+   ;
+[then]
+
+   fload ${BP}/dev/olpc/mmp2camera/smbus.fth
+   fload ${BP}/dev/olpc/mmp2camera/platform.fth
+   fload ${BP}/dev/olpc/mmp2camera/ov.fth
+   fload ${BP}/dev/olpc/mmp2camera/ccic.fth
+end-package
+
+

Added: dev/olpc/mmp2camera/ov.fth
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ dev/olpc/mmp2camera/ov.fth	Fri Oct 29 18:17:26 2010	(r1992)
@@ -0,0 +1,261 @@
+\ ======================= OV7670 SMBUS operations ==========================
+
+: ov@  ( reg -- data )  ov-smb-setup smb-byte@  ;
+: ov!  ( data reg -- )  ov-smb-setup smb-byte!  ;
+
+: ovc  ( val adr -- )
+   2dup ov@      ( val reg# val actual )
+   tuck <>  if   ( val reg# actual )
+      ." Bad camera I2C value at " swap 2 u.r  ( val actual )
+      ."  expected " swap 2 u.r  ."  got " 2 u.r  cr    ( )
+   else          ( val reg# actual )
+      3drop      ( )
+   then          ( )
+;
+
+\ ============================= camera operations =============================
+
+false value ov7670-detected?
+
+: ((camera-init)  ( -- )
+   80 12 ov!  2 ms		\ reset (reads back different)
+   01 11 ov!			\ 30 fps
+   04 3a ov!			\ UYVY or VYUY
+   00 12 ov!			\ VGA
+
+   \ Hardware window
+   13 17 ov!			\ Horiz start high bits
+   01 18 ov!			\ Horiz stop high bits
+   b6 32 ov!			\ HREF pieces
+   0a 19 ov!			\ Vert start high bits
+   7a 1a ov!			\ Vert stop high bits
+   0a 03 ov!			\ GAIN, VSTART, VSTOP pieces
+
+   \ Mystery scaling numbers
+   00 0c ov!			\ Control 3
+   00 3e ov!			\ Control 14
+   3a 70 ov!  35 71 ov!  11 72 ov!  f0 73 ov!
+   02 a2 ov!
+   00 15 ov!			\ Control 10
+
+   \ Gamma curve values
+   20 7a ov!  10 7b ov!  1e 7c ov!  35 7d ov!
+   5a 7e ov!  69 7f ov!  76 80 ov!  80 81 ov!
+   88 82 ov!  8f 83 ov!  96 84 ov!  a3 85 ov!
+   af 86 ov!  c4 87 ov!  d7 88 ov!  e8 89 ov!
+
+   \ AGC and AEC parameters
+   e0 13 ov!			\ Control 8
+   00 00 ov!			\ Gain lower 8 bits
+   40 0d ov!			\ Control 4 magic reserved bit
+   18 14 ov!			\ Control 9: 4x gain + magic reserved bit
+   05 a5 ov!			\ 50hz banding step limit
+   07 ab ov!			\ 60hz banding step limit
+   95 24 ov!			\ AGC upper limit
+   33 25 ov!			\ AGC lower limit
+   e3 24 ov!			\ AGC/AEC fast mode op region
+   78 9f ov!			\ Hist AEC/AGC control 1
+   68 a0 ov!			\ Hist AEC/AGC control 2
+   03 a1 ov!			\ Magic
+   d8 a6 ov!			\ Hist AEC/AGC control 3
+   d8 a7 ov!			\ Hist AEC/AGC control 4
+   f0 a8 ov!			\ Hist AEC/AGC control 5
+   90 a9 ov!			\ Hist AEC/AGC control 6
+   94 aa ov!			\ Hist AEC/AGC control 7
+   e5 13 ov!			\ Control 8
+
+   \ Mostly magic
+   61 0e ov!  4b 0f ov!  02 16 ov!  07 1e ov!
+   02 21 ov!  91 22 ov!  07 29 ov!  0b 33 ov!
+   0b 35 ov!  1d 37 ov!  71 38 ov!  2a 39 ov!
+   78 3c ov!  40 4d ov!  20 4e ov!  00 69 ov! 
+   4a 6b ov!  10 74 ov!  4f 8d ov!  00 8e ov!
+   00 8f ov!  00 90 ov!  00 91 ov!  00 96 ov!
+   00 9a ov!  84 b0 ov!  0c b1 ov!  0e b2 ov!
+   82 b3 ov!  0a b8 ov!
+
+   \ More magic, some of which tweaks white balance
+   0a 43 ov!  f0 44 ov!  34 45 ov!  58 46 ov!
+   28 47 ov!  3a 48 ov!  88 59 ov!  88 5a ov!
+   44 5b ov!  67 5c ov!  49 5d ov!  0e 5e ov!
+   0a 6c ov!  55 6d ov!  11 6e ov!
+   9f 6f ov!			\ 9e for advance AWB
+   40 6a ov!
+   40 01 ov!			\ Blue gain
+   60 02 ov!			\ Red gain
+   e7 13 ov!			\ Control 8
+
+   \ Matrix coefficients
+   80 4f ov!  80 50 ov!  00 51 ov!  22 52 ov!
+   5e 53 ov!  80 54 ov!  9e 58 ov!
+
+   08 41 ov!			\ AWB gain enable
+   00 3f ov!			\ Edge enhancement factor
+   05 75 ov!  e1 76 ov!  00 4c ov!  01 77 ov!
+   c3 3d ov!			\ Control 13
+   09 4b ov!  60 c9 ov!         \ Reads back differently
+   38 41 ov!			\ Control 16
+   40 56 ov!
+
+   11 34 ov!
+   12 3b ov!			\ Control 11
+   88 a4 ov!  00 96 ov!  30 97 ov!  20 98 ov!
+   30 99 ov!  84 9a ov!  29 9b ov!  03 9c ov!
+   5c 9d ov!  3f 9e ov!  04 78 ov!
+
+   \ Extra-weird stuff.  Some sort of multiplexor register
+   01 79 ov!  f0 c8 ov!
+   0f 79 ov!  00 c8 ov!
+   10 79 ov!  7e c8 ov!
+   0a 79 ov!  80 c8 ov!
+   0b 79 ov!  01 c8 ov!
+   0c 79 ov!  0f c8 ov!
+   0d 79 ov!  20 c8 ov!
+   09 79 ov!  80 c8 ov!
+   02 79 ov!  c0 c8 ov!
+   03 79 ov!  40 c8 ov!
+   05 79 ov!  30 c8 ov!
+   26 79 ov!
+
+   \ OVT says that rewrite this works around a bug in 565 mode.
+   \ The symptom of the bug is red and green speckles in the image.
+   01 11 ov!			\ 30 fps def 80
+;
+
+: config-check  ( -- )
+   01 11 ovc			\ 30 fps
+   04 3a ovc			\ UYVY or VYUY
+   ( 00 12 ovc )		\ VGA
+
+   \ Hardware window
+   13 17 ovc			\ Horiz start high bits
+   01 18 ovc			\ Horiz stop high bits
+   b6 32 ovc			\ HREF pieces
+   ( 0a 19 ovc )		\ Vert start high bits
+   7a 1a ovc			\ Vert stop high bits
+   0a 03 ovc			\ GAIN, VSTART, VSTOP pieces
+
+   \ Mystery scaling numbers
+   00 0c ovc			\ Control 3
+   00 3e ovc			\ Control 14
+   3a 70 ovc  35 71 ovc  11 72 ovc  f0 73 ovc
+   02 a2 ovc
+   00 15 ovc			\ Control 10
+
+   \ Gamma curve values
+   20 7a ovc  10 7b ovc  1e 7c ovc  35 7d ovc
+   5a 7e ovc  69 7f ovc  76 80 ovc  80 81 ovc
+   88 82 ovc  8f 83 ovc  96 84 ovc  a3 85 ovc
+   af 86 ovc  c4 87 ovc  d7 88 ovc  e8 89 ovc
+
+   \ AGC and AEC parameters
+   ( e0 13 ovc )		\ Control 8
+   ( 00 00 ovc )		\ Gain lower 8 bits
+   40 0d ovc			\ Control 4 magic reserved bit
+   ( 18 14 ovc )		\ Control 9: 4x gain + magic reserved bit
+   05 a5 ovc			\ 50hz banding step limit
+   07 ab ovc			\ 60hz banding step limit
+   ( 95 24 ovc )		\ AGC upper limit
+   33 25 ovc			\ AGC lower limit
+   e3 24 ovc			\ AGC/AEC fast mode op region
+   78 9f ovc			\ Hist AEC/AGC control 1
+   68 a0 ovc			\ Hist AEC/AGC control 2
+   03 a1 ovc			\ Magic
+   d8 a6 ovc			\ Hist AEC/AGC control 3
+   d8 a7 ovc			\ Hist AEC/AGC control 4
+   f0 a8 ovc			\ Hist AEC/AGC control 5
+   90 a9 ovc			\ Hist AEC/AGC control 6
+   94 aa ovc			\ Hist AEC/AGC control 7
+   ( e5 13 ovc	)		\ Control 8
+
+   \ Mostly magic
+   61 0e ovc  4b 0f ovc  02 16 ovc  07 1e ovc
+   02 21 ovc  91 22 ovc  07 29 ovc  0b 33 ovc
+   0b 35 ovc  1d 37 ovc  71 38 ovc  2a 39 ovc
+   78 3c ovc  40 4d ovc  20 4e ovc  00 69 ovc 
+   4a 6b ovc  10 74 ovc  4f 8d ovc  00 8e ovc
+   00 8f ovc  00 90 ovc  00 91 ovc  00 96 ovc
+   ( 00 9a ovc )  84 b0 ovc  0c b1 ovc  0e b2 ovc
+   82 b3 ovc  0a b8 ovc
+
+   \ More magic, some of which tweaks white balance
+   0a 43 ovc  f0 44 ovc  34 45 ovc  58 46 ovc
+   28 47 ovc  3a 48 ovc  88 59 ovc  88 5a ovc
+   44 5b ovc  67 5c ovc  49 5d ovc  0e 5e ovc
+   0a 6c ovc  55 6d ovc  11 6e ovc
+   9f 6f ovc			\ 9e for advance AWB
+   ( 40 6a ovc )
+   ( 40 01 ovc )		\ Blue gain
+   ( 60 02 ovc )		\ Red gain
+   e7 13 ovc			\ Control 8
+
+   \ Matrix coefficients
+   b3 4f ovc  b3 50 ovc  00 51 ovc  3d 52 ovc
+   a7 53 ovc  e4 54 ovc  9e 58 ovc
+
+   \ 08 41 ovc			\ AWB gain enable
+   ( 00 3f ovc )		\ Edge enhancement factor
+   05 75 ovc  e1 76 ovc  ( 00 4c ovc )  01 77 ovc
+   c0 3d ovc			\ Control 13
+   09 4b ovc  ( 60 c9 ovc )
+   38 41 ovc			\ Control 16
+   40 56 ovc
+
+   11 34 ovc
+   12 3b ovc			\ Control 11
+   88 a4 ovc  00 96 ovc  30 97 ovc  20 98 ovc
+   30 99 ovc  84 9a ovc  29 9b ovc  03 9c ovc
+   5c 9d ovc  3f 9e ovc  04 78 ovc
+
+;
+
+: camera-init  ( -- )
+   false to ov7670-detected?
+   ((camera-init)
+   1d ov@ 1c ov@  bwjoin 7fa2 <>  if  exit  then	\ Manufacturing ID
+    b ov@  a ov@  bwjoin 7673 <>  if  exit  then	\ Product ID
+   true to ov7670-detected?
+;
+
+\ VGA RGB565
+: init-rgb565  ( -- )
+   04 12 ov!				\ VGA, RGB565
+   00 8c ov!				\ No RGB444
+   00 04 ov!				\ Control 1 (CaFe value is 00, Via is 40 for CCIR656)
+   10 40 ov!				\ RGB565 output
+   38 14 ov!				\ 16x gain ceiling
+   b3 4f ov!				\ v-red
+   b3 50 ov!				\ v-green
+   00 51 ov!				\ v-blue
+   3d 52 ov!				\ u-red
+   a7 53 ov!				\ u-green
+   e4 54 ov!				\ u-blue
+   c0 3d ov!				\ Gamma enable, UV saturation auto adjust
+;
+
+: read-agc  ( -- n )
+   3 ov@  h# c0 and  2 lshift  0 ov@ or
+;
+
+: read-aec  ( -- n )
+   7 ov@  h# 3f and  d# 10 lshift
+   h# 10 ov@  2 lshift  or
+   4 ov@  3 and  or
+;
+
+: set-hw  ( vstop vstart hstop hstart -- )
+   dup  3 >> 17 ov!			\ Horiz start high bits
+   over 3 >> 18 ov!			\ Horiz stop high bits
+   32 ov@ swap 7 and or swap 7 and 3 << or 10 ms 32 ov!	\ Horiz bottom bits
+
+   dup  2 >> 19 ov!			\ Vert start high bits
+   over 2 >> 1a ov!			\ Vert start high bits
+   03 ov@ swap 3 and or swap 3 and 2 << or 10 ms 03 ov!	\ Vert bottom bits
+;
+
+: camera-config  ( -- )
+   ((camera-init)
+   init-rgb565
+   d# 490 d# 10 d# 14 d# 158 set-hw	\ VGA window info
+;
+

Added: dev/olpc/mmp2camera/platform.fth
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ dev/olpc/mmp2camera/platform.fth	Fri Oct 29 18:17:26 2010	(r1992)
@@ -0,0 +1,26 @@
+\ See license at end of file
+purpose: Driver for OLPC camera connected to Via VX855 Video Capture Port
+
+headers
+hex
+
+" OV7670" " sensor" string-property
+
+: ov-smb-setup  ( -- )
+   1 to smb-dly-us
+   d# 108 to smb-clk-gpio# d# 109 to smb-data-gpio#
+   h# 42 to smb-slave
+;
+
+: reset-sensor  ( -- )  d# 73 gpio-clr  1 ms  d# 73 gpio-set  ;
+
+: sensor-power-on   ( -- )  d# 145 gpio-set  ;
+: sensor-power-off  ( -- )  d# 145 gpio-clr  ;
+
+\ CAM_HSYNC is on GPIO67, CAM_VSYNC is on GPIO68
+\ PIXMCLK on GPIO69, PIXCLK on GPIO70, PIXDATA[7:0] on GPIO[59:66]
+\ CAM_SCL on GPIO108, CAM_SDA on GPIO109 (bitbang)
+
+: cl!  ( l adr -- )  h# d420a000 + rl!  ;
+: cl@  ( adr -- l )  h# d420a000 + rl@  ;
+

Added: dev/olpc/mmp2camera/smbus.fth
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ dev/olpc/mmp2camera/smbus.fth	Fri Oct 29 18:17:26 2010	(r1992)
@@ -0,0 +1,120 @@
+4 value smb-dly-us
+: smb-dly  smb-dly-us us  ;
+
+0 value smb-data-gpio#
+0 value smb-clk-gpio#
+
+: smb-data-hi  ( -- )  smb-data-gpio# gpio-set  smb-dly  ;
+: smb-data-lo  ( -- )  smb-data-gpio# gpio-clr  smb-dly  ;
+: smb-clk-hi  ( -- )  smb-clk-gpio# gpio-set  smb-dly  ;
+: smb-clk-lo  ( -- )  smb-clk-gpio# gpio-clr  smb-dly  ;
+: smb-data@  ( -- flag )  smb-data-gpio# gpio-pin@  ;
+: smb-clk@  ( -- )  smb-clk-gpio#  gpio-pin@  ;
+: smb-off  ( -- )  smb-data-gpio# gpio-dir-in  ;
+: smb-on  ( -- )  smb-data-gpio# gpio-dir-out  smb-clk-gpio# gpio-dir-out  ;
+: smb-data-dir-out  ( -- )  smb-data-gpio# gpio-dir-out  ;
+: smb-data-dir-in  ( -- )  smb-data-gpio# gpio-dir-in  ;
+
+h# 3500 constant smb-clk-timeout-us
+\ Slave can flow control by holding CLK low temporarily
+: smb-wait-clk-hi  ( -- )
+   smb-clk-timeout-us 0  do
+      smb-clk@  if  smb-dly  unloop exit  then  1 us
+   loop
+   true abort" I2C clock stuck low"
+;
+: smb-data-hi-w  ( -- )  smb-data-hi  smb-wait-clk-hi  ;
+
+h# 3500 constant smb-data-timeout-us
+: smb-wait-data-hi  ( -- )
+   smb-data-timeout-us 0  do
+      smb-data@  if  unloop exit  then  1 us
+   loop
+   true abort" I2C data stuck low"
+;
+
+: smb-restart  ( -- )
+   smb-clk-hi  smb-data-lo  smb-clk-lo
+;
+
+: smb-start ( -- )  smb-clk-hi  smb-data-hi  smb-data-lo smb-clk-lo  ;
+: smb-stop  ( -- )  smb-clk-lo  smb-data-lo  smb-clk-hi  smb-data-hi  ;
+
+: smb-get-ack  ( -- )
+   smb-data-dir-in
+   smb-data-hi
+   smb-clk-hi smb-wait-clk-hi  
+   smb-data@  \ drop		\ SCCB generates an don't care bit
+   if  smb-stop  smb-data-dir-out  true abort" I2c NAK" then
+   smb-clk-lo
+\   smb-wait-data-hi
+   smb-data-dir-out
+;
+: smb-bit  ( flag -- )
+   if  smb-data-hi  else  smb-data-lo  then
+   smb-clk-hi smb-wait-clk-hi  smb-clk-lo
+;
+
+: smb-byte  ( b -- )
+   8 0  do                     ( b )
+      dup h# 80 and  smb-bit   ( b )
+      2*                       ( b' )
+   loop                        ( b )
+   drop                        ( )
+   smb-get-ack
+;
+: smb-byte-in  ( ack=0/nak=1 -- b )
+   smb-data-dir-in
+   0
+   8 0  do             ( n )
+      smb-clk-hi       ( n )
+      2*  smb-data@  if  1 or  then  ( n' )
+      smb-clk-lo
+   loop
+   smb-data-dir-out
+   swap smb-bit  smb-data-hi  \ Send ACK or NAK
+;
+
+0 value smb-slave
+: smb-addr  ( lowbit -- )  smb-slave or  smb-byte  ;
+
+: smb-byte!  ( byte reg# -- )
+   smb-start
+   0 smb-addr          ( byte reg# )
+   smb-byte            ( byte )
+   smb-byte            ( )
+   smb-stop
+;
+
+: smb-byte@  ( reg# -- byte )
+   smb-start
+   0 smb-addr          ( reg# )
+   smb-byte            ( )
+   smb-stop smb-start	\ SCCB bus needs a stop and a start for the second phase
+   1 smb-addr
+   1 smb-byte-in       ( byte )
+   smb-stop
+;
+
+: smb-word!  ( word reg# -- )
+   smb-start
+   0 smb-addr          ( word reg# )
+   smb-byte            ( word )
+   wbsplit swap smb-byte smb-byte  ( )
+   smb-stop
+;
+
+: smb-word@  ( reg# -- word )
+   smb-start
+   0 smb-addr          ( reg# )
+   smb-byte            ( )
+   smb-restart
+   1 smb-addr          ( )
+   0 smb-byte-in   1 smb-byte-in  bwjoin  ( word )
+   smb-stop
+;
+
+\ This can useful for clearing out DCON SMB internal state
+: smb-pulses  ( -- )
+   d# 32 0  do  smb-clk-lo smb-clk-hi  loop
+;

Modified: dev/pci/isakbd.fth
==============================================================================
--- dev/pci/isakbd.fth	Mon Oct 25 14:42:14 2010	(r1991)
+++ dev/pci/isakbd.fth	Fri Oct 29 18:17:26 2010	(r1992)
@@ -6,11 +6,13 @@
 fload ${BP}/dev/i8042.fth
 
    new-device
+   " "  " 0" set-args
    fload ${BP}/dev/pckbd.fth
 \   d# 1 encode-int  3 encode-int encode+  " interrupts" property
    finish-device
 
    new-device
+   " "  " 1" set-args
    fload ${BP}/dev/ps2mouse.fth
 \   d# 12 encode-int  3 encode-int encode+  " interrupts" property
    finish-device

Modified: dev/pckbd.fth
==============================================================================
--- dev/pckbd.fth	Mon Oct 25 14:42:14 2010	(r1991)
+++ dev/pckbd.fth	Fri Oct 29 18:17:26 2010	(r1992)
@@ -6,14 +6,13 @@
 hex
 headerless
 
+my-space " reg " integer-property
 " keyboard"  device-name
 
 " pnpPNP,303" " compatible" string-property
 
 " keyboard" device-type
 
-0 " reg" integer-property
-
 : kbdtest ;
 
 0 value #queued
@@ -528,7 +527,7 @@
 : open  ( -- okay? )
    kbd-refcount @  if  1 +refcnt  true exit  then
    unlock
-   0 set-port
+   my-space set-port
    keyboard-present?  if  clear-out-buf  else  reset  then
    keyboard-present?  0=  if  false exit  then
    choose-type

Modified: dev/ps2mouse.fth
==============================================================================
--- dev/ps2mouse.fth	Mon Oct 25 14:42:14 2010	(r1991)
+++ dev/ps2mouse.fth	Fri Oct 29 18:17:26 2010	(r1992)
@@ -4,7 +4,7 @@
 " mouse"          device-name
 " mouse"          device-type
 " pnpPNP,f03" " compatible" string-property
-1 " reg" integer-property
+my-space " reg " integer-property
 
 headerless
 : get-data  ( -- byte )  " get-data" $call-parent  ;
@@ -299,7 +299,7 @@
    lock[
    identify  if
       \ This port is unresponsive; try the other
-      0 set-port  identify  if  ]unlock  true exit  then
+      my-unit 1- set-port  identify  if  ]unlock  true exit  then
    then                                   ( id )
 
    dup  h# ab =  if                       ( id )
@@ -312,7 +312,7 @@
       my-port 0=  if  ]unlock  true exit  then
 
       \ Otherwise look for the mouse on the keyboard port
-      0 set-port  identify  if  ]unlock  true exit  then  ( id )
+      my-unit 1- set-port  identify  if  ]unlock  true exit  then  ( id )
    then                                   ( id )
    ]unlock                                ( id )
 
@@ -325,7 +325,7 @@
 
 headers
 : open  ( -- flag )
-   1 set-port
+   my-unit set-port
 
    open-count 0=  if
       \ The "force" argument causes the open to succeed even if no mouse



More information about the openfirmware mailing list