cs8900.c 7.8 KB

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