xref: /netbsd-src/sys/dev/acpi/com_acpi.c (revision 627f7eb200a4419d89b531d55fccd2ee3ffdcde0)
1 /* $NetBSD: com_acpi.c,v 1.42 2021/03/25 05:33:59 rin Exp $ */
2 
3 /*
4  * Copyright (c) 2002 Jared D. McNeill <jmcneill@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: com_acpi.c,v 1.42 2021/03/25 05:33:59 rin Exp $");
30 
31 #include <sys/param.h>
32 #include <sys/device.h>
33 #include <sys/systm.h>
34 #include <sys/termios.h>
35 
36 #include <dev/acpi/acpivar.h>
37 #include <dev/acpi/acpi_intr.h>
38 
39 #include <dev/ic/comvar.h>
40 
41 static int	com_acpi_match(device_t, cfdata_t , void *);
42 static void	com_acpi_attach(device_t, device_t, void *);
43 
44 struct com_acpi_softc {
45 	struct com_softc sc_com;
46 	void *sc_ih;
47 };
48 
49 CFATTACH_DECL_NEW(com_acpi, sizeof(struct com_acpi_softc), com_acpi_match,
50     com_acpi_attach, NULL, NULL);
51 
52 /*
53  * Supported device IDs
54  */
55 
56 static const struct device_compatible_entry compat_data[] = {
57 	/* Standard PC COM port */
58 	{ .compat = "PNP0500",		.value = COM_TYPE_NORMAL },
59 
60 	/* 16550A-compatible COM port */
61 	{ .compat = "PNP0501",		.value = COM_TYPE_NORMAL },
62 
63 	/* Generic IRDA-compatible device */
64 	{ .compat = "PNP0510",		.value = COM_TYPE_NORMAL },
65 
66 	/* Generic IRDA-compatible device */
67 	{ .compat = "PNP0511",		.value = COM_TYPE_NORMAL },
68 
69 	/* IBM ThinkPad IRDA device */
70 	{ .compat = "IBM0071",		.value = COM_TYPE_NORMAL },
71 
72 	/* SMC SuperIO IRDA device */
73 	{ .compat = "SMCF010",		.value = COM_TYPE_NORMAL },
74 
75 	/* NSC IRDA device */
76 	{ .compat = "NSC6001",		.value = COM_TYPE_NORMAL },
77 
78 	/* Fujitsu Serial Pen Tablet */
79 	{ .compat = "FUJ02E6",		.value = COM_TYPE_NORMAL },
80 
81 	/* Hisilicon UART */
82 	{ .compat = "HISI0031",		.value = COM_TYPE_DW_APB },
83 
84 	/* Designware APB UART */
85 	{ .compat = "8250dw",		.value = COM_TYPE_DW_APB },
86 
87 	DEVICE_COMPAT_EOL
88 };
89 
90 /*
91  * com_acpi_match: autoconf(9) match routine
92  */
93 static int
94 com_acpi_match(device_t parent, cfdata_t match, void *aux)
95 {
96 	struct acpi_attach_args *aa = aux;
97 
98 	return acpi_compatible_match(aa, compat_data);
99 }
100 
101 /*
102  * com_acpi_attach: autoconf(9) attach routine
103  */
104 static void
105 com_acpi_attach(device_t parent, device_t self, void *aux)
106 {
107 	struct com_acpi_softc *asc = device_private(self);
108 	struct com_softc *sc = &asc->sc_com;
109 	struct acpi_attach_args *aa = aux;
110 	const struct device_compatible_entry *dce;
111 	struct acpi_resources res;
112 	struct acpi_io *io;
113 	struct acpi_mem *mem;
114 	struct acpi_irq *irq;
115 	bus_space_tag_t iot;
116 	bus_space_handle_t ioh;
117 	bus_addr_t base;
118 	bus_size_t size;
119 	ACPI_STATUS rv;
120 	ACPI_INTEGER clock_freq;
121 
122 	sc->sc_dev = self;
123 
124 	/* parse resources */
125 	rv = acpi_resource_parse(sc->sc_dev, aa->aa_node->ad_handle, "_CRS",
126 	    &res, &acpi_resource_parse_ops_default);
127 	if (ACPI_FAILURE(rv))
128 		return;
129 
130 	/* find our i/o registers */
131 	io = acpi_res_io(&res, 0);
132 	if (io != NULL) {
133 		iot = aa->aa_iot;
134 		base = io->ar_base;
135 		size = io->ar_length;
136 	} else {
137 		mem = acpi_res_mem(&res, 0);
138 		if (mem != NULL) {
139 			iot = aa->aa_memt;
140 			base = mem->ar_base;
141 			size = mem->ar_length;
142 		} else {
143 			aprint_error_dev(self, "unable to find i/o register "
144 			    "and memory resource\n");
145 			goto out;
146 		}
147 	}
148 
149 	/* find our IRQ */
150 	irq = acpi_res_irq(&res, 0);
151 	if (irq == NULL) {
152 		aprint_error_dev(self, "unable to find irq resource\n");
153 		goto out;
154 	}
155 
156 	if (!com_is_console(iot, base, &ioh))
157 		if (bus_space_map(iot, base, size, 0, &ioh)) {
158 			aprint_error_dev(self, "can't map i/o space\n");
159 			goto out;
160 		}
161 	com_init_regs(&sc->sc_regs, iot, ioh, base);
162 
163 	aprint_normal("%s", device_xname(self));
164 
165 	dce = acpi_compatible_lookup(aa, compat_data);
166 	KASSERT(dce != NULL);
167 
168 	sc->sc_type = dce->value;
169 
170 	if (sc->sc_type == COM_TYPE_DW_APB) {
171 		sc->sc_poll_ticks = 1;	/* XXX */
172 	} else {
173 		if (com_probe_subr(&sc->sc_regs) == 0) {
174 			aprint_error(": com probe failed\n");
175 			goto out;
176 		}
177 	}
178 
179 	rv = acpi_dsd_integer(aa->aa_node->ad_handle, "clock-frequency",
180 	    &clock_freq);
181 	if (ACPI_SUCCESS(rv))
182 		sc->sc_frequency = clock_freq;
183 	else
184 		sc->sc_frequency = 115200 * 16;
185 
186 	com_attach_subr(sc);
187 
188 	if (sc->sc_poll_ticks == 0)
189 		asc->sc_ih = acpi_intr_establish(self,
190 		    (uint64_t)(uintptr_t)aa->aa_node->ad_handle,
191 		    IPL_SERIAL, true, comintr, sc, device_xname(self));
192 
193 	if (!pmf_device_register(self, NULL, com_resume))
194 		aprint_error_dev(self, "couldn't establish a power handler\n");
195 	goto cleanup;
196 
197 	/*
198 	 * In case of irq resource or i/o space mapping error, just set
199 	 * a NULL power handler.  This may allow us to sleep later on.
200 	 */
201  out:
202 	if (!pmf_device_register(self, NULL, NULL))
203 		aprint_error_dev(self, "couldn't establish a power handler\n");
204 
205  cleanup:
206 	acpi_resource_cleanup(&res);
207 }
208