cs8900.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 * CONFIG_SYS_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 (void)
  99. {
  100. int i;
  101. uchar enetaddr[6];
  102. /* if the env is setup, then bail */
  103. if (eth_getenv_enetaddr("ethaddr", enetaddr))
  104. return;
  105. /* verify chip id */
  106. if (get_reg_init_bus (PP_ChipID) != 0x630e)
  107. return;
  108. eth_reset ();
  109. if ((get_reg (PP_SelfSTAT) & (PP_SelfSTAT_EEPROM | PP_SelfSTAT_EEPROM_OK)) ==
  110. (PP_SelfSTAT_EEPROM | PP_SelfSTAT_EEPROM_OK)) {
  111. /* Load the MAC from EEPROM */
  112. for (i = 0; i < 6 / 2; i++) {
  113. unsigned int Addr;
  114. Addr = get_reg (PP_IA + i * 2);
  115. enetaddr[i * 2] = Addr & 0xFF;
  116. enetaddr[i * 2 + 1] = Addr >> 8;
  117. }
  118. eth_setenv_enetaddr("ethaddr", enetaddr);
  119. debug("### Set environment from HW MAC addr = \"%pM\"\n", enetaddr);
  120. }
  121. }
  122. void eth_halt (void)
  123. {
  124. /* disable transmitter/receiver mode */
  125. put_reg (PP_LineCTL, 0);
  126. /* "shutdown" to show ChipID or kernel wouldn't find he cs8900 ... */
  127. get_reg_init_bus (PP_ChipID);
  128. }
  129. int eth_init (bd_t * bd)
  130. {
  131. uchar enetaddr[6];
  132. /* verify chip id */
  133. if (get_reg_init_bus (PP_ChipID) != 0x630e) {
  134. printf ("CS8900 Ethernet chip not found?!\n");
  135. return 0;
  136. }
  137. eth_reset ();
  138. /* set the ethernet address */
  139. eth_getenv_enetaddr("ethaddr", enetaddr);
  140. put_reg (PP_IA + 0, enetaddr[0] | (enetaddr[1] << 8));
  141. put_reg (PP_IA + 2, enetaddr[2] | (enetaddr[3] << 8));
  142. put_reg (PP_IA + 4, enetaddr[4] | (enetaddr[5] << 8));
  143. eth_reginit ();
  144. return 0;
  145. }
  146. /* Get a data block via Ethernet */
  147. int eth_rx (void)
  148. {
  149. int i;
  150. unsigned short rxlen;
  151. unsigned short *addr;
  152. unsigned short status;
  153. status = get_reg (PP_RER);
  154. if ((status & PP_RER_RxOK) == 0)
  155. return 0;
  156. status = CS8900_RTDATA; /* stat */
  157. rxlen = CS8900_RTDATA; /* len */
  158. #ifdef DEBUG
  159. if (rxlen > PKTSIZE_ALIGN + PKTALIGN)
  160. printf ("packet too big!\n");
  161. #endif
  162. for (addr = (unsigned short *) NetRxPackets[0], i = rxlen >> 1; i > 0;
  163. i--)
  164. *addr++ = CS8900_RTDATA;
  165. if (rxlen & 1)
  166. *addr++ = CS8900_RTDATA;
  167. /* Pass the packet up to the protocol layers. */
  168. NetReceive (NetRxPackets[0], rxlen);
  169. return rxlen;
  170. }
  171. /* Send a data block via Ethernet. */
  172. int eth_send (volatile void *packet, int length)
  173. {
  174. volatile unsigned short *addr;
  175. int tmo;
  176. unsigned short s;
  177. retry:
  178. /* initiate a transmit sequence */
  179. CS8900_TxCMD = PP_TxCmd_TxStart_Full;
  180. CS8900_TxLEN = length;
  181. /* Test to see if the chip has allocated memory for the packet */
  182. if ((get_reg (PP_BusSTAT) & PP_BusSTAT_TxRDY) == 0) {
  183. /* Oops... this should not happen! */
  184. #ifdef DEBUG
  185. printf ("cs: unable to send packet; retrying...\n");
  186. #endif
  187. for (tmo = get_timer (0) + 5 * CONFIG_SYS_HZ; get_timer (0) < tmo;)
  188. /*NOP*/;
  189. eth_reset ();
  190. eth_reginit ();
  191. goto retry;
  192. }
  193. /* Write the contents of the packet */
  194. /* assume even number of bytes */
  195. for (addr = packet; length > 0; length -= 2)
  196. CS8900_RTDATA = *addr++;
  197. /* wait for transfer to succeed */
  198. tmo = get_timer (0) + 5 * CONFIG_SYS_HZ;
  199. while ((s = get_reg (PP_TER) & ~0x1F) == 0) {
  200. if (get_timer (0) >= tmo)
  201. break;
  202. }
  203. /* nothing */ ;
  204. if ((s & (PP_TER_CRS | PP_TER_TxOK)) != PP_TER_TxOK) {
  205. #ifdef DEBUG
  206. printf ("\ntransmission error %#x\n", s);
  207. #endif
  208. }
  209. return 0;
  210. }
  211. static void cs8900_e2prom_ready(void)
  212. {
  213. while (get_reg(PP_SelfSTAT) & SI_BUSY)
  214. ;
  215. }
  216. /***********************************************************/
  217. /* read a 16-bit word out of the EEPROM */
  218. /***********************************************************/
  219. int cs8900_e2prom_read(unsigned char addr, unsigned short *value)
  220. {
  221. cs8900_e2prom_ready();
  222. put_reg(PP_EECMD, EEPROM_READ_CMD | addr);
  223. cs8900_e2prom_ready();
  224. *value = get_reg(PP_EEData);
  225. return 0;
  226. }
  227. /***********************************************************/
  228. /* write a 16-bit word into the EEPROM */
  229. /***********************************************************/
  230. int cs8900_e2prom_write(unsigned char addr, unsigned short value)
  231. {
  232. cs8900_e2prom_ready();
  233. put_reg(PP_EECMD, EEPROM_WRITE_EN);
  234. cs8900_e2prom_ready();
  235. put_reg(PP_EEData, value);
  236. put_reg(PP_EECMD, EEPROM_WRITE_CMD | addr);
  237. cs8900_e2prom_ready();
  238. put_reg(PP_EECMD, EEPROM_WRITE_DIS);
  239. cs8900_e2prom_ready();
  240. return 0;
  241. }