patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / arch / ppc64 / kernel / pmac_feature.c
1 /*
2  *  arch/ppc/platforms/pmac_feature.c
3  *
4  *  Copyright (C) 1996-2001 Paul Mackerras (paulus@cs.anu.edu.au)
5  *                          Ben. Herrenschmidt (benh@kernel.crashing.org)
6  *
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.
11  *
12  *  TODO:
13  *
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...
19  *
20  */
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>
43
44 #undef DEBUG_FEATURE
45
46 #ifdef DEBUG_FEATURE
47 #define DBG(fmt,...) printk(KERN_DEBUG fmt)
48 #else
49 #define DBG(fmt,...)
50 #endif
51
52 /*
53  * We use a single global lock to protect accesses. Each driver has
54  * to take care of its own locking
55  */
56 static spinlock_t feature_lock  __pmacdata = SPIN_LOCK_UNLOCKED;
57
58 #define LOCK(flags)     spin_lock_irqsave(&feature_lock, flags);
59 #define UNLOCK(flags)   spin_unlock_irqrestore(&feature_lock, flags);
60
61
62 /*
63  * Instance of some macio stuffs
64  */
65 struct macio_chip macio_chips[MAX_MACIO_CHIPS]  __pmacdata;
66
67 struct macio_chip* __pmac
68 macio_find(struct device_node* child, int type)
69 {
70         while(child) {
71                 int     i;
72
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;
78         }
79         return NULL;
80 }
81
82 static const char* macio_names[] __pmacdata =
83 {
84         "Unknown",
85         "Grand Central",
86         "OHare",
87         "OHareII",
88         "Heathrow",
89         "Gatwick",
90         "Paddington",
91         "Keylargo",
92         "Pangea",
93         "Intrepid",
94         "K2"
95 };
96
97
98
99 /*
100  * Uninorth reg. access. Note that Uni-N regs are big endian
101  */
102
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)))
108
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;
113
114 extern struct pci_dev *k2_skiplist[2];
115
116 /*
117  * For each motherboard family, we have a table of functions pointers
118  * that handle the various features.
119  */
120
121 typedef long (*feature_call)(struct device_node* node, long param, long value);
122
123 struct feature_table_entry {
124         unsigned int    selector;
125         feature_call    function;
126 };
127
128 struct pmac_mb_def
129 {
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;
135 };
136 static struct pmac_mb_def pmac_mb __pmacdata;
137
138 /*
139  * Here are the chip specific feature functions
140  */
141
142
143 static long __pmac g5_read_gpio(struct device_node* node, long param, long value)
144 {
145         struct macio_chip* macio = &macio_chips[0];
146
147         return MACIO_IN8(param);
148 }
149
150
151 static long __pmac g5_write_gpio(struct device_node* node, long param, long value)
152 {
153         struct macio_chip* macio = &macio_chips[0];
154
155         MACIO_OUT8(param, (u8)(value & 0xff));
156         return 0;
157 }
158
159 static long __pmac g5_gmac_enable(struct device_node* node, long param, long value)
160 {
161         struct macio_chip* macio = &macio_chips[0];
162         unsigned long flags;
163         struct pci_dev *pdev = NULL;
164
165         if (node == NULL)
166                 return -ENODEV;
167
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.
176          */
177         if (!value)
178                 pdev = pci_find_slot(node->busno, node->devfn);
179
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);
189         }
190         
191         UNLOCK(flags);
192         mdelay(1);
193
194         return 0;
195 }
196
197 static long __pmac g5_fw_enable(struct device_node* node, long param, long value)
198 {
199         struct macio_chip* macio = &macio_chips[0];
200         unsigned long flags;
201         struct pci_dev *pdev = NULL;
202
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.
211          */
212         if (node == NULL)
213                 return -ENODEV;
214
215         if (!value)
216                 pdev = pci_find_slot(node->busno, node->devfn);
217
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);
227         }
228         
229         UNLOCK(flags);
230         mdelay(1);
231
232         return 0;
233 }
234
235 static long __pmac g5_mpic_enable(struct device_node* node, long param, long value)
236 {
237         unsigned long flags;
238
239         if (node->parent == NULL || strcmp(node->parent->name, "u3"))
240                 return 0;
241
242         LOCK(flags);
243         UN_BIS(U3_TOGGLE_REG, U3_MPIC_RESET | U3_MPIC_OUTPUT_ENABLE);
244         UNLOCK(flags);
245
246         return 0;
247 }
248
249 #ifdef CONFIG_SMP
250 static long __pmac g5_reset_cpu(struct device_node* node, long param, long value)
251 {
252         unsigned int reset_io = 0;
253         unsigned long flags;
254         struct macio_chip* macio;
255         struct device_node* np;
256
257         macio = &macio_chips[0];
258         if (macio->type != macio_keylargo2)
259                 return -ENODEV;
260
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;
272                 }
273         }
274         if (np == NULL || reset_io == 0)
275                 return -ENODEV;
276
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);
284
285         return 0;
286 }
287 #endif /* CONFIG_SMP */
288
289 /*
290  * This can be called from pmac_smp so isn't static
291  *
292  * This takes the second CPU off the bus on dual CPU machines
293  * running UP
294  */
295 void __pmac g5_phy_disable_cpu1(void)
296 {
297         UN_OUT(U3_API_PHY_CONFIG_1, 0);
298 }
299
300 static long __pmac generic_get_mb_info(struct device_node* node, long param, long value)
301 {
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;
311         }
312         return -EINVAL;
313 }
314
315
316 /*
317  * Table definitions
318  */
319
320 /* Used on any machine
321  */
322 static struct feature_table_entry any_features[]  __pmacdata = {
323         { PMAC_FTR_GET_MB_INFO,         generic_get_mb_info },
324         { 0, NULL }
325 };
326
327 /* G5 features
328  */
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 }
339 };
340
341 static struct pmac_mb_def pmac_mb_defs[] __pmacdata = {
342         {       "PowerMac7,2",                  "PowerMac G5",
343                 PMAC_TYPE_POWERMAC_G5,          g5_features,
344                 0,
345         },
346         {       "RackMac3,1",                   "XServe G5",
347                 PMAC_TYPE_POWERMAC_G5,          g5_features,
348                 0,
349         },
350 };
351
352 /*
353  * The toplevel feature_call callback
354  */
355 long __pmac pmac_do_feature_call(unsigned int selector, ...)
356 {
357         struct device_node* node;
358         long param, value;
359         int i;
360         feature_call func = NULL;
361         va_list args;
362
363         if (pmac_mb.features)
364                 for (i=0; pmac_mb.features[i].function; i++)
365                         if (pmac_mb.features[i].selector == selector) {
366                                 func = pmac_mb.features[i].function;
367                                 break;
368                         }
369         if (!func)
370                 for (i=0; any_features[i].function; i++)
371                         if (any_features[i].selector == selector) {
372                                 func = any_features[i].function;
373                                 break;
374                         }
375         if (!func)
376                 return -ENODEV;
377
378         va_start(args, selector);
379         node = (struct device_node*)va_arg(args, void*);
380         param = va_arg(args, long);
381         value = va_arg(args, long);
382         va_end(args);
383
384         return func(node, param, value);
385 }
386
387 static int __init probe_motherboard(void)
388 {
389         int i;
390         struct macio_chip* macio = &macio_chips[0];
391         const char* model = NULL;
392         struct device_node *dt;
393
394         /* Lookup known motherboard type in device-tree. First try an
395          * exact match on the "model" property, then try a "compatible"
396          * match is none is found.
397          */
398         dt = find_devices("device-tree");
399         if (dt != NULL)
400                 model = (const char *) get_property(dt, "model", NULL);
401         for(i=0; model && i<(sizeof(pmac_mb_defs)/sizeof(struct pmac_mb_def)); i++) {
402             if (strcmp(model, pmac_mb_defs[i].model_string) == 0) {
403                 pmac_mb = pmac_mb_defs[i];
404                 goto found;
405             }
406         }
407         for(i=0; i<(sizeof(pmac_mb_defs)/sizeof(struct pmac_mb_def)); i++) {
408             if (machine_is_compatible(pmac_mb_defs[i].model_string)) {
409                 pmac_mb = pmac_mb_defs[i];
410                 goto found;
411             }
412         }
413
414         /* Fallback to selection depending on mac-io chip type */
415         switch(macio->type) {
416         case macio_keylargo2:
417                 pmac_mb.model_id = PMAC_TYPE_UNKNOWN_K2;
418                 pmac_mb.model_name = "Unknown K2-based";
419                 pmac_mb.features = g5_features;
420                 
421         default:
422                 return -ENODEV;
423         }
424 found:
425         /* Check for "mobile" machine */
426         if (model && (strncmp(model, "PowerBook", 9) == 0
427                    || strncmp(model, "iBook", 5) == 0))
428                 pmac_mb.board_flags |= PMAC_MB_MOBILE;
429
430
431         printk(KERN_INFO "PowerMac motherboard: %s\n", pmac_mb.model_name);
432         return 0;
433 }
434
435 /* Initialize the Core99 UniNorth host bridge and memory controller
436  */
437 static void __init probe_uninorth(void)
438 {
439         uninorth_node = of_find_node_by_name(NULL, "u3");
440         if (uninorth_node && uninorth_node->n_addrs > 0) {
441                 /* Small hack until I figure out if parsing in prom.c is correct. I should
442                  * get rid of those pre-parsed junk anyway
443                  */
444                 unsigned long address = uninorth_node->addrs[0].address;
445                 uninorth_base = ioremap(address, 0x40000);
446                 uninorth_rev = in_be32(UN_REG(UNI_N_VERSION));
447                 u3_ht = ioremap(address + U3_HT_CONFIG_BASE, 0x1000);
448         } else
449                 uninorth_node = NULL;
450
451         if (!uninorth_node)
452                 return;
453
454         printk(KERN_INFO "Found U3 memory controller & host bridge, revision: %d\n",
455                uninorth_rev);
456         printk(KERN_INFO "Mapped at 0x%08lx\n", (unsigned long)uninorth_base);
457
458 }
459
460 static void __init probe_one_macio(const char* name, const char* compat, int type)
461 {
462         struct device_node*     node;
463         int                     i;
464         volatile u32*           base;
465         u32*                    revp;
466
467         node = find_devices(name);
468         if (!node || !node->n_addrs)
469                 return;
470         if (compat)
471                 do {
472                         if (device_is_compatible(node, compat))
473                                 break;
474                         node = node->next;
475                 } while (node);
476         if (!node)
477                 return;
478         for(i=0; i<MAX_MACIO_CHIPS; i++) {
479                 if (!macio_chips[i].of_node)
480                         break;
481                 if (macio_chips[i].of_node == node)
482                         return;
483         }
484         if (i >= MAX_MACIO_CHIPS) {
485                 printk(KERN_ERR "pmac_feature: Please increase MAX_MACIO_CHIPS !\n");
486                 printk(KERN_ERR "pmac_feature: %s skipped\n", node->full_name);
487                 return;
488         }
489         base = (volatile u32*)ioremap(node->addrs[0].address, node->addrs[0].size);
490         if (!base) {
491                 printk(KERN_ERR "pmac_feature: Can't map mac-io chip !\n");
492                 return;
493         }
494         if (type == macio_keylargo) {
495                 u32* did = (u32 *)get_property(node, "device-id", NULL);
496                 if (*did == 0x00000025)
497                         type = macio_pangea;
498                 if (*did == 0x0000003e)
499                         type = macio_intrepid;
500         }
501         macio_chips[i].of_node  = node;
502         macio_chips[i].type     = type;
503         macio_chips[i].base     = base;
504         macio_chips[i].flags    = MACIO_FLAG_SCCB_ON | MACIO_FLAG_SCCB_ON;
505         macio_chips[i].name     = macio_names[type];
506         revp = (u32 *)get_property(node, "revision-id", NULL);
507         if (revp)
508                 macio_chips[i].rev = *revp;
509         printk(KERN_INFO "Found a %s mac-io controller, rev: %d, mapped at 0x%p\n",
510                 macio_names[type], macio_chips[i].rev, macio_chips[i].base);
511 }
512
513 static int __init
514 probe_macios(void)
515 {
516         probe_one_macio("mac-io", "K2-Keylargo", macio_keylargo2);
517
518         macio_chips[0].lbus.index = 0;
519         macio_chips[1].lbus.index = 1;
520
521         return (macio_chips[0].of_node == NULL) ? -ENODEV : 0;
522 }
523
524 static void __init
525 set_initial_features(void)
526 {
527         struct device_node *np;
528
529         if (macio_chips[0].type == macio_keylargo2) {
530 #ifndef CONFIG_SMP
531                 /* On SMP machines running UP, we have the second CPU eating
532                  * bus cycles. We need to take it off the bus. This is done
533                  * from pmac_smp for SMP kernels running on one CPU
534                  */
535                 np = of_find_node_by_type(NULL, "cpu");
536                 if (np != NULL)
537                         np = of_find_node_by_type(np, "cpu");
538                 if (np != NULL) {
539                         g5_phy_disable_cpu1();
540                         of_node_put(np);
541                 }
542 #endif /* CONFIG_SMP */
543                 /* Enable GMAC for now for PCI probing. It will be disabled
544                  * later on after PCI probe
545                  */
546                 np = of_find_node_by_name(NULL, "ethernet");
547                 while(np) {
548                         if (device_is_compatible(np, "K2-GMAC"))
549                                 g5_gmac_enable(np, 0, 1);
550                         np = of_find_node_by_name(np, "ethernet");
551                 }
552
553                 /* Enable FW before PCI probe. Will be disabled later on
554                  * Note: We should have a batter way to check that we are
555                  * dealing with uninorth internal cell and not a PCI cell
556                  * on the external PCI. The code below works though.
557                  */
558                 np = of_find_node_by_name(NULL, "firewire");
559                 while(np) {
560                         if (device_is_compatible(np, "pci106b,5811")) {
561                                 macio_chips[0].flags |= MACIO_FLAG_FW_SUPPORTED;
562                                 g5_fw_enable(np, 0, 1);
563                         }
564                         np = of_find_node_by_name(np, "firewire");
565                 }
566         }
567 }
568
569 void __init
570 pmac_feature_init(void)
571 {
572         /* Detect the UniNorth memory controller */
573         probe_uninorth();
574
575         /* Probe mac-io controllers */
576         if (probe_macios()) {
577                 printk(KERN_WARNING "No mac-io chip found\n");
578                 return;
579         }
580
581         /* Setup low-level i2c stuffs */
582         pmac_init_low_i2c();
583
584         /* Probe machine type */
585         if (probe_motherboard())
586                 printk(KERN_WARNING "Unknown PowerMac !\n");
587
588         /* Set some initial features (turn off some chips that will
589          * be later turned on)
590          */
591         set_initial_features();
592 }
593
594 int __init pmac_feature_late_init(void)
595 {
596 #if 0
597         struct device_node* np;
598
599         /* Request some resources late */
600         if (uninorth_node)
601                 request_OF_resource(uninorth_node, 0, NULL);
602         np = find_devices("hammerhead");
603         if (np)
604                 request_OF_resource(np, 0, NULL);
605         np = find_devices("interrupt-controller");
606         if (np)
607                 request_OF_resource(np, 0, NULL);
608 #endif
609         return 0;
610 }
611
612 device_initcall(pmac_feature_late_init);
613
614
615 static void dump_HT_speeds(char *name, u32 cfg, u32 frq)
616 {
617         int     freqs[16] = { 200,300,400,500,600,800,1000,0,0,0,0,0,0,0,0,0 };
618         int     bits[8] = { 8,16,0,32,2,4,0,0 };
619         int     freq = (frq >> 8) & 0xf;
620
621         if (freqs[freq] == 0)
622                 printk("%s: Unknown HT link frequency %x\n", name, freq);
623         else
624                 printk("%s: %d MHz on main link, (%d in / %d out) bits width\n",
625                        name, freqs[freq],
626                        bits[(cfg >> 28) & 0x7], bits[(cfg >> 24) & 0x7]);
627 }
628
629 void __init pmac_check_ht_link(void)
630 {
631 #if 0 /* Disabled for now */
632         u32     ufreq, freq, ucfg, cfg;
633         struct device_node *pcix_node;
634         u8      px_bus, px_devfn;
635         struct pci_controller *px_hose;
636
637         (void)in_be32(u3_ht + U3_HT_LINK_COMMAND);
638         ucfg = cfg = in_be32(u3_ht + U3_HT_LINK_CONFIG);
639         ufreq = freq = in_be32(u3_ht + U3_HT_LINK_FREQ);
640         dump_HT_speeds("U3 HyperTransport", cfg, freq);
641
642         pcix_node = of_find_compatible_node(NULL, "pci", "pci-x");
643         if (pcix_node == NULL) {
644                 printk("No PCI-X bridge found\n");
645                 return;
646         }
647         px_hose = pcix_node->phb;
648         px_bus = pcix_node->busno;
649         px_devfn = pcix_node->devfn;
650         
651         early_read_config_dword(px_hose, px_bus, px_devfn, 0xc4, &cfg);
652         early_read_config_dword(px_hose, px_bus, px_devfn, 0xcc, &freq);
653         dump_HT_speeds("PCI-X HT Uplink", cfg, freq);
654         early_read_config_dword(px_hose, px_bus, px_devfn, 0xc8, &cfg);
655         early_read_config_dword(px_hose, px_bus, px_devfn, 0xd0, &freq);
656         dump_HT_speeds("PCI-X HT Downlink", cfg, freq);
657 #endif
658 }