2 * SMSC LAN9[12]1[567] Network driver
4 * (c) 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
6 * See file CREDITS for list of people who contributed to this
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.
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.
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,
33 u32 pkt_data_pull(struct eth_device *dev, u32 addr) \
34 __attribute__ ((weak, alias ("smc911x_reg_read")));
35 void pkt_data_push(struct eth_device *dev, u32 addr, u32 val) \
36 __attribute__ ((weak, alias ("smc911x_reg_write")));
38 static void smc911x_handle_mac_address(struct eth_device *dev)
40 unsigned long addrh, addrl;
41 uchar *m = dev->enetaddr;
43 addrl = m[0] | (m[1] << 8) | (m[2] << 16) | (m[3] << 24);
44 addrh = m[4] | (m[5] << 8);
45 smc911x_set_mac_csr(dev, ADDRL, addrl);
46 smc911x_set_mac_csr(dev, ADDRH, addrh);
48 printf(DRIVERNAME ": MAC %pM\n", m);
51 static int smc911x_eth_phy_read(struct eth_device *dev,
52 u8 phy, u8 reg, u16 *val)
54 while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY)
57 smc911x_set_mac_csr(dev, MII_ACC, phy << 11 | reg << 6 |
60 while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY)
63 *val = smc911x_get_mac_csr(dev, MII_DATA);
68 static int smc911x_eth_phy_write(struct eth_device *dev,
69 u8 phy, u8 reg, u16 val)
71 while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY)
74 smc911x_set_mac_csr(dev, MII_DATA, val);
75 smc911x_set_mac_csr(dev, MII_ACC,
76 phy << 11 | reg << 6 | MII_ACC_MII_BUSY | MII_ACC_MII_WRITE);
78 while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY)
83 static int smc911x_phy_reset(struct eth_device *dev)
87 reg = smc911x_reg_read(dev, PMT_CTRL);
89 reg |= PMT_CTRL_PHY_RST;
90 smc911x_reg_write(dev, PMT_CTRL, reg);
97 static void smc911x_phy_configure(struct eth_device *dev)
102 smc911x_phy_reset(dev);
104 smc911x_eth_phy_write(dev, 1, MII_BMCR, BMCR_RESET);
106 smc911x_eth_phy_write(dev, 1, MII_ADVERTISE, 0x01e1);
107 smc911x_eth_phy_write(dev, 1, MII_BMCR, BMCR_ANENABLE |
113 if ((timeout--) == 0)
116 if (smc911x_eth_phy_read(dev, 1, MII_BMSR, &status) != 0)
118 } while (!(status & BMSR_LSTATUS));
120 printf(DRIVERNAME ": phy initialized\n");
125 printf(DRIVERNAME ": autonegotiation timed out\n");
128 static void smc911x_enable(struct eth_device *dev)
131 smc911x_reg_write(dev, HW_CFG, 8 << 16 | HW_CFG_SF);
133 smc911x_reg_write(dev, GPT_CFG, GPT_CFG_TIMER_EN | 10000);
135 smc911x_reg_write(dev, TX_CFG, TX_CFG_TX_ON);
137 /* no padding to start of packets */
138 smc911x_reg_write(dev, RX_CFG, 0);
140 smc911x_set_mac_csr(dev, MAC_CR, MAC_CR_TXEN | MAC_CR_RXEN |
145 static int smc911x_init(struct eth_device *dev, bd_t * bd)
147 struct chip_id *id = dev->priv;
149 printf(DRIVERNAME ": detected %s controller\n", id->name);
153 /* Configure the PHY, initialize the link state */
154 smc911x_phy_configure(dev);
156 smc911x_handle_mac_address(dev);
158 /* Turn on Tx + Rx */
164 static int smc911x_send(struct eth_device *dev, void *packet, int length)
166 u32 *data = (u32*)packet;
170 smc911x_reg_write(dev, TX_DATA_FIFO, TX_CMD_A_INT_FIRST_SEG |
171 TX_CMD_A_INT_LAST_SEG | length);
172 smc911x_reg_write(dev, TX_DATA_FIFO, length);
174 tmplen = (length + 3) / 4;
177 pkt_data_push(dev, TX_DATA_FIFO, *data++);
179 /* wait for transmission */
180 while (!((smc911x_reg_read(dev, TX_FIFO_INF) &
181 TX_FIFO_INF_TSUSED) >> 16));
183 /* get status. Ignore 'no carrier' error, it has no meaning for
184 * full duplex operation
186 status = smc911x_reg_read(dev, TX_STATUS_FIFO) &
187 (TX_STS_LOC | TX_STS_LATE_COLL | TX_STS_MANY_COLL |
188 TX_STS_MANY_DEFER | TX_STS_UNDERRUN);
193 printf(DRIVERNAME ": failed to send packet: %s%s%s%s%s\n",
194 status & TX_STS_LOC ? "TX_STS_LOC " : "",
195 status & TX_STS_LATE_COLL ? "TX_STS_LATE_COLL " : "",
196 status & TX_STS_MANY_COLL ? "TX_STS_MANY_COLL " : "",
197 status & TX_STS_MANY_DEFER ? "TX_STS_MANY_DEFER " : "",
198 status & TX_STS_UNDERRUN ? "TX_STS_UNDERRUN" : "");
203 static void smc911x_halt(struct eth_device *dev)
208 static int smc911x_rx(struct eth_device *dev)
210 u32 *data = (u32 *)NetRxPackets[0];
214 if ((smc911x_reg_read(dev, RX_FIFO_INF) & RX_FIFO_INF_RXSUSED) >> 16) {
215 status = smc911x_reg_read(dev, RX_STATUS_FIFO);
216 pktlen = (status & RX_STS_PKT_LEN) >> 16;
218 smc911x_reg_write(dev, RX_CFG, 0);
220 tmplen = (pktlen + 3) / 4;
222 *data++ = pkt_data_pull(dev, RX_DATA_FIFO);
224 if (status & RX_STS_ES)
226 ": dropped bad packet. Status: 0x%08x\n",
229 NetReceive(NetRxPackets[0], pktlen);
235 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
236 /* wrapper for smc911x_eth_phy_read */
237 static int smc911x_miiphy_read(const char *devname, u8 phy, u8 reg, u16 *val)
239 struct eth_device *dev = eth_get_dev_by_name(devname);
241 return smc911x_eth_phy_read(dev, phy, reg, val);
244 /* wrapper for smc911x_eth_phy_write */
245 static int smc911x_miiphy_write(const char *devname, u8 phy, u8 reg, u16 val)
247 struct eth_device *dev = eth_get_dev_by_name(devname);
249 return smc911x_eth_phy_write(dev, phy, reg, val);
254 int smc911x_initialize(u8 dev_num, int base_addr)
256 unsigned long addrl, addrh;
257 struct eth_device *dev;
259 dev = malloc(sizeof(*dev));
263 memset(dev, 0, sizeof(*dev));
265 dev->iobase = base_addr;
267 /* Try to detect chip. Will fail if not present. */
268 if (smc911x_detect_chip(dev)) {
273 addrh = smc911x_get_mac_csr(dev, ADDRH);
274 addrl = smc911x_get_mac_csr(dev, ADDRL);
275 if (!(addrl == 0xffffffff && addrh == 0x0000ffff)) {
276 /* address is obtained from optional eeprom */
277 dev->enetaddr[0] = addrl;
278 dev->enetaddr[1] = addrl >> 8;
279 dev->enetaddr[2] = addrl >> 16;
280 dev->enetaddr[3] = addrl >> 24;
281 dev->enetaddr[4] = addrh;
282 dev->enetaddr[5] = addrh >> 8;
285 dev->init = smc911x_init;
286 dev->halt = smc911x_halt;
287 dev->send = smc911x_send;
288 dev->recv = smc911x_rx;
289 sprintf(dev->name, "%s-%hu", DRIVERNAME, dev_num);
293 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
294 miiphy_register(dev->name, smc911x_miiphy_read, smc911x_miiphy_write);