smc911x.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. #define mdelay(n) udelay((n)*1000)
  35. static void smx911x_handle_mac_address(struct eth_device *dev)
  36. {
  37. unsigned long addrh, addrl;
  38. uchar *m = dev->enetaddr;
  39. addrl = m[0] | (m[1] << 8) | (m[2] << 16) | (m[3] << 24);
  40. addrh = m[4] | (m[5] << 8);
  41. smc911x_set_mac_csr(dev, ADDRL, addrl);
  42. smc911x_set_mac_csr(dev, ADDRH, addrh);
  43. printf(DRIVERNAME ": MAC %pM\n", m);
  44. }
  45. static int smc911x_miiphy_read(struct eth_device *dev,
  46. u8 phy, u8 reg, u16 *val)
  47. {
  48. while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY)
  49. ;
  50. smc911x_set_mac_csr(dev, MII_ACC, phy << 11 | reg << 6 |
  51. MII_ACC_MII_BUSY);
  52. while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY)
  53. ;
  54. *val = smc911x_get_mac_csr(dev, MII_DATA);
  55. return 0;
  56. }
  57. static int smc911x_miiphy_write(struct eth_device *dev,
  58. u8 phy, u8 reg, u16 val)
  59. {
  60. while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY)
  61. ;
  62. smc911x_set_mac_csr(dev, MII_DATA, val);
  63. smc911x_set_mac_csr(dev, MII_ACC,
  64. phy << 11 | reg << 6 | MII_ACC_MII_BUSY | MII_ACC_MII_WRITE);
  65. while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY)
  66. ;
  67. return 0;
  68. }
  69. static int smc911x_phy_reset(struct eth_device *dev)
  70. {
  71. u32 reg;
  72. reg = smc911x_reg_read(dev, PMT_CTRL);
  73. reg &= ~0xfffff030;
  74. reg |= PMT_CTRL_PHY_RST;
  75. smc911x_reg_write(dev, PMT_CTRL, reg);
  76. mdelay(100);
  77. return 0;
  78. }
  79. static void smc911x_phy_configure(struct eth_device *dev)
  80. {
  81. int timeout;
  82. u16 status;
  83. smc911x_phy_reset(dev);
  84. smc911x_miiphy_write(dev, 1, PHY_BMCR, PHY_BMCR_RESET);
  85. mdelay(1);
  86. smc911x_miiphy_write(dev, 1, PHY_ANAR, 0x01e1);
  87. smc911x_miiphy_write(dev, 1, PHY_BMCR, PHY_BMCR_AUTON |
  88. PHY_BMCR_RST_NEG);
  89. timeout = 5000;
  90. do {
  91. mdelay(1);
  92. if ((timeout--) == 0)
  93. goto err_out;
  94. if (smc911x_miiphy_read(dev, 1, PHY_BMSR, &status) != 0)
  95. goto err_out;
  96. } while (!(status & PHY_BMSR_LS));
  97. printf(DRIVERNAME ": phy initialized\n");
  98. return;
  99. err_out:
  100. printf(DRIVERNAME ": autonegotiation timed out\n");
  101. }
  102. static void smc911x_enable(struct eth_device *dev)
  103. {
  104. /* Enable TX */
  105. smc911x_reg_write(dev, HW_CFG, 8 << 16 | HW_CFG_SF);
  106. smc911x_reg_write(dev, GPT_CFG, GPT_CFG_TIMER_EN | 10000);
  107. smc911x_reg_write(dev, TX_CFG, TX_CFG_TX_ON);
  108. /* no padding to start of packets */
  109. smc911x_reg_write(dev, RX_CFG, 0);
  110. smc911x_set_mac_csr(dev, MAC_CR, MAC_CR_TXEN | MAC_CR_RXEN |
  111. MAC_CR_HBDIS);
  112. }
  113. static int smc911x_init(struct eth_device *dev, bd_t * bd)
  114. {
  115. struct chip_id *id = dev->priv;
  116. printf(DRIVERNAME ": detected %s controller\n", id->name);
  117. smc911x_reset(dev);
  118. /* Configure the PHY, initialize the link state */
  119. smc911x_phy_configure(dev);
  120. smx911x_handle_mac_address(dev);
  121. /* Turn on Tx + Rx */
  122. smc911x_enable(dev);
  123. return 0;
  124. }
  125. static int smc911x_send(struct eth_device *dev,
  126. volatile void *packet, int length)
  127. {
  128. u32 *data = (u32*)packet;
  129. u32 tmplen;
  130. u32 status;
  131. smc911x_reg_write(dev, TX_DATA_FIFO, TX_CMD_A_INT_FIRST_SEG |
  132. TX_CMD_A_INT_LAST_SEG | length);
  133. smc911x_reg_write(dev, TX_DATA_FIFO, length);
  134. tmplen = (length + 3) / 4;
  135. while (tmplen--)
  136. pkt_data_push(dev, TX_DATA_FIFO, *data++);
  137. /* wait for transmission */
  138. while (!((smc911x_reg_read(dev, TX_FIFO_INF) &
  139. TX_FIFO_INF_TSUSED) >> 16));
  140. /* get status. Ignore 'no carrier' error, it has no meaning for
  141. * full duplex operation
  142. */
  143. status = smc911x_reg_read(dev, TX_STATUS_FIFO) &
  144. (TX_STS_LOC | TX_STS_LATE_COLL | TX_STS_MANY_COLL |
  145. TX_STS_MANY_DEFER | TX_STS_UNDERRUN);
  146. if (!status)
  147. return 0;
  148. printf(DRIVERNAME ": failed to send packet: %s%s%s%s%s\n",
  149. status & TX_STS_LOC ? "TX_STS_LOC " : "",
  150. status & TX_STS_LATE_COLL ? "TX_STS_LATE_COLL " : "",
  151. status & TX_STS_MANY_COLL ? "TX_STS_MANY_COLL " : "",
  152. status & TX_STS_MANY_DEFER ? "TX_STS_MANY_DEFER " : "",
  153. status & TX_STS_UNDERRUN ? "TX_STS_UNDERRUN" : "");
  154. return -1;
  155. }
  156. static void smc911x_halt(struct eth_device *dev)
  157. {
  158. smc911x_reset(dev);
  159. }
  160. static int smc911x_rx(struct eth_device *dev)
  161. {
  162. u32 *data = (u32 *)NetRxPackets[0];
  163. u32 pktlen, tmplen;
  164. u32 status;
  165. if ((smc911x_reg_read(dev, RX_FIFO_INF) & RX_FIFO_INF_RXSUSED) >> 16) {
  166. status = smc911x_reg_read(dev, RX_STATUS_FIFO);
  167. pktlen = (status & RX_STS_PKT_LEN) >> 16;
  168. smc911x_reg_write(dev, RX_CFG, 0);
  169. tmplen = (pktlen + 2+ 3) / 4;
  170. while (tmplen--)
  171. *data++ = pkt_data_pull(dev, RX_DATA_FIFO);
  172. if (status & RX_STS_ES)
  173. printf(DRIVERNAME
  174. ": dropped bad packet. Status: 0x%08x\n",
  175. status);
  176. else
  177. NetReceive(NetRxPackets[0], pktlen);
  178. }
  179. return 0;
  180. }
  181. int smc911x_initialize(u8 dev_num, int base_addr)
  182. {
  183. unsigned long addrl, addrh;
  184. struct eth_device *dev;
  185. dev = malloc(sizeof(*dev));
  186. if (!dev) {
  187. free(dev);
  188. return 0;
  189. }
  190. memset(dev, 0, sizeof(*dev));
  191. dev->iobase = base_addr;
  192. /* Try to detect chip. Will fail if not present. */
  193. if (smc911x_detect_chip(dev)) {
  194. free(dev);
  195. return 0;
  196. }
  197. addrh = smc911x_get_mac_csr(dev, ADDRH);
  198. addrl = smc911x_get_mac_csr(dev, ADDRL);
  199. dev->enetaddr[0] = addrl;
  200. dev->enetaddr[1] = addrl >> 8;
  201. dev->enetaddr[2] = addrl >> 16;
  202. dev->enetaddr[3] = addrl >> 24;
  203. dev->enetaddr[4] = addrh;
  204. dev->enetaddr[5] = addrh >> 8;
  205. dev->init = smc911x_init;
  206. dev->halt = smc911x_halt;
  207. dev->send = smc911x_send;
  208. dev->recv = smc911x_rx;
  209. sprintf(dev->name, "%s-%hu", DRIVERNAME, dev_num);
  210. eth_register(dev);
  211. return 0;
  212. }