smc911x.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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, void *packet, int length)
  125. {
  126. u32 *data = (u32*)packet;
  127. u32 tmplen;
  128. u32 status;
  129. smc911x_reg_write(dev, TX_DATA_FIFO, TX_CMD_A_INT_FIRST_SEG |
  130. TX_CMD_A_INT_LAST_SEG | length);
  131. smc911x_reg_write(dev, TX_DATA_FIFO, length);
  132. tmplen = (length + 3) / 4;
  133. while (tmplen--)
  134. pkt_data_push(dev, TX_DATA_FIFO, *data++);
  135. /* wait for transmission */
  136. while (!((smc911x_reg_read(dev, TX_FIFO_INF) &
  137. TX_FIFO_INF_TSUSED) >> 16));
  138. /* get status. Ignore 'no carrier' error, it has no meaning for
  139. * full duplex operation
  140. */
  141. status = smc911x_reg_read(dev, TX_STATUS_FIFO) &
  142. (TX_STS_LOC | TX_STS_LATE_COLL | TX_STS_MANY_COLL |
  143. TX_STS_MANY_DEFER | TX_STS_UNDERRUN);
  144. if (!status)
  145. return 0;
  146. printf(DRIVERNAME ": failed to send packet: %s%s%s%s%s\n",
  147. status & TX_STS_LOC ? "TX_STS_LOC " : "",
  148. status & TX_STS_LATE_COLL ? "TX_STS_LATE_COLL " : "",
  149. status & TX_STS_MANY_COLL ? "TX_STS_MANY_COLL " : "",
  150. status & TX_STS_MANY_DEFER ? "TX_STS_MANY_DEFER " : "",
  151. status & TX_STS_UNDERRUN ? "TX_STS_UNDERRUN" : "");
  152. return -1;
  153. }
  154. static void smc911x_halt(struct eth_device *dev)
  155. {
  156. smc911x_reset(dev);
  157. }
  158. static int smc911x_rx(struct eth_device *dev)
  159. {
  160. u32 *data = (u32 *)NetRxPackets[0];
  161. u32 pktlen, tmplen;
  162. u32 status;
  163. if ((smc911x_reg_read(dev, RX_FIFO_INF) & RX_FIFO_INF_RXSUSED) >> 16) {
  164. status = smc911x_reg_read(dev, RX_STATUS_FIFO);
  165. pktlen = (status & RX_STS_PKT_LEN) >> 16;
  166. smc911x_reg_write(dev, RX_CFG, 0);
  167. tmplen = (pktlen + 3) / 4;
  168. while (tmplen--)
  169. *data++ = pkt_data_pull(dev, RX_DATA_FIFO);
  170. if (status & RX_STS_ES)
  171. printf(DRIVERNAME
  172. ": dropped bad packet. Status: 0x%08x\n",
  173. status);
  174. else
  175. NetReceive(NetRxPackets[0], pktlen);
  176. }
  177. return 0;
  178. }
  179. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  180. /* wrapper for smc911x_eth_phy_read */
  181. static int smc911x_miiphy_read(const char *devname, u8 phy, u8 reg, u16 *val)
  182. {
  183. struct eth_device *dev = eth_get_dev_by_name(devname);
  184. if (dev)
  185. return smc911x_eth_phy_read(dev, phy, reg, val);
  186. return -1;
  187. }
  188. /* wrapper for smc911x_eth_phy_write */
  189. static int smc911x_miiphy_write(const char *devname, u8 phy, u8 reg, u16 val)
  190. {
  191. struct eth_device *dev = eth_get_dev_by_name(devname);
  192. if (dev)
  193. return smc911x_eth_phy_write(dev, phy, reg, val);
  194. return -1;
  195. }
  196. #endif
  197. int smc911x_initialize(u8 dev_num, int base_addr)
  198. {
  199. unsigned long addrl, addrh;
  200. struct eth_device *dev;
  201. dev = malloc(sizeof(*dev));
  202. if (!dev) {
  203. return -1;
  204. }
  205. memset(dev, 0, sizeof(*dev));
  206. dev->iobase = base_addr;
  207. /* Try to detect chip. Will fail if not present. */
  208. if (smc911x_detect_chip(dev)) {
  209. free(dev);
  210. return 0;
  211. }
  212. addrh = smc911x_get_mac_csr(dev, ADDRH);
  213. addrl = smc911x_get_mac_csr(dev, ADDRL);
  214. if (!(addrl == 0xffffffff && addrh == 0x0000ffff)) {
  215. /* address is obtained from optional eeprom */
  216. dev->enetaddr[0] = addrl;
  217. dev->enetaddr[1] = addrl >> 8;
  218. dev->enetaddr[2] = addrl >> 16;
  219. dev->enetaddr[3] = addrl >> 24;
  220. dev->enetaddr[4] = addrh;
  221. dev->enetaddr[5] = addrh >> 8;
  222. }
  223. dev->init = smc911x_init;
  224. dev->halt = smc911x_halt;
  225. dev->send = smc911x_send;
  226. dev->recv = smc911x_rx;
  227. sprintf(dev->name, "%s-%hu", DRIVERNAME, dev_num);
  228. eth_register(dev);
  229. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  230. miiphy_register(dev->name, smc911x_miiphy_read, smc911x_miiphy_write);
  231. #endif
  232. return 1;
  233. }