hcu4.c 12 KB

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