3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 * Thomas Langer, Ralph Hempel
8 * See file CREDITS for list of people who contributed to this
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
31 #include <asm/addrspace.h>
33 #include <asm/reboot.h>
35 #if defined(CONFIG_CMD_HTTPD)
39 extern ulong
ifx_get_ddr_hz(void);
40 extern ulong
ifx_get_cpuclk(void);
42 /* definitions for external PHYs / Switches */
43 /* Split values into phy address and register address */
44 #define PHYADDR(_reg) ((_reg >> 5) & 0xff), (_reg & 0x1f)
46 /* IDs and registers of known external switches */
47 #define ID_SAMURAI_0 0x1020
48 #define ID_SAMURAI_1 0x0007
49 #define SAMURAI_ID_REG0 0xA0
50 #define SAMURAI_ID_REG1 0xA1
51 #define ID_TANTOS 0x2599
55 #define REV_MII_MODE 2
56 #define RED_MII_MODE_IC 3 /*Input clock */
57 #define RGMII_MODE_100MB 4
58 #define TURBO_REV_MII_MODE 6 /*Turbo Rev Mii mode */
59 #define RED_MII_MODE_OC 7 /*Output clock */
60 #define RGMII_MODE_10MB 8
62 #define mdelay(n) udelay((n)*1000)
64 static void ar9_sw_chip_init(u8 port
, u8 mode
);
65 static void ar9_enable_sw_port(u8 port
, u8 state
);
66 static void ar9_configure_sw_port(u8 port
, u8 mode
);
67 static u16
ar9_smi_reg_read(u16 reg
);
68 static u16
ar9_smi_reg_write(u16 reg
, u16 data
);
69 static char * const name
= "lq_cpe_eth";
70 static int external_switch_init(void);
72 void _machine_restart(void)
74 *AR9_RCU_RST_REQ
|= AR9_RST_ALL
;
77 #ifdef CONFIG_SYS_RAMBOOT
78 phys_size_t
initdram(int board_type
)
80 return get_ram_size((long *)CONFIG_SYS_SDRAM_BASE
, CONFIG_SYS_MAX_RAM
);
82 #elif defined(CONFIG_USE_DDR_RAM)
83 phys_size_t
initdram(int board_type
)
85 return (CONFIG_SYS_MAX_RAM
);
89 static ulong
max_sdram_size(void) /* per Chip Select */
91 /* The only supported SDRAM data width is 16bit.
95 /* The only supported number of SDRAM banks is 4.
99 ulong cfgpb0
= *AR9_SDRAM_MC_CFGPB0
;
100 int cols
= cfgpb0
& 0xF;
101 int rows
= (cfgpb0
& 0xF0) >> 4;
102 ulong size
= (1 << (rows
+ cols
)) * CFG_DW
* CFG_NB
;
108 * Check memory range for valid RAM. A simple memory test determines
109 * the actually available RAM size between addresses `base' and
113 static long int dram_size(long int *base
, long int maxsize
)
115 volatile long int *addr
;
117 ulong save
[32]; /* to make test non-destructive */
120 for (cnt
= (maxsize
/ sizeof (long)) >> 1; cnt
> 0; cnt
>>= 1) {
121 addr
= base
+ cnt
; /* pointer arith! */
127 /* write 0 to base address */
132 /* check at base address */
133 if ((val
= *addr
) != 0) {
138 for (cnt
= 1; cnt
< maxsize
/ sizeof (long); cnt
<<= 1) {
139 addr
= base
+ cnt
; /* pointer arith! */
145 return (cnt
* sizeof (long));
151 phys_size_t
initdram(int board_type
)
153 int rows
, cols
, best_val
= *AR9_SDRAM_MC_CFGPB0
;
154 ulong size
, max_size
= 0;
157 /* load t9 into our_address */
158 asm volatile ("move %0, $25" : "=r" (our_address
) :);
160 /* Can't probe for RAM size unless we are running from Flash.
161 * find out whether running from DRAM or Flash.
163 if (CPHYSADDR(our_address
) < CPHYSADDR(PHYS_FLASH_1
))
165 return max_sdram_size();
168 for (cols
= 0x8; cols
<= 0xC; cols
++)
170 for (rows
= 0xB; rows
<= 0xD; rows
++)
172 *AR9_SDRAM_MC_CFGPB0
= (0x14 << 8) |
174 size
= get_ram_size((long *)CONFIG_SYS_SDRAM_BASE
,
179 best_val
= *AR9_SDRAM_MC_CFGPB0
;
185 *AR9_SDRAM_MC_CFGPB0
= best_val
;
190 int checkboard (void)
192 unsigned long chipid
= *AR9_MPS_CHIPID
;
197 part_num
= AR9_MPS_CHIPID_PARTNUM_GET(chipid
);
216 printf ("unknown, chip part number 0x%03X ", part_num
);
219 printf ("V1.%ld, ", AR9_MPS_CHIPID_VERSION_GET(chipid
));
221 printf("DDR Speed %ld MHz, ", ifx_get_ddr_hz()/1000000);
222 printf("CPU Speed %ld MHz\n", ifx_get_cpuclk()/1000000);
227 #ifdef CONFIG_SKIP_LOWLEVEL_INIT
228 int board_early_init_f(void)
230 #ifdef CONFIG_EBU_ADDSEL0
231 (*AR9_EBU_ADDSEL0
) = CONFIG_EBU_ADDSEL0
;
233 #ifdef CONFIG_EBU_ADDSEL1
234 (*AR9_EBU_ADDSEL1
) = CONFIG_EBU_ADDSEL1
;
236 #ifdef CONFIG_EBU_ADDSEL2
237 (*AR9_EBU_ADDSEL2
) = CONFIG_EBU_ADDSEL2
;
239 #ifdef CONFIG_EBU_ADDSEL3
240 (*AR9_EBU_ADDSEL3
) = CONFIG_EBU_ADDSEL3
;
242 #ifdef CONFIG_EBU_BUSCON0
243 (*AR9_EBU_BUSCON0
) = CONFIG_EBU_BUSCON0
;
245 #ifdef CONFIG_EBU_BUSCON1
246 (*AR9_EBU_BUSCON1
) = CONFIG_EBU_BUSCON1
;
248 #ifdef CONFIG_EBU_BUSCON2
249 (*AR9_EBU_BUSCON2
) = CONFIG_EBU_BUSCON2
;
251 #ifdef CONFIG_EBU_BUSCON3
252 (*AR9_EBU_BUSCON3
) = CONFIG_EBU_BUSCON3
;
257 #endif /* CONFIG_SKIP_LOWLEVEL_INIT */
259 int board_eth_init(bd_t
*bis
)
261 #if defined(CONFIG_IFX_ETOP)
263 *AR9_PMU_PWDCR
&= 0xFFFFEFDF;
264 *AR9_PMU_PWDCR
&= ~AR9_PMU_DMA
; /* enable DMA from PMU */
266 if (lq_eth_initialize(bis
) < 0)
269 *AR9_RCU_RST_REQ
|= 1;
271 *AR9_RCU_RST_REQ
&= (unsigned long)~1;
274 #ifdef CONFIG_EXTRA_SWITCH
275 if (external_switch_init()<0)
277 #endif /* CONFIG_EXTRA_SWITCH */
278 #endif /* CONFIG_IFX_ETOP */
283 static void ar9_configure_sw_port(u8 port
, u8 mode
)
287 if (mode
== 1) //MII mode
289 *AR9_GPIO_P2_ALTSEL0
= *AR9_GPIO_P2_ALTSEL0
| (0xf000);
290 *AR9_GPIO_P2_ALTSEL1
= *AR9_GPIO_P2_ALTSEL1
& ~(0xf000);
291 *AR9_GPIO_P2_DIR
= (*AR9_GPIO_P2_DIR
& ~(0xf000)) | 0x2000;
292 *AR9_GPIO_P2_OD
= *AR9_GPIO_P2_OD
| 0x2000;
294 else if(mode
== 2 || mode
== 6) //Rev Mii mode
296 *AR9_GPIO_P2_ALTSEL0
= *AR9_GPIO_P2_ALTSEL0
| (0xf000);
297 *AR9_GPIO_P2_ALTSEL1
= *AR9_GPIO_P2_ALTSEL1
& ~(0xf000);
298 *AR9_GPIO_P2_DIR
= (*AR9_GPIO_P2_DIR
| (0xf000)) & ~0x2000;
299 *AR9_GPIO_P2_OD
= *AR9_GPIO_P2_OD
| 0xd000;
304 if (mode
== 1) //MII mode
306 *AR9_GPIO_P2_ALTSEL0
= *AR9_GPIO_P2_ALTSEL0
| (0x0303);
307 *AR9_GPIO_P2_ALTSEL1
= *AR9_GPIO_P2_ALTSEL1
& ~(0x0303);
308 *AR9_GPIO_P2_DIR
= (*AR9_GPIO_P2_DIR
& ~(0x0303)) | 0x0100;
309 *AR9_GPIO_P2_OD
= *AR9_GPIO_P2_OD
| 0x0100;
311 else if(mode
==2 || mode
==6) //Rev Mii mode
313 *AR9_GPIO_P2_ALTSEL0
= *AR9_GPIO_P2_ALTSEL0
| (0x0303);
314 *AR9_GPIO_P2_ALTSEL1
= *AR9_GPIO_P2_ALTSEL1
& ~(0x0303);
315 *AR9_GPIO_P2_DIR
= (*AR9_GPIO_P2_DIR
| (0x0303)) & ~0x0100;
316 *AR9_GPIO_P2_OD
= *AR9_GPIO_P2_OD
| 0x0203;
322 Call this function to place either MAC port 0 or 1 into working mode.
324 port - select ports 0 or 1.
325 state of interface : state
329 3: Reduce MII (input clock)
333 7: Reduce MII (output clock)
335 void ar9_enable_sw_port(u8 port
, u8 state
)
337 REG32(AR9_SW_GCTL0
) |= 0x80000000;
340 REG32(AR9_SW_RGMII_CTL
) &= 0xffcffc0e ;
341 //#if AR9_REFBOARD_TANTOS
342 REG32(0xbf20302c) &= 0xffff81ff;
343 REG32(0xbf20302c) |= 4<<9 ;
345 REG32(AR9_SW_RGMII_CTL
) |= ((u32
)(state
&0x3))<<8;
346 if((state
&0x3) == 0)
348 REG32(AR9_SW_RGMII_CTL
) &= 0xfffffff3;
350 REG32(AR9_SW_RGMII_CTL
) |= 0x4;
352 REG32(AR9_SW_RGMII_CTL
) |= 0x8;
355 REG32(AR9_SW_RGMII_CTL
) |= ((u32
) (1<<20));
357 REG32(AR9_SW_RGMII_CTL
) |= ((u32
) (1<<21));
359 // *AR9_PPE32_ETOP_CFG = *AR9_PPE32_ETOP_CFG & 0xfffffffe;
362 REG32(AR9_SW_RGMII_CTL
) &= 0xff303fff ;
363 REG32(AR9_SW_RGMII_CTL
) |= ((u32
)(state
&0x3))<<18;
364 if((state
&0x3) == 0)
366 REG32(AR9_SW_RGMII_CTL
) &= 0xffffcfff;
368 REG32(AR9_SW_RGMII_CTL
) |= 0x1000;
370 REG32(AR9_SW_RGMII_CTL
) |= 0x2000;
373 REG32(AR9_SW_RGMII_CTL
) |= ((u32
) (1<<22));
375 REG32(AR9_SW_RGMII_CTL
) |= ((u32
) (1<<23));
382 #define AR9_V1_PCI_RST_FIX 1
383 #if AR9_V1_PCI_RST_FIX // 5th June 2008 Add GPIO19 to control EJTAG_TRST
384 *AR9_GPIO_P1_ALTSEL0
= *AR9_GPIO_P1_ALTSEL0
& ~0x8;
385 *AR9_GPIO_P1_ALTSEL1
= *AR9_GPIO_P1_ALTSEL1
& ~0x8;
386 *AR9_GPIO_P1_DIR
= *AR9_GPIO_P1_DIR
| 0x8;
387 *AR9_GPIO_P1_OD
= *AR9_GPIO_P1_OD
| 0x8;
388 *AR9_GPIO_P1_OUT
= *AR9_GPIO_P1_OUT
| 0x8;
389 *AR9_GPIO_P0_ALTSEL0
= *AR9_GPIO_P0_ALTSEL0
& ~0x4000;
390 *AR9_GPIO_P0_ALTSEL1
= *AR9_GPIO_P0_ALTSEL1
& ~0x4000;
391 *AR9_GPIO_P0_DIR
= *AR9_GPIO_P0_DIR
| 0x4000;
392 *AR9_GPIO_P0_OD
= *AR9_GPIO_P0_OD
| 0x4000;
394 *AR9_GPIO_P0_OUT
= *AR9_GPIO_P0_OUT
& ~0x4000;
395 for(i
=0;i
<0x10000;i
++);
396 *AR9_GPIO_P0_OUT
= *AR9_GPIO_P0_OUT
| 0x4000;
397 for(i
=0;i
<0x10000;i
++);
399 *AR9_GPIO_P0_DIR
= *AR9_GPIO_P0_DIR
& ~0x4000;
400 *AR9_GPIO_P1_DIR
= *AR9_GPIO_P1_DIR
& ~0x8;
404 static u16
ar9_smi_reg_read(u16 reg
)
407 while(REG32(AR9_SW_MDIO_CTL
) & 0x8000);
408 REG32(AR9_SW_MDIO_CTL
) = 0x8000| 0x2<<10 | ((u32
) (reg
&0x3ff)) ; /*0x10=MDIO_OP_READ*/
409 for(i
=0;i
<0x3fff;i
++);
411 while(REG32(AR9_SW_MDIO_CTL
) & 0x8000);
412 return((u16
) (REG32(AR9_SW_MDIO_DATA
)));
415 static u16
ar9_smi_reg_write(u16 reg
, u16 data
)
418 while(REG32(AR9_SW_MDIO_CTL
) & 0x8000);
419 REG32(AR9_SW_MDIO_CTL
) = 0x8000| (((u32
) data
)<<16) | 0x01<<10 | ((u32
) (reg
&0x3ff)) ; /*0x01=MDIO_OP_WRITE*/
420 for(i
=0;i
<0x3fff;i
++);
425 static void ar9_sw_chip_init(u8 port
, u8 mode
)
430 debug("\nsearching for switches ... ");
435 /* 25mhz clock out */
436 *AR9_CGU_IFCCR
&= ~(3<<10);
437 *AR9_GPIO_P0_ALTSEL0
= *AR9_GPIO_P0_ALTSEL0
| (1<<3);
438 *AR9_GPIO_P0_ALTSEL1
= *AR9_GPIO_P0_ALTSEL1
& ~(1<<3);
439 *AR9_GPIO_P0_DIR
= *AR9_GPIO_P0_DIR
| (1<<3);
440 *AR9_GPIO_P0_OD
= *AR9_GPIO_P0_OD
| (1<<3);
441 *AR9_GPIO_P2_ALTSEL0
= *AR9_GPIO_P2_ALTSEL0
& ~(1<<0);
442 *AR9_GPIO_P2_ALTSEL1
= *AR9_GPIO_P2_ALTSEL1
& ~(1<<0);
443 *AR9_GPIO_P2_DIR
= *AR9_GPIO_P2_DIR
| (1<<0);
444 *AR9_GPIO_P2_OD
= *AR9_GPIO_P2_OD
| (1<<0);
446 *AR9_PMU_PWDCR
= (*AR9_PMU_PWDCR
& 0xFFFBDFDF) ;
447 *AR9_PMU_PWDCR
= (*AR9_PMU_PWDCR
& ~(AR9_PMU_DMA
| AR9_PMU_SWITCH
));
448 *AR9_PMU_PWDCR
= (*AR9_PMU_PWDCR
| AR9_PMU_USB0
| AR9_PMU_USB0_P
);
450 *AR9_GPIO_P2_OUT
&= ~(1<<0);
453 ar9_configure_sw_port(port
, mode
);
454 ar9_enable_sw_port(port
, mode
);
455 REG32(AR9_SW_P0_CTL
) |= 0x400000; /* disable mdio polling for tantos */
458 /*GPIO 55(P3.7) used as output, set high*/
459 *AR9_GPIO_P3_OD
|=(1<<7);
460 *AR9_GPIO_P3_DIR
|= (1<<7);
461 *AR9_GPIO_P3_ALTSEL0
&=~(1<<7);
462 *AR9_GPIO_P3_ALTSEL1
&=~(1<<7);
466 *AR9_GPIO_P3_OUT
&= ~(1<<7);
469 *AR9_GPIO_P3_OUT
|=(1<<7);
473 REG32(AR9_SW_P0_CTL
) |= 0x40001;
475 REG32(AR9_SW_P1_CTL
) |= 0x40001;
477 REG32(AR9_SW_P2_CTL
) |= 0x40001;
478 REG32(AR9_SW_PMAC_HD_CTL
) |= 0x40000; /* enable CRC */
480 *AR9_GPIO_P2_ALTSEL0
= *AR9_GPIO_P2_ALTSEL0
| (0xc00);
481 *AR9_GPIO_P2_ALTSEL1
= *AR9_GPIO_P2_ALTSEL1
& ~(0xc00);
482 *AR9_GPIO_P2_DIR
= *AR9_GPIO_P2_DIR
| 0xc00;
483 *AR9_GPIO_P2_OD
= *AR9_GPIO_P2_OD
| 0xc00;
486 chipid
= (unsigned short)(ar9_smi_reg_read(0x101));
487 printf("\nswitch chip id=%08x\n",chipid
);
488 if (chipid
!= ID_TANTOS
) {
489 debug("whatever detected\n");
490 ar9_smi_reg_write(0x1,0x840f);
491 ar9_smi_reg_write(0x3,0x840f);
492 ar9_smi_reg_write(0x5,0x840f);
493 ar9_smi_reg_write(0x7,0x840f);
494 ar9_smi_reg_write(0x8,0x840f);
495 ar9_smi_reg_write(0x12,0x3602);
496 #ifdef CLK_OUT2_25MHZ
497 ar9_smi_reg_write(0x33,0x4000);
499 } else { // Tantos switch ship
500 debug("Tantos switch detected\n");
501 ar9_smi_reg_write(0xa1,0x0004); /*port 5 force link up*/
502 ar9_smi_reg_write(0xc1,0x0004); /*port 6 force link up*/
503 ar9_smi_reg_write(0xf5,0x0BBB); /*port 4 duplex mode, flow control enable,1000Mbit/s*/
504 /*port 5 duplex mode, flow control enable, 1000Mbit/s*/
505 /*port 6 duplex mode, flow control enable, 1000Mbit/s*/
511 *AR9_RCU_RST_REQ
|= (AR9_RCU_RST_REQ_DMA
| AR9_RCU_RST_REQ_PPE
) ;
513 *AR9_GPIO_P2_OUT
|= (1<<0);
516 static void ar9_dma_init(void)
522 TXWGT 14:12 rw Port Weight for Transmit Direction (the default value \93001\94)
524 TXENDI 11:10 rw Endianness for Transmit Direction
525 Determine a byte swap between memory interface (left hand side) and
526 peripheral interface (right hand side).
527 00B B0_B1_B2_B3 No byte switching
528 01B B1_B0_B3_B2 B0B1B2B3 => B1B0B3B2
529 10B B2_B3_B0_B1 B0B1B2B3 => B2B3B0B1
531 RXENDI 9:8 rw Endianness for Receive Direction
532 Determine a byte swap between peripheral (left hand side) and memory
533 interface (right hand side).
534 00B B0_B1_B2_B3 No byte switching
535 01B B1_B0_B3_B2 B0B1B2B3 => B1B0B3B2
536 10B B2_B3_B0_B1 B0B1B2B3 => B2B3B0B1
537 11B B3_B2_B1_B0 B0B1B2B3 => B3B2B1B0
539 TXBL 5:4 rw Burst Length for Transmit Direction
540 Selects burst length for TX direction.
541 Others are reserved and will result in 2_WORDS burst length.
546 RXBL 3:2 rw Burst Length for Receive Direction
547 Selects burst length for RX direction.
548 Others are reserved and will result in 2_WORDS burst length.
553 *AR9_DMA_PCTRL
= 0x1f28;
556 #ifdef CONFIG_EXTRA_SWITCH
557 static int external_switch_init(void)
559 ar9_sw_chip_init(0, RGMII_MODE
);
565 #endif /* CONFIG_EXTRA_SWITCH */
567 #if defined(CONFIG_CMD_HTTPD)
568 int do_http_upgrade(const unsigned char *data
, const ulong size
)
572 if(getenv ("ram_addr") == NULL
)
574 if(getenv ("kernel_addr") == NULL
)
576 /* check the image */
577 if(run_command("imi ${ram_addr}", 0) < 0) {
580 /* write the image to the flash */
581 puts("http ugrade ...\n");
582 sprintf(buf
, "era ${kernel_addr} +0x%x; cp.b ${ram_addr} ${kernel_addr} 0x%x", size
, size
);
583 return run_command(buf
, 0);
586 int do_http_progress(const int state
)
588 /* toggle LED's here */
590 case HTTP_PROGRESS_START
:
591 puts("http start\n");
593 case HTTP_PROGRESS_TIMEOUT
:
596 case HTTP_PROGRESS_UPLOAD_READY
:
597 puts("http upload ready\n");
599 case HTTP_PROGRESS_UGRADE_READY
:
600 puts("http ugrade ready\n");
602 case HTTP_PROGRESS_UGRADE_FAILED
:
603 puts("http ugrade failed\n");
609 unsigned long do_http_tmp_address(void)
611 char *s
= getenv ("ram_addr");
613 ulong tmp
= simple_strtoul (s
, NULL
, 16);
616 return 0 /*0x80a00000*/;