/*-
* Copyright (c) 2012 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Tohru Nishimura.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This DM9000 is wired as a 16bit device and manages Tx/Rx SRAM buffer
* in 16bit quantity. MRCMD/MWCMD access increments buffer pointer
* by two regardless of r/w size. Mixing 16/8bit access is not possible.
* Byte read would end up with loosing every odd indexed datum.
*
* The DM9000 CMD pin is tied with SoC LADDR2 address line. SA9-SA4
* pins are hardwired to fixed decoding 0x300. Thus address [26:3] in
* CS4 range 0x2000'0000 are don't-care bits to manipulate the chip.
* The DM9000 INDEX port is accessed at the address b'000 while the
* DATA port at the address b'100.
*
* This code assumes Little endian CPU.
*/
#define NCR 0x00 /* control */
#define NCR_FDX (1<<3) /* FDX link detection report */
#define NCR_RST (1<<0) /* instruct reset, goes 0 at done */
#define NSR 0x01 /* status */
#define NSR_SPEED (1<<7) /* 1->100M, 0->10M, when link is up */
#define NSR_LINKST (1<<6) /* 1->linkup, 0->linkdown */
#define NSR_TX2END (1<<3) /* Tx frame #2 completed */
#define NSR_TX1END (1<<2) /* Tx frame #1 completed */
#define NSR_RXOV (1<<1) /* Rx FIFO overflow detected */
#define TCR 0x02 /* Tx control */
#define TCR_TXREQ 0x01 /* request to start transmit, goes 0 at done */
#define TCR2 0x2d /* Tx control #2 */
#define TCR2_ONEPM 0x10 /* send single Tx frame at a time */
#define RCR 0x05 /* Rx control */
#define RCR_WTDIS 0x40 /* disable frame receipt watchdog timer */
#define RCR_DIS_LONG 0x20 /* discard too-long Rx frame */
#define RCR_DIS_CRC 0x10 /* discard CRC error Rx frame */
#define RCR_ALL 0x08 /* accept MCAST frames */
#define RCR_RUNT 0x04 /* accept runt Rx frame */
#define RCR_PRMSC 0x02 /* promiscuous */
#define RCR_RXEN 0x01 /* enable frame reception */
#define RSR 0x06 /* RX status */
#define RSR_MF (1<<6) /* bcast/mcast frame found */
#define FCR 0x0a /* flow control */
#define FCR_FLCE 0x01 /* enable Tx/Rx flow control */
#define EPCR 0x0b /* EEPROM and PHY control */
#define EP_EPOS (1<<3) /* 1 for PHY op, 0 for EEPROM op */
#define EP_ERPRR (1<<2) /* instruct to start read op */
#define EP_ERPRW (1<<1) /* instruct to start write op */
#define EP_ERRE (1<<0) /* 1 while operation is in progress */
#define EPAR 0x0c /* [7:6] for PHY#, [5:0] for addr */
#define EPDRL 0x0d /* EEPROM/PHY data low byte */
#define EPDRH 0x0e /* EEPROM/PHY data high byte */
#define PAR 0x10 /* station address */
#define MAR 0x16 /* multicast filter hash value */
#define GPR 0x1f /* gpio control */
#define GPR_PHYPWROFF 0x01 /* powerdown internal PHY */
#define VID0 0x28 /* vendor ID low byte */
#define VID1 0x29 /* vendor ID high byte */
#define PID0 0x2a /* product ID low byte */
#define PID1 0x2b /* product ID high byte */
#define CHIPR 0x2c /* chip revision */
#define MRCMDX 0xf0 /* read data w/o pointer incr */
#define MRCMD 0xf2 /* read data with pointer auto incr */
#define MWCMD 0xf8 /* write data with pointer auto incr */
#define TXPLL 0xfc /* Tx frame length low byte */
#define TXPLH 0xfd /* Tx frame length high byte */
#define ISR 0xfe /* interrupt status report */
#define ISR_LNKCHG 0x20 /* link status change detected */
#define ISR_UDRUN 0x10 /* transmit underrun detected */
#define ISR_PTM (1<<1) /* frame Tx completed */
#define ISR_PRS (1<<0) /* frame Rx completed */
#define IMR 0xff /* interrupt mask */
#define IMR_PAR (1<<7) /* use 3/13K SRAM partitioning with autowrap */
#define IMR_PRM (1<<0) /* post interrupt when Rx completed */
/* SW reset, again */
CSR_WRITE_1(l, NCR, NCR_RST);
do {
usleep(1);
} while (NCR_RST & CSR_READ_1(l, NCR));
/* clear NSR bits */
(void) CSR_READ_1(l, NSR);
printf("MAC address %02x:%02x:%02x:%02x:%02x:%02x\n",
en[0], en[1], en[2], en[3], en[4], en[5]);
CSR_WRITE_1(l, PAR + 0, en[0]);
CSR_WRITE_1(l, PAR + 1, en[1]);
CSR_WRITE_1(l, PAR + 2, en[2]);
CSR_WRITE_1(l, PAR + 3, en[3]);
CSR_WRITE_1(l, PAR + 4, en[4]);
CSR_WRITE_1(l, PAR + 5, en[5]);
/* make sure not to receive bcast/mcast frames */
CSR_WRITE_1(l, MAR + 0, 0);
CSR_WRITE_1(l, MAR + 1, 0);
CSR_WRITE_1(l, MAR + 2, 0);
CSR_WRITE_1(l, MAR + 3, 0);
CSR_WRITE_1(l, MAR + 4, 0);
CSR_WRITE_1(l, MAR + 5, 0);
CSR_WRITE_1(l, MAR + 6, 0);
CSR_WRITE_1(l, MAR + 7, 0);
/* perform link auto-negotiation */
printf("waiting for linkup ... ");
mii_dealan(l, 5);
val = CSR_READ_1(l, NSR);
if ((val & NSR_LINKST) == 0) {
printf("failed; cable problem?\n");
return NULL;
}
/*
* speed and duplexity can be seen in MII 17.
* bit15 100Mbps-FDX
* bit14 100Mbps
* bit13 10Mbps-FDX
* bit12 10Mbps
* also available in NSR[SPEED] and NCR[FDX] respectively.
*/
val = mii_read(l, l->phy, 17);
if (val & (03 << 14))
printf("100Mbps");
if (val & (03 << 12))
printf("10Mbps");
fdx = !!(val & (05 << 13));
if (fdx) {
printf("-FDX");
val = CSR_READ_1(l, FCR);
CSR_WRITE_1(l, FCR, val | FCR_FLCE);
}
printf("\n");