18c2ecf20Sopenharmony_ci/***********************license start***************
28c2ecf20Sopenharmony_ci * Author: Cavium Networks
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Contact: support@caviumnetworks.com
58c2ecf20Sopenharmony_ci * This file is part of the OCTEON SDK
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Copyright (C) 2003-2018 Cavium, Inc.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * This file is free software; you can redistribute it and/or modify
108c2ecf20Sopenharmony_ci * it under the terms of the GNU General Public License, Version 2, as
118c2ecf20Sopenharmony_ci * published by the Free Software Foundation.
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * This file is distributed in the hope that it will be useful, but
148c2ecf20Sopenharmony_ci * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
158c2ecf20Sopenharmony_ci * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
168c2ecf20Sopenharmony_ci * NONINFRINGEMENT.  See the GNU General Public License for more
178c2ecf20Sopenharmony_ci * details.
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * You should have received a copy of the GNU General Public License
208c2ecf20Sopenharmony_ci * along with this file; if not, write to the Free Software
218c2ecf20Sopenharmony_ci * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
228c2ecf20Sopenharmony_ci * or visit http://www.gnu.org/licenses/.
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci * This file may also be available under a different license from Cavium.
258c2ecf20Sopenharmony_ci * Contact Cavium Networks for more information
268c2ecf20Sopenharmony_ci ***********************license end**************************************/
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/*
298c2ecf20Sopenharmony_ci * Functions for SPI initialization, configuration,
308c2ecf20Sopenharmony_ci * and monitoring.
318c2ecf20Sopenharmony_ci */
328c2ecf20Sopenharmony_ci#include <asm/octeon/octeon.h>
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#include <asm/octeon/cvmx-config.h>
358c2ecf20Sopenharmony_ci#include <asm/octeon/cvmx-spi.h>
368c2ecf20Sopenharmony_ci#include <asm/octeon/cvmx-helper.h>
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#include <asm/octeon/cvmx-pip-defs.h>
398c2ecf20Sopenharmony_ci#include <asm/octeon/cvmx-pko-defs.h>
408c2ecf20Sopenharmony_ci#include <asm/octeon/cvmx-spxx-defs.h>
418c2ecf20Sopenharmony_ci#include <asm/octeon/cvmx-stxx-defs.h>
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci/*
448c2ecf20Sopenharmony_ci * CVMX_HELPER_SPI_TIMEOUT is used to determine how long the SPI
458c2ecf20Sopenharmony_ci * initialization routines wait for SPI training. You can override the
468c2ecf20Sopenharmony_ci * value using executive-config.h if necessary.
478c2ecf20Sopenharmony_ci */
488c2ecf20Sopenharmony_ci#ifndef CVMX_HELPER_SPI_TIMEOUT
498c2ecf20Sopenharmony_ci#define CVMX_HELPER_SPI_TIMEOUT 10
508c2ecf20Sopenharmony_ci#endif
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ciint __cvmx_helper_spi_enumerate(int interface)
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	if ((cvmx_sysinfo_get()->board_type != CVMX_BOARD_TYPE_SIM) &&
558c2ecf20Sopenharmony_ci	    cvmx_spi4000_is_present(interface)) {
568c2ecf20Sopenharmony_ci		return 10;
578c2ecf20Sopenharmony_ci	} else {
588c2ecf20Sopenharmony_ci		return 16;
598c2ecf20Sopenharmony_ci	}
608c2ecf20Sopenharmony_ci}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci/**
638c2ecf20Sopenharmony_ci * Probe a SPI interface and determine the number of ports
648c2ecf20Sopenharmony_ci * connected to it. The SPI interface should still be down after
658c2ecf20Sopenharmony_ci * this call.
668c2ecf20Sopenharmony_ci *
678c2ecf20Sopenharmony_ci * @interface: Interface to probe
688c2ecf20Sopenharmony_ci *
698c2ecf20Sopenharmony_ci * Returns Number of ports on the interface. Zero to disable.
708c2ecf20Sopenharmony_ci */
718c2ecf20Sopenharmony_ciint __cvmx_helper_spi_probe(int interface)
728c2ecf20Sopenharmony_ci{
738c2ecf20Sopenharmony_ci	int num_ports = 0;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	if ((cvmx_sysinfo_get()->board_type != CVMX_BOARD_TYPE_SIM) &&
768c2ecf20Sopenharmony_ci	    cvmx_spi4000_is_present(interface)) {
778c2ecf20Sopenharmony_ci		num_ports = 10;
788c2ecf20Sopenharmony_ci	} else {
798c2ecf20Sopenharmony_ci		union cvmx_pko_reg_crc_enable enable;
808c2ecf20Sopenharmony_ci		num_ports = 16;
818c2ecf20Sopenharmony_ci		/*
828c2ecf20Sopenharmony_ci		 * Unlike the SPI4000, most SPI devices don't
838c2ecf20Sopenharmony_ci		 * automatically put on the L2 CRC. For everything
848c2ecf20Sopenharmony_ci		 * except for the SPI4000 have PKO append the L2 CRC
858c2ecf20Sopenharmony_ci		 * to the packet.
868c2ecf20Sopenharmony_ci		 */
878c2ecf20Sopenharmony_ci		enable.u64 = cvmx_read_csr(CVMX_PKO_REG_CRC_ENABLE);
888c2ecf20Sopenharmony_ci		enable.s.enable |= 0xffff << (interface * 16);
898c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_PKO_REG_CRC_ENABLE, enable.u64);
908c2ecf20Sopenharmony_ci	}
918c2ecf20Sopenharmony_ci	__cvmx_helper_setup_gmx(interface, num_ports);
928c2ecf20Sopenharmony_ci	return num_ports;
938c2ecf20Sopenharmony_ci}
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci/**
968c2ecf20Sopenharmony_ci * Bringup and enable a SPI interface. After this call packet I/O
978c2ecf20Sopenharmony_ci * should be fully functional. This is called with IPD enabled but
988c2ecf20Sopenharmony_ci * PKO disabled.
998c2ecf20Sopenharmony_ci *
1008c2ecf20Sopenharmony_ci * @interface: Interface to bring up
1018c2ecf20Sopenharmony_ci *
1028c2ecf20Sopenharmony_ci * Returns Zero on success, negative on failure
1038c2ecf20Sopenharmony_ci */
1048c2ecf20Sopenharmony_ciint __cvmx_helper_spi_enable(int interface)
1058c2ecf20Sopenharmony_ci{
1068c2ecf20Sopenharmony_ci	/*
1078c2ecf20Sopenharmony_ci	 * Normally the ethernet L2 CRC is checked and stripped in the
1088c2ecf20Sopenharmony_ci	 * GMX block.  When you are using SPI, this isn' the case and
1098c2ecf20Sopenharmony_ci	 * IPD needs to check the L2 CRC.
1108c2ecf20Sopenharmony_ci	 */
1118c2ecf20Sopenharmony_ci	int num_ports = cvmx_helper_ports_on_interface(interface);
1128c2ecf20Sopenharmony_ci	int ipd_port;
1138c2ecf20Sopenharmony_ci	for (ipd_port = interface * 16; ipd_port < interface * 16 + num_ports;
1148c2ecf20Sopenharmony_ci	     ipd_port++) {
1158c2ecf20Sopenharmony_ci		union cvmx_pip_prt_cfgx port_config;
1168c2ecf20Sopenharmony_ci		port_config.u64 = cvmx_read_csr(CVMX_PIP_PRT_CFGX(ipd_port));
1178c2ecf20Sopenharmony_ci		port_config.s.crc_en = 1;
1188c2ecf20Sopenharmony_ci		cvmx_write_csr(CVMX_PIP_PRT_CFGX(ipd_port), port_config.u64);
1198c2ecf20Sopenharmony_ci	}
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	if (cvmx_sysinfo_get()->board_type != CVMX_BOARD_TYPE_SIM) {
1228c2ecf20Sopenharmony_ci		cvmx_spi_start_interface(interface, CVMX_SPI_MODE_DUPLEX,
1238c2ecf20Sopenharmony_ci					 CVMX_HELPER_SPI_TIMEOUT, num_ports);
1248c2ecf20Sopenharmony_ci		if (cvmx_spi4000_is_present(interface))
1258c2ecf20Sopenharmony_ci			cvmx_spi4000_initialize(interface);
1268c2ecf20Sopenharmony_ci	}
1278c2ecf20Sopenharmony_ci	__cvmx_interrupt_spxx_int_msk_enable(interface);
1288c2ecf20Sopenharmony_ci	__cvmx_interrupt_stxx_int_msk_enable(interface);
1298c2ecf20Sopenharmony_ci	__cvmx_interrupt_gmxx_enable(interface);
1308c2ecf20Sopenharmony_ci	return 0;
1318c2ecf20Sopenharmony_ci}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci/**
1348c2ecf20Sopenharmony_ci * Return the link state of an IPD/PKO port as returned by
1358c2ecf20Sopenharmony_ci * auto negotiation. The result of this function may not match
1368c2ecf20Sopenharmony_ci * Octeon's link config if auto negotiation has changed since
1378c2ecf20Sopenharmony_ci * the last call to cvmx_helper_link_set().
1388c2ecf20Sopenharmony_ci *
1398c2ecf20Sopenharmony_ci * @ipd_port: IPD/PKO port to query
1408c2ecf20Sopenharmony_ci *
1418c2ecf20Sopenharmony_ci * Returns Link state
1428c2ecf20Sopenharmony_ci */
1438c2ecf20Sopenharmony_ciunion cvmx_helper_link_info __cvmx_helper_spi_link_get(int ipd_port)
1448c2ecf20Sopenharmony_ci{
1458c2ecf20Sopenharmony_ci	union cvmx_helper_link_info result;
1468c2ecf20Sopenharmony_ci	int interface = cvmx_helper_get_interface_num(ipd_port);
1478c2ecf20Sopenharmony_ci	int index = cvmx_helper_get_interface_index_num(ipd_port);
1488c2ecf20Sopenharmony_ci	result.u64 = 0;
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	if (cvmx_sysinfo_get()->board_type == CVMX_BOARD_TYPE_SIM) {
1518c2ecf20Sopenharmony_ci		/* The simulator gives you a simulated full duplex link */
1528c2ecf20Sopenharmony_ci		result.s.link_up = 1;
1538c2ecf20Sopenharmony_ci		result.s.full_duplex = 1;
1548c2ecf20Sopenharmony_ci		result.s.speed = 10000;
1558c2ecf20Sopenharmony_ci	} else if (cvmx_spi4000_is_present(interface)) {
1568c2ecf20Sopenharmony_ci		union cvmx_gmxx_rxx_rx_inbnd inband =
1578c2ecf20Sopenharmony_ci		    cvmx_spi4000_check_speed(interface, index);
1588c2ecf20Sopenharmony_ci		result.s.link_up = inband.s.status;
1598c2ecf20Sopenharmony_ci		result.s.full_duplex = inband.s.duplex;
1608c2ecf20Sopenharmony_ci		switch (inband.s.speed) {
1618c2ecf20Sopenharmony_ci		case 0: /* 10 Mbps */
1628c2ecf20Sopenharmony_ci			result.s.speed = 10;
1638c2ecf20Sopenharmony_ci			break;
1648c2ecf20Sopenharmony_ci		case 1: /* 100 Mbps */
1658c2ecf20Sopenharmony_ci			result.s.speed = 100;
1668c2ecf20Sopenharmony_ci			break;
1678c2ecf20Sopenharmony_ci		case 2: /* 1 Gbps */
1688c2ecf20Sopenharmony_ci			result.s.speed = 1000;
1698c2ecf20Sopenharmony_ci			break;
1708c2ecf20Sopenharmony_ci		case 3: /* Illegal */
1718c2ecf20Sopenharmony_ci			result.s.speed = 0;
1728c2ecf20Sopenharmony_ci			result.s.link_up = 0;
1738c2ecf20Sopenharmony_ci			break;
1748c2ecf20Sopenharmony_ci		}
1758c2ecf20Sopenharmony_ci	} else {
1768c2ecf20Sopenharmony_ci		/* For generic SPI we can't determine the link, just return some
1778c2ecf20Sopenharmony_ci		   sane results */
1788c2ecf20Sopenharmony_ci		result.s.link_up = 1;
1798c2ecf20Sopenharmony_ci		result.s.full_duplex = 1;
1808c2ecf20Sopenharmony_ci		result.s.speed = 10000;
1818c2ecf20Sopenharmony_ci	}
1828c2ecf20Sopenharmony_ci	return result;
1838c2ecf20Sopenharmony_ci}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci/**
1868c2ecf20Sopenharmony_ci * Configure an IPD/PKO port for the specified link state. This
1878c2ecf20Sopenharmony_ci * function does not influence auto negotiation at the PHY level.
1888c2ecf20Sopenharmony_ci * The passed link state must always match the link state returned
1898c2ecf20Sopenharmony_ci * by cvmx_helper_link_get().
1908c2ecf20Sopenharmony_ci *
1918c2ecf20Sopenharmony_ci * @ipd_port:  IPD/PKO port to configure
1928c2ecf20Sopenharmony_ci * @link_info: The new link state
1938c2ecf20Sopenharmony_ci *
1948c2ecf20Sopenharmony_ci * Returns Zero on success, negative on failure
1958c2ecf20Sopenharmony_ci */
1968c2ecf20Sopenharmony_ciint __cvmx_helper_spi_link_set(int ipd_port, union cvmx_helper_link_info link_info)
1978c2ecf20Sopenharmony_ci{
1988c2ecf20Sopenharmony_ci	/* Nothing to do. If we have a SPI4000 then the setup was already performed
1998c2ecf20Sopenharmony_ci	   by cvmx_spi4000_check_speed(). If not then there isn't any link
2008c2ecf20Sopenharmony_ci	   info */
2018c2ecf20Sopenharmony_ci	return 0;
2028c2ecf20Sopenharmony_ci}
203