1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
/* $NetBSD: rtciic.c,v 1.5 2021/08/07 16:19:00 thorpej Exp $ */
/*
* Copyright (c) 2011 KIYOHARA Takashi
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: rtciic.c,v 1.5 2021/08/07 16:19:00 thorpej Exp $");
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/device.h>
#include <sys/errno.h>
#include <machine/autoconf.h>
#include <dev/i2c/i2cvar.h>
#include <dev/i2c/i2c_bitbang.h>
#include "locators.h"
#ifdef RTCIIC_DEBUG
#define DPRINTF(x) printf x
#else
#define DPRINTF(x)
#endif
#define RTCIIC_SDAR (1 << 3) /* received serial data */
#define RTCIIC_SDAW (1 << 2) /* sended serial data */
#define RTCIIC_SCL (1 << 1) /* serial clock */
#define RTCIIC_RW (1 << 0) /* data direction (0:write, 1:read) */
/* This device is Big Endian */
#define RTCIIC_READ(sc) \
bswap16(bus_space_read_2((sc)->sc_iot, (sc)->sc_ioh, 0))
#define RTCIIC_WRITE(sc, val) \
bus_space_write_2((sc)->sc_iot, (sc)->sc_ioh, 0, bswap16(val))
struct rtciic_softc {
device_t sc_dev;
bus_space_tag_t sc_iot;
bus_space_handle_t sc_ioh;
struct i2c_controller sc_i2c;
struct i2c_bitbang_ops sc_bops;
int sc_rw;
};
static int rtciic_match(device_t, cfdata_t , void *);
static void rtciic_attach(device_t, device_t, void *);
static int rtciic_send_start(void *, int);
static int rtciic_send_stop(void *, int);
static int rtciic_initiate_xfer(void *, i2c_addr_t, int);
static int rtciic_read_byte(void *, uint8_t *, int);
static int rtciic_write_byte(void *, uint8_t, int);
static void rtciic_set_dir(void *, uint32_t);
static void rtciic_set_bits(void *, uint32_t);
static uint32_t rtciic_read_bits(void *);
CFATTACH_DECL_NEW(rtciic, sizeof(struct rtciic_softc),
rtciic_match, rtciic_attach, NULL, NULL);
static int
rtciic_match(device_t parent, cfdata_t match, void *aux)
{
struct mainbus_attach_args *ma = aux;
if (strcmp(ma->ma_name, match->cf_name) != 0)
return 0;
/* Disallow wildcarded values. */
if (ma->ma_addr1 == MAINBUSCF_ADDR1_DEFAULT)
return 0;
/* no irq */
if (ma->ma_irq1 != MAINBUSCF_IRQ1_DEFAULT)
return 0;
return 1;
}
void
rtciic_attach(device_t parent, device_t self, void *aux)
{
struct rtciic_softc *sc = device_private(self);
struct mainbus_attach_args *ma = aux;
struct i2cbus_attach_args iba;
sc->sc_dev = self;
aprint_normal("\n");
aprint_naive("\n");
/* Map I/O space(16bit). */
sc->sc_iot = 0;
if (bus_space_map(sc->sc_iot, ma->ma_addr1, 2, 0, &sc->sc_ioh)) {
aprint_error_dev(self, "can't map registers\n");
return;
}
sc->sc_rw = RTCIIC_READ(sc) & RTCIIC_RW;
/* register with iic */
iic_tag_init(&sc->sc_i2c);
sc->sc_i2c.ic_cookie = sc;
sc->sc_i2c.ic_send_start = rtciic_send_start;
sc->sc_i2c.ic_send_stop = rtciic_send_stop;
sc->sc_i2c.ic_initiate_xfer = rtciic_initiate_xfer;
sc->sc_i2c.ic_read_byte = rtciic_read_byte;
sc->sc_i2c.ic_write_byte = rtciic_write_byte;
sc->sc_bops.ibo_set_dir = rtciic_set_dir;
sc->sc_bops.ibo_set_bits = rtciic_set_bits;
sc->sc_bops.ibo_read_bits = rtciic_read_bits;
sc->sc_bops.ibo_bits[I2C_BIT_SDA] = RTCIIC_SDAW;
sc->sc_bops.ibo_bits[I2C_BIT_SCL] = RTCIIC_SCL;
sc->sc_bops.ibo_bits[I2C_BIT_OUTPUT] = 0;
sc->sc_bops.ibo_bits[I2C_BIT_INPUT] = RTCIIC_RW;
memset(&iba, 0, sizeof(iba));
iba.iba_tag = &sc->sc_i2c;
config_found(sc->sc_dev, &iba, iicbus_print, CFARGS_NONE);
}
static int
rtciic_send_start(void *arg, int flags)
{
struct rtciic_softc *sc = arg;
return i2c_bitbang_send_start(sc, flags, &sc->sc_bops);
}
static int
rtciic_send_stop(void *arg, int flags)
{
struct rtciic_softc *sc = arg;
return i2c_bitbang_send_stop(sc, flags, &sc->sc_bops);
}
static int
rtciic_initiate_xfer(void *arg, i2c_addr_t addr, int flags)
{
struct rtciic_softc *sc = arg;
return i2c_bitbang_initiate_xfer(sc, addr, flags, &sc->sc_bops);
}
static int
rtciic_read_byte(void *arg, uint8_t *vp, int flags)
{
struct rtciic_softc *sc = arg;
return i2c_bitbang_read_byte(sc, vp, flags, &sc->sc_bops);
}
static int
rtciic_write_byte(void *arg, uint8_t v, int flags)
{
struct rtciic_softc *sc = arg;
return i2c_bitbang_write_byte(sc, v, flags, &sc->sc_bops);
}
static void
rtciic_set_dir(void *arg, uint32_t bits)
{
struct rtciic_softc *sc = arg;
uint16_t reg;
DPRINTF(("%s: set dir %s\n",
device_xname(sc->sc_dev), (bits & RTCIIC_RW) ? "READ" : "WRITE"));
if (sc->sc_rw != (bits & RTCIIC_RW)) {
reg = RTCIIC_READ(sc);
reg &= ~RTCIIC_RW;
reg |= bits;
RTCIIC_WRITE(sc, reg);
delay(30);
sc->sc_rw = bits & RTCIIC_RW;
}
}
static void
rtciic_set_bits(void *arg, uint32_t bits)
{
struct rtciic_softc *sc = arg;
DPRINTF(("%s: %s\n",
device_xname(sc->sc_dev),
(bits == (RTCIIC_SDAW | RTCIIC_SCL)) ? "set SDA/SCL" :
((bits == RTCIIC_SDAW) ? "set SDA" :
((bits == RTCIIC_SCL) ? "set SCL" : "reset"))));
if (sc->sc_rw & RTCIIC_RW) {
bits &= RTCIIC_SCL;
bits |= RTCIIC_RW;
}
RTCIIC_WRITE(sc, bits);
delay(40);
}
static uint32_t
rtciic_read_bits(void *arg)
{
struct rtciic_softc *sc = arg;
uint8_t rv, v;
v = RTCIIC_READ(sc);
rv = v & RTCIIC_SCL;
if (v & RTCIIC_SDAR)
rv |= RTCIIC_SDAW;
DPRINTF(("%s: read %s\n",
device_xname(sc->sc_dev),
(rv == (RTCIIC_SDAW | RTCIIC_SCL)) ? "SDA/SCL" :
((rv == RTCIIC_SDAW) ? "SDA" :
((rv == RTCIIC_SCL) ? "SCL" : "no"))));
return (uint32_t)rv;
}
|