asix.c 11 KB

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