2 * Copyright (c) 2013 Luiz Otavio O Souza.
3 * Copyright (c) 2011-2012 Stefan Bethke.
4 * Copyright (c) 2012 Adrian Chadd.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include "opt_platform.h"
33 #include <sys/param.h>
35 #include <sys/errno.h>
36 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/socket.h>
42 #include <sys/sockio.h>
43 #include <sys/sysctl.h>
44 #include <sys/systm.h>
45 #include <sys/types.h>
48 #include <net/ethernet.h>
49 #include <net/if_media.h>
50 #include <net/if_types.h>
51 #include <net/if_var.h>
53 #include <machine/bus.h>
54 #include <dev/mii/mii.h>
55 #include <dev/mii/miivar.h>
56 #include <dev/mdio/mdio.h>
58 #include <dev/etherswitch/etherswitch.h>
59 #include <dev/etherswitch/ip17x/ip17x_phy.h>
60 #include <dev/etherswitch/ip17x/ip17x_reg.h>
61 #include <dev/etherswitch/ip17x/ip17x_var.h>
62 #include <dev/etherswitch/ip17x/ip17x_vlans.h>
63 #include <dev/etherswitch/ip17x/ip175c.h>
64 #include <dev/etherswitch/ip17x/ip175d.h>
67 #include <dev/fdt/fdt_common.h>
68 #include <dev/ofw/ofw_bus.h>
69 #include <dev/ofw/ofw_bus_subr.h>
73 #include "miibus_if.h"
74 #include "etherswitch_if.h"
76 MALLOC_DECLARE(M_IP17X);
77 MALLOC_DEFINE(M_IP17X, "ip17x", "ip17x data structures");
79 static void ip17x_tick(void *);
80 static int ip17x_ifmedia_upd(struct ifnet *);
81 static void ip17x_ifmedia_sts(struct ifnet *, struct ifmediareq *);
84 ip17x_identify(driver_t *driver, device_t parent)
86 if (device_find_child(parent, "ip17x", -1) == NULL)
87 BUS_ADD_CHILD(parent, 0, "ip17x", -1);
91 ip17x_probe(device_t dev)
93 struct ip17x_softc *sc;
94 uint32_t oui, model, phy_id1, phy_id2;
99 ip17x_node = fdt_find_compatible(OF_finddevice("/"),
106 sc = device_get_softc(dev);
108 /* Read ID from PHY 0. */
109 phy_id1 = MDIO_READREG(device_get_parent(dev), 0, MII_PHYIDR1);
110 phy_id2 = MDIO_READREG(device_get_parent(dev), 0, MII_PHYIDR2);
112 oui = MII_OUI(phy_id1, phy_id2);
113 model = MII_MODEL(phy_id2);
114 /* We only care about IC+ devices. */
115 if (oui != IP17X_OUI) {
117 "Unsupported IC+ switch. Unknown OUI: %#x\n", oui);
123 sc->sc_switchtype = IP17X_SWITCH_IP175A;
126 sc->sc_switchtype = IP17X_SWITCH_IP175C;
129 device_printf(dev, "Unsupported IC+ switch model: %#x\n",
134 /* IP175D has a specific ID register. */
135 model = MDIO_READREG(device_get_parent(dev), IP175D_ID_PHY,
138 sc->sc_switchtype = IP17X_SWITCH_IP175D;
140 /* IP178 has more PHYs. Try it. */
141 model = MDIO_READREG(device_get_parent(dev), 5, MII_PHYIDR1);
142 if (phy_id1 == model)
143 sc->sc_switchtype = IP17X_SWITCH_IP178C;
148 if ((OF_getencprop(ip17x_node, "mii-poll",
149 &cell, sizeof(cell))) > 0)
150 sc->miipoll = cell ? 1 : 0;
152 (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
153 "mii-poll", &sc->miipoll);
155 device_set_desc_copy(dev, "IC+ IP17x switch driver");
156 return (BUS_PROBE_DEFAULT);
160 ip17x_attach_phys(struct ip17x_softc *sc)
167 /* PHYs need an interface, so we generate a dummy one */
168 snprintf(name, IFNAMSIZ, "%sport", device_get_nameunit(sc->sc_dev));
169 for (phy = 0; phy < MII_NPHY; phy++) {
170 if (((1 << phy) & sc->phymask) == 0)
172 sc->phyport[phy] = port;
173 sc->portphy[port] = phy;
174 sc->ifp[port] = if_alloc(IFT_ETHER);
175 sc->ifp[port]->if_softc = sc;
176 sc->ifp[port]->if_flags |= IFF_UP | IFF_BROADCAST |
177 IFF_DRV_RUNNING | IFF_SIMPLEX;
178 if_initname(sc->ifp[port], name, port);
179 sc->miibus[port] = malloc(sizeof(device_t), M_IP17X,
181 err = mii_attach(sc->sc_dev, sc->miibus[port], sc->ifp[port],
182 ip17x_ifmedia_upd, ip17x_ifmedia_sts, \
183 BMSR_DEFCAPMASK, phy, MII_OFFSET_ANY, 0);
184 DPRINTF(sc->sc_dev, "%s attached to pseudo interface %s\n",
185 device_get_nameunit(*sc->miibus[port]),
186 sc->ifp[port]->if_xname);
188 device_printf(sc->sc_dev,
189 "attaching PHY %d failed\n",
193 sc->info.es_nports = port + 1;
194 if (++port >= sc->numports)
201 ip17x_attach(device_t dev)
203 struct ip17x_softc *sc;
206 sc = device_get_softc(dev);
209 mtx_init(&sc->sc_mtx, "ip17x", NULL, MTX_DEF);
210 strlcpy(sc->info.es_name, device_get_desc(dev),
211 sizeof(sc->info.es_name));
217 (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
218 "phymask", &sc->phymask);
220 /* Number of vlans supported by the switch. */
221 sc->info.es_nvlangroups = IP17X_MAX_VLANS;
223 /* Attach the switch related functions. */
224 if (IP17X_IS_SWITCH(sc, IP175C))
226 else if (IP17X_IS_SWITCH(sc, IP175D))
229 /* We don't have support to all the models yet :-/ */
232 /* Always attach the cpu port. */
233 sc->phymask |= (1 << sc->cpuport);
235 sc->ifp = malloc(sizeof(struct ifnet *) * sc->numports, M_IP17X,
237 sc->pvid = malloc(sizeof(uint32_t) * sc->numports, M_IP17X,
239 sc->miibus = malloc(sizeof(device_t *) * sc->numports, M_IP17X,
241 sc->portphy = malloc(sizeof(int) * sc->numports, M_IP17X,
244 /* Initialize the switch. */
245 sc->hal.ip17x_reset(sc);
248 * Attach the PHYs and complete the bus enumeration.
250 err = ip17x_attach_phys(sc);
255 * Set the switch to port based vlans or disabled (if not supported
258 sc->hal.ip17x_set_vlan_mode(sc, ETHERSWITCH_VLAN_PORT);
260 bus_generic_probe(dev);
261 bus_enumerate_hinted_children(dev);
262 err = bus_generic_attach(dev);
267 callout_init(&sc->callout_tick, 0);
276 ip17x_detach(device_t dev)
278 struct ip17x_softc *sc;
281 sc = device_get_softc(dev);
283 callout_drain(&sc->callout_tick);
285 for (i=0; i < MII_NPHY; i++) {
286 if (((1 << i) & sc->phymask) == 0)
288 port = sc->phyport[i];
289 if (sc->miibus[port] != NULL)
290 device_delete_child(dev, (*sc->miibus[port]));
291 if (sc->ifp[port] != NULL)
292 if_free(sc->ifp[port]);
293 free(sc->miibus[port], M_IP17X);
296 free(sc->portphy, M_IP17X);
297 free(sc->miibus, M_IP17X);
298 free(sc->pvid, M_IP17X);
299 free(sc->ifp, M_IP17X);
301 /* Reset the switch. */
302 sc->hal.ip17x_reset(sc);
304 bus_generic_detach(dev);
305 mtx_destroy(&sc->sc_mtx);
310 static inline struct mii_data *
311 ip17x_miiforport(struct ip17x_softc *sc, int port)
314 if (port < 0 || port > sc->numports)
316 return (device_get_softc(*sc->miibus[port]));
319 static inline struct ifnet *
320 ip17x_ifpforport(struct ip17x_softc *sc, int port)
323 if (port < 0 || port > sc->numports)
325 return (sc->ifp[port]);
329 * Poll the status for all PHYs.
332 ip17x_miipollstat(struct ip17x_softc *sc)
334 struct mii_softc *miisc;
335 struct mii_data *mii;
338 IP17X_LOCK_ASSERT(sc, MA_NOTOWNED);
340 for (i = 0; i < MII_NPHY; i++) {
341 if (((1 << i) & sc->phymask) == 0)
343 port = sc->phyport[i];
344 if ((*sc->miibus[port]) == NULL)
346 mii = device_get_softc(*sc->miibus[port]);
347 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) {
348 if (IFM_INST(mii->mii_media.ifm_cur->ifm_media) !=
352 mii_phy_update(miisc, MII_POLLSTAT);
358 ip17x_tick(void *arg)
360 struct ip17x_softc *sc;
363 ip17x_miipollstat(sc);
364 callout_reset(&sc->callout_tick, hz, ip17x_tick, sc);
368 ip17x_lock(device_t dev)
370 struct ip17x_softc *sc;
372 sc = device_get_softc(dev);
373 IP17X_LOCK_ASSERT(sc, MA_NOTOWNED);
378 ip17x_unlock(device_t dev)
380 struct ip17x_softc *sc;
382 sc = device_get_softc(dev);
383 IP17X_LOCK_ASSERT(sc, MA_OWNED);
387 static etherswitch_info_t *
388 ip17x_getinfo(device_t dev)
390 struct ip17x_softc *sc;
392 sc = device_get_softc(dev);
397 ip17x_getport(device_t dev, etherswitch_port_t *p)
399 struct ip17x_softc *sc;
400 struct ifmediareq *ifmr;
401 struct mii_data *mii;
404 sc = device_get_softc(dev);
405 if (p->es_port < 0 || p->es_port >= sc->numports)
408 phy = sc->portphy[p->es_port];
410 /* Retrieve the PVID. */
411 p->es_pvid = sc->pvid[phy];
414 if (sc->addtag & (1 << phy))
415 p->es_flags |= ETHERSWITCH_PORT_ADDTAG;
416 if (sc->striptag & (1 << phy))
417 p->es_flags |= ETHERSWITCH_PORT_STRIPTAG;
421 /* No media settings ? */
422 if (p->es_ifmr.ifm_count == 0)
425 mii = ip17x_miiforport(sc, p->es_port);
428 if (phy == sc->cpuport) {
429 /* fill in fixed values for CPU port */
430 p->es_flags |= ETHERSWITCH_PORT_CPU;
432 if (sc->media == 100)
433 ifmr->ifm_current = ifmr->ifm_active =
434 IFM_ETHER | IFM_100_TX | IFM_FDX;
436 ifmr->ifm_current = ifmr->ifm_active =
437 IFM_ETHER | IFM_1000_T | IFM_FDX;
439 ifmr->ifm_status = IFM_ACTIVE | IFM_AVALID;
441 err = ifmedia_ioctl(mii->mii_ifp, &p->es_ifr,
442 &mii->mii_media, SIOCGIFMEDIA);
450 ip17x_setport(device_t dev, etherswitch_port_t *p)
452 struct ip17x_softc *sc;
455 struct mii_data *mii;
458 sc = device_get_softc(dev);
459 if (p->es_port < 0 || p->es_port >= sc->numports)
462 phy = sc->portphy[p->es_port];
463 ifp = ip17x_ifpforport(sc, p->es_port);
464 mii = ip17x_miiforport(sc, p->es_port);
465 if (ifp == NULL || mii == NULL)
469 if (sc->vlan_mode == ETHERSWITCH_VLAN_DOT1Q) {
472 if (p->es_pvid != 0) {
473 if (IP17X_IS_SWITCH(sc, IP175C) &&
474 p->es_pvid > IP175C_LAST_VLAN)
476 sc->pvid[phy] = p->es_pvid;
479 /* Mutually exclusive. */
480 if (p->es_flags & ETHERSWITCH_PORT_ADDTAG &&
481 p->es_flags & ETHERSWITCH_PORT_STRIPTAG)
484 /* Reset the settings for this port. */
485 sc->addtag &= ~(1 << phy);
486 sc->striptag &= ~(1 << phy);
488 /* And then set it to the new value. */
489 if (p->es_flags & ETHERSWITCH_PORT_ADDTAG)
490 sc->addtag |= (1 << phy);
491 if (p->es_flags & ETHERSWITCH_PORT_STRIPTAG)
492 sc->striptag |= (1 << phy);
495 /* Update the switch configuration. */
496 if (sc->hal.ip17x_hw_setup(sc))
499 /* Do not allow media changes on CPU port. */
500 if (phy == sc->cpuport)
503 /* No media settings ? */
504 if (p->es_ifmr.ifm_count == 0)
507 ifm = &mii->mii_media;
508 return (ifmedia_ioctl(ifp, &p->es_ifr, ifm, SIOCSIFMEDIA));
512 ip17x_statchg(device_t dev)
515 DPRINTF(dev, "%s\n", __func__);
519 ip17x_ifmedia_upd(struct ifnet *ifp)
521 struct ip17x_softc *sc;
522 struct mii_data *mii;
525 DPRINTF(sc->sc_dev, "%s\n", __func__);
526 mii = ip17x_miiforport(sc, ifp->if_dunit);
535 ip17x_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
537 struct ip17x_softc *sc;
538 struct mii_data *mii;
541 DPRINTF(sc->sc_dev, "%s\n", __func__);
542 mii = ip17x_miiforport(sc, ifp->if_dunit);
546 ifmr->ifm_active = mii->mii_media_active;
547 ifmr->ifm_status = mii->mii_media_status;
551 ip17x_readreg(device_t dev, int addr)
553 struct ip17x_softc *sc;
555 sc = device_get_softc(dev);
556 IP17X_LOCK_ASSERT(sc, MA_OWNED);
563 ip17x_writereg(device_t dev, int addr, int value)
565 struct ip17x_softc *sc;
567 sc = device_get_softc(dev);
568 IP17X_LOCK_ASSERT(sc, MA_OWNED);
575 ip17x_getconf(device_t dev, etherswitch_conf_t *conf)
577 struct ip17x_softc *sc;
579 sc = device_get_softc(dev);
581 /* Return the VLAN mode. */
582 conf->cmd = ETHERSWITCH_CONF_VLAN_MODE;
583 conf->vlan_mode = sc->hal.ip17x_get_vlan_mode(sc);
589 ip17x_setconf(device_t dev, etherswitch_conf_t *conf)
591 struct ip17x_softc *sc;
593 sc = device_get_softc(dev);
595 /* Set the VLAN mode. */
596 if (conf->cmd & ETHERSWITCH_CONF_VLAN_MODE)
597 sc->hal.ip17x_set_vlan_mode(sc, conf->vlan_mode);
602 static device_method_t ip17x_methods[] = {
603 /* Device interface */
604 DEVMETHOD(device_identify, ip17x_identify),
605 DEVMETHOD(device_probe, ip17x_probe),
606 DEVMETHOD(device_attach, ip17x_attach),
607 DEVMETHOD(device_detach, ip17x_detach),
610 DEVMETHOD(bus_add_child, device_add_child_ordered),
613 DEVMETHOD(miibus_readreg, ip17x_readphy),
614 DEVMETHOD(miibus_writereg, ip17x_writephy),
615 DEVMETHOD(miibus_statchg, ip17x_statchg),
618 DEVMETHOD(mdio_readreg, ip17x_readphy),
619 DEVMETHOD(mdio_writereg, ip17x_writephy),
621 /* etherswitch interface */
622 DEVMETHOD(etherswitch_lock, ip17x_lock),
623 DEVMETHOD(etherswitch_unlock, ip17x_unlock),
624 DEVMETHOD(etherswitch_getinfo, ip17x_getinfo),
625 DEVMETHOD(etherswitch_readreg, ip17x_readreg),
626 DEVMETHOD(etherswitch_writereg, ip17x_writereg),
627 DEVMETHOD(etherswitch_readphyreg, ip17x_readphy),
628 DEVMETHOD(etherswitch_writephyreg, ip17x_writephy),
629 DEVMETHOD(etherswitch_getport, ip17x_getport),
630 DEVMETHOD(etherswitch_setport, ip17x_setport),
631 DEVMETHOD(etherswitch_getvgroup, ip17x_getvgroup),
632 DEVMETHOD(etherswitch_setvgroup, ip17x_setvgroup),
633 DEVMETHOD(etherswitch_getconf, ip17x_getconf),
634 DEVMETHOD(etherswitch_setconf, ip17x_setconf),
639 DEFINE_CLASS_0(ip17x, ip17x_driver, ip17x_methods,
640 sizeof(struct ip17x_softc));
641 static devclass_t ip17x_devclass;
643 DRIVER_MODULE(ip17x, mdio, ip17x_driver, ip17x_devclass, 0, 0);
644 DRIVER_MODULE(miibus, ip17x, miibus_driver, miibus_devclass, 0, 0);
645 DRIVER_MODULE(etherswitch, ip17x, etherswitch_driver, etherswitch_devclass, 0, 0);
646 MODULE_VERSION(ip17x, 1);
649 MODULE_DEPEND(ip17x, mdio, 1, 1, 1); /* XXX which versions? */
651 DRIVER_MODULE(mdio, ip17x, mdio_driver, mdio_devclass, 0, 0);
652 MODULE_DEPEND(ip17x, miibus, 1, 1, 1); /* XXX which versions? */
653 MODULE_DEPEND(ip17x, etherswitch, 1, 1, 1); /* XXX which versions? */