xref: /netbsd-src/sys/arch/mips/cavium/dev/octeon_uart.c (revision 42b9e898991e23b560315a9b1da6a36a39d4351b)
1 /*	$NetBSD: octeon_uart.c,v 1.9 2020/06/23 05:18: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.9 2020/06/23 05:18:43 simonb Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/types.h>
35 #include <sys/device.h>
36 #include <sys/tty.h>
37 
38 #include <sys/bus.h>
39 #include <sys/cpu.h>
40 #include <machine/intr.h>
41 
42 #include <dev/cons.h>
43 #include <dev/ic/comreg.h>
44 #include <dev/ic/comvar.h>
45 
46 #include <mips/cavium/include/iobusvar.h>
47 #include <mips/cavium/dev/octeon_uartreg.h>
48 #include <mips/cavium/dev/octeon_uartvar.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 /* octputc() is not declared static so it can be used for debugging elsewhere */
63 void		octputc(dev_t, int);
64 
65 /* XXX */
66 const bus_addr_t octuart_com_bases[] = {
67 	MIO_UART0_BASE,
68 	MIO_UART1_BASE
69 };
70 const struct com_regs octuart_com_regs = {
71 	.cr_nports = COM_NPORTS,
72 	.cr_map = {
73 		[COM_REG_RXDATA] =	MIO_UART_RBR_OFFSET,
74 		[COM_REG_TXDATA] =	MIO_UART_THR_OFFSET,
75 		[COM_REG_DLBL] =	MIO_UART_DLL_OFFSET,
76 		[COM_REG_DLBH] =	MIO_UART_DLH_OFFSET,
77 		[COM_REG_IER] =		MIO_UART_IER_OFFSET,
78 		[COM_REG_IIR] =		MIO_UART_IIR_OFFSET,
79 		[COM_REG_FIFO] =	MIO_UART_FCR_OFFSET,
80 		[COM_REG_EFR] =		0,
81 		[COM_REG_LCR] =		MIO_UART_LCR_OFFSET,
82 		[COM_REG_MCR] =		MIO_UART_MCR_OFFSET,
83 		[COM_REG_LSR] =		MIO_UART_LSR_OFFSET,
84 		[COM_REG_MSR] =		MIO_UART_MSR_OFFSET,
85 #if 0 /* XXX COM_TYPE_16750_NOERS */
86 		[COM_REG_USR] =		MIO_UART_USR_OFFSET,
87 		[COM_REG_SRR] =		MIO_UART_SRR_OFFSET
88 #endif
89 	}
90 };
91 
92 CFATTACH_DECL_NEW(com_iobus, sizeof(struct octuart_iobus_softc),
93     octuart_iobus_match, octuart_iobus_attach, NULL, NULL);
94 
95 static int
96 octuart_iobus_match(device_t parent, struct cfdata *cf, void *aux)
97 {
98 	struct iobus_attach_args *aa = aux;
99 	int result = 0;
100 
101 	if (strcmp(cf->cf_name, aa->aa_name) != 0)
102 		goto out;
103 	if (cf->cf_unit != aa->aa_unitno)
104 		goto out;
105 	result = 1;
106 
107 out:
108 	return result;
109 }
110 
111 static void
112 octuart_iobus_attach(device_t parent, device_t self, void *aux)
113 {
114 	struct octuart_iobus_softc *sc = device_private(self);
115 	struct com_softc *sc_com = &sc->sc_com;
116 	struct iobus_attach_args *aa = aux;
117 	int status;
118 
119 	sc_com->sc_dev = self;
120 	sc_com->sc_regs = octuart_com_regs;
121 	sc_com->sc_regs.cr_iot = aa->aa_bust;
122 	sc_com->sc_regs.cr_iobase = aa->aa_unit->addr;
123 
124 	sc->sc_irq = aa->aa_unit->irq;
125 
126 	status = bus_space_map(
127 		aa->aa_bust,
128 		aa->aa_unit->addr,
129 		COM_NPORTS,
130 		0,
131 		&sc_com->sc_regs.cr_ioh);
132 	if (status != 0) {
133 		aprint_error(": can't map i/o space\n");
134 		return;
135 	}
136 
137 	sc_com->sc_type = COM_TYPE_16550_NOERS;
138 	sc_com->sc_frequency = octeon_ioclock_speed();
139 	sc_com->enable = octuart_com_enable;
140 	sc_com->disable = octuart_com_disable;
141 
142 	octuart_com_enable(sc_com);
143 	sc_com->enabled = 1;
144 
145 	com_attach_subr(sc_com);
146 
147 	sc->sc_ih = octeon_intr_establish(CIU_INT_UART_0 + device_unit(self),
148 	    IPL_SERIAL, comintr, sc_com);
149 	if (sc->sc_ih == NULL)
150 		panic("%s: can't establish interrupt\n",
151 		    device_xname(self));
152 
153 	/* XXX disable if kgdb? */
154 }
155 
156 static int
157 octuart_com_enable(struct com_softc *sc_com)
158 {
159 	struct com_regs *regsp = &sc_com->sc_regs;
160 
161 	/* XXX Clear old busy detect interrupts */
162 	bus_space_read_1(regsp->cr_iot, regsp->cr_ioh,
163 	    MIO_UART_USR_OFFSET);
164 
165 	return 0;
166 }
167 
168 static void
169 octuart_com_disable(struct com_softc *sc_com)
170 {
171 	/*
172 	 * XXX chip specific procedure
173 	 */
174 }
175 
176 
177 #ifndef CONMODE
178 #define	CONMODE	((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
179 #endif
180 
181 int
182 octuart_com_cnattach(bus_space_tag_t bust, int portno, int speed)
183 {
184 	struct com_regs regs;
185 
186 	(void)memcpy(&regs, &octuart_com_regs, sizeof(regs));
187 	regs.cr_iot = bust;
188 	regs.cr_iobase = octuart_com_bases[portno];
189 
190 	return comcnattach1(
191 		&regs,
192 		speed,
193 		octeon_ioclock_speed(),
194 		COM_TYPE_16550_NOERS,
195 		CONMODE);
196 }
197 
198 
199 /*
200  * A very simple output-only console so early printf() can work.
201  */
202 struct consdev early_console = {
203 	.cn_putc = octputc,
204 	.cn_pollc = nullcnpollc,
205 	.cn_dev = makedev(0, 0),
206 	.cn_pri = CN_DEAD
207 };
208 static int early_comcnrate;
209 
210 void
211 octputc(dev_t dev, int c)
212 {
213 
214 	octeon_xkphys_write_8(MIO_UART0_RBR, (uint8_t)c);
215 	delay(1000000 / (early_comcnrate / 10)); /* wait for char to drain */
216 }
217 
218 void
219 octuart_early_cnattach(int rate)
220 {
221 
222 	early_comcnrate = rate;
223 	cn_tab = &early_console;
224 }
225