hisi_femac.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Hisilicon Fast Ethernet MAC Driver
  4. *
  5. * Copyright (c) 2016 HiSilicon Technologies Co., Ltd.
  6. */
  7. #include <linux/circ_buf.h>
  8. #include <linux/clk.h>
  9. #include <linux/etherdevice.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/module.h>
  12. #include <linux/of_mdio.h>
  13. #include <linux/of_net.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/reset.h>
  16. /* MAC control register list */
  17. #define MAC_PORTSEL 0x0200
  18. #define MAC_PORTSEL_STAT_CPU BIT(0)
  19. #define MAC_PORTSEL_RMII BIT(1)
  20. #define MAC_PORTSET 0x0208
  21. #define MAC_PORTSET_DUPLEX_FULL BIT(0)
  22. #define MAC_PORTSET_LINKED BIT(1)
  23. #define MAC_PORTSET_SPEED_100M BIT(2)
  24. #define MAC_SET 0x0210
  25. #define MAX_FRAME_SIZE 1600
  26. #define MAX_FRAME_SIZE_MASK GENMASK(10, 0)
  27. #define BIT_PAUSE_EN BIT(18)
  28. #define RX_COALESCE_SET 0x0340
  29. #define RX_COALESCED_FRAME_OFFSET 24
  30. #define RX_COALESCED_FRAMES 8
  31. #define RX_COALESCED_TIMER 0x74
  32. #define QLEN_SET 0x0344
  33. #define RX_DEPTH_OFFSET 8
  34. #define MAX_HW_FIFO_DEPTH 64
  35. #define HW_TX_FIFO_DEPTH 12
  36. #define HW_RX_FIFO_DEPTH (MAX_HW_FIFO_DEPTH - HW_TX_FIFO_DEPTH)
  37. #define IQFRM_DES 0x0354
  38. #define RX_FRAME_LEN_MASK GENMASK(11, 0)
  39. #define IQ_ADDR 0x0358
  40. #define EQ_ADDR 0x0360
  41. #define EQFRM_LEN 0x0364
  42. #define ADDRQ_STAT 0x036C
  43. #define TX_CNT_INUSE_MASK GENMASK(5, 0)
  44. #define BIT_TX_READY BIT(24)
  45. #define BIT_RX_READY BIT(25)
  46. /* global control register list */
  47. #define GLB_HOSTMAC_L32 0x0000
  48. #define GLB_HOSTMAC_H16 0x0004
  49. #define GLB_SOFT_RESET 0x0008
  50. #define SOFT_RESET_ALL BIT(0)
  51. #define GLB_FWCTRL 0x0010
  52. #define FWCTRL_VLAN_ENABLE BIT(0)
  53. #define FWCTRL_FW2CPU_ENA BIT(5)
  54. #define FWCTRL_FWALL2CPU BIT(7)
  55. #define GLB_MACTCTRL 0x0014
  56. #define MACTCTRL_UNI2CPU BIT(1)
  57. #define MACTCTRL_MULTI2CPU BIT(3)
  58. #define MACTCTRL_BROAD2CPU BIT(5)
  59. #define MACTCTRL_MACT_ENA BIT(7)
  60. #define GLB_IRQ_STAT 0x0030
  61. #define GLB_IRQ_ENA 0x0034
  62. #define IRQ_ENA_PORT0_MASK GENMASK(7, 0)
  63. #define IRQ_ENA_PORT0 BIT(18)
  64. #define IRQ_ENA_ALL BIT(19)
  65. #define GLB_IRQ_RAW 0x0038
  66. #define IRQ_INT_RX_RDY BIT(0)
  67. #define IRQ_INT_TX_PER_PACKET BIT(1)
  68. #define IRQ_INT_TX_FIFO_EMPTY BIT(6)
  69. #define IRQ_INT_MULTI_RXRDY BIT(7)
  70. #define DEF_INT_MASK (IRQ_INT_MULTI_RXRDY | \
  71. IRQ_INT_TX_PER_PACKET | \
  72. IRQ_INT_TX_FIFO_EMPTY)
  73. #define GLB_MAC_L32_BASE 0x0100
  74. #define GLB_MAC_H16_BASE 0x0104
  75. #define MACFLT_HI16_MASK GENMASK(15, 0)
  76. #define BIT_MACFLT_ENA BIT(17)
  77. #define BIT_MACFLT_FW2CPU BIT(21)
  78. #define GLB_MAC_H16(reg) (GLB_MAC_H16_BASE + ((reg) * 0x8))
  79. #define GLB_MAC_L32(reg) (GLB_MAC_L32_BASE + ((reg) * 0x8))
  80. #define MAX_MAC_FILTER_NUM 8
  81. #define MAX_UNICAST_ADDRESSES 2
  82. #define MAX_MULTICAST_ADDRESSES (MAX_MAC_FILTER_NUM - \
  83. MAX_UNICAST_ADDRESSES)
  84. /* software tx and rx queue number, should be power of 2 */
  85. #define TXQ_NUM 64
  86. #define RXQ_NUM 128
  87. #define FEMAC_POLL_WEIGHT 16
  88. #define PHY_RESET_DELAYS_PROPERTY "hisilicon,phy-reset-delays-us"
  89. enum phy_reset_delays {
  90. PRE_DELAY,
  91. PULSE,
  92. POST_DELAY,
  93. DELAYS_NUM,
  94. };
  95. struct hisi_femac_queue {
  96. struct sk_buff **skb;
  97. dma_addr_t *dma_phys;
  98. int num;
  99. unsigned int head;
  100. unsigned int tail;
  101. };
  102. struct hisi_femac_priv {
  103. void __iomem *port_base;
  104. void __iomem *glb_base;
  105. struct clk *clk;
  106. struct reset_control *mac_rst;
  107. struct reset_control *phy_rst;
  108. u32 phy_reset_delays[DELAYS_NUM];
  109. u32 link_status;
  110. struct device *dev;
  111. struct net_device *ndev;
  112. struct hisi_femac_queue txq;
  113. struct hisi_femac_queue rxq;
  114. u32 tx_fifo_used_cnt;
  115. struct napi_struct napi;
  116. };
  117. static void hisi_femac_irq_enable(struct hisi_femac_priv *priv, int irqs)
  118. {
  119. u32 val;
  120. val = readl(priv->glb_base + GLB_IRQ_ENA);
  121. writel(val | irqs, priv->glb_base + GLB_IRQ_ENA);
  122. }
  123. static void hisi_femac_irq_disable(struct hisi_femac_priv *priv, int irqs)
  124. {
  125. u32 val;
  126. val = readl(priv->glb_base + GLB_IRQ_ENA);
  127. writel(val & (~irqs), priv->glb_base + GLB_IRQ_ENA);
  128. }
  129. static void hisi_femac_tx_dma_unmap(struct hisi_femac_priv *priv,
  130. struct sk_buff *skb, unsigned int pos)
  131. {
  132. dma_addr_t dma_addr;
  133. dma_addr = priv->txq.dma_phys[pos];
  134. dma_unmap_single(priv->dev, dma_addr, skb->len, DMA_TO_DEVICE);
  135. }
  136. static void hisi_femac_xmit_reclaim(struct net_device *dev)
  137. {
  138. struct sk_buff *skb;
  139. struct hisi_femac_priv *priv = netdev_priv(dev);
  140. struct hisi_femac_queue *txq = &priv->txq;
  141. unsigned int bytes_compl = 0, pkts_compl = 0;
  142. u32 val;
  143. netif_tx_lock(dev);
  144. val = readl(priv->port_base + ADDRQ_STAT) & TX_CNT_INUSE_MASK;
  145. while (val < priv->tx_fifo_used_cnt) {
  146. skb = txq->skb[txq->tail];
  147. if (unlikely(!skb)) {
  148. netdev_err(dev, "xmitq_cnt_inuse=%d, tx_fifo_used=%d\n",
  149. val, priv->tx_fifo_used_cnt);
  150. break;
  151. }
  152. hisi_femac_tx_dma_unmap(priv, skb, txq->tail);
  153. pkts_compl++;
  154. bytes_compl += skb->len;
  155. dev_kfree_skb_any(skb);
  156. priv->tx_fifo_used_cnt--;
  157. val = readl(priv->port_base + ADDRQ_STAT) & TX_CNT_INUSE_MASK;
  158. txq->skb[txq->tail] = NULL;
  159. txq->tail = (txq->tail + 1) % txq->num;
  160. }
  161. netdev_completed_queue(dev, pkts_compl, bytes_compl);
  162. if (unlikely(netif_queue_stopped(dev)) && pkts_compl)
  163. netif_wake_queue(dev);
  164. netif_tx_unlock(dev);
  165. }
  166. static void hisi_femac_adjust_link(struct net_device *dev)
  167. {
  168. struct hisi_femac_priv *priv = netdev_priv(dev);
  169. struct phy_device *phy = dev->phydev;
  170. u32 status = 0;
  171. if (phy->link)
  172. status |= MAC_PORTSET_LINKED;
  173. if (phy->duplex == DUPLEX_FULL)
  174. status |= MAC_PORTSET_DUPLEX_FULL;
  175. if (phy->speed == SPEED_100)
  176. status |= MAC_PORTSET_SPEED_100M;
  177. if ((status != priv->link_status) &&
  178. ((status | priv->link_status) & MAC_PORTSET_LINKED)) {
  179. writel(status, priv->port_base + MAC_PORTSET);
  180. priv->link_status = status;
  181. phy_print_status(phy);
  182. }
  183. }
  184. static void hisi_femac_rx_refill(struct hisi_femac_priv *priv)
  185. {
  186. struct hisi_femac_queue *rxq = &priv->rxq;
  187. struct sk_buff *skb;
  188. u32 pos;
  189. u32 len = MAX_FRAME_SIZE;
  190. dma_addr_t addr;
  191. pos = rxq->head;
  192. while (readl(priv->port_base + ADDRQ_STAT) & BIT_RX_READY) {
  193. if (!CIRC_SPACE(pos, rxq->tail, rxq->num))
  194. break;
  195. if (unlikely(rxq->skb[pos])) {
  196. netdev_err(priv->ndev, "err skb[%d]=%p\n",
  197. pos, rxq->skb[pos]);
  198. break;
  199. }
  200. skb = netdev_alloc_skb_ip_align(priv->ndev, len);
  201. if (unlikely(!skb))
  202. break;
  203. addr = dma_map_single(priv->dev, skb->data, len,
  204. DMA_FROM_DEVICE);
  205. if (dma_mapping_error(priv->dev, addr)) {
  206. dev_kfree_skb_any(skb);
  207. break;
  208. }
  209. rxq->dma_phys[pos] = addr;
  210. rxq->skb[pos] = skb;
  211. writel(addr, priv->port_base + IQ_ADDR);
  212. pos = (pos + 1) % rxq->num;
  213. }
  214. rxq->head = pos;
  215. }
  216. static int hisi_femac_rx(struct net_device *dev, int limit)
  217. {
  218. struct hisi_femac_priv *priv = netdev_priv(dev);
  219. struct hisi_femac_queue *rxq = &priv->rxq;
  220. struct sk_buff *skb;
  221. dma_addr_t addr;
  222. u32 rx_pkt_info, pos, len, rx_pkts_num = 0;
  223. pos = rxq->tail;
  224. while (readl(priv->glb_base + GLB_IRQ_RAW) & IRQ_INT_RX_RDY) {
  225. rx_pkt_info = readl(priv->port_base + IQFRM_DES);
  226. len = rx_pkt_info & RX_FRAME_LEN_MASK;
  227. len -= ETH_FCS_LEN;
  228. /* tell hardware we will deal with this packet */
  229. writel(IRQ_INT_RX_RDY, priv->glb_base + GLB_IRQ_RAW);
  230. rx_pkts_num++;
  231. skb = rxq->skb[pos];
  232. if (unlikely(!skb)) {
  233. netdev_err(dev, "rx skb NULL. pos=%d\n", pos);
  234. break;
  235. }
  236. rxq->skb[pos] = NULL;
  237. addr = rxq->dma_phys[pos];
  238. dma_unmap_single(priv->dev, addr, MAX_FRAME_SIZE,
  239. DMA_FROM_DEVICE);
  240. skb_put(skb, len);
  241. if (unlikely(skb->len > MAX_FRAME_SIZE)) {
  242. netdev_err(dev, "rcv len err, len = %d\n", skb->len);
  243. dev->stats.rx_errors++;
  244. dev->stats.rx_length_errors++;
  245. dev_kfree_skb_any(skb);
  246. goto next;
  247. }
  248. skb->protocol = eth_type_trans(skb, dev);
  249. napi_gro_receive(&priv->napi, skb);
  250. dev->stats.rx_packets++;
  251. dev->stats.rx_bytes += len;
  252. next:
  253. pos = (pos + 1) % rxq->num;
  254. if (rx_pkts_num >= limit)
  255. break;
  256. }
  257. rxq->tail = pos;
  258. hisi_femac_rx_refill(priv);
  259. return rx_pkts_num;
  260. }
  261. static int hisi_femac_poll(struct napi_struct *napi, int budget)
  262. {
  263. struct hisi_femac_priv *priv = container_of(napi,
  264. struct hisi_femac_priv, napi);
  265. struct net_device *dev = priv->ndev;
  266. int work_done = 0, task = budget;
  267. int ints, num;
  268. do {
  269. hisi_femac_xmit_reclaim(dev);
  270. num = hisi_femac_rx(dev, task);
  271. work_done += num;
  272. task -= num;
  273. if (work_done >= budget)
  274. break;
  275. ints = readl(priv->glb_base + GLB_IRQ_RAW);
  276. writel(ints & DEF_INT_MASK,
  277. priv->glb_base + GLB_IRQ_RAW);
  278. } while (ints & DEF_INT_MASK);
  279. if (work_done < budget) {
  280. napi_complete_done(napi, work_done);
  281. hisi_femac_irq_enable(priv, DEF_INT_MASK &
  282. (~IRQ_INT_TX_PER_PACKET));
  283. }
  284. return work_done;
  285. }
  286. static irqreturn_t hisi_femac_interrupt(int irq, void *dev_id)
  287. {
  288. int ints;
  289. struct net_device *dev = (struct net_device *)dev_id;
  290. struct hisi_femac_priv *priv = netdev_priv(dev);
  291. ints = readl(priv->glb_base + GLB_IRQ_RAW);
  292. if (likely(ints & DEF_INT_MASK)) {
  293. writel(ints & DEF_INT_MASK,
  294. priv->glb_base + GLB_IRQ_RAW);
  295. hisi_femac_irq_disable(priv, DEF_INT_MASK);
  296. napi_schedule(&priv->napi);
  297. }
  298. return IRQ_HANDLED;
  299. }
  300. static int hisi_femac_init_queue(struct device *dev,
  301. struct hisi_femac_queue *queue,
  302. unsigned int num)
  303. {
  304. queue->skb = devm_kcalloc(dev, num, sizeof(struct sk_buff *),
  305. GFP_KERNEL);
  306. if (!queue->skb)
  307. return -ENOMEM;
  308. queue->dma_phys = devm_kcalloc(dev, num, sizeof(dma_addr_t),
  309. GFP_KERNEL);
  310. if (!queue->dma_phys)
  311. return -ENOMEM;
  312. queue->num = num;
  313. queue->head = 0;
  314. queue->tail = 0;
  315. return 0;
  316. }
  317. static int hisi_femac_init_tx_and_rx_queues(struct hisi_femac_priv *priv)
  318. {
  319. int ret;
  320. ret = hisi_femac_init_queue(priv->dev, &priv->txq, TXQ_NUM);
  321. if (ret)
  322. return ret;
  323. ret = hisi_femac_init_queue(priv->dev, &priv->rxq, RXQ_NUM);
  324. if (ret)
  325. return ret;
  326. priv->tx_fifo_used_cnt = 0;
  327. return 0;
  328. }
  329. static void hisi_femac_free_skb_rings(struct hisi_femac_priv *priv)
  330. {
  331. struct hisi_femac_queue *txq = &priv->txq;
  332. struct hisi_femac_queue *rxq = &priv->rxq;
  333. struct sk_buff *skb;
  334. dma_addr_t dma_addr;
  335. u32 pos;
  336. pos = rxq->tail;
  337. while (pos != rxq->head) {
  338. skb = rxq->skb[pos];
  339. if (unlikely(!skb)) {
  340. netdev_err(priv->ndev, "NULL rx skb. pos=%d, head=%d\n",
  341. pos, rxq->head);
  342. continue;
  343. }
  344. dma_addr = rxq->dma_phys[pos];
  345. dma_unmap_single(priv->dev, dma_addr, MAX_FRAME_SIZE,
  346. DMA_FROM_DEVICE);
  347. dev_kfree_skb_any(skb);
  348. rxq->skb[pos] = NULL;
  349. pos = (pos + 1) % rxq->num;
  350. }
  351. rxq->tail = pos;
  352. pos = txq->tail;
  353. while (pos != txq->head) {
  354. skb = txq->skb[pos];
  355. if (unlikely(!skb)) {
  356. netdev_err(priv->ndev, "NULL tx skb. pos=%d, head=%d\n",
  357. pos, txq->head);
  358. continue;
  359. }
  360. hisi_femac_tx_dma_unmap(priv, skb, pos);
  361. dev_kfree_skb_any(skb);
  362. txq->skb[pos] = NULL;
  363. pos = (pos + 1) % txq->num;
  364. }
  365. txq->tail = pos;
  366. priv->tx_fifo_used_cnt = 0;
  367. }
  368. static int hisi_femac_set_hw_mac_addr(struct hisi_femac_priv *priv,
  369. const unsigned char *mac)
  370. {
  371. u32 reg;
  372. reg = mac[1] | (mac[0] << 8);
  373. writel(reg, priv->glb_base + GLB_HOSTMAC_H16);
  374. reg = mac[5] | (mac[4] << 8) | (mac[3] << 16) | (mac[2] << 24);
  375. writel(reg, priv->glb_base + GLB_HOSTMAC_L32);
  376. return 0;
  377. }
  378. static int hisi_femac_port_reset(struct hisi_femac_priv *priv)
  379. {
  380. u32 val;
  381. val = readl(priv->glb_base + GLB_SOFT_RESET);
  382. val |= SOFT_RESET_ALL;
  383. writel(val, priv->glb_base + GLB_SOFT_RESET);
  384. usleep_range(500, 800);
  385. val &= ~SOFT_RESET_ALL;
  386. writel(val, priv->glb_base + GLB_SOFT_RESET);
  387. return 0;
  388. }
  389. static int hisi_femac_net_open(struct net_device *dev)
  390. {
  391. struct hisi_femac_priv *priv = netdev_priv(dev);
  392. hisi_femac_port_reset(priv);
  393. hisi_femac_set_hw_mac_addr(priv, dev->dev_addr);
  394. hisi_femac_rx_refill(priv);
  395. netif_carrier_off(dev);
  396. netdev_reset_queue(dev);
  397. netif_start_queue(dev);
  398. napi_enable(&priv->napi);
  399. priv->link_status = 0;
  400. if (dev->phydev)
  401. phy_start(dev->phydev);
  402. writel(IRQ_ENA_PORT0_MASK, priv->glb_base + GLB_IRQ_RAW);
  403. hisi_femac_irq_enable(priv, IRQ_ENA_ALL | IRQ_ENA_PORT0 | DEF_INT_MASK);
  404. return 0;
  405. }
  406. static int hisi_femac_net_close(struct net_device *dev)
  407. {
  408. struct hisi_femac_priv *priv = netdev_priv(dev);
  409. hisi_femac_irq_disable(priv, IRQ_ENA_PORT0);
  410. if (dev->phydev)
  411. phy_stop(dev->phydev);
  412. netif_stop_queue(dev);
  413. napi_disable(&priv->napi);
  414. hisi_femac_free_skb_rings(priv);
  415. return 0;
  416. }
  417. static netdev_tx_t hisi_femac_net_xmit(struct sk_buff *skb,
  418. struct net_device *dev)
  419. {
  420. struct hisi_femac_priv *priv = netdev_priv(dev);
  421. struct hisi_femac_queue *txq = &priv->txq;
  422. dma_addr_t addr;
  423. u32 val;
  424. val = readl(priv->port_base + ADDRQ_STAT);
  425. val &= BIT_TX_READY;
  426. if (!val) {
  427. hisi_femac_irq_enable(priv, IRQ_INT_TX_PER_PACKET);
  428. dev->stats.tx_dropped++;
  429. dev->stats.tx_fifo_errors++;
  430. netif_stop_queue(dev);
  431. return NETDEV_TX_BUSY;
  432. }
  433. if (unlikely(!CIRC_SPACE(txq->head, txq->tail,
  434. txq->num))) {
  435. hisi_femac_irq_enable(priv, IRQ_INT_TX_PER_PACKET);
  436. dev->stats.tx_dropped++;
  437. dev->stats.tx_fifo_errors++;
  438. netif_stop_queue(dev);
  439. return NETDEV_TX_BUSY;
  440. }
  441. addr = dma_map_single(priv->dev, skb->data,
  442. skb->len, DMA_TO_DEVICE);
  443. if (unlikely(dma_mapping_error(priv->dev, addr))) {
  444. dev_kfree_skb_any(skb);
  445. dev->stats.tx_dropped++;
  446. return NETDEV_TX_OK;
  447. }
  448. txq->dma_phys[txq->head] = addr;
  449. txq->skb[txq->head] = skb;
  450. txq->head = (txq->head + 1) % txq->num;
  451. writel(addr, priv->port_base + EQ_ADDR);
  452. writel(skb->len + ETH_FCS_LEN, priv->port_base + EQFRM_LEN);
  453. priv->tx_fifo_used_cnt++;
  454. dev->stats.tx_packets++;
  455. dev->stats.tx_bytes += skb->len;
  456. netdev_sent_queue(dev, skb->len);
  457. return NETDEV_TX_OK;
  458. }
  459. static int hisi_femac_set_mac_address(struct net_device *dev, void *p)
  460. {
  461. struct hisi_femac_priv *priv = netdev_priv(dev);
  462. struct sockaddr *skaddr = p;
  463. if (!is_valid_ether_addr(skaddr->sa_data))
  464. return -EADDRNOTAVAIL;
  465. eth_hw_addr_set(dev, skaddr->sa_data);
  466. dev->addr_assign_type &= ~NET_ADDR_RANDOM;
  467. hisi_femac_set_hw_mac_addr(priv, dev->dev_addr);
  468. return 0;
  469. }
  470. static void hisi_femac_enable_hw_addr_filter(struct hisi_femac_priv *priv,
  471. unsigned int reg_n, bool enable)
  472. {
  473. u32 val;
  474. val = readl(priv->glb_base + GLB_MAC_H16(reg_n));
  475. if (enable)
  476. val |= BIT_MACFLT_ENA;
  477. else
  478. val &= ~BIT_MACFLT_ENA;
  479. writel(val, priv->glb_base + GLB_MAC_H16(reg_n));
  480. }
  481. static void hisi_femac_set_hw_addr_filter(struct hisi_femac_priv *priv,
  482. unsigned char *addr,
  483. unsigned int reg_n)
  484. {
  485. unsigned int high, low;
  486. u32 val;
  487. high = GLB_MAC_H16(reg_n);
  488. low = GLB_MAC_L32(reg_n);
  489. val = (addr[2] << 24) | (addr[3] << 16) | (addr[4] << 8) | addr[5];
  490. writel(val, priv->glb_base + low);
  491. val = readl(priv->glb_base + high);
  492. val &= ~MACFLT_HI16_MASK;
  493. val |= ((addr[0] << 8) | addr[1]);
  494. val |= (BIT_MACFLT_ENA | BIT_MACFLT_FW2CPU);
  495. writel(val, priv->glb_base + high);
  496. }
  497. static void hisi_femac_set_promisc_mode(struct hisi_femac_priv *priv,
  498. bool promisc_mode)
  499. {
  500. u32 val;
  501. val = readl(priv->glb_base + GLB_FWCTRL);
  502. if (promisc_mode)
  503. val |= FWCTRL_FWALL2CPU;
  504. else
  505. val &= ~FWCTRL_FWALL2CPU;
  506. writel(val, priv->glb_base + GLB_FWCTRL);
  507. }
  508. /* Handle multiple multicast addresses (perfect filtering)*/
  509. static void hisi_femac_set_mc_addr_filter(struct hisi_femac_priv *priv)
  510. {
  511. struct net_device *dev = priv->ndev;
  512. u32 val;
  513. val = readl(priv->glb_base + GLB_MACTCTRL);
  514. if ((netdev_mc_count(dev) > MAX_MULTICAST_ADDRESSES) ||
  515. (dev->flags & IFF_ALLMULTI)) {
  516. val |= MACTCTRL_MULTI2CPU;
  517. } else {
  518. int reg = MAX_UNICAST_ADDRESSES;
  519. int i;
  520. struct netdev_hw_addr *ha;
  521. for (i = reg; i < MAX_MAC_FILTER_NUM; i++)
  522. hisi_femac_enable_hw_addr_filter(priv, i, false);
  523. netdev_for_each_mc_addr(ha, dev) {
  524. hisi_femac_set_hw_addr_filter(priv, ha->addr, reg);
  525. reg++;
  526. }
  527. val &= ~MACTCTRL_MULTI2CPU;
  528. }
  529. writel(val, priv->glb_base + GLB_MACTCTRL);
  530. }
  531. /* Handle multiple unicast addresses (perfect filtering)*/
  532. static void hisi_femac_set_uc_addr_filter(struct hisi_femac_priv *priv)
  533. {
  534. struct net_device *dev = priv->ndev;
  535. u32 val;
  536. val = readl(priv->glb_base + GLB_MACTCTRL);
  537. if (netdev_uc_count(dev) > MAX_UNICAST_ADDRESSES) {
  538. val |= MACTCTRL_UNI2CPU;
  539. } else {
  540. int reg = 0;
  541. int i;
  542. struct netdev_hw_addr *ha;
  543. for (i = reg; i < MAX_UNICAST_ADDRESSES; i++)
  544. hisi_femac_enable_hw_addr_filter(priv, i, false);
  545. netdev_for_each_uc_addr(ha, dev) {
  546. hisi_femac_set_hw_addr_filter(priv, ha->addr, reg);
  547. reg++;
  548. }
  549. val &= ~MACTCTRL_UNI2CPU;
  550. }
  551. writel(val, priv->glb_base + GLB_MACTCTRL);
  552. }
  553. static void hisi_femac_net_set_rx_mode(struct net_device *dev)
  554. {
  555. struct hisi_femac_priv *priv = netdev_priv(dev);
  556. if (dev->flags & IFF_PROMISC) {
  557. hisi_femac_set_promisc_mode(priv, true);
  558. } else {
  559. hisi_femac_set_promisc_mode(priv, false);
  560. hisi_femac_set_mc_addr_filter(priv);
  561. hisi_femac_set_uc_addr_filter(priv);
  562. }
  563. }
  564. static const struct ethtool_ops hisi_femac_ethtools_ops = {
  565. .get_link = ethtool_op_get_link,
  566. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  567. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  568. };
  569. static const struct net_device_ops hisi_femac_netdev_ops = {
  570. .ndo_open = hisi_femac_net_open,
  571. .ndo_stop = hisi_femac_net_close,
  572. .ndo_start_xmit = hisi_femac_net_xmit,
  573. .ndo_eth_ioctl = phy_do_ioctl_running,
  574. .ndo_set_mac_address = hisi_femac_set_mac_address,
  575. .ndo_set_rx_mode = hisi_femac_net_set_rx_mode,
  576. };
  577. static void hisi_femac_core_reset(struct hisi_femac_priv *priv)
  578. {
  579. reset_control_assert(priv->mac_rst);
  580. reset_control_deassert(priv->mac_rst);
  581. }
  582. static void hisi_femac_sleep_us(u32 time_us)
  583. {
  584. u32 time_ms;
  585. if (!time_us)
  586. return;
  587. time_ms = DIV_ROUND_UP(time_us, 1000);
  588. if (time_ms < 20)
  589. usleep_range(time_us, time_us + 500);
  590. else
  591. msleep(time_ms);
  592. }
  593. static void hisi_femac_phy_reset(struct hisi_femac_priv *priv)
  594. {
  595. /* To make sure PHY hardware reset success,
  596. * we must keep PHY in deassert state first and
  597. * then complete the hardware reset operation
  598. */
  599. reset_control_deassert(priv->phy_rst);
  600. hisi_femac_sleep_us(priv->phy_reset_delays[PRE_DELAY]);
  601. reset_control_assert(priv->phy_rst);
  602. /* delay some time to ensure reset ok,
  603. * this depends on PHY hardware feature
  604. */
  605. hisi_femac_sleep_us(priv->phy_reset_delays[PULSE]);
  606. reset_control_deassert(priv->phy_rst);
  607. /* delay some time to ensure later MDIO access */
  608. hisi_femac_sleep_us(priv->phy_reset_delays[POST_DELAY]);
  609. }
  610. static void hisi_femac_port_init(struct hisi_femac_priv *priv)
  611. {
  612. u32 val;
  613. /* MAC gets link status info and phy mode by software config */
  614. val = MAC_PORTSEL_STAT_CPU;
  615. if (priv->ndev->phydev->interface == PHY_INTERFACE_MODE_RMII)
  616. val |= MAC_PORTSEL_RMII;
  617. writel(val, priv->port_base + MAC_PORTSEL);
  618. /*clear all interrupt status */
  619. writel(IRQ_ENA_PORT0_MASK, priv->glb_base + GLB_IRQ_RAW);
  620. hisi_femac_irq_disable(priv, IRQ_ENA_PORT0_MASK | IRQ_ENA_PORT0);
  621. val = readl(priv->glb_base + GLB_FWCTRL);
  622. val &= ~(FWCTRL_VLAN_ENABLE | FWCTRL_FWALL2CPU);
  623. val |= FWCTRL_FW2CPU_ENA;
  624. writel(val, priv->glb_base + GLB_FWCTRL);
  625. val = readl(priv->glb_base + GLB_MACTCTRL);
  626. val |= (MACTCTRL_BROAD2CPU | MACTCTRL_MACT_ENA);
  627. writel(val, priv->glb_base + GLB_MACTCTRL);
  628. val = readl(priv->port_base + MAC_SET);
  629. val &= ~MAX_FRAME_SIZE_MASK;
  630. val |= MAX_FRAME_SIZE;
  631. writel(val, priv->port_base + MAC_SET);
  632. val = RX_COALESCED_TIMER |
  633. (RX_COALESCED_FRAMES << RX_COALESCED_FRAME_OFFSET);
  634. writel(val, priv->port_base + RX_COALESCE_SET);
  635. val = (HW_RX_FIFO_DEPTH << RX_DEPTH_OFFSET) | HW_TX_FIFO_DEPTH;
  636. writel(val, priv->port_base + QLEN_SET);
  637. }
  638. static int hisi_femac_drv_probe(struct platform_device *pdev)
  639. {
  640. struct device *dev = &pdev->dev;
  641. struct device_node *node = dev->of_node;
  642. struct net_device *ndev;
  643. struct hisi_femac_priv *priv;
  644. struct phy_device *phy;
  645. int ret;
  646. ndev = alloc_etherdev(sizeof(*priv));
  647. if (!ndev)
  648. return -ENOMEM;
  649. platform_set_drvdata(pdev, ndev);
  650. SET_NETDEV_DEV(ndev, &pdev->dev);
  651. priv = netdev_priv(ndev);
  652. priv->dev = dev;
  653. priv->ndev = ndev;
  654. priv->port_base = devm_platform_ioremap_resource(pdev, 0);
  655. if (IS_ERR(priv->port_base)) {
  656. ret = PTR_ERR(priv->port_base);
  657. goto out_free_netdev;
  658. }
  659. priv->glb_base = devm_platform_ioremap_resource(pdev, 1);
  660. if (IS_ERR(priv->glb_base)) {
  661. ret = PTR_ERR(priv->glb_base);
  662. goto out_free_netdev;
  663. }
  664. priv->clk = devm_clk_get(&pdev->dev, NULL);
  665. if (IS_ERR(priv->clk)) {
  666. dev_err(dev, "failed to get clk\n");
  667. ret = -ENODEV;
  668. goto out_free_netdev;
  669. }
  670. ret = clk_prepare_enable(priv->clk);
  671. if (ret) {
  672. dev_err(dev, "failed to enable clk %d\n", ret);
  673. goto out_free_netdev;
  674. }
  675. priv->mac_rst = devm_reset_control_get(dev, "mac");
  676. if (IS_ERR(priv->mac_rst)) {
  677. ret = PTR_ERR(priv->mac_rst);
  678. goto out_disable_clk;
  679. }
  680. hisi_femac_core_reset(priv);
  681. priv->phy_rst = devm_reset_control_get(dev, "phy");
  682. if (IS_ERR(priv->phy_rst)) {
  683. priv->phy_rst = NULL;
  684. } else {
  685. ret = of_property_read_u32_array(node,
  686. PHY_RESET_DELAYS_PROPERTY,
  687. priv->phy_reset_delays,
  688. DELAYS_NUM);
  689. if (ret)
  690. goto out_disable_clk;
  691. hisi_femac_phy_reset(priv);
  692. }
  693. phy = of_phy_get_and_connect(ndev, node, hisi_femac_adjust_link);
  694. if (!phy) {
  695. dev_err(dev, "connect to PHY failed!\n");
  696. ret = -ENODEV;
  697. goto out_disable_clk;
  698. }
  699. phy_attached_print(phy, "phy_id=0x%.8lx, phy_mode=%s\n",
  700. (unsigned long)phy->phy_id,
  701. phy_modes(phy->interface));
  702. ret = of_get_ethdev_address(node, ndev);
  703. if (ret) {
  704. eth_hw_addr_random(ndev);
  705. dev_warn(dev, "using random MAC address %pM\n",
  706. ndev->dev_addr);
  707. }
  708. ndev->watchdog_timeo = 6 * HZ;
  709. ndev->priv_flags |= IFF_UNICAST_FLT;
  710. ndev->netdev_ops = &hisi_femac_netdev_ops;
  711. ndev->ethtool_ops = &hisi_femac_ethtools_ops;
  712. netif_napi_add_weight(ndev, &priv->napi, hisi_femac_poll,
  713. FEMAC_POLL_WEIGHT);
  714. hisi_femac_port_init(priv);
  715. ret = hisi_femac_init_tx_and_rx_queues(priv);
  716. if (ret)
  717. goto out_disconnect_phy;
  718. ndev->irq = platform_get_irq(pdev, 0);
  719. if (ndev->irq <= 0) {
  720. ret = -ENODEV;
  721. goto out_disconnect_phy;
  722. }
  723. ret = devm_request_irq(dev, ndev->irq, hisi_femac_interrupt,
  724. IRQF_SHARED, pdev->name, ndev);
  725. if (ret) {
  726. dev_err(dev, "devm_request_irq %d failed!\n", ndev->irq);
  727. goto out_disconnect_phy;
  728. }
  729. ret = register_netdev(ndev);
  730. if (ret) {
  731. dev_err(dev, "register_netdev failed!\n");
  732. goto out_disconnect_phy;
  733. }
  734. return ret;
  735. out_disconnect_phy:
  736. netif_napi_del(&priv->napi);
  737. phy_disconnect(phy);
  738. out_disable_clk:
  739. clk_disable_unprepare(priv->clk);
  740. out_free_netdev:
  741. free_netdev(ndev);
  742. return ret;
  743. }
  744. static int hisi_femac_drv_remove(struct platform_device *pdev)
  745. {
  746. struct net_device *ndev = platform_get_drvdata(pdev);
  747. struct hisi_femac_priv *priv = netdev_priv(ndev);
  748. netif_napi_del(&priv->napi);
  749. unregister_netdev(ndev);
  750. phy_disconnect(ndev->phydev);
  751. clk_disable_unprepare(priv->clk);
  752. free_netdev(ndev);
  753. return 0;
  754. }
  755. #ifdef CONFIG_PM
  756. static int hisi_femac_drv_suspend(struct platform_device *pdev,
  757. pm_message_t state)
  758. {
  759. struct net_device *ndev = platform_get_drvdata(pdev);
  760. struct hisi_femac_priv *priv = netdev_priv(ndev);
  761. disable_irq(ndev->irq);
  762. if (netif_running(ndev)) {
  763. hisi_femac_net_close(ndev);
  764. netif_device_detach(ndev);
  765. }
  766. clk_disable_unprepare(priv->clk);
  767. return 0;
  768. }
  769. static int hisi_femac_drv_resume(struct platform_device *pdev)
  770. {
  771. struct net_device *ndev = platform_get_drvdata(pdev);
  772. struct hisi_femac_priv *priv = netdev_priv(ndev);
  773. clk_prepare_enable(priv->clk);
  774. if (priv->phy_rst)
  775. hisi_femac_phy_reset(priv);
  776. if (netif_running(ndev)) {
  777. hisi_femac_port_init(priv);
  778. hisi_femac_net_open(ndev);
  779. netif_device_attach(ndev);
  780. }
  781. enable_irq(ndev->irq);
  782. return 0;
  783. }
  784. #endif
  785. static const struct of_device_id hisi_femac_match[] = {
  786. {.compatible = "hisilicon,hisi-femac-v1",},
  787. {.compatible = "hisilicon,hisi-femac-v2",},
  788. {.compatible = "hisilicon,hi3516cv300-femac",},
  789. {},
  790. };
  791. MODULE_DEVICE_TABLE(of, hisi_femac_match);
  792. static struct platform_driver hisi_femac_driver = {
  793. .driver = {
  794. .name = "hisi-femac",
  795. .of_match_table = hisi_femac_match,
  796. },
  797. .probe = hisi_femac_drv_probe,
  798. .remove = hisi_femac_drv_remove,
  799. #ifdef CONFIG_PM
  800. .suspend = hisi_femac_drv_suspend,
  801. .resume = hisi_femac_drv_resume,
  802. #endif
  803. };
  804. module_platform_driver(hisi_femac_driver);
  805. MODULE_DESCRIPTION("Hisilicon Fast Ethernet MAC driver");
  806. MODULE_AUTHOR("Dongpo Li <[email protected]>");
  807. MODULE_LICENSE("GPL v2");
  808. MODULE_ALIAS("platform:hisi-femac");