1282985Szbb/*-
2282985Szbb * Copyright (c) 2013 Ruslan Bukin <br@bsdpad.com>
3282985Szbb * Copyright (c) 2015 Semihalf.
4282985Szbb * All rights reserved.
5282985Szbb *
6282985Szbb * Redistribution and use in source and binary forms, with or without
7282985Szbb * modification, are permitted provided that the following conditions
8282985Szbb * are met:
9282985Szbb * 1. Redistributions of source code must retain the above copyright
10282985Szbb *    notice, this list of conditions and the following disclaimer.
11282985Szbb * 2. Redistributions in binary form must reproduce the above copyright
12282985Szbb *    notice, this list of conditions and the following disclaimer in the
13282985Szbb *    documentation and/or other materials provided with the distribution.
14282985Szbb *
15282985Szbb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16282985Szbb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17282985Szbb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18282985Szbb * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19282985Szbb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20282985Szbb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21282985Szbb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22282985Szbb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23282985Szbb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24282985Szbb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25282985Szbb * SUCH DAMAGE.
26282985Szbb *
27282985Szbb */
28282985Szbb
29282985Szbb#include <sys/cdefs.h>
30282985Szbb__FBSDID("$FreeBSD: stable/11/sys/arm/annapurna/alpine/common.c 314506 2017-03-01 19:55:04Z ian $");
31282985Szbb
32289547Sian#include "opt_platform.h"
33289547Sian
34282985Szbb#include <sys/param.h>
35282985Szbb#include <sys/systm.h>
36282985Szbb#include <sys/bus.h>
37282985Szbb#include <sys/kernel.h>
38282985Szbb
39282985Szbb#include <dev/fdt/fdt_common.h>
40282985Szbb#include <dev/ofw/openfirm.h>
41282985Szbb
42282985Szbb#include <machine/bus.h>
43282985Szbb#include <machine/fdt.h>
44282985Szbb#include <machine/intr.h>
45282985Szbb
46282985Szbb#define WDTLOAD		0x000
47282985Szbb#define LOAD_MIN	0x00000001
48282985Szbb#define LOAD_MAX	0xFFFFFFFF
49282985Szbb#define WDTVALUE	0x004
50282985Szbb#define WDTCONTROL	0x008
51282985Szbb/* control register masks */
52282985Szbb#define INT_ENABLE	(1 << 0)
53282985Szbb#define RESET_ENABLE	(1 << 1)
54282985Szbb#define WDTLOCK		0xC00
55282985Szbb#define UNLOCK		0x1ACCE551
56282985Szbb#define LOCK		0x00000001
57282985Szbb
58282985Szbbextern bus_addr_t  al_devmap_pa;
59282985Szbb
60282985Szbbstatic int alpine_get_wdt_base(uint32_t *pbase, uint32_t *psize);
61282985Szbbstatic int alpine_pic_decode_fdt(uint32_t iparent, uint32_t *intr,
62282985Szbb    int *interrupt, int *trig, int *pol);
63282985Szbb
64282985Szbbint alpine_get_devmap_base(bus_addr_t *pa, bus_addr_t *size);
65282985Szbb
66282985Szbbint alpine_get_devmap_base(bus_addr_t *pa, bus_addr_t *size)
67282985Szbb{
68282985Szbb	phandle_t node;
69282985Szbb
70282985Szbb	if ((node = OF_finddevice("/")) == 0)
71282985Szbb		return (ENXIO);
72282985Szbb
73282985Szbb	if ((node = fdt_find_compatible(node, "simple-bus", 1)) == 0)
74282985Szbb		return (ENXIO);
75282985Szbb
76282985Szbb	return fdt_get_range(node, 0, pa, size);
77282985Szbb}
78282985Szbb
79282985Szbbstatic int
80282985Szbbalpine_get_wdt_base(uint32_t *pbase, uint32_t *psize)
81282985Szbb{
82282985Szbb	phandle_t node;
83282985Szbb	u_long base = 0;
84282985Szbb	u_long size = 0;
85282985Szbb
86282985Szbb	if (pbase == NULL || psize == NULL)
87282985Szbb		return (EINVAL);
88282985Szbb
89282985Szbb	if ((node = OF_finddevice("/")) == -1)
90282985Szbb		return (EFAULT);
91282985Szbb
92282985Szbb	if ((node = fdt_find_compatible(node, "simple-bus", 1)) == 0)
93282985Szbb		return (EFAULT);
94282985Szbb
95282985Szbb	if ((node =
96282985Szbb	    fdt_find_compatible(node, "arm,sp805", 1)) == 0)
97282985Szbb		return (EFAULT);
98282985Szbb
99282985Szbb	if (fdt_regsize(node, &base, &size))
100282985Szbb		return (EFAULT);
101282985Szbb
102282985Szbb	*pbase = base;
103282985Szbb	*psize = size;
104282985Szbb
105282985Szbb	return (0);
106282985Szbb}
107282985Szbb
108282985Szbbvoid
109282985Szbbcpu_reset(void)
110282985Szbb{
111282985Szbb	uint32_t wdbase, wdsize;
112282985Szbb	bus_addr_t wdbaddr;
113282985Szbb	int ret;
114282985Szbb
115282985Szbb	ret = alpine_get_wdt_base(&wdbase, &wdsize);
116282985Szbb	if (ret) {
117282985Szbb		printf("Unable to get WDT base, do power down manually...");
118282985Szbb		goto infinite;
119282985Szbb	}
120282985Szbb
121282985Szbb	ret = bus_space_map(fdtbus_bs_tag, al_devmap_pa + wdbase,
122282985Szbb	    wdsize, 0, &wdbaddr);
123282985Szbb	if (ret) {
124282985Szbb		printf("Unable to map WDT base, do power down manually...");
125282985Szbb		goto infinite;
126282985Szbb	}
127282985Szbb
128282985Szbb	bus_space_write_4(fdtbus_bs_tag, wdbaddr, WDTLOCK, UNLOCK);
129282985Szbb	bus_space_write_4(fdtbus_bs_tag, wdbaddr, WDTLOAD, LOAD_MIN);
130282985Szbb	bus_space_write_4(fdtbus_bs_tag, wdbaddr, WDTCONTROL, INT_ENABLE | RESET_ENABLE);
131282985Szbb
132282985Szbbinfinite:
133282985Szbb	while (1) {}
134282985Szbb}
135282985Szbb
136298068Sandrew#ifndef INTRNG
137282985Szbbstatic int
138282985Szbbalpine_pic_decode_fdt(uint32_t iparent, uint32_t *intr, int *interrupt,
139282985Szbb    int *trig, int *pol)
140282985Szbb{
141282985Szbb	int rv = 0;
142282985Szbb
143282985Szbb	rv = gic_decode_fdt(iparent, intr, interrupt, trig, pol);
144282985Szbb	if (rv == 0) {
145282985Szbb		/* This was recognized as our PIC and decoded. */
146282985Szbb		interrupt = FDT_MAP_IRQ(iparent, interrupt);
147282985Szbb
148282985Szbb		/* Configure the interrupt if callback provided */
149282985Szbb		if (arm_config_irq)
150282985Szbb			(*arm_config_irq)(*interrupt, *trig, *pol);
151282985Szbb	}
152282985Szbb	return (rv);
153282985Szbb}
154282985Szbb
155282985Szbbfdt_pic_decode_t fdt_pic_table[] = {
156282985Szbb	&alpine_pic_decode_fdt,
157282985Szbb	NULL
158282985Szbb};
159295509Sandrew#endif
160