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
|
/* $NetBSD: com_hpcio.c,v 1.12 2018/12/08 17:46:11 thorpej Exp $ */
/*-
* Copyright (c) 2002 TAKEMRUA Shin. 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.
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
*
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: com_hpcio.c,v 1.12 2018/12/08 17:46:11 thorpej Exp $");
#include "opt_kgdb.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/reboot.h>
#include <sys/termios.h>
#include <machine/intr.h>
#include <machine/bus.h>
#include <machine/platid.h>
#include <machine/platid_mask.h>
#include <hpcmips/dev/com_hpciovar.h>
#include <dev/hpc/hpciovar.h>
#include <dev/ic/comvar.h>
#include <dev/ic/comreg.h>
#include "locators.h"
#define COM_HPCIODEBUG
#ifdef COM_HPCIODEBUG
int com_hpcio_debug = 0;
#define DPRINTF(arg...) do { if (com_hpcio_debug) printf(arg); } while (0)
#else
#define DPRINTF(arg...) do {} while (0)
#endif
/*
* XXX: NOTE: With the new com(4) COM_REGMAP support, the custom bus_space
* in this file could/should probably be removed and replaced with a custom
* map. Resulting in smaller/cleaner code. An hpcmips hacker should fix
* this, since I don't have h/w to test with -- I'm taking the route of least
* risk. - gdamore
*/
#define COM_HPCIO_BYTE_ALIGNMENT 0
#define COM_HPCIO_HALFWORD_ALIGNMENT 1
struct com_hpcio_softc {
struct com_softc hsc_com;
struct bus_space_tag hsc_iot;
struct hpcio_chip *hsc_hc;
};
static struct bus_space_tag com_hpcio_cniotx;
static bus_space_tag_t com_hpcio_cniot = &com_hpcio_cniotx;
static int com_hpcio_cniobase;
static int com_hpcio_probe(device_t, cfdata_t , void *);
static void com_hpcio_attach(device_t, device_t, void *);
static int com_hpcio_common_probe(bus_space_tag_t, int, int *);
void com_hpcio_iot_init(bus_space_tag_t iot, bus_space_tag_t base);
bus_space_protos(bs_notimpl);
bus_space_protos(bs_through);
bus_space_protos(com_hpcio);
CFATTACH_DECL_NEW(com_hpcio, sizeof(struct com_hpcio_softc),
com_hpcio_probe, com_hpcio_attach, NULL, NULL);
struct bus_space_ops com_hpcio_bs_ops = {
/* mapping/unmapping */
bs_through_bs_map,
bs_through_bs_unmap,
bs_through_bs_subregion,
/* allocation/deallocation */
bs_through_bs_alloc,
bs_through_bs_free,
/* get kernel virtual address */
bs_through_bs_vaddr, /* there is no linear mapping */
/* Mmap bus space for user */
bs_through_bs_mmap,
/* barrier */
bs_through_bs_barrier,
/* probe */
bs_through_bs_peek,
bs_through_bs_poke,
/* read (single) */
com_hpcio_bs_r_1,
bs_notimpl_bs_r_2,
bs_notimpl_bs_r_4,
bs_notimpl_bs_r_8,
/* read multiple */
bs_notimpl_bs_rm_1,
bs_notimpl_bs_rm_2,
bs_notimpl_bs_rm_4,
bs_notimpl_bs_rm_8,
/* read region */
bs_notimpl_bs_rr_1,
bs_notimpl_bs_rr_2,
bs_notimpl_bs_rr_4,
bs_notimpl_bs_rr_8,
/* write (single) */
com_hpcio_bs_w_1,
bs_notimpl_bs_w_2,
bs_notimpl_bs_w_4,
bs_notimpl_bs_w_8,
/* write multiple */
com_hpcio_bs_wm_1,
bs_notimpl_bs_wm_2,
bs_notimpl_bs_wm_4,
bs_notimpl_bs_wm_8,
/* write region */
bs_notimpl_bs_wr_1,
bs_notimpl_bs_wr_2,
bs_notimpl_bs_wr_4,
bs_notimpl_bs_wr_8,
#ifdef BUS_SPACE_HAS_REAL_STREAM_METHODS
/* read stream (single) */
bs_notimpl_bs_rs_1,
bs_notimpl_bs_rs_2,
bs_notimpl_bs_rs_4,
bs_notimpl_bs_rs_8,
/* read multiple stream */
bs_notimpl_bs_rms_1,
bs_notimpl_bs_rms_2,
bs_notimpl_bs_rms_4,
bs_notimpl_bs_rms_8,
/* read region stream */
bs_notimpl_bs_rrs_1,
bs_notimpl_bs_rrs_2,
bs_notimpl_bs_rrs_4,
bs_notimpl_bs_rrs_8,
/* write stream (single) */
bs_notimpl_bs_ws_1,
bs_notimpl_bs_ws_2,
bs_notimpl_bs_ws_4,
bs_notimpl_bs_ws_8,
/* write multiple stream */
bs_notimpl_bs_wms_1,
bs_notimpl_bs_wms_2,
bs_notimpl_bs_wms_4,
bs_notimpl_bs_wms_8,
/* write region stream */
bs_notimpl_bs_wrs_1,
bs_notimpl_bs_wrs_2,
bs_notimpl_bs_wrs_4,
bs_notimpl_bs_wrs_8,
#endif /* BUS_SPACE_HAS_REAL_STREAM_METHODS */
/* set multi */
bs_notimpl_bs_sm_1,
bs_notimpl_bs_sm_2,
bs_notimpl_bs_sm_4,
bs_notimpl_bs_sm_8,
/* set region */
bs_notimpl_bs_sr_1,
bs_notimpl_bs_sr_2,
bs_notimpl_bs_sr_4,
bs_notimpl_bs_sr_8,
/* copy */
bs_notimpl_bs_c_1,
bs_notimpl_bs_c_2,
bs_notimpl_bs_c_4,
bs_notimpl_bs_c_8,
};
int
com_hpcio_cndb_attach(bus_space_tag_t iot, int iobase, int rate,
int frequency, tcflag_t cflag, int kgdb)
{
int alignment;
DPRINTF("com_hpcio_cndb_attach()\n");
if (!com_hpcio_common_probe(iot, iobase, &alignment)) {
DPRINTF("com_hpcio_cndb_attach(): probe failed\n");
return (ENOTTY);
}
if (alignment == COM_HPCIO_HALFWORD_ALIGNMENT) {
DPRINTF("com_hpcio_cndb_attach(): half word aligned\n");
com_hpcio_iot_init(&com_hpcio_cniotx, iot);
com_hpcio_cniot = &com_hpcio_cniotx;
} else {
com_hpcio_cniot = iot;
}
com_hpcio_cniobase = iobase;
DPRINTF("com_hpcio_cndb_attach(): probe succeeded\n");
#ifdef KGDB
if (kgdb)
return (com_kgdb_attach(com_hpcio_cniot, iobase, rate,
frequency, COM_TYPE_NORMAL, cflag));
else
#endif
return (comcnattach(com_hpcio_cniot, iobase, rate,
frequency, COM_TYPE_NORMAL, cflag));
}
static int
com_hpcio_common_probe(bus_space_tag_t iot, int iobase, int *alignment)
{
bus_space_handle_t ioh;
static struct bus_space_tag tmpiot;
int rv;
/*
* try byte aligned register
*/
*alignment = COM_HPCIO_BYTE_ALIGNMENT;
if (bus_space_map(iot, iobase, 1, 0, &ioh))
return 0;
rv = comprobe1(iot, ioh);
bus_space_unmap(iot, ioh, 1);
if (rv != 0)
return (rv);
/*
* try half word aligned register
*/
*alignment = COM_HPCIO_HALFWORD_ALIGNMENT;
com_hpcio_iot_init(&tmpiot, iot);
if (bus_space_map(&tmpiot, iobase, 1, 0, &ioh))
return 0;
rv = comprobe1(&tmpiot, ioh);
bus_space_unmap(&tmpiot, ioh, 1);
return (rv);
}
static int
com_hpcio_probe(device_t parent, cfdata_t cf, void *aux)
{
struct hpcio_attach_args *haa = aux;
bus_space_tag_t iot = haa->haa_iot;
int addr, alignment;
if (cf->cf_loc[HPCIOIFCF_PLATFORM] != HPCIOIFCF_PLATFORM_DEFAULT) {
platid_mask_t mask;
mask = PLATID_DEREF(cf->cf_loc[HPCIOIFCF_PLATFORM]);
if (!platid_match(&platid, &mask))
return (0); /* platform id didn't match */
}
if ((addr = cf->cf_loc[HPCIOIFCF_ADDR]) == HPCIOIFCF_ADDR_DEFAULT)
return (0); /* address wasn't specified */
return com_hpcio_common_probe(iot, addr, &alignment);
}
static void
com_hpcio_attach(device_t parent, device_t self, void *aux)
{
struct com_hpcio_softc *hsc = device_private(self);
struct com_softc *sc = &hsc->hsc_com;
struct hpcio_attach_args *haa = aux;
bus_space_tag_t iot;
bus_space_handle_t ioh;
int addr, port, mode, alignment, *loc;
sc->sc_dev = self;
loc = device_cfdata(sc->sc_dev)->cf_loc;
addr = loc[HPCIOIFCF_ADDR];
aprint_normal(" addr %x", addr);
if ((com_hpcio_cniot == haa->haa_iot ||
com_hpcio_cniot->bs_base == haa->haa_iot) &&
com_hpcio_cniobase == addr &&
com_is_console(com_hpcio_cniot, addr, 0)) {
iot = com_hpcio_cniot;
if (com_hpcio_cniot->bs_base == haa->haa_iot)
aprint_normal(", half word aligned");
} else {
com_hpcio_common_probe(haa->haa_iot, addr, &alignment);
if (alignment == COM_HPCIO_HALFWORD_ALIGNMENT) {
aprint_normal(", half word aligned");
iot = &hsc->hsc_iot;
com_hpcio_iot_init(iot, haa->haa_iot);
} else {
iot = haa->haa_iot;
}
}
if (bus_space_map(iot, addr, 1, 0, &ioh)) {
aprint_error(": can't map bus space\n");
return;
}
com_init_regs(&sc->sc_regs, iot, ioh, addr);
sc->enable = NULL;
sc->disable = NULL;
sc->sc_frequency = COM_FREQ;
com_attach_subr(sc);
hsc->hsc_hc = (*haa->haa_getchip)(haa->haa_sc, loc[HPCIOIFCF_IOCHIP]);
port = loc[HPCIOIFCF_PORT];
mode = HPCIO_INTR_LEVEL | HPCIO_INTR_HIGH;
hpcio_intr_establish(hsc->hsc_hc, port, mode, comintr, sc);
}
/*
* bus stuff (registershalf word aligned)
*/
void
com_hpcio_iot_init(bus_space_tag_t iot, bus_space_tag_t base)
{
iot->bs_base = base;
iot->bs_ops = com_hpcio_bs_ops; /* structure assignment */
iot->bs_ops.bs_r_1 = com_hpcio_bs_r_1;
iot->bs_ops.bs_w_1 = com_hpcio_bs_w_1;
iot->bs_ops.bs_wm_1 = com_hpcio_bs_wm_1;
}
u_int8_t
com_hpcio_bs_r_1(bus_space_tag_t t, bus_space_handle_t bsh,
bus_size_t offset)
{
return bus_space_read_1(t->bs_base, bsh, offset * 2);
}
void
com_hpcio_bs_w_1(bus_space_tag_t t, bus_space_handle_t bsh,
bus_size_t offset, u_int8_t value)
{
bus_space_write_1(t->bs_base, bsh, offset * 2, value);
}
void
com_hpcio_bs_wm_1(bus_space_tag_t t, bus_space_handle_t bsh,
bus_size_t offset, const u_int8_t *addr, bus_size_t count)
{
bus_space_write_multi_1(t->bs_base, bsh, offset * 2, addr, count);
}
|