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