au1x00_eth.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* Only eth0 supported for now
  2. *
  3. * (C) Copyright 2003
  4. * Thomas.Lange@corelatus.se
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #include <config.h>
  25. #ifdef CONFIG_AU1X00
  26. #if defined(CFG_DISCOVER_PHY) || (CONFIG_COMMANDS & CFG_CMD_MII)
  27. #error "PHY and MII not supported yet"
  28. /* We just assume that we are running 100FD for now */
  29. /* We all use switches, right? ;-) */
  30. #endif
  31. /* I assume ethernet behaves like au1000 */
  32. #ifdef CONFIG_AU1000
  33. /* Base address differ between cpu:s */
  34. #define ETH0_BASE AU1000_ETH0_BASE
  35. #define MAC0_ENABLE AU1000_MAC0_ENABLE
  36. #else
  37. #ifdef CONFIG_AU1100
  38. #define ETH0_BASE AU1100_ETH0_BASE
  39. #define MAC0_ENABLE AU1100_MAC0_ENABLE
  40. #else
  41. #ifdef CONFIG_AU1500
  42. #define ETH0_BASE AU1500_ETH0_BASE
  43. #define MAC0_ENABLE AU1500_MAC0_ENABLE
  44. #else
  45. #error "No valid cpu set"
  46. #endif
  47. #endif
  48. #endif
  49. #include <common.h>
  50. #include <malloc.h>
  51. #include <net.h>
  52. #include <command.h>
  53. #include <asm/io.h>
  54. #include <asm/au1x00.h>
  55. /* Ethernet Transmit and Receive Buffers */
  56. #define DBUF_LENGTH 1520
  57. #define PKT_MAXBUF_SIZE 1518
  58. static char txbuf[DBUF_LENGTH];
  59. static int next_tx;
  60. static int next_rx;
  61. /* 4 rx and 4 tx fifos */
  62. #define NO_OF_FIFOS 4
  63. typedef struct{
  64. u32 status;
  65. u32 addr;
  66. u32 len; /* Only used for tx */
  67. u32 not_used;
  68. } mac_fifo_t;
  69. mac_fifo_t mac_fifo[NO_OF_FIFOS];
  70. #define MAX_WAIT 1000
  71. static int au1x00_send(struct eth_device* dev, volatile void *packet, int length){
  72. volatile mac_fifo_t *fifo_tx =
  73. (volatile mac_fifo_t*)(MAC0_TX_DMA_ADDR+MAC_TX_BUFF0_STATUS);
  74. int i;
  75. int res;
  76. /* tx fifo should always be idle */
  77. fifo_tx[next_tx].len = length;
  78. fifo_tx[next_tx].addr = (virt_to_phys(packet))|TX_DMA_ENABLE;
  79. au_sync();
  80. udelay(1);
  81. i=0;
  82. while(!(fifo_tx[next_tx].addr&TX_T_DONE)){
  83. if(i>MAX_WAIT){
  84. printf("TX timeout\n");
  85. break;
  86. }
  87. udelay(1);
  88. i++;
  89. }
  90. /* Clear done bit */
  91. fifo_tx[next_tx].addr = 0;
  92. fifo_tx[next_tx].len = 0;
  93. au_sync();
  94. res = fifo_tx[next_tx].status;
  95. next_tx++;
  96. if(next_tx>=NO_OF_FIFOS){
  97. next_tx=0;
  98. }
  99. return(res);
  100. }
  101. static int au1x00_recv(struct eth_device* dev){
  102. volatile mac_fifo_t *fifo_rx =
  103. (volatile mac_fifo_t*)(MAC0_RX_DMA_ADDR+MAC_RX_BUFF0_STATUS);
  104. int length;
  105. u32 status;
  106. for(;;){
  107. if(!(fifo_rx[next_rx].addr&RX_T_DONE)){
  108. /* Nothing has been received */
  109. return(-1);
  110. }
  111. status = fifo_rx[next_rx].status;
  112. length = status&0x3FFF;
  113. if(status&RX_ERROR){
  114. printf("Rx error 0x%x\n", status);
  115. }
  116. else{
  117. /* Pass the packet up to the protocol layers. */
  118. NetReceive(NetRxPackets[next_rx], length - 4);
  119. }
  120. fifo_rx[next_rx].addr = (virt_to_phys(NetRxPackets[next_rx]))|RX_DMA_ENABLE;
  121. next_rx++;
  122. if(next_rx>=NO_OF_FIFOS){
  123. next_rx=0;
  124. }
  125. } /* for */
  126. return(0); /* Does anyone use this? */
  127. }
  128. static int au1x00_init(struct eth_device* dev, bd_t * bd){
  129. volatile u32 *macen = (volatile u32*)MAC0_ENABLE;
  130. volatile u32 *mac_ctrl = (volatile u32*)(ETH0_BASE+MAC_CONTROL);
  131. volatile u32 *mac_addr_high = (volatile u32*)(ETH0_BASE+MAC_ADDRESS_HIGH);
  132. volatile u32 *mac_addr_low = (volatile u32*)(ETH0_BASE+MAC_ADDRESS_LOW);
  133. volatile u32 *mac_mcast_high = (volatile u32*)(ETH0_BASE+MAC_MCAST_HIGH);
  134. volatile u32 *mac_mcast_low = (volatile u32*)(ETH0_BASE+MAC_MCAST_LOW);
  135. volatile mac_fifo_t *fifo_tx =
  136. (volatile mac_fifo_t*)(MAC0_TX_DMA_ADDR+MAC_TX_BUFF0_STATUS);
  137. volatile mac_fifo_t *fifo_rx =
  138. (volatile mac_fifo_t*)(MAC0_RX_DMA_ADDR+MAC_RX_BUFF0_STATUS);
  139. int i;
  140. next_tx = 0;
  141. next_rx = 0;
  142. /* We have to enable clocks before releasing reset */
  143. *macen = MAC_EN_CLOCK_ENABLE;
  144. udelay(10);
  145. /* Enable MAC0 */
  146. /* We have to release reset before accessing registers */
  147. *macen = MAC_EN_CLOCK_ENABLE|MAC_EN_RESET0|
  148. MAC_EN_RESET1|MAC_EN_RESET2;
  149. udelay(10);
  150. for(i=0;i<NO_OF_FIFOS;i++){
  151. fifo_tx[i].len = 0;
  152. fifo_tx[i].addr = virt_to_phys(&txbuf[0]);
  153. fifo_rx[i].addr = (virt_to_phys(NetRxPackets[i]))|RX_DMA_ENABLE;
  154. }
  155. /* Put mac addr in little endian */
  156. #define ea eth_get_dev()->enetaddr
  157. *mac_addr_high = (ea[5] << 8) | (ea[4] ) ;
  158. *mac_addr_low = (ea[3] << 24) | (ea[2] << 16) |
  159. (ea[1] << 8) | (ea[0] ) ;
  160. #undef ea
  161. *mac_mcast_low = 0;
  162. *mac_mcast_high = 0;
  163. /* Make sure the MAC buffer is in the correct endian mode */
  164. #ifdef __LITTLE_ENDIAN
  165. *mac_ctrl = MAC_FULL_DUPLEX;
  166. udelay(1);
  167. *mac_ctrl = MAC_FULL_DUPLEX|MAC_RX_ENABLE|MAC_TX_ENABLE;
  168. #else
  169. *mac_ctrl = MAC_BIG_ENDIAN|MAC_FULL_DUPLEX;
  170. udelay(1);
  171. *mac_ctrl = MAC_BIG_ENDIAN|MAC_FULL_DUPLEX|MAC_RX_ENABLE|MAC_TX_ENABLE;
  172. #endif
  173. return(1);
  174. }
  175. static void au1x00_halt(struct eth_device* dev){
  176. }
  177. int au1x00_enet_initialize(bd_t *bis){
  178. struct eth_device* dev;
  179. dev = (struct eth_device*) malloc(sizeof *dev);
  180. memset(dev, 0, sizeof *dev);
  181. sprintf(dev->name, "Au1X00 ETHERNET");
  182. dev->iobase = 0;
  183. dev->priv = 0;
  184. dev->init = au1x00_init;
  185. dev->halt = au1x00_halt;
  186. dev->send = au1x00_send;
  187. dev->recv = au1x00_recv;
  188. eth_register(dev);
  189. return 1;
  190. }
  191. #endif /* CONFIG_AU1X00 */