altera_jtag_uart_cons.c revision 245380
1239676Srwatson/*-
2239676Srwatson * Copyright (c) 2011-2012 Robert N. M. Watson
3239676Srwatson * All rights reserved.
4239676Srwatson *
5239676Srwatson * This software was developed by SRI International and the University of
6239676Srwatson * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
7239676Srwatson * ("CTSRD"), as part of the DARPA CRASH research programme.
8239676Srwatson *
9239676Srwatson * Redistribution and use in source and binary forms, with or without
10239676Srwatson * modification, are permitted provided that the following conditions
11239676Srwatson * are met:
12239676Srwatson * 1. Redistributions of source code must retain the above copyright
13239676Srwatson *    notice, this list of conditions and the following disclaimer.
14239676Srwatson * 2. Redistributions in binary form must reproduce the above copyright
15239676Srwatson *    notice, this list of conditions and the following disclaimer in the
16239676Srwatson *    documentation and/or other materials provided with the distribution.
17239676Srwatson *
18239676Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19239676Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20239676Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21239676Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22239676Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23239676Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24239676Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25239676Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26239676Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27239676Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28239676Srwatson * SUCH DAMAGE.
29239676Srwatson */
30239676Srwatson
31239676Srwatson#include <sys/cdefs.h>
32239676Srwatson__FBSDID("$FreeBSD: head/sys/dev/altera/jtag_uart/altera_jtag_uart_cons.c 245380 2013-01-13 16:57:11Z rwatson $");
33239676Srwatson
34239676Srwatson#include <sys/param.h>
35245380Srwatson#include <sys/bus.h>
36239676Srwatson#include <sys/cons.h>
37239676Srwatson#include <sys/endian.h>
38239676Srwatson#include <sys/kdb.h>
39239676Srwatson#include <sys/kernel.h>
40239676Srwatson#include <sys/lock.h>
41239676Srwatson#include <sys/mutex.h>
42239676Srwatson#include <sys/reboot.h>
43239676Srwatson#include <sys/systm.h>
44239676Srwatson#include <sys/tty.h>
45239676Srwatson
46239676Srwatson#include <ddb/ddb.h>
47239676Srwatson
48239676Srwatson#include <dev/altera/jtag_uart/altera_jtag_uart.h>
49239676Srwatson
50245380Srwatsondevclass_t	altera_jtag_uart_devclass;
51245380Srwatson
52239676Srwatson/*
53239676Srwatson * One-byte buffer as we can't check whether the UART is readable without
54239676Srwatson * actually reading from it, synchronised by a spinlock; this lock also
55239676Srwatson * synchronises access to the I/O ports for non-atomic sequences.  These
56239676Srwatson * symbols are public so that the TTY layer can use them when working on an
57239676Srwatson * instance of the UART that is also a low-level console.
58239676Srwatson */
59239676Srwatsonchar		aju_cons_buffer_data;
60239676Srwatsonint		aju_cons_buffer_valid;
61239676Srwatsonint		aju_cons_jtag_present;
62239676Srwatsonu_int		aju_cons_jtag_missed;
63239676Srwatsonstruct mtx	aju_cons_lock;
64239676Srwatson
65239676Srwatson/*
66239676Srwatson * Low-level console driver functions.
67239676Srwatson */
68239676Srwatsonstatic cn_probe_t	aju_cnprobe;
69239676Srwatsonstatic cn_init_t	aju_cninit;
70239676Srwatsonstatic cn_term_t	aju_cnterm;
71239676Srwatsonstatic cn_getc_t	aju_cngetc;
72239676Srwatsonstatic cn_putc_t	aju_cnputc;
73239676Srwatsonstatic cn_grab_t	aju_cngrab;
74239676Srwatsonstatic cn_ungrab_t	aju_cnungrab;
75239676Srwatson
76239676Srwatson/*
77239676Srwatson * JTAG sets the ALTERA_JTAG_UART_CONTROL_AC bit whenever it accesses the
78239676Srwatson * FIFO.  This allows us to (sort of) tell when JTAG is present, so that we
79239676Srwatson * can adopt lossy, rather than blocking, behaviour when JTAG isn't there.
80239676Srwatson * When it is present, we do full flow control.  This delay is how long we
81239676Srwatson * wait to see if JTAG has really disappeared when finding a full buffer and
82239676Srwatson * no AC bit set.
83239676Srwatson */
84239676Srwatson#define	ALTERA_JTAG_UART_AC_POLL_DELAY	10000
85239676Srwatson
86239676Srwatson/*
87239676Srwatson * I/O routines lifted from Deimos.  This is not only MIPS-specific, but also
88240518Seadler * BERI-specific, as we're hard coding the address at which we expect to
89239676Srwatson * find the Altera JTAG UART and using it unconditionally.  We use these
90239676Srwatson * low-level routines so that we can perform console I/O long before newbus
91239676Srwatson * has initialised and devices have attached.  The TTY layer of the driver
92239676Srwatson * knows about this, and uses the console-layer spinlock instead of the
93239676Srwatson * TTY-layer lock to avoid confusion between layers for the console UART.
94239676Srwatson *
95239676Srwatson * XXXRW: The only place this inter-layer behaviour breaks down is if the
96239676Srwatson * low-level console is used for polled read while the TTY driver is also
97239676Srwatson * looking for input.  Probably we should also share buffers between layers.
98239676Srwatson */
99239676Srwatson#define	MIPS_XKPHYS_UNCACHED_BASE	0x9000000000000000
100239676Srwatson
101239676Srwatsontypedef	uint64_t	paddr_t;
102239676Srwatsontypedef	uint64_t	vaddr_t;
103239676Srwatson
104239676Srwatsonstatic inline vaddr_t
105239676Srwatsonmips_phys_to_uncached(paddr_t phys)
106239676Srwatson{
107239676Srwatson
108239676Srwatson	return (phys | MIPS_XKPHYS_UNCACHED_BASE);
109239676Srwatson}
110239676Srwatson
111239676Srwatsonstatic inline uint32_t
112239676Srwatsonmips_ioread_uint32(vaddr_t vaddr)
113239676Srwatson{
114239676Srwatson	uint32_t v;
115239676Srwatson
116239676Srwatson	__asm__ __volatile__ ("lw %0, 0(%1)" : "=r" (v) : "r" (vaddr));
117239676Srwatson	return (v);
118239676Srwatson}
119239676Srwatson
120239676Srwatsonstatic inline void
121239676Srwatsonmips_iowrite_uint32(vaddr_t vaddr, uint32_t v)
122239676Srwatson{
123239676Srwatson
124239676Srwatson	__asm__ __volatile__ ("sw %0, 0(%1)" : : "r" (v), "r" (vaddr));
125239676Srwatson}
126239676Srwatson
127239676Srwatson/*
128239676Srwatson * Little-endian versions of 32-bit I/O routines.
129239676Srwatson */
130239676Srwatsonstatic inline uint32_t
131239676Srwatsonmips_ioread_uint32le(vaddr_t vaddr)
132239676Srwatson{
133239676Srwatson
134239676Srwatson	return (le32toh(mips_ioread_uint32(vaddr)));
135239676Srwatson}
136239676Srwatson
137239676Srwatsonstatic inline void
138239676Srwatsonmips_iowrite_uint32le(vaddr_t vaddr, uint32_t v)
139239676Srwatson{
140239676Srwatson
141239676Srwatson	mips_iowrite_uint32(vaddr, htole32(v));
142239676Srwatson}
143239676Srwatson
144239676Srwatson/*
145239676Srwatson * Low-level read and write register routines; the Altera UART is little
146239676Srwatson * endian, so we byte swap 32-bit reads and writes.
147239676Srwatson */
148239676Srwatsonstatic inline uint32_t
149239676Srwatsonaju_cons_data_read(void)
150239676Srwatson{
151239676Srwatson
152239676Srwatson	return (mips_ioread_uint32le(mips_phys_to_uncached(BERI_UART_BASE +
153239676Srwatson	    ALTERA_JTAG_UART_DATA_OFF)));
154239676Srwatson}
155239676Srwatson
156239676Srwatsonstatic inline void
157239676Srwatsonaju_cons_data_write(uint32_t v)
158239676Srwatson{
159239676Srwatson
160239676Srwatson	mips_iowrite_uint32le(mips_phys_to_uncached(BERI_UART_BASE +
161239676Srwatson	    ALTERA_JTAG_UART_DATA_OFF), v);
162239676Srwatson}
163239676Srwatson
164239676Srwatsonstatic inline uint32_t
165239676Srwatsonaju_cons_control_read(void)
166239676Srwatson{
167239676Srwatson
168239676Srwatson	return (mips_ioread_uint32le(mips_phys_to_uncached(BERI_UART_BASE +
169239676Srwatson	    ALTERA_JTAG_UART_CONTROL_OFF)));
170239676Srwatson}
171239676Srwatson
172239676Srwatsonstatic inline void
173239676Srwatsonaju_cons_control_write(uint32_t v)
174239676Srwatson{
175239676Srwatson
176239676Srwatson	mips_iowrite_uint32le(mips_phys_to_uncached(BERI_UART_BASE +
177239676Srwatson	    ALTERA_JTAG_UART_CONTROL_OFF), v);
178239676Srwatson}
179239676Srwatson
180239676Srwatson/*
181239676Srwatson * Slightly higher-level routines aware of buffering and flow control.
182239676Srwatson */
183239676Srwatsonstatic int
184239676Srwatsonaju_cons_readable(void)
185239676Srwatson{
186239676Srwatson	uint32_t v;
187239676Srwatson
188239676Srwatson	AJU_CONSOLE_LOCK_ASSERT();
189239676Srwatson
190239676Srwatson	if (aju_cons_buffer_valid)
191239676Srwatson		return (1);
192239676Srwatson	v = aju_cons_data_read();
193239676Srwatson	if ((v & ALTERA_JTAG_UART_DATA_RVALID) != 0) {
194239676Srwatson		aju_cons_buffer_valid = 1;
195239676Srwatson		aju_cons_buffer_data = (v & ALTERA_JTAG_UART_DATA_DATA);
196239676Srwatson		return (1);
197239676Srwatson	}
198239676Srwatson	return (0);
199239676Srwatson}
200239676Srwatson
201239676Srwatsonstatic void
202239676Srwatsonaju_cons_write(char ch)
203239676Srwatson{
204239676Srwatson	uint32_t v;
205239676Srwatson
206239676Srwatson	AJU_CONSOLE_LOCK_ASSERT();
207239676Srwatson
208239676Srwatson	/*
209239676Srwatson	 * The flow control logic here is somewhat subtle: we want to wait for
210239676Srwatson	 * write buffer space only while JTAG is present.  However, we can't
211239676Srwatson	 * directly ask if JTAG is present -- just whether it's been seen
212239676Srwatson	 * since we last cleared the ALTERA_JTAG_UART_CONTROL_AC bit.  As
213239676Srwatson	 * such, implement a polling loop in which we both wait for space and
214239676Srwatson	 * try to decide whether JTAG has disappeared on us.  We will have to
215239676Srwatson	 * wait one complete polling delay to detect that JTAG has gone away,
216239676Srwatson	 * but otherwise shouldn't wait any further once it has gone.  And we
217239676Srwatson	 * had to wait for buffer space anyway, if it was there.
218239676Srwatson	 *
219239676Srwatson	 * If JTAG is spotted, reset the TTY-layer miss counter so console-
220239676Srwatson	 * layer clearing of the bit doesn't trigger a TTY-layer
221239676Srwatson	 * disconnection.
222239676Srwatson	 *
223239676Srwatson	 * XXXRW: The polling delay may require tuning.
224239676Srwatson	 */
225239676Srwatson	v = aju_cons_control_read();
226239676Srwatson	if (v & ALTERA_JTAG_UART_CONTROL_AC) {
227239676Srwatson		aju_cons_jtag_present = 1;
228239676Srwatson		aju_cons_jtag_missed = 0;
229239676Srwatson		v &= ~ALTERA_JTAG_UART_CONTROL_AC;
230239676Srwatson		aju_cons_control_write(v);
231239676Srwatson	}
232239676Srwatson	while ((v & ALTERA_JTAG_UART_CONTROL_WSPACE) == 0) {
233239676Srwatson		if (!aju_cons_jtag_present)
234239676Srwatson			return;
235239676Srwatson		DELAY(ALTERA_JTAG_UART_AC_POLL_DELAY);
236239676Srwatson		v = aju_cons_control_read();
237239676Srwatson		if (v & ALTERA_JTAG_UART_CONTROL_AC) {
238239676Srwatson			aju_cons_jtag_present = 1;
239239676Srwatson			v &= ~ALTERA_JTAG_UART_CONTROL_AC;
240239676Srwatson			aju_cons_control_write(v);
241239676Srwatson		} else
242239676Srwatson			aju_cons_jtag_present = 0;
243239676Srwatson	}
244239676Srwatson	aju_cons_data_write(ch);
245239676Srwatson}
246239676Srwatson
247239676Srwatsonstatic char
248239676Srwatsonaju_cons_read(void)
249239676Srwatson{
250239676Srwatson
251239676Srwatson	AJU_CONSOLE_LOCK_ASSERT();
252239676Srwatson
253239676Srwatson	while (!aju_cons_readable());
254239676Srwatson	aju_cons_buffer_valid = 0;
255239676Srwatson	return (aju_cons_buffer_data);
256239676Srwatson}
257239676Srwatson
258239676Srwatson/*
259239676Srwatson * Implementation of a FreeBSD low-level, polled console driver.
260239676Srwatson */
261239676Srwatsonstatic void
262239676Srwatsonaju_cnprobe(struct consdev *cp)
263239676Srwatson{
264239676Srwatson
265239676Srwatson	sprintf(cp->cn_name, "%s%d", AJU_TTYNAME, 0);
266239676Srwatson	cp->cn_pri = (boothowto & RB_SERIAL) ? CN_REMOTE : CN_NORMAL;
267239676Srwatson}
268239676Srwatson
269239676Srwatsonstatic void
270239676Srwatsonaju_cninit(struct consdev *cp)
271239676Srwatson{
272239676Srwatson	uint32_t v;
273239676Srwatson
274239676Srwatson	AJU_CONSOLE_LOCK_INIT();
275239676Srwatson
276239676Srwatson	AJU_CONSOLE_LOCK();
277239676Srwatson	v = aju_cons_control_read();
278239676Srwatson	v &= ~ALTERA_JTAG_UART_CONTROL_AC;
279239676Srwatson	aju_cons_control_write(v);
280239676Srwatson	AJU_CONSOLE_UNLOCK();
281239676Srwatson}
282239676Srwatson
283239676Srwatsonstatic void
284239676Srwatsonaju_cnterm(struct consdev *cp)
285239676Srwatson{
286239676Srwatson
287239676Srwatson}
288239676Srwatson
289239676Srwatsonstatic int
290239676Srwatsonaju_cngetc(struct consdev *cp)
291239676Srwatson{
292239676Srwatson	int ret;
293239676Srwatson
294239676Srwatson	AJU_CONSOLE_LOCK();
295239676Srwatson	ret = aju_cons_read();
296239676Srwatson	AJU_CONSOLE_UNLOCK();
297239676Srwatson	return (ret);
298239676Srwatson}
299239676Srwatson
300239676Srwatsonstatic void
301239676Srwatsonaju_cnputc(struct consdev *cp, int c)
302239676Srwatson{
303239676Srwatson
304239676Srwatson	AJU_CONSOLE_LOCK();
305239676Srwatson	aju_cons_write(c);
306239676Srwatson	AJU_CONSOLE_UNLOCK();
307239676Srwatson}
308239676Srwatson
309239676Srwatsonstatic void
310239676Srwatsonaju_cngrab(struct consdev *cp)
311239676Srwatson{
312239676Srwatson
313239676Srwatson}
314239676Srwatson
315239676Srwatsonstatic void
316239676Srwatsonaju_cnungrab(struct consdev *cp)
317239676Srwatson{
318239676Srwatson
319239676Srwatson}
320239676Srwatson
321239676SrwatsonCONSOLE_DRIVER(aju);
322