18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * FCC driver for Motorola MPC82xx (PQ2).
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (c) 2003 Intracom S.A.
58c2ecf20Sopenharmony_ci *  by Pantelis Antoniou <panto@intracom.gr>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * 2005 (c) MontaVista Software, Inc.
88c2ecf20Sopenharmony_ci * Vitaly Bordug <vbordug@ru.mvista.com>
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * This file is licensed under the terms of the GNU General Public License
118c2ecf20Sopenharmony_ci * version 2. This program is licensed "as is" without any warranty of any
128c2ecf20Sopenharmony_ci * kind, whether express or implied.
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <linux/module.h>
168c2ecf20Sopenharmony_ci#include <linux/kernel.h>
178c2ecf20Sopenharmony_ci#include <linux/types.h>
188c2ecf20Sopenharmony_ci#include <linux/string.h>
198c2ecf20Sopenharmony_ci#include <linux/ptrace.h>
208c2ecf20Sopenharmony_ci#include <linux/errno.h>
218c2ecf20Sopenharmony_ci#include <linux/ioport.h>
228c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
238c2ecf20Sopenharmony_ci#include <linux/delay.h>
248c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
258c2ecf20Sopenharmony_ci#include <linux/etherdevice.h>
268c2ecf20Sopenharmony_ci#include <linux/skbuff.h>
278c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
288c2ecf20Sopenharmony_ci#include <linux/mii.h>
298c2ecf20Sopenharmony_ci#include <linux/ethtool.h>
308c2ecf20Sopenharmony_ci#include <linux/bitops.h>
318c2ecf20Sopenharmony_ci#include <linux/fs.h>
328c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
338c2ecf20Sopenharmony_ci#include <linux/phy.h>
348c2ecf20Sopenharmony_ci#include <linux/of_address.h>
358c2ecf20Sopenharmony_ci#include <linux/of_device.h>
368c2ecf20Sopenharmony_ci#include <linux/of_irq.h>
378c2ecf20Sopenharmony_ci#include <linux/gfp.h>
388c2ecf20Sopenharmony_ci#include <linux/pgtable.h>
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#include <asm/immap_cpm2.h>
418c2ecf20Sopenharmony_ci#include <asm/mpc8260.h>
428c2ecf20Sopenharmony_ci#include <asm/cpm2.h>
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#include <asm/irq.h>
458c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci#include "fs_enet.h"
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci/*************************************************/
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci/* FCC access macros */
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci/* write, read, set bits, clear bits */
548c2ecf20Sopenharmony_ci#define W32(_p, _m, _v)	out_be32(&(_p)->_m, (_v))
558c2ecf20Sopenharmony_ci#define R32(_p, _m)	in_be32(&(_p)->_m)
568c2ecf20Sopenharmony_ci#define S32(_p, _m, _v)	W32(_p, _m, R32(_p, _m) | (_v))
578c2ecf20Sopenharmony_ci#define C32(_p, _m, _v)	W32(_p, _m, R32(_p, _m) & ~(_v))
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci#define W16(_p, _m, _v)	out_be16(&(_p)->_m, (_v))
608c2ecf20Sopenharmony_ci#define R16(_p, _m)	in_be16(&(_p)->_m)
618c2ecf20Sopenharmony_ci#define S16(_p, _m, _v)	W16(_p, _m, R16(_p, _m) | (_v))
628c2ecf20Sopenharmony_ci#define C16(_p, _m, _v)	W16(_p, _m, R16(_p, _m) & ~(_v))
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci#define W8(_p, _m, _v)	out_8(&(_p)->_m, (_v))
658c2ecf20Sopenharmony_ci#define R8(_p, _m)	in_8(&(_p)->_m)
668c2ecf20Sopenharmony_ci#define S8(_p, _m, _v)	W8(_p, _m, R8(_p, _m) | (_v))
678c2ecf20Sopenharmony_ci#define C8(_p, _m, _v)	W8(_p, _m, R8(_p, _m) & ~(_v))
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci/*************************************************/
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci#define FCC_MAX_MULTICAST_ADDRS	64
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci#define mk_mii_read(REG)	(0x60020000 | ((REG & 0x1f) << 18))
748c2ecf20Sopenharmony_ci#define mk_mii_write(REG, VAL)	(0x50020000 | ((REG & 0x1f) << 18) | (VAL & 0xffff))
758c2ecf20Sopenharmony_ci#define mk_mii_end		0
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci#define MAX_CR_CMD_LOOPS	10000
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cistatic inline int fcc_cr_cmd(struct fs_enet_private *fep, u32 op)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	const struct fs_platform_info *fpi = fep->fpi;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	return cpm_command(fpi->cp_command, op);
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistatic int do_pd_setup(struct fs_enet_private *fep)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	struct platform_device *ofdev = to_platform_device(fep->dev);
898c2ecf20Sopenharmony_ci	struct fs_platform_info *fpi = fep->fpi;
908c2ecf20Sopenharmony_ci	int ret = -EINVAL;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	fep->interrupt = irq_of_parse_and_map(ofdev->dev.of_node, 0);
938c2ecf20Sopenharmony_ci	if (!fep->interrupt)
948c2ecf20Sopenharmony_ci		goto out;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	fep->fcc.fccp = of_iomap(ofdev->dev.of_node, 0);
978c2ecf20Sopenharmony_ci	if (!fep->fcc.fccp)
988c2ecf20Sopenharmony_ci		goto out;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	fep->fcc.ep = of_iomap(ofdev->dev.of_node, 1);
1018c2ecf20Sopenharmony_ci	if (!fep->fcc.ep)
1028c2ecf20Sopenharmony_ci		goto out_fccp;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	fep->fcc.fcccp = of_iomap(ofdev->dev.of_node, 2);
1058c2ecf20Sopenharmony_ci	if (!fep->fcc.fcccp)
1068c2ecf20Sopenharmony_ci		goto out_ep;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	fep->fcc.mem = (void __iomem *)cpm2_immr;
1098c2ecf20Sopenharmony_ci	fpi->dpram_offset = cpm_dpalloc(128, 32);
1108c2ecf20Sopenharmony_ci	if (IS_ERR_VALUE(fpi->dpram_offset)) {
1118c2ecf20Sopenharmony_ci		ret = fpi->dpram_offset;
1128c2ecf20Sopenharmony_ci		goto out_fcccp;
1138c2ecf20Sopenharmony_ci	}
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	return 0;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ciout_fcccp:
1188c2ecf20Sopenharmony_ci	iounmap(fep->fcc.fcccp);
1198c2ecf20Sopenharmony_ciout_ep:
1208c2ecf20Sopenharmony_ci	iounmap(fep->fcc.ep);
1218c2ecf20Sopenharmony_ciout_fccp:
1228c2ecf20Sopenharmony_ci	iounmap(fep->fcc.fccp);
1238c2ecf20Sopenharmony_ciout:
1248c2ecf20Sopenharmony_ci	return ret;
1258c2ecf20Sopenharmony_ci}
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci#define FCC_NAPI_EVENT_MSK	(FCC_ENET_RXF | FCC_ENET_RXB | FCC_ENET_TXB)
1288c2ecf20Sopenharmony_ci#define FCC_EVENT		(FCC_ENET_RXF | FCC_ENET_TXB)
1298c2ecf20Sopenharmony_ci#define FCC_ERR_EVENT_MSK	(FCC_ENET_TXE)
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_cistatic int setup_data(struct net_device *dev)
1328c2ecf20Sopenharmony_ci{
1338c2ecf20Sopenharmony_ci	struct fs_enet_private *fep = netdev_priv(dev);
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	if (do_pd_setup(fep) != 0)
1368c2ecf20Sopenharmony_ci		return -EINVAL;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	fep->ev_napi = FCC_NAPI_EVENT_MSK;
1398c2ecf20Sopenharmony_ci	fep->ev = FCC_EVENT;
1408c2ecf20Sopenharmony_ci	fep->ev_err = FCC_ERR_EVENT_MSK;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	return 0;
1438c2ecf20Sopenharmony_ci}
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_cistatic int allocate_bd(struct net_device *dev)
1468c2ecf20Sopenharmony_ci{
1478c2ecf20Sopenharmony_ci	struct fs_enet_private *fep = netdev_priv(dev);
1488c2ecf20Sopenharmony_ci	const struct fs_platform_info *fpi = fep->fpi;
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	fep->ring_base = (void __iomem __force *)dma_alloc_coherent(fep->dev,
1518c2ecf20Sopenharmony_ci					    (fpi->tx_ring + fpi->rx_ring) *
1528c2ecf20Sopenharmony_ci					    sizeof(cbd_t), &fep->ring_mem_addr,
1538c2ecf20Sopenharmony_ci					    GFP_KERNEL);
1548c2ecf20Sopenharmony_ci	if (fep->ring_base == NULL)
1558c2ecf20Sopenharmony_ci		return -ENOMEM;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	return 0;
1588c2ecf20Sopenharmony_ci}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_cistatic void free_bd(struct net_device *dev)
1618c2ecf20Sopenharmony_ci{
1628c2ecf20Sopenharmony_ci	struct fs_enet_private *fep = netdev_priv(dev);
1638c2ecf20Sopenharmony_ci	const struct fs_platform_info *fpi = fep->fpi;
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	if (fep->ring_base)
1668c2ecf20Sopenharmony_ci		dma_free_coherent(fep->dev,
1678c2ecf20Sopenharmony_ci			(fpi->tx_ring + fpi->rx_ring) * sizeof(cbd_t),
1688c2ecf20Sopenharmony_ci			(void __force *)fep->ring_base, fep->ring_mem_addr);
1698c2ecf20Sopenharmony_ci}
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_cistatic void cleanup_data(struct net_device *dev)
1728c2ecf20Sopenharmony_ci{
1738c2ecf20Sopenharmony_ci	/* nothing */
1748c2ecf20Sopenharmony_ci}
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_cistatic void set_promiscuous_mode(struct net_device *dev)
1778c2ecf20Sopenharmony_ci{
1788c2ecf20Sopenharmony_ci	struct fs_enet_private *fep = netdev_priv(dev);
1798c2ecf20Sopenharmony_ci	fcc_t __iomem *fccp = fep->fcc.fccp;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	S32(fccp, fcc_fpsmr, FCC_PSMR_PRO);
1828c2ecf20Sopenharmony_ci}
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_cistatic void set_multicast_start(struct net_device *dev)
1858c2ecf20Sopenharmony_ci{
1868c2ecf20Sopenharmony_ci	struct fs_enet_private *fep = netdev_priv(dev);
1878c2ecf20Sopenharmony_ci	fcc_enet_t __iomem *ep = fep->fcc.ep;
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	W32(ep, fen_gaddrh, 0);
1908c2ecf20Sopenharmony_ci	W32(ep, fen_gaddrl, 0);
1918c2ecf20Sopenharmony_ci}
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_cistatic void set_multicast_one(struct net_device *dev, const u8 *mac)
1948c2ecf20Sopenharmony_ci{
1958c2ecf20Sopenharmony_ci	struct fs_enet_private *fep = netdev_priv(dev);
1968c2ecf20Sopenharmony_ci	fcc_enet_t __iomem *ep = fep->fcc.ep;
1978c2ecf20Sopenharmony_ci	u16 taddrh, taddrm, taddrl;
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	taddrh = ((u16)mac[5] << 8) | mac[4];
2008c2ecf20Sopenharmony_ci	taddrm = ((u16)mac[3] << 8) | mac[2];
2018c2ecf20Sopenharmony_ci	taddrl = ((u16)mac[1] << 8) | mac[0];
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	W16(ep, fen_taddrh, taddrh);
2048c2ecf20Sopenharmony_ci	W16(ep, fen_taddrm, taddrm);
2058c2ecf20Sopenharmony_ci	W16(ep, fen_taddrl, taddrl);
2068c2ecf20Sopenharmony_ci	fcc_cr_cmd(fep, CPM_CR_SET_GADDR);
2078c2ecf20Sopenharmony_ci}
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_cistatic void set_multicast_finish(struct net_device *dev)
2108c2ecf20Sopenharmony_ci{
2118c2ecf20Sopenharmony_ci	struct fs_enet_private *fep = netdev_priv(dev);
2128c2ecf20Sopenharmony_ci	fcc_t __iomem *fccp = fep->fcc.fccp;
2138c2ecf20Sopenharmony_ci	fcc_enet_t __iomem *ep = fep->fcc.ep;
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	/* clear promiscuous always */
2168c2ecf20Sopenharmony_ci	C32(fccp, fcc_fpsmr, FCC_PSMR_PRO);
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	/* if all multi or too many multicasts; just enable all */
2198c2ecf20Sopenharmony_ci	if ((dev->flags & IFF_ALLMULTI) != 0 ||
2208c2ecf20Sopenharmony_ci	    netdev_mc_count(dev) > FCC_MAX_MULTICAST_ADDRS) {
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci		W32(ep, fen_gaddrh, 0xffffffff);
2238c2ecf20Sopenharmony_ci		W32(ep, fen_gaddrl, 0xffffffff);
2248c2ecf20Sopenharmony_ci	}
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	/* read back */
2278c2ecf20Sopenharmony_ci	fep->fcc.gaddrh = R32(ep, fen_gaddrh);
2288c2ecf20Sopenharmony_ci	fep->fcc.gaddrl = R32(ep, fen_gaddrl);
2298c2ecf20Sopenharmony_ci}
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_cistatic void set_multicast_list(struct net_device *dev)
2328c2ecf20Sopenharmony_ci{
2338c2ecf20Sopenharmony_ci	struct netdev_hw_addr *ha;
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	if ((dev->flags & IFF_PROMISC) == 0) {
2368c2ecf20Sopenharmony_ci		set_multicast_start(dev);
2378c2ecf20Sopenharmony_ci		netdev_for_each_mc_addr(ha, dev)
2388c2ecf20Sopenharmony_ci			set_multicast_one(dev, ha->addr);
2398c2ecf20Sopenharmony_ci		set_multicast_finish(dev);
2408c2ecf20Sopenharmony_ci	} else
2418c2ecf20Sopenharmony_ci		set_promiscuous_mode(dev);
2428c2ecf20Sopenharmony_ci}
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_cistatic void restart(struct net_device *dev)
2458c2ecf20Sopenharmony_ci{
2468c2ecf20Sopenharmony_ci	struct fs_enet_private *fep = netdev_priv(dev);
2478c2ecf20Sopenharmony_ci	const struct fs_platform_info *fpi = fep->fpi;
2488c2ecf20Sopenharmony_ci	fcc_t __iomem *fccp = fep->fcc.fccp;
2498c2ecf20Sopenharmony_ci	fcc_c_t __iomem *fcccp = fep->fcc.fcccp;
2508c2ecf20Sopenharmony_ci	fcc_enet_t __iomem *ep = fep->fcc.ep;
2518c2ecf20Sopenharmony_ci	dma_addr_t rx_bd_base_phys, tx_bd_base_phys;
2528c2ecf20Sopenharmony_ci	u16 paddrh, paddrm, paddrl;
2538c2ecf20Sopenharmony_ci	const unsigned char *mac;
2548c2ecf20Sopenharmony_ci	int i;
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	C32(fccp, fcc_gfmr, FCC_GFMR_ENR | FCC_GFMR_ENT);
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	/* clear everything (slow & steady does it) */
2598c2ecf20Sopenharmony_ci	for (i = 0; i < sizeof(*ep); i++)
2608c2ecf20Sopenharmony_ci		out_8((u8 __iomem *)ep + i, 0);
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	/* get physical address */
2638c2ecf20Sopenharmony_ci	rx_bd_base_phys = fep->ring_mem_addr;
2648c2ecf20Sopenharmony_ci	tx_bd_base_phys = rx_bd_base_phys + sizeof(cbd_t) * fpi->rx_ring;
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	/* point to bds */
2678c2ecf20Sopenharmony_ci	W32(ep, fen_genfcc.fcc_rbase, rx_bd_base_phys);
2688c2ecf20Sopenharmony_ci	W32(ep, fen_genfcc.fcc_tbase, tx_bd_base_phys);
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	/* Set maximum bytes per receive buffer.
2718c2ecf20Sopenharmony_ci	 * It must be a multiple of 32.
2728c2ecf20Sopenharmony_ci	 */
2738c2ecf20Sopenharmony_ci	W16(ep, fen_genfcc.fcc_mrblr, PKT_MAXBLR_SIZE);
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	W32(ep, fen_genfcc.fcc_rstate, (CPMFCR_GBL | CPMFCR_EB) << 24);
2768c2ecf20Sopenharmony_ci	W32(ep, fen_genfcc.fcc_tstate, (CPMFCR_GBL | CPMFCR_EB) << 24);
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	/* Allocate space in the reserved FCC area of DPRAM for the
2798c2ecf20Sopenharmony_ci	 * internal buffers.  No one uses this space (yet), so we
2808c2ecf20Sopenharmony_ci	 * can do this.  Later, we will add resource management for
2818c2ecf20Sopenharmony_ci	 * this area.
2828c2ecf20Sopenharmony_ci	 */
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	W16(ep, fen_genfcc.fcc_riptr, fpi->dpram_offset);
2858c2ecf20Sopenharmony_ci	W16(ep, fen_genfcc.fcc_tiptr, fpi->dpram_offset + 32);
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	W16(ep, fen_padptr, fpi->dpram_offset + 64);
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	/* fill with special symbol...  */
2908c2ecf20Sopenharmony_ci	memset_io(fep->fcc.mem + fpi->dpram_offset + 64, 0x88, 32);
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	W32(ep, fen_genfcc.fcc_rbptr, 0);
2938c2ecf20Sopenharmony_ci	W32(ep, fen_genfcc.fcc_tbptr, 0);
2948c2ecf20Sopenharmony_ci	W32(ep, fen_genfcc.fcc_rcrc, 0);
2958c2ecf20Sopenharmony_ci	W32(ep, fen_genfcc.fcc_tcrc, 0);
2968c2ecf20Sopenharmony_ci	W16(ep, fen_genfcc.fcc_res1, 0);
2978c2ecf20Sopenharmony_ci	W32(ep, fen_genfcc.fcc_res2, 0);
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci	/* no CAM */
3008c2ecf20Sopenharmony_ci	W32(ep, fen_camptr, 0);
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	/* Set CRC preset and mask */
3038c2ecf20Sopenharmony_ci	W32(ep, fen_cmask, 0xdebb20e3);
3048c2ecf20Sopenharmony_ci	W32(ep, fen_cpres, 0xffffffff);
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	W32(ep, fen_crcec, 0);		/* CRC Error counter       */
3078c2ecf20Sopenharmony_ci	W32(ep, fen_alec, 0);		/* alignment error counter */
3088c2ecf20Sopenharmony_ci	W32(ep, fen_disfc, 0);		/* discard frame counter   */
3098c2ecf20Sopenharmony_ci	W16(ep, fen_retlim, 15);	/* Retry limit threshold   */
3108c2ecf20Sopenharmony_ci	W16(ep, fen_pper, 0);		/* Normal persistence      */
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	/* set group address */
3138c2ecf20Sopenharmony_ci	W32(ep, fen_gaddrh, fep->fcc.gaddrh);
3148c2ecf20Sopenharmony_ci	W32(ep, fen_gaddrl, fep->fcc.gaddrh);
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	/* Clear hash filter tables */
3178c2ecf20Sopenharmony_ci	W32(ep, fen_iaddrh, 0);
3188c2ecf20Sopenharmony_ci	W32(ep, fen_iaddrl, 0);
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	/* Clear the Out-of-sequence TxBD  */
3218c2ecf20Sopenharmony_ci	W16(ep, fen_tfcstat, 0);
3228c2ecf20Sopenharmony_ci	W16(ep, fen_tfclen, 0);
3238c2ecf20Sopenharmony_ci	W32(ep, fen_tfcptr, 0);
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	W16(ep, fen_mflr, PKT_MAXBUF_SIZE);	/* maximum frame length register */
3268c2ecf20Sopenharmony_ci	W16(ep, fen_minflr, PKT_MINBUF_SIZE);	/* minimum frame length register */
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	/* set address */
3298c2ecf20Sopenharmony_ci	mac = dev->dev_addr;
3308c2ecf20Sopenharmony_ci	paddrh = ((u16)mac[5] << 8) | mac[4];
3318c2ecf20Sopenharmony_ci	paddrm = ((u16)mac[3] << 8) | mac[2];
3328c2ecf20Sopenharmony_ci	paddrl = ((u16)mac[1] << 8) | mac[0];
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	W16(ep, fen_paddrh, paddrh);
3358c2ecf20Sopenharmony_ci	W16(ep, fen_paddrm, paddrm);
3368c2ecf20Sopenharmony_ci	W16(ep, fen_paddrl, paddrl);
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	W16(ep, fen_taddrh, 0);
3398c2ecf20Sopenharmony_ci	W16(ep, fen_taddrm, 0);
3408c2ecf20Sopenharmony_ci	W16(ep, fen_taddrl, 0);
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci	W16(ep, fen_maxd1, 1520);	/* maximum DMA1 length */
3438c2ecf20Sopenharmony_ci	W16(ep, fen_maxd2, 1520);	/* maximum DMA2 length */
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci	/* Clear stat counters, in case we ever enable RMON */
3468c2ecf20Sopenharmony_ci	W32(ep, fen_octc, 0);
3478c2ecf20Sopenharmony_ci	W32(ep, fen_colc, 0);
3488c2ecf20Sopenharmony_ci	W32(ep, fen_broc, 0);
3498c2ecf20Sopenharmony_ci	W32(ep, fen_mulc, 0);
3508c2ecf20Sopenharmony_ci	W32(ep, fen_uspc, 0);
3518c2ecf20Sopenharmony_ci	W32(ep, fen_frgc, 0);
3528c2ecf20Sopenharmony_ci	W32(ep, fen_ospc, 0);
3538c2ecf20Sopenharmony_ci	W32(ep, fen_jbrc, 0);
3548c2ecf20Sopenharmony_ci	W32(ep, fen_p64c, 0);
3558c2ecf20Sopenharmony_ci	W32(ep, fen_p65c, 0);
3568c2ecf20Sopenharmony_ci	W32(ep, fen_p128c, 0);
3578c2ecf20Sopenharmony_ci	W32(ep, fen_p256c, 0);
3588c2ecf20Sopenharmony_ci	W32(ep, fen_p512c, 0);
3598c2ecf20Sopenharmony_ci	W32(ep, fen_p1024c, 0);
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	W16(ep, fen_rfthr, 0);	/* Suggested by manual */
3628c2ecf20Sopenharmony_ci	W16(ep, fen_rfcnt, 0);
3638c2ecf20Sopenharmony_ci	W16(ep, fen_cftype, 0);
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci	fs_init_bds(dev);
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	/* adjust to speed (for RMII mode) */
3688c2ecf20Sopenharmony_ci	if (fpi->use_rmii) {
3698c2ecf20Sopenharmony_ci		if (dev->phydev->speed == 100)
3708c2ecf20Sopenharmony_ci			C8(fcccp, fcc_gfemr, 0x20);
3718c2ecf20Sopenharmony_ci		else
3728c2ecf20Sopenharmony_ci			S8(fcccp, fcc_gfemr, 0x20);
3738c2ecf20Sopenharmony_ci	}
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	fcc_cr_cmd(fep, CPM_CR_INIT_TRX);
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	/* clear events */
3788c2ecf20Sopenharmony_ci	W16(fccp, fcc_fcce, 0xffff);
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	/* Enable interrupts we wish to service */
3818c2ecf20Sopenharmony_ci	W16(fccp, fcc_fccm, FCC_ENET_TXE | FCC_ENET_RXF | FCC_ENET_TXB);
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	/* Set GFMR to enable Ethernet operating mode */
3848c2ecf20Sopenharmony_ci	W32(fccp, fcc_gfmr, FCC_GFMR_TCI | FCC_GFMR_MODE_ENET);
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	/* set sync/delimiters */
3878c2ecf20Sopenharmony_ci	W16(fccp, fcc_fdsr, 0xd555);
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	W32(fccp, fcc_fpsmr, FCC_PSMR_ENCRC);
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	if (fpi->use_rmii)
3928c2ecf20Sopenharmony_ci		S32(fccp, fcc_fpsmr, FCC_PSMR_RMII);
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci	/* adjust to duplex mode */
3958c2ecf20Sopenharmony_ci	if (dev->phydev->duplex)
3968c2ecf20Sopenharmony_ci		S32(fccp, fcc_fpsmr, FCC_PSMR_FDE | FCC_PSMR_LPB);
3978c2ecf20Sopenharmony_ci	else
3988c2ecf20Sopenharmony_ci		C32(fccp, fcc_fpsmr, FCC_PSMR_FDE | FCC_PSMR_LPB);
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	/* Restore multicast and promiscuous settings */
4018c2ecf20Sopenharmony_ci	set_multicast_list(dev);
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci	S32(fccp, fcc_gfmr, FCC_GFMR_ENR | FCC_GFMR_ENT);
4048c2ecf20Sopenharmony_ci}
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_cistatic void stop(struct net_device *dev)
4078c2ecf20Sopenharmony_ci{
4088c2ecf20Sopenharmony_ci	struct fs_enet_private *fep = netdev_priv(dev);
4098c2ecf20Sopenharmony_ci	fcc_t __iomem *fccp = fep->fcc.fccp;
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	/* stop ethernet */
4128c2ecf20Sopenharmony_ci	C32(fccp, fcc_gfmr, FCC_GFMR_ENR | FCC_GFMR_ENT);
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	/* clear events */
4158c2ecf20Sopenharmony_ci	W16(fccp, fcc_fcce, 0xffff);
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci	/* clear interrupt mask */
4188c2ecf20Sopenharmony_ci	W16(fccp, fcc_fccm, 0);
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	fs_cleanup_bds(dev);
4218c2ecf20Sopenharmony_ci}
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_cistatic void napi_clear_event_fs(struct net_device *dev)
4248c2ecf20Sopenharmony_ci{
4258c2ecf20Sopenharmony_ci	struct fs_enet_private *fep = netdev_priv(dev);
4268c2ecf20Sopenharmony_ci	fcc_t __iomem *fccp = fep->fcc.fccp;
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	W16(fccp, fcc_fcce, FCC_NAPI_EVENT_MSK);
4298c2ecf20Sopenharmony_ci}
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_cistatic void napi_enable_fs(struct net_device *dev)
4328c2ecf20Sopenharmony_ci{
4338c2ecf20Sopenharmony_ci	struct fs_enet_private *fep = netdev_priv(dev);
4348c2ecf20Sopenharmony_ci	fcc_t __iomem *fccp = fep->fcc.fccp;
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci	S16(fccp, fcc_fccm, FCC_NAPI_EVENT_MSK);
4378c2ecf20Sopenharmony_ci}
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_cistatic void napi_disable_fs(struct net_device *dev)
4408c2ecf20Sopenharmony_ci{
4418c2ecf20Sopenharmony_ci	struct fs_enet_private *fep = netdev_priv(dev);
4428c2ecf20Sopenharmony_ci	fcc_t __iomem *fccp = fep->fcc.fccp;
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	C16(fccp, fcc_fccm, FCC_NAPI_EVENT_MSK);
4458c2ecf20Sopenharmony_ci}
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_cistatic void rx_bd_done(struct net_device *dev)
4488c2ecf20Sopenharmony_ci{
4498c2ecf20Sopenharmony_ci	/* nothing */
4508c2ecf20Sopenharmony_ci}
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_cistatic void tx_kickstart(struct net_device *dev)
4538c2ecf20Sopenharmony_ci{
4548c2ecf20Sopenharmony_ci	struct fs_enet_private *fep = netdev_priv(dev);
4558c2ecf20Sopenharmony_ci	fcc_t __iomem *fccp = fep->fcc.fccp;
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci	S16(fccp, fcc_ftodr, 0x8000);
4588c2ecf20Sopenharmony_ci}
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_cistatic u32 get_int_events(struct net_device *dev)
4618c2ecf20Sopenharmony_ci{
4628c2ecf20Sopenharmony_ci	struct fs_enet_private *fep = netdev_priv(dev);
4638c2ecf20Sopenharmony_ci	fcc_t __iomem *fccp = fep->fcc.fccp;
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci	return (u32)R16(fccp, fcc_fcce);
4668c2ecf20Sopenharmony_ci}
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_cistatic void clear_int_events(struct net_device *dev, u32 int_events)
4698c2ecf20Sopenharmony_ci{
4708c2ecf20Sopenharmony_ci	struct fs_enet_private *fep = netdev_priv(dev);
4718c2ecf20Sopenharmony_ci	fcc_t __iomem *fccp = fep->fcc.fccp;
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	W16(fccp, fcc_fcce, int_events & 0xffff);
4748c2ecf20Sopenharmony_ci}
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_cistatic void ev_error(struct net_device *dev, u32 int_events)
4778c2ecf20Sopenharmony_ci{
4788c2ecf20Sopenharmony_ci	struct fs_enet_private *fep = netdev_priv(dev);
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci	dev_warn(fep->dev, "FS_ENET ERROR(s) 0x%x\n", int_events);
4818c2ecf20Sopenharmony_ci}
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_cistatic int get_regs(struct net_device *dev, void *p, int *sizep)
4848c2ecf20Sopenharmony_ci{
4858c2ecf20Sopenharmony_ci	struct fs_enet_private *fep = netdev_priv(dev);
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci	if (*sizep < sizeof(fcc_t) + sizeof(fcc_enet_t) + 1)
4888c2ecf20Sopenharmony_ci		return -EINVAL;
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	memcpy_fromio(p, fep->fcc.fccp, sizeof(fcc_t));
4918c2ecf20Sopenharmony_ci	p = (char *)p + sizeof(fcc_t);
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	memcpy_fromio(p, fep->fcc.ep, sizeof(fcc_enet_t));
4948c2ecf20Sopenharmony_ci	p = (char *)p + sizeof(fcc_enet_t);
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci	memcpy_fromio(p, fep->fcc.fcccp, 1);
4978c2ecf20Sopenharmony_ci	return 0;
4988c2ecf20Sopenharmony_ci}
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_cistatic int get_regs_len(struct net_device *dev)
5018c2ecf20Sopenharmony_ci{
5028c2ecf20Sopenharmony_ci	return sizeof(fcc_t) + sizeof(fcc_enet_t) + 1;
5038c2ecf20Sopenharmony_ci}
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci/* Some transmit errors cause the transmitter to shut
5068c2ecf20Sopenharmony_ci * down.  We now issue a restart transmit.
5078c2ecf20Sopenharmony_ci * Also, to workaround 8260 device erratum CPM37, we must
5088c2ecf20Sopenharmony_ci * disable and then re-enable the transmitterfollowing a
5098c2ecf20Sopenharmony_ci * Late Collision, Underrun, or Retry Limit error.
5108c2ecf20Sopenharmony_ci * In addition, tbptr may point beyond BDs beyond still marked
5118c2ecf20Sopenharmony_ci * as ready due to internal pipelining, so we need to look back
5128c2ecf20Sopenharmony_ci * through the BDs and adjust tbptr to point to the last BD
5138c2ecf20Sopenharmony_ci * marked as ready.  This may result in some buffers being
5148c2ecf20Sopenharmony_ci * retransmitted.
5158c2ecf20Sopenharmony_ci */
5168c2ecf20Sopenharmony_cistatic void tx_restart(struct net_device *dev)
5178c2ecf20Sopenharmony_ci{
5188c2ecf20Sopenharmony_ci	struct fs_enet_private *fep = netdev_priv(dev);
5198c2ecf20Sopenharmony_ci	fcc_t __iomem *fccp = fep->fcc.fccp;
5208c2ecf20Sopenharmony_ci	const struct fs_platform_info *fpi = fep->fpi;
5218c2ecf20Sopenharmony_ci	fcc_enet_t __iomem *ep = fep->fcc.ep;
5228c2ecf20Sopenharmony_ci	cbd_t __iomem *curr_tbptr;
5238c2ecf20Sopenharmony_ci	cbd_t __iomem *recheck_bd;
5248c2ecf20Sopenharmony_ci	cbd_t __iomem *prev_bd;
5258c2ecf20Sopenharmony_ci	cbd_t __iomem *last_tx_bd;
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	last_tx_bd = fep->tx_bd_base + (fpi->tx_ring - 1);
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci	/* get the current bd held in TBPTR  and scan back from this point */
5308c2ecf20Sopenharmony_ci	recheck_bd = curr_tbptr = (cbd_t __iomem *)
5318c2ecf20Sopenharmony_ci		((R32(ep, fen_genfcc.fcc_tbptr) - fep->ring_mem_addr) +
5328c2ecf20Sopenharmony_ci		fep->ring_base);
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci	prev_bd = (recheck_bd == fep->tx_bd_base) ? last_tx_bd : recheck_bd - 1;
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci	/* Move through the bds in reverse, look for the earliest buffer
5378c2ecf20Sopenharmony_ci	 * that is not ready.  Adjust TBPTR to the following buffer */
5388c2ecf20Sopenharmony_ci	while ((CBDR_SC(prev_bd) & BD_ENET_TX_READY) != 0) {
5398c2ecf20Sopenharmony_ci		/* Go back one buffer */
5408c2ecf20Sopenharmony_ci		recheck_bd = prev_bd;
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_ci		/* update the previous buffer */
5438c2ecf20Sopenharmony_ci		prev_bd = (prev_bd == fep->tx_bd_base) ? last_tx_bd : prev_bd - 1;
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci		/* We should never see all bds marked as ready, check anyway */
5468c2ecf20Sopenharmony_ci		if (recheck_bd == curr_tbptr)
5478c2ecf20Sopenharmony_ci			break;
5488c2ecf20Sopenharmony_ci	}
5498c2ecf20Sopenharmony_ci	/* Now update the TBPTR and dirty flag to the current buffer */
5508c2ecf20Sopenharmony_ci	W32(ep, fen_genfcc.fcc_tbptr,
5518c2ecf20Sopenharmony_ci		(uint) (((void *)recheck_bd - fep->ring_base) +
5528c2ecf20Sopenharmony_ci		fep->ring_mem_addr));
5538c2ecf20Sopenharmony_ci	fep->dirty_tx = recheck_bd;
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci	C32(fccp, fcc_gfmr, FCC_GFMR_ENT);
5568c2ecf20Sopenharmony_ci	udelay(10);
5578c2ecf20Sopenharmony_ci	S32(fccp, fcc_gfmr, FCC_GFMR_ENT);
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci	fcc_cr_cmd(fep, CPM_CR_RESTART_TX);
5608c2ecf20Sopenharmony_ci}
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci/*************************************************************************/
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ciconst struct fs_ops fs_fcc_ops = {
5658c2ecf20Sopenharmony_ci	.setup_data		= setup_data,
5668c2ecf20Sopenharmony_ci	.cleanup_data		= cleanup_data,
5678c2ecf20Sopenharmony_ci	.set_multicast_list	= set_multicast_list,
5688c2ecf20Sopenharmony_ci	.restart		= restart,
5698c2ecf20Sopenharmony_ci	.stop			= stop,
5708c2ecf20Sopenharmony_ci	.napi_clear_event	= napi_clear_event_fs,
5718c2ecf20Sopenharmony_ci	.napi_enable		= napi_enable_fs,
5728c2ecf20Sopenharmony_ci	.napi_disable		= napi_disable_fs,
5738c2ecf20Sopenharmony_ci	.rx_bd_done		= rx_bd_done,
5748c2ecf20Sopenharmony_ci	.tx_kickstart		= tx_kickstart,
5758c2ecf20Sopenharmony_ci	.get_int_events		= get_int_events,
5768c2ecf20Sopenharmony_ci	.clear_int_events	= clear_int_events,
5778c2ecf20Sopenharmony_ci	.ev_error		= ev_error,
5788c2ecf20Sopenharmony_ci	.get_regs		= get_regs,
5798c2ecf20Sopenharmony_ci	.get_regs_len		= get_regs_len,
5808c2ecf20Sopenharmony_ci	.tx_restart		= tx_restart,
5818c2ecf20Sopenharmony_ci	.allocate_bd		= allocate_bd,
5828c2ecf20Sopenharmony_ci	.free_bd		= free_bd,
5838c2ecf20Sopenharmony_ci};
584