Merge branches 'work.misc' and 'work.dcache' of git://git.kernel.org/pub/scm/linux...
[sfrench/cifs-2.6.git] / arch / mips / pic32 / pic32mzda / early_console.c
1 /*
2  * Joshua Henderson <joshua.henderson@microchip.com>
3  * Copyright (C) 2015 Microchip Technology Inc.  All rights reserved.
4  *
5  *  This program is free software; you can distribute it and/or modify it
6  *  under the terms of the GNU General Public License (Version 2) as
7  *  published by the Free Software Foundation.
8  *
9  *  This program is distributed in the hope it will be useful, but WITHOUT
10  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  *  for more details.
13  */
14 #include <asm/mach-pic32/pic32.h>
15 #include <asm/fw/fw.h>
16 #include <asm/setup.h>
17
18 #include "pic32mzda.h"
19 #include "early_pin.h"
20
21 /* Default early console parameters */
22 #define EARLY_CONSOLE_PORT      1
23 #define EARLY_CONSOLE_BAUDRATE  115200
24
25 #define UART_ENABLE             BIT(15)
26 #define UART_ENABLE_RX          BIT(12)
27 #define UART_ENABLE_TX          BIT(10)
28 #define UART_TX_FULL            BIT(9)
29
30 /* UART1(x == 0) - UART6(x == 5) */
31 #define UART_BASE(x)    ((x) * 0x0200)
32 #define U_MODE(x)       UART_BASE(x)
33 #define U_STA(x)        (UART_BASE(x) + 0x10)
34 #define U_TXR(x)        (UART_BASE(x) + 0x20)
35 #define U_BRG(x)        (UART_BASE(x) + 0x40)
36
37 static void __iomem *uart_base;
38 static char console_port = -1;
39
40 static int __init configure_uart_pins(int port)
41 {
42         switch (port) {
43         case 1:
44                 pic32_pps_input(IN_FUNC_U2RX, IN_RPB0);
45                 pic32_pps_output(OUT_FUNC_U2TX, OUT_RPG9);
46                 break;
47         case 5:
48                 pic32_pps_input(IN_FUNC_U6RX, IN_RPD0);
49                 pic32_pps_output(OUT_FUNC_U6TX, OUT_RPB8);
50                 break;
51         default:
52                 return -1;
53         }
54
55         return 0;
56 }
57
58 static void __init configure_uart(char port, int baud)
59 {
60         u32 pbclk;
61
62         pbclk = pic32_get_pbclk(2);
63
64         __raw_writel(0, uart_base + U_MODE(port));
65         __raw_writel(((pbclk / baud) / 16) - 1, uart_base + U_BRG(port));
66         __raw_writel(UART_ENABLE, uart_base + U_MODE(port));
67         __raw_writel(UART_ENABLE_TX | UART_ENABLE_RX,
68                      uart_base + PIC32_SET(U_STA(port)));
69 }
70
71 static void __init setup_early_console(char port, int baud)
72 {
73         if (configure_uart_pins(port))
74                 return;
75
76         console_port = port;
77         configure_uart(console_port, baud);
78 }
79
80 static char * __init pic32_getcmdline(void)
81 {
82         /*
83          * arch_mem_init() has not been called yet, so we don't have a real
84          * command line setup if using CONFIG_CMDLINE_BOOL.
85          */
86 #ifdef CONFIG_CMDLINE_OVERRIDE
87         return CONFIG_CMDLINE;
88 #else
89         return fw_getcmdline();
90 #endif
91 }
92
93 static int __init get_port_from_cmdline(char *arch_cmdline)
94 {
95         char *s;
96         int port = -1;
97
98         if (!arch_cmdline || *arch_cmdline == '\0')
99                 goto _out;
100
101         s = strstr(arch_cmdline, "earlyprintk=");
102         if (s) {
103                 s = strstr(s, "ttyS");
104                 if (s)
105                         s += 4;
106                 else
107                         goto _out;
108
109                 port = (*s) - '0';
110         }
111
112 _out:
113         return port;
114 }
115
116 static int __init get_baud_from_cmdline(char *arch_cmdline)
117 {
118         char *s;
119         int baud = -1;
120
121         if (!arch_cmdline || *arch_cmdline == '\0')
122                 goto _out;
123
124         s = strstr(arch_cmdline, "earlyprintk=");
125         if (s) {
126                 s = strstr(s, "ttyS");
127                 if (s)
128                         s += 6;
129                 else
130                         goto _out;
131
132                 baud = 0;
133                 while (*s >= '0' && *s <= '9')
134                         baud = baud * 10 + *s++ - '0';
135         }
136
137 _out:
138         return baud;
139 }
140
141 void __init fw_init_early_console(char port)
142 {
143         char *arch_cmdline = pic32_getcmdline();
144         int baud = -1;
145
146         uart_base = ioremap_nocache(PIC32_BASE_UART, 0xc00);
147
148         baud = get_baud_from_cmdline(arch_cmdline);
149         if (port == -1)
150                 port = get_port_from_cmdline(arch_cmdline);
151
152         if (port == -1)
153                 port = EARLY_CONSOLE_PORT;
154
155         if (baud == -1)
156                 baud = EARLY_CONSOLE_BAUDRATE;
157
158         setup_early_console(port, baud);
159 }
160
161 void prom_putchar(char c)
162 {
163         if (console_port >= 0) {
164                 while (__raw_readl(
165                                 uart_base + U_STA(console_port)) & UART_TX_FULL)
166                         ;
167
168                 __raw_writel(c, uart_base + U_TXR(console_port));
169         }
170 }