blob: 662cd67428de599703c44637e08e4ef18e7b02e2 [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>
7#include <asm/io.h>
8#include <asm/mmu.h>
9#include <asm/prom.h>
10#include <asm/serial.h>
11#include <asm/udbg.h>
12#include <asm/pci-bridge.h>
13#include <asm/ppc-pci.h>
14
15#undef DEBUG
16
17#ifdef DEBUG
18#define DBG(fmt...) do { printk(fmt); } while(0)
19#else
20#define DBG(fmt...) do { } while(0)
21#endif
22
23#define MAX_LEGACY_SERIAL_PORTS 8
24
25static struct plat_serial8250_port
26legacy_serial_ports[MAX_LEGACY_SERIAL_PORTS+1];
27static struct legacy_serial_info {
28 struct device_node *np;
29 unsigned int speed;
30 unsigned int clock;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100031 int irq_check_parent;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110032 phys_addr_t taddr;
33} legacy_serial_infos[MAX_LEGACY_SERIAL_PORTS];
34static unsigned int legacy_serial_count;
35static int legacy_serial_console = -1;
36
37static int __init add_legacy_port(struct device_node *np, int want_index,
38 int iotype, phys_addr_t base,
Kumar Galab580d462005-12-20 16:16:52 -060039 phys_addr_t taddr, unsigned long irq,
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100040 upf_t flags, int irq_check_parent)
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110041{
Jeremy Kerra7f67bd2006-07-12 15:35:54 +100042 const u32 *clk, *spd;
43 u32 clock = BASE_BAUD * 16;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110044 int index;
45
46 /* get clock freq. if present */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +100047 clk = of_get_property(np, "clock-frequency", NULL);
David Woodhouse31df1672005-11-24 17:08:56 +000048 if (clk && *clk)
49 clock = *clk;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110050
51 /* get default speed if present */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +100052 spd = of_get_property(np, "current-speed", NULL);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110053
54 /* If we have a location index, then try to use it */
55 if (want_index >= 0 && want_index < MAX_LEGACY_SERIAL_PORTS)
56 index = want_index;
57 else
58 index = legacy_serial_count;
59
60 /* if our index is still out of range, that mean that
61 * array is full, we could scan for a free slot but that
62 * make little sense to bother, just skip the port
63 */
64 if (index >= MAX_LEGACY_SERIAL_PORTS)
65 return -1;
66 if (index >= legacy_serial_count)
67 legacy_serial_count = index + 1;
68
69 /* Check if there is a port who already claimed our slot */
70 if (legacy_serial_infos[index].np != 0) {
71 /* if we still have some room, move it, else override */
72 if (legacy_serial_count < MAX_LEGACY_SERIAL_PORTS) {
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100073 printk(KERN_DEBUG "Moved legacy port %d -> %d\n",
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110074 index, legacy_serial_count);
75 legacy_serial_ports[legacy_serial_count] =
76 legacy_serial_ports[index];
77 legacy_serial_infos[legacy_serial_count] =
78 legacy_serial_infos[index];
79 legacy_serial_count++;
80 } else {
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +100081 printk(KERN_DEBUG "Replacing legacy port %d\n", index);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110082 }
83 }
84
85 /* Now fill the entry */
86 memset(&legacy_serial_ports[index], 0,
87 sizeof(struct plat_serial8250_port));
88 if (iotype == UPIO_PORT)
89 legacy_serial_ports[index].iobase = base;
90 else
Benjamin Herrenschmidt8dacaed2005-11-29 11:21:59 +110091 legacy_serial_ports[index].mapbase = base;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110092 legacy_serial_ports[index].iotype = iotype;
93 legacy_serial_ports[index].uartclk = clock;
94 legacy_serial_ports[index].irq = irq;
Kumar Galab580d462005-12-20 16:16:52 -060095 legacy_serial_ports[index].flags = flags;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110096 legacy_serial_infos[index].taddr = taddr;
97 legacy_serial_infos[index].np = of_node_get(np);
98 legacy_serial_infos[index].clock = clock;
99 legacy_serial_infos[index].speed = spd ? *spd : 0;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000100 legacy_serial_infos[index].irq_check_parent = irq_check_parent;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100101
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000102 printk(KERN_DEBUG "Found legacy serial port %d for %s\n",
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100103 index, np->full_name);
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000104 printk(KERN_DEBUG " %s=%llx, taddr=%llx, irq=%lx, clk=%d, speed=%d\n",
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100105 (iotype == UPIO_PORT) ? "port" : "mem",
106 (unsigned long long)base, (unsigned long long)taddr, irq,
107 legacy_serial_ports[index].uartclk,
108 legacy_serial_infos[index].speed);
109
110 return index;
111}
112
Kumar Galab580d462005-12-20 16:16:52 -0600113static int __init add_legacy_soc_port(struct device_node *np,
114 struct device_node *soc_dev)
115{
Benjamin Herrenschmidtf704b8d2006-07-04 14:14:07 +1000116 u64 addr;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000117 const u32 *addrp;
Stephen Rothwellaf308372006-03-23 17:38:10 +1100118 upf_t flags = UPF_BOOT_AUTOCONF | UPF_SKIP_TEST | UPF_SHARE_IRQ;
Zang Roy-r61911be9633e2006-08-23 10:20:27 +0800119 struct device_node *tsi = of_get_parent(np);
Kumar Galab580d462005-12-20 16:16:52 -0600120
121 /* We only support ports that have a clock frequency properly
122 * encoded in the device-tree.
123 */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000124 if (of_get_property(np, "clock-frequency", NULL) == NULL)
Kumar Galab580d462005-12-20 16:16:52 -0600125 return -1;
126
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100127 /* if rtas uses this device, don't try to use it as well */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000128 if (of_get_property(np, "used-by-rtas", NULL) != NULL)
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100129 return -1;
130
Kumar Galab580d462005-12-20 16:16:52 -0600131 /* Get the address */
132 addrp = of_get_address(soc_dev, 0, NULL, NULL);
133 if (addrp == NULL)
134 return -1;
135
136 addr = of_translate_address(soc_dev, addrp);
Benjamin Herrenschmidt7c6efda2006-07-03 17:24:15 +1000137 if (addr == OF_BAD_ADDR)
138 return -1;
Kumar Galab580d462005-12-20 16:16:52 -0600139
140 /* Add port, irq will be dealt with later. We passed a translated
141 * IO port value. It will be fixed up later along with the irq
142 */
Zang Roy-r61911be9633e2006-08-23 10:20:27 +0800143 if (tsi && !strcmp(tsi->type, "tsi-bridge"))
144 return add_legacy_port(np, -1, UPIO_TSI, addr, addr, NO_IRQ, flags, 0);
145 else
146 return add_legacy_port(np, -1, UPIO_MEM, addr, addr, NO_IRQ, flags, 0);
Kumar Galab580d462005-12-20 16:16:52 -0600147}
148
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100149static int __init add_legacy_isa_port(struct device_node *np,
Kumar Gala017e0fa2006-01-03 16:15:21 -0600150 struct device_node *isa_brg)
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100151{
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000152 const u32 *reg;
153 const char *typep;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100154 int index = -1;
Benjamin Herrenschmidtf704b8d2006-07-04 14:14:07 +1000155 u64 taddr;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100156
Benjamin Herrenschmidt7c6efda2006-07-03 17:24:15 +1000157 DBG(" -> add_legacy_isa_port(%s)\n", np->full_name);
158
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100159 /* Get the ISA port number */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000160 reg = of_get_property(np, "reg", NULL);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100161 if (reg == NULL)
162 return -1;
163
164 /* Verify it's an IO port, we don't support anything else */
165 if (!(reg[0] & 0x00000001))
166 return -1;
167
168 /* Now look for an "ibm,aix-loc" property that gives us ordering
169 * if any...
170 */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000171 typep = of_get_property(np, "ibm,aix-loc", NULL);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100172
173 /* If we have a location index, then use it */
174 if (typep && *typep == 'S')
175 index = simple_strtol(typep+1, NULL, 0) - 1;
176
Benjamin Herrenschmidtf704b8d2006-07-04 14:14:07 +1000177 /* Translate ISA address. If it fails, we still register the port
178 * with no translated address so that it can be picked up as an IO
179 * port later by the serial driver
180 */
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100181 taddr = of_translate_address(np, reg);
Benjamin Herrenschmidt7c6efda2006-07-03 17:24:15 +1000182 if (taddr == OF_BAD_ADDR)
Benjamin Herrenschmidtf704b8d2006-07-04 14:14:07 +1000183 taddr = 0;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100184
185 /* Add port, irq will be dealt with later */
Benjamin Herrenschmidt7c6efda2006-07-03 17:24:15 +1000186 return add_legacy_port(np, index, UPIO_PORT, reg[1], taddr,
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000187 NO_IRQ, UPF_BOOT_AUTOCONF, 0);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100188
189}
190
Kumar Gala017e0fa2006-01-03 16:15:21 -0600191#ifdef CONFIG_PCI
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100192static int __init add_legacy_pci_port(struct device_node *np,
193 struct device_node *pci_dev)
194{
Benjamin Herrenschmidtf704b8d2006-07-04 14:14:07 +1000195 u64 addr, base;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000196 const u32 *addrp;
Benjamin Herrenschmidtd2dd4822005-11-30 16:57:28 +1100197 unsigned int flags;
Benjamin Herrenschmidt8dacaed2005-11-29 11:21:59 +1100198 int iotype, index = -1, lindex = 0;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100199
Benjamin Herrenschmidt7c6efda2006-07-03 17:24:15 +1000200 DBG(" -> add_legacy_pci_port(%s)\n", np->full_name);
201
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100202 /* We only support ports that have a clock frequency properly
203 * encoded in the device-tree (that is have an fcode). Anything
204 * else can't be used that early and will be normally probed by
Benjamin Herrenschmidt8dacaed2005-11-29 11:21:59 +1100205 * the generic 8250_pci driver later on. The reason is that 8250
206 * compatible UARTs on PCI need all sort of quirks (port offsets
207 * etc...) that this code doesn't know about
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100208 */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000209 if (of_get_property(np, "clock-frequency", NULL) == NULL)
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100210 return -1;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100211
212 /* Get the PCI address. Assume BAR 0 */
Benjamin Herrenschmidtd2dd4822005-11-30 16:57:28 +1100213 addrp = of_get_pci_address(pci_dev, 0, NULL, &flags);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100214 if (addrp == NULL)
215 return -1;
216
217 /* We only support BAR 0 for now */
Benjamin Herrenschmidtd2dd4822005-11-30 16:57:28 +1100218 iotype = (flags & IORESOURCE_MEM) ? UPIO_MEM : UPIO_PORT;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100219 addr = of_translate_address(pci_dev, addrp);
Benjamin Herrenschmidt7c6efda2006-07-03 17:24:15 +1000220 if (addr == OF_BAD_ADDR)
221 return -1;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100222
223 /* Set the IO base to the same as the translated address for MMIO,
224 * or to the domain local IO base for PIO (it will be fixed up later)
225 */
226 if (iotype == UPIO_MEM)
227 base = addr;
228 else
229 base = addrp[2];
230
231 /* Try to guess an index... If we have subdevices of the pci dev,
232 * we get to their "reg" property
233 */
234 if (np != pci_dev) {
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000235 const u32 *reg = of_get_property(np, "reg", NULL);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100236 if (reg && (*reg < 4))
Benjamin Herrenschmidt8dacaed2005-11-29 11:21:59 +1100237 index = lindex = *reg;
238 }
239
240 /* Local index means it's the Nth port in the PCI chip. Unfortunately
241 * the offset to add here is device specific. We know about those
242 * EXAR ports and we default to the most common case. If your UART
243 * doesn't work for these settings, you'll have to add your own special
244 * cases here
245 */
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000246 if (of_device_is_compatible(pci_dev, "pci13a8,152") ||
247 of_device_is_compatible(pci_dev, "pci13a8,154") ||
248 of_device_is_compatible(pci_dev, "pci13a8,158")) {
Benjamin Herrenschmidt8dacaed2005-11-29 11:21:59 +1100249 addr += 0x200 * lindex;
250 base += 0x200 * lindex;
251 } else {
252 addr += 8 * lindex;
253 base += 8 * lindex;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100254 }
255
256 /* Add port, irq will be dealt with later. We passed a translated
257 * IO port value. It will be fixed up later along with the irq
258 */
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000259 return add_legacy_port(np, index, iotype, base, addr, NO_IRQ,
260 UPF_BOOT_AUTOCONF, np != pci_dev);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100261}
Kumar Gala017e0fa2006-01-03 16:15:21 -0600262#endif
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100263
Michael Neuling37a801c2006-03-26 10:07:35 +1000264static void __init setup_legacy_serial_console(int console)
265{
266 struct legacy_serial_info *info =
267 &legacy_serial_infos[console];
268 void __iomem *addr;
269
270 if (info->taddr == 0)
271 return;
272 addr = ioremap(info->taddr, 0x1000);
273 if (addr == NULL)
274 return;
275 if (info->speed == 0)
276 info->speed = udbg_probe_uart_speed(addr, info->clock);
277 DBG("default console speed = %d\n", info->speed);
278 udbg_init_uart(addr, info->speed, info->clock);
279}
280
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100281/*
282 * This is called very early, as part of setup_system() or eventually
283 * setup_arch(), basically before anything else in this file. This function
284 * will try to build a list of all the available 8250-compatible serial ports
285 * in the machine using the Open Firmware device-tree. It currently only deals
286 * with ISA and PCI busses but could be extended. It allows a very early boot
287 * console to be initialized, that list is also used later to provide 8250 with
288 * the machine non-PCI ports and to properly pick the default console port
289 */
290void __init find_legacy_serial_ports(void)
291{
Kumar Galab580d462005-12-20 16:16:52 -0600292 struct device_node *np, *stdout = NULL;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000293 const char *path;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100294 int index;
295
296 DBG(" -> find_legacy_serial_port()\n");
297
298 /* Now find out if one of these is out firmware console */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000299 path = of_get_property(of_chosen, "linux,stdout-path", NULL);
Kumar Galab580d462005-12-20 16:16:52 -0600300 if (path != NULL) {
301 stdout = of_find_node_by_path(path);
302 if (stdout)
303 DBG("stdout is %s\n", stdout->full_name);
304 } else {
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100305 DBG(" no linux,stdout-path !\n");
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100306 }
Kumar Galab580d462005-12-20 16:16:52 -0600307
308 /* First fill our array with SOC ports */
309 for (np = NULL; (np = of_find_compatible_node(np, "serial", "ns16550")) != NULL;) {
310 struct device_node *soc = of_get_parent(np);
311 if (soc && !strcmp(soc->type, "soc")) {
312 index = add_legacy_soc_port(np, np);
313 if (index >= 0 && np == stdout)
314 legacy_serial_console = index;
315 }
316 of_node_put(soc);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100317 }
318
319 /* First fill our array with ISA ports */
320 for (np = NULL; (np = of_find_node_by_type(np, "serial"));) {
321 struct device_node *isa = of_get_parent(np);
322 if (isa && !strcmp(isa->name, "isa")) {
323 index = add_legacy_isa_port(np, isa);
324 if (index >= 0 && np == stdout)
325 legacy_serial_console = index;
326 }
327 of_node_put(isa);
328 }
329
Zang Roy-r61911c5d56332006-06-13 15:07:15 +0800330 /* First fill our array with tsi-bridge ports */
331 for (np = NULL; (np = of_find_compatible_node(np, "serial", "ns16550")) != NULL;) {
332 struct device_node *tsi = of_get_parent(np);
333 if (tsi && !strcmp(tsi->type, "tsi-bridge")) {
334 index = add_legacy_soc_port(np, np);
335 if (index >= 0 && np == stdout)
336 legacy_serial_console = index;
337 }
338 of_node_put(tsi);
339 }
340
Arnd Bergmann8d38a5b2007-02-13 21:35:38 +0100341 /* First fill our array with opb bus ports */
342 for (np = NULL; (np = of_find_compatible_node(np, "serial", "ns16750")) != NULL;) {
343 struct device_node *opb = of_get_parent(np);
344 if (opb && !strcmp(opb->type, "opb")) {
345 index = add_legacy_soc_port(np, np);
346 if (index >= 0 && np == stdout)
347 legacy_serial_console = index;
348 }
349 of_node_put(opb);
350 }
351
Kumar Gala017e0fa2006-01-03 16:15:21 -0600352#ifdef CONFIG_PCI
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100353 /* Next, try to locate PCI ports */
354 for (np = NULL; (np = of_find_all_nodes(np));) {
355 struct device_node *pci, *parent = of_get_parent(np);
356 if (parent && !strcmp(parent->name, "isa")) {
357 of_node_put(parent);
358 continue;
359 }
360 if (strcmp(np->name, "serial") && strcmp(np->type, "serial")) {
361 of_node_put(parent);
362 continue;
363 }
364 /* Check for known pciclass, and also check wether we have
365 * a device with child nodes for ports or not
366 */
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000367 if (of_device_is_compatible(np, "pciclass,0700") ||
368 of_device_is_compatible(np, "pciclass,070002"))
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100369 pci = np;
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000370 else if (of_device_is_compatible(parent, "pciclass,0700") ||
371 of_device_is_compatible(parent, "pciclass,070002"))
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100372 pci = parent;
373 else {
374 of_node_put(parent);
375 continue;
376 }
377 index = add_legacy_pci_port(np, pci);
378 if (index >= 0 && np == stdout)
379 legacy_serial_console = index;
380 of_node_put(parent);
381 }
Kumar Gala017e0fa2006-01-03 16:15:21 -0600382#endif
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100383
384 DBG("legacy_serial_console = %d\n", legacy_serial_console);
Michael Neuling37a801c2006-03-26 10:07:35 +1000385 if (legacy_serial_console >= 0)
386 setup_legacy_serial_console(legacy_serial_console);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100387 DBG(" <- find_legacy_serial_port()\n");
388}
389
390static struct platform_device serial_device = {
391 .name = "serial8250",
392 .id = PLAT8250_DEV_PLATFORM,
393 .dev = {
394 .platform_data = legacy_serial_ports,
395 },
396};
397
398static void __init fixup_port_irq(int index,
399 struct device_node *np,
400 struct plat_serial8250_port *port)
401{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000402 unsigned int virq;
403
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100404 DBG("fixup_port_irq(%d)\n", index);
405
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000406 virq = irq_of_parse_and_map(np, 0);
407 if (virq == NO_IRQ && legacy_serial_infos[index].irq_check_parent) {
408 np = of_get_parent(np);
409 if (np == NULL)
410 return;
411 virq = irq_of_parse_and_map(np, 0);
412 of_node_put(np);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100413 }
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000414 if (virq == NO_IRQ)
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100415 return;
416
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000417 port->irq = virq;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100418}
419
420static void __init fixup_port_pio(int index,
421 struct device_node *np,
422 struct plat_serial8250_port *port)
423{
Kumar Gala017e0fa2006-01-03 16:15:21 -0600424#ifdef CONFIG_PCI
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100425 struct pci_controller *hose;
426
427 DBG("fixup_port_pio(%d)\n", index);
428
429 hose = pci_find_hose_for_OF_device(np);
430 if (hose) {
431 unsigned long offset = (unsigned long)hose->io_base_virt -
432#ifdef CONFIG_PPC64
433 pci_io_base;
434#else
435 isa_io_base;
436#endif
437 DBG("port %d, IO %lx -> %lx\n",
438 index, port->iobase, port->iobase + offset);
439 port->iobase += offset;
440 }
Kumar Gala017e0fa2006-01-03 16:15:21 -0600441#endif
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100442}
443
Benjamin Herrenschmidt8dacaed2005-11-29 11:21:59 +1100444static void __init fixup_port_mmio(int index,
445 struct device_node *np,
446 struct plat_serial8250_port *port)
447{
448 DBG("fixup_port_mmio(%d)\n", index);
449
450 port->membase = ioremap(port->mapbase, 0x100);
451}
452
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100453/*
454 * This is called as an arch initcall, hopefully before the PCI bus is
455 * probed and/or the 8250 driver loaded since we need to register our
456 * platform devices before 8250 PCI ones are detected as some of them
457 * must properly "override" the platform ones.
458 *
459 * This function fixes up the interrupt value for platform ports as it
460 * couldn't be done earlier before interrupt maps have been parsed. It
461 * also "corrects" the IO address for PIO ports for the same reason,
462 * since earlier, the PHBs virtual IO space wasn't assigned yet. It then
463 * registers all those platform ports for use by the 8250 driver when it
464 * finally loads.
465 */
466static int __init serial_dev_init(void)
467{
468 int i;
469
470 if (legacy_serial_count == 0)
471 return -ENODEV;
472
473 /*
474 * Before we register the platfrom serial devices, we need
475 * to fixup their interrutps and their IO ports.
476 */
477 DBG("Fixing serial ports interrupts and IO ports ...\n");
478
479 for (i = 0; i < legacy_serial_count; i++) {
480 struct plat_serial8250_port *port = &legacy_serial_ports[i];
481 struct device_node *np = legacy_serial_infos[i].np;
482
483 if (port->irq == NO_IRQ)
484 fixup_port_irq(i, np, port);
485 if (port->iotype == UPIO_PORT)
486 fixup_port_pio(i, np, port);
Zang Roy-r61911be9633e2006-08-23 10:20:27 +0800487 if ((port->iotype == UPIO_MEM) || (port->iotype == UPIO_TSI))
Benjamin Herrenschmidt8dacaed2005-11-29 11:21:59 +1100488 fixup_port_mmio(i, np, port);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100489 }
490
491 DBG("Registering platform serial ports\n");
492
493 return platform_device_register(&serial_device);
494}
495arch_initcall(serial_dev_init);
496
497
498/*
499 * This is called very early, as part of console_init() (typically just after
500 * time_init()). This function is respondible for trying to find a good
501 * default console on serial ports. It tries to match the open firmware
502 * default output with one of the available serial console drivers, either
503 * one of the platform serial ports that have been probed earlier by
504 * find_legacy_serial_ports() or some more platform specific ones.
505 */
506static int __init check_legacy_serial_console(void)
507{
508 struct device_node *prom_stdout = NULL;
509 int speed = 0, offset = 0;
Jeremy Kerra7f67bd2006-07-12 15:35:54 +1000510 const char *name;
511 const u32 *spd;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100512
513 DBG(" -> check_legacy_serial_console()\n");
514
515 /* The user has requested a console so this is already set up. */
Alon Bar-Levb8757b22007-02-12 00:54:17 -0800516 if (strstr(boot_command_line, "console=")) {
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100517 DBG(" console was specified !\n");
518 return -EBUSY;
519 }
520
521 if (!of_chosen) {
522 DBG(" of_chosen is NULL !\n");
523 return -ENODEV;
524 }
Kumar Galab580d462005-12-20 16:16:52 -0600525
526 if (legacy_serial_console < 0) {
527 DBG(" legacy_serial_console not found !\n");
528 return -ENODEV;
529 }
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100530 /* We are getting a weird phandle from OF ... */
531 /* ... So use the full path instead */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000532 name = of_get_property(of_chosen, "linux,stdout-path", NULL);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100533 if (name == NULL) {
534 DBG(" no linux,stdout-path !\n");
535 return -ENODEV;
536 }
537 prom_stdout = of_find_node_by_path(name);
538 if (!prom_stdout) {
539 DBG(" can't find stdout package %s !\n", name);
540 return -ENODEV;
541 }
542 DBG("stdout is %s\n", prom_stdout->full_name);
543
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000544 name = of_get_property(prom_stdout, "name", NULL);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100545 if (!name) {
546 DBG(" stdout package has no name !\n");
547 goto not_found;
548 }
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000549 spd = of_get_property(prom_stdout, "current-speed", NULL);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100550 if (spd)
551 speed = *spd;
552
553 if (0)
554 ;
555#ifdef CONFIG_SERIAL_8250_CONSOLE
556 else if (strcmp(name, "serial") == 0) {
557 int i;
558 /* Look for it in probed array */
559 for (i = 0; i < legacy_serial_count; i++) {
560 if (prom_stdout != legacy_serial_infos[i].np)
561 continue;
562 offset = i;
563 speed = legacy_serial_infos[i].speed;
564 break;
565 }
566 if (i >= legacy_serial_count)
567 goto not_found;
568 }
569#endif /* CONFIG_SERIAL_8250_CONSOLE */
570#ifdef CONFIG_SERIAL_PMACZILOG_CONSOLE
571 else if (strcmp(name, "ch-a") == 0)
572 offset = 0;
573 else if (strcmp(name, "ch-b") == 0)
574 offset = 1;
575#endif /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
576 else
577 goto not_found;
578 of_node_put(prom_stdout);
579
580 DBG("Found serial console at ttyS%d\n", offset);
581
582 if (speed) {
583 static char __initdata opt[16];
584 sprintf(opt, "%d", speed);
585 return add_preferred_console("ttyS", offset, opt);
586 } else
587 return add_preferred_console("ttyS", offset, NULL);
588
589 not_found:
590 DBG("No preferred console found !\n");
591 of_node_put(prom_stdout);
592 return -ENODEV;
593}
594console_initcall(check_legacy_serial_console);
595