uart_bus_fdt.c revision 283327
1208748Sraj/*-
2208748Sraj * Copyright (c) 2009-2010 The FreeBSD Foundation
3208748Sraj * All rights reserved.
4208748Sraj *
5208748Sraj * This software was developed by Semihalf under sponsorship from
6208748Sraj * the FreeBSD Foundation.
7208748Sraj *
8208748Sraj * Redistribution and use in source and binary forms, with or without
9208748Sraj * modification, are permitted provided that the following conditions
10208748Sraj * are met:
11208748Sraj * 1. Redistributions of source code must retain the above copyright
12208748Sraj *    notice, this list of conditions and the following disclaimer.
13208748Sraj * 2. Redistributions in binary form must reproduce the above copyright
14208748Sraj *    notice, this list of conditions and the following disclaimer in the
15208748Sraj *    documentation and/or other materials provided with the distribution.
16208748Sraj *
17208748Sraj * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18208748Sraj * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19208748Sraj * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20208748Sraj * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21208748Sraj * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22208748Sraj * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23208748Sraj * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24208748Sraj * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25208748Sraj * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26208748Sraj * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27208748Sraj * SUCH DAMAGE.
28208748Sraj */
29208748Sraj
30208748Sraj#include <sys/cdefs.h>
31208748Sraj__FBSDID("$FreeBSD: stable/10/sys/dev/uart/uart_bus_fdt.c 283327 2015-05-23 20:54:25Z ian $");
32208748Sraj
33259357Sian#include "opt_platform.h"
34259357Sian
35208748Sraj#include <sys/param.h>
36208748Sraj#include <sys/bus.h>
37208748Sraj#include <sys/kernel.h>
38208748Sraj#include <sys/module.h>
39208748Sraj
40208748Sraj#include <machine/bus.h>
41208748Sraj
42208748Sraj#include <dev/fdt/fdt_common.h>
43208748Sraj#include <dev/ofw/ofw_bus.h>
44208748Sraj#include <dev/ofw/ofw_bus_subr.h>
45208748Sraj#include <dev/uart/uart.h>
46208748Sraj#include <dev/uart/uart_bus.h>
47208748Sraj#include <dev/uart/uart_cpu.h>
48283327Sian#include <dev/uart/uart_cpu_fdt.h>
49208748Sraj
50208748Srajstatic int uart_fdt_probe(device_t);
51208748Sraj
52208748Srajstatic device_method_t uart_fdt_methods[] = {
53208748Sraj	/* Device interface */
54208748Sraj	DEVMETHOD(device_probe,		uart_fdt_probe),
55208748Sraj	DEVMETHOD(device_attach,	uart_bus_attach),
56208748Sraj	DEVMETHOD(device_detach,	uart_bus_detach),
57208748Sraj	{ 0, 0 }
58208748Sraj};
59208748Sraj
60208748Srajstatic driver_t uart_fdt_driver = {
61208748Sraj	uart_driver_name,
62208748Sraj	uart_fdt_methods,
63208748Sraj	sizeof(struct uart_softc),
64208748Sraj};
65208748Sraj
66208748Srajstatic int
67208748Srajuart_fdt_get_clock(phandle_t node, pcell_t *cell)
68208748Sraj{
69208748Sraj	pcell_t clock;
70208748Sraj
71266277Sian	/*
72266277Sian	 * clock-frequency is a FreeBSD-specific hack. Make its presence optional.
73266277Sian	 */
74208748Sraj	if ((OF_getprop(node, "clock-frequency", &clock,
75208748Sraj	    sizeof(clock))) <= 0)
76266277Sian		clock = 0;
77208748Sraj
78208748Sraj	if (clock == 0)
79208748Sraj		/* Try to retrieve parent 'bus-frequency' */
80208748Sraj		/* XXX this should go to simple-bus fixup or so */
81208748Sraj		if ((OF_getprop(OF_parent(node), "bus-frequency", &clock,
82208748Sraj		    sizeof(clock))) <= 0)
83208748Sraj			clock = 0;
84208748Sraj
85208748Sraj	*cell = fdt32_to_cpu(clock);
86208748Sraj	return (0);
87208748Sraj}
88208748Sraj
89208748Srajstatic int
90208748Srajuart_fdt_get_shift(phandle_t node, pcell_t *cell)
91208748Sraj{
92208748Sraj	pcell_t shift;
93208748Sraj
94208748Sraj	if ((OF_getprop(node, "reg-shift", &shift, sizeof(shift))) <= 0)
95208748Sraj		shift = 0;
96208748Sraj	*cell = fdt32_to_cpu(shift);
97208748Sraj	return (0);
98208748Sraj}
99208748Sraj
100283327Sianstatic uintptr_t
101283327Sianuart_fdt_find_device(device_t dev)
102283327Sian{
103283327Sian	struct ofw_compat_data **cd;
104283327Sian	const struct ofw_compat_data *ocd;
105283327Sian
106283327Sian	SET_FOREACH(cd, uart_fdt_class_and_device_set) {
107283327Sian		ocd = ofw_bus_search_compatible(dev, *cd);
108283327Sian		if (ocd->ocd_data != 0)
109283327Sian			return (ocd->ocd_data);
110283327Sian	}
111283327Sian	return (0);
112283327Sian}
113283327Sian
114208748Srajstatic int
115208748Srajuart_fdt_probe(device_t dev)
116208748Sraj{
117208748Sraj	struct uart_softc *sc;
118208748Sraj	phandle_t node;
119208748Sraj	pcell_t clock, shift;
120208748Sraj	int err;
121208748Sraj
122239278Sgonzo	sc = device_get_softc(dev);
123259323Sian
124266152Sian	if (!ofw_bus_status_okay(dev))
125266152Sian		return (ENXIO);
126266152Sian
127283327Sian	sc->sc_class = (struct uart_class *)uart_fdt_find_device(dev);
128283327Sian	if (sc->sc_class == NULL)
129208748Sraj		return (ENXIO);
130208748Sraj
131208748Sraj	node = ofw_bus_get_node(dev);
132208748Sraj
133208748Sraj	if ((err = uart_fdt_get_clock(node, &clock)) != 0)
134208748Sraj		return (err);
135208748Sraj	uart_fdt_get_shift(node, &shift);
136208748Sraj
137208748Sraj	return (uart_bus_probe(dev, (int)shift, (int)clock, 0, 0));
138208748Sraj}
139208748Sraj
140208748SrajDRIVER_MODULE(uart, simplebus, uart_fdt_driver, uart_devclass, 0, 0);
141283321SianDRIVER_MODULE(uart, ofwbus, uart_fdt_driver, uart_devclass, 0, 0);
142