altera_tse.h 15 KB

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