smc911x.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 <net.h>
  27. #include <miiphy.h>
  28. #include "smc911x.h"
  29. u32 pkt_data_pull(u32 addr) \
  30. __attribute__ ((weak, alias ("smc911x_reg_read")));
  31. void pkt_data_push(u32 addr, u32 val) \
  32. __attribute__ ((weak, alias ("smc911x_reg_write")));
  33. #define mdelay(n) udelay((n)*1000)
  34. static int smx911x_handle_mac_address(bd_t *bd)
  35. {
  36. unsigned long addrh, addrl;
  37. uchar m[6];
  38. /* if the environment has a valid mac address then use it */
  39. if (!eth_getenv_enetaddr("ethaddr", m)) {
  40. /* if not, try to get one from the eeprom */
  41. addrh = smc911x_get_mac_csr(ADDRH);
  42. addrl = smc911x_get_mac_csr(ADDRL);
  43. m[0] = (addrl ) & 0xff;
  44. m[1] = (addrl >> 8 ) & 0xff;
  45. m[2] = (addrl >> 16 ) & 0xff;
  46. m[3] = (addrl >> 24 ) & 0xff;
  47. m[4] = (addrh ) & 0xff;
  48. m[5] = (addrh >> 8 ) & 0xff;
  49. /* we get 0xff when there is no eeprom connected */
  50. if ((m[0] & m[1] & m[2] & m[3] & m[4] & m[5]) == 0xff) {
  51. printf(DRIVERNAME ": no valid mac address in environment "
  52. "and no eeprom found\n");
  53. return -1;
  54. }
  55. eth_setenv_enetaddr("ethaddr", m);
  56. }
  57. printf(DRIVERNAME ": MAC %pM\n", m);
  58. return 0;
  59. }
  60. static int smc911x_miiphy_read(u8 phy, u8 reg, u16 *val)
  61. {
  62. while (smc911x_get_mac_csr(MII_ACC) & MII_ACC_MII_BUSY)
  63. ;
  64. smc911x_set_mac_csr(MII_ACC, phy << 11 | reg << 6 | MII_ACC_MII_BUSY);
  65. while (smc911x_get_mac_csr(MII_ACC) & MII_ACC_MII_BUSY)
  66. ;
  67. *val = smc911x_get_mac_csr(MII_DATA);
  68. return 0;
  69. }
  70. static int smc911x_miiphy_write(u8 phy, u8 reg, u16 val)
  71. {
  72. while (smc911x_get_mac_csr(MII_ACC) & MII_ACC_MII_BUSY)
  73. ;
  74. smc911x_set_mac_csr(MII_DATA, val);
  75. smc911x_set_mac_csr(MII_ACC,
  76. phy << 11 | reg << 6 | MII_ACC_MII_BUSY | MII_ACC_MII_WRITE);
  77. while (smc911x_get_mac_csr(MII_ACC) & MII_ACC_MII_BUSY)
  78. ;
  79. return 0;
  80. }
  81. static int smc911x_phy_reset(void)
  82. {
  83. u32 reg;
  84. reg = smc911x_reg_read(PMT_CTRL);
  85. reg &= ~0xfffff030;
  86. reg |= PMT_CTRL_PHY_RST;
  87. smc911x_reg_write(PMT_CTRL, reg);
  88. mdelay(100);
  89. return 0;
  90. }
  91. static void smc911x_phy_configure(void)
  92. {
  93. int timeout;
  94. u16 status;
  95. smc911x_phy_reset();
  96. smc911x_miiphy_write(1, PHY_BMCR, PHY_BMCR_RESET);
  97. mdelay(1);
  98. smc911x_miiphy_write(1, PHY_ANAR, 0x01e1);
  99. smc911x_miiphy_write(1, PHY_BMCR, PHY_BMCR_AUTON | PHY_BMCR_RST_NEG);
  100. timeout = 5000;
  101. do {
  102. mdelay(1);
  103. if ((timeout--) == 0)
  104. goto err_out;
  105. if (smc911x_miiphy_read(1, PHY_BMSR, &status) != 0)
  106. goto err_out;
  107. } while (!(status & PHY_BMSR_LS));
  108. printf(DRIVERNAME ": phy initialized\n");
  109. return;
  110. err_out:
  111. printf(DRIVERNAME ": autonegotiation timed out\n");
  112. }
  113. static void smc911x_enable(void)
  114. {
  115. /* Enable TX */
  116. smc911x_reg_write(HW_CFG, 8 << 16 | HW_CFG_SF);
  117. smc911x_reg_write(GPT_CFG, GPT_CFG_TIMER_EN | 10000);
  118. smc911x_reg_write(TX_CFG, TX_CFG_TX_ON);
  119. /* no padding to start of packets */
  120. smc911x_reg_write(RX_CFG, 0);
  121. smc911x_set_mac_csr(MAC_CR, MAC_CR_TXEN | MAC_CR_RXEN | MAC_CR_HBDIS);
  122. }
  123. int eth_init(bd_t *bd)
  124. {
  125. printf(DRIVERNAME ": initializing\n");
  126. if (smc911x_detect_chip())
  127. goto err_out;
  128. smc911x_reset();
  129. /* Configure the PHY, initialize the link state */
  130. smc911x_phy_configure();
  131. if (smx911x_handle_mac_address(bd))
  132. goto err_out;
  133. /* Turn on Tx + Rx */
  134. smc911x_enable();
  135. return 0;
  136. err_out:
  137. return -1;
  138. }
  139. int eth_send(volatile void *packet, int length)
  140. {
  141. u32 *data = (u32*)packet;
  142. u32 tmplen;
  143. u32 status;
  144. smc911x_reg_write(TX_DATA_FIFO, TX_CMD_A_INT_FIRST_SEG | TX_CMD_A_INT_LAST_SEG | length);
  145. smc911x_reg_write(TX_DATA_FIFO, length);
  146. tmplen = (length + 3) / 4;
  147. while (tmplen--)
  148. pkt_data_push(TX_DATA_FIFO, *data++);
  149. /* wait for transmission */
  150. while (!((smc911x_reg_read(TX_FIFO_INF) & TX_FIFO_INF_TSUSED) >> 16));
  151. /* get status. Ignore 'no carrier' error, it has no meaning for
  152. * full duplex operation
  153. */
  154. status = smc911x_reg_read(TX_STATUS_FIFO) & (TX_STS_LOC | TX_STS_LATE_COLL |
  155. TX_STS_MANY_COLL | TX_STS_MANY_DEFER | TX_STS_UNDERRUN);
  156. if (!status)
  157. return 0;
  158. printf(DRIVERNAME ": failed to send packet: %s%s%s%s%s\n",
  159. status & TX_STS_LOC ? "TX_STS_LOC " : "",
  160. status & TX_STS_LATE_COLL ? "TX_STS_LATE_COLL " : "",
  161. status & TX_STS_MANY_COLL ? "TX_STS_MANY_COLL " : "",
  162. status & TX_STS_MANY_DEFER ? "TX_STS_MANY_DEFER " : "",
  163. status & TX_STS_UNDERRUN ? "TX_STS_UNDERRUN" : "");
  164. return -1;
  165. }
  166. void eth_halt(void)
  167. {
  168. smc911x_reset();
  169. }
  170. int eth_rx(void)
  171. {
  172. u32 *data = (u32 *)NetRxPackets[0];
  173. u32 pktlen, tmplen;
  174. u32 status;
  175. if ((smc911x_reg_read(RX_FIFO_INF) & RX_FIFO_INF_RXSUSED) >> 16) {
  176. status = smc911x_reg_read(RX_STATUS_FIFO);
  177. pktlen = (status & RX_STS_PKT_LEN) >> 16;
  178. smc911x_reg_write(RX_CFG, 0);
  179. tmplen = (pktlen + 2+ 3) / 4;
  180. while (tmplen--)
  181. *data++ = pkt_data_pull(RX_DATA_FIFO);
  182. if (status & RX_STS_ES)
  183. printf(DRIVERNAME
  184. ": dropped bad packet. Status: 0x%08x\n",
  185. status);
  186. else
  187. NetReceive(NetRxPackets[0], pktlen);
  188. }
  189. return 0;
  190. }