needs lib/struct com/ether drv/sunxi/ccu unit drv/sunxi/emac 1500 const MTU MTU 8 align const BUFSZ 8 const RXDESCCNT create macaddr $72ca84ee , $000012bc , \ a random MAC addrress 4 4* allot@ const TXDESC BUFSZ allot@ const TXBUF 4 4* RXDESCCNT * allot@ const RXDESC BUFSZ RXDESCCNT * allot@ const RXBUF struct TxRxDesc { uint Status Size Addr Next ; } $01c00030 const EMAC_CLK_REG \ SYSCON register struct EMAC { +$00 uint BASIC_CTL_0 BASIC_CTL_1 ; +$10 uint TX_CTL_0 TX_CTL_1 ; +$20 uint TX_DESC_LIST ; +$24 uint RX_CTL_0 RX_CTL_1 ; +$34 uint RX_DESC_LIST ; +$48 uint MII_CMD MII_DATA ADDR_HIGH_0 ADDR_LOW_0 ; } here# $01c30000 , EMAC bindstruct : miiwait ticks begin 10 over ?timeoutms"mii timeout" MII_CMD 1 and not until drop ; : mii@ ( reg -- u ) 4 lshift $00301001 or to MII_CMD miiwait MII_DATA ; : mii! ( u reg -- ) 4 lshift $00301003 or swap to MII_DATA to MII_CMD miiwait ; : linkup? 1 mii@ 4 and bool ; : tx$ 0 TXDESC A! to Status 0 A> to Size TXBUF A> to Addr TXDESC A> to Next TXDESC to TX_DESC_LIST $80000000 to TX_CTL_0 ; : rxbuf ( idx -- a ) BUFSZ * RXBUF + ; : rxdesc ( idx -- a ) 16 * RXDESC + ; : resetrxdesc ( idx -- A=a ) r! rxdesc >A BUFSZ A> to Size r@ rxbuf A> to Addr r@ 1+ RXDESCCNT mod rxdesc A> to Next $80000000 A> to Status rdrop ; : rx$ RXDESCCNT 0 do i resetrxdesc loop RXDESC to RX_DESC_LIST $80000000 to RX_CTL_0 ; : :waitsent ( link -- ) drop ticks begin 10 over ?timeoutms":txbuf timeout" TXDESC Status $80000000 and not until drop ; : :sendframe ( link -- ) drop frameptr TXDESC A! to Addr payloadsz ETHERHDRSZ + $60000000 or A> to Size $80000000 A> to Status $c0000000 to TX_CTL_1 ; : :txbuf ( link -- a u ) :waitsent TXBUF BUFSZ ; 0 value prevdesc : :rxbuf ( link -- ?a u-or-0 ) $c0000000 to RX_CTL_1 drop doto prevdesc 0 | ?dup if 1- resetrxdesc then ( ) RXDESCCNT 0 do i rxdesc Status $80000000 and not if i 1+ to prevdesc i rxbuf BUFSZ unloop exit then loop 0 ; : sunxiemac$ doto BUS_CLK_GATING_REG0 1 17 lshift or | doto BUS_SOFT_RST_REG0 1 17 lshift or | doto BUS_CLK_GATING_REG4 1 or | doto BUS_SOFT_RST_REG2 4 or | $00168000 EMAC_CLK_REG ! 1 to BASIC_CTL_1 macaddr @+ to ADDR_LOW_0 @ to ADDR_HIGH_0 $d to BASIC_CTL_0 $8000 0 mii! tx$ rx$ ; ' :waitsent ' :sendframe ' :txbuf ' :rxbuf macaddr newetherlink const sunxiemac