1178476Sjb/* $NetBSD: wb_acpi.c,v 1.4 2010/08/19 18:30:33 jmcneill Exp $ */
2178476Sjb
3178476Sjb/*
4178476Sjb * Copyright (c) 2009 Jared D. McNeill <jmcneill@invisible.ca>
5178476Sjb * All rights reserved.
6178476Sjb *
7178476Sjb * Redistribution and use in source and binary forms, with or without
8178476Sjb * modification, are permitted provided that the following conditions
9178476Sjb * are met:
10178476Sjb * 1. Redistributions of source code must retain the above copyright
11178476Sjb *    notice, this list of conditions and the following disclaimer.
12178476Sjb * 2. The name of the author may not be used to endorse or promote products
13178476Sjb *    derived from this software without specific prior written permission.
14178476Sjb *
15178476Sjb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16178476Sjb * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17178476Sjb * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18178476Sjb * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19178476Sjb * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20178476Sjb * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21178476Sjb * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22178476Sjb * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23178476Sjb * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24178476Sjb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25178476Sjb * SUCH DAMAGE.
26178476Sjb */
27178476Sjb
28178476Sjb#include <sys/cdefs.h>
29178476Sjb__KERNEL_RCSID(0, "$NetBSD: wb_acpi.c,v 1.4 2010/08/19 18:30:33 jmcneill Exp $");
30178476Sjb
31178476Sjb#include <sys/param.h>
32178476Sjb#include <sys/device.h>
33178476Sjb#include <sys/systm.h>
34178476Sjb
35178476Sjb#include <dev/acpi/acpireg.h>
36178476Sjb#include <dev/acpi/acpivar.h>
37178476Sjb
38178476Sjb#include <dev/ic/w83l518dvar.h>
39178476Sjb#include <dev/ic/w83l518dreg.h>
40178476Sjb
41178476Sjb#include <dev/isa/isadmavar.h>
42178476Sjb
43178476Sjb#include <dev/sdmmc/sdmmcvar.h>
44178476Sjb
45178476Sjb#define _COMPONENT	ACPI_RESOURCE_COMPONENT
46178476SjbACPI_MODULE_NAME	("wb_acpi")
47178476Sjb
48178476Sjbstatic int	wb_acpi_match(device_t, cfdata_t, void *);
49178476Sjbstatic void	wb_acpi_attach(device_t, device_t, void *);
50178476Sjbstatic int	wb_acpi_detach(device_t, int);
51178476Sjbstatic bool	wb_acpi_suspend(device_t, const pmf_qual_t *);
52178476Sjbstatic bool	wb_acpi_resume(device_t, const pmf_qual_t *);
53178476Sjb
54struct wb_acpi_softc {
55	struct wb_softc sc_wb;
56	isa_chipset_tag_t sc_ic;
57	void *sc_ih;
58	int sc_ioh_length;
59
60	ACPI_HANDLE sc_crs, sc_srs;
61	ACPI_BUFFER sc_crs_buffer;
62};
63
64CFATTACH_DECL_NEW(wb_acpi, sizeof(struct wb_acpi_softc),
65    wb_acpi_match,
66    wb_acpi_attach,
67    wb_acpi_detach,
68    NULL
69);
70
71static const char * const wb_acpi_ids[] = {
72#if notyet
73	"WEC0515",	/* Memory Stick interface */
74#endif
75	"WEC0517",	/* SD Memory Card interface */
76	NULL
77};
78
79static int
80wb_acpi_match(device_t parent, cfdata_t match, void *opaque)
81{
82	struct acpi_attach_args *aa = opaque;
83
84	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
85		return 0;
86
87	return acpi_match_hid(aa->aa_node->ad_devinfo, wb_acpi_ids);
88}
89
90static void
91wb_acpi_attach(device_t parent, device_t self, void *opaque)
92{
93	struct wb_acpi_softc *sc = device_private(self);
94	struct acpi_attach_args *aa = opaque;
95	struct acpi_resources res;
96	struct acpi_io *io;
97	struct acpi_irq *irq;
98	bus_space_handle_t ioh;
99	ACPI_STATUS rv;
100
101	sc->sc_ic = aa->aa_ic;
102
103	rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
104	    &res, &acpi_resource_parse_ops_default);
105	if (ACPI_FAILURE(rv))
106		return;
107
108	AcpiGetHandle(aa->aa_node->ad_handle, "_CRS", &sc->sc_crs);
109	AcpiGetHandle(aa->aa_node->ad_handle, "_SRS", &sc->sc_srs);
110	if (sc->sc_crs && sc->sc_srs) {
111		sc->sc_crs_buffer.Pointer = NULL;
112		sc->sc_crs_buffer.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
113		rv = AcpiGetCurrentResources(sc->sc_crs, &sc->sc_crs_buffer);
114		if (ACPI_FAILURE(rv))
115			sc->sc_crs = sc->sc_srs = NULL;
116	}
117
118	io = acpi_res_io(&res, 0);
119	irq = acpi_res_irq(&res, 0);
120	if (io == NULL || irq == NULL) {
121		aprint_error_dev(self, "incomplete resources\n");
122		goto cleanup;
123	}
124
125	if (bus_space_map(aa->aa_iot, io->ar_base, io->ar_length, 0, &ioh)) {
126		aprint_error_dev(self, "couldn't map registers\n");
127		goto cleanup;
128	}
129	sc->sc_ioh_length = io->ar_length;
130
131	sc->sc_ih = isa_intr_establish(sc->sc_ic, irq->ar_irq,
132	    (irq->ar_type == ACPI_EDGE_SENSITIVE) ? IST_EDGE : IST_LEVEL,
133	    IPL_SDMMC, wb_intr, &sc->sc_wb);
134	if (sc->sc_ih == NULL) {
135		aprint_error_dev(self,
136		    "couldn't establish interrupt handler\n");
137		goto cleanup;
138	}
139
140	sc->sc_wb.wb_dev = self;
141	sc->sc_wb.wb_type = WB_DEVNO_SD;
142	sc->sc_wb.wb_iot = aa->aa_iot;
143	sc->sc_wb.wb_ioh = ioh;
144	sc->sc_wb.wb_irq = irq->ar_irq;
145	sc->sc_wb.wb_base = io->ar_base;
146	wb_attach(&sc->sc_wb);
147
148	pmf_device_register(self, wb_acpi_suspend, wb_acpi_resume);
149
150cleanup:
151	acpi_resource_cleanup(&res);
152}
153
154static int
155wb_acpi_detach(device_t self, int flags)
156{
157	struct wb_acpi_softc *sc = device_private(self);
158	int rv;
159
160	pmf_device_deregister(self);
161
162	if (sc->sc_crs_buffer.Pointer)
163		ACPI_FREE(sc->sc_crs_buffer.Pointer);
164
165	rv = wb_detach(&sc->sc_wb, flags);
166	if (rv)
167		return rv;
168
169	if (sc->sc_ih)
170		isa_intr_disestablish(sc->sc_ic, sc->sc_ih);
171
172	if (sc->sc_ioh_length > 0)
173		bus_space_unmap(sc->sc_wb.wb_iot, sc->sc_wb.wb_ioh,
174		    sc->sc_ioh_length);
175
176	return 0;
177}
178
179static bool
180wb_acpi_suspend(device_t self, const pmf_qual_t *qual)
181{
182	struct wb_acpi_softc *sc = device_private(self);
183
184	return wb_suspend(&sc->sc_wb);
185}
186
187static bool
188wb_acpi_resume(device_t self, const pmf_qual_t *qual)
189{
190	struct wb_acpi_softc *sc = device_private(self);
191	ACPI_STATUS rv;
192
193	if (sc->sc_crs && sc->sc_srs) {
194		rv = AcpiSetCurrentResources(sc->sc_srs, &sc->sc_crs_buffer);
195		if (ACPI_FAILURE(rv))
196			printf("%s: _SRS failed: %s\n",
197			    device_xname(self), AcpiFormatException(rv));
198	}
199
200	return wb_resume(&sc->sc_wb);
201}
202