rtl8019.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Realtek 8019AS Ethernet
  3. * (C) Copyright 2002-2003
  4. * Xue Ligong(lgxue@hotmail.com),Wang Kehao, ESLAB, whut.edu.cn
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. /*
  25. * This code works in 8bit mode.
  26. * If you need to work in 16bit mode, PLS change it!
  27. */
  28. #include <common.h>
  29. #include <command.h>
  30. #include "rtl8019.h"
  31. #include <net.h>
  32. #ifdef CONFIG_DRIVER_RTL8019
  33. #if defined(CONFIG_CMD_NET)
  34. /* packet page register access functions */
  35. static unsigned char get_reg (unsigned int regno)
  36. {
  37. return (*(unsigned char *) regno);
  38. }
  39. static void put_reg (unsigned int regno, unsigned char val)
  40. {
  41. *(volatile unsigned char *) regno = val;
  42. }
  43. static void eth_reset (void)
  44. {
  45. unsigned char ucTemp;
  46. /* reset NIC */
  47. ucTemp = get_reg (RTL8019_RESET);
  48. put_reg (RTL8019_RESET, ucTemp);
  49. put_reg (RTL8019_INTERRUPTSTATUS, 0xff);
  50. udelay (2000); /* wait for 2ms */
  51. }
  52. void rtl8019_get_enetaddr (uchar * addr)
  53. {
  54. unsigned char i;
  55. unsigned char temp;
  56. eth_reset ();
  57. put_reg (RTL8019_COMMAND, RTL8019_REMOTEDMARD);
  58. put_reg (RTL8019_DATACONFIGURATION, 0x48);
  59. put_reg (RTL8019_REMOTESTARTADDRESS0, 0x00);
  60. put_reg (RTL8019_REMOTESTARTADDRESS1, 0x00);
  61. put_reg (RTL8019_REMOTEBYTECOUNT0, 12);
  62. put_reg (RTL8019_REMOTEBYTECOUNT1, 0x00);
  63. put_reg (RTL8019_COMMAND, RTL8019_REMOTEDMARD);
  64. printf ("MAC: ");
  65. for (i = 0; i < 6; i++) {
  66. temp = get_reg (RTL8019_DMA_DATA);
  67. *addr++ = temp;
  68. temp = get_reg (RTL8019_DMA_DATA);
  69. printf ("%x:", temp);
  70. }
  71. while ((!get_reg (RTL8019_INTERRUPTSTATUS) & 0x40));
  72. printf ("\b \n");
  73. put_reg (RTL8019_REMOTEBYTECOUNT0, 0x00);
  74. put_reg (RTL8019_REMOTEBYTECOUNT1, 0x00);
  75. put_reg (RTL8019_COMMAND, RTL8019_PAGE0);
  76. }
  77. void eth_halt (void)
  78. {
  79. put_reg (RTL8019_COMMAND, 0x01);
  80. }
  81. int eth_init (bd_t * bd)
  82. {
  83. eth_reset ();
  84. put_reg (RTL8019_COMMAND, RTL8019_PAGE0STOP);
  85. put_reg (RTL8019_DATACONFIGURATION, 0x48);
  86. put_reg (RTL8019_REMOTEBYTECOUNT0, 0x00);
  87. put_reg (RTL8019_REMOTEBYTECOUNT1, 0x00);
  88. put_reg (RTL8019_RECEIVECONFIGURATION, 0x00); /*00; */
  89. put_reg (RTL8019_TRANSMITPAGE, RTL8019_TPSTART);
  90. put_reg (RTL8019_TRANSMITCONFIGURATION, 0x02);
  91. put_reg (RTL8019_PAGESTART, RTL8019_PSTART);
  92. put_reg (RTL8019_BOUNDARY, RTL8019_PSTART);
  93. put_reg (RTL8019_PAGESTOP, RTL8019_PSTOP);
  94. put_reg (RTL8019_INTERRUPTSTATUS, 0xff);
  95. put_reg (RTL8019_INTERRUPTMASK, 0x11); /*b; */
  96. put_reg (RTL8019_COMMAND, RTL8019_PAGE1STOP);
  97. put_reg (RTL8019_PHYSICALADDRESS0, bd->bi_enetaddr[0]);
  98. put_reg (RTL8019_PHYSICALADDRESS1, bd->bi_enetaddr[1]);
  99. put_reg (RTL8019_PHYSICALADDRESS2, bd->bi_enetaddr[2]);
  100. put_reg (RTL8019_PHYSICALADDRESS3, bd->bi_enetaddr[3]);
  101. put_reg (RTL8019_PHYSICALADDRESS4, bd->bi_enetaddr[4]);
  102. put_reg (RTL8019_PHYSICALADDRESS5, bd->bi_enetaddr[5]);
  103. put_reg (RTL8019_MULTIADDRESS0, 0x00);
  104. put_reg (RTL8019_MULTIADDRESS1, 0x00);
  105. put_reg (RTL8019_MULTIADDRESS2, 0x00);
  106. put_reg (RTL8019_MULTIADDRESS3, 0x00);
  107. put_reg (RTL8019_MULTIADDRESS4, 0x00);
  108. put_reg (RTL8019_MULTIADDRESS5, 0x00);
  109. put_reg (RTL8019_MULTIADDRESS6, 0x00);
  110. put_reg (RTL8019_MULTIADDRESS7, 0x00);
  111. put_reg (RTL8019_CURRENT, RTL8019_PSTART);
  112. put_reg (RTL8019_COMMAND, RTL8019_PAGE0);
  113. put_reg (RTL8019_TRANSMITCONFIGURATION, 0xe0); /*58; */
  114. return 0;
  115. }
  116. static unsigned char nic_to_pc (void)
  117. {
  118. unsigned char rec_head_status;
  119. unsigned char next_packet_pointer;
  120. unsigned char packet_length0;
  121. unsigned char packet_length1;
  122. unsigned short rxlen = 0;
  123. unsigned int i = 4;
  124. unsigned char current_point;
  125. unsigned char *addr;
  126. /*
  127. * The RTL8019's first 4B is packet status,page of next packet
  128. * and packet length(2B).So we receive the fist 4B.
  129. */
  130. put_reg (RTL8019_REMOTESTARTADDRESS1, get_reg (RTL8019_BOUNDARY));
  131. put_reg (RTL8019_REMOTESTARTADDRESS0, 0x00);
  132. put_reg (RTL8019_REMOTEBYTECOUNT1, 0x00);
  133. put_reg (RTL8019_REMOTEBYTECOUNT0, 0x04);
  134. put_reg (RTL8019_COMMAND, RTL8019_REMOTEDMARD);
  135. rec_head_status = get_reg (RTL8019_DMA_DATA);
  136. next_packet_pointer = get_reg (RTL8019_DMA_DATA);
  137. packet_length0 = get_reg (RTL8019_DMA_DATA);
  138. packet_length1 = get_reg (RTL8019_DMA_DATA);
  139. put_reg (RTL8019_COMMAND, RTL8019_PAGE0);
  140. /*Packet length is in two 8bit registers */
  141. rxlen = packet_length1;
  142. rxlen = (((rxlen << 8) & 0xff00) + packet_length0);
  143. rxlen -= 4;
  144. if (rxlen > PKTSIZE_ALIGN + PKTALIGN)
  145. printf ("packet too big!\n");
  146. /*Receive the packet */
  147. put_reg (RTL8019_REMOTESTARTADDRESS0, 0x04);
  148. put_reg (RTL8019_REMOTESTARTADDRESS1, get_reg (RTL8019_BOUNDARY));
  149. put_reg (RTL8019_REMOTEBYTECOUNT0, (rxlen & 0xff));
  150. put_reg (RTL8019_REMOTEBYTECOUNT1, ((rxlen >> 8) & 0xff));
  151. put_reg (RTL8019_COMMAND, RTL8019_REMOTEDMARD);
  152. for (addr = (unsigned char *) NetRxPackets[0], i = rxlen; i > 0; i--)
  153. *addr++ = get_reg (RTL8019_DMA_DATA);
  154. /* Pass the packet up to the protocol layers. */
  155. NetReceive (NetRxPackets[0], rxlen);
  156. while (!(get_reg (RTL8019_INTERRUPTSTATUS)) & 0x40); /* wait for the op. */
  157. /*
  158. * To test whether the packets are all received,get the
  159. * location of current point
  160. */
  161. put_reg (RTL8019_COMMAND, RTL8019_PAGE1);
  162. current_point = get_reg (RTL8019_CURRENT);
  163. put_reg (RTL8019_COMMAND, RTL8019_PAGE0);
  164. put_reg (RTL8019_BOUNDARY, next_packet_pointer);
  165. return current_point;
  166. }
  167. /* Get a data block via Ethernet */
  168. extern int eth_rx (void)
  169. {
  170. unsigned char temp, current_point;
  171. put_reg (RTL8019_COMMAND, RTL8019_PAGE0);
  172. while (1) {
  173. temp = get_reg (RTL8019_INTERRUPTSTATUS);
  174. if (temp & 0x90) {
  175. /*overflow */
  176. put_reg (RTL8019_COMMAND, RTL8019_PAGE0STOP);
  177. udelay (2000);
  178. put_reg (RTL8019_REMOTEBYTECOUNT0, 0);
  179. put_reg (RTL8019_REMOTEBYTECOUNT1, 0);
  180. put_reg (RTL8019_TRANSMITCONFIGURATION, 2);
  181. do {
  182. current_point = nic_to_pc ();
  183. } while (get_reg (RTL8019_BOUNDARY) != current_point);
  184. put_reg (RTL8019_TRANSMITCONFIGURATION, 0xe0);
  185. }
  186. if (temp & 0x1) {
  187. /*packet received */
  188. do {
  189. put_reg (RTL8019_INTERRUPTSTATUS, 0x01);
  190. current_point = nic_to_pc ();
  191. } while (get_reg (RTL8019_BOUNDARY) != current_point);
  192. }
  193. if (!(temp & 0x1))
  194. return 0;
  195. /* done and exit. */
  196. }
  197. }
  198. /* Send a data block via Ethernet. */
  199. extern int eth_send (volatile void *packet, int length)
  200. {
  201. volatile unsigned char *p;
  202. unsigned int pn;
  203. pn = length;
  204. p = (volatile unsigned char *) packet;
  205. while (get_reg (RTL8019_COMMAND) == RTL8019_TRANSMIT);
  206. put_reg (RTL8019_REMOTESTARTADDRESS0, 0);
  207. put_reg (RTL8019_REMOTESTARTADDRESS1, RTL8019_TPSTART);
  208. put_reg (RTL8019_REMOTEBYTECOUNT0, (pn & 0xff));
  209. put_reg (RTL8019_REMOTEBYTECOUNT1, ((pn >> 8) & 0xff));
  210. put_reg (RTL8019_COMMAND, RTL8019_REMOTEDMAWR);
  211. while (pn > 0) {
  212. put_reg (RTL8019_DMA_DATA, *p++);
  213. pn--;
  214. }
  215. pn = length;
  216. while (pn < 60) { /*Padding */
  217. put_reg (RTL8019_DMA_DATA, 0);
  218. pn++;
  219. }
  220. while (!(get_reg (RTL8019_INTERRUPTSTATUS)) & 0x40);
  221. put_reg (RTL8019_INTERRUPTSTATUS, 0x40);
  222. put_reg (RTL8019_TRANSMITPAGE, RTL8019_TPSTART);
  223. put_reg (RTL8019_TRANSMITBYTECOUNT0, (pn & 0xff));
  224. put_reg (RTL8019_TRANSMITBYTECOUNT1, ((pn >> 8 & 0xff)));
  225. put_reg (RTL8019_COMMAND, RTL8019_TRANSMIT);
  226. return 0;
  227. }
  228. #endif /* COMMANDS & CFG_NET */
  229. #endif /* CONFIG_DRIVER_RTL8019 */