gth2.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * (C) Copyright 2005
  3. * Thomas.Lange@corelatus.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. #include <common.h>
  24. #include <command.h>
  25. #include <asm/au1x00.h>
  26. #include <asm/addrspace.h>
  27. #include <asm/mipsregs.h>
  28. #include <watchdog.h>
  29. #include "ee_access.h"
  30. static int wdi_status = 0;
  31. unsigned long mips_io_port_base = 0;
  32. #define SDRAM_SIZE ((64*1024*1024)-(12*4096))
  33. #define SERIAL_LOG_BUFFER KSEG1ADDR(SDRAM_SIZE + (8*4096))
  34. void inline log_serial_char(char c){
  35. char *serial_log_buffer = (char*)SERIAL_LOG_BUFFER;
  36. int serial_log_offset;
  37. u32 *serial_log_offsetp = (u32*)SERIAL_LOG_BUFFER;
  38. serial_log_offset = *serial_log_offsetp;
  39. *(serial_log_buffer + serial_log_offset) = c;
  40. serial_log_offset++;
  41. if(serial_log_offset >= 4096){
  42. serial_log_offset = 4;
  43. }
  44. *serial_log_offsetp = serial_log_offset;
  45. }
  46. void init_log_serial(void){
  47. char *serial_log_buffer = (char*)SERIAL_LOG_BUFFER;
  48. u32 *serial_log_offsetp = (u32*)SERIAL_LOG_BUFFER;
  49. /* Copy buffer from last run */
  50. memcpy(serial_log_buffer + 4096,
  51. serial_log_buffer,
  52. 4096);
  53. memset(serial_log_buffer, 0, 4096);
  54. *serial_log_offsetp = 4;
  55. }
  56. void hw_watchdog_reset(void){
  57. volatile u32 *sys_outputset = (volatile u32*)SYS_OUTPUTSET;
  58. volatile u32 *sys_outputclear = (volatile u32*)SYS_OUTPUTCLR;
  59. if(wdi_status){
  60. *sys_outputset = GPIO_CPU_LED|GPIO_WDI;
  61. wdi_status = 0;
  62. }
  63. else{
  64. *sys_outputclear = GPIO_CPU_LED|GPIO_WDI;
  65. wdi_status = 1;
  66. }
  67. }
  68. long int initdram(int board_type)
  69. {
  70. /* Sdram is setup by assembler code */
  71. /* If memory could be changed, we should return the true value here */
  72. WATCHDOG_RESET();
  73. return (SDRAM_SIZE);
  74. }
  75. /* In cpu/mips/cpu.c */
  76. void write_one_tlb( int index, u32 pagemask, u32 hi, u32 low0, u32 low1 );
  77. void set_ledcard(u32 value){
  78. /* Clock 24 bits to led card */
  79. int i;
  80. volatile u32 *sys_outputset = (volatile u32*)SYS_OUTPUTSET;
  81. volatile u32 *sys_outputclr = (volatile u32*)SYS_OUTPUTCLR;
  82. /* Start with known values */
  83. *sys_outputclr = GPIO_LEDCLK|GPIO_LEDD;
  84. for(i=0;i<24;i++){
  85. if(value&0x00800000){
  86. *sys_outputset = GPIO_LEDD;
  87. }
  88. else{
  89. *sys_outputclr = GPIO_LEDD;
  90. }
  91. udelay(1);
  92. *sys_outputset = GPIO_LEDCLK;
  93. udelay(1);
  94. *sys_outputclr = GPIO_LEDCLK;
  95. udelay(1);
  96. value<<=1;
  97. }
  98. /* Data is enable output */
  99. *sys_outputset = GPIO_LEDD;
  100. }
  101. int checkboard (void)
  102. {
  103. volatile u32 *sys_counter = (volatile u32*)SYS_COUNTER_CNTRL;
  104. volatile u32 *sys_outputset = (volatile u32*)SYS_OUTPUTSET;
  105. volatile u32 *sys_outputclr = (volatile u32*)SYS_OUTPUTCLR;
  106. u32 proc_id;
  107. WATCHDOG_RESET();
  108. *sys_counter = 0x100; /* Enable 32 kHz oscillator for RTC/TOY */
  109. proc_id = read_32bit_cp0_register(CP0_PRID);
  110. switch (proc_id >> 24) {
  111. case 0:
  112. puts ("Board: GTH2\n");
  113. printf ("CPU: Au1000 500 MHz, id: 0x%02x, rev: 0x%02x\n",
  114. (proc_id >> 8) & 0xFF, proc_id & 0xFF);
  115. break;
  116. default:
  117. printf ("Unsupported cpu %d, proc_id=0x%x\n", proc_id >> 24, proc_id);
  118. }
  119. #ifdef CONFIG_IDE_PCMCIA
  120. /* PCMCIA is on a 36 bit physical address.
  121. We need to map it into a 32 bit addresses */
  122. write_one_tlb(20, /* index */
  123. 0x01ffe000, /* Pagemask, 16 MB pages */
  124. CFG_PCMCIA_IO_BASE, /* Hi */
  125. 0x3C000017, /* Lo0 */
  126. 0x3C200017); /* Lo1 */
  127. write_one_tlb(21, /* index */
  128. 0x01ffe000, /* Pagemask, 16 MB pages */
  129. CFG_PCMCIA_ATTR_BASE, /* Hi */
  130. 0x3D000017, /* Lo0 */
  131. 0x3D200017); /* Lo1 */
  132. write_one_tlb(22, /* index */
  133. 0x01ffe000, /* Pagemask, 16 MB pages */
  134. CFG_PCMCIA_MEM_ADDR, /* Hi */
  135. 0x3E000017, /* Lo0 */
  136. 0x3E200017); /* Lo1 */
  137. #endif /* CONFIG_IDE_PCMCIA */
  138. /* Wait for GPIO ports to become stable */
  139. udelay(5000); /* FIXME */
  140. /* Release reset of ethernet PHY chips */
  141. /* Always do this, because linux does not know about it */
  142. *sys_outputset = GPIO_ERESET;
  143. /* Kill FPGA:s */
  144. *sys_outputclr = GPIO_CACONFIG|GPIO_DPACONFIG;
  145. udelay(2);
  146. *sys_outputset = GPIO_CACONFIG|GPIO_DPACONFIG;
  147. /* Turn front led yellow */
  148. set_ledcard(0x00100000);
  149. return 0;
  150. }
  151. #define POWER_OFFSET 0xF0000
  152. #define SW_WATCHDOG_REASON 13
  153. #define BOOTDATA_OFFSET 0xF8000
  154. #define MAX_ATTEMPTS 5
  155. #define FAILSAFE_BOOT 1
  156. #define SYSTEM_BOOT 2
  157. #define SYSTEM2_BOOT 3
  158. #define WRITE_FLASH16(a, d) \
  159. do \
  160. { \
  161. *((volatile u16 *) (a)) = (d);\
  162. } while(0)
  163. static void write_bootdata (volatile u16 * addr, u8 System, u8 Count)
  164. {
  165. u16 data;
  166. volatile u16 *flash = (u16 *) (CFG_FLASH_BASE);
  167. switch(System){
  168. case FAILSAFE_BOOT:
  169. printf ("Setting failsafe boot in flash\n");
  170. break;
  171. case SYSTEM_BOOT:
  172. printf ("Setting system boot in flash\n");
  173. break;
  174. case SYSTEM2_BOOT:
  175. printf ("Setting system2 boot in flash\n");
  176. break;
  177. default:
  178. printf ("Invalid system data %u, setting failsafe\n", System);
  179. System = FAILSAFE_BOOT;
  180. }
  181. if ((Count < 1) | (Count > MAX_ATTEMPTS)) {
  182. printf ("Invalid boot count %u, setting 1\n", Count);
  183. Count = 1;
  184. }
  185. printf ("Boot attempt %d\n", Count);
  186. data = (System << 8) | Count;
  187. /* AMD 16 bit */
  188. WRITE_FLASH16 (&flash[0x555], 0xAAAA);
  189. WRITE_FLASH16 (&flash[0x2AA], 0x5555);
  190. WRITE_FLASH16 (&flash[0x555], 0xA0A0);
  191. WRITE_FLASH16 (addr, data);
  192. }
  193. static int random_system(void){
  194. /* EEPROM read failed. Just try to choose one
  195. system release and hope it works */
  196. /* FIXME */
  197. return(SYSTEM_BOOT);
  198. }
  199. static int switch_system(int old_system){
  200. u8 Rx[10];
  201. u8 Tx[5];
  202. int valid_release;
  203. if(old_system==FAILSAFE_BOOT){
  204. /* Find out which system release to use */
  205. /* Copy from nvram to scratchpad */
  206. Tx[0] = RECALL_MEMORY;
  207. Tx[1] = 7; /* Page */
  208. if (ee_do_cpu_command (Tx, 2, NULL, 0, 1)) {
  209. printf ("EE user page 7 recall failed\n");
  210. return (random_system());
  211. }
  212. Tx[0] = READ_SCRATCHPAD;
  213. if (ee_do_cpu_command (Tx, 2, Rx, 9, 1)) {
  214. printf ("EE user page 7 read failed\n");
  215. return (random_system());
  216. }
  217. /* Crc in 9:th byte */
  218. if (!ee_crc_ok (Rx, 8, *(Rx + 8))) {
  219. printf ("EE read failed, page 7. CRC error\n");
  220. return (random_system());
  221. }
  222. valid_release = Rx[7];
  223. if((valid_release==0xFF)|
  224. ((valid_release&1) == 0)){
  225. return(SYSTEM_BOOT);
  226. }
  227. else{
  228. return(SYSTEM2_BOOT);
  229. }
  230. }
  231. else{
  232. return(FAILSAFE_BOOT);
  233. }
  234. }
  235. static void check_boot_tries (void)
  236. {
  237. /* Count the number of boot attemps
  238. switch system if too many */
  239. int i;
  240. volatile u16 *addr;
  241. volatile u16 data;
  242. u8 system = FAILSAFE_BOOT;
  243. u8 count;
  244. addr = (u16 *) (CFG_FLASH_BASE + BOOTDATA_OFFSET);
  245. if (*addr == 0xFFFF) {
  246. printf ("*** No bootdata exists. ***\n");
  247. write_bootdata (addr, FAILSAFE_BOOT, 1);
  248. } else {
  249. /* Search for latest written bootdata */
  250. i = 0;
  251. while ((*(addr + 1) != 0xFFFF) & (i < 8000)) {
  252. addr++;
  253. i++;
  254. }
  255. if (i >= 8000) {
  256. /* Whoa, dont write any more */
  257. printf ("*** No bootdata found. Not updating flash***\n");
  258. } else {
  259. /* See how many times we have tried to boot real system */
  260. data = *addr;
  261. system = data >> 8;
  262. count = data & 0xFF;
  263. if ((system != SYSTEM_BOOT) &
  264. (system != SYSTEM2_BOOT) &
  265. (system != FAILSAFE_BOOT)) {
  266. printf ("*** Wrong system %d\n", system);
  267. system = FAILSAFE_BOOT;
  268. count = 1;
  269. } else {
  270. switch (count) {
  271. case 0:
  272. case 1:
  273. case 2:
  274. case 3:
  275. case 4:
  276. /* Try same system again if needed */
  277. count++;
  278. break;
  279. case 5:
  280. /* Switch system and reset tries */
  281. count = 1;
  282. system = switch_system(system);
  283. printf ("***Too many boot attempts, switching system***\n");
  284. break;
  285. default:
  286. /* Switch system, start over and hope it works */
  287. printf ("***Unexpected data on addr 0x%x, %u***\n",
  288. (u32) addr, data);
  289. count = 1;
  290. system = switch_system(system);
  291. }
  292. }
  293. write_bootdata (addr + 1, system, count);
  294. }
  295. }
  296. switch(system){
  297. case FAILSAFE_BOOT:
  298. printf ("Booting failsafe system\n");
  299. setenv ("bootargs", "panic=1 root=/dev/hda7");
  300. setenv ("bootcmd", "ide reset;disk 0x81000000 0:5;run addmisc;bootm");
  301. break;
  302. case SYSTEM_BOOT:
  303. printf ("Using normal system\n");
  304. setenv ("bootargs", "panic=1 root=/dev/hda4");
  305. setenv ("bootcmd", "ide reset;disk 0x81000000 0:2;run addmisc;bootm");
  306. break;
  307. case SYSTEM2_BOOT:
  308. printf ("Using normal system2\n");
  309. setenv ("bootargs", "panic=1 root=/dev/hda9");
  310. setenv ("bootcmd", "ide reset;disk 0x81000000 0:8;run addmisc;bootm");
  311. break;
  312. default:
  313. printf ("Invalid system %d\n", system);
  314. printf ("Hanging\n");
  315. while(1);
  316. }
  317. }
  318. int misc_init_r(void){
  319. u8 Rx[80];
  320. u8 Tx[5];
  321. int page;
  322. int read = 0;
  323. WATCHDOG_RESET();
  324. if (ee_init_cpu_data ()) {
  325. printf ("EEPROM init failed\n");
  326. return (0);
  327. }
  328. /* Check which release to boot */
  329. check_boot_tries ();
  330. /* Read the pages where ethernet address is stored */
  331. for (page = EE_USER_PAGE_0; page <= EE_USER_PAGE_0 + 2; page++) {
  332. /* Copy from nvram to scratchpad */
  333. Tx[0] = RECALL_MEMORY;
  334. Tx[1] = page;
  335. if (ee_do_cpu_command (Tx, 2, NULL, 0, 1)) {
  336. printf ("EE user page %d recall failed\n", page);
  337. return (0);
  338. }
  339. Tx[0] = READ_SCRATCHPAD;
  340. if (ee_do_cpu_command (Tx, 2, Rx + read, 9, 1)) {
  341. printf ("EE user page %d read failed\n", page);
  342. return (0);
  343. }
  344. /* Crc in 9:th byte */
  345. if (!ee_crc_ok (Rx + read, 8, *(Rx + read + 8))) {
  346. printf ("EE read failed, page %d. CRC error\n", page);
  347. return (0);
  348. }
  349. read += 8;
  350. }
  351. /* Add eos after eth addr */
  352. Rx[17] = 0;
  353. printf ("Ethernet addr read from eeprom: %s\n\n", Rx);
  354. if ((Rx[2] != ':') |
  355. (Rx[5] != ':') |
  356. (Rx[8] != ':') | (Rx[11] != ':') | (Rx[14] != ':')) {
  357. printf ("*** ethernet addr invalid, using default ***\n");
  358. } else {
  359. setenv ("ethaddr", Rx);
  360. }
  361. return (0);
  362. }