1 // SPDX-License-Identifier: GPL-2.0+
2 /* Only eth0 supported for now
5 * Thomas.Lange@corelatus.se
9 #if defined(CONFIG_SYS_DISCOVER_PHY)
10 #error "PHY not supported yet"
11 /* We just assume that we are running 100FD for now */
12 /* We all use switches, right? ;-) */
15 /* I assume ethernet behaves like au1000 */
17 #ifdef CONFIG_SOC_AU1000
18 /* Base address differ between cpu:s */
19 #define ETH0_BASE AU1000_ETH0_BASE
20 #define MAC0_ENABLE AU1000_MAC0_ENABLE
22 #ifdef CONFIG_SOC_AU1100
23 #define ETH0_BASE AU1100_ETH0_BASE
24 #define MAC0_ENABLE AU1100_MAC0_ENABLE
26 #ifdef CONFIG_SOC_AU1500
27 #define ETH0_BASE AU1500_ETH0_BASE
28 #define MAC0_ENABLE AU1500_MAC0_ENABLE
30 #ifdef CONFIG_SOC_AU1550
31 #define ETH0_BASE AU1550_ETH0_BASE
32 #define MAC0_ENABLE AU1550_MAC0_ENABLE
34 #error "No valid cpu set"
45 #include <mach/au1x00.h>
47 #if defined(CONFIG_CMD_MII)
51 /* Ethernet Transmit and Receive Buffers */
52 #define DBUF_LENGTH 1520
53 #define PKT_MAXBUF_SIZE 1518
55 static char txbuf[DBUF_LENGTH];
60 /* 4 rx and 4 tx fifos */
66 u32 len; /* Only used for tx */
70 mac_fifo_t mac_fifo[NO_OF_FIFOS];
74 #if defined(CONFIG_CMD_MII)
75 int au1x00_miiphy_read(struct mii_dev *bus, int addr, int devad, int reg)
77 unsigned short value = 0;
78 volatile u32 *mii_control_reg = (volatile u32*)(ETH0_BASE+MAC_MII_CNTRL);
79 volatile u32 *mii_data_reg = (volatile u32*)(ETH0_BASE+MAC_MII_DATA);
81 unsigned int timedout = 20;
83 while (*mii_control_reg & MAC_MII_BUSY) {
85 if (--timedout == 0) {
86 printf("au1x00_eth: miiphy_read busy timeout!!\n");
91 mii_control = MAC_SET_MII_SELECT_REG(reg) |
92 MAC_SET_MII_SELECT_PHY(addr) | MAC_MII_READ;
94 *mii_control_reg = mii_control;
97 while (*mii_control_reg & MAC_MII_BUSY) {
99 if (--timedout == 0) {
100 printf("au1x00_eth: miiphy_read busy timeout!!\n");
104 value = *mii_data_reg;
108 int au1x00_miiphy_write(struct mii_dev *bus, int addr, int devad, int reg,
111 volatile u32 *mii_control_reg = (volatile u32*)(ETH0_BASE+MAC_MII_CNTRL);
112 volatile u32 *mii_data_reg = (volatile u32*)(ETH0_BASE+MAC_MII_DATA);
114 unsigned int timedout = 20;
116 while (*mii_control_reg & MAC_MII_BUSY) {
118 if (--timedout == 0) {
119 printf("au1x00_eth: miiphy_write busy timeout!!\n");
124 mii_control = MAC_SET_MII_SELECT_REG(reg) |
125 MAC_SET_MII_SELECT_PHY(addr) | MAC_MII_WRITE;
127 *mii_data_reg = value;
128 *mii_control_reg = mii_control;
133 static int au1x00_send(struct eth_device *dev, void *packet, int length)
135 volatile mac_fifo_t *fifo_tx =
136 (volatile mac_fifo_t*)(MAC0_TX_DMA_ADDR+MAC_TX_BUFF0_STATUS);
140 /* tx fifo should always be idle */
141 fifo_tx[next_tx].len = length;
142 fifo_tx[next_tx].addr = (virt_to_phys(packet))|TX_DMA_ENABLE;
147 while(!(fifo_tx[next_tx].addr&TX_T_DONE)){
149 printf("TX timeout\n");
157 fifo_tx[next_tx].addr = 0;
158 fifo_tx[next_tx].len = 0;
161 res = fifo_tx[next_tx].status;
164 if(next_tx>=NO_OF_FIFOS){
170 static int au1x00_recv(struct eth_device* dev){
171 volatile mac_fifo_t *fifo_rx =
172 (volatile mac_fifo_t*)(MAC0_RX_DMA_ADDR+MAC_RX_BUFF0_STATUS);
178 if(!(fifo_rx[next_rx].addr&RX_T_DONE)){
179 /* Nothing has been received */
183 status = fifo_rx[next_rx].status;
185 length = status&0x3FFF;
188 printf("Rx error 0x%x\n", status);
190 /* Pass the packet up to the protocol layers. */
191 net_process_received_packet(net_rx_packets[next_rx],
195 fifo_rx[next_rx].addr =
196 (virt_to_phys(net_rx_packets[next_rx])) | RX_DMA_ENABLE;
199 if(next_rx>=NO_OF_FIFOS){
204 return(0); /* Does anyone use this? */
207 static int au1x00_init(struct eth_device* dev, bd_t * bd){
209 volatile u32 *macen = (volatile u32*)MAC0_ENABLE;
210 volatile u32 *mac_ctrl = (volatile u32*)(ETH0_BASE+MAC_CONTROL);
211 volatile u32 *mac_addr_high = (volatile u32*)(ETH0_BASE+MAC_ADDRESS_HIGH);
212 volatile u32 *mac_addr_low = (volatile u32*)(ETH0_BASE+MAC_ADDRESS_LOW);
213 volatile u32 *mac_mcast_high = (volatile u32*)(ETH0_BASE+MAC_MCAST_HIGH);
214 volatile u32 *mac_mcast_low = (volatile u32*)(ETH0_BASE+MAC_MCAST_LOW);
215 volatile mac_fifo_t *fifo_tx =
216 (volatile mac_fifo_t*)(MAC0_TX_DMA_ADDR+MAC_TX_BUFF0_STATUS);
217 volatile mac_fifo_t *fifo_rx =
218 (volatile mac_fifo_t*)(MAC0_RX_DMA_ADDR+MAC_RX_BUFF0_STATUS);
221 next_tx = TX_GET_DMA_BUFFER(fifo_tx[0].addr);
222 next_rx = RX_GET_DMA_BUFFER(fifo_rx[0].addr);
224 /* We have to enable clocks before releasing reset */
225 *macen = MAC_EN_CLOCK_ENABLE;
229 /* We have to release reset before accessing registers */
230 *macen = MAC_EN_CLOCK_ENABLE|MAC_EN_RESET0|
231 MAC_EN_RESET1|MAC_EN_RESET2;
234 for(i=0;i<NO_OF_FIFOS;i++){
236 fifo_tx[i].addr = virt_to_phys(&txbuf[0]);
237 fifo_rx[i].addr = (virt_to_phys(net_rx_packets[i])) |
241 /* Put mac addr in little endian */
242 #define ea eth_get_ethaddr()
243 *mac_addr_high = (ea[5] << 8) | (ea[4] ) ;
244 *mac_addr_low = (ea[3] << 24) | (ea[2] << 16) |
245 (ea[1] << 8) | (ea[0] ) ;
250 /* Make sure the MAC buffer is in the correct endian mode */
251 #ifdef __LITTLE_ENDIAN
252 *mac_ctrl = MAC_FULL_DUPLEX;
254 *mac_ctrl = MAC_FULL_DUPLEX|MAC_RX_ENABLE|MAC_TX_ENABLE;
256 *mac_ctrl = MAC_BIG_ENDIAN|MAC_FULL_DUPLEX;
258 *mac_ctrl = MAC_BIG_ENDIAN|MAC_FULL_DUPLEX|MAC_RX_ENABLE|MAC_TX_ENABLE;
264 static void au1x00_halt(struct eth_device* dev){
265 volatile u32 *macen = (volatile u32*)MAC0_ENABLE;
267 /* Put MAC0 in reset */
271 int au1x00_enet_initialize(bd_t *bis){
272 struct eth_device* dev;
274 if ((dev = (struct eth_device*)malloc(sizeof *dev)) == NULL) {
275 puts ("malloc failed\n");
279 memset(dev, 0, sizeof *dev);
281 strcpy(dev->name, "Au1X00 ethernet");
284 dev->init = au1x00_init;
285 dev->halt = au1x00_halt;
286 dev->send = au1x00_send;
287 dev->recv = au1x00_recv;
291 #if defined(CONFIG_CMD_MII)
293 struct mii_dev *mdiodev = mdio_alloc();
296 strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
297 mdiodev->read = au1x00_miiphy_read;
298 mdiodev->write = au1x00_miiphy_write;
300 retval = mdio_register(mdiodev);
308 int cpu_eth_init(bd_t *bis)
310 au1x00_enet_initialize(bis);