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