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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
/* $NetBSD: cdnsiic.c,v 1.1 2022/11/05 17:31:37 jmcneill Exp $ */
/*-
* Copyright (c) 2022 Jared McNeill <jmcneill@invisible.ca>
* 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``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 FOUNDATION OR CONTRIBUTORS
* 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.
*/
/*
* Cadence I2C controller
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: cdnsiic.c,v 1.1 2022/11/05 17:31:37 jmcneill Exp $");
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/device.h>
#include <sys/intr.h>
#include <sys/systm.h>
#include <sys/time.h>
#include <sys/kmem.h>
#include <dev/clk/clk.h>
#include <dev/i2c/i2cvar.h>
#include <dev/ic/cdnsiicvar.h>
/* From Zynq-7000 SoC Technical Reference Manual, "Supports 16-byte FIFO" */
#define FIFO_DEPTH 16
/* Poll timeout, in microseconds. */
#define POLL_TIMEOUT 10000
#define CR_REG 0x00
#define CR_DIV_A __BITS(15,14)
#define CR_DIV_B __BITS(13,8)
#define CR_CLR_FIFO __BIT(6)
#define CR_HOLD __BIT(4)
#define CR_ACKEN __BIT(3)
#define CR_NEA __BIT(2)
#define CR_MS __BIT(1)
#define CR_RD_WR __BIT(0)
#define SR_REG 0x04
#define SR_TXDV __BIT(6)
#define SR_RXDV __BIT(5)
#define ADDR_REG 0x08
#define DATA_REG 0x0c
#define ISR_REG 0x10
#define ISR_ARB_LOST __BIT(9)
#define ISR_RX_UNF __BIT(7)
#define ISR_TX_OVR __BIT(6)
#define ISR_RX_OVR __BIT(5)
#define ISR_SLV_RDY __BIT(4)
#define ISR_TO __BIT(3)
#define ISR_NACK __BIT(2)
#define ISR_DATA __BIT(1)
#define ISR_COMP __BIT(0)
#define ISR_ERROR_MASK (ISR_ARB_LOST | ISR_TX_OVR | ISR_RX_OVR | ISR_NACK)
#define TRANS_SIZE_REG 0x14
#define SLV_PAUSE_REG 0x18
#define TIME_OUT_REG 0x1c
#define IMR_REG 0x20
#define IER_REG 0x24
#define IDR_REG 0x28
#define RD4(sc, reg) \
bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
#define WR4(sc, reg, val) \
bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
static int
cdnsiic_init(struct cdnsiic_softc *sc)
{
int diva, divb;
int diff, calc_bus_freq;
int best_diva, best_divb, best_diff;
u_int pclk;
/*
* SCL frequency is calculated by the following formula:
*
* SCL Divisor = 22 * (divisor_a + 1) * (divisor_b + 1)
* SCL = PCLK / SCLK Divisor
*/
pclk = clk_get_rate(sc->sc_pclk);
best_diff = sc->sc_bus_freq;
best_diva = best_divb = 0;
for (diva = 0; diva <= 0x3; diva++) {
divb = howmany(pclk, 22 * sc->sc_bus_freq * (diva + 1)) - 1;
if (divb < 0 || divb > 0x3f) {
continue;
}
calc_bus_freq = pclk / (22 * (diva + 1) * (divb + 1));
diff = sc->sc_bus_freq - calc_bus_freq;
if (diff < best_diff) {
best_diff = diff;
best_diva = diva;
best_divb = divb;
}
}
if (best_diff == sc->sc_bus_freq) {
return ENXIO;
}
WR4(sc, CR_REG,
__SHIFTIN(best_diva, CR_DIV_A) |
__SHIFTIN(best_divb, CR_DIV_B) |
CR_CLR_FIFO |
CR_ACKEN |
CR_NEA |
CR_MS);
WR4(sc, TIME_OUT_REG, 0xff);
return 0;
}
static int
cdnsiic_poll_fifo(struct cdnsiic_softc *sc, uint32_t sr_mask, uint32_t sr_maskval)
{
uint32_t sr_val, isr_val;
int retry = POLL_TIMEOUT;
while (--retry > 0) {
sr_val = RD4(sc, SR_REG);
isr_val = RD4(sc, ISR_REG);
if ((isr_val & ISR_ERROR_MASK) != 0) {
return EIO;
}
if ((sr_val & sr_mask) == sr_maskval) {
return 0;
}
delay(1);
}
return ETIMEDOUT;
}
static int
cdnsiic_poll_transfer_complete(struct cdnsiic_softc *sc)
{
uint32_t val;
int retry = POLL_TIMEOUT;
while (--retry > 0) {
val = RD4(sc, ISR_REG);
if ((val & ISR_COMP) != 0) {
return 0;
}
delay(1);
}
return ETIMEDOUT;
}
static int
cdnsiic_write(struct cdnsiic_softc *sc, i2c_addr_t addr,
const uint8_t *data, size_t datalen, bool send_stop)
{
uint32_t val;
u_int xferlen, fifo_space, n;
bool write_addr = true;
int error;
if (datalen == 0 || datalen > 256) {
return EINVAL;
}
val = RD4(sc, CR_REG);
val |= CR_CLR_FIFO;
val &= ~CR_RD_WR;
WR4(sc, CR_REG, val);
WR4(sc, ISR_REG, RD4(sc, ISR_REG));
while (datalen > 0) {
fifo_space = FIFO_DEPTH - RD4(sc, TRANS_SIZE_REG);
xferlen = uimin(datalen, fifo_space);
for (n = 0; n < xferlen; n++, data++) {
WR4(sc, DATA_REG, *data);
}
if (write_addr) {
WR4(sc, ADDR_REG, addr);
write_addr = false;
}
datalen -= xferlen;
error = cdnsiic_poll_fifo(sc, SR_TXDV, 0);
if (error != 0) {
return error;
}
}
return cdnsiic_poll_transfer_complete(sc);
}
static int
cdnsiic_read(struct cdnsiic_softc *sc, i2c_addr_t addr,
uint8_t *data, size_t datalen)
{
uint32_t val;
int error;
if (datalen == 0 || datalen > 255) {
return EINVAL;
}
val = RD4(sc, CR_REG);
val |= CR_CLR_FIFO | CR_RD_WR;
WR4(sc, CR_REG, val);
WR4(sc, ISR_REG, RD4(sc, ISR_REG));
WR4(sc, TRANS_SIZE_REG, datalen);
WR4(sc, ADDR_REG, addr);
while (datalen > 0) {
error = cdnsiic_poll_fifo(sc, SR_RXDV, SR_RXDV);
if (error != 0) {
return error;
}
*data = RD4(sc, DATA_REG) & 0xff;
data++;
datalen--;
}
return cdnsiic_poll_transfer_complete(sc);
}
static int
cdnsiic_exec(void *priv, i2c_op_t op, i2c_addr_t addr,
const void *cmdbuf, size_t cmdlen, void *buf, size_t buflen, int flags)
{
struct cdnsiic_softc * const sc = priv;
uint32_t val;
int error;
val = RD4(sc, CR_REG);
WR4(sc, CR_REG, val | CR_HOLD);
if (cmdlen > 0) {
error = cdnsiic_write(sc, addr, cmdbuf, cmdlen, false);
if (error != 0) {
goto done;
}
}
if (I2C_OP_READ_P(op)) {
error = cdnsiic_read(sc, addr, buf, buflen);
} else {
error = cdnsiic_write(sc, addr, buf, buflen, true);
}
done:
val = RD4(sc, CR_REG);
WR4(sc, CR_REG, val & ~CR_HOLD);
return error;
}
int
cdnsiic_attach(struct cdnsiic_softc *sc)
{
int error;
aprint_naive("\n");
aprint_normal(": Cadence I2C (%u Hz)\n", sc->sc_bus_freq);
error = cdnsiic_init(sc);
if (error != 0) {
return error;
}
iic_tag_init(&sc->sc_ic);
sc->sc_ic.ic_cookie = sc;
sc->sc_ic.ic_exec = cdnsiic_exec;
return 0;
}
|