at91usart.c revision 1.10 1 /* $Id: at91usart.c,v 1.10 2014/07/25 08:10:31 dholland Exp $ */
2 /* $NetBSD: at91usart.c,v 1.10 2014/07/25 08:10:31 dholland Exp $ */
3
4 /*
5 * Copyright (c) 2007 Embedtronics Oy. All rights reserved.
6 *
7 * Copyright (c) 1998, 1999, 2001, 2002, 2004 The NetBSD Foundation, Inc.
8 * All rights reserved.
9 *
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by Jesse Off
12 *
13 * This code is derived from software contributed to The NetBSD Foundation
14 * by Ichiro FUKUHARA and Naoto Shimazaki.
15 *
16 * This code is derived from software contributed to The NetBSD Foundation
17 * by IWAMOTO Toshihiro.
18 *
19 * This code is derived from software contributed to The NetBSD Foundation
20 * by Charles M. Hannum.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
32 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
33 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
34 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
35 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
36 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
39 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGE.
42 */
43
44 /*
45 * Copyright (c) 1991 The Regents of the University of California.
46 * All rights reserved.
47 *
48 * Redistribution and use in source and binary forms, with or without
49 * modification, are permitted provided that the following conditions
50 * are met:
51 * 1. Redistributions of source code must retain the above copyright
52 * notice, this list of conditions and the following disclaimer.
53 * 2. Redistributions in binary form must reproduce the above copyright
54 * notice, this list of conditions and the following disclaimer in the
55 * documentation and/or other materials provided with the distribution.
56 * 3. Neither the name of the University nor the names of its contributors
57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * SUCH DAMAGE.
71 *
72 * @(#)com.c 7.5 (Berkeley) 5/16/91
73 */
74
75 /*
76 * TODO: hardware flow control
77 */
78
79 #include <sys/cdefs.h>
80 __KERNEL_RCSID(0, "$NetBSD: at91usart.c,v 1.10 2014/07/25 08:10:31 dholland Exp $");
81
82 #include "opt_ddb.h"
83 #include "opt_kgdb.h"
84
85 #include "rnd.h"
86 #ifdef RND_COM
87 #include <sys/rnd.h>
88 #endif
89
90 #ifdef NOTYET
91 /*
92 * Override cnmagic(9) macro before including <sys/systm.h>.
93 * We need to know if cn_check_magic triggered debugger, so set a flag.
94 * Callers of cn_check_magic must declare int cn_trapped = 0;
95 * XXX: this is *ugly*!
96 */
97 #define cn_trap() \
98 do { \
99 console_debugger(); \
100 cn_trapped = 1; \
101 } while (/* CONSTCOND */ 0)
102 #endif /* NOTYET */
103
104
105 #include <sys/param.h>
106 #include <sys/systm.h>
107 #include <sys/types.h>
108 #include <sys/conf.h>
109 #include <sys/file.h>
110 #include <sys/device.h>
111 #include <sys/kernel.h>
112 #include <sys/tty.h>
113 #include <sys/uio.h>
114 #include <sys/vnode.h>
115 #include <sys/kauth.h>
116
117 #include <machine/intr.h>
118 #include <sys/bus.h>
119
120 #include <arm/at91/at91reg.h>
121 #include <arm/at91/at91var.h>
122 #include <arm/at91/at91usartreg.h>
123 #include <arm/at91/at91usartvar.h>
124
125 #include <dev/cons.h>
126
127 static int at91usart_param(struct tty *, struct termios *);
128 static void at91usart_start(struct tty *);
129 static int at91usart_hwiflow(struct tty *, int);
130
131 #if 0
132 static u_int cflag2lcrhi(tcflag_t);
133 #endif
134 static void at91usart_set(struct at91usart_softc *);
135
136 #if NOTYET
137 int at91usart_cn_getc(dev_t);
138 void at91usart_cn_putc(dev_t, int);
139 void at91usart_cn_pollc(dev_t, int);
140 void at91usart_cn_probe(struct consdev *);
141 void at91usart_cn_init(struct consdev *);
142
143 static struct at91usart_cons_softc {
144 bus_space_tag_t sc_iot;
145 bus_space_handle_t sc_ioh;
146 bus_addr_t sc_hwbase;
147 int sc_ospeed;
148 tcflag_t sc_cflag;
149 int sc_attached;
150
151 uint8_t *sc_rx_ptr;
152 uint8_t sc_rx_fifo[64];
153 } usart_cn_sc;
154
155 static struct cnm_state at91usart_cnm_state;
156 #endif /* NOTYET */
157
158 static void at91usart_soft(void* arg);
159 inline static void at91usart_txsoft(struct at91usart_softc *, struct tty *);
160 inline static void at91usart_rxsoft(struct at91usart_softc *, struct tty *, unsigned csr);
161
162 #define PDC_BLOCK_SIZE 64
163
164 //CFATTACH_DECL_NEW(at91usart, sizeof(struct at91usart_softc),
165 // at91usart_match, at91usart_attach, NULL, NULL);
166
167 //#define USART_DEBUG 10
168
169 #ifdef USART_DEBUG
170 int usart_debug = USART_DEBUG;
171 #define DPRINTFN(n,fmt) if (usart_debug >= (n)) printf fmt
172 #else
173 #define DPRINTFN(n,fmt)
174 #endif
175
176 extern struct cfdriver at91usart_cd;
177
178 dev_type_open(at91usart_open);
179 dev_type_close(at91usart_close);
180 dev_type_read(at91usart_read);
181 dev_type_write(at91usart_write);
182 dev_type_ioctl(at91usart_ioctl);
183 dev_type_stop(at91usart_stop);
184 dev_type_tty(at91usart_tty);
185 dev_type_poll(at91usart_poll);
186
187 const struct cdevsw at91usart_cdevsw = {
188 .d_open = at91usart_open,
189 .d_close = at91usart_close,
190 .d_read = at91usart_read,
191 .d_write = at91usart_write,
192 .d_ioctl = at91usart_ioctl,
193 .d_stop = at91usart_stop,
194 .d_tty = at91usart_tty,
195 .d_poll = at91usart_poll,
196 .d_mmap = nommap,
197 .d_kqfilter = ttykqfilter,
198 .d_discard = nodiscard,
199 .d_flag = D_TTY
200 };
201
202 #if NOTYET
203 struct consdev at91usart_cons = {
204 at91usart_cn_probe, NULL, at91usart_cn_getc, at91usart_cn_putc, at91usart_cn_pollc, NULL,
205 NULL, NULL, NODEV, CN_REMOTE
206 };
207 #endif /* NOTYET */
208
209 #ifndef DEFAULT_COMSPEED
210 #define DEFAULT_COMSPEED 115200
211 #endif
212
213 #define COMUNIT_MASK 0x7ffff
214 #define COMDIALOUT_MASK 0x80000
215
216 #define COMUNIT(x) (minor(x) & COMUNIT_MASK)
217 #define COMDIALOUT(x) (minor(x) & COMDIALOUT_MASK)
218
219 #define COM_ISALIVE(sc) ((sc)->enabled != 0 && device_is_active((sc)->sc_dev))
220
221 static inline void
222 at91usart_writereg(struct at91usart_softc *sc, int reg, u_int val)
223 {
224 bus_space_write_4(sc->sc_iot, sc->sc_ioh, reg, val);
225 }
226
227 static inline u_int
228 at91usart_readreg(struct at91usart_softc *sc, int reg)
229 {
230 return bus_space_read_4(sc->sc_iot, sc->sc_ioh, reg);
231 }
232 #if 0
233 static int
234 at91usart_match(device_t parent, cfdata_t cf, void *aux)
235 {
236 if (strcmp(cf->cf_name, "at91usart") == 0)
237 return 1;
238 return 0;
239 }
240 #endif
241 static int at91usart_intr(void* arg);
242
243 void
244 at91usart_attach_subr(struct at91usart_softc *sc, struct at91bus_attach_args *sa)
245 {
246 struct tty *tp;
247 int err;
248
249 printf("\n");
250
251 if (bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 0, &sc->sc_ioh))
252 panic("%s: Cannot map registers", device_xname(sc->sc_dev));
253
254 sc->sc_iot = sa->sa_iot;
255 sc->sc_hwbase = sa->sa_addr;
256 sc->sc_dmat = sa->sa_dmat;
257 sc->sc_pid = sa->sa_pid;
258
259 /* allocate fifos */
260 err = at91pdc_alloc_fifo(sc->sc_dmat, &sc->sc_rx_fifo, AT91USART_RING_SIZE, BUS_DMA_READ | BUS_DMA_STREAMING);
261 if (err)
262 panic("%s: cannot allocate rx fifo", device_xname(sc->sc_dev));
263
264 err = at91pdc_alloc_fifo(sc->sc_dmat, &sc->sc_tx_fifo, AT91USART_RING_SIZE, BUS_DMA_WRITE | BUS_DMA_STREAMING);
265 if (err)
266 panic("%s: cannot allocate tx fifo", device_xname(sc->sc_dev));
267
268 /* initialize uart */
269 at91_peripheral_clock(sc->sc_pid, 1);
270
271 at91usart_writereg(sc, US_IDR, -1);
272 at91usart_writereg(sc, US_RTOR, 12); // 12-bit timeout
273 at91usart_writereg(sc, US_PDC + PDC_PTCR, PDC_PTCR_TXTDIS | PDC_PTCR_RXTDIS);
274 at91_intr_establish(sa->sa_pid, IPL_TTY, INTR_HIGH_LEVEL, at91usart_intr, sc);
275 USART_INIT(sc, 115200U);
276
277 #ifdef NOTYET
278 if (sc->sc_iot == usart_cn_sc.sc_iot
279 && sc->sc_hwbase == usart_cn_sc.sc_hwbase) {
280 usart_cn_sc.sc_attached = 1;
281 /* Make sure the console is always "hardwired". */
282 delay(10000); /* wait for output to finish */
283 SET(sc->sc_hwflags, COM_HW_CONSOLE);
284 SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
285 SET(sc->sc_ier, USART_INT_RXRDY);
286 USARTREG(USART_IER) = USART_INT_RXRDY; // @@@@@
287 }
288 #endif // NOTYET
289
290 tp = tty_alloc();
291 tp->t_oproc = at91usart_start;
292 tp->t_param = at91usart_param;
293 tp->t_hwiflow = at91usart_hwiflow;
294
295 sc->sc_tty = tp;
296
297 tty_attach(tp);
298
299 #if NOTYET
300 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
301 int maj;
302
303 /* locate the major number */
304 maj = cdevsw_lookup_major(&at91usart_cdevsw);
305
306 cn_tab->cn_dev = makedev(maj, device_unit(sc->sc_dev));
307
308 aprint_normal("%s: console (maj %u min %u cn_dev %u)\n",
309 device_xname(sc->sc_dev), maj, device_unit(sc->sc_dev),
310 cn_tab->cn_dev);
311 }
312 #endif /* NOTYET */
313
314 sc->sc_si = softint_establish(SOFTINT_SERIAL, at91usart_soft, sc);
315
316 #ifdef RND_COM
317 rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
318 RND_TYPE_TTY, 0);
319 #endif
320
321 /* if there are no enable/disable functions, assume the device
322 is always enabled */
323 if (!sc->enable)
324 sc->enabled = 1;
325
326 /* XXX configure register */
327 /* xxx_config(sc) */
328
329 SET(sc->sc_hwflags, COM_HW_DEV_OK);
330 }
331
332 static int
333 at91usart_param(struct tty *tp, struct termios *t)
334 {
335 struct at91usart_softc *sc
336 = device_lookup_private(&at91usart_cd, COMUNIT(tp->t_dev));
337 int s;
338
339 if (COM_ISALIVE(sc) == 0)
340 return (EIO);
341
342 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
343 return (EINVAL);
344
345 /*
346 * For the console, always force CLOCAL and !HUPCL, so that the port
347 * is always active.
348 */
349 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
350 ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
351 SET(t->c_cflag, CLOCAL);
352 CLR(t->c_cflag, HUPCL);
353 }
354
355 /*
356 * If there were no changes, don't do anything. This avoids dropping
357 * input and improves performance when all we did was frob things like
358 * VMIN and VTIME.
359 */
360 if (tp->t_ospeed == t->c_ospeed &&
361 tp->t_cflag == t->c_cflag)
362 return (0);
363
364 s = spltty();
365
366 sc->sc_brgr = (AT91_MSTCLK / 16 + t->c_ospeed / 2) / t->c_ospeed;
367
368 /* And copy to tty. */
369 tp->t_ispeed = 0;
370 tp->t_ospeed = t->c_ospeed;
371 tp->t_cflag = t->c_cflag;
372 at91usart_set(sc);
373
374 splx(s);
375
376 /*
377 * Update the tty layer's idea of the carrier bit.
378 * We tell tty the carrier is always on.
379 */
380 (void) (*tp->t_linesw->l_modem)(tp, 1);
381
382 #ifdef COM_DEBUG
383 if (com_debug)
384 comstatus(sc, "comparam ");
385 #endif
386
387 /* tell the upper layer about hwflow.. */
388 if (sc->hwflow)
389 (*sc->hwflow)(sc, t->c_cflag);
390
391 return (0);
392 }
393
394 static int
395 at91usart_hwiflow(struct tty *tp, int block)
396 {
397 if (block) {
398 /* tty discipline wants to block */
399 } else {
400 /* tty discipline wants to unblock */
401 }
402 return (0);
403 }
404
405 static __inline void
406 at91usart_start_tx(struct at91usart_softc *sc)
407 {
408 if (!sc->start_tx)
409 at91usart_writereg(sc, US_PDC + PDC_PTCR, PDC_PTCR_TXTEN);
410 else
411 (*sc->start_tx)(sc);
412 }
413
414 static __inline void
415 at91usart_stop_tx(struct at91usart_softc *sc)
416 {
417 if (!sc->stop_tx)
418 at91usart_writereg(sc, US_PDC + PDC_PTCR, PDC_PTCR_TXTDIS);
419 else
420 (*sc->stop_tx)(sc);
421 }
422
423 static __inline void
424 at91usart_rx_started(struct at91usart_softc *sc)
425 {
426 if (sc->rx_started)
427 (*sc->rx_started)(sc);
428 }
429
430 static __inline void
431 at91usart_rx_stopped(struct at91usart_softc *sc)
432 {
433 if (sc->rx_stopped)
434 (*sc->rx_stopped)(sc);
435 }
436
437 static __inline void
438 at91usart_rx_rts_ctl(struct at91usart_softc *sc, int enabled)
439 {
440 if (sc->rx_rts_ctl)
441 (*sc->rx_rts_ctl)(sc, enabled);
442 }
443
444 static void
445 at91usart_filltx(struct at91usart_softc *sc)
446 {
447 struct tty *tp = sc->sc_tty;
448 int len;
449 void *dst;
450
451 // post write handler
452 AT91PDC_FIFO_POSTWRITE(sc->sc_iot, sc->sc_ioh, sc->sc_dmat, US_PDC,
453 &sc->sc_tx_fifo);
454
455 // copy more data to fifo:
456 if (sc->sc_tbc > 0
457 && (dst = AT91PDC_FIFO_WRPTR(&sc->sc_tx_fifo, &len)) != NULL) {
458 // copy data to fifo
459 if (len > sc->sc_tbc)
460 len = sc->sc_tbc;
461 memcpy(dst, sc->sc_tba, len);
462 sc->sc_tba += len;
463 if ((sc->sc_tbc -= len) <= 0)
464 CLR(tp->t_state, TS_BUSY);
465 // update fifo
466 AT91PDC_FIFO_WRITTEN(&sc->sc_tx_fifo, len);
467 // tell tty interface we've sent some bytes
468 ndflush(&tp->t_outq, len);
469 }
470
471 // start sending data...
472 if (AT91PDC_FIFO_PREWRITE(sc->sc_iot, sc->sc_ioh, sc->sc_dmat,
473 US_PDC, &sc->sc_tx_fifo, PDC_BLOCK_SIZE)) {
474 at91usart_start_tx(sc);
475 SET(sc->sc_ier, US_CSR_TXEMPTY | US_CSR_ENDTX);
476 } else {
477 CLR(sc->sc_ier, US_CSR_ENDTX);
478 }
479 }
480
481 static void
482 at91usart_start(struct tty *tp)
483 {
484 struct at91usart_softc *sc
485 = device_lookup_private(&at91usart_cd, COMUNIT(tp->t_dev));
486 int s;
487
488 if (COM_ISALIVE(sc) == 0) {
489 DPRINTFN(5, ("%s: %s / COM_ISALIVE == 0\n", device_xname(sc->sc_dev), __FUNCTION__));
490 return;
491 }
492
493 s = spltty();
494 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) {
495 DPRINTFN(5, ("%s: %s: TS_BUSY || TS_TIMEOUT || TS_TTSTOP\n", device_xname(sc->sc_dev), __FUNCTION__));
496 goto out;
497 }
498
499 if (!ttypull(tp))
500 goto out;
501
502 /* Grab the first contiguous region of buffer space. */
503 {
504 u_char *tba;
505 int tbc;
506
507 tba = tp->t_outq.c_cf;
508 tbc = ndqb(&tp->t_outq, 0);
509
510 sc->sc_tba = tba;
511 sc->sc_tbc = tbc;
512 }
513
514 SET(tp->t_state, TS_BUSY);
515
516 /* Output the first chunk of the contiguous buffer. */
517 at91usart_filltx(sc);
518 at91usart_writereg(sc, US_IER, sc->sc_ier);
519 DPRINTFN(5, ("%s: %s, ier=%08x (csr=%08x)\n", device_xname(sc->sc_dev), __FUNCTION__, sc->sc_ier, at91usart_readreg(sc, US_CSR)));
520
521 out:
522 splx(s);
523
524 return;
525 }
526
527 static __inline__ void
528 at91usart_break(struct at91usart_softc *sc, int onoff)
529 {
530 at91usart_writereg(sc, US_CR, onoff ? US_CR_STTBRK : US_CR_STPBRK);
531 }
532
533 static void
534 at91usart_shutdown(struct at91usart_softc *sc)
535 {
536 int s;
537
538 s = spltty();
539
540 /* turn of dma */
541 at91usart_writereg(sc, US_PDC + PDC_PTCR, PDC_PTCR_TXTDIS | PDC_PTCR_RXTDIS);
542 at91usart_writereg(sc, US_PDC + PDC_TNCR, 0);
543 at91usart_writereg(sc, US_PDC + PDC_TCR, 0);
544 at91usart_writereg(sc, US_PDC + PDC_RNCR, 0);
545 at91usart_writereg(sc, US_PDC + PDC_RCR, 0);
546
547 /* Turn off interrupts. */
548 at91usart_writereg(sc, US_IDR, -1);
549
550 /* Clear any break condition set with TIOCSBRK. */
551 at91usart_break(sc, 0);
552 at91usart_set(sc);
553
554 if (sc->disable) {
555 #ifdef DIAGNOSTIC
556 if (!sc->enabled)
557 panic("at91usart_shutdown: not enabled?");
558 #endif
559 (*sc->disable)(sc);
560 sc->enabled = 0;
561 }
562 splx(s);
563 }
564
565 int
566 at91usart_open(dev_t dev, int flag, int mode, struct lwp *l)
567 {
568 struct at91usart_softc *sc;
569 struct tty *tp;
570 int s;
571 int error;
572
573 sc = device_lookup_private(&at91usart_cd, COMUNIT(dev));
574 if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK))
575 return (ENXIO);
576
577 if (!device_is_active(sc->sc_dev))
578 return (ENXIO);
579
580 #ifdef KGDB
581 /*
582 * If this is the kgdb port, no other use is permitted.
583 */
584 if (ISSET(sc->sc_hwflags, COM_HW_KGDB))
585 return (EBUSY);
586 #endif
587
588 tp = sc->sc_tty;
589
590 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
591 return (EBUSY);
592
593 s = spltty();
594
595 /*
596 * Do the following iff this is a first open.
597 */
598 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
599 struct termios t;
600
601 tp->t_dev = dev;
602
603 if (sc->enable) {
604 if ((*sc->enable)(sc)) {
605 splx(s);
606 printf("%s: device enable failed\n",
607 device_xname(sc->sc_dev));
608 return (EIO);
609 }
610 sc->enabled = 1;
611 #if 0
612 /* XXXXXXXXXXXXXXX */
613 com_config(sc);
614 #endif
615 }
616
617 /* reset fifos: */
618 AT91PDC_RESET_FIFO(sc->sc_iot, sc->sc_ioh, sc->sc_dmat, US_PDC, &sc->sc_rx_fifo, 0);
619 AT91PDC_RESET_FIFO(sc->sc_iot, sc->sc_ioh, sc->sc_dmat, US_PDC, &sc->sc_tx_fifo, 1);
620
621 /* reset receive */
622 at91usart_writereg(sc, US_CR, US_CR_RSTSTA | US_CR_STTTO);
623
624 /* Turn on interrupts. */
625 sc->sc_ier = US_CSR_ENDRX|US_CSR_RXBUFF|US_CSR_TIMEOUT|US_CSR_RXBRK;
626 at91usart_writereg(sc, US_IER, sc->sc_ier);
627
628 /* enable DMA: */
629 at91usart_writereg(sc, US_PDC + PDC_PTCR, PDC_PTCR_RXTEN);
630
631 /*
632 * Initialize the termios status to the defaults. Add in the
633 * sticky bits from TIOCSFLAGS.
634 */
635 t.c_ispeed = 0;
636 /* if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
637 t.c_ospeed = usart_cn_sc.sc_ospeed;
638 t.c_cflag = usart_cn_sc.sc_cflag;
639 } else*/ {
640 t.c_ospeed = TTYDEF_SPEED;
641 t.c_cflag = TTYDEF_CFLAG;
642 }
643 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
644 SET(t.c_cflag, CLOCAL);
645 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
646 SET(t.c_cflag, CRTSCTS);
647 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
648 SET(t.c_cflag, MDMBUF);
649
650 /* Make sure at91usart_param() will do something. */
651 tp->t_ospeed = 0;
652 (void) at91usart_param(tp, &t);
653 tp->t_iflag = TTYDEF_IFLAG;
654 tp->t_oflag = TTYDEF_OFLAG;
655 tp->t_lflag = TTYDEF_LFLAG;
656 ttychars(tp);
657 ttsetwater(tp);
658
659 /* and unblock. */
660 CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
661
662 #ifdef COM_DEBUG
663 if (at91usart_debug)
664 comstatus(sc, "at91usart_open ");
665 #endif
666
667 }
668
669 splx(s);
670
671 error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
672 if (error)
673 goto bad;
674
675 error = (*tp->t_linesw->l_open)(dev, tp);
676 if (error)
677 goto bad;
678
679 return (0);
680
681 bad:
682 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
683 /*
684 * We failed to open the device, and nobody else had it opened.
685 * Clean up the state as appropriate.
686 */
687 at91usart_shutdown(sc);
688 }
689
690 return (error);
691 }
692
693 int
694 at91usart_close(dev_t dev, int flag, int mode, struct lwp *l)
695 {
696 struct at91usart_softc *sc = device_lookup_private(&at91usart_cd, COMUNIT(dev));
697 struct tty *tp = sc->sc_tty;
698
699 /* XXX This is for cons.c. */
700 if (!ISSET(tp->t_state, TS_ISOPEN))
701 return (0);
702
703 (*tp->t_linesw->l_close)(tp, flag);
704 ttyclose(tp);
705
706 if (COM_ISALIVE(sc) == 0)
707 return (0);
708
709 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
710 /*
711 * Although we got a last close, the device may still be in
712 * use; e.g. if this was the dialout node, and there are still
713 * processes waiting for carrier on the non-dialout node.
714 */
715 at91usart_shutdown(sc);
716 }
717
718 return (0);
719 }
720
721 int
722 at91usart_read(dev_t dev, struct uio *uio, int flag)
723 {
724 struct at91usart_softc *sc = device_lookup_private(&at91usart_cd, COMUNIT(dev));
725 struct tty *tp = sc->sc_tty;
726
727 if (COM_ISALIVE(sc) == 0)
728 return (EIO);
729
730 return ((*tp->t_linesw->l_read)(tp, uio, flag));
731 }
732
733 int
734 at91usart_write(dev_t dev, struct uio *uio, int flag)
735 {
736 struct at91usart_softc *sc = device_lookup_private(&at91usart_cd, COMUNIT(dev));
737 struct tty *tp = sc->sc_tty;
738
739 if (COM_ISALIVE(sc) == 0)
740 return (EIO);
741
742 return ((*tp->t_linesw->l_write)(tp, uio, flag));
743 }
744
745 int
746 at91usart_poll(dev_t dev, int events, struct lwp *l)
747 {
748 struct at91usart_softc *sc = device_lookup_private(&at91usart_cd, COMUNIT(dev));
749 struct tty *tp = sc->sc_tty;
750
751 if (COM_ISALIVE(sc) == 0)
752 return (EIO);
753
754 return ((*tp->t_linesw->l_poll)(tp, events, l));
755 }
756
757 struct tty *
758 at91usart_tty(dev_t dev)
759 {
760 struct at91usart_softc *sc = device_lookup_private(&at91usart_cd, COMUNIT(dev));
761 struct tty *tp = sc->sc_tty;
762
763 return (tp);
764 }
765
766 int
767 at91usart_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
768 {
769 struct at91usart_softc *sc = device_lookup_private(&at91usart_cd, COMUNIT(dev));
770 struct tty *tp = sc->sc_tty;
771 int error;
772 int s;
773
774 if (COM_ISALIVE(sc) == 0)
775 return (EIO);
776
777 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
778 if (error != EPASSTHROUGH)
779 return (error);
780
781 error = ttioctl(tp, cmd, data, flag, l);
782 if (error != EPASSTHROUGH)
783 return (error);
784
785 error = 0;
786
787 s = spltty();
788
789 switch (cmd) {
790 case TIOCSBRK:
791 at91usart_break(sc, 1);
792 break;
793
794 case TIOCCBRK:
795 at91usart_break(sc, 0);
796 break;
797
798 case TIOCGFLAGS:
799 *(int *)data = sc->sc_swflags;
800 break;
801
802 case TIOCSFLAGS:
803 error = kauth_authorize_device_tty(l->l_cred,
804 KAUTH_DEVICE_TTY_PRIVSET, tp);
805 if (error)
806 break;
807 sc->sc_swflags = *(int *)data;
808 break;
809
810 default:
811 error = EPASSTHROUGH;
812 break;
813 }
814
815 splx(s);
816
817 return (error);
818 }
819
820 /*
821 * Stop output on a line.
822 */
823 void
824 at91usart_stop(struct tty *tp, int flag)
825 {
826 struct at91usart_softc *sc
827 = device_lookup_private(&at91usart_cd, COMUNIT(tp->t_dev));
828 int s;
829
830 s = spltty();
831 if (ISSET(tp->t_state, TS_BUSY)) {
832 /* Stop transmitting at the next chunk. */
833 sc->sc_tbc = 0;
834 if (!ISSET(tp->t_state, TS_TTSTOP))
835 SET(tp->t_state, TS_FLUSH);
836 }
837 splx(s);
838 }
839
840 #if 0
841 static u_int
842 cflag2lcrhi(tcflag_t cflag)
843 {
844 uint32_t mr;
845
846 switch (cflag & CSIZE) {
847 default:
848 mr = 0x0;
849 break;
850 }
851 #if 0
852 mr |= (cflag & PARENB) ? LinCtrlHigh_PEN : 0;
853 mr |= (cflag & PARODD) ? 0 : LinCtrlHigh_EPS;
854 mr |= (cflag & CSTOPB) ? LinCtrlHigh_STP2 : 0;
855 mr |= LinCtrlHigh_FEN; /* FIFO always enabled */
856 #endif
857 mr |= USART_MR_PAR_NONE;
858 return (mr);
859 }
860 #endif
861
862
863 static void
864 at91usart_set(struct at91usart_softc *sc)
865 {
866 at91usart_writereg(sc, US_MR, US_MR_CHRL_8 | US_MR_PAR_NONE | US_MR_NBSTOP_1);
867 at91usart_writereg(sc, US_BRGR, sc->sc_brgr);
868 at91usart_writereg(sc, US_CR, US_CR_TXEN | US_CR_RXEN); // @@@ just in case
869 }
870
871 #if NOTYET
872 int
873 at91usart_cn_attach(bus_space_tag_t iot, bus_addr_t iobase, bus_space_handle_t ioh,
874 uint32_t mstclk, int ospeed, tcflag_t cflag)
875 {
876 cn_tab = &at91usart_cons;
877 cn_init_magic(&at91usart_cnm_state);
878 cn_set_magic("\047\001");
879
880 usart_cn_sc.sc_iot = iot;
881 usart_cn_sc.sc_ioh = ioh;
882 usart_cn_sc.sc_hwbase = iobase;
883 usart_cn_sc.sc_ospeed = ospeed;
884 usart_cn_sc.sc_cflag = cflag;
885
886 USART_INIT(mstclk, ospeed);
887
888 return (0);
889 }
890
891 void
892 at91usart_cn_probe(struct consdev *cp)
893 {
894 cp->cn_pri = CN_REMOTE;
895 }
896
897 void
898 at91usart_cn_pollc(dev_t dev, int on)
899 {
900 if (on) {
901 // enable polling mode
902 USARTREG(US_IDR) = USART_INT_RXRDY;
903 } else {
904 // disable polling mode
905 USARTREG(US_IER) = USART_INT_RXRDY;
906 }
907 }
908
909 void
910 at91usart_cn_putc(dev_t dev, int c)
911 {
912 int s;
913 #if 0
914 bus_space_tag_t iot = usart_cn_sc.sc_iot;
915 bus_space_handle_t ioh = usart_cn_sc.sc_ioh;
916 #endif
917 s = spltty();
918
919 USART_PUTC(c);
920
921 #ifdef DEBUG
922 if (c == '\r') {
923 while((USARTREG(USART_SR) & USART_SR_TXEMPTY) == 0)
924 ;
925 }
926 #endif
927
928 splx(s);
929 }
930
931 int
932 at91usart_cn_getc(dev_t dev)
933 {
934 int c, sr;
935 int s;
936 #if 0
937 bus_space_tag_t iot = usart_cn_sc.sc_iot;
938 bus_space_handle_t ioh = usart_cn_sc.sc_ioh;
939 #endif
940
941 s = spltty();
942
943 while ((c = USART_PEEKC()) == -1) {
944 splx(s);
945 s = spltty();
946 }
947 ;
948 sr = USARTREG(USART_SR);
949 if (ISSET(sr, USART_SR_FRAME) && c == 0) {
950 USARTREG(USART_CR) = USART_CR_RSTSTA; // reset status bits
951 c = CNC_BREAK;
952 }
953 #ifdef DDB
954 extern int db_active;
955 if (!db_active)
956 #endif
957 {
958 int cn_trapped = 0; /* unused */
959
960 cn_check_magic(dev, c, at91usart_cnm_state);
961 }
962 splx(s);
963
964 c &= 0xff;
965
966 return (c);
967 }
968 #endif /* NOTYET */
969
970 inline static void
971 at91usart_rxsoft(struct at91usart_softc *sc, struct tty *tp, unsigned csr)
972 {
973 u_char *start, *get, *end;
974 int cc;
975
976 AT91PDC_FIFO_POSTREAD(sc->sc_iot, sc->sc_ioh, sc->sc_dmat, US_PDC,
977 &sc->sc_rx_fifo);
978
979 if (ISSET(csr, US_CSR_TIMEOUT | US_CSR_RXBRK))
980 at91usart_rx_stopped(sc);
981
982 while ((start = AT91PDC_FIFO_RDPTR(&sc->sc_rx_fifo, &cc)) != NULL) {
983 int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
984 int code;
985
986 if (!ISSET(csr, US_CSR_TIMEOUT | US_CSR_RXBRK))
987 at91usart_rx_started(sc);
988
989 for (get = start, end = start + cc; get < end; get++) {
990 code = *get;
991 if ((*rint)(code, tp) == -1) {
992 /*
993 * The line discipline's buffer is out of space.
994 */
995 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
996 /*
997 * We're either not using flow control, or the
998 * line discipline didn't tell us to block for
999 * some reason. Either way, we have no way to
1000 * know when there's more space available, so
1001 * just drop the rest of the data.
1002 */
1003 get = end;
1004 printf("%s: receive missing data!\n",
1005 device_xname(sc->sc_dev));
1006 } else {
1007 /*
1008 * Don't schedule any more receive processing
1009 * until the line discipline tells us there's
1010 * space available (through comhwiflow()).
1011 * Leave the rest of the data in the input
1012 * buffer.
1013 */
1014 SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1015 }
1016 break;
1017 }
1018 }
1019
1020 // tell we've read some bytes...
1021 AT91PDC_FIFO_READ(&sc->sc_rx_fifo, get - start);
1022
1023 if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED))
1024 break;
1025 }
1026
1027 // h/w flow control hook:
1028 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
1029 at91usart_rx_rts_ctl(sc, (AT91PDC_FIFO_SPACE(&sc->sc_rx_fifo) > PDC_BLOCK_SIZE * 2));
1030
1031 // write next pointer if USART is ready:
1032 if (AT91PDC_FIFO_PREREAD(sc->sc_iot, sc->sc_ioh, sc->sc_dmat, US_PDC,
1033 &sc->sc_rx_fifo, PDC_BLOCK_SIZE)) {
1034 SET(sc->sc_ier, US_CSR_ENDRX | US_CSR_RXBUFF | US_CSR_TIMEOUT | US_CSR_RXBRK);
1035 } else {
1036 CLR(sc->sc_ier, US_CSR_ENDRX | US_CSR_RXBUFF | US_CSR_TIMEOUT | US_CSR_RXBRK);
1037 }
1038 }
1039
1040 inline static void
1041 at91usart_txsoft(struct at91usart_softc *sc, struct tty *tp)
1042 {
1043 at91usart_filltx(sc);
1044 if (!ISSET(tp->t_state, TS_BUSY))
1045 (*tp->t_linesw->l_start)(tp);
1046 }
1047
1048
1049 static void
1050 at91usart_soft(void* arg)
1051 {
1052 struct at91usart_softc *sc = arg;
1053 int s;
1054 u_int csr;
1055
1056 if (COM_ISALIVE(sc) == 0)
1057 return;
1058
1059 s = spltty();
1060 csr = sc->sc_csr;
1061 while (csr != 0) {
1062 if ((csr &= sc->sc_ier) == 0)
1063 break;
1064 // splx(s);
1065 DPRINTFN(5, ("%s: %s / csr = 0x%08x\n", device_xname(sc->sc_dev), __FUNCTION__, csr));
1066 if (ISSET(csr, US_CSR_ENDRX | US_CSR_RXBUFF | US_CSR_TIMEOUT | US_CSR_RXBRK)) {
1067 /* receive interrupt */
1068 if (ISSET(csr, US_CSR_RXBRK)) {
1069 // break received!
1070 at91usart_writereg(sc, US_CR, US_CR_RSTSTA | US_CR_STTTO);
1071 } else if (ISSET(csr, US_CSR_TIMEOUT)) {
1072 // timeout received
1073 at91usart_writereg(sc, US_CR, US_CR_STTTO);
1074 }
1075 at91usart_rxsoft(sc, sc->sc_tty, csr);
1076 }
1077 if (ISSET(csr, US_CSR_TXEMPTY)) {
1078 at91usart_stop_tx(sc);
1079 CLR(sc->sc_ier, US_CSR_TXEMPTY);
1080 if (AT91PDC_FIFO_EMPTY(&sc->sc_tx_fifo)) {
1081 // everything sent!
1082 if (ISSET(sc->sc_tty->t_state, TS_FLUSH))
1083 CLR(sc->sc_tty->t_state, TS_FLUSH);
1084 }
1085 }
1086 if (ISSET(csr, US_CSR_TXEMPTY | US_CSR_ENDTX)) {
1087 /* transmit interrupt! */
1088 at91usart_txsoft(sc, sc->sc_tty);
1089 }
1090 // s = spltty();
1091 csr = at91usart_readreg(sc, US_CSR);
1092 }
1093 sc->sc_csr = 0;
1094 at91usart_writereg(sc, US_IER, sc->sc_ier); // re-enable interrupts
1095 splx(s);
1096 }
1097
1098
1099 static int
1100 at91usart_intr(void* arg)
1101 {
1102 struct at91usart_softc *sc = arg;
1103 u_int csr, imr;
1104
1105 // get out if interrupts are not enabled
1106 imr = at91usart_readreg(sc, US_IMR);
1107 if (!imr)
1108 return 0;
1109 // get out if pending interrupt is not enabled
1110 csr = at91usart_readreg(sc, US_CSR);
1111 DPRINTFN(6,("%s: csr=%08X imr=%08X\n", device_xname(sc->sc_dev), csr, imr));
1112 if (!ISSET(csr, imr))
1113 return 0;
1114
1115 // ok, we DO have some interrupts to serve! let softint do it
1116 sc->sc_csr = csr;
1117 at91usart_writereg(sc, US_IDR, -1);
1118
1119 /* Wake up the poller. */
1120 softint_schedule(sc->sc_si);
1121
1122 /* we're done for now */
1123 return (1);
1124
1125 }
1126