smc911x.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * SMSC LAN9[12]1[567] Network driver
  3. *
  4. * (c) 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #include <common.h>
  25. #include <command.h>
  26. #include <malloc.h>
  27. #include <net.h>
  28. #include <miiphy.h>
  29. #include "smc911x.h"
  30. u32 pkt_data_pull(struct eth_device *dev, u32 addr) \
  31. __attribute__ ((weak, alias ("smc911x_reg_read")));
  32. void pkt_data_push(struct eth_device *dev, u32 addr, u32 val) \
  33. __attribute__ ((weak, alias ("smc911x_reg_write")));
  34. static void smc911x_handle_mac_address(struct eth_device *dev)
  35. {
  36. unsigned long addrh, addrl;
  37. uchar *m = dev->enetaddr;
  38. addrl = m[0] | (m[1] << 8) | (m[2] << 16) | (m[3] << 24);
  39. addrh = m[4] | (m[5] << 8);
  40. smc911x_set_mac_csr(dev, ADDRL, addrl);
  41. smc911x_set_mac_csr(dev, ADDRH, addrh);
  42. printf(DRIVERNAME ": MAC %pM\n", m);
  43. }
  44. static int smc911x_eth_phy_read(struct eth_device *dev,
  45. u8 phy, u8 reg, u16 *val)
  46. {
  47. while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY)
  48. ;
  49. smc911x_set_mac_csr(dev, MII_ACC, phy << 11 | reg << 6 |
  50. MII_ACC_MII_BUSY);
  51. while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY)
  52. ;
  53. *val = smc911x_get_mac_csr(dev, MII_DATA);
  54. return 0;
  55. }
  56. static int smc911x_eth_phy_write(struct eth_device *dev,
  57. u8 phy, u8 reg, u16 val)
  58. {
  59. while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY)
  60. ;
  61. smc911x_set_mac_csr(dev, MII_DATA, val);
  62. smc911x_set_mac_csr(dev, MII_ACC,
  63. phy << 11 | reg << 6 | MII_ACC_MII_BUSY | MII_ACC_MII_WRITE);
  64. while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY)
  65. ;
  66. return 0;
  67. }
  68. static int smc911x_phy_reset(struct eth_device *dev)
  69. {
  70. u32 reg;
  71. reg = smc911x_reg_read(dev, PMT_CTRL);
  72. reg &= ~0xfffff030;
  73. reg |= PMT_CTRL_PHY_RST;
  74. smc911x_reg_write(dev, PMT_CTRL, reg);
  75. mdelay(100);
  76. return 0;
  77. }
  78. static void smc911x_phy_configure(struct eth_device *dev)
  79. {
  80. int timeout;
  81. u16 status;
  82. smc911x_phy_reset(dev);
  83. smc911x_eth_phy_write(dev, 1, MII_BMCR, BMCR_RESET);
  84. mdelay(1);
  85. smc911x_eth_phy_write(dev, 1, MII_ADVERTISE, 0x01e1);
  86. smc911x_eth_phy_write(dev, 1, MII_BMCR, BMCR_ANENABLE |
  87. BMCR_ANRESTART);
  88. timeout = 5000;
  89. do {
  90. mdelay(1);
  91. if ((timeout--) == 0)
  92. goto err_out;
  93. if (smc911x_eth_phy_read(dev, 1, MII_BMSR, &status) != 0)
  94. goto err_out;
  95. } while (!(status & BMSR_LSTATUS));
  96. printf(DRIVERNAME ": phy initialized\n");
  97. return;
  98. err_out:
  99. printf(DRIVERNAME ": autonegotiation timed out\n");
  100. }
  101. static void smc911x_enable(struct eth_device *dev)
  102. {
  103. /* Enable TX */
  104. smc911x_reg_write(dev, HW_CFG, 8 << 16 | HW_CFG_SF);
  105. smc911x_reg_write(dev, GPT_CFG, GPT_CFG_TIMER_EN | 10000);
  106. smc911x_reg_write(dev, TX_CFG, TX_CFG_TX_ON);
  107. /* no padding to start of packets */
  108. smc911x_reg_write(dev, RX_CFG, 0);
  109. smc911x_set_mac_csr(dev, MAC_CR, MAC_CR_TXEN | MAC_CR_RXEN |
  110. MAC_CR_HBDIS);
  111. }
  112. static int smc911x_init(struct eth_device *dev, bd_t * bd)
  113. {
  114. struct chip_id *id = dev->priv;
  115. printf(DRIVERNAME ": detected %s controller\n", id->name);
  116. smc911x_reset(dev);
  117. /* Configure the PHY, initialize the link state */
  118. smc911x_phy_configure(dev);
  119. smc911x_handle_mac_address(dev);
  120. /* Turn on Tx + Rx */
  121. smc911x_enable(dev);
  122. return 0;
  123. }
  124. static int smc911x_send(struct eth_device *dev,
  125. volatile void *packet, int length)
  126. {
  127. u32 *data = (u32*)packet;
  128. u32 tmplen;
  129. u32 status;
  130. smc911x_reg_write(dev, TX_DATA_FIFO, TX_CMD_A_INT_FIRST_SEG |
  131. TX_CMD_A_INT_LAST_SEG | length);
  132. smc911x_reg_write(dev, TX_DATA_FIFO, length);
  133. tmplen = (length + 3) / 4;
  134. while (tmplen--)
  135. pkt_data_push(dev, TX_DATA_FIFO, *data++);
  136. /* wait for transmission */
  137. while (!((smc911x_reg_read(dev, TX_FIFO_INF) &
  138. TX_FIFO_INF_TSUSED) >> 16));
  139. /* get status. Ignore 'no carrier' error, it has no meaning for
  140. * full duplex operation
  141. */
  142. status = smc911x_reg_read(dev, TX_STATUS_FIFO) &
  143. (TX_STS_LOC | TX_STS_LATE_COLL | TX_STS_MANY_COLL |
  144. TX_STS_MANY_DEFER | TX_STS_UNDERRUN);
  145. if (!status)
  146. return 0;
  147. printf(DRIVERNAME ": failed to send packet: %s%s%s%s%s\n",
  148. status & TX_STS_LOC ? "TX_STS_LOC " : "",
  149. status & TX_STS_LATE_COLL ? "TX_STS_LATE_COLL " : "",
  150. status & TX_STS_MANY_COLL ? "TX_STS_MANY_COLL " : "",
  151. status & TX_STS_MANY_DEFER ? "TX_STS_MANY_DEFER " : "",
  152. status & TX_STS_UNDERRUN ? "TX_STS_UNDERRUN" : "");
  153. return -1;
  154. }
  155. static void smc911x_halt(struct eth_device *dev)
  156. {
  157. smc911x_reset(dev);
  158. }
  159. static int smc911x_rx(struct eth_device *dev)
  160. {
  161. u32 *data = (u32 *)NetRxPackets[0];
  162. u32 pktlen, tmplen;
  163. u32 status;
  164. if ((smc911x_reg_read(dev, RX_FIFO_INF) & RX_FIFO_INF_RXSUSED) >> 16) {
  165. status = smc911x_reg_read(dev, RX_STATUS_FIFO);
  166. pktlen = (status & RX_STS_PKT_LEN) >> 16;
  167. smc911x_reg_write(dev, RX_CFG, 0);
  168. tmplen = (pktlen + 3) / 4;
  169. while (tmplen--)
  170. *data++ = pkt_data_pull(dev, RX_DATA_FIFO);
  171. if (status & RX_STS_ES)
  172. printf(DRIVERNAME
  173. ": dropped bad packet. Status: 0x%08x\n",
  174. status);
  175. else
  176. NetReceive(NetRxPackets[0], pktlen);
  177. }
  178. return 0;
  179. }
  180. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  181. /* wrapper for smc911x_eth_phy_read */
  182. static int smc911x_miiphy_read(const char *devname, u8 phy, u8 reg, u16 *val)
  183. {
  184. struct eth_device *dev = eth_get_dev_by_name(devname);
  185. if (dev)
  186. return smc911x_eth_phy_read(dev, phy, reg, val);
  187. return -1;
  188. }
  189. /* wrapper for smc911x_eth_phy_write */
  190. static int smc911x_miiphy_write(const char *devname, u8 phy, u8 reg, u16 val)
  191. {
  192. struct eth_device *dev = eth_get_dev_by_name(devname);
  193. if (dev)
  194. return smc911x_eth_phy_write(dev, phy, reg, val);
  195. return -1;
  196. }
  197. #endif
  198. int smc911x_initialize(u8 dev_num, int base_addr)
  199. {
  200. unsigned long addrl, addrh;
  201. struct eth_device *dev;
  202. dev = malloc(sizeof(*dev));
  203. if (!dev) {
  204. return -1;
  205. }
  206. memset(dev, 0, sizeof(*dev));
  207. dev->iobase = base_addr;
  208. /* Try to detect chip. Will fail if not present. */
  209. if (smc911x_detect_chip(dev)) {
  210. free(dev);
  211. return 0;
  212. }
  213. addrh = smc911x_get_mac_csr(dev, ADDRH);
  214. addrl = smc911x_get_mac_csr(dev, ADDRL);
  215. if (!(addrl == 0xffffffff && addrh == 0x0000ffff)) {
  216. /* address is obtained from optional eeprom */
  217. dev->enetaddr[0] = addrl;
  218. dev->enetaddr[1] = addrl >> 8;
  219. dev->enetaddr[2] = addrl >> 16;
  220. dev->enetaddr[3] = addrl >> 24;
  221. dev->enetaddr[4] = addrh;
  222. dev->enetaddr[5] = addrh >> 8;
  223. }
  224. dev->init = smc911x_init;
  225. dev->halt = smc911x_halt;
  226. dev->send = smc911x_send;
  227. dev->recv = smc911x_rx;
  228. sprintf(dev->name, "%s-%hu", DRIVERNAME, dev_num);
  229. eth_register(dev);
  230. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  231. miiphy_register(dev->name, smc911x_miiphy_read, smc911x_miiphy_write);
  232. #endif
  233. return 1;
  234. }