1 /* $OpenBSD: xp.c,v 1.4 2024/06/26 01:40:49 jsg Exp $ */
2 /* $NetBSD: xp.c,v 1.1 2016/12/03 17:38:02 tsutsui Exp $ */
3
4 /*-
5 * Copyright (c) 2016 Izumi Tsutsui. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 /*
29 * LUNA's Hitachi HD647180 "XP" I/O processor driver
30 */
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/conf.h>
35 #include <sys/device.h>
36 #include <sys/ioctl.h>
37 #include <sys/malloc.h>
38 #include <sys/errno.h>
39
40 #include <uvm/uvm_extern.h>
41
42 #include <machine/autoconf.h>
43 #include <machine/board.h>
44 #include <machine/xpio.h>
45
46 #define XP_SHM_BASE TRI_PORT_RAM
47 #define XP_SHM_SIZE 0x00010000 /* 64KB for XP; rest 64KB for lance */
48 #define XP_TAS_ADDR OBIO_TAS
49
50 struct xp_softc {
51 struct device sc_dev;
52
53 vaddr_t sc_shm_base;
54 vsize_t sc_shm_size;
55 vaddr_t sc_tas;
56
57 bool sc_isopen;
58 };
59
60 static int xp_match(struct device *, void *, void *);
61 static void xp_attach(struct device *, struct device *, void *);
62
63 const struct cfattach xp_ca = {
64 sizeof (struct xp_softc), xp_match, xp_attach
65 };
66
67 struct cfdriver xp_cd = {
68 NULL, "xp", DV_DULL
69 };
70
71 /* #define XP_DEBUG */
72
73 #ifdef XP_DEBUG
74 #define XP_DEBUG_ALL 0xff
75 uint32_t xp_debug = 0;
76 #define DPRINTF(x, y) if (xp_debug & (x)) printf y
77 #else
78 #define DPRINTF(x, y) /* nothing */
79 #endif
80
81 static bool xp_matched;
82
83 /*
84 * PIO 0 port C is connected to XP's reset line
85 *
86 * XXX: PIO port functions should be shared with machdep.c for DIP SWs
87 */
88 #define PIO_ADDR OBIO_PIO0_BASE
89 #define PORT_A (0 * 4)
90 #define PORT_B (1 * 4)
91 #define PORT_C (2 * 4)
92 #define CTRL (3 * 4)
93
94 /* PIO0 Port C bit definition */
95 #define XP_INT1_REQ 0 /* INTR B */
96 /* unused */ /* IBF B */
97 #define XP_INT1_ENA 2 /* INTE B */
98 #define XP_INT5_REQ 3 /* INTR A */
99 #define XP_INT5_ENA 4 /* INTE A */
100 /* unused */ /* IBF A */
101 #define PARITY 6 /* PC6 output to enable parity error */
102 #define XP_RESET 7 /* PC7 output to reset HD647180 XP */
103
104 /* Port control for PC6 and PC7 */
105 #define ON 1
106 #define OFF 0
107
108 static uint8_t
put_pio0c(uint8_t bit,uint8_t set)109 put_pio0c(uint8_t bit, uint8_t set)
110 {
111 volatile uint8_t * const pio0 = (uint8_t *)PIO_ADDR;
112
113 pio0[CTRL] = (bit << 1) | (set & 0x01);
114
115 return pio0[PORT_C];
116 }
117
118 static int
xp_match(struct device * parent,void * cf,void * aux)119 xp_match(struct device *parent, void *cf, void *aux)
120 {
121 struct mainbus_attach_args *maa = aux;
122
123 /* only one XP processor */
124 if (xp_matched)
125 return 0;
126
127 if (strcmp(maa->ma_name, xp_cd.cd_name))
128 return 0;
129
130 if (maa->ma_addr != XP_SHM_BASE)
131 return 0;
132
133 xp_matched = true;
134 return 1;
135 }
136
137 static void
xp_attach(struct device * parent,struct device * self,void * aux)138 xp_attach(struct device *parent, struct device *self, void *aux)
139 {
140 struct xp_softc *sc = (void *)self;
141
142 printf(": HD647180X I/O processor\n");
143
144 sc->sc_shm_base = XP_SHM_BASE;
145 sc->sc_shm_size = XP_SHM_SIZE;
146 sc->sc_tas = XP_TAS_ADDR;
147 }
148
149 int
xpopen(dev_t dev,int flags,int mode,struct proc * p)150 xpopen(dev_t dev, int flags, int mode, struct proc *p)
151 {
152 struct xp_softc *sc;
153 int unit;
154
155 DPRINTF(XP_DEBUG_ALL, ("%s\n", __func__));
156
157 unit = minor(dev);
158 if (unit >= xp_cd.cd_ndevs)
159 return ENXIO;
160 sc = xp_cd.cd_devs[unit];
161 if (sc == NULL)
162 return ENXIO;
163 if (sc->sc_isopen)
164 return EBUSY;
165
166 sc->sc_isopen = true;
167
168 return 0;
169 }
170
171 int
xpclose(dev_t dev,int flags,int mode,struct proc * p)172 xpclose(dev_t dev, int flags, int mode, struct proc *p)
173 {
174 struct xp_softc *sc;
175 int unit;
176
177 DPRINTF(XP_DEBUG_ALL, ("%s\n", __func__));
178
179 unit = minor(dev);
180 if (unit >= xp_cd.cd_ndevs)
181 return ENXIO;
182 sc = xp_cd.cd_devs[unit];
183 sc->sc_isopen = false;
184
185 return 0;
186 }
187
188 int
xpioctl(dev_t dev,u_long cmd,void * addr,int flags,struct proc * p)189 xpioctl(dev_t dev, u_long cmd, void *addr, int flags, struct proc *p)
190 {
191 struct xp_softc *sc;
192 int unit, error;
193 struct xp_download *downld;
194 uint8_t *loadbuf;
195 size_t loadsize;
196
197 DPRINTF(XP_DEBUG_ALL, ("%s\n", __func__));
198
199 unit = minor(dev);
200 if (unit >= xp_cd.cd_ndevs)
201 return ENXIO;
202 sc = xp_cd.cd_devs[unit];
203
204 switch (cmd) {
205 case XPIOCDOWNLD:
206 downld = addr;
207 loadsize = downld->size;
208 if (loadsize == 0 || loadsize > sc->sc_shm_size) {
209 return EINVAL;
210 }
211
212 loadbuf = malloc(loadsize, M_DEVBUF, M_WAITOK);
213 if (loadbuf == NULL) {
214 return ENOMEM;
215 }
216 error = copyin(downld->data, loadbuf, loadsize);
217 if (error == 0) {
218 put_pio0c(XP_RESET, ON);
219 delay(100);
220 memcpy((void *)sc->sc_shm_base, loadbuf, loadsize);
221 delay(100);
222 put_pio0c(XP_RESET, OFF);
223 } else {
224 DPRINTF(XP_DEBUG_ALL, ("%s: ioctl failed (err = %d)\n",
225 __func__, error));
226 }
227
228 free(loadbuf, M_DEVBUF, loadsize);
229 return error;
230
231 default:
232 return ENOTTY;
233 }
234 }
235
236 paddr_t
xpmmap(dev_t dev,off_t offset,int prot)237 xpmmap(dev_t dev, off_t offset, int prot)
238 {
239 struct xp_softc *sc;
240 int unit;
241 paddr_t pa;
242
243 pa = -1;
244
245 unit = minor(dev);
246 sc = xp_cd.cd_devs[unit];
247
248 if (offset >= 0 &&
249 offset < sc->sc_shm_size) {
250 pa = (paddr_t)(trunc_page(sc->sc_shm_base) + offset);
251 }
252
253 return pa;
254 }
255