octeon_uart.c revision 1.8 1 /* $NetBSD: octeon_uart.c,v 1.8 2020/06/19 02:23:43 simonb Exp $ */
2
3 /*
4 * Copyright (c) 2007 Internet Initiative Japan, Inc.
5 * All rights reserved.
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 THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: octeon_uart.c,v 1.8 2020/06/19 02:23:43 simonb Exp $");
31
32 #include "opt_octeon.h"
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/types.h>
37 #include <sys/device.h>
38 #include <sys/tty.h>
39
40 #include <sys/bus.h>
41 #include <sys/cpu.h>
42 #include <machine/intr.h>
43
44 #include <dev/ic/comreg.h>
45 #include <dev/ic/comvar.h>
46
47 #include <mips/cavium/include/iobusvar.h>
48 #include <mips/cavium/dev/octeon_uartreg.h>
49 #include <mips/cavium/dev/octeon_ciureg.h>
50
51 struct octuart_iobus_softc {
52 struct com_softc sc_com;
53 int sc_irq;
54 void *sc_ih;
55 };
56
57 static int octuart_iobus_match(device_t, struct cfdata *, void *);
58 static void octuart_iobus_attach(device_t, device_t, void *);
59 static int octuart_com_enable(struct com_softc *);
60 static void octuart_com_disable(struct com_softc *);
61
62
63 /* XXX */
64 int octuart_com_cnattach(bus_space_tag_t, int, int);
65
66 /* XXX */
67 const bus_addr_t octuart_com_bases[] = {
68 MIO_UART0_BASE,
69 MIO_UART1_BASE
70 };
71 const struct com_regs octuart_com_regs = {
72 .cr_nports = COM_NPORTS,
73 .cr_map = {
74 [COM_REG_RXDATA] = MIO_UART_RBR_OFFSET,
75 [COM_REG_TXDATA] = MIO_UART_THR_OFFSET,
76 [COM_REG_DLBL] = MIO_UART_DLL_OFFSET,
77 [COM_REG_DLBH] = MIO_UART_DLH_OFFSET,
78 [COM_REG_IER] = MIO_UART_IER_OFFSET,
79 [COM_REG_IIR] = MIO_UART_IIR_OFFSET,
80 [COM_REG_FIFO] = MIO_UART_FCR_OFFSET,
81 [COM_REG_EFR] = 0,
82 [COM_REG_LCR] = MIO_UART_LCR_OFFSET,
83 [COM_REG_MCR] = MIO_UART_MCR_OFFSET,
84 [COM_REG_LSR] = MIO_UART_LSR_OFFSET,
85 [COM_REG_MSR] = MIO_UART_MSR_OFFSET,
86 #if 0 /* XXX COM_TYPE_16750_NOERS */
87 [COM_REG_USR] = MIO_UART_USR_OFFSET,
88 [COM_REG_SRR] = MIO_UART_SRR_OFFSET
89 #endif
90 }
91 };
92
93 CFATTACH_DECL_NEW(com_iobus, sizeof(struct octuart_iobus_softc),
94 octuart_iobus_match, octuart_iobus_attach, NULL, NULL);
95
96 int
97 octuart_iobus_match(device_t parent, struct cfdata *cf, void *aux)
98 {
99 struct iobus_attach_args *aa = aux;
100 int result = 0;
101
102 if (strcmp(cf->cf_name, aa->aa_name) != 0)
103 goto out;
104 if (cf->cf_unit != aa->aa_unitno)
105 goto out;
106 result = 1;
107
108 out:
109 return result;
110 }
111
112 void
113 octuart_iobus_attach(device_t parent, device_t self, void *aux)
114 {
115 struct octuart_iobus_softc *sc = device_private(self);
116 struct com_softc *sc_com = &sc->sc_com;
117 struct iobus_attach_args *aa = aux;
118 int status;
119
120 sc_com->sc_dev = self;
121 sc_com->sc_regs = octuart_com_regs;
122 sc_com->sc_regs.cr_iot = aa->aa_bust;
123 sc_com->sc_regs.cr_iobase = aa->aa_unit->addr;
124
125 sc->sc_irq = aa->aa_unit->irq;
126
127 status = bus_space_map(
128 aa->aa_bust,
129 aa->aa_unit->addr,
130 COM_NPORTS,
131 0,
132 &sc_com->sc_regs.cr_ioh);
133 if (status != 0) {
134 aprint_error(": can't map i/o space\n");
135 return;
136 }
137
138 sc_com->sc_type = COM_TYPE_16550_NOERS;
139 sc_com->sc_frequency = octeon_ioclock_speed();
140 sc_com->enable = octuart_com_enable;
141 sc_com->disable = octuart_com_disable;
142
143 octuart_com_enable(sc_com);
144 sc_com->enabled = 1;
145
146 com_attach_subr(sc_com);
147
148 sc->sc_ih = octeon_intr_establish(CIU_INT_UART_0 + device_unit(self),
149 IPL_SERIAL, comintr, sc_com);
150 if (sc->sc_ih == NULL)
151 panic("%s: can't establish interrupt\n",
152 device_xname(self));
153
154 /* XXX disable if kgdb? */
155 }
156
157 #if 0
158 void
159 octuart_iobus_detach(device_t self, ...)
160 {
161 struct octuart_iobus_softc *sc = (void *)self;
162
163 octeon_intr_disestablish(sc->ih);
164 }
165 #endif
166
167 int
168 octuart_com_enable(struct com_softc *sc_com)
169 {
170 struct com_regs *regsp = &sc_com->sc_regs;
171
172 /* XXX Clear old busy detect interrupts */
173 bus_space_read_1(regsp->cr_iot, regsp->cr_ioh,
174 MIO_UART_USR_OFFSET);
175
176 return 0;
177 }
178
179 void
180 octuart_com_disable(struct com_softc *sc_com)
181 {
182 /*
183 * XXX chip specific procedure
184 */
185 }
186
187
188 #ifndef CONMODE
189 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
190 #endif
191
192 int
193 octuart_com_cnattach(bus_space_tag_t bust, int portno, int speed)
194 {
195 struct com_regs regs;
196
197 (void)memcpy(®s, &octuart_com_regs, sizeof(regs));
198 regs.cr_iot = bust;
199 regs.cr_iobase = octuart_com_bases[portno];
200
201 return comcnattach1(
202 ®s,
203 speed,
204 octeon_ioclock_speed(),
205 COM_TYPE_16550_NOERS,
206 CONMODE);
207 }
208