2 * Copyright (c) 2017-2018 QCM Technologies.
3 * Copyright (c) 2017-2018 Semihalf.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include "opt_platform.h"
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/endian.h>
37 #include <sys/systm.h>
40 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/mutex.h>
47 #include <machine/bus.h>
52 #include <dev/iicbus/iiconf.h>
53 #include <dev/iicbus/iicbus.h>
54 #include "iicbus_if.h"
59 #include <dev/ofw/ofw_bus.h>
60 #include <dev/ofw/ofw_bus_subr.h>
71 /* OPAL I2C request */
72 struct opal_i2c_request {
74 #define OPAL_I2C_RAW_READ 0
75 #define OPAL_I2C_RAW_WRITE 1
76 #define OPAL_I2C_SM_READ 2
77 #define OPAL_I2C_SM_WRITE 3
79 uint8_t subaddr_sz; /* Max 4 */
81 uint16_t addr; /* 7 or 10 bit address */
83 uint32_t subaddr; /* Sub-address if any */
84 uint32_t size; /* Data size */
85 uint64_t buffer_pa; /* Buffer real address */
88 static int opal_i2c_attach(device_t);
89 static int opal_i2c_callback(device_t, int, caddr_t);
90 static int opal_i2c_probe(device_t);
91 static int opal_i2c_transfer(device_t, struct iic_msg *, uint32_t);
92 static int i2c_opal_send_request(uint32_t, struct opal_i2c_request *);
93 static phandle_t opal_i2c_get_node(device_t bus, device_t dev);
95 static device_method_t opal_i2c_methods[] = {
96 /* Device interface */
97 DEVMETHOD(device_probe, opal_i2c_probe),
98 DEVMETHOD(device_attach, opal_i2c_attach),
100 /* iicbus interface */
101 DEVMETHOD(iicbus_callback, opal_i2c_callback),
102 DEVMETHOD(iicbus_transfer, opal_i2c_transfer),
103 DEVMETHOD(ofw_bus_get_node, opal_i2c_get_node),
107 #define I2C_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
108 #define I2C_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
109 #define I2C_LOCK_INIT(_sc) \
110 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
113 static devclass_t opal_i2c_devclass;
115 static driver_t opal_i2c_driver = {
118 sizeof(struct opal_i2c_softc),
122 opal_i2c_probe(device_t dev)
125 if (!(ofw_bus_is_compatible(dev, "ibm,opal-i2c")))
128 device_set_desc(dev, "opal-i2c");
134 opal_i2c_attach(device_t dev)
136 struct opal_i2c_softc *sc;
139 sc = device_get_softc(dev);
142 len = OF_getproplen(ofw_bus_get_node(dev), "ibm,opal-id");
145 OF_getencprop(ofw_bus_get_node(dev), "ibm,opal-id", &sc->opal_id, len);
147 if ((sc->iicbus = device_add_child(dev, "iicbus", -1)) == NULL) {
148 device_printf(dev, "could not allocate iicbus instance\n");
154 return (bus_generic_attach(dev));
158 opal_get_async_rc(struct opal_msg msg)
160 if (msg.msg_type != OPAL_MSG_ASYNC_COMP)
161 return OPAL_PARAMETER;
163 return htobe64(msg.params[1]);
167 i2c_opal_send_request(uint32_t bus_id, struct opal_i2c_request *req)
173 token = opal_alloc_async_token();
175 memset(&msg, 0, sizeof(msg));
177 rc = opal_call(OPAL_I2C_REQUEST, token, bus_id,
179 if (rc != OPAL_ASYNC_COMPLETION)
182 rc = opal_wait_completion(&msg, sizeof(msg), token);
184 if (rc != OPAL_SUCCESS)
187 rc = opal_get_async_rc(msg);
190 opal_free_async_token(token);
196 opal_i2c_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
198 struct opal_i2c_softc *sc;
200 struct opal_i2c_request req;
202 sc = device_get_softc(dev);
204 memset(&req, 0, sizeof(req));
207 for (i = 0; i < nmsgs; i++) {
208 req.type = (msgs[i].flags & IIC_M_RD) ?
209 OPAL_I2C_RAW_READ : OPAL_I2C_RAW_WRITE;
210 req.addr = htobe16(msgs[i].slave >> 1);
211 req.size = htobe32(msgs[i].len);
212 req.buffer_pa = htobe64(pmap_kextract((uint64_t)msgs[i].buf));
214 err = i2c_opal_send_request(sc->opal_id, &req);
222 opal_i2c_callback(device_t dev, int index, caddr_t data)
227 case IIC_REQUEST_BUS:
230 case IIC_RELEASE_BUS:
241 opal_i2c_get_node(device_t bus, device_t dev)
244 /* Share controller node with iibus device. */
245 return (ofw_bus_get_node(bus));
248 DRIVER_MODULE(opal_i2c, opal_i2cm, opal_i2c_driver, opal_i2c_devclass, NULL,
250 DRIVER_MODULE(iicbus, opal_i2c, iicbus_driver, iicbus_devclass, NULL, NULL);
251 MODULE_DEPEND(opal_i2c, iicbus, 1, 1, 1);