14Srgrimes/*-
24Srgrimes * Copyright (c) 2010 Juli Mallett <jmallett@FreeBSD.org>
34Srgrimes * All rights reserved.
44Srgrimes *
54Srgrimes * Redistribution and use in source and binary forms, with or without
64Srgrimes * modification, are permitted provided that the following conditions
74Srgrimes * are met:
84Srgrimes * 1. Redistributions of source code must retain the above copyright
94Srgrimes *    notice, this list of conditions and the following disclaimer.
104Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
114Srgrimes *    notice, this list of conditions and the following disclaimer in the
124Srgrimes *    documentation and/or other materials provided with the distribution.
134Srgrimes *
144Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
154Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
164Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
174Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
184Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
194Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
204Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
214Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
224Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
234Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
244Srgrimes * SUCH DAMAGE.
254Srgrimes *
264Srgrimes * $FreeBSD$
274Srgrimes */
284Srgrimes
294Srgrimes/*
304Srgrimes * Interface to the Marvell 88E61XX SMI/MDIO.
314Srgrimes */
32621Srgrimes
3350477Speter#include <sys/cdefs.h>
344Srgrimes__FBSDID("$FreeBSD$");
354Srgrimes
36719Swollman#include <sys/param.h>
37719Swollman#include <sys/systm.h>
38719Swollman#include <sys/bus.h>
394Srgrimes#include <sys/endian.h>
40114349Speter#include <sys/kernel.h>
41114349Speter#include <sys/mbuf.h>
42114349Speter#include <sys/socket.h>
43114349Speter
44114349Speter#include <dev/mii/mii.h>
454Srgrimes
46114349Speter#include <net/ethernet.h>
47114349Speter#include <net/if.h>
48114349Speter
49114349Speter#include "wrapper-cvmx-includes.h"
50114349Speter#include "ethernet-headers.h"
51114349Speter
52114349Speter#define	MV88E61XX_SMI_REG_CMD	0x00	/* Indirect command register.  */
53114349Speter#define	 MV88E61XX_SMI_CMD_BUSY		0x8000	/* Busy bit.  */
54114349Speter#define	 MV88E61XX_SMI_CMD_22		0x1000	/* Clause 22 (default 45.)  */
55114349Speter#define	 MV88E61XX_SMI_CMD_READ		0x0800	/* Read command.  */
56114349Speter#define	 MV88E61XX_SMI_CMD_WRITE	0x0400	/* Write command.  */
57114349Speter#define	 MV88E61XX_SMI_CMD_PHY(phy)	(((phy) & 0x1f) << 5)
58114349Speter#define	 MV88E61XX_SMI_CMD_REG(reg)	((reg) & 0x1f)
59114349Speter
60145120Speter#define	MV88E61XX_SMI_REG_DAT	0x01	/* Indirect data register.  */
61114349Speter
62145120Speterstatic int cvm_oct_mv88e61xx_smi_read(struct ifnet *, int, int);
63114349Speterstatic void cvm_oct_mv88e61xx_smi_write(struct ifnet *, int, int, int);
644Srgrimesstatic int cvm_oct_mv88e61xx_smi_wait(struct ifnet *);
6546129Sluoqi
66114349Speterint
67122849Spetercvm_oct_mv88e61xx_setup_device(struct ifnet *ifp)
68114349Speter{
69114349Speter	cvm_oct_private_t *priv = (cvm_oct_private_t *)ifp->if_softc;
70719Swollman
71	priv->mdio_read = cvm_oct_mv88e61xx_smi_read;
72	priv->mdio_write = cvm_oct_mv88e61xx_smi_write;
73	priv->phy_device = "mv88e61xxphy";
74
75	return (0);
76}
77
78static int
79cvm_oct_mv88e61xx_smi_read(struct ifnet *ifp, int phy_id, int location)
80{
81	cvm_oct_private_t *priv = (cvm_oct_private_t *)ifp->if_softc;
82	int error;
83
84	error = cvm_oct_mv88e61xx_smi_wait(ifp);
85	if (error != 0)
86		return (0);
87
88	cvm_oct_mdio_write(ifp, priv->phy_id, MV88E61XX_SMI_REG_CMD,
89	    MV88E61XX_SMI_CMD_BUSY | MV88E61XX_SMI_CMD_22 |
90	    MV88E61XX_SMI_CMD_READ | MV88E61XX_SMI_CMD_PHY(phy_id) |
91	    MV88E61XX_SMI_CMD_REG(location));
92
93	error = cvm_oct_mv88e61xx_smi_wait(ifp);
94	if (error != 0)
95		return (0);
96
97	return (cvm_oct_mdio_read(ifp, priv->phy_id, MV88E61XX_SMI_REG_DAT));
98}
99
100static void
101cvm_oct_mv88e61xx_smi_write(struct ifnet *ifp, int phy_id, int location, int val)
102{
103	cvm_oct_private_t *priv = (cvm_oct_private_t *)ifp->if_softc;
104
105	cvm_oct_mv88e61xx_smi_wait(ifp);
106	cvm_oct_mdio_write(ifp, priv->phy_id, MV88E61XX_SMI_REG_DAT, val);
107	cvm_oct_mdio_write(ifp, priv->phy_id, MV88E61XX_SMI_REG_CMD,
108	    MV88E61XX_SMI_CMD_BUSY | MV88E61XX_SMI_CMD_22 |
109	    MV88E61XX_SMI_CMD_WRITE | MV88E61XX_SMI_CMD_PHY(phy_id) |
110	    MV88E61XX_SMI_CMD_REG(location));
111	cvm_oct_mv88e61xx_smi_wait(ifp);
112}
113
114static int
115cvm_oct_mv88e61xx_smi_wait(struct ifnet *ifp)
116{
117	cvm_oct_private_t *priv = (cvm_oct_private_t *)ifp->if_softc;
118	uint16_t cmd;
119	unsigned i;
120
121	for (i = 0; i < 10000; i++) {
122		cmd = cvm_oct_mdio_read(ifp, priv->phy_id, MV88E61XX_SMI_REG_CMD);
123		if ((cmd & MV88E61XX_SMI_CMD_BUSY) == 0)
124			return (0);
125	}
126	return (ETIMEDOUT);
127}
128