18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (c) 2003-2014 Broadcom Corporation
38c2ecf20Sopenharmony_ci * All Rights Reserved
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * This software is available to you under a choice of one of two
68c2ecf20Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
78c2ecf20Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
88c2ecf20Sopenharmony_ci * COPYING in the main directory of this source tree, or the Broadcom
98c2ecf20Sopenharmony_ci * license below:
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
128c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions
138c2ecf20Sopenharmony_ci * are met:
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright
168c2ecf20Sopenharmony_ci *    notice, this list of conditions and the following disclaimer.
178c2ecf20Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright
188c2ecf20Sopenharmony_ci *    notice, this list of conditions and the following disclaimer in
198c2ecf20Sopenharmony_ci *    the documentation and/or other materials provided with the
208c2ecf20Sopenharmony_ci *    distribution.
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY BROADCOM ``AS IS'' AND ANY EXPRESS OR
238c2ecf20Sopenharmony_ci * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
248c2ecf20Sopenharmony_ci * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
258c2ecf20Sopenharmony_ci * ARE DISCLAIMED. IN NO EVENT SHALL BROADCOM OR CONTRIBUTORS BE LIABLE
268c2ecf20Sopenharmony_ci * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
278c2ecf20Sopenharmony_ci * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
288c2ecf20Sopenharmony_ci * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
298c2ecf20Sopenharmony_ci * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
308c2ecf20Sopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
318c2ecf20Sopenharmony_ci * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
328c2ecf20Sopenharmony_ci * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
338c2ecf20Sopenharmony_ci */
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h>
368c2ecf20Sopenharmony_ci#include <linux/kernel.h>
378c2ecf20Sopenharmony_ci#include <linux/delay.h>
388c2ecf20Sopenharmony_ci#include <linux/init.h>
398c2ecf20Sopenharmony_ci#include <linux/pci.h>
408c2ecf20Sopenharmony_ci#include <linux/irq.h>
418c2ecf20Sopenharmony_ci#include <linux/bitops.h>
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci#include <asm/cpu.h>
448c2ecf20Sopenharmony_ci#include <asm/mipsregs.h>
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci#include <asm/netlogic/haldefs.h>
478c2ecf20Sopenharmony_ci#include <asm/netlogic/xlp-hal/xlp.h>
488c2ecf20Sopenharmony_ci#include <asm/netlogic/common.h>
498c2ecf20Sopenharmony_ci#include <asm/netlogic/xlp-hal/iomap.h>
508c2ecf20Sopenharmony_ci#include <asm/netlogic/mips-extns.h>
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci#define SATA_CTL		0x0
538c2ecf20Sopenharmony_ci#define SATA_STATUS		0x1	/* Status Reg */
548c2ecf20Sopenharmony_ci#define SATA_INT		0x2	/* Interrupt Reg */
558c2ecf20Sopenharmony_ci#define SATA_INT_MASK		0x3	/* Interrupt Mask Reg */
568c2ecf20Sopenharmony_ci#define SATA_CR_REG_TIMER	0x4	/* PHY Conrol Timer Reg */
578c2ecf20Sopenharmony_ci#define SATA_CORE_ID		0x5	/* Core ID Reg */
588c2ecf20Sopenharmony_ci#define SATA_AXI_SLAVE_OPT1	0x6	/* AXI Slave Options Reg */
598c2ecf20Sopenharmony_ci#define SATA_PHY_LOS_LEV	0x7	/* PHY LOS Level Reg */
608c2ecf20Sopenharmony_ci#define SATA_PHY_MULTI		0x8	/* PHY Multiplier Reg */
618c2ecf20Sopenharmony_ci#define SATA_PHY_CLK_SEL	0x9	/* Clock Select Reg */
628c2ecf20Sopenharmony_ci#define SATA_PHY_AMP1_GEN1	0xa	/* PHY Transmit Amplitude Reg 1 */
638c2ecf20Sopenharmony_ci#define SATA_PHY_AMP1_GEN2	0xb	/* PHY Transmit Amplitude Reg 2 */
648c2ecf20Sopenharmony_ci#define SATA_PHY_AMP1_GEN3	0xc	/* PHY Transmit Amplitude Reg 3 */
658c2ecf20Sopenharmony_ci#define SATA_PHY_PRE1		0xd	/* PHY Transmit Preemphasis Reg 1 */
668c2ecf20Sopenharmony_ci#define SATA_PHY_PRE2		0xe	/* PHY Transmit Preemphasis Reg 2 */
678c2ecf20Sopenharmony_ci#define SATA_PHY_PRE3		0xf	/* PHY Transmit Preemphasis Reg 3 */
688c2ecf20Sopenharmony_ci#define SATA_SPDMODE		0x10	/* Speed Mode Reg */
698c2ecf20Sopenharmony_ci#define SATA_REFCLK		0x11	/* Reference Clock Control Reg */
708c2ecf20Sopenharmony_ci#define SATA_BYTE_SWAP_DIS	0x12	/* byte swap disable */
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci/*SATA_CTL Bits */
738c2ecf20Sopenharmony_ci#define SATA_RST_N		BIT(0)
748c2ecf20Sopenharmony_ci#define PHY0_RESET_N		BIT(16)
758c2ecf20Sopenharmony_ci#define PHY1_RESET_N		BIT(17)
768c2ecf20Sopenharmony_ci#define PHY2_RESET_N		BIT(18)
778c2ecf20Sopenharmony_ci#define PHY3_RESET_N		BIT(19)
788c2ecf20Sopenharmony_ci#define M_CSYSREQ		BIT(2)
798c2ecf20Sopenharmony_ci#define S_CSYSREQ		BIT(3)
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci/*SATA_STATUS Bits */
828c2ecf20Sopenharmony_ci#define P0_PHY_READY		BIT(4)
838c2ecf20Sopenharmony_ci#define P1_PHY_READY		BIT(5)
848c2ecf20Sopenharmony_ci#define P2_PHY_READY		BIT(6)
858c2ecf20Sopenharmony_ci#define P3_PHY_READY		BIT(7)
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci#define nlm_read_sata_reg(b, r)		nlm_read_reg(b, r)
888c2ecf20Sopenharmony_ci#define nlm_write_sata_reg(b, r, v)	nlm_write_reg(b, r, v)
898c2ecf20Sopenharmony_ci#define nlm_get_sata_pcibase(node)	\
908c2ecf20Sopenharmony_ci		nlm_pcicfg_base(XLP_IO_SATA_OFFSET(node))
918c2ecf20Sopenharmony_ci/* SATA device specific configuration registers are starts at 0x900 offset */
928c2ecf20Sopenharmony_ci#define nlm_get_sata_regbase(node)	\
938c2ecf20Sopenharmony_ci		(nlm_get_sata_pcibase(node) + 0x900)
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_cistatic void sata_clear_glue_reg(uint64_t regbase, uint32_t off, uint32_t bit)
968c2ecf20Sopenharmony_ci{
978c2ecf20Sopenharmony_ci	uint32_t reg_val;
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	reg_val = nlm_read_sata_reg(regbase, off);
1008c2ecf20Sopenharmony_ci	nlm_write_sata_reg(regbase, off, (reg_val & ~bit));
1018c2ecf20Sopenharmony_ci}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_cistatic void sata_set_glue_reg(uint64_t regbase, uint32_t off, uint32_t bit)
1048c2ecf20Sopenharmony_ci{
1058c2ecf20Sopenharmony_ci	uint32_t reg_val;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	reg_val = nlm_read_sata_reg(regbase, off);
1088c2ecf20Sopenharmony_ci	nlm_write_sata_reg(regbase, off, (reg_val | bit));
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cistatic void nlm_sata_firmware_init(int node)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	uint32_t reg_val;
1148c2ecf20Sopenharmony_ci	uint64_t regbase;
1158c2ecf20Sopenharmony_ci	int i;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	pr_info("XLP AHCI Initialization started.\n");
1188c2ecf20Sopenharmony_ci	regbase = nlm_get_sata_regbase(node);
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	/* Reset SATA */
1218c2ecf20Sopenharmony_ci	sata_clear_glue_reg(regbase, SATA_CTL, SATA_RST_N);
1228c2ecf20Sopenharmony_ci	/* Reset PHY */
1238c2ecf20Sopenharmony_ci	sata_clear_glue_reg(regbase, SATA_CTL,
1248c2ecf20Sopenharmony_ci			(PHY3_RESET_N | PHY2_RESET_N
1258c2ecf20Sopenharmony_ci			 | PHY1_RESET_N | PHY0_RESET_N));
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	/* Set SATA */
1288c2ecf20Sopenharmony_ci	sata_set_glue_reg(regbase, SATA_CTL, SATA_RST_N);
1298c2ecf20Sopenharmony_ci	/* Set PHY */
1308c2ecf20Sopenharmony_ci	sata_set_glue_reg(regbase, SATA_CTL,
1318c2ecf20Sopenharmony_ci			(PHY3_RESET_N | PHY2_RESET_N
1328c2ecf20Sopenharmony_ci			 | PHY1_RESET_N | PHY0_RESET_N));
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	pr_debug("Waiting for PHYs to come up.\n");
1358c2ecf20Sopenharmony_ci	i = 0;
1368c2ecf20Sopenharmony_ci	do {
1378c2ecf20Sopenharmony_ci		reg_val = nlm_read_sata_reg(regbase, SATA_STATUS);
1388c2ecf20Sopenharmony_ci		i++;
1398c2ecf20Sopenharmony_ci	} while (((reg_val & 0xF0) != 0xF0) && (i < 10000));
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	for (i = 0; i < 4; i++) {
1428c2ecf20Sopenharmony_ci		if (reg_val  & (P0_PHY_READY << i))
1438c2ecf20Sopenharmony_ci			pr_info("PHY%d is up.\n", i);
1448c2ecf20Sopenharmony_ci		else
1458c2ecf20Sopenharmony_ci			pr_info("PHY%d is down.\n", i);
1468c2ecf20Sopenharmony_ci	}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	pr_info("XLP AHCI init done.\n");
1498c2ecf20Sopenharmony_ci}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_cistatic int __init nlm_ahci_init(void)
1528c2ecf20Sopenharmony_ci{
1538c2ecf20Sopenharmony_ci	int node = 0;
1548c2ecf20Sopenharmony_ci	int chip = read_c0_prid() & PRID_IMP_MASK;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	if (chip == PRID_IMP_NETLOGIC_XLP3XX)
1578c2ecf20Sopenharmony_ci		nlm_sata_firmware_init(node);
1588c2ecf20Sopenharmony_ci	return 0;
1598c2ecf20Sopenharmony_ci}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_cistatic void nlm_sata_intr_ack(struct irq_data *data)
1628c2ecf20Sopenharmony_ci{
1638c2ecf20Sopenharmony_ci	uint32_t val = 0;
1648c2ecf20Sopenharmony_ci	uint64_t regbase;
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	regbase = nlm_get_sata_regbase(nlm_nodeid());
1678c2ecf20Sopenharmony_ci	val = nlm_read_sata_reg(regbase, SATA_INT);
1688c2ecf20Sopenharmony_ci	sata_set_glue_reg(regbase, SATA_INT, val);
1698c2ecf20Sopenharmony_ci}
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_cistatic void nlm_sata_fixup_bar(struct pci_dev *dev)
1728c2ecf20Sopenharmony_ci{
1738c2ecf20Sopenharmony_ci	/*
1748c2ecf20Sopenharmony_ci	 * The AHCI resource is in BAR 0, move it to
1758c2ecf20Sopenharmony_ci	 * BAR 5, where it is expected
1768c2ecf20Sopenharmony_ci	 */
1778c2ecf20Sopenharmony_ci	dev->resource[5] = dev->resource[0];
1788c2ecf20Sopenharmony_ci	memset(&dev->resource[0], 0, sizeof(dev->resource[0]));
1798c2ecf20Sopenharmony_ci}
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_cistatic void nlm_sata_fixup_final(struct pci_dev *dev)
1828c2ecf20Sopenharmony_ci{
1838c2ecf20Sopenharmony_ci	uint32_t val;
1848c2ecf20Sopenharmony_ci	uint64_t regbase;
1858c2ecf20Sopenharmony_ci	int node = 0; /* XLP3XX does not support multi-node */
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	regbase = nlm_get_sata_regbase(node);
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	/* clear pending interrupts and then enable them */
1908c2ecf20Sopenharmony_ci	val = nlm_read_sata_reg(regbase, SATA_INT);
1918c2ecf20Sopenharmony_ci	sata_set_glue_reg(regbase, SATA_INT, val);
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	/* Mask the core interrupt. If all the interrupts
1948c2ecf20Sopenharmony_ci	 * are enabled there are spurious interrupt flow
1958c2ecf20Sopenharmony_ci	 * happening, to avoid only enable core interrupt
1968c2ecf20Sopenharmony_ci	 * mask.
1978c2ecf20Sopenharmony_ci	 */
1988c2ecf20Sopenharmony_ci	sata_set_glue_reg(regbase, SATA_INT_MASK, 0x1);
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	dev->irq = PIC_SATA_IRQ;
2018c2ecf20Sopenharmony_ci	nlm_set_pic_extra_ack(node, PIC_SATA_IRQ, nlm_sata_intr_ack);
2028c2ecf20Sopenharmony_ci}
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ciarch_initcall(nlm_ahci_init);
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ciDECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_NETLOGIC, PCI_DEVICE_ID_NLM_SATA,
2078c2ecf20Sopenharmony_ci		nlm_sata_fixup_bar);
2088c2ecf20Sopenharmony_ciDECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_NETLOGIC, PCI_DEVICE_ID_NLM_SATA,
2098c2ecf20Sopenharmony_ci		nlm_sata_fixup_final);
210