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