hix5hd2_gmac.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  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_device.h>
  14. #include <linux/of_net.h>
  15. #include <linux/of_mdio.h>
  16. #include <linux/reset.h>
  17. #include <linux/clk.h>
  18. #include <linux/circ_buf.h>
  19. #define STATION_ADDR_LOW 0x0000
  20. #define STATION_ADDR_HIGH 0x0004
  21. #define MAC_DUPLEX_HALF_CTRL 0x0008
  22. #define MAX_FRM_SIZE 0x003c
  23. #define PORT_MODE 0x0040
  24. #define PORT_EN 0x0044
  25. #define BITS_TX_EN BIT(2)
  26. #define BITS_RX_EN BIT(1)
  27. #define REC_FILT_CONTROL 0x0064
  28. #define BIT_CRC_ERR_PASS BIT(5)
  29. #define BIT_PAUSE_FRM_PASS BIT(4)
  30. #define BIT_VLAN_DROP_EN BIT(3)
  31. #define BIT_BC_DROP_EN BIT(2)
  32. #define BIT_MC_MATCH_EN BIT(1)
  33. #define BIT_UC_MATCH_EN BIT(0)
  34. #define PORT_MC_ADDR_LOW 0x0068
  35. #define PORT_MC_ADDR_HIGH 0x006C
  36. #define CF_CRC_STRIP 0x01b0
  37. #define MODE_CHANGE_EN 0x01b4
  38. #define BIT_MODE_CHANGE_EN BIT(0)
  39. #define COL_SLOT_TIME 0x01c0
  40. #define RECV_CONTROL 0x01e0
  41. #define BIT_STRIP_PAD_EN BIT(3)
  42. #define BIT_RUNT_PKT_EN BIT(4)
  43. #define CONTROL_WORD 0x0214
  44. #define MDIO_SINGLE_CMD 0x03c0
  45. #define MDIO_SINGLE_DATA 0x03c4
  46. #define MDIO_CTRL 0x03cc
  47. #define MDIO_RDATA_STATUS 0x03d0
  48. #define MDIO_START BIT(20)
  49. #define MDIO_R_VALID BIT(0)
  50. #define MDIO_READ (BIT(17) | MDIO_START)
  51. #define MDIO_WRITE (BIT(16) | MDIO_START)
  52. #define RX_FQ_START_ADDR 0x0500
  53. #define RX_FQ_DEPTH 0x0504
  54. #define RX_FQ_WR_ADDR 0x0508
  55. #define RX_FQ_RD_ADDR 0x050c
  56. #define RX_FQ_VLDDESC_CNT 0x0510
  57. #define RX_FQ_ALEMPTY_TH 0x0514
  58. #define RX_FQ_REG_EN 0x0518
  59. #define BITS_RX_FQ_START_ADDR_EN BIT(2)
  60. #define BITS_RX_FQ_DEPTH_EN BIT(1)
  61. #define BITS_RX_FQ_RD_ADDR_EN BIT(0)
  62. #define RX_FQ_ALFULL_TH 0x051c
  63. #define RX_BQ_START_ADDR 0x0520
  64. #define RX_BQ_DEPTH 0x0524
  65. #define RX_BQ_WR_ADDR 0x0528
  66. #define RX_BQ_RD_ADDR 0x052c
  67. #define RX_BQ_FREE_DESC_CNT 0x0530
  68. #define RX_BQ_ALEMPTY_TH 0x0534
  69. #define RX_BQ_REG_EN 0x0538
  70. #define BITS_RX_BQ_START_ADDR_EN BIT(2)
  71. #define BITS_RX_BQ_DEPTH_EN BIT(1)
  72. #define BITS_RX_BQ_WR_ADDR_EN BIT(0)
  73. #define RX_BQ_ALFULL_TH 0x053c
  74. #define TX_BQ_START_ADDR 0x0580
  75. #define TX_BQ_DEPTH 0x0584
  76. #define TX_BQ_WR_ADDR 0x0588
  77. #define TX_BQ_RD_ADDR 0x058c
  78. #define TX_BQ_VLDDESC_CNT 0x0590
  79. #define TX_BQ_ALEMPTY_TH 0x0594
  80. #define TX_BQ_REG_EN 0x0598
  81. #define BITS_TX_BQ_START_ADDR_EN BIT(2)
  82. #define BITS_TX_BQ_DEPTH_EN BIT(1)
  83. #define BITS_TX_BQ_RD_ADDR_EN BIT(0)
  84. #define TX_BQ_ALFULL_TH 0x059c
  85. #define TX_RQ_START_ADDR 0x05a0
  86. #define TX_RQ_DEPTH 0x05a4
  87. #define TX_RQ_WR_ADDR 0x05a8
  88. #define TX_RQ_RD_ADDR 0x05ac
  89. #define TX_RQ_FREE_DESC_CNT 0x05b0
  90. #define TX_RQ_ALEMPTY_TH 0x05b4
  91. #define TX_RQ_REG_EN 0x05b8
  92. #define BITS_TX_RQ_START_ADDR_EN BIT(2)
  93. #define BITS_TX_RQ_DEPTH_EN BIT(1)
  94. #define BITS_TX_RQ_WR_ADDR_EN BIT(0)
  95. #define TX_RQ_ALFULL_TH 0x05bc
  96. #define RAW_PMU_INT 0x05c0
  97. #define ENA_PMU_INT 0x05c4
  98. #define STATUS_PMU_INT 0x05c8
  99. #define MAC_FIFO_ERR_IN BIT(30)
  100. #define TX_RQ_IN_TIMEOUT_INT BIT(29)
  101. #define RX_BQ_IN_TIMEOUT_INT BIT(28)
  102. #define TXOUTCFF_FULL_INT BIT(27)
  103. #define TXOUTCFF_EMPTY_INT BIT(26)
  104. #define TXCFF_FULL_INT BIT(25)
  105. #define TXCFF_EMPTY_INT BIT(24)
  106. #define RXOUTCFF_FULL_INT BIT(23)
  107. #define RXOUTCFF_EMPTY_INT BIT(22)
  108. #define RXCFF_FULL_INT BIT(21)
  109. #define RXCFF_EMPTY_INT BIT(20)
  110. #define TX_RQ_IN_INT BIT(19)
  111. #define TX_BQ_OUT_INT BIT(18)
  112. #define RX_BQ_IN_INT BIT(17)
  113. #define RX_FQ_OUT_INT BIT(16)
  114. #define TX_RQ_EMPTY_INT BIT(15)
  115. #define TX_RQ_FULL_INT BIT(14)
  116. #define TX_RQ_ALEMPTY_INT BIT(13)
  117. #define TX_RQ_ALFULL_INT BIT(12)
  118. #define TX_BQ_EMPTY_INT BIT(11)
  119. #define TX_BQ_FULL_INT BIT(10)
  120. #define TX_BQ_ALEMPTY_INT BIT(9)
  121. #define TX_BQ_ALFULL_INT BIT(8)
  122. #define RX_BQ_EMPTY_INT BIT(7)
  123. #define RX_BQ_FULL_INT BIT(6)
  124. #define RX_BQ_ALEMPTY_INT BIT(5)
  125. #define RX_BQ_ALFULL_INT BIT(4)
  126. #define RX_FQ_EMPTY_INT BIT(3)
  127. #define RX_FQ_FULL_INT BIT(2)
  128. #define RX_FQ_ALEMPTY_INT BIT(1)
  129. #define RX_FQ_ALFULL_INT BIT(0)
  130. #define DEF_INT_MASK (RX_BQ_IN_INT | RX_BQ_IN_TIMEOUT_INT | \
  131. TX_RQ_IN_INT | TX_RQ_IN_TIMEOUT_INT)
  132. #define DESC_WR_RD_ENA 0x05cc
  133. #define IN_QUEUE_TH 0x05d8
  134. #define OUT_QUEUE_TH 0x05dc
  135. #define QUEUE_TX_BQ_SHIFT 16
  136. #define RX_BQ_IN_TIMEOUT_TH 0x05e0
  137. #define TX_RQ_IN_TIMEOUT_TH 0x05e4
  138. #define STOP_CMD 0x05e8
  139. #define BITS_TX_STOP BIT(1)
  140. #define BITS_RX_STOP BIT(0)
  141. #define FLUSH_CMD 0x05eC
  142. #define BITS_TX_FLUSH_CMD BIT(5)
  143. #define BITS_RX_FLUSH_CMD BIT(4)
  144. #define BITS_TX_FLUSH_FLAG_DOWN BIT(3)
  145. #define BITS_TX_FLUSH_FLAG_UP BIT(2)
  146. #define BITS_RX_FLUSH_FLAG_DOWN BIT(1)
  147. #define BITS_RX_FLUSH_FLAG_UP BIT(0)
  148. #define RX_CFF_NUM_REG 0x05f0
  149. #define PMU_FSM_REG 0x05f8
  150. #define RX_FIFO_PKT_IN_NUM 0x05fc
  151. #define RX_FIFO_PKT_OUT_NUM 0x0600
  152. #define RGMII_SPEED_1000 0x2c
  153. #define RGMII_SPEED_100 0x2f
  154. #define RGMII_SPEED_10 0x2d
  155. #define MII_SPEED_100 0x0f
  156. #define MII_SPEED_10 0x0d
  157. #define GMAC_SPEED_1000 0x05
  158. #define GMAC_SPEED_100 0x01
  159. #define GMAC_SPEED_10 0x00
  160. #define GMAC_FULL_DUPLEX BIT(4)
  161. #define RX_BQ_INT_THRESHOLD 0x01
  162. #define TX_RQ_INT_THRESHOLD 0x01
  163. #define RX_BQ_IN_TIMEOUT 0x10000
  164. #define TX_RQ_IN_TIMEOUT 0x50000
  165. #define MAC_MAX_FRAME_SIZE 1600
  166. #define DESC_SIZE 32
  167. #define RX_DESC_NUM 1024
  168. #define TX_DESC_NUM 1024
  169. #define DESC_VLD_FREE 0
  170. #define DESC_VLD_BUSY 0x80000000
  171. #define DESC_FL_MID 0
  172. #define DESC_FL_LAST 0x20000000
  173. #define DESC_FL_FIRST 0x40000000
  174. #define DESC_FL_FULL 0x60000000
  175. #define DESC_DATA_LEN_OFF 16
  176. #define DESC_BUFF_LEN_OFF 0
  177. #define DESC_DATA_MASK 0x7ff
  178. #define DESC_SG BIT(30)
  179. #define DESC_FRAGS_NUM_OFF 11
  180. /* DMA descriptor ring helpers */
  181. #define dma_ring_incr(n, s) (((n) + 1) & ((s) - 1))
  182. #define dma_cnt(n) ((n) >> 5)
  183. #define dma_byte(n) ((n) << 5)
  184. #define HW_CAP_TSO BIT(0)
  185. #define GEMAC_V1 0
  186. #define GEMAC_V2 (GEMAC_V1 | HW_CAP_TSO)
  187. #define HAS_CAP_TSO(hw_cap) ((hw_cap) & HW_CAP_TSO)
  188. #define PHY_RESET_DELAYS_PROPERTY "hisilicon,phy-reset-delays-us"
  189. enum phy_reset_delays {
  190. PRE_DELAY,
  191. PULSE,
  192. POST_DELAY,
  193. DELAYS_NUM,
  194. };
  195. struct hix5hd2_desc {
  196. __le32 buff_addr;
  197. __le32 cmd;
  198. } __aligned(32);
  199. struct hix5hd2_desc_sw {
  200. struct hix5hd2_desc *desc;
  201. dma_addr_t phys_addr;
  202. unsigned int count;
  203. unsigned int size;
  204. };
  205. struct hix5hd2_sg_desc_ring {
  206. struct sg_desc *desc;
  207. dma_addr_t phys_addr;
  208. };
  209. struct frags_info {
  210. __le32 addr;
  211. __le32 size;
  212. };
  213. /* hardware supported max skb frags num */
  214. #define SG_MAX_SKB_FRAGS 17
  215. struct sg_desc {
  216. __le32 total_len;
  217. __le32 resvd0;
  218. __le32 linear_addr;
  219. __le32 linear_len;
  220. /* reserve one more frags for memory alignment */
  221. struct frags_info frags[SG_MAX_SKB_FRAGS + 1];
  222. };
  223. #define QUEUE_NUMS 4
  224. struct hix5hd2_priv {
  225. struct hix5hd2_desc_sw pool[QUEUE_NUMS];
  226. #define rx_fq pool[0]
  227. #define rx_bq pool[1]
  228. #define tx_bq pool[2]
  229. #define tx_rq pool[3]
  230. struct hix5hd2_sg_desc_ring tx_ring;
  231. void __iomem *base;
  232. void __iomem *ctrl_base;
  233. struct sk_buff *tx_skb[TX_DESC_NUM];
  234. struct sk_buff *rx_skb[RX_DESC_NUM];
  235. struct device *dev;
  236. struct net_device *netdev;
  237. struct device_node *phy_node;
  238. phy_interface_t phy_mode;
  239. unsigned long hw_cap;
  240. unsigned int speed;
  241. unsigned int duplex;
  242. struct clk *mac_core_clk;
  243. struct clk *mac_ifc_clk;
  244. struct reset_control *mac_core_rst;
  245. struct reset_control *mac_ifc_rst;
  246. struct reset_control *phy_rst;
  247. u32 phy_reset_delays[DELAYS_NUM];
  248. struct mii_bus *bus;
  249. struct napi_struct napi;
  250. struct work_struct tx_timeout_task;
  251. };
  252. static inline void hix5hd2_mac_interface_reset(struct hix5hd2_priv *priv)
  253. {
  254. if (!priv->mac_ifc_rst)
  255. return;
  256. reset_control_assert(priv->mac_ifc_rst);
  257. reset_control_deassert(priv->mac_ifc_rst);
  258. }
  259. static void hix5hd2_config_port(struct net_device *dev, u32 speed, u32 duplex)
  260. {
  261. struct hix5hd2_priv *priv = netdev_priv(dev);
  262. u32 val;
  263. priv->speed = speed;
  264. priv->duplex = duplex;
  265. switch (priv->phy_mode) {
  266. case PHY_INTERFACE_MODE_RGMII:
  267. if (speed == SPEED_1000)
  268. val = RGMII_SPEED_1000;
  269. else if (speed == SPEED_100)
  270. val = RGMII_SPEED_100;
  271. else
  272. val = RGMII_SPEED_10;
  273. break;
  274. case PHY_INTERFACE_MODE_MII:
  275. if (speed == SPEED_100)
  276. val = MII_SPEED_100;
  277. else
  278. val = MII_SPEED_10;
  279. break;
  280. default:
  281. netdev_warn(dev, "not supported mode\n");
  282. val = MII_SPEED_10;
  283. break;
  284. }
  285. if (duplex)
  286. val |= GMAC_FULL_DUPLEX;
  287. writel_relaxed(val, priv->ctrl_base);
  288. hix5hd2_mac_interface_reset(priv);
  289. writel_relaxed(BIT_MODE_CHANGE_EN, priv->base + MODE_CHANGE_EN);
  290. if (speed == SPEED_1000)
  291. val = GMAC_SPEED_1000;
  292. else if (speed == SPEED_100)
  293. val = GMAC_SPEED_100;
  294. else
  295. val = GMAC_SPEED_10;
  296. writel_relaxed(val, priv->base + PORT_MODE);
  297. writel_relaxed(0, priv->base + MODE_CHANGE_EN);
  298. writel_relaxed(duplex, priv->base + MAC_DUPLEX_HALF_CTRL);
  299. }
  300. static void hix5hd2_set_desc_depth(struct hix5hd2_priv *priv, int rx, int tx)
  301. {
  302. writel_relaxed(BITS_RX_FQ_DEPTH_EN, priv->base + RX_FQ_REG_EN);
  303. writel_relaxed(rx << 3, priv->base + RX_FQ_DEPTH);
  304. writel_relaxed(0, priv->base + RX_FQ_REG_EN);
  305. writel_relaxed(BITS_RX_BQ_DEPTH_EN, priv->base + RX_BQ_REG_EN);
  306. writel_relaxed(rx << 3, priv->base + RX_BQ_DEPTH);
  307. writel_relaxed(0, priv->base + RX_BQ_REG_EN);
  308. writel_relaxed(BITS_TX_BQ_DEPTH_EN, priv->base + TX_BQ_REG_EN);
  309. writel_relaxed(tx << 3, priv->base + TX_BQ_DEPTH);
  310. writel_relaxed(0, priv->base + TX_BQ_REG_EN);
  311. writel_relaxed(BITS_TX_RQ_DEPTH_EN, priv->base + TX_RQ_REG_EN);
  312. writel_relaxed(tx << 3, priv->base + TX_RQ_DEPTH);
  313. writel_relaxed(0, priv->base + TX_RQ_REG_EN);
  314. }
  315. static void hix5hd2_set_rx_fq(struct hix5hd2_priv *priv, dma_addr_t phy_addr)
  316. {
  317. writel_relaxed(BITS_RX_FQ_START_ADDR_EN, priv->base + RX_FQ_REG_EN);
  318. writel_relaxed(phy_addr, priv->base + RX_FQ_START_ADDR);
  319. writel_relaxed(0, priv->base + RX_FQ_REG_EN);
  320. }
  321. static void hix5hd2_set_rx_bq(struct hix5hd2_priv *priv, dma_addr_t phy_addr)
  322. {
  323. writel_relaxed(BITS_RX_BQ_START_ADDR_EN, priv->base + RX_BQ_REG_EN);
  324. writel_relaxed(phy_addr, priv->base + RX_BQ_START_ADDR);
  325. writel_relaxed(0, priv->base + RX_BQ_REG_EN);
  326. }
  327. static void hix5hd2_set_tx_bq(struct hix5hd2_priv *priv, dma_addr_t phy_addr)
  328. {
  329. writel_relaxed(BITS_TX_BQ_START_ADDR_EN, priv->base + TX_BQ_REG_EN);
  330. writel_relaxed(phy_addr, priv->base + TX_BQ_START_ADDR);
  331. writel_relaxed(0, priv->base + TX_BQ_REG_EN);
  332. }
  333. static void hix5hd2_set_tx_rq(struct hix5hd2_priv *priv, dma_addr_t phy_addr)
  334. {
  335. writel_relaxed(BITS_TX_RQ_START_ADDR_EN, priv->base + TX_RQ_REG_EN);
  336. writel_relaxed(phy_addr, priv->base + TX_RQ_START_ADDR);
  337. writel_relaxed(0, priv->base + TX_RQ_REG_EN);
  338. }
  339. static void hix5hd2_set_desc_addr(struct hix5hd2_priv *priv)
  340. {
  341. hix5hd2_set_rx_fq(priv, priv->rx_fq.phys_addr);
  342. hix5hd2_set_rx_bq(priv, priv->rx_bq.phys_addr);
  343. hix5hd2_set_tx_rq(priv, priv->tx_rq.phys_addr);
  344. hix5hd2_set_tx_bq(priv, priv->tx_bq.phys_addr);
  345. }
  346. static void hix5hd2_hw_init(struct hix5hd2_priv *priv)
  347. {
  348. u32 val;
  349. /* disable and clear all interrupts */
  350. writel_relaxed(0, priv->base + ENA_PMU_INT);
  351. writel_relaxed(~0, priv->base + RAW_PMU_INT);
  352. writel_relaxed(BIT_CRC_ERR_PASS, priv->base + REC_FILT_CONTROL);
  353. writel_relaxed(MAC_MAX_FRAME_SIZE, priv->base + CONTROL_WORD);
  354. writel_relaxed(0, priv->base + COL_SLOT_TIME);
  355. val = RX_BQ_INT_THRESHOLD | TX_RQ_INT_THRESHOLD << QUEUE_TX_BQ_SHIFT;
  356. writel_relaxed(val, priv->base + IN_QUEUE_TH);
  357. writel_relaxed(RX_BQ_IN_TIMEOUT, priv->base + RX_BQ_IN_TIMEOUT_TH);
  358. writel_relaxed(TX_RQ_IN_TIMEOUT, priv->base + TX_RQ_IN_TIMEOUT_TH);
  359. hix5hd2_set_desc_depth(priv, RX_DESC_NUM, TX_DESC_NUM);
  360. hix5hd2_set_desc_addr(priv);
  361. }
  362. static void hix5hd2_irq_enable(struct hix5hd2_priv *priv)
  363. {
  364. writel_relaxed(DEF_INT_MASK, priv->base + ENA_PMU_INT);
  365. }
  366. static void hix5hd2_irq_disable(struct hix5hd2_priv *priv)
  367. {
  368. writel_relaxed(0, priv->base + ENA_PMU_INT);
  369. }
  370. static void hix5hd2_port_enable(struct hix5hd2_priv *priv)
  371. {
  372. writel_relaxed(0xf, priv->base + DESC_WR_RD_ENA);
  373. writel_relaxed(BITS_RX_EN | BITS_TX_EN, priv->base + PORT_EN);
  374. }
  375. static void hix5hd2_port_disable(struct hix5hd2_priv *priv)
  376. {
  377. writel_relaxed(~(u32)(BITS_RX_EN | BITS_TX_EN), priv->base + PORT_EN);
  378. writel_relaxed(0, priv->base + DESC_WR_RD_ENA);
  379. }
  380. static void hix5hd2_hw_set_mac_addr(struct net_device *dev)
  381. {
  382. struct hix5hd2_priv *priv = netdev_priv(dev);
  383. unsigned char *mac = dev->dev_addr;
  384. u32 val;
  385. val = mac[1] | (mac[0] << 8);
  386. writel_relaxed(val, priv->base + STATION_ADDR_HIGH);
  387. val = mac[5] | (mac[4] << 8) | (mac[3] << 16) | (mac[2] << 24);
  388. writel_relaxed(val, priv->base + STATION_ADDR_LOW);
  389. }
  390. static int hix5hd2_net_set_mac_address(struct net_device *dev, void *p)
  391. {
  392. int ret;
  393. ret = eth_mac_addr(dev, p);
  394. if (!ret)
  395. hix5hd2_hw_set_mac_addr(dev);
  396. return ret;
  397. }
  398. static void hix5hd2_adjust_link(struct net_device *dev)
  399. {
  400. struct hix5hd2_priv *priv = netdev_priv(dev);
  401. struct phy_device *phy = dev->phydev;
  402. if ((priv->speed != phy->speed) || (priv->duplex != phy->duplex)) {
  403. hix5hd2_config_port(dev, phy->speed, phy->duplex);
  404. phy_print_status(phy);
  405. }
  406. }
  407. static void hix5hd2_rx_refill(struct hix5hd2_priv *priv)
  408. {
  409. struct hix5hd2_desc *desc;
  410. struct sk_buff *skb;
  411. u32 start, end, num, pos, i;
  412. u32 len = MAC_MAX_FRAME_SIZE;
  413. dma_addr_t addr;
  414. /* software write pointer */
  415. start = dma_cnt(readl_relaxed(priv->base + RX_FQ_WR_ADDR));
  416. /* logic read pointer */
  417. end = dma_cnt(readl_relaxed(priv->base + RX_FQ_RD_ADDR));
  418. num = CIRC_SPACE(start, end, RX_DESC_NUM);
  419. for (i = 0, pos = start; i < num; i++) {
  420. if (priv->rx_skb[pos]) {
  421. break;
  422. } else {
  423. skb = netdev_alloc_skb_ip_align(priv->netdev, len);
  424. if (unlikely(skb == NULL))
  425. break;
  426. }
  427. addr = dma_map_single(priv->dev, skb->data, len, DMA_FROM_DEVICE);
  428. if (dma_mapping_error(priv->dev, addr)) {
  429. dev_kfree_skb_any(skb);
  430. break;
  431. }
  432. desc = priv->rx_fq.desc + pos;
  433. desc->buff_addr = cpu_to_le32(addr);
  434. priv->rx_skb[pos] = skb;
  435. desc->cmd = cpu_to_le32(DESC_VLD_FREE |
  436. (len - 1) << DESC_BUFF_LEN_OFF);
  437. pos = dma_ring_incr(pos, RX_DESC_NUM);
  438. }
  439. /* ensure desc updated */
  440. wmb();
  441. if (pos != start)
  442. writel_relaxed(dma_byte(pos), priv->base + RX_FQ_WR_ADDR);
  443. }
  444. static int hix5hd2_rx(struct net_device *dev, int limit)
  445. {
  446. struct hix5hd2_priv *priv = netdev_priv(dev);
  447. struct sk_buff *skb;
  448. struct hix5hd2_desc *desc;
  449. dma_addr_t addr;
  450. u32 start, end, num, pos, i, len;
  451. /* software read pointer */
  452. start = dma_cnt(readl_relaxed(priv->base + RX_BQ_RD_ADDR));
  453. /* logic write pointer */
  454. end = dma_cnt(readl_relaxed(priv->base + RX_BQ_WR_ADDR));
  455. num = CIRC_CNT(end, start, RX_DESC_NUM);
  456. if (num > limit)
  457. num = limit;
  458. /* ensure get updated desc */
  459. rmb();
  460. for (i = 0, pos = start; i < num; i++) {
  461. skb = priv->rx_skb[pos];
  462. if (unlikely(!skb)) {
  463. netdev_err(dev, "inconsistent rx_skb\n");
  464. break;
  465. }
  466. priv->rx_skb[pos] = NULL;
  467. desc = priv->rx_bq.desc + pos;
  468. len = (le32_to_cpu(desc->cmd) >> DESC_DATA_LEN_OFF) &
  469. DESC_DATA_MASK;
  470. addr = le32_to_cpu(desc->buff_addr);
  471. dma_unmap_single(priv->dev, addr, MAC_MAX_FRAME_SIZE,
  472. DMA_FROM_DEVICE);
  473. skb_put(skb, len);
  474. if (skb->len > MAC_MAX_FRAME_SIZE) {
  475. netdev_err(dev, "rcv len err, len = %d\n", skb->len);
  476. dev->stats.rx_errors++;
  477. dev->stats.rx_length_errors++;
  478. dev_kfree_skb_any(skb);
  479. goto next;
  480. }
  481. skb->protocol = eth_type_trans(skb, dev);
  482. napi_gro_receive(&priv->napi, skb);
  483. dev->stats.rx_packets++;
  484. dev->stats.rx_bytes += skb->len;
  485. next:
  486. pos = dma_ring_incr(pos, RX_DESC_NUM);
  487. }
  488. if (pos != start)
  489. writel_relaxed(dma_byte(pos), priv->base + RX_BQ_RD_ADDR);
  490. hix5hd2_rx_refill(priv);
  491. return num;
  492. }
  493. static void hix5hd2_clean_sg_desc(struct hix5hd2_priv *priv,
  494. struct sk_buff *skb, u32 pos)
  495. {
  496. struct sg_desc *desc;
  497. dma_addr_t addr;
  498. u32 len;
  499. int i;
  500. desc = priv->tx_ring.desc + pos;
  501. addr = le32_to_cpu(desc->linear_addr);
  502. len = le32_to_cpu(desc->linear_len);
  503. dma_unmap_single(priv->dev, addr, len, DMA_TO_DEVICE);
  504. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  505. addr = le32_to_cpu(desc->frags[i].addr);
  506. len = le32_to_cpu(desc->frags[i].size);
  507. dma_unmap_page(priv->dev, addr, len, DMA_TO_DEVICE);
  508. }
  509. }
  510. static void hix5hd2_xmit_reclaim(struct net_device *dev)
  511. {
  512. struct sk_buff *skb;
  513. struct hix5hd2_desc *desc;
  514. struct hix5hd2_priv *priv = netdev_priv(dev);
  515. unsigned int bytes_compl = 0, pkts_compl = 0;
  516. u32 start, end, num, pos, i;
  517. dma_addr_t addr;
  518. netif_tx_lock(dev);
  519. /* software read */
  520. start = dma_cnt(readl_relaxed(priv->base + TX_RQ_RD_ADDR));
  521. /* logic write */
  522. end = dma_cnt(readl_relaxed(priv->base + TX_RQ_WR_ADDR));
  523. num = CIRC_CNT(end, start, TX_DESC_NUM);
  524. for (i = 0, pos = start; i < num; i++) {
  525. skb = priv->tx_skb[pos];
  526. if (unlikely(!skb)) {
  527. netdev_err(dev, "inconsistent tx_skb\n");
  528. break;
  529. }
  530. pkts_compl++;
  531. bytes_compl += skb->len;
  532. desc = priv->tx_rq.desc + pos;
  533. if (skb_shinfo(skb)->nr_frags) {
  534. hix5hd2_clean_sg_desc(priv, skb, pos);
  535. } else {
  536. addr = le32_to_cpu(desc->buff_addr);
  537. dma_unmap_single(priv->dev, addr, skb->len,
  538. DMA_TO_DEVICE);
  539. }
  540. priv->tx_skb[pos] = NULL;
  541. dev_consume_skb_any(skb);
  542. pos = dma_ring_incr(pos, TX_DESC_NUM);
  543. }
  544. if (pos != start)
  545. writel_relaxed(dma_byte(pos), priv->base + TX_RQ_RD_ADDR);
  546. netif_tx_unlock(dev);
  547. if (pkts_compl || bytes_compl)
  548. netdev_completed_queue(dev, pkts_compl, bytes_compl);
  549. if (unlikely(netif_queue_stopped(priv->netdev)) && pkts_compl)
  550. netif_wake_queue(priv->netdev);
  551. }
  552. static int hix5hd2_poll(struct napi_struct *napi, int budget)
  553. {
  554. struct hix5hd2_priv *priv = container_of(napi,
  555. struct hix5hd2_priv, napi);
  556. struct net_device *dev = priv->netdev;
  557. int work_done = 0, task = budget;
  558. int ints, num;
  559. do {
  560. hix5hd2_xmit_reclaim(dev);
  561. num = hix5hd2_rx(dev, task);
  562. work_done += num;
  563. task -= num;
  564. if ((work_done >= budget) || (num == 0))
  565. break;
  566. ints = readl_relaxed(priv->base + RAW_PMU_INT);
  567. writel_relaxed(ints, priv->base + RAW_PMU_INT);
  568. } while (ints & DEF_INT_MASK);
  569. if (work_done < budget) {
  570. napi_complete_done(napi, work_done);
  571. hix5hd2_irq_enable(priv);
  572. }
  573. return work_done;
  574. }
  575. static irqreturn_t hix5hd2_interrupt(int irq, void *dev_id)
  576. {
  577. struct net_device *dev = (struct net_device *)dev_id;
  578. struct hix5hd2_priv *priv = netdev_priv(dev);
  579. int ints = readl_relaxed(priv->base + RAW_PMU_INT);
  580. writel_relaxed(ints, priv->base + RAW_PMU_INT);
  581. if (likely(ints & DEF_INT_MASK)) {
  582. hix5hd2_irq_disable(priv);
  583. napi_schedule(&priv->napi);
  584. }
  585. return IRQ_HANDLED;
  586. }
  587. static u32 hix5hd2_get_desc_cmd(struct sk_buff *skb, unsigned long hw_cap)
  588. {
  589. u32 cmd = 0;
  590. if (HAS_CAP_TSO(hw_cap)) {
  591. if (skb_shinfo(skb)->nr_frags)
  592. cmd |= DESC_SG;
  593. cmd |= skb_shinfo(skb)->nr_frags << DESC_FRAGS_NUM_OFF;
  594. } else {
  595. cmd |= DESC_FL_FULL |
  596. ((skb->len & DESC_DATA_MASK) << DESC_BUFF_LEN_OFF);
  597. }
  598. cmd |= (skb->len & DESC_DATA_MASK) << DESC_DATA_LEN_OFF;
  599. cmd |= DESC_VLD_BUSY;
  600. return cmd;
  601. }
  602. static int hix5hd2_fill_sg_desc(struct hix5hd2_priv *priv,
  603. struct sk_buff *skb, u32 pos)
  604. {
  605. struct sg_desc *desc;
  606. dma_addr_t addr;
  607. int ret;
  608. int i;
  609. desc = priv->tx_ring.desc + pos;
  610. desc->total_len = cpu_to_le32(skb->len);
  611. addr = dma_map_single(priv->dev, skb->data, skb_headlen(skb),
  612. DMA_TO_DEVICE);
  613. if (unlikely(dma_mapping_error(priv->dev, addr)))
  614. return -EINVAL;
  615. desc->linear_addr = cpu_to_le32(addr);
  616. desc->linear_len = cpu_to_le32(skb_headlen(skb));
  617. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  618. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  619. int len = frag->size;
  620. addr = skb_frag_dma_map(priv->dev, frag, 0, len, DMA_TO_DEVICE);
  621. ret = dma_mapping_error(priv->dev, addr);
  622. if (unlikely(ret))
  623. return -EINVAL;
  624. desc->frags[i].addr = cpu_to_le32(addr);
  625. desc->frags[i].size = cpu_to_le32(len);
  626. }
  627. return 0;
  628. }
  629. static netdev_tx_t hix5hd2_net_xmit(struct sk_buff *skb, struct net_device *dev)
  630. {
  631. struct hix5hd2_priv *priv = netdev_priv(dev);
  632. struct hix5hd2_desc *desc;
  633. dma_addr_t addr;
  634. u32 pos;
  635. u32 cmd;
  636. int ret;
  637. /* software write pointer */
  638. pos = dma_cnt(readl_relaxed(priv->base + TX_BQ_WR_ADDR));
  639. if (unlikely(priv->tx_skb[pos])) {
  640. dev->stats.tx_dropped++;
  641. dev->stats.tx_fifo_errors++;
  642. netif_stop_queue(dev);
  643. return NETDEV_TX_BUSY;
  644. }
  645. desc = priv->tx_bq.desc + pos;
  646. cmd = hix5hd2_get_desc_cmd(skb, priv->hw_cap);
  647. desc->cmd = cpu_to_le32(cmd);
  648. if (skb_shinfo(skb)->nr_frags) {
  649. ret = hix5hd2_fill_sg_desc(priv, skb, pos);
  650. if (unlikely(ret)) {
  651. dev_kfree_skb_any(skb);
  652. dev->stats.tx_dropped++;
  653. return NETDEV_TX_OK;
  654. }
  655. addr = priv->tx_ring.phys_addr + pos * sizeof(struct sg_desc);
  656. } else {
  657. addr = dma_map_single(priv->dev, skb->data, skb->len,
  658. DMA_TO_DEVICE);
  659. if (unlikely(dma_mapping_error(priv->dev, addr))) {
  660. dev_kfree_skb_any(skb);
  661. dev->stats.tx_dropped++;
  662. return NETDEV_TX_OK;
  663. }
  664. }
  665. desc->buff_addr = cpu_to_le32(addr);
  666. priv->tx_skb[pos] = skb;
  667. /* ensure desc updated */
  668. wmb();
  669. pos = dma_ring_incr(pos, TX_DESC_NUM);
  670. writel_relaxed(dma_byte(pos), priv->base + TX_BQ_WR_ADDR);
  671. netif_trans_update(dev);
  672. dev->stats.tx_packets++;
  673. dev->stats.tx_bytes += skb->len;
  674. netdev_sent_queue(dev, skb->len);
  675. return NETDEV_TX_OK;
  676. }
  677. static void hix5hd2_free_dma_desc_rings(struct hix5hd2_priv *priv)
  678. {
  679. struct hix5hd2_desc *desc;
  680. dma_addr_t addr;
  681. int i;
  682. for (i = 0; i < RX_DESC_NUM; i++) {
  683. struct sk_buff *skb = priv->rx_skb[i];
  684. if (skb == NULL)
  685. continue;
  686. desc = priv->rx_fq.desc + i;
  687. addr = le32_to_cpu(desc->buff_addr);
  688. dma_unmap_single(priv->dev, addr,
  689. MAC_MAX_FRAME_SIZE, DMA_FROM_DEVICE);
  690. dev_kfree_skb_any(skb);
  691. priv->rx_skb[i] = NULL;
  692. }
  693. for (i = 0; i < TX_DESC_NUM; i++) {
  694. struct sk_buff *skb = priv->tx_skb[i];
  695. if (skb == NULL)
  696. continue;
  697. desc = priv->tx_rq.desc + i;
  698. addr = le32_to_cpu(desc->buff_addr);
  699. dma_unmap_single(priv->dev, addr, skb->len, DMA_TO_DEVICE);
  700. dev_kfree_skb_any(skb);
  701. priv->tx_skb[i] = NULL;
  702. }
  703. }
  704. static int hix5hd2_net_open(struct net_device *dev)
  705. {
  706. struct hix5hd2_priv *priv = netdev_priv(dev);
  707. struct phy_device *phy;
  708. int ret;
  709. ret = clk_prepare_enable(priv->mac_core_clk);
  710. if (ret < 0) {
  711. netdev_err(dev, "failed to enable mac core clk %d\n", ret);
  712. return ret;
  713. }
  714. ret = clk_prepare_enable(priv->mac_ifc_clk);
  715. if (ret < 0) {
  716. clk_disable_unprepare(priv->mac_core_clk);
  717. netdev_err(dev, "failed to enable mac ifc clk %d\n", ret);
  718. return ret;
  719. }
  720. phy = of_phy_connect(dev, priv->phy_node,
  721. &hix5hd2_adjust_link, 0, priv->phy_mode);
  722. if (!phy) {
  723. clk_disable_unprepare(priv->mac_ifc_clk);
  724. clk_disable_unprepare(priv->mac_core_clk);
  725. return -ENODEV;
  726. }
  727. phy_start(phy);
  728. hix5hd2_hw_init(priv);
  729. hix5hd2_rx_refill(priv);
  730. netdev_reset_queue(dev);
  731. netif_start_queue(dev);
  732. napi_enable(&priv->napi);
  733. hix5hd2_port_enable(priv);
  734. hix5hd2_irq_enable(priv);
  735. return 0;
  736. }
  737. static int hix5hd2_net_close(struct net_device *dev)
  738. {
  739. struct hix5hd2_priv *priv = netdev_priv(dev);
  740. hix5hd2_port_disable(priv);
  741. hix5hd2_irq_disable(priv);
  742. napi_disable(&priv->napi);
  743. netif_stop_queue(dev);
  744. hix5hd2_free_dma_desc_rings(priv);
  745. if (dev->phydev) {
  746. phy_stop(dev->phydev);
  747. phy_disconnect(dev->phydev);
  748. }
  749. clk_disable_unprepare(priv->mac_ifc_clk);
  750. clk_disable_unprepare(priv->mac_core_clk);
  751. return 0;
  752. }
  753. static void hix5hd2_tx_timeout_task(struct work_struct *work)
  754. {
  755. struct hix5hd2_priv *priv;
  756. priv = container_of(work, struct hix5hd2_priv, tx_timeout_task);
  757. hix5hd2_net_close(priv->netdev);
  758. hix5hd2_net_open(priv->netdev);
  759. }
  760. static void hix5hd2_net_timeout(struct net_device *dev)
  761. {
  762. struct hix5hd2_priv *priv = netdev_priv(dev);
  763. schedule_work(&priv->tx_timeout_task);
  764. }
  765. static const struct net_device_ops hix5hd2_netdev_ops = {
  766. .ndo_open = hix5hd2_net_open,
  767. .ndo_stop = hix5hd2_net_close,
  768. .ndo_start_xmit = hix5hd2_net_xmit,
  769. .ndo_tx_timeout = hix5hd2_net_timeout,
  770. .ndo_set_mac_address = hix5hd2_net_set_mac_address,
  771. };
  772. static const struct ethtool_ops hix5hd2_ethtools_ops = {
  773. .get_link = ethtool_op_get_link,
  774. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  775. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  776. };
  777. static int hix5hd2_mdio_wait_ready(struct mii_bus *bus)
  778. {
  779. struct hix5hd2_priv *priv = bus->priv;
  780. void __iomem *base = priv->base;
  781. int i, timeout = 10000;
  782. for (i = 0; readl_relaxed(base + MDIO_SINGLE_CMD) & MDIO_START; i++) {
  783. if (i == timeout)
  784. return -ETIMEDOUT;
  785. usleep_range(10, 20);
  786. }
  787. return 0;
  788. }
  789. static int hix5hd2_mdio_read(struct mii_bus *bus, int phy, int reg)
  790. {
  791. struct hix5hd2_priv *priv = bus->priv;
  792. void __iomem *base = priv->base;
  793. int val, ret;
  794. ret = hix5hd2_mdio_wait_ready(bus);
  795. if (ret < 0)
  796. goto out;
  797. writel_relaxed(MDIO_READ | phy << 8 | reg, base + MDIO_SINGLE_CMD);
  798. ret = hix5hd2_mdio_wait_ready(bus);
  799. if (ret < 0)
  800. goto out;
  801. val = readl_relaxed(base + MDIO_RDATA_STATUS);
  802. if (val & MDIO_R_VALID) {
  803. dev_err(bus->parent, "SMI bus read not valid\n");
  804. ret = -ENODEV;
  805. goto out;
  806. }
  807. val = readl_relaxed(priv->base + MDIO_SINGLE_DATA);
  808. ret = (val >> 16) & 0xFFFF;
  809. out:
  810. return ret;
  811. }
  812. static int hix5hd2_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
  813. {
  814. struct hix5hd2_priv *priv = bus->priv;
  815. void __iomem *base = priv->base;
  816. int ret;
  817. ret = hix5hd2_mdio_wait_ready(bus);
  818. if (ret < 0)
  819. goto out;
  820. writel_relaxed(val, base + MDIO_SINGLE_DATA);
  821. writel_relaxed(MDIO_WRITE | phy << 8 | reg, base + MDIO_SINGLE_CMD);
  822. ret = hix5hd2_mdio_wait_ready(bus);
  823. out:
  824. return ret;
  825. }
  826. static void hix5hd2_destroy_hw_desc_queue(struct hix5hd2_priv *priv)
  827. {
  828. int i;
  829. for (i = 0; i < QUEUE_NUMS; i++) {
  830. if (priv->pool[i].desc) {
  831. dma_free_coherent(priv->dev, priv->pool[i].size,
  832. priv->pool[i].desc,
  833. priv->pool[i].phys_addr);
  834. priv->pool[i].desc = NULL;
  835. }
  836. }
  837. }
  838. static int hix5hd2_init_hw_desc_queue(struct hix5hd2_priv *priv)
  839. {
  840. struct device *dev = priv->dev;
  841. struct hix5hd2_desc *virt_addr;
  842. dma_addr_t phys_addr;
  843. int size, i;
  844. priv->rx_fq.count = RX_DESC_NUM;
  845. priv->rx_bq.count = RX_DESC_NUM;
  846. priv->tx_bq.count = TX_DESC_NUM;
  847. priv->tx_rq.count = TX_DESC_NUM;
  848. for (i = 0; i < QUEUE_NUMS; i++) {
  849. size = priv->pool[i].count * sizeof(struct hix5hd2_desc);
  850. virt_addr = dma_zalloc_coherent(dev, size, &phys_addr,
  851. GFP_KERNEL);
  852. if (virt_addr == NULL)
  853. goto error_free_pool;
  854. priv->pool[i].size = size;
  855. priv->pool[i].desc = virt_addr;
  856. priv->pool[i].phys_addr = phys_addr;
  857. }
  858. return 0;
  859. error_free_pool:
  860. hix5hd2_destroy_hw_desc_queue(priv);
  861. return -ENOMEM;
  862. }
  863. static int hix5hd2_init_sg_desc_queue(struct hix5hd2_priv *priv)
  864. {
  865. struct sg_desc *desc;
  866. dma_addr_t phys_addr;
  867. desc = (struct sg_desc *)dma_alloc_coherent(priv->dev,
  868. TX_DESC_NUM * sizeof(struct sg_desc),
  869. &phys_addr, GFP_KERNEL);
  870. if (!desc)
  871. return -ENOMEM;
  872. priv->tx_ring.desc = desc;
  873. priv->tx_ring.phys_addr = phys_addr;
  874. return 0;
  875. }
  876. static void hix5hd2_destroy_sg_desc_queue(struct hix5hd2_priv *priv)
  877. {
  878. if (priv->tx_ring.desc) {
  879. dma_free_coherent(priv->dev,
  880. TX_DESC_NUM * sizeof(struct sg_desc),
  881. priv->tx_ring.desc, priv->tx_ring.phys_addr);
  882. priv->tx_ring.desc = NULL;
  883. }
  884. }
  885. static inline void hix5hd2_mac_core_reset(struct hix5hd2_priv *priv)
  886. {
  887. if (!priv->mac_core_rst)
  888. return;
  889. reset_control_assert(priv->mac_core_rst);
  890. reset_control_deassert(priv->mac_core_rst);
  891. }
  892. static void hix5hd2_sleep_us(u32 time_us)
  893. {
  894. u32 time_ms;
  895. if (!time_us)
  896. return;
  897. time_ms = DIV_ROUND_UP(time_us, 1000);
  898. if (time_ms < 20)
  899. usleep_range(time_us, time_us + 500);
  900. else
  901. msleep(time_ms);
  902. }
  903. static void hix5hd2_phy_reset(struct hix5hd2_priv *priv)
  904. {
  905. /* To make sure PHY hardware reset success,
  906. * we must keep PHY in deassert state first and
  907. * then complete the hardware reset operation
  908. */
  909. reset_control_deassert(priv->phy_rst);
  910. hix5hd2_sleep_us(priv->phy_reset_delays[PRE_DELAY]);
  911. reset_control_assert(priv->phy_rst);
  912. /* delay some time to ensure reset ok,
  913. * this depends on PHY hardware feature
  914. */
  915. hix5hd2_sleep_us(priv->phy_reset_delays[PULSE]);
  916. reset_control_deassert(priv->phy_rst);
  917. /* delay some time to ensure later MDIO access */
  918. hix5hd2_sleep_us(priv->phy_reset_delays[POST_DELAY]);
  919. }
  920. static const struct of_device_id hix5hd2_of_match[];
  921. static int hix5hd2_dev_probe(struct platform_device *pdev)
  922. {
  923. struct device *dev = &pdev->dev;
  924. struct device_node *node = dev->of_node;
  925. const struct of_device_id *of_id = NULL;
  926. struct net_device *ndev;
  927. struct hix5hd2_priv *priv;
  928. struct resource *res;
  929. struct mii_bus *bus;
  930. const char *mac_addr;
  931. int ret;
  932. ndev = alloc_etherdev(sizeof(struct hix5hd2_priv));
  933. if (!ndev)
  934. return -ENOMEM;
  935. platform_set_drvdata(pdev, ndev);
  936. priv = netdev_priv(ndev);
  937. priv->dev = dev;
  938. priv->netdev = ndev;
  939. of_id = of_match_device(hix5hd2_of_match, dev);
  940. if (!of_id) {
  941. ret = -EINVAL;
  942. goto out_free_netdev;
  943. }
  944. priv->hw_cap = (unsigned long)of_id->data;
  945. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  946. priv->base = devm_ioremap_resource(dev, res);
  947. if (IS_ERR(priv->base)) {
  948. ret = PTR_ERR(priv->base);
  949. goto out_free_netdev;
  950. }
  951. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  952. priv->ctrl_base = devm_ioremap_resource(dev, res);
  953. if (IS_ERR(priv->ctrl_base)) {
  954. ret = PTR_ERR(priv->ctrl_base);
  955. goto out_free_netdev;
  956. }
  957. priv->mac_core_clk = devm_clk_get(&pdev->dev, "mac_core");
  958. if (IS_ERR(priv->mac_core_clk)) {
  959. netdev_err(ndev, "failed to get mac core clk\n");
  960. ret = -ENODEV;
  961. goto out_free_netdev;
  962. }
  963. ret = clk_prepare_enable(priv->mac_core_clk);
  964. if (ret < 0) {
  965. netdev_err(ndev, "failed to enable mac core clk %d\n", ret);
  966. goto out_free_netdev;
  967. }
  968. priv->mac_ifc_clk = devm_clk_get(&pdev->dev, "mac_ifc");
  969. if (IS_ERR(priv->mac_ifc_clk))
  970. priv->mac_ifc_clk = NULL;
  971. ret = clk_prepare_enable(priv->mac_ifc_clk);
  972. if (ret < 0) {
  973. netdev_err(ndev, "failed to enable mac ifc clk %d\n", ret);
  974. goto out_disable_mac_core_clk;
  975. }
  976. priv->mac_core_rst = devm_reset_control_get(dev, "mac_core");
  977. if (IS_ERR(priv->mac_core_rst))
  978. priv->mac_core_rst = NULL;
  979. hix5hd2_mac_core_reset(priv);
  980. priv->mac_ifc_rst = devm_reset_control_get(dev, "mac_ifc");
  981. if (IS_ERR(priv->mac_ifc_rst))
  982. priv->mac_ifc_rst = NULL;
  983. priv->phy_rst = devm_reset_control_get(dev, "phy");
  984. if (IS_ERR(priv->phy_rst)) {
  985. priv->phy_rst = NULL;
  986. } else {
  987. ret = of_property_read_u32_array(node,
  988. PHY_RESET_DELAYS_PROPERTY,
  989. priv->phy_reset_delays,
  990. DELAYS_NUM);
  991. if (ret)
  992. goto out_disable_clk;
  993. hix5hd2_phy_reset(priv);
  994. }
  995. bus = mdiobus_alloc();
  996. if (bus == NULL) {
  997. ret = -ENOMEM;
  998. goto out_disable_clk;
  999. }
  1000. bus->priv = priv;
  1001. bus->name = "hix5hd2_mii_bus";
  1002. bus->read = hix5hd2_mdio_read;
  1003. bus->write = hix5hd2_mdio_write;
  1004. bus->parent = &pdev->dev;
  1005. snprintf(bus->id, MII_BUS_ID_SIZE, "%s-mii", dev_name(&pdev->dev));
  1006. priv->bus = bus;
  1007. ret = of_mdiobus_register(bus, node);
  1008. if (ret)
  1009. goto err_free_mdio;
  1010. priv->phy_mode = of_get_phy_mode(node);
  1011. if ((int)priv->phy_mode < 0) {
  1012. netdev_err(ndev, "not find phy-mode\n");
  1013. ret = -EINVAL;
  1014. goto err_mdiobus;
  1015. }
  1016. priv->phy_node = of_parse_phandle(node, "phy-handle", 0);
  1017. if (!priv->phy_node) {
  1018. netdev_err(ndev, "not find phy-handle\n");
  1019. ret = -EINVAL;
  1020. goto err_mdiobus;
  1021. }
  1022. ndev->irq = platform_get_irq(pdev, 0);
  1023. if (ndev->irq <= 0) {
  1024. netdev_err(ndev, "No irq resource\n");
  1025. ret = -EINVAL;
  1026. goto out_phy_node;
  1027. }
  1028. ret = devm_request_irq(dev, ndev->irq, hix5hd2_interrupt,
  1029. 0, pdev->name, ndev);
  1030. if (ret) {
  1031. netdev_err(ndev, "devm_request_irq failed\n");
  1032. goto out_phy_node;
  1033. }
  1034. mac_addr = of_get_mac_address(node);
  1035. if (mac_addr)
  1036. ether_addr_copy(ndev->dev_addr, mac_addr);
  1037. if (!is_valid_ether_addr(ndev->dev_addr)) {
  1038. eth_hw_addr_random(ndev);
  1039. netdev_warn(ndev, "using random MAC address %pM\n",
  1040. ndev->dev_addr);
  1041. }
  1042. INIT_WORK(&priv->tx_timeout_task, hix5hd2_tx_timeout_task);
  1043. ndev->watchdog_timeo = 6 * HZ;
  1044. ndev->priv_flags |= IFF_UNICAST_FLT;
  1045. ndev->netdev_ops = &hix5hd2_netdev_ops;
  1046. ndev->ethtool_ops = &hix5hd2_ethtools_ops;
  1047. SET_NETDEV_DEV(ndev, dev);
  1048. if (HAS_CAP_TSO(priv->hw_cap))
  1049. ndev->hw_features |= NETIF_F_SG;
  1050. ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
  1051. ndev->vlan_features |= ndev->features;
  1052. ret = hix5hd2_init_hw_desc_queue(priv);
  1053. if (ret)
  1054. goto out_phy_node;
  1055. netif_napi_add(ndev, &priv->napi, hix5hd2_poll, NAPI_POLL_WEIGHT);
  1056. if (HAS_CAP_TSO(priv->hw_cap)) {
  1057. ret = hix5hd2_init_sg_desc_queue(priv);
  1058. if (ret)
  1059. goto out_destroy_queue;
  1060. }
  1061. ret = register_netdev(priv->netdev);
  1062. if (ret) {
  1063. netdev_err(ndev, "register_netdev failed!");
  1064. goto out_destroy_queue;
  1065. }
  1066. clk_disable_unprepare(priv->mac_ifc_clk);
  1067. clk_disable_unprepare(priv->mac_core_clk);
  1068. return ret;
  1069. out_destroy_queue:
  1070. if (HAS_CAP_TSO(priv->hw_cap))
  1071. hix5hd2_destroy_sg_desc_queue(priv);
  1072. netif_napi_del(&priv->napi);
  1073. hix5hd2_destroy_hw_desc_queue(priv);
  1074. out_phy_node:
  1075. of_node_put(priv->phy_node);
  1076. err_mdiobus:
  1077. mdiobus_unregister(bus);
  1078. err_free_mdio:
  1079. mdiobus_free(bus);
  1080. out_disable_clk:
  1081. clk_disable_unprepare(priv->mac_ifc_clk);
  1082. out_disable_mac_core_clk:
  1083. clk_disable_unprepare(priv->mac_core_clk);
  1084. out_free_netdev:
  1085. free_netdev(ndev);
  1086. return ret;
  1087. }
  1088. static int hix5hd2_dev_remove(struct platform_device *pdev)
  1089. {
  1090. struct net_device *ndev = platform_get_drvdata(pdev);
  1091. struct hix5hd2_priv *priv = netdev_priv(ndev);
  1092. netif_napi_del(&priv->napi);
  1093. unregister_netdev(ndev);
  1094. mdiobus_unregister(priv->bus);
  1095. mdiobus_free(priv->bus);
  1096. if (HAS_CAP_TSO(priv->hw_cap))
  1097. hix5hd2_destroy_sg_desc_queue(priv);
  1098. hix5hd2_destroy_hw_desc_queue(priv);
  1099. of_node_put(priv->phy_node);
  1100. cancel_work_sync(&priv->tx_timeout_task);
  1101. free_netdev(ndev);
  1102. return 0;
  1103. }
  1104. static const struct of_device_id hix5hd2_of_match[] = {
  1105. { .compatible = "hisilicon,hisi-gmac-v1", .data = (void *)GEMAC_V1 },
  1106. { .compatible = "hisilicon,hisi-gmac-v2", .data = (void *)GEMAC_V2 },
  1107. { .compatible = "hisilicon,hix5hd2-gmac", .data = (void *)GEMAC_V1 },
  1108. { .compatible = "hisilicon,hi3798cv200-gmac", .data = (void *)GEMAC_V2 },
  1109. { .compatible = "hisilicon,hi3516a-gmac", .data = (void *)GEMAC_V2 },
  1110. {},
  1111. };
  1112. MODULE_DEVICE_TABLE(of, hix5hd2_of_match);
  1113. static struct platform_driver hix5hd2_dev_driver = {
  1114. .driver = {
  1115. .name = "hisi-gmac",
  1116. .of_match_table = hix5hd2_of_match,
  1117. },
  1118. .probe = hix5hd2_dev_probe,
  1119. .remove = hix5hd2_dev_remove,
  1120. };
  1121. module_platform_driver(hix5hd2_dev_driver);
  1122. MODULE_DESCRIPTION("HISILICON Gigabit Ethernet MAC driver");
  1123. MODULE_LICENSE("GPL v2");
  1124. MODULE_ALIAS("platform:hisi-gmac");