blob: 40bd7bd4e19a88ee9b573c37deea8bc8694375b9 [file] [log] [blame]
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +11001#include <linux/kernel.h>
2#include <linux/serial.h>
3#include <linux/serial_8250.h>
4#include <linux/serial_core.h>
5#include <linux/console.h>
6#include <linux/pci.h>
Grant Likely22ae7822010-07-29 11:49:01 -06007#include <linux/of_address.h>
Paul Gortmaker1a7507c2008-01-24 11:59:12 -05008#include <linux/of_device.h>
Arnd Bergmann7df56592011-06-27 11:45:16 +00009#include <linux/serial_reg.h>
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110010#include <asm/io.h>
11#include <asm/mmu.h>
12#include <asm/prom.h>
13#include <asm/serial.h>
14#include <asm/udbg.h>
15#include <asm/pci-bridge.h>
16#include <asm/ppc-pci.h>
17
18#undef DEBUG
19
20#ifdef DEBUG
21#define DBG(fmt...) do { printk(fmt); } while(0)
22#else
23#define DBG(fmt...) do { } while(0)
24#endif
25
26#define MAX_LEGACY_SERIAL_PORTS 8
27
28static struct plat_serial8250_port
29legacy_serial_ports[MAX_LEGACY_SERIAL_PORTS+1];
30static struct legacy_serial_info {
31 struct device_node *np;
32 unsigned int speed;
33 unsigned int clock;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100034 int irq_check_parent;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110035 phys_addr_t taddr;
36} legacy_serial_infos[MAX_LEGACY_SERIAL_PORTS];
Paul Gortmaker1a7507c2008-01-24 11:59:12 -050037
Bartlomiej Zolnierkiewiczdc1473d2013-09-30 15:11:42 +020038static struct of_device_id legacy_serial_parents[] __initdata = {
Paul Gortmaker1a7507c2008-01-24 11:59:12 -050039 {.type = "soc",},
40 {.type = "tsi-bridge",},
Michael Ellermanf5903ed2008-02-05 23:01:50 +110041 {.type = "opb", },
42 {.compatible = "ibm,opb",},
Paul Gortmaker1a7507c2008-01-24 11:59:12 -050043 {.compatible = "simple-bus",},
44 {.compatible = "wrs,epld-localbus",},
Benjamin Herrenschmidt3bc5ab92008-07-07 16:39:50 +100045 {},
Paul Gortmaker1a7507c2008-01-24 11:59:12 -050046};
47
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110048static unsigned int legacy_serial_count;
49static int legacy_serial_console = -1;
50
Arnd Bergmann7df56592011-06-27 11:45:16 +000051static unsigned int tsi_serial_in(struct uart_port *p, int offset)
52{
53 unsigned int tmp;
54 offset = offset << p->regshift;
55 if (offset == UART_IIR) {
56 tmp = readl(p->membase + (UART_IIR & ~3));
57 return (tmp >> 16) & 0xff; /* UART_IIR % 4 == 2 */
58 } else
59 return readb(p->membase + offset);
60}
61
62static void tsi_serial_out(struct uart_port *p, int offset, int value)
63{
64 offset = offset << p->regshift;
65 if (!((offset == UART_IER) && (value & UART_IER_UUE)))
66 writeb(value, p->membase + offset);
67}
68
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110069static int __init add_legacy_port(struct device_node *np, int want_index,
70 int iotype, phys_addr_t base,
Kumar Galab580d462005-12-20 16:16:52 -060071 phys_addr_t taddr, unsigned long irq,
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100072 upf_t flags, int irq_check_parent)
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110073{
Ian Munsief14362d2010-10-01 17:06:07 +100074 const __be32 *clk, *spd;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +100075 u32 clock = BASE_BAUD * 16;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110076 int index;
77
78 /* get clock freq. if present */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +100079 clk = of_get_property(np, "clock-frequency", NULL);
David Woodhouse31df1672005-11-24 17:08:56 +000080 if (clk && *clk)
Ian Munsief14362d2010-10-01 17:06:07 +100081 clock = be32_to_cpup(clk);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110082
83 /* get default speed if present */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +100084 spd = of_get_property(np, "current-speed", NULL);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110085
86 /* If we have a location index, then try to use it */
87 if (want_index >= 0 && want_index < MAX_LEGACY_SERIAL_PORTS)
88 index = want_index;
89 else
90 index = legacy_serial_count;
91
92 /* if our index is still out of range, that mean that
93 * array is full, we could scan for a free slot but that
94 * make little sense to bother, just skip the port
95 */
96 if (index >= MAX_LEGACY_SERIAL_PORTS)
97 return -1;
98 if (index >= legacy_serial_count)
99 legacy_serial_count = index + 1;
100
101 /* Check if there is a port who already claimed our slot */
Anton Blanchardb0d436c2013-08-07 02:01:24 +1000102 if (legacy_serial_infos[index].np != NULL) {
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100103 /* if we still have some room, move it, else override */
104 if (legacy_serial_count < MAX_LEGACY_SERIAL_PORTS) {
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000105 printk(KERN_DEBUG "Moved legacy port %d -> %d\n",
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100106 index, legacy_serial_count);
107 legacy_serial_ports[legacy_serial_count] =
108 legacy_serial_ports[index];
109 legacy_serial_infos[legacy_serial_count] =
110 legacy_serial_infos[index];
111 legacy_serial_count++;
112 } else {
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000113 printk(KERN_DEBUG "Replacing legacy port %d\n", index);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100114 }
115 }
116
117 /* Now fill the entry */
118 memset(&legacy_serial_ports[index], 0,
119 sizeof(struct plat_serial8250_port));
120 if (iotype == UPIO_PORT)
121 legacy_serial_ports[index].iobase = base;
122 else
Benjamin Herrenschmidt8dacaed2005-11-29 11:21:59 +1100123 legacy_serial_ports[index].mapbase = base;
Arnd Bergmann7df56592011-06-27 11:45:16 +0000124
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100125 legacy_serial_ports[index].iotype = iotype;
126 legacy_serial_ports[index].uartclk = clock;
127 legacy_serial_ports[index].irq = irq;
Kumar Galab580d462005-12-20 16:16:52 -0600128 legacy_serial_ports[index].flags = flags;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100129 legacy_serial_infos[index].taddr = taddr;
130 legacy_serial_infos[index].np = of_node_get(np);
131 legacy_serial_infos[index].clock = clock;
Ian Munsief14362d2010-10-01 17:06:07 +1000132 legacy_serial_infos[index].speed = spd ? be32_to_cpup(spd) : 0;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000133 legacy_serial_infos[index].irq_check_parent = irq_check_parent;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100134
Arnd Bergmann7df56592011-06-27 11:45:16 +0000135 if (iotype == UPIO_TSI) {
136 legacy_serial_ports[index].serial_in = tsi_serial_in;
137 legacy_serial_ports[index].serial_out = tsi_serial_out;
138 }
139
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000140 printk(KERN_DEBUG "Found legacy serial port %d for %s\n",
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100141 index, np->full_name);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000142 printk(KERN_DEBUG " %s=%llx, taddr=%llx, irq=%lx, clk=%d, speed=%d\n",
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100143 (iotype == UPIO_PORT) ? "port" : "mem",
144 (unsigned long long)base, (unsigned long long)taddr, irq,
145 legacy_serial_ports[index].uartclk,
146 legacy_serial_infos[index].speed);
147
148 return index;
149}
150
Kumar Galab580d462005-12-20 16:16:52 -0600151static int __init add_legacy_soc_port(struct device_node *np,
152 struct device_node *soc_dev)
153{
Benjamin Herrenschmidtf704b8d2006-07-04 14:14:07 +1000154 u64 addr;
Alistair Popple4a396dc2013-08-07 02:01:43 +1000155 const __be32 *addrp;
David Gibsonabb4a232007-05-06 14:48:49 -0700156 upf_t flags = UPF_BOOT_AUTOCONF | UPF_SKIP_TEST | UPF_SHARE_IRQ
157 | UPF_FIXED_PORT;
Zang Roy-r61911be9633e2006-08-23 10:20:27 +0800158 struct device_node *tsi = of_get_parent(np);
Kumar Galab580d462005-12-20 16:16:52 -0600159
160 /* We only support ports that have a clock frequency properly
161 * encoded in the device-tree.
162 */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000163 if (of_get_property(np, "clock-frequency", NULL) == NULL)
Kumar Galab580d462005-12-20 16:16:52 -0600164 return -1;
165
John Linn1e6d1f22008-07-01 10:52:41 -0700166 /* if reg-shift or offset, don't try to use it */
167 if ((of_get_property(np, "reg-shift", NULL) != NULL) ||
168 (of_get_property(np, "reg-offset", NULL) != NULL))
169 return -1;
170
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100171 /* if rtas uses this device, don't try to use it as well */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000172 if (of_get_property(np, "used-by-rtas", NULL) != NULL)
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100173 return -1;
174
Kumar Galab580d462005-12-20 16:16:52 -0600175 /* Get the address */
176 addrp = of_get_address(soc_dev, 0, NULL, NULL);
177 if (addrp == NULL)
178 return -1;
179
180 addr = of_translate_address(soc_dev, addrp);
Benjamin Herrenschmidt7c6efda2006-07-03 17:24:15 +1000181 if (addr == OF_BAD_ADDR)
182 return -1;
Kumar Galab580d462005-12-20 16:16:52 -0600183
184 /* Add port, irq will be dealt with later. We passed a translated
185 * IO port value. It will be fixed up later along with the irq
186 */
Zang Roy-r61911be9633e2006-08-23 10:20:27 +0800187 if (tsi && !strcmp(tsi->type, "tsi-bridge"))
188 return add_legacy_port(np, -1, UPIO_TSI, addr, addr, NO_IRQ, flags, 0);
189 else
190 return add_legacy_port(np, -1, UPIO_MEM, addr, addr, NO_IRQ, flags, 0);
Kumar Galab580d462005-12-20 16:16:52 -0600191}
192
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100193static int __init add_legacy_isa_port(struct device_node *np,
Kumar Gala017e0fa2006-01-03 16:15:21 -0600194 struct device_node *isa_brg)
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100195{
Ian Munsief14362d2010-10-01 17:06:07 +1000196 const __be32 *reg;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000197 const char *typep;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100198 int index = -1;
Benjamin Herrenschmidtf704b8d2006-07-04 14:14:07 +1000199 u64 taddr;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100200
Benjamin Herrenschmidt7c6efda2006-07-03 17:24:15 +1000201 DBG(" -> add_legacy_isa_port(%s)\n", np->full_name);
202
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100203 /* Get the ISA port number */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000204 reg = of_get_property(np, "reg", NULL);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100205 if (reg == NULL)
206 return -1;
207
208 /* Verify it's an IO port, we don't support anything else */
Ian Munsief14362d2010-10-01 17:06:07 +1000209 if (!(be32_to_cpu(reg[0]) & 0x00000001))
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100210 return -1;
211
212 /* Now look for an "ibm,aix-loc" property that gives us ordering
213 * if any...
214 */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000215 typep = of_get_property(np, "ibm,aix-loc", NULL);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100216
217 /* If we have a location index, then use it */
218 if (typep && *typep == 'S')
219 index = simple_strtol(typep+1, NULL, 0) - 1;
220
Benjamin Herrenschmidtf704b8d2006-07-04 14:14:07 +1000221 /* Translate ISA address. If it fails, we still register the port
222 * with no translated address so that it can be picked up as an IO
223 * port later by the serial driver
Benjamin Herrenschmidt30925742013-07-15 13:03:12 +1000224 *
225 * Note: Don't even try on P8 lpc, we know it's not directly mapped
Benjamin Herrenschmidtf704b8d2006-07-04 14:14:07 +1000226 */
Benjamin Herrenschmidt30925742013-07-15 13:03:12 +1000227 if (!of_device_is_compatible(isa_brg, "ibm,power8-lpc")) {
228 taddr = of_translate_address(np, reg);
229 if (taddr == OF_BAD_ADDR)
230 taddr = 0;
231 } else
Benjamin Herrenschmidtf704b8d2006-07-04 14:14:07 +1000232 taddr = 0;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100233
234 /* Add port, irq will be dealt with later */
Benjamin Herrenschmidt30925742013-07-15 13:03:12 +1000235 return add_legacy_port(np, index, UPIO_PORT, be32_to_cpu(reg[1]),
236 taddr, NO_IRQ, UPF_BOOT_AUTOCONF, 0);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100237
238}
239
Kumar Gala017e0fa2006-01-03 16:15:21 -0600240#ifdef CONFIG_PCI
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100241static int __init add_legacy_pci_port(struct device_node *np,
242 struct device_node *pci_dev)
243{
Benjamin Herrenschmidtf704b8d2006-07-04 14:14:07 +1000244 u64 addr, base;
Alistair Popple4a396dc2013-08-07 02:01:43 +1000245 const __be32 *addrp;
Benjamin Herrenschmidtd2dd4822005-11-30 16:57:28 +1100246 unsigned int flags;
Benjamin Herrenschmidt8dacaed2005-11-29 11:21:59 +1100247 int iotype, index = -1, lindex = 0;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100248
Benjamin Herrenschmidt7c6efda2006-07-03 17:24:15 +1000249 DBG(" -> add_legacy_pci_port(%s)\n", np->full_name);
250
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100251 /* We only support ports that have a clock frequency properly
252 * encoded in the device-tree (that is have an fcode). Anything
253 * else can't be used that early and will be normally probed by
Benjamin Herrenschmidt8dacaed2005-11-29 11:21:59 +1100254 * the generic 8250_pci driver later on. The reason is that 8250
255 * compatible UARTs on PCI need all sort of quirks (port offsets
256 * etc...) that this code doesn't know about
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100257 */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000258 if (of_get_property(np, "clock-frequency", NULL) == NULL)
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100259 return -1;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100260
261 /* Get the PCI address. Assume BAR 0 */
Benjamin Herrenschmidtd2dd4822005-11-30 16:57:28 +1100262 addrp = of_get_pci_address(pci_dev, 0, NULL, &flags);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100263 if (addrp == NULL)
264 return -1;
265
266 /* We only support BAR 0 for now */
Benjamin Herrenschmidtd2dd4822005-11-30 16:57:28 +1100267 iotype = (flags & IORESOURCE_MEM) ? UPIO_MEM : UPIO_PORT;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100268 addr = of_translate_address(pci_dev, addrp);
Benjamin Herrenschmidt7c6efda2006-07-03 17:24:15 +1000269 if (addr == OF_BAD_ADDR)
270 return -1;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100271
272 /* Set the IO base to the same as the translated address for MMIO,
273 * or to the domain local IO base for PIO (it will be fixed up later)
274 */
275 if (iotype == UPIO_MEM)
276 base = addr;
277 else
Alistair Popple4a396dc2013-08-07 02:01:43 +1000278 base = of_read_number(&addrp[2], 1);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100279
280 /* Try to guess an index... If we have subdevices of the pci dev,
281 * we get to their "reg" property
282 */
283 if (np != pci_dev) {
Ian Munsief14362d2010-10-01 17:06:07 +1000284 const __be32 *reg = of_get_property(np, "reg", NULL);
285 if (reg && (be32_to_cpup(reg) < 4))
286 index = lindex = be32_to_cpup(reg);
Benjamin Herrenschmidt8dacaed2005-11-29 11:21:59 +1100287 }
288
289 /* Local index means it's the Nth port in the PCI chip. Unfortunately
290 * the offset to add here is device specific. We know about those
291 * EXAR ports and we default to the most common case. If your UART
292 * doesn't work for these settings, you'll have to add your own special
293 * cases here
294 */
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000295 if (of_device_is_compatible(pci_dev, "pci13a8,152") ||
296 of_device_is_compatible(pci_dev, "pci13a8,154") ||
297 of_device_is_compatible(pci_dev, "pci13a8,158")) {
Benjamin Herrenschmidt8dacaed2005-11-29 11:21:59 +1100298 addr += 0x200 * lindex;
299 base += 0x200 * lindex;
300 } else {
301 addr += 8 * lindex;
302 base += 8 * lindex;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100303 }
304
305 /* Add port, irq will be dealt with later. We passed a translated
306 * IO port value. It will be fixed up later along with the irq
307 */
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000308 return add_legacy_port(np, index, iotype, base, addr, NO_IRQ,
309 UPF_BOOT_AUTOCONF, np != pci_dev);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100310}
Kumar Gala017e0fa2006-01-03 16:15:21 -0600311#endif
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100312
Michael Neuling37a801c2006-03-26 10:07:35 +1000313static void __init setup_legacy_serial_console(int console)
314{
Benjamin Herrenschmidt30925742013-07-15 13:03:12 +1000315 struct legacy_serial_info *info = &legacy_serial_infos[console];
316 struct plat_serial8250_port *port = &legacy_serial_ports[console];
Michael Neuling37a801c2006-03-26 10:07:35 +1000317 void __iomem *addr;
318
Benjamin Herrenschmidt30925742013-07-15 13:03:12 +1000319 /* Check if a translated MMIO address has been found */
320 if (info->taddr) {
321 addr = ioremap(info->taddr, 0x1000);
322 if (addr == NULL)
323 return;
324 udbg_uart_init_mmio(addr, 1);
325 } else {
326 /* Check if it's PIO and we support untranslated PIO */
327 if (port->iotype == UPIO_PORT && isa_io_special)
328 udbg_uart_init_pio(port->iobase, 1);
329 else
330 return;
331 }
332
333 /* Try to query the current speed */
Michael Neuling37a801c2006-03-26 10:07:35 +1000334 if (info->speed == 0)
Benjamin Herrenschmidt30925742013-07-15 13:03:12 +1000335 info->speed = udbg_probe_uart_speed(info->clock);
336
337 /* Set it up */
Michael Neuling37a801c2006-03-26 10:07:35 +1000338 DBG("default console speed = %d\n", info->speed);
Benjamin Herrenschmidt30925742013-07-15 13:03:12 +1000339 udbg_uart_setup(info->speed, info->clock);
Michael Neuling37a801c2006-03-26 10:07:35 +1000340}
341
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100342/*
343 * This is called very early, as part of setup_system() or eventually
344 * setup_arch(), basically before anything else in this file. This function
345 * will try to build a list of all the available 8250-compatible serial ports
346 * in the machine using the Open Firmware device-tree. It currently only deals
347 * with ISA and PCI busses but could be extended. It allows a very early boot
348 * console to be initialized, that list is also used later to provide 8250 with
349 * the machine non-PCI ports and to properly pick the default console port
350 */
351void __init find_legacy_serial_ports(void)
352{
Kumar Galab580d462005-12-20 16:16:52 -0600353 struct device_node *np, *stdout = NULL;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000354 const char *path;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100355 int index;
356
357 DBG(" -> find_legacy_serial_port()\n");
358
359 /* Now find out if one of these is out firmware console */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000360 path = of_get_property(of_chosen, "linux,stdout-path", NULL);
Kumar Galab580d462005-12-20 16:16:52 -0600361 if (path != NULL) {
362 stdout = of_find_node_by_path(path);
363 if (stdout)
364 DBG("stdout is %s\n", stdout->full_name);
365 } else {
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100366 DBG(" no linux,stdout-path !\n");
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100367 }
Kumar Galab580d462005-12-20 16:16:52 -0600368
Paul Gortmaker1a7507c2008-01-24 11:59:12 -0500369 /* Iterate over all the 16550 ports, looking for known parents */
Cyrill Gorcunov3329c0d2007-11-30 06:45:47 +1100370 for_each_compatible_node(np, "serial", "ns16550") {
Paul Gortmaker1a7507c2008-01-24 11:59:12 -0500371 struct device_node *parent = of_get_parent(np);
372 if (!parent)
373 continue;
Benjamin Herrenschmidt3bc5ab92008-07-07 16:39:50 +1000374 if (of_match_node(legacy_serial_parents, parent) != NULL) {
Prabhakar Kushwaha07d9fce2011-04-06 12:56:29 +0530375 if (of_device_is_available(np)) {
376 index = add_legacy_soc_port(np, np);
377 if (index >= 0 && np == stdout)
378 legacy_serial_console = index;
379 }
Kumar Galab580d462005-12-20 16:16:52 -0600380 }
Paul Gortmaker1a7507c2008-01-24 11:59:12 -0500381 of_node_put(parent);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100382 }
383
Paul Gortmaker1a7507c2008-01-24 11:59:12 -0500384 /* Next, fill our array with ISA ports */
Cyrill Gorcunov3329c0d2007-11-30 06:45:47 +1100385 for_each_node_by_type(np, "serial") {
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100386 struct device_node *isa = of_get_parent(np);
Benjamin Herrenschmidt30925742013-07-15 13:03:12 +1000387 if (isa && (!strcmp(isa->name, "isa") ||
388 !strcmp(isa->name, "lpc"))) {
Benjamin Herrenschmidte0f5fa92013-07-15 13:03:13 +1000389 if (of_device_is_available(np)) {
390 index = add_legacy_isa_port(np, isa);
391 if (index >= 0 && np == stdout)
392 legacy_serial_console = index;
393 }
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100394 }
395 of_node_put(isa);
396 }
397
Kumar Gala017e0fa2006-01-03 16:15:21 -0600398#ifdef CONFIG_PCI
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100399 /* Next, try to locate PCI ports */
400 for (np = NULL; (np = of_find_all_nodes(np));) {
401 struct device_node *pci, *parent = of_get_parent(np);
402 if (parent && !strcmp(parent->name, "isa")) {
403 of_node_put(parent);
404 continue;
405 }
406 if (strcmp(np->name, "serial") && strcmp(np->type, "serial")) {
407 of_node_put(parent);
408 continue;
409 }
Adam Buchbinder48fc7f72012-09-19 21:48:00 -0400410 /* Check for known pciclass, and also check whether we have
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100411 * a device with child nodes for ports or not
412 */
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000413 if (of_device_is_compatible(np, "pciclass,0700") ||
414 of_device_is_compatible(np, "pciclass,070002"))
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100415 pci = np;
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000416 else if (of_device_is_compatible(parent, "pciclass,0700") ||
417 of_device_is_compatible(parent, "pciclass,070002"))
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100418 pci = parent;
419 else {
420 of_node_put(parent);
421 continue;
422 }
423 index = add_legacy_pci_port(np, pci);
424 if (index >= 0 && np == stdout)
425 legacy_serial_console = index;
426 of_node_put(parent);
427 }
Kumar Gala017e0fa2006-01-03 16:15:21 -0600428#endif
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100429
430 DBG("legacy_serial_console = %d\n", legacy_serial_console);
Michael Neuling37a801c2006-03-26 10:07:35 +1000431 if (legacy_serial_console >= 0)
432 setup_legacy_serial_console(legacy_serial_console);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100433 DBG(" <- find_legacy_serial_port()\n");
434}
435
436static struct platform_device serial_device = {
437 .name = "serial8250",
438 .id = PLAT8250_DEV_PLATFORM,
439 .dev = {
440 .platform_data = legacy_serial_ports,
441 },
442};
443
444static void __init fixup_port_irq(int index,
445 struct device_node *np,
446 struct plat_serial8250_port *port)
447{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000448 unsigned int virq;
449
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100450 DBG("fixup_port_irq(%d)\n", index);
451
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000452 virq = irq_of_parse_and_map(np, 0);
453 if (virq == NO_IRQ && legacy_serial_infos[index].irq_check_parent) {
454 np = of_get_parent(np);
455 if (np == NULL)
456 return;
457 virq = irq_of_parse_and_map(np, 0);
458 of_node_put(np);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100459 }
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000460 if (virq == NO_IRQ)
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100461 return;
462
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000463 port->irq = virq;
Paul Gortmaker9deaa532011-12-04 18:42:23 -0500464
Benjamin Herrenschmidt3493c852012-01-25 13:33:22 +1100465#ifdef CONFIG_SERIAL_8250_FSL
Paul Gortmaker9deaa532011-12-04 18:42:23 -0500466 if (of_device_is_compatible(np, "fsl,ns16550"))
467 port->handle_irq = fsl8250_handle_irq;
Benjamin Herrenschmidt3493c852012-01-25 13:33:22 +1100468#endif
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100469}
470
471static void __init fixup_port_pio(int index,
472 struct device_node *np,
473 struct plat_serial8250_port *port)
474{
Kumar Gala017e0fa2006-01-03 16:15:21 -0600475#ifdef CONFIG_PCI
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100476 struct pci_controller *hose;
477
478 DBG("fixup_port_pio(%d)\n", index);
479
480 hose = pci_find_hose_for_OF_device(np);
481 if (hose) {
482 unsigned long offset = (unsigned long)hose->io_base_virt -
483#ifdef CONFIG_PPC64
484 pci_io_base;
485#else
486 isa_io_base;
487#endif
488 DBG("port %d, IO %lx -> %lx\n",
489 index, port->iobase, port->iobase + offset);
490 port->iobase += offset;
491 }
Kumar Gala017e0fa2006-01-03 16:15:21 -0600492#endif
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100493}
494
Benjamin Herrenschmidt8dacaed2005-11-29 11:21:59 +1100495static void __init fixup_port_mmio(int index,
496 struct device_node *np,
497 struct plat_serial8250_port *port)
498{
499 DBG("fixup_port_mmio(%d)\n", index);
500
501 port->membase = ioremap(port->mapbase, 0x100);
502}
503
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100504/*
505 * This is called as an arch initcall, hopefully before the PCI bus is
506 * probed and/or the 8250 driver loaded since we need to register our
507 * platform devices before 8250 PCI ones are detected as some of them
508 * must properly "override" the platform ones.
509 *
510 * This function fixes up the interrupt value for platform ports as it
511 * couldn't be done earlier before interrupt maps have been parsed. It
512 * also "corrects" the IO address for PIO ports for the same reason,
513 * since earlier, the PHBs virtual IO space wasn't assigned yet. It then
514 * registers all those platform ports for use by the 8250 driver when it
515 * finally loads.
516 */
517static int __init serial_dev_init(void)
518{
519 int i;
520
521 if (legacy_serial_count == 0)
522 return -ENODEV;
523
524 /*
Stefan Weil947af292010-01-07 00:03:52 +0100525 * Before we register the platform serial devices, we need
joe@perches.com00d70412007-12-18 06:30:12 +1100526 * to fixup their interrupts and their IO ports.
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100527 */
528 DBG("Fixing serial ports interrupts and IO ports ...\n");
529
530 for (i = 0; i < legacy_serial_count; i++) {
531 struct plat_serial8250_port *port = &legacy_serial_ports[i];
532 struct device_node *np = legacy_serial_infos[i].np;
533
534 if (port->irq == NO_IRQ)
535 fixup_port_irq(i, np, port);
536 if (port->iotype == UPIO_PORT)
537 fixup_port_pio(i, np, port);
Zang Roy-r61911be9633e2006-08-23 10:20:27 +0800538 if ((port->iotype == UPIO_MEM) || (port->iotype == UPIO_TSI))
Benjamin Herrenschmidt8dacaed2005-11-29 11:21:59 +1100539 fixup_port_mmio(i, np, port);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100540 }
541
542 DBG("Registering platform serial ports\n");
543
544 return platform_device_register(&serial_device);
545}
Olof Johanssonee56c472007-08-22 19:26:37 -0500546device_initcall(serial_dev_init);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100547
548
Benjamin Herrenschmidt025d7912008-07-28 13:49:15 +1000549#ifdef CONFIG_SERIAL_8250_CONSOLE
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100550/*
551 * This is called very early, as part of console_init() (typically just after
552 * time_init()). This function is respondible for trying to find a good
553 * default console on serial ports. It tries to match the open firmware
Benjamin Herrenschmidt025d7912008-07-28 13:49:15 +1000554 * default output with one of the available serial console drivers that have
555 * been probed earlier by find_legacy_serial_ports()
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100556 */
557static int __init check_legacy_serial_console(void)
558{
559 struct device_node *prom_stdout = NULL;
Benjamin Herrenschmidt025d7912008-07-28 13:49:15 +1000560 int i, speed = 0, offset = 0;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000561 const char *name;
Ian Munsief14362d2010-10-01 17:06:07 +1000562 const __be32 *spd;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100563
564 DBG(" -> check_legacy_serial_console()\n");
565
566 /* The user has requested a console so this is already set up. */
Alon Bar-Levb8757b22007-02-12 00:54:17 -0800567 if (strstr(boot_command_line, "console=")) {
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100568 DBG(" console was specified !\n");
569 return -EBUSY;
570 }
571
572 if (!of_chosen) {
573 DBG(" of_chosen is NULL !\n");
574 return -ENODEV;
575 }
Kumar Galab580d462005-12-20 16:16:52 -0600576
577 if (legacy_serial_console < 0) {
578 DBG(" legacy_serial_console not found !\n");
579 return -ENODEV;
580 }
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100581 /* We are getting a weird phandle from OF ... */
582 /* ... So use the full path instead */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000583 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100584 if (name == NULL) {
585 DBG(" no linux,stdout-path !\n");
586 return -ENODEV;
587 }
588 prom_stdout = of_find_node_by_path(name);
589 if (!prom_stdout) {
590 DBG(" can't find stdout package %s !\n", name);
591 return -ENODEV;
592 }
593 DBG("stdout is %s\n", prom_stdout->full_name);
594
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000595 name = of_get_property(prom_stdout, "name", NULL);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100596 if (!name) {
597 DBG(" stdout package has no name !\n");
598 goto not_found;
599 }
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000600 spd = of_get_property(prom_stdout, "current-speed", NULL);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100601 if (spd)
Ian Munsief14362d2010-10-01 17:06:07 +1000602 speed = be32_to_cpup(spd);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100603
Benjamin Herrenschmidt025d7912008-07-28 13:49:15 +1000604 if (strcmp(name, "serial") != 0)
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100605 goto not_found;
Benjamin Herrenschmidt025d7912008-07-28 13:49:15 +1000606
607 /* Look for it in probed array */
608 for (i = 0; i < legacy_serial_count; i++) {
609 if (prom_stdout != legacy_serial_infos[i].np)
610 continue;
611 offset = i;
612 speed = legacy_serial_infos[i].speed;
613 break;
614 }
615 if (i >= legacy_serial_count)
616 goto not_found;
617
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100618 of_node_put(prom_stdout);
619
620 DBG("Found serial console at ttyS%d\n", offset);
621
622 if (speed) {
623 static char __initdata opt[16];
624 sprintf(opt, "%d", speed);
625 return add_preferred_console("ttyS", offset, opt);
626 } else
627 return add_preferred_console("ttyS", offset, NULL);
628
629 not_found:
630 DBG("No preferred console found !\n");
631 of_node_put(prom_stdout);
632 return -ENODEV;
633}
634console_initcall(check_legacy_serial_console);
635
Benjamin Herrenschmidt025d7912008-07-28 13:49:15 +1000636#endif /* CONFIG_SERIAL_8250_CONSOLE */