au1x00_eth.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. #ifdef CONFIG_AU1550
  46. #define ETH0_BASE AU1550_ETH0_BASE
  47. #define MAC0_ENABLE AU1550_MAC0_ENABLE
  48. #else
  49. #error "No valid cpu set"
  50. #endif
  51. #endif
  52. #endif
  53. #endif
  54. #include <common.h>
  55. #include <malloc.h>
  56. #include <net.h>
  57. #include <command.h>
  58. #include <asm/io.h>
  59. #include <asm/au1x00.h>
  60. /* Ethernet Transmit and Receive Buffers */
  61. #define DBUF_LENGTH 1520
  62. #define PKT_MAXBUF_SIZE 1518
  63. static char txbuf[DBUF_LENGTH];
  64. static int next_tx;
  65. static int next_rx;
  66. /* 4 rx and 4 tx fifos */
  67. #define NO_OF_FIFOS 4
  68. typedef struct{
  69. u32 status;
  70. u32 addr;
  71. u32 len; /* Only used for tx */
  72. u32 not_used;
  73. } mac_fifo_t;
  74. mac_fifo_t mac_fifo[NO_OF_FIFOS];
  75. #define MAX_WAIT 1000
  76. static int au1x00_send(struct eth_device* dev, volatile void *packet, int length){
  77. volatile mac_fifo_t *fifo_tx =
  78. (volatile mac_fifo_t*)(MAC0_TX_DMA_ADDR+MAC_TX_BUFF0_STATUS);
  79. int i;
  80. int res;
  81. /* tx fifo should always be idle */
  82. fifo_tx[next_tx].len = length;
  83. fifo_tx[next_tx].addr = (virt_to_phys(packet))|TX_DMA_ENABLE;
  84. au_sync();
  85. udelay(1);
  86. i=0;
  87. while(!(fifo_tx[next_tx].addr&TX_T_DONE)){
  88. if(i>MAX_WAIT){
  89. printf("TX timeout\n");
  90. break;
  91. }
  92. udelay(1);
  93. i++;
  94. }
  95. /* Clear done bit */
  96. fifo_tx[next_tx].addr = 0;
  97. fifo_tx[next_tx].len = 0;
  98. au_sync();
  99. res = fifo_tx[next_tx].status;
  100. next_tx++;
  101. if(next_tx>=NO_OF_FIFOS){
  102. next_tx=0;
  103. }
  104. return(res);
  105. }
  106. static int au1x00_recv(struct eth_device* dev){
  107. volatile mac_fifo_t *fifo_rx =
  108. (volatile mac_fifo_t*)(MAC0_RX_DMA_ADDR+MAC_RX_BUFF0_STATUS);
  109. int length;
  110. u32 status;
  111. for(;;){
  112. if(!(fifo_rx[next_rx].addr&RX_T_DONE)){
  113. /* Nothing has been received */
  114. return(-1);
  115. }
  116. status = fifo_rx[next_rx].status;
  117. length = status&0x3FFF;
  118. if(status&RX_ERROR){
  119. printf("Rx error 0x%x\n", status);
  120. }
  121. else{
  122. /* Pass the packet up to the protocol layers. */
  123. NetReceive(NetRxPackets[next_rx], length - 4);
  124. }
  125. fifo_rx[next_rx].addr = (virt_to_phys(NetRxPackets[next_rx]))|RX_DMA_ENABLE;
  126. next_rx++;
  127. if(next_rx>=NO_OF_FIFOS){
  128. next_rx=0;
  129. }
  130. } /* for */
  131. return(0); /* Does anyone use this? */
  132. }
  133. static int au1x00_init(struct eth_device* dev, bd_t * bd){
  134. volatile u32 *macen = (volatile u32*)MAC0_ENABLE;
  135. volatile u32 *mac_ctrl = (volatile u32*)(ETH0_BASE+MAC_CONTROL);
  136. volatile u32 *mac_addr_high = (volatile u32*)(ETH0_BASE+MAC_ADDRESS_HIGH);
  137. volatile u32 *mac_addr_low = (volatile u32*)(ETH0_BASE+MAC_ADDRESS_LOW);
  138. volatile u32 *mac_mcast_high = (volatile u32*)(ETH0_BASE+MAC_MCAST_HIGH);
  139. volatile u32 *mac_mcast_low = (volatile u32*)(ETH0_BASE+MAC_MCAST_LOW);
  140. volatile mac_fifo_t *fifo_tx =
  141. (volatile mac_fifo_t*)(MAC0_TX_DMA_ADDR+MAC_TX_BUFF0_STATUS);
  142. volatile mac_fifo_t *fifo_rx =
  143. (volatile mac_fifo_t*)(MAC0_RX_DMA_ADDR+MAC_RX_BUFF0_STATUS);
  144. int i;
  145. next_tx = 0;
  146. next_rx = 0;
  147. /* We have to enable clocks before releasing reset */
  148. *macen = MAC_EN_CLOCK_ENABLE;
  149. udelay(10);
  150. /* Enable MAC0 */
  151. /* We have to release reset before accessing registers */
  152. *macen = MAC_EN_CLOCK_ENABLE|MAC_EN_RESET0|
  153. MAC_EN_RESET1|MAC_EN_RESET2;
  154. udelay(10);
  155. for(i=0;i<NO_OF_FIFOS;i++){
  156. fifo_tx[i].len = 0;
  157. fifo_tx[i].addr = virt_to_phys(&txbuf[0]);
  158. fifo_rx[i].addr = (virt_to_phys(NetRxPackets[i]))|RX_DMA_ENABLE;
  159. }
  160. /* Put mac addr in little endian */
  161. #define ea eth_get_dev()->enetaddr
  162. *mac_addr_high = (ea[5] << 8) | (ea[4] ) ;
  163. *mac_addr_low = (ea[3] << 24) | (ea[2] << 16) |
  164. (ea[1] << 8) | (ea[0] ) ;
  165. #undef ea
  166. *mac_mcast_low = 0;
  167. *mac_mcast_high = 0;
  168. /* Make sure the MAC buffer is in the correct endian mode */
  169. #ifdef __LITTLE_ENDIAN
  170. *mac_ctrl = MAC_FULL_DUPLEX;
  171. udelay(1);
  172. *mac_ctrl = MAC_FULL_DUPLEX|MAC_RX_ENABLE|MAC_TX_ENABLE;
  173. #else
  174. *mac_ctrl = MAC_BIG_ENDIAN|MAC_FULL_DUPLEX;
  175. udelay(1);
  176. *mac_ctrl = MAC_BIG_ENDIAN|MAC_FULL_DUPLEX|MAC_RX_ENABLE|MAC_TX_ENABLE;
  177. #endif
  178. return(1);
  179. }
  180. static void au1x00_halt(struct eth_device* dev){
  181. }
  182. int au1x00_enet_initialize(bd_t *bis){
  183. struct eth_device* dev;
  184. dev = (struct eth_device*) malloc(sizeof *dev);
  185. memset(dev, 0, sizeof *dev);
  186. sprintf(dev->name, "Au1X00 ETHERNET");
  187. dev->iobase = 0;
  188. dev->priv = 0;
  189. dev->init = au1x00_init;
  190. dev->halt = au1x00_halt;
  191. dev->send = au1x00_send;
  192. dev->recv = au1x00_recv;
  193. eth_register(dev);
  194. return 1;
  195. }
  196. #endif /* CONFIG_AU1X00 */