1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2003-2012 Broadcom Corporation
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
15 * the documentation and/or other materials provided with the
16 * distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY BROADCOM ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL BROADCOM OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
25 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
28 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/lock.h>
38 #include <sys/module.h>
39 #include <sys/mutex.h>
40 #include <sys/bus.h>
41 #include <sys/rman.h>
42
43 #include <machine/bus.h>
44
45 #include <dev/iicbus/iiconf.h>
46 #include <dev/iicbus/iicbus.h>
47 #include <dev/iicbus/iicoc.h>
48
49 #include <dev/pci/pcireg.h>
50 #include <dev/pci/pcivar.h>
51
52 #include "iicbus_if.h"
53
54 static devclass_t iicoc_devclass;
55
56 /*
57 * Device methods
58 */
59 static int iicoc_probe(device_t);
60 static int iicoc_attach(device_t);
61 static int iicoc_detach(device_t);
62
63 static int iicoc_start(device_t dev, u_char slave, int timeout);
64 static int iicoc_stop(device_t dev);
65 static int iicoc_read(device_t dev, char *buf,
66 int len, int *read, int last, int delay);
67 static int iicoc_write(device_t dev, const char *buf,
68 int len, int *sent, int timeout);
69 static int iicoc_repeated_start(device_t dev, u_char slave, int timeout);
70
71 struct iicoc_softc {
72 device_t dev; /* Self */
73 u_int reg_shift; /* Chip specific */
74 u_int clockfreq;
75 u_int i2cfreq;
76 struct resource *mem_res; /* Memory resource */
77 int mem_rid;
78 int sc_started;
79 uint8_t i2cdev_addr;
80 device_t iicbus;
81 struct mtx sc_mtx;
82 };
83
84 static void
iicoc_dev_write(device_t dev,int reg,int value)85 iicoc_dev_write(device_t dev, int reg, int value)
86 {
87 struct iicoc_softc *sc;
88
89 sc = device_get_softc(dev);
90 bus_write_1(sc->mem_res, reg<<sc->reg_shift, value);
91 }
92
93 static int
iicoc_dev_read(device_t dev,int reg)94 iicoc_dev_read(device_t dev, int reg)
95 {
96 uint8_t val;
97 struct iicoc_softc *sc;
98
99 sc = device_get_softc(dev);
100 val = bus_read_1(sc->mem_res, reg<<sc->reg_shift);
101 return (val);
102 }
103
104 static int
iicoc_wait_on_status(device_t dev,uint8_t bit)105 iicoc_wait_on_status(device_t dev, uint8_t bit)
106 {
107 int tries = I2C_TIMEOUT;
108 uint8_t status;
109
110 do {
111 status = iicoc_dev_read(dev, OC_I2C_STATUS_REG);
112 } while ((status & bit) != 0 && --tries > 0);
113
114 return (tries == 0 ? -1: 0);
115 }
116
117 static int
iicoc_rd_cmd(device_t dev,uint8_t cmd)118 iicoc_rd_cmd(device_t dev, uint8_t cmd)
119 {
120 uint8_t data;
121
122 iicoc_dev_write(dev, OC_I2C_CMD_REG, cmd);
123 if (iicoc_wait_on_status(dev, OC_STATUS_TIP) < 0) {
124 device_printf(dev, "read: Timeout waiting for TIP clear.\n");
125 return (-1);
126 }
127 data = iicoc_dev_read(dev, OC_I2C_DATA_REG);
128 return (data);
129 }
130
131 static int
iicoc_wr_cmd(device_t dev,uint8_t data,uint8_t cmd)132 iicoc_wr_cmd(device_t dev, uint8_t data, uint8_t cmd)
133 {
134
135 iicoc_dev_write(dev, OC_I2C_DATA_REG, data);
136 iicoc_dev_write(dev, OC_I2C_CMD_REG, cmd);
137 if (iicoc_wait_on_status(dev, OC_STATUS_TIP) < 0) {
138 device_printf(dev, "write: Timeout waiting for TIP clear.\n");
139 return (-1);
140 }
141 return (0);
142 }
143
144 static int
iicoc_wr_ack_cmd(device_t dev,uint8_t data,uint8_t cmd)145 iicoc_wr_ack_cmd(device_t dev, uint8_t data, uint8_t cmd)
146 {
147 if (iicoc_wr_cmd(dev, data, cmd) < 0)
148 return (-1);
149
150 if (iicoc_dev_read(dev, OC_I2C_STATUS_REG) & OC_STATUS_NACK) {
151 device_printf(dev, "write: I2C command ACK Error.\n");
152 return (IIC_ENOACK);
153 }
154 return (0);
155 }
156
157 static int
iicoc_init(device_t dev)158 iicoc_init(device_t dev)
159 {
160 struct iicoc_softc *sc;
161 int value;
162
163 sc = device_get_softc(dev);
164 value = iicoc_dev_read(dev, OC_I2C_CTRL_REG);
165 iicoc_dev_write(dev, OC_I2C_CTRL_REG,
166 value & ~(OC_CONTROL_EN | OC_CONTROL_IEN));
167 value = (sc->clockfreq/(5 * sc->i2cfreq)) - 1;
168 iicoc_dev_write(dev, OC_I2C_PRESCALE_LO_REG, value & 0xff);
169 iicoc_dev_write(dev, OC_I2C_PRESCALE_HI_REG, value >> 8);
170 value = iicoc_dev_read(dev, OC_I2C_CTRL_REG);
171 iicoc_dev_write(dev, OC_I2C_CTRL_REG, value | OC_CONTROL_EN);
172
173 value = iicoc_dev_read(dev, OC_I2C_CTRL_REG);
174 /* return 0 on success, 1 on error */
175 return ((value & OC_CONTROL_EN) == 0);
176 }
177
178 static int
iicoc_probe(device_t dev)179 iicoc_probe(device_t dev)
180 {
181 struct iicoc_softc *sc;
182
183 sc = device_get_softc(dev);
184 if ((pci_get_vendor(dev) == 0x184e) &&
185 (pci_get_device(dev) == 0x1011)) {
186 sc->clockfreq = XLP_I2C_CLKFREQ;
187 sc->i2cfreq = XLP_I2C_FREQ;
188 sc->reg_shift = 2;
189 device_set_desc(dev, "Netlogic XLP I2C Controller");
190 return (BUS_PROBE_DEFAULT);
191 }
192 return (ENXIO);
193 }
194
195
196 /*
197 * We add all the devices which we know about.
198 * The generic attach routine will attach them if they are alive.
199 */
200 static int
iicoc_attach(device_t dev)201 iicoc_attach(device_t dev)
202 {
203 int bus;
204 struct iicoc_softc *sc;
205
206 sc = device_get_softc(dev);
207 bus = device_get_unit(dev);
208
209 sc->dev = dev;
210 mtx_init(&sc->sc_mtx, "iicoc", "iicoc", MTX_DEF);
211 sc->mem_rid = 0;
212 sc->mem_res = bus_alloc_resource_anywhere(dev,
213 SYS_RES_MEMORY, &sc->mem_rid, 0x100, RF_ACTIVE);
214
215 if (sc->mem_res == NULL) {
216 device_printf(dev, "Could not allocate bus resource.\n");
217 return (-1);
218 }
219 iicoc_init(dev);
220 sc->iicbus = device_add_child(dev, "iicbus", -1);
221 if (sc->iicbus == NULL) {
222 device_printf(dev, "Could not allocate iicbus instance.\n");
223 return (-1);
224 }
225 bus_generic_attach(dev);
226
227 return (0);
228 }
229
230 static int
iicoc_detach(device_t dev)231 iicoc_detach(device_t dev)
232 {
233 bus_generic_detach(dev);
234 device_delete_children(dev);
235
236 return (0);
237 }
238
239 static int
iicoc_start(device_t dev,u_char slave,int timeout)240 iicoc_start(device_t dev, u_char slave, int timeout)
241 {
242 int error = IIC_EBUSERR;
243 struct iicoc_softc *sc;
244
245 sc = device_get_softc(dev);
246 mtx_lock(&sc->sc_mtx);
247 sc->i2cdev_addr = (slave >> 1);
248
249 /* Verify the bus is idle */
250 if (iicoc_wait_on_status(dev, OC_STATUS_BUSY) < 0)
251 goto i2c_stx_error;
252
253 /* Write Slave Address */
254 if (iicoc_wr_ack_cmd(dev, slave, OC_COMMAND_START)) {
255 device_printf(dev,
256 "I2C write slave address [0x%x] failed.\n", slave);
257 error = IIC_ENOACK;
258 goto i2c_stx_error;
259 }
260
261 /* Verify Arbitration is not Lost */
262 if (iicoc_dev_read(dev, OC_I2C_STATUS_REG) & OC_STATUS_AL) {
263 device_printf(dev, "I2C Bus Arbitration Lost, Aborting.\n");
264 error = IIC_EBUSERR;
265 goto i2c_stx_error;
266 }
267 error = IIC_NOERR;
268 mtx_unlock(&sc->sc_mtx);
269 return (error);
270 i2c_stx_error:
271 iicoc_dev_write(dev, OC_I2C_CMD_REG, OC_COMMAND_STOP);
272 iicoc_wait_on_status(dev, OC_STATUS_BUSY); /* wait for idle */
273 mtx_unlock(&sc->sc_mtx);
274 return (error);
275 }
276
277 static int
iicoc_stop(device_t dev)278 iicoc_stop(device_t dev)
279 {
280 int error = 0;
281 struct iicoc_softc *sc;
282
283 sc = device_get_softc(dev);
284 mtx_lock(&sc->sc_mtx);
285 iicoc_dev_write(dev, OC_I2C_CMD_REG, OC_COMMAND_STOP);
286 iicoc_wait_on_status(dev, OC_STATUS_BUSY); /* wait for idle */
287 mtx_unlock(&sc->sc_mtx);
288 return (error);
289
290 }
291
292 static int
iicoc_write(device_t dev,const char * buf,int len,int * sent,int timeout)293 iicoc_write(device_t dev, const char *buf, int len,
294 int *sent, int timeout /* us */ )
295 {
296 uint8_t value;
297 int i;
298
299 value = buf[0];
300 /* Write Slave Offset */
301 if (iicoc_wr_ack_cmd(dev, value, OC_COMMAND_WRITE)) {
302 device_printf(dev, "I2C write slave offset failed.\n");
303 goto i2c_tx_error;
304 }
305
306 for (i = 1; i < len; i++) {
307 /* Write data byte */
308 value = buf[i];
309 if (iicoc_wr_cmd(dev, value, OC_COMMAND_WRITE)) {
310 device_printf(dev, "I2C write data byte %d failed.\n",
311 i);
312 goto i2c_tx_error;
313 }
314 }
315 *sent = len;
316 return (IIC_NOERR);
317
318 i2c_tx_error:
319 return (IIC_EBUSERR);
320 }
321
322 static int
iicoc_read(device_t dev,char * buf,int len,int * read,int last,int delay)323 iicoc_read(device_t dev, char *buf, int len, int *read, int last,
324 int delay)
325 {
326 int data, i;
327 uint8_t cmd;
328
329 for (i = 0; i < len; i++) {
330 /* Read data byte */
331 cmd = (i == len - 1) ? OC_COMMAND_RDNACK : OC_COMMAND_READ;
332 data = iicoc_rd_cmd(dev, cmd);
333 if (data < 0) {
334 device_printf(dev,
335 "I2C read data byte %d failed.\n", i);
336 goto i2c_rx_error;
337 }
338 buf[i] = (uint8_t)data;
339 }
340
341 *read = len;
342 return (IIC_NOERR);
343
344 i2c_rx_error:
345 return (IIC_EBUSERR);
346 }
347
348 static int
iicoc_reset(device_t dev,u_char speed,u_char addr,u_char * oldadr)349 iicoc_reset(device_t dev, u_char speed, u_char addr, u_char *oldadr)
350 {
351 int error;
352 struct iicoc_softc *sc;
353
354 sc = device_get_softc(dev);
355 mtx_lock(&sc->sc_mtx);
356 error = iicoc_init(dev);
357 mtx_unlock(&sc->sc_mtx);
358 return (error);
359 }
360
361 static int
iicoc_repeated_start(device_t dev,u_char slave,int timeout)362 iicoc_repeated_start(device_t dev, u_char slave, int timeout)
363 {
364 return 0;
365 }
366
367 static device_method_t iicoc_methods[] = {
368 /* device interface */
369 DEVMETHOD(device_probe, iicoc_probe),
370 DEVMETHOD(device_attach, iicoc_attach),
371 DEVMETHOD(device_detach, iicoc_detach),
372
373 /* iicbus interface */
374 DEVMETHOD(iicbus_callback, iicbus_null_callback),
375 DEVMETHOD(iicbus_repeated_start, iicoc_repeated_start),
376 DEVMETHOD(iicbus_start, iicoc_start),
377 DEVMETHOD(iicbus_stop, iicoc_stop),
378 DEVMETHOD(iicbus_reset, iicoc_reset),
379 DEVMETHOD(iicbus_write, iicoc_write),
380 DEVMETHOD(iicbus_read, iicoc_read),
381 DEVMETHOD(iicbus_transfer, iicbus_transfer_gen),
382
383 DEVMETHOD_END
384 };
385
386 static driver_t iicoc_driver = {
387 "iicoc",
388 iicoc_methods,
389 sizeof(struct iicoc_softc),
390 };
391
392 DRIVER_MODULE(iicoc, pci, iicoc_driver, iicoc_devclass, 0, 0);
393 DRIVER_MODULE(iicbus, iicoc, iicbus_driver, iicbus_devclass, 0, 0);
394