1/*	$NetBSD: at91_bus_space.c,v 1.3 2009/10/23 06:53:12 snj Exp $ */
2
3/*
4 * Based on ep93xx_space.c
5 * Copyright (c) 2007 Embedtronics Oy
6 * All rights reserved.
7 *
8 * Copyright (c) 2004 Jesse Off
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY ICHIRO FUKUHARA ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL ICHIRO FUKUHARA OR THE VOICES IN HIS HEAD BE LIABLE FOR
24 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34__KERNEL_RCSID(0, "$NetBSD: at91_bus_space.c,v 1.3 2009/10/23 06:53:12 snj Exp $");
35
36/*
37 * bus_space I/O functions for ep93xx
38 */
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/queue.h>
43
44#include <uvm/uvm.h>
45
46#include <sys/bus.h>
47
48#include <arm/at91/at91var.h>
49//#include <arm/ep93xx/ep93xxreg.h>
50//#include <arm/ep93xx/ep93xxvar.h>
51
52/* Proto types for all the bus_space structure functions */
53bs_protos(at91);
54bs_protos(generic);
55bs_protos(generic_armv4);
56bs_protos(bs_notimpl);
57
58struct bus_space at91_bs_tag = {
59	/* cookie */
60	(void *) 0,
61
62	/* mapping/unmapping */
63	at91_bs_map,
64	at91_bs_unmap,
65	at91_bs_subregion,
66
67	/* allocation/deallocation */
68	at91_bs_alloc,
69	at91_bs_free,
70
71	/* get kernel virtual address */
72	at91_bs_vaddr,
73
74	/* mmap bus space for userland */
75	at91_bs_mmap,
76
77	/* barrier */
78	at91_bs_barrier,
79
80	/* read (single) */
81        generic_bs_r_1,
82        generic_armv4_bs_r_2,
83        generic_bs_r_4,
84        bs_notimpl_bs_r_8,
85
86        /* read multiple */
87        generic_bs_rm_1,
88        generic_armv4_bs_rm_2,
89        generic_bs_rm_4,
90        bs_notimpl_bs_rm_8,
91
92        /* read region */
93        generic_bs_rr_1,
94        generic_armv4_bs_rr_2,
95        generic_bs_rr_4,
96        bs_notimpl_bs_rr_8,
97
98        /* write (single) */
99        generic_bs_w_1,
100        generic_armv4_bs_w_2,
101        generic_bs_w_4,
102        bs_notimpl_bs_w_8,
103
104        /* write multiple */
105        generic_bs_wm_1,
106        generic_armv4_bs_wm_2,
107        generic_bs_wm_4,
108        bs_notimpl_bs_wm_8,
109
110        /* write region */
111        generic_bs_wr_1,
112        generic_armv4_bs_wr_2,
113        generic_bs_wr_4,
114        bs_notimpl_bs_wr_8,
115
116        /* set multiple */
117        bs_notimpl_bs_sm_1,
118        bs_notimpl_bs_sm_2,
119        bs_notimpl_bs_sm_4,
120        bs_notimpl_bs_sm_8,
121
122        /* set region */
123        bs_notimpl_bs_sr_1,
124        generic_armv4_bs_sr_2,
125        generic_bs_sr_4,
126        bs_notimpl_bs_sr_8,
127
128        /* copy */
129        bs_notimpl_bs_c_1,
130        generic_armv4_bs_c_2,
131        bs_notimpl_bs_c_4,
132        bs_notimpl_bs_c_8,
133};
134
135int
136at91_bs_map(void *t, bus_addr_t bpa, bus_size_t size,
137	      int cacheable, bus_space_handle_t *bshp)
138{
139	const struct pmap_devmap	*pd;
140
141	paddr_t		startpa;
142        paddr_t		endpa;
143        paddr_t		pa;
144        paddr_t		offset;
145        vaddr_t		va;
146
147	if ((pd = pmap_devmap_find_pa(bpa, size)) != NULL) {
148		/* Device was statically mapped. */
149		*bshp = pd->pd_va + (bpa - pd->pd_pa);
150		return 0;
151	}
152
153	endpa = round_page(bpa + size);
154	offset = bpa & PAGE_MASK;
155	startpa = trunc_page(bpa);
156
157	/* Get some VM.  */
158	va = uvm_km_alloc(kernel_map, endpa - startpa, 0,
159	    UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
160	if (va == 0)
161		return ENOMEM;
162
163	/* Store the bus space handle */
164	*bshp = va + offset;
165
166	/* Now map the pages */
167	for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE) {
168		pmap_enter(pmap_kernel(), va, pa,
169		    VM_PROT_READ | VM_PROT_WRITE,
170		    VM_PROT_READ | VM_PROT_WRITE | PMAP_WIRED);
171	}
172	pmap_update(pmap_kernel());
173
174	return(0);
175}
176
177void
178at91_bs_unmap(void *t, bus_space_handle_t bsh, bus_size_t size)
179{
180	vaddr_t	va;
181	vaddr_t	endva;
182
183	if (pmap_devmap_find_va(bsh, size) != NULL) {
184		/* Device was statically mapped; nothing to do. */
185		return;
186	}
187
188	endva = round_page(bsh + size);
189	va = trunc_page(bsh);
190
191	pmap_remove(pmap_kernel(), va, endva);
192	pmap_update(pmap_kernel());
193	uvm_km_free(kernel_map, va, endva - va, UVM_KMF_VAONLY);
194}
195
196int
197at91_bs_alloc(void *t, bus_addr_t rstart, bus_addr_t rend,
198	bus_size_t size, bus_size_t alignment, bus_size_t boundary, int cacheable,
199	bus_addr_t *bpap, bus_space_handle_t *bshp)
200{
201	panic("at91_bs_alloc(): not implemented\n");
202}
203
204void
205at91_bs_free(void *t, bus_space_handle_t bsh, bus_size_t size)
206{
207	panic("at91_bs_free(): not implemented\n");
208}
209
210int
211at91_bs_subregion(void *t, bus_space_handle_t bsh, bus_size_t offset,
212	bus_size_t size, bus_space_handle_t *nbshp)
213{
214	*nbshp = bsh + offset;
215	return (0);
216}
217
218void *
219at91_bs_vaddr(void *t, bus_space_handle_t bsh)
220{
221	return ((void *)bsh);
222}
223
224paddr_t
225at91_bs_mmap(void *t, bus_addr_t addr, off_t off, int prot, int flags)
226{
227	/* Not supported. */
228	return (-1);
229}
230
231void
232at91_bs_barrier(void *t, bus_space_handle_t bsh, bus_size_t offset,
233    bus_size_t len, int flags)
234{
235/* NULL */
236}
237/* End of at91_io.c */
238