cs8900.c 8.0 KB

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