blob: 76b862bd1fe9aab628afabc4180b120040b3b7a8 [file] [log] [blame]
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +11001#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>
Paul Gortmaker1a7507c2008-01-24 11:59:12 -05007#include <linux/of_device.h>
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +11008#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
26static struct plat_serial8250_port
27legacy_serial_ports[MAX_LEGACY_SERIAL_PORTS+1];
28static struct legacy_serial_info {
29 struct device_node *np;
30 unsigned int speed;
31 unsigned int clock;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100032 int irq_check_parent;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110033 phys_addr_t taddr;
34} legacy_serial_infos[MAX_LEGACY_SERIAL_PORTS];
Paul Gortmaker1a7507c2008-01-24 11:59:12 -050035
36static struct __initdata of_device_id parents[] = {
37 {.type = "soc",},
38 {.type = "tsi-bridge",},
39 {.type = "opb", .compatible = "ibm,opb",},
40 {.compatible = "simple-bus",},
41 {.compatible = "wrs,epld-localbus",},
42};
43
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110044static unsigned int legacy_serial_count;
45static int legacy_serial_console = -1;
46
47static int __init add_legacy_port(struct device_node *np, int want_index,
48 int iotype, phys_addr_t base,
Kumar Galab580d462005-12-20 16:16:52 -060049 phys_addr_t taddr, unsigned long irq,
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100050 upf_t flags, int irq_check_parent)
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110051{
Jeremy Kerra7f67bd2006-07-12 15:35:54 +100052 const u32 *clk, *spd;
53 u32 clock = BASE_BAUD * 16;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110054 int index;
55
56 /* get clock freq. if present */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +100057 clk = of_get_property(np, "clock-frequency", NULL);
David Woodhouse31df1672005-11-24 17:08:56 +000058 if (clk && *clk)
59 clock = *clk;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110060
61 /* get default speed if present */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +100062 spd = of_get_property(np, "current-speed", NULL);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110063
64 /* If we have a location index, then try to use it */
65 if (want_index >= 0 && want_index < MAX_LEGACY_SERIAL_PORTS)
66 index = want_index;
67 else
68 index = legacy_serial_count;
69
70 /* if our index is still out of range, that mean that
71 * array is full, we could scan for a free slot but that
72 * make little sense to bother, just skip the port
73 */
74 if (index >= MAX_LEGACY_SERIAL_PORTS)
75 return -1;
76 if (index >= legacy_serial_count)
77 legacy_serial_count = index + 1;
78
79 /* Check if there is a port who already claimed our slot */
80 if (legacy_serial_infos[index].np != 0) {
81 /* if we still have some room, move it, else override */
82 if (legacy_serial_count < MAX_LEGACY_SERIAL_PORTS) {
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100083 printk(KERN_DEBUG "Moved legacy port %d -> %d\n",
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110084 index, legacy_serial_count);
85 legacy_serial_ports[legacy_serial_count] =
86 legacy_serial_ports[index];
87 legacy_serial_infos[legacy_serial_count] =
88 legacy_serial_infos[index];
89 legacy_serial_count++;
90 } else {
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100091 printk(KERN_DEBUG "Replacing legacy port %d\n", index);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110092 }
93 }
94
95 /* Now fill the entry */
96 memset(&legacy_serial_ports[index], 0,
97 sizeof(struct plat_serial8250_port));
98 if (iotype == UPIO_PORT)
99 legacy_serial_ports[index].iobase = base;
100 else
Benjamin Herrenschmidt8dacaed2005-11-29 11:21:59 +1100101 legacy_serial_ports[index].mapbase = base;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100102 legacy_serial_ports[index].iotype = iotype;
103 legacy_serial_ports[index].uartclk = clock;
104 legacy_serial_ports[index].irq = irq;
Kumar Galab580d462005-12-20 16:16:52 -0600105 legacy_serial_ports[index].flags = flags;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100106 legacy_serial_infos[index].taddr = taddr;
107 legacy_serial_infos[index].np = of_node_get(np);
108 legacy_serial_infos[index].clock = clock;
109 legacy_serial_infos[index].speed = spd ? *spd : 0;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000110 legacy_serial_infos[index].irq_check_parent = irq_check_parent;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100111
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000112 printk(KERN_DEBUG "Found legacy serial port %d for %s\n",
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100113 index, np->full_name);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000114 printk(KERN_DEBUG " %s=%llx, taddr=%llx, irq=%lx, clk=%d, speed=%d\n",
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100115 (iotype == UPIO_PORT) ? "port" : "mem",
116 (unsigned long long)base, (unsigned long long)taddr, irq,
117 legacy_serial_ports[index].uartclk,
118 legacy_serial_infos[index].speed);
119
120 return index;
121}
122
Kumar Galab580d462005-12-20 16:16:52 -0600123static int __init add_legacy_soc_port(struct device_node *np,
124 struct device_node *soc_dev)
125{
Benjamin Herrenschmidtf704b8d2006-07-04 14:14:07 +1000126 u64 addr;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000127 const u32 *addrp;
David Gibsonabb4a232007-05-06 14:48:49 -0700128 upf_t flags = UPF_BOOT_AUTOCONF | UPF_SKIP_TEST | UPF_SHARE_IRQ
129 | UPF_FIXED_PORT;
Zang Roy-r61911be9633e2006-08-23 10:20:27 +0800130 struct device_node *tsi = of_get_parent(np);
Kumar Galab580d462005-12-20 16:16:52 -0600131
132 /* We only support ports that have a clock frequency properly
133 * encoded in the device-tree.
134 */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000135 if (of_get_property(np, "clock-frequency", NULL) == NULL)
Kumar Galab580d462005-12-20 16:16:52 -0600136 return -1;
137
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100138 /* if rtas uses this device, don't try to use it as well */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000139 if (of_get_property(np, "used-by-rtas", NULL) != NULL)
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100140 return -1;
141
Kumar Galab580d462005-12-20 16:16:52 -0600142 /* Get the address */
143 addrp = of_get_address(soc_dev, 0, NULL, NULL);
144 if (addrp == NULL)
145 return -1;
146
147 addr = of_translate_address(soc_dev, addrp);
Benjamin Herrenschmidt7c6efda2006-07-03 17:24:15 +1000148 if (addr == OF_BAD_ADDR)
149 return -1;
Kumar Galab580d462005-12-20 16:16:52 -0600150
151 /* Add port, irq will be dealt with later. We passed a translated
152 * IO port value. It will be fixed up later along with the irq
153 */
Zang Roy-r61911be9633e2006-08-23 10:20:27 +0800154 if (tsi && !strcmp(tsi->type, "tsi-bridge"))
155 return add_legacy_port(np, -1, UPIO_TSI, addr, addr, NO_IRQ, flags, 0);
156 else
157 return add_legacy_port(np, -1, UPIO_MEM, addr, addr, NO_IRQ, flags, 0);
Kumar Galab580d462005-12-20 16:16:52 -0600158}
159
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100160static int __init add_legacy_isa_port(struct device_node *np,
Kumar Gala017e0fa2006-01-03 16:15:21 -0600161 struct device_node *isa_brg)
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100162{
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000163 const u32 *reg;
164 const char *typep;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100165 int index = -1;
Benjamin Herrenschmidtf704b8d2006-07-04 14:14:07 +1000166 u64 taddr;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100167
Benjamin Herrenschmidt7c6efda2006-07-03 17:24:15 +1000168 DBG(" -> add_legacy_isa_port(%s)\n", np->full_name);
169
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100170 /* Get the ISA port number */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000171 reg = of_get_property(np, "reg", NULL);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100172 if (reg == NULL)
173 return -1;
174
175 /* Verify it's an IO port, we don't support anything else */
176 if (!(reg[0] & 0x00000001))
177 return -1;
178
179 /* Now look for an "ibm,aix-loc" property that gives us ordering
180 * if any...
181 */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000182 typep = of_get_property(np, "ibm,aix-loc", NULL);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100183
184 /* If we have a location index, then use it */
185 if (typep && *typep == 'S')
186 index = simple_strtol(typep+1, NULL, 0) - 1;
187
Benjamin Herrenschmidtf704b8d2006-07-04 14:14:07 +1000188 /* Translate ISA address. If it fails, we still register the port
189 * with no translated address so that it can be picked up as an IO
190 * port later by the serial driver
191 */
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100192 taddr = of_translate_address(np, reg);
Benjamin Herrenschmidt7c6efda2006-07-03 17:24:15 +1000193 if (taddr == OF_BAD_ADDR)
Benjamin Herrenschmidtf704b8d2006-07-04 14:14:07 +1000194 taddr = 0;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100195
196 /* Add port, irq will be dealt with later */
Benjamin Herrenschmidt7c6efda2006-07-03 17:24:15 +1000197 return add_legacy_port(np, index, UPIO_PORT, reg[1], taddr,
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000198 NO_IRQ, UPF_BOOT_AUTOCONF, 0);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100199
200}
201
Kumar Gala017e0fa2006-01-03 16:15:21 -0600202#ifdef CONFIG_PCI
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100203static int __init add_legacy_pci_port(struct device_node *np,
204 struct device_node *pci_dev)
205{
Benjamin Herrenschmidtf704b8d2006-07-04 14:14:07 +1000206 u64 addr, base;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000207 const u32 *addrp;
Benjamin Herrenschmidtd2dd4822005-11-30 16:57:28 +1100208 unsigned int flags;
Benjamin Herrenschmidt8dacaed2005-11-29 11:21:59 +1100209 int iotype, index = -1, lindex = 0;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100210
Benjamin Herrenschmidt7c6efda2006-07-03 17:24:15 +1000211 DBG(" -> add_legacy_pci_port(%s)\n", np->full_name);
212
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100213 /* We only support ports that have a clock frequency properly
214 * encoded in the device-tree (that is have an fcode). Anything
215 * else can't be used that early and will be normally probed by
Benjamin Herrenschmidt8dacaed2005-11-29 11:21:59 +1100216 * the generic 8250_pci driver later on. The reason is that 8250
217 * compatible UARTs on PCI need all sort of quirks (port offsets
218 * etc...) that this code doesn't know about
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100219 */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000220 if (of_get_property(np, "clock-frequency", NULL) == NULL)
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100221 return -1;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100222
223 /* Get the PCI address. Assume BAR 0 */
Benjamin Herrenschmidtd2dd4822005-11-30 16:57:28 +1100224 addrp = of_get_pci_address(pci_dev, 0, NULL, &flags);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100225 if (addrp == NULL)
226 return -1;
227
228 /* We only support BAR 0 for now */
Benjamin Herrenschmidtd2dd4822005-11-30 16:57:28 +1100229 iotype = (flags & IORESOURCE_MEM) ? UPIO_MEM : UPIO_PORT;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100230 addr = of_translate_address(pci_dev, addrp);
Benjamin Herrenschmidt7c6efda2006-07-03 17:24:15 +1000231 if (addr == OF_BAD_ADDR)
232 return -1;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100233
234 /* Set the IO base to the same as the translated address for MMIO,
235 * or to the domain local IO base for PIO (it will be fixed up later)
236 */
237 if (iotype == UPIO_MEM)
238 base = addr;
239 else
240 base = addrp[2];
241
242 /* Try to guess an index... If we have subdevices of the pci dev,
243 * we get to their "reg" property
244 */
245 if (np != pci_dev) {
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000246 const u32 *reg = of_get_property(np, "reg", NULL);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100247 if (reg && (*reg < 4))
Benjamin Herrenschmidt8dacaed2005-11-29 11:21:59 +1100248 index = lindex = *reg;
249 }
250
251 /* Local index means it's the Nth port in the PCI chip. Unfortunately
252 * the offset to add here is device specific. We know about those
253 * EXAR ports and we default to the most common case. If your UART
254 * doesn't work for these settings, you'll have to add your own special
255 * cases here
256 */
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000257 if (of_device_is_compatible(pci_dev, "pci13a8,152") ||
258 of_device_is_compatible(pci_dev, "pci13a8,154") ||
259 of_device_is_compatible(pci_dev, "pci13a8,158")) {
Benjamin Herrenschmidt8dacaed2005-11-29 11:21:59 +1100260 addr += 0x200 * lindex;
261 base += 0x200 * lindex;
262 } else {
263 addr += 8 * lindex;
264 base += 8 * lindex;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100265 }
266
267 /* Add port, irq will be dealt with later. We passed a translated
268 * IO port value. It will be fixed up later along with the irq
269 */
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000270 return add_legacy_port(np, index, iotype, base, addr, NO_IRQ,
271 UPF_BOOT_AUTOCONF, np != pci_dev);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100272}
Kumar Gala017e0fa2006-01-03 16:15:21 -0600273#endif
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100274
Michael Neuling37a801c2006-03-26 10:07:35 +1000275static void __init setup_legacy_serial_console(int console)
276{
277 struct legacy_serial_info *info =
278 &legacy_serial_infos[console];
279 void __iomem *addr;
280
281 if (info->taddr == 0)
282 return;
283 addr = ioremap(info->taddr, 0x1000);
284 if (addr == NULL)
285 return;
286 if (info->speed == 0)
287 info->speed = udbg_probe_uart_speed(addr, info->clock);
288 DBG("default console speed = %d\n", info->speed);
289 udbg_init_uart(addr, info->speed, info->clock);
290}
291
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100292/*
293 * This is called very early, as part of setup_system() or eventually
294 * setup_arch(), basically before anything else in this file. This function
295 * will try to build a list of all the available 8250-compatible serial ports
296 * in the machine using the Open Firmware device-tree. It currently only deals
297 * with ISA and PCI busses but could be extended. It allows a very early boot
298 * console to be initialized, that list is also used later to provide 8250 with
299 * the machine non-PCI ports and to properly pick the default console port
300 */
301void __init find_legacy_serial_ports(void)
302{
Kumar Galab580d462005-12-20 16:16:52 -0600303 struct device_node *np, *stdout = NULL;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000304 const char *path;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100305 int index;
306
307 DBG(" -> find_legacy_serial_port()\n");
308
309 /* Now find out if one of these is out firmware console */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000310 path = of_get_property(of_chosen, "linux,stdout-path", NULL);
Kumar Galab580d462005-12-20 16:16:52 -0600311 if (path != NULL) {
312 stdout = of_find_node_by_path(path);
313 if (stdout)
314 DBG("stdout is %s\n", stdout->full_name);
315 } else {
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100316 DBG(" no linux,stdout-path !\n");
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100317 }
Kumar Galab580d462005-12-20 16:16:52 -0600318
Paul Gortmaker1a7507c2008-01-24 11:59:12 -0500319 /* Iterate over all the 16550 ports, looking for known parents */
Cyrill Gorcunov3329c0d2007-11-30 06:45:47 +1100320 for_each_compatible_node(np, "serial", "ns16550") {
Paul Gortmaker1a7507c2008-01-24 11:59:12 -0500321 struct device_node *parent = of_get_parent(np);
322 if (!parent)
323 continue;
324 if (of_match_node(parents, parent) != NULL) {
Kumar Galab580d462005-12-20 16:16:52 -0600325 index = add_legacy_soc_port(np, np);
326 if (index >= 0 && np == stdout)
327 legacy_serial_console = index;
328 }
Paul Gortmaker1a7507c2008-01-24 11:59:12 -0500329 of_node_put(parent);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100330 }
331
Paul Gortmaker1a7507c2008-01-24 11:59:12 -0500332 /* Next, fill our array with ISA ports */
Cyrill Gorcunov3329c0d2007-11-30 06:45:47 +1100333 for_each_node_by_type(np, "serial") {
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100334 struct device_node *isa = of_get_parent(np);
335 if (isa && !strcmp(isa->name, "isa")) {
336 index = add_legacy_isa_port(np, isa);
337 if (index >= 0 && np == stdout)
338 legacy_serial_console = index;
339 }
340 of_node_put(isa);
341 }
342
Kumar Gala017e0fa2006-01-03 16:15:21 -0600343#ifdef CONFIG_PCI
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100344 /* Next, try to locate PCI ports */
345 for (np = NULL; (np = of_find_all_nodes(np));) {
346 struct device_node *pci, *parent = of_get_parent(np);
347 if (parent && !strcmp(parent->name, "isa")) {
348 of_node_put(parent);
349 continue;
350 }
351 if (strcmp(np->name, "serial") && strcmp(np->type, "serial")) {
352 of_node_put(parent);
353 continue;
354 }
355 /* Check for known pciclass, and also check wether we have
356 * a device with child nodes for ports or not
357 */
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000358 if (of_device_is_compatible(np, "pciclass,0700") ||
359 of_device_is_compatible(np, "pciclass,070002"))
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100360 pci = np;
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000361 else if (of_device_is_compatible(parent, "pciclass,0700") ||
362 of_device_is_compatible(parent, "pciclass,070002"))
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100363 pci = parent;
364 else {
365 of_node_put(parent);
366 continue;
367 }
368 index = add_legacy_pci_port(np, pci);
369 if (index >= 0 && np == stdout)
370 legacy_serial_console = index;
371 of_node_put(parent);
372 }
Kumar Gala017e0fa2006-01-03 16:15:21 -0600373#endif
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100374
375 DBG("legacy_serial_console = %d\n", legacy_serial_console);
Michael Neuling37a801c2006-03-26 10:07:35 +1000376 if (legacy_serial_console >= 0)
377 setup_legacy_serial_console(legacy_serial_console);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100378 DBG(" <- find_legacy_serial_port()\n");
379}
380
381static struct platform_device serial_device = {
382 .name = "serial8250",
383 .id = PLAT8250_DEV_PLATFORM,
384 .dev = {
385 .platform_data = legacy_serial_ports,
386 },
387};
388
389static void __init fixup_port_irq(int index,
390 struct device_node *np,
391 struct plat_serial8250_port *port)
392{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000393 unsigned int virq;
394
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100395 DBG("fixup_port_irq(%d)\n", index);
396
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000397 virq = irq_of_parse_and_map(np, 0);
398 if (virq == NO_IRQ && legacy_serial_infos[index].irq_check_parent) {
399 np = of_get_parent(np);
400 if (np == NULL)
401 return;
402 virq = irq_of_parse_and_map(np, 0);
403 of_node_put(np);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100404 }
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000405 if (virq == NO_IRQ)
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100406 return;
407
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000408 port->irq = virq;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100409}
410
411static void __init fixup_port_pio(int index,
412 struct device_node *np,
413 struct plat_serial8250_port *port)
414{
Kumar Gala017e0fa2006-01-03 16:15:21 -0600415#ifdef CONFIG_PCI
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100416 struct pci_controller *hose;
417
418 DBG("fixup_port_pio(%d)\n", index);
419
420 hose = pci_find_hose_for_OF_device(np);
421 if (hose) {
422 unsigned long offset = (unsigned long)hose->io_base_virt -
423#ifdef CONFIG_PPC64
424 pci_io_base;
425#else
426 isa_io_base;
427#endif
428 DBG("port %d, IO %lx -> %lx\n",
429 index, port->iobase, port->iobase + offset);
430 port->iobase += offset;
431 }
Kumar Gala017e0fa2006-01-03 16:15:21 -0600432#endif
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100433}
434
Benjamin Herrenschmidt8dacaed2005-11-29 11:21:59 +1100435static void __init fixup_port_mmio(int index,
436 struct device_node *np,
437 struct plat_serial8250_port *port)
438{
439 DBG("fixup_port_mmio(%d)\n", index);
440
441 port->membase = ioremap(port->mapbase, 0x100);
442}
443
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100444/*
445 * This is called as an arch initcall, hopefully before the PCI bus is
446 * probed and/or the 8250 driver loaded since we need to register our
447 * platform devices before 8250 PCI ones are detected as some of them
448 * must properly "override" the platform ones.
449 *
450 * This function fixes up the interrupt value for platform ports as it
451 * couldn't be done earlier before interrupt maps have been parsed. It
452 * also "corrects" the IO address for PIO ports for the same reason,
453 * since earlier, the PHBs virtual IO space wasn't assigned yet. It then
454 * registers all those platform ports for use by the 8250 driver when it
455 * finally loads.
456 */
457static int __init serial_dev_init(void)
458{
459 int i;
460
461 if (legacy_serial_count == 0)
462 return -ENODEV;
463
464 /*
465 * Before we register the platfrom serial devices, we need
joe@perches.com00d70412007-12-18 06:30:12 +1100466 * to fixup their interrupts and their IO ports.
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100467 */
468 DBG("Fixing serial ports interrupts and IO ports ...\n");
469
470 for (i = 0; i < legacy_serial_count; i++) {
471 struct plat_serial8250_port *port = &legacy_serial_ports[i];
472 struct device_node *np = legacy_serial_infos[i].np;
473
474 if (port->irq == NO_IRQ)
475 fixup_port_irq(i, np, port);
476 if (port->iotype == UPIO_PORT)
477 fixup_port_pio(i, np, port);
Zang Roy-r61911be9633e2006-08-23 10:20:27 +0800478 if ((port->iotype == UPIO_MEM) || (port->iotype == UPIO_TSI))
Benjamin Herrenschmidt8dacaed2005-11-29 11:21:59 +1100479 fixup_port_mmio(i, np, port);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100480 }
481
482 DBG("Registering platform serial ports\n");
483
484 return platform_device_register(&serial_device);
485}
Olof Johanssonee56c472007-08-22 19:26:37 -0500486device_initcall(serial_dev_init);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100487
488
489/*
490 * This is called very early, as part of console_init() (typically just after
491 * time_init()). This function is respondible for trying to find a good
492 * default console on serial ports. It tries to match the open firmware
493 * default output with one of the available serial console drivers, either
494 * one of the platform serial ports that have been probed earlier by
495 * find_legacy_serial_ports() or some more platform specific ones.
496 */
497static int __init check_legacy_serial_console(void)
498{
499 struct device_node *prom_stdout = NULL;
500 int speed = 0, offset = 0;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000501 const char *name;
502 const u32 *spd;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100503
504 DBG(" -> check_legacy_serial_console()\n");
505
506 /* The user has requested a console so this is already set up. */
Alon Bar-Levb8757b22007-02-12 00:54:17 -0800507 if (strstr(boot_command_line, "console=")) {
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100508 DBG(" console was specified !\n");
509 return -EBUSY;
510 }
511
512 if (!of_chosen) {
513 DBG(" of_chosen is NULL !\n");
514 return -ENODEV;
515 }
Kumar Galab580d462005-12-20 16:16:52 -0600516
517 if (legacy_serial_console < 0) {
518 DBG(" legacy_serial_console not found !\n");
519 return -ENODEV;
520 }
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100521 /* We are getting a weird phandle from OF ... */
522 /* ... So use the full path instead */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000523 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100524 if (name == NULL) {
525 DBG(" no linux,stdout-path !\n");
526 return -ENODEV;
527 }
528 prom_stdout = of_find_node_by_path(name);
529 if (!prom_stdout) {
530 DBG(" can't find stdout package %s !\n", name);
531 return -ENODEV;
532 }
533 DBG("stdout is %s\n", prom_stdout->full_name);
534
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000535 name = of_get_property(prom_stdout, "name", NULL);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100536 if (!name) {
537 DBG(" stdout package has no name !\n");
538 goto not_found;
539 }
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000540 spd = of_get_property(prom_stdout, "current-speed", NULL);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100541 if (spd)
542 speed = *spd;
543
544 if (0)
545 ;
546#ifdef CONFIG_SERIAL_8250_CONSOLE
547 else if (strcmp(name, "serial") == 0) {
548 int i;
549 /* Look for it in probed array */
550 for (i = 0; i < legacy_serial_count; i++) {
551 if (prom_stdout != legacy_serial_infos[i].np)
552 continue;
553 offset = i;
554 speed = legacy_serial_infos[i].speed;
555 break;
556 }
557 if (i >= legacy_serial_count)
558 goto not_found;
559 }
560#endif /* CONFIG_SERIAL_8250_CONSOLE */
561#ifdef CONFIG_SERIAL_PMACZILOG_CONSOLE
562 else if (strcmp(name, "ch-a") == 0)
563 offset = 0;
564 else if (strcmp(name, "ch-b") == 0)
565 offset = 1;
566#endif /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
567 else
568 goto not_found;
569 of_node_put(prom_stdout);
570
571 DBG("Found serial console at ttyS%d\n", offset);
572
573 if (speed) {
574 static char __initdata opt[16];
575 sprintf(opt, "%d", speed);
576 return add_preferred_console("ttyS", offset, opt);
577 } else
578 return add_preferred_console("ttyS", offset, NULL);
579
580 not_found:
581 DBG("No preferred console found !\n");
582 of_node_put(prom_stdout);
583 return -ENODEV;
584}
585console_initcall(check_legacy_serial_console);
586