1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2004-2006 Marcel Moolenaar
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/param.h>
30#include <sys/systm.h>
31#include <sys/bus.h>
32#include <sys/conf.h>
33#include <sys/kernel.h>
34#include <sys/module.h>
35
36#include <machine/bus.h>
37#include <sys/rman.h>
38#include <machine/resource.h>
39
40#include <dev/scc/scc_bus.h>
41
42#include <dev/uart/uart.h>
43#include <dev/uart/uart_bus.h>
44
45static int uart_scc_attach(device_t dev);
46static int uart_scc_probe(device_t dev);
47
48static device_method_t uart_scc_methods[] = {
49	/* Device interface */
50	DEVMETHOD(device_probe,		uart_scc_probe),
51	DEVMETHOD(device_attach,	uart_scc_attach),
52	DEVMETHOD(device_detach,	uart_bus_detach),
53	/* Serdev interface */
54	DEVMETHOD(serdev_ihand,		uart_bus_ihand),
55	DEVMETHOD(serdev_sysdev,	uart_bus_sysdev),
56	{ 0, 0 }
57};
58
59static driver_t uart_scc_driver = {
60	uart_driver_name,
61	uart_scc_methods,
62	sizeof(struct uart_softc),
63};
64
65static int
66uart_scc_attach(device_t dev)
67{
68	device_t parent;
69	struct uart_softc *sc;
70	uintptr_t mtx;
71
72	parent = device_get_parent(dev);
73	sc = device_get_softc(dev);
74
75	if (BUS_READ_IVAR(parent, dev, SCC_IVAR_HWMTX, &mtx))
76		return (ENXIO);
77	sc->sc_hwmtx = (struct mtx *)(void *)mtx;
78	return (uart_bus_attach(dev));
79}
80
81static int
82uart_scc_probe(device_t dev)
83{
84	device_t parent;
85	struct uart_softc *sc;
86	uintptr_t ch, cl, md, rs;
87
88	parent = device_get_parent(dev);
89	sc = device_get_softc(dev);
90
91	if (BUS_READ_IVAR(parent, dev, SCC_IVAR_MODE, &md) ||
92	    BUS_READ_IVAR(parent, dev, SCC_IVAR_CLASS, &cl))
93		return (ENXIO);
94	if (md != SCC_MODE_ASYNC)
95		return (ENXIO);
96	switch (cl) {
97	case SCC_CLASS_QUICC:
98		sc->sc_class = &uart_quicc_class;
99		break;
100	case SCC_CLASS_Z8530:
101		sc->sc_class = &uart_z8530_class;
102		break;
103	default:
104		return (ENXIO);
105	}
106	if (BUS_READ_IVAR(parent, dev, SCC_IVAR_CHANNEL, &ch) ||
107	    BUS_READ_IVAR(parent, dev, SCC_IVAR_CLOCK, &cl) ||
108	    BUS_READ_IVAR(parent, dev, SCC_IVAR_REGSHFT, &rs))
109		return (ENXIO);
110
111	return (uart_bus_probe(dev, rs, 0, cl, 0, ch, 0));
112}
113
114DRIVER_MODULE(uart, scc, uart_scc_driver, 0, 0);
115