14863dea3SSunil Goutham /*
24863dea3SSunil Goutham  * Copyright (C) 2015 Cavium, Inc.
34863dea3SSunil Goutham  *
44863dea3SSunil Goutham  * This program is free software; you can redistribute it and/or modify it
54863dea3SSunil Goutham  * under the terms of version 2 of the GNU General Public License
64863dea3SSunil Goutham  * as published by the Free Software Foundation.
74863dea3SSunil Goutham  */
84863dea3SSunil Goutham 
946b903a0SDavid Daney #include <linux/acpi.h>
104863dea3SSunil Goutham #include <linux/module.h>
114863dea3SSunil Goutham #include <linux/interrupt.h>
124863dea3SSunil Goutham #include <linux/pci.h>
134863dea3SSunil Goutham #include <linux/netdevice.h>
144863dea3SSunil Goutham #include <linux/etherdevice.h>
154863dea3SSunil Goutham #include <linux/phy.h>
164863dea3SSunil Goutham #include <linux/of.h>
174863dea3SSunil Goutham #include <linux/of_mdio.h>
184863dea3SSunil Goutham #include <linux/of_net.h>
194863dea3SSunil Goutham 
204863dea3SSunil Goutham #include "nic_reg.h"
214863dea3SSunil Goutham #include "nic.h"
224863dea3SSunil Goutham #include "thunder_bgx.h"
234863dea3SSunil Goutham 
244863dea3SSunil Goutham #define DRV_NAME	"thunder-BGX"
254863dea3SSunil Goutham #define DRV_VERSION	"1.0"
264863dea3SSunil Goutham 
274863dea3SSunil Goutham struct lmac {
284863dea3SSunil Goutham 	struct bgx		*bgx;
294863dea3SSunil Goutham 	int			dmac;
3046b903a0SDavid Daney 	u8			mac[ETH_ALEN];
314863dea3SSunil Goutham 	bool			link_up;
324863dea3SSunil Goutham 	int			lmacid; /* ID within BGX */
334863dea3SSunil Goutham 	int			lmacid_bd; /* ID on board */
344863dea3SSunil Goutham 	struct net_device       netdev;
354863dea3SSunil Goutham 	struct phy_device       *phydev;
364863dea3SSunil Goutham 	unsigned int            last_duplex;
374863dea3SSunil Goutham 	unsigned int            last_link;
384863dea3SSunil Goutham 	unsigned int            last_speed;
394863dea3SSunil Goutham 	bool			is_sgmii;
404863dea3SSunil Goutham 	struct delayed_work	dwork;
414863dea3SSunil Goutham 	struct workqueue_struct *check_link;
420c886a1dSAleksey Makarov };
434863dea3SSunil Goutham 
444863dea3SSunil Goutham struct bgx {
454863dea3SSunil Goutham 	u8			bgx_id;
464863dea3SSunil Goutham 	u8			qlm_mode;
474863dea3SSunil Goutham 	struct	lmac		lmac[MAX_LMAC_PER_BGX];
484863dea3SSunil Goutham 	int			lmac_count;
494863dea3SSunil Goutham 	int                     lmac_type;
504863dea3SSunil Goutham 	int                     lane_to_sds;
514863dea3SSunil Goutham 	int			use_training;
524863dea3SSunil Goutham 	void __iomem		*reg_base;
534863dea3SSunil Goutham 	struct pci_dev		*pdev;
540c886a1dSAleksey Makarov };
554863dea3SSunil Goutham 
56fd7ec062SAleksey Makarov static struct bgx *bgx_vnic[MAX_BGX_THUNDER];
574863dea3SSunil Goutham static int lmac_count; /* Total no of LMACs in system */
584863dea3SSunil Goutham 
594863dea3SSunil Goutham static int bgx_xaui_check_link(struct lmac *lmac);
604863dea3SSunil Goutham 
614863dea3SSunil Goutham /* Supported devices */
624863dea3SSunil Goutham static const struct pci_device_id bgx_id_table[] = {
634863dea3SSunil Goutham 	{ PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVICE_ID_THUNDER_BGX) },
644863dea3SSunil Goutham 	{ 0, }  /* end of table */
654863dea3SSunil Goutham };
664863dea3SSunil Goutham 
674863dea3SSunil Goutham MODULE_AUTHOR("Cavium Inc");
684863dea3SSunil Goutham MODULE_DESCRIPTION("Cavium Thunder BGX/MAC Driver");
694863dea3SSunil Goutham MODULE_LICENSE("GPL v2");
704863dea3SSunil Goutham MODULE_VERSION(DRV_VERSION);
714863dea3SSunil Goutham MODULE_DEVICE_TABLE(pci, bgx_id_table);
724863dea3SSunil Goutham 
734863dea3SSunil Goutham /* The Cavium ThunderX network controller can *only* be found in SoCs
744863dea3SSunil Goutham  * containing the ThunderX ARM64 CPU implementation.  All accesses to the device
754863dea3SSunil Goutham  * registers on this platform are implicitly strongly ordered with respect
764863dea3SSunil Goutham  * to memory accesses. So writeq_relaxed() and readq_relaxed() are safe to use
774863dea3SSunil Goutham  * with no memory barriers in this driver.  The readq()/writeq() functions add
784863dea3SSunil Goutham  * explicit ordering operation which in this case are redundant, and only
794863dea3SSunil Goutham  * add overhead.
804863dea3SSunil Goutham  */
814863dea3SSunil Goutham 
824863dea3SSunil Goutham /* Register read/write APIs */
834863dea3SSunil Goutham static u64 bgx_reg_read(struct bgx *bgx, u8 lmac, u64 offset)
844863dea3SSunil Goutham {
854863dea3SSunil Goutham 	void __iomem *addr = bgx->reg_base + ((u32)lmac << 20) + offset;
864863dea3SSunil Goutham 
874863dea3SSunil Goutham 	return readq_relaxed(addr);
884863dea3SSunil Goutham }
894863dea3SSunil Goutham 
904863dea3SSunil Goutham static void bgx_reg_write(struct bgx *bgx, u8 lmac, u64 offset, u64 val)
914863dea3SSunil Goutham {
924863dea3SSunil Goutham 	void __iomem *addr = bgx->reg_base + ((u32)lmac << 20) + offset;
934863dea3SSunil Goutham 
944863dea3SSunil Goutham 	writeq_relaxed(val, addr);
954863dea3SSunil Goutham }
964863dea3SSunil Goutham 
974863dea3SSunil Goutham static void bgx_reg_modify(struct bgx *bgx, u8 lmac, u64 offset, u64 val)
984863dea3SSunil Goutham {
994863dea3SSunil Goutham 	void __iomem *addr = bgx->reg_base + ((u32)lmac << 20) + offset;
1004863dea3SSunil Goutham 
1014863dea3SSunil Goutham 	writeq_relaxed(val | readq_relaxed(addr), addr);
1024863dea3SSunil Goutham }
1034863dea3SSunil Goutham 
1044863dea3SSunil Goutham static int bgx_poll_reg(struct bgx *bgx, u8 lmac, u64 reg, u64 mask, bool zero)
1054863dea3SSunil Goutham {
1064863dea3SSunil Goutham 	int timeout = 100;
1074863dea3SSunil Goutham 	u64 reg_val;
1084863dea3SSunil Goutham 
1094863dea3SSunil Goutham 	while (timeout) {
1104863dea3SSunil Goutham 		reg_val = bgx_reg_read(bgx, lmac, reg);
1114863dea3SSunil Goutham 		if (zero && !(reg_val & mask))
1124863dea3SSunil Goutham 			return 0;
1134863dea3SSunil Goutham 		if (!zero && (reg_val & mask))
1144863dea3SSunil Goutham 			return 0;
1154863dea3SSunil Goutham 		usleep_range(1000, 2000);
1164863dea3SSunil Goutham 		timeout--;
1174863dea3SSunil Goutham 	}
1184863dea3SSunil Goutham 	return 1;
1194863dea3SSunil Goutham }
1204863dea3SSunil Goutham 
1214863dea3SSunil Goutham /* Return number of BGX present in HW */
1224863dea3SSunil Goutham unsigned bgx_get_map(int node)
1234863dea3SSunil Goutham {
1244863dea3SSunil Goutham 	int i;
1254863dea3SSunil Goutham 	unsigned map = 0;
1264863dea3SSunil Goutham 
1274863dea3SSunil Goutham 	for (i = 0; i < MAX_BGX_PER_CN88XX; i++) {
1284863dea3SSunil Goutham 		if (bgx_vnic[(node * MAX_BGX_PER_CN88XX) + i])
1294863dea3SSunil Goutham 			map |= (1 << i);
1304863dea3SSunil Goutham 	}
1314863dea3SSunil Goutham 
1324863dea3SSunil Goutham 	return map;
1334863dea3SSunil Goutham }
1344863dea3SSunil Goutham EXPORT_SYMBOL(bgx_get_map);
1354863dea3SSunil Goutham 
1364863dea3SSunil Goutham /* Return number of LMAC configured for this BGX */
1374863dea3SSunil Goutham int bgx_get_lmac_count(int node, int bgx_idx)
1384863dea3SSunil Goutham {
1394863dea3SSunil Goutham 	struct bgx *bgx;
1404863dea3SSunil Goutham 
1414863dea3SSunil Goutham 	bgx = bgx_vnic[(node * MAX_BGX_PER_CN88XX) + bgx_idx];
1424863dea3SSunil Goutham 	if (bgx)
1434863dea3SSunil Goutham 		return bgx->lmac_count;
1444863dea3SSunil Goutham 
1454863dea3SSunil Goutham 	return 0;
1464863dea3SSunil Goutham }
1474863dea3SSunil Goutham EXPORT_SYMBOL(bgx_get_lmac_count);
1484863dea3SSunil Goutham 
1494863dea3SSunil Goutham /* Returns the current link status of LMAC */
1504863dea3SSunil Goutham void bgx_get_lmac_link_state(int node, int bgx_idx, int lmacid, void *status)
1514863dea3SSunil Goutham {
1524863dea3SSunil Goutham 	struct bgx_link_status *link = (struct bgx_link_status *)status;
1534863dea3SSunil Goutham 	struct bgx *bgx;
1544863dea3SSunil Goutham 	struct lmac *lmac;
1554863dea3SSunil Goutham 
1564863dea3SSunil Goutham 	bgx = bgx_vnic[(node * MAX_BGX_PER_CN88XX) + bgx_idx];
1574863dea3SSunil Goutham 	if (!bgx)
1584863dea3SSunil Goutham 		return;
1594863dea3SSunil Goutham 
1604863dea3SSunil Goutham 	lmac = &bgx->lmac[lmacid];
1614863dea3SSunil Goutham 	link->link_up = lmac->link_up;
1624863dea3SSunil Goutham 	link->duplex = lmac->last_duplex;
1634863dea3SSunil Goutham 	link->speed = lmac->last_speed;
1644863dea3SSunil Goutham }
1654863dea3SSunil Goutham EXPORT_SYMBOL(bgx_get_lmac_link_state);
1664863dea3SSunil Goutham 
167e610cb32SAleksey Makarov const u8 *bgx_get_lmac_mac(int node, int bgx_idx, int lmacid)
1684863dea3SSunil Goutham {
1694863dea3SSunil Goutham 	struct bgx *bgx = bgx_vnic[(node * MAX_BGX_PER_CN88XX) + bgx_idx];
1704863dea3SSunil Goutham 
1714863dea3SSunil Goutham 	if (bgx)
1724863dea3SSunil Goutham 		return bgx->lmac[lmacid].mac;
1734863dea3SSunil Goutham 
1744863dea3SSunil Goutham 	return NULL;
1754863dea3SSunil Goutham }
1764863dea3SSunil Goutham EXPORT_SYMBOL(bgx_get_lmac_mac);
1774863dea3SSunil Goutham 
178e610cb32SAleksey Makarov void bgx_set_lmac_mac(int node, int bgx_idx, int lmacid, const u8 *mac)
1794863dea3SSunil Goutham {
1804863dea3SSunil Goutham 	struct bgx *bgx = bgx_vnic[(node * MAX_BGX_PER_CN88XX) + bgx_idx];
1814863dea3SSunil Goutham 
1824863dea3SSunil Goutham 	if (!bgx)
1834863dea3SSunil Goutham 		return;
1844863dea3SSunil Goutham 
1854863dea3SSunil Goutham 	ether_addr_copy(bgx->lmac[lmacid].mac, mac);
1864863dea3SSunil Goutham }
1874863dea3SSunil Goutham EXPORT_SYMBOL(bgx_set_lmac_mac);
1884863dea3SSunil Goutham 
1894863dea3SSunil Goutham static void bgx_sgmii_change_link_state(struct lmac *lmac)
1904863dea3SSunil Goutham {
1914863dea3SSunil Goutham 	struct bgx *bgx = lmac->bgx;
1924863dea3SSunil Goutham 	u64 cmr_cfg;
1934863dea3SSunil Goutham 	u64 port_cfg = 0;
1944863dea3SSunil Goutham 	u64 misc_ctl = 0;
1954863dea3SSunil Goutham 
1964863dea3SSunil Goutham 	cmr_cfg = bgx_reg_read(bgx, lmac->lmacid, BGX_CMRX_CFG);
1974863dea3SSunil Goutham 	cmr_cfg &= ~CMR_EN;
1984863dea3SSunil Goutham 	bgx_reg_write(bgx, lmac->lmacid, BGX_CMRX_CFG, cmr_cfg);
1994863dea3SSunil Goutham 
2004863dea3SSunil Goutham 	port_cfg = bgx_reg_read(bgx, lmac->lmacid, BGX_GMP_GMI_PRTX_CFG);
2014863dea3SSunil Goutham 	misc_ctl = bgx_reg_read(bgx, lmac->lmacid, BGX_GMP_PCS_MISCX_CTL);
2024863dea3SSunil Goutham 
2034863dea3SSunil Goutham 	if (lmac->link_up) {
2044863dea3SSunil Goutham 		misc_ctl &= ~PCS_MISC_CTL_GMX_ENO;
2054863dea3SSunil Goutham 		port_cfg &= ~GMI_PORT_CFG_DUPLEX;
2064863dea3SSunil Goutham 		port_cfg |=  (lmac->last_duplex << 2);
2074863dea3SSunil Goutham 	} else {
2084863dea3SSunil Goutham 		misc_ctl |= PCS_MISC_CTL_GMX_ENO;
2094863dea3SSunil Goutham 	}
2104863dea3SSunil Goutham 
2114863dea3SSunil Goutham 	switch (lmac->last_speed) {
2124863dea3SSunil Goutham 	case 10:
2134863dea3SSunil Goutham 		port_cfg &= ~GMI_PORT_CFG_SPEED; /* speed 0 */
2144863dea3SSunil Goutham 		port_cfg |= GMI_PORT_CFG_SPEED_MSB;  /* speed_msb 1 */
2154863dea3SSunil Goutham 		port_cfg &= ~GMI_PORT_CFG_SLOT_TIME; /* slottime 0 */
2164863dea3SSunil Goutham 		misc_ctl &= ~PCS_MISC_CTL_SAMP_PT_MASK;
2174863dea3SSunil Goutham 		misc_ctl |= 50; /* samp_pt */
2184863dea3SSunil Goutham 		bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_TXX_SLOT, 64);
2194863dea3SSunil Goutham 		bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_TXX_BURST, 0);
2204863dea3SSunil Goutham 		break;
2214863dea3SSunil Goutham 	case 100:
2224863dea3SSunil Goutham 		port_cfg &= ~GMI_PORT_CFG_SPEED; /* speed 0 */
2234863dea3SSunil Goutham 		port_cfg &= ~GMI_PORT_CFG_SPEED_MSB; /* speed_msb 0 */
2244863dea3SSunil Goutham 		port_cfg &= ~GMI_PORT_CFG_SLOT_TIME; /* slottime 0 */
2254863dea3SSunil Goutham 		misc_ctl &= ~PCS_MISC_CTL_SAMP_PT_MASK;
2264863dea3SSunil Goutham 		misc_ctl |= 5; /* samp_pt */
2274863dea3SSunil Goutham 		bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_TXX_SLOT, 64);
2284863dea3SSunil Goutham 		bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_TXX_BURST, 0);
2294863dea3SSunil Goutham 		break;
2304863dea3SSunil Goutham 	case 1000:
2314863dea3SSunil Goutham 		port_cfg |= GMI_PORT_CFG_SPEED; /* speed 1 */
2324863dea3SSunil Goutham 		port_cfg &= ~GMI_PORT_CFG_SPEED_MSB; /* speed_msb 0 */
2334863dea3SSunil Goutham 		port_cfg |= GMI_PORT_CFG_SLOT_TIME; /* slottime 1 */
2344863dea3SSunil Goutham 		misc_ctl &= ~PCS_MISC_CTL_SAMP_PT_MASK;
2354863dea3SSunil Goutham 		misc_ctl |= 1; /* samp_pt */
2364863dea3SSunil Goutham 		bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_TXX_SLOT, 512);
2374863dea3SSunil Goutham 		if (lmac->last_duplex)
2384863dea3SSunil Goutham 			bgx_reg_write(bgx, lmac->lmacid,
2394863dea3SSunil Goutham 				      BGX_GMP_GMI_TXX_BURST, 0);
2404863dea3SSunil Goutham 		else
2414863dea3SSunil Goutham 			bgx_reg_write(bgx, lmac->lmacid,
2424863dea3SSunil Goutham 				      BGX_GMP_GMI_TXX_BURST, 8192);
2434863dea3SSunil Goutham 		break;
2444863dea3SSunil Goutham 	default:
2454863dea3SSunil Goutham 		break;
2464863dea3SSunil Goutham 	}
2474863dea3SSunil Goutham 	bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_PCS_MISCX_CTL, misc_ctl);
2484863dea3SSunil Goutham 	bgx_reg_write(bgx, lmac->lmacid, BGX_GMP_GMI_PRTX_CFG, port_cfg);
2494863dea3SSunil Goutham 
2504863dea3SSunil Goutham 	port_cfg = bgx_reg_read(bgx, lmac->lmacid, BGX_GMP_GMI_PRTX_CFG);
2514863dea3SSunil Goutham 
2524863dea3SSunil Goutham 	/* renable lmac */
2534863dea3SSunil Goutham 	cmr_cfg |= CMR_EN;
2544863dea3SSunil Goutham 	bgx_reg_write(bgx, lmac->lmacid, BGX_CMRX_CFG, cmr_cfg);
2554863dea3SSunil Goutham }
2564863dea3SSunil Goutham 
257fd7ec062SAleksey Makarov static void bgx_lmac_handler(struct net_device *netdev)
2584863dea3SSunil Goutham {
2594863dea3SSunil Goutham 	struct lmac *lmac = container_of(netdev, struct lmac, netdev);
2604863dea3SSunil Goutham 	struct phy_device *phydev = lmac->phydev;
2614863dea3SSunil Goutham 	int link_changed = 0;
2624863dea3SSunil Goutham 
2634863dea3SSunil Goutham 	if (!lmac)
2644863dea3SSunil Goutham 		return;
2654863dea3SSunil Goutham 
2664863dea3SSunil Goutham 	if (!phydev->link && lmac->last_link)
2674863dea3SSunil Goutham 		link_changed = -1;
2684863dea3SSunil Goutham 
2694863dea3SSunil Goutham 	if (phydev->link &&
2704863dea3SSunil Goutham 	    (lmac->last_duplex != phydev->duplex ||
2714863dea3SSunil Goutham 	     lmac->last_link != phydev->link ||
2724863dea3SSunil Goutham 	     lmac->last_speed != phydev->speed)) {
2734863dea3SSunil Goutham 			link_changed = 1;
2744863dea3SSunil Goutham 	}
2754863dea3SSunil Goutham 
2764863dea3SSunil Goutham 	lmac->last_link = phydev->link;
2774863dea3SSunil Goutham 	lmac->last_speed = phydev->speed;
2784863dea3SSunil Goutham 	lmac->last_duplex = phydev->duplex;
2794863dea3SSunil Goutham 
2804863dea3SSunil Goutham 	if (!link_changed)
2814863dea3SSunil Goutham 		return;
2824863dea3SSunil Goutham 
2834863dea3SSunil Goutham 	if (link_changed > 0)
2844863dea3SSunil Goutham 		lmac->link_up = true;
2854863dea3SSunil Goutham 	else
2864863dea3SSunil Goutham 		lmac->link_up = false;
2874863dea3SSunil Goutham 
2884863dea3SSunil Goutham 	if (lmac->is_sgmii)
2894863dea3SSunil Goutham 		bgx_sgmii_change_link_state(lmac);
2904863dea3SSunil Goutham 	else
2914863dea3SSunil Goutham 		bgx_xaui_check_link(lmac);
2924863dea3SSunil Goutham }
2934863dea3SSunil Goutham 
2944863dea3SSunil Goutham u64 bgx_get_rx_stats(int node, int bgx_idx, int lmac, int idx)
2954863dea3SSunil Goutham {
2964863dea3SSunil Goutham 	struct bgx *bgx;
2974863dea3SSunil Goutham 
2984863dea3SSunil Goutham 	bgx = bgx_vnic[(node * MAX_BGX_PER_CN88XX) + bgx_idx];
2994863dea3SSunil Goutham 	if (!bgx)
3004863dea3SSunil Goutham 		return 0;
3014863dea3SSunil Goutham 
3024863dea3SSunil Goutham 	if (idx > 8)
3034863dea3SSunil Goutham 		lmac = 0;
3044863dea3SSunil Goutham 	return bgx_reg_read(bgx, lmac, BGX_CMRX_RX_STAT0 + (idx * 8));
3054863dea3SSunil Goutham }
3064863dea3SSunil Goutham EXPORT_SYMBOL(bgx_get_rx_stats);
3074863dea3SSunil Goutham 
3084863dea3SSunil Goutham u64 bgx_get_tx_stats(int node, int bgx_idx, int lmac, int idx)
3094863dea3SSunil Goutham {
3104863dea3SSunil Goutham 	struct bgx *bgx;
3114863dea3SSunil Goutham 
3124863dea3SSunil Goutham 	bgx = bgx_vnic[(node * MAX_BGX_PER_CN88XX) + bgx_idx];
3134863dea3SSunil Goutham 	if (!bgx)
3144863dea3SSunil Goutham 		return 0;
3154863dea3SSunil Goutham 
3164863dea3SSunil Goutham 	return bgx_reg_read(bgx, lmac, BGX_CMRX_TX_STAT0 + (idx * 8));
3174863dea3SSunil Goutham }
3184863dea3SSunil Goutham EXPORT_SYMBOL(bgx_get_tx_stats);
3194863dea3SSunil Goutham 
3204863dea3SSunil Goutham static void bgx_flush_dmac_addrs(struct bgx *bgx, int lmac)
3214863dea3SSunil Goutham {
3224863dea3SSunil Goutham 	u64 offset;
3234863dea3SSunil Goutham 
3244863dea3SSunil Goutham 	while (bgx->lmac[lmac].dmac > 0) {
3254863dea3SSunil Goutham 		offset = ((bgx->lmac[lmac].dmac - 1) * sizeof(u64)) +
3264863dea3SSunil Goutham 			(lmac * MAX_DMAC_PER_LMAC * sizeof(u64));
3274863dea3SSunil Goutham 		bgx_reg_write(bgx, 0, BGX_CMR_RX_DMACX_CAM + offset, 0);
3284863dea3SSunil Goutham 		bgx->lmac[lmac].dmac--;
3294863dea3SSunil Goutham 	}
3304863dea3SSunil Goutham }
3314863dea3SSunil Goutham 
3324863dea3SSunil Goutham static int bgx_lmac_sgmii_init(struct bgx *bgx, int lmacid)
3334863dea3SSunil Goutham {
3344863dea3SSunil Goutham 	u64 cfg;
3354863dea3SSunil Goutham 
3364863dea3SSunil Goutham 	bgx_reg_modify(bgx, lmacid, BGX_GMP_GMI_TXX_THRESH, 0x30);
3374863dea3SSunil Goutham 	/* max packet size */
3384863dea3SSunil Goutham 	bgx_reg_modify(bgx, lmacid, BGX_GMP_GMI_RXX_JABBER, MAX_FRAME_SIZE);
3394863dea3SSunil Goutham 
3404863dea3SSunil Goutham 	/* Disable frame alignment if using preamble */
3414863dea3SSunil Goutham 	cfg = bgx_reg_read(bgx, lmacid, BGX_GMP_GMI_TXX_APPEND);
3424863dea3SSunil Goutham 	if (cfg & 1)
3434863dea3SSunil Goutham 		bgx_reg_write(bgx, lmacid, BGX_GMP_GMI_TXX_SGMII_CTL, 0);
3444863dea3SSunil Goutham 
3454863dea3SSunil Goutham 	/* Enable lmac */
3464863dea3SSunil Goutham 	bgx_reg_modify(bgx, lmacid, BGX_CMRX_CFG, CMR_EN);
3474863dea3SSunil Goutham 
3484863dea3SSunil Goutham 	/* PCS reset */
3494863dea3SSunil Goutham 	bgx_reg_modify(bgx, lmacid, BGX_GMP_PCS_MRX_CTL, PCS_MRX_CTL_RESET);
3504863dea3SSunil Goutham 	if (bgx_poll_reg(bgx, lmacid, BGX_GMP_PCS_MRX_CTL,
3514863dea3SSunil Goutham 			 PCS_MRX_CTL_RESET, true)) {
3524863dea3SSunil Goutham 		dev_err(&bgx->pdev->dev, "BGX PCS reset not completed\n");
3534863dea3SSunil Goutham 		return -1;
3544863dea3SSunil Goutham 	}
3554863dea3SSunil Goutham 
3564863dea3SSunil Goutham 	/* power down, reset autoneg, autoneg enable */
3574863dea3SSunil Goutham 	cfg = bgx_reg_read(bgx, lmacid, BGX_GMP_PCS_MRX_CTL);
3584863dea3SSunil Goutham 	cfg &= ~PCS_MRX_CTL_PWR_DN;
3594863dea3SSunil Goutham 	cfg |= (PCS_MRX_CTL_RST_AN | PCS_MRX_CTL_AN_EN);
3604863dea3SSunil Goutham 	bgx_reg_write(bgx, lmacid, BGX_GMP_PCS_MRX_CTL, cfg);
3614863dea3SSunil Goutham 
3624863dea3SSunil Goutham 	if (bgx_poll_reg(bgx, lmacid, BGX_GMP_PCS_MRX_STATUS,
3634863dea3SSunil Goutham 			 PCS_MRX_STATUS_AN_CPT, false)) {
3644863dea3SSunil Goutham 		dev_err(&bgx->pdev->dev, "BGX AN_CPT not completed\n");
3654863dea3SSunil Goutham 		return -1;
3664863dea3SSunil Goutham 	}
3674863dea3SSunil Goutham 
3684863dea3SSunil Goutham 	return 0;
3694863dea3SSunil Goutham }
3704863dea3SSunil Goutham 
3714863dea3SSunil Goutham static int bgx_lmac_xaui_init(struct bgx *bgx, int lmacid, int lmac_type)
3724863dea3SSunil Goutham {
3734863dea3SSunil Goutham 	u64 cfg;
3744863dea3SSunil Goutham 
3754863dea3SSunil Goutham 	/* Reset SPU */
3764863dea3SSunil Goutham 	bgx_reg_modify(bgx, lmacid, BGX_SPUX_CONTROL1, SPU_CTL_RESET);
3774863dea3SSunil Goutham 	if (bgx_poll_reg(bgx, lmacid, BGX_SPUX_CONTROL1, SPU_CTL_RESET, true)) {
3784863dea3SSunil Goutham 		dev_err(&bgx->pdev->dev, "BGX SPU reset not completed\n");
3794863dea3SSunil Goutham 		return -1;
3804863dea3SSunil Goutham 	}
3814863dea3SSunil Goutham 
3824863dea3SSunil Goutham 	/* Disable LMAC */
3834863dea3SSunil Goutham 	cfg = bgx_reg_read(bgx, lmacid, BGX_CMRX_CFG);
3844863dea3SSunil Goutham 	cfg &= ~CMR_EN;
3854863dea3SSunil Goutham 	bgx_reg_write(bgx, lmacid, BGX_CMRX_CFG, cfg);
3864863dea3SSunil Goutham 
3874863dea3SSunil Goutham 	bgx_reg_modify(bgx, lmacid, BGX_SPUX_CONTROL1, SPU_CTL_LOW_POWER);
3884863dea3SSunil Goutham 	/* Set interleaved running disparity for RXAUI */
3894863dea3SSunil Goutham 	if (bgx->lmac_type != BGX_MODE_RXAUI)
3904863dea3SSunil Goutham 		bgx_reg_modify(bgx, lmacid,
3914863dea3SSunil Goutham 			       BGX_SPUX_MISC_CONTROL, SPU_MISC_CTL_RX_DIS);
3924863dea3SSunil Goutham 	else
3934863dea3SSunil Goutham 		bgx_reg_modify(bgx, lmacid, BGX_SPUX_MISC_CONTROL,
3944863dea3SSunil Goutham 			       SPU_MISC_CTL_RX_DIS | SPU_MISC_CTL_INTLV_RDISP);
3954863dea3SSunil Goutham 
3964863dea3SSunil Goutham 	/* clear all interrupts */
3974863dea3SSunil Goutham 	cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_RX_INT);
3984863dea3SSunil Goutham 	bgx_reg_write(bgx, lmacid, BGX_SMUX_RX_INT, cfg);
3994863dea3SSunil Goutham 	cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_TX_INT);
4004863dea3SSunil Goutham 	bgx_reg_write(bgx, lmacid, BGX_SMUX_TX_INT, cfg);
4014863dea3SSunil Goutham 	cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_INT);
4024863dea3SSunil Goutham 	bgx_reg_write(bgx, lmacid, BGX_SPUX_INT, cfg);
4034863dea3SSunil Goutham 
4044863dea3SSunil Goutham 	if (bgx->use_training) {
4054863dea3SSunil Goutham 		bgx_reg_write(bgx, lmacid, BGX_SPUX_BR_PMD_LP_CUP, 0x00);
4064863dea3SSunil Goutham 		bgx_reg_write(bgx, lmacid, BGX_SPUX_BR_PMD_LD_CUP, 0x00);
4074863dea3SSunil Goutham 		bgx_reg_write(bgx, lmacid, BGX_SPUX_BR_PMD_LD_REP, 0x00);
4084863dea3SSunil Goutham 		/* training enable */
4094863dea3SSunil Goutham 		bgx_reg_modify(bgx, lmacid,
4104863dea3SSunil Goutham 			       BGX_SPUX_BR_PMD_CRTL, SPU_PMD_CRTL_TRAIN_EN);
4114863dea3SSunil Goutham 	}
4124863dea3SSunil Goutham 
4134863dea3SSunil Goutham 	/* Append FCS to each packet */
4144863dea3SSunil Goutham 	bgx_reg_modify(bgx, lmacid, BGX_SMUX_TX_APPEND, SMU_TX_APPEND_FCS_D);
4154863dea3SSunil Goutham 
4164863dea3SSunil Goutham 	/* Disable forward error correction */
4174863dea3SSunil Goutham 	cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_FEC_CONTROL);
4184863dea3SSunil Goutham 	cfg &= ~SPU_FEC_CTL_FEC_EN;
4194863dea3SSunil Goutham 	bgx_reg_write(bgx, lmacid, BGX_SPUX_FEC_CONTROL, cfg);
4204863dea3SSunil Goutham 
4214863dea3SSunil Goutham 	/* Disable autoneg */
4224863dea3SSunil Goutham 	cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_AN_CONTROL);
4234863dea3SSunil Goutham 	cfg = cfg & ~(SPU_AN_CTL_AN_EN | SPU_AN_CTL_XNP_EN);
4244863dea3SSunil Goutham 	bgx_reg_write(bgx, lmacid, BGX_SPUX_AN_CONTROL, cfg);
4254863dea3SSunil Goutham 
4264863dea3SSunil Goutham 	cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_AN_ADV);
4274863dea3SSunil Goutham 	if (bgx->lmac_type == BGX_MODE_10G_KR)
4284863dea3SSunil Goutham 		cfg |= (1 << 23);
4294863dea3SSunil Goutham 	else if (bgx->lmac_type == BGX_MODE_40G_KR)
4304863dea3SSunil Goutham 		cfg |= (1 << 24);
4314863dea3SSunil Goutham 	else
4324863dea3SSunil Goutham 		cfg &= ~((1 << 23) | (1 << 24));
4334863dea3SSunil Goutham 	cfg = cfg & (~((1ULL << 25) | (1ULL << 22) | (1ULL << 12)));
4344863dea3SSunil Goutham 	bgx_reg_write(bgx, lmacid, BGX_SPUX_AN_ADV, cfg);
4354863dea3SSunil Goutham 
4364863dea3SSunil Goutham 	cfg = bgx_reg_read(bgx, 0, BGX_SPU_DBG_CONTROL);
4374863dea3SSunil Goutham 	cfg &= ~SPU_DBG_CTL_AN_ARB_LINK_CHK_EN;
4384863dea3SSunil Goutham 	bgx_reg_write(bgx, 0, BGX_SPU_DBG_CONTROL, cfg);
4394863dea3SSunil Goutham 
4404863dea3SSunil Goutham 	/* Enable lmac */
4414863dea3SSunil Goutham 	bgx_reg_modify(bgx, lmacid, BGX_CMRX_CFG, CMR_EN);
4424863dea3SSunil Goutham 
4434863dea3SSunil Goutham 	cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_CONTROL1);
4444863dea3SSunil Goutham 	cfg &= ~SPU_CTL_LOW_POWER;
4454863dea3SSunil Goutham 	bgx_reg_write(bgx, lmacid, BGX_SPUX_CONTROL1, cfg);
4464863dea3SSunil Goutham 
4474863dea3SSunil Goutham 	cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_TX_CTL);
4484863dea3SSunil Goutham 	cfg &= ~SMU_TX_CTL_UNI_EN;
4494863dea3SSunil Goutham 	cfg |= SMU_TX_CTL_DIC_EN;
4504863dea3SSunil Goutham 	bgx_reg_write(bgx, lmacid, BGX_SMUX_TX_CTL, cfg);
4514863dea3SSunil Goutham 
4524863dea3SSunil Goutham 	/* take lmac_count into account */
4534863dea3SSunil Goutham 	bgx_reg_modify(bgx, lmacid, BGX_SMUX_TX_THRESH, (0x100 - 1));
4544863dea3SSunil Goutham 	/* max packet size */
4554863dea3SSunil Goutham 	bgx_reg_modify(bgx, lmacid, BGX_SMUX_RX_JABBER, MAX_FRAME_SIZE);
4564863dea3SSunil Goutham 
4574863dea3SSunil Goutham 	return 0;
4584863dea3SSunil Goutham }
4594863dea3SSunil Goutham 
4604863dea3SSunil Goutham static int bgx_xaui_check_link(struct lmac *lmac)
4614863dea3SSunil Goutham {
4624863dea3SSunil Goutham 	struct bgx *bgx = lmac->bgx;
4634863dea3SSunil Goutham 	int lmacid = lmac->lmacid;
4644863dea3SSunil Goutham 	int lmac_type = bgx->lmac_type;
4654863dea3SSunil Goutham 	u64 cfg;
4664863dea3SSunil Goutham 
4674863dea3SSunil Goutham 	bgx_reg_modify(bgx, lmacid, BGX_SPUX_MISC_CONTROL, SPU_MISC_CTL_RX_DIS);
4684863dea3SSunil Goutham 	if (bgx->use_training) {
4694863dea3SSunil Goutham 		cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_INT);
4704863dea3SSunil Goutham 		if (!(cfg & (1ull << 13))) {
4714863dea3SSunil Goutham 			cfg = (1ull << 13) | (1ull << 14);
4724863dea3SSunil Goutham 			bgx_reg_write(bgx, lmacid, BGX_SPUX_INT, cfg);
4734863dea3SSunil Goutham 			cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_BR_PMD_CRTL);
4744863dea3SSunil Goutham 			cfg |= (1ull << 0);
4754863dea3SSunil Goutham 			bgx_reg_write(bgx, lmacid, BGX_SPUX_BR_PMD_CRTL, cfg);
4764863dea3SSunil Goutham 			return -1;
4774863dea3SSunil Goutham 		}
4784863dea3SSunil Goutham 	}
4794863dea3SSunil Goutham 
4804863dea3SSunil Goutham 	/* wait for PCS to come out of reset */
4814863dea3SSunil Goutham 	if (bgx_poll_reg(bgx, lmacid, BGX_SPUX_CONTROL1, SPU_CTL_RESET, true)) {
4824863dea3SSunil Goutham 		dev_err(&bgx->pdev->dev, "BGX SPU reset not completed\n");
4834863dea3SSunil Goutham 		return -1;
4844863dea3SSunil Goutham 	}
4854863dea3SSunil Goutham 
4864863dea3SSunil Goutham 	if ((lmac_type == BGX_MODE_10G_KR) || (lmac_type == BGX_MODE_XFI) ||
4874863dea3SSunil Goutham 	    (lmac_type == BGX_MODE_40G_KR) || (lmac_type == BGX_MODE_XLAUI)) {
4884863dea3SSunil Goutham 		if (bgx_poll_reg(bgx, lmacid, BGX_SPUX_BR_STATUS1,
4894863dea3SSunil Goutham 				 SPU_BR_STATUS_BLK_LOCK, false)) {
4904863dea3SSunil Goutham 			dev_err(&bgx->pdev->dev,
4914863dea3SSunil Goutham 				"SPU_BR_STATUS_BLK_LOCK not completed\n");
4924863dea3SSunil Goutham 			return -1;
4934863dea3SSunil Goutham 		}
4944863dea3SSunil Goutham 	} else {
4954863dea3SSunil Goutham 		if (bgx_poll_reg(bgx, lmacid, BGX_SPUX_BX_STATUS,
4964863dea3SSunil Goutham 				 SPU_BX_STATUS_RX_ALIGN, false)) {
4974863dea3SSunil Goutham 			dev_err(&bgx->pdev->dev,
4984863dea3SSunil Goutham 				"SPU_BX_STATUS_RX_ALIGN not completed\n");
4994863dea3SSunil Goutham 			return -1;
5004863dea3SSunil Goutham 		}
5014863dea3SSunil Goutham 	}
5024863dea3SSunil Goutham 
5034863dea3SSunil Goutham 	/* Clear rcvflt bit (latching high) and read it back */
5044863dea3SSunil Goutham 	bgx_reg_modify(bgx, lmacid, BGX_SPUX_STATUS2, SPU_STATUS2_RCVFLT);
5054863dea3SSunil Goutham 	if (bgx_reg_read(bgx, lmacid, BGX_SPUX_STATUS2) & SPU_STATUS2_RCVFLT) {
5064863dea3SSunil Goutham 		dev_err(&bgx->pdev->dev, "Receive fault, retry training\n");
5074863dea3SSunil Goutham 		if (bgx->use_training) {
5084863dea3SSunil Goutham 			cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_INT);
5094863dea3SSunil Goutham 			if (!(cfg & (1ull << 13))) {
5104863dea3SSunil Goutham 				cfg = (1ull << 13) | (1ull << 14);
5114863dea3SSunil Goutham 				bgx_reg_write(bgx, lmacid, BGX_SPUX_INT, cfg);
5124863dea3SSunil Goutham 				cfg = bgx_reg_read(bgx, lmacid,
5134863dea3SSunil Goutham 						   BGX_SPUX_BR_PMD_CRTL);
5144863dea3SSunil Goutham 				cfg |= (1ull << 0);
5154863dea3SSunil Goutham 				bgx_reg_write(bgx, lmacid,
5164863dea3SSunil Goutham 					      BGX_SPUX_BR_PMD_CRTL, cfg);
5174863dea3SSunil Goutham 				return -1;
5184863dea3SSunil Goutham 			}
5194863dea3SSunil Goutham 		}
5204863dea3SSunil Goutham 		return -1;
5214863dea3SSunil Goutham 	}
5224863dea3SSunil Goutham 
5234863dea3SSunil Goutham 	/* Wait for MAC RX to be ready */
5244863dea3SSunil Goutham 	if (bgx_poll_reg(bgx, lmacid, BGX_SMUX_RX_CTL,
5254863dea3SSunil Goutham 			 SMU_RX_CTL_STATUS, true)) {
5264863dea3SSunil Goutham 		dev_err(&bgx->pdev->dev, "SMU RX link not okay\n");
5274863dea3SSunil Goutham 		return -1;
5284863dea3SSunil Goutham 	}
5294863dea3SSunil Goutham 
5304863dea3SSunil Goutham 	/* Wait for BGX RX to be idle */
5314863dea3SSunil Goutham 	if (bgx_poll_reg(bgx, lmacid, BGX_SMUX_CTL, SMU_CTL_RX_IDLE, false)) {
5324863dea3SSunil Goutham 		dev_err(&bgx->pdev->dev, "SMU RX not idle\n");
5334863dea3SSunil Goutham 		return -1;
5344863dea3SSunil Goutham 	}
5354863dea3SSunil Goutham 
5364863dea3SSunil Goutham 	/* Wait for BGX TX to be idle */
5374863dea3SSunil Goutham 	if (bgx_poll_reg(bgx, lmacid, BGX_SMUX_CTL, SMU_CTL_TX_IDLE, false)) {
5384863dea3SSunil Goutham 		dev_err(&bgx->pdev->dev, "SMU TX not idle\n");
5394863dea3SSunil Goutham 		return -1;
5404863dea3SSunil Goutham 	}
5414863dea3SSunil Goutham 
5424863dea3SSunil Goutham 	if (bgx_reg_read(bgx, lmacid, BGX_SPUX_STATUS2) & SPU_STATUS2_RCVFLT) {
5434863dea3SSunil Goutham 		dev_err(&bgx->pdev->dev, "Receive fault\n");
5444863dea3SSunil Goutham 		return -1;
5454863dea3SSunil Goutham 	}
5464863dea3SSunil Goutham 
5474863dea3SSunil Goutham 	/* Receive link is latching low. Force it high and verify it */
5484863dea3SSunil Goutham 	bgx_reg_modify(bgx, lmacid, BGX_SPUX_STATUS1, SPU_STATUS1_RCV_LNK);
5494863dea3SSunil Goutham 	if (bgx_poll_reg(bgx, lmacid, BGX_SPUX_STATUS1,
5504863dea3SSunil Goutham 			 SPU_STATUS1_RCV_LNK, false)) {
5514863dea3SSunil Goutham 		dev_err(&bgx->pdev->dev, "SPU receive link down\n");
5524863dea3SSunil Goutham 		return -1;
5534863dea3SSunil Goutham 	}
5544863dea3SSunil Goutham 
5554863dea3SSunil Goutham 	cfg = bgx_reg_read(bgx, lmacid, BGX_SPUX_MISC_CONTROL);
5564863dea3SSunil Goutham 	cfg &= ~SPU_MISC_CTL_RX_DIS;
5574863dea3SSunil Goutham 	bgx_reg_write(bgx, lmacid, BGX_SPUX_MISC_CONTROL, cfg);
5584863dea3SSunil Goutham 	return 0;
5594863dea3SSunil Goutham }
5604863dea3SSunil Goutham 
5614863dea3SSunil Goutham static void bgx_poll_for_link(struct work_struct *work)
5624863dea3SSunil Goutham {
5634863dea3SSunil Goutham 	struct lmac *lmac;
5644863dea3SSunil Goutham 	u64 link;
5654863dea3SSunil Goutham 
5664863dea3SSunil Goutham 	lmac = container_of(work, struct lmac, dwork.work);
5674863dea3SSunil Goutham 
5684863dea3SSunil Goutham 	/* Receive link is latching low. Force it high and verify it */
5694863dea3SSunil Goutham 	bgx_reg_modify(lmac->bgx, lmac->lmacid,
5704863dea3SSunil Goutham 		       BGX_SPUX_STATUS1, SPU_STATUS1_RCV_LNK);
5714863dea3SSunil Goutham 	bgx_poll_reg(lmac->bgx, lmac->lmacid, BGX_SPUX_STATUS1,
5724863dea3SSunil Goutham 		     SPU_STATUS1_RCV_LNK, false);
5734863dea3SSunil Goutham 
5744863dea3SSunil Goutham 	link = bgx_reg_read(lmac->bgx, lmac->lmacid, BGX_SPUX_STATUS1);
5754863dea3SSunil Goutham 	if (link & SPU_STATUS1_RCV_LNK) {
5764863dea3SSunil Goutham 		lmac->link_up = 1;
5774863dea3SSunil Goutham 		if (lmac->bgx->lmac_type == BGX_MODE_XLAUI)
5784863dea3SSunil Goutham 			lmac->last_speed = 40000;
5794863dea3SSunil Goutham 		else
5804863dea3SSunil Goutham 			lmac->last_speed = 10000;
5814863dea3SSunil Goutham 		lmac->last_duplex = 1;
5824863dea3SSunil Goutham 	} else {
5834863dea3SSunil Goutham 		lmac->link_up = 0;
5844863dea3SSunil Goutham 	}
5854863dea3SSunil Goutham 
5864863dea3SSunil Goutham 	if (lmac->last_link != lmac->link_up) {
5874863dea3SSunil Goutham 		lmac->last_link = lmac->link_up;
5884863dea3SSunil Goutham 		if (lmac->link_up)
5894863dea3SSunil Goutham 			bgx_xaui_check_link(lmac);
5904863dea3SSunil Goutham 	}
5914863dea3SSunil Goutham 
5924863dea3SSunil Goutham 	queue_delayed_work(lmac->check_link, &lmac->dwork, HZ * 2);
5934863dea3SSunil Goutham }
5944863dea3SSunil Goutham 
5954863dea3SSunil Goutham static int bgx_lmac_enable(struct bgx *bgx, u8 lmacid)
5964863dea3SSunil Goutham {
5974863dea3SSunil Goutham 	struct lmac *lmac;
5984863dea3SSunil Goutham 	u64 cfg;
5994863dea3SSunil Goutham 
6004863dea3SSunil Goutham 	lmac = &bgx->lmac[lmacid];
6014863dea3SSunil Goutham 	lmac->bgx = bgx;
6024863dea3SSunil Goutham 
6034863dea3SSunil Goutham 	if (bgx->lmac_type == BGX_MODE_SGMII) {
6044863dea3SSunil Goutham 		lmac->is_sgmii = 1;
6054863dea3SSunil Goutham 		if (bgx_lmac_sgmii_init(bgx, lmacid))
6064863dea3SSunil Goutham 			return -1;
6074863dea3SSunil Goutham 	} else {
6084863dea3SSunil Goutham 		lmac->is_sgmii = 0;
6094863dea3SSunil Goutham 		if (bgx_lmac_xaui_init(bgx, lmacid, bgx->lmac_type))
6104863dea3SSunil Goutham 			return -1;
6114863dea3SSunil Goutham 	}
6124863dea3SSunil Goutham 
6134863dea3SSunil Goutham 	if (lmac->is_sgmii) {
6144863dea3SSunil Goutham 		cfg = bgx_reg_read(bgx, lmacid, BGX_GMP_GMI_TXX_APPEND);
6154863dea3SSunil Goutham 		cfg |= ((1ull << 2) | (1ull << 1)); /* FCS and PAD */
6164863dea3SSunil Goutham 		bgx_reg_modify(bgx, lmacid, BGX_GMP_GMI_TXX_APPEND, cfg);
6174863dea3SSunil Goutham 		bgx_reg_write(bgx, lmacid, BGX_GMP_GMI_TXX_MIN_PKT, 60 - 1);
6184863dea3SSunil Goutham 	} else {
6194863dea3SSunil Goutham 		cfg = bgx_reg_read(bgx, lmacid, BGX_SMUX_TX_APPEND);
6204863dea3SSunil Goutham 		cfg |= ((1ull << 2) | (1ull << 1)); /* FCS and PAD */
6214863dea3SSunil Goutham 		bgx_reg_modify(bgx, lmacid, BGX_SMUX_TX_APPEND, cfg);
6224863dea3SSunil Goutham 		bgx_reg_write(bgx, lmacid, BGX_SMUX_TX_MIN_PKT, 60 + 4);
6234863dea3SSunil Goutham 	}
6244863dea3SSunil Goutham 
6254863dea3SSunil Goutham 	/* Enable lmac */
6264863dea3SSunil Goutham 	bgx_reg_modify(bgx, lmacid, BGX_CMRX_CFG,
6274863dea3SSunil Goutham 		       CMR_EN | CMR_PKT_RX_EN | CMR_PKT_TX_EN);
6284863dea3SSunil Goutham 
6294863dea3SSunil Goutham 	/* Restore default cfg, incase low level firmware changed it */
6304863dea3SSunil Goutham 	bgx_reg_write(bgx, lmacid, BGX_CMRX_RX_DMAC_CTL, 0x03);
6314863dea3SSunil Goutham 
6324863dea3SSunil Goutham 	if ((bgx->lmac_type != BGX_MODE_XFI) &&
6334863dea3SSunil Goutham 	    (bgx->lmac_type != BGX_MODE_XLAUI) &&
6344863dea3SSunil Goutham 	    (bgx->lmac_type != BGX_MODE_40G_KR) &&
6354863dea3SSunil Goutham 	    (bgx->lmac_type != BGX_MODE_10G_KR)) {
6364863dea3SSunil Goutham 		if (!lmac->phydev)
6374863dea3SSunil Goutham 			return -ENODEV;
6384863dea3SSunil Goutham 
6394863dea3SSunil Goutham 		lmac->phydev->dev_flags = 0;
6404863dea3SSunil Goutham 
6414863dea3SSunil Goutham 		if (phy_connect_direct(&lmac->netdev, lmac->phydev,
6424863dea3SSunil Goutham 				       bgx_lmac_handler,
6434863dea3SSunil Goutham 				       PHY_INTERFACE_MODE_SGMII))
6444863dea3SSunil Goutham 			return -ENODEV;
6454863dea3SSunil Goutham 
6464863dea3SSunil Goutham 		phy_start_aneg(lmac->phydev);
6474863dea3SSunil Goutham 	} else {
6484863dea3SSunil Goutham 		lmac->check_link = alloc_workqueue("check_link", WQ_UNBOUND |
6494863dea3SSunil Goutham 						   WQ_MEM_RECLAIM, 1);
6504863dea3SSunil Goutham 		if (!lmac->check_link)
6514863dea3SSunil Goutham 			return -ENOMEM;
6524863dea3SSunil Goutham 		INIT_DELAYED_WORK(&lmac->dwork, bgx_poll_for_link);
6534863dea3SSunil Goutham 		queue_delayed_work(lmac->check_link, &lmac->dwork, 0);
6544863dea3SSunil Goutham 	}
6554863dea3SSunil Goutham 
6564863dea3SSunil Goutham 	return 0;
6574863dea3SSunil Goutham }
6584863dea3SSunil Goutham 
659fd7ec062SAleksey Makarov static void bgx_lmac_disable(struct bgx *bgx, u8 lmacid)
6604863dea3SSunil Goutham {
6614863dea3SSunil Goutham 	struct lmac *lmac;
6624863dea3SSunil Goutham 	u64 cmrx_cfg;
6634863dea3SSunil Goutham 
6644863dea3SSunil Goutham 	lmac = &bgx->lmac[lmacid];
6654863dea3SSunil Goutham 	if (lmac->check_link) {
6664863dea3SSunil Goutham 		/* Destroy work queue */
6674863dea3SSunil Goutham 		cancel_delayed_work(&lmac->dwork);
6684863dea3SSunil Goutham 		flush_workqueue(lmac->check_link);
6694863dea3SSunil Goutham 		destroy_workqueue(lmac->check_link);
6704863dea3SSunil Goutham 	}
6714863dea3SSunil Goutham 
6724863dea3SSunil Goutham 	cmrx_cfg = bgx_reg_read(bgx, lmacid, BGX_CMRX_CFG);
6734863dea3SSunil Goutham 	cmrx_cfg &= ~(1 << 15);
6744863dea3SSunil Goutham 	bgx_reg_write(bgx, lmacid, BGX_CMRX_CFG, cmrx_cfg);
6754863dea3SSunil Goutham 	bgx_flush_dmac_addrs(bgx, lmacid);
6764863dea3SSunil Goutham 
67760f83c89SThanneeru Srinivasulu 	if ((bgx->lmac_type != BGX_MODE_XFI) &&
67860f83c89SThanneeru Srinivasulu 	    (bgx->lmac_type != BGX_MODE_XLAUI) &&
67960f83c89SThanneeru Srinivasulu 	    (bgx->lmac_type != BGX_MODE_40G_KR) &&
68060f83c89SThanneeru Srinivasulu 	    (bgx->lmac_type != BGX_MODE_10G_KR) && lmac->phydev)
6814863dea3SSunil Goutham 		phy_disconnect(lmac->phydev);
6824863dea3SSunil Goutham 
6834863dea3SSunil Goutham 	lmac->phydev = NULL;
6844863dea3SSunil Goutham }
6854863dea3SSunil Goutham 
6864863dea3SSunil Goutham static void bgx_set_num_ports(struct bgx *bgx)
6874863dea3SSunil Goutham {
6884863dea3SSunil Goutham 	u64 lmac_count;
6894863dea3SSunil Goutham 
6904863dea3SSunil Goutham 	switch (bgx->qlm_mode) {
6914863dea3SSunil Goutham 	case QLM_MODE_SGMII:
6924863dea3SSunil Goutham 		bgx->lmac_count = 4;
6934863dea3SSunil Goutham 		bgx->lmac_type = BGX_MODE_SGMII;
6944863dea3SSunil Goutham 		bgx->lane_to_sds = 0;
6954863dea3SSunil Goutham 		break;
6964863dea3SSunil Goutham 	case QLM_MODE_XAUI_1X4:
6974863dea3SSunil Goutham 		bgx->lmac_count = 1;
6984863dea3SSunil Goutham 		bgx->lmac_type = BGX_MODE_XAUI;
6994863dea3SSunil Goutham 		bgx->lane_to_sds = 0xE4;
7004863dea3SSunil Goutham 			break;
7014863dea3SSunil Goutham 	case QLM_MODE_RXAUI_2X2:
7024863dea3SSunil Goutham 		bgx->lmac_count = 2;
7034863dea3SSunil Goutham 		bgx->lmac_type = BGX_MODE_RXAUI;
7044863dea3SSunil Goutham 		bgx->lane_to_sds = 0xE4;
7054863dea3SSunil Goutham 			break;
7064863dea3SSunil Goutham 	case QLM_MODE_XFI_4X1:
7074863dea3SSunil Goutham 		bgx->lmac_count = 4;
7084863dea3SSunil Goutham 		bgx->lmac_type = BGX_MODE_XFI;
7094863dea3SSunil Goutham 		bgx->lane_to_sds = 0;
7104863dea3SSunil Goutham 		break;
7114863dea3SSunil Goutham 	case QLM_MODE_XLAUI_1X4:
7124863dea3SSunil Goutham 		bgx->lmac_count = 1;
7134863dea3SSunil Goutham 		bgx->lmac_type = BGX_MODE_XLAUI;
7144863dea3SSunil Goutham 		bgx->lane_to_sds = 0xE4;
7154863dea3SSunil Goutham 		break;
7164863dea3SSunil Goutham 	case QLM_MODE_10G_KR_4X1:
7174863dea3SSunil Goutham 		bgx->lmac_count = 4;
7184863dea3SSunil Goutham 		bgx->lmac_type = BGX_MODE_10G_KR;
7194863dea3SSunil Goutham 		bgx->lane_to_sds = 0;
7204863dea3SSunil Goutham 		bgx->use_training = 1;
7214863dea3SSunil Goutham 		break;
7224863dea3SSunil Goutham 	case QLM_MODE_40G_KR4_1X4:
7234863dea3SSunil Goutham 		bgx->lmac_count = 1;
7244863dea3SSunil Goutham 		bgx->lmac_type = BGX_MODE_40G_KR;
7254863dea3SSunil Goutham 		bgx->lane_to_sds = 0xE4;
7264863dea3SSunil Goutham 		bgx->use_training = 1;
7274863dea3SSunil Goutham 		break;
7284863dea3SSunil Goutham 	default:
7294863dea3SSunil Goutham 		bgx->lmac_count = 0;
7304863dea3SSunil Goutham 		break;
7314863dea3SSunil Goutham 	}
7324863dea3SSunil Goutham 
7334863dea3SSunil Goutham 	/* Check if low level firmware has programmed LMAC count
7344863dea3SSunil Goutham 	 * based on board type, if yes consider that otherwise
7354863dea3SSunil Goutham 	 * the default static values
7364863dea3SSunil Goutham 	 */
7374863dea3SSunil Goutham 	lmac_count = bgx_reg_read(bgx, 0, BGX_CMR_RX_LMACS) & 0x7;
7384863dea3SSunil Goutham 	if (lmac_count != 4)
7394863dea3SSunil Goutham 		bgx->lmac_count = lmac_count;
7404863dea3SSunil Goutham }
7414863dea3SSunil Goutham 
7424863dea3SSunil Goutham static void bgx_init_hw(struct bgx *bgx)
7434863dea3SSunil Goutham {
7444863dea3SSunil Goutham 	int i;
7454863dea3SSunil Goutham 
7464863dea3SSunil Goutham 	bgx_set_num_ports(bgx);
7474863dea3SSunil Goutham 
7484863dea3SSunil Goutham 	bgx_reg_modify(bgx, 0, BGX_CMR_GLOBAL_CFG, CMR_GLOBAL_CFG_FCS_STRIP);
7494863dea3SSunil Goutham 	if (bgx_reg_read(bgx, 0, BGX_CMR_BIST_STATUS))
7504863dea3SSunil Goutham 		dev_err(&bgx->pdev->dev, "BGX%d BIST failed\n", bgx->bgx_id);
7514863dea3SSunil Goutham 
7524863dea3SSunil Goutham 	/* Set lmac type and lane2serdes mapping */
7534863dea3SSunil Goutham 	for (i = 0; i < bgx->lmac_count; i++) {
7544863dea3SSunil Goutham 		if (bgx->lmac_type == BGX_MODE_RXAUI) {
7554863dea3SSunil Goutham 			if (i)
7564863dea3SSunil Goutham 				bgx->lane_to_sds = 0x0e;
7574863dea3SSunil Goutham 			else
7584863dea3SSunil Goutham 				bgx->lane_to_sds = 0x04;
7594863dea3SSunil Goutham 			bgx_reg_write(bgx, i, BGX_CMRX_CFG,
7604863dea3SSunil Goutham 				      (bgx->lmac_type << 8) | bgx->lane_to_sds);
7614863dea3SSunil Goutham 			continue;
7624863dea3SSunil Goutham 		}
7634863dea3SSunil Goutham 		bgx_reg_write(bgx, i, BGX_CMRX_CFG,
7644863dea3SSunil Goutham 			      (bgx->lmac_type << 8) | (bgx->lane_to_sds + i));
7654863dea3SSunil Goutham 		bgx->lmac[i].lmacid_bd = lmac_count;
7664863dea3SSunil Goutham 		lmac_count++;
7674863dea3SSunil Goutham 	}
7684863dea3SSunil Goutham 
7694863dea3SSunil Goutham 	bgx_reg_write(bgx, 0, BGX_CMR_TX_LMACS, bgx->lmac_count);
7704863dea3SSunil Goutham 	bgx_reg_write(bgx, 0, BGX_CMR_RX_LMACS, bgx->lmac_count);
7714863dea3SSunil Goutham 
7724863dea3SSunil Goutham 	/* Set the backpressure AND mask */
7734863dea3SSunil Goutham 	for (i = 0; i < bgx->lmac_count; i++)
7744863dea3SSunil Goutham 		bgx_reg_modify(bgx, 0, BGX_CMR_CHAN_MSK_AND,
7754863dea3SSunil Goutham 			       ((1ULL << MAX_BGX_CHANS_PER_LMAC) - 1) <<
7764863dea3SSunil Goutham 			       (i * MAX_BGX_CHANS_PER_LMAC));
7774863dea3SSunil Goutham 
7784863dea3SSunil Goutham 	/* Disable all MAC filtering */
7794863dea3SSunil Goutham 	for (i = 0; i < RX_DMAC_COUNT; i++)
7804863dea3SSunil Goutham 		bgx_reg_write(bgx, 0, BGX_CMR_RX_DMACX_CAM + (i * 8), 0x00);
7814863dea3SSunil Goutham 
7824863dea3SSunil Goutham 	/* Disable MAC steering (NCSI traffic) */
7834863dea3SSunil Goutham 	for (i = 0; i < RX_TRAFFIC_STEER_RULE_COUNT; i++)
7844863dea3SSunil Goutham 		bgx_reg_write(bgx, 0, BGX_CMR_RX_STREERING + (i * 8), 0x00);
7854863dea3SSunil Goutham }
7864863dea3SSunil Goutham 
7874863dea3SSunil Goutham static void bgx_get_qlm_mode(struct bgx *bgx)
7884863dea3SSunil Goutham {
7894863dea3SSunil Goutham 	struct device *dev = &bgx->pdev->dev;
7904863dea3SSunil Goutham 	int lmac_type;
7914863dea3SSunil Goutham 	int train_en;
7924863dea3SSunil Goutham 
7934863dea3SSunil Goutham 	/* Read LMAC0 type to figure out QLM mode
7944863dea3SSunil Goutham 	 * This is configured by low level firmware
7954863dea3SSunil Goutham 	 */
7964863dea3SSunil Goutham 	lmac_type = bgx_reg_read(bgx, 0, BGX_CMRX_CFG);
7974863dea3SSunil Goutham 	lmac_type = (lmac_type >> 8) & 0x07;
7984863dea3SSunil Goutham 
7994863dea3SSunil Goutham 	train_en = bgx_reg_read(bgx, 0, BGX_SPUX_BR_PMD_CRTL) &
8004863dea3SSunil Goutham 				SPU_PMD_CRTL_TRAIN_EN;
8014863dea3SSunil Goutham 
8024863dea3SSunil Goutham 	switch (lmac_type) {
8034863dea3SSunil Goutham 	case BGX_MODE_SGMII:
8044863dea3SSunil Goutham 		bgx->qlm_mode = QLM_MODE_SGMII;
8054863dea3SSunil Goutham 		dev_info(dev, "BGX%d QLM mode: SGMII\n", bgx->bgx_id);
8064863dea3SSunil Goutham 		break;
8074863dea3SSunil Goutham 	case BGX_MODE_XAUI:
8084863dea3SSunil Goutham 		bgx->qlm_mode = QLM_MODE_XAUI_1X4;
8094863dea3SSunil Goutham 		dev_info(dev, "BGX%d QLM mode: XAUI\n", bgx->bgx_id);
8104863dea3SSunil Goutham 		break;
8114863dea3SSunil Goutham 	case BGX_MODE_RXAUI:
8124863dea3SSunil Goutham 		bgx->qlm_mode = QLM_MODE_RXAUI_2X2;
8134863dea3SSunil Goutham 		dev_info(dev, "BGX%d QLM mode: RXAUI\n", bgx->bgx_id);
8144863dea3SSunil Goutham 		break;
8154863dea3SSunil Goutham 	case BGX_MODE_XFI:
8164863dea3SSunil Goutham 		if (!train_en) {
8174863dea3SSunil Goutham 			bgx->qlm_mode = QLM_MODE_XFI_4X1;
8184863dea3SSunil Goutham 			dev_info(dev, "BGX%d QLM mode: XFI\n", bgx->bgx_id);
8194863dea3SSunil Goutham 		} else {
8204863dea3SSunil Goutham 			bgx->qlm_mode = QLM_MODE_10G_KR_4X1;
8214863dea3SSunil Goutham 			dev_info(dev, "BGX%d QLM mode: 10G_KR\n", bgx->bgx_id);
8224863dea3SSunil Goutham 		}
8234863dea3SSunil Goutham 		break;
8244863dea3SSunil Goutham 	case BGX_MODE_XLAUI:
8254863dea3SSunil Goutham 		if (!train_en) {
8264863dea3SSunil Goutham 			bgx->qlm_mode = QLM_MODE_XLAUI_1X4;
8274863dea3SSunil Goutham 			dev_info(dev, "BGX%d QLM mode: XLAUI\n", bgx->bgx_id);
8284863dea3SSunil Goutham 		} else {
8294863dea3SSunil Goutham 			bgx->qlm_mode = QLM_MODE_40G_KR4_1X4;
8304863dea3SSunil Goutham 			dev_info(dev, "BGX%d QLM mode: 40G_KR4\n", bgx->bgx_id);
8314863dea3SSunil Goutham 		}
8324863dea3SSunil Goutham 		break;
8334863dea3SSunil Goutham 	default:
8344863dea3SSunil Goutham 		bgx->qlm_mode = QLM_MODE_SGMII;
8354863dea3SSunil Goutham 		dev_info(dev, "BGX%d QLM default mode: SGMII\n", bgx->bgx_id);
8364863dea3SSunil Goutham 	}
8374863dea3SSunil Goutham }
8384863dea3SSunil Goutham 
83946b903a0SDavid Daney #ifdef CONFIG_ACPI
84046b903a0SDavid Daney 
84146b903a0SDavid Daney static int acpi_get_mac_address(struct acpi_device *adev, u8 *dst)
84246b903a0SDavid Daney {
84346b903a0SDavid Daney 	u8 mac[ETH_ALEN];
84446b903a0SDavid Daney 	int ret;
84546b903a0SDavid Daney 
84646b903a0SDavid Daney 	ret = fwnode_property_read_u8_array(acpi_fwnode_handle(adev),
84746b903a0SDavid Daney 					    "mac-address", mac, ETH_ALEN);
84846b903a0SDavid Daney 	if (ret)
84946b903a0SDavid Daney 		goto out;
85046b903a0SDavid Daney 
85146b903a0SDavid Daney 	if (!is_valid_ether_addr(mac)) {
85246b903a0SDavid Daney 		ret = -EINVAL;
85346b903a0SDavid Daney 		goto out;
85446b903a0SDavid Daney 	}
85546b903a0SDavid Daney 
85646b903a0SDavid Daney 	memcpy(dst, mac, ETH_ALEN);
85746b903a0SDavid Daney out:
85846b903a0SDavid Daney 	return ret;
85946b903a0SDavid Daney }
86046b903a0SDavid Daney 
86146b903a0SDavid Daney /* Currently only sets the MAC address. */
86246b903a0SDavid Daney static acpi_status bgx_acpi_register_phy(acpi_handle handle,
86346b903a0SDavid Daney 					 u32 lvl, void *context, void **rv)
86446b903a0SDavid Daney {
86546b903a0SDavid Daney 	struct bgx *bgx = context;
86646b903a0SDavid Daney 	struct acpi_device *adev;
86746b903a0SDavid Daney 
86846b903a0SDavid Daney 	if (acpi_bus_get_device(handle, &adev))
86946b903a0SDavid Daney 		goto out;
87046b903a0SDavid Daney 
87146b903a0SDavid Daney 	acpi_get_mac_address(adev, bgx->lmac[bgx->lmac_count].mac);
87246b903a0SDavid Daney 
87346b903a0SDavid Daney 	SET_NETDEV_DEV(&bgx->lmac[bgx->lmac_count].netdev, &bgx->pdev->dev);
87446b903a0SDavid Daney 
87546b903a0SDavid Daney 	bgx->lmac[bgx->lmac_count].lmacid = bgx->lmac_count;
87646b903a0SDavid Daney out:
87746b903a0SDavid Daney 	bgx->lmac_count++;
87846b903a0SDavid Daney 	return AE_OK;
87946b903a0SDavid Daney }
88046b903a0SDavid Daney 
88146b903a0SDavid Daney static acpi_status bgx_acpi_match_id(acpi_handle handle, u32 lvl,
88246b903a0SDavid Daney 				     void *context, void **ret_val)
88346b903a0SDavid Daney {
88446b903a0SDavid Daney 	struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
88546b903a0SDavid Daney 	struct bgx *bgx = context;
88646b903a0SDavid Daney 	char bgx_sel[5];
88746b903a0SDavid Daney 
88846b903a0SDavid Daney 	snprintf(bgx_sel, 5, "BGX%d", bgx->bgx_id);
88946b903a0SDavid Daney 	if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &string))) {
89046b903a0SDavid Daney 		pr_warn("Invalid link device\n");
89146b903a0SDavid Daney 		return AE_OK;
89246b903a0SDavid Daney 	}
89346b903a0SDavid Daney 
89446b903a0SDavid Daney 	if (strncmp(string.pointer, bgx_sel, 4))
89546b903a0SDavid Daney 		return AE_OK;
89646b903a0SDavid Daney 
89746b903a0SDavid Daney 	acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
89846b903a0SDavid Daney 			    bgx_acpi_register_phy, NULL, bgx, NULL);
89946b903a0SDavid Daney 
90046b903a0SDavid Daney 	kfree(string.pointer);
90146b903a0SDavid Daney 	return AE_CTRL_TERMINATE;
90246b903a0SDavid Daney }
90346b903a0SDavid Daney 
90446b903a0SDavid Daney static int bgx_init_acpi_phy(struct bgx *bgx)
90546b903a0SDavid Daney {
90646b903a0SDavid Daney 	acpi_get_devices(NULL, bgx_acpi_match_id, bgx, (void **)NULL);
90746b903a0SDavid Daney 	return 0;
90846b903a0SDavid Daney }
90946b903a0SDavid Daney 
91046b903a0SDavid Daney #else
91146b903a0SDavid Daney 
91246b903a0SDavid Daney static int bgx_init_acpi_phy(struct bgx *bgx)
91346b903a0SDavid Daney {
91446b903a0SDavid Daney 	return -ENODEV;
91546b903a0SDavid Daney }
91646b903a0SDavid Daney 
91746b903a0SDavid Daney #endif /* CONFIG_ACPI */
91846b903a0SDavid Daney 
919de387e11SRobert Richter #if IS_ENABLED(CONFIG_OF_MDIO)
920de387e11SRobert Richter 
921de387e11SRobert Richter static int bgx_init_of_phy(struct bgx *bgx)
9224863dea3SSunil Goutham {
923de387e11SRobert Richter 	struct device_node *np;
9244863dea3SSunil Goutham 	struct device_node *np_child;
9254863dea3SSunil Goutham 	u8 lmac = 0;
926de387e11SRobert Richter 	char bgx_sel[5];
9274863dea3SSunil Goutham 	const char *mac;
9284863dea3SSunil Goutham 
929de387e11SRobert Richter 	/* Get BGX node from DT */
930de387e11SRobert Richter 	snprintf(bgx_sel, 5, "bgx%d", bgx->bgx_id);
931de387e11SRobert Richter 	np = of_find_node_by_name(NULL, bgx_sel);
932de387e11SRobert Richter 	if (!np)
933de387e11SRobert Richter 		return -ENODEV;
934de387e11SRobert Richter 
935de387e11SRobert Richter 	for_each_child_of_node(np, np_child) {
936de387e11SRobert Richter 		struct device_node *phy_np = of_parse_phandle(np_child,
937de387e11SRobert Richter 							      "phy-handle", 0);
938de387e11SRobert Richter 		if (!phy_np)
939de387e11SRobert Richter 			continue;
9404863dea3SSunil Goutham 		bgx->lmac[lmac].phydev = of_phy_find_device(phy_np);
9414863dea3SSunil Goutham 
9424863dea3SSunil Goutham 		mac = of_get_mac_address(np_child);
9434863dea3SSunil Goutham 		if (mac)
9444863dea3SSunil Goutham 			ether_addr_copy(bgx->lmac[lmac].mac, mac);
9454863dea3SSunil Goutham 
9464863dea3SSunil Goutham 		SET_NETDEV_DEV(&bgx->lmac[lmac].netdev, &bgx->pdev->dev);
9474863dea3SSunil Goutham 		bgx->lmac[lmac].lmacid = lmac;
9484863dea3SSunil Goutham 		lmac++;
9494863dea3SSunil Goutham 		if (lmac == MAX_LMAC_PER_BGX)
9504863dea3SSunil Goutham 			break;
9514863dea3SSunil Goutham 	}
952de387e11SRobert Richter 	return 0;
953de387e11SRobert Richter }
954de387e11SRobert Richter 
955de387e11SRobert Richter #else
956de387e11SRobert Richter 
957de387e11SRobert Richter static int bgx_init_of_phy(struct bgx *bgx)
958de387e11SRobert Richter {
959de387e11SRobert Richter 	return -ENODEV;
960de387e11SRobert Richter }
961de387e11SRobert Richter 
962de387e11SRobert Richter #endif /* CONFIG_OF_MDIO */
963de387e11SRobert Richter 
964de387e11SRobert Richter static int bgx_init_phy(struct bgx *bgx)
965de387e11SRobert Richter {
96646b903a0SDavid Daney 	if (!acpi_disabled)
96746b903a0SDavid Daney 		return bgx_init_acpi_phy(bgx);
96846b903a0SDavid Daney 
969de387e11SRobert Richter 	return bgx_init_of_phy(bgx);
9704863dea3SSunil Goutham }
9714863dea3SSunil Goutham 
9724863dea3SSunil Goutham static int bgx_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
9734863dea3SSunil Goutham {
9744863dea3SSunil Goutham 	int err;
9754863dea3SSunil Goutham 	struct device *dev = &pdev->dev;
9764863dea3SSunil Goutham 	struct bgx *bgx = NULL;
9774863dea3SSunil Goutham 	u8 lmac;
9784863dea3SSunil Goutham 
9794863dea3SSunil Goutham 	bgx = devm_kzalloc(dev, sizeof(*bgx), GFP_KERNEL);
9804863dea3SSunil Goutham 	if (!bgx)
9814863dea3SSunil Goutham 		return -ENOMEM;
9824863dea3SSunil Goutham 	bgx->pdev = pdev;
9834863dea3SSunil Goutham 
9844863dea3SSunil Goutham 	pci_set_drvdata(pdev, bgx);
9854863dea3SSunil Goutham 
9864863dea3SSunil Goutham 	err = pci_enable_device(pdev);
9874863dea3SSunil Goutham 	if (err) {
9884863dea3SSunil Goutham 		dev_err(dev, "Failed to enable PCI device\n");
9894863dea3SSunil Goutham 		pci_set_drvdata(pdev, NULL);
9904863dea3SSunil Goutham 		return err;
9914863dea3SSunil Goutham 	}
9924863dea3SSunil Goutham 
9934863dea3SSunil Goutham 	err = pci_request_regions(pdev, DRV_NAME);
9944863dea3SSunil Goutham 	if (err) {
9954863dea3SSunil Goutham 		dev_err(dev, "PCI request regions failed 0x%x\n", err);
9964863dea3SSunil Goutham 		goto err_disable_device;
9974863dea3SSunil Goutham 	}
9984863dea3SSunil Goutham 
9994863dea3SSunil Goutham 	/* MAP configuration registers */
10004863dea3SSunil Goutham 	bgx->reg_base = pcim_iomap(pdev, PCI_CFG_REG_BAR_NUM, 0);
10014863dea3SSunil Goutham 	if (!bgx->reg_base) {
10024863dea3SSunil Goutham 		dev_err(dev, "BGX: Cannot map CSR memory space, aborting\n");
10034863dea3SSunil Goutham 		err = -ENOMEM;
10044863dea3SSunil Goutham 		goto err_release_regions;
10054863dea3SSunil Goutham 	}
10064863dea3SSunil Goutham 	bgx->bgx_id = (pci_resource_start(pdev, PCI_CFG_REG_BAR_NUM) >> 24) & 1;
1007d768b678SRobert Richter 	bgx->bgx_id += nic_get_node_id(pdev) * MAX_BGX_PER_CN88XX;
1008d768b678SRobert Richter 
10094863dea3SSunil Goutham 	bgx_vnic[bgx->bgx_id] = bgx;
10104863dea3SSunil Goutham 	bgx_get_qlm_mode(bgx);
10114863dea3SSunil Goutham 
1012de387e11SRobert Richter 	err = bgx_init_phy(bgx);
1013de387e11SRobert Richter 	if (err)
1014de387e11SRobert Richter 		goto err_enable;
10154863dea3SSunil Goutham 
10164863dea3SSunil Goutham 	bgx_init_hw(bgx);
10174863dea3SSunil Goutham 
10184863dea3SSunil Goutham 	/* Enable all LMACs */
10194863dea3SSunil Goutham 	for (lmac = 0; lmac < bgx->lmac_count; lmac++) {
10204863dea3SSunil Goutham 		err = bgx_lmac_enable(bgx, lmac);
10214863dea3SSunil Goutham 		if (err) {
10224863dea3SSunil Goutham 			dev_err(dev, "BGX%d failed to enable lmac%d\n",
10234863dea3SSunil Goutham 				bgx->bgx_id, lmac);
10244863dea3SSunil Goutham 			goto err_enable;
10254863dea3SSunil Goutham 		}
10264863dea3SSunil Goutham 	}
10274863dea3SSunil Goutham 
10284863dea3SSunil Goutham 	return 0;
10294863dea3SSunil Goutham 
10304863dea3SSunil Goutham err_enable:
10314863dea3SSunil Goutham 	bgx_vnic[bgx->bgx_id] = NULL;
10324863dea3SSunil Goutham err_release_regions:
10334863dea3SSunil Goutham 	pci_release_regions(pdev);
10344863dea3SSunil Goutham err_disable_device:
10354863dea3SSunil Goutham 	pci_disable_device(pdev);
10364863dea3SSunil Goutham 	pci_set_drvdata(pdev, NULL);
10374863dea3SSunil Goutham 	return err;
10384863dea3SSunil Goutham }
10394863dea3SSunil Goutham 
10404863dea3SSunil Goutham static void bgx_remove(struct pci_dev *pdev)
10414863dea3SSunil Goutham {
10424863dea3SSunil Goutham 	struct bgx *bgx = pci_get_drvdata(pdev);
10434863dea3SSunil Goutham 	u8 lmac;
10444863dea3SSunil Goutham 
10454863dea3SSunil Goutham 	/* Disable all LMACs */
10464863dea3SSunil Goutham 	for (lmac = 0; lmac < bgx->lmac_count; lmac++)
10474863dea3SSunil Goutham 		bgx_lmac_disable(bgx, lmac);
10484863dea3SSunil Goutham 
10494863dea3SSunil Goutham 	bgx_vnic[bgx->bgx_id] = NULL;
10504863dea3SSunil Goutham 	pci_release_regions(pdev);
10514863dea3SSunil Goutham 	pci_disable_device(pdev);
10524863dea3SSunil Goutham 	pci_set_drvdata(pdev, NULL);
10534863dea3SSunil Goutham }
10544863dea3SSunil Goutham 
10554863dea3SSunil Goutham static struct pci_driver bgx_driver = {
10564863dea3SSunil Goutham 	.name = DRV_NAME,
10574863dea3SSunil Goutham 	.id_table = bgx_id_table,
10584863dea3SSunil Goutham 	.probe = bgx_probe,
10594863dea3SSunil Goutham 	.remove = bgx_remove,
10604863dea3SSunil Goutham };
10614863dea3SSunil Goutham 
10624863dea3SSunil Goutham static int __init bgx_init_module(void)
10634863dea3SSunil Goutham {
10644863dea3SSunil Goutham 	pr_info("%s, ver %s\n", DRV_NAME, DRV_VERSION);
10654863dea3SSunil Goutham 
10664863dea3SSunil Goutham 	return pci_register_driver(&bgx_driver);
10674863dea3SSunil Goutham }
10684863dea3SSunil Goutham 
10694863dea3SSunil Goutham static void __exit bgx_cleanup_module(void)
10704863dea3SSunil Goutham {
10714863dea3SSunil Goutham 	pci_unregister_driver(&bgx_driver);
10724863dea3SSunil Goutham }
10734863dea3SSunil Goutham 
10744863dea3SSunil Goutham module_init(bgx_init_module);
10754863dea3SSunil Goutham module_exit(bgx_cleanup_module);
1076