apple_smc_acpi.c revision 1.1 1 1.1 riastrad /* $NetBSD: apple_smc_acpi.c,v 1.1 2014/04/01 17:47:36 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*
4 1.1 riastrad * Apple System Management Controller: ACPI Attachment
5 1.1 riastrad */
6 1.1 riastrad
7 1.1 riastrad /*-
8 1.1 riastrad * Copyright (c) 2013 Taylor R. Campbell
9 1.1 riastrad * All rights reserved.
10 1.1 riastrad *
11 1.1 riastrad * Redistribution and use in source and binary forms, with or without
12 1.1 riastrad * modification, are permitted provided that the following conditions
13 1.1 riastrad * are met:
14 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
15 1.1 riastrad * notice, this list of conditions and the following disclaimer.
16 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
18 1.1 riastrad * documentation and/or other materials provided with the distribution.
19 1.1 riastrad *
20 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 1.1 riastrad * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 1.1 riastrad * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.1 riastrad * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 1.1 riastrad * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.1 riastrad * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 1.1 riastrad * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.1 riastrad * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.1 riastrad * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.1 riastrad * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.1 riastrad * SUCH DAMAGE.
31 1.1 riastrad */
32 1.1 riastrad
33 1.1 riastrad #include <sys/cdefs.h>
34 1.1 riastrad __KERNEL_RCSID(0, "$NetBSD: apple_smc_acpi.c,v 1.1 2014/04/01 17:47:36 riastradh Exp $");
35 1.1 riastrad
36 1.1 riastrad #include <sys/types.h>
37 1.1 riastrad #include <sys/param.h>
38 1.1 riastrad
39 1.1 riastrad #include <dev/acpi/acpireg.h>
40 1.1 riastrad #include <dev/acpi/acpivar.h>
41 1.1 riastrad
42 1.1 riastrad #include <dev/ic/apple_smcreg.h>
43 1.1 riastrad #include <dev/ic/apple_smcvar.h>
44 1.1 riastrad
45 1.1 riastrad #define _COMPONENT ACPI_RESOURCE_COMPONENT
46 1.1 riastrad ACPI_MODULE_NAME ("apple_smc_acpi")
47 1.1 riastrad
48 1.1 riastrad struct apple_smc_acpi_softc {
49 1.1 riastrad struct apple_smc_tag sc_smc;
50 1.1 riastrad };
51 1.1 riastrad
52 1.1 riastrad static int apple_smc_acpi_match(device_t, cfdata_t, void *);
53 1.1 riastrad static void apple_smc_acpi_attach(device_t, device_t, void *);
54 1.1 riastrad static int apple_smc_acpi_detach(device_t, int);
55 1.1 riastrad
56 1.1 riastrad CFATTACH_DECL_NEW(apple_smc_acpi, sizeof(struct apple_smc_acpi_softc),
57 1.1 riastrad apple_smc_acpi_match, apple_smc_acpi_attach, apple_smc_acpi_detach, NULL);
58 1.1 riastrad
59 1.1 riastrad static const char *const apple_smc_ids[] = {
60 1.1 riastrad "APP0001",
61 1.1 riastrad NULL
62 1.1 riastrad };
63 1.1 riastrad
64 1.1 riastrad static int
65 1.1 riastrad apple_smc_acpi_match(device_t parent, cfdata_t match, void *aux)
66 1.1 riastrad {
67 1.1 riastrad struct acpi_attach_args *aa = aux;
68 1.1 riastrad
69 1.1 riastrad if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
70 1.1 riastrad return 0;
71 1.1 riastrad
72 1.1 riastrad if (!acpi_match_hid(aa->aa_node->ad_devinfo, apple_smc_ids))
73 1.1 riastrad return 0;
74 1.1 riastrad
75 1.1 riastrad return 1;
76 1.1 riastrad }
77 1.1 riastrad
78 1.1 riastrad static void
79 1.1 riastrad apple_smc_acpi_attach(device_t parent, device_t self, void *aux)
80 1.1 riastrad {
81 1.1 riastrad struct apple_smc_acpi_softc *sc = device_private(self);
82 1.1 riastrad struct apple_smc_tag *smc = &sc->sc_smc;
83 1.1 riastrad struct acpi_attach_args *aa = aux;
84 1.1 riastrad struct acpi_resources res;
85 1.1 riastrad struct acpi_io *io;
86 1.1 riastrad int rv;
87 1.1 riastrad
88 1.1 riastrad smc->smc_dev = self;
89 1.1 riastrad
90 1.1 riastrad aprint_normal("\n");
91 1.1 riastrad aprint_naive("\n");
92 1.1 riastrad
93 1.1 riastrad rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
94 1.1 riastrad &res, &acpi_resource_parse_ops_default);
95 1.1 riastrad if (ACPI_FAILURE(rv)) {
96 1.1 riastrad aprint_error_dev(self, "couldn't parse SMC resources: %s\n",
97 1.1 riastrad AcpiFormatException(rv));
98 1.1 riastrad goto out0;
99 1.1 riastrad }
100 1.1 riastrad
101 1.1 riastrad io = acpi_res_io(&res, 0);
102 1.1 riastrad if (io == NULL) {
103 1.1 riastrad aprint_error_dev(self, "no I/O resource\n");
104 1.1 riastrad goto out1;
105 1.1 riastrad }
106 1.1 riastrad
107 1.1 riastrad if (io->ar_length < APPLE_SMC_REGSIZE) {
108 1.1 riastrad aprint_error_dev(self, "I/O resources too small: %"PRId32"\n",
109 1.1 riastrad io->ar_length);
110 1.1 riastrad goto out1;
111 1.1 riastrad }
112 1.1 riastrad
113 1.1 riastrad if (bus_space_map(aa->aa_iot, io->ar_base, io->ar_length, 0,
114 1.1 riastrad &smc->smc_bsh) != 0) {
115 1.1 riastrad aprint_error_dev(self, "unable to map I/O registers\n");
116 1.1 riastrad goto out1;
117 1.1 riastrad }
118 1.1 riastrad
119 1.1 riastrad smc->smc_bst = aa->aa_iot;
120 1.1 riastrad smc->smc_size = io->ar_length;
121 1.1 riastrad
122 1.1 riastrad apple_smc_attach(smc);
123 1.1 riastrad
124 1.1 riastrad out1: acpi_resource_cleanup(&res);
125 1.1 riastrad out0: return;
126 1.1 riastrad }
127 1.1 riastrad
128 1.1 riastrad static int
129 1.1 riastrad apple_smc_acpi_detach(device_t self, int flags)
130 1.1 riastrad {
131 1.1 riastrad struct apple_smc_acpi_softc *sc = device_private(self);
132 1.1 riastrad struct apple_smc_tag *smc = &sc->sc_smc;
133 1.1 riastrad int error;
134 1.1 riastrad
135 1.1 riastrad if (smc->smc_size != 0) {
136 1.1 riastrad error = apple_smc_detach(smc, flags);
137 1.1 riastrad if (error)
138 1.1 riastrad return error;
139 1.1 riastrad
140 1.1 riastrad bus_space_unmap(smc->smc_bst, smc->smc_bsh, smc->smc_size);
141 1.1 riastrad smc->smc_size = 0;
142 1.1 riastrad }
143 1.1 riastrad
144 1.1 riastrad return 0;
145 1.1 riastrad }
146