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