tegra_i2c.c revision 1.1 1 1.1 jmcneill /* $NetBSD: tegra_i2c.c,v 1.1 2015/05/10 23:50:21 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include "locators.h"
30 1.1 jmcneill
31 1.1 jmcneill #include <sys/cdefs.h>
32 1.1 jmcneill __KERNEL_RCSID(0, "$NetBSD: tegra_i2c.c,v 1.1 2015/05/10 23:50:21 jmcneill Exp $");
33 1.1 jmcneill
34 1.1 jmcneill #include <sys/param.h>
35 1.1 jmcneill #include <sys/bus.h>
36 1.1 jmcneill #include <sys/device.h>
37 1.1 jmcneill #include <sys/intr.h>
38 1.1 jmcneill #include <sys/systm.h>
39 1.1 jmcneill #include <sys/kernel.h>
40 1.1 jmcneill
41 1.1 jmcneill #include <dev/i2c/i2cvar.h>
42 1.1 jmcneill
43 1.1 jmcneill #include <arm/nvidia/tegra_reg.h>
44 1.1 jmcneill #include <arm/nvidia/tegra_i2creg.h>
45 1.1 jmcneill #include <arm/nvidia/tegra_var.h>
46 1.1 jmcneill
47 1.1 jmcneill static int tegra_i2c_match(device_t, cfdata_t, void *);
48 1.1 jmcneill static void tegra_i2c_attach(device_t, device_t, void *);
49 1.1 jmcneill
50 1.1 jmcneill struct tegra_i2c_softc {
51 1.1 jmcneill device_t sc_dev;
52 1.1 jmcneill bus_space_tag_t sc_bst;
53 1.1 jmcneill bus_space_handle_t sc_bsh;
54 1.1 jmcneill void * sc_ih;
55 1.1 jmcneill
56 1.1 jmcneill struct i2c_controller sc_ic;
57 1.1 jmcneill kmutex_t sc_lock;
58 1.1 jmcneill kcondvar_t sc_cv;
59 1.1 jmcneill device_t sc_i2cdev;
60 1.1 jmcneill };
61 1.1 jmcneill
62 1.1 jmcneill static void tegra_i2c_init(struct tegra_i2c_softc *);
63 1.1 jmcneill static int tegra_i2c_intr(void *);
64 1.1 jmcneill
65 1.1 jmcneill static int tegra_i2c_acquire_bus(void *, int);
66 1.1 jmcneill static void tegra_i2c_release_bus(void *, int);
67 1.1 jmcneill static int tegra_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *,
68 1.1 jmcneill size_t, void *, size_t, int);
69 1.1 jmcneill
70 1.1 jmcneill static int tegra_i2c_wait(struct tegra_i2c_softc *, int);
71 1.1 jmcneill static int tegra_i2c_write(struct tegra_i2c_softc *, i2c_addr_t,
72 1.1 jmcneill const uint8_t *, size_t, int);
73 1.1 jmcneill static int tegra_i2c_read(struct tegra_i2c_softc *, i2c_addr_t, uint8_t *,
74 1.1 jmcneill size_t, int);
75 1.1 jmcneill
76 1.1 jmcneill CFATTACH_DECL_NEW(tegra_i2c, sizeof(struct tegra_i2c_softc),
77 1.1 jmcneill tegra_i2c_match, tegra_i2c_attach, NULL, NULL);
78 1.1 jmcneill
79 1.1 jmcneill #define I2C_WRITE(sc, reg, val) \
80 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
81 1.1 jmcneill #define I2C_READ(sc, reg) \
82 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
83 1.1 jmcneill #define I2C_SET_CLEAR(sc, reg, setval, clrval) \
84 1.1 jmcneill tegra_reg_set_clear((sc)->sc_bst, (sc)->sc_bsh, (reg), (setval), (clrval))
85 1.1 jmcneill
86 1.1 jmcneill static int
87 1.1 jmcneill tegra_i2c_match(device_t parent, cfdata_t cf, void *aux)
88 1.1 jmcneill {
89 1.1 jmcneill struct tegraio_attach_args * const tio = aux;
90 1.1 jmcneill const struct tegra_locators * const loc = &tio->tio_loc;
91 1.1 jmcneill
92 1.1 jmcneill if (loc->loc_port == TEGRAIOCF_PORT_DEFAULT)
93 1.1 jmcneill return 0;
94 1.1 jmcneill
95 1.1 jmcneill return 1;
96 1.1 jmcneill }
97 1.1 jmcneill
98 1.1 jmcneill static void
99 1.1 jmcneill tegra_i2c_attach(device_t parent, device_t self, void *aux)
100 1.1 jmcneill {
101 1.1 jmcneill struct tegra_i2c_softc * const sc = device_private(self);
102 1.1 jmcneill struct tegraio_attach_args * const tio = aux;
103 1.1 jmcneill const struct tegra_locators * const loc = &tio->tio_loc;
104 1.1 jmcneill struct i2cbus_attach_args iba;
105 1.1 jmcneill
106 1.1 jmcneill sc->sc_dev = self;
107 1.1 jmcneill sc->sc_bst = tio->tio_bst;
108 1.1 jmcneill bus_space_subregion(tio->tio_bst, tio->tio_bsh,
109 1.1 jmcneill loc->loc_offset, loc->loc_size, &sc->sc_bsh);
110 1.1 jmcneill mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
111 1.1 jmcneill cv_init(&sc->sc_cv, device_xname(self));
112 1.1 jmcneill
113 1.1 jmcneill aprint_naive("\n");
114 1.1 jmcneill aprint_normal(": I2C%d\n", loc->loc_port + 1);
115 1.1 jmcneill
116 1.1 jmcneill sc->sc_ih = intr_establish(loc->loc_intr, IPL_VM, IST_LEVEL|IST_MPSAFE,
117 1.1 jmcneill tegra_i2c_intr, sc);
118 1.1 jmcneill if (sc->sc_ih == NULL) {
119 1.1 jmcneill aprint_error_dev(self, "couldn't establish interrupt %d\n",
120 1.1 jmcneill loc->loc_intr);
121 1.1 jmcneill return;
122 1.1 jmcneill }
123 1.1 jmcneill aprint_normal_dev(self, "interrupting on irq %d\n", loc->loc_intr);
124 1.1 jmcneill
125 1.1 jmcneill /* Recommended setting for standard mode */
126 1.1 jmcneill tegra_car_periph_i2c_enable(loc->loc_port, 204000000);
127 1.1 jmcneill
128 1.1 jmcneill tegra_i2c_init(sc);
129 1.1 jmcneill
130 1.1 jmcneill sc->sc_ic.ic_cookie = sc;
131 1.1 jmcneill sc->sc_ic.ic_acquire_bus = tegra_i2c_acquire_bus;
132 1.1 jmcneill sc->sc_ic.ic_release_bus = tegra_i2c_release_bus;
133 1.1 jmcneill sc->sc_ic.ic_exec = tegra_i2c_exec;
134 1.1 jmcneill
135 1.1 jmcneill iba.iba_tag = &sc->sc_ic;
136 1.1 jmcneill sc->sc_i2cdev = config_found_ia(self, "i2cbus", &iba, iicbus_print);
137 1.1 jmcneill }
138 1.1 jmcneill
139 1.1 jmcneill static void
140 1.1 jmcneill tegra_i2c_init(struct tegra_i2c_softc *sc)
141 1.1 jmcneill {
142 1.1 jmcneill I2C_WRITE(sc, I2C_CLK_DIVISOR_REG,
143 1.1 jmcneill __SHIFTIN(0x19, I2C_CLK_DIVISOR_STD_FAST_MODE) |
144 1.1 jmcneill __SHIFTIN(0x1, I2C_CLK_DIVISOR_HSMODE));
145 1.1 jmcneill
146 1.1 jmcneill I2C_WRITE(sc, I2C_INTERRUPT_MASK_REG, 0);
147 1.1 jmcneill I2C_WRITE(sc, I2C_CNFG_REG, I2C_CNFG_NEW_MASTER_FSM);
148 1.1 jmcneill I2C_SET_CLEAR(sc, I2C_SL_CNFG_REG, I2C_SL_CNFG_NEWSL, 0);
149 1.1 jmcneill }
150 1.1 jmcneill
151 1.1 jmcneill static int
152 1.1 jmcneill tegra_i2c_intr(void *priv)
153 1.1 jmcneill {
154 1.1 jmcneill struct tegra_i2c_softc * const sc = priv;
155 1.1 jmcneill
156 1.1 jmcneill const uint32_t istatus = I2C_READ(sc, I2C_INTERRUPT_STATUS_REG);
157 1.1 jmcneill if (istatus == 0)
158 1.1 jmcneill return 0;
159 1.1 jmcneill I2C_WRITE(sc, I2C_INTERRUPT_STATUS_REG, istatus);
160 1.1 jmcneill
161 1.1 jmcneill mutex_enter(&sc->sc_lock);
162 1.1 jmcneill cv_broadcast(&sc->sc_cv);
163 1.1 jmcneill mutex_exit(&sc->sc_lock);
164 1.1 jmcneill
165 1.1 jmcneill return 1;
166 1.1 jmcneill }
167 1.1 jmcneill
168 1.1 jmcneill static int
169 1.1 jmcneill tegra_i2c_acquire_bus(void *priv, int flags)
170 1.1 jmcneill {
171 1.1 jmcneill struct tegra_i2c_softc * const sc = priv;
172 1.1 jmcneill
173 1.1 jmcneill mutex_enter(&sc->sc_lock);
174 1.1 jmcneill
175 1.1 jmcneill return 0;
176 1.1 jmcneill }
177 1.1 jmcneill
178 1.1 jmcneill static void
179 1.1 jmcneill tegra_i2c_release_bus(void *priv, int flags)
180 1.1 jmcneill {
181 1.1 jmcneill struct tegra_i2c_softc * const sc = priv;
182 1.1 jmcneill
183 1.1 jmcneill mutex_exit(&sc->sc_lock);
184 1.1 jmcneill }
185 1.1 jmcneill
186 1.1 jmcneill static int
187 1.1 jmcneill tegra_i2c_exec(void *priv, i2c_op_t op, i2c_addr_t addr, const void *cmdbuf,
188 1.1 jmcneill size_t cmdlen, void *buf, size_t buflen, int flags)
189 1.1 jmcneill {
190 1.1 jmcneill struct tegra_i2c_softc * const sc = priv;
191 1.1 jmcneill int retry, error;
192 1.1 jmcneill
193 1.1 jmcneill #if notyet
194 1.1 jmcneill if (cold)
195 1.1 jmcneill #endif
196 1.1 jmcneill flags |= I2C_F_POLL;
197 1.1 jmcneill
198 1.1 jmcneill KASSERT(mutex_owned(&sc->sc_lock));
199 1.1 jmcneill
200 1.1 jmcneill if ((flags & I2C_F_POLL) == 0) {
201 1.1 jmcneill I2C_WRITE(sc, I2C_INTERRUPT_MASK_REG,
202 1.1 jmcneill I2C_INTERRUPT_MASK_NOACK | I2C_INTERRUPT_MASK_ARB_LOST |
203 1.1 jmcneill I2C_INTERRUPT_MASK_TIMEOUT |
204 1.1 jmcneill I2C_INTERRUPT_MASK_ALL_PACKETS_XFER_COMPLETE);
205 1.1 jmcneill }
206 1.1 jmcneill
207 1.1 jmcneill const uint32_t flush_mask =
208 1.1 jmcneill I2C_FIFO_CONTROL_TX_FIFO_FLUSH | I2C_FIFO_CONTROL_RX_FIFO_FLUSH;
209 1.1 jmcneill
210 1.1 jmcneill I2C_SET_CLEAR(sc, I2C_FIFO_CONTROL_REG, flush_mask, 0);
211 1.1 jmcneill for (retry = 10000; retry > 0; retry--) {
212 1.1 jmcneill const uint32_t v = I2C_READ(sc, I2C_FIFO_CONTROL_REG);
213 1.1 jmcneill if ((v & flush_mask) == 0)
214 1.1 jmcneill break;
215 1.1 jmcneill delay(1);
216 1.1 jmcneill }
217 1.1 jmcneill if (retry == 0) {
218 1.1 jmcneill device_printf(sc->sc_dev, "timeout flushing FIFO\n");
219 1.1 jmcneill return EIO;
220 1.1 jmcneill }
221 1.1 jmcneill
222 1.1 jmcneill if (cmdlen > 0) {
223 1.1 jmcneill error = tegra_i2c_write(sc, addr, cmdbuf, cmdlen, flags);
224 1.1 jmcneill if (error) {
225 1.1 jmcneill goto done;
226 1.1 jmcneill }
227 1.1 jmcneill }
228 1.1 jmcneill
229 1.1 jmcneill if (I2C_OP_READ_P(op)) {
230 1.1 jmcneill error = tegra_i2c_read(sc, addr, buf, buflen, flags);
231 1.1 jmcneill } else {
232 1.1 jmcneill error = tegra_i2c_write(sc, addr, buf, buflen, flags);
233 1.1 jmcneill }
234 1.1 jmcneill
235 1.1 jmcneill done:
236 1.1 jmcneill if ((flags & I2C_F_POLL) == 0) {
237 1.1 jmcneill I2C_WRITE(sc, I2C_INTERRUPT_MASK_REG, 0);
238 1.1 jmcneill }
239 1.1 jmcneill return error;
240 1.1 jmcneill }
241 1.1 jmcneill
242 1.1 jmcneill static int
243 1.1 jmcneill tegra_i2c_wait(struct tegra_i2c_softc *sc, int flags)
244 1.1 jmcneill {
245 1.1 jmcneill const struct timeval timeout = { .tv_sec = 1, .tv_usec = 0 };
246 1.1 jmcneill struct timeval tnow, tend;
247 1.1 jmcneill uint32_t stat;
248 1.1 jmcneill int error;
249 1.1 jmcneill
250 1.1 jmcneill getmicrotime(&tnow);
251 1.1 jmcneill timeradd(&tnow, &timeout, &tend);
252 1.1 jmcneill
253 1.1 jmcneill for (;;) {
254 1.1 jmcneill getmicrotime(&tnow);
255 1.1 jmcneill if (timercmp(&tnow, &tend, >=)) {
256 1.1 jmcneill return ETIMEDOUT;
257 1.1 jmcneill }
258 1.1 jmcneill if ((flags & I2C_F_POLL) == 0) {
259 1.1 jmcneill struct timeval trem;
260 1.1 jmcneill timersub(&tend, &tnow, &trem);
261 1.1 jmcneill const u_int ms = (trem.tv_sec * 1000) +
262 1.1 jmcneill (trem.tv_usec / 1000);
263 1.1 jmcneill KASSERT(ms > 0);
264 1.1 jmcneill error = cv_timedwait_sig(&sc->sc_cv, &sc->sc_lock,
265 1.1 jmcneill max(mstohz(ms), 1));
266 1.1 jmcneill if (error) {
267 1.1 jmcneill return error;
268 1.1 jmcneill }
269 1.1 jmcneill }
270 1.1 jmcneill stat = I2C_READ(sc, I2C_STATUS_REG);
271 1.1 jmcneill if ((stat & I2C_STATUS_BUSY) == 0) {
272 1.1 jmcneill break;
273 1.1 jmcneill }
274 1.1 jmcneill if (flags & I2C_F_POLL) {
275 1.1 jmcneill delay(1);
276 1.1 jmcneill }
277 1.1 jmcneill }
278 1.1 jmcneill
279 1.1 jmcneill
280 1.1 jmcneill if (__SHIFTOUT(stat, I2C_STATUS_CMD1_STAT) != 0)
281 1.1 jmcneill return EIO;
282 1.1 jmcneill
283 1.1 jmcneill return 0;
284 1.1 jmcneill }
285 1.1 jmcneill
286 1.1 jmcneill static int
287 1.1 jmcneill tegra_i2c_write(struct tegra_i2c_softc *sc, i2c_addr_t addr, const uint8_t *buf,
288 1.1 jmcneill size_t buflen, int flags)
289 1.1 jmcneill {
290 1.1 jmcneill uint32_t data, cnfg;
291 1.1 jmcneill size_t n;
292 1.1 jmcneill
293 1.1 jmcneill if (buflen > 4)
294 1.1 jmcneill return EINVAL;
295 1.1 jmcneill
296 1.1 jmcneill I2C_WRITE(sc, I2C_CMD_ADDR0_REG, addr << 1);
297 1.1 jmcneill for (n = 0, data = 0; n < buflen; n++) {
298 1.1 jmcneill data |= (uint32_t)buf[n] << (n * 8);
299 1.1 jmcneill }
300 1.1 jmcneill I2C_WRITE(sc, I2C_CMD_DATA1_REG, data);
301 1.1 jmcneill
302 1.1 jmcneill cnfg = I2C_READ(sc, I2C_CNFG_REG);
303 1.1 jmcneill cnfg &= ~I2C_CNFG_DEBOUNCE_CNT;
304 1.1 jmcneill cnfg |= __SHIFTIN(2, I2C_CNFG_DEBOUNCE_CNT);
305 1.1 jmcneill cnfg &= ~I2C_CNFG_LENGTH;
306 1.1 jmcneill cnfg |= __SHIFTIN(buflen - 1, I2C_CNFG_LENGTH);
307 1.1 jmcneill cnfg &= ~I2C_CNFG_SLV2;
308 1.1 jmcneill cnfg &= ~I2C_CNFG_CMD1;
309 1.1 jmcneill cnfg &= ~I2C_CNFG_NOACK;
310 1.1 jmcneill cnfg &= ~I2C_CNFG_A_MOD;
311 1.1 jmcneill I2C_WRITE(sc, I2C_CNFG_REG, cnfg);
312 1.1 jmcneill
313 1.1 jmcneill I2C_SET_CLEAR(sc, I2C_BUS_CONFIG_LOAD_REG,
314 1.1 jmcneill I2C_BUS_CONFIG_LOAD_MSTR_CONFIG_LOAD, 0);
315 1.1 jmcneill
316 1.1 jmcneill I2C_SET_CLEAR(sc, I2C_CNFG_REG, I2C_CNFG_SEND, 0);
317 1.1 jmcneill
318 1.1 jmcneill return tegra_i2c_wait(sc, flags);
319 1.1 jmcneill }
320 1.1 jmcneill
321 1.1 jmcneill static int
322 1.1 jmcneill tegra_i2c_read(struct tegra_i2c_softc *sc, i2c_addr_t addr, uint8_t *buf,
323 1.1 jmcneill size_t buflen, int flags)
324 1.1 jmcneill {
325 1.1 jmcneill uint32_t data, cnfg;
326 1.1 jmcneill int error;
327 1.1 jmcneill size_t n;
328 1.1 jmcneill
329 1.1 jmcneill if (buflen > 4)
330 1.1 jmcneill return EINVAL;
331 1.1 jmcneill
332 1.1 jmcneill I2C_WRITE(sc, I2C_CMD_ADDR0_REG, (addr << 1) | 1);
333 1.1 jmcneill cnfg = I2C_READ(sc, I2C_CNFG_REG);
334 1.1 jmcneill cnfg &= ~I2C_CNFG_SLV2;
335 1.1 jmcneill cnfg |= I2C_CNFG_CMD1;
336 1.1 jmcneill cnfg &= ~I2C_CNFG_LENGTH;
337 1.1 jmcneill cnfg |= __SHIFTIN(buflen - 1, I2C_CNFG_LENGTH);
338 1.1 jmcneill I2C_WRITE(sc, I2C_CNFG_REG, cnfg);
339 1.1 jmcneill
340 1.1 jmcneill I2C_SET_CLEAR(sc, I2C_CNFG_REG, I2C_CNFG_SEND, 0);
341 1.1 jmcneill
342 1.1 jmcneill error = tegra_i2c_wait(sc, flags);
343 1.1 jmcneill if (error)
344 1.1 jmcneill return error;
345 1.1 jmcneill
346 1.1 jmcneill data = I2C_READ(sc, I2C_CMD_DATA1_REG);
347 1.1 jmcneill for (n = 0; n < buflen; n++) {
348 1.1 jmcneill buf[n] = (data >> (n * 8)) & 0xff;
349 1.1 jmcneill }
350 1.1 jmcneill
351 1.1 jmcneill return 0;
352 1.1 jmcneill }
353