xref: /freebsd/sys/arm/annapurna/alpine/alpine_serdes.c (revision 5dae51da3da0cc94d17bd67b308fad304ebec7e0)
1 /*-
2  * Copyright (c) 2015,2016 Annapurna Labs Ltd. and affiliates
3  * All rights reserved.
4  *
5  * Developed by Semihalf.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/bus.h>
37 #include <sys/rman.h>
38 #include <sys/conf.h>
39 #include <sys/resource.h>
40 
41 #include <machine/bus.h>
42 #include <dev/ofw/ofw_bus_subr.h>
43 
44 #include "al_serdes.h"
45 #include "alpine_serdes.h"
46 
47 #define SERDES_NUM_GROUPS	5
48 
49 static void *serdes_base;
50 static uint32_t serdes_grp_offset[] = {0, 0x400, 0x800, 0xc00, 0x2000};
51 
52 static struct alpine_serdes_eth_group_mode {
53 	struct mtx			lock;
54 	enum alpine_serdes_eth_mode	mode;
55 	bool				mode_set;
56 } alpine_serdes_eth_group_mode[SERDES_NUM_GROUPS];
57 
58 static int al_serdes_probe(device_t dev);
59 static int al_serdes_attach(device_t dev);
60 static int al_serdes_detach(device_t dev);
61 
62 static struct resource_spec al_serdes_spec[] = {
63 	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
64 	{ -1, 0 }
65 };
66 
67 struct al_serdes_softc {
68 	struct resource *res;
69 };
70 
71 static device_method_t al_serdes_methods[] = {
72 	DEVMETHOD(device_probe,		al_serdes_probe),
73 	DEVMETHOD(device_attach,	al_serdes_attach),
74 	DEVMETHOD(device_detach,	al_serdes_detach),
75 
76 	DEVMETHOD_END
77 };
78 
79 static driver_t al_serdes_driver = {
80 	"serdes",
81 	al_serdes_methods,
82 	sizeof(struct al_serdes_softc)
83 };
84 
85 static devclass_t al_serdes_devclass;
86 
87 DRIVER_MODULE(al_serdes, simplebus, al_serdes_driver,
88     al_serdes_devclass, 0, 0);
89 DRIVER_MODULE(al_serdes, ofwbus, al_serdes_driver,
90     al_serdes_devclass, 0, 0);
91 
92 static int
93 al_serdes_probe(device_t dev)
94 {
95 
96 	if (!ofw_bus_status_okay(dev))
97 		return (ENXIO);
98 
99 	if (!ofw_bus_is_compatible(dev, "annapurna-labs,al-serdes"))
100 		return (ENXIO);
101 
102 	device_set_desc(dev, "Alpine Serdes");
103 
104 	return (BUS_PROBE_DEFAULT);
105 }
106 
107 static int
108 al_serdes_attach(device_t dev)
109 {
110 	struct al_serdes_softc *sc;
111 	int err;
112 
113 	sc = device_get_softc(dev);
114 
115 	err = bus_alloc_resources(dev, al_serdes_spec, &sc->res);
116 	if (err != 0) {
117 		device_printf(dev, "could not allocate resources\n");
118 		return (err);
119 	}
120 
121 	/* Initialize Serdes group locks and mode */
122 	for (int i = 0; i < nitems(alpine_serdes_eth_group_mode); i++) {
123 		mtx_init(&alpine_serdes_eth_group_mode[i].lock, "AlSerdesMtx",
124 		    NULL, MTX_DEF);
125 		alpine_serdes_eth_group_mode[i].mode_set = false;
126 	}
127 
128 	serdes_base = (void *)rman_get_bushandle(sc->res);
129 
130 	return (0);
131 }
132 
133 static int
134 al_serdes_detach(device_t dev)
135 {
136 	struct al_serdes_softc *sc;
137 
138 	sc = device_get_softc(dev);
139 
140 	bus_release_resources(dev, al_serdes_spec, &sc->res);
141 
142 	for (int i = 0; i < nitems(alpine_serdes_eth_group_mode); i++) {
143 		mtx_destroy(&alpine_serdes_eth_group_mode[i].lock);
144 		alpine_serdes_eth_group_mode[i].mode_set = false;
145 	}
146 
147 	return (0);
148 }
149 
150 void *
151 alpine_serdes_resource_get(uint32_t group)
152 {
153 	void *base;
154 
155 	base = NULL;
156 	if (group >= SERDES_NUM_GROUPS)
157 		return (NULL);
158 
159 	if (serdes_base != NULL)
160 		base = (void *)((uintptr_t)serdes_base +
161 		    serdes_grp_offset[group]);
162 
163 	return (base);
164 }
165 
166 int
167 alpine_serdes_eth_mode_set(uint32_t group, enum alpine_serdes_eth_mode mode)
168 {
169 	struct alpine_serdes_eth_group_mode *group_mode;
170 
171 	group_mode = &alpine_serdes_eth_group_mode[group];
172 
173 	if (serdes_base == NULL)
174 		return (EINVAL);
175 
176 	if (group >= SERDES_NUM_GROUPS)
177 		return (EINVAL);
178 
179 	mtx_lock(&group_mode->lock);
180 
181 	if (!group_mode->mode_set || (group_mode->mode != mode)) {
182 		struct al_serdes_grp_obj obj;
183 
184 		al_serdes_handle_grp_init(alpine_serdes_resource_get(group),
185 		    group, &obj);
186 
187 		if (mode == ALPINE_SERDES_ETH_MODE_SGMII)
188 			obj.mode_set_sgmii(&obj);
189 		else
190 			obj.mode_set_kr(&obj);
191 
192 		group_mode->mode = mode;
193 		group_mode->mode_set = true;
194 	}
195 
196 	mtx_unlock(&group_mode->lock);
197 
198 	return (0);
199 }
200 
201 void
202 alpine_serdes_eth_group_lock(uint32_t group)
203 {
204 	struct alpine_serdes_eth_group_mode *group_mode;
205 
206 	group_mode = &alpine_serdes_eth_group_mode[group];
207 
208 	if (mtx_initialized(&group_mode->lock) == 0)
209 		return;
210 
211 	mtx_lock(&group_mode->lock);
212 }
213 
214 void
215 alpine_serdes_eth_group_unlock(uint32_t group)
216 {
217 	struct alpine_serdes_eth_group_mode *group_mode;
218 
219 	group_mode = &alpine_serdes_eth_group_mode[group];
220 
221 	if (mtx_initialized(&group_mode->lock) == 0)
222 		return;
223 
224 	mtx_unlock(&group_mode->lock);
225 }
226