aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/neta/if_mvneta_fdt.c
blob: 67b31bddbdf119347e26188278f8351e671d682d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
 * Copyright (c) 2017 Stormshield.
 * Copyright (c) 2017 Semihalf.
 * All rights reserved.
 *
 * 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 AUTHOR ``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 AUTHOR 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.
 */

#include "opt_platform.h"
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <sys/rman.h>
#include <sys/socket.h>
#include <sys/taskqueue.h>

#include <net/ethernet.h>
#include <net/if.h>
#include <net/if_media.h>

#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp_lro.h>

#include <machine/bus.h>
#include <machine/resource.h>

#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>

#include <dev/mii/mii.h>
#include <dev/mii/miivar.h>

#include "if_mvnetareg.h"
#include "if_mvnetavar.h"

#define	PHY_MODE_MAXLEN	10
#define	INBAND_STATUS_MAXLEN 16

static int mvneta_fdt_probe(device_t);
static int mvneta_fdt_attach(device_t);

static device_method_t mvneta_fdt_methods[] = {
	/* Device interface */
	DEVMETHOD(device_probe,		mvneta_fdt_probe),
	DEVMETHOD(device_attach,	mvneta_fdt_attach),

	/* End */
	DEVMETHOD_END
};

DEFINE_CLASS_1(mvneta, mvneta_fdt_driver, mvneta_fdt_methods,
    sizeof(struct mvneta_softc), mvneta_driver);

static devclass_t mvneta_fdt_devclass;

DRIVER_MODULE(mvneta, ofwbus, mvneta_fdt_driver, mvneta_fdt_devclass, 0, 0);
DRIVER_MODULE(mvneta, simplebus, mvneta_fdt_driver, mvneta_fdt_devclass, 0, 0);

static int mvneta_fdt_phy_acquire(device_t);

static int
mvneta_fdt_probe(device_t dev)
{

	if (!ofw_bus_status_okay(dev))
		return (ENXIO);

	if (!ofw_bus_is_compatible(dev, "marvell,armada-370-neta"))
		return (ENXIO);

	device_set_desc(dev, "NETA controller");
	return (BUS_PROBE_DEFAULT);
}

static int
mvneta_fdt_attach(device_t dev)
{
	int err;

	/* Try to fetch PHY information from FDT */
	err = mvneta_fdt_phy_acquire(dev);
	if (err != 0)
		return (err);

	return (mvneta_attach(dev));
}

static int
mvneta_fdt_phy_acquire(device_t dev)
{
	struct mvneta_softc *sc;
	phandle_t node, child, phy_handle;
	char phymode[PHY_MODE_MAXLEN];
	char managed[INBAND_STATUS_MAXLEN];
	char *name;

	sc = device_get_softc(dev);
	node = ofw_bus_get_node(dev);

	/* PHY mode is crucial */
	if (OF_getprop(node, "phy-mode", phymode, sizeof(phymode)) <= 0) {
		device_printf(dev, "Failed to acquire PHY mode from FDT.\n");
		return (ENXIO);
	}

	if (strncmp(phymode, "rgmii-id", 8) == 0)
		sc->phy_mode = MVNETA_PHY_RGMII_ID;
	else if (strncmp(phymode, "rgmii", 5) == 0)
		sc->phy_mode = MVNETA_PHY_RGMII;
	else if (strncmp(phymode, "sgmii", 5) == 0)
		sc->phy_mode = MVNETA_PHY_SGMII;
	else if (strncmp(phymode, "qsgmii", 6) == 0)
		sc->phy_mode = MVNETA_PHY_QSGMII;
	else
		sc->phy_mode = MVNETA_PHY_SGMII;

	/* Check if in-band link status will be used */
	if (OF_getprop(node, "managed", managed, sizeof(managed)) > 0) {
		if (strncmp(managed, "in-band-status", 14) == 0) {
			sc->use_inband_status = TRUE;
			device_printf(dev, "Use in-band link status.\n");
			return (0);
		}
	}

	if (OF_getencprop(node, "phy", (void *)&phy_handle,
	    sizeof(phy_handle)) <= 0) {
		/* Test for fixed-link (present i.e. in 388-gp) */
		for (child = OF_child(node); child != 0; child = OF_peer(child)) {
			if (OF_getprop_alloc(child,
			    "name", 1, (void **)&name) <= 0) {
				continue;
			}
			if (strncmp(name, "fixed-link", 10) == 0) {
				free(name, M_OFWPROP);
				if (OF_getencprop(child, "speed",
				    &sc->phy_speed, sizeof(sc->phy_speed)) <= 0) {
					if (bootverbose) {
						device_printf(dev,
						    "No PHY information.\n");
					}
					return (ENXIO);
				}
				if (OF_hasprop(child, "full-duplex"))
					sc->phy_fdx = TRUE;
				else
					sc->phy_fdx = FALSE;

				/* Keep this flag just for the record */
				sc->phy_addr = MII_PHY_ANY;

				return (0);
			}
			free(name, M_OFWPROP);
		}
		if (bootverbose) {
			device_printf(dev,
			    "Could not find PHY information in FDT.\n");
		}
		return (ENXIO);
	} else {
		phy_handle = OF_instance_to_package(phy_handle);
		if (OF_getencprop(phy_handle, "reg", &sc->phy_addr,
		    sizeof(sc->phy_addr)) <= 0) {
			device_printf(dev,
			    "Could not find PHY address in FDT.\n");
			return (ENXIO);
		}
	}

	return (0);
}

int
mvneta_fdt_mac_address(struct mvneta_softc *sc, uint8_t *addr)
{
	phandle_t node;
	uint8_t lmac[ETHER_ADDR_LEN];
	uint8_t zeromac[] = {[0 ... (ETHER_ADDR_LEN - 1)] = 0};
	int len;

	/*
	 * Retrieve hw address from the device tree.
	 */
	node = ofw_bus_get_node(sc->dev);
	if (node == 0)
		return (ENXIO);

	len = OF_getprop(node, "local-mac-address", (void *)lmac, sizeof(lmac));
	if (len != ETHER_ADDR_LEN)
		return (ENOENT);

	if (memcmp(lmac, zeromac, ETHER_ADDR_LEN) == 0) {
		/* Invalid MAC address (all zeros) */
		return (EINVAL);
	}
	memcpy(addr, lmac, ETHER_ADDR_LEN);

	return (0);
}