Module Name:    src
Committed By:   jmcneill
Date:           Sun Dec  6 14:01:40 UTC 2020

Modified Files:
        src/sys/arch/arm/acpi: acpi_platform.c

Log Message:
acpi: Cleanup SPCR setup and style fixes.


To generate a diff of this commit:
cvs rdiff -u -r1.21 -r1.22 src/sys/arch/arm/acpi/acpi_platform.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/arm/acpi/acpi_platform.c
diff -u src/sys/arch/arm/acpi/acpi_platform.c:1.21 src/sys/arch/arm/acpi/acpi_platform.c:1.22
--- src/sys/arch/arm/acpi/acpi_platform.c:1.21	Sat Oct 10 15:25:31 2020
+++ src/sys/arch/arm/acpi/acpi_platform.c	Sun Dec  6 14:01:40 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: acpi_platform.c,v 1.21 2020/10/10 15:25:31 jmcneill Exp $ */
+/* $NetBSD: acpi_platform.c,v 1.22 2020/12/06 14:01:40 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2018 The NetBSD Foundation, Inc.
@@ -35,7 +35,7 @@
 #include "opt_multiprocessor.h"
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: acpi_platform.c,v 1.21 2020/10/10 15:25:31 jmcneill Exp $");
+__KERNEL_RCSID(0, "$NetBSD: acpi_platform.c,v 1.22 2020/12/06 14:01:40 jmcneill Exp $");
 
 #include <sys/param.h>
 #include <sys/bus.h>
@@ -81,14 +81,24 @@ __KERNEL_RCSID(0, "$NetBSD: acpi_platfor
 
 #include <libfdt.h>
 
-#define	SPCR_BAUD_UNKNOWN			0
+#define	SPCR_BAUD_DEFAULT			0
 #define	SPCR_BAUD_9600				3
 #define	SPCR_BAUD_19200				4
 #define	SPCR_BAUD_57600				6
 #define	SPCR_BAUD_115200			7
 
+static const struct acpi_spcr_baud_rate {
+	uint8_t		id;
+	uint32_t	baud_rate;
+} acpi_spcr_baud_rates[] = {
+	{ SPCR_BAUD_DEFAULT,	0 },
+	{ SPCR_BAUD_9600,	9600 },
+	{ SPCR_BAUD_19200,	19200 },
+	{ SPCR_BAUD_57600,	57600 },
+	{ SPCR_BAUD_115200,	115200 },
+};
+
 extern struct bus_space arm_generic_bs_tag;
-extern struct bus_space arm_generic_a4x_bs_tag;
 
 #if NPLCOM > 0
 static struct plcom_instance plcom_console;
@@ -125,75 +135,101 @@ acpi_platform_bootstrap(void)
 }
 
 static void
-acpi_platform_startup(void)
+acpi_platform_attach_uart(ACPI_TABLE_SPCR *spcr)
 {
-	ACPI_TABLE_SPCR *spcr;
-	ACPI_TABLE_FADT *fadt;
-#ifdef MULTIPROCESSOR
-	ACPI_TABLE_MADT *madt;
+#if NCOM > 0
+	struct com_regs regs;
+	bus_space_handle_t dummy_bsh;
+	u_int reg_shift;
 #endif
-	int baud_rate;
+	int baud_rate, n;
 
 	/*
-	 * Setup serial console device
+	 * Only MMIO access is supported today.
 	 */
-	if (ACPI_SUCCESS(acpi_table_find(ACPI_SIG_SPCR, (void **)&spcr))) {
+	if (spcr->SerialPort.SpaceId != ACPI_ADR_SPACE_SYSTEM_MEMORY) {
+		return;
+	}
+	if (le64toh(spcr->SerialPort.Address) == 0) {
+		return;
+	}
 
-		switch (spcr->BaudRate) {
-		case SPCR_BAUD_9600:
-			baud_rate = 9600;
-			break;
-		case SPCR_BAUD_19200:
-			baud_rate = 19200;
-			break;
-		case SPCR_BAUD_57600:
-			baud_rate = 57600;
-			break;
-		case SPCR_BAUD_115200:
-		case SPCR_BAUD_UNKNOWN:
-		default:
-			baud_rate = 115200;
+	/*
+	 * Lookup SPCR baud rate.
+	 */
+	baud_rate = 0;
+	for (n = 0; n < __arraycount(acpi_spcr_baud_rates); n++) {
+		if (acpi_spcr_baud_rates[n].id == spcr->BaudRate) {
+			baud_rate = acpi_spcr_baud_rates[n].baud_rate;
 			break;
 		}
+	}
 
-		if (spcr->SerialPort.SpaceId == ACPI_ADR_SPACE_SYSTEM_MEMORY &&
-		    le64toh(spcr->SerialPort.Address) != 0) {
-			switch (spcr->InterfaceType) {
+	/*
+	 * Attach console device.
+	 */
+	switch (spcr->InterfaceType) {
 #if NPLCOM > 0
-			case ACPI_DBG2_ARM_PL011:
-			case ACPI_DBG2_ARM_SBSA_32BIT:
-			case ACPI_DBG2_ARM_SBSA_GENERIC:
-				plcom_console.pi_type = PLCOM_TYPE_PL011;
-				plcom_console.pi_iot = &arm_generic_bs_tag;
-				plcom_console.pi_iobase = le64toh(spcr->SerialPort.Address);
-				plcom_console.pi_size = PL011COM_UART_SIZE;
-				plcom_console.pi_flags = PLC_FLAG_32BIT_ACCESS;
+	case ACPI_DBG2_ARM_PL011:
+	case ACPI_DBG2_ARM_SBSA_32BIT:
+	case ACPI_DBG2_ARM_SBSA_GENERIC:
+		plcom_console.pi_type = PLCOM_TYPE_PL011;
+		plcom_console.pi_iot = &arm_generic_bs_tag;
+		plcom_console.pi_iobase = le64toh(spcr->SerialPort.Address);
+		plcom_console.pi_size = PL011COM_UART_SIZE;
+		plcom_console.pi_flags = PLC_FLAG_32BIT_ACCESS;
 
-				plcomcnattach(&plcom_console, baud_rate, 0, TTYDEF_CFLAG, -1);
-				break;
+		plcomcnattach(&plcom_console, baud_rate, 0, TTYDEF_CFLAG, -1);
+		break;
 #endif
+
 #if NCOM > 0
-			case ACPI_DBG2_16550_COMPATIBLE:
-			case ACPI_DBG2_16550_SUBSET:
-				if (ACPI_ACCESS_BIT_WIDTH(spcr->SerialPort.AccessWidth) == 8) {
-					comcnattach(&arm_generic_bs_tag, le64toh(spcr->SerialPort.Address), baud_rate, -1,
-					    COM_TYPE_NORMAL, TTYDEF_CFLAG);
-				} else {
-					comcnattach(&arm_generic_a4x_bs_tag, le64toh(spcr->SerialPort.Address), baud_rate, -1,
-					    COM_TYPE_NORMAL, TTYDEF_CFLAG);
-				}
-				break;
-			case ACPI_DBG2_BCM2835:
-				comcnattach(&arm_generic_a4x_bs_tag, le64toh(spcr->SerialPort.Address) + 0x40, baud_rate, -1,
-				    COM_TYPE_BCMAUXUART, TTYDEF_CFLAG);
-				cn_set_magic("+++++");
-				break;
-#endif
-			default:
-				printf("SPCR: kernel does not support interface type %#x\n", spcr->InterfaceType);
-				break;
-			}
+	case ACPI_DBG2_16550_COMPATIBLE:
+	case ACPI_DBG2_16550_SUBSET:
+		memset(&dummy_bsh, 0, sizeof(dummy_bsh));
+		if (ACPI_ACCESS_BIT_WIDTH(spcr->SerialPort.AccessWidth) == 8) {
+			reg_shift = 0;
+		} else {
+			reg_shift = 2;
 		}
+		com_init_regs_stride(&regs, &arm_generic_bs_tag, dummy_bsh,
+		    le64toh(spcr->SerialPort.Address), reg_shift);
+		comcnattach1(&regs, baud_rate, -1, COM_TYPE_NORMAL,
+		    TTYDEF_CFLAG);
+		break;
+
+	case ACPI_DBG2_BCM2835:
+		memset(&dummy_bsh, 0, sizeof(dummy_bsh));
+		com_init_regs_stride(&regs, &arm_generic_bs_tag, dummy_bsh,
+		    le64toh(spcr->SerialPort.Address) + 0x40, 2);
+		comcnattach1(&regs, baud_rate, -1, COM_TYPE_BCMAUXUART,
+		    TTYDEF_CFLAG);
+		cn_set_magic("+++++");
+		break;
+#endif
+
+	default:
+		printf("SPCR: kernel does not support interface type %#x\n",
+		    spcr->InterfaceType);
+		break;
+	}
+
+}
+
+static void
+acpi_platform_startup(void)
+{
+	ACPI_TABLE_SPCR *spcr;
+	ACPI_TABLE_FADT *fadt;
+#ifdef MULTIPROCESSOR
+	ACPI_TABLE_MADT *madt;
+#endif
+
+	/*
+	 * Setup serial console device
+	 */
+	if (ACPI_SUCCESS(acpi_table_find(ACPI_SIG_SPCR, (void **)&spcr))) {
+		acpi_platform_attach_uart(spcr);
 		acpi_table_unmap((ACPI_TABLE_HEADER *)spcr);
 	}
 
@@ -201,8 +237,9 @@ acpi_platform_startup(void)
 	 * Initialize PSCI 0.2+ if implemented
 	 */
 	if (ACPI_SUCCESS(acpi_table_find(ACPI_SIG_FADT, (void **)&fadt))) {
-		if (le16toh(fadt->ArmBootFlags) & ACPI_FADT_PSCI_COMPLIANT) {
-			if (le16toh(fadt->ArmBootFlags) & ACPI_FADT_PSCI_USE_HVC) {
+		const uint16_t boot_flags = le16toh(fadt->ArmBootFlags);
+		if ((boot_flags & ACPI_FADT_PSCI_COMPLIANT) != 0) {
+			if ((boot_flags & ACPI_FADT_PSCI_USE_HVC) != 0) {
 				psci_init(psci_call_hvc);
 			} else {
 				psci_init(psci_call_smc);
@@ -219,7 +256,8 @@ acpi_platform_startup(void)
 		char *end = (char *)madt + le32toh(madt->Header.Length);
 		char *where = (char *)madt + sizeof(ACPI_TABLE_MADT);
 		while (where < end) {
-			ACPI_SUBTABLE_HEADER *subtable = (ACPI_SUBTABLE_HEADER *)where;
+			ACPI_SUBTABLE_HEADER *subtable =
+			    (ACPI_SUBTABLE_HEADER *)where;
 			if (subtable->Type == ACPI_MADT_TYPE_GENERIC_INTERRUPT)
 				arm_cpu_max++;
 			where += subtable->Length;
@@ -243,20 +281,26 @@ acpi_platform_device_register(device_t s
 {
 #if NCOM > 0
 	prop_dictionary_t prop = device_properties(self);
+	ACPI_STATUS rv;
 
 	if (device_is_a(self, "com")) {
 		ACPI_TABLE_SPCR *spcr;
 
-		if (ACPI_FAILURE(acpi_table_find(ACPI_SIG_SPCR, (void **)&spcr)))
+		rv = acpi_table_find(ACPI_SIG_SPCR, (void **)&spcr);
+		if (ACPI_FAILURE(rv)) {
 			return;
+		}
 
-		if (spcr->SerialPort.SpaceId != ACPI_ADR_SPACE_SYSTEM_MEMORY)
+		if (spcr->SerialPort.SpaceId != ACPI_ADR_SPACE_SYSTEM_MEMORY) {
 			goto spcr_unmap;
-		if (le64toh(spcr->SerialPort.Address) == 0)
+		}
+		if (le64toh(spcr->SerialPort.Address) == 0) {
 			goto spcr_unmap;
+		}
 		if (spcr->InterfaceType != ACPI_DBG2_16550_COMPATIBLE &&
-		    spcr->InterfaceType != ACPI_DBG2_16550_SUBSET)
+		    spcr->InterfaceType != ACPI_DBG2_16550_SUBSET) {
 			goto spcr_unmap;
+		}
 
 		if (device_is_a(device_parent(self), "puc")) {
 			const struct puc_attach_args * const paa = aux;
@@ -266,8 +310,10 @@ acpi_platform_device_register(device_t s
 			pci_decompose_tag(paa->pc, paa->tag, &b, &d, &f);
 
 			if (spcr->PciSegment == s && spcr->PciBus == b &&
-			    spcr->PciDevice == d && spcr->PciFunction == f)
-				prop_dictionary_set_bool(prop, "force_console", true);
+			    spcr->PciDevice == d && spcr->PciFunction == f) {
+				prop_dictionary_set_bool(prop,
+				    "force_console", true);
+			}
 		}
 
 		if (device_is_a(device_parent(self), "acpi")) {
@@ -275,16 +321,21 @@ acpi_platform_device_register(device_t s
 			struct acpi_resources res;
 			struct acpi_mem *mem;
 
-			if (ACPI_FAILURE(acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
-			    &res, &acpi_resource_parse_ops_quiet)))
+			if (ACPI_FAILURE(acpi_resource_parse(self,
+					 aa->aa_node->ad_handle, "_CRS", &res,
+					 &acpi_resource_parse_ops_quiet))) {
 				goto spcr_unmap;
+			}
 
 			mem = acpi_res_mem(&res, 0);
-			if (mem == NULL)
+			if (mem == NULL) {
 				goto crs_cleanup;
+			}
 
-			if (mem->ar_base == le64toh(spcr->SerialPort.Address))
-				prop_dictionary_set_bool(prop, "force_console", true);
+			if (mem->ar_base == le64toh(spcr->SerialPort.Address)) {
+				prop_dictionary_set_bool(prop,
+				    "force_console", true);
+			}
 
 crs_cleanup:
 			acpi_resource_cleanup(&res);

Reply via email to