2 * Early printk for Nios2.
4 * Copyright (C) 2015, Altera Corporation
5 * Copyright (C) 2010, Tobias Klauser <tklauser@distanz.ch>
6 * Copyright (C) 2009, Wind River Systems Inc
7 * Implemented by fredrik.markstrom@gmail.com and ivarholmqvist@gmail.com
9 * This file is subject to the terms and conditions of the GNU General Public
10 * License. See the file "COPYING" in the main directory of this archive
14 #include <linux/console.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
21 static unsigned long base_addr;
23 #if defined(CONFIG_SERIAL_ALTERA_JTAGUART_CONSOLE)
25 #define ALTERA_JTAGUART_DATA_REG 0
26 #define ALTERA_JTAGUART_CONTROL_REG 4
27 #define ALTERA_JTAGUART_CONTROL_WSPACE_MSK 0xFFFF0000
28 #define ALTERA_JTAGUART_CONTROL_AC_MSK 0x00000400
30 #define JUART_GET_CR() \
31 __builtin_ldwio((void *)(base_addr + ALTERA_JTAGUART_CONTROL_REG))
32 #define JUART_SET_CR(v) \
33 __builtin_stwio((void *)(base_addr + ALTERA_JTAGUART_CONTROL_REG), v)
34 #define JUART_SET_TX(v) \
35 __builtin_stwio((void *)(base_addr + ALTERA_JTAGUART_DATA_REG), v)
37 static void early_console_write(struct console *con, const char *s, unsigned n)
42 while (((status = JUART_GET_CR())
43 & ALTERA_JTAGUART_CONTROL_WSPACE_MSK) == 0) {
44 #if defined(CONFIG_SERIAL_ALTERA_JTAGUART_CONSOLE_BYPASS)
45 if ((status & ALTERA_JTAGUART_CONTROL_AC_MSK) == 0)
46 return; /* no connection activity */
54 #elif defined(CONFIG_SERIAL_ALTERA_UART_CONSOLE)
56 #define ALTERA_UART_TXDATA_REG 4
57 #define ALTERA_UART_STATUS_REG 8
58 #define ALTERA_UART_STATUS_TRDY 0x0040
60 #define UART_GET_SR() \
61 __builtin_ldwio((void *)(base_addr + ALTERA_UART_STATUS_REG))
62 #define UART_SET_TX(v) \
63 __builtin_stwio((void *)(base_addr + ALTERA_UART_TXDATA_REG), v)
65 static void early_console_putc(char c)
67 while (!(UART_GET_SR() & ALTERA_UART_STATUS_TRDY))
73 static void early_console_write(struct console *con, const char *s, unsigned n)
76 early_console_putc(*s);
78 early_console_putc('\r');
85 static void early_console_write(struct console *con, const char *s, unsigned n)
91 static struct console early_console_prom = {
93 .write = early_console_write,
94 .flags = CON_PRINTBUFFER | CON_BOOT,
98 void __init setup_early_printk(void)
100 #if defined(CONFIG_SERIAL_ALTERA_JTAGUART_CONSOLE) || \
101 defined(CONFIG_SERIAL_ALTERA_UART_CONSOLE)
102 base_addr = of_early_console();
110 #if defined(CONFIG_SERIAL_ALTERA_JTAGUART_CONSOLE_BYPASS)
111 /* Clear activity bit so BYPASS doesn't stall if we've used JTAG for
112 * downloading the kernel. This might cause early data to be lost even
113 * if the JTAG terminal is running.
115 JUART_SET_CR(JUART_GET_CR() | ALTERA_JTAGUART_CONTROL_AC_MSK);
118 early_console = &early_console_prom;
119 register_console(early_console);
120 pr_info("early_console initialized at 0x%08lx\n", base_addr);