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