Linux Audio

Check our new training course

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