1250386Sadrian/*-
2250386Sadrian * Copyright (c) 2013 Luiz Otavio O Souza.
3250386Sadrian * Copyright (c) 2011-2012 Stefan Bethke.
4250386Sadrian * Copyright (c) 2012 Adrian Chadd.
5250386Sadrian * All rights reserved.
6250386Sadrian *
7250386Sadrian * Redistribution and use in source and binary forms, with or without
8250386Sadrian * modification, are permitted provided that the following conditions
9250386Sadrian * are met:
10250386Sadrian * 1. Redistributions of source code must retain the above copyright
11250386Sadrian *    notice, this list of conditions and the following disclaimer.
12250386Sadrian * 2. Redistributions in binary form must reproduce the above copyright
13250386Sadrian *    notice, this list of conditions and the following disclaimer in the
14250386Sadrian *    documentation and/or other materials provided with the distribution.
15250386Sadrian *
16250386Sadrian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17250386Sadrian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18250386Sadrian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19250386Sadrian * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20250386Sadrian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21250386Sadrian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22250386Sadrian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23250386Sadrian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24250386Sadrian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25250386Sadrian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26250386Sadrian * SUCH DAMAGE.
27250386Sadrian *
28250386Sadrian * $FreeBSD$
29250386Sadrian */
30250386Sadrian
31250386Sadrian#include <sys/param.h>
32250386Sadrian#include <sys/bus.h>
33250386Sadrian#include <sys/kernel.h>
34250386Sadrian#include <sys/lock.h>
35250386Sadrian#include <sys/mutex.h>
36250386Sadrian#include <sys/systm.h>
37250386Sadrian#include <sys/socket.h>
38250386Sadrian
39250386Sadrian#include <net/if.h>
40250386Sadrian
41250386Sadrian#include <dev/mii/mii.h>
42250386Sadrian
43250386Sadrian#include <dev/etherswitch/etherswitch.h>
44250386Sadrian#include <dev/etherswitch/ip17x/ip17x_phy.h>
45250386Sadrian#include <dev/etherswitch/ip17x/ip17x_reg.h>
46250386Sadrian#include <dev/etherswitch/ip17x/ip17x_var.h>
47250386Sadrian#include <dev/etherswitch/ip17x/ip17x_vlans.h>
48250386Sadrian#include <dev/etherswitch/ip17x/ip175c.h>
49250386Sadrian
50250386Sadrian/*
51250386Sadrian * IP175C specific functions.
52250386Sadrian */
53250386Sadrian
54250386Sadrian/*
55250386Sadrian * Reset the switch.
56250386Sadrian */
57250386Sadrianstatic int
58250386Sadrianip175c_reset(struct ip17x_softc *sc)
59250386Sadrian{
60250386Sadrian	uint32_t data;
61250386Sadrian
62250386Sadrian	/* Reset all the switch settings. */
63250386Sadrian	if (ip17x_writephy(sc->sc_dev, IP175C_RESET_PHY, IP175C_RESET_REG,
64250386Sadrian	    0x175c))
65250386Sadrian		return (-1);
66250386Sadrian	DELAY(2);
67250386Sadrian
68250386Sadrian	/* Force IP175C mode. */
69250386Sadrian	data = ip17x_readphy(sc->sc_dev, IP175C_MODE_PHY, IP175C_MODE_REG);
70250386Sadrian	if (data == 0x175a) {
71250386Sadrian		if (ip17x_writephy(sc->sc_dev, IP175C_MODE_PHY, IP175C_MODE_REG,
72250386Sadrian		    0x175c))
73250386Sadrian		return (-1);
74250386Sadrian	}
75250386Sadrian
76250386Sadrian	return (0);
77250386Sadrian}
78250386Sadrian
79250386Sadrianstatic int
80250386Sadrianip175c_port_vlan_setup(struct ip17x_softc *sc)
81250386Sadrian{
82250386Sadrian	struct ip17x_vlan *v;
83250386Sadrian	uint32_t ports[IP175X_NUM_PORTS], reg[IP175X_NUM_PORTS/2];
84250386Sadrian	int i, err, phy;
85250386Sadrian
86250386Sadrian	KASSERT(sc->cpuport == 5, ("cpuport != 5 not supported for IP175C"));
87250386Sadrian	KASSERT(sc->numports == 6, ("numports != 6 not supported for IP175C"));
88250386Sadrian
89250386Sadrian	/* Build the port access masks. */
90250386Sadrian	memset(ports, 0, sizeof(ports));
91250386Sadrian	for (i = 0; i < sc->info.es_nports; i++) {
92250386Sadrian		phy = sc->portphy[i];
93250386Sadrian		v = &sc->vlan[i];
94250386Sadrian		ports[phy] = v->ports;
95250386Sadrian	}
96250386Sadrian
97250386Sadrian	/* Move the cpuport bit to its correct place. */
98250386Sadrian	for (i = 0; i < sc->numports; i++) {
99250386Sadrian		if (ports[i] & (1 << sc->cpuport)) {
100250386Sadrian			ports[i] |= (1 << 7);
101250386Sadrian			ports[i] &= ~(1 << sc->cpuport);
102250386Sadrian		}
103250386Sadrian	}
104250386Sadrian
105250386Sadrian	/* And now build the switch register data. */
106250386Sadrian	memset(reg, 0, sizeof(reg));
107250386Sadrian	for (i = 0; i < (sc->numports / 2); i++)
108250386Sadrian		reg[i] = ports[i * 2] << 8 | ports[i * 2 + 1];
109250386Sadrian
110250386Sadrian	/* Update the switch resgisters. */
111250386Sadrian	err = ip17x_writephy(sc->sc_dev, 29, 19, reg[0]);
112250386Sadrian	if (err == 0)
113250386Sadrian		err = ip17x_writephy(sc->sc_dev, 29, 20, reg[1]);
114250386Sadrian	if (err == 0)
115250386Sadrian		err = ip17x_updatephy(sc->sc_dev, 29, 21, 0xff00, reg[2]);
116250386Sadrian	if (err == 0)
117250386Sadrian		err = ip17x_updatephy(sc->sc_dev, 30, 18, 0x00ff, reg[2]);
118250386Sadrian	return (err);
119250386Sadrian}
120250386Sadrian
121250386Sadrianstatic int
122250386Sadrianip175c_dot1q_vlan_setup(struct ip17x_softc *sc)
123250386Sadrian{
124250386Sadrian	struct ip17x_vlan *v;
125250386Sadrian	uint32_t data;
126250386Sadrian	uint32_t vlans[IP17X_MAX_VLANS];
127250386Sadrian	int i, j;
128250386Sadrian
129250386Sadrian	KASSERT(sc->cpuport == 5, ("cpuport != 5 not supported for IP175C"));
130250386Sadrian	KASSERT(sc->numports == 6, ("numports != 6 not supported for IP175C"));
131250386Sadrian
132250386Sadrian	/* Add and strip VLAN tags. */
133250386Sadrian	data = (sc->addtag & ~(1 << IP175X_CPU_PORT)) << 11;
134250386Sadrian	data |= (sc->striptag & ~(1 << IP175X_CPU_PORT)) << 6;
135250386Sadrian	if (sc->addtag & (1 << IP175X_CPU_PORT))
136250386Sadrian		data |= (1 << 1);
137250386Sadrian	if (sc->striptag & (1 << IP175X_CPU_PORT))
138250386Sadrian		data |= (1 << 0);
139250386Sadrian	if (ip17x_writephy(sc->sc_dev, 29, 23, data))
140250386Sadrian		return (-1);
141250386Sadrian
142250386Sadrian	/* Set the VID_IDX_SEL to 0. */
143250386Sadrian	if (ip17x_updatephy(sc->sc_dev, 30, 9, 0x70, 0))
144250386Sadrian		return (-1);
145250386Sadrian
146250386Sadrian	/* Calculate the port masks. */
147250386Sadrian	memset(vlans, 0, sizeof(vlans));
148250386Sadrian	for (i = 0; i < IP17X_MAX_VLANS; i++) {
149250386Sadrian		v = &sc->vlan[i];
150253569Sloos		if ((v->vlanid & ETHERSWITCH_VID_VALID) == 0)
151250386Sadrian			continue;
152253569Sloos		vlans[v->vlanid & ETHERSWITCH_VID_MASK] = v->ports;
153250386Sadrian	}
154250386Sadrian
155250386Sadrian	for (j = 0, i = 1; i <= IP17X_MAX_VLANS / 2; i++) {
156250386Sadrian		data = vlans[j++] & 0x3f;
157250386Sadrian		data |= (vlans[j++] & 0x3f) << 8;
158250386Sadrian		if (ip17x_writephy(sc->sc_dev, 30, i, data))
159250386Sadrian			return (-1);
160250386Sadrian	}
161250386Sadrian
162250386Sadrian	/* Port default VLAN ID. */
163250386Sadrian	for (i = 0; i < sc->numports; i++) {
164250386Sadrian		if (i == IP175X_CPU_PORT) {
165250386Sadrian			if (ip17x_writephy(sc->sc_dev, 29, 30, sc->pvid[i]))
166250386Sadrian				return (-1);
167250386Sadrian		} else {
168250386Sadrian			if (ip17x_writephy(sc->sc_dev, 29, 24 + i, sc->pvid[i]))
169250386Sadrian				return (-1);
170250386Sadrian		}
171250386Sadrian	}
172250386Sadrian
173250386Sadrian	return (0);
174250386Sadrian}
175250386Sadrian
176250386Sadrian/*
177250386Sadrian * Set the Switch configuration.
178250386Sadrian */
179250386Sadrianstatic int
180250386Sadrianip175c_hw_setup(struct ip17x_softc *sc)
181250386Sadrian{
182250386Sadrian
183250386Sadrian	switch (sc->vlan_mode) {
184250386Sadrian	case ETHERSWITCH_VLAN_PORT:
185250386Sadrian		return (ip175c_port_vlan_setup(sc));
186250386Sadrian		break;
187250386Sadrian	case ETHERSWITCH_VLAN_DOT1Q:
188250386Sadrian		return (ip175c_dot1q_vlan_setup(sc));
189250386Sadrian		break;
190250386Sadrian	}
191250386Sadrian	return (-1);
192250386Sadrian}
193250386Sadrian
194250386Sadrian/*
195250386Sadrian * Set the switch VLAN mode.
196250386Sadrian */
197250386Sadrianstatic int
198250386Sadrianip175c_set_vlan_mode(struct ip17x_softc *sc, uint32_t mode)
199250386Sadrian{
200250386Sadrian
201250386Sadrian	switch (mode) {
202250386Sadrian	case ETHERSWITCH_VLAN_DOT1Q:
203250386Sadrian		/* Enable VLAN tag processing. */
204250386Sadrian		ip17x_updatephy(sc->sc_dev, 30, 9, 0x80, 0x80);
205250386Sadrian		sc->vlan_mode = mode;
206250386Sadrian		break;
207250386Sadrian	case ETHERSWITCH_VLAN_PORT:
208250386Sadrian	default:
209250386Sadrian		/* Disable VLAN tag processing. */
210250386Sadrian		ip17x_updatephy(sc->sc_dev, 30, 9, 0x80, 0);
211250386Sadrian		sc->vlan_mode = ETHERSWITCH_VLAN_PORT;
212250386Sadrian		break;
213250386Sadrian	};
214250386Sadrian
215250386Sadrian	/* Reset vlans. */
216250386Sadrian	ip17x_reset_vlans(sc, sc->vlan_mode);
217250386Sadrian
218250386Sadrian	/* Update switch configuration. */
219250386Sadrian	ip175c_hw_setup(sc);
220250386Sadrian
221250386Sadrian	return (0);
222250386Sadrian}
223250386Sadrian
224250386Sadrian/*
225250386Sadrian * Get the switch VLAN mode.
226250386Sadrian */
227250386Sadrianstatic int
228250386Sadrianip175c_get_vlan_mode(struct ip17x_softc *sc)
229250386Sadrian{
230250386Sadrian
231250386Sadrian	return (sc->vlan_mode);
232250386Sadrian}
233250386Sadrian
234250386Sadrianvoid
235250386Sadrianip175c_attach(struct ip17x_softc *sc)
236250386Sadrian{
237250386Sadrian
238250386Sadrian	sc->hal.ip17x_reset = ip175c_reset;
239250386Sadrian	sc->hal.ip17x_hw_setup = ip175c_hw_setup;
240250386Sadrian	sc->hal.ip17x_get_vlan_mode = ip175c_get_vlan_mode;
241250386Sadrian	sc->hal.ip17x_set_vlan_mode = ip175c_set_vlan_mode;
242250386Sadrian
243250386Sadrian	/* Defaults for IP175C. */
244250386Sadrian	sc->cpuport = IP175X_CPU_PORT;
245250386Sadrian	sc->numports = IP175X_NUM_PORTS;
246250386Sadrian	sc->info.es_vlan_caps = ETHERSWITCH_VLAN_PORT | ETHERSWITCH_VLAN_DOT1Q;
247250386Sadrian
248250386Sadrian	device_printf(sc->sc_dev, "type: IP175C\n");
249250386Sadrian}
250