]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - lib_i386/ic/sc520_asm.S
* Patch by Daniel Engström, 13 Nov 2002:
[karo-tx-uboot.git] / lib_i386 / ic / sc520_asm.S
1 /*
2  * (C) Copyright 2002
3  * Daniel Engström, Omicron Ceti AB <daniel@omicron.se>.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23
24 /* This file is largely based on code obtned from AMD. AMD's original
25  * copyright is included below 
26  */
27
28 /*
29  *  =============================================================================
30  *                                                                              
31  *   Copyright 1999 Advanced Micro Devices, Inc.                                
32  *                                                                              
33  *  This software is the property of Advanced Micro Devices, Inc  (AMD)  which 
34  *  specifically grants the user the right to modify, use and distribute this 
35  *  software provided this COPYRIGHT NOTICE is not removed or altered.  All 
36  *  other rights are reserved by AMD.                                                       
37  *                                                                             
38  *  THE MATERIALS ARE PROVIDED "AS IS" WITHOUT ANY EXPRESS OR IMPLIED WARRANTY 
39  *  OF ANY KIND INCLUDING WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT OF 
40  *  THIRD-PARTY INTELLECTUAL PROPERTY, OR FITNESS FOR ANY PARTICULAR PURPOSE.
41  *  IN NO EVENT SHALL AMD OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER
42  *  (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF PROFITS, BUSINESS
43  *  INTERRUPTION, LOSS OF INFORMAITON) ARISING OUT OF THE USE OF OR INABILITY
44  *  TO USE THE MATERIALS, EVEN IF AMD HAS BEEN ADVISED OF THE POSSIBILITY OF
45  *  SUCH DAMAGES.  BECAUSE SOME JURSIDICTIONS PROHIBIT THE EXCLUSION OR
46  *  LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES, THE ABOVE
47  *  LIMITATION MAY NOT APPLY TO YOU.
48  * 
49  *  AMD does not assume any responsibility for any errors that may appear in
50  *  the Materials nor any responsibility to support or update the Materials.
51  *  AMD retains the right to make changes to its test specifications at any
52  *  time, without notice.
53  * 
54  *  So that all may benefit from your experience, please report  any  problems 
55  *  or suggestions about this software back to AMD.  Please include your name, 
56  *  company,  telephone number,  AMD product requiring support and question or 
57  *  problem encountered.                                                       
58  *                                                                             
59  *  Advanced Micro Devices, Inc.         Worldwide support and contact           
60  *  Embedded Processor Division            information available at:               
61  *  Systems Engineering                       epd.support@amd.com
62  *  5204 E. Ben White Blvd.                          -or-
63  *  Austin, TX 78741                http://www.amd.com/html/support/techsup.html
64  *  ============================================================================
65  */
66
67
68 /*******************************************************************************
69  *       AUTHOR      : Buddy Fey - Original. 
70  *******************************************************************************
71  */
72
73
74 /*******************************************************************************
75  *       FUNCTIONAL DESCRIPTION:
76  * This routine is called to autodetect the geometry of the DRAM.
77  *
78  * This routine is called to determine the number of column bits for the DRAM
79  * devices in this external bank. This routine assumes that the external bank
80  * has been configured for an 11-bit column and for 4 internal banks. This gives
81  * us the maximum address reach in memory. By writing a test value to the max
82  * address and locating where it aliases to, we can determine the number of valid
83  * column bits.
84  *
85  * This routine is called to determine the number of internal banks each DRAM
86  * device has. The external bank (under test) is configured for maximum reach
87  * with 11-bit columns and 4 internal banks. This routine will write to a max
88  * address (BA1 and BA0 = 1) and then read from an address with BA1=0 to see if
89  * that column is a "don't care". If BA1 does not affect write/read of data,
90  * then this device has only 2 internal banks.
91  *
92  * This routine is called to determine the ending address for this external
93  * bank of SDRAM. We write to a max address with a data value and then disable
94  * row address bits looking for "don't care" locations. Each "don't care" bit
95  * represents a dividing of the maximum density (128M) by 2. By dividing the
96  * maximum of 32 4M chunks in an external bank down by all the "don't care" bits
97  * determined during sizing, we set the proper density.
98  *
99  * WARNINGS.
100  * bp must be preserved because it is used for return linkage.
101  *
102  * EXIT
103  * nothing returned - but the memory subsystem is enabled
104  *******************************************************************************
105  */
106
107 .section .text
108 .equ            DRCCTL,     0x0fffef010   /* DRAM control register */
109 .equ            DRCTMCTL,   0x0fffef012   /* DRAM timing control register */
110 .equ            DRCCFG,     0x0fffef014   /* DRAM bank configuration register */
111 .equ            DRCBENDADR, 0x0fffef018   /* DRAM bank ending address register */
112 .equ            ECCCTL,     0x0fffef020   /* DRAM ECC control register */
113 .equ            DBCTL,      0x0fffef040   /* DRAM buffer control register */
114
115 .equ            CACHELINESZ, 0x00000010   /* size of our cache line (read buffer) */
116 .equ            COL11_ADR,  0x0e001e00    /* 11 col addrs */
117 .equ            COL10_ADR,  0x0e000e00    /* 10 col addrs */
118 .equ            COL09_ADR,  0x0e000600    /*  9 col addrs */
119 .equ            COL08_ADR,  0x0e000200    /*  8 col addrs */
120 .equ            ROW14_ADR,  0x0f000000    /* 14 row addrs */
121 .equ            ROW13_ADR,  0x07000000    /* 13 row addrs */
122 .equ            ROW12_ADR,  0x03000000    /* 12 row addrs */
123 .equ            ROW11_ADR,  0x01000000    /* 11 row addrs/also bank switch */
124 .equ            ROW10_ADR,  0x00000000    /* 10 row addrs/also bank switch */
125 .equ            COL11_DATA, 0x0b0b0b0b    /* 11 col addrs */
126 .equ            COL10_DATA, 0x0a0a0a0a    /* 10 col data */
127 .equ            COL09_DATA, 0x09090909    /*  9 col data */
128 .equ            COL08_DATA, 0x08080808    /*  8 col data */
129 .equ            ROW14_DATA, 0x3f3f3f3f    /* 14 row data (MASK) */
130 .equ            ROW13_DATA, 0x1f1f1f1f    /* 13 row data (MASK) */
131 .equ            ROW12_DATA, 0x0f0f0f0f    /* 12 row data (MASK) */
132 .equ            ROW11_DATA, 0x07070707    /* 11 row data/also bank switch (MASK) */
133 .equ            ROW10_DATA, 0xaaaaaaaa    /* 10 row data/also bank switch (MASK) */
134
135
136  /*
137   * initialize dram controller registers
138   */
139 .globl mem_init
140 mem_init: 
141         xorw    %ax,%ax
142         movl    $DBCTL, %edi             
143 fs      movb     %al, (%edi)             /* disable write buffer */
144
145         movl    $ECCCTL, %edi            
146 fs      movb     %al, (%edi)             /* disable ECC */
147
148         movl    $DRCTMCTL, %edi           
149         movb    $0x1E,%al                /* Set SDRAM timing for slowest */
150 fs      movb     %al, (%edi)
151
152  /*
153   * setup loop to do 4 external banks starting with bank 3
154   */
155         movl    $0xff000000,%eax         /* enable last bank and setup */
156         movl    $DRCBENDADR, %edi        /* ending address register */
157 fs      movl     %eax, (%edi)
158
159         movl    $DRCCFG, %edi            /* setup */
160         movw    $0xbbbb,%ax              /* dram config register for  */
161 fs      movw    %ax, (%edi)
162
163  /*
164   * issue a NOP to all DRAMs
165   */
166         movl    $DRCCTL, %edi            /* setup DRAM control register with */
167         movb    $0x1,%al                 /* Disable refresh,disable write buffer */ 
168 fs      movb     %al, (%edi)
169         movl    $CACHELINESZ, %esi       /* just a dummy address to write for */ 
170 fs      movw     %ax, (%esi)
171  /*
172   * delay for 100 usec? 200?
173   * ******this is a cludge for now *************
174   */
175         movw    $100,%cx
176 sizdelay: 
177         loop    sizdelay                 /* we need 100 usec here */
178  /***********************************************/
179
180  /*
181   * issue all banks precharge
182   */
183         movb    $0x2,%al                 /* All banks precharge */
184 fs      movb     %al, (%edi)
185 fs      movw     %ax, (%esi)
186
187  /*
188   * issue 2 auto refreshes to all banks 
189   */
190         movb    $0x4,%al                 /* Auto refresh cmd */
191 fs      movb     %al, (%edi)
192         movw    $2,%cx
193 refresh1: 
194 fs      movw     %ax, (%esi)
195         loop    refresh1
196
197  /*
198   * issue LOAD MODE REGISTER command
199   */
200         movb    $0x3,%al                 /* Load mode register cmd */
201 fs      movb     %al, (%edi)
202 fs      movw     %ax, (%esi)
203
204  /*
205   * issue 8 more auto refreshes to all banks 
206   */ 
207         movb    $0x4,%al                 /* Auto refresh cmd */
208 fs      movb     %al, (%edi)
209         movw    $8,%cx
210 refresh2: 
211 fs      movw     %ax, (%esi)
212         loop    refresh2
213
214  /*
215   * set control register to NORMAL mode 
216   */
217         movb    $0x0,%al                 /* Normal mode value */
218 fs      movb     %al, (%edi)
219
220  /*
221   * size dram starting with external bank 3 moving to external bank 0
222   */
223         movl    $0x3,%ecx                /* start with external bank 3 */
224
225 nextbank: 
226
227  /*
228   * write col 11 wrap adr
229   */
230         movl    $COL11_ADR, %esi         /* set address to max col (11) wrap addr */
231         movl    $COL11_DATA, %eax        /* pattern for max supported columns(11) */
232 fs      movl    %eax, (%esi)             /* write max col pattern at max col adr */
233 fs      movl    (%esi), %ebx             /* optional read */
234         cmpl    %ebx,%eax                /* to verify write */
235         jnz     bad_ram                  /* this ram is bad */
236  /*
237   * write col 10 wrap adr
238   */
239
240         movl    $COL10_ADR, %esi         /* set address to 10 col wrap address */
241         movl    $COL10_DATA, %eax        /* pattern for 10 col wrap */
242 fs      movl    %eax, (%esi)             /* write 10 col pattern @ 10 col wrap adr */
243 fs      movl    (%esi), %ebx             /* optional read */
244         cmpl    %ebx,%eax                /* to verify write */
245         jnz     bad_ram                  /* this ram is bad */
246  /*
247   * write col 9 wrap adr
248   */
249         movl    $COL09_ADR, %esi         /* set address to 9 col wrap address */
250         movl    $COL09_DATA, %eax        /* pattern for 9 col wrap */
251 fs      movl    %eax, (%esi)             /* write 9 col pattern @ 9 col wrap adr */
252 fs      movl    (%esi), %ebx             /* optional read */
253         cmpl    %ebx,%eax                /* to verify write */
254         jnz     bad_ram                  /* this ram is bad */
255  /*
256   * write col 8 wrap adr
257   */
258         movl    $COL08_ADR, %esi         /* set address to min(8) col wrap address */
259         movl    $COL08_DATA, %eax        /* pattern for min (8) col wrap */
260 fs      movl    %eax, (%esi)             /* write min col pattern @ min col adr */
261 fs      movl    (%esi), %ebx             /* optional read */
262         cmpl    %ebx,%eax                /* to verify write */
263         jnz     bad_ram                  /* this ram is bad */
264  /*
265   * write row 14 wrap adr
266   */
267         movl    $ROW14_ADR, %esi         /* set address to max row (14) wrap addr */
268         movl    $ROW14_DATA, %eax        /* pattern for max supported rows(14) */
269 fs      movl    %eax, (%esi)             /* write max row pattern at max row adr */
270 fs      movl    (%esi), %ebx             /* optional read */
271         cmpl    %ebx,%eax                /* to verify write */
272         jnz     bad_ram                  /* this ram is bad */
273  /*
274   * write row 13 wrap adr
275   */
276         movl    $ROW13_ADR, %esi         /* set address to 13 row wrap address */
277         movl    $ROW13_DATA, %eax        /* pattern for 13 row wrap */
278 fs      movl    %eax, (%esi)             /* write 13 row pattern @ 13 row wrap adr */
279 fs      movl    (%esi), %ebx             /* optional read */
280         cmpl    %ebx,%eax                /* to verify write */
281         jnz     bad_ram                  /* this ram is bad */
282  /*
283   * write row 12 wrap adr
284   */
285         movl    $ROW12_ADR, %esi         /* set address to 12 row wrap address */
286         movl    $ROW12_DATA, %eax        /* pattern for 12 row wrap */
287 fs      movl    %eax, (%esi)             /* write 12 row pattern @ 12 row wrap adr */
288 fs      movl    (%esi), %ebx             /* optional read */
289         cmpl    %ebx,%eax                /* to verify write */
290         jnz     bad_ram                  /* this ram is bad */
291  /*
292   * write row 11 wrap adr
293   */
294         movl    $ROW11_ADR, %edi         /* set address to 11 row wrap address */
295         movl    $ROW11_DATA, %eax        /* pattern for 11 row wrap */
296 fs      movl    %eax, (%edi)             /* write 11 row pattern @ 11 row wrap adr */
297 fs      movl    (%edi), %ebx             /* optional read */
298         cmpl    %ebx,%eax                /* to verify write */
299         jnz     bad_ram                  /* this ram is bad */
300  /*
301   * write row 10 wrap adr --- this write is really to determine number of banks
302   */
303         movl    $ROW10_ADR, %edi         /* set address to 10 row wrap address */
304         movl    $ROW10_DATA, %eax        /* pattern for 10 row wrap (AA) */
305 fs      movl    %eax, (%edi)             /* write 10 row pattern @ 10 row wrap adr */
306 fs      movl    (%edi), %ebx             /* optional read */
307         cmpl    %ebx,%eax                /* to verify write */
308         jnz     bad_ram                  /* this ram is bad */
309  /*
310   * read data @ row 12 wrap adr to determine  * banks, 
311   * and read data @ row 14 wrap adr to determine  * rows.
312   * if data @ row 12 wrap adr is not AA, 11 or 12 we have bad RAM.
313   * if data @ row 12 wrap == AA, we only have 2 banks, NOT 4 
314   * if data @ row 12 wrap == 11 or 12, we have 4 banks,
315   */
316         xorw    %di,%di                  /* value for 2 banks in DI */
317 fs      movl    (%esi), %ebx             /* read from 12 row wrap to check banks 
318                                           * (esi is setup from the write to row 12 wrap) */
319         cmpl    %ebx,%eax                /* check for AA pattern  (eax holds the aa pattern) */
320         jz      only2                    /* if pattern == AA, we only have 2 banks */
321
322         /* 4 banks */
323         
324         movw    $8,%di                   /* value for 4 banks in DI (BNK_CNT bit) */
325         cmpl    $ROW11_DATA, %ebx        /* only other legitimate values are 11 */
326         jz      only2
327         cmpl    $ROW12_DATA, %ebx        /* and 12 */
328         jnz     bad_ram                  /* its bad if not 11 or 12! */
329         
330         /* fall through */
331 only2: 
332  /*
333   * validate row mask
334   */
335         movl    $ROW14_ADR, %esi         /* set address back to max row wrap addr */
336 fs      movl    (%esi), %eax             /* read actual number of rows @ row14 adr */
337
338         cmpl    $ROW11_DATA, %eax        /* row must be greater than 11 pattern */
339         jb      bad_ram
340
341         cmpl    $ROW14_DATA, %eax        /* and row must be less than 14 pattern */
342         ja      bad_ram
343
344         cmpb    %ah,%al                  /* verify all 4 bytes of dword same */
345         jnz     bad_ram
346         movl    %eax,%ebx
347         shrl    $16,%ebx
348         cmpw    %bx,%ax
349         jnz     bad_ram
350  /*
351   * read col 11 wrap adr for real column data value
352   */
353         movl    $COL11_ADR, %esi         /* set address to max col (11) wrap addr */
354 fs      movl    (%esi), %eax             /* read real col number at max col adr */
355  /*
356   * validate column data
357   */
358         cmpl    $COL08_DATA, %eax        /* col must be greater than 8 pattern */
359         jb      bad_ram
360
361         cmpl    $COL11_DATA, %eax        /* and row must be less than 11 pattern */
362         ja      bad_ram
363
364         subl    $COL08_DATA, %eax        /* normalize column data to zero */
365         jc      bad_ram
366         cmpb    %ah,%al                  /* verify all 4 bytes of dword equal */
367         jnz     bad_ram
368         movl    %eax,%edx
369         shrl    $16,%edx
370         cmpw    %dx,%ax
371         jnz     bad_ram
372  /*
373   * merge bank and col data together
374   */
375         addw    %di,%dx                  /* merge of bank and col info in dl */
376  /*
377   * fix ending addr mask based upon col info
378   */
379         movb    $3,%al
380         subb    %dh,%al                  /* dh contains the overflow from the bank/col merge  */
381         movb    %bl,%dh                  /* bl contains the row mask (aa, 07, 0f, 1f or 3f) */
382         xchgw   %cx,%ax                  /* cx = ax = 3 or 2 depending on 2 or 4 bank device */
383         shrb    %cl,%dh                  /*  */
384         incb    %dh                      /* ending addr is 1 greater than real end */
385         xchgw   %cx,%ax                  /* cx is bank number again */
386  /*
387   * issue all banks precharge
388   */
389 bad_reint: 
390         movl    $DRCCTL, %esi            /* setup DRAM control register with */
391         movb    $0x2,%al                 /* All banks precharge */
392 fs      movb     %al, (%esi)
393         movl    $CACHELINESZ, %esi       /* address to init read buffer */
394 fs      movw     %ax, (%esi)
395
396  /*
397   * update ENDING ADDRESS REGISTER
398   */
399         movl    $DRCBENDADR, %edi        /* DRAM ending address register */
400         movl    %ecx,%ebx
401         addl    %ebx, %edi
402 fs      movb    %dh, (%edi)
403  /*
404   * update CONFIG REGISTER
405   */
406         xorb    %dh,%dh
407         movw    $0x00f,%bx
408         movw    %cx,%ax
409         shlw    $2,%ax
410         xchgw   %cx,%ax
411         shlw    %cl,%dx
412         shlw    %cl,%bx
413         notw    %bx
414         xchgw   %cx,%ax
415         movl    $DRCCFG, %edi
416 fs      mov     (%edi), %ax
417         andw    %bx,%ax
418         orw     %dx,%ax
419 fs      movw    %ax, (%edi)
420         jcxz    cleanup
421
422         decw    %cx
423         movl    %ecx,%ebx
424         movl    $DRCBENDADR, %edi        /* DRAM ending address register */
425         movb    $0xff,%al
426         addl    %ebx, %edi
427 fs      movb    %al, (%edi)
428  /*
429   * set control register to NORMAL mode 
430   */
431         movl    $DRCCTL, %esi            /* setup DRAM control register with */
432         movb    $0x0,%al                 /* Normal mode value */
433 fs      movb    %al, (%esi)
434         movl    $CACHELINESZ, %esi       /* address to init read buffer */
435 fs      movw    %ax, (%esi)
436         jmp     nextbank
437
438 cleanup: 
439         movl    $DRCBENDADR, %edi        /* DRAM ending address register  */
440         movw    $4,%cx
441         xorw    %ax,%ax
442 cleanuplp: 
443 fs      movb   (%edi), %al
444         orb     %al,%al
445         jz      emptybank
446
447         addb    %ah,%al
448         jns     nottoomuch
449
450         movb    $0x7f,%al
451 nottoomuch: 
452         movb    %al,%ah
453         orb     $0x80,%al
454 fs      movb    %al, (%edi)
455 emptybank: 
456         incl    %edi
457         loop    cleanuplp
458
459 #if defined(CFG_SDRAM_CAS_LATENCY_2T) || defined(CFG_SDRAM_CAS_LATENCY_3T)
460         /* set the CAS latency now since it is hard to do
461          * when we run from the RAM */
462         movl    $DRCTMCTL, %edi          /* DRAM timing register */
463         movb    (%edi), %al     
464 #ifdef CFG_SDRAM_CAS_LATENCY_2T
465         andb    $0xef, %al
466 #endif
467 #ifdef CFG_SDRAM_CAS_LATENCY_3T
468         orb     $0x10, %al
469 #endif   
470         movb    %al, (%edi)
471 #endif
472         movl    $DRCCTL, %edi            /* DRAM Control register */
473         movb    $0x3,%al                 /* Load mode register cmd */
474 fs      movb     %al, (%edi)
475 fs      movw     %ax, (%esi)
476
477
478         movl    $DRCCTL, %edi            /* DRAM Control register */
479         movb    $0x18,%al                /*  Enable refresh and NORMAL mode */
480 fs      movb    %al, (%edi)
481
482         jmp     dram_done
483
484 bad_ram: 
485         xorl    %edx,%edx
486         xorl    %edi,%edi
487         jmp     bad_reint
488
489 dram_done: 
490         
491         /* readback DRCBENDADR and return the number
492          * of available ram bytes in %eax */ 
493
494         movl    $DRCBENDADR, %edi        /* DRAM ending address register  */
495         
496         movl    (%edi), %eax
497         movl    %eax, %ecx
498         andl    $0x80000000, %ecx
499         jz      bank2
500         andl    $0x7f000000, %eax
501         shrl    $2, %eax 
502         movl    %eax, %ebx
503
504 bank2:  movl    (%edi), %eax
505         movl    %eax, %ecx
506         andl    $0x00800000, %ecx
507         jz      bank1
508         andl    $0x007f0000, %eax
509         shll    $6, %eax 
510         movl    %eax, %ebx
511
512 bank1:  movl    (%edi), %eax
513         movl    %eax, %ecx
514         andl    $0x00008000, %ecx
515         jz      bank0
516         andl    $0x00007f00, %eax
517         shll    $14, %eax 
518         movl    %eax, %ebx
519
520 bank0:  movl    (%edi), %eax
521         movl    %eax, %ecx
522         andl    $0x00000080, %ecx
523         jz      done
524         andl    $0x0000007f, %eax
525         shll    $22, %eax 
526         movl    %eax, %ebx
527
528 done:   movl    %ebx, %eax
529
530         jmp     *%ebp