Home | History | Annotate | Line # | Download | only in mpc85xx
machdep.c revision 1.1.2.4
      1  1.1.2.4  matt /*	$NetBSD: machdep.c,v 1.1.2.4 2011/08/02 01:34:36 matt Exp $	*/
      2  1.1.2.1  matt /*-
      3  1.1.2.1  matt  * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
      4  1.1.2.1  matt  * All rights reserved.
      5  1.1.2.1  matt  *
      6  1.1.2.1  matt  * This code is derived from software contributed to The NetBSD Foundation
      7  1.1.2.1  matt  * by Raytheon BBN Technologies Corp and Defense Advanced Research Projects
      8  1.1.2.1  matt  * Agency and which was developed by Matt Thomas of 3am Software Foundry.
      9  1.1.2.1  matt  *
     10  1.1.2.1  matt  * This material is based upon work supported by the Defense Advanced Research
     11  1.1.2.1  matt  * Projects Agency and Space and Naval Warfare Systems Center, Pacific, under
     12  1.1.2.1  matt  * Contract No. N66001-09-C-2073.
     13  1.1.2.1  matt  * Approved for Public Release, Distribution Unlimited
     14  1.1.2.1  matt  *
     15  1.1.2.1  matt  * Redistribution and use in source and binary forms, with or without
     16  1.1.2.1  matt  * modification, are permitted provided that the following conditions
     17  1.1.2.1  matt  * are met:
     18  1.1.2.1  matt  * 1. Redistributions of source code must retain the above copyright
     19  1.1.2.1  matt  *    notice, this list of conditions and the following disclaimer.
     20  1.1.2.1  matt  * 2. Redistributions in binary form must reproduce the above copyright
     21  1.1.2.1  matt  *    notice, this list of conditions and the following disclaimer in the
     22  1.1.2.1  matt  *    documentation and/or other materials provided with the distribution.
     23  1.1.2.1  matt  *
     24  1.1.2.1  matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     25  1.1.2.1  matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     26  1.1.2.1  matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     27  1.1.2.1  matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     28  1.1.2.1  matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     29  1.1.2.1  matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     30  1.1.2.1  matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     31  1.1.2.1  matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     32  1.1.2.1  matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     33  1.1.2.1  matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     34  1.1.2.1  matt  * POSSIBILITY OF SUCH DAMAGE.
     35  1.1.2.1  matt  */
     36  1.1.2.1  matt 
     37  1.1.2.1  matt #include <sys/cdefs.h>
     38  1.1.2.1  matt 
     39  1.1.2.1  matt __KERNEL_RCSID(0, "$NetSBD$");
     40  1.1.2.1  matt 
     41  1.1.2.1  matt #include "opt_mpc85xx.h"
     42  1.1.2.2  matt #include "opt_altivec.h"
     43  1.1.2.1  matt #include "opt_pci.h"
     44  1.1.2.2  matt #include "opt_ddb.h"
     45  1.1.2.1  matt #include "gpio.h"
     46  1.1.2.1  matt #include "pci.h"
     47  1.1.2.1  matt 
     48  1.1.2.1  matt #define	DDRC_PRIVATE
     49  1.1.2.1  matt #define	GLOBAL_PRIVATE
     50  1.1.2.1  matt #define	L2CACHE_PRIVATE
     51  1.1.2.1  matt #define _POWERPC_BUS_DMA_PRIVATE
     52  1.1.2.1  matt 
     53  1.1.2.1  matt #include <sys/param.h>
     54  1.1.2.1  matt #include <sys/cpu.h>
     55  1.1.2.1  matt #include <sys/intr.h>
     56  1.1.2.1  matt #include <sys/msgbuf.h>
     57  1.1.2.1  matt #include <sys/tty.h>
     58  1.1.2.1  matt #include <sys/kcore.h>
     59  1.1.2.1  matt #include <sys/bitops.h>
     60  1.1.2.1  matt #include <sys/bus.h>
     61  1.1.2.1  matt #include <sys/extent.h>
     62  1.1.2.1  matt #include <sys/malloc.h>
     63  1.1.2.1  matt #include <sys/ksyms.h>
     64  1.1.2.1  matt 
     65  1.1.2.1  matt #include <uvm/uvm_extern.h>
     66  1.1.2.1  matt 
     67  1.1.2.1  matt #include <prop/proplib.h>
     68  1.1.2.1  matt 
     69  1.1.2.1  matt #include <machine/stdarg.h>
     70  1.1.2.2  matt #include <powerpc/pcb.h>
     71  1.1.2.1  matt 
     72  1.1.2.1  matt #include <dev/cons.h>
     73  1.1.2.1  matt 
     74  1.1.2.1  matt #include <dev/ic/comreg.h>
     75  1.1.2.1  matt #include <dev/ic/comvar.h>
     76  1.1.2.1  matt 
     77  1.1.2.1  matt #include <net/if.h>
     78  1.1.2.1  matt #include <net/if_media.h>
     79  1.1.2.1  matt #include <dev/mii/miivar.h>
     80  1.1.2.1  matt 
     81  1.1.2.1  matt #include <powerpc/spr.h>
     82  1.1.2.1  matt #include <powerpc/booke/spr.h>
     83  1.1.2.1  matt 
     84  1.1.2.1  matt #include <powerpc/booke/cpuvar.h>
     85  1.1.2.1  matt #include <powerpc/booke/e500reg.h>
     86  1.1.2.1  matt #include <powerpc/booke/e500var.h>
     87  1.1.2.1  matt #include <powerpc/booke/etsecreg.h>
     88  1.1.2.1  matt #include <powerpc/booke/openpicreg.h>
     89  1.1.2.1  matt #ifdef CADMUS
     90  1.1.2.1  matt #include <evbppc/mpc85xx/cadmusreg.h>
     91  1.1.2.1  matt #endif
     92  1.1.2.1  matt #ifdef PIXIS
     93  1.1.2.1  matt #include <evbppc/mpc85xx/pixisreg.h>
     94  1.1.2.1  matt #endif
     95  1.1.2.1  matt 
     96  1.1.2.1  matt #include "ksyms.h"
     97  1.1.2.1  matt 
     98  1.1.2.1  matt void	initppc(vaddr_t, vaddr_t);
     99  1.1.2.1  matt 
    100  1.1.2.1  matt #define	MEMREGIONS	4
    101  1.1.2.1  matt phys_ram_seg_t physmemr[MEMREGIONS];         /* All memory */
    102  1.1.2.1  matt phys_ram_seg_t availmemr[MEMREGIONS];        /* Available memory */
    103  1.1.2.1  matt static u_int nmemr;
    104  1.1.2.1  matt 
    105  1.1.2.1  matt #ifndef CONSFREQ
    106  1.1.2.1  matt # define CONSFREQ	-1            /* inherit from firmware */
    107  1.1.2.1  matt #endif
    108  1.1.2.1  matt #ifndef CONSPEED
    109  1.1.2.1  matt # define CONSPEED	115200
    110  1.1.2.1  matt #endif
    111  1.1.2.1  matt #ifndef CONMODE
    112  1.1.2.1  matt # define CONMODE	((TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8)
    113  1.1.2.1  matt #endif
    114  1.1.2.1  matt #ifndef CONSADDR
    115  1.1.2.1  matt # define CONSADDR	DUART2_BASE
    116  1.1.2.1  matt #endif
    117  1.1.2.1  matt 
    118  1.1.2.1  matt int		comcnfreq  = CONSFREQ;
    119  1.1.2.1  matt int		comcnspeed = CONSPEED;
    120  1.1.2.1  matt tcflag_t	comcnmode  = CONMODE;
    121  1.1.2.1  matt bus_addr_t	comcnaddr  = (bus_addr_t)CONSADDR;
    122  1.1.2.1  matt 
    123  1.1.2.1  matt #if NPCI > 0
    124  1.1.2.1  matt struct extent *pcimem_ex;
    125  1.1.2.1  matt struct extent *pciio_ex;
    126  1.1.2.1  matt #endif
    127  1.1.2.1  matt 
    128  1.1.2.1  matt struct powerpc_bus_space gur_bst = {
    129  1.1.2.1  matt 	.pbs_flags = _BUS_SPACE_BIG_ENDIAN|_BUS_SPACE_MEM_TYPE,
    130  1.1.2.1  matt 	.pbs_offset = GUR_BASE,
    131  1.1.2.1  matt 	.pbs_limit = GUR_SIZE,
    132  1.1.2.1  matt };
    133  1.1.2.1  matt 
    134  1.1.2.4  matt struct powerpc_bus_space gur_le_bst = {
    135  1.1.2.4  matt 	.pbs_flags = _BUS_SPACE_LITTLE_ENDIAN|_BUS_SPACE_MEM_TYPE,
    136  1.1.2.4  matt 	.pbs_offset = GUR_BASE,
    137  1.1.2.4  matt 	.pbs_limit = GUR_SIZE,
    138  1.1.2.4  matt };
    139  1.1.2.4  matt 
    140  1.1.2.1  matt const bus_space_handle_t gur_bsh = (bus_space_handle_t)(uintptr_t)(GUR_BASE);
    141  1.1.2.1  matt 
    142  1.1.2.1  matt #ifdef CADMUS
    143  1.1.2.1  matt static uint8_t cadmus_pci;
    144  1.1.2.1  matt static uint8_t cadmus_csr;
    145  1.1.2.1  matt static uint64_t e500_sys_clk = 33333333; /* 33.333333Mhz */
    146  1.1.2.1  matt #elif defined(PIXIS)
    147  1.1.2.1  matt static const uint32_t pixis_spd_map[8] = {
    148  1.1.2.1  matt     [PX_SPD_33MHZ] = 33333333,
    149  1.1.2.1  matt     [PX_SPD_40MHZ] = 40000000,
    150  1.1.2.1  matt     [PX_SPD_50MHZ] = 50000000,
    151  1.1.2.1  matt     [PX_SPD_66MHZ] = 66666666,
    152  1.1.2.1  matt     [PX_SPD_83MHZ] = 83333333,
    153  1.1.2.3  matt     [PX_SPD_100MHZ] = 100000000,
    154  1.1.2.1  matt     [PX_SPD_133MHZ] = 133333333,
    155  1.1.2.1  matt     [PX_SPD_166MHZ] = 166666667,
    156  1.1.2.1  matt };
    157  1.1.2.1  matt static uint8_t pixis_spd;
    158  1.1.2.1  matt static uint64_t e500_sys_clk;
    159  1.1.2.1  matt #elif defined(SYS_CLK)
    160  1.1.2.1  matt static uint64_t e500_sys_clk = SYS_CLK;	/* from config file */
    161  1.1.2.1  matt #else
    162  1.1.2.1  matt static uint64_t e500_sys_clk = 66666667; /* 66.666667Mhz */
    163  1.1.2.1  matt #endif
    164  1.1.2.1  matt 
    165  1.1.2.1  matt static int e500_cngetc(dev_t);
    166  1.1.2.1  matt static void e500_cnputc(dev_t, int);
    167  1.1.2.1  matt 
    168  1.1.2.1  matt static struct consdev e500_earlycons = {
    169  1.1.2.1  matt 	.cn_getc = e500_cngetc,
    170  1.1.2.1  matt 	.cn_putc = e500_cnputc,
    171  1.1.2.1  matt 	.cn_pollc = nullcnpollc,
    172  1.1.2.1  matt };
    173  1.1.2.1  matt 
    174  1.1.2.1  matt /*
    175  1.1.2.1  matt  * List of port-specific devices to attach to the processor local bus.
    176  1.1.2.1  matt  */
    177  1.1.2.1  matt static const struct cpunode_locators mpc8548_cpunode_locs[] = {
    178  1.1.2.1  matt 	{ "cpu" },	/* not a real device */
    179  1.1.2.1  matt 	{ "wdog" },	/* not a real device */
    180  1.1.2.1  matt 	{ "duart", DUART1_BASE, 2*DUART_SIZE, 0, 1,
    181  1.1.2.1  matt 		{ ISOURCE_DUART },
    182  1.1.2.1  matt 		1 + ilog2(DEVDISR_DUART) },
    183  1.1.2.1  matt #if defined(MPC8548) || defined(MPC8572)
    184  1.1.2.1  matt 	{ "tsec", ETSEC1_BASE, ETSEC_SIZE, 1, 3,
    185  1.1.2.1  matt 		{ ISOURCE_ETSEC1_TX, ISOURCE_ETSEC1_RX, ISOURCE_ETSEC1_ERR },
    186  1.1.2.1  matt 		1 + ilog2(DEVDISR_TSEC1) },
    187  1.1.2.1  matt 	{ "tsec", ETSEC2_BASE, ETSEC_SIZE, 2, 3,
    188  1.1.2.1  matt 		{ ISOURCE_ETSEC2_TX, ISOURCE_ETSEC2_RX, ISOURCE_ETSEC2_ERR },
    189  1.1.2.1  matt 		1 + ilog2(DEVDISR_TSEC2) },
    190  1.1.2.1  matt 	{ "tsec", ETSEC3_BASE, ETSEC_SIZE, 3, 3,
    191  1.1.2.1  matt 		{ ISOURCE_ETSEC3_TX, ISOURCE_ETSEC3_RX, ISOURCE_ETSEC3_ERR },
    192  1.1.2.1  matt 		1 + ilog2(DEVDISR_TSEC3) },
    193  1.1.2.1  matt 	{ "tsec", ETSEC4_BASE, ETSEC_SIZE, 4, 3,
    194  1.1.2.1  matt 		{ ISOURCE_ETSEC4_TX, ISOURCE_ETSEC4_RX, ISOURCE_ETSEC4_ERR },
    195  1.1.2.1  matt 		1 + ilog2(DEVDISR_TSEC4) },
    196  1.1.2.1  matt #endif
    197  1.1.2.1  matt #if defined(MPC8544) || defined(MPC8536)
    198  1.1.2.1  matt 	{ "tsec", ETSEC1_BASE, ETSEC_SIZE, 1, 3,
    199  1.1.2.1  matt 		{ ISOURCE_ETSEC1_TX, ISOURCE_ETSEC1_RX, ISOURCE_ETSEC1_ERR },
    200  1.1.2.1  matt 		1 + ilog2(DEVDISR_TSEC1) },
    201  1.1.2.1  matt 	{ "tsec", ETSEC3_BASE, ETSEC_SIZE, 2, 3,
    202  1.1.2.1  matt 		{ ISOURCE_ETSEC3_TX, ISOURCE_ETSEC3_RX, ISOURCE_ETSEC3_ERR },
    203  1.1.2.1  matt 		1 + ilog2(DEVDISR_TSEC2) },
    204  1.1.2.1  matt #endif
    205  1.1.2.1  matt 	{ "diic", I2C1_BASE, 2*I2C_SIZE, 0, 1,
    206  1.1.2.1  matt 		{ ISOURCE_I2C },
    207  1.1.2.1  matt 		1 + ilog2(DEVDISR_TSEC2) },
    208  1.1.2.1  matt #ifndef MPC8572
    209  1.1.2.1  matt 	/* MPC8572 doesn't have any GPIO */
    210  1.1.2.1  matt 	{ "gpio", GLOBAL_BASE, GLOBAL_SIZE, 0, 0 },
    211  1.1.2.1  matt #endif
    212  1.1.2.1  matt 	{ "ddrc", DDRC1_BASE, DDRC_SIZE, 0, 1,
    213  1.1.2.1  matt 		{ ISOURCE_DDR },
    214  1.1.2.1  matt 		1 + ilog2(DEVDISR_TSEC2) },
    215  1.1.2.1  matt #if defined(MPC8544) || defined(MPC8536)
    216  1.1.2.1  matt 	{ "pcie", PCIE1_BASE, PCI_SIZE, 1, 1,
    217  1.1.2.1  matt 		{ ISOURCE_PCIEX },
    218  1.1.2.1  matt 		1 + ilog2(DEVDISR_PCIE) },
    219  1.1.2.1  matt 	{ "pcie", PCIE2_MPC8544_BASE, PCI_SIZE, 2, 1,
    220  1.1.2.1  matt 		{ ISOURCE_PCIEX2 },
    221  1.1.2.1  matt 		1 + ilog2(DEVDISR_PCIE3) },
    222  1.1.2.1  matt 	{ "pcie", PCIE3_MPC8544_BASE, PCI_SIZE, 3, 1,
    223  1.1.2.1  matt 		{ ISOURCE_PCIEX3 },
    224  1.1.2.1  matt 		1 + ilog2(DEVDISR_PCIE2) },
    225  1.1.2.1  matt 	{ "pci", PCIX1_MPC8544_BASE, PCI_SIZE, 1, 1,
    226  1.1.2.1  matt 		{ ISOURCE_PCI1 },
    227  1.1.2.1  matt 		1 + ilog2(DEVDISR_PCI1) },
    228  1.1.2.1  matt #endif
    229  1.1.2.1  matt #ifdef MPC8548
    230  1.1.2.1  matt 	{ "pcie", PCIE1_BASE, PCI_SIZE, 0, 1,
    231  1.1.2.1  matt 		{ ISOURCE_PCIEX },
    232  1.1.2.1  matt 		1 + ilog2(DEVDISR_PCIE) },
    233  1.1.2.1  matt 	{ "pci", PCIX1_MPC8548_BASE, PCI_SIZE, 1, 1,
    234  1.1.2.1  matt 		{ ISOURCE_PCI1 },
    235  1.1.2.1  matt 		1 + ilog2(DEVDISR_PCI1) },
    236  1.1.2.1  matt 	{ "pci", PCIX2_MPC8548_BASE, PCI_SIZE, 2, 1,
    237  1.1.2.1  matt 		{ ISOURCE_PCI2 },
    238  1.1.2.1  matt 		1 + ilog2(DEVDISR_PCI2) },
    239  1.1.2.1  matt #endif
    240  1.1.2.1  matt #ifdef MPC8536
    241  1.1.2.1  matt 	{ "ehci", USB1_BASE, USB_SIZE, 1, 1,
    242  1.1.2.1  matt 		{ ISOURCE_USB1 },
    243  1.1.2.1  matt 		1 + ilog2(DEVDISR_USB1) },
    244  1.1.2.1  matt 	{ "ehci", USB2_BASE, USB_SIZE, 2, 1,
    245  1.1.2.1  matt 		{ ISOURCE_USB2 },
    246  1.1.2.1  matt 		1 + ilog2(DEVDISR_USB2) },
    247  1.1.2.1  matt 	{ "ehci", USB3_BASE, USB_SIZE, 3, 1,
    248  1.1.2.1  matt 		{ ISOURCE_USB3 },
    249  1.1.2.1  matt 		1 + ilog2(DEVDISR_USB3) },
    250  1.1.2.1  matt 	{ "sata", SATA1_BASE, SATA_SIZE, 1, 1,
    251  1.1.2.1  matt 		{ ISOURCE_SATA1 },
    252  1.1.2.1  matt 		1 + ilog2(DEVDISR_SATA1) },
    253  1.1.2.1  matt 	{ "sata", SATA2_BASE, SATA_SIZE, 2, 1,
    254  1.1.2.1  matt 		{ ISOURCE_SATA2 },
    255  1.1.2.1  matt 		1 + ilog2(DEVDISR_SATA2) },
    256  1.1.2.1  matt 	{ "spi", SPI_BASE, SPI_SIZE, 0, 1,
    257  1.1.2.1  matt 		{ ISOURCE_SPI },
    258  1.1.2.1  matt 		1 + ilog2(DEVDISR_SPI) },
    259  1.1.2.2  matt 	{ "sdhc", ESDHC_BASE, ESDHC_SIZE, 0, 1,
    260  1.1.2.1  matt 		{ ISOURCE_ESDHC },
    261  1.1.2.1  matt 		1 + ilog2(DEVDISR_ESDHC) },
    262  1.1.2.1  matt #endif
    263  1.1.2.1  matt 	{ "lbc", LBC_BASE, LBC_SIZE, 0, 1,
    264  1.1.2.1  matt 		{ ISOURCE_LBC },
    265  1.1.2.1  matt 		1 + ilog2(DEVDISR_LBC) },
    266  1.1.2.1  matt 	//{ "sec", RNG_BASE, RNG_SIZE, 0, 0, },
    267  1.1.2.1  matt 	{ NULL }
    268  1.1.2.1  matt };
    269  1.1.2.1  matt 
    270  1.1.2.1  matt static int
    271  1.1.2.1  matt e500_cngetc(dev_t dv)
    272  1.1.2.1  matt {
    273  1.1.2.1  matt 	volatile uint8_t * const com0addr = (void *)(GUR_BASE+CONSADDR);
    274  1.1.2.1  matt 
    275  1.1.2.1  matt         if ((com0addr[com_lsr] & LSR_RXRDY) == 0)
    276  1.1.2.1  matt 		return -1;
    277  1.1.2.1  matt 
    278  1.1.2.1  matt 	return com0addr[com_data] & 0xff;
    279  1.1.2.1  matt }
    280  1.1.2.1  matt 
    281  1.1.2.1  matt static void
    282  1.1.2.1  matt e500_cnputc(dev_t dv, int c)
    283  1.1.2.1  matt {
    284  1.1.2.1  matt 	volatile uint8_t * const com0addr = (void *)(GUR_BASE+CONSADDR);
    285  1.1.2.1  matt 	int timo = 150000;
    286  1.1.2.1  matt 
    287  1.1.2.1  matt 	while ((com0addr[com_lsr] & LSR_TXRDY) == 0 && --timo > 0)
    288  1.1.2.1  matt 		;
    289  1.1.2.1  matt 
    290  1.1.2.1  matt 	com0addr[com_data] = c;
    291  1.1.2.1  matt 	__asm("mbar");
    292  1.1.2.1  matt 
    293  1.1.2.1  matt 	while ((com0addr[com_lsr] & LSR_TSRE) == 0 && --timo > 0)
    294  1.1.2.1  matt 		;
    295  1.1.2.1  matt }
    296  1.1.2.1  matt 
    297  1.1.2.1  matt static void *
    298  1.1.2.1  matt gur_tlb_mapiodev(paddr_t pa, psize_t len)
    299  1.1.2.1  matt {
    300  1.1.2.1  matt 	if (pa < gur_bst.pbs_offset)
    301  1.1.2.1  matt 		return NULL;
    302  1.1.2.1  matt 	if (pa + len > gur_bst.pbs_offset + gur_bst.pbs_limit)
    303  1.1.2.1  matt 		return NULL;
    304  1.1.2.1  matt 	return (void *)pa;
    305  1.1.2.1  matt }
    306  1.1.2.1  matt 
    307  1.1.2.1  matt static void *(* const early_tlb_mapiodev)(paddr_t, psize_t) = gur_tlb_mapiodev;
    308  1.1.2.1  matt 
    309  1.1.2.1  matt static void
    310  1.1.2.1  matt e500_cpu_reset(void)
    311  1.1.2.1  matt {
    312  1.1.2.1  matt 	__asm volatile("sync");
    313  1.1.2.1  matt 	cpu_write_4(GLOBAL_BASE + RSTCR, HRESET_REQ);
    314  1.1.2.1  matt 	__asm volatile("msync;isync");
    315  1.1.2.1  matt }
    316  1.1.2.1  matt 
    317  1.1.2.1  matt static psize_t
    318  1.1.2.1  matt memprobe(vaddr_t endkernel)
    319  1.1.2.1  matt {
    320  1.1.2.1  matt 	phys_ram_seg_t *mr;
    321  1.1.2.1  matt 
    322  1.1.2.1  matt 	/*
    323  1.1.2.1  matt 	 * First we need to find out how much physical memory we have.
    324  1.1.2.1  matt 	 * We could let our bootloader tell us, but it's almost as easy
    325  1.1.2.1  matt 	 * to ask the DDR memory controller.
    326  1.1.2.1  matt 	 */
    327  1.1.2.1  matt 	mr = physmemr;
    328  1.1.2.1  matt #if 1
    329  1.1.2.1  matt 	for (u_int i = 0; i < 4; i++) {
    330  1.1.2.1  matt 		uint32_t v = cpu_read_4(DDRC1_BASE + CS_CONFIG(i));
    331  1.1.2.1  matt 		if (v & CS_CONFIG_EN) {
    332  1.1.2.1  matt 			v = cpu_read_4(DDRC1_BASE + CS_BNDS(i));
    333  1.1.2.1  matt 			mr->start = BNDS_SA_GET(v);
    334  1.1.2.1  matt 			mr->size  = BNDS_SIZE_GET(v);
    335  1.1.2.1  matt 			mr++;
    336  1.1.2.1  matt 		}
    337  1.1.2.1  matt 	}
    338  1.1.2.1  matt 
    339  1.1.2.1  matt 	if (mr == physmemr)
    340  1.1.2.1  matt 		panic("no memory configured!");
    341  1.1.2.1  matt #else
    342  1.1.2.1  matt 	mr->start = 0;
    343  1.1.2.1  matt 	mr->size = 32 << 20;
    344  1.1.2.1  matt 	mr++;
    345  1.1.2.1  matt #endif
    346  1.1.2.1  matt 
    347  1.1.2.1  matt 	/*
    348  1.1.2.1  matt 	 * Sort memory regions from low to high and coalesce adjacent regions
    349  1.1.2.1  matt 	 */
    350  1.1.2.1  matt 	u_int cnt = mr - physmemr;
    351  1.1.2.1  matt 	if (cnt > 1) {
    352  1.1.2.1  matt 		for (u_int i = 0; i < cnt - 1; i++) {
    353  1.1.2.1  matt 			for (u_int j = i + 1; j < cnt; j++) {
    354  1.1.2.1  matt 				if (physmemr[j].start < physmemr[i].start) {
    355  1.1.2.1  matt 					phys_ram_seg_t tmp = physmemr[i];
    356  1.1.2.1  matt 					physmemr[i] = physmemr[j];
    357  1.1.2.1  matt 					physmemr[j] = tmp;
    358  1.1.2.1  matt 				}
    359  1.1.2.1  matt 			}
    360  1.1.2.1  matt 		}
    361  1.1.2.1  matt 		mr = physmemr;
    362  1.1.2.1  matt 		for (u_int i = 0; i < cnt; i++, mr++) {
    363  1.1.2.1  matt 			if (mr->start + mr->size == mr[1].start) {
    364  1.1.2.1  matt 				mr->size += mr[1].size;
    365  1.1.2.1  matt 				for (u_int j = 1; j < cnt - i; j++)
    366  1.1.2.1  matt 					mr[j] = mr[j+1];
    367  1.1.2.1  matt 				cnt--;
    368  1.1.2.1  matt 			}
    369  1.1.2.1  matt 		}
    370  1.1.2.1  matt 	}
    371  1.1.2.1  matt 
    372  1.1.2.1  matt 	/*
    373  1.1.2.1  matt 	 * Copy physical memory to available memory.
    374  1.1.2.1  matt 	 */
    375  1.1.2.1  matt 	memcpy(availmemr, physmemr, cnt * sizeof(physmemr[0]));
    376  1.1.2.1  matt 
    377  1.1.2.1  matt 	/*
    378  1.1.2.1  matt 	 * Adjust available memory to skip kernel at start of memory.
    379  1.1.2.1  matt 	 */
    380  1.1.2.1  matt 	availmemr[0].size -= endkernel - availmemr[0].start;
    381  1.1.2.1  matt 	availmemr[0].start = endkernel;
    382  1.1.2.1  matt 
    383  1.1.2.1  matt 	/*
    384  1.1.2.1  matt 	 * Steal pages at the end of memory for the kernel message buffer.
    385  1.1.2.1  matt 	 */
    386  1.1.2.1  matt 	availmemr[cnt-1].size -= round_page(MSGBUFSIZE);
    387  1.1.2.1  matt 	msgbuf_paddr =
    388  1.1.2.1  matt 	    (uintptr_t)(availmemr[cnt-1].start + availmemr[cnt-1].size);
    389  1.1.2.1  matt 
    390  1.1.2.1  matt 	/*
    391  1.1.2.1  matt 	 * Calculate physmem.
    392  1.1.2.1  matt 	 */
    393  1.1.2.1  matt 	for (u_int i = 0; i < cnt; i++)
    394  1.1.2.1  matt 		physmem += atop(physmemr[i].size);
    395  1.1.2.1  matt 
    396  1.1.2.1  matt 	nmemr = cnt;
    397  1.1.2.1  matt 	return physmemr[cnt-1].start + physmemr[cnt-1].size;
    398  1.1.2.1  matt }
    399  1.1.2.1  matt 
    400  1.1.2.1  matt void
    401  1.1.2.1  matt consinit(void)
    402  1.1.2.1  matt {
    403  1.1.2.1  matt 	static bool attached = false;
    404  1.1.2.1  matt 
    405  1.1.2.1  matt 	if (attached)
    406  1.1.2.1  matt 		return;
    407  1.1.2.1  matt 	attached = true;
    408  1.1.2.1  matt 
    409  1.1.2.1  matt 	if (comcnfreq == -1) {
    410  1.1.2.1  matt 		const uint32_t porpplsr = cpu_read_4(GLOBAL_BASE + PORPLLSR);
    411  1.1.2.1  matt 		const uint32_t plat_ratio = PLAT_RATIO_GET(porpplsr);
    412  1.1.2.1  matt 		comcnfreq = e500_sys_clk * plat_ratio;
    413  1.1.2.1  matt 		printf(" comcnfreq=%u", comcnfreq);
    414  1.1.2.1  matt 	}
    415  1.1.2.1  matt 
    416  1.1.2.1  matt 	comcnattach(&gur_bst, comcnaddr, comcnspeed, comcnfreq,
    417  1.1.2.1  matt 	    COM_TYPE_NORMAL, comcnmode);
    418  1.1.2.1  matt }
    419  1.1.2.1  matt 
    420  1.1.2.1  matt void
    421  1.1.2.1  matt cpu_probe_cache(void)
    422  1.1.2.1  matt {
    423  1.1.2.1  matt 	struct cpu_info * const ci = curcpu();
    424  1.1.2.1  matt 	const uint32_t l1cfg0 = mfspr(SPR_L1CFG0);
    425  1.1.2.1  matt 
    426  1.1.2.1  matt 	ci->ci_ci.dcache_size = L1CFG_CSIZE_GET(l1cfg0);
    427  1.1.2.1  matt 	ci->ci_ci.dcache_line_size = 32 << L1CFG_CBSIZE_GET(l1cfg0);
    428  1.1.2.1  matt 
    429  1.1.2.1  matt 	if (L1CFG_CARCH_GET(l1cfg0) == L1CFG_CARCH_HARVARD) {
    430  1.1.2.1  matt 		const uint32_t l1cfg1 = mfspr(SPR_L1CFG1);
    431  1.1.2.1  matt 
    432  1.1.2.1  matt 		ci->ci_ci.icache_size = L1CFG_CSIZE_GET(l1cfg1);
    433  1.1.2.1  matt 		ci->ci_ci.icache_line_size = 32 << L1CFG_CBSIZE_GET(l1cfg1);
    434  1.1.2.1  matt 	} else {
    435  1.1.2.1  matt 		ci->ci_ci.icache_size = ci->ci_ci.dcache_size;
    436  1.1.2.1  matt 		ci->ci_ci.icache_line_size = ci->ci_ci.dcache_line_size;
    437  1.1.2.1  matt 	}
    438  1.1.2.1  matt 
    439  1.1.2.1  matt #ifdef DEBUG
    440  1.1.2.1  matt 	uint32_t l1csr0 = mfspr(SPR_L1CSR0);
    441  1.1.2.1  matt 	if ((L1CSR_CE & l1csr0) == 0)
    442  1.1.2.1  matt 		printf(" DC=off");
    443  1.1.2.1  matt 
    444  1.1.2.1  matt 	uint32_t l1csr1 = mfspr(SPR_L1CSR1);
    445  1.1.2.1  matt 	if ((L1CSR_CE & l1csr1) == 0)
    446  1.1.2.1  matt 		printf(" IC=off");
    447  1.1.2.1  matt #endif
    448  1.1.2.1  matt }
    449  1.1.2.1  matt 
    450  1.1.2.1  matt static const char *
    451  1.1.2.1  matt socname(uint32_t svr)
    452  1.1.2.1  matt {
    453  1.1.2.1  matt 	svr &= ~0x80000;
    454  1.1.2.1  matt 	switch (svr >> 8) {
    455  1.1.2.1  matt 	case SVR_MPC8548v2 >> 8: return "MPC8548";
    456  1.1.2.1  matt 	case SVR_MPC8547v2 >> 8: return "MPC8547";
    457  1.1.2.1  matt 	case SVR_MPC8545v2 >> 8: return "MPC8545";
    458  1.1.2.1  matt 	case SVR_MPC8543v2 >> 8: return "MPC8543";
    459  1.1.2.1  matt 	case SVR_MPC8544v1 >> 8: return "MPC8544";
    460  1.1.2.1  matt 	case SVR_MPC8536v1 >> 8: return "MPC8536";
    461  1.1.2.1  matt 	case SVR_MPC8572 >> 8: return "MPC8572";
    462  1.1.2.1  matt 	default:
    463  1.1.2.1  matt 		panic("%s: unknown SVR %#x", __func__, svr);
    464  1.1.2.1  matt 	}
    465  1.1.2.1  matt }
    466  1.1.2.1  matt 
    467  1.1.2.1  matt static void
    468  1.1.2.1  matt e500_tlb_print(device_t self, const char *name, uint32_t tlbcfg)
    469  1.1.2.1  matt {
    470  1.1.2.1  matt 	static const char units[16] = "KKKKKMMMMMGGGGGT";
    471  1.1.2.1  matt 
    472  1.1.2.1  matt 	const uint32_t minsize = 1U << (2 * TLBCFG_MINSIZE(tlbcfg));
    473  1.1.2.1  matt 	const uint32_t assoc = TLBCFG_ASSOC(tlbcfg);
    474  1.1.2.1  matt 	const u_int maxsize_log4k = TLBCFG_MAXSIZE(tlbcfg);
    475  1.1.2.1  matt 	const uint64_t maxsize = 1ULL << (2 * maxsize_log4k % 10);
    476  1.1.2.1  matt 	const uint32_t nentries = TLBCFG_NENTRY(tlbcfg);
    477  1.1.2.1  matt 
    478  1.1.2.1  matt 	aprint_normal_dev(self, "%s:", name);
    479  1.1.2.1  matt 
    480  1.1.2.1  matt 	aprint_normal(" %u", nentries);
    481  1.1.2.1  matt 	if (TLBCFG_AVAIL_P(tlbcfg)) {
    482  1.1.2.1  matt 		aprint_normal(" variable-size (%uKB..%"PRIu64"%cB)",
    483  1.1.2.1  matt 		    minsize, maxsize, units[maxsize_log4k]);
    484  1.1.2.1  matt 	} else {
    485  1.1.2.1  matt 		aprint_normal(" fixed-size (%uKB)", minsize);
    486  1.1.2.1  matt 	}
    487  1.1.2.1  matt 	if (assoc == 0 || assoc == nentries)
    488  1.1.2.1  matt 		aprint_normal(" fully");
    489  1.1.2.1  matt 	else
    490  1.1.2.1  matt 		aprint_normal(" %u-way set", assoc);
    491  1.1.2.1  matt 	aprint_normal(" associative entries\n");
    492  1.1.2.1  matt }
    493  1.1.2.1  matt 
    494  1.1.2.1  matt static void
    495  1.1.2.1  matt e500_cpu_attach(device_t self, u_int instance)
    496  1.1.2.1  matt {
    497  1.1.2.1  matt 	struct cpu_info * const ci = &cpu_info[instance];
    498  1.1.2.1  matt 
    499  1.1.2.1  matt 	KASSERT(instance == 0);
    500  1.1.2.1  matt 	self->dv_private = ci;
    501  1.1.2.1  matt 
    502  1.1.2.1  matt 	ci->ci_cpuid = instance;
    503  1.1.2.1  matt 	ci->ci_dev = self;
    504  1.1.2.1  matt         //ci->ci_idlespin = cpu_idlespin;
    505  1.1.2.1  matt 	if (instance > 0) {
    506  1.1.2.1  matt 		ci->ci_idepth = -1;
    507  1.1.2.1  matt 		cpu_probe_cache();
    508  1.1.2.1  matt 	}
    509  1.1.2.1  matt 
    510  1.1.2.1  matt 	uint64_t freq = board_info_get_number("processor-frequency");
    511  1.1.2.1  matt 	char freqbuf[10];
    512  1.1.2.1  matt 	if (freq >= 999500000) {
    513  1.1.2.1  matt 		const uint32_t freq32 = (freq + 500000) / 10000000;
    514  1.1.2.1  matt 		snprintf(freqbuf, sizeof(freqbuf), "%u.%02u GHz",
    515  1.1.2.1  matt 		    freq32 / 100, freq32 % 100);
    516  1.1.2.1  matt 	} else {
    517  1.1.2.1  matt 		const uint32_t freq32 = (freq + 500000) / 1000000;
    518  1.1.2.1  matt 		snprintf(freqbuf, sizeof(freqbuf), "%u MHz", freq32);
    519  1.1.2.1  matt 	}
    520  1.1.2.1  matt 
    521  1.1.2.1  matt 	const uint32_t pvr = mfpvr();
    522  1.1.2.1  matt 	const uint32_t svr = mfspr(SPR_SVR);
    523  1.1.2.1  matt 	const uint32_t pir = mfspr(SPR_PIR);
    524  1.1.2.1  matt 
    525  1.1.2.1  matt 	aprint_normal_dev(self, "%s %s%s %u.%u with an e500%s %u.%u core, "
    526  1.1.2.1  matt 	   "ID %u%s\n",
    527  1.1.2.1  matt 	   freqbuf, socname(svr), (SVR_SECURITY_P(svr) ? "E" : ""),
    528  1.1.2.1  matt 	   (svr >> 4) & 15, svr & 15,
    529  1.1.2.1  matt 	   (pvr >> 16) == PVR_MPCe500v2 ? "v2" : "",
    530  1.1.2.1  matt 	   (pvr >> 4) & 15, pvr & 15,
    531  1.1.2.1  matt 	   pir, (pir == 0 ? " (Primary)" : ""));
    532  1.1.2.1  matt 
    533  1.1.2.1  matt 	const uint32_t l1cfg0 = mfspr(SPR_L1CFG0);
    534  1.1.2.1  matt 	aprint_normal_dev(self,
    535  1.1.2.1  matt 	    "%uKB/%uB %u-way L1 %s cache\n",
    536  1.1.2.1  matt 	    L1CFG_CSIZE_GET(l1cfg0) >> 10,
    537  1.1.2.1  matt 	    32 << L1CFG_CBSIZE_GET(l1cfg0),
    538  1.1.2.1  matt 	    L1CFG_CNWAY_GET(l1cfg0),
    539  1.1.2.1  matt 	    L1CFG_CARCH_GET(l1cfg0) == L1CFG_CARCH_HARVARD
    540  1.1.2.1  matt 		? "data" : "unified");
    541  1.1.2.1  matt 
    542  1.1.2.1  matt 	if (L1CFG_CARCH_GET(l1cfg0) == L1CFG_CARCH_HARVARD) {
    543  1.1.2.1  matt 		const uint32_t l1cfg1 = mfspr(SPR_L1CFG1);
    544  1.1.2.1  matt 		aprint_normal_dev(self,
    545  1.1.2.1  matt 		    "%uKB/%uB %u-way L1 %s cache\n",
    546  1.1.2.1  matt 		    L1CFG_CSIZE_GET(l1cfg1) >> 10,
    547  1.1.2.1  matt 		    32 << L1CFG_CBSIZE_GET(l1cfg1),
    548  1.1.2.1  matt 		    L1CFG_CNWAY_GET(l1cfg1),
    549  1.1.2.1  matt 		    "instruction");
    550  1.1.2.1  matt 	}
    551  1.1.2.1  matt 
    552  1.1.2.1  matt 	const uint32_t mmucfg = mfspr(SPR_MMUCFG);
    553  1.1.2.1  matt 	aprint_normal_dev(self,
    554  1.1.2.1  matt 	    "%u TLBs, %u concurrent %u-bit PIDs (%u total)\n",
    555  1.1.2.1  matt 	    MMUCFG_NTLBS_GET(mmucfg) + 1,
    556  1.1.2.1  matt 	    MMUCFG_NPIDS_GET(mmucfg),
    557  1.1.2.1  matt 	    MMUCFG_PIDSIZE_GET(mmucfg) + 1,
    558  1.1.2.1  matt 	    1 << (MMUCFG_PIDSIZE_GET(mmucfg) + 1));
    559  1.1.2.1  matt 
    560  1.1.2.1  matt 	e500_tlb_print(self, "tlb0", mfspr(SPR_TLB0CFG));
    561  1.1.2.1  matt 	e500_tlb_print(self, "tlb1", mfspr(SPR_TLB1CFG));
    562  1.1.2.1  matt 
    563  1.1.2.1  matt 	intr_cpu_init(ci);
    564  1.1.2.1  matt 	cpu_evcnt_attach(ci);
    565  1.1.2.1  matt }
    566  1.1.2.1  matt 
    567  1.1.2.1  matt static void
    568  1.1.2.1  matt calltozero(void)
    569  1.1.2.1  matt {
    570  1.1.2.1  matt 	panic("call to 0 from %p", __builtin_return_address(0));
    571  1.1.2.1  matt }
    572  1.1.2.1  matt 
    573  1.1.2.1  matt void
    574  1.1.2.1  matt initppc(vaddr_t startkernel, vaddr_t endkernel)
    575  1.1.2.1  matt {
    576  1.1.2.1  matt 	struct cpu_info * const ci = curcpu();
    577  1.1.2.1  matt 	struct cpu_softc * const cpu = ci->ci_softc;
    578  1.1.2.1  matt 
    579  1.1.2.1  matt 	cn_tab = &e500_earlycons;
    580  1.1.2.1  matt 	printf(" initppc<enter>");
    581  1.1.2.1  matt 
    582  1.1.2.1  matt 	const register_t hid0 = mfspr(SPR_HID0);
    583  1.1.2.1  matt 	mtspr(SPR_HID0, hid0 | HID0_TBEN | HID0_EMCP);
    584  1.1.2.1  matt #ifdef CADMUS
    585  1.1.2.1  matt 	/*
    586  1.1.2.1  matt 	 * Need to cache this from cadmus since we need to unmap cadmus since
    587  1.1.2.1  matt 	 * it falls in the middle of kernel address space.
    588  1.1.2.1  matt 	 */
    589  1.1.2.1  matt 	cadmus_pci = ((uint8_t *)0xf8004000)[CM_PCI];
    590  1.1.2.1  matt 	cadmus_csr = ((uint8_t *)0xf8004000)[CM_CSR];
    591  1.1.2.1  matt 	((uint8_t *)0xf8004000)[CM_CSR] |= CM_RST_PHYRST;
    592  1.1.2.1  matt 	printf(" cadmus_pci=%#x", cadmus_pci);
    593  1.1.2.1  matt 	printf(" cadmus_csr=%#x", cadmus_csr);
    594  1.1.2.1  matt 	((uint8_t *)0xf8004000)[CM_CSR] = 0;
    595  1.1.2.1  matt 	if ((cadmus_pci & CM_PCI_PSPEED) == CM_PCI_PSPEED_66) {
    596  1.1.2.1  matt 		e500_sys_clk *= 2;
    597  1.1.2.1  matt 	}
    598  1.1.2.1  matt #endif
    599  1.1.2.1  matt #ifdef PIXIS
    600  1.1.2.1  matt 	pixis_spd = ((uint8_t *)PX_BASE)[PX_SPD];
    601  1.1.2.1  matt 	printf(" pixis_spd=%#x ", pixis_spd);
    602  1.1.2.1  matt 	e500_sys_clk = pixis_spd_map[PX_SPD_SYSCLK_GET(pixis_spd)];
    603  1.1.2.1  matt #endif
    604  1.1.2.1  matt 	printf(" porpllsr=0x%08x",
    605  1.1.2.1  matt 	    *(uint32_t *)(GUR_BASE + GLOBAL_BASE + PORPLLSR));
    606  1.1.2.1  matt 	printf(" sys_clk=%"PRIu64, e500_sys_clk);
    607  1.1.2.1  matt 
    608  1.1.2.1  matt 	/*
    609  1.1.2.1  matt 	 * Make sure arguments are page aligned.
    610  1.1.2.1  matt 	 */
    611  1.1.2.1  matt 	startkernel = trunc_page(startkernel);
    612  1.1.2.1  matt 	endkernel = round_page(endkernel);
    613  1.1.2.1  matt 
    614  1.1.2.1  matt 	/*
    615  1.1.2.1  matt 	 * Initialize the bus space tag used to access the 85xx general
    616  1.1.2.1  matt 	 * utility registers.  It doesn't need to be extent protected.
    617  1.1.2.1  matt 	 * We know the GUR is mapped via a TLB1 entry so we add a limited
    618  1.1.2.1  matt 	 * mapiodev which allows mappings in GUR space.
    619  1.1.2.1  matt 	 */
    620  1.1.2.1  matt 	CTASSERT(offsetof(struct tlb_md_ops, md_tlb_mapiodev) == 0);
    621  1.1.2.1  matt 	cpu_md_ops.md_tlb_ops = (const void *)&early_tlb_mapiodev;
    622  1.1.2.1  matt 	bus_space_init(&gur_bst, NULL, NULL, 0);
    623  1.1.2.4  matt 	bus_space_init(&gur_le_bst, NULL, NULL, 0);
    624  1.1.2.1  matt 	cpu->cpu_bst = &gur_bst;
    625  1.1.2.4  matt 	cpu->cpu_le_bst = &gur_le_bst;
    626  1.1.2.1  matt 	cpu->cpu_bsh = gur_bsh;
    627  1.1.2.1  matt 
    628  1.1.2.1  matt 	/*
    629  1.1.2.1  matt 	 * Attach the console early, really early.
    630  1.1.2.1  matt 	 */
    631  1.1.2.1  matt 	consinit();
    632  1.1.2.1  matt 
    633  1.1.2.1  matt 	/*
    634  1.1.2.1  matt 	 * Reset the PIC to a known state.
    635  1.1.2.1  matt 	 */
    636  1.1.2.1  matt 	cpu_write_4(OPENPIC_BASE + OPENPIC_GCR, GCR_RST);
    637  1.1.2.1  matt 	while (cpu_read_4(OPENPIC_BASE + OPENPIC_GCR) & GCR_RST)
    638  1.1.2.1  matt 		;
    639  1.1.2.1  matt #if 0
    640  1.1.2.1  matt 	cpu_write_4(OPENPIC_BASE + OPENPIC_CTPR, 15);	/* IPL_HIGH */
    641  1.1.2.1  matt #endif
    642  1.1.2.1  matt 	printf(" openpic-reset(ctpr=%u)",
    643  1.1.2.1  matt 	    cpu_read_4(OPENPIC_BASE + OPENPIC_CTPR));
    644  1.1.2.1  matt 
    645  1.1.2.1  matt 	/*
    646  1.1.2.1  matt 	 * fill in with an absolute branch to a routine that will panic.
    647  1.1.2.1  matt 	 */
    648  1.1.2.1  matt 	*(int *)0 = 0x48000002 | (int) calltozero;
    649  1.1.2.1  matt 
    650  1.1.2.1  matt 	/*
    651  1.1.2.1  matt 	 * Get the cache sizes.
    652  1.1.2.1  matt 	 */
    653  1.1.2.1  matt 	cpu_probe_cache();
    654  1.1.2.1  matt 		printf(" cache(DC=%u/%u,IC=%u/%u)",
    655  1.1.2.1  matt 		    ci->ci_ci.dcache_size >> 10,
    656  1.1.2.1  matt 		    ci->ci_ci.dcache_line_size,
    657  1.1.2.1  matt 		    ci->ci_ci.icache_size >> 10,
    658  1.1.2.1  matt 		    ci->ci_ci.icache_line_size);
    659  1.1.2.1  matt 
    660  1.1.2.1  matt 	/*
    661  1.1.2.1  matt 	 * Now find out how much memory is attached
    662  1.1.2.1  matt 	 */
    663  1.1.2.1  matt 	pmemsize = memprobe(endkernel);
    664  1.1.2.4  matt 	cpu->cpu_highmem = pmemsize;
    665  1.1.2.1  matt 		printf(" memprobe=%zuMB", (size_t) (pmemsize >> 20));
    666  1.1.2.1  matt 
    667  1.1.2.1  matt 	/*
    668  1.1.2.1  matt 	 * Now we need cleanout the TLB of stuff that we don't need.
    669  1.1.2.1  matt 	 */
    670  1.1.2.1  matt 	e500_tlb_init(endkernel, pmemsize);
    671  1.1.2.1  matt 		printf(" e500_tlbinit(%#lx,%zuMB)",
    672  1.1.2.1  matt 		    endkernel, (size_t) (pmemsize >> 20));
    673  1.1.2.1  matt 
    674  1.1.2.1  matt 	/*
    675  1.1.2.1  matt 	 *
    676  1.1.2.1  matt 	 */
    677  1.1.2.1  matt 	printf(" hid0=%#lx/%#lx", hid0, mfspr(SPR_HID0));
    678  1.1.2.1  matt 	printf(" hid1=%#lx", mfspr(SPR_HID1));
    679  1.1.2.1  matt 	printf(" pordevsr=%#x", cpu_read_4(GLOBAL_BASE + PORDEVSR));
    680  1.1.2.1  matt 	printf(" devdisr=%#x", cpu_read_4(GLOBAL_BASE + DEVDISR));
    681  1.1.2.1  matt 
    682  1.1.2.1  matt 	mtmsr(mfmsr() | PSL_CE | PSL_ME | PSL_DE);
    683  1.1.2.1  matt 
    684  1.1.2.1  matt 	/*
    685  1.1.2.1  matt 	 * Initialize the message buffer.
    686  1.1.2.1  matt 	 */
    687  1.1.2.1  matt 	initmsgbuf((void *)msgbuf_paddr, round_page(MSGBUFSIZE));
    688  1.1.2.1  matt 	printf(" msgbuf=%p", (void *)msgbuf_paddr);
    689  1.1.2.1  matt 
    690  1.1.2.1  matt 	/*
    691  1.1.2.1  matt 	 * Initialize exception vectors and interrupts
    692  1.1.2.1  matt 	 */
    693  1.1.2.1  matt 	exception_init(&e500_intrsw);
    694  1.1.2.1  matt 	printf(" exception_init=%p", &e500_intrsw);
    695  1.1.2.1  matt 	mtspr(SPR_TCR, TCR_WIE | mfspr(SPR_TCR));
    696  1.1.2.1  matt 
    697  1.1.2.1  matt 	/*
    698  1.1.2.1  matt 	 * Set the page size.
    699  1.1.2.1  matt 	 */
    700  1.1.2.1  matt 	uvm_setpagesize();
    701  1.1.2.1  matt 
    702  1.1.2.1  matt 	/*
    703  1.1.2.1  matt 	 * Initialize the pmap.
    704  1.1.2.1  matt 	 */
    705  1.1.2.1  matt 	pmap_bootstrap(startkernel, endkernel, availmemr, nmemr);
    706  1.1.2.1  matt 
    707  1.1.2.1  matt 	/*
    708  1.1.2.1  matt 	 * Let's take all the indirect calls via our stubs and patch
    709  1.1.2.1  matt 	 * them to be direct calls.
    710  1.1.2.1  matt 	 */
    711  1.1.2.1  matt 	booke_fixup_stubs();
    712  1.1.2.1  matt #if 0
    713  1.1.2.1  matt 	/*
    714  1.1.2.1  matt 	 * As a debug measure we can change the TLB entry that maps all of
    715  1.1.2.1  matt 	 * memory to one that encompasses the 64KB with the kernel vectors.
    716  1.1.2.1  matt 	 * All other pages will be soft faulted into the TLB as needed.
    717  1.1.2.1  matt 	 */
    718  1.1.2.1  matt 	const uint32_t saved_mas0 = mfspr(SPR_MAS0);
    719  1.1.2.1  matt 	mtspr(SPR_MAS6, 0);
    720  1.1.2.1  matt 	__asm volatile("tlbsx\t0, %0" :: "b"(startkernel));
    721  1.1.2.1  matt 	uint32_t mas0 = mfspr(SPR_MAS0);
    722  1.1.2.1  matt 	uint32_t mas1 = mfspr(SPR_MAS1);
    723  1.1.2.1  matt 	uint32_t mas2 = mfspr(SPR_MAS2);
    724  1.1.2.1  matt 	uint32_t mas3 = mfspr(SPR_MAS3);
    725  1.1.2.1  matt 	KASSERT(mas3 & MAS3_SW);
    726  1.1.2.1  matt 	KASSERT(mas3 & MAS3_SR);
    727  1.1.2.1  matt 	KASSERT(mas3 & MAS3_SX);
    728  1.1.2.1  matt 	mas1 = (mas1 & ~MAS1_TSIZE) | MASX_TSIZE_64KB;
    729  1.1.2.1  matt 	pt_entry_t xpn_mask = ~0 << (10 + 2 * MASX_TSIZE_GET(mas1));
    730  1.1.2.1  matt 	mas2 = (mas2 & ~(MAS2_EPN        )) | (startkernel & xpn_mask);
    731  1.1.2.1  matt 	mas3 = (mas3 & ~(MAS3_RPN|MAS3_SW)) | (startkernel & xpn_mask);
    732  1.1.2.1  matt 	printf(" %#lx=<%#x,%#x,%#x,%#x>", startkernel, mas0, mas1, mas2, mas3);
    733  1.1.2.1  matt #if 1
    734  1.1.2.1  matt 	mtspr(SPR_MAS1, mas1);
    735  1.1.2.1  matt 	mtspr(SPR_MAS2, mas2);
    736  1.1.2.1  matt 	mtspr(SPR_MAS3, mas3);
    737  1.1.2.1  matt 	extern void tlbwe(void);
    738  1.1.2.1  matt 	tlbwe();
    739  1.1.2.1  matt 	mtspr(SPR_MAS0, saved_mas0);
    740  1.1.2.1  matt 	printf("(ok)");
    741  1.1.2.1  matt #endif
    742  1.1.2.1  matt #endif
    743  1.1.2.1  matt 
    744  1.1.2.1  matt 	/*
    745  1.1.2.2  matt 	 * Initialize a few things in lwp0.
    746  1.1.2.2  matt 	 */
    747  1.1.2.2  matt 	lwp0.l_md.md_veccpu = curcpu();
    748  1.1.2.2  matt 	lwp0.l_md.md_fpucpu = curcpu();
    749  1.1.2.2  matt 	{
    750  1.1.2.2  matt 		extern void *proc0paddr;
    751  1.1.2.2  matt 		lwp0.l_addr = proc0paddr;
    752  1.1.2.2  matt 	}
    753  1.1.2.2  matt 	lwp0.l_md.md_utf = trapframe(&lwp0);
    754  1.1.2.2  matt 
    755  1.1.2.2  matt 	/*
    756  1.1.2.1  matt 	 * Set some more MD helpers
    757  1.1.2.1  matt 	 */
    758  1.1.2.1  matt 	cpu_md_ops.md_cpunode_locs = mpc8548_cpunode_locs;
    759  1.1.2.1  matt 	cpu_md_ops.md_device_register = e500_device_register;
    760  1.1.2.1  matt 	cpu_md_ops.md_cpu_attach = e500_cpu_attach;
    761  1.1.2.1  matt 	cpu_md_ops.md_cpu_reset = e500_cpu_reset;
    762  1.1.2.1  matt #if NGPIO > 0
    763  1.1.2.1  matt 	cpu_md_ops.md_cpunode_attach = pq3gpio_attach;
    764  1.1.2.1  matt #endif
    765  1.1.2.1  matt 
    766  1.1.2.1  matt #if NKSYMS || defined(DDB) || defined(LKM)
    767  1.1.2.1  matt 	{
    768  1.1.2.1  matt 		extern void *startsym, *endsym;
    769  1.1.2.1  matt 		ksyms_init((int)((u_int)endsym - (u_int)startsym),
    770  1.1.2.1  matt 		    startsym, endsym);
    771  1.1.2.1  matt 	}
    772  1.1.2.1  matt #endif
    773  1.1.2.1  matt 
    774  1.1.2.2  matt 	printf(" initppc done!\n");
    775  1.1.2.1  matt }
    776  1.1.2.1  matt 
    777  1.1.2.1  matt #ifdef MPC8548
    778  1.1.2.1  matt static const char * const mpc8548cds_extirq_names[] = {
    779  1.1.2.1  matt 	[0] = "pci inta",
    780  1.1.2.1  matt 	[1] = "pci intb",
    781  1.1.2.1  matt 	[2] = "pci intc",
    782  1.1.2.1  matt 	[3] = "pci intd",
    783  1.1.2.1  matt 	[4] = "irq4",
    784  1.1.2.1  matt 	[5] = "gige phy",
    785  1.1.2.1  matt 	[6] = "atm phy",
    786  1.1.2.1  matt 	[7] = "cpld",
    787  1.1.2.1  matt 	[8] = "irq8",
    788  1.1.2.1  matt 	[9] = "nvram",
    789  1.1.2.1  matt 	[10] = "debug",
    790  1.1.2.1  matt 	[11] = "pci2 inta",
    791  1.1.2.1  matt };
    792  1.1.2.1  matt #endif
    793  1.1.2.1  matt 
    794  1.1.2.1  matt static const char * const mpc85xx_extirq_names[] = {
    795  1.1.2.1  matt 	[0] = "extirq 0",
    796  1.1.2.1  matt 	[1] = "extirq 1",
    797  1.1.2.1  matt 	[2] = "extirq 2",
    798  1.1.2.1  matt 	[3] = "extirq 3",
    799  1.1.2.1  matt 	[4] = "extirq 4",
    800  1.1.2.1  matt 	[5] = "extirq 5",
    801  1.1.2.1  matt 	[6] = "extirq 6",
    802  1.1.2.1  matt 	[7] = "extirq 7",
    803  1.1.2.1  matt 	[8] = "extirq 8",
    804  1.1.2.1  matt 	[9] = "extirq 9",
    805  1.1.2.1  matt 	[10] = "extirq 10",
    806  1.1.2.1  matt 	[11] = "extirq 11",
    807  1.1.2.1  matt };
    808  1.1.2.1  matt 
    809  1.1.2.1  matt static void
    810  1.1.2.1  matt mpc85xx_extirq_setup(void)
    811  1.1.2.1  matt {
    812  1.1.2.1  matt #ifdef MPC8548
    813  1.1.2.1  matt 	const char * const * names = mpc8548cds_extirq_names;
    814  1.1.2.1  matt 	const size_t n = __arraycount(mpc8548cds_extirq_names);
    815  1.1.2.1  matt #else
    816  1.1.2.1  matt 	const char * const * names = mpc85xx_extirq_names;
    817  1.1.2.1  matt 	const size_t n = __arraycount(mpc85xx_extirq_names);
    818  1.1.2.1  matt #endif
    819  1.1.2.1  matt 	prop_array_t extirqs = prop_array_create_with_capacity(n);
    820  1.1.2.1  matt 	for (u_int i = 0; i < n; i++) {
    821  1.1.2.1  matt 		prop_string_t ps = prop_string_create_cstring_nocopy(names[i]);
    822  1.1.2.1  matt 		prop_array_set(extirqs, i, ps);
    823  1.1.2.1  matt 		prop_object_release(ps);
    824  1.1.2.1  matt 	}
    825  1.1.2.1  matt 	board_info_add_object("external-irqs", extirqs);
    826  1.1.2.1  matt 	prop_object_release(extirqs);
    827  1.1.2.1  matt }
    828  1.1.2.1  matt 
    829  1.1.2.1  matt static void
    830  1.1.2.1  matt mpc85xx_pci_setup(const char *name, uint32_t intmask, int ist, int inta, ...)
    831  1.1.2.1  matt {
    832  1.1.2.1  matt 	prop_dictionary_t pci_intmap = prop_dictionary_create();
    833  1.1.2.1  matt 	KASSERT(pci_intmap != NULL);
    834  1.1.2.1  matt 	prop_number_t mask = prop_number_create_unsigned_integer(intmask);
    835  1.1.2.1  matt 	KASSERT(mask != NULL);
    836  1.1.2.1  matt 	prop_dictionary_set(pci_intmap, "interrupt-mask", mask);
    837  1.1.2.1  matt 	prop_object_release(mask);
    838  1.1.2.1  matt 	prop_number_t pn_ist = prop_number_create_unsigned_integer(ist);
    839  1.1.2.1  matt 	KASSERT(pn_ist != NULL);
    840  1.1.2.1  matt 	prop_number_t pn_intr = prop_number_create_unsigned_integer(inta);
    841  1.1.2.1  matt 	KASSERT(pn_intr != NULL);
    842  1.1.2.1  matt 	prop_dictionary_t entry = prop_dictionary_create();
    843  1.1.2.1  matt 	KASSERT(entry != NULL);
    844  1.1.2.1  matt 	prop_dictionary_set(entry, "interrupt", pn_intr);
    845  1.1.2.1  matt 	prop_dictionary_set(entry, "type", pn_ist);
    846  1.1.2.1  matt 	prop_dictionary_set(pci_intmap, "000000", entry);
    847  1.1.2.1  matt 	prop_object_release(pn_intr);
    848  1.1.2.1  matt 	prop_object_release(entry);
    849  1.1.2.1  matt 	va_list ap;
    850  1.1.2.1  matt 	va_start(ap, inta);
    851  1.1.2.1  matt 	u_int intrinc = __LOWEST_SET_BIT(intmask);
    852  1.1.2.1  matt 	for (u_int i = 0; i < intmask; i += intrinc) {
    853  1.1.2.1  matt 		char prop_name[12];
    854  1.1.2.1  matt 		snprintf(prop_name, sizeof(prop_name), "%06x", i + intrinc);
    855  1.1.2.1  matt 		entry = prop_dictionary_create();
    856  1.1.2.1  matt 		KASSERT(entry != NULL);
    857  1.1.2.1  matt 		pn_intr = prop_number_create_unsigned_integer(va_arg(ap, u_int));
    858  1.1.2.1  matt 		KASSERT(pn_intr != NULL);
    859  1.1.2.1  matt 		prop_dictionary_set(entry, "interrupt", pn_intr);
    860  1.1.2.1  matt 		prop_dictionary_set(entry, "type", pn_ist);
    861  1.1.2.1  matt 		prop_dictionary_set(pci_intmap, prop_name, entry);
    862  1.1.2.1  matt 		prop_object_release(pn_intr);
    863  1.1.2.1  matt 		prop_object_release(entry);
    864  1.1.2.1  matt 	}
    865  1.1.2.1  matt 	va_end(ap);
    866  1.1.2.1  matt 	prop_object_release(pn_ist);
    867  1.1.2.1  matt 	board_info_add_object(name, pci_intmap);
    868  1.1.2.1  matt 	prop_object_release(pci_intmap);
    869  1.1.2.1  matt }
    870  1.1.2.1  matt 
    871  1.1.2.1  matt void
    872  1.1.2.1  matt cpu_startup(void)
    873  1.1.2.1  matt {
    874  1.1.2.1  matt 	struct cpu_info * const ci = curcpu();
    875  1.1.2.1  matt 
    876  1.1.2.2  matt 	booke_cpu_startup(socname(mfspr(SPR_SVR)));
    877  1.1.2.1  matt 
    878  1.1.2.1  matt 	uint32_t v = cpu_read_4(GLOBAL_BASE + PORPLLSR);
    879  1.1.2.1  matt 	uint32_t plat_ratio = PLAT_RATIO_GET(v);
    880  1.1.2.1  matt 	uint32_t e500_ratio = E500_RATIO_GET(v);
    881  1.1.2.1  matt 
    882  1.1.2.1  matt 	uint64_t ccb_freq = e500_sys_clk * plat_ratio;
    883  1.1.2.1  matt 	uint64_t cpu_freq = ccb_freq * e500_ratio / 2;
    884  1.1.2.1  matt 
    885  1.1.2.1  matt 	ci->ci_khz = (cpu_freq + 500) / 1000;
    886  1.1.2.1  matt 	cpu_timebase = ci->ci_data.cpu_cc_freq = ccb_freq / 8;
    887  1.1.2.1  matt 
    888  1.1.2.1  matt 	board_info_add_bool("pq3");
    889  1.1.2.1  matt 	board_info_add_number("mem-size", pmemsize);
    890  1.1.2.1  matt 	const uint32_t l2ctl = cpu_read_4(L2CACHE_BASE + L2CTL);
    891  1.1.2.1  matt 	uint32_t l2siz = L2CTL_L2SIZ_GET(l2ctl);
    892  1.1.2.1  matt 	uint32_t l2banks = l2siz >> 16;
    893  1.1.2.1  matt #ifdef MPC85555
    894  1.1.2.1  matt 	if (e500_get_svr() == (MPC8555v1 >> 16)) {
    895  1.1.2.1  matt 		l2siz >>= 1;
    896  1.1.2.1  matt 		l2banks >>= 1;
    897  1.1.2.1  matt 	}
    898  1.1.2.1  matt #endif
    899  1.1.2.1  matt 	board_info_add_number("l2-cache-size", l2siz);
    900  1.1.2.1  matt 	board_info_add_number("l2-cache-line-size", 32);
    901  1.1.2.1  matt 	board_info_add_number("l2-cache-banks", l2banks);
    902  1.1.2.1  matt 	board_info_add_number("l2-cache-ways", 8);
    903  1.1.2.1  matt 
    904  1.1.2.1  matt 	board_info_add_number("processor-frequency", cpu_freq);
    905  1.1.2.1  matt 	board_info_add_number("bus-frequency", ccb_freq);
    906  1.1.2.1  matt 	board_info_add_number("pci-frequency", e500_sys_clk);
    907  1.1.2.1  matt 	board_info_add_number("timebase-frequency", ccb_freq / 8);
    908  1.1.2.1  matt 
    909  1.1.2.1  matt #ifdef CADMUS
    910  1.1.2.1  matt 	const uint8_t phy_base = CM_CSR_EPHY_GET(cadmus_csr) << 2;
    911  1.1.2.1  matt 	board_info_add_number("tsec1-phy-addr", phy_base + 0);
    912  1.1.2.1  matt 	board_info_add_number("tsec2-phy-addr", phy_base + 1);
    913  1.1.2.1  matt 	board_info_add_number("tsec3-phy-addr", phy_base + 2);
    914  1.1.2.1  matt 	board_info_add_number("tsec4-phy-addr", phy_base + 3);
    915  1.1.2.1  matt #elif defined(PIXIS)
    916  1.1.2.1  matt 	board_info_add_number("tsec1-phy-addr", 1);
    917  1.1.2.1  matt 	board_info_add_number("tsec2-phy-addr", 0);
    918  1.1.2.1  matt #else
    919  1.1.2.1  matt 	board_info_add_number("tsec1-phy-addr", MII_PHY_ANY);
    920  1.1.2.1  matt 	board_info_add_number("tsec2-phy-addr", MII_PHY_ANY);
    921  1.1.2.1  matt 	board_info_add_number("tsec3-phy-addr", MII_PHY_ANY);
    922  1.1.2.1  matt 	board_info_add_number("tsec4-phy-addr", MII_PHY_ANY);
    923  1.1.2.1  matt #endif
    924  1.1.2.1  matt 
    925  1.1.2.1  matt 	uint64_t macstnaddr =
    926  1.1.2.1  matt 	    ((uint64_t)le32toh(cpu_read_4(ETSEC1_BASE + MACSTNADDR1)) << 16)
    927  1.1.2.1  matt 	    | ((uint64_t)le32toh(cpu_read_4(ETSEC1_BASE + MACSTNADDR2)) << 48);
    928  1.1.2.1  matt 	board_info_add_data("tsec-mac-addr-base", &macstnaddr, 6);
    929  1.1.2.1  matt 
    930  1.1.2.1  matt #if NPCI > 0 && defined(PCI_MEMBASE)
    931  1.1.2.1  matt 	pcimem_ex = extent_create("pcimem",
    932  1.1.2.1  matt 	    PCI_MEMBASE, PCI_MEMBASE + 4*PCI_MEMSIZE,
    933  1.1.2.1  matt 	    M_DEVBUF, NULL, 0, EX_WAITOK);
    934  1.1.2.1  matt #endif
    935  1.1.2.1  matt #if NPCI > 0 && defined(PCI_IOBASE)
    936  1.1.2.1  matt 	pciio_ex = extent_create("pciio",
    937  1.1.2.1  matt 	    PCI_IOBASE, PCI_IOBASE + 4*PCI_IOSIZE,
    938  1.1.2.1  matt 	    M_DEVBUF, NULL, 0, EX_WAITOK);
    939  1.1.2.1  matt #endif
    940  1.1.2.1  matt 	mpc85xx_extirq_setup();
    941  1.1.2.1  matt 	/*
    942  1.1.2.1  matt 	 * PCI-Express virtual wire interrupts on combined with
    943  1.1.2.1  matt 	 * External IRQ0/1/2/3.
    944  1.1.2.1  matt 	 */
    945  1.1.2.1  matt #if defined(MPC8548)
    946  1.1.2.1  matt 	mpc85xx_pci_setup("pcie0-interrupt-map", 0x001800, IST_LEVEL, 0, 1, 2, 3);
    947  1.1.2.1  matt #endif
    948  1.1.2.1  matt #if defined(MPC8544) || defined(MPC8572) || defined(MPC8536)
    949  1.1.2.1  matt 	mpc85xx_pci_setup("pcie1-interrupt-map", 0x001800, IST_LEVEL, 0, 1, 2, 3);
    950  1.1.2.1  matt 	mpc85xx_pci_setup("pcie2-interrupt-map", 0x001800, IST_LEVEL, 4, 5, 6, 7);
    951  1.1.2.1  matt 	mpc85xx_pci_setup("pcie3-interrupt-map", 0x001800, IST_LEVEL, 8, 9, 10, 11);
    952  1.1.2.1  matt #endif
    953  1.1.2.1  matt #if defined(MPC8544) || defined(MPC8548)
    954  1.1.2.1  matt 	mpc85xx_pci_setup("pci1-interrupt-map", 0x001800, IST_LEVEL, 0, 1, 2, 3);
    955  1.1.2.1  matt #endif
    956  1.1.2.1  matt #if defined(MPC8536)
    957  1.1.2.1  matt 	mpc85xx_pci_setup("pci1-interrupt-map", 0x001800, IST_LEVEL, 1, 2, 3, 4);
    958  1.1.2.1  matt #endif
    959  1.1.2.1  matt #if defined(MPC8548)
    960  1.1.2.1  matt 	mpc85xx_pci_setup("pci2-interrupt-map", 0x001800, IST_LEVEL, 11, 1, 2, 3);
    961  1.1.2.1  matt #endif
    962  1.1.2.1  matt }
    963