1 /* $OpenBSD: com_leioc.c,v 1.2 2017/02/19 10:18:41 visa Exp $ */
2
3 /*
4 * Copyright (c) 2016 Visa Hankala
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <sys/param.h>
20 #include <sys/systm.h>
21 #include <sys/conf.h>
22 #include <sys/device.h>
23 #include <sys/termios.h>
24 #include <sys/tty.h>
25
26 #include <machine/autoconf.h>
27 #include <machine/bus.h>
28 #include <machine/loongson3.h>
29
30 #include <dev/ic/comreg.h>
31 #include <dev/ic/comvar.h>
32
33 #include <loongson/dev/leiocreg.h>
34 #include <loongson/dev/leiocvar.h>
35
36 #define UART_FREQ 33000000
37 #define UART_RATE 115200
38
39 int com_leioc_match(struct device *, void *, void *);
40 void com_leioc_attach(struct device *, struct device *, void *);
41 int com_leioc_intr(void *);
42
43 const struct cfattach com_leioc_ca = {
44 sizeof(struct com_softc), com_leioc_match, com_leioc_attach
45 };
46
47 extern struct cfdriver com_cd;
48
49 int
com_leioc_match(struct device * parent,void * match,void * aux)50 com_leioc_match(struct device *parent, void *match, void *aux)
51 {
52 struct leioc_attach_args *laa = aux;
53
54 if (strcmp(laa->laa_name, com_cd.cd_name) == 0)
55 return 1;
56
57 return 0;
58 }
59
60 void
com_leioc_attach(struct device * parent,struct device * self,void * aux)61 com_leioc_attach(struct device *parent, struct device *self, void *aux)
62 {
63 struct leioc_attach_args *laa = aux;
64 struct com_softc *sc = (void *)self;
65 int console = 0;
66
67 if (comconsiot == &leioc_io_space_tag && comconsaddr == laa->laa_base)
68 console = 1;
69
70 sc->sc_hwflags = 0;
71 sc->sc_swflags = 0;
72 sc->sc_frequency = UART_FREQ;
73
74 if (!console || comconsattached) {
75 sc->sc_iot = &leioc_io_space_tag;
76 sc->sc_iobase = laa->laa_base;
77 if (bus_space_map(sc->sc_iot, sc->sc_iobase, COM_NPORTS, 0,
78 &sc->sc_ioh)) {
79 printf(": could not map UART registers\n");
80 return;
81 }
82 } else {
83 /* Reuse the early console settings. */
84 sc->sc_iot = comconsiot;
85 sc->sc_iobase = comconsaddr;
86 if (comcnattach(sc->sc_iot, sc->sc_iobase, comconsrate,
87 sc->sc_frequency, comconscflag))
88 panic("could not set up serial console");
89 sc->sc_ioh = comconsioh;
90 }
91
92 com_attach_subr(sc);
93
94 loongson3_intr_establish(LS3_IRQ_LPC, IPL_TTY, com_leioc_intr, sc,
95 sc->sc_dev.dv_xname);
96 }
97
98 int
com_leioc_intr(void * arg)99 com_leioc_intr(void *arg)
100 {
101 comintr(arg);
102
103 /*
104 * Always return non-zero to prevent console clutter about spurious
105 * interrupts. comstart() enables the transmitter holding register
106 * empty interrupt before adding data to the FIFO, which can trigger
107 * a premature interrupt on the primary CPU in a multiprocessor system.
108 */
109 return 1;
110 }
111
112 void
leioc_cons_setup(void)113 leioc_cons_setup(void)
114 {
115 comconsiot = &leioc_io_space_tag;
116 comconsaddr = LEIOC_UART0_BASE;
117 comconsfreq = UART_FREQ;
118 comconsrate = UART_RATE;
119 comconscflag = (TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) |
120 CS8 | CLOCAL;
121 }
122