xref: /freebsd/sys/dev/dpaa/fman.c (revision a32b54357f2b915718c7d179537be19ea2ebd622)
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 <machine/bus.h>
43 
44 #include "opt_platform.h"
45 
46 #include <contrib/ncsw/inc/Peripherals/fm_ext.h>
47 #include <contrib/ncsw/inc/Peripherals/fm_muram_ext.h>
48 #include <contrib/ncsw/inc/ncsw_ext.h>
49 #include <contrib/ncsw/integrations/fman_ucode.h>
50 
51 #include "fman.h"
52 
53 
54 static MALLOC_DEFINE(M_FMAN, "fman", "fman devices information");
55 
56 /**
57  * @group FMan private defines.
58  * @{
59  */
60 enum fman_irq_enum {
61 	FMAN_IRQ_NUM		= 0,
62 	FMAN_ERR_IRQ_NUM	= 1
63 };
64 
65 enum fman_mu_ram_map {
66 	FMAN_MURAM_OFF		= 0x0,
67 	FMAN_MURAM_SIZE		= 0x28000
68 };
69 
70 struct fman_config {
71 	device_t fman_device;
72 	uintptr_t mem_base_addr;
73 	uintptr_t irq_num;
74 	uintptr_t err_irq_num;
75 	uint8_t fm_id;
76 	t_FmExceptionsCallback *exception_callback;
77 	t_FmBusErrorCallback *bus_error_callback;
78 };
79 
80 /**
81  * @group FMan private methods/members.
82  * @{
83  */
84 /**
85  * Frame Manager firmware.
86  * We use the same firmware for both P3041 and P2041 devices.
87  */
88 const uint32_t fman_firmware[] = FMAN_UC_IMG;
89 const uint32_t fman_firmware_size = sizeof(fman_firmware);
90 static struct fman_softc *fm_sc = NULL;
91 
92 int
93 fman_activate_resource(device_t bus, device_t child, int type, int rid,
94     struct resource *res)
95 {
96 	struct fman_softc *sc;
97 	bus_space_tag_t bt;
98 	bus_space_handle_t bh;
99 	int i, rv;
100 
101 	sc = device_get_softc(bus);
102 	if (type != SYS_RES_IRQ) {
103 		for (i = 0; i < sc->sc_base.nranges; i++) {
104 			if (rman_is_region_manager(res, &sc->rman) != 0) {
105 				bt = rman_get_bustag(sc->mem_res);
106 				rv = bus_space_subregion(bt,
107 				    rman_get_bushandle(sc->mem_res),
108 				    rman_get_start(res) -
109 				    rman_get_start(sc->mem_res),
110 				    rman_get_size(res), &bh);
111 				if (rv != 0)
112 					return (rv);
113 				rman_set_bustag(res, bt);
114 				rman_set_bushandle(res, bh);
115 				return (rman_activate_resource(res));
116 			}
117 		}
118 		return (EINVAL);
119 	}
120 	return (bus_generic_activate_resource(bus, child, type, rid, res));
121 }
122 
123 int
124 fman_release_resource(device_t bus, device_t child, int type, int rid,
125     struct resource *res)
126 {
127 	struct fman_softc *sc;
128 	struct resource_list *rl;
129 	struct resource_list_entry *rle;
130 	int passthrough, rv;
131 
132 	passthrough = (device_get_parent(child) != bus);
133 	rl = BUS_GET_RESOURCE_LIST(bus, child);
134 	sc = device_get_softc(bus);
135 	if (type != SYS_RES_IRQ) {
136 		if ((rman_get_flags(res) & RF_ACTIVE) != 0 ){
137 			rv = bus_deactivate_resource(child, type, rid, res);
138 			if (rv != 0)
139 				return (rv);
140 		}
141 		rv = rman_release_resource(res);
142 		if (rv != 0)
143 			return (rv);
144 		if (!passthrough) {
145 			rle = resource_list_find(rl, type, rid);
146 			KASSERT(rle != NULL,
147 			    ("%s: resource entry not found!", __func__));
148 			KASSERT(rle->res != NULL,
149 			   ("%s: resource entry is not busy", __func__));
150 			rle->res = NULL;
151 		}
152 		return (0);
153 	}
154 	return (resource_list_release(rl, bus, child, type, rid, res));
155 }
156 
157 struct resource *
158 fman_alloc_resource(device_t bus, device_t child, int type, int *rid,
159     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
160 {
161 	struct fman_softc *sc;
162 	struct resource_list *rl;
163 	struct resource_list_entry *rle = NULL;
164 	struct resource *res;
165 	int i, isdefault, passthrough;
166 
167 	isdefault = RMAN_IS_DEFAULT_RANGE(start, end);
168 	passthrough = (device_get_parent(child) != bus);
169 	sc = device_get_softc(bus);
170 	rl = BUS_GET_RESOURCE_LIST(bus, child);
171 	switch (type) {
172 	case SYS_RES_MEMORY:
173 		KASSERT(!(isdefault && passthrough),
174 		    ("%s: passthrough of default allocation", __func__));
175 		if (!passthrough) {
176 			rle = resource_list_find(rl, type, *rid);
177 			if (rle == NULL)
178 				return (NULL);
179 			KASSERT(rle->res == NULL,
180 			    ("%s: resource entry is busy", __func__));
181 			if (isdefault) {
182 				start = rle->start;
183 				count = ulmax(count, rle->count);
184 				end = ulmax(rle->end, start + count - 1);
185 			}
186 		}
187 
188 		res = NULL;
189 		/* Map fman ranges to nexus ranges. */
190 		for (i = 0; i < sc->sc_base.nranges; i++) {
191 			if (start >= sc->sc_base.ranges[i].bus && end <
192 			    sc->sc_base.ranges[i].bus + sc->sc_base.ranges[i].size) {
193 				start += rman_get_start(sc->mem_res);
194 				end += rman_get_start(sc->mem_res);
195 				res = rman_reserve_resource(&sc->rman, start,
196 				    end, count, flags & ~RF_ACTIVE, child);
197 				if (res == NULL)
198 					return (NULL);
199 				rman_set_rid(res, *rid);
200 				if ((flags & RF_ACTIVE) != 0 && bus_activate_resource(
201 				    child, type, *rid, res) != 0) {
202 					rman_release_resource(res);
203 					return (NULL);
204 				}
205 				break;
206 			}
207 		}
208 		if (!passthrough)
209 			rle->res = res;
210 		return (res);
211 	case SYS_RES_IRQ:
212 		return (resource_list_alloc(rl, bus, child, type, rid, start,
213 		    end, count, flags));
214 	}
215 	return (NULL);
216 }
217 
218 static int
219 fman_fill_ranges(phandle_t node, struct simplebus_softc *sc)
220 {
221 	int host_address_cells;
222 	cell_t *base_ranges;
223 	ssize_t nbase_ranges;
224 	int err;
225 	int i, j, k;
226 
227 	err = OF_searchencprop(OF_parent(node), "#address-cells",
228 	    &host_address_cells, sizeof(host_address_cells));
229 	if (err <= 0)
230 		return (-1);
231 
232 	nbase_ranges = OF_getproplen(node, "ranges");
233 	if (nbase_ranges < 0)
234 		return (-1);
235 	sc->nranges = nbase_ranges / sizeof(cell_t) /
236 	    (sc->acells + host_address_cells + sc->scells);
237 	if (sc->nranges == 0)
238 		return (0);
239 
240 	sc->ranges = malloc(sc->nranges * sizeof(sc->ranges[0]),
241 	    M_DEVBUF, M_WAITOK);
242 	base_ranges = malloc(nbase_ranges, M_DEVBUF, M_WAITOK);
243 	OF_getencprop(node, "ranges", base_ranges, nbase_ranges);
244 
245 	for (i = 0, j = 0; i < sc->nranges; i++) {
246 		sc->ranges[i].bus = 0;
247 		for (k = 0; k < sc->acells; k++) {
248 			sc->ranges[i].bus <<= 32;
249 			sc->ranges[i].bus |= base_ranges[j++];
250 		}
251 		sc->ranges[i].host = 0;
252 		for (k = 0; k < host_address_cells; k++) {
253 			sc->ranges[i].host <<= 32;
254 			sc->ranges[i].host |= base_ranges[j++];
255 		}
256 		sc->ranges[i].size = 0;
257 		for (k = 0; k < sc->scells; k++) {
258 			sc->ranges[i].size <<= 32;
259 			sc->ranges[i].size |= base_ranges[j++];
260 		}
261 	}
262 
263 	free(base_ranges, M_DEVBUF);
264 	return (sc->nranges);
265 }
266 
267 static t_Handle
268 fman_init(struct fman_softc *sc, struct fman_config *cfg)
269 {
270 	phandle_t node;
271 	t_FmParams fm_params;
272 	t_Handle muram_handle, fm_handle;
273 	t_Error error;
274 	t_FmRevisionInfo revision_info;
275 	uint16_t clock;
276 	uint32_t tmp, mod;
277 
278 	/* MURAM configuration */
279 	muram_handle = FM_MURAM_ConfigAndInit(cfg->mem_base_addr +
280 	    FMAN_MURAM_OFF, FMAN_MURAM_SIZE);
281 	if (muram_handle == NULL) {
282 		device_printf(cfg->fman_device, "couldn't init FM MURAM module"
283 		    "\n");
284 		return (NULL);
285 	}
286 	sc->muram_handle = muram_handle;
287 
288 	/* Fill in FM configuration */
289 	fm_params.fmId = cfg->fm_id;
290 	/* XXX we support only one partition thus each fman has master id */
291 	fm_params.guestId = NCSW_MASTER_ID;
292 
293 	fm_params.baseAddr = cfg->mem_base_addr;
294 	fm_params.h_FmMuram = muram_handle;
295 
296 	/* Get FMan clock in Hz */
297 	if ((tmp = fman_get_clock(sc)) == 0)
298 		return (NULL);
299 
300 	/* Convert FMan clock to MHz */
301 	clock = (uint16_t)(tmp / 1000000);
302 	mod = tmp % 1000000;
303 
304 	if (mod >= 500000)
305 		++clock;
306 
307 	fm_params.fmClkFreq = clock;
308 	fm_params.f_Exception = cfg->exception_callback;
309 	fm_params.f_BusError = cfg->bus_error_callback;
310 	fm_params.h_App = cfg->fman_device;
311 	fm_params.irq = cfg->irq_num;
312 	fm_params.errIrq = cfg->err_irq_num;
313 
314 	fm_params.firmware.size = fman_firmware_size;
315 	fm_params.firmware.p_Code = (uint32_t*)fman_firmware;
316 
317 	fm_handle = FM_Config(&fm_params);
318 	if (fm_handle == NULL) {
319 		device_printf(cfg->fman_device, "couldn't configure FM "
320 		    "module\n");
321 		goto err;
322 	}
323 
324 	FM_ConfigResetOnInit(fm_handle, TRUE);
325 
326 	error = FM_Init(fm_handle);
327 	if (error != E_OK) {
328 		device_printf(cfg->fman_device, "couldn't init FM module\n");
329 		goto err2;
330 	}
331 
332 	error = FM_GetRevision(fm_handle, &revision_info);
333 	if (error != E_OK) {
334 		device_printf(cfg->fman_device, "couldn't get FM revision\n");
335 		goto err2;
336 	}
337 
338 	device_printf(cfg->fman_device, "Hardware version: %d.%d.\n",
339 	    revision_info.majorRev, revision_info.minorRev);
340 
341 	/* Initialize the simplebus part of things */
342 	simplebus_init(sc->sc_base.dev, 0);
343 
344 	node = ofw_bus_get_node(sc->sc_base.dev);
345 	fman_fill_ranges(node, &sc->sc_base);
346 	sc->rman.rm_type = RMAN_ARRAY;
347 	sc->rman.rm_descr = "FMan range";
348 	rman_init_from_resource(&sc->rman, sc->mem_res);
349 	for (node = OF_child(node); node > 0; node = OF_peer(node)) {
350 		simplebus_add_device(sc->sc_base.dev, node, 0, NULL, -1, NULL);
351 	}
352 
353 	return (fm_handle);
354 
355 err2:
356 	FM_Free(fm_handle);
357 err:
358 	FM_MURAM_Free(muram_handle);
359 	return (NULL);
360 }
361 
362 static void
363 fman_exception_callback(t_Handle app_handle, e_FmExceptions exception)
364 {
365 	struct fman_softc *sc;
366 
367 	sc = app_handle;
368 	device_printf(sc->sc_base.dev, "FMan exception occurred.\n");
369 }
370 
371 static void
372 fman_error_callback(t_Handle app_handle, e_FmPortType port_type,
373     uint8_t port_id, uint64_t addr, uint8_t tnum, uint16_t liodn)
374 {
375 	struct fman_softc *sc;
376 
377 	sc = app_handle;
378 	device_printf(sc->sc_base.dev, "FMan error occurred.\n");
379 }
380 /** @} */
381 
382 
383 /**
384  * @group FMan driver interface.
385  * @{
386  */
387 
388 int
389 fman_get_handle(t_Handle *fmh)
390 {
391 
392 	if (fm_sc == NULL)
393 		return (ENOMEM);
394 
395 	*fmh = fm_sc->fm_handle;
396 
397 	return (0);
398 }
399 
400 int
401 fman_get_muram_handle(t_Handle *muramh)
402 {
403 
404 	if (fm_sc == NULL)
405 		return (ENOMEM);
406 
407 	*muramh = fm_sc->muram_handle;
408 
409 	return (0);
410 }
411 
412 int
413 fman_get_bushandle(vm_offset_t *fm_base)
414 {
415 
416 	if (fm_sc == NULL)
417 		return (ENOMEM);
418 
419 	*fm_base = rman_get_bushandle(fm_sc->mem_res);
420 
421 	return (0);
422 }
423 
424 int
425 fman_get_dev(device_t *fm_dev)
426 {
427 
428 	if (fm_sc == NULL)
429 		return (ENOMEM);
430 
431 	*fm_dev = fm_sc->sc_base.dev;
432 
433 	return (0);
434 }
435 
436 int
437 fman_attach(device_t dev)
438 {
439 	struct fman_softc *sc;
440 	struct fman_config cfg;
441 	pcell_t qchan_range[2];
442 	phandle_t node;
443 
444 	sc = device_get_softc(dev);
445 	sc->sc_base.dev = dev;
446 	fm_sc = sc;
447 
448 	/* Check if MallocSmart allocator is ready */
449 	if (XX_MallocSmartInit() != E_OK) {
450 		device_printf(dev, "could not initialize smart allocator.\n");
451 		return (ENXIO);
452 	}
453 
454 	node = ofw_bus_get_node(dev);
455 	if (OF_getencprop(node, "fsl,qman-channel-range", qchan_range,
456 	    sizeof(qchan_range)) <= 0) {
457 		device_printf(dev, "Missing QMan channel range property!\n");
458 		return (ENXIO);
459 	}
460 	sc->qman_chan_base = qchan_range[0];
461 	sc->qman_chan_count = qchan_range[1];
462 	sc->mem_rid = 0;
463 	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid,
464 	    RF_ACTIVE | RF_SHAREABLE);
465 	if (!sc->mem_res) {
466 		device_printf(dev, "could not allocate memory.\n");
467 		return (ENXIO);
468 	}
469 
470 	sc->irq_rid = 0;
471 	sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
472 	    RF_ACTIVE);
473 	if (!sc->irq_res) {
474 		device_printf(dev, "could not allocate interrupt.\n");
475 		goto err;
476 	}
477 
478 	/*
479 	 * XXX: Fix FMan interrupt. This is workaround for the issue with
480 	 * interrupts directed to multiple CPUs by the interrupts subsystem.
481 	 * Workaround is to bind the interrupt to only one CPU0.
482 	 */
483 	XX_FmanFixIntr(rman_get_start(sc->irq_res));
484 
485 	sc->err_irq_rid = 1;
486 	sc->err_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
487 	    &sc->err_irq_rid, RF_ACTIVE | RF_SHAREABLE);
488 	if (!sc->err_irq_res) {
489 		device_printf(dev, "could not allocate error interrupt.\n");
490 		goto err;
491 	}
492 
493 	/* Set FMan configuration */
494 	cfg.fman_device = dev;
495 	cfg.fm_id = device_get_unit(dev);
496 	cfg.mem_base_addr = rman_get_bushandle(sc->mem_res);
497 	cfg.irq_num = (uintptr_t)sc->irq_res;
498 	cfg.err_irq_num = (uintptr_t)sc->err_irq_res;
499 	cfg.exception_callback = fman_exception_callback;
500 	cfg.bus_error_callback = fman_error_callback;
501 
502 	sc->fm_handle = fman_init(sc, &cfg);
503 	if (sc->fm_handle == NULL) {
504 		device_printf(dev, "could not be configured\n");
505 		return (ENXIO);
506 	}
507 
508 	return (bus_generic_attach(dev));
509 
510 err:
511 	fman_detach(dev);
512 	return (ENXIO);
513 }
514 
515 int
516 fman_detach(device_t dev)
517 {
518 	struct fman_softc *sc;
519 
520 	sc = device_get_softc(dev);
521 
522 	if (sc->muram_handle) {
523 		FM_MURAM_Free(sc->muram_handle);
524 	}
525 
526 	if (sc->fm_handle) {
527 		FM_Free(sc->fm_handle);
528 	}
529 
530 	if (sc->mem_res) {
531 		bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid,
532 		    sc->mem_res);
533 	}
534 
535 	if (sc->irq_res) {
536 		bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid,
537 		    sc->irq_res);
538 	}
539 
540 	if (sc->irq_res) {
541 		bus_release_resource(dev, SYS_RES_IRQ, sc->err_irq_rid,
542 		    sc->err_irq_res);
543 	}
544 
545 	return (0);
546 }
547 
548 int
549 fman_suspend(device_t dev)
550 {
551 
552 	return (0);
553 }
554 
555 int
556 fman_resume_dev(device_t dev)
557 {
558 
559 	return (0);
560 }
561 
562 int
563 fman_shutdown(device_t dev)
564 {
565 
566 	return (0);
567 }
568 
569 int
570 fman_qman_channel_id(device_t dev, int port)
571 {
572 	struct fman_softc *sc;
573 	int qman_port_id[] = {0x31, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e,
574 	    0x2f, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
575 	int i;
576 
577 	sc = device_get_softc(dev);
578 	for (i = 0; i < sc->qman_chan_count; i++) {
579 		if (qman_port_id[i] == port)
580 			return (sc->qman_chan_base + i);
581 	}
582 
583 	return (0);
584 }
585 
586 /** @} */
587