Full support for Ginger Console
[linux-ginger.git] / arch / powerpc / platforms / iseries / dt.c
blobc5a87a72057b42b700fe1a514795185e12b31f80
1 /*
2 * Copyright (C) 2005-2006 Michael Ellerman, IBM Corporation
3 * Copyright (C) 2000-2004, IBM Corporation
5 * Description:
6 * This file contains all the routines to build a flattened device
7 * tree for a legacy iSeries machine.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
15 #undef DEBUG
17 #include <linux/types.h>
18 #include <linux/init.h>
19 #include <linux/pci.h>
20 #include <linux/pci_regs.h>
21 #include <linux/pci_ids.h>
22 #include <linux/threads.h>
23 #include <linux/bitops.h>
24 #include <linux/string.h>
25 #include <linux/kernel.h>
26 #include <linux/if_ether.h> /* ETH_ALEN */
28 #include <asm/machdep.h>
29 #include <asm/prom.h>
30 #include <asm/lppaca.h>
31 #include <asm/cputable.h>
32 #include <asm/abs_addr.h>
33 #include <asm/system.h>
34 #include <asm/iseries/hv_types.h>
35 #include <asm/iseries/hv_lp_config.h>
36 #include <asm/iseries/hv_call_xm.h>
37 #include <asm/udbg.h>
39 #include "processor_vpd.h"
40 #include "call_hpt.h"
41 #include "call_pci.h"
42 #include "pci.h"
43 #include "it_exp_vpd_panel.h"
44 #include "naca.h"
46 #ifdef DEBUG
47 #define DBG(fmt...) udbg_printf(fmt)
48 #else
49 #define DBG(fmt...)
50 #endif
53 * These are created by the linker script at the start and end
54 * of the section containing all the strings from this file.
56 extern char __dt_strings_start[];
57 extern char __dt_strings_end[];
59 struct iseries_flat_dt {
60 struct boot_param_header header;
61 u64 reserve_map[2];
64 static void * __initdata dt_data;
67 * Putting these strings here keeps them out of the section
68 * that we rename to .dt_strings using objcopy and capture
69 * for the strings blob of the flattened device tree.
71 static char __initdata device_type_cpu[] = "cpu";
72 static char __initdata device_type_memory[] = "memory";
73 static char __initdata device_type_serial[] = "serial";
74 static char __initdata device_type_network[] = "network";
75 static char __initdata device_type_pci[] = "pci";
76 static char __initdata device_type_vdevice[] = "vdevice";
77 static char __initdata device_type_vscsi[] = "vscsi";
80 /* EBCDIC to ASCII conversion routines */
82 static unsigned char __init e2a(unsigned char x)
84 switch (x) {
85 case 0x81 ... 0x89:
86 return x - 0x81 + 'a';
87 case 0x91 ... 0x99:
88 return x - 0x91 + 'j';
89 case 0xA2 ... 0xA9:
90 return x - 0xA2 + 's';
91 case 0xC1 ... 0xC9:
92 return x - 0xC1 + 'A';
93 case 0xD1 ... 0xD9:
94 return x - 0xD1 + 'J';
95 case 0xE2 ... 0xE9:
96 return x - 0xE2 + 'S';
97 case 0xF0 ... 0xF9:
98 return x - 0xF0 + '0';
100 return ' ';
103 static unsigned char * __init strne2a(unsigned char *dest,
104 const unsigned char *src, size_t n)
106 int i;
108 n = strnlen(src, n);
110 for (i = 0; i < n; i++)
111 dest[i] = e2a(src[i]);
113 return dest;
116 static struct iseries_flat_dt * __init dt_init(void)
118 struct iseries_flat_dt *dt;
119 unsigned long str_len;
121 str_len = __dt_strings_end - __dt_strings_start;
122 dt = (struct iseries_flat_dt *)ALIGN(klimit, 8);
123 dt->header.off_mem_rsvmap =
124 offsetof(struct iseries_flat_dt, reserve_map);
125 dt->header.off_dt_strings = ALIGN(sizeof(*dt), 8);
126 dt->header.off_dt_struct = dt->header.off_dt_strings
127 + ALIGN(str_len, 8);
128 dt_data = (void *)((unsigned long)dt + dt->header.off_dt_struct);
129 dt->header.dt_strings_size = str_len;
131 /* There is no notion of hardware cpu id on iSeries */
132 dt->header.boot_cpuid_phys = smp_processor_id();
134 memcpy((char *)dt + dt->header.off_dt_strings, __dt_strings_start,
135 str_len);
137 dt->header.magic = OF_DT_HEADER;
138 dt->header.version = 0x10;
139 dt->header.last_comp_version = 0x10;
141 dt->reserve_map[0] = 0;
142 dt->reserve_map[1] = 0;
144 return dt;
147 static void __init dt_push_u32(struct iseries_flat_dt *dt, u32 value)
149 *((u32 *)dt_data) = value;
150 dt_data += sizeof(u32);
153 #ifdef notyet
154 static void __init dt_push_u64(struct iseries_flat_dt *dt, u64 value)
156 *((u64 *)dt_data) = value;
157 dt_data += sizeof(u64);
159 #endif
161 static void __init dt_push_bytes(struct iseries_flat_dt *dt, const char *data,
162 int len)
164 memcpy(dt_data, data, len);
165 dt_data += ALIGN(len, 4);
168 static void __init dt_start_node(struct iseries_flat_dt *dt, const char *name)
170 dt_push_u32(dt, OF_DT_BEGIN_NODE);
171 dt_push_bytes(dt, name, strlen(name) + 1);
174 #define dt_end_node(dt) dt_push_u32(dt, OF_DT_END_NODE)
176 static void __init dt_prop(struct iseries_flat_dt *dt, const char *name,
177 const void *data, int len)
179 unsigned long offset;
181 dt_push_u32(dt, OF_DT_PROP);
183 /* Length of the data */
184 dt_push_u32(dt, len);
186 offset = name - __dt_strings_start;
188 /* The offset of the properties name in the string blob. */
189 dt_push_u32(dt, (u32)offset);
191 /* The actual data. */
192 dt_push_bytes(dt, data, len);
195 static void __init dt_prop_str(struct iseries_flat_dt *dt, const char *name,
196 const char *data)
198 dt_prop(dt, name, data, strlen(data) + 1); /* + 1 for NULL */
201 static void __init dt_prop_u32(struct iseries_flat_dt *dt, const char *name,
202 u32 data)
204 dt_prop(dt, name, &data, sizeof(u32));
207 static void __init __maybe_unused dt_prop_u64(struct iseries_flat_dt *dt,
208 const char *name,
209 u64 data)
211 dt_prop(dt, name, &data, sizeof(u64));
214 static void __init dt_prop_u64_list(struct iseries_flat_dt *dt,
215 const char *name, u64 *data, int n)
217 dt_prop(dt, name, data, sizeof(u64) * n);
220 static void __init dt_prop_u32_list(struct iseries_flat_dt *dt,
221 const char *name, u32 *data, int n)
223 dt_prop(dt, name, data, sizeof(u32) * n);
226 #ifdef notyet
227 static void __init dt_prop_empty(struct iseries_flat_dt *dt, const char *name)
229 dt_prop(dt, name, NULL, 0);
231 #endif
233 static void __init dt_cpus(struct iseries_flat_dt *dt)
235 unsigned char buf[32];
236 unsigned char *p;
237 unsigned int i, index;
238 struct IoHriProcessorVpd *d;
239 u32 pft_size[2];
241 /* yuck */
242 snprintf(buf, 32, "PowerPC,%s", cur_cpu_spec->cpu_name);
243 p = strchr(buf, ' ');
244 if (!p) p = buf + strlen(buf);
246 dt_start_node(dt, "cpus");
247 dt_prop_u32(dt, "#address-cells", 1);
248 dt_prop_u32(dt, "#size-cells", 0);
250 pft_size[0] = 0; /* NUMA CEC cookie, 0 for non NUMA */
251 pft_size[1] = __ilog2(HvCallHpt_getHptPages() * HW_PAGE_SIZE);
253 for (i = 0; i < NR_CPUS; i++) {
254 if (lppaca[i].dyn_proc_status >= 2)
255 continue;
257 snprintf(p, 32 - (p - buf), "@%d", i);
258 dt_start_node(dt, buf);
260 dt_prop_str(dt, "device_type", device_type_cpu);
262 index = lppaca[i].dyn_hv_phys_proc_index;
263 d = &xIoHriProcessorVpd[index];
265 dt_prop_u32(dt, "i-cache-size", d->xInstCacheSize * 1024);
266 dt_prop_u32(dt, "i-cache-line-size", d->xInstCacheOperandSize);
268 dt_prop_u32(dt, "d-cache-size", d->xDataL1CacheSizeKB * 1024);
269 dt_prop_u32(dt, "d-cache-line-size", d->xDataCacheOperandSize);
271 /* magic conversions to Hz copied from old code */
272 dt_prop_u32(dt, "clock-frequency",
273 ((1UL << 34) * 1000000) / d->xProcFreq);
274 dt_prop_u32(dt, "timebase-frequency",
275 ((1UL << 32) * 1000000) / d->xTimeBaseFreq);
277 dt_prop_u32(dt, "reg", i);
279 dt_prop_u32_list(dt, "ibm,pft-size", pft_size, 2);
281 dt_end_node(dt);
284 dt_end_node(dt);
287 static void __init dt_model(struct iseries_flat_dt *dt)
289 char buf[16] = "IBM,";
291 /* N.B. lparcfg.c knows about the "IBM," prefixes ... */
292 /* "IBM," + mfgId[2:3] + systemSerial[1:5] */
293 strne2a(buf + 4, xItExtVpdPanel.mfgID + 2, 2);
294 strne2a(buf + 6, xItExtVpdPanel.systemSerial + 1, 5);
295 buf[11] = '\0';
296 dt_prop_str(dt, "system-id", buf);
298 /* "IBM," + machineType[0:4] */
299 strne2a(buf + 4, xItExtVpdPanel.machineType, 4);
300 buf[8] = '\0';
301 dt_prop_str(dt, "model", buf);
303 dt_prop_str(dt, "compatible", "IBM,iSeries");
304 dt_prop_u32(dt, "ibm,partition-no", HvLpConfig_getLpIndex());
307 static void __init dt_initrd(struct iseries_flat_dt *dt)
309 #ifdef CONFIG_BLK_DEV_INITRD
310 if (naca.xRamDisk) {
311 dt_prop_u64(dt, "linux,initrd-start", (u64)naca.xRamDisk);
312 dt_prop_u64(dt, "linux,initrd-end",
313 (u64)naca.xRamDisk + naca.xRamDiskSize * HW_PAGE_SIZE);
315 #endif
318 static void __init dt_do_vdevice(struct iseries_flat_dt *dt,
319 const char *name, u32 reg, int unit,
320 const char *type, const char *compat, int end)
322 char buf[32];
324 snprintf(buf, 32, "%s@%08x", name, reg + ((unit >= 0) ? unit : 0));
325 dt_start_node(dt, buf);
326 dt_prop_str(dt, "device_type", type);
327 if (compat)
328 dt_prop_str(dt, "compatible", compat);
329 dt_prop_u32(dt, "reg", reg + ((unit >= 0) ? unit : 0));
330 if (unit >= 0)
331 dt_prop_u32(dt, "linux,unit_address", unit);
332 if (end)
333 dt_end_node(dt);
336 static void __init dt_vdevices(struct iseries_flat_dt *dt)
338 u32 reg = 0;
339 HvLpIndexMap vlan_map;
340 int i;
342 dt_start_node(dt, "vdevice");
343 dt_prop_str(dt, "device_type", device_type_vdevice);
344 dt_prop_str(dt, "compatible", "IBM,iSeries-vdevice");
345 dt_prop_u32(dt, "#address-cells", 1);
346 dt_prop_u32(dt, "#size-cells", 0);
348 dt_do_vdevice(dt, "vty", reg, -1, device_type_serial,
349 "IBM,iSeries-vty", 1);
350 reg++;
352 dt_do_vdevice(dt, "v-scsi", reg, -1, device_type_vscsi,
353 "IBM,v-scsi", 1);
354 reg++;
356 vlan_map = HvLpConfig_getVirtualLanIndexMap();
357 for (i = 0; i < HVMAXARCHITECTEDVIRTUALLANS; i++) {
358 unsigned char mac_addr[ETH_ALEN];
360 if ((vlan_map & (0x8000 >> i)) == 0)
361 continue;
362 dt_do_vdevice(dt, "l-lan", reg, i, device_type_network,
363 "IBM,iSeries-l-lan", 0);
364 mac_addr[0] = 0x02;
365 mac_addr[1] = 0x01;
366 mac_addr[2] = 0xff;
367 mac_addr[3] = i;
368 mac_addr[4] = 0xff;
369 mac_addr[5] = HvLpConfig_getLpIndex_outline();
370 dt_prop(dt, "local-mac-address", (char *)mac_addr, ETH_ALEN);
371 dt_prop(dt, "mac-address", (char *)mac_addr, ETH_ALEN);
372 dt_prop_u32(dt, "max-frame-size", 9000);
373 dt_prop_u32(dt, "address-bits", 48);
375 dt_end_node(dt);
378 dt_end_node(dt);
381 struct pci_class_name {
382 u16 code;
383 const char *name;
384 const char *type;
387 static struct pci_class_name __initdata pci_class_name[] = {
388 { PCI_CLASS_NETWORK_ETHERNET, "ethernet", device_type_network },
391 static struct pci_class_name * __init dt_find_pci_class_name(u16 class_code)
393 struct pci_class_name *cp;
395 for (cp = pci_class_name;
396 cp < &pci_class_name[ARRAY_SIZE(pci_class_name)]; cp++)
397 if (cp->code == class_code)
398 return cp;
399 return NULL;
403 * This assumes that the node slot is always on the primary bus!
405 static void __init scan_bridge_slot(struct iseries_flat_dt *dt,
406 HvBusNumber bus, struct HvCallPci_BridgeInfo *bridge_info)
408 HvSubBusNumber sub_bus = bridge_info->subBusNumber;
409 u16 vendor_id;
410 u16 device_id;
411 u32 class_id;
412 int err;
413 char buf[32];
414 u32 reg[5];
415 int id_sel = ISERIES_GET_DEVICE_FROM_SUBBUS(sub_bus);
416 int function = ISERIES_GET_FUNCTION_FROM_SUBBUS(sub_bus);
417 HvAgentId eads_id_sel = ISERIES_PCI_AGENTID(id_sel, function);
418 u8 devfn;
419 struct pci_class_name *cp;
422 * Connect all functions of any device found.
424 for (id_sel = 1; id_sel <= bridge_info->maxAgents; id_sel++) {
425 for (function = 0; function < 8; function++) {
426 HvAgentId agent_id = ISERIES_PCI_AGENTID(id_sel,
427 function);
428 err = HvCallXm_connectBusUnit(bus, sub_bus,
429 agent_id, 0);
430 if (err) {
431 if (err != 0x302)
432 DBG("connectBusUnit(%x, %x, %x) %x\n",
433 bus, sub_bus, agent_id, err);
434 continue;
437 err = HvCallPci_configLoad16(bus, sub_bus, agent_id,
438 PCI_VENDOR_ID, &vendor_id);
439 if (err) {
440 DBG("ReadVendor(%x, %x, %x) %x\n",
441 bus, sub_bus, agent_id, err);
442 continue;
444 err = HvCallPci_configLoad16(bus, sub_bus, agent_id,
445 PCI_DEVICE_ID, &device_id);
446 if (err) {
447 DBG("ReadDevice(%x, %x, %x) %x\n",
448 bus, sub_bus, agent_id, err);
449 continue;
451 err = HvCallPci_configLoad32(bus, sub_bus, agent_id,
452 PCI_CLASS_REVISION , &class_id);
453 if (err) {
454 DBG("ReadClass(%x, %x, %x) %x\n",
455 bus, sub_bus, agent_id, err);
456 continue;
459 devfn = PCI_DEVFN(ISERIES_ENCODE_DEVICE(eads_id_sel),
460 function);
461 cp = dt_find_pci_class_name(class_id >> 16);
462 if (cp && cp->name)
463 strncpy(buf, cp->name, sizeof(buf) - 1);
464 else
465 snprintf(buf, sizeof(buf), "pci%x,%x",
466 vendor_id, device_id);
467 buf[sizeof(buf) - 1] = '\0';
468 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
469 "@%x", PCI_SLOT(devfn));
470 buf[sizeof(buf) - 1] = '\0';
471 if (function != 0)
472 snprintf(buf + strlen(buf),
473 sizeof(buf) - strlen(buf),
474 ",%x", function);
475 dt_start_node(dt, buf);
476 reg[0] = (bus << 16) | (devfn << 8);
477 reg[1] = 0;
478 reg[2] = 0;
479 reg[3] = 0;
480 reg[4] = 0;
481 dt_prop_u32_list(dt, "reg", reg, 5);
482 if (cp && (cp->type || cp->name))
483 dt_prop_str(dt, "device_type",
484 cp->type ? cp->type : cp->name);
485 dt_prop_u32(dt, "vendor-id", vendor_id);
486 dt_prop_u32(dt, "device-id", device_id);
487 dt_prop_u32(dt, "class-code", class_id >> 8);
488 dt_prop_u32(dt, "revision-id", class_id & 0xff);
489 dt_prop_u32(dt, "linux,subbus", sub_bus);
490 dt_prop_u32(dt, "linux,agent-id", agent_id);
491 dt_prop_u32(dt, "linux,logical-slot-number",
492 bridge_info->logicalSlotNumber);
493 dt_end_node(dt);
499 static void __init scan_bridge(struct iseries_flat_dt *dt, HvBusNumber bus,
500 HvSubBusNumber sub_bus, int id_sel)
502 struct HvCallPci_BridgeInfo bridge_info;
503 HvAgentId agent_id;
504 int function;
505 int ret;
507 /* Note: hvSubBus and irq is always be 0 at this level! */
508 for (function = 0; function < 8; ++function) {
509 agent_id = ISERIES_PCI_AGENTID(id_sel, function);
510 ret = HvCallXm_connectBusUnit(bus, sub_bus, agent_id, 0);
511 if (ret != 0) {
512 if (ret != 0xb)
513 DBG("connectBusUnit(%x, %x, %x) %x\n",
514 bus, sub_bus, agent_id, ret);
515 continue;
517 DBG("found device at bus %d idsel %d func %d (AgentId %x)\n",
518 bus, id_sel, function, agent_id);
519 ret = HvCallPci_getBusUnitInfo(bus, sub_bus, agent_id,
520 iseries_hv_addr(&bridge_info),
521 sizeof(struct HvCallPci_BridgeInfo));
522 if (ret != 0)
523 continue;
524 DBG("bridge info: type %x subbus %x "
525 "maxAgents %x maxsubbus %x logslot %x\n",
526 bridge_info.busUnitInfo.deviceType,
527 bridge_info.subBusNumber,
528 bridge_info.maxAgents,
529 bridge_info.maxSubBusNumber,
530 bridge_info.logicalSlotNumber);
531 if (bridge_info.busUnitInfo.deviceType ==
532 HvCallPci_BridgeDevice)
533 scan_bridge_slot(dt, bus, &bridge_info);
534 else
535 DBG("PCI: Invalid Bridge Configuration(0x%02X)",
536 bridge_info.busUnitInfo.deviceType);
540 static void __init scan_phb(struct iseries_flat_dt *dt, HvBusNumber bus)
542 struct HvCallPci_DeviceInfo dev_info;
543 const HvSubBusNumber sub_bus = 0; /* EADs is always 0. */
544 int err;
545 int id_sel;
546 const int max_agents = 8;
549 * Probe for EADs Bridges
551 for (id_sel = 1; id_sel < max_agents; ++id_sel) {
552 err = HvCallPci_getDeviceInfo(bus, sub_bus, id_sel,
553 iseries_hv_addr(&dev_info),
554 sizeof(struct HvCallPci_DeviceInfo));
555 if (err) {
556 if (err != 0x302)
557 DBG("getDeviceInfo(%x, %x, %x) %x\n",
558 bus, sub_bus, id_sel, err);
559 continue;
561 if (dev_info.deviceType != HvCallPci_NodeDevice) {
562 DBG("PCI: Invalid System Configuration"
563 "(0x%02X) for bus 0x%02x id 0x%02x.\n",
564 dev_info.deviceType, bus, id_sel);
565 continue;
567 scan_bridge(dt, bus, sub_bus, id_sel);
571 static void __init dt_pci_devices(struct iseries_flat_dt *dt)
573 HvBusNumber bus;
574 char buf[32];
575 u32 buses[2];
576 int phb_num = 0;
578 /* Check all possible buses. */
579 for (bus = 0; bus < 256; bus++) {
580 int err = HvCallXm_testBus(bus);
582 if (err) {
584 * Check for Unexpected Return code, a clue that
585 * something has gone wrong.
587 if (err != 0x0301)
588 DBG("Unexpected Return on Probe(0x%02X) "
589 "0x%04X\n", bus, err);
590 continue;
592 DBG("bus %d appears to exist\n", bus);
593 snprintf(buf, 32, "pci@%d", phb_num);
594 dt_start_node(dt, buf);
595 dt_prop_str(dt, "device_type", device_type_pci);
596 dt_prop_str(dt, "compatible", "IBM,iSeries-Logical-PHB");
597 dt_prop_u32(dt, "#address-cells", 3);
598 dt_prop_u32(dt, "#size-cells", 2);
599 buses[0] = buses[1] = bus;
600 dt_prop_u32_list(dt, "bus-range", buses, 2);
601 scan_phb(dt, bus);
602 dt_end_node(dt);
603 phb_num++;
607 static void dt_finish(struct iseries_flat_dt *dt)
609 dt_push_u32(dt, OF_DT_END);
610 dt->header.totalsize = (unsigned long)dt_data - (unsigned long)dt;
611 klimit = ALIGN((unsigned long)dt_data, 8);
614 void * __init build_flat_dt(unsigned long phys_mem_size)
616 struct iseries_flat_dt *iseries_dt;
617 u64 tmp[2];
619 iseries_dt = dt_init();
621 dt_start_node(iseries_dt, "");
623 dt_prop_u32(iseries_dt, "#address-cells", 2);
624 dt_prop_u32(iseries_dt, "#size-cells", 2);
625 dt_model(iseries_dt);
627 /* /memory */
628 dt_start_node(iseries_dt, "memory@0");
629 dt_prop_str(iseries_dt, "device_type", device_type_memory);
630 tmp[0] = 0;
631 tmp[1] = phys_mem_size;
632 dt_prop_u64_list(iseries_dt, "reg", tmp, 2);
633 dt_end_node(iseries_dt);
635 /* /chosen */
636 dt_start_node(iseries_dt, "chosen");
637 dt_prop_str(iseries_dt, "bootargs", cmd_line);
638 dt_initrd(iseries_dt);
639 dt_end_node(iseries_dt);
641 dt_cpus(iseries_dt);
643 dt_vdevices(iseries_dt);
644 dt_pci_devices(iseries_dt);
646 dt_end_node(iseries_dt);
648 dt_finish(iseries_dt);
650 return iseries_dt;