Home | History | Annotate | Line # | Download | only in apple
apple_platform.c revision 1.1
      1  1.1  jmcneill /* $NetBSD: apple_platform.c,v 1.1 2021/08/30 23:26:26 jmcneill Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2021 Jared 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. Redistributions in binary form must reproduce the above copyright
     13  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15  1.1  jmcneill  *
     16  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  jmcneill  * SUCH DAMAGE.
     27  1.1  jmcneill  */
     28  1.1  jmcneill 
     29  1.1  jmcneill #include <sys/cdefs.h>
     30  1.1  jmcneill __KERNEL_RCSID(0, "$NetBSD: apple_platform.c,v 1.1 2021/08/30 23:26:26 jmcneill Exp $");
     31  1.1  jmcneill 
     32  1.1  jmcneill #include <sys/param.h>
     33  1.1  jmcneill #include <sys/bus.h>
     34  1.1  jmcneill #include <sys/cpu.h>
     35  1.1  jmcneill #include <sys/device.h>
     36  1.1  jmcneill #include <sys/termios.h>
     37  1.1  jmcneill 
     38  1.1  jmcneill #include <dev/fdt/fdtvar.h>
     39  1.1  jmcneill #include <arm/fdt/arm_fdtvar.h>
     40  1.1  jmcneill 
     41  1.1  jmcneill #include <uvm/uvm_extern.h>
     42  1.1  jmcneill 
     43  1.1  jmcneill #include <machine/bootconfig.h>
     44  1.1  jmcneill 
     45  1.1  jmcneill #include <net/if_ether.h>
     46  1.1  jmcneill 
     47  1.1  jmcneill #include <dev/pci/pcivar.h>
     48  1.1  jmcneill #include <machine/pci_machdep.h>
     49  1.1  jmcneill 
     50  1.1  jmcneill #include <arm/cpufunc.h>
     51  1.1  jmcneill 
     52  1.1  jmcneill #include <arm/cortex/gtmr_var.h>
     53  1.1  jmcneill 
     54  1.1  jmcneill #include <arm/arm/psci.h>
     55  1.1  jmcneill #include <arm/fdt/psci_fdtvar.h>
     56  1.1  jmcneill 
     57  1.1  jmcneill #include <libfdt.h>
     58  1.1  jmcneill 
     59  1.1  jmcneill #include <arch/evbarm/fdt/platform.h>
     60  1.1  jmcneill 
     61  1.1  jmcneill extern struct bus_space arm_generic_bs_tag;
     62  1.1  jmcneill 
     63  1.1  jmcneill struct arm32_bus_dma_tag apple_coherent_dma_tag;
     64  1.1  jmcneill static struct arm32_dma_range apple_coherent_ranges[] = {
     65  1.1  jmcneill 	[0] = {
     66  1.1  jmcneill 		.dr_sysbase = 0,
     67  1.1  jmcneill 		.dr_busbase = 0,
     68  1.1  jmcneill 		.dr_len = UINTPTR_MAX,
     69  1.1  jmcneill 		.dr_flags = _BUS_DMAMAP_COHERENT,
     70  1.1  jmcneill 	}
     71  1.1  jmcneill };
     72  1.1  jmcneill 
     73  1.1  jmcneill static void
     74  1.1  jmcneill apple_platform_bootstrap(void)
     75  1.1  jmcneill {
     76  1.1  jmcneill 	extern struct arm32_bus_dma_tag arm_generic_dma_tag;
     77  1.1  jmcneill 
     78  1.1  jmcneill 	apple_coherent_dma_tag = arm_generic_dma_tag;
     79  1.1  jmcneill 	apple_coherent_dma_tag._ranges = apple_coherent_ranges;
     80  1.1  jmcneill 	apple_coherent_dma_tag._nranges = __arraycount(apple_coherent_ranges);
     81  1.1  jmcneill 
     82  1.1  jmcneill 	arm_fdt_cpu_bootstrap();
     83  1.1  jmcneill }
     84  1.1  jmcneill 
     85  1.1  jmcneill static void
     86  1.1  jmcneill apple_platform_init_attach_args(struct fdt_attach_args *faa)
     87  1.1  jmcneill {
     88  1.1  jmcneill 	faa->faa_bst = &arm_generic_bs_tag;
     89  1.1  jmcneill 	faa->faa_dmat = &apple_coherent_dma_tag;
     90  1.1  jmcneill }
     91  1.1  jmcneill 
     92  1.1  jmcneill static const struct pmap_devmap *
     93  1.1  jmcneill apple_platform_devmap(void)
     94  1.1  jmcneill {
     95  1.1  jmcneill 	/* Size this to hold possible entries for the UART and SMP spin-table */
     96  1.1  jmcneill 	static struct pmap_devmap devmap[] = {
     97  1.1  jmcneill 		DEVMAP_ENTRY_END,
     98  1.1  jmcneill 		DEVMAP_ENTRY_END,
     99  1.1  jmcneill 		DEVMAP_ENTRY_END
    100  1.1  jmcneill 	};
    101  1.1  jmcneill 	bus_addr_t uart_base;
    102  1.1  jmcneill 	vaddr_t devmap_va = KERNEL_IO_VBASE;
    103  1.1  jmcneill 	u_int devmap_index = 0;
    104  1.1  jmcneill 	uint64_t release_addr;
    105  1.1  jmcneill 	int phandle;
    106  1.1  jmcneill 
    107  1.1  jmcneill 	phandle = fdtbus_get_stdout_phandle();
    108  1.1  jmcneill 	if (phandle > 0 && fdtbus_get_reg(phandle, 0, &uart_base, NULL) == 0) {
    109  1.1  jmcneill 		devmap[devmap_index].pd_pa = DEVMAP_ALIGN(uart_base);
    110  1.1  jmcneill 		devmap[devmap_index].pd_va = DEVMAP_ALIGN(devmap_va);
    111  1.1  jmcneill 		devmap[devmap_index].pd_size = DEVMAP_SIZE(L3_SIZE);
    112  1.1  jmcneill 		devmap[devmap_index].pd_prot = VM_PROT_READ | VM_PROT_WRITE;
    113  1.1  jmcneill 		devmap[devmap_index].pd_flags = PMAP_DEV_SO;
    114  1.1  jmcneill 		devmap_va = DEVMAP_SIZE(devmap[devmap_index].pd_va +
    115  1.1  jmcneill 		    devmap[devmap_index].pd_size);
    116  1.1  jmcneill 		devmap_index++;
    117  1.1  jmcneill 	}
    118  1.1  jmcneill 
    119  1.1  jmcneill 	/* XXX hopefully all release addresses are in the same 2M */
    120  1.1  jmcneill 	phandle = OF_finddevice("/cpus/cpu@1");
    121  1.1  jmcneill 	if (phandle > 0 &&
    122  1.1  jmcneill 	    of_getprop_uint64(phandle, "cpu-release-addr", &release_addr) == 0) {
    123  1.1  jmcneill 		devmap[devmap_index].pd_pa = DEVMAP_ALIGN(release_addr);
    124  1.1  jmcneill 		devmap[devmap_index].pd_va = DEVMAP_ALIGN(devmap_va);
    125  1.1  jmcneill 		devmap[devmap_index].pd_size = DEVMAP_SIZE(L2_SIZE);
    126  1.1  jmcneill 		devmap[devmap_index].pd_prot = VM_PROT_READ | VM_PROT_WRITE;
    127  1.1  jmcneill 		devmap[devmap_index].pd_flags = PMAP_WRITE_BACK;
    128  1.1  jmcneill 		devmap_va = DEVMAP_SIZE(devmap[devmap_index].pd_va +
    129  1.1  jmcneill 		    devmap[devmap_index].pd_size);
    130  1.1  jmcneill 		devmap_index++;
    131  1.1  jmcneill 	}
    132  1.1  jmcneill 
    133  1.1  jmcneill 	return devmap;
    134  1.1  jmcneill }
    135  1.1  jmcneill 
    136  1.1  jmcneill static u_int
    137  1.1  jmcneill arm_platform_uart_freq(void)
    138  1.1  jmcneill {
    139  1.1  jmcneill 	return 0;
    140  1.1  jmcneill }
    141  1.1  jmcneill 
    142  1.1  jmcneill static int
    143  1.1  jmcneill apple_platform_get_mac_address(pci_chipset_tag_t pc, pcitag_t tag,
    144  1.1  jmcneill     uint8_t *eaddr)
    145  1.1  jmcneill {
    146  1.1  jmcneill 	int b, d, f;
    147  1.1  jmcneill 	int bridge, len;
    148  1.1  jmcneill 	u_int bdf;
    149  1.1  jmcneill 
    150  1.1  jmcneill 	const int pcie = of_find_bycompat(OF_finddevice("/"), "apple,pcie");
    151  1.1  jmcneill 	if (pcie == -1) {
    152  1.1  jmcneill 		return -1;
    153  1.1  jmcneill 	}
    154  1.1  jmcneill 
    155  1.1  jmcneill 	/* Convert PCI tag to encoding of phys.hi for PCI-PCI bridge regs */
    156  1.1  jmcneill 	pci_decompose_tag(pc, tag, &b, &d, &f);
    157  1.1  jmcneill 	bdf = (b << 16) | (d << 11) | (f << 8);
    158  1.1  jmcneill 
    159  1.1  jmcneill 	for (bridge = OF_child(pcie); bridge; bridge = OF_peer(bridge)) {
    160  1.1  jmcneill 		const int ethernet =
    161  1.1  jmcneill 		    of_find_firstchild_byname(bridge, "ethernet");
    162  1.1  jmcneill 		if (ethernet == -1) {
    163  1.1  jmcneill 			continue;
    164  1.1  jmcneill 		}
    165  1.1  jmcneill 		const u_int *data = fdtbus_get_prop(ethernet, "reg", &len);
    166  1.1  jmcneill 		if (data == NULL || len < 4) {
    167  1.1  jmcneill 			continue;
    168  1.1  jmcneill 		}
    169  1.1  jmcneill 		if (bdf != be32toh(data[0])) {
    170  1.1  jmcneill 			continue;
    171  1.1  jmcneill 		}
    172  1.1  jmcneill 
    173  1.1  jmcneill 		return OF_getprop(ethernet, "local-mac-address",
    174  1.1  jmcneill 		    eaddr, ETHER_ADDR_LEN);
    175  1.1  jmcneill 	}
    176  1.1  jmcneill 
    177  1.1  jmcneill 	return -1;
    178  1.1  jmcneill }
    179  1.1  jmcneill 
    180  1.1  jmcneill static void
    181  1.1  jmcneill apple_platform_device_register(device_t self, void *aux)
    182  1.1  jmcneill {
    183  1.1  jmcneill 	prop_dictionary_t prop = device_properties(self);
    184  1.1  jmcneill 	uint8_t eaddr[ETHER_ADDR_LEN];
    185  1.1  jmcneill 	int len;
    186  1.1  jmcneill 
    187  1.1  jmcneill 	if (device_is_a(self, "bge") &&
    188  1.1  jmcneill 	    device_is_a(device_parent(self), "pci")) {
    189  1.1  jmcneill 		struct pci_attach_args * const pa = aux;
    190  1.1  jmcneill 
    191  1.1  jmcneill 		len = apple_platform_get_mac_address(pa->pa_pc, pa->pa_tag,
    192  1.1  jmcneill 		    eaddr);
    193  1.1  jmcneill 		if (len == ETHER_ADDR_LEN) {
    194  1.1  jmcneill 			prop_dictionary_set_bool(prop, "without-seeprom", true);
    195  1.1  jmcneill 			prop_dictionary_set_data(prop, "mac-address", eaddr,
    196  1.1  jmcneill 			    sizeof(eaddr));
    197  1.1  jmcneill 		}
    198  1.1  jmcneill 		return;
    199  1.1  jmcneill 	}
    200  1.1  jmcneill }
    201  1.1  jmcneill 
    202  1.1  jmcneill static const struct arm_platform apple_arm_platform = {
    203  1.1  jmcneill 	.ap_devmap = apple_platform_devmap,
    204  1.1  jmcneill 	.ap_bootstrap = apple_platform_bootstrap,
    205  1.1  jmcneill 	.ap_init_attach_args = apple_platform_init_attach_args,
    206  1.1  jmcneill 	.ap_reset = psci_fdt_reset,
    207  1.1  jmcneill 	.ap_delay = gtmr_delay,
    208  1.1  jmcneill 	.ap_uart_freq = arm_platform_uart_freq,
    209  1.1  jmcneill 	.ap_device_register = apple_platform_device_register,
    210  1.1  jmcneill 	.ap_mpstart = arm_fdt_cpu_mpstart,
    211  1.1  jmcneill };
    212  1.1  jmcneill 
    213  1.1  jmcneill ARM_PLATFORM(apple_arm, "apple,arm-platform", &apple_arm_platform);
    214