asix.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /* ASIX AX88772 USB 2.0 Ethernet driver.
  2. *
  3. * The AX88772 is a vendor-specific USB device with one bulk IN and one bulk
  4. * OUT endpoint. Configuration, the embedded PHY and the MAC are reached with
  5. * the ASIX vendor control requests; the receive path prepends a 4-byte header
  6. * (length in the low 16 bits and its complement in the high 16 bits), the
  7. * transmit path expects the same header before the Ethernet frame.
  8. *
  9. * The USB core's bulk API is start-or-poll: usb_bulk_in() arms on the first
  10. * call and leaves the transfer disarmed after delivering a chunk, so the
  11. * RX buffer stays stable while its (possibly several) frames are consumed. */
  12. #include "asix.h"
  13. #include "io.h"
  14. #include "netdev.h"
  15. #include "timer.h"
  16. #include "usb.h"
  17. /* ASIX vendor requests. */
  18. #define AX_CMD_SET_SW_MII 0x06
  19. #define AX_CMD_READ_MII_REG 0x07
  20. #define AX_CMD_WRITE_MII_REG 0x08
  21. #define AX_CMD_SET_HW_MII 0x0A
  22. #define AX_CMD_READ_RX_CTL 0x0F
  23. #define AX_CMD_WRITE_RX_CTL 0x10
  24. #define AX_CMD_WRITE_IPG0 0x12
  25. #define AX_CMD_READ_NODE_ID 0x13
  26. #define AX_CMD_WRITE_NODE_ID 0x14
  27. #define AX_CMD_READ_PHY_ID 0x19
  28. #define AX_CMD_WRITE_MEDIUM_MODE 0x1B
  29. #define AX_CMD_WRITE_GPIOS 0x1F
  30. #define AX_CMD_SW_RESET 0x20
  31. #define AX_CMD_SW_PHY_SELECT 0x22
  32. #define AX_GPIO_RSE 0x80
  33. #define AX_GPIO_GPO_2 0x20
  34. #define AX_GPIO_GPO2EN 0x10
  35. #define AX_SWRESET_CLEAR 0x00
  36. #define AX_SWRESET_PRTE 0x04
  37. #define AX_SWRESET_PRL 0x08
  38. #define AX_SWRESET_IPRL 0x20
  39. #define AX_SWRESET_IPPD 0x40
  40. #define AX88772_IPG0_DEFAULT 0x15
  41. #define AX88772_IPG1_DEFAULT 0x0C
  42. #define AX88772_IPG2_DEFAULT 0x12
  43. #define AX_MEDIUM_FD 0x0002
  44. #define AX_MEDIUM_AC 0x0004
  45. #define AX_MEDIUM_RFC 0x0010
  46. #define AX_MEDIUM_TFC 0x0020
  47. #define AX_MEDIUM_RE 0x0100
  48. #define AX_MEDIUM_PS 0x0200
  49. #define AX88772_MEDIUM_DEFAULT (AX_MEDIUM_FD | AX_MEDIUM_RFC | \
  50. AX_MEDIUM_TFC | AX_MEDIUM_PS | \
  51. AX_MEDIUM_AC | AX_MEDIUM_RE)
  52. #define AX_RX_CTL_SO 0x0080
  53. #define AX_RX_CTL_AB 0x0008
  54. #define AX_DEFAULT_RX_CTL (AX_RX_CTL_SO | AX_RX_CTL_AB)
  55. #define MII_BMCR 0
  56. #define MII_BMSR 1
  57. #define MII_ADVERTISE 4
  58. #define BMCR_RESET 0x8000
  59. #define BMCR_ANENABLE 0x1000
  60. #define BMCR_ANRESTART 0x0200
  61. #define BMSR_LSTATUS 0x0004
  62. #define ADVERTISE_ALL 0x01E0
  63. #define ADVERTISE_CSMA 0x0001
  64. #define ASIX_RX_MAX 2048
  65. #define ASIX_TX_MAX (1514 + 4)
  66. struct asix_dev {
  67. int present;
  68. int chip_ready;
  69. int link;
  70. unsigned int ctrl;
  71. unsigned int addr;
  72. unsigned int mps;
  73. unsigned int port;
  74. int low;
  75. unsigned int ep_in, ep_in_mps;
  76. unsigned int ep_out, ep_out_mps;
  77. unsigned int phy;
  78. unsigned char mac[6];
  79. };
  80. static struct asix_dev dev;
  81. static unsigned char rx_buf[ASIX_RX_MAX] __attribute__((aligned(64)));
  82. static unsigned char tx_buf[ASIX_TX_MAX] __attribute__((aligned(64)));
  83. static unsigned int rx_pos, rx_end;
  84. static void delay_ms(unsigned int ms)
  85. {
  86. unsigned int t0 = get_ticks_ms();
  87. while (get_ticks_ms() - t0 < ms)
  88. ;
  89. }
  90. static int read_cmd(unsigned char cmd, unsigned short value,
  91. unsigned short index, void *buf, unsigned int len)
  92. {
  93. int n = usb_control_in(dev.ctrl, dev.addr, dev.mps, dev.low,
  94. 0xC0, cmd, value, index, (unsigned char *)buf,
  95. len);
  96. return n == (int)len ? 0 : -1;
  97. }
  98. static int write_cmd(unsigned char cmd, unsigned short value,
  99. unsigned short index, const void *buf, unsigned int len)
  100. {
  101. if (len) {
  102. int n = usb_control_out_data(dev.ctrl, dev.addr, dev.mps,
  103. dev.low, 0x40, cmd, value, index,
  104. (const unsigned char *)buf, len);
  105. return n < 0 ? -1 : 0;
  106. }
  107. return usb_control_out(dev.ctrl, dev.addr, dev.mps, dev.low,
  108. 0x40, cmd, value, index) < 0 ? -1 : 0;
  109. }
  110. static int mdio_read(unsigned int loc)
  111. {
  112. unsigned char b[2];
  113. if (write_cmd(AX_CMD_SET_SW_MII, 0, 0, 0, 0) < 0)
  114. return -1;
  115. if (read_cmd(AX_CMD_READ_MII_REG, (unsigned short)dev.phy,
  116. (unsigned short)loc, b, 2) < 0) {
  117. write_cmd(AX_CMD_SET_HW_MII, 0, 0, 0, 0);
  118. return -1;
  119. }
  120. write_cmd(AX_CMD_SET_HW_MII, 0, 0, 0, 0);
  121. return (int)((unsigned int)b[0] | ((unsigned int)b[1] << 8));
  122. }
  123. static void mdio_write(unsigned int loc, unsigned int val)
  124. {
  125. unsigned char b[2];
  126. b[0] = (unsigned char)(val & 0xFF);
  127. b[1] = (unsigned char)((val >> 8) & 0xFF);
  128. write_cmd(AX_CMD_SET_SW_MII, 0, 0, 0, 0);
  129. write_cmd(AX_CMD_WRITE_MII_REG, (unsigned short)dev.phy,
  130. (unsigned short)loc, b, 2);
  131. write_cmd(AX_CMD_SET_HW_MII, 0, 0, 0, 0);
  132. }
  133. static int chip_init(void)
  134. {
  135. unsigned char b[2];
  136. unsigned int phy, rx_ctl, i;
  137. int bmcr;
  138. dev.chip_ready = 0;
  139. dev.link = 0;
  140. if (write_cmd(AX_CMD_WRITE_GPIOS,
  141. AX_GPIO_RSE | AX_GPIO_GPO_2 | AX_GPIO_GPO2EN,
  142. 0, 0, 0) < 0)
  143. return -1;
  144. delay_ms(5);
  145. if (read_cmd(AX_CMD_READ_PHY_ID, 0, 0, b, 2) < 0)
  146. return -1;
  147. phy = b[1];
  148. dev.phy = phy;
  149. if (write_cmd(AX_CMD_SW_PHY_SELECT,
  150. (phy & 0x1F) == 0x10 ? 1 : 0, 0, 0, 0) < 0)
  151. return -1;
  152. if (write_cmd(AX_CMD_SW_RESET, AX_SWRESET_IPPD | AX_SWRESET_PRL,
  153. 0, 0, 0) < 0)
  154. return -1;
  155. delay_ms(150);
  156. if (write_cmd(AX_CMD_SW_RESET, AX_SWRESET_CLEAR, 0, 0, 0) < 0)
  157. return -1;
  158. delay_ms(150);
  159. if ((phy & 0x1F) == 0x10)
  160. write_cmd(AX_CMD_SW_RESET, AX_SWRESET_IPRL, 0, 0, 0);
  161. else
  162. write_cmd(AX_CMD_SW_RESET, AX_SWRESET_PRTE, 0, 0, 0);
  163. delay_ms(150);
  164. if (read_cmd(AX_CMD_READ_RX_CTL, 0, 0, b, 2) < 0)
  165. return -1;
  166. rx_ctl = (unsigned int)b[0] | ((unsigned int)b[1] << 8);
  167. (void)rx_ctl;
  168. if (write_cmd(AX_CMD_WRITE_RX_CTL, 0, 0, 0, 0) < 0)
  169. return -1;
  170. if (read_cmd(AX_CMD_READ_NODE_ID, 0, 0, dev.mac, 6) < 0)
  171. return -1;
  172. /* PHY reset and 10/100 autonegotiation. */
  173. mdio_write(MII_BMCR, BMCR_RESET);
  174. delay_ms(2);
  175. mdio_write(MII_ADVERTISE, ADVERTISE_ALL | ADVERTISE_CSMA);
  176. bmcr = mdio_read(MII_BMCR);
  177. if (bmcr >= 0 && (bmcr & BMCR_ANENABLE))
  178. mdio_write(MII_BMCR, (unsigned int)bmcr | BMCR_ANRESTART);
  179. if (write_cmd(AX_CMD_WRITE_MEDIUM_MODE, AX88772_MEDIUM_DEFAULT,
  180. 0, 0, 0) < 0)
  181. return -1;
  182. if (write_cmd(AX_CMD_WRITE_IPG0,
  183. AX88772_IPG0_DEFAULT | AX88772_IPG1_DEFAULT,
  184. AX88772_IPG2_DEFAULT, 0, 0) < 0)
  185. return -1;
  186. if (write_cmd(AX_CMD_WRITE_RX_CTL, AX_DEFAULT_RX_CTL,
  187. 0, 0, 0) < 0)
  188. return -1;
  189. if (write_cmd(AX_CMD_WRITE_NODE_ID, 0, 0, dev.mac, 6) < 0)
  190. return -1;
  191. /* Wait for the PHY link, but do not fail the NIC if no cable is
  192. * plugged in yet: dhcp/netinfo can retry later. */
  193. for (i = 0; i < 60; i++) {
  194. int st = mdio_read(MII_BMSR);
  195. if (st >= 0 && (st & BMSR_LSTATUS)) {
  196. dev.link = 1;
  197. break;
  198. }
  199. delay_ms(50);
  200. }
  201. dev.chip_ready = 1;
  202. return 0;
  203. }
  204. static void put_mac(void)
  205. {
  206. unsigned int i;
  207. for (i = 0; i < 6; i++) {
  208. static const char hex[] = "0123456789ABCDEF";
  209. if (i)
  210. io_putchar(':');
  211. io_putchar(hex[(dev.mac[i] >> 4) & 0xF]);
  212. io_putchar(hex[dev.mac[i] & 0xF]);
  213. }
  214. }
  215. static int net_init(void)
  216. {
  217. if (!dev.present)
  218. return -1;
  219. if (!dev.chip_ready && chip_init() < 0)
  220. return -1;
  221. return 0;
  222. }
  223. static void net_get_mac(unsigned char mac[6])
  224. {
  225. unsigned int i;
  226. for (i = 0; i < 6; i++)
  227. mac[i] = dev.mac[i];
  228. }
  229. static int net_rx(unsigned char *buf, unsigned int maxlen)
  230. {
  231. unsigned int hdr, len, i;
  232. int n;
  233. if (!dev.present)
  234. return -1;
  235. for (;;) {
  236. if (rx_pos < rx_end) {
  237. if (rx_pos + 4 > rx_end) {
  238. rx_pos = rx_end;
  239. return -1;
  240. }
  241. hdr = (unsigned int)rx_buf[rx_pos] |
  242. ((unsigned int)rx_buf[rx_pos + 1] << 8) |
  243. ((unsigned int)rx_buf[rx_pos + 2] << 16) |
  244. ((unsigned int)rx_buf[rx_pos + 3] << 24);
  245. len = hdr & 0x7FF;
  246. if (((~hdr >> 16) & 0x7FF) != len ||
  247. len < 14 || len > maxlen ||
  248. rx_pos + 4 + len > rx_end) {
  249. rx_pos = rx_end;
  250. return -1;
  251. }
  252. for (i = 0; i < len; i++)
  253. buf[i] = rx_buf[rx_pos + 4 + i];
  254. rx_pos += 4 + ((len + 1) & ~1u);
  255. return (int)len;
  256. }
  257. n = usb_bulk_in(dev.ctrl, dev.addr, dev.ep_in,
  258. dev.ep_in_mps, rx_buf, ASIX_RX_MAX);
  259. if (n <= 0)
  260. return -1;
  261. rx_pos = 0;
  262. rx_end = (unsigned int)n;
  263. }
  264. }
  265. static int net_tx(const unsigned char *buf, unsigned int len)
  266. {
  267. unsigned int hdr;
  268. if (!dev.present || len > 1514)
  269. return -1;
  270. hdr = len | ((~len & 0xFFFF) << 16);
  271. tx_buf[0] = (unsigned char)(hdr & 0xFF);
  272. tx_buf[1] = (unsigned char)((hdr >> 8) & 0xFF);
  273. tx_buf[2] = (unsigned char)((hdr >> 16) & 0xFF);
  274. tx_buf[3] = (unsigned char)((hdr >> 24) & 0xFF);
  275. {
  276. unsigned int i;
  277. for (i = 0; i < len; i++)
  278. tx_buf[4 + i] = buf[i];
  279. }
  280. return usb_bulk_out(dev.ctrl, dev.addr, dev.ep_out,
  281. dev.ep_out_mps, tx_buf, len + 4) == 0 ? 0 : -1;
  282. }
  283. static void net_print_info(void)
  284. {
  285. int st;
  286. io_putstr("ASIX AX88772 USB 2.0 Ethernet\n");
  287. io_putstr(" MAC ");
  288. put_mac();
  289. io_putstr("\n");
  290. st = dev.phy ? mdio_read(MII_BMSR) : -1;
  291. if (st >= 0 && (st & BMSR_LSTATUS))
  292. io_putstr(" link up, 100 Mbit capable\n");
  293. else
  294. io_putstr(" link down\n");
  295. }
  296. static void net_reset(void)
  297. {
  298. rx_pos = 0;
  299. rx_end = 0;
  300. dev.chip_ready = 0;
  301. if (dev.present)
  302. chip_init();
  303. }
  304. static const unsigned char asix_default_ip[4] = { 192, 168, 50, 2 };
  305. static const unsigned char asix_default_mask[4] = { 255, 255, 255, 0 };
  306. const struct netdev asix_netdev = {
  307. "asix",
  308. "AX88772 USB 2.0 Ethernet",
  309. net_init,
  310. net_get_mac,
  311. net_rx,
  312. net_tx,
  313. net_print_info,
  314. 0,
  315. 0,
  316. net_reset,
  317. 0,
  318. 0,
  319. asix_default_ip,
  320. asix_default_mask,
  321. 0,
  322. 0,
  323. };
  324. /* Read and parse the configuration descriptor. The AX88772 has one
  325. * vendor-specific interface with a bulk IN and a bulk OUT endpoint. */
  326. static int asix_config(void)
  327. {
  328. unsigned char cfg[128];
  329. unsigned int total, i, config_value = 0;
  330. unsigned int ep_in = 0, ep_out = 0, ep_in_mps = 0, ep_out_mps = 0;
  331. if (usb_control_in(dev.ctrl, dev.addr, dev.mps, dev.low,
  332. 0x80, 0x06, 0x0200, 0, cfg, 9) < 0)
  333. return -1;
  334. total = (unsigned int)cfg[2] | ((unsigned int)cfg[3] << 8);
  335. if (total < 9)
  336. return -1;
  337. if (total > sizeof(cfg))
  338. total = sizeof(cfg);
  339. if (usb_control_in(dev.ctrl, dev.addr, dev.mps, dev.low,
  340. 0x80, 0x06, 0x0200, 0, cfg, total) < 0)
  341. return -1;
  342. config_value = cfg[5];
  343. i = 0;
  344. while (i + 2 <= total) {
  345. unsigned int blen = cfg[i];
  346. unsigned int btype = cfg[i + 1];
  347. if (blen < 2 || i + blen > total)
  348. break;
  349. if (btype == 0x05 && blen >= 7) {
  350. unsigned int epa = cfg[i + 2];
  351. unsigned int attr = cfg[i + 3] & 3u;
  352. unsigned int mps = ((unsigned int)cfg[i + 4] |
  353. ((unsigned int)cfg[i + 5] << 8)) &
  354. 0x7FFu;
  355. if (attr == 2) {
  356. if (epa & 0x80) {
  357. ep_in = epa & 0x0F;
  358. ep_in_mps = mps;
  359. } else {
  360. ep_out = epa & 0x0F;
  361. ep_out_mps = mps;
  362. }
  363. }
  364. }
  365. i += blen;
  366. }
  367. if (!ep_in || !ep_out || !config_value)
  368. return -1;
  369. if (usb_control_out(dev.ctrl, dev.addr, dev.mps, dev.low,
  370. 0x00, 0x09, (unsigned short)config_value, 0) < 0)
  371. return -1;
  372. dev.ep_in = ep_in;
  373. dev.ep_in_mps = ep_in_mps ? ep_in_mps : 64;
  374. dev.ep_out = ep_out;
  375. dev.ep_out_mps = ep_out_mps ? ep_out_mps : 64;
  376. return 0;
  377. }
  378. static int device_probe(unsigned int ctrl, unsigned int addr,
  379. unsigned int mps, int low_speed, unsigned int port,
  380. unsigned int vendor, unsigned int product)
  381. {
  382. if (vendor != 0x0B95 || (product != 0x7720 && product != 0x772A &&
  383. product != 0x772B))
  384. return 0;
  385. dev.ctrl = ctrl;
  386. dev.addr = addr;
  387. dev.mps = mps;
  388. dev.low = low_speed;
  389. dev.port = port;
  390. dev.present = 1;
  391. dev.chip_ready = 0;
  392. rx_pos = rx_end = 0;
  393. if (asix_config() < 0) {
  394. dev.present = 0;
  395. return 0;
  396. }
  397. if (chip_init() < 0) {
  398. dev.present = 0;
  399. return 0;
  400. }
  401. netdev_hotplug();
  402. return 1;
  403. }
  404. void asix_register(void)
  405. {
  406. usb_register_device_probe(device_probe);
  407. }