git repos for lemote 502 board
[pmon-gdium.git] / Targets / Bonito / Bonito / start.S
blobd7032d689d422725f3f1abe5286a2a506f03aa2a
1 /*      $Id: start.S,v 1.1.1.1 2006/09/14 01:59:08 root Exp $ */
3 /*
4  * Copyright (c) 2001 Opsycon AB  (www.opsycon.se)
5  * 
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by Opsycon AB, Sweden.
17  * 4. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
21  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
24  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  */
34 #ifndef _KERNEL
35 #define _KERNEL
36 #endif
38 #include <asm.h>
39 #include <regnum.h>
40 #include <cpu.h>
41 #include <pte.h>
44 #include "pmon/dev/ns16550.h"
45 #include "target/i82371eb.h"
46 #include "target/prid.h"
47 #include "target/sbd.h"
48 #include "target/bonito.h"
49 #include "target/i8254.h"
50 #include "target/pc97307.h"
51 #include "target/isapnpreg.h"
52 #define DEBUG_LOCORE
53 #undef  DEBUG_DIMM_SPD
54 #ifdef DEBUG_LOCORE
55 #define TTYDBG(x) \
56         .rdata;98: .asciz x; .text; la a0, 98b; bal stringserial; nop
57 #else
58 #define TTYDBG(x)
59 #endif
60 #define PRINTSTR(x) \
61         .rdata;98: .asciz x; .text; la a0, 98b; bal stringserial; nop
63 #define CONFIG_CACHE_64K_4WAY 1
65 #define tmpsize         s1
66 #define msize           s2
67 #define sdShape         s3
68 #define bonito          s4
69 #define dbg             s5
70 #define sdCfg           s6
72 #define CFG_IB 0x00000020
73 #define CFG_DB 0x00000010
74 #define CFG_C_WBACK 3
75 #define CFG_BE 0x00008000
76 #define CFG_EPMASK 0x0f000000
77 #define CFG_EPD 0x00000000
78 #define CFG_EM_R4K 0x00000000
79 #define CFG_EMMASK 0x00c00000
80 #define CFG_AD 0x00800000
82 #define CP0_CONFIG $16
83 #define CP0_TAGLO  $28
84 #define CP0_TAGHI  $29
86 #define DDR100 0x04041091
87 #define DDR266 0x0410435e
88 #define DDR300 0x041453df
90 #if 0
91 #define DEBUG_WR(num) \
92 li v0,0xbfe00170; \
93 li v1,num; \
94 sw v1,(v0);
95 #else
96 #define DEBUG_WR(num) 
97 #endif
100  *   Register usage:
102  *      s0      link versus load offset, used to relocate absolute adresses.
103  *      s1      free
104  *      s2      memory size.
105  *      s3      sdShape.
106  *      s4      Bonito base address.
107  *      s5      dbg.
108  *      s6      sdCfg.
109  *      s7      rasave.
110  *      s8      L3 Cache size.
111  */
114         .set    noreorder
115         .globl  _start
116         .globl  start
117         .globl  __main
118 _start:
119 start:
120         .globl  stack
121 stack = start - 0x4000          /* Place PMON stack below PMON start in RAM */
123 /* NOTE!! Not more that 16 instructions here!!! Right now it's FULL! */
124         mtc0    zero, COP_0_STATUS_REG
125         mtc0    zero, COP_0_CAUSE_REG
126         li      t0, SR_BOOT_EXC_VEC     /* Exception to Boostrap Location */
127         mtc0    t0, COP_0_STATUS_REG
128         la      sp, stack
129         la      gp, _gp
131         bal     uncached                /* Switch to uncached address space */
132         nop
134         bal     locate                  /* Get current execute address */
135         nop
137 uncached:
138         or      ra, UNCACHED_MEMORY_ADDR
139         j       ra
140         nop
143  *  Reboot vector usable from outside pmon.
144  */
145         .align  8
146 ext_map_and_reboot:
147         bal     CPU_TLBClear
148         nop
150         li      a0, 0xc0000000
151         li      a1, 0x40000000
152         bal     CPU_TLBInit
153         nop
154         la      v0, tgt_reboot
155         la      v1, start
156         subu    v0, v1
157         lui     v1, 0xffc0
158         addu    v0, v1
159         jr      v0
160         nop
163  *  Exception vectors here for rom, before we are up and running. Catch
164  *  whatever comes up before we have a fully fledged exception handler.
165  */
166         .align  9                       /* bfc00200 */
167         la      a0, v200_msg
168         bal     stringserial
169         nop
170         b       exc_common
172         .align  7                       /* bfc00280 */
173         la      a0, v280_msg
174         bal     stringserial
175         nop
176         b       exc_common
178 /* Cache error */
179         .align  8                       /* bfc00300 */
180         PRINTSTR("\r\nPANIC! Unexpected Cache Error exception! ")
181         mfc0    a0, COP_0_CACHE_ERR
182         bal     hexserial
183         nop
184         b       exc_common
186 /* General exception */
187         .align  7                       /* bfc00380 */
188         la      a0, v380_msg
189         bal     stringserial
190         nop
191         b       exc_common
192         
193         .align  8                       /* bfc00400 */
194         la      a0, v400_msg
195         bal     stringserial
196         nop
199         
200 exc_common:
201         PRINTSTR("\r\nCAUSE=")
202         mfc0    a0, COP_0_CAUSE_REG
203         bal     hexserial
204         nop
205         PRINTSTR("\r\nSTATUS=")
206         mfc0    a0, COP_0_STATUS_REG
207         bal     hexserial
208         nop
209         PRINTSTR("\r\nERRORPC=")
210         mfc0    a0, COP_0_ERROR_PC
211         bal     hexserial
212         nop
213         PRINTSTR("\r\nEPC=")
214         mfc0    a0, COP_0_EXC_PC
215         bal     hexserial
216         nop
217         PRINTSTR("\r\nDERR0=")
218         cfc0    a0, COP_0_DERR_0
219         bal     hexserial
220         nop
221         PRINTSTR("\r\nDERR1=")
222         cfc0    a0, COP_0_DERR_1
223         bal     hexserial
224         nop
226 //      b       ext_map_and_reboot
227         nop
228 #if 1
229         .align 8
230                 nop
231         .align 8
232         .word read
233         .word write
234         .word open
235         .word close
236         .word nullfunction
237         .word printf
238         .word vsprintf
239         .word nullfunction
240         .word nullfunction
241         .word getenv
242         .word nullfunction
243         .word nullfunction
244         .word nullfunction
245         .word nullfunction
246 #endif
249  *  We get here from executing a bal to get the PC value of the current execute
250  *  location into ra. Check to see if we run from ROM or if this is ramloaded.
251  */
252 locate:
253         la      s0,start
254         subu    s0,ra,s0
255         and     s0,0xffff0000
257         li      t0,SR_BOOT_EXC_VEC
258         mtc0    t0,COP_0_STATUS_REG
259         mtc0    zero,COP_0_CAUSE_REG
260         .set noreorder
262         li      bonito,PHYS_TO_UNCACHED(BONITO_REG_BASE)
265 #define MOD_MASK        0x00000003
266 #define MOD_B           0x00000000 /* byte "modifier" */
267 #define MOD_H           0x00000001 /* halfword "modifier" */
268 #define MOD_W           0x00000002 /* word "modifier" */
269 #if __mips64
270 #       define MOD_D            0x00000003 /* doubleword "modifier" */
271 #endif
273 #define OP_MASK         0x000000fc
274 #define OP_EXIT         0x00000000 /* exit (status) */
275 #define OP_DELAY        0x00000008 /* delay (cycles) */
276 #define OP_RD           0x00000010 /* read (addr) */
277 #define OP_WR           0x00000014 /* write (addr, val) */
278 #define OP_RMW          0x00000018 /* read-modify-write (addr, and, or) */
279 #define OP_WAIT         0x00000020 /* wait (addr, mask, value) */
281 #define WR_INIT(mod,addr,val) \
282         .word   OP_WR|mod,PHYS_TO_UNCACHED(addr);\
283         .word   (val),0
284         
285 #define RD_INIT(mod,addr) \
286         .word   OP_RD|mod,PHYS_TO_UNCACHED(addr);\
287         .word   0,0
288         
289 #define RMW_INIT(mod,addr,and,or) \
290         .word   OP_RMW|mod,PHYS_TO_UNCACHED(addr);\
291         .word   (and),(or)
292         
293 #define WAIT_INIT(mod,addr,and,or) \
294         .word   OP_WAIT|mod,PHYS_TO_UNCACHED(addr);\
295         .word   (mask),(val)
297 #define DELAY_INIT(cycles) \
298         .word   OP_DELAY,(cycles);\
299         .word   0,0
300         
301 #define EXIT_INIT(status) \
302         .word   OP_EXIT,(status);\
303         .word   0,0
305 #define BONITO_INIT(r,v) WR_INIT(MOD_W,BONITO_BASE+/**/r,v)
306 #define BONITO_BIS(r,b) RMW_INIT(MOD_W,BONITO_BASE+(r),~0,b)
307 #define BONITO_BIC(r,b) RMW_INIT(MOD_W,BONITO_BASE+(r),~(b),0)
308 #define BONITO_RMW(r,c,s) RMW_INIT(MOD_W,BONITO_BASE+(r),~(c),s)
309         
310 #define CFGADDR(idsel,function,reg) ((1<<(11+(idsel)))+((function)<<8)+(reg))
311 #define _ISABWR_INIT(mod,function,isabreg,val) \
312         WR_INIT(MOD_W,BONITO_BASE+BONITO_PCIMAP_CFG,CFGADDR(PCI_IDSEL_I82371,function,isabreg)>>16) ; \
313         RD_INIT(MOD_W,BONITO_BASE+BONITO_PCIMAP_CFG) ; \
314         WR_INIT(mod,PCI_CFG_SPACE+(CFGADDR(PCI_IDSEL_I82371,function,isabreg)&0xffff),val)
315         
316 #define _ISABRD_INIT(mod,function,isabreg) \
317         WR_INIT(MOD_W,BONITO_BASE+BONITO_PCIMAP_CFG,CFGADDR(PCI_IDSEL_I82371,function,isabreg)>>16) ; \
318         RD_INIT(MOD_W,BONITO_BASE+BONITO_PCIMAP_CFG) ; \
319         RD_INIT(mod,PCI_CFG_SPACE+(CFGADDR(PCI_IDSEL_I82371,function,isabreg)&0xffff))
322 #define _ISAWR_INIT(isareg,val) \
323         WR_INIT(MOD_B,PCI_IO_SPACE+(isareg),val)
324         
325 #define _ISARD_INIT(isareg) \
326         RD_INIT(MOD_B,PCI_IO_SPACE+(isareg))
327         
329 #define ISABBWR_INIT(function,isabreg,val) \
330         _ISABWR_INIT(MOD_B,function,(isabreg),val)
331 #define ISABHWR_INIT(function,isabreg,val) \
332         _ISABWR_INIT(MOD_H,function,(isabreg),val)
333 #define ISABWWR_INIT(function,isabreg,val) \
334         _ISABWR_INIT(MOD_W,function,isabreg,val)
335 #define ISAWR_INIT(isareg,val) \
336         _ISAWR_INIT(isareg,val)
337 #define ISARD_INIT(isareg) \
338         _ISARD_INIT(isareg)
340         bal     1f      
341         nop
343         /* bonito endianess */
344         BONITO_BIC(BONITO_BONPONCFG,BONITO_BONPONCFG_CPUBIGEND)
345         BONITO_BIC(BONITO_BONGENCFG,BONITO_BONGENCFG_BYTESWAP|BONITO_BONGENCFG_MSTRBYTESWAP)
346         BONITO_BIS(BONITO_BONPONCFG, BONITO_BONPONCFG_IS_ARBITER)
347         
348         /*
349          * In certain situations it is possible for the Bonito ASIC
350          * to come up with the PCI registers uninitialised, so do them here
351          */
352 #define PCI_CLASS_BRIDGE                0x06
353 #define PCI_CLASS_SHIFT                 24
354 #define PCI_SUBCLASS_BRIDGE_HOST        0x00
355 #define PCI_SUBCLASS_SHIFT              16
356 #define PCI_COMMAND_IO_ENABLE           0x00000001
357 #define PCI_COMMAND_MEM_ENABLE          0x00000002
358 #define PCI_COMMAND_MASTER_ENABLE       0x00000004
359 #define PCI_COMMAND_STATUS_REG          0x04
360 #define PCI_MAP_IO                      0X00000001
361 #define PCI_DEV_I82371                  17
362 #define PCI_CFG_SPACE                   BONITO_PCICFG_BASE
364         BONITO_INIT(BONITO_PCICLASS,(PCI_CLASS_BRIDGE << PCI_CLASS_SHIFT) | (PCI_SUBCLASS_BRIDGE_HOST << PCI_SUBCLASS_SHIFT))
365         BONITO_INIT(BONITO_PCICMD, BONITO_PCICMD_PERR_CLR|BONITO_PCICMD_SERR_CLR|BONITO_PCICMD_MABORT_CLR|BONITO_PCICMD_MTABORT_CLR|BONITO_PCICMD_TABORT_CLR|BONITO_PCICMD_MPERR_CLR)
366         BONITO_INIT(BONITO_PCILTIMER, 0)
367         BONITO_INIT(BONITO_PCIBASE0, 0)
368         BONITO_INIT(BONITO_PCIBASE1, 0)
369         BONITO_INIT(BONITO_PCIBASE2, 0)
370         BONITO_INIT(BONITO_PCIEXPRBASE, 0)
371         BONITO_INIT(BONITO_PCIINT, 0)
373         BONITO_BIS(BONITO_PCICMD, BONITO_PCICMD_PERRRESPEN)
374         
375         BONITO_BIS(BONITO_PCICMD, PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE|PCI_COMMAND_MASTER_ENABLE)
376         
377         /* enable i/o buffer cache and other go faster bits */
378         BONITO_BIS(BONITO_BONGENCFG, \
379                         BONITO_BONGENCFG_BUSERREN| \
380                         BONITO_BONGENCFG_PREFETCHEN| \
381                         BONITO_BONGENCFG_WBEHINDEN| \
382                         BONITO_BONGENCFG_PCIQUEUE| \
383                         BONITO_BONGENCFG_SNOOPEN)
384         
385        BONITO_BIC(BONITO_BONGENCFG, 0x80)  #½ûÖ¹iobc
387 #      BONITO_BIS(BONITO_BONGENCFG, BONITO_BONGENCFG_BUSERREN)
389         /* Set debug mode */
390         BONITO_BIS(BONITO_BONGENCFG, BONITO_BONGENCFG_DEBUGMODE)
392         /******** added to void init southbridge*/
393 #if 1
394 #if 0
395         /* zhb init USB */
396         ISABWWR_INIT(2, 0x20, 0x8040|PCI_MAP_IO)
397         ISABWWR_INIT(2, PCI_COMMAND_STATUS_REG, 5)
398 #endif
399 #if 1
400         /* zhb init floppy-disk */
401         ISABWWR_INIT(3, I82371_PCI3_DEVRESD, 0x1800)
402         ISABWWR_INIT(3, I82371_PCI3_DEVRESB, 0x20000000)
403 #endif
404 #if 1
405         /* Turn most special purpose pins into GPIO; set ISA mode */
406         ISABWWR_INIT(0, I82371_GENCFG, I82371_GENCFG_CFG)
407         
408         /* Set the SMB base address */
409         ISABWWR_INIT(3, I82371_PCI3_SMBBA, SMB_PORT|PCI_MAP_IO)
410         /* enable the host controller */
411         ISABBWR_INIT(3, I82371_PCI3_SMBHSTCFG, I82371_PCI3_SMB_HST_EN)
412         /* enable the SMB IO ports */
413         ISABBWR_INIT(3, PCI_COMMAND_STATUS_REG, PCI_COMMAND_IO_ENABLE)
414         
415         ISABWWR_INIT(3, I82371_PCI3_PMBA, PM_PORT|PCI_MAP_IO) /*notice*/
417         ISABBWR_INIT(3, I82371_PCI3_PMREGMISC, 0x01)
418 #endif
419 #if 1
420         /* disable RTC & KBD chip selects */
421         ISABHWR_INIT(0, I82371_XBCS, 0)
422         
423         /* Enable PCI 2.1 timing support */
424         ISABBWR_INIT(0, I82371_DLC, I82371_DLC_DT /* | I82371_DLC_PR */ | I82371_DLC_USBPR | I82371_DLC_DTTE)
426         /* Set top of memory to 16MB, so all ISA bus master & DMA
427            accesses are forwarded to PCI mem space
428          */
429         ISABBWR_INIT(0, I82371_TOM, I82371_TOM_TOM(16) | I82371_TOM_FWD_LBIOS | I82371_TOM_FWD_AB | I82371_TOM_FWD_89)
430         
431         
432 /* 15us ISA bus refresh clock */
433 #define ISAREFRESH (PT_CRYSTAL/(1000000/15))
434         ISARD_INIT(CTC_PORT+PT_CONTROL)
435         
436         /* program i8254 ISA refresh counter */
437         ISAWR_INIT(CTC_PORT+PT_CONTROL,PTCW_SC(PT_REFRESH)|PTCW_16B|PTCW_MODE(MODE_RG))
438         ISAWR_INIT(CTC_PORT+PT_REFRESH, ISAREFRESH & 0xff)
439         ISAWR_INIT(CTC_PORT+PT_REFRESH, ISAREFRESH >> 8)
441         /* program ISA ICU */
442         ISAWR_INIT(ICU1_PORT,  0x11) /* ICW1 */
443         ISAWR_INIT(ICU1_PORT+1,0x00) /* ICW2: vector */
444         ISAWR_INIT(ICU1_PORT+1,0x04) /* ICW3: cascade on IRQ2 */
445         ISAWR_INIT(ICU1_PORT+1,0x01) /* ICW4: 8086 mode */
446         ISAWR_INIT(ICU1_PORT+1,0xff) /* OCW1: mask all */
448         ISAWR_INIT(ICU2_PORT,  0x11) /* ICW1 */
449         ISAWR_INIT(ICU2_PORT+1,0x08) /* ICW2: vector */
450         ISAWR_INIT(ICU2_PORT+1,0x02) /* ICW3:  */
451         ISAWR_INIT(ICU2_PORT+1,0x01) /* ICW4: 8086 mode */
452         ISAWR_INIT(ICU2_PORT+1,0xff) /* OCW1: mask all */
453                 
454         ISAWR_INIT(ICU1_PORT+1,~(1<<2)) /* enable IRQ2 */
455         /* set up ISA devices */
456         
457         /* select logical device 1 (mouse) */
458         ISAWR_INIT(ISAPNP_MBADDR,ISAPNP_LOGICAL_DEV_NUM)
459         ISAWR_INIT(ISAPNP_MBDATA,1)
460         ISAWR_INIT(ISAPNP_MBADDR,ISAPNP_ACTIVATE)
461         ISAWR_INIT(ISAPNP_MBDATA,1)
462                                                                                
463         /* select logical device 4 (parallel) */
464         ISAWR_INIT(ISAPNP_MBADDR,ISAPNP_LOGICAL_DEV_NUM)
465         ISAWR_INIT(ISAPNP_MBDATA,4)
466         ISAWR_INIT(ISAPNP_MBADDR,ISAPNP_IO_DESC0+ISAPNP_IO_BASE_15_8)
467         ISAWR_INIT(ISAPNP_MBDATA,(ECP_PORT>>8) & 0xff)
468         ISAWR_INIT(ISAPNP_MBADDR,ISAPNP_IO_DESC0+ISAPNP_IO_BASE_7_0)
469         ISAWR_INIT(ISAPNP_MBDATA,ECP_PORT & 0xff)
470         ISAWR_INIT(ISAPNP_MBADDR,ISAPNP_IRQ_DESC0+ISAPNP_IRQ_CONTROL)
471         ISAWR_INIT(ISAPNP_MBDATA,ISAPNP_IRQ_HIGH)
472         ISAWR_INIT(ISAPNP_MBADDR,ISAPNP_ACTIVATE)
473         ISAWR_INIT(ISAPNP_MBDATA,1)
474                                                                                
475         /* select logical device 5 (COM2) */
476         ISAWR_INIT(ISAPNP_MBADDR,ISAPNP_LOGICAL_DEV_NUM)
477         ISAWR_INIT(ISAPNP_MBDATA,5)
478         ISAWR_INIT(ISAPNP_MBADDR,ISAPNP_ACTIVATE)
479         ISAWR_INIT(ISAPNP_MBDATA,1)
481         /* select logical device 6 (COM1) */
482         ISAWR_INIT(ISAPNP_MBADDR,ISAPNP_LOGICAL_DEV_NUM)
483         ISAWR_INIT(ISAPNP_MBDATA,6)
484         ISAWR_INIT(ISAPNP_MBADDR,ISAPNP_ACTIVATE)
485         ISAWR_INIT(ISAPNP_MBDATA,1)
486 #endif
487 #endif                                                                       
489                                                                         
490         EXIT_INIT(0)
492 #define Init_Op 0
493 #define Init_A0 4
494 #define Init_A1 8
495 #define Init_A2 12
496 #define Init_Size       16
498 1:      move a0,ra
499 reginit:                        /* local name */
500         lw      t3, Init_Op(a0)
501         lw      t0, Init_A0(a0)
502         and     t4,t3,OP_MASK
503         
504         
505         /* 
506          * EXIT(STATUS) 
507          */
508         bne     t4, OP_EXIT, 8f
509         nop
510         move    v0,t0
511         b       .done
512         nop
513         
514         /* 
515          * DELAY(CYCLES) 
516          */
517 8:      bne     t4, OP_DELAY, 8f
518         nop
519 1:      bnez    t0,1b
520         subu    t0,1
521         b       .next
522         nop     
523         /* 
524          * READ(ADDR) 
525          */
526 8:      bne     t4,OP_RD,8f
527         nop
528         and     t4,t3,MOD_MASK
529         
530         bne     t4,MOD_B,1f
531         nop
532         lbu     t5,0(t0)
533         b       .next
534         nop
535 1:      bne     t4,MOD_H,1f
536         nop
537         lhu     t5,0(t0)
538         b       .next
539         nop
540 1:      bne     t4,MOD_W,1f
541         nop
542 #if __mips64
543         lwu     t5,0(t0)
544 #else 
545         lw      t5,0(t0)
546 #endif
547         b       .next
548         nop
549 1:      
550 #if __mips64
551         lw      t5,0(t0)
552         b       .next
553         nop
554 #else
555         b       .fatal
556         nop
557 #endif
558         
559         /* 
560          * WRITE(ADDR,VAL) 
561          */
562 8:      bne     t4,OP_WR,8f
563         nop
564         lw      t1,Init_A1(a0)
565         and     t4,t3,MOD_MASK
566         
567         bne     t4,MOD_B,1f
568         nop
569         sb      t1,0(t0)
570         b       .next
571         nop
572 1:      bne     t4,MOD_H,1f
573         nop
574         sh      t1,0(t0)
575         b       .next
576         nop
577 1:      bne     t4,MOD_W,1f
578         nop
579         sw      t1,0(t0)
580         b       .next
581         nop
582         
583 1:      
584 #if __mips64
585         sd      t1,0(t0)
586         b       .next
587         nop
588 #else
589         b       .fatal
590         nop
591 #endif
592                 
593         
594         /* 
595          * RMW(ADDR,AND,OR) 
596          */
597 8:      bne     t4,OP_RMW,8f
598         nop
599         lw      t1,Init_A1(a0)
600         lw      t2,Init_A2(a0)
601         and     t4,t3,MOD_MASK
602         
603         bne     t4,MOD_B,1f
604         nop
605         lbu     t4,0(t0)
606         and     t4,t1
607         or      t4,t2
608         sb      t4,0(t0)
609         b       .next
610         nop
611 1:      bne     t4,MOD_H,1f
612         nop
613         lhu     t4,0(t0)
614         and     t4,t1
615         or      t4,t2
616         sh      t4,0(t0)
617         b       .next
618         nop
619 1:      bne     t4,MOD_W,1f
620         nop
621         lw      t4,0(t0)
622         and     t4,t1
623         or      t4,t2
624         sw      t4,0(t0)
625         b       .next
626         nop
627         
628 1:              
629 #if __mips64
630         ld      t4,0(t0)
631         and     t4,t1
632         or      t4,t2
633         sd      t4,0(t0)
634         b       .next
635         nop
636 #else   
637         b       .fatal
638         nop
639 #endif
640                 
641         
642         /* 
643          * WAIT(ADDR,MASK,VAL) 
644          */
645 8:      bne     t4,OP_WAIT,8f
646         nop
647         lw      t1,Init_A1(a0)
648         lw      t2,Init_A2(a0)
649         and     t4,t3,MOD_MASK
650         
651         bne     t4,MOD_B,1f
652         nop
653 3:      lbu     t4,0(t0)
654         and     t4,t1
655         bne     t4,t2,3b
656         nop
657         b       .next
658         nop
659 1:      bne     t4,MOD_H,1f
660         nop
661 3:      lhu     t4,0(t0)
662         and     t4,t1
663         bne     t4,t2,3b
664         nop
665         b       .next
666         nop
667 1:      bne     t4,MOD_W,1f
668         nop
669 3:      lw      t4,0(t0)
670         and     t4,t1
671         bne     t4,t2,3b
672         nop
673         b       .next
674         nop
675 1:              
676 #if __mips64
677 3:      ld      t4,0(t0)
678         and     t4,t1
679         bne     t4,t2,3b
680         nop
681         b       .next
682         nop
683 #else   
684         b       .fatal  
685         nop
686 #endif
687         
688         
689 .next:  addu    a0,Init_Size
690         b       reginit 
691         nop     
692         
694 .fatal: b .done
695         nop
696         bal     stuck
697         nop
698 .done:  
699         /* Initialise other low-level I/O devices */
701         bal initgpio
702         nop
704         move k0,v0
706         bal nb_pcirst
707         nop
709         li a0,ACTIVECOM_OFFS
710         bal get_userenv
711         nop
712         move k1,v0
714         bal     initserial
715         nop
716         
717         move a0,k0
718         bal     hexserial
719         nop
722         PRINTSTR("\r\nPMON2000 MIPS Initializing. Standby...\r\n")
723         
725         PRINTSTR("ERRORPC=")
726         mfc0    a0, COP_0_ERROR_PC
727         bal     hexserial
728         nop
730         PRINTSTR(" CONFIG=")
731         mfc0    a0, COP_0_CONFIG
732         bal     hexserial
733         nop
734         PRINTSTR("\r\n")
735         
736         PRINTSTR(" PRID=")
737         mfc0    a0, COP_0_PRID
738         bal     hexserial
739         nop
740         PRINTSTR("\r\n")
742         PRINTSTR("Raw word read of SMB base address: ");
743         li      a0,CFGADDR(PCI_DEV_I82371,3,I82371_PCI3_SMBBA)
744         li      a1,PHYS_TO_UNCACHED(PCI_CFG_SPACE)
745         and     a2,a0,0xffff
746         or      a1,a2
747         srl     a0,16
748         li      a2,BONITO_BASE+BONITO_PCIMAP_CFG
749         sw      a0,BONITO_PCIMAP_CFG(bonito)
750         lw      zero,BONITO_PCIMAP_CFG(bonito)
751         lw      a0,(a1)
752         
753         li      a1,2
754         bal     hexserial
755         nop
756         PRINTSTR("\r\n")        
757         
758         
759 /***(qiaochong) memeory initialization use macro,not smbbus ***/
760                                                          /*bit 31: DDRÅäÖýáÊø±êÖ¾£¬1±íʾ½áÊø£¬Ö»¶Á*/
761 #define DDR_DQS_SELECT  (0<<30)                  /*bit 30 Ñ¡ÔñÊý¾ÝÀ´Ô´£¬0: Ë«ÑزÉÑù£»1£ºDQS²ÉÑù*/
762 #define DDR_DIMM_DIC (1<<29)           /*bit 29 ±êʶDIMM_slot0ÊÇ·ñ²åÓÐÄÚ´æÌõ¡£
763                                 0£ºÎÞ£»1£ºÓУ»*/
764 #define DDR_DIMM_MODULE_NUM (3<<27)    /*bit 28:27 DIMM0/DIMM1ÉÏMOUDLEµÄÊýÄ¿£º
765                                                                 2¡¯b00£ºDIMM1: 1; DIMM0: 1
766                                                                 2¡¯b01£ºDIMM1: 1; DIMM0: 2
767                                                                 2¡¯b10£ºDIMM1: 2; DIMM0: 1
768                                                                 2¡¯b11£ºDIMM1: 2; DIMM0: 2
769                                                                 */
770 #define DDR_IS_SEQ  (1<<26)            /*bit 26 ÒåÍ»·¢Ê½¶ÁдʱµÄ¿éÄÚ˳Ðò£¬
771                                                                 1¡¯b0£ºË³Ðò£»1¡¯b1£º½»Ì棬ÏÖÔÚÖ»Ö§³Ö½»Ì淽ʽ*/
772 #define DDR_TYPE (5<<22) /*bit 25:22 ±í2£ºDDR ¿ØÖÆÆ÷ËùÖ§³ÖµÄDDR SDRAM ¼ÆоƬÀàÐÍ
773                                                                 BITS Density Org.  Row Addr.  Col Addr.
774                                                                 0000 64Mb 16Mb X 4 DA[11:0] DA[9:0]
775                                                                         128Mb 16Mb X 8
776                                                                 0001 64Mb 8Mb X 8 DA[11:0] DA[8:0]
777                                                                         128Mb 8Mb X 16 
778                                                                 0010 64Mb 4Mb X 16 DA[11:0] DA[7:0]
779                                                                 0011 128Mb 32Mb X 4 DA[11:0] DA[11],DA[9:0]
780                                                                 0100 256Mb 64Mb X 4 DA[12:0] DA[11],DA[9:0]
781                                                                          512Mb 64Mb X 8 
782                                                                 0101 256Mb 32Mb X 8 DA[12:0] DA[9:0]
783                                                                          512Mb 32Mb X 16
784                                                                 0110 256Mb 16Mb X 16 DA[12:0] DA[8:0]
785                                                                 0111 512Mb 128Mb X 4 DA[12:0] DA[12:11],DA[9:0]
786                                                                 1000 1Gb 256Mb X 4 DA[13:0] DA[12:11],DA[9:0]
787                                                                 1001 1Gb 128Mb X 8 DA[13:0] DA[11],DA[9:0]
788                                                                 1010 1Gb 64Mb X 16 DA[13:0] DA[9:0]
789                                                                 */
790 #define DDR_tREF  (100<<10) /*bit 21:10 SDRAMˢвÙ×÷Ö®¼ä¼ÆÊý£¨Ö÷Ƶ100MHz£©£º
791                                                                 780      7.8us
792                                                                 1560     15.6us
793                                                                 SDRAMˢвÙ×÷Ö®¼ä¼ÆÊý£¨Ö÷Ƶ133MHz£©£º
794                                                                 1040      7.8us
795                                                                 2080      15.6us
796                                                                 SDRAMˢвÙ×÷Ö®¼ä¼ÆÊý£¨Ö÷Ƶ166MHz£©£º
797                                                                 1300      7.8us
798                                                                 2600      15.6us
799                                                                 */
800 #define DDR_TRCD (0<<9) /*bit 9 ÐеØÖ·ÓÐЧµ½ÁеØÖ·ÓÐЧ֮¼äÐè¾­¹ýµÄ¼ÆÊý
801                                                                 1¡¯b0   2 cycles£¨DDR100£©
802                                                                 1¡¯b1   3 cycles£¨DDR266¡¢DDR333£©*/
803 #define DDR_TRPC (1<<7) /*bit 8:7 AUTO_REFRESHµ½ACTIVEÖ®¼äÐè¾­¹ýµÄ¼ÆÊý
804                                                                 2¡¯b00  Null 
805                                                                 2¡¯b01  8 cylces £¨DDR100£©
806                                                                 2¡¯b10  10 cycles£¨DDR266£©
807                                                                 2¡¯b11  12 cycles£¨DDR333£©
808                                                                 */
809 #define DDR_TRAS (0<<6) /*bit 6 ACTIVEµ½PRECHARGEÖ®¼äÐè¾­¹ýµÄ¼ÆÊý
810                                                                 1¡¯b0   5 cycles£¨DDR100£©
811                                                                 1¡¯b1   7 cycles£¨DDR266¡¢DDR333£©  
812                                                                 */
813 #define DDR_TCAS (1<<4) /*bit 5:4 ´Ó¶ÁÃüÁîµ½µÚÒ»¸öÊý¾Ýµ½À´Ðè¾­¹ýµÄ¼ÆÊý
814                                                                 2¡¯b00  1.5 cycles
815                                                                 2¡¯b01  2 cycles
816                                                                 2¡¯b10  2.5 cycles
817                                                                 2¡¯b11  3 cycles
818                                                                 */
819 #define DDR_TWR (0<<3) /*bit 3 Ð´²Ù×÷×îºóÒ»¸öÊý¾Ýµ½PRECHARGEÖ®¼äÐè¾­¹ýµÄ¼ÆÊý
820                                                                 1¡¯b0   2 cycles£¨DDR100£©
821                                                                 1¡¯b1   3 cycles£¨DDR266¡¢DDR333£©
822                                                                 */
823 #define DDR_TRP (0<<2) /*bit 2 PRECHARGEÃüÁîÖ´ÐÐʱ¼ä¼ÆÊý
824                                                                 1¡¯b0   2 cycles£¨DDR100£©
825                                                                 1¡¯b1   3 cycles£¨DDR266¡¢DDR333£©
826                                                                 */
827 #define DDR_TRC (1<<0) /*bit 1:0 ACTIVEÓëACTIVE/AUTO_REFRESHÃüÁîÖ®¼ä¼ÆÊý
828                                                                 2¡¯b00  Null
829                                                                 2¡¯b01  7 cycles£¨DDR100£©
830                                                                 2¡¯b10  9 cycles£¨DDR266£©
831                                                                 2¡¯b11  10cycles£¨DDR333£©
832                                                                 ×¢£¨ÓÉÓÚprechargeºÍras casµÄÑÓʱ¼ÓÆðÀ´ÕýºÃÂú×ãÕâ¸öÑÓʱ£¬ËùÒÔÔÚDDR¿ØÖÆÆ÷ÀïûÓоßÌ忼ÂÇÕâ¸ö²ÎÊý£©
833                                                                 */
834 #define sdcfg_DATA DDR_DQS_SELECT|DDR_DIMM_DIC|DDR_DIMM_MODULE_NUM|DDR_IS_SEQ|DDR_TYPE|DDR_tREF|DDR_TRCD|DDR_TRPC|DDR_TRAS|DDR_TCAS|DDR_TWR|DDR_TRP|DDR_TRC
835         li  msize,512*(1<<20)
836         li  sdCfg,sdcfg_DATA
837 #       li sdCfg,0x05441091
838         sw      sdCfg,BONITO_SDCFG(bonito)
839 #if 1//def DBGSBD
840         PRINTSTR("sdcfg=");
841         move    a0,sdCfg
842         bal     hexserial
843         nop
844         PRINTSTR("\r\n");
845         PRINTSTR("msize=");
846         move    a0,msize
847         sw      a0,BONITO_MEMSIZE(bonito)
848         bal     hexserial
849         nop
850         PRINTSTR("\r\n")
851 #endif
852         li      t1,0            # accumulate pcimembasecfg settings
853                 
854         /* set bar0 mask and translation to point to SDRAM */
855         sub     t0,msize,1
856         not     t0
857         srl     t0,BONITO_PCIMEMBASECFG_ASHIFT-BONITO_PCIMEMBASECFG_MEMBASE0_MASK_SHIFT
858         and     t0,BONITO_PCIMEMBASECFG_MEMBASE0_MASK
859         or      t1,t0
860         
861         li      t0,0x00000000
862         srl     t0,BONITO_PCIMEMBASECFG_ASHIFT-BONITO_PCIMEMBASECFG_MEMBASE0_TRANS_SHIFT
863         and     t0,BONITO_PCIMEMBASECFG_MEMBASE0_TRANS
864         or      t1,t0
865         or      t1,BONITO_PCIMEMBASECFG_MEMBASE0_CACHED
867         /* set bar1 to minimum size to conserve PCI space */
868         li      t0, ~0
869         srl     t0,BONITO_PCIMEMBASECFG_ASHIFT-BONITO_PCIMEMBASECFG_MEMBASE1_MASK_SHIFT
870         and     t0,BONITO_PCIMEMBASECFG_MEMBASE1_MASK
871         or      t1,t0
872         
873         li      t0,0x00000000
874         srl     t0,BONITO_PCIMEMBASECFG_ASHIFT-BONITO_PCIMEMBASECFG_MEMBASE1_TRANS_SHIFT
875         and     t0,BONITO_PCIMEMBASECFG_MEMBASE1_TRANS
876         or      t1,t0
877         or      t1,BONITO_PCIMEMBASECFG_MEMBASE1_CACHED
879         sw      t1,BONITO_PCIMEMBASECFG(bonito)
881         /* enable configuration cycles now */
882         lw      t0,BONITO_BONPONCFG(bonito)
883         and     t0,~BONITO_BONPONCFG_CONFIG_DIS
884         sw      t0,BONITO_BONPONCFG(bonito)
886         PRINTSTR("Init SDRAM Done!\r\n");
889  *  Reset and initialize caches to a known state.
890  */
891 #define IndexStoreTagI  0x08
892 #define IndexStoreTagD  0x09
893 #define IndexStoreTagS  0x0b
894 #define IndexStoreTagT  0x0a
895 #define FillI           0x14
898  *  RM7000 config register bits.
899  */
900 #define CF_7_SE         (1 << 3)        /* Secondary cache enable */
901 #define CF_7_SC         (1 << 31)       /* Secondary cache not present */
902 #define CF_7_TE         (1 << 12)       /* Tertiary cache enable */
903 #define CF_7_TC         (1 << 17)       /* Tertiary cache not present */
904 #define CF_7_TS         (3 << 20)       /* Tertiary cache size */
905 #define CF_7_TS_AL      20              /* Shift to align */
906 #define NOP8 nop;nop;nop;nop;nop;nop;nop;nop
907 do_caches:
908         TTYDBG("Sizing caches...\r\n");
909 /* godson2 */
910         mfc0    a0, COP_0_PRID
911         andi    a0, a0, 0xff00
912         li      a1, 0x6300
913         bne     a0, a1, cache_done
914         nop
915         TTYDBG("godson2 caches found\r\n")
916         bal     godson2_cache_init
917         nop
918 cache_done:
920 #ifdef DEBUG_LOCORE
921         TTYDBG("Init caches done, cfg = ")
922         mfc0    a0, COP_0_CONFIG
923         bal     hexserial
924         nop
925         TTYDBG("\r\n")
926 #endif
927 #include "machine/newtest/mydebug.S"
928 bootnow:
930         TTYDBG("Copy PMON to execute location...\r\n")
931 #include "copypmon.S"
932         TTYDBG("Copy PMON to execute location done.\r\n")
933 #include "test_after_copy1.S"
936         li      a0, 0
937         sw      a0, CpuTertiaryCacheSize /* Set L3 cache size */
938         move    a0,msize
939         srl     a0,20
941         la      v0, initmips
942         jalr    v0
943         nop
944 stuck:
945 #ifdef DEBUG_LOCORE
946         TTYDBG("Dumping GT64240 setup.\r\n")
947         TTYDBG("offset----data------------------------.\r\n")
948         li      s3, 0
950         move    a0, s3
951         bal     hexserial
952         nop
953         TTYDBG(": ")
955         add     a0, s3, bonito
956         lw      a0, 0(a0)
957         bal     hexserial
958         addiu   s3, 4
959         TTYDBG(" ")
960         li      a0, 0xfff
961         and     a0, s3
962         beqz    a0, 3f
963         li      a0, 0x01f
964         and     a0, s3
965         bnez    a0, 2b
966         TTYDBG("\r\n")
967         b       1b
968         nop
970         b       3b
971         nop
973 #else
974         b       stuck
975         nop
976 #endif
978  *  Clear the TLB. Normally called from start.S.
979  */
980 #if __mips64
981 #define MTC0 dmtc0
982 #else 
983 #define MTC0 mtc0
984 #endif
985 LEAF(CPU_TLBClear)
986         li      a3, 0                   # First TLB index.
988         li      a2, PG_SIZE_4K
989         MTC0   a2, COP_0_TLB_PG_MASK   # Whatever...
992         MTC0   zero, COP_0_TLB_HI       # Clear entry high.
993         MTC0   zero, COP_0_TLB_LO0      # Clear entry low0.
994         MTC0   zero, COP_0_TLB_LO1      # Clear entry low1.
996         mtc0    a3, COP_0_TLB_INDEX     # Set the index.
997         addiu   a3, 1
998         li      a2, 64
999         nop
1000         nop
1001         tlbwi                           # Write the TLB
1003         bne     a3, a2, 1b
1004         nop
1006         jr      ra
1007         nop
1008 END(CPU_TLBClear)
1011  *  Set up the TLB. Normally called from start.S.
1012  */
1013 LEAF(CPU_TLBInit)
1014         li      a3, 0                   # First TLB index.
1016         li      a2, PG_SIZE_16M
1017         MTC0   a2, COP_0_TLB_PG_MASK   # All pages are 16Mb.
1020         and     a2, a0, PG_SVPN
1021         MTC0   a2, COP_0_TLB_HI # Set up entry high.
1023         move    a2, a0
1024         srl     a2, a0, PG_SHIFT 
1025         and     a2, a2, PG_FRAME
1026         ori     a2, PG_IOPAGE
1027         MTC0   a2, COP_0_TLB_LO0        # Set up entry low0.
1028         addu    a2, (0x01000000 >> PG_SHIFT)
1029         MTC0   a2, COP_0_TLB_LO1        # Set up entry low1.
1031         mtc0    a3, COP_0_TLB_INDEX     # Set the index.
1032         addiu   a3, 1
1033         li      a2, 0x02000000
1034         subu    a1, a2
1035         nop
1036         tlbwi                           # Write the TLB
1038         bgtz    a1, 1b
1039         addu    a0, a2                  # Step address 32Mb.
1041         jr      ra
1042         nop
1043 END(CPU_TLBInit)
1045 LEAF(add_tlb_entry)
1047         and     a2, a0, PG_SVPN
1048         MTC0   a2, COP_0_TLB_HI # Set up entry high.
1049         tlbp
1050         and     a2, a0, PG_SVPN
1051         MTC0   a2, COP_0_TLB_HI # Set up entry high.
1053         srl     a2, a1, PG_SHIFT 
1054         and     a2, a1, PG_FRAME
1055         ori     a2, PG_IOPAGE
1056         MTC0   a2, COP_0_TLB_LO0        # Set up entry low0.
1057         addu    a2, (0x01000000 >> PG_SHIFT)
1058         MTC0   a2, COP_0_TLB_LO1        # Set up entry low1.
1059         MFC0    a3,COP_0_TLB_INDEX
1060         blez a3,1f
1061         nop
1062         tlbwi                           # Write the TLB
1063         bal 2f
1064         nop
1066         tlbwr
1068         jr      ra
1069         nop
1070 END(add_tlb_entry)
1074  * Simple character printing routine used before full initialization
1075  */
1077 LEAF(stringserial)
1078         move    a2, ra
1079         addu    a1, a0, s0
1080         lbu     a0, 0(a1)
1082         beqz    a0, 2f
1083         nop
1084         bal     tgt_putchar
1085         addiu   a1, 1
1086         b       1b
1087         lbu     a0, 0(a1)
1090         j       a2
1091         nop
1092 END(stringserial)
1093 LEAF(outstring)
1094         move    a2, ra
1095         move    a1, a0
1096         lbu     a0, 0(a1)
1098         beqz    a0, 2f
1099         nop
1100         bal     tgt_putchar
1101         addiu   a1, 1
1102         b       1b
1103         lbu     a0, 0(a1)
1106         j       a2
1107         nop
1108 END(outstring)
1109 LEAF(hexserial)
1110         move    a2, ra
1111         move    a1, a0
1112         li      a3, 7
1114         rol     a0, a1, 4
1115         move    a1, a0
1116         and     a0, 0xf
1117         la      v0, hexchar
1118         addu    v0, s0
1119         addu    v0, a0
1120         bal     tgt_putchar
1121         lbu     a0, 0(v0)
1123         bnez    a3, 1b
1124         addu    a3, -1
1126         j       a2
1127         nop
1128 END(hexserial)
1131 LEAF(tgt_putchar)
1132         .set noat
1133         move AT,ra
1134 #ifdef HAVE_NB_SERIAL
1135         la      v0, COM3_BASE_ADDR
1136         bal 1f
1137         nop
1138 #endif
1139         and v0,k1,1
1140         beqz v0,11f
1141         nop
1142         la      v0, COM1_BASE_ADDR
1143         bal 1f
1144         nop
1146         and v0,k1,2
1147         beqz v0,12f
1148         la      v0, COM2_BASE_ADDR
1149         bal 1f
1150         nop
1152         jr AT
1153         nop
1155         lbu     v1, NSREG(NS16550_LSR)(v0)
1156         and     v1, LSR_TXRDY
1157         beqz    v1, 1b
1158         nop
1160         sb      a0, NSREG(NS16550_DATA)(v0)
1162         j       ra
1163         nop     
1164         .set at
1165 END(tgt_putchar)
1166 /* baud rate definitions, matching include/termios.h */
1167 #define B0      0
1168 #define B50     50      
1169 #define B75     75
1170 #define B110    110
1171 #define B134    134
1172 #define B150    150
1173 #define B200    200
1174 #define B300    300
1175 #define B600    600
1176 #define B1200   1200
1177 #define B1800   1800
1178 #define B2400   2400
1179 #define B4800   4800
1180 #define B9600   9600
1181 #define B19200  19200
1182 #define B38400  38400
1183 #define B57600  57600
1184 #define B115200 115200
1186 LEAF(initserial)
1187         .set noat
1188         move AT,ra
1189 #ifdef HAVE_NB_SERIAL
1190         la      v0, COM3_BASE_ADDR
1191         li      a0, NS16550HZ/(16*CONS_BAUD)
1192         bal 1f
1193         nop
1194 #endif
1195         li      a0, NS16550HZ/2/(16*CONS_BAUD)
1196         and v0,k1,1
1197         beqz v0,11f
1198         nop
1199         la      v0, COM1_BASE_ADDR
1200         bal 1f
1201         nop
1203         and v0,k1,2
1204         beqz v0,12f
1205         nop
1206         la      v0, COM2_BASE_ADDR
1207         bal 1f
1208         nop
1210         jr AT
1211         nop
1213         li      v1, FIFO_ENABLE|FIFO_RCV_RST|FIFO_XMT_RST|FIFO_TRIGGER_4
1214         sb      v1, NSREG(NS16550_FIFO)(v0)
1215         li      v1, CFCR_DLAB
1216         sb      v1, NSREG(NS16550_CFCR)(v0)
1217         //li    v1, NS16550HZ/(16*CONS_BAUD)
1218         move v1,a0
1219         sb      v1, NSREG(NS16550_DATA)(v0)
1220         srl     v1, 8
1221         sb      v1, NSREG(NS16550_IER)(v0)
1222         li      v1, CFCR_8BITS
1223         sb      v1, NSREG(NS16550_CFCR)(v0)
1224         li      v1, MCR_DTR|MCR_RTS
1225         sb      v1, NSREG(NS16550_MCR)(v0)
1226         li      v1, 0x0
1227         sb      v1, NSREG(NS16550_IER)(v0)
1229         j       ra
1230         nop
1231         .set at
1232 END(initserial)
1234 #define get1counts \
1235 li v0,0; \
1236 li v1,32; \
1237 991:\
1238 and a1,a0,1; \
1239 add v0,a1; \
1240 srl a0,1; \
1241 bnez v1,991b; \
1242 addiu v1,-1; 
1244 LEAF(get_userenv)
1245 #ifdef NVRAM_IN_FLASH
1246         li a1,(0xbfc00000+NVRAM_OFFS+3)
1247         add a1,a0
1248         addu a0,a1,-3
1249         move v1,zero
1251         lbu v0,(a1)
1252         sll v1,8
1253         or v1,v0
1254         bne a0,a1,1b
1255         addu a1,-1
1256 #else
1257         li a1,RTC_NVRAM_BASE+3
1258         add a1,a0
1259         addu a0,a1,-3
1260         move v1,zero
1261         1:
1262         sb a1,(PHYS_TO_UNCACHED(PCI_IO_SPACE+RTC_INDEX_REG))
1263         lbu v0,(PHYS_TO_UNCACHED(PCI_IO_SPACE+RTC_DATA_REG))
1264         sll v1,v1,8
1265         or v1,v0
1266         bne a1,a0,1b
1267         addiu a1,-1
1268 #endif
1269         move v0,v1
1270         jr ra
1271         nop
1272 END(get_userenv)
1274 LEAF(initgpio)
1275         move a3,ra
1276         li a0,GPIOCFG_OFFS
1277         bal get_userenv
1278         nop
1279         move ra,a3
1280         move a2,v0
1281         and a0,a2,UART1_232|UART1_422|UART1_485
1282         get1counts
1283         addiu v0,-1
1284         bgtz v0,1f
1285         nop
1286         and a0,a2,UART2_232|UART2_422|UART2_485
1287         get1counts
1288         addiu v0,-1
1289         bgtz v0,1f
1290         nop
1291         or a2,GPIO_SETS
1292         sw a2,(PHYS_TO_UNCACHED(PCI_IO_SPACE+GPO_REG))
1293         move v0,a2
1294         jr ra
1295         nop
1297         move v0,a2
1298         li a0,(UART1_232|UART2_232|LAN1_EN|LAN2A_EN|LAN2B_EN|GPIO_SETS)
1299         sw a0,(PHYS_TO_UNCACHED(PCI_IO_SPACE+GPO_REG))
1300         jr ra
1301         nop
1302 END(initgpio)
1304 LEAF(nb_pcirst)
1305 /* enable configuration cycles now */
1306     lw  t0,BONITO_BONPONCFG(bonito)
1307     and t1,t0,~(1<<3)
1308     sw  t1,BONITO_BONPONCFG(bonito)
1309         li v0, 0x100;   
1310 1:                      
1311         bnez    v0, 1b;
1312         addiu   v0, -1
1313     sw  t0,BONITO_BONPONCFG(bonito)
1314         jr ra
1315         nop
1316 END(nb_pcirst)
1318 #define I82371_RST_REG 0xcf9
1319 LEAF(sb_pcirst)
1320         li v0,2
1321         sb v0,(PHYS_TO_UNCACHED(PCI_IO_SPACE+I82371_RST_REG))
1322         li v0,6
1323         sb v0,(PHYS_TO_UNCACHED(PCI_IO_SPACE+I82371_RST_REG))
1324         li v0, 0x8000;  
1325 1:                      
1326         bnez    v0, 1b;
1327         addiu   v0, -1
1328         jr ra
1329         nop
1330 END(sb_pcirst)
1332 #define SMBOFFS(reg) I82371_SMB_SMB##reg
1334 LEAF(i2cdump)
1335         move    t4,ra
1336         
1337         li      a0,PHYS_TO_UNCACHED(ISAPORT_BASE(SMB_PORT))
1338         lbu     a0,SMBOFFS(HSTSTS)(a0)
1339         li      a1,1
1340         bal     hexserial
1341         nop
1342         PRINTSTR("\r\n")
1343                 
1344         li      a0,PHYS_TO_UNCACHED(ISAPORT_BASE(SMB_PORT))
1345         lbu     a0,SMBOFFS(SLVSTS)(a0)
1346         li      a1,1
1347         bal     hexserial
1348         nop
1349         PRINTSTR("\r\n")
1350         
1351         li      a0,PHYS_TO_UNCACHED(ISAPORT_BASE(SMB_PORT))
1352         lbu     a0,SMBOFFS(HSTCNT)(a0)
1353         li      a1,1
1354         bal     hexserial
1355         nop
1356         PRINTSTR("\r\n")
1357         
1358         li      a0,PHYS_TO_UNCACHED(ISAPORT_BASE(SMB_PORT))
1359         lbu     a0,SMBOFFS(HSTCMD)(a0)
1360         li      a1,1
1361         bal     hexserial
1362         nop
1363         PRINTSTR("\r\n")
1364         
1365         li      a0,PHYS_TO_UNCACHED(ISAPORT_BASE(SMB_PORT))
1366         lbu     a0,SMBOFFS(HSTADD)(a0)
1367         li      a1,1
1368         bal     hexserial
1369         nop
1370         PRINTSTR("\r\n")
1371         
1372         li      a0,PHYS_TO_UNCACHED(ISAPORT_BASE(SMB_PORT))
1373         lbu     a0,SMBOFFS(HSTDAT0)(a0)
1374         li      a1,1
1375         bal     hexserial
1376         nop
1377         PRINTSTR("\r\n")
1378         
1379         li      a0,PHYS_TO_UNCACHED(ISAPORT_BASE(SMB_PORT))
1380         lbu     a0,SMBOFFS(HSTDAT1)(a0)
1381         li      a1,1
1382         bal     hexserial
1383         nop
1384         PRINTSTR("\r\n")
1385         
1386         li      a0,PHYS_TO_UNCACHED(ISAPORT_BASE(SMB_PORT))
1387         lbu     a0,SMBOFFS(BLKDAT)(a0)
1388         li      a1,1
1389         bal     hexserial
1390         nop
1391         PRINTSTR("\r\n")
1392         
1393         li      a0,PHYS_TO_UNCACHED(ISAPORT_BASE(SMB_PORT))
1394         lbu     a0,SMBOFFS(SLVCNT)(a0)
1395         li      a1,1
1396         bal     hexserial
1397         nop
1398         PRINTSTR("\r\n")
1399         
1400         li      a0,PHYS_TO_UNCACHED(ISAPORT_BASE(SMB_PORT))
1401         lbu     a0,SMBOFFS(SHDWCMD)(a0)
1402         li      a1,1
1403         bal     hexserial
1404         nop
1405         PRINTSTR("\r\n")
1406         
1407         li      a0,PHYS_TO_UNCACHED(ISAPORT_BASE(SMB_PORT))
1408         lbu     a0,SMBOFFS(SLVEVT)(a0)
1409         li      a1,1
1410         bal     hexserial
1411         nop
1412         PRINTSTR("\r\n")
1413         
1414         li      a0,PHYS_TO_UNCACHED(ISAPORT_BASE(SMB_PORT))
1415         lbu     a0,SMBOFFS(SLVDAT)(a0)
1416         li      a1,1
1417         bal     hexserial
1418         nop
1419         PRINTSTR("\r\n")
1420         
1421         jr      t4
1422         nop
1423 END(i2cdump)
1427 LEAF(i2cread)
1428         li      t0,PHYS_TO_UNCACHED(ISAPORT_BASE(SMB_PORT))
1430         lbu     t1,SMBOFFS(HSTSTS)(t0)
1431         and     t1,~(I82371_SMB_FAILED|I82371_SMB_BUS_ERR|I82371_SMB_DEV_ERR|I82371_SMB_INTER)
1432         sb      t1,SMBOFFS(HSTSTS)(t0)
1433         
1434         sll     t1,a0,1
1435         or      t1,0xa1         # DIMM base address and read bit
1436         sb      t1,SMBOFFS(HSTADD)(t0)
1437         sb      a1,SMBOFFS(HSTCMD)(t0)
1438         
1439         
1440         li      t1,I82371_SMB_START|I82371_SMB_BDRW
1441         sb      t1,SMBOFFS(HSTCNT)(t0)
1443         li      t3,10000        
1444 1:      lbu     t1,SMBOFFS(HSTSTS)(t0)
1445         and     t2,t1,I82371_SMB_FAILED|I82371_SMB_BUS_ERR|I82371_SMB_DEV_ERR|I82371_SMB_INTER
1446         bnez    t2,1f
1447         nop
1448         sub     t3,1
1449         bnez    t3,1b
1450         nop
1451         b       9f              
1452         nop
1453 1:      
1454         # clear pending errors/interrupts
1455         sb      t1,SMBOFFS(HSTSTS)(t0)
1456         
1457         and     t2,t1,I82371_SMB_FAILED|I82371_SMB_BUS_ERR|I82371_SMB_DEV_ERR
1458         bnez    t2,9f
1459         nop
1460         lbu     v0,SMBOFFS(HSTDAT0)(t0)
1461         j       ra
1462         nop
1463         
1464 9:      li      v0,-1
1465         j       ra
1466         nop
1467 END(i2cread)    
1469 __main:
1470         j       ra
1471         nop
1474         .rdata
1475 transmit_pat_msg:
1476         .asciz  "\r\nInvalid transmit pattern.  Must be DDDD or DDxDDx\r\n"
1477 v200_msg:
1478         .asciz  "\r\nPANIC! Unexpected TLB refill exception!\r\n"
1479 v280_msg:
1480         .asciz  "\r\nPANIC! Unexpected XTLB refill exception!\r\n"
1481 v380_msg:
1482         .asciz  "\r\nPANIC! Unexpected General exception!\r\n"
1483 v400_msg:
1484         .asciz  "\r\nPANIC! Unexpected Interrupt exception!\r\n"
1485 hexchar:
1486         .ascii  "0123456789abcdef"
1488         .text
1489         .align  2
1491  *   I2C Functions used in early startup code to get SPD info from
1492  *   SDRAM modules. This code must be entirely PIC and RAM independent.
1493  */
1495 /* Delay macro */
1496 #define DELAY(count)    \
1497         li v0, count;   \
1498 99:                     \
1499         bnz     vo, 99b;\
1500         addiu   v0, -1
1503 #define I2C_INT_ENABLE  0x80
1504 #define I2C_ENABLE      0x40
1505 #define I2C_ACK         0x04
1506 #define I2C_INT_FLAG    0x08
1507 #define I2C_STOP_BIT    0x10
1508 #define I2C_START_BIT   0x20
1510 #define I2C_AMOD_RD     0x01
1512 #define BUS_ERROR                               0x00
1513 #define START_CONDITION_TRA                     0x08
1514 #define RSTART_CONDITION_TRA                    0x10
1515 #define ADDR_AND_WRITE_BIT_TRA_ACK_REC          0x18
1516 #define ADDR_AND_READ_BIT_TRA_ACK_REC           0x40
1517 #define SLAVE_REC_WRITE_DATA_ACK_TRA            0x28
1518 #define MAS_REC_READ_DATA_ACK_NOT_TRA           0x58
1520 #define Index_Store_Tag_D                       0x05
1521 #define Index_Invalidate_I                      0x00
1522 #define Index_Writeback_Inv_D                   0x01
1524 LEAF(nullfunction)
1525         jr ra
1526         nop
1527 END(nullfunction)
1529 LEAF(godson2_cache_init)
1530 ####part 2####
1531 cache_detect_size_way:
1532         mfc0    t4, CP0_CONFIG
1533         andi    t5, t4, 0x0e00
1534         srl     t5, t5, 9
1535         andi    t6, t4, 0x01c0
1536         srl     t6, t6, 6
1537         addiu   t6, t6, 11
1538         addiu   t5, t5, 11
1539         addiu   t4, $0, 1
1540         sllv    t6, t4, t6
1541         sllv    t5, t4, t5
1543         mfc0    t7, COP_0_PRID
1544         andi    t7, t7, 1
1545         srlv    t6, t6, t7              /* 4way */
1546         srlv    t5, t5, t7
1548 ####part 3####
1549         lui     a0, 0x8000
1550         addu    a1, $0, t5
1551         addu    a2, $0, t6
1552 cache_init_dway:
1553 #a0=0x80000000, a1=icache_size, a2=dcache_size
1554 #a3, v0 and v1 used as local registers
1555         mtc0    $0, CP0_TAGHI
1556         addu    v0, $0, a0
1557         addu    v1, a0, a2
1558 1:      slt     a3, v0, v1
1559         beq     a3, $0, 1f
1560         nop
1561         mtc0    $0, CP0_TAGLO
1562         cache   Index_Store_Tag_D, 0x0(v0)
1563         cache   Index_Store_Tag_D, 0x1(v0)
1564         beqzl   t7, 1b
1565         addiu   v0, v0, 0x20
1566 /* godson2c 4way */
1567         cache   Index_Store_Tag_D, 0x2(v0)
1568         cache   Index_Store_Tag_D, 0x3(v0)
1569 /* godson2c 4way */
1570         beq     $0, $0, 1b
1571         addiu   v0, v0, 0x20
1573 cache_flush_iway:
1574         addu    v0, $0, a0
1575         addu    v1, a0, a1
1576 1:      slt     a3, v0, v1
1577         beq     a3, $0, 1f
1578         nop
1579         cache   Index_Invalidate_I, 0x0(v0)
1580         beq     $0, $0, 1b
1581         addiu   v0, v0, 0x20
1583 cache_flush_dway:
1584         addu    v0, $0, a0
1585         addu    v1, a0, a2
1586 1:      slt     a3, v0, v1
1587         beq     a3, $0, 1f
1588         nop
1589         cache   Index_Writeback_Inv_D, 0x0(v0)
1590         cache   Index_Writeback_Inv_D, 0x1(v0)
1591         beqzl   t7, 1b
1592         addiu   v0, v0, 0x20
1593 /* godson2c 4way */
1594         cache   Index_Writeback_Inv_D, 0x2(v0)
1595         cache   Index_Writeback_Inv_D, 0x3(v0)
1596 /* godson2c 4way */
1597         beq     $0, $0, 1b
1598         addiu   v0, v0, 0x20
1600 cache_init_finish:
1601         nop
1602         jr      ra
1603         nop
1605 cache_init_panic:
1606         TTYDBG("cache init panic\r\n");
1607 1:      b       1b
1608         nop
1609         .end    godson2_cache_init
1611 #define WATCHDOG_REG BONITO(0x0160)
1612 rebootnow1:
1613         sw v1,(a0)
1614         li a0,0
1615         sw      a0,WATCHDOG_REG(bonito)
1616         or a0,3<<24
1617         sw      a0,WATCHDOG_REG(bonito)
1619         b 1b
1620         nop
1621 rebootnow:
1622         sw v1,(a0)
1623         li a0,0xbfe00104
1624         lw a1,(a0)      
1625         and a1,~(1<<2)  
1626         sw a1,(a0)
1627         or a1,(1<<2)
1628         sw a1,(a0)
1630         b 1b
1631         nop