Split up brcm63xx into files/
[librecmc/librecmc.git] / target / linux / brcm63xx-2.6 / files / arch / mips / bcm963xx / setup.c
1 /*
2 <:copyright-gpl 
3  Copyright 2002 Broadcom Corp. All Rights Reserved. 
4  
5  This program is free software; you can distribute it and/or modify it 
6  under the terms of the GNU General Public License (Version 2) as 
7  published by the Free Software Foundation. 
8  
9  This program is distributed in the hope it will be useful, but WITHOUT 
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
11  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License 
12  for more details. 
13  
14  You should have received a copy of the GNU General Public License along 
15  with this program; if not, write to the Free Software Foundation, Inc., 
16  59 Temple Place - Suite 330, Boston MA 02111-1307, USA. 
17 :>
18 */
19 /*
20  * Generic setup routines for Broadcom 963xx MIPS boards
21  */
22
23 #include <linux/autoconf.h>
24 #include <linux/init.h>
25 #include <linux/interrupt.h>
26 #include <linux/kernel.h>
27 #include <linux/kdev_t.h>
28 #include <linux/types.h>
29 #include <linux/console.h>
30 #include <linux/sched.h>
31 #include <linux/mm.h>
32 #include <linux/slab.h>
33 #include <linux/module.h>
34 #include <linux/pm.h>
35
36 #include <asm/addrspace.h>
37 #include <asm/bcache.h>
38 #include <asm/irq.h>
39 #include <asm/time.h>
40 #include <asm/reboot.h>
41 #include <asm/gdb-stub.h>
42
43 extern void brcm_time_init(void);
44 extern unsigned long getMemorySize(void);
45
46 #if defined(CONFIG_BCM96348) && defined(CONFIG_PCI)
47 #include <linux/pci.h>
48 #include <linux/delay.h>
49 #include <bcm_map_part.h>
50 #include <bcmpci.h>
51
52 static volatile MpiRegisters * mpi = (MpiRegisters *)(MPI_BASE);
53 #endif
54
55 /* This function should be in a board specific directory.  For now,
56  * assume that all boards that include this file use a Broadcom chip
57  * with a soft reset bit in the PLL control register.
58  */
59 static void brcm_machine_restart(char *command)
60 {
61     const unsigned long ulSoftReset = 0x00000001;
62     unsigned long *pulPllCtrl = (unsigned long *) 0xfffe0008;
63     *pulPllCtrl |= ulSoftReset;
64 }
65
66 static void brcm_machine_halt(void)
67 {
68     printk("System halted\n");
69     while (1);
70 }
71
72 #if defined(CONFIG_BCM96348) && defined(CONFIG_PCI)
73
74 static void mpi_SetLocalPciConfigReg(uint32 reg, uint32 value)
75 {
76     /* write index then value */
77     mpi->pcicfgcntrl = PCI_CFG_REG_WRITE_EN + reg;;
78     mpi->pcicfgdata = value;
79 }
80
81 static uint32 mpi_GetLocalPciConfigReg(uint32 reg)
82 {
83     /* write index then get value */
84     mpi->pcicfgcntrl = PCI_CFG_REG_WRITE_EN + reg;;
85     return mpi->pcicfgdata;
86 }
87
88 /*
89  * mpi_ResetPcCard: Set/Reset the PcCard
90  */
91 static void mpi_ResetPcCard(int cardtype, BOOL bReset)
92 {
93     if (cardtype == MPI_CARDTYPE_NONE) {
94         return;
95     }
96
97     if (cardtype == MPI_CARDTYPE_CARDBUS) {
98         bReset = ! bReset;
99     }
100
101     if (bReset) {
102         mpi->pcmcia_cntl1 = (mpi->pcmcia_cntl1 & ~PCCARD_CARD_RESET);
103     } else {
104         mpi->pcmcia_cntl1 = (mpi->pcmcia_cntl1 | PCCARD_CARD_RESET);
105     }
106 }
107
108 /*
109  * mpi_ConfigCs: Configure an MPI/EBI chip select
110  */
111 static void mpi_ConfigCs(uint32 cs, uint32 base, uint32 size, uint32 flags)
112 {
113     mpi->cs[cs].base = ((base & 0x1FFFFFFF) | size);
114     mpi->cs[cs].config = flags;
115 }
116
117 /*
118  * mpi_InitPcmciaSpace
119  */
120 static void mpi_InitPcmciaSpace(void)
121 {
122     // ChipSelect 4 controls PCMCIA Memory accesses
123     mpi_ConfigCs(PCMCIA_COMMON_BASE, pcmciaMem, EBI_SIZE_1M, (EBI_WORD_WIDE|EBI_ENABLE));
124     // ChipSelect 5 controls PCMCIA Attribute accesses
125     mpi_ConfigCs(PCMCIA_ATTRIBUTE_BASE, pcmciaAttr, EBI_SIZE_1M, (EBI_WORD_WIDE|EBI_ENABLE));
126     // ChipSelect 6 controls PCMCIA I/O accesses
127     mpi_ConfigCs(PCMCIA_IO_BASE, pcmciaIo, EBI_SIZE_64K, (EBI_WORD_WIDE|EBI_ENABLE));
128
129     mpi->pcmcia_cntl2 = ((PCMCIA_ATTR_ACTIVE << RW_ACTIVE_CNT_BIT) | 
130                          (PCMCIA_ATTR_INACTIVE << INACTIVE_CNT_BIT) | 
131                          (PCMCIA_ATTR_CE_SETUP << CE_SETUP_CNT_BIT) | 
132                          (PCMCIA_ATTR_CE_HOLD << CE_HOLD_CNT_BIT));
133
134     mpi->pcmcia_cntl2 |= (PCMCIA_HALFWORD_EN | PCMCIA_BYTESWAP_DIS);
135 }
136
137 /*
138  * cardtype_vcc_detect: PC Card's card detect and voltage sense connection
139  * 
140  *   CD1#/      CD2#/     VS1#/     VS2#/    Card       Initial Vcc
141  *  CCD1#      CCD2#     CVS1      CVS2      Type
142  *
143  *   GND        GND       open      open     16-bit     5 vdc
144  *
145  *   GND        GND       GND       open     16-bit     3.3 vdc
146  *
147  *   GND        GND       open      GND      16-bit     x.x vdc
148  *
149  *   GND        GND       GND       GND      16-bit     3.3 & x.x vdc
150  *
151  *====================================================================
152  *
153  *   CVS1       GND       CCD1#     open     CardBus    3.3 vdc
154  *
155  *   GND        CVS2      open      CCD2#    CardBus    x.x vdc
156  *
157  *   GND        CVS1      CCD2#     open     CardBus    y.y vdc
158  *
159  *   GND        CVS2      GND       CCD2#    CardBus    3.3 & x.x vdc
160  *
161  *   CVS2       GND       open      CCD1#    CardBus    x.x & y.y vdc
162  *
163  *   GND        CVS1      CCD2#     open     CardBus    3.3, x.x & y.y vdc
164  *
165  */
166 static int cardtype_vcc_detect(void)
167 {
168     uint32 data32;
169     int cardtype;
170
171     cardtype = MPI_CARDTYPE_NONE;
172     mpi->pcmcia_cntl1 = 0x0000A000; // Turn on the output enables and drive
173                                         // the CVS pins to 0.
174     data32 = mpi->pcmcia_cntl1;
175     switch (data32 & 0x00000003)  // Test CD1# and CD2#, see if card is plugged in.
176     {
177     case 0x00000003:  // No Card is in the slot.
178         printk("mpi: No Card is in the PCMCIA slot\n");
179         break;
180
181     case 0x00000002:  // Partial insertion, No CD2#.
182         printk("mpi: Card in the PCMCIA slot partial insertion, no CD2 signal\n");
183         break;
184
185     case 0x00000001:  // Partial insertion, No CD1#.
186         printk("mpi: Card in the PCMCIA slot partial insertion, no CD1 signal\n");
187         break;
188
189     case 0x00000000:
190         mpi->pcmcia_cntl1 = 0x0000A0C0; // Turn off the CVS output enables and
191                                         // float the CVS pins.
192         mdelay(1);
193         data32 = mpi->pcmcia_cntl1;
194         // Read the Register.
195         switch (data32 & 0x0000000C)  // See what is on the CVS pins.
196         {
197         case 0x00000000: // CVS1 and CVS2 are tied to ground, only 1 option.
198             printk("mpi: Detected 3.3 & x.x 16-bit PCMCIA card\n");
199             cardtype = MPI_CARDTYPE_PCMCIA;
200             break;
201           
202         case 0x00000004: // CVS1 is open or tied to CCD1/CCD2 and CVS2 is tied to ground.
203                          // 2 valid voltage options.
204         switch (data32 & 0x00000003)  // Test the values of CCD1 and CCD2.
205         {
206             case 0x00000003:  // CCD1 and CCD2 are tied to 1 of the CVS pins.
207                               // This is not a valid combination.
208                 printk("mpi: Unknown card plugged into slot\n"); 
209                 break;
210       
211             case 0x00000002:  // CCD2 is tied to either CVS1 or CVS2. 
212                 mpi->pcmcia_cntl1 = 0x0000A080; // Drive CVS1 to a 0.
213                 mdelay(1);
214                 data32 = mpi->pcmcia_cntl1;
215                 if (data32 & 0x00000002) { // CCD2 is tied to CVS2, not valid.
216                     printk("mpi: Unknown card plugged into slot\n"); 
217                 } else {                   // CCD2 is tied to CVS1.
218                     printk("mpi: Detected 3.3, x.x and y.y Cardbus card\n");
219                     cardtype = MPI_CARDTYPE_CARDBUS;
220                 }
221                 break;
222                 
223             case 0x00000001: // CCD1 is tied to either CVS1 or CVS2.
224                              // This is not a valid combination.
225                 printk("mpi: Unknown card plugged into slot\n"); 
226                 break;
227                 
228             case 0x00000000:  // CCD1 and CCD2 are tied to ground.
229                 printk("mpi: Detected x.x vdc 16-bit PCMCIA card\n");
230                 cardtype = MPI_CARDTYPE_PCMCIA;
231                 break;
232             }
233             break;
234           
235         case 0x00000008: // CVS2 is open or tied to CCD1/CCD2 and CVS1 is tied to ground.
236                          // 2 valid voltage options.
237             switch (data32 & 0x00000003)  // Test the values of CCD1 and CCD2.
238             {
239             case 0x00000003:  // CCD1 and CCD2 are tied to 1 of the CVS pins.
240                               // This is not a valid combination.
241                 printk("mpi: Unknown card plugged into slot\n"); 
242                 break;
243       
244             case 0x00000002:  // CCD2 is tied to either CVS1 or CVS2.
245                 mpi->pcmcia_cntl1 = 0x0000A040; // Drive CVS2 to a 0.
246                 mdelay(1);
247                 data32 = mpi->pcmcia_cntl1;
248                 if (data32 & 0x00000002) { // CCD2 is tied to CVS1, not valid.
249                     printk("mpi: Unknown card plugged into slot\n"); 
250                 } else {// CCD2 is tied to CVS2.
251                     printk("mpi: Detected 3.3 and x.x Cardbus card\n");
252                     cardtype = MPI_CARDTYPE_CARDBUS;
253                 }
254                 break;
255
256             case 0x00000001: // CCD1 is tied to either CVS1 or CVS2.
257                              // This is not a valid combination.
258                 printk("mpi: Unknown card plugged into slot\n"); 
259                 break;
260
261             case 0x00000000:  // CCD1 and CCD2 are tied to ground.
262                 cardtype = MPI_CARDTYPE_PCMCIA;
263                 printk("mpi: Detected 3.3 vdc 16-bit PCMCIA card\n");
264                 break;
265             }
266             break;
267           
268         case 0x0000000C:  // CVS1 and CVS2 are open or tied to CCD1/CCD2.
269                           // 5 valid voltage options.
270       
271             switch (data32 & 0x00000003)  // Test the values of CCD1 and CCD2.
272             {
273             case 0x00000003:  // CCD1 and CCD2 are tied to 1 of the CVS pins.
274                               // This is not a valid combination.
275                 printk("mpi: Unknown card plugged into slot\n"); 
276                 break;
277       
278             case 0x00000002:  // CCD2 is tied to either CVS1 or CVS2.
279                               // CCD1 is tied to ground.
280                 mpi->pcmcia_cntl1 = 0x0000A040; // Drive CVS2 to a 0.
281                 mdelay(1);
282                 data32 = mpi->pcmcia_cntl1;
283                 if (data32 & 0x00000002) {  // CCD2 is tied to CVS1.
284                     printk("mpi: Detected y.y vdc Cardbus card\n");
285                 } else {                    // CCD2 is tied to CVS2.
286                     printk("mpi: Detected x.x vdc Cardbus card\n");
287                 }
288                 cardtype = MPI_CARDTYPE_CARDBUS;
289                 break;
290       
291             case 0x00000001: // CCD1 is tied to either CVS1 or CVS2.
292                              // CCD2 is tied to ground.
293       
294                 mpi->pcmcia_cntl1 = 0x0000A040; // Drive CVS2 to a 0.
295                 mdelay(1);
296                 data32 = mpi->pcmcia_cntl1;
297                 if (data32 & 0x00000001) {// CCD1 is tied to CVS1.
298                     printk("mpi: Detected 3.3 vdc Cardbus card\n");
299                 } else {                    // CCD1 is tied to CVS2.
300                     printk("mpi: Detected x.x and y.y Cardbus card\n");
301                 }
302                 cardtype = MPI_CARDTYPE_CARDBUS;
303                 break;
304       
305             case 0x00000000:  // CCD1 and CCD2 are tied to ground.
306                 cardtype = MPI_CARDTYPE_PCMCIA;
307                 printk("mpi: Detected 5 vdc 16-bit PCMCIA card\n");
308                 break;
309             }
310             break;
311       
312         default:
313             printk("mpi: Unknown card plugged into slot\n"); 
314             break;
315         
316         }
317     }
318     return cardtype;
319 }
320
321 /*
322  * mpi_DetectPcCard: Detect the plugged in PC-Card
323  * Return: < 0 => Unknown card detected
324  *         0 => No card detected
325  *         1 => 16-bit card detected
326  *         2 => 32-bit CardBus card detected
327  */
328 static int mpi_DetectPcCard(void)
329 {
330     int cardtype;
331
332     cardtype = cardtype_vcc_detect();
333     switch(cardtype) {
334         case MPI_CARDTYPE_PCMCIA:
335             mpi->pcmcia_cntl1 &= ~0x0000e000; // disable enable bits
336             //mpi->pcmcia_cntl1 = (mpi->pcmcia_cntl1 & ~PCCARD_CARD_RESET);
337             mpi->pcmcia_cntl1 |= (PCMCIA_ENABLE | PCMCIA_GPIO_ENABLE);
338             mpi_InitPcmciaSpace();
339             mpi_ResetPcCard(cardtype, FALSE);
340             // Hold card in reset for 10ms
341             mdelay(10);
342             mpi_ResetPcCard(cardtype, TRUE);
343             // Let card come out of reset
344             mdelay(100);
345             break;
346         case MPI_CARDTYPE_CARDBUS:
347             // 8 => CardBus Enable
348             // 1 => PCI Slot Number
349             // C => Float VS1 & VS2
350             mpi->pcmcia_cntl1 = (mpi->pcmcia_cntl1 & 0xFFFF0000) | 
351                                 CARDBUS_ENABLE | 
352                                 (CARDBUS_SLOT << 8)| 
353                                 VS2_OEN |
354                                 VS1_OEN;
355             /* access to this memory window will be to/from CardBus */
356             mpi->l2pmremap1 |= CARDBUS_MEM;
357
358             // Need to reset the Cardbus Card. There's no CardManager to do this, 
359             // and we need to be ready for PCI configuration. 
360             mpi_ResetPcCard(cardtype, FALSE);
361             // Hold card in reset for 10ms
362             mdelay(10);
363             mpi_ResetPcCard(cardtype, TRUE);
364             // Let card come out of reset
365             mdelay(100);
366             break;
367         default:
368             break;
369     }
370     return cardtype;
371 }
372
373 static int mpi_init(void)
374 {
375     unsigned long data;
376     unsigned int chipid;
377     unsigned int chiprev;
378     unsigned int sdramsize;
379
380     chipid  = (PERF->RevID & 0xFFFF0000) >> 16;
381     chiprev = (PERF->RevID & 0xFF);
382     sdramsize = getMemorySize();
383     /*
384      * Init the pci interface 
385      */
386     data = GPIO->GPIOMode; // GPIO mode register
387     data |= GROUP2_PCI | GROUP1_MII_PCCARD; // PCI internal arbiter + Cardbus
388     GPIO->GPIOMode = data; // PCI internal arbiter
389
390     /*
391      * In the BCM6348 CardBus support is defaulted to Slot 0
392      * because there is no external IDSEL for CardBus.  To disable
393      * the CardBus and allow a standard PCI card in Slot 0 
394      * set the cbus_idsel field to 0x1f.
395     */
396     /*
397     uData = mpi->pcmcia_cntl1;
398     uData |= CARDBUS_IDSEL;
399     mpi->pcmcia_cntl1 = uData;
400     */
401     // Setup PCI I/O Window range. Give 64K to PCI I/O
402     mpi->l2piorange = ~(BCM_PCI_IO_SIZE_64KB-1);
403     // UBUS to PCI I/O base address 
404     mpi->l2piobase = BCM_PCI_IO_BASE & BCM_PCI_ADDR_MASK;
405     // UBUS to PCI I/O Window remap
406     mpi->l2pioremap = (BCM_PCI_IO_BASE | MEM_WINDOW_EN);
407
408     // enable PCI related GPIO pins and data swap between system and PCI bus
409     mpi->locbuscntrl = (EN_PCI_GPIO | DIR_U2P_NOSWAP);
410
411     /* Enable 6348 BusMaster and Memory access mode */
412     data = mpi_GetLocalPciConfigReg(PCI_COMMAND);
413     data |= (PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
414     mpi_SetLocalPciConfigReg(PCI_COMMAND, data);
415
416     /* Configure two 16 MByte PCI to System memory regions. */
417     /* These memory regions are used when PCI device is a bus master */
418     /* Accesses to the SDRAM from PCI bus will be "byte swapped" for this region */
419     mpi_SetLocalPciConfigReg(PCI_BASE_ADDRESS_3, BCM_HOST_MEM_SPACE1);
420     mpi->sp0remap = 0x0;
421
422     /* Accesses to the SDRAM from PCI bus will not be "byte swapped" for this region */
423     mpi_SetLocalPciConfigReg(PCI_BASE_ADDRESS_4, BCM_HOST_MEM_SPACE2);
424     mpi->sp1remap = 0x0;
425     mpi->pcimodesel |= (PCI_BAR2_NOSWAP | 0x40);
426
427     if ((chipid == 0x6348) && (chiprev == 0xb0)) {
428         mpi->sp0range = ~(sdramsize-1);
429         mpi->sp1range = ~(sdramsize-1);
430     }
431     /*
432      * Change 6348 PCI Cfg Reg. offset 0x40 to PCI memory read retry count infinity
433      * by set 0 in bit 8~15.  This resolve read Bcm4306 srom return 0xffff in
434      * first read.
435      */
436     data = mpi_GetLocalPciConfigReg(BRCM_PCI_CONFIG_TIMER);
437     data &= ~BRCM_PCI_CONFIG_TIMER_RETRY_MASK;
438     data |= 0x00000080;
439     mpi_SetLocalPciConfigReg(BRCM_PCI_CONFIG_TIMER, data);
440
441     /* enable pci interrupt */
442     mpi->locintstat |= (EXT_PCI_INT << 16);
443
444     mpi_DetectPcCard();
445
446     ioport_resource.start = BCM_PCI_IO_BASE;
447     ioport_resource.end = BCM_PCI_IO_BASE + BCM_PCI_IO_SIZE_64KB;
448
449 #if defined(CONFIG_USB)
450     PERF->blkEnables |= USBH_CLK_EN;
451     mdelay(100);
452     *USBH_NON_OHCI = NON_OHCI_BYTE_SWAP;
453 #endif
454
455     return 0;
456 }
457 #endif
458
459 static int __init brcm63xx_setup(void)
460 {
461     extern int panic_timeout;
462
463     _machine_restart = brcm_machine_restart;
464     _machine_halt = brcm_machine_halt;
465     pm_power_off = brcm_machine_halt;
466
467     board_time_init = brcm_time_init;
468
469     panic_timeout = 5;
470
471 #if defined(CONFIG_BCM96348) && defined(CONFIG_PCI)
472     /* mpi initialization */
473     mpi_init();
474 #endif
475     return 0;
476 }
477
478 void __init plat_mem_setup(void)
479 {
480     brcm63xx_setup();
481 }
482
483 /***************************************************************************
484  * C++ New and delete operator functions
485  ***************************************************************************/
486
487 /* void *operator new(unsigned int sz) */
488 void *_Znwj(unsigned int sz)
489 {
490     return( kmalloc(sz, GFP_KERNEL) );
491 }
492
493 /* void *operator new[](unsigned int sz)*/
494 void *_Znaj(unsigned int sz)
495 {
496     return( kmalloc(sz, GFP_KERNEL) );
497 }
498
499 /* placement new operator */
500 /* void *operator new (unsigned int size, void *ptr) */
501 void *ZnwjPv(unsigned int size, void *ptr)
502 {
503     return ptr;
504 }
505
506 /* void operator delete(void *m) */
507 void _ZdlPv(void *m)
508 {
509     kfree(m);
510 }
511
512 /* void operator delete[](void *m) */
513 void _ZdaPv(void *m)
514 {
515     kfree(m);
516 }
517
518 EXPORT_SYMBOL(_Znwj);
519 EXPORT_SYMBOL(_Znaj);
520 EXPORT_SYMBOL(ZnwjPv);
521 EXPORT_SYMBOL(_ZdlPv);
522 EXPORT_SYMBOL(_ZdaPv);
523