Home | History | Annotate | Line # | Download | only in acpi
acpi_platform.c revision 1.3
      1 /* $NetBSD: acpi_platform.c,v 1.3 2018/10/15 20:09:06 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2018 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jared McNeill <jmcneill (at) invisible.ca>.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: acpi_platform.c,v 1.3 2018/10/15 20:09:06 jmcneill Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/bus.h>
     37 #include <sys/cpu.h>
     38 #include <sys/device.h>
     39 #include <sys/termios.h>
     40 
     41 #include <dev/fdt/fdtvar.h>
     42 #include <arm/fdt/arm_fdtvar.h>
     43 
     44 #include <uvm/uvm_extern.h>
     45 
     46 #include <machine/bootconfig.h>
     47 #include <arm/cpufunc.h>
     48 #include <arm/locore.h>
     49 
     50 #include <arm/cortex/gtmr_var.h>
     51 
     52 #include <arm/arm/psci.h>
     53 #include <arm/fdt/psci_fdtvar.h>
     54 
     55 #include <evbarm/fdt/platform.h>
     56 
     57 #include <evbarm/dev/plcomreg.h>
     58 #include <evbarm/dev/plcomvar.h>
     59 #include <dev/ic/ns16550reg.h>
     60 #include <dev/ic/comreg.h>
     61 
     62 #include <dev/acpi/acpireg.h>
     63 #include <dev/acpi/acpivar.h>
     64 #include <arch/arm/acpi/acpi_table.h>
     65 
     66 #define	SPCR_INTERFACE_TYPE_PL011		0x0003
     67 #define	SPCR_INTERFACE_TYPE_SBSA_32BIT		0x000d
     68 #define	SPCR_INTERFACE_TYPE_SBSA_GENERIC	0x000e
     69 #define	SPCR_INTERFACE_TYPE_BCM2835		0x0010
     70 
     71 #define	SPCR_BAUD_UNKNOWN			0
     72 #define	SPCR_BAUD_9600				3
     73 #define	SPCR_BAUD_19200				4
     74 #define	SPCR_BAUD_57600				6
     75 #define	SPCR_BAUD_115200			7
     76 
     77 extern struct bus_space arm_generic_bs_tag;
     78 
     79 static struct plcom_instance plcom_console;
     80 
     81 static const struct pmap_devmap *
     82 acpi_platform_devmap(void)
     83 {
     84 	static const struct pmap_devmap devmap[] = {
     85 		DEVMAP_ENTRY_END
     86 	};
     87 
     88 	return devmap;
     89 }
     90 
     91 static void
     92 acpi_platform_bootstrap(void)
     93 {
     94 }
     95 
     96 static void
     97 acpi_platform_startup(void)
     98 {
     99 	ACPI_TABLE_SPCR *spcr;
    100 	ACPI_TABLE_FADT *fadt;
    101 	ACPI_TABLE_MADT *madt;
    102 	int baud_rate;
    103 
    104 	/*
    105 	 * Setup serial console device
    106 	 */
    107 	if (ACPI_SUCCESS(acpi_table_find(ACPI_SIG_SPCR, (void **)&spcr))) {
    108 		if (spcr->SerialPort.SpaceId == ACPI_ADR_SPACE_SYSTEM_MEMORY &&
    109 		    spcr->SerialPort.Address != 0) {
    110 			switch (spcr->InterfaceType) {
    111 			case SPCR_INTERFACE_TYPE_PL011:
    112 			case SPCR_INTERFACE_TYPE_SBSA_32BIT:
    113 			case SPCR_INTERFACE_TYPE_SBSA_GENERIC:
    114 			case SPCR_INTERFACE_TYPE_BCM2835:
    115 				plcom_console.pi_type = PLCOM_TYPE_PL011;
    116 				plcom_console.pi_iot = &arm_generic_bs_tag;
    117 				plcom_console.pi_iobase = spcr->SerialPort.Address;
    118 				plcom_console.pi_size = PL011COM_UART_SIZE;
    119 				if (spcr->InterfaceType == SPCR_INTERFACE_TYPE_SBSA_32BIT) {
    120 					plcom_console.pi_flags = PLC_FLAG_32BIT_ACCESS;
    121 				} else {
    122 					plcom_console.pi_flags = ACPI_ACCESS_BIT_WIDTH(spcr->SerialPort.AccessWidth) == 8 ?
    123 					    0 : PLC_FLAG_32BIT_ACCESS;
    124 				}
    125 				switch (spcr->BaudRate) {
    126 				case SPCR_BAUD_9600:
    127 					baud_rate = 9600;
    128 					break;
    129 				case SPCR_BAUD_19200:
    130 					baud_rate = 19200;
    131 					break;
    132 				case SPCR_BAUD_57600:
    133 					baud_rate = 57600;
    134 					break;
    135 				case SPCR_BAUD_115200:
    136 				case SPCR_BAUD_UNKNOWN:
    137 				default:
    138 					baud_rate = 115200;
    139 					break;
    140 				}
    141 
    142 				plcomcnattach(&plcom_console, baud_rate, 0, TTYDEF_CFLAG, -1);
    143 				break;
    144 			}
    145 		}
    146 		acpi_table_unmap((ACPI_TABLE_HEADER *)spcr);
    147 	}
    148 
    149 	/*
    150 	 * Initialize PSCI 0.2+ if implemented
    151 	 */
    152 	if (ACPI_SUCCESS(acpi_table_find(ACPI_SIG_FADT, (void **)&fadt))) {
    153 		if (fadt->ArmBootFlags & ACPI_FADT_PSCI_COMPLIANT) {
    154 			if (fadt->ArmBootFlags & ACPI_FADT_PSCI_USE_HVC) {
    155 				psci_init(psci_call_hvc);
    156 			} else {
    157 				psci_init(psci_call_smc);
    158 			}
    159 		}
    160 		acpi_table_unmap((ACPI_TABLE_HEADER *)fadt);
    161 	}
    162 
    163 	/*
    164 	 * Count CPUs
    165 	 */
    166 	if (ACPI_SUCCESS(acpi_table_find(ACPI_SIG_MADT, (void **)&madt))) {
    167 		char *end = (char *)madt + madt->Header.Length;
    168 		char *where = (char *)madt + sizeof(ACPI_TABLE_MADT);
    169 		while (where < end) {
    170 			ACPI_SUBTABLE_HEADER *subtable = (ACPI_SUBTABLE_HEADER *)where;
    171 			if (subtable->Type == ACPI_MADT_TYPE_GENERIC_INTERRUPT)
    172 				arm_cpu_max++;
    173 			where += subtable->Length;
    174 		}
    175 		acpi_table_unmap((ACPI_TABLE_HEADER *)madt);
    176 	}
    177 }
    178 
    179 static void
    180 acpi_platform_init_attach_args(struct fdt_attach_args *faa)
    181 {
    182 	extern struct arm32_bus_dma_tag arm_generic_dma_tag;
    183 	extern struct bus_space arm_generic_bs_tag;
    184 	extern struct bus_space arm_generic_a4x_bs_tag;
    185 
    186 	faa->faa_bst = &arm_generic_bs_tag;
    187 	faa->faa_a4x_bst = &arm_generic_a4x_bs_tag;
    188 	faa->faa_dmat = &arm_generic_dma_tag;
    189 }
    190 
    191 static void
    192 acpi_platform_early_putchar(char c)
    193 {
    194 }
    195 
    196 static void
    197 acpi_platform_device_register(device_t self, void *aux)
    198 {
    199 }
    200 
    201 static void
    202 acpi_platform_reset(void)
    203 {
    204 	if (psci_available())
    205 		psci_system_reset();
    206 }
    207 
    208 static u_int
    209 acpi_platform_uart_freq(void)
    210 {
    211 	return 0;
    212 }
    213 
    214 static const struct arm_platform acpi_platform = {
    215 	.ap_devmap = acpi_platform_devmap,
    216 	.ap_bootstrap = acpi_platform_bootstrap,
    217 	.ap_startup = acpi_platform_startup,
    218 	.ap_init_attach_args = acpi_platform_init_attach_args,
    219 	.ap_early_putchar = acpi_platform_early_putchar,
    220 	.ap_device_register = acpi_platform_device_register,
    221 	.ap_reset = acpi_platform_reset,
    222 	.ap_delay = gtmr_delay,
    223 	.ap_uart_freq = acpi_platform_uart_freq,
    224 };
    225 
    226 ARM_PLATFORM(virt, "netbsd,generic-acpi", &acpi_platform);
    227