plcom.c revision 1.68 1 1.68 riastrad /* $NetBSD: plcom.c,v 1.68 2023/04/11 12:56:07 riastradh Exp $ */
2 1.1 rearnsha
3 1.1 rearnsha /*-
4 1.1 rearnsha * Copyright (c) 2001 ARM Ltd
5 1.1 rearnsha * All rights reserved.
6 1.1 rearnsha *
7 1.1 rearnsha * Redistribution and use in source and binary forms, with or without
8 1.1 rearnsha * modification, are permitted provided that the following conditions
9 1.1 rearnsha * are met:
10 1.1 rearnsha * 1. Redistributions of source code must retain the above copyright
11 1.1 rearnsha * notice, this list of conditions and the following disclaimer.
12 1.1 rearnsha * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 rearnsha * notice, this list of conditions and the following disclaimer in the
14 1.1 rearnsha * documentation and/or other materials provided with the distribution.
15 1.1 rearnsha * 3. The name of the company may not be used to endorse or promote
16 1.1 rearnsha * products derived from this software without specific prior written
17 1.1 rearnsha * permission.
18 1.1 rearnsha *
19 1.1 rearnsha * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
20 1.1 rearnsha * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 1.1 rearnsha * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 1.1 rearnsha * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 1.1 rearnsha * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 1.1 rearnsha * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 1.1 rearnsha * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 rearnsha * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 rearnsha * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 rearnsha * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 rearnsha * SUCH DAMAGE.
30 1.46 skrll *
31 1.40 skrll * Copyright (c) 1998, 1999, 2012 The NetBSD Foundation, Inc.
32 1.1 rearnsha * All rights reserved.
33 1.1 rearnsha *
34 1.1 rearnsha * This code is derived from software contributed to The NetBSD Foundation
35 1.40 skrll * by Charles M. Hannum and Nick Hudson.
36 1.1 rearnsha *
37 1.1 rearnsha * Redistribution and use in source and binary forms, with or without
38 1.1 rearnsha * modification, are permitted provided that the following conditions
39 1.1 rearnsha * are met:
40 1.1 rearnsha * 1. Redistributions of source code must retain the above copyright
41 1.1 rearnsha * notice, this list of conditions and the following disclaimer.
42 1.1 rearnsha * 2. Redistributions in binary form must reproduce the above copyright
43 1.1 rearnsha * notice, this list of conditions and the following disclaimer in the
44 1.1 rearnsha * documentation and/or other materials provided with the distribution.
45 1.1 rearnsha *
46 1.1 rearnsha * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
47 1.1 rearnsha * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
48 1.1 rearnsha * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
49 1.1 rearnsha * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
50 1.1 rearnsha * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
51 1.1 rearnsha * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
52 1.1 rearnsha * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
53 1.1 rearnsha * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
54 1.1 rearnsha * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55 1.1 rearnsha * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
56 1.1 rearnsha * POSSIBILITY OF SUCH DAMAGE.
57 1.1 rearnsha */
58 1.1 rearnsha
59 1.1 rearnsha /*
60 1.1 rearnsha * Copyright (c) 1991 The Regents of the University of California.
61 1.1 rearnsha * All rights reserved.
62 1.1 rearnsha *
63 1.1 rearnsha * Redistribution and use in source and binary forms, with or without
64 1.1 rearnsha * modification, are permitted provided that the following conditions
65 1.1 rearnsha * are met:
66 1.1 rearnsha * 1. Redistributions of source code must retain the above copyright
67 1.1 rearnsha * notice, this list of conditions and the following disclaimer.
68 1.1 rearnsha * 2. Redistributions in binary form must reproduce the above copyright
69 1.1 rearnsha * notice, this list of conditions and the following disclaimer in the
70 1.1 rearnsha * documentation and/or other materials provided with the distribution.
71 1.9 agc * 3. Neither the name of the University nor the names of its contributors
72 1.1 rearnsha * may be used to endorse or promote products derived from this software
73 1.1 rearnsha * without specific prior written permission.
74 1.1 rearnsha *
75 1.1 rearnsha * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
76 1.1 rearnsha * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
77 1.1 rearnsha * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
78 1.1 rearnsha * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
79 1.1 rearnsha * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
80 1.1 rearnsha * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
81 1.1 rearnsha * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
82 1.1 rearnsha * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
83 1.1 rearnsha * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
84 1.1 rearnsha * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
85 1.1 rearnsha * SUCH DAMAGE.
86 1.1 rearnsha *
87 1.1 rearnsha * @(#)com.c 7.5 (Berkeley) 5/16/91
88 1.1 rearnsha */
89 1.1 rearnsha
90 1.1 rearnsha /*
91 1.40 skrll * COM driver for the Prime Cell PL010 and PL011 UARTs. Both are is similar to
92 1.40 skrll * the 16C550, but have a completely different programmer's model.
93 1.1 rearnsha * Derived from the NS16550AF com driver.
94 1.68 riastrad *
95 1.68 riastrad * Lock order:
96 1.68 riastrad * tty_lock (IPL_VM)
97 1.68 riastrad * -> sc->sc_lock (IPL_HIGH)
98 1.68 riastrad * -> timecounter_lock (IPL_HIGH)
99 1.1 rearnsha */
100 1.8 lukem
101 1.8 lukem #include <sys/cdefs.h>
102 1.68 riastrad __KERNEL_RCSID(0, "$NetBSD: plcom.c,v 1.68 2023/04/11 12:56:07 riastradh Exp $");
103 1.1 rearnsha
104 1.1 rearnsha #include "opt_plcom.h"
105 1.1 rearnsha #include "opt_kgdb.h"
106 1.7 martin #include "opt_lockdebug.h"
107 1.7 martin #include "opt_multiprocessor.h"
108 1.1 rearnsha
109 1.1 rearnsha /*
110 1.1 rearnsha * Override cnmagic(9) macro before including <sys/systm.h>.
111 1.1 rearnsha * We need to know if cn_check_magic triggered debugger, so set a flag.
112 1.1 rearnsha * Callers of cn_check_magic must declare int cn_trapped = 0;
113 1.1 rearnsha * XXX: this is *ugly*!
114 1.1 rearnsha */
115 1.1 rearnsha #define cn_trap() \
116 1.1 rearnsha do { \
117 1.1 rearnsha console_debugger(); \
118 1.1 rearnsha cn_trapped = 1; \
119 1.1 rearnsha } while (/* CONSTCOND */ 0)
120 1.1 rearnsha
121 1.1 rearnsha #include <sys/param.h>
122 1.1 rearnsha #include <sys/systm.h>
123 1.1 rearnsha #include <sys/ioctl.h>
124 1.1 rearnsha #include <sys/select.h>
125 1.1 rearnsha #include <sys/tty.h>
126 1.1 rearnsha #include <sys/proc.h>
127 1.1 rearnsha #include <sys/conf.h>
128 1.1 rearnsha #include <sys/file.h>
129 1.1 rearnsha #include <sys/uio.h>
130 1.1 rearnsha #include <sys/kernel.h>
131 1.1 rearnsha #include <sys/syslog.h>
132 1.1 rearnsha #include <sys/types.h>
133 1.1 rearnsha #include <sys/device.h>
134 1.65 skrll #include <sys/kmem.h>
135 1.1 rearnsha #include <sys/timepps.h>
136 1.1 rearnsha #include <sys/vnode.h>
137 1.16 elad #include <sys/kauth.h>
138 1.25 ad #include <sys/intr.h>
139 1.25 ad #include <sys/bus.h>
140 1.40 skrll #ifdef RND_COM
141 1.52 riastrad #include <sys/rndsource.h>
142 1.40 skrll #endif
143 1.1 rearnsha
144 1.66 riastrad #include <ddb/db_active.h>
145 1.66 riastrad
146 1.1 rearnsha #include <evbarm/dev/plcomreg.h>
147 1.1 rearnsha #include <evbarm/dev/plcomvar.h>
148 1.1 rearnsha
149 1.1 rearnsha #include <dev/cons.h>
150 1.1 rearnsha
151 1.1 rearnsha static void plcom_enable_debugport (struct plcom_softc *);
152 1.1 rearnsha
153 1.1 rearnsha void plcom_config (struct plcom_softc *);
154 1.1 rearnsha void plcom_shutdown (struct plcom_softc *);
155 1.40 skrll int pl010comspeed (long, long);
156 1.40 skrll int pl011comspeed (long, long);
157 1.67 mlelstv static uint32_t cflag2lcr (tcflag_t);
158 1.1 rearnsha int plcomparam (struct tty *, struct termios *);
159 1.1 rearnsha void plcomstart (struct tty *);
160 1.1 rearnsha int plcomhwiflow (struct tty *, int);
161 1.1 rearnsha
162 1.1 rearnsha void plcom_loadchannelregs (struct plcom_softc *);
163 1.1 rearnsha void plcom_hwiflow (struct plcom_softc *);
164 1.1 rearnsha void plcom_break (struct plcom_softc *, int);
165 1.1 rearnsha void plcom_modem (struct plcom_softc *, int);
166 1.1 rearnsha void tiocm_to_plcom (struct plcom_softc *, u_long, int);
167 1.1 rearnsha int plcom_to_tiocm (struct plcom_softc *);
168 1.1 rearnsha void plcom_iflush (struct plcom_softc *);
169 1.1 rearnsha
170 1.40 skrll int plcom_common_getc (dev_t, struct plcom_instance *);
171 1.40 skrll void plcom_common_putc (dev_t, struct plcom_instance *, int);
172 1.1 rearnsha
173 1.40 skrll int plcominit (struct plcom_instance *, int, int, tcflag_t);
174 1.1 rearnsha
175 1.4 gehenna dev_type_open(plcomopen);
176 1.4 gehenna dev_type_close(plcomclose);
177 1.4 gehenna dev_type_read(plcomread);
178 1.4 gehenna dev_type_write(plcomwrite);
179 1.4 gehenna dev_type_ioctl(plcomioctl);
180 1.4 gehenna dev_type_stop(plcomstop);
181 1.4 gehenna dev_type_tty(plcomtty);
182 1.4 gehenna dev_type_poll(plcompoll);
183 1.1 rearnsha
184 1.1 rearnsha int plcomcngetc (dev_t);
185 1.1 rearnsha void plcomcnputc (dev_t, int);
186 1.1 rearnsha void plcomcnpollc (dev_t, int);
187 1.59 jmcneill void plcomcnhalt (dev_t);
188 1.1 rearnsha
189 1.1 rearnsha void plcomsoft (void *);
190 1.68 riastrad static inline void plcom_rxsoft (struct plcom_softc *, struct tty *);
191 1.68 riastrad static inline void plcom_txsoft (struct plcom_softc *, struct tty *);
192 1.68 riastrad static inline void plcom_stsoft (struct plcom_softc *, struct tty *);
193 1.68 riastrad static inline void plcom_schedrx (struct plcom_softc *);
194 1.1 rearnsha void plcomdiag (void *);
195 1.1 rearnsha
196 1.40 skrll bool plcom_intstatus(struct plcom_instance *, u_int *);
197 1.40 skrll
198 1.1 rearnsha extern struct cfdriver plcom_cd;
199 1.1 rearnsha
200 1.4 gehenna const struct cdevsw plcom_cdevsw = {
201 1.48 dholland .d_open = plcomopen,
202 1.48 dholland .d_close = plcomclose,
203 1.48 dholland .d_read = plcomread,
204 1.48 dholland .d_write = plcomwrite,
205 1.48 dholland .d_ioctl = plcomioctl,
206 1.48 dholland .d_stop = plcomstop,
207 1.48 dholland .d_tty = plcomtty,
208 1.48 dholland .d_poll = plcompoll,
209 1.48 dholland .d_mmap = nommap,
210 1.48 dholland .d_kqfilter = ttykqfilter,
211 1.49 dholland .d_discard = nodiscard,
212 1.48 dholland .d_flag = D_TTY
213 1.4 gehenna };
214 1.4 gehenna
215 1.1 rearnsha /*
216 1.1 rearnsha * Make this an option variable one can patch.
217 1.1 rearnsha * But be warned: this must be a power of 2!
218 1.1 rearnsha */
219 1.1 rearnsha u_int plcom_rbuf_size = PLCOM_RING_SIZE;
220 1.1 rearnsha
221 1.1 rearnsha /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
222 1.1 rearnsha u_int plcom_rbuf_hiwat = (PLCOM_RING_SIZE * 1) / 4;
223 1.1 rearnsha u_int plcom_rbuf_lowat = (PLCOM_RING_SIZE * 3) / 4;
224 1.1 rearnsha
225 1.1 rearnsha static int plcomconsunit = -1;
226 1.40 skrll static struct plcom_instance plcomcons_info;
227 1.40 skrll
228 1.40 skrll static int plcomconsattached;
229 1.1 rearnsha static int plcomconsrate;
230 1.1 rearnsha static tcflag_t plcomconscflag;
231 1.1 rearnsha static struct cnm_state plcom_cnm_state;
232 1.1 rearnsha
233 1.1 rearnsha static int ppscap =
234 1.1 rearnsha PPS_TSFMT_TSPEC |
235 1.46 skrll PPS_CAPTUREASSERT |
236 1.1 rearnsha PPS_CAPTURECLEAR |
237 1.46 skrll #ifdef PPS_SYNC
238 1.1 rearnsha PPS_HARDPPSONASSERT | PPS_HARDPPSONCLEAR |
239 1.1 rearnsha #endif /* PPS_SYNC */
240 1.1 rearnsha PPS_OFFSETASSERT | PPS_OFFSETCLEAR;
241 1.1 rearnsha
242 1.1 rearnsha #ifdef KGDB
243 1.1 rearnsha #include <sys/kgdb.h>
244 1.1 rearnsha
245 1.40 skrll static struct plcom_instance plcomkgdb_info;
246 1.1 rearnsha static int plcom_kgdb_attached;
247 1.1 rearnsha
248 1.1 rearnsha int plcom_kgdb_getc (void *);
249 1.1 rearnsha void plcom_kgdb_putc (void *, int);
250 1.1 rearnsha #endif /* KGDB */
251 1.1 rearnsha
252 1.51 christos #define PLCOMDIALOUT_MASK TTDIALOUT_MASK
253 1.1 rearnsha
254 1.51 christos #define PLCOMUNIT(x) TTUNIT(x)
255 1.51 christos #define PLCOMDIALOUT(x) TTDIALOUT(x)
256 1.1 rearnsha
257 1.1 rearnsha #define PLCOM_ISALIVE(sc) ((sc)->enabled != 0 && \
258 1.38 skrll device_is_active((sc)->sc_dev))
259 1.1 rearnsha
260 1.1 rearnsha #define BR BUS_SPACE_BARRIER_READ
261 1.1 rearnsha #define BW BUS_SPACE_BARRIER_WRITE
262 1.40 skrll #define PLCOM_BARRIER(pi, f) \
263 1.40 skrll bus_space_barrier((pi)->pi_iot, (pi)->pi_ioh, 0, (pi)->pi_size, (f))
264 1.40 skrll
265 1.40 skrll static uint8_t
266 1.40 skrll pread1(struct plcom_instance *pi, bus_size_t reg)
267 1.40 skrll {
268 1.40 skrll if (!ISSET(pi->pi_flags, PLC_FLAG_32BIT_ACCESS))
269 1.40 skrll return bus_space_read_1(pi->pi_iot, pi->pi_ioh, reg);
270 1.40 skrll
271 1.40 skrll return bus_space_read_4(pi->pi_iot, pi->pi_ioh, reg & -4) >>
272 1.40 skrll (8 * (reg & 3));
273 1.40 skrll }
274 1.67 mlelstv
275 1.67 mlelstv static uint16_t
276 1.67 mlelstv pread2(struct plcom_instance *pi, bus_size_t reg)
277 1.67 mlelstv {
278 1.67 mlelstv if (!ISSET(pi->pi_flags, PLC_FLAG_32BIT_ACCESS))
279 1.67 mlelstv return bus_space_read_2(pi->pi_iot, pi->pi_ioh, reg);
280 1.67 mlelstv
281 1.67 mlelstv return bus_space_read_4(pi->pi_iot, pi->pi_ioh, reg & -4) >>
282 1.67 mlelstv (8 * (reg & 3));
283 1.67 mlelstv }
284 1.67 mlelstv
285 1.40 skrll static void
286 1.40 skrll pwrite1(struct plcom_instance *pi, bus_size_t o, uint8_t val)
287 1.40 skrll {
288 1.40 skrll if (!ISSET(pi->pi_flags, PLC_FLAG_32BIT_ACCESS)) {
289 1.40 skrll bus_space_write_1(pi->pi_iot, pi->pi_ioh, o, val);
290 1.40 skrll } else {
291 1.40 skrll const size_t shift = 8 * (o & 3);
292 1.40 skrll o &= -4;
293 1.40 skrll uint32_t tmp = bus_space_read_4(pi->pi_iot, pi->pi_ioh, o);
294 1.40 skrll tmp = (val << shift) | (tmp & ~(0xff << shift));
295 1.40 skrll bus_space_write_4(pi->pi_iot, pi->pi_ioh, o, tmp);
296 1.40 skrll }
297 1.40 skrll }
298 1.40 skrll
299 1.40 skrll static void
300 1.67 mlelstv pwrite2(struct plcom_instance *pi, bus_size_t o, uint16_t val)
301 1.67 mlelstv {
302 1.67 mlelstv if (!ISSET(pi->pi_flags, PLC_FLAG_32BIT_ACCESS)) {
303 1.67 mlelstv bus_space_write_2(pi->pi_iot, pi->pi_ioh, o, val);
304 1.67 mlelstv } else {
305 1.67 mlelstv const size_t shift = 8 * (o & 3);
306 1.67 mlelstv o &= -4;
307 1.67 mlelstv uint32_t tmp = bus_space_read_4(pi->pi_iot, pi->pi_ioh, o);
308 1.67 mlelstv tmp = (val << shift) | (tmp & ~(0xffff << shift));
309 1.67 mlelstv bus_space_write_4(pi->pi_iot, pi->pi_ioh, o, tmp);
310 1.67 mlelstv }
311 1.67 mlelstv }
312 1.67 mlelstv
313 1.67 mlelstv static void
314 1.40 skrll pwritem1(struct plcom_instance *pi, bus_size_t o, const uint8_t *datap,
315 1.40 skrll bus_size_t count)
316 1.40 skrll {
317 1.40 skrll if (!ISSET(pi->pi_flags, PLC_FLAG_32BIT_ACCESS)) {
318 1.40 skrll bus_space_write_multi_1(pi->pi_iot, pi->pi_ioh, o, datap, count);
319 1.40 skrll } else {
320 1.40 skrll KASSERT((o & 3) == 0);
321 1.40 skrll while (count--) {
322 1.40 skrll bus_space_write_4(pi->pi_iot, pi->pi_ioh, o, *datap++);
323 1.40 skrll };
324 1.40 skrll }
325 1.40 skrll }
326 1.40 skrll
327 1.40 skrll #define PREAD1(pi, reg) pread1(pi, reg)
328 1.67 mlelstv #define PREAD2(pi, reg) pread2(pi, reg)
329 1.40 skrll #define PREAD4(pi, reg) \
330 1.62 tnn bus_space_read_4((pi)->pi_iot, (pi)->pi_ioh, (reg))
331 1.40 skrll
332 1.40 skrll #define PWRITE1(pi, reg, val) pwrite1(pi, reg, val)
333 1.40 skrll #define PWRITEM1(pi, reg, d, c) pwritem1(pi, reg, d, c)
334 1.67 mlelstv #define PWRITE2(pi, reg, val) pwrite2(pi, reg, val)
335 1.40 skrll #define PWRITE4(pi, reg, val) \
336 1.62 tnn bus_space_write_4((pi)->pi_iot, (pi)->pi_ioh, (reg), (val))
337 1.1 rearnsha
338 1.1 rearnsha int
339 1.40 skrll pl010comspeed(long speed, long frequency)
340 1.1 rearnsha {
341 1.1 rearnsha #define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */
342 1.1 rearnsha
343 1.1 rearnsha int x, err;
344 1.1 rearnsha
345 1.1 rearnsha #if 0
346 1.1 rearnsha if (speed == 0)
347 1.1 rearnsha return 0;
348 1.1 rearnsha #endif
349 1.1 rearnsha if (speed <= 0)
350 1.1 rearnsha return -1;
351 1.1 rearnsha x = divrnd(frequency / 16, speed);
352 1.1 rearnsha if (x <= 0)
353 1.1 rearnsha return -1;
354 1.1 rearnsha err = divrnd(((quad_t)frequency) * 1000 / 16, speed * x) - 1000;
355 1.1 rearnsha if (err < 0)
356 1.1 rearnsha err = -err;
357 1.1 rearnsha if (err > PLCOM_TOLERANCE)
358 1.1 rearnsha return -1;
359 1.1 rearnsha return x;
360 1.1 rearnsha
361 1.1 rearnsha #undef divrnd
362 1.1 rearnsha }
363 1.1 rearnsha
364 1.40 skrll int
365 1.40 skrll pl011comspeed(long speed, long frequency)
366 1.40 skrll {
367 1.40 skrll int denom = 16 * speed;
368 1.40 skrll int div = frequency / denom;
369 1.40 skrll int rem = frequency % denom;
370 1.46 skrll
371 1.40 skrll int ibrd = div << 6;
372 1.40 skrll int fbrd = (((8 * rem) / speed) + 1) / 2;
373 1.46 skrll
374 1.40 skrll /* Tolerance? */
375 1.40 skrll return ibrd | fbrd;
376 1.40 skrll }
377 1.40 skrll
378 1.1 rearnsha #ifdef PLCOM_DEBUG
379 1.1 rearnsha int plcom_debug = 0;
380 1.1 rearnsha
381 1.34 bsh void plcomstatus (struct plcom_softc *, const char *);
382 1.1 rearnsha void
383 1.34 bsh plcomstatus(struct plcom_softc *sc, const char *str)
384 1.1 rearnsha {
385 1.1 rearnsha struct tty *tp = sc->sc_tty;
386 1.1 rearnsha
387 1.1 rearnsha printf("%s: %s %sclocal %sdcd %sts_carr_on %sdtr %stx_stopped\n",
388 1.38 skrll device_xname(sc->sc_dev), str,
389 1.1 rearnsha ISSET(tp->t_cflag, CLOCAL) ? "+" : "-",
390 1.35 skrll ISSET(sc->sc_msr, PL01X_MSR_DCD) ? "+" : "-",
391 1.1 rearnsha ISSET(tp->t_state, TS_CARR_ON) ? "+" : "-",
392 1.35 skrll ISSET(sc->sc_mcr, PL01X_MCR_DTR) ? "+" : "-",
393 1.1 rearnsha sc->sc_tx_stopped ? "+" : "-");
394 1.1 rearnsha
395 1.1 rearnsha printf("%s: %s %scrtscts %scts %sts_ttstop %srts %xrx_flags\n",
396 1.38 skrll device_xname(sc->sc_dev), str,
397 1.1 rearnsha ISSET(tp->t_cflag, CRTSCTS) ? "+" : "-",
398 1.35 skrll ISSET(sc->sc_msr, PL01X_MSR_CTS) ? "+" : "-",
399 1.1 rearnsha ISSET(tp->t_state, TS_TTSTOP) ? "+" : "-",
400 1.35 skrll ISSET(sc->sc_mcr, PL01X_MCR_RTS) ? "+" : "-",
401 1.1 rearnsha sc->sc_rx_flags);
402 1.1 rearnsha }
403 1.1 rearnsha #endif
404 1.1 rearnsha
405 1.40 skrll #if 0
406 1.1 rearnsha int
407 1.1 rearnsha plcomprobe1(bus_space_tag_t iot, bus_space_handle_t ioh)
408 1.1 rearnsha {
409 1.1 rearnsha int data;
410 1.1 rearnsha
411 1.1 rearnsha /* Disable the UART. */
412 1.1 rearnsha bus_space_write_1(iot, ioh, plcom_cr, 0);
413 1.1 rearnsha /* Make sure the FIFO is off. */
414 1.67 mlelstv bus_space_write_4(iot, ioh, plcom_lcr, PL01X_LCR_8BITS);
415 1.1 rearnsha /* Disable interrupts. */
416 1.1 rearnsha bus_space_write_1(iot, ioh, plcom_iir, 0);
417 1.1 rearnsha
418 1.1 rearnsha /* Make sure we swallow anything in the receiving register. */
419 1.1 rearnsha data = bus_space_read_1(iot, ioh, plcom_dr);
420 1.1 rearnsha
421 1.67 mlelstv if (bus_space_read_4(iot, ioh, plcom_lcr) != PL01X_LCR_8BITS)
422 1.1 rearnsha return 0;
423 1.1 rearnsha
424 1.35 skrll data = bus_space_read_1(iot, ioh, plcom_fr) & (PL01X_FR_RXFF | PL01X_FR_RXFE);
425 1.1 rearnsha
426 1.35 skrll if (data != PL01X_FR_RXFE)
427 1.1 rearnsha return 0;
428 1.1 rearnsha
429 1.1 rearnsha return 1;
430 1.1 rearnsha }
431 1.40 skrll #endif
432 1.1 rearnsha
433 1.36 skrll /*
434 1.36 skrll * No locking in this routine; it is only called during attach,
435 1.36 skrll * or with the port already locked.
436 1.36 skrll */
437 1.1 rearnsha static void
438 1.1 rearnsha plcom_enable_debugport(struct plcom_softc *sc)
439 1.1 rearnsha {
440 1.40 skrll struct plcom_instance *pi = &sc->sc_pi;
441 1.40 skrll
442 1.40 skrll sc->sc_cr = PL01X_CR_UARTEN;
443 1.40 skrll SET(sc->sc_mcr, PL01X_MCR_DTR | PL01X_MCR_RTS);
444 1.1 rearnsha
445 1.1 rearnsha /* Turn on line break interrupt, set carrier. */
446 1.40 skrll switch (pi->pi_type) {
447 1.40 skrll case PLCOM_TYPE_PL010:
448 1.40 skrll SET(sc->sc_cr, PL010_CR_RIE | PL010_CR_RTIE);
449 1.40 skrll PWRITE1(pi, PL010COM_CR, sc->sc_cr);
450 1.40 skrll if (sc->sc_set_mcr) {
451 1.40 skrll /* XXX device_unit() abuse */
452 1.40 skrll sc->sc_set_mcr(sc->sc_set_mcr_arg,
453 1.40 skrll device_unit(sc->sc_dev), sc->sc_mcr);
454 1.40 skrll }
455 1.40 skrll break;
456 1.40 skrll case PLCOM_TYPE_PL011:
457 1.67 mlelstv case PLCOM_TYPE_GENERIC_UART:
458 1.40 skrll sc->sc_imsc = PL011_INT_RX | PL011_INT_RT;
459 1.40 skrll SET(sc->sc_cr, PL011_CR_RXE | PL011_CR_TXE);
460 1.40 skrll SET(sc->sc_cr, PL011_MCR(sc->sc_mcr));
461 1.67 mlelstv PWRITE2(pi, PL011COM_CR, sc->sc_cr);
462 1.40 skrll PWRITE4(pi, PL011COM_IMSC, sc->sc_imsc);
463 1.40 skrll break;
464 1.40 skrll }
465 1.40 skrll
466 1.1 rearnsha }
467 1.1 rearnsha
468 1.1 rearnsha void
469 1.1 rearnsha plcom_attach_subr(struct plcom_softc *sc)
470 1.1 rearnsha {
471 1.40 skrll struct plcom_instance *pi = &sc->sc_pi;
472 1.1 rearnsha struct tty *tp;
473 1.1 rearnsha
474 1.21 ad callout_init(&sc->sc_diag_callout, 0);
475 1.36 skrll mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_HIGH);
476 1.1 rearnsha
477 1.40 skrll switch (pi->pi_type) {
478 1.40 skrll case PLCOM_TYPE_PL010:
479 1.40 skrll case PLCOM_TYPE_PL011:
480 1.67 mlelstv case PLCOM_TYPE_GENERIC_UART:
481 1.40 skrll break;
482 1.40 skrll default:
483 1.40 skrll aprint_error_dev(sc->sc_dev,
484 1.40 skrll "Unknown plcom type: %d\n", pi->pi_type);
485 1.40 skrll return;
486 1.40 skrll }
487 1.40 skrll
488 1.1 rearnsha /* Disable interrupts before configuring the device. */
489 1.1 rearnsha sc->sc_cr = 0;
490 1.40 skrll sc->sc_imsc = 0;
491 1.1 rearnsha
492 1.40 skrll if (bus_space_is_equal(pi->pi_iot, plcomcons_info.pi_iot) &&
493 1.40 skrll pi->pi_iobase == plcomcons_info.pi_iobase) {
494 1.1 rearnsha plcomconsattached = 1;
495 1.1 rearnsha
496 1.1 rearnsha /* Make sure the console is always "hardwired". */
497 1.1 rearnsha delay(1000); /* wait for output to finish */
498 1.1 rearnsha SET(sc->sc_hwflags, PLCOM_HW_CONSOLE);
499 1.1 rearnsha SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
500 1.40 skrll /*
501 1.40 skrll * Must re-enable the console immediately, or we will
502 1.40 skrll * hang when trying to print.
503 1.40 skrll */
504 1.35 skrll sc->sc_cr = PL01X_CR_UARTEN;
505 1.67 mlelstv switch (pi->pi_type) {
506 1.67 mlelstv case PLCOM_TYPE_PL011:
507 1.67 mlelstv case PLCOM_TYPE_GENERIC_UART:
508 1.40 skrll SET(sc->sc_cr, PL011_CR_RXE | PL011_CR_TXE);
509 1.67 mlelstv break;
510 1.67 mlelstv }
511 1.67 mlelstv }
512 1.67 mlelstv
513 1.67 mlelstv switch (pi->pi_type) {
514 1.67 mlelstv case PLCOM_TYPE_PL011:
515 1.67 mlelstv if (pi->pi_periphid == 0) {
516 1.67 mlelstv pi->pi_periphid = PREAD1(pi, PL011COM_PID0) << 0
517 1.67 mlelstv | PREAD1(pi, PL011COM_PID1) << 8
518 1.67 mlelstv | PREAD1(pi, PL011COM_PID2) << 16
519 1.67 mlelstv | PREAD1(pi, PL011COM_PID3) << 24;
520 1.67 mlelstv }
521 1.67 mlelstv aprint_debug_dev(sc->sc_dev, "PID %08x\n", pi->pi_periphid);
522 1.67 mlelstv break;
523 1.1 rearnsha }
524 1.1 rearnsha
525 1.40 skrll switch (pi->pi_type) {
526 1.40 skrll case PLCOM_TYPE_PL010:
527 1.40 skrll PWRITE1(pi, PL010COM_CR, sc->sc_cr);
528 1.40 skrll break;
529 1.46 skrll
530 1.40 skrll case PLCOM_TYPE_PL011:
531 1.67 mlelstv case PLCOM_TYPE_GENERIC_UART:
532 1.67 mlelstv PWRITE2(pi, PL011COM_CR, sc->sc_cr);
533 1.40 skrll PWRITE4(pi, PL011COM_IMSC, sc->sc_imsc);
534 1.40 skrll break;
535 1.46 skrll }
536 1.40 skrll
537 1.40 skrll if (sc->sc_fifolen == 0) {
538 1.40 skrll switch (pi->pi_type) {
539 1.40 skrll case PLCOM_TYPE_PL010:
540 1.40 skrll /*
541 1.40 skrll * The PL010 has a 16-byte fifo, but the tx interrupt
542 1.40 skrll * triggers when there is space for 8 more bytes.
543 1.40 skrll */
544 1.42 skrll sc->sc_fifolen = 8;
545 1.40 skrll break;
546 1.40 skrll case PLCOM_TYPE_PL011:
547 1.40 skrll /* Some revisions have a 32 byte TX FIFO */
548 1.67 mlelstv switch (pi->pi_periphid & (PL011_DESIGNER_MASK | PL011_HWREV_MASK)) {
549 1.67 mlelstv case PL011_DESIGNER_ARM | __SHIFTIN(0, PL011_HWREV_MASK):
550 1.67 mlelstv case PL011_DESIGNER_ARM | __SHIFTIN(1, PL011_HWREV_MASK):
551 1.67 mlelstv case PL011_DESIGNER_ARM | __SHIFTIN(2, PL011_HWREV_MASK):
552 1.67 mlelstv sc->sc_fifolen = 16;
553 1.67 mlelstv break;
554 1.67 mlelstv default:
555 1.67 mlelstv sc->sc_fifolen = 32;
556 1.67 mlelstv break;
557 1.67 mlelstv }
558 1.67 mlelstv break;
559 1.67 mlelstv case PLCOM_TYPE_GENERIC_UART:
560 1.67 mlelstv /* At least 32 byte TX FIFO */
561 1.67 mlelstv sc->sc_fifolen = 32;
562 1.40 skrll break;
563 1.40 skrll }
564 1.40 skrll }
565 1.1 rearnsha
566 1.67 mlelstv /* Safe amount of bytes to fill when TX FIFO signals empty */
567 1.67 mlelstv sc->sc_burstlen = 1;
568 1.67 mlelstv
569 1.1 rearnsha if (ISSET(sc->sc_hwflags, PLCOM_HW_TXFIFO_DISABLE)) {
570 1.1 rearnsha sc->sc_fifolen = 1;
571 1.40 skrll aprint_normal_dev(sc->sc_dev, "txfifo disabled\n");
572 1.1 rearnsha }
573 1.1 rearnsha
574 1.67 mlelstv if (sc->sc_fifolen > 1) {
575 1.1 rearnsha SET(sc->sc_hwflags, PLCOM_HW_FIFO);
576 1.67 mlelstv aprint_normal_dev(sc->sc_dev, "txfifo %u bytes\n", sc->sc_fifolen);
577 1.67 mlelstv }
578 1.1 rearnsha
579 1.32 rmind tp = tty_alloc();
580 1.1 rearnsha tp->t_oproc = plcomstart;
581 1.1 rearnsha tp->t_param = plcomparam;
582 1.1 rearnsha tp->t_hwiflow = plcomhwiflow;
583 1.1 rearnsha
584 1.1 rearnsha sc->sc_tty = tp;
585 1.65 skrll sc->sc_rbuf = kmem_alloc(plcom_rbuf_size << 1, KM_SLEEP);
586 1.1 rearnsha sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
587 1.1 rearnsha sc->sc_rbavail = plcom_rbuf_size;
588 1.1 rearnsha sc->sc_ebuf = sc->sc_rbuf + (plcom_rbuf_size << 1);
589 1.1 rearnsha
590 1.1 rearnsha tty_attach(tp);
591 1.1 rearnsha
592 1.1 rearnsha if (ISSET(sc->sc_hwflags, PLCOM_HW_CONSOLE)) {
593 1.1 rearnsha int maj;
594 1.1 rearnsha
595 1.1 rearnsha /* locate the major number */
596 1.4 gehenna maj = cdevsw_lookup_major(&plcom_cdevsw);
597 1.1 rearnsha
598 1.40 skrll tp->t_dev = cn_tab->cn_dev = makedev(maj, device_unit(sc->sc_dev));
599 1.1 rearnsha
600 1.40 skrll aprint_normal_dev(sc->sc_dev, "console\n");
601 1.1 rearnsha }
602 1.1 rearnsha
603 1.1 rearnsha #ifdef KGDB
604 1.1 rearnsha /*
605 1.1 rearnsha * Allow kgdb to "take over" this port. If this is
606 1.1 rearnsha * the kgdb device, it has exclusive use.
607 1.1 rearnsha */
608 1.44 mlelstv if (bus_space_is_equal(pi->pi_iot, plcomkgdb_info.pi_iot) &&
609 1.40 skrll pi->pi_iobase == plcomkgdb_info.pi_iobase) {
610 1.40 skrll if (!ISSET(sc->sc_hwflags, PLCOM_HW_CONSOLE)) {
611 1.40 skrll plcom_kgdb_attached = 1;
612 1.1 rearnsha
613 1.40 skrll SET(sc->sc_hwflags, PLCOM_HW_KGDB);
614 1.40 skrll }
615 1.40 skrll aprint_normal_dev(sc->sc_dev, "kgdb\n");
616 1.1 rearnsha }
617 1.1 rearnsha #endif
618 1.1 rearnsha
619 1.25 ad sc->sc_si = softint_establish(SOFTINT_SERIAL, plcomsoft, sc);
620 1.1 rearnsha
621 1.33 tls #ifdef RND_COM
622 1.40 skrll rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
623 1.50 tls RND_TYPE_TTY, RND_FLAG_DEFAULT);
624 1.1 rearnsha #endif
625 1.1 rearnsha
626 1.40 skrll /*
627 1.40 skrll * if there are no enable/disable functions, assume the device
628 1.40 skrll * is always enabled
629 1.40 skrll */
630 1.1 rearnsha if (!sc->enable)
631 1.1 rearnsha sc->enabled = 1;
632 1.1 rearnsha
633 1.1 rearnsha plcom_config(sc);
634 1.1 rearnsha
635 1.1 rearnsha SET(sc->sc_hwflags, PLCOM_HW_DEV_OK);
636 1.1 rearnsha }
637 1.1 rearnsha
638 1.1 rearnsha void
639 1.1 rearnsha plcom_config(struct plcom_softc *sc)
640 1.1 rearnsha {
641 1.40 skrll struct plcom_instance *pi = &sc->sc_pi;
642 1.1 rearnsha
643 1.1 rearnsha /* Disable interrupts before configuring the device. */
644 1.1 rearnsha sc->sc_cr = 0;
645 1.40 skrll sc->sc_imsc = 0;
646 1.40 skrll switch (pi->pi_type) {
647 1.40 skrll case PLCOM_TYPE_PL010:
648 1.40 skrll PWRITE1(pi, PL010COM_CR, sc->sc_cr);
649 1.40 skrll break;
650 1.46 skrll
651 1.40 skrll case PLCOM_TYPE_PL011:
652 1.67 mlelstv case PLCOM_TYPE_GENERIC_UART:
653 1.67 mlelstv PWRITE2(pi, PL011COM_CR, sc->sc_cr);
654 1.40 skrll PWRITE4(pi, PL011COM_IMSC, sc->sc_imsc);
655 1.40 skrll break;
656 1.46 skrll }
657 1.1 rearnsha
658 1.1 rearnsha if (ISSET(sc->sc_hwflags, PLCOM_HW_CONSOLE|PLCOM_HW_KGDB))
659 1.1 rearnsha plcom_enable_debugport(sc);
660 1.1 rearnsha }
661 1.1 rearnsha
662 1.1 rearnsha int
663 1.38 skrll plcom_detach(device_t self, int flags)
664 1.1 rearnsha {
665 1.38 skrll struct plcom_softc *sc = device_private(self);
666 1.1 rearnsha int maj, mn;
667 1.1 rearnsha
668 1.31 dyoung if (sc->sc_hwflags & (PLCOM_HW_CONSOLE|PLCOM_HW_KGDB))
669 1.31 dyoung return EBUSY;
670 1.31 dyoung
671 1.31 dyoung if (sc->disable != NULL && sc->enabled != 0) {
672 1.31 dyoung (*sc->disable)(sc);
673 1.31 dyoung sc->enabled = 0;
674 1.31 dyoung }
675 1.31 dyoung
676 1.1 rearnsha /* locate the major number */
677 1.4 gehenna maj = cdevsw_lookup_major(&plcom_cdevsw);
678 1.1 rearnsha
679 1.1 rearnsha /* Nuke the vnodes for any open instances. */
680 1.15 thorpej mn = device_unit(self);
681 1.1 rearnsha vdevgone(maj, mn, mn, VCHR);
682 1.1 rearnsha
683 1.1 rearnsha mn |= PLCOMDIALOUT_MASK;
684 1.1 rearnsha vdevgone(maj, mn, mn, VCHR);
685 1.1 rearnsha
686 1.40 skrll if (sc->sc_rbuf == NULL) {
687 1.40 skrll /*
688 1.40 skrll * Ring buffer allocation failed in the plcom_attach_subr,
689 1.40 skrll * only the tty is allocated, and nothing else.
690 1.46 skrll */
691 1.40 skrll tty_free(sc->sc_tty);
692 1.40 skrll return 0;
693 1.40 skrll }
694 1.40 skrll
695 1.1 rearnsha /* Free the receive buffer. */
696 1.65 skrll kmem_free(sc->sc_rbuf, sc->sc_ebuf - sc->sc_rbuf);
697 1.1 rearnsha
698 1.1 rearnsha /* Detach and free the tty. */
699 1.1 rearnsha tty_detach(sc->sc_tty);
700 1.32 rmind tty_free(sc->sc_tty);
701 1.1 rearnsha
702 1.1 rearnsha /* Unhook the soft interrupt handler. */
703 1.25 ad softint_disestablish(sc->sc_si);
704 1.1 rearnsha
705 1.33 tls #ifdef RND_COM
706 1.1 rearnsha /* Unhook the entropy source. */
707 1.1 rearnsha rnd_detach_source(&sc->rnd_source);
708 1.1 rearnsha #endif
709 1.40 skrll callout_destroy(&sc->sc_diag_callout);
710 1.1 rearnsha
711 1.36 skrll /* Destroy the lock. */
712 1.36 skrll mutex_destroy(&sc->sc_lock);
713 1.36 skrll
714 1.1 rearnsha return 0;
715 1.1 rearnsha }
716 1.1 rearnsha
717 1.1 rearnsha int
718 1.31 dyoung plcom_activate(device_t self, enum devact act)
719 1.1 rearnsha {
720 1.31 dyoung struct plcom_softc *sc = device_private(self);
721 1.1 rearnsha
722 1.1 rearnsha switch (act) {
723 1.1 rearnsha case DVACT_DEACTIVATE:
724 1.31 dyoung sc->enabled = 0;
725 1.31 dyoung return 0;
726 1.31 dyoung default:
727 1.31 dyoung return EOPNOTSUPP;
728 1.1 rearnsha }
729 1.1 rearnsha }
730 1.1 rearnsha
731 1.1 rearnsha void
732 1.1 rearnsha plcom_shutdown(struct plcom_softc *sc)
733 1.1 rearnsha {
734 1.40 skrll struct plcom_instance *pi = &sc->sc_pi;
735 1.1 rearnsha struct tty *tp = sc->sc_tty;
736 1.36 skrll mutex_spin_enter(&sc->sc_lock);
737 1.1 rearnsha
738 1.1 rearnsha /* If we were asserting flow control, then deassert it. */
739 1.1 rearnsha SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
740 1.1 rearnsha plcom_hwiflow(sc);
741 1.1 rearnsha
742 1.1 rearnsha /* Clear any break condition set with TIOCSBRK. */
743 1.1 rearnsha plcom_break(sc, 0);
744 1.1 rearnsha
745 1.1 rearnsha /* Turn off PPS capture on last close. */
746 1.26 ad mutex_spin_enter(&timecounter_lock);
747 1.1 rearnsha sc->sc_ppsmask = 0;
748 1.1 rearnsha sc->ppsparam.mode = 0;
749 1.26 ad mutex_spin_exit(&timecounter_lock);
750 1.1 rearnsha
751 1.1 rearnsha /*
752 1.68 riastrad * Hang up if necessary. Record when we hung up, so if we
753 1.68 riastrad * immediately open the port again, we will wait a bit until
754 1.68 riastrad * the other side has had time to notice that we hung up.
755 1.1 rearnsha */
756 1.1 rearnsha if (ISSET(tp->t_cflag, HUPCL)) {
757 1.1 rearnsha plcom_modem(sc, 0);
758 1.64 jmcneill microuptime(&sc->sc_hup_pending);
759 1.63 jmcneill sc->sc_hup_pending.tv_sec++;
760 1.1 rearnsha }
761 1.1 rearnsha
762 1.40 skrll sc->sc_cr = 0;
763 1.40 skrll sc->sc_imsc = 0;
764 1.1 rearnsha /* Turn off interrupts. */
765 1.40 skrll if (ISSET(sc->sc_hwflags, PLCOM_HW_CONSOLE)) {
766 1.1 rearnsha /* interrupt on break */
767 1.46 skrll
768 1.40 skrll sc->sc_cr = PL01X_CR_UARTEN;
769 1.40 skrll sc->sc_imsc = 0;
770 1.40 skrll switch (pi->pi_type) {
771 1.40 skrll case PLCOM_TYPE_PL010:
772 1.40 skrll SET(sc->sc_cr, PL010_CR_RIE | PL010_CR_RTIE);
773 1.40 skrll break;
774 1.40 skrll case PLCOM_TYPE_PL011:
775 1.67 mlelstv case PLCOM_TYPE_GENERIC_UART:
776 1.40 skrll SET(sc->sc_cr, PL011_CR_RXE);
777 1.40 skrll SET(sc->sc_imsc, PL011_INT_RT | PL011_INT_RX);
778 1.40 skrll break;
779 1.40 skrll }
780 1.40 skrll }
781 1.40 skrll switch (pi->pi_type) {
782 1.40 skrll case PLCOM_TYPE_PL010:
783 1.40 skrll SET(sc->sc_cr, PL010_CR_RIE | PL010_CR_RTIE);
784 1.40 skrll PWRITE1(pi, PL010COM_CR, sc->sc_cr);
785 1.40 skrll break;
786 1.40 skrll case PLCOM_TYPE_PL011:
787 1.67 mlelstv case PLCOM_TYPE_GENERIC_UART:
788 1.40 skrll SET(sc->sc_cr, PL011_CR_RXE | PL011_CR_TXE);
789 1.40 skrll SET(sc->sc_imsc, PL011_INT_RT | PL011_INT_RX);
790 1.67 mlelstv PWRITE2(pi, PL011COM_CR, sc->sc_cr);
791 1.40 skrll PWRITE4(pi, PL011COM_IMSC, sc->sc_imsc);
792 1.40 skrll break;
793 1.40 skrll }
794 1.1 rearnsha
795 1.36 skrll mutex_spin_exit(&sc->sc_lock);
796 1.1 rearnsha if (sc->disable) {
797 1.1 rearnsha #ifdef DIAGNOSTIC
798 1.1 rearnsha if (!sc->enabled)
799 1.1 rearnsha panic("plcom_shutdown: not enabled?");
800 1.1 rearnsha #endif
801 1.1 rearnsha (*sc->disable)(sc);
802 1.1 rearnsha sc->enabled = 0;
803 1.1 rearnsha }
804 1.1 rearnsha }
805 1.1 rearnsha
806 1.1 rearnsha int
807 1.12 christos plcomopen(dev_t dev, int flag, int mode, struct lwp *l)
808 1.1 rearnsha {
809 1.1 rearnsha struct plcom_softc *sc;
810 1.40 skrll struct plcom_instance *pi;
811 1.1 rearnsha struct tty *tp;
812 1.36 skrll int s;
813 1.1 rearnsha int error;
814 1.1 rearnsha
815 1.28 cegger sc = device_lookup_private(&plcom_cd, PLCOMUNIT(dev));
816 1.1 rearnsha if (sc == NULL || !ISSET(sc->sc_hwflags, PLCOM_HW_DEV_OK) ||
817 1.1 rearnsha sc->sc_rbuf == NULL)
818 1.1 rearnsha return ENXIO;
819 1.1 rearnsha
820 1.38 skrll if (!device_is_active(sc->sc_dev))
821 1.1 rearnsha return ENXIO;
822 1.1 rearnsha
823 1.40 skrll pi = &sc->sc_pi;
824 1.40 skrll
825 1.1 rearnsha #ifdef KGDB
826 1.1 rearnsha /*
827 1.1 rearnsha * If this is the kgdb port, no other use is permitted.
828 1.1 rearnsha */
829 1.1 rearnsha if (ISSET(sc->sc_hwflags, PLCOM_HW_KGDB))
830 1.1 rearnsha return EBUSY;
831 1.1 rearnsha #endif
832 1.1 rearnsha
833 1.1 rearnsha tp = sc->sc_tty;
834 1.1 rearnsha
835 1.18 elad if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
836 1.18 elad return (EBUSY);
837 1.1 rearnsha
838 1.1 rearnsha s = spltty();
839 1.1 rearnsha
840 1.1 rearnsha /*
841 1.1 rearnsha * Do the following iff this is a first open.
842 1.1 rearnsha */
843 1.1 rearnsha if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
844 1.1 rearnsha struct termios t;
845 1.63 jmcneill struct timeval now, diff;
846 1.1 rearnsha
847 1.1 rearnsha tp->t_dev = dev;
848 1.1 rearnsha
849 1.1 rearnsha if (sc->enable) {
850 1.1 rearnsha if ((*sc->enable)(sc)) {
851 1.1 rearnsha splx(s);
852 1.40 skrll aprint_error_dev(sc->sc_dev,
853 1.40 skrll "device enable failed\n");
854 1.1 rearnsha return EIO;
855 1.1 rearnsha }
856 1.36 skrll mutex_spin_enter(&sc->sc_lock);
857 1.1 rearnsha sc->enabled = 1;
858 1.1 rearnsha plcom_config(sc);
859 1.36 skrll } else {
860 1.36 skrll mutex_spin_enter(&sc->sc_lock);
861 1.1 rearnsha }
862 1.1 rearnsha
863 1.63 jmcneill if (timerisset(&sc->sc_hup_pending)) {
864 1.64 jmcneill microuptime(&now);
865 1.63 jmcneill while (timercmp(&now, &sc->sc_hup_pending, <)) {
866 1.63 jmcneill timersub(&sc->sc_hup_pending, &now, &diff);
867 1.63 jmcneill const int ms = diff.tv_sec * 100 +
868 1.64 jmcneill diff.tv_usec / 1000;
869 1.64 jmcneill kpause(ttclos, false, uimax(mstohz(ms), 1),
870 1.64 jmcneill &sc->sc_lock);
871 1.64 jmcneill microuptime(&now);
872 1.63 jmcneill }
873 1.63 jmcneill timerclear(&sc->sc_hup_pending);
874 1.63 jmcneill }
875 1.63 jmcneill
876 1.1 rearnsha /* Turn on interrupts. */
877 1.1 rearnsha /* IER_ERXRDY | IER_ERLS | IER_EMSC; */
878 1.1 rearnsha /* Fetch the current modem control status, needed later. */
879 1.40 skrll sc->sc_cr = PL01X_CR_UARTEN;
880 1.40 skrll switch (pi->pi_type) {
881 1.40 skrll case PLCOM_TYPE_PL010:
882 1.40 skrll SET(sc->sc_cr,
883 1.40 skrll PL010_CR_RIE | PL010_CR_RTIE | PL010_CR_MSIE);
884 1.40 skrll PWRITE1(pi, PL010COM_CR, sc->sc_cr);
885 1.40 skrll sc->sc_msr = PREAD1(pi, PL01XCOM_FR);
886 1.40 skrll break;
887 1.40 skrll case PLCOM_TYPE_PL011:
888 1.67 mlelstv case PLCOM_TYPE_GENERIC_UART:
889 1.40 skrll SET(sc->sc_cr, PL011_CR_RXE | PL011_CR_TXE);
890 1.40 skrll SET(sc->sc_imsc, PL011_INT_RT | PL011_INT_RX |
891 1.40 skrll PL011_INT_MSMASK);
892 1.40 skrll PWRITE4(pi, PL011COM_IMSC, sc->sc_imsc);
893 1.40 skrll sc->sc_msr = PREAD4(pi, PL01XCOM_FR);
894 1.40 skrll break;
895 1.40 skrll }
896 1.1 rearnsha
897 1.1 rearnsha /* Clear PPS capture state on first open. */
898 1.26 ad
899 1.26 ad mutex_spin_enter(&timecounter_lock);
900 1.1 rearnsha sc->sc_ppsmask = 0;
901 1.1 rearnsha sc->ppsparam.mode = 0;
902 1.26 ad mutex_spin_exit(&timecounter_lock);
903 1.1 rearnsha
904 1.39 skrll mutex_spin_exit(&sc->sc_lock);
905 1.1 rearnsha
906 1.1 rearnsha /*
907 1.1 rearnsha * Initialize the termios status to the defaults. Add in the
908 1.1 rearnsha * sticky bits from TIOCSFLAGS.
909 1.1 rearnsha */
910 1.1 rearnsha if (ISSET(sc->sc_hwflags, PLCOM_HW_CONSOLE)) {
911 1.1 rearnsha t.c_ospeed = plcomconsrate;
912 1.1 rearnsha t.c_cflag = plcomconscflag;
913 1.1 rearnsha } else {
914 1.1 rearnsha t.c_ospeed = TTYDEF_SPEED;
915 1.1 rearnsha t.c_cflag = TTYDEF_CFLAG;
916 1.1 rearnsha }
917 1.40 skrll t.c_ispeed = t.c_ospeed;
918 1.40 skrll
919 1.1 rearnsha if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
920 1.1 rearnsha SET(t.c_cflag, CLOCAL);
921 1.1 rearnsha if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
922 1.1 rearnsha SET(t.c_cflag, CRTSCTS);
923 1.1 rearnsha if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
924 1.1 rearnsha SET(t.c_cflag, MDMBUF);
925 1.1 rearnsha /* Make sure plcomparam() will do something. */
926 1.1 rearnsha tp->t_ospeed = 0;
927 1.1 rearnsha (void) plcomparam(tp, &t);
928 1.1 rearnsha tp->t_iflag = TTYDEF_IFLAG;
929 1.1 rearnsha tp->t_oflag = TTYDEF_OFLAG;
930 1.1 rearnsha tp->t_lflag = TTYDEF_LFLAG;
931 1.1 rearnsha ttychars(tp);
932 1.1 rearnsha ttsetwater(tp);
933 1.1 rearnsha
934 1.36 skrll mutex_spin_enter(&sc->sc_lock);
935 1.1 rearnsha
936 1.1 rearnsha /*
937 1.1 rearnsha * Turn on DTR. We must always do this, even if carrier is not
938 1.1 rearnsha * present, because otherwise we'd have to use TIOCSDTR
939 1.1 rearnsha * immediately after setting CLOCAL, which applications do not
940 1.1 rearnsha * expect. We always assert DTR while the device is open
941 1.1 rearnsha * unless explicitly requested to deassert it.
942 1.1 rearnsha */
943 1.1 rearnsha plcom_modem(sc, 1);
944 1.1 rearnsha
945 1.1 rearnsha /* Clear the input ring, and unblock. */
946 1.1 rearnsha sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
947 1.1 rearnsha sc->sc_rbavail = plcom_rbuf_size;
948 1.1 rearnsha plcom_iflush(sc);
949 1.1 rearnsha CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
950 1.1 rearnsha plcom_hwiflow(sc);
951 1.1 rearnsha
952 1.1 rearnsha #ifdef PLCOM_DEBUG
953 1.1 rearnsha if (plcom_debug)
954 1.1 rearnsha plcomstatus(sc, "plcomopen ");
955 1.1 rearnsha #endif
956 1.1 rearnsha
957 1.36 skrll mutex_spin_exit(&sc->sc_lock);
958 1.1 rearnsha }
959 1.46 skrll
960 1.1 rearnsha splx(s);
961 1.1 rearnsha
962 1.1 rearnsha error = ttyopen(tp, PLCOMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
963 1.1 rearnsha if (error)
964 1.1 rearnsha goto bad;
965 1.1 rearnsha
966 1.1 rearnsha error = (*tp->t_linesw->l_open)(dev, tp);
967 1.1 rearnsha if (error)
968 1.1 rearnsha goto bad;
969 1.1 rearnsha
970 1.1 rearnsha return 0;
971 1.1 rearnsha
972 1.1 rearnsha bad:
973 1.1 rearnsha if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
974 1.1 rearnsha /*
975 1.1 rearnsha * We failed to open the device, and nobody else had it opened.
976 1.1 rearnsha * Clean up the state as appropriate.
977 1.1 rearnsha */
978 1.1 rearnsha plcom_shutdown(sc);
979 1.1 rearnsha }
980 1.1 rearnsha
981 1.1 rearnsha return error;
982 1.1 rearnsha }
983 1.46 skrll
984 1.1 rearnsha int
985 1.12 christos plcomclose(dev_t dev, int flag, int mode, struct lwp *l)
986 1.1 rearnsha {
987 1.28 cegger struct plcom_softc *sc =
988 1.28 cegger device_lookup_private(&plcom_cd, PLCOMUNIT(dev));
989 1.1 rearnsha struct tty *tp = sc->sc_tty;
990 1.1 rearnsha
991 1.1 rearnsha /* XXX This is for cons.c. */
992 1.1 rearnsha if (!ISSET(tp->t_state, TS_ISOPEN))
993 1.1 rearnsha return 0;
994 1.1 rearnsha
995 1.1 rearnsha (*tp->t_linesw->l_close)(tp, flag);
996 1.1 rearnsha ttyclose(tp);
997 1.1 rearnsha
998 1.1 rearnsha if (PLCOM_ISALIVE(sc) == 0)
999 1.1 rearnsha return 0;
1000 1.1 rearnsha
1001 1.1 rearnsha if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
1002 1.1 rearnsha /*
1003 1.1 rearnsha * Although we got a last close, the device may still be in
1004 1.1 rearnsha * use; e.g. if this was the dialout node, and there are still
1005 1.1 rearnsha * processes waiting for carrier on the non-dialout node.
1006 1.1 rearnsha */
1007 1.1 rearnsha plcom_shutdown(sc);
1008 1.1 rearnsha }
1009 1.1 rearnsha
1010 1.1 rearnsha return 0;
1011 1.1 rearnsha }
1012 1.46 skrll
1013 1.1 rearnsha int
1014 1.1 rearnsha plcomread(dev_t dev, struct uio *uio, int flag)
1015 1.1 rearnsha {
1016 1.28 cegger struct plcom_softc *sc =
1017 1.28 cegger device_lookup_private(&plcom_cd, PLCOMUNIT(dev));
1018 1.1 rearnsha struct tty *tp = sc->sc_tty;
1019 1.1 rearnsha
1020 1.1 rearnsha if (PLCOM_ISALIVE(sc) == 0)
1021 1.1 rearnsha return EIO;
1022 1.46 skrll
1023 1.1 rearnsha return (*tp->t_linesw->l_read)(tp, uio, flag);
1024 1.1 rearnsha }
1025 1.46 skrll
1026 1.1 rearnsha int
1027 1.1 rearnsha plcomwrite(dev_t dev, struct uio *uio, int flag)
1028 1.1 rearnsha {
1029 1.28 cegger struct plcom_softc *sc =
1030 1.28 cegger device_lookup_private(&plcom_cd, PLCOMUNIT(dev));
1031 1.1 rearnsha struct tty *tp = sc->sc_tty;
1032 1.1 rearnsha
1033 1.1 rearnsha if (PLCOM_ISALIVE(sc) == 0)
1034 1.1 rearnsha return EIO;
1035 1.46 skrll
1036 1.1 rearnsha return (*tp->t_linesw->l_write)(tp, uio, flag);
1037 1.1 rearnsha }
1038 1.1 rearnsha
1039 1.1 rearnsha int
1040 1.12 christos plcompoll(dev_t dev, int events, struct lwp *l)
1041 1.1 rearnsha {
1042 1.28 cegger struct plcom_softc *sc =
1043 1.28 cegger device_lookup_private(&plcom_cd, PLCOMUNIT(dev));
1044 1.1 rearnsha struct tty *tp = sc->sc_tty;
1045 1.1 rearnsha
1046 1.1 rearnsha if (PLCOM_ISALIVE(sc) == 0)
1047 1.1 rearnsha return EIO;
1048 1.46 skrll
1049 1.12 christos return (*tp->t_linesw->l_poll)(tp, events, l);
1050 1.1 rearnsha }
1051 1.1 rearnsha
1052 1.1 rearnsha struct tty *
1053 1.1 rearnsha plcomtty(dev_t dev)
1054 1.1 rearnsha {
1055 1.28 cegger struct plcom_softc *sc =
1056 1.28 cegger device_lookup_private(&plcom_cd, PLCOMUNIT(dev));
1057 1.1 rearnsha struct tty *tp = sc->sc_tty;
1058 1.1 rearnsha
1059 1.1 rearnsha return tp;
1060 1.1 rearnsha }
1061 1.1 rearnsha
1062 1.1 rearnsha int
1063 1.20 christos plcomioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
1064 1.1 rearnsha {
1065 1.28 cegger struct plcom_softc *sc =
1066 1.28 cegger device_lookup_private(&plcom_cd, PLCOMUNIT(dev));
1067 1.40 skrll struct tty *tp;
1068 1.1 rearnsha int error;
1069 1.1 rearnsha
1070 1.40 skrll if (sc == NULL)
1071 1.40 skrll return ENXIO;
1072 1.1 rearnsha if (PLCOM_ISALIVE(sc) == 0)
1073 1.1 rearnsha return EIO;
1074 1.1 rearnsha
1075 1.40 skrll tp = sc->sc_tty;
1076 1.40 skrll
1077 1.12 christos error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
1078 1.3 atatat if (error != EPASSTHROUGH)
1079 1.1 rearnsha return error;
1080 1.1 rearnsha
1081 1.12 christos error = ttioctl(tp, cmd, data, flag, l);
1082 1.3 atatat if (error != EPASSTHROUGH)
1083 1.1 rearnsha return error;
1084 1.1 rearnsha
1085 1.1 rearnsha error = 0;
1086 1.40 skrll switch (cmd) {
1087 1.40 skrll case TIOCSFLAGS:
1088 1.40 skrll error = kauth_authorize_device_tty(l->l_cred,
1089 1.40 skrll KAUTH_DEVICE_TTY_PRIVSET, tp);
1090 1.40 skrll break;
1091 1.40 skrll default:
1092 1.40 skrll /* nothing */
1093 1.40 skrll break;
1094 1.40 skrll }
1095 1.40 skrll if (error) {
1096 1.40 skrll return error;
1097 1.40 skrll }
1098 1.1 rearnsha
1099 1.36 skrll mutex_spin_enter(&sc->sc_lock);
1100 1.1 rearnsha switch (cmd) {
1101 1.1 rearnsha case TIOCSBRK:
1102 1.1 rearnsha plcom_break(sc, 1);
1103 1.1 rearnsha break;
1104 1.1 rearnsha
1105 1.1 rearnsha case TIOCCBRK:
1106 1.1 rearnsha plcom_break(sc, 0);
1107 1.1 rearnsha break;
1108 1.1 rearnsha
1109 1.1 rearnsha case TIOCSDTR:
1110 1.1 rearnsha plcom_modem(sc, 1);
1111 1.1 rearnsha break;
1112 1.1 rearnsha
1113 1.1 rearnsha case TIOCCDTR:
1114 1.1 rearnsha plcom_modem(sc, 0);
1115 1.1 rearnsha break;
1116 1.1 rearnsha
1117 1.1 rearnsha case TIOCGFLAGS:
1118 1.1 rearnsha *(int *)data = sc->sc_swflags;
1119 1.1 rearnsha break;
1120 1.1 rearnsha
1121 1.1 rearnsha case TIOCSFLAGS:
1122 1.1 rearnsha sc->sc_swflags = *(int *)data;
1123 1.1 rearnsha break;
1124 1.1 rearnsha
1125 1.1 rearnsha case TIOCMSET:
1126 1.1 rearnsha case TIOCMBIS:
1127 1.1 rearnsha case TIOCMBIC:
1128 1.1 rearnsha tiocm_to_plcom(sc, cmd, *(int *)data);
1129 1.1 rearnsha break;
1130 1.1 rearnsha
1131 1.1 rearnsha case TIOCMGET:
1132 1.1 rearnsha *(int *)data = plcom_to_tiocm(sc);
1133 1.1 rearnsha break;
1134 1.1 rearnsha
1135 1.1 rearnsha case PPS_IOC_CREATE:
1136 1.1 rearnsha break;
1137 1.1 rearnsha
1138 1.1 rearnsha case PPS_IOC_DESTROY:
1139 1.1 rearnsha break;
1140 1.1 rearnsha
1141 1.1 rearnsha case PPS_IOC_GETPARAMS: {
1142 1.1 rearnsha pps_params_t *pp;
1143 1.1 rearnsha pp = (pps_params_t *)data;
1144 1.26 ad mutex_spin_enter(&timecounter_lock);
1145 1.1 rearnsha *pp = sc->ppsparam;
1146 1.26 ad mutex_spin_exit(&timecounter_lock);
1147 1.1 rearnsha break;
1148 1.1 rearnsha }
1149 1.1 rearnsha
1150 1.1 rearnsha case PPS_IOC_SETPARAMS: {
1151 1.1 rearnsha pps_params_t *pp;
1152 1.1 rearnsha int mode;
1153 1.1 rearnsha pp = (pps_params_t *)data;
1154 1.26 ad mutex_spin_enter(&timecounter_lock);
1155 1.1 rearnsha if (pp->mode & ~ppscap) {
1156 1.1 rearnsha error = EINVAL;
1157 1.26 ad mutex_spin_exit(&timecounter_lock);
1158 1.1 rearnsha break;
1159 1.1 rearnsha }
1160 1.1 rearnsha sc->ppsparam = *pp;
1161 1.46 skrll /*
1162 1.1 rearnsha * Compute msr masks from user-specified timestamp state.
1163 1.1 rearnsha */
1164 1.1 rearnsha mode = sc->ppsparam.mode;
1165 1.1 rearnsha #ifdef PPS_SYNC
1166 1.1 rearnsha if (mode & PPS_HARDPPSONASSERT) {
1167 1.1 rearnsha mode |= PPS_CAPTUREASSERT;
1168 1.1 rearnsha /* XXX revoke any previous HARDPPS source */
1169 1.1 rearnsha }
1170 1.1 rearnsha if (mode & PPS_HARDPPSONCLEAR) {
1171 1.1 rearnsha mode |= PPS_CAPTURECLEAR;
1172 1.1 rearnsha /* XXX revoke any previous HARDPPS source */
1173 1.1 rearnsha }
1174 1.1 rearnsha #endif /* PPS_SYNC */
1175 1.1 rearnsha switch (mode & PPS_CAPTUREBOTH) {
1176 1.1 rearnsha case 0:
1177 1.1 rearnsha sc->sc_ppsmask = 0;
1178 1.1 rearnsha break;
1179 1.46 skrll
1180 1.1 rearnsha case PPS_CAPTUREASSERT:
1181 1.35 skrll sc->sc_ppsmask = PL01X_MSR_DCD;
1182 1.35 skrll sc->sc_ppsassert = PL01X_MSR_DCD;
1183 1.1 rearnsha sc->sc_ppsclear = -1;
1184 1.1 rearnsha break;
1185 1.46 skrll
1186 1.1 rearnsha case PPS_CAPTURECLEAR:
1187 1.35 skrll sc->sc_ppsmask = PL01X_MSR_DCD;
1188 1.1 rearnsha sc->sc_ppsassert = -1;
1189 1.1 rearnsha sc->sc_ppsclear = 0;
1190 1.1 rearnsha break;
1191 1.1 rearnsha
1192 1.1 rearnsha case PPS_CAPTUREBOTH:
1193 1.35 skrll sc->sc_ppsmask = PL01X_MSR_DCD;
1194 1.35 skrll sc->sc_ppsassert = PL01X_MSR_DCD;
1195 1.1 rearnsha sc->sc_ppsclear = 0;
1196 1.1 rearnsha break;
1197 1.1 rearnsha
1198 1.1 rearnsha default:
1199 1.1 rearnsha error = EINVAL;
1200 1.1 rearnsha break;
1201 1.1 rearnsha }
1202 1.26 ad mutex_spin_exit(&timecounter_lock);
1203 1.1 rearnsha break;
1204 1.1 rearnsha }
1205 1.1 rearnsha
1206 1.1 rearnsha case PPS_IOC_GETCAP:
1207 1.1 rearnsha *(int*)data = ppscap;
1208 1.1 rearnsha break;
1209 1.1 rearnsha
1210 1.1 rearnsha case PPS_IOC_FETCH: {
1211 1.1 rearnsha pps_info_t *pi;
1212 1.1 rearnsha pi = (pps_info_t *)data;
1213 1.26 ad mutex_spin_enter(&timecounter_lock);
1214 1.1 rearnsha *pi = sc->ppsinfo;
1215 1.26 ad mutex_spin_exit(&timecounter_lock);
1216 1.1 rearnsha break;
1217 1.1 rearnsha }
1218 1.1 rearnsha
1219 1.1 rearnsha case TIOCDCDTIMESTAMP: /* XXX old, overloaded API used by xntpd v3 */
1220 1.1 rearnsha /*
1221 1.1 rearnsha * Some GPS clocks models use the falling rather than
1222 1.46 skrll * rising edge as the on-the-second signal.
1223 1.1 rearnsha * The old API has no way to specify PPS polarity.
1224 1.1 rearnsha */
1225 1.26 ad mutex_spin_enter(&timecounter_lock);
1226 1.35 skrll sc->sc_ppsmask = PL01X_MSR_DCD;
1227 1.1 rearnsha #ifndef PPS_TRAILING_EDGE
1228 1.35 skrll sc->sc_ppsassert = PL01X_MSR_DCD;
1229 1.1 rearnsha sc->sc_ppsclear = -1;
1230 1.46 skrll TIMESPEC_TO_TIMEVAL((struct timeval *)data,
1231 1.1 rearnsha &sc->ppsinfo.assert_timestamp);
1232 1.1 rearnsha #else
1233 1.1 rearnsha sc->sc_ppsassert = -1
1234 1.1 rearnsha sc->sc_ppsclear = 0;
1235 1.46 skrll TIMESPEC_TO_TIMEVAL((struct timeval *)data,
1236 1.1 rearnsha &sc->ppsinfo.clear_timestamp);
1237 1.1 rearnsha #endif
1238 1.26 ad mutex_spin_exit(&timecounter_lock);
1239 1.1 rearnsha break;
1240 1.1 rearnsha
1241 1.1 rearnsha default:
1242 1.3 atatat error = EPASSTHROUGH;
1243 1.1 rearnsha break;
1244 1.1 rearnsha }
1245 1.1 rearnsha
1246 1.36 skrll mutex_spin_exit(&sc->sc_lock);
1247 1.1 rearnsha
1248 1.1 rearnsha #ifdef PLCOM_DEBUG
1249 1.1 rearnsha if (plcom_debug)
1250 1.1 rearnsha plcomstatus(sc, "plcomioctl ");
1251 1.1 rearnsha #endif
1252 1.1 rearnsha
1253 1.1 rearnsha return error;
1254 1.1 rearnsha }
1255 1.1 rearnsha
1256 1.68 riastrad static inline void
1257 1.1 rearnsha plcom_schedrx(struct plcom_softc *sc)
1258 1.1 rearnsha {
1259 1.1 rearnsha
1260 1.1 rearnsha sc->sc_rx_ready = 1;
1261 1.1 rearnsha
1262 1.1 rearnsha /* Wake up the poller. */
1263 1.25 ad softint_schedule(sc->sc_si);
1264 1.1 rearnsha }
1265 1.1 rearnsha
1266 1.1 rearnsha void
1267 1.1 rearnsha plcom_break(struct plcom_softc *sc, int onoff)
1268 1.1 rearnsha {
1269 1.1 rearnsha
1270 1.1 rearnsha if (onoff)
1271 1.35 skrll SET(sc->sc_lcr, PL01X_LCR_BRK);
1272 1.1 rearnsha else
1273 1.35 skrll CLR(sc->sc_lcr, PL01X_LCR_BRK);
1274 1.1 rearnsha
1275 1.1 rearnsha if (!sc->sc_heldchange) {
1276 1.1 rearnsha if (sc->sc_tx_busy) {
1277 1.1 rearnsha sc->sc_heldtbc = sc->sc_tbc;
1278 1.1 rearnsha sc->sc_tbc = 0;
1279 1.1 rearnsha sc->sc_heldchange = 1;
1280 1.1 rearnsha } else
1281 1.1 rearnsha plcom_loadchannelregs(sc);
1282 1.1 rearnsha }
1283 1.1 rearnsha }
1284 1.1 rearnsha
1285 1.1 rearnsha void
1286 1.1 rearnsha plcom_modem(struct plcom_softc *sc, int onoff)
1287 1.1 rearnsha {
1288 1.1 rearnsha
1289 1.1 rearnsha if (sc->sc_mcr_dtr == 0)
1290 1.1 rearnsha return;
1291 1.1 rearnsha
1292 1.1 rearnsha if (onoff)
1293 1.1 rearnsha SET(sc->sc_mcr, sc->sc_mcr_dtr);
1294 1.1 rearnsha else
1295 1.1 rearnsha CLR(sc->sc_mcr, sc->sc_mcr_dtr);
1296 1.1 rearnsha
1297 1.1 rearnsha if (!sc->sc_heldchange) {
1298 1.1 rearnsha if (sc->sc_tx_busy) {
1299 1.1 rearnsha sc->sc_heldtbc = sc->sc_tbc;
1300 1.1 rearnsha sc->sc_tbc = 0;
1301 1.1 rearnsha sc->sc_heldchange = 1;
1302 1.1 rearnsha } else
1303 1.1 rearnsha plcom_loadchannelregs(sc);
1304 1.1 rearnsha }
1305 1.1 rearnsha }
1306 1.1 rearnsha
1307 1.1 rearnsha void
1308 1.1 rearnsha tiocm_to_plcom(struct plcom_softc *sc, u_long how, int ttybits)
1309 1.1 rearnsha {
1310 1.1 rearnsha u_char plcombits;
1311 1.1 rearnsha
1312 1.1 rearnsha plcombits = 0;
1313 1.1 rearnsha if (ISSET(ttybits, TIOCM_DTR))
1314 1.35 skrll SET(plcombits, PL01X_MCR_DTR);
1315 1.1 rearnsha if (ISSET(ttybits, TIOCM_RTS))
1316 1.35 skrll SET(plcombits, PL01X_MCR_RTS);
1317 1.46 skrll
1318 1.1 rearnsha switch (how) {
1319 1.1 rearnsha case TIOCMBIC:
1320 1.1 rearnsha CLR(sc->sc_mcr, plcombits);
1321 1.1 rearnsha break;
1322 1.1 rearnsha
1323 1.1 rearnsha case TIOCMBIS:
1324 1.1 rearnsha SET(sc->sc_mcr, plcombits);
1325 1.1 rearnsha break;
1326 1.1 rearnsha
1327 1.1 rearnsha case TIOCMSET:
1328 1.35 skrll CLR(sc->sc_mcr, PL01X_MCR_DTR | PL01X_MCR_RTS);
1329 1.1 rearnsha SET(sc->sc_mcr, plcombits);
1330 1.1 rearnsha break;
1331 1.1 rearnsha }
1332 1.1 rearnsha
1333 1.1 rearnsha if (!sc->sc_heldchange) {
1334 1.1 rearnsha if (sc->sc_tx_busy) {
1335 1.1 rearnsha sc->sc_heldtbc = sc->sc_tbc;
1336 1.1 rearnsha sc->sc_tbc = 0;
1337 1.1 rearnsha sc->sc_heldchange = 1;
1338 1.1 rearnsha } else
1339 1.1 rearnsha plcom_loadchannelregs(sc);
1340 1.1 rearnsha }
1341 1.1 rearnsha }
1342 1.1 rearnsha
1343 1.1 rearnsha int
1344 1.1 rearnsha plcom_to_tiocm(struct plcom_softc *sc)
1345 1.1 rearnsha {
1346 1.1 rearnsha u_char plcombits;
1347 1.1 rearnsha int ttybits = 0;
1348 1.1 rearnsha
1349 1.1 rearnsha plcombits = sc->sc_mcr;
1350 1.35 skrll if (ISSET(plcombits, PL01X_MCR_DTR))
1351 1.1 rearnsha SET(ttybits, TIOCM_DTR);
1352 1.35 skrll if (ISSET(plcombits, PL01X_MCR_RTS))
1353 1.1 rearnsha SET(ttybits, TIOCM_RTS);
1354 1.1 rearnsha
1355 1.1 rearnsha plcombits = sc->sc_msr;
1356 1.35 skrll if (ISSET(plcombits, PL01X_MSR_DCD))
1357 1.1 rearnsha SET(ttybits, TIOCM_CD);
1358 1.35 skrll if (ISSET(plcombits, PL01X_MSR_CTS))
1359 1.1 rearnsha SET(ttybits, TIOCM_CTS);
1360 1.35 skrll if (ISSET(plcombits, PL01X_MSR_DSR))
1361 1.1 rearnsha SET(ttybits, TIOCM_DSR);
1362 1.40 skrll if (ISSET(plcombits, PL011_MSR_RI))
1363 1.40 skrll SET(ttybits, TIOCM_RI);
1364 1.1 rearnsha
1365 1.1 rearnsha if (sc->sc_cr != 0)
1366 1.1 rearnsha SET(ttybits, TIOCM_LE);
1367 1.1 rearnsha
1368 1.1 rearnsha return ttybits;
1369 1.1 rearnsha }
1370 1.1 rearnsha
1371 1.67 mlelstv static uint32_t
1372 1.1 rearnsha cflag2lcr(tcflag_t cflag)
1373 1.1 rearnsha {
1374 1.67 mlelstv uint32_t lcr = 0;
1375 1.1 rearnsha
1376 1.1 rearnsha switch (ISSET(cflag, CSIZE)) {
1377 1.1 rearnsha case CS5:
1378 1.35 skrll SET(lcr, PL01X_LCR_5BITS);
1379 1.1 rearnsha break;
1380 1.1 rearnsha case CS6:
1381 1.35 skrll SET(lcr, PL01X_LCR_6BITS);
1382 1.1 rearnsha break;
1383 1.1 rearnsha case CS7:
1384 1.35 skrll SET(lcr, PL01X_LCR_7BITS);
1385 1.1 rearnsha break;
1386 1.1 rearnsha case CS8:
1387 1.35 skrll SET(lcr, PL01X_LCR_8BITS);
1388 1.1 rearnsha break;
1389 1.1 rearnsha }
1390 1.1 rearnsha if (ISSET(cflag, PARENB)) {
1391 1.35 skrll SET(lcr, PL01X_LCR_PEN);
1392 1.1 rearnsha if (!ISSET(cflag, PARODD))
1393 1.35 skrll SET(lcr, PL01X_LCR_EPS);
1394 1.1 rearnsha }
1395 1.1 rearnsha if (ISSET(cflag, CSTOPB))
1396 1.35 skrll SET(lcr, PL01X_LCR_STP2);
1397 1.1 rearnsha
1398 1.1 rearnsha return lcr;
1399 1.1 rearnsha }
1400 1.1 rearnsha
1401 1.1 rearnsha int
1402 1.1 rearnsha plcomparam(struct tty *tp, struct termios *t)
1403 1.1 rearnsha {
1404 1.28 cegger struct plcom_softc *sc =
1405 1.28 cegger device_lookup_private(&plcom_cd, PLCOMUNIT(tp->t_dev));
1406 1.40 skrll struct plcom_instance *pi = &sc->sc_pi;
1407 1.67 mlelstv int ospeed = -1, lvl;
1408 1.67 mlelstv uint32_t lcr;
1409 1.1 rearnsha
1410 1.1 rearnsha if (PLCOM_ISALIVE(sc) == 0)
1411 1.1 rearnsha return EIO;
1412 1.1 rearnsha
1413 1.40 skrll switch (pi->pi_type) {
1414 1.40 skrll case PLCOM_TYPE_PL010:
1415 1.40 skrll ospeed = pl010comspeed(t->c_ospeed, sc->sc_frequency);
1416 1.40 skrll break;
1417 1.40 skrll case PLCOM_TYPE_PL011:
1418 1.67 mlelstv case PLCOM_TYPE_GENERIC_UART:
1419 1.40 skrll ospeed = pl011comspeed(t->c_ospeed, sc->sc_frequency);
1420 1.40 skrll break;
1421 1.40 skrll }
1422 1.1 rearnsha
1423 1.1 rearnsha /* Check requested parameters. */
1424 1.1 rearnsha if (ospeed < 0)
1425 1.1 rearnsha return EINVAL;
1426 1.1 rearnsha if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
1427 1.1 rearnsha return EINVAL;
1428 1.1 rearnsha
1429 1.1 rearnsha /*
1430 1.1 rearnsha * For the console, always force CLOCAL and !HUPCL, so that the port
1431 1.1 rearnsha * is always active.
1432 1.1 rearnsha */
1433 1.1 rearnsha if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
1434 1.1 rearnsha ISSET(sc->sc_hwflags, PLCOM_HW_CONSOLE)) {
1435 1.1 rearnsha SET(t->c_cflag, CLOCAL);
1436 1.1 rearnsha CLR(t->c_cflag, HUPCL);
1437 1.1 rearnsha }
1438 1.1 rearnsha
1439 1.1 rearnsha /*
1440 1.1 rearnsha * If there were no changes, don't do anything. This avoids dropping
1441 1.1 rearnsha * input and improves performance when all we did was frob things like
1442 1.1 rearnsha * VMIN and VTIME.
1443 1.1 rearnsha */
1444 1.1 rearnsha if (tp->t_ospeed == t->c_ospeed &&
1445 1.1 rearnsha tp->t_cflag == t->c_cflag)
1446 1.1 rearnsha return 0;
1447 1.1 rearnsha
1448 1.35 skrll lcr = ISSET(sc->sc_lcr, PL01X_LCR_BRK) | cflag2lcr(t->c_cflag);
1449 1.1 rearnsha
1450 1.36 skrll mutex_spin_enter(&sc->sc_lock);
1451 1.1 rearnsha
1452 1.1 rearnsha sc->sc_lcr = lcr;
1453 1.1 rearnsha
1454 1.1 rearnsha /*
1455 1.1 rearnsha * PL010 has a fixed-length FIFO trigger point.
1456 1.1 rearnsha */
1457 1.1 rearnsha if (ISSET(sc->sc_hwflags, PLCOM_HW_FIFO))
1458 1.1 rearnsha sc->sc_fifo = 1;
1459 1.1 rearnsha else
1460 1.1 rearnsha sc->sc_fifo = 0;
1461 1.1 rearnsha
1462 1.67 mlelstv if (sc->sc_fifo) {
1463 1.35 skrll SET(sc->sc_lcr, PL01X_LCR_FEN);
1464 1.1 rearnsha
1465 1.67 mlelstv switch (pi->pi_type) {
1466 1.67 mlelstv case PLCOM_TYPE_PL010:
1467 1.67 mlelstv sc->sc_ifls = 0;
1468 1.67 mlelstv break;
1469 1.67 mlelstv case PLCOM_TYPE_PL011:
1470 1.67 mlelstv case PLCOM_TYPE_GENERIC_UART:
1471 1.67 mlelstv lvl = PL011_IFLS_3QUARTERS;
1472 1.67 mlelstv sc->sc_ifls = PL011_IFLS_RXIFLS(lvl);
1473 1.67 mlelstv
1474 1.67 mlelstv lvl = PL011_IFLS_1QUARTER;
1475 1.67 mlelstv sc->sc_ifls |= PL011_IFLS_TXIFLS(lvl);
1476 1.67 mlelstv sc->sc_burstlen = uimin(sc->sc_fifolen * 3 / 4, 1);
1477 1.67 mlelstv break;
1478 1.67 mlelstv }
1479 1.67 mlelstv } else
1480 1.67 mlelstv sc->sc_ifls = 0;
1481 1.67 mlelstv
1482 1.1 rearnsha /*
1483 1.1 rearnsha * If we're not in a mode that assumes a connection is present, then
1484 1.1 rearnsha * ignore carrier changes.
1485 1.1 rearnsha */
1486 1.1 rearnsha if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
1487 1.1 rearnsha sc->sc_msr_dcd = 0;
1488 1.1 rearnsha else
1489 1.35 skrll sc->sc_msr_dcd = PL01X_MSR_DCD;
1490 1.1 rearnsha /*
1491 1.1 rearnsha * Set the flow control pins depending on the current flow control
1492 1.1 rearnsha * mode.
1493 1.1 rearnsha */
1494 1.1 rearnsha if (ISSET(t->c_cflag, CRTSCTS)) {
1495 1.35 skrll sc->sc_mcr_dtr = PL01X_MCR_DTR;
1496 1.67 mlelstv
1497 1.67 mlelstv switch (pi->pi_type) {
1498 1.67 mlelstv case PLCOM_TYPE_PL010:
1499 1.67 mlelstv sc->sc_mcr_rts = PL01X_MCR_RTS;
1500 1.67 mlelstv sc->sc_msr_cts = PL01X_MSR_CTS;
1501 1.67 mlelstv break;
1502 1.67 mlelstv case PLCOM_TYPE_PL011:
1503 1.67 mlelstv case PLCOM_TYPE_GENERIC_UART:
1504 1.67 mlelstv sc->sc_mcr_rts = 0;
1505 1.67 mlelstv sc->sc_msr_cts = 0;
1506 1.67 mlelstv SET(sc->sc_cr, PL011_CR_CTSEN | PL011_CR_RTSEN);
1507 1.67 mlelstv break;
1508 1.67 mlelstv }
1509 1.1 rearnsha } else if (ISSET(t->c_cflag, MDMBUF)) {
1510 1.1 rearnsha /*
1511 1.1 rearnsha * For DTR/DCD flow control, make sure we don't toggle DTR for
1512 1.1 rearnsha * carrier detection.
1513 1.1 rearnsha */
1514 1.1 rearnsha sc->sc_mcr_dtr = 0;
1515 1.35 skrll sc->sc_mcr_rts = PL01X_MCR_DTR;
1516 1.35 skrll sc->sc_msr_cts = PL01X_MSR_DCD;
1517 1.67 mlelstv
1518 1.67 mlelstv switch (pi->pi_type) {
1519 1.67 mlelstv case PLCOM_TYPE_PL011:
1520 1.67 mlelstv CLR(sc->sc_cr, PL011_CR_CTSEN | PL011_CR_RTSEN);
1521 1.67 mlelstv break;
1522 1.67 mlelstv }
1523 1.1 rearnsha } else {
1524 1.1 rearnsha /*
1525 1.1 rearnsha * If no flow control, then always set RTS. This will make
1526 1.1 rearnsha * the other side happy if it mistakenly thinks we're doing
1527 1.1 rearnsha * RTS/CTS flow control.
1528 1.1 rearnsha */
1529 1.35 skrll sc->sc_mcr_dtr = PL01X_MCR_DTR | PL01X_MCR_RTS;
1530 1.1 rearnsha sc->sc_mcr_rts = 0;
1531 1.1 rearnsha sc->sc_msr_cts = 0;
1532 1.35 skrll if (ISSET(sc->sc_mcr, PL01X_MCR_DTR))
1533 1.35 skrll SET(sc->sc_mcr, PL01X_MCR_RTS);
1534 1.1 rearnsha else
1535 1.35 skrll CLR(sc->sc_mcr, PL01X_MCR_RTS);
1536 1.67 mlelstv switch (pi->pi_type) {
1537 1.67 mlelstv case PLCOM_TYPE_PL011:
1538 1.67 mlelstv case PLCOM_TYPE_GENERIC_UART:
1539 1.67 mlelstv CLR(sc->sc_cr, PL011_CR_CTSEN | PL011_CR_RTSEN);
1540 1.67 mlelstv break;
1541 1.67 mlelstv }
1542 1.1 rearnsha }
1543 1.1 rearnsha sc->sc_msr_mask = sc->sc_msr_cts | sc->sc_msr_dcd;
1544 1.1 rearnsha
1545 1.1 rearnsha #if 0
1546 1.1 rearnsha if (ospeed == 0)
1547 1.1 rearnsha CLR(sc->sc_mcr, sc->sc_mcr_dtr);
1548 1.1 rearnsha else
1549 1.1 rearnsha SET(sc->sc_mcr, sc->sc_mcr_dtr);
1550 1.1 rearnsha #endif
1551 1.1 rearnsha
1552 1.40 skrll switch (pi->pi_type) {
1553 1.40 skrll case PLCOM_TYPE_PL010:
1554 1.40 skrll sc->sc_ratel = ospeed & 0xff;
1555 1.40 skrll sc->sc_rateh = (ospeed >> 8) & 0xff;
1556 1.40 skrll break;
1557 1.40 skrll case PLCOM_TYPE_PL011:
1558 1.67 mlelstv case PLCOM_TYPE_GENERIC_UART:
1559 1.40 skrll sc->sc_ratel = ospeed & ((1 << 6) - 1);
1560 1.40 skrll sc->sc_rateh = ospeed >> 6;
1561 1.40 skrll break;
1562 1.40 skrll }
1563 1.1 rearnsha
1564 1.1 rearnsha /* And copy to tty. */
1565 1.40 skrll tp->t_ispeed = t->c_ospeed;
1566 1.1 rearnsha tp->t_ospeed = t->c_ospeed;
1567 1.1 rearnsha tp->t_cflag = t->c_cflag;
1568 1.1 rearnsha
1569 1.1 rearnsha if (!sc->sc_heldchange) {
1570 1.1 rearnsha if (sc->sc_tx_busy) {
1571 1.1 rearnsha sc->sc_heldtbc = sc->sc_tbc;
1572 1.1 rearnsha sc->sc_tbc = 0;
1573 1.1 rearnsha sc->sc_heldchange = 1;
1574 1.1 rearnsha } else
1575 1.1 rearnsha plcom_loadchannelregs(sc);
1576 1.1 rearnsha }
1577 1.1 rearnsha
1578 1.1 rearnsha if (!ISSET(t->c_cflag, CHWFLOW)) {
1579 1.1 rearnsha /* Disable the high water mark. */
1580 1.1 rearnsha sc->sc_r_hiwat = 0;
1581 1.1 rearnsha sc->sc_r_lowat = 0;
1582 1.1 rearnsha if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
1583 1.1 rearnsha CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1584 1.1 rearnsha plcom_schedrx(sc);
1585 1.1 rearnsha }
1586 1.1 rearnsha if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
1587 1.1 rearnsha CLR(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
1588 1.1 rearnsha plcom_hwiflow(sc);
1589 1.1 rearnsha }
1590 1.1 rearnsha } else {
1591 1.1 rearnsha sc->sc_r_hiwat = plcom_rbuf_hiwat;
1592 1.1 rearnsha sc->sc_r_lowat = plcom_rbuf_lowat;
1593 1.1 rearnsha }
1594 1.1 rearnsha
1595 1.36 skrll mutex_spin_exit(&sc->sc_lock);
1596 1.1 rearnsha
1597 1.1 rearnsha /*
1598 1.1 rearnsha * Update the tty layer's idea of the carrier bit, in case we changed
1599 1.1 rearnsha * CLOCAL or MDMBUF. We don't hang up here; we only do that by
1600 1.1 rearnsha * explicit request.
1601 1.1 rearnsha */
1602 1.35 skrll (void) (*tp->t_linesw->l_modem)(tp, ISSET(sc->sc_msr, PL01X_MSR_DCD));
1603 1.1 rearnsha
1604 1.1 rearnsha #ifdef PLCOM_DEBUG
1605 1.1 rearnsha if (plcom_debug)
1606 1.1 rearnsha plcomstatus(sc, "plcomparam ");
1607 1.1 rearnsha #endif
1608 1.1 rearnsha
1609 1.1 rearnsha if (!ISSET(t->c_cflag, CHWFLOW)) {
1610 1.1 rearnsha if (sc->sc_tx_stopped) {
1611 1.1 rearnsha sc->sc_tx_stopped = 0;
1612 1.1 rearnsha plcomstart(tp);
1613 1.1 rearnsha }
1614 1.1 rearnsha }
1615 1.1 rearnsha
1616 1.1 rearnsha return 0;
1617 1.1 rearnsha }
1618 1.1 rearnsha
1619 1.1 rearnsha void
1620 1.1 rearnsha plcom_iflush(struct plcom_softc *sc)
1621 1.1 rearnsha {
1622 1.40 skrll struct plcom_instance *pi = &sc->sc_pi;
1623 1.1 rearnsha #ifdef DIAGNOSTIC
1624 1.1 rearnsha int reg;
1625 1.1 rearnsha #endif
1626 1.1 rearnsha int timo;
1627 1.1 rearnsha
1628 1.1 rearnsha #ifdef DIAGNOSTIC
1629 1.1 rearnsha reg = 0xffff;
1630 1.1 rearnsha #endif
1631 1.1 rearnsha timo = 50000;
1632 1.1 rearnsha /* flush any pending I/O */
1633 1.40 skrll while (! ISSET(PREAD1(pi, PL01XCOM_FR), PL01X_FR_RXFE)
1634 1.1 rearnsha && --timo)
1635 1.1 rearnsha #ifdef DIAGNOSTIC
1636 1.1 rearnsha reg =
1637 1.1 rearnsha #else
1638 1.1 rearnsha (void)
1639 1.1 rearnsha #endif
1640 1.40 skrll PREAD1(pi, PL01XCOM_DR);
1641 1.1 rearnsha #ifdef DIAGNOSTIC
1642 1.1 rearnsha if (!timo)
1643 1.40 skrll aprint_error_dev(sc->sc_dev, ": %s timeout %02x\n", __func__,
1644 1.40 skrll reg);
1645 1.1 rearnsha #endif
1646 1.1 rearnsha }
1647 1.1 rearnsha
1648 1.1 rearnsha void
1649 1.1 rearnsha plcom_loadchannelregs(struct plcom_softc *sc)
1650 1.1 rearnsha {
1651 1.40 skrll struct plcom_instance *pi = &sc->sc_pi;
1652 1.67 mlelstv uint16_t ifls;
1653 1.1 rearnsha
1654 1.1 rearnsha /* XXXXX necessary? */
1655 1.1 rearnsha plcom_iflush(sc);
1656 1.1 rearnsha
1657 1.40 skrll switch (pi->pi_type) {
1658 1.40 skrll case PLCOM_TYPE_PL010:
1659 1.40 skrll PWRITE1(pi, PL010COM_CR, 0);
1660 1.55 jmcneill if (sc->sc_frequency != 0) {
1661 1.55 jmcneill PWRITE1(pi, PL010COM_DLBL, sc->sc_ratel);
1662 1.55 jmcneill PWRITE1(pi, PL010COM_DLBH, sc->sc_rateh);
1663 1.55 jmcneill }
1664 1.40 skrll PWRITE1(pi, PL010COM_LCR, sc->sc_lcr);
1665 1.40 skrll
1666 1.40 skrll /* XXX device_unit() abuse */
1667 1.40 skrll if (sc->sc_set_mcr)
1668 1.40 skrll sc->sc_set_mcr(sc->sc_set_mcr_arg,
1669 1.40 skrll device_unit(sc->sc_dev),
1670 1.40 skrll sc->sc_mcr_active = sc->sc_mcr);
1671 1.40 skrll
1672 1.40 skrll PWRITE1(pi, PL010COM_CR, sc->sc_cr);
1673 1.40 skrll break;
1674 1.40 skrll
1675 1.40 skrll case PLCOM_TYPE_PL011:
1676 1.67 mlelstv case PLCOM_TYPE_GENERIC_UART:
1677 1.67 mlelstv PWRITE2(pi, PL011COM_CR, 0);
1678 1.55 jmcneill if (sc->sc_frequency != 0) {
1679 1.55 jmcneill PWRITE1(pi, PL011COM_FBRD, sc->sc_ratel);
1680 1.55 jmcneill PWRITE4(pi, PL011COM_IBRD, sc->sc_rateh);
1681 1.55 jmcneill }
1682 1.67 mlelstv
1683 1.67 mlelstv /* Bits 6..15 are reserved, don't modify, read as zero */
1684 1.67 mlelstv ifls = PREAD2(pi, PL011COM_IFLS) & ~PL011_IFLS_MASK;
1685 1.67 mlelstv ifls |= sc->sc_ifls & PL011_IFLS_MASK;
1686 1.67 mlelstv PWRITE2(pi, PL011COM_IFLS, ifls);
1687 1.67 mlelstv
1688 1.40 skrll PWRITE1(pi, PL011COM_LCRH, sc->sc_lcr);
1689 1.40 skrll sc->sc_mcr_active = sc->sc_mcr;
1690 1.40 skrll CLR(sc->sc_cr, PL011_MCR(PL01X_MCR_RTS | PL01X_MCR_DTR));
1691 1.40 skrll SET(sc->sc_cr, PL011_MCR(sc->sc_mcr_active));
1692 1.67 mlelstv PWRITE2(pi, PL011COM_CR, sc->sc_cr);
1693 1.40 skrll break;
1694 1.40 skrll }
1695 1.1 rearnsha }
1696 1.1 rearnsha
1697 1.1 rearnsha int
1698 1.1 rearnsha plcomhwiflow(struct tty *tp, int block)
1699 1.1 rearnsha {
1700 1.28 cegger struct plcom_softc *sc =
1701 1.28 cegger device_lookup_private(&plcom_cd, PLCOMUNIT(tp->t_dev));
1702 1.1 rearnsha
1703 1.1 rearnsha if (PLCOM_ISALIVE(sc) == 0)
1704 1.1 rearnsha return 0;
1705 1.1 rearnsha
1706 1.1 rearnsha if (sc->sc_mcr_rts == 0)
1707 1.1 rearnsha return 0;
1708 1.1 rearnsha
1709 1.36 skrll mutex_spin_enter(&sc->sc_lock);
1710 1.46 skrll
1711 1.1 rearnsha if (block) {
1712 1.1 rearnsha if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1713 1.1 rearnsha SET(sc->sc_rx_flags, RX_TTY_BLOCKED);
1714 1.1 rearnsha plcom_hwiflow(sc);
1715 1.1 rearnsha }
1716 1.1 rearnsha } else {
1717 1.1 rearnsha if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
1718 1.1 rearnsha CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1719 1.1 rearnsha plcom_schedrx(sc);
1720 1.1 rearnsha }
1721 1.1 rearnsha if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1722 1.1 rearnsha CLR(sc->sc_rx_flags, RX_TTY_BLOCKED);
1723 1.1 rearnsha plcom_hwiflow(sc);
1724 1.1 rearnsha }
1725 1.1 rearnsha }
1726 1.1 rearnsha
1727 1.36 skrll mutex_spin_exit(&sc->sc_lock);
1728 1.1 rearnsha return 1;
1729 1.1 rearnsha }
1730 1.46 skrll
1731 1.1 rearnsha /*
1732 1.1 rearnsha * (un)block input via hw flowcontrol
1733 1.1 rearnsha */
1734 1.1 rearnsha void
1735 1.1 rearnsha plcom_hwiflow(struct plcom_softc *sc)
1736 1.1 rearnsha {
1737 1.40 skrll struct plcom_instance *pi = &sc->sc_pi;
1738 1.40 skrll
1739 1.1 rearnsha if (sc->sc_mcr_rts == 0)
1740 1.1 rearnsha return;
1741 1.1 rearnsha
1742 1.1 rearnsha if (ISSET(sc->sc_rx_flags, RX_ANY_BLOCK)) {
1743 1.1 rearnsha CLR(sc->sc_mcr, sc->sc_mcr_rts);
1744 1.1 rearnsha CLR(sc->sc_mcr_active, sc->sc_mcr_rts);
1745 1.1 rearnsha } else {
1746 1.1 rearnsha SET(sc->sc_mcr, sc->sc_mcr_rts);
1747 1.1 rearnsha SET(sc->sc_mcr_active, sc->sc_mcr_rts);
1748 1.1 rearnsha }
1749 1.40 skrll switch (pi->pi_type) {
1750 1.40 skrll case PLCOM_TYPE_PL010:
1751 1.40 skrll if (sc->sc_set_mcr)
1752 1.40 skrll /* XXX device_unit() abuse */
1753 1.40 skrll sc->sc_set_mcr(sc->sc_set_mcr_arg,
1754 1.40 skrll device_unit(sc->sc_dev), sc->sc_mcr_active);
1755 1.40 skrll break;
1756 1.40 skrll case PLCOM_TYPE_PL011:
1757 1.67 mlelstv case PLCOM_TYPE_GENERIC_UART:
1758 1.40 skrll CLR(sc->sc_cr, PL011_MCR(PL01X_MCR_RTS | PL01X_MCR_DTR));
1759 1.40 skrll SET(sc->sc_cr, PL011_MCR(sc->sc_mcr_active));
1760 1.67 mlelstv PWRITE2(pi, PL011COM_CR, sc->sc_cr);
1761 1.40 skrll break;
1762 1.40 skrll }
1763 1.1 rearnsha }
1764 1.1 rearnsha
1765 1.1 rearnsha
1766 1.1 rearnsha void
1767 1.1 rearnsha plcomstart(struct tty *tp)
1768 1.1 rearnsha {
1769 1.28 cegger struct plcom_softc *sc =
1770 1.28 cegger device_lookup_private(&plcom_cd, PLCOMUNIT(tp->t_dev));
1771 1.40 skrll struct plcom_instance *pi = &sc->sc_pi;
1772 1.1 rearnsha
1773 1.1 rearnsha if (PLCOM_ISALIVE(sc) == 0)
1774 1.1 rearnsha return;
1775 1.1 rearnsha
1776 1.1 rearnsha if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
1777 1.68 riastrad return;
1778 1.1 rearnsha if (sc->sc_tx_stopped)
1779 1.68 riastrad return;
1780 1.1 rearnsha
1781 1.24 ad if (!ttypull(tp))
1782 1.68 riastrad return;
1783 1.1 rearnsha
1784 1.1 rearnsha /* Grab the first contiguous region of buffer space. */
1785 1.1 rearnsha {
1786 1.1 rearnsha u_char *tba;
1787 1.1 rearnsha int tbc;
1788 1.1 rearnsha
1789 1.1 rearnsha tba = tp->t_outq.c_cf;
1790 1.1 rearnsha tbc = ndqb(&tp->t_outq, 0);
1791 1.1 rearnsha
1792 1.36 skrll mutex_spin_enter(&sc->sc_lock);
1793 1.1 rearnsha
1794 1.1 rearnsha sc->sc_tba = tba;
1795 1.1 rearnsha sc->sc_tbc = tbc;
1796 1.1 rearnsha }
1797 1.1 rearnsha
1798 1.1 rearnsha SET(tp->t_state, TS_BUSY);
1799 1.1 rearnsha sc->sc_tx_busy = 1;
1800 1.1 rearnsha
1801 1.1 rearnsha /* Enable transmit completion interrupts if necessary. */
1802 1.40 skrll switch (pi->pi_type) {
1803 1.40 skrll case PLCOM_TYPE_PL010:
1804 1.40 skrll if (!ISSET(sc->sc_cr, PL010_CR_TIE)) {
1805 1.40 skrll SET(sc->sc_cr, PL010_CR_TIE);
1806 1.40 skrll PWRITE1(pi, PL010COM_CR, sc->sc_cr);
1807 1.40 skrll }
1808 1.40 skrll break;
1809 1.40 skrll case PLCOM_TYPE_PL011:
1810 1.67 mlelstv case PLCOM_TYPE_GENERIC_UART:
1811 1.40 skrll if (!ISSET(sc->sc_imsc, PL011_INT_TX)) {
1812 1.40 skrll SET(sc->sc_imsc, PL011_INT_TX);
1813 1.40 skrll PWRITE4(pi, PL011COM_IMSC, sc->sc_imsc);
1814 1.40 skrll }
1815 1.40 skrll break;
1816 1.1 rearnsha }
1817 1.1 rearnsha
1818 1.1 rearnsha /* Output the first chunk of the contiguous buffer. */
1819 1.1 rearnsha {
1820 1.42 skrll int n;
1821 1.1 rearnsha
1822 1.1 rearnsha n = sc->sc_tbc;
1823 1.67 mlelstv if (n > sc->sc_burstlen)
1824 1.67 mlelstv n = sc->sc_burstlen;
1825 1.40 skrll PWRITEM1(pi, PL01XCOM_DR, sc->sc_tba, n);
1826 1.1 rearnsha sc->sc_tbc -= n;
1827 1.1 rearnsha sc->sc_tba += n;
1828 1.1 rearnsha }
1829 1.36 skrll mutex_spin_exit(&sc->sc_lock);
1830 1.1 rearnsha }
1831 1.1 rearnsha
1832 1.1 rearnsha /*
1833 1.1 rearnsha * Stop output on a line.
1834 1.1 rearnsha */
1835 1.1 rearnsha void
1836 1.1 rearnsha plcomstop(struct tty *tp, int flag)
1837 1.1 rearnsha {
1838 1.28 cegger struct plcom_softc *sc =
1839 1.28 cegger device_lookup_private(&plcom_cd, PLCOMUNIT(tp->t_dev));
1840 1.1 rearnsha
1841 1.36 skrll mutex_spin_enter(&sc->sc_lock);
1842 1.1 rearnsha if (ISSET(tp->t_state, TS_BUSY)) {
1843 1.1 rearnsha /* Stop transmitting at the next chunk. */
1844 1.1 rearnsha sc->sc_tbc = 0;
1845 1.1 rearnsha sc->sc_heldtbc = 0;
1846 1.1 rearnsha if (!ISSET(tp->t_state, TS_TTSTOP))
1847 1.1 rearnsha SET(tp->t_state, TS_FLUSH);
1848 1.1 rearnsha }
1849 1.36 skrll mutex_spin_exit(&sc->sc_lock);
1850 1.1 rearnsha }
1851 1.1 rearnsha
1852 1.1 rearnsha void
1853 1.1 rearnsha plcomdiag(void *arg)
1854 1.1 rearnsha {
1855 1.1 rearnsha struct plcom_softc *sc = arg;
1856 1.1 rearnsha int overflows, floods;
1857 1.1 rearnsha
1858 1.36 skrll mutex_spin_enter(&sc->sc_lock);
1859 1.1 rearnsha overflows = sc->sc_overflows;
1860 1.1 rearnsha sc->sc_overflows = 0;
1861 1.1 rearnsha floods = sc->sc_floods;
1862 1.1 rearnsha sc->sc_floods = 0;
1863 1.1 rearnsha sc->sc_errors = 0;
1864 1.36 skrll mutex_spin_exit(&sc->sc_lock);
1865 1.1 rearnsha
1866 1.1 rearnsha log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
1867 1.38 skrll device_xname(sc->sc_dev),
1868 1.1 rearnsha overflows, overflows == 1 ? "" : "s",
1869 1.1 rearnsha floods, floods == 1 ? "" : "s");
1870 1.1 rearnsha }
1871 1.1 rearnsha
1872 1.68 riastrad static inline void
1873 1.1 rearnsha plcom_rxsoft(struct plcom_softc *sc, struct tty *tp)
1874 1.1 rearnsha {
1875 1.1 rearnsha int (*rint) (int, struct tty *) = tp->t_linesw->l_rint;
1876 1.40 skrll struct plcom_instance *pi = &sc->sc_pi;
1877 1.1 rearnsha u_char *get, *end;
1878 1.1 rearnsha u_int cc, scc;
1879 1.1 rearnsha u_char rsr;
1880 1.1 rearnsha int code;
1881 1.1 rearnsha
1882 1.1 rearnsha end = sc->sc_ebuf;
1883 1.1 rearnsha get = sc->sc_rbget;
1884 1.1 rearnsha scc = cc = plcom_rbuf_size - sc->sc_rbavail;
1885 1.1 rearnsha
1886 1.1 rearnsha if (cc == plcom_rbuf_size) {
1887 1.1 rearnsha sc->sc_floods++;
1888 1.1 rearnsha if (sc->sc_errors++ == 0)
1889 1.1 rearnsha callout_reset(&sc->sc_diag_callout, 60 * hz,
1890 1.1 rearnsha plcomdiag, sc);
1891 1.1 rearnsha }
1892 1.1 rearnsha
1893 1.1 rearnsha while (cc) {
1894 1.1 rearnsha code = get[0];
1895 1.1 rearnsha rsr = get[1];
1896 1.35 skrll if (ISSET(rsr, PL01X_RSR_ERROR)) {
1897 1.35 skrll if (ISSET(rsr, PL01X_RSR_OE)) {
1898 1.1 rearnsha sc->sc_overflows++;
1899 1.1 rearnsha if (sc->sc_errors++ == 0)
1900 1.1 rearnsha callout_reset(&sc->sc_diag_callout,
1901 1.1 rearnsha 60 * hz, plcomdiag, sc);
1902 1.1 rearnsha }
1903 1.35 skrll if (ISSET(rsr, PL01X_RSR_BE | PL01X_RSR_FE))
1904 1.1 rearnsha SET(code, TTY_FE);
1905 1.35 skrll if (ISSET(rsr, PL01X_RSR_PE))
1906 1.1 rearnsha SET(code, TTY_PE);
1907 1.1 rearnsha }
1908 1.1 rearnsha if ((*rint)(code, tp) == -1) {
1909 1.1 rearnsha /*
1910 1.1 rearnsha * The line discipline's buffer is out of space.
1911 1.1 rearnsha */
1912 1.1 rearnsha if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1913 1.1 rearnsha /*
1914 1.1 rearnsha * We're either not using flow control, or the
1915 1.1 rearnsha * line discipline didn't tell us to block for
1916 1.1 rearnsha * some reason. Either way, we have no way to
1917 1.1 rearnsha * know when there's more space available, so
1918 1.1 rearnsha * just drop the rest of the data.
1919 1.1 rearnsha */
1920 1.1 rearnsha get += cc << 1;
1921 1.1 rearnsha if (get >= end)
1922 1.1 rearnsha get -= plcom_rbuf_size << 1;
1923 1.1 rearnsha cc = 0;
1924 1.1 rearnsha } else {
1925 1.1 rearnsha /*
1926 1.1 rearnsha * Don't schedule any more receive processing
1927 1.1 rearnsha * until the line discipline tells us there's
1928 1.1 rearnsha * space available (through plcomhwiflow()).
1929 1.1 rearnsha * Leave the rest of the data in the input
1930 1.1 rearnsha * buffer.
1931 1.1 rearnsha */
1932 1.1 rearnsha SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1933 1.1 rearnsha }
1934 1.1 rearnsha break;
1935 1.1 rearnsha }
1936 1.1 rearnsha get += 2;
1937 1.1 rearnsha if (get >= end)
1938 1.1 rearnsha get = sc->sc_rbuf;
1939 1.1 rearnsha cc--;
1940 1.1 rearnsha }
1941 1.1 rearnsha if (cc != scc) {
1942 1.1 rearnsha sc->sc_rbget = get;
1943 1.36 skrll mutex_spin_enter(&sc->sc_lock);
1944 1.36 skrll
1945 1.1 rearnsha cc = sc->sc_rbavail += scc - cc;
1946 1.1 rearnsha /* Buffers should be ok again, release possible block. */
1947 1.1 rearnsha if (cc >= sc->sc_r_lowat) {
1948 1.1 rearnsha if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1949 1.1 rearnsha CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1950 1.40 skrll switch (pi->pi_type) {
1951 1.40 skrll case PLCOM_TYPE_PL010:
1952 1.40 skrll SET(sc->sc_cr,
1953 1.40 skrll PL010_CR_RIE | PL010_CR_RTIE);
1954 1.40 skrll PWRITE1(pi, PL010COM_CR, sc->sc_cr);
1955 1.40 skrll break;
1956 1.40 skrll case PLCOM_TYPE_PL011:
1957 1.67 mlelstv case PLCOM_TYPE_GENERIC_UART:
1958 1.40 skrll SET(sc->sc_imsc,
1959 1.40 skrll PL011_INT_RX | PL011_INT_RT);
1960 1.40 skrll PWRITE4(pi, PL011COM_IMSC, sc->sc_imsc);
1961 1.40 skrll break;
1962 1.40 skrll }
1963 1.1 rearnsha }
1964 1.1 rearnsha if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
1965 1.1 rearnsha CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
1966 1.1 rearnsha plcom_hwiflow(sc);
1967 1.1 rearnsha }
1968 1.1 rearnsha }
1969 1.36 skrll mutex_spin_exit(&sc->sc_lock);
1970 1.1 rearnsha }
1971 1.1 rearnsha }
1972 1.1 rearnsha
1973 1.68 riastrad static inline void
1974 1.1 rearnsha plcom_txsoft(struct plcom_softc *sc, struct tty *tp)
1975 1.1 rearnsha {
1976 1.1 rearnsha
1977 1.1 rearnsha CLR(tp->t_state, TS_BUSY);
1978 1.1 rearnsha if (ISSET(tp->t_state, TS_FLUSH))
1979 1.1 rearnsha CLR(tp->t_state, TS_FLUSH);
1980 1.1 rearnsha else
1981 1.1 rearnsha ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
1982 1.1 rearnsha (*tp->t_linesw->l_start)(tp);
1983 1.1 rearnsha }
1984 1.1 rearnsha
1985 1.68 riastrad static inline void
1986 1.1 rearnsha plcom_stsoft(struct plcom_softc *sc, struct tty *tp)
1987 1.1 rearnsha {
1988 1.1 rearnsha u_char msr, delta;
1989 1.1 rearnsha
1990 1.36 skrll mutex_spin_enter(&sc->sc_lock);
1991 1.1 rearnsha msr = sc->sc_msr;
1992 1.1 rearnsha delta = sc->sc_msr_delta;
1993 1.1 rearnsha sc->sc_msr_delta = 0;
1994 1.36 skrll mutex_spin_exit(&sc->sc_lock);
1995 1.1 rearnsha
1996 1.1 rearnsha if (ISSET(delta, sc->sc_msr_dcd)) {
1997 1.1 rearnsha /*
1998 1.1 rearnsha * Inform the tty layer that carrier detect changed.
1999 1.1 rearnsha */
2000 1.35 skrll (void) (*tp->t_linesw->l_modem)(tp, ISSET(msr, PL01X_MSR_DCD));
2001 1.1 rearnsha }
2002 1.1 rearnsha
2003 1.1 rearnsha if (ISSET(delta, sc->sc_msr_cts)) {
2004 1.1 rearnsha /* Block or unblock output according to flow control. */
2005 1.1 rearnsha if (ISSET(msr, sc->sc_msr_cts)) {
2006 1.1 rearnsha sc->sc_tx_stopped = 0;
2007 1.1 rearnsha (*tp->t_linesw->l_start)(tp);
2008 1.1 rearnsha } else {
2009 1.1 rearnsha sc->sc_tx_stopped = 1;
2010 1.1 rearnsha }
2011 1.1 rearnsha }
2012 1.1 rearnsha
2013 1.1 rearnsha #ifdef PLCOM_DEBUG
2014 1.1 rearnsha if (plcom_debug)
2015 1.1 rearnsha plcomstatus(sc, "plcom_stsoft");
2016 1.1 rearnsha #endif
2017 1.1 rearnsha }
2018 1.1 rearnsha
2019 1.1 rearnsha void
2020 1.1 rearnsha plcomsoft(void *arg)
2021 1.1 rearnsha {
2022 1.1 rearnsha struct plcom_softc *sc = arg;
2023 1.1 rearnsha struct tty *tp;
2024 1.1 rearnsha
2025 1.1 rearnsha if (PLCOM_ISALIVE(sc) == 0)
2026 1.1 rearnsha return;
2027 1.1 rearnsha
2028 1.21 ad tp = sc->sc_tty;
2029 1.46 skrll
2030 1.21 ad if (sc->sc_rx_ready) {
2031 1.21 ad sc->sc_rx_ready = 0;
2032 1.21 ad plcom_rxsoft(sc, tp);
2033 1.21 ad }
2034 1.1 rearnsha
2035 1.21 ad if (sc->sc_st_check) {
2036 1.21 ad sc->sc_st_check = 0;
2037 1.21 ad plcom_stsoft(sc, tp);
2038 1.21 ad }
2039 1.1 rearnsha
2040 1.21 ad if (sc->sc_tx_done) {
2041 1.21 ad sc->sc_tx_done = 0;
2042 1.21 ad plcom_txsoft(sc, tp);
2043 1.1 rearnsha }
2044 1.1 rearnsha }
2045 1.1 rearnsha
2046 1.40 skrll bool
2047 1.40 skrll plcom_intstatus(struct plcom_instance *pi, u_int *istatus)
2048 1.40 skrll {
2049 1.40 skrll bool ret = false;
2050 1.40 skrll u_int stat = 0;
2051 1.40 skrll
2052 1.40 skrll switch (pi->pi_type) {
2053 1.40 skrll case PLCOM_TYPE_PL010:
2054 1.40 skrll stat = PREAD1(pi, PL010COM_IIR);
2055 1.40 skrll ret = ISSET(stat, PL010_IIR_IMASK);
2056 1.40 skrll break;
2057 1.40 skrll case PLCOM_TYPE_PL011:
2058 1.67 mlelstv case PLCOM_TYPE_GENERIC_UART:
2059 1.40 skrll stat = PREAD4(pi, PL011COM_MIS);
2060 1.40 skrll ret = ISSET(stat, PL011_INT_ALLMASK);
2061 1.40 skrll break;
2062 1.40 skrll }
2063 1.40 skrll *istatus = stat;
2064 1.40 skrll
2065 1.40 skrll return ret;
2066 1.46 skrll }
2067 1.40 skrll
2068 1.1 rearnsha int
2069 1.1 rearnsha plcomintr(void *arg)
2070 1.1 rearnsha {
2071 1.1 rearnsha struct plcom_softc *sc = arg;
2072 1.40 skrll struct plcom_instance *pi = &sc->sc_pi;
2073 1.1 rearnsha u_char *put, *end;
2074 1.1 rearnsha u_int cc;
2075 1.40 skrll u_int istatus = 0;
2076 1.40 skrll u_char rsr;
2077 1.40 skrll bool intr = false;
2078 1.40 skrll
2079 1.40 skrll PLCOM_BARRIER(pi, BR | BW);
2080 1.1 rearnsha
2081 1.1 rearnsha if (PLCOM_ISALIVE(sc) == 0)
2082 1.1 rearnsha return 0;
2083 1.1 rearnsha
2084 1.36 skrll mutex_spin_enter(&sc->sc_lock);
2085 1.40 skrll intr = plcom_intstatus(pi, &istatus);
2086 1.40 skrll if (!intr) {
2087 1.36 skrll mutex_spin_exit(&sc->sc_lock);
2088 1.1 rearnsha return 0;
2089 1.1 rearnsha }
2090 1.1 rearnsha
2091 1.1 rearnsha end = sc->sc_ebuf;
2092 1.1 rearnsha put = sc->sc_rbput;
2093 1.1 rearnsha cc = sc->sc_rbavail;
2094 1.1 rearnsha
2095 1.1 rearnsha do {
2096 1.40 skrll u_int msr = 0, delta, fr;
2097 1.40 skrll bool rxintr = false, txintr = false, msintr;
2098 1.1 rearnsha
2099 1.40 skrll /* don't need RI here*/
2100 1.40 skrll fr = PREAD1(pi, PL01XCOM_FR);
2101 1.1 rearnsha
2102 1.35 skrll if (!ISSET(fr, PL01X_FR_RXFE) &&
2103 1.1 rearnsha !ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
2104 1.1 rearnsha while (cc > 0) {
2105 1.1 rearnsha int cn_trapped = 0;
2106 1.40 skrll put[0] = PREAD1(pi, PL01XCOM_DR);
2107 1.40 skrll rsr = PREAD1(pi, PL01XCOM_RSR);
2108 1.1 rearnsha /* Clear any error status. */
2109 1.35 skrll if (ISSET(rsr, PL01X_RSR_ERROR))
2110 1.40 skrll PWRITE1(pi, PL01XCOM_ECR, 0);
2111 1.67 mlelstv
2112 1.35 skrll if (ISSET(rsr, PL01X_RSR_BE)) {
2113 1.10 rearnsha cn_trapped = 0;
2114 1.1 rearnsha cn_check_magic(sc->sc_tty->t_dev,
2115 1.1 rearnsha CNC_BREAK, plcom_cnm_state);
2116 1.1 rearnsha if (cn_trapped)
2117 1.1 rearnsha continue;
2118 1.1 rearnsha #if defined(KGDB)
2119 1.1 rearnsha if (ISSET(sc->sc_hwflags,
2120 1.1 rearnsha PLCOM_HW_KGDB)) {
2121 1.1 rearnsha kgdb_connect(1);
2122 1.1 rearnsha continue;
2123 1.1 rearnsha }
2124 1.1 rearnsha #endif
2125 1.1 rearnsha }
2126 1.1 rearnsha
2127 1.1 rearnsha put[1] = rsr;
2128 1.10 rearnsha cn_trapped = 0;
2129 1.40 skrll cn_check_magic(sc->sc_tty->t_dev, put[0],
2130 1.40 skrll plcom_cnm_state);
2131 1.1 rearnsha if (cn_trapped) {
2132 1.40 skrll fr = PREAD1(pi, PL01XCOM_FR);
2133 1.35 skrll if (ISSET(fr, PL01X_FR_RXFE))
2134 1.1 rearnsha break;
2135 1.1 rearnsha
2136 1.1 rearnsha continue;
2137 1.1 rearnsha }
2138 1.1 rearnsha put += 2;
2139 1.1 rearnsha if (put >= end)
2140 1.1 rearnsha put = sc->sc_rbuf;
2141 1.1 rearnsha cc--;
2142 1.1 rearnsha
2143 1.40 skrll /* don't need RI here*/
2144 1.40 skrll fr = PREAD1(pi, PL01XCOM_FR);
2145 1.35 skrll if (ISSET(fr, PL01X_FR_RXFE))
2146 1.1 rearnsha break;
2147 1.1 rearnsha }
2148 1.1 rearnsha
2149 1.1 rearnsha /*
2150 1.1 rearnsha * Current string of incoming characters ended because
2151 1.1 rearnsha * no more data was available or we ran out of space.
2152 1.1 rearnsha * Schedule a receive event if any data was received.
2153 1.1 rearnsha * If we're out of space, turn off receive interrupts.
2154 1.1 rearnsha */
2155 1.1 rearnsha sc->sc_rbput = put;
2156 1.1 rearnsha sc->sc_rbavail = cc;
2157 1.1 rearnsha if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
2158 1.1 rearnsha sc->sc_rx_ready = 1;
2159 1.1 rearnsha
2160 1.1 rearnsha /*
2161 1.1 rearnsha * See if we are in danger of overflowing a buffer. If
2162 1.1 rearnsha * so, use hardware flow control to ease the pressure.
2163 1.1 rearnsha */
2164 1.1 rearnsha if (!ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED) &&
2165 1.1 rearnsha cc < sc->sc_r_hiwat) {
2166 1.1 rearnsha SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
2167 1.1 rearnsha plcom_hwiflow(sc);
2168 1.1 rearnsha }
2169 1.1 rearnsha
2170 1.1 rearnsha /*
2171 1.1 rearnsha * If we're out of space, disable receive interrupts
2172 1.1 rearnsha * until the queue has drained a bit.
2173 1.1 rearnsha */
2174 1.1 rearnsha if (!cc) {
2175 1.1 rearnsha SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
2176 1.40 skrll switch (pi->pi_type) {
2177 1.40 skrll case PLCOM_TYPE_PL010:
2178 1.40 skrll CLR(sc->sc_cr,
2179 1.40 skrll PL010_CR_RIE | PL010_CR_RTIE);
2180 1.40 skrll PWRITE1(pi, PL010COM_CR, sc->sc_cr);
2181 1.40 skrll break;
2182 1.40 skrll case PLCOM_TYPE_PL011:
2183 1.67 mlelstv case PLCOM_TYPE_GENERIC_UART:
2184 1.40 skrll CLR(sc->sc_imsc,
2185 1.40 skrll PL011_INT_RT | PL011_INT_RX);
2186 1.40 skrll PWRITE4(pi, PL011COM_IMSC, sc->sc_imsc);
2187 1.40 skrll break;
2188 1.40 skrll }
2189 1.1 rearnsha }
2190 1.1 rearnsha } else {
2191 1.40 skrll switch (pi->pi_type) {
2192 1.40 skrll case PLCOM_TYPE_PL010:
2193 1.40 skrll rxintr = ISSET(istatus, PL010_IIR_RIS);
2194 1.40 skrll if (rxintr) {
2195 1.40 skrll PWRITE1(pi, PL010COM_CR, 0);
2196 1.40 skrll delay(10);
2197 1.40 skrll PWRITE1(pi, PL010COM_CR, sc->sc_cr);
2198 1.40 skrll continue;
2199 1.40 skrll }
2200 1.40 skrll break;
2201 1.40 skrll case PLCOM_TYPE_PL011:
2202 1.67 mlelstv case PLCOM_TYPE_GENERIC_UART:
2203 1.40 skrll rxintr = ISSET(istatus, PL011_INT_RX);
2204 1.40 skrll if (rxintr) {
2205 1.67 mlelstv PWRITE2(pi, PL011COM_CR, 0);
2206 1.40 skrll delay(10);
2207 1.67 mlelstv PWRITE2(pi, PL011COM_CR, sc->sc_cr);
2208 1.40 skrll continue;
2209 1.40 skrll }
2210 1.40 skrll break;
2211 1.1 rearnsha }
2212 1.1 rearnsha }
2213 1.1 rearnsha
2214 1.40 skrll switch (pi->pi_type) {
2215 1.40 skrll case PLCOM_TYPE_PL010:
2216 1.40 skrll msr = PREAD1(pi, PL01XCOM_FR);
2217 1.40 skrll break;
2218 1.40 skrll case PLCOM_TYPE_PL011:
2219 1.67 mlelstv case PLCOM_TYPE_GENERIC_UART:
2220 1.40 skrll msr = PREAD4(pi, PL01XCOM_FR);
2221 1.40 skrll break;
2222 1.40 skrll }
2223 1.1 rearnsha delta = msr ^ sc->sc_msr;
2224 1.1 rearnsha sc->sc_msr = msr;
2225 1.40 skrll
2226 1.1 rearnsha /* Clear any pending modem status interrupt. */
2227 1.40 skrll switch (pi->pi_type) {
2228 1.40 skrll case PLCOM_TYPE_PL010:
2229 1.40 skrll msintr = ISSET(istatus, PL010_IIR_MIS);
2230 1.40 skrll if (msintr) {
2231 1.40 skrll PWRITE1(pi, PL010COM_ICR, 0);
2232 1.40 skrll }
2233 1.40 skrll break;
2234 1.40 skrll case PLCOM_TYPE_PL011:
2235 1.67 mlelstv case PLCOM_TYPE_GENERIC_UART:
2236 1.40 skrll msintr = ISSET(istatus, PL011_INT_MSMASK);
2237 1.40 skrll if (msintr) {
2238 1.40 skrll PWRITE4(pi, PL011COM_ICR, PL011_INT_MSMASK);
2239 1.40 skrll }
2240 1.40 skrll break;
2241 1.40 skrll }
2242 1.1 rearnsha /*
2243 1.1 rearnsha * Pulse-per-second (PSS) signals on edge of DCD?
2244 1.1 rearnsha * Process these even if line discipline is ignoring DCD.
2245 1.1 rearnsha */
2246 1.1 rearnsha if (delta & sc->sc_ppsmask) {
2247 1.1 rearnsha struct timeval tv;
2248 1.26 ad mutex_spin_enter(&timecounter_lock);
2249 1.1 rearnsha if ((msr & sc->sc_ppsmask) == sc->sc_ppsassert) {
2250 1.1 rearnsha /* XXX nanotime() */
2251 1.1 rearnsha microtime(&tv);
2252 1.46 skrll TIMEVAL_TO_TIMESPEC(&tv,
2253 1.1 rearnsha &sc->ppsinfo.assert_timestamp);
2254 1.1 rearnsha if (sc->ppsparam.mode & PPS_OFFSETASSERT) {
2255 1.1 rearnsha timespecadd(&sc->ppsinfo.assert_timestamp,
2256 1.1 rearnsha &sc->ppsparam.assert_offset,
2257 1.1 rearnsha &sc->ppsinfo.assert_timestamp);
2258 1.1 rearnsha }
2259 1.1 rearnsha
2260 1.1 rearnsha #ifdef PPS_SYNC
2261 1.1 rearnsha if (sc->ppsparam.mode & PPS_HARDPPSONASSERT)
2262 1.1 rearnsha hardpps(&tv, tv.tv_usec);
2263 1.1 rearnsha #endif
2264 1.1 rearnsha sc->ppsinfo.assert_sequence++;
2265 1.1 rearnsha sc->ppsinfo.current_mode = sc->ppsparam.mode;
2266 1.1 rearnsha
2267 1.1 rearnsha } else if ((msr & sc->sc_ppsmask) == sc->sc_ppsclear) {
2268 1.1 rearnsha /* XXX nanotime() */
2269 1.1 rearnsha microtime(&tv);
2270 1.46 skrll TIMEVAL_TO_TIMESPEC(&tv,
2271 1.1 rearnsha &sc->ppsinfo.clear_timestamp);
2272 1.1 rearnsha if (sc->ppsparam.mode & PPS_OFFSETCLEAR) {
2273 1.1 rearnsha timespecadd(&sc->ppsinfo.clear_timestamp,
2274 1.1 rearnsha &sc->ppsparam.clear_offset,
2275 1.1 rearnsha &sc->ppsinfo.clear_timestamp);
2276 1.1 rearnsha }
2277 1.1 rearnsha
2278 1.1 rearnsha #ifdef PPS_SYNC
2279 1.1 rearnsha if (sc->ppsparam.mode & PPS_HARDPPSONCLEAR)
2280 1.1 rearnsha hardpps(&tv, tv.tv_usec);
2281 1.1 rearnsha #endif
2282 1.1 rearnsha sc->ppsinfo.clear_sequence++;
2283 1.1 rearnsha sc->ppsinfo.current_mode = sc->ppsparam.mode;
2284 1.1 rearnsha }
2285 1.26 ad mutex_spin_exit(&timecounter_lock);
2286 1.1 rearnsha }
2287 1.1 rearnsha
2288 1.1 rearnsha /*
2289 1.1 rearnsha * Process normal status changes
2290 1.1 rearnsha */
2291 1.1 rearnsha if (ISSET(delta, sc->sc_msr_mask)) {
2292 1.1 rearnsha SET(sc->sc_msr_delta, delta);
2293 1.1 rearnsha
2294 1.1 rearnsha /*
2295 1.1 rearnsha * Stop output immediately if we lose the output
2296 1.1 rearnsha * flow control signal or carrier detect.
2297 1.1 rearnsha */
2298 1.1 rearnsha if (ISSET(~msr, sc->sc_msr_mask)) {
2299 1.1 rearnsha sc->sc_tbc = 0;
2300 1.1 rearnsha sc->sc_heldtbc = 0;
2301 1.1 rearnsha #ifdef PLCOM_DEBUG
2302 1.1 rearnsha if (plcom_debug)
2303 1.1 rearnsha plcomstatus(sc, "plcomintr ");
2304 1.1 rearnsha #endif
2305 1.1 rearnsha }
2306 1.1 rearnsha
2307 1.1 rearnsha sc->sc_st_check = 1;
2308 1.1 rearnsha }
2309 1.1 rearnsha
2310 1.46 skrll /*
2311 1.1 rearnsha * Done handling any receive interrupts. See if data
2312 1.40 skrll * can be transmitted as well. Schedule tx done
2313 1.40 skrll * event if no data left and tty was marked busy.
2314 1.1 rearnsha */
2315 1.46 skrll
2316 1.40 skrll switch (pi->pi_type) {
2317 1.40 skrll case PLCOM_TYPE_PL010:
2318 1.40 skrll txintr = ISSET(istatus, PL010_IIR_TIS);
2319 1.40 skrll break;
2320 1.40 skrll case PLCOM_TYPE_PL011:
2321 1.40 skrll txintr = ISSET(istatus, PL011_INT_TX);
2322 1.40 skrll break;
2323 1.40 skrll }
2324 1.40 skrll if (txintr) {
2325 1.1 rearnsha /*
2326 1.1 rearnsha * If we've delayed a parameter change, do it
2327 1.1 rearnsha * now, and restart * output.
2328 1.1 rearnsha */
2329 1.40 skrll // PWRITE4(pi, PL011COM_ICR, PL011_INT_TX);
2330 1.1 rearnsha if (sc->sc_heldchange) {
2331 1.1 rearnsha plcom_loadchannelregs(sc);
2332 1.1 rearnsha sc->sc_heldchange = 0;
2333 1.1 rearnsha sc->sc_tbc = sc->sc_heldtbc;
2334 1.1 rearnsha sc->sc_heldtbc = 0;
2335 1.1 rearnsha }
2336 1.1 rearnsha
2337 1.46 skrll /*
2338 1.1 rearnsha * Output the next chunk of the contiguous
2339 1.1 rearnsha * buffer, if any.
2340 1.1 rearnsha */
2341 1.1 rearnsha if (sc->sc_tbc > 0) {
2342 1.1 rearnsha int n;
2343 1.1 rearnsha
2344 1.1 rearnsha n = sc->sc_tbc;
2345 1.67 mlelstv if (n > sc->sc_burstlen)
2346 1.67 mlelstv n = sc->sc_burstlen;
2347 1.40 skrll PWRITEM1(pi, PL01XCOM_DR, sc->sc_tba, n);
2348 1.1 rearnsha sc->sc_tbc -= n;
2349 1.1 rearnsha sc->sc_tba += n;
2350 1.1 rearnsha } else {
2351 1.1 rearnsha /*
2352 1.40 skrll * Disable transmit completion
2353 1.1 rearnsha * interrupts if necessary.
2354 1.1 rearnsha */
2355 1.40 skrll switch (pi->pi_type) {
2356 1.40 skrll case PLCOM_TYPE_PL010:
2357 1.40 skrll if (ISSET(sc->sc_cr, PL010_CR_TIE)) {
2358 1.40 skrll CLR(sc->sc_cr, PL010_CR_TIE);
2359 1.40 skrll PWRITE1(pi, PL010COM_CR,
2360 1.40 skrll sc->sc_cr);
2361 1.40 skrll }
2362 1.40 skrll break;
2363 1.40 skrll case PLCOM_TYPE_PL011:
2364 1.67 mlelstv case PLCOM_TYPE_GENERIC_UART:
2365 1.40 skrll if (ISSET(sc->sc_imsc, PL011_INT_TX)) {
2366 1.40 skrll CLR(sc->sc_imsc, PL011_INT_TX);
2367 1.40 skrll PWRITE4(pi, PL011COM_IMSC,
2368 1.40 skrll sc->sc_imsc);
2369 1.40 skrll }
2370 1.40 skrll break;
2371 1.1 rearnsha }
2372 1.1 rearnsha if (sc->sc_tx_busy) {
2373 1.1 rearnsha sc->sc_tx_busy = 0;
2374 1.1 rearnsha sc->sc_tx_done = 1;
2375 1.1 rearnsha }
2376 1.1 rearnsha }
2377 1.1 rearnsha }
2378 1.40 skrll
2379 1.40 skrll } while (plcom_intstatus(pi, &istatus));
2380 1.1 rearnsha
2381 1.36 skrll mutex_spin_exit(&sc->sc_lock);
2382 1.1 rearnsha
2383 1.1 rearnsha /* Wake up the poller. */
2384 1.67 mlelstv if ((sc->sc_rx_ready | sc->sc_st_check | sc->sc_tx_done) != 0)
2385 1.67 mlelstv softint_schedule(sc->sc_si);
2386 1.1 rearnsha
2387 1.33 tls #ifdef RND_COM
2388 1.40 skrll rnd_add_uint32(&sc->rnd_source, istatus | rsr);
2389 1.1 rearnsha #endif
2390 1.1 rearnsha
2391 1.40 skrll PLCOM_BARRIER(pi, BR | BW);
2392 1.40 skrll
2393 1.1 rearnsha return 1;
2394 1.1 rearnsha }
2395 1.1 rearnsha
2396 1.1 rearnsha /*
2397 1.1 rearnsha * The following functions are polled getc and putc routines, shared
2398 1.1 rearnsha * by the console and kgdb glue.
2399 1.46 skrll *
2400 1.1 rearnsha * The read-ahead code is so that you can detect pending in-band
2401 1.1 rearnsha * cn_magic in polled mode while doing output rather than having to
2402 1.1 rearnsha * wait until the kernel decides it needs input.
2403 1.1 rearnsha */
2404 1.1 rearnsha
2405 1.1 rearnsha #define MAX_READAHEAD 20
2406 1.1 rearnsha static int plcom_readahead[MAX_READAHEAD];
2407 1.1 rearnsha static int plcom_readaheadcount = 0;
2408 1.1 rearnsha
2409 1.1 rearnsha int
2410 1.40 skrll plcom_common_getc(dev_t dev, struct plcom_instance *pi)
2411 1.1 rearnsha {
2412 1.1 rearnsha int s = splserial();
2413 1.58 skrll u_char c;
2414 1.1 rearnsha
2415 1.1 rearnsha /* got a character from reading things earlier */
2416 1.1 rearnsha if (plcom_readaheadcount > 0) {
2417 1.1 rearnsha int i;
2418 1.1 rearnsha
2419 1.1 rearnsha c = plcom_readahead[0];
2420 1.1 rearnsha for (i = 1; i < plcom_readaheadcount; i++) {
2421 1.1 rearnsha plcom_readahead[i-1] = plcom_readahead[i];
2422 1.1 rearnsha }
2423 1.1 rearnsha plcom_readaheadcount--;
2424 1.1 rearnsha splx(s);
2425 1.1 rearnsha return c;
2426 1.1 rearnsha }
2427 1.1 rearnsha
2428 1.58 skrll if (ISSET(PREAD1(pi, PL01XCOM_FR), PL01X_FR_RXFE)) {
2429 1.58 skrll splx(s);
2430 1.58 skrll return -1;
2431 1.58 skrll }
2432 1.1 rearnsha
2433 1.40 skrll c = PREAD1(pi, PL01XCOM_DR);
2434 1.1 rearnsha {
2435 1.47 skrll int cn_trapped __unused = 0;
2436 1.66 riastrad
2437 1.1 rearnsha if (!db_active)
2438 1.1 rearnsha cn_check_magic(dev, c, plcom_cnm_state);
2439 1.1 rearnsha }
2440 1.1 rearnsha splx(s);
2441 1.1 rearnsha return c;
2442 1.1 rearnsha }
2443 1.1 rearnsha
2444 1.1 rearnsha void
2445 1.40 skrll plcom_common_putc(dev_t dev, struct plcom_instance *pi, int c)
2446 1.1 rearnsha {
2447 1.1 rearnsha int s = splserial();
2448 1.1 rearnsha int timo;
2449 1.1 rearnsha
2450 1.1 rearnsha int cin, stat;
2451 1.46 skrll if (plcom_readaheadcount < MAX_READAHEAD
2452 1.40 skrll && !ISSET(stat = PREAD1(pi, PL01XCOM_FR), PL01X_FR_RXFE)) {
2453 1.47 skrll int cn_trapped __unused = 0;
2454 1.40 skrll cin = PREAD1(pi, PL01XCOM_DR);
2455 1.1 rearnsha cn_check_magic(dev, cin, plcom_cnm_state);
2456 1.1 rearnsha plcom_readahead[plcom_readaheadcount++] = cin;
2457 1.1 rearnsha }
2458 1.1 rearnsha
2459 1.1 rearnsha /* wait for any pending transmission to finish */
2460 1.1 rearnsha timo = 150000;
2461 1.53 skrll while (ISSET(PREAD1(pi, PL01XCOM_FR), PL01X_FR_TXFF) && --timo)
2462 1.1 rearnsha continue;
2463 1.1 rearnsha
2464 1.40 skrll PWRITE1(pi, PL01XCOM_DR, c);
2465 1.40 skrll PLCOM_BARRIER(pi, BR | BW);
2466 1.1 rearnsha
2467 1.1 rearnsha splx(s);
2468 1.1 rearnsha }
2469 1.1 rearnsha
2470 1.1 rearnsha /*
2471 1.1 rearnsha * Initialize UART for use as console or KGDB line.
2472 1.1 rearnsha */
2473 1.1 rearnsha int
2474 1.40 skrll plcominit(struct plcom_instance *pi, int rate, int frequency, tcflag_t cflag)
2475 1.1 rearnsha {
2476 1.67 mlelstv uint32_t lcr;
2477 1.1 rearnsha
2478 1.40 skrll switch (pi->pi_type) {
2479 1.40 skrll case PLCOM_TYPE_PL010:
2480 1.40 skrll if (pi->pi_size == 0)
2481 1.40 skrll pi->pi_size = PL010COM_UART_SIZE;
2482 1.40 skrll break;
2483 1.40 skrll case PLCOM_TYPE_PL011:
2484 1.67 mlelstv case PLCOM_TYPE_GENERIC_UART:
2485 1.40 skrll if (pi->pi_size == 0)
2486 1.40 skrll pi->pi_size = PL011COM_UART_SIZE;
2487 1.40 skrll break;
2488 1.40 skrll default:
2489 1.40 skrll panic("Unknown plcom type");
2490 1.40 skrll }
2491 1.40 skrll
2492 1.40 skrll if (bus_space_map(pi->pi_iot, pi->pi_iobase, pi->pi_size, 0,
2493 1.40 skrll &pi->pi_ioh))
2494 1.1 rearnsha return ENOMEM; /* ??? */
2495 1.1 rearnsha
2496 1.40 skrll lcr = cflag2lcr(cflag) | PL01X_LCR_FEN;
2497 1.40 skrll switch (pi->pi_type) {
2498 1.40 skrll case PLCOM_TYPE_PL010:
2499 1.40 skrll PWRITE1(pi, PL010COM_CR, 0);
2500 1.40 skrll
2501 1.54 jmcneill if (rate && frequency) {
2502 1.54 jmcneill rate = pl010comspeed(rate, frequency);
2503 1.54 jmcneill PWRITE1(pi, PL010COM_DLBL, (rate & 0xff));
2504 1.54 jmcneill PWRITE1(pi, PL010COM_DLBH, ((rate >> 8) & 0xff));
2505 1.54 jmcneill }
2506 1.40 skrll PWRITE1(pi, PL010COM_LCR, lcr);
2507 1.40 skrll PWRITE1(pi, PL010COM_CR, PL01X_CR_UARTEN);
2508 1.40 skrll break;
2509 1.40 skrll case PLCOM_TYPE_PL011:
2510 1.67 mlelstv case PLCOM_TYPE_GENERIC_UART:
2511 1.67 mlelstv PWRITE2(pi, PL011COM_CR, 0);
2512 1.40 skrll
2513 1.54 jmcneill if (rate && frequency) {
2514 1.54 jmcneill rate = pl011comspeed(rate, frequency);
2515 1.54 jmcneill PWRITE1(pi, PL011COM_FBRD, rate & ((1 << 6) - 1));
2516 1.54 jmcneill PWRITE4(pi, PL011COM_IBRD, rate >> 6);
2517 1.54 jmcneill }
2518 1.40 skrll PWRITE1(pi, PL011COM_LCRH, lcr);
2519 1.67 mlelstv PWRITE2(pi, PL011COM_CR,
2520 1.40 skrll PL01X_CR_UARTEN | PL011_CR_RXE | PL011_CR_TXE);
2521 1.40 skrll break;
2522 1.40 skrll }
2523 1.1 rearnsha
2524 1.1 rearnsha #if 0
2525 1.1 rearnsha /* Ought to do something like this, but we have no sc to
2526 1.1 rearnsha dereference. */
2527 1.15 thorpej /* XXX device_unit() abuse */
2528 1.43 skrll sc->sc_set_mcr(sc->sc_set_mcr_arg, device_unit(sc->sc_dev),
2529 1.35 skrll PL01X_MCR_DTR | PL01X_MCR_RTS);
2530 1.1 rearnsha #endif
2531 1.1 rearnsha
2532 1.1 rearnsha return 0;
2533 1.1 rearnsha }
2534 1.1 rearnsha
2535 1.1 rearnsha /*
2536 1.1 rearnsha * Following are all routines needed for PLCOM to act as console
2537 1.1 rearnsha */
2538 1.1 rearnsha struct consdev plcomcons = {
2539 1.1 rearnsha NULL, NULL, plcomcngetc, plcomcnputc, plcomcnpollc, NULL,
2540 1.59 jmcneill plcomcnhalt, NULL, NODEV, CN_NORMAL
2541 1.1 rearnsha };
2542 1.1 rearnsha
2543 1.1 rearnsha int
2544 1.40 skrll plcomcnattach(struct plcom_instance *pi, int rate, int frequency,
2545 1.1 rearnsha tcflag_t cflag, int unit)
2546 1.1 rearnsha {
2547 1.1 rearnsha int res;
2548 1.1 rearnsha
2549 1.40 skrll plcomcons_info = *pi;
2550 1.40 skrll
2551 1.40 skrll res = plcominit(&plcomcons_info, rate, frequency, cflag);
2552 1.1 rearnsha if (res)
2553 1.1 rearnsha return res;
2554 1.1 rearnsha
2555 1.1 rearnsha cn_tab = &plcomcons;
2556 1.1 rearnsha cn_init_magic(&plcom_cnm_state);
2557 1.1 rearnsha cn_set_magic("\047\001"); /* default magic is BREAK */
2558 1.1 rearnsha
2559 1.1 rearnsha plcomconsunit = unit;
2560 1.1 rearnsha plcomconsrate = rate;
2561 1.1 rearnsha plcomconscflag = cflag;
2562 1.1 rearnsha
2563 1.1 rearnsha return 0;
2564 1.1 rearnsha }
2565 1.1 rearnsha
2566 1.1 rearnsha void
2567 1.1 rearnsha plcomcndetach(void)
2568 1.1 rearnsha {
2569 1.40 skrll
2570 1.40 skrll bus_space_unmap(plcomcons_info.pi_iot, plcomcons_info.pi_ioh,
2571 1.40 skrll plcomcons_info.pi_size);
2572 1.40 skrll plcomcons_info.pi_iot = NULL;
2573 1.1 rearnsha
2574 1.1 rearnsha cn_tab = NULL;
2575 1.1 rearnsha }
2576 1.1 rearnsha
2577 1.1 rearnsha int
2578 1.1 rearnsha plcomcngetc(dev_t dev)
2579 1.1 rearnsha {
2580 1.40 skrll return plcom_common_getc(dev, &plcomcons_info);
2581 1.1 rearnsha }
2582 1.1 rearnsha
2583 1.1 rearnsha /*
2584 1.1 rearnsha * Console kernel output character routine.
2585 1.1 rearnsha */
2586 1.1 rearnsha void
2587 1.1 rearnsha plcomcnputc(dev_t dev, int c)
2588 1.1 rearnsha {
2589 1.40 skrll plcom_common_putc(dev, &plcomcons_info, c);
2590 1.1 rearnsha }
2591 1.1 rearnsha
2592 1.1 rearnsha void
2593 1.1 rearnsha plcomcnpollc(dev_t dev, int on)
2594 1.1 rearnsha {
2595 1.1 rearnsha
2596 1.45 mlelstv plcom_readaheadcount = 0;
2597 1.1 rearnsha }
2598 1.1 rearnsha
2599 1.59 jmcneill void
2600 1.59 jmcneill plcomcnhalt(dev_t dev)
2601 1.59 jmcneill {
2602 1.59 jmcneill struct plcom_instance *pi = &plcomcons_info;
2603 1.59 jmcneill
2604 1.59 jmcneill switch (pi->pi_type) {
2605 1.59 jmcneill case PLCOM_TYPE_PL010:
2606 1.59 jmcneill PWRITE1(pi, PL010COM_CR, PL01X_CR_UARTEN);
2607 1.59 jmcneill break;
2608 1.59 jmcneill case PLCOM_TYPE_PL011:
2609 1.67 mlelstv case PLCOM_TYPE_GENERIC_UART:
2610 1.67 mlelstv PWRITE2(pi, PL011COM_CR,
2611 1.59 jmcneill PL01X_CR_UARTEN | PL011_CR_RXE | PL011_CR_TXE);
2612 1.59 jmcneill PWRITE4(pi, PL011COM_IMSC, 0);
2613 1.59 jmcneill break;
2614 1.59 jmcneill }
2615 1.59 jmcneill }
2616 1.59 jmcneill
2617 1.1 rearnsha #ifdef KGDB
2618 1.1 rearnsha int
2619 1.40 skrll plcom_kgdb_attach(struct plcom_instance *pi, int rate, int frequency,
2620 1.40 skrll tcflag_t cflag, int unit)
2621 1.1 rearnsha {
2622 1.1 rearnsha int res;
2623 1.1 rearnsha
2624 1.40 skrll if (pi->pi_iot == plcomcons_info.pi_iot &&
2625 1.40 skrll pi->pi_iobase == plcomcons_info.pi_iobase)
2626 1.1 rearnsha return EBUSY; /* cannot share with console */
2627 1.1 rearnsha
2628 1.40 skrll res = plcominit(pi, rate, frequency, cflag);
2629 1.1 rearnsha if (res)
2630 1.1 rearnsha return res;
2631 1.1 rearnsha
2632 1.1 rearnsha kgdb_attach(plcom_kgdb_getc, plcom_kgdb_putc, NULL);
2633 1.1 rearnsha kgdb_dev = 123; /* unneeded, only to satisfy some tests */
2634 1.1 rearnsha
2635 1.40 skrll plcomkgdb_info.pi_iot = pi->pi_iot;
2636 1.40 skrll plcomkgdb_info.pi_ioh = pi->pi_ioh;
2637 1.40 skrll plcomkgdb_info.pi_iobase = pi->pi_iobase;
2638 1.1 rearnsha
2639 1.1 rearnsha return 0;
2640 1.1 rearnsha }
2641 1.1 rearnsha
2642 1.1 rearnsha /* ARGSUSED */
2643 1.1 rearnsha int
2644 1.1 rearnsha plcom_kgdb_getc(void *arg)
2645 1.1 rearnsha {
2646 1.44 mlelstv return plcom_common_getc(NODEV, &plcomkgdb_info);
2647 1.1 rearnsha }
2648 1.1 rearnsha
2649 1.1 rearnsha /* ARGSUSED */
2650 1.1 rearnsha void
2651 1.1 rearnsha plcom_kgdb_putc(void *arg, int c)
2652 1.1 rearnsha {
2653 1.44 mlelstv plcom_common_putc(NODEV, &plcomkgdb_info, c);
2654 1.1 rearnsha }
2655 1.1 rearnsha #endif /* KGDB */
2656 1.1 rearnsha
2657 1.1 rearnsha /* helper function to identify the plcom ports used by
2658 1.1 rearnsha console or KGDB (and not yet autoconf attached) */
2659 1.1 rearnsha int
2660 1.46 skrll plcom_is_console(bus_space_tag_t iot, bus_addr_t iobase,
2661 1.1 rearnsha bus_space_handle_t *ioh)
2662 1.1 rearnsha {
2663 1.1 rearnsha bus_space_handle_t help;
2664 1.1 rearnsha
2665 1.1 rearnsha if (!plcomconsattached &&
2666 1.40 skrll bus_space_is_equal(iot, plcomcons_info.pi_iot) &&
2667 1.40 skrll iobase == plcomcons_info.pi_iobase)
2668 1.40 skrll help = plcomcons_info.pi_ioh;
2669 1.1 rearnsha #ifdef KGDB
2670 1.1 rearnsha else if (!plcom_kgdb_attached &&
2671 1.40 skrll bus_space_is_equal(iot, plcomkgdb_info.pi_iot) &&
2672 1.57 skrll iobase == plcomkgdb_info.pi_iobase)
2673 1.44 mlelstv help = plcomkgdb_info.pi_ioh;
2674 1.1 rearnsha #endif
2675 1.1 rearnsha else
2676 1.1 rearnsha return 0;
2677 1.1 rearnsha
2678 1.1 rearnsha if (ioh)
2679 1.1 rearnsha *ioh = help;
2680 1.1 rearnsha return 1;
2681 1.1 rearnsha }
2682