summaryrefslogtreecommitdiffstats
path: root/sys/pc98/pc98/busiosubr.c
blob: 926c1ce88e85213fa75b195882a2a532fe5bb3c3 (plain)
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
/* $FreeBSD$ */
/*	$NecBSD: busiosubr.c,v 1.30.4.4 1999/08/28 02:25:35 honda Exp $	*/
/*	$NetBSD$	*/

/*-
 * [NetBSD for NEC PC-98 series]
 *  Copyright (c) 1996, 1997, 1998
 *	NetBSD/pc98 porting staff. All rights reserved.
 *
 * [Ported for FreeBSD]
 *  Copyright (c) 2001
 *	TAKAHASHI Yoshihiro. 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. The name of the author may not be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 * 
 * 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.
 */

/*
 * Copyright (c) 1997, 1998
 *	Naofumi HONDA.  All rights reserved.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <machine/bus.h>

static MALLOC_DEFINE(M_BUSSPACEHANDLE, "busspacehandle", "Bus space handle");

_BUS_SPACE_CALL_FUNCS_PROTO(SBUS_DA_io,u_int8_t,1)
_BUS_SPACE_CALL_FUNCS_PROTO(SBUS_DA_io,u_int16_t,2)
_BUS_SPACE_CALL_FUNCS_PROTO(SBUS_DA_io,u_int32_t,4)
_BUS_SPACE_CALL_FUNCS_PROTO(SBUS_DA_mem,u_int8_t,1)
_BUS_SPACE_CALL_FUNCS_PROTO(SBUS_DA_mem,u_int16_t,2)
_BUS_SPACE_CALL_FUNCS_PROTO(SBUS_DA_mem,u_int32_t,4)

_BUS_SPACE_CALL_FUNCS_PROTO(SBUS_RA_io,u_int8_t,1)
_BUS_SPACE_CALL_FUNCS_PROTO(SBUS_RA_io,u_int16_t,2)
_BUS_SPACE_CALL_FUNCS_PROTO(SBUS_RA_io,u_int32_t,4)
_BUS_SPACE_CALL_FUNCS_PROTO(SBUS_RA_mem,u_int8_t,1)
_BUS_SPACE_CALL_FUNCS_PROTO(SBUS_RA_mem,u_int16_t,2)
_BUS_SPACE_CALL_FUNCS_PROTO(SBUS_RA_mem,u_int32_t,4)

struct bus_space_tag SBUS_io_space_tag = {
	BUS_SPACE_TAG_IO,

	/* direct bus access methods */
	{
		_BUS_SPACE_CALL_FUNCS_TAB(SBUS_DA_io,u_int8_t,1),
		_BUS_SPACE_CALL_FUNCS_TAB(SBUS_DA_io,u_int16_t,2),
		_BUS_SPACE_CALL_FUNCS_TAB(SBUS_DA_io,u_int32_t,4),
	},

	/* relocate bus access methods */
	{
		_BUS_SPACE_CALL_FUNCS_TAB(SBUS_RA_io,u_int8_t,1),
		_BUS_SPACE_CALL_FUNCS_TAB(SBUS_RA_io,u_int16_t,2),
		_BUS_SPACE_CALL_FUNCS_TAB(SBUS_RA_io,u_int32_t,4),
	}
};

struct bus_space_tag SBUS_mem_space_tag = {
	BUS_SPACE_TAG_MEM,

	/* direct bus access methods */
	{
		_BUS_SPACE_CALL_FUNCS_TAB(SBUS_DA_mem,u_int8_t,1),
		_BUS_SPACE_CALL_FUNCS_TAB(SBUS_DA_mem,u_int16_t,2),
		_BUS_SPACE_CALL_FUNCS_TAB(SBUS_DA_mem,u_int32_t,4),
	},

	/* relocate bus access methods */
	{
		_BUS_SPACE_CALL_FUNCS_TAB(SBUS_RA_mem,u_int8_t,1),
		_BUS_SPACE_CALL_FUNCS_TAB(SBUS_RA_mem,u_int16_t,2),
		_BUS_SPACE_CALL_FUNCS_TAB(SBUS_RA_mem,u_int32_t,4),
	}
};


#include "opt_mecia.h"
#ifdef DEV_MECIA

_BUS_SPACE_CALL_FUNCS_PROTO(NEPC_DA_io,u_int16_t,2)
_BUS_SPACE_CALL_FUNCS_PROTO(NEPC_DA_io,u_int32_t,4)

_BUS_SPACE_CALL_FUNCS_PROTO(NEPC_RA_io,u_int16_t,2)
_BUS_SPACE_CALL_FUNCS_PROTO(NEPC_RA_io,u_int32_t,4)

struct bus_space_tag NEPC_io_space_tag = {
	BUS_SPACE_TAG_IO,

	/* direct bus access methods */
	{
		_BUS_SPACE_CALL_FUNCS_TAB(SBUS_DA_io,u_int8_t,1),
		_BUS_SPACE_CALL_FUNCS_TAB(NEPC_DA_io,u_int16_t,2),
		_BUS_SPACE_CALL_FUNCS_TAB(NEPC_DA_io,u_int32_t,4),
	},

	/* relocate bus access methods */
	{
		_BUS_SPACE_CALL_FUNCS_TAB(SBUS_RA_io,u_int8_t,1),
		_BUS_SPACE_CALL_FUNCS_TAB(NEPC_RA_io,u_int16_t,2),
		_BUS_SPACE_CALL_FUNCS_TAB(NEPC_RA_io,u_int32_t,4),
	}
};

struct bus_space_tag NEPC_mem_space_tag = {
	BUS_SPACE_TAG_MEM,

	/* direct bus access methods */
	{
		_BUS_SPACE_CALL_FUNCS_TAB(SBUS_DA_mem,u_int8_t,1),
		_BUS_SPACE_CALL_FUNCS_TAB(SBUS_DA_mem,u_int16_t,2),
		_BUS_SPACE_CALL_FUNCS_TAB(SBUS_DA_mem,u_int32_t,4),
	},

	/* relocate bus access methods */
	{
		_BUS_SPACE_CALL_FUNCS_TAB(SBUS_RA_mem,u_int8_t,1),
		_BUS_SPACE_CALL_FUNCS_TAB(SBUS_RA_mem,u_int16_t,2),
		_BUS_SPACE_CALL_FUNCS_TAB(SBUS_RA_mem,u_int32_t,4),
	}
};

#endif /* DEV_MECIA */

/*************************************************************************
 * map init
 *************************************************************************/
static __inline void
bus_space_iat_init(bus_space_handle_t bsh)
{
	int i;

	for (i = 0; i < bsh->bsh_maxiatsz; i++)
		bsh->bsh_iat[i] = bsh->bsh_base + i;
}

/*************************************************************************
 * handle allocation
 *************************************************************************/
int
i386_bus_space_handle_alloc(bus_space_tag_t t, bus_addr_t bpa, bus_size_t size,
			    bus_space_handle_t *bshp)
{
	bus_space_handle_t bsh;

	bsh = (bus_space_handle_t) malloc(sizeof (*bsh), M_BUSSPACEHANDLE,
					  M_NOWAIT | M_ZERO);
	if (bsh == NULL)
		return ENOMEM;

	bsh->bsh_maxiatsz = BUS_SPACE_IAT_MAXSIZE;
	bsh->bsh_iatsz = 0;
	bsh->bsh_base = bpa;
	bsh->bsh_sz = size;
	bsh->bsh_res = NULL;
	bsh->bsh_ressz = 0;
	bus_space_iat_init(bsh);

	bsh->bsh_bam = t->bs_da;		/* default: direct access */

	*bshp = bsh;
	return 0;
}

void
i386_bus_space_handle_free(bus_space_tag_t t, bus_space_handle_t bsh,
			   size_t size)
{

	free(bsh, M_BUSSPACEHANDLE);
}

/*************************************************************************
 * map
 *************************************************************************/
int
i386_memio_map(bus_space_tag_t t, bus_addr_t bpa, bus_size_t size, int flags,
	       bus_space_handle_t *bshp)
{

	return i386_bus_space_handle_alloc(t, bpa, size, bshp);
}

void
i386_memio_unmap(bus_space_tag_t t, bus_space_handle_t bsh, bus_size_t size)
{

	i386_bus_space_handle_free(t, bsh, bsh->bsh_sz);
}

void
i386_memio_free(bus_space_tag_t t, bus_space_handle_t bsh, bus_size_t size)
{

	/* i386_memio_unmap() does all that we need to do. */
	i386_memio_unmap(t, bsh, bsh->bsh_sz);
}

int
i386_memio_map_load(bus_space_tag_t t, bus_space_handle_t bsh,
		    bus_size_t size, bus_space_iat_t iat, u_int flags __unused)
{
	int i;

	if (size > bsh->bsh_maxiatsz) {
		printf("i386_memio_map_load: map size too large\n");
		return EINVAL;
	}

	for (i = 0; i < bsh->bsh_maxiatsz; i++) {
		if (i < size)
			bsh->bsh_iat[i] = iat[i];
		else
			bsh->bsh_iat[i] = 0;
		bsh->bsh_iat[i] += bsh->bsh_base;
	}

	bsh->bsh_iatsz = size;
	bsh->bsh_bam = t->bs_ra;	/* relocate access */

	return 0;
}

int
i386_memio_subregion(bus_space_tag_t t, bus_space_handle_t pbsh,
		     bus_size_t offset, bus_size_t size,
		     bus_space_handle_t *tbshp)
{
	int i, error = 0;
	bus_space_handle_t bsh;
	bus_addr_t pbase;

	pbase = pbsh->bsh_base + offset;
	switch (t->bs_tag) {
	case BUS_SPACE_TAG_IO:
		if (pbsh->bsh_iatsz > 0) {
			if (offset >= pbsh->bsh_iatsz || 
			    offset + size > pbsh->bsh_iatsz)
				return EINVAL;
			pbase = pbsh->bsh_base;
		}
		break;

	case BUS_SPACE_TAG_MEM:
		if (pbsh->bsh_iatsz > 0)
			return EINVAL;
		if (offset > pbsh->bsh_sz || offset + size > pbsh->bsh_sz)
			return EINVAL;
		break;

	default:
		panic("i386_memio_subregion: bad bus space tag");
		break;
	}

	error = i386_bus_space_handle_alloc(t, pbase, size, &bsh);
	if (error != 0)
		return error;

	switch (t->bs_tag) {
	case BUS_SPACE_TAG_IO:
		if (pbsh->bsh_iatsz > 0) {
			for (i = 0; i < size; i ++)
				bsh->bsh_iat[i] = pbsh->bsh_iat[i + offset];
			bsh->bsh_iatsz = size;
		} else if (pbsh->bsh_base > bsh->bsh_base ||
		         pbsh->bsh_base + pbsh->bsh_sz <
		         bsh->bsh_base + bsh->bsh_sz) {
			i386_bus_space_handle_free(t, bsh, size);
			return EINVAL;
		}
		break;

	case BUS_SPACE_TAG_MEM:
		break;
	}

	if (pbsh->bsh_iatsz > 0)
		bsh->bsh_bam = t->bs_ra;	/* relocate access */
	*tbshp = bsh;
	return error;
}

int
i386_memio_compare(bus_space_tag_t t1, bus_space_handle_t bsh1,
		   bus_space_tag_t t2, bus_space_handle_t bsh2)
{
	int i;

	if (t1->bs_tag != t2->bs_tag)
		return (1);
	if (bsh1->bsh_base != bsh2->bsh_base)
		return (1);
	if (bsh1->bsh_sz != bsh2->bsh_sz)
		return (1);
	if (bsh1->bsh_bam.bs_read_1 != bsh2->bsh_bam.bs_read_1)	/* XXX */
		return (1);

	if (bsh1->bsh_iatsz != bsh2->bsh_iatsz)
		return (1);
	for (i = 0; i < bsh1->bsh_iatsz; i++) {
		if (bsh1->bsh_iat[i] != bsh2->bsh_iat[i])
			return (1);
	}

	return (0);
}
OpenPOWER on IntegriCloud