1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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>
44
45#include <asm/cpu.h>
46#include <asm/mipsregs.h>
47
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>
53
54#define SATA_CTL 0x0
55#define SATA_STATUS 0x1
56#define SATA_INT 0x2
57#define SATA_INT_MASK 0x3
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
67#define CORE_ID 0xd
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
82#define RdExBase 0x1c
83#define RdExLimit 0x1d
84#define CacheAllocBase 0x1e
85#define CacheAllocLimit 0x1f
86#define BiuSlaveCmdGstNum 0x20
87
88
89#define SATA_RST_N BIT(0)
90#define SataCtlReserve0 BIT(1)
91#define M_CSYSREQ BIT(2)
92#define S_CSYSREQ BIT(3)
93#define P0_CP_DET BIT(8)
94#define P0_MP_SW BIT(9)
95#define P0_DISABLE BIT(10)
96#define P0_ACT_LED_EN BIT(11)
97#define P0_IRST_HARD_SYNTH BIT(12)
98#define P0_IRST_HARD_TXRX BIT(13)
99#define P0_IRST_POR BIT(14)
100#define P0_IPDTXL BIT(15)
101#define P0_IPDRXL BIT(16)
102#define P0_IPDIPDMSYNTH BIT(17)
103#define P0_CP_POD_EN BIT(18)
104#define P0_AT_BYPASS BIT(19)
105#define P1_CP_DET BIT(20)
106#define P1_MP_SW BIT(21)
107#define P1_DISABLE BIT(22)
108#define P1_ACT_LED_EN BIT(23)
109#define P1_IRST_HARD_SYNTH BIT(24)
110#define P1_IRST_HARD_TXRX BIT(25)
111#define P1_IRST_POR BIT(26)
112#define P1_IPDTXL BIT(27)
113#define P1_IPDRXL BIT(28)
114#define P1_IPDIPDMSYNTH BIT(29)
115#define P1_CP_POD_EN BIT(30)
116#define P1_AT_BYPASS BIT(31)
117
118
119#define M_CACTIVE BIT(0)
120#define S_CACTIVE BIT(1)
121#define P0_PHY_READY BIT(8)
122#define P0_CP_POD BIT(9)
123#define P0_SLUMBER BIT(10)
124#define P0_PATIAL BIT(11)
125#define P0_PHY_SIG_DET BIT(12)
126#define P0_PHY_CALI BIT(13)
127#define P1_PHY_READY BIT(16)
128#define P1_CP_POD BIT(17)
129#define P1_SLUMBER BIT(18)
130#define P1_PATIAL BIT(19)
131#define P1_PHY_SIG_DET BIT(20)
132#define P1_PHY_CALI BIT(21)
133
134
135#define CR_TIME_SCALE (0x1000 << 0)
136
137
138#define RXCDRCALFOSC0 0x0065
139#define CALDUTY 0x006e
140#define RXDPIF 0x8065
141#define PPMDRIFTMAX_HI 0x80A4
142
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)
149
150
151static const u8 sata_phy_config1[] = {
152 0xC9, 0xC9, 0x07, 0x07, 0x18, 0x18, 0x01, 0x01, 0x22, 0x00
153};
154
155
156static 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,
165};
166
167const int sata_phy_debug = 0;
168
169static void sata_clear_glue_reg(u64 regbase, u32 off, u32 bit)
170{
171 u32 reg_val;
172
173 reg_val = nlm_read_sata_reg(regbase, off);
174 nlm_write_sata_reg(regbase, off, (reg_val & ~bit));
175}
176
177static void sata_set_glue_reg(u64 regbase, u32 off, u32 bit)
178{
179 u32 reg_val;
180
181 reg_val = nlm_read_sata_reg(regbase, off);
182 nlm_write_sata_reg(regbase, off, (reg_val | bit));
183}
184
185static void write_phy_reg(u64 regbase, u32 addr, u32 physel, u8 data)
186{
187 nlm_write_sata_reg(regbase, PHY_MEM_ACCESS,
188 (1u << 31) | (physel << 24) | (data << 16) | addr);
189 udelay(850);
190}
191
192static u8 read_phy_reg(u64 regbase, u32 addr, u32 physel)
193{
194 u32 val;
195
196 nlm_write_sata_reg(regbase, PHY_MEM_ACCESS,
197 (0 << 31) | (physel << 24) | (0 << 16) | addr);
198 udelay(850);
199 val = nlm_read_sata_reg(regbase, PHY_MEM_ACCESS);
200 return (val >> 16) & 0xff;
201}
202
203static void config_sata_phy(u64 regbase)
204{
205 u32 port, i, reg;
206 u8 val;
207
208 for (port = 0; port < 2; port++) {
209 for (i = 0, reg = RXCDRCALFOSC0; reg <= CALDUTY; reg++, i++)
210 write_phy_reg(regbase, reg, port, sata_phy_config1[i]);
211
212 for (i = 0, reg = RXDPIF; reg <= PPMDRIFTMAX_HI; reg++, i++)
213 write_phy_reg(regbase, reg, port, sata_phy_config2[i]);
214
215
216 write_phy_reg(regbase, 0x800F, port, 0x1f);
217
218 val = read_phy_reg(regbase, 0x0029, port);
219 write_phy_reg(regbase, 0x0029, port, val | (0x7 << 1));
220
221 val = read_phy_reg(regbase, 0x0056, port);
222 write_phy_reg(regbase, 0x0056, port, val & ~(1 << 3));
223
224 val = read_phy_reg(regbase, 0x0018, port);
225 write_phy_reg(regbase, 0x0018, port, val & ~(0x7 << 0));
226 }
227}
228
229static void check_phy_register(u64 regbase, u32 addr, u32 physel, u8 xdata)
230{
231 u8 data;
232
233 data = read_phy_reg(regbase, addr, physel);
234 pr_info("PHY read addr = 0x%x physel = %d data = 0x%x %s\n",
235 addr, physel, data, data == xdata ? "TRUE" : "FALSE");
236}
237
238static void verify_sata_phy_config(u64 regbase)
239{
240 u32 port, i, reg;
241
242 for (port = 0; port < 2; port++) {
243 for (i = 0, reg = RXCDRCALFOSC0; reg <= CALDUTY; reg++, i++)
244 check_phy_register(regbase, reg, port,
245 sata_phy_config1[i]);
246
247 for (i = 0, reg = RXDPIF; reg <= PPMDRIFTMAX_HI; reg++, i++)
248 check_phy_register(regbase, reg, port,
249 sata_phy_config2[i]);
250 }
251}
252
253static void nlm_sata_firmware_init(int node)
254{
255 u32 reg_val;
256 u64 regbase;
257 int n;
258
259 pr_info("Initializing XLP9XX On-chip AHCI...\n");
260 regbase = nlm_get_sata_regbase(node);
261
262
263 sata_clear_glue_reg(regbase, SATA_CTL, P0_IRST_POR);
264 sata_clear_glue_reg(regbase, SATA_CTL, P0_IRST_HARD_TXRX);
265 sata_clear_glue_reg(regbase, SATA_CTL, P0_IRST_HARD_SYNTH);
266 sata_clear_glue_reg(regbase, SATA_CTL, P0_IPDTXL);
267 sata_clear_glue_reg(regbase, SATA_CTL, P0_IPDRXL);
268 sata_clear_glue_reg(regbase, SATA_CTL, P0_IPDIPDMSYNTH);
269
270
271 sata_clear_glue_reg(regbase, SATA_CTL, P1_IRST_POR);
272 sata_clear_glue_reg(regbase, SATA_CTL, P1_IRST_HARD_TXRX);
273 sata_clear_glue_reg(regbase, SATA_CTL, P1_IRST_HARD_SYNTH);
274 sata_clear_glue_reg(regbase, SATA_CTL, P1_IPDTXL);
275 sata_clear_glue_reg(regbase, SATA_CTL, P1_IPDRXL);
276 sata_clear_glue_reg(regbase, SATA_CTL, P1_IPDIPDMSYNTH);
277 udelay(300);
278
279
280 sata_set_glue_reg(regbase, SATA_CTL, P0_IPDTXL);
281 sata_set_glue_reg(regbase, SATA_CTL, P0_IPDRXL);
282 sata_set_glue_reg(regbase, SATA_CTL, P0_IPDIPDMSYNTH);
283 sata_set_glue_reg(regbase, SATA_CTL, P1_IPDTXL);
284 sata_set_glue_reg(regbase, SATA_CTL, P1_IPDRXL);
285 sata_set_glue_reg(regbase, SATA_CTL, P1_IPDIPDMSYNTH);
286
287 udelay(1000);
288 sata_set_glue_reg(regbase, SATA_CTL, P0_IRST_POR);
289 udelay(1000);
290 sata_set_glue_reg(regbase, SATA_CTL, P1_IRST_POR);
291 udelay(1000);
292
293
294 config_sata_phy(regbase);
295 if (sata_phy_debug)
296 verify_sata_phy_config(regbase);
297
298 udelay(1000);
299 sata_set_glue_reg(regbase, SATA_CTL, P0_IRST_HARD_TXRX);
300 sata_set_glue_reg(regbase, SATA_CTL, P0_IRST_HARD_SYNTH);
301 sata_set_glue_reg(regbase, SATA_CTL, P1_IRST_HARD_TXRX);
302 sata_set_glue_reg(regbase, SATA_CTL, P1_IRST_HARD_SYNTH);
303 udelay(300);
304
305
306 sata_set_glue_reg(regbase, CR_REG_TIMER, CR_TIME_SCALE);
307
308 sata_set_glue_reg(regbase, SATA_CTL, SATA_RST_N);
309 sata_set_glue_reg(regbase, SATA_CTL, M_CSYSREQ);
310 sata_set_glue_reg(regbase, SATA_CTL, S_CSYSREQ);
311
312 pr_debug("Waiting for PHYs to come up.\n");
313 n = 10000;
314 do {
315 reg_val = nlm_read_sata_reg(regbase, SATA_STATUS);
316 if ((reg_val & P1_PHY_READY) && (reg_val & P0_PHY_READY))
317 break;
318 udelay(10);
319 } while (--n > 0);
320
321 if (reg_val & P0_PHY_READY)
322 pr_info("PHY0 is up.\n");
323 else
324 pr_info("PHY0 is down.\n");
325 if (reg_val & P1_PHY_READY)
326 pr_info("PHY1 is up.\n");
327 else
328 pr_info("PHY1 is down.\n");
329
330 pr_info("XLP AHCI Init Done.\n");
331}
332
333static int __init nlm_ahci_init(void)
334{
335 int node;
336
337 if (!cpu_is_xlp9xx())
338 return 0;
339 for (node = 0; node < NLM_NR_NODES; node++)
340 if (nlm_node_present(node))
341 nlm_sata_firmware_init(node);
342 return 0;
343}
344
345static void nlm_sata_intr_ack(struct irq_data *data)
346{
347 u64 regbase;
348 u32 val;
349 int node;
350
351 node = data->irq / NLM_IRQS_PER_NODE;
352 regbase = nlm_get_sata_regbase(node);
353 val = nlm_read_sata_reg(regbase, SATA_INT);
354 sata_set_glue_reg(regbase, SATA_INT, val);
355}
356
357static void nlm_sata_fixup_bar(struct pci_dev *dev)
358{
359 dev->resource[5] = dev->resource[0];
360 memset(&dev->resource[0], 0, sizeof(dev->resource[0]));
361}
362
363static void nlm_sata_fixup_final(struct pci_dev *dev)
364{
365 u32 val;
366 u64 regbase;
367 int node;
368
369
370 node = xlp_socdev_to_node(dev);
371 regbase = nlm_get_sata_regbase(node);
372
373
374 val = nlm_read_sata_reg(regbase, SATA_INT);
375 sata_set_glue_reg(regbase, SATA_INT, val);
376
377
378 sata_set_glue_reg(regbase, SATA_INT_MASK, 0x1);
379
380 dev->irq = nlm_irq_to_xirq(node, PIC_SATA_IRQ);
381 nlm_set_pic_extra_ack(node, PIC_SATA_IRQ, nlm_sata_intr_ack);
382}
383
384arch_initcall(nlm_ahci_init);
385
386DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_XLP9XX_SATA,
387 nlm_sata_fixup_bar);
388
389DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_XLP9XX_SATA,
390 nlm_sata_fixup_final);
391