1139749Simp/*-
2113584Ssimokawa * Copyright (c) 2016 Michal Meloun <mmel@FreeBSD.org>
3103285Sikob * All rights reserved.
4103285Sikob *
5103285Sikob * Redistribution and use in source and binary forms, with or without
6103285Sikob * modification, are permitted provided that the following conditions
7103285Sikob * are met:
8103285Sikob * 1. Redistributions of source code must retain the above copyright
9103285Sikob *    notice, this list of conditions and the following disclaimer.
10103285Sikob * 2. Redistributions in binary form must reproduce the above copyright
11103285Sikob *    notice, this list of conditions and the following disclaimer in the
12103285Sikob *    documentation and/or other materials provided with the distribution.
13103285Sikob *
14103285Sikob * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15103285Sikob * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16103285Sikob * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17103285Sikob * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18103285Sikob * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19103285Sikob * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20103285Sikob * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21103285Sikob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22103285Sikob * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23103285Sikob * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24103285Sikob * SUCH DAMAGE.
25103285Sikob */
26103285Sikob
27103285Sikob#include "opt_platform.h"
28103285Sikob
29103285Sikob#include <sys/cdefs.h>
30103285Sikob__FBSDID("$FreeBSD: stable/11/sys/arm/nvidia/tegra124/tegra124_machdep.c 331893 2018-04-02 23:19:07Z gonzo $");
31103285Sikob
32103285Sikob#include <sys/param.h>
33103285Sikob#include <sys/systm.h>
34103285Sikob#include <sys/bus.h>
35103285Sikob#include <sys/reboot.h>
36103285Sikob#include <sys/devmap.h>
37103285Sikob
38127468Ssimokawa#include <vm/vm.h>
39127468Ssimokawa
40127468Ssimokawa#include <machine/bus.h>
41127468Ssimokawa#include <machine/fdt.h>
42103285Sikob#include <machine/intr.h>
43103285Sikob#include <machine/machdep.h>
44103285Sikob#include <machine/platformvar.h>
45103285Sikob
46103285Sikob#include <dev/fdt/fdt_common.h>
47103285Sikob#include <dev/ofw/openfirm.h>
48103285Sikob
49113584Ssimokawa#include <arm/nvidia/tegra124/tegra124_mp.h>
50113584Ssimokawa
51103285Sikob#include "platform_if.h"
52103285Sikob
53103285Sikob#define	PMC_PHYSBASE		0x7000e400
54129585Sdfr#define	PMC_SIZE		0x400
55103285Sikob#define	PMC_CONTROL_REG		0x0
56129585Sdfr#define	PMC_SCRATCH0		0x50
57129585Sdfr#define	 PMC_SCRATCH0_MODE_RECOVERY	(1 << 31)
58129585Sdfr#define	 PMC_SCRATCH0_MODE_BOOTLOADER	(1 << 30)
59129585Sdfr#define	 PMC_SCRATCH0_MODE_RCM		(1 << 1)
60103285Sikob#define	 PMC_SCRATCH0_MODE_MASK		(PMC_SCRATCH0_MODE_RECOVERY | \
61103285Sikob					PMC_SCRATCH0_MODE_BOOTLOADER | \
62103285Sikob					PMC_SCRATCH0_MODE_RCM)
63129585Sdfr
64103285Sikobstatic vm_offset_t
65106810Ssimokawategra124_lastaddr(platform_t plat)
66129585Sdfr{
67103285Sikob
68103285Sikob	return (devmap_lastaddr());
69103285Sikob}
70110193Ssimokawa
71103285Sikobstatic int
72109645Ssimokawategra124_attach(platform_t plat)
73103285Sikob{
74127468Ssimokawa
75130585Sphk	return (0);
76103285Sikob}
77103285Sikob
78103285Sikobstatic void
79109645Ssimokawategra124_late_init(platform_t plat)
80103285Sikob{
81103285Sikob
82103285Sikob}
83109645Ssimokawa
84103285Sikob/*
85103285Sikob * Set up static device mappings.
86103285Sikob *
87124169Ssimokawa */
88124169Ssimokawastatic int
89103285Sikobtegra124_devmap_init(platform_t plat)
90103285Sikob{
91103285Sikob
92103285Sikob	devmap_add_entry(0x70000000, 0x01000000);
93103285Sikob	return (0);
94103285Sikob}
95103285Sikob
96103285Sikobstatic void
97103285Sikobtegra124_cpu_reset(platform_t plat)
98103285Sikob{
99103285Sikob	bus_space_handle_t pmc;
100103285Sikob	uint32_t reg;
101103285Sikob
102103285Sikob	printf("Resetting...\n");
103103285Sikob	bus_space_map(fdtbus_bs_tag, PMC_PHYSBASE, PMC_SIZE, 0, &pmc);
104129585Sdfr
105103285Sikob	reg = bus_space_read_4(fdtbus_bs_tag, pmc, PMC_SCRATCH0);
106103285Sikob	reg &= PMC_SCRATCH0_MODE_MASK;
107103285Sikob	bus_space_write_4(fdtbus_bs_tag, pmc, PMC_SCRATCH0,
108103285Sikob	   reg | PMC_SCRATCH0_MODE_BOOTLOADER); 	/* boot to bootloader */
109103285Sikob	bus_space_read_4(fdtbus_bs_tag, pmc, PMC_SCRATCH0);
110103285Sikob
111103285Sikob	reg = bus_space_read_4(fdtbus_bs_tag, pmc, PMC_CONTROL_REG);
112103285Sikob	spinlock_enter();
113103285Sikob	dsb();
114129585Sdfr	bus_space_write_4(fdtbus_bs_tag, pmc, PMC_CONTROL_REG, reg | 0x10);
115103285Sikob	bus_space_read_4(fdtbus_bs_tag, pmc, PMC_CONTROL_REG);
116103285Sikob	while(1)
117103285Sikob		;
118103285Sikob
119103285Sikob}
120103285Sikob
121103285Sikob/*
122129585Sdfr * Early putc routine for EARLY_PRINTF support.  To use, add to kernel config:
123116978Ssimokawa *   option SOCDEV_PA=0x70000000
124103285Sikob *   option SOCDEV_VA=0x70000000
125103285Sikob *   option EARLY_PRINTF
126103285Sikob */
127103285Sikob#ifdef EARLY_PRINTF
128103285Sikobstatic void
129103285Sikobtegra124_early_putc(int c)
130103285Sikob{
131103285Sikob
132103285Sikob	volatile uint32_t * UART_STAT_REG = (uint32_t *)(0x70006314);
133103285Sikob	volatile uint32_t * UART_TX_REG   = (uint32_t *)(0x70006300);
134109814Ssimokawa	const uint32_t      UART_TXRDY    = (1 << 6);
135103285Sikob	while ((*UART_STAT_REG & UART_TXRDY) == 0)
136103285Sikob		continue;
137103285Sikob	*UART_TX_REG = c;
138103285Sikob}
139110193Ssimokawaearly_putc_t *early_putc = tegra124_early_putc;
140103285Sikob#endif
141103285Sikob
142129585Sdfrstatic platform_method_t tegra124_methods[] = {
143103285Sikob	PLATFORMMETHOD(platform_attach,		tegra124_attach),
144129585Sdfr	PLATFORMMETHOD(platform_lastaddr,	tegra124_lastaddr),
145116376Ssimokawa	PLATFORMMETHOD(platform_devmap_init,	tegra124_devmap_init),
146116376Ssimokawa	PLATFORMMETHOD(platform_late_init,	tegra124_late_init),
147116376Ssimokawa	PLATFORMMETHOD(platform_cpu_reset,	tegra124_cpu_reset),
148103285Sikob
149103285Sikob#ifdef SMP
150108853Ssimokawa	PLATFORMMETHOD(platform_mp_start_ap,	tegra124_mp_start_ap),
151110193Ssimokawa	PLATFORMMETHOD(platform_mp_setmaxid,	tegra124_mp_setmaxid),
152110193Ssimokawa#endif
153129585Sdfr	PLATFORMMETHOD_END,
154124169Ssimokawa};
155129585Sdfr
156130585SphkFDT_PLATFORM_DEF(tegra124, "Nvidia Jetson-TK1", 0, "nvidia,jetson-tk1", 120);
157124169Ssimokawa