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