1 /*************************************************************************
2 SPDX-License-Identifier: BSD-3-Clause
3 
4 Copyright (c) 2003-2007  Cavium Networks ([email protected]). All rights
5 reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are
9 met:
10 
11     * Redistributions of source code must retain the above copyright
12       notice, this list of conditions and the following disclaimer.
13 
14     * Redistributions in binary form must reproduce the above
15       copyright notice, this list of conditions and the following
16       disclaimer in the documentation and/or other materials provided
17       with the distribution.
18 
19     * Neither the name of Cavium Networks nor the names of
20       its contributors may be used to endorse or promote products
21       derived from this software without specific prior written
22       permission.
23 
24 This Software, including technical data, may be subject to U.S. export  control laws, including the U.S. Export Administration Act and its  associated regulations, and may be subject to export or import  regulations in other countries.
25 
26 TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
27 AND WITH ALL FAULTS AND CAVIUM  NETWORKS MAKES NO PROMISES, REPRESENTATIONS OR WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR CORRESPONDENCE TO DESCRIPTION. THE ENTIRE  RISK ARISING OUT OF USE OR PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
28 
29 *************************************************************************/
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bus.h>
37 #include <sys/endian.h>
38 #include <sys/kernel.h>
39 #include <sys/mbuf.h>
40 #include <sys/socket.h>
41 #include <sys/lock.h>
42 #include <sys/mutex.h>
43 
44 #include <net/ethernet.h>
45 #include <net/if.h>
46 #include <net/if_var.h>
47 
48 #include "wrapper-cvmx-includes.h"
49 #include "ethernet-headers.h"
50 
51 struct mtx cvm_oct_mdio_mtx;
52 MTX_SYSINIT(cvm_oct_mdio, &cvm_oct_mdio_mtx, "MDIO", MTX_DEF);
53 
54 /**
55  * Perform an MII read. Called by the generic MII routines
56  *
57  * @param dev      Device to perform read for
58  * @param phy_id   The MII phy id
59  * @param location Register location to read
60  * @return Result from the read or zero on failure
61  */
cvm_oct_mdio_read(struct ifnet * ifp,int phy_id,int location)62 int cvm_oct_mdio_read(struct ifnet *ifp, int phy_id, int location)
63 {
64 	cvmx_smi_cmd_t          smi_cmd;
65 	cvmx_smi_rd_dat_t       smi_rd;
66 
67 	MDIO_LOCK();
68 	smi_cmd.u64 = 0;
69 	smi_cmd.s.phy_op = 1;
70 	smi_cmd.s.phy_adr = phy_id;
71 	smi_cmd.s.reg_adr = location;
72 	cvmx_write_csr(CVMX_SMI_CMD, smi_cmd.u64);
73 
74 	do {
75 		cvmx_wait(1000);
76 		smi_rd.u64 = cvmx_read_csr(CVMX_SMI_RD_DAT);
77 	} while (smi_rd.s.pending);
78 
79 	MDIO_UNLOCK();
80 
81 	if (smi_rd.s.val)
82 		return smi_rd.s.dat;
83 	else
84 		return 0;
85 }
86 
87 /**
88  * Perform an MII write. Called by the generic MII routines
89  *
90  * @param dev      Device to perform write for
91  * @param phy_id   The MII phy id
92  * @param location Register location to write
93  * @param val      Value to write
94  */
cvm_oct_mdio_write(struct ifnet * ifp,int phy_id,int location,int val)95 void cvm_oct_mdio_write(struct ifnet *ifp, int phy_id, int location, int val)
96 {
97 	cvmx_smi_cmd_t          smi_cmd;
98 	cvmx_smi_wr_dat_t       smi_wr;
99 
100 	MDIO_LOCK();
101 	smi_wr.u64 = 0;
102 	smi_wr.s.dat = val;
103 	cvmx_write_csr(CVMX_SMI_WR_DAT, smi_wr.u64);
104 
105 	smi_cmd.u64 = 0;
106 	smi_cmd.s.phy_op = 0;
107 	smi_cmd.s.phy_adr = phy_id;
108 	smi_cmd.s.reg_adr = location;
109 	cvmx_write_csr(CVMX_SMI_CMD, smi_cmd.u64);
110 
111 	do {
112 		cvmx_wait(1000);
113 		smi_wr.u64 = cvmx_read_csr(CVMX_SMI_WR_DAT);
114 	} while (smi_wr.s.pending);
115 	MDIO_UNLOCK();
116 }
117 
118 /**
119  * Setup the MDIO device structures
120  *
121  * @param dev    Device to setup
122  *
123  * @return Zero on success, negative on failure
124  */
cvm_oct_mdio_setup_device(struct ifnet * ifp)125 int cvm_oct_mdio_setup_device(struct ifnet *ifp)
126 {
127 	cvm_oct_private_t *priv = (cvm_oct_private_t *)ifp->if_softc;
128 
129 	priv->phy_id = cvmx_helper_board_get_mii_address(priv->port);
130 	priv->phy_device = NULL;
131 	priv->mdio_read = NULL;
132 	priv->mdio_write = NULL;
133 
134 	return 0;
135 }
136