blob: 4d96e1db55ee276c6c7a1e5e7b063c38d926b338 [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
Benjamin Herrenschmidt3bc5ab92008-07-07 16:39:50 +100036static struct __initdata of_device_id legacy_serial_parents[] = {
Paul Gortmaker1a7507c2008-01-24 11:59:12 -050037 {.type = "soc",},
38 {.type = "tsi-bridge",},
Michael Ellermanf5903ed2008-02-05 23:01:50 +110039 {.type = "opb", },
40 {.compatible = "ibm,opb",},
Paul Gortmaker1a7507c2008-01-24 11:59:12 -050041 {.compatible = "simple-bus",},
42 {.compatible = "wrs,epld-localbus",},
Benjamin Herrenschmidt3bc5ab92008-07-07 16:39:50 +100043 {},
Paul Gortmaker1a7507c2008-01-24 11:59:12 -050044};
45
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110046static unsigned int legacy_serial_count;
47static int legacy_serial_console = -1;
48
49static int __init add_legacy_port(struct device_node *np, int want_index,
50 int iotype, phys_addr_t base,
Kumar Galab580d462005-12-20 16:16:52 -060051 phys_addr_t taddr, unsigned long irq,
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100052 upf_t flags, int irq_check_parent)
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110053{
Jeremy Kerra7f67bd2006-07-12 15:35:54 +100054 const u32 *clk, *spd;
55 u32 clock = BASE_BAUD * 16;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110056 int index;
57
58 /* get clock freq. if present */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +100059 clk = of_get_property(np, "clock-frequency", NULL);
David Woodhouse31df1672005-11-24 17:08:56 +000060 if (clk && *clk)
61 clock = *clk;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110062
63 /* get default speed if present */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +100064 spd = of_get_property(np, "current-speed", NULL);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110065
66 /* If we have a location index, then try to use it */
67 if (want_index >= 0 && want_index < MAX_LEGACY_SERIAL_PORTS)
68 index = want_index;
69 else
70 index = legacy_serial_count;
71
72 /* if our index is still out of range, that mean that
73 * array is full, we could scan for a free slot but that
74 * make little sense to bother, just skip the port
75 */
76 if (index >= MAX_LEGACY_SERIAL_PORTS)
77 return -1;
78 if (index >= legacy_serial_count)
79 legacy_serial_count = index + 1;
80
81 /* Check if there is a port who already claimed our slot */
82 if (legacy_serial_infos[index].np != 0) {
83 /* if we still have some room, move it, else override */
84 if (legacy_serial_count < MAX_LEGACY_SERIAL_PORTS) {
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100085 printk(KERN_DEBUG "Moved legacy port %d -> %d\n",
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110086 index, legacy_serial_count);
87 legacy_serial_ports[legacy_serial_count] =
88 legacy_serial_ports[index];
89 legacy_serial_infos[legacy_serial_count] =
90 legacy_serial_infos[index];
91 legacy_serial_count++;
92 } else {
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100093 printk(KERN_DEBUG "Replacing legacy port %d\n", index);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110094 }
95 }
96
97 /* Now fill the entry */
98 memset(&legacy_serial_ports[index], 0,
99 sizeof(struct plat_serial8250_port));
100 if (iotype == UPIO_PORT)
101 legacy_serial_ports[index].iobase = base;
102 else
Benjamin Herrenschmidt8dacaed2005-11-29 11:21:59 +1100103 legacy_serial_ports[index].mapbase = base;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100104 legacy_serial_ports[index].iotype = iotype;
105 legacy_serial_ports[index].uartclk = clock;
106 legacy_serial_ports[index].irq = irq;
Kumar Galab580d462005-12-20 16:16:52 -0600107 legacy_serial_ports[index].flags = flags;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100108 legacy_serial_infos[index].taddr = taddr;
109 legacy_serial_infos[index].np = of_node_get(np);
110 legacy_serial_infos[index].clock = clock;
111 legacy_serial_infos[index].speed = spd ? *spd : 0;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000112 legacy_serial_infos[index].irq_check_parent = irq_check_parent;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100113
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000114 printk(KERN_DEBUG "Found legacy serial port %d for %s\n",
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100115 index, np->full_name);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000116 printk(KERN_DEBUG " %s=%llx, taddr=%llx, irq=%lx, clk=%d, speed=%d\n",
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100117 (iotype == UPIO_PORT) ? "port" : "mem",
118 (unsigned long long)base, (unsigned long long)taddr, irq,
119 legacy_serial_ports[index].uartclk,
120 legacy_serial_infos[index].speed);
121
122 return index;
123}
124
Kumar Galab580d462005-12-20 16:16:52 -0600125static int __init add_legacy_soc_port(struct device_node *np,
126 struct device_node *soc_dev)
127{
Benjamin Herrenschmidtf704b8d2006-07-04 14:14:07 +1000128 u64 addr;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000129 const u32 *addrp;
David Gibsonabb4a232007-05-06 14:48:49 -0700130 upf_t flags = UPF_BOOT_AUTOCONF | UPF_SKIP_TEST | UPF_SHARE_IRQ
131 | UPF_FIXED_PORT;
Zang Roy-r61911be9633e2006-08-23 10:20:27 +0800132 struct device_node *tsi = of_get_parent(np);
Kumar Galab580d462005-12-20 16:16:52 -0600133
134 /* We only support ports that have a clock frequency properly
135 * encoded in the device-tree.
136 */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000137 if (of_get_property(np, "clock-frequency", NULL) == NULL)
Kumar Galab580d462005-12-20 16:16:52 -0600138 return -1;
139
John Linn1e6d1f22008-07-01 10:52:41 -0700140 /* if reg-shift or offset, don't try to use it */
141 if ((of_get_property(np, "reg-shift", NULL) != NULL) ||
142 (of_get_property(np, "reg-offset", NULL) != NULL))
143 return -1;
144
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100145 /* if rtas uses this device, don't try to use it as well */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000146 if (of_get_property(np, "used-by-rtas", NULL) != NULL)
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100147 return -1;
148
Kumar Galab580d462005-12-20 16:16:52 -0600149 /* Get the address */
150 addrp = of_get_address(soc_dev, 0, NULL, NULL);
151 if (addrp == NULL)
152 return -1;
153
154 addr = of_translate_address(soc_dev, addrp);
Benjamin Herrenschmidt7c6efda2006-07-03 17:24:15 +1000155 if (addr == OF_BAD_ADDR)
156 return -1;
Kumar Galab580d462005-12-20 16:16:52 -0600157
158 /* Add port, irq will be dealt with later. We passed a translated
159 * IO port value. It will be fixed up later along with the irq
160 */
Zang Roy-r61911be9633e2006-08-23 10:20:27 +0800161 if (tsi && !strcmp(tsi->type, "tsi-bridge"))
162 return add_legacy_port(np, -1, UPIO_TSI, addr, addr, NO_IRQ, flags, 0);
163 else
164 return add_legacy_port(np, -1, UPIO_MEM, addr, addr, NO_IRQ, flags, 0);
Kumar Galab580d462005-12-20 16:16:52 -0600165}
166
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100167static int __init add_legacy_isa_port(struct device_node *np,
Kumar Gala017e0fa2006-01-03 16:15:21 -0600168 struct device_node *isa_brg)
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100169{
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000170 const u32 *reg;
171 const char *typep;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100172 int index = -1;
Benjamin Herrenschmidtf704b8d2006-07-04 14:14:07 +1000173 u64 taddr;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100174
Benjamin Herrenschmidt7c6efda2006-07-03 17:24:15 +1000175 DBG(" -> add_legacy_isa_port(%s)\n", np->full_name);
176
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100177 /* Get the ISA port number */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000178 reg = of_get_property(np, "reg", NULL);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100179 if (reg == NULL)
180 return -1;
181
182 /* Verify it's an IO port, we don't support anything else */
183 if (!(reg[0] & 0x00000001))
184 return -1;
185
186 /* Now look for an "ibm,aix-loc" property that gives us ordering
187 * if any...
188 */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000189 typep = of_get_property(np, "ibm,aix-loc", NULL);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100190
191 /* If we have a location index, then use it */
192 if (typep && *typep == 'S')
193 index = simple_strtol(typep+1, NULL, 0) - 1;
194
Benjamin Herrenschmidtf704b8d2006-07-04 14:14:07 +1000195 /* Translate ISA address. If it fails, we still register the port
196 * with no translated address so that it can be picked up as an IO
197 * port later by the serial driver
198 */
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100199 taddr = of_translate_address(np, reg);
Benjamin Herrenschmidt7c6efda2006-07-03 17:24:15 +1000200 if (taddr == OF_BAD_ADDR)
Benjamin Herrenschmidtf704b8d2006-07-04 14:14:07 +1000201 taddr = 0;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100202
203 /* Add port, irq will be dealt with later */
Benjamin Herrenschmidt7c6efda2006-07-03 17:24:15 +1000204 return add_legacy_port(np, index, UPIO_PORT, reg[1], taddr,
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000205 NO_IRQ, UPF_BOOT_AUTOCONF, 0);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100206
207}
208
Kumar Gala017e0fa2006-01-03 16:15:21 -0600209#ifdef CONFIG_PCI
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100210static int __init add_legacy_pci_port(struct device_node *np,
211 struct device_node *pci_dev)
212{
Benjamin Herrenschmidtf704b8d2006-07-04 14:14:07 +1000213 u64 addr, base;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000214 const u32 *addrp;
Benjamin Herrenschmidtd2dd4822005-11-30 16:57:28 +1100215 unsigned int flags;
Benjamin Herrenschmidt8dacaed2005-11-29 11:21:59 +1100216 int iotype, index = -1, lindex = 0;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100217
Benjamin Herrenschmidt7c6efda2006-07-03 17:24:15 +1000218 DBG(" -> add_legacy_pci_port(%s)\n", np->full_name);
219
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100220 /* We only support ports that have a clock frequency properly
221 * encoded in the device-tree (that is have an fcode). Anything
222 * else can't be used that early and will be normally probed by
Benjamin Herrenschmidt8dacaed2005-11-29 11:21:59 +1100223 * the generic 8250_pci driver later on. The reason is that 8250
224 * compatible UARTs on PCI need all sort of quirks (port offsets
225 * etc...) that this code doesn't know about
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100226 */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000227 if (of_get_property(np, "clock-frequency", NULL) == NULL)
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100228 return -1;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100229
230 /* Get the PCI address. Assume BAR 0 */
Benjamin Herrenschmidtd2dd4822005-11-30 16:57:28 +1100231 addrp = of_get_pci_address(pci_dev, 0, NULL, &flags);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100232 if (addrp == NULL)
233 return -1;
234
235 /* We only support BAR 0 for now */
Benjamin Herrenschmidtd2dd4822005-11-30 16:57:28 +1100236 iotype = (flags & IORESOURCE_MEM) ? UPIO_MEM : UPIO_PORT;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100237 addr = of_translate_address(pci_dev, addrp);
Benjamin Herrenschmidt7c6efda2006-07-03 17:24:15 +1000238 if (addr == OF_BAD_ADDR)
239 return -1;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100240
241 /* Set the IO base to the same as the translated address for MMIO,
242 * or to the domain local IO base for PIO (it will be fixed up later)
243 */
244 if (iotype == UPIO_MEM)
245 base = addr;
246 else
247 base = addrp[2];
248
249 /* Try to guess an index... If we have subdevices of the pci dev,
250 * we get to their "reg" property
251 */
252 if (np != pci_dev) {
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000253 const u32 *reg = of_get_property(np, "reg", NULL);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100254 if (reg && (*reg < 4))
Benjamin Herrenschmidt8dacaed2005-11-29 11:21:59 +1100255 index = lindex = *reg;
256 }
257
258 /* Local index means it's the Nth port in the PCI chip. Unfortunately
259 * the offset to add here is device specific. We know about those
260 * EXAR ports and we default to the most common case. If your UART
261 * doesn't work for these settings, you'll have to add your own special
262 * cases here
263 */
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000264 if (of_device_is_compatible(pci_dev, "pci13a8,152") ||
265 of_device_is_compatible(pci_dev, "pci13a8,154") ||
266 of_device_is_compatible(pci_dev, "pci13a8,158")) {
Benjamin Herrenschmidt8dacaed2005-11-29 11:21:59 +1100267 addr += 0x200 * lindex;
268 base += 0x200 * lindex;
269 } else {
270 addr += 8 * lindex;
271 base += 8 * lindex;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100272 }
273
274 /* Add port, irq will be dealt with later. We passed a translated
275 * IO port value. It will be fixed up later along with the irq
276 */
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000277 return add_legacy_port(np, index, iotype, base, addr, NO_IRQ,
278 UPF_BOOT_AUTOCONF, np != pci_dev);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100279}
Kumar Gala017e0fa2006-01-03 16:15:21 -0600280#endif
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100281
Michael Neuling37a801c2006-03-26 10:07:35 +1000282static void __init setup_legacy_serial_console(int console)
283{
284 struct legacy_serial_info *info =
285 &legacy_serial_infos[console];
286 void __iomem *addr;
287
288 if (info->taddr == 0)
289 return;
290 addr = ioremap(info->taddr, 0x1000);
291 if (addr == NULL)
292 return;
293 if (info->speed == 0)
294 info->speed = udbg_probe_uart_speed(addr, info->clock);
295 DBG("default console speed = %d\n", info->speed);
296 udbg_init_uart(addr, info->speed, info->clock);
297}
298
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100299/*
300 * This is called very early, as part of setup_system() or eventually
301 * setup_arch(), basically before anything else in this file. This function
302 * will try to build a list of all the available 8250-compatible serial ports
303 * in the machine using the Open Firmware device-tree. It currently only deals
304 * with ISA and PCI busses but could be extended. It allows a very early boot
305 * console to be initialized, that list is also used later to provide 8250 with
306 * the machine non-PCI ports and to properly pick the default console port
307 */
308void __init find_legacy_serial_ports(void)
309{
Kumar Galab580d462005-12-20 16:16:52 -0600310 struct device_node *np, *stdout = NULL;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000311 const char *path;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100312 int index;
313
314 DBG(" -> find_legacy_serial_port()\n");
315
316 /* Now find out if one of these is out firmware console */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000317 path = of_get_property(of_chosen, "linux,stdout-path", NULL);
Kumar Galab580d462005-12-20 16:16:52 -0600318 if (path != NULL) {
319 stdout = of_find_node_by_path(path);
320 if (stdout)
321 DBG("stdout is %s\n", stdout->full_name);
322 } else {
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100323 DBG(" no linux,stdout-path !\n");
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100324 }
Kumar Galab580d462005-12-20 16:16:52 -0600325
Paul Gortmaker1a7507c2008-01-24 11:59:12 -0500326 /* Iterate over all the 16550 ports, looking for known parents */
Cyrill Gorcunov3329c0d2007-11-30 06:45:47 +1100327 for_each_compatible_node(np, "serial", "ns16550") {
Paul Gortmaker1a7507c2008-01-24 11:59:12 -0500328 struct device_node *parent = of_get_parent(np);
329 if (!parent)
330 continue;
Benjamin Herrenschmidt3bc5ab92008-07-07 16:39:50 +1000331 if (of_match_node(legacy_serial_parents, parent) != NULL) {
Kumar Galab580d462005-12-20 16:16:52 -0600332 index = add_legacy_soc_port(np, np);
333 if (index >= 0 && np == stdout)
334 legacy_serial_console = index;
335 }
Paul Gortmaker1a7507c2008-01-24 11:59:12 -0500336 of_node_put(parent);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100337 }
338
Paul Gortmaker1a7507c2008-01-24 11:59:12 -0500339 /* Next, fill our array with ISA ports */
Cyrill Gorcunov3329c0d2007-11-30 06:45:47 +1100340 for_each_node_by_type(np, "serial") {
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100341 struct device_node *isa = of_get_parent(np);
342 if (isa && !strcmp(isa->name, "isa")) {
343 index = add_legacy_isa_port(np, isa);
344 if (index >= 0 && np == stdout)
345 legacy_serial_console = index;
346 }
347 of_node_put(isa);
348 }
349
Kumar Gala017e0fa2006-01-03 16:15:21 -0600350#ifdef CONFIG_PCI
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100351 /* Next, try to locate PCI ports */
352 for (np = NULL; (np = of_find_all_nodes(np));) {
353 struct device_node *pci, *parent = of_get_parent(np);
354 if (parent && !strcmp(parent->name, "isa")) {
355 of_node_put(parent);
356 continue;
357 }
358 if (strcmp(np->name, "serial") && strcmp(np->type, "serial")) {
359 of_node_put(parent);
360 continue;
361 }
362 /* Check for known pciclass, and also check wether we have
363 * a device with child nodes for ports or not
364 */
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000365 if (of_device_is_compatible(np, "pciclass,0700") ||
366 of_device_is_compatible(np, "pciclass,070002"))
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100367 pci = np;
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000368 else if (of_device_is_compatible(parent, "pciclass,0700") ||
369 of_device_is_compatible(parent, "pciclass,070002"))
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100370 pci = parent;
371 else {
372 of_node_put(parent);
373 continue;
374 }
375 index = add_legacy_pci_port(np, pci);
376 if (index >= 0 && np == stdout)
377 legacy_serial_console = index;
378 of_node_put(parent);
379 }
Kumar Gala017e0fa2006-01-03 16:15:21 -0600380#endif
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100381
382 DBG("legacy_serial_console = %d\n", legacy_serial_console);
Michael Neuling37a801c2006-03-26 10:07:35 +1000383 if (legacy_serial_console >= 0)
384 setup_legacy_serial_console(legacy_serial_console);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100385 DBG(" <- find_legacy_serial_port()\n");
386}
387
388static struct platform_device serial_device = {
389 .name = "serial8250",
390 .id = PLAT8250_DEV_PLATFORM,
391 .dev = {
392 .platform_data = legacy_serial_ports,
393 },
394};
395
396static void __init fixup_port_irq(int index,
397 struct device_node *np,
398 struct plat_serial8250_port *port)
399{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000400 unsigned int virq;
401
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100402 DBG("fixup_port_irq(%d)\n", index);
403
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000404 virq = irq_of_parse_and_map(np, 0);
405 if (virq == NO_IRQ && legacy_serial_infos[index].irq_check_parent) {
406 np = of_get_parent(np);
407 if (np == NULL)
408 return;
409 virq = irq_of_parse_and_map(np, 0);
410 of_node_put(np);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100411 }
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000412 if (virq == NO_IRQ)
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100413 return;
414
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000415 port->irq = virq;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100416}
417
418static void __init fixup_port_pio(int index,
419 struct device_node *np,
420 struct plat_serial8250_port *port)
421{
Kumar Gala017e0fa2006-01-03 16:15:21 -0600422#ifdef CONFIG_PCI
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100423 struct pci_controller *hose;
424
425 DBG("fixup_port_pio(%d)\n", index);
426
427 hose = pci_find_hose_for_OF_device(np);
428 if (hose) {
429 unsigned long offset = (unsigned long)hose->io_base_virt -
430#ifdef CONFIG_PPC64
431 pci_io_base;
432#else
433 isa_io_base;
434#endif
435 DBG("port %d, IO %lx -> %lx\n",
436 index, port->iobase, port->iobase + offset);
437 port->iobase += offset;
438 }
Kumar Gala017e0fa2006-01-03 16:15:21 -0600439#endif
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100440}
441
Benjamin Herrenschmidt8dacaed2005-11-29 11:21:59 +1100442static void __init fixup_port_mmio(int index,
443 struct device_node *np,
444 struct plat_serial8250_port *port)
445{
446 DBG("fixup_port_mmio(%d)\n", index);
447
448 port->membase = ioremap(port->mapbase, 0x100);
449}
450
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100451/*
452 * This is called as an arch initcall, hopefully before the PCI bus is
453 * probed and/or the 8250 driver loaded since we need to register our
454 * platform devices before 8250 PCI ones are detected as some of them
455 * must properly "override" the platform ones.
456 *
457 * This function fixes up the interrupt value for platform ports as it
458 * couldn't be done earlier before interrupt maps have been parsed. It
459 * also "corrects" the IO address for PIO ports for the same reason,
460 * since earlier, the PHBs virtual IO space wasn't assigned yet. It then
461 * registers all those platform ports for use by the 8250 driver when it
462 * finally loads.
463 */
464static int __init serial_dev_init(void)
465{
466 int i;
467
468 if (legacy_serial_count == 0)
469 return -ENODEV;
470
471 /*
472 * Before we register the platfrom serial devices, we need
joe@perches.com00d70412007-12-18 06:30:12 +1100473 * to fixup their interrupts and their IO ports.
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100474 */
475 DBG("Fixing serial ports interrupts and IO ports ...\n");
476
477 for (i = 0; i < legacy_serial_count; i++) {
478 struct plat_serial8250_port *port = &legacy_serial_ports[i];
479 struct device_node *np = legacy_serial_infos[i].np;
480
481 if (port->irq == NO_IRQ)
482 fixup_port_irq(i, np, port);
483 if (port->iotype == UPIO_PORT)
484 fixup_port_pio(i, np, port);
Zang Roy-r61911be9633e2006-08-23 10:20:27 +0800485 if ((port->iotype == UPIO_MEM) || (port->iotype == UPIO_TSI))
Benjamin Herrenschmidt8dacaed2005-11-29 11:21:59 +1100486 fixup_port_mmio(i, np, port);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100487 }
488
489 DBG("Registering platform serial ports\n");
490
491 return platform_device_register(&serial_device);
492}
Olof Johanssonee56c472007-08-22 19:26:37 -0500493device_initcall(serial_dev_init);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100494
495
496/*
497 * This is called very early, as part of console_init() (typically just after
498 * time_init()). This function is respondible for trying to find a good
499 * default console on serial ports. It tries to match the open firmware
500 * default output with one of the available serial console drivers, either
501 * one of the platform serial ports that have been probed earlier by
502 * find_legacy_serial_ports() or some more platform specific ones.
503 */
504static int __init check_legacy_serial_console(void)
505{
506 struct device_node *prom_stdout = NULL;
507 int speed = 0, offset = 0;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000508 const char *name;
509 const u32 *spd;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100510
511 DBG(" -> check_legacy_serial_console()\n");
512
513 /* The user has requested a console so this is already set up. */
Alon Bar-Levb8757b22007-02-12 00:54:17 -0800514 if (strstr(boot_command_line, "console=")) {
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100515 DBG(" console was specified !\n");
516 return -EBUSY;
517 }
518
519 if (!of_chosen) {
520 DBG(" of_chosen is NULL !\n");
521 return -ENODEV;
522 }
Kumar Galab580d462005-12-20 16:16:52 -0600523
524 if (legacy_serial_console < 0) {
525 DBG(" legacy_serial_console not found !\n");
526 return -ENODEV;
527 }
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100528 /* We are getting a weird phandle from OF ... */
529 /* ... So use the full path instead */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000530 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100531 if (name == NULL) {
532 DBG(" no linux,stdout-path !\n");
533 return -ENODEV;
534 }
535 prom_stdout = of_find_node_by_path(name);
536 if (!prom_stdout) {
537 DBG(" can't find stdout package %s !\n", name);
538 return -ENODEV;
539 }
540 DBG("stdout is %s\n", prom_stdout->full_name);
541
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000542 name = of_get_property(prom_stdout, "name", NULL);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100543 if (!name) {
544 DBG(" stdout package has no name !\n");
545 goto not_found;
546 }
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000547 spd = of_get_property(prom_stdout, "current-speed", NULL);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100548 if (spd)
549 speed = *spd;
550
551 if (0)
552 ;
553#ifdef CONFIG_SERIAL_8250_CONSOLE
554 else if (strcmp(name, "serial") == 0) {
555 int i;
556 /* Look for it in probed array */
557 for (i = 0; i < legacy_serial_count; i++) {
558 if (prom_stdout != legacy_serial_infos[i].np)
559 continue;
560 offset = i;
561 speed = legacy_serial_infos[i].speed;
562 break;
563 }
564 if (i >= legacy_serial_count)
565 goto not_found;
566 }
567#endif /* CONFIG_SERIAL_8250_CONSOLE */
568#ifdef CONFIG_SERIAL_PMACZILOG_CONSOLE
569 else if (strcmp(name, "ch-a") == 0)
570 offset = 0;
571 else if (strcmp(name, "ch-b") == 0)
572 offset = 1;
573#endif /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
574 else
575 goto not_found;
576 of_node_put(prom_stdout);
577
578 DBG("Found serial console at ttyS%d\n", offset);
579
580 if (speed) {
581 static char __initdata opt[16];
582 sprintf(opt, "%d", speed);
583 return add_preferred_console("ttyS", offset, opt);
584 } else
585 return add_preferred_console("ttyS", offset, NULL);
586
587 not_found:
588 DBG("No preferred console found !\n");
589 of_node_put(prom_stdout);
590 return -ENODEV;
591}
592console_initcall(check_legacy_serial_console);
593