]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/netstal/hcu4/hcu4.c
Add PPC4xx-HCU4 and HCU5 boards: HCU4 files
[karo-tx-uboot.git] / board / netstal / hcu4 / hcu4.c
1 /*
2  *(C) Copyright 2005-2007 Netstal Maschinen AG
3  *    Niklaus Giger (Niklaus.Giger@netstal.com)
4  *
5  *    This source code is free software; you can redistribute it
6  *    and/or modify it in source code form under the terms of the GNU
7  *    General Public License as published by the Free Software
8  *    Foundation; either version 2 of the License, or (at your option)
9  *    any later version.
10  *
11  *    This program is distributed in the hope that it will be useful,
12  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *    GNU General Public License for more details.
15  *
16  *    You should have received a copy of the GNU General Public License
17  *    along with this program; if not, write to the Free Software
18  *    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19  */
20
21 #include  <common.h>
22 #include  <ppc4xx.h>
23 #include  <asm/processor.h>
24 #include  <asm/io.h>
25 #include  <asm-ppc/u-boot.h>
26 #include  "../common/nm_bsp.c"
27
28 DECLARE_GLOBAL_DATA_PTR;
29
30 void sysLedSet(u32 value);
31 long int spd_sdram(int(read_spd)(uint addr))
32 #ifdef CONFIG_SPD_EEPROM
33         #define DEBUG
34 #endif
35
36 #if defined(DEBUG)
37 void show_sdram_registers(void);
38 #endif
39
40 /*
41  * This function is run very early, out of flash, and before devices are
42  * initialized. It is called by lib_ppc/board.c:board_init_f by virtue
43  * of being in the init_sequence array.
44  *
45  * The SDRAM has been initialized already -- start.S:start called
46  * init.S:init_sdram early on -- but it is not yet being used for
47  * anything, not even stack. So be careful.
48  */
49
50 #define CPC0_CR0        0xb1    /* Chip control register 0 */
51 #define CPC0_CR1        0xb2    /* Chip control register 1 */
52 /* Attention: If you want 1 microsecs times from the external oscillator
53  * use  0x00804051. But this causes problems with u-boot and linux!
54  */
55 #define CPC0_CR1_VALUE  0x00004051
56 #define CPC0_ECR        0xaa    /* Edge condition register */
57 #define EBC0_CFG        0x23    /* External Peripheral Control Register */
58 #define CPC0_EIRR       0xb6    /* External Interrupt Register */
59
60
61 int board_early_init_f (void)
62 {
63         /*-------------------------------------------------------------------+
64         | Interrupt controller setup for the HCU4 board.
65         | Note: IRQ 0-15  405GP internally generated; high; level sensitive
66         |       IRQ 16    405GP internally generated; low; level sensitive
67         |       IRQ 17-24 RESERVED/UNUSED
68         |       IRQ 31 (EXT IRQ 6) (unused)
69         +-------------------------------------------------------------------*/
70         mtdcr (uicsr, 0xFFFFFFFF); /* clear all ints */
71         mtdcr (uicer, 0x00000000); /* disable all ints */
72         mtdcr (uiccr, 0x00000000); /* set all to be non-critical */
73         mtdcr (uicpr, 0xFFFFFF87); /* set int polarities */
74         mtdcr (uictr, 0x10000000); /* set int trigger levels */
75         mtdcr (uicsr, 0xFFFFFFFF); /* clear all ints */
76
77         mtdcr(CPC0_CR1,  CPC0_CR1_VALUE);
78         mtdcr(CPC0_ECR,  0x60606000);
79         mtdcr(CPC0_EIRR, 0x7c000000);
80
81         return 0;
82 }
83
84 #ifdef CONFIG_BOARD_PRE_INIT
85 int board_pre_init (void)
86 {
87         return board_early_init_f ();
88 }
89
90 #endif
91
92 enum {
93         /* HW_GENERATION_HCU wird nicht mehr unterstuetzt */
94         HW_GENERATION_HCU2  = 0x10,
95         HW_GENERATION_HCU3  = 0x10,
96         HW_GENERATION_HCU4  = 0x20,
97         HW_GENERATION_MCU   = 0x08,
98         HW_GENERATION_MCU20 = 0x0a,
99         HW_GENERATION_MCU25 = 0x09,
100 };
101
102 int checkboard (void)
103 {
104 #define HCU_MACH_VERSIONS_REGISTER ( 0x7C000000 + 0xF00000 )
105         unsigned j;
106         uint16_t *boardVersReg = (uint16_t *) HCU_MACH_VERSIONS_REGISTER;
107         uint16_t generation = *boardVersReg & 0xf0;
108         uint16_t index      = *boardVersReg & 0x0f;
109         /* Force /RTS to active. The board it not wired quite
110            correctly to use cts/rtc flow control, so just force the
111            /RST active and forget about it. */
112         writeb (readb (0xef600404) | 0x03, 0xef600404);
113         printf ("\nNetstal Maschinen AG ");
114         if (generation == HW_GENERATION_HCU3)
115                 printf ("HCU3: index %d\n\n", index);
116         else if (generation == HW_GENERATION_HCU4)
117                 printf ("HCU4: index %d\n\n", index);
118         /* GPIO here noch nicht richtig initialisert !!! */
119         sysLedSet(0);
120         for (j=0; j < 7;j++) {
121                 sysLedSet(1 << j);
122                 udelay(50*1000);
123         }
124         return 0;
125 }
126
127 u32 sysLedGet(void)
128 {
129         return( ~( (*(u32 *)GPIO0_OR)) >> 23) & 0xff;
130 }
131
132 void sysLedSet(u32 value /* value to place in LEDs */)
133 {
134         u32   tmp = ~value;
135         u32   *ledReg;
136         tmp  = (tmp << 23) | 0x7FFFFF;
137         ledReg = (u32   *)GPIO0_OR;
138         *ledReg = tmp;
139 }
140
141 /*
142  * sdram_init - Dummy implementation for start.S, spd_sdram  or initdram
143  *              used for HCUx
144  */
145 void sdram_init(void)
146 {
147         return;
148 }
149
150 #define mtsdram(reg, data)  { mtdcr(memcfga,reg);mtdcr(memcfgd,data); }
151 #define mfsdram(value, reg) { mtdcr(memcfga,reg); value = mfdcr(memcfgd); }
152
153 #if defined(DEBUG)
154 void show_sdram_registers(void)
155 {
156         u32 value;
157         printf ("SDRAM Controller Registers --\n");
158         mfsdram(value, mem_mcopt1);
159         printf ("    SDRAM0_CFG   : 0x%08x\n", value);
160         mfsdram(value, mem_status);
161         printf ("    SDRAM0_STATUS: 0x%08x\n", value);
162         mfsdram(value, mem_mb0cf);
163         printf ("    SDRAM0_B0CR  : 0x%08x\n", value);
164         mfsdram(value, mem_mb1cf);
165         printf ("    SDRAM0_B1CR  : 0x%08x\n", value);
166         mfsdram(value, mem_sdtr1);
167         printf ("    SDRAM0_TR    : 0x%08x\n", value);
168         mfsdram(value, mem_rtr);
169         printf ("    SDRAM0_RTR   : 0x%08x\n", value);
170 }
171 #endif
172
173 #define SDRAM_LEN 32*1024*1024 /* 32 MB -RAM */
174
175 /*
176  * this is even after checkboard. It returns the size of the SDRAM
177  * that we have installed. This function is called by board_init_f
178  * in lib_ppc/board.c to initialize the memory and return what I
179  * found. These are default value, which will be overridden later.
180  */
181
182 long int fixed_hcu4_sdram (int board_type)
183 {
184 #ifdef DEBUG
185         printf (__FUNCTION__);
186 #endif
187         /* disable memory controller */
188         mtdcr (memcfga, mem_mcopt1);
189         mtdcr (memcfgd, 0x00000000);
190
191         udelay (500);
192
193         /* Clear SDRAM0_BESR0 (Bus Error Syndrome Register) */
194         mtdcr (memcfga, mem_besra);
195         mtdcr (memcfgd, 0xffffffff);
196
197         /* Clear SDRAM0_BESR1 (Bus Error Syndrome Register) */
198         mtdcr (memcfga, mem_besrb);
199         mtdcr (memcfgd, 0xffffffff);
200
201         /* Clear SDRAM0_ECCCFG (disable ECC) */
202         mtdcr (memcfga, mem_ecccf);
203         mtdcr (memcfgd, 0x00000000);
204
205         /* Clear SDRAM0_ECCESR (ECC Error Syndrome Register) */
206         mtdcr (memcfga, mem_eccerr);
207         mtdcr (memcfgd, 0xffffffff);
208
209         /* Timing register: CASL=2, PTA=2, CTP=2, LDF=1, RFTA=5, RCD=2
210          * TODO ngngng
211          */
212         mtdcr (memcfga, mem_sdtr1);
213         mtdcr (memcfgd, 0x008a4015);
214
215         /* Memory Bank 0 Config == BA=0x00000000, SZ=64M, AM=3, BE=1
216          * TODO ngngng
217          */
218         mtdcr (memcfga, mem_mb0cf);
219         mtdcr (memcfgd, 0x00062001);
220
221         /* refresh timer = 0x400  */
222         mtdcr (memcfga, mem_rtr);
223         mtdcr (memcfgd, 0x04000000);
224
225         /* Power management idle timer set to the default. */
226         mtdcr (memcfga, mem_pmit);
227         mtdcr (memcfgd, 0x07c00000);
228
229         udelay (500);
230
231         /* Enable banks (DCE=1, BPRF=1, ECCDD=1, EMDUL=1) TODO */
232         mtdcr (memcfga, mem_mcopt1);
233         mtdcr (memcfgd, 0x90800000);
234
235 #ifdef DEBUG
236         printf ("%s: done\n", __FUNCTION__);
237 #endif
238         return SDRAM_LEN;
239 }
240
241 /*---------------------------------------------------------------------------+
242  * getSerialNr
243  *---------------------------------------------------------------------------*/
244 static u32 getSerialNr(void)
245 {
246         u32 *serial = (u32 *)CFG_FLASH_BASE;
247         if (*serial == 0xffffffff) {
248                 return get_ticks();
249         }
250         return *serial;
251 }
252
253
254 /*---------------------------------------------------------------------------+
255  * misc_init_r.
256  *---------------------------------------------------------------------------*/
257
258 int misc_init_r(void)
259 {
260         char *s = getenv("ethaddr");
261         char *e;
262         int i;
263         u32 serial =  getSerialNr();
264         for (i = 0; i < 6; ++i) {
265                 gd->bd->bi_enetaddr[i] = s ? simple_strtoul (s, &e, 16) : 0;
266                 if (s)
267                         s = (*e) ? e + 1 : e;
268         }
269         if (gd->bd->bi_enetaddr[3] == 0 &&
270             gd->bd->bi_enetaddr[4] == 0 &&
271             gd->bd->bi_enetaddr[5] == 0) {
272                 char ethaddr[22];
273                 /* [0..3] Must be in sync with CONFIG_ETHADDR */
274                 gd->bd->bi_enetaddr[0] = 0x00;
275                 gd->bd->bi_enetaddr[1] = 0x60;
276                 gd->bd->bi_enetaddr[2] = 0x13;
277                 gd->bd->bi_enetaddr[3] = (serial          >> 16) & 0xff;
278                 gd->bd->bi_enetaddr[4] = (serial          >>  8) & 0xff;
279                 gd->bd->bi_enetaddr[5] = (serial          >>  0) & 0xff;
280                 sprintf (ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X\0",
281                          gd->bd->bi_enetaddr[0], gd->bd->bi_enetaddr[1],
282                          gd->bd->bi_enetaddr[2], gd->bd->bi_enetaddr[3],
283                          gd->bd->bi_enetaddr[4], gd->bd->bi_enetaddr[5]) ;
284                 printf("%s: Setting eth %s serial 0x%x\n",  __FUNCTION__,
285                        ethaddr, serial);
286                 setenv ("ethaddr", ethaddr);
287         }
288         return 0;
289 }
290
291 #define DO_UGLY_SDRAM_WORKAROUND
292 #ifdef  DO_UGLY_SDRAM_WORKAROUND
293         #include "i2c.h"
294 void set_spd_default_value(unsigned int spd_addr,uchar def_val)
295 {
296         uchar value;
297         int res = i2c_read(SPD_EEPROM_ADDRESS, spd_addr, 1, &value, 1) ;
298         if (res == 0 && value == 0xff) {
299                 res = i2c_write(SPD_EEPROM_ADDRESS,
300                                 spd_addr, 1, &def_val, 1) ;
301 #ifdef DEBUG
302                 printf("%s: Setting spd offset %3d to %3d res %d\n",
303                        __FUNCTION__, spd_addr,  def_val, res);
304 #endif
305         }
306 }
307 #endif
308
309 long int initdram(int board_type)
310 {
311         long dram_size = 0;
312
313 #if !defined(CONFIG_SPD_EEPROM)
314         dram_size = fixed_hcu4_sdram();
315 #else
316 #ifdef  DO_UGLY_SDRAM_WORKAROUND
317         /* Workaround if you have no working I2C-EEPROM-SPD-configuration */
318         i2c_init(CFG_I2C_SPEED, CFG_I2C_SLAVE);
319         set_spd_default_value(2,  4); /* SDRAM Type */
320         set_spd_default_value(7,  0); /* module width, high byte */
321         set_spd_default_value(12, 1); /* Refresh or 0x81 */
322
323         /* Only correct for HCU3 with 32 MB RAM*/
324         /* Number of bytes used by module manufacturer */
325         set_spd_default_value( 0, 128);
326         set_spd_default_value( 1, 11 ); /* Total SPD memory size */
327         set_spd_default_value( 2, 4  ); /* Memory type */
328         set_spd_default_value( 3, 12 ); /* Number of row address bits */
329         set_spd_default_value( 4, 9  ); /* Number of column address bits */
330         set_spd_default_value( 5, 1  ); /* Number of module rows */
331         set_spd_default_value( 6, 32 ); /* Module data width, LSB */
332         set_spd_default_value( 7, 0  ); /* Module data width, MSB */
333         set_spd_default_value( 8, 1  ); /* Module interface signal levels */
334         /* SDRAM cycle time for highest CL (Tclk) */
335         set_spd_default_value( 9, 112);
336         /* SDRAM access time from clock for highest CL (Tac) */
337         set_spd_default_value(10, 84 );
338         set_spd_default_value(11, 2  ); /* Module configuration type */
339         set_spd_default_value(12, 128); /* Refresh rate/type */
340         set_spd_default_value(13, 16 ); /* Primary SDRAM width */
341         set_spd_default_value(14, 8  ); /* Error Checking SDRAM width */
342         /* SDRAM device attributes, min clock delay for back to back */
343         /*random column addresses (Tccd) */
344         set_spd_default_value(15, 1  );
345         /* SDRAM device attributes, burst lengths supported */
346         set_spd_default_value(16, 143);
347         /* SDRAM device attributes, number of banks on SDRAM device */
348         set_spd_default_value(17, 4  );
349         /* SDRAM device attributes, CAS latency */
350         set_spd_default_value(18, 6  );
351         /* SDRAM device attributes, CS latency */
352         set_spd_default_value(19, 1  );
353         /* SDRAM device attributes, WE latency */
354         set_spd_default_value(20, 1  );
355         set_spd_default_value(21, 0  ); /* SDRAM module attributes */
356         /* SDRAM device attributes, general */
357         set_spd_default_value(22, 14 );
358         /* SDRAM cycle time for 2nd highest CL (Tclk) */
359         set_spd_default_value(23, 117);
360         /* SDRAM access time from clock for2nd highest CL (Tac) */
361         set_spd_default_value(24, 84 );
362         /* SDRAM cycle time for 3rd highest CL (Tclk) */
363         set_spd_default_value(25, 0  );
364         /* SDRAM access time from clock for3rd highest CL (Tac) */
365         set_spd_default_value(26, 0  );
366         set_spd_default_value(27, 15 ); /* Minimum row precharge time (Trp) */
367         /* Minimum row active to row active delay (Trrd) */
368         set_spd_default_value(28, 14 );
369         set_spd_default_value(29, 15 ); /* Minimum CAS to RAS delay (Trcd) */
370         set_spd_default_value(30, 37 ); /* Minimum RAS pulse width (Tras) */
371         set_spd_default_value(31, 8  ); /* Module bank density */
372         /* Command and Address signal input setup time */
373         set_spd_default_value(32, 21 );
374         /* Command and Address signal input hold time */
375         set_spd_default_value(33, 8  );
376         set_spd_default_value(34, 21 ); /* Data signal input setup time */
377         set_spd_default_value(35, 8  ); /* Data signal input hold time */
378 #endif  /* DO_UGLY_SDRAM_WORKAROUND */
379         dram_size = spd_sdram(0);
380 #endif
381
382 #ifdef DEBUG
383         show_sdram_registers();
384 #endif
385
386 #if defined(CFG_DRAM_TEST)
387         bcu4_testdram(dram_size);
388         printf("%s %d MB of SDRAM\n", __FUNCTION__, dram_size/(1024*1024));
389 #endif
390         return dram_size;
391 }
392
393