1580d00f4SEmmanuel Vadot /*-
2580d00f4SEmmanuel Vadot * Copyright (c) 2016-2019 Emmanuel Vadot <manu@freebsd.org>
3580d00f4SEmmanuel Vadot *
4580d00f4SEmmanuel Vadot * Redistribution and use in source and binary forms, with or without
5580d00f4SEmmanuel Vadot * modification, are permitted provided that the following conditions
6580d00f4SEmmanuel Vadot * are met:
7580d00f4SEmmanuel Vadot * 1. Redistributions of source code must retain the above copyright
8580d00f4SEmmanuel Vadot * notice, this list of conditions and the following disclaimer.
9580d00f4SEmmanuel Vadot * 2. Redistributions in binary form must reproduce the above copyright
10580d00f4SEmmanuel Vadot * notice, this list of conditions and the following disclaimer in the
11580d00f4SEmmanuel Vadot * documentation and/or other materials provided with the distribution.
12580d00f4SEmmanuel Vadot *
13580d00f4SEmmanuel Vadot * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14580d00f4SEmmanuel Vadot * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15580d00f4SEmmanuel Vadot * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16580d00f4SEmmanuel Vadot * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17580d00f4SEmmanuel Vadot * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18580d00f4SEmmanuel Vadot * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19580d00f4SEmmanuel Vadot * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20580d00f4SEmmanuel Vadot * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21580d00f4SEmmanuel Vadot * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22580d00f4SEmmanuel Vadot * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23580d00f4SEmmanuel Vadot * SUCH DAMAGE.
24580d00f4SEmmanuel Vadot */
25580d00f4SEmmanuel Vadot
26580d00f4SEmmanuel Vadot #include <sys/param.h>
27580d00f4SEmmanuel Vadot #include <sys/systm.h>
28580d00f4SEmmanuel Vadot #include <sys/bus.h>
29580d00f4SEmmanuel Vadot #include <sys/kernel.h>
30580d00f4SEmmanuel Vadot #include <sys/module.h>
31580d00f4SEmmanuel Vadot #include <sys/resource.h>
32580d00f4SEmmanuel Vadot
33580d00f4SEmmanuel Vadot #include <machine/bus.h>
34580d00f4SEmmanuel Vadot #include <machine/resource.h>
35580d00f4SEmmanuel Vadot
36580d00f4SEmmanuel Vadot #include <sys/rman.h>
37580d00f4SEmmanuel Vadot
38580d00f4SEmmanuel Vadot #include <sys/lock.h>
39580d00f4SEmmanuel Vadot #include <sys/mutex.h>
40580d00f4SEmmanuel Vadot
41580d00f4SEmmanuel Vadot #include <dev/iicbus/iiconf.h>
42580d00f4SEmmanuel Vadot #include <dev/iicbus/iicbus.h>
43580d00f4SEmmanuel Vadot #include <dev/iicbus/controller/twsi/twsi.h>
44580d00f4SEmmanuel Vadot #include <dev/ofw/ofw_bus.h>
45580d00f4SEmmanuel Vadot #include <dev/ofw/ofw_bus_subr.h>
46580d00f4SEmmanuel Vadot
47be82b3a0SEmmanuel Vadot #include <dev/clk/clk.h>
48*1f469a9fSEmmanuel Vadot #include <dev/hwreset/hwreset.h>
49580d00f4SEmmanuel Vadot
50580d00f4SEmmanuel Vadot #include "iicbus_if.h"
51580d00f4SEmmanuel Vadot
52580d00f4SEmmanuel Vadot #define TWI_ADDR 0x0
53580d00f4SEmmanuel Vadot #define TWI_XADDR 0x4
54580d00f4SEmmanuel Vadot #define TWI_DATA 0x8
55580d00f4SEmmanuel Vadot #define TWI_CNTR 0xC
56580d00f4SEmmanuel Vadot #define TWI_STAT 0x10
57580d00f4SEmmanuel Vadot #define TWI_CCR 0x14
58580d00f4SEmmanuel Vadot #define TWI_SRST 0x18
59580d00f4SEmmanuel Vadot #define TWI_EFR 0x1C
60580d00f4SEmmanuel Vadot #define TWI_LCR 0x20
61580d00f4SEmmanuel Vadot
62580d00f4SEmmanuel Vadot static struct ofw_compat_data compat_data[] = {
63580d00f4SEmmanuel Vadot {"allwinner,sun4i-a10-i2c", 1},
64580d00f4SEmmanuel Vadot {"allwinner,sun6i-a31-i2c", 1},
65580d00f4SEmmanuel Vadot {"allwinner,sun8i-a83t-i2c", 1},
66580d00f4SEmmanuel Vadot {NULL, 0},
67580d00f4SEmmanuel Vadot };
68580d00f4SEmmanuel Vadot
69580d00f4SEmmanuel Vadot static int
a10_twsi_probe(device_t dev)70580d00f4SEmmanuel Vadot a10_twsi_probe(device_t dev)
71580d00f4SEmmanuel Vadot {
72580d00f4SEmmanuel Vadot
73580d00f4SEmmanuel Vadot if (!ofw_bus_status_okay(dev))
74580d00f4SEmmanuel Vadot return (ENXIO);
75580d00f4SEmmanuel Vadot
76580d00f4SEmmanuel Vadot if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
77580d00f4SEmmanuel Vadot return (ENXIO);
78580d00f4SEmmanuel Vadot
79580d00f4SEmmanuel Vadot device_set_desc(dev, "Allwinner Integrated I2C Bus Controller");
80580d00f4SEmmanuel Vadot return (BUS_PROBE_DEFAULT);
81580d00f4SEmmanuel Vadot }
82580d00f4SEmmanuel Vadot
83580d00f4SEmmanuel Vadot static int
a10_twsi_attach(device_t dev)84580d00f4SEmmanuel Vadot a10_twsi_attach(device_t dev)
85580d00f4SEmmanuel Vadot {
86580d00f4SEmmanuel Vadot struct twsi_softc *sc;
87580d00f4SEmmanuel Vadot hwreset_t rst;
88580d00f4SEmmanuel Vadot int error;
89580d00f4SEmmanuel Vadot
90580d00f4SEmmanuel Vadot sc = device_get_softc(dev);
91580d00f4SEmmanuel Vadot
92580d00f4SEmmanuel Vadot /* De-assert reset */
93580d00f4SEmmanuel Vadot if (hwreset_get_by_ofw_idx(dev, 0, 0, &rst) == 0) {
94580d00f4SEmmanuel Vadot error = hwreset_deassert(rst);
95580d00f4SEmmanuel Vadot if (error != 0) {
96580d00f4SEmmanuel Vadot device_printf(dev, "could not de-assert reset\n");
97580d00f4SEmmanuel Vadot return (error);
98580d00f4SEmmanuel Vadot }
99580d00f4SEmmanuel Vadot }
100580d00f4SEmmanuel Vadot
101580d00f4SEmmanuel Vadot /* Activate clock */
102580d00f4SEmmanuel Vadot error = clk_get_by_ofw_index(dev, 0, 0, &sc->clk_core);
103580d00f4SEmmanuel Vadot if (error != 0) {
104580d00f4SEmmanuel Vadot device_printf(dev, "could not find clock\n");
105580d00f4SEmmanuel Vadot return (error);
106580d00f4SEmmanuel Vadot }
107580d00f4SEmmanuel Vadot error = clk_enable(sc->clk_core);
108580d00f4SEmmanuel Vadot if (error != 0) {
109580d00f4SEmmanuel Vadot device_printf(dev, "could not enable clock\n");
110580d00f4SEmmanuel Vadot return (error);
111580d00f4SEmmanuel Vadot }
112580d00f4SEmmanuel Vadot
113580d00f4SEmmanuel Vadot sc->reg_data = TWI_DATA;
114580d00f4SEmmanuel Vadot sc->reg_slave_addr = TWI_ADDR;
115580d00f4SEmmanuel Vadot sc->reg_slave_ext_addr = TWI_XADDR;
116580d00f4SEmmanuel Vadot sc->reg_control = TWI_CNTR;
117580d00f4SEmmanuel Vadot sc->reg_status = TWI_STAT;
118580d00f4SEmmanuel Vadot sc->reg_baud_rate = TWI_CCR;
119580d00f4SEmmanuel Vadot sc->reg_soft_reset = TWI_SRST;
120580d00f4SEmmanuel Vadot
121580d00f4SEmmanuel Vadot if (ofw_bus_is_compatible(dev, "allwinner,sun6i-a31-i2c") ||
122580d00f4SEmmanuel Vadot ofw_bus_is_compatible(dev, "allwinner,sun6i-a83t-i2c"))
123580d00f4SEmmanuel Vadot sc->iflag_w1c = true;
124580d00f4SEmmanuel Vadot return (twsi_attach(dev));
125580d00f4SEmmanuel Vadot }
126580d00f4SEmmanuel Vadot
127580d00f4SEmmanuel Vadot static phandle_t
a10_twsi_get_node(device_t bus,device_t dev)128580d00f4SEmmanuel Vadot a10_twsi_get_node(device_t bus, device_t dev)
129580d00f4SEmmanuel Vadot {
130580d00f4SEmmanuel Vadot return (ofw_bus_get_node(bus));
131580d00f4SEmmanuel Vadot }
132580d00f4SEmmanuel Vadot
133580d00f4SEmmanuel Vadot static device_method_t a10_twsi_methods[] = {
134580d00f4SEmmanuel Vadot /* device interface */
135580d00f4SEmmanuel Vadot DEVMETHOD(device_probe, a10_twsi_probe),
136580d00f4SEmmanuel Vadot DEVMETHOD(device_attach, a10_twsi_attach),
137580d00f4SEmmanuel Vadot
138580d00f4SEmmanuel Vadot /* OFW methods */
139580d00f4SEmmanuel Vadot DEVMETHOD(ofw_bus_get_node, a10_twsi_get_node),
140580d00f4SEmmanuel Vadot
141580d00f4SEmmanuel Vadot { 0, 0 }
142580d00f4SEmmanuel Vadot };
143580d00f4SEmmanuel Vadot
144580d00f4SEmmanuel Vadot DEFINE_CLASS_1(iichb, a10_twsi_driver, a10_twsi_methods,
145580d00f4SEmmanuel Vadot sizeof(struct twsi_softc), twsi_driver);
146580d00f4SEmmanuel Vadot
147580d00f4SEmmanuel Vadot EARLY_DRIVER_MODULE(a10_twsi, simplebus, a10_twsi_driver,
148580d00f4SEmmanuel Vadot 0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LATE);
149580d00f4SEmmanuel Vadot EARLY_DRIVER_MODULE(iicbus, a10_twsi, iicbus_driver,
150580d00f4SEmmanuel Vadot 0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LATE);
151580d00f4SEmmanuel Vadot MODULE_DEPEND(a10_twsi, iicbus, 1, 1, 1);
152580d00f4SEmmanuel Vadot SIMPLEBUS_PNP_INFO(compat_data);
153