ks8695eth.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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: ");
  126. for (i = 0; (i < 5); i++) {
  127. bd->bi_enetaddr[i] = eth_mac[i];
  128. printf("%02x:", eth_mac[i]);
  129. }
  130. bd->bi_enetaddr[i] = eth_mac[i];
  131. printf("%02x\n", eth_mac[i]);
  132. }
  133. /****************************************************************************/
  134. int eth_init(bd_t *bd)
  135. {
  136. debug ("%s(%d): eth_init()\n", __FILE__, __LINE__);
  137. eth_reset(bd);
  138. return 0;
  139. }
  140. /****************************************************************************/
  141. void eth_halt(void)
  142. {
  143. debug ("%s(%d): eth_halt()\n", __FILE__, __LINE__);
  144. /* Reset the ethernet engines */
  145. ks8695_write(KS8695_LAN_DMA_TX, 0x80000000);
  146. ks8695_write(KS8695_LAN_DMA_RX, 0x80000000);
  147. }
  148. /****************************************************************************/
  149. int eth_rx(void)
  150. {
  151. volatile struct ks8695_rxdesc *dp;
  152. int i, len = 0;
  153. debug ("%s(%d): eth_rx()\n", __FILE__, __LINE__);
  154. for (i = 0; (i < RXDESCS); i++) {
  155. dp= &ks8695_rx[i];
  156. if ((dp->status & 0x80000000) == 0) {
  157. len = (dp->status & 0x7ff) - 4;
  158. NetReceive((void *) dp->addr, len);
  159. dp->status = 0x80000000;
  160. ks8695_write(KS8695_LAN_DMA_RX_START, 0x1);
  161. break;
  162. }
  163. }
  164. return len;
  165. }
  166. /****************************************************************************/
  167. int eth_send(volatile void *packet, int len)
  168. {
  169. volatile struct ks8695_txdesc *dp;
  170. static int next = 0;
  171. debug ("%s(%d): eth_send(packet=%x,len=%d)\n", __FILE__, __LINE__,
  172. packet, len);
  173. dp = &ks8695_tx[next];
  174. memcpy((void *) dp->addr, packet, len);
  175. if (len < 64) {
  176. memset(dp->addr+len, 0, 64-len);
  177. len = 64;
  178. }
  179. dp->ctrl = len | 0xe0000000;
  180. dp->owner = 0x80000000;
  181. ks8695_write(KS8695_LAN_DMA_TX, 0x3);
  182. ks8695_write(KS8695_LAN_DMA_TX_START, 0x1);
  183. if (++next >= TXDESCS)
  184. next = 0;
  185. return len;
  186. }
  187. /****************************************************************************/