2 * Copyright (c) 2003-2014 Broadcom Corporation
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the Broadcom
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
22 * THIS SOFTWARE IS PROVIDED BY BROADCOM ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL BROADCOM OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
29 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
31 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
32 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <linux/dma-mapping.h>
36 #include <linux/kernel.h>
37 #include <linux/delay.h>
38 #include <linux/init.h>
39 #include <linux/pci.h>
40 #include <linux/irq.h>
41 #include <linux/bitops.h>
42 #include <linux/pci_ids.h>
43 #include <linux/nodemask.h>
46 #include <asm/mipsregs.h>
48 #include <asm/netlogic/common.h>
49 #include <asm/netlogic/haldefs.h>
50 #include <asm/netlogic/mips-extns.h>
51 #include <asm/netlogic/xlp-hal/xlp.h>
52 #include <asm/netlogic/xlp-hal/iomap.h>
55 #define SATA_STATUS 0x1 /* Status Reg */
56 #define SATA_INT 0x2 /* Interrupt Reg */
57 #define SATA_INT_MASK 0x3 /* Interrupt Mask Reg */
58 #define SATA_BIU_TIMEOUT 0x4
59 #define AXIWRSPERRLOG 0x5
60 #define AXIRDSPERRLOG 0x6
61 #define BiuTimeoutLow 0x7
62 #define BiuTimeoutHi 0x8
63 #define BiuSlvErLow 0x9
64 #define BiuSlvErHi 0xa
65 #define IO_CONFIG_SWAP_DIS 0xb
66 #define CR_REG_TIMER 0xc
68 #define AXI_SLAVE_OPT1 0xe
69 #define PHY_MEM_ACCESS 0xf
70 #define PHY0_CNTRL 0x10
71 #define PHY0_STAT 0x11
72 #define PHY0_RX_ALIGN 0x12
73 #define PHY0_RX_EQ_LO 0x13
74 #define PHY0_RX_EQ_HI 0x14
75 #define PHY0_BIST_LOOP 0x15
76 #define PHY1_CNTRL 0x16
77 #define PHY1_STAT 0x17
78 #define PHY1_RX_ALIGN 0x18
79 #define PHY1_RX_EQ_LO 0x19
80 #define PHY1_RX_EQ_HI 0x1a
81 #define PHY1_BIST_LOOP 0x1b
83 #define RdExLimit 0x1d
84 #define CacheAllocBase 0x1e
85 #define CacheAllocLimit 0x1f
86 #define BiuSlaveCmdGstNum 0x20
89 #define SATA_RST_N BIT(0) /* Active low reset sata_core phy */
90 #define SataCtlReserve0 BIT(1)
91 #define M_CSYSREQ BIT(2) /* AXI master low power, not used */
92 #define S_CSYSREQ BIT(3) /* AXI slave low power, not used */
93 #define P0_CP_DET BIT(8) /* Reserved, bring in from pad */
94 #define P0_MP_SW BIT(9) /* Mech Switch */
95 #define P0_DISABLE BIT(10) /* disable p0 */
96 #define P0_ACT_LED_EN BIT(11) /* Active LED enable */
97 #define P0_IRST_HARD_SYNTH BIT(12) /* PHY hard synth reset */
98 #define P0_IRST_HARD_TXRX BIT(13) /* PHY lane hard reset */
99 #define P0_IRST_POR BIT(14) /* PHY power on reset*/
100 #define P0_IPDTXL BIT(15) /* PHY Tx lane dis/power down */
101 #define P0_IPDRXL BIT(16) /* PHY Rx lane dis/power down */
102 #define P0_IPDIPDMSYNTH BIT(17) /* PHY synthesizer dis/porwer down */
103 #define P0_CP_POD_EN BIT(18) /* CP_POD enable */
104 #define P0_AT_BYPASS BIT(19) /* P0 address translation by pass */
105 #define P1_CP_DET BIT(20) /* Reserved,Cold Detect */
106 #define P1_MP_SW BIT(21) /* Mech Switch */
107 #define P1_DISABLE BIT(22) /* disable p1 */
108 #define P1_ACT_LED_EN BIT(23) /* Active LED enable */
109 #define P1_IRST_HARD_SYNTH BIT(24) /* PHY hard synth reset */
110 #define P1_IRST_HARD_TXRX BIT(25) /* PHY lane hard reset */
111 #define P1_IRST_POR BIT(26) /* PHY power on reset*/
112 #define P1_IPDTXL BIT(27) /* PHY Tx lane dis/porwer down */
113 #define P1_IPDRXL BIT(28) /* PHY Rx lane dis/porwer down */
114 #define P1_IPDIPDMSYNTH BIT(29) /* PHY synthesizer dis/porwer down */
115 #define P1_CP_POD_EN BIT(30)
116 #define P1_AT_BYPASS BIT(31) /* P1 address translation by pass */
118 /* Status register */
119 #define M_CACTIVE BIT(0) /* m_cactive, not used */
120 #define S_CACTIVE BIT(1) /* s_cactive, not used */
121 #define P0_PHY_READY BIT(8) /* phy is ready */
122 #define P0_CP_POD BIT(9) /* Cold PowerOn */
123 #define P0_SLUMBER BIT(10) /* power mode slumber */
124 #define P0_PATIAL BIT(11) /* power mode patial */
125 #define P0_PHY_SIG_DET BIT(12) /* phy dignal detect */
126 #define P0_PHY_CALI BIT(13) /* phy calibration done */
127 #define P1_PHY_READY BIT(16) /* phy is ready */
128 #define P1_CP_POD BIT(17) /* Cold PowerOn */
129 #define P1_SLUMBER BIT(18) /* power mode slumber */
130 #define P1_PATIAL BIT(19) /* power mode patial */
131 #define P1_PHY_SIG_DET BIT(20) /* phy dignal detect */
132 #define P1_PHY_CALI BIT(21) /* phy calibration done */
134 /* SATA CR_REG_TIMER bits */
135 #define CR_TIME_SCALE (0x1000 << 0)
137 /* SATA PHY specific registers start and end address */
138 #define RXCDRCALFOSC0 0x0065
139 #define CALDUTY 0x006e
140 #define RXDPIF 0x8065
141 #define PPMDRIFTMAX_HI 0x80A4
143 #define nlm_read_sata_reg(b, r) nlm_read_reg(b, r)
144 #define nlm_write_sata_reg(b, r, v) nlm_write_reg(b, r, v)
145 #define nlm_get_sata_pcibase(node) \
146 nlm_pcicfg_base(XLP9XX_IO_SATA_OFFSET(node))
147 #define nlm_get_sata_regbase(node) \
148 (nlm_get_sata_pcibase(node) + 0x100)
150 /* SATA PHY config for register block 1 0x0065 .. 0x006e */
151 static const u8 sata_phy_config1[] = {
152 0xC9, 0xC9, 0x07, 0x07, 0x18, 0x18, 0x01, 0x01, 0x22, 0x00
155 /* SATA PHY config for register block 2 0x0x8065 .. 0x0x80A4 */
156 static const u8 sata_phy_config2[] = {
157 0xAA, 0x00, 0x4C, 0xC9, 0xC9, 0x07, 0x07, 0x18,
158 0x18, 0x05, 0x0C, 0x10, 0x00, 0x10, 0x00, 0xFF,
159 0xCF, 0xF7, 0xE1, 0xF5, 0xFD, 0xFD, 0xFF, 0xFF,
160 0xFF, 0xFF, 0xE3, 0xE7, 0xDB, 0xF5, 0xFD, 0xFD,
161 0xF5, 0xF5, 0xFF, 0xFF, 0xE3, 0xE7, 0xDB, 0xF5,
162 0xFD, 0xFD, 0xF5, 0xF5, 0xFF, 0xFF, 0xFF, 0xF5,
163 0x3F, 0x00, 0x32, 0x00, 0x03, 0x01, 0x05, 0x05,
164 0x04, 0x00, 0x00, 0x08, 0x04, 0x00, 0x00, 0x04,
167 const int sata_phy_debug = 0; /* set to verify PHY writes */
169 static void sata_clear_glue_reg(u64 regbase, u32 off, u32 bit)
173 reg_val = nlm_read_sata_reg(regbase, off);
174 nlm_write_sata_reg(regbase, off, (reg_val & ~bit));
177 static void sata_set_glue_reg(u64 regbase, u32 off, u32 bit)
181 reg_val = nlm_read_sata_reg(regbase, off);
182 nlm_write_sata_reg(regbase, off, (reg_val | bit));
185 static void write_phy_reg(u64 regbase, u32 addr, u32 physel, u8 data)
187 nlm_write_sata_reg(regbase, PHY_MEM_ACCESS,
188 (1u << 31) | (physel << 24) | (data << 16) | addr);
192 static u8 read_phy_reg(u64 regbase, u32 addr, u32 physel)
196 nlm_write_sata_reg(regbase, PHY_MEM_ACCESS,
197 (0 << 31) | (physel << 24) | (0 << 16) | addr);
199 val = nlm_read_sata_reg(regbase, PHY_MEM_ACCESS);
200 return (val >> 16) & 0xff;
203 static void config_sata_phy(u64 regbase)
207 for (port = 0; port < 2; port++) {
208 for (i = 0, reg = RXCDRCALFOSC0; reg <= CALDUTY; reg++, i++)
209 write_phy_reg(regbase, reg, port, sata_phy_config1[i]);
211 for (i = 0, reg = RXDPIF; reg <= PPMDRIFTMAX_HI; reg++, i++)
212 write_phy_reg(regbase, reg, port, sata_phy_config2[i]);
216 static void check_phy_register(u64 regbase, u32 addr, u32 physel, u8 xdata)
220 data = read_phy_reg(regbase, addr, physel);
221 pr_info("PHY read addr = 0x%x physel = %d data = 0x%x %s\n",
222 addr, physel, data, data == xdata ? "TRUE" : "FALSE");
225 static void verify_sata_phy_config(u64 regbase)
229 for (port = 0; port < 2; port++) {
230 for (i = 0, reg = RXCDRCALFOSC0; reg <= CALDUTY; reg++, i++)
231 check_phy_register(regbase, reg, port,
232 sata_phy_config1[i]);
234 for (i = 0, reg = RXDPIF; reg <= PPMDRIFTMAX_HI; reg++, i++)
235 check_phy_register(regbase, reg, port,
236 sata_phy_config2[i]);
240 static void nlm_sata_firmware_init(int node)
246 pr_info("Initializing XLP9XX On-chip AHCI...\n");
247 regbase = nlm_get_sata_regbase(node);
250 sata_clear_glue_reg(regbase, SATA_CTL, P0_IRST_POR);
251 sata_clear_glue_reg(regbase, SATA_CTL, P0_IRST_HARD_TXRX);
252 sata_clear_glue_reg(regbase, SATA_CTL, P0_IRST_HARD_SYNTH);
253 sata_clear_glue_reg(regbase, SATA_CTL, P0_IPDTXL);
254 sata_clear_glue_reg(regbase, SATA_CTL, P0_IPDRXL);
255 sata_clear_glue_reg(regbase, SATA_CTL, P0_IPDIPDMSYNTH);
258 sata_clear_glue_reg(regbase, SATA_CTL, P1_IRST_POR);
259 sata_clear_glue_reg(regbase, SATA_CTL, P1_IRST_HARD_TXRX);
260 sata_clear_glue_reg(regbase, SATA_CTL, P1_IRST_HARD_SYNTH);
261 sata_clear_glue_reg(regbase, SATA_CTL, P1_IPDTXL);
262 sata_clear_glue_reg(regbase, SATA_CTL, P1_IPDRXL);
263 sata_clear_glue_reg(regbase, SATA_CTL, P1_IPDIPDMSYNTH);
267 sata_set_glue_reg(regbase, SATA_CTL, P0_IPDTXL);
268 sata_set_glue_reg(regbase, SATA_CTL, P0_IPDRXL);
269 sata_set_glue_reg(regbase, SATA_CTL, P0_IPDIPDMSYNTH);
270 sata_set_glue_reg(regbase, SATA_CTL, P1_IPDTXL);
271 sata_set_glue_reg(regbase, SATA_CTL, P1_IPDRXL);
272 sata_set_glue_reg(regbase, SATA_CTL, P1_IPDIPDMSYNTH);
275 sata_set_glue_reg(regbase, SATA_CTL, P0_IRST_POR);
277 sata_set_glue_reg(regbase, SATA_CTL, P1_IRST_POR);
281 config_sata_phy(regbase);
283 verify_sata_phy_config(regbase);
286 sata_set_glue_reg(regbase, SATA_CTL, P0_IRST_HARD_TXRX);
287 sata_set_glue_reg(regbase, SATA_CTL, P0_IRST_HARD_SYNTH);
288 sata_set_glue_reg(regbase, SATA_CTL, P1_IRST_HARD_TXRX);
289 sata_set_glue_reg(regbase, SATA_CTL, P1_IRST_HARD_SYNTH);
292 /* Override reset in serial PHY mode */
293 sata_set_glue_reg(regbase, CR_REG_TIMER, CR_TIME_SCALE);
295 sata_set_glue_reg(regbase, SATA_CTL, SATA_RST_N);
296 sata_set_glue_reg(regbase, SATA_CTL, M_CSYSREQ);
297 sata_set_glue_reg(regbase, SATA_CTL, S_CSYSREQ);
299 pr_debug("Waiting for PHYs to come up.\n");
302 reg_val = nlm_read_sata_reg(regbase, SATA_STATUS);
303 if ((reg_val & P1_PHY_READY) && (reg_val & P0_PHY_READY))
308 if (reg_val & P0_PHY_READY)
309 pr_info("PHY0 is up.\n");
311 pr_info("PHY0 is down.\n");
312 if (reg_val & P1_PHY_READY)
313 pr_info("PHY1 is up.\n");
315 pr_info("PHY1 is down.\n");
317 pr_info("XLP AHCI Init Done.\n");
320 static int __init nlm_ahci_init(void)
324 if (!cpu_is_xlp9xx())
326 for (node = 0; node < NLM_NR_NODES; node++)
327 if (nlm_node_present(node))
328 nlm_sata_firmware_init(node);
332 static void nlm_sata_intr_ack(struct irq_data *data)
338 node = data->irq / NLM_IRQS_PER_NODE;
339 regbase = nlm_get_sata_regbase(node);
340 val = nlm_read_sata_reg(regbase, SATA_INT);
341 sata_set_glue_reg(regbase, SATA_INT, val);
344 static void nlm_sata_fixup_bar(struct pci_dev *dev)
346 dev->resource[5] = dev->resource[0];
347 memset(&dev->resource[0], 0, sizeof(dev->resource[0]));
350 static void nlm_sata_fixup_final(struct pci_dev *dev)
356 /* Find end bridge function to find node */
357 node = xlp_socdev_to_node(dev);
358 regbase = nlm_get_sata_regbase(node);
360 /* clear pending interrupts and then enable them */
361 val = nlm_read_sata_reg(regbase, SATA_INT);
362 sata_set_glue_reg(regbase, SATA_INT, val);
364 /* Enable only the core interrupt */
365 sata_set_glue_reg(regbase, SATA_INT_MASK, 0x1);
367 dev->irq = nlm_irq_to_xirq(node, PIC_SATA_IRQ);
368 nlm_set_pic_extra_ack(node, PIC_SATA_IRQ, nlm_sata_intr_ack);
371 arch_initcall(nlm_ahci_init);
373 DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_XLP9XX_SATA,
376 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_XLP9XX_SATA,
377 nlm_sata_fixup_final);