Linux 2.6.28-rc5
[cris-mirror.git] / arch / powerpc / platforms / iseries / dt.c
blob4543c4bc3a56e158cb47a3387d97c715dabc515e
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 dt_prop_u64(struct iseries_flat_dt *dt, const char *name,
208 u64 data)
210 dt_prop(dt, name, &data, sizeof(u64));
213 static void __init dt_prop_u64_list(struct iseries_flat_dt *dt,
214 const char *name, u64 *data, int n)
216 dt_prop(dt, name, data, sizeof(u64) * n);
219 static void __init dt_prop_u32_list(struct iseries_flat_dt *dt,
220 const char *name, u32 *data, int n)
222 dt_prop(dt, name, data, sizeof(u32) * n);
225 #ifdef notyet
226 static void __init dt_prop_empty(struct iseries_flat_dt *dt, const char *name)
228 dt_prop(dt, name, NULL, 0);
230 #endif
232 static void __init dt_cpus(struct iseries_flat_dt *dt)
234 unsigned char buf[32];
235 unsigned char *p;
236 unsigned int i, index;
237 struct IoHriProcessorVpd *d;
238 u32 pft_size[2];
240 /* yuck */
241 snprintf(buf, 32, "PowerPC,%s", cur_cpu_spec->cpu_name);
242 p = strchr(buf, ' ');
243 if (!p) p = buf + strlen(buf);
245 dt_start_node(dt, "cpus");
246 dt_prop_u32(dt, "#address-cells", 1);
247 dt_prop_u32(dt, "#size-cells", 0);
249 pft_size[0] = 0; /* NUMA CEC cookie, 0 for non NUMA */
250 pft_size[1] = __ilog2(HvCallHpt_getHptPages() * HW_PAGE_SIZE);
252 for (i = 0; i < NR_CPUS; i++) {
253 if (lppaca[i].dyn_proc_status >= 2)
254 continue;
256 snprintf(p, 32 - (p - buf), "@%d", i);
257 dt_start_node(dt, buf);
259 dt_prop_str(dt, "device_type", device_type_cpu);
261 index = lppaca[i].dyn_hv_phys_proc_index;
262 d = &xIoHriProcessorVpd[index];
264 dt_prop_u32(dt, "i-cache-size", d->xInstCacheSize * 1024);
265 dt_prop_u32(dt, "i-cache-line-size", d->xInstCacheOperandSize);
267 dt_prop_u32(dt, "d-cache-size", d->xDataL1CacheSizeKB * 1024);
268 dt_prop_u32(dt, "d-cache-line-size", d->xDataCacheOperandSize);
270 /* magic conversions to Hz copied from old code */
271 dt_prop_u32(dt, "clock-frequency",
272 ((1UL << 34) * 1000000) / d->xProcFreq);
273 dt_prop_u32(dt, "timebase-frequency",
274 ((1UL << 32) * 1000000) / d->xTimeBaseFreq);
276 dt_prop_u32(dt, "reg", i);
278 dt_prop_u32_list(dt, "ibm,pft-size", pft_size, 2);
280 dt_end_node(dt);
283 dt_end_node(dt);
286 static void __init dt_model(struct iseries_flat_dt *dt)
288 char buf[16] = "IBM,";
290 /* N.B. lparcfg.c knows about the "IBM," prefixes ... */
291 /* "IBM," + mfgId[2:3] + systemSerial[1:5] */
292 strne2a(buf + 4, xItExtVpdPanel.mfgID + 2, 2);
293 strne2a(buf + 6, xItExtVpdPanel.systemSerial + 1, 5);
294 buf[11] = '\0';
295 dt_prop_str(dt, "system-id", buf);
297 /* "IBM," + machineType[0:4] */
298 strne2a(buf + 4, xItExtVpdPanel.machineType, 4);
299 buf[8] = '\0';
300 dt_prop_str(dt, "model", buf);
302 dt_prop_str(dt, "compatible", "IBM,iSeries");
303 dt_prop_u32(dt, "ibm,partition-no", HvLpConfig_getLpIndex());
306 static void __init dt_initrd(struct iseries_flat_dt *dt)
308 #ifdef CONFIG_BLK_DEV_INITRD
309 if (naca.xRamDisk) {
310 dt_prop_u64(dt, "linux,initrd-start", (u64)naca.xRamDisk);
311 dt_prop_u64(dt, "linux,initrd-end",
312 (u64)naca.xRamDisk + naca.xRamDiskSize * HW_PAGE_SIZE);
314 #endif
317 static void __init dt_do_vdevice(struct iseries_flat_dt *dt,
318 const char *name, u32 reg, int unit,
319 const char *type, const char *compat, int end)
321 char buf[32];
323 snprintf(buf, 32, "%s@%08x", name, reg + ((unit >= 0) ? unit : 0));
324 dt_start_node(dt, buf);
325 dt_prop_str(dt, "device_type", type);
326 if (compat)
327 dt_prop_str(dt, "compatible", compat);
328 dt_prop_u32(dt, "reg", reg + ((unit >= 0) ? unit : 0));
329 if (unit >= 0)
330 dt_prop_u32(dt, "linux,unit_address", unit);
331 if (end)
332 dt_end_node(dt);
335 static void __init dt_vdevices(struct iseries_flat_dt *dt)
337 u32 reg = 0;
338 HvLpIndexMap vlan_map;
339 int i;
341 dt_start_node(dt, "vdevice");
342 dt_prop_str(dt, "device_type", device_type_vdevice);
343 dt_prop_str(dt, "compatible", "IBM,iSeries-vdevice");
344 dt_prop_u32(dt, "#address-cells", 1);
345 dt_prop_u32(dt, "#size-cells", 0);
347 dt_do_vdevice(dt, "vty", reg, -1, device_type_serial,
348 "IBM,iSeries-vty", 1);
349 reg++;
351 dt_do_vdevice(dt, "v-scsi", reg, -1, device_type_vscsi,
352 "IBM,v-scsi", 1);
353 reg++;
355 vlan_map = HvLpConfig_getVirtualLanIndexMap();
356 for (i = 0; i < HVMAXARCHITECTEDVIRTUALLANS; i++) {
357 unsigned char mac_addr[ETH_ALEN];
359 if ((vlan_map & (0x8000 >> i)) == 0)
360 continue;
361 dt_do_vdevice(dt, "l-lan", reg, i, device_type_network,
362 "IBM,iSeries-l-lan", 0);
363 mac_addr[0] = 0x02;
364 mac_addr[1] = 0x01;
365 mac_addr[2] = 0xff;
366 mac_addr[3] = i;
367 mac_addr[4] = 0xff;
368 mac_addr[5] = HvLpConfig_getLpIndex_outline();
369 dt_prop(dt, "local-mac-address", (char *)mac_addr, ETH_ALEN);
370 dt_prop(dt, "mac-address", (char *)mac_addr, ETH_ALEN);
371 dt_prop_u32(dt, "max-frame-size", 9000);
372 dt_prop_u32(dt, "address-bits", 48);
374 dt_end_node(dt);
377 dt_end_node(dt);
380 struct pci_class_name {
381 u16 code;
382 const char *name;
383 const char *type;
386 static struct pci_class_name __initdata pci_class_name[] = {
387 { PCI_CLASS_NETWORK_ETHERNET, "ethernet", device_type_network },
390 static struct pci_class_name * __init dt_find_pci_class_name(u16 class_code)
392 struct pci_class_name *cp;
394 for (cp = pci_class_name;
395 cp < &pci_class_name[ARRAY_SIZE(pci_class_name)]; cp++)
396 if (cp->code == class_code)
397 return cp;
398 return NULL;
402 * This assumes that the node slot is always on the primary bus!
404 static void __init scan_bridge_slot(struct iseries_flat_dt *dt,
405 HvBusNumber bus, struct HvCallPci_BridgeInfo *bridge_info)
407 HvSubBusNumber sub_bus = bridge_info->subBusNumber;
408 u16 vendor_id;
409 u16 device_id;
410 u32 class_id;
411 int err;
412 char buf[32];
413 u32 reg[5];
414 int id_sel = ISERIES_GET_DEVICE_FROM_SUBBUS(sub_bus);
415 int function = ISERIES_GET_FUNCTION_FROM_SUBBUS(sub_bus);
416 HvAgentId eads_id_sel = ISERIES_PCI_AGENTID(id_sel, function);
417 u8 devfn;
418 struct pci_class_name *cp;
421 * Connect all functions of any device found.
423 for (id_sel = 1; id_sel <= bridge_info->maxAgents; id_sel++) {
424 for (function = 0; function < 8; function++) {
425 HvAgentId agent_id = ISERIES_PCI_AGENTID(id_sel,
426 function);
427 err = HvCallXm_connectBusUnit(bus, sub_bus,
428 agent_id, 0);
429 if (err) {
430 if (err != 0x302)
431 DBG("connectBusUnit(%x, %x, %x) %x\n",
432 bus, sub_bus, agent_id, err);
433 continue;
436 err = HvCallPci_configLoad16(bus, sub_bus, agent_id,
437 PCI_VENDOR_ID, &vendor_id);
438 if (err) {
439 DBG("ReadVendor(%x, %x, %x) %x\n",
440 bus, sub_bus, agent_id, err);
441 continue;
443 err = HvCallPci_configLoad16(bus, sub_bus, agent_id,
444 PCI_DEVICE_ID, &device_id);
445 if (err) {
446 DBG("ReadDevice(%x, %x, %x) %x\n",
447 bus, sub_bus, agent_id, err);
448 continue;
450 err = HvCallPci_configLoad32(bus, sub_bus, agent_id,
451 PCI_CLASS_REVISION , &class_id);
452 if (err) {
453 DBG("ReadClass(%x, %x, %x) %x\n",
454 bus, sub_bus, agent_id, err);
455 continue;
458 devfn = PCI_DEVFN(ISERIES_ENCODE_DEVICE(eads_id_sel),
459 function);
460 cp = dt_find_pci_class_name(class_id >> 16);
461 if (cp && cp->name)
462 strncpy(buf, cp->name, sizeof(buf) - 1);
463 else
464 snprintf(buf, sizeof(buf), "pci%x,%x",
465 vendor_id, device_id);
466 buf[sizeof(buf) - 1] = '\0';
467 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
468 "@%x", PCI_SLOT(devfn));
469 buf[sizeof(buf) - 1] = '\0';
470 if (function != 0)
471 snprintf(buf + strlen(buf),
472 sizeof(buf) - strlen(buf),
473 ",%x", function);
474 dt_start_node(dt, buf);
475 reg[0] = (bus << 16) | (devfn << 8);
476 reg[1] = 0;
477 reg[2] = 0;
478 reg[3] = 0;
479 reg[4] = 0;
480 dt_prop_u32_list(dt, "reg", reg, 5);
481 if (cp && (cp->type || cp->name))
482 dt_prop_str(dt, "device_type",
483 cp->type ? cp->type : cp->name);
484 dt_prop_u32(dt, "vendor-id", vendor_id);
485 dt_prop_u32(dt, "device-id", device_id);
486 dt_prop_u32(dt, "class-code", class_id >> 8);
487 dt_prop_u32(dt, "revision-id", class_id & 0xff);
488 dt_prop_u32(dt, "linux,subbus", sub_bus);
489 dt_prop_u32(dt, "linux,agent-id", agent_id);
490 dt_prop_u32(dt, "linux,logical-slot-number",
491 bridge_info->logicalSlotNumber);
492 dt_end_node(dt);
498 static void __init scan_bridge(struct iseries_flat_dt *dt, HvBusNumber bus,
499 HvSubBusNumber sub_bus, int id_sel)
501 struct HvCallPci_BridgeInfo bridge_info;
502 HvAgentId agent_id;
503 int function;
504 int ret;
506 /* Note: hvSubBus and irq is always be 0 at this level! */
507 for (function = 0; function < 8; ++function) {
508 agent_id = ISERIES_PCI_AGENTID(id_sel, function);
509 ret = HvCallXm_connectBusUnit(bus, sub_bus, agent_id, 0);
510 if (ret != 0) {
511 if (ret != 0xb)
512 DBG("connectBusUnit(%x, %x, %x) %x\n",
513 bus, sub_bus, agent_id, ret);
514 continue;
516 DBG("found device at bus %d idsel %d func %d (AgentId %x)\n",
517 bus, id_sel, function, agent_id);
518 ret = HvCallPci_getBusUnitInfo(bus, sub_bus, agent_id,
519 iseries_hv_addr(&bridge_info),
520 sizeof(struct HvCallPci_BridgeInfo));
521 if (ret != 0)
522 continue;
523 DBG("bridge info: type %x subbus %x "
524 "maxAgents %x maxsubbus %x logslot %x\n",
525 bridge_info.busUnitInfo.deviceType,
526 bridge_info.subBusNumber,
527 bridge_info.maxAgents,
528 bridge_info.maxSubBusNumber,
529 bridge_info.logicalSlotNumber);
530 if (bridge_info.busUnitInfo.deviceType ==
531 HvCallPci_BridgeDevice)
532 scan_bridge_slot(dt, bus, &bridge_info);
533 else
534 DBG("PCI: Invalid Bridge Configuration(0x%02X)",
535 bridge_info.busUnitInfo.deviceType);
539 static void __init scan_phb(struct iseries_flat_dt *dt, HvBusNumber bus)
541 struct HvCallPci_DeviceInfo dev_info;
542 const HvSubBusNumber sub_bus = 0; /* EADs is always 0. */
543 int err;
544 int id_sel;
545 const int max_agents = 8;
548 * Probe for EADs Bridges
550 for (id_sel = 1; id_sel < max_agents; ++id_sel) {
551 err = HvCallPci_getDeviceInfo(bus, sub_bus, id_sel,
552 iseries_hv_addr(&dev_info),
553 sizeof(struct HvCallPci_DeviceInfo));
554 if (err) {
555 if (err != 0x302)
556 DBG("getDeviceInfo(%x, %x, %x) %x\n",
557 bus, sub_bus, id_sel, err);
558 continue;
560 if (dev_info.deviceType != HvCallPci_NodeDevice) {
561 DBG("PCI: Invalid System Configuration"
562 "(0x%02X) for bus 0x%02x id 0x%02x.\n",
563 dev_info.deviceType, bus, id_sel);
564 continue;
566 scan_bridge(dt, bus, sub_bus, id_sel);
570 static void __init dt_pci_devices(struct iseries_flat_dt *dt)
572 HvBusNumber bus;
573 char buf[32];
574 u32 buses[2];
575 int phb_num = 0;
577 /* Check all possible buses. */
578 for (bus = 0; bus < 256; bus++) {
579 int err = HvCallXm_testBus(bus);
581 if (err) {
583 * Check for Unexpected Return code, a clue that
584 * something has gone wrong.
586 if (err != 0x0301)
587 DBG("Unexpected Return on Probe(0x%02X) "
588 "0x%04X\n", bus, err);
589 continue;
591 DBG("bus %d appears to exist\n", bus);
592 snprintf(buf, 32, "pci@%d", phb_num);
593 dt_start_node(dt, buf);
594 dt_prop_str(dt, "device_type", device_type_pci);
595 dt_prop_str(dt, "compatible", "IBM,iSeries-Logical-PHB");
596 dt_prop_u32(dt, "#address-cells", 3);
597 dt_prop_u32(dt, "#size-cells", 2);
598 buses[0] = buses[1] = bus;
599 dt_prop_u32_list(dt, "bus-range", buses, 2);
600 scan_phb(dt, bus);
601 dt_end_node(dt);
602 phb_num++;
606 static void dt_finish(struct iseries_flat_dt *dt)
608 dt_push_u32(dt, OF_DT_END);
609 dt->header.totalsize = (unsigned long)dt_data - (unsigned long)dt;
610 klimit = ALIGN((unsigned long)dt_data, 8);
613 void * __init build_flat_dt(unsigned long phys_mem_size)
615 struct iseries_flat_dt *iseries_dt;
616 u64 tmp[2];
618 iseries_dt = dt_init();
620 dt_start_node(iseries_dt, "");
622 dt_prop_u32(iseries_dt, "#address-cells", 2);
623 dt_prop_u32(iseries_dt, "#size-cells", 2);
624 dt_model(iseries_dt);
626 /* /memory */
627 dt_start_node(iseries_dt, "memory@0");
628 dt_prop_str(iseries_dt, "device_type", device_type_memory);
629 tmp[0] = 0;
630 tmp[1] = phys_mem_size;
631 dt_prop_u64_list(iseries_dt, "reg", tmp, 2);
632 dt_end_node(iseries_dt);
634 /* /chosen */
635 dt_start_node(iseries_dt, "chosen");
636 dt_prop_str(iseries_dt, "bootargs", cmd_line);
637 dt_initrd(iseries_dt);
638 dt_end_node(iseries_dt);
640 dt_cpus(iseries_dt);
642 dt_vdevices(iseries_dt);
643 dt_pci_devices(iseries_dt);
645 dt_end_node(iseries_dt);
647 dt_finish(iseries_dt);
649 return iseries_dt;