blob: 6dc471e30e793aaf99bfc1bac6d376387d9e21a5 [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 */
Peter Hurley470ca0d2015-03-09 16:27:21 -040013
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
Rob Herring9aac5882014-04-18 17:19:55 -050016#include <linux/console.h>
17#include <linux/kernel.h>
18#include <linux/init.h>
19#include <linux/io.h>
20#include <linux/serial_core.h>
Rob Herringb0b6abd2014-03-27 08:06:16 -050021#include <linux/sizes.h>
22#include <linux/mod_devicetable.h>
Rob Herring9aac5882014-04-18 17:19:55 -050023
24#ifdef CONFIG_FIX_EARLYCON_MEM
25#include <asm/fixmap.h>
26#endif
27
28#include <asm/serial.h>
29
30static struct console early_con = {
Rob Herring60efcf02014-06-12 12:52:44 -050031 .name = "uart", /* 8250 console switch requires this name */
Rob Herring9aac5882014-04-18 17:19:55 -050032 .flags = CON_PRINTBUFFER | CON_BOOT,
33 .index = -1,
34};
35
36static struct earlycon_device early_console_dev = {
37 .con = &early_con,
38};
39
Peter Hurley470ca0d2015-03-09 16:27:21 -040040extern struct earlycon_id __earlycon_table[];
41static const struct earlycon_id __earlycon_table_sentinel
42 __used __section(__earlycon_table_end);
43
Rob Herringb0b6abd2014-03-27 08:06:16 -050044static const struct of_device_id __earlycon_of_table_sentinel
45 __used __section(__earlycon_of_table_end);
46
Rob Herring9aac5882014-04-18 17:19:55 -050047static void __iomem * __init earlycon_map(unsigned long paddr, size_t size)
48{
49 void __iomem *base;
50#ifdef CONFIG_FIX_EARLYCON_MEM
51 set_fixmap_io(FIX_EARLYCON_MEM_BASE, paddr & PAGE_MASK);
52 base = (void __iomem *)__fix_to_virt(FIX_EARLYCON_MEM_BASE);
53 base += paddr & ~PAGE_MASK;
54#else
55 base = ioremap(paddr, size);
56#endif
57 if (!base)
58 pr_err("%s: Couldn't map 0x%llx\n", __func__,
59 (unsigned long long)paddr);
60
61 return base;
62}
63
Peter Hurley73abaf82015-03-01 11:05:46 -050064static int __init parse_options(struct earlycon_device *device, char *options)
Rob Herring9aac5882014-04-18 17:19:55 -050065{
66 struct uart_port *port = &device->port;
Peter Hurley73abaf82015-03-01 11:05:46 -050067 int length;
Rob Herring9aac5882014-04-18 17:19:55 -050068 unsigned long addr;
69
Peter Hurley73abaf82015-03-01 11:05:46 -050070 if (uart_parse_earlycon(options, &port->iotype, &addr, &options))
71 return -EINVAL;
Rob Herring9aac5882014-04-18 17:19:55 -050072
Peter Hurley73abaf82015-03-01 11:05:46 -050073 switch (port->iotype) {
74 case UPIO_MEM32:
75 port->regshift = 2; /* fall-through */
76 case UPIO_MEM:
Rob Herring9aac5882014-04-18 17:19:55 -050077 port->mapbase = addr;
Peter Hurley73abaf82015-03-01 11:05:46 -050078 break;
79 case UPIO_PORT:
Rob Herring9aac5882014-04-18 17:19:55 -050080 port->iobase = addr;
Peter Hurley73abaf82015-03-01 11:05:46 -050081 break;
82 default:
Rob Herring9aac5882014-04-18 17:19:55 -050083 return -EINVAL;
84 }
85
Rob Herring9aac5882014-04-18 17:19:55 -050086 if (options) {
Rob Herringe26f1db2014-04-30 19:48:29 -050087 device->baud = simple_strtoul(options, NULL, 0);
Rob Herring9aac5882014-04-18 17:19:55 -050088 length = min(strcspn(options, " ") + 1,
89 (size_t)(sizeof(device->options)));
90 strlcpy(device->options, options, length);
91 }
92
Kumar Galafe655de2014-10-17 04:01:11 -050093 if (port->iotype == UPIO_MEM || port->iotype == UPIO_MEM32)
Rob Herring9aac5882014-04-18 17:19:55 -050094 pr_info("Early serial console at MMIO%s 0x%llx (options '%s')\n",
Peter Hurley73abaf82015-03-01 11:05:46 -050095 (port->iotype == UPIO_MEM32) ? "32" : "",
Rob Herring9aac5882014-04-18 17:19:55 -050096 (unsigned long long)port->mapbase,
97 device->options);
98 else
99 pr_info("Early serial console at I/O port 0x%lx (options '%s')\n",
100 port->iobase,
101 device->options);
102
103 return 0;
104}
105
Peter Hurley470ca0d2015-03-09 16:27:21 -0400106static int __init register_earlycon(char *buf, const struct earlycon_id *match)
Rob Herring9aac5882014-04-18 17:19:55 -0500107{
108 int err;
Rob Herring9aac5882014-04-18 17:19:55 -0500109 struct uart_port *port = &early_console_dev.port;
110
Rob Herring9aac5882014-04-18 17:19:55 -0500111 /* On parsing error, pass the options buf to the setup function */
Peter Hurley60846882015-03-09 16:27:19 -0400112 if (buf && !parse_options(&early_console_dev, buf))
Rob Herring9aac5882014-04-18 17:19:55 -0500113 buf = NULL;
114
Peter Hurleyfeed5ba2015-03-09 16:27:15 -0400115 port->uartclk = BASE_BAUD * 16;
Rob Herring9aac5882014-04-18 17:19:55 -0500116 if (port->mapbase)
117 port->membase = earlycon_map(port->mapbase, 64);
118
119 early_console_dev.con->data = &early_console_dev;
Peter Hurley470ca0d2015-03-09 16:27:21 -0400120 err = match->setup(&early_console_dev, buf);
Rob Herring9aac5882014-04-18 17:19:55 -0500121 if (err < 0)
122 return err;
123 if (!early_console_dev.con->write)
124 return -ENODEV;
125
126 register_console(early_console_dev.con);
127 return 0;
128}
Rob Herringb0b6abd2014-03-27 08:06:16 -0500129
Peter Hurley470ca0d2015-03-09 16:27:21 -0400130/**
131 * setup_earlycon - match and register earlycon console
132 * @buf: earlycon param string
133 *
134 * Registers the earlycon console matching the earlycon specified
135 * in the param string @buf. Acceptable param strings are of the form
136 * <name>,io|mmio|mmio32,<addr>,<options>
137 * <name>,0x<addr>,<options>
138 * <name>,<options>
139 * <name>
140 *
141 * Only for the third form does the earlycon setup() method receive the
142 * <options> string in the 'options' parameter; all other forms set
143 * the parameter to NULL.
144 *
145 * Returns 0 if an attempt to register the earlycon was made,
146 * otherwise negative error code
147 */
148int __init setup_earlycon(char *buf)
Peter Hurley7c53cb32015-03-09 16:27:20 -0400149{
Peter Hurley470ca0d2015-03-09 16:27:21 -0400150 const struct earlycon_id *match;
Peter Hurley7c53cb32015-03-09 16:27:20 -0400151
Peter Hurley470ca0d2015-03-09 16:27:21 -0400152 if (!buf || !buf[0])
153 return -EINVAL;
Peter Hurley7c53cb32015-03-09 16:27:20 -0400154
Peter Hurley470ca0d2015-03-09 16:27:21 -0400155 if (early_con.flags & CON_ENABLED)
156 return -EALREADY;
Peter Hurley7c53cb32015-03-09 16:27:20 -0400157
Peter Hurley470ca0d2015-03-09 16:27:21 -0400158 for (match = __earlycon_table; match->name[0]; match++) {
159 size_t len = strlen(match->name);
Peter Hurley7c53cb32015-03-09 16:27:20 -0400160
Peter Hurley470ca0d2015-03-09 16:27:21 -0400161 if (strncmp(buf, match->name, len))
162 continue;
163
164 if (buf[len]) {
165 if (buf[len] != ',')
166 continue;
167 buf += len + 1;
168 } else
169 buf = NULL;
170
171 return register_earlycon(buf, match);
172 }
173
174 return -ENOENT;
Peter Hurley7c53cb32015-03-09 16:27:20 -0400175}
176
Peter Hurley470ca0d2015-03-09 16:27:21 -0400177/* early_param wrapper for setup_earlycon() */
178static int __init param_setup_earlycon(char *buf)
179{
180 int err;
181
182 /*
183 * Just 'earlycon' is a valid param for devicetree earlycons;
184 * don't generate a warning from parse_early_params() in that case
185 */
186 if (!buf || !buf[0])
187 return 0;
188
189 err = setup_earlycon(buf);
Peter Hurley66c53aa2015-05-07 14:19:21 -0400190 if (err == -ENOENT || err == -EALREADY)
191 return 0;
Peter Hurley470ca0d2015-03-09 16:27:21 -0400192 return err;
193}
194early_param("earlycon", param_setup_earlycon);
195
Rob Herringb0b6abd2014-03-27 08:06:16 -0500196int __init of_setup_earlycon(unsigned long addr,
197 int (*setup)(struct earlycon_device *, const char *))
198{
199 int err;
200 struct uart_port *port = &early_console_dev.port;
201
202 port->iotype = UPIO_MEM;
203 port->mapbase = addr;
204 port->uartclk = BASE_BAUD * 16;
205 port->membase = earlycon_map(addr, SZ_4K);
206
207 early_console_dev.con->data = &early_console_dev;
208 err = setup(&early_console_dev, NULL);
209 if (err < 0)
210 return err;
211 if (!early_console_dev.con->write)
212 return -ENODEV;
213
214
215 register_console(early_console_dev.con);
216 return 0;
217}