Home | History | Annotate | Line # | Download | only in gbus
      1 /* $NetBSD: gbus_io.c,v 1.1 2024/03/06 05:33:09 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2024 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Bus space implementation for the Gbus: a general 8-bit bus found
     34  * on Laser / TurboLaser CPU modules.
     35  */
     36 
     37 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     38 
     39 __KERNEL_RCSID(0, "$NetBSD: gbus_io.c,v 1.1 2024/03/06 05:33:09 thorpej Exp $");
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/device.h>
     44 #include <sys/endian.h>
     45 #include <sys/vmem.h>
     46 
     47 #include <sys/bus.h>
     48 
     49 #include <alpha/gbus/gbusvar.h>
     50 
     51 /*
     52  * While physically there is one Gbus per CPU module on the system,
     53  * logically there is only one (the one on the CPU module that holds
     54  * the primary CPU), and this describes it.
     55  */
     56 static struct gbus_config {
     57 	paddr_t		gc_sysbase;	/* system base address of Gbus */
     58 	struct alpha_bus_space gc_st;	/* the Gbus's space tag */
     59 } gbus_config;
     60 
     61 /* Adjacent bytes on the Gbus are spaced 64 bytes apart. */
     62 #define	GBUS_ADDR_SHIFT		6
     63 
     64 static int
     65 gbus_io_map(
     66 	void *v,
     67 	bus_addr_t addr,
     68 	bus_size_t size			__unused,
     69 	int flags			__unused,
     70 	bus_space_handle_t *iohp,
     71 	int acct			__unused)
     72 {
     73 	struct gbus_config * const gc = v;
     74 
     75 	*iohp = ALPHA_PHYS_TO_K0SEG(gc->gc_sysbase + addr);
     76 
     77 	return 0;
     78 }
     79 
     80 static void
     81 gbus_io_unmap(
     82 	void *v				__unused,
     83 	bus_space_handle_t ioh		__unused,
     84 	bus_size_t size			__unused,
     85 	int acct			__unused)
     86 {
     87 	/* No work to do. */
     88 }
     89 
     90 static void
     91 gbus_io_barrier(
     92 	void *v				__unused,
     93 	bus_space_handle_t ioh		__unused,
     94 	bus_size_t off			__unused,
     95 	bus_size_t len			__unused,
     96 	int flags			__unused)
     97 {
     98 	if (flags & BUS_SPACE_BARRIER_READ) {
     99 		alpha_mb();
    100 	} else if (flags & BUS_SPACE_BARRIER_WRITE) {
    101 		alpha_wmb();
    102 	}
    103 }
    104 
    105 __CTASSERT(_BYTE_ORDER == _LITTLE_ENDIAN);
    106 
    107 /*
    108  * A 32-bit pointer is used here because the bus transaction is going to
    109  * be 32-bit regardless, and this avoids having the compiler generate
    110  * extbl / insbl / mskbl unnecessarily (or r/m/w in the write case).
    111  */
    112 #define	GBUS_ADDR(ioh, off)						\
    113 	(uint32_t *)((ioh) + ((off) << GBUS_ADDR_SHIFT))
    114 
    115 static uint8_t
    116 gbus_io_read_1(
    117 	void *v				__unused,
    118 	bus_space_handle_t ioh,
    119 	bus_size_t off)
    120 {
    121 	uint32_t *addr = GBUS_ADDR(ioh, off);
    122 
    123 	alpha_mb();
    124 	return (uint8_t)atomic_load_relaxed(addr);
    125 }
    126 
    127 static void
    128 gbus_io_write_1(
    129 	void *v				__unused,
    130 	bus_space_handle_t ioh,
    131 	bus_size_t off,
    132 	uint8_t val)
    133 {
    134 	uint32_t *addr = GBUS_ADDR(ioh, off);
    135 
    136 	atomic_store_relaxed(addr, (uint32_t)val);
    137 	alpha_mb();
    138 }
    139 
    140 bus_space_tag_t
    141 gbus_io_init(paddr_t sysbase)
    142 {
    143 	struct gbus_config * const gc = &gbus_config;
    144 	bus_space_tag_t t = &gc->gc_st;
    145 
    146 	if (t->abs_cookie == gc) {
    147 		/* Already initialized. */
    148 		KASSERT(gc->gc_sysbase == sysbase);
    149 		return t;
    150 	}
    151 
    152 	gc->gc_sysbase = sysbase;
    153 	memset(t, 0, sizeof(*t));
    154 
    155 	/*
    156 	 * Initialize the bus space tag.  We just support the bare
    157 	 * minimum operations required by Gbus devices.
    158 	 */
    159 
    160 	/* cookie */
    161 	t->abs_cookie =		gc;
    162 
    163 	/* mapping/unmapping */
    164 	t->abs_map =		gbus_io_map;
    165 	t->abs_unmap =		gbus_io_unmap;
    166 
    167 	/* barrier */
    168 	t->abs_barrier =	gbus_io_barrier;
    169 
    170 	/* read (single) */
    171 	t->abs_r_1 =		gbus_io_read_1;
    172 
    173 	/* write (single) */
    174 	t->abs_w_1 =		gbus_io_write_1;
    175 
    176 	return t;
    177 }
    178