xref: /OK3568_Linux_fs/kernel/arch/mips/cavium-octeon/executive/cvmx-helper-spi.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /***********************license start***************
2*4882a593Smuzhiyun  * Author: Cavium Networks
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Contact: support@caviumnetworks.com
5*4882a593Smuzhiyun  * This file is part of the OCTEON SDK
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Copyright (C) 2003-2018 Cavium, Inc.
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * This file is free software; you can redistribute it and/or modify
10*4882a593Smuzhiyun  * it under the terms of the GNU General Public License, Version 2, as
11*4882a593Smuzhiyun  * published by the Free Software Foundation.
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * This file is distributed in the hope that it will be useful, but
14*4882a593Smuzhiyun  * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
15*4882a593Smuzhiyun  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
16*4882a593Smuzhiyun  * NONINFRINGEMENT.  See the GNU General Public License for more
17*4882a593Smuzhiyun  * details.
18*4882a593Smuzhiyun  *
19*4882a593Smuzhiyun  * You should have received a copy of the GNU General Public License
20*4882a593Smuzhiyun  * along with this file; if not, write to the Free Software
21*4882a593Smuzhiyun  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22*4882a593Smuzhiyun  * or visit http://www.gnu.org/licenses/.
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  * This file may also be available under a different license from Cavium.
25*4882a593Smuzhiyun  * Contact Cavium Networks for more information
26*4882a593Smuzhiyun  ***********************license end**************************************/
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun /*
29*4882a593Smuzhiyun  * Functions for SPI initialization, configuration,
30*4882a593Smuzhiyun  * and monitoring.
31*4882a593Smuzhiyun  */
32*4882a593Smuzhiyun #include <asm/octeon/octeon.h>
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun #include <asm/octeon/cvmx-config.h>
35*4882a593Smuzhiyun #include <asm/octeon/cvmx-spi.h>
36*4882a593Smuzhiyun #include <asm/octeon/cvmx-helper.h>
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun #include <asm/octeon/cvmx-pip-defs.h>
39*4882a593Smuzhiyun #include <asm/octeon/cvmx-pko-defs.h>
40*4882a593Smuzhiyun #include <asm/octeon/cvmx-spxx-defs.h>
41*4882a593Smuzhiyun #include <asm/octeon/cvmx-stxx-defs.h>
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun /*
44*4882a593Smuzhiyun  * CVMX_HELPER_SPI_TIMEOUT is used to determine how long the SPI
45*4882a593Smuzhiyun  * initialization routines wait for SPI training. You can override the
46*4882a593Smuzhiyun  * value using executive-config.h if necessary.
47*4882a593Smuzhiyun  */
48*4882a593Smuzhiyun #ifndef CVMX_HELPER_SPI_TIMEOUT
49*4882a593Smuzhiyun #define CVMX_HELPER_SPI_TIMEOUT 10
50*4882a593Smuzhiyun #endif
51*4882a593Smuzhiyun 
__cvmx_helper_spi_enumerate(int interface)52*4882a593Smuzhiyun int __cvmx_helper_spi_enumerate(int interface)
53*4882a593Smuzhiyun {
54*4882a593Smuzhiyun 	if ((cvmx_sysinfo_get()->board_type != CVMX_BOARD_TYPE_SIM) &&
55*4882a593Smuzhiyun 	    cvmx_spi4000_is_present(interface)) {
56*4882a593Smuzhiyun 		return 10;
57*4882a593Smuzhiyun 	} else {
58*4882a593Smuzhiyun 		return 16;
59*4882a593Smuzhiyun 	}
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun /**
63*4882a593Smuzhiyun  * Probe a SPI interface and determine the number of ports
64*4882a593Smuzhiyun  * connected to it. The SPI interface should still be down after
65*4882a593Smuzhiyun  * this call.
66*4882a593Smuzhiyun  *
67*4882a593Smuzhiyun  * @interface: Interface to probe
68*4882a593Smuzhiyun  *
69*4882a593Smuzhiyun  * Returns Number of ports on the interface. Zero to disable.
70*4882a593Smuzhiyun  */
__cvmx_helper_spi_probe(int interface)71*4882a593Smuzhiyun int __cvmx_helper_spi_probe(int interface)
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun 	int num_ports = 0;
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	if ((cvmx_sysinfo_get()->board_type != CVMX_BOARD_TYPE_SIM) &&
76*4882a593Smuzhiyun 	    cvmx_spi4000_is_present(interface)) {
77*4882a593Smuzhiyun 		num_ports = 10;
78*4882a593Smuzhiyun 	} else {
79*4882a593Smuzhiyun 		union cvmx_pko_reg_crc_enable enable;
80*4882a593Smuzhiyun 		num_ports = 16;
81*4882a593Smuzhiyun 		/*
82*4882a593Smuzhiyun 		 * Unlike the SPI4000, most SPI devices don't
83*4882a593Smuzhiyun 		 * automatically put on the L2 CRC. For everything
84*4882a593Smuzhiyun 		 * except for the SPI4000 have PKO append the L2 CRC
85*4882a593Smuzhiyun 		 * to the packet.
86*4882a593Smuzhiyun 		 */
87*4882a593Smuzhiyun 		enable.u64 = cvmx_read_csr(CVMX_PKO_REG_CRC_ENABLE);
88*4882a593Smuzhiyun 		enable.s.enable |= 0xffff << (interface * 16);
89*4882a593Smuzhiyun 		cvmx_write_csr(CVMX_PKO_REG_CRC_ENABLE, enable.u64);
90*4882a593Smuzhiyun 	}
91*4882a593Smuzhiyun 	__cvmx_helper_setup_gmx(interface, num_ports);
92*4882a593Smuzhiyun 	return num_ports;
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun /**
96*4882a593Smuzhiyun  * Bringup and enable a SPI interface. After this call packet I/O
97*4882a593Smuzhiyun  * should be fully functional. This is called with IPD enabled but
98*4882a593Smuzhiyun  * PKO disabled.
99*4882a593Smuzhiyun  *
100*4882a593Smuzhiyun  * @interface: Interface to bring up
101*4882a593Smuzhiyun  *
102*4882a593Smuzhiyun  * Returns Zero on success, negative on failure
103*4882a593Smuzhiyun  */
__cvmx_helper_spi_enable(int interface)104*4882a593Smuzhiyun int __cvmx_helper_spi_enable(int interface)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun 	/*
107*4882a593Smuzhiyun 	 * Normally the ethernet L2 CRC is checked and stripped in the
108*4882a593Smuzhiyun 	 * GMX block.  When you are using SPI, this isn' the case and
109*4882a593Smuzhiyun 	 * IPD needs to check the L2 CRC.
110*4882a593Smuzhiyun 	 */
111*4882a593Smuzhiyun 	int num_ports = cvmx_helper_ports_on_interface(interface);
112*4882a593Smuzhiyun 	int ipd_port;
113*4882a593Smuzhiyun 	for (ipd_port = interface * 16; ipd_port < interface * 16 + num_ports;
114*4882a593Smuzhiyun 	     ipd_port++) {
115*4882a593Smuzhiyun 		union cvmx_pip_prt_cfgx port_config;
116*4882a593Smuzhiyun 		port_config.u64 = cvmx_read_csr(CVMX_PIP_PRT_CFGX(ipd_port));
117*4882a593Smuzhiyun 		port_config.s.crc_en = 1;
118*4882a593Smuzhiyun 		cvmx_write_csr(CVMX_PIP_PRT_CFGX(ipd_port), port_config.u64);
119*4882a593Smuzhiyun 	}
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	if (cvmx_sysinfo_get()->board_type != CVMX_BOARD_TYPE_SIM) {
122*4882a593Smuzhiyun 		cvmx_spi_start_interface(interface, CVMX_SPI_MODE_DUPLEX,
123*4882a593Smuzhiyun 					 CVMX_HELPER_SPI_TIMEOUT, num_ports);
124*4882a593Smuzhiyun 		if (cvmx_spi4000_is_present(interface))
125*4882a593Smuzhiyun 			cvmx_spi4000_initialize(interface);
126*4882a593Smuzhiyun 	}
127*4882a593Smuzhiyun 	__cvmx_interrupt_spxx_int_msk_enable(interface);
128*4882a593Smuzhiyun 	__cvmx_interrupt_stxx_int_msk_enable(interface);
129*4882a593Smuzhiyun 	__cvmx_interrupt_gmxx_enable(interface);
130*4882a593Smuzhiyun 	return 0;
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun /**
134*4882a593Smuzhiyun  * Return the link state of an IPD/PKO port as returned by
135*4882a593Smuzhiyun  * auto negotiation. The result of this function may not match
136*4882a593Smuzhiyun  * Octeon's link config if auto negotiation has changed since
137*4882a593Smuzhiyun  * the last call to cvmx_helper_link_set().
138*4882a593Smuzhiyun  *
139*4882a593Smuzhiyun  * @ipd_port: IPD/PKO port to query
140*4882a593Smuzhiyun  *
141*4882a593Smuzhiyun  * Returns Link state
142*4882a593Smuzhiyun  */
__cvmx_helper_spi_link_get(int ipd_port)143*4882a593Smuzhiyun union cvmx_helper_link_info __cvmx_helper_spi_link_get(int ipd_port)
144*4882a593Smuzhiyun {
145*4882a593Smuzhiyun 	union cvmx_helper_link_info result;
146*4882a593Smuzhiyun 	int interface = cvmx_helper_get_interface_num(ipd_port);
147*4882a593Smuzhiyun 	int index = cvmx_helper_get_interface_index_num(ipd_port);
148*4882a593Smuzhiyun 	result.u64 = 0;
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 	if (cvmx_sysinfo_get()->board_type == CVMX_BOARD_TYPE_SIM) {
151*4882a593Smuzhiyun 		/* The simulator gives you a simulated full duplex link */
152*4882a593Smuzhiyun 		result.s.link_up = 1;
153*4882a593Smuzhiyun 		result.s.full_duplex = 1;
154*4882a593Smuzhiyun 		result.s.speed = 10000;
155*4882a593Smuzhiyun 	} else if (cvmx_spi4000_is_present(interface)) {
156*4882a593Smuzhiyun 		union cvmx_gmxx_rxx_rx_inbnd inband =
157*4882a593Smuzhiyun 		    cvmx_spi4000_check_speed(interface, index);
158*4882a593Smuzhiyun 		result.s.link_up = inband.s.status;
159*4882a593Smuzhiyun 		result.s.full_duplex = inband.s.duplex;
160*4882a593Smuzhiyun 		switch (inband.s.speed) {
161*4882a593Smuzhiyun 		case 0: /* 10 Mbps */
162*4882a593Smuzhiyun 			result.s.speed = 10;
163*4882a593Smuzhiyun 			break;
164*4882a593Smuzhiyun 		case 1: /* 100 Mbps */
165*4882a593Smuzhiyun 			result.s.speed = 100;
166*4882a593Smuzhiyun 			break;
167*4882a593Smuzhiyun 		case 2: /* 1 Gbps */
168*4882a593Smuzhiyun 			result.s.speed = 1000;
169*4882a593Smuzhiyun 			break;
170*4882a593Smuzhiyun 		case 3: /* Illegal */
171*4882a593Smuzhiyun 			result.s.speed = 0;
172*4882a593Smuzhiyun 			result.s.link_up = 0;
173*4882a593Smuzhiyun 			break;
174*4882a593Smuzhiyun 		}
175*4882a593Smuzhiyun 	} else {
176*4882a593Smuzhiyun 		/* For generic SPI we can't determine the link, just return some
177*4882a593Smuzhiyun 		   sane results */
178*4882a593Smuzhiyun 		result.s.link_up = 1;
179*4882a593Smuzhiyun 		result.s.full_duplex = 1;
180*4882a593Smuzhiyun 		result.s.speed = 10000;
181*4882a593Smuzhiyun 	}
182*4882a593Smuzhiyun 	return result;
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun /**
186*4882a593Smuzhiyun  * Configure an IPD/PKO port for the specified link state. This
187*4882a593Smuzhiyun  * function does not influence auto negotiation at the PHY level.
188*4882a593Smuzhiyun  * The passed link state must always match the link state returned
189*4882a593Smuzhiyun  * by cvmx_helper_link_get().
190*4882a593Smuzhiyun  *
191*4882a593Smuzhiyun  * @ipd_port:  IPD/PKO port to configure
192*4882a593Smuzhiyun  * @link_info: The new link state
193*4882a593Smuzhiyun  *
194*4882a593Smuzhiyun  * Returns Zero on success, negative on failure
195*4882a593Smuzhiyun  */
__cvmx_helper_spi_link_set(int ipd_port,union cvmx_helper_link_info link_info)196*4882a593Smuzhiyun int __cvmx_helper_spi_link_set(int ipd_port, union cvmx_helper_link_info link_info)
197*4882a593Smuzhiyun {
198*4882a593Smuzhiyun 	/* Nothing to do. If we have a SPI4000 then the setup was already performed
199*4882a593Smuzhiyun 	   by cvmx_spi4000_check_speed(). If not then there isn't any link
200*4882a593Smuzhiyun 	   info */
201*4882a593Smuzhiyun 	return 0;
202*4882a593Smuzhiyun }
203