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