1*2a902189SDmitry Salychev /*-
2*2a902189SDmitry Salychev * SPDX-License-Identifier: BSD-2-Clause
3*2a902189SDmitry Salychev *
4*2a902189SDmitry Salychev * Copyright © 2023 Dmitry Salychev
5*2a902189SDmitry Salychev *
6*2a902189SDmitry Salychev * Redistribution and use in source and binary forms, with or without
7*2a902189SDmitry Salychev * modification, are permitted provided that the following conditions
8*2a902189SDmitry Salychev * are met:
9*2a902189SDmitry Salychev * 1. Redistributions of source code must retain the above copyright
10*2a902189SDmitry Salychev * notice, this list of conditions and the following disclaimer.
11*2a902189SDmitry Salychev * 2. Redistributions in binary form must reproduce the above copyright
12*2a902189SDmitry Salychev * notice, this list of conditions and the following disclaimer in the
13*2a902189SDmitry Salychev * documentation and/or other materials provided with the distribution.
14*2a902189SDmitry Salychev *
15*2a902189SDmitry Salychev * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*2a902189SDmitry Salychev * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*2a902189SDmitry Salychev * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*2a902189SDmitry Salychev * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*2a902189SDmitry Salychev * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*2a902189SDmitry Salychev * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*2a902189SDmitry Salychev * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*2a902189SDmitry Salychev * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*2a902189SDmitry Salychev * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*2a902189SDmitry Salychev * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*2a902189SDmitry Salychev * SUCH DAMAGE.
26*2a902189SDmitry Salychev */
27*2a902189SDmitry Salychev
28*2a902189SDmitry Salychev /*
29*2a902189SDmitry Salychev * Small Form Factor (SFF) Committee Pluggable (SFP) Transceiver (FDT-based).
30*2a902189SDmitry Salychev */
31*2a902189SDmitry Salychev
32*2a902189SDmitry Salychev #include <sys/param.h>
33*2a902189SDmitry Salychev #include <sys/kernel.h>
34*2a902189SDmitry Salychev #include <sys/bus.h>
35*2a902189SDmitry Salychev #include <sys/module.h>
36*2a902189SDmitry Salychev
37*2a902189SDmitry Salychev #include <dev/ofw/ofw_bus.h>
38*2a902189SDmitry Salychev #include <dev/ofw/ofw_bus_subr.h>
39*2a902189SDmitry Salychev #include <dev/fdt/simplebus.h>
40*2a902189SDmitry Salychev
41*2a902189SDmitry Salychev #include "sff_if.h"
42*2a902189SDmitry Salychev
43*2a902189SDmitry Salychev struct sfp_fdt_softc {
44*2a902189SDmitry Salychev phandle_t ofw_node;
45*2a902189SDmitry Salychev phandle_t i2c_bus;
46*2a902189SDmitry Salychev
47*2a902189SDmitry Salychev phandle_t mod_def;
48*2a902189SDmitry Salychev phandle_t los;
49*2a902189SDmitry Salychev phandle_t tx_fault;
50*2a902189SDmitry Salychev phandle_t tx_disable;
51*2a902189SDmitry Salychev phandle_t rx_rate;
52*2a902189SDmitry Salychev phandle_t tx_rate;
53*2a902189SDmitry Salychev uint32_t max_power; /* in mW */
54*2a902189SDmitry Salychev };
55*2a902189SDmitry Salychev
56*2a902189SDmitry Salychev static int
sfp_fdt_probe(device_t dev)57*2a902189SDmitry Salychev sfp_fdt_probe(device_t dev)
58*2a902189SDmitry Salychev {
59*2a902189SDmitry Salychev phandle_t node;
60*2a902189SDmitry Salychev ssize_t s;
61*2a902189SDmitry Salychev
62*2a902189SDmitry Salychev node = ofw_bus_get_node(dev);
63*2a902189SDmitry Salychev if (!ofw_bus_node_is_compatible(node, "sff,sfp"))
64*2a902189SDmitry Salychev return (ENXIO);
65*2a902189SDmitry Salychev
66*2a902189SDmitry Salychev s = device_get_property(dev, "i2c-bus", &node, sizeof(node),
67*2a902189SDmitry Salychev DEVICE_PROP_HANDLE);
68*2a902189SDmitry Salychev if (s == -1) {
69*2a902189SDmitry Salychev device_printf(dev, "%s: '%s' has no 'i2c-bus' property, s %zd\n",
70*2a902189SDmitry Salychev __func__, ofw_bus_get_name(dev), s);
71*2a902189SDmitry Salychev return (ENXIO);
72*2a902189SDmitry Salychev }
73*2a902189SDmitry Salychev
74*2a902189SDmitry Salychev device_set_desc(dev, "Small Form-factor Pluggable Transceiver");
75*2a902189SDmitry Salychev return (BUS_PROBE_DEFAULT);
76*2a902189SDmitry Salychev }
77*2a902189SDmitry Salychev
78*2a902189SDmitry Salychev static int
sfp_fdt_attach(device_t dev)79*2a902189SDmitry Salychev sfp_fdt_attach(device_t dev)
80*2a902189SDmitry Salychev {
81*2a902189SDmitry Salychev struct sfp_fdt_softc *sc;
82*2a902189SDmitry Salychev ssize_t s;
83*2a902189SDmitry Salychev int error;
84*2a902189SDmitry Salychev
85*2a902189SDmitry Salychev sc = device_get_softc(dev);
86*2a902189SDmitry Salychev sc->ofw_node = ofw_bus_get_node(dev);
87*2a902189SDmitry Salychev
88*2a902189SDmitry Salychev s = device_get_property(dev, "i2c-bus", &sc->i2c_bus,
89*2a902189SDmitry Salychev sizeof(sc->i2c_bus), DEVICE_PROP_HANDLE);
90*2a902189SDmitry Salychev if (s == -1) {
91*2a902189SDmitry Salychev device_printf(dev, "%s: cannot find 'i2c-bus' property: %zd\n",
92*2a902189SDmitry Salychev __func__, s);
93*2a902189SDmitry Salychev return (ENXIO);
94*2a902189SDmitry Salychev }
95*2a902189SDmitry Salychev
96*2a902189SDmitry Salychev /* Optional properties */
97*2a902189SDmitry Salychev (void)device_get_property(dev, "mod-def0-gpios", &sc->mod_def,
98*2a902189SDmitry Salychev sizeof(sc->mod_def), DEVICE_PROP_HANDLE);
99*2a902189SDmitry Salychev (void)device_get_property(dev, "los-gpios", &sc->los, sizeof(sc->los),
100*2a902189SDmitry Salychev DEVICE_PROP_HANDLE);
101*2a902189SDmitry Salychev (void)device_get_property(dev, "tx-fault-gpios", &sc->tx_fault,
102*2a902189SDmitry Salychev sizeof(sc->tx_fault), DEVICE_PROP_HANDLE);
103*2a902189SDmitry Salychev (void)device_get_property(dev, "tx-disable-gpios", &sc->tx_disable,
104*2a902189SDmitry Salychev sizeof(sc->tx_disable), DEVICE_PROP_HANDLE);
105*2a902189SDmitry Salychev (void)device_get_property(dev, "rate-select0-gpios", &sc->rx_rate,
106*2a902189SDmitry Salychev sizeof(sc->rx_rate), DEVICE_PROP_HANDLE);
107*2a902189SDmitry Salychev (void)device_get_property(dev, "rate-select1-gpios", &sc->tx_rate,
108*2a902189SDmitry Salychev sizeof(sc->tx_rate), DEVICE_PROP_HANDLE);
109*2a902189SDmitry Salychev (void)device_get_property(dev, "maximum-power-milliwatt", &sc->max_power,
110*2a902189SDmitry Salychev sizeof(sc->max_power), DEVICE_PROP_UINT32);
111*2a902189SDmitry Salychev
112*2a902189SDmitry Salychev error = OF_device_register_xref(OF_xref_from_node(sc->ofw_node), dev);
113*2a902189SDmitry Salychev if (error != 0)
114*2a902189SDmitry Salychev device_printf(dev, "%s: failed to register xref %#x\n",
115*2a902189SDmitry Salychev __func__, OF_xref_from_node(sc->ofw_node));
116*2a902189SDmitry Salychev
117*2a902189SDmitry Salychev return (error);
118*2a902189SDmitry Salychev }
119*2a902189SDmitry Salychev
120*2a902189SDmitry Salychev static int
sfp_fdt_get_i2c_bus(device_t dev,device_t * i2c_bus)121*2a902189SDmitry Salychev sfp_fdt_get_i2c_bus(device_t dev, device_t *i2c_bus)
122*2a902189SDmitry Salychev {
123*2a902189SDmitry Salychev struct sfp_fdt_softc *sc;
124*2a902189SDmitry Salychev device_t xdev;
125*2a902189SDmitry Salychev
126*2a902189SDmitry Salychev KASSERT(i2c_bus != NULL, ("%s: i2c_bus is NULL", __func__));
127*2a902189SDmitry Salychev
128*2a902189SDmitry Salychev sc = device_get_softc(dev);
129*2a902189SDmitry Salychev xdev = OF_device_from_xref(OF_xref_from_node(sc->i2c_bus));
130*2a902189SDmitry Salychev if (xdev == NULL)
131*2a902189SDmitry Salychev return (ENXIO);
132*2a902189SDmitry Salychev
133*2a902189SDmitry Salychev *i2c_bus = xdev;
134*2a902189SDmitry Salychev return (0);
135*2a902189SDmitry Salychev }
136*2a902189SDmitry Salychev
137*2a902189SDmitry Salychev static device_method_t sfp_fdt_methods[] = {
138*2a902189SDmitry Salychev /* Device interface */
139*2a902189SDmitry Salychev DEVMETHOD(device_probe, sfp_fdt_probe),
140*2a902189SDmitry Salychev DEVMETHOD(device_attach, sfp_fdt_attach),
141*2a902189SDmitry Salychev
142*2a902189SDmitry Salychev /* SFF */
143*2a902189SDmitry Salychev DEVMETHOD(sff_get_i2c_bus, sfp_fdt_get_i2c_bus),
144*2a902189SDmitry Salychev
145*2a902189SDmitry Salychev DEVMETHOD_END
146*2a902189SDmitry Salychev };
147*2a902189SDmitry Salychev
148*2a902189SDmitry Salychev DEFINE_CLASS_0(sfp_fdt, sfp_fdt_driver, sfp_fdt_methods,
149*2a902189SDmitry Salychev sizeof(struct sfp_fdt_softc));
150*2a902189SDmitry Salychev
151*2a902189SDmitry Salychev EARLY_DRIVER_MODULE(sfp_fdt, simplebus, sfp_fdt_driver, 0, 0,
152*2a902189SDmitry Salychev BUS_PASS_SUPPORTDEV);
153*2a902189SDmitry Salychev EARLY_DRIVER_MODULE(sfp_fdt, ofwbus, sfp_fdt_driver, 0, 0,
154*2a902189SDmitry Salychev BUS_PASS_SUPPORTDEV);
155