smsc95xx.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors.
  3. * Copyright (C) 2009 NVIDIA, Corporation
  4. * See file CREDITS for list of people who contributed to this
  5. * project.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. #include <asm/unaligned.h>
  23. #include <common.h>
  24. #include <usb.h>
  25. #include <linux/mii.h>
  26. #include "usb_ether.h"
  27. #include <malloc.h>
  28. /* SMSC LAN95xx based USB 2.0 Ethernet Devices */
  29. /* Tx command words */
  30. #define TX_CMD_A_FIRST_SEG_ 0x00002000
  31. #define TX_CMD_A_LAST_SEG_ 0x00001000
  32. /* Rx status word */
  33. #define RX_STS_FL_ 0x3FFF0000 /* Frame Length */
  34. #define RX_STS_ES_ 0x00008000 /* Error Summary */
  35. /* SCSRs */
  36. #define ID_REV 0x00
  37. #define INT_STS 0x08
  38. #define TX_CFG 0x10
  39. #define TX_CFG_ON_ 0x00000004
  40. #define HW_CFG 0x14
  41. #define HW_CFG_BIR_ 0x00001000
  42. #define HW_CFG_RXDOFF_ 0x00000600
  43. #define HW_CFG_MEF_ 0x00000020
  44. #define HW_CFG_BCE_ 0x00000002
  45. #define HW_CFG_LRST_ 0x00000008
  46. #define PM_CTRL 0x20
  47. #define PM_CTL_PHY_RST_ 0x00000010
  48. #define AFC_CFG 0x2C
  49. /*
  50. * Hi watermark = 15.5Kb (~10 mtu pkts)
  51. * low watermark = 3k (~2 mtu pkts)
  52. * backpressure duration = ~ 350us
  53. * Apply FC on any frame.
  54. */
  55. #define AFC_CFG_DEFAULT 0x00F830A1
  56. #define E2P_CMD 0x30
  57. #define E2P_CMD_BUSY_ 0x80000000
  58. #define E2P_CMD_READ_ 0x00000000
  59. #define E2P_CMD_TIMEOUT_ 0x00000400
  60. #define E2P_CMD_LOADED_ 0x00000200
  61. #define E2P_CMD_ADDR_ 0x000001FF
  62. #define E2P_DATA 0x34
  63. #define BURST_CAP 0x38
  64. #define INT_EP_CTL 0x68
  65. #define INT_EP_CTL_PHY_INT_ 0x00008000
  66. #define BULK_IN_DLY 0x6C
  67. /* MAC CSRs */
  68. #define MAC_CR 0x100
  69. #define MAC_CR_MCPAS_ 0x00080000
  70. #define MAC_CR_PRMS_ 0x00040000
  71. #define MAC_CR_HPFILT_ 0x00002000
  72. #define MAC_CR_TXEN_ 0x00000008
  73. #define MAC_CR_RXEN_ 0x00000004
  74. #define ADDRH 0x104
  75. #define ADDRL 0x108
  76. #define MII_ADDR 0x114
  77. #define MII_WRITE_ 0x02
  78. #define MII_BUSY_ 0x01
  79. #define MII_READ_ 0x00 /* ~of MII Write bit */
  80. #define MII_DATA 0x118
  81. #define FLOW 0x11C
  82. #define VLAN1 0x120
  83. #define COE_CR 0x130
  84. #define Tx_COE_EN_ 0x00010000
  85. #define Rx_COE_EN_ 0x00000001
  86. /* Vendor-specific PHY Definitions */
  87. #define PHY_INT_SRC 29
  88. #define PHY_INT_MASK 30
  89. #define PHY_INT_MASK_ANEG_COMP_ ((u16)0x0040)
  90. #define PHY_INT_MASK_LINK_DOWN_ ((u16)0x0010)
  91. #define PHY_INT_MASK_DEFAULT_ (PHY_INT_MASK_ANEG_COMP_ | \
  92. PHY_INT_MASK_LINK_DOWN_)
  93. /* USB Vendor Requests */
  94. #define USB_VENDOR_REQUEST_WRITE_REGISTER 0xA0
  95. #define USB_VENDOR_REQUEST_READ_REGISTER 0xA1
  96. /* Some extra defines */
  97. #define HS_USB_PKT_SIZE 512
  98. #define FS_USB_PKT_SIZE 64
  99. #define DEFAULT_HS_BURST_CAP_SIZE (16 * 1024 + 5 * HS_USB_PKT_SIZE)
  100. #define DEFAULT_FS_BURST_CAP_SIZE (6 * 1024 + 33 * FS_USB_PKT_SIZE)
  101. #define DEFAULT_BULK_IN_DELAY 0x00002000
  102. #define MAX_SINGLE_PACKET_SIZE 2048
  103. #define EEPROM_MAC_OFFSET 0x01
  104. #define SMSC95XX_INTERNAL_PHY_ID 1
  105. #define ETH_P_8021Q 0x8100 /* 802.1Q VLAN Extended Header */
  106. /* local defines */
  107. #define SMSC95XX_BASE_NAME "sms"
  108. #define USB_CTRL_SET_TIMEOUT 5000
  109. #define USB_CTRL_GET_TIMEOUT 5000
  110. #define USB_BULK_SEND_TIMEOUT 5000
  111. #define USB_BULK_RECV_TIMEOUT 5000
  112. #define AX_RX_URB_SIZE 2048
  113. #define PHY_CONNECT_TIMEOUT 5000
  114. #define TURBO_MODE
  115. /* local vars */
  116. static int curr_eth_dev; /* index for name of next device detected */
  117. /* driver private */
  118. struct smsc95xx_private {
  119. size_t rx_urb_size; /* maximum USB URB size */
  120. u32 mac_cr; /* MAC control register value */
  121. int have_hwaddr; /* 1 if we have a hardware MAC address */
  122. };
  123. /*
  124. * Smsc95xx infrastructure commands
  125. */
  126. static int smsc95xx_write_reg(struct ueth_data *dev, u32 index, u32 data)
  127. {
  128. int len;
  129. ALLOC_CACHE_ALIGN_BUFFER(u32, tmpbuf, 1);
  130. cpu_to_le32s(&data);
  131. tmpbuf[0] = data;
  132. len = usb_control_msg(dev->pusb_dev, usb_sndctrlpipe(dev->pusb_dev, 0),
  133. USB_VENDOR_REQUEST_WRITE_REGISTER,
  134. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  135. 00, index, tmpbuf, sizeof(data), USB_CTRL_SET_TIMEOUT);
  136. if (len != sizeof(data)) {
  137. debug("smsc95xx_write_reg failed: index=%d, data=%d, len=%d",
  138. index, data, len);
  139. return -1;
  140. }
  141. return 0;
  142. }
  143. static int smsc95xx_read_reg(struct ueth_data *dev, u32 index, u32 *data)
  144. {
  145. int len;
  146. ALLOC_CACHE_ALIGN_BUFFER(u32, tmpbuf, 1);
  147. len = usb_control_msg(dev->pusb_dev, usb_rcvctrlpipe(dev->pusb_dev, 0),
  148. USB_VENDOR_REQUEST_READ_REGISTER,
  149. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  150. 00, index, tmpbuf, sizeof(data), USB_CTRL_GET_TIMEOUT);
  151. *data = tmpbuf[0];
  152. if (len != sizeof(data)) {
  153. debug("smsc95xx_read_reg failed: index=%d, len=%d",
  154. index, len);
  155. return -1;
  156. }
  157. le32_to_cpus(data);
  158. return 0;
  159. }
  160. /* Loop until the read is completed with timeout */
  161. static int smsc95xx_phy_wait_not_busy(struct ueth_data *dev)
  162. {
  163. unsigned long start_time = get_timer(0);
  164. u32 val;
  165. do {
  166. smsc95xx_read_reg(dev, MII_ADDR, &val);
  167. if (!(val & MII_BUSY_))
  168. return 0;
  169. } while (get_timer(start_time) < 1 * 1000 * 1000);
  170. return -1;
  171. }
  172. static int smsc95xx_mdio_read(struct ueth_data *dev, int phy_id, int idx)
  173. {
  174. u32 val, addr;
  175. /* confirm MII not busy */
  176. if (smsc95xx_phy_wait_not_busy(dev)) {
  177. debug("MII is busy in smsc95xx_mdio_read\n");
  178. return -1;
  179. }
  180. /* set the address, index & direction (read from PHY) */
  181. addr = (phy_id << 11) | (idx << 6) | MII_READ_;
  182. smsc95xx_write_reg(dev, MII_ADDR, addr);
  183. if (smsc95xx_phy_wait_not_busy(dev)) {
  184. debug("Timed out reading MII reg %02X\n", idx);
  185. return -1;
  186. }
  187. smsc95xx_read_reg(dev, MII_DATA, &val);
  188. return (u16)(val & 0xFFFF);
  189. }
  190. static void smsc95xx_mdio_write(struct ueth_data *dev, int phy_id, int idx,
  191. int regval)
  192. {
  193. u32 val, addr;
  194. /* confirm MII not busy */
  195. if (smsc95xx_phy_wait_not_busy(dev)) {
  196. debug("MII is busy in smsc95xx_mdio_write\n");
  197. return;
  198. }
  199. val = regval;
  200. smsc95xx_write_reg(dev, MII_DATA, val);
  201. /* set the address, index & direction (write to PHY) */
  202. addr = (phy_id << 11) | (idx << 6) | MII_WRITE_;
  203. smsc95xx_write_reg(dev, MII_ADDR, addr);
  204. if (smsc95xx_phy_wait_not_busy(dev))
  205. debug("Timed out writing MII reg %02X\n", idx);
  206. }
  207. static int smsc95xx_eeprom_confirm_not_busy(struct ueth_data *dev)
  208. {
  209. unsigned long start_time = get_timer(0);
  210. u32 val;
  211. do {
  212. smsc95xx_read_reg(dev, E2P_CMD, &val);
  213. if (!(val & E2P_CMD_BUSY_))
  214. return 0;
  215. udelay(40);
  216. } while (get_timer(start_time) < 1 * 1000 * 1000);
  217. debug("EEPROM is busy\n");
  218. return -1;
  219. }
  220. static int smsc95xx_wait_eeprom(struct ueth_data *dev)
  221. {
  222. unsigned long start_time = get_timer(0);
  223. u32 val;
  224. do {
  225. smsc95xx_read_reg(dev, E2P_CMD, &val);
  226. if (!(val & E2P_CMD_BUSY_) || (val & E2P_CMD_TIMEOUT_))
  227. break;
  228. udelay(40);
  229. } while (get_timer(start_time) < 1 * 1000 * 1000);
  230. if (val & (E2P_CMD_TIMEOUT_ | E2P_CMD_BUSY_)) {
  231. debug("EEPROM read operation timeout\n");
  232. return -1;
  233. }
  234. return 0;
  235. }
  236. static int smsc95xx_read_eeprom(struct ueth_data *dev, u32 offset, u32 length,
  237. u8 *data)
  238. {
  239. u32 val;
  240. int i, ret;
  241. ret = smsc95xx_eeprom_confirm_not_busy(dev);
  242. if (ret)
  243. return ret;
  244. for (i = 0; i < length; i++) {
  245. val = E2P_CMD_BUSY_ | E2P_CMD_READ_ | (offset & E2P_CMD_ADDR_);
  246. smsc95xx_write_reg(dev, E2P_CMD, val);
  247. ret = smsc95xx_wait_eeprom(dev);
  248. if (ret < 0)
  249. return ret;
  250. smsc95xx_read_reg(dev, E2P_DATA, &val);
  251. data[i] = val & 0xFF;
  252. offset++;
  253. }
  254. return 0;
  255. }
  256. /*
  257. * mii_nway_restart - restart NWay (autonegotiation) for this interface
  258. *
  259. * Returns 0 on success, negative on error.
  260. */
  261. static int mii_nway_restart(struct ueth_data *dev)
  262. {
  263. int bmcr;
  264. int r = -1;
  265. /* if autoneg is off, it's an error */
  266. bmcr = smsc95xx_mdio_read(dev, dev->phy_id, MII_BMCR);
  267. if (bmcr & BMCR_ANENABLE) {
  268. bmcr |= BMCR_ANRESTART;
  269. smsc95xx_mdio_write(dev, dev->phy_id, MII_BMCR, bmcr);
  270. r = 0;
  271. }
  272. return r;
  273. }
  274. static int smsc95xx_phy_initialize(struct ueth_data *dev)
  275. {
  276. smsc95xx_mdio_write(dev, dev->phy_id, MII_BMCR, BMCR_RESET);
  277. smsc95xx_mdio_write(dev, dev->phy_id, MII_ADVERTISE,
  278. ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP |
  279. ADVERTISE_PAUSE_ASYM);
  280. /* read to clear */
  281. smsc95xx_mdio_read(dev, dev->phy_id, PHY_INT_SRC);
  282. smsc95xx_mdio_write(dev, dev->phy_id, PHY_INT_MASK,
  283. PHY_INT_MASK_DEFAULT_);
  284. mii_nway_restart(dev);
  285. debug("phy initialised succesfully\n");
  286. return 0;
  287. }
  288. static int smsc95xx_init_mac_address(struct eth_device *eth,
  289. struct ueth_data *dev)
  290. {
  291. /* try reading mac address from EEPROM */
  292. if (smsc95xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
  293. eth->enetaddr) == 0) {
  294. if (is_valid_ether_addr(eth->enetaddr)) {
  295. /* eeprom values are valid so use them */
  296. debug("MAC address read from EEPROM\n");
  297. return 0;
  298. }
  299. }
  300. /*
  301. * No eeprom, or eeprom values are invalid. Generating a random MAC
  302. * address is not safe. Just return an error.
  303. */
  304. return -1;
  305. }
  306. static int smsc95xx_write_hwaddr(struct eth_device *eth)
  307. {
  308. struct ueth_data *dev = (struct ueth_data *)eth->priv;
  309. struct smsc95xx_private *priv = dev->dev_priv;
  310. u32 addr_lo = __get_unaligned_le32(&eth->enetaddr[0]);
  311. u32 addr_hi = __get_unaligned_le16(&eth->enetaddr[4]);
  312. int ret;
  313. /* set hardware address */
  314. debug("** %s()\n", __func__);
  315. ret = smsc95xx_write_reg(dev, ADDRL, addr_lo);
  316. if (ret < 0)
  317. return ret;
  318. ret = smsc95xx_write_reg(dev, ADDRH, addr_hi);
  319. if (ret < 0)
  320. return ret;
  321. debug("MAC %pM\n", eth->enetaddr);
  322. priv->have_hwaddr = 1;
  323. return 0;
  324. }
  325. /* Enable or disable Tx & Rx checksum offload engines */
  326. static int smsc95xx_set_csums(struct ueth_data *dev,
  327. int use_tx_csum, int use_rx_csum)
  328. {
  329. u32 read_buf;
  330. int ret = smsc95xx_read_reg(dev, COE_CR, &read_buf);
  331. if (ret < 0)
  332. return ret;
  333. if (use_tx_csum)
  334. read_buf |= Tx_COE_EN_;
  335. else
  336. read_buf &= ~Tx_COE_EN_;
  337. if (use_rx_csum)
  338. read_buf |= Rx_COE_EN_;
  339. else
  340. read_buf &= ~Rx_COE_EN_;
  341. ret = smsc95xx_write_reg(dev, COE_CR, read_buf);
  342. if (ret < 0)
  343. return ret;
  344. debug("COE_CR = 0x%08x\n", read_buf);
  345. return 0;
  346. }
  347. static void smsc95xx_set_multicast(struct ueth_data *dev)
  348. {
  349. struct smsc95xx_private *priv = dev->dev_priv;
  350. /* No multicast in u-boot */
  351. priv->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
  352. }
  353. /* starts the TX path */
  354. static void smsc95xx_start_tx_path(struct ueth_data *dev)
  355. {
  356. struct smsc95xx_private *priv = dev->dev_priv;
  357. u32 reg_val;
  358. /* Enable Tx at MAC */
  359. priv->mac_cr |= MAC_CR_TXEN_;
  360. smsc95xx_write_reg(dev, MAC_CR, priv->mac_cr);
  361. /* Enable Tx at SCSRs */
  362. reg_val = TX_CFG_ON_;
  363. smsc95xx_write_reg(dev, TX_CFG, reg_val);
  364. }
  365. /* Starts the Receive path */
  366. static void smsc95xx_start_rx_path(struct ueth_data *dev)
  367. {
  368. struct smsc95xx_private *priv = dev->dev_priv;
  369. priv->mac_cr |= MAC_CR_RXEN_;
  370. smsc95xx_write_reg(dev, MAC_CR, priv->mac_cr);
  371. }
  372. /*
  373. * Smsc95xx callbacks
  374. */
  375. static int smsc95xx_init(struct eth_device *eth, bd_t *bd)
  376. {
  377. int ret;
  378. u32 write_buf;
  379. u32 read_buf;
  380. u32 burst_cap;
  381. int timeout;
  382. struct ueth_data *dev = (struct ueth_data *)eth->priv;
  383. struct smsc95xx_private *priv =
  384. (struct smsc95xx_private *)dev->dev_priv;
  385. #define TIMEOUT_RESOLUTION 50 /* ms */
  386. int link_detected;
  387. debug("** %s()\n", __func__);
  388. dev->phy_id = SMSC95XX_INTERNAL_PHY_ID; /* fixed phy id */
  389. write_buf = HW_CFG_LRST_;
  390. ret = smsc95xx_write_reg(dev, HW_CFG, write_buf);
  391. if (ret < 0)
  392. return ret;
  393. timeout = 0;
  394. do {
  395. ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
  396. if (ret < 0)
  397. return ret;
  398. udelay(10 * 1000);
  399. timeout++;
  400. } while ((read_buf & HW_CFG_LRST_) && (timeout < 100));
  401. if (timeout >= 100) {
  402. debug("timeout waiting for completion of Lite Reset\n");
  403. return -1;
  404. }
  405. write_buf = PM_CTL_PHY_RST_;
  406. ret = smsc95xx_write_reg(dev, PM_CTRL, write_buf);
  407. if (ret < 0)
  408. return ret;
  409. timeout = 0;
  410. do {
  411. ret = smsc95xx_read_reg(dev, PM_CTRL, &read_buf);
  412. if (ret < 0)
  413. return ret;
  414. udelay(10 * 1000);
  415. timeout++;
  416. } while ((read_buf & PM_CTL_PHY_RST_) && (timeout < 100));
  417. if (timeout >= 100) {
  418. debug("timeout waiting for PHY Reset\n");
  419. return -1;
  420. }
  421. if (!priv->have_hwaddr && smsc95xx_init_mac_address(eth, dev) == 0)
  422. priv->have_hwaddr = 1;
  423. if (!priv->have_hwaddr) {
  424. puts("Error: SMSC95xx: No MAC address set - set usbethaddr\n");
  425. return -1;
  426. }
  427. if (smsc95xx_write_hwaddr(eth) < 0)
  428. return -1;
  429. ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
  430. if (ret < 0)
  431. return ret;
  432. debug("Read Value from HW_CFG : 0x%08x\n", read_buf);
  433. read_buf |= HW_CFG_BIR_;
  434. ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
  435. if (ret < 0)
  436. return ret;
  437. ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
  438. if (ret < 0)
  439. return ret;
  440. debug("Read Value from HW_CFG after writing "
  441. "HW_CFG_BIR_: 0x%08x\n", read_buf);
  442. #ifdef TURBO_MODE
  443. if (dev->pusb_dev->speed == USB_SPEED_HIGH) {
  444. burst_cap = DEFAULT_HS_BURST_CAP_SIZE / HS_USB_PKT_SIZE;
  445. priv->rx_urb_size = DEFAULT_HS_BURST_CAP_SIZE;
  446. } else {
  447. burst_cap = DEFAULT_FS_BURST_CAP_SIZE / FS_USB_PKT_SIZE;
  448. priv->rx_urb_size = DEFAULT_FS_BURST_CAP_SIZE;
  449. }
  450. #else
  451. burst_cap = 0;
  452. priv->rx_urb_size = MAX_SINGLE_PACKET_SIZE;
  453. #endif
  454. debug("rx_urb_size=%ld\n", (ulong)priv->rx_urb_size);
  455. ret = smsc95xx_write_reg(dev, BURST_CAP, burst_cap);
  456. if (ret < 0)
  457. return ret;
  458. ret = smsc95xx_read_reg(dev, BURST_CAP, &read_buf);
  459. if (ret < 0)
  460. return ret;
  461. debug("Read Value from BURST_CAP after writing: 0x%08x\n", read_buf);
  462. read_buf = DEFAULT_BULK_IN_DELAY;
  463. ret = smsc95xx_write_reg(dev, BULK_IN_DLY, read_buf);
  464. if (ret < 0)
  465. return ret;
  466. ret = smsc95xx_read_reg(dev, BULK_IN_DLY, &read_buf);
  467. if (ret < 0)
  468. return ret;
  469. debug("Read Value from BULK_IN_DLY after writing: "
  470. "0x%08x\n", read_buf);
  471. ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
  472. if (ret < 0)
  473. return ret;
  474. debug("Read Value from HW_CFG: 0x%08x\n", read_buf);
  475. #ifdef TURBO_MODE
  476. read_buf |= (HW_CFG_MEF_ | HW_CFG_BCE_);
  477. #endif
  478. read_buf &= ~HW_CFG_RXDOFF_;
  479. #define NET_IP_ALIGN 0
  480. read_buf |= NET_IP_ALIGN << 9;
  481. ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
  482. if (ret < 0)
  483. return ret;
  484. ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
  485. if (ret < 0)
  486. return ret;
  487. debug("Read Value from HW_CFG after writing: 0x%08x\n", read_buf);
  488. write_buf = 0xFFFFFFFF;
  489. ret = smsc95xx_write_reg(dev, INT_STS, write_buf);
  490. if (ret < 0)
  491. return ret;
  492. ret = smsc95xx_read_reg(dev, ID_REV, &read_buf);
  493. if (ret < 0)
  494. return ret;
  495. debug("ID_REV = 0x%08x\n", read_buf);
  496. /* Init Tx */
  497. write_buf = 0;
  498. ret = smsc95xx_write_reg(dev, FLOW, write_buf);
  499. if (ret < 0)
  500. return ret;
  501. read_buf = AFC_CFG_DEFAULT;
  502. ret = smsc95xx_write_reg(dev, AFC_CFG, read_buf);
  503. if (ret < 0)
  504. return ret;
  505. ret = smsc95xx_read_reg(dev, MAC_CR, &priv->mac_cr);
  506. if (ret < 0)
  507. return ret;
  508. /* Init Rx. Set Vlan */
  509. write_buf = (u32)ETH_P_8021Q;
  510. ret = smsc95xx_write_reg(dev, VLAN1, write_buf);
  511. if (ret < 0)
  512. return ret;
  513. /* Disable checksum offload engines */
  514. ret = smsc95xx_set_csums(dev, 0, 0);
  515. if (ret < 0) {
  516. debug("Failed to set csum offload: %d\n", ret);
  517. return ret;
  518. }
  519. smsc95xx_set_multicast(dev);
  520. if (smsc95xx_phy_initialize(dev) < 0)
  521. return -1;
  522. ret = smsc95xx_read_reg(dev, INT_EP_CTL, &read_buf);
  523. if (ret < 0)
  524. return ret;
  525. /* enable PHY interrupts */
  526. read_buf |= INT_EP_CTL_PHY_INT_;
  527. ret = smsc95xx_write_reg(dev, INT_EP_CTL, read_buf);
  528. if (ret < 0)
  529. return ret;
  530. smsc95xx_start_tx_path(dev);
  531. smsc95xx_start_rx_path(dev);
  532. timeout = 0;
  533. do {
  534. link_detected = smsc95xx_mdio_read(dev, dev->phy_id, MII_BMSR)
  535. & BMSR_LSTATUS;
  536. if (!link_detected) {
  537. if (timeout == 0)
  538. printf("Waiting for Ethernet connection... ");
  539. udelay(TIMEOUT_RESOLUTION * 1000);
  540. timeout += TIMEOUT_RESOLUTION;
  541. }
  542. } while (!link_detected && timeout < PHY_CONNECT_TIMEOUT);
  543. if (link_detected) {
  544. if (timeout != 0)
  545. printf("done.\n");
  546. } else {
  547. printf("unable to connect.\n");
  548. return -1;
  549. }
  550. return 0;
  551. }
  552. static int smsc95xx_send(struct eth_device *eth, void* packet, int length)
  553. {
  554. struct ueth_data *dev = (struct ueth_data *)eth->priv;
  555. int err;
  556. int actual_len;
  557. u32 tx_cmd_a;
  558. u32 tx_cmd_b;
  559. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, msg,
  560. PKTSIZE + sizeof(tx_cmd_a) + sizeof(tx_cmd_b));
  561. debug("** %s(), len %d, buf %#x\n", __func__, length, (int)msg);
  562. if (length > PKTSIZE)
  563. return -1;
  564. tx_cmd_a = (u32)length | TX_CMD_A_FIRST_SEG_ | TX_CMD_A_LAST_SEG_;
  565. tx_cmd_b = (u32)length;
  566. cpu_to_le32s(&tx_cmd_a);
  567. cpu_to_le32s(&tx_cmd_b);
  568. /* prepend cmd_a and cmd_b */
  569. memcpy(msg, &tx_cmd_a, sizeof(tx_cmd_a));
  570. memcpy(msg + sizeof(tx_cmd_a), &tx_cmd_b, sizeof(tx_cmd_b));
  571. memcpy(msg + sizeof(tx_cmd_a) + sizeof(tx_cmd_b), (void *)packet,
  572. length);
  573. err = usb_bulk_msg(dev->pusb_dev,
  574. usb_sndbulkpipe(dev->pusb_dev, dev->ep_out),
  575. (void *)msg,
  576. length + sizeof(tx_cmd_a) + sizeof(tx_cmd_b),
  577. &actual_len,
  578. USB_BULK_SEND_TIMEOUT);
  579. debug("Tx: len = %u, actual = %u, err = %d\n",
  580. length + sizeof(tx_cmd_a) + sizeof(tx_cmd_b),
  581. actual_len, err);
  582. return err;
  583. }
  584. static int smsc95xx_recv(struct eth_device *eth)
  585. {
  586. struct ueth_data *dev = (struct ueth_data *)eth->priv;
  587. DEFINE_CACHE_ALIGN_BUFFER(unsigned char, recv_buf, AX_RX_URB_SIZE);
  588. unsigned char *buf_ptr;
  589. int err;
  590. int actual_len;
  591. u32 packet_len;
  592. int cur_buf_align;
  593. debug("** %s()\n", __func__);
  594. err = usb_bulk_msg(dev->pusb_dev,
  595. usb_rcvbulkpipe(dev->pusb_dev, dev->ep_in),
  596. (void *)recv_buf,
  597. AX_RX_URB_SIZE,
  598. &actual_len,
  599. USB_BULK_RECV_TIMEOUT);
  600. debug("Rx: len = %u, actual = %u, err = %d\n", AX_RX_URB_SIZE,
  601. actual_len, err);
  602. if (err != 0) {
  603. debug("Rx: failed to receive\n");
  604. return -1;
  605. }
  606. if (actual_len > AX_RX_URB_SIZE) {
  607. debug("Rx: received too many bytes %d\n", actual_len);
  608. return -1;
  609. }
  610. buf_ptr = recv_buf;
  611. while (actual_len > 0) {
  612. /*
  613. * 1st 4 bytes contain the length of the actual data plus error
  614. * info. Extract data length.
  615. */
  616. if (actual_len < sizeof(packet_len)) {
  617. debug("Rx: incomplete packet length\n");
  618. return -1;
  619. }
  620. memcpy(&packet_len, buf_ptr, sizeof(packet_len));
  621. le32_to_cpus(&packet_len);
  622. if (packet_len & RX_STS_ES_) {
  623. debug("Rx: Error header=%#x", packet_len);
  624. return -1;
  625. }
  626. packet_len = ((packet_len & RX_STS_FL_) >> 16);
  627. if (packet_len > actual_len - sizeof(packet_len)) {
  628. debug("Rx: too large packet: %d\n", packet_len);
  629. return -1;
  630. }
  631. /* Notify net stack */
  632. NetReceive(buf_ptr + sizeof(packet_len), packet_len - 4);
  633. /* Adjust for next iteration */
  634. actual_len -= sizeof(packet_len) + packet_len;
  635. buf_ptr += sizeof(packet_len) + packet_len;
  636. cur_buf_align = (int)buf_ptr - (int)recv_buf;
  637. if (cur_buf_align & 0x03) {
  638. int align = 4 - (cur_buf_align & 0x03);
  639. actual_len -= align;
  640. buf_ptr += align;
  641. }
  642. }
  643. return err;
  644. }
  645. static void smsc95xx_halt(struct eth_device *eth)
  646. {
  647. debug("** %s()\n", __func__);
  648. }
  649. /*
  650. * SMSC probing functions
  651. */
  652. void smsc95xx_eth_before_probe(void)
  653. {
  654. curr_eth_dev = 0;
  655. }
  656. struct smsc95xx_dongle {
  657. unsigned short vendor;
  658. unsigned short product;
  659. };
  660. static const struct smsc95xx_dongle smsc95xx_dongles[] = {
  661. { 0x0424, 0xec00 }, /* LAN9512/LAN9514 Ethernet */
  662. { 0x0424, 0x9500 }, /* LAN9500 Ethernet */
  663. { 0x0000, 0x0000 } /* END - Do not remove */
  664. };
  665. /* Probe to see if a new device is actually an SMSC device */
  666. int smsc95xx_eth_probe(struct usb_device *dev, unsigned int ifnum,
  667. struct ueth_data *ss)
  668. {
  669. struct usb_interface *iface;
  670. struct usb_interface_descriptor *iface_desc;
  671. int i;
  672. /* let's examine the device now */
  673. iface = &dev->config.if_desc[ifnum];
  674. iface_desc = &dev->config.if_desc[ifnum].desc;
  675. for (i = 0; smsc95xx_dongles[i].vendor != 0; i++) {
  676. if (dev->descriptor.idVendor == smsc95xx_dongles[i].vendor &&
  677. dev->descriptor.idProduct == smsc95xx_dongles[i].product)
  678. /* Found a supported dongle */
  679. break;
  680. }
  681. if (smsc95xx_dongles[i].vendor == 0)
  682. return 0;
  683. /* At this point, we know we've got a live one */
  684. debug("\n\nUSB Ethernet device detected\n");
  685. memset(ss, '\0', sizeof(struct ueth_data));
  686. /* Initialize the ueth_data structure with some useful info */
  687. ss->ifnum = ifnum;
  688. ss->pusb_dev = dev;
  689. ss->subclass = iface_desc->bInterfaceSubClass;
  690. ss->protocol = iface_desc->bInterfaceProtocol;
  691. /*
  692. * We are expecting a minimum of 3 endpoints - in, out (bulk), and int.
  693. * We will ignore any others.
  694. */
  695. for (i = 0; i < iface_desc->bNumEndpoints; i++) {
  696. /* is it an BULK endpoint? */
  697. if ((iface->ep_desc[i].bmAttributes &
  698. USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) {
  699. if (iface->ep_desc[i].bEndpointAddress & USB_DIR_IN)
  700. ss->ep_in =
  701. iface->ep_desc[i].bEndpointAddress &
  702. USB_ENDPOINT_NUMBER_MASK;
  703. else
  704. ss->ep_out =
  705. iface->ep_desc[i].bEndpointAddress &
  706. USB_ENDPOINT_NUMBER_MASK;
  707. }
  708. /* is it an interrupt endpoint? */
  709. if ((iface->ep_desc[i].bmAttributes &
  710. USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) {
  711. ss->ep_int = iface->ep_desc[i].bEndpointAddress &
  712. USB_ENDPOINT_NUMBER_MASK;
  713. ss->irqinterval = iface->ep_desc[i].bInterval;
  714. }
  715. }
  716. debug("Endpoints In %d Out %d Int %d\n",
  717. ss->ep_in, ss->ep_out, ss->ep_int);
  718. /* Do some basic sanity checks, and bail if we find a problem */
  719. if (usb_set_interface(dev, iface_desc->bInterfaceNumber, 0) ||
  720. !ss->ep_in || !ss->ep_out || !ss->ep_int) {
  721. debug("Problems with device\n");
  722. return 0;
  723. }
  724. dev->privptr = (void *)ss;
  725. /* alloc driver private */
  726. ss->dev_priv = calloc(1, sizeof(struct smsc95xx_private));
  727. if (!ss->dev_priv)
  728. return 0;
  729. return 1;
  730. }
  731. int smsc95xx_eth_get_info(struct usb_device *dev, struct ueth_data *ss,
  732. struct eth_device *eth)
  733. {
  734. debug("** %s()\n", __func__);
  735. if (!eth) {
  736. debug("%s: missing parameter.\n", __func__);
  737. return 0;
  738. }
  739. sprintf(eth->name, "%s%d", SMSC95XX_BASE_NAME, curr_eth_dev++);
  740. eth->init = smsc95xx_init;
  741. eth->send = smsc95xx_send;
  742. eth->recv = smsc95xx_recv;
  743. eth->halt = smsc95xx_halt;
  744. eth->write_hwaddr = smsc95xx_write_hwaddr;
  745. eth->priv = ss;
  746. return 1;
  747. }