Debian Codegraph @

src/vendorcode/cavium/bdk/libbdk-hal/qlm/bdk-qlm-common-sata.c

raw symbols SCIP
1 /***********************license start***********************************
2 * Copyright (c) 2003-2017 Cavium Inc. ([email protected]). All rights
3 * reserved.
4 *
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 *
18 * * Neither the name of Cavium Inc. nor the names of
19 * its contributors may be used to endorse or promote products
20 * derived from this software without specific prior written
21 * permission.
22 *
23 * This Software, including technical data, may be subject to U.S. export
24 * control laws, including the U.S. Export Administration Act and its
25 * associated regulations, and may be subject to export or import
26 * regulations in other countries.
27 *
28 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
29 * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS OR
30 * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT
31 * TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY
32 * REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT
33 * DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES
34 * OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR
35 * PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT,
36 * QUIET POSSESSION OR CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK
37 * ARISING OUT OF USE OR PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
38 ***********************license end**************************************/
39 #include <bdk.h>
40 #include "libbdk-hal/if/bdk-if.h"
41 #include "libbdk-hal/bdk-qlm.h"
42 #include "libbdk-hal/qlm/bdk-qlm-common.h"
43 #include "libbdk-arch/bdk-csrs-gser.h"
44 #include "libbdk-arch/bdk-csrs-sata.h"
45
46 /**
47 * Initialize a DLM/QLM for use with SATA controllers
48 *
49 * @param node Node to intialize
50 * @param qlm Which DLM/QLM to init
51 * @param baud_mhz QLM speed in Gbaud
52 * @param sata_first First SATA controller connected to this DLM/QLM
53 * @param sata_last Last SATA controller connected to this DLM/QLM (inclusive)
54 *
55 * @return Zero on success, negative on failure
56 */
57 int __bdk_qlm_set_sata_cn8xxx(bdk_node_t node, int qlm, int baud_mhz, int sata_first, int sata_last)
58 {
59 const int NUM_LANES = sata_last - sata_first + 1;
60 const int MAX_A_CLK = 333000000; /* Max of 333Mhz */
61
62 /* 26.4.1 Cold Reset */
63 /* 1. Ensure that the SerDes reference clock is up and stable. */
64 /* Already done */
65
66 /* 2. Optionally program the GPIO CSRs for SATA features.
67 a. For cold-presence detect, select a GPIO for the input and program GPI-
68 O_BIT_CFG(0..50)[PIN_SEL] = GPIO_PIN_SEL_E::SATA(0..15)_CP_DET.
69 b. For mechanical-presence detect, select a GPIO for the input and program
70 GPIO_BIT_CFG(0..50)[PIN_SEL] = GPI-
71 O_PIN_SEL_E::SATA(0..15)_MP_SWITCH.
72 c. For BIST board-test loopback, select a GPIO for the input and program GPI-
73 O_BIT_CFG(0..50)[PIN_SEL] = GPIO_PIN_SEL_E:::SATA_LAB_LB.
74 d. For LED activity, select a GPIO for the output and program GPI-
75 O_BIT_CFG(0..50)[PIN_SEL] = GPIO_PIN_SEL_E:::SATA(0..15)_ACT_LED.
76 e. For cold-presence power-on-device, select a GPIO for the output and program
77 GPIO_BIT_CFG(0..50)[PIN_SEL] = GPIO_PIN_SEL_E:::SATA(0..15)_CP_-
78 POD. */
79 /* Skipping */
80
81 /* 3. Optionally program the SGPIO unit. */
82 /* Skipping */
83
84 /* 4. Assert all resets:
85 a. UAHC reset: SATA(0..15)_UCTL_CTL[SATA_UAHC_RST] = 1
86 b. UCTL reset: SATA(0..15)_UCTL_CTL[SATA_UCTL_RST] = 1 */
87 for (int p = sata_first; p <= sata_last; p++)
88 {
89 BDK_CSR_MODIFY(c, node, BDK_SATAX_UCTL_CTL(p),
90 c.s.sata_uahc_rst = 1;
91 c.s.sata_uctl_rst = 1);
92 }
93
94 /* 5. Configure the ACLK:
95 a. Reset the clock dividers: SATA(0..15)_UCTL_CTL[A_CLKDIV_RST] = 1.
96 b. Select the ACLK frequency (refer to maximum values in Table 26 1).
97 i. SATA(0..15)_UCTL_CTL[A_CLKDIV_SEL] = desired value,
98 ii. SATA(0..15)_UCTL_CTL[A_CLK_EN] = 1 to enable the ACLK.
99 c. Deassert the ACLK clock divider reset:
100 SATA(0..15)_UCTL_CTL[A_CLKDIV_RST] = 0. */
101 int divisor = (bdk_clock_get_rate(node, BDK_CLOCK_SCLK) + MAX_A_CLK - 1) / MAX_A_CLK;
102 int a_clkdiv;
103 /* This screwy if logic is from the description of
104 SATAX_UCTL_CTL[a_clkdiv_sel] in the CSR */
105 if (divisor <= 4)
106 {
107 a_clkdiv = divisor - 1;
108 /* Divisor matches calculated value */
109 }
110 else if (divisor <= 6)
111 {
112 a_clkdiv = 4;
113 divisor = 6;
114 }
115 else if (divisor <= 8)
116 {
117 a_clkdiv = 5;
118 divisor = 8;
119 }
120 else if (divisor <= 16)
121 {
122 a_clkdiv = 6;
123 divisor = 16;
124 }
125 else if (divisor <= 24)
126 {
127 a_clkdiv = 7;
128 divisor = 24;
129 }
130 else
131 {
132 bdk_error("Unable to determine SATA clock divisor\n");
133 return -1;
134 }
135 /* Calculate the final clock rate */
136 int a_clk = bdk_clock_get_rate(node, BDK_CLOCK_SCLK) / divisor;
137
138 for (int p = sata_first; p <= sata_last; p++)
139 {
140 BDK_CSR_MODIFY(c, node, BDK_SATAX_UCTL_CTL(p),
141 c.s.a_clkdiv_rst = 1);
142 BDK_CSR_MODIFY(c, node, BDK_SATAX_UCTL_CTL(p),
143 c.s.a_clk_byp_sel = 0;
144 c.s.a_clkdiv_sel = a_clkdiv;
145 c.s.a_clk_en = 1);
146 BDK_CSR_MODIFY(c, node, BDK_SATAX_UCTL_CTL(p),
147 c.s.a_clkdiv_rst = 0);
148 }
149 bdk_wait_usec(1);
150
151 /* 8. Configure PHY for SATA. Refer to Section 21.1.2. */
152 /* Done below, section 24.1.2.3 */
153
154 /* 9. TBD: Poll QLM2_MPLL_STATUS for MPLL lock */
155 /* Not needed */
156
157 /* 10. Initialize UAHC as described in the AHCI specification
158 (UAHC_* registers). */
159 /* Done when a SATA driver is initialized */
160
161 /* 24.1.2.3 SATA Configuration
162 Software must perform the following steps to configure the GSER_WEST
163 for a SATA application. Note that the GSERW steps are on a QLM basis. */
164
165 /* 1. Configure the SATA controller (refer to Chapter 26). */
166 /* This is the code above */
167
168 /* 2. Configure the QLM Reference clock.
169 Set GSER(0..13)_REFCLK_SEL[COM_CLK_SEL] = 1 to source the reference
170 clock from the external clock multiplexer.
171 Configure GSER(0..13)_REFCLK_SEL[USE_COM1]:
172 0 = use QLMC_REF_CLK0_P/N
173 1 = use QLMC_REF_CLK1_P/N */
174 /* Already done */
175
176 /* Make sure the PHY is in reset before we reconfig */
177 BDK_CSR_MODIFY(c, node, BDK_GSERX_PHY_CTL(qlm),
178 c.s.phy_reset = 1);
179 bdk_wait_usec(1);
180
181 /* 3. Configure the QLM for SATA mode: set GSER(0..13)_CFG[SATA] = 1. */
182 BDK_CSR_MODIFY(c, node, BDK_GSERX_CFG(qlm),
183 c.u = 0;
184 c.s.sata = 1);
185
186 /* 9. Clear the appropriate lane resets:
187 GSER(0..13)_SATA_LANE_RST[Ln_RST] = 0, where n is the lane number 0-3. */
188 BDK_CSR_WRITE(node, BDK_GSERX_SATA_LANE_RST(qlm), 0);
189 BDK_CSR_READ(node, BDK_GSERX_SATA_LANE_RST(qlm));
190
191 /* We'll check for the SATA_PCS Ready in step 8a below */
192 /* Short 1 usec wait */
193 bdk_wait_usec(1);
194
195 /* 4. Take the PHY out of reset: write GSER(0..13)_PHY_CTL[PHY_RESET] = 0. */
196 BDK_CSR_MODIFY(c, node, BDK_GSERX_PHY_CTL(qlm),
197 c.s.phy_reset = 0);
198
199 /* 4a. Poll for PHY RST_RDY indicating the PHY has initialized before
200 trying to access internal registers to reconfigure for SATA */
201 /* 8. Wait for GSER(0..13)_QLM_STAT[RST_RDY] = 1, indicating that the PHY
202 has been reconfigured and PLLs are locked. */
203 if (BDK_CSR_WAIT_FOR_FIELD(node, BDK_GSERX_QLM_STAT(qlm), rst_rdy, ==, 1, 10000))
204 {
205 bdk_error("QLM%d: Timeout waiting for GSERX_QLM_STAT[rst_rdy]\n", qlm);
206 return -1;
207 }
208
209 /* Workaround for errata GSER-30310: SATA HDD Not Ready due to
210 PHY SDLL/LDLL lockup at 3GHz */
211 for (int slice = 0; slice < NUM_LANES / 2; slice++)
212 {
213 BDK_CSR_MODIFY(c, node, BDK_GSERX_SLICEX_PCIE1_MODE(qlm, slice),
214 c.s.rx_pi_bwsel = 1;
215 c.s.rx_ldll_bwsel = 1;
216 c.s.rx_sdll_bwsel = 1);
217 BDK_CSR_MODIFY(c, node, BDK_GSERX_SLICEX_PCIE2_MODE(qlm, slice),
218 c.s.rx_pi_bwsel = 1;
219 c.s.rx_ldll_bwsel = 1;
220 c.s.rx_sdll_bwsel = 1);
221 BDK_CSR_MODIFY(c, node, BDK_GSERX_SLICEX_PCIE3_MODE(qlm, slice),
222 c.s.rx_pi_bwsel = 1;
223 c.s.rx_ldll_bwsel = 1;
224 c.s.rx_sdll_bwsel = 1);
225 }
226
227 /* 5. Change the P2 termination
228 GSERn_RX_PWR_CTRL_P2[P2_RX_SUBBLK_PD<0>] = 0 (termination) */
229 BDK_CSR_MODIFY(c, node, BDK_GSERX_RX_PWR_CTRL_P2(qlm),
230 c.s.p2_rx_subblk_pd &= 0x1e);
231
232 /* 6. Modify the electrical IDLE detect on delay: set
233 GSER(0..13)_LANE(0..3)_MISC_CFG_0[EIE_DET_STL_ON_TIME] = 0x4 */
234 for (int lane = 0; lane < NUM_LANES; lane++)
235 {
236 BDK_CSR_MODIFY(c, node, BDK_GSERX_LANEX_MISC_CFG_0(qlm, lane),
237 c.s.eie_det_stl_on_time = 4);
238 }
239
240 /* 7. Modify the PLL and lane-protocol-mode registers to configure the
241 PHY for SATA */
242 /* Errata (GSER-26724) SATA never indicates GSER QLM_STAT[RST_RDY]
243 We program PLL_PX_MODE_0 last due to this errata */
244 for (int p=0; p<3; p++)
245 {
246 BDK_CSR_MODIFY(c, node, BDK_GSERX_PLL_PX_MODE_1(qlm, p),
247 c.s.pll_16p5en = 0x0;
248 c.s.pll_cpadj = 0x2;
249 c.s.pll_pcie3en = 0;
250 c.s.pll_opr = 0x0;
251 c.s.pll_div = 0x1e);
252 BDK_CSR_MODIFY(c, node, BDK_GSERX_LANE_PX_MODE_0(qlm, p),
253 c.s.ctle = 0x0;
254 c.s.pcie = 0;
255 c.s.tx_ldiv = 0x0;
256 c.s.rx_ldiv = 2 - p;
257 c.s.srate = 0;
258 c.s.tx_mode = 3;
259 c.s.rx_mode = 3);
260 BDK_CSR_MODIFY(c, node, BDK_GSERX_LANE_PX_MODE_1(qlm, p),
261 c.s.vma_fine_cfg_sel = 0;
262 c.s.vma_mm = 1;
263 c.s.cdr_fgain = 0xa;
264 c.s.ph_acc_adj = 0x15);
265 }
266 for (int p=0; p<3; p++)
267 {
268 BDK_CSR_MODIFY(c, node, BDK_GSERX_PLL_PX_MODE_0(qlm, p),
269 c.s.pll_icp = 0x1;
270 c.s.pll_rloop = 0x3;
271 c.s.pll_pcs_div = 0x5);
272 }
273
274 for (int s = 0; s < NUM_LANES / 2; s++)
275 {
276 BDK_CSR_MODIFY(c, node, BDK_GSERX_SLICEX_RX_SDLL_CTRL(qlm, s),
277 c.s.pcs_sds_oob_clk_ctrl = 2;
278 c.s.pcs_sds_rx_sdll_tune = 0;
279 c.s.pcs_sds_rx_sdll_swsel = 0);
280 }
281
282 for (int lane = 0; lane < NUM_LANES; lane++)
283 {
284 BDK_CSR_MODIFY(c, node, BDK_GSERX_LANEX_MISC_CFG_0(qlm, lane),
285 c.s.use_pma_polarity = 0;
286 c.s.cfg_pcs_loopback = 0;
287 c.s.pcs_tx_mode_ovrrd_en = 0;
288 c.s.pcs_rx_mode_ovrrd_en = 0;
289 c.s.cfg_eie_det_cnt = 0;
290 c.s.eie_det_stl_on_time = 4;
291 c.s.eie_det_stl_off_time = 0;
292 c.s.tx_bit_order = 1;
293 c.s.rx_bit_order = 1);
294 }
295
296 /* 8. Wait for GSER(0..13)_QLM_STAT[RST_RDY] = 1, indicating that the PHY
297 has been reconfigured and PLLs are locked. */
298 if (BDK_CSR_WAIT_FOR_FIELD(node, BDK_GSERX_QLM_STAT(qlm), rst_rdy, ==, 1, 10000))
299 {
300 bdk_error("QLM%d: Timeout waiting for GSERX_QLM_STAT[rst_rdy]\n", qlm);
301 return -1;
302 }
303 /* 8a. Check that the SATA_PCS is "Ready" here, should be but check it */
304 /* Poll GSERX_SATA_STATUS for PX_RDY = 1 */
305 if (BDK_CSR_WAIT_FOR_FIELD(node, BDK_GSERX_SATA_STATUS(qlm), p0_rdy, ==, 1, 10000))
306 {
307 bdk_error("QLM%d: Timeout waiting for GSERX_SATA_STATUS[p0_rdy]\n", qlm);
308 return -1;
309 }
310
311 /* Add 1ms delay for everything to stabilize*/
312 bdk_wait_usec(1000);
313
314 /* Apply any custom tuning */
315 __bdk_qlm_tune(node, qlm, BDK_QLM_MODE_SATA_4X1, baud_mhz);
316 bdk_wait_usec(1000);
317
318
319 /* 6. Deassert UCTL and UAHC resets:
320 a. SATA(0..15)_UCTL_CTL[SATA_UAHC_RST] = 0
321 b. SATA(0..15)_UCTL_CTL[SATA_UCTL_RST] = 0
322 c. Wait 10 ACLK cycles before accessing any ACLK-only registers. */
323 for (int p = sata_first; p <= sata_last; p++)
324 {
325 BDK_CSR_MODIFY(c, node, BDK_SATAX_UCTL_CTL(p),
326 c.s.sata_uahc_rst = 0;
327 c.s.sata_uctl_rst = 0);
328 }
329 bdk_wait_usec(1);
330
331 /* 7. Enable conditional SCLK of UCTL by writing
332 SATA(0..15)_UCTL_CTL[CSCLK_EN] = 1. */
333 for (int p = sata_first; p <= sata_last; p++)
334 {
335 if (CAVIUM_IS_MODEL(CAVIUM_CN8XXX))
336 {
337 /* CN9XXX make coprocessor clock automatic */
338 BDK_CSR_MODIFY(c, node, BDK_SATAX_UCTL_CTL(p),
339 c.cn8.csclk_en = 1);
340 }
341 }
342
343 /* Check BIST on the SATA controller. Start BIST in parallel on the
344 controllers */
345
346 for (int p = sata_first; p <= sata_last; p++)
347 {
348 /* Make sure BIST is configured properly before we start it. We
349 want full BIST, not just CLEAR */
350 BDK_CSR_MODIFY(c, node, BDK_SATAX_UCTL_CTL(p),
351 c.s.clear_bist = 0;
352 c.s.start_bist = 0);
353 /* Start BIST */
354 BDK_CSR_MODIFY(c, node, BDK_SATAX_UCTL_CTL(p),
355 c.s.start_bist = 1);
356 }
357 bdk_wait_usec(1000);
358 for (int p = sata_first; p <= sata_last; p++)
359 {
360 BDK_CSR_INIT(bist, node, BDK_SATAX_UCTL_BIST_STATUS(p));
361 if (bist.u)
362 bdk_error("N%d.SATA%d: Controller failed BIST (0x%llx)\n", node, p, bist.u);
363 else
364 BDK_TRACE(SATA, "N%d.SATA%d: Passed BIST\n", node, p);
365 }
366 /* Clear start_bist so it is ready for the next run */
367 for (int p = sata_first; p <= sata_last; p++)
368 {
369 BDK_CSR_MODIFY(c, node, BDK_SATAX_UCTL_CTL(p),
370 c.s.start_bist = 0);
371 }
372
373 int spd;
374 if (baud_mhz < 3000)
375 spd = 1;
376 else if (baud_mhz < 6000)
377 spd = 2;
378 else
379 spd = 3;
380
381 for (int p = sata_first; p <= sata_last; p++)
382 {
383 /* From the synopsis data book, SATAX_UAHC_GBL_TIMER1MS is the
384 AMBA clock in MHz * 1000, which is a_clk(Hz) / 1000 */
385 BDK_TRACE(QLM, "QLM%d: SATA%d set to %d Hz\n", qlm, p, a_clk);
386 BDK_CSR_MODIFY(c, node, BDK_SATAX_UAHC_GBL_TIMER1MS(p),
387 c.s.timv = a_clk / 1000);
388 /* Set speed */
389 BDK_CSR_MODIFY(c, node, BDK_SATAX_UAHC_P0_SCTL(p),
390 c.s.ipm = 3; /* Disable parial and slumber power management */
391 c.s.spd = spd);
392 /* The following SATA setup is from the AHCI 1.3 spec, section
393 10.1.1, Firmware Specific Initialization. */
394 BDK_CSR_MODIFY(c, node, BDK_SATAX_UAHC_GBL_CAP(p),
395 c.s.sss = 1; /* Support staggered spin-up */
396 c.s.smps = 1); /* Support mechanical presence switch */
397 BDK_CSR_MODIFY(c, node, BDK_SATAX_UAHC_GBL_PI(p),
398 c.s.pi = 1); /* One port per controller */
399 BDK_CSR_MODIFY(c, node, BDK_SATAX_UAHC_P0_CMD(p),
400 c.s.hpcp = 1; /* Hot-plug-capable support */
401 c.s.mpsp = 1; /* Mechanical presence switch attached to port */
402 c.s.cpd = 1); /* Cold-presence detection */
403 }
404 return 0;
405 }
406
407 /**
408 * Initialize a DLM/QLM for use with SATA controllers
409 *
410 * @param node Node to intialize
411 * @param qlm Which DLM/QLM to init
412 * @param baud_mhz QLM speed in Gbaud
413 * @param sata_first First SATA controller connected to this DLM/QLM
414 * @param sata_last Last SATA controller connected to this DLM/QLM (inclusive)
415 *
416 * @return Zero on success, negative on failure
417 */
418 int __bdk_qlm_set_sata_cn9xxx(bdk_node_t node, int qlm, int baud_mhz, int sata_first, int sata_last)
419 {
420 //const int NUM_LANES = sata_last - sata_first + 1;
421 const int MAX_A_CLK = 333000000; /* Max of 333Mhz */
422
423 /* 26.4.1 Cold Reset */
424 /* 1. Ensure that the SerDes reference clock is up and stable. */
425 /* Already done */
426
427 /* 2. Optionally program the GPIO CSRs for SATA features.
428 a. For cold-presence detect, select a GPIO for the input and program GPI-
429 O_BIT_CFG(0..50)[PIN_SEL] = GPIO_PIN_SEL_E::SATA(0..15)_CP_DET.
430 b. For mechanical-presence detect, select a GPIO for the input and program
431 GPIO_BIT_CFG(0..50)[PIN_SEL] = GPI-
432 O_PIN_SEL_E::SATA(0..15)_MP_SWITCH.
433 c. For BIST board-test loopback, select a GPIO for the input and program GPI-
434 O_BIT_CFG(0..50)[PIN_SEL] = GPIO_PIN_SEL_E:::SATA_LAB_LB.
435 d. For LED activity, select a GPIO for the output and program GPI-
436 O_BIT_CFG(0..50)[PIN_SEL] = GPIO_PIN_SEL_E:::SATA(0..15)_ACT_LED.
437 e. For cold-presence power-on-device, select a GPIO for the output and program
438 GPIO_BIT_CFG(0..50)[PIN_SEL] = GPIO_PIN_SEL_E:::SATA(0..15)_CP_-
439 POD. */
440 /* Skipping */
441
442 /* 3. Optionally program the SGPIO unit. */
443 /* Skipping */
444
445 /* 4. Assert all resets:
446 a. UAHC reset: SATA(0..15)_UCTL_CTL[SATA_UAHC_RST] = 1
447 b. UCTL reset: SATA(0..15)_UCTL_CTL[SATA_UCTL_RST] = 1 */
448 for (int p = sata_first; p <= sata_last; p++)
449 {
450 BDK_CSR_MODIFY(c, node, BDK_SATAX_UCTL_CTL(p),
451 c.s.sata_uahc_rst = 1;
452 c.s.sata_uctl_rst = 1);
453 }
454
455 /* 5. Configure the ACLK:
456 a. Reset the clock dividers: SATA(0..15)_UCTL_CTL[A_CLKDIV_RST] = 1.
457 b. Select the ACLK frequency (refer to maximum values in Table 26 1).
458 i. SATA(0..15)_UCTL_CTL[A_CLKDIV_SEL] = desired value,
459 ii. SATA(0..15)_UCTL_CTL[A_CLK_EN] = 1 to enable the ACLK.
460 c. Deassert the ACLK clock divider reset:
461 SATA(0..15)_UCTL_CTL[A_CLKDIV_RST] = 0. */
462 int divisor = (bdk_clock_get_rate(node, BDK_CLOCK_SCLK) + MAX_A_CLK - 1) / MAX_A_CLK;
463 int a_clkdiv;
464 /* This screwy if logic is from the description of
465 SATAX_UCTL_CTL[a_clkdiv_sel] in the CSR */
466 if (divisor <= 4)
467 {
468 a_clkdiv = divisor - 1;
469 /* Divisor matches calculated value */
470 }
471 else if (divisor <= 6)
472 {
473 a_clkdiv = 4;
474 divisor = 6;
475 }
476 else if (divisor <= 8)
477 {
478 a_clkdiv = 5;
479 divisor = 8;
480 }
481 else if (divisor <= 16)
482 {
483 a_clkdiv = 6;
484 divisor = 16;
485 }
486 else if (divisor <= 24)
487 {
488 a_clkdiv = 7;
489 divisor = 24;
490 }
491 else
492 {
493 bdk_error("Unable to determine SATA clock divisor\n");
494 return -1;
495 }
496 /* Calculate the final clock rate */
497 int a_clk = bdk_clock_get_rate(node, BDK_CLOCK_SCLK) / divisor;
498
499 for (int p = sata_first; p <= sata_last; p++)
500 {
501 BDK_CSR_MODIFY(c, node, BDK_SATAX_UCTL_CTL(p),
502 c.s.a_clkdiv_rst = 1);
503 BDK_CSR_MODIFY(c, node, BDK_SATAX_UCTL_CTL(p),
504 c.s.a_clk_byp_sel = 0;
505 c.s.a_clkdiv_sel = a_clkdiv;
506 c.s.a_clk_en = 1);
507 BDK_CSR_MODIFY(c, node, BDK_SATAX_UCTL_CTL(p),
508 c.s.a_clkdiv_rst = 0);
509 }
510 bdk_wait_usec(1);
511
512 /* 8. Configure PHY for SATA. Refer to Section 21.1.2. */
513 /* Done below, section 24.1.2.3 */
514
515 /* 9. TBD: Poll QLM2_MPLL_STATUS for MPLL lock */
516 /* Not needed */
517
518 /* 10. Initialize UAHC as described in the AHCI specification
519 (UAHC_* registers). */
520 /* Done when a SATA driver is initialized */
521
522 /* 24.1.2.3 SATA Configuration
523 Software must perform the following steps to configure the GSER_WEST
524 for a SATA application. Note that the GSERW steps are on a QLM basis. */
525
526 /* 1. Configure the SATA controller (refer to Chapter 26). */
527 /* This is the code above */
528
529 /* 2. Configure the QLM Reference clock.
530 Set GSER(0..13)_REFCLK_SEL[COM_CLK_SEL] = 1 to source the reference
531 clock from the external clock multiplexer.
532 Configure GSER(0..13)_REFCLK_SEL[USE_COM1]:
533 0 = use QLMC_REF_CLK0_P/N
534 1 = use QLMC_REF_CLK1_P/N */
535 /* Already done */
536
537 // FIXME: GSERN setup
538
539 /* 6. Deassert UCTL and UAHC resets:
540 a. SATA(0..15)_UCTL_CTL[SATA_UAHC_RST] = 0
541 b. SATA(0..15)_UCTL_CTL[SATA_UCTL_RST] = 0
542 c. Wait 10 ACLK cycles before accessing any ACLK-only registers. */
543 for (int p = sata_first; p <= sata_last; p++)
544 {
545 BDK_CSR_MODIFY(c, node, BDK_SATAX_UCTL_CTL(p),
546 c.s.sata_uahc_rst = 0;
547 c.s.sata_uctl_rst = 0);
548 }
549 bdk_wait_usec(1);
550
551 /* 7. Enable conditional SCLK of UCTL by writing
552 SATA(0..15)_UCTL_CTL[CSCLK_EN] = 1. */
553 for (int p = sata_first; p <= sata_last; p++)
554 {
555 if (CAVIUM_IS_MODEL(CAVIUM_CN8XXX))
556 {
557 /* CN9XXX make coprocessor clock automatic */
558 BDK_CSR_MODIFY(c, node, BDK_SATAX_UCTL_CTL(p),
559 c.cn8.csclk_en = 1);
560 }
561 }
562
563 /* Check BIST on the SATA controller. Start BIST in parallel on the
564 controllers */
565 for (int p = sata_first; p <= sata_last; p++)
566 {
567 /* Make sure BIST is configured properly before we start it. We
568 want full BIST, not just CLEAR */
569 BDK_CSR_MODIFY(c, node, BDK_SATAX_UCTL_CTL(p),
570 c.s.clear_bist = 0;
571 c.s.start_bist = 0);
572 /* Start BIST */
573 BDK_CSR_MODIFY(c, node, BDK_SATAX_UCTL_CTL(p),
574 c.s.start_bist = 1);
575 }
576 bdk_wait_usec(1000);
577 for (int p = sata_first; p <= sata_last; p++)
578 {
579 BDK_CSR_INIT(bist, node, BDK_SATAX_UCTL_BIST_STATUS(p));
580 if (bist.u)
581 bdk_error("N%d.SATA%d: Controller failed BIST (0x%llx)\n", node, p, bist.u);
582 else
583 BDK_TRACE(SATA, "N%d.SATA%d: Passed BIST\n", node, p);
584 }
585 /* Clear start_bist so it is ready for the next run */
586 for (int p = sata_first; p <= sata_last; p++)
587 {
588 BDK_CSR_MODIFY(c, node, BDK_SATAX_UCTL_CTL(p),
589 c.s.start_bist = 0);
590 }
591
592 int spd;
593 if (baud_mhz < 3000)
594 spd = 1;
595 else if (baud_mhz < 6000)
596 spd = 2;
597 else
598 spd = 3;
599
600 for (int p = sata_first; p <= sata_last; p++)
601 {
602 /* From the synopsis data book, SATAX_UAHC_GBL_TIMER1MS is the
603 AMBA clock in MHz * 1000, which is a_clk(Hz) / 1000 */
604 BDK_TRACE(QLM, "QLM%d: SATA%d set to %d Hz\n", qlm, p, a_clk);
605 BDK_CSR_MODIFY(c, node, BDK_SATAX_UAHC_GBL_TIMER1MS(p),
606 c.s.timv = a_clk / 1000);
607 /* Set speed */
608 BDK_CSR_MODIFY(c, node, BDK_SATAX_UAHC_P0_SCTL(p),
609 c.s.ipm = 3; /* Disable parial and slumber power management */
610 c.s.spd = spd);
611 /* The following SATA setup is from the AHCI 1.3 spec, section
612 10.1.1, Firmware Specific Initialization. */
613 BDK_CSR_MODIFY(c, node, BDK_SATAX_UAHC_GBL_CAP(p),
614 c.s.sss = 1; /* Support staggered spin-up */
615 c.s.smps = 1); /* Support mechanical presence switch */
616 BDK_CSR_MODIFY(c, node, BDK_SATAX_UAHC_GBL_PI(p),
617 c.s.pi = 1); /* One port per controller */
618 BDK_CSR_MODIFY(c, node, BDK_SATAX_UAHC_P0_CMD(p),
619 c.s.hpcp = 1; /* Hot-plug-capable support */
620 c.s.mpsp = 1; /* Mechanical presence switch attached to port */
621 c.s.cpd = 1); /* Cold-presence detection */
622 }
623 return 0;
624 }
625