1 #include <linux/config.h>
2 #include <linux/kernel.h>
3 #include <linux/serial.h>
4 #include <linux/serial_8250.h>
5 #include <linux/serial_core.h>
6 #include <linux/console.h>
11 #include <asm/serial.h>
13 #include <asm/pci-bridge.h>
14 #include <asm/ppc-pci.h>
19 #define DBG(fmt...) do { printk(fmt); } while(0)
21 #define DBG(fmt...) do { } while(0)
24 #define MAX_LEGACY_SERIAL_PORTS 8
26 static struct plat_serial8250_port
27 legacy_serial_ports[MAX_LEGACY_SERIAL_PORTS+1];
28 static struct legacy_serial_info {
29 struct device_node *np;
33 } legacy_serial_infos[MAX_LEGACY_SERIAL_PORTS];
34 static unsigned int legacy_serial_count;
35 static int legacy_serial_console = -1;
37 static int __init add_legacy_port(struct device_node *np, int want_index,
38 int iotype, phys_addr_t base,
39 phys_addr_t taddr, unsigned long irq)
41 u32 *clk, *spd, clock;
44 /* get clock freq. if present */
45 clk = (u32 *)get_property(np, "clock-frequency", NULL);
46 clock = clk ? *clk : BASE_BAUD * 16;
48 /* get default speed if present */
49 spd = (u32 *)get_property(np, "current-speed", NULL);
51 /* If we have a location index, then try to use it */
52 if (want_index >= 0 && want_index < MAX_LEGACY_SERIAL_PORTS)
55 index = legacy_serial_count;
57 /* if our index is still out of range, that mean that
58 * array is full, we could scan for a free slot but that
59 * make little sense to bother, just skip the port
61 if (index >= MAX_LEGACY_SERIAL_PORTS)
63 if (index >= legacy_serial_count)
64 legacy_serial_count = index + 1;
66 /* Check if there is a port who already claimed our slot */
67 if (legacy_serial_infos[index].np != 0) {
68 /* if we still have some room, move it, else override */
69 if (legacy_serial_count < MAX_LEGACY_SERIAL_PORTS) {
70 printk(KERN_INFO "Moved legacy port %d -> %d\n",
71 index, legacy_serial_count);
72 legacy_serial_ports[legacy_serial_count] =
73 legacy_serial_ports[index];
74 legacy_serial_infos[legacy_serial_count] =
75 legacy_serial_infos[index];
76 legacy_serial_count++;
78 printk(KERN_INFO "Replacing legacy port %d\n", index);
82 /* Now fill the entry */
83 memset(&legacy_serial_ports[index], 0,
84 sizeof(struct plat_serial8250_port));
85 if (iotype == UPIO_PORT)
86 legacy_serial_ports[index].iobase = base;
88 legacy_serial_ports[index].membase = (void __iomem *)base;
89 legacy_serial_ports[index].iotype = iotype;
90 legacy_serial_ports[index].uartclk = clock;
91 legacy_serial_ports[index].irq = irq;
92 legacy_serial_ports[index].flags = ASYNC_BOOT_AUTOCONF;
93 legacy_serial_infos[index].taddr = taddr;
94 legacy_serial_infos[index].np = of_node_get(np);
95 legacy_serial_infos[index].clock = clock;
96 legacy_serial_infos[index].speed = spd ? *spd : 0;
98 printk(KERN_INFO "Found legacy serial port %d for %s\n",
99 index, np->full_name);
100 printk(KERN_INFO " %s=%llx, taddr=%llx, irq=%lx, clk=%d, speed=%d\n",
101 (iotype == UPIO_PORT) ? "port" : "mem",
102 (unsigned long long)base, (unsigned long long)taddr, irq,
103 legacy_serial_ports[index].uartclk,
104 legacy_serial_infos[index].speed);
109 static int __init add_legacy_isa_port(struct device_node *np,
110 struct device_node *isa_bridge)
117 /* Get the ISA port number */
118 reg = (u32 *)get_property(np, "reg", NULL);
122 /* Verify it's an IO port, we don't support anything else */
123 if (!(reg[0] & 0x00000001))
126 /* Now look for an "ibm,aix-loc" property that gives us ordering
129 typep = (char *)get_property(np, "ibm,aix-loc", NULL);
131 /* If we have a location index, then use it */
132 if (typep && *typep == 'S')
133 index = simple_strtol(typep+1, NULL, 0) - 1;
135 /* Translate ISA address */
136 taddr = of_translate_address(np, reg);
138 /* Add port, irq will be dealt with later */
139 return add_legacy_port(np, index, UPIO_PORT, reg[1], taddr, NO_IRQ);
143 static int __init add_legacy_pci_port(struct device_node *np,
144 struct device_node *pci_dev)
146 phys_addr_t addr, base;
148 int iotype, index = -1;
151 /* We only support ports that have a clock frequency properly
152 * encoded in the device-tree (that is have an fcode). Anything
153 * else can't be used that early and will be normally probed by
154 * the generic 8250_pci driver later on.
156 if (get_property(np, "clock-frequency", NULL) == NULL)
160 /* Get the PCI address. Assume BAR 0 */
161 addrp = of_get_pci_address(pci_dev, 0, NULL);
165 /* We only support BAR 0 for now */
166 iotype = (addrp[0] & 0x02000000) ? UPIO_MEM : UPIO_PORT;
167 addr = of_translate_address(pci_dev, addrp);
169 /* Set the IO base to the same as the translated address for MMIO,
170 * or to the domain local IO base for PIO (it will be fixed up later)
172 if (iotype == UPIO_MEM)
177 /* Try to guess an index... If we have subdevices of the pci dev,
178 * we get to their "reg" property
181 u32 *reg = (u32 *)get_property(np, "reg", NULL);
182 if (reg && (*reg < 4))
183 index = legacy_serial_count + *reg;
186 /* Add port, irq will be dealt with later. We passed a translated
187 * IO port value. It will be fixed up later along with the irq
189 return add_legacy_port(np, index, iotype, base, addr, NO_IRQ);
193 * This is called very early, as part of setup_system() or eventually
194 * setup_arch(), basically before anything else in this file. This function
195 * will try to build a list of all the available 8250-compatible serial ports
196 * in the machine using the Open Firmware device-tree. It currently only deals
197 * with ISA and PCI busses but could be extended. It allows a very early boot
198 * console to be initialized, that list is also used later to provide 8250 with
199 * the machine non-PCI ports and to properly pick the default console port
201 void __init find_legacy_serial_ports(void)
203 struct device_node *np, *stdout;
207 DBG(" -> find_legacy_serial_port()\n");
209 /* Now find out if one of these is out firmware console */
210 path = (char *)get_property(of_chosen, "linux,stdout-path", NULL);
212 DBG(" no linux,stdout-path !\n");
215 stdout = of_find_node_by_path(path);
217 DBG("stdout is %s\n", stdout->full_name);
220 /* First fill our array with ISA ports */
221 for (np = NULL; (np = of_find_node_by_type(np, "serial"));) {
222 struct device_node *isa = of_get_parent(np);
223 if (isa && !strcmp(isa->name, "isa")) {
224 index = add_legacy_isa_port(np, isa);
225 if (index >= 0 && np == stdout)
226 legacy_serial_console = index;
231 /* Next, try to locate PCI ports */
232 for (np = NULL; (np = of_find_all_nodes(np));) {
233 struct device_node *pci, *parent = of_get_parent(np);
234 if (parent && !strcmp(parent->name, "isa")) {
238 if (strcmp(np->name, "serial") && strcmp(np->type, "serial")) {
242 /* Check for known pciclass, and also check wether we have
243 * a device with child nodes for ports or not
245 if (device_is_compatible(np, "pciclass,0700") ||
246 device_is_compatible(np, "pciclass,070002"))
248 else if (device_is_compatible(parent, "pciclass,0700") ||
249 device_is_compatible(parent, "pciclass,070002"))
255 index = add_legacy_pci_port(np, pci);
256 if (index >= 0 && np == stdout)
257 legacy_serial_console = index;
261 DBG("legacy_serial_console = %d\n", legacy_serial_console);
263 /* udbg is 64 bits only for now, that will change soon though ... */
265 while (legacy_serial_console >= 0) {
266 struct legacy_serial_info *info =
267 &legacy_serial_infos[legacy_serial_console];
270 if (info->taddr == 0)
272 addr = ioremap(info->taddr, 0x1000);
275 if (info->speed == 0)
276 info->speed = udbg_probe_uart_speed(addr, info->clock);
277 DBG("default console speed = %d\n", info->speed);
278 udbg_init_uart(addr, info->speed, info->clock);
281 #endif /* CONFIG_PPC64 */
283 DBG(" <- find_legacy_serial_port()\n");
286 static struct platform_device serial_device = {
287 .name = "serial8250",
288 .id = PLAT8250_DEV_PLATFORM,
290 .platform_data = legacy_serial_ports,
294 static void __init fixup_port_irq(int index,
295 struct device_node *np,
296 struct plat_serial8250_port *port)
298 DBG("fixup_port_irq(%d)\n", index);
300 /* Check for interrupts in that node */
301 if (np->n_intrs > 0) {
302 port->irq = np->intrs[0].line;
303 DBG(" port %d (%s), irq=%d\n",
304 index, np->full_name, port->irq);
308 /* Check for interrupts in the parent */
309 np = of_get_parent(np);
313 if (np->n_intrs > 0) {
314 port->irq = np->intrs[0].line;
315 DBG(" port %d (%s), irq=%d\n",
316 index, np->full_name, port->irq);
321 static void __init fixup_port_pio(int index,
322 struct device_node *np,
323 struct plat_serial8250_port *port)
325 struct pci_controller *hose;
327 DBG("fixup_port_pio(%d)\n", index);
329 hose = pci_find_hose_for_OF_device(np);
331 unsigned long offset = (unsigned long)hose->io_base_virt -
337 DBG("port %d, IO %lx -> %lx\n",
338 index, port->iobase, port->iobase + offset);
339 port->iobase += offset;
344 * This is called as an arch initcall, hopefully before the PCI bus is
345 * probed and/or the 8250 driver loaded since we need to register our
346 * platform devices before 8250 PCI ones are detected as some of them
347 * must properly "override" the platform ones.
349 * This function fixes up the interrupt value for platform ports as it
350 * couldn't be done earlier before interrupt maps have been parsed. It
351 * also "corrects" the IO address for PIO ports for the same reason,
352 * since earlier, the PHBs virtual IO space wasn't assigned yet. It then
353 * registers all those platform ports for use by the 8250 driver when it
356 static int __init serial_dev_init(void)
360 if (legacy_serial_count == 0)
364 * Before we register the platfrom serial devices, we need
365 * to fixup their interrutps and their IO ports.
367 DBG("Fixing serial ports interrupts and IO ports ...\n");
369 for (i = 0; i < legacy_serial_count; i++) {
370 struct plat_serial8250_port *port = &legacy_serial_ports[i];
371 struct device_node *np = legacy_serial_infos[i].np;
373 if (port->irq == NO_IRQ)
374 fixup_port_irq(i, np, port);
375 if (port->iotype == UPIO_PORT)
376 fixup_port_pio(i, np, port);
379 DBG("Registering platform serial ports\n");
381 return platform_device_register(&serial_device);
383 arch_initcall(serial_dev_init);
387 * This is called very early, as part of console_init() (typically just after
388 * time_init()). This function is respondible for trying to find a good
389 * default console on serial ports. It tries to match the open firmware
390 * default output with one of the available serial console drivers, either
391 * one of the platform serial ports that have been probed earlier by
392 * find_legacy_serial_ports() or some more platform specific ones.
394 static int __init check_legacy_serial_console(void)
396 struct device_node *prom_stdout = NULL;
397 int speed = 0, offset = 0;
401 DBG(" -> check_legacy_serial_console()\n");
403 /* The user has requested a console so this is already set up. */
404 if (strstr(saved_command_line, "console=")) {
405 DBG(" console was specified !\n");
410 DBG(" of_chosen is NULL !\n");
413 /* We are getting a weird phandle from OF ... */
414 /* ... So use the full path instead */
415 name = (char *)get_property(of_chosen, "linux,stdout-path", NULL);
417 DBG(" no linux,stdout-path !\n");
420 prom_stdout = of_find_node_by_path(name);
422 DBG(" can't find stdout package %s !\n", name);
425 DBG("stdout is %s\n", prom_stdout->full_name);
427 name = (char *)get_property(prom_stdout, "name", NULL);
429 DBG(" stdout package has no name !\n");
432 spd = (u32 *)get_property(prom_stdout, "current-speed", NULL);
438 #ifdef CONFIG_SERIAL_8250_CONSOLE
439 else if (strcmp(name, "serial") == 0) {
441 /* Look for it in probed array */
442 for (i = 0; i < legacy_serial_count; i++) {
443 if (prom_stdout != legacy_serial_infos[i].np)
446 speed = legacy_serial_infos[i].speed;
449 if (i >= legacy_serial_count)
452 #endif /* CONFIG_SERIAL_8250_CONSOLE */
453 #ifdef CONFIG_SERIAL_PMACZILOG_CONSOLE
454 else if (strcmp(name, "ch-a") == 0)
456 else if (strcmp(name, "ch-b") == 0)
458 #endif /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
461 of_node_put(prom_stdout);
463 DBG("Found serial console at ttyS%d\n", offset);
466 static char __initdata opt[16];
467 sprintf(opt, "%d", speed);
468 return add_preferred_console("ttyS", offset, opt);
470 return add_preferred_console("ttyS", offset, NULL);
473 DBG("No preferred console found !\n");
474 of_node_put(prom_stdout);
477 console_initcall(check_legacy_serial_console);