Commit | Line | Data |
---|---|---|
463ce0e1 BH |
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, | |
b580d46c KG |
39 | phys_addr_t taddr, unsigned long irq, |
40 | unsigned int flags) | |
463ce0e1 | 41 | { |
8dacaedf | 42 | u32 *clk, *spd, clock = BASE_BAUD * 16; |
463ce0e1 BH |
43 | int index; |
44 | ||
45 | /* get clock freq. if present */ | |
46 | clk = (u32 *)get_property(np, "clock-frequency", NULL); | |
31df1678 DW |
47 | if (clk && *clk) |
48 | clock = *clk; | |
463ce0e1 BH |
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 | |
8dacaedf | 90 | legacy_serial_ports[index].mapbase = base; |
463ce0e1 BH |
91 | legacy_serial_ports[index].iotype = iotype; |
92 | legacy_serial_ports[index].uartclk = clock; | |
93 | legacy_serial_ports[index].irq = irq; | |
b580d46c | 94 | legacy_serial_ports[index].flags = flags; |
463ce0e1 BH |
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 | ||
b580d46c KG |
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 | unsigned int 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 | ||
463ce0e1 BH |
137 | static int __init add_legacy_isa_port(struct device_node *np, |
138 | struct device_node *isa_bridge) | |
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 */ | |
b580d46c | 167 | return add_legacy_port(np, index, UPIO_PORT, reg[1], taddr, NO_IRQ, UPF_BOOT_AUTOCONF); |
463ce0e1 BH |
168 | |
169 | } | |
170 | ||
171 | static int __init add_legacy_pci_port(struct device_node *np, | |
172 | struct device_node *pci_dev) | |
173 | { | |
174 | phys_addr_t addr, base; | |
175 | u32 *addrp; | |
d2dd482b | 176 | unsigned int flags; |
8dacaedf | 177 | int iotype, index = -1, lindex = 0; |
463ce0e1 | 178 | |
463ce0e1 BH |
179 | /* We only support ports that have a clock frequency properly |
180 | * encoded in the device-tree (that is have an fcode). Anything | |
181 | * else can't be used that early and will be normally probed by | |
8dacaedf BH |
182 | * the generic 8250_pci driver later on. The reason is that 8250 |
183 | * compatible UARTs on PCI need all sort of quirks (port offsets | |
184 | * etc...) that this code doesn't know about | |
463ce0e1 BH |
185 | */ |
186 | if (get_property(np, "clock-frequency", NULL) == NULL) | |
187 | return -1; | |
463ce0e1 BH |
188 | |
189 | /* Get the PCI address. Assume BAR 0 */ | |
d2dd482b | 190 | addrp = of_get_pci_address(pci_dev, 0, NULL, &flags); |
463ce0e1 BH |
191 | if (addrp == NULL) |
192 | return -1; | |
193 | ||
194 | /* We only support BAR 0 for now */ | |
d2dd482b | 195 | iotype = (flags & IORESOURCE_MEM) ? UPIO_MEM : UPIO_PORT; |
463ce0e1 BH |
196 | addr = of_translate_address(pci_dev, addrp); |
197 | ||
198 | /* Set the IO base to the same as the translated address for MMIO, | |
199 | * or to the domain local IO base for PIO (it will be fixed up later) | |
200 | */ | |
201 | if (iotype == UPIO_MEM) | |
202 | base = addr; | |
203 | else | |
204 | base = addrp[2]; | |
205 | ||
206 | /* Try to guess an index... If we have subdevices of the pci dev, | |
207 | * we get to their "reg" property | |
208 | */ | |
209 | if (np != pci_dev) { | |
210 | u32 *reg = (u32 *)get_property(np, "reg", NULL); | |
211 | if (reg && (*reg < 4)) | |
8dacaedf BH |
212 | index = lindex = *reg; |
213 | } | |
214 | ||
215 | /* Local index means it's the Nth port in the PCI chip. Unfortunately | |
216 | * the offset to add here is device specific. We know about those | |
217 | * EXAR ports and we default to the most common case. If your UART | |
218 | * doesn't work for these settings, you'll have to add your own special | |
219 | * cases here | |
220 | */ | |
221 | if (device_is_compatible(pci_dev, "pci13a8,152") || | |
222 | device_is_compatible(pci_dev, "pci13a8,154") || | |
223 | device_is_compatible(pci_dev, "pci13a8,158")) { | |
224 | addr += 0x200 * lindex; | |
225 | base += 0x200 * lindex; | |
226 | } else { | |
227 | addr += 8 * lindex; | |
228 | base += 8 * lindex; | |
463ce0e1 BH |
229 | } |
230 | ||
231 | /* Add port, irq will be dealt with later. We passed a translated | |
232 | * IO port value. It will be fixed up later along with the irq | |
233 | */ | |
b580d46c | 234 | return add_legacy_port(np, index, iotype, base, addr, NO_IRQ, UPF_BOOT_AUTOCONF); |
463ce0e1 BH |
235 | } |
236 | ||
237 | /* | |
238 | * This is called very early, as part of setup_system() or eventually | |
239 | * setup_arch(), basically before anything else in this file. This function | |
240 | * will try to build a list of all the available 8250-compatible serial ports | |
241 | * in the machine using the Open Firmware device-tree. It currently only deals | |
242 | * with ISA and PCI busses but could be extended. It allows a very early boot | |
243 | * console to be initialized, that list is also used later to provide 8250 with | |
244 | * the machine non-PCI ports and to properly pick the default console port | |
245 | */ | |
246 | void __init find_legacy_serial_ports(void) | |
247 | { | |
b580d46c | 248 | struct device_node *np, *stdout = NULL; |
463ce0e1 BH |
249 | char *path; |
250 | int index; | |
251 | ||
252 | DBG(" -> find_legacy_serial_port()\n"); | |
253 | ||
254 | /* Now find out if one of these is out firmware console */ | |
255 | path = (char *)get_property(of_chosen, "linux,stdout-path", NULL); | |
b580d46c KG |
256 | if (path != NULL) { |
257 | stdout = of_find_node_by_path(path); | |
258 | if (stdout) | |
259 | DBG("stdout is %s\n", stdout->full_name); | |
260 | } else { | |
463ce0e1 | 261 | DBG(" no linux,stdout-path !\n"); |
463ce0e1 | 262 | } |
b580d46c KG |
263 | |
264 | /* First fill our array with SOC ports */ | |
265 | for (np = NULL; (np = of_find_compatible_node(np, "serial", "ns16550")) != NULL;) { | |
266 | struct device_node *soc = of_get_parent(np); | |
267 | if (soc && !strcmp(soc->type, "soc")) { | |
268 | index = add_legacy_soc_port(np, np); | |
269 | if (index >= 0 && np == stdout) | |
270 | legacy_serial_console = index; | |
271 | } | |
272 | of_node_put(soc); | |
463ce0e1 BH |
273 | } |
274 | ||
275 | /* First fill our array with ISA ports */ | |
276 | for (np = NULL; (np = of_find_node_by_type(np, "serial"));) { | |
277 | struct device_node *isa = of_get_parent(np); | |
278 | if (isa && !strcmp(isa->name, "isa")) { | |
279 | index = add_legacy_isa_port(np, isa); | |
280 | if (index >= 0 && np == stdout) | |
281 | legacy_serial_console = index; | |
282 | } | |
283 | of_node_put(isa); | |
284 | } | |
285 | ||
286 | /* Next, try to locate PCI ports */ | |
287 | for (np = NULL; (np = of_find_all_nodes(np));) { | |
288 | struct device_node *pci, *parent = of_get_parent(np); | |
289 | if (parent && !strcmp(parent->name, "isa")) { | |
290 | of_node_put(parent); | |
291 | continue; | |
292 | } | |
293 | if (strcmp(np->name, "serial") && strcmp(np->type, "serial")) { | |
294 | of_node_put(parent); | |
295 | continue; | |
296 | } | |
297 | /* Check for known pciclass, and also check wether we have | |
298 | * a device with child nodes for ports or not | |
299 | */ | |
300 | if (device_is_compatible(np, "pciclass,0700") || | |
301 | device_is_compatible(np, "pciclass,070002")) | |
302 | pci = np; | |
303 | else if (device_is_compatible(parent, "pciclass,0700") || | |
304 | device_is_compatible(parent, "pciclass,070002")) | |
305 | pci = parent; | |
306 | else { | |
307 | of_node_put(parent); | |
308 | continue; | |
309 | } | |
310 | index = add_legacy_pci_port(np, pci); | |
311 | if (index >= 0 && np == stdout) | |
312 | legacy_serial_console = index; | |
313 | of_node_put(parent); | |
314 | } | |
315 | ||
316 | DBG("legacy_serial_console = %d\n", legacy_serial_console); | |
317 | ||
318 | /* udbg is 64 bits only for now, that will change soon though ... */ | |
463ce0e1 BH |
319 | while (legacy_serial_console >= 0) { |
320 | struct legacy_serial_info *info = | |
321 | &legacy_serial_infos[legacy_serial_console]; | |
322 | void __iomem *addr; | |
323 | ||
324 | if (info->taddr == 0) | |
325 | break; | |
326 | addr = ioremap(info->taddr, 0x1000); | |
327 | if (addr == NULL) | |
328 | break; | |
329 | if (info->speed == 0) | |
330 | info->speed = udbg_probe_uart_speed(addr, info->clock); | |
331 | DBG("default console speed = %d\n", info->speed); | |
332 | udbg_init_uart(addr, info->speed, info->clock); | |
333 | break; | |
334 | } | |
463ce0e1 BH |
335 | |
336 | DBG(" <- find_legacy_serial_port()\n"); | |
337 | } | |
338 | ||
339 | static struct platform_device serial_device = { | |
340 | .name = "serial8250", | |
341 | .id = PLAT8250_DEV_PLATFORM, | |
342 | .dev = { | |
343 | .platform_data = legacy_serial_ports, | |
344 | }, | |
345 | }; | |
346 | ||
347 | static void __init fixup_port_irq(int index, | |
348 | struct device_node *np, | |
349 | struct plat_serial8250_port *port) | |
350 | { | |
351 | DBG("fixup_port_irq(%d)\n", index); | |
352 | ||
353 | /* Check for interrupts in that node */ | |
354 | if (np->n_intrs > 0) { | |
355 | port->irq = np->intrs[0].line; | |
356 | DBG(" port %d (%s), irq=%d\n", | |
357 | index, np->full_name, port->irq); | |
358 | return; | |
359 | } | |
360 | ||
361 | /* Check for interrupts in the parent */ | |
362 | np = of_get_parent(np); | |
363 | if (np == NULL) | |
364 | return; | |
365 | ||
366 | if (np->n_intrs > 0) { | |
367 | port->irq = np->intrs[0].line; | |
368 | DBG(" port %d (%s), irq=%d\n", | |
369 | index, np->full_name, port->irq); | |
370 | } | |
371 | of_node_put(np); | |
372 | } | |
373 | ||
374 | static void __init fixup_port_pio(int index, | |
375 | struct device_node *np, | |
376 | struct plat_serial8250_port *port) | |
377 | { | |
378 | struct pci_controller *hose; | |
379 | ||
380 | DBG("fixup_port_pio(%d)\n", index); | |
381 | ||
382 | hose = pci_find_hose_for_OF_device(np); | |
383 | if (hose) { | |
384 | unsigned long offset = (unsigned long)hose->io_base_virt - | |
385 | #ifdef CONFIG_PPC64 | |
386 | pci_io_base; | |
387 | #else | |
388 | isa_io_base; | |
389 | #endif | |
390 | DBG("port %d, IO %lx -> %lx\n", | |
391 | index, port->iobase, port->iobase + offset); | |
392 | port->iobase += offset; | |
393 | } | |
394 | } | |
395 | ||
8dacaedf BH |
396 | static void __init fixup_port_mmio(int index, |
397 | struct device_node *np, | |
398 | struct plat_serial8250_port *port) | |
399 | { | |
400 | DBG("fixup_port_mmio(%d)\n", index); | |
401 | ||
402 | port->membase = ioremap(port->mapbase, 0x100); | |
403 | } | |
404 | ||
463ce0e1 BH |
405 | /* |
406 | * This is called as an arch initcall, hopefully before the PCI bus is | |
407 | * probed and/or the 8250 driver loaded since we need to register our | |
408 | * platform devices before 8250 PCI ones are detected as some of them | |
409 | * must properly "override" the platform ones. | |
410 | * | |
411 | * This function fixes up the interrupt value for platform ports as it | |
412 | * couldn't be done earlier before interrupt maps have been parsed. It | |
413 | * also "corrects" the IO address for PIO ports for the same reason, | |
414 | * since earlier, the PHBs virtual IO space wasn't assigned yet. It then | |
415 | * registers all those platform ports for use by the 8250 driver when it | |
416 | * finally loads. | |
417 | */ | |
418 | static int __init serial_dev_init(void) | |
419 | { | |
420 | int i; | |
421 | ||
422 | if (legacy_serial_count == 0) | |
423 | return -ENODEV; | |
424 | ||
425 | /* | |
426 | * Before we register the platfrom serial devices, we need | |
427 | * to fixup their interrutps and their IO ports. | |
428 | */ | |
429 | DBG("Fixing serial ports interrupts and IO ports ...\n"); | |
430 | ||
431 | for (i = 0; i < legacy_serial_count; i++) { | |
432 | struct plat_serial8250_port *port = &legacy_serial_ports[i]; | |
433 | struct device_node *np = legacy_serial_infos[i].np; | |
434 | ||
435 | if (port->irq == NO_IRQ) | |
436 | fixup_port_irq(i, np, port); | |
437 | if (port->iotype == UPIO_PORT) | |
438 | fixup_port_pio(i, np, port); | |
8dacaedf BH |
439 | if (port->iotype == UPIO_MEM) |
440 | fixup_port_mmio(i, np, port); | |
463ce0e1 BH |
441 | } |
442 | ||
443 | DBG("Registering platform serial ports\n"); | |
444 | ||
445 | return platform_device_register(&serial_device); | |
446 | } | |
447 | arch_initcall(serial_dev_init); | |
448 | ||
449 | ||
450 | /* | |
451 | * This is called very early, as part of console_init() (typically just after | |
452 | * time_init()). This function is respondible for trying to find a good | |
453 | * default console on serial ports. It tries to match the open firmware | |
454 | * default output with one of the available serial console drivers, either | |
455 | * one of the platform serial ports that have been probed earlier by | |
456 | * find_legacy_serial_ports() or some more platform specific ones. | |
457 | */ | |
458 | static int __init check_legacy_serial_console(void) | |
459 | { | |
460 | struct device_node *prom_stdout = NULL; | |
461 | int speed = 0, offset = 0; | |
462 | char *name; | |
463 | u32 *spd; | |
464 | ||
465 | DBG(" -> check_legacy_serial_console()\n"); | |
466 | ||
467 | /* The user has requested a console so this is already set up. */ | |
468 | if (strstr(saved_command_line, "console=")) { | |
469 | DBG(" console was specified !\n"); | |
470 | return -EBUSY; | |
471 | } | |
472 | ||
473 | if (!of_chosen) { | |
474 | DBG(" of_chosen is NULL !\n"); | |
475 | return -ENODEV; | |
476 | } | |
b580d46c KG |
477 | |
478 | if (legacy_serial_console < 0) { | |
479 | DBG(" legacy_serial_console not found !\n"); | |
480 | return -ENODEV; | |
481 | } | |
463ce0e1 BH |
482 | /* We are getting a weird phandle from OF ... */ |
483 | /* ... So use the full path instead */ | |
484 | name = (char *)get_property(of_chosen, "linux,stdout-path", NULL); | |
485 | if (name == NULL) { | |
486 | DBG(" no linux,stdout-path !\n"); | |
487 | return -ENODEV; | |
488 | } | |
489 | prom_stdout = of_find_node_by_path(name); | |
490 | if (!prom_stdout) { | |
491 | DBG(" can't find stdout package %s !\n", name); | |
492 | return -ENODEV; | |
493 | } | |
494 | DBG("stdout is %s\n", prom_stdout->full_name); | |
495 | ||
496 | name = (char *)get_property(prom_stdout, "name", NULL); | |
497 | if (!name) { | |
498 | DBG(" stdout package has no name !\n"); | |
499 | goto not_found; | |
500 | } | |
501 | spd = (u32 *)get_property(prom_stdout, "current-speed", NULL); | |
502 | if (spd) | |
503 | speed = *spd; | |
504 | ||
505 | if (0) | |
506 | ; | |
507 | #ifdef CONFIG_SERIAL_8250_CONSOLE | |
508 | else if (strcmp(name, "serial") == 0) { | |
509 | int i; | |
510 | /* Look for it in probed array */ | |
511 | for (i = 0; i < legacy_serial_count; i++) { | |
512 | if (prom_stdout != legacy_serial_infos[i].np) | |
513 | continue; | |
514 | offset = i; | |
515 | speed = legacy_serial_infos[i].speed; | |
516 | break; | |
517 | } | |
518 | if (i >= legacy_serial_count) | |
519 | goto not_found; | |
520 | } | |
521 | #endif /* CONFIG_SERIAL_8250_CONSOLE */ | |
522 | #ifdef CONFIG_SERIAL_PMACZILOG_CONSOLE | |
523 | else if (strcmp(name, "ch-a") == 0) | |
524 | offset = 0; | |
525 | else if (strcmp(name, "ch-b") == 0) | |
526 | offset = 1; | |
527 | #endif /* CONFIG_SERIAL_PMACZILOG_CONSOLE */ | |
528 | else | |
529 | goto not_found; | |
530 | of_node_put(prom_stdout); | |
531 | ||
532 | DBG("Found serial console at ttyS%d\n", offset); | |
533 | ||
534 | if (speed) { | |
535 | static char __initdata opt[16]; | |
536 | sprintf(opt, "%d", speed); | |
537 | return add_preferred_console("ttyS", offset, opt); | |
538 | } else | |
539 | return add_preferred_console("ttyS", offset, NULL); | |
540 | ||
541 | not_found: | |
542 | DBG("No preferred console found !\n"); | |
543 | of_node_put(prom_stdout); | |
544 | return -ENODEV; | |
545 | } | |
546 | console_initcall(check_legacy_serial_console); | |
547 |