hisi_femac.c 25 KB

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