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
|
/* $OpenBSD: sxirsb.c,v 1.6 2022/07/09 20:52:46 kettenis Exp $ */
/*
* Copyright (c) 2017 Mark Kettenis <kettenis@openbsd.org>
*
* 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.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <machine/bus.h>
#include <machine/fdt.h>
#include <dev/fdt/rsbvar.h>
#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_clock.h>
#include <dev/ofw/ofw_pinctrl.h>
#include <dev/ofw/fdt.h>
#define RSB_CTRL 0x0000
#define RSB_CTRL_START_TRANS (1 << 7)
#define RSB_CTRL_ABORT_TRANS (1 << 6)
#define RSB_CTRL_GLOBAL_INT_ENB (1 << 1)
#define RSB_CTRL_SOFT_RESET (1 << 0)
#define RSB_CCR 0x0004
#define RSB_CCR_CD_ODLY_SHIFT 8
#define RSB_CCR_CD_ODLY_MAX 0x7
#define RSB_CCR_CK_DIV_SHIFT 0
#define RSB_CCR_CK_DIV_MAX 0xff
#define RSB_STAT 0x000c
#define RSB_STAT_TRANS_OVER (1 << 0)
#define RSB_AR 0x0010
#define RSB_DATA 0x001c
#define RSB_DMCR 0x0028
#define RSB_DMCR_DEVICE_MODE_START (1U << 31)
#define RSB_DMCR_DEVICE_MODE_DATA 0x7c3e00
#define RSB_CMD 0x002c
#define RSB_DAR 0x0030
#define SRTA 0xe8
#define RD8 0x8b
#define RD16 0x9c
#define RD32 0xa6
#define WR8 0x4e
#define WR16 0x59
#define WR32 0x63
#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))
#define HSET4(sc, reg, bits) \
HWRITE4((sc), (reg), HREAD4((sc), (reg)) | (bits))
#define HCLR4(sc, reg, bits) \
HWRITE4((sc), (reg), HREAD4((sc), (reg)) & ~(bits))
struct sxirsb_softc {
struct device sc_dev;
bus_space_tag_t sc_iot;
bus_space_handle_t sc_ioh;
int sc_node;
int sc_addr;
};
int sxirsb_match(struct device *, void *, void *);
void sxirsb_attach(struct device *, struct device *, void *);
int sxirsb_activate(struct device *, int);
const struct cfattach sxirsb_ca = {
sizeof(struct sxirsb_softc), sxirsb_match, sxirsb_attach,
NULL, sxirsb_activate
};
struct cfdriver sxirsb_cd = {
NULL, "sxirsb", DV_DULL
};
int sxirsb_init(struct sxirsb_softc *);
uint8_t sxirsb_rta(uint16_t);
int
sxirsb_match(struct device *parent, void *match, void *aux)
{
struct fdt_attach_args *faa = aux;
return OF_is_compatible(faa->fa_node, "allwinner,sun8i-a23-rsb");
}
void
sxirsb_attach(struct device *parent, struct device *self, void *aux)
{
struct sxirsb_softc *sc = (struct sxirsb_softc *)self;
struct fdt_attach_args *faa = aux;
struct rsb_attach_args ra;
char name[32];
uint32_t reg;
int node;
if (faa->fa_nreg < 1) {
printf(": no registers\n");
return;
}
sc->sc_node = faa->fa_node;
sc->sc_iot = faa->fa_iot;
if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr,
faa->fa_reg[0].size, 0, &sc->sc_ioh)) {
printf(": can't map registers\n");
return;
}
printf("\n");
if (sxirsb_init(sc))
return;
for (node = OF_child(faa->fa_node); node; node = OF_peer(node)) {
reg = OF_getpropint(node, "reg", 0);
if (reg == 0)
continue;
memset(name, 0, sizeof(name));
if (OF_getprop(node, "compatible", name, sizeof(name)) == -1)
continue;
if (name[0] == '\0')
continue;
memset(&ra, 0, sizeof(ra));
ra.ra_cookie = sc;
ra.ra_da = reg;
ra.ra_rta = sxirsb_rta(reg);
ra.ra_name = name;
ra.ra_node = node;
config_found(self, &ra, rsb_print);
}
}
int
sxirsb_activate(struct device *self, int act)
{
struct sxirsb_softc *sc = (struct sxirsb_softc *)self;
int error = 0;
switch (act) {
case DVACT_RESUME:
error = sxirsb_init(sc);
if (error)
return error;
error = config_activate_children(self, act);
break;
default:
error = config_activate_children(self, act);
break;
}
return error;
}
int
sxirsb_init(struct sxirsb_softc *sc)
{
uint32_t freq, parent_freq, div, odly;
uint32_t reg;
uint8_t rta;
int node;
int timo;
pinctrl_byname(sc->sc_node, "default");
clock_enable_all(sc->sc_node);
reset_deassert_all(sc->sc_node);
HWRITE4(sc, RSB_CTRL, RSB_CTRL_SOFT_RESET);
for (timo = 1000; timo > 0; timo--) {
if ((HREAD4(sc, RSB_CTRL) & RSB_CTRL_SOFT_RESET) == 0)
break;
delay(100);
}
if (timo == 0) {
printf("%s: reset failed\n", sc->sc_dev.dv_xname);
return EIO;
}
freq = OF_getpropint(sc->sc_node, "clock-frequency", 3000000);
parent_freq = clock_get_frequency_idx(sc->sc_node, 0);
div = parent_freq / freq / 2;
if (div == 0)
div = 1;
if (div > (RSB_CCR_CK_DIV_MAX + 1))
div = (RSB_CCR_CK_DIV_MAX + 1);
odly = div >> 1;
if (odly == 0)
odly = 1;
if (odly > RSB_CCR_CD_ODLY_MAX)
odly = RSB_CCR_CD_ODLY_MAX;
HWRITE4(sc, RSB_CCR, (odly << RSB_CCR_CD_ODLY_SHIFT) |
((div - 1) << RSB_CCR_CK_DIV_SHIFT));
HWRITE4(sc, RSB_DMCR, RSB_DMCR_DEVICE_MODE_START |
RSB_DMCR_DEVICE_MODE_DATA);
for (timo = 1000; timo > 0; timo--) {
if ((HREAD4(sc, RSB_DMCR) & RSB_DMCR_DEVICE_MODE_START) == 0)
break;
delay(100);
}
if (timo == 0) {
printf("%s: mode switch failed\n", sc->sc_dev.dv_xname);
return EIO;
}
for (node = OF_child(sc->sc_node); node; node = OF_peer(node)) {
reg = OF_getpropint(node, "reg", 0);
if (reg == 0)
continue;
rta = sxirsb_rta(reg);
HWRITE4(sc, RSB_CMD, SRTA);
HWRITE4(sc, RSB_DAR, (rta << 16 | reg));
HSET4(sc, RSB_CTRL, RSB_CTRL_START_TRANS);
for (timo = 1000; timo > 0; timo--) {
if ((HREAD4(sc, RSB_CTRL) & RSB_CTRL_START_TRANS) == 0)
break;
delay(10);
}
if (timo == 0) {
printf("%s: SRTA failed for device 0x%03x\n",
sc->sc_dev.dv_xname, reg);
return EIO;
}
}
return 0;
}
/*
* Use a fixed device address to run-time address mapping. This keeps
* things simple and matches what Linux does.
*/
struct rsb_addr_map {
uint16_t da;
uint8_t rta;
};
struct rsb_addr_map rsb_addr_map[] = {
{ 0x3a3, 0x2d },
{ 0x745, 0x3a },
{ 0xe89, 0x4e }
};
uint8_t
sxirsb_rta(uint16_t da)
{
int i;
for (i = 0; i < nitems(rsb_addr_map); i++) {
if (rsb_addr_map[i].da == da)
return rsb_addr_map[i].rta;
}
return 0;
}
int
sxirsb_do_trans(struct sxirsb_softc *sc)
{
uint16_t stat;
int timo;
HSET4(sc, RSB_CTRL, RSB_CTRL_START_TRANS);
for (timo = 1000; timo > 0; timo--) {
if ((HREAD4(sc, RSB_CTRL) & RSB_CTRL_START_TRANS) == 0)
break;
delay(10);
}
stat = HREAD4(sc, RSB_STAT);
HWRITE4(sc, RSB_STAT, stat);
if (stat != RSB_STAT_TRANS_OVER)
return EIO;
if (timo == 0)
return ETIMEDOUT;
return 0;
}
uint8_t
rsb_read_1(void *cookie, uint8_t rta, uint8_t addr)
{
struct sxirsb_softc *sc = cookie;
HWRITE4(sc, RSB_CMD, RD8);
HWRITE4(sc, RSB_DAR, rta << 16);
HWRITE4(sc, RSB_AR, addr);
if (sxirsb_do_trans(sc)) {
printf("%s: RD8 failed for run-time address 0x%02x\n",
sc->sc_dev.dv_xname, rta);
return 0xff;
}
return HREAD4(sc, RSB_DATA);
}
uint16_t
rsb_read_2(void *cookie, uint8_t rta, uint8_t addr)
{
struct sxirsb_softc *sc = cookie;
HWRITE4(sc, RSB_CMD, RD16);
HWRITE4(sc, RSB_DAR, rta << 16);
HWRITE4(sc, RSB_AR, addr);
if (sxirsb_do_trans(sc)) {
printf("%s: RD16 failed for run-time address 0x%02x\n",
sc->sc_dev.dv_xname, rta);
return 0xff;
}
return HREAD4(sc, RSB_DATA);
}
void
rsb_write_1(void *cookie, uint8_t rta, uint8_t addr, uint8_t data)
{
struct sxirsb_softc *sc = cookie;
HWRITE4(sc, RSB_CMD, WR8);
HWRITE4(sc, RSB_DAR, rta << 16);
HWRITE4(sc, RSB_AR, addr);
HWRITE4(sc, RSB_DATA, data);
if (sxirsb_do_trans(sc)) {
printf("%s: WR8 failed for run-time address 0x%02x\n",
sc->sc_dev.dv_xname, rta);
return;
}
}
void
rsb_write_2(void *cookie, uint8_t rta, uint8_t addr, uint16_t data)
{
struct sxirsb_softc *sc = cookie;
HWRITE4(sc, RSB_CMD, WR16);
HWRITE4(sc, RSB_DAR, rta << 16);
HWRITE4(sc, RSB_AR, addr);
HWRITE4(sc, RSB_DATA, data);
if (sxirsb_do_trans(sc)) {
printf("%s: WR16 failed for run-time address 0x%02x\n",
sc->sc_dev.dv_xname, rta);
return;
}
}
int
rsb_print(void *aux, const char *pnp)
{
struct rsb_attach_args *ra = aux;
if (pnp != NULL)
printf("\"%s\" at %s", ra->ra_name, pnp);
printf(" addr 0x%x", ra->ra_da);
return (UNCONF);
}
|