drv_smc911x.c 14 KB

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