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