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