cs8900.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. #undef DEBUG
  43. /* packet page register access functions */
  44. #ifdef CS8900_BUS32
  45. /* we don't need 16 bit initialisation on 32 bit bus */
  46. #define get_reg_init_bus(x) get_reg((x))
  47. #else
  48. static unsigned short get_reg_init_bus (int regno)
  49. {
  50. /* force 16 bit busmode */
  51. volatile unsigned char c;
  52. c = CS8900_BUS16_0;
  53. c = CS8900_BUS16_1;
  54. c = CS8900_BUS16_0;
  55. c = CS8900_BUS16_1;
  56. c = CS8900_BUS16_0;
  57. CS8900_PPTR = regno;
  58. return CS8900_PDATA;
  59. }
  60. #endif
  61. static unsigned short get_reg (int regno)
  62. {
  63. CS8900_PPTR = regno;
  64. return CS8900_PDATA;
  65. }
  66. static void put_reg (int regno, unsigned short val)
  67. {
  68. CS8900_PPTR = regno;
  69. CS8900_PDATA = val;
  70. }
  71. static void eth_reset (void)
  72. {
  73. int tmo;
  74. unsigned short us;
  75. /* reset NIC */
  76. put_reg (PP_SelfCTL, get_reg (PP_SelfCTL) | PP_SelfCTL_Reset);
  77. /* wait for 200ms */
  78. udelay (200000);
  79. /* Wait until the chip is reset */
  80. tmo = get_timer (0) + 1 * CFG_HZ;
  81. while ((((us = get_reg_init_bus (PP_SelfSTAT)) & PP_SelfSTAT_InitD) == 0)
  82. && tmo < get_timer (0))
  83. /*NOP*/;
  84. }
  85. static void eth_reginit (void)
  86. {
  87. /* receive only error free packets addressed to this card */
  88. put_reg (PP_RxCTL, PP_RxCTL_IA | PP_RxCTL_Broadcast | PP_RxCTL_RxOK);
  89. /* do not generate any interrupts on receive operations */
  90. put_reg (PP_RxCFG, 0);
  91. /* do not generate any interrupts on transmit operations */
  92. put_reg (PP_TxCFG, 0);
  93. /* do not generate any interrupts on buffer operations */
  94. put_reg (PP_BufCFG, 0);
  95. /* enable transmitter/receiver mode */
  96. put_reg (PP_LineCTL, PP_LineCTL_Rx | PP_LineCTL_Tx);
  97. }
  98. void cs8900_get_enetaddr (uchar * addr)
  99. {
  100. int i;
  101. unsigned char env_enetaddr[6];
  102. char *tmp = getenv ("ethaddr");
  103. char *end;
  104. for (i=0; i<6; i++) {
  105. env_enetaddr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
  106. if (tmp)
  107. tmp = (*end) ? end+1 : end;
  108. }
  109. /* verify chip id */
  110. if (get_reg_init_bus (PP_ChipID) != 0x630e)
  111. return;
  112. eth_reset ();
  113. if ((get_reg (PP_SelfSTAT) & (PP_SelfSTAT_EEPROM | PP_SelfSTAT_EEPROM_OK)) ==
  114. (PP_SelfSTAT_EEPROM | PP_SelfSTAT_EEPROM_OK)) {
  115. /* Load the MAC from EEPROM */
  116. for (i = 0; i < 6 / 2; i++) {
  117. unsigned int Addr;
  118. Addr = get_reg (PP_IA + i * 2);
  119. addr[i * 2] = Addr & 0xFF;
  120. addr[i * 2 + 1] = Addr >> 8;
  121. }
  122. if (memcmp(env_enetaddr, "\0\0\0\0\0\0", 6) != 0 &&
  123. memcmp(env_enetaddr, addr, 6) != 0) {
  124. printf ("\nWarning: MAC addresses don't match:\n");
  125. printf ("\tHW MAC address: "
  126. "%02X:%02X:%02X:%02X:%02X:%02X\n",
  127. addr[0], addr[1],
  128. addr[2], addr[3],
  129. addr[4], addr[5] );
  130. printf ("\t\"ethaddr\" value: "
  131. "%02X:%02X:%02X:%02X:%02X:%02X\n",
  132. env_enetaddr[0], env_enetaddr[1],
  133. env_enetaddr[2], env_enetaddr[3],
  134. env_enetaddr[4], env_enetaddr[5]) ;
  135. debug ("### Set MAC addr from environment\n");
  136. memcpy (addr, env_enetaddr, 6);
  137. }
  138. if (!tmp) {
  139. char ethaddr[20];
  140. sprintf (ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
  141. addr[0], addr[1],
  142. addr[2], addr[3],
  143. addr[4], addr[5]) ;
  144. debug ("### Set environment from HW MAC addr = \"%s\"\n", ethaddr);
  145. setenv ("ethaddr", ethaddr);
  146. }
  147. }
  148. }
  149. void eth_halt (void)
  150. {
  151. /* disable transmitter/receiver mode */
  152. put_reg (PP_LineCTL, 0);
  153. /* "shutdown" to show ChipID or kernel wouldn't find he cs8900 ... */
  154. get_reg_init_bus (PP_ChipID);
  155. }
  156. int eth_init (bd_t * bd)
  157. {
  158. /* verify chip id */
  159. if (get_reg_init_bus (PP_ChipID) != 0x630e) {
  160. printf ("CS8900 Ethernet chip not found?!\n");
  161. return 0;
  162. }
  163. eth_reset ();
  164. /* set the ethernet address */
  165. put_reg (PP_IA + 0, bd->bi_enetaddr[0] | (bd->bi_enetaddr[1] << 8));
  166. put_reg (PP_IA + 2, bd->bi_enetaddr[2] | (bd->bi_enetaddr[3] << 8));
  167. put_reg (PP_IA + 4, bd->bi_enetaddr[4] | (bd->bi_enetaddr[5] << 8));
  168. eth_reginit ();
  169. return 0;
  170. }
  171. /* Get a data block via Ethernet */
  172. int eth_rx (void)
  173. {
  174. int i;
  175. unsigned short rxlen;
  176. unsigned short *addr;
  177. unsigned short status;
  178. status = get_reg (PP_RER);
  179. if ((status & PP_RER_RxOK) == 0)
  180. return 0;
  181. status = CS8900_RTDATA; /* stat */
  182. rxlen = CS8900_RTDATA; /* len */
  183. #ifdef DEBUG
  184. if (rxlen > PKTSIZE_ALIGN + PKTALIGN)
  185. printf ("packet too big!\n");
  186. #endif
  187. for (addr = (unsigned short *) NetRxPackets[0], i = rxlen >> 1; i > 0;
  188. i--)
  189. *addr++ = CS8900_RTDATA;
  190. if (rxlen & 1)
  191. *addr++ = CS8900_RTDATA;
  192. /* Pass the packet up to the protocol layers. */
  193. NetReceive (NetRxPackets[0], rxlen);
  194. return rxlen;
  195. }
  196. /* Send a data block via Ethernet. */
  197. int eth_send (volatile void *packet, int length)
  198. {
  199. volatile unsigned short *addr;
  200. int tmo;
  201. unsigned short s;
  202. retry:
  203. /* initiate a transmit sequence */
  204. CS8900_TxCMD = PP_TxCmd_TxStart_Full;
  205. CS8900_TxLEN = length;
  206. /* Test to see if the chip has allocated memory for the packet */
  207. if ((get_reg (PP_BusSTAT) & PP_BusSTAT_TxRDY) == 0) {
  208. /* Oops... this should not happen! */
  209. #ifdef DEBUG
  210. printf ("cs: unable to send packet; retrying...\n");
  211. #endif
  212. for (tmo = get_timer (0) + 5 * CFG_HZ; get_timer (0) < tmo;)
  213. /*NOP*/;
  214. eth_reset ();
  215. eth_reginit ();
  216. goto retry;
  217. }
  218. /* Write the contents of the packet */
  219. /* assume even number of bytes */
  220. for (addr = packet; length > 0; length -= 2)
  221. CS8900_RTDATA = *addr++;
  222. /* wait for transfer to succeed */
  223. tmo = get_timer (0) + 5 * CFG_HZ;
  224. while ((s = get_reg (PP_TER) & ~0x1F) == 0) {
  225. if (get_timer (0) >= tmo)
  226. break;
  227. }
  228. /* nothing */ ;
  229. if ((s & (PP_TER_CRS | PP_TER_TxOK)) != PP_TER_TxOK) {
  230. #ifdef DEBUG
  231. printf ("\ntransmission error %#x\n", s);
  232. #endif
  233. }
  234. return 0;
  235. }
  236. static void cs8900_e2prom_ready(void)
  237. {
  238. while (get_reg(PP_SelfSTAT) & SI_BUSY)
  239. ;
  240. }
  241. /***********************************************************/
  242. /* read a 16-bit word out of the EEPROM */
  243. /***********************************************************/
  244. int cs8900_e2prom_read(unsigned char addr, unsigned short *value)
  245. {
  246. cs8900_e2prom_ready();
  247. put_reg(PP_EECMD, EEPROM_READ_CMD | addr);
  248. cs8900_e2prom_ready();
  249. *value = get_reg(PP_EEData);
  250. return 0;
  251. }
  252. /***********************************************************/
  253. /* write a 16-bit word into the EEPROM */
  254. /***********************************************************/
  255. int cs8900_e2prom_write(unsigned char addr, unsigned short value)
  256. {
  257. cs8900_e2prom_ready();
  258. put_reg(PP_EECMD, EEPROM_WRITE_EN);
  259. cs8900_e2prom_ready();
  260. put_reg(PP_EEData, value);
  261. put_reg(PP_EECMD, EEPROM_WRITE_CMD | addr);
  262. cs8900_e2prom_ready();
  263. put_reg(PP_EECMD, EEPROM_WRITE_DIS);
  264. cs8900_e2prom_ready();
  265. return 0;
  266. }