wb_acpi.c revision 1.2.2.2 1 /* $NetBSD: wb_acpi.c,v 1.2.2.2 2010/03/11 15:03:23 yamt Exp $ */
2
3 /*
4 * Copyright (c) 2009 Jared D. McNeill <jmcneill (at) invisible.ca>
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 * 2. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: wb_acpi.c,v 1.2.2.2 2010/03/11 15:03:23 yamt Exp $");
30
31 #include <sys/param.h>
32 #include <sys/device.h>
33 #include <sys/systm.h>
34
35 #include <dev/acpi/acpivar.h>
36
37 #include <dev/ic/w83l518dvar.h>
38 #include <dev/ic/w83l518dreg.h>
39
40 #include <dev/isa/isadmavar.h>
41
42 #include <dev/sdmmc/sdmmcvar.h>
43
44 static int wb_acpi_match(device_t, cfdata_t, void *);
45 static void wb_acpi_attach(device_t, device_t, void *);
46 static int wb_acpi_detach(device_t, int);
47
48 struct wb_acpi_softc {
49 struct wb_softc sc_wb;
50 isa_chipset_tag_t sc_ic;
51 void *sc_ih;
52 int sc_ioh_length;
53 };
54
55 CFATTACH_DECL_NEW(wb_acpi, sizeof(struct wb_acpi_softc),
56 wb_acpi_match,
57 wb_acpi_attach,
58 wb_acpi_detach,
59 NULL
60 );
61
62 static const char * const wb_acpi_ids[] = {
63 #if notyet
64 "WEC0515", /* Memory Stick interface */
65 #endif
66 "WEC0517", /* SD Memory Card interface */
67 NULL
68 };
69
70 static int
71 wb_acpi_match(device_t parent, cfdata_t match, void *opaque)
72 {
73 struct acpi_attach_args *aa = opaque;
74
75 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
76 return 0;
77
78 return acpi_match_hid(aa->aa_node->ad_devinfo, wb_acpi_ids);
79 }
80
81 static void
82 wb_acpi_attach(device_t parent, device_t self, void *opaque)
83 {
84 struct wb_acpi_softc *sc = device_private(self);
85 struct acpi_attach_args *aa = opaque;
86 struct acpi_resources res;
87 struct acpi_io *io;
88 struct acpi_irq *irq;
89 bus_space_handle_t ioh;
90 ACPI_STATUS rv;
91
92 sc->sc_ic = aa->aa_ic;
93
94 rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
95 &res, &acpi_resource_parse_ops_default);
96 if (ACPI_FAILURE(rv))
97 return;
98
99 io = acpi_res_io(&res, 0);
100 irq = acpi_res_irq(&res, 0);
101 if (io == NULL || irq == NULL) {
102 aprint_error_dev(self, "incomplete resources\n");
103 goto cleanup;
104 }
105
106 if (bus_space_map(aa->aa_iot, io->ar_base, io->ar_length, 0, &ioh)) {
107 aprint_error_dev(self, "couldn't map registers\n");
108 goto cleanup;
109 }
110 sc->sc_ioh_length = io->ar_length;
111
112 sc->sc_ih = isa_intr_establish(sc->sc_ic, irq->ar_irq,
113 (irq->ar_type == ACPI_EDGE_SENSITIVE) ? IST_EDGE : IST_LEVEL,
114 IPL_SDMMC, wb_intr, &sc->sc_wb);
115 if (sc->sc_ih == NULL) {
116 aprint_error_dev(self,
117 "couldn't establish interrupt handler\n");
118 goto cleanup;
119 }
120
121 sc->sc_wb.wb_dev = self;
122 sc->sc_wb.wb_type = WB_DEVNO_SD;
123 sc->sc_wb.wb_iot = aa->aa_iot;
124 sc->sc_wb.wb_ioh = ioh;
125 sc->sc_wb.wb_irq = irq->ar_irq;
126 sc->sc_wb.wb_base = io->ar_base;
127 wb_attach(&sc->sc_wb);
128
129 cleanup:
130 acpi_resource_cleanup(&res);
131 }
132
133 static int
134 wb_acpi_detach(device_t self, int flags)
135 {
136 struct wb_acpi_softc *sc = device_private(self);
137 int rv;
138
139 rv = wb_detach(&sc->sc_wb, flags);
140 if (rv)
141 return rv;
142
143 if (sc->sc_ih)
144 isa_intr_disestablish(sc->sc_ic, sc->sc_ih);
145
146 if (sc->sc_ioh_length > 0)
147 bus_space_unmap(sc->sc_wb.wb_iot, sc->sc_wb.wb_ioh,
148 sc->sc_ioh_length);
149
150 return 0;
151 }
152