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