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