hix5hd2_gmac.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. /* Copyright (c) 2014 Linaro Ltd.
  2. * Copyright (c) 2014 Hisilicon Limited.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/etherdevice.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/of_net.h>
  14. #include <linux/of_mdio.h>
  15. #include <linux/clk.h>
  16. #include <linux/circ_buf.h>
  17. #define STATION_ADDR_LOW 0x0000
  18. #define STATION_ADDR_HIGH 0x0004
  19. #define MAC_DUPLEX_HALF_CTRL 0x0008
  20. #define MAX_FRM_SIZE 0x003c
  21. #define PORT_MODE 0x0040
  22. #define PORT_EN 0x0044
  23. #define BITS_TX_EN BIT(2)
  24. #define BITS_RX_EN BIT(1)
  25. #define REC_FILT_CONTROL 0x0064
  26. #define BIT_CRC_ERR_PASS BIT(5)
  27. #define BIT_PAUSE_FRM_PASS BIT(4)
  28. #define BIT_VLAN_DROP_EN BIT(3)
  29. #define BIT_BC_DROP_EN BIT(2)
  30. #define BIT_MC_MATCH_EN BIT(1)
  31. #define BIT_UC_MATCH_EN BIT(0)
  32. #define PORT_MC_ADDR_LOW 0x0068
  33. #define PORT_MC_ADDR_HIGH 0x006C
  34. #define CF_CRC_STRIP 0x01b0
  35. #define MODE_CHANGE_EN 0x01b4
  36. #define BIT_MODE_CHANGE_EN BIT(0)
  37. #define COL_SLOT_TIME 0x01c0
  38. #define RECV_CONTROL 0x01e0
  39. #define BIT_STRIP_PAD_EN BIT(3)
  40. #define BIT_RUNT_PKT_EN BIT(4)
  41. #define CONTROL_WORD 0x0214
  42. #define MDIO_SINGLE_CMD 0x03c0
  43. #define MDIO_SINGLE_DATA 0x03c4
  44. #define MDIO_CTRL 0x03cc
  45. #define MDIO_RDATA_STATUS 0x03d0
  46. #define MDIO_START BIT(20)
  47. #define MDIO_R_VALID BIT(0)
  48. #define MDIO_READ (BIT(17) | MDIO_START)
  49. #define MDIO_WRITE (BIT(16) | MDIO_START)
  50. #define RX_FQ_START_ADDR 0x0500
  51. #define RX_FQ_DEPTH 0x0504
  52. #define RX_FQ_WR_ADDR 0x0508
  53. #define RX_FQ_RD_ADDR 0x050c
  54. #define RX_FQ_VLDDESC_CNT 0x0510
  55. #define RX_FQ_ALEMPTY_TH 0x0514
  56. #define RX_FQ_REG_EN 0x0518
  57. #define BITS_RX_FQ_START_ADDR_EN BIT(2)
  58. #define BITS_RX_FQ_DEPTH_EN BIT(1)
  59. #define BITS_RX_FQ_RD_ADDR_EN BIT(0)
  60. #define RX_FQ_ALFULL_TH 0x051c
  61. #define RX_BQ_START_ADDR 0x0520
  62. #define RX_BQ_DEPTH 0x0524
  63. #define RX_BQ_WR_ADDR 0x0528
  64. #define RX_BQ_RD_ADDR 0x052c
  65. #define RX_BQ_FREE_DESC_CNT 0x0530
  66. #define RX_BQ_ALEMPTY_TH 0x0534
  67. #define RX_BQ_REG_EN 0x0538
  68. #define BITS_RX_BQ_START_ADDR_EN BIT(2)
  69. #define BITS_RX_BQ_DEPTH_EN BIT(1)
  70. #define BITS_RX_BQ_WR_ADDR_EN BIT(0)
  71. #define RX_BQ_ALFULL_TH 0x053c
  72. #define TX_BQ_START_ADDR 0x0580
  73. #define TX_BQ_DEPTH 0x0584
  74. #define TX_BQ_WR_ADDR 0x0588
  75. #define TX_BQ_RD_ADDR 0x058c
  76. #define TX_BQ_VLDDESC_CNT 0x0590
  77. #define TX_BQ_ALEMPTY_TH 0x0594
  78. #define TX_BQ_REG_EN 0x0598
  79. #define BITS_TX_BQ_START_ADDR_EN BIT(2)
  80. #define BITS_TX_BQ_DEPTH_EN BIT(1)
  81. #define BITS_TX_BQ_RD_ADDR_EN BIT(0)
  82. #define TX_BQ_ALFULL_TH 0x059c
  83. #define TX_RQ_START_ADDR 0x05a0
  84. #define TX_RQ_DEPTH 0x05a4
  85. #define TX_RQ_WR_ADDR 0x05a8
  86. #define TX_RQ_RD_ADDR 0x05ac
  87. #define TX_RQ_FREE_DESC_CNT 0x05b0
  88. #define TX_RQ_ALEMPTY_TH 0x05b4
  89. #define TX_RQ_REG_EN 0x05b8
  90. #define BITS_TX_RQ_START_ADDR_EN BIT(2)
  91. #define BITS_TX_RQ_DEPTH_EN BIT(1)
  92. #define BITS_TX_RQ_WR_ADDR_EN BIT(0)
  93. #define TX_RQ_ALFULL_TH 0x05bc
  94. #define RAW_PMU_INT 0x05c0
  95. #define ENA_PMU_INT 0x05c4
  96. #define STATUS_PMU_INT 0x05c8
  97. #define MAC_FIFO_ERR_IN BIT(30)
  98. #define TX_RQ_IN_TIMEOUT_INT BIT(29)
  99. #define RX_BQ_IN_TIMEOUT_INT BIT(28)
  100. #define TXOUTCFF_FULL_INT BIT(27)
  101. #define TXOUTCFF_EMPTY_INT BIT(26)
  102. #define TXCFF_FULL_INT BIT(25)
  103. #define TXCFF_EMPTY_INT BIT(24)
  104. #define RXOUTCFF_FULL_INT BIT(23)
  105. #define RXOUTCFF_EMPTY_INT BIT(22)
  106. #define RXCFF_FULL_INT BIT(21)
  107. #define RXCFF_EMPTY_INT BIT(20)
  108. #define TX_RQ_IN_INT BIT(19)
  109. #define TX_BQ_OUT_INT BIT(18)
  110. #define RX_BQ_IN_INT BIT(17)
  111. #define RX_FQ_OUT_INT BIT(16)
  112. #define TX_RQ_EMPTY_INT BIT(15)
  113. #define TX_RQ_FULL_INT BIT(14)
  114. #define TX_RQ_ALEMPTY_INT BIT(13)
  115. #define TX_RQ_ALFULL_INT BIT(12)
  116. #define TX_BQ_EMPTY_INT BIT(11)
  117. #define TX_BQ_FULL_INT BIT(10)
  118. #define TX_BQ_ALEMPTY_INT BIT(9)
  119. #define TX_BQ_ALFULL_INT BIT(8)
  120. #define RX_BQ_EMPTY_INT BIT(7)
  121. #define RX_BQ_FULL_INT BIT(6)
  122. #define RX_BQ_ALEMPTY_INT BIT(5)
  123. #define RX_BQ_ALFULL_INT BIT(4)
  124. #define RX_FQ_EMPTY_INT BIT(3)
  125. #define RX_FQ_FULL_INT BIT(2)
  126. #define RX_FQ_ALEMPTY_INT BIT(1)
  127. #define RX_FQ_ALFULL_INT BIT(0)
  128. #define DEF_INT_MASK (RX_BQ_IN_INT | RX_BQ_IN_TIMEOUT_INT | \
  129. TX_RQ_IN_INT | TX_RQ_IN_TIMEOUT_INT)
  130. #define DESC_WR_RD_ENA 0x05cc
  131. #define IN_QUEUE_TH 0x05d8
  132. #define OUT_QUEUE_TH 0x05dc
  133. #define QUEUE_TX_BQ_SHIFT 16
  134. #define RX_BQ_IN_TIMEOUT_TH 0x05e0
  135. #define TX_RQ_IN_TIMEOUT_TH 0x05e4
  136. #define STOP_CMD 0x05e8
  137. #define BITS_TX_STOP BIT(1)
  138. #define BITS_RX_STOP BIT(0)
  139. #define FLUSH_CMD 0x05eC
  140. #define BITS_TX_FLUSH_CMD BIT(5)
  141. #define BITS_RX_FLUSH_CMD BIT(4)
  142. #define BITS_TX_FLUSH_FLAG_DOWN BIT(3)
  143. #define BITS_TX_FLUSH_FLAG_UP BIT(2)
  144. #define BITS_RX_FLUSH_FLAG_DOWN BIT(1)
  145. #define BITS_RX_FLUSH_FLAG_UP BIT(0)
  146. #define RX_CFF_NUM_REG 0x05f0
  147. #define PMU_FSM_REG 0x05f8
  148. #define RX_FIFO_PKT_IN_NUM 0x05fc
  149. #define RX_FIFO_PKT_OUT_NUM 0x0600
  150. #define RGMII_SPEED_1000 0x2c
  151. #define RGMII_SPEED_100 0x2f
  152. #define RGMII_SPEED_10 0x2d
  153. #define MII_SPEED_100 0x0f
  154. #define MII_SPEED_10 0x0d
  155. #define GMAC_SPEED_1000 0x05
  156. #define GMAC_SPEED_100 0x01
  157. #define GMAC_SPEED_10 0x00
  158. #define GMAC_FULL_DUPLEX BIT(4)
  159. #define RX_BQ_INT_THRESHOLD 0x01
  160. #define TX_RQ_INT_THRESHOLD 0x01
  161. #define RX_BQ_IN_TIMEOUT 0x10000
  162. #define TX_RQ_IN_TIMEOUT 0x50000
  163. #define MAC_MAX_FRAME_SIZE 1600
  164. #define DESC_SIZE 32
  165. #define RX_DESC_NUM 1024
  166. #define TX_DESC_NUM 1024
  167. #define DESC_VLD_FREE 0
  168. #define DESC_VLD_BUSY 0x80000000
  169. #define DESC_FL_MID 0
  170. #define DESC_FL_LAST 0x20000000
  171. #define DESC_FL_FIRST 0x40000000
  172. #define DESC_FL_FULL 0x60000000
  173. #define DESC_DATA_LEN_OFF 16
  174. #define DESC_BUFF_LEN_OFF 0
  175. #define DESC_DATA_MASK 0x7ff
  176. /* DMA descriptor ring helpers */
  177. #define dma_ring_incr(n, s) (((n) + 1) & ((s) - 1))
  178. #define dma_cnt(n) ((n) >> 5)
  179. #define dma_byte(n) ((n) << 5)
  180. struct hix5hd2_desc {
  181. __le32 buff_addr;
  182. __le32 cmd;
  183. } __aligned(32);
  184. struct hix5hd2_desc_sw {
  185. struct hix5hd2_desc *desc;
  186. dma_addr_t phys_addr;
  187. unsigned int count;
  188. unsigned int size;
  189. };
  190. #define QUEUE_NUMS 4
  191. struct hix5hd2_priv {
  192. struct hix5hd2_desc_sw pool[QUEUE_NUMS];
  193. #define rx_fq pool[0]
  194. #define rx_bq pool[1]
  195. #define tx_bq pool[2]
  196. #define tx_rq pool[3]
  197. void __iomem *base;
  198. void __iomem *ctrl_base;
  199. struct sk_buff *tx_skb[TX_DESC_NUM];
  200. struct sk_buff *rx_skb[RX_DESC_NUM];
  201. struct device *dev;
  202. struct net_device *netdev;
  203. struct device_node *phy_node;
  204. phy_interface_t phy_mode;
  205. unsigned int speed;
  206. unsigned int duplex;
  207. struct clk *clk;
  208. struct mii_bus *bus;
  209. struct napi_struct napi;
  210. struct work_struct tx_timeout_task;
  211. };
  212. static void hix5hd2_config_port(struct net_device *dev, u32 speed, u32 duplex)
  213. {
  214. struct hix5hd2_priv *priv = netdev_priv(dev);
  215. u32 val;
  216. priv->speed = speed;
  217. priv->duplex = duplex;
  218. switch (priv->phy_mode) {
  219. case PHY_INTERFACE_MODE_RGMII:
  220. if (speed == SPEED_1000)
  221. val = RGMII_SPEED_1000;
  222. else if (speed == SPEED_100)
  223. val = RGMII_SPEED_100;
  224. else
  225. val = RGMII_SPEED_10;
  226. break;
  227. case PHY_INTERFACE_MODE_MII:
  228. if (speed == SPEED_100)
  229. val = MII_SPEED_100;
  230. else
  231. val = MII_SPEED_10;
  232. break;
  233. default:
  234. netdev_warn(dev, "not supported mode\n");
  235. val = MII_SPEED_10;
  236. break;
  237. }
  238. if (duplex)
  239. val |= GMAC_FULL_DUPLEX;
  240. writel_relaxed(val, priv->ctrl_base);
  241. writel_relaxed(BIT_MODE_CHANGE_EN, priv->base + MODE_CHANGE_EN);
  242. if (speed == SPEED_1000)
  243. val = GMAC_SPEED_1000;
  244. else if (speed == SPEED_100)
  245. val = GMAC_SPEED_100;
  246. else
  247. val = GMAC_SPEED_10;
  248. writel_relaxed(val, priv->base + PORT_MODE);
  249. writel_relaxed(0, priv->base + MODE_CHANGE_EN);
  250. writel_relaxed(duplex, priv->base + MAC_DUPLEX_HALF_CTRL);
  251. }
  252. static void hix5hd2_set_desc_depth(struct hix5hd2_priv *priv, int rx, int tx)
  253. {
  254. writel_relaxed(BITS_RX_FQ_DEPTH_EN, priv->base + RX_FQ_REG_EN);
  255. writel_relaxed(rx << 3, priv->base + RX_FQ_DEPTH);
  256. writel_relaxed(0, priv->base + RX_FQ_REG_EN);
  257. writel_relaxed(BITS_RX_BQ_DEPTH_EN, priv->base + RX_BQ_REG_EN);
  258. writel_relaxed(rx << 3, priv->base + RX_BQ_DEPTH);
  259. writel_relaxed(0, priv->base + RX_BQ_REG_EN);
  260. writel_relaxed(BITS_TX_BQ_DEPTH_EN, priv->base + TX_BQ_REG_EN);
  261. writel_relaxed(tx << 3, priv->base + TX_BQ_DEPTH);
  262. writel_relaxed(0, priv->base + TX_BQ_REG_EN);
  263. writel_relaxed(BITS_TX_RQ_DEPTH_EN, priv->base + TX_RQ_REG_EN);
  264. writel_relaxed(tx << 3, priv->base + TX_RQ_DEPTH);
  265. writel_relaxed(0, priv->base + TX_RQ_REG_EN);
  266. }
  267. static void hix5hd2_set_rx_fq(struct hix5hd2_priv *priv, dma_addr_t phy_addr)
  268. {
  269. writel_relaxed(BITS_RX_FQ_START_ADDR_EN, priv->base + RX_FQ_REG_EN);
  270. writel_relaxed(phy_addr, priv->base + RX_FQ_START_ADDR);
  271. writel_relaxed(0, priv->base + RX_FQ_REG_EN);
  272. }
  273. static void hix5hd2_set_rx_bq(struct hix5hd2_priv *priv, dma_addr_t phy_addr)
  274. {
  275. writel_relaxed(BITS_RX_BQ_START_ADDR_EN, priv->base + RX_BQ_REG_EN);
  276. writel_relaxed(phy_addr, priv->base + RX_BQ_START_ADDR);
  277. writel_relaxed(0, priv->base + RX_BQ_REG_EN);
  278. }
  279. static void hix5hd2_set_tx_bq(struct hix5hd2_priv *priv, dma_addr_t phy_addr)
  280. {
  281. writel_relaxed(BITS_TX_BQ_START_ADDR_EN, priv->base + TX_BQ_REG_EN);
  282. writel_relaxed(phy_addr, priv->base + TX_BQ_START_ADDR);
  283. writel_relaxed(0, priv->base + TX_BQ_REG_EN);
  284. }
  285. static void hix5hd2_set_tx_rq(struct hix5hd2_priv *priv, dma_addr_t phy_addr)
  286. {
  287. writel_relaxed(BITS_TX_RQ_START_ADDR_EN, priv->base + TX_RQ_REG_EN);
  288. writel_relaxed(phy_addr, priv->base + TX_RQ_START_ADDR);
  289. writel_relaxed(0, priv->base + TX_RQ_REG_EN);
  290. }
  291. static void hix5hd2_set_desc_addr(struct hix5hd2_priv *priv)
  292. {
  293. hix5hd2_set_rx_fq(priv, priv->rx_fq.phys_addr);
  294. hix5hd2_set_rx_bq(priv, priv->rx_bq.phys_addr);
  295. hix5hd2_set_tx_rq(priv, priv->tx_rq.phys_addr);
  296. hix5hd2_set_tx_bq(priv, priv->tx_bq.phys_addr);
  297. }
  298. static void hix5hd2_hw_init(struct hix5hd2_priv *priv)
  299. {
  300. u32 val;
  301. /* disable and clear all interrupts */
  302. writel_relaxed(0, priv->base + ENA_PMU_INT);
  303. writel_relaxed(~0, priv->base + RAW_PMU_INT);
  304. writel_relaxed(BIT_CRC_ERR_PASS, priv->base + REC_FILT_CONTROL);
  305. writel_relaxed(MAC_MAX_FRAME_SIZE, priv->base + CONTROL_WORD);
  306. writel_relaxed(0, priv->base + COL_SLOT_TIME);
  307. val = RX_BQ_INT_THRESHOLD | TX_RQ_INT_THRESHOLD << QUEUE_TX_BQ_SHIFT;
  308. writel_relaxed(val, priv->base + IN_QUEUE_TH);
  309. writel_relaxed(RX_BQ_IN_TIMEOUT, priv->base + RX_BQ_IN_TIMEOUT_TH);
  310. writel_relaxed(TX_RQ_IN_TIMEOUT, priv->base + TX_RQ_IN_TIMEOUT_TH);
  311. hix5hd2_set_desc_depth(priv, RX_DESC_NUM, TX_DESC_NUM);
  312. hix5hd2_set_desc_addr(priv);
  313. }
  314. static void hix5hd2_irq_enable(struct hix5hd2_priv *priv)
  315. {
  316. writel_relaxed(DEF_INT_MASK, priv->base + ENA_PMU_INT);
  317. }
  318. static void hix5hd2_irq_disable(struct hix5hd2_priv *priv)
  319. {
  320. writel_relaxed(0, priv->base + ENA_PMU_INT);
  321. }
  322. static void hix5hd2_port_enable(struct hix5hd2_priv *priv)
  323. {
  324. writel_relaxed(0xf, priv->base + DESC_WR_RD_ENA);
  325. writel_relaxed(BITS_RX_EN | BITS_TX_EN, priv->base + PORT_EN);
  326. }
  327. static void hix5hd2_port_disable(struct hix5hd2_priv *priv)
  328. {
  329. writel_relaxed(~(u32)(BITS_RX_EN | BITS_TX_EN), priv->base + PORT_EN);
  330. writel_relaxed(0, priv->base + DESC_WR_RD_ENA);
  331. }
  332. static void hix5hd2_hw_set_mac_addr(struct net_device *dev)
  333. {
  334. struct hix5hd2_priv *priv = netdev_priv(dev);
  335. unsigned char *mac = dev->dev_addr;
  336. u32 val;
  337. val = mac[1] | (mac[0] << 8);
  338. writel_relaxed(val, priv->base + STATION_ADDR_HIGH);
  339. val = mac[5] | (mac[4] << 8) | (mac[3] << 16) | (mac[2] << 24);
  340. writel_relaxed(val, priv->base + STATION_ADDR_LOW);
  341. }
  342. static int hix5hd2_net_set_mac_address(struct net_device *dev, void *p)
  343. {
  344. int ret;
  345. ret = eth_mac_addr(dev, p);
  346. if (!ret)
  347. hix5hd2_hw_set_mac_addr(dev);
  348. return ret;
  349. }
  350. static void hix5hd2_adjust_link(struct net_device *dev)
  351. {
  352. struct hix5hd2_priv *priv = netdev_priv(dev);
  353. struct phy_device *phy = dev->phydev;
  354. if ((priv->speed != phy->speed) || (priv->duplex != phy->duplex)) {
  355. hix5hd2_config_port(dev, phy->speed, phy->duplex);
  356. phy_print_status(phy);
  357. }
  358. }
  359. static void hix5hd2_rx_refill(struct hix5hd2_priv *priv)
  360. {
  361. struct hix5hd2_desc *desc;
  362. struct sk_buff *skb;
  363. u32 start, end, num, pos, i;
  364. u32 len = MAC_MAX_FRAME_SIZE;
  365. dma_addr_t addr;
  366. /* software write pointer */
  367. start = dma_cnt(readl_relaxed(priv->base + RX_FQ_WR_ADDR));
  368. /* logic read pointer */
  369. end = dma_cnt(readl_relaxed(priv->base + RX_FQ_RD_ADDR));
  370. num = CIRC_SPACE(start, end, RX_DESC_NUM);
  371. for (i = 0, pos = start; i < num; i++) {
  372. if (priv->rx_skb[pos]) {
  373. break;
  374. } else {
  375. skb = netdev_alloc_skb_ip_align(priv->netdev, len);
  376. if (unlikely(skb == NULL))
  377. break;
  378. }
  379. addr = dma_map_single(priv->dev, skb->data, len, DMA_FROM_DEVICE);
  380. if (dma_mapping_error(priv->dev, addr)) {
  381. dev_kfree_skb_any(skb);
  382. break;
  383. }
  384. desc = priv->rx_fq.desc + pos;
  385. desc->buff_addr = cpu_to_le32(addr);
  386. priv->rx_skb[pos] = skb;
  387. desc->cmd = cpu_to_le32(DESC_VLD_FREE |
  388. (len - 1) << DESC_BUFF_LEN_OFF);
  389. pos = dma_ring_incr(pos, RX_DESC_NUM);
  390. }
  391. /* ensure desc updated */
  392. wmb();
  393. if (pos != start)
  394. writel_relaxed(dma_byte(pos), priv->base + RX_FQ_WR_ADDR);
  395. }
  396. static int hix5hd2_rx(struct net_device *dev, int limit)
  397. {
  398. struct hix5hd2_priv *priv = netdev_priv(dev);
  399. struct sk_buff *skb;
  400. struct hix5hd2_desc *desc;
  401. dma_addr_t addr;
  402. u32 start, end, num, pos, i, len;
  403. /* software read pointer */
  404. start = dma_cnt(readl_relaxed(priv->base + RX_BQ_RD_ADDR));
  405. /* logic write pointer */
  406. end = dma_cnt(readl_relaxed(priv->base + RX_BQ_WR_ADDR));
  407. num = CIRC_CNT(end, start, RX_DESC_NUM);
  408. if (num > limit)
  409. num = limit;
  410. /* ensure get updated desc */
  411. rmb();
  412. for (i = 0, pos = start; i < num; i++) {
  413. skb = priv->rx_skb[pos];
  414. if (unlikely(!skb)) {
  415. netdev_err(dev, "inconsistent rx_skb\n");
  416. break;
  417. }
  418. priv->rx_skb[pos] = NULL;
  419. desc = priv->rx_bq.desc + pos;
  420. len = (le32_to_cpu(desc->cmd) >> DESC_DATA_LEN_OFF) &
  421. DESC_DATA_MASK;
  422. addr = le32_to_cpu(desc->buff_addr);
  423. dma_unmap_single(priv->dev, addr, MAC_MAX_FRAME_SIZE,
  424. DMA_FROM_DEVICE);
  425. skb_put(skb, len);
  426. if (skb->len > MAC_MAX_FRAME_SIZE) {
  427. netdev_err(dev, "rcv len err, len = %d\n", skb->len);
  428. dev->stats.rx_errors++;
  429. dev->stats.rx_length_errors++;
  430. dev_kfree_skb_any(skb);
  431. goto next;
  432. }
  433. skb->protocol = eth_type_trans(skb, dev);
  434. napi_gro_receive(&priv->napi, skb);
  435. dev->stats.rx_packets++;
  436. dev->stats.rx_bytes += skb->len;
  437. next:
  438. pos = dma_ring_incr(pos, RX_DESC_NUM);
  439. }
  440. if (pos != start)
  441. writel_relaxed(dma_byte(pos), priv->base + RX_BQ_RD_ADDR);
  442. hix5hd2_rx_refill(priv);
  443. return num;
  444. }
  445. static void hix5hd2_xmit_reclaim(struct net_device *dev)
  446. {
  447. struct sk_buff *skb;
  448. struct hix5hd2_desc *desc;
  449. struct hix5hd2_priv *priv = netdev_priv(dev);
  450. unsigned int bytes_compl = 0, pkts_compl = 0;
  451. u32 start, end, num, pos, i;
  452. dma_addr_t addr;
  453. netif_tx_lock(dev);
  454. /* software read */
  455. start = dma_cnt(readl_relaxed(priv->base + TX_RQ_RD_ADDR));
  456. /* logic write */
  457. end = dma_cnt(readl_relaxed(priv->base + TX_RQ_WR_ADDR));
  458. num = CIRC_CNT(end, start, TX_DESC_NUM);
  459. for (i = 0, pos = start; i < num; i++) {
  460. skb = priv->tx_skb[pos];
  461. if (unlikely(!skb)) {
  462. netdev_err(dev, "inconsistent tx_skb\n");
  463. break;
  464. }
  465. pkts_compl++;
  466. bytes_compl += skb->len;
  467. desc = priv->tx_rq.desc + pos;
  468. addr = le32_to_cpu(desc->buff_addr);
  469. dma_unmap_single(priv->dev, addr, skb->len, DMA_TO_DEVICE);
  470. priv->tx_skb[pos] = NULL;
  471. dev_consume_skb_any(skb);
  472. pos = dma_ring_incr(pos, TX_DESC_NUM);
  473. }
  474. if (pos != start)
  475. writel_relaxed(dma_byte(pos), priv->base + TX_RQ_RD_ADDR);
  476. netif_tx_unlock(dev);
  477. if (pkts_compl || bytes_compl)
  478. netdev_completed_queue(dev, pkts_compl, bytes_compl);
  479. if (unlikely(netif_queue_stopped(priv->netdev)) && pkts_compl)
  480. netif_wake_queue(priv->netdev);
  481. }
  482. static int hix5hd2_poll(struct napi_struct *napi, int budget)
  483. {
  484. struct hix5hd2_priv *priv = container_of(napi,
  485. struct hix5hd2_priv, napi);
  486. struct net_device *dev = priv->netdev;
  487. int work_done = 0, task = budget;
  488. int ints, num;
  489. do {
  490. hix5hd2_xmit_reclaim(dev);
  491. num = hix5hd2_rx(dev, task);
  492. work_done += num;
  493. task -= num;
  494. if ((work_done >= budget) || (num == 0))
  495. break;
  496. ints = readl_relaxed(priv->base + RAW_PMU_INT);
  497. writel_relaxed(ints, priv->base + RAW_PMU_INT);
  498. } while (ints & DEF_INT_MASK);
  499. if (work_done < budget) {
  500. napi_complete(napi);
  501. hix5hd2_irq_enable(priv);
  502. }
  503. return work_done;
  504. }
  505. static irqreturn_t hix5hd2_interrupt(int irq, void *dev_id)
  506. {
  507. struct net_device *dev = (struct net_device *)dev_id;
  508. struct hix5hd2_priv *priv = netdev_priv(dev);
  509. int ints = readl_relaxed(priv->base + RAW_PMU_INT);
  510. writel_relaxed(ints, priv->base + RAW_PMU_INT);
  511. if (likely(ints & DEF_INT_MASK)) {
  512. hix5hd2_irq_disable(priv);
  513. napi_schedule(&priv->napi);
  514. }
  515. return IRQ_HANDLED;
  516. }
  517. static int hix5hd2_net_xmit(struct sk_buff *skb, struct net_device *dev)
  518. {
  519. struct hix5hd2_priv *priv = netdev_priv(dev);
  520. struct hix5hd2_desc *desc;
  521. dma_addr_t addr;
  522. u32 pos;
  523. /* software write pointer */
  524. pos = dma_cnt(readl_relaxed(priv->base + TX_BQ_WR_ADDR));
  525. if (unlikely(priv->tx_skb[pos])) {
  526. dev->stats.tx_dropped++;
  527. dev->stats.tx_fifo_errors++;
  528. netif_stop_queue(dev);
  529. return NETDEV_TX_BUSY;
  530. }
  531. addr = dma_map_single(priv->dev, skb->data, skb->len, DMA_TO_DEVICE);
  532. if (dma_mapping_error(priv->dev, addr)) {
  533. dev_kfree_skb_any(skb);
  534. return NETDEV_TX_OK;
  535. }
  536. desc = priv->tx_bq.desc + pos;
  537. desc->buff_addr = cpu_to_le32(addr);
  538. priv->tx_skb[pos] = skb;
  539. desc->cmd = cpu_to_le32(DESC_VLD_BUSY | DESC_FL_FULL |
  540. (skb->len & DESC_DATA_MASK) << DESC_DATA_LEN_OFF |
  541. (skb->len & DESC_DATA_MASK) << DESC_BUFF_LEN_OFF);
  542. /* ensure desc updated */
  543. wmb();
  544. pos = dma_ring_incr(pos, TX_DESC_NUM);
  545. writel_relaxed(dma_byte(pos), priv->base + TX_BQ_WR_ADDR);
  546. netif_trans_update(dev);
  547. dev->stats.tx_packets++;
  548. dev->stats.tx_bytes += skb->len;
  549. netdev_sent_queue(dev, skb->len);
  550. return NETDEV_TX_OK;
  551. }
  552. static void hix5hd2_free_dma_desc_rings(struct hix5hd2_priv *priv)
  553. {
  554. struct hix5hd2_desc *desc;
  555. dma_addr_t addr;
  556. int i;
  557. for (i = 0; i < RX_DESC_NUM; i++) {
  558. struct sk_buff *skb = priv->rx_skb[i];
  559. if (skb == NULL)
  560. continue;
  561. desc = priv->rx_fq.desc + i;
  562. addr = le32_to_cpu(desc->buff_addr);
  563. dma_unmap_single(priv->dev, addr,
  564. MAC_MAX_FRAME_SIZE, DMA_FROM_DEVICE);
  565. dev_kfree_skb_any(skb);
  566. priv->rx_skb[i] = NULL;
  567. }
  568. for (i = 0; i < TX_DESC_NUM; i++) {
  569. struct sk_buff *skb = priv->tx_skb[i];
  570. if (skb == NULL)
  571. continue;
  572. desc = priv->tx_rq.desc + i;
  573. addr = le32_to_cpu(desc->buff_addr);
  574. dma_unmap_single(priv->dev, addr, skb->len, DMA_TO_DEVICE);
  575. dev_kfree_skb_any(skb);
  576. priv->tx_skb[i] = NULL;
  577. }
  578. }
  579. static int hix5hd2_net_open(struct net_device *dev)
  580. {
  581. struct hix5hd2_priv *priv = netdev_priv(dev);
  582. struct phy_device *phy;
  583. int ret;
  584. ret = clk_prepare_enable(priv->clk);
  585. if (ret < 0) {
  586. netdev_err(dev, "failed to enable clk %d\n", ret);
  587. return ret;
  588. }
  589. phy = of_phy_connect(dev, priv->phy_node,
  590. &hix5hd2_adjust_link, 0, priv->phy_mode);
  591. if (!phy)
  592. return -ENODEV;
  593. phy_start(phy);
  594. hix5hd2_hw_init(priv);
  595. hix5hd2_rx_refill(priv);
  596. netdev_reset_queue(dev);
  597. netif_start_queue(dev);
  598. napi_enable(&priv->napi);
  599. hix5hd2_port_enable(priv);
  600. hix5hd2_irq_enable(priv);
  601. return 0;
  602. }
  603. static int hix5hd2_net_close(struct net_device *dev)
  604. {
  605. struct hix5hd2_priv *priv = netdev_priv(dev);
  606. hix5hd2_port_disable(priv);
  607. hix5hd2_irq_disable(priv);
  608. napi_disable(&priv->napi);
  609. netif_stop_queue(dev);
  610. hix5hd2_free_dma_desc_rings(priv);
  611. if (dev->phydev) {
  612. phy_stop(dev->phydev);
  613. phy_disconnect(dev->phydev);
  614. }
  615. clk_disable_unprepare(priv->clk);
  616. return 0;
  617. }
  618. static void hix5hd2_tx_timeout_task(struct work_struct *work)
  619. {
  620. struct hix5hd2_priv *priv;
  621. priv = container_of(work, struct hix5hd2_priv, tx_timeout_task);
  622. hix5hd2_net_close(priv->netdev);
  623. hix5hd2_net_open(priv->netdev);
  624. }
  625. static void hix5hd2_net_timeout(struct net_device *dev)
  626. {
  627. struct hix5hd2_priv *priv = netdev_priv(dev);
  628. schedule_work(&priv->tx_timeout_task);
  629. }
  630. static const struct net_device_ops hix5hd2_netdev_ops = {
  631. .ndo_open = hix5hd2_net_open,
  632. .ndo_stop = hix5hd2_net_close,
  633. .ndo_start_xmit = hix5hd2_net_xmit,
  634. .ndo_tx_timeout = hix5hd2_net_timeout,
  635. .ndo_set_mac_address = hix5hd2_net_set_mac_address,
  636. };
  637. static const struct ethtool_ops hix5hd2_ethtools_ops = {
  638. .get_link = ethtool_op_get_link,
  639. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  640. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  641. };
  642. static int hix5hd2_mdio_wait_ready(struct mii_bus *bus)
  643. {
  644. struct hix5hd2_priv *priv = bus->priv;
  645. void __iomem *base = priv->base;
  646. int i, timeout = 10000;
  647. for (i = 0; readl_relaxed(base + MDIO_SINGLE_CMD) & MDIO_START; i++) {
  648. if (i == timeout)
  649. return -ETIMEDOUT;
  650. usleep_range(10, 20);
  651. }
  652. return 0;
  653. }
  654. static int hix5hd2_mdio_read(struct mii_bus *bus, int phy, int reg)
  655. {
  656. struct hix5hd2_priv *priv = bus->priv;
  657. void __iomem *base = priv->base;
  658. int val, ret;
  659. ret = hix5hd2_mdio_wait_ready(bus);
  660. if (ret < 0)
  661. goto out;
  662. writel_relaxed(MDIO_READ | phy << 8 | reg, base + MDIO_SINGLE_CMD);
  663. ret = hix5hd2_mdio_wait_ready(bus);
  664. if (ret < 0)
  665. goto out;
  666. val = readl_relaxed(base + MDIO_RDATA_STATUS);
  667. if (val & MDIO_R_VALID) {
  668. dev_err(bus->parent, "SMI bus read not valid\n");
  669. ret = -ENODEV;
  670. goto out;
  671. }
  672. val = readl_relaxed(priv->base + MDIO_SINGLE_DATA);
  673. ret = (val >> 16) & 0xFFFF;
  674. out:
  675. return ret;
  676. }
  677. static int hix5hd2_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
  678. {
  679. struct hix5hd2_priv *priv = bus->priv;
  680. void __iomem *base = priv->base;
  681. int ret;
  682. ret = hix5hd2_mdio_wait_ready(bus);
  683. if (ret < 0)
  684. goto out;
  685. writel_relaxed(val, base + MDIO_SINGLE_DATA);
  686. writel_relaxed(MDIO_WRITE | phy << 8 | reg, base + MDIO_SINGLE_CMD);
  687. ret = hix5hd2_mdio_wait_ready(bus);
  688. out:
  689. return ret;
  690. }
  691. static void hix5hd2_destroy_hw_desc_queue(struct hix5hd2_priv *priv)
  692. {
  693. int i;
  694. for (i = 0; i < QUEUE_NUMS; i++) {
  695. if (priv->pool[i].desc) {
  696. dma_free_coherent(priv->dev, priv->pool[i].size,
  697. priv->pool[i].desc,
  698. priv->pool[i].phys_addr);
  699. priv->pool[i].desc = NULL;
  700. }
  701. }
  702. }
  703. static int hix5hd2_init_hw_desc_queue(struct hix5hd2_priv *priv)
  704. {
  705. struct device *dev = priv->dev;
  706. struct hix5hd2_desc *virt_addr;
  707. dma_addr_t phys_addr;
  708. int size, i;
  709. priv->rx_fq.count = RX_DESC_NUM;
  710. priv->rx_bq.count = RX_DESC_NUM;
  711. priv->tx_bq.count = TX_DESC_NUM;
  712. priv->tx_rq.count = TX_DESC_NUM;
  713. for (i = 0; i < QUEUE_NUMS; i++) {
  714. size = priv->pool[i].count * sizeof(struct hix5hd2_desc);
  715. virt_addr = dma_alloc_coherent(dev, size, &phys_addr,
  716. GFP_KERNEL);
  717. if (virt_addr == NULL)
  718. goto error_free_pool;
  719. memset(virt_addr, 0, size);
  720. priv->pool[i].size = size;
  721. priv->pool[i].desc = virt_addr;
  722. priv->pool[i].phys_addr = phys_addr;
  723. }
  724. return 0;
  725. error_free_pool:
  726. hix5hd2_destroy_hw_desc_queue(priv);
  727. return -ENOMEM;
  728. }
  729. static int hix5hd2_dev_probe(struct platform_device *pdev)
  730. {
  731. struct device *dev = &pdev->dev;
  732. struct device_node *node = dev->of_node;
  733. struct net_device *ndev;
  734. struct hix5hd2_priv *priv;
  735. struct resource *res;
  736. struct mii_bus *bus;
  737. const char *mac_addr;
  738. int ret;
  739. ndev = alloc_etherdev(sizeof(struct hix5hd2_priv));
  740. if (!ndev)
  741. return -ENOMEM;
  742. platform_set_drvdata(pdev, ndev);
  743. priv = netdev_priv(ndev);
  744. priv->dev = dev;
  745. priv->netdev = ndev;
  746. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  747. priv->base = devm_ioremap_resource(dev, res);
  748. if (IS_ERR(priv->base)) {
  749. ret = PTR_ERR(priv->base);
  750. goto out_free_netdev;
  751. }
  752. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  753. priv->ctrl_base = devm_ioremap_resource(dev, res);
  754. if (IS_ERR(priv->ctrl_base)) {
  755. ret = PTR_ERR(priv->ctrl_base);
  756. goto out_free_netdev;
  757. }
  758. priv->clk = devm_clk_get(&pdev->dev, NULL);
  759. if (IS_ERR(priv->clk)) {
  760. netdev_err(ndev, "failed to get clk\n");
  761. ret = -ENODEV;
  762. goto out_free_netdev;
  763. }
  764. ret = clk_prepare_enable(priv->clk);
  765. if (ret < 0) {
  766. netdev_err(ndev, "failed to enable clk %d\n", ret);
  767. goto out_free_netdev;
  768. }
  769. bus = mdiobus_alloc();
  770. if (bus == NULL) {
  771. ret = -ENOMEM;
  772. goto out_free_netdev;
  773. }
  774. bus->priv = priv;
  775. bus->name = "hix5hd2_mii_bus";
  776. bus->read = hix5hd2_mdio_read;
  777. bus->write = hix5hd2_mdio_write;
  778. bus->parent = &pdev->dev;
  779. snprintf(bus->id, MII_BUS_ID_SIZE, "%s-mii", dev_name(&pdev->dev));
  780. priv->bus = bus;
  781. ret = of_mdiobus_register(bus, node);
  782. if (ret)
  783. goto err_free_mdio;
  784. priv->phy_mode = of_get_phy_mode(node);
  785. if (priv->phy_mode < 0) {
  786. netdev_err(ndev, "not find phy-mode\n");
  787. ret = -EINVAL;
  788. goto err_mdiobus;
  789. }
  790. priv->phy_node = of_parse_phandle(node, "phy-handle", 0);
  791. if (!priv->phy_node) {
  792. netdev_err(ndev, "not find phy-handle\n");
  793. ret = -EINVAL;
  794. goto err_mdiobus;
  795. }
  796. ndev->irq = platform_get_irq(pdev, 0);
  797. if (ndev->irq <= 0) {
  798. netdev_err(ndev, "No irq resource\n");
  799. ret = -EINVAL;
  800. goto out_phy_node;
  801. }
  802. ret = devm_request_irq(dev, ndev->irq, hix5hd2_interrupt,
  803. 0, pdev->name, ndev);
  804. if (ret) {
  805. netdev_err(ndev, "devm_request_irq failed\n");
  806. goto out_phy_node;
  807. }
  808. mac_addr = of_get_mac_address(node);
  809. if (mac_addr)
  810. ether_addr_copy(ndev->dev_addr, mac_addr);
  811. if (!is_valid_ether_addr(ndev->dev_addr)) {
  812. eth_hw_addr_random(ndev);
  813. netdev_warn(ndev, "using random MAC address %pM\n",
  814. ndev->dev_addr);
  815. }
  816. INIT_WORK(&priv->tx_timeout_task, hix5hd2_tx_timeout_task);
  817. ndev->watchdog_timeo = 6 * HZ;
  818. ndev->priv_flags |= IFF_UNICAST_FLT;
  819. ndev->netdev_ops = &hix5hd2_netdev_ops;
  820. ndev->ethtool_ops = &hix5hd2_ethtools_ops;
  821. SET_NETDEV_DEV(ndev, dev);
  822. ret = hix5hd2_init_hw_desc_queue(priv);
  823. if (ret)
  824. goto out_phy_node;
  825. netif_napi_add(ndev, &priv->napi, hix5hd2_poll, NAPI_POLL_WEIGHT);
  826. ret = register_netdev(priv->netdev);
  827. if (ret) {
  828. netdev_err(ndev, "register_netdev failed!");
  829. goto out_destroy_queue;
  830. }
  831. clk_disable_unprepare(priv->clk);
  832. return ret;
  833. out_destroy_queue:
  834. netif_napi_del(&priv->napi);
  835. hix5hd2_destroy_hw_desc_queue(priv);
  836. out_phy_node:
  837. of_node_put(priv->phy_node);
  838. err_mdiobus:
  839. mdiobus_unregister(bus);
  840. err_free_mdio:
  841. mdiobus_free(bus);
  842. out_free_netdev:
  843. free_netdev(ndev);
  844. return ret;
  845. }
  846. static int hix5hd2_dev_remove(struct platform_device *pdev)
  847. {
  848. struct net_device *ndev = platform_get_drvdata(pdev);
  849. struct hix5hd2_priv *priv = netdev_priv(ndev);
  850. netif_napi_del(&priv->napi);
  851. unregister_netdev(ndev);
  852. mdiobus_unregister(priv->bus);
  853. mdiobus_free(priv->bus);
  854. hix5hd2_destroy_hw_desc_queue(priv);
  855. of_node_put(priv->phy_node);
  856. cancel_work_sync(&priv->tx_timeout_task);
  857. free_netdev(ndev);
  858. return 0;
  859. }
  860. static const struct of_device_id hix5hd2_of_match[] = {
  861. {.compatible = "hisilicon,hix5hd2-gmac",},
  862. {},
  863. };
  864. MODULE_DEVICE_TABLE(of, hix5hd2_of_match);
  865. static struct platform_driver hix5hd2_dev_driver = {
  866. .driver = {
  867. .name = "hix5hd2-gmac",
  868. .of_match_table = hix5hd2_of_match,
  869. },
  870. .probe = hix5hd2_dev_probe,
  871. .remove = hix5hd2_dev_remove,
  872. };
  873. module_platform_driver(hix5hd2_dev_driver);
  874. MODULE_DESCRIPTION("HISILICON HIX5HD2 Ethernet driver");
  875. MODULE_LICENSE("GPL v2");
  876. MODULE_ALIAS("platform:hix5hd2-gmac");