altera_tse.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /* Altera Triple-Speed Ethernet MAC driver
  3. * Copyright (C) 2008-2014 Altera Corporation. All rights reserved
  4. *
  5. * Contributors:
  6. * Dalon Westergreen
  7. * Thomas Chou
  8. * Ian Abbott
  9. * Yuriy Kozlov
  10. * Tobias Klauser
  11. * Andriy Smolskyy
  12. * Roman Bulgakov
  13. * Dmytro Mytarchuk
  14. * Matthew Gerlach
  15. *
  16. * Original driver contributed by SLS.
  17. * Major updates contributed by GlobalLogic
  18. */
  19. #ifndef __ALTERA_TSE_H__
  20. #define __ALTERA_TSE_H__
  21. #define ALTERA_TSE_RESOURCE_NAME "altera_tse"
  22. #include <linux/bitops.h>
  23. #include <linux/if_vlan.h>
  24. #include <linux/list.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/phy.h>
  27. #define ALTERA_TSE_SW_RESET_WATCHDOG_CNTR 10000
  28. #define ALTERA_TSE_MAC_FIFO_WIDTH 4 /* TX/RX FIFO width in
  29. * bytes
  30. */
  31. /* Rx FIFO default settings */
  32. #define ALTERA_TSE_RX_SECTION_EMPTY 16
  33. #define ALTERA_TSE_RX_SECTION_FULL 0
  34. #define ALTERA_TSE_RX_ALMOST_EMPTY 8
  35. #define ALTERA_TSE_RX_ALMOST_FULL 8
  36. /* Tx FIFO default settings */
  37. #define ALTERA_TSE_TX_SECTION_EMPTY 16
  38. #define ALTERA_TSE_TX_SECTION_FULL 0
  39. #define ALTERA_TSE_TX_ALMOST_EMPTY 8
  40. #define ALTERA_TSE_TX_ALMOST_FULL 3
  41. /* MAC function configuration default settings */
  42. #define ALTERA_TSE_TX_IPG_LENGTH 12
  43. #define ALTERA_TSE_PAUSE_QUANTA 0xffff
  44. #define GET_BIT_VALUE(v, bit) (((v) >> (bit)) & 0x1)
  45. /* MAC Command_Config Register Bit Definitions
  46. */
  47. #define MAC_CMDCFG_TX_ENA BIT(0)
  48. #define MAC_CMDCFG_RX_ENA BIT(1)
  49. #define MAC_CMDCFG_XON_GEN BIT(2)
  50. #define MAC_CMDCFG_ETH_SPEED BIT(3)
  51. #define MAC_CMDCFG_PROMIS_EN BIT(4)
  52. #define MAC_CMDCFG_PAD_EN BIT(5)
  53. #define MAC_CMDCFG_CRC_FWD BIT(6)
  54. #define MAC_CMDCFG_PAUSE_FWD BIT(7)
  55. #define MAC_CMDCFG_PAUSE_IGNORE BIT(8)
  56. #define MAC_CMDCFG_TX_ADDR_INS BIT(9)
  57. #define MAC_CMDCFG_HD_ENA BIT(10)
  58. #define MAC_CMDCFG_EXCESS_COL BIT(11)
  59. #define MAC_CMDCFG_LATE_COL BIT(12)
  60. #define MAC_CMDCFG_SW_RESET BIT(13)
  61. #define MAC_CMDCFG_MHASH_SEL BIT(14)
  62. #define MAC_CMDCFG_LOOP_ENA BIT(15)
  63. #define MAC_CMDCFG_TX_ADDR_SEL(v) (((v) & 0x7) << 16)
  64. #define MAC_CMDCFG_MAGIC_ENA BIT(19)
  65. #define MAC_CMDCFG_SLEEP BIT(20)
  66. #define MAC_CMDCFG_WAKEUP BIT(21)
  67. #define MAC_CMDCFG_XOFF_GEN BIT(22)
  68. #define MAC_CMDCFG_CNTL_FRM_ENA BIT(23)
  69. #define MAC_CMDCFG_NO_LGTH_CHECK BIT(24)
  70. #define MAC_CMDCFG_ENA_10 BIT(25)
  71. #define MAC_CMDCFG_RX_ERR_DISC BIT(26)
  72. #define MAC_CMDCFG_DISABLE_READ_TIMEOUT BIT(27)
  73. #define MAC_CMDCFG_CNT_RESET BIT(31)
  74. #define MAC_CMDCFG_TX_ENA_GET(v) GET_BIT_VALUE(v, 0)
  75. #define MAC_CMDCFG_RX_ENA_GET(v) GET_BIT_VALUE(v, 1)
  76. #define MAC_CMDCFG_XON_GEN_GET(v) GET_BIT_VALUE(v, 2)
  77. #define MAC_CMDCFG_ETH_SPEED_GET(v) GET_BIT_VALUE(v, 3)
  78. #define MAC_CMDCFG_PROMIS_EN_GET(v) GET_BIT_VALUE(v, 4)
  79. #define MAC_CMDCFG_PAD_EN_GET(v) GET_BIT_VALUE(v, 5)
  80. #define MAC_CMDCFG_CRC_FWD_GET(v) GET_BIT_VALUE(v, 6)
  81. #define MAC_CMDCFG_PAUSE_FWD_GET(v) GET_BIT_VALUE(v, 7)
  82. #define MAC_CMDCFG_PAUSE_IGNORE_GET(v) GET_BIT_VALUE(v, 8)
  83. #define MAC_CMDCFG_TX_ADDR_INS_GET(v) GET_BIT_VALUE(v, 9)
  84. #define MAC_CMDCFG_HD_ENA_GET(v) GET_BIT_VALUE(v, 10)
  85. #define MAC_CMDCFG_EXCESS_COL_GET(v) GET_BIT_VALUE(v, 11)
  86. #define MAC_CMDCFG_LATE_COL_GET(v) GET_BIT_VALUE(v, 12)
  87. #define MAC_CMDCFG_SW_RESET_GET(v) GET_BIT_VALUE(v, 13)
  88. #define MAC_CMDCFG_MHASH_SEL_GET(v) GET_BIT_VALUE(v, 14)
  89. #define MAC_CMDCFG_LOOP_ENA_GET(v) GET_BIT_VALUE(v, 15)
  90. #define MAC_CMDCFG_TX_ADDR_SEL_GET(v) (((v) >> 16) & 0x7)
  91. #define MAC_CMDCFG_MAGIC_ENA_GET(v) GET_BIT_VALUE(v, 19)
  92. #define MAC_CMDCFG_SLEEP_GET(v) GET_BIT_VALUE(v, 20)
  93. #define MAC_CMDCFG_WAKEUP_GET(v) GET_BIT_VALUE(v, 21)
  94. #define MAC_CMDCFG_XOFF_GEN_GET(v) GET_BIT_VALUE(v, 22)
  95. #define MAC_CMDCFG_CNTL_FRM_ENA_GET(v) GET_BIT_VALUE(v, 23)
  96. #define MAC_CMDCFG_NO_LGTH_CHECK_GET(v) GET_BIT_VALUE(v, 24)
  97. #define MAC_CMDCFG_ENA_10_GET(v) GET_BIT_VALUE(v, 25)
  98. #define MAC_CMDCFG_RX_ERR_DISC_GET(v) GET_BIT_VALUE(v, 26)
  99. #define MAC_CMDCFG_DISABLE_READ_TIMEOUT_GET(v) GET_BIT_VALUE(v, 27)
  100. #define MAC_CMDCFG_CNT_RESET_GET(v) GET_BIT_VALUE(v, 31)
  101. /* SGMII PCS register addresses
  102. */
  103. #define SGMII_PCS_SCRATCH 0x10
  104. #define SGMII_PCS_REV 0x11
  105. #define SGMII_PCS_LINK_TIMER_0 0x12
  106. #define SGMII_PCS_LINK_TIMER_1 0x13
  107. #define SGMII_PCS_IF_MODE 0x14
  108. #define SGMII_PCS_DIS_READ_TO 0x15
  109. #define SGMII_PCS_READ_TO 0x16
  110. #define SGMII_PCS_SW_RESET_TIMEOUT 100 /* usecs */
  111. /* MDIO registers within MAC register Space
  112. */
  113. struct altera_tse_mdio {
  114. u32 control; /* PHY device operation control register */
  115. u32 status; /* PHY device operation status register */
  116. u32 phy_id1; /* Bits 31:16 of PHY identifier */
  117. u32 phy_id2; /* Bits 15:0 of PHY identifier */
  118. u32 auto_negotiation_advertisement; /* Auto-negotiation
  119. * advertisement
  120. * register
  121. */
  122. u32 remote_partner_base_page_ability;
  123. u32 reg6;
  124. u32 reg7;
  125. u32 reg8;
  126. u32 reg9;
  127. u32 rega;
  128. u32 regb;
  129. u32 regc;
  130. u32 regd;
  131. u32 rege;
  132. u32 regf;
  133. u32 reg10;
  134. u32 reg11;
  135. u32 reg12;
  136. u32 reg13;
  137. u32 reg14;
  138. u32 reg15;
  139. u32 reg16;
  140. u32 reg17;
  141. u32 reg18;
  142. u32 reg19;
  143. u32 reg1a;
  144. u32 reg1b;
  145. u32 reg1c;
  146. u32 reg1d;
  147. u32 reg1e;
  148. u32 reg1f;
  149. };
  150. /* MAC register Space. Note that some of these registers may or may not be
  151. * present depending upon options chosen by the user when the core was
  152. * configured and built. Please consult the Altera Triple Speed Ethernet User
  153. * Guide for details.
  154. */
  155. struct altera_tse_mac {
  156. /* Bits 15:0: MegaCore function revision (0x0800). Bit 31:16: Customer
  157. * specific revision
  158. */
  159. u32 megacore_revision;
  160. /* Provides a memory location for user applications to test the device
  161. * memory operation.
  162. */
  163. u32 scratch_pad;
  164. /* The host processor uses this register to control and configure the
  165. * MAC block
  166. */
  167. u32 command_config;
  168. /* 32-bit primary MAC address word 0 bits 0 to 31 of the primary
  169. * MAC address
  170. */
  171. u32 mac_addr_0;
  172. /* 32-bit primary MAC address word 1 bits 32 to 47 of the primary
  173. * MAC address
  174. */
  175. u32 mac_addr_1;
  176. /* 14-bit maximum frame length. The MAC receive logic */
  177. u32 frm_length;
  178. /* The pause quanta is used in each pause frame sent to a remote
  179. * Ethernet device, in increments of 512 Ethernet bit times
  180. */
  181. u32 pause_quanta;
  182. /* 12-bit receive FIFO section-empty threshold */
  183. u32 rx_section_empty;
  184. /* 12-bit receive FIFO section-full threshold */
  185. u32 rx_section_full;
  186. /* 12-bit transmit FIFO section-empty threshold */
  187. u32 tx_section_empty;
  188. /* 12-bit transmit FIFO section-full threshold */
  189. u32 tx_section_full;
  190. /* 12-bit receive FIFO almost-empty threshold */
  191. u32 rx_almost_empty;
  192. /* 12-bit receive FIFO almost-full threshold */
  193. u32 rx_almost_full;
  194. /* 12-bit transmit FIFO almost-empty threshold */
  195. u32 tx_almost_empty;
  196. /* 12-bit transmit FIFO almost-full threshold */
  197. u32 tx_almost_full;
  198. /* MDIO address of PHY Device 0. Bits 0 to 4 hold a 5-bit PHY address */
  199. u32 mdio_phy0_addr;
  200. /* MDIO address of PHY Device 1. Bits 0 to 4 hold a 5-bit PHY address */
  201. u32 mdio_phy1_addr;
  202. /* Bit[15:0]—16-bit holdoff quanta */
  203. u32 holdoff_quant;
  204. /* only if 100/1000 BaseX PCS, reserved otherwise */
  205. u32 reserved1[5];
  206. /* Minimum IPG between consecutive transmit frame in terms of bytes */
  207. u32 tx_ipg_length;
  208. /* IEEE 802.3 oEntity Managed Object Support */
  209. /* The MAC addresses */
  210. u32 mac_id_1;
  211. u32 mac_id_2;
  212. /* Number of frames transmitted without error including pause frames */
  213. u32 frames_transmitted_ok;
  214. /* Number of frames received without error including pause frames */
  215. u32 frames_received_ok;
  216. /* Number of frames received with a CRC error */
  217. u32 frames_check_sequence_errors;
  218. /* Frame received with an alignment error */
  219. u32 alignment_errors;
  220. /* Sum of payload and padding octets of frames transmitted without
  221. * error
  222. */
  223. u32 octets_transmitted_ok;
  224. /* Sum of payload and padding octets of frames received without error */
  225. u32 octets_received_ok;
  226. /* IEEE 802.3 oPausedEntity Managed Object Support */
  227. /* Number of transmitted pause frames */
  228. u32 tx_pause_mac_ctrl_frames;
  229. /* Number of Received pause frames */
  230. u32 rx_pause_mac_ctrl_frames;
  231. /* IETF MIB (MIB-II) Object Support */
  232. /* Number of frames received with error */
  233. u32 if_in_errors;
  234. /* Number of frames transmitted with error */
  235. u32 if_out_errors;
  236. /* Number of valid received unicast frames */
  237. u32 if_in_ucast_pkts;
  238. /* Number of valid received multicasts frames (without pause) */
  239. u32 if_in_multicast_pkts;
  240. /* Number of valid received broadcast frames */
  241. u32 if_in_broadcast_pkts;
  242. u32 if_out_discards;
  243. /* The number of valid unicast frames transmitted */
  244. u32 if_out_ucast_pkts;
  245. /* The number of valid multicast frames transmitted,
  246. * excluding pause frames
  247. */
  248. u32 if_out_multicast_pkts;
  249. u32 if_out_broadcast_pkts;
  250. /* IETF RMON MIB Object Support */
  251. /* Counts the number of dropped packets due to internal errors
  252. * of the MAC client.
  253. */
  254. u32 ether_stats_drop_events;
  255. /* Total number of bytes received. Good and bad frames. */
  256. u32 ether_stats_octets;
  257. /* Total number of packets received. Counts good and bad packets. */
  258. u32 ether_stats_pkts;
  259. /* Number of packets received with less than 64 bytes. */
  260. u32 ether_stats_undersize_pkts;
  261. /* The number of frames received that are longer than the
  262. * value configured in the frm_length register
  263. */
  264. u32 ether_stats_oversize_pkts;
  265. /* Number of received packet with 64 bytes */
  266. u32 ether_stats_pkts_64_octets;
  267. /* Frames (good and bad) with 65 to 127 bytes */
  268. u32 ether_stats_pkts_65to127_octets;
  269. /* Frames (good and bad) with 128 to 255 bytes */
  270. u32 ether_stats_pkts_128to255_octets;
  271. /* Frames (good and bad) with 256 to 511 bytes */
  272. u32 ether_stats_pkts_256to511_octets;
  273. /* Frames (good and bad) with 512 to 1023 bytes */
  274. u32 ether_stats_pkts_512to1023_octets;
  275. /* Frames (good and bad) with 1024 to 1518 bytes */
  276. u32 ether_stats_pkts_1024to1518_octets;
  277. /* Any frame length from 1519 to the maximum length configured in the
  278. * frm_length register, if it is greater than 1518
  279. */
  280. u32 ether_stats_pkts_1519tox_octets;
  281. /* Too long frames with CRC error */
  282. u32 ether_stats_jabbers;
  283. /* Too short frames with CRC error */
  284. u32 ether_stats_fragments;
  285. u32 reserved2;
  286. /* FIFO control register */
  287. u32 tx_cmd_stat;
  288. u32 rx_cmd_stat;
  289. /* Extended Statistics Counters */
  290. u32 msb_octets_transmitted_ok;
  291. u32 msb_octets_received_ok;
  292. u32 msb_ether_stats_octets;
  293. u32 reserved3;
  294. /* Multicast address resolution table, mapped in the controller address
  295. * space
  296. */
  297. u32 hash_table[64];
  298. /* Registers 0 to 31 within PHY device 0/1 connected to the MDIO PHY
  299. * management interface
  300. */
  301. struct altera_tse_mdio mdio_phy0;
  302. struct altera_tse_mdio mdio_phy1;
  303. /* 4 Supplemental MAC Addresses */
  304. u32 supp_mac_addr_0_0;
  305. u32 supp_mac_addr_0_1;
  306. u32 supp_mac_addr_1_0;
  307. u32 supp_mac_addr_1_1;
  308. u32 supp_mac_addr_2_0;
  309. u32 supp_mac_addr_2_1;
  310. u32 supp_mac_addr_3_0;
  311. u32 supp_mac_addr_3_1;
  312. u32 reserved4[8];
  313. /* IEEE 1588v2 Feature */
  314. u32 tx_period;
  315. u32 tx_adjust_fns;
  316. u32 tx_adjust_ns;
  317. u32 rx_period;
  318. u32 rx_adjust_fns;
  319. u32 rx_adjust_ns;
  320. u32 reserved5[42];
  321. };
  322. #define tse_csroffs(a) (offsetof(struct altera_tse_mac, a))
  323. /* Transmit and Receive Command Registers Bit Definitions
  324. */
  325. #define ALTERA_TSE_TX_CMD_STAT_OMIT_CRC BIT(17)
  326. #define ALTERA_TSE_TX_CMD_STAT_TX_SHIFT16 BIT(18)
  327. #define ALTERA_TSE_RX_CMD_STAT_RX_SHIFT16 BIT(25)
  328. /* Wrapper around a pointer to a socket buffer,
  329. * so a DMA handle can be stored along with the buffer
  330. */
  331. struct tse_buffer {
  332. struct list_head lh;
  333. struct sk_buff *skb;
  334. dma_addr_t dma_addr;
  335. u32 len;
  336. int mapped_as_page;
  337. };
  338. struct altera_tse_private;
  339. #define ALTERA_DTYPE_SGDMA 1
  340. #define ALTERA_DTYPE_MSGDMA 2
  341. /* standard DMA interface for SGDMA and MSGDMA */
  342. struct altera_dmaops {
  343. int altera_dtype;
  344. int dmamask;
  345. void (*reset_dma)(struct altera_tse_private *);
  346. void (*enable_txirq)(struct altera_tse_private *);
  347. void (*enable_rxirq)(struct altera_tse_private *);
  348. void (*disable_txirq)(struct altera_tse_private *);
  349. void (*disable_rxirq)(struct altera_tse_private *);
  350. void (*clear_txirq)(struct altera_tse_private *);
  351. void (*clear_rxirq)(struct altera_tse_private *);
  352. int (*tx_buffer)(struct altera_tse_private *, struct tse_buffer *);
  353. u32 (*tx_completions)(struct altera_tse_private *);
  354. void (*add_rx_desc)(struct altera_tse_private *, struct tse_buffer *);
  355. u32 (*get_rx_status)(struct altera_tse_private *);
  356. int (*init_dma)(struct altera_tse_private *);
  357. void (*uninit_dma)(struct altera_tse_private *);
  358. void (*start_rxdma)(struct altera_tse_private *);
  359. };
  360. /* This structure is private to each device.
  361. */
  362. struct altera_tse_private {
  363. struct net_device *dev;
  364. struct device *device;
  365. struct napi_struct napi;
  366. /* MAC address space */
  367. struct altera_tse_mac __iomem *mac_dev;
  368. /* TSE Revision */
  369. u32 revision;
  370. /* mSGDMA Rx Dispatcher address space */
  371. void __iomem *rx_dma_csr;
  372. void __iomem *rx_dma_desc;
  373. void __iomem *rx_dma_resp;
  374. /* mSGDMA Tx Dispatcher address space */
  375. void __iomem *tx_dma_csr;
  376. void __iomem *tx_dma_desc;
  377. /* Rx buffers queue */
  378. struct tse_buffer *rx_ring;
  379. u32 rx_cons;
  380. u32 rx_prod;
  381. u32 rx_ring_size;
  382. u32 rx_dma_buf_sz;
  383. /* Tx ring buffer */
  384. struct tse_buffer *tx_ring;
  385. u32 tx_prod;
  386. u32 tx_cons;
  387. u32 tx_ring_size;
  388. /* Interrupts */
  389. u32 tx_irq;
  390. u32 rx_irq;
  391. /* RX/TX MAC FIFO configs */
  392. u32 tx_fifo_depth;
  393. u32 rx_fifo_depth;
  394. /* Hash filter settings */
  395. u32 hash_filter;
  396. u32 added_unicast;
  397. /* Descriptor memory info for managing SGDMA */
  398. u32 txdescmem;
  399. u32 rxdescmem;
  400. dma_addr_t rxdescmem_busaddr;
  401. dma_addr_t txdescmem_busaddr;
  402. u32 txctrlreg;
  403. u32 rxctrlreg;
  404. dma_addr_t rxdescphys;
  405. dma_addr_t txdescphys;
  406. struct list_head txlisthd;
  407. struct list_head rxlisthd;
  408. /* MAC command_config register protection */
  409. spinlock_t mac_cfg_lock;
  410. /* Tx path protection */
  411. spinlock_t tx_lock;
  412. /* Rx DMA & interrupt control protection */
  413. spinlock_t rxdma_irq_lock;
  414. /* PHY */
  415. int phy_addr; /* PHY's MDIO address, -1 for autodetection */
  416. phy_interface_t phy_iface;
  417. struct mii_bus *mdio;
  418. int oldspeed;
  419. int oldduplex;
  420. int oldlink;
  421. /* ethtool msglvl option */
  422. u32 msg_enable;
  423. struct altera_dmaops *dmaops;
  424. };
  425. /* Function prototypes
  426. */
  427. void altera_tse_set_ethtool_ops(struct net_device *);
  428. static inline
  429. u32 csrrd32(void __iomem *mac, size_t offs)
  430. {
  431. void __iomem *paddr = (void __iomem *)((uintptr_t)mac + offs);
  432. return readl(paddr);
  433. }
  434. static inline
  435. u16 csrrd16(void __iomem *mac, size_t offs)
  436. {
  437. void __iomem *paddr = (void __iomem *)((uintptr_t)mac + offs);
  438. return readw(paddr);
  439. }
  440. static inline
  441. u8 csrrd8(void __iomem *mac, size_t offs)
  442. {
  443. void __iomem *paddr = (void __iomem *)((uintptr_t)mac + offs);
  444. return readb(paddr);
  445. }
  446. static inline
  447. void csrwr32(u32 val, void __iomem *mac, size_t offs)
  448. {
  449. void __iomem *paddr = (void __iomem *)((uintptr_t)mac + offs);
  450. writel(val, paddr);
  451. }
  452. static inline
  453. void csrwr16(u16 val, void __iomem *mac, size_t offs)
  454. {
  455. void __iomem *paddr = (void __iomem *)((uintptr_t)mac + offs);
  456. writew(val, paddr);
  457. }
  458. static inline
  459. void csrwr8(u8 val, void __iomem *mac, size_t offs)
  460. {
  461. void __iomem *paddr = (void __iomem *)((uintptr_t)mac + offs);
  462. writeb(val, paddr);
  463. }
  464. #endif /* __ALTERA_TSE_H__ */