xref: /freebsd/sys/dev/uart/uart_dev_z8530.c (revision 7660b554bc59a07be0431c17e0e33815818baa69)
1 /*
2  * Copyright (c) 2003 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/conf.h>
34 #include <machine/bus.h>
35 
36 #include <dev/uart/uart.h>
37 #include <dev/uart/uart_cpu.h>
38 #include <dev/uart/uart_bus.h>
39 #include <dev/uart/uart_dev_z8530.h>
40 
41 #include "uart_if.h"
42 
43 #define	DEFAULT_RCLK	307200
44 
45 #define	IS_CHANNEL_A(bas)	((uart_cpu_busaddr(bas) & 7) != 0)
46 #define	IS_CHANNEL_B(bas)	((uart_cpu_busaddr(bas) & 7) == 0)
47 
48 /* Multiplexed I/O. */
49 static __inline void
50 uart_setmreg(struct uart_bas *bas, int reg, int val)
51 {
52 
53 	uart_setreg(bas, REG_CTRL, reg);
54 	uart_barrier(bas);
55 	uart_setreg(bas, REG_CTRL, val);
56 }
57 
58 static __inline uint8_t
59 uart_getmreg(struct uart_bas *bas, int reg)
60 {
61 
62 	uart_setreg(bas, REG_CTRL, reg);
63 	uart_barrier(bas);
64 	return (uart_getreg(bas, REG_CTRL));
65 }
66 
67 static int
68 z8530_divisor(int rclk, int baudrate)
69 {
70 	int act_baud, divisor, error;
71 
72 	if (baudrate == 0)
73 		return (0);
74 
75 	divisor = (rclk + baudrate) / (baudrate << 1) - 2;
76 	if (divisor >= 65536)
77 		return (0);
78 	act_baud = rclk / 2 / (divisor + 2);
79 
80 	/* 10 times error in percent: */
81 	error = ((act_baud - baudrate) * 2000 / baudrate + 1) >> 1;
82 
83 	/* 3.0% maximum error tolerance: */
84 	if (error < -30 || error > 30)
85 		return (0);
86 
87 	return (divisor);
88 }
89 
90 static int
91 z8530_param(struct uart_bas *bas, int baudrate, int databits, int stopbits,
92     int parity, uint8_t *tpcp)
93 {
94 	int divisor;
95 	uint8_t mpm, rpc, tpc;
96 
97 	rpc = RPC_RXE;
98 	mpm = MPM_CM16;
99 	tpc = TPC_TXE | (*tpcp & (TPC_DTR | TPC_RTS));
100 
101 	if (databits >= 8) {
102 		rpc |= RPC_RB8;
103 		tpc |= TPC_TB8;
104 	} else if (databits == 7) {
105 		rpc |= RPC_RB7;
106 		tpc |= TPC_TB7;
107 	} else if (databits == 6) {
108 		rpc |= RPC_RB6;
109 		tpc |= TPC_TB6;
110 	} else {
111 		rpc |= RPC_RB5;
112 		tpc |= TPC_TB5;
113 	}
114 	mpm |= (stopbits > 1) ? MPM_SB2 : MPM_SB1;
115 	switch (parity) {
116 	case UART_PARITY_EVEN:	mpm |= MPM_PE | MPM_EVEN; break;
117 	case UART_PARITY_NONE:	break;
118 	case UART_PARITY_ODD:	mpm |= MPM_PE; break;
119 	default:		return (EINVAL);
120 	}
121 
122 	/* Set baudrate. */
123 	if (baudrate > 0) {
124 		divisor = z8530_divisor(bas->rclk, baudrate);
125 		if (divisor == 0)
126 			return (EINVAL);
127 		uart_setmreg(bas, WR_TCL, divisor & 0xff);
128 		uart_barrier(bas);
129 		uart_setmreg(bas, WR_TCH, (divisor >> 8) & 0xff);
130 		uart_barrier(bas);
131 	}
132 
133 	uart_setmreg(bas, WR_RPC, rpc);
134 	uart_barrier(bas);
135 	uart_setmreg(bas, WR_MPM, mpm);
136 	uart_barrier(bas);
137 	uart_setmreg(bas, WR_TPC, tpc);
138 	uart_barrier(bas);
139 	*tpcp = tpc;
140 	return (0);
141 }
142 
143 static int
144 z8530_setup(struct uart_bas *bas, int baudrate, int databits, int stopbits,
145     int parity)
146 {
147 	uint8_t tpc;
148 
149 	if (bas->rclk == 0)
150 		bas->rclk = DEFAULT_RCLK;
151 
152 	/* Assume we don't need to perform a full hardware reset. */
153 	uart_setmreg(bas, WR_MIC, ((IS_CHANNEL_A(bas)) ? MIC_CRA : MIC_CRB) |
154 	    MIC_MIE | MIC_NV);
155 	uart_barrier(bas);
156 	/* Set clock sources and enable BRG. */
157 	uart_setmreg(bas, WR_CMC, CMC_RC_BRG | CMC_TC_BRG);
158 	uart_setmreg(bas, WR_MCB2, MCB2_PCLK | MCB2_BRGE);
159 	uart_barrier(bas);
160 	/* Set data encoding. */
161 	uart_setmreg(bas, WR_MCB1, MCB1_NRZ);
162 	uart_barrier(bas);
163 
164 	tpc = TPC_DTR | TPC_RTS;
165 	z8530_param(bas, baudrate, databits, stopbits, parity, &tpc);
166 	return (int)tpc;
167 }
168 
169 /*
170  * Low-level UART interface.
171  */
172 static int z8530_probe(struct uart_bas *bas);
173 static void z8530_init(struct uart_bas *bas, int, int, int, int);
174 static void z8530_term(struct uart_bas *bas);
175 static void z8530_putc(struct uart_bas *bas, int);
176 static int z8530_poll(struct uart_bas *bas);
177 static int z8530_getc(struct uart_bas *bas);
178 
179 struct uart_ops uart_z8530_ops = {
180 	.probe = z8530_probe,
181 	.init = z8530_init,
182 	.term = z8530_term,
183 	.putc = z8530_putc,
184 	.poll = z8530_poll,
185 	.getc = z8530_getc,
186 };
187 
188 static int
189 z8530_probe(struct uart_bas *bas)
190 {
191 
192 	return (0);
193 }
194 
195 static void
196 z8530_init(struct uart_bas *bas, int baudrate, int databits, int stopbits,
197     int parity)
198 {
199 
200 	z8530_setup(bas, baudrate, databits, stopbits, parity);
201 }
202 
203 static void
204 z8530_term(struct uart_bas *bas)
205 {
206 }
207 
208 static void
209 z8530_putc(struct uart_bas *bas, int c)
210 {
211 
212 	while (!(uart_getmreg(bas, RR_BES) & BES_TXE))
213 		;
214 	uart_setreg(bas, REG_DATA, c);
215 	uart_barrier(bas);
216 }
217 
218 static int
219 z8530_poll(struct uart_bas *bas)
220 {
221 
222 	if (!(uart_getmreg(bas, RR_BES) & BES_RXA))
223 		return (-1);
224 	return (uart_getreg(bas, REG_DATA));
225 }
226 
227 static int
228 z8530_getc(struct uart_bas *bas)
229 {
230 
231 	while (!(uart_getmreg(bas, RR_BES) & BES_RXA))
232 		;
233 	return (uart_getreg(bas, REG_DATA));
234 }
235 
236 /*
237  * High-level UART interface.
238  */
239 struct z8530_softc {
240 	struct uart_softc base;
241 	uint8_t	tpc;
242 };
243 
244 static int z8530_bus_attach(struct uart_softc *);
245 static int z8530_bus_detach(struct uart_softc *);
246 static int z8530_bus_flush(struct uart_softc *, int);
247 static int z8530_bus_getsig(struct uart_softc *);
248 static int z8530_bus_ioctl(struct uart_softc *, int, intptr_t);
249 static int z8530_bus_ipend(struct uart_softc *);
250 static int z8530_bus_param(struct uart_softc *, int, int, int, int);
251 static int z8530_bus_probe(struct uart_softc *);
252 static int z8530_bus_receive(struct uart_softc *);
253 static int z8530_bus_setsig(struct uart_softc *, int);
254 static int z8530_bus_transmit(struct uart_softc *);
255 
256 static kobj_method_t z8530_methods[] = {
257 	KOBJMETHOD(uart_attach,		z8530_bus_attach),
258 	KOBJMETHOD(uart_detach,		z8530_bus_detach),
259 	KOBJMETHOD(uart_flush,		z8530_bus_flush),
260 	KOBJMETHOD(uart_getsig,		z8530_bus_getsig),
261 	KOBJMETHOD(uart_ioctl,		z8530_bus_ioctl),
262 	KOBJMETHOD(uart_ipend,		z8530_bus_ipend),
263 	KOBJMETHOD(uart_param,		z8530_bus_param),
264 	KOBJMETHOD(uart_probe,		z8530_bus_probe),
265 	KOBJMETHOD(uart_receive,	z8530_bus_receive),
266 	KOBJMETHOD(uart_setsig,		z8530_bus_setsig),
267 	KOBJMETHOD(uart_transmit,	z8530_bus_transmit),
268 	{ 0, 0 }
269 };
270 
271 struct uart_class uart_z8530_class = {
272 	"z8530 class",
273 	z8530_methods,
274 	sizeof(struct z8530_softc),
275 	.uc_range = 2,
276 	.uc_rclk = DEFAULT_RCLK
277 };
278 
279 #define	SIGCHG(c, i, s, d)				\
280 	if (c) {					\
281 		i |= (i & s) ? s : s | d;		\
282 	} else {					\
283 		i = (i & s) ? (i & ~s) | d : i;		\
284 	}
285 
286 static int
287 z8530_bus_attach(struct uart_softc *sc)
288 {
289 	struct z8530_softc *z8530 = (struct z8530_softc*)sc;
290 	struct uart_bas *bas;
291 	struct uart_devinfo *di;
292 
293 	bas = &sc->sc_bas;
294 	if (sc->sc_sysdev != NULL) {
295 		di = sc->sc_sysdev;
296 		z8530->tpc = TPC_DTR|TPC_RTS;
297 		z8530_param(bas, di->baudrate, di->databits, di->stopbits,
298 		    di->parity, &z8530->tpc);
299 	} else {
300 		z8530->tpc = z8530_setup(bas, 9600, 8, 1, UART_PARITY_NONE);
301 		z8530->tpc &= ~(TPC_DTR|TPC_RTS);
302 	}
303 
304 	sc->sc_rxfifosz = 3;
305 	sc->sc_txfifosz = 1;
306 
307 	(void)z8530_bus_getsig(sc);
308 
309 	uart_setmreg(bas, WR_IC, IC_BRK | IC_CTS | IC_DCD);
310 	uart_barrier(bas);
311 	uart_setmreg(bas, WR_IDT, IDT_TIE | IDT_RIA);
312 	uart_barrier(bas);
313 	uart_setmreg(bas, WR_IV, 0);
314 	uart_barrier(bas);
315 	uart_setmreg(bas, WR_TPC, z8530->tpc);
316 	uart_barrier(bas);
317 	return (0);
318 }
319 
320 static int
321 z8530_bus_detach(struct uart_softc *sc)
322 {
323 
324 	return (0);
325 }
326 
327 static int
328 z8530_bus_flush(struct uart_softc *sc, int what)
329 {
330 
331 	return (0);
332 }
333 
334 static int
335 z8530_bus_getsig(struct uart_softc *sc)
336 {
337 	uint32_t new, old, sig;
338 	uint8_t bes;
339 
340 	do {
341 		old = sc->sc_hwsig;
342 		sig = old;
343 		mtx_lock_spin(&sc->sc_hwmtx);
344 		bes = uart_getmreg(&sc->sc_bas, RR_BES);
345 		mtx_unlock_spin(&sc->sc_hwmtx);
346 		SIGCHG(bes & BES_CTS, sig, UART_SIG_CTS, UART_SIG_DCTS);
347 		SIGCHG(bes & BES_DCD, sig, UART_SIG_DCD, UART_SIG_DDCD);
348 		new = sig & ~UART_SIGMASK_DELTA;
349 	} while (!atomic_cmpset_32(&sc->sc_hwsig, old, new));
350 	return (sig);
351 }
352 
353 static int
354 z8530_bus_ioctl(struct uart_softc *sc, int request, intptr_t data)
355 {
356 	struct z8530_softc *z8530 = (struct z8530_softc*)sc;
357 	struct uart_bas *bas;
358 	int error;
359 
360 	bas = &sc->sc_bas;
361 	error = 0;
362 	mtx_lock_spin(&sc->sc_hwmtx);
363 	switch (request) {
364 	case UART_IOCTL_BREAK:
365 		if (data)
366 			z8530->tpc |= TPC_BRK;
367 		else
368 			z8530->tpc &= ~TPC_BRK;
369 		uart_setmreg(bas, WR_TPC, z8530->tpc);
370 		uart_barrier(bas);
371 		break;
372 	default:
373 		error = EINVAL;
374 		break;
375 	}
376 	mtx_unlock_spin(&sc->sc_hwmtx);
377 	return (error);
378 }
379 
380 static int
381 z8530_bus_ipend(struct uart_softc *sc)
382 {
383 	struct uart_bas *bas;
384 	int ipend;
385 	uint32_t sig;
386 	uint8_t bes, src;
387 
388 	bas = &sc->sc_bas;
389 	ipend = 0;
390 	mtx_lock_spin(&sc->sc_hwmtx);
391 	uart_setreg(bas, REG_CTRL, CR_RSTIUS);
392 	uart_barrier(bas);
393 	bes = uart_getmreg(bas, RR_BES);
394 	if (bes & BES_BRK) {
395 		uart_setreg(bas, REG_CTRL, CR_RSTXSI);
396 		ipend |= UART_IPEND_BREAK;
397 	}
398 	if (bes & BES_TXE) {
399 		uart_setreg(bas, REG_CTRL, CR_RSTTXI);
400 		ipend |= UART_IPEND_TXIDLE;
401 	}
402 	if (bes & BES_RXA)
403 		ipend |= UART_IPEND_RXREADY;
404 	sig = sc->sc_hwsig;
405 	SIGCHG(bes & BES_CTS, sig, UART_SIG_CTS, UART_SIG_DCTS);
406 	SIGCHG(bes & BES_DCD, sig, UART_SIG_DCD, UART_SIG_DDCD);
407 	if (sig & UART_SIGMASK_DELTA)
408 		ipend |= UART_IPEND_SIGCHG;
409 	src = uart_getmreg(bas, RR_SRC);
410 	if (src & SRC_OVR) {
411 		uart_setreg(bas, REG_CTRL, CR_RSTERR);
412 		ipend |= UART_IPEND_OVERRUN;
413 	}
414 	mtx_unlock_spin(&sc->sc_hwmtx);
415 	return (ipend);
416 }
417 
418 static int
419 z8530_bus_param(struct uart_softc *sc, int baudrate, int databits,
420     int stopbits, int parity)
421 {
422 	struct z8530_softc *z8530 = (struct z8530_softc*)sc;
423 	int error;
424 
425 	mtx_lock_spin(&sc->sc_hwmtx);
426 	error = z8530_param(&sc->sc_bas, baudrate, databits, stopbits, parity,
427 	    &z8530->tpc);
428 	mtx_unlock_spin(&sc->sc_hwmtx);
429 	return (error);
430 }
431 
432 static int
433 z8530_bus_probe(struct uart_softc *sc)
434 {
435 	char buf[80];
436 	const char *ch;
437 	int error;
438 
439 	error = z8530_probe(&sc->sc_bas);
440 	if (error)
441 		return (error);
442 
443 	/* Assume the address range is naturally aligned. */
444 	ch = IS_CHANNEL_A(&sc->sc_bas) ? "A" : "B";
445 
446 	snprintf(buf, sizeof(buf), "z8530, channel %s", ch);
447 	device_set_desc_copy(sc->sc_dev, buf);
448 	return (0);
449 }
450 
451 static int
452 z8530_bus_receive(struct uart_softc *sc)
453 {
454 	struct uart_bas *bas;
455 	int xc;
456 	uint8_t bes, src;
457 
458 	bas = &sc->sc_bas;
459 	mtx_lock_spin(&sc->sc_hwmtx);
460 	bes = uart_getmreg(bas, RR_BES);
461 	while (bes & BES_RXA) {
462 		if (uart_rx_full(sc)) {
463 			sc->sc_rxbuf[sc->sc_rxput] = UART_STAT_OVERRUN;
464 			break;
465 		}
466 		src = uart_getmreg(bas, RR_SRC);
467 		xc = uart_getreg(bas, REG_DATA);
468 		if (src & SRC_FE)
469 			xc |= UART_STAT_FRAMERR;
470 		if (src & SRC_PE)
471 			xc |= UART_STAT_PARERR;
472 		uart_rx_put(sc, xc);
473 		if (src & (SRC_FE | SRC_PE)) {
474 			uart_setreg(bas, REG_CTRL, CR_RSTERR);
475 			uart_barrier(bas);
476 		}
477 		bes = uart_getmreg(bas, RR_BES);
478 	}
479 	/* Discard everything left in the Rx FIFO. */
480 	while (bes & BES_RXA) {
481 		src = uart_getmreg(bas, RR_SRC);
482 		(void)uart_getreg(bas, REG_DATA);
483 		if (src & (SRC_FE | SRC_PE)) {
484 			uart_setreg(bas, REG_CTRL, CR_RSTERR);
485 			uart_barrier(bas);
486 		}
487 		bes = uart_getmreg(bas, RR_BES);
488 	}
489 	mtx_unlock_spin(&sc->sc_hwmtx);
490 	return (0);
491 }
492 
493 static int
494 z8530_bus_setsig(struct uart_softc *sc, int sig)
495 {
496 	struct z8530_softc *z8530 = (struct z8530_softc*)sc;
497 	struct uart_bas *bas;
498 	uint32_t new, old;
499 
500 	bas = &sc->sc_bas;
501 	do {
502 		old = sc->sc_hwsig;
503 		new = old;
504 		if (sig & UART_SIG_DDTR) {
505 			SIGCHG(sig & UART_SIG_DTR, new, UART_SIG_DTR,
506 			    UART_SIG_DDTR);
507 		}
508 		if (sig & UART_SIG_DRTS) {
509 			SIGCHG(sig & UART_SIG_RTS, new, UART_SIG_RTS,
510 			    UART_SIG_DRTS);
511 		}
512 	} while (!atomic_cmpset_32(&sc->sc_hwsig, old, new));
513 
514 	mtx_lock_spin(&sc->sc_hwmtx);
515 	if (new & UART_SIG_DTR)
516 		z8530->tpc |= TPC_DTR;
517 	else
518 		z8530->tpc &= ~TPC_DTR;
519 	if (new & UART_SIG_RTS)
520 		z8530->tpc |= TPC_RTS;
521 	else
522 		z8530->tpc &= ~TPC_RTS;
523 	uart_setmreg(bas, WR_TPC, z8530->tpc);
524 	uart_barrier(bas);
525 	mtx_unlock_spin(&sc->sc_hwmtx);
526 	return (0);
527 }
528 
529 static int
530 z8530_bus_transmit(struct uart_softc *sc)
531 {
532 	struct uart_bas *bas;
533 
534 	bas = &sc->sc_bas;
535 	mtx_lock_spin(&sc->sc_hwmtx);
536 	while (!(uart_getmreg(bas, RR_BES) & BES_TXE))
537 		;
538 	uart_setreg(bas, REG_DATA, sc->sc_txbuf[0]);
539 	uart_barrier(bas);
540 	sc->sc_txbusy = 1;
541 	mtx_unlock_spin(&sc->sc_hwmtx);
542 	return (0);
543 }
544