1/*-
2 * Copyright (c) 2015-2016 Landon Fuller <landon@freebsd.org>
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 *    without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13 *    redistribution must be conditioned upon including a substantially
14 *    similar Disclaimer requirement for further binary redistribution.
15 *
16 * NO WARRANTY
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGES.
28 *
29 * $FreeBSD: releng/11.0/sys/dev/bhnd/bhnd_nexus.c 301971 2016-06-16 19:57:24Z landonf $
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: releng/11.0/sys/dev/bhnd/bhnd_nexus.c 301971 2016-06-16 19:57:24Z landonf $");
34
35
36/*
37 * bhnd(4) driver mix-in providing shared common methods for
38 * bhnd bus devices attached via a root nexus.
39 */
40
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/bus.h>
44#include <sys/kernel.h>
45#include <sys/module.h>
46#include <sys/rman.h>
47#include <sys/malloc.h>
48
49#include <machine/bus.h>
50
51#include <dev/bhnd/bhnd_ids.h>
52#include <dev/bhnd/cores/chipc/chipcreg.h>
53
54#include "bhnd_nexusvar.h"
55
56static const struct resource_spec bhnd_nexus_res_spec[] = {
57	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },	/* chipc registers */
58	{ -1,			0,	0 }
59};
60
61/**
62 * Map ChipCommon's register block and read the chip identifier data.
63 *
64 * @param dev A bhnd_nexus device.
65 * @param chipid On success, will be populated with the chip identifier.
66 * @retval 0 success
67 * @retval non-zero An error occurred reading the chip identifier..
68 */
69int
70bhnd_nexus_read_chipid(device_t dev, struct bhnd_chipid *chipid)
71{
72	struct resource_spec	rspec[nitems(bhnd_nexus_res_spec)];
73	int			error;
74
75	memcpy(rspec, bhnd_nexus_res_spec, sizeof(rspec));
76	error = bhnd_read_chipid(dev, rspec, 0, chipid);
77	if (error)
78		device_printf(dev, "error %d reading chip ID\n", error);
79
80	return (error);
81}
82
83static bool
84bhnd_nexus_is_hw_disabled(device_t dev, device_t child)
85{
86	return false;
87}
88
89static bhnd_attach_type
90bhnd_nexus_get_attach_type(device_t dev, device_t child)
91{
92	return (BHND_ATTACH_NATIVE);
93}
94
95static int
96bhnd_nexus_activate_resource(device_t dev, device_t child, int type, int rid,
97    struct bhnd_resource *r)
98{
99	int error;
100
101	/* Always direct */
102	if ((error = bus_activate_resource(child, type, rid, r->res)))
103		return (error);
104
105	r->direct = true;
106	return (0);
107}
108
109static int
110bhnd_nexus_deactivate_resource(device_t dev, device_t child,
111    int type, int rid, struct bhnd_resource *r)
112{
113	int error;
114
115	/* Always direct */
116	KASSERT(r->direct, ("indirect resource delegated to bhnd_nexus\n"));
117
118	if ((error = bus_deactivate_resource(child, type, rid, r->res)))
119		return (error);
120
121	r->direct = false;
122	return (0);
123}
124
125static device_method_t bhnd_nexus_methods[] = {
126	/* bhnd interface */
127	DEVMETHOD(bhnd_bus_activate_resource,	bhnd_nexus_activate_resource),
128	DEVMETHOD(bhnd_bus_deactivate_resource, bhnd_nexus_deactivate_resource),
129	DEVMETHOD(bhnd_bus_is_hw_disabled,	bhnd_nexus_is_hw_disabled),
130	DEVMETHOD(bhnd_bus_get_attach_type,	bhnd_nexus_get_attach_type),
131
132	DEVMETHOD_END
133};
134
135DEFINE_CLASS_0(bhnd, bhnd_nexus_driver, bhnd_nexus_methods,
136    sizeof(struct bhnd_softc));
137