xref: /freebsd/sys/dev/dpaa/fman.c (revision c7d813a93eeb447470734c9bc0c140d90a54c271)
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/simplebus.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 static MALLOC_DEFINE(M_FMAN, "fman", "fman devices information");
53 
54 /**
55  * @group FMan private defines.
56  * @{
57  */
58 enum fman_irq_enum {
59 	FMAN_IRQ_NUM		= 0,
60 	FMAN_ERR_IRQ_NUM	= 1
61 };
62 
63 enum fman_mu_ram_map {
64 	FMAN_MURAM_OFF		= 0x0,
65 	FMAN_MURAM_SIZE		= 0x28000
66 };
67 
68 struct fman_config {
69 	device_t fman_device;
70 	uintptr_t mem_base_addr;
71 	uintptr_t irq_num;
72 	uintptr_t err_irq_num;
73 	uint8_t fm_id;
74 	t_FmExceptionsCallback *exception_callback;
75 	t_FmBusErrorCallback *bus_error_callback;
76 };
77 
78 /**
79  * @group FMan private methods/members.
80  * @{
81  */
82 /**
83  * Frame Manager firmware.
84  * We use the same firmware for both P3041 and P2041 devices.
85  */
86 const uint32_t fman_firmware[] = FMAN_UC_IMG;
87 const uint32_t fman_firmware_size = sizeof(fman_firmware);
88 static struct fman_softc *fm_sc = NULL;
89 
90 static t_Handle
91 fman_init(struct fman_softc *sc, struct fman_config *cfg)
92 {
93 	struct ofw_bus_devinfo obd;
94 	phandle_t node;
95 	t_FmParams fm_params;
96 	t_Handle muram_handle, fm_handle;
97 	t_Error error;
98 	t_FmRevisionInfo revision_info;
99 	uint16_t clock;
100 	uint32_t tmp, mod;
101 
102 	/* MURAM configuration */
103 	muram_handle = FM_MURAM_ConfigAndInit(cfg->mem_base_addr +
104 	    FMAN_MURAM_OFF, FMAN_MURAM_SIZE);
105 	if (muram_handle == NULL) {
106 		device_printf(cfg->fman_device, "couldn't init FM MURAM module"
107 		    "\n");
108 		return (NULL);
109 	}
110 	sc->muram_handle = muram_handle;
111 
112 	/* Fill in FM configuration */
113 	fm_params.fmId = cfg->fm_id;
114 	/* XXX we support only one partition thus each fman has master id */
115 	fm_params.guestId = NCSW_MASTER_ID;
116 
117 	fm_params.baseAddr = cfg->mem_base_addr;
118 	fm_params.h_FmMuram = muram_handle;
119 
120 	/* Get FMan clock in Hz */
121 	if ((tmp = fman_get_clock(sc)) == 0)
122 		return (NULL);
123 
124 	/* Convert FMan clock to MHz */
125 	clock = (uint16_t)(tmp / 1000000);
126 	mod = tmp % 1000000;
127 
128 	if (mod >= 500000)
129 		++clock;
130 
131 	fm_params.fmClkFreq = clock;
132 	fm_params.f_Exception = cfg->exception_callback;
133 	fm_params.f_BusError = cfg->bus_error_callback;
134 	fm_params.h_App = cfg->fman_device;
135 	fm_params.irq = cfg->irq_num;
136 	fm_params.errIrq = cfg->err_irq_num;
137 
138 	fm_params.firmware.size = fman_firmware_size;
139 	fm_params.firmware.p_Code = (uint32_t*)fman_firmware;
140 
141 	fm_handle = FM_Config(&fm_params);
142 	if (fm_handle == NULL) {
143 		device_printf(cfg->fman_device, "couldn't configure FM "
144 		    "module\n");
145 		goto err;
146 	}
147 
148 	FM_ConfigResetOnInit(fm_handle, TRUE);
149 
150 	error = FM_Init(fm_handle);
151 	if (error != E_OK) {
152 		device_printf(cfg->fman_device, "couldn't init FM module\n");
153 		goto err2;
154 	}
155 
156 	error = FM_GetRevision(fm_handle, &revision_info);
157 	if (error != E_OK) {
158 		device_printf(cfg->fman_device, "couldn't get FM revision\n");
159 		goto err2;
160 	}
161 
162 	device_printf(cfg->fman_device, "Hardware version: %d.%d.\n",
163 	    revision_info.majorRev, revision_info.minorRev);
164 
165 	/* Initialize the simplebus part of things */
166 	simplebus_init(sc->sc_base.dev, 0);
167 
168 	node = ofw_bus_get_node(sc->sc_base.dev);
169 	for (node = OF_child(node); node > 0; node = OF_peer(node)) {
170 		if (ofw_bus_gen_setup_devinfo(&obd, node) != 0)
171 			continue;
172 		simplebus_add_device(sc->sc_base.dev, node, 0, NULL, -1, NULL);
173 	}
174 
175 	return (fm_handle);
176 
177 err2:
178 	FM_Free(fm_handle);
179 err:
180 	FM_MURAM_Free(muram_handle);
181 	return (NULL);
182 }
183 
184 static void
185 fman_exception_callback(t_Handle app_handle, e_FmExceptions exception)
186 {
187 	struct fman_softc *sc;
188 
189 	sc = app_handle;
190 	device_printf(sc->sc_base.dev, "FMan exception occurred.\n");
191 }
192 
193 static void
194 fman_error_callback(t_Handle app_handle, e_FmPortType port_type,
195     uint8_t port_id, uint64_t addr, uint8_t tnum, uint16_t liodn)
196 {
197 	struct fman_softc *sc;
198 
199 	sc = app_handle;
200 	device_printf(sc->sc_base.dev, "FMan error occurred.\n");
201 }
202 /** @} */
203 
204 
205 /**
206  * @group FMan driver interface.
207  * @{
208  */
209 
210 int
211 fman_get_handle(t_Handle *fmh)
212 {
213 
214 	if (fm_sc == NULL)
215 		return (ENOMEM);
216 
217 	*fmh = fm_sc->fm_handle;
218 
219 	return (0);
220 }
221 
222 int
223 fman_get_muram_handle(t_Handle *muramh)
224 {
225 
226 	if (fm_sc == NULL)
227 		return (ENOMEM);
228 
229 	*muramh = fm_sc->muram_handle;
230 
231 	return (0);
232 }
233 
234 int
235 fman_get_bushandle(vm_offset_t *fm_base)
236 {
237 
238 	if (fm_sc == NULL)
239 		return (ENOMEM);
240 
241 	*fm_base = rman_get_bushandle(fm_sc->mem_res);
242 
243 	return (0);
244 }
245 
246 int
247 fman_get_dev(device_t *fm_dev)
248 {
249 
250 	if (fm_sc == NULL)
251 		return (ENOMEM);
252 
253 	*fm_dev = fm_sc->sc_base.dev;
254 
255 	return (0);
256 }
257 
258 int
259 fman_attach(device_t dev)
260 {
261 	struct fman_softc *sc;
262 	struct fman_config cfg;
263 	pcell_t qchan_range[2];
264 	phandle_t node;
265 
266 	sc = device_get_softc(dev);
267 	sc->sc_base.dev = dev;
268 	fm_sc = sc;
269 
270 	/* Check if MallocSmart allocator is ready */
271 	if (XX_MallocSmartInit() != E_OK) {
272 		device_printf(dev, "could not initialize smart allocator.\n");
273 		return (ENXIO);
274 	}
275 
276 	node = ofw_bus_get_node(dev);
277 	if (OF_getencprop(node, "fsl,qman-channel-range", qchan_range,
278 	    sizeof(qchan_range)) <= 0) {
279 		device_printf(dev, "Missing QMan channel range property!\n");
280 		return (ENXIO);
281 	}
282 	sc->qman_chan_base = qchan_range[0];
283 	sc->qman_chan_count = qchan_range[1];
284 	sc->mem_rid = 0;
285 	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
286 	    RF_ACTIVE | RF_SHAREABLE);
287 	if (!sc->mem_res) {
288 		device_printf(dev, "could not allocate memory.\n");
289 		return (ENXIO);
290 	}
291 
292 	sc->irq_rid = 0;
293 	sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
294 	    RF_ACTIVE);
295 	if (!sc->irq_res) {
296 		device_printf(dev, "could not allocate interrupt.\n");
297 		goto err;
298 	}
299 
300 	/*
301 	 * XXX: Fix FMan interrupt. This is workaround for the issue with
302 	 * interrupts directed to multiple CPUs by the interrupts subsystem.
303 	 * Workaround is to bind the interrupt to only one CPU0.
304 	 */
305 	XX_FmanFixIntr(rman_get_start(sc->irq_res));
306 
307 	sc->err_irq_rid = 1;
308 	sc->err_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
309 	    &sc->err_irq_rid, RF_ACTIVE | RF_SHAREABLE);
310 	if (!sc->err_irq_res) {
311 		device_printf(dev, "could not allocate error interrupt.\n");
312 		goto err;
313 	}
314 
315 	/* Set FMan configuration */
316 	cfg.fman_device = dev;
317 	cfg.fm_id = device_get_unit(dev);
318 	cfg.mem_base_addr = rman_get_bushandle(sc->mem_res);
319 	cfg.irq_num = (uintptr_t)sc->irq_res;
320 	cfg.err_irq_num = (uintptr_t)sc->err_irq_res;
321 	cfg.exception_callback = fman_exception_callback;
322 	cfg.bus_error_callback = fman_error_callback;
323 
324 	sc->fm_handle = fman_init(sc, &cfg);
325 	if (sc->fm_handle == NULL) {
326 		device_printf(dev, "could not be configured\n");
327 		return (ENXIO);
328 	}
329 
330 	return (bus_generic_attach(dev));
331 
332 err:
333 	fman_detach(dev);
334 	return (ENXIO);
335 }
336 
337 int
338 fman_detach(device_t dev)
339 {
340 	struct fman_softc *sc;
341 
342 	sc = device_get_softc(dev);
343 
344 	if (sc->muram_handle) {
345 		FM_MURAM_Free(sc->muram_handle);
346 	}
347 
348 	if (sc->fm_handle) {
349 		FM_Free(sc->fm_handle);
350 	}
351 
352 	if (sc->mem_res) {
353 		bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid,
354 		    sc->mem_res);
355 	}
356 
357 	if (sc->irq_res) {
358 		bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid,
359 		    sc->irq_res);
360 	}
361 
362 	if (sc->irq_res) {
363 		bus_release_resource(dev, SYS_RES_IRQ, sc->err_irq_rid,
364 		    sc->err_irq_res);
365 	}
366 
367 	return (0);
368 }
369 
370 int
371 fman_suspend(device_t dev)
372 {
373 
374 	return (0);
375 }
376 
377 int
378 fman_resume(device_t dev)
379 {
380 
381 	return (0);
382 }
383 
384 int
385 fman_shutdown(device_t dev)
386 {
387 
388 	return (0);
389 }
390 
391 int
392 fman_qman_channel_id(device_t dev, int port)
393 {
394 	struct fman_softc *sc;
395 	int qman_port_id[] = {0x31, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e,
396 	    0x2f, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
397 	int i;
398 
399 	sc = device_get_softc(dev);
400 	for (i = 0; i < sc->qman_chan_count; i++) {
401 		if (qman_port_id[i] == port)
402 			return (sc->qman_chan_base + i);
403 	}
404 
405 	return (0);
406 }
407 
408 /** @} */
409