1194140Simp/*-
2199738Simp * Copyright (c) 2009 M. Warner Losh <imp@FreeBSD.org>
3194140Simp * Copyright (c) 2006 Wojciech A. Koszek <wkoszek@FreeBSD.org>
4194140Simp * All rights reserved.
5194140Simp *
6194140Simp * Redistribution and use in source and binary forms, with or without
7194140Simp * modification, are permitted provided that the following conditions
8194140Simp * are met:
9194140Simp * 1. Redistributions of source code must retain the above copyright
10194140Simp *    notice, this list of conditions and the following disclaimer.
11194140Simp * 2. Redistributions in binary form must reproduce the above copyright
12194140Simp *    notice, this list of conditions and the following disclaimer in the
13194140Simp *    documentation and/or other materials provided with the distribution.
14194140Simp *
15194140Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16194140Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17194140Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18194140Simp * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19194140Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20194140Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21194140Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22194140Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23194140Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24194140Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25194140Simp * SUCH DAMAGE.
26194140Simp *
27194140Simp * $Id$
28194140Simp */
29194140Simp#include "opt_uart.h"
30194140Simp
31194140Simp#include <sys/cdefs.h>
32194140Simp__FBSDID("$FreeBSD$");
33194140Simp
34194140Simp#include <sys/param.h>
35194140Simp#include <sys/systm.h>
36194140Simp#include <sys/bus.h>
37194140Simp#include <sys/cons.h>
38194140Simp
39194140Simp#include <machine/bus.h>
40194140Simp
41194140Simp#include <dev/uart/uart.h>
42194140Simp#include <dev/uart/uart_cpu.h>
43194140Simp
44202063Simp#include <mips/cavium/octeon_pcmap_regs.h>
45194140Simp
46210311Sjmallett#include <contrib/octeon-sdk/cvmx.h>
47210311Sjmallett
48196259Simpbus_space_tag_t uart_bus_space_io;
49196259Simpbus_space_tag_t uart_bus_space_mem;
50196259Simp
51199738Simp/*
52199738Simp * Specailized uart bus space.  We present a 1 apart byte oriented
53199738Simp * bus to the outside world, but internally translate to/from the 8-apart
54199738Simp * 64-bit word bus that's on the octeon.  We only support simple read/write
55199738Simp * in this space.  Everything else is undefined.
56199738Simp */
57199738Simpstatic uint8_t
58199738Simpou_bs_r_1(void *t, bus_space_handle_t handle, bus_size_t offset)
59199738Simp{
60199738Simp
61213345Sjmallett	return (oct_read64(handle + offset));
62199738Simp}
63199738Simp
64199738Simpstatic uint16_t
65199738Simpou_bs_r_2(void *t, bus_space_handle_t handle, bus_size_t offset)
66199738Simp{
67199738Simp
68213345Sjmallett	return (oct_read64(handle + offset));
69199738Simp}
70199738Simp
71199738Simpstatic uint32_t
72199738Simpou_bs_r_4(void *t, bus_space_handle_t handle, bus_size_t offset)
73199738Simp{
74199738Simp
75213345Sjmallett	return (oct_read64(handle + offset));
76199738Simp}
77199738Simp
78199738Simpstatic uint64_t
79199738Simpou_bs_r_8(void *t, bus_space_handle_t handle, bus_size_t offset)
80199738Simp{
81199738Simp
82213345Sjmallett	return (oct_read64(handle + offset));
83199738Simp}
84199738Simp
85199738Simpstatic void
86199738Simpou_bs_w_1(void *t, bus_space_handle_t bsh, bus_size_t offset, uint8_t value)
87199738Simp{
88199738Simp
89213345Sjmallett	oct_write64(bsh + offset, value);
90199738Simp}
91199738Simp
92199738Simpstatic void
93199738Simpou_bs_w_2(void *t, bus_space_handle_t bsh, bus_size_t offset, uint16_t value)
94199738Simp{
95199738Simp
96213345Sjmallett	oct_write64(bsh + offset, value);
97199738Simp}
98199738Simp
99199738Simpstatic void
100199738Simpou_bs_w_4(void *t, bus_space_handle_t bsh, bus_size_t offset, uint32_t value)
101199738Simp{
102199738Simp
103213345Sjmallett	oct_write64(bsh + offset, value);
104199738Simp}
105199738Simp
106199738Simpstatic void
107199738Simpou_bs_w_8(void *t, bus_space_handle_t bsh, bus_size_t offset, uint64_t value)
108199738Simp{
109199738Simp
110213345Sjmallett	oct_write64(bsh + offset, value);
111199738Simp}
112199738Simp
113202985Simpstruct bus_space octeon_uart_tag = {
114199738Simp	.bs_map = generic_bs_map,
115199738Simp	.bs_unmap = generic_bs_unmap,
116199738Simp	.bs_subregion = generic_bs_subregion,
117199738Simp	.bs_barrier = generic_bs_barrier,
118199738Simp	.bs_r_1 = ou_bs_r_1,
119199738Simp	.bs_r_2 = ou_bs_r_2,
120199738Simp	.bs_r_4 = ou_bs_r_4,
121199738Simp	.bs_r_8 = ou_bs_r_8,
122199738Simp	.bs_w_1 = ou_bs_w_1,
123199738Simp	.bs_w_2 = ou_bs_w_2,
124199738Simp	.bs_w_4 = ou_bs_w_4,
125199738Simp	.bs_w_8 = ou_bs_w_8,
126199738Simp};
127199738Simp
128194175Simpextern struct uart_class uart_oct16550_class;
129194140Simp
130194140Simpint
131194140Simpuart_cpu_eqres(struct uart_bas *b1, struct uart_bas *b2)
132194140Simp{
133199738Simp
134194140Simp	return ((b1->bsh == b2->bsh && b1->bst == b2->bst) ? 1 : 0);
135194140Simp}
136194140Simp
137194140Simpint
138194140Simpuart_cpu_getdev(int devtype, struct uart_devinfo *di)
139194140Simp{
140199738Simp	struct uart_class *class = &uart_oct16550_class;
141194175Simp
142199738Simp	/*
143199738Simp	 * These fields need to be setup corretly for uart_getenv to
144199738Simp	 * work in all cases.
145199738Simp	 */
146199738Simp	uart_bus_space_io = NULL;		/* No io map for this device */
147199738Simp	uart_bus_space_mem = &octeon_uart_tag;
148199738Simp	di->bas.bst = uart_bus_space_mem;
149199738Simp
150199738Simp	/*
151199738Simp	 * If env specification for UART exists it takes precedence:
152199738Simp	 * hw.uart.console="mm:0xf1012000" or similar
153199738Simp	 */
154199738Simp	if (uart_getenv(devtype, di, class) == 0)
155199738Simp		return (0);
156199738Simp
157199738Simp	/*
158199738Simp	 * Fallback to UART0 for console.
159199738Simp	 */
160194175Simp	di->ops = uart_getops(class);
161194140Simp	di->bas.chan = 0;
162210311Sjmallett	/* XXX */
163213345Sjmallett	if (bus_space_map(di->bas.bst, CVMX_MIO_UARTX_RBR(0),
164213345Sjmallett	    uart_getrange(class), 0, &di->bas.bsh) != 0)
165199738Simp		return (ENXIO);
166213345Sjmallett	di->bas.regshft = 3;
167194140Simp	di->bas.rclk = 0;
168194140Simp	di->baudrate = 115200;
169194140Simp	di->databits = 8;
170194140Simp	di->stopbits = 1;
171194140Simp	di->parity = UART_PARITY_NONE;
172194140Simp
173194140Simp	return (0);
174194140Simp}
175