Home | History | Annotate | Line # | Download | only in sa11x0
      1  1.22   skrll /*	$NetBSD: sa11x0_io.c,v 1.22 2023/04/21 15:00:48 skrll Exp $	*/
      2   1.1     rjs 
      3   1.1     rjs /*
      4   1.1     rjs  * Copyright (c) 1997 Mark Brinicombe.
      5   1.1     rjs  * Copyright (c) 1997 Causality Limited.
      6   1.1     rjs  * All rights reserved.
      7   1.1     rjs  *
      8   1.1     rjs  * This code is derived from software contributed to The NetBSD Foundation
      9   1.1     rjs  * by Ichiro FUKUHARA.
     10   1.1     rjs  *
     11   1.1     rjs  * Redistribution and use in source and binary forms, with or without
     12   1.1     rjs  * modification, are permitted provided that the following conditions
     13   1.1     rjs  * are met:
     14   1.1     rjs  * 1. Redistributions of source code must retain the above copyright
     15   1.1     rjs  *    notice, this list of conditions and the following disclaimer.
     16   1.1     rjs  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1     rjs  *    notice, this list of conditions and the following disclaimer in the
     18   1.1     rjs  *    documentation and/or other materials provided with the distribution.
     19   1.1     rjs  * 3. All advertising materials mentioning features or use of this software
     20   1.1     rjs  *    must display the following acknowledgement:
     21   1.1     rjs  *	This product includes software developed by Mark Brinicombe.
     22   1.1     rjs  * 4. The name of the company nor the name of the author may be used to
     23   1.1     rjs  *    endorse or promote products derived from this software without specific
     24   1.1     rjs  *    prior written permission.
     25   1.1     rjs  *
     26   1.1     rjs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     27   1.1     rjs  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     28   1.1     rjs  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     29   1.1     rjs  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     30   1.1     rjs  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     31   1.1     rjs  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     32   1.1     rjs  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33   1.1     rjs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34   1.1     rjs  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35   1.1     rjs  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36   1.1     rjs  * SUCH DAMAGE.
     37   1.1     rjs  */
     38   1.1     rjs 
     39   1.1     rjs /*
     40   1.1     rjs  * bus_space I/O functions for sa11x0
     41   1.1     rjs  */
     42  1.12   lukem 
     43  1.12   lukem #include <sys/cdefs.h>
     44  1.22   skrll __KERNEL_RCSID(0, "$NetBSD: sa11x0_io.c,v 1.22 2023/04/21 15:00:48 skrll Exp $");
     45   1.1     rjs 
     46   1.1     rjs #include <sys/param.h>
     47   1.1     rjs #include <sys/systm.h>
     48   1.1     rjs #include <sys/queue.h>
     49   1.1     rjs 
     50   1.1     rjs #include <uvm/uvm.h>
     51   1.1     rjs 
     52  1.19  dyoung #include <sys/bus.h>
     53   1.1     rjs #include <machine/pmap.h>
     54   1.1     rjs 
     55  1.17   peter /* Prototypes for all the bus_space structure functions */
     56   1.1     rjs 
     57   1.1     rjs bs_protos(sa11x0);
     58   1.1     rjs bs_protos(bs_notimpl);
     59   1.1     rjs 
     60   1.1     rjs /* Declare the sa11x0 bus space tag */
     61   1.1     rjs 
     62   1.1     rjs struct bus_space sa11x0_bs_tag = {
     63   1.1     rjs 	/* cookie */
     64  1.21     ryo 	.bs_cookie = NULL,
     65   1.1     rjs 
     66   1.1     rjs 	/* mapping/unmapping */
     67  1.21     ryo 	.bs_map = sa11x0_bs_map,
     68  1.21     ryo 	.bs_unmap = sa11x0_bs_unmap,
     69  1.21     ryo 	.bs_subregion = sa11x0_bs_subregion,
     70   1.1     rjs 
     71   1.1     rjs 	/* allocation/deallocation */
     72  1.21     ryo 	.bs_alloc = sa11x0_bs_alloc,
     73  1.21     ryo 	.bs_free = sa11x0_bs_free,
     74   1.1     rjs 
     75   1.1     rjs 	/* get kernel virtual address */
     76  1.21     ryo 	.bs_vaddr = sa11x0_bs_vaddr,
     77   1.4     rjs 
     78   1.4     rjs 	/* mmap bus space for userland */
     79  1.21     ryo 	.bs_mmap = sa11x0_bs_mmap,
     80   1.1     rjs 
     81   1.1     rjs 	/* barrier */
     82  1.21     ryo 	.bs_barrier = sa11x0_bs_barrier,
     83   1.1     rjs 
     84   1.1     rjs 	/* read (single) */
     85  1.21     ryo 	.bs_r_1 = sa11x0_bs_r_1,
     86  1.21     ryo 	.bs_r_2 = sa11x0_bs_r_2,
     87  1.21     ryo 	.bs_r_4 = sa11x0_bs_r_4,
     88  1.21     ryo 	.bs_r_8 = bs_notimpl_bs_r_8,
     89   1.1     rjs 
     90   1.1     rjs 	/* read multiple */
     91  1.21     ryo 	.bs_rm_1 = sa11x0_bs_rm_1,
     92  1.21     ryo 	.bs_rm_2 = sa11x0_bs_rm_2,
     93  1.21     ryo 	.bs_rm_4 = sa11x0_bs_rm_4,
     94  1.21     ryo 	.bs_rm_8 = bs_notimpl_bs_rm_8,
     95   1.1     rjs 
     96   1.1     rjs 	/* read region */
     97  1.21     ryo 	.bs_rr_1 = bs_notimpl_bs_rr_1,
     98  1.21     ryo 	.bs_rr_2 = sa11x0_bs_rr_2,
     99  1.21     ryo 	.bs_rr_4 = bs_notimpl_bs_rr_4,
    100  1.21     ryo 	.bs_rr_8 = bs_notimpl_bs_rr_8,
    101   1.1     rjs 
    102   1.1     rjs 	/* write (single) */
    103  1.21     ryo 	.bs_w_1 = sa11x0_bs_w_1,
    104  1.21     ryo 	.bs_w_2 = sa11x0_bs_w_2,
    105  1.21     ryo 	.bs_w_4 = sa11x0_bs_w_4,
    106  1.21     ryo 	.bs_w_8 = bs_notimpl_bs_w_8,
    107   1.1     rjs 
    108   1.1     rjs 	/* write multiple */
    109  1.21     ryo 	.bs_wm_1 = sa11x0_bs_wm_1,
    110  1.21     ryo 	.bs_wm_2 = sa11x0_bs_wm_2,
    111  1.21     ryo 	.bs_wm_4 = sa11x0_bs_wm_4,
    112  1.21     ryo 	.bs_wm_8 = bs_notimpl_bs_wm_8,
    113   1.1     rjs 
    114   1.1     rjs 	/* write region */
    115  1.21     ryo 	.bs_wr_1 = bs_notimpl_bs_wr_1,
    116  1.21     ryo 	.bs_wr_2 = sa11x0_bs_wr_2,
    117  1.21     ryo 	.bs_wr_4 = bs_notimpl_bs_wr_4,
    118  1.21     ryo 	.bs_wr_8 = bs_notimpl_bs_wr_8,
    119   1.1     rjs 
    120   1.1     rjs 	/* set multiple */
    121  1.21     ryo 	.bs_sm_1 = bs_notimpl_bs_sm_1,
    122  1.21     ryo 	.bs_sm_2 = bs_notimpl_bs_sm_2,
    123  1.21     ryo 	.bs_sm_4 = bs_notimpl_bs_sm_4,
    124  1.21     ryo 	.bs_sm_8 = bs_notimpl_bs_sm_8,
    125   1.1     rjs 
    126   1.1     rjs 	/* set region */
    127  1.21     ryo 	.bs_sr_1 = bs_notimpl_bs_sr_1,
    128  1.21     ryo 	.bs_sr_2 = sa11x0_bs_sr_2,
    129  1.21     ryo 	.bs_sr_4 = bs_notimpl_bs_sr_4,
    130  1.21     ryo 	.bs_sr_8 = bs_notimpl_bs_sr_8,
    131   1.1     rjs 
    132   1.1     rjs 	/* copy */
    133  1.21     ryo 	.bs_c_1 = bs_notimpl_bs_c_1,
    134  1.21     ryo 	.bs_c_2 = sa11x0_bs_c_2,
    135  1.21     ryo 	.bs_c_4 = bs_notimpl_bs_c_4,
    136  1.21     ryo 	.bs_c_8 = bs_notimpl_bs_c_8,
    137   1.1     rjs };
    138   1.1     rjs 
    139   1.1     rjs /* bus space functions */
    140   1.1     rjs 
    141   1.1     rjs int
    142  1.20    matt sa11x0_bs_map(void *t, bus_addr_t bpa, bus_size_t size, int flags,
    143  1.15   peter     bus_space_handle_t *bshp)
    144   1.1     rjs {
    145   1.1     rjs 	u_long startpa, endpa, pa;
    146   1.1     rjs 	vaddr_t va;
    147  1.16   peter 	const struct pmap_devmap *pd;
    148   1.1     rjs 
    149  1.16   peter 	if ((pd = pmap_devmap_find_pa(bpa, size)) != NULL) {
    150  1.16   peter                 /* Device was statically mapped. */
    151  1.16   peter 		*bshp = pd->pd_va + (bpa - pd->pd_pa);
    152  1.16   peter 		return 0;
    153   1.1     rjs 	}
    154   1.1     rjs 
    155   1.1     rjs 	startpa = trunc_page(bpa);
    156   1.1     rjs 	endpa = round_page(bpa + size);
    157   1.1     rjs 
    158   1.1     rjs 	/* XXX use extent manager to check duplicate mapping */
    159   1.1     rjs 
    160  1.14    yamt 	va = uvm_km_alloc(kernel_map, endpa - startpa, 0,
    161  1.14    yamt 	    UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
    162  1.17   peter 	if (!va)
    163  1.17   peter 		return ENOMEM;
    164   1.1     rjs 
    165   1.1     rjs 	*bshp = (bus_space_handle_t)(va + (bpa - startpa));
    166   1.1     rjs 
    167  1.20    matt 	const int pmapflags =
    168  1.20    matt 	    (flags & (BUS_SPACE_MAP_CACHEABLE|BUS_SPACE_MAP_PREFETCHABLE))
    169  1.20    matt 		? 0
    170  1.20    matt 		: PMAP_NOCACHE;
    171  1.20    matt 
    172  1.17   peter 	for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE) {
    173  1.20    matt 		pmap_kenter_pa(va, pa, VM_PROT_READ | VM_PROT_WRITE, pmapflags);
    174   1.1     rjs 	}
    175   1.3   chris 	pmap_update(pmap_kernel());
    176   1.1     rjs 
    177  1.17   peter 	return 0;
    178   1.1     rjs }
    179   1.1     rjs 
    180   1.1     rjs int
    181  1.15   peter sa11x0_bs_alloc(void *t, bus_addr_t rstart, bus_addr_t rend, bus_size_t size,
    182  1.15   peter     bus_size_t alignment, bus_size_t boundary, int cacheable,
    183  1.15   peter     bus_addr_t *bpap, bus_space_handle_t *bshp)
    184   1.1     rjs {
    185  1.17   peter 
    186  1.17   peter 	panic("sa11x0_bs_alloc(): Help!");
    187   1.1     rjs }
    188   1.1     rjs 
    189   1.1     rjs void
    190  1.15   peter sa11x0_bs_unmap(void *t, bus_space_handle_t bsh, bus_size_t size)
    191   1.1     rjs {
    192  1.16   peter 	vaddr_t va, endva;
    193  1.16   peter 
    194  1.16   peter 	if (pmap_devmap_find_va(bsh, size) != NULL) {
    195  1.16   peter 		/* Device was statically mapped; nothing to do. */
    196  1.16   peter 		return;
    197  1.16   peter 	}
    198  1.16   peter 
    199  1.16   peter 	va = trunc_page(bsh);
    200  1.16   peter 	endva = round_page(bsh + size);
    201  1.16   peter 
    202  1.16   peter 	pmap_kremove(va, endva - va);
    203  1.16   peter 	pmap_update(pmap_kernel());
    204  1.16   peter 
    205  1.16   peter 	uvm_km_free(kernel_map, va, endva - va, UVM_KMF_VAONLY);
    206   1.1     rjs }
    207   1.1     rjs 
    208  1.22   skrll void
    209  1.15   peter sa11x0_bs_free(void *t, bus_space_handle_t bsh, bus_size_t size)
    210   1.1     rjs {
    211   1.1     rjs 
    212  1.17   peter 	panic("sa11x0_bs_free(): Help!");
    213   1.1     rjs }
    214   1.1     rjs 
    215   1.1     rjs int
    216  1.15   peter sa11x0_bs_subregion(void *t, bus_space_handle_t bsh, bus_size_t offset,
    217  1.15   peter     bus_size_t size, bus_space_handle_t *nbshp)
    218   1.1     rjs {
    219   1.1     rjs 
    220   1.1     rjs 	*nbshp = bsh + offset;
    221  1.17   peter 	return 0;
    222  1.11     rjs }
    223  1.11     rjs 
    224  1.11     rjs paddr_t
    225  1.15   peter sa11x0_bs_mmap(void *t, bus_addr_t paddr, off_t offset, int prot, int flags)
    226  1.11     rjs {
    227  1.11     rjs 	/*
    228  1.11     rjs 	 * mmap from address `paddr+offset' for one page
    229  1.11     rjs 	 */
    230  1.17   peter 	 return arm_btop((paddr + offset));
    231   1.1     rjs }
    232   1.1     rjs 
    233   1.1     rjs void *
    234  1.15   peter sa11x0_bs_vaddr(void *t, bus_space_handle_t bsh)
    235   1.1     rjs {
    236  1.17   peter 	return (void *)bsh;
    237   1.1     rjs }
    238   1.1     rjs 
    239   1.1     rjs void
    240  1.15   peter sa11x0_bs_barrier(void *t, bus_space_handle_t bsh, bus_size_t offset,
    241  1.15   peter     bus_size_t len, int flags)
    242   1.1     rjs {
    243  1.17   peter 
    244  1.22   skrll }
    245