blob: c92e83088adb817457cc5a39101b435ed8ed6d89 [file] [log] [blame]
Rob Herring9aac5882014-04-18 17:19:55 -05001/*
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>
18
19#ifdef CONFIG_FIX_EARLYCON_MEM
20#include <asm/fixmap.h>
21#endif
22
23#include <asm/serial.h>
24
25static struct console early_con = {
26 .name = "earlycon",
27 .flags = CON_PRINTBUFFER | CON_BOOT,
28 .index = -1,
29};
30
31static struct earlycon_device early_console_dev = {
32 .con = &early_con,
33};
34
35static void __iomem * __init earlycon_map(unsigned long paddr, size_t size)
36{
37 void __iomem *base;
38#ifdef CONFIG_FIX_EARLYCON_MEM
39 set_fixmap_io(FIX_EARLYCON_MEM_BASE, paddr & PAGE_MASK);
40 base = (void __iomem *)__fix_to_virt(FIX_EARLYCON_MEM_BASE);
41 base += paddr & ~PAGE_MASK;
42#else
43 base = ioremap(paddr, size);
44#endif
45 if (!base)
46 pr_err("%s: Couldn't map 0x%llx\n", __func__,
47 (unsigned long long)paddr);
48
49 return base;
50}
51
52static int __init parse_options(struct earlycon_device *device,
53 char *options)
54{
55 struct uart_port *port = &device->port;
Rob Herringe26f1db2014-04-30 19:48:29 -050056 int mmio, mmio32, length;
Rob Herring9aac5882014-04-18 17:19:55 -050057 unsigned long addr;
58
59 if (!options)
60 return -ENODEV;
61
62 mmio = !strncmp(options, "mmio,", 5);
63 mmio32 = !strncmp(options, "mmio32,", 7);
64 if (mmio || mmio32) {
65 port->iotype = (mmio ? UPIO_MEM : UPIO_MEM32);
66 options += mmio ? 5 : 7;
Rob Herringe26f1db2014-04-30 19:48:29 -050067 addr = simple_strtoul(options, NULL, 0);
Rob Herring9aac5882014-04-18 17:19:55 -050068 port->mapbase = addr;
69 if (mmio32)
70 port->regshift = 2;
71 } else if (!strncmp(options, "io,", 3)) {
72 port->iotype = UPIO_PORT;
73 options += 3;
Rob Herringe26f1db2014-04-30 19:48:29 -050074 addr = simple_strtoul(options, NULL, 0);
Rob Herring9aac5882014-04-18 17:19:55 -050075 port->iobase = addr;
76 mmio = 0;
77 } else if (!strncmp(options, "0x", 2)) {
78 port->iotype = UPIO_MEM;
Rob Herringe26f1db2014-04-30 19:48:29 -050079 addr = simple_strtoul(options, NULL, 0);
Rob Herring9aac5882014-04-18 17:19:55 -050080 port->mapbase = addr;
81 } else {
82 return -EINVAL;
83 }
84
85 port->uartclk = BASE_BAUD * 16;
86
87 options = strchr(options, ',');
88 if (options) {
89 options++;
Rob Herringe26f1db2014-04-30 19:48:29 -050090 device->baud = simple_strtoul(options, NULL, 0);
Rob Herring9aac5882014-04-18 17:19:55 -050091 length = min(strcspn(options, " ") + 1,
92 (size_t)(sizeof(device->options)));
93 strlcpy(device->options, options, length);
94 }
95
96 if (mmio || mmio32)
97 pr_info("Early serial console at MMIO%s 0x%llx (options '%s')\n",
98 mmio32 ? "32" : "",
99 (unsigned long long)port->mapbase,
100 device->options);
101 else
102 pr_info("Early serial console at I/O port 0x%lx (options '%s')\n",
103 port->iobase,
104 device->options);
105
106 return 0;
107}
108
109int __init setup_earlycon(char *buf, const char *match,
110 int (*setup)(struct earlycon_device *, const char *))
111{
112 int err;
113 size_t len;
114 struct uart_port *port = &early_console_dev.port;
115
116 if (!buf || !match || !setup)
117 return 0;
118
119 len = strlen(match);
120 if (strncmp(buf, match, len))
121 return 0;
122 if (buf[len] && (buf[len] != ','))
123 return 0;
124
125 buf += len + 1;
126
127 err = parse_options(&early_console_dev, buf);
128 /* On parsing error, pass the options buf to the setup function */
129 if (!err)
130 buf = NULL;
131
132 if (port->mapbase)
133 port->membase = earlycon_map(port->mapbase, 64);
134
135 early_console_dev.con->data = &early_console_dev;
136 err = setup(&early_console_dev, buf);
137 if (err < 0)
138 return err;
139 if (!early_console_dev.con->write)
140 return -ENODEV;
141
142 register_console(early_console_dev.con);
143 return 0;
144}