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);
67
68void
69i81342_bs_init(bus_space_tag_t bs, void *cookie)
70{
71
72	*bs = *arm_base_bs_tag;
73	bs->bs_privdata = cookie;
74}
75
76void
77i81342_io_bs_init(bus_space_tag_t bs, void *cookie)
78{
79
80	*bs = *arm_base_bs_tag;
81	bs->bs_privdata = cookie;
82
83	bs->bs_map = i81342_io_bs_map;
84	bs->bs_unmap = i81342_io_bs_unmap;
85	bs->bs_alloc = i81342_io_bs_alloc;
86	bs->bs_free = i81342_io_bs_free;
87
88}
89
90void
91i81342_mem_bs_init(bus_space_tag_t bs, void *cookie)
92{
93
94	*bs = *arm_base_bs_tag;
95	bs->bs_privdata = cookie;
96
97	bs->bs_map = i81342_mem_bs_map;
98	bs->bs_unmap = i81342_mem_bs_unmap;
99	bs->bs_alloc = i81342_mem_bs_alloc;
100	bs->bs_free = i81342_mem_bs_free;
101
102}
103
104/* *** Routines shared by i81342, PCI IO, and PCI MEM. *** */
105
106int
107i81342_bs_subregion(bus_space_tag_t tag, bus_space_handle_t bsh, bus_size_t offset,
108    bus_size_t size, bus_space_handle_t *nbshp)
109{
110
111	*nbshp = bsh + offset;
112	return (0);
113}
114
115void
116i81342_bs_barrier(bus_space_tag_t tag, bus_space_handle_t bsh, bus_size_t offset,
117    bus_size_t len, int flags)
118{
119
120	/* Nothing to do. */
121}
122
123/* *** Routines for PCI IO. *** */
124
125int
126i81342_io_bs_map(bus_space_tag_t tag, bus_addr_t bpa, bus_size_t size, int flags,
127    bus_space_handle_t *bshp)
128{
129
130	*bshp = bpa;
131	return (0);
132}
133
134void
135i81342_io_bs_unmap(bus_space_tag_t tag, bus_space_handle_t h, bus_size_t size)
136{
137
138	/* Nothing to do. */
139}
140
141int
142i81342_io_bs_alloc(bus_space_tag_t tag, bus_addr_t rstart, bus_addr_t rend,
143    bus_size_t size, bus_size_t alignment, bus_size_t boundary, int flags,
144    bus_addr_t *bpap, bus_space_handle_t *bshp)
145{
146
147	panic("i81342_io_bs_alloc(): not implemented");
148}
149
150void
151i81342_io_bs_free(bus_space_tag_t tag, bus_space_handle_t bsh, bus_size_t size)
152{
153
154	panic("i81342_io_bs_free(): not implemented");
155}
156
157
158/* *** Routines for PCI MEM. *** */
159extern int badaddr_read(void *, int, void *);
160static vm_offset_t allocable = 0xe1000000;
161int
162i81342_mem_bs_map(bus_space_tag_t tag, bus_addr_t bpa, bus_size_t size, int flags,
163    bus_space_handle_t *bshp)
164{
165	struct i81342_pci_softc *sc = (struct i81342_pci_softc *)tag->bs_privdata;
166	struct i81342_pci_map *tmp;
167	vm_offset_t addr, endaddr;
168	vm_paddr_t paddr;
169
170	/* Lookup to see if we already have a mapping at this address. */
171	tmp = sc->sc_pci_mappings;
172	while (tmp) {
173		if (tmp->paddr <= bpa && tmp->paddr + tmp->size >
174		    bpa + size) {
175			*bshp = bpa - tmp->paddr + tmp->vaddr;
176			return (0);
177		}
178		tmp = tmp->next;
179	}
180	addr = allocable;
181	endaddr = rounddown2(addr + size, 0x1000000) + 0x1000000;
182	if (endaddr >= IOP34X_VADDR)
183		panic("PCI virtual memory exhausted");
184	allocable = endaddr;
185	tmp = malloc(sizeof(*tmp), M_DEVBUF, M_WAITOK);
186	tmp->next = NULL;
187	paddr = rounddown2(bpa, 0x100000);
188	tmp->paddr = paddr;
189	tmp->vaddr = addr;
190	tmp->size = 0;
191	while (addr < endaddr) {
192		pmap_kenter_supersection(addr, paddr + (sc->sc_is_atux ?
193		    IOP34X_PCIX_OMBAR : IOP34X_PCIE_OMBAR), 0);
194		addr += 0x1000000;
195		paddr += 0x1000000;
196		tmp->size += 0x1000000;
197	}
198	tmp->next = sc->sc_pci_mappings;
199	sc->sc_pci_mappings = tmp;
200	*bshp = bpa - tmp->paddr + tmp->vaddr;
201	return (0);
202}
203
204void
205i81342_mem_bs_unmap(bus_space_tag_t tag, bus_space_handle_t h, bus_size_t size)
206{
207#if 0
208	vm_offset_t va, endva;
209
210	va = trunc_page((vm_offset_t)h);
211	endva = va + round_page(size);
212
213	/* Free the kernel virtual mapping. */
214	kva_free(va, endva - va);
215#endif
216}
217
218int
219i81342_mem_bs_alloc(bus_space_tag_t tag, bus_addr_t rstart, bus_addr_t rend,
220    bus_size_t size, bus_size_t alignment, bus_size_t boundary, int flags,
221    bus_addr_t *bpap, bus_space_handle_t *bshp)
222{
223
224	panic("i81342_mem_bs_alloc(): not implemented");
225}
226
227void
228i81342_mem_bs_free(bus_space_tag_t tag, bus_space_handle_t bsh, bus_size_t size)
229{
230
231	panic("i81342_mem_bs_free(): not implemented");
232}
233