1 /*        $NetBSD: z3rambd.c,v 1.6 2023/12/20 00:40:42 thorpej Exp $ */
2 
3 /*-
4  * Copyright (c) 2012 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.
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 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: z3rambd.c,v 1.6 2023/12/20 00:40:42 thorpej Exp $");
34 
35 /*
36  * Z3 RAM virtual block device. Supports ZorRAM, BigRamPlus and FastLane Z3 so
37  * far.
38  */
39 
40 #include <sys/param.h>
41 #include <sys/device.h>
42 #include <sys/socket.h>
43 #include <sys/systm.h>
44 
45 #include <machine/cpu.h>
46 
47 #include <amiga/dev/zbusvar.h>
48 #include <amiga/dev/z3rambdvar.h>
49 
50 #include <dev/altmem/altmemvar.h>
51 
52 static int          z3rambd_match(device_t, cfdata_t , void *);
53 static void         z3rambd_attach(device_t, device_t, void *);
54 
55 static int          z3rambd_altmem_print(void *, const char *);
56 
57 static void         z3rambd_altmem_strategy(void *, struct buf *);
58 static size_t       z3rambd_altmem_getsize(void *);
59 
60 static const struct altmem_memops z3rambd_altmem_memops = {
61           .getsize = z3rambd_altmem_getsize,
62           .strategy = z3rambd_altmem_strategy
63 };
64 
65 CFATTACH_DECL_NEW(z3rambd, sizeof(struct z3rambd_softc),
66     z3rambd_match, z3rambd_attach, NULL, NULL);
67 
68 int
z3rambd_match(device_t parent,cfdata_t cf,void * aux)69 z3rambd_match(device_t parent, cfdata_t cf, void *aux)
70 {
71           struct zbus_args *zap;
72           zap = aux;
73 
74           if (z3rambd_match_id(zap->manid, zap->prodid) > 0)
75                     return 100;
76 
77           return 0;
78 }
79 
80 void
z3rambd_attach(device_t parent,device_t self,void * aux)81 z3rambd_attach(device_t parent, device_t self, void *aux)
82 {
83           struct z3rambd_softc *sc;
84           struct zbus_args *zap;
85           struct altmem_attach_args aaa;
86 
87           sc = device_private(self);
88           sc->sc_dev = self;
89           zap = aux;
90 
91           sc->sc_bst.base = (bus_addr_t)zap->va;
92           sc->sc_bst.absm = &amiga_bus_stride_1;
93           sc->sc_iot = &sc->sc_bst;
94 
95           /* XXX: duh, size of the board does not necessarily equal mem size */
96           sc->sc_size = zap->size;
97 
98           if (zap->prodid == ZORRO_PRODID_ZORRAM)
99                     aprint_normal(": AmigaKit ZorRAM / Individual Computers BigRamPlus\n");
100           else if (zap->prodid == ZORRO_PRODID_3128)
101                     aprint_normal(": DKB 3128\n");
102           else if (zap->prodid == ZORRO_PRODID_FLZ3MEM)
103                     aprint_normal(": FastLane Z3 memory\n");
104           else
105                     aprint_normal("\n");
106 
107           if (bus_space_map(sc->sc_iot, 0, sc->sc_size, 0,
108               &sc->sc_ioh)) {
109                     aprint_error_dev(sc->sc_dev, "can't map the RAM\n");
110           }
111 
112           sc->sc_va = bus_space_vaddr(sc->sc_iot, sc->sc_ioh);
113 
114           aaa.cookie = sc;
115           aaa.memops = &z3rambd_altmem_memops;
116           config_found(self, &aaa, z3rambd_altmem_print, CFARGS_NONE);
117 }
118 
119 static int
z3rambd_altmem_print(void * aux,const char * pnp)120 z3rambd_altmem_print(void *aux, const char *pnp)
121 {
122           if (pnp)
123                     aprint_normal("altmem at %s", pnp);
124 
125           return UNCONF;
126 }
127 
128 /* XXX: should be rewritten using bus_space_read_region? */
129 static void
z3rambd_altmem_strategy(void * aux,struct buf * bp)130 z3rambd_altmem_strategy(void *aux, struct buf *bp)
131 {
132           struct z3rambd_softc *sc = aux;
133           void *addr;
134           size_t off;
135           int s;
136 
137           bp->b_resid = bp->b_bcount;
138           off = bp->b_blkno << DEV_BSHIFT;
139 
140           s = splbio();
141 
142           addr = (char *)((char*)sc->sc_va + off);
143 #ifdef Z3RAMBD_DEBUG
144           aprint_normal_dev(sc->sc_dev,"stratetgy at %x %x\n", (bus_addr_t) addr,
145               (bus_addr_t) kvtop(addr));
146 #endif /* Z3RAMBD_DEBUG */
147 
148           if (bp->b_flags & B_READ)
149                     memcpy((char *)bp->b_data, addr, bp->b_resid);
150           else
151                     memcpy(addr, (char *)bp->b_data, bp->b_resid);
152 
153           splx(s);
154 }
155 
156 static size_t
z3rambd_altmem_getsize(void * aux)157 z3rambd_altmem_getsize(void *aux)
158 {
159           struct z3rambd_softc *sc = aux;
160           return sc->sc_size;
161 }
162