xref: /netbsd-src/sys/dev/acpi/com_acpi.c (revision f3cfa6f6ce31685c6c4a758bc430e69eb99f50a4)
1 /* $NetBSD: com_acpi.c,v 1.40 2019/03/01 09:21:06 mlelstv 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.40 2019/03/01 09:21:06 mlelstv 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 char * const com_acpi_ids[] = {
57 	"PNP0500",	/* Standard PC COM port */
58 	"PNP0501",	/* 16550A-compatible COM port */
59 	"PNP0510",	/* Generic IRDA-compatible device */
60 	"PNP0511",	/* Generic IRDA-compatible device */
61 	"IBM0071",	/* IBM ThinkPad IRDA device */
62 	"SMCF010",	/* SMC SuperIO IRDA device */
63 	"NSC6001",	/* NSC IRDA device */
64 	"FUJ02E6",	/* Fujitsu Serial Pen Tablet */
65 	"HISI0031",	/* Hisilicon UART */
66 	"8250dw",	/* Designware APB UART */
67 	NULL
68 };
69 
70 /*
71  * Subset of supported device IDs of type COM_TYPE_DW_APB
72  */
73 static const char * const com_acpi_dw_ids[] = {
74 	"HISI0031",	/* Hisilicon UART */
75 	"8250dw",	/* Designware APB UART */
76 	NULL
77 };
78 
79 /*
80  * com_acpi_match: autoconf(9) match routine
81  */
82 static int
83 com_acpi_match(device_t parent, cfdata_t match, void *aux)
84 {
85 	struct acpi_attach_args *aa = aux;
86 
87 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
88 		return 0;
89 
90 	return acpi_match_hid(aa->aa_node->ad_devinfo, com_acpi_ids);
91 }
92 
93 /*
94  * com_acpi_attach: autoconf(9) attach routine
95  */
96 static void
97 com_acpi_attach(device_t parent, device_t self, void *aux)
98 {
99 	struct com_acpi_softc *asc = device_private(self);
100 	struct com_softc *sc = &asc->sc_com;
101 	struct acpi_attach_args *aa = aux;
102 	struct acpi_resources res;
103 	struct acpi_io *io;
104 	struct acpi_mem *mem;
105 	struct acpi_irq *irq;
106 	bus_space_tag_t iot;
107 	bus_space_handle_t ioh;
108 	bus_addr_t base;
109 	bus_size_t size;
110 	ACPI_STATUS rv;
111 	ACPI_INTEGER clock_freq;
112 
113 	sc->sc_dev = self;
114 
115 	/* parse resources */
116 	rv = acpi_resource_parse(sc->sc_dev, aa->aa_node->ad_handle, "_CRS",
117 	    &res, &acpi_resource_parse_ops_default);
118 	if (ACPI_FAILURE(rv))
119 		return;
120 
121 	/* find our i/o registers */
122 	io = acpi_res_io(&res, 0);
123 	if (io != NULL) {
124 		iot = aa->aa_iot;
125 		base = io->ar_base;
126 		size = io->ar_length;
127 	} else {
128 		mem = acpi_res_mem(&res, 0);
129 		if (mem != NULL) {
130 			iot = aa->aa_memt;
131 			base = mem->ar_base;
132 			size = mem->ar_length;
133 		} else {
134 			aprint_error_dev(self, "unable to find i/o register "
135 			    "and memory resource\n");
136 			goto out;
137 		}
138 	}
139 
140 	/* find our IRQ */
141 	irq = acpi_res_irq(&res, 0);
142 	if (irq == NULL) {
143 		aprint_error_dev(self, "unable to find irq resource\n");
144 		goto out;
145 	}
146 
147 	if (!com_is_console(iot, base, &ioh))
148 		if (bus_space_map(iot, base, size, 0, &ioh)) {
149 			aprint_error_dev(self, "can't map i/o space\n");
150 			goto out;
151 		}
152 	com_init_regs(&sc->sc_regs, iot, ioh, base);
153 
154 	aprint_normal("%s", device_xname(self));
155 
156 	if (acpi_match_hid(aa->aa_node->ad_devinfo, com_acpi_dw_ids) != 0) {
157 		sc->sc_type = COM_TYPE_DW_APB;
158 		SET(sc->sc_hwflags, COM_HW_POLL);	/* XXX */
159 	} else {
160 		if (com_probe_subr(&sc->sc_regs) == 0) {
161 			aprint_error(": com probe failed\n");
162 			goto out;
163 		}
164 	}
165 
166 	rv = acpi_dsd_integer(aa->aa_node->ad_handle, "clock-frequency",
167 	    &clock_freq);
168 	if (ACPI_SUCCESS(rv))
169 		sc->sc_frequency = clock_freq;
170 	else
171 		sc->sc_frequency = 115200 * 16;
172 
173 	com_attach_subr(sc);
174 
175 	if (!ISSET(sc->sc_hwflags, COM_HW_POLL))
176 		asc->sc_ih = acpi_intr_establish(self,
177 		    (uint64_t)(uintptr_t)aa->aa_node->ad_handle,
178 		    IPL_SERIAL, true, comintr, sc, device_xname(self));
179 
180 	if (!pmf_device_register(self, NULL, com_resume))
181 		aprint_error_dev(self, "couldn't establish a power handler\n");
182 	goto cleanup;
183 
184 	/*
185 	 * In case of irq resource or i/o space mapping error, just set
186 	 * a NULL power handler.  This may allow us to sleep later on.
187 	 */
188  out:
189 	if (!pmf_device_register(self, NULL, NULL))
190 		aprint_error_dev(self, "couldn't establish a power handler\n");
191 
192  cleanup:
193 	acpi_resource_cleanup(&res);
194 }
195