#include <sys/param.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/systm.h>
#include <sys/socket.h>
#include <net/if.h>
#include <dev/mii/mii.h>
#include <dev/etherswitch/etherswitch.h>
#include <dev/etherswitch/ip17x/ip17x_phy.h>
#include <dev/etherswitch/ip17x/ip17x_reg.h>
#include <dev/etherswitch/ip17x/ip17x_var.h>
#include <dev/etherswitch/ip17x/ip17x_vlans.h>
#include <dev/etherswitch/ip17x/ip175d.h>
static int
ip175d_reset(struct ip17x_softc *sc)
{
ip17x_writephy(sc->sc_dev, IP175D_RESET_PHY, IP175D_RESET_REG, 0x175d);
DELAY(2000);
ip17x_updatephy(sc->sc_dev, 21, 22, 0x3, 0x0);
ip17x_writephy(sc->sc_dev, 22, 3, 0x8100);
return (0);
}
static int
ip175d_hw_setup(struct ip17x_softc *sc)
{
struct ip17x_vlan *v;
uint32_t ports[IP17X_MAX_VLANS];
uint32_t addtag[IP17X_MAX_VLANS];
uint32_t striptag[IP17X_MAX_VLANS];
uint32_t vlan_mask;
int i, j;
vlan_mask = 0;
for (i = 0; i < IP17X_MAX_VLANS; i++) {
ports[i] = 0;
addtag[i] = 0;
striptag[i] = 0;
v = &sc->vlan[i];
if ((v->vlanid & ETHERSWITCH_VID_VALID) == 0 ||
sc->vlan_mode == 0) {
ip17x_writephy(sc->sc_dev, 22, 14 + i, i + 1);
ports[i] = 0x3f;
continue;
}
vlan_mask |= (1 << i);
ports[i] = v->ports;
ip17x_writephy(sc->sc_dev, 22, 14 + i,
v->vlanid & ETHERSWITCH_VID_MASK);
for (j = 0; j < MII_NPHY; j++) {
if ((ports[i] & (1 << j)) == 0)
continue;
if (sc->addtag & (1 << j))
addtag[i] |= (1 << j);
if (sc->striptag & (1 << j))
striptag[i] |= (1 << j);
}
}
for (i = 0; i < IP17X_MAX_VLANS / 2; i++) {
ip17x_writephy(sc->sc_dev, 23, i,
ports[2 * i] | (ports[2 * i + 1] << 8));
ip17x_writephy(sc->sc_dev, 23, i + 8,
addtag[2 * i] | (addtag[2 * i + 1] << 8));
ip17x_writephy(sc->sc_dev, 23, i + 16,
striptag[2 * i] | (striptag[2 * i + 1] << 8));
}
ip17x_writephy(sc->sc_dev, 22, 10, vlan_mask);
for (i = 0; i < sc->numports; i++)
ip17x_writephy(sc->sc_dev, 22, 4 + i, sc->pvid[i]);
return (0);
}
static int
ip175d_set_vlan_mode(struct ip17x_softc *sc, uint32_t mode)
{
switch (mode) {
case ETHERSWITCH_VLAN_DOT1Q:
ip17x_updatephy(sc->sc_dev, 22, 0, 0x3fff, 0x003f);
sc->vlan_mode = mode;
break;
case ETHERSWITCH_VLAN_PORT:
sc->vlan_mode = mode;
default:
ip17x_updatephy(sc->sc_dev, 22, 0, 0xbfff, 0x8000);
sc->vlan_mode = 0;
break;
}
if (sc->vlan_mode != 0) {
ip17x_updatephy(sc->sc_dev, 22, 1, 0x0fff, 0x0c3f);
ip17x_updatephy(sc->sc_dev, 22, 2, 0x0fff, 0x0000);
} else {
ip17x_updatephy(sc->sc_dev, 22, 1, 0x0fff, 0x043f);
ip17x_updatephy(sc->sc_dev, 22, 2, 0x0fff, 0x0020);
}
ip17x_reset_vlans(sc, sc->vlan_mode);
ip175d_hw_setup(sc);
return (0);
}
static int
ip175d_get_vlan_mode(struct ip17x_softc *sc)
{
return (sc->vlan_mode);
}
void
ip175d_attach(struct ip17x_softc *sc)
{
sc->hal.ip17x_reset = ip175d_reset;
sc->hal.ip17x_hw_setup = ip175d_hw_setup;
sc->hal.ip17x_get_vlan_mode = ip175d_get_vlan_mode;
sc->hal.ip17x_set_vlan_mode = ip175d_set_vlan_mode;
sc->cpuport = IP175X_CPU_PORT;
sc->numports = IP175X_NUM_PORTS;
sc->info.es_vlan_caps = ETHERSWITCH_VLAN_DOT1Q;
device_printf(sc->sc_dev, "type: IP175D\n");
}