Home | History | Annotate | Line # | Download | only in broadcom
bcmgen_space.c revision 1.2
      1  1.2  matt /*	$NetBSD: bcmgen_space.c,v 1.2 2013/01/09 17:45:13 matt Exp $	*/
      2  1.1  matt 
      3  1.1  matt /*-
      4  1.1  matt  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      5  1.1  matt  * All rights reserved.
      6  1.1  matt  *
      7  1.1  matt  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  matt  * by Nick Hudson
      9  1.1  matt  *
     10  1.1  matt  * Redistribution and use in source and binary forms, with or without
     11  1.1  matt  * modification, are permitted provided that the following conditions
     12  1.1  matt  * are met:
     13  1.1  matt  * 1. Redistributions of source code must retain the above copyright
     14  1.1  matt  *    notice, this list of conditions and the following disclaimer.
     15  1.1  matt  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  matt  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  matt  *    documentation and/or other materials provided with the distribution.
     18  1.1  matt  *
     19  1.1  matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  matt  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  matt  */
     31  1.1  matt 
     32  1.1  matt 
     33  1.1  matt #include <sys/cdefs.h>
     34  1.2  matt __KERNEL_RCSID(0, "$NetBSD: bcmgen_space.c,v 1.2 2013/01/09 17:45:13 matt Exp $");
     35  1.1  matt 
     36  1.1  matt #include <sys/param.h>
     37  1.1  matt #include <sys/systm.h>
     38  1.1  matt 
     39  1.1  matt #include <uvm/uvm_extern.h>
     40  1.1  matt 
     41  1.1  matt #include <sys/bus.h>
     42  1.1  matt 
     43  1.1  matt /* Prototypes for all the bus_space structure functions */
     44  1.1  matt bs_protos(bcmgen);
     45  1.1  matt bs_protos(generic);
     46  1.1  matt bs_protos(generic_armv4);
     47  1.1  matt bs_protos(bs_notimpl);
     48  1.1  matt 
     49  1.1  matt struct bus_space bcmgen_bs_tag = {
     50  1.1  matt 	/* cookie */
     51  1.1  matt 	(void *) 0,
     52  1.1  matt 
     53  1.1  matt 	/* mapping/unmapping */
     54  1.1  matt 	bcmgen_bs_map,
     55  1.1  matt 	bcmgen_bs_unmap,
     56  1.1  matt 	bcmgen_bs_subregion,
     57  1.1  matt 
     58  1.1  matt 	/* allocation/deallocation */
     59  1.1  matt 	bcmgen_bs_alloc,	/* not implemented */
     60  1.1  matt 	bcmgen_bs_free,	/* not implemented */
     61  1.1  matt 
     62  1.1  matt 	/* get kernel virtual address */
     63  1.1  matt 	bcmgen_bs_vaddr,
     64  1.1  matt 
     65  1.1  matt 	/* mmap */
     66  1.1  matt 	bs_notimpl_bs_mmap,
     67  1.1  matt 
     68  1.1  matt 	/* barrier */
     69  1.1  matt 	bcmgen_bs_barrier,
     70  1.1  matt 
     71  1.1  matt 	/* read (single) */
     72  1.1  matt 	generic_bs_r_1,
     73  1.1  matt 	generic_armv4_bs_r_2,
     74  1.1  matt 	generic_bs_r_4,
     75  1.1  matt 	bs_notimpl_bs_r_8,
     76  1.1  matt 
     77  1.1  matt 	/* read multiple */
     78  1.1  matt 	generic_bs_rm_1,
     79  1.1  matt 	generic_armv4_bs_rm_2,
     80  1.1  matt 	generic_bs_rm_4,
     81  1.1  matt 	bs_notimpl_bs_rm_8,
     82  1.1  matt 
     83  1.1  matt 	/* read region */
     84  1.1  matt 	generic_bs_rr_1,
     85  1.1  matt 	generic_armv4_bs_rr_2,
     86  1.1  matt 	generic_bs_rr_4,
     87  1.1  matt 	bs_notimpl_bs_rr_8,
     88  1.1  matt 
     89  1.1  matt 	/* write (single) */
     90  1.1  matt 	generic_bs_w_1,
     91  1.1  matt 	generic_armv4_bs_w_2,
     92  1.1  matt 	generic_bs_w_4,
     93  1.1  matt 	bs_notimpl_bs_w_8,
     94  1.1  matt 
     95  1.1  matt 	/* write multiple */
     96  1.1  matt 	generic_bs_wm_1,
     97  1.1  matt 	generic_armv4_bs_wm_2,
     98  1.1  matt 	generic_bs_wm_4,
     99  1.1  matt 	bs_notimpl_bs_wm_8,
    100  1.1  matt 
    101  1.1  matt 	/* write region */
    102  1.1  matt 	generic_bs_wr_1,
    103  1.1  matt 	generic_armv4_bs_wr_2,
    104  1.1  matt 	generic_bs_wr_4,
    105  1.1  matt 	bs_notimpl_bs_wr_8,
    106  1.1  matt 
    107  1.1  matt 	/* set multiple */
    108  1.1  matt 	bs_notimpl_bs_sm_1,
    109  1.1  matt 	bs_notimpl_bs_sm_2,
    110  1.1  matt 	bs_notimpl_bs_sm_4,
    111  1.1  matt 	bs_notimpl_bs_sm_8,
    112  1.1  matt 
    113  1.1  matt 	/* set region */
    114  1.1  matt 	generic_bs_sr_1,
    115  1.1  matt 	generic_armv4_bs_sr_2,
    116  1.1  matt 	bs_notimpl_bs_sr_4,
    117  1.1  matt 	bs_notimpl_bs_sr_8,
    118  1.1  matt 
    119  1.1  matt 	/* copy */
    120  1.1  matt 	bs_notimpl_bs_c_1,
    121  1.1  matt 	generic_armv4_bs_c_2,
    122  1.1  matt 	bs_notimpl_bs_c_4,
    123  1.1  matt 	bs_notimpl_bs_c_8,
    124  1.1  matt 
    125  1.1  matt #ifdef __BUS_SPACE_HAS_STREAM_METHODS
    126  1.1  matt 	/* read (single) */
    127  1.1  matt 	generic_bs_r_1,
    128  1.1  matt 	generic_armv4_bs_r_2,
    129  1.1  matt 	generic_bs_r_4,
    130  1.1  matt 	bs_notimpl_bs_r_8,
    131  1.1  matt 
    132  1.1  matt 	/* read multiple */
    133  1.1  matt 	generic_bs_rm_1,
    134  1.1  matt 	generic_armv4_bs_rm_2,
    135  1.1  matt 	generic_bs_rm_4,
    136  1.1  matt 	bs_notimpl_bs_rm_8,
    137  1.1  matt 
    138  1.1  matt 	/* read region */
    139  1.1  matt 	generic_bs_rr_1,
    140  1.1  matt 	generic_armv4_bs_rr_2,
    141  1.1  matt 	generic_bs_rr_4,
    142  1.1  matt 	bs_notimpl_bs_rr_8,
    143  1.1  matt 
    144  1.1  matt 	/* write (single) */
    145  1.1  matt 	generic_bs_w_1,
    146  1.1  matt 	generic_armv4_bs_w_2,
    147  1.1  matt 	generic_bs_w_4,
    148  1.1  matt 	bs_notimpl_bs_w_8,
    149  1.1  matt 
    150  1.1  matt 	/* write multiple */
    151  1.1  matt 	generic_bs_wm_1,
    152  1.1  matt 	generic_armv4_bs_wm_2,
    153  1.1  matt 	generic_bs_wm_4,
    154  1.1  matt 	bs_notimpl_bs_wm_8,
    155  1.1  matt 
    156  1.1  matt 	/* write region */
    157  1.1  matt 	generic_bs_wr_1,
    158  1.1  matt 	generic_armv4_bs_wr_2,
    159  1.1  matt 	generic_bs_wr_4,
    160  1.1  matt 	bs_notimpl_bs_wr_8,
    161  1.1  matt #endif
    162  1.1  matt };
    163  1.1  matt 
    164  1.1  matt int
    165  1.1  matt bcmgen_bs_map(void *t, bus_addr_t bpa, bus_size_t size, int flag,
    166  1.1  matt     bus_space_handle_t *bshp)
    167  1.1  matt {
    168  1.1  matt 	u_long startpa, endpa, pa;
    169  1.1  matt 	const struct pmap_devmap *pd;
    170  1.1  matt 	vaddr_t va;
    171  1.1  matt 
    172  1.1  matt 	if ((pd = pmap_devmap_find_pa(bpa, size)) != NULL) {
    173  1.1  matt 		/* Device was statically mapped. */
    174  1.1  matt 		*bshp = pd->pd_va + (bpa - pd->pd_pa);
    175  1.1  matt 		return 0;
    176  1.1  matt 	}
    177  1.1  matt 
    178  1.1  matt 	startpa = trunc_page(bpa);
    179  1.1  matt 	endpa = round_page(bpa + size);
    180  1.1  matt 
    181  1.1  matt 	/* XXX use extent manager to check duplicate mapping */
    182  1.1  matt 
    183  1.1  matt 	va = uvm_km_alloc(kernel_map, endpa - startpa, 0,
    184  1.2  matt 	    UVM_KMF_VAONLY | UVM_KMF_NOWAIT | UVM_KMF_COLORMATCH);
    185  1.1  matt 	if (!va)
    186  1.1  matt 		return ENOMEM;
    187  1.1  matt 
    188  1.1  matt 	*bshp = (bus_space_handle_t)(va + (bpa - startpa));
    189  1.1  matt 
    190  1.2  matt 	const int pmapflags =
    191  1.2  matt 	    (flag & (BUS_SPACE_MAP_CACHEABLE|BUS_SPACE_MAP_PREFETCHABLE))
    192  1.2  matt 		? 0
    193  1.2  matt 		: PMAP_NOCACHE;
    194  1.1  matt 	for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE) {
    195  1.1  matt 		pmap_kenter_pa(va, pa, VM_PROT_READ | VM_PROT_WRITE, pmapflags);
    196  1.1  matt 	}
    197  1.1  matt 	pmap_update(pmap_kernel());
    198  1.1  matt 
    199  1.1  matt 	return 0;
    200  1.1  matt }
    201  1.1  matt 
    202  1.1  matt void
    203  1.1  matt bcmgen_bs_unmap(void *t, bus_space_handle_t bsh, bus_size_t size)
    204  1.1  matt {
    205  1.1  matt 	vaddr_t	va;
    206  1.1  matt 	vsize_t	sz;
    207  1.1  matt 
    208  1.1  matt 	if (pmap_devmap_find_va(bsh, size) != NULL) {
    209  1.1  matt 		/* Device was statically mapped; nothing to do. */
    210  1.1  matt 		return;
    211  1.1  matt 	}
    212  1.1  matt 
    213  1.1  matt 	va = trunc_page(bsh);
    214  1.1  matt 	sz = round_page(bsh + size) - va;
    215  1.1  matt 
    216  1.1  matt 	pmap_kremove(va, sz);
    217  1.1  matt 	pmap_update(pmap_kernel());
    218  1.1  matt 	uvm_km_free(kernel_map, va, sz, UVM_KMF_VAONLY);
    219  1.1  matt }
    220  1.1  matt 
    221  1.1  matt 
    222  1.1  matt int
    223  1.1  matt bcmgen_bs_subregion(void *t, bus_space_handle_t bsh, bus_size_t offset,
    224  1.1  matt     bus_size_t size, bus_space_handle_t *nbshp)
    225  1.1  matt {
    226  1.1  matt 
    227  1.1  matt 	*nbshp = bsh + offset;
    228  1.1  matt 	return (0);
    229  1.1  matt }
    230  1.1  matt 
    231  1.1  matt void
    232  1.1  matt bcmgen_bs_barrier(void *t, bus_space_handle_t bsh, bus_size_t offset,
    233  1.1  matt     bus_size_t len, int flags)
    234  1.1  matt {
    235  1.1  matt 	flags &= BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE;
    236  1.1  matt 
    237  1.1  matt 	if (flags) {
    238  1.1  matt 		/* Issue an ARM11 Data Syncronisation Barrier (DSB) */
    239  1.1  matt #ifdef _ARM_ARCH_7
    240  1.1  matt 		__asm __volatile("dsb");
    241  1.1  matt #else
    242  1.1  matt 		__asm__ __volatile__ ("mcr p15, 0, %0, c7, c10, 4" : : "r" (0)
    243  1.1  matt 		    : "memory");
    244  1.1  matt #endif
    245  1.1  matt 		return;
    246  1.1  matt 	}
    247  1.1  matt 
    248  1.1  matt }
    249  1.1  matt 
    250  1.1  matt void *
    251  1.1  matt bcmgen_bs_vaddr(void *t, bus_space_handle_t bsh)
    252  1.1  matt {
    253  1.1  matt 
    254  1.1  matt 	return (void *)bsh;
    255  1.1  matt }
    256  1.1  matt 
    257  1.1  matt 
    258  1.1  matt int
    259  1.1  matt bcmgen_bs_alloc(void *t, bus_addr_t rstart, bus_addr_t rend,
    260  1.1  matt     bus_size_t size, bus_size_t alignment, bus_size_t boundary, int flags,
    261  1.1  matt     bus_addr_t *bpap, bus_space_handle_t *bshp)
    262  1.1  matt {
    263  1.1  matt 
    264  1.1  matt 	panic("%s(): not implemented\n", __func__);
    265  1.1  matt }
    266  1.1  matt 
    267  1.1  matt void
    268  1.1  matt bcmgen_bs_free(void *t, bus_space_handle_t bsh, bus_size_t size)
    269  1.1  matt {
    270  1.1  matt 
    271  1.1  matt 	panic("%s(): not implemented\n", __func__);
    272  1.1  matt }
    273