xref: /linux/arch/mips/ath79/early_printk.c (revision d5be4aeb5b36ba9cecd6ddbcce972e5d842c5df6)
1 /*
2  *  Atheros AR7XXX/AR9XXX SoC early printk support
3  *
4  *  Copyright (C) 2008-2011 Gabor Juhos <juhosg@openwrt.org>
5  *  Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
6  *
7  *  This program is free software; you can redistribute it and/or modify it
8  *  under the terms of the GNU General Public License version 2 as published
9  *  by the Free Software Foundation.
10  */
11 
12 #include <linux/io.h>
13 #include <linux/errno.h>
14 #include <linux/serial_reg.h>
15 #include <asm/addrspace.h>
16 #include <asm/setup.h>
17 
18 #include <asm/mach-ath79/ath79.h>
19 #include <asm/mach-ath79/ar71xx_regs.h>
20 #include <asm/mach-ath79/ar933x_uart.h>
21 
22 static void (*_prom_putchar)(char);
23 
24 static inline void prom_putchar_wait(void __iomem *reg, u32 mask, u32 val)
25 {
26 	u32 t;
27 
28 	do {
29 		t = __raw_readl(reg);
30 		if ((t & mask) == val)
31 			break;
32 	} while (1);
33 }
34 
35 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
36 
37 static void prom_putchar_ar71xx(char ch)
38 {
39 	void __iomem *base = (void __iomem *)(KSEG1ADDR(AR71XX_UART_BASE));
40 
41 	prom_putchar_wait(base + UART_LSR * 4, BOTH_EMPTY, BOTH_EMPTY);
42 	__raw_writel((unsigned char)ch, base + UART_TX * 4);
43 	prom_putchar_wait(base + UART_LSR * 4, BOTH_EMPTY, BOTH_EMPTY);
44 }
45 
46 static void prom_putchar_ar933x(char ch)
47 {
48 	void __iomem *base = (void __iomem *)(KSEG1ADDR(AR933X_UART_BASE));
49 
50 	prom_putchar_wait(base + AR933X_UART_DATA_REG, AR933X_UART_DATA_TX_CSR,
51 			  AR933X_UART_DATA_TX_CSR);
52 	__raw_writel(AR933X_UART_DATA_TX_CSR | (unsigned char)ch,
53 		     base + AR933X_UART_DATA_REG);
54 	prom_putchar_wait(base + AR933X_UART_DATA_REG, AR933X_UART_DATA_TX_CSR,
55 			  AR933X_UART_DATA_TX_CSR);
56 }
57 
58 static void prom_putchar_dummy(char ch)
59 {
60 	/* nothing to do */
61 }
62 
63 static void prom_putchar_init(void)
64 {
65 	void __iomem *base;
66 	u32 id;
67 
68 	base = (void __iomem *)(KSEG1ADDR(AR71XX_RESET_BASE));
69 	id = __raw_readl(base + AR71XX_RESET_REG_REV_ID);
70 	id &= REV_ID_MAJOR_MASK;
71 
72 	switch (id) {
73 	case REV_ID_MAJOR_AR71XX:
74 	case REV_ID_MAJOR_AR7240:
75 	case REV_ID_MAJOR_AR7241:
76 	case REV_ID_MAJOR_AR7242:
77 	case REV_ID_MAJOR_AR913X:
78 	case REV_ID_MAJOR_AR9341:
79 	case REV_ID_MAJOR_AR9342:
80 	case REV_ID_MAJOR_AR9344:
81 	case REV_ID_MAJOR_QCA9556:
82 	case REV_ID_MAJOR_QCA9558:
83 		_prom_putchar = prom_putchar_ar71xx;
84 		break;
85 
86 	case REV_ID_MAJOR_AR9330:
87 	case REV_ID_MAJOR_AR9331:
88 		_prom_putchar = prom_putchar_ar933x;
89 		break;
90 
91 	default:
92 		_prom_putchar = prom_putchar_dummy;
93 		break;
94 	}
95 }
96 
97 void prom_putchar(char ch)
98 {
99 	if (!_prom_putchar)
100 		prom_putchar_init();
101 
102 	_prom_putchar(ch);
103 }
104