au1x00_eth.c 6.9 KB

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