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