Linux Audio

Check our new training course

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