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