cs8900.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * Cirrus Logic CS8900A Ethernet
  3. *
  4. * (C) 2009 Ben Warren , biggerbadderben@gmail.com
  5. * Converted to use CONFIG_NET_MULTI API
  6. *
  7. * (C) 2003 Wolfgang Denk, wd@denx.de
  8. * Extension to synchronize ethaddr environment variable
  9. * against value in EEPROM
  10. *
  11. * (C) Copyright 2002
  12. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  13. * Marius Groeger <mgroeger@sysgo.de>
  14. *
  15. * Copyright (C) 1999 Ben Williamson <benw@pobox.com>
  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. * SPDX-License-Identifier: GPL-2.0+
  25. */
  26. #include <common.h>
  27. #include <command.h>
  28. #include <asm/io.h>
  29. #include <net.h>
  30. #include <malloc.h>
  31. #include "cs8900.h"
  32. #undef DEBUG
  33. /* packet page register access functions */
  34. #ifdef CONFIG_CS8900_BUS32
  35. #define REG_WRITE(v, a) writel((v),(a))
  36. #define REG_READ(a) readl((a))
  37. /* we don't need 16 bit initialisation on 32 bit bus */
  38. #define get_reg_init_bus(r,d) get_reg((r),(d))
  39. #else
  40. #define REG_WRITE(v, a) writew((v),(a))
  41. #define REG_READ(a) readw((a))
  42. static u16 get_reg_init_bus(struct eth_device *dev, int regno)
  43. {
  44. /* force 16 bit busmode */
  45. struct cs8900_priv *priv = (struct cs8900_priv *)(dev->priv);
  46. uint8_t volatile * const iob = (uint8_t volatile * const)dev->iobase;
  47. readb(iob);
  48. readb(iob + 1);
  49. readb(iob);
  50. readb(iob + 1);
  51. readb(iob);
  52. REG_WRITE(regno, &priv->regs->pptr);
  53. return REG_READ(&priv->regs->pdata);
  54. }
  55. #endif
  56. static u16 get_reg(struct eth_device *dev, int regno)
  57. {
  58. struct cs8900_priv *priv = (struct cs8900_priv *)(dev->priv);
  59. REG_WRITE(regno, &priv->regs->pptr);
  60. return REG_READ(&priv->regs->pdata);
  61. }
  62. static void put_reg(struct eth_device *dev, int regno, u16 val)
  63. {
  64. struct cs8900_priv *priv = (struct cs8900_priv *)(dev->priv);
  65. REG_WRITE(regno, &priv->regs->pptr);
  66. REG_WRITE(val, &priv->regs->pdata);
  67. }
  68. static void cs8900_reset(struct eth_device *dev)
  69. {
  70. int tmo;
  71. u16 us;
  72. /* reset NIC */
  73. put_reg(dev, PP_SelfCTL, get_reg(dev, PP_SelfCTL) | PP_SelfCTL_Reset);
  74. /* wait for 200ms */
  75. udelay(200000);
  76. /* Wait until the chip is reset */
  77. tmo = get_timer(0) + 1 * CONFIG_SYS_HZ;
  78. while ((((us = get_reg_init_bus(dev, PP_SelfSTAT)) &
  79. PP_SelfSTAT_InitD) == 0) && tmo < get_timer(0))
  80. /*NOP*/;
  81. }
  82. static void cs8900_reginit(struct eth_device *dev)
  83. {
  84. /* receive only error free packets addressed to this card */
  85. put_reg(dev, PP_RxCTL,
  86. PP_RxCTL_IA | PP_RxCTL_Broadcast | PP_RxCTL_RxOK);
  87. /* do not generate any interrupts on receive operations */
  88. put_reg(dev, PP_RxCFG, 0);
  89. /* do not generate any interrupts on transmit operations */
  90. put_reg(dev, PP_TxCFG, 0);
  91. /* do not generate any interrupts on buffer operations */
  92. put_reg(dev, PP_BufCFG, 0);
  93. /* enable transmitter/receiver mode */
  94. put_reg(dev, PP_LineCTL, PP_LineCTL_Rx | PP_LineCTL_Tx);
  95. }
  96. void cs8900_get_enetaddr(struct eth_device *dev)
  97. {
  98. int i;
  99. /* verify chip id */
  100. if (get_reg_init_bus(dev, PP_ChipID) != 0x630e)
  101. return;
  102. cs8900_reset(dev);
  103. if ((get_reg(dev, PP_SelfSTAT) &
  104. (PP_SelfSTAT_EEPROM | PP_SelfSTAT_EEPROM_OK)) ==
  105. (PP_SelfSTAT_EEPROM | PP_SelfSTAT_EEPROM_OK)) {
  106. /* Load the MAC from EEPROM */
  107. for (i = 0; i < 3; i++) {
  108. u32 Addr;
  109. Addr = get_reg(dev, PP_IA + i * 2);
  110. dev->enetaddr[i * 2] = Addr & 0xFF;
  111. dev->enetaddr[i * 2 + 1] = Addr >> 8;
  112. }
  113. }
  114. }
  115. void cs8900_halt(struct eth_device *dev)
  116. {
  117. /* disable transmitter/receiver mode */
  118. put_reg(dev, PP_LineCTL, 0);
  119. /* "shutdown" to show ChipID or kernel wouldn't find he cs8900 ... */
  120. get_reg_init_bus(dev, PP_ChipID);
  121. }
  122. static int cs8900_init(struct eth_device *dev, bd_t * bd)
  123. {
  124. uchar *enetaddr = dev->enetaddr;
  125. u16 id;
  126. /* verify chip id */
  127. id = get_reg_init_bus(dev, PP_ChipID);
  128. if (id != 0x630e) {
  129. printf ("CS8900 Ethernet chip not found: "
  130. "ID=0x%04x instead 0x%04x\n", id, 0x630e);
  131. return 1;
  132. }
  133. cs8900_reset (dev);
  134. /* set the ethernet address */
  135. put_reg(dev, PP_IA + 0, enetaddr[0] | (enetaddr[1] << 8));
  136. put_reg(dev, PP_IA + 2, enetaddr[2] | (enetaddr[3] << 8));
  137. put_reg(dev, PP_IA + 4, enetaddr[4] | (enetaddr[5] << 8));
  138. cs8900_reginit(dev);
  139. return 0;
  140. }
  141. /* Get a data block via Ethernet */
  142. static int cs8900_recv(struct eth_device *dev)
  143. {
  144. int i;
  145. u16 rxlen;
  146. u16 *addr;
  147. u16 status;
  148. struct cs8900_priv *priv = (struct cs8900_priv *)(dev->priv);
  149. status = get_reg(dev, PP_RER);
  150. if ((status & PP_RER_RxOK) == 0)
  151. return 0;
  152. status = REG_READ(&priv->regs->rtdata);
  153. rxlen = REG_READ(&priv->regs->rtdata);
  154. if (rxlen > PKTSIZE_ALIGN + PKTALIGN)
  155. debug("packet too big!\n");
  156. for (addr = (u16 *)net_rx_packets[0], i = rxlen >> 1; i > 0; i--)
  157. *addr++ = REG_READ(&priv->regs->rtdata);
  158. if (rxlen & 1)
  159. *addr++ = REG_READ(&priv->regs->rtdata);
  160. /* Pass the packet up to the protocol layers. */
  161. net_process_received_packet(net_rx_packets[0], rxlen);
  162. return rxlen;
  163. }
  164. /* Send a data block via Ethernet. */
  165. static int cs8900_send(struct eth_device *dev, void *packet, int length)
  166. {
  167. volatile u16 *addr;
  168. int tmo;
  169. u16 s;
  170. struct cs8900_priv *priv = (struct cs8900_priv *)(dev->priv);
  171. retry:
  172. /* initiate a transmit sequence */
  173. REG_WRITE(PP_TxCmd_TxStart_Full, &priv->regs->txcmd);
  174. REG_WRITE(length, &priv->regs->txlen);
  175. /* Test to see if the chip has allocated memory for the packet */
  176. if ((get_reg(dev, PP_BusSTAT) & PP_BusSTAT_TxRDY) == 0) {
  177. /* Oops... this should not happen! */
  178. debug("cs: unable to send packet; retrying...\n");
  179. for (tmo = get_timer(0) + 5 * CONFIG_SYS_HZ;
  180. get_timer(0) < tmo;)
  181. /*NOP*/;
  182. cs8900_reset(dev);
  183. cs8900_reginit(dev);
  184. goto retry;
  185. }
  186. /* Write the contents of the packet */
  187. /* assume even number of bytes */
  188. for (addr = packet; length > 0; length -= 2)
  189. REG_WRITE(*addr++, &priv->regs->rtdata);
  190. /* wait for transfer to succeed */
  191. tmo = get_timer(0) + 5 * CONFIG_SYS_HZ;
  192. while ((s = get_reg(dev, PP_TER) & ~0x1F) == 0) {
  193. if (get_timer(0) >= tmo)
  194. break;
  195. }
  196. /* nothing */ ;
  197. if((s & (PP_TER_CRS | PP_TER_TxOK)) != PP_TER_TxOK) {
  198. debug("\ntransmission error %#x\n", s);
  199. }
  200. return 0;
  201. }
  202. static void cs8900_e2prom_ready(struct eth_device *dev)
  203. {
  204. while (get_reg(dev, PP_SelfSTAT) & SI_BUSY)
  205. ;
  206. }
  207. /***********************************************************/
  208. /* read a 16-bit word out of the EEPROM */
  209. /***********************************************************/
  210. int cs8900_e2prom_read(struct eth_device *dev,
  211. u8 addr, u16 *value)
  212. {
  213. cs8900_e2prom_ready(dev);
  214. put_reg(dev, PP_EECMD, EEPROM_READ_CMD | addr);
  215. cs8900_e2prom_ready(dev);
  216. *value = get_reg(dev, PP_EEData);
  217. return 0;
  218. }
  219. /***********************************************************/
  220. /* write a 16-bit word into the EEPROM */
  221. /***********************************************************/
  222. int cs8900_e2prom_write(struct eth_device *dev, u8 addr, u16 value)
  223. {
  224. cs8900_e2prom_ready(dev);
  225. put_reg(dev, PP_EECMD, EEPROM_WRITE_EN);
  226. cs8900_e2prom_ready(dev);
  227. put_reg(dev, PP_EEData, value);
  228. put_reg(dev, PP_EECMD, EEPROM_WRITE_CMD | addr);
  229. cs8900_e2prom_ready(dev);
  230. put_reg(dev, PP_EECMD, EEPROM_WRITE_DIS);
  231. cs8900_e2prom_ready(dev);
  232. return 0;
  233. }
  234. int cs8900_initialize(u8 dev_num, int base_addr)
  235. {
  236. struct eth_device *dev;
  237. struct cs8900_priv *priv;
  238. dev = malloc(sizeof(*dev));
  239. if (!dev) {
  240. return 0;
  241. }
  242. memset(dev, 0, sizeof(*dev));
  243. priv = malloc(sizeof(*priv));
  244. if (!priv) {
  245. free(dev);
  246. return 0;
  247. }
  248. memset(priv, 0, sizeof(*priv));
  249. priv->regs = (struct cs8900_regs *)base_addr;
  250. dev->iobase = base_addr;
  251. dev->priv = priv;
  252. dev->init = cs8900_init;
  253. dev->halt = cs8900_halt;
  254. dev->send = cs8900_send;
  255. dev->recv = cs8900_recv;
  256. /* Load MAC address from EEPROM */
  257. cs8900_get_enetaddr(dev);
  258. sprintf(dev->name, "%s-%hu", CS8900_DRIVERNAME, dev_num);
  259. eth_register(dev);
  260. return 0;
  261. }