xref: /rk3399_rockchip-uboot/drivers/net/cpsw.c (revision 2e205ef7eb9e7793e922d0b6cd0fe4181309c058)
12b62997cSCyril Chemparathy /*
22b62997cSCyril Chemparathy  * CPSW Ethernet Switch Driver
32b62997cSCyril Chemparathy  *
42b62997cSCyril Chemparathy  * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/
52b62997cSCyril Chemparathy  *
62b62997cSCyril Chemparathy  * This program is free software; you can redistribute it and/or
72b62997cSCyril Chemparathy  * modify it under the terms of the GNU General Public License as
82b62997cSCyril Chemparathy  * published by the Free Software Foundation version 2.
92b62997cSCyril Chemparathy  *
102b62997cSCyril Chemparathy  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
112b62997cSCyril Chemparathy  * kind, whether express or implied; without even the implied warranty
122b62997cSCyril Chemparathy  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
132b62997cSCyril Chemparathy  * GNU General Public License for more details.
142b62997cSCyril Chemparathy  */
152b62997cSCyril Chemparathy 
162b62997cSCyril Chemparathy #include <common.h>
172b62997cSCyril Chemparathy #include <command.h>
182b62997cSCyril Chemparathy #include <net.h>
192b62997cSCyril Chemparathy #include <miiphy.h>
202b62997cSCyril Chemparathy #include <malloc.h>
212b62997cSCyril Chemparathy #include <net.h>
222b62997cSCyril Chemparathy #include <netdev.h>
232b62997cSCyril Chemparathy #include <cpsw.h>
242b62997cSCyril Chemparathy #include <asm/errno.h>
25*2e205ef7SVignesh R #include <asm/gpio.h>
262b62997cSCyril Chemparathy #include <asm/io.h>
272b62997cSCyril Chemparathy #include <phy.h>
2898f92001STom Rini #include <asm/arch/cpu.h>
294cc77895SMugunthan V N #include <dm.h>
30e4310566SMugunthan V N #include <fdt_support.h>
314cc77895SMugunthan V N 
324cc77895SMugunthan V N DECLARE_GLOBAL_DATA_PTR;
332b62997cSCyril Chemparathy 
342b62997cSCyril Chemparathy #define BITMASK(bits)		(BIT(bits) - 1)
352b62997cSCyril Chemparathy #define PHY_REG_MASK		0x1f
362b62997cSCyril Chemparathy #define PHY_ID_MASK		0x1f
372b62997cSCyril Chemparathy #define NUM_DESCS		(PKTBUFSRX * 2)
382b62997cSCyril Chemparathy #define PKT_MIN			60
392b62997cSCyril Chemparathy #define PKT_MAX			(1500 + 14 + 4 + 4)
402b62997cSCyril Chemparathy #define CLEAR_BIT		1
412b62997cSCyril Chemparathy #define GIGABITEN		BIT(7)
422b62997cSCyril Chemparathy #define FULLDUPLEXEN		BIT(0)
432b62997cSCyril Chemparathy #define MIIEN			BIT(15)
442b62997cSCyril Chemparathy 
454cc77895SMugunthan V N /* reg offset */
464cc77895SMugunthan V N #define CPSW_HOST_PORT_OFFSET	0x108
474cc77895SMugunthan V N #define CPSW_SLAVE0_OFFSET	0x208
484cc77895SMugunthan V N #define CPSW_SLAVE1_OFFSET	0x308
494cc77895SMugunthan V N #define CPSW_SLAVE_SIZE		0x100
504cc77895SMugunthan V N #define CPSW_CPDMA_OFFSET	0x800
514cc77895SMugunthan V N #define CPSW_HW_STATS		0x900
524cc77895SMugunthan V N #define CPSW_STATERAM_OFFSET	0xa00
534cc77895SMugunthan V N #define CPSW_CPTS_OFFSET	0xc00
544cc77895SMugunthan V N #define CPSW_ALE_OFFSET		0xd00
554cc77895SMugunthan V N #define CPSW_SLIVER0_OFFSET	0xd80
564cc77895SMugunthan V N #define CPSW_SLIVER1_OFFSET	0xdc0
574cc77895SMugunthan V N #define CPSW_BD_OFFSET		0x2000
584cc77895SMugunthan V N #define CPSW_MDIO_DIV		0xff
594cc77895SMugunthan V N 
604cc77895SMugunthan V N #define AM335X_GMII_SEL_OFFSET	0x630
614cc77895SMugunthan V N 
622b62997cSCyril Chemparathy /* DMA Registers */
632b62997cSCyril Chemparathy #define CPDMA_TXCONTROL		0x004
642b62997cSCyril Chemparathy #define CPDMA_RXCONTROL		0x014
652b62997cSCyril Chemparathy #define CPDMA_SOFTRESET		0x01c
662b62997cSCyril Chemparathy #define CPDMA_RXFREE		0x0e0
672b62997cSCyril Chemparathy #define CPDMA_TXHDP_VER1	0x100
682b62997cSCyril Chemparathy #define CPDMA_TXHDP_VER2	0x200
692b62997cSCyril Chemparathy #define CPDMA_RXHDP_VER1	0x120
702b62997cSCyril Chemparathy #define CPDMA_RXHDP_VER2	0x220
712b62997cSCyril Chemparathy #define CPDMA_TXCP_VER1		0x140
722b62997cSCyril Chemparathy #define CPDMA_TXCP_VER2		0x240
732b62997cSCyril Chemparathy #define CPDMA_RXCP_VER1		0x160
742b62997cSCyril Chemparathy #define CPDMA_RXCP_VER2		0x260
752b62997cSCyril Chemparathy 
762b62997cSCyril Chemparathy /* Descriptor mode bits */
772b62997cSCyril Chemparathy #define CPDMA_DESC_SOP		BIT(31)
782b62997cSCyril Chemparathy #define CPDMA_DESC_EOP		BIT(30)
792b62997cSCyril Chemparathy #define CPDMA_DESC_OWNER	BIT(29)
802b62997cSCyril Chemparathy #define CPDMA_DESC_EOQ		BIT(28)
812b62997cSCyril Chemparathy 
822b62997cSCyril Chemparathy /*
832b62997cSCyril Chemparathy  * This timeout definition is a worst-case ultra defensive measure against
842b62997cSCyril Chemparathy  * unexpected controller lock ups.  Ideally, we should never ever hit this
852b62997cSCyril Chemparathy  * scenario in practice.
862b62997cSCyril Chemparathy  */
872b62997cSCyril Chemparathy #define MDIO_TIMEOUT            100 /* msecs */
882b62997cSCyril Chemparathy #define CPDMA_TIMEOUT		100 /* msecs */
892b62997cSCyril Chemparathy 
902b62997cSCyril Chemparathy struct cpsw_mdio_regs {
912b62997cSCyril Chemparathy 	u32	version;
922b62997cSCyril Chemparathy 	u32	control;
932b62997cSCyril Chemparathy #define CONTROL_IDLE		BIT(31)
942b62997cSCyril Chemparathy #define CONTROL_ENABLE		BIT(30)
952b62997cSCyril Chemparathy 
962b62997cSCyril Chemparathy 	u32	alive;
972b62997cSCyril Chemparathy 	u32	link;
982b62997cSCyril Chemparathy 	u32	linkintraw;
992b62997cSCyril Chemparathy 	u32	linkintmasked;
1002b62997cSCyril Chemparathy 	u32	__reserved_0[2];
1012b62997cSCyril Chemparathy 	u32	userintraw;
1022b62997cSCyril Chemparathy 	u32	userintmasked;
1032b62997cSCyril Chemparathy 	u32	userintmaskset;
1042b62997cSCyril Chemparathy 	u32	userintmaskclr;
1052b62997cSCyril Chemparathy 	u32	__reserved_1[20];
1062b62997cSCyril Chemparathy 
1072b62997cSCyril Chemparathy 	struct {
1082b62997cSCyril Chemparathy 		u32		access;
1092b62997cSCyril Chemparathy 		u32		physel;
1102b62997cSCyril Chemparathy #define USERACCESS_GO		BIT(31)
1112b62997cSCyril Chemparathy #define USERACCESS_WRITE	BIT(30)
1122b62997cSCyril Chemparathy #define USERACCESS_ACK		BIT(29)
1132b62997cSCyril Chemparathy #define USERACCESS_READ		(0)
1142b62997cSCyril Chemparathy #define USERACCESS_DATA		(0xffff)
1152b62997cSCyril Chemparathy 	} user[0];
1162b62997cSCyril Chemparathy };
1172b62997cSCyril Chemparathy 
1182b62997cSCyril Chemparathy struct cpsw_regs {
1192b62997cSCyril Chemparathy 	u32	id_ver;
1202b62997cSCyril Chemparathy 	u32	control;
1212b62997cSCyril Chemparathy 	u32	soft_reset;
1222b62997cSCyril Chemparathy 	u32	stat_port_en;
1232b62997cSCyril Chemparathy 	u32	ptype;
1242b62997cSCyril Chemparathy };
1252b62997cSCyril Chemparathy 
1262b62997cSCyril Chemparathy struct cpsw_slave_regs {
1272b62997cSCyril Chemparathy 	u32	max_blks;
1282b62997cSCyril Chemparathy 	u32	blk_cnt;
1292b62997cSCyril Chemparathy 	u32	flow_thresh;
1302b62997cSCyril Chemparathy 	u32	port_vlan;
1312b62997cSCyril Chemparathy 	u32	tx_pri_map;
132f6f86a64SMatt Porter #ifdef CONFIG_AM33XX
1332b62997cSCyril Chemparathy 	u32	gap_thresh;
134f6f86a64SMatt Porter #elif defined(CONFIG_TI814X)
135f6f86a64SMatt Porter 	u32	ts_ctl;
136f6f86a64SMatt Porter 	u32	ts_seq_ltype;
137f6f86a64SMatt Porter 	u32	ts_vlan;
138f6f86a64SMatt Porter #endif
1392b62997cSCyril Chemparathy 	u32	sa_lo;
1402b62997cSCyril Chemparathy 	u32	sa_hi;
1412b62997cSCyril Chemparathy };
1422b62997cSCyril Chemparathy 
1432b62997cSCyril Chemparathy struct cpsw_host_regs {
1442b62997cSCyril Chemparathy 	u32	max_blks;
1452b62997cSCyril Chemparathy 	u32	blk_cnt;
1462b62997cSCyril Chemparathy 	u32	flow_thresh;
1472b62997cSCyril Chemparathy 	u32	port_vlan;
1482b62997cSCyril Chemparathy 	u32	tx_pri_map;
1492b62997cSCyril Chemparathy 	u32	cpdma_tx_pri_map;
1502b62997cSCyril Chemparathy 	u32	cpdma_rx_chan_map;
1512b62997cSCyril Chemparathy };
1522b62997cSCyril Chemparathy 
1532b62997cSCyril Chemparathy struct cpsw_sliver_regs {
1542b62997cSCyril Chemparathy 	u32	id_ver;
1552b62997cSCyril Chemparathy 	u32	mac_control;
1562b62997cSCyril Chemparathy 	u32	mac_status;
1572b62997cSCyril Chemparathy 	u32	soft_reset;
1582b62997cSCyril Chemparathy 	u32	rx_maxlen;
1592b62997cSCyril Chemparathy 	u32	__reserved_0;
1602b62997cSCyril Chemparathy 	u32	rx_pause;
1612b62997cSCyril Chemparathy 	u32	tx_pause;
1622b62997cSCyril Chemparathy 	u32	__reserved_1;
1632b62997cSCyril Chemparathy 	u32	rx_pri_map;
1642b62997cSCyril Chemparathy };
1652b62997cSCyril Chemparathy 
1662b62997cSCyril Chemparathy #define ALE_ENTRY_BITS		68
1672b62997cSCyril Chemparathy #define ALE_ENTRY_WORDS		DIV_ROUND_UP(ALE_ENTRY_BITS, 32)
1682b62997cSCyril Chemparathy 
1692b62997cSCyril Chemparathy /* ALE Registers */
1702b62997cSCyril Chemparathy #define ALE_CONTROL		0x08
1712b62997cSCyril Chemparathy #define ALE_UNKNOWNVLAN		0x18
1722b62997cSCyril Chemparathy #define ALE_TABLE_CONTROL	0x20
1732b62997cSCyril Chemparathy #define ALE_TABLE		0x34
1742b62997cSCyril Chemparathy #define ALE_PORTCTL		0x40
1752b62997cSCyril Chemparathy 
1762b62997cSCyril Chemparathy #define ALE_TABLE_WRITE		BIT(31)
1772b62997cSCyril Chemparathy 
1782b62997cSCyril Chemparathy #define ALE_TYPE_FREE			0
1792b62997cSCyril Chemparathy #define ALE_TYPE_ADDR			1
1802b62997cSCyril Chemparathy #define ALE_TYPE_VLAN			2
1812b62997cSCyril Chemparathy #define ALE_TYPE_VLAN_ADDR		3
1822b62997cSCyril Chemparathy 
1832b62997cSCyril Chemparathy #define ALE_UCAST_PERSISTANT		0
1842b62997cSCyril Chemparathy #define ALE_UCAST_UNTOUCHED		1
1852b62997cSCyril Chemparathy #define ALE_UCAST_OUI			2
1862b62997cSCyril Chemparathy #define ALE_UCAST_TOUCHED		3
1872b62997cSCyril Chemparathy 
1882b62997cSCyril Chemparathy #define ALE_MCAST_FWD			0
1892b62997cSCyril Chemparathy #define ALE_MCAST_BLOCK_LEARN_FWD	1
1902b62997cSCyril Chemparathy #define ALE_MCAST_FWD_LEARN		2
1912b62997cSCyril Chemparathy #define ALE_MCAST_FWD_2			3
1922b62997cSCyril Chemparathy 
1932b62997cSCyril Chemparathy enum cpsw_ale_port_state {
1942b62997cSCyril Chemparathy 	ALE_PORT_STATE_DISABLE	= 0x00,
1952b62997cSCyril Chemparathy 	ALE_PORT_STATE_BLOCK	= 0x01,
1962b62997cSCyril Chemparathy 	ALE_PORT_STATE_LEARN	= 0x02,
1972b62997cSCyril Chemparathy 	ALE_PORT_STATE_FORWARD	= 0x03,
1982b62997cSCyril Chemparathy };
1992b62997cSCyril Chemparathy 
2002b62997cSCyril Chemparathy /* ALE unicast entry flags - passed into cpsw_ale_add_ucast() */
2012b62997cSCyril Chemparathy #define ALE_SECURE	1
2022b62997cSCyril Chemparathy #define ALE_BLOCKED	2
2032b62997cSCyril Chemparathy 
2042b62997cSCyril Chemparathy struct cpsw_slave {
2052b62997cSCyril Chemparathy 	struct cpsw_slave_regs		*regs;
2062b62997cSCyril Chemparathy 	struct cpsw_sliver_regs		*sliver;
2072b62997cSCyril Chemparathy 	int				slave_num;
2082b62997cSCyril Chemparathy 	u32				mac_control;
2092b62997cSCyril Chemparathy 	struct cpsw_slave_data		*data;
2102b62997cSCyril Chemparathy };
2112b62997cSCyril Chemparathy 
2122b62997cSCyril Chemparathy struct cpdma_desc {
2132b62997cSCyril Chemparathy 	/* hardware fields */
2142b62997cSCyril Chemparathy 	u32			hw_next;
2152b62997cSCyril Chemparathy 	u32			hw_buffer;
2162b62997cSCyril Chemparathy 	u32			hw_len;
2172b62997cSCyril Chemparathy 	u32			hw_mode;
2182b62997cSCyril Chemparathy 	/* software fields */
2192b62997cSCyril Chemparathy 	u32			sw_buffer;
2202b62997cSCyril Chemparathy 	u32			sw_len;
2212b62997cSCyril Chemparathy };
2222b62997cSCyril Chemparathy 
2232b62997cSCyril Chemparathy struct cpdma_chan {
2242b62997cSCyril Chemparathy 	struct cpdma_desc	*head, *tail;
2252b62997cSCyril Chemparathy 	void			*hdp, *cp, *rxfree;
2262b62997cSCyril Chemparathy };
2272b62997cSCyril Chemparathy 
2282b62997cSCyril Chemparathy #define desc_write(desc, fld, val)	__raw_writel((u32)(val), &(desc)->fld)
2292b62997cSCyril Chemparathy #define desc_read(desc, fld)		__raw_readl(&(desc)->fld)
2302b62997cSCyril Chemparathy #define desc_read_ptr(desc, fld)	((void *)__raw_readl(&(desc)->fld))
2312b62997cSCyril Chemparathy 
2322b62997cSCyril Chemparathy #define chan_write(chan, fld, val)	__raw_writel((u32)(val), (chan)->fld)
2332b62997cSCyril Chemparathy #define chan_read(chan, fld)		__raw_readl((chan)->fld)
2342b62997cSCyril Chemparathy #define chan_read_ptr(chan, fld)	((void *)__raw_readl((chan)->fld))
2352b62997cSCyril Chemparathy 
2367a022753SMugunthan V N #define for_active_slave(slave, priv) \
2377a022753SMugunthan V N 	slave = (priv)->slaves + (priv)->data.active_slave; if (slave)
2382b62997cSCyril Chemparathy #define for_each_slave(slave, priv) \
2392b62997cSCyril Chemparathy 	for (slave = (priv)->slaves; slave != (priv)->slaves + \
2402b62997cSCyril Chemparathy 				(priv)->data.slaves; slave++)
2412b62997cSCyril Chemparathy 
2422b62997cSCyril Chemparathy struct cpsw_priv {
2434cc77895SMugunthan V N #ifdef CONFIG_DM_ETH
2444cc77895SMugunthan V N 	struct udevice			*dev;
2454cc77895SMugunthan V N #else
2462b62997cSCyril Chemparathy 	struct eth_device		*dev;
2474cc77895SMugunthan V N #endif
2482b62997cSCyril Chemparathy 	struct cpsw_platform_data	data;
2492b62997cSCyril Chemparathy 	int				host_port;
2502b62997cSCyril Chemparathy 
2512b62997cSCyril Chemparathy 	struct cpsw_regs		*regs;
2522b62997cSCyril Chemparathy 	void				*dma_regs;
2532b62997cSCyril Chemparathy 	struct cpsw_host_regs		*host_port_regs;
2542b62997cSCyril Chemparathy 	void				*ale_regs;
2552b62997cSCyril Chemparathy 
2562b62997cSCyril Chemparathy 	struct cpdma_desc		*descs;
2572b62997cSCyril Chemparathy 	struct cpdma_desc		*desc_free;
2582b62997cSCyril Chemparathy 	struct cpdma_chan		rx_chan, tx_chan;
2592b62997cSCyril Chemparathy 
2602b62997cSCyril Chemparathy 	struct cpsw_slave		*slaves;
2612b62997cSCyril Chemparathy 	struct phy_device		*phydev;
2622b62997cSCyril Chemparathy 	struct mii_dev			*bus;
26348ec5291SMugunthan V N 
26448ec5291SMugunthan V N 	u32				phy_mask;
2652b62997cSCyril Chemparathy };
2662b62997cSCyril Chemparathy 
2672b62997cSCyril Chemparathy static inline int cpsw_ale_get_field(u32 *ale_entry, u32 start, u32 bits)
2682b62997cSCyril Chemparathy {
2692b62997cSCyril Chemparathy 	int idx;
2702b62997cSCyril Chemparathy 
2712b62997cSCyril Chemparathy 	idx    = start / 32;
2722b62997cSCyril Chemparathy 	start -= idx * 32;
2732b62997cSCyril Chemparathy 	idx    = 2 - idx; /* flip */
2742b62997cSCyril Chemparathy 	return (ale_entry[idx] >> start) & BITMASK(bits);
2752b62997cSCyril Chemparathy }
2762b62997cSCyril Chemparathy 
2772b62997cSCyril Chemparathy static inline void cpsw_ale_set_field(u32 *ale_entry, u32 start, u32 bits,
2782b62997cSCyril Chemparathy 				      u32 value)
2792b62997cSCyril Chemparathy {
2802b62997cSCyril Chemparathy 	int idx;
2812b62997cSCyril Chemparathy 
2822b62997cSCyril Chemparathy 	value &= BITMASK(bits);
2832b62997cSCyril Chemparathy 	idx    = start / 32;
2842b62997cSCyril Chemparathy 	start -= idx * 32;
2852b62997cSCyril Chemparathy 	idx    = 2 - idx; /* flip */
2862b62997cSCyril Chemparathy 	ale_entry[idx] &= ~(BITMASK(bits) << start);
2872b62997cSCyril Chemparathy 	ale_entry[idx] |=  (value << start);
2882b62997cSCyril Chemparathy }
2892b62997cSCyril Chemparathy 
2902b62997cSCyril Chemparathy #define DEFINE_ALE_FIELD(name, start, bits)				\
2912b62997cSCyril Chemparathy static inline int cpsw_ale_get_##name(u32 *ale_entry)			\
2922b62997cSCyril Chemparathy {									\
2932b62997cSCyril Chemparathy 	return cpsw_ale_get_field(ale_entry, start, bits);		\
2942b62997cSCyril Chemparathy }									\
2952b62997cSCyril Chemparathy static inline void cpsw_ale_set_##name(u32 *ale_entry, u32 value)	\
2962b62997cSCyril Chemparathy {									\
2972b62997cSCyril Chemparathy 	cpsw_ale_set_field(ale_entry, start, bits, value);		\
2982b62997cSCyril Chemparathy }
2992b62997cSCyril Chemparathy 
3002b62997cSCyril Chemparathy DEFINE_ALE_FIELD(entry_type,		60,	2)
3012b62997cSCyril Chemparathy DEFINE_ALE_FIELD(mcast_state,		62,	2)
3022b62997cSCyril Chemparathy DEFINE_ALE_FIELD(port_mask,		66,	3)
3032b62997cSCyril Chemparathy DEFINE_ALE_FIELD(ucast_type,		62,	2)
3042b62997cSCyril Chemparathy DEFINE_ALE_FIELD(port_num,		66,	2)
3052b62997cSCyril Chemparathy DEFINE_ALE_FIELD(blocked,		65,	1)
3062b62997cSCyril Chemparathy DEFINE_ALE_FIELD(secure,		64,	1)
3072b62997cSCyril Chemparathy DEFINE_ALE_FIELD(mcast,			40,	1)
3082b62997cSCyril Chemparathy 
3092b62997cSCyril Chemparathy /* The MAC address field in the ALE entry cannot be macroized as above */
3102b62997cSCyril Chemparathy static inline void cpsw_ale_get_addr(u32 *ale_entry, u8 *addr)
3112b62997cSCyril Chemparathy {
3122b62997cSCyril Chemparathy 	int i;
3132b62997cSCyril Chemparathy 
3142b62997cSCyril Chemparathy 	for (i = 0; i < 6; i++)
3152b62997cSCyril Chemparathy 		addr[i] = cpsw_ale_get_field(ale_entry, 40 - 8*i, 8);
3162b62997cSCyril Chemparathy }
3172b62997cSCyril Chemparathy 
3180adb5b76SJoe Hershberger static inline void cpsw_ale_set_addr(u32 *ale_entry, const u8 *addr)
3192b62997cSCyril Chemparathy {
3202b62997cSCyril Chemparathy 	int i;
3212b62997cSCyril Chemparathy 
3222b62997cSCyril Chemparathy 	for (i = 0; i < 6; i++)
3232b62997cSCyril Chemparathy 		cpsw_ale_set_field(ale_entry, 40 - 8*i, 8, addr[i]);
3242b62997cSCyril Chemparathy }
3252b62997cSCyril Chemparathy 
3262b62997cSCyril Chemparathy static int cpsw_ale_read(struct cpsw_priv *priv, int idx, u32 *ale_entry)
3272b62997cSCyril Chemparathy {
3282b62997cSCyril Chemparathy 	int i;
3292b62997cSCyril Chemparathy 
3302b62997cSCyril Chemparathy 	__raw_writel(idx, priv->ale_regs + ALE_TABLE_CONTROL);
3312b62997cSCyril Chemparathy 
3322b62997cSCyril Chemparathy 	for (i = 0; i < ALE_ENTRY_WORDS; i++)
3332b62997cSCyril Chemparathy 		ale_entry[i] = __raw_readl(priv->ale_regs + ALE_TABLE + 4 * i);
3342b62997cSCyril Chemparathy 
3352b62997cSCyril Chemparathy 	return idx;
3362b62997cSCyril Chemparathy }
3372b62997cSCyril Chemparathy 
3382b62997cSCyril Chemparathy static int cpsw_ale_write(struct cpsw_priv *priv, int idx, u32 *ale_entry)
3392b62997cSCyril Chemparathy {
3402b62997cSCyril Chemparathy 	int i;
3412b62997cSCyril Chemparathy 
3422b62997cSCyril Chemparathy 	for (i = 0; i < ALE_ENTRY_WORDS; i++)
3432b62997cSCyril Chemparathy 		__raw_writel(ale_entry[i], priv->ale_regs + ALE_TABLE + 4 * i);
3442b62997cSCyril Chemparathy 
3452b62997cSCyril Chemparathy 	__raw_writel(idx | ALE_TABLE_WRITE, priv->ale_regs + ALE_TABLE_CONTROL);
3462b62997cSCyril Chemparathy 
3472b62997cSCyril Chemparathy 	return idx;
3482b62997cSCyril Chemparathy }
3492b62997cSCyril Chemparathy 
3500adb5b76SJoe Hershberger static int cpsw_ale_match_addr(struct cpsw_priv *priv, const u8 *addr)
3512b62997cSCyril Chemparathy {
3522b62997cSCyril Chemparathy 	u32 ale_entry[ALE_ENTRY_WORDS];
3532b62997cSCyril Chemparathy 	int type, idx;
3542b62997cSCyril Chemparathy 
3552b62997cSCyril Chemparathy 	for (idx = 0; idx < priv->data.ale_entries; idx++) {
3562b62997cSCyril Chemparathy 		u8 entry_addr[6];
3572b62997cSCyril Chemparathy 
3582b62997cSCyril Chemparathy 		cpsw_ale_read(priv, idx, ale_entry);
3592b62997cSCyril Chemparathy 		type = cpsw_ale_get_entry_type(ale_entry);
3602b62997cSCyril Chemparathy 		if (type != ALE_TYPE_ADDR && type != ALE_TYPE_VLAN_ADDR)
3612b62997cSCyril Chemparathy 			continue;
3622b62997cSCyril Chemparathy 		cpsw_ale_get_addr(ale_entry, entry_addr);
3632b62997cSCyril Chemparathy 		if (memcmp(entry_addr, addr, 6) == 0)
3642b62997cSCyril Chemparathy 			return idx;
3652b62997cSCyril Chemparathy 	}
3662b62997cSCyril Chemparathy 	return -ENOENT;
3672b62997cSCyril Chemparathy }
3682b62997cSCyril Chemparathy 
3692b62997cSCyril Chemparathy static int cpsw_ale_match_free(struct cpsw_priv *priv)
3702b62997cSCyril Chemparathy {
3712b62997cSCyril Chemparathy 	u32 ale_entry[ALE_ENTRY_WORDS];
3722b62997cSCyril Chemparathy 	int type, idx;
3732b62997cSCyril Chemparathy 
3742b62997cSCyril Chemparathy 	for (idx = 0; idx < priv->data.ale_entries; idx++) {
3752b62997cSCyril Chemparathy 		cpsw_ale_read(priv, idx, ale_entry);
3762b62997cSCyril Chemparathy 		type = cpsw_ale_get_entry_type(ale_entry);
3772b62997cSCyril Chemparathy 		if (type == ALE_TYPE_FREE)
3782b62997cSCyril Chemparathy 			return idx;
3792b62997cSCyril Chemparathy 	}
3802b62997cSCyril Chemparathy 	return -ENOENT;
3812b62997cSCyril Chemparathy }
3822b62997cSCyril Chemparathy 
3832b62997cSCyril Chemparathy static int cpsw_ale_find_ageable(struct cpsw_priv *priv)
3842b62997cSCyril Chemparathy {
3852b62997cSCyril Chemparathy 	u32 ale_entry[ALE_ENTRY_WORDS];
3862b62997cSCyril Chemparathy 	int type, idx;
3872b62997cSCyril Chemparathy 
3882b62997cSCyril Chemparathy 	for (idx = 0; idx < priv->data.ale_entries; idx++) {
3892b62997cSCyril Chemparathy 		cpsw_ale_read(priv, idx, ale_entry);
3902b62997cSCyril Chemparathy 		type = cpsw_ale_get_entry_type(ale_entry);
3912b62997cSCyril Chemparathy 		if (type != ALE_TYPE_ADDR && type != ALE_TYPE_VLAN_ADDR)
3922b62997cSCyril Chemparathy 			continue;
3932b62997cSCyril Chemparathy 		if (cpsw_ale_get_mcast(ale_entry))
3942b62997cSCyril Chemparathy 			continue;
3952b62997cSCyril Chemparathy 		type = cpsw_ale_get_ucast_type(ale_entry);
3962b62997cSCyril Chemparathy 		if (type != ALE_UCAST_PERSISTANT &&
3972b62997cSCyril Chemparathy 		    type != ALE_UCAST_OUI)
3982b62997cSCyril Chemparathy 			return idx;
3992b62997cSCyril Chemparathy 	}
4002b62997cSCyril Chemparathy 	return -ENOENT;
4012b62997cSCyril Chemparathy }
4022b62997cSCyril Chemparathy 
4030adb5b76SJoe Hershberger static int cpsw_ale_add_ucast(struct cpsw_priv *priv, const u8 *addr,
4042b62997cSCyril Chemparathy 			      int port, int flags)
4052b62997cSCyril Chemparathy {
4062b62997cSCyril Chemparathy 	u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
4072b62997cSCyril Chemparathy 	int idx;
4082b62997cSCyril Chemparathy 
4092b62997cSCyril Chemparathy 	cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_ADDR);
4102b62997cSCyril Chemparathy 	cpsw_ale_set_addr(ale_entry, addr);
4112b62997cSCyril Chemparathy 	cpsw_ale_set_ucast_type(ale_entry, ALE_UCAST_PERSISTANT);
4122b62997cSCyril Chemparathy 	cpsw_ale_set_secure(ale_entry, (flags & ALE_SECURE) ? 1 : 0);
4132b62997cSCyril Chemparathy 	cpsw_ale_set_blocked(ale_entry, (flags & ALE_BLOCKED) ? 1 : 0);
4142b62997cSCyril Chemparathy 	cpsw_ale_set_port_num(ale_entry, port);
4152b62997cSCyril Chemparathy 
4162b62997cSCyril Chemparathy 	idx = cpsw_ale_match_addr(priv, addr);
4172b62997cSCyril Chemparathy 	if (idx < 0)
4182b62997cSCyril Chemparathy 		idx = cpsw_ale_match_free(priv);
4192b62997cSCyril Chemparathy 	if (idx < 0)
4202b62997cSCyril Chemparathy 		idx = cpsw_ale_find_ageable(priv);
4212b62997cSCyril Chemparathy 	if (idx < 0)
4222b62997cSCyril Chemparathy 		return -ENOMEM;
4232b62997cSCyril Chemparathy 
4242b62997cSCyril Chemparathy 	cpsw_ale_write(priv, idx, ale_entry);
4252b62997cSCyril Chemparathy 	return 0;
4262b62997cSCyril Chemparathy }
4272b62997cSCyril Chemparathy 
4280adb5b76SJoe Hershberger static int cpsw_ale_add_mcast(struct cpsw_priv *priv, const u8 *addr,
4290adb5b76SJoe Hershberger 			      int port_mask)
4302b62997cSCyril Chemparathy {
4312b62997cSCyril Chemparathy 	u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
4322b62997cSCyril Chemparathy 	int idx, mask;
4332b62997cSCyril Chemparathy 
4342b62997cSCyril Chemparathy 	idx = cpsw_ale_match_addr(priv, addr);
4352b62997cSCyril Chemparathy 	if (idx >= 0)
4362b62997cSCyril Chemparathy 		cpsw_ale_read(priv, idx, ale_entry);
4372b62997cSCyril Chemparathy 
4382b62997cSCyril Chemparathy 	cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_ADDR);
4392b62997cSCyril Chemparathy 	cpsw_ale_set_addr(ale_entry, addr);
4402b62997cSCyril Chemparathy 	cpsw_ale_set_mcast_state(ale_entry, ALE_MCAST_FWD_2);
4412b62997cSCyril Chemparathy 
4422b62997cSCyril Chemparathy 	mask = cpsw_ale_get_port_mask(ale_entry);
4432b62997cSCyril Chemparathy 	port_mask |= mask;
4442b62997cSCyril Chemparathy 	cpsw_ale_set_port_mask(ale_entry, port_mask);
4452b62997cSCyril Chemparathy 
4462b62997cSCyril Chemparathy 	if (idx < 0)
4472b62997cSCyril Chemparathy 		idx = cpsw_ale_match_free(priv);
4482b62997cSCyril Chemparathy 	if (idx < 0)
4492b62997cSCyril Chemparathy 		idx = cpsw_ale_find_ageable(priv);
4502b62997cSCyril Chemparathy 	if (idx < 0)
4512b62997cSCyril Chemparathy 		return -ENOMEM;
4522b62997cSCyril Chemparathy 
4532b62997cSCyril Chemparathy 	cpsw_ale_write(priv, idx, ale_entry);
4542b62997cSCyril Chemparathy 	return 0;
4552b62997cSCyril Chemparathy }
4562b62997cSCyril Chemparathy 
4572b62997cSCyril Chemparathy static inline void cpsw_ale_control(struct cpsw_priv *priv, int bit, int val)
4582b62997cSCyril Chemparathy {
4592b62997cSCyril Chemparathy 	u32 tmp, mask = BIT(bit);
4602b62997cSCyril Chemparathy 
4612b62997cSCyril Chemparathy 	tmp  = __raw_readl(priv->ale_regs + ALE_CONTROL);
4622b62997cSCyril Chemparathy 	tmp &= ~mask;
4632b62997cSCyril Chemparathy 	tmp |= val ? mask : 0;
4642b62997cSCyril Chemparathy 	__raw_writel(tmp, priv->ale_regs + ALE_CONTROL);
4652b62997cSCyril Chemparathy }
4662b62997cSCyril Chemparathy 
4672b62997cSCyril Chemparathy #define cpsw_ale_enable(priv, val)	cpsw_ale_control(priv, 31, val)
4682b62997cSCyril Chemparathy #define cpsw_ale_clear(priv, val)	cpsw_ale_control(priv, 30, val)
4692b62997cSCyril Chemparathy #define cpsw_ale_vlan_aware(priv, val)	cpsw_ale_control(priv,  2, val)
4702b62997cSCyril Chemparathy 
4712b62997cSCyril Chemparathy static inline void cpsw_ale_port_state(struct cpsw_priv *priv, int port,
4722b62997cSCyril Chemparathy 				       int val)
4732b62997cSCyril Chemparathy {
4742b62997cSCyril Chemparathy 	int offset = ALE_PORTCTL + 4 * port;
4752b62997cSCyril Chemparathy 	u32 tmp, mask = 0x3;
4762b62997cSCyril Chemparathy 
4772b62997cSCyril Chemparathy 	tmp  = __raw_readl(priv->ale_regs + offset);
4782b62997cSCyril Chemparathy 	tmp &= ~mask;
4792b62997cSCyril Chemparathy 	tmp |= val & mask;
4802b62997cSCyril Chemparathy 	__raw_writel(tmp, priv->ale_regs + offset);
4812b62997cSCyril Chemparathy }
4822b62997cSCyril Chemparathy 
4832b62997cSCyril Chemparathy static struct cpsw_mdio_regs *mdio_regs;
4842b62997cSCyril Chemparathy 
4852b62997cSCyril Chemparathy /* wait until hardware is ready for another user access */
4862b62997cSCyril Chemparathy static inline u32 wait_for_user_access(void)
4872b62997cSCyril Chemparathy {
4882b62997cSCyril Chemparathy 	u32 reg = 0;
4892b62997cSCyril Chemparathy 	int timeout = MDIO_TIMEOUT;
4902b62997cSCyril Chemparathy 
4912b62997cSCyril Chemparathy 	while (timeout-- &&
4922b62997cSCyril Chemparathy 	((reg = __raw_readl(&mdio_regs->user[0].access)) & USERACCESS_GO))
4932b62997cSCyril Chemparathy 		udelay(10);
4942b62997cSCyril Chemparathy 
4952b62997cSCyril Chemparathy 	if (timeout == -1) {
4962b62997cSCyril Chemparathy 		printf("wait_for_user_access Timeout\n");
4972b62997cSCyril Chemparathy 		return -ETIMEDOUT;
4982b62997cSCyril Chemparathy 	}
4992b62997cSCyril Chemparathy 	return reg;
5002b62997cSCyril Chemparathy }
5012b62997cSCyril Chemparathy 
5022b62997cSCyril Chemparathy /* wait until hardware state machine is idle */
5032b62997cSCyril Chemparathy static inline void wait_for_idle(void)
5042b62997cSCyril Chemparathy {
5052b62997cSCyril Chemparathy 	int timeout = MDIO_TIMEOUT;
5062b62997cSCyril Chemparathy 
5072b62997cSCyril Chemparathy 	while (timeout-- &&
5082b62997cSCyril Chemparathy 		((__raw_readl(&mdio_regs->control) & CONTROL_IDLE) == 0))
5092b62997cSCyril Chemparathy 		udelay(10);
5102b62997cSCyril Chemparathy 
5112b62997cSCyril Chemparathy 	if (timeout == -1)
5122b62997cSCyril Chemparathy 		printf("wait_for_idle Timeout\n");
5132b62997cSCyril Chemparathy }
5142b62997cSCyril Chemparathy 
5152b62997cSCyril Chemparathy static int cpsw_mdio_read(struct mii_dev *bus, int phy_id,
5162b62997cSCyril Chemparathy 				int dev_addr, int phy_reg)
5172b62997cSCyril Chemparathy {
518f6d1f6e4SHeiko Schocher 	int data;
5192b62997cSCyril Chemparathy 	u32 reg;
5202b62997cSCyril Chemparathy 
5212b62997cSCyril Chemparathy 	if (phy_reg & ~PHY_REG_MASK || phy_id & ~PHY_ID_MASK)
5222b62997cSCyril Chemparathy 		return -EINVAL;
5232b62997cSCyril Chemparathy 
5242b62997cSCyril Chemparathy 	wait_for_user_access();
5252b62997cSCyril Chemparathy 	reg = (USERACCESS_GO | USERACCESS_READ | (phy_reg << 21) |
5262b62997cSCyril Chemparathy 	       (phy_id << 16));
5272b62997cSCyril Chemparathy 	__raw_writel(reg, &mdio_regs->user[0].access);
5282b62997cSCyril Chemparathy 	reg = wait_for_user_access();
5292b62997cSCyril Chemparathy 
5302b62997cSCyril Chemparathy 	data = (reg & USERACCESS_ACK) ? (reg & USERACCESS_DATA) : -1;
5312b62997cSCyril Chemparathy 	return data;
5322b62997cSCyril Chemparathy }
5332b62997cSCyril Chemparathy 
5342b62997cSCyril Chemparathy static int cpsw_mdio_write(struct mii_dev *bus, int phy_id, int dev_addr,
5352b62997cSCyril Chemparathy 				int phy_reg, u16 data)
5362b62997cSCyril Chemparathy {
5372b62997cSCyril Chemparathy 	u32 reg;
5382b62997cSCyril Chemparathy 
5392b62997cSCyril Chemparathy 	if (phy_reg & ~PHY_REG_MASK || phy_id & ~PHY_ID_MASK)
5402b62997cSCyril Chemparathy 		return -EINVAL;
5412b62997cSCyril Chemparathy 
5422b62997cSCyril Chemparathy 	wait_for_user_access();
5432b62997cSCyril Chemparathy 	reg = (USERACCESS_GO | USERACCESS_WRITE | (phy_reg << 21) |
5442b62997cSCyril Chemparathy 		   (phy_id << 16) | (data & USERACCESS_DATA));
5452b62997cSCyril Chemparathy 	__raw_writel(reg, &mdio_regs->user[0].access);
5462b62997cSCyril Chemparathy 	wait_for_user_access();
5472b62997cSCyril Chemparathy 
5482b62997cSCyril Chemparathy 	return 0;
5492b62997cSCyril Chemparathy }
5502b62997cSCyril Chemparathy 
5514cc77895SMugunthan V N static void cpsw_mdio_init(const char *name, u32 mdio_base, u32 div)
5522b62997cSCyril Chemparathy {
5532b62997cSCyril Chemparathy 	struct mii_dev *bus = mdio_alloc();
5542b62997cSCyril Chemparathy 
5552b62997cSCyril Chemparathy 	mdio_regs = (struct cpsw_mdio_regs *)mdio_base;
5562b62997cSCyril Chemparathy 
5572b62997cSCyril Chemparathy 	/* set enable and clock divider */
5582b62997cSCyril Chemparathy 	__raw_writel(div | CONTROL_ENABLE, &mdio_regs->control);
5592b62997cSCyril Chemparathy 
5602b62997cSCyril Chemparathy 	/*
5612b62997cSCyril Chemparathy 	 * wait for scan logic to settle:
5622b62997cSCyril Chemparathy 	 * the scan time consists of (a) a large fixed component, and (b) a
5632b62997cSCyril Chemparathy 	 * small component that varies with the mii bus frequency.  These
5642b62997cSCyril Chemparathy 	 * were estimated using measurements at 1.1 and 2.2 MHz on tnetv107x
5652b62997cSCyril Chemparathy 	 * silicon.  Since the effect of (b) was found to be largely
5662b62997cSCyril Chemparathy 	 * negligible, we keep things simple here.
5672b62997cSCyril Chemparathy 	 */
5682b62997cSCyril Chemparathy 	udelay(1000);
5692b62997cSCyril Chemparathy 
5702b62997cSCyril Chemparathy 	bus->read = cpsw_mdio_read;
5712b62997cSCyril Chemparathy 	bus->write = cpsw_mdio_write;
572192bc694SBen Whitten 	strcpy(bus->name, name);
5732b62997cSCyril Chemparathy 
5742b62997cSCyril Chemparathy 	mdio_register(bus);
5752b62997cSCyril Chemparathy }
5762b62997cSCyril Chemparathy 
5772b62997cSCyril Chemparathy /* Set a self-clearing bit in a register, and wait for it to clear */
5782b62997cSCyril Chemparathy static inline void setbit_and_wait_for_clear32(void *addr)
5792b62997cSCyril Chemparathy {
5802b62997cSCyril Chemparathy 	__raw_writel(CLEAR_BIT, addr);
5812b62997cSCyril Chemparathy 	while (__raw_readl(addr) & CLEAR_BIT)
5822b62997cSCyril Chemparathy 		;
5832b62997cSCyril Chemparathy }
5842b62997cSCyril Chemparathy 
5852b62997cSCyril Chemparathy #define mac_hi(mac)	(((mac)[0] << 0) | ((mac)[1] << 8) |	\
5862b62997cSCyril Chemparathy 			 ((mac)[2] << 16) | ((mac)[3] << 24))
5872b62997cSCyril Chemparathy #define mac_lo(mac)	(((mac)[4] << 0) | ((mac)[5] << 8))
5882b62997cSCyril Chemparathy 
5892b62997cSCyril Chemparathy static void cpsw_set_slave_mac(struct cpsw_slave *slave,
5902b62997cSCyril Chemparathy 			       struct cpsw_priv *priv)
5912b62997cSCyril Chemparathy {
5924cc77895SMugunthan V N #ifdef CONFIG_DM_ETH
5934cc77895SMugunthan V N 	struct eth_pdata *pdata = dev_get_platdata(priv->dev);
5944cc77895SMugunthan V N 
5954cc77895SMugunthan V N 	writel(mac_hi(pdata->enetaddr), &slave->regs->sa_hi);
5964cc77895SMugunthan V N 	writel(mac_lo(pdata->enetaddr), &slave->regs->sa_lo);
5974cc77895SMugunthan V N #else
5982b62997cSCyril Chemparathy 	__raw_writel(mac_hi(priv->dev->enetaddr), &slave->regs->sa_hi);
5992b62997cSCyril Chemparathy 	__raw_writel(mac_lo(priv->dev->enetaddr), &slave->regs->sa_lo);
6004cc77895SMugunthan V N #endif
6012b62997cSCyril Chemparathy }
6022b62997cSCyril Chemparathy 
6032b62997cSCyril Chemparathy static void cpsw_slave_update_link(struct cpsw_slave *slave,
6042b62997cSCyril Chemparathy 				   struct cpsw_priv *priv, int *link)
6052b62997cSCyril Chemparathy {
60693ff2552SHeiko Schocher 	struct phy_device *phy;
6072b62997cSCyril Chemparathy 	u32 mac_control = 0;
6082b62997cSCyril Chemparathy 
60993ff2552SHeiko Schocher 	phy = priv->phydev;
61093ff2552SHeiko Schocher 
61193ff2552SHeiko Schocher 	if (!phy)
61293ff2552SHeiko Schocher 		return;
61393ff2552SHeiko Schocher 
6142b62997cSCyril Chemparathy 	phy_startup(phy);
6152b62997cSCyril Chemparathy 	*link = phy->link;
6162b62997cSCyril Chemparathy 
6172b62997cSCyril Chemparathy 	if (*link) { /* link up */
6182b62997cSCyril Chemparathy 		mac_control = priv->data.mac_control;
6192b62997cSCyril Chemparathy 		if (phy->speed == 1000)
6202b62997cSCyril Chemparathy 			mac_control |= GIGABITEN;
6212b62997cSCyril Chemparathy 		if (phy->duplex == DUPLEX_FULL)
6222b62997cSCyril Chemparathy 			mac_control |= FULLDUPLEXEN;
6232b62997cSCyril Chemparathy 		if (phy->speed == 100)
6242b62997cSCyril Chemparathy 			mac_control |= MIIEN;
6252b62997cSCyril Chemparathy 	}
6262b62997cSCyril Chemparathy 
6272b62997cSCyril Chemparathy 	if (mac_control == slave->mac_control)
6282b62997cSCyril Chemparathy 		return;
6292b62997cSCyril Chemparathy 
6302b62997cSCyril Chemparathy 	if (mac_control) {
6312b62997cSCyril Chemparathy 		printf("link up on port %d, speed %d, %s duplex\n",
6322b62997cSCyril Chemparathy 				slave->slave_num, phy->speed,
6332b62997cSCyril Chemparathy 				(phy->duplex == DUPLEX_FULL) ? "full" : "half");
6342b62997cSCyril Chemparathy 	} else {
6352b62997cSCyril Chemparathy 		printf("link down on port %d\n", slave->slave_num);
6362b62997cSCyril Chemparathy 	}
6372b62997cSCyril Chemparathy 
6382b62997cSCyril Chemparathy 	__raw_writel(mac_control, &slave->sliver->mac_control);
6392b62997cSCyril Chemparathy 	slave->mac_control = mac_control;
6402b62997cSCyril Chemparathy }
6412b62997cSCyril Chemparathy 
6422b62997cSCyril Chemparathy static int cpsw_update_link(struct cpsw_priv *priv)
6432b62997cSCyril Chemparathy {
6442b62997cSCyril Chemparathy 	int link = 0;
6452b62997cSCyril Chemparathy 	struct cpsw_slave *slave;
6462b62997cSCyril Chemparathy 
6477a022753SMugunthan V N 	for_active_slave(slave, priv)
6482b62997cSCyril Chemparathy 		cpsw_slave_update_link(slave, priv, &link);
6495a834c1fSStefan Roese 
6502b62997cSCyril Chemparathy 	return link;
6512b62997cSCyril Chemparathy }
6522b62997cSCyril Chemparathy 
6532b62997cSCyril Chemparathy static inline u32  cpsw_get_slave_port(struct cpsw_priv *priv, u32 slave_num)
6542b62997cSCyril Chemparathy {
6552b62997cSCyril Chemparathy 	if (priv->host_port == 0)
6562b62997cSCyril Chemparathy 		return slave_num + 1;
6572b62997cSCyril Chemparathy 	else
6582b62997cSCyril Chemparathy 		return slave_num;
6592b62997cSCyril Chemparathy }
6602b62997cSCyril Chemparathy 
6612b62997cSCyril Chemparathy static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_priv *priv)
6622b62997cSCyril Chemparathy {
6632b62997cSCyril Chemparathy 	u32     slave_port;
6642b62997cSCyril Chemparathy 
6652b62997cSCyril Chemparathy 	setbit_and_wait_for_clear32(&slave->sliver->soft_reset);
6662b62997cSCyril Chemparathy 
6672b62997cSCyril Chemparathy 	/* setup priority mapping */
6682b62997cSCyril Chemparathy 	__raw_writel(0x76543210, &slave->sliver->rx_pri_map);
6692b62997cSCyril Chemparathy 	__raw_writel(0x33221100, &slave->regs->tx_pri_map);
6702b62997cSCyril Chemparathy 
6712b62997cSCyril Chemparathy 	/* setup max packet size, and mac address */
6722b62997cSCyril Chemparathy 	__raw_writel(PKT_MAX, &slave->sliver->rx_maxlen);
6732b62997cSCyril Chemparathy 	cpsw_set_slave_mac(slave, priv);
6742b62997cSCyril Chemparathy 
6752b62997cSCyril Chemparathy 	slave->mac_control = 0;	/* no link yet */
6762b62997cSCyril Chemparathy 
6772b62997cSCyril Chemparathy 	/* enable forwarding */
6782b62997cSCyril Chemparathy 	slave_port = cpsw_get_slave_port(priv, slave->slave_num);
6792b62997cSCyril Chemparathy 	cpsw_ale_port_state(priv, slave_port, ALE_PORT_STATE_FORWARD);
6802b62997cSCyril Chemparathy 
6810adb5b76SJoe Hershberger 	cpsw_ale_add_mcast(priv, net_bcast_ethaddr, 1 << slave_port);
68248ec5291SMugunthan V N 
6839c653aadSMugunthan V N 	priv->phy_mask |= 1 << slave->data->phy_addr;
6842b62997cSCyril Chemparathy }
6852b62997cSCyril Chemparathy 
6862b62997cSCyril Chemparathy static struct cpdma_desc *cpdma_desc_alloc(struct cpsw_priv *priv)
6872b62997cSCyril Chemparathy {
6882b62997cSCyril Chemparathy 	struct cpdma_desc *desc = priv->desc_free;
6892b62997cSCyril Chemparathy 
6902b62997cSCyril Chemparathy 	if (desc)
6912b62997cSCyril Chemparathy 		priv->desc_free = desc_read_ptr(desc, hw_next);
6922b62997cSCyril Chemparathy 	return desc;
6932b62997cSCyril Chemparathy }
6942b62997cSCyril Chemparathy 
6952b62997cSCyril Chemparathy static void cpdma_desc_free(struct cpsw_priv *priv, struct cpdma_desc *desc)
6962b62997cSCyril Chemparathy {
6972b62997cSCyril Chemparathy 	if (desc) {
6982b62997cSCyril Chemparathy 		desc_write(desc, hw_next, priv->desc_free);
6992b62997cSCyril Chemparathy 		priv->desc_free = desc;
7002b62997cSCyril Chemparathy 	}
7012b62997cSCyril Chemparathy }
7022b62997cSCyril Chemparathy 
7032b62997cSCyril Chemparathy static int cpdma_submit(struct cpsw_priv *priv, struct cpdma_chan *chan,
7042b62997cSCyril Chemparathy 			void *buffer, int len)
7052b62997cSCyril Chemparathy {
7062b62997cSCyril Chemparathy 	struct cpdma_desc *desc, *prev;
7072b62997cSCyril Chemparathy 	u32 mode;
7082b62997cSCyril Chemparathy 
7092b62997cSCyril Chemparathy 	desc = cpdma_desc_alloc(priv);
7102b62997cSCyril Chemparathy 	if (!desc)
7112b62997cSCyril Chemparathy 		return -ENOMEM;
7122b62997cSCyril Chemparathy 
7132b62997cSCyril Chemparathy 	if (len < PKT_MIN)
7142b62997cSCyril Chemparathy 		len = PKT_MIN;
7152b62997cSCyril Chemparathy 
7162b62997cSCyril Chemparathy 	mode = CPDMA_DESC_OWNER | CPDMA_DESC_SOP | CPDMA_DESC_EOP;
7172b62997cSCyril Chemparathy 
7182b62997cSCyril Chemparathy 	desc_write(desc, hw_next,   0);
7192b62997cSCyril Chemparathy 	desc_write(desc, hw_buffer, buffer);
7202b62997cSCyril Chemparathy 	desc_write(desc, hw_len,    len);
7212b62997cSCyril Chemparathy 	desc_write(desc, hw_mode,   mode | len);
7222b62997cSCyril Chemparathy 	desc_write(desc, sw_buffer, buffer);
7232b62997cSCyril Chemparathy 	desc_write(desc, sw_len,    len);
7242b62997cSCyril Chemparathy 
7252b62997cSCyril Chemparathy 	if (!chan->head) {
7262b62997cSCyril Chemparathy 		/* simple case - first packet enqueued */
7272b62997cSCyril Chemparathy 		chan->head = desc;
7282b62997cSCyril Chemparathy 		chan->tail = desc;
7292b62997cSCyril Chemparathy 		chan_write(chan, hdp, desc);
7302b62997cSCyril Chemparathy 		goto done;
7312b62997cSCyril Chemparathy 	}
7322b62997cSCyril Chemparathy 
7332b62997cSCyril Chemparathy 	/* not the first packet - enqueue at the tail */
7342b62997cSCyril Chemparathy 	prev = chan->tail;
7352b62997cSCyril Chemparathy 	desc_write(prev, hw_next, desc);
7362b62997cSCyril Chemparathy 	chan->tail = desc;
7372b62997cSCyril Chemparathy 
7382b62997cSCyril Chemparathy 	/* next check if EOQ has been triggered already */
7392b62997cSCyril Chemparathy 	if (desc_read(prev, hw_mode) & CPDMA_DESC_EOQ)
7402b62997cSCyril Chemparathy 		chan_write(chan, hdp, desc);
7412b62997cSCyril Chemparathy 
7422b62997cSCyril Chemparathy done:
7432b62997cSCyril Chemparathy 	if (chan->rxfree)
7442b62997cSCyril Chemparathy 		chan_write(chan, rxfree, 1);
7452b62997cSCyril Chemparathy 	return 0;
7462b62997cSCyril Chemparathy }
7472b62997cSCyril Chemparathy 
7482b62997cSCyril Chemparathy static int cpdma_process(struct cpsw_priv *priv, struct cpdma_chan *chan,
7492b62997cSCyril Chemparathy 			 void **buffer, int *len)
7502b62997cSCyril Chemparathy {
7512b62997cSCyril Chemparathy 	struct cpdma_desc *desc = chan->head;
7522b62997cSCyril Chemparathy 	u32 status;
7532b62997cSCyril Chemparathy 
7542b62997cSCyril Chemparathy 	if (!desc)
7552b62997cSCyril Chemparathy 		return -ENOENT;
7562b62997cSCyril Chemparathy 
7572b62997cSCyril Chemparathy 	status = desc_read(desc, hw_mode);
7582b62997cSCyril Chemparathy 
7592b62997cSCyril Chemparathy 	if (len)
7602b62997cSCyril Chemparathy 		*len = status & 0x7ff;
7612b62997cSCyril Chemparathy 
7622b62997cSCyril Chemparathy 	if (buffer)
7632b62997cSCyril Chemparathy 		*buffer = desc_read_ptr(desc, sw_buffer);
7642b62997cSCyril Chemparathy 
7652b62997cSCyril Chemparathy 	if (status & CPDMA_DESC_OWNER) {
7662b62997cSCyril Chemparathy 		if (chan_read(chan, hdp) == 0) {
7672b62997cSCyril Chemparathy 			if (desc_read(desc, hw_mode) & CPDMA_DESC_OWNER)
7682b62997cSCyril Chemparathy 				chan_write(chan, hdp, desc);
7692b62997cSCyril Chemparathy 		}
7702b62997cSCyril Chemparathy 
7712b62997cSCyril Chemparathy 		return -EBUSY;
7722b62997cSCyril Chemparathy 	}
7732b62997cSCyril Chemparathy 
7742b62997cSCyril Chemparathy 	chan->head = desc_read_ptr(desc, hw_next);
7752b62997cSCyril Chemparathy 	chan_write(chan, cp, desc);
7762b62997cSCyril Chemparathy 
7772b62997cSCyril Chemparathy 	cpdma_desc_free(priv, desc);
7782b62997cSCyril Chemparathy 	return 0;
7792b62997cSCyril Chemparathy }
7802b62997cSCyril Chemparathy 
781bcd5eedfSMugunthan V N static int _cpsw_init(struct cpsw_priv *priv, u8 *enetaddr)
7822b62997cSCyril Chemparathy {
7832b62997cSCyril Chemparathy 	struct cpsw_slave	*slave;
7842b62997cSCyril Chemparathy 	int i, ret;
7852b62997cSCyril Chemparathy 
7862b62997cSCyril Chemparathy 	/* soft reset the controller and initialize priv */
7872b62997cSCyril Chemparathy 	setbit_and_wait_for_clear32(&priv->regs->soft_reset);
7882b62997cSCyril Chemparathy 
7892b62997cSCyril Chemparathy 	/* initialize and reset the address lookup engine */
7902b62997cSCyril Chemparathy 	cpsw_ale_enable(priv, 1);
7912b62997cSCyril Chemparathy 	cpsw_ale_clear(priv, 1);
7922b62997cSCyril Chemparathy 	cpsw_ale_vlan_aware(priv, 0); /* vlan unaware mode */
7932b62997cSCyril Chemparathy 
7942b62997cSCyril Chemparathy 	/* setup host port priority mapping */
7952b62997cSCyril Chemparathy 	__raw_writel(0x76543210, &priv->host_port_regs->cpdma_tx_pri_map);
7962b62997cSCyril Chemparathy 	__raw_writel(0, &priv->host_port_regs->cpdma_rx_chan_map);
7972b62997cSCyril Chemparathy 
7982b62997cSCyril Chemparathy 	/* disable priority elevation and enable statistics on all ports */
7992b62997cSCyril Chemparathy 	__raw_writel(0, &priv->regs->ptype);
8002b62997cSCyril Chemparathy 
8012b62997cSCyril Chemparathy 	/* enable statistics collection only on the host port */
8022b62997cSCyril Chemparathy 	__raw_writel(BIT(priv->host_port), &priv->regs->stat_port_en);
803454ac635SMugunthan V N 	__raw_writel(0x7, &priv->regs->stat_port_en);
8042b62997cSCyril Chemparathy 
8052b62997cSCyril Chemparathy 	cpsw_ale_port_state(priv, priv->host_port, ALE_PORT_STATE_FORWARD);
8062b62997cSCyril Chemparathy 
807bcd5eedfSMugunthan V N 	cpsw_ale_add_ucast(priv, enetaddr, priv->host_port, ALE_SECURE);
8080adb5b76SJoe Hershberger 	cpsw_ale_add_mcast(priv, net_bcast_ethaddr, 1 << priv->host_port);
8092b62997cSCyril Chemparathy 
8107a022753SMugunthan V N 	for_active_slave(slave, priv)
8112b62997cSCyril Chemparathy 		cpsw_slave_init(slave, priv);
8122b62997cSCyril Chemparathy 
8132b62997cSCyril Chemparathy 	cpsw_update_link(priv);
8142b62997cSCyril Chemparathy 
8152b62997cSCyril Chemparathy 	/* init descriptor pool */
8162b62997cSCyril Chemparathy 	for (i = 0; i < NUM_DESCS; i++) {
8172b62997cSCyril Chemparathy 		desc_write(&priv->descs[i], hw_next,
8182b62997cSCyril Chemparathy 			   (i == (NUM_DESCS - 1)) ? 0 : &priv->descs[i+1]);
8192b62997cSCyril Chemparathy 	}
8202b62997cSCyril Chemparathy 	priv->desc_free = &priv->descs[0];
8212b62997cSCyril Chemparathy 
8222b62997cSCyril Chemparathy 	/* initialize channels */
8232b62997cSCyril Chemparathy 	if (priv->data.version == CPSW_CTRL_VERSION_2) {
8242b62997cSCyril Chemparathy 		memset(&priv->rx_chan, 0, sizeof(struct cpdma_chan));
8252b62997cSCyril Chemparathy 		priv->rx_chan.hdp       = priv->dma_regs + CPDMA_RXHDP_VER2;
8262b62997cSCyril Chemparathy 		priv->rx_chan.cp        = priv->dma_regs + CPDMA_RXCP_VER2;
8272b62997cSCyril Chemparathy 		priv->rx_chan.rxfree    = priv->dma_regs + CPDMA_RXFREE;
8282b62997cSCyril Chemparathy 
8292b62997cSCyril Chemparathy 		memset(&priv->tx_chan, 0, sizeof(struct cpdma_chan));
8302b62997cSCyril Chemparathy 		priv->tx_chan.hdp       = priv->dma_regs + CPDMA_TXHDP_VER2;
8312b62997cSCyril Chemparathy 		priv->tx_chan.cp        = priv->dma_regs + CPDMA_TXCP_VER2;
8322b62997cSCyril Chemparathy 	} else {
8332b62997cSCyril Chemparathy 		memset(&priv->rx_chan, 0, sizeof(struct cpdma_chan));
8342b62997cSCyril Chemparathy 		priv->rx_chan.hdp       = priv->dma_regs + CPDMA_RXHDP_VER1;
8352b62997cSCyril Chemparathy 		priv->rx_chan.cp        = priv->dma_regs + CPDMA_RXCP_VER1;
8362b62997cSCyril Chemparathy 		priv->rx_chan.rxfree    = priv->dma_regs + CPDMA_RXFREE;
8372b62997cSCyril Chemparathy 
8382b62997cSCyril Chemparathy 		memset(&priv->tx_chan, 0, sizeof(struct cpdma_chan));
8392b62997cSCyril Chemparathy 		priv->tx_chan.hdp       = priv->dma_regs + CPDMA_TXHDP_VER1;
8402b62997cSCyril Chemparathy 		priv->tx_chan.cp        = priv->dma_regs + CPDMA_TXCP_VER1;
8412b62997cSCyril Chemparathy 	}
8422b62997cSCyril Chemparathy 
8432b62997cSCyril Chemparathy 	/* clear dma state */
8442b62997cSCyril Chemparathy 	setbit_and_wait_for_clear32(priv->dma_regs + CPDMA_SOFTRESET);
8452b62997cSCyril Chemparathy 
8462b62997cSCyril Chemparathy 	if (priv->data.version == CPSW_CTRL_VERSION_2) {
8472b62997cSCyril Chemparathy 		for (i = 0; i < priv->data.channels; i++) {
8482b62997cSCyril Chemparathy 			__raw_writel(0, priv->dma_regs + CPDMA_RXHDP_VER2 + 4
8492b62997cSCyril Chemparathy 					* i);
8502b62997cSCyril Chemparathy 			__raw_writel(0, priv->dma_regs + CPDMA_RXFREE + 4
8512b62997cSCyril Chemparathy 					* i);
8522b62997cSCyril Chemparathy 			__raw_writel(0, priv->dma_regs + CPDMA_RXCP_VER2 + 4
8532b62997cSCyril Chemparathy 					* i);
8542b62997cSCyril Chemparathy 			__raw_writel(0, priv->dma_regs + CPDMA_TXHDP_VER2 + 4
8552b62997cSCyril Chemparathy 					* i);
8562b62997cSCyril Chemparathy 			__raw_writel(0, priv->dma_regs + CPDMA_TXCP_VER2 + 4
8572b62997cSCyril Chemparathy 					* i);
8582b62997cSCyril Chemparathy 		}
8592b62997cSCyril Chemparathy 	} else {
8602b62997cSCyril Chemparathy 		for (i = 0; i < priv->data.channels; i++) {
8612b62997cSCyril Chemparathy 			__raw_writel(0, priv->dma_regs + CPDMA_RXHDP_VER1 + 4
8622b62997cSCyril Chemparathy 					* i);
8632b62997cSCyril Chemparathy 			__raw_writel(0, priv->dma_regs + CPDMA_RXFREE + 4
8642b62997cSCyril Chemparathy 					* i);
8652b62997cSCyril Chemparathy 			__raw_writel(0, priv->dma_regs + CPDMA_RXCP_VER1 + 4
8662b62997cSCyril Chemparathy 					* i);
8672b62997cSCyril Chemparathy 			__raw_writel(0, priv->dma_regs + CPDMA_TXHDP_VER1 + 4
8682b62997cSCyril Chemparathy 					* i);
8692b62997cSCyril Chemparathy 			__raw_writel(0, priv->dma_regs + CPDMA_TXCP_VER1 + 4
8702b62997cSCyril Chemparathy 					* i);
8712b62997cSCyril Chemparathy 
8722b62997cSCyril Chemparathy 		}
8732b62997cSCyril Chemparathy 	}
8742b62997cSCyril Chemparathy 
8752b62997cSCyril Chemparathy 	__raw_writel(1, priv->dma_regs + CPDMA_TXCONTROL);
8762b62997cSCyril Chemparathy 	__raw_writel(1, priv->dma_regs + CPDMA_RXCONTROL);
8772b62997cSCyril Chemparathy 
8782b62997cSCyril Chemparathy 	/* submit rx descs */
8792b62997cSCyril Chemparathy 	for (i = 0; i < PKTBUFSRX; i++) {
8801fd92db8SJoe Hershberger 		ret = cpdma_submit(priv, &priv->rx_chan, net_rx_packets[i],
8812b62997cSCyril Chemparathy 				   PKTSIZE);
8822b62997cSCyril Chemparathy 		if (ret < 0) {
8832b62997cSCyril Chemparathy 			printf("error %d submitting rx desc\n", ret);
8842b62997cSCyril Chemparathy 			break;
8852b62997cSCyril Chemparathy 		}
8862b62997cSCyril Chemparathy 	}
8872b62997cSCyril Chemparathy 
8882b62997cSCyril Chemparathy 	return 0;
8892b62997cSCyril Chemparathy }
8902b62997cSCyril Chemparathy 
891bcd5eedfSMugunthan V N static void _cpsw_halt(struct cpsw_priv *priv)
8922b62997cSCyril Chemparathy {
8932b62997cSCyril Chemparathy 	writel(0, priv->dma_regs + CPDMA_TXCONTROL);
8942b62997cSCyril Chemparathy 	writel(0, priv->dma_regs + CPDMA_RXCONTROL);
8952b62997cSCyril Chemparathy 
8962b62997cSCyril Chemparathy 	/* soft reset the controller and initialize priv */
8972b62997cSCyril Chemparathy 	setbit_and_wait_for_clear32(&priv->regs->soft_reset);
8982b62997cSCyril Chemparathy 
8992b62997cSCyril Chemparathy 	/* clear dma state */
9002b62997cSCyril Chemparathy 	setbit_and_wait_for_clear32(priv->dma_regs + CPDMA_SOFTRESET);
9012b62997cSCyril Chemparathy 
9022b62997cSCyril Chemparathy }
9032b62997cSCyril Chemparathy 
904bcd5eedfSMugunthan V N static int _cpsw_send(struct cpsw_priv *priv, void *packet, int length)
9052b62997cSCyril Chemparathy {
9062b62997cSCyril Chemparathy 	void *buffer;
9072b62997cSCyril Chemparathy 	int len;
9082b62997cSCyril Chemparathy 	int timeout = CPDMA_TIMEOUT;
9092b62997cSCyril Chemparathy 
9102b62997cSCyril Chemparathy 	flush_dcache_range((unsigned long)packet,
9112b62997cSCyril Chemparathy 			   (unsigned long)packet + length);
9122b62997cSCyril Chemparathy 
9132b62997cSCyril Chemparathy 	/* first reap completed packets */
9142b62997cSCyril Chemparathy 	while (timeout-- &&
9152b62997cSCyril Chemparathy 		(cpdma_process(priv, &priv->tx_chan, &buffer, &len) >= 0))
9162b62997cSCyril Chemparathy 		;
9172b62997cSCyril Chemparathy 
9182b62997cSCyril Chemparathy 	if (timeout == -1) {
9192b62997cSCyril Chemparathy 		printf("cpdma_process timeout\n");
9202b62997cSCyril Chemparathy 		return -ETIMEDOUT;
9212b62997cSCyril Chemparathy 	}
9222b62997cSCyril Chemparathy 
9232b62997cSCyril Chemparathy 	return cpdma_submit(priv, &priv->tx_chan, packet, length);
9242b62997cSCyril Chemparathy }
9252b62997cSCyril Chemparathy 
926bcd5eedfSMugunthan V N static int _cpsw_recv(struct cpsw_priv *priv, uchar **pkt)
9272b62997cSCyril Chemparathy {
9282b62997cSCyril Chemparathy 	void *buffer;
9292b62997cSCyril Chemparathy 	int len;
930bcd5eedfSMugunthan V N 	int ret = -EAGAIN;
9312b62997cSCyril Chemparathy 
932bcd5eedfSMugunthan V N 	ret = cpdma_process(priv, &priv->rx_chan, &buffer, &len);
933bcd5eedfSMugunthan V N 	if (ret < 0)
934bcd5eedfSMugunthan V N 		return ret;
935bcd5eedfSMugunthan V N 
9362b62997cSCyril Chemparathy 	invalidate_dcache_range((unsigned long)buffer,
9372b62997cSCyril Chemparathy 				(unsigned long)buffer + PKTSIZE_ALIGN);
938bcd5eedfSMugunthan V N 	*pkt = buffer;
9392b62997cSCyril Chemparathy 
940bcd5eedfSMugunthan V N 	return len;
9412b62997cSCyril Chemparathy }
9422b62997cSCyril Chemparathy 
9432b62997cSCyril Chemparathy static void cpsw_slave_setup(struct cpsw_slave *slave, int slave_num,
9442b62997cSCyril Chemparathy 			    struct cpsw_priv *priv)
9452b62997cSCyril Chemparathy {
9462b62997cSCyril Chemparathy 	void			*regs = priv->regs;
9472b62997cSCyril Chemparathy 	struct cpsw_slave_data	*data = priv->data.slave_data + slave_num;
9482b62997cSCyril Chemparathy 	slave->slave_num = slave_num;
9492b62997cSCyril Chemparathy 	slave->data	= data;
9502b62997cSCyril Chemparathy 	slave->regs	= regs + data->slave_reg_ofs;
9512b62997cSCyril Chemparathy 	slave->sliver	= regs + data->sliver_reg_ofs;
9522b62997cSCyril Chemparathy }
9532b62997cSCyril Chemparathy 
954bcd5eedfSMugunthan V N static int cpsw_phy_init(struct cpsw_priv *priv, struct cpsw_slave *slave)
9552b62997cSCyril Chemparathy {
9562b62997cSCyril Chemparathy 	struct phy_device *phydev;
957ef59bb7cSIlya Ledvich 	u32 supported = PHY_GBIT_FEATURES;
9582b62997cSCyril Chemparathy 
959cdd0729eSYegor Yefremov 	phydev = phy_connect(priv->bus,
9609c653aadSMugunthan V N 			slave->data->phy_addr,
961bcd5eedfSMugunthan V N 			priv->dev,
962cdd0729eSYegor Yefremov 			slave->data->phy_if);
9632b62997cSCyril Chemparathy 
96493ff2552SHeiko Schocher 	if (!phydev)
96593ff2552SHeiko Schocher 		return -1;
96693ff2552SHeiko Schocher 
9672b62997cSCyril Chemparathy 	phydev->supported &= supported;
9682b62997cSCyril Chemparathy 	phydev->advertising = phydev->supported;
9692b62997cSCyril Chemparathy 
970cb386227SDan Murphy #ifdef CONFIG_DM_ETH
971cb386227SDan Murphy 	if (slave->data->phy_of_handle)
972cb386227SDan Murphy 		phydev->dev->of_offset = slave->data->phy_of_handle;
973cb386227SDan Murphy #endif
974cb386227SDan Murphy 
9752b62997cSCyril Chemparathy 	priv->phydev = phydev;
9762b62997cSCyril Chemparathy 	phy_config(phydev);
9772b62997cSCyril Chemparathy 
9782b62997cSCyril Chemparathy 	return 1;
9792b62997cSCyril Chemparathy }
9802b62997cSCyril Chemparathy 
981bcd5eedfSMugunthan V N int _cpsw_register(struct cpsw_priv *priv)
9822b62997cSCyril Chemparathy {
9832b62997cSCyril Chemparathy 	struct cpsw_slave	*slave;
984bcd5eedfSMugunthan V N 	struct cpsw_platform_data *data = &priv->data;
9852b62997cSCyril Chemparathy 	void			*regs = (void *)data->cpsw_base;
9862b62997cSCyril Chemparathy 
9872b62997cSCyril Chemparathy 	priv->slaves = malloc(sizeof(struct cpsw_slave) * data->slaves);
9882b62997cSCyril Chemparathy 	if (!priv->slaves) {
9892b62997cSCyril Chemparathy 		return -ENOMEM;
9902b62997cSCyril Chemparathy 	}
9912b62997cSCyril Chemparathy 
9922b62997cSCyril Chemparathy 	priv->host_port		= data->host_port_num;
9932b62997cSCyril Chemparathy 	priv->regs		= regs;
9942b62997cSCyril Chemparathy 	priv->host_port_regs	= regs + data->host_port_reg_ofs;
9952b62997cSCyril Chemparathy 	priv->dma_regs		= regs + data->cpdma_reg_ofs;
9962b62997cSCyril Chemparathy 	priv->ale_regs		= regs + data->ale_reg_ofs;
9972bf36ac6SMugunthan V N 	priv->descs		= (void *)regs + data->bd_ram_ofs;
9982b62997cSCyril Chemparathy 
9992b62997cSCyril Chemparathy 	int idx = 0;
10002b62997cSCyril Chemparathy 
10012b62997cSCyril Chemparathy 	for_each_slave(slave, priv) {
10022b62997cSCyril Chemparathy 		cpsw_slave_setup(slave, idx, priv);
10032b62997cSCyril Chemparathy 		idx = idx + 1;
10042b62997cSCyril Chemparathy 	}
10052b62997cSCyril Chemparathy 
1006bcd5eedfSMugunthan V N 	cpsw_mdio_init(priv->dev->name, data->mdio_base, data->mdio_div);
1007bcd5eedfSMugunthan V N 	priv->bus = miiphy_get_dev_by_name(priv->dev->name);
1008bcd5eedfSMugunthan V N 	for_active_slave(slave, priv)
1009bcd5eedfSMugunthan V N 		cpsw_phy_init(priv, slave);
1010bcd5eedfSMugunthan V N 
1011bcd5eedfSMugunthan V N 	return 0;
1012bcd5eedfSMugunthan V N }
1013bcd5eedfSMugunthan V N 
10144cc77895SMugunthan V N #ifndef CONFIG_DM_ETH
1015bcd5eedfSMugunthan V N static int cpsw_init(struct eth_device *dev, bd_t *bis)
1016bcd5eedfSMugunthan V N {
1017bcd5eedfSMugunthan V N 	struct cpsw_priv	*priv = dev->priv;
1018bcd5eedfSMugunthan V N 
1019bcd5eedfSMugunthan V N 	return _cpsw_init(priv, dev->enetaddr);
1020bcd5eedfSMugunthan V N }
1021bcd5eedfSMugunthan V N 
1022bcd5eedfSMugunthan V N static void cpsw_halt(struct eth_device *dev)
1023bcd5eedfSMugunthan V N {
1024bcd5eedfSMugunthan V N 	struct cpsw_priv *priv = dev->priv;
1025bcd5eedfSMugunthan V N 
1026bcd5eedfSMugunthan V N 	return _cpsw_halt(priv);
1027bcd5eedfSMugunthan V N }
1028bcd5eedfSMugunthan V N 
1029bcd5eedfSMugunthan V N static int cpsw_send(struct eth_device *dev, void *packet, int length)
1030bcd5eedfSMugunthan V N {
1031bcd5eedfSMugunthan V N 	struct cpsw_priv	*priv = dev->priv;
1032bcd5eedfSMugunthan V N 
1033bcd5eedfSMugunthan V N 	return _cpsw_send(priv, packet, length);
1034bcd5eedfSMugunthan V N }
1035bcd5eedfSMugunthan V N 
1036bcd5eedfSMugunthan V N static int cpsw_recv(struct eth_device *dev)
1037bcd5eedfSMugunthan V N {
1038bcd5eedfSMugunthan V N 	struct cpsw_priv *priv = dev->priv;
1039bcd5eedfSMugunthan V N 	uchar *pkt = NULL;
1040bcd5eedfSMugunthan V N 	int len;
1041bcd5eedfSMugunthan V N 
1042bcd5eedfSMugunthan V N 	len = _cpsw_recv(priv, &pkt);
1043bcd5eedfSMugunthan V N 
1044bcd5eedfSMugunthan V N 	if (len > 0) {
1045bcd5eedfSMugunthan V N 		net_process_received_packet(pkt, len);
1046bcd5eedfSMugunthan V N 		cpdma_submit(priv, &priv->rx_chan, pkt, PKTSIZE);
1047bcd5eedfSMugunthan V N 	}
1048bcd5eedfSMugunthan V N 
1049bcd5eedfSMugunthan V N 	return len;
1050bcd5eedfSMugunthan V N }
1051bcd5eedfSMugunthan V N 
1052bcd5eedfSMugunthan V N int cpsw_register(struct cpsw_platform_data *data)
1053bcd5eedfSMugunthan V N {
1054bcd5eedfSMugunthan V N 	struct cpsw_priv	*priv;
1055bcd5eedfSMugunthan V N 	struct eth_device	*dev;
1056bcd5eedfSMugunthan V N 	int ret;
1057bcd5eedfSMugunthan V N 
1058bcd5eedfSMugunthan V N 	dev = calloc(sizeof(*dev), 1);
1059bcd5eedfSMugunthan V N 	if (!dev)
1060bcd5eedfSMugunthan V N 		return -ENOMEM;
1061bcd5eedfSMugunthan V N 
1062bcd5eedfSMugunthan V N 	priv = calloc(sizeof(*priv), 1);
1063bcd5eedfSMugunthan V N 	if (!priv) {
1064bcd5eedfSMugunthan V N 		free(dev);
1065bcd5eedfSMugunthan V N 		return -ENOMEM;
1066bcd5eedfSMugunthan V N 	}
1067bcd5eedfSMugunthan V N 
1068bcd5eedfSMugunthan V N 	priv->dev = dev;
1069bcd5eedfSMugunthan V N 	priv->data = *data;
1070bcd5eedfSMugunthan V N 
10712b62997cSCyril Chemparathy 	strcpy(dev->name, "cpsw");
10722b62997cSCyril Chemparathy 	dev->iobase	= 0;
10732b62997cSCyril Chemparathy 	dev->init	= cpsw_init;
10742b62997cSCyril Chemparathy 	dev->halt	= cpsw_halt;
10752b62997cSCyril Chemparathy 	dev->send	= cpsw_send;
10762b62997cSCyril Chemparathy 	dev->recv	= cpsw_recv;
10772b62997cSCyril Chemparathy 	dev->priv	= priv;
10782b62997cSCyril Chemparathy 
10792b62997cSCyril Chemparathy 	eth_register(dev);
10802b62997cSCyril Chemparathy 
1081bcd5eedfSMugunthan V N 	ret = _cpsw_register(priv);
1082bcd5eedfSMugunthan V N 	if (ret < 0) {
1083bcd5eedfSMugunthan V N 		eth_unregister(dev);
1084bcd5eedfSMugunthan V N 		free(dev);
1085bcd5eedfSMugunthan V N 		free(priv);
1086bcd5eedfSMugunthan V N 		return ret;
1087bcd5eedfSMugunthan V N 	}
10882b62997cSCyril Chemparathy 
10892b62997cSCyril Chemparathy 	return 1;
10902b62997cSCyril Chemparathy }
10914cc77895SMugunthan V N #else
10924cc77895SMugunthan V N static int cpsw_eth_start(struct udevice *dev)
10934cc77895SMugunthan V N {
10944cc77895SMugunthan V N 	struct eth_pdata *pdata = dev_get_platdata(dev);
10954cc77895SMugunthan V N 	struct cpsw_priv *priv = dev_get_priv(dev);
10964cc77895SMugunthan V N 
10974cc77895SMugunthan V N 	return _cpsw_init(priv, pdata->enetaddr);
10984cc77895SMugunthan V N }
10994cc77895SMugunthan V N 
11004cc77895SMugunthan V N static int cpsw_eth_send(struct udevice *dev, void *packet, int length)
11014cc77895SMugunthan V N {
11024cc77895SMugunthan V N 	struct cpsw_priv *priv = dev_get_priv(dev);
11034cc77895SMugunthan V N 
11044cc77895SMugunthan V N 	return _cpsw_send(priv, packet, length);
11054cc77895SMugunthan V N }
11064cc77895SMugunthan V N 
11074cc77895SMugunthan V N static int cpsw_eth_recv(struct udevice *dev, int flags, uchar **packetp)
11084cc77895SMugunthan V N {
11094cc77895SMugunthan V N 	struct cpsw_priv *priv = dev_get_priv(dev);
11104cc77895SMugunthan V N 
11114cc77895SMugunthan V N 	return _cpsw_recv(priv, packetp);
11124cc77895SMugunthan V N }
11134cc77895SMugunthan V N 
11144cc77895SMugunthan V N static int cpsw_eth_free_pkt(struct udevice *dev, uchar *packet,
11154cc77895SMugunthan V N 				   int length)
11164cc77895SMugunthan V N {
11174cc77895SMugunthan V N 	struct cpsw_priv *priv = dev_get_priv(dev);
11184cc77895SMugunthan V N 
11194cc77895SMugunthan V N 	return cpdma_submit(priv, &priv->rx_chan, packet, PKTSIZE);
11204cc77895SMugunthan V N }
11214cc77895SMugunthan V N 
11224cc77895SMugunthan V N static void cpsw_eth_stop(struct udevice *dev)
11234cc77895SMugunthan V N {
11244cc77895SMugunthan V N 	struct cpsw_priv *priv = dev_get_priv(dev);
11254cc77895SMugunthan V N 
11264cc77895SMugunthan V N 	return _cpsw_halt(priv);
11274cc77895SMugunthan V N }
11284cc77895SMugunthan V N 
11294cc77895SMugunthan V N 
11304cc77895SMugunthan V N static int cpsw_eth_probe(struct udevice *dev)
11314cc77895SMugunthan V N {
11324cc77895SMugunthan V N 	struct cpsw_priv *priv = dev_get_priv(dev);
11334cc77895SMugunthan V N 
11344cc77895SMugunthan V N 	priv->dev = dev;
11354cc77895SMugunthan V N 
11364cc77895SMugunthan V N 	return _cpsw_register(priv);
11374cc77895SMugunthan V N }
11384cc77895SMugunthan V N 
11394cc77895SMugunthan V N static const struct eth_ops cpsw_eth_ops = {
11404cc77895SMugunthan V N 	.start		= cpsw_eth_start,
11414cc77895SMugunthan V N 	.send		= cpsw_eth_send,
11424cc77895SMugunthan V N 	.recv		= cpsw_eth_recv,
11434cc77895SMugunthan V N 	.free_pkt	= cpsw_eth_free_pkt,
11444cc77895SMugunthan V N 	.stop		= cpsw_eth_stop,
11454cc77895SMugunthan V N };
11464cc77895SMugunthan V N 
114766e740cbSMugunthan V N static inline fdt_addr_t cpsw_get_addr_by_node(const void *fdt, int node)
114866e740cbSMugunthan V N {
114966e740cbSMugunthan V N 	return fdtdec_get_addr_size_auto_noparent(fdt, node, "reg", 0, NULL);
115066e740cbSMugunthan V N }
115166e740cbSMugunthan V N 
11524cc77895SMugunthan V N static int cpsw_eth_ofdata_to_platdata(struct udevice *dev)
11534cc77895SMugunthan V N {
11544cc77895SMugunthan V N 	struct eth_pdata *pdata = dev_get_platdata(dev);
11554cc77895SMugunthan V N 	struct cpsw_priv *priv = dev_get_priv(dev);
1156*2e205ef7SVignesh R 	struct gpio_desc *mode_gpios;
11574cc77895SMugunthan V N 	const char *phy_mode;
11584cc77895SMugunthan V N 	const void *fdt = gd->fdt_blob;
11594cc77895SMugunthan V N 	int node = dev->of_offset;
11604cc77895SMugunthan V N 	int subnode;
11614cc77895SMugunthan V N 	int slave_index = 0;
11624cc77895SMugunthan V N 	int active_slave;
1163*2e205ef7SVignesh R 	int num_mode_gpios;
1164e4310566SMugunthan V N 	int ret;
11654cc77895SMugunthan V N 
11664cc77895SMugunthan V N 	pdata->iobase = dev_get_addr(dev);
11674cc77895SMugunthan V N 	priv->data.version = CPSW_CTRL_VERSION_2;
11684cc77895SMugunthan V N 	priv->data.bd_ram_ofs = CPSW_BD_OFFSET;
11694cc77895SMugunthan V N 	priv->data.ale_reg_ofs = CPSW_ALE_OFFSET;
11704cc77895SMugunthan V N 	priv->data.cpdma_reg_ofs = CPSW_CPDMA_OFFSET;
11714cc77895SMugunthan V N 	priv->data.mdio_div = CPSW_MDIO_DIV;
11724cc77895SMugunthan V N 	priv->data.host_port_reg_ofs = CPSW_HOST_PORT_OFFSET,
11734cc77895SMugunthan V N 
11744cc77895SMugunthan V N 	pdata->phy_interface = -1;
11754cc77895SMugunthan V N 
11764cc77895SMugunthan V N 	priv->data.cpsw_base = pdata->iobase;
11774cc77895SMugunthan V N 	priv->data.channels = fdtdec_get_int(fdt, node, "cpdma_channels", -1);
11784cc77895SMugunthan V N 	if (priv->data.channels <= 0) {
11794cc77895SMugunthan V N 		printf("error: cpdma_channels not found in dt\n");
11804cc77895SMugunthan V N 		return -ENOENT;
11814cc77895SMugunthan V N 	}
11824cc77895SMugunthan V N 
11834cc77895SMugunthan V N 	priv->data.slaves = fdtdec_get_int(fdt, node, "slaves", -1);
11844cc77895SMugunthan V N 	if (priv->data.slaves <= 0) {
11854cc77895SMugunthan V N 		printf("error: slaves not found in dt\n");
11864cc77895SMugunthan V N 		return -ENOENT;
11874cc77895SMugunthan V N 	}
11884cc77895SMugunthan V N 	priv->data.slave_data = malloc(sizeof(struct cpsw_slave_data) *
11894cc77895SMugunthan V N 				       priv->data.slaves);
11904cc77895SMugunthan V N 
11914cc77895SMugunthan V N 	priv->data.ale_entries = fdtdec_get_int(fdt, node, "ale_entries", -1);
11924cc77895SMugunthan V N 	if (priv->data.ale_entries <= 0) {
11934cc77895SMugunthan V N 		printf("error: ale_entries not found in dt\n");
11944cc77895SMugunthan V N 		return -ENOENT;
11954cc77895SMugunthan V N 	}
11964cc77895SMugunthan V N 
11974cc77895SMugunthan V N 	priv->data.bd_ram_ofs = fdtdec_get_int(fdt, node, "bd_ram_size", -1);
11984cc77895SMugunthan V N 	if (priv->data.bd_ram_ofs <= 0) {
11994cc77895SMugunthan V N 		printf("error: bd_ram_size not found in dt\n");
12004cc77895SMugunthan V N 		return -ENOENT;
12014cc77895SMugunthan V N 	}
12024cc77895SMugunthan V N 
12034cc77895SMugunthan V N 	priv->data.mac_control = fdtdec_get_int(fdt, node, "mac_control", -1);
12044cc77895SMugunthan V N 	if (priv->data.mac_control <= 0) {
12054cc77895SMugunthan V N 		printf("error: ale_entries not found in dt\n");
12064cc77895SMugunthan V N 		return -ENOENT;
12074cc77895SMugunthan V N 	}
12084cc77895SMugunthan V N 
1209*2e205ef7SVignesh R 	num_mode_gpios = gpio_get_list_count(dev, "mode-gpios");
1210*2e205ef7SVignesh R 	if (num_mode_gpios > 0) {
1211*2e205ef7SVignesh R 		mode_gpios = malloc(sizeof(struct gpio_desc) *
1212*2e205ef7SVignesh R 				    num_mode_gpios);
1213*2e205ef7SVignesh R 		gpio_request_list_by_name(dev, "mode-gpios", mode_gpios,
1214*2e205ef7SVignesh R 					  num_mode_gpios, GPIOD_IS_OUT);
1215*2e205ef7SVignesh R 		free(mode_gpios);
1216*2e205ef7SVignesh R 	}
1217*2e205ef7SVignesh R 
12184cc77895SMugunthan V N 	active_slave = fdtdec_get_int(fdt, node, "active_slave", 0);
12194cc77895SMugunthan V N 	priv->data.active_slave = active_slave;
12204cc77895SMugunthan V N 
12214cc77895SMugunthan V N 	fdt_for_each_subnode(fdt, subnode, node) {
12224cc77895SMugunthan V N 		int len;
12234cc77895SMugunthan V N 		const char *name;
12244cc77895SMugunthan V N 
12254cc77895SMugunthan V N 		name = fdt_get_name(fdt, subnode, &len);
12264cc77895SMugunthan V N 		if (!strncmp(name, "mdio", 4)) {
122766e740cbSMugunthan V N 			u32 mdio_base;
122866e740cbSMugunthan V N 
122966e740cbSMugunthan V N 			mdio_base = cpsw_get_addr_by_node(fdt, subnode);
123066e740cbSMugunthan V N 			if (mdio_base == FDT_ADDR_T_NONE) {
123166e740cbSMugunthan V N 				error("Not able to get MDIO address space\n");
123266e740cbSMugunthan V N 				return -ENOENT;
123366e740cbSMugunthan V N 			}
123466e740cbSMugunthan V N 			priv->data.mdio_base = mdio_base;
12354cc77895SMugunthan V N 		}
12364cc77895SMugunthan V N 
12374cc77895SMugunthan V N 		if (!strncmp(name, "slave", 5)) {
12384cc77895SMugunthan V N 			u32 phy_id[2];
12394cc77895SMugunthan V N 
1240b2003c54SMugunthan V N 			if (slave_index >= priv->data.slaves)
1241b2003c54SMugunthan V N 				continue;
12424cc77895SMugunthan V N 			phy_mode = fdt_getprop(fdt, subnode, "phy-mode", NULL);
12434cc77895SMugunthan V N 			if (phy_mode)
12444cc77895SMugunthan V N 				priv->data.slave_data[slave_index].phy_if =
12454cc77895SMugunthan V N 					phy_get_interface_by_name(phy_mode);
1246cb386227SDan Murphy 
1247cb386227SDan Murphy 			priv->data.slave_data[slave_index].phy_of_handle =
1248cb386227SDan Murphy 				fdtdec_lookup_phandle(fdt, subnode,
1249cb386227SDan Murphy 						      "phy-handle");
1250cb386227SDan Murphy 
1251cb386227SDan Murphy 			if (priv->data.slave_data[slave_index].phy_of_handle >= 0) {
1252cb386227SDan Murphy 				priv->data.slave_data[slave_index].phy_addr =
1253cb386227SDan Murphy 						fdtdec_get_int(gd->fdt_blob,
1254cb386227SDan Murphy 							       priv->data.slave_data[slave_index].phy_of_handle,
1255cb386227SDan Murphy 							       "reg", -1);
1256cb386227SDan Murphy 			} else {
1257cb386227SDan Murphy 				fdtdec_get_int_array(fdt, subnode, "phy_id",
1258cb386227SDan Murphy 						     phy_id, 2);
1259cb386227SDan Murphy 				priv->data.slave_data[slave_index].phy_addr =
1260cb386227SDan Murphy 						phy_id[1];
1261cb386227SDan Murphy 			}
12624cc77895SMugunthan V N 			slave_index++;
12634cc77895SMugunthan V N 		}
12644cc77895SMugunthan V N 
12654cc77895SMugunthan V N 		if (!strncmp(name, "cpsw-phy-sel", 12)) {
126666e740cbSMugunthan V N 			priv->data.gmii_sel = cpsw_get_addr_by_node(fdt,
126766e740cbSMugunthan V N 								    subnode);
126866e740cbSMugunthan V N 
126966e740cbSMugunthan V N 			if (priv->data.gmii_sel == FDT_ADDR_T_NONE) {
127066e740cbSMugunthan V N 				error("Not able to get gmii_sel reg address\n");
127166e740cbSMugunthan V N 				return -ENOENT;
127266e740cbSMugunthan V N 			}
12734cc77895SMugunthan V N 		}
12744cc77895SMugunthan V N 	}
12754cc77895SMugunthan V N 
12764cc77895SMugunthan V N 	priv->data.slave_data[0].slave_reg_ofs = CPSW_SLAVE0_OFFSET;
12774cc77895SMugunthan V N 	priv->data.slave_data[0].sliver_reg_ofs = CPSW_SLIVER0_OFFSET;
12784cc77895SMugunthan V N 
12794cc77895SMugunthan V N 	if (priv->data.slaves == 2) {
12804cc77895SMugunthan V N 		priv->data.slave_data[1].slave_reg_ofs = CPSW_SLAVE1_OFFSET;
12814cc77895SMugunthan V N 		priv->data.slave_data[1].sliver_reg_ofs = CPSW_SLIVER1_OFFSET;
12824cc77895SMugunthan V N 	}
12834cc77895SMugunthan V N 
1284e4310566SMugunthan V N 	ret = ti_cm_get_macid(dev, active_slave, pdata->enetaddr);
1285e4310566SMugunthan V N 	if (ret < 0) {
1286e4310566SMugunthan V N 		error("cpsw read efuse mac failed\n");
1287e4310566SMugunthan V N 		return ret;
1288e4310566SMugunthan V N 	}
12894cc77895SMugunthan V N 
12904cc77895SMugunthan V N 	pdata->phy_interface = priv->data.slave_data[active_slave].phy_if;
12914cc77895SMugunthan V N 	if (pdata->phy_interface == -1) {
12924cc77895SMugunthan V N 		debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
12934cc77895SMugunthan V N 		return -EINVAL;
12944cc77895SMugunthan V N 	}
12954cc77895SMugunthan V N 	switch (pdata->phy_interface) {
12964cc77895SMugunthan V N 	case PHY_INTERFACE_MODE_MII:
12974cc77895SMugunthan V N 		writel(MII_MODE_ENABLE, priv->data.gmii_sel);
12984cc77895SMugunthan V N 		break;
12994cc77895SMugunthan V N 	case PHY_INTERFACE_MODE_RMII:
13004cc77895SMugunthan V N 		writel(RMII_MODE_ENABLE, priv->data.gmii_sel);
13014cc77895SMugunthan V N 		break;
13024cc77895SMugunthan V N 	case PHY_INTERFACE_MODE_RGMII:
13034cc77895SMugunthan V N 	case PHY_INTERFACE_MODE_RGMII_ID:
13044cc77895SMugunthan V N 	case PHY_INTERFACE_MODE_RGMII_RXID:
13054cc77895SMugunthan V N 	case PHY_INTERFACE_MODE_RGMII_TXID:
13064cc77895SMugunthan V N 		writel(RGMII_MODE_ENABLE, priv->data.gmii_sel);
13074cc77895SMugunthan V N 		break;
13084cc77895SMugunthan V N 	}
1309e4310566SMugunthan V N 
13104cc77895SMugunthan V N 	return 0;
13114cc77895SMugunthan V N }
13124cc77895SMugunthan V N 
13134cc77895SMugunthan V N 
13144cc77895SMugunthan V N static const struct udevice_id cpsw_eth_ids[] = {
13154cc77895SMugunthan V N 	{ .compatible = "ti,cpsw" },
13164cc77895SMugunthan V N 	{ .compatible = "ti,am335x-cpsw" },
13174cc77895SMugunthan V N 	{ }
13184cc77895SMugunthan V N };
13194cc77895SMugunthan V N 
13204cc77895SMugunthan V N U_BOOT_DRIVER(eth_cpsw) = {
13214cc77895SMugunthan V N 	.name	= "eth_cpsw",
13224cc77895SMugunthan V N 	.id	= UCLASS_ETH,
13234cc77895SMugunthan V N 	.of_match = cpsw_eth_ids,
13244cc77895SMugunthan V N 	.ofdata_to_platdata = cpsw_eth_ofdata_to_platdata,
13254cc77895SMugunthan V N 	.probe	= cpsw_eth_probe,
13264cc77895SMugunthan V N 	.ops	= &cpsw_eth_ops,
13274cc77895SMugunthan V N 	.priv_auto_alloc_size = sizeof(struct cpsw_priv),
13284cc77895SMugunthan V N 	.platdata_auto_alloc_size = sizeof(struct eth_pdata),
13294cc77895SMugunthan V N 	.flags = DM_FLAG_ALLOC_PRIV_DMA,
13304cc77895SMugunthan V N };
13314cc77895SMugunthan V N #endif /* CONFIG_DM_ETH */
1332