Merge branch 'upstream-fixes' into upstream
[linux-2.6] / arch / sparc64 / kernel / central.c
1 /* $Id: central.c,v 1.15 2001/12/19 00:29:51 davem Exp $
2  * central.c: Central FHC driver for Sunfire/Starfire/Wildfire.
3  *
4  * Copyright (C) 1997, 1999 David S. Miller (davem@redhat.com)
5  */
6
7 #include <linux/kernel.h>
8 #include <linux/types.h>
9 #include <linux/string.h>
10 #include <linux/timer.h>
11 #include <linux/sched.h>
12 #include <linux/delay.h>
13 #include <linux/init.h>
14 #include <linux/bootmem.h>
15
16 #include <asm/page.h>
17 #include <asm/fhc.h>
18 #include <asm/starfire.h>
19
20 struct linux_central *central_bus = NULL;
21 struct linux_fhc *fhc_list = NULL;
22
23 #define IS_CENTRAL_FHC(__fhc)   ((__fhc) == central_bus->child)
24
25 static void central_probe_failure(int line)
26 {
27         prom_printf("CENTRAL: Critical device probe failure at central.c:%d\n",
28                     line);
29         prom_halt();
30 }
31
32 static void central_ranges_init(struct linux_central *central)
33 {
34         struct device_node *dp = central->prom_node;
35         void *pval;
36         int len;
37         
38         central->num_central_ranges = 0;
39         pval = of_get_property(dp, "ranges", &len);
40         if (pval) {
41                 memcpy(central->central_ranges, pval, len);
42                 central->num_central_ranges =
43                         (len / sizeof(struct linux_prom_ranges));
44         }
45 }
46
47 static void fhc_ranges_init(struct linux_fhc *fhc)
48 {
49         struct device_node *dp = fhc->prom_node;
50         void *pval;
51         int len;
52         
53         fhc->num_fhc_ranges = 0;
54         pval = of_get_property(dp, "ranges", &len);
55         if (pval) {
56                 memcpy(fhc->fhc_ranges, pval, len);
57                 fhc->num_fhc_ranges =
58                         (len / sizeof(struct linux_prom_ranges));
59         }
60 }
61
62 /* Range application routines are exported to various drivers,
63  * so do not __init this.
64  */
65 static void adjust_regs(struct linux_prom_registers *regp, int nregs,
66                         struct linux_prom_ranges *rangep, int nranges)
67 {
68         int regc, rngc;
69
70         for (regc = 0; regc < nregs; regc++) {
71                 for (rngc = 0; rngc < nranges; rngc++)
72                         if (regp[regc].which_io == rangep[rngc].ot_child_space)
73                                 break; /* Fount it */
74                 if (rngc == nranges) /* oops */
75                         central_probe_failure(__LINE__);
76                 regp[regc].which_io = rangep[rngc].ot_parent_space;
77                 regp[regc].phys_addr -= rangep[rngc].ot_child_base;
78                 regp[regc].phys_addr += rangep[rngc].ot_parent_base;
79         }
80 }
81
82 /* Apply probed fhc ranges to registers passed, if no ranges return. */
83 void apply_fhc_ranges(struct linux_fhc *fhc,
84                       struct linux_prom_registers *regs,
85                       int nregs)
86 {
87         if (fhc->num_fhc_ranges)
88                 adjust_regs(regs, nregs, fhc->fhc_ranges,
89                             fhc->num_fhc_ranges);
90 }
91
92 /* Apply probed central ranges to registers passed, if no ranges return. */
93 void apply_central_ranges(struct linux_central *central,
94                           struct linux_prom_registers *regs, int nregs)
95 {
96         if (central->num_central_ranges)
97                 adjust_regs(regs, nregs, central->central_ranges,
98                             central->num_central_ranges);
99 }
100
101 void * __init central_alloc_bootmem(unsigned long size)
102 {
103         void *ret;
104
105         ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
106         if (ret != NULL)
107                 memset(ret, 0, size);
108
109         return ret;
110 }
111
112 static unsigned long prom_reg_to_paddr(struct linux_prom_registers *r)
113 {
114         unsigned long ret = ((unsigned long) r->which_io) << 32;
115
116         return ret | (unsigned long) r->phys_addr;
117 }
118
119 static void probe_other_fhcs(void)
120 {
121         struct device_node *dp;
122         struct linux_prom64_registers *fpregs;
123
124         for_each_node_by_name(dp, "fhc") {
125                 struct linux_fhc *fhc;
126                 int board;
127                 u32 tmp;
128
129                 fhc = (struct linux_fhc *)
130                         central_alloc_bootmem(sizeof(struct linux_fhc));
131                 if (fhc == NULL)
132                         central_probe_failure(__LINE__);
133
134                 /* Link it into the FHC chain. */
135                 fhc->next = fhc_list;
136                 fhc_list = fhc;
137
138                 /* Toplevel FHCs have no parent. */
139                 fhc->parent = NULL;
140                 
141                 fhc->prom_node = dp;
142                 fhc_ranges_init(fhc);
143
144                 /* Non-central FHC's have 64-bit OBP format registers. */
145                 fpregs = of_get_property(dp, "reg", NULL);
146                 if (!fpregs)
147                         central_probe_failure(__LINE__);
148
149                 /* Only central FHC needs special ranges applied. */
150                 fhc->fhc_regs.pregs = fpregs[0].phys_addr;
151                 fhc->fhc_regs.ireg = fpregs[1].phys_addr;
152                 fhc->fhc_regs.ffregs = fpregs[2].phys_addr;
153                 fhc->fhc_regs.sregs = fpregs[3].phys_addr;
154                 fhc->fhc_regs.uregs = fpregs[4].phys_addr;
155                 fhc->fhc_regs.tregs = fpregs[5].phys_addr;
156
157                 board = of_getintprop_default(dp, "board#", -1);
158                 fhc->board = board;
159
160                 tmp = upa_readl(fhc->fhc_regs.pregs + FHC_PREGS_JCTRL);
161                 if ((tmp & FHC_JTAG_CTRL_MENAB) != 0)
162                         fhc->jtag_master = 1;
163                 else
164                         fhc->jtag_master = 0;
165
166                 tmp = upa_readl(fhc->fhc_regs.pregs + FHC_PREGS_ID);
167                 printk("FHC(board %d): Version[%x] PartID[%x] Manuf[%x] %s\n",
168                        board,
169                        (tmp & FHC_ID_VERS) >> 28,
170                        (tmp & FHC_ID_PARTID) >> 12,
171                        (tmp & FHC_ID_MANUF) >> 1,
172                        (fhc->jtag_master ? "(JTAG Master)" : ""));
173                 
174                 /* This bit must be set in all non-central FHC's in
175                  * the system.  When it is clear, this identifies
176                  * the central board.
177                  */
178                 tmp = upa_readl(fhc->fhc_regs.pregs + FHC_PREGS_CTRL);
179                 tmp |= FHC_CONTROL_IXIST;
180                 upa_writel(tmp, fhc->fhc_regs.pregs + FHC_PREGS_CTRL);
181         }
182 }
183
184 static void probe_clock_board(struct linux_central *central,
185                               struct linux_fhc *fhc,
186                               struct device_node *fp)
187 {
188         struct device_node *dp;
189         struct linux_prom_registers cregs[3], *pr;
190         int nslots, tmp, nregs;
191
192         dp = fp->child;
193         while (dp) {
194                 if (!strcmp(dp->name, "clock-board"))
195                         break;
196                 dp = dp->sibling;
197         }
198         if (!dp)
199                 central_probe_failure(__LINE__);
200
201         pr = of_get_property(dp, "reg", &nregs);
202         if (!pr)
203                 central_probe_failure(__LINE__);
204
205         memcpy(cregs, pr, nregs);
206         nregs /= sizeof(struct linux_prom_registers);
207
208         apply_fhc_ranges(fhc, &cregs[0], nregs);
209         apply_central_ranges(central, &cregs[0], nregs);
210         central->cfreg = prom_reg_to_paddr(&cregs[0]);
211         central->clkregs = prom_reg_to_paddr(&cregs[1]);
212
213         if (nregs == 2)
214                 central->clkver = 0UL;
215         else
216                 central->clkver = prom_reg_to_paddr(&cregs[2]);
217
218         tmp = upa_readb(central->clkregs + CLOCK_STAT1);
219         tmp &= 0xc0;
220         switch(tmp) {
221         case 0x40:
222                 nslots = 16;
223                 break;
224         case 0xc0:
225                 nslots = 8;
226                 break;
227         case 0x80:
228                 if (central->clkver != 0UL &&
229                    upa_readb(central->clkver) != 0) {
230                         if ((upa_readb(central->clkver) & 0x80) != 0)
231                                 nslots = 4;
232                         else
233                                 nslots = 5;
234                         break;
235                 }
236         default:
237                 nslots = 4;
238                 break;
239         };
240         central->slots = nslots;
241         printk("CENTRAL: Detected %d slot Enterprise system. cfreg[%02x] cver[%02x]\n",
242                central->slots, upa_readb(central->cfreg),
243                (central->clkver ? upa_readb(central->clkver) : 0x00));
244 }
245
246 static void ZAP(unsigned long iclr, unsigned long imap)
247 {
248         u32 imap_tmp;
249
250         upa_writel(0, iclr);
251         upa_readl(iclr);
252         imap_tmp = upa_readl(imap);
253         imap_tmp &= ~(0x80000000);
254         upa_writel(imap_tmp, imap);
255         upa_readl(imap);
256 }
257
258 static void init_all_fhc_hw(void)
259 {
260         struct linux_fhc *fhc;
261
262         for (fhc = fhc_list; fhc != NULL; fhc = fhc->next) {
263                 u32 tmp;
264
265                 /* Clear all of the interrupt mapping registers
266                  * just in case OBP left them in a foul state.
267                  */
268                 ZAP(fhc->fhc_regs.ffregs + FHC_FFREGS_ICLR,
269                     fhc->fhc_regs.ffregs + FHC_FFREGS_IMAP);
270                 ZAP(fhc->fhc_regs.sregs + FHC_SREGS_ICLR,
271                     fhc->fhc_regs.sregs + FHC_SREGS_IMAP);
272                 ZAP(fhc->fhc_regs.uregs + FHC_UREGS_ICLR,
273                     fhc->fhc_regs.uregs + FHC_UREGS_IMAP);
274                 ZAP(fhc->fhc_regs.tregs + FHC_TREGS_ICLR,
275                     fhc->fhc_regs.tregs + FHC_TREGS_IMAP);
276
277                 /* Setup FHC control register. */
278                 tmp = upa_readl(fhc->fhc_regs.pregs + FHC_PREGS_CTRL);
279
280                 /* All non-central boards have this bit set. */
281                 if (! IS_CENTRAL_FHC(fhc))
282                         tmp |= FHC_CONTROL_IXIST;
283
284                 /* For all FHCs, clear the firmware synchronization
285                  * line and both low power mode enables.
286                  */
287                 tmp &= ~(FHC_CONTROL_AOFF | FHC_CONTROL_BOFF |
288                          FHC_CONTROL_SLINE);
289
290                 upa_writel(tmp, fhc->fhc_regs.pregs + FHC_PREGS_CTRL);
291                 upa_readl(fhc->fhc_regs.pregs + FHC_PREGS_CTRL);
292         }
293
294 }
295
296 void central_probe(void)
297 {
298         struct linux_prom_registers fpregs[6], *pr;
299         struct linux_fhc *fhc;
300         struct device_node *dp, *fp;
301         int err;
302
303         dp = of_find_node_by_name(NULL, "central");
304         if (!dp) {
305                 if (this_is_starfire)
306                         starfire_cpu_setup();
307                 return;
308         }
309
310         /* Ok we got one, grab some memory for software state. */
311         central_bus = (struct linux_central *)
312                 central_alloc_bootmem(sizeof(struct linux_central));
313         if (central_bus == NULL)
314                 central_probe_failure(__LINE__);
315
316         fhc = (struct linux_fhc *)
317                 central_alloc_bootmem(sizeof(struct linux_fhc));
318         if (fhc == NULL)
319                 central_probe_failure(__LINE__);
320
321         /* First init central. */
322         central_bus->child = fhc;
323         central_bus->prom_node = dp;
324         central_ranges_init(central_bus);
325
326         /* And then central's FHC. */
327         fhc->next = fhc_list;
328         fhc_list = fhc;
329
330         fhc->parent = central_bus;
331         fp = dp->child;
332         while (fp) {
333                 if (!strcmp(fp->name, "fhc"))
334                         break;
335                 fp = fp->sibling;
336         }
337         if (!fp)
338                 central_probe_failure(__LINE__);
339
340         fhc->prom_node = fp;
341         fhc_ranges_init(fhc);
342
343         /* Now, map in FHC register set. */
344         pr = of_get_property(fp, "reg", NULL);
345         if (!pr)
346                 central_probe_failure(__LINE__);
347         memcpy(fpregs, pr, sizeof(fpregs));
348
349         apply_central_ranges(central_bus, &fpregs[0], 6);
350         
351         fhc->fhc_regs.pregs = prom_reg_to_paddr(&fpregs[0]);
352         fhc->fhc_regs.ireg = prom_reg_to_paddr(&fpregs[1]);
353         fhc->fhc_regs.ffregs = prom_reg_to_paddr(&fpregs[2]);
354         fhc->fhc_regs.sregs = prom_reg_to_paddr(&fpregs[3]);
355         fhc->fhc_regs.uregs = prom_reg_to_paddr(&fpregs[4]);
356         fhc->fhc_regs.tregs = prom_reg_to_paddr(&fpregs[5]);
357
358         /* Obtain board number from board status register, Central's
359          * FHC lacks "board#" property.
360          */
361         err = upa_readl(fhc->fhc_regs.pregs + FHC_PREGS_BSR);
362         fhc->board = (((err >> 16) & 0x01) |
363                       ((err >> 12) & 0x0e));
364
365         fhc->jtag_master = 0;
366
367         /* Attach the clock board registers for CENTRAL. */
368         probe_clock_board(central_bus, fhc, fp);
369
370         err = upa_readl(fhc->fhc_regs.pregs + FHC_PREGS_ID);
371         printk("FHC(board %d): Version[%x] PartID[%x] Manuf[%x] (CENTRAL)\n",
372                fhc->board,
373                ((err & FHC_ID_VERS) >> 28),
374                ((err & FHC_ID_PARTID) >> 12),
375                ((err & FHC_ID_MANUF) >> 1));
376
377         probe_other_fhcs();
378
379         init_all_fhc_hw();
380 }
381
382 static __inline__ void fhc_ledblink(struct linux_fhc *fhc, int on)
383 {
384         u32 tmp;
385
386         tmp = upa_readl(fhc->fhc_regs.pregs + FHC_PREGS_CTRL);
387
388         /* NOTE: reverse logic on this bit */
389         if (on)
390                 tmp &= ~(FHC_CONTROL_RLED);
391         else
392                 tmp |= FHC_CONTROL_RLED;
393         tmp &= ~(FHC_CONTROL_AOFF | FHC_CONTROL_BOFF | FHC_CONTROL_SLINE);
394
395         upa_writel(tmp, fhc->fhc_regs.pregs + FHC_PREGS_CTRL);
396         upa_readl(fhc->fhc_regs.pregs + FHC_PREGS_CTRL);
397 }
398
399 static __inline__ void central_ledblink(struct linux_central *central, int on)
400 {
401         u8 tmp;
402
403         tmp = upa_readb(central->clkregs + CLOCK_CTRL);
404
405         /* NOTE: reverse logic on this bit */
406         if (on)
407                 tmp &= ~(CLOCK_CTRL_RLED);
408         else
409                 tmp |= CLOCK_CTRL_RLED;
410
411         upa_writeb(tmp, central->clkregs + CLOCK_CTRL);
412         upa_readb(central->clkregs + CLOCK_CTRL);
413 }
414
415 static struct timer_list sftimer;
416 static int led_state;
417
418 static void sunfire_timer(unsigned long __ignored)
419 {
420         struct linux_fhc *fhc;
421
422         central_ledblink(central_bus, led_state);
423         for (fhc = fhc_list; fhc != NULL; fhc = fhc->next)
424                 if (! IS_CENTRAL_FHC(fhc))
425                         fhc_ledblink(fhc, led_state);
426         led_state = ! led_state;
427         sftimer.expires = jiffies + (HZ >> 1);
428         add_timer(&sftimer);
429 }
430
431 /* After PCI/SBUS busses have been probed, this is called to perform
432  * final initialization of all FireHose Controllers in the system.
433  */
434 void firetruck_init(void)
435 {
436         struct linux_central *central = central_bus;
437         u8 ctrl;
438
439         /* No central bus, nothing to do. */
440         if (central == NULL)
441                 return;
442
443         /* OBP leaves it on, turn it off so clock board timer LED
444          * is in sync with FHC ones.
445          */
446         ctrl = upa_readb(central->clkregs + CLOCK_CTRL);
447         ctrl &= ~(CLOCK_CTRL_RLED);
448         upa_writeb(ctrl, central->clkregs + CLOCK_CTRL);
449
450         led_state = 0;
451         init_timer(&sftimer);
452         sftimer.data = 0;
453         sftimer.function = &sunfire_timer;
454         sftimer.expires = jiffies + (HZ >> 1);
455         add_timer(&sftimer);
456 }