blob: cf37f5ca4b71e6b30c2a3d7bb46fb17f8bf66f50 [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>
Paul Gortmaker1a7507c2008-01-24 11:59:12 -05007#include <linux/of_device.h>
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +11008#include <asm/io.h>
9#include <asm/mmu.h>
10#include <asm/prom.h>
11#include <asm/serial.h>
12#include <asm/udbg.h>
13#include <asm/pci-bridge.h>
14#include <asm/ppc-pci.h>
15
16#undef DEBUG
17
18#ifdef DEBUG
19#define DBG(fmt...) do { printk(fmt); } while(0)
20#else
21#define DBG(fmt...) do { } while(0)
22#endif
23
24#define MAX_LEGACY_SERIAL_PORTS 8
25
26static struct plat_serial8250_port
27legacy_serial_ports[MAX_LEGACY_SERIAL_PORTS+1];
28static struct legacy_serial_info {
29 struct device_node *np;
30 unsigned int speed;
31 unsigned int clock;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100032 int irq_check_parent;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110033 phys_addr_t taddr;
34} legacy_serial_infos[MAX_LEGACY_SERIAL_PORTS];
Paul Gortmaker1a7507c2008-01-24 11:59:12 -050035
36static struct __initdata of_device_id parents[] = {
37 {.type = "soc",},
38 {.type = "tsi-bridge",},
Michael Ellermanf5903ed2008-02-05 23:01:50 +110039 {.type = "opb", },
40 {.compatible = "ibm,opb",},
Paul Gortmaker1a7507c2008-01-24 11:59:12 -050041 {.compatible = "simple-bus",},
42 {.compatible = "wrs,epld-localbus",},
43};
44
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110045static unsigned int legacy_serial_count;
46static int legacy_serial_console = -1;
47
48static int __init add_legacy_port(struct device_node *np, int want_index,
49 int iotype, phys_addr_t base,
Kumar Galab580d462005-12-20 16:16:52 -060050 phys_addr_t taddr, unsigned long irq,
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100051 upf_t flags, int irq_check_parent)
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110052{
Jeremy Kerra7f67bd2006-07-12 15:35:54 +100053 const u32 *clk, *spd;
54 u32 clock = BASE_BAUD * 16;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110055 int index;
56
57 /* get clock freq. if present */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +100058 clk = of_get_property(np, "clock-frequency", NULL);
David Woodhouse31df1672005-11-24 17:08:56 +000059 if (clk && *clk)
60 clock = *clk;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110061
62 /* get default speed if present */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +100063 spd = of_get_property(np, "current-speed", NULL);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110064
65 /* If we have a location index, then try to use it */
66 if (want_index >= 0 && want_index < MAX_LEGACY_SERIAL_PORTS)
67 index = want_index;
68 else
69 index = legacy_serial_count;
70
71 /* if our index is still out of range, that mean that
72 * array is full, we could scan for a free slot but that
73 * make little sense to bother, just skip the port
74 */
75 if (index >= MAX_LEGACY_SERIAL_PORTS)
76 return -1;
77 if (index >= legacy_serial_count)
78 legacy_serial_count = index + 1;
79
80 /* Check if there is a port who already claimed our slot */
81 if (legacy_serial_infos[index].np != 0) {
82 /* if we still have some room, move it, else override */
83 if (legacy_serial_count < MAX_LEGACY_SERIAL_PORTS) {
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100084 printk(KERN_DEBUG "Moved legacy port %d -> %d\n",
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110085 index, legacy_serial_count);
86 legacy_serial_ports[legacy_serial_count] =
87 legacy_serial_ports[index];
88 legacy_serial_infos[legacy_serial_count] =
89 legacy_serial_infos[index];
90 legacy_serial_count++;
91 } else {
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100092 printk(KERN_DEBUG "Replacing legacy port %d\n", index);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110093 }
94 }
95
96 /* Now fill the entry */
97 memset(&legacy_serial_ports[index], 0,
98 sizeof(struct plat_serial8250_port));
99 if (iotype == UPIO_PORT)
100 legacy_serial_ports[index].iobase = base;
101 else
Benjamin Herrenschmidt8dacaed2005-11-29 11:21:59 +1100102 legacy_serial_ports[index].mapbase = base;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100103 legacy_serial_ports[index].iotype = iotype;
104 legacy_serial_ports[index].uartclk = clock;
105 legacy_serial_ports[index].irq = irq;
Kumar Galab580d462005-12-20 16:16:52 -0600106 legacy_serial_ports[index].flags = flags;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100107 legacy_serial_infos[index].taddr = taddr;
108 legacy_serial_infos[index].np = of_node_get(np);
109 legacy_serial_infos[index].clock = clock;
110 legacy_serial_infos[index].speed = spd ? *spd : 0;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000111 legacy_serial_infos[index].irq_check_parent = irq_check_parent;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100112
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000113 printk(KERN_DEBUG "Found legacy serial port %d for %s\n",
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100114 index, np->full_name);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000115 printk(KERN_DEBUG " %s=%llx, taddr=%llx, irq=%lx, clk=%d, speed=%d\n",
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100116 (iotype == UPIO_PORT) ? "port" : "mem",
117 (unsigned long long)base, (unsigned long long)taddr, irq,
118 legacy_serial_ports[index].uartclk,
119 legacy_serial_infos[index].speed);
120
121 return index;
122}
123
Kumar Galab580d462005-12-20 16:16:52 -0600124static int __init add_legacy_soc_port(struct device_node *np,
125 struct device_node *soc_dev)
126{
Benjamin Herrenschmidtf704b8d2006-07-04 14:14:07 +1000127 u64 addr;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000128 const u32 *addrp;
David Gibsonabb4a232007-05-06 14:48:49 -0700129 upf_t flags = UPF_BOOT_AUTOCONF | UPF_SKIP_TEST | UPF_SHARE_IRQ
130 | UPF_FIXED_PORT;
Zang Roy-r61911be9633e2006-08-23 10:20:27 +0800131 struct device_node *tsi = of_get_parent(np);
Kumar Galab580d462005-12-20 16:16:52 -0600132
133 /* We only support ports that have a clock frequency properly
134 * encoded in the device-tree.
135 */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000136 if (of_get_property(np, "clock-frequency", NULL) == NULL)
Kumar Galab580d462005-12-20 16:16:52 -0600137 return -1;
138
John Linn1e6d1f22008-07-01 10:52:41 -0700139 /* if reg-shift or offset, don't try to use it */
140 if ((of_get_property(np, "reg-shift", NULL) != NULL) ||
141 (of_get_property(np, "reg-offset", NULL) != NULL))
142 return -1;
143
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100144 /* if rtas uses this device, don't try to use it as well */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000145 if (of_get_property(np, "used-by-rtas", NULL) != NULL)
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100146 return -1;
147
Kumar Galab580d462005-12-20 16:16:52 -0600148 /* Get the address */
149 addrp = of_get_address(soc_dev, 0, NULL, NULL);
150 if (addrp == NULL)
151 return -1;
152
153 addr = of_translate_address(soc_dev, addrp);
Benjamin Herrenschmidt7c6efda2006-07-03 17:24:15 +1000154 if (addr == OF_BAD_ADDR)
155 return -1;
Kumar Galab580d462005-12-20 16:16:52 -0600156
157 /* Add port, irq will be dealt with later. We passed a translated
158 * IO port value. It will be fixed up later along with the irq
159 */
Zang Roy-r61911be9633e2006-08-23 10:20:27 +0800160 if (tsi && !strcmp(tsi->type, "tsi-bridge"))
161 return add_legacy_port(np, -1, UPIO_TSI, addr, addr, NO_IRQ, flags, 0);
162 else
163 return add_legacy_port(np, -1, UPIO_MEM, addr, addr, NO_IRQ, flags, 0);
Kumar Galab580d462005-12-20 16:16:52 -0600164}
165
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100166static int __init add_legacy_isa_port(struct device_node *np,
Kumar Gala017e0fa2006-01-03 16:15:21 -0600167 struct device_node *isa_brg)
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100168{
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000169 const u32 *reg;
170 const char *typep;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100171 int index = -1;
Benjamin Herrenschmidtf704b8d2006-07-04 14:14:07 +1000172 u64 taddr;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100173
Benjamin Herrenschmidt7c6efda2006-07-03 17:24:15 +1000174 DBG(" -> add_legacy_isa_port(%s)\n", np->full_name);
175
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100176 /* Get the ISA port number */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000177 reg = of_get_property(np, "reg", NULL);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100178 if (reg == NULL)
179 return -1;
180
181 /* Verify it's an IO port, we don't support anything else */
182 if (!(reg[0] & 0x00000001))
183 return -1;
184
185 /* Now look for an "ibm,aix-loc" property that gives us ordering
186 * if any...
187 */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000188 typep = of_get_property(np, "ibm,aix-loc", NULL);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100189
190 /* If we have a location index, then use it */
191 if (typep && *typep == 'S')
192 index = simple_strtol(typep+1, NULL, 0) - 1;
193
Benjamin Herrenschmidtf704b8d2006-07-04 14:14:07 +1000194 /* Translate ISA address. If it fails, we still register the port
195 * with no translated address so that it can be picked up as an IO
196 * port later by the serial driver
197 */
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100198 taddr = of_translate_address(np, reg);
Benjamin Herrenschmidt7c6efda2006-07-03 17:24:15 +1000199 if (taddr == OF_BAD_ADDR)
Benjamin Herrenschmidtf704b8d2006-07-04 14:14:07 +1000200 taddr = 0;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100201
202 /* Add port, irq will be dealt with later */
Benjamin Herrenschmidt7c6efda2006-07-03 17:24:15 +1000203 return add_legacy_port(np, index, UPIO_PORT, reg[1], taddr,
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000204 NO_IRQ, UPF_BOOT_AUTOCONF, 0);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100205
206}
207
Kumar Gala017e0fa2006-01-03 16:15:21 -0600208#ifdef CONFIG_PCI
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100209static int __init add_legacy_pci_port(struct device_node *np,
210 struct device_node *pci_dev)
211{
Benjamin Herrenschmidtf704b8d2006-07-04 14:14:07 +1000212 u64 addr, base;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000213 const u32 *addrp;
Benjamin Herrenschmidtd2dd4822005-11-30 16:57:28 +1100214 unsigned int flags;
Benjamin Herrenschmidt8dacaed2005-11-29 11:21:59 +1100215 int iotype, index = -1, lindex = 0;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100216
Benjamin Herrenschmidt7c6efda2006-07-03 17:24:15 +1000217 DBG(" -> add_legacy_pci_port(%s)\n", np->full_name);
218
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100219 /* We only support ports that have a clock frequency properly
220 * encoded in the device-tree (that is have an fcode). Anything
221 * else can't be used that early and will be normally probed by
Benjamin Herrenschmidt8dacaed2005-11-29 11:21:59 +1100222 * the generic 8250_pci driver later on. The reason is that 8250
223 * compatible UARTs on PCI need all sort of quirks (port offsets
224 * etc...) that this code doesn't know about
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100225 */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000226 if (of_get_property(np, "clock-frequency", NULL) == NULL)
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100227 return -1;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100228
229 /* Get the PCI address. Assume BAR 0 */
Benjamin Herrenschmidtd2dd4822005-11-30 16:57:28 +1100230 addrp = of_get_pci_address(pci_dev, 0, NULL, &flags);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100231 if (addrp == NULL)
232 return -1;
233
234 /* We only support BAR 0 for now */
Benjamin Herrenschmidtd2dd4822005-11-30 16:57:28 +1100235 iotype = (flags & IORESOURCE_MEM) ? UPIO_MEM : UPIO_PORT;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100236 addr = of_translate_address(pci_dev, addrp);
Benjamin Herrenschmidt7c6efda2006-07-03 17:24:15 +1000237 if (addr == OF_BAD_ADDR)
238 return -1;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100239
240 /* Set the IO base to the same as the translated address for MMIO,
241 * or to the domain local IO base for PIO (it will be fixed up later)
242 */
243 if (iotype == UPIO_MEM)
244 base = addr;
245 else
246 base = addrp[2];
247
248 /* Try to guess an index... If we have subdevices of the pci dev,
249 * we get to their "reg" property
250 */
251 if (np != pci_dev) {
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000252 const u32 *reg = of_get_property(np, "reg", NULL);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100253 if (reg && (*reg < 4))
Benjamin Herrenschmidt8dacaed2005-11-29 11:21:59 +1100254 index = lindex = *reg;
255 }
256
257 /* Local index means it's the Nth port in the PCI chip. Unfortunately
258 * the offset to add here is device specific. We know about those
259 * EXAR ports and we default to the most common case. If your UART
260 * doesn't work for these settings, you'll have to add your own special
261 * cases here
262 */
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000263 if (of_device_is_compatible(pci_dev, "pci13a8,152") ||
264 of_device_is_compatible(pci_dev, "pci13a8,154") ||
265 of_device_is_compatible(pci_dev, "pci13a8,158")) {
Benjamin Herrenschmidt8dacaed2005-11-29 11:21:59 +1100266 addr += 0x200 * lindex;
267 base += 0x200 * lindex;
268 } else {
269 addr += 8 * lindex;
270 base += 8 * lindex;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100271 }
272
273 /* Add port, irq will be dealt with later. We passed a translated
274 * IO port value. It will be fixed up later along with the irq
275 */
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000276 return add_legacy_port(np, index, iotype, base, addr, NO_IRQ,
277 UPF_BOOT_AUTOCONF, np != pci_dev);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100278}
Kumar Gala017e0fa2006-01-03 16:15:21 -0600279#endif
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100280
Michael Neuling37a801c2006-03-26 10:07:35 +1000281static void __init setup_legacy_serial_console(int console)
282{
283 struct legacy_serial_info *info =
284 &legacy_serial_infos[console];
285 void __iomem *addr;
286
287 if (info->taddr == 0)
288 return;
289 addr = ioremap(info->taddr, 0x1000);
290 if (addr == NULL)
291 return;
292 if (info->speed == 0)
293 info->speed = udbg_probe_uart_speed(addr, info->clock);
294 DBG("default console speed = %d\n", info->speed);
295 udbg_init_uart(addr, info->speed, info->clock);
296}
297
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100298/*
299 * This is called very early, as part of setup_system() or eventually
300 * setup_arch(), basically before anything else in this file. This function
301 * will try to build a list of all the available 8250-compatible serial ports
302 * in the machine using the Open Firmware device-tree. It currently only deals
303 * with ISA and PCI busses but could be extended. It allows a very early boot
304 * console to be initialized, that list is also used later to provide 8250 with
305 * the machine non-PCI ports and to properly pick the default console port
306 */
307void __init find_legacy_serial_ports(void)
308{
Kumar Galab580d462005-12-20 16:16:52 -0600309 struct device_node *np, *stdout = NULL;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000310 const char *path;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100311 int index;
312
313 DBG(" -> find_legacy_serial_port()\n");
314
315 /* Now find out if one of these is out firmware console */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000316 path = of_get_property(of_chosen, "linux,stdout-path", NULL);
Kumar Galab580d462005-12-20 16:16:52 -0600317 if (path != NULL) {
318 stdout = of_find_node_by_path(path);
319 if (stdout)
320 DBG("stdout is %s\n", stdout->full_name);
321 } else {
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100322 DBG(" no linux,stdout-path !\n");
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100323 }
Kumar Galab580d462005-12-20 16:16:52 -0600324
Paul Gortmaker1a7507c2008-01-24 11:59:12 -0500325 /* Iterate over all the 16550 ports, looking for known parents */
Cyrill Gorcunov3329c0d2007-11-30 06:45:47 +1100326 for_each_compatible_node(np, "serial", "ns16550") {
Paul Gortmaker1a7507c2008-01-24 11:59:12 -0500327 struct device_node *parent = of_get_parent(np);
328 if (!parent)
329 continue;
330 if (of_match_node(parents, parent) != NULL) {
Kumar Galab580d462005-12-20 16:16:52 -0600331 index = add_legacy_soc_port(np, np);
332 if (index >= 0 && np == stdout)
333 legacy_serial_console = index;
334 }
Paul Gortmaker1a7507c2008-01-24 11:59:12 -0500335 of_node_put(parent);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100336 }
337
Paul Gortmaker1a7507c2008-01-24 11:59:12 -0500338 /* Next, fill our array with ISA ports */
Cyrill Gorcunov3329c0d2007-11-30 06:45:47 +1100339 for_each_node_by_type(np, "serial") {
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100340 struct device_node *isa = of_get_parent(np);
341 if (isa && !strcmp(isa->name, "isa")) {
342 index = add_legacy_isa_port(np, isa);
343 if (index >= 0 && np == stdout)
344 legacy_serial_console = index;
345 }
346 of_node_put(isa);
347 }
348
Kumar Gala017e0fa2006-01-03 16:15:21 -0600349#ifdef CONFIG_PCI
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100350 /* Next, try to locate PCI ports */
351 for (np = NULL; (np = of_find_all_nodes(np));) {
352 struct device_node *pci, *parent = of_get_parent(np);
353 if (parent && !strcmp(parent->name, "isa")) {
354 of_node_put(parent);
355 continue;
356 }
357 if (strcmp(np->name, "serial") && strcmp(np->type, "serial")) {
358 of_node_put(parent);
359 continue;
360 }
361 /* Check for known pciclass, and also check wether we have
362 * a device with child nodes for ports or not
363 */
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000364 if (of_device_is_compatible(np, "pciclass,0700") ||
365 of_device_is_compatible(np, "pciclass,070002"))
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100366 pci = np;
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000367 else if (of_device_is_compatible(parent, "pciclass,0700") ||
368 of_device_is_compatible(parent, "pciclass,070002"))
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100369 pci = parent;
370 else {
371 of_node_put(parent);
372 continue;
373 }
374 index = add_legacy_pci_port(np, pci);
375 if (index >= 0 && np == stdout)
376 legacy_serial_console = index;
377 of_node_put(parent);
378 }
Kumar Gala017e0fa2006-01-03 16:15:21 -0600379#endif
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100380
381 DBG("legacy_serial_console = %d\n", legacy_serial_console);
Michael Neuling37a801c2006-03-26 10:07:35 +1000382 if (legacy_serial_console >= 0)
383 setup_legacy_serial_console(legacy_serial_console);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100384 DBG(" <- find_legacy_serial_port()\n");
385}
386
387static struct platform_device serial_device = {
388 .name = "serial8250",
389 .id = PLAT8250_DEV_PLATFORM,
390 .dev = {
391 .platform_data = legacy_serial_ports,
392 },
393};
394
395static void __init fixup_port_irq(int index,
396 struct device_node *np,
397 struct plat_serial8250_port *port)
398{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000399 unsigned int virq;
400
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100401 DBG("fixup_port_irq(%d)\n", index);
402
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000403 virq = irq_of_parse_and_map(np, 0);
404 if (virq == NO_IRQ && legacy_serial_infos[index].irq_check_parent) {
405 np = of_get_parent(np);
406 if (np == NULL)
407 return;
408 virq = irq_of_parse_and_map(np, 0);
409 of_node_put(np);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100410 }
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000411 if (virq == NO_IRQ)
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100412 return;
413
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000414 port->irq = virq;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100415}
416
417static void __init fixup_port_pio(int index,
418 struct device_node *np,
419 struct plat_serial8250_port *port)
420{
Kumar Gala017e0fa2006-01-03 16:15:21 -0600421#ifdef CONFIG_PCI
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100422 struct pci_controller *hose;
423
424 DBG("fixup_port_pio(%d)\n", index);
425
426 hose = pci_find_hose_for_OF_device(np);
427 if (hose) {
428 unsigned long offset = (unsigned long)hose->io_base_virt -
429#ifdef CONFIG_PPC64
430 pci_io_base;
431#else
432 isa_io_base;
433#endif
434 DBG("port %d, IO %lx -> %lx\n",
435 index, port->iobase, port->iobase + offset);
436 port->iobase += offset;
437 }
Kumar Gala017e0fa2006-01-03 16:15:21 -0600438#endif
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100439}
440
Benjamin Herrenschmidt8dacaed2005-11-29 11:21:59 +1100441static void __init fixup_port_mmio(int index,
442 struct device_node *np,
443 struct plat_serial8250_port *port)
444{
445 DBG("fixup_port_mmio(%d)\n", index);
446
447 port->membase = ioremap(port->mapbase, 0x100);
448}
449
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100450/*
451 * This is called as an arch initcall, hopefully before the PCI bus is
452 * probed and/or the 8250 driver loaded since we need to register our
453 * platform devices before 8250 PCI ones are detected as some of them
454 * must properly "override" the platform ones.
455 *
456 * This function fixes up the interrupt value for platform ports as it
457 * couldn't be done earlier before interrupt maps have been parsed. It
458 * also "corrects" the IO address for PIO ports for the same reason,
459 * since earlier, the PHBs virtual IO space wasn't assigned yet. It then
460 * registers all those platform ports for use by the 8250 driver when it
461 * finally loads.
462 */
463static int __init serial_dev_init(void)
464{
465 int i;
466
467 if (legacy_serial_count == 0)
468 return -ENODEV;
469
470 /*
471 * Before we register the platfrom serial devices, we need
joe@perches.com00d70412007-12-18 06:30:12 +1100472 * to fixup their interrupts and their IO ports.
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100473 */
474 DBG("Fixing serial ports interrupts and IO ports ...\n");
475
476 for (i = 0; i < legacy_serial_count; i++) {
477 struct plat_serial8250_port *port = &legacy_serial_ports[i];
478 struct device_node *np = legacy_serial_infos[i].np;
479
480 if (port->irq == NO_IRQ)
481 fixup_port_irq(i, np, port);
482 if (port->iotype == UPIO_PORT)
483 fixup_port_pio(i, np, port);
Zang Roy-r61911be9633e2006-08-23 10:20:27 +0800484 if ((port->iotype == UPIO_MEM) || (port->iotype == UPIO_TSI))
Benjamin Herrenschmidt8dacaed2005-11-29 11:21:59 +1100485 fixup_port_mmio(i, np, port);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100486 }
487
488 DBG("Registering platform serial ports\n");
489
490 return platform_device_register(&serial_device);
491}
Olof Johanssonee56c472007-08-22 19:26:37 -0500492device_initcall(serial_dev_init);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100493
494
495/*
496 * This is called very early, as part of console_init() (typically just after
497 * time_init()). This function is respondible for trying to find a good
498 * default console on serial ports. It tries to match the open firmware
499 * default output with one of the available serial console drivers, either
500 * one of the platform serial ports that have been probed earlier by
501 * find_legacy_serial_ports() or some more platform specific ones.
502 */
503static int __init check_legacy_serial_console(void)
504{
505 struct device_node *prom_stdout = NULL;
506 int speed = 0, offset = 0;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000507 const char *name;
508 const u32 *spd;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100509
510 DBG(" -> check_legacy_serial_console()\n");
511
512 /* The user has requested a console so this is already set up. */
Alon Bar-Levb8757b22007-02-12 00:54:17 -0800513 if (strstr(boot_command_line, "console=")) {
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100514 DBG(" console was specified !\n");
515 return -EBUSY;
516 }
517
518 if (!of_chosen) {
519 DBG(" of_chosen is NULL !\n");
520 return -ENODEV;
521 }
Kumar Galab580d462005-12-20 16:16:52 -0600522
523 if (legacy_serial_console < 0) {
524 DBG(" legacy_serial_console not found !\n");
525 return -ENODEV;
526 }
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100527 /* We are getting a weird phandle from OF ... */
528 /* ... So use the full path instead */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000529 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100530 if (name == NULL) {
531 DBG(" no linux,stdout-path !\n");
532 return -ENODEV;
533 }
534 prom_stdout = of_find_node_by_path(name);
535 if (!prom_stdout) {
536 DBG(" can't find stdout package %s !\n", name);
537 return -ENODEV;
538 }
539 DBG("stdout is %s\n", prom_stdout->full_name);
540
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000541 name = of_get_property(prom_stdout, "name", NULL);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100542 if (!name) {
543 DBG(" stdout package has no name !\n");
544 goto not_found;
545 }
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000546 spd = of_get_property(prom_stdout, "current-speed", NULL);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100547 if (spd)
548 speed = *spd;
549
550 if (0)
551 ;
552#ifdef CONFIG_SERIAL_8250_CONSOLE
553 else if (strcmp(name, "serial") == 0) {
554 int i;
555 /* Look for it in probed array */
556 for (i = 0; i < legacy_serial_count; i++) {
557 if (prom_stdout != legacy_serial_infos[i].np)
558 continue;
559 offset = i;
560 speed = legacy_serial_infos[i].speed;
561 break;
562 }
563 if (i >= legacy_serial_count)
564 goto not_found;
565 }
566#endif /* CONFIG_SERIAL_8250_CONSOLE */
567#ifdef CONFIG_SERIAL_PMACZILOG_CONSOLE
568 else if (strcmp(name, "ch-a") == 0)
569 offset = 0;
570 else if (strcmp(name, "ch-b") == 0)
571 offset = 1;
572#endif /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
573 else
574 goto not_found;
575 of_node_put(prom_stdout);
576
577 DBG("Found serial console at ttyS%d\n", offset);
578
579 if (speed) {
580 static char __initdata opt[16];
581 sprintf(opt, "%d", speed);
582 return add_preferred_console("ttyS", offset, opt);
583 } else
584 return add_preferred_console("ttyS", offset, NULL);
585
586 not_found:
587 DBG("No preferred console found !\n");
588 of_node_put(prom_stdout);
589 return -ENODEV;
590}
591console_initcall(check_legacy_serial_console);
592