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