1 #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>
7 #include <linux/of_device.h>
8 #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 
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;
30 	unsigned int			speed;
31 	unsigned int			clock;
32 	int				irq_check_parent;
33 	phys_addr_t			taddr;
34 } legacy_serial_infos[MAX_LEGACY_SERIAL_PORTS];
35 
36 static struct __initdata of_device_id legacy_serial_parents[] = {
37 	{.type = "soc",},
38 	{.type = "tsi-bridge",},
39 	{.type = "opb", },
40 	{.compatible = "ibm,opb",},
41 	{.compatible = "simple-bus",},
42 	{.compatible = "wrs,epld-localbus",},
43 	{},
44 };
45 
46 static unsigned int legacy_serial_count;
47 static int legacy_serial_console = -1;
48 
49 static int __init add_legacy_port(struct device_node *np, int want_index,
50 				  int iotype, phys_addr_t base,
51 				  phys_addr_t taddr, unsigned long irq,
52 				  upf_t flags, int irq_check_parent)
53 {
54 	const u32 *clk, *spd;
55 	u32 clock = BASE_BAUD * 16;
56 	int index;
57 
58 	/* get clock freq. if present */
59 	clk = of_get_property(np, "clock-frequency", NULL);
60 	if (clk && *clk)
61 		clock = *clk;
62 
63 	/* get default speed if present */
64 	spd = of_get_property(np, "current-speed", NULL);
65 
66 	/* If we have a location index, then try to use it */
67 	if (want_index >= 0 && want_index < MAX_LEGACY_SERIAL_PORTS)
68 		index = want_index;
69 	else
70 		index = legacy_serial_count;
71 
72 	/* if our index is still out of range, that mean that
73 	 * array is full, we could scan for a free slot but that
74 	 * make little sense to bother, just skip the port
75 	 */
76 	if (index >= MAX_LEGACY_SERIAL_PORTS)
77 		return -1;
78 	if (index >= legacy_serial_count)
79 		legacy_serial_count = index + 1;
80 
81 	/* Check if there is a port who already claimed our slot */
82 	if (legacy_serial_infos[index].np != 0) {
83 		/* if we still have some room, move it, else override */
84 		if (legacy_serial_count < MAX_LEGACY_SERIAL_PORTS) {
85 			printk(KERN_DEBUG "Moved legacy port %d -> %d\n",
86 			       index, legacy_serial_count);
87 			legacy_serial_ports[legacy_serial_count] =
88 				legacy_serial_ports[index];
89 			legacy_serial_infos[legacy_serial_count] =
90 				legacy_serial_infos[index];
91 			legacy_serial_count++;
92 		} else {
93 			printk(KERN_DEBUG "Replacing legacy port %d\n", index);
94 		}
95 	}
96 
97 	/* Now fill the entry */
98 	memset(&legacy_serial_ports[index], 0,
99 	       sizeof(struct plat_serial8250_port));
100 	if (iotype == UPIO_PORT)
101 		legacy_serial_ports[index].iobase = base;
102 	else
103 		legacy_serial_ports[index].mapbase = base;
104 	legacy_serial_ports[index].iotype = iotype;
105 	legacy_serial_ports[index].uartclk = clock;
106 	legacy_serial_ports[index].irq = irq;
107 	legacy_serial_ports[index].flags = flags;
108 	legacy_serial_infos[index].taddr = taddr;
109 	legacy_serial_infos[index].np = of_node_get(np);
110 	legacy_serial_infos[index].clock = clock;
111 	legacy_serial_infos[index].speed = spd ? *spd : 0;
112 	legacy_serial_infos[index].irq_check_parent = irq_check_parent;
113 
114 	printk(KERN_DEBUG "Found legacy serial port %d for %s\n",
115 	       index, np->full_name);
116 	printk(KERN_DEBUG "  %s=%llx, taddr=%llx, irq=%lx, clk=%d, speed=%d\n",
117 	       (iotype == UPIO_PORT) ? "port" : "mem",
118 	       (unsigned long long)base, (unsigned long long)taddr, irq,
119 	       legacy_serial_ports[index].uartclk,
120 	       legacy_serial_infos[index].speed);
121 
122 	return index;
123 }
124 
125 static int __init add_legacy_soc_port(struct device_node *np,
126 				      struct device_node *soc_dev)
127 {
128 	u64 addr;
129 	const u32 *addrp;
130 	upf_t flags = UPF_BOOT_AUTOCONF | UPF_SKIP_TEST | UPF_SHARE_IRQ
131 		| UPF_FIXED_PORT;
132 	struct device_node *tsi = of_get_parent(np);
133 
134 	/* We only support ports that have a clock frequency properly
135 	 * encoded in the device-tree.
136 	 */
137 	if (of_get_property(np, "clock-frequency", NULL) == NULL)
138 		return -1;
139 
140 	/* if reg-shift or offset, don't try to use it */
141 	if ((of_get_property(np, "reg-shift", NULL) != NULL) ||
142 		(of_get_property(np, "reg-offset", NULL) != NULL))
143 		return -1;
144 
145 	/* if rtas uses this device, don't try to use it as well */
146 	if (of_get_property(np, "used-by-rtas", NULL) != NULL)
147 		return -1;
148 
149 	/* Get the address */
150 	addrp = of_get_address(soc_dev, 0, NULL, NULL);
151 	if (addrp == NULL)
152 		return -1;
153 
154 	addr = of_translate_address(soc_dev, addrp);
155 	if (addr == OF_BAD_ADDR)
156 		return -1;
157 
158 	/* Add port, irq will be dealt with later. We passed a translated
159 	 * IO port value. It will be fixed up later along with the irq
160 	 */
161 	if (tsi && !strcmp(tsi->type, "tsi-bridge"))
162 		return add_legacy_port(np, -1, UPIO_TSI, addr, addr, NO_IRQ, flags, 0);
163 	else
164 		return add_legacy_port(np, -1, UPIO_MEM, addr, addr, NO_IRQ, flags, 0);
165 }
166 
167 static int __init add_legacy_isa_port(struct device_node *np,
168 				      struct device_node *isa_brg)
169 {
170 	const u32 *reg;
171 	const char *typep;
172 	int index = -1;
173 	u64 taddr;
174 
175 	DBG(" -> add_legacy_isa_port(%s)\n", np->full_name);
176 
177 	/* Get the ISA port number */
178 	reg = of_get_property(np, "reg", NULL);
179 	if (reg == NULL)
180 		return -1;
181 
182 	/* Verify it's an IO port, we don't support anything else */
183 	if (!(reg[0] & 0x00000001))
184 		return -1;
185 
186 	/* Now look for an "ibm,aix-loc" property that gives us ordering
187 	 * if any...
188 	 */
189 	typep = of_get_property(np, "ibm,aix-loc", NULL);
190 
191 	/* If we have a location index, then use it */
192 	if (typep && *typep == 'S')
193 		index = simple_strtol(typep+1, NULL, 0) - 1;
194 
195 	/* Translate ISA address. If it fails, we still register the port
196 	 * with no translated address so that it can be picked up as an IO
197 	 * port later by the serial driver
198 	 */
199 	taddr = of_translate_address(np, reg);
200 	if (taddr == OF_BAD_ADDR)
201 		taddr = 0;
202 
203 	/* Add port, irq will be dealt with later */
204 	return add_legacy_port(np, index, UPIO_PORT, reg[1], taddr,
205 			       NO_IRQ, UPF_BOOT_AUTOCONF, 0);
206 
207 }
208 
209 #ifdef CONFIG_PCI
210 static int __init add_legacy_pci_port(struct device_node *np,
211 				      struct device_node *pci_dev)
212 {
213 	u64 addr, base;
214 	const u32 *addrp;
215 	unsigned int flags;
216 	int iotype, index = -1, lindex = 0;
217 
218 	DBG(" -> add_legacy_pci_port(%s)\n", np->full_name);
219 
220 	/* We only support ports that have a clock frequency properly
221 	 * encoded in the device-tree (that is have an fcode). Anything
222 	 * else can't be used that early and will be normally probed by
223 	 * the generic 8250_pci driver later on. The reason is that 8250
224 	 * compatible UARTs on PCI need all sort of quirks (port offsets
225 	 * etc...) that this code doesn't know about
226 	 */
227 	if (of_get_property(np, "clock-frequency", NULL) == NULL)
228 		return -1;
229 
230 	/* Get the PCI address. Assume BAR 0 */
231 	addrp = of_get_pci_address(pci_dev, 0, NULL, &flags);
232 	if (addrp == NULL)
233 		return -1;
234 
235 	/* We only support BAR 0 for now */
236 	iotype = (flags & IORESOURCE_MEM) ? UPIO_MEM : UPIO_PORT;
237 	addr = of_translate_address(pci_dev, addrp);
238 	if (addr == OF_BAD_ADDR)
239 		return -1;
240 
241 	/* Set the IO base to the same as the translated address for MMIO,
242 	 * or to the domain local IO base for PIO (it will be fixed up later)
243 	 */
244 	if (iotype == UPIO_MEM)
245 		base = addr;
246 	else
247 		base = addrp[2];
248 
249 	/* Try to guess an index... If we have subdevices of the pci dev,
250 	 * we get to their "reg" property
251 	 */
252 	if (np != pci_dev) {
253 		const u32 *reg = of_get_property(np, "reg", NULL);
254 		if (reg && (*reg < 4))
255 			index = lindex = *reg;
256 	}
257 
258 	/* Local index means it's the Nth port in the PCI chip. Unfortunately
259 	 * the offset to add here is device specific. We know about those
260 	 * EXAR ports and we default to the most common case. If your UART
261 	 * doesn't work for these settings, you'll have to add your own special
262 	 * cases here
263 	 */
264 	if (of_device_is_compatible(pci_dev, "pci13a8,152") ||
265 	    of_device_is_compatible(pci_dev, "pci13a8,154") ||
266 	    of_device_is_compatible(pci_dev, "pci13a8,158")) {
267 		addr += 0x200 * lindex;
268 		base += 0x200 * lindex;
269 	} else {
270 		addr += 8 * lindex;
271 		base += 8 * lindex;
272 	}
273 
274 	/* Add port, irq will be dealt with later. We passed a translated
275 	 * IO port value. It will be fixed up later along with the irq
276 	 */
277 	return add_legacy_port(np, index, iotype, base, addr, NO_IRQ,
278 			       UPF_BOOT_AUTOCONF, np != pci_dev);
279 }
280 #endif
281 
282 static void __init setup_legacy_serial_console(int console)
283 {
284 	struct legacy_serial_info *info =
285 		&legacy_serial_infos[console];
286 	void __iomem *addr;
287 
288 	if (info->taddr == 0)
289 		return;
290 	addr = ioremap(info->taddr, 0x1000);
291 	if (addr == NULL)
292 		return;
293 	if (info->speed == 0)
294 		info->speed = udbg_probe_uart_speed(addr, info->clock);
295 	DBG("default console speed = %d\n", info->speed);
296 	udbg_init_uart(addr, info->speed, info->clock);
297 }
298 
299 /*
300  * This is called very early, as part of setup_system() or eventually
301  * setup_arch(), basically before anything else in this file. This function
302  * will try to build a list of all the available 8250-compatible serial ports
303  * in the machine using the Open Firmware device-tree. It currently only deals
304  * with ISA and PCI busses but could be extended. It allows a very early boot
305  * console to be initialized, that list is also used later to provide 8250 with
306  * the machine non-PCI ports and to properly pick the default console port
307  */
308 void __init find_legacy_serial_ports(void)
309 {
310 	struct device_node *np, *stdout = NULL;
311 	const char *path;
312 	int index;
313 
314 	DBG(" -> find_legacy_serial_port()\n");
315 
316 	/* Now find out if one of these is out firmware console */
317 	path = of_get_property(of_chosen, "linux,stdout-path", NULL);
318 	if (path != NULL) {
319 		stdout = of_find_node_by_path(path);
320 		if (stdout)
321 			DBG("stdout is %s\n", stdout->full_name);
322 	} else {
323 		DBG(" no linux,stdout-path !\n");
324 	}
325 
326 	/* Iterate over all the 16550 ports, looking for known parents */
327 	for_each_compatible_node(np, "serial", "ns16550") {
328 		struct device_node *parent = of_get_parent(np);
329 		if (!parent)
330 			continue;
331 		if (of_match_node(legacy_serial_parents, parent) != NULL) {
332 			index = add_legacy_soc_port(np, np);
333 			if (index >= 0 && np == stdout)
334 				legacy_serial_console = index;
335 		}
336 		of_node_put(parent);
337 	}
338 
339 	/* Next, fill our array with ISA ports */
340 	for_each_node_by_type(np, "serial") {
341 		struct device_node *isa = of_get_parent(np);
342 		if (isa && !strcmp(isa->name, "isa")) {
343 			index = add_legacy_isa_port(np, isa);
344 			if (index >= 0 && np == stdout)
345 				legacy_serial_console = index;
346 		}
347 		of_node_put(isa);
348 	}
349 
350 #ifdef CONFIG_PCI
351 	/* Next, try to locate PCI ports */
352 	for (np = NULL; (np = of_find_all_nodes(np));) {
353 		struct device_node *pci, *parent = of_get_parent(np);
354 		if (parent && !strcmp(parent->name, "isa")) {
355 			of_node_put(parent);
356 			continue;
357 		}
358 		if (strcmp(np->name, "serial") && strcmp(np->type, "serial")) {
359 			of_node_put(parent);
360 			continue;
361 		}
362 		/* Check for known pciclass, and also check wether we have
363 		 * a device with child nodes for ports or not
364 		 */
365 		if (of_device_is_compatible(np, "pciclass,0700") ||
366 		    of_device_is_compatible(np, "pciclass,070002"))
367 			pci = np;
368 		else if (of_device_is_compatible(parent, "pciclass,0700") ||
369 			 of_device_is_compatible(parent, "pciclass,070002"))
370 			pci = parent;
371 		else {
372 			of_node_put(parent);
373 			continue;
374 		}
375 		index = add_legacy_pci_port(np, pci);
376 		if (index >= 0 && np == stdout)
377 			legacy_serial_console = index;
378 		of_node_put(parent);
379 	}
380 #endif
381 
382 	DBG("legacy_serial_console = %d\n", legacy_serial_console);
383 	if (legacy_serial_console >= 0)
384 		setup_legacy_serial_console(legacy_serial_console);
385 	DBG(" <- find_legacy_serial_port()\n");
386 }
387 
388 static struct platform_device serial_device = {
389 	.name	= "serial8250",
390 	.id	= PLAT8250_DEV_PLATFORM,
391 	.dev	= {
392 		.platform_data = legacy_serial_ports,
393 	},
394 };
395 
396 static void __init fixup_port_irq(int index,
397 				  struct device_node *np,
398 				  struct plat_serial8250_port *port)
399 {
400 	unsigned int virq;
401 
402 	DBG("fixup_port_irq(%d)\n", index);
403 
404 	virq = irq_of_parse_and_map(np, 0);
405 	if (virq == NO_IRQ && legacy_serial_infos[index].irq_check_parent) {
406 		np = of_get_parent(np);
407 		if (np == NULL)
408 			return;
409 		virq = irq_of_parse_and_map(np, 0);
410 		of_node_put(np);
411 	}
412 	if (virq == NO_IRQ)
413 		return;
414 
415 	port->irq = virq;
416 }
417 
418 static void __init fixup_port_pio(int index,
419 				  struct device_node *np,
420 				  struct plat_serial8250_port *port)
421 {
422 #ifdef CONFIG_PCI
423 	struct pci_controller *hose;
424 
425 	DBG("fixup_port_pio(%d)\n", index);
426 
427 	hose = pci_find_hose_for_OF_device(np);
428 	if (hose) {
429 		unsigned long offset = (unsigned long)hose->io_base_virt -
430 #ifdef CONFIG_PPC64
431 			pci_io_base;
432 #else
433 			isa_io_base;
434 #endif
435 		DBG("port %d, IO %lx -> %lx\n",
436 		    index, port->iobase, port->iobase + offset);
437 		port->iobase += offset;
438 	}
439 #endif
440 }
441 
442 static void __init fixup_port_mmio(int index,
443 				   struct device_node *np,
444 				   struct plat_serial8250_port *port)
445 {
446 	DBG("fixup_port_mmio(%d)\n", index);
447 
448 	port->membase = ioremap(port->mapbase, 0x100);
449 }
450 
451 /*
452  * This is called as an arch initcall, hopefully before the PCI bus is
453  * probed and/or the 8250 driver loaded since we need to register our
454  * platform devices before 8250 PCI ones are detected as some of them
455  * must properly "override" the platform ones.
456  *
457  * This function fixes up the interrupt value for platform ports as it
458  * couldn't be done earlier before interrupt maps have been parsed. It
459  * also "corrects" the IO address for PIO ports for the same reason,
460  * since earlier, the PHBs virtual IO space wasn't assigned yet. It then
461  * registers all those platform ports for use by the 8250 driver when it
462  * finally loads.
463  */
464 static int __init serial_dev_init(void)
465 {
466 	int i;
467 
468 	if (legacy_serial_count == 0)
469 		return -ENODEV;
470 
471 	/*
472 	 * Before we register the platfrom serial devices, we need
473 	 * to fixup their interrupts and their IO ports.
474 	 */
475 	DBG("Fixing serial ports interrupts and IO ports ...\n");
476 
477 	for (i = 0; i < legacy_serial_count; i++) {
478 		struct plat_serial8250_port *port = &legacy_serial_ports[i];
479 		struct device_node *np = legacy_serial_infos[i].np;
480 
481 		if (port->irq == NO_IRQ)
482 			fixup_port_irq(i, np, port);
483 		if (port->iotype == UPIO_PORT)
484 			fixup_port_pio(i, np, port);
485 		if ((port->iotype == UPIO_MEM) || (port->iotype == UPIO_TSI))
486 			fixup_port_mmio(i, np, port);
487 	}
488 
489 	DBG("Registering platform serial ports\n");
490 
491 	return platform_device_register(&serial_device);
492 }
493 device_initcall(serial_dev_init);
494 
495 
496 #ifdef CONFIG_SERIAL_8250_CONSOLE
497 /*
498  * This is called very early, as part of console_init() (typically just after
499  * time_init()). This function is respondible for trying to find a good
500  * default console on serial ports. It tries to match the open firmware
501  * default output with one of the available serial console drivers that have
502  * been probed earlier by find_legacy_serial_ports()
503  */
504 static int __init check_legacy_serial_console(void)
505 {
506 	struct device_node *prom_stdout = NULL;
507 	int i, speed = 0, offset = 0;
508 	const char *name;
509 	const u32 *spd;
510 
511 	DBG(" -> check_legacy_serial_console()\n");
512 
513 	/* The user has requested a console so this is already set up. */
514 	if (strstr(boot_command_line, "console=")) {
515 		DBG(" console was specified !\n");
516 		return -EBUSY;
517 	}
518 
519 	if (!of_chosen) {
520 		DBG(" of_chosen is NULL !\n");
521 		return -ENODEV;
522 	}
523 
524 	if (legacy_serial_console < 0) {
525 		DBG(" legacy_serial_console not found !\n");
526 		return -ENODEV;
527 	}
528 	/* We are getting a weird phandle from OF ... */
529 	/* ... So use the full path instead */
530 	name = of_get_property(of_chosen, "linux,stdout-path", NULL);
531 	if (name == NULL) {
532 		DBG(" no linux,stdout-path !\n");
533 		return -ENODEV;
534 	}
535 	prom_stdout = of_find_node_by_path(name);
536 	if (!prom_stdout) {
537 		DBG(" can't find stdout package %s !\n", name);
538 		return -ENODEV;
539 	}
540 	DBG("stdout is %s\n", prom_stdout->full_name);
541 
542 	name = of_get_property(prom_stdout, "name", NULL);
543 	if (!name) {
544 		DBG(" stdout package has no name !\n");
545 		goto not_found;
546 	}
547 	spd = of_get_property(prom_stdout, "current-speed", NULL);
548 	if (spd)
549 		speed = *spd;
550 
551 	if (strcmp(name, "serial") != 0)
552 		goto not_found;
553 
554 	/* Look for it in probed array */
555 	for (i = 0; i < legacy_serial_count; i++) {
556 		if (prom_stdout != legacy_serial_infos[i].np)
557 			continue;
558 		offset = i;
559 		speed = legacy_serial_infos[i].speed;
560 		break;
561 	}
562 	if (i >= legacy_serial_count)
563 		goto not_found;
564 
565 	of_node_put(prom_stdout);
566 
567 	DBG("Found serial console at ttyS%d\n", offset);
568 
569 	if (speed) {
570 		static char __initdata opt[16];
571 		sprintf(opt, "%d", speed);
572 		return add_preferred_console("ttyS", offset, opt);
573 	} else
574 		return add_preferred_console("ttyS", offset, NULL);
575 
576  not_found:
577 	DBG("No preferred console found !\n");
578 	of_node_put(prom_stdout);
579 	return -ENODEV;
580 }
581 console_initcall(check_legacy_serial_console);
582 
583 #endif /* CONFIG_SERIAL_8250_CONSOLE */
584