Home | History | Annotate | Line # | Download | only in samsung
exynos_soc.c revision 1.14.4.1
      1  1.14.4.1   martin /*	$NetBSD: exynos_soc.c,v 1.14.4.1 2015/01/04 11:19:00 martin Exp $	*/
      2  1.14.4.1   martin 
      3       1.1     matt /*-
      4       1.1     matt  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5       1.1     matt  * All rights reserved.
      6       1.1     matt  *
      7       1.1     matt  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1     matt  * by Reinoud Zandijk.
      9       1.1     matt  *
     10       1.1     matt  * Redistribution and use in source and binary forms, with or without
     11       1.1     matt  * modification, are permitted provided that the following conditions
     12       1.1     matt  * are met:
     13       1.1     matt  * 1. Redistributions of source code must retain the above copyright
     14       1.1     matt  *    notice, this list of conditions and the following disclaimer.
     15       1.1     matt  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1     matt  *    notice, this list of conditions and the following disclaimer in the
     17       1.1     matt  *    documentation and/or other materials provided with the distribution.
     18       1.1     matt  *
     19       1.1     matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1     matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1     matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1     matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1     matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1     matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1     matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1     matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1     matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1     matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1     matt  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1     matt  */
     31       1.1     matt 
     32       1.1     matt #include "opt_exynos.h"
     33       1.1     matt 
     34       1.1     matt #define	_ARM32_BUS_DMA_PRIVATE
     35       1.1     matt 
     36       1.1     matt #include <sys/cdefs.h>
     37  1.14.4.1   martin __KERNEL_RCSID(1, "$NetBSD: exynos_soc.c,v 1.14.4.1 2015/01/04 11:19:00 martin Exp $");
     38       1.1     matt 
     39       1.1     matt #include <sys/param.h>
     40       1.1     matt #include <sys/bus.h>
     41       1.1     matt #include <sys/cpu.h>
     42       1.1     matt #include <sys/device.h>
     43       1.1     matt 
     44       1.1     matt #include <prop/proplib.h>
     45       1.1     matt 
     46       1.1     matt #include <net/if.h>
     47       1.1     matt #include <net/if_ether.h>
     48       1.1     matt 
     49       1.1     matt #include <arm/locore.h>
     50       1.1     matt 
     51       1.1     matt #include <arm/mainbus/mainbus.h>
     52       1.1     matt #include <arm/cortex/mpcore_var.h>
     53       1.1     matt 
     54       1.1     matt #include <arm/samsung/exynos_reg.h>
     55       1.1     matt #include <arm/samsung/exynos_var.h>
     56      1.14     matt #include <arm/samsung/mct_reg.h>
     57       1.1     matt #include <arm/samsung/smc.h>
     58       1.1     matt 
     59       1.1     matt #include <arm/cortex/pl310_var.h>
     60       1.1     matt #include <arm/cortex/pl310_reg.h>
     61       1.1     matt 
     62       1.1     matt /* XXXNH */
     63       1.1     matt #include <evbarm/odroid/platform.h>
     64       1.1     matt 
     65       1.1     matt 
     66       1.1     matt /* these variables are retrieved in start.S and stored in .data */
     67       1.1     matt uint32_t  exynos_soc_id = 0;
     68       1.1     matt uint32_t  exynos_pop_id = 0;
     69       1.1     matt 
     70  1.14.4.1   martin /* cpu frequencies */
     71  1.14.4.1   martin struct cpu_freq {
     72  1.14.4.1   martin 	uint64_t freq;
     73  1.14.4.1   martin 	int	 P;
     74  1.14.4.1   martin 	int	 M;
     75  1.14.4.1   martin 	int	 S;
     76  1.14.4.1   martin };
     77  1.14.4.1   martin 
     78  1.14.4.1   martin 
     79  1.14.4.1   martin #ifdef EXYNOS4
     80  1.14.4.1   martin const struct cpu_freq cpu_freq_settings_exynos4[] = {
     81  1.14.4.1   martin 	{ 200, 3, 100, 2},
     82  1.14.4.1   martin 	{ 300, 4, 200, 2},
     83  1.14.4.1   martin 	{ 400, 3, 100, 1},
     84  1.14.4.1   martin 	{ 500, 3, 125, 1},
     85  1.14.4.1   martin 	{ 600, 4, 200, 1},
     86  1.14.4.1   martin 	{ 700, 3, 175, 1},
     87  1.14.4.1   martin 	{ 800, 3, 100, 0},
     88  1.14.4.1   martin 	{ 900, 4, 150, 0},
     89  1.14.4.1   martin 	{1000, 3, 125, 0},
     90  1.14.4.1   martin 	{1100, 6, 275, 0},
     91  1.14.4.1   martin 	{1200, 4, 200, 0},
     92  1.14.4.1   martin 	{1300, 6, 325, 0},
     93  1.14.4.1   martin 	{1400, 3, 175, 0},
     94  1.14.4.1   martin 	{1600, 3, 200, 0},
     95  1.14.4.1   martin //	{1704, 3, 213, 0},
     96  1.14.4.1   martin //	{1800, 4, 300, 0},
     97  1.14.4.1   martin //	{1920, 3, 240, 0},
     98  1.14.4.1   martin //	{2000, 3, 250, 0},
     99  1.14.4.1   martin };
    100  1.14.4.1   martin #endif
    101  1.14.4.1   martin 
    102  1.14.4.1   martin 
    103  1.14.4.1   martin #ifdef EXYNOS5
    104  1.14.4.1   martin #define EXYNOS5_DEFAULT_ENTRY 7
    105  1.14.4.1   martin const struct cpu_freq cpu_freq_settings_exynos5[] = {
    106  1.14.4.1   martin 	{ 200,  3, 100, 2},
    107  1.14.4.1   martin 	{ 333,  4, 222, 2},
    108  1.14.4.1   martin 	{ 400,  3, 100, 1},
    109  1.14.4.1   martin 	{ 533, 12, 533, 1},
    110  1.14.4.1   martin 	{ 600,  4, 200, 1},
    111  1.14.4.1   martin 	{ 667,  7, 389, 1},
    112  1.14.4.1   martin 	{ 800,  3, 100, 0},
    113  1.14.4.1   martin 	{ 900,  4, 150, 0},
    114  1.14.4.1   martin 	{1000,  3, 125, 0},
    115  1.14.4.1   martin 	{1066, 12, 533, 0},
    116  1.14.4.1   martin 	{1200,  3, 150, 0},
    117  1.14.4.1   martin 	{1400,  3, 175, 0},
    118  1.14.4.1   martin 	{1600,  3, 200, 0},
    119  1.14.4.1   martin };
    120  1.14.4.1   martin #endif
    121  1.14.4.1   martin 
    122  1.14.4.1   martin static struct cpu_freq const *cpu_freq_settings = NULL;
    123  1.14.4.1   martin static int ncpu_freq_settings = 0;
    124  1.14.4.1   martin 
    125  1.14.4.1   martin static int cpu_freq_target = 0;
    126  1.14.4.1   martin #define NFRQS 18
    127  1.14.4.1   martin static char sysctl_cpu_freqs_txt[NFRQS*5];
    128  1.14.4.1   martin 
    129  1.14.4.1   martin bus_space_handle_t exynos_core_bsh;
    130  1.14.4.1   martin bus_space_handle_t exynos_audiocore_bsh;
    131  1.14.4.1   martin 
    132  1.14.4.1   martin bus_space_handle_t exynos_wdt_bsh;
    133  1.14.4.1   martin bus_space_handle_t exynos_pmu_bsh;
    134  1.14.4.1   martin bus_space_handle_t exynos_cmu_bsh;
    135  1.14.4.1   martin bus_space_handle_t exynos_cmu_apll_bsh;
    136  1.14.4.1   martin bus_space_handle_t exynos_sysreg_bsh;
    137  1.14.4.1   martin 
    138  1.14.4.1   martin 
    139  1.14.4.1   martin static int sysctl_cpufreq_target(SYSCTLFN_ARGS);
    140  1.14.4.1   martin static int sysctl_cpufreq_current(SYSCTLFN_ARGS);
    141       1.1     matt 
    142       1.1     matt /*
    143       1.1     matt  * the early serial console
    144       1.1     matt  */
    145       1.1     matt #ifdef EXYNOS_CONSOLE_EARLY
    146       1.1     matt 
    147       1.1     matt #include "opt_sscom.h"
    148       1.1     matt #include <arm/samsung/sscom_reg.h>
    149       1.1     matt #include <arm/samsung/sscom_var.h>
    150       1.1     matt #include <dev/cons.h>
    151       1.1     matt 
    152       1.1     matt static volatile uint8_t *uart_base;
    153       1.1     matt 
    154       1.1     matt #define CON_REG(a) (*((volatile uint32_t *)(uart_base + (a))))
    155       1.1     matt 
    156       1.1     matt static int
    157       1.1     matt exynos_cngetc(dev_t dv)
    158       1.1     matt {
    159       1.1     matt         if ((CON_REG(SSCOM_UTRSTAT) & UTRSTAT_RXREADY) == 0)
    160       1.1     matt 		return -1;
    161       1.1     matt 
    162       1.1     matt 	return CON_REG(SSCOM_URXH);
    163       1.1     matt }
    164       1.1     matt 
    165       1.1     matt static void
    166       1.1     matt exynos_cnputc(dev_t dv, int c)
    167       1.1     matt {
    168       1.1     matt 	int timo = 150000;
    169       1.1     matt 
    170       1.1     matt 	while ((CON_REG(SSCOM_UFSTAT) & UFSTAT_TXFULL) && --timo > 0);
    171       1.1     matt 
    172       1.1     matt 	CON_REG(SSCOM_UTXH) = c & 0xff;
    173       1.1     matt }
    174       1.1     matt 
    175       1.1     matt static struct consdev exynos_earlycons = {
    176       1.1     matt 	.cn_putc = exynos_cnputc,
    177       1.1     matt 	.cn_getc = exynos_cngetc,
    178       1.1     matt 	.cn_pollc = nullcnpollc,
    179       1.1     matt };
    180       1.1     matt #endif /* EXYNOS_CONSOLE_EARLY */
    181       1.1     matt 
    182       1.1     matt 
    183       1.1     matt #ifdef ARM_TRUSTZONE_FIRMWARE
    184       1.2  reinoud int
    185       1.1     matt exynos_do_idle(void)
    186       1.1     matt {
    187       1.1     matt         exynos_smc(SMC_CMD_SLEEP, 0, 0, 0);
    188       1.1     matt 
    189       1.1     matt 	return 0;
    190       1.1     matt }
    191       1.1     matt 
    192       1.1     matt 
    193       1.2  reinoud int
    194       1.1     matt exynos_set_cpu_boot_addr(int cpu, vaddr_t boot_addr)
    195       1.1     matt {
    196       1.2  reinoud 	/* XXX we need to map in iRAM space for this XXX */
    197       1.1     matt 	return 0;
    198       1.1     matt }
    199       1.1     matt 
    200       1.1     matt 
    201       1.2  reinoud int
    202       1.1     matt exynos_cpu_boot(int cpu)
    203       1.1     matt {
    204       1.1     matt 	exynos_smc(SMC_CMD_CPU1BOOT, cpu, 0, 0);
    205       1.1     matt 
    206       1.1     matt 	return 0;
    207       1.1     matt }
    208       1.1     matt 
    209       1.1     matt 
    210  1.14.4.1   martin #ifdef EXYNOS4
    211       1.1     matt /*
    212  1.14.4.1   martin  * The latency values used below are `magic' and probably chosen empirically.
    213       1.1     matt  * For the 4210 variant the data latency is lower, a 0x110. This is currently
    214       1.1     matt  * not enforced.
    215       1.1     matt  *
    216       1.1     matt  * The prefetch values are also different for the revision 0 of the
    217       1.1     matt  * Exynos4412, but why?
    218       1.1     matt  */
    219       1.1     matt 
    220       1.2  reinoud int
    221  1.14.4.1   martin exynos4_l2cc_init(void)
    222       1.1     matt {
    223       1.1     matt 	const uint32_t tag_latency  = 0x110;
    224       1.2  reinoud 	const uint32_t data_latency = IS_EXYNOS4410_P() ? 0x110 : 0x120;
    225       1.1     matt 	const uint32_t prefetch4412   = /* 0111 0001 0000 0000 0000 0000 0000 0111 */
    226       1.1     matt 				PREFETCHCTL_DBLLINEF_EN  |
    227       1.1     matt 				PREFETCHCTL_INSTRPREF_EN |
    228       1.1     matt 				PREFETCHCTL_DATAPREF_EN  |
    229       1.1     matt 				PREFETCHCTL_PREF_DROP_EN |
    230       1.1     matt 				PREFETCHCTL_PREFETCH_OFFSET_7;
    231       1.1     matt 	const uint32_t prefetch4412_r0 = /* 0011 0000 0000 0000 0000 0000 0000 0111 */
    232       1.1     matt 				PREFETCHCTL_INSTRPREF_EN |
    233       1.1     matt 				PREFETCHCTL_DATAPREF_EN  |
    234       1.1     matt 				PREFETCHCTL_PREFETCH_OFFSET_7;
    235       1.1     matt 	const uint32_t aux_val      =    /* 0111 1100 0100 0111 0000 0000 0000 0001 */
    236       1.1     matt 				AUXCTL_EARLY_BRESP_EN |
    237       1.1     matt 				AUXCTL_I_PREFETCH     |
    238       1.1     matt 				AUXCTL_D_PREFETCH     |
    239       1.1     matt 				AUXCTL_NS_INT_ACC_CTL |
    240       1.1     matt 				AUXCTL_NS_INT_LOCK_EN |
    241       1.1     matt 				AUXCTL_SHARED_ATT_OVR |
    242       1.1     matt 				AUXCTL_WAY_SIZE_RSVD7 << 16 | /* why rsvd7 ??? */
    243       1.1     matt 				AUXCTL_FULL_LINE_WR0;
    244       1.1     matt 	const uint32_t aux_keepmask =    /* 1100 0010 0000 0000 1111 1111 1111 1111  */
    245       1.1     matt 				AUXCTL_RSVD31         |
    246       1.1     matt 				AUXCTL_EARLY_BRESP_EN |
    247       1.1     matt 				AUXCTL_CACHE_REPL_RR  |
    248       1.1     matt 
    249       1.1     matt 				AUXCTL_SH_ATTR_INV_ENA|
    250       1.1     matt 				AUXCTL_EXCL_CACHE_CFG |
    251       1.1     matt 				AUXCTL_ST_BUF_DEV_LIM_EN |
    252       1.1     matt 				AUXCTL_HIPRO_SO_DEV_EN |
    253       1.1     matt 				AUXCTL_FULL_LINE_WR0  |
    254       1.1     matt 				0xffff;
    255       1.1     matt 	uint32_t prefetch;
    256       1.1     matt 
    257       1.1     matt 	/* check the bitmaps are the same as the linux implementation uses */
    258       1.1     matt 	KASSERT(prefetch4412    == 0x71000007);
    259       1.1     matt 	KASSERT(prefetch4412_r0 == 0x30000007);
    260       1.1     matt 	KASSERT(aux_val         == 0x7C470001);
    261       1.1     matt 	KASSERT(aux_keepmask    == 0xC200FFFF);
    262       1.1     matt 
    263       1.2  reinoud 	if (IS_EXYNOS4412_R0_P())
    264       1.1     matt 		prefetch = prefetch4412_r0;
    265       1.1     matt 	else
    266       1.1     matt 		prefetch = prefetch4412;	/* newer than >= r1_0 */
    267       1.1     matt 	;
    268       1.1     matt 
    269       1.1     matt 	exynos_smc(SMC_CMD_L2X0SETUP1, tag_latency, data_latency, prefetch);
    270       1.1     matt 	exynos_smc(SMC_CMD_L2X0SETUP2,
    271       1.1     matt 		POWERCTL_DYNCLKGATE | POWERCTL_STANDBY,
    272       1.1     matt 		aux_val, aux_keepmask);
    273       1.1     matt 	exynos_smc(SMC_CMD_L2X0INVALL, 0, 0, 0);
    274       1.1     matt 	exynos_smc(SMC_CMD_L2X0CTRL, 1, 0, 0);
    275       1.1     matt 
    276       1.1     matt 	return 0;
    277       1.1     matt }
    278  1.14.4.1   martin #endif
    279       1.2  reinoud #endif /* ARM_TRUSTZONE_FIRMWARE */
    280       1.1     matt 
    281       1.1     matt 
    282       1.1     matt void
    283  1.14.4.1   martin exynos_sysctl_cpufreq_init(void)
    284       1.1     matt {
    285  1.14.4.1   martin 	const struct sysctlnode *node, *cpunode, *freqnode;
    286  1.14.4.1   martin 	char *cpos;
    287  1.14.4.1   martin 	int i, val;
    288       1.5  reinoud 	int error;
    289      1.11  reinoud 
    290  1.14.4.1   martin 	memset(sysctl_cpu_freqs_txt, (int) ' ', sizeof(sysctl_cpu_freqs_txt));
    291  1.14.4.1   martin 	cpos = sysctl_cpu_freqs_txt;
    292  1.14.4.1   martin 	for (i = 0; i < ncpu_freq_settings; i++) {
    293  1.14.4.1   martin 		val = cpu_freq_settings[i].freq;
    294  1.14.4.1   martin 		snprintf(cpos, 6, "%d ", val);
    295  1.14.4.1   martin 		cpos += (val < 1000) ? 4 : 5;
    296  1.14.4.1   martin 	}
    297  1.14.4.1   martin 	*cpos = 0;
    298  1.14.4.1   martin 
    299  1.14.4.1   martin 	error = sysctl_createv(NULL, 0, NULL, &node,
    300  1.14.4.1   martin 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL,
    301  1.14.4.1   martin 	    NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL);
    302  1.14.4.1   martin 	if (error)
    303  1.14.4.1   martin 		printf("couldn't create `machdep' node\n");
    304  1.14.4.1   martin 
    305  1.14.4.1   martin 	error = sysctl_createv(NULL, 0, &node, &cpunode,
    306  1.14.4.1   martin 	    0, CTLTYPE_NODE, "cpu", NULL,
    307  1.14.4.1   martin 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
    308  1.14.4.1   martin 	if (error)
    309  1.14.4.1   martin 		printf("couldn't create `cpu' node\n");
    310  1.14.4.1   martin 
    311  1.14.4.1   martin 	error = sysctl_createv(NULL, 0, &cpunode, &freqnode,
    312  1.14.4.1   martin 	    0, CTLTYPE_NODE, "frequency", NULL,
    313  1.14.4.1   martin 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
    314  1.14.4.1   martin 	if (error)
    315  1.14.4.1   martin 		printf("couldn't create `frequency' node\n");
    316  1.14.4.1   martin 
    317  1.14.4.1   martin 	error = sysctl_createv(NULL, 0, &freqnode, &node,
    318  1.14.4.1   martin 	    CTLFLAG_READWRITE, CTLTYPE_INT, "target", NULL,
    319  1.14.4.1   martin 	    sysctl_cpufreq_target, 0, &cpu_freq_target, 0,
    320  1.14.4.1   martin 	    CTL_CREATE, CTL_EOL);
    321  1.14.4.1   martin 	if (error)
    322  1.14.4.1   martin 		printf("couldn't create `target' node\n");
    323  1.14.4.1   martin 
    324  1.14.4.1   martin 	error = sysctl_createv(NULL, 0, &freqnode, &node,
    325  1.14.4.1   martin 	    0, CTLTYPE_INT, "current", NULL,
    326  1.14.4.1   martin 	    sysctl_cpufreq_current, 0, NULL, 0,
    327  1.14.4.1   martin 	    CTL_CREATE, CTL_EOL);
    328  1.14.4.1   martin 	if (error)
    329  1.14.4.1   martin 		printf("couldn't create `current' node\n");
    330  1.14.4.1   martin 
    331  1.14.4.1   martin 	error = sysctl_createv(NULL, 0, &freqnode, &node,
    332  1.14.4.1   martin 	    CTLFLAG_READONLY, CTLTYPE_STRING, "available", NULL,
    333  1.14.4.1   martin 	    NULL, 0, sysctl_cpu_freqs_txt, 0,
    334  1.14.4.1   martin 	    CTL_CREATE, CTL_EOL);
    335  1.14.4.1   martin 	if (error)
    336  1.14.4.1   martin 		printf("couldn't create `available' node\b");
    337  1.14.4.1   martin }
    338  1.14.4.1   martin 
    339  1.14.4.1   martin 
    340  1.14.4.1   martin uint64_t
    341  1.14.4.1   martin exynos_get_cpufreq(void)
    342  1.14.4.1   martin {
    343  1.14.4.1   martin 	uint32_t regval;
    344  1.14.4.1   martin 	uint32_t freq;
    345  1.14.4.1   martin 
    346  1.14.4.1   martin 	regval = bus_space_read_4(&exynos_bs_tag, exynos_cmu_apll_bsh,
    347  1.14.4.1   martin 			PLL_CON0_OFFSET);
    348  1.14.4.1   martin 	freq   = PLL_FREQ(EXYNOS_F_IN_FREQ, regval);
    349  1.14.4.1   martin 
    350  1.14.4.1   martin 	return freq;
    351  1.14.4.1   martin }
    352  1.14.4.1   martin 
    353  1.14.4.1   martin 
    354  1.14.4.1   martin static void
    355  1.14.4.1   martin exynos_set_cpufreq(const struct cpu_freq *freqreq)
    356  1.14.4.1   martin {
    357  1.14.4.1   martin 	struct cpu_info *ci;
    358  1.14.4.1   martin 	uint32_t regval;
    359  1.14.4.1   martin 	int M, P, S;
    360  1.14.4.1   martin 	int cii;
    361  1.14.4.1   martin 
    362  1.14.4.1   martin 	M = freqreq->M;
    363  1.14.4.1   martin 	P = freqreq->P;
    364  1.14.4.1   martin 	S = freqreq->S;
    365  1.14.4.1   martin 
    366  1.14.4.1   martin 	regval = __SHIFTIN(M, PLL_CON0_M) |
    367  1.14.4.1   martin 		 __SHIFTIN(P, PLL_CON0_P) |
    368  1.14.4.1   martin 		 __SHIFTIN(S, PLL_CON0_S);
    369  1.14.4.1   martin 
    370  1.14.4.1   martin 	/* enable PPL and write config */
    371  1.14.4.1   martin 	regval |= PLL_CON0_ENABLE;
    372  1.14.4.1   martin 	bus_space_write_4(&exynos_bs_tag, exynos_cmu_apll_bsh, PLL_CON0_OFFSET,
    373  1.14.4.1   martin 		regval);
    374  1.14.4.1   martin 
    375  1.14.4.1   martin 	/* update our cycle counter i.e. our CPU frequency for all CPUs */
    376  1.14.4.1   martin 	for (CPU_INFO_FOREACH(cii, ci)) {
    377  1.14.4.1   martin 		ci->ci_data.cpu_cc_freq = exynos_get_cpufreq();
    378      1.11  reinoud 	}
    379  1.14.4.1   martin }
    380  1.14.4.1   martin 
    381  1.14.4.1   martin 
    382  1.14.4.1   martin static int
    383  1.14.4.1   martin sysctl_cpufreq_target(SYSCTLFN_ARGS)
    384  1.14.4.1   martin {
    385  1.14.4.1   martin 	struct sysctlnode node;
    386  1.14.4.1   martin 	uint32_t t, curfreq, minfreq, maxfreq;
    387  1.14.4.1   martin 	int i, best_i, diff;
    388  1.14.4.1   martin 	int error;
    389  1.14.4.1   martin 
    390  1.14.4.1   martin 	curfreq = exynos_get_cpufreq() / (1000*1000);
    391  1.14.4.1   martin 	t = *(int *)rnode->sysctl_data;
    392  1.14.4.1   martin 	if (t == 0)
    393  1.14.4.1   martin 		t = curfreq;
    394  1.14.4.1   martin 
    395  1.14.4.1   martin 	node = *rnode;
    396  1.14.4.1   martin 	node.sysctl_data = &t;
    397  1.14.4.1   martin 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    398  1.14.4.1   martin 	if (error || newp == NULL)
    399  1.14.4.1   martin 		return error;
    400  1.14.4.1   martin 
    401  1.14.4.1   martin 	minfreq = cpu_freq_settings[0].freq;
    402  1.14.4.1   martin 	maxfreq = cpu_freq_settings[ncpu_freq_settings-1].freq;
    403  1.14.4.1   martin 
    404  1.14.4.1   martin 	if ((t < minfreq) || (t > maxfreq))
    405  1.14.4.1   martin 		return EINVAL;
    406  1.14.4.1   martin 
    407  1.14.4.1   martin 	if (t == curfreq) {
    408  1.14.4.1   martin 		*(int *)rnode->sysctl_data = t;
    409  1.14.4.1   martin 		return 0;
    410  1.14.4.1   martin 	}
    411  1.14.4.1   martin 
    412  1.14.4.1   martin 	diff = maxfreq;
    413  1.14.4.1   martin 	best_i = -1;
    414  1.14.4.1   martin 	for (i = 0; i < ncpu_freq_settings; i++) {
    415  1.14.4.1   martin 		if (abs(t - cpu_freq_settings[i].freq) <= diff) {
    416  1.14.4.1   martin 			diff = labs(t - cpu_freq_settings[i].freq);
    417  1.14.4.1   martin 			best_i = i;
    418  1.14.4.1   martin 		}
    419  1.14.4.1   martin 	}
    420  1.14.4.1   martin 	if (best_i < 0)
    421  1.14.4.1   martin 		return EINVAL;
    422  1.14.4.1   martin 
    423  1.14.4.1   martin 	exynos_set_cpufreq(&cpu_freq_settings[best_i]);
    424  1.14.4.1   martin 
    425  1.14.4.1   martin 	*(int *)rnode->sysctl_data = t;
    426  1.14.4.1   martin 	return 0;
    427  1.14.4.1   martin }
    428  1.14.4.1   martin 
    429  1.14.4.1   martin 
    430  1.14.4.1   martin static int
    431  1.14.4.1   martin sysctl_cpufreq_current(SYSCTLFN_ARGS)
    432  1.14.4.1   martin {
    433  1.14.4.1   martin 	struct sysctlnode node = *rnode;
    434  1.14.4.1   martin 	uint32_t freq;
    435  1.14.4.1   martin 
    436  1.14.4.1   martin 	freq = exynos_get_cpufreq() / (1000*1000);
    437  1.14.4.1   martin 	node.sysctl_data = &freq;
    438  1.14.4.1   martin 
    439  1.14.4.1   martin 	return sysctl_lookup(SYSCTLFN_CALL(&node));
    440  1.14.4.1   martin }
    441  1.14.4.1   martin 
    442  1.14.4.1   martin 
    443  1.14.4.1   martin #ifdef VERBOSE_INIT_ARM
    444  1.14.4.1   martin #define DUMP_PLL(v, var) \
    445  1.14.4.1   martin 	reg = EXYNOS##v##_CMU_##var + PLL_CON0_OFFSET;\
    446  1.14.4.1   martin 	regval = bus_space_read_4(&exynos_bs_tag, exynos_cmu_bsh, reg); \
    447  1.14.4.1   martin 	freq   = PLL_FREQ(EXYNOS_F_IN_FREQ, regval); \
    448  1.14.4.1   martin 	printf("%8s at %d Mhz\n", #var, freq/(1000*1000));
    449  1.14.4.1   martin 
    450  1.14.4.1   martin 
    451  1.14.4.1   martin static void
    452  1.14.4.1   martin exynos_dump_clocks(void)
    453  1.14.4.1   martin {
    454  1.14.4.1   martin 	uint32_t reg = 0;
    455  1.14.4.1   martin 	uint32_t regval;
    456  1.14.4.1   martin 	uint32_t freq;
    457  1.14.4.1   martin 
    458  1.14.4.1   martin 	printf("Initial PLL settings\n");
    459  1.14.4.1   martin #ifdef EXYNOS4
    460  1.14.4.1   martin 	DUMP_PLL(4, APLL);
    461  1.14.4.1   martin 	DUMP_PLL(4, MPLL);
    462  1.14.4.1   martin 	DUMP_PLL(4, EPLL);
    463  1.14.4.1   martin 	DUMP_PLL(4, VPLL);
    464  1.14.4.1   martin #endif
    465  1.14.4.1   martin #ifdef EXYNOS5
    466  1.14.4.1   martin 	DUMP_PLL(5, APLL);
    467  1.14.4.1   martin 	DUMP_PLL(5, MPLL);
    468  1.14.4.1   martin 	DUMP_PLL(5, KPLL);
    469  1.14.4.1   martin 	DUMP_PLL(5, DPLL);
    470  1.14.4.1   martin 	DUMP_PLL(5, VPLL);
    471  1.14.4.1   martin 	DUMP_PLL(5, CPLL);
    472  1.14.4.1   martin 	DUMP_PLL(5, GPLL);
    473  1.14.4.1   martin 	DUMP_PLL(5, BPLL);
    474  1.14.4.1   martin #endif
    475  1.14.4.1   martin }
    476  1.14.4.1   martin #undef DUMP_PLL
    477      1.11  reinoud #endif
    478      1.11  reinoud 
    479  1.14.4.1   martin 
    480  1.14.4.1   martin /* XXX clock stuff needs major work XXX */
    481  1.14.4.1   martin 
    482  1.14.4.1   martin void
    483  1.14.4.1   martin exynos_clocks_bootstrap(void)
    484  1.14.4.1   martin {
    485  1.14.4.1   martin 	KASSERT(ncpu_freq_settings != 0);
    486  1.14.4.1   martin 	KASSERT(ncpu_freq_settings < NFRQS);
    487  1.14.4.1   martin 	int fsel;
    488  1.14.4.1   martin 
    489  1.14.4.1   martin #ifdef VERBOSE_INIT_ARM
    490  1.14.4.1   martin 	exynos_dump_clocks();
    491  1.14.4.1   martin #endif
    492  1.14.4.1   martin 
    493  1.14.4.1   martin 	/* set (max) cpufreq */
    494  1.14.4.1   martin 	fsel = ncpu_freq_settings-1;
    495  1.14.4.1   martin 
    496      1.11  reinoud #ifdef EXYNOS5
    497  1.14.4.1   martin 	/* XXX BUGFIX selecting freq on E5 goes wrong for now XXX */
    498  1.14.4.1   martin 	fsel = EXYNOS5_DEFAULT_ENTRY;
    499      1.11  reinoud #endif
    500       1.1     matt 
    501  1.14.4.1   martin 	exynos_set_cpufreq(&cpu_freq_settings[fsel]);
    502  1.14.4.1   martin 
    503  1.14.4.1   martin 	/* set external USB frequency to XCLKOUT */
    504  1.14.4.1   martin 	exynos_init_clkout_for_usb();
    505  1.14.4.1   martin }
    506  1.14.4.1   martin 
    507  1.14.4.1   martin 
    508  1.14.4.1   martin void
    509  1.14.4.1   martin exynos_bootstrap(vaddr_t iobase, vaddr_t uartbase)
    510  1.14.4.1   martin {
    511  1.14.4.1   martin 	int error;
    512  1.14.4.1   martin 	size_t core_size, audiocore_size;
    513  1.14.4.1   martin 	bus_addr_t audiocore_pbase;
    514  1.14.4.1   martin 	bus_addr_t audiocore_vbase __diagused;
    515  1.14.4.1   martin 	bus_addr_t exynos_wdt_offset;
    516  1.14.4.1   martin 	bus_addr_t exynos_pmu_offset;
    517  1.14.4.1   martin 	bus_addr_t exynos_sysreg_offset;
    518  1.14.4.1   martin 	bus_addr_t exynos_cmu_apll_offset;
    519  1.14.4.1   martin 
    520       1.1     matt 	/* set up early console so we can use printf() and friends */
    521       1.1     matt #ifdef EXYNOS_CONSOLE_EARLY
    522       1.1     matt 	uart_base = (volatile uint8_t *) uartbase;
    523       1.1     matt 	cn_tab = &exynos_earlycons;
    524       1.1     matt 	printf("Exynos early console operational\n\n");
    525       1.1     matt #endif
    526  1.14.4.1   martin 
    527  1.14.4.1   martin #ifdef EXYNOS4
    528  1.14.4.1   martin 	core_size = EXYNOS4_CORE_SIZE;
    529  1.14.4.1   martin 	audiocore_size = EXYNOS4_AUDIOCORE_SIZE;
    530  1.14.4.1   martin 	audiocore_pbase = EXYNOS4_AUDIOCORE_PBASE;
    531  1.14.4.1   martin 	audiocore_vbase = EXYNOS4_AUDIOCORE_VBASE;
    532  1.14.4.1   martin 	exynos_wdt_offset = EXYNOS4_WDT_OFFSET;
    533  1.14.4.1   martin 	exynos_pmu_offset = EXYNOS4_PMU_OFFSET;
    534  1.14.4.1   martin 	exynos_sysreg_offset = EXYNOS4_SYSREG_OFFSET;
    535  1.14.4.1   martin 	exynos_cmu_apll_offset = EXYNOS4_CMU_APLL;
    536  1.14.4.1   martin 
    537  1.14.4.1   martin 	cpu_freq_settings = cpu_freq_settings_exynos4;
    538  1.14.4.1   martin 	ncpu_freq_settings = __arraycount(cpu_freq_settings_exynos4);
    539  1.14.4.1   martin #endif
    540  1.14.4.1   martin 
    541  1.14.4.1   martin #ifdef EXYNOS5
    542  1.14.4.1   martin 	core_size = EXYNOS5_CORE_SIZE;
    543  1.14.4.1   martin 	audiocore_size = EXYNOS5_AUDIOCORE_SIZE;
    544  1.14.4.1   martin 	audiocore_pbase = EXYNOS5_AUDIOCORE_PBASE;
    545  1.14.4.1   martin 	audiocore_vbase = EXYNOS5_AUDIOCORE_VBASE;
    546  1.14.4.1   martin 	exynos_wdt_offset = EXYNOS5_WDT_OFFSET;
    547  1.14.4.1   martin 	exynos_pmu_offset = EXYNOS5_PMU_OFFSET;
    548  1.14.4.1   martin 	exynos_sysreg_offset = EXYNOS5_SYSREG_OFFSET;
    549  1.14.4.1   martin 	exynos_cmu_apll_offset = EXYNOS5_CMU_APLL;
    550  1.14.4.1   martin 
    551  1.14.4.1   martin 	cpu_freq_settings = cpu_freq_settings_exynos5;
    552  1.14.4.1   martin 	ncpu_freq_settings = __arraycount(cpu_freq_settings_exynos5);
    553  1.14.4.1   martin #endif
    554  1.14.4.1   martin 
    555       1.1     matt 	/* map in the exynos io registers */
    556       1.1     matt 	error = bus_space_map(&exynos_bs_tag, EXYNOS_CORE_PBASE,
    557       1.5  reinoud 		core_size, 0, &exynos_core_bsh);
    558       1.1     matt 	if (error)
    559      1.11  reinoud 		panic("%s: failed to map in Exynos SFR registers: %d",
    560       1.1     matt 			__func__, error);
    561       1.1     matt 	KASSERT(exynos_core_bsh == iobase);
    562       1.7  reinoud 
    563      1.11  reinoud 	error = bus_space_map(&exynos_bs_tag, audiocore_pbase,
    564      1.11  reinoud 		audiocore_size, 0, &exynos_audiocore_bsh);
    565      1.11  reinoud 	if (error)
    566      1.11  reinoud 		panic("%s: failed to map in Exynos audio SFR registers: %d",
    567      1.11  reinoud 			__func__, error);
    568      1.12  reinoud 	KASSERT(exynos_audiocore_bsh == audiocore_vbase);
    569      1.11  reinoud 
    570  1.14.4.1   martin 	/* map in commonly used subregions and common used register banks */
    571  1.14.4.1   martin 	error = bus_space_subregion(&exynos_bs_tag, exynos_core_bsh,
    572  1.14.4.1   martin 		exynos_wdt_offset, EXYNOS_BLOCK_SIZE, &exynos_wdt_bsh);
    573  1.14.4.1   martin 	if (error)
    574  1.14.4.1   martin 		panic("%s: failed to subregion wdt registers: %d",
    575  1.14.4.1   martin 			__func__, error);
    576  1.14.4.1   martin 
    577  1.14.4.1   martin 	error = bus_space_subregion(&exynos_bs_tag, exynos_core_bsh,
    578  1.14.4.1   martin 		exynos_pmu_offset, EXYNOS_BLOCK_SIZE, &exynos_pmu_bsh);
    579  1.14.4.1   martin 	if (error)
    580  1.14.4.1   martin 		panic("%s: failed to subregion pmu registers: %d",
    581  1.14.4.1   martin 			__func__, error);
    582  1.14.4.1   martin 
    583  1.14.4.1   martin 	exynos_cmu_bsh = exynos_core_bsh;
    584  1.14.4.1   martin 	bus_space_subregion(&exynos_bs_tag, exynos_core_bsh,
    585  1.14.4.1   martin 		exynos_sysreg_offset, EXYNOS_BLOCK_SIZE,
    586  1.14.4.1   martin 		&exynos_sysreg_bsh);
    587  1.14.4.1   martin 	if (error)
    588  1.14.4.1   martin 		panic("%s: failed to subregion sysreg registers: %d",
    589  1.14.4.1   martin 			__func__, error);
    590  1.14.4.1   martin 
    591  1.14.4.1   martin 	error = bus_space_subregion(&exynos_bs_tag, exynos_cmu_bsh,
    592  1.14.4.1   martin 		exynos_cmu_apll_offset, 0xfff, &exynos_cmu_apll_bsh);
    593  1.14.4.1   martin 	if (error)
    594  1.14.4.1   martin 		panic("%s: failed to subregion cmu apll registers: %d",
    595  1.14.4.1   martin 			__func__, error);
    596  1.14.4.1   martin 
    597       1.7  reinoud 	/* init bus dma tags */
    598       1.7  reinoud 	exynos_dma_bootstrap(physmem * PAGE_SIZE);
    599       1.8  reinoud 
    600      1.11  reinoud 	/* gpio bootstrapping delayed */
    601       1.1     matt }
    602       1.1     matt 
    603       1.1     matt 
    604       1.1     matt void
    605       1.1     matt exynos_device_register(device_t self, void *aux)
    606       1.1     matt {
    607       1.1     matt 	if (device_is_a(self, "armperiph")
    608       1.1     matt 	    && device_is_a(device_parent(self), "mainbus")) {
    609       1.1     matt 		/*
    610       1.1     matt 		 * XXX KLUDGE ALERT XXX
    611       1.1     matt 		 * The iot mainbus supplies is completely wrong since it scales
    612  1.14.4.1   martin 		 * addresses by 2.  The simplest remedy is to replace with our
    613  1.14.4.1   martin 		 * bus space used for the armcore registers (which armperiph uses).
    614       1.1     matt 		 */
    615       1.1     matt 		struct mainbus_attach_args * const mb = aux;
    616       1.1     matt 		mb->mb_iot = &exynos_bs_tag;
    617       1.1     matt 		return;
    618       1.1     matt 	}
    619       1.1     matt 	if (device_is_a(self, "armgic")
    620       1.1     matt 	    && device_is_a(device_parent(self), "armperiph")) {
    621       1.1     matt 		/*
    622       1.1     matt 		 * The Exynos4420 armgic is located at a different location!
    623       1.1     matt 		 */
    624       1.1     matt 
    625       1.1     matt 		extern uint32_t exynos_soc_id;
    626       1.6  reinoud 
    627       1.1     matt 		switch (EXYNOS_PRODUCT_ID(exynos_soc_id)) {
    628  1.14.4.1   martin #ifdef EXYNOS5
    629       1.1     matt 		case 0xe5410:
    630       1.6  reinoud 			/* offsets not changed on matt's request */
    631       1.1     matt #if 0
    632       1.6  reinoud 			mpcaa->mpcaa_memh = EXYNOS_CORE_VBASE;
    633       1.1     matt 			mpcaa->mpcaa_off1 = EXYNOS5_GIC_IOP_DISTRIBUTOR_OFFSET;
    634       1.1     matt 			mpcaa->mpcaa_off2 = EXYNOS5_GIC_IOP_CONTROLLER_OFFSET;
    635       1.1     matt #endif
    636       1.1     matt 			break;
    637       1.1     matt #endif
    638  1.14.4.1   martin #ifdef EXYNOS4
    639       1.1     matt 		case 0xe4410:
    640      1.12  reinoud 		case 0xe4412: {
    641      1.12  reinoud 			struct mpcore_attach_args * const mpcaa = aux;
    642      1.12  reinoud 
    643       1.1     matt 			mpcaa->mpcaa_memh = EXYNOS_CORE_VBASE;
    644       1.1     matt 			mpcaa->mpcaa_off1 = EXYNOS4_GIC_DISTRIBUTOR_OFFSET;
    645       1.1     matt 			mpcaa->mpcaa_off2 = EXYNOS4_GIC_CNTR_OFFSET;
    646       1.1     matt 			break;
    647      1.12  reinoud 		      }
    648       1.1     matt #endif
    649       1.1     matt 		default:
    650       1.1     matt 			panic("%s: unknown SoC product id %#x", __func__,
    651       1.1     matt 			    (u_int)EXYNOS_PRODUCT_ID(exynos_soc_id));
    652       1.1     matt 		}
    653       1.1     matt 		return;
    654       1.1     matt 	}
    655      1.10  reinoud 	if (device_is_a(self, "armgtmr") || device_is_a(self, "mct")) {
    656      1.14     matt #ifdef EXYNOS5
    657      1.13     matt 		/*
    658      1.13     matt 		 * The global timer is dependent on the MCT running.
    659      1.13     matt 		 */
    660      1.13     matt 		bus_size_t o = EXYNOS5_MCT_OFFSET + MCT_G_TCON;
    661      1.13     matt 		uint32_t v = bus_space_read_4(&exynos_bs_tag, exynos_core_bsh,
    662      1.14     matt 		     o);
    663      1.13     matt 		v |= G_TCON_START;
    664      1.13     matt 		bus_space_write_4(&exynos_bs_tag, exynos_core_bsh, o, v);
    665      1.13     matt #endif
    666       1.1     matt 		/*
    667      1.10  reinoud 		 * The frequencies of the timers are the reference
    668       1.1     matt 		 * frequency.
    669       1.1     matt 		 */
    670       1.1     matt 		prop_dictionary_set_uint32(device_properties(self),
    671      1.10  reinoud 		    "frequency", EXYNOS_F_IN_FREQ);
    672       1.1     matt 		return;
    673       1.1     matt 	}
    674       1.1     matt 
    675       1.1     matt 	exyo_device_register(self, aux);
    676       1.1     matt }
    677       1.1     matt 
    678       1.9  reinoud 
    679       1.9  reinoud void
    680       1.9  reinoud exynos_device_register_post_config(device_t self, void *aux)
    681       1.9  reinoud {
    682       1.9  reinoud 	exyo_device_register_post_config(self, aux);
    683       1.9  reinoud }
    684       1.9  reinoud 
    685  1.14.4.1   martin 
    686  1.14.4.1   martin /*
    687  1.14.4.1   martin  * USB power SoC dependent handling
    688  1.14.4.1   martin  */
    689  1.14.4.1   martin 
    690  1.14.4.1   martin #ifdef EXYNOS4
    691  1.14.4.1   martin static struct exynos_gpio_pinset e4_uhost_pwr_pinset = {
    692  1.14.4.1   martin 	.pinset_group = "ETC6",
    693  1.14.4.1   martin 	.pinset_func  = 0,
    694  1.14.4.1   martin 	.pinset_mask  = __BIT(6) | __BIT(7),
    695  1.14.4.1   martin };
    696  1.14.4.1   martin #endif
    697  1.14.4.1   martin 
    698  1.14.4.1   martin 
    699  1.14.4.1   martin #ifdef EXYNOS5
    700  1.14.4.1   martin static struct exynos_gpio_pinset e5_uhost_pwr_pinset = {
    701  1.14.4.1   martin 	.pinset_group = "ETC6",
    702  1.14.4.1   martin 	.pinset_func  = 0,
    703  1.14.4.1   martin 	.pinset_mask  = __BIT(5) | __BIT(6),
    704  1.14.4.1   martin };
    705  1.14.4.1   martin static struct exynos_gpio_pinset e5_usb3_bus0_pinset = {
    706  1.14.4.1   martin 	.pinset_group = "GPK3",
    707  1.14.4.1   martin 	.pinset_func  = 2,
    708  1.14.4.1   martin 	.pinset_mask  = __BIT(0) | __BIT(1) | __BIT(3),
    709  1.14.4.1   martin };
    710  1.14.4.1   martin static struct exynos_gpio_pinset e5_usb3_bus1_pinset = {
    711  1.14.4.1   martin 	.pinset_group = "GPK2",
    712  1.14.4.1   martin 	.pinset_func  = 2,
    713  1.14.4.1   martin 	.pinset_mask  = __BIT(4) | __BIT(5) | __BIT(7),
    714  1.14.4.1   martin };
    715  1.14.4.1   martin #endif
    716  1.14.4.1   martin 
    717  1.14.4.1   martin 
    718  1.14.4.1   martin void
    719  1.14.4.1   martin exynos_usb_soc_powerup(void)
    720  1.14.4.1   martin {
    721  1.14.4.1   martin 	struct exynos_gpio_pindata XuhostOVERCUR;
    722  1.14.4.1   martin 	struct exynos_gpio_pindata XuhostPWREN;
    723  1.14.4.1   martin 
    724  1.14.4.1   martin #ifdef EXYNOS4
    725  1.14.4.1   martin 		exynos_gpio_pinset_acquire(&e4_uhost_pwr_pinset);
    726  1.14.4.1   martin 		exynos_gpio_pinset_to_pindata(&e4_uhost_pwr_pinset, 6, &XuhostPWREN);
    727  1.14.4.1   martin 		exynos_gpio_pinset_to_pindata(&e4_uhost_pwr_pinset, 7, &XuhostOVERCUR);
    728  1.14.4.1   martin 
    729  1.14.4.1   martin 		/* enable power and set Xuhost OVERCUR to inactive by pulling it up */
    730  1.14.4.1   martin 		exynos_gpio_pindata_ctl(&XuhostPWREN, GPIO_PIN_PULLUP);
    731  1.14.4.1   martin 		exynos_gpio_pindata_ctl(&XuhostOVERCUR, GPIO_PIN_PULLUP);
    732  1.14.4.1   martin 		DELAY(80000);
    733  1.14.4.1   martin #endif
    734  1.14.4.1   martin #ifdef EXYNOS5
    735  1.14.4.1   martin 	if (IS_EXYNOS5410_P()) {
    736  1.14.4.1   martin 		struct exynos_gpio_pindata Xovercur2, Xovercur3;
    737  1.14.4.1   martin 		struct exynos_gpio_pindata Xvbus;
    738  1.14.4.1   martin 
    739  1.14.4.1   martin 		/* BUS 0 */
    740  1.14.4.1   martin 		exynos_gpio_pinset_acquire(&e5_usb3_bus0_pinset);
    741  1.14.4.1   martin 		exynos_gpio_pinset_to_pindata(&e5_usb3_bus0_pinset, 0, &Xovercur2);
    742  1.14.4.1   martin 		exynos_gpio_pinset_to_pindata(&e5_usb3_bus0_pinset, 1, &Xovercur3);
    743  1.14.4.1   martin 		exynos_gpio_pinset_to_pindata(&e5_usb3_bus0_pinset, 3, &Xvbus);
    744  1.14.4.1   martin 
    745  1.14.4.1   martin 		/* enable power and set overcur inactive by pulling them up */
    746  1.14.4.1   martin 		exynos_gpio_pindata_ctl(&Xvbus, GPIO_PIN_PULLUP);
    747  1.14.4.1   martin 		exynos_gpio_pindata_ctl(&Xovercur2, GPIO_PIN_PULLUP);
    748  1.14.4.1   martin 		exynos_gpio_pindata_ctl(&Xovercur3, GPIO_PIN_PULLUP);
    749  1.14.4.1   martin 
    750  1.14.4.1   martin 		/* BUS 1 */
    751  1.14.4.1   martin 		exynos_gpio_pinset_acquire(&e5_usb3_bus1_pinset);
    752  1.14.4.1   martin 		exynos_gpio_pinset_to_pindata(&e5_usb3_bus1_pinset, 4, &Xovercur2);
    753  1.14.4.1   martin 		exynos_gpio_pinset_to_pindata(&e5_usb3_bus1_pinset, 5, &Xovercur3);
    754  1.14.4.1   martin 		exynos_gpio_pinset_to_pindata(&e5_usb3_bus1_pinset, 7, &Xvbus);
    755  1.14.4.1   martin 
    756  1.14.4.1   martin 		/* enable power and set overcur inactive by pulling them up */
    757  1.14.4.1   martin 		exynos_gpio_pindata_ctl(&Xvbus, GPIO_PIN_PULLUP);
    758  1.14.4.1   martin 		exynos_gpio_pindata_ctl(&Xovercur2, GPIO_PIN_PULLUP);
    759  1.14.4.1   martin 		exynos_gpio_pindata_ctl(&Xovercur3, GPIO_PIN_PULLUP);
    760  1.14.4.1   martin 
    761  1.14.4.1   martin 		/* enable power to the hub */
    762  1.14.4.1   martin 		exynos_gpio_pinset_acquire(&e5_uhost_pwr_pinset);
    763  1.14.4.1   martin 		exynos_gpio_pinset_to_pindata(&e5_uhost_pwr_pinset, 5, &XuhostPWREN);
    764  1.14.4.1   martin 		exynos_gpio_pinset_to_pindata(&e5_uhost_pwr_pinset, 6, &XuhostOVERCUR);
    765  1.14.4.1   martin 
    766  1.14.4.1   martin 		/* enable power and set Xuhost OVERCUR to inactive by pulling it up */
    767  1.14.4.1   martin 		exynos_gpio_pindata_ctl(&XuhostPWREN, GPIO_PIN_PULLUP);
    768  1.14.4.1   martin 		exynos_gpio_pindata_ctl(&XuhostOVERCUR, GPIO_PIN_PULLUP);
    769  1.14.4.1   martin 		DELAY(80000);
    770  1.14.4.1   martin 	}
    771  1.14.4.1   martin 	/* XXX 5422 XXX */
    772  1.14.4.1   martin #endif
    773  1.14.4.1   martin }
    774  1.14.4.1   martin 
    775  1.14.4.1   martin 
    776  1.14.4.1   martin /*
    777  1.14.4.1   martin  * USB Phy SoC dependent handling
    778  1.14.4.1   martin  */
    779  1.14.4.1   martin 
    780  1.14.4.1   martin /* XXX 5422 not handled since its unknown how it handles this XXX*/
    781  1.14.4.1   martin static void
    782  1.14.4.1   martin exynos_usb2_set_isolation(bool on)
    783  1.14.4.1   martin {
    784  1.14.4.1   martin 	uint32_t en_mask, regval;
    785  1.14.4.1   martin 	bus_addr_t reg;
    786  1.14.4.1   martin 
    787  1.14.4.1   martin 	/* enable PHY */
    788  1.14.4.1   martin 	reg = EXYNOS_PMU_USB_PHY_CTRL;
    789  1.14.4.1   martin 
    790  1.14.4.1   martin 	if (IS_EXYNOS5_P() || IS_EXYNOS4410_P()) {
    791  1.14.4.1   martin 		/* set usbhost mode */
    792  1.14.4.1   martin 		regval = on ? 0 : USB20_PHY_HOST_LINK_EN;
    793  1.14.4.1   martin 		bus_space_write_4(&exynos_bs_tag, exynos_sysreg_bsh,
    794  1.14.4.1   martin 			EXYNOS5_SYSREG_USB20_PHY_TYPE, regval);
    795  1.14.4.1   martin 		reg = EXYNOS_PMU_USBHOST_PHY_CTRL;
    796  1.14.4.1   martin 	}
    797  1.14.4.1   martin 
    798  1.14.4.1   martin 	/* do enable PHY */
    799  1.14.4.1   martin 	en_mask = PMU_PHY_ENABLE;
    800  1.14.4.1   martin 	regval = bus_space_read_4(&exynos_bs_tag, exynos_pmu_bsh, reg);
    801  1.14.4.1   martin 	regval = on ? regval & ~en_mask : regval | en_mask;
    802  1.14.4.1   martin 
    803  1.14.4.1   martin 	bus_space_write_4(&exynos_bs_tag, exynos_pmu_bsh,
    804  1.14.4.1   martin 		reg, regval);
    805  1.14.4.1   martin 
    806  1.14.4.1   martin 	if (IS_EXYNOS4X12_P()) {
    807  1.14.4.1   martin 		bus_space_write_4(&exynos_bs_tag, exynos_pmu_bsh,
    808  1.14.4.1   martin 			EXYNOS_PMU_USB_HSIC_1_PHY_CTRL, regval);
    809  1.14.4.1   martin 		bus_space_write_4(&exynos_bs_tag, exynos_pmu_bsh,
    810  1.14.4.1   martin 			EXYNOS_PMU_USB_HSIC_2_PHY_CTRL, regval);
    811  1.14.4.1   martin 	}
    812  1.14.4.1   martin }
    813  1.14.4.1   martin 
    814  1.14.4.1   martin 
    815  1.14.4.1   martin #ifdef EXYNOS4
    816  1.14.4.1   martin static void
    817  1.14.4.1   martin exynos4_usb2phy_enable(bus_space_handle_t usb2phy_bsh)
    818  1.14.4.1   martin {
    819  1.14.4.1   martin 	uint32_t phypwr, rstcon, clkreg;
    820  1.14.4.1   martin 
    821  1.14.4.1   martin 	/* write clock value */
    822  1.14.4.1   martin 	clkreg = FSEL_CLKSEL_24M;
    823  1.14.4.1   martin 	bus_space_write_4(&exynos_bs_tag, usb2phy_bsh,
    824  1.14.4.1   martin 		USB_PHYCLK, clkreg);
    825  1.14.4.1   martin 
    826  1.14.4.1   martin 	/* set device and host to normal */
    827  1.14.4.1   martin 	phypwr = bus_space_read_4(&exynos_bs_tag, usb2phy_bsh,
    828  1.14.4.1   martin 		USB_PHYPWR);
    829  1.14.4.1   martin 
    830  1.14.4.1   martin 	/* enable analog, enable otg, unsleep phy0 (host) */
    831  1.14.4.1   martin 	phypwr &= ~PHYPWR_NORMAL_MASK_PHY0;
    832  1.14.4.1   martin 	bus_space_write_4(&exynos_bs_tag, usb2phy_bsh,
    833  1.14.4.1   martin 		USB_PHYPWR, phypwr);
    834  1.14.4.1   martin 
    835  1.14.4.1   martin 	if (IS_EXYNOS4X12_P()) {
    836  1.14.4.1   martin 		/* enable hsic0 (host), enable hsic1 and phy1 (otg) */
    837  1.14.4.1   martin 		phypwr = bus_space_read_4(&exynos_bs_tag, usb2phy_bsh,
    838  1.14.4.1   martin 			USB_PHYPWR);
    839  1.14.4.1   martin 		phypwr &= ~(PHYPWR_NORMAL_MASK_HSIC0 |
    840  1.14.4.1   martin 			    PHYPWR_NORMAL_MASK_HSIC1 |
    841  1.14.4.1   martin 			    PHYPWR_NORMAL_MASK_PHY1);
    842  1.14.4.1   martin 		bus_space_write_4(&exynos_bs_tag, usb2phy_bsh,
    843  1.14.4.1   martin 			USB_PHYPWR, phypwr);
    844  1.14.4.1   martin 	}
    845  1.14.4.1   martin 
    846  1.14.4.1   martin 	/* reset both phy and link of device */
    847  1.14.4.1   martin 	rstcon = bus_space_read_4(&exynos_bs_tag, usb2phy_bsh,
    848  1.14.4.1   martin 		USB_RSTCON);
    849  1.14.4.1   martin 	rstcon |= RSTCON_DEVPHY_SWRST;
    850  1.14.4.1   martin 	bus_space_write_4(&exynos_bs_tag, usb2phy_bsh,
    851  1.14.4.1   martin 		USB_RSTCON, rstcon);
    852  1.14.4.1   martin 	DELAY(10000);
    853  1.14.4.1   martin 	rstcon &= ~RSTCON_DEVPHY_SWRST;
    854  1.14.4.1   martin 	bus_space_write_4(&exynos_bs_tag, usb2phy_bsh,
    855  1.14.4.1   martin 		USB_RSTCON, rstcon);
    856  1.14.4.1   martin 
    857  1.14.4.1   martin 	if (IS_EXYNOS4X12_P()) {
    858  1.14.4.1   martin 		/* reset both phy and link of host */
    859  1.14.4.1   martin 		rstcon = bus_space_read_4(&exynos_bs_tag, usb2phy_bsh,
    860  1.14.4.1   martin 			USB_RSTCON);
    861  1.14.4.1   martin 		rstcon |= RSTCON_HOSTPHY_SWRST | RSTCON_HOSTPHYLINK_SWRST;
    862  1.14.4.1   martin 		bus_space_write_4(&exynos_bs_tag, usb2phy_bsh,
    863  1.14.4.1   martin 			USB_RSTCON, rstcon);
    864  1.14.4.1   martin 		DELAY(10000);
    865  1.14.4.1   martin 		rstcon &= ~(RSTCON_HOSTPHY_SWRST | RSTCON_HOSTPHYLINK_SWRST);
    866  1.14.4.1   martin 		bus_space_write_4(&exynos_bs_tag, usb2phy_bsh,
    867  1.14.4.1   martin 			USB_RSTCON, rstcon);
    868  1.14.4.1   martin 	}
    869  1.14.4.1   martin 
    870  1.14.4.1   martin 	/* wait for everything to be initialized */
    871  1.14.4.1   martin 	DELAY(80000);
    872  1.14.4.1   martin }
    873  1.14.4.1   martin #endif
    874  1.14.4.1   martin 
    875  1.14.4.1   martin 
    876  1.14.4.1   martin #ifdef EXYNOS5
    877  1.14.4.1   martin static void
    878  1.14.4.1   martin exynos5410_usb2phy_enable(bus_space_handle_t usb2phy_bsh)
    879  1.14.4.1   martin {
    880  1.14.4.1   martin 	uint32_t phyhost; //, phyotg;
    881  1.14.4.1   martin 	uint32_t phyhsic;
    882  1.14.4.1   martin 	uint32_t ehcictrl, ohcictrl;
    883  1.14.4.1   martin 
    884  1.14.4.1   martin 	/* host configuration: */
    885  1.14.4.1   martin 	phyhost = bus_space_read_4(&exynos_bs_tag, usb2phy_bsh,
    886  1.14.4.1   martin 	    USB_PHY_HOST_CTRL0);
    887  1.14.4.1   martin 
    888  1.14.4.1   martin 	/* host phy reference clock; assumption its 24 MHz now */
    889  1.14.4.1   martin 	phyhost &= ~HOST_CTRL0_FSEL_MASK;
    890  1.14.4.1   martin 	phyhost |= __SHIFTIN(FSEL_CLKSEL_24M, HOST_CTRL0_FSEL_MASK);
    891  1.14.4.1   martin 
    892  1.14.4.1   martin 	/* enable normal mode of operation */
    893  1.14.4.1   martin 	phyhost &= ~(HOST_CTRL0_FORCESUSPEND | HOST_CTRL0_FORCESLEEP);
    894  1.14.4.1   martin 
    895  1.14.4.1   martin 	/* host phy reset */
    896  1.14.4.1   martin 	phyhost &= ~(HOST_CTRL0_PHY_SWRST | HOST_CTRL0_PHY_SWRST_ALL |
    897  1.14.4.1   martin 	    HOST_CTRL0_SIDDQ | HOST_CTRL0_FORCESUSPEND |
    898  1.14.4.1   martin 	    HOST_CTRL0_FORCESLEEP);
    899  1.14.4.1   martin 
    900  1.14.4.1   martin 	/* host link reset */
    901  1.14.4.1   martin 	phyhost |= HOST_CTRL0_LINK_SWRST | HOST_CTRL0_UTMI_SWRST |
    902  1.14.4.1   martin 	    HOST_CTRL0_COMMONON_N;
    903  1.14.4.1   martin 	/* do the reset */
    904  1.14.4.1   martin 	bus_space_write_4(&exynos_bs_tag, usb2phy_bsh, USB_PHY_HOST_CTRL0,
    905  1.14.4.1   martin 	    phyhost);
    906  1.14.4.1   martin 	DELAY(10000);
    907  1.14.4.1   martin 
    908  1.14.4.1   martin 	phyhost &= ~(HOST_CTRL0_LINK_SWRST | HOST_CTRL0_UTMI_SWRST);
    909  1.14.4.1   martin 	bus_space_write_4(&exynos_bs_tag, usb2phy_bsh, USB_PHY_HOST_CTRL0,
    910  1.14.4.1   martin 	   phyhost);
    911  1.14.4.1   martin 
    912  1.14.4.1   martin 	/* HSIC control */
    913  1.14.4.1   martin 	phyhsic =
    914  1.14.4.1   martin 	    __SHIFTIN(HSIC_CTRL_REFCLKDIV_12, HSIC_CTRL_REFCLKDIV_MASK) |
    915  1.14.4.1   martin 	    __SHIFTIN(HSIC_CTRL_REFCLKSEL_DEFAULT, HSIC_CTRL_REFCLKSEL_MASK) |
    916  1.14.4.1   martin 	    HSIC_CTRL_PHY_SWRST;
    917  1.14.4.1   martin 
    918  1.14.4.1   martin 	bus_space_write_4(&exynos_bs_tag, usb2phy_bsh, USB_PHY_HSIC_CTRL1,
    919  1.14.4.1   martin 	   phyhsic);
    920  1.14.4.1   martin 	bus_space_write_4(&exynos_bs_tag, usb2phy_bsh, USB_PHY_HSIC_CTRL2,
    921  1.14.4.1   martin 	   phyhsic);
    922  1.14.4.1   martin 	DELAY(10);
    923  1.14.4.1   martin 
    924  1.14.4.1   martin 	phyhsic &= ~HSIC_CTRL_PHY_SWRST;
    925  1.14.4.1   martin 	bus_space_write_4(&exynos_bs_tag, usb2phy_bsh, USB_PHY_HSIC_CTRL1,
    926  1.14.4.1   martin 	   phyhsic);
    927  1.14.4.1   martin 	bus_space_write_4(&exynos_bs_tag, usb2phy_bsh, USB_PHY_HSIC_CTRL2,
    928  1.14.4.1   martin 	   phyhsic);
    929  1.14.4.1   martin 	DELAY(80);
    930  1.14.4.1   martin 
    931  1.14.4.1   martin #if 0
    932  1.14.4.1   martin 	/* otg configuration: */
    933  1.14.4.1   martin 	phyotg = bus_space_read_4(&exynos_bs_tag, usb2phy_bsh,
    934  1.14.4.1   martin 		USB_PHY_OTG_SYS);
    935  1.14.4.1   martin 
    936  1.14.4.1   martin 	/* otg phy refrence clock: assumption its 24 Mhz now */
    937  1.14.4.1   martin 	phyotg &= ~OTG_SYS_FSEL_MASK;
    938  1.14.4.1   martin 	phyotg |= __SHIFTIN(OTG_SYS_FSEL_MASK, FSEL_CLKSEL_24M);
    939  1.14.4.1   martin 
    940  1.14.4.1   martin 	/* enable normal mode of operation */
    941  1.14.4.1   martin 	phyotg &= ~(OTG_SYS_FORCESUSPEND | OTG_SYS_FORCESLEEP |
    942  1.14.4.1   martin 		OTG_SYS_SIDDQ_UOTG | OTG_SYS_REFCLKSEL_MASK |
    943  1.14.4.1   martin 		OTG_SYS_COMMON_ON);
    944  1.14.4.1   martin 
    945  1.14.4.1   martin 	/* OTG phy and link reset */
    946  1.14.4.1   martin 	phyotg |= OTG_SYS_PHY0_SWRST | OTG_SYS_PHYLINK_SWRST |
    947  1.14.4.1   martin 		OTG_SYS_OTGDISABLE | OTG_SYS_REFCLKSEL_MASK;
    948  1.14.4.1   martin 
    949  1.14.4.1   martin 	/* do the reset */
    950  1.14.4.1   martin 	bus_space_write_4(&exynos_bs_tag, usb2phy_bsh,
    951  1.14.4.1   martin 		USB_PHY_OTG_SYS, phyotg);
    952  1.14.4.1   martin 	DELAY(10000);
    953  1.14.4.1   martin 	phyotg &= ~(OTG_SYS_PHY0_SWRST | OTG_SYS_LINK_SWRST_UOTG |
    954  1.14.4.1   martin 		OTG_SYS_PHYLINK_SWRST);
    955  1.14.4.1   martin 	bus_space_write_4(&exynos_bs_tag, usb2phy_bsh,
    956  1.14.4.1   martin 		USB_PHY_OTG_SYS, phyotg);
    957  1.14.4.1   martin #endif
    958  1.14.4.1   martin 
    959  1.14.4.1   martin 	/* enable EHCI DMA burst: */
    960  1.14.4.1   martin 	ehcictrl = bus_space_read_4(&exynos_bs_tag, usb2phy_bsh,
    961  1.14.4.1   martin 	    USB_PHY_HOST_EHCICTRL);
    962  1.14.4.1   martin 	ehcictrl |= HOST_EHCICTRL_ENA_INCRXALIGN |
    963  1.14.4.1   martin 	    HOST_EHCICTRL_ENA_INCR4 | HOST_EHCICTRL_ENA_INCR8 |
    964  1.14.4.1   martin 	    HOST_EHCICTRL_ENA_INCR16;
    965  1.14.4.1   martin 	bus_space_write_4(&exynos_bs_tag, usb2phy_bsh,
    966  1.14.4.1   martin 	    USB_PHY_HOST_EHCICTRL, ehcictrl);
    967  1.14.4.1   martin 
    968  1.14.4.1   martin 	/* Set OHCI suspend */
    969  1.14.4.1   martin 	ohcictrl = bus_space_read_4(&exynos_bs_tag, usb2phy_bsh,
    970  1.14.4.1   martin 	    USB_PHY_HOST_OHCICTRL);
    971  1.14.4.1   martin 	ohcictrl |= HOST_OHCICTRL_SUSPLGCY;
    972  1.14.4.1   martin 	bus_space_write_4(&exynos_bs_tag, usb2phy_bsh,
    973  1.14.4.1   martin 	    USB_PHY_HOST_OHCICTRL, ohcictrl);
    974  1.14.4.1   martin }
    975  1.14.4.1   martin 
    976  1.14.4.1   martin 
    977  1.14.4.1   martin static void
    978  1.14.4.1   martin exynos5422_usb2phy_enable(bus_space_handle_t usb2phy_bsh)
    979  1.14.4.1   martin {
    980  1.14.4.1   martin 	aprint_error("%s not implemented\n", __func__);
    981  1.14.4.1   martin }
    982  1.14.4.1   martin #endif
    983  1.14.4.1   martin 
    984  1.14.4.1   martin 
    985  1.14.4.1   martin void
    986  1.14.4.1   martin exynos_usb_phy_init(bus_space_handle_t usb2phy_bsh)
    987  1.14.4.1   martin {
    988  1.14.4.1   martin 	/* disable phy isolation */
    989  1.14.4.1   martin 	exynos_usb2_set_isolation(false);
    990  1.14.4.1   martin 
    991  1.14.4.1   martin #ifdef EXYNOS4
    992  1.14.4.1   martin 	exynos4_usb2phy_enable(usb2phy_bsh);
    993  1.14.4.1   martin #endif
    994  1.14.4.1   martin #ifdef EXYNOS5
    995  1.14.4.1   martin 	if (IS_EXYNOS5410_P()) {
    996  1.14.4.1   martin 		exynos5410_usb2phy_enable(usb2phy_bsh);
    997  1.14.4.1   martin 		/* TBD: USB3 phy init */
    998  1.14.4.1   martin 	} else if (IS_EXYNOS5422_P()) {
    999  1.14.4.1   martin 		exynos5422_usb2phy_enable(usb2phy_bsh);
   1000  1.14.4.1   martin 		/* TBD: USB3 phy init */
   1001  1.14.4.1   martin 	}
   1002  1.14.4.1   martin #endif
   1003  1.14.4.1   martin }
   1004  1.14.4.1   martin 
   1005  1.14.4.1   martin 
   1006