Home | History | Annotate | Line # | Download | only in acpi
com_acpi.c revision 1.40.12.1
      1  1.40.12.1   thorpej /* $NetBSD: com_acpi.c,v 1.40.12.1 2021/04/03 22:28:43 thorpej Exp $ */
      2        1.1  jmcneill 
      3        1.1  jmcneill /*
      4        1.1  jmcneill  * Copyright (c) 2002 Jared D. McNeill <jmcneill (at) invisible.ca>
      5        1.1  jmcneill  * All rights reserved.
      6        1.1  jmcneill  *
      7        1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8        1.1  jmcneill  * modification, are permitted provided that the following conditions
      9        1.1  jmcneill  * are met:
     10        1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11        1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12        1.1  jmcneill  * 2. The name of the author may not be used to endorse or promote products
     13        1.1  jmcneill  *    derived from this software without specific prior written permission.
     14        1.1  jmcneill  *
     15        1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16        1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17        1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18        1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19        1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     20        1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     21        1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     22        1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     23        1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24        1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25        1.1  jmcneill  * SUCH DAMAGE.
     26        1.1  jmcneill  */
     27        1.1  jmcneill 
     28        1.1  jmcneill #include <sys/cdefs.h>
     29  1.40.12.1   thorpej __KERNEL_RCSID(0, "$NetBSD: com_acpi.c,v 1.40.12.1 2021/04/03 22:28:43 thorpej Exp $");
     30        1.1  jmcneill 
     31        1.1  jmcneill #include <sys/param.h>
     32       1.31    jruoho #include <sys/device.h>
     33        1.1  jmcneill #include <sys/systm.h>
     34        1.1  jmcneill #include <sys/termios.h>
     35        1.1  jmcneill 
     36        1.1  jmcneill #include <dev/acpi/acpivar.h>
     37       1.37  jmcneill #include <dev/acpi/acpi_intr.h>
     38        1.1  jmcneill 
     39        1.1  jmcneill #include <dev/ic/comvar.h>
     40        1.1  jmcneill 
     41       1.27      cube static int	com_acpi_match(device_t, cfdata_t , void *);
     42       1.27      cube static void	com_acpi_attach(device_t, device_t, void *);
     43        1.1  jmcneill 
     44        1.1  jmcneill struct com_acpi_softc {
     45        1.1  jmcneill 	struct com_softc sc_com;
     46        1.1  jmcneill 	void *sc_ih;
     47        1.1  jmcneill };
     48        1.1  jmcneill 
     49       1.27      cube CFATTACH_DECL_NEW(com_acpi, sizeof(struct com_acpi_softc), com_acpi_match,
     50       1.30    dyoung     com_acpi_attach, NULL, NULL);
     51        1.1  jmcneill 
     52        1.1  jmcneill /*
     53        1.1  jmcneill  * Supported device IDs
     54        1.1  jmcneill  */
     55        1.1  jmcneill 
     56  1.40.12.1   thorpej static const struct device_compatible_entry compat_data[] = {
     57  1.40.12.1   thorpej 	/* Standard PC COM port */
     58  1.40.12.1   thorpej 	{ .compat = "PNP0500",		.value = COM_TYPE_NORMAL },
     59       1.37  jmcneill 
     60  1.40.12.1   thorpej 	/* 16550A-compatible COM port */
     61  1.40.12.1   thorpej 	{ .compat = "PNP0501",		.value = COM_TYPE_NORMAL },
     62  1.40.12.1   thorpej 
     63  1.40.12.1   thorpej 	/* Generic IRDA-compatible device */
     64  1.40.12.1   thorpej 	{ .compat = "PNP0510",		.value = COM_TYPE_NORMAL },
     65  1.40.12.1   thorpej 
     66  1.40.12.1   thorpej 	/* Generic IRDA-compatible device */
     67  1.40.12.1   thorpej 	{ .compat = "PNP0511",		.value = COM_TYPE_NORMAL },
     68  1.40.12.1   thorpej 
     69  1.40.12.1   thorpej 	/* IBM ThinkPad IRDA device */
     70  1.40.12.1   thorpej 	{ .compat = "IBM0071",		.value = COM_TYPE_NORMAL },
     71  1.40.12.1   thorpej 
     72  1.40.12.1   thorpej 	/* SMC SuperIO IRDA device */
     73  1.40.12.1   thorpej 	{ .compat = "SMCF010",		.value = COM_TYPE_NORMAL },
     74  1.40.12.1   thorpej 
     75  1.40.12.1   thorpej 	/* NSC IRDA device */
     76  1.40.12.1   thorpej 	{ .compat = "NSC6001",		.value = COM_TYPE_NORMAL },
     77  1.40.12.1   thorpej 
     78  1.40.12.1   thorpej 	/* Fujitsu Serial Pen Tablet */
     79  1.40.12.1   thorpej 	{ .compat = "FUJ02E6",		.value = COM_TYPE_NORMAL },
     80  1.40.12.1   thorpej 
     81  1.40.12.1   thorpej 	/* Hisilicon UART */
     82  1.40.12.1   thorpej 	{ .compat = "HISI0031",		.value = COM_TYPE_DW_APB },
     83  1.40.12.1   thorpej 
     84  1.40.12.1   thorpej 	/* Designware APB UART */
     85  1.40.12.1   thorpej 	{ .compat = "8250dw",		.value = COM_TYPE_DW_APB },
     86  1.40.12.1   thorpej 
     87  1.40.12.1   thorpej 	DEVICE_COMPAT_EOL
     88        1.1  jmcneill };
     89        1.1  jmcneill 
     90        1.1  jmcneill /*
     91        1.1  jmcneill  * com_acpi_match: autoconf(9) match routine
     92        1.1  jmcneill  */
     93       1.15     kochi static int
     94       1.27      cube com_acpi_match(device_t parent, cfdata_t match, void *aux)
     95        1.1  jmcneill {
     96        1.1  jmcneill 	struct acpi_attach_args *aa = aux;
     97        1.1  jmcneill 
     98  1.40.12.1   thorpej 	return acpi_compatible_match(aa, compat_data);
     99        1.1  jmcneill }
    100        1.1  jmcneill 
    101        1.1  jmcneill /*
    102        1.1  jmcneill  * com_acpi_attach: autoconf(9) attach routine
    103        1.1  jmcneill  */
    104       1.15     kochi static void
    105       1.27      cube com_acpi_attach(device_t parent, device_t self, void *aux)
    106        1.1  jmcneill {
    107       1.27      cube 	struct com_acpi_softc *asc = device_private(self);
    108        1.1  jmcneill 	struct com_softc *sc = &asc->sc_com;
    109        1.1  jmcneill 	struct acpi_attach_args *aa = aux;
    110  1.40.12.1   thorpej 	const struct device_compatible_entry *dce;
    111        1.1  jmcneill 	struct acpi_resources res;
    112        1.1  jmcneill 	struct acpi_io *io;
    113       1.29  kiyohara 	struct acpi_mem *mem;
    114        1.1  jmcneill 	struct acpi_irq *irq;
    115       1.29  kiyohara 	bus_space_tag_t iot;
    116       1.19   gdamore 	bus_space_handle_t ioh;
    117       1.29  kiyohara 	bus_addr_t base;
    118       1.29  kiyohara 	bus_size_t size;
    119        1.1  jmcneill 	ACPI_STATUS rv;
    120       1.37  jmcneill 	ACPI_INTEGER clock_freq;
    121        1.1  jmcneill 
    122       1.27      cube 	sc->sc_dev = self;
    123       1.27      cube 
    124        1.1  jmcneill 	/* parse resources */
    125       1.27      cube 	rv = acpi_resource_parse(sc->sc_dev, aa->aa_node->ad_handle, "_CRS",
    126       1.13     kochi 	    &res, &acpi_resource_parse_ops_default);
    127       1.11   mycroft 	if (ACPI_FAILURE(rv))
    128        1.1  jmcneill 		return;
    129        1.1  jmcneill 
    130        1.1  jmcneill 	/* find our i/o registers */
    131        1.1  jmcneill 	io = acpi_res_io(&res, 0);
    132       1.29  kiyohara 	if (io != NULL) {
    133       1.29  kiyohara 		iot = aa->aa_iot;
    134       1.29  kiyohara 		base = io->ar_base;
    135       1.29  kiyohara 		size = io->ar_length;
    136       1.29  kiyohara 	} else {
    137       1.29  kiyohara 		mem = acpi_res_mem(&res, 0);
    138       1.29  kiyohara 		if (mem != NULL) {
    139       1.29  kiyohara 			iot = aa->aa_memt;
    140       1.29  kiyohara 			base = mem->ar_base;
    141       1.29  kiyohara 			size = mem->ar_length;
    142       1.29  kiyohara 		} else {
    143       1.33   msaitoh 			aprint_error_dev(self, "unable to find i/o register "
    144       1.33   msaitoh 			    "and memory resource\n");
    145       1.29  kiyohara 			goto out;
    146       1.29  kiyohara 		}
    147        1.1  jmcneill 	}
    148        1.1  jmcneill 
    149        1.1  jmcneill 	/* find our IRQ */
    150        1.1  jmcneill 	irq = acpi_res_irq(&res, 0);
    151        1.1  jmcneill 	if (irq == NULL) {
    152       1.27      cube 		aprint_error_dev(self, "unable to find irq resource\n");
    153       1.14     kochi 		goto out;
    154        1.1  jmcneill 	}
    155        1.1  jmcneill 
    156       1.29  kiyohara 	if (!com_is_console(iot, base, &ioh))
    157       1.29  kiyohara 		if (bus_space_map(iot, base, size, 0, &ioh)) {
    158       1.27      cube 			aprint_error_dev(self, "can't map i/o space\n");
    159       1.14     kochi 			goto out;
    160        1.5  jmcneill 		}
    161       1.36   thorpej 	com_init_regs(&sc->sc_regs, iot, ioh, base);
    162        1.1  jmcneill 
    163       1.27      cube 	aprint_normal("%s", device_xname(self));
    164        1.1  jmcneill 
    165  1.40.12.1   thorpej 	dce = acpi_compatible_lookup(aa, compat_data);
    166  1.40.12.1   thorpej 	KASSERT(dce != NULL);
    167  1.40.12.1   thorpej 
    168  1.40.12.1   thorpej 	sc->sc_type = dce->value;
    169  1.40.12.1   thorpej 
    170  1.40.12.1   thorpej 	if (sc->sc_type == COM_TYPE_DW_APB) {
    171  1.40.12.1   thorpej 		sc->sc_poll_ticks = 1;	/* XXX */
    172       1.37  jmcneill 	} else {
    173       1.37  jmcneill 		if (com_probe_subr(&sc->sc_regs) == 0) {
    174       1.37  jmcneill 			aprint_error(": com probe failed\n");
    175       1.37  jmcneill 			goto out;
    176       1.37  jmcneill 		}
    177        1.1  jmcneill 	}
    178        1.1  jmcneill 
    179       1.38  christos 	rv = acpi_dsd_integer(aa->aa_node->ad_handle, "clock-frequency",
    180       1.38  christos 	    &clock_freq);
    181       1.37  jmcneill 	if (ACPI_SUCCESS(rv))
    182       1.37  jmcneill 		sc->sc_frequency = clock_freq;
    183       1.37  jmcneill 	else
    184       1.37  jmcneill 		sc->sc_frequency = 115200 * 16;
    185        1.1  jmcneill 
    186        1.1  jmcneill 	com_attach_subr(sc);
    187        1.1  jmcneill 
    188  1.40.12.1   thorpej 	if (sc->sc_poll_ticks == 0)
    189       1.38  christos 		asc->sc_ih = acpi_intr_establish(self,
    190       1.40   mlelstv 		    (uint64_t)(uintptr_t)aa->aa_node->ad_handle,
    191       1.37  jmcneill 		    IPL_SERIAL, true, comintr, sc, device_xname(self));
    192       1.25   xtraeme 
    193       1.25   xtraeme 	if (!pmf_device_register(self, NULL, com_resume))
    194       1.25   xtraeme 		aprint_error_dev(self, "couldn't establish a power handler\n");
    195       1.32  pgoyette 	goto cleanup;
    196       1.25   xtraeme 
    197       1.32  pgoyette 	/*
    198       1.32  pgoyette 	 * In case of irq resource or i/o space mapping error, just set
    199       1.32  pgoyette 	 * a NULL power handler.  This may allow us to sleep later on.
    200       1.32  pgoyette 	 */
    201       1.14     kochi  out:
    202       1.32  pgoyette 	if (!pmf_device_register(self, NULL, NULL))
    203       1.32  pgoyette 		aprint_error_dev(self, "couldn't establish a power handler\n");
    204       1.32  pgoyette 
    205       1.32  pgoyette  cleanup:
    206       1.14     kochi 	acpi_resource_cleanup(&res);
    207        1.1  jmcneill }
    208