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