cs8900.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Cirrus Logic CS8900A Ethernet
  3. *
  4. * (C) 2003 Wolfgang Denk, wd@denx.de
  5. * Extension to synchronize ethaddr environment variable
  6. * against value in EEPROM
  7. *
  8. * (C) Copyright 2002
  9. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  10. * Marius Groeger <mgroeger@sysgo.de>
  11. *
  12. * Copyright (C) 1999 Ben Williamson <benw@pobox.com>
  13. *
  14. * See file CREDITS for list of people who contributed to this
  15. * project.
  16. *
  17. * This program is loaded into SRAM in bootstrap mode, where it waits
  18. * for commands on UART1 to read and write memory, jump to code etc.
  19. * A design goal for this program is to be entirely independent of the
  20. * target board. Anything with a CL-PS7111 or EP7211 should be able to run
  21. * this code in bootstrap mode. All the board specifics can be handled on
  22. * the host.
  23. *
  24. * This program is free software; you can redistribute it and/or modify
  25. * it under the terms of the GNU General Public License as published by
  26. * the Free Software Foundation; either version 2 of the License, or
  27. * (at your option) any later version.
  28. *
  29. * This program is distributed in the hope that it will be useful,
  30. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32. * GNU General Public License for more details.
  33. *
  34. * You should have received a copy of the GNU General Public License
  35. * along with this program; if not, write to the Free Software
  36. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  37. */
  38. #include <common.h>
  39. #include <command.h>
  40. #include "cs8900.h"
  41. #include <net.h>
  42. #ifdef CONFIG_DRIVER_CS8900
  43. #if (CONFIG_COMMANDS & CFG_CMD_NET)
  44. /* packet page register access functions */
  45. #ifdef CS8900_BUS32
  46. /* we don't need 16 bit initialisation on 32 bit bus */
  47. #define get_reg_init_bus(x) get_reg((x))
  48. #else
  49. static unsigned short get_reg_init_bus (int regno)
  50. {
  51. /* force 16 bit busmode */
  52. volatile unsigned char c;
  53. c = CS8900_BUS16_0;
  54. c = CS8900_BUS16_1;
  55. c = CS8900_BUS16_0;
  56. c = CS8900_BUS16_1;
  57. c = CS8900_BUS16_0;
  58. CS8900_PPTR = regno;
  59. return (unsigned short) CS8900_PDATA;
  60. }
  61. #endif
  62. static unsigned short get_reg (int regno)
  63. {
  64. CS8900_PPTR = regno;
  65. return (unsigned short) CS8900_PDATA;
  66. }
  67. static void put_reg (int regno, unsigned short val)
  68. {
  69. CS8900_PPTR = regno;
  70. CS8900_PDATA = val;
  71. }
  72. static void eth_reset (void)
  73. {
  74. int tmo;
  75. unsigned short us;
  76. /* reset NIC */
  77. put_reg (PP_SelfCTL, get_reg (PP_SelfCTL) | PP_SelfCTL_Reset);
  78. /* wait for 200ms */
  79. udelay (200000);
  80. /* Wait until the chip is reset */
  81. tmo = get_timer (0) + 1 * CFG_HZ;
  82. while ((((us = get_reg_init_bus (PP_SelfSTAT)) & PP_SelfSTAT_InitD) == 0)
  83. && tmo < get_timer (0))
  84. /*NOP*/;
  85. }
  86. void cs8900_get_enetaddr (uchar * addr)
  87. {
  88. int i;
  89. unsigned char env_enetaddr[6];
  90. char *tmp = getenv ("ethaddr");
  91. char *end;
  92. for (i=0; i<6; i++) {
  93. env_enetaddr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
  94. if (tmp)
  95. tmp = (*end) ? end+1 : end;
  96. }
  97. /* verify chip id */
  98. if (get_reg_init_bus (PP_ChipID) != 0x630e)
  99. return;
  100. eth_reset ();
  101. if ((get_reg (PP_SelfST) & (PP_SelfSTAT_EEPROM | PP_SelfSTAT_EEPROM_OK)) ==
  102. (PP_SelfSTAT_EEPROM | PP_SelfSTAT_EEPROM_OK)) {
  103. /* Load the MAC from EEPROM */
  104. for (i = 0; i < 6 / 2; i++) {
  105. unsigned int Addr;
  106. Addr = get_reg (PP_IA + i * 2);
  107. addr[i * 2] = Addr & 0xFF;
  108. addr[i * 2 + 1] = Addr >> 8;
  109. }
  110. if (memcmp(env_enetaddr, "\0\0\0\0\0\0", 6) != 0 &&
  111. memcmp(env_enetaddr, addr, 6) != 0) {
  112. printf ("\nWarning: MAC addresses don't match:\n");
  113. printf ("\tHW MAC address: "
  114. "%02X:%02X:%02X:%02X:%02X:%02X\n",
  115. addr[0], addr[1],
  116. addr[2], addr[3],
  117. addr[4], addr[5] );
  118. printf ("\t\"ethaddr\" value: "
  119. "%02X:%02X:%02X:%02X:%02X:%02X\n",
  120. env_enetaddr[0], env_enetaddr[1],
  121. env_enetaddr[2], env_enetaddr[3],
  122. env_enetaddr[4], env_enetaddr[5]) ;
  123. debug ("### Set MAC addr from environment\n");
  124. memcpy (addr, env_enetaddr, 6);
  125. }
  126. if (!tmp) {
  127. char ethaddr[20];
  128. sprintf (ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
  129. addr[0], addr[1],
  130. addr[2], addr[3],
  131. addr[4], addr[5]) ;
  132. debug ("### Set environment from HW MAC addr = \"%s\"\n", ethaddr);
  133. setenv ("ethaddr", ethaddr);
  134. }
  135. }
  136. }
  137. void eth_halt (void)
  138. {
  139. /* disable transmitter/receiver mode */
  140. put_reg (PP_LineCTL, 0);
  141. /* "shutdown" to show ChipID or kernel wouldn't find he cs8900 ... */
  142. get_reg_init_bus (PP_ChipID);
  143. }
  144. int eth_init (bd_t * bd)
  145. {
  146. /* verify chip id */
  147. if (get_reg_init_bus (PP_ChipID) != 0x630e) {
  148. printf ("CS8900 Ethernet chip not found?!\n");
  149. return 0;
  150. }
  151. eth_reset ();
  152. /* set the ethernet address */
  153. put_reg (PP_IA + 0, bd->bi_enetaddr[0] | (bd->bi_enetaddr[1] << 8));
  154. put_reg (PP_IA + 2, bd->bi_enetaddr[2] | (bd->bi_enetaddr[3] << 8));
  155. put_reg (PP_IA + 4, bd->bi_enetaddr[4] | (bd->bi_enetaddr[5] << 8));
  156. /* receive only error free packets addressed to this card */
  157. put_reg (PP_RxCTL, PP_RxCTL_IA | PP_RxCTL_Broadcast | PP_RxCTL_RxOK);
  158. /* do not generate any interrupts on receive operations */
  159. put_reg (PP_RxCFG, 0);
  160. /* do not generate any interrupts on transmit operations */
  161. put_reg (PP_TxCFG, 0);
  162. /* do not generate any interrupts on buffer operations */
  163. put_reg (PP_BufCFG, 0);
  164. /* enable transmitter/receiver mode */
  165. put_reg (PP_LineCTL, PP_LineCTL_Rx | PP_LineCTL_Tx);
  166. return 0;
  167. }
  168. /* Get a data block via Ethernet */
  169. extern int eth_rx (void)
  170. {
  171. int i;
  172. unsigned short rxlen;
  173. unsigned short *addr;
  174. unsigned short status;
  175. status = get_reg (PP_RER);
  176. if ((status & PP_RER_RxOK) == 0)
  177. return 0;
  178. status = CS8900_RTDATA; /* stat */
  179. rxlen = CS8900_RTDATA; /* len */
  180. if (rxlen > PKTSIZE_ALIGN + PKTALIGN)
  181. printf ("packet too big!\n");
  182. for (addr = (unsigned short *) NetRxPackets[0], i = rxlen >> 1; i > 0;
  183. i--)
  184. *addr++ = CS8900_RTDATA;
  185. if (rxlen & 1)
  186. *addr++ = CS8900_RTDATA;
  187. /* Pass the packet up to the protocol layers. */
  188. NetReceive (NetRxPackets[0], rxlen);
  189. return rxlen;
  190. }
  191. /* Send a data block via Ethernet. */
  192. extern int eth_send (volatile void *packet, int length)
  193. {
  194. volatile unsigned short *addr;
  195. int tmo;
  196. unsigned short s;
  197. retry:
  198. /* initiate a transmit sequence */
  199. CS8900_TxCMD = PP_TxCmd_TxStart_Full;
  200. CS8900_TxLEN = length;
  201. /* Test to see if the chip has allocated memory for the packet */
  202. if ((get_reg (PP_BusSTAT) & PP_BusSTAT_TxRDY) == 0) {
  203. /* Oops... this should not happen! */
  204. printf ("cs: unable to send packet; retrying...\n");
  205. for (tmo = get_timer (0) + 5 * CFG_HZ; get_timer (0) < tmo;)
  206. /*NOP*/;
  207. eth_reset ();
  208. goto retry;
  209. }
  210. /* Write the contents of the packet */
  211. /* assume even number of bytes */
  212. for (addr = packet; length > 0; length -= 2)
  213. CS8900_RTDATA = *addr++;
  214. /* wait for transfer to succeed */
  215. tmo = get_timer (0) + 5 * CFG_HZ;
  216. while ((s = get_reg (PP_TER) & ~0x1F) == 0) {
  217. if (get_timer (0) >= tmo)
  218. break;
  219. }
  220. /* nothing */ ;
  221. if ((s & (PP_TER_CRS | PP_TER_TxOK)) != PP_TER_TxOK) {
  222. printf ("\ntransmission error %#x\n", s);
  223. }
  224. return 0;
  225. }
  226. #endif /* COMMANDS & CFG_NET */
  227. #endif /* CONFIG_DRIVER_CS8900 */