xref: /freebsd/sys/dev/uart/uart_dev_mu.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 /*-
2  * Copyright (c) 2018 Diane Bruce
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 /*
27  * Based on uart_dev_pl011.c
28  * Copyright (c) 2012 Semihalf.
29  * All rights reserved.
30  */
31 /*
32  * The mini Uart has the following features:
33  * - 7 or 8 bit operation.
34  * - 1 start and 1 stop bit.
35  * - No parities.
36  * - Break generation.
37  * - 8 symbols deep FIFOs for receive and transmit.
38  * - SW controlled RTS, SW readable CTS.
39  * - Auto flow control with programmable FIFO level.
40  * - 16550 like registers.
41  * - Baudrate derived from system clock.
42  * This is a mini UART and it does NOT have the following capabilities:
43  * - Break detection
44  * - Framing errors detection.
45  * - Parity bit
46  * - Receive Time-out interrupt
47  * - DCD, DSR, DTR or RI signals.
48  * The implemented UART is not a 16650 compatible UART However as far
49  * as possible the first 8 control and status registers are laid out
50  * like a 16550 UART. All 16550 register bits which are not supported can
51  * be written but will be ignored and read back as 0. All control bits
52  * for simple UART receive/transmit operations are available.
53  */
54 
55 #include "opt_acpi.h"
56 #include "opt_platform.h"
57 
58 #include <sys/cdefs.h>
59 __FBSDID("$FreeBSD$");
60 
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/kernel.h>
64 #include <sys/bus.h>
65 
66 #include <machine/bus.h>
67 #include <machine/machdep.h>
68 #include <machine/pcpu.h>
69 
70 #include <dev/uart/uart.h>
71 #include <dev/uart/uart_cpu.h>
72 #ifdef FDT
73 #include <dev/uart/uart_cpu_fdt.h>
74 #include <dev/ofw/ofw_bus.h>
75 #endif
76 #include <dev/uart/uart_bus.h>
77 #include "uart_if.h"
78 
79 /* BCM2835 Micro UART registers and masks*/
80 #define	AUX_MU_IO_REG		0x00		/* I/O register */
81 
82 /*
83  * According to errata bits 1 and 2 are swapped,
84  * Also bits 2 and 3 are required to enable interrupts.
85  */
86 #define	AUX_MU_IER_REG		0x01
87 #define IER_RXENABLE		(1)
88 #define IER_TXENABLE		(1<<1)
89 #define IER_REQUIRED		(3<<2)
90 #define IER_MASK_ALL		(IER_TXENABLE|IER_RXENABLE)
91 
92 #define	AUX_MU_IIR_REG		0x02
93 #define IIR_READY		(1)
94 #define IIR_TXREADY		(1<<1)
95 #define IIR_RXREADY		(1<<2)
96 #define IIR_CLEAR		(3<<1)
97 
98 #define	AUX_MU_LCR_REG		0x03
99 #define LCR_WLEN7		(0)
100 #define LCR_WLEN8		(3)
101 
102 #define AUX_MU_MCR_REG		0x04
103 #define AUX_MCR_RTS		(1<<1)
104 
105 #define AUX_MU_LSR_REG		0x05
106 #define LSR_RXREADY		(1)
107 #define LSR_OVRRUN		(1<<1)
108 #define LSR_TXEMPTY		(1<<5)
109 #define LSR_TXIDLE		(1<<6)
110 
111 #define AUX_MU_MSR_REG		0x06
112 #define MSR_CTS			(1<<5)
113 
114 #define AUX_MU_SCRATCH_REG	0x07
115 
116 #define AUX_MU_CNTL_REG		0x08
117 #define CNTL_RXENAB		(1)
118 #define CNTL_TXENAB		(1<<1)
119 
120 #define AUX_MU_STAT_REG		0x09
121 #define STAT_TX_SA		(1<<1)
122 #define STAT_RX_SA		(1)
123 
124 #define AUX_MU_BAUD_REG		0x0a
125 
126 /*
127  * FIXME: actual register size is SoC-dependent, we need to handle it
128  */
129 #define	__uart_getreg(bas, reg)		\
130 	bus_space_read_4((bas)->bst, (bas)->bsh, uart_regofs(bas, reg))
131 #define	__uart_setreg(bas, reg, value)	\
132 	bus_space_write_4((bas)->bst, (bas)->bsh, uart_regofs(bas, reg), value)
133 
134 /*
135  * Low-level UART interface.
136  */
137 static int uart_mu_probe(struct uart_bas *bas);
138 static void uart_mu_init(struct uart_bas *bas, int, int, int, int);
139 static void uart_mu_term(struct uart_bas *bas);
140 static void uart_mu_putc(struct uart_bas *bas, int);
141 static int uart_mu_rxready(struct uart_bas *bas);
142 static int uart_mu_getc(struct uart_bas *bas, struct mtx *);
143 
144 static struct uart_ops uart_mu_ops = {
145 	.probe = uart_mu_probe,
146 	.init = uart_mu_init,
147 	.term = uart_mu_term,
148 	.putc = uart_mu_putc,
149 	.rxready = uart_mu_rxready,
150 	.getc = uart_mu_getc,
151 };
152 
153 static int
154 uart_mu_probe(struct uart_bas *bas)
155 {
156 
157 	return (0);
158 }
159 
160 /*
161  * According to the docs, the cpu clock is locked to 250Mhz when
162  * the micro-uart is used
163  */
164 #define CPU_CLOCK	250000000
165 
166 static void
167 uart_mu_param(struct uart_bas *bas, int baudrate, int databits, int stopbits,
168     int parity)
169 {
170 	uint32_t line;
171 	uint32_t baud;
172 
173 	/*
174 	 * Zero all settings to make sure
175 	 * UART is disabled and not configured
176 	 */
177 	line = 0x0;
178 	__uart_setreg(bas, AUX_MU_CNTL_REG, line);
179 
180 	/* As I know UART is disabled I can setup the line */
181 	switch (databits) {
182 	case 7:
183 		line |= LCR_WLEN7;
184 		break;
185 	case 6:
186 	case 8:
187 	default:
188 		line |= LCR_WLEN8;
189 		break;
190 	}
191 
192 	__uart_setreg(bas, AUX_MU_LCR_REG, line);
193 
194 	/* See 2.2.1 BCM2835-ARM-Peripherals baudrate */
195 	if (baudrate != 0) {
196 		baud = CPU_CLOCK / (8 * baudrate);
197 		/* XXX
198 		 *  baud = cpu_clock() / (8 * baudrate);
199 		 */
200 		__uart_setreg(bas, AUX_MU_BAUD_REG, ((uint32_t)(baud & 0xFFFF)));
201 	}
202 
203 	/* re-enable UART */
204 	__uart_setreg(bas, AUX_MU_CNTL_REG, CNTL_RXENAB|CNTL_TXENAB);
205 }
206 
207 static void
208 uart_mu_init(struct uart_bas *bas, int baudrate, int databits, int stopbits,
209     int parity)
210 {
211 
212 	/* Mask all interrupts */
213 	__uart_setreg(bas, AUX_MU_IER_REG, 0);
214 	uart_mu_param(bas, baudrate, databits, stopbits, parity);
215 }
216 
217 static void
218 uart_mu_term(struct uart_bas *bas)
219 {
220 }
221 
222 static void
223 uart_mu_putc(struct uart_bas *bas, int c)
224 {
225 
226 	/* Wait when TX FIFO full. Push character otherwise. */
227 	while ((__uart_getreg(bas, AUX_MU_LSR_REG) & LSR_TXEMPTY) == 0)
228 		;
229 	__uart_setreg(bas, AUX_MU_IO_REG, c & 0xff);
230 }
231 
232 static int
233 uart_mu_rxready(struct uart_bas *bas)
234 {
235 
236 	return ((__uart_getreg(bas, AUX_MU_LSR_REG) & LSR_RXREADY) != 0);
237 }
238 
239 static int
240 uart_mu_getc(struct uart_bas *bas, struct mtx *hwmtx)
241 {
242 	int c;
243 
244 	while(!uart_mu_rxready(bas))
245 		;
246 	c = __uart_getreg(bas, AUX_MU_IO_REG) & 0xff;
247 	return (c);
248 }
249 
250 /*
251  * High-level UART interface.
252  */
253 struct uart_mu_softc {
254 	struct uart_softc	bas;
255   	uint16_t		aux_ier; /* Interrupt mask */
256 };
257 
258 static int uart_mu_bus_attach(struct uart_softc *);
259 static int uart_mu_bus_detach(struct uart_softc *);
260 static int uart_mu_bus_flush(struct uart_softc *, int);
261 static int uart_mu_bus_getsig(struct uart_softc *);
262 static int uart_mu_bus_ioctl(struct uart_softc *, int, intptr_t);
263 static int uart_mu_bus_ipend(struct uart_softc *);
264 static int uart_mu_bus_param(struct uart_softc *, int, int, int, int);
265 static int uart_mu_bus_probe(struct uart_softc *);
266 static int uart_mu_bus_receive(struct uart_softc *);
267 static int uart_mu_bus_setsig(struct uart_softc *, int);
268 static int uart_mu_bus_transmit(struct uart_softc *);
269 static void uart_mu_bus_grab(struct uart_softc *);
270 static void uart_mu_bus_ungrab(struct uart_softc *);
271 
272 static kobj_method_t uart_mu_methods[] = {
273 	KOBJMETHOD(uart_attach,		uart_mu_bus_attach),
274 	KOBJMETHOD(uart_detach,		uart_mu_bus_detach),
275 	KOBJMETHOD(uart_flush,		uart_mu_bus_flush),
276 	KOBJMETHOD(uart_getsig,		uart_mu_bus_getsig),
277 	KOBJMETHOD(uart_ioctl,		uart_mu_bus_ioctl),
278 	KOBJMETHOD(uart_ipend,		uart_mu_bus_ipend),
279 	KOBJMETHOD(uart_param,		uart_mu_bus_param),
280 	KOBJMETHOD(uart_probe,		uart_mu_bus_probe),
281 	KOBJMETHOD(uart_receive,	uart_mu_bus_receive),
282 	KOBJMETHOD(uart_setsig,		uart_mu_bus_setsig),
283 	KOBJMETHOD(uart_transmit,	uart_mu_bus_transmit),
284 	KOBJMETHOD(uart_grab,		uart_mu_bus_grab),
285 	KOBJMETHOD(uart_ungrab,		uart_mu_bus_ungrab),
286 	{ 0, 0 }
287 };
288 
289 static struct uart_class uart_mu_class = {
290 	"aux-uart",
291 	uart_mu_methods,
292 	sizeof(struct uart_mu_softc),
293 	.uc_ops = &uart_mu_ops,
294 	.uc_range = 0x48,
295 	.uc_rclk = 0,
296 	.uc_rshift = 2
297 };
298 
299 #ifdef FDT
300 static struct ofw_compat_data fdt_compat_data[] = {
301 	{"brcm,bcm2835-aux-uart" , (uintptr_t)&uart_mu_class},
302 	{NULL,			   (uintptr_t)NULL},
303 };
304 UART_FDT_CLASS_AND_DEVICE(fdt_compat_data);
305 #endif
306 
307 static int
308 uart_mu_bus_attach(struct uart_softc *sc)
309 {
310 	struct uart_mu_softc *psc;
311 	struct uart_bas *bas;
312 
313 	psc = (struct uart_mu_softc *)sc;
314 	bas = &sc->sc_bas;
315 	/* Clear interrupts */
316 	__uart_setreg(bas, AUX_MU_IIR_REG, IIR_CLEAR);
317 	/* Enable interrupts */
318 	psc->aux_ier = (IER_RXENABLE|IER_TXENABLE|IER_REQUIRED);
319 	__uart_setreg(bas, AUX_MU_IER_REG, psc->aux_ier);
320 	sc->sc_txbusy = 0;
321 
322 	return (0);
323 }
324 
325 static int
326 uart_mu_bus_detach(struct uart_softc *sc)
327 {
328 
329 	return (0);
330 }
331 
332 static int
333 uart_mu_bus_flush(struct uart_softc *sc, int what)
334 {
335 
336 	return (0);
337 }
338 
339 static int
340 uart_mu_bus_getsig(struct uart_softc *sc)
341 {
342 
343 	return (0);
344 }
345 
346 static int
347 uart_mu_bus_ioctl(struct uart_softc *sc, int request, intptr_t data)
348 {
349 	int error;
350 
351 	error = 0;
352 	uart_lock(sc->sc_hwmtx);
353 	switch (request) {
354 	case UART_IOCTL_BREAK:
355 		break;
356 	case UART_IOCTL_BAUD:
357 		*(int*)data = 115200;
358 		break;
359 	default:
360 		error = EINVAL;
361 		break;
362 	}
363 	uart_unlock(sc->sc_hwmtx);
364 
365 	return (error);
366 }
367 
368 static int
369 uart_mu_bus_ipend(struct uart_softc *sc)
370 {
371 	struct uart_mu_softc *psc;
372 	struct uart_bas *bas;
373 	uint32_t ints;
374 	int ipend;
375 
376 	psc = (struct uart_mu_softc *)sc;
377 	bas = &sc->sc_bas;
378 
379 	uart_lock(sc->sc_hwmtx);
380 	ints = __uart_getreg(bas, AUX_MU_IIR_REG);
381 	ipend = 0;
382 
383 	/*
384 	 * According to docs only one of IIR_RXREADY
385 	 * or IIR_TXREADY are valid eg. Only one or the other.
386 	 */
387 	if (ints & IIR_RXREADY) {
388 		ipend |= SER_INT_RXREADY;
389 	} else if (ints & IIR_TXREADY) {
390 		if (__uart_getreg(bas, AUX_MU_LSR_REG) & LSR_TXIDLE) {
391 			if (sc->sc_txbusy)
392 				ipend |= SER_INT_TXIDLE;
393 
394 			/* Disable TX interrupt */
395 			__uart_setreg(bas, AUX_MU_IER_REG,
396 				      psc->aux_ier & ~IER_TXENABLE);
397 		}
398 	}
399 
400 	uart_unlock(sc->sc_hwmtx);
401 
402 	return (ipend);
403 }
404 
405 static int
406 uart_mu_bus_param(struct uart_softc *sc, int baudrate, int databits,
407     int stopbits, int parity)
408 {
409 
410 	uart_lock(sc->sc_hwmtx);
411 	uart_mu_param(&sc->sc_bas, baudrate, databits, stopbits, parity);
412 	uart_unlock(sc->sc_hwmtx);
413 
414 	return (0);
415 }
416 
417 static int
418 uart_mu_bus_probe(struct uart_softc *sc)
419 {
420 
421 	/* MU always has 8 byte deep fifo */
422 	sc->sc_rxfifosz = 8;
423 	sc->sc_txfifosz = 8;
424 	device_set_desc(sc->sc_dev, "BCM2835 Mini-UART");
425 
426 	return (0);
427 }
428 
429 static int
430 uart_mu_bus_receive(struct uart_softc *sc)
431 {
432 	struct uart_bas *bas;
433 	uint32_t lsr, xc;
434 	int rx;
435 
436 	bas = &sc->sc_bas;
437 	uart_lock(sc->sc_hwmtx);
438 
439 	lsr = __uart_getreg(bas, AUX_MU_LSR_REG);
440 	while (lsr & LSR_RXREADY) {
441 		xc = __uart_getreg(bas, AUX_MU_IO_REG);
442 		rx = xc & 0xff;
443 		if (uart_rx_full(sc)) {
444 			sc->sc_rxbuf[sc->sc_rxput] = UART_STAT_OVERRUN;
445 			break;
446 		}
447 		uart_rx_put(sc, rx);
448 		lsr = __uart_getreg(bas, AUX_MU_LSR_REG);
449 	}
450 	uart_unlock(sc->sc_hwmtx);
451 
452 	return (0);
453 }
454 
455 static int
456 uart_mu_bus_setsig(struct uart_softc *sc, int sig)
457 {
458 
459 	return (0);
460 }
461 
462 static int
463 uart_mu_bus_transmit(struct uart_softc *sc)
464 {
465 	struct uart_mu_softc *psc;
466 	struct uart_bas *bas;
467 	int i;
468 
469 	psc = (struct uart_mu_softc *)sc;
470 	bas = &sc->sc_bas;
471 	uart_lock(sc->sc_hwmtx);
472 
473   	for (i = 0; i < sc->sc_txdatasz; i++) {
474 		__uart_setreg(bas, AUX_MU_IO_REG, sc->sc_txbuf[i] & 0xff);
475 		uart_barrier(bas);
476 	}
477 
478 	/* Mark busy and enable TX interrupt */
479 	sc->sc_txbusy = 1;
480 	__uart_setreg(bas, AUX_MU_IER_REG, psc->aux_ier);
481 
482 	uart_unlock(sc->sc_hwmtx);
483 
484 	return (0);
485 }
486 
487 static void
488 uart_mu_bus_grab(struct uart_softc *sc)
489 {
490 	struct uart_mu_softc *psc;
491 	struct uart_bas *bas;
492 
493 	psc = (struct uart_mu_softc *)sc;
494 	bas = &sc->sc_bas;
495 
496 	/* Disable interrupts on switch to polling */
497 	uart_lock(sc->sc_hwmtx);
498 	__uart_setreg(bas, AUX_MU_IER_REG, psc->aux_ier &~IER_MASK_ALL);
499 	uart_unlock(sc->sc_hwmtx);
500 }
501 
502 static void
503 uart_mu_bus_ungrab(struct uart_softc *sc)
504 {
505 	struct uart_mu_softc *psc;
506 	struct uart_bas *bas;
507 
508 	psc = (struct uart_mu_softc *)sc;
509 	bas = &sc->sc_bas;
510 
511 	/* Switch to using interrupts while not grabbed */
512 	uart_lock(sc->sc_hwmtx);
513 	__uart_setreg(bas, AUX_MU_CNTL_REG, CNTL_RXENAB|CNTL_TXENAB);
514 	__uart_setreg(bas, AUX_MU_IER_REG, psc->aux_ier);
515 	uart_unlock(sc->sc_hwmtx);
516 }
517