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: releng/10.2/sys/dev/uart/uart_bus_fdt.c 283481 2015-05-24 17:57:29Z 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
66283481Sianint
67208748Srajuart_fdt_get_clock(phandle_t node, pcell_t *cell)
68208748Sraj{
69208748Sraj
70283481Sian	/* clock-frequency is a FreeBSD-only extention. */
71283481Sian	if ((OF_getencprop(node, "clock-frequency", cell,
72283481Sian	    sizeof(*cell))) <= 0) {
73208748Sraj		/* Try to retrieve parent 'bus-frequency' */
74208748Sraj		/* XXX this should go to simple-bus fixup or so */
75283481Sian		if ((OF_getencprop(OF_parent(node), "bus-frequency", cell,
76283481Sian		    sizeof(*cell))) <= 0)
77283481Sian			*cell = 0;
78283481Sian	}
79208748Sraj
80208748Sraj	return (0);
81208748Sraj}
82208748Sraj
83283481Sianint
84208748Srajuart_fdt_get_shift(phandle_t node, pcell_t *cell)
85208748Sraj{
86208748Sraj
87283481Sian	if ((OF_getencprop(node, "reg-shift", cell, sizeof(*cell))) <= 0)
88283481Sian		*cell = 0;
89208748Sraj	return (0);
90208748Sraj}
91208748Sraj
92283327Sianstatic uintptr_t
93283327Sianuart_fdt_find_device(device_t dev)
94283327Sian{
95283327Sian	struct ofw_compat_data **cd;
96283327Sian	const struct ofw_compat_data *ocd;
97283327Sian
98283327Sian	SET_FOREACH(cd, uart_fdt_class_and_device_set) {
99283327Sian		ocd = ofw_bus_search_compatible(dev, *cd);
100283327Sian		if (ocd->ocd_data != 0)
101283327Sian			return (ocd->ocd_data);
102283327Sian	}
103283327Sian	return (0);
104283327Sian}
105283327Sian
106208748Srajstatic int
107208748Srajuart_fdt_probe(device_t dev)
108208748Sraj{
109208748Sraj	struct uart_softc *sc;
110208748Sraj	phandle_t node;
111208748Sraj	pcell_t clock, shift;
112208748Sraj	int err;
113208748Sraj
114239278Sgonzo	sc = device_get_softc(dev);
115259323Sian
116266152Sian	if (!ofw_bus_status_okay(dev))
117266152Sian		return (ENXIO);
118266152Sian
119283327Sian	sc->sc_class = (struct uart_class *)uart_fdt_find_device(dev);
120283327Sian	if (sc->sc_class == NULL)
121208748Sraj		return (ENXIO);
122208748Sraj
123208748Sraj	node = ofw_bus_get_node(dev);
124208748Sraj
125208748Sraj	if ((err = uart_fdt_get_clock(node, &clock)) != 0)
126208748Sraj		return (err);
127208748Sraj	uart_fdt_get_shift(node, &shift);
128208748Sraj
129208748Sraj	return (uart_bus_probe(dev, (int)shift, (int)clock, 0, 0));
130208748Sraj}
131208748Sraj
132208748SrajDRIVER_MODULE(uart, simplebus, uart_fdt_driver, uart_devclass, 0, 0);
133283321SianDRIVER_MODULE(uart, ofwbus, uart_fdt_driver, uart_devclass, 0, 0);
134