smc911x.c 5.8 KB

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