xlcom.c revision 1.1.4.4 1 1.1.4.4 yamt /* $NetBSD: xlcom.c,v 1.1.4.4 2007/10/27 11:26:04 yamt Exp $ */
2 1.1.4.2 yamt
3 1.1.4.2 yamt /*
4 1.1.4.2 yamt * Copyright (c) 2006 Jachym Holecek
5 1.1.4.2 yamt * All rights reserved.
6 1.1.4.2 yamt *
7 1.1.4.2 yamt * Written for DFC Design, s.r.o.
8 1.1.4.2 yamt *
9 1.1.4.2 yamt * Redistribution and use in source and binary forms, with or without
10 1.1.4.2 yamt * modification, are permitted provided that the following conditions
11 1.1.4.2 yamt * are met:
12 1.1.4.2 yamt *
13 1.1.4.2 yamt * 1. Redistributions of source code must retain the above copyright
14 1.1.4.2 yamt * notice, this list of conditions and the following disclaimer.
15 1.1.4.2 yamt *
16 1.1.4.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
17 1.1.4.2 yamt * notice, this list of conditions and the following disclaimer in the
18 1.1.4.2 yamt * documentation and/or other materials provided with the distribution.
19 1.1.4.2 yamt *
20 1.1.4.2 yamt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 1.1.4.2 yamt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 1.1.4.2 yamt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 1.1.4.2 yamt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.1.4.2 yamt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 1.1.4.2 yamt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 1.1.4.2 yamt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 1.1.4.2 yamt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 1.1.4.2 yamt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 1.1.4.2 yamt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.1.4.2 yamt */
31 1.1.4.2 yamt
32 1.1.4.2 yamt #include <sys/cdefs.h>
33 1.1.4.4 yamt __KERNEL_RCSID(0, "$NetBSD: xlcom.c,v 1.1.4.4 2007/10/27 11:26:04 yamt Exp $");
34 1.1.4.4 yamt
35 1.1.4.4 yamt #include "opt_kgdb.h"
36 1.1.4.2 yamt
37 1.1.4.2 yamt #include <sys/param.h>
38 1.1.4.2 yamt #include <sys/systm.h>
39 1.1.4.2 yamt #include <sys/conf.h>
40 1.1.4.2 yamt #include <sys/device.h>
41 1.1.4.2 yamt #include <sys/file.h>
42 1.1.4.2 yamt #include <sys/ioctl.h>
43 1.1.4.2 yamt #include <sys/kauth.h>
44 1.1.4.2 yamt #include <sys/kernel.h>
45 1.1.4.2 yamt #include <sys/proc.h>
46 1.1.4.2 yamt #include <sys/tty.h>
47 1.1.4.2 yamt #include <sys/time.h>
48 1.1.4.2 yamt #include <sys/syslog.h>
49 1.1.4.2 yamt
50 1.1.4.4 yamt #if defined(KGDB)
51 1.1.4.4 yamt #include <sys/kgdb.h>
52 1.1.4.4 yamt #endif /* KGDB */
53 1.1.4.4 yamt
54 1.1.4.2 yamt #include <dev/cons.h>
55 1.1.4.2 yamt
56 1.1.4.2 yamt #include <machine/intr.h>
57 1.1.4.2 yamt #include <machine/bus.h>
58 1.1.4.2 yamt
59 1.1.4.2 yamt #include <evbppc/virtex/virtex.h>
60 1.1.4.2 yamt #include <evbppc/virtex/dev/xcvbusvar.h>
61 1.1.4.2 yamt #include <evbppc/virtex/dev/xlcomreg.h>
62 1.1.4.2 yamt
63 1.1.4.2 yamt
64 1.1.4.2 yamt #define XLCOM_UNIT_MASK 0x7f
65 1.1.4.2 yamt #define XLCOM_DIALOUT_MASK 0x80
66 1.1.4.2 yamt
67 1.1.4.2 yamt #define UNIT(dev) (minor(dev) & XLCOM_UNIT_MASK)
68 1.1.4.2 yamt #define DIALOUT(dev) (minor(dev) & XLCOM_DIALOUT_MASK)
69 1.1.4.2 yamt
70 1.1.4.2 yamt #define XLCOM_CHAR_PE 0x8000 /* Parity error flag */
71 1.1.4.2 yamt #define XLCOM_CHAR_FE 0x4000 /* Frame error flag */
72 1.1.4.2 yamt #define next(idx) (void)((idx) = ((idx) + 1) % XLCOM_RXBUF_SIZE)
73 1.1.4.2 yamt
74 1.1.4.2 yamt #define XLCOM_RXBUF_SIZE 1024
75 1.1.4.2 yamt
76 1.1.4.2 yamt struct xlcom_softc {
77 1.1.4.2 yamt struct device sc_dev;
78 1.1.4.2 yamt struct tty *sc_tty;
79 1.1.4.2 yamt void *sc_ih;
80 1.1.4.2 yamt
81 1.1.4.2 yamt bus_space_tag_t sc_iot;
82 1.1.4.2 yamt bus_space_handle_t sc_ioh;
83 1.1.4.2 yamt
84 1.1.4.2 yamt /* Deffered execution context. */
85 1.1.4.2 yamt void *sc_rx_soft;
86 1.1.4.2 yamt void *sc_tx_soft;
87 1.1.4.2 yamt
88 1.1.4.2 yamt /* Receive buffer */
89 1.1.4.2 yamt u_short sc_rbuf[XLCOM_RXBUF_SIZE];
90 1.1.4.2 yamt volatile u_int sc_rput;
91 1.1.4.2 yamt volatile u_int sc_rget;
92 1.1.4.2 yamt volatile u_int sc_ravail;
93 1.1.4.2 yamt
94 1.1.4.2 yamt /* Transmit buffer */
95 1.1.4.2 yamt u_char *sc_tba;
96 1.1.4.2 yamt u_int sc_tbc;
97 1.1.4.2 yamt };
98 1.1.4.2 yamt
99 1.1.4.2 yamt static int xlcom_intr(void *);
100 1.1.4.2 yamt static void xlcom_rx_soft(void *);
101 1.1.4.2 yamt static void xlcom_tx_soft(void *);
102 1.1.4.2 yamt static void xlcom_reset(bus_space_tag_t, bus_space_handle_t);
103 1.1.4.4 yamt static int xlcom_busy_getc(bus_space_tag_t, bus_space_handle_t);
104 1.1.4.4 yamt static void xlcom_busy_putc(bus_space_tag_t, bus_space_handle_t, int);
105 1.1.4.2 yamt
106 1.1.4.2 yamt /* System console interface. */
107 1.1.4.2 yamt static int xlcom_cngetc(dev_t);
108 1.1.4.2 yamt static void xlcom_cnputc(dev_t, int);
109 1.1.4.2 yamt void xlcom_cninit(struct consdev *);
110 1.1.4.2 yamt
111 1.1.4.4 yamt #if defined(KGDB)
112 1.1.4.4 yamt
113 1.1.4.4 yamt void xlcom_kgdbinit(void);
114 1.1.4.4 yamt static void xlcom_kgdb_putc(void *, int);
115 1.1.4.4 yamt static int xlcom_kgdb_getc(void *);
116 1.1.4.4 yamt
117 1.1.4.4 yamt #endif /* KGDB */
118 1.1.4.4 yamt
119 1.1.4.2 yamt static struct cnm_state xlcom_cnm_state;
120 1.1.4.2 yamt
121 1.1.4.2 yamt struct consdev consdev_xlcom = {
122 1.1.4.2 yamt .cn_probe = nullcnprobe,
123 1.1.4.2 yamt .cn_init = xlcom_cninit,
124 1.1.4.2 yamt .cn_getc = xlcom_cngetc,
125 1.1.4.2 yamt .cn_putc = xlcom_cnputc,
126 1.1.4.2 yamt .cn_pollc = nullcnpollc,
127 1.1.4.2 yamt .cn_pri = CN_REMOTE
128 1.1.4.2 yamt };
129 1.1.4.2 yamt
130 1.1.4.2 yamt /* Character device. */
131 1.1.4.2 yamt static dev_type_open(xlcom_open);
132 1.1.4.2 yamt static dev_type_read(xlcom_read);
133 1.1.4.2 yamt static dev_type_write(xlcom_write);
134 1.1.4.2 yamt static dev_type_ioctl(xlcom_ioctl);
135 1.1.4.2 yamt static dev_type_poll(xlcom_poll);
136 1.1.4.2 yamt static dev_type_close(xlcom_close);
137 1.1.4.2 yamt
138 1.1.4.2 yamt static dev_type_tty(xlcom_tty);
139 1.1.4.2 yamt static dev_type_stop(xlcom_stop);
140 1.1.4.2 yamt
141 1.1.4.2 yamt const struct cdevsw xlcom_cdevsw = {
142 1.1.4.2 yamt xlcom_open, xlcom_close, xlcom_read, xlcom_write, xlcom_ioctl,
143 1.1.4.2 yamt xlcom_stop, xlcom_tty, xlcom_poll, nommap, ttykqfilter, D_TTY
144 1.1.4.2 yamt };
145 1.1.4.2 yamt
146 1.1.4.2 yamt extern struct cfdriver xlcom_cd;
147 1.1.4.2 yamt
148 1.1.4.2 yamt /* Terminal line. */
149 1.1.4.2 yamt static int xlcom_param(struct tty *, struct termios *);
150 1.1.4.2 yamt static void xlcom_start(struct tty *);
151 1.1.4.2 yamt
152 1.1.4.2 yamt /* Generic device. */
153 1.1.4.2 yamt static void xlcom_attach(struct device *, struct device *, void *);
154 1.1.4.2 yamt
155 1.1.4.2 yamt CFATTACH_DECL(xlcom, sizeof(struct xlcom_softc),
156 1.1.4.2 yamt xcvbus_child_match, xlcom_attach, NULL, NULL);
157 1.1.4.2 yamt
158 1.1.4.2 yamt
159 1.1.4.2 yamt static void
160 1.1.4.2 yamt xlcom_attach(struct device *parent, struct device *self, void *aux)
161 1.1.4.2 yamt {
162 1.1.4.2 yamt struct xcvbus_attach_args *vaa = aux;
163 1.1.4.2 yamt struct xlcom_softc *sc = (struct xlcom_softc *)self;
164 1.1.4.2 yamt struct tty *tp;
165 1.1.4.2 yamt dev_t dev;
166 1.1.4.2 yamt
167 1.1.4.2 yamt printf(": UartLite serial port\n");
168 1.1.4.2 yamt
169 1.1.4.4 yamt #if defined(KGDB)
170 1.1.4.4 yamt /* We don't want to share kgdb port with the user. */
171 1.1.4.4 yamt if (sc->sc_iot == kgdb_iot && sc->sc_ioh == kgdb_ioh) {
172 1.1.4.4 yamt printf("%s: already in use by kgdb\n", device_xname(self));
173 1.1.4.4 yamt return;
174 1.1.4.4 yamt }
175 1.1.4.4 yamt #endif /* KGDB */
176 1.1.4.4 yamt
177 1.1.4.2 yamt if ((sc->sc_ih = intr_establish(vaa->vaa_intr, IST_LEVEL, IPL_SERIAL,
178 1.1.4.2 yamt xlcom_intr, sc)) == NULL) {
179 1.1.4.2 yamt printf("%s: could not establish interrupt\n",
180 1.1.4.2 yamt device_xname(self));
181 1.1.4.2 yamt return ;
182 1.1.4.2 yamt }
183 1.1.4.2 yamt
184 1.1.4.2 yamt dev = makedev(cdevsw_lookup_major(&xlcom_cdevsw), device_unit(self));
185 1.1.4.2 yamt if (cn_tab == &consdev_xlcom) {
186 1.1.4.2 yamt cn_init_magic(&xlcom_cnm_state);
187 1.1.4.2 yamt cn_set_magic("\x23\x2e"); /* #. */
188 1.1.4.2 yamt cn_tab->cn_dev = dev;
189 1.1.4.2 yamt
190 1.1.4.2 yamt sc->sc_iot = consdev_iot;
191 1.1.4.2 yamt sc->sc_ioh = consdev_ioh;
192 1.1.4.2 yamt
193 1.1.4.2 yamt printf("%s: console\n", sc->sc_dev.dv_xname);
194 1.1.4.2 yamt } else {
195 1.1.4.2 yamt sc->sc_iot = vaa->vaa_iot;
196 1.1.4.2 yamt
197 1.1.4.2 yamt if (bus_space_map(vaa->vaa_iot, vaa->vaa_addr, XLCOM_SIZE, 0,
198 1.1.4.2 yamt &sc->sc_ioh) != 0) {
199 1.1.4.2 yamt printf("%s: could not map registers\n",
200 1.1.4.2 yamt device_xname(self));
201 1.1.4.2 yamt return ;
202 1.1.4.2 yamt }
203 1.1.4.2 yamt
204 1.1.4.2 yamt /* Reset FIFOs. */
205 1.1.4.2 yamt xlcom_reset(sc->sc_iot, sc->sc_ioh);
206 1.1.4.2 yamt }
207 1.1.4.2 yamt
208 1.1.4.2 yamt sc->sc_tbc = 0;
209 1.1.4.2 yamt sc->sc_tba = NULL;
210 1.1.4.2 yamt
211 1.1.4.2 yamt sc->sc_rput = sc->sc_rget = 0;
212 1.1.4.2 yamt sc->sc_ravail = XLCOM_RXBUF_SIZE;
213 1.1.4.2 yamt
214 1.1.4.2 yamt sc->sc_rx_soft = softintr_establish(IPL_SOFTSERIAL, xlcom_rx_soft, sc);
215 1.1.4.2 yamt sc->sc_tx_soft = softintr_establish(IPL_SOFTSERIAL, xlcom_tx_soft, sc);
216 1.1.4.2 yamt
217 1.1.4.2 yamt if (sc->sc_rx_soft == NULL || sc->sc_tx_soft == NULL) {
218 1.1.4.2 yamt printf("%s: could not establish Rx or Tx softintr\n",
219 1.1.4.2 yamt sc->sc_dev.dv_xname);
220 1.1.4.2 yamt return ;
221 1.1.4.2 yamt }
222 1.1.4.2 yamt
223 1.1.4.2 yamt tp = ttymalloc();
224 1.1.4.2 yamt tp->t_dev = dev;
225 1.1.4.2 yamt tp->t_oproc = xlcom_start;
226 1.1.4.2 yamt tp->t_param = xlcom_param;
227 1.1.4.2 yamt tp->t_hwiflow = NULL; /* No HW flow control */
228 1.1.4.2 yamt tty_attach(tp);
229 1.1.4.2 yamt
230 1.1.4.2 yamt /* XXX anything else to do for console early? */
231 1.1.4.2 yamt if (cn_tab == &consdev_xlcom) {
232 1.1.4.2 yamt /* Before first open, so that we can enter ddb(4). */
233 1.1.4.2 yamt bus_space_write_4(sc->sc_iot, sc->sc_ioh, XLCOM_CNTL,
234 1.1.4.2 yamt CNTL_INTR_EN);
235 1.1.4.2 yamt }
236 1.1.4.2 yamt
237 1.1.4.2 yamt sc->sc_tty = tp;
238 1.1.4.2 yamt }
239 1.1.4.2 yamt
240 1.1.4.2 yamt /*
241 1.1.4.2 yamt * Misc hooks.
242 1.1.4.2 yamt */
243 1.1.4.2 yamt static void
244 1.1.4.2 yamt xlcom_tx_soft(void *arg)
245 1.1.4.2 yamt {
246 1.1.4.2 yamt struct xlcom_softc *sc = (struct xlcom_softc *)arg;
247 1.1.4.2 yamt struct tty *tp = sc->sc_tty;
248 1.1.4.2 yamt
249 1.1.4.2 yamt if (tp->t_state & TS_FLUSH)
250 1.1.4.2 yamt tp->t_state &= ~TS_FLUSH;
251 1.1.4.2 yamt else
252 1.1.4.2 yamt ndflush(&tp->t_outq,
253 1.1.4.2 yamt (int)(sc->sc_tba - tp->t_outq.c_cf));
254 1.1.4.2 yamt (tp->t_linesw->l_start)(tp);
255 1.1.4.2 yamt }
256 1.1.4.2 yamt
257 1.1.4.2 yamt static void
258 1.1.4.2 yamt xlcom_rx_soft(void *arg)
259 1.1.4.2 yamt {
260 1.1.4.2 yamt struct xlcom_softc *sc = (struct xlcom_softc *)arg;
261 1.1.4.2 yamt struct tty *tp = sc->sc_tty;
262 1.1.4.2 yamt int (*rint)(int, struct tty *);
263 1.1.4.2 yamt u_short c;
264 1.1.4.2 yamt int d;
265 1.1.4.2 yamt
266 1.1.4.2 yamt /*
267 1.1.4.2 yamt * XXX: we don't do any synchronization, rput may change below
268 1.1.4.2 yamt * XXX: our hands -- it doesn't seem to be troublesome as long
269 1.1.4.2 yamt * XXX: as "sc->sc_rget = sc->sc_rput" is atomic.
270 1.1.4.2 yamt */
271 1.1.4.2 yamt rint = tp->t_linesw->l_rint;
272 1.1.4.2 yamt
273 1.1.4.2 yamt /* Run until we catch our tail. */
274 1.1.4.2 yamt while (sc->sc_rput != sc->sc_rget) {
275 1.1.4.2 yamt c = sc->sc_rbuf[sc->sc_rget];
276 1.1.4.2 yamt
277 1.1.4.2 yamt next(sc->sc_rget);
278 1.1.4.2 yamt sc->sc_ravail++;
279 1.1.4.2 yamt
280 1.1.4.2 yamt d = (c & 0xff) |
281 1.1.4.2 yamt ((c & XLCOM_CHAR_PE) != 0 ? TTY_PE : 0) |
282 1.1.4.2 yamt ((c & XLCOM_CHAR_FE) != 0 ? TTY_FE : 0);
283 1.1.4.2 yamt
284 1.1.4.2 yamt /*
285 1.1.4.2 yamt * Drop the rest of data if discipline runs out of buffer
286 1.1.4.2 yamt * space. We'd use flow control here, if we had any.
287 1.1.4.2 yamt */
288 1.1.4.2 yamt if ((rint)(d, tp) == -1) {
289 1.1.4.2 yamt sc->sc_rget = sc->sc_rput;
290 1.1.4.2 yamt return ;
291 1.1.4.2 yamt }
292 1.1.4.2 yamt }
293 1.1.4.2 yamt }
294 1.1.4.2 yamt
295 1.1.4.2 yamt static void
296 1.1.4.2 yamt xlcom_send_chunk(struct xlcom_softc *sc)
297 1.1.4.2 yamt {
298 1.1.4.2 yamt uint32_t stat;
299 1.1.4.2 yamt
300 1.1.4.2 yamt /* Chunk flushed, no more data available. */
301 1.1.4.2 yamt if (sc->sc_tbc <= 0) {
302 1.1.4.2 yamt return ;
303 1.1.4.2 yamt }
304 1.1.4.2 yamt
305 1.1.4.2 yamt /* Run as long as we have space and data. */
306 1.1.4.2 yamt while (sc->sc_tbc > 0) {
307 1.1.4.2 yamt stat = bus_space_read_4(sc->sc_iot, sc->sc_ioh, XLCOM_STAT);
308 1.1.4.2 yamt if (stat & STAT_TX_FULL)
309 1.1.4.2 yamt break;
310 1.1.4.2 yamt
311 1.1.4.2 yamt bus_space_write_4(sc->sc_iot, sc->sc_ioh, XLCOM_TX_FIFO,
312 1.1.4.2 yamt *sc->sc_tba);
313 1.1.4.2 yamt
314 1.1.4.2 yamt sc->sc_tbc--;
315 1.1.4.2 yamt sc->sc_tba++;
316 1.1.4.2 yamt }
317 1.1.4.2 yamt
318 1.1.4.2 yamt /* Try to grab more data while FIFO drains. */
319 1.1.4.2 yamt if (sc->sc_tbc == 0) {
320 1.1.4.2 yamt sc->sc_tty->t_state &= ~TS_BUSY;
321 1.1.4.2 yamt softintr_schedule(sc->sc_tx_soft);
322 1.1.4.2 yamt }
323 1.1.4.2 yamt }
324 1.1.4.2 yamt
325 1.1.4.2 yamt static void
326 1.1.4.2 yamt xlcom_recv_chunk(struct xlcom_softc *sc)
327 1.1.4.2 yamt {
328 1.1.4.2 yamt uint32_t stat;
329 1.1.4.2 yamt u_short c;
330 1.1.4.2 yamt u_int n;
331 1.1.4.2 yamt
332 1.1.4.2 yamt n = sc->sc_ravail;
333 1.1.4.2 yamt stat = bus_space_read_4(sc->sc_iot, sc->sc_ioh, XLCOM_STAT);
334 1.1.4.2 yamt
335 1.1.4.2 yamt /* Run as long as we have data and space. */
336 1.1.4.2 yamt while ((stat & STAT_RX_DATA) != 0 && sc->sc_ravail > 0) {
337 1.1.4.2 yamt c = bus_space_read_4(sc->sc_iot, sc->sc_ioh, XLCOM_RX_FIFO);
338 1.1.4.2 yamt
339 1.1.4.2 yamt cn_check_magic(sc->sc_tty->t_dev, c, xlcom_cnm_state);
340 1.1.4.2 yamt
341 1.1.4.2 yamt /* XXX: Should we pass rx-overrun upstream too? */
342 1.1.4.2 yamt c |= ((stat & STAT_PARITY_ERR) != 0 ? XLCOM_CHAR_PE : 0) |
343 1.1.4.2 yamt ((stat & STAT_FRAME_ERR) != 0 ? XLCOM_CHAR_FE : 0);
344 1.1.4.2 yamt sc->sc_rbuf[sc->sc_rput] = c;
345 1.1.4.2 yamt sc->sc_ravail--;
346 1.1.4.2 yamt
347 1.1.4.2 yamt next(sc->sc_rput);
348 1.1.4.2 yamt stat = bus_space_read_4(sc->sc_iot, sc->sc_ioh, XLCOM_STAT);
349 1.1.4.2 yamt }
350 1.1.4.2 yamt
351 1.1.4.2 yamt /* Shedule completion hook if we received any. */
352 1.1.4.2 yamt if (n != sc->sc_ravail)
353 1.1.4.2 yamt softintr_schedule(sc->sc_rx_soft);
354 1.1.4.2 yamt }
355 1.1.4.2 yamt
356 1.1.4.2 yamt static int
357 1.1.4.2 yamt xlcom_intr(void *arg)
358 1.1.4.2 yamt {
359 1.1.4.2 yamt struct xlcom_softc *sc = arg;
360 1.1.4.2 yamt uint32_t stat;
361 1.1.4.2 yamt
362 1.1.4.2 yamt /*
363 1.1.4.2 yamt * Xilinx DS422, "OPB UART Lite v1.00b"
364 1.1.4.2 yamt *
365 1.1.4.2 yamt * If interrupts are enabled, an interrupt is generated when one
366 1.1.4.2 yamt * of the following conditions is true:
367 1.1.4.2 yamt */
368 1.1.4.2 yamt stat = bus_space_read_4(sc->sc_iot, sc->sc_ioh, XLCOM_STAT);
369 1.1.4.2 yamt
370 1.1.4.2 yamt /*
371 1.1.4.2 yamt * 1. When there exists any valid character in the receive FIFO,
372 1.1.4.2 yamt * the interrupt stays active until the receive FIFO is empty.
373 1.1.4.2 yamt * This is a level interrupt.
374 1.1.4.2 yamt */
375 1.1.4.2 yamt if (stat & STAT_RX_DATA)
376 1.1.4.2 yamt xlcom_recv_chunk(sc);
377 1.1.4.2 yamt
378 1.1.4.2 yamt /*
379 1.1.4.2 yamt * 2. When the transmit FIFO goes from not empty to empty, such
380 1.1.4.2 yamt * as when the last character in the transmit FIFO is transmitted,
381 1.1.4.2 yamt * the interrupt is only active one clock cycle. This is an
382 1.1.4.2 yamt * edge interrupt.
383 1.1.4.2 yamt */
384 1.1.4.2 yamt if (stat & STAT_TX_EMPTY)
385 1.1.4.2 yamt xlcom_send_chunk(sc);
386 1.1.4.2 yamt
387 1.1.4.2 yamt return (0);
388 1.1.4.2 yamt }
389 1.1.4.2 yamt
390 1.1.4.2 yamt /*
391 1.1.4.2 yamt * Character device.
392 1.1.4.2 yamt */
393 1.1.4.2 yamt static int
394 1.1.4.2 yamt xlcom_open(dev_t dev, int flags, int mode, struct lwp *l)
395 1.1.4.2 yamt {
396 1.1.4.2 yamt struct xlcom_softc *sc;
397 1.1.4.2 yamt struct tty *tp;
398 1.1.4.2 yamt struct proc *p;
399 1.1.4.2 yamt int error, s;
400 1.1.4.2 yamt
401 1.1.4.2 yamt sc = device_lookup(&xlcom_cd, minor(dev));
402 1.1.4.2 yamt if (sc == NULL)
403 1.1.4.2 yamt return (ENXIO);
404 1.1.4.2 yamt
405 1.1.4.2 yamt tp = sc->sc_tty;
406 1.1.4.2 yamt p = l->l_proc;
407 1.1.4.2 yamt
408 1.1.4.2 yamt s = spltty(); /* { */
409 1.1.4.2 yamt
410 1.1.4.2 yamt if ((tp->t_state & TS_ISOPEN) && (tp->t_state & TS_XCLUDE) &&
411 1.1.4.2 yamt kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER,
412 1.1.4.2 yamt &p->p_acflag) != 0) {
413 1.1.4.2 yamt error = EBUSY;
414 1.1.4.2 yamt goto fail;
415 1.1.4.2 yamt }
416 1.1.4.2 yamt
417 1.1.4.2 yamt /* Is this the first open? */
418 1.1.4.2 yamt if (!(tp->t_state & TS_ISOPEN) && tp->t_wopen == 0) {
419 1.1.4.2 yamt tp->t_dev = dev;
420 1.1.4.2 yamt
421 1.1.4.2 yamt /* Values hardwired at synthesis time. XXXFreza: xparam.h */
422 1.1.4.2 yamt tp->t_ispeed = tp->t_ospeed = B38400;
423 1.1.4.2 yamt tp->t_cflag = CLOCAL | CREAD | CS8;
424 1.1.4.2 yamt tp->t_iflag = TTYDEF_IFLAG;
425 1.1.4.2 yamt tp->t_oflag = TTYDEF_OFLAG;
426 1.1.4.2 yamt tp->t_lflag = TTYDEF_LFLAG;
427 1.1.4.2 yamt
428 1.1.4.2 yamt ttychars(tp);
429 1.1.4.2 yamt ttsetwater(tp);
430 1.1.4.2 yamt
431 1.1.4.2 yamt /* Enable interrupt. */
432 1.1.4.2 yamt bus_space_write_4(sc->sc_iot, sc->sc_ioh, XLCOM_CNTL,
433 1.1.4.2 yamt CNTL_INTR_EN);
434 1.1.4.2 yamt }
435 1.1.4.2 yamt
436 1.1.4.2 yamt error = ttyopen(tp, DIALOUT(dev), (flags & O_NONBLOCK));
437 1.1.4.2 yamt if (error)
438 1.1.4.2 yamt goto fail;
439 1.1.4.2 yamt
440 1.1.4.2 yamt error = (tp->t_linesw->l_open)(dev, tp);
441 1.1.4.2 yamt if (error)
442 1.1.4.2 yamt goto fail;
443 1.1.4.2 yamt
444 1.1.4.2 yamt splx(s); /* } */
445 1.1.4.2 yamt return (0);
446 1.1.4.2 yamt
447 1.1.4.2 yamt fail:
448 1.1.4.2 yamt /* XXXFreza: Shutdown if nobody else has the device open. */
449 1.1.4.2 yamt splx(s);
450 1.1.4.2 yamt return (error);
451 1.1.4.2 yamt }
452 1.1.4.2 yamt
453 1.1.4.2 yamt static int
454 1.1.4.2 yamt xlcom_read(dev_t dev, struct uio *uio, int flag)
455 1.1.4.2 yamt {
456 1.1.4.2 yamt struct xlcom_softc *sc;
457 1.1.4.2 yamt struct tty *tp;
458 1.1.4.2 yamt
459 1.1.4.2 yamt sc = device_lookup(&xlcom_cd, minor(dev));
460 1.1.4.2 yamt if (sc == NULL)
461 1.1.4.2 yamt return (ENXIO);
462 1.1.4.2 yamt tp = sc->sc_tty;
463 1.1.4.2 yamt
464 1.1.4.2 yamt return (tp->t_linesw->l_read)(tp, uio, flag);
465 1.1.4.2 yamt }
466 1.1.4.2 yamt
467 1.1.4.2 yamt static int
468 1.1.4.2 yamt xlcom_write(dev_t dev, struct uio *uio, int flag)
469 1.1.4.2 yamt {
470 1.1.4.2 yamt struct xlcom_softc *sc;
471 1.1.4.2 yamt struct tty *tp;
472 1.1.4.2 yamt
473 1.1.4.2 yamt sc = device_lookup(&xlcom_cd, minor(dev));
474 1.1.4.2 yamt if (sc == NULL)
475 1.1.4.2 yamt return (ENXIO);
476 1.1.4.2 yamt tp = sc->sc_tty;
477 1.1.4.2 yamt
478 1.1.4.2 yamt return (tp->t_linesw->l_write)(tp, uio, flag);
479 1.1.4.2 yamt }
480 1.1.4.2 yamt
481 1.1.4.2 yamt static int
482 1.1.4.2 yamt xlcom_poll(dev_t dev, int events, struct lwp *l)
483 1.1.4.2 yamt {
484 1.1.4.2 yamt struct xlcom_softc *sc;
485 1.1.4.2 yamt struct tty *tp;
486 1.1.4.2 yamt
487 1.1.4.2 yamt sc = device_lookup(&xlcom_cd, minor(dev));
488 1.1.4.2 yamt if (sc == NULL)
489 1.1.4.2 yamt return (ENXIO);
490 1.1.4.2 yamt tp = sc->sc_tty;
491 1.1.4.2 yamt
492 1.1.4.2 yamt return (tp->t_linesw->l_poll)(tp, events, l);
493 1.1.4.2 yamt }
494 1.1.4.2 yamt
495 1.1.4.2 yamt static struct tty *
496 1.1.4.2 yamt xlcom_tty(dev_t dev)
497 1.1.4.2 yamt {
498 1.1.4.2 yamt struct xlcom_softc *sc;
499 1.1.4.2 yamt struct tty *tp;
500 1.1.4.2 yamt
501 1.1.4.2 yamt sc = device_lookup(&xlcom_cd, minor(dev));
502 1.1.4.2 yamt if (sc == NULL)
503 1.1.4.2 yamt return (NULL);
504 1.1.4.2 yamt tp = sc->sc_tty;
505 1.1.4.2 yamt
506 1.1.4.2 yamt return (tp);
507 1.1.4.2 yamt }
508 1.1.4.2 yamt
509 1.1.4.2 yamt static int
510 1.1.4.3 yamt xlcom_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
511 1.1.4.2 yamt {
512 1.1.4.2 yamt struct xlcom_softc *sc;
513 1.1.4.2 yamt struct tty *tp;
514 1.1.4.2 yamt int error;
515 1.1.4.2 yamt
516 1.1.4.2 yamt sc = device_lookup(&xlcom_cd, minor(dev));
517 1.1.4.2 yamt if (sc == NULL)
518 1.1.4.2 yamt return (ENXIO);
519 1.1.4.2 yamt tp = sc->sc_tty;
520 1.1.4.2 yamt
521 1.1.4.2 yamt error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
522 1.1.4.2 yamt if (error != EPASSTHROUGH)
523 1.1.4.2 yamt return (error);
524 1.1.4.2 yamt
525 1.1.4.2 yamt error = ttioctl(tp, cmd, data, flag, l);
526 1.1.4.2 yamt if (error != EPASSTHROUGH)
527 1.1.4.2 yamt return (error);
528 1.1.4.2 yamt
529 1.1.4.2 yamt /* XXXFreza: error = 0; switch (cmd); return error; */
530 1.1.4.2 yamt
531 1.1.4.2 yamt return (error);
532 1.1.4.2 yamt }
533 1.1.4.2 yamt
534 1.1.4.2 yamt static int
535 1.1.4.2 yamt xlcom_close(dev_t dev, int flag, int mode, struct lwp *l)
536 1.1.4.2 yamt {
537 1.1.4.2 yamt struct xlcom_softc *sc;
538 1.1.4.2 yamt struct tty *tp;
539 1.1.4.2 yamt
540 1.1.4.2 yamt sc = device_lookup(&xlcom_cd, minor(dev));
541 1.1.4.2 yamt if (sc == NULL)
542 1.1.4.2 yamt return (ENXIO);
543 1.1.4.2 yamt tp = sc->sc_tty;
544 1.1.4.2 yamt
545 1.1.4.2 yamt (tp->t_linesw->l_close)(tp, flag);
546 1.1.4.2 yamt ttyclose(tp);
547 1.1.4.2 yamt
548 1.1.4.2 yamt /* Is this the last close? XXXFreza: hum? */
549 1.1.4.2 yamt if (!(tp->t_state & TS_ISOPEN) && tp->t_wopen == 0) {
550 1.1.4.2 yamt }
551 1.1.4.2 yamt
552 1.1.4.2 yamt return (0);
553 1.1.4.2 yamt }
554 1.1.4.2 yamt
555 1.1.4.2 yamt static void
556 1.1.4.2 yamt xlcom_stop(struct tty *tp, int flag)
557 1.1.4.2 yamt {
558 1.1.4.2 yamt struct xlcom_softc *sc;
559 1.1.4.2 yamt int s;
560 1.1.4.2 yamt
561 1.1.4.2 yamt sc = device_lookup(&xlcom_cd, UNIT(tp->t_dev));
562 1.1.4.2 yamt if (sc == NULL)
563 1.1.4.2 yamt return ;
564 1.1.4.2 yamt
565 1.1.4.2 yamt s = splserial();
566 1.1.4.2 yamt if (tp->t_state & TS_BUSY) {
567 1.1.4.2 yamt /* XXXFreza: make sure we stop xmitting at next chunk */
568 1.1.4.2 yamt
569 1.1.4.2 yamt if (! (tp->t_state & TS_TTSTOP))
570 1.1.4.2 yamt tp->t_state |= TS_FLUSH;
571 1.1.4.2 yamt }
572 1.1.4.2 yamt splx(s);
573 1.1.4.2 yamt }
574 1.1.4.2 yamt
575 1.1.4.2 yamt /*
576 1.1.4.2 yamt * Terminal line.
577 1.1.4.2 yamt */
578 1.1.4.2 yamt static int
579 1.1.4.2 yamt xlcom_param(struct tty *tp, struct termios *t)
580 1.1.4.2 yamt {
581 1.1.4.2 yamt t->c_cflag &= ~HUPCL;
582 1.1.4.2 yamt
583 1.1.4.2 yamt if (tp->t_ospeed == t->c_ospeed &&
584 1.1.4.2 yamt tp->t_ispeed == t->c_ispeed &&
585 1.1.4.2 yamt tp->t_cflag == t->c_cflag)
586 1.1.4.2 yamt return (0);
587 1.1.4.2 yamt
588 1.1.4.2 yamt return (EINVAL);
589 1.1.4.2 yamt }
590 1.1.4.2 yamt
591 1.1.4.2 yamt static void
592 1.1.4.2 yamt xlcom_start(struct tty *tp)
593 1.1.4.2 yamt {
594 1.1.4.2 yamt struct xlcom_softc *sc;
595 1.1.4.2 yamt int s1, s2;
596 1.1.4.2 yamt
597 1.1.4.2 yamt sc = device_lookup(&xlcom_cd, UNIT(tp->t_dev));
598 1.1.4.2 yamt if (sc == NULL)
599 1.1.4.2 yamt return ;
600 1.1.4.2 yamt
601 1.1.4.2 yamt s1 = spltty();
602 1.1.4.2 yamt
603 1.1.4.2 yamt if (tp->t_state & (TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) {
604 1.1.4.2 yamt splx(s1);
605 1.1.4.2 yamt return ;
606 1.1.4.2 yamt }
607 1.1.4.2 yamt
608 1.1.4.2 yamt if (tp->t_outq.c_cc <= tp->t_lowat) {
609 1.1.4.2 yamt if (tp->t_state & TS_ASLEEP) {
610 1.1.4.2 yamt tp->t_state &= ~TS_ASLEEP;
611 1.1.4.2 yamt wakeup(&tp->t_outq);
612 1.1.4.2 yamt }
613 1.1.4.2 yamt selwakeup(&tp->t_wsel);
614 1.1.4.2 yamt
615 1.1.4.2 yamt if (tp->t_outq.c_cc == 0) {
616 1.1.4.2 yamt splx(s1);
617 1.1.4.2 yamt return ;
618 1.1.4.2 yamt }
619 1.1.4.2 yamt }
620 1.1.4.2 yamt
621 1.1.4.2 yamt tp->t_state |= TS_BUSY;
622 1.1.4.2 yamt splx(s1);
623 1.1.4.2 yamt
624 1.1.4.2 yamt s2 = splserial();
625 1.1.4.2 yamt sc->sc_tba = tp->t_outq.c_cf;
626 1.1.4.2 yamt sc->sc_tbc = ndqb(&tp->t_outq, 0);
627 1.1.4.2 yamt xlcom_send_chunk(sc);
628 1.1.4.2 yamt splx(s2);
629 1.1.4.2 yamt }
630 1.1.4.2 yamt
631 1.1.4.2 yamt static void
632 1.1.4.2 yamt xlcom_reset(bus_space_tag_t iot, bus_space_handle_t ioh)
633 1.1.4.2 yamt {
634 1.1.4.2 yamt /* Wait while the fifo drains. */
635 1.1.4.2 yamt while (! (bus_space_read_4(iot, ioh, XLCOM_STAT) & STAT_TX_EMPTY))
636 1.1.4.2 yamt ;
637 1.1.4.2 yamt
638 1.1.4.2 yamt bus_space_write_4(iot, ioh, XLCOM_CNTL, CNTL_RX_CLEAR | CNTL_TX_CLEAR);
639 1.1.4.2 yamt }
640 1.1.4.2 yamt
641 1.1.4.4 yamt static int
642 1.1.4.4 yamt xlcom_busy_getc(bus_space_tag_t t, bus_space_handle_t h)
643 1.1.4.4 yamt {
644 1.1.4.4 yamt while (! (bus_space_read_4(t, h, XLCOM_STAT) & STAT_RX_DATA))
645 1.1.4.4 yamt ;
646 1.1.4.4 yamt
647 1.1.4.4 yamt return (bus_space_read_4(t, h, XLCOM_RX_FIFO));
648 1.1.4.4 yamt }
649 1.1.4.4 yamt
650 1.1.4.4 yamt static void
651 1.1.4.4 yamt xlcom_busy_putc(bus_space_tag_t t, bus_space_handle_t h, int c)
652 1.1.4.4 yamt {
653 1.1.4.4 yamt while (bus_space_read_4(t, h, XLCOM_STAT) & STAT_TX_FULL)
654 1.1.4.4 yamt ;
655 1.1.4.4 yamt
656 1.1.4.4 yamt bus_space_write_4(t, h, XLCOM_TX_FIFO, c);
657 1.1.4.4 yamt }
658 1.1.4.4 yamt
659 1.1.4.2 yamt /*
660 1.1.4.2 yamt * Console on UartLite.
661 1.1.4.2 yamt */
662 1.1.4.2 yamt void
663 1.1.4.2 yamt nullcnprobe(struct consdev *cn)
664 1.1.4.2 yamt {
665 1.1.4.2 yamt }
666 1.1.4.2 yamt
667 1.1.4.2 yamt void
668 1.1.4.2 yamt xlcom_cninit(struct consdev *cn)
669 1.1.4.2 yamt {
670 1.1.4.2 yamt if (bus_space_map(consdev_iot, CONS_ADDR, XLCOM_SIZE, 0, &consdev_ioh))
671 1.1.4.2 yamt panic("xlcom_cninit: could not map consdev_ioh");
672 1.1.4.2 yamt
673 1.1.4.2 yamt xlcom_reset(consdev_iot, consdev_ioh);
674 1.1.4.2 yamt }
675 1.1.4.2 yamt
676 1.1.4.2 yamt static int
677 1.1.4.2 yamt xlcom_cngetc(dev_t dev)
678 1.1.4.2 yamt {
679 1.1.4.4 yamt return (xlcom_busy_getc(consdev_iot, consdev_ioh));
680 1.1.4.2 yamt }
681 1.1.4.2 yamt
682 1.1.4.2 yamt static void
683 1.1.4.2 yamt xlcom_cnputc(dev_t dev, int c)
684 1.1.4.2 yamt {
685 1.1.4.4 yamt xlcom_busy_putc(consdev_iot, consdev_ioh, c);
686 1.1.4.4 yamt }
687 1.1.4.4 yamt
688 1.1.4.4 yamt /*
689 1.1.4.4 yamt * Remote GDB (aka "kgdb") interface.
690 1.1.4.4 yamt */
691 1.1.4.4 yamt #if defined(KGDB)
692 1.1.4.4 yamt
693 1.1.4.4 yamt static int
694 1.1.4.4 yamt xlcom_kgdb_getc(void *arg)
695 1.1.4.4 yamt {
696 1.1.4.4 yamt return (xlcom_busy_getc(kgdb_iot, kgdb_ioh));
697 1.1.4.4 yamt }
698 1.1.4.4 yamt
699 1.1.4.4 yamt static void
700 1.1.4.4 yamt xlcom_kgdb_putc(void *arg, int c)
701 1.1.4.4 yamt {
702 1.1.4.4 yamt xlcom_busy_putc(kgdb_iot, kgdb_ioh, c);
703 1.1.4.4 yamt }
704 1.1.4.2 yamt
705 1.1.4.4 yamt void
706 1.1.4.4 yamt xlcom_kgdbinit(void)
707 1.1.4.4 yamt {
708 1.1.4.4 yamt if (bus_space_map(kgdb_iot, KGDB_ADDR, XLCOM_SIZE, 0, &kgdb_ioh))
709 1.1.4.4 yamt panic("xlcom_kgdbinit: could not map kgdb_ioh");
710 1.1.4.4 yamt
711 1.1.4.4 yamt xlcom_reset(kgdb_iot, kgdb_ioh);
712 1.1.4.4 yamt
713 1.1.4.4 yamt kgdb_attach(xlcom_kgdb_getc, xlcom_kgdb_putc, NULL);
714 1.1.4.4 yamt kgdb_dev = 123; /* arbitrary strictly positive value */
715 1.1.4.2 yamt }
716 1.1.4.4 yamt
717 1.1.4.4 yamt #endif /* KGDB */
718