Home | History | Annotate | Line # | Download | only in xscale
i80312_space.c revision 1.1
      1 /*	$NetBSD: i80312_space.c,v 1.1 2001/11/09 03:27:51 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001 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 i80312 Companion I/O chip.
     40  */
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 
     45 #include <uvm/uvm_extern.h>
     46 
     47 #include <machine/bus.h>
     48 
     49 #include <arm/xscale/i80312var.h>
     50 
     51 /* Prototypes for all the bus_space structure functions */
     52 bs_protos(i80312);
     53 bs_protos(i80312_io);
     54 bs_protos(i80312_mem);
     55 bs_protos(bs_notimpl);
     56 
     57 /*
     58  * Template bus_space -- copied, and the bits that are NULL are
     59  * filled in.
     60  */
     61 const struct bus_space i80312_bs_tag_template = {
     62 	/* cookie */
     63 	(void *) 0,
     64 
     65 	/* mapping/unmapping */
     66 	NULL,
     67 	NULL,
     68 	i80312_bs_subregion,
     69 
     70 	/* allocation/deallocation */
     71 	NULL,
     72 	NULL,
     73 
     74 	/* get kernel virtual address */
     75 	i80312_bs_vaddr,
     76 
     77 	/* mmap */
     78 	i80312_bs_mmap,
     79 
     80 	/* barrier */
     81 	i80312_bs_barrier,
     82 
     83 	/* read (single) */
     84 	i80312_bs_r_1,
     85 	i80312_bs_r_2,
     86 	i80312_bs_r_4,
     87 	bs_notimpl_bs_r_8,
     88 
     89 	/* read multiple */
     90 	i80312_bs_rm_1,
     91 	i80312_bs_rm_2,
     92 	i80312_bs_rm_4,
     93 	bs_notimpl_bs_rm_8,
     94 
     95 	/* read region */
     96 	bs_notimpl_bs_rr_1,
     97 	i80312_bs_rr_2,
     98 	i80312_bs_rr_4,
     99 	bs_notimpl_bs_rr_8,
    100 
    101 	/* write (single) */
    102 	i80312_bs_w_1,
    103 	i80312_bs_w_2,
    104 	i80312_bs_w_4,
    105 	bs_notimpl_bs_w_8,
    106 
    107 	/* write multiple */
    108 	i80312_bs_wm_1,
    109 	i80312_bs_wm_2,
    110 	i80312_bs_wm_4,
    111 	bs_notimpl_bs_wm_8,
    112 
    113 	/* write region */
    114 	bs_notimpl_bs_wr_1,
    115 	i80312_bs_wr_2,
    116 	i80312_bs_wr_4,
    117 	bs_notimpl_bs_wr_8,
    118 
    119 	/* set multiple */
    120 	bs_notimpl_bs_sm_1,
    121 	bs_notimpl_bs_sm_2,
    122 	bs_notimpl_bs_sm_4,
    123 	bs_notimpl_bs_sm_8,
    124 
    125 	/* set region */
    126 	bs_notimpl_bs_sr_1,
    127 	i80312_bs_sr_2,
    128 	bs_notimpl_bs_sr_4,
    129 	bs_notimpl_bs_sr_8,
    130 
    131 	/* copy */
    132 	bs_notimpl_bs_c_1,
    133 	i80312_bs_c_2,
    134 	bs_notimpl_bs_c_4,
    135 	bs_notimpl_bs_c_8,
    136 };
    137 
    138 void
    139 i80312_bs_init(bus_space_tag_t bs, void *cookie)
    140 {
    141 
    142 	*bs = i80312_bs_tag_template;
    143 	bs->bs_cookie = cookie;
    144 }
    145 
    146 void
    147 i80312_io_bs_init(bus_space_tag_t bs, void *cookie)
    148 {
    149 
    150 	*bs = i80312_bs_tag_template;
    151 	bs->bs_cookie = cookie;
    152 
    153 	bs->bs_map = i80312_io_bs_map;
    154 	bs->bs_unmap = i80312_io_bs_unmap;
    155 	bs->bs_alloc = i80312_io_bs_alloc;
    156 	bs->bs_free = i80312_io_bs_free;
    157 
    158 	bs->bs_vaddr = i80312_io_bs_vaddr;
    159 }
    160 
    161 void
    162 i80312_mem_bs_init(bus_space_tag_t bs, void *cookie)
    163 {
    164 
    165 	*bs = i80312_bs_tag_template;
    166 	bs->bs_cookie = cookie;
    167 
    168 	bs->bs_map = i80312_mem_bs_map;
    169 	bs->bs_unmap = i80312_mem_bs_unmap;
    170 	bs->bs_alloc = i80312_mem_bs_alloc;
    171 	bs->bs_free = i80312_mem_bs_free;
    172 
    173 	bs->bs_mmap = i80312_mem_bs_mmap;
    174 }
    175 
    176 /* *** Routines shared by i80312, PCI IO, and PCI MEM. *** */
    177 
    178 int
    179 i80312_bs_subregion(void *t, bus_space_handle_t bsh, bus_size_t offset,
    180     bus_size_t size, bus_space_handle_t *nbshp)
    181 {
    182 
    183 	*nbshp = bsh + offset;
    184 	return (0);
    185 }
    186 
    187 void
    188 i80312_bs_barrier(void *t, bus_space_handle_t bsh, bus_size_t offset,
    189     bus_size_t len, int flags)
    190 {
    191 
    192 	/* Nothing to do. */
    193 }
    194 
    195 void *
    196 i80312_bs_vaddr(void *t, bus_space_handle_t bsh)
    197 {
    198 
    199 	return ((void *)bsh);
    200 }
    201 
    202 paddr_t
    203 i80312_bs_mmap(void *t, bus_addr_t addr, off_t off, int prot, int flags)
    204 {
    205 
    206 	/* Not supported. */
    207 	return (-1);
    208 }
    209 
    210 /* *** Routines for PCI IO. *** */
    211 
    212 int
    213 i80312_io_bs_map(void *t, bus_addr_t bpa, bus_size_t size, int flags,
    214     bus_space_handle_t *bshp)
    215 {
    216 	struct i80312_softc *sc = t;
    217 	vaddr_t winvaddr;
    218 	uint32_t busbase, bussize;
    219 
    220 	if (bpa >= sc->sc_pioout_base &&
    221 	    bpa < (sc->sc_pioout_base + sc->sc_pioout_size)) {
    222 		busbase = sc->sc_pioout_base;
    223 		bussize = sc->sc_pioout_size;
    224 		winvaddr = sc->sc_piow_vaddr;
    225 	} else if (bpa >= sc->sc_sioout_base &&
    226 		   bpa < (sc->sc_sioout_base + sc->sc_sioout_size)) {
    227 		busbase = sc->sc_sioout_base;
    228 		bussize = sc->sc_sioout_size;
    229 		winvaddr = sc->sc_siow_vaddr;
    230 	} else
    231 		return (EINVAL);
    232 
    233 	if ((bpa + size) >= (busbase + bussize))
    234 		return (EINVAL);
    235 
    236 	/*
    237 	 * Found the window -- PCI I/O space is mapped at a fixed
    238 	 * virtual address by board-specific code.  Translate the
    239 	 * bus address to the virtual address.
    240 	 */
    241 	*bshp = winvaddr + (bpa - busbase);
    242 
    243 	return (0);
    244 }
    245 
    246 void
    247 i80312_io_bs_unmap(void *t, bus_space_handle_t bsh, bus_size_t size)
    248 {
    249 
    250 	/* Nothing to do. */
    251 }
    252 
    253 int
    254 i80312_io_bs_alloc(void *t, bus_addr_t rstart, bus_addr_t rend,
    255     bus_size_t size, bus_size_t alignment, bus_size_t boundary, int flags,
    256     bus_addr_t *bpap, bus_space_handle_t *bshp)
    257 {
    258 
    259 	panic("i80312_io_bs_alloc(): not implemented\n");
    260 }
    261 
    262 void
    263 i80312_io_bs_free(void *t, bus_space_handle_t bsh, bus_size_t size)
    264 {
    265 
    266 	panic("i80312_io_bs_free(): not implemented\n");
    267 }
    268 
    269 void *
    270 i80312_io_bs_vaddr(void *t, bus_space_handle_t bsh)
    271 {
    272 
    273 	/* Not supported. */
    274 	return (NULL);
    275 }
    276 
    277 /* *** Routines for PCI MEM. *** */
    278 
    279 int
    280 i80312_mem_bs_map(void *t, bus_addr_t bpa, bus_size_t size, int flags,
    281     bus_space_handle_t *bshp)
    282 {
    283 
    284 	struct i80312_softc *sc = t;
    285 	vaddr_t va;
    286 	uint32_t busbase, bussize;
    287 	paddr_t pa, endpa;
    288 
    289 	if (bpa >= sc->sc_pmemout_base &&
    290 	    bpa < (sc->sc_pmemout_base + sc->sc_pmemout_size)) {
    291 		busbase = sc->sc_pmemout_base;
    292 		bussize = sc->sc_pmemout_size;
    293 	} else if (bpa >= sc->sc_smemout_base &&
    294 		   bpa < (sc->sc_smemout_base + sc->sc_smemout_size)) {
    295 		busbase = sc->sc_smemout_base;
    296 		bussize = sc->sc_smemout_size;
    297 	} else
    298 		return (EINVAL);
    299 
    300 	if ((bpa + size) >= (busbase + bussize))
    301 		return (EINVAL);
    302 
    303 	/*
    304 	 * Found the window -- PCI MEM space is not mapped by allocating
    305 	 * some kernel VA space and mapping the pages with pmap_enter().
    306 	 * pmap_enter() will map unmanaged pages as non-cacheable.
    307 	 */
    308 	pa = trunc_page(bpa - busbase);
    309 	endpa = round_page((bpa - busbase) + size);
    310 
    311 	va = uvm_km_valloc(kernel_map, endpa - pa);
    312 	if (va == 0)
    313 		return (ENOMEM);
    314 
    315 	*bshp = va + (bpa & PAGE_MASK);
    316 
    317 	for (; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE) {
    318 		pmap_enter(pmap_kernel(), va, pa,
    319 		    VM_PROT_READ | VM_PROT_WRITE, PMAP_WIRED);
    320 	}
    321 	pmap_update(pmap_kernel());
    322 
    323 	return (0);
    324 }
    325 
    326 void
    327 i80312_mem_bs_unmap(void *t, bus_space_handle_t bsh, bus_size_t size)
    328 {
    329 	vaddr_t va, endva;
    330 
    331 	va = trunc_page(bsh);
    332 	endva = round_page(bsh + size);
    333 
    334 	/* Free the kernel virtual mapping. */
    335 	uvm_km_free(kernel_map, va, endva - va);
    336 }
    337 
    338 int
    339 i80312_mem_bs_alloc(void *t, bus_addr_t rstart, bus_addr_t rend,
    340     bus_size_t size, bus_size_t alignment, bus_size_t boundary, int flags,
    341     bus_addr_t *bpap, bus_space_handle_t *bshp)
    342 {
    343 
    344 	panic("i80312_mem_bs_alloc(): not implemented\n");
    345 }
    346 
    347 void
    348 i80312_mem_bs_free(void *t, bus_space_handle_t bsh, bus_size_t size)
    349 {
    350 
    351 	panic("i80312_mem_bs_free(): not implemented\n");
    352 }
    353 
    354 paddr_t
    355 i80312_mem_bs_mmap(void *t, bus_addr_t addr, off_t off, int prot, int flags)
    356 {
    357 
    358 	/* XXX */
    359 	return (-1);
    360 }
    361