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