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