xref: /openbmc/linux/drivers/net/ethernet/ibm/emac/mal.h (revision 2874c5fd)
12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
29aa32835SJeff Kirsher /*
33396c782SPaul Gortmaker  * drivers/net/ethernet/ibm/emac/mal.h
49aa32835SJeff Kirsher  *
59aa32835SJeff Kirsher  * Memory Access Layer (MAL) support
69aa32835SJeff Kirsher  *
79aa32835SJeff Kirsher  * Copyright 2007 Benjamin Herrenschmidt, IBM Corp.
89aa32835SJeff Kirsher  *                <benh@kernel.crashing.org>
99aa32835SJeff Kirsher  *
109aa32835SJeff Kirsher  * Based on the arch/ppc version of the driver:
119aa32835SJeff Kirsher  *
129aa32835SJeff Kirsher  * Copyright (c) 2004, 2005 Zultys Technologies.
139aa32835SJeff Kirsher  * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
149aa32835SJeff Kirsher  *
159aa32835SJeff Kirsher  * Based on original work by
169aa32835SJeff Kirsher  *      Armin Kuster <akuster@mvista.com>
179aa32835SJeff Kirsher  *      Copyright 2002 MontaVista Softare Inc.
189aa32835SJeff Kirsher  */
199aa32835SJeff Kirsher #ifndef __IBM_NEWEMAC_MAL_H
209aa32835SJeff Kirsher #define __IBM_NEWEMAC_MAL_H
219aa32835SJeff Kirsher 
229aa32835SJeff Kirsher /*
239aa32835SJeff Kirsher  * There are some variations on the MAL, we express them in this driver as
249aa32835SJeff Kirsher  * MAL Version 1 and 2 though that doesn't match any IBM terminology.
259aa32835SJeff Kirsher  *
269aa32835SJeff Kirsher  * We call MAL 1 the version in 405GP, 405GPR, 405EP, 440EP, 440GR and
279aa32835SJeff Kirsher  * NP405H.
289aa32835SJeff Kirsher  *
299aa32835SJeff Kirsher  * We call MAL 2 the version in 440GP, 440GX, 440SP, 440SPE and Axon
309aa32835SJeff Kirsher  *
319aa32835SJeff Kirsher  * The driver expects a "version" property in the emac node containing
329aa32835SJeff Kirsher  * a number 1 or 2. New device-trees for EMAC capable platforms are thus
339aa32835SJeff Kirsher  * required to include that when porting to arch/powerpc.
349aa32835SJeff Kirsher  */
359aa32835SJeff Kirsher 
369aa32835SJeff Kirsher /* MALx DCR registers */
379aa32835SJeff Kirsher #define	MAL_CFG			0x00
389aa32835SJeff Kirsher #define	  MAL_CFG_SR		0x80000000
399aa32835SJeff Kirsher #define   MAL_CFG_PLBB		0x00004000
409aa32835SJeff Kirsher #define   MAL_CFG_OPBBL		0x00000080
419aa32835SJeff Kirsher #define   MAL_CFG_EOPIE		0x00000004
429aa32835SJeff Kirsher #define   MAL_CFG_LEA		0x00000002
439aa32835SJeff Kirsher #define   MAL_CFG_SD		0x00000001
449aa32835SJeff Kirsher 
459aa32835SJeff Kirsher /* MAL V1 CFG bits */
469aa32835SJeff Kirsher #define   MAL1_CFG_PLBP_MASK	0x00c00000
479aa32835SJeff Kirsher #define   MAL1_CFG_PLBP_10	0x00800000
489aa32835SJeff Kirsher #define   MAL1_CFG_GA		0x00200000
499aa32835SJeff Kirsher #define   MAL1_CFG_OA		0x00100000
509aa32835SJeff Kirsher #define   MAL1_CFG_PLBLE	0x00080000
519aa32835SJeff Kirsher #define   MAL1_CFG_PLBT_MASK	0x00078000
529aa32835SJeff Kirsher #define   MAL1_CFG_DEFAULT	(MAL1_CFG_PLBP_10 | MAL1_CFG_PLBT_MASK)
539aa32835SJeff Kirsher 
549aa32835SJeff Kirsher /* MAL V2 CFG bits */
559aa32835SJeff Kirsher #define   MAL2_CFG_RPP_MASK	0x00c00000
569aa32835SJeff Kirsher #define   MAL2_CFG_RPP_10	0x00800000
579aa32835SJeff Kirsher #define   MAL2_CFG_RMBS_MASK	0x00300000
589aa32835SJeff Kirsher #define   MAL2_CFG_WPP_MASK	0x000c0000
599aa32835SJeff Kirsher #define   MAL2_CFG_WPP_10	0x00080000
609aa32835SJeff Kirsher #define   MAL2_CFG_WMBS_MASK	0x00030000
619aa32835SJeff Kirsher #define   MAL2_CFG_PLBLE	0x00008000
629aa32835SJeff Kirsher #define   MAL2_CFG_DEFAULT	(MAL2_CFG_RMBS_MASK | MAL2_CFG_WMBS_MASK | \
639aa32835SJeff Kirsher 				 MAL2_CFG_RPP_10 | MAL2_CFG_WPP_10)
649aa32835SJeff Kirsher 
659aa32835SJeff Kirsher #define MAL_ESR			0x01
669aa32835SJeff Kirsher #define   MAL_ESR_EVB		0x80000000
679aa32835SJeff Kirsher #define   MAL_ESR_CIDT		0x40000000
689aa32835SJeff Kirsher #define   MAL_ESR_CID_MASK	0x3e000000
699aa32835SJeff Kirsher #define   MAL_ESR_CID_SHIFT	25
709aa32835SJeff Kirsher #define   MAL_ESR_DE		0x00100000
719aa32835SJeff Kirsher #define   MAL_ESR_OTE		0x00040000
729aa32835SJeff Kirsher #define   MAL_ESR_OSE		0x00020000
739aa32835SJeff Kirsher #define   MAL_ESR_PEIN		0x00010000
749aa32835SJeff Kirsher #define   MAL_ESR_DEI		0x00000010
759aa32835SJeff Kirsher #define   MAL_ESR_OTEI		0x00000004
769aa32835SJeff Kirsher #define   MAL_ESR_OSEI		0x00000002
779aa32835SJeff Kirsher #define   MAL_ESR_PBEI		0x00000001
789aa32835SJeff Kirsher 
799aa32835SJeff Kirsher /* MAL V1 ESR bits */
809aa32835SJeff Kirsher #define   MAL1_ESR_ONE		0x00080000
819aa32835SJeff Kirsher #define   MAL1_ESR_ONEI		0x00000008
829aa32835SJeff Kirsher 
839aa32835SJeff Kirsher /* MAL V2 ESR bits */
849aa32835SJeff Kirsher #define   MAL2_ESR_PTE		0x00800000
859aa32835SJeff Kirsher #define   MAL2_ESR_PRE		0x00400000
869aa32835SJeff Kirsher #define   MAL2_ESR_PWE		0x00200000
879aa32835SJeff Kirsher #define   MAL2_ESR_PTEI		0x00000080
889aa32835SJeff Kirsher #define   MAL2_ESR_PREI		0x00000040
899aa32835SJeff Kirsher #define   MAL2_ESR_PWEI		0x00000020
909aa32835SJeff Kirsher 
919aa32835SJeff Kirsher 
929aa32835SJeff Kirsher #define MAL_IER			0x02
9309271db6SIvan Mikhaylov /* MAL IER bits */
949aa32835SJeff Kirsher #define   MAL_IER_DE		0x00000010
959aa32835SJeff Kirsher #define   MAL_IER_OTE		0x00000004
969aa32835SJeff Kirsher #define   MAL_IER_OE		0x00000002
979aa32835SJeff Kirsher #define   MAL_IER_PE		0x00000001
989aa32835SJeff Kirsher 
9909271db6SIvan Mikhaylov /* PLB read/write/timeout errors */
10009271db6SIvan Mikhaylov #define   MAL_IER_PTE		0x00000080
10109271db6SIvan Mikhaylov #define   MAL_IER_PRE		0x00000040
10209271db6SIvan Mikhaylov #define   MAL_IER_PWE		0x00000020
1039aa32835SJeff Kirsher 
10409271db6SIvan Mikhaylov #define   MAL_IER_SOC_EVENTS	(MAL_IER_PTE | MAL_IER_PRE | MAL_IER_PWE)
10509271db6SIvan Mikhaylov #define   MAL_IER_EVENTS	(MAL_IER_SOC_EVENTS | MAL_IER_DE | \
10609271db6SIvan Mikhaylov 				 MAL_IER_OTE | MAL_IER_OE | MAL_IER_PE)
1079aa32835SJeff Kirsher 
1089aa32835SJeff Kirsher #define MAL_TXCASR		0x04
1099aa32835SJeff Kirsher #define MAL_TXCARR		0x05
1109aa32835SJeff Kirsher #define MAL_TXEOBISR		0x06
1119aa32835SJeff Kirsher #define MAL_TXDEIR		0x07
1129aa32835SJeff Kirsher #define MAL_RXCASR		0x10
1139aa32835SJeff Kirsher #define MAL_RXCARR		0x11
1149aa32835SJeff Kirsher #define MAL_RXEOBISR		0x12
1159aa32835SJeff Kirsher #define MAL_RXDEIR		0x13
1169aa32835SJeff Kirsher #define MAL_TXCTPR(n)		((n) + 0x20)
1179aa32835SJeff Kirsher #define MAL_RXCTPR(n)		((n) + 0x40)
1189aa32835SJeff Kirsher #define MAL_RCBS(n)		((n) + 0x60)
1199aa32835SJeff Kirsher 
1209aa32835SJeff Kirsher /* In reality MAL can handle TX buffers up to 4095 bytes long,
1219aa32835SJeff Kirsher  * but this isn't a good round number :) 		 --ebs
1229aa32835SJeff Kirsher  */
1239aa32835SJeff Kirsher #define MAL_MAX_TX_SIZE		4080
1249aa32835SJeff Kirsher #define MAL_MAX_RX_SIZE		4080
1259aa32835SJeff Kirsher 
mal_rx_size(int len)1269aa32835SJeff Kirsher static inline int mal_rx_size(int len)
1279aa32835SJeff Kirsher {
1289aa32835SJeff Kirsher 	len = (len + 0xf) & ~0xf;
1299aa32835SJeff Kirsher 	return len > MAL_MAX_RX_SIZE ? MAL_MAX_RX_SIZE : len;
1309aa32835SJeff Kirsher }
1319aa32835SJeff Kirsher 
mal_tx_chunks(int len)1329aa32835SJeff Kirsher static inline int mal_tx_chunks(int len)
1339aa32835SJeff Kirsher {
134f8a1988fSzhong jiang 	return DIV_ROUND_UP(len, MAL_MAX_TX_SIZE);
1359aa32835SJeff Kirsher }
1369aa32835SJeff Kirsher 
1379aa32835SJeff Kirsher #define MAL_CHAN_MASK(n)	(0x80000000 >> (n))
1389aa32835SJeff Kirsher 
1399aa32835SJeff Kirsher /* MAL Buffer Descriptor structure */
1409aa32835SJeff Kirsher struct mal_descriptor {
1419aa32835SJeff Kirsher 	u16 ctrl;		/* MAL / Commac status control bits */
1429aa32835SJeff Kirsher 	u16 data_len;		/* Max length is 4K-1 (12 bits)     */
1439aa32835SJeff Kirsher 	u32 data_ptr;		/* pointer to actual data buffer    */
1449aa32835SJeff Kirsher };
1459aa32835SJeff Kirsher 
1469aa32835SJeff Kirsher /* the following defines are for the MadMAL status and control registers. */
1479aa32835SJeff Kirsher /* MADMAL transmit and receive status/control bits  */
1489aa32835SJeff Kirsher #define MAL_RX_CTRL_EMPTY	0x8000
1499aa32835SJeff Kirsher #define MAL_RX_CTRL_WRAP	0x4000
1509aa32835SJeff Kirsher #define MAL_RX_CTRL_CM		0x2000
1519aa32835SJeff Kirsher #define MAL_RX_CTRL_LAST	0x1000
1529aa32835SJeff Kirsher #define MAL_RX_CTRL_FIRST	0x0800
1539aa32835SJeff Kirsher #define MAL_RX_CTRL_INTR	0x0400
1549aa32835SJeff Kirsher #define MAL_RX_CTRL_SINGLE	(MAL_RX_CTRL_LAST | MAL_RX_CTRL_FIRST)
1559aa32835SJeff Kirsher #define MAL_IS_SINGLE_RX(ctrl)	(((ctrl) & MAL_RX_CTRL_SINGLE) == MAL_RX_CTRL_SINGLE)
1569aa32835SJeff Kirsher 
1579aa32835SJeff Kirsher #define MAL_TX_CTRL_READY	0x8000
1589aa32835SJeff Kirsher #define MAL_TX_CTRL_WRAP	0x4000
1599aa32835SJeff Kirsher #define MAL_TX_CTRL_CM		0x2000
1609aa32835SJeff Kirsher #define MAL_TX_CTRL_LAST	0x1000
1619aa32835SJeff Kirsher #define MAL_TX_CTRL_INTR	0x0400
1629aa32835SJeff Kirsher 
1639aa32835SJeff Kirsher struct mal_commac_ops {
1649aa32835SJeff Kirsher 	void	(*poll_tx) (void *dev);
1659aa32835SJeff Kirsher 	int	(*poll_rx) (void *dev, int budget);
1669aa32835SJeff Kirsher 	int	(*peek_rx) (void *dev);
1679aa32835SJeff Kirsher 	void	(*rxde) (void *dev);
1689aa32835SJeff Kirsher };
1699aa32835SJeff Kirsher 
1709aa32835SJeff Kirsher struct mal_commac {
1719aa32835SJeff Kirsher 	struct mal_commac_ops	*ops;
1729aa32835SJeff Kirsher 	void			*dev;
1739aa32835SJeff Kirsher 	struct list_head	poll_list;
1749aa32835SJeff Kirsher 	long       		flags;
1759aa32835SJeff Kirsher #define MAL_COMMAC_RX_STOPPED		0
1769aa32835SJeff Kirsher #define MAL_COMMAC_POLL_DISABLED	1
1779aa32835SJeff Kirsher 	u32			tx_chan_mask;
1789aa32835SJeff Kirsher 	u32			rx_chan_mask;
1799aa32835SJeff Kirsher 	struct list_head	list;
1809aa32835SJeff Kirsher };
1819aa32835SJeff Kirsher 
1829aa32835SJeff Kirsher struct mal_instance {
1839aa32835SJeff Kirsher 	int			version;
1849aa32835SJeff Kirsher 	dcr_host_t		dcr_host;
1859aa32835SJeff Kirsher 
1869aa32835SJeff Kirsher 	int			num_tx_chans;	/* Number of TX channels */
1879aa32835SJeff Kirsher 	int			num_rx_chans;	/* Number of RX channels */
1889aa32835SJeff Kirsher 	int 			txeob_irq;	/* TX End Of Buffer IRQ  */
1899aa32835SJeff Kirsher 	int 			rxeob_irq;	/* RX End Of Buffer IRQ  */
1909aa32835SJeff Kirsher 	int			txde_irq;	/* TX Descriptor Error IRQ */
1919aa32835SJeff Kirsher 	int			rxde_irq;	/* RX Descriptor Error IRQ */
1929aa32835SJeff Kirsher 	int			serr_irq;	/* MAL System Error IRQ    */
1939aa32835SJeff Kirsher 
1949aa32835SJeff Kirsher 	struct list_head	poll_list;
1959aa32835SJeff Kirsher 	struct napi_struct	napi;
1969aa32835SJeff Kirsher 
1979aa32835SJeff Kirsher 	struct list_head	list;
1989aa32835SJeff Kirsher 	u32			tx_chan_mask;
1999aa32835SJeff Kirsher 	u32			rx_chan_mask;
2009aa32835SJeff Kirsher 
2019aa32835SJeff Kirsher 	dma_addr_t		bd_dma;
2029aa32835SJeff Kirsher 	struct mal_descriptor	*bd_virt;
2039aa32835SJeff Kirsher 
2049aa32835SJeff Kirsher 	struct platform_device	*ofdev;
2059aa32835SJeff Kirsher 	int			index;
2069aa32835SJeff Kirsher 	spinlock_t		lock;
2079aa32835SJeff Kirsher 
2089aa32835SJeff Kirsher 	struct net_device	dummy_dev;
2099aa32835SJeff Kirsher 
2109aa32835SJeff Kirsher 	unsigned int features;
2119aa32835SJeff Kirsher };
2129aa32835SJeff Kirsher 
get_mal_dcrn(struct mal_instance * mal,int reg)2139aa32835SJeff Kirsher static inline u32 get_mal_dcrn(struct mal_instance *mal, int reg)
2149aa32835SJeff Kirsher {
2159aa32835SJeff Kirsher 	return dcr_read(mal->dcr_host, reg);
2169aa32835SJeff Kirsher }
2179aa32835SJeff Kirsher 
set_mal_dcrn(struct mal_instance * mal,int reg,u32 val)2189aa32835SJeff Kirsher static inline void set_mal_dcrn(struct mal_instance *mal, int reg, u32 val)
2199aa32835SJeff Kirsher {
2209aa32835SJeff Kirsher 	dcr_write(mal->dcr_host, reg, val);
2219aa32835SJeff Kirsher }
2229aa32835SJeff Kirsher 
2239aa32835SJeff Kirsher /* Features of various MAL implementations */
2249aa32835SJeff Kirsher 
2259aa32835SJeff Kirsher /* Set if you have interrupt coalescing and you have to clear the SDR
2269aa32835SJeff Kirsher  * register for TXEOB and RXEOB interrupts to work
2279aa32835SJeff Kirsher  */
2289aa32835SJeff Kirsher #define MAL_FTR_CLEAR_ICINTSTAT	0x00000001
2299aa32835SJeff Kirsher 
2309aa32835SJeff Kirsher /* Set if your MAL has SERR, TXDE, and RXDE OR'd into a single UIC
2319aa32835SJeff Kirsher  * interrupt
2329aa32835SJeff Kirsher  */
2339aa32835SJeff Kirsher #define MAL_FTR_COMMON_ERR_INT	0x00000002
2349aa32835SJeff Kirsher 
2359aa32835SJeff Kirsher enum {
2369aa32835SJeff Kirsher 	MAL_FTRS_ALWAYS = 0,
2379aa32835SJeff Kirsher 
2389aa32835SJeff Kirsher 	MAL_FTRS_POSSIBLE =
2393b3bceefSTony Breeds #ifdef CONFIG_IBM_EMAC_MAL_CLR_ICINTSTAT
2409aa32835SJeff Kirsher 		MAL_FTR_CLEAR_ICINTSTAT |
2419aa32835SJeff Kirsher #endif
2423b3bceefSTony Breeds #ifdef CONFIG_IBM_EMAC_MAL_COMMON_ERR
2439aa32835SJeff Kirsher 		MAL_FTR_COMMON_ERR_INT |
2449aa32835SJeff Kirsher #endif
2459aa32835SJeff Kirsher 		0,
2469aa32835SJeff Kirsher };
2479aa32835SJeff Kirsher 
mal_has_feature(struct mal_instance * dev,unsigned long feature)2489aa32835SJeff Kirsher static inline int mal_has_feature(struct mal_instance *dev,
2499aa32835SJeff Kirsher 		unsigned long feature)
2509aa32835SJeff Kirsher {
2519aa32835SJeff Kirsher 	return (MAL_FTRS_ALWAYS & feature) ||
2529aa32835SJeff Kirsher 		(MAL_FTRS_POSSIBLE & dev->features & feature);
2539aa32835SJeff Kirsher }
2549aa32835SJeff Kirsher 
2559aa32835SJeff Kirsher /* Register MAL devices */
2569aa32835SJeff Kirsher int mal_init(void);
2579aa32835SJeff Kirsher void mal_exit(void);
2589aa32835SJeff Kirsher 
2599aa32835SJeff Kirsher int mal_register_commac(struct mal_instance *mal,
2609aa32835SJeff Kirsher 			struct mal_commac *commac);
2619aa32835SJeff Kirsher void mal_unregister_commac(struct mal_instance *mal,
2629aa32835SJeff Kirsher 			   struct mal_commac *commac);
2639aa32835SJeff Kirsher int mal_set_rcbs(struct mal_instance *mal, int channel, unsigned long size);
2649aa32835SJeff Kirsher 
2659aa32835SJeff Kirsher /* Returns BD ring offset for a particular channel
2669aa32835SJeff Kirsher    (in 'struct mal_descriptor' elements)
2679aa32835SJeff Kirsher */
2689aa32835SJeff Kirsher int mal_tx_bd_offset(struct mal_instance *mal, int channel);
2699aa32835SJeff Kirsher int mal_rx_bd_offset(struct mal_instance *mal, int channel);
2709aa32835SJeff Kirsher 
2719aa32835SJeff Kirsher void mal_enable_tx_channel(struct mal_instance *mal, int channel);
2729aa32835SJeff Kirsher void mal_disable_tx_channel(struct mal_instance *mal, int channel);
2739aa32835SJeff Kirsher void mal_enable_rx_channel(struct mal_instance *mal, int channel);
2749aa32835SJeff Kirsher void mal_disable_rx_channel(struct mal_instance *mal, int channel);
2759aa32835SJeff Kirsher 
2769aa32835SJeff Kirsher void mal_poll_disable(struct mal_instance *mal, struct mal_commac *commac);
2779aa32835SJeff Kirsher void mal_poll_enable(struct mal_instance *mal, struct mal_commac *commac);
2789aa32835SJeff Kirsher 
2799aa32835SJeff Kirsher /* Add/remove EMAC to/from MAL polling list */
2809aa32835SJeff Kirsher void mal_poll_add(struct mal_instance *mal, struct mal_commac *commac);
2819aa32835SJeff Kirsher void mal_poll_del(struct mal_instance *mal, struct mal_commac *commac);
2829aa32835SJeff Kirsher 
2839aa32835SJeff Kirsher /* Ethtool MAL registers */
2849aa32835SJeff Kirsher struct mal_regs {
2859aa32835SJeff Kirsher 	u32 tx_count;
2869aa32835SJeff Kirsher 	u32 rx_count;
2879aa32835SJeff Kirsher 
2889aa32835SJeff Kirsher 	u32 cfg;
2899aa32835SJeff Kirsher 	u32 esr;
2909aa32835SJeff Kirsher 	u32 ier;
2919aa32835SJeff Kirsher 	u32 tx_casr;
2929aa32835SJeff Kirsher 	u32 tx_carr;
2939aa32835SJeff Kirsher 	u32 tx_eobisr;
2949aa32835SJeff Kirsher 	u32 tx_deir;
2959aa32835SJeff Kirsher 	u32 rx_casr;
2969aa32835SJeff Kirsher 	u32 rx_carr;
2979aa32835SJeff Kirsher 	u32 rx_eobisr;
2989aa32835SJeff Kirsher 	u32 rx_deir;
2999aa32835SJeff Kirsher 	u32 tx_ctpr[32];
3009aa32835SJeff Kirsher 	u32 rx_ctpr[32];
3019aa32835SJeff Kirsher 	u32 rcbs[32];
3029aa32835SJeff Kirsher };
3039aa32835SJeff Kirsher 
3049aa32835SJeff Kirsher int mal_get_regs_len(struct mal_instance *mal);
3059aa32835SJeff Kirsher void *mal_dump_regs(struct mal_instance *mal, void *buf);
3069aa32835SJeff Kirsher 
3079aa32835SJeff Kirsher #endif /* __IBM_NEWEMAC_MAL_H */
308