cs8900.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. * See file CREDITS for list of people who contributed to this
  18. * project.
  19. *
  20. * This program is loaded into SRAM in bootstrap mode, where it waits
  21. * for commands on UART1 to read and write memory, jump to code etc.
  22. * A design goal for this program is to be entirely independent of the
  23. * target board. Anything with a CL-PS7111 or EP7211 should be able to run
  24. * this code in bootstrap mode. All the board specifics can be handled on
  25. * the host.
  26. *
  27. * This program is free software; you can redistribute it and/or modify
  28. * it under the terms of the GNU General Public License as published by
  29. * the Free Software Foundation; either version 2 of the License, or
  30. * (at your option) any later version.
  31. *
  32. * This program is distributed in the hope that it will be useful,
  33. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  34. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  35. * GNU General Public License for more details.
  36. *
  37. * You should have received a copy of the GNU General Public License
  38. * along with this program; if not, write to the Free Software
  39. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  40. */
  41. #include <common.h>
  42. #include <command.h>
  43. #include <asm/io.h>
  44. #include <net.h>
  45. #include <malloc.h>
  46. #include "cs8900.h"
  47. #undef DEBUG
  48. /* packet page register access functions */
  49. #ifdef CONFIG_CS8900_BUS32
  50. #define REG_WRITE(v, a) writel((v),(a))
  51. #define REG_READ(a) readl((a))
  52. /* we don't need 16 bit initialisation on 32 bit bus */
  53. #define get_reg_init_bus(r,d) get_reg((r),(d))
  54. #else
  55. #define REG_WRITE(v, a) writew((v),(a))
  56. #define REG_READ(a) readw((a))
  57. static u16 get_reg_init_bus(struct eth_device *dev, int regno)
  58. {
  59. /* force 16 bit busmode */
  60. volatile u8 c;
  61. struct cs8900_priv *priv = (struct cs8900_priv *)(dev->priv);
  62. uint8_t volatile * const iob = (uint8_t volatile * const)dev->iobase;
  63. c = readb(iob);
  64. c = readb(iob + 1);
  65. c = readb(iob);
  66. c = readb(iob + 1);
  67. c = readb(iob);
  68. REG_WRITE(regno, &priv->regs->pptr);
  69. return REG_READ(&priv->regs->pdata);
  70. }
  71. #endif
  72. static u16 get_reg(struct eth_device *dev, int regno)
  73. {
  74. struct cs8900_priv *priv = (struct cs8900_priv *)(dev->priv);
  75. REG_WRITE(regno, &priv->regs->pptr);
  76. return REG_READ(&priv->regs->pdata);
  77. }
  78. static void put_reg(struct eth_device *dev, int regno, u16 val)
  79. {
  80. struct cs8900_priv *priv = (struct cs8900_priv *)(dev->priv);
  81. REG_WRITE(regno, &priv->regs->pptr);
  82. REG_WRITE(val, &priv->regs->pdata);
  83. }
  84. static void cs8900_reset(struct eth_device *dev)
  85. {
  86. int tmo;
  87. u16 us;
  88. /* reset NIC */
  89. put_reg(dev, PP_SelfCTL, get_reg(dev, PP_SelfCTL) | PP_SelfCTL_Reset);
  90. /* wait for 200ms */
  91. udelay(200000);
  92. /* Wait until the chip is reset */
  93. tmo = get_timer(0) + 1 * CONFIG_SYS_HZ;
  94. while ((((us = get_reg_init_bus(dev, PP_SelfSTAT)) &
  95. PP_SelfSTAT_InitD) == 0) && tmo < get_timer(0))
  96. /*NOP*/;
  97. }
  98. static void cs8900_reginit(struct eth_device *dev)
  99. {
  100. /* receive only error free packets addressed to this card */
  101. put_reg(dev, PP_RxCTL,
  102. PP_RxCTL_IA | PP_RxCTL_Broadcast | PP_RxCTL_RxOK);
  103. /* do not generate any interrupts on receive operations */
  104. put_reg(dev, PP_RxCFG, 0);
  105. /* do not generate any interrupts on transmit operations */
  106. put_reg(dev, PP_TxCFG, 0);
  107. /* do not generate any interrupts on buffer operations */
  108. put_reg(dev, PP_BufCFG, 0);
  109. /* enable transmitter/receiver mode */
  110. put_reg(dev, PP_LineCTL, PP_LineCTL_Rx | PP_LineCTL_Tx);
  111. }
  112. void cs8900_get_enetaddr(struct eth_device *dev)
  113. {
  114. int i;
  115. /* verify chip id */
  116. if (get_reg_init_bus(dev, PP_ChipID) != 0x630e)
  117. return;
  118. cs8900_reset(dev);
  119. if ((get_reg(dev, PP_SelfSTAT) &
  120. (PP_SelfSTAT_EEPROM | PP_SelfSTAT_EEPROM_OK)) ==
  121. (PP_SelfSTAT_EEPROM | PP_SelfSTAT_EEPROM_OK)) {
  122. /* Load the MAC from EEPROM */
  123. for (i = 0; i < 3; i++) {
  124. u32 Addr;
  125. Addr = get_reg(dev, PP_IA + i * 2);
  126. dev->enetaddr[i * 2] = Addr & 0xFF;
  127. dev->enetaddr[i * 2 + 1] = Addr >> 8;
  128. }
  129. }
  130. }
  131. void cs8900_halt(struct eth_device *dev)
  132. {
  133. /* disable transmitter/receiver mode */
  134. put_reg(dev, PP_LineCTL, 0);
  135. /* "shutdown" to show ChipID or kernel wouldn't find he cs8900 ... */
  136. get_reg_init_bus(dev, PP_ChipID);
  137. }
  138. static int cs8900_init(struct eth_device *dev, bd_t * bd)
  139. {
  140. uchar *enetaddr = dev->enetaddr;
  141. u16 id;
  142. /* verify chip id */
  143. id = get_reg_init_bus(dev, PP_ChipID);
  144. if (id != 0x630e) {
  145. printf ("CS8900 Ethernet chip not found: "
  146. "ID=0x%04x instead 0x%04x\n", id, 0x630e);
  147. return 1;
  148. }
  149. cs8900_reset (dev);
  150. /* set the ethernet address */
  151. put_reg(dev, PP_IA + 0, enetaddr[0] | (enetaddr[1] << 8));
  152. put_reg(dev, PP_IA + 2, enetaddr[2] | (enetaddr[3] << 8));
  153. put_reg(dev, PP_IA + 4, enetaddr[4] | (enetaddr[5] << 8));
  154. cs8900_reginit(dev);
  155. return 0;
  156. }
  157. /* Get a data block via Ethernet */
  158. static int cs8900_recv(struct eth_device *dev)
  159. {
  160. int i;
  161. u16 rxlen;
  162. u16 *addr;
  163. u16 status;
  164. struct cs8900_priv *priv = (struct cs8900_priv *)(dev->priv);
  165. status = get_reg(dev, PP_RER);
  166. if ((status & PP_RER_RxOK) == 0)
  167. return 0;
  168. status = REG_READ(&priv->regs->rtdata);
  169. rxlen = REG_READ(&priv->regs->rtdata);
  170. if (rxlen > PKTSIZE_ALIGN + PKTALIGN)
  171. debug("packet too big!\n");
  172. for (addr = (u16 *) NetRxPackets[0], i = rxlen >> 1; i > 0;
  173. i--)
  174. *addr++ = REG_READ(&priv->regs->rtdata);
  175. if (rxlen & 1)
  176. *addr++ = REG_READ(&priv->regs->rtdata);
  177. /* Pass the packet up to the protocol layers. */
  178. NetReceive (NetRxPackets[0], rxlen);
  179. return rxlen;
  180. }
  181. /* Send a data block via Ethernet. */
  182. static int cs8900_send(struct eth_device *dev,
  183. volatile void *packet, int length)
  184. {
  185. volatile u16 *addr;
  186. int tmo;
  187. u16 s;
  188. struct cs8900_priv *priv = (struct cs8900_priv *)(dev->priv);
  189. retry:
  190. /* initiate a transmit sequence */
  191. REG_WRITE(PP_TxCmd_TxStart_Full, &priv->regs->txcmd);
  192. REG_WRITE(length, &priv->regs->txlen);
  193. /* Test to see if the chip has allocated memory for the packet */
  194. if ((get_reg(dev, PP_BusSTAT) & PP_BusSTAT_TxRDY) == 0) {
  195. /* Oops... this should not happen! */
  196. debug("cs: unable to send packet; retrying...\n");
  197. for (tmo = get_timer(0) + 5 * CONFIG_SYS_HZ;
  198. get_timer(0) < tmo;)
  199. /*NOP*/;
  200. cs8900_reset(dev);
  201. cs8900_reginit(dev);
  202. goto retry;
  203. }
  204. /* Write the contents of the packet */
  205. /* assume even number of bytes */
  206. for (addr = packet; length > 0; length -= 2)
  207. REG_WRITE(*addr++, &priv->regs->rtdata);
  208. /* wait for transfer to succeed */
  209. tmo = get_timer(0) + 5 * CONFIG_SYS_HZ;
  210. while ((s = get_reg(dev, PP_TER) & ~0x1F) == 0) {
  211. if (get_timer(0) >= tmo)
  212. break;
  213. }
  214. /* nothing */ ;
  215. if((s & (PP_TER_CRS | PP_TER_TxOK)) != PP_TER_TxOK) {
  216. debug("\ntransmission error %#x\n", s);
  217. }
  218. return 0;
  219. }
  220. static void cs8900_e2prom_ready(struct eth_device *dev)
  221. {
  222. while (get_reg(dev, PP_SelfSTAT) & SI_BUSY)
  223. ;
  224. }
  225. /***********************************************************/
  226. /* read a 16-bit word out of the EEPROM */
  227. /***********************************************************/
  228. int cs8900_e2prom_read(struct eth_device *dev,
  229. u8 addr, u16 *value)
  230. {
  231. cs8900_e2prom_ready(dev);
  232. put_reg(dev, PP_EECMD, EEPROM_READ_CMD | addr);
  233. cs8900_e2prom_ready(dev);
  234. *value = get_reg(dev, PP_EEData);
  235. return 0;
  236. }
  237. /***********************************************************/
  238. /* write a 16-bit word into the EEPROM */
  239. /***********************************************************/
  240. int cs8900_e2prom_write(struct eth_device *dev, u8 addr, u16 value)
  241. {
  242. cs8900_e2prom_ready(dev);
  243. put_reg(dev, PP_EECMD, EEPROM_WRITE_EN);
  244. cs8900_e2prom_ready(dev);
  245. put_reg(dev, PP_EEData, value);
  246. put_reg(dev, PP_EECMD, EEPROM_WRITE_CMD | addr);
  247. cs8900_e2prom_ready(dev);
  248. put_reg(dev, PP_EECMD, EEPROM_WRITE_DIS);
  249. cs8900_e2prom_ready(dev);
  250. return 0;
  251. }
  252. int cs8900_initialize(u8 dev_num, int base_addr)
  253. {
  254. struct eth_device *dev;
  255. struct cs8900_priv *priv;
  256. dev = malloc(sizeof(*dev));
  257. if (!dev) {
  258. return 0;
  259. }
  260. memset(dev, 0, sizeof(*dev));
  261. priv = malloc(sizeof(*priv));
  262. if (!priv) {
  263. free(dev);
  264. return 0;
  265. }
  266. memset(priv, 0, sizeof(*priv));
  267. priv->regs = (struct cs8900_regs *)base_addr;
  268. dev->iobase = base_addr;
  269. dev->priv = priv;
  270. dev->init = cs8900_init;
  271. dev->halt = cs8900_halt;
  272. dev->send = cs8900_send;
  273. dev->recv = cs8900_recv;
  274. /* Load MAC address from EEPROM */
  275. cs8900_get_enetaddr(dev);
  276. sprintf(dev->name, "%s-%hu", CS8900_DRIVERNAME, dev_num);
  277. eth_register(dev);
  278. return 0;
  279. }