initial commit with v2.6.9
[linux-2.6.9-moxart.git] / arch / ppc64 / kernel / pmac_feature.c
blobc45ecbdb2836610ec6bfe0415a123043a0a928a2
1 /*
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.
12 * TODO:
14 * - Replace mdelay with some schedule loop if possible
15 * - Shorten some obfuscated delays on some routines (like modem
16 * power)
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>
36 #include <asm/io.h>
37 #include <asm/prom.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>
44 #undef DEBUG_FEATURE
46 #ifdef DEBUG_FEATURE
47 #define DBG(fmt...) printk(KERN_DEBUG fmt)
48 #else
49 #define DBG(fmt...)
50 #endif
53 * We use a single global lock to protect accesses. Each driver has
54 * to take care of its own locking
56 static spinlock_t feature_lock __pmacdata = SPIN_LOCK_UNLOCKED;
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)
70 while(child) {
71 int i;
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;
79 return NULL;
82 static const char* macio_names[] __pmacdata =
84 "Unknown",
85 "Grand Central",
86 "OHare",
87 "OHareII",
88 "Heathrow",
89 "Gatwick",
90 "Paddington",
91 "Keylargo",
92 "Pangea",
93 "Intrepid",
94 "K2"
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;
112 static void *u3_ht;
114 extern struct pci_dev *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;
128 struct pmac_mb_def
130 const char* model_string;
131 const char* model_name;
132 int model_id;
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));
156 return 0;
159 static long __pmac g5_gmac_enable(struct device_node* node, long param, long value)
161 struct macio_chip* macio = &macio_chips[0];
162 unsigned long flags;
163 struct pci_dev *pdev = NULL;
165 if (node == NULL)
166 return -ENODEV;
168 /* XXX FIXME: We should fix pci_device_from_OF_node here, and
169 * get to a real pci_dev or we'll get into trouble with PCI
170 * domains the day we get overlapping numbers (like if we ever
171 * decide to show the HT root.
172 * Note that we only get the slot when value is 0. This is called
173 * early during boot with value 1 to enable all devices, at which
174 * point, we don't yet have probed pci_find_slot, so it would fail
175 * to look for the slot at this point.
177 if (!value)
178 pdev = pci_find_slot(node->busno, node->devfn);
180 LOCK(flags);
181 if (value) {
182 MACIO_BIS(KEYLARGO_FCR1, K2_FCR1_GMAC_CLK_ENABLE);
183 mb();
184 k2_skiplist[0] = NULL;
185 } else {
186 k2_skiplist[0] = pdev;
187 mb();
188 MACIO_BIC(KEYLARGO_FCR1, K2_FCR1_GMAC_CLK_ENABLE);
191 UNLOCK(flags);
192 mdelay(1);
194 return 0;
197 static long __pmac g5_fw_enable(struct device_node* node, long param, long value)
199 struct macio_chip* macio = &macio_chips[0];
200 unsigned long flags;
201 struct pci_dev *pdev = NULL;
203 /* XXX FIXME: We should fix pci_device_from_OF_node here, and
204 * get to a real pci_dev or we'll get into trouble with PCI
205 * domains the day we get overlapping numbers (like if we ever
206 * decide to show the HT root
207 * Note that we only get the slot when value is 0. This is called
208 * early during boot with value 1 to enable all devices, at which
209 * point, we don't yet have probed pci_find_slot, so it would fail
210 * to look for the slot at this point.
212 if (node == NULL)
213 return -ENODEV;
215 if (!value)
216 pdev = pci_find_slot(node->busno, node->devfn);
218 LOCK(flags);
219 if (value) {
220 MACIO_BIS(KEYLARGO_FCR1, K2_FCR1_FW_CLK_ENABLE);
221 mb();
222 k2_skiplist[1] = NULL;
223 } else {
224 k2_skiplist[1] = pdev;
225 mb();
226 MACIO_BIC(KEYLARGO_FCR1, K2_FCR1_FW_CLK_ENABLE);
229 UNLOCK(flags);
230 mdelay(1);
232 return 0;
235 static long __pmac g5_mpic_enable(struct device_node* node, long param, long value)
237 unsigned long flags;
239 if (node->parent == NULL || strcmp(node->parent->name, "u3"))
240 return 0;
242 LOCK(flags);
243 UN_BIS(U3_TOGGLE_REG, U3_MPIC_RESET | U3_MPIC_OUTPUT_ENABLE);
244 UNLOCK(flags);
246 return 0;
249 #ifdef CONFIG_SMP
250 static long __pmac g5_reset_cpu(struct device_node* node, long param, long value)
252 unsigned int reset_io = 0;
253 unsigned long flags;
254 struct macio_chip* macio;
255 struct device_node* np;
257 macio = &macio_chips[0];
258 if (macio->type != macio_keylargo2)
259 return -ENODEV;
261 np = find_path_device("/cpus");
262 if (np == NULL)
263 return -ENODEV;
264 for (np = np->child; np != NULL; np = np->sibling) {
265 u32* num = (u32 *)get_property(np, "reg", NULL);
266 u32* rst = (u32 *)get_property(np, "soft-reset", NULL);
267 if (num == NULL || rst == NULL)
268 continue;
269 if (param == *num) {
270 reset_io = *rst;
271 break;
274 if (np == NULL || reset_io == 0)
275 return -ENODEV;
277 LOCK(flags);
278 MACIO_OUT8(reset_io, KEYLARGO_GPIO_OUTPUT_ENABLE);
279 (void)MACIO_IN8(reset_io);
280 udelay(1);
281 MACIO_OUT8(reset_io, 0);
282 (void)MACIO_IN8(reset_io);
283 UNLOCK(flags);
285 return 0;
287 #endif /* CONFIG_SMP */
290 * This can be called from pmac_smp so isn't static
292 * This takes the second CPU off the bus on dual CPU machines
293 * running UP
295 void __pmac g5_phy_disable_cpu1(void)
297 UN_OUT(U3_API_PHY_CONFIG_1, 0);
300 static long __pmac generic_get_mb_info(struct device_node* node, long param, long value)
302 switch(param) {
303 case PMAC_MB_INFO_MODEL:
304 return pmac_mb.model_id;
305 case PMAC_MB_INFO_FLAGS:
306 return pmac_mb.board_flags;
307 case PMAC_MB_INFO_NAME:
308 /* hack hack hack... but should work */
309 *((const char **)value) = pmac_mb.model_name;
310 return 0;
312 return -EINVAL;
317 * Table definitions
320 /* Used on any machine
322 static struct feature_table_entry any_features[] __pmacdata = {
323 { PMAC_FTR_GET_MB_INFO, generic_get_mb_info },
324 { 0, NULL }
327 /* G5 features
329 static struct feature_table_entry g5_features[] __pmacdata = {
330 { PMAC_FTR_GMAC_ENABLE, g5_gmac_enable },
331 { PMAC_FTR_1394_ENABLE, g5_fw_enable },
332 { PMAC_FTR_ENABLE_MPIC, g5_mpic_enable },
333 { PMAC_FTR_READ_GPIO, g5_read_gpio },
334 { PMAC_FTR_WRITE_GPIO, g5_write_gpio },
335 #ifdef CONFIG_SMP
336 { PMAC_FTR_RESET_CPU, g5_reset_cpu },
337 #endif /* CONFIG_SMP */
338 { 0, NULL }
341 static struct pmac_mb_def pmac_mb_defs[] __pmacdata = {
342 { "PowerMac7,2", "PowerMac G5",
343 PMAC_TYPE_POWERMAC_G5, g5_features,
346 { "PowerMac7,3", "PowerMac G5",
347 PMAC_TYPE_POWERMAC_G5, g5_features,
350 { "RackMac3,1", "XServe G5",
351 PMAC_TYPE_POWERMAC_G5, g5_features,
357 * The toplevel feature_call callback
359 long __pmac pmac_do_feature_call(unsigned int selector, ...)
361 struct device_node* node;
362 long param, value;
363 int i;
364 feature_call func = NULL;
365 va_list args;
367 if (pmac_mb.features)
368 for (i=0; pmac_mb.features[i].function; i++)
369 if (pmac_mb.features[i].selector == selector) {
370 func = pmac_mb.features[i].function;
371 break;
373 if (!func)
374 for (i=0; any_features[i].function; i++)
375 if (any_features[i].selector == selector) {
376 func = any_features[i].function;
377 break;
379 if (!func)
380 return -ENODEV;
382 va_start(args, selector);
383 node = (struct device_node*)va_arg(args, void*);
384 param = va_arg(args, long);
385 value = va_arg(args, long);
386 va_end(args);
388 return func(node, param, value);
391 static int __init probe_motherboard(void)
393 int i;
394 struct macio_chip* macio = &macio_chips[0];
395 const char* model = NULL;
396 struct device_node *dt;
398 /* Lookup known motherboard type in device-tree. First try an
399 * exact match on the "model" property, then try a "compatible"
400 * match is none is found.
402 dt = find_devices("device-tree");
403 if (dt != NULL)
404 model = (const char *) get_property(dt, "model", NULL);
405 for(i=0; model && i<(sizeof(pmac_mb_defs)/sizeof(struct pmac_mb_def)); i++) {
406 if (strcmp(model, pmac_mb_defs[i].model_string) == 0) {
407 pmac_mb = pmac_mb_defs[i];
408 goto found;
411 for(i=0; i<(sizeof(pmac_mb_defs)/sizeof(struct pmac_mb_def)); i++) {
412 if (machine_is_compatible(pmac_mb_defs[i].model_string)) {
413 pmac_mb = pmac_mb_defs[i];
414 goto found;
418 /* Fallback to selection depending on mac-io chip type */
419 switch(macio->type) {
420 case macio_keylargo2:
421 pmac_mb.model_id = PMAC_TYPE_UNKNOWN_K2;
422 pmac_mb.model_name = "Unknown K2-based";
423 pmac_mb.features = g5_features;
425 default:
426 return -ENODEV;
428 found:
429 /* Check for "mobile" machine */
430 if (model && (strncmp(model, "PowerBook", 9) == 0
431 || strncmp(model, "iBook", 5) == 0))
432 pmac_mb.board_flags |= PMAC_MB_MOBILE;
435 printk(KERN_INFO "PowerMac motherboard: %s\n", pmac_mb.model_name);
436 return 0;
439 /* Initialize the Core99 UniNorth host bridge and memory controller
441 static void __init probe_uninorth(void)
443 uninorth_node = of_find_node_by_name(NULL, "u3");
444 if (uninorth_node && uninorth_node->n_addrs > 0) {
445 /* Small hack until I figure out if parsing in prom.c is correct. I should
446 * get rid of those pre-parsed junk anyway
448 unsigned long address = uninorth_node->addrs[0].address;
449 uninorth_base = ioremap(address, 0x40000);
450 uninorth_rev = in_be32(UN_REG(UNI_N_VERSION));
451 u3_ht = ioremap(address + U3_HT_CONFIG_BASE, 0x1000);
452 } else
453 uninorth_node = NULL;
455 if (!uninorth_node)
456 return;
458 printk(KERN_INFO "Found U3 memory controller & host bridge, revision: %d\n",
459 uninorth_rev);
460 printk(KERN_INFO "Mapped at 0x%08lx\n", (unsigned long)uninorth_base);
464 static void __init probe_one_macio(const char* name, const char* compat, int type)
466 struct device_node* node;
467 int i;
468 volatile u32* base;
469 u32* revp;
471 node = find_devices(name);
472 if (!node || !node->n_addrs)
473 return;
474 if (compat)
475 do {
476 if (device_is_compatible(node, compat))
477 break;
478 node = node->next;
479 } while (node);
480 if (!node)
481 return;
482 for(i=0; i<MAX_MACIO_CHIPS; i++) {
483 if (!macio_chips[i].of_node)
484 break;
485 if (macio_chips[i].of_node == node)
486 return;
488 if (i >= MAX_MACIO_CHIPS) {
489 printk(KERN_ERR "pmac_feature: Please increase MAX_MACIO_CHIPS !\n");
490 printk(KERN_ERR "pmac_feature: %s skipped\n", node->full_name);
491 return;
493 base = (volatile u32*)ioremap(node->addrs[0].address, node->addrs[0].size);
494 if (!base) {
495 printk(KERN_ERR "pmac_feature: Can't map mac-io chip !\n");
496 return;
498 if (type == macio_keylargo) {
499 u32* did = (u32 *)get_property(node, "device-id", NULL);
500 if (*did == 0x00000025)
501 type = macio_pangea;
502 if (*did == 0x0000003e)
503 type = macio_intrepid;
505 macio_chips[i].of_node = node;
506 macio_chips[i].type = type;
507 macio_chips[i].base = base;
508 macio_chips[i].flags = MACIO_FLAG_SCCB_ON | MACIO_FLAG_SCCB_ON;
509 macio_chips[i].name = macio_names[type];
510 revp = (u32 *)get_property(node, "revision-id", NULL);
511 if (revp)
512 macio_chips[i].rev = *revp;
513 printk(KERN_INFO "Found a %s mac-io controller, rev: %d, mapped at 0x%p\n",
514 macio_names[type], macio_chips[i].rev, macio_chips[i].base);
517 static int __init
518 probe_macios(void)
520 probe_one_macio("mac-io", "K2-Keylargo", macio_keylargo2);
522 macio_chips[0].lbus.index = 0;
523 macio_chips[1].lbus.index = 1;
525 return (macio_chips[0].of_node == NULL) ? -ENODEV : 0;
528 static void __init
529 set_initial_features(void)
531 struct device_node *np;
533 if (macio_chips[0].type == macio_keylargo2) {
534 #ifndef CONFIG_SMP
535 /* On SMP machines running UP, we have the second CPU eating
536 * bus cycles. We need to take it off the bus. This is done
537 * from pmac_smp for SMP kernels running on one CPU
539 np = of_find_node_by_type(NULL, "cpu");
540 if (np != NULL)
541 np = of_find_node_by_type(np, "cpu");
542 if (np != NULL) {
543 g5_phy_disable_cpu1();
544 of_node_put(np);
546 #endif /* CONFIG_SMP */
547 /* Enable GMAC for now for PCI probing. It will be disabled
548 * later on after PCI probe
550 np = of_find_node_by_name(NULL, "ethernet");
551 while(np) {
552 if (device_is_compatible(np, "K2-GMAC"))
553 g5_gmac_enable(np, 0, 1);
554 np = of_find_node_by_name(np, "ethernet");
557 /* Enable FW before PCI probe. Will be disabled later on
558 * Note: We should have a batter way to check that we are
559 * dealing with uninorth internal cell and not a PCI cell
560 * on the external PCI. The code below works though.
562 np = of_find_node_by_name(NULL, "firewire");
563 while(np) {
564 if (device_is_compatible(np, "pci106b,5811")) {
565 macio_chips[0].flags |= MACIO_FLAG_FW_SUPPORTED;
566 g5_fw_enable(np, 0, 1);
568 np = of_find_node_by_name(np, "firewire");
573 void __init
574 pmac_feature_init(void)
576 /* Detect the UniNorth memory controller */
577 probe_uninorth();
579 /* Probe mac-io controllers */
580 if (probe_macios()) {
581 printk(KERN_WARNING "No mac-io chip found\n");
582 return;
585 /* Setup low-level i2c stuffs */
586 pmac_init_low_i2c();
588 /* Probe machine type */
589 if (probe_motherboard())
590 printk(KERN_WARNING "Unknown PowerMac !\n");
592 /* Set some initial features (turn off some chips that will
593 * be later turned on)
595 set_initial_features();
598 int __init pmac_feature_late_init(void)
600 #if 0
601 struct device_node* np;
603 /* Request some resources late */
604 if (uninorth_node)
605 request_OF_resource(uninorth_node, 0, NULL);
606 np = find_devices("hammerhead");
607 if (np)
608 request_OF_resource(np, 0, NULL);
609 np = find_devices("interrupt-controller");
610 if (np)
611 request_OF_resource(np, 0, NULL);
612 #endif
613 return 0;
616 device_initcall(pmac_feature_late_init);
618 #if 0
619 static void dump_HT_speeds(char *name, u32 cfg, u32 frq)
621 int freqs[16] = { 200,300,400,500,600,800,1000,0,0,0,0,0,0,0,0,0 };
622 int bits[8] = { 8,16,0,32,2,4,0,0 };
623 int freq = (frq >> 8) & 0xf;
625 if (freqs[freq] == 0)
626 printk("%s: Unknown HT link frequency %x\n", name, freq);
627 else
628 printk("%s: %d MHz on main link, (%d in / %d out) bits width\n",
629 name, freqs[freq],
630 bits[(cfg >> 28) & 0x7], bits[(cfg >> 24) & 0x7]);
632 #endif
634 void __init pmac_check_ht_link(void)
636 #if 0 /* Disabled for now */
637 u32 ufreq, freq, ucfg, cfg;
638 struct device_node *pcix_node;
639 u8 px_bus, px_devfn;
640 struct pci_controller *px_hose;
642 (void)in_be32(u3_ht + U3_HT_LINK_COMMAND);
643 ucfg = cfg = in_be32(u3_ht + U3_HT_LINK_CONFIG);
644 ufreq = freq = in_be32(u3_ht + U3_HT_LINK_FREQ);
645 dump_HT_speeds("U3 HyperTransport", cfg, freq);
647 pcix_node = of_find_compatible_node(NULL, "pci", "pci-x");
648 if (pcix_node == NULL) {
649 printk("No PCI-X bridge found\n");
650 return;
652 px_hose = pcix_node->phb;
653 px_bus = pcix_node->busno;
654 px_devfn = pcix_node->devfn;
656 early_read_config_dword(px_hose, px_bus, px_devfn, 0xc4, &cfg);
657 early_read_config_dword(px_hose, px_bus, px_devfn, 0xcc, &freq);
658 dump_HT_speeds("PCI-X HT Uplink", cfg, freq);
659 early_read_config_dword(px_hose, px_bus, px_devfn, 0xc8, &cfg);
660 early_read_config_dword(px_hose, px_bus, px_devfn, 0xd0, &freq);
661 dump_HT_speeds("PCI-X HT Downlink", cfg, freq);
662 #endif