]>
Commit | Line | Data |
---|---|---|
9aac5887 RH |
1 | /* |
2 | * Copyright (C) 2014 Linaro Ltd. | |
3 | * Author: Rob Herring <robh@kernel.org> | |
4 | * | |
5 | * Based on 8250 earlycon: | |
6 | * (c) Copyright 2004 Hewlett-Packard Development Company, L.P. | |
7 | * Bjorn Helgaas <bjorn.helgaas@hp.com> | |
8 | * | |
9 | * This program is free software: you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License version 2 as | |
11 | * published by the Free Software Foundation. | |
12 | */ | |
13 | #include <linux/console.h> | |
14 | #include <linux/kernel.h> | |
15 | #include <linux/init.h> | |
16 | #include <linux/io.h> | |
17 | #include <linux/serial_core.h> | |
b0b6abd3 RH |
18 | #include <linux/sizes.h> |
19 | #include <linux/mod_devicetable.h> | |
9aac5887 RH |
20 | |
21 | #ifdef CONFIG_FIX_EARLYCON_MEM | |
22 | #include <asm/fixmap.h> | |
23 | #endif | |
24 | ||
25 | #include <asm/serial.h> | |
26 | ||
27 | static struct console early_con = { | |
60efcf04 | 28 | .name = "uart", /* 8250 console switch requires this name */ |
9aac5887 RH |
29 | .flags = CON_PRINTBUFFER | CON_BOOT, |
30 | .index = -1, | |
31 | }; | |
32 | ||
33 | static struct earlycon_device early_console_dev = { | |
34 | .con = &early_con, | |
35 | }; | |
36 | ||
b0b6abd3 RH |
37 | static const struct of_device_id __earlycon_of_table_sentinel |
38 | __used __section(__earlycon_of_table_end); | |
39 | ||
9aac5887 RH |
40 | static void __iomem * __init earlycon_map(unsigned long paddr, size_t size) |
41 | { | |
42 | void __iomem *base; | |
43 | #ifdef CONFIG_FIX_EARLYCON_MEM | |
44 | set_fixmap_io(FIX_EARLYCON_MEM_BASE, paddr & PAGE_MASK); | |
45 | base = (void __iomem *)__fix_to_virt(FIX_EARLYCON_MEM_BASE); | |
46 | base += paddr & ~PAGE_MASK; | |
47 | #else | |
48 | base = ioremap(paddr, size); | |
49 | #endif | |
50 | if (!base) | |
51 | pr_err("%s: Couldn't map 0x%llx\n", __func__, | |
52 | (unsigned long long)paddr); | |
53 | ||
54 | return base; | |
55 | } | |
56 | ||
73abaf87 | 57 | static int __init parse_options(struct earlycon_device *device, char *options) |
9aac5887 RH |
58 | { |
59 | struct uart_port *port = &device->port; | |
73abaf87 | 60 | int length; |
9aac5887 RH |
61 | unsigned long addr; |
62 | ||
73abaf87 PH |
63 | if (uart_parse_earlycon(options, &port->iotype, &addr, &options)) |
64 | return -EINVAL; | |
9aac5887 | 65 | |
73abaf87 PH |
66 | switch (port->iotype) { |
67 | case UPIO_MEM32: | |
68 | port->regshift = 2; /* fall-through */ | |
69 | case UPIO_MEM: | |
9aac5887 | 70 | port->mapbase = addr; |
73abaf87 PH |
71 | break; |
72 | case UPIO_PORT: | |
9aac5887 | 73 | port->iobase = addr; |
73abaf87 PH |
74 | break; |
75 | default: | |
9aac5887 RH |
76 | return -EINVAL; |
77 | } | |
78 | ||
9aac5887 | 79 | if (options) { |
e26f1db9 | 80 | device->baud = simple_strtoul(options, NULL, 0); |
9aac5887 RH |
81 | length = min(strcspn(options, " ") + 1, |
82 | (size_t)(sizeof(device->options))); | |
83 | strlcpy(device->options, options, length); | |
84 | } | |
85 | ||
fe655de1 | 86 | if (port->iotype == UPIO_MEM || port->iotype == UPIO_MEM32) |
9aac5887 | 87 | pr_info("Early serial console at MMIO%s 0x%llx (options '%s')\n", |
73abaf87 | 88 | (port->iotype == UPIO_MEM32) ? "32" : "", |
9aac5887 RH |
89 | (unsigned long long)port->mapbase, |
90 | device->options); | |
91 | else | |
92 | pr_info("Early serial console at I/O port 0x%lx (options '%s')\n", | |
93 | port->iobase, | |
94 | device->options); | |
95 | ||
96 | return 0; | |
97 | } | |
98 | ||
99 | int __init setup_earlycon(char *buf, const char *match, | |
100 | int (*setup)(struct earlycon_device *, const char *)) | |
101 | { | |
102 | int err; | |
103 | size_t len; | |
104 | struct uart_port *port = &early_console_dev.port; | |
105 | ||
106 | if (!buf || !match || !setup) | |
107 | return 0; | |
108 | ||
109 | len = strlen(match); | |
110 | if (strncmp(buf, match, len)) | |
111 | return 0; | |
112 | if (buf[len] && (buf[len] != ',')) | |
113 | return 0; | |
114 | ||
115 | buf += len + 1; | |
116 | ||
117 | err = parse_options(&early_console_dev, buf); | |
118 | /* On parsing error, pass the options buf to the setup function */ | |
119 | if (!err) | |
120 | buf = NULL; | |
121 | ||
feed5bab | 122 | port->uartclk = BASE_BAUD * 16; |
9aac5887 RH |
123 | if (port->mapbase) |
124 | port->membase = earlycon_map(port->mapbase, 64); | |
125 | ||
126 | early_console_dev.con->data = &early_console_dev; | |
127 | err = setup(&early_console_dev, buf); | |
128 | if (err < 0) | |
129 | return err; | |
130 | if (!early_console_dev.con->write) | |
131 | return -ENODEV; | |
132 | ||
133 | register_console(early_console_dev.con); | |
134 | return 0; | |
135 | } | |
b0b6abd3 RH |
136 | |
137 | int __init of_setup_earlycon(unsigned long addr, | |
138 | int (*setup)(struct earlycon_device *, const char *)) | |
139 | { | |
140 | int err; | |
141 | struct uart_port *port = &early_console_dev.port; | |
142 | ||
143 | port->iotype = UPIO_MEM; | |
144 | port->mapbase = addr; | |
145 | port->uartclk = BASE_BAUD * 16; | |
146 | port->membase = earlycon_map(addr, SZ_4K); | |
147 | ||
148 | early_console_dev.con->data = &early_console_dev; | |
149 | err = setup(&early_console_dev, NULL); | |
150 | if (err < 0) | |
151 | return err; | |
152 | if (!early_console_dev.con->write) | |
153 | return -ENODEV; | |
154 | ||
155 | ||
156 | register_console(early_console_dev.con); | |
157 | return 0; | |
158 | } |