rtl8019.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. /* packet page register access functions */
  33. static unsigned char get_reg (unsigned int regno)
  34. {
  35. return (*(unsigned char *) regno);
  36. }
  37. static void put_reg (unsigned int regno, unsigned char val)
  38. {
  39. *(volatile unsigned char *) regno = val;
  40. }
  41. static void eth_reset (void)
  42. {
  43. unsigned char ucTemp;
  44. /* reset NIC */
  45. ucTemp = get_reg (RTL8019_RESET);
  46. put_reg (RTL8019_RESET, ucTemp);
  47. put_reg (RTL8019_INTERRUPTSTATUS, 0xff);
  48. udelay (2000); /* wait for 2ms */
  49. }
  50. void rtl8019_get_enetaddr (uchar * addr)
  51. {
  52. unsigned char i;
  53. unsigned char temp;
  54. eth_reset ();
  55. put_reg (RTL8019_COMMAND, RTL8019_REMOTEDMARD);
  56. put_reg (RTL8019_DATACONFIGURATION, 0x48);
  57. put_reg (RTL8019_REMOTESTARTADDRESS0, 0x00);
  58. put_reg (RTL8019_REMOTESTARTADDRESS1, 0x00);
  59. put_reg (RTL8019_REMOTEBYTECOUNT0, 12);
  60. put_reg (RTL8019_REMOTEBYTECOUNT1, 0x00);
  61. put_reg (RTL8019_COMMAND, RTL8019_REMOTEDMARD);
  62. printf ("MAC: ");
  63. for (i = 0; i < 6; i++) {
  64. temp = get_reg (RTL8019_DMA_DATA);
  65. *addr++ = temp;
  66. temp = get_reg (RTL8019_DMA_DATA);
  67. printf ("%x:", temp);
  68. }
  69. while ((!get_reg (RTL8019_INTERRUPTSTATUS) & 0x40));
  70. printf ("\b \n");
  71. put_reg (RTL8019_REMOTEBYTECOUNT0, 0x00);
  72. put_reg (RTL8019_REMOTEBYTECOUNT1, 0x00);
  73. put_reg (RTL8019_COMMAND, RTL8019_PAGE0);
  74. }
  75. void eth_halt (void)
  76. {
  77. put_reg (RTL8019_COMMAND, 0x01);
  78. }
  79. int eth_init (bd_t * bd)
  80. {
  81. eth_reset ();
  82. put_reg (RTL8019_COMMAND, RTL8019_PAGE0STOP);
  83. put_reg (RTL8019_DATACONFIGURATION, 0x48);
  84. put_reg (RTL8019_REMOTEBYTECOUNT0, 0x00);
  85. put_reg (RTL8019_REMOTEBYTECOUNT1, 0x00);
  86. put_reg (RTL8019_RECEIVECONFIGURATION, 0x00); /*00; */
  87. put_reg (RTL8019_TRANSMITPAGE, RTL8019_TPSTART);
  88. put_reg (RTL8019_TRANSMITCONFIGURATION, 0x02);
  89. put_reg (RTL8019_PAGESTART, RTL8019_PSTART);
  90. put_reg (RTL8019_BOUNDARY, RTL8019_PSTART);
  91. put_reg (RTL8019_PAGESTOP, RTL8019_PSTOP);
  92. put_reg (RTL8019_INTERRUPTSTATUS, 0xff);
  93. put_reg (RTL8019_INTERRUPTMASK, 0x11); /*b; */
  94. put_reg (RTL8019_COMMAND, RTL8019_PAGE1STOP);
  95. put_reg (RTL8019_PHYSICALADDRESS0, bd->bi_enetaddr[0]);
  96. put_reg (RTL8019_PHYSICALADDRESS1, bd->bi_enetaddr[1]);
  97. put_reg (RTL8019_PHYSICALADDRESS2, bd->bi_enetaddr[2]);
  98. put_reg (RTL8019_PHYSICALADDRESS3, bd->bi_enetaddr[3]);
  99. put_reg (RTL8019_PHYSICALADDRESS4, bd->bi_enetaddr[4]);
  100. put_reg (RTL8019_PHYSICALADDRESS5, bd->bi_enetaddr[5]);
  101. put_reg (RTL8019_MULTIADDRESS0, 0x00);
  102. put_reg (RTL8019_MULTIADDRESS1, 0x00);
  103. put_reg (RTL8019_MULTIADDRESS2, 0x00);
  104. put_reg (RTL8019_MULTIADDRESS3, 0x00);
  105. put_reg (RTL8019_MULTIADDRESS4, 0x00);
  106. put_reg (RTL8019_MULTIADDRESS5, 0x00);
  107. put_reg (RTL8019_MULTIADDRESS6, 0x00);
  108. put_reg (RTL8019_MULTIADDRESS7, 0x00);
  109. put_reg (RTL8019_CURRENT, RTL8019_PSTART);
  110. put_reg (RTL8019_COMMAND, RTL8019_PAGE0);
  111. put_reg (RTL8019_TRANSMITCONFIGURATION, 0xe0); /*58; */
  112. return 0;
  113. }
  114. static unsigned char nic_to_pc (void)
  115. {
  116. unsigned char rec_head_status;
  117. unsigned char next_packet_pointer;
  118. unsigned char packet_length0;
  119. unsigned char packet_length1;
  120. unsigned short rxlen = 0;
  121. unsigned int i = 4;
  122. unsigned char current_point;
  123. unsigned char *addr;
  124. /*
  125. * The RTL8019's first 4B is packet status,page of next packet
  126. * and packet length(2B).So we receive the fist 4B.
  127. */
  128. put_reg (RTL8019_REMOTESTARTADDRESS1, get_reg (RTL8019_BOUNDARY));
  129. put_reg (RTL8019_REMOTESTARTADDRESS0, 0x00);
  130. put_reg (RTL8019_REMOTEBYTECOUNT1, 0x00);
  131. put_reg (RTL8019_REMOTEBYTECOUNT0, 0x04);
  132. put_reg (RTL8019_COMMAND, RTL8019_REMOTEDMARD);
  133. rec_head_status = get_reg (RTL8019_DMA_DATA);
  134. next_packet_pointer = get_reg (RTL8019_DMA_DATA);
  135. packet_length0 = get_reg (RTL8019_DMA_DATA);
  136. packet_length1 = get_reg (RTL8019_DMA_DATA);
  137. put_reg (RTL8019_COMMAND, RTL8019_PAGE0);
  138. /*Packet length is in two 8bit registers */
  139. rxlen = packet_length1;
  140. rxlen = (((rxlen << 8) & 0xff00) + packet_length0);
  141. rxlen -= 4;
  142. if (rxlen > PKTSIZE_ALIGN + PKTALIGN)
  143. printf ("packet too big!\n");
  144. /*Receive the packet */
  145. put_reg (RTL8019_REMOTESTARTADDRESS0, 0x04);
  146. put_reg (RTL8019_REMOTESTARTADDRESS1, get_reg (RTL8019_BOUNDARY));
  147. put_reg (RTL8019_REMOTEBYTECOUNT0, (rxlen & 0xff));
  148. put_reg (RTL8019_REMOTEBYTECOUNT1, ((rxlen >> 8) & 0xff));
  149. put_reg (RTL8019_COMMAND, RTL8019_REMOTEDMARD);
  150. for (addr = (unsigned char *) NetRxPackets[0], i = rxlen; i > 0; i--)
  151. *addr++ = get_reg (RTL8019_DMA_DATA);
  152. /* Pass the packet up to the protocol layers. */
  153. NetReceive (NetRxPackets[0], rxlen);
  154. while (!(get_reg (RTL8019_INTERRUPTSTATUS)) & 0x40); /* wait for the op. */
  155. /*
  156. * To test whether the packets are all received,get the
  157. * location of current point
  158. */
  159. put_reg (RTL8019_COMMAND, RTL8019_PAGE1);
  160. current_point = get_reg (RTL8019_CURRENT);
  161. put_reg (RTL8019_COMMAND, RTL8019_PAGE0);
  162. put_reg (RTL8019_BOUNDARY, next_packet_pointer);
  163. return current_point;
  164. }
  165. /* Get a data block via Ethernet */
  166. extern int eth_rx (void)
  167. {
  168. unsigned char temp, current_point;
  169. put_reg (RTL8019_COMMAND, RTL8019_PAGE0);
  170. while (1) {
  171. temp = get_reg (RTL8019_INTERRUPTSTATUS);
  172. if (temp & 0x90) {
  173. /*overflow */
  174. put_reg (RTL8019_COMMAND, RTL8019_PAGE0STOP);
  175. udelay (2000);
  176. put_reg (RTL8019_REMOTEBYTECOUNT0, 0);
  177. put_reg (RTL8019_REMOTEBYTECOUNT1, 0);
  178. put_reg (RTL8019_TRANSMITCONFIGURATION, 2);
  179. do {
  180. current_point = nic_to_pc ();
  181. } while (get_reg (RTL8019_BOUNDARY) != current_point);
  182. put_reg (RTL8019_TRANSMITCONFIGURATION, 0xe0);
  183. }
  184. if (temp & 0x1) {
  185. /*packet received */
  186. do {
  187. put_reg (RTL8019_INTERRUPTSTATUS, 0x01);
  188. current_point = nic_to_pc ();
  189. } while (get_reg (RTL8019_BOUNDARY) != current_point);
  190. }
  191. if (!(temp & 0x1))
  192. return 0;
  193. /* done and exit. */
  194. }
  195. }
  196. /* Send a data block via Ethernet. */
  197. extern int eth_send (volatile void *packet, int length)
  198. {
  199. volatile unsigned char *p;
  200. unsigned int pn;
  201. pn = length;
  202. p = (volatile unsigned char *) packet;
  203. while (get_reg (RTL8019_COMMAND) == RTL8019_TRANSMIT);
  204. put_reg (RTL8019_REMOTESTARTADDRESS0, 0);
  205. put_reg (RTL8019_REMOTESTARTADDRESS1, RTL8019_TPSTART);
  206. put_reg (RTL8019_REMOTEBYTECOUNT0, (pn & 0xff));
  207. put_reg (RTL8019_REMOTEBYTECOUNT1, ((pn >> 8) & 0xff));
  208. put_reg (RTL8019_COMMAND, RTL8019_REMOTEDMAWR);
  209. while (pn > 0) {
  210. put_reg (RTL8019_DMA_DATA, *p++);
  211. pn--;
  212. }
  213. pn = length;
  214. while (pn < 60) { /*Padding */
  215. put_reg (RTL8019_DMA_DATA, 0);
  216. pn++;
  217. }
  218. while (!(get_reg (RTL8019_INTERRUPTSTATUS)) & 0x40);
  219. put_reg (RTL8019_INTERRUPTSTATUS, 0x40);
  220. put_reg (RTL8019_TRANSMITPAGE, RTL8019_TPSTART);
  221. put_reg (RTL8019_TRANSMITBYTECOUNT0, (pn & 0xff));
  222. put_reg (RTL8019_TRANSMITBYTECOUNT1, ((pn >> 8 & 0xff)));
  223. put_reg (RTL8019_COMMAND, RTL8019_TRANSMIT);
  224. return 0;
  225. }