core.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*
  2. * drivers/net/ethernet/ibm/emac/core.h
  3. *
  4. * Driver for PowerPC 4xx on-chip ethernet controller.
  5. *
  6. * Copyright 2007 Benjamin Herrenschmidt, IBM Corp.
  7. * <benh@kernel.crashing.org>
  8. *
  9. * Based on the arch/ppc version of the driver:
  10. *
  11. * Copyright (c) 2004, 2005 Zultys Technologies.
  12. * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
  13. *
  14. * Based on original work by
  15. * Armin Kuster <akuster@mvista.com>
  16. * Johnnie Peters <jpeters@mvista.com>
  17. * Copyright 2000, 2001 MontaVista Softare Inc.
  18. *
  19. * This program is free software; you can redistribute it and/or modify it
  20. * under the terms of the GNU General Public License as published by the
  21. * Free Software Foundation; either version 2 of the License, or (at your
  22. * option) any later version.
  23. *
  24. */
  25. #ifndef __IBM_NEWEMAC_CORE_H
  26. #define __IBM_NEWEMAC_CORE_H
  27. #include <linux/module.h>
  28. #include <linux/list.h>
  29. #include <linux/kernel.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/netdevice.h>
  32. #include <linux/dma-mapping.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/of_platform.h>
  35. #include <linux/slab.h>
  36. #include <asm/io.h>
  37. #include <asm/dcr.h>
  38. #include "emac.h"
  39. #include "phy.h"
  40. #include "zmii.h"
  41. #include "rgmii.h"
  42. #include "mal.h"
  43. #include "tah.h"
  44. #include "debug.h"
  45. #define NUM_TX_BUFF CONFIG_IBM_EMAC_TXB
  46. #define NUM_RX_BUFF CONFIG_IBM_EMAC_RXB
  47. /* Simple sanity check */
  48. #if NUM_TX_BUFF > 256 || NUM_RX_BUFF > 256
  49. #error Invalid number of buffer descriptors (greater than 256)
  50. #endif
  51. #define EMAC_MIN_MTU 46
  52. /* Maximum L2 header length (VLAN tagged, no FCS) */
  53. #define EMAC_MTU_OVERHEAD (6 * 2 + 2 + 4)
  54. /* RX BD size for the given MTU */
  55. static inline int emac_rx_size(int mtu)
  56. {
  57. if (mtu > ETH_DATA_LEN)
  58. return MAL_MAX_RX_SIZE;
  59. else
  60. return mal_rx_size(ETH_DATA_LEN + EMAC_MTU_OVERHEAD);
  61. }
  62. #define EMAC_DMA_ALIGN(x) ALIGN((x), dma_get_cache_alignment())
  63. #define EMAC_RX_SKB_HEADROOM \
  64. EMAC_DMA_ALIGN(CONFIG_IBM_EMAC_RX_SKB_HEADROOM)
  65. /* Size of RX skb for the given MTU */
  66. static inline int emac_rx_skb_size(int mtu)
  67. {
  68. int size = max(mtu + EMAC_MTU_OVERHEAD, emac_rx_size(mtu));
  69. return EMAC_DMA_ALIGN(size + 2) + EMAC_RX_SKB_HEADROOM;
  70. }
  71. /* RX DMA sync size */
  72. static inline int emac_rx_sync_size(int mtu)
  73. {
  74. return EMAC_DMA_ALIGN(emac_rx_size(mtu) + 2);
  75. }
  76. /* Driver statistcs is split into two parts to make it more cache friendly:
  77. * - normal statistics (packet count, etc)
  78. * - error statistics
  79. *
  80. * When statistics is requested by ethtool, these parts are concatenated,
  81. * normal one goes first.
  82. *
  83. * Please, keep these structures in sync with emac_stats_keys.
  84. */
  85. /* Normal TX/RX Statistics */
  86. struct emac_stats {
  87. u64 rx_packets;
  88. u64 rx_bytes;
  89. u64 tx_packets;
  90. u64 tx_bytes;
  91. u64 rx_packets_csum;
  92. u64 tx_packets_csum;
  93. };
  94. /* Error statistics */
  95. struct emac_error_stats {
  96. u64 tx_undo;
  97. /* Software RX Errors */
  98. u64 rx_dropped_stack;
  99. u64 rx_dropped_oom;
  100. u64 rx_dropped_error;
  101. u64 rx_dropped_resize;
  102. u64 rx_dropped_mtu;
  103. u64 rx_stopped;
  104. /* BD reported RX errors */
  105. u64 rx_bd_errors;
  106. u64 rx_bd_overrun;
  107. u64 rx_bd_bad_packet;
  108. u64 rx_bd_runt_packet;
  109. u64 rx_bd_short_event;
  110. u64 rx_bd_alignment_error;
  111. u64 rx_bd_bad_fcs;
  112. u64 rx_bd_packet_too_long;
  113. u64 rx_bd_out_of_range;
  114. u64 rx_bd_in_range;
  115. /* EMAC IRQ reported RX errors */
  116. u64 rx_parity;
  117. u64 rx_fifo_overrun;
  118. u64 rx_overrun;
  119. u64 rx_bad_packet;
  120. u64 rx_runt_packet;
  121. u64 rx_short_event;
  122. u64 rx_alignment_error;
  123. u64 rx_bad_fcs;
  124. u64 rx_packet_too_long;
  125. u64 rx_out_of_range;
  126. u64 rx_in_range;
  127. /* Software TX Errors */
  128. u64 tx_dropped;
  129. /* BD reported TX errors */
  130. u64 tx_bd_errors;
  131. u64 tx_bd_bad_fcs;
  132. u64 tx_bd_carrier_loss;
  133. u64 tx_bd_excessive_deferral;
  134. u64 tx_bd_excessive_collisions;
  135. u64 tx_bd_late_collision;
  136. u64 tx_bd_multple_collisions;
  137. u64 tx_bd_single_collision;
  138. u64 tx_bd_underrun;
  139. u64 tx_bd_sqe;
  140. /* EMAC IRQ reported TX errors */
  141. u64 tx_parity;
  142. u64 tx_underrun;
  143. u64 tx_sqe;
  144. u64 tx_errors;
  145. };
  146. #define EMAC_ETHTOOL_STATS_COUNT ((sizeof(struct emac_stats) + \
  147. sizeof(struct emac_error_stats)) \
  148. / sizeof(u64))
  149. struct emac_instance {
  150. struct net_device *ndev;
  151. struct resource rsrc_regs;
  152. struct emac_regs __iomem *emacp;
  153. struct platform_device *ofdev;
  154. struct device_node **blist; /* bootlist entry */
  155. /* MAL linkage */
  156. u32 mal_ph;
  157. struct platform_device *mal_dev;
  158. u32 mal_rx_chan;
  159. u32 mal_tx_chan;
  160. struct mal_instance *mal;
  161. struct mal_commac commac;
  162. /* PHY infos */
  163. int phy_mode;
  164. u32 phy_map;
  165. u32 phy_address;
  166. u32 phy_feat_exc;
  167. struct mii_phy phy;
  168. struct mutex link_lock;
  169. struct delayed_work link_work;
  170. int link_polling;
  171. /* GPCS PHY infos */
  172. u32 gpcs_address;
  173. /* Shared MDIO if any */
  174. u32 mdio_ph;
  175. struct platform_device *mdio_dev;
  176. struct emac_instance *mdio_instance;
  177. struct mutex mdio_lock;
  178. /* ZMII infos if any */
  179. u32 zmii_ph;
  180. u32 zmii_port;
  181. struct platform_device *zmii_dev;
  182. /* RGMII infos if any */
  183. u32 rgmii_ph;
  184. u32 rgmii_port;
  185. struct platform_device *rgmii_dev;
  186. /* TAH infos if any */
  187. u32 tah_ph;
  188. u32 tah_port;
  189. struct platform_device *tah_dev;
  190. /* IRQs */
  191. int wol_irq;
  192. int emac_irq;
  193. /* OPB bus frequency in Mhz */
  194. u32 opb_bus_freq;
  195. /* Cell index within an ASIC (for clk mgmnt) */
  196. u32 cell_index;
  197. /* Max supported MTU */
  198. u32 max_mtu;
  199. /* Feature bits (from probe table) */
  200. unsigned int features;
  201. /* Tx and Rx fifo sizes & other infos in bytes */
  202. u32 tx_fifo_size;
  203. u32 tx_fifo_size_gige;
  204. u32 rx_fifo_size;
  205. u32 rx_fifo_size_gige;
  206. u32 fifo_entry_size;
  207. u32 mal_burst_size; /* move to MAL ? */
  208. /* IAHT and GAHT filter parameterization */
  209. u32 xaht_slots_shift;
  210. u32 xaht_width_shift;
  211. /* Descriptor management
  212. */
  213. struct mal_descriptor *tx_desc;
  214. int tx_cnt;
  215. int tx_slot;
  216. int ack_slot;
  217. struct mal_descriptor *rx_desc;
  218. int rx_slot;
  219. struct sk_buff *rx_sg_skb; /* 1 */
  220. int rx_skb_size;
  221. int rx_sync_size;
  222. struct sk_buff *tx_skb[NUM_TX_BUFF];
  223. struct sk_buff *rx_skb[NUM_RX_BUFF];
  224. /* Stats
  225. */
  226. struct emac_error_stats estats;
  227. struct net_device_stats nstats;
  228. struct emac_stats stats;
  229. /* Misc
  230. */
  231. int reset_failed;
  232. int stop_timeout; /* in us */
  233. int no_mcast;
  234. int mcast_pending;
  235. int opened;
  236. struct work_struct reset_work;
  237. spinlock_t lock;
  238. };
  239. /*
  240. * Features of various EMAC implementations
  241. */
  242. /*
  243. * No flow control on 40x according to the original driver
  244. */
  245. #define EMAC_FTR_NO_FLOW_CONTROL_40x 0x00000001
  246. /*
  247. * Cell is an EMAC4
  248. */
  249. #define EMAC_FTR_EMAC4 0x00000002
  250. /*
  251. * For the 440SPe, AMCC inexplicably changed the polarity of
  252. * the "operation complete" bit in the MII control register.
  253. */
  254. #define EMAC_FTR_STACR_OC_INVERT 0x00000004
  255. /*
  256. * Set if we have a TAH.
  257. */
  258. #define EMAC_FTR_HAS_TAH 0x00000008
  259. /*
  260. * Set if we have a ZMII.
  261. */
  262. #define EMAC_FTR_HAS_ZMII 0x00000010
  263. /*
  264. * Set if we have a RGMII.
  265. */
  266. #define EMAC_FTR_HAS_RGMII 0x00000020
  267. /*
  268. * Set if we have new type STACR with STAOPC
  269. */
  270. #define EMAC_FTR_HAS_NEW_STACR 0x00000040
  271. /*
  272. * Set if we need phy clock workaround for 440gx
  273. */
  274. #define EMAC_FTR_440GX_PHY_CLK_FIX 0x00000080
  275. /*
  276. * Set if we need phy clock workaround for 440ep or 440gr
  277. */
  278. #define EMAC_FTR_440EP_PHY_CLK_FIX 0x00000100
  279. /*
  280. * The 405EX and 460EX contain the EMAC4SYNC core
  281. */
  282. #define EMAC_FTR_EMAC4SYNC 0x00000200
  283. /*
  284. * Set if we need phy clock workaround for 460ex or 460gt
  285. */
  286. #define EMAC_FTR_460EX_PHY_CLK_FIX 0x00000400
  287. /*
  288. * APM821xx requires Jumbo frame size set explicitly
  289. */
  290. #define EMAC_APM821XX_REQ_JUMBO_FRAME_SIZE 0x00000800
  291. /*
  292. * APM821xx does not support Half Duplex mode
  293. */
  294. #define EMAC_FTR_APM821XX_NO_HALF_DUPLEX 0x00001000
  295. /* Right now, we don't quite handle the always/possible masks on the
  296. * most optimal way as we don't have a way to say something like
  297. * always EMAC4. Patches welcome.
  298. */
  299. enum {
  300. EMAC_FTRS_ALWAYS = 0,
  301. EMAC_FTRS_POSSIBLE =
  302. #ifdef CONFIG_IBM_EMAC_EMAC4
  303. EMAC_FTR_EMAC4 | EMAC_FTR_EMAC4SYNC |
  304. EMAC_FTR_HAS_NEW_STACR |
  305. EMAC_FTR_STACR_OC_INVERT | EMAC_FTR_440GX_PHY_CLK_FIX |
  306. #endif
  307. #ifdef CONFIG_IBM_EMAC_TAH
  308. EMAC_FTR_HAS_TAH |
  309. #endif
  310. #ifdef CONFIG_IBM_EMAC_ZMII
  311. EMAC_FTR_HAS_ZMII |
  312. #endif
  313. #ifdef CONFIG_IBM_EMAC_RGMII
  314. EMAC_FTR_HAS_RGMII |
  315. #endif
  316. #ifdef CONFIG_IBM_EMAC_NO_FLOW_CTRL
  317. EMAC_FTR_NO_FLOW_CONTROL_40x |
  318. #endif
  319. EMAC_FTR_460EX_PHY_CLK_FIX |
  320. EMAC_FTR_440EP_PHY_CLK_FIX |
  321. EMAC_APM821XX_REQ_JUMBO_FRAME_SIZE |
  322. EMAC_FTR_APM821XX_NO_HALF_DUPLEX,
  323. };
  324. static inline int emac_has_feature(struct emac_instance *dev,
  325. unsigned long feature)
  326. {
  327. return (EMAC_FTRS_ALWAYS & feature) ||
  328. (EMAC_FTRS_POSSIBLE & dev->features & feature);
  329. }
  330. /*
  331. * Various instances of the EMAC core have varying 1) number of
  332. * address match slots, 2) width of the registers for handling address
  333. * match slots, 3) number of registers for handling address match
  334. * slots and 4) base offset for those registers.
  335. *
  336. * These macros and inlines handle these differences based on
  337. * parameters supplied by the device structure which are, in turn,
  338. * initialized based on the "compatible" entry in the device tree.
  339. */
  340. #define EMAC4_XAHT_SLOTS_SHIFT 6
  341. #define EMAC4_XAHT_WIDTH_SHIFT 4
  342. #define EMAC4SYNC_XAHT_SLOTS_SHIFT 8
  343. #define EMAC4SYNC_XAHT_WIDTH_SHIFT 5
  344. #define EMAC_XAHT_SLOTS(dev) (1 << (dev)->xaht_slots_shift)
  345. #define EMAC_XAHT_WIDTH(dev) (1 << (dev)->xaht_width_shift)
  346. #define EMAC_XAHT_REGS(dev) (1 << ((dev)->xaht_slots_shift - \
  347. (dev)->xaht_width_shift))
  348. #define EMAC_XAHT_CRC_TO_SLOT(dev, crc) \
  349. ((EMAC_XAHT_SLOTS(dev) - 1) - \
  350. ((crc) >> ((sizeof (u32) * BITS_PER_BYTE) - \
  351. (dev)->xaht_slots_shift)))
  352. #define EMAC_XAHT_SLOT_TO_REG(dev, slot) \
  353. ((slot) >> (dev)->xaht_width_shift)
  354. #define EMAC_XAHT_SLOT_TO_MASK(dev, slot) \
  355. ((u32)(1 << (EMAC_XAHT_WIDTH(dev) - 1)) >> \
  356. ((slot) & (u32)(EMAC_XAHT_WIDTH(dev) - 1)))
  357. static inline u32 *emac_xaht_base(struct emac_instance *dev)
  358. {
  359. struct emac_regs __iomem *p = dev->emacp;
  360. int offset;
  361. /* The first IAHT entry always is the base of the block of
  362. * IAHT and GAHT registers.
  363. */
  364. if (emac_has_feature(dev, EMAC_FTR_EMAC4SYNC))
  365. offset = offsetof(struct emac_regs, u1.emac4sync.iaht1);
  366. else
  367. offset = offsetof(struct emac_regs, u0.emac4.iaht1);
  368. return (u32 *)((ptrdiff_t)p + offset);
  369. }
  370. static inline u32 *emac_gaht_base(struct emac_instance *dev)
  371. {
  372. /* GAHT registers always come after an identical number of
  373. * IAHT registers.
  374. */
  375. return emac_xaht_base(dev) + EMAC_XAHT_REGS(dev);
  376. }
  377. static inline u32 *emac_iaht_base(struct emac_instance *dev)
  378. {
  379. /* IAHT registers always come before an identical number of
  380. * GAHT registers.
  381. */
  382. return emac_xaht_base(dev);
  383. }
  384. /* Ethtool get_regs complex data.
  385. * We want to get not just EMAC registers, but also MAL, ZMII, RGMII, TAH
  386. * when available.
  387. *
  388. * Returned BLOB consists of the ibm_emac_ethtool_regs_hdr,
  389. * MAL registers, EMAC registers and optional ZMII, RGMII, TAH registers.
  390. * Each register component is preceded with emac_ethtool_regs_subhdr.
  391. * Order of the optional headers follows their relative bit posititions
  392. * in emac_ethtool_regs_hdr.components
  393. */
  394. #define EMAC_ETHTOOL_REGS_ZMII 0x00000001
  395. #define EMAC_ETHTOOL_REGS_RGMII 0x00000002
  396. #define EMAC_ETHTOOL_REGS_TAH 0x00000004
  397. struct emac_ethtool_regs_hdr {
  398. u32 components;
  399. };
  400. struct emac_ethtool_regs_subhdr {
  401. u32 version;
  402. u32 index;
  403. };
  404. #define EMAC_ETHTOOL_REGS_VER 3
  405. #define EMAC4_ETHTOOL_REGS_VER 4
  406. #define EMAC4SYNC_ETHTOOL_REGS_VER 5
  407. #endif /* __IBM_NEWEMAC_CORE_H */