2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2015-2016 Landon Fuller <landon@landonf.org>
5 * Copyright (c) 2017 The FreeBSD Foundation
8 * Portions of this software were developed by Landon Fuller
9 * under sponsorship from the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
19 * redistribution must be conditioned upon including a substantially
20 * similar Disclaimer requirement for further binary redistribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
26 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
28 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
31 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33 * THE POSSIBILITY OF SUCH DAMAGES.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/module.h>
44 #include <sys/systm.h>
46 #include <machine/bus.h>
48 #include <dev/bhnd/cores/pmu/bhnd_pmu.h>
52 #include "bcma_eromreg.h"
53 #include "bcma_eromvar.h"
57 /* RID used when allocating EROM table */
58 #define BCMA_EROM_RID 0
60 static bhnd_erom_class_t *
61 bcma_get_erom_class(driver_t *driver)
63 return (&bcma_erom_parser);
67 bcma_probe(device_t dev)
69 device_set_desc(dev, "BCMA BHND bus");
70 return (BUS_PROBE_DEFAULT);
74 * Default bcma(4) bus driver implementation of DEVICE_ATTACH().
76 * This implementation initializes internal bcma(4) state and performs
77 * bus enumeration, and must be called by subclassing drivers in
78 * DEVICE_ATTACH() before any other bus methods.
81 bcma_attach(device_t dev)
85 /* Enumerate children */
86 if ((error = bcma_add_children(dev))) {
87 device_delete_children(dev);
95 bcma_detach(device_t dev)
97 return (bhnd_generic_detach(dev));
101 bcma_add_child(device_t dev, u_int order, const char *name, int unit)
103 struct bcma_devinfo *dinfo;
106 child = device_add_child_ordered(dev, order, name, unit);
110 if ((dinfo = bcma_alloc_dinfo(dev)) == NULL) {
111 device_delete_child(dev, child);
115 device_set_ivars(child, dinfo);
121 bcma_child_deleted(device_t dev, device_t child)
123 struct bhnd_softc *sc;
124 struct bcma_devinfo *dinfo;
126 sc = device_get_softc(dev);
128 /* Call required bhnd(4) implementation */
129 bhnd_generic_child_deleted(dev, child);
131 /* Free bcma device info */
132 if ((dinfo = device_get_ivars(child)) != NULL)
133 bcma_free_dinfo(dev, child, dinfo);
135 device_set_ivars(child, NULL);
139 bcma_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
141 const struct bcma_devinfo *dinfo;
142 const struct bhnd_core_info *ci;
144 dinfo = device_get_ivars(child);
145 ci = &dinfo->corecfg->core_info;
148 case BHND_IVAR_VENDOR:
149 *result = ci->vendor;
151 case BHND_IVAR_DEVICE:
152 *result = ci->device;
154 case BHND_IVAR_HWREV:
157 case BHND_IVAR_DEVICE_CLASS:
158 *result = bhnd_core_class(ci);
160 case BHND_IVAR_VENDOR_NAME:
161 *result = (uintptr_t) bhnd_vendor_name(ci->vendor);
163 case BHND_IVAR_DEVICE_NAME:
164 *result = (uintptr_t) bhnd_core_name(ci);
166 case BHND_IVAR_CORE_INDEX:
167 *result = ci->core_idx;
169 case BHND_IVAR_CORE_UNIT:
172 case BHND_IVAR_PMU_INFO:
173 *result = (uintptr_t) dinfo->pmu_info;
181 bcma_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
183 struct bcma_devinfo *dinfo;
185 dinfo = device_get_ivars(child);
188 case BHND_IVAR_VENDOR:
189 case BHND_IVAR_DEVICE:
190 case BHND_IVAR_HWREV:
191 case BHND_IVAR_DEVICE_CLASS:
192 case BHND_IVAR_VENDOR_NAME:
193 case BHND_IVAR_DEVICE_NAME:
194 case BHND_IVAR_CORE_INDEX:
195 case BHND_IVAR_CORE_UNIT:
197 case BHND_IVAR_PMU_INFO:
198 dinfo->pmu_info = (void *)value;
205 static struct resource_list *
206 bcma_get_resource_list(device_t dev, device_t child)
208 struct bcma_devinfo *dinfo = device_get_ivars(child);
209 return (&dinfo->resources);
213 bcma_read_iost(device_t dev, device_t child, uint16_t *iost)
218 if ((error = bhnd_read_config(child, BCMA_DMP_IOSTATUS, &value, 4)))
221 /* Return only the bottom 16 bits */
222 *iost = (value & BCMA_DMP_IOST_MASK);
227 bcma_read_ioctl(device_t dev, device_t child, uint16_t *ioctl)
232 if ((error = bhnd_read_config(child, BCMA_DMP_IOCTRL, &value, 4)))
235 /* Return only the bottom 16 bits */
236 *ioctl = (value & BCMA_DMP_IOCTRL_MASK);
241 bcma_write_ioctl(device_t dev, device_t child, uint16_t value, uint16_t mask)
243 struct bcma_devinfo *dinfo;
244 struct bhnd_resource *r;
247 if (device_get_parent(child) != dev)
250 dinfo = device_get_ivars(child);
251 if ((r = dinfo->res_agent) == NULL)
254 /* Write new value */
255 ioctl = bhnd_bus_read_4(r, BCMA_DMP_IOCTRL);
256 ioctl &= ~(BCMA_DMP_IOCTRL_MASK & mask);
257 ioctl |= (value & mask);
259 bhnd_bus_write_4(r, BCMA_DMP_IOCTRL, ioctl);
261 /* Perform read-back and wait for completion */
262 bhnd_bus_read_4(r, BCMA_DMP_IOCTRL);
269 bcma_is_hw_suspended(device_t dev, device_t child)
275 /* Is core held in RESET? */
276 error = bhnd_read_config(child, BCMA_DMP_RESETCTRL, &rst, 4);
278 device_printf(child, "error reading HW reset state: %d\n",
283 if (rst & BCMA_DMP_RC_RESET)
286 /* Is core clocked? */
287 error = bhnd_read_ioctl(child, &ioctl);
289 device_printf(child, "error reading HW ioctl register: %d\n",
294 if (!(ioctl & BHND_IOCTL_CLK_EN))
301 bcma_reset_hw(device_t dev, device_t child, uint16_t ioctl,
302 uint16_t reset_ioctl)
304 struct bcma_devinfo *dinfo;
305 struct bhnd_resource *r;
309 if (device_get_parent(child) != dev)
312 dinfo = device_get_ivars(child);
314 /* We require exclusive control over BHND_IOCTL_CLK_(EN|FORCE) */
315 clkflags = BHND_IOCTL_CLK_EN | BHND_IOCTL_CLK_FORCE;
316 if (ioctl & clkflags)
319 /* Can't suspend the core without access to the agent registers */
320 if ((r = dinfo->res_agent) == NULL)
323 /* Place core into known RESET state */
324 if ((error = bhnd_suspend_hw(child, reset_ioctl)))
328 * Leaving the core in reset:
329 * - Set the caller's IOCTL flags
331 * - Force clock distribution to ensure propagation throughout the
334 if ((error = bhnd_write_ioctl(child, ioctl | clkflags, UINT16_MAX)))
337 /* Bring the core out of reset */
338 if ((error = bcma_dmp_write_reset(child, dinfo, 0x0)))
341 /* Disable forced clock gating (leaving clock enabled) */
342 error = bhnd_write_ioctl(child, 0x0, BHND_IOCTL_CLK_FORCE);
350 bcma_suspend_hw(device_t dev, device_t child, uint16_t ioctl)
352 struct bcma_devinfo *dinfo;
353 struct bhnd_resource *r;
357 if (device_get_parent(child) != dev)
360 dinfo = device_get_ivars(child);
362 /* We require exclusive control over BHND_IOCTL_CLK_(EN|FORCE) */
363 clkflags = BHND_IOCTL_CLK_EN | BHND_IOCTL_CLK_FORCE;
364 if (ioctl & clkflags)
367 /* Can't suspend the core without access to the agent registers */
368 if ((r = dinfo->res_agent) == NULL)
371 /* Wait for any pending reset operations to clear */
372 if ((error = bcma_dmp_wait_reset(child, dinfo)))
375 /* Put core into reset (if not already in reset) */
376 if ((error = bcma_dmp_write_reset(child, dinfo, BCMA_DMP_RC_RESET)))
379 /* Write core flags (and clear CLK_EN/CLK_FORCE) */
380 if ((error = bhnd_write_ioctl(child, ioctl, ~clkflags)))
387 bcma_read_config(device_t dev, device_t child, bus_size_t offset, void *value,
390 struct bcma_devinfo *dinfo;
391 struct bhnd_resource *r;
393 /* Must be a directly attached child core */
394 if (device_get_parent(child) != dev)
397 /* Fetch the agent registers */
398 dinfo = device_get_ivars(child);
399 if ((r = dinfo->res_agent) == NULL)
403 if (offset > rman_get_size(r->res))
406 if (rman_get_size(r->res) - offset < width)
411 *((uint8_t *)value) = bhnd_bus_read_1(r, offset);
414 *((uint16_t *)value) = bhnd_bus_read_2(r, offset);
417 *((uint32_t *)value) = bhnd_bus_read_4(r, offset);
425 bcma_write_config(device_t dev, device_t child, bus_size_t offset,
426 const void *value, u_int width)
428 struct bcma_devinfo *dinfo;
429 struct bhnd_resource *r;
431 /* Must be a directly attached child core */
432 if (device_get_parent(child) != dev)
435 /* Fetch the agent registers */
436 dinfo = device_get_ivars(child);
437 if ((r = dinfo->res_agent) == NULL)
441 if (offset > rman_get_size(r->res))
444 if (rman_get_size(r->res) - offset < width)
449 bhnd_bus_write_1(r, offset, *(const uint8_t *)value);
452 bhnd_bus_write_2(r, offset, *(const uint16_t *)value);
455 bhnd_bus_write_4(r, offset, *(const uint32_t *)value);
463 bcma_get_port_count(device_t dev, device_t child, bhnd_port_type type)
465 struct bcma_devinfo *dinfo;
467 /* delegate non-bus-attached devices to our parent */
468 if (device_get_parent(child) != dev)
469 return (BHND_BUS_GET_PORT_COUNT(device_get_parent(dev), child,
472 dinfo = device_get_ivars(child);
474 case BHND_PORT_DEVICE:
475 return (dinfo->corecfg->num_dev_ports);
476 case BHND_PORT_BRIDGE:
477 return (dinfo->corecfg->num_bridge_ports);
478 case BHND_PORT_AGENT:
479 return (dinfo->corecfg->num_wrapper_ports);
481 device_printf(dev, "%s: unknown type (%d)\n",
489 bcma_get_region_count(device_t dev, device_t child, bhnd_port_type type,
492 struct bcma_devinfo *dinfo;
493 struct bcma_sport_list *ports;
494 struct bcma_sport *port;
496 /* delegate non-bus-attached devices to our parent */
497 if (device_get_parent(child) != dev)
498 return (BHND_BUS_GET_REGION_COUNT(device_get_parent(dev), child,
501 dinfo = device_get_ivars(child);
502 ports = bcma_corecfg_get_port_list(dinfo->corecfg, type);
504 STAILQ_FOREACH(port, ports, sp_link) {
505 if (port->sp_num == port_num)
506 return (port->sp_num_maps);
514 bcma_get_port_rid(device_t dev, device_t child, bhnd_port_type port_type,
515 u_int port_num, u_int region_num)
517 struct bcma_devinfo *dinfo;
518 struct bcma_map *map;
519 struct bcma_sport_list *ports;
520 struct bcma_sport *port;
522 dinfo = device_get_ivars(child);
523 ports = bcma_corecfg_get_port_list(dinfo->corecfg, port_type);
525 STAILQ_FOREACH(port, ports, sp_link) {
526 if (port->sp_num != port_num)
529 STAILQ_FOREACH(map, &port->sp_maps, m_link)
530 if (map->m_region_num == region_num)
538 bcma_decode_port_rid(device_t dev, device_t child, int type, int rid,
539 bhnd_port_type *port_type, u_int *port_num, u_int *region_num)
541 struct bcma_devinfo *dinfo;
542 struct bcma_map *map;
543 struct bcma_sport_list *ports;
544 struct bcma_sport *port;
546 dinfo = device_get_ivars(child);
548 /* Ports are always memory mapped */
549 if (type != SYS_RES_MEMORY)
552 /* Starting with the most likely device list, search all three port
554 bhnd_port_type types[] = {
560 for (int i = 0; i < nitems(types); i++) {
561 ports = bcma_corecfg_get_port_list(dinfo->corecfg, types[i]);
563 STAILQ_FOREACH(port, ports, sp_link) {
564 STAILQ_FOREACH(map, &port->sp_maps, m_link) {
565 if (map->m_rid != rid)
568 *port_type = port->sp_type;
569 *port_num = port->sp_num;
570 *region_num = map->m_region_num;
580 bcma_get_region_addr(device_t dev, device_t child, bhnd_port_type port_type,
581 u_int port_num, u_int region_num, bhnd_addr_t *addr, bhnd_size_t *size)
583 struct bcma_devinfo *dinfo;
584 struct bcma_map *map;
585 struct bcma_sport_list *ports;
586 struct bcma_sport *port;
588 dinfo = device_get_ivars(child);
589 ports = bcma_corecfg_get_port_list(dinfo->corecfg, port_type);
591 /* Search the port list */
592 STAILQ_FOREACH(port, ports, sp_link) {
593 if (port->sp_num != port_num)
596 STAILQ_FOREACH(map, &port->sp_maps, m_link) {
597 if (map->m_region_num != region_num)
611 * Default bcma(4) bus driver implementation of BHND_BUS_GET_INTR_COUNT().
614 bcma_get_intr_count(device_t dev, device_t child)
616 struct bcma_devinfo *dinfo;
618 /* delegate non-bus-attached devices to our parent */
619 if (device_get_parent(child) != dev)
620 return (BHND_BUS_GET_INTR_COUNT(device_get_parent(dev), child));
622 dinfo = device_get_ivars(child);
623 return (dinfo->num_intrs);
627 * Default bcma(4) bus driver implementation of BHND_BUS_GET_INTR_IVEC().
630 bcma_get_intr_ivec(device_t dev, device_t child, u_int intr, u_int *ivec)
632 struct bcma_devinfo *dinfo;
633 struct bcma_intr *desc;
635 /* delegate non-bus-attached devices to our parent */
636 if (device_get_parent(child) != dev) {
637 return (BHND_BUS_GET_INTR_IVEC(device_get_parent(dev), child,
641 dinfo = device_get_ivars(child);
643 STAILQ_FOREACH(desc, &dinfo->intrs, i_link) {
644 if (desc->i_sel == intr) {
645 *ivec = desc->i_busline;
655 * Scan the device enumeration ROM table, adding all valid discovered cores to
658 * @param bus The bcma bus.
661 bcma_add_children(device_t bus)
664 struct bcma_erom *bcma_erom;
665 struct bhnd_erom_io *eio;
666 const struct bhnd_chipid *cid;
667 struct bcma_corecfg *corecfg;
668 struct bcma_devinfo *dinfo;
672 cid = BHND_BUS_GET_CHIPID(bus, bus);
675 /* Allocate our EROM parser */
676 eio = bhnd_erom_iores_new(bus, BCMA_EROM_RID);
677 erom = bhnd_erom_alloc(&bcma_erom_parser, cid, eio);
679 bhnd_erom_io_fini(eio);
684 bcma_erom = (struct bcma_erom *)erom;
685 while ((error = bcma_erom_next_corecfg(bcma_erom, &corecfg)) == 0) {
686 /* Add the child device */
687 child = BUS_ADD_CHILD(bus, 0, NULL, -1);
693 /* Initialize device ivars */
694 dinfo = device_get_ivars(child);
695 if ((error = bcma_init_dinfo(bus, child, dinfo, corecfg)))
698 /* The dinfo instance now owns the corecfg value */
701 /* If pins are floating or the hardware is otherwise
702 * unpopulated, the device shouldn't be used. */
703 if (bhnd_is_hw_disabled(child))
704 device_disable(child);
706 /* Issue bus callback for fully initialized child. */
707 BHND_BUS_CHILD_ADDED(bus, child);
710 /* EOF while parsing cores is expected */
715 bhnd_erom_free(erom);
718 bcma_free_corecfg(corecfg);
721 device_delete_children(bus);
727 static device_method_t bcma_methods[] = {
728 /* Device interface */
729 DEVMETHOD(device_probe, bcma_probe),
730 DEVMETHOD(device_attach, bcma_attach),
731 DEVMETHOD(device_detach, bcma_detach),
734 DEVMETHOD(bus_add_child, bcma_add_child),
735 DEVMETHOD(bus_child_deleted, bcma_child_deleted),
736 DEVMETHOD(bus_read_ivar, bcma_read_ivar),
737 DEVMETHOD(bus_write_ivar, bcma_write_ivar),
738 DEVMETHOD(bus_get_resource_list, bcma_get_resource_list),
741 DEVMETHOD(bhnd_bus_get_erom_class, bcma_get_erom_class),
742 DEVMETHOD(bhnd_bus_read_ioctl, bcma_read_ioctl),
743 DEVMETHOD(bhnd_bus_write_ioctl, bcma_write_ioctl),
744 DEVMETHOD(bhnd_bus_read_iost, bcma_read_iost),
745 DEVMETHOD(bhnd_bus_is_hw_suspended, bcma_is_hw_suspended),
746 DEVMETHOD(bhnd_bus_reset_hw, bcma_reset_hw),
747 DEVMETHOD(bhnd_bus_suspend_hw, bcma_suspend_hw),
748 DEVMETHOD(bhnd_bus_read_config, bcma_read_config),
749 DEVMETHOD(bhnd_bus_write_config, bcma_write_config),
750 DEVMETHOD(bhnd_bus_get_port_count, bcma_get_port_count),
751 DEVMETHOD(bhnd_bus_get_region_count, bcma_get_region_count),
752 DEVMETHOD(bhnd_bus_get_port_rid, bcma_get_port_rid),
753 DEVMETHOD(bhnd_bus_decode_port_rid, bcma_decode_port_rid),
754 DEVMETHOD(bhnd_bus_get_region_addr, bcma_get_region_addr),
755 DEVMETHOD(bhnd_bus_get_intr_count, bcma_get_intr_count),
756 DEVMETHOD(bhnd_bus_get_intr_ivec, bcma_get_intr_ivec),
761 DEFINE_CLASS_1(bhnd, bcma_driver, bcma_methods, sizeof(struct bcma_softc), bhnd_driver);
762 MODULE_VERSION(bcma, 1);
763 MODULE_DEPEND(bcma, bhnd, 1, 1, 1);