Home | History | Annotate | Line # | Download | only in acpi
      1 /* $NetBSD: com_acpi.c,v 1.44 2021/10/23 17:46:26 jmcneill Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2002 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: com_acpi.c,v 1.44 2021/10/23 17:46:26 jmcneill 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 	ACPI_INTEGER reg_shift;
    122 
    123 	sc->sc_dev = self;
    124 
    125 	/* parse resources */
    126 	rv = acpi_resource_parse(sc->sc_dev, aa->aa_node->ad_handle, "_CRS",
    127 	    &res, &acpi_resource_parse_ops_default);
    128 	if (ACPI_FAILURE(rv))
    129 		return;
    130 
    131 	/* find our i/o registers */
    132 	io = acpi_res_io(&res, 0);
    133 	if (io != NULL) {
    134 		iot = aa->aa_iot;
    135 		base = io->ar_base;
    136 		size = io->ar_length;
    137 	} else {
    138 		mem = acpi_res_mem(&res, 0);
    139 		if (mem != NULL) {
    140 			iot = aa->aa_memt;
    141 			base = mem->ar_base;
    142 			size = mem->ar_length;
    143 		} else {
    144 			aprint_error_dev(self, "unable to find i/o register "
    145 			    "and memory resource\n");
    146 			goto out;
    147 		}
    148 	}
    149 
    150 	/* find our IRQ */
    151 	irq = acpi_res_irq(&res, 0);
    152 	if (irq == NULL) {
    153 		sc->sc_poll_ticks = 1;
    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 
    162 	rv = acpi_dsd_integer(aa->aa_node->ad_handle, "reg-shift",
    163 	    &reg_shift);
    164 	if (ACPI_FAILURE(rv)) {
    165 		reg_shift = 0;
    166 	}
    167 
    168 	com_init_regs_stride(&sc->sc_regs, iot, ioh, base, reg_shift);
    169 
    170 	aprint_normal("%s", device_xname(self));
    171 
    172 	dce = acpi_compatible_lookup(aa, compat_data);
    173 	KASSERT(dce != NULL);
    174 
    175 	sc->sc_type = dce->value;
    176 
    177 	if (com_probe_subr(&sc->sc_regs) == 0) {
    178 		aprint_error(": com probe failed\n");
    179 		goto out;
    180 	}
    181 
    182 	rv = acpi_dsd_integer(aa->aa_node->ad_handle, "clock-frequency",
    183 	    &clock_freq);
    184 	if (ACPI_SUCCESS(rv))
    185 		sc->sc_frequency = clock_freq;
    186 	else
    187 		sc->sc_frequency = 115200 * 16;
    188 
    189 	com_attach_subr(sc);
    190 
    191 	if (sc->sc_poll_ticks == 0) {
    192 		asc->sc_ih = acpi_intr_establish(self,
    193 		    (uint64_t)(uintptr_t)aa->aa_node->ad_handle,
    194 		    IPL_SERIAL, true, comintr, sc, device_xname(self));
    195 	}
    196 
    197 	if (!pmf_device_register(self, NULL, com_resume))
    198 		aprint_error_dev(self, "couldn't establish a power handler\n");
    199 	goto cleanup;
    200 
    201 	/*
    202 	 * In case of irq resource or i/o space mapping error, just set
    203 	 * a NULL power handler.  This may allow us to sleep later on.
    204 	 */
    205  out:
    206 	if (!pmf_device_register(self, NULL, NULL))
    207 		aprint_error_dev(self, "couldn't establish a power handler\n");
    208 
    209  cleanup:
    210 	acpi_resource_cleanup(&res);
    211 }
    212