uart_cpu_mips_xlr.c revision 198625
1/*-
2 * Copyright (c) 2006 Wojciech A. Koszek <wkoszek@FreeBSD.org>
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 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $Id: uart_cpu_mips_xlr.c,v 1.5 2008-07-16 20:22:39 jayachandranc Exp $
27 */
28/*
29 * Skeleton of this file was based on respective code for ARM
30 * code written by Olivier Houchard.
31 */
32/*
33 * XLRMIPS: This file is hacked from arm/...
34 */
35#include "opt_uart.h"
36
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD$");
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/bus.h>
43#include <sys/cons.h>
44
45#include <machine/bus.h>
46
47#include <dev/uart/uart.h>
48#include <dev/uart/uart_cpu.h>
49#include <sys/kdb.h>
50#include <sys/kernel.h>
51#include <sys/lock.h>
52#include <sys/mutex.h>
53
54static int xlr_uart_probe(struct uart_bas *bas);
55static void xlr_uart_init(struct uart_bas *bas, int, int, int, int);
56static void xlr_uart_term(struct uart_bas *bas);
57static void xlr_uart_putc(struct uart_bas *bas, int);
58
59/*static int xlr_uart_poll(struct uart_bas *bas);*/
60static int xlr_uart_getc(struct uart_bas *bas, struct mtx *hwmtx);
61struct mtx xlr_uart_mtx;	/* UartLock */
62
63extern struct uart_ops uart_ns8250_ops;
64
65struct uart_ops xlr_uart_ns8250_ops = {
66	.probe = xlr_uart_probe,
67	.init = xlr_uart_init,
68	.term = xlr_uart_term,
69	.putc = xlr_uart_putc,
70	/* .poll = xlr_uart_poll, ?? */
71	.getc = xlr_uart_getc,
72};
73
74bus_space_tag_t uart_bus_space_io;
75bus_space_tag_t uart_bus_space_mem;
76
77static __inline void
78xlr_uart_lock(struct mtx *hwmtx)
79{
80	if (!mtx_initialized(hwmtx))
81		return;
82	if (!kdb_active && hwmtx != NULL)
83		mtx_lock_spin(hwmtx);
84}
85
86static __inline void
87xlr_uart_unlock(struct mtx *hwmtx)
88{
89	if (!mtx_initialized(hwmtx))
90		return;
91	if (!kdb_active && hwmtx != NULL)
92		mtx_unlock_spin(hwmtx);
93}
94
95
96static int
97xlr_uart_probe(struct uart_bas *bas)
98{
99	int res;
100
101	xlr_uart_lock(&xlr_uart_mtx);
102	res = uart_ns8250_ops.probe(bas);
103	xlr_uart_unlock(&xlr_uart_mtx);
104	return res;
105}
106
107static void
108xlr_uart_init(struct uart_bas *bas, int baudrate, int databits,
109    int stopbits, int parity)
110{
111	xlr_uart_lock(&xlr_uart_mtx);
112	uart_ns8250_ops.init(bas, baudrate, databits, stopbits, parity);
113	xlr_uart_unlock(&xlr_uart_mtx);
114}
115
116static void
117xlr_uart_term(struct uart_bas *bas)
118{
119	xlr_uart_lock(&xlr_uart_mtx);
120	uart_ns8250_ops.term(bas);
121	xlr_uart_unlock(&xlr_uart_mtx);
122}
123
124static void
125xlr_uart_putc(struct uart_bas *bas, int c)
126{
127	xlr_uart_lock(&xlr_uart_mtx);
128	uart_ns8250_ops.putc(bas, c);
129	xlr_uart_unlock(&xlr_uart_mtx);
130}
131
132/*
133static int xlr_uart_poll(struct uart_bas *bas)
134{
135	int res;
136	xlr_uart_lock(&xlr_uart_mtx);
137	res = uart_ns8250_ops.poll(bas);
138	xlr_uart_unlock(&xlr_uart_mtx);
139	return res;
140}
141*/
142
143static int
144xlr_uart_getc(struct uart_bas *bas, struct mtx *hwmtx)
145{
146	return uart_ns8250_ops.getc(bas, hwmtx);
147}
148
149int
150uart_cpu_eqres(struct uart_bas *b1, struct uart_bas *b2)
151{
152	return ((b1->bsh == b2->bsh && b1->bst == b2->bst) ? 1 : 0);
153}
154
155
156int
157uart_cpu_getdev(int devtype, struct uart_devinfo *di)
158{
159	di->ops = &xlr_uart_ns8250_ops;
160	di->bas.chan = 0;
161	di->bas.bst = uart_bus_space_mem;
162	/* TODO Need to call bus_space_map() here */
163	di->bas.bsh = 0xbef14000;	/* Try with UART0 */
164	di->bas.regshft = 2;
165	/* divisor = rclk / (baudrate * 16); */
166	di->bas.rclk = 66000000;
167
168	di->baudrate = 38400;
169	di->databits = 8;
170	di->stopbits = 1;
171	di->parity = UART_PARITY_NONE;
172
173	/* TODO: Read env variables for all console parameters */
174
175	return (0);
176}
177
178static void
179xlr_uart_mtx_init(void *dummy __unused)
180{
181	mtx_init(&xlr_uart_mtx, "uart lock", NULL, MTX_SPIN);
182}
183
184SYSINIT(xlr_init_uart_mtx, SI_SUB_LOCK, SI_ORDER_ANY, xlr_uart_mtx_init, NULL);
185