Home | History | Annotate | Line # | Download | only in mpc5200
      1 /*	$NetBSD: sram.c,v 1.1 2026/06/27 13:28:35 rkujawa Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2026 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Radoslaw Kujawa and Robert Swindells.
      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  * Driver and allocator for the MPC5200B on-chip SRAM (16KB at MBAR+0x8000).
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: sram.c,v 1.1 2026/06/27 13:28:35 rkujawa Exp $");
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/device.h>
     42 #include <sys/vmem.h>
     43 #include <sys/bus.h>
     44 
     45 #include <dev/ofw/openfirm.h>
     46 
     47 #include <machine/autoconf.h>
     48 
     49 #include <powerpc/mpc5200/obiovar.h>
     50 #include <powerpc/mpc5200/mpc5200reg.h>
     51 #include <powerpc/mpc5200/sramvar.h>
     52 
     53 static int	sram_match(device_t, cfdata_t, void *);
     54 static void	sram_attach(device_t, device_t, void *);
     55 
     56 CFATTACH_DECL_NEW(sram, sizeof(struct sram_softc),
     57     sram_match, sram_attach, NULL, NULL);
     58 
     59 /* Single instance; the allocator API is global for BestComm's use. */
     60 static struct sram_softc *sram_sc;
     61 
     62 static int
     63 sram_match(device_t parent, cfdata_t cf, void *aux)
     64 {
     65 	struct obio_attach_args *oba = aux;
     66 	char compat[32];
     67 	int len;
     68 
     69 	if (strcmp(oba->obio_name, "sram") == 0)
     70 		return 1;
     71 
     72 	len = OF_getprop(oba->obio_node, "compatible", compat, sizeof(compat));
     73 	if (len > 0 &&
     74 	    (strcmp(compat, "mpc5200-sram") == 0 ||
     75 	     strcmp(compat, "mpc5200b-sram") == 0))
     76 		return 1;
     77 
     78 	return 0;
     79 }
     80 
     81 static void
     82 sram_attach(device_t parent, device_t self, void *aux)
     83 {
     84 	struct sram_softc *sc = device_private(self);
     85 	struct obio_attach_args *oba = aux;
     86 	bus_size_t size;
     87 
     88 	sc->sc_dev = self;
     89 	sc->sc_iot = oba->obio_bst;
     90 
     91 	size = oba->obio_size != 0 ? oba->obio_size : MPC5200_SRAM_SIZE;
     92 	if (bus_space_map(sc->sc_iot, oba->obio_addr, size,
     93 	    BUS_SPACE_MAP_LINEAR, &sc->sc_ioh) != 0) {
     94 		aprint_error(": can't map registers\n");
     95 		return;
     96 	}
     97 
     98 	sc->sc_pa = oba->obio_addr;
     99 	sc->sc_kva = (vaddr_t)bus_space_vaddr(sc->sc_iot, sc->sc_ioh);
    100 	sc->sc_size = size;
    101 
    102 	/*
    103 	 * Arena addresses are the physical SRAM addresses, so an allocation
    104 	 * is directly the value a DMA descriptor or the TaskBar wants.
    105 	 */
    106 	sc->sc_arena = vmem_create(device_xname(self), sc->sc_pa, size,
    107 	    sizeof(uint32_t), NULL, NULL, NULL, 0, VM_SLEEP, IPL_VM);
    108 	if (sc->sc_arena == NULL) {
    109 		aprint_error(": can't create arena\n");
    110 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, size);
    111 		return;
    112 	}
    113 
    114 	sram_sc = sc;
    115 
    116 	aprint_normal(": %ju KB on-chip SRAM\n", (uintmax_t)(size / 1024));
    117 }
    118 
    119 bool
    120 sram_available(void)
    121 {
    122 	return sram_sc != NULL;
    123 }
    124 
    125 /*
    126  * Allocate "size" bytes of SRAM with the given power-of-two alignment (0 or 1
    127  * means word alignment).
    128  */
    129 bus_addr_t
    130 sram_alloc(size_t size, size_t align)
    131 {
    132 	vmem_addr_t addr;
    133 
    134 	if (sram_sc == NULL)
    135 		return 0;
    136 
    137 	if (vmem_xalloc(sram_sc->sc_arena, size, align, 0, 0,
    138 	    VMEM_ADDR_MIN, VMEM_ADDR_MAX, VM_BESTFIT | VM_NOSLEEP, &addr) != 0)
    139 		return 0;
    140 
    141 	return (bus_addr_t)addr;
    142 }
    143 
    144 void
    145 sram_free(bus_addr_t addr, size_t size)
    146 {
    147 	if (sram_sc == NULL || addr == 0)
    148 		return;
    149 
    150 	vmem_xfree(sram_sc->sc_arena, (vmem_addr_t)addr, size);
    151 }
    152 
    153 /*
    154  * Translate a physical SRAM address to a CPU pointer in mapped window.
    155  */
    156 void *
    157 sram_kva(bus_addr_t addr)
    158 {
    159 	if (sram_sc == NULL ||
    160 	    addr < sram_sc->sc_pa || addr >= sram_sc->sc_pa + sram_sc->sc_size)
    161 		return NULL;
    162 
    163 	return (void *)(sram_sc->sc_kva + (addr - sram_sc->sc_pa));
    164 }
    165