core.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. * drivers/net/ibm_newemac/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/init.h>
  29. #include <linux/list.h>
  30. #include <linux/kernel.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/netdevice.h>
  33. #include <linux/dma-mapping.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/of_platform.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_NEW_EMAC_TXB
  46. #define NUM_RX_BUFF CONFIG_IBM_NEW_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_NEW_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 of_device *ofdev;
  154. struct device_node **blist; /* bootlist entry */
  155. /* MAL linkage */
  156. u32 mal_ph;
  157. struct of_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. u32 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. /* Shared MDIO if any */
  172. u32 mdio_ph;
  173. struct of_device *mdio_dev;
  174. struct emac_instance *mdio_instance;
  175. struct mutex mdio_lock;
  176. /* ZMII infos if any */
  177. u32 zmii_ph;
  178. u32 zmii_port;
  179. struct of_device *zmii_dev;
  180. /* RGMII infos if any */
  181. u32 rgmii_ph;
  182. u32 rgmii_port;
  183. struct of_device *rgmii_dev;
  184. /* TAH infos if any */
  185. u32 tah_ph;
  186. u32 tah_port;
  187. struct of_device *tah_dev;
  188. /* IRQs */
  189. int wol_irq;
  190. int emac_irq;
  191. /* OPB bus frequency in Mhz */
  192. u32 opb_bus_freq;
  193. /* Cell index within an ASIC (for clk mgmnt) */
  194. u32 cell_index;
  195. /* Max supported MTU */
  196. u32 max_mtu;
  197. /* Feature bits (from probe table) */
  198. unsigned int features;
  199. /* Tx and Rx fifo sizes & other infos in bytes */
  200. u32 tx_fifo_size;
  201. u32 tx_fifo_size_gige;
  202. u32 rx_fifo_size;
  203. u32 rx_fifo_size_gige;
  204. u32 fifo_entry_size;
  205. u32 mal_burst_size; /* move to MAL ? */
  206. /* IAHT and GAHT filter parameterization */
  207. u32 xaht_slots_shift;
  208. u32 xaht_width_shift;
  209. /* Descriptor management
  210. */
  211. struct mal_descriptor *tx_desc;
  212. int tx_cnt;
  213. int tx_slot;
  214. int ack_slot;
  215. struct mal_descriptor *rx_desc;
  216. int rx_slot;
  217. struct sk_buff *rx_sg_skb; /* 1 */
  218. int rx_skb_size;
  219. int rx_sync_size;
  220. struct sk_buff *tx_skb[NUM_TX_BUFF];
  221. struct sk_buff *rx_skb[NUM_RX_BUFF];
  222. /* Stats
  223. */
  224. struct emac_error_stats estats;
  225. struct net_device_stats nstats;
  226. struct emac_stats stats;
  227. /* Misc
  228. */
  229. int reset_failed;
  230. int stop_timeout; /* in us */
  231. int no_mcast;
  232. int mcast_pending;
  233. int opened;
  234. struct work_struct reset_work;
  235. spinlock_t lock;
  236. };
  237. /*
  238. * Features of various EMAC implementations
  239. */
  240. /*
  241. * No flow control on 40x according to the original driver
  242. */
  243. #define EMAC_FTR_NO_FLOW_CONTROL_40x 0x00000001
  244. /*
  245. * Cell is an EMAC4
  246. */
  247. #define EMAC_FTR_EMAC4 0x00000002
  248. /*
  249. * For the 440SPe, AMCC inexplicably changed the polarity of
  250. * the "operation complete" bit in the MII control register.
  251. */
  252. #define EMAC_FTR_STACR_OC_INVERT 0x00000004
  253. /*
  254. * Set if we have a TAH.
  255. */
  256. #define EMAC_FTR_HAS_TAH 0x00000008
  257. /*
  258. * Set if we have a ZMII.
  259. */
  260. #define EMAC_FTR_HAS_ZMII 0x00000010
  261. /*
  262. * Set if we have a RGMII.
  263. */
  264. #define EMAC_FTR_HAS_RGMII 0x00000020
  265. /*
  266. * Set if we have new type STACR with STAOPC
  267. */
  268. #define EMAC_FTR_HAS_NEW_STACR 0x00000040
  269. /*
  270. * Set if we need phy clock workaround for 440gx
  271. */
  272. #define EMAC_FTR_440GX_PHY_CLK_FIX 0x00000080
  273. /*
  274. * Set if we need phy clock workaround for 440ep or 440gr
  275. */
  276. #define EMAC_FTR_440EP_PHY_CLK_FIX 0x00000100
  277. /*
  278. * The 405EX and 460EX contain the EMAC4SYNC core
  279. */
  280. #define EMAC_FTR_EMAC4SYNC 0x00000200
  281. /*
  282. * Set if we need phy clock workaround for 460ex or 460gt
  283. */
  284. #define EMAC_FTR_460EX_PHY_CLK_FIX 0x00000400
  285. /* Right now, we don't quite handle the always/possible masks on the
  286. * most optimal way as we don't have a way to say something like
  287. * always EMAC4. Patches welcome.
  288. */
  289. enum {
  290. EMAC_FTRS_ALWAYS = 0,
  291. EMAC_FTRS_POSSIBLE =
  292. #ifdef CONFIG_IBM_NEW_EMAC_EMAC4
  293. EMAC_FTR_EMAC4 | EMAC_FTR_EMAC4SYNC |
  294. EMAC_FTR_HAS_NEW_STACR |
  295. EMAC_FTR_STACR_OC_INVERT | EMAC_FTR_440GX_PHY_CLK_FIX |
  296. #endif
  297. #ifdef CONFIG_IBM_NEW_EMAC_TAH
  298. EMAC_FTR_HAS_TAH |
  299. #endif
  300. #ifdef CONFIG_IBM_NEW_EMAC_ZMII
  301. EMAC_FTR_HAS_ZMII |
  302. #endif
  303. #ifdef CONFIG_IBM_NEW_EMAC_RGMII
  304. EMAC_FTR_HAS_RGMII |
  305. #endif
  306. EMAC_FTR_460EX_PHY_CLK_FIX |
  307. EMAC_FTR_440EP_PHY_CLK_FIX,
  308. };
  309. static inline int emac_has_feature(struct emac_instance *dev,
  310. unsigned long feature)
  311. {
  312. return (EMAC_FTRS_ALWAYS & feature) ||
  313. (EMAC_FTRS_POSSIBLE & dev->features & feature);
  314. }
  315. /*
  316. * Various instances of the EMAC core have varying 1) number of
  317. * address match slots, 2) width of the registers for handling address
  318. * match slots, 3) number of registers for handling address match
  319. * slots and 4) base offset for those registers.
  320. *
  321. * These macros and inlines handle these differences based on
  322. * parameters supplied by the device structure which are, in turn,
  323. * initialized based on the "compatible" entry in the device tree.
  324. */
  325. #define EMAC4_XAHT_SLOTS_SHIFT 6
  326. #define EMAC4_XAHT_WIDTH_SHIFT 4
  327. #define EMAC4SYNC_XAHT_SLOTS_SHIFT 8
  328. #define EMAC4SYNC_XAHT_WIDTH_SHIFT 5
  329. #define EMAC_XAHT_SLOTS(dev) (1 << (dev)->xaht_slots_shift)
  330. #define EMAC_XAHT_WIDTH(dev) (1 << (dev)->xaht_width_shift)
  331. #define EMAC_XAHT_REGS(dev) (1 << ((dev)->xaht_slots_shift - \
  332. (dev)->xaht_width_shift))
  333. #define EMAC_XAHT_CRC_TO_SLOT(dev, crc) \
  334. ((EMAC_XAHT_SLOTS(dev) - 1) - \
  335. ((crc) >> ((sizeof (u32) * BITS_PER_BYTE) - \
  336. (dev)->xaht_slots_shift)))
  337. #define EMAC_XAHT_SLOT_TO_REG(dev, slot) \
  338. ((slot) >> (dev)->xaht_width_shift)
  339. #define EMAC_XAHT_SLOT_TO_MASK(dev, slot) \
  340. ((u32)(1 << (EMAC_XAHT_WIDTH(dev) - 1)) >> \
  341. ((slot) & (u32)(EMAC_XAHT_WIDTH(dev) - 1)))
  342. static inline u32 *emac_xaht_base(struct emac_instance *dev)
  343. {
  344. struct emac_regs __iomem *p = dev->emacp;
  345. int offset;
  346. /* The first IAHT entry always is the base of the block of
  347. * IAHT and GAHT registers.
  348. */
  349. if (emac_has_feature(dev, EMAC_FTR_EMAC4SYNC))
  350. offset = offsetof(struct emac_regs, u1.emac4sync.iaht1);
  351. else
  352. offset = offsetof(struct emac_regs, u0.emac4.iaht1);
  353. return ((u32 *)((ptrdiff_t)p + offset));
  354. }
  355. static inline u32 *emac_gaht_base(struct emac_instance *dev)
  356. {
  357. /* GAHT registers always come after an identical number of
  358. * IAHT registers.
  359. */
  360. return (emac_xaht_base(dev) + EMAC_XAHT_REGS(dev));
  361. }
  362. static inline u32 *emac_iaht_base(struct emac_instance *dev)
  363. {
  364. /* IAHT registers always come before an identical number of
  365. * GAHT registers.
  366. */
  367. return (emac_xaht_base(dev));
  368. }
  369. /* Ethtool get_regs complex data.
  370. * We want to get not just EMAC registers, but also MAL, ZMII, RGMII, TAH
  371. * when available.
  372. *
  373. * Returned BLOB consists of the ibm_emac_ethtool_regs_hdr,
  374. * MAL registers, EMAC registers and optional ZMII, RGMII, TAH registers.
  375. * Each register component is preceded with emac_ethtool_regs_subhdr.
  376. * Order of the optional headers follows their relative bit posititions
  377. * in emac_ethtool_regs_hdr.components
  378. */
  379. #define EMAC_ETHTOOL_REGS_ZMII 0x00000001
  380. #define EMAC_ETHTOOL_REGS_RGMII 0x00000002
  381. #define EMAC_ETHTOOL_REGS_TAH 0x00000004
  382. struct emac_ethtool_regs_hdr {
  383. u32 components;
  384. };
  385. struct emac_ethtool_regs_subhdr {
  386. u32 version;
  387. u32 index;
  388. };
  389. #define EMAC_ETHTOOL_REGS_VER 0
  390. #define EMAC_ETHTOOL_REGS_SIZE(dev) ((dev)->rsrc_regs.end - \
  391. (dev)->rsrc_regs.start + 1)
  392. #define EMAC4_ETHTOOL_REGS_VER 1
  393. #define EMAC4_ETHTOOL_REGS_SIZE(dev) ((dev)->rsrc_regs.end - \
  394. (dev)->rsrc_regs.start + 1)
  395. #endif /* __IBM_NEWEMAC_CORE_H */