ks8695eth.c 6.2 KB

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