blob: 9fb76b66c54541ac676faeb5325b6fa54d137ec5 [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>
Rob Herringb0b6abd2014-03-27 08:06:16 -050018#include <linux/sizes.h>
19#include <linux/mod_devicetable.h>
Rob Herring9aac5882014-04-18 17:19:55 -050020
21#ifdef CONFIG_FIX_EARLYCON_MEM
22#include <asm/fixmap.h>
23#endif
24
25#include <asm/serial.h>
26
27static struct console early_con = {
Rob Herring60efcf02014-06-12 12:52:44 -050028 .name = "uart", /* 8250 console switch requires this name */
Rob Herring9aac5882014-04-18 17:19:55 -050029 .flags = CON_PRINTBUFFER | CON_BOOT,
30 .index = -1,
31};
32
33static struct earlycon_device early_console_dev = {
34 .con = &early_con,
35};
36
Rob Herringb0b6abd2014-03-27 08:06:16 -050037static const struct of_device_id __earlycon_of_table_sentinel
38 __used __section(__earlycon_of_table_end);
39
Rob Herring9aac5882014-04-18 17:19:55 -050040static 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
Peter Hurley73abaf82015-03-01 11:05:46 -050057static int __init parse_options(struct earlycon_device *device, char *options)
Rob Herring9aac5882014-04-18 17:19:55 -050058{
59 struct uart_port *port = &device->port;
Peter Hurley73abaf82015-03-01 11:05:46 -050060 int length;
Rob Herring9aac5882014-04-18 17:19:55 -050061 unsigned long addr;
62
Peter Hurley73abaf82015-03-01 11:05:46 -050063 if (uart_parse_earlycon(options, &port->iotype, &addr, &options))
64 return -EINVAL;
Rob Herring9aac5882014-04-18 17:19:55 -050065
Peter Hurley73abaf82015-03-01 11:05:46 -050066 switch (port->iotype) {
67 case UPIO_MEM32:
68 port->regshift = 2; /* fall-through */
69 case UPIO_MEM:
Rob Herring9aac5882014-04-18 17:19:55 -050070 port->mapbase = addr;
Peter Hurley73abaf82015-03-01 11:05:46 -050071 break;
72 case UPIO_PORT:
Rob Herring9aac5882014-04-18 17:19:55 -050073 port->iobase = addr;
Peter Hurley73abaf82015-03-01 11:05:46 -050074 break;
75 default:
Rob Herring9aac5882014-04-18 17:19:55 -050076 return -EINVAL;
77 }
78
Rob Herring9aac5882014-04-18 17:19:55 -050079 if (options) {
Rob Herringe26f1db2014-04-30 19:48:29 -050080 device->baud = simple_strtoul(options, NULL, 0);
Rob Herring9aac5882014-04-18 17:19:55 -050081 length = min(strcspn(options, " ") + 1,
82 (size_t)(sizeof(device->options)));
83 strlcpy(device->options, options, length);
84 }
85
Kumar Galafe655de2014-10-17 04:01:11 -050086 if (port->iotype == UPIO_MEM || port->iotype == UPIO_MEM32)
Rob Herring9aac5882014-04-18 17:19:55 -050087 pr_info("Early serial console at MMIO%s 0x%llx (options '%s')\n",
Peter Hurley73abaf82015-03-01 11:05:46 -050088 (port->iotype == UPIO_MEM32) ? "32" : "",
Rob Herring9aac5882014-04-18 17:19:55 -050089 (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
Peter Hurley7c53cb32015-03-09 16:27:20 -040099
100static int __init
101register_earlycon(char *buf, int (*setup)(struct earlycon_device *, const char *))
Rob Herring9aac5882014-04-18 17:19:55 -0500102{
103 int err;
Rob Herring9aac5882014-04-18 17:19:55 -0500104 struct uart_port *port = &early_console_dev.port;
105
Rob Herring9aac5882014-04-18 17:19:55 -0500106 /* On parsing error, pass the options buf to the setup function */
Peter Hurley60846882015-03-09 16:27:19 -0400107 if (buf && !parse_options(&early_console_dev, buf))
Rob Herring9aac5882014-04-18 17:19:55 -0500108 buf = NULL;
109
Peter Hurleyfeed5ba2015-03-09 16:27:15 -0400110 port->uartclk = BASE_BAUD * 16;
Rob Herring9aac5882014-04-18 17:19:55 -0500111 if (port->mapbase)
112 port->membase = earlycon_map(port->mapbase, 64);
113
114 early_console_dev.con->data = &early_console_dev;
115 err = setup(&early_console_dev, buf);
116 if (err < 0)
117 return err;
118 if (!early_console_dev.con->write)
119 return -ENODEV;
120
121 register_console(early_console_dev.con);
122 return 0;
123}
Rob Herringb0b6abd2014-03-27 08:06:16 -0500124
Peter Hurley7c53cb32015-03-09 16:27:20 -0400125int __init setup_earlycon(char *buf, const char *match,
126 int (*setup)(struct earlycon_device *, const char *))
127{
128 size_t len;
129
130 if (!buf || !match || !setup)
131 return 0;
132
133 len = strlen(match);
134 if (strncmp(buf, match, len))
135 return 0;
136
137 if (buf[len]) {
138 if (buf[len] != ',')
139 return 0;
140 buf += len + 1;
141 } else
142 buf = NULL;
143
144 return register_earlycon(buf, setup);
145}
146
Rob Herringb0b6abd2014-03-27 08:06:16 -0500147int __init of_setup_earlycon(unsigned long addr,
148 int (*setup)(struct earlycon_device *, const char *))
149{
150 int err;
151 struct uart_port *port = &early_console_dev.port;
152
153 port->iotype = UPIO_MEM;
154 port->mapbase = addr;
155 port->uartclk = BASE_BAUD * 16;
156 port->membase = earlycon_map(addr, SZ_4K);
157
158 early_console_dev.con->data = &early_console_dev;
159 err = setup(&early_console_dev, NULL);
160 if (err < 0)
161 return err;
162 if (!early_console_dev.con->write)
163 return -ENODEV;
164
165
166 register_console(early_console_dev.con);
167 return 0;
168}