ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 };
347
348 /*
349  * The toplevel feature_call callback
350  */
351 long __pmac pmac_do_feature_call(unsigned int selector, ...)
352 {
353         struct device_node* node;
354         long param, value;
355         int i;
356         feature_call func = NULL;
357         va_list args;
358
359         if (pmac_mb.features)
360                 for (i=0; pmac_mb.features[i].function; i++)
361                         if (pmac_mb.features[i].selector == selector) {
362                                 func = pmac_mb.features[i].function;
363                                 break;
364                         }
365         if (!func)
366                 for (i=0; any_features[i].function; i++)
367                         if (any_features[i].selector == selector) {
368                                 func = any_features[i].function;
369                                 break;
370                         }
371         if (!func)
372                 return -ENODEV;
373
374         va_start(args, selector);
375         node = (struct device_node*)va_arg(args, void*);
376         param = va_arg(args, long);
377         value = va_arg(args, long);
378         va_end(args);
379
380         return func(node, param, value);
381 }
382
383 static int __init probe_motherboard(void)
384 {
385         int i;
386         struct macio_chip* macio = &macio_chips[0];
387         const char* model = NULL;
388         struct device_node *dt;
389
390         /* Lookup known motherboard type in device-tree. First try an
391          * exact match on the "model" property, then try a "compatible"
392          * match is none is found.
393          */
394         dt = find_devices("device-tree");
395         if (dt != NULL)
396                 model = (const char *) get_property(dt, "model", NULL);
397         for(i=0; model && i<(sizeof(pmac_mb_defs)/sizeof(struct pmac_mb_def)); i++) {
398             if (strcmp(model, pmac_mb_defs[i].model_string) == 0) {
399                 pmac_mb = pmac_mb_defs[i];
400                 goto found;
401             }
402         }
403         for(i=0; i<(sizeof(pmac_mb_defs)/sizeof(struct pmac_mb_def)); i++) {
404             if (machine_is_compatible(pmac_mb_defs[i].model_string)) {
405                 pmac_mb = pmac_mb_defs[i];
406                 goto found;
407             }
408         }
409
410         /* Fallback to selection depending on mac-io chip type */
411         switch(macio->type) {
412         case macio_keylargo2:
413                 pmac_mb.model_id = PMAC_TYPE_UNKNOWN_K2;
414                 pmac_mb.model_name = "Unknown K2-based";
415                 pmac_mb.features = g5_features;
416                 
417         default:
418                 return -ENODEV;
419         }
420 found:
421         /* Check for "mobile" machine */
422         if (model && (strncmp(model, "PowerBook", 9) == 0
423                    || strncmp(model, "iBook", 5) == 0))
424                 pmac_mb.board_flags |= PMAC_MB_MOBILE;
425
426
427         printk(KERN_INFO "PowerMac motherboard: %s\n", pmac_mb.model_name);
428         return 0;
429 }
430
431 /* Initialize the Core99 UniNorth host bridge and memory controller
432  */
433 static void __init probe_uninorth(void)
434 {
435         uninorth_node = of_find_node_by_name(NULL, "u3");
436         if (uninorth_node && uninorth_node->n_addrs > 0) {
437                 /* Small hack until I figure out if parsing in prom.c is correct. I should
438                  * get rid of those pre-parsed junk anyway
439                  */
440                 unsigned long address = uninorth_node->addrs[0].address;
441                 uninorth_base = ioremap(address, 0x40000);
442                 uninorth_rev = in_be32(UN_REG(UNI_N_VERSION));
443                 u3_ht = ioremap(address + U3_HT_CONFIG_BASE, 0x1000);
444         } else
445                 uninorth_node = NULL;
446
447         if (!uninorth_node)
448                 return;
449
450         printk(KERN_INFO "Found U3 memory controller & host bridge, revision: %d\n",
451                uninorth_rev);
452         printk(KERN_INFO "Mapped at 0x%08lx\n", (unsigned long)uninorth_base);
453
454 }
455
456 static void __init probe_one_macio(const char* name, const char* compat, int type)
457 {
458         struct device_node*     node;
459         int                     i;
460         volatile u32*           base;
461         u32*                    revp;
462
463         node = find_devices(name);
464         if (!node || !node->n_addrs)
465                 return;
466         if (compat)
467                 do {
468                         if (device_is_compatible(node, compat))
469                                 break;
470                         node = node->next;
471                 } while (node);
472         if (!node)
473                 return;
474         for(i=0; i<MAX_MACIO_CHIPS; i++) {
475                 if (!macio_chips[i].of_node)
476                         break;
477                 if (macio_chips[i].of_node == node)
478                         return;
479         }
480         if (i >= MAX_MACIO_CHIPS) {
481                 printk(KERN_ERR "pmac_feature: Please increase MAX_MACIO_CHIPS !\n");
482                 printk(KERN_ERR "pmac_feature: %s skipped\n", node->full_name);
483                 return;
484         }
485         base = (volatile u32*)ioremap(node->addrs[0].address, node->addrs[0].size);
486         if (!base) {
487                 printk(KERN_ERR "pmac_feature: Can't map mac-io chip !\n");
488                 return;
489         }
490         if (type == macio_keylargo) {
491                 u32* did = (u32 *)get_property(node, "device-id", NULL);
492                 if (*did == 0x00000025)
493                         type = macio_pangea;
494                 if (*did == 0x0000003e)
495                         type = macio_intrepid;
496         }
497         macio_chips[i].of_node  = node;
498         macio_chips[i].type     = type;
499         macio_chips[i].base     = base;
500         macio_chips[i].flags    = MACIO_FLAG_SCCB_ON | MACIO_FLAG_SCCB_ON;
501         macio_chips[i].name     = macio_names[type];
502         revp = (u32 *)get_property(node, "revision-id", NULL);
503         if (revp)
504                 macio_chips[i].rev = *revp;
505         printk(KERN_INFO "Found a %s mac-io controller, rev: %d, mapped at 0x%p\n",
506                 macio_names[type], macio_chips[i].rev, macio_chips[i].base);
507 }
508
509 static int __init
510 probe_macios(void)
511 {
512         probe_one_macio("mac-io", "K2-Keylargo", macio_keylargo2);
513
514         macio_chips[0].lbus.index = 0;
515         macio_chips[1].lbus.index = 1;
516
517         return (macio_chips[0].of_node == NULL) ? -ENODEV : 0;
518 }
519
520 static void __init
521 set_initial_features(void)
522 {
523         struct device_node *np;
524
525         if (macio_chips[0].type == macio_keylargo2) {
526 #ifndef CONFIG_SMP
527                 /* On SMP machines running UP, we have the second CPU eating
528                  * bus cycles. We need to take it off the bus. This is done
529                  * from pmac_smp for SMP kernels running on one CPU
530                  */
531                 np = of_find_node_by_type(NULL, "cpu");
532                 if (np != NULL)
533                         np = of_find_node_by_type(np, "cpu");
534                 if (np != NULL) {
535                         g5_phy_disable_cpu1();
536                         of_node_put(np);
537                 }
538 #endif /* CONFIG_SMP */
539                 /* Enable GMAC for now for PCI probing. It will be disabled
540                  * later on after PCI probe
541                  */
542                 np = of_find_node_by_name(NULL, "ethernet");
543                 while(np) {
544                         if (device_is_compatible(np, "K2-GMAC"))
545                                 g5_gmac_enable(np, 0, 1);
546                         np = of_find_node_by_name(np, "ethernet");
547                 }
548
549                 /* Enable FW before PCI probe. Will be disabled later on
550                  * Note: We should have a batter way to check that we are
551                  * dealing with uninorth internal cell and not a PCI cell
552                  * on the external PCI. The code below works though.
553                  */
554                 np = of_find_node_by_name(NULL, "firewire");
555                 while(np) {
556                         if (device_is_compatible(np, "pci106b,5811")) {
557                                 macio_chips[0].flags |= MACIO_FLAG_FW_SUPPORTED;
558                                 g5_fw_enable(np, 0, 1);
559                         }
560                         np = of_find_node_by_name(np, "firewire");
561                 }
562         }
563 }
564
565 void __init
566 pmac_feature_init(void)
567 {
568         /* Detect the UniNorth memory controller */
569         probe_uninorth();
570
571         /* Probe mac-io controllers */
572         if (probe_macios()) {
573                 printk(KERN_WARNING "No mac-io chip found\n");
574                 return;
575         }
576
577         /* Setup low-level i2c stuffs */
578         pmac_init_low_i2c();
579
580         /* Probe machine type */
581         if (probe_motherboard())
582                 printk(KERN_WARNING "Unknown PowerMac !\n");
583
584         /* Set some initial features (turn off some chips that will
585          * be later turned on)
586          */
587         set_initial_features();
588 }
589
590 int __init pmac_feature_late_init(void)
591 {
592 #if 0
593         struct device_node* np;
594
595         /* Request some resources late */
596         if (uninorth_node)
597                 request_OF_resource(uninorth_node, 0, NULL);
598         np = find_devices("hammerhead");
599         if (np)
600                 request_OF_resource(np, 0, NULL);
601         np = find_devices("interrupt-controller");
602         if (np)
603                 request_OF_resource(np, 0, NULL);
604 #endif
605         return 0;
606 }
607
608 device_initcall(pmac_feature_late_init);
609
610
611 static void dump_HT_speeds(char *name, u32 cfg, u32 frq)
612 {
613         int     freqs[16] = { 200,300,400,500,600,800,1000,0,0,0,0,0,0,0,0,0 };
614         int     bits[8] = { 8,16,0,32,2,4,0,0 };
615         int     freq = (frq >> 8) & 0xf;
616
617         if (freqs[freq] == 0)
618                 printk("%s: Unknown HT link frequency %x\n", name, freq);
619         else
620                 printk("%s: %d MHz on main link, (%d in / %d out) bits width\n",
621                        name, freqs[freq],
622                        bits[(cfg >> 28) & 0x7], bits[(cfg >> 24) & 0x7]);
623 }
624
625 void __init pmac_check_ht_link(void)
626 {
627 #if 0 /* Disabled for now */
628         u32     ufreq, freq, ucfg, cfg;
629         struct device_node *pcix_node;
630         u8      px_bus, px_devfn;
631         struct pci_controller *px_hose;
632
633         (void)in_be32(u3_ht + U3_HT_LINK_COMMAND);
634         ucfg = cfg = in_be32(u3_ht + U3_HT_LINK_CONFIG);
635         ufreq = freq = in_be32(u3_ht + U3_HT_LINK_FREQ);
636         dump_HT_speeds("U3 HyperTransport", cfg, freq);
637
638         pcix_node = of_find_compatible_node(NULL, "pci", "pci-x");
639         if (pcix_node == NULL) {
640                 printk("No PCI-X bridge found\n");
641                 return;
642         }
643         px_hose = pcix_node->phb;
644         px_bus = pcix_node->busno;
645         px_devfn = pcix_node->devfn;
646         
647         early_read_config_dword(px_hose, px_bus, px_devfn, 0xc4, &cfg);
648         early_read_config_dword(px_hose, px_bus, px_devfn, 0xcc, &freq);
649         dump_HT_speeds("PCI-X HT Uplink", cfg, freq);
650         early_read_config_dword(px_hose, px_bus, px_devfn, 0xc8, &cfg);
651         early_read_config_dword(px_hose, px_bus, px_devfn, 0xd0, &freq);
652         dump_HT_speeds("PCI-X HT Downlink", cfg, freq);
653 #endif
654 }