2 * arch/ppc/platforms/pmac_feature.c
4 * Copyright (C) 1996-2001 Paul Mackerras (paulus@cs.anu.edu.au)
5 * Ben. Herrenschmidt (benh@kernel.crashing.org)
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
14 * - Replace mdelay with some schedule loop if possible
15 * - Shorten some obfuscated delays on some routines (like modem
17 * - Refcount some clocks (see darwin)
18 * - Split split split...
21 #include <linux/config.h>
22 #include <linux/types.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/kernel.h>
26 #include <linux/sched.h>
27 #include <linux/spinlock.h>
28 #include <linux/adb.h>
29 #include <linux/pmu.h>
30 #include <linux/ioport.h>
31 #include <linux/pci.h>
32 #include <asm/sections.h>
33 #include <asm/errno.h>
34 #include <asm/keylargo.h>
35 #include <asm/uninorth.h>
38 #include <asm/machdep.h>
39 #include <asm/pmac_feature.h>
40 #include <asm/dbdma.h>
41 #include <asm/pci-bridge.h>
42 #include <asm/pmac_low_i2c.h>
47 #define DBG(fmt...) printk(KERN_DEBUG fmt)
53 * We use a single global lock to protect accesses. Each driver has
54 * to take care of its own locking
56 static DEFINE_SPINLOCK(feature_lock __pmacdata);
58 #define LOCK(flags) spin_lock_irqsave(&feature_lock, flags);
59 #define UNLOCK(flags) spin_unlock_irqrestore(&feature_lock, flags);
63 * Instance of some macio stuffs
65 struct macio_chip macio_chips[MAX_MACIO_CHIPS] __pmacdata;
67 struct macio_chip* __pmac
68 macio_find(struct device_node* child, int type)
73 for (i=0; i < MAX_MACIO_CHIPS && macio_chips[i].of_node; i++)
74 if (child == macio_chips[i].of_node &&
75 (!type || macio_chips[i].type == type))
76 return &macio_chips[i];
77 child = child->parent;
82 static const char* macio_names[] __pmacdata =
100 * Uninorth reg. access. Note that Uni-N regs are big endian
103 #define UN_REG(r) (uninorth_base + ((r) >> 2))
104 #define UN_IN(r) (in_be32(UN_REG(r)))
105 #define UN_OUT(r,v) (out_be32(UN_REG(r), (v)))
106 #define UN_BIS(r,v) (UN_OUT((r), UN_IN(r) | (v)))
107 #define UN_BIC(r,v) (UN_OUT((r), UN_IN(r) & ~(v)))
109 static struct device_node* uninorth_node __pmacdata;
110 static u32* uninorth_base __pmacdata;
111 static u32 uninorth_rev __pmacdata;
114 extern struct device_node *k2_skiplist[2];
117 * For each motherboard family, we have a table of functions pointers
118 * that handle the various features.
121 typedef long (*feature_call)(struct device_node* node, long param, long value);
123 struct feature_table_entry {
124 unsigned int selector;
125 feature_call function;
130 const char* model_string;
131 const char* model_name;
133 struct feature_table_entry* features;
134 unsigned long board_flags;
136 static struct pmac_mb_def pmac_mb __pmacdata;
139 * Here are the chip specific feature functions
143 static long __pmac g5_read_gpio(struct device_node* node, long param, long value)
145 struct macio_chip* macio = &macio_chips[0];
147 return MACIO_IN8(param);
151 static long __pmac g5_write_gpio(struct device_node* node, long param, long value)
153 struct macio_chip* macio = &macio_chips[0];
155 MACIO_OUT8(param, (u8)(value & 0xff));
159 static long __pmac g5_gmac_enable(struct device_node* node, long param, long value)
161 struct macio_chip* macio = &macio_chips[0];
169 MACIO_BIS(KEYLARGO_FCR1, K2_FCR1_GMAC_CLK_ENABLE);
171 k2_skiplist[0] = NULL;
173 k2_skiplist[0] = node;
175 MACIO_BIC(KEYLARGO_FCR1, K2_FCR1_GMAC_CLK_ENABLE);
184 static long __pmac g5_fw_enable(struct device_node* node, long param, long value)
186 struct macio_chip* macio = &macio_chips[0];
194 MACIO_BIS(KEYLARGO_FCR1, K2_FCR1_FW_CLK_ENABLE);
196 k2_skiplist[1] = NULL;
198 k2_skiplist[1] = node;
200 MACIO_BIC(KEYLARGO_FCR1, K2_FCR1_FW_CLK_ENABLE);
209 static long __pmac g5_mpic_enable(struct device_node* node, long param, long value)
213 if (node->parent == NULL || strcmp(node->parent->name, "u3"))
217 UN_BIS(U3_TOGGLE_REG, U3_MPIC_RESET | U3_MPIC_OUTPUT_ENABLE);
224 static long __pmac g5_reset_cpu(struct device_node* node, long param, long value)
226 unsigned int reset_io = 0;
228 struct macio_chip* macio;
229 struct device_node* np;
231 macio = &macio_chips[0];
232 if (macio->type != macio_keylargo2)
235 np = find_path_device("/cpus");
238 for (np = np->child; np != NULL; np = np->sibling) {
239 u32* num = (u32 *)get_property(np, "reg", NULL);
240 u32* rst = (u32 *)get_property(np, "soft-reset", NULL);
241 if (num == NULL || rst == NULL)
248 if (np == NULL || reset_io == 0)
252 MACIO_OUT8(reset_io, KEYLARGO_GPIO_OUTPUT_ENABLE);
253 (void)MACIO_IN8(reset_io);
255 MACIO_OUT8(reset_io, 0);
256 (void)MACIO_IN8(reset_io);
261 #endif /* CONFIG_SMP */
264 * This can be called from pmac_smp so isn't static
266 * This takes the second CPU off the bus on dual CPU machines
269 void __pmac g5_phy_disable_cpu1(void)
271 UN_OUT(U3_API_PHY_CONFIG_1, 0);
274 static long __pmac generic_get_mb_info(struct device_node* node, long param, long value)
277 case PMAC_MB_INFO_MODEL:
278 return pmac_mb.model_id;
279 case PMAC_MB_INFO_FLAGS:
280 return pmac_mb.board_flags;
281 case PMAC_MB_INFO_NAME:
282 /* hack hack hack... but should work */
283 *((const char **)value) = pmac_mb.model_name;
294 /* Used on any machine
296 static struct feature_table_entry any_features[] __pmacdata = {
297 { PMAC_FTR_GET_MB_INFO, generic_get_mb_info },
303 static struct feature_table_entry g5_features[] __pmacdata = {
304 { PMAC_FTR_GMAC_ENABLE, g5_gmac_enable },
305 { PMAC_FTR_1394_ENABLE, g5_fw_enable },
306 { PMAC_FTR_ENABLE_MPIC, g5_mpic_enable },
307 { PMAC_FTR_READ_GPIO, g5_read_gpio },
308 { PMAC_FTR_WRITE_GPIO, g5_write_gpio },
310 { PMAC_FTR_RESET_CPU, g5_reset_cpu },
311 #endif /* CONFIG_SMP */
315 static struct pmac_mb_def pmac_mb_defs[] __pmacdata = {
316 { "PowerMac7,2", "PowerMac G5",
317 PMAC_TYPE_POWERMAC_G5, g5_features,
320 { "PowerMac7,3", "PowerMac G5",
321 PMAC_TYPE_POWERMAC_G5, g5_features,
324 { "RackMac3,1", "XServe G5",
325 PMAC_TYPE_POWERMAC_G5, g5_features,
331 * The toplevel feature_call callback
333 long __pmac pmac_do_feature_call(unsigned int selector, ...)
335 struct device_node* node;
338 feature_call func = NULL;
341 if (pmac_mb.features)
342 for (i=0; pmac_mb.features[i].function; i++)
343 if (pmac_mb.features[i].selector == selector) {
344 func = pmac_mb.features[i].function;
348 for (i=0; any_features[i].function; i++)
349 if (any_features[i].selector == selector) {
350 func = any_features[i].function;
356 va_start(args, selector);
357 node = (struct device_node*)va_arg(args, void*);
358 param = va_arg(args, long);
359 value = va_arg(args, long);
362 return func(node, param, value);
365 static int __init probe_motherboard(void)
368 struct macio_chip* macio = &macio_chips[0];
369 const char* model = NULL;
370 struct device_node *dt;
372 /* Lookup known motherboard type in device-tree. First try an
373 * exact match on the "model" property, then try a "compatible"
374 * match is none is found.
376 dt = find_devices("device-tree");
378 model = (const char *) get_property(dt, "model", NULL);
379 for(i=0; model && i<(sizeof(pmac_mb_defs)/sizeof(struct pmac_mb_def)); i++) {
380 if (strcmp(model, pmac_mb_defs[i].model_string) == 0) {
381 pmac_mb = pmac_mb_defs[i];
385 for(i=0; i<(sizeof(pmac_mb_defs)/sizeof(struct pmac_mb_def)); i++) {
386 if (machine_is_compatible(pmac_mb_defs[i].model_string)) {
387 pmac_mb = pmac_mb_defs[i];
392 /* Fallback to selection depending on mac-io chip type */
393 switch(macio->type) {
394 case macio_keylargo2:
395 pmac_mb.model_id = PMAC_TYPE_UNKNOWN_K2;
396 pmac_mb.model_name = "Unknown K2-based";
397 pmac_mb.features = g5_features;
403 /* Check for "mobile" machine */
404 if (model && (strncmp(model, "PowerBook", 9) == 0
405 || strncmp(model, "iBook", 5) == 0))
406 pmac_mb.board_flags |= PMAC_MB_MOBILE;
409 printk(KERN_INFO "PowerMac motherboard: %s\n", pmac_mb.model_name);
413 /* Initialize the Core99 UniNorth host bridge and memory controller
415 static void __init probe_uninorth(void)
417 uninorth_node = of_find_node_by_name(NULL, "u3");
418 if (uninorth_node && uninorth_node->n_addrs > 0) {
419 /* Small hack until I figure out if parsing in prom.c is correct. I should
420 * get rid of those pre-parsed junk anyway
422 unsigned long address = uninorth_node->addrs[0].address;
423 uninorth_base = ioremap(address, 0x40000);
424 uninorth_rev = in_be32(UN_REG(UNI_N_VERSION));
425 u3_ht = ioremap(address + U3_HT_CONFIG_BASE, 0x1000);
427 uninorth_node = NULL;
432 printk(KERN_INFO "Found U3 memory controller & host bridge, revision: %d\n",
434 printk(KERN_INFO "Mapped at 0x%08lx\n", (unsigned long)uninorth_base);
438 static void __init probe_one_macio(const char* name, const char* compat, int type)
440 struct device_node* node;
445 node = find_devices(name);
446 if (!node || !node->n_addrs)
450 if (device_is_compatible(node, compat))
456 for(i=0; i<MAX_MACIO_CHIPS; i++) {
457 if (!macio_chips[i].of_node)
459 if (macio_chips[i].of_node == node)
462 if (i >= MAX_MACIO_CHIPS) {
463 printk(KERN_ERR "pmac_feature: Please increase MAX_MACIO_CHIPS !\n");
464 printk(KERN_ERR "pmac_feature: %s skipped\n", node->full_name);
467 base = (volatile u32*)ioremap(node->addrs[0].address, node->addrs[0].size);
469 printk(KERN_ERR "pmac_feature: Can't map mac-io chip !\n");
472 if (type == macio_keylargo) {
473 u32* did = (u32 *)get_property(node, "device-id", NULL);
474 if (*did == 0x00000025)
476 if (*did == 0x0000003e)
477 type = macio_intrepid;
479 macio_chips[i].of_node = node;
480 macio_chips[i].type = type;
481 macio_chips[i].base = base;
482 macio_chips[i].flags = MACIO_FLAG_SCCB_ON | MACIO_FLAG_SCCB_ON;
483 macio_chips[i].name = macio_names[type];
484 revp = (u32 *)get_property(node, "revision-id", NULL);
486 macio_chips[i].rev = *revp;
487 printk(KERN_INFO "Found a %s mac-io controller, rev: %d, mapped at 0x%p\n",
488 macio_names[type], macio_chips[i].rev, macio_chips[i].base);
494 probe_one_macio("mac-io", "K2-Keylargo", macio_keylargo2);
496 macio_chips[0].lbus.index = 0;
497 macio_chips[1].lbus.index = 1;
499 return (macio_chips[0].of_node == NULL) ? -ENODEV : 0;
503 set_initial_features(void)
505 struct device_node *np;
507 if (macio_chips[0].type == macio_keylargo2) {
509 /* On SMP machines running UP, we have the second CPU eating
510 * bus cycles. We need to take it off the bus. This is done
511 * from pmac_smp for SMP kernels running on one CPU
513 np = of_find_node_by_type(NULL, "cpu");
515 np = of_find_node_by_type(np, "cpu");
517 g5_phy_disable_cpu1();
520 #endif /* CONFIG_SMP */
521 /* Enable GMAC for now for PCI probing. It will be disabled
522 * later on after PCI probe
524 np = of_find_node_by_name(NULL, "ethernet");
526 if (device_is_compatible(np, "K2-GMAC"))
527 g5_gmac_enable(np, 0, 1);
528 np = of_find_node_by_name(np, "ethernet");
531 /* Enable FW before PCI probe. Will be disabled later on
532 * Note: We should have a batter way to check that we are
533 * dealing with uninorth internal cell and not a PCI cell
534 * on the external PCI. The code below works though.
536 np = of_find_node_by_name(NULL, "firewire");
538 if (device_is_compatible(np, "pci106b,5811")) {
539 macio_chips[0].flags |= MACIO_FLAG_FW_SUPPORTED;
540 g5_fw_enable(np, 0, 1);
542 np = of_find_node_by_name(np, "firewire");
548 pmac_feature_init(void)
550 /* Detect the UniNorth memory controller */
553 /* Probe mac-io controllers */
554 if (probe_macios()) {
555 printk(KERN_WARNING "No mac-io chip found\n");
559 /* Setup low-level i2c stuffs */
562 /* Probe machine type */
563 if (probe_motherboard())
564 printk(KERN_WARNING "Unknown PowerMac !\n");
566 /* Set some initial features (turn off some chips that will
567 * be later turned on)
569 set_initial_features();
572 int __init pmac_feature_late_init(void)
575 struct device_node* np;
577 /* Request some resources late */
579 request_OF_resource(uninorth_node, 0, NULL);
580 np = find_devices("hammerhead");
582 request_OF_resource(np, 0, NULL);
583 np = find_devices("interrupt-controller");
585 request_OF_resource(np, 0, NULL);
590 device_initcall(pmac_feature_late_init);
593 static void dump_HT_speeds(char *name, u32 cfg, u32 frq)
595 int freqs[16] = { 200,300,400,500,600,800,1000,0,0,0,0,0,0,0,0,0 };
596 int bits[8] = { 8,16,0,32,2,4,0,0 };
597 int freq = (frq >> 8) & 0xf;
599 if (freqs[freq] == 0)
600 printk("%s: Unknown HT link frequency %x\n", name, freq);
602 printk("%s: %d MHz on main link, (%d in / %d out) bits width\n",
604 bits[(cfg >> 28) & 0x7], bits[(cfg >> 24) & 0x7]);
608 void __init pmac_check_ht_link(void)
610 #if 0 /* Disabled for now */
611 u32 ufreq, freq, ucfg, cfg;
612 struct device_node *pcix_node;
614 struct pci_controller *px_hose;
616 (void)in_be32(u3_ht + U3_HT_LINK_COMMAND);
617 ucfg = cfg = in_be32(u3_ht + U3_HT_LINK_CONFIG);
618 ufreq = freq = in_be32(u3_ht + U3_HT_LINK_FREQ);
619 dump_HT_speeds("U3 HyperTransport", cfg, freq);
621 pcix_node = of_find_compatible_node(NULL, "pci", "pci-x");
622 if (pcix_node == NULL) {
623 printk("No PCI-X bridge found\n");
626 px_hose = pcix_node->phb;
627 px_bus = pcix_node->busno;
628 px_devfn = pcix_node->devfn;
630 early_read_config_dword(px_hose, px_bus, px_devfn, 0xc4, &cfg);
631 early_read_config_dword(px_hose, px_bus, px_devfn, 0xcc, &freq);
632 dump_HT_speeds("PCI-X HT Uplink", cfg, freq);
633 early_read_config_dword(px_hose, px_bus, px_devfn, 0xc8, &cfg);
634 early_read_config_dword(px_hose, px_bus, px_devfn, 0xd0, &freq);
635 dump_HT_speeds("PCI-X HT Downlink", cfg, freq);