common.c revision 298068
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: head/sys/arm/annapurna/alpine/common.c 298068 2016-04-15 16:05:41Z andrew $");
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;
59282985Szbbstruct fdt_fixup_entry fdt_fixup_table[] = {
60282985Szbb	{ NULL, NULL }
61282985Szbb};
62282985Szbb
63282985Szbbstatic int alpine_get_wdt_base(uint32_t *pbase, uint32_t *psize);
64282985Szbbstatic int alpine_pic_decode_fdt(uint32_t iparent, uint32_t *intr,
65282985Szbb    int *interrupt, int *trig, int *pol);
66282985Szbb
67282985Szbbint alpine_get_devmap_base(bus_addr_t *pa, bus_addr_t *size);
68282985Szbb
69282985Szbbint alpine_get_devmap_base(bus_addr_t *pa, bus_addr_t *size)
70282985Szbb{
71282985Szbb	phandle_t node;
72282985Szbb
73282985Szbb	if ((node = OF_finddevice("/")) == 0)
74282985Szbb		return (ENXIO);
75282985Szbb
76282985Szbb	if ((node = fdt_find_compatible(node, "simple-bus", 1)) == 0)
77282985Szbb		return (ENXIO);
78282985Szbb
79282985Szbb	return fdt_get_range(node, 0, pa, size);
80282985Szbb}
81282985Szbb
82282985Szbbstatic int
83282985Szbbalpine_get_wdt_base(uint32_t *pbase, uint32_t *psize)
84282985Szbb{
85282985Szbb	phandle_t node;
86282985Szbb	u_long base = 0;
87282985Szbb	u_long size = 0;
88282985Szbb
89282985Szbb	if (pbase == NULL || psize == NULL)
90282985Szbb		return (EINVAL);
91282985Szbb
92282985Szbb	if ((node = OF_finddevice("/")) == -1)
93282985Szbb		return (EFAULT);
94282985Szbb
95282985Szbb	if ((node = fdt_find_compatible(node, "simple-bus", 1)) == 0)
96282985Szbb		return (EFAULT);
97282985Szbb
98282985Szbb	if ((node =
99282985Szbb	    fdt_find_compatible(node, "arm,sp805", 1)) == 0)
100282985Szbb		return (EFAULT);
101282985Szbb
102282985Szbb	if (fdt_regsize(node, &base, &size))
103282985Szbb		return (EFAULT);
104282985Szbb
105282985Szbb	*pbase = base;
106282985Szbb	*psize = size;
107282985Szbb
108282985Szbb	return (0);
109282985Szbb}
110282985Szbb
111282985Szbbvoid
112282985Szbbcpu_reset(void)
113282985Szbb{
114282985Szbb	uint32_t wdbase, wdsize;
115282985Szbb	bus_addr_t wdbaddr;
116282985Szbb	int ret;
117282985Szbb
118282985Szbb	ret = alpine_get_wdt_base(&wdbase, &wdsize);
119282985Szbb	if (ret) {
120282985Szbb		printf("Unable to get WDT base, do power down manually...");
121282985Szbb		goto infinite;
122282985Szbb	}
123282985Szbb
124282985Szbb	ret = bus_space_map(fdtbus_bs_tag, al_devmap_pa + wdbase,
125282985Szbb	    wdsize, 0, &wdbaddr);
126282985Szbb	if (ret) {
127282985Szbb		printf("Unable to map WDT base, do power down manually...");
128282985Szbb		goto infinite;
129282985Szbb	}
130282985Szbb
131282985Szbb	bus_space_write_4(fdtbus_bs_tag, wdbaddr, WDTLOCK, UNLOCK);
132282985Szbb	bus_space_write_4(fdtbus_bs_tag, wdbaddr, WDTLOAD, LOAD_MIN);
133282985Szbb	bus_space_write_4(fdtbus_bs_tag, wdbaddr, WDTCONTROL, INT_ENABLE | RESET_ENABLE);
134282985Szbb
135282985Szbbinfinite:
136282985Szbb	while (1) {}
137282985Szbb}
138282985Szbb
139298068Sandrew#ifndef INTRNG
140282985Szbbstatic int
141282985Szbbalpine_pic_decode_fdt(uint32_t iparent, uint32_t *intr, int *interrupt,
142282985Szbb    int *trig, int *pol)
143282985Szbb{
144282985Szbb	int rv = 0;
145282985Szbb
146282985Szbb	rv = gic_decode_fdt(iparent, intr, interrupt, trig, pol);
147282985Szbb	if (rv == 0) {
148282985Szbb		/* This was recognized as our PIC and decoded. */
149282985Szbb		interrupt = FDT_MAP_IRQ(iparent, interrupt);
150282985Szbb
151282985Szbb		/* Configure the interrupt if callback provided */
152282985Szbb		if (arm_config_irq)
153282985Szbb			(*arm_config_irq)(*interrupt, *trig, *pol);
154282985Szbb	}
155282985Szbb	return (rv);
156282985Szbb}
157282985Szbb
158282985Szbbfdt_pic_decode_t fdt_pic_table[] = {
159282985Szbb	&alpine_pic_decode_fdt,
160282985Szbb	NULL
161282985Szbb};
162295509Sandrew#endif
163