drv_smc911x.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. #include <board.h>
  2. #include <rtthread.h>
  3. #include <netif/ethernetif.h>
  4. #include <lwipopts.h>
  5. #define MAX_ADDR_LEN 6
  6. #define SMC911X_EMAC_DEVICE(eth) (struct eth_device_smc911x*)(eth)
  7. #include "drv_smc911x.h"
  8. #define DRIVERNAME "EMAC"
  9. struct eth_device_smc911x
  10. {
  11. /* inherit from Ethernet device */
  12. struct eth_device parent;
  13. /* interface address info. */
  14. rt_uint8_t enetaddr[MAX_ADDR_LEN]; /* MAC address */
  15. uint32_t iobase;
  16. uint32_t irqno;
  17. };
  18. static struct eth_device_smc911x _emac;
  19. int udelay(int value)
  20. {
  21. return 0;
  22. }
  23. int mdelay(int value)
  24. {
  25. return 0;
  26. }
  27. #if defined (CONFIG_SMC911X_32_BIT)
  28. rt_inline uint32_t smc911x_reg_read(struct eth_device_smc911x *dev, uint32_t offset)
  29. {
  30. return *(volatile uint32_t*)(dev->iobase + offset);
  31. }
  32. rt_inline void smc911x_reg_write(struct eth_device_smc911x *dev, uint32_t offset, uint32_t val)
  33. {
  34. *(volatile uint32_t*)(dev->iobase + offset) = val;
  35. }
  36. #elif defined (CONFIG_SMC911X_16_BIT)
  37. rt_inline uint32_t smc911x_reg_read(struct eth_device_smc911x *dev, uint32_t offset)
  38. {
  39. volatile uint16_t *addr_16 = (uint16_t *)(dev->iobase + offset);
  40. return ((*addr_16 & 0x0000ffff) | (*(addr_16 + 1) << 16));
  41. }
  42. rt_inline void smc911x_reg_write(struct eth_device_smc911x *dev, uint32_t offset, uint32_t val)
  43. {
  44. *(volatile uint16_t *)(dev->iobase + offset) = (uint16_t)val;
  45. *(volatile uint16_t *)(dev->iobase + offset + 2) = (uint16_t)(val >> 16);
  46. }
  47. #else
  48. #error "SMC911X: undefined bus width"
  49. #endif /* CONFIG_SMC911X_16_BIT */
  50. struct chip_id
  51. {
  52. uint16_t id;
  53. char *name;
  54. };
  55. static const struct chip_id chip_ids[] =
  56. {
  57. { CHIP_89218,"LAN89218" },
  58. { CHIP_9115, "LAN9115" },
  59. { CHIP_9116, "LAN9116" },
  60. { CHIP_9117, "LAN9117" },
  61. { CHIP_9118, "LAN9118" },
  62. { CHIP_9211, "LAN9211" },
  63. { CHIP_9215, "LAN9215" },
  64. { CHIP_9216, "LAN9216" },
  65. { CHIP_9217, "LAN9217" },
  66. { CHIP_9218, "LAN9218" },
  67. { CHIP_9220, "LAN9220" },
  68. { CHIP_9221, "LAN9221" },
  69. { 0, RT_NULL },
  70. };
  71. static uint32_t smc911x_get_mac_csr(struct eth_device_smc911x *dev, uint8_t reg)
  72. {
  73. while (smc911x_reg_read(dev, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY) ;
  74. smc911x_reg_write(dev, MAC_CSR_CMD, MAC_CSR_CMD_CSR_BUSY | MAC_CSR_CMD_R_NOT_W | reg);
  75. while (smc911x_reg_read(dev, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY) ;
  76. return smc911x_reg_read(dev, MAC_CSR_DATA);
  77. }
  78. static void smc911x_set_mac_csr(struct eth_device_smc911x *dev, uint8_t reg, uint32_t data)
  79. {
  80. while (smc911x_reg_read(dev, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY) ;
  81. smc911x_reg_write(dev, MAC_CSR_DATA, data);
  82. smc911x_reg_write(dev, MAC_CSR_CMD, MAC_CSR_CMD_CSR_BUSY | reg);
  83. while (smc911x_reg_read(dev, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY) ;
  84. }
  85. static int smc911x_detect_chip(struct eth_device_smc911x *dev)
  86. {
  87. unsigned long val, i;
  88. val = smc911x_reg_read(dev, BYTE_TEST);
  89. if (val == 0xffffffff)
  90. {
  91. /* Special case -- no chip present */
  92. return -1;
  93. }
  94. else if (val != 0x87654321)
  95. {
  96. rt_kprintf(DRIVERNAME ": Invalid chip endian 0x%08lx\n", val);
  97. return -1;
  98. }
  99. val = smc911x_reg_read(dev, ID_REV) >> 16;
  100. for (i = 0; chip_ids[i].id != 0; i++)
  101. {
  102. if (chip_ids[i].id == val) break;
  103. }
  104. if (!chip_ids[i].id)
  105. {
  106. rt_kprintf(DRIVERNAME ": Unknown chip ID %04lx\n", val);
  107. return -1;
  108. }
  109. return 0;
  110. }
  111. static void smc911x_reset(struct eth_device_smc911x *dev)
  112. {
  113. int timeout;
  114. /*
  115. * Take out of PM setting first
  116. * Device is already wake up if PMT_CTRL_READY bit is set
  117. */
  118. if ((smc911x_reg_read(dev, PMT_CTRL) & PMT_CTRL_READY) == 0)
  119. {
  120. /* Write to the bytetest will take out of powerdown */
  121. smc911x_reg_write(dev, BYTE_TEST, 0x0);
  122. timeout = 10;
  123. while (timeout-- && !(smc911x_reg_read(dev, PMT_CTRL) & PMT_CTRL_READY))
  124. udelay(10);
  125. if (timeout < 0)
  126. {
  127. rt_kprintf(DRIVERNAME
  128. ": timeout waiting for PM restore\n");
  129. return;
  130. }
  131. }
  132. /* Disable interrupts */
  133. smc911x_reg_write(dev, INT_EN, 0);
  134. smc911x_reg_write(dev, HW_CFG, HW_CFG_SRST);
  135. timeout = 1000;
  136. while (timeout-- && smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY)
  137. udelay(10);
  138. if (timeout < 0)
  139. {
  140. rt_kprintf(DRIVERNAME ": reset timeout\n");
  141. return;
  142. }
  143. /* Reset the FIFO level and flow control settings */
  144. smc911x_set_mac_csr(dev, FLOW, FLOW_FCPT | FLOW_FCEN);
  145. smc911x_reg_write(dev, AFC_CFG, 0x0050287F);
  146. /* Set to LED outputs */
  147. smc911x_reg_write(dev, GPIO_CFG, 0x70070000);
  148. }
  149. static void smc911x_handle_mac_address(struct eth_device_smc911x *dev)
  150. {
  151. unsigned long addrh, addrl;
  152. uint8_t *m = dev->enetaddr;
  153. addrl = m[0] | (m[1] << 8) | (m[2] << 16) | (m[3] << 24);
  154. addrh = m[4] | (m[5] << 8);
  155. smc911x_set_mac_csr(dev, ADDRL, addrl);
  156. smc911x_set_mac_csr(dev, ADDRH, addrh);
  157. }
  158. static int smc911x_eth_phy_read(struct eth_device_smc911x *dev,
  159. uint8_t phy, uint8_t reg, uint16_t *val)
  160. {
  161. while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY) ;
  162. smc911x_set_mac_csr(dev, MII_ACC, phy << 11 | reg << 6 | MII_ACC_MII_BUSY);
  163. while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY) ;
  164. *val = smc911x_get_mac_csr(dev, MII_DATA);
  165. return 0;
  166. }
  167. static int smc911x_eth_phy_write(struct eth_device_smc911x *dev,
  168. uint8_t phy, uint8_t reg, uint16_t val)
  169. {
  170. while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY)
  171. ;
  172. smc911x_set_mac_csr(dev, MII_DATA, val);
  173. smc911x_set_mac_csr(dev, MII_ACC,
  174. phy << 11 | reg << 6 | MII_ACC_MII_BUSY | MII_ACC_MII_WRITE);
  175. while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY)
  176. ;
  177. return 0;
  178. }
  179. static int smc911x_phy_reset(struct eth_device_smc911x *dev)
  180. {
  181. uint32_t reg;
  182. reg = smc911x_reg_read(dev, PMT_CTRL);
  183. reg &= ~0xfffff030;
  184. reg |= PMT_CTRL_PHY_RST;
  185. smc911x_reg_write(dev, PMT_CTRL, reg);
  186. mdelay(100);
  187. return 0;
  188. }
  189. static void smc911x_phy_configure(struct eth_device_smc911x *dev)
  190. {
  191. int timeout;
  192. uint16_t status;
  193. smc911x_phy_reset(dev);
  194. smc911x_eth_phy_write(dev, 1, MII_BMCR, BMCR_RESET);
  195. mdelay(1);
  196. smc911x_eth_phy_write(dev, 1, MII_ADVERTISE, 0x01e1);
  197. smc911x_eth_phy_write(dev, 1, MII_BMCR, BMCR_ANENABLE | BMCR_ANRESTART);
  198. timeout = 5000;
  199. do
  200. {
  201. mdelay(1);
  202. if ((timeout--) == 0)
  203. goto err_out;
  204. if (smc911x_eth_phy_read(dev, 1, MII_BMSR, &status) != 0)
  205. goto err_out;
  206. }
  207. while (!(status & BMSR_LSTATUS));
  208. return;
  209. err_out:
  210. rt_kprintf(DRIVERNAME ": autonegotiation timed out\n");
  211. }
  212. static void smc911x_enable(struct eth_device_smc911x *dev)
  213. {
  214. /* Enable TX */
  215. smc911x_reg_write(dev, HW_CFG, 8 << 16 | HW_CFG_SF);
  216. smc911x_reg_write(dev, GPT_CFG, GPT_CFG_TIMER_EN | 10000);
  217. smc911x_reg_write(dev, TX_CFG, TX_CFG_TX_ON);
  218. /* no padding to start of packets */
  219. smc911x_reg_write(dev, RX_CFG, 0);
  220. smc911x_set_mac_csr(dev, MAC_CR, MAC_CR_TXEN | MAC_CR_RXEN |
  221. MAC_CR_HBDIS);
  222. }
  223. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  224. /* wrapper for smc911x_eth_phy_read */
  225. static int smc911x_miiphy_read(struct mii_dev *bus, int phy, int devad,
  226. int reg)
  227. {
  228. uint16_t val = 0;
  229. struct eth_device_smc911x *dev = eth_get_dev_by_name(bus->name);
  230. if (dev)
  231. {
  232. int retval = smc911x_eth_phy_read(dev, phy, reg, &val);
  233. if (retval < 0)
  234. return retval;
  235. return val;
  236. }
  237. return -ENODEV;
  238. }
  239. /* wrapper for smc911x_eth_phy_write */
  240. static int smc911x_miiphy_write(struct mii_dev *bus, int phy, int devad,
  241. int reg, uint16_t val)
  242. {
  243. struct eth_device_smc911x *dev = eth_get_dev_by_name(bus->name);
  244. if (dev)
  245. return smc911x_eth_phy_write(dev, phy, reg, val);
  246. return -ENODEV;
  247. }
  248. #endif
  249. static void smc911x_isr(int vector, void *param)
  250. {
  251. uint32_t status;
  252. struct eth_device_smc911x *emac;
  253. emac = SMC911X_EMAC_DEVICE(param);
  254. status = smc911x_reg_read(emac, INT_STS);
  255. if (status & INT_STS_RSFL)
  256. {
  257. eth_device_ready(&emac->parent);
  258. }
  259. smc911x_reg_write(emac, INT_STS, status);
  260. return ;
  261. }
  262. static rt_err_t smc911x_emac_init(rt_device_t dev)
  263. {
  264. // uint32_t value;
  265. struct eth_device_smc911x *emac;
  266. emac = SMC911X_EMAC_DEVICE(dev);
  267. RT_ASSERT(emac != RT_NULL);
  268. smc911x_reset(emac);
  269. /* Configure the PHY, initialize the link state */
  270. smc911x_phy_configure(emac);
  271. smc911x_handle_mac_address(emac);
  272. /* Turn on Tx + Rx */
  273. smc911x_enable(emac);
  274. #if 1
  275. /* Interrupt on every received packet */
  276. smc911x_reg_write(emac, FIFO_INT, 0x01 << 8);
  277. smc911x_reg_write(emac, INT_EN, INT_EN_RDFL_EN | INT_EN_RSFL_EN);
  278. /* enable interrupt */
  279. smc911x_reg_write(emac, INT_CFG, INT_CFG_IRQ_EN | INT_CFG_IRQ_POL | INT_CFG_IRQ_TYPE);
  280. #else
  281. /* disable interrupt */
  282. smc911x_reg_write(emac, INT_EN, 0);
  283. value = smc911x_reg_read(emac, INT_CFG);
  284. value &= ~INT_CFG_IRQ_EN;
  285. smc911x_reg_write(emac, INT_CFG, value);
  286. #endif
  287. rt_hw_interrupt_install(emac->irqno, smc911x_isr, emac, "smc911x");
  288. rt_hw_interrupt_umask(emac->irqno);
  289. return RT_EOK;
  290. }
  291. static rt_err_t smc911x_emac_control(rt_device_t dev, int cmd, void *args)
  292. {
  293. struct eth_device_smc911x *emac;
  294. emac = SMC911X_EMAC_DEVICE(dev);
  295. RT_ASSERT(emac != RT_NULL);
  296. switch(cmd)
  297. {
  298. case NIOCTL_GADDR:
  299. /* get MAC address */
  300. if(args) rt_memcpy(args, emac->enetaddr, 6);
  301. else return -RT_ERROR;
  302. break;
  303. default :
  304. break;
  305. }
  306. return RT_EOK;
  307. }
  308. /* Ethernet device interface */
  309. /* transmit packet. */
  310. static uint8_t tx_buf[2048];
  311. rt_err_t smc911x_emac_tx(rt_device_t dev, struct pbuf* p)
  312. {
  313. struct eth_device_smc911x *emac;
  314. uint32_t *data;
  315. uint32_t tmplen;
  316. uint32_t status;
  317. uint32_t length;
  318. emac = SMC911X_EMAC_DEVICE(dev);
  319. RT_ASSERT(emac != RT_NULL);
  320. /* copy pbuf to a whole ETH frame */
  321. pbuf_copy_partial(p, tx_buf, p->tot_len, 0);
  322. /* send it out */
  323. data = (uint32_t*)tx_buf;
  324. length = p->tot_len;
  325. smc911x_reg_write(emac, TX_DATA_FIFO, TX_CMD_A_INT_FIRST_SEG | TX_CMD_A_INT_LAST_SEG | length);
  326. smc911x_reg_write(emac, TX_DATA_FIFO, length);
  327. tmplen = (length + 3) / 4;
  328. while (tmplen--)
  329. {
  330. smc911x_reg_write(emac, TX_DATA_FIFO, *data++);
  331. }
  332. /* wait for transmission */
  333. while (!((smc911x_reg_read(emac, TX_FIFO_INF) & TX_FIFO_INF_TSUSED) >> 16));
  334. /* get status. Ignore 'no carrier' error, it has no meaning for
  335. * full duplex operation
  336. */
  337. status = smc911x_reg_read(emac, TX_STATUS_FIFO) &
  338. (TX_STS_LOC | TX_STS_LATE_COLL | TX_STS_MANY_COLL |
  339. TX_STS_MANY_DEFER | TX_STS_UNDERRUN);
  340. if (!status) return 0;
  341. rt_kprintf(DRIVERNAME ": failed to send packet: %s%s%s%s%s\n",
  342. status & TX_STS_LOC ? "TX_STS_LOC " : "",
  343. status & TX_STS_LATE_COLL ? "TX_STS_LATE_COLL " : "",
  344. status & TX_STS_MANY_COLL ? "TX_STS_MANY_COLL " : "",
  345. status & TX_STS_MANY_DEFER ? "TX_STS_MANY_DEFER " : "",
  346. status & TX_STS_UNDERRUN ? "TX_STS_UNDERRUN" : "");
  347. return -RT_EIO;
  348. }
  349. /* reception packet. */
  350. struct pbuf *smc911x_emac_rx(rt_device_t dev)
  351. {
  352. struct pbuf* p = RT_NULL;
  353. struct eth_device_smc911x *emac;
  354. emac = SMC911X_EMAC_DEVICE(dev);
  355. RT_ASSERT(emac != RT_NULL);
  356. /* take the emac buffer to the pbuf */
  357. if ((smc911x_reg_read(emac, RX_FIFO_INF) & RX_FIFO_INF_RXSUSED) >> 16)
  358. {
  359. uint32_t status;
  360. uint32_t pktlen, tmplen;
  361. status = smc911x_reg_read(emac, RX_STATUS_FIFO);
  362. /* get frame length */
  363. pktlen = (status & RX_STS_PKT_LEN) >> 16;
  364. smc911x_reg_write(emac, RX_CFG, 0);
  365. tmplen = (pktlen + 3) / 4;
  366. /* allocate pbuf */
  367. p = pbuf_alloc(PBUF_LINK, tmplen * 4, PBUF_RAM);
  368. if (p)
  369. {
  370. uint32_t *data = (uint32_t *)p->payload;
  371. while (tmplen--)
  372. {
  373. *data++ = smc911x_reg_read(emac, RX_DATA_FIFO);
  374. }
  375. }
  376. if (status & RX_STS_ES)
  377. {
  378. rt_kprintf(DRIVERNAME ": dropped bad packet. Status: 0x%08x\n", status);
  379. }
  380. }
  381. return p;
  382. }
  383. int smc911x_emac_hw_init(void)
  384. {
  385. _emac.iobase = VEXPRESS_ETH_BASE;
  386. _emac.irqno = IRQ_VEXPRESS_A9_ETH;
  387. if (smc911x_detect_chip(&_emac))
  388. {
  389. rt_kprintf("no smc911x network interface found!\n");
  390. return -1;
  391. }
  392. /* set INT CFG */
  393. smc911x_reg_write(&_emac, INT_CFG, INT_CFG_IRQ_POL | INT_CFG_IRQ_TYPE);
  394. /* test MAC address */
  395. _emac.enetaddr[0] = 0x52;
  396. _emac.enetaddr[1] = 0x54;
  397. _emac.enetaddr[2] = 0x00;
  398. _emac.enetaddr[3] = 0x11;
  399. _emac.enetaddr[4] = 0x22;
  400. _emac.enetaddr[5] = 0x33;
  401. _emac.parent.parent.init = smc911x_emac_init;
  402. _emac.parent.parent.open = RT_NULL;
  403. _emac.parent.parent.close = RT_NULL;
  404. _emac.parent.parent.read = RT_NULL;
  405. _emac.parent.parent.write = RT_NULL;
  406. _emac.parent.parent.control = smc911x_emac_control;
  407. _emac.parent.parent.user_data = RT_NULL;
  408. _emac.parent.eth_rx = smc911x_emac_rx;
  409. _emac.parent.eth_tx = smc911x_emac_tx;
  410. /* register ETH device */
  411. eth_device_init(&(_emac.parent), "e0");
  412. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  413. {
  414. int retval;
  415. struct mii_dev *mdiodev = mdio_alloc();
  416. if (!mdiodev)
  417. return -ENOMEM;
  418. strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
  419. mdiodev->read = smc911x_miiphy_read;
  420. mdiodev->write = smc911x_miiphy_write;
  421. retval = mdio_register(mdiodev);
  422. if (retval < 0)
  423. return retval;
  424. }
  425. #endif
  426. eth_device_linkchange(&_emac.parent, RT_TRUE);
  427. return 0;
  428. }
  429. INIT_APP_EXPORT(smc911x_emac_hw_init);
  430. #include <finsh.h>
  431. int emac(int argc, char** argv)
  432. {
  433. rt_hw_interrupt_umask(_emac.irqno);
  434. return 0;
  435. }
  436. MSH_CMD_EXPORT(emac, emac dump);