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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
/* $OpenBSD: mpfiic.c,v 1.1 2022/02/16 13:07:36 visa Exp $ */
/*
* Copyright (c) 2022 Visa Hankala
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* Driver for PolarFire SoC MSS I2C controller.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/malloc.h>
#include <machine/bus.h>
#include <machine/fdt.h>
#define _I2C_PRIVATE
#include <dev/i2c/i2cvar.h>
#include <dev/ofw/fdt.h>
#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_clock.h>
#include <dev/ofw/ofw_misc.h>
#define I2C_CTRL 0x0000
#define I2C_CTRL_CR2 (1 << 7)
#define I2C_CTRL_ENS1 (1 << 6)
#define I2C_CTRL_STA (1 << 5)
#define I2C_CTRL_STO (1 << 4)
#define I2C_CTRL_SI (1 << 3)
#define I2C_CTRL_AA (1 << 2)
#define I2C_CTRL_CR1 (1 << 1)
#define I2C_CTRL_CR0 (1 << 0)
#define I2C_STATUS 0x0004
#define I2C_DATA 0x0008
#define I2C_SLAVE0ADR 0x000c
#define I2C_SMBUS 0x0010
#define I2C_FREQ 0x0014
#define I2C_GLITCHREG 0x0018
#define I2C_SLAVE1ADR 0x001c
#define I2C_STATUS_START 0x08
#define I2C_STATUS_RESTART 0x10
#define I2C_STATUS_SLAW_ACK 0x18
#define I2C_STATUS_DATAW_ACK 0x28
#define I2C_STATUS_LOSTARB 0x38
#define I2C_STATUS_SLAR_ACK 0x40
#define I2C_STATUS_DATAR_ACK 0x50
#define I2C_STATUS_DATAR_NACK 0x58
#define I2C_STATUS_IDLE 0xf8
struct mpfiic_softc {
struct device sc_dev;
bus_space_tag_t sc_iot;
bus_space_handle_t sc_ioh;
int sc_node;
struct i2c_bus sc_i2c_bus;
struct i2c_controller sc_i2c_tag;
struct rwlock sc_i2c_lock;
uint32_t sc_bus_freq; /* in Hz */
uint8_t sc_ctrl;
uint8_t sc_start_sent;
};
#define HREAD4(sc, reg) \
(bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg)))
#define HWRITE4(sc, reg, val) \
bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
int mpfiic_match(struct device *, void *, void*);
void mpfiic_attach(struct device *, struct device *, void *);
int mpfiic_i2c_acquire_bus(void *, int);
void mpfiic_i2c_release_bus(void *, int);
int mpfiic_i2c_send_start(void *, int);
int mpfiic_i2c_send_stop(void *, int);
int mpfiic_i2c_initiate_xfer(void *, i2c_addr_t, int);
int mpfiic_i2c_read_byte(void *, uint8_t *, int);
int mpfiic_i2c_write_byte(void *, uint8_t, int);
void mpfiic_i2c_scan(struct device *, struct i2cbus_attach_args *, void *);
int mpfiic_wait(struct mpfiic_softc *, uint8_t);
const struct cfattach mpfiic_ca = {
sizeof(struct mpfiic_softc), mpfiic_match, mpfiic_attach
};
struct cfdriver mpfiic_cd = {
NULL, "mpfiic", DV_DULL
};
static struct {
uint32_t div;
uint32_t cr;
} mpfiic_clk_divs[] = {
#ifdef notused
/* BCLK */
{ 8, I2C_CTRL_CR2 | I2C_CTRL_CR1 | I2C_CTRL_CR0 },
#endif
/* PCLK */
{ 60, I2C_CTRL_CR2 | I2C_CTRL_CR1 },
{ 120, I2C_CTRL_CR2 | I2C_CTRL_CR0 },
{ 160, I2C_CTRL_CR1 | I2C_CTRL_CR0 },
{ 192, I2C_CTRL_CR1 },
{ 224, I2C_CTRL_CR0 },
{ 256, 0 },
{ 960, I2C_CTRL_CR2 },
};
int
mpfiic_match(struct device *parent, void *match, void *aux)
{
struct fdt_attach_args *faa = aux;
if (faa->fa_nreg < 1)
return 0;
return OF_is_compatible(faa->fa_node, "microchip,mpfs-i2c");
}
void
mpfiic_attach(struct device *parent, struct device *self, void *aux)
{
struct i2cbus_attach_args iba;
struct fdt_attach_args *faa = aux;
struct mpfiic_softc *sc = (struct mpfiic_softc *)self;
uint32_t i, bus_freq, clock_freq;
sc->sc_node = faa->fa_node;
sc->sc_iot = faa->fa_iot;
clock_freq = clock_get_frequency(sc->sc_node, NULL);
bus_freq = OF_getpropint(sc->sc_node, "clock-frequency", 100000);
/* Determine clock divider, assumes PCLK. */
for (i = 0; i < nitems(mpfiic_clk_divs) - 1; i++) {
if (clock_freq / mpfiic_clk_divs[i].div <= bus_freq)
break;
}
sc->sc_bus_freq = clock_freq / mpfiic_clk_divs[i].div;
sc->sc_ctrl = mpfiic_clk_divs[i].cr | I2C_CTRL_ENS1;
if (sc->sc_bus_freq == 0) {
printf(": invalid bus frequency\n");
return;
}
if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr, faa->fa_reg[0].size,
0, &sc->sc_ioh) != 0) {
printf(": can't map registers\n");
return;
}
clock_enable_all(sc->sc_node);
/* Initialize the device. */
HWRITE4(sc, I2C_CTRL, sc->sc_ctrl);
HWRITE4(sc, I2C_CTRL, 0);
/* Disable slave address comparison. */
HWRITE4(sc, I2C_SLAVE0ADR, 0);
HWRITE4(sc, I2C_SLAVE1ADR, 0);
/* Disable SMBus logic, operate in standard I2C mode. */
HWRITE4(sc, I2C_SMBUS, 0);
printf("\n");
rw_init(&sc->sc_i2c_lock, "iiclk");
sc->sc_i2c_tag.ic_cookie = sc;
sc->sc_i2c_tag.ic_acquire_bus = mpfiic_i2c_acquire_bus;
sc->sc_i2c_tag.ic_release_bus = mpfiic_i2c_release_bus;
sc->sc_i2c_tag.ic_send_start = mpfiic_i2c_send_start;
sc->sc_i2c_tag.ic_send_stop = mpfiic_i2c_send_stop;
sc->sc_i2c_tag.ic_initiate_xfer = mpfiic_i2c_initiate_xfer;
sc->sc_i2c_tag.ic_read_byte = mpfiic_i2c_read_byte;
sc->sc_i2c_tag.ic_write_byte = mpfiic_i2c_write_byte;
memset(&iba, 0, sizeof(iba));
iba.iba_name = "iic";
iba.iba_tag = &sc->sc_i2c_tag;
iba.iba_bus_scan = mpfiic_i2c_scan;
iba.iba_bus_scan_arg = &sc->sc_node;
config_found(self, &iba, iicbus_print);
sc->sc_i2c_bus.ib_node = faa->fa_node;
sc->sc_i2c_bus.ib_ic = &sc->sc_i2c_tag;
i2c_register(&sc->sc_i2c_bus);
}
int
mpfiic_i2c_acquire_bus(void *arg, int flags)
{
struct mpfiic_softc *sc = arg;
if (cold || (flags & I2C_F_POLL))
return 0;
return rw_enter(&sc->sc_i2c_lock, RW_WRITE | RW_INTR);
}
void
mpfiic_i2c_release_bus(void *arg, int flags)
{
struct mpfiic_softc *sc = arg;
if (cold || (flags & I2C_F_POLL))
return;
rw_exit(&sc->sc_i2c_lock);
}
int
mpfiic_i2c_send_start(void *cookie, int flags)
{
struct mpfiic_softc *sc = cookie;
int error;
uint8_t nstatus;
HWRITE4(sc, I2C_CTRL, sc->sc_ctrl | I2C_CTRL_STA);
if (sc->sc_start_sent)
nstatus = I2C_STATUS_RESTART;
else
nstatus = I2C_STATUS_START;
error = mpfiic_wait(sc, nstatus);
if (error != 0)
return error;
sc->sc_start_sent = 1;
return 0;
}
int
mpfiic_i2c_send_stop(void *cookie, int flags)
{
struct mpfiic_softc *sc = cookie;
sc->sc_start_sent = 0;
HWRITE4(sc, I2C_CTRL, sc->sc_ctrl | I2C_CTRL_STO);
/* Let a few bus clock cycles pass. */
delay(4 * 1000000 / sc->sc_bus_freq);
/* Disable the device. This resets the state machine. */
HWRITE4(sc, I2C_CTRL, 0);
return 0;
}
int
mpfiic_i2c_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
{
struct mpfiic_softc *sc = cookie;
int error;
uint8_t mode, nstatus;
if (addr >= 0x80)
return EINVAL;
error = mpfiic_i2c_send_start(sc, flags);
if (error != 0)
return error;
if (flags & I2C_F_READ) {
mode = 0x01;
nstatus = I2C_STATUS_SLAR_ACK;
} else {
mode = 0x00;
nstatus = I2C_STATUS_SLAW_ACK;
}
HWRITE4(sc, I2C_DATA, (addr << 1) | mode);
HWRITE4(sc, I2C_CTRL, sc->sc_ctrl);
return mpfiic_wait(sc, nstatus);
}
int
mpfiic_i2c_read_byte(void *cookie, uint8_t *datap, int flags)
{
struct mpfiic_softc *sc = cookie;
int error;
uint8_t ack = 0, nstatus;
if ((flags & I2C_F_LAST) == 0)
ack = I2C_CTRL_AA;
HWRITE4(sc, I2C_CTRL, sc->sc_ctrl | ack);
if (flags & I2C_F_LAST)
nstatus = I2C_STATUS_DATAR_NACK;
else
nstatus = I2C_STATUS_DATAR_ACK;
error = mpfiic_wait(sc, nstatus);
if (error != 0)
return error;
*datap = HREAD4(sc, I2C_DATA);
if (flags & I2C_F_STOP)
error = mpfiic_i2c_send_stop(sc, flags);
return error;
}
int
mpfiic_i2c_write_byte(void *cookie, uint8_t data, int flags)
{
struct mpfiic_softc *sc = cookie;
int error;
HWRITE4(sc, I2C_DATA, data);
HWRITE4(sc, I2C_CTRL, sc->sc_ctrl);
error = mpfiic_wait(sc, I2C_STATUS_DATAW_ACK);
if (error != 0)
return error;
if (flags & I2C_F_STOP)
error = mpfiic_i2c_send_stop(sc, flags);
return error;
}
void
mpfiic_i2c_scan(struct device *self, struct i2cbus_attach_args *iba, void *arg)
{
struct i2c_attach_args ia;
char status[32];
char *compat;
uint32_t reg[1];
int iba_node = *(int *)arg;
int len, node;
for (node = OF_child(iba_node); node != 0; node = OF_peer(node)) {
memset(status, 0, sizeof(status));
if (OF_getprop(node, "status", status, sizeof(status)) > 0 &&
strcmp(status, "disabled") == 0)
continue;
memset(reg, 0, sizeof(reg));
if (OF_getprop(node, "reg", ®, sizeof(reg)) != sizeof(reg))
continue;
len = OF_getproplen(node, "compatible");
if (len <= 0)
continue;
compat = malloc(len, M_TEMP, M_WAITOK);
OF_getprop(node, "compatible", compat, len);
memset(&ia, 0, sizeof(ia));
ia.ia_tag = iba->iba_tag;
ia.ia_addr = bemtoh32(®[0]);
ia.ia_name = compat;
ia.ia_namelen = len;
ia.ia_cookie = &node;
config_found(self, &ia, iic_print);
free(compat, M_TEMP, len);
}
}
int
mpfiic_wait(struct mpfiic_softc *sc, uint8_t nstatus)
{
int timeout;
uint8_t ctrl, status;
for (timeout = 100000; timeout > 0; timeout--) {
ctrl = HREAD4(sc, I2C_CTRL);
if (ctrl & I2C_CTRL_SI)
break;
delay(1);
}
if (timeout == 0)
return ETIMEDOUT;
status = HREAD4(sc, I2C_STATUS);
if (status != nstatus)
return EIO;
return 0;
}
|