imxuart.c revision 1.26 1 /* $NetBSD: imxuart.c,v 1.26 2020/11/20 18:16:40 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 2009, 2010 Genetec Corporation. All rights reserved.
5 * Written by Hiroyuki Bessho for Genetec Corporation.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENETEC CORPORATION
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29
30 /*
31 * derived from sys/dev/ic/com.c
32 */
33
34 /*-
35 * Copyright (c) 1998, 1999, 2004, 2008 The NetBSD Foundation, Inc.
36 * All rights reserved.
37 *
38 * This code is derived from software contributed to The NetBSD Foundation
39 * by Charles M. Hannum.
40 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
51 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
52 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
53 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
54 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
55 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
56 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
57 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
58 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
59 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
60 * POSSIBILITY OF SUCH DAMAGE.
61 */
62
63 /*
64 * Copyright (c) 1991 The Regents of the University of California.
65 * All rights reserved.
66 *
67 * Redistribution and use in source and binary forms, with or without
68 * modification, are permitted provided that the following conditions
69 * are met:
70 * 1. Redistributions of source code must retain the above copyright
71 * notice, this list of conditions and the following disclaimer.
72 * 2. Redistributions in binary form must reproduce the above copyright
73 * notice, this list of conditions and the following disclaimer in the
74 * documentation and/or other materials provided with the distribution.
75 * 3. Neither the name of the University nor the names of its contributors
76 * may be used to endorse or promote products derived from this software
77 * without specific prior written permission.
78 *
79 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
80 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
81 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
82 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
83 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
84 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
85 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
86 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
87 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
88 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
89 * SUCH DAMAGE.
90 *
91 * @(#)com.c 7.5 (Berkeley) 5/16/91
92 */
93
94 /*
95 * driver for UART in i.MX SoC.
96 */
97
98 #include <sys/cdefs.h>
99 __KERNEL_RCSID(0, "$NetBSD: imxuart.c,v 1.26 2020/11/20 18:16:40 thorpej Exp $");
100
101 #include "opt_imxuart.h"
102 #include "opt_ddb.h"
103 #include "opt_ddbparam.h"
104 #include "opt_kgdb.h"
105 #include "opt_lockdebug.h"
106 #include "opt_multiprocessor.h"
107 #include "opt_ntp.h"
108 #include "opt_imxuart.h"
109
110 #ifdef RND_COM
111 #include <sys/rndsource.h>
112 #endif
113
114 #ifndef IMXUART_TOLERANCE
115 #define IMXUART_TOLERANCE 30 /* baud rate tolerance, in 0.1% units */
116 #endif
117
118 #ifndef IMXUART_FREQDIV
119 #define IMXUART_FREQDIV 2 /* XXX */
120 #endif
121
122 #ifndef IMXUART_FREQ
123 #define IMXUART_FREQ (56900000)
124 #endif
125
126 /*
127 * Override cnmagic(9) macro before including <sys/systm.h>.
128 * We need to know if cn_check_magic triggered debugger, so set a flag.
129 * Callers of cn_check_magic must declare int cn_trapped = 0;
130 * XXX: this is *ugly*!
131 */
132 #define cn_trap() \
133 do { \
134 console_debugger(); \
135 cn_trapped = 1; \
136 } while (/* CONSTCOND */ 0)
137
138 #include <sys/param.h>
139 #include <sys/systm.h>
140 #include <sys/ioctl.h>
141 #include <sys/select.h>
142 #include <sys/poll.h>
143 #include <sys/tty.h>
144 #include <sys/proc.h>
145 #include <sys/conf.h>
146 #include <sys/file.h>
147 #include <sys/uio.h>
148 #include <sys/kernel.h>
149 #include <sys/syslog.h>
150 #include <sys/device.h>
151 #include <sys/kmem.h>
152 #include <sys/timepps.h>
153 #include <sys/vnode.h>
154 #include <sys/kauth.h>
155 #include <sys/intr.h>
156
157 #include <sys/bus.h>
158
159 #include <arm/imx/imxuartreg.h>
160 #include <arm/imx/imxuartvar.h>
161 #include <dev/cons.h>
162
163 #ifndef IMXUART_RING_SIZE
164 #define IMXUART_RING_SIZE 2048
165 #endif
166
167 int imxuspeed(long, struct imxuart_baudrate_ratio *);
168 int imxuparam(struct tty *, struct termios *);
169 void imxustart(struct tty *);
170 int imxuhwiflow(struct tty *, int);
171
172 void imxuart_shutdown(struct imxuart_softc *);
173 void imxuart_loadchannelregs(struct imxuart_softc *);
174 void imxuart_hwiflow(struct imxuart_softc *);
175 void imxuart_break(struct imxuart_softc *, bool);
176 void imxuart_modem(struct imxuart_softc *, int);
177 void tiocm_to_imxu(struct imxuart_softc *, u_long, int);
178 int imxuart_to_tiocm(struct imxuart_softc *);
179 void imxuart_iflush(struct imxuart_softc *);
180 int imxuintr(void *);
181
182 int imxuart_common_getc(dev_t, struct imxuart_regs *);
183 void imxuart_common_putc(dev_t, struct imxuart_regs *, int);
184
185
186 int imxuart_init(struct imxuart_regs *, int, tcflag_t, int);
187
188 int imxucngetc(dev_t);
189 void imxucnputc(dev_t, int);
190 void imxucnpollc(dev_t, int);
191
192 static void imxuintr_read(struct imxuart_softc *);
193 static void imxuintr_send(struct imxuart_softc *);
194
195 static void imxuart_enable_debugport(struct imxuart_softc *);
196 static void imxuart_disable_all_interrupts(struct imxuart_softc *);
197 static void imxuart_control_rxint(struct imxuart_softc *, bool);
198 static void imxuart_control_txint(struct imxuart_softc *, bool);
199 static u_int imxuart_txfifo_space(struct imxuart_softc *sc);
200
201 static uint32_t cflag_to_ucr2(tcflag_t, uint32_t);
202
203 #define integrate static inline
204 void imxusoft(void *);
205 integrate void imxuart_rxsoft(struct imxuart_softc *, struct tty *);
206 integrate void imxuart_txsoft(struct imxuart_softc *, struct tty *);
207 integrate void imxuart_stsoft(struct imxuart_softc *, struct tty *);
208 integrate void imxuart_schedrx(struct imxuart_softc *);
209 void imxudiag(void *);
210 static void imxuart_load_speed(struct imxuart_softc *);
211 static void imxuart_load_params(struct imxuart_softc *);
212 integrate void imxuart_load_pendings(struct imxuart_softc *);
213
214
215 extern struct cfdriver imxuart_cd;
216
217 dev_type_open(imxuopen);
218 dev_type_close(imxuclose);
219 dev_type_read(imxuread);
220 dev_type_write(imxuwrite);
221 dev_type_ioctl(imxuioctl);
222 dev_type_stop(imxustop);
223 dev_type_tty(imxutty);
224 dev_type_poll(imxupoll);
225
226 const struct cdevsw imxcom_cdevsw = {
227 .d_open = imxuopen,
228 .d_close = imxuclose,
229 .d_read = imxuread,
230 .d_write = imxuwrite,
231 .d_ioctl = imxuioctl,
232 .d_stop = imxustop,
233 .d_tty = imxutty,
234 .d_poll = imxupoll,
235 .d_mmap = nommap,
236 .d_kqfilter = ttykqfilter,
237 .d_discard = nodiscard,
238 .d_flag = D_TTY
239 };
240
241 /*
242 * Make this an option variable one can patch.
243 * But be warned: this must be a power of 2!
244 */
245 u_int imxuart_rbuf_size = IMXUART_RING_SIZE;
246
247 /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
248 u_int imxuart_rbuf_hiwat = (IMXUART_RING_SIZE * 1) / 4;
249 u_int imxuart_rbuf_lowat = (IMXUART_RING_SIZE * 3) / 4;
250
251 static struct imxuart_regs imxuconsregs;
252 static int imxuconsattached;
253 static int imxuconsrate;
254 static tcflag_t imxuconscflag;
255 static struct cnm_state imxuart_cnm_state;
256
257 u_int imxuart_freq = IMXUART_FREQ;
258 u_int imxuart_freqdiv = IMXUART_FREQDIV;
259
260 #ifdef KGDB
261 #include <sys/kgdb.h>
262
263 static struct imxuart_regs imxu_kgdb_regs;
264 static int imxu_kgdb_attached;
265
266 int imxuart_kgdb_getc(void *);
267 void imxuart_kgdb_putc(void *, int);
268 #endif /* KGDB */
269
270 #define IMXUART_DIALOUT_MASK TTDIALOUT_MASK
271
272 #define IMXUART_UNIT(x) TTUNIT(x)
273 #define IMXUART_DIALOUT(x) TTDIALOUT(x)
274
275 #define IMXUART_ISALIVE(sc) ((sc)->enabled != 0 && \
276 device_is_active((sc)->sc_dev))
277
278 #define BR BUS_SPACE_BARRIER_READ
279 #define BW BUS_SPACE_BARRIER_WRITE
280 #define IMXUART_BARRIER(r, f) \
281 bus_space_barrier((r)->ur_iot, (r)->ur_ioh, 0, IMX_UART_SIZE, (f))
282
283
284 void
285 imxuart_attach_common(device_t parent, device_t self,
286 bus_space_tag_t iot, paddr_t iobase, size_t size, int intr, int flags)
287 {
288 struct imxuart_softc *sc = device_private(self);
289 struct imxuart_regs *regsp = &sc->sc_regs;
290 bus_space_handle_t ioh;
291
292 aprint_naive("\n");
293 aprint_normal("\n");
294
295 sc->sc_dev = self;
296
297 if (size <= 0)
298 size = IMX_UART_SIZE;
299
300 sc->sc_intr = intr;
301 regsp->ur_iot = iot;
302 regsp->ur_iobase = iobase;
303
304 if (bus_space_map(iot, regsp->ur_iobase, size, 0, &ioh)) {
305 return;
306 }
307 regsp->ur_ioh = ioh;
308
309 sc->sc_ih = intr_establish(sc->sc_intr, IPL_SERIAL, IST_LEVEL,
310 imxuintr, sc);
311 if (sc->sc_ih == NULL) {
312 aprint_error_dev(sc->sc_dev, "intr_establish failed\n");
313 return;
314 }
315
316 imxuart_attach_subr(sc);
317 }
318
319 void
320 imxuart_attach_subr(struct imxuart_softc *sc)
321 {
322 struct imxuart_regs *regsp = &sc->sc_regs;
323 bus_space_tag_t iot = regsp->ur_iot;
324 bus_space_handle_t ioh = regsp->ur_ioh;
325 struct tty *tp;
326
327 callout_init(&sc->sc_diag_callout, 0);
328 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_HIGH);
329
330 if (regsp->ur_iobase != imxuconsregs.ur_iobase)
331 imxuart_init(&sc->sc_regs, TTYDEF_SPEED, TTYDEF_CFLAG, false);
332
333 bus_space_read_region_4(iot, ioh, IMX_UCR1, sc->sc_ucr, 4);
334 sc->sc_ucr2_d = sc->sc_ucr2;
335
336 /* Disable interrupts before configuring the device. */
337 imxuart_disable_all_interrupts(sc);
338
339 if (regsp->ur_iobase == imxuconsregs.ur_iobase) {
340 imxuconsattached = 1;
341
342 /* Make sure the console is always "hardwired". */
343 #if 0
344 delay(10000); /* wait for output to finish */
345 #endif
346 SET(sc->sc_hwflags, IMXUART_HW_CONSOLE);
347 SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
348 }
349
350
351 tp = tty_alloc();
352 tp->t_oproc = imxustart;
353 tp->t_param = imxuparam;
354 tp->t_hwiflow = imxuhwiflow;
355
356 sc->sc_tty = tp;
357 sc->sc_rbuf = kmem_alloc(sizeof (*sc->sc_rbuf) * imxuart_rbuf_size,
358 KM_SLEEP);
359 sc->sc_rbuf_size = imxuart_rbuf_size;
360 sc->sc_rbuf_in = sc->sc_rbuf_out = 0;
361 sc->sc_txfifo_len = 32;
362 sc->sc_txfifo_thresh = 16; /* when USR1.TRDY, fifo has space
363 * for this many characters */
364
365 tty_attach(tp);
366
367 if (ISSET(sc->sc_hwflags, IMXUART_HW_CONSOLE)) {
368 int maj;
369
370 /* locate the major number */
371 maj = cdevsw_lookup_major(&imxcom_cdevsw);
372
373 if (maj != NODEVMAJOR) {
374 tp->t_dev = cn_tab->cn_dev = makedev(maj,
375 device_unit(sc->sc_dev));
376
377 aprint_normal_dev(sc->sc_dev, "console\n");
378 }
379 }
380
381 #ifdef KGDB
382 /*
383 * Allow kgdb to "take over" this port. If this is
384 * not the console and is the kgdb device, it has
385 * exclusive use. If it's the console _and_ the
386 * kgdb device, it doesn't.
387 */
388 if (regsp->ur_iobase == imxu_kgdb_regs.ur_iobase) {
389 if (!ISSET(sc->sc_hwflags, IMXUART_HW_CONSOLE)) {
390 imxu_kgdb_attached = 1;
391
392 SET(sc->sc_hwflags, IMXUART_HW_KGDB);
393 }
394 aprint_normal_dev(sc->sc_dev, "kgdb\n");
395 }
396 #endif
397
398 sc->sc_si = softint_establish(SOFTINT_SERIAL, imxusoft, sc);
399
400 #ifdef RND_COM
401 rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
402 RND_TYPE_TTY, RND_FLAG_COLLECT_TIME |
403 RND_FLAG_ESTIMATE_TIME);
404 #endif
405
406 /* if there are no enable/disable functions, assume the device
407 is always enabled */
408 if (!sc->enable)
409 sc->enabled = 1;
410
411 imxuart_enable_debugport(sc);
412
413 SET(sc->sc_hwflags, IMXUART_HW_DEV_OK);
414
415 //shutdownhook_establish(imxuart_shutdownhook, sc);
416
417
418 #if 0
419 {
420 uint32_t reg;
421 reg = bus_space_read_4(iot, ioh, IMX_UCR1);
422 reg |= IMX_UCR1_TXDMAEN | IMX_UCR1_RXDMAEN;
423 bus_space_write_4(iot, ioh, IMX_UCR1, reg);
424 }
425 #endif
426 }
427
428 /*
429 * baudrate = RefFreq / (16 * (UMBR + 1)/(UBIR + 1))
430 *
431 * (UBIR + 1) / (UBMR + 1) = (16 * BaurdRate) / RefFreq
432 */
433
434 static long
435 gcd(long m, long n)
436 {
437
438 if (m < n)
439 return gcd(n, m);
440
441 if (n <= 0)
442 return m;
443 return gcd(n, m % n);
444 }
445
446 int
447 imxuspeed(long speed, struct imxuart_baudrate_ratio *ratio)
448 {
449 #define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */
450 long b = 16 * speed;
451 long f = imxuart_freq / imxuart_freqdiv;
452 long d;
453 int err = 0;
454
455 /* reduce b/f */
456 while ((f > (1<<16) || b > (1<<16)) && (d = gcd(f, b)) > 1) {
457 f /= d;
458 b /= d;
459 }
460
461
462 while (f > (1<<16) || b > (1<<16)) {
463 f /= 2;
464 b /= 2;
465 }
466 if (f <= 0 || b <= 0)
467 return -1;
468
469 #ifdef DIAGNOSTIC
470 err = divrnd(((uint64_t)imxuart_freq) * 1000 / imxuart_freqdiv,
471 (uint64_t)speed * 16 * f / b) - 1000;
472 if (err < 0)
473 err = -err;
474 #endif
475
476 ratio->numerator = b-1;
477 ratio->modulator = f-1;
478
479 if (err > IMXUART_TOLERANCE)
480 return -1;
481
482 return 0;
483 #undef divrnd
484 }
485
486 #ifdef IMXUART_DEBUG
487 int imxuart_debug = 0;
488
489 void imxustatus(struct imxuart_softc *, const char *);
490 void
491 imxustatus(struct imxuart_softc *sc, const char *str)
492 {
493 struct tty *tp = sc->sc_tty;
494
495 aprint_normal_dev(sc->sc_dev,
496 "%s %cclocal %cdcd %cts_carr_on %cdtr %ctx_stopped\n",
497 str,
498 ISSET(tp->t_cflag, CLOCAL) ? '+' : '-',
499 ISSET(sc->sc_msr, MSR_DCD) ? '+' : '-',
500 ISSET(tp->t_state, TS_CARR_ON) ? '+' : '-',
501 ISSET(sc->sc_mcr, MCR_DTR) ? '+' : '-',
502 sc->sc_tx_stopped ? '+' : '-');
503
504 aprint_normal_dev(sc->sc_dev,
505 "%s %ccrtscts %ccts %cts_ttstop %crts rx_flags=0x%x\n",
506 str,
507 ISSET(tp->t_cflag, CRTSCTS) ? '+' : '-',
508 ISSET(sc->sc_msr, MSR_CTS) ? '+' : '-',
509 ISSET(tp->t_state, TS_TTSTOP) ? '+' : '-',
510 ISSET(sc->sc_mcr, MCR_RTS) ? '+' : '-',
511 sc->sc_rx_flags);
512 }
513 #endif
514
515 #if 0
516 int
517 imxuart_detach(device_t self, int flags)
518 {
519 struct imxuart_softc *sc = device_private(self);
520 int maj, mn;
521
522 if (ISSET(sc->sc_hwflags, IMXUART_HW_CONSOLE))
523 return EBUSY;
524
525 /* locate the major number */
526 maj = cdevsw_lookup_major(&imxcom_cdevsw);
527
528 /* Nuke the vnodes for any open instances. */
529 mn = device_unit(self);
530 vdevgone(maj, mn, mn, VCHR);
531
532 mn |= IMXUART_DIALOUT_MASK;
533 vdevgone(maj, mn, mn, VCHR);
534
535 if (sc->sc_rbuf == NULL) {
536 /*
537 * Ring buffer allocation failed in the imxuart_attach_subr,
538 * only the tty is allocated, and nothing else.
539 */
540 tty_free(sc->sc_tty);
541 return 0;
542 }
543
544 /* Free the receive buffer. */
545 kmem_free(sc->sc_rbuf, sizeof(*sc->sc_rbuf) * sc->sc_rbuf_size);
546
547 /* Detach and free the tty. */
548 tty_detach(sc->sc_tty);
549 tty_free(sc->sc_tty);
550
551 /* Unhook the soft interrupt handler. */
552 softint_disestablish(sc->sc_si);
553
554 #ifdef RND_COM
555 /* Unhook the entropy source. */
556 rnd_detach_source(&sc->rnd_source);
557 #endif
558 callout_destroy(&sc->sc_diag_callout);
559
560 /* Destroy the lock. */
561 mutex_destroy(&sc->sc_lock);
562
563 return (0);
564 }
565 #endif
566
567 #ifdef notyet
568 int
569 imxuart_activate(device_t self, enum devact act)
570 {
571 struct imxuart_softc *sc = device_private(self);
572 int rv = 0;
573
574 switch (act) {
575 case DVACT_ACTIVATE:
576 rv = EOPNOTSUPP;
577 break;
578
579 case DVACT_DEACTIVATE:
580 if (sc->sc_hwflags & (IMXUART_HW_CONSOLE|IMXUART_HW_KGDB)) {
581 rv = EBUSY;
582 break;
583 }
584
585 if (sc->disable != NULL && sc->enabled != 0) {
586 (*sc->disable)(sc);
587 sc->enabled = 0;
588 }
589 break;
590 }
591
592 return (rv);
593 }
594 #endif
595
596 void
597 imxuart_shutdown(struct imxuart_softc *sc)
598 {
599 struct tty *tp = sc->sc_tty;
600
601 mutex_spin_enter(&sc->sc_lock);
602
603 /* If we were asserting flow control, then deassert it. */
604 SET(sc->sc_rx_flags, IMXUART_RX_IBUF_BLOCKED);
605 imxuart_hwiflow(sc);
606
607 /* Clear any break condition set with TIOCSBRK. */
608 imxuart_break(sc, false);
609
610 /*
611 * Hang up if necessary. Wait a bit, so the other side has time to
612 * notice even if we immediately open the port again.
613 * Avoid tsleeping above splhigh().
614 */
615 if (ISSET(tp->t_cflag, HUPCL)) {
616 imxuart_modem(sc, 0);
617 mutex_spin_exit(&sc->sc_lock);
618 /* XXX will only timeout */
619 (void) kpause(ttclos, false, hz, NULL);
620 mutex_spin_enter(&sc->sc_lock);
621 }
622
623 /* Turn off interrupts. */
624 imxuart_disable_all_interrupts(sc);
625 /* re-enable recv interrupt for console or kgdb port */
626 imxuart_enable_debugport(sc);
627
628 mutex_spin_exit(&sc->sc_lock);
629
630 #ifdef notyet
631 if (sc->disable) {
632 #ifdef DIAGNOSTIC
633 if (!sc->enabled)
634 panic("imxuart_shutdown: not enabled?");
635 #endif
636 (*sc->disable)(sc);
637 sc->enabled = 0;
638 }
639 #endif
640 }
641
642 int
643 imxuopen(dev_t dev, int flag, int mode, struct lwp *l)
644 {
645 struct imxuart_softc *sc;
646 struct tty *tp;
647 int s;
648 int error;
649
650 sc = device_lookup_private(&imxuart_cd, IMXUART_UNIT(dev));
651 if (sc == NULL || !ISSET(sc->sc_hwflags, IMXUART_HW_DEV_OK) ||
652 sc->sc_rbuf == NULL)
653 return (ENXIO);
654
655 if (!device_is_active(sc->sc_dev))
656 return (ENXIO);
657
658 #ifdef KGDB
659 /*
660 * If this is the kgdb port, no other use is permitted.
661 */
662 if (ISSET(sc->sc_hwflags, IMXUART_HW_KGDB))
663 return (EBUSY);
664 #endif
665
666 tp = sc->sc_tty;
667
668 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
669 return (EBUSY);
670
671 s = spltty();
672
673 /*
674 * Do the following iff this is a first open.
675 */
676 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
677 struct termios t;
678
679 tp->t_dev = dev;
680
681
682 #ifdef notyet
683 if (sc->enable) {
684 if ((*sc->enable)(sc)) {
685 splx(s);
686 aprint_error_dev(sc->sc_dev,
687 "device enable failed\n");
688 return (EIO);
689 }
690 sc->enabled = 1;
691 }
692 #endif
693
694 mutex_spin_enter(&sc->sc_lock);
695
696 imxuart_disable_all_interrupts(sc);
697
698 /* Fetch the current modem control status, needed later. */
699
700 #ifdef IMXUART_PPS
701 /* Clear PPS capture state on first open. */
702 mutex_spin_enter(&timecounter_lock);
703 memset(&sc->sc_pps_state, 0, sizeof(sc->sc_pps_state));
704 sc->sc_pps_state.ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR;
705 pps_init(&sc->sc_pps_state);
706 mutex_spin_exit(&timecounter_lock);
707 #endif
708
709 mutex_spin_exit(&sc->sc_lock);
710
711 /*
712 * Initialize the termios status to the defaults. Add in the
713 * sticky bits from TIOCSFLAGS.
714 */
715 if (ISSET(sc->sc_hwflags, IMXUART_HW_CONSOLE)) {
716 t.c_ospeed = imxuconsrate;
717 t.c_cflag = imxuconscflag;
718 } else {
719 t.c_ospeed = TTYDEF_SPEED;
720 t.c_cflag = TTYDEF_CFLAG;
721 }
722 t.c_ispeed = t.c_ospeed;
723 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
724 SET(t.c_cflag, CLOCAL);
725 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
726 SET(t.c_cflag, CRTSCTS);
727 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
728 SET(t.c_cflag, MDMBUF);
729 /* Make sure imxuparam() will do something. */
730 tp->t_ospeed = 0;
731 (void) imxuparam(tp, &t);
732 tp->t_iflag = TTYDEF_IFLAG;
733 tp->t_oflag = TTYDEF_OFLAG;
734 tp->t_lflag = TTYDEF_LFLAG;
735 ttychars(tp);
736 ttsetwater(tp);
737
738 mutex_spin_enter(&sc->sc_lock);
739
740 /*
741 * Turn on DTR. We must always do this, even if carrier is not
742 * present, because otherwise we'd have to use TIOCSDTR
743 * immediately after setting CLOCAL, which applications do not
744 * expect. We always assert DTR while the device is open
745 * unless explicitly requested to deassert it.
746 */
747 imxuart_modem(sc, 1);
748
749 /* Clear the input ring, and unblock. */
750 sc->sc_rbuf_in = sc->sc_rbuf_out = 0;
751 imxuart_iflush(sc);
752 CLR(sc->sc_rx_flags, IMXUART_RX_ANY_BLOCK);
753 imxuart_hwiflow(sc);
754
755 /* Turn on interrupts. */
756 imxuart_control_rxint(sc, true);
757
758 #ifdef IMXUART_DEBUG
759 if (imxuart_debug)
760 imxustatus(sc, "imxuopen ");
761 #endif
762
763 mutex_spin_exit(&sc->sc_lock);
764 }
765
766 splx(s);
767
768 #if 0
769 error = ttyopen(tp, IMXUART_DIALOUT(dev), ISSET(flag, O_NONBLOCK));
770 #else
771 error = ttyopen(tp, 1, ISSET(flag, O_NONBLOCK));
772 #endif
773 if (error)
774 goto bad;
775
776 error = (*tp->t_linesw->l_open)(dev, tp);
777 if (error)
778 goto bad;
779
780 return (0);
781
782 bad:
783 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
784 /*
785 * We failed to open the device, and nobody else had it opened.
786 * Clean up the state as appropriate.
787 */
788 imxuart_shutdown(sc);
789 }
790
791 return (error);
792 }
793
794 int
795 imxuclose(dev_t dev, int flag, int mode, struct lwp *l)
796 {
797 struct imxuart_softc *sc =
798 device_lookup_private(&imxuart_cd, IMXUART_UNIT(dev));
799 struct tty *tp = sc->sc_tty;
800
801 /* XXX This is for cons.c. */
802 if (!ISSET(tp->t_state, TS_ISOPEN))
803 return (0);
804
805 (*tp->t_linesw->l_close)(tp, flag);
806 ttyclose(tp);
807
808 if (IMXUART_ISALIVE(sc) == 0)
809 return (0);
810
811 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
812 /*
813 * Although we got a last close, the device may still be in
814 * use; e.g. if this was the dialout node, and there are still
815 * processes waiting for carrier on the non-dialout node.
816 */
817 imxuart_shutdown(sc);
818 }
819
820 return (0);
821 }
822
823 int
824 imxuread(dev_t dev, struct uio *uio, int flag)
825 {
826 struct imxuart_softc *sc =
827 device_lookup_private(&imxuart_cd, IMXUART_UNIT(dev));
828 struct tty *tp = sc->sc_tty;
829
830 if (IMXUART_ISALIVE(sc) == 0)
831 return (EIO);
832
833 return ((*tp->t_linesw->l_read)(tp, uio, flag));
834 }
835
836 int
837 imxuwrite(dev_t dev, struct uio *uio, int flag)
838 {
839 struct imxuart_softc *sc =
840 device_lookup_private(&imxuart_cd, IMXUART_UNIT(dev));
841 struct tty *tp = sc->sc_tty;
842
843 if (IMXUART_ISALIVE(sc) == 0)
844 return (EIO);
845
846 return ((*tp->t_linesw->l_write)(tp, uio, flag));
847 }
848
849 int
850 imxupoll(dev_t dev, int events, struct lwp *l)
851 {
852 struct imxuart_softc *sc =
853 device_lookup_private(&imxuart_cd, IMXUART_UNIT(dev));
854 struct tty *tp = sc->sc_tty;
855
856 if (IMXUART_ISALIVE(sc) == 0)
857 return (POLLHUP);
858
859 return ((*tp->t_linesw->l_poll)(tp, events, l));
860 }
861
862 struct tty *
863 imxutty(dev_t dev)
864 {
865 struct imxuart_softc *sc =
866 device_lookup_private(&imxuart_cd, IMXUART_UNIT(dev));
867 struct tty *tp = sc->sc_tty;
868
869 return (tp);
870 }
871
872 int
873 imxuioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
874 {
875 struct imxuart_softc *sc;
876 struct tty *tp;
877 int error;
878
879 sc = device_lookup_private(&imxuart_cd, IMXUART_UNIT(dev));
880 if (sc == NULL)
881 return ENXIO;
882 if (IMXUART_ISALIVE(sc) == 0)
883 return (EIO);
884
885 tp = sc->sc_tty;
886
887 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
888 if (error != EPASSTHROUGH)
889 return (error);
890
891 error = ttioctl(tp, cmd, data, flag, l);
892 if (error != EPASSTHROUGH)
893 return (error);
894
895 error = 0;
896 switch (cmd) {
897 case TIOCSFLAGS:
898 error = kauth_authorize_device_tty(l->l_cred,
899 KAUTH_DEVICE_TTY_PRIVSET, tp);
900 break;
901 default:
902 /* nothing */
903 break;
904 }
905 if (error) {
906 return error;
907 }
908
909 mutex_spin_enter(&sc->sc_lock);
910
911 switch (cmd) {
912 case TIOCSBRK:
913 imxuart_break(sc, true);
914 break;
915
916 case TIOCCBRK:
917 imxuart_break(sc, false);
918 break;
919
920 case TIOCSDTR:
921 imxuart_modem(sc, 1);
922 break;
923
924 case TIOCCDTR:
925 imxuart_modem(sc, 0);
926 break;
927
928 case TIOCGFLAGS:
929 *(int *)data = sc->sc_swflags;
930 break;
931
932 case TIOCSFLAGS:
933 sc->sc_swflags = *(int *)data;
934 break;
935
936 case TIOCMSET:
937 case TIOCMBIS:
938 case TIOCMBIC:
939 tiocm_to_imxu(sc, cmd, *(int *)data);
940 break;
941
942 case TIOCMGET:
943 *(int *)data = imxuart_to_tiocm(sc);
944 break;
945
946 #ifdef notyet
947 case PPS_IOC_CREATE:
948 case PPS_IOC_DESTROY:
949 case PPS_IOC_GETPARAMS:
950 case PPS_IOC_SETPARAMS:
951 case PPS_IOC_GETCAP:
952 case PPS_IOC_FETCH:
953 #ifdef PPS_SYNC
954 case PPS_IOC_KCBIND:
955 #endif
956 mutex_spin_enter(&timecounter_lock);
957 error = pps_ioctl(cmd, data, &sc->sc_pps_state);
958 mutex_spin_exit(&timecounter_lock);
959 break;
960
961 case TIOCDCDTIMESTAMP: /* XXX old, overloaded API used by xntpd v3 */
962 mutex_spin_enter(&timecounter_lock);
963 #ifndef PPS_TRAILING_EDGE
964 TIMESPEC_TO_TIMEVAL((struct timeval *)data,
965 &sc->sc_pps_state.ppsinfo.assert_timestamp);
966 #else
967 TIMESPEC_TO_TIMEVAL((struct timeval *)data,
968 &sc->sc_pps_state.ppsinfo.clear_timestamp);
969 #endif
970 mutex_spin_exit(&timecounter_lock);
971 break;
972 #endif
973
974 default:
975 error = EPASSTHROUGH;
976 break;
977 }
978
979 mutex_spin_exit(&sc->sc_lock);
980
981 #ifdef IMXUART_DEBUG
982 if (imxuart_debug)
983 imxustatus(sc, "imxuioctl ");
984 #endif
985
986 return (error);
987 }
988
989 integrate void
990 imxuart_schedrx(struct imxuart_softc *sc)
991 {
992 sc->sc_rx_ready = 1;
993
994 /* Wake up the poller. */
995 softint_schedule(sc->sc_si);
996 }
997
998 void
999 imxuart_break(struct imxuart_softc *sc, bool onoff)
1000 {
1001 bus_space_tag_t iot = sc->sc_regs.ur_iot;
1002 bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
1003
1004 if (onoff)
1005 SET(sc->sc_ucr1, IMX_UCR1_SNDBRK);
1006 else
1007 CLR(sc->sc_ucr1, IMX_UCR1_SNDBRK);
1008
1009 bus_space_write_4(iot, ioh, IMX_UCR1, sc->sc_ucr1);
1010 }
1011
1012 void
1013 imxuart_modem(struct imxuart_softc *sc, int onoff)
1014 {
1015 #ifdef notyet
1016 if (sc->sc_mcr_dtr == 0)
1017 return;
1018
1019 if (onoff)
1020 SET(sc->sc_mcr, sc->sc_mcr_dtr);
1021 else
1022 CLR(sc->sc_mcr, sc->sc_mcr_dtr);
1023
1024 if (!sc->sc_heldchange) {
1025 if (sc->sc_tx_busy) {
1026 sc->sc_heldtbc = sc->sc_tbc;
1027 sc->sc_tbc = 0;
1028 sc->sc_heldchange = 1;
1029 } else
1030 imxuart_loadchannelregs(sc);
1031 }
1032 #endif
1033 }
1034
1035 /*
1036 * RTS output is controlled by UCR2.CTS bit.
1037 * DTR output is controlled by UCR3.DSR bit.
1038 * (i.MX reference manual uses names in DCE mode)
1039 *
1040 * note: if UCR2.CTSC == 1 for automatic HW flow control, UCR2.CTS is ignored.
1041 */
1042 void
1043 tiocm_to_imxu(struct imxuart_softc *sc, u_long how, int ttybits)
1044 {
1045 bus_space_tag_t iot = sc->sc_regs.ur_iot;
1046 bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
1047
1048 uint32_t ucr2 = sc->sc_ucr2_d;
1049 uint32_t ucr3 = sc->sc_ucr3;
1050
1051 uint32_t ucr2_mask = 0;
1052 uint32_t ucr3_mask = 0;
1053
1054
1055 if (ISSET(ttybits, TIOCM_DTR))
1056 ucr3_mask = IMX_UCR3_DSR;
1057 if (ISSET(ttybits, TIOCM_RTS))
1058 ucr2_mask = IMX_UCR2_CTS;
1059
1060 switch (how) {
1061 case TIOCMBIC:
1062 CLR(ucr2, ucr2_mask);
1063 CLR(ucr3, ucr3_mask);
1064 break;
1065
1066 case TIOCMBIS:
1067 SET(ucr2, ucr2_mask);
1068 SET(ucr3, ucr3_mask);
1069 break;
1070
1071 case TIOCMSET:
1072 CLR(ucr2, ucr2_mask);
1073 CLR(ucr3, ucr3_mask);
1074 SET(ucr2, ucr2_mask);
1075 SET(ucr3, ucr3_mask);
1076 break;
1077 }
1078
1079 if (ucr3 != sc->sc_ucr3) {
1080 bus_space_write_4(iot, ioh, IMX_UCR3, ucr3);
1081 sc->sc_ucr3 = ucr3;
1082 }
1083
1084 if (ucr2 == sc->sc_ucr2_d)
1085 return;
1086
1087 sc->sc_ucr2_d = ucr2;
1088 /* update CTS bit only */
1089 ucr2 = (sc->sc_ucr2 & ~IMX_UCR2_CTS) |
1090 (ucr2 & IMX_UCR2_CTS);
1091
1092 bus_space_write_4(iot, ioh, IMX_UCR2, ucr2);
1093 sc->sc_ucr2 = ucr2;
1094 }
1095
1096 int
1097 imxuart_to_tiocm(struct imxuart_softc *sc)
1098 {
1099 bus_space_tag_t iot = sc->sc_regs.ur_iot;
1100 bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
1101 int ttybits = 0;
1102 uint32_t usr[2];
1103
1104 if (ISSET(sc->sc_ucr3, IMX_UCR3_DSR))
1105 SET(ttybits, TIOCM_DTR);
1106 if (ISSET(sc->sc_ucr2, IMX_UCR2_CTS))
1107 SET(ttybits, TIOCM_RTS);
1108
1109 bus_space_read_region_4(iot, ioh, IMX_USR1, usr, 2);
1110
1111 if (ISSET(usr[0], IMX_USR1_RTSS))
1112 SET(ttybits, TIOCM_CTS);
1113
1114 if (ISSET(usr[1], IMX_USR2_DCDIN))
1115 SET(ttybits, TIOCM_CD);
1116
1117 #if 0
1118 /* XXXbsh: I couldn't find the way to read ipp_uart_dsr_dte_i signal,
1119 although there are bits in UART registers to detect delta of DSR.
1120 */
1121 if (ISSET(imxubits, MSR_DSR))
1122 SET(ttybits, TIOCM_DSR);
1123 #endif
1124
1125 if (ISSET(usr[1], IMX_USR2_RIIN))
1126 SET(ttybits, TIOCM_RI);
1127
1128
1129 #ifdef notyet
1130 if (ISSET(sc->sc_ier, IER_ERXRDY | IER_ETXRDY | IER_ERLS | IER_EMSC))
1131 SET(ttybits, TIOCM_LE);
1132 #endif
1133
1134 return (ttybits);
1135 }
1136
1137 static uint32_t
1138 cflag_to_ucr2(tcflag_t cflag, uint32_t oldval)
1139 {
1140 uint32_t val = oldval;
1141
1142 CLR(val,IMX_UCR2_WS|IMX_UCR2_PREN|IMX_UCR2_PROE|IMX_UCR2_STPB);
1143
1144 switch (cflag & CSIZE) {
1145 case CS5:
1146 case CS6:
1147 /* not suppreted. use 7-bits */
1148 case CS7:
1149 break;
1150 case CS8:
1151 SET(val, IMX_UCR2_WS);
1152 break;
1153 }
1154
1155
1156 if (ISSET(cflag, PARENB)) {
1157 SET(val, IMX_UCR2_PREN);
1158
1159 /* odd parity */
1160 if (!ISSET(cflag, PARODD))
1161 SET(val, IMX_UCR2_PROE);
1162 }
1163
1164 if (ISSET(cflag, CSTOPB))
1165 SET(val, IMX_UCR2_STPB);
1166
1167 val |= IMX_UCR2_TXEN| IMX_UCR2_RXEN|IMX_UCR2_SRST;
1168
1169 return val;
1170 }
1171
1172 int
1173 imxuparam(struct tty *tp, struct termios *t)
1174 {
1175 struct imxuart_softc *sc =
1176 device_lookup_private(&imxuart_cd, IMXUART_UNIT(tp->t_dev));
1177 struct imxuart_baudrate_ratio ratio;
1178 uint32_t ucr2;
1179 bool change_speed = tp->t_ospeed != t->c_ospeed;
1180
1181 if (IMXUART_ISALIVE(sc) == 0)
1182 return (EIO);
1183
1184 /* Check requested parameters. */
1185 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
1186 return (EINVAL);
1187
1188 /*
1189 * For the console, always force CLOCAL and !HUPCL, so that the port
1190 * is always active.
1191 */
1192 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
1193 ISSET(sc->sc_hwflags, IMXUART_HW_CONSOLE)) {
1194 SET(t->c_cflag, CLOCAL);
1195 CLR(t->c_cflag, HUPCL);
1196 }
1197
1198 /*
1199 * If there were no changes, don't do anything. This avoids dropping
1200 * input and improves performance when all we did was frob things like
1201 * VMIN and VTIME.
1202 */
1203 if ( !change_speed && tp->t_cflag == t->c_cflag)
1204 return (0);
1205
1206 if (change_speed) {
1207 /* calculate baudrate modulator value */
1208 if (imxuspeed(t->c_ospeed, &ratio) < 0)
1209 return (EINVAL);
1210 sc->sc_ratio = ratio;
1211 }
1212
1213 ucr2 = cflag_to_ucr2(t->c_cflag, sc->sc_ucr2_d);
1214
1215 mutex_spin_enter(&sc->sc_lock);
1216
1217 #if 0 /* flow control stuff. not yet */
1218 /*
1219 * If we're not in a mode that assumes a connection is present, then
1220 * ignore carrier changes.
1221 */
1222 if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
1223 sc->sc_msr_dcd = 0;
1224 else
1225 sc->sc_msr_dcd = MSR_DCD;
1226 /*
1227 * Set the flow control pins depending on the current flow control
1228 * mode.
1229 */
1230 if (ISSET(t->c_cflag, CRTSCTS)) {
1231 sc->sc_mcr_dtr = MCR_DTR;
1232 sc->sc_mcr_rts = MCR_RTS;
1233 sc->sc_msr_cts = MSR_CTS;
1234 sc->sc_efr = EFR_AUTORTS | EFR_AUTOCTS;
1235 } else if (ISSET(t->c_cflag, MDMBUF)) {
1236 /*
1237 * For DTR/DCD flow control, make sure we don't toggle DTR for
1238 * carrier detection.
1239 */
1240 sc->sc_mcr_dtr = 0;
1241 sc->sc_mcr_rts = MCR_DTR;
1242 sc->sc_msr_cts = MSR_DCD;
1243 sc->sc_efr = 0;
1244 } else {
1245 /*
1246 * If no flow control, then always set RTS. This will make
1247 * the other side happy if it mistakenly thinks we're doing
1248 * RTS/CTS flow control.
1249 */
1250 sc->sc_mcr_dtr = MCR_DTR | MCR_RTS;
1251 sc->sc_mcr_rts = 0;
1252 sc->sc_msr_cts = 0;
1253 sc->sc_efr = 0;
1254 if (ISSET(sc->sc_mcr, MCR_DTR))
1255 SET(sc->sc_mcr, MCR_RTS);
1256 else
1257 CLR(sc->sc_mcr, MCR_RTS);
1258 }
1259 sc->sc_msr_mask = sc->sc_msr_cts | sc->sc_msr_dcd;
1260 #endif
1261
1262 /* And copy to tty. */
1263 tp->t_ispeed = t->c_ospeed;
1264 tp->t_ospeed = t->c_ospeed;
1265 tp->t_cflag = t->c_cflag;
1266
1267 if (!change_speed && ucr2 == sc->sc_ucr2_d) {
1268 /* noop */
1269 }
1270 else if (!sc->sc_pending && !sc->sc_tx_busy) {
1271 if (ucr2 != sc->sc_ucr2_d) {
1272 sc->sc_ucr2_d = ucr2;
1273 imxuart_load_params(sc);
1274 }
1275 if (change_speed)
1276 imxuart_load_speed(sc);
1277 }
1278 else {
1279 if (!sc->sc_pending) {
1280 sc->sc_heldtbc = sc->sc_tbc;
1281 sc->sc_tbc = 0;
1282 }
1283 sc->sc_pending |=
1284 (ucr2 == sc->sc_ucr2_d ? 0 : IMXUART_PEND_PARAM) |
1285 (change_speed ? 0 : IMXUART_PEND_SPEED);
1286 sc->sc_ucr2_d = ucr2;
1287 }
1288
1289 if (!ISSET(t->c_cflag, CHWFLOW)) {
1290 /* Disable the high water mark. */
1291 sc->sc_r_hiwat = 0;
1292 sc->sc_r_lowat = 0;
1293 if (ISSET(sc->sc_rx_flags, IMXUART_RX_TTY_OVERFLOWED)) {
1294 CLR(sc->sc_rx_flags, IMXUART_RX_TTY_OVERFLOWED);
1295 imxuart_schedrx(sc);
1296 }
1297 if (ISSET(sc->sc_rx_flags,
1298 IMXUART_RX_TTY_BLOCKED|IMXUART_RX_IBUF_BLOCKED)) {
1299 CLR(sc->sc_rx_flags,
1300 IMXUART_RX_TTY_BLOCKED|IMXUART_RX_IBUF_BLOCKED);
1301 imxuart_hwiflow(sc);
1302 }
1303 } else {
1304 sc->sc_r_hiwat = imxuart_rbuf_hiwat;
1305 sc->sc_r_lowat = imxuart_rbuf_lowat;
1306 }
1307
1308 mutex_spin_exit(&sc->sc_lock);
1309
1310 #if 0
1311 /*
1312 * Update the tty layer's idea of the carrier bit, in case we changed
1313 * CLOCAL or MDMBUF. We don't hang up here; we only do that by
1314 * explicit request.
1315 */
1316 (void) (*tp->t_linesw->l_modem)(tp, ISSET(sc->sc_msr, MSR_DCD));
1317 #else
1318 /* XXX: always report that we have DCD */
1319 (void) (*tp->t_linesw->l_modem)(tp, 1);
1320 #endif
1321
1322 #ifdef IMXUART_DEBUG
1323 if (imxuart_debug)
1324 imxustatus(sc, "imxuparam ");
1325 #endif
1326
1327 if (!ISSET(t->c_cflag, CHWFLOW)) {
1328 if (sc->sc_tx_stopped) {
1329 sc->sc_tx_stopped = 0;
1330 imxustart(tp);
1331 }
1332 }
1333
1334 return (0);
1335 }
1336
1337 void
1338 imxuart_iflush(struct imxuart_softc *sc)
1339 {
1340 bus_space_tag_t iot = sc->sc_regs.ur_iot;
1341 bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
1342 #ifdef DIAGNOSTIC
1343 uint32_t reg = 0xffff;
1344 #endif
1345 int timo;
1346
1347 timo = 50000;
1348 /* flush any pending I/O */
1349 while (ISSET(bus_space_read_4(iot, ioh, IMX_USR2), IMX_USR2_RDR)
1350 && --timo)
1351 #ifdef DIAGNOSTIC
1352 reg =
1353 #else
1354 (void)
1355 #endif
1356 bus_space_read_4(iot, ioh, IMX_URXD);
1357 #ifdef DIAGNOSTIC
1358 if (!timo)
1359 aprint_error_dev(sc->sc_dev, "imxuart_iflush timeout %02x\n", reg);
1360 #endif
1361 }
1362
1363 int
1364 imxuhwiflow(struct tty *tp, int block)
1365 {
1366 struct imxuart_softc *sc =
1367 device_lookup_private(&imxuart_cd, IMXUART_UNIT(tp->t_dev));
1368
1369 if (IMXUART_ISALIVE(sc) == 0)
1370 return (0);
1371
1372 #ifdef notyet
1373 if (sc->sc_mcr_rts == 0)
1374 return (0);
1375 #endif
1376
1377 mutex_spin_enter(&sc->sc_lock);
1378
1379 if (block) {
1380 if (!ISSET(sc->sc_rx_flags, IMXUART_RX_TTY_BLOCKED)) {
1381 SET(sc->sc_rx_flags, IMXUART_RX_TTY_BLOCKED);
1382 imxuart_hwiflow(sc);
1383 }
1384 } else {
1385 if (ISSET(sc->sc_rx_flags, IMXUART_RX_TTY_OVERFLOWED)) {
1386 CLR(sc->sc_rx_flags, IMXUART_RX_TTY_OVERFLOWED);
1387 imxuart_schedrx(sc);
1388 }
1389 if (ISSET(sc->sc_rx_flags, IMXUART_RX_TTY_BLOCKED)) {
1390 CLR(sc->sc_rx_flags, IMXUART_RX_TTY_BLOCKED);
1391 imxuart_hwiflow(sc);
1392 }
1393 }
1394
1395 mutex_spin_exit(&sc->sc_lock);
1396 return (1);
1397 }
1398
1399 /*
1400 * (un)block input via hw flowcontrol
1401 */
1402 void
1403 imxuart_hwiflow(struct imxuart_softc *sc)
1404 {
1405 #ifdef notyet
1406 struct imxuart_regs *regsp= &sc->sc_regs;
1407
1408 if (sc->sc_mcr_rts == 0)
1409 return;
1410
1411 if (ISSET(sc->sc_rx_flags, RX_ANY_BLOCK)) {
1412 CLR(sc->sc_mcr, sc->sc_mcr_rts);
1413 CLR(sc->sc_mcr_active, sc->sc_mcr_rts);
1414 } else {
1415 SET(sc->sc_mcr, sc->sc_mcr_rts);
1416 SET(sc->sc_mcr_active, sc->sc_mcr_rts);
1417 }
1418 UR_WRITE_1(regsp, IMXUART_REG_MCR, sc->sc_mcr_active);
1419 #endif
1420 }
1421
1422
1423 void
1424 imxustart(struct tty *tp)
1425 {
1426 struct imxuart_softc *sc =
1427 device_lookup_private(&imxuart_cd, IMXUART_UNIT(tp->t_dev));
1428 int s;
1429 u_char *tba;
1430 int tbc;
1431 u_int n;
1432 u_int space;
1433 bus_space_tag_t iot = sc->sc_regs.ur_iot;
1434 bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
1435
1436 if (IMXUART_ISALIVE(sc) == 0)
1437 return;
1438
1439 s = spltty();
1440 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
1441 goto out;
1442 if (sc->sc_tx_stopped)
1443 goto out;
1444 if (!ttypull(tp))
1445 goto out;
1446
1447 /* Grab the first contiguous region of buffer space. */
1448 tba = tp->t_outq.c_cf;
1449 tbc = ndqb(&tp->t_outq, 0);
1450
1451 mutex_spin_enter(&sc->sc_lock);
1452
1453 sc->sc_tba = tba;
1454 sc->sc_tbc = tbc;
1455
1456 SET(tp->t_state, TS_BUSY);
1457 sc->sc_tx_busy = 1;
1458
1459 space = imxuart_txfifo_space(sc);
1460 n = MIN(sc->sc_tbc, space);
1461
1462 if (n > 0) {
1463 bus_space_write_multi_1(iot, ioh, IMX_UTXD, sc->sc_tba, n);
1464 sc->sc_tbc -= n;
1465 sc->sc_tba += n;
1466 }
1467
1468 /* Enable transmit completion interrupts */
1469 imxuart_control_txint(sc, true);
1470
1471 mutex_spin_exit(&sc->sc_lock);
1472 out:
1473 splx(s);
1474 return;
1475 }
1476
1477 /*
1478 * Stop output on a line.
1479 */
1480 void
1481 imxustop(struct tty *tp, int flag)
1482 {
1483 struct imxuart_softc *sc =
1484 device_lookup_private(&imxuart_cd, IMXUART_UNIT(tp->t_dev));
1485
1486 mutex_spin_enter(&sc->sc_lock);
1487 if (ISSET(tp->t_state, TS_BUSY)) {
1488 /* Stop transmitting at the next chunk. */
1489 sc->sc_tbc = 0;
1490 sc->sc_heldtbc = 0;
1491 if (!ISSET(tp->t_state, TS_TTSTOP))
1492 SET(tp->t_state, TS_FLUSH);
1493 }
1494 mutex_spin_exit(&sc->sc_lock);
1495 }
1496
1497 void
1498 imxudiag(void *arg)
1499 {
1500 #ifdef notyet
1501 struct imxuart_softc *sc = arg;
1502 int overflows, floods;
1503
1504 mutex_spin_enter(&sc->sc_lock);
1505 overflows = sc->sc_overflows;
1506 sc->sc_overflows = 0;
1507 floods = sc->sc_floods;
1508 sc->sc_floods = 0;
1509 sc->sc_errors = 0;
1510 mutex_spin_exit(&sc->sc_lock);
1511
1512 log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
1513 device_xname(sc->sc_dev),
1514 overflows, overflows == 1 ? "" : "s",
1515 floods, floods == 1 ? "" : "s");
1516 #endif
1517 }
1518
1519 integrate void
1520 imxuart_rxsoft(struct imxuart_softc *sc, struct tty *tp)
1521 {
1522 int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
1523 u_int cc, scc, outp;
1524 uint16_t data;
1525 u_int code;
1526
1527 scc = cc = IMXUART_RBUF_AVAIL(sc);
1528
1529 #if 0
1530 if (cc == imxuart_rbuf_size-1) {
1531 sc->sc_floods++;
1532 if (sc->sc_errors++ == 0)
1533 callout_reset(&sc->sc_diag_callout, 60 * hz,
1534 imxudiag, sc);
1535 }
1536 #endif
1537
1538 /* If not yet open, drop the entire buffer content here */
1539 if (!ISSET(tp->t_state, TS_ISOPEN)) {
1540 sc->sc_rbuf_out = sc->sc_rbuf_in;
1541 cc = 0;
1542 }
1543
1544 outp = sc->sc_rbuf_out;
1545
1546 #define ERRBITS (IMX_URXD_PRERR|IMX_URXD_BRK|IMX_URXD_FRMERR|IMX_URXD_OVRRUN)
1547
1548 while (cc) {
1549 data = sc->sc_rbuf[outp];
1550 code = data & IMX_URXD_RX_DATA;
1551 if (ISSET(data, ERRBITS)) {
1552 if (sc->sc_errors.err == 0)
1553 callout_reset(&sc->sc_diag_callout,
1554 60 * hz, imxudiag, sc);
1555 if (ISSET(data, IMX_URXD_OVRRUN))
1556 sc->sc_errors.ovrrun++;
1557 if (ISSET(data, IMX_URXD_BRK)) {
1558 sc->sc_errors.brk++;
1559 SET(code, TTY_FE);
1560 }
1561 if (ISSET(data, IMX_URXD_FRMERR)) {
1562 sc->sc_errors.frmerr++;
1563 SET(code, TTY_FE);
1564 }
1565 if (ISSET(data, IMX_URXD_PRERR)) {
1566 sc->sc_errors.prerr++;
1567 SET(code, TTY_PE);
1568 }
1569 }
1570 if ((*rint)(code, tp) == -1) {
1571 /*
1572 * The line discipline's buffer is out of space.
1573 */
1574 if (!ISSET(sc->sc_rx_flags, IMXUART_RX_TTY_BLOCKED)) {
1575 /*
1576 * We're either not using flow control, or the
1577 * line discipline didn't tell us to block for
1578 * some reason. Either way, we have no way to
1579 * know when there's more space available, so
1580 * just drop the rest of the data.
1581 */
1582 sc->sc_rbuf_out = sc->sc_rbuf_in;
1583 cc = 0;
1584 } else {
1585 /*
1586 * Don't schedule any more receive processing
1587 * until the line discipline tells us there's
1588 * space available (through imxuhwiflow()).
1589 * Leave the rest of the data in the input
1590 * buffer.
1591 */
1592 SET(sc->sc_rx_flags, IMXUART_RX_TTY_OVERFLOWED);
1593 }
1594 break;
1595 }
1596 outp = IMXUART_RBUF_INC(sc, outp, 1);
1597 cc--;
1598 }
1599
1600 if (cc != scc) {
1601 sc->sc_rbuf_out = outp;
1602 mutex_spin_enter(&sc->sc_lock);
1603
1604 cc = IMXUART_RBUF_SPACE(sc);
1605
1606 /* Buffers should be ok again, release possible block. */
1607 if (cc >= sc->sc_r_lowat) {
1608 if (ISSET(sc->sc_rx_flags, IMXUART_RX_IBUF_OVERFLOWED)) {
1609 CLR(sc->sc_rx_flags, IMXUART_RX_IBUF_OVERFLOWED);
1610 imxuart_control_rxint(sc, true);
1611 }
1612 if (ISSET(sc->sc_rx_flags, IMXUART_RX_IBUF_BLOCKED)) {
1613 CLR(sc->sc_rx_flags, IMXUART_RX_IBUF_BLOCKED);
1614 imxuart_hwiflow(sc);
1615 }
1616 }
1617 mutex_spin_exit(&sc->sc_lock);
1618 }
1619 }
1620
1621 integrate void
1622 imxuart_txsoft(struct imxuart_softc *sc, struct tty *tp)
1623 {
1624
1625 CLR(tp->t_state, TS_BUSY);
1626 if (ISSET(tp->t_state, TS_FLUSH))
1627 CLR(tp->t_state, TS_FLUSH);
1628 else
1629 ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
1630 (*tp->t_linesw->l_start)(tp);
1631 }
1632
1633 integrate void
1634 imxuart_stsoft(struct imxuart_softc *sc, struct tty *tp)
1635 {
1636 #ifdef notyet
1637 u_char msr, delta;
1638
1639 mutex_spin_enter(&sc->sc_lock);
1640 msr = sc->sc_msr;
1641 delta = sc->sc_msr_delta;
1642 sc->sc_msr_delta = 0;
1643 mutex_spin_exit(&sc->sc_lock);
1644
1645 if (ISSET(delta, sc->sc_msr_dcd)) {
1646 /*
1647 * Inform the tty layer that carrier detect changed.
1648 */
1649 (void) (*tp->t_linesw->l_modem)(tp, ISSET(msr, MSR_DCD));
1650 }
1651
1652 if (ISSET(delta, sc->sc_msr_cts)) {
1653 /* Block or unblock output according to flow control. */
1654 if (ISSET(msr, sc->sc_msr_cts)) {
1655 sc->sc_tx_stopped = 0;
1656 (*tp->t_linesw->l_start)(tp);
1657 } else {
1658 sc->sc_tx_stopped = 1;
1659 }
1660 }
1661
1662 #endif
1663 #ifdef IMXUART_DEBUG
1664 if (imxuart_debug)
1665 imxustatus(sc, "imxuart_stsoft");
1666 #endif
1667 }
1668
1669 void
1670 imxusoft(void *arg)
1671 {
1672 struct imxuart_softc *sc = arg;
1673 struct tty *tp;
1674
1675 if (IMXUART_ISALIVE(sc) == 0)
1676 return;
1677
1678 tp = sc->sc_tty;
1679
1680 if (sc->sc_rx_ready) {
1681 sc->sc_rx_ready = 0;
1682 imxuart_rxsoft(sc, tp);
1683 }
1684
1685 if (sc->sc_st_check) {
1686 sc->sc_st_check = 0;
1687 imxuart_stsoft(sc, tp);
1688 }
1689
1690 if (sc->sc_tx_done) {
1691 sc->sc_tx_done = 0;
1692 imxuart_txsoft(sc, tp);
1693 }
1694 }
1695
1696 int
1697 imxuintr(void *arg)
1698 {
1699 struct imxuart_softc *sc = arg;
1700 uint32_t usr1, usr2;
1701 bus_space_tag_t iot = sc->sc_regs.ur_iot;
1702 bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
1703
1704
1705 if (IMXUART_ISALIVE(sc) == 0)
1706 return (0);
1707
1708 mutex_spin_enter(&sc->sc_lock);
1709
1710 usr2 = bus_space_read_4(iot, ioh, IMX_USR2);
1711
1712
1713 do {
1714 bus_space_write_4(iot, ioh, IMX_USR2,
1715 usr2 & (IMX_USR2_BRCD|IMX_USR2_ORE));
1716 if (usr2 & IMX_USR2_BRCD) {
1717 /* Break signal detected */
1718 int cn_trapped = 0;
1719
1720 cn_check_magic(sc->sc_tty->t_dev,
1721 CNC_BREAK, imxuart_cnm_state);
1722 if (cn_trapped)
1723 continue;
1724 #if defined(KGDB) && !defined(DDB)
1725 if (ISSET(sc->sc_hwflags, IMXUART_HW_KGDB)) {
1726 kgdb_connect(1);
1727 continue;
1728 }
1729 #endif
1730 }
1731
1732 if (usr2 & IMX_USR2_RDR)
1733 imxuintr_read(sc);
1734
1735 #ifdef IMXUART_PPS
1736 {
1737 u_char msr, delta;
1738
1739 msr = CSR_READ_1(regsp, IMXUART_REG_MSR);
1740 delta = msr ^ sc->sc_msr;
1741 sc->sc_msr = msr;
1742 if ((sc->sc_pps_state.ppsparam.mode & PPS_CAPTUREBOTH) &&
1743 (delta & MSR_DCD)) {
1744 mutex_spin_enter(&timecounter_lock);
1745 pps_capture(&sc->sc_pps_state);
1746 pps_event(&sc->sc_pps_state,
1747 (msr & MSR_DCD) ?
1748 PPS_CAPTUREASSERT :
1749 PPS_CAPTURECLEAR);
1750 mutex_spin_exit(&timecounter_lock);
1751 }
1752 }
1753 #endif
1754
1755 #ifdef notyet
1756 /*
1757 * Process normal status changes
1758 */
1759 if (ISSET(delta, sc->sc_msr_mask)) {
1760 SET(sc->sc_msr_delta, delta);
1761
1762 /*
1763 * Stop output immediately if we lose the output
1764 * flow control signal or carrier detect.
1765 */
1766 if (ISSET(~msr, sc->sc_msr_mask)) {
1767 sc->sc_tbc = 0;
1768 sc->sc_heldtbc = 0;
1769 #ifdef IMXUART_DEBUG
1770 if (imxuart_debug)
1771 imxustatus(sc, "imxuintr ");
1772 #endif
1773 }
1774
1775 sc->sc_st_check = 1;
1776 }
1777 #endif
1778
1779 usr2 = bus_space_read_4(iot, ioh, IMX_USR2);
1780 } while (usr2 & (IMX_USR2_RDR|IMX_USR2_BRCD));
1781
1782 usr1 = bus_space_read_4(iot, ioh, IMX_USR1);
1783 if (usr1 & IMX_USR1_TRDY)
1784 imxuintr_send(sc);
1785
1786 mutex_spin_exit(&sc->sc_lock);
1787
1788 /* Wake up the poller. */
1789 softint_schedule(sc->sc_si);
1790
1791 #ifdef RND_COM
1792 rnd_add_uint32(&sc->rnd_source, iir | lsr);
1793 #endif
1794
1795 return (1);
1796 }
1797
1798
1799 /*
1800 * called when there is least one character in rxfifo
1801 *
1802 */
1803
1804 static void
1805 imxuintr_read(struct imxuart_softc *sc)
1806 {
1807 int cc;
1808 uint16_t rd;
1809 uint32_t usr2;
1810 bus_space_tag_t iot = sc->sc_regs.ur_iot;
1811 bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
1812
1813 cc = IMXUART_RBUF_SPACE(sc);
1814
1815 /* clear aging timer interrupt */
1816 bus_space_write_4(iot, ioh, IMX_USR1, IMX_USR1_AGTIM);
1817
1818 while (cc > 0) {
1819 int cn_trapped = 0;
1820
1821
1822 sc->sc_rbuf[sc->sc_rbuf_in] = rd =
1823 bus_space_read_4(iot, ioh, IMX_URXD);
1824
1825 cn_check_magic(sc->sc_tty->t_dev,
1826 rd & 0xff, imxuart_cnm_state);
1827
1828 if (!cn_trapped) {
1829 #if defined(DDB) && defined(DDB_KEYCODE)
1830 /*
1831 * Temporary hack so that I can force the kernel into
1832 * the debugger via the serial port
1833 */
1834 if ((rd & 0xff) == DDB_KEYCODE)
1835 Debugger();
1836 #endif
1837 sc->sc_rbuf_in = IMXUART_RBUF_INC(sc, sc->sc_rbuf_in, 1);
1838 cc--;
1839 }
1840
1841 usr2 = bus_space_read_4(iot, ioh, IMX_USR2);
1842 if (!(usr2 & IMX_USR2_RDR))
1843 break;
1844 }
1845
1846 /*
1847 * Current string of incoming characters ended because
1848 * no more data was available or we ran out of space.
1849 * Schedule a receive event if any data was received.
1850 * If we're out of space, turn off receive interrupts.
1851 */
1852 if (!ISSET(sc->sc_rx_flags, IMXUART_RX_TTY_OVERFLOWED))
1853 sc->sc_rx_ready = 1;
1854 /*
1855 * See if we are in danger of overflowing a buffer. If
1856 * so, use hardware flow control to ease the pressure.
1857 */
1858 if (!ISSET(sc->sc_rx_flags, IMXUART_RX_IBUF_BLOCKED) &&
1859 cc < sc->sc_r_hiwat) {
1860 sc->sc_rx_flags |= IMXUART_RX_IBUF_BLOCKED;
1861 imxuart_hwiflow(sc);
1862 }
1863
1864 /*
1865 * If we're out of space, disable receive interrupts
1866 * until the queue has drained a bit.
1867 */
1868 if (!cc) {
1869 sc->sc_rx_flags |= IMXUART_RX_IBUF_OVERFLOWED;
1870 imxuart_control_rxint(sc, false);
1871 }
1872 }
1873
1874
1875
1876 /*
1877 * find how many chars we can put into tx-fifo
1878 */
1879 static u_int
1880 imxuart_txfifo_space(struct imxuart_softc *sc)
1881 {
1882 uint32_t usr1, usr2;
1883 u_int cc;
1884 bus_space_tag_t iot = sc->sc_regs.ur_iot;
1885 bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
1886
1887 usr2 = bus_space_read_4(iot, ioh, IMX_USR2);
1888 if (usr2 & IMX_USR2_TXFE)
1889 cc = sc->sc_txfifo_len;
1890 else {
1891 usr1 = bus_space_read_4(iot, ioh, IMX_USR1);
1892 if (usr1 & IMX_USR1_TRDY)
1893 cc = sc->sc_txfifo_thresh;
1894 else
1895 cc = 0;
1896 }
1897
1898 return cc;
1899 }
1900
1901 void
1902 imxuintr_send(struct imxuart_softc *sc)
1903 {
1904 uint32_t usr2;
1905 bus_space_tag_t iot = sc->sc_regs.ur_iot;
1906 bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
1907 int cc = 0;
1908
1909 usr2 = bus_space_read_4(iot, ioh, IMX_USR2);
1910
1911 if (sc->sc_pending) {
1912 if (usr2 & IMX_USR2_TXFE) {
1913 imxuart_load_pendings(sc);
1914 sc->sc_tbc = sc->sc_heldtbc;
1915 sc->sc_heldtbc = 0;
1916 }
1917 else {
1918 /* wait for TX fifo empty */
1919 imxuart_control_txint(sc, true);
1920 return;
1921 }
1922 }
1923
1924 cc = imxuart_txfifo_space(sc);
1925 cc = MIN(cc, sc->sc_tbc);
1926
1927 if (cc > 0) {
1928 bus_space_write_multi_1(iot, ioh, IMX_UTXD, sc->sc_tba, cc);
1929 sc->sc_tbc -= cc;
1930 sc->sc_tba += cc;
1931 }
1932
1933 if (sc->sc_tbc > 0)
1934 imxuart_control_txint(sc, true);
1935 else {
1936 /* no more chars to send.
1937 we don't need tx interrupt any more. */
1938 imxuart_control_txint(sc, false);
1939 if (sc->sc_tx_busy) {
1940 sc->sc_tx_busy = 0;
1941 sc->sc_tx_done = 1;
1942 }
1943 }
1944 }
1945
1946 static void
1947 imxuart_disable_all_interrupts(struct imxuart_softc *sc)
1948 {
1949 bus_space_tag_t iot = sc->sc_regs.ur_iot;
1950 bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
1951
1952 sc->sc_ucr1 &= ~IMXUART_INTRS_UCR1;
1953 sc->sc_ucr2 &= ~IMXUART_INTRS_UCR2;
1954 sc->sc_ucr3 &= ~IMXUART_INTRS_UCR3;
1955 sc->sc_ucr4 &= ~IMXUART_INTRS_UCR4;
1956
1957
1958 bus_space_write_region_4(iot, ioh, IMX_UCR1, sc->sc_ucr, 4);
1959 }
1960
1961 static void
1962 imxuart_control_rxint(struct imxuart_softc *sc, bool enable)
1963 {
1964 bus_space_tag_t iot = sc->sc_regs.ur_iot;
1965 bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
1966 uint32_t ucr1, ucr2;
1967
1968 ucr1 = sc->sc_ucr1;
1969 ucr2 = sc->sc_ucr2;
1970
1971 if (enable) {
1972 ucr1 |= IMX_UCR1_RRDYEN;
1973 ucr2 |= IMX_UCR2_ATEN;
1974 }
1975 else {
1976 ucr1 &= ~IMX_UCR1_RRDYEN;
1977 ucr2 &= ~IMX_UCR2_ATEN;
1978 }
1979
1980 if (ucr1 != sc->sc_ucr1 || ucr2 != sc->sc_ucr2) {
1981 sc->sc_ucr1 = ucr1;
1982 sc->sc_ucr2 = ucr2;
1983 bus_space_write_region_4(iot, ioh, IMX_UCR1, sc->sc_ucr, 2);
1984 }
1985 }
1986
1987 static void
1988 imxuart_control_txint(struct imxuart_softc *sc, bool enable)
1989 {
1990 bus_space_tag_t iot = sc->sc_regs.ur_iot;
1991 bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
1992 uint32_t ucr1;
1993 uint32_t mask;
1994
1995 /* if parameter change is pending, get interrupt when Tx fifo
1996 is completely empty. otherwise, get interrupt when txfifo
1997 has less characters than threshold */
1998 mask = sc->sc_pending ? IMX_UCR1_TXMPTYEN : IMX_UCR1_TRDYEN;
1999
2000 ucr1 = sc->sc_ucr1;
2001
2002 CLR(ucr1, IMX_UCR1_TXMPTYEN|IMX_UCR1_TRDYEN);
2003 if (enable)
2004 SET(ucr1, mask);
2005
2006 if (ucr1 != sc->sc_ucr1) {
2007 bus_space_write_4(iot, ioh, IMX_UCR1, ucr1);
2008 sc->sc_ucr1 = ucr1;
2009 }
2010 }
2011
2012
2013 static void
2014 imxuart_load_params(struct imxuart_softc *sc)
2015 {
2016 uint32_t ucr2;
2017 bus_space_tag_t iot = sc->sc_regs.ur_iot;
2018 bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
2019
2020 ucr2 = (sc->sc_ucr2_d & ~IMX_UCR2_ATEN) |
2021 (sc->sc_ucr2 & IMX_UCR2_ATEN);
2022
2023 bus_space_write_4(iot, ioh, IMX_UCR2, ucr2);
2024 sc->sc_ucr2 = ucr2;
2025 }
2026
2027 static void
2028 imxuart_load_speed(struct imxuart_softc *sc)
2029 {
2030 bus_space_tag_t iot = sc->sc_regs.ur_iot;
2031 bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
2032 int n, rfdiv, ufcr;
2033
2034 #ifdef notyet
2035 /*
2036 * Set the FIFO threshold based on the receive speed.
2037 *
2038 * * If it's a low speed, it's probably a mouse or some other
2039 * interactive device, so set the threshold low.
2040 * * If it's a high speed, trim the trigger level down to prevent
2041 * overflows.
2042 * * Otherwise set it a bit higher.
2043 */
2044 if (t->c_ospeed <= 1200)
2045 sc->sc_fifo = FIFO_ENABLE | FIFO_TRIGGER_1;
2046 else if (t->c_ospeed <= 38400)
2047 sc->sc_fifo = FIFO_ENABLE | FIFO_TRIGGER_8;
2048 else
2049 sc->sc_fifo = FIFO_ENABLE | FIFO_TRIGGER_4;
2050 #endif
2051
2052 n = 32 - sc->sc_txfifo_thresh;
2053 n = MAX(2, n);
2054
2055 rfdiv = IMX_UFCR_DIVIDER_TO_RFDIV(imxuart_freqdiv);
2056
2057 ufcr = (n << IMX_UFCR_TXTL_SHIFT) |
2058 (rfdiv << IMX_UFCR_RFDIV_SHIFT) |
2059 (16 << IMX_UFCR_RXTL_SHIFT);
2060
2061 /* keep DCE/DTE bit */
2062 ufcr |= bus_space_read_4(iot, ioh, IMX_UFCR) & IMX_UFCR_DCEDTE;
2063
2064 bus_space_write_4(iot, ioh, IMX_UFCR, ufcr);
2065
2066 /* UBIR must updated before UBMR */
2067 bus_space_write_4(iot, ioh,
2068 IMX_UBIR, sc->sc_ratio.numerator);
2069 bus_space_write_4(iot, ioh,
2070 IMX_UBMR, sc->sc_ratio.modulator);
2071
2072
2073 }
2074
2075
2076 static void
2077 imxuart_load_pendings(struct imxuart_softc *sc)
2078 {
2079 if (sc->sc_pending & IMXUART_PEND_PARAM)
2080 imxuart_load_params(sc);
2081 if (sc->sc_pending & IMXUART_PEND_SPEED)
2082 imxuart_load_speed(sc);
2083 sc->sc_pending = 0;
2084 }
2085
2086 /*
2087 * The following functions are polled getc and putc routines, shared
2088 * by the console and kgdb glue.
2089 *
2090 * The read-ahead code is so that you can detect pending in-band
2091 * cn_magic in polled mode while doing output rather than having to
2092 * wait until the kernel decides it needs input.
2093 */
2094
2095 #define READAHEAD_RING_LEN 16
2096 static int imxuart_readahead[READAHEAD_RING_LEN];
2097 static int imxuart_readahead_in = 0;
2098 static int imxuart_readahead_out = 0;
2099 #define READAHEAD_IS_EMPTY() (imxuart_readahead_in==imxuart_readahead_out)
2100 #define READAHEAD_IS_FULL() \
2101 (((imxuart_readahead_in+1) & (READAHEAD_RING_LEN-1)) ==imxuart_readahead_out)
2102
2103 int
2104 imxuart_common_getc(dev_t dev, struct imxuart_regs *regsp)
2105 {
2106 int s = splserial();
2107 u_char c;
2108 bus_space_tag_t iot = regsp->ur_iot;
2109 bus_space_handle_t ioh = regsp->ur_ioh;
2110 uint32_t usr2;
2111
2112 /* got a character from reading things earlier */
2113 if (imxuart_readahead_in != imxuart_readahead_out) {
2114
2115 c = imxuart_readahead[imxuart_readahead_out];
2116 imxuart_readahead_out = (imxuart_readahead_out + 1) &
2117 (READAHEAD_RING_LEN-1);
2118 splx(s);
2119 return (c);
2120 }
2121
2122 /* block until a character becomes available */
2123 while (!((usr2 = bus_space_read_4(iot, ioh, IMX_USR2)) & IMX_USR2_RDR))
2124 ;
2125
2126 c = 0xff & bus_space_read_4(iot, ioh, IMX_URXD);
2127
2128 {
2129 int __attribute__((__unused__))cn_trapped = 0; /* unused */
2130 #ifdef DDB
2131 extern int db_active;
2132 if (!db_active)
2133 #endif
2134 cn_check_magic(dev, c, imxuart_cnm_state);
2135 }
2136 splx(s);
2137 return (c);
2138 }
2139
2140 void
2141 imxuart_common_putc(dev_t dev, struct imxuart_regs *regsp, int c)
2142 {
2143 int s = splserial();
2144 int cin, timo;
2145 bus_space_tag_t iot = regsp->ur_iot;
2146 bus_space_handle_t ioh = regsp->ur_ioh;
2147 uint32_t usr2;
2148
2149 if (!READAHEAD_IS_FULL() &&
2150 ((usr2 = bus_space_read_4(iot, ioh, IMX_USR2)) & IMX_USR2_RDR)) {
2151
2152 int __attribute__((__unused__))cn_trapped = 0;
2153 cin = bus_space_read_4(iot, ioh, IMX_URXD);
2154 cn_check_magic(dev, cin & 0xff, imxuart_cnm_state);
2155 imxuart_readahead_in = (imxuart_readahead_in + 1) &
2156 (READAHEAD_RING_LEN-1);
2157 }
2158
2159 /* wait for any pending transmission to finish */
2160 timo = 150000;
2161 do {
2162 if (bus_space_read_4(iot, ioh, IMX_USR1) & IMX_USR1_TRDY) {
2163 bus_space_write_4(iot, ioh, IMX_UTXD, c);
2164 break;
2165 }
2166 } while(--timo > 0);
2167
2168 IMXUART_BARRIER(regsp, BR | BW);
2169
2170 splx(s);
2171 }
2172
2173 /*
2174 * Initialize UART
2175 */
2176 int
2177 imxuart_init(struct imxuart_regs *regsp, int rate, tcflag_t cflag, int domap)
2178 {
2179 struct imxuart_baudrate_ratio ratio;
2180 int rfdiv = IMX_UFCR_DIVIDER_TO_RFDIV(imxuart_freqdiv);
2181 uint32_t ufcr;
2182 int error;
2183
2184 if (domap && (error = bus_space_map(regsp->ur_iot, regsp->ur_iobase,
2185 IMX_UART_SIZE, 0, ®sp->ur_ioh)) != 0)
2186 return error;
2187
2188 if (imxuart_freq != 0) {
2189 if (imxuspeed(rate, &ratio) < 0)
2190 return EINVAL;
2191
2192 /* UBIR must updated before UBMR */
2193 bus_space_write_4(regsp->ur_iot, regsp->ur_ioh,
2194 IMX_UBIR, ratio.numerator);
2195 bus_space_write_4(regsp->ur_iot, regsp->ur_ioh,
2196 IMX_UBMR, ratio.modulator);
2197 }
2198
2199 /* XXX: DTREN, DPEC */
2200 bus_space_write_4(regsp->ur_iot, regsp->ur_ioh, IMX_UCR3,
2201 IMX_UCR3_DSR|IMX_UCR3_RXDMUXSEL);
2202
2203 ufcr = bus_space_read_4(regsp->ur_iot, regsp->ur_ioh, IMX_UFCR);
2204 ufcr &= ~IMX_UFCR_TXTL;
2205 ufcr |= (8 << IMX_UFCR_TXTL_SHIFT);
2206 ufcr &= ~IMX_UFCR_RXTL;
2207 ufcr |= (1 << IMX_UFCR_RXTL_SHIFT);
2208 if (imxuart_freq != 0) {
2209 ufcr &= ~IMX_UFCR_RFDIV;
2210 ufcr |= (rfdiv << IMX_UFCR_RFDIV_SHIFT);
2211 }
2212 bus_space_write_4(regsp->ur_iot, regsp->ur_ioh, IMX_UFCR, ufcr);
2213
2214 if (imxuart_freq != 0) {
2215 bus_space_write_4(regsp->ur_iot, regsp->ur_ioh, IMX_ONEMS,
2216 imxuart_freq / imxuart_freqdiv / 1000);
2217 }
2218
2219 bus_space_write_4(regsp->ur_iot, regsp->ur_ioh, IMX_UCR2,
2220 IMX_UCR2_IRTS|
2221 IMX_UCR2_CTSC|
2222 IMX_UCR2_WS|IMX_UCR2_TXEN|
2223 IMX_UCR2_RXEN|IMX_UCR2_SRST);
2224 /* clear status registers */
2225 bus_space_write_4(regsp->ur_iot, regsp->ur_ioh, IMX_USR1, 0xffff);
2226 bus_space_write_4(regsp->ur_iot, regsp->ur_ioh, IMX_USR2, 0xffff);
2227
2228
2229 bus_space_write_4(regsp->ur_iot, regsp->ur_ioh, IMX_UCR1,
2230 IMX_UCR1_UARTEN);
2231
2232 return (0);
2233 }
2234
2235
2236 /*
2237 * Following are all routines needed for UART to act as console
2238 */
2239 struct consdev imxucons = {
2240 NULL, NULL, imxucngetc, imxucnputc, imxucnpollc, NULL, NULL, NULL,
2241 NODEV, CN_NORMAL
2242 };
2243
2244
2245 int
2246 imxuart_cnattach(bus_space_tag_t iot, paddr_t iobase, u_int rate,
2247 tcflag_t cflag)
2248 {
2249 struct imxuart_regs regs;
2250 int res;
2251
2252 regs.ur_iot = iot;
2253 regs.ur_iobase = iobase;
2254
2255 res = imxuart_init(®s, rate, cflag, true);
2256 if (res)
2257 return (res);
2258
2259 cn_tab = &imxucons;
2260 cn_init_magic(&imxuart_cnm_state);
2261 cn_set_magic("\047\001"); /* default magic is BREAK */
2262
2263 imxuconsrate = rate;
2264 imxuconscflag = cflag;
2265
2266 imxuconsregs = regs;
2267
2268 return 0;
2269 }
2270
2271 int
2272 imxucngetc(dev_t dev)
2273 {
2274 return (imxuart_common_getc(dev, &imxuconsregs));
2275 }
2276
2277 /*
2278 * Console kernel output character routine.
2279 */
2280 void
2281 imxucnputc(dev_t dev, int c)
2282 {
2283 imxuart_common_putc(dev, &imxuconsregs, c);
2284 }
2285
2286 void
2287 imxucnpollc(dev_t dev, int on)
2288 {
2289
2290 imxuart_readahead_in = 0;
2291 imxuart_readahead_out = 0;
2292 }
2293
2294 #ifdef KGDB
2295 int
2296 imxuart_kgdb_attach(bus_space_tag_t iot, paddr_t iobase, u_int rate,
2297 tcflag_t cflag)
2298 {
2299 int res;
2300
2301 if (iot == imxuconsregs.ur_iot &&
2302 iobase == imxuconsregs.ur_iobase) {
2303 #if !defined(DDB)
2304 return (EBUSY); /* cannot share with console */
2305 #else
2306 imxu_kgdb_regs.ur_iot = iot;
2307 imxu_kgdb_regs.ur_ioh = imxuconsregs.ur_ioh;
2308 imxu_kgdb_regs.ur_iobase = iobase;
2309 #endif
2310 } else {
2311 imxu_kgdb_regs.ur_iot = iot;
2312 imxu_kgdb_regs.ur_iobase = iobase;
2313
2314 res = imxuart_init(&imxu_kgdb_regs, rate, cflag, true);
2315 if (res)
2316 return (res);
2317
2318 /*
2319 * XXXfvdl this shouldn't be needed, but the cn_magic goo
2320 * expects this to be initialized
2321 */
2322 cn_init_magic(&imxuart_cnm_state);
2323 cn_set_magic("\047\001");
2324 }
2325
2326 kgdb_attach(imxuart_kgdb_getc, imxuart_kgdb_putc, &imxu_kgdb_regs);
2327 kgdb_dev = 123; /* unneeded, only to satisfy some tests */
2328
2329 return (0);
2330 }
2331
2332 /* ARGSUSED */
2333 int
2334 imxuart_kgdb_getc(void *arg)
2335 {
2336 struct imxuart_regs *regs = arg;
2337
2338 return (imxuart_common_getc(NODEV, regs));
2339 }
2340
2341 /* ARGSUSED */
2342 void
2343 imxuart_kgdb_putc(void *arg, int c)
2344 {
2345 struct imxuart_regs *regs = arg;
2346
2347 imxuart_common_putc(NODEV, regs, c);
2348 }
2349 #endif /* KGDB */
2350
2351 /* helper function to identify the imxu ports used by
2352 console or KGDB (and not yet autoconf attached) */
2353 int
2354 imxuart_is_console(bus_space_tag_t iot, bus_addr_t iobase, bus_space_handle_t *ioh)
2355 {
2356 bus_space_handle_t help;
2357
2358 if (!imxuconsattached &&
2359 iot == imxuconsregs.ur_iot && iobase == imxuconsregs.ur_iobase)
2360 help = imxuconsregs.ur_ioh;
2361 #ifdef KGDB
2362 else if (!imxu_kgdb_attached &&
2363 iot == imxu_kgdb_regs.ur_iot && iobase == imxu_kgdb_regs.ur_iobase)
2364 help = imxu_kgdb_regs.ur_ioh;
2365 #endif
2366 else
2367 return (0);
2368
2369 if (ioh)
2370 *ioh = help;
2371 return (1);
2372 }
2373
2374 #ifdef notyet
2375
2376 bool
2377 imxuart_cleanup(device_t self, int how)
2378 {
2379 /*
2380 * this routine exists to serve as a shutdown hook for systems that
2381 * have firmware which doesn't interact properly with a imxuart device in
2382 * FIFO mode.
2383 */
2384 struct imxuart_softc *sc = device_private(self);
2385
2386 if (ISSET(sc->sc_hwflags, IMXUART_HW_FIFO))
2387 UR_WRITE_1(&sc->sc_regs, IMXUART_REG_FIFO, 0);
2388
2389 return true;
2390 }
2391 #endif
2392
2393 #ifdef notyet
2394 bool
2395 imxuart_suspend(device_t self PMF_FN_ARGS)
2396 {
2397 struct imxuart_softc *sc = device_private(self);
2398
2399 UR_WRITE_1(&sc->sc_regs, IMXUART_REG_IER, 0);
2400 (void)CSR_READ_1(&sc->sc_regs, IMXUART_REG_IIR);
2401
2402 return true;
2403 }
2404 #endif
2405
2406 #ifdef notyet
2407 bool
2408 imxuart_resume(device_t self PMF_FN_ARGS)
2409 {
2410 struct imxuart_softc *sc = device_private(self);
2411
2412 mutex_spin_enter(&sc->sc_lock);
2413 imxuart_loadchannelregs(sc);
2414 mutex_spin_exit(&sc->sc_lock);
2415
2416 return true;
2417 }
2418 #endif
2419
2420 static void
2421 imxuart_enable_debugport(struct imxuart_softc *sc)
2422 {
2423 bus_space_tag_t iot = sc->sc_regs.ur_iot;
2424 bus_space_handle_t ioh = sc->sc_regs.ur_ioh;
2425
2426 if (sc->sc_hwflags & (IMXUART_HW_CONSOLE|IMXUART_HW_KGDB)) {
2427
2428 /* Turn on line break interrupt, set carrier. */
2429
2430 sc->sc_ucr3 |= IMX_UCR3_DSR;
2431 bus_space_write_4(iot, ioh, IMX_UCR3, sc->sc_ucr3);
2432
2433 sc->sc_ucr4 |= IMX_UCR4_BKEN;
2434 bus_space_write_4(iot, ioh, IMX_UCR4, sc->sc_ucr4);
2435
2436 sc->sc_ucr2 |= IMX_UCR2_TXEN|IMX_UCR2_RXEN|
2437 IMX_UCR2_CTS;
2438 bus_space_write_4(iot, ioh, IMX_UCR2, sc->sc_ucr2);
2439
2440 sc->sc_ucr1 |= IMX_UCR1_UARTEN;
2441 bus_space_write_4(iot, ioh, IMX_UCR1, sc->sc_ucr1);
2442 }
2443 }
2444
2445
2446 void
2447 imxuart_set_frequency(u_int freq, u_int div)
2448 {
2449 imxuart_freq = freq;
2450 imxuart_freqdiv = div;
2451 }
2452