ks8695eth.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * ks8695eth.c -- KS8695 ethernet driver
  3. *
  4. * (C) Copyright 2004-2005, Greg Ungerer <greg.ungerer@opengear.com>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. /****************************************************************************/
  9. #include <common.h>
  10. #include <malloc.h>
  11. #include <net.h>
  12. #include <asm/io.h>
  13. #include <asm/arch/platform.h>
  14. /****************************************************************************/
  15. /*
  16. * Hardware register access to the KS8695 LAN ethernet port
  17. * (well, it is the 4 port switch really).
  18. */
  19. #define ks8695_read(a) *((volatile unsigned long *) (KS8695_IO_BASE + (a)))
  20. #define ks8695_write(a,v) *((volatile unsigned long *) (KS8695_IO_BASE + (a))) = (v)
  21. /****************************************************************************/
  22. /*
  23. * Define the descriptor in-memory data structures.
  24. */
  25. struct ks8695_txdesc {
  26. uint32_t owner;
  27. uint32_t ctrl;
  28. uint32_t addr;
  29. uint32_t next;
  30. };
  31. struct ks8695_rxdesc {
  32. uint32_t status;
  33. uint32_t ctrl;
  34. uint32_t addr;
  35. uint32_t next;
  36. };
  37. /****************************************************************************/
  38. /*
  39. * Allocate local data structures to use for receiving and sending
  40. * packets. Just to keep it all nice and simple.
  41. */
  42. #define TXDESCS 4
  43. #define RXDESCS 4
  44. #define BUFSIZE 2048
  45. volatile struct ks8695_txdesc ks8695_tx[TXDESCS] __attribute__((aligned(256)));
  46. volatile struct ks8695_rxdesc ks8695_rx[RXDESCS] __attribute__((aligned(256)));
  47. volatile uint8_t ks8695_bufs[BUFSIZE*(TXDESCS+RXDESCS)] __attribute__((aligned(2048)));;
  48. /****************************************************************************/
  49. /*
  50. * Ideally we want to use the MAC address stored in flash.
  51. * But we do some sanity checks in case they are not present
  52. * first.
  53. */
  54. unsigned char eth_mac[] = {
  55. 0x00, 0x13, 0xc6, 0x00, 0x00, 0x00
  56. };
  57. void ks8695_getmac(void)
  58. {
  59. unsigned char *fp;
  60. int i;
  61. /* Check if flash MAC is valid */
  62. fp = (unsigned char *) 0x0201c000;
  63. for (i = 0; (i < 6); i++) {
  64. if ((fp[i] != 0) && (fp[i] != 0xff))
  65. break;
  66. }
  67. /* If we found a valid looking MAC address then use it */
  68. if (i < 6)
  69. memcpy(&eth_mac[0], fp, 6);
  70. }
  71. /****************************************************************************/
  72. static int ks8695_eth_init(struct eth_device *dev, bd_t *bd)
  73. {
  74. int i;
  75. debug ("%s(%d): eth_reset()\n", __FILE__, __LINE__);
  76. /* Reset the ethernet engines first */
  77. ks8695_write(KS8695_LAN_DMA_TX, 0x80000000);
  78. ks8695_write(KS8695_LAN_DMA_RX, 0x80000000);
  79. ks8695_getmac();
  80. /* Set MAC address */
  81. ks8695_write(KS8695_LAN_MAC_LOW, (eth_mac[5] | (eth_mac[4] << 8) |
  82. (eth_mac[3] << 16) | (eth_mac[2] << 24)));
  83. ks8695_write(KS8695_LAN_MAC_HIGH, (eth_mac[1] | (eth_mac[0] << 8)));
  84. /* Turn the 4 port switch on */
  85. i = ks8695_read(KS8695_SWITCH_CTRL0);
  86. ks8695_write(KS8695_SWITCH_CTRL0, (i | 0x1));
  87. /* ks8695_write(KS8695_WAN_CONTROL, 0x3f000066); */
  88. /* Initialize descriptor rings */
  89. for (i = 0; (i < TXDESCS); i++) {
  90. ks8695_tx[i].owner = 0;
  91. ks8695_tx[i].ctrl = 0;
  92. ks8695_tx[i].addr = (uint32_t) &ks8695_bufs[i*BUFSIZE];
  93. ks8695_tx[i].next = (uint32_t) &ks8695_tx[i+1];
  94. }
  95. ks8695_tx[TXDESCS-1].ctrl = 0x02000000;
  96. ks8695_tx[TXDESCS-1].next = (uint32_t) &ks8695_tx[0];
  97. for (i = 0; (i < RXDESCS); i++) {
  98. ks8695_rx[i].status = 0x80000000;
  99. ks8695_rx[i].ctrl = BUFSIZE - 4;
  100. ks8695_rx[i].addr = (uint32_t) &ks8695_bufs[(i+TXDESCS)*BUFSIZE];
  101. ks8695_rx[i].next = (uint32_t) &ks8695_rx[i+1];
  102. }
  103. ks8695_rx[RXDESCS-1].ctrl |= 0x00080000;
  104. ks8695_rx[RXDESCS-1].next = (uint32_t) &ks8695_rx[0];
  105. /* The KS8695 is pretty slow reseting the ethernets... */
  106. udelay(2000000);
  107. /* Enable the ethernet engine */
  108. ks8695_write(KS8695_LAN_TX_LIST, (uint32_t) &ks8695_tx[0]);
  109. ks8695_write(KS8695_LAN_RX_LIST, (uint32_t) &ks8695_rx[0]);
  110. ks8695_write(KS8695_LAN_DMA_TX, 0x3);
  111. ks8695_write(KS8695_LAN_DMA_RX, 0x71);
  112. ks8695_write(KS8695_LAN_DMA_RX_START, 0x1);
  113. printf("KS8695 ETHERNET: %pM\n", eth_mac);
  114. return 0;
  115. }
  116. /****************************************************************************/
  117. static void ks8695_eth_halt(struct eth_device *dev)
  118. {
  119. debug ("%s(%d): eth_halt()\n", __FILE__, __LINE__);
  120. /* Reset the ethernet engines */
  121. ks8695_write(KS8695_LAN_DMA_TX, 0x80000000);
  122. ks8695_write(KS8695_LAN_DMA_RX, 0x80000000);
  123. }
  124. /****************************************************************************/
  125. static int ks8695_eth_recv(struct eth_device *dev)
  126. {
  127. volatile struct ks8695_rxdesc *dp;
  128. int i, len = 0;
  129. debug ("%s(%d): eth_rx()\n", __FILE__, __LINE__);
  130. for (i = 0; (i < RXDESCS); i++) {
  131. dp= &ks8695_rx[i];
  132. if ((dp->status & 0x80000000) == 0) {
  133. len = (dp->status & 0x7ff) - 4;
  134. NetReceive((void *) dp->addr, len);
  135. dp->status = 0x80000000;
  136. ks8695_write(KS8695_LAN_DMA_RX_START, 0x1);
  137. break;
  138. }
  139. }
  140. return len;
  141. }
  142. /****************************************************************************/
  143. static int ks8695_eth_send(struct eth_device *dev, void *packet, int len)
  144. {
  145. volatile struct ks8695_txdesc *dp;
  146. static int next = 0;
  147. debug ("%s(%d): eth_send(packet=%p,len=%d)\n", __FILE__, __LINE__,
  148. packet, len);
  149. dp = &ks8695_tx[next];
  150. memcpy((void *) dp->addr, (void *) packet, len);
  151. if (len < 64) {
  152. memset((void *) (dp->addr + len), 0, 64-len);
  153. len = 64;
  154. }
  155. dp->ctrl = len | 0xe0000000;
  156. dp->owner = 0x80000000;
  157. ks8695_write(KS8695_LAN_DMA_TX, 0x3);
  158. ks8695_write(KS8695_LAN_DMA_TX_START, 0x1);
  159. if (++next >= TXDESCS)
  160. next = 0;
  161. return 0;
  162. }
  163. /****************************************************************************/
  164. int ks8695_eth_initialize(void)
  165. {
  166. struct eth_device *dev;
  167. dev = malloc(sizeof(*dev));
  168. if (dev == NULL)
  169. return -1;
  170. memset(dev, 0, sizeof(*dev));
  171. dev->iobase = KS8695_IO_BASE + KS8695_LAN_DMA_TX;
  172. dev->init = ks8695_eth_init;
  173. dev->halt = ks8695_eth_halt;
  174. dev->send = ks8695_eth_send;
  175. dev->recv = ks8695_eth_recv;
  176. strcpy(dev->name, "ks8695eth");
  177. eth_register(dev);
  178. return 0;
  179. }