ks8695eth.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. static int ks8695_eth_init(struct eth_device *dev, 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. return 0;
  127. }
  128. /****************************************************************************/
  129. static void ks8695_eth_halt(struct eth_device *dev)
  130. {
  131. debug ("%s(%d): eth_halt()\n", __FILE__, __LINE__);
  132. /* Reset the ethernet engines */
  133. ks8695_write(KS8695_LAN_DMA_TX, 0x80000000);
  134. ks8695_write(KS8695_LAN_DMA_RX, 0x80000000);
  135. }
  136. /****************************************************************************/
  137. static int ks8695_eth_recv(struct eth_device *dev)
  138. {
  139. volatile struct ks8695_rxdesc *dp;
  140. int i, len = 0;
  141. debug ("%s(%d): eth_rx()\n", __FILE__, __LINE__);
  142. for (i = 0; (i < RXDESCS); i++) {
  143. dp= &ks8695_rx[i];
  144. if ((dp->status & 0x80000000) == 0) {
  145. len = (dp->status & 0x7ff) - 4;
  146. NetReceive((void *) dp->addr, len);
  147. dp->status = 0x80000000;
  148. ks8695_write(KS8695_LAN_DMA_RX_START, 0x1);
  149. break;
  150. }
  151. }
  152. return len;
  153. }
  154. /****************************************************************************/
  155. static int ks8695_eth_send(struct eth_device *dev, void *packet, int len)
  156. {
  157. volatile struct ks8695_txdesc *dp;
  158. static int next = 0;
  159. debug ("%s(%d): eth_send(packet=%p,len=%d)\n", __FILE__, __LINE__,
  160. packet, len);
  161. dp = &ks8695_tx[next];
  162. memcpy((void *) dp->addr, (void *) packet, len);
  163. if (len < 64) {
  164. memset((void *) (dp->addr + len), 0, 64-len);
  165. len = 64;
  166. }
  167. dp->ctrl = len | 0xe0000000;
  168. dp->owner = 0x80000000;
  169. ks8695_write(KS8695_LAN_DMA_TX, 0x3);
  170. ks8695_write(KS8695_LAN_DMA_TX_START, 0x1);
  171. if (++next >= TXDESCS)
  172. next = 0;
  173. return 0;
  174. }
  175. /****************************************************************************/
  176. int ks8695_eth_initialize(void)
  177. {
  178. struct eth_device *dev;
  179. dev = malloc(sizeof(*dev));
  180. if (dev == NULL)
  181. return -1;
  182. memset(dev, 0, sizeof(*dev));
  183. dev->iobase = KS8695_IO_BASE + KS8695_LAN_DMA_TX;
  184. dev->init = ks8695_eth_init;
  185. dev->halt = ks8695_eth_halt;
  186. dev->send = ks8695_eth_send;
  187. dev->recv = ks8695_eth_recv;
  188. strcpy(dev->name, "ks8695eth");
  189. eth_register(dev);
  190. return 0;
  191. }