au1x00_eth.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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)
  27. #error "PHY 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. #if defined(CONFIG_CMD_MII)
  61. #include <miiphy.h>
  62. #endif
  63. /* Ethernet Transmit and Receive Buffers */
  64. #define DBUF_LENGTH 1520
  65. #define PKT_MAXBUF_SIZE 1518
  66. static char txbuf[DBUF_LENGTH];
  67. static int next_tx;
  68. static int next_rx;
  69. /* 4 rx and 4 tx fifos */
  70. #define NO_OF_FIFOS 4
  71. typedef struct{
  72. u32 status;
  73. u32 addr;
  74. u32 len; /* Only used for tx */
  75. u32 not_used;
  76. } mac_fifo_t;
  77. mac_fifo_t mac_fifo[NO_OF_FIFOS];
  78. #define MAX_WAIT 1000
  79. static int au1x00_send(struct eth_device* dev, volatile void *packet, int length){
  80. volatile mac_fifo_t *fifo_tx =
  81. (volatile mac_fifo_t*)(MAC0_TX_DMA_ADDR+MAC_TX_BUFF0_STATUS);
  82. int i;
  83. int res;
  84. /* tx fifo should always be idle */
  85. fifo_tx[next_tx].len = length;
  86. fifo_tx[next_tx].addr = (virt_to_phys(packet))|TX_DMA_ENABLE;
  87. au_sync();
  88. udelay(1);
  89. i=0;
  90. while(!(fifo_tx[next_tx].addr&TX_T_DONE)){
  91. if(i>MAX_WAIT){
  92. printf("TX timeout\n");
  93. break;
  94. }
  95. udelay(1);
  96. i++;
  97. }
  98. /* Clear done bit */
  99. fifo_tx[next_tx].addr = 0;
  100. fifo_tx[next_tx].len = 0;
  101. au_sync();
  102. res = fifo_tx[next_tx].status;
  103. next_tx++;
  104. if(next_tx>=NO_OF_FIFOS){
  105. next_tx=0;
  106. }
  107. return(res);
  108. }
  109. static int au1x00_recv(struct eth_device* dev){
  110. volatile mac_fifo_t *fifo_rx =
  111. (volatile mac_fifo_t*)(MAC0_RX_DMA_ADDR+MAC_RX_BUFF0_STATUS);
  112. int length;
  113. u32 status;
  114. for(;;){
  115. if(!(fifo_rx[next_rx].addr&RX_T_DONE)){
  116. /* Nothing has been received */
  117. return(-1);
  118. }
  119. status = fifo_rx[next_rx].status;
  120. length = status&0x3FFF;
  121. if(status&RX_ERROR){
  122. printf("Rx error 0x%x\n", status);
  123. }
  124. else{
  125. /* Pass the packet up to the protocol layers. */
  126. NetReceive(NetRxPackets[next_rx], length - 4);
  127. }
  128. fifo_rx[next_rx].addr = (virt_to_phys(NetRxPackets[next_rx]))|RX_DMA_ENABLE;
  129. next_rx++;
  130. if(next_rx>=NO_OF_FIFOS){
  131. next_rx=0;
  132. }
  133. } /* for */
  134. return(0); /* Does anyone use this? */
  135. }
  136. static int au1x00_init(struct eth_device* dev, bd_t * bd){
  137. volatile u32 *macen = (volatile u32*)MAC0_ENABLE;
  138. volatile u32 *mac_ctrl = (volatile u32*)(ETH0_BASE+MAC_CONTROL);
  139. volatile u32 *mac_addr_high = (volatile u32*)(ETH0_BASE+MAC_ADDRESS_HIGH);
  140. volatile u32 *mac_addr_low = (volatile u32*)(ETH0_BASE+MAC_ADDRESS_LOW);
  141. volatile u32 *mac_mcast_high = (volatile u32*)(ETH0_BASE+MAC_MCAST_HIGH);
  142. volatile u32 *mac_mcast_low = (volatile u32*)(ETH0_BASE+MAC_MCAST_LOW);
  143. volatile mac_fifo_t *fifo_tx =
  144. (volatile mac_fifo_t*)(MAC0_TX_DMA_ADDR+MAC_TX_BUFF0_STATUS);
  145. volatile mac_fifo_t *fifo_rx =
  146. (volatile mac_fifo_t*)(MAC0_RX_DMA_ADDR+MAC_RX_BUFF0_STATUS);
  147. int i;
  148. next_tx = TX_GET_DMA_BUFFER(fifo_tx[0].addr);
  149. next_rx = RX_GET_DMA_BUFFER(fifo_rx[0].addr);
  150. /* We have to enable clocks before releasing reset */
  151. *macen = MAC_EN_CLOCK_ENABLE;
  152. udelay(10);
  153. /* Enable MAC0 */
  154. /* We have to release reset before accessing registers */
  155. *macen = MAC_EN_CLOCK_ENABLE|MAC_EN_RESET0|
  156. MAC_EN_RESET1|MAC_EN_RESET2;
  157. udelay(10);
  158. for(i=0;i<NO_OF_FIFOS;i++){
  159. fifo_tx[i].len = 0;
  160. fifo_tx[i].addr = virt_to_phys(&txbuf[0]);
  161. fifo_rx[i].addr = (virt_to_phys(NetRxPackets[i]))|RX_DMA_ENABLE;
  162. }
  163. /* Put mac addr in little endian */
  164. #define ea eth_get_dev()->enetaddr
  165. *mac_addr_high = (ea[5] << 8) | (ea[4] ) ;
  166. *mac_addr_low = (ea[3] << 24) | (ea[2] << 16) |
  167. (ea[1] << 8) | (ea[0] ) ;
  168. #undef ea
  169. *mac_mcast_low = 0;
  170. *mac_mcast_high = 0;
  171. /* Make sure the MAC buffer is in the correct endian mode */
  172. #ifdef __LITTLE_ENDIAN
  173. *mac_ctrl = MAC_FULL_DUPLEX;
  174. udelay(1);
  175. *mac_ctrl = MAC_FULL_DUPLEX|MAC_RX_ENABLE|MAC_TX_ENABLE;
  176. #else
  177. *mac_ctrl = MAC_BIG_ENDIAN|MAC_FULL_DUPLEX;
  178. udelay(1);
  179. *mac_ctrl = MAC_BIG_ENDIAN|MAC_FULL_DUPLEX|MAC_RX_ENABLE|MAC_TX_ENABLE;
  180. #endif
  181. return(1);
  182. }
  183. static void au1x00_halt(struct eth_device* dev){
  184. }
  185. int au1x00_enet_initialize(bd_t *bis){
  186. struct eth_device* dev;
  187. if ((dev = (struct eth_device*)malloc(sizeof *dev)) == NULL) {
  188. puts ("malloc failed\n");
  189. return 0;
  190. }
  191. memset(dev, 0, sizeof *dev);
  192. sprintf(dev->name, "Au1X00 ethernet");
  193. dev->iobase = 0;
  194. dev->priv = 0;
  195. dev->init = au1x00_init;
  196. dev->halt = au1x00_halt;
  197. dev->send = au1x00_send;
  198. dev->recv = au1x00_recv;
  199. eth_register(dev);
  200. #if defined(CONFIG_CMD_MII)
  201. miiphy_register(dev->name,
  202. au1x00_miiphy_read, au1x00_miiphy_write);
  203. #endif
  204. return 1;
  205. }
  206. #if defined(CONFIG_CMD_MII)
  207. int au1x00_miiphy_read(char *devname, unsigned char addr,
  208. unsigned char reg, unsigned short * value)
  209. {
  210. volatile u32 *mii_control_reg = (volatile u32*)(ETH0_BASE+MAC_MII_CNTRL);
  211. volatile u32 *mii_data_reg = (volatile u32*)(ETH0_BASE+MAC_MII_DATA);
  212. u32 mii_control;
  213. unsigned int timedout = 20;
  214. while (*mii_control_reg & MAC_MII_BUSY) {
  215. udelay(1000);
  216. if (--timedout == 0) {
  217. printf("au1x00_eth: miiphy_read busy timeout!!\n");
  218. return -1;
  219. }
  220. }
  221. mii_control = MAC_SET_MII_SELECT_REG(reg) |
  222. MAC_SET_MII_SELECT_PHY(addr) | MAC_MII_READ;
  223. *mii_control_reg = mii_control;
  224. timedout = 20;
  225. while (*mii_control_reg & MAC_MII_BUSY) {
  226. udelay(1000);
  227. if (--timedout == 0) {
  228. printf("au1x00_eth: miiphy_read busy timeout!!\n");
  229. return -1;
  230. }
  231. }
  232. *value = *mii_data_reg;
  233. return 0;
  234. }
  235. int au1x00_miiphy_write(char *devname, unsigned char addr,
  236. unsigned char reg, unsigned short value)
  237. {
  238. volatile u32 *mii_control_reg = (volatile u32*)(ETH0_BASE+MAC_MII_CNTRL);
  239. volatile u32 *mii_data_reg = (volatile u32*)(ETH0_BASE+MAC_MII_DATA);
  240. u32 mii_control;
  241. unsigned int timedout = 20;
  242. while (*mii_control_reg & MAC_MII_BUSY) {
  243. udelay(1000);
  244. if (--timedout == 0) {
  245. printf("au1x00_eth: miiphy_write busy timeout!!\n");
  246. return;
  247. }
  248. }
  249. mii_control = MAC_SET_MII_SELECT_REG(reg) |
  250. MAC_SET_MII_SELECT_PHY(addr) | MAC_MII_WRITE;
  251. *mii_data_reg = value;
  252. *mii_control_reg = mii_control;
  253. return 0;
  254. }
  255. #endif
  256. #endif /* CONFIG_AU1X00 */