FreeBSD/Linux Kernel Cross Reference
sys/dev/iicbus/iicoc.c
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/bus.h>
37 #include <sys/kernel.h>
38 #include <sys/lock.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/rman.h>
42
43 #include <machine/bus.h>
44
45 #include <dev/iicbus/iicbus.h>
46 #include <dev/iicbus/iiconf.h>
47
48 #include "iicbus_if.h"
49 #include "iicoc.h"
50
51 DRIVER_MODULE(iicbus, iicoc, iicbus_driver, 0, 0);
52
53 static void
54 iicoc_dev_write(device_t dev, int reg, int value)
55 {
56 struct iicoc_softc *sc;
57
58 sc = device_get_softc(dev);
59 bus_write_1(sc->mem_res, reg<<sc->reg_shift, value);
60 }
61
62 static int
63 iicoc_dev_read(device_t dev, int reg)
64 {
65 uint8_t val;
66 struct iicoc_softc *sc;
67
68 sc = device_get_softc(dev);
69 val = bus_read_1(sc->mem_res, reg<<sc->reg_shift);
70 return (val);
71 }
72
73 static int
74 iicoc_wait_on_status(device_t dev, uint8_t bit)
75 {
76 int tries = I2C_TIMEOUT;
77 uint8_t status;
78
79 do {
80 status = iicoc_dev_read(dev, OC_I2C_STATUS_REG);
81 } while ((status & bit) != 0 && --tries > 0);
82
83 return (tries == 0 ? -1: 0);
84 }
85
86 static int
87 iicoc_rd_cmd(device_t dev, uint8_t cmd)
88 {
89 uint8_t data;
90
91 iicoc_dev_write(dev, OC_I2C_CMD_REG, cmd);
92 if (iicoc_wait_on_status(dev, OC_STATUS_TIP) < 0) {
93 device_printf(dev, "read: Timeout waiting for TIP clear.\n");
94 return (-1);
95 }
96 data = iicoc_dev_read(dev, OC_I2C_DATA_REG);
97 return (data);
98 }
99
100 static int
101 iicoc_wr_cmd(device_t dev, uint8_t data, uint8_t cmd)
102 {
103
104 iicoc_dev_write(dev, OC_I2C_DATA_REG, data);
105 iicoc_dev_write(dev, OC_I2C_CMD_REG, cmd);
106 if (iicoc_wait_on_status(dev, OC_STATUS_TIP) < 0) {
107 device_printf(dev, "write: Timeout waiting for TIP clear.\n");
108 return (-1);
109 }
110 return (0);
111 }
112
113 static int
114 iicoc_wr_ack_cmd(device_t dev, uint8_t data, uint8_t cmd)
115 {
116
117 if (iicoc_wr_cmd(dev, data, cmd) < 0)
118 return (-1);
119
120 if (iicoc_dev_read(dev, OC_I2C_STATUS_REG) & OC_STATUS_NACK) {
121 device_printf(dev, "write: I2C command ACK Error.\n");
122 return (IIC_ENOACK);
123 }
124 return (0);
125 }
126
127 int
128 iicoc_init(device_t dev)
129 {
130 struct iicoc_softc *sc;
131 int value;
132
133 sc = device_get_softc(dev);
134 value = iicoc_dev_read(dev, OC_I2C_CTRL_REG);
135 iicoc_dev_write(dev, OC_I2C_CTRL_REG,
136 value & ~(OC_CONTROL_EN | OC_CONTROL_IEN));
137 value = (sc->clockfreq/(5 * sc->i2cfreq)) - 1;
138 iicoc_dev_write(dev, OC_I2C_PRESCALE_LO_REG, value & 0xff);
139 iicoc_dev_write(dev, OC_I2C_PRESCALE_HI_REG, value >> 8);
140 value = iicoc_dev_read(dev, OC_I2C_CTRL_REG);
141 iicoc_dev_write(dev, OC_I2C_CTRL_REG, value | OC_CONTROL_EN);
142
143 value = iicoc_dev_read(dev, OC_I2C_CTRL_REG);
144 /* return 0 on success, 1 on error */
145 return ((value & OC_CONTROL_EN) == 0);
146 }
147
148 static int
149 iicoc_iicbus_start_common(device_t dev, u_char slave, int timeout, bool repeat)
150 {
151 int error = IIC_EBUSERR;
152 struct iicoc_softc *sc;
153
154 sc = device_get_softc(dev);
155 mtx_lock(&sc->sc_mtx);
156 sc->i2cdev_addr = (slave >> 1);
157
158 /* Verify the bus is idle */
159 if (!repeat && iicoc_wait_on_status(dev, OC_STATUS_BUSY) < 0)
160 goto i2c_stx_error;
161
162 /* Write Slave Address */
163 if (iicoc_wr_ack_cmd(dev, slave, OC_COMMAND_START)) {
164 device_printf(dev,
165 "I2C write slave address [0x%x] failed.\n", slave);
166 error = IIC_ENOACK;
167 goto i2c_stx_error;
168 }
169
170 /* Verify Arbitration is not Lost */
171 if (iicoc_dev_read(dev, OC_I2C_STATUS_REG) & OC_STATUS_AL) {
172 device_printf(dev, "I2C Bus Arbitration Lost, Aborting.\n");
173 error = IIC_EBUSERR;
174 goto i2c_stx_error;
175 }
176 error = IIC_NOERR;
177 mtx_unlock(&sc->sc_mtx);
178 return (error);
179
180 i2c_stx_error:
181 iicoc_dev_write(dev, OC_I2C_CMD_REG, OC_COMMAND_STOP);
182 iicoc_wait_on_status(dev, OC_STATUS_BUSY); /* wait for idle */
183 mtx_unlock(&sc->sc_mtx);
184 return (error);
185 }
186
187 int
188 iicoc_iicbus_start(device_t dev, u_char slave, int timeout)
189 {
190
191 return (iicoc_iicbus_start_common(dev, slave, timeout, false));
192 }
193
194 int
195 iicoc_iicbus_repeated_start(device_t dev, u_char slave, int timeout)
196 {
197
198 return (iicoc_iicbus_start_common(dev, slave, timeout, true));
199 }
200
201 int
202 iicoc_iicbus_stop(device_t dev)
203 {
204 int error = 0;
205 struct iicoc_softc *sc;
206
207 sc = device_get_softc(dev);
208 mtx_lock(&sc->sc_mtx);
209 iicoc_dev_write(dev, OC_I2C_CMD_REG, OC_COMMAND_STOP);
210 iicoc_wait_on_status(dev, OC_STATUS_BUSY); /* wait for idle */
211 mtx_unlock(&sc->sc_mtx);
212 return (error);
213 }
214
215 int
216 iicoc_iicbus_write(device_t dev, const char *buf, int len, int *sent,
217 int timeout)
218 {
219 uint8_t value;
220 int i;
221
222 value = buf[0];
223 /* Write Slave Offset */
224 if (iicoc_wr_ack_cmd(dev, value, OC_COMMAND_WRITE)) {
225 device_printf(dev, "I2C write slave offset failed.\n");
226 goto i2c_tx_error;
227 }
228
229 for (i = 1; i < len; i++) {
230 /* Write data byte */
231 value = buf[i];
232 if (iicoc_wr_cmd(dev, value, OC_COMMAND_WRITE)) {
233 device_printf(dev, "I2C write data byte %d failed.\n",
234 i);
235 goto i2c_tx_error;
236 }
237 }
238 *sent = len;
239 return (IIC_NOERR);
240
241 i2c_tx_error:
242 return (IIC_EBUSERR);
243 }
244
245 int
246 iicoc_iicbus_read(device_t dev, char *buf, int len, int *read, int last,
247 int delay)
248 {
249 int data, i;
250 uint8_t cmd;
251
252 for (i = 0; i < len; i++) {
253 /* Read data byte */
254 cmd = (i == len - 1) ? OC_COMMAND_RDNACK : OC_COMMAND_READ;
255 data = iicoc_rd_cmd(dev, cmd);
256 if (data < 0) {
257 device_printf(dev,
258 "I2C read data byte %d failed.\n", i);
259 goto i2c_rx_error;
260 }
261 buf[i] = (uint8_t)data;
262 }
263
264 *read = len;
265 return (IIC_NOERR);
266
267 i2c_rx_error:
268 return (IIC_EBUSERR);
269 }
270
271 int
272 iicoc_iicbus_reset(device_t dev, u_char speed, u_char addr, u_char *oldadr)
273 {
274 int error;
275 struct iicoc_softc *sc;
276
277 sc = device_get_softc(dev);
278 mtx_lock(&sc->sc_mtx);
279 error = iicoc_init(dev);
280 mtx_unlock(&sc->sc_mtx);
281 return (error);
282 }
Cache object: b894e33ea3eb021d9f484b1c07c49e63
|