1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2016 Landon Fuller <landon@landonf.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer,
12 *    without modification.
13 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14 *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
15 *    redistribution must be conditioned upon including a substantially
16 *    similar Disclaimer requirement for further binary redistribution.
17 *
18 * NO WARRANTY
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
22 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
24 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29 * THE POSSIBILITY OF SUCH DAMAGES.
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD$");
34
35/*
36 * ChipCommon attachment support for the bhnd(4) PMU driver.
37 *
38 * Supports non-AOB ("Always-on Bus") devices that map the PMU register blocks
39 * via the ChipCommon core, rather than vending a distinct PMU core on the
40 * bhnd bus.
41 */
42
43#include <sys/param.h>
44#include <sys/kernel.h>
45#include <sys/bus.h>
46#include <sys/limits.h>
47#include <sys/malloc.h>
48#include <sys/module.h>
49#include <sys/systm.h>
50
51#include <dev/bhnd/bhnd.h>
52
53#include <dev/bhnd/cores/pmu/bhnd_pmuvar.h>
54#include <dev/bhnd/cores/pmu/bhnd_pmureg.h>
55
56#include "bhnd_chipc_if.h"
57#include "bhnd_pmu_if.h"
58
59#include "chipcvar.h"
60
61static int
62bhnd_pmu_chipc_probe(device_t dev)
63{
64	struct bhnd_pmu_softc	*sc;
65	struct chipc_caps	*ccaps;
66	struct chipc_softc	*chipc_sc;
67	device_t		 chipc;
68	char			 desc[34];
69	int			 error;
70	uint32_t		 pcaps;
71	uint8_t			 rev;
72
73	sc = device_get_softc(dev);
74
75	/* Look for chipc parent */
76	chipc = device_get_parent(dev);
77	if (device_get_devclass(chipc) != devclass_find("bhnd_chipc"))
78		return (ENXIO);
79
80	/* Check the chipc PMU capability flag. */
81	ccaps = BHND_CHIPC_GET_CAPS(chipc);
82	if (!ccaps->pmu)
83		return (ENXIO);
84
85	/* Delegate to common driver implementation */
86	if ((error = bhnd_pmu_probe(dev)) > 0)
87		return (error);
88
89	/* Fetch PMU capability flags */
90	chipc_sc = device_get_softc(chipc);
91	pcaps = bhnd_bus_read_4(chipc_sc->core, BHND_PMU_CAP);
92
93	/* Set description */
94	rev = BHND_PMU_GET_BITS(pcaps, BHND_PMU_CAP_REV);
95	snprintf(desc, sizeof(desc), "Broadcom ChipCommon PMU, rev %hhu", rev);
96	device_set_desc_copy(dev, desc);
97
98	return (BUS_PROBE_NOWILDCARD);
99}
100
101static int
102bhnd_pmu_chipc_attach(device_t dev)
103{
104	struct chipc_softc	*chipc_sc;
105	struct bhnd_resource	*r;
106
107	/* Fetch core registers from ChipCommon parent */
108	chipc_sc = device_get_softc(device_get_parent(dev));
109	r = chipc_sc->core;
110
111	return (bhnd_pmu_attach(dev, r));
112}
113
114static device_method_t bhnd_pmu_chipc_methods[] = {
115	/* Device interface */
116	DEVMETHOD(device_probe,			bhnd_pmu_chipc_probe),
117	DEVMETHOD(device_attach,		bhnd_pmu_chipc_attach),
118
119	DEVMETHOD_END
120};
121
122DEFINE_CLASS_1(bhnd_pmu, bhnd_pmu_chipc_driver, bhnd_pmu_chipc_methods,
123    sizeof(struct bhnd_pmu_softc), bhnd_pmu_driver);
124EARLY_DRIVER_MODULE(bhnd_pmu_chipc, bhnd_chipc, bhnd_pmu_chipc_driver,
125    bhnd_pmu_devclass, NULL, NULL, BUS_PASS_TIMER + BUS_PASS_ORDER_MIDDLE);
126
127MODULE_DEPEND(bhnd_pmu_chipc, bhnd, 1, 1, 1);
128MODULE_VERSION(bhnd_pmu_chipc, 1);
129