11.14Sskrll/*	$NetBSD: obio_space.c,v 1.14 2023/04/21 14:58:34 skrll Exp $	*/
21.1Sthorpej
31.1Sthorpej/*
41.5Sthorpej * Copyright (c) 2001, 2002, 2003 Wasabi Systems, Inc.
51.1Sthorpej * All rights reserved.
61.1Sthorpej *
71.1Sthorpej * Written by Jason R. Thorpe for Wasabi Systems, Inc.
81.1Sthorpej *
91.1Sthorpej * Redistribution and use in source and binary forms, with or without
101.1Sthorpej * modification, are permitted provided that the following conditions
111.1Sthorpej * are met:
121.1Sthorpej * 1. Redistributions of source code must retain the above copyright
131.1Sthorpej *    notice, this list of conditions and the following disclaimer.
141.1Sthorpej * 2. Redistributions in binary form must reproduce the above copyright
151.1Sthorpej *    notice, this list of conditions and the following disclaimer in the
161.1Sthorpej *    documentation and/or other materials provided with the distribution.
171.1Sthorpej * 3. All advertising materials mentioning features or use of this software
181.1Sthorpej *    must display the following acknowledgement:
191.1Sthorpej *	This product includes software developed for the NetBSD Project by
201.1Sthorpej *	Wasabi Systems, Inc.
211.1Sthorpej * 4. The name of Wasabi Systems, Inc. may not be used to endorse
221.1Sthorpej *    or promote products derived from this software without specific prior
231.1Sthorpej *    written permission.
241.1Sthorpej *
251.1Sthorpej * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
261.1Sthorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
271.1Sthorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
281.1Sthorpej * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
291.1Sthorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
301.1Sthorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
311.1Sthorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
321.1Sthorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
331.1Sthorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
341.1Sthorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
351.1Sthorpej * POSSIBILITY OF SUCH DAMAGE.
361.1Sthorpej */
371.1Sthorpej
381.1Sthorpej/*
391.1Sthorpej * bus_space functions for IQ80321 on-board devices
401.1Sthorpej */
411.6Slukem
421.6Slukem#include <sys/cdefs.h>
431.14Sskrll__KERNEL_RCSID(0, "$NetBSD: obio_space.c,v 1.14 2023/04/21 14:58:34 skrll Exp $");
441.1Sthorpej
451.1Sthorpej#include <sys/param.h>
461.1Sthorpej#include <sys/systm.h>
471.1Sthorpej
481.1Sthorpej#include <uvm/uvm_extern.h>
491.1Sthorpej
501.11Sdyoung#include <sys/bus.h>
511.1Sthorpej
521.1Sthorpej/* Prototypes for all the bus_space structure functions */
531.1Sthorpejbs_protos(obio);
541.2Sthorpejbs_protos(generic);
551.4Sbriggsbs_protos(generic_armv4);
561.1Sthorpejbs_protos(bs_notimpl);
571.1Sthorpej
581.1Sthorpej/*
591.1Sthorpej * The obio bus space tag.  This is constant for all instances, so
601.1Sthorpej * we never have to explicitly "create" it.
611.1Sthorpej */
621.1Sthorpejstruct bus_space obio_bs_tag = {
631.1Sthorpej	/* cookie */
641.13Sryo	.bs_cookie = (void *) 0,
651.1Sthorpej
661.1Sthorpej	/* mapping/unmapping */
671.13Sryo	.bs_map = obio_bs_map,
681.13Sryo	.bs_unmap = obio_bs_unmap,
691.13Sryo	.bs_subregion = obio_bs_subregion,
701.1Sthorpej
711.1Sthorpej	/* allocation/deallocation */
721.13Sryo	.bs_alloc = obio_bs_alloc,
731.13Sryo	.bs_free = obio_bs_free,
741.1Sthorpej
751.1Sthorpej	/* get kernel virtual address */
761.13Sryo	.bs_vaddr = obio_bs_vaddr,
771.1Sthorpej
781.1Sthorpej	/* mmap */
791.13Sryo	.bs_mmap = bs_notimpl_bs_mmap,
801.1Sthorpej
811.1Sthorpej	/* barrier */
821.13Sryo	.bs_barrier = obio_bs_barrier,
831.1Sthorpej
841.1Sthorpej	/* read (single) */
851.13Sryo	.bs_r_1 = generic_bs_r_1,
861.13Sryo	.bs_r_2 = generic_armv4_bs_r_2,
871.13Sryo	.bs_r_4 = generic_bs_r_4,
881.13Sryo	.bs_r_8 = bs_notimpl_bs_r_8,
891.1Sthorpej
901.1Sthorpej	/* read multiple */
911.13Sryo	.bs_rm_1 = generic_bs_rm_1,
921.13Sryo	.bs_rm_2 = generic_armv4_bs_rm_2,
931.13Sryo	.bs_rm_4 = bs_notimpl_bs_rm_4,
941.13Sryo	.bs_rm_8 = bs_notimpl_bs_rm_8,
951.1Sthorpej
961.1Sthorpej	/* read region */
971.13Sryo	.bs_rr_1 = generic_bs_rr_1,
981.13Sryo	.bs_rr_2 = bs_notimpl_bs_rr_2,
991.13Sryo	.bs_rr_4 = bs_notimpl_bs_rr_4,
1001.13Sryo	.bs_rr_8 = bs_notimpl_bs_rr_8,
1011.1Sthorpej
1021.1Sthorpej	/* write (single) */
1031.13Sryo	.bs_w_1 = generic_bs_w_1,
1041.13Sryo	.bs_w_2 = generic_armv4_bs_w_2,
1051.13Sryo	.bs_w_4 = generic_bs_w_4,
1061.13Sryo	.bs_w_8 = bs_notimpl_bs_w_8,
1071.1Sthorpej
1081.1Sthorpej	/* write multiple */
1091.13Sryo	.bs_wm_1 = generic_bs_wm_1,
1101.13Sryo	.bs_wm_2 = generic_armv4_bs_wm_2,
1111.13Sryo	.bs_wm_4 = bs_notimpl_bs_wm_4,
1121.13Sryo	.bs_wm_8 = bs_notimpl_bs_wm_8,
1131.1Sthorpej
1141.1Sthorpej	/* write region */
1151.13Sryo	.bs_wr_1 = bs_notimpl_bs_wr_1,
1161.13Sryo	.bs_wr_2 = bs_notimpl_bs_wr_2,
1171.13Sryo	.bs_wr_4 = bs_notimpl_bs_wr_4,
1181.13Sryo	.bs_wr_8 = bs_notimpl_bs_wr_8,
1191.1Sthorpej
1201.1Sthorpej	/* set multiple */
1211.13Sryo	.bs_sm_1 = bs_notimpl_bs_sm_1,
1221.13Sryo	.bs_sm_2 = bs_notimpl_bs_sm_2,
1231.13Sryo	.bs_sm_4 = bs_notimpl_bs_sm_4,
1241.13Sryo	.bs_sm_8 = bs_notimpl_bs_sm_8,
1251.1Sthorpej
1261.1Sthorpej	/* set region */
1271.13Sryo	.bs_sr_1 = bs_notimpl_bs_sr_1,
1281.13Sryo	.bs_sr_2 = bs_notimpl_bs_sr_2,
1291.13Sryo	.bs_sr_4 = bs_notimpl_bs_sr_4,
1301.13Sryo	.bs_sr_8 = bs_notimpl_bs_sr_8,
1311.1Sthorpej
1321.1Sthorpej	/* copy */
1331.13Sryo	.bs_c_1 = bs_notimpl_bs_c_1,
1341.13Sryo	.bs_c_2 = bs_notimpl_bs_c_2,
1351.13Sryo	.bs_c_4 = bs_notimpl_bs_c_4,
1361.13Sryo	.bs_c_8 = bs_notimpl_bs_c_8,
1371.1Sthorpej};
1381.1Sthorpej
1391.1Sthorpejint
1401.12Smattobio_bs_map(void *t, bus_addr_t bpa, bus_size_t size, int flag,
1411.1Sthorpej    bus_space_handle_t *bshp)
1421.1Sthorpej{
1431.5Sthorpej	const struct pmap_devmap *pd;
1441.5Sthorpej	paddr_t startpa, endpa, pa, offset;
1451.4Sbriggs	vaddr_t va;
1461.4Sbriggs
1471.5Sthorpej	if ((pd = pmap_devmap_find_pa(bpa, size)) != NULL) {
1481.5Sthorpej		/* Device was statically mapped. */
1491.5Sthorpej		*bshp = pd->pd_va + (bpa - pd->pd_pa);
1501.5Sthorpej		return (0);
1511.5Sthorpej	}
1521.5Sthorpej
1531.5Sthorpej	endpa = round_page(bpa + size);
1541.5Sthorpej	offset = bpa & PAGE_MASK;
1551.5Sthorpej	startpa = trunc_page(bpa);
1561.14Sskrll
1571.9Syamt	va = uvm_km_alloc(kernel_map, endpa - startpa, 0,
1581.9Syamt	    UVM_KMF_VAONLY | UVM_KMF_NOWAIT);
1591.5Sthorpej	if (va == 0)
1601.5Sthorpej		return (ENOMEM);
1611.5Sthorpej
1621.5Sthorpej	*bshp = va + offset;
1631.5Sthorpej
1641.12Smatt	const int pmapflags =
1651.12Smatt	    (flag & (BUS_SPACE_MAP_CACHEABLE|BUS_SPACE_MAP_PREFETCHABLE))
1661.12Smatt		? 0
1671.12Smatt		: PMAP_NOCACHE;
1681.12Smatt
1691.5Sthorpej	for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE) {
1701.12Smatt		pmap_kenter_pa(va, pa, VM_PROT_READ | VM_PROT_WRITE, pmapflags);
1711.4Sbriggs	}
1721.5Sthorpej	pmap_update(pmap_kernel());
1731.1Sthorpej
1741.5Sthorpej	return (0);
1751.1Sthorpej}
1761.1Sthorpej
1771.1Sthorpejint
1781.1Sthorpejobio_bs_alloc(void *t, bus_addr_t rstart, bus_addr_t rend, bus_size_t size,
1791.1Sthorpej    bus_size_t alignment, bus_size_t boundary, int flags, bus_addr_t *bpap,
1801.1Sthorpej    bus_space_handle_t *bshp)
1811.1Sthorpej{
1821.1Sthorpej
1831.3Sprovos	panic("obio_bs_alloc(): not implemented");
1841.1Sthorpej}
1851.1Sthorpej
1861.1Sthorpej
1871.1Sthorpejvoid
1881.1Sthorpejobio_bs_unmap(void *t, bus_space_handle_t bsh, bus_size_t size)
1891.1Sthorpej{
1901.5Sthorpej	vaddr_t va, endva;
1911.5Sthorpej
1921.5Sthorpej	if (pmap_devmap_find_va(bsh, size) != NULL) {
1931.5Sthorpej		/* Device was statically mapped; nothing to do. */
1941.5Sthorpej		return;
1951.5Sthorpej	}
1961.5Sthorpej
1971.5Sthorpej	endva = round_page(bsh + size);
1981.5Sthorpej	va = trunc_page(bsh);
1991.1Sthorpej
2001.5Sthorpej	pmap_kremove(va, endva - va);
2011.8Syamt	uvm_km_free(kernel_map, va, endva - va, UVM_KMF_VAONLY);
2021.1Sthorpej}
2031.1Sthorpej
2041.14Sskrllvoid
2051.1Sthorpejobio_bs_free(void *t, bus_space_handle_t bsh, bus_size_t size)
2061.1Sthorpej{
2071.1Sthorpej
2081.3Sprovos	panic("obio_bs_free(): not implemented");
2091.1Sthorpej}
2101.1Sthorpej
2111.1Sthorpejint
2121.1Sthorpejobio_bs_subregion(void *t, bus_space_handle_t bsh, bus_size_t offset,
2131.1Sthorpej    bus_size_t size, bus_space_handle_t *nbshp)
2141.1Sthorpej{
2151.1Sthorpej
2161.1Sthorpej	*nbshp = bsh + offset;
2171.1Sthorpej	return (0);
2181.1Sthorpej}
2191.1Sthorpej
2201.1Sthorpejvoid *
2211.1Sthorpejobio_bs_vaddr(void *t, bus_space_handle_t bsh)
2221.1Sthorpej{
2231.1Sthorpej
2241.1Sthorpej	return ((void *)bsh);
2251.1Sthorpej}
2261.1Sthorpej
2271.1Sthorpejvoid
2281.1Sthorpejobio_bs_barrier(void *t, bus_space_handle_t bsh, bus_size_t offset,
2291.1Sthorpej    bus_size_t len, int flags)
2301.1Sthorpej{
2311.1Sthorpej
2321.1Sthorpej	/* Nothing to do. */
2331.1Sthorpej}
234