xref: /freebsd/sys/dev/dpaa/fman.c (revision 2abb9b42a53c39a8b781e25a05de1de9e98b8b9a)
1 /*-
2  * Copyright (c) 2011-2012 Semihalf.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
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/malloc.h>
37 
38 #include <dev/fdt/fdt_common.h>
39 #include <dev/ofw/ofw_bus.h>
40 #include <dev/ofw/ofw_bus_subr.h>
41 
42 #include "opt_platform.h"
43 
44 #include <contrib/ncsw/inc/Peripherals/fm_ext.h>
45 #include <contrib/ncsw/inc/Peripherals/fm_muram_ext.h>
46 #include <contrib/ncsw/inc/ncsw_ext.h>
47 #include <contrib/ncsw/integrations/fman_ucode.h>
48 
49 #include "fman.h"
50 
51 
52 /**
53  * @group FMan private defines.
54  * @{
55  */
56 enum fman_irq_enum {
57 	FMAN_IRQ_NUM		= 0,
58 	FMAN_ERR_IRQ_NUM	= 1
59 };
60 
61 enum fman_mu_ram_map {
62 	FMAN_MURAM_OFF		= 0x0,
63 	FMAN_MURAM_SIZE		= 0x28000
64 };
65 
66 struct fman_config {
67 	device_t fman_device;
68 	uintptr_t mem_base_addr;
69 	int irq_num;
70 	int err_irq_num;
71 	uint8_t fm_id;
72 	t_FmExceptionsCallback *exception_callback;
73 	t_FmBusErrorCallback *bus_error_callback;
74 };
75 
76 /**
77  * @group FMan private methods/members.
78  * @{
79  */
80 /**
81  * Frame Manager firmware.
82  * We use the same firmware for both P3041 and P2041 devices.
83  */
84 const uint32_t fman_firmware[] = FMAN_UC_IMG;
85 const uint32_t fman_firmware_size = sizeof(fman_firmware);
86 static struct fman_softc *fm_sc = NULL;
87 
88 static t_Handle
89 fman_init(struct fman_softc *sc, struct fman_config *cfg)
90 {
91 	t_FmParams fm_params;
92 	t_Handle muram_handle, fm_handle;
93 	t_Error error;
94 	t_FmRevisionInfo revision_info;
95 	uint16_t clock;
96 	uint32_t tmp, mod;
97 
98 	/* MURAM configuration */
99 	muram_handle = FM_MURAM_ConfigAndInit(cfg->mem_base_addr +
100 	    FMAN_MURAM_OFF, FMAN_MURAM_SIZE);
101 	if (muram_handle == NULL) {
102 		device_printf(cfg->fman_device, "couldn't init FM MURAM module"
103 		    "\n");
104 		return (NULL);
105 	}
106 	sc->muram_handle = muram_handle;
107 
108 	/* Fill in FM configuration */
109 	fm_params.fmId = cfg->fm_id;
110 	/* XXX we support only one partition thus each fman has master id */
111 	fm_params.guestId = NCSW_MASTER_ID;
112 
113 	fm_params.baseAddr = cfg->mem_base_addr;
114 	fm_params.h_FmMuram = muram_handle;
115 
116 	/* Get FMan clock in Hz */
117 	if ((tmp = fman_get_clock(sc)) == 0)
118 		return (NULL);
119 
120 	/* Convert FMan clock to MHz */
121 	clock = (uint16_t)(tmp / 1000000);
122 	mod = tmp % 1000000;
123 
124 	if (mod >= 500000)
125 		++clock;
126 
127 	fm_params.fmClkFreq = clock;
128 	fm_params.f_Exception = cfg->exception_callback;
129 	fm_params.f_BusError = cfg->bus_error_callback;
130 	fm_params.h_App = cfg->fman_device;
131 	fm_params.irq = cfg->irq_num;
132 	fm_params.errIrq = cfg->err_irq_num;
133 
134 	fm_params.firmware.size = fman_firmware_size;
135 	fm_params.firmware.p_Code = (uint32_t*)fman_firmware;
136 
137 	fm_handle = FM_Config(&fm_params);
138 	if (fm_handle == NULL) {
139 		device_printf(cfg->fman_device, "couldn't configure FM "
140 		    "module\n");
141 		goto err;
142 	}
143 
144 	FM_ConfigResetOnInit(fm_handle, TRUE);
145 
146 	error = FM_Init(fm_handle);
147 	if (error != E_OK) {
148 		device_printf(cfg->fman_device, "couldn't init FM module\n");
149 		goto err2;
150 	}
151 
152 	error = FM_GetRevision(fm_handle, &revision_info);
153 	if (error != E_OK) {
154 		device_printf(cfg->fman_device, "couldn't get FM revision\n");
155 		goto err2;
156 	}
157 
158 	device_printf(cfg->fman_device, "Hardware version: %d.%d.\n",
159 	    revision_info.majorRev, revision_info.minorRev);
160 
161 	return (fm_handle);
162 
163 err2:
164 	FM_Free(fm_handle);
165 err:
166 	FM_MURAM_Free(muram_handle);
167 	return (NULL);
168 }
169 
170 static void
171 fman_exception_callback(t_Handle app_handle, e_FmExceptions exception)
172 {
173 	struct fman_softc *sc;
174 
175 	sc = app_handle;
176 	device_printf(sc->dev, "FMan exception occurred.\n");
177 }
178 
179 static void
180 fman_error_callback(t_Handle app_handle, e_FmPortType port_type,
181     uint8_t port_id, uint64_t addr, uint8_t tnum, uint16_t liodn)
182 {
183 	struct fman_softc *sc;
184 
185 	sc = app_handle;
186 	device_printf(sc->dev, "FMan error occurred.\n");
187 }
188 /** @} */
189 
190 
191 /**
192  * @group FMan driver interface.
193  * @{
194  */
195 
196 int
197 fman_get_handle(t_Handle *fmh)
198 {
199 
200 	if (fm_sc == NULL)
201 		return (ENOMEM);
202 
203 	*fmh = fm_sc->fm_handle;
204 
205 	return (0);
206 }
207 
208 int
209 fman_get_muram_handle(t_Handle *muramh)
210 {
211 
212 	if (fm_sc == NULL)
213 		return (ENOMEM);
214 
215 	*muramh = fm_sc->muram_handle;
216 
217 	return (0);
218 }
219 
220 int
221 fman_get_bushandle(vm_offset_t *fm_base)
222 {
223 
224 	if (fm_sc == NULL)
225 		return (ENOMEM);
226 
227 	*fm_base = rman_get_bushandle(fm_sc->mem_res);
228 
229 	return (0);
230 }
231 
232 int
233 fman_attach(device_t dev)
234 {
235 	struct fman_softc *sc;
236 	struct fman_config cfg;
237 
238 	sc = device_get_softc(dev);
239 	sc->dev = dev;
240 	fm_sc = sc;
241 
242 	/* Check if MallocSmart allocator is ready */
243 	if (XX_MallocSmartInit() != E_OK) {
244 		device_printf(dev, "could not initialize smart allocator.\n");
245 		return (ENXIO);
246 	}
247 
248 	XX_TrackInit();
249 
250 	sc->mem_rid = 0;
251 	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
252 	    RF_ACTIVE);
253 	if (!sc->mem_res) {
254 		device_printf(dev, "could not allocate memory.\n");
255 		return (ENXIO);
256 	}
257 
258 	sc->irq_rid = 0;
259 	sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
260 	    RF_ACTIVE);
261 	if (!sc->irq_res) {
262 		device_printf(dev, "could not allocate interrupt.\n");
263 		goto err;
264 	}
265 
266 	/*
267 	 * XXX: Fix FMan interrupt. This is workaround for the issue with
268 	 * interrupts directed to multiple CPUs by the interrupts subsystem.
269 	 * Workaround is to bind the interrupt to only one CPU0.
270 	 */
271 	XX_FmanFixIntr(rman_get_start(sc->irq_res));
272 
273 	sc->err_irq_rid = 1;
274 	sc->err_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
275 	    &sc->err_irq_rid, RF_ACTIVE | RF_SHAREABLE);
276 	if (!sc->err_irq_res) {
277 		device_printf(dev, "could not allocate error interrupt.\n");
278 		goto err;
279 	}
280 
281 	/* Set FMan configuration */
282 	cfg.fman_device = dev;
283 	cfg.fm_id = device_get_unit(dev);
284 	cfg.mem_base_addr = rman_get_bushandle(sc->mem_res);
285 	cfg.irq_num = (int)sc->irq_res;
286 	cfg.err_irq_num = (int)sc->err_irq_res;
287 	cfg.exception_callback = fman_exception_callback;
288 	cfg.bus_error_callback = fman_error_callback;
289 
290 	sc->fm_handle = fman_init(sc, &cfg);
291 	if (sc->fm_handle == NULL) {
292 		device_printf(dev, "could not be configured\n");
293 		return (ENXIO);
294 	}
295 
296 	return (bus_generic_attach(dev));
297 
298 err:
299 	fman_detach(dev);
300 	return (ENXIO);
301 }
302 
303 int
304 fman_detach(device_t dev)
305 {
306 	struct fman_softc *sc;
307 
308 	sc = device_get_softc(dev);
309 
310 	if (sc->muram_handle) {
311 		FM_MURAM_Free(sc->muram_handle);
312 	}
313 
314 	if (sc->fm_handle) {
315 		FM_Free(sc->fm_handle);
316 	}
317 
318 	if (sc->mem_res) {
319 		bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid,
320 		    sc->mem_res);
321 	}
322 
323 	if (sc->irq_res) {
324 		bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid,
325 		    sc->irq_res);
326 	}
327 
328 	if (sc->irq_res) {
329 		bus_release_resource(dev, SYS_RES_IRQ, sc->err_irq_rid,
330 		    sc->err_irq_res);
331 	}
332 
333 	return (0);
334 }
335 
336 int
337 fman_suspend(device_t dev)
338 {
339 
340 	return (0);
341 }
342 
343 int
344 fman_resume(device_t dev)
345 {
346 
347 	return (0);
348 }
349 
350 int
351 fman_shutdown(device_t dev)
352 {
353 
354 	return (0);
355 }
356 
357 /** @} */
358