Loading...
1/*
2 * AppliedMicro X-Gene Multi-purpose PHY driver
3 *
4 * Copyright (c) 2014, Applied Micro Circuits Corporation
5 * Author: Loc Ho <lho@apm.com>
6 * Tuan Phan <tphan@apm.com>
7 * Suman Tripathi <stripathi@apm.com>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * The APM X-Gene PHY consists of two PLL clock macro's (CMU) and lanes.
23 * The first PLL clock macro is used for internal reference clock. The second
24 * PLL clock macro is used to generate the clock for the PHY. This driver
25 * configures the first PLL CMU, the second PLL CMU, and programs the PHY to
26 * operate according to the mode of operation. The first PLL CMU is only
27 * required if internal clock is enabled.
28 *
29 * Logical Layer Out Of HW module units:
30 *
31 * -----------------
32 * | Internal | |------|
33 * | Ref PLL CMU |----| | ------------- ---------
34 * ------------ ---- | MUX |-----|PHY PLL CMU|----| Serdes|
35 * | | | | ---------
36 * External Clock ------| | -------------
37 * |------|
38 *
39 * The Ref PLL CMU CSR (Configuration System Registers) is accessed
40 * indirectly from the SDS offset at 0x2000. It is only required for
41 * internal reference clock.
42 * The PHY PLL CMU CSR is accessed indirectly from the SDS offset at 0x0000.
43 * The Serdes CSR is accessed indirectly from the SDS offset at 0x0400.
44 *
45 * The Ref PLL CMU can be located within the same PHY IP or outside the PHY IP
46 * due to shared Ref PLL CMU. For PHY with Ref PLL CMU shared with another IP,
47 * it is located outside the PHY IP. This is the case for the PHY located
48 * at 0x1f23a000 (SATA Port 4/5). For such PHY, another resource is required
49 * to located the SDS/Ref PLL CMU module and its clock for that IP enabled.
50 *
51 * Currently, this driver only supports Gen3 SATA mode with external clock.
52 */
53#include <linux/module.h>
54#include <linux/platform_device.h>
55#include <linux/io.h>
56#include <linux/delay.h>
57#include <linux/phy/phy.h>
58#include <linux/clk.h>
59
60/* Max 2 lanes per a PHY unit */
61#define MAX_LANE 2
62
63/* Register offset inside the PHY */
64#define SERDES_PLL_INDIRECT_OFFSET 0x0000
65#define SERDES_PLL_REF_INDIRECT_OFFSET 0x2000
66#define SERDES_INDIRECT_OFFSET 0x0400
67#define SERDES_LANE_STRIDE 0x0200
68
69/* Some default Serdes parameters */
70#define DEFAULT_SATA_TXBOOST_GAIN { 0x1e, 0x1e, 0x1e }
71#define DEFAULT_SATA_TXEYEDIRECTION { 0x0, 0x0, 0x0 }
72#define DEFAULT_SATA_TXEYETUNING { 0xa, 0xa, 0xa }
73#define DEFAULT_SATA_SPD_SEL { 0x1, 0x3, 0x7 }
74#define DEFAULT_SATA_TXAMP { 0x8, 0x8, 0x8 }
75#define DEFAULT_SATA_TXCN1 { 0x2, 0x2, 0x2 }
76#define DEFAULT_SATA_TXCN2 { 0x0, 0x0, 0x0 }
77#define DEFAULT_SATA_TXCP1 { 0xa, 0xa, 0xa }
78
79#define SATA_SPD_SEL_GEN3 0x7
80#define SATA_SPD_SEL_GEN2 0x3
81#define SATA_SPD_SEL_GEN1 0x1
82
83#define SSC_DISABLE 0
84#define SSC_ENABLE 1
85
86#define FBDIV_VAL_50M 0x77
87#define REFDIV_VAL_50M 0x1
88#define FBDIV_VAL_100M 0x3B
89#define REFDIV_VAL_100M 0x0
90
91/* SATA Clock/Reset CSR */
92#define SATACLKENREG 0x00000000
93#define SATA0_CORE_CLKEN 0x00000002
94#define SATA1_CORE_CLKEN 0x00000004
95#define SATASRESETREG 0x00000004
96#define SATA_MEM_RESET_MASK 0x00000020
97#define SATA_MEM_RESET_RD(src) (((src) & 0x00000020) >> 5)
98#define SATA_SDS_RESET_MASK 0x00000004
99#define SATA_CSR_RESET_MASK 0x00000001
100#define SATA_CORE_RESET_MASK 0x00000002
101#define SATA_PMCLK_RESET_MASK 0x00000010
102#define SATA_PCLK_RESET_MASK 0x00000008
103
104/* SDS CSR used for PHY Indirect access */
105#define SATA_ENET_SDS_PCS_CTL0 0x00000000
106#define REGSPEC_CFG_I_TX_WORDMODE0_SET(dst, src) \
107 (((dst) & ~0x00070000) | (((u32) (src) << 16) & 0x00070000))
108#define REGSPEC_CFG_I_RX_WORDMODE0_SET(dst, src) \
109 (((dst) & ~0x00e00000) | (((u32) (src) << 21) & 0x00e00000))
110#define SATA_ENET_SDS_CTL0 0x0000000c
111#define REGSPEC_CFG_I_CUSTOMER_PIN_MODE0_SET(dst, src) \
112 (((dst) & ~0x00007fff) | (((u32) (src)) & 0x00007fff))
113#define SATA_ENET_SDS_CTL1 0x00000010
114#define CFG_I_SPD_SEL_CDR_OVR1_SET(dst, src) \
115 (((dst) & ~0x0000000f) | (((u32) (src)) & 0x0000000f))
116#define SATA_ENET_SDS_RST_CTL 0x00000024
117#define SATA_ENET_SDS_IND_CMD_REG 0x0000003c
118#define CFG_IND_WR_CMD_MASK 0x00000001
119#define CFG_IND_RD_CMD_MASK 0x00000002
120#define CFG_IND_CMD_DONE_MASK 0x00000004
121#define CFG_IND_ADDR_SET(dst, src) \
122 (((dst) & ~0x003ffff0) | (((u32) (src) << 4) & 0x003ffff0))
123#define SATA_ENET_SDS_IND_RDATA_REG 0x00000040
124#define SATA_ENET_SDS_IND_WDATA_REG 0x00000044
125#define SATA_ENET_CLK_MACRO_REG 0x0000004c
126#define I_RESET_B_SET(dst, src) \
127 (((dst) & ~0x00000001) | (((u32) (src)) & 0x00000001))
128#define I_PLL_FBDIV_SET(dst, src) \
129 (((dst) & ~0x001ff000) | (((u32) (src) << 12) & 0x001ff000))
130#define I_CUSTOMEROV_SET(dst, src) \
131 (((dst) & ~0x00000f80) | (((u32) (src) << 7) & 0x00000f80))
132#define O_PLL_LOCK_RD(src) (((src) & 0x40000000) >> 30)
133#define O_PLL_READY_RD(src) (((src) & 0x80000000) >> 31)
134
135/* PLL Clock Macro Unit (CMU) CSR accessing from SDS indirectly */
136#define CMU_REG0 0x00000
137#define CMU_REG0_PLL_REF_SEL_MASK 0x00002000
138#define CMU_REG0_PLL_REF_SEL_SET(dst, src) \
139 (((dst) & ~0x00002000) | (((u32) (src) << 13) & 0x00002000))
140#define CMU_REG0_PDOWN_MASK 0x00004000
141#define CMU_REG0_CAL_COUNT_RESOL_SET(dst, src) \
142 (((dst) & ~0x000000e0) | (((u32) (src) << 5) & 0x000000e0))
143#define CMU_REG1 0x00002
144#define CMU_REG1_PLL_CP_SET(dst, src) \
145 (((dst) & ~0x00003c00) | (((u32) (src) << 10) & 0x00003c00))
146#define CMU_REG1_PLL_MANUALCAL_SET(dst, src) \
147 (((dst) & ~0x00000008) | (((u32) (src) << 3) & 0x00000008))
148#define CMU_REG1_PLL_CP_SEL_SET(dst, src) \
149 (((dst) & ~0x000003e0) | (((u32) (src) << 5) & 0x000003e0))
150#define CMU_REG1_REFCLK_CMOS_SEL_MASK 0x00000001
151#define CMU_REG1_REFCLK_CMOS_SEL_SET(dst, src) \
152 (((dst) & ~0x00000001) | (((u32) (src) << 0) & 0x00000001))
153#define CMU_REG2 0x00004
154#define CMU_REG2_PLL_REFDIV_SET(dst, src) \
155 (((dst) & ~0x0000c000) | (((u32) (src) << 14) & 0x0000c000))
156#define CMU_REG2_PLL_LFRES_SET(dst, src) \
157 (((dst) & ~0x0000001e) | (((u32) (src) << 1) & 0x0000001e))
158#define CMU_REG2_PLL_FBDIV_SET(dst, src) \
159 (((dst) & ~0x00003fe0) | (((u32) (src) << 5) & 0x00003fe0))
160#define CMU_REG3 0x00006
161#define CMU_REG3_VCOVARSEL_SET(dst, src) \
162 (((dst) & ~0x0000000f) | (((u32) (src) << 0) & 0x0000000f))
163#define CMU_REG3_VCO_MOMSEL_INIT_SET(dst, src) \
164 (((dst) & ~0x000003f0) | (((u32) (src) << 4) & 0x000003f0))
165#define CMU_REG3_VCO_MANMOMSEL_SET(dst, src) \
166 (((dst) & ~0x0000fc00) | (((u32) (src) << 10) & 0x0000fc00))
167#define CMU_REG4 0x00008
168#define CMU_REG5 0x0000a
169#define CMU_REG5_PLL_LFSMCAP_SET(dst, src) \
170 (((dst) & ~0x0000c000) | (((u32) (src) << 14) & 0x0000c000))
171#define CMU_REG5_PLL_LOCK_RESOLUTION_SET(dst, src) \
172 (((dst) & ~0x0000000e) | (((u32) (src) << 1) & 0x0000000e))
173#define CMU_REG5_PLL_LFCAP_SET(dst, src) \
174 (((dst) & ~0x00003000) | (((u32) (src) << 12) & 0x00003000))
175#define CMU_REG5_PLL_RESETB_MASK 0x00000001
176#define CMU_REG6 0x0000c
177#define CMU_REG6_PLL_VREGTRIM_SET(dst, src) \
178 (((dst) & ~0x00000600) | (((u32) (src) << 9) & 0x00000600))
179#define CMU_REG6_MAN_PVT_CAL_SET(dst, src) \
180 (((dst) & ~0x00000004) | (((u32) (src) << 2) & 0x00000004))
181#define CMU_REG7 0x0000e
182#define CMU_REG7_PLL_CALIB_DONE_RD(src) ((0x00004000 & (u32) (src)) >> 14)
183#define CMU_REG7_VCO_CAL_FAIL_RD(src) ((0x00000c00 & (u32) (src)) >> 10)
184#define CMU_REG8 0x00010
185#define CMU_REG9 0x00012
186#define CMU_REG9_WORD_LEN_8BIT 0x000
187#define CMU_REG9_WORD_LEN_10BIT 0x001
188#define CMU_REG9_WORD_LEN_16BIT 0x002
189#define CMU_REG9_WORD_LEN_20BIT 0x003
190#define CMU_REG9_WORD_LEN_32BIT 0x004
191#define CMU_REG9_WORD_LEN_40BIT 0x005
192#define CMU_REG9_WORD_LEN_64BIT 0x006
193#define CMU_REG9_WORD_LEN_66BIT 0x007
194#define CMU_REG9_TX_WORD_MODE_CH1_SET(dst, src) \
195 (((dst) & ~0x00000380) | (((u32) (src) << 7) & 0x00000380))
196#define CMU_REG9_TX_WORD_MODE_CH0_SET(dst, src) \
197 (((dst) & ~0x00000070) | (((u32) (src) << 4) & 0x00000070))
198#define CMU_REG9_PLL_POST_DIVBY2_SET(dst, src) \
199 (((dst) & ~0x00000008) | (((u32) (src) << 3) & 0x00000008))
200#define CMU_REG9_VBG_BYPASSB_SET(dst, src) \
201 (((dst) & ~0x00000004) | (((u32) (src) << 2) & 0x00000004))
202#define CMU_REG9_IGEN_BYPASS_SET(dst, src) \
203 (((dst) & ~0x00000002) | (((u32) (src) << 1) & 0x00000002))
204#define CMU_REG10 0x00014
205#define CMU_REG10_VREG_REFSEL_SET(dst, src) \
206 (((dst) & ~0x00000001) | (((u32) (src) << 0) & 0x00000001))
207#define CMU_REG11 0x00016
208#define CMU_REG12 0x00018
209#define CMU_REG12_STATE_DELAY9_SET(dst, src) \
210 (((dst) & ~0x000000f0) | (((u32) (src) << 4) & 0x000000f0))
211#define CMU_REG13 0x0001a
212#define CMU_REG14 0x0001c
213#define CMU_REG15 0x0001e
214#define CMU_REG16 0x00020
215#define CMU_REG16_PVT_DN_MAN_ENA_MASK 0x00000001
216#define CMU_REG16_PVT_UP_MAN_ENA_MASK 0x00000002
217#define CMU_REG16_VCOCAL_WAIT_BTW_CODE_SET(dst, src) \
218 (((dst) & ~0x0000001c) | (((u32) (src) << 2) & 0x0000001c))
219#define CMU_REG16_CALIBRATION_DONE_OVERRIDE_SET(dst, src) \
220 (((dst) & ~0x00000040) | (((u32) (src) << 6) & 0x00000040))
221#define CMU_REG16_BYPASS_PLL_LOCK_SET(dst, src) \
222 (((dst) & ~0x00000020) | (((u32) (src) << 5) & 0x00000020))
223#define CMU_REG17 0x00022
224#define CMU_REG17_PVT_CODE_R2A_SET(dst, src) \
225 (((dst) & ~0x00007f00) | (((u32) (src) << 8) & 0x00007f00))
226#define CMU_REG17_RESERVED_7_SET(dst, src) \
227 (((dst) & ~0x000000e0) | (((u32) (src) << 5) & 0x000000e0))
228#define CMU_REG17_PVT_TERM_MAN_ENA_MASK 0x00008000
229#define CMU_REG18 0x00024
230#define CMU_REG19 0x00026
231#define CMU_REG20 0x00028
232#define CMU_REG21 0x0002a
233#define CMU_REG22 0x0002c
234#define CMU_REG23 0x0002e
235#define CMU_REG24 0x00030
236#define CMU_REG25 0x00032
237#define CMU_REG26 0x00034
238#define CMU_REG26_FORCE_PLL_LOCK_SET(dst, src) \
239 (((dst) & ~0x00000001) | (((u32) (src) << 0) & 0x00000001))
240#define CMU_REG27 0x00036
241#define CMU_REG28 0x00038
242#define CMU_REG29 0x0003a
243#define CMU_REG30 0x0003c
244#define CMU_REG30_LOCK_COUNT_SET(dst, src) \
245 (((dst) & ~0x00000006) | (((u32) (src) << 1) & 0x00000006))
246#define CMU_REG30_PCIE_MODE_SET(dst, src) \
247 (((dst) & ~0x00000008) | (((u32) (src) << 3) & 0x00000008))
248#define CMU_REG31 0x0003e
249#define CMU_REG32 0x00040
250#define CMU_REG32_FORCE_VCOCAL_START_MASK 0x00004000
251#define CMU_REG32_PVT_CAL_WAIT_SEL_SET(dst, src) \
252 (((dst) & ~0x00000006) | (((u32) (src) << 1) & 0x00000006))
253#define CMU_REG32_IREF_ADJ_SET(dst, src) \
254 (((dst) & ~0x00000180) | (((u32) (src) << 7) & 0x00000180))
255#define CMU_REG33 0x00042
256#define CMU_REG34 0x00044
257#define CMU_REG34_VCO_CAL_VTH_LO_MAX_SET(dst, src) \
258 (((dst) & ~0x0000000f) | (((u32) (src) << 0) & 0x0000000f))
259#define CMU_REG34_VCO_CAL_VTH_HI_MAX_SET(dst, src) \
260 (((dst) & ~0x00000f00) | (((u32) (src) << 8) & 0x00000f00))
261#define CMU_REG34_VCO_CAL_VTH_LO_MIN_SET(dst, src) \
262 (((dst) & ~0x000000f0) | (((u32) (src) << 4) & 0x000000f0))
263#define CMU_REG34_VCO_CAL_VTH_HI_MIN_SET(dst, src) \
264 (((dst) & ~0x0000f000) | (((u32) (src) << 12) & 0x0000f000))
265#define CMU_REG35 0x00046
266#define CMU_REG35_PLL_SSC_MOD_SET(dst, src) \
267 (((dst) & ~0x0000fe00) | (((u32) (src) << 9) & 0x0000fe00))
268#define CMU_REG36 0x00048
269#define CMU_REG36_PLL_SSC_EN_SET(dst, src) \
270 (((dst) & ~0x00000010) | (((u32) (src) << 4) & 0x00000010))
271#define CMU_REG36_PLL_SSC_VSTEP_SET(dst, src) \
272 (((dst) & ~0x0000ffc0) | (((u32) (src) << 6) & 0x0000ffc0))
273#define CMU_REG36_PLL_SSC_DSMSEL_SET(dst, src) \
274 (((dst) & ~0x00000020) | (((u32) (src) << 5) & 0x00000020))
275#define CMU_REG37 0x0004a
276#define CMU_REG38 0x0004c
277#define CMU_REG39 0x0004e
278
279/* PHY lane CSR accessing from SDS indirectly */
280#define RXTX_REG0 0x000
281#define RXTX_REG0_CTLE_EQ_HR_SET(dst, src) \
282 (((dst) & ~0x0000f800) | (((u32) (src) << 11) & 0x0000f800))
283#define RXTX_REG0_CTLE_EQ_QR_SET(dst, src) \
284 (((dst) & ~0x000007c0) | (((u32) (src) << 6) & 0x000007c0))
285#define RXTX_REG0_CTLE_EQ_FR_SET(dst, src) \
286 (((dst) & ~0x0000003e) | (((u32) (src) << 1) & 0x0000003e))
287#define RXTX_REG1 0x002
288#define RXTX_REG1_RXACVCM_SET(dst, src) \
289 (((dst) & ~0x0000f000) | (((u32) (src) << 12) & 0x0000f000))
290#define RXTX_REG1_CTLE_EQ_SET(dst, src) \
291 (((dst) & ~0x00000f80) | (((u32) (src) << 7) & 0x00000f80))
292#define RXTX_REG1_RXVREG1_SET(dst, src) \
293 (((dst) & ~0x00000060) | (((u32) (src) << 5) & 0x00000060))
294#define RXTX_REG1_RXIREF_ADJ_SET(dst, src) \
295 (((dst) & ~0x00000006) | (((u32) (src) << 1) & 0x00000006))
296#define RXTX_REG2 0x004
297#define RXTX_REG2_VTT_ENA_SET(dst, src) \
298 (((dst) & ~0x00000100) | (((u32) (src) << 8) & 0x00000100))
299#define RXTX_REG2_TX_FIFO_ENA_SET(dst, src) \
300 (((dst) & ~0x00000020) | (((u32) (src) << 5) & 0x00000020))
301#define RXTX_REG2_VTT_SEL_SET(dst, src) \
302 (((dst) & ~0x000000c0) | (((u32) (src) << 6) & 0x000000c0))
303#define RXTX_REG4 0x008
304#define RXTX_REG4_TX_LOOPBACK_BUF_EN_MASK 0x00000040
305#define RXTX_REG4_TX_DATA_RATE_SET(dst, src) \
306 (((dst) & ~0x0000c000) | (((u32) (src) << 14) & 0x0000c000))
307#define RXTX_REG4_TX_WORD_MODE_SET(dst, src) \
308 (((dst) & ~0x00003800) | (((u32) (src) << 11) & 0x00003800))
309#define RXTX_REG5 0x00a
310#define RXTX_REG5_TX_CN1_SET(dst, src) \
311 (((dst) & ~0x0000f800) | (((u32) (src) << 11) & 0x0000f800))
312#define RXTX_REG5_TX_CP1_SET(dst, src) \
313 (((dst) & ~0x000007e0) | (((u32) (src) << 5) & 0x000007e0))
314#define RXTX_REG5_TX_CN2_SET(dst, src) \
315 (((dst) & ~0x0000001f) | (((u32) (src) << 0) & 0x0000001f))
316#define RXTX_REG6 0x00c
317#define RXTX_REG6_TXAMP_CNTL_SET(dst, src) \
318 (((dst) & ~0x00000780) | (((u32) (src) << 7) & 0x00000780))
319#define RXTX_REG6_TXAMP_ENA_SET(dst, src) \
320 (((dst) & ~0x00000040) | (((u32) (src) << 6) & 0x00000040))
321#define RXTX_REG6_RX_BIST_ERRCNT_RD_SET(dst, src) \
322 (((dst) & ~0x00000001) | (((u32) (src) << 0) & 0x00000001))
323#define RXTX_REG6_TX_IDLE_SET(dst, src) \
324 (((dst) & ~0x00000008) | (((u32) (src) << 3) & 0x00000008))
325#define RXTX_REG6_RX_BIST_RESYNC_SET(dst, src) \
326 (((dst) & ~0x00000002) | (((u32) (src) << 1) & 0x00000002))
327#define RXTX_REG7 0x00e
328#define RXTX_REG7_RESETB_RXD_MASK 0x00000100
329#define RXTX_REG7_RESETB_RXA_MASK 0x00000080
330#define RXTX_REG7_BIST_ENA_RX_SET(dst, src) \
331 (((dst) & ~0x00000040) | (((u32) (src) << 6) & 0x00000040))
332#define RXTX_REG7_RX_WORD_MODE_SET(dst, src) \
333 (((dst) & ~0x00003800) | (((u32) (src) << 11) & 0x00003800))
334#define RXTX_REG8 0x010
335#define RXTX_REG8_CDR_LOOP_ENA_SET(dst, src) \
336 (((dst) & ~0x00004000) | (((u32) (src) << 14) & 0x00004000))
337#define RXTX_REG8_CDR_BYPASS_RXLOS_SET(dst, src) \
338 (((dst) & ~0x00000800) | (((u32) (src) << 11) & 0x00000800))
339#define RXTX_REG8_SSC_ENABLE_SET(dst, src) \
340 (((dst) & ~0x00000200) | (((u32) (src) << 9) & 0x00000200))
341#define RXTX_REG8_SD_VREF_SET(dst, src) \
342 (((dst) & ~0x000000f0) | (((u32) (src) << 4) & 0x000000f0))
343#define RXTX_REG8_SD_DISABLE_SET(dst, src) \
344 (((dst) & ~0x00000100) | (((u32) (src) << 8) & 0x00000100))
345#define RXTX_REG7 0x00e
346#define RXTX_REG7_RESETB_RXD_SET(dst, src) \
347 (((dst) & ~0x00000100) | (((u32) (src) << 8) & 0x00000100))
348#define RXTX_REG7_RESETB_RXA_SET(dst, src) \
349 (((dst) & ~0x00000080) | (((u32) (src) << 7) & 0x00000080))
350#define RXTX_REG7_LOOP_BACK_ENA_CTLE_MASK 0x00004000
351#define RXTX_REG7_LOOP_BACK_ENA_CTLE_SET(dst, src) \
352 (((dst) & ~0x00004000) | (((u32) (src) << 14) & 0x00004000))
353#define RXTX_REG11 0x016
354#define RXTX_REG11_PHASE_ADJUST_LIMIT_SET(dst, src) \
355 (((dst) & ~0x0000f800) | (((u32) (src) << 11) & 0x0000f800))
356#define RXTX_REG12 0x018
357#define RXTX_REG12_LATCH_OFF_ENA_SET(dst, src) \
358 (((dst) & ~0x00002000) | (((u32) (src) << 13) & 0x00002000))
359#define RXTX_REG12_SUMOS_ENABLE_SET(dst, src) \
360 (((dst) & ~0x00000004) | (((u32) (src) << 2) & 0x00000004))
361#define RXTX_REG12_RX_DET_TERM_ENABLE_MASK 0x00000002
362#define RXTX_REG12_RX_DET_TERM_ENABLE_SET(dst, src) \
363 (((dst) & ~0x00000002) | (((u32) (src) << 1) & 0x00000002))
364#define RXTX_REG13 0x01a
365#define RXTX_REG14 0x01c
366#define RXTX_REG14_CLTE_LATCAL_MAN_PROG_SET(dst, src) \
367 (((dst) & ~0x0000003f) | (((u32) (src) << 0) & 0x0000003f))
368#define RXTX_REG14_CTLE_LATCAL_MAN_ENA_SET(dst, src) \
369 (((dst) & ~0x00000040) | (((u32) (src) << 6) & 0x00000040))
370#define RXTX_REG26 0x034
371#define RXTX_REG26_PERIOD_ERROR_LATCH_SET(dst, src) \
372 (((dst) & ~0x00003800) | (((u32) (src) << 11) & 0x00003800))
373#define RXTX_REG26_BLWC_ENA_SET(dst, src) \
374 (((dst) & ~0x00000008) | (((u32) (src) << 3) & 0x00000008))
375#define RXTX_REG21 0x02a
376#define RXTX_REG21_DO_LATCH_CALOUT_RD(src) ((0x0000fc00 & (u32) (src)) >> 10)
377#define RXTX_REG21_XO_LATCH_CALOUT_RD(src) ((0x000003f0 & (u32) (src)) >> 4)
378#define RXTX_REG21_LATCH_CAL_FAIL_ODD_RD(src) ((0x0000000f & (u32)(src)))
379#define RXTX_REG22 0x02c
380#define RXTX_REG22_SO_LATCH_CALOUT_RD(src) ((0x000003f0 & (u32) (src)) >> 4)
381#define RXTX_REG22_EO_LATCH_CALOUT_RD(src) ((0x0000fc00 & (u32) (src)) >> 10)
382#define RXTX_REG22_LATCH_CAL_FAIL_EVEN_RD(src) ((0x0000000f & (u32)(src)))
383#define RXTX_REG23 0x02e
384#define RXTX_REG23_DE_LATCH_CALOUT_RD(src) ((0x0000fc00 & (u32) (src)) >> 10)
385#define RXTX_REG23_XE_LATCH_CALOUT_RD(src) ((0x000003f0 & (u32) (src)) >> 4)
386#define RXTX_REG24 0x030
387#define RXTX_REG24_EE_LATCH_CALOUT_RD(src) ((0x0000fc00 & (u32) (src)) >> 10)
388#define RXTX_REG24_SE_LATCH_CALOUT_RD(src) ((0x000003f0 & (u32) (src)) >> 4)
389#define RXTX_REG27 0x036
390#define RXTX_REG28 0x038
391#define RXTX_REG31 0x03e
392#define RXTX_REG38 0x04c
393#define RXTX_REG38_CUSTOMER_PINMODE_INV_SET(dst, src) \
394 (((dst) & 0x0000fffe) | (((u32) (src) << 1) & 0x0000fffe))
395#define RXTX_REG39 0x04e
396#define RXTX_REG40 0x050
397#define RXTX_REG41 0x052
398#define RXTX_REG42 0x054
399#define RXTX_REG43 0x056
400#define RXTX_REG44 0x058
401#define RXTX_REG45 0x05a
402#define RXTX_REG46 0x05c
403#define RXTX_REG47 0x05e
404#define RXTX_REG48 0x060
405#define RXTX_REG49 0x062
406#define RXTX_REG50 0x064
407#define RXTX_REG51 0x066
408#define RXTX_REG52 0x068
409#define RXTX_REG53 0x06a
410#define RXTX_REG54 0x06c
411#define RXTX_REG55 0x06e
412#define RXTX_REG61 0x07a
413#define RXTX_REG61_ISCAN_INBERT_SET(dst, src) \
414 (((dst) & ~0x00000010) | (((u32) (src) << 4) & 0x00000010))
415#define RXTX_REG61_LOADFREQ_SHIFT_SET(dst, src) \
416 (((dst) & ~0x00000008) | (((u32) (src) << 3) & 0x00000008))
417#define RXTX_REG61_EYE_COUNT_WIDTH_SEL_SET(dst, src) \
418 (((dst) & ~0x000000c0) | (((u32) (src) << 6) & 0x000000c0))
419#define RXTX_REG61_SPD_SEL_CDR_SET(dst, src) \
420 (((dst) & ~0x00003c00) | (((u32) (src) << 10) & 0x00003c00))
421#define RXTX_REG62 0x07c
422#define RXTX_REG62_PERIOD_H1_QLATCH_SET(dst, src) \
423 (((dst) & ~0x00003800) | (((u32) (src) << 11) & 0x00003800))
424#define RXTX_REG81 0x0a2
425#define RXTX_REG89_MU_TH7_SET(dst, src) \
426 (((dst) & ~0x0000f800) | (((u32) (src) << 11) & 0x0000f800))
427#define RXTX_REG89_MU_TH8_SET(dst, src) \
428 (((dst) & ~0x000007c0) | (((u32) (src) << 6) & 0x000007c0))
429#define RXTX_REG89_MU_TH9_SET(dst, src) \
430 (((dst) & ~0x0000003e) | (((u32) (src) << 1) & 0x0000003e))
431#define RXTX_REG96 0x0c0
432#define RXTX_REG96_MU_FREQ1_SET(dst, src) \
433 (((dst) & ~0x0000f800) | (((u32) (src) << 11) & 0x0000f800))
434#define RXTX_REG96_MU_FREQ2_SET(dst, src) \
435 (((dst) & ~0x000007c0) | (((u32) (src) << 6) & 0x000007c0))
436#define RXTX_REG96_MU_FREQ3_SET(dst, src) \
437 (((dst) & ~0x0000003e) | (((u32) (src) << 1) & 0x0000003e))
438#define RXTX_REG99 0x0c6
439#define RXTX_REG99_MU_PHASE1_SET(dst, src) \
440 (((dst) & ~0x0000f800) | (((u32) (src) << 11) & 0x0000f800))
441#define RXTX_REG99_MU_PHASE2_SET(dst, src) \
442 (((dst) & ~0x000007c0) | (((u32) (src) << 6) & 0x000007c0))
443#define RXTX_REG99_MU_PHASE3_SET(dst, src) \
444 (((dst) & ~0x0000003e) | (((u32) (src) << 1) & 0x0000003e))
445#define RXTX_REG102 0x0cc
446#define RXTX_REG102_FREQLOOP_LIMIT_SET(dst, src) \
447 (((dst) & ~0x00000060) | (((u32) (src) << 5) & 0x00000060))
448#define RXTX_REG114 0x0e4
449#define RXTX_REG121 0x0f2
450#define RXTX_REG121_SUMOS_CAL_CODE_RD(src) ((0x0000003e & (u32)(src)) >> 0x1)
451#define RXTX_REG125 0x0fa
452#define RXTX_REG125_PQ_REG_SET(dst, src) \
453 (((dst) & ~0x0000fe00) | (((u32) (src) << 9) & 0x0000fe00))
454#define RXTX_REG125_SIGN_PQ_SET(dst, src) \
455 (((dst) & ~0x00000100) | (((u32) (src) << 8) & 0x00000100))
456#define RXTX_REG125_SIGN_PQ_2C_SET(dst, src) \
457 (((dst) & ~0x00000080) | (((u32) (src) << 7) & 0x00000080))
458#define RXTX_REG125_PHZ_MANUALCODE_SET(dst, src) \
459 (((dst) & ~0x0000007c) | (((u32) (src) << 2) & 0x0000007c))
460#define RXTX_REG125_PHZ_MANUAL_SET(dst, src) \
461 (((dst) & ~0x00000002) | (((u32) (src) << 1) & 0x00000002))
462#define RXTX_REG127 0x0fe
463#define RXTX_REG127_FORCE_SUM_CAL_START_MASK 0x00000002
464#define RXTX_REG127_FORCE_LAT_CAL_START_MASK 0x00000004
465#define RXTX_REG127_FORCE_SUM_CAL_START_SET(dst, src) \
466 (((dst) & ~0x00000002) | (((u32) (src) << 1) & 0x00000002))
467#define RXTX_REG127_FORCE_LAT_CAL_START_SET(dst, src) \
468 (((dst) & ~0x00000004) | (((u32) (src) << 2) & 0x00000004))
469#define RXTX_REG127_LATCH_MAN_CAL_ENA_SET(dst, src) \
470 (((dst) & ~0x00000008) | (((u32) (src) << 3) & 0x00000008))
471#define RXTX_REG127_DO_LATCH_MANCAL_SET(dst, src) \
472 (((dst) & ~0x0000fc00) | (((u32) (src) << 10) & 0x0000fc00))
473#define RXTX_REG127_XO_LATCH_MANCAL_SET(dst, src) \
474 (((dst) & ~0x000003f0) | (((u32) (src) << 4) & 0x000003f0))
475#define RXTX_REG128 0x100
476#define RXTX_REG128_LATCH_CAL_WAIT_SEL_SET(dst, src) \
477 (((dst) & ~0x0000000c) | (((u32) (src) << 2) & 0x0000000c))
478#define RXTX_REG128_EO_LATCH_MANCAL_SET(dst, src) \
479 (((dst) & ~0x0000fc00) | (((u32) (src) << 10) & 0x0000fc00))
480#define RXTX_REG128_SO_LATCH_MANCAL_SET(dst, src) \
481 (((dst) & ~0x000003f0) | (((u32) (src) << 4) & 0x000003f0))
482#define RXTX_REG129 0x102
483#define RXTX_REG129_DE_LATCH_MANCAL_SET(dst, src) \
484 (((dst) & ~0x0000fc00) | (((u32) (src) << 10) & 0x0000fc00))
485#define RXTX_REG129_XE_LATCH_MANCAL_SET(dst, src) \
486 (((dst) & ~0x000003f0) | (((u32) (src) << 4) & 0x000003f0))
487#define RXTX_REG130 0x104
488#define RXTX_REG130_EE_LATCH_MANCAL_SET(dst, src) \
489 (((dst) & ~0x0000fc00) | (((u32) (src) << 10) & 0x0000fc00))
490#define RXTX_REG130_SE_LATCH_MANCAL_SET(dst, src) \
491 (((dst) & ~0x000003f0) | (((u32) (src) << 4) & 0x000003f0))
492#define RXTX_REG145 0x122
493#define RXTX_REG145_TX_IDLE_SATA_SET(dst, src) \
494 (((dst) & ~0x00000001) | (((u32) (src) << 0) & 0x00000001))
495#define RXTX_REG145_RXES_ENA_SET(dst, src) \
496 (((dst) & ~0x00000002) | (((u32) (src) << 1) & 0x00000002))
497#define RXTX_REG145_RXDFE_CONFIG_SET(dst, src) \
498 (((dst) & ~0x0000c000) | (((u32) (src) << 14) & 0x0000c000))
499#define RXTX_REG145_RXVWES_LATENA_SET(dst, src) \
500 (((dst) & ~0x00000004) | (((u32) (src) << 2) & 0x00000004))
501#define RXTX_REG147 0x126
502#define RXTX_REG148 0x128
503
504/* Clock macro type */
505enum cmu_type_t {
506 REF_CMU = 0, /* Clock macro is the internal reference clock */
507 PHY_CMU = 1, /* Clock macro is the PLL for the Serdes */
508};
509
510enum mux_type_t {
511 MUX_SELECT_ATA = 0, /* Switch the MUX to ATA */
512 MUX_SELECT_SGMMII = 0, /* Switch the MUX to SGMII */
513};
514
515enum clk_type_t {
516 CLK_EXT_DIFF = 0, /* External differential */
517 CLK_INT_DIFF = 1, /* Internal differential */
518 CLK_INT_SING = 2, /* Internal single ended */
519};
520
521enum xgene_phy_mode {
522 MODE_SATA = 0, /* List them for simple reference */
523 MODE_SGMII = 1,
524 MODE_PCIE = 2,
525 MODE_USB = 3,
526 MODE_XFI = 4,
527 MODE_MAX
528};
529
530struct xgene_sata_override_param {
531 u32 speed[MAX_LANE]; /* Index for override parameter per lane */
532 u32 txspeed[3]; /* Tx speed */
533 u32 txboostgain[MAX_LANE*3]; /* Tx freq boost and gain control */
534 u32 txeyetuning[MAX_LANE*3]; /* Tx eye tuning */
535 u32 txeyedirection[MAX_LANE*3]; /* Tx eye tuning direction */
536 u32 txamplitude[MAX_LANE*3]; /* Tx amplitude control */
537 u32 txprecursor_cn1[MAX_LANE*3]; /* Tx emphasis taps 1st pre-cursor */
538 u32 txprecursor_cn2[MAX_LANE*3]; /* Tx emphasis taps 2nd pre-cursor */
539 u32 txpostcursor_cp1[MAX_LANE*3]; /* Tx emphasis taps post-cursor */
540};
541
542struct xgene_phy_ctx {
543 struct device *dev;
544 struct phy *phy;
545 enum xgene_phy_mode mode; /* Mode of operation */
546 enum clk_type_t clk_type; /* Input clock selection */
547 void __iomem *sds_base; /* PHY CSR base addr */
548 struct clk *clk; /* Optional clock */
549
550 /* Override Serdes parameters */
551 struct xgene_sata_override_param sata_param;
552};
553
554/*
555 * For chip earlier than A3 version, enable this flag.
556 * To enable, pass boot argument phy_xgene.preA3Chip=1
557 */
558static int preA3Chip;
559MODULE_PARM_DESC(preA3Chip, "Enable pre-A3 chip support (1=enable 0=disable)");
560module_param_named(preA3Chip, preA3Chip, int, 0444);
561
562static void sds_wr(void __iomem *csr_base, u32 indirect_cmd_reg,
563 u32 indirect_data_reg, u32 addr, u32 data)
564{
565 unsigned long deadline = jiffies + HZ;
566 u32 val;
567 u32 cmd;
568
569 cmd = CFG_IND_WR_CMD_MASK | CFG_IND_CMD_DONE_MASK;
570 cmd = CFG_IND_ADDR_SET(cmd, addr);
571 writel(data, csr_base + indirect_data_reg);
572 readl(csr_base + indirect_data_reg); /* Force a barrier */
573 writel(cmd, csr_base + indirect_cmd_reg);
574 readl(csr_base + indirect_cmd_reg); /* Force a barrier */
575 do {
576 val = readl(csr_base + indirect_cmd_reg);
577 } while (!(val & CFG_IND_CMD_DONE_MASK) &&
578 time_before(jiffies, deadline));
579 if (!(val & CFG_IND_CMD_DONE_MASK))
580 pr_err("SDS WR timeout at 0x%p offset 0x%08X value 0x%08X\n",
581 csr_base + indirect_cmd_reg, addr, data);
582}
583
584static void sds_rd(void __iomem *csr_base, u32 indirect_cmd_reg,
585 u32 indirect_data_reg, u32 addr, u32 *data)
586{
587 unsigned long deadline = jiffies + HZ;
588 u32 val;
589 u32 cmd;
590
591 cmd = CFG_IND_RD_CMD_MASK | CFG_IND_CMD_DONE_MASK;
592 cmd = CFG_IND_ADDR_SET(cmd, addr);
593 writel(cmd, csr_base + indirect_cmd_reg);
594 readl(csr_base + indirect_cmd_reg); /* Force a barrier */
595 do {
596 val = readl(csr_base + indirect_cmd_reg);
597 } while (!(val & CFG_IND_CMD_DONE_MASK) &&
598 time_before(jiffies, deadline));
599 *data = readl(csr_base + indirect_data_reg);
600 if (!(val & CFG_IND_CMD_DONE_MASK))
601 pr_err("SDS WR timeout at 0x%p offset 0x%08X value 0x%08X\n",
602 csr_base + indirect_cmd_reg, addr, *data);
603}
604
605static void cmu_wr(struct xgene_phy_ctx *ctx, enum cmu_type_t cmu_type,
606 u32 reg, u32 data)
607{
608 void __iomem *sds_base = ctx->sds_base;
609 u32 val;
610
611 if (cmu_type == REF_CMU)
612 reg += SERDES_PLL_REF_INDIRECT_OFFSET;
613 else
614 reg += SERDES_PLL_INDIRECT_OFFSET;
615 sds_wr(sds_base, SATA_ENET_SDS_IND_CMD_REG,
616 SATA_ENET_SDS_IND_WDATA_REG, reg, data);
617 sds_rd(sds_base, SATA_ENET_SDS_IND_CMD_REG,
618 SATA_ENET_SDS_IND_RDATA_REG, reg, &val);
619 pr_debug("CMU WR addr 0x%X value 0x%08X <-> 0x%08X\n", reg, data, val);
620}
621
622static void cmu_rd(struct xgene_phy_ctx *ctx, enum cmu_type_t cmu_type,
623 u32 reg, u32 *data)
624{
625 void __iomem *sds_base = ctx->sds_base;
626
627 if (cmu_type == REF_CMU)
628 reg += SERDES_PLL_REF_INDIRECT_OFFSET;
629 else
630 reg += SERDES_PLL_INDIRECT_OFFSET;
631 sds_rd(sds_base, SATA_ENET_SDS_IND_CMD_REG,
632 SATA_ENET_SDS_IND_RDATA_REG, reg, data);
633 pr_debug("CMU RD addr 0x%X value 0x%08X\n", reg, *data);
634}
635
636static void cmu_toggle1to0(struct xgene_phy_ctx *ctx, enum cmu_type_t cmu_type,
637 u32 reg, u32 bits)
638{
639 u32 val;
640
641 cmu_rd(ctx, cmu_type, reg, &val);
642 val |= bits;
643 cmu_wr(ctx, cmu_type, reg, val);
644 cmu_rd(ctx, cmu_type, reg, &val);
645 val &= ~bits;
646 cmu_wr(ctx, cmu_type, reg, val);
647}
648
649static void cmu_clrbits(struct xgene_phy_ctx *ctx, enum cmu_type_t cmu_type,
650 u32 reg, u32 bits)
651{
652 u32 val;
653
654 cmu_rd(ctx, cmu_type, reg, &val);
655 val &= ~bits;
656 cmu_wr(ctx, cmu_type, reg, val);
657}
658
659static void cmu_setbits(struct xgene_phy_ctx *ctx, enum cmu_type_t cmu_type,
660 u32 reg, u32 bits)
661{
662 u32 val;
663
664 cmu_rd(ctx, cmu_type, reg, &val);
665 val |= bits;
666 cmu_wr(ctx, cmu_type, reg, val);
667}
668
669static void serdes_wr(struct xgene_phy_ctx *ctx, int lane, u32 reg, u32 data)
670{
671 void __iomem *sds_base = ctx->sds_base;
672 u32 val;
673
674 reg += SERDES_INDIRECT_OFFSET;
675 reg += lane * SERDES_LANE_STRIDE;
676 sds_wr(sds_base, SATA_ENET_SDS_IND_CMD_REG,
677 SATA_ENET_SDS_IND_WDATA_REG, reg, data);
678 sds_rd(sds_base, SATA_ENET_SDS_IND_CMD_REG,
679 SATA_ENET_SDS_IND_RDATA_REG, reg, &val);
680 pr_debug("SERDES WR addr 0x%X value 0x%08X <-> 0x%08X\n", reg, data,
681 val);
682}
683
684static void serdes_rd(struct xgene_phy_ctx *ctx, int lane, u32 reg, u32 *data)
685{
686 void __iomem *sds_base = ctx->sds_base;
687
688 reg += SERDES_INDIRECT_OFFSET;
689 reg += lane * SERDES_LANE_STRIDE;
690 sds_rd(sds_base, SATA_ENET_SDS_IND_CMD_REG,
691 SATA_ENET_SDS_IND_RDATA_REG, reg, data);
692 pr_debug("SERDES RD addr 0x%X value 0x%08X\n", reg, *data);
693}
694
695static void serdes_clrbits(struct xgene_phy_ctx *ctx, int lane, u32 reg,
696 u32 bits)
697{
698 u32 val;
699
700 serdes_rd(ctx, lane, reg, &val);
701 val &= ~bits;
702 serdes_wr(ctx, lane, reg, val);
703}
704
705static void serdes_setbits(struct xgene_phy_ctx *ctx, int lane, u32 reg,
706 u32 bits)
707{
708 u32 val;
709
710 serdes_rd(ctx, lane, reg, &val);
711 val |= bits;
712 serdes_wr(ctx, lane, reg, val);
713}
714
715static void xgene_phy_cfg_cmu_clk_type(struct xgene_phy_ctx *ctx,
716 enum cmu_type_t cmu_type,
717 enum clk_type_t clk_type)
718{
719 u32 val;
720
721 /* Set the reset sequence delay for TX ready assertion */
722 cmu_rd(ctx, cmu_type, CMU_REG12, &val);
723 val = CMU_REG12_STATE_DELAY9_SET(val, 0x1);
724 cmu_wr(ctx, cmu_type, CMU_REG12, val);
725 /* Set the programmable stage delays between various enable stages */
726 cmu_wr(ctx, cmu_type, CMU_REG13, 0x0222);
727 cmu_wr(ctx, cmu_type, CMU_REG14, 0x2225);
728
729 /* Configure clock type */
730 if (clk_type == CLK_EXT_DIFF) {
731 /* Select external clock mux */
732 cmu_rd(ctx, cmu_type, CMU_REG0, &val);
733 val = CMU_REG0_PLL_REF_SEL_SET(val, 0x0);
734 cmu_wr(ctx, cmu_type, CMU_REG0, val);
735 /* Select CMOS as reference clock */
736 cmu_rd(ctx, cmu_type, CMU_REG1, &val);
737 val = CMU_REG1_REFCLK_CMOS_SEL_SET(val, 0x0);
738 cmu_wr(ctx, cmu_type, CMU_REG1, val);
739 dev_dbg(ctx->dev, "Set external reference clock\n");
740 } else if (clk_type == CLK_INT_DIFF) {
741 /* Select internal clock mux */
742 cmu_rd(ctx, cmu_type, CMU_REG0, &val);
743 val = CMU_REG0_PLL_REF_SEL_SET(val, 0x1);
744 cmu_wr(ctx, cmu_type, CMU_REG0, val);
745 /* Select CMOS as reference clock */
746 cmu_rd(ctx, cmu_type, CMU_REG1, &val);
747 val = CMU_REG1_REFCLK_CMOS_SEL_SET(val, 0x1);
748 cmu_wr(ctx, cmu_type, CMU_REG1, val);
749 dev_dbg(ctx->dev, "Set internal reference clock\n");
750 } else if (clk_type == CLK_INT_SING) {
751 /*
752 * NOTE: This clock type is NOT support for controller
753 * whose internal clock shared in the PCIe controller
754 *
755 * Select internal clock mux
756 */
757 cmu_rd(ctx, cmu_type, CMU_REG1, &val);
758 val = CMU_REG1_REFCLK_CMOS_SEL_SET(val, 0x1);
759 cmu_wr(ctx, cmu_type, CMU_REG1, val);
760 /* Select CML as reference clock */
761 cmu_rd(ctx, cmu_type, CMU_REG1, &val);
762 val = CMU_REG1_REFCLK_CMOS_SEL_SET(val, 0x0);
763 cmu_wr(ctx, cmu_type, CMU_REG1, val);
764 dev_dbg(ctx->dev,
765 "Set internal single ended reference clock\n");
766 }
767}
768
769static void xgene_phy_sata_cfg_cmu_core(struct xgene_phy_ctx *ctx,
770 enum cmu_type_t cmu_type,
771 enum clk_type_t clk_type)
772{
773 u32 val;
774 int ref_100MHz;
775
776 if (cmu_type == REF_CMU) {
777 /* Set VCO calibration voltage threshold */
778 cmu_rd(ctx, cmu_type, CMU_REG34, &val);
779 val = CMU_REG34_VCO_CAL_VTH_LO_MAX_SET(val, 0x7);
780 val = CMU_REG34_VCO_CAL_VTH_HI_MAX_SET(val, 0xc);
781 val = CMU_REG34_VCO_CAL_VTH_LO_MIN_SET(val, 0x3);
782 val = CMU_REG34_VCO_CAL_VTH_HI_MIN_SET(val, 0x8);
783 cmu_wr(ctx, cmu_type, CMU_REG34, val);
784 }
785
786 /* Set the VCO calibration counter */
787 cmu_rd(ctx, cmu_type, CMU_REG0, &val);
788 if (cmu_type == REF_CMU || preA3Chip)
789 val = CMU_REG0_CAL_COUNT_RESOL_SET(val, 0x4);
790 else
791 val = CMU_REG0_CAL_COUNT_RESOL_SET(val, 0x7);
792 cmu_wr(ctx, cmu_type, CMU_REG0, val);
793
794 /* Configure PLL for calibration */
795 cmu_rd(ctx, cmu_type, CMU_REG1, &val);
796 val = CMU_REG1_PLL_CP_SET(val, 0x1);
797 if (cmu_type == REF_CMU || preA3Chip)
798 val = CMU_REG1_PLL_CP_SEL_SET(val, 0x5);
799 else
800 val = CMU_REG1_PLL_CP_SEL_SET(val, 0x3);
801 if (cmu_type == REF_CMU)
802 val = CMU_REG1_PLL_MANUALCAL_SET(val, 0x0);
803 else
804 val = CMU_REG1_PLL_MANUALCAL_SET(val, 0x1);
805 cmu_wr(ctx, cmu_type, CMU_REG1, val);
806
807 if (cmu_type != REF_CMU)
808 cmu_clrbits(ctx, cmu_type, CMU_REG5, CMU_REG5_PLL_RESETB_MASK);
809
810 /* Configure the PLL for either 100MHz or 50MHz */
811 cmu_rd(ctx, cmu_type, CMU_REG2, &val);
812 if (cmu_type == REF_CMU) {
813 val = CMU_REG2_PLL_LFRES_SET(val, 0xa);
814 ref_100MHz = 1;
815 } else {
816 val = CMU_REG2_PLL_LFRES_SET(val, 0x3);
817 if (clk_type == CLK_EXT_DIFF)
818 ref_100MHz = 0;
819 else
820 ref_100MHz = 1;
821 }
822 if (ref_100MHz) {
823 val = CMU_REG2_PLL_FBDIV_SET(val, FBDIV_VAL_100M);
824 val = CMU_REG2_PLL_REFDIV_SET(val, REFDIV_VAL_100M);
825 } else {
826 val = CMU_REG2_PLL_FBDIV_SET(val, FBDIV_VAL_50M);
827 val = CMU_REG2_PLL_REFDIV_SET(val, REFDIV_VAL_50M);
828 }
829 cmu_wr(ctx, cmu_type, CMU_REG2, val);
830
831 /* Configure the VCO */
832 cmu_rd(ctx, cmu_type, CMU_REG3, &val);
833 if (cmu_type == REF_CMU) {
834 val = CMU_REG3_VCOVARSEL_SET(val, 0x3);
835 val = CMU_REG3_VCO_MOMSEL_INIT_SET(val, 0x10);
836 } else {
837 val = CMU_REG3_VCOVARSEL_SET(val, 0xF);
838 if (preA3Chip)
839 val = CMU_REG3_VCO_MOMSEL_INIT_SET(val, 0x15);
840 else
841 val = CMU_REG3_VCO_MOMSEL_INIT_SET(val, 0x1a);
842 val = CMU_REG3_VCO_MANMOMSEL_SET(val, 0x15);
843 }
844 cmu_wr(ctx, cmu_type, CMU_REG3, val);
845
846 /* Disable force PLL lock */
847 cmu_rd(ctx, cmu_type, CMU_REG26, &val);
848 val = CMU_REG26_FORCE_PLL_LOCK_SET(val, 0x0);
849 cmu_wr(ctx, cmu_type, CMU_REG26, val);
850
851 /* Setup PLL loop filter */
852 cmu_rd(ctx, cmu_type, CMU_REG5, &val);
853 val = CMU_REG5_PLL_LFSMCAP_SET(val, 0x3);
854 val = CMU_REG5_PLL_LFCAP_SET(val, 0x3);
855 if (cmu_type == REF_CMU || !preA3Chip)
856 val = CMU_REG5_PLL_LOCK_RESOLUTION_SET(val, 0x7);
857 else
858 val = CMU_REG5_PLL_LOCK_RESOLUTION_SET(val, 0x4);
859 cmu_wr(ctx, cmu_type, CMU_REG5, val);
860
861 /* Enable or disable manual calibration */
862 cmu_rd(ctx, cmu_type, CMU_REG6, &val);
863 val = CMU_REG6_PLL_VREGTRIM_SET(val, preA3Chip ? 0x0 : 0x2);
864 val = CMU_REG6_MAN_PVT_CAL_SET(val, preA3Chip ? 0x1 : 0x0);
865 cmu_wr(ctx, cmu_type, CMU_REG6, val);
866
867 /* Configure lane for 20-bits */
868 if (cmu_type == PHY_CMU) {
869 cmu_rd(ctx, cmu_type, CMU_REG9, &val);
870 val = CMU_REG9_TX_WORD_MODE_CH1_SET(val,
871 CMU_REG9_WORD_LEN_20BIT);
872 val = CMU_REG9_TX_WORD_MODE_CH0_SET(val,
873 CMU_REG9_WORD_LEN_20BIT);
874 val = CMU_REG9_PLL_POST_DIVBY2_SET(val, 0x1);
875 if (!preA3Chip) {
876 val = CMU_REG9_VBG_BYPASSB_SET(val, 0x0);
877 val = CMU_REG9_IGEN_BYPASS_SET(val , 0x0);
878 }
879 cmu_wr(ctx, cmu_type, CMU_REG9, val);
880
881 if (!preA3Chip) {
882 cmu_rd(ctx, cmu_type, CMU_REG10, &val);
883 val = CMU_REG10_VREG_REFSEL_SET(val, 0x1);
884 cmu_wr(ctx, cmu_type, CMU_REG10, val);
885 }
886 }
887
888 cmu_rd(ctx, cmu_type, CMU_REG16, &val);
889 val = CMU_REG16_CALIBRATION_DONE_OVERRIDE_SET(val, 0x1);
890 val = CMU_REG16_BYPASS_PLL_LOCK_SET(val, 0x1);
891 if (cmu_type == REF_CMU || preA3Chip)
892 val = CMU_REG16_VCOCAL_WAIT_BTW_CODE_SET(val, 0x4);
893 else
894 val = CMU_REG16_VCOCAL_WAIT_BTW_CODE_SET(val, 0x7);
895 cmu_wr(ctx, cmu_type, CMU_REG16, val);
896
897 /* Configure for SATA */
898 cmu_rd(ctx, cmu_type, CMU_REG30, &val);
899 val = CMU_REG30_PCIE_MODE_SET(val, 0x0);
900 val = CMU_REG30_LOCK_COUNT_SET(val, 0x3);
901 cmu_wr(ctx, cmu_type, CMU_REG30, val);
902
903 /* Disable state machine bypass */
904 cmu_wr(ctx, cmu_type, CMU_REG31, 0xF);
905
906 cmu_rd(ctx, cmu_type, CMU_REG32, &val);
907 val = CMU_REG32_PVT_CAL_WAIT_SEL_SET(val, 0x3);
908 if (cmu_type == REF_CMU || preA3Chip)
909 val = CMU_REG32_IREF_ADJ_SET(val, 0x3);
910 else
911 val = CMU_REG32_IREF_ADJ_SET(val, 0x1);
912 cmu_wr(ctx, cmu_type, CMU_REG32, val);
913
914 /* Set VCO calibration threshold */
915 if (cmu_type != REF_CMU && preA3Chip)
916 cmu_wr(ctx, cmu_type, CMU_REG34, 0x8d27);
917 else
918 cmu_wr(ctx, cmu_type, CMU_REG34, 0x873c);
919
920 /* Set CTLE Override and override waiting from state machine */
921 cmu_wr(ctx, cmu_type, CMU_REG37, 0xF00F);
922}
923
924static void xgene_phy_ssc_enable(struct xgene_phy_ctx *ctx,
925 enum cmu_type_t cmu_type)
926{
927 u32 val;
928
929 /* Set SSC modulation value */
930 cmu_rd(ctx, cmu_type, CMU_REG35, &val);
931 val = CMU_REG35_PLL_SSC_MOD_SET(val, 98);
932 cmu_wr(ctx, cmu_type, CMU_REG35, val);
933
934 /* Enable SSC, set vertical step and DSM value */
935 cmu_rd(ctx, cmu_type, CMU_REG36, &val);
936 val = CMU_REG36_PLL_SSC_VSTEP_SET(val, 30);
937 val = CMU_REG36_PLL_SSC_EN_SET(val, 1);
938 val = CMU_REG36_PLL_SSC_DSMSEL_SET(val, 1);
939 cmu_wr(ctx, cmu_type, CMU_REG36, val);
940
941 /* Reset the PLL */
942 cmu_clrbits(ctx, cmu_type, CMU_REG5, CMU_REG5_PLL_RESETB_MASK);
943 cmu_setbits(ctx, cmu_type, CMU_REG5, CMU_REG5_PLL_RESETB_MASK);
944
945 /* Force VCO calibration to restart */
946 cmu_toggle1to0(ctx, cmu_type, CMU_REG32,
947 CMU_REG32_FORCE_VCOCAL_START_MASK);
948}
949
950static void xgene_phy_sata_cfg_lanes(struct xgene_phy_ctx *ctx)
951{
952 u32 val;
953 u32 reg;
954 int i;
955 int lane;
956
957 for (lane = 0; lane < MAX_LANE; lane++) {
958 serdes_wr(ctx, lane, RXTX_REG147, 0x6);
959
960 /* Set boost control for quarter, half, and full rate */
961 serdes_rd(ctx, lane, RXTX_REG0, &val);
962 val = RXTX_REG0_CTLE_EQ_HR_SET(val, 0x10);
963 val = RXTX_REG0_CTLE_EQ_QR_SET(val, 0x10);
964 val = RXTX_REG0_CTLE_EQ_FR_SET(val, 0x10);
965 serdes_wr(ctx, lane, RXTX_REG0, val);
966
967 /* Set boost control value */
968 serdes_rd(ctx, lane, RXTX_REG1, &val);
969 val = RXTX_REG1_RXACVCM_SET(val, 0x7);
970 val = RXTX_REG1_CTLE_EQ_SET(val,
971 ctx->sata_param.txboostgain[lane * 3 +
972 ctx->sata_param.speed[lane]]);
973 serdes_wr(ctx, lane, RXTX_REG1, val);
974
975 /* Latch VTT value based on the termination to ground and
976 enable TX FIFO */
977 serdes_rd(ctx, lane, RXTX_REG2, &val);
978 val = RXTX_REG2_VTT_ENA_SET(val, 0x1);
979 val = RXTX_REG2_VTT_SEL_SET(val, 0x1);
980 val = RXTX_REG2_TX_FIFO_ENA_SET(val, 0x1);
981 serdes_wr(ctx, lane, RXTX_REG2, val);
982
983 /* Configure Tx for 20-bits */
984 serdes_rd(ctx, lane, RXTX_REG4, &val);
985 val = RXTX_REG4_TX_WORD_MODE_SET(val, CMU_REG9_WORD_LEN_20BIT);
986 serdes_wr(ctx, lane, RXTX_REG4, val);
987
988 if (!preA3Chip) {
989 serdes_rd(ctx, lane, RXTX_REG1, &val);
990 val = RXTX_REG1_RXVREG1_SET(val, 0x2);
991 val = RXTX_REG1_RXIREF_ADJ_SET(val, 0x2);
992 serdes_wr(ctx, lane, RXTX_REG1, val);
993 }
994
995 /* Set pre-emphasis first 1 and 2, and post-emphasis values */
996 serdes_rd(ctx, lane, RXTX_REG5, &val);
997 val = RXTX_REG5_TX_CN1_SET(val,
998 ctx->sata_param.txprecursor_cn1[lane * 3 +
999 ctx->sata_param.speed[lane]]);
1000 val = RXTX_REG5_TX_CP1_SET(val,
1001 ctx->sata_param.txpostcursor_cp1[lane * 3 +
1002 ctx->sata_param.speed[lane]]);
1003 val = RXTX_REG5_TX_CN2_SET(val,
1004 ctx->sata_param.txprecursor_cn2[lane * 3 +
1005 ctx->sata_param.speed[lane]]);
1006 serdes_wr(ctx, lane, RXTX_REG5, val);
1007
1008 /* Set TX amplitude value */
1009 serdes_rd(ctx, lane, RXTX_REG6, &val);
1010 val = RXTX_REG6_TXAMP_CNTL_SET(val,
1011 ctx->sata_param.txamplitude[lane * 3 +
1012 ctx->sata_param.speed[lane]]);
1013 val = RXTX_REG6_TXAMP_ENA_SET(val, 0x1);
1014 val = RXTX_REG6_TX_IDLE_SET(val, 0x0);
1015 val = RXTX_REG6_RX_BIST_RESYNC_SET(val, 0x0);
1016 val = RXTX_REG6_RX_BIST_ERRCNT_RD_SET(val, 0x0);
1017 serdes_wr(ctx, lane, RXTX_REG6, val);
1018
1019 /* Configure Rx for 20-bits */
1020 serdes_rd(ctx, lane, RXTX_REG7, &val);
1021 val = RXTX_REG7_BIST_ENA_RX_SET(val, 0x0);
1022 val = RXTX_REG7_RX_WORD_MODE_SET(val, CMU_REG9_WORD_LEN_20BIT);
1023 serdes_wr(ctx, lane, RXTX_REG7, val);
1024
1025 /* Set CDR and LOS values and enable Rx SSC */
1026 serdes_rd(ctx, lane, RXTX_REG8, &val);
1027 val = RXTX_REG8_CDR_LOOP_ENA_SET(val, 0x1);
1028 val = RXTX_REG8_CDR_BYPASS_RXLOS_SET(val, 0x0);
1029 val = RXTX_REG8_SSC_ENABLE_SET(val, 0x1);
1030 val = RXTX_REG8_SD_DISABLE_SET(val, 0x0);
1031 val = RXTX_REG8_SD_VREF_SET(val, 0x4);
1032 serdes_wr(ctx, lane, RXTX_REG8, val);
1033
1034 /* Set phase adjust upper/lower limits */
1035 serdes_rd(ctx, lane, RXTX_REG11, &val);
1036 val = RXTX_REG11_PHASE_ADJUST_LIMIT_SET(val, 0x0);
1037 serdes_wr(ctx, lane, RXTX_REG11, val);
1038
1039 /* Enable Latch Off; disable SUMOS and Tx termination */
1040 serdes_rd(ctx, lane, RXTX_REG12, &val);
1041 val = RXTX_REG12_LATCH_OFF_ENA_SET(val, 0x1);
1042 val = RXTX_REG12_SUMOS_ENABLE_SET(val, 0x0);
1043 val = RXTX_REG12_RX_DET_TERM_ENABLE_SET(val, 0x0);
1044 serdes_wr(ctx, lane, RXTX_REG12, val);
1045
1046 /* Set period error latch to 512T and enable BWL */
1047 serdes_rd(ctx, lane, RXTX_REG26, &val);
1048 val = RXTX_REG26_PERIOD_ERROR_LATCH_SET(val, 0x0);
1049 val = RXTX_REG26_BLWC_ENA_SET(val, 0x1);
1050 serdes_wr(ctx, lane, RXTX_REG26, val);
1051
1052 serdes_wr(ctx, lane, RXTX_REG28, 0x0);
1053
1054 /* Set DFE loop preset value */
1055 serdes_wr(ctx, lane, RXTX_REG31, 0x0);
1056
1057 /* Set Eye Monitor counter width to 12-bit */
1058 serdes_rd(ctx, lane, RXTX_REG61, &val);
1059 val = RXTX_REG61_ISCAN_INBERT_SET(val, 0x1);
1060 val = RXTX_REG61_LOADFREQ_SHIFT_SET(val, 0x0);
1061 val = RXTX_REG61_EYE_COUNT_WIDTH_SEL_SET(val, 0x0);
1062 serdes_wr(ctx, lane, RXTX_REG61, val);
1063
1064 serdes_rd(ctx, lane, RXTX_REG62, &val);
1065 val = RXTX_REG62_PERIOD_H1_QLATCH_SET(val, 0x0);
1066 serdes_wr(ctx, lane, RXTX_REG62, val);
1067
1068 /* Set BW select tap X for DFE loop */
1069 for (i = 0; i < 9; i++) {
1070 reg = RXTX_REG81 + i * 2;
1071 serdes_rd(ctx, lane, reg, &val);
1072 val = RXTX_REG89_MU_TH7_SET(val, 0xe);
1073 val = RXTX_REG89_MU_TH8_SET(val, 0xe);
1074 val = RXTX_REG89_MU_TH9_SET(val, 0xe);
1075 serdes_wr(ctx, lane, reg, val);
1076 }
1077
1078 /* Set BW select tap X for frequency adjust loop */
1079 for (i = 0; i < 3; i++) {
1080 reg = RXTX_REG96 + i * 2;
1081 serdes_rd(ctx, lane, reg, &val);
1082 val = RXTX_REG96_MU_FREQ1_SET(val, 0x10);
1083 val = RXTX_REG96_MU_FREQ2_SET(val, 0x10);
1084 val = RXTX_REG96_MU_FREQ3_SET(val, 0x10);
1085 serdes_wr(ctx, lane, reg, val);
1086 }
1087
1088 /* Set BW select tap X for phase adjust loop */
1089 for (i = 0; i < 3; i++) {
1090 reg = RXTX_REG99 + i * 2;
1091 serdes_rd(ctx, lane, reg, &val);
1092 val = RXTX_REG99_MU_PHASE1_SET(val, 0x7);
1093 val = RXTX_REG99_MU_PHASE2_SET(val, 0x7);
1094 val = RXTX_REG99_MU_PHASE3_SET(val, 0x7);
1095 serdes_wr(ctx, lane, reg, val);
1096 }
1097
1098 serdes_rd(ctx, lane, RXTX_REG102, &val);
1099 val = RXTX_REG102_FREQLOOP_LIMIT_SET(val, 0x0);
1100 serdes_wr(ctx, lane, RXTX_REG102, val);
1101
1102 serdes_wr(ctx, lane, RXTX_REG114, 0xffe0);
1103
1104 serdes_rd(ctx, lane, RXTX_REG125, &val);
1105 val = RXTX_REG125_SIGN_PQ_SET(val,
1106 ctx->sata_param.txeyedirection[lane * 3 +
1107 ctx->sata_param.speed[lane]]);
1108 val = RXTX_REG125_PQ_REG_SET(val,
1109 ctx->sata_param.txeyetuning[lane * 3 +
1110 ctx->sata_param.speed[lane]]);
1111 val = RXTX_REG125_PHZ_MANUAL_SET(val, 0x1);
1112 serdes_wr(ctx, lane, RXTX_REG125, val);
1113
1114 serdes_rd(ctx, lane, RXTX_REG127, &val);
1115 val = RXTX_REG127_LATCH_MAN_CAL_ENA_SET(val, 0x0);
1116 serdes_wr(ctx, lane, RXTX_REG127, val);
1117
1118 serdes_rd(ctx, lane, RXTX_REG128, &val);
1119 val = RXTX_REG128_LATCH_CAL_WAIT_SEL_SET(val, 0x3);
1120 serdes_wr(ctx, lane, RXTX_REG128, val);
1121
1122 serdes_rd(ctx, lane, RXTX_REG145, &val);
1123 val = RXTX_REG145_RXDFE_CONFIG_SET(val, 0x3);
1124 val = RXTX_REG145_TX_IDLE_SATA_SET(val, 0x0);
1125 if (preA3Chip) {
1126 val = RXTX_REG145_RXES_ENA_SET(val, 0x1);
1127 val = RXTX_REG145_RXVWES_LATENA_SET(val, 0x1);
1128 } else {
1129 val = RXTX_REG145_RXES_ENA_SET(val, 0x0);
1130 val = RXTX_REG145_RXVWES_LATENA_SET(val, 0x0);
1131 }
1132 serdes_wr(ctx, lane, RXTX_REG145, val);
1133
1134 /*
1135 * Set Rx LOS filter clock rate, sample rate, and threshold
1136 * windows
1137 */
1138 for (i = 0; i < 4; i++) {
1139 reg = RXTX_REG148 + i * 2;
1140 serdes_wr(ctx, lane, reg, 0xFFFF);
1141 }
1142 }
1143}
1144
1145static int xgene_phy_cal_rdy_chk(struct xgene_phy_ctx *ctx,
1146 enum cmu_type_t cmu_type,
1147 enum clk_type_t clk_type)
1148{
1149 void __iomem *csr_serdes = ctx->sds_base;
1150 int loop;
1151 u32 val;
1152
1153 /* Release PHY main reset */
1154 writel(0xdf, csr_serdes + SATA_ENET_SDS_RST_CTL);
1155 readl(csr_serdes + SATA_ENET_SDS_RST_CTL); /* Force a barrier */
1156
1157 if (cmu_type != REF_CMU) {
1158 cmu_setbits(ctx, cmu_type, CMU_REG5, CMU_REG5_PLL_RESETB_MASK);
1159 /*
1160 * As per PHY design spec, the PLL reset requires a minimum
1161 * of 800us.
1162 */
1163 usleep_range(800, 1000);
1164
1165 cmu_rd(ctx, cmu_type, CMU_REG1, &val);
1166 val = CMU_REG1_PLL_MANUALCAL_SET(val, 0x0);
1167 cmu_wr(ctx, cmu_type, CMU_REG1, val);
1168 /*
1169 * As per PHY design spec, the PLL auto calibration requires
1170 * a minimum of 800us.
1171 */
1172 usleep_range(800, 1000);
1173
1174 cmu_toggle1to0(ctx, cmu_type, CMU_REG32,
1175 CMU_REG32_FORCE_VCOCAL_START_MASK);
1176 /*
1177 * As per PHY design spec, the PLL requires a minimum of
1178 * 800us to settle.
1179 */
1180 usleep_range(800, 1000);
1181 }
1182
1183 if (!preA3Chip)
1184 goto skip_manual_cal;
1185
1186 /*
1187 * Configure the termination resister calibration
1188 * The serial receive pins, RXP/RXN, have TERMination resistor
1189 * that is required to be calibrated.
1190 */
1191 cmu_rd(ctx, cmu_type, CMU_REG17, &val);
1192 val = CMU_REG17_PVT_CODE_R2A_SET(val, 0x12);
1193 val = CMU_REG17_RESERVED_7_SET(val, 0x0);
1194 cmu_wr(ctx, cmu_type, CMU_REG17, val);
1195 cmu_toggle1to0(ctx, cmu_type, CMU_REG17,
1196 CMU_REG17_PVT_TERM_MAN_ENA_MASK);
1197 /*
1198 * The serial transmit pins, TXP/TXN, have Pull-UP and Pull-DOWN
1199 * resistors that are required to the calibrated.
1200 * Configure the pull DOWN calibration
1201 */
1202 cmu_rd(ctx, cmu_type, CMU_REG17, &val);
1203 val = CMU_REG17_PVT_CODE_R2A_SET(val, 0x29);
1204 val = CMU_REG17_RESERVED_7_SET(val, 0x0);
1205 cmu_wr(ctx, cmu_type, CMU_REG17, val);
1206 cmu_toggle1to0(ctx, cmu_type, CMU_REG16,
1207 CMU_REG16_PVT_DN_MAN_ENA_MASK);
1208 /* Configure the pull UP calibration */
1209 cmu_rd(ctx, cmu_type, CMU_REG17, &val);
1210 val = CMU_REG17_PVT_CODE_R2A_SET(val, 0x28);
1211 val = CMU_REG17_RESERVED_7_SET(val, 0x0);
1212 cmu_wr(ctx, cmu_type, CMU_REG17, val);
1213 cmu_toggle1to0(ctx, cmu_type, CMU_REG16,
1214 CMU_REG16_PVT_UP_MAN_ENA_MASK);
1215
1216skip_manual_cal:
1217 /* Poll the PLL calibration completion status for at least 1 ms */
1218 loop = 100;
1219 do {
1220 cmu_rd(ctx, cmu_type, CMU_REG7, &val);
1221 if (CMU_REG7_PLL_CALIB_DONE_RD(val))
1222 break;
1223 /*
1224 * As per PHY design spec, PLL calibration status requires
1225 * a minimum of 10us to be updated.
1226 */
1227 usleep_range(10, 100);
1228 } while (--loop > 0);
1229
1230 cmu_rd(ctx, cmu_type, CMU_REG7, &val);
1231 dev_dbg(ctx->dev, "PLL calibration %s\n",
1232 CMU_REG7_PLL_CALIB_DONE_RD(val) ? "done" : "failed");
1233 if (CMU_REG7_VCO_CAL_FAIL_RD(val)) {
1234 dev_err(ctx->dev,
1235 "PLL calibration failed due to VCO failure\n");
1236 return -1;
1237 }
1238 dev_dbg(ctx->dev, "PLL calibration successful\n");
1239
1240 cmu_rd(ctx, cmu_type, CMU_REG15, &val);
1241 dev_dbg(ctx->dev, "PHY Tx is %sready\n", val & 0x300 ? "" : "not ");
1242 return 0;
1243}
1244
1245static void xgene_phy_pdwn_force_vco(struct xgene_phy_ctx *ctx,
1246 enum cmu_type_t cmu_type,
1247 enum clk_type_t clk_type)
1248{
1249 u32 val;
1250
1251 dev_dbg(ctx->dev, "Reset VCO and re-start again\n");
1252 if (cmu_type == PHY_CMU) {
1253 cmu_rd(ctx, cmu_type, CMU_REG16, &val);
1254 val = CMU_REG16_VCOCAL_WAIT_BTW_CODE_SET(val, 0x7);
1255 cmu_wr(ctx, cmu_type, CMU_REG16, val);
1256 }
1257
1258 cmu_toggle1to0(ctx, cmu_type, CMU_REG0, CMU_REG0_PDOWN_MASK);
1259 cmu_toggle1to0(ctx, cmu_type, CMU_REG32,
1260 CMU_REG32_FORCE_VCOCAL_START_MASK);
1261}
1262
1263static int xgene_phy_hw_init_sata(struct xgene_phy_ctx *ctx,
1264 enum clk_type_t clk_type, int ssc_enable)
1265{
1266 void __iomem *sds_base = ctx->sds_base;
1267 u32 val;
1268 int i;
1269
1270 /* Configure the PHY for operation */
1271 dev_dbg(ctx->dev, "Reset PHY\n");
1272 /* Place PHY into reset */
1273 writel(0x0, sds_base + SATA_ENET_SDS_RST_CTL);
1274 val = readl(sds_base + SATA_ENET_SDS_RST_CTL); /* Force a barrier */
1275 /* Release PHY lane from reset (active high) */
1276 writel(0x20, sds_base + SATA_ENET_SDS_RST_CTL);
1277 readl(sds_base + SATA_ENET_SDS_RST_CTL); /* Force a barrier */
1278 /* Release all PHY module out of reset except PHY main reset */
1279 writel(0xde, sds_base + SATA_ENET_SDS_RST_CTL);
1280 readl(sds_base + SATA_ENET_SDS_RST_CTL); /* Force a barrier */
1281
1282 /* Set the operation speed */
1283 val = readl(sds_base + SATA_ENET_SDS_CTL1);
1284 val = CFG_I_SPD_SEL_CDR_OVR1_SET(val,
1285 ctx->sata_param.txspeed[ctx->sata_param.speed[0]]);
1286 writel(val, sds_base + SATA_ENET_SDS_CTL1);
1287
1288 dev_dbg(ctx->dev, "Set the customer pin mode to SATA\n");
1289 val = readl(sds_base + SATA_ENET_SDS_CTL0);
1290 val = REGSPEC_CFG_I_CUSTOMER_PIN_MODE0_SET(val, 0x4421);
1291 writel(val, sds_base + SATA_ENET_SDS_CTL0);
1292
1293 /* Configure the clock macro unit (CMU) clock type */
1294 xgene_phy_cfg_cmu_clk_type(ctx, PHY_CMU, clk_type);
1295
1296 /* Configure the clock macro */
1297 xgene_phy_sata_cfg_cmu_core(ctx, PHY_CMU, clk_type);
1298
1299 /* Enable SSC if enabled */
1300 if (ssc_enable)
1301 xgene_phy_ssc_enable(ctx, PHY_CMU);
1302
1303 /* Configure PHY lanes */
1304 xgene_phy_sata_cfg_lanes(ctx);
1305
1306 /* Set Rx/Tx 20-bit */
1307 val = readl(sds_base + SATA_ENET_SDS_PCS_CTL0);
1308 val = REGSPEC_CFG_I_RX_WORDMODE0_SET(val, 0x3);
1309 val = REGSPEC_CFG_I_TX_WORDMODE0_SET(val, 0x3);
1310 writel(val, sds_base + SATA_ENET_SDS_PCS_CTL0);
1311
1312 /* Start PLL calibration and try for three times */
1313 i = 10;
1314 do {
1315 if (!xgene_phy_cal_rdy_chk(ctx, PHY_CMU, clk_type))
1316 break;
1317 /* If failed, toggle the VCO power signal and start again */
1318 xgene_phy_pdwn_force_vco(ctx, PHY_CMU, clk_type);
1319 } while (--i > 0);
1320 /* Even on failure, allow to continue any way */
1321 if (i <= 0)
1322 dev_err(ctx->dev, "PLL calibration failed\n");
1323
1324 return 0;
1325}
1326
1327static int xgene_phy_hw_initialize(struct xgene_phy_ctx *ctx,
1328 enum clk_type_t clk_type,
1329 int ssc_enable)
1330{
1331 int rc;
1332
1333 dev_dbg(ctx->dev, "PHY init clk type %d\n", clk_type);
1334
1335 if (ctx->mode == MODE_SATA) {
1336 rc = xgene_phy_hw_init_sata(ctx, clk_type, ssc_enable);
1337 if (rc)
1338 return rc;
1339 } else {
1340 dev_err(ctx->dev, "Un-supported customer pin mode %d\n",
1341 ctx->mode);
1342 return -ENODEV;
1343 }
1344
1345 return 0;
1346}
1347
1348/*
1349 * Receiver Offset Calibration:
1350 *
1351 * Calibrate the receiver signal path offset in two steps - summar and
1352 * latch calibrations
1353 */
1354static void xgene_phy_force_lat_summer_cal(struct xgene_phy_ctx *ctx, int lane)
1355{
1356 int i;
1357 struct {
1358 u32 reg;
1359 u32 val;
1360 } serdes_reg[] = {
1361 {RXTX_REG38, 0x0},
1362 {RXTX_REG39, 0xff00},
1363 {RXTX_REG40, 0xffff},
1364 {RXTX_REG41, 0xffff},
1365 {RXTX_REG42, 0xffff},
1366 {RXTX_REG43, 0xffff},
1367 {RXTX_REG44, 0xffff},
1368 {RXTX_REG45, 0xffff},
1369 {RXTX_REG46, 0xffff},
1370 {RXTX_REG47, 0xfffc},
1371 {RXTX_REG48, 0x0},
1372 {RXTX_REG49, 0x0},
1373 {RXTX_REG50, 0x0},
1374 {RXTX_REG51, 0x0},
1375 {RXTX_REG52, 0x0},
1376 {RXTX_REG53, 0x0},
1377 {RXTX_REG54, 0x0},
1378 {RXTX_REG55, 0x0},
1379 };
1380
1381 /* Start SUMMER calibration */
1382 serdes_setbits(ctx, lane, RXTX_REG127,
1383 RXTX_REG127_FORCE_SUM_CAL_START_MASK);
1384 /*
1385 * As per PHY design spec, the Summer calibration requires a minimum
1386 * of 100us to complete.
1387 */
1388 usleep_range(100, 500);
1389 serdes_clrbits(ctx, lane, RXTX_REG127,
1390 RXTX_REG127_FORCE_SUM_CAL_START_MASK);
1391 /*
1392 * As per PHY design spec, the auto calibration requires a minimum
1393 * of 100us to complete.
1394 */
1395 usleep_range(100, 500);
1396
1397 /* Start latch calibration */
1398 serdes_setbits(ctx, lane, RXTX_REG127,
1399 RXTX_REG127_FORCE_LAT_CAL_START_MASK);
1400 /*
1401 * As per PHY design spec, the latch calibration requires a minimum
1402 * of 100us to complete.
1403 */
1404 usleep_range(100, 500);
1405 serdes_clrbits(ctx, lane, RXTX_REG127,
1406 RXTX_REG127_FORCE_LAT_CAL_START_MASK);
1407
1408 /* Configure the PHY lane for calibration */
1409 serdes_wr(ctx, lane, RXTX_REG28, 0x7);
1410 serdes_wr(ctx, lane, RXTX_REG31, 0x7e00);
1411 serdes_clrbits(ctx, lane, RXTX_REG4,
1412 RXTX_REG4_TX_LOOPBACK_BUF_EN_MASK);
1413 serdes_clrbits(ctx, lane, RXTX_REG7,
1414 RXTX_REG7_LOOP_BACK_ENA_CTLE_MASK);
1415 for (i = 0; i < ARRAY_SIZE(serdes_reg); i++)
1416 serdes_wr(ctx, lane, serdes_reg[i].reg,
1417 serdes_reg[i].val);
1418}
1419
1420static void xgene_phy_reset_rxd(struct xgene_phy_ctx *ctx, int lane)
1421{
1422 /* Reset digital Rx */
1423 serdes_clrbits(ctx, lane, RXTX_REG7, RXTX_REG7_RESETB_RXD_MASK);
1424 /* As per PHY design spec, the reset requires a minimum of 100us. */
1425 usleep_range(100, 150);
1426 serdes_setbits(ctx, lane, RXTX_REG7, RXTX_REG7_RESETB_RXD_MASK);
1427}
1428
1429static int xgene_phy_get_avg(int accum, int samples)
1430{
1431 return (accum + (samples / 2)) / samples;
1432}
1433
1434static void xgene_phy_gen_avg_val(struct xgene_phy_ctx *ctx, int lane)
1435{
1436 int max_loop = 10;
1437 int avg_loop = 0;
1438 int lat_do = 0, lat_xo = 0, lat_eo = 0, lat_so = 0;
1439 int lat_de = 0, lat_xe = 0, lat_ee = 0, lat_se = 0;
1440 int sum_cal = 0;
1441 int lat_do_itr, lat_xo_itr, lat_eo_itr, lat_so_itr;
1442 int lat_de_itr, lat_xe_itr, lat_ee_itr, lat_se_itr;
1443 int sum_cal_itr;
1444 int fail_even;
1445 int fail_odd;
1446 u32 val;
1447
1448 dev_dbg(ctx->dev, "Generating avg calibration value for lane %d\n",
1449 lane);
1450
1451 /* Enable RX Hi-Z termination */
1452 serdes_setbits(ctx, lane, RXTX_REG12,
1453 RXTX_REG12_RX_DET_TERM_ENABLE_MASK);
1454 /* Turn off DFE */
1455 serdes_wr(ctx, lane, RXTX_REG28, 0x0000);
1456 /* DFE Presets to zero */
1457 serdes_wr(ctx, lane, RXTX_REG31, 0x0000);
1458
1459 /*
1460 * Receiver Offset Calibration:
1461 * Calibrate the receiver signal path offset in two steps - summar
1462 * and latch calibration.
1463 * Runs the "Receiver Offset Calibration multiple times to determine
1464 * the average value to use.
1465 */
1466 while (avg_loop < max_loop) {
1467 /* Start the calibration */
1468 xgene_phy_force_lat_summer_cal(ctx, lane);
1469
1470 serdes_rd(ctx, lane, RXTX_REG21, &val);
1471 lat_do_itr = RXTX_REG21_DO_LATCH_CALOUT_RD(val);
1472 lat_xo_itr = RXTX_REG21_XO_LATCH_CALOUT_RD(val);
1473 fail_odd = RXTX_REG21_LATCH_CAL_FAIL_ODD_RD(val);
1474
1475 serdes_rd(ctx, lane, RXTX_REG22, &val);
1476 lat_eo_itr = RXTX_REG22_EO_LATCH_CALOUT_RD(val);
1477 lat_so_itr = RXTX_REG22_SO_LATCH_CALOUT_RD(val);
1478 fail_even = RXTX_REG22_LATCH_CAL_FAIL_EVEN_RD(val);
1479
1480 serdes_rd(ctx, lane, RXTX_REG23, &val);
1481 lat_de_itr = RXTX_REG23_DE_LATCH_CALOUT_RD(val);
1482 lat_xe_itr = RXTX_REG23_XE_LATCH_CALOUT_RD(val);
1483
1484 serdes_rd(ctx, lane, RXTX_REG24, &val);
1485 lat_ee_itr = RXTX_REG24_EE_LATCH_CALOUT_RD(val);
1486 lat_se_itr = RXTX_REG24_SE_LATCH_CALOUT_RD(val);
1487
1488 serdes_rd(ctx, lane, RXTX_REG121, &val);
1489 sum_cal_itr = RXTX_REG121_SUMOS_CAL_CODE_RD(val);
1490
1491 /* Check for failure. If passed, sum them for averaging */
1492 if ((fail_even == 0 || fail_even == 1) &&
1493 (fail_odd == 0 || fail_odd == 1)) {
1494 lat_do += lat_do_itr;
1495 lat_xo += lat_xo_itr;
1496 lat_eo += lat_eo_itr;
1497 lat_so += lat_so_itr;
1498 lat_de += lat_de_itr;
1499 lat_xe += lat_xe_itr;
1500 lat_ee += lat_ee_itr;
1501 lat_se += lat_se_itr;
1502 sum_cal += sum_cal_itr;
1503
1504 dev_dbg(ctx->dev, "Iteration %d:\n", avg_loop);
1505 dev_dbg(ctx->dev, "DO 0x%x XO 0x%x EO 0x%x SO 0x%x\n",
1506 lat_do_itr, lat_xo_itr, lat_eo_itr,
1507 lat_so_itr);
1508 dev_dbg(ctx->dev, "DE 0x%x XE 0x%x EE 0x%x SE 0x%x\n",
1509 lat_de_itr, lat_xe_itr, lat_ee_itr,
1510 lat_se_itr);
1511 dev_dbg(ctx->dev, "SUM 0x%x\n", sum_cal_itr);
1512 ++avg_loop;
1513 } else {
1514 dev_err(ctx->dev,
1515 "Receiver calibration failed at %d loop\n",
1516 avg_loop);
1517 }
1518 xgene_phy_reset_rxd(ctx, lane);
1519 }
1520
1521 /* Update latch manual calibration with average value */
1522 serdes_rd(ctx, lane, RXTX_REG127, &val);
1523 val = RXTX_REG127_DO_LATCH_MANCAL_SET(val,
1524 xgene_phy_get_avg(lat_do, max_loop));
1525 val = RXTX_REG127_XO_LATCH_MANCAL_SET(val,
1526 xgene_phy_get_avg(lat_xo, max_loop));
1527 serdes_wr(ctx, lane, RXTX_REG127, val);
1528
1529 serdes_rd(ctx, lane, RXTX_REG128, &val);
1530 val = RXTX_REG128_EO_LATCH_MANCAL_SET(val,
1531 xgene_phy_get_avg(lat_eo, max_loop));
1532 val = RXTX_REG128_SO_LATCH_MANCAL_SET(val,
1533 xgene_phy_get_avg(lat_so, max_loop));
1534 serdes_wr(ctx, lane, RXTX_REG128, val);
1535
1536 serdes_rd(ctx, lane, RXTX_REG129, &val);
1537 val = RXTX_REG129_DE_LATCH_MANCAL_SET(val,
1538 xgene_phy_get_avg(lat_de, max_loop));
1539 val = RXTX_REG129_XE_LATCH_MANCAL_SET(val,
1540 xgene_phy_get_avg(lat_xe, max_loop));
1541 serdes_wr(ctx, lane, RXTX_REG129, val);
1542
1543 serdes_rd(ctx, lane, RXTX_REG130, &val);
1544 val = RXTX_REG130_EE_LATCH_MANCAL_SET(val,
1545 xgene_phy_get_avg(lat_ee, max_loop));
1546 val = RXTX_REG130_SE_LATCH_MANCAL_SET(val,
1547 xgene_phy_get_avg(lat_se, max_loop));
1548 serdes_wr(ctx, lane, RXTX_REG130, val);
1549
1550 /* Update SUMMER calibration with average value */
1551 serdes_rd(ctx, lane, RXTX_REG14, &val);
1552 val = RXTX_REG14_CLTE_LATCAL_MAN_PROG_SET(val,
1553 xgene_phy_get_avg(sum_cal, max_loop));
1554 serdes_wr(ctx, lane, RXTX_REG14, val);
1555
1556 dev_dbg(ctx->dev, "Average Value:\n");
1557 dev_dbg(ctx->dev, "DO 0x%x XO 0x%x EO 0x%x SO 0x%x\n",
1558 xgene_phy_get_avg(lat_do, max_loop),
1559 xgene_phy_get_avg(lat_xo, max_loop),
1560 xgene_phy_get_avg(lat_eo, max_loop),
1561 xgene_phy_get_avg(lat_so, max_loop));
1562 dev_dbg(ctx->dev, "DE 0x%x XE 0x%x EE 0x%x SE 0x%x\n",
1563 xgene_phy_get_avg(lat_de, max_loop),
1564 xgene_phy_get_avg(lat_xe, max_loop),
1565 xgene_phy_get_avg(lat_ee, max_loop),
1566 xgene_phy_get_avg(lat_se, max_loop));
1567 dev_dbg(ctx->dev, "SUM 0x%x\n",
1568 xgene_phy_get_avg(sum_cal, max_loop));
1569
1570 serdes_rd(ctx, lane, RXTX_REG14, &val);
1571 val = RXTX_REG14_CTLE_LATCAL_MAN_ENA_SET(val, 0x1);
1572 serdes_wr(ctx, lane, RXTX_REG14, val);
1573 dev_dbg(ctx->dev, "Enable Manual Summer calibration\n");
1574
1575 serdes_rd(ctx, lane, RXTX_REG127, &val);
1576 val = RXTX_REG127_LATCH_MAN_CAL_ENA_SET(val, 0x1);
1577 dev_dbg(ctx->dev, "Enable Manual Latch calibration\n");
1578 serdes_wr(ctx, lane, RXTX_REG127, val);
1579
1580 /* Disable RX Hi-Z termination */
1581 serdes_rd(ctx, lane, RXTX_REG12, &val);
1582 val = RXTX_REG12_RX_DET_TERM_ENABLE_SET(val, 0);
1583 serdes_wr(ctx, lane, RXTX_REG12, val);
1584 /* Turn on DFE */
1585 serdes_wr(ctx, lane, RXTX_REG28, 0x0007);
1586 /* Set DFE preset */
1587 serdes_wr(ctx, lane, RXTX_REG31, 0x7e00);
1588}
1589
1590static int xgene_phy_hw_init(struct phy *phy)
1591{
1592 struct xgene_phy_ctx *ctx = phy_get_drvdata(phy);
1593 int rc;
1594 int i;
1595
1596 rc = xgene_phy_hw_initialize(ctx, CLK_EXT_DIFF, SSC_DISABLE);
1597 if (rc) {
1598 dev_err(ctx->dev, "PHY initialize failed %d\n", rc);
1599 return rc;
1600 }
1601
1602 /* Setup clock properly after PHY configuration */
1603 if (!IS_ERR(ctx->clk)) {
1604 /* HW requires an toggle of the clock */
1605 clk_prepare_enable(ctx->clk);
1606 clk_disable_unprepare(ctx->clk);
1607 clk_prepare_enable(ctx->clk);
1608 }
1609
1610 /* Compute average value */
1611 for (i = 0; i < MAX_LANE; i++)
1612 xgene_phy_gen_avg_val(ctx, i);
1613
1614 dev_dbg(ctx->dev, "PHY initialized\n");
1615 return 0;
1616}
1617
1618static const struct phy_ops xgene_phy_ops = {
1619 .init = xgene_phy_hw_init,
1620 .owner = THIS_MODULE,
1621};
1622
1623static struct phy *xgene_phy_xlate(struct device *dev,
1624 struct of_phandle_args *args)
1625{
1626 struct xgene_phy_ctx *ctx = dev_get_drvdata(dev);
1627
1628 if (args->args_count <= 0)
1629 return ERR_PTR(-EINVAL);
1630 if (args->args[0] < MODE_SATA || args->args[0] >= MODE_MAX)
1631 return ERR_PTR(-EINVAL);
1632
1633 ctx->mode = args->args[0];
1634 return ctx->phy;
1635}
1636
1637static void xgene_phy_get_param(struct platform_device *pdev,
1638 const char *name, u32 *buffer,
1639 int count, u32 *default_val,
1640 u32 conv_factor)
1641{
1642 int i;
1643
1644 if (!of_property_read_u32_array(pdev->dev.of_node, name, buffer,
1645 count)) {
1646 for (i = 0; i < count; i++)
1647 buffer[i] /= conv_factor;
1648 return;
1649 }
1650 /* Does not exist, load default */
1651 for (i = 0; i < count; i++)
1652 buffer[i] = default_val[i % 3];
1653}
1654
1655static int xgene_phy_probe(struct platform_device *pdev)
1656{
1657 struct phy_provider *phy_provider;
1658 struct xgene_phy_ctx *ctx;
1659 struct resource *res;
1660 u32 default_spd[] = DEFAULT_SATA_SPD_SEL;
1661 u32 default_txboost_gain[] = DEFAULT_SATA_TXBOOST_GAIN;
1662 u32 default_txeye_direction[] = DEFAULT_SATA_TXEYEDIRECTION;
1663 u32 default_txeye_tuning[] = DEFAULT_SATA_TXEYETUNING;
1664 u32 default_txamp[] = DEFAULT_SATA_TXAMP;
1665 u32 default_txcn1[] = DEFAULT_SATA_TXCN1;
1666 u32 default_txcn2[] = DEFAULT_SATA_TXCN2;
1667 u32 default_txcp1[] = DEFAULT_SATA_TXCP1;
1668 int i;
1669
1670 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
1671 if (!ctx)
1672 return -ENOMEM;
1673
1674 ctx->dev = &pdev->dev;
1675
1676 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1677 ctx->sds_base = devm_ioremap_resource(&pdev->dev, res);
1678 if (IS_ERR(ctx->sds_base))
1679 return PTR_ERR(ctx->sds_base);
1680
1681 /* Retrieve optional clock */
1682 ctx->clk = clk_get(&pdev->dev, NULL);
1683
1684 /* Load override paramaters */
1685 xgene_phy_get_param(pdev, "apm,tx-eye-tuning",
1686 ctx->sata_param.txeyetuning, 6, default_txeye_tuning, 1);
1687 xgene_phy_get_param(pdev, "apm,tx-eye-direction",
1688 ctx->sata_param.txeyedirection, 6, default_txeye_direction, 1);
1689 xgene_phy_get_param(pdev, "apm,tx-boost-gain",
1690 ctx->sata_param.txboostgain, 6, default_txboost_gain, 1);
1691 xgene_phy_get_param(pdev, "apm,tx-amplitude",
1692 ctx->sata_param.txamplitude, 6, default_txamp, 13300);
1693 xgene_phy_get_param(pdev, "apm,tx-pre-cursor1",
1694 ctx->sata_param.txprecursor_cn1, 6, default_txcn1, 18200);
1695 xgene_phy_get_param(pdev, "apm,tx-pre-cursor2",
1696 ctx->sata_param.txprecursor_cn2, 6, default_txcn2, 18200);
1697 xgene_phy_get_param(pdev, "apm,tx-post-cursor",
1698 ctx->sata_param.txpostcursor_cp1, 6, default_txcp1, 18200);
1699 xgene_phy_get_param(pdev, "apm,tx-speed",
1700 ctx->sata_param.txspeed, 3, default_spd, 1);
1701 for (i = 0; i < MAX_LANE; i++)
1702 ctx->sata_param.speed[i] = 2; /* Default to Gen3 */
1703
1704 platform_set_drvdata(pdev, ctx);
1705
1706 ctx->phy = devm_phy_create(ctx->dev, NULL, &xgene_phy_ops);
1707 if (IS_ERR(ctx->phy)) {
1708 dev_dbg(&pdev->dev, "Failed to create PHY\n");
1709 return PTR_ERR(ctx->phy);
1710 }
1711 phy_set_drvdata(ctx->phy, ctx);
1712
1713 phy_provider = devm_of_phy_provider_register(ctx->dev, xgene_phy_xlate);
1714 return PTR_ERR_OR_ZERO(phy_provider);
1715}
1716
1717static const struct of_device_id xgene_phy_of_match[] = {
1718 {.compatible = "apm,xgene-phy",},
1719 {},
1720};
1721MODULE_DEVICE_TABLE(of, xgene_phy_of_match);
1722
1723static struct platform_driver xgene_phy_driver = {
1724 .probe = xgene_phy_probe,
1725 .driver = {
1726 .name = "xgene-phy",
1727 .of_match_table = xgene_phy_of_match,
1728 },
1729};
1730module_platform_driver(xgene_phy_driver);
1731
1732MODULE_DESCRIPTION("APM X-Gene Multi-Purpose PHY driver");
1733MODULE_AUTHOR("Loc Ho <lho@apm.com>");
1734MODULE_LICENSE("GPL v2");
1735MODULE_VERSION("0.1");
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * AppliedMicro X-Gene Multi-purpose PHY driver
4 *
5 * Copyright (c) 2014, Applied Micro Circuits Corporation
6 * Author: Loc Ho <lho@apm.com>
7 * Tuan Phan <tphan@apm.com>
8 * Suman Tripathi <stripathi@apm.com>
9 *
10 * The APM X-Gene PHY consists of two PLL clock macro's (CMU) and lanes.
11 * The first PLL clock macro is used for internal reference clock. The second
12 * PLL clock macro is used to generate the clock for the PHY. This driver
13 * configures the first PLL CMU, the second PLL CMU, and programs the PHY to
14 * operate according to the mode of operation. The first PLL CMU is only
15 * required if internal clock is enabled.
16 *
17 * Logical Layer Out Of HW module units:
18 *
19 * -----------------
20 * | Internal | |------|
21 * | Ref PLL CMU |----| | ------------- ---------
22 * ------------ ---- | MUX |-----|PHY PLL CMU|----| Serdes|
23 * | | | | ---------
24 * External Clock ------| | -------------
25 * |------|
26 *
27 * The Ref PLL CMU CSR (Configuration System Registers) is accessed
28 * indirectly from the SDS offset at 0x2000. It is only required for
29 * internal reference clock.
30 * The PHY PLL CMU CSR is accessed indirectly from the SDS offset at 0x0000.
31 * The Serdes CSR is accessed indirectly from the SDS offset at 0x0400.
32 *
33 * The Ref PLL CMU can be located within the same PHY IP or outside the PHY IP
34 * due to shared Ref PLL CMU. For PHY with Ref PLL CMU shared with another IP,
35 * it is located outside the PHY IP. This is the case for the PHY located
36 * at 0x1f23a000 (SATA Port 4/5). For such PHY, another resource is required
37 * to located the SDS/Ref PLL CMU module and its clock for that IP enabled.
38 *
39 * Currently, this driver only supports Gen3 SATA mode with external clock.
40 */
41#include <linux/module.h>
42#include <linux/of.h>
43#include <linux/platform_device.h>
44#include <linux/io.h>
45#include <linux/delay.h>
46#include <linux/phy/phy.h>
47#include <linux/clk.h>
48
49/* Max 2 lanes per a PHY unit */
50#define MAX_LANE 2
51
52/* Register offset inside the PHY */
53#define SERDES_PLL_INDIRECT_OFFSET 0x0000
54#define SERDES_PLL_REF_INDIRECT_OFFSET 0x2000
55#define SERDES_INDIRECT_OFFSET 0x0400
56#define SERDES_LANE_STRIDE 0x0200
57
58/* Some default Serdes parameters */
59#define DEFAULT_SATA_TXBOOST_GAIN { 0x1e, 0x1e, 0x1e }
60#define DEFAULT_SATA_TXEYEDIRECTION { 0x0, 0x0, 0x0 }
61#define DEFAULT_SATA_TXEYETUNING { 0xa, 0xa, 0xa }
62#define DEFAULT_SATA_SPD_SEL { 0x1, 0x3, 0x7 }
63#define DEFAULT_SATA_TXAMP { 0x8, 0x8, 0x8 }
64#define DEFAULT_SATA_TXCN1 { 0x2, 0x2, 0x2 }
65#define DEFAULT_SATA_TXCN2 { 0x0, 0x0, 0x0 }
66#define DEFAULT_SATA_TXCP1 { 0xa, 0xa, 0xa }
67
68#define SATA_SPD_SEL_GEN3 0x7
69#define SATA_SPD_SEL_GEN2 0x3
70#define SATA_SPD_SEL_GEN1 0x1
71
72#define SSC_DISABLE 0
73#define SSC_ENABLE 1
74
75#define FBDIV_VAL_50M 0x77
76#define REFDIV_VAL_50M 0x1
77#define FBDIV_VAL_100M 0x3B
78#define REFDIV_VAL_100M 0x0
79
80/* SATA Clock/Reset CSR */
81#define SATACLKENREG 0x00000000
82#define SATA0_CORE_CLKEN 0x00000002
83#define SATA1_CORE_CLKEN 0x00000004
84#define SATASRESETREG 0x00000004
85#define SATA_MEM_RESET_MASK 0x00000020
86#define SATA_MEM_RESET_RD(src) (((src) & 0x00000020) >> 5)
87#define SATA_SDS_RESET_MASK 0x00000004
88#define SATA_CSR_RESET_MASK 0x00000001
89#define SATA_CORE_RESET_MASK 0x00000002
90#define SATA_PMCLK_RESET_MASK 0x00000010
91#define SATA_PCLK_RESET_MASK 0x00000008
92
93/* SDS CSR used for PHY Indirect access */
94#define SATA_ENET_SDS_PCS_CTL0 0x00000000
95#define REGSPEC_CFG_I_TX_WORDMODE0_SET(dst, src) \
96 (((dst) & ~0x00070000) | (((u32) (src) << 16) & 0x00070000))
97#define REGSPEC_CFG_I_RX_WORDMODE0_SET(dst, src) \
98 (((dst) & ~0x00e00000) | (((u32) (src) << 21) & 0x00e00000))
99#define SATA_ENET_SDS_CTL0 0x0000000c
100#define REGSPEC_CFG_I_CUSTOMER_PIN_MODE0_SET(dst, src) \
101 (((dst) & ~0x00007fff) | (((u32) (src)) & 0x00007fff))
102#define SATA_ENET_SDS_CTL1 0x00000010
103#define CFG_I_SPD_SEL_CDR_OVR1_SET(dst, src) \
104 (((dst) & ~0x0000000f) | (((u32) (src)) & 0x0000000f))
105#define SATA_ENET_SDS_RST_CTL 0x00000024
106#define SATA_ENET_SDS_IND_CMD_REG 0x0000003c
107#define CFG_IND_WR_CMD_MASK 0x00000001
108#define CFG_IND_RD_CMD_MASK 0x00000002
109#define CFG_IND_CMD_DONE_MASK 0x00000004
110#define CFG_IND_ADDR_SET(dst, src) \
111 (((dst) & ~0x003ffff0) | (((u32) (src) << 4) & 0x003ffff0))
112#define SATA_ENET_SDS_IND_RDATA_REG 0x00000040
113#define SATA_ENET_SDS_IND_WDATA_REG 0x00000044
114#define SATA_ENET_CLK_MACRO_REG 0x0000004c
115#define I_RESET_B_SET(dst, src) \
116 (((dst) & ~0x00000001) | (((u32) (src)) & 0x00000001))
117#define I_PLL_FBDIV_SET(dst, src) \
118 (((dst) & ~0x001ff000) | (((u32) (src) << 12) & 0x001ff000))
119#define I_CUSTOMEROV_SET(dst, src) \
120 (((dst) & ~0x00000f80) | (((u32) (src) << 7) & 0x00000f80))
121#define O_PLL_LOCK_RD(src) (((src) & 0x40000000) >> 30)
122#define O_PLL_READY_RD(src) (((src) & 0x80000000) >> 31)
123
124/* PLL Clock Macro Unit (CMU) CSR accessing from SDS indirectly */
125#define CMU_REG0 0x00000
126#define CMU_REG0_PLL_REF_SEL_MASK 0x00002000
127#define CMU_REG0_PLL_REF_SEL_SET(dst, src) \
128 (((dst) & ~0x00002000) | (((u32) (src) << 13) & 0x00002000))
129#define CMU_REG0_PDOWN_MASK 0x00004000
130#define CMU_REG0_CAL_COUNT_RESOL_SET(dst, src) \
131 (((dst) & ~0x000000e0) | (((u32) (src) << 5) & 0x000000e0))
132#define CMU_REG1 0x00002
133#define CMU_REG1_PLL_CP_SET(dst, src) \
134 (((dst) & ~0x00003c00) | (((u32) (src) << 10) & 0x00003c00))
135#define CMU_REG1_PLL_MANUALCAL_SET(dst, src) \
136 (((dst) & ~0x00000008) | (((u32) (src) << 3) & 0x00000008))
137#define CMU_REG1_PLL_CP_SEL_SET(dst, src) \
138 (((dst) & ~0x000003e0) | (((u32) (src) << 5) & 0x000003e0))
139#define CMU_REG1_REFCLK_CMOS_SEL_MASK 0x00000001
140#define CMU_REG1_REFCLK_CMOS_SEL_SET(dst, src) \
141 (((dst) & ~0x00000001) | (((u32) (src) << 0) & 0x00000001))
142#define CMU_REG2 0x00004
143#define CMU_REG2_PLL_REFDIV_SET(dst, src) \
144 (((dst) & ~0x0000c000) | (((u32) (src) << 14) & 0x0000c000))
145#define CMU_REG2_PLL_LFRES_SET(dst, src) \
146 (((dst) & ~0x0000001e) | (((u32) (src) << 1) & 0x0000001e))
147#define CMU_REG2_PLL_FBDIV_SET(dst, src) \
148 (((dst) & ~0x00003fe0) | (((u32) (src) << 5) & 0x00003fe0))
149#define CMU_REG3 0x00006
150#define CMU_REG3_VCOVARSEL_SET(dst, src) \
151 (((dst) & ~0x0000000f) | (((u32) (src) << 0) & 0x0000000f))
152#define CMU_REG3_VCO_MOMSEL_INIT_SET(dst, src) \
153 (((dst) & ~0x000003f0) | (((u32) (src) << 4) & 0x000003f0))
154#define CMU_REG3_VCO_MANMOMSEL_SET(dst, src) \
155 (((dst) & ~0x0000fc00) | (((u32) (src) << 10) & 0x0000fc00))
156#define CMU_REG4 0x00008
157#define CMU_REG5 0x0000a
158#define CMU_REG5_PLL_LFSMCAP_SET(dst, src) \
159 (((dst) & ~0x0000c000) | (((u32) (src) << 14) & 0x0000c000))
160#define CMU_REG5_PLL_LOCK_RESOLUTION_SET(dst, src) \
161 (((dst) & ~0x0000000e) | (((u32) (src) << 1) & 0x0000000e))
162#define CMU_REG5_PLL_LFCAP_SET(dst, src) \
163 (((dst) & ~0x00003000) | (((u32) (src) << 12) & 0x00003000))
164#define CMU_REG5_PLL_RESETB_MASK 0x00000001
165#define CMU_REG6 0x0000c
166#define CMU_REG6_PLL_VREGTRIM_SET(dst, src) \
167 (((dst) & ~0x00000600) | (((u32) (src) << 9) & 0x00000600))
168#define CMU_REG6_MAN_PVT_CAL_SET(dst, src) \
169 (((dst) & ~0x00000004) | (((u32) (src) << 2) & 0x00000004))
170#define CMU_REG7 0x0000e
171#define CMU_REG7_PLL_CALIB_DONE_RD(src) ((0x00004000 & (u32) (src)) >> 14)
172#define CMU_REG7_VCO_CAL_FAIL_RD(src) ((0x00000c00 & (u32) (src)) >> 10)
173#define CMU_REG8 0x00010
174#define CMU_REG9 0x00012
175#define CMU_REG9_WORD_LEN_8BIT 0x000
176#define CMU_REG9_WORD_LEN_10BIT 0x001
177#define CMU_REG9_WORD_LEN_16BIT 0x002
178#define CMU_REG9_WORD_LEN_20BIT 0x003
179#define CMU_REG9_WORD_LEN_32BIT 0x004
180#define CMU_REG9_WORD_LEN_40BIT 0x005
181#define CMU_REG9_WORD_LEN_64BIT 0x006
182#define CMU_REG9_WORD_LEN_66BIT 0x007
183#define CMU_REG9_TX_WORD_MODE_CH1_SET(dst, src) \
184 (((dst) & ~0x00000380) | (((u32) (src) << 7) & 0x00000380))
185#define CMU_REG9_TX_WORD_MODE_CH0_SET(dst, src) \
186 (((dst) & ~0x00000070) | (((u32) (src) << 4) & 0x00000070))
187#define CMU_REG9_PLL_POST_DIVBY2_SET(dst, src) \
188 (((dst) & ~0x00000008) | (((u32) (src) << 3) & 0x00000008))
189#define CMU_REG9_VBG_BYPASSB_SET(dst, src) \
190 (((dst) & ~0x00000004) | (((u32) (src) << 2) & 0x00000004))
191#define CMU_REG9_IGEN_BYPASS_SET(dst, src) \
192 (((dst) & ~0x00000002) | (((u32) (src) << 1) & 0x00000002))
193#define CMU_REG10 0x00014
194#define CMU_REG10_VREG_REFSEL_SET(dst, src) \
195 (((dst) & ~0x00000001) | (((u32) (src) << 0) & 0x00000001))
196#define CMU_REG11 0x00016
197#define CMU_REG12 0x00018
198#define CMU_REG12_STATE_DELAY9_SET(dst, src) \
199 (((dst) & ~0x000000f0) | (((u32) (src) << 4) & 0x000000f0))
200#define CMU_REG13 0x0001a
201#define CMU_REG14 0x0001c
202#define CMU_REG15 0x0001e
203#define CMU_REG16 0x00020
204#define CMU_REG16_PVT_DN_MAN_ENA_MASK 0x00000001
205#define CMU_REG16_PVT_UP_MAN_ENA_MASK 0x00000002
206#define CMU_REG16_VCOCAL_WAIT_BTW_CODE_SET(dst, src) \
207 (((dst) & ~0x0000001c) | (((u32) (src) << 2) & 0x0000001c))
208#define CMU_REG16_CALIBRATION_DONE_OVERRIDE_SET(dst, src) \
209 (((dst) & ~0x00000040) | (((u32) (src) << 6) & 0x00000040))
210#define CMU_REG16_BYPASS_PLL_LOCK_SET(dst, src) \
211 (((dst) & ~0x00000020) | (((u32) (src) << 5) & 0x00000020))
212#define CMU_REG17 0x00022
213#define CMU_REG17_PVT_CODE_R2A_SET(dst, src) \
214 (((dst) & ~0x00007f00) | (((u32) (src) << 8) & 0x00007f00))
215#define CMU_REG17_RESERVED_7_SET(dst, src) \
216 (((dst) & ~0x000000e0) | (((u32) (src) << 5) & 0x000000e0))
217#define CMU_REG17_PVT_TERM_MAN_ENA_MASK 0x00008000
218#define CMU_REG18 0x00024
219#define CMU_REG19 0x00026
220#define CMU_REG20 0x00028
221#define CMU_REG21 0x0002a
222#define CMU_REG22 0x0002c
223#define CMU_REG23 0x0002e
224#define CMU_REG24 0x00030
225#define CMU_REG25 0x00032
226#define CMU_REG26 0x00034
227#define CMU_REG26_FORCE_PLL_LOCK_SET(dst, src) \
228 (((dst) & ~0x00000001) | (((u32) (src) << 0) & 0x00000001))
229#define CMU_REG27 0x00036
230#define CMU_REG28 0x00038
231#define CMU_REG29 0x0003a
232#define CMU_REG30 0x0003c
233#define CMU_REG30_LOCK_COUNT_SET(dst, src) \
234 (((dst) & ~0x00000006) | (((u32) (src) << 1) & 0x00000006))
235#define CMU_REG30_PCIE_MODE_SET(dst, src) \
236 (((dst) & ~0x00000008) | (((u32) (src) << 3) & 0x00000008))
237#define CMU_REG31 0x0003e
238#define CMU_REG32 0x00040
239#define CMU_REG32_FORCE_VCOCAL_START_MASK 0x00004000
240#define CMU_REG32_PVT_CAL_WAIT_SEL_SET(dst, src) \
241 (((dst) & ~0x00000006) | (((u32) (src) << 1) & 0x00000006))
242#define CMU_REG32_IREF_ADJ_SET(dst, src) \
243 (((dst) & ~0x00000180) | (((u32) (src) << 7) & 0x00000180))
244#define CMU_REG33 0x00042
245#define CMU_REG34 0x00044
246#define CMU_REG34_VCO_CAL_VTH_LO_MAX_SET(dst, src) \
247 (((dst) & ~0x0000000f) | (((u32) (src) << 0) & 0x0000000f))
248#define CMU_REG34_VCO_CAL_VTH_HI_MAX_SET(dst, src) \
249 (((dst) & ~0x00000f00) | (((u32) (src) << 8) & 0x00000f00))
250#define CMU_REG34_VCO_CAL_VTH_LO_MIN_SET(dst, src) \
251 (((dst) & ~0x000000f0) | (((u32) (src) << 4) & 0x000000f0))
252#define CMU_REG34_VCO_CAL_VTH_HI_MIN_SET(dst, src) \
253 (((dst) & ~0x0000f000) | (((u32) (src) << 12) & 0x0000f000))
254#define CMU_REG35 0x00046
255#define CMU_REG35_PLL_SSC_MOD_SET(dst, src) \
256 (((dst) & ~0x0000fe00) | (((u32) (src) << 9) & 0x0000fe00))
257#define CMU_REG36 0x00048
258#define CMU_REG36_PLL_SSC_EN_SET(dst, src) \
259 (((dst) & ~0x00000010) | (((u32) (src) << 4) & 0x00000010))
260#define CMU_REG36_PLL_SSC_VSTEP_SET(dst, src) \
261 (((dst) & ~0x0000ffc0) | (((u32) (src) << 6) & 0x0000ffc0))
262#define CMU_REG36_PLL_SSC_DSMSEL_SET(dst, src) \
263 (((dst) & ~0x00000020) | (((u32) (src) << 5) & 0x00000020))
264#define CMU_REG37 0x0004a
265#define CMU_REG38 0x0004c
266#define CMU_REG39 0x0004e
267
268/* PHY lane CSR accessing from SDS indirectly */
269#define RXTX_REG0 0x000
270#define RXTX_REG0_CTLE_EQ_HR_SET(dst, src) \
271 (((dst) & ~0x0000f800) | (((u32) (src) << 11) & 0x0000f800))
272#define RXTX_REG0_CTLE_EQ_QR_SET(dst, src) \
273 (((dst) & ~0x000007c0) | (((u32) (src) << 6) & 0x000007c0))
274#define RXTX_REG0_CTLE_EQ_FR_SET(dst, src) \
275 (((dst) & ~0x0000003e) | (((u32) (src) << 1) & 0x0000003e))
276#define RXTX_REG1 0x002
277#define RXTX_REG1_RXACVCM_SET(dst, src) \
278 (((dst) & ~0x0000f000) | (((u32) (src) << 12) & 0x0000f000))
279#define RXTX_REG1_CTLE_EQ_SET(dst, src) \
280 (((dst) & ~0x00000f80) | (((u32) (src) << 7) & 0x00000f80))
281#define RXTX_REG1_RXVREG1_SET(dst, src) \
282 (((dst) & ~0x00000060) | (((u32) (src) << 5) & 0x00000060))
283#define RXTX_REG1_RXIREF_ADJ_SET(dst, src) \
284 (((dst) & ~0x00000006) | (((u32) (src) << 1) & 0x00000006))
285#define RXTX_REG2 0x004
286#define RXTX_REG2_VTT_ENA_SET(dst, src) \
287 (((dst) & ~0x00000100) | (((u32) (src) << 8) & 0x00000100))
288#define RXTX_REG2_TX_FIFO_ENA_SET(dst, src) \
289 (((dst) & ~0x00000020) | (((u32) (src) << 5) & 0x00000020))
290#define RXTX_REG2_VTT_SEL_SET(dst, src) \
291 (((dst) & ~0x000000c0) | (((u32) (src) << 6) & 0x000000c0))
292#define RXTX_REG4 0x008
293#define RXTX_REG4_TX_LOOPBACK_BUF_EN_MASK 0x00000040
294#define RXTX_REG4_TX_DATA_RATE_SET(dst, src) \
295 (((dst) & ~0x0000c000) | (((u32) (src) << 14) & 0x0000c000))
296#define RXTX_REG4_TX_WORD_MODE_SET(dst, src) \
297 (((dst) & ~0x00003800) | (((u32) (src) << 11) & 0x00003800))
298#define RXTX_REG5 0x00a
299#define RXTX_REG5_TX_CN1_SET(dst, src) \
300 (((dst) & ~0x0000f800) | (((u32) (src) << 11) & 0x0000f800))
301#define RXTX_REG5_TX_CP1_SET(dst, src) \
302 (((dst) & ~0x000007e0) | (((u32) (src) << 5) & 0x000007e0))
303#define RXTX_REG5_TX_CN2_SET(dst, src) \
304 (((dst) & ~0x0000001f) | (((u32) (src) << 0) & 0x0000001f))
305#define RXTX_REG6 0x00c
306#define RXTX_REG6_TXAMP_CNTL_SET(dst, src) \
307 (((dst) & ~0x00000780) | (((u32) (src) << 7) & 0x00000780))
308#define RXTX_REG6_TXAMP_ENA_SET(dst, src) \
309 (((dst) & ~0x00000040) | (((u32) (src) << 6) & 0x00000040))
310#define RXTX_REG6_RX_BIST_ERRCNT_RD_SET(dst, src) \
311 (((dst) & ~0x00000001) | (((u32) (src) << 0) & 0x00000001))
312#define RXTX_REG6_TX_IDLE_SET(dst, src) \
313 (((dst) & ~0x00000008) | (((u32) (src) << 3) & 0x00000008))
314#define RXTX_REG6_RX_BIST_RESYNC_SET(dst, src) \
315 (((dst) & ~0x00000002) | (((u32) (src) << 1) & 0x00000002))
316#define RXTX_REG7 0x00e
317#define RXTX_REG7_RESETB_RXD_MASK 0x00000100
318#define RXTX_REG7_RESETB_RXA_MASK 0x00000080
319#define RXTX_REG7_BIST_ENA_RX_SET(dst, src) \
320 (((dst) & ~0x00000040) | (((u32) (src) << 6) & 0x00000040))
321#define RXTX_REG7_RX_WORD_MODE_SET(dst, src) \
322 (((dst) & ~0x00003800) | (((u32) (src) << 11) & 0x00003800))
323#define RXTX_REG8 0x010
324#define RXTX_REG8_CDR_LOOP_ENA_SET(dst, src) \
325 (((dst) & ~0x00004000) | (((u32) (src) << 14) & 0x00004000))
326#define RXTX_REG8_CDR_BYPASS_RXLOS_SET(dst, src) \
327 (((dst) & ~0x00000800) | (((u32) (src) << 11) & 0x00000800))
328#define RXTX_REG8_SSC_ENABLE_SET(dst, src) \
329 (((dst) & ~0x00000200) | (((u32) (src) << 9) & 0x00000200))
330#define RXTX_REG8_SD_VREF_SET(dst, src) \
331 (((dst) & ~0x000000f0) | (((u32) (src) << 4) & 0x000000f0))
332#define RXTX_REG8_SD_DISABLE_SET(dst, src) \
333 (((dst) & ~0x00000100) | (((u32) (src) << 8) & 0x00000100))
334#define RXTX_REG7 0x00e
335#define RXTX_REG7_RESETB_RXD_SET(dst, src) \
336 (((dst) & ~0x00000100) | (((u32) (src) << 8) & 0x00000100))
337#define RXTX_REG7_RESETB_RXA_SET(dst, src) \
338 (((dst) & ~0x00000080) | (((u32) (src) << 7) & 0x00000080))
339#define RXTX_REG7_LOOP_BACK_ENA_CTLE_MASK 0x00004000
340#define RXTX_REG7_LOOP_BACK_ENA_CTLE_SET(dst, src) \
341 (((dst) & ~0x00004000) | (((u32) (src) << 14) & 0x00004000))
342#define RXTX_REG11 0x016
343#define RXTX_REG11_PHASE_ADJUST_LIMIT_SET(dst, src) \
344 (((dst) & ~0x0000f800) | (((u32) (src) << 11) & 0x0000f800))
345#define RXTX_REG12 0x018
346#define RXTX_REG12_LATCH_OFF_ENA_SET(dst, src) \
347 (((dst) & ~0x00002000) | (((u32) (src) << 13) & 0x00002000))
348#define RXTX_REG12_SUMOS_ENABLE_SET(dst, src) \
349 (((dst) & ~0x00000004) | (((u32) (src) << 2) & 0x00000004))
350#define RXTX_REG12_RX_DET_TERM_ENABLE_MASK 0x00000002
351#define RXTX_REG12_RX_DET_TERM_ENABLE_SET(dst, src) \
352 (((dst) & ~0x00000002) | (((u32) (src) << 1) & 0x00000002))
353#define RXTX_REG13 0x01a
354#define RXTX_REG14 0x01c
355#define RXTX_REG14_CLTE_LATCAL_MAN_PROG_SET(dst, src) \
356 (((dst) & ~0x0000003f) | (((u32) (src) << 0) & 0x0000003f))
357#define RXTX_REG14_CTLE_LATCAL_MAN_ENA_SET(dst, src) \
358 (((dst) & ~0x00000040) | (((u32) (src) << 6) & 0x00000040))
359#define RXTX_REG26 0x034
360#define RXTX_REG26_PERIOD_ERROR_LATCH_SET(dst, src) \
361 (((dst) & ~0x00003800) | (((u32) (src) << 11) & 0x00003800))
362#define RXTX_REG26_BLWC_ENA_SET(dst, src) \
363 (((dst) & ~0x00000008) | (((u32) (src) << 3) & 0x00000008))
364#define RXTX_REG21 0x02a
365#define RXTX_REG21_DO_LATCH_CALOUT_RD(src) ((0x0000fc00 & (u32) (src)) >> 10)
366#define RXTX_REG21_XO_LATCH_CALOUT_RD(src) ((0x000003f0 & (u32) (src)) >> 4)
367#define RXTX_REG21_LATCH_CAL_FAIL_ODD_RD(src) ((0x0000000f & (u32)(src)))
368#define RXTX_REG22 0x02c
369#define RXTX_REG22_SO_LATCH_CALOUT_RD(src) ((0x000003f0 & (u32) (src)) >> 4)
370#define RXTX_REG22_EO_LATCH_CALOUT_RD(src) ((0x0000fc00 & (u32) (src)) >> 10)
371#define RXTX_REG22_LATCH_CAL_FAIL_EVEN_RD(src) ((0x0000000f & (u32)(src)))
372#define RXTX_REG23 0x02e
373#define RXTX_REG23_DE_LATCH_CALOUT_RD(src) ((0x0000fc00 & (u32) (src)) >> 10)
374#define RXTX_REG23_XE_LATCH_CALOUT_RD(src) ((0x000003f0 & (u32) (src)) >> 4)
375#define RXTX_REG24 0x030
376#define RXTX_REG24_EE_LATCH_CALOUT_RD(src) ((0x0000fc00 & (u32) (src)) >> 10)
377#define RXTX_REG24_SE_LATCH_CALOUT_RD(src) ((0x000003f0 & (u32) (src)) >> 4)
378#define RXTX_REG27 0x036
379#define RXTX_REG28 0x038
380#define RXTX_REG31 0x03e
381#define RXTX_REG38 0x04c
382#define RXTX_REG38_CUSTOMER_PINMODE_INV_SET(dst, src) \
383 (((dst) & 0x0000fffe) | (((u32) (src) << 1) & 0x0000fffe))
384#define RXTX_REG39 0x04e
385#define RXTX_REG40 0x050
386#define RXTX_REG41 0x052
387#define RXTX_REG42 0x054
388#define RXTX_REG43 0x056
389#define RXTX_REG44 0x058
390#define RXTX_REG45 0x05a
391#define RXTX_REG46 0x05c
392#define RXTX_REG47 0x05e
393#define RXTX_REG48 0x060
394#define RXTX_REG49 0x062
395#define RXTX_REG50 0x064
396#define RXTX_REG51 0x066
397#define RXTX_REG52 0x068
398#define RXTX_REG53 0x06a
399#define RXTX_REG54 0x06c
400#define RXTX_REG55 0x06e
401#define RXTX_REG61 0x07a
402#define RXTX_REG61_ISCAN_INBERT_SET(dst, src) \
403 (((dst) & ~0x00000010) | (((u32) (src) << 4) & 0x00000010))
404#define RXTX_REG61_LOADFREQ_SHIFT_SET(dst, src) \
405 (((dst) & ~0x00000008) | (((u32) (src) << 3) & 0x00000008))
406#define RXTX_REG61_EYE_COUNT_WIDTH_SEL_SET(dst, src) \
407 (((dst) & ~0x000000c0) | (((u32) (src) << 6) & 0x000000c0))
408#define RXTX_REG61_SPD_SEL_CDR_SET(dst, src) \
409 (((dst) & ~0x00003c00) | (((u32) (src) << 10) & 0x00003c00))
410#define RXTX_REG62 0x07c
411#define RXTX_REG62_PERIOD_H1_QLATCH_SET(dst, src) \
412 (((dst) & ~0x00003800) | (((u32) (src) << 11) & 0x00003800))
413#define RXTX_REG81 0x0a2
414#define RXTX_REG89_MU_TH7_SET(dst, src) \
415 (((dst) & ~0x0000f800) | (((u32) (src) << 11) & 0x0000f800))
416#define RXTX_REG89_MU_TH8_SET(dst, src) \
417 (((dst) & ~0x000007c0) | (((u32) (src) << 6) & 0x000007c0))
418#define RXTX_REG89_MU_TH9_SET(dst, src) \
419 (((dst) & ~0x0000003e) | (((u32) (src) << 1) & 0x0000003e))
420#define RXTX_REG96 0x0c0
421#define RXTX_REG96_MU_FREQ1_SET(dst, src) \
422 (((dst) & ~0x0000f800) | (((u32) (src) << 11) & 0x0000f800))
423#define RXTX_REG96_MU_FREQ2_SET(dst, src) \
424 (((dst) & ~0x000007c0) | (((u32) (src) << 6) & 0x000007c0))
425#define RXTX_REG96_MU_FREQ3_SET(dst, src) \
426 (((dst) & ~0x0000003e) | (((u32) (src) << 1) & 0x0000003e))
427#define RXTX_REG99 0x0c6
428#define RXTX_REG99_MU_PHASE1_SET(dst, src) \
429 (((dst) & ~0x0000f800) | (((u32) (src) << 11) & 0x0000f800))
430#define RXTX_REG99_MU_PHASE2_SET(dst, src) \
431 (((dst) & ~0x000007c0) | (((u32) (src) << 6) & 0x000007c0))
432#define RXTX_REG99_MU_PHASE3_SET(dst, src) \
433 (((dst) & ~0x0000003e) | (((u32) (src) << 1) & 0x0000003e))
434#define RXTX_REG102 0x0cc
435#define RXTX_REG102_FREQLOOP_LIMIT_SET(dst, src) \
436 (((dst) & ~0x00000060) | (((u32) (src) << 5) & 0x00000060))
437#define RXTX_REG114 0x0e4
438#define RXTX_REG121 0x0f2
439#define RXTX_REG121_SUMOS_CAL_CODE_RD(src) ((0x0000003e & (u32)(src)) >> 0x1)
440#define RXTX_REG125 0x0fa
441#define RXTX_REG125_PQ_REG_SET(dst, src) \
442 (((dst) & ~0x0000fe00) | (((u32) (src) << 9) & 0x0000fe00))
443#define RXTX_REG125_SIGN_PQ_SET(dst, src) \
444 (((dst) & ~0x00000100) | (((u32) (src) << 8) & 0x00000100))
445#define RXTX_REG125_SIGN_PQ_2C_SET(dst, src) \
446 (((dst) & ~0x00000080) | (((u32) (src) << 7) & 0x00000080))
447#define RXTX_REG125_PHZ_MANUALCODE_SET(dst, src) \
448 (((dst) & ~0x0000007c) | (((u32) (src) << 2) & 0x0000007c))
449#define RXTX_REG125_PHZ_MANUAL_SET(dst, src) \
450 (((dst) & ~0x00000002) | (((u32) (src) << 1) & 0x00000002))
451#define RXTX_REG127 0x0fe
452#define RXTX_REG127_FORCE_SUM_CAL_START_MASK 0x00000002
453#define RXTX_REG127_FORCE_LAT_CAL_START_MASK 0x00000004
454#define RXTX_REG127_FORCE_SUM_CAL_START_SET(dst, src) \
455 (((dst) & ~0x00000002) | (((u32) (src) << 1) & 0x00000002))
456#define RXTX_REG127_FORCE_LAT_CAL_START_SET(dst, src) \
457 (((dst) & ~0x00000004) | (((u32) (src) << 2) & 0x00000004))
458#define RXTX_REG127_LATCH_MAN_CAL_ENA_SET(dst, src) \
459 (((dst) & ~0x00000008) | (((u32) (src) << 3) & 0x00000008))
460#define RXTX_REG127_DO_LATCH_MANCAL_SET(dst, src) \
461 (((dst) & ~0x0000fc00) | (((u32) (src) << 10) & 0x0000fc00))
462#define RXTX_REG127_XO_LATCH_MANCAL_SET(dst, src) \
463 (((dst) & ~0x000003f0) | (((u32) (src) << 4) & 0x000003f0))
464#define RXTX_REG128 0x100
465#define RXTX_REG128_LATCH_CAL_WAIT_SEL_SET(dst, src) \
466 (((dst) & ~0x0000000c) | (((u32) (src) << 2) & 0x0000000c))
467#define RXTX_REG128_EO_LATCH_MANCAL_SET(dst, src) \
468 (((dst) & ~0x0000fc00) | (((u32) (src) << 10) & 0x0000fc00))
469#define RXTX_REG128_SO_LATCH_MANCAL_SET(dst, src) \
470 (((dst) & ~0x000003f0) | (((u32) (src) << 4) & 0x000003f0))
471#define RXTX_REG129 0x102
472#define RXTX_REG129_DE_LATCH_MANCAL_SET(dst, src) \
473 (((dst) & ~0x0000fc00) | (((u32) (src) << 10) & 0x0000fc00))
474#define RXTX_REG129_XE_LATCH_MANCAL_SET(dst, src) \
475 (((dst) & ~0x000003f0) | (((u32) (src) << 4) & 0x000003f0))
476#define RXTX_REG130 0x104
477#define RXTX_REG130_EE_LATCH_MANCAL_SET(dst, src) \
478 (((dst) & ~0x0000fc00) | (((u32) (src) << 10) & 0x0000fc00))
479#define RXTX_REG130_SE_LATCH_MANCAL_SET(dst, src) \
480 (((dst) & ~0x000003f0) | (((u32) (src) << 4) & 0x000003f0))
481#define RXTX_REG145 0x122
482#define RXTX_REG145_TX_IDLE_SATA_SET(dst, src) \
483 (((dst) & ~0x00000001) | (((u32) (src) << 0) & 0x00000001))
484#define RXTX_REG145_RXES_ENA_SET(dst, src) \
485 (((dst) & ~0x00000002) | (((u32) (src) << 1) & 0x00000002))
486#define RXTX_REG145_RXDFE_CONFIG_SET(dst, src) \
487 (((dst) & ~0x0000c000) | (((u32) (src) << 14) & 0x0000c000))
488#define RXTX_REG145_RXVWES_LATENA_SET(dst, src) \
489 (((dst) & ~0x00000004) | (((u32) (src) << 2) & 0x00000004))
490#define RXTX_REG147 0x126
491#define RXTX_REG148 0x128
492
493/* Clock macro type */
494enum cmu_type_t {
495 REF_CMU = 0, /* Clock macro is the internal reference clock */
496 PHY_CMU = 1, /* Clock macro is the PLL for the Serdes */
497};
498
499enum mux_type_t {
500 MUX_SELECT_ATA = 0, /* Switch the MUX to ATA */
501 MUX_SELECT_SGMMII = 0, /* Switch the MUX to SGMII */
502};
503
504enum clk_type_t {
505 CLK_EXT_DIFF = 0, /* External differential */
506 CLK_INT_DIFF = 1, /* Internal differential */
507 CLK_INT_SING = 2, /* Internal single ended */
508};
509
510enum xgene_phy_mode {
511 MODE_SATA = 0, /* List them for simple reference */
512 MODE_SGMII = 1,
513 MODE_PCIE = 2,
514 MODE_USB = 3,
515 MODE_XFI = 4,
516 MODE_MAX
517};
518
519struct xgene_sata_override_param {
520 u32 speed[MAX_LANE]; /* Index for override parameter per lane */
521 u32 txspeed[3]; /* Tx speed */
522 u32 txboostgain[MAX_LANE*3]; /* Tx freq boost and gain control */
523 u32 txeyetuning[MAX_LANE*3]; /* Tx eye tuning */
524 u32 txeyedirection[MAX_LANE*3]; /* Tx eye tuning direction */
525 u32 txamplitude[MAX_LANE*3]; /* Tx amplitude control */
526 u32 txprecursor_cn1[MAX_LANE*3]; /* Tx emphasis taps 1st pre-cursor */
527 u32 txprecursor_cn2[MAX_LANE*3]; /* Tx emphasis taps 2nd pre-cursor */
528 u32 txpostcursor_cp1[MAX_LANE*3]; /* Tx emphasis taps post-cursor */
529};
530
531struct xgene_phy_ctx {
532 struct device *dev;
533 struct phy *phy;
534 enum xgene_phy_mode mode; /* Mode of operation */
535 enum clk_type_t clk_type; /* Input clock selection */
536 void __iomem *sds_base; /* PHY CSR base addr */
537 struct clk *clk; /* Optional clock */
538
539 /* Override Serdes parameters */
540 struct xgene_sata_override_param sata_param;
541};
542
543/*
544 * For chip earlier than A3 version, enable this flag.
545 * To enable, pass boot argument phy_xgene.preA3Chip=1
546 */
547static int preA3Chip;
548MODULE_PARM_DESC(preA3Chip, "Enable pre-A3 chip support (1=enable 0=disable)");
549module_param_named(preA3Chip, preA3Chip, int, 0444);
550
551static void sds_wr(void __iomem *csr_base, u32 indirect_cmd_reg,
552 u32 indirect_data_reg, u32 addr, u32 data)
553{
554 unsigned long deadline = jiffies + HZ;
555 u32 val;
556 u32 cmd;
557
558 cmd = CFG_IND_WR_CMD_MASK | CFG_IND_CMD_DONE_MASK;
559 cmd = CFG_IND_ADDR_SET(cmd, addr);
560 writel(data, csr_base + indirect_data_reg);
561 readl(csr_base + indirect_data_reg); /* Force a barrier */
562 writel(cmd, csr_base + indirect_cmd_reg);
563 readl(csr_base + indirect_cmd_reg); /* Force a barrier */
564 do {
565 val = readl(csr_base + indirect_cmd_reg);
566 } while (!(val & CFG_IND_CMD_DONE_MASK) &&
567 time_before(jiffies, deadline));
568 if (!(val & CFG_IND_CMD_DONE_MASK))
569 pr_err("SDS WR timeout at 0x%p offset 0x%08X value 0x%08X\n",
570 csr_base + indirect_cmd_reg, addr, data);
571}
572
573static void sds_rd(void __iomem *csr_base, u32 indirect_cmd_reg,
574 u32 indirect_data_reg, u32 addr, u32 *data)
575{
576 unsigned long deadline = jiffies + HZ;
577 u32 val;
578 u32 cmd;
579
580 cmd = CFG_IND_RD_CMD_MASK | CFG_IND_CMD_DONE_MASK;
581 cmd = CFG_IND_ADDR_SET(cmd, addr);
582 writel(cmd, csr_base + indirect_cmd_reg);
583 readl(csr_base + indirect_cmd_reg); /* Force a barrier */
584 do {
585 val = readl(csr_base + indirect_cmd_reg);
586 } while (!(val & CFG_IND_CMD_DONE_MASK) &&
587 time_before(jiffies, deadline));
588 *data = readl(csr_base + indirect_data_reg);
589 if (!(val & CFG_IND_CMD_DONE_MASK))
590 pr_err("SDS WR timeout at 0x%p offset 0x%08X value 0x%08X\n",
591 csr_base + indirect_cmd_reg, addr, *data);
592}
593
594static void cmu_wr(struct xgene_phy_ctx *ctx, enum cmu_type_t cmu_type,
595 u32 reg, u32 data)
596{
597 void __iomem *sds_base = ctx->sds_base;
598 u32 val;
599
600 if (cmu_type == REF_CMU)
601 reg += SERDES_PLL_REF_INDIRECT_OFFSET;
602 else
603 reg += SERDES_PLL_INDIRECT_OFFSET;
604 sds_wr(sds_base, SATA_ENET_SDS_IND_CMD_REG,
605 SATA_ENET_SDS_IND_WDATA_REG, reg, data);
606 sds_rd(sds_base, SATA_ENET_SDS_IND_CMD_REG,
607 SATA_ENET_SDS_IND_RDATA_REG, reg, &val);
608 pr_debug("CMU WR addr 0x%X value 0x%08X <-> 0x%08X\n", reg, data, val);
609}
610
611static void cmu_rd(struct xgene_phy_ctx *ctx, enum cmu_type_t cmu_type,
612 u32 reg, u32 *data)
613{
614 void __iomem *sds_base = ctx->sds_base;
615
616 if (cmu_type == REF_CMU)
617 reg += SERDES_PLL_REF_INDIRECT_OFFSET;
618 else
619 reg += SERDES_PLL_INDIRECT_OFFSET;
620 sds_rd(sds_base, SATA_ENET_SDS_IND_CMD_REG,
621 SATA_ENET_SDS_IND_RDATA_REG, reg, data);
622 pr_debug("CMU RD addr 0x%X value 0x%08X\n", reg, *data);
623}
624
625static void cmu_toggle1to0(struct xgene_phy_ctx *ctx, enum cmu_type_t cmu_type,
626 u32 reg, u32 bits)
627{
628 u32 val;
629
630 cmu_rd(ctx, cmu_type, reg, &val);
631 val |= bits;
632 cmu_wr(ctx, cmu_type, reg, val);
633 cmu_rd(ctx, cmu_type, reg, &val);
634 val &= ~bits;
635 cmu_wr(ctx, cmu_type, reg, val);
636}
637
638static void cmu_clrbits(struct xgene_phy_ctx *ctx, enum cmu_type_t cmu_type,
639 u32 reg, u32 bits)
640{
641 u32 val;
642
643 cmu_rd(ctx, cmu_type, reg, &val);
644 val &= ~bits;
645 cmu_wr(ctx, cmu_type, reg, val);
646}
647
648static void cmu_setbits(struct xgene_phy_ctx *ctx, enum cmu_type_t cmu_type,
649 u32 reg, u32 bits)
650{
651 u32 val;
652
653 cmu_rd(ctx, cmu_type, reg, &val);
654 val |= bits;
655 cmu_wr(ctx, cmu_type, reg, val);
656}
657
658static void serdes_wr(struct xgene_phy_ctx *ctx, int lane, u32 reg, u32 data)
659{
660 void __iomem *sds_base = ctx->sds_base;
661 u32 val;
662
663 reg += SERDES_INDIRECT_OFFSET;
664 reg += lane * SERDES_LANE_STRIDE;
665 sds_wr(sds_base, SATA_ENET_SDS_IND_CMD_REG,
666 SATA_ENET_SDS_IND_WDATA_REG, reg, data);
667 sds_rd(sds_base, SATA_ENET_SDS_IND_CMD_REG,
668 SATA_ENET_SDS_IND_RDATA_REG, reg, &val);
669 pr_debug("SERDES WR addr 0x%X value 0x%08X <-> 0x%08X\n", reg, data,
670 val);
671}
672
673static void serdes_rd(struct xgene_phy_ctx *ctx, int lane, u32 reg, u32 *data)
674{
675 void __iomem *sds_base = ctx->sds_base;
676
677 reg += SERDES_INDIRECT_OFFSET;
678 reg += lane * SERDES_LANE_STRIDE;
679 sds_rd(sds_base, SATA_ENET_SDS_IND_CMD_REG,
680 SATA_ENET_SDS_IND_RDATA_REG, reg, data);
681 pr_debug("SERDES RD addr 0x%X value 0x%08X\n", reg, *data);
682}
683
684static void serdes_clrbits(struct xgene_phy_ctx *ctx, int lane, u32 reg,
685 u32 bits)
686{
687 u32 val;
688
689 serdes_rd(ctx, lane, reg, &val);
690 val &= ~bits;
691 serdes_wr(ctx, lane, reg, val);
692}
693
694static void serdes_setbits(struct xgene_phy_ctx *ctx, int lane, u32 reg,
695 u32 bits)
696{
697 u32 val;
698
699 serdes_rd(ctx, lane, reg, &val);
700 val |= bits;
701 serdes_wr(ctx, lane, reg, val);
702}
703
704static void xgene_phy_cfg_cmu_clk_type(struct xgene_phy_ctx *ctx,
705 enum cmu_type_t cmu_type,
706 enum clk_type_t clk_type)
707{
708 u32 val;
709
710 /* Set the reset sequence delay for TX ready assertion */
711 cmu_rd(ctx, cmu_type, CMU_REG12, &val);
712 val = CMU_REG12_STATE_DELAY9_SET(val, 0x1);
713 cmu_wr(ctx, cmu_type, CMU_REG12, val);
714 /* Set the programmable stage delays between various enable stages */
715 cmu_wr(ctx, cmu_type, CMU_REG13, 0x0222);
716 cmu_wr(ctx, cmu_type, CMU_REG14, 0x2225);
717
718 /* Configure clock type */
719 if (clk_type == CLK_EXT_DIFF) {
720 /* Select external clock mux */
721 cmu_rd(ctx, cmu_type, CMU_REG0, &val);
722 val = CMU_REG0_PLL_REF_SEL_SET(val, 0x0);
723 cmu_wr(ctx, cmu_type, CMU_REG0, val);
724 /* Select CMOS as reference clock */
725 cmu_rd(ctx, cmu_type, CMU_REG1, &val);
726 val = CMU_REG1_REFCLK_CMOS_SEL_SET(val, 0x0);
727 cmu_wr(ctx, cmu_type, CMU_REG1, val);
728 dev_dbg(ctx->dev, "Set external reference clock\n");
729 } else if (clk_type == CLK_INT_DIFF) {
730 /* Select internal clock mux */
731 cmu_rd(ctx, cmu_type, CMU_REG0, &val);
732 val = CMU_REG0_PLL_REF_SEL_SET(val, 0x1);
733 cmu_wr(ctx, cmu_type, CMU_REG0, val);
734 /* Select CMOS as reference clock */
735 cmu_rd(ctx, cmu_type, CMU_REG1, &val);
736 val = CMU_REG1_REFCLK_CMOS_SEL_SET(val, 0x1);
737 cmu_wr(ctx, cmu_type, CMU_REG1, val);
738 dev_dbg(ctx->dev, "Set internal reference clock\n");
739 } else if (clk_type == CLK_INT_SING) {
740 /*
741 * NOTE: This clock type is NOT support for controller
742 * whose internal clock shared in the PCIe controller
743 *
744 * Select internal clock mux
745 */
746 cmu_rd(ctx, cmu_type, CMU_REG1, &val);
747 val = CMU_REG1_REFCLK_CMOS_SEL_SET(val, 0x1);
748 cmu_wr(ctx, cmu_type, CMU_REG1, val);
749 /* Select CML as reference clock */
750 cmu_rd(ctx, cmu_type, CMU_REG1, &val);
751 val = CMU_REG1_REFCLK_CMOS_SEL_SET(val, 0x0);
752 cmu_wr(ctx, cmu_type, CMU_REG1, val);
753 dev_dbg(ctx->dev,
754 "Set internal single ended reference clock\n");
755 }
756}
757
758static void xgene_phy_sata_cfg_cmu_core(struct xgene_phy_ctx *ctx,
759 enum cmu_type_t cmu_type,
760 enum clk_type_t clk_type)
761{
762 u32 val;
763 int ref_100MHz;
764
765 if (cmu_type == REF_CMU) {
766 /* Set VCO calibration voltage threshold */
767 cmu_rd(ctx, cmu_type, CMU_REG34, &val);
768 val = CMU_REG34_VCO_CAL_VTH_LO_MAX_SET(val, 0x7);
769 val = CMU_REG34_VCO_CAL_VTH_HI_MAX_SET(val, 0xc);
770 val = CMU_REG34_VCO_CAL_VTH_LO_MIN_SET(val, 0x3);
771 val = CMU_REG34_VCO_CAL_VTH_HI_MIN_SET(val, 0x8);
772 cmu_wr(ctx, cmu_type, CMU_REG34, val);
773 }
774
775 /* Set the VCO calibration counter */
776 cmu_rd(ctx, cmu_type, CMU_REG0, &val);
777 if (cmu_type == REF_CMU || preA3Chip)
778 val = CMU_REG0_CAL_COUNT_RESOL_SET(val, 0x4);
779 else
780 val = CMU_REG0_CAL_COUNT_RESOL_SET(val, 0x7);
781 cmu_wr(ctx, cmu_type, CMU_REG0, val);
782
783 /* Configure PLL for calibration */
784 cmu_rd(ctx, cmu_type, CMU_REG1, &val);
785 val = CMU_REG1_PLL_CP_SET(val, 0x1);
786 if (cmu_type == REF_CMU || preA3Chip)
787 val = CMU_REG1_PLL_CP_SEL_SET(val, 0x5);
788 else
789 val = CMU_REG1_PLL_CP_SEL_SET(val, 0x3);
790 if (cmu_type == REF_CMU)
791 val = CMU_REG1_PLL_MANUALCAL_SET(val, 0x0);
792 else
793 val = CMU_REG1_PLL_MANUALCAL_SET(val, 0x1);
794 cmu_wr(ctx, cmu_type, CMU_REG1, val);
795
796 if (cmu_type != REF_CMU)
797 cmu_clrbits(ctx, cmu_type, CMU_REG5, CMU_REG5_PLL_RESETB_MASK);
798
799 /* Configure the PLL for either 100MHz or 50MHz */
800 cmu_rd(ctx, cmu_type, CMU_REG2, &val);
801 if (cmu_type == REF_CMU) {
802 val = CMU_REG2_PLL_LFRES_SET(val, 0xa);
803 ref_100MHz = 1;
804 } else {
805 val = CMU_REG2_PLL_LFRES_SET(val, 0x3);
806 if (clk_type == CLK_EXT_DIFF)
807 ref_100MHz = 0;
808 else
809 ref_100MHz = 1;
810 }
811 if (ref_100MHz) {
812 val = CMU_REG2_PLL_FBDIV_SET(val, FBDIV_VAL_100M);
813 val = CMU_REG2_PLL_REFDIV_SET(val, REFDIV_VAL_100M);
814 } else {
815 val = CMU_REG2_PLL_FBDIV_SET(val, FBDIV_VAL_50M);
816 val = CMU_REG2_PLL_REFDIV_SET(val, REFDIV_VAL_50M);
817 }
818 cmu_wr(ctx, cmu_type, CMU_REG2, val);
819
820 /* Configure the VCO */
821 cmu_rd(ctx, cmu_type, CMU_REG3, &val);
822 if (cmu_type == REF_CMU) {
823 val = CMU_REG3_VCOVARSEL_SET(val, 0x3);
824 val = CMU_REG3_VCO_MOMSEL_INIT_SET(val, 0x10);
825 } else {
826 val = CMU_REG3_VCOVARSEL_SET(val, 0xF);
827 if (preA3Chip)
828 val = CMU_REG3_VCO_MOMSEL_INIT_SET(val, 0x15);
829 else
830 val = CMU_REG3_VCO_MOMSEL_INIT_SET(val, 0x1a);
831 val = CMU_REG3_VCO_MANMOMSEL_SET(val, 0x15);
832 }
833 cmu_wr(ctx, cmu_type, CMU_REG3, val);
834
835 /* Disable force PLL lock */
836 cmu_rd(ctx, cmu_type, CMU_REG26, &val);
837 val = CMU_REG26_FORCE_PLL_LOCK_SET(val, 0x0);
838 cmu_wr(ctx, cmu_type, CMU_REG26, val);
839
840 /* Setup PLL loop filter */
841 cmu_rd(ctx, cmu_type, CMU_REG5, &val);
842 val = CMU_REG5_PLL_LFSMCAP_SET(val, 0x3);
843 val = CMU_REG5_PLL_LFCAP_SET(val, 0x3);
844 if (cmu_type == REF_CMU || !preA3Chip)
845 val = CMU_REG5_PLL_LOCK_RESOLUTION_SET(val, 0x7);
846 else
847 val = CMU_REG5_PLL_LOCK_RESOLUTION_SET(val, 0x4);
848 cmu_wr(ctx, cmu_type, CMU_REG5, val);
849
850 /* Enable or disable manual calibration */
851 cmu_rd(ctx, cmu_type, CMU_REG6, &val);
852 val = CMU_REG6_PLL_VREGTRIM_SET(val, preA3Chip ? 0x0 : 0x2);
853 val = CMU_REG6_MAN_PVT_CAL_SET(val, preA3Chip ? 0x1 : 0x0);
854 cmu_wr(ctx, cmu_type, CMU_REG6, val);
855
856 /* Configure lane for 20-bits */
857 if (cmu_type == PHY_CMU) {
858 cmu_rd(ctx, cmu_type, CMU_REG9, &val);
859 val = CMU_REG9_TX_WORD_MODE_CH1_SET(val,
860 CMU_REG9_WORD_LEN_20BIT);
861 val = CMU_REG9_TX_WORD_MODE_CH0_SET(val,
862 CMU_REG9_WORD_LEN_20BIT);
863 val = CMU_REG9_PLL_POST_DIVBY2_SET(val, 0x1);
864 if (!preA3Chip) {
865 val = CMU_REG9_VBG_BYPASSB_SET(val, 0x0);
866 val = CMU_REG9_IGEN_BYPASS_SET(val , 0x0);
867 }
868 cmu_wr(ctx, cmu_type, CMU_REG9, val);
869
870 if (!preA3Chip) {
871 cmu_rd(ctx, cmu_type, CMU_REG10, &val);
872 val = CMU_REG10_VREG_REFSEL_SET(val, 0x1);
873 cmu_wr(ctx, cmu_type, CMU_REG10, val);
874 }
875 }
876
877 cmu_rd(ctx, cmu_type, CMU_REG16, &val);
878 val = CMU_REG16_CALIBRATION_DONE_OVERRIDE_SET(val, 0x1);
879 val = CMU_REG16_BYPASS_PLL_LOCK_SET(val, 0x1);
880 if (cmu_type == REF_CMU || preA3Chip)
881 val = CMU_REG16_VCOCAL_WAIT_BTW_CODE_SET(val, 0x4);
882 else
883 val = CMU_REG16_VCOCAL_WAIT_BTW_CODE_SET(val, 0x7);
884 cmu_wr(ctx, cmu_type, CMU_REG16, val);
885
886 /* Configure for SATA */
887 cmu_rd(ctx, cmu_type, CMU_REG30, &val);
888 val = CMU_REG30_PCIE_MODE_SET(val, 0x0);
889 val = CMU_REG30_LOCK_COUNT_SET(val, 0x3);
890 cmu_wr(ctx, cmu_type, CMU_REG30, val);
891
892 /* Disable state machine bypass */
893 cmu_wr(ctx, cmu_type, CMU_REG31, 0xF);
894
895 cmu_rd(ctx, cmu_type, CMU_REG32, &val);
896 val = CMU_REG32_PVT_CAL_WAIT_SEL_SET(val, 0x3);
897 if (cmu_type == REF_CMU || preA3Chip)
898 val = CMU_REG32_IREF_ADJ_SET(val, 0x3);
899 else
900 val = CMU_REG32_IREF_ADJ_SET(val, 0x1);
901 cmu_wr(ctx, cmu_type, CMU_REG32, val);
902
903 /* Set VCO calibration threshold */
904 if (cmu_type != REF_CMU && preA3Chip)
905 cmu_wr(ctx, cmu_type, CMU_REG34, 0x8d27);
906 else
907 cmu_wr(ctx, cmu_type, CMU_REG34, 0x873c);
908
909 /* Set CTLE Override and override waiting from state machine */
910 cmu_wr(ctx, cmu_type, CMU_REG37, 0xF00F);
911}
912
913static void xgene_phy_ssc_enable(struct xgene_phy_ctx *ctx,
914 enum cmu_type_t cmu_type)
915{
916 u32 val;
917
918 /* Set SSC modulation value */
919 cmu_rd(ctx, cmu_type, CMU_REG35, &val);
920 val = CMU_REG35_PLL_SSC_MOD_SET(val, 98);
921 cmu_wr(ctx, cmu_type, CMU_REG35, val);
922
923 /* Enable SSC, set vertical step and DSM value */
924 cmu_rd(ctx, cmu_type, CMU_REG36, &val);
925 val = CMU_REG36_PLL_SSC_VSTEP_SET(val, 30);
926 val = CMU_REG36_PLL_SSC_EN_SET(val, 1);
927 val = CMU_REG36_PLL_SSC_DSMSEL_SET(val, 1);
928 cmu_wr(ctx, cmu_type, CMU_REG36, val);
929
930 /* Reset the PLL */
931 cmu_clrbits(ctx, cmu_type, CMU_REG5, CMU_REG5_PLL_RESETB_MASK);
932 cmu_setbits(ctx, cmu_type, CMU_REG5, CMU_REG5_PLL_RESETB_MASK);
933
934 /* Force VCO calibration to restart */
935 cmu_toggle1to0(ctx, cmu_type, CMU_REG32,
936 CMU_REG32_FORCE_VCOCAL_START_MASK);
937}
938
939static void xgene_phy_sata_cfg_lanes(struct xgene_phy_ctx *ctx)
940{
941 u32 val;
942 u32 reg;
943 int i;
944 int lane;
945
946 for (lane = 0; lane < MAX_LANE; lane++) {
947 serdes_wr(ctx, lane, RXTX_REG147, 0x6);
948
949 /* Set boost control for quarter, half, and full rate */
950 serdes_rd(ctx, lane, RXTX_REG0, &val);
951 val = RXTX_REG0_CTLE_EQ_HR_SET(val, 0x10);
952 val = RXTX_REG0_CTLE_EQ_QR_SET(val, 0x10);
953 val = RXTX_REG0_CTLE_EQ_FR_SET(val, 0x10);
954 serdes_wr(ctx, lane, RXTX_REG0, val);
955
956 /* Set boost control value */
957 serdes_rd(ctx, lane, RXTX_REG1, &val);
958 val = RXTX_REG1_RXACVCM_SET(val, 0x7);
959 val = RXTX_REG1_CTLE_EQ_SET(val,
960 ctx->sata_param.txboostgain[lane * 3 +
961 ctx->sata_param.speed[lane]]);
962 serdes_wr(ctx, lane, RXTX_REG1, val);
963
964 /* Latch VTT value based on the termination to ground and
965 * enable TX FIFO
966 */
967 serdes_rd(ctx, lane, RXTX_REG2, &val);
968 val = RXTX_REG2_VTT_ENA_SET(val, 0x1);
969 val = RXTX_REG2_VTT_SEL_SET(val, 0x1);
970 val = RXTX_REG2_TX_FIFO_ENA_SET(val, 0x1);
971 serdes_wr(ctx, lane, RXTX_REG2, val);
972
973 /* Configure Tx for 20-bits */
974 serdes_rd(ctx, lane, RXTX_REG4, &val);
975 val = RXTX_REG4_TX_WORD_MODE_SET(val, CMU_REG9_WORD_LEN_20BIT);
976 serdes_wr(ctx, lane, RXTX_REG4, val);
977
978 if (!preA3Chip) {
979 serdes_rd(ctx, lane, RXTX_REG1, &val);
980 val = RXTX_REG1_RXVREG1_SET(val, 0x2);
981 val = RXTX_REG1_RXIREF_ADJ_SET(val, 0x2);
982 serdes_wr(ctx, lane, RXTX_REG1, val);
983 }
984
985 /* Set pre-emphasis first 1 and 2, and post-emphasis values */
986 serdes_rd(ctx, lane, RXTX_REG5, &val);
987 val = RXTX_REG5_TX_CN1_SET(val,
988 ctx->sata_param.txprecursor_cn1[lane * 3 +
989 ctx->sata_param.speed[lane]]);
990 val = RXTX_REG5_TX_CP1_SET(val,
991 ctx->sata_param.txpostcursor_cp1[lane * 3 +
992 ctx->sata_param.speed[lane]]);
993 val = RXTX_REG5_TX_CN2_SET(val,
994 ctx->sata_param.txprecursor_cn2[lane * 3 +
995 ctx->sata_param.speed[lane]]);
996 serdes_wr(ctx, lane, RXTX_REG5, val);
997
998 /* Set TX amplitude value */
999 serdes_rd(ctx, lane, RXTX_REG6, &val);
1000 val = RXTX_REG6_TXAMP_CNTL_SET(val,
1001 ctx->sata_param.txamplitude[lane * 3 +
1002 ctx->sata_param.speed[lane]]);
1003 val = RXTX_REG6_TXAMP_ENA_SET(val, 0x1);
1004 val = RXTX_REG6_TX_IDLE_SET(val, 0x0);
1005 val = RXTX_REG6_RX_BIST_RESYNC_SET(val, 0x0);
1006 val = RXTX_REG6_RX_BIST_ERRCNT_RD_SET(val, 0x0);
1007 serdes_wr(ctx, lane, RXTX_REG6, val);
1008
1009 /* Configure Rx for 20-bits */
1010 serdes_rd(ctx, lane, RXTX_REG7, &val);
1011 val = RXTX_REG7_BIST_ENA_RX_SET(val, 0x0);
1012 val = RXTX_REG7_RX_WORD_MODE_SET(val, CMU_REG9_WORD_LEN_20BIT);
1013 serdes_wr(ctx, lane, RXTX_REG7, val);
1014
1015 /* Set CDR and LOS values and enable Rx SSC */
1016 serdes_rd(ctx, lane, RXTX_REG8, &val);
1017 val = RXTX_REG8_CDR_LOOP_ENA_SET(val, 0x1);
1018 val = RXTX_REG8_CDR_BYPASS_RXLOS_SET(val, 0x0);
1019 val = RXTX_REG8_SSC_ENABLE_SET(val, 0x1);
1020 val = RXTX_REG8_SD_DISABLE_SET(val, 0x0);
1021 val = RXTX_REG8_SD_VREF_SET(val, 0x4);
1022 serdes_wr(ctx, lane, RXTX_REG8, val);
1023
1024 /* Set phase adjust upper/lower limits */
1025 serdes_rd(ctx, lane, RXTX_REG11, &val);
1026 val = RXTX_REG11_PHASE_ADJUST_LIMIT_SET(val, 0x0);
1027 serdes_wr(ctx, lane, RXTX_REG11, val);
1028
1029 /* Enable Latch Off; disable SUMOS and Tx termination */
1030 serdes_rd(ctx, lane, RXTX_REG12, &val);
1031 val = RXTX_REG12_LATCH_OFF_ENA_SET(val, 0x1);
1032 val = RXTX_REG12_SUMOS_ENABLE_SET(val, 0x0);
1033 val = RXTX_REG12_RX_DET_TERM_ENABLE_SET(val, 0x0);
1034 serdes_wr(ctx, lane, RXTX_REG12, val);
1035
1036 /* Set period error latch to 512T and enable BWL */
1037 serdes_rd(ctx, lane, RXTX_REG26, &val);
1038 val = RXTX_REG26_PERIOD_ERROR_LATCH_SET(val, 0x0);
1039 val = RXTX_REG26_BLWC_ENA_SET(val, 0x1);
1040 serdes_wr(ctx, lane, RXTX_REG26, val);
1041
1042 serdes_wr(ctx, lane, RXTX_REG28, 0x0);
1043
1044 /* Set DFE loop preset value */
1045 serdes_wr(ctx, lane, RXTX_REG31, 0x0);
1046
1047 /* Set Eye Monitor counter width to 12-bit */
1048 serdes_rd(ctx, lane, RXTX_REG61, &val);
1049 val = RXTX_REG61_ISCAN_INBERT_SET(val, 0x1);
1050 val = RXTX_REG61_LOADFREQ_SHIFT_SET(val, 0x0);
1051 val = RXTX_REG61_EYE_COUNT_WIDTH_SEL_SET(val, 0x0);
1052 serdes_wr(ctx, lane, RXTX_REG61, val);
1053
1054 serdes_rd(ctx, lane, RXTX_REG62, &val);
1055 val = RXTX_REG62_PERIOD_H1_QLATCH_SET(val, 0x0);
1056 serdes_wr(ctx, lane, RXTX_REG62, val);
1057
1058 /* Set BW select tap X for DFE loop */
1059 for (i = 0; i < 9; i++) {
1060 reg = RXTX_REG81 + i * 2;
1061 serdes_rd(ctx, lane, reg, &val);
1062 val = RXTX_REG89_MU_TH7_SET(val, 0xe);
1063 val = RXTX_REG89_MU_TH8_SET(val, 0xe);
1064 val = RXTX_REG89_MU_TH9_SET(val, 0xe);
1065 serdes_wr(ctx, lane, reg, val);
1066 }
1067
1068 /* Set BW select tap X for frequency adjust loop */
1069 for (i = 0; i < 3; i++) {
1070 reg = RXTX_REG96 + i * 2;
1071 serdes_rd(ctx, lane, reg, &val);
1072 val = RXTX_REG96_MU_FREQ1_SET(val, 0x10);
1073 val = RXTX_REG96_MU_FREQ2_SET(val, 0x10);
1074 val = RXTX_REG96_MU_FREQ3_SET(val, 0x10);
1075 serdes_wr(ctx, lane, reg, val);
1076 }
1077
1078 /* Set BW select tap X for phase adjust loop */
1079 for (i = 0; i < 3; i++) {
1080 reg = RXTX_REG99 + i * 2;
1081 serdes_rd(ctx, lane, reg, &val);
1082 val = RXTX_REG99_MU_PHASE1_SET(val, 0x7);
1083 val = RXTX_REG99_MU_PHASE2_SET(val, 0x7);
1084 val = RXTX_REG99_MU_PHASE3_SET(val, 0x7);
1085 serdes_wr(ctx, lane, reg, val);
1086 }
1087
1088 serdes_rd(ctx, lane, RXTX_REG102, &val);
1089 val = RXTX_REG102_FREQLOOP_LIMIT_SET(val, 0x0);
1090 serdes_wr(ctx, lane, RXTX_REG102, val);
1091
1092 serdes_wr(ctx, lane, RXTX_REG114, 0xffe0);
1093
1094 serdes_rd(ctx, lane, RXTX_REG125, &val);
1095 val = RXTX_REG125_SIGN_PQ_SET(val,
1096 ctx->sata_param.txeyedirection[lane * 3 +
1097 ctx->sata_param.speed[lane]]);
1098 val = RXTX_REG125_PQ_REG_SET(val,
1099 ctx->sata_param.txeyetuning[lane * 3 +
1100 ctx->sata_param.speed[lane]]);
1101 val = RXTX_REG125_PHZ_MANUAL_SET(val, 0x1);
1102 serdes_wr(ctx, lane, RXTX_REG125, val);
1103
1104 serdes_rd(ctx, lane, RXTX_REG127, &val);
1105 val = RXTX_REG127_LATCH_MAN_CAL_ENA_SET(val, 0x0);
1106 serdes_wr(ctx, lane, RXTX_REG127, val);
1107
1108 serdes_rd(ctx, lane, RXTX_REG128, &val);
1109 val = RXTX_REG128_LATCH_CAL_WAIT_SEL_SET(val, 0x3);
1110 serdes_wr(ctx, lane, RXTX_REG128, val);
1111
1112 serdes_rd(ctx, lane, RXTX_REG145, &val);
1113 val = RXTX_REG145_RXDFE_CONFIG_SET(val, 0x3);
1114 val = RXTX_REG145_TX_IDLE_SATA_SET(val, 0x0);
1115 if (preA3Chip) {
1116 val = RXTX_REG145_RXES_ENA_SET(val, 0x1);
1117 val = RXTX_REG145_RXVWES_LATENA_SET(val, 0x1);
1118 } else {
1119 val = RXTX_REG145_RXES_ENA_SET(val, 0x0);
1120 val = RXTX_REG145_RXVWES_LATENA_SET(val, 0x0);
1121 }
1122 serdes_wr(ctx, lane, RXTX_REG145, val);
1123
1124 /*
1125 * Set Rx LOS filter clock rate, sample rate, and threshold
1126 * windows
1127 */
1128 for (i = 0; i < 4; i++) {
1129 reg = RXTX_REG148 + i * 2;
1130 serdes_wr(ctx, lane, reg, 0xFFFF);
1131 }
1132 }
1133}
1134
1135static int xgene_phy_cal_rdy_chk(struct xgene_phy_ctx *ctx,
1136 enum cmu_type_t cmu_type,
1137 enum clk_type_t clk_type)
1138{
1139 void __iomem *csr_serdes = ctx->sds_base;
1140 int loop;
1141 u32 val;
1142
1143 /* Release PHY main reset */
1144 writel(0xdf, csr_serdes + SATA_ENET_SDS_RST_CTL);
1145 readl(csr_serdes + SATA_ENET_SDS_RST_CTL); /* Force a barrier */
1146
1147 if (cmu_type != REF_CMU) {
1148 cmu_setbits(ctx, cmu_type, CMU_REG5, CMU_REG5_PLL_RESETB_MASK);
1149 /*
1150 * As per PHY design spec, the PLL reset requires a minimum
1151 * of 800us.
1152 */
1153 usleep_range(800, 1000);
1154
1155 cmu_rd(ctx, cmu_type, CMU_REG1, &val);
1156 val = CMU_REG1_PLL_MANUALCAL_SET(val, 0x0);
1157 cmu_wr(ctx, cmu_type, CMU_REG1, val);
1158 /*
1159 * As per PHY design spec, the PLL auto calibration requires
1160 * a minimum of 800us.
1161 */
1162 usleep_range(800, 1000);
1163
1164 cmu_toggle1to0(ctx, cmu_type, CMU_REG32,
1165 CMU_REG32_FORCE_VCOCAL_START_MASK);
1166 /*
1167 * As per PHY design spec, the PLL requires a minimum of
1168 * 800us to settle.
1169 */
1170 usleep_range(800, 1000);
1171 }
1172
1173 if (!preA3Chip)
1174 goto skip_manual_cal;
1175
1176 /*
1177 * Configure the termination resister calibration
1178 * The serial receive pins, RXP/RXN, have TERMination resistor
1179 * that is required to be calibrated.
1180 */
1181 cmu_rd(ctx, cmu_type, CMU_REG17, &val);
1182 val = CMU_REG17_PVT_CODE_R2A_SET(val, 0x12);
1183 val = CMU_REG17_RESERVED_7_SET(val, 0x0);
1184 cmu_wr(ctx, cmu_type, CMU_REG17, val);
1185 cmu_toggle1to0(ctx, cmu_type, CMU_REG17,
1186 CMU_REG17_PVT_TERM_MAN_ENA_MASK);
1187 /*
1188 * The serial transmit pins, TXP/TXN, have Pull-UP and Pull-DOWN
1189 * resistors that are required to the calibrated.
1190 * Configure the pull DOWN calibration
1191 */
1192 cmu_rd(ctx, cmu_type, CMU_REG17, &val);
1193 val = CMU_REG17_PVT_CODE_R2A_SET(val, 0x29);
1194 val = CMU_REG17_RESERVED_7_SET(val, 0x0);
1195 cmu_wr(ctx, cmu_type, CMU_REG17, val);
1196 cmu_toggle1to0(ctx, cmu_type, CMU_REG16,
1197 CMU_REG16_PVT_DN_MAN_ENA_MASK);
1198 /* Configure the pull UP calibration */
1199 cmu_rd(ctx, cmu_type, CMU_REG17, &val);
1200 val = CMU_REG17_PVT_CODE_R2A_SET(val, 0x28);
1201 val = CMU_REG17_RESERVED_7_SET(val, 0x0);
1202 cmu_wr(ctx, cmu_type, CMU_REG17, val);
1203 cmu_toggle1to0(ctx, cmu_type, CMU_REG16,
1204 CMU_REG16_PVT_UP_MAN_ENA_MASK);
1205
1206skip_manual_cal:
1207 /* Poll the PLL calibration completion status for at least 1 ms */
1208 loop = 100;
1209 do {
1210 cmu_rd(ctx, cmu_type, CMU_REG7, &val);
1211 if (CMU_REG7_PLL_CALIB_DONE_RD(val))
1212 break;
1213 /*
1214 * As per PHY design spec, PLL calibration status requires
1215 * a minimum of 10us to be updated.
1216 */
1217 usleep_range(10, 100);
1218 } while (--loop > 0);
1219
1220 cmu_rd(ctx, cmu_type, CMU_REG7, &val);
1221 dev_dbg(ctx->dev, "PLL calibration %s\n",
1222 CMU_REG7_PLL_CALIB_DONE_RD(val) ? "done" : "failed");
1223 if (CMU_REG7_VCO_CAL_FAIL_RD(val)) {
1224 dev_err(ctx->dev,
1225 "PLL calibration failed due to VCO failure\n");
1226 return -1;
1227 }
1228 dev_dbg(ctx->dev, "PLL calibration successful\n");
1229
1230 cmu_rd(ctx, cmu_type, CMU_REG15, &val);
1231 dev_dbg(ctx->dev, "PHY Tx is %sready\n", val & 0x300 ? "" : "not ");
1232 return 0;
1233}
1234
1235static void xgene_phy_pdwn_force_vco(struct xgene_phy_ctx *ctx,
1236 enum cmu_type_t cmu_type,
1237 enum clk_type_t clk_type)
1238{
1239 u32 val;
1240
1241 dev_dbg(ctx->dev, "Reset VCO and re-start again\n");
1242 if (cmu_type == PHY_CMU) {
1243 cmu_rd(ctx, cmu_type, CMU_REG16, &val);
1244 val = CMU_REG16_VCOCAL_WAIT_BTW_CODE_SET(val, 0x7);
1245 cmu_wr(ctx, cmu_type, CMU_REG16, val);
1246 }
1247
1248 cmu_toggle1to0(ctx, cmu_type, CMU_REG0, CMU_REG0_PDOWN_MASK);
1249 cmu_toggle1to0(ctx, cmu_type, CMU_REG32,
1250 CMU_REG32_FORCE_VCOCAL_START_MASK);
1251}
1252
1253static int xgene_phy_hw_init_sata(struct xgene_phy_ctx *ctx,
1254 enum clk_type_t clk_type, int ssc_enable)
1255{
1256 void __iomem *sds_base = ctx->sds_base;
1257 u32 val;
1258 int i;
1259
1260 /* Configure the PHY for operation */
1261 dev_dbg(ctx->dev, "Reset PHY\n");
1262 /* Place PHY into reset */
1263 writel(0x0, sds_base + SATA_ENET_SDS_RST_CTL);
1264 val = readl(sds_base + SATA_ENET_SDS_RST_CTL); /* Force a barrier */
1265 /* Release PHY lane from reset (active high) */
1266 writel(0x20, sds_base + SATA_ENET_SDS_RST_CTL);
1267 readl(sds_base + SATA_ENET_SDS_RST_CTL); /* Force a barrier */
1268 /* Release all PHY module out of reset except PHY main reset */
1269 writel(0xde, sds_base + SATA_ENET_SDS_RST_CTL);
1270 readl(sds_base + SATA_ENET_SDS_RST_CTL); /* Force a barrier */
1271
1272 /* Set the operation speed */
1273 val = readl(sds_base + SATA_ENET_SDS_CTL1);
1274 val = CFG_I_SPD_SEL_CDR_OVR1_SET(val,
1275 ctx->sata_param.txspeed[ctx->sata_param.speed[0]]);
1276 writel(val, sds_base + SATA_ENET_SDS_CTL1);
1277
1278 dev_dbg(ctx->dev, "Set the customer pin mode to SATA\n");
1279 val = readl(sds_base + SATA_ENET_SDS_CTL0);
1280 val = REGSPEC_CFG_I_CUSTOMER_PIN_MODE0_SET(val, 0x4421);
1281 writel(val, sds_base + SATA_ENET_SDS_CTL0);
1282
1283 /* Configure the clock macro unit (CMU) clock type */
1284 xgene_phy_cfg_cmu_clk_type(ctx, PHY_CMU, clk_type);
1285
1286 /* Configure the clock macro */
1287 xgene_phy_sata_cfg_cmu_core(ctx, PHY_CMU, clk_type);
1288
1289 /* Enable SSC if enabled */
1290 if (ssc_enable)
1291 xgene_phy_ssc_enable(ctx, PHY_CMU);
1292
1293 /* Configure PHY lanes */
1294 xgene_phy_sata_cfg_lanes(ctx);
1295
1296 /* Set Rx/Tx 20-bit */
1297 val = readl(sds_base + SATA_ENET_SDS_PCS_CTL0);
1298 val = REGSPEC_CFG_I_RX_WORDMODE0_SET(val, 0x3);
1299 val = REGSPEC_CFG_I_TX_WORDMODE0_SET(val, 0x3);
1300 writel(val, sds_base + SATA_ENET_SDS_PCS_CTL0);
1301
1302 /* Start PLL calibration and try for three times */
1303 i = 10;
1304 do {
1305 if (!xgene_phy_cal_rdy_chk(ctx, PHY_CMU, clk_type))
1306 break;
1307 /* If failed, toggle the VCO power signal and start again */
1308 xgene_phy_pdwn_force_vco(ctx, PHY_CMU, clk_type);
1309 } while (--i > 0);
1310 /* Even on failure, allow to continue any way */
1311 if (i <= 0)
1312 dev_err(ctx->dev, "PLL calibration failed\n");
1313
1314 return 0;
1315}
1316
1317static int xgene_phy_hw_initialize(struct xgene_phy_ctx *ctx,
1318 enum clk_type_t clk_type,
1319 int ssc_enable)
1320{
1321 int rc;
1322
1323 dev_dbg(ctx->dev, "PHY init clk type %d\n", clk_type);
1324
1325 if (ctx->mode == MODE_SATA) {
1326 rc = xgene_phy_hw_init_sata(ctx, clk_type, ssc_enable);
1327 if (rc)
1328 return rc;
1329 } else {
1330 dev_err(ctx->dev, "Un-supported customer pin mode %d\n",
1331 ctx->mode);
1332 return -ENODEV;
1333 }
1334
1335 return 0;
1336}
1337
1338/*
1339 * Receiver Offset Calibration:
1340 *
1341 * Calibrate the receiver signal path offset in two steps - summar and
1342 * latch calibrations
1343 */
1344static void xgene_phy_force_lat_summer_cal(struct xgene_phy_ctx *ctx, int lane)
1345{
1346 int i;
1347 static const struct {
1348 u32 reg;
1349 u32 val;
1350 } serdes_reg[] = {
1351 {RXTX_REG38, 0x0},
1352 {RXTX_REG39, 0xff00},
1353 {RXTX_REG40, 0xffff},
1354 {RXTX_REG41, 0xffff},
1355 {RXTX_REG42, 0xffff},
1356 {RXTX_REG43, 0xffff},
1357 {RXTX_REG44, 0xffff},
1358 {RXTX_REG45, 0xffff},
1359 {RXTX_REG46, 0xffff},
1360 {RXTX_REG47, 0xfffc},
1361 {RXTX_REG48, 0x0},
1362 {RXTX_REG49, 0x0},
1363 {RXTX_REG50, 0x0},
1364 {RXTX_REG51, 0x0},
1365 {RXTX_REG52, 0x0},
1366 {RXTX_REG53, 0x0},
1367 {RXTX_REG54, 0x0},
1368 {RXTX_REG55, 0x0},
1369 };
1370
1371 /* Start SUMMER calibration */
1372 serdes_setbits(ctx, lane, RXTX_REG127,
1373 RXTX_REG127_FORCE_SUM_CAL_START_MASK);
1374 /*
1375 * As per PHY design spec, the Summer calibration requires a minimum
1376 * of 100us to complete.
1377 */
1378 usleep_range(100, 500);
1379 serdes_clrbits(ctx, lane, RXTX_REG127,
1380 RXTX_REG127_FORCE_SUM_CAL_START_MASK);
1381 /*
1382 * As per PHY design spec, the auto calibration requires a minimum
1383 * of 100us to complete.
1384 */
1385 usleep_range(100, 500);
1386
1387 /* Start latch calibration */
1388 serdes_setbits(ctx, lane, RXTX_REG127,
1389 RXTX_REG127_FORCE_LAT_CAL_START_MASK);
1390 /*
1391 * As per PHY design spec, the latch calibration requires a minimum
1392 * of 100us to complete.
1393 */
1394 usleep_range(100, 500);
1395 serdes_clrbits(ctx, lane, RXTX_REG127,
1396 RXTX_REG127_FORCE_LAT_CAL_START_MASK);
1397
1398 /* Configure the PHY lane for calibration */
1399 serdes_wr(ctx, lane, RXTX_REG28, 0x7);
1400 serdes_wr(ctx, lane, RXTX_REG31, 0x7e00);
1401 serdes_clrbits(ctx, lane, RXTX_REG4,
1402 RXTX_REG4_TX_LOOPBACK_BUF_EN_MASK);
1403 serdes_clrbits(ctx, lane, RXTX_REG7,
1404 RXTX_REG7_LOOP_BACK_ENA_CTLE_MASK);
1405 for (i = 0; i < ARRAY_SIZE(serdes_reg); i++)
1406 serdes_wr(ctx, lane, serdes_reg[i].reg,
1407 serdes_reg[i].val);
1408}
1409
1410static void xgene_phy_reset_rxd(struct xgene_phy_ctx *ctx, int lane)
1411{
1412 /* Reset digital Rx */
1413 serdes_clrbits(ctx, lane, RXTX_REG7, RXTX_REG7_RESETB_RXD_MASK);
1414 /* As per PHY design spec, the reset requires a minimum of 100us. */
1415 usleep_range(100, 150);
1416 serdes_setbits(ctx, lane, RXTX_REG7, RXTX_REG7_RESETB_RXD_MASK);
1417}
1418
1419static int xgene_phy_get_avg(int accum, int samples)
1420{
1421 return (accum + (samples / 2)) / samples;
1422}
1423
1424static void xgene_phy_gen_avg_val(struct xgene_phy_ctx *ctx, int lane)
1425{
1426 int max_loop = 10;
1427 int avg_loop = 0;
1428 int lat_do = 0, lat_xo = 0, lat_eo = 0, lat_so = 0;
1429 int lat_de = 0, lat_xe = 0, lat_ee = 0, lat_se = 0;
1430 int sum_cal = 0;
1431 int lat_do_itr, lat_xo_itr, lat_eo_itr, lat_so_itr;
1432 int lat_de_itr, lat_xe_itr, lat_ee_itr, lat_se_itr;
1433 int sum_cal_itr;
1434 int fail_even;
1435 int fail_odd;
1436 u32 val;
1437
1438 dev_dbg(ctx->dev, "Generating avg calibration value for lane %d\n",
1439 lane);
1440
1441 /* Enable RX Hi-Z termination */
1442 serdes_setbits(ctx, lane, RXTX_REG12,
1443 RXTX_REG12_RX_DET_TERM_ENABLE_MASK);
1444 /* Turn off DFE */
1445 serdes_wr(ctx, lane, RXTX_REG28, 0x0000);
1446 /* DFE Presets to zero */
1447 serdes_wr(ctx, lane, RXTX_REG31, 0x0000);
1448
1449 /*
1450 * Receiver Offset Calibration:
1451 * Calibrate the receiver signal path offset in two steps - summar
1452 * and latch calibration.
1453 * Runs the "Receiver Offset Calibration multiple times to determine
1454 * the average value to use.
1455 */
1456 while (avg_loop < max_loop) {
1457 /* Start the calibration */
1458 xgene_phy_force_lat_summer_cal(ctx, lane);
1459
1460 serdes_rd(ctx, lane, RXTX_REG21, &val);
1461 lat_do_itr = RXTX_REG21_DO_LATCH_CALOUT_RD(val);
1462 lat_xo_itr = RXTX_REG21_XO_LATCH_CALOUT_RD(val);
1463 fail_odd = RXTX_REG21_LATCH_CAL_FAIL_ODD_RD(val);
1464
1465 serdes_rd(ctx, lane, RXTX_REG22, &val);
1466 lat_eo_itr = RXTX_REG22_EO_LATCH_CALOUT_RD(val);
1467 lat_so_itr = RXTX_REG22_SO_LATCH_CALOUT_RD(val);
1468 fail_even = RXTX_REG22_LATCH_CAL_FAIL_EVEN_RD(val);
1469
1470 serdes_rd(ctx, lane, RXTX_REG23, &val);
1471 lat_de_itr = RXTX_REG23_DE_LATCH_CALOUT_RD(val);
1472 lat_xe_itr = RXTX_REG23_XE_LATCH_CALOUT_RD(val);
1473
1474 serdes_rd(ctx, lane, RXTX_REG24, &val);
1475 lat_ee_itr = RXTX_REG24_EE_LATCH_CALOUT_RD(val);
1476 lat_se_itr = RXTX_REG24_SE_LATCH_CALOUT_RD(val);
1477
1478 serdes_rd(ctx, lane, RXTX_REG121, &val);
1479 sum_cal_itr = RXTX_REG121_SUMOS_CAL_CODE_RD(val);
1480
1481 /* Check for failure. If passed, sum them for averaging */
1482 if ((fail_even == 0 || fail_even == 1) &&
1483 (fail_odd == 0 || fail_odd == 1)) {
1484 lat_do += lat_do_itr;
1485 lat_xo += lat_xo_itr;
1486 lat_eo += lat_eo_itr;
1487 lat_so += lat_so_itr;
1488 lat_de += lat_de_itr;
1489 lat_xe += lat_xe_itr;
1490 lat_ee += lat_ee_itr;
1491 lat_se += lat_se_itr;
1492 sum_cal += sum_cal_itr;
1493
1494 dev_dbg(ctx->dev, "Iteration %d:\n", avg_loop);
1495 dev_dbg(ctx->dev, "DO 0x%x XO 0x%x EO 0x%x SO 0x%x\n",
1496 lat_do_itr, lat_xo_itr, lat_eo_itr,
1497 lat_so_itr);
1498 dev_dbg(ctx->dev, "DE 0x%x XE 0x%x EE 0x%x SE 0x%x\n",
1499 lat_de_itr, lat_xe_itr, lat_ee_itr,
1500 lat_se_itr);
1501 dev_dbg(ctx->dev, "SUM 0x%x\n", sum_cal_itr);
1502 ++avg_loop;
1503 } else {
1504 dev_err(ctx->dev,
1505 "Receiver calibration failed at %d loop\n",
1506 avg_loop);
1507 }
1508 xgene_phy_reset_rxd(ctx, lane);
1509 }
1510
1511 /* Update latch manual calibration with average value */
1512 serdes_rd(ctx, lane, RXTX_REG127, &val);
1513 val = RXTX_REG127_DO_LATCH_MANCAL_SET(val,
1514 xgene_phy_get_avg(lat_do, max_loop));
1515 val = RXTX_REG127_XO_LATCH_MANCAL_SET(val,
1516 xgene_phy_get_avg(lat_xo, max_loop));
1517 serdes_wr(ctx, lane, RXTX_REG127, val);
1518
1519 serdes_rd(ctx, lane, RXTX_REG128, &val);
1520 val = RXTX_REG128_EO_LATCH_MANCAL_SET(val,
1521 xgene_phy_get_avg(lat_eo, max_loop));
1522 val = RXTX_REG128_SO_LATCH_MANCAL_SET(val,
1523 xgene_phy_get_avg(lat_so, max_loop));
1524 serdes_wr(ctx, lane, RXTX_REG128, val);
1525
1526 serdes_rd(ctx, lane, RXTX_REG129, &val);
1527 val = RXTX_REG129_DE_LATCH_MANCAL_SET(val,
1528 xgene_phy_get_avg(lat_de, max_loop));
1529 val = RXTX_REG129_XE_LATCH_MANCAL_SET(val,
1530 xgene_phy_get_avg(lat_xe, max_loop));
1531 serdes_wr(ctx, lane, RXTX_REG129, val);
1532
1533 serdes_rd(ctx, lane, RXTX_REG130, &val);
1534 val = RXTX_REG130_EE_LATCH_MANCAL_SET(val,
1535 xgene_phy_get_avg(lat_ee, max_loop));
1536 val = RXTX_REG130_SE_LATCH_MANCAL_SET(val,
1537 xgene_phy_get_avg(lat_se, max_loop));
1538 serdes_wr(ctx, lane, RXTX_REG130, val);
1539
1540 /* Update SUMMER calibration with average value */
1541 serdes_rd(ctx, lane, RXTX_REG14, &val);
1542 val = RXTX_REG14_CLTE_LATCAL_MAN_PROG_SET(val,
1543 xgene_phy_get_avg(sum_cal, max_loop));
1544 serdes_wr(ctx, lane, RXTX_REG14, val);
1545
1546 dev_dbg(ctx->dev, "Average Value:\n");
1547 dev_dbg(ctx->dev, "DO 0x%x XO 0x%x EO 0x%x SO 0x%x\n",
1548 xgene_phy_get_avg(lat_do, max_loop),
1549 xgene_phy_get_avg(lat_xo, max_loop),
1550 xgene_phy_get_avg(lat_eo, max_loop),
1551 xgene_phy_get_avg(lat_so, max_loop));
1552 dev_dbg(ctx->dev, "DE 0x%x XE 0x%x EE 0x%x SE 0x%x\n",
1553 xgene_phy_get_avg(lat_de, max_loop),
1554 xgene_phy_get_avg(lat_xe, max_loop),
1555 xgene_phy_get_avg(lat_ee, max_loop),
1556 xgene_phy_get_avg(lat_se, max_loop));
1557 dev_dbg(ctx->dev, "SUM 0x%x\n",
1558 xgene_phy_get_avg(sum_cal, max_loop));
1559
1560 serdes_rd(ctx, lane, RXTX_REG14, &val);
1561 val = RXTX_REG14_CTLE_LATCAL_MAN_ENA_SET(val, 0x1);
1562 serdes_wr(ctx, lane, RXTX_REG14, val);
1563 dev_dbg(ctx->dev, "Enable Manual Summer calibration\n");
1564
1565 serdes_rd(ctx, lane, RXTX_REG127, &val);
1566 val = RXTX_REG127_LATCH_MAN_CAL_ENA_SET(val, 0x1);
1567 dev_dbg(ctx->dev, "Enable Manual Latch calibration\n");
1568 serdes_wr(ctx, lane, RXTX_REG127, val);
1569
1570 /* Disable RX Hi-Z termination */
1571 serdes_rd(ctx, lane, RXTX_REG12, &val);
1572 val = RXTX_REG12_RX_DET_TERM_ENABLE_SET(val, 0);
1573 serdes_wr(ctx, lane, RXTX_REG12, val);
1574 /* Turn on DFE */
1575 serdes_wr(ctx, lane, RXTX_REG28, 0x0007);
1576 /* Set DFE preset */
1577 serdes_wr(ctx, lane, RXTX_REG31, 0x7e00);
1578}
1579
1580static int xgene_phy_hw_init(struct phy *phy)
1581{
1582 struct xgene_phy_ctx *ctx = phy_get_drvdata(phy);
1583 int rc;
1584 int i;
1585
1586 rc = xgene_phy_hw_initialize(ctx, CLK_EXT_DIFF, SSC_DISABLE);
1587 if (rc) {
1588 dev_err(ctx->dev, "PHY initialize failed %d\n", rc);
1589 return rc;
1590 }
1591
1592 /* Setup clock properly after PHY configuration */
1593 if (!IS_ERR(ctx->clk)) {
1594 /* HW requires an toggle of the clock */
1595 clk_prepare_enable(ctx->clk);
1596 clk_disable_unprepare(ctx->clk);
1597 clk_prepare_enable(ctx->clk);
1598 }
1599
1600 /* Compute average value */
1601 for (i = 0; i < MAX_LANE; i++)
1602 xgene_phy_gen_avg_val(ctx, i);
1603
1604 dev_dbg(ctx->dev, "PHY initialized\n");
1605 return 0;
1606}
1607
1608static const struct phy_ops xgene_phy_ops = {
1609 .init = xgene_phy_hw_init,
1610 .owner = THIS_MODULE,
1611};
1612
1613static struct phy *xgene_phy_xlate(struct device *dev,
1614 const struct of_phandle_args *args)
1615{
1616 struct xgene_phy_ctx *ctx = dev_get_drvdata(dev);
1617
1618 if (args->args_count <= 0)
1619 return ERR_PTR(-EINVAL);
1620 if (args->args[0] >= MODE_MAX)
1621 return ERR_PTR(-EINVAL);
1622
1623 ctx->mode = args->args[0];
1624 return ctx->phy;
1625}
1626
1627static void xgene_phy_get_param(struct platform_device *pdev,
1628 const char *name, u32 *buffer,
1629 int count, u32 *default_val,
1630 u32 conv_factor)
1631{
1632 int i;
1633
1634 if (!of_property_read_u32_array(pdev->dev.of_node, name, buffer,
1635 count)) {
1636 for (i = 0; i < count; i++)
1637 buffer[i] /= conv_factor;
1638 return;
1639 }
1640 /* Does not exist, load default */
1641 for (i = 0; i < count; i++)
1642 buffer[i] = default_val[i % 3];
1643}
1644
1645static int xgene_phy_probe(struct platform_device *pdev)
1646{
1647 struct phy_provider *phy_provider;
1648 struct xgene_phy_ctx *ctx;
1649 u32 default_spd[] = DEFAULT_SATA_SPD_SEL;
1650 u32 default_txboost_gain[] = DEFAULT_SATA_TXBOOST_GAIN;
1651 u32 default_txeye_direction[] = DEFAULT_SATA_TXEYEDIRECTION;
1652 u32 default_txeye_tuning[] = DEFAULT_SATA_TXEYETUNING;
1653 u32 default_txamp[] = DEFAULT_SATA_TXAMP;
1654 u32 default_txcn1[] = DEFAULT_SATA_TXCN1;
1655 u32 default_txcn2[] = DEFAULT_SATA_TXCN2;
1656 u32 default_txcp1[] = DEFAULT_SATA_TXCP1;
1657 int i;
1658
1659 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
1660 if (!ctx)
1661 return -ENOMEM;
1662
1663 ctx->dev = &pdev->dev;
1664
1665 ctx->sds_base = devm_platform_ioremap_resource(pdev, 0);
1666 if (IS_ERR(ctx->sds_base))
1667 return PTR_ERR(ctx->sds_base);
1668
1669 /* Retrieve optional clock */
1670 ctx->clk = clk_get(&pdev->dev, NULL);
1671
1672 /* Load override paramaters */
1673 xgene_phy_get_param(pdev, "apm,tx-eye-tuning",
1674 ctx->sata_param.txeyetuning, 6, default_txeye_tuning, 1);
1675 xgene_phy_get_param(pdev, "apm,tx-eye-direction",
1676 ctx->sata_param.txeyedirection, 6, default_txeye_direction, 1);
1677 xgene_phy_get_param(pdev, "apm,tx-boost-gain",
1678 ctx->sata_param.txboostgain, 6, default_txboost_gain, 1);
1679 xgene_phy_get_param(pdev, "apm,tx-amplitude",
1680 ctx->sata_param.txamplitude, 6, default_txamp, 13300);
1681 xgene_phy_get_param(pdev, "apm,tx-pre-cursor1",
1682 ctx->sata_param.txprecursor_cn1, 6, default_txcn1, 18200);
1683 xgene_phy_get_param(pdev, "apm,tx-pre-cursor2",
1684 ctx->sata_param.txprecursor_cn2, 6, default_txcn2, 18200);
1685 xgene_phy_get_param(pdev, "apm,tx-post-cursor",
1686 ctx->sata_param.txpostcursor_cp1, 6, default_txcp1, 18200);
1687 xgene_phy_get_param(pdev, "apm,tx-speed",
1688 ctx->sata_param.txspeed, 3, default_spd, 1);
1689 for (i = 0; i < MAX_LANE; i++)
1690 ctx->sata_param.speed[i] = 2; /* Default to Gen3 */
1691
1692 platform_set_drvdata(pdev, ctx);
1693
1694 ctx->phy = devm_phy_create(ctx->dev, NULL, &xgene_phy_ops);
1695 if (IS_ERR(ctx->phy)) {
1696 dev_dbg(&pdev->dev, "Failed to create PHY\n");
1697 return PTR_ERR(ctx->phy);
1698 }
1699 phy_set_drvdata(ctx->phy, ctx);
1700
1701 phy_provider = devm_of_phy_provider_register(ctx->dev, xgene_phy_xlate);
1702 return PTR_ERR_OR_ZERO(phy_provider);
1703}
1704
1705static const struct of_device_id xgene_phy_of_match[] = {
1706 {.compatible = "apm,xgene-phy",},
1707 {},
1708};
1709MODULE_DEVICE_TABLE(of, xgene_phy_of_match);
1710
1711static struct platform_driver xgene_phy_driver = {
1712 .probe = xgene_phy_probe,
1713 .driver = {
1714 .name = "xgene-phy",
1715 .of_match_table = xgene_phy_of_match,
1716 },
1717};
1718module_platform_driver(xgene_phy_driver);
1719
1720MODULE_DESCRIPTION("APM X-Gene Multi-Purpose PHY driver");
1721MODULE_AUTHOR("Loc Ho <lho@apm.com>");
1722MODULE_LICENSE("GPL v2");
1723MODULE_VERSION("0.1");