sr9700.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /*
  2. * CoreChip-sz SR9700 one chip USB 1.1 Ethernet Devices
  3. *
  4. * Author : Liu Junliang <liujunliang_ljl@163.com>
  5. *
  6. * Based on dm9601.c
  7. *
  8. * This file is licensed under the terms of the GNU General Public License
  9. * version 2. This program is licensed "as is" without any warranty of any
  10. * kind, whether express or implied.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/sched.h>
  14. #include <linux/stddef.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/etherdevice.h>
  17. #include <linux/ethtool.h>
  18. #include <linux/mii.h>
  19. #include <linux/usb.h>
  20. #include <linux/crc32.h>
  21. #include <linux/usb/usbnet.h>
  22. #include "sr9700.h"
  23. static int sr_read(struct usbnet *dev, u8 reg, u16 length, void *data)
  24. {
  25. int err;
  26. err = usbnet_read_cmd(dev, SR_RD_REGS, SR_REQ_RD_REG, 0, reg, data,
  27. length);
  28. if ((err != length) && (err >= 0))
  29. err = -EINVAL;
  30. return err;
  31. }
  32. static int sr_write(struct usbnet *dev, u8 reg, u16 length, void *data)
  33. {
  34. int err;
  35. err = usbnet_write_cmd(dev, SR_WR_REGS, SR_REQ_WR_REG, 0, reg, data,
  36. length);
  37. if ((err >= 0) && (err < length))
  38. err = -EINVAL;
  39. return err;
  40. }
  41. static int sr_read_reg(struct usbnet *dev, u8 reg, u8 *value)
  42. {
  43. return sr_read(dev, reg, 1, value);
  44. }
  45. static int sr_write_reg(struct usbnet *dev, u8 reg, u8 value)
  46. {
  47. return usbnet_write_cmd(dev, SR_WR_REGS, SR_REQ_WR_REG,
  48. value, reg, NULL, 0);
  49. }
  50. static void sr_write_async(struct usbnet *dev, u8 reg, u16 length, void *data)
  51. {
  52. usbnet_write_cmd_async(dev, SR_WR_REGS, SR_REQ_WR_REG,
  53. 0, reg, data, length);
  54. }
  55. static void sr_write_reg_async(struct usbnet *dev, u8 reg, u8 value)
  56. {
  57. usbnet_write_cmd_async(dev, SR_WR_REGS, SR_REQ_WR_REG,
  58. value, reg, NULL, 0);
  59. }
  60. static int wait_phy_eeprom_ready(struct usbnet *dev, int phy)
  61. {
  62. int i;
  63. for (i = 0; i < SR_SHARE_TIMEOUT; i++) {
  64. u8 tmp = 0;
  65. int ret;
  66. udelay(1);
  67. ret = sr_read_reg(dev, SR_EPCR, &tmp);
  68. if (ret < 0)
  69. return ret;
  70. /* ready */
  71. if (!(tmp & EPCR_ERRE))
  72. return 0;
  73. }
  74. netdev_err(dev->net, "%s write timed out!\n", phy ? "phy" : "eeprom");
  75. return -EIO;
  76. }
  77. static int sr_share_read_word(struct usbnet *dev, int phy, u8 reg,
  78. __le16 *value)
  79. {
  80. int ret;
  81. mutex_lock(&dev->phy_mutex);
  82. sr_write_reg(dev, SR_EPAR, phy ? (reg | EPAR_PHY_ADR) : reg);
  83. sr_write_reg(dev, SR_EPCR, phy ? (EPCR_EPOS | EPCR_ERPRR) : EPCR_ERPRR);
  84. ret = wait_phy_eeprom_ready(dev, phy);
  85. if (ret < 0)
  86. goto out_unlock;
  87. sr_write_reg(dev, SR_EPCR, 0x0);
  88. ret = sr_read(dev, SR_EPDR, 2, value);
  89. netdev_dbg(dev->net, "read shared %d 0x%02x returned 0x%04x, %d\n",
  90. phy, reg, *value, ret);
  91. out_unlock:
  92. mutex_unlock(&dev->phy_mutex);
  93. return ret;
  94. }
  95. static int sr_share_write_word(struct usbnet *dev, int phy, u8 reg,
  96. __le16 value)
  97. {
  98. int ret;
  99. mutex_lock(&dev->phy_mutex);
  100. ret = sr_write(dev, SR_EPDR, 2, &value);
  101. if (ret < 0)
  102. goto out_unlock;
  103. sr_write_reg(dev, SR_EPAR, phy ? (reg | EPAR_PHY_ADR) : reg);
  104. sr_write_reg(dev, SR_EPCR, phy ? (EPCR_WEP | EPCR_EPOS | EPCR_ERPRW) :
  105. (EPCR_WEP | EPCR_ERPRW));
  106. ret = wait_phy_eeprom_ready(dev, phy);
  107. if (ret < 0)
  108. goto out_unlock;
  109. sr_write_reg(dev, SR_EPCR, 0x0);
  110. out_unlock:
  111. mutex_unlock(&dev->phy_mutex);
  112. return ret;
  113. }
  114. static int sr_read_eeprom_word(struct usbnet *dev, u8 offset, void *value)
  115. {
  116. return sr_share_read_word(dev, 0, offset, value);
  117. }
  118. static int sr9700_get_eeprom_len(struct net_device *netdev)
  119. {
  120. return SR_EEPROM_LEN;
  121. }
  122. static int sr9700_get_eeprom(struct net_device *netdev,
  123. struct ethtool_eeprom *eeprom, u8 *data)
  124. {
  125. struct usbnet *dev = netdev_priv(netdev);
  126. __le16 *buf = (__le16 *)data;
  127. int ret = 0;
  128. int i;
  129. /* access is 16bit */
  130. if ((eeprom->offset & 0x01) || (eeprom->len & 0x01))
  131. return -EINVAL;
  132. for (i = 0; i < eeprom->len / 2; i++) {
  133. ret = sr_read_eeprom_word(dev, eeprom->offset / 2 + i, buf + i);
  134. if (ret < 0)
  135. break;
  136. }
  137. return ret;
  138. }
  139. static int sr_mdio_read(struct net_device *netdev, int phy_id, int loc)
  140. {
  141. struct usbnet *dev = netdev_priv(netdev);
  142. __le16 res;
  143. int rc = 0;
  144. if (phy_id) {
  145. netdev_dbg(netdev, "Only internal phy supported\n");
  146. return 0;
  147. }
  148. /* Access NSR_LINKST bit for link status instead of MII_BMSR */
  149. if (loc == MII_BMSR) {
  150. u8 value;
  151. sr_read_reg(dev, SR_NSR, &value);
  152. if (value & NSR_LINKST)
  153. rc = 1;
  154. }
  155. sr_share_read_word(dev, 1, loc, &res);
  156. if (rc == 1)
  157. res = le16_to_cpu(res) | BMSR_LSTATUS;
  158. else
  159. res = le16_to_cpu(res) & ~BMSR_LSTATUS;
  160. netdev_dbg(netdev, "sr_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
  161. phy_id, loc, res);
  162. return res;
  163. }
  164. static void sr_mdio_write(struct net_device *netdev, int phy_id, int loc,
  165. int val)
  166. {
  167. struct usbnet *dev = netdev_priv(netdev);
  168. __le16 res = cpu_to_le16(val);
  169. if (phy_id) {
  170. netdev_dbg(netdev, "Only internal phy supported\n");
  171. return;
  172. }
  173. netdev_dbg(netdev, "sr_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
  174. phy_id, loc, val);
  175. sr_share_write_word(dev, 1, loc, res);
  176. }
  177. static u32 sr9700_get_link(struct net_device *netdev)
  178. {
  179. struct usbnet *dev = netdev_priv(netdev);
  180. u8 value = 0;
  181. int rc = 0;
  182. /* Get the Link Status directly */
  183. sr_read_reg(dev, SR_NSR, &value);
  184. if (value & NSR_LINKST)
  185. rc = 1;
  186. return rc;
  187. }
  188. static int sr9700_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
  189. {
  190. struct usbnet *dev = netdev_priv(netdev);
  191. return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
  192. }
  193. static const struct ethtool_ops sr9700_ethtool_ops = {
  194. .get_drvinfo = usbnet_get_drvinfo,
  195. .get_link = sr9700_get_link,
  196. .get_msglevel = usbnet_get_msglevel,
  197. .set_msglevel = usbnet_set_msglevel,
  198. .get_eeprom_len = sr9700_get_eeprom_len,
  199. .get_eeprom = sr9700_get_eeprom,
  200. .get_settings = usbnet_get_settings,
  201. .set_settings = usbnet_set_settings,
  202. .nway_reset = usbnet_nway_reset,
  203. };
  204. static void sr9700_set_multicast(struct net_device *netdev)
  205. {
  206. struct usbnet *dev = netdev_priv(netdev);
  207. /* We use the 20 byte dev->data for our 8 byte filter buffer
  208. * to avoid allocating memory that is tricky to free later
  209. */
  210. u8 *hashes = (u8 *)&dev->data;
  211. /* rx_ctl setting : enable, disable_long, disable_crc */
  212. u8 rx_ctl = RCR_RXEN | RCR_DIS_CRC | RCR_DIS_LONG;
  213. memset(hashes, 0x00, SR_MCAST_SIZE);
  214. /* broadcast address */
  215. hashes[SR_MCAST_SIZE - 1] |= SR_MCAST_ADDR_FLAG;
  216. if (netdev->flags & IFF_PROMISC) {
  217. rx_ctl |= RCR_PRMSC;
  218. } else if (netdev->flags & IFF_ALLMULTI ||
  219. netdev_mc_count(netdev) > SR_MCAST_MAX) {
  220. rx_ctl |= RCR_RUNT;
  221. } else if (!netdev_mc_empty(netdev)) {
  222. struct netdev_hw_addr *ha;
  223. netdev_for_each_mc_addr(ha, netdev) {
  224. u32 crc = ether_crc(ETH_ALEN, ha->addr) >> 26;
  225. hashes[crc >> 3] |= 1 << (crc & 0x7);
  226. }
  227. }
  228. sr_write_async(dev, SR_MAR, SR_MCAST_SIZE, hashes);
  229. sr_write_reg_async(dev, SR_RCR, rx_ctl);
  230. }
  231. static int sr9700_set_mac_address(struct net_device *netdev, void *p)
  232. {
  233. struct usbnet *dev = netdev_priv(netdev);
  234. struct sockaddr *addr = p;
  235. if (!is_valid_ether_addr(addr->sa_data)) {
  236. netdev_err(netdev, "not setting invalid mac address %pM\n",
  237. addr->sa_data);
  238. return -EINVAL;
  239. }
  240. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  241. sr_write_async(dev, SR_PAR, 6, netdev->dev_addr);
  242. return 0;
  243. }
  244. static const struct net_device_ops sr9700_netdev_ops = {
  245. .ndo_open = usbnet_open,
  246. .ndo_stop = usbnet_stop,
  247. .ndo_start_xmit = usbnet_start_xmit,
  248. .ndo_tx_timeout = usbnet_tx_timeout,
  249. .ndo_change_mtu = usbnet_change_mtu,
  250. .ndo_validate_addr = eth_validate_addr,
  251. .ndo_do_ioctl = sr9700_ioctl,
  252. .ndo_set_rx_mode = sr9700_set_multicast,
  253. .ndo_set_mac_address = sr9700_set_mac_address,
  254. };
  255. static int sr9700_bind(struct usbnet *dev, struct usb_interface *intf)
  256. {
  257. struct net_device *netdev;
  258. struct mii_if_info *mii;
  259. int ret;
  260. ret = usbnet_get_endpoints(dev, intf);
  261. if (ret)
  262. goto out;
  263. netdev = dev->net;
  264. netdev->netdev_ops = &sr9700_netdev_ops;
  265. netdev->ethtool_ops = &sr9700_ethtool_ops;
  266. netdev->hard_header_len += SR_TX_OVERHEAD;
  267. dev->hard_mtu = netdev->mtu + netdev->hard_header_len;
  268. /* bulkin buffer is preferably not less than 3K */
  269. dev->rx_urb_size = 3072;
  270. mii = &dev->mii;
  271. mii->dev = netdev;
  272. mii->mdio_read = sr_mdio_read;
  273. mii->mdio_write = sr_mdio_write;
  274. mii->phy_id_mask = 0x1f;
  275. mii->reg_num_mask = 0x1f;
  276. sr_write_reg(dev, SR_NCR, NCR_RST);
  277. udelay(20);
  278. /* read MAC
  279. * After Chip Power on, the Chip will reload the MAC from
  280. * EEPROM automatically to PAR. In case there is no EEPROM externally,
  281. * a default MAC address is stored in PAR for making chip work properly.
  282. */
  283. if (sr_read(dev, SR_PAR, ETH_ALEN, netdev->dev_addr) < 0) {
  284. netdev_err(netdev, "Error reading MAC address\n");
  285. ret = -ENODEV;
  286. goto out;
  287. }
  288. /* power up and reset phy */
  289. sr_write_reg(dev, SR_PRR, PRR_PHY_RST);
  290. /* at least 10ms, here 20ms for safe */
  291. mdelay(20);
  292. sr_write_reg(dev, SR_PRR, 0);
  293. /* at least 1ms, here 2ms for reading right register */
  294. udelay(2 * 1000);
  295. /* receive broadcast packets */
  296. sr9700_set_multicast(netdev);
  297. sr_mdio_write(netdev, mii->phy_id, MII_BMCR, BMCR_RESET);
  298. sr_mdio_write(netdev, mii->phy_id, MII_ADVERTISE, ADVERTISE_ALL |
  299. ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
  300. mii_nway_restart(mii);
  301. out:
  302. return ret;
  303. }
  304. static int sr9700_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
  305. {
  306. struct sk_buff *sr_skb;
  307. int len;
  308. /* skb content (packets) format :
  309. * p0 p1 p2 ...... pm
  310. * / \
  311. * / \
  312. * / \
  313. * / \
  314. * p0b0 p0b1 p0b2 p0b3 ...... p0b(n-4) p0b(n-3)...p0bn
  315. *
  316. * p0 : packet 0
  317. * p0b0 : packet 0 byte 0
  318. *
  319. * b0: rx status
  320. * b1: packet length (incl crc) low
  321. * b2: packet length (incl crc) high
  322. * b3..n-4: packet data
  323. * bn-3..bn: ethernet packet crc
  324. */
  325. if (unlikely(skb->len < SR_RX_OVERHEAD)) {
  326. netdev_err(dev->net, "unexpected tiny rx frame\n");
  327. return 0;
  328. }
  329. /* one skb may contains multiple packets */
  330. while (skb->len > SR_RX_OVERHEAD) {
  331. if (skb->data[0] != 0x40)
  332. return 0;
  333. /* ignore the CRC length */
  334. len = (skb->data[1] | (skb->data[2] << 8)) - 4;
  335. if (len > ETH_FRAME_LEN)
  336. return 0;
  337. /* the last packet of current skb */
  338. if (skb->len == (len + SR_RX_OVERHEAD)) {
  339. skb_pull(skb, 3);
  340. skb->len = len;
  341. skb_set_tail_pointer(skb, len);
  342. skb->truesize = len + sizeof(struct sk_buff);
  343. return 2;
  344. }
  345. /* skb_clone is used for address align */
  346. sr_skb = skb_clone(skb, GFP_ATOMIC);
  347. if (!sr_skb)
  348. return 0;
  349. sr_skb->len = len;
  350. sr_skb->data = skb->data + 3;
  351. skb_set_tail_pointer(sr_skb, len);
  352. sr_skb->truesize = len + sizeof(struct sk_buff);
  353. usbnet_skb_return(dev, sr_skb);
  354. skb_pull(skb, len + SR_RX_OVERHEAD);
  355. };
  356. return 0;
  357. }
  358. static struct sk_buff *sr9700_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
  359. gfp_t flags)
  360. {
  361. int len;
  362. /* SR9700 can only send out one ethernet packet at once.
  363. *
  364. * b0 b1 b2 b3 ...... b(n-4) b(n-3)...bn
  365. *
  366. * b0: rx status
  367. * b1: packet length (incl crc) low
  368. * b2: packet length (incl crc) high
  369. * b3..n-4: packet data
  370. * bn-3..bn: ethernet packet crc
  371. */
  372. len = skb->len;
  373. if (skb_cow_head(skb, SR_TX_OVERHEAD)) {
  374. dev_kfree_skb_any(skb);
  375. return NULL;
  376. }
  377. __skb_push(skb, SR_TX_OVERHEAD);
  378. /* usbnet adds padding if length is a multiple of packet size
  379. * if so, adjust length value in header
  380. */
  381. if ((skb->len % dev->maxpacket) == 0)
  382. len++;
  383. skb->data[0] = len;
  384. skb->data[1] = len >> 8;
  385. return skb;
  386. }
  387. static void sr9700_status(struct usbnet *dev, struct urb *urb)
  388. {
  389. int link;
  390. u8 *buf;
  391. /* format:
  392. b0: net status
  393. b1: tx status 1
  394. b2: tx status 2
  395. b3: rx status
  396. b4: rx overflow
  397. b5: rx count
  398. b6: tx count
  399. b7: gpr
  400. */
  401. if (urb->actual_length < 8)
  402. return;
  403. buf = urb->transfer_buffer;
  404. link = !!(buf[0] & 0x40);
  405. if (netif_carrier_ok(dev->net) != link) {
  406. usbnet_link_change(dev, link, 1);
  407. netdev_dbg(dev->net, "Link Status is: %d\n", link);
  408. }
  409. }
  410. static int sr9700_link_reset(struct usbnet *dev)
  411. {
  412. struct ethtool_cmd ecmd;
  413. mii_check_media(&dev->mii, 1, 1);
  414. mii_ethtool_gset(&dev->mii, &ecmd);
  415. netdev_dbg(dev->net, "link_reset() speed: %d duplex: %d\n",
  416. ecmd.speed, ecmd.duplex);
  417. return 0;
  418. }
  419. static const struct driver_info sr9700_driver_info = {
  420. .description = "CoreChip SR9700 USB Ethernet",
  421. .flags = FLAG_ETHER,
  422. .bind = sr9700_bind,
  423. .rx_fixup = sr9700_rx_fixup,
  424. .tx_fixup = sr9700_tx_fixup,
  425. .status = sr9700_status,
  426. .link_reset = sr9700_link_reset,
  427. .reset = sr9700_link_reset,
  428. };
  429. static const struct usb_device_id products[] = {
  430. {
  431. USB_DEVICE(0x0fe6, 0x9700), /* SR9700 device */
  432. .driver_info = (unsigned long)&sr9700_driver_info,
  433. },
  434. {}, /* END */
  435. };
  436. MODULE_DEVICE_TABLE(usb, products);
  437. static struct usb_driver sr9700_usb_driver = {
  438. .name = "sr9700",
  439. .id_table = products,
  440. .probe = usbnet_probe,
  441. .disconnect = usbnet_disconnect,
  442. .suspend = usbnet_suspend,
  443. .resume = usbnet_resume,
  444. .disable_hub_initiated_lpm = 1,
  445. };
  446. module_usb_driver(sr9700_usb_driver);
  447. MODULE_AUTHOR("liujl <liujunliang_ljl@163.com>");
  448. MODULE_DESCRIPTION("SR9700 one chip USB 1.1 USB to Ethernet device from http://www.corechip-sz.com/");
  449. MODULE_LICENSE("GPL");