2 * Copyright (c) 2003-2012 Broadcom Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * 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
13 * the documentation and/or other materials provided with the
16 * THIS SOFTWARE IS PROVIDED BY BROADCOM ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL BROADCOM OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/mutex.h>
41 #include <machine/bus.h>
43 #include <dev/iicbus/iiconf.h>
44 #include <dev/iicbus/iicbus.h>
45 #include <dev/iicbus/iicoc.h>
47 #include <dev/pci/pcireg.h>
48 #include <dev/pci/pcivar.h>
50 #include "iicbus_if.h"
52 static devclass_t iicoc_devclass;
57 static int iicoc_probe(device_t);
58 static int iicoc_attach(device_t);
59 static int iicoc_detach(device_t);
61 static int iicoc_start(device_t dev, u_char slave, int timeout);
62 static int iicoc_stop(device_t dev);
63 static int iicoc_read(device_t dev, char *buf,
64 int len, int *read, int last, int delay);
65 static int iicoc_write(device_t dev, const char *buf,
66 int len, int *sent, int timeout);
67 static int iicoc_repeated_start(device_t dev, u_char slave, int timeout);
70 device_t dev; /* Self */
71 u_int reg_shift; /* Chip specific */
74 struct resource *mem_res; /* Memory resource */
83 iicoc_dev_write(device_t dev, int reg, int value)
85 struct iicoc_softc *sc;
87 sc = device_get_softc(dev);
88 bus_write_1(sc->mem_res, reg<<sc->reg_shift, value);
92 iicoc_dev_read(device_t dev, int reg)
95 struct iicoc_softc *sc;
97 sc = device_get_softc(dev);
98 val = bus_read_1(sc->mem_res, reg<<sc->reg_shift);
103 iicoc_wait_on_status(device_t dev, uint8_t bit)
105 int tries = I2C_TIMEOUT;
109 status = iicoc_dev_read(dev, OC_I2C_STATUS_REG);
110 } while ((status & bit) != 0 && --tries > 0);
112 return (tries == 0 ? -1: 0);
116 iicoc_rd_cmd(device_t dev, uint8_t cmd)
120 iicoc_dev_write(dev, OC_I2C_CMD_REG, cmd);
121 if (iicoc_wait_on_status(dev, OC_STATUS_TIP) < 0) {
122 device_printf(dev, "read: Timeout waiting for TIP clear.\n");
125 data = iicoc_dev_read(dev, OC_I2C_DATA_REG);
130 iicoc_wr_cmd(device_t dev, uint8_t data, uint8_t cmd)
133 iicoc_dev_write(dev, OC_I2C_DATA_REG, data);
134 iicoc_dev_write(dev, OC_I2C_CMD_REG, cmd);
135 if (iicoc_wait_on_status(dev, OC_STATUS_TIP) < 0) {
136 device_printf(dev, "write: Timeout waiting for TIP clear.\n");
143 iicoc_wr_ack_cmd(device_t dev, uint8_t data, uint8_t cmd)
145 if (iicoc_wr_cmd(dev, data, cmd) < 0)
148 if (iicoc_dev_read(dev, OC_I2C_STATUS_REG) & OC_STATUS_NACK) {
149 device_printf(dev, "write: I2C command ACK Error.\n");
156 iicoc_init(device_t dev)
158 struct iicoc_softc *sc;
161 sc = device_get_softc(dev);
162 value = iicoc_dev_read(dev, OC_I2C_CTRL_REG);
163 iicoc_dev_write(dev, OC_I2C_CTRL_REG,
164 value & ~(OC_CONTROL_EN | OC_CONTROL_IEN));
165 value = (sc->clockfreq/(5 * sc->i2cfreq)) - 1;
166 iicoc_dev_write(dev, OC_I2C_PRESCALE_LO_REG, value & 0xff);
167 iicoc_dev_write(dev, OC_I2C_PRESCALE_HI_REG, value >> 8);
168 value = iicoc_dev_read(dev, OC_I2C_CTRL_REG);
169 iicoc_dev_write(dev, OC_I2C_CTRL_REG, value | OC_CONTROL_EN);
171 value = iicoc_dev_read(dev, OC_I2C_CTRL_REG);
172 /* return 0 on success, 1 on error */
173 return ((value & OC_CONTROL_EN) == 0);
177 iicoc_probe(device_t dev)
179 struct iicoc_softc *sc;
181 sc = device_get_softc(dev);
182 if ((pci_get_vendor(dev) == 0x184e) &&
183 (pci_get_device(dev) == 0x1011)) {
184 sc->clockfreq = XLP_I2C_CLKFREQ;
185 sc->i2cfreq = XLP_I2C_FREQ;
187 device_set_desc(dev, "Netlogic XLP I2C Controller");
188 return (BUS_PROBE_DEFAULT);
195 * We add all the devices which we know about.
196 * The generic attach routine will attach them if they are alive.
199 iicoc_attach(device_t dev)
202 struct iicoc_softc *sc;
204 sc = device_get_softc(dev);
205 bus = device_get_unit(dev);
208 mtx_init(&sc->sc_mtx, "iicoc", "iicoc", MTX_DEF);
210 sc->mem_res = bus_alloc_resource(dev,
211 SYS_RES_MEMORY, &sc->mem_rid, 0ul, ~0ul, 0x100, RF_ACTIVE);
213 if (sc->mem_res == NULL) {
214 device_printf(dev, "Could not allocate bus resource.\n");
218 sc->iicbus = device_add_child(dev, "iicbus", -1);
219 if (sc->iicbus == NULL) {
220 device_printf(dev, "Could not allocate iicbus instance.\n");
223 bus_generic_attach(dev);
229 iicoc_detach(device_t dev)
231 bus_generic_detach(dev);
237 iicoc_start(device_t dev, u_char slave, int timeout)
239 int error = IIC_EBUSBSY;
240 struct iicoc_softc *sc;
242 sc = device_get_softc(dev);
243 mtx_lock(&sc->sc_mtx);
244 sc->i2cdev_addr = (slave >> 1);
246 /* Verify the bus is idle */
247 if (iicoc_wait_on_status(dev, OC_STATUS_BUSY) < 0)
250 /* Write Slave Address */
251 if (iicoc_wr_ack_cmd(dev, slave, OC_COMMAND_START)) {
253 "I2C write slave address [0x%x] failed.\n", slave);
258 /* Verify Arbitration is not Lost */
259 if (iicoc_dev_read(dev, OC_I2C_STATUS_REG) & OC_STATUS_AL) {
260 device_printf(dev, "I2C Bus Arbitration Lost, Aborting.\n");
265 mtx_unlock(&sc->sc_mtx);
268 iicoc_dev_write(dev, OC_I2C_CMD_REG, OC_COMMAND_STOP);
269 iicoc_wait_on_status(dev, OC_STATUS_BUSY); /* wait for idle */
270 mtx_unlock(&sc->sc_mtx);
275 iicoc_stop(device_t dev)
278 struct iicoc_softc *sc;
280 sc = device_get_softc(dev);
281 mtx_lock(&sc->sc_mtx);
282 iicoc_dev_write(dev, OC_I2C_CMD_REG, OC_COMMAND_STOP);
283 iicoc_wait_on_status(dev, OC_STATUS_BUSY); /* wait for idle */
284 mtx_unlock(&sc->sc_mtx);
290 iicoc_write(device_t dev, const char *buf, int len,
291 int *sent, int timeout /* us */ )
297 /* Write Slave Offset */
298 if (iicoc_wr_ack_cmd(dev, value, OC_COMMAND_WRITE)) {
299 device_printf(dev, "I2C write slave offset failed.\n");
303 for (i = 1; i < len; i++) {
304 /* Write data byte */
306 if (iicoc_wr_cmd(dev, value, OC_COMMAND_WRITE)) {
307 device_printf(dev, "I2C write data byte %d failed.\n",
316 return (IIC_EBUSERR);
320 iicoc_read(device_t dev, char *buf, int len, int *read, int last,
326 for (i = 0; i < len; i++) {
328 cmd = (i == len - 1) ? OC_COMMAND_RDNACK : OC_COMMAND_READ;
329 data = iicoc_rd_cmd(dev, cmd);
332 "I2C read data byte %d failed.\n", i);
335 buf[i] = (uint8_t)data;
342 return (IIC_EBUSERR);
346 iicoc_reset(device_t dev, u_char speed, u_char addr, u_char *oldadr)
349 struct iicoc_softc *sc;
351 sc = device_get_softc(dev);
352 mtx_lock(&sc->sc_mtx);
353 error = iicoc_init(dev);
354 mtx_unlock(&sc->sc_mtx);
359 iicoc_repeated_start(device_t dev, u_char slave, int timeout)
364 static device_method_t iicoc_methods[] = {
365 /* device interface */
366 DEVMETHOD(device_probe, iicoc_probe),
367 DEVMETHOD(device_attach, iicoc_attach),
368 DEVMETHOD(device_detach, iicoc_detach),
370 /* iicbus interface */
371 DEVMETHOD(iicbus_callback, iicbus_null_callback),
372 DEVMETHOD(iicbus_repeated_start, iicoc_repeated_start),
373 DEVMETHOD(iicbus_start, iicoc_start),
374 DEVMETHOD(iicbus_stop, iicoc_stop),
375 DEVMETHOD(iicbus_reset, iicoc_reset),
376 DEVMETHOD(iicbus_write, iicoc_write),
377 DEVMETHOD(iicbus_read, iicoc_read),
378 DEVMETHOD(iicbus_transfer, iicbus_transfer_gen),
383 static driver_t iicoc_driver = {
386 sizeof(struct iicoc_softc),
389 DRIVER_MODULE(iicoc, pci, iicoc_driver, iicoc_devclass, 0, 0);
390 DRIVER_MODULE(iicbus, iicoc, iicbus_driver, iicbus_devclass, 0, 0);