1/*	$NetBSD: i80321_space.c,v 1.6 2003/10/06 15:43:35 thorpej Exp $	*/
2
3/*-
4 * Copyright (c) 2001, 2002 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 *    must display the following acknowledgement:
19 *	This product includes software developed for the NetBSD Project by
20 *	Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 *    or promote products derived from this software without specific prior
23 *    written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38/*
39 * bus_space functions for i81342 I/O Processor.
40 */
41
42#include <sys/cdefs.h>
43__FBSDID("$FreeBSD$");
44
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/bus.h>
48#include <sys/malloc.h>
49
50#include <machine/pcb.h>
51
52#include <vm/vm.h>
53#include <vm/vm_kern.h>
54#include <vm/pmap.h>
55#include <vm/vm_page.h>
56#include <vm/vm_extern.h>
57
58#include <machine/bus.h>
59
60#include <arm/xscale/i8134x/i81342reg.h>
61#include <arm/xscale/i8134x/i81342var.h>
62
63/* Prototypes for all the bus_space structure functions */
64bs_protos(i81342);
65bs_protos(i81342_io);
66bs_protos(i81342_mem);
67bs_protos(generic);
68bs_protos(generic_armv4);
69
70/*
71 * Template bus_space -- copied, and the bits that are NULL are
72 * filled in.
73 */
74const struct bus_space i81342_bs_tag_template = {
75	/* cookie */
76	(void *) 0,
77
78	/* mapping/unmapping */
79	NULL,
80	NULL,
81	i81342_bs_subregion,
82
83	/* allocation/deallocation */
84	NULL,
85	NULL,
86
87	/* barrier */
88	i81342_bs_barrier,
89
90	/* read (single) */
91	generic_bs_r_1,
92	generic_armv4_bs_r_2,
93	generic_bs_r_4,
94	NULL,
95
96	/* read multiple */
97	generic_bs_rm_1,
98	generic_armv4_bs_rm_2,
99	generic_bs_rm_4,
100	NULL,
101
102	/* read region */
103	generic_bs_rr_1,
104	generic_armv4_bs_rr_2,
105	generic_bs_rr_4,
106	NULL,
107
108	/* write (single) */
109	generic_bs_w_1,
110	generic_armv4_bs_w_2,
111	generic_bs_w_4,
112	NULL,
113
114	/* write multiple */
115	generic_bs_wm_1,
116	generic_armv4_bs_wm_2,
117	generic_bs_wm_4,
118	NULL,
119
120	/* write region */
121	NULL,
122	generic_armv4_bs_wr_2,
123	generic_bs_wr_4,
124	NULL,
125
126	/* set multiple */
127	NULL,
128	NULL,
129	NULL,
130	NULL,
131
132	/* set region */
133	NULL,
134	generic_armv4_bs_sr_2,
135	generic_bs_sr_4,
136	NULL,
137
138	/* copy */
139	NULL,
140	generic_armv4_bs_c_2,
141	NULL,
142	NULL,
143
144	/* read (single) stream */
145	generic_bs_r_1,
146	generic_armv4_bs_r_2,
147	generic_bs_r_4,
148	NULL,
149
150	/* read multiple stream */
151	generic_bs_rm_1,
152	generic_armv4_bs_rm_2,
153	generic_bs_rm_4,
154	NULL,
155
156	/* read region stream */
157	generic_bs_rr_1,
158	generic_armv4_bs_rr_2,
159	generic_bs_rr_4,
160	NULL,
161
162	/* write (single) stream */
163	generic_bs_w_1,
164	generic_armv4_bs_w_2,
165	generic_bs_w_4,
166	NULL,
167
168	/* write multiple stream */
169	generic_bs_wm_1,
170	generic_armv4_bs_wm_2,
171	generic_bs_wm_4,
172	NULL,
173
174	/* write region stream */
175	NULL,
176	generic_armv4_bs_wr_2,
177	generic_bs_wr_4,
178	NULL,
179};
180
181void
182i81342_bs_init(bus_space_tag_t bs, void *cookie)
183{
184
185	*bs = i81342_bs_tag_template;
186	bs->bs_cookie = cookie;
187}
188
189void
190i81342_io_bs_init(bus_space_tag_t bs, void *cookie)
191{
192
193	*bs = i81342_bs_tag_template;
194	bs->bs_cookie = cookie;
195
196	bs->bs_map = i81342_io_bs_map;
197	bs->bs_unmap = i81342_io_bs_unmap;
198	bs->bs_alloc = i81342_io_bs_alloc;
199	bs->bs_free = i81342_io_bs_free;
200
201}
202
203void
204i81342_mem_bs_init(bus_space_tag_t bs, void *cookie)
205{
206
207	*bs = i81342_bs_tag_template;
208	bs->bs_cookie = cookie;
209
210	bs->bs_map = i81342_mem_bs_map;
211	bs->bs_unmap = i81342_mem_bs_unmap;
212	bs->bs_alloc = i81342_mem_bs_alloc;
213	bs->bs_free = i81342_mem_bs_free;
214
215}
216
217/* *** Routines shared by i81342, PCI IO, and PCI MEM. *** */
218
219int
220i81342_bs_subregion(void *t, bus_space_handle_t bsh, bus_size_t offset,
221    bus_size_t size, bus_space_handle_t *nbshp)
222{
223
224	*nbshp = bsh + offset;
225	return (0);
226}
227
228void
229i81342_bs_barrier(void *t, bus_space_handle_t bsh, bus_size_t offset,
230    bus_size_t len, int flags)
231{
232
233	/* Nothing to do. */
234}
235
236/* *** Routines for PCI IO. *** */
237
238int
239i81342_io_bs_map(void *t, bus_addr_t bpa, bus_size_t size, int flags,
240    bus_space_handle_t *bshp)
241{
242
243	*bshp = bpa;
244	return (0);
245}
246
247void
248i81342_io_bs_unmap(void *t, bus_space_handle_t h, bus_size_t size)
249{
250
251	/* Nothing to do. */
252}
253
254int
255i81342_io_bs_alloc(void *t, bus_addr_t rstart, bus_addr_t rend,
256    bus_size_t size, bus_size_t alignment, bus_size_t boundary, int flags,
257    bus_addr_t *bpap, bus_space_handle_t *bshp)
258{
259
260	panic("i81342_io_bs_alloc(): not implemented");
261}
262
263void
264i81342_io_bs_free(void *t, bus_space_handle_t bsh, bus_size_t size)
265{
266
267	panic("i81342_io_bs_free(): not implemented");
268}
269
270
271/* *** Routines for PCI MEM. *** */
272extern int badaddr_read(void *, int, void *);
273static vm_offset_t allocable = 0xe1000000;
274int
275i81342_mem_bs_map(void *t, bus_addr_t bpa, bus_size_t size, int flags,
276    bus_space_handle_t *bshp)
277{
278	struct i81342_pci_softc *sc = (struct i81342_pci_softc *)t;
279	struct i81342_pci_map *tmp;
280	vm_offset_t addr, endaddr;
281	vm_paddr_t paddr;
282
283	/* Lookup to see if we already have a mapping at this address. */
284	tmp = sc->sc_pci_mappings;
285	while (tmp) {
286		if (tmp->paddr <= bpa && tmp->paddr + tmp->size >
287		    bpa + size) {
288			*bshp = bpa - tmp->paddr + tmp->vaddr;
289			return (0);
290		}
291		tmp = tmp->next;
292	}
293	addr = allocable;
294	endaddr = ((addr + size) &~ (0x1000000 - 1)) + 0x1000000;
295	if (endaddr >= IOP34X_VADDR)
296		panic("PCI virtual memory exhausted");
297	allocable = endaddr;
298	tmp = malloc(sizeof(*tmp), M_DEVBUF, M_WAITOK);
299	tmp->next = NULL;
300	paddr = bpa &~ (0x100000 - 1);
301	tmp->paddr = paddr;
302	tmp->vaddr = addr;
303	tmp->size = 0;
304	while (addr < endaddr) {
305		pmap_kenter_supersection(addr, paddr + (sc->sc_is_atux ?
306		    IOP34X_PCIX_OMBAR : IOP34X_PCIE_OMBAR), 0);
307		addr += 0x1000000;
308		paddr += 0x1000000;
309		tmp->size += 0x1000000;
310	}
311	tmp->next = sc->sc_pci_mappings;
312	sc->sc_pci_mappings = tmp;
313	*bshp = bpa - tmp->paddr + tmp->vaddr;
314	return (0);
315}
316
317void
318i81342_mem_bs_unmap(void *t, bus_space_handle_t h, bus_size_t size)
319{
320#if 0
321	vm_offset_t va, endva;
322
323	va = trunc_page((vm_offset_t)t);
324	endva = va + round_page(size);
325
326	/* Free the kernel virtual mapping. */
327	kmem_free(kernel_map, va, endva - va);
328#endif
329}
330
331int
332i81342_mem_bs_alloc(void *t, bus_addr_t rstart, bus_addr_t rend,
333    bus_size_t size, bus_size_t alignment, bus_size_t boundary, int flags,
334    bus_addr_t *bpap, bus_space_handle_t *bshp)
335{
336
337	panic("i81342_mem_bs_alloc(): not implemented");
338}
339
340void
341i81342_mem_bs_free(void *t, bus_space_handle_t bsh, bus_size_t size)
342{
343
344	panic("i81342_mem_bs_free(): not implemented");
345}
346