Lines Matching +full:i2c +full:- +full:slave +full:- +full:addr

1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
5 * Copyright (c) 2024 Pierre-Luc Drouin <pldrouin@pldrouin.net>
30 * Vybrid Family Inter-Integrated Circuit (I2C)
62 #define I2C_IBAD 0x0 /* I2C Bus Address Register */
63 #define I2C_IBFD 0x1 /* I2C Bus Frequency Divider Register */
64 #define I2C_IBCR 0x2 /* I2C Bus Control Register */
66 #define IBCR_IBIE (1 << 6) /* I-Bus Interrupt Enable. */
67 #define IBCR_MSSL (1 << 5) /* Master/Slave mode select. */
72 #define I2C_IBSR 0x3 /* I2C Bus Status Register */
74 #define IBSR_IAAS (1 << 6) /* Addressed as a slave. */
77 #define IBSR_SRW (1 << 2) /* Slave Read/Write. */
78 #define IBSR_IBIF (1 << 1) /* I-Bus Interrupt Flag. */
80 #define I2C_IBDR 0x4 /* I2C Bus Data I/O Register */
81 #define I2C_IBIC 0x5 /* I2C Bus Interrupt Config Register */
83 #define I2C_IBDBG 0x6 /* I2C Bus Debug Register */
87 #define READ1(_sc, _reg) bus_space_read_1(_sc->bst, _sc->bsh, _reg)
88 #define WRITE1(_sc, _reg, _val) bus_space_write_1(_sc->bst,\
89 _sc->bsh, _reg, _val)
93 device_printf((_sc)->dev, fmt, ##args)
98 bus_space_write_1(_sc->bst, _sc->bsh, _reg, _val);\
102 uint32_t ret=bus_space_read_1(_sc->bst, _sc->bsh, _reg);\
126 { -1, 0 }
153 vf_i2c_dbg(sc, "i2c attach common\n");
155 mtx_init(&sc->mutex, device_get_nameunit(dev), "I2C", MTX_DEF);
157 error = bus_alloc_resources(dev, i2c_spec, sc->res);
159 mtx_destroy(&sc->mutex);
165 sc->bst = rman_get_bustag(sc->res[0]);
166 sc->bsh = rman_get_bushandle(sc->res[0]);
168 mtx_lock(&sc->mutex);
172 if (sc->freq == 0) {
178 sc->freq = UINT32_MAX;
181 device_printf(dev, "Bus frequency divider value appears unset, defaulting to low I2C bus speed.\n");
185 mtx_unlock(&sc->mutex);
187 sc->iicbus = device_add_child(dev, "iicbus", DEVICE_UNIT_ANY);
189 if (sc->iicbus == NULL) {
191 mtx_destroy(&sc->mutex);
192 bus_release_resources(dev, i2c_spec, sc->res);
208 vf_i2c_dbg(sc, "i2c detach\n");
216 mtx_lock(&sc->mutex);
218 if (sc->freq == 0) {
223 bus_release_resources(dev, i2c_spec, sc->res);
225 mtx_unlock(&sc->mutex);
227 mtx_destroy(&sc->mutex);
239 while (retry --) {
255 vf_i2c_dbg(sc, "i2c wait for transfer complete + interrupt flag\n");
258 while (retry --) {
281 vf_i2c_dbg(sc, "i2c get ACK bit from last write\n");
288 i2c_repeated_start(device_t dev, u_char slave, int timeout)
296 vf_i2c_dbg(sc, "i2c repeated start\n");
298 mtx_lock(&sc->mutex);
301 vf_i2c_dbg(sc, "cant i2c repeat start: bus is no longer busy\n");
302 mtx_unlock(&sc->mutex);
310 /* Write target address - LSB is R/W bit */
311 WRITE1(sc, I2C_IBDR, slave);
316 mtx_unlock(&sc->mutex);
317 vf_i2c_dbg(sc, "cant i2c repeat start: missing ACK after slave address\n");
321 mtx_unlock(&sc->mutex);
330 i2c_start(device_t dev, u_char slave, int timeout)
338 vf_i2c_dbg(sc, "i2c start\n");
340 mtx_lock(&sc->mutex);
353 mtx_unlock(&sc->mutex);
354 vf_i2c_dbg(sc, "cant i2c start: %i\n", error);
364 /* Write target address - LSB is R/W bit */
365 WRITE1(sc, I2C_IBDR, slave);
369 mtx_unlock(&sc->mutex);
370 vf_i2c_dbg(sc, "cant i2c start: iif error\n");
373 mtx_unlock(&sc->mutex);
376 vf_i2c_dbg(sc, "cant i2c start: missing ACK after slave address\n");
390 vf_i2c_dbg(sc, "i2c stop\n");
392 mtx_lock(&sc->mutex);
397 mtx_unlock(&sc->mutex);
410 if (sc->freq == UINT32_MAX)
413 div_reg = vf610_div_table[nitems(vf610_div_table) - 1].reg_val;
415 if (sc->hwtype == HW_MVF600)
417 else if (sc->freq == 0)
418 div_reg = vf610_div_table[nitems(vf610_div_table) - 1].reg_val;
423 error = clk_get_freq(sc->clock, &clk_freq);
427 div_reg = vf610_div_table[nitems(vf610_div_table) - 1].reg_val;
430 for (i = 0; i < nitems(vf610_div_table) - 1; i++)
431 if ((clk_freq / vf610_div_table[i].div) <= sc->freq)
442 i2c_reset(device_t dev, u_char speed, u_char addr, u_char *oldadr)
449 vf_i2c_dbg(sc, "i2c reset\n");
451 mtx_lock(&sc->mutex);
457 WRITE1(sc, I2C_IBCR, 0x0); /* Enable i2c */
459 mtx_unlock(&sc->mutex);
472 vf_i2c_dbg(sc, "i2c read\n");
476 mtx_lock(&sc->mutex);
490 mtx_unlock(&sc->mutex);
495 if (*read == len - 2) {
499 } else if (*read == len - 1) {
509 mtx_unlock(&sc->mutex);
522 vf_i2c_dbg(sc, "i2c write\n");
526 mtx_lock(&sc->mutex);
532 mtx_unlock(&sc->mutex);
536 if (!tx_acked(sc) && (*sent = (len - 2)) ){
537 mtx_unlock(&sc->mutex);
544 mtx_unlock(&sc->mutex);
574 "i2c",