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