ks8695eth.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * ks8695eth.c -- KS8695 ethernet driver
  3. *
  4. * (C) Copyright 2004-2005, Greg Ungerer <greg.ungerer@opengear.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. /****************************************************************************/
  21. #include <common.h>
  22. #include <malloc.h>
  23. #include <net.h>
  24. #include <asm/io.h>
  25. #include <asm/arch/platform.h>
  26. /****************************************************************************/
  27. /*
  28. * Hardware register access to the KS8695 LAN ethernet port
  29. * (well, it is the 4 port switch really).
  30. */
  31. #define ks8695_read(a) *((volatile unsigned long *) (KS8695_IO_BASE + (a)))
  32. #define ks8695_write(a,v) *((volatile unsigned long *) (KS8695_IO_BASE + (a))) = (v)
  33. /****************************************************************************/
  34. /*
  35. * Define the descriptor in-memory data structures.
  36. */
  37. struct ks8695_txdesc {
  38. uint32_t owner;
  39. uint32_t ctrl;
  40. uint32_t addr;
  41. uint32_t next;
  42. };
  43. struct ks8695_rxdesc {
  44. uint32_t status;
  45. uint32_t ctrl;
  46. uint32_t addr;
  47. uint32_t next;
  48. };
  49. /****************************************************************************/
  50. /*
  51. * Allocate local data structures to use for receiving and sending
  52. * packets. Just to keep it all nice and simple.
  53. */
  54. #define TXDESCS 4
  55. #define RXDESCS 4
  56. #define BUFSIZE 2048
  57. volatile struct ks8695_txdesc ks8695_tx[TXDESCS] __attribute__((aligned(256)));
  58. volatile struct ks8695_rxdesc ks8695_rx[RXDESCS] __attribute__((aligned(256)));
  59. volatile uint8_t ks8695_bufs[BUFSIZE*(TXDESCS+RXDESCS)] __attribute__((aligned(2048)));;
  60. /****************************************************************************/
  61. /*
  62. * Ideally we want to use the MAC address stored in flash.
  63. * But we do some sanity checks in case they are not present
  64. * first.
  65. */
  66. unsigned char eth_mac[] = {
  67. 0x00, 0x13, 0xc6, 0x00, 0x00, 0x00
  68. };
  69. void ks8695_getmac(void)
  70. {
  71. unsigned char *fp;
  72. int i;
  73. /* Check if flash MAC is valid */
  74. fp = (unsigned char *) 0x0201c000;
  75. for (i = 0; (i < 6); i++) {
  76. if ((fp[i] != 0) && (fp[i] != 0xff))
  77. break;
  78. }
  79. /* If we found a valid looking MAC address then use it */
  80. if (i < 6)
  81. memcpy(&eth_mac[0], fp, 6);
  82. }
  83. /****************************************************************************/
  84. void eth_reset(bd_t *bd)
  85. {
  86. int i;
  87. debug ("%s(%d): eth_reset()\n", __FILE__, __LINE__);
  88. /* Reset the ethernet engines first */
  89. ks8695_write(KS8695_LAN_DMA_TX, 0x80000000);
  90. ks8695_write(KS8695_LAN_DMA_RX, 0x80000000);
  91. ks8695_getmac();
  92. /* Set MAC address */
  93. ks8695_write(KS8695_LAN_MAC_LOW, (eth_mac[5] | (eth_mac[4] << 8) |
  94. (eth_mac[3] << 16) | (eth_mac[2] << 24)));
  95. ks8695_write(KS8695_LAN_MAC_HIGH, (eth_mac[1] | (eth_mac[0] << 8)));
  96. /* Turn the 4 port switch on */
  97. i = ks8695_read(KS8695_SWITCH_CTRL0);
  98. ks8695_write(KS8695_SWITCH_CTRL0, (i | 0x1));
  99. /* ks8695_write(KS8695_WAN_CONTROL, 0x3f000066); */
  100. /* Initialize descriptor rings */
  101. for (i = 0; (i < TXDESCS); i++) {
  102. ks8695_tx[i].owner = 0;
  103. ks8695_tx[i].ctrl = 0;
  104. ks8695_tx[i].addr = (uint32_t) &ks8695_bufs[i*BUFSIZE];
  105. ks8695_tx[i].next = (uint32_t) &ks8695_tx[i+1];
  106. }
  107. ks8695_tx[TXDESCS-1].ctrl = 0x02000000;
  108. ks8695_tx[TXDESCS-1].next = (uint32_t) &ks8695_tx[0];
  109. for (i = 0; (i < RXDESCS); i++) {
  110. ks8695_rx[i].status = 0x80000000;
  111. ks8695_rx[i].ctrl = BUFSIZE - 4;
  112. ks8695_rx[i].addr = (uint32_t) &ks8695_bufs[(i+TXDESCS)*BUFSIZE];
  113. ks8695_rx[i].next = (uint32_t) &ks8695_rx[i+1];
  114. }
  115. ks8695_rx[RXDESCS-1].ctrl |= 0x00080000;
  116. ks8695_rx[RXDESCS-1].next = (uint32_t) &ks8695_rx[0];
  117. /* The KS8695 is pretty slow reseting the ethernets... */
  118. udelay(2000000);
  119. /* Enable the ethernet engine */
  120. ks8695_write(KS8695_LAN_TX_LIST, (uint32_t) &ks8695_tx[0]);
  121. ks8695_write(KS8695_LAN_RX_LIST, (uint32_t) &ks8695_rx[0]);
  122. ks8695_write(KS8695_LAN_DMA_TX, 0x3);
  123. ks8695_write(KS8695_LAN_DMA_RX, 0x71);
  124. ks8695_write(KS8695_LAN_DMA_RX_START, 0x1);
  125. printf("KS8695 ETHERNET: %pM\n", eth_mac);
  126. }
  127. /****************************************************************************/
  128. int eth_init(bd_t *bd)
  129. {
  130. debug ("%s(%d): eth_init()\n", __FILE__, __LINE__);
  131. eth_reset(bd);
  132. return 0;
  133. }
  134. /****************************************************************************/
  135. void eth_halt(void)
  136. {
  137. debug ("%s(%d): eth_halt()\n", __FILE__, __LINE__);
  138. /* Reset the ethernet engines */
  139. ks8695_write(KS8695_LAN_DMA_TX, 0x80000000);
  140. ks8695_write(KS8695_LAN_DMA_RX, 0x80000000);
  141. }
  142. /****************************************************************************/
  143. int eth_rx(void)
  144. {
  145. volatile struct ks8695_rxdesc *dp;
  146. int i, len = 0;
  147. debug ("%s(%d): eth_rx()\n", __FILE__, __LINE__);
  148. for (i = 0; (i < RXDESCS); i++) {
  149. dp= &ks8695_rx[i];
  150. if ((dp->status & 0x80000000) == 0) {
  151. len = (dp->status & 0x7ff) - 4;
  152. NetReceive((void *) dp->addr, len);
  153. dp->status = 0x80000000;
  154. ks8695_write(KS8695_LAN_DMA_RX_START, 0x1);
  155. break;
  156. }
  157. }
  158. return len;
  159. }
  160. /****************************************************************************/
  161. int eth_send(volatile void *packet, int len)
  162. {
  163. volatile struct ks8695_txdesc *dp;
  164. static int next = 0;
  165. debug ("%s(%d): eth_send(packet=%x,len=%d)\n", __FILE__, __LINE__,
  166. packet, len);
  167. dp = &ks8695_tx[next];
  168. memcpy((void *) dp->addr, (void *) packet, len);
  169. if (len < 64) {
  170. memset((void *) (dp->addr + len), 0, 64-len);
  171. len = 64;
  172. }
  173. dp->ctrl = len | 0xe0000000;
  174. dp->owner = 0x80000000;
  175. ks8695_write(KS8695_LAN_DMA_TX, 0x3);
  176. ks8695_write(KS8695_LAN_DMA_TX_START, 0x1);
  177. if (++next >= TXDESCS)
  178. next = 0;
  179. return len;
  180. }