blob: d8ff8956ed056aa6b8422d1e457491fc7650843e [file] [log] [blame]
Gabriel FERNANDEZ2c14e9b2014-11-04 11:51:19 +01001/*
2 * Copyright (C) 2014 STMicroelectronics
3 *
4 * STMicroelectronics PHY driver MiPHY28lp (for SoC STiH407).
5 *
6 * Author: Alexandre Torgue <alexandre.torgue@st.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2, as
10 * published by the Free Software Foundation.
11 *
12 */
13
14#include <linux/platform_device.h>
15#include <linux/io.h>
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/of.h>
19#include <linux/of_platform.h>
20#include <linux/of_address.h>
21#include <linux/clk.h>
22#include <linux/phy/phy.h>
23#include <linux/delay.h>
24#include <linux/mfd/syscon.h>
25#include <linux/regmap.h>
26#include <linux/reset.h>
27
28#include <dt-bindings/phy/phy.h>
29
30/* MiPHY registers */
31#define MIPHY_CONF_RESET 0x00
32#define RST_APPLI_SW BIT(0)
33#define RST_CONF_SW BIT(1)
34#define RST_MACRO_SW BIT(2)
35
36#define MIPHY_RESET 0x01
37#define RST_PLL_SW BIT(0)
38#define RST_COMP_SW BIT(2)
39
40#define MIPHY_STATUS_1 0x02
41#define PHY_RDY BIT(0)
42#define HFC_RDY BIT(1)
43#define HFC_PLL BIT(2)
44
45#define MIPHY_CONTROL 0x04
46#define TERM_EN_SW BIT(2)
47#define DIS_LINK_RST BIT(3)
48#define AUTO_RST_RX BIT(4)
49#define PX_RX_POL BIT(5)
50
51#define MIPHY_BOUNDARY_SEL 0x0a
52#define TX_SEL BIT(6)
53#define SSC_SEL BIT(4)
54#define GENSEL_SEL BIT(0)
55
56#define MIPHY_BOUNDARY_1 0x0b
57#define MIPHY_BOUNDARY_2 0x0c
58#define SSC_EN_SW BIT(2)
59
60#define MIPHY_PLL_CLKREF_FREQ 0x0d
61#define MIPHY_SPEED 0x0e
62#define TX_SPDSEL_80DEC 0
63#define TX_SPDSEL_40DEC 1
64#define TX_SPDSEL_20DEC 2
65#define RX_SPDSEL_80DEC 0
66#define RX_SPDSEL_40DEC (1 << 2)
67#define RX_SPDSEL_20DEC (2 << 2)
68
69#define MIPHY_CONF 0x0f
70#define MIPHY_CTRL_TEST_SEL 0x20
71#define MIPHY_CTRL_TEST_1 0x21
72#define MIPHY_CTRL_TEST_2 0x22
73#define MIPHY_CTRL_TEST_3 0x23
74#define MIPHY_CTRL_TEST_4 0x24
75#define MIPHY_FEEDBACK_TEST 0x25
76#define MIPHY_DEBUG_BUS 0x26
77#define MIPHY_DEBUG_STATUS_MSB 0x27
78#define MIPHY_DEBUG_STATUS_LSB 0x28
79#define MIPHY_PWR_RAIL_1 0x29
80#define MIPHY_PWR_RAIL_2 0x2a
81#define MIPHY_SYNCHAR_CONTROL 0x30
82
83#define MIPHY_COMP_FSM_1 0x3a
84#define COMP_START BIT(6)
85
86#define MIPHY_COMP_FSM_6 0x3f
87#define COMP_DONE BIT(7)
88
89#define MIPHY_COMP_POSTP 0x42
90#define MIPHY_TX_CTRL_1 0x49
91#define TX_REG_STEP_0V 0
92#define TX_REG_STEP_P_25MV 1
93#define TX_REG_STEP_P_50MV 2
94#define TX_REG_STEP_N_25MV 7
95#define TX_REG_STEP_N_50MV 6
96#define TX_REG_STEP_N_75MV 5
97
98#define MIPHY_TX_CTRL_2 0x4a
99#define TX_SLEW_SW_40_PS 0
100#define TX_SLEW_SW_80_PS 1
101#define TX_SLEW_SW_120_PS 2
102
103#define MIPHY_TX_CTRL_3 0x4b
104#define MIPHY_TX_CAL_MAN 0x4e
105#define TX_SLEW_CAL_MAN_EN BIT(0)
106
107#define MIPHY_TST_BIAS_BOOST_2 0x62
108#define MIPHY_BIAS_BOOST_1 0x63
109#define MIPHY_BIAS_BOOST_2 0x64
110#define MIPHY_RX_DESBUFF_FDB_2 0x67
111#define MIPHY_RX_DESBUFF_FDB_3 0x68
112#define MIPHY_SIGDET_COMPENS1 0x69
113#define MIPHY_SIGDET_COMPENS2 0x6a
114#define MIPHY_JITTER_PERIOD 0x6b
115#define MIPHY_JITTER_AMPLITUDE_1 0x6c
116#define MIPHY_JITTER_AMPLITUDE_2 0x6d
117#define MIPHY_JITTER_AMPLITUDE_3 0x6e
118#define MIPHY_RX_K_GAIN 0x78
119#define MIPHY_RX_BUFFER_CTRL 0x7a
120#define VGA_GAIN BIT(0)
121#define EQ_DC_GAIN BIT(2)
122#define EQ_BOOST_GAIN BIT(3)
123
124#define MIPHY_RX_VGA_GAIN 0x7b
125#define MIPHY_RX_EQU_GAIN_1 0x7f
126#define MIPHY_RX_EQU_GAIN_2 0x80
127#define MIPHY_RX_EQU_GAIN_3 0x81
128#define MIPHY_RX_CAL_CTRL_1 0x97
129#define MIPHY_RX_CAL_CTRL_2 0x98
130
131#define MIPHY_RX_CAL_OFFSET_CTRL 0x99
132#define CAL_OFFSET_VGA_64 (0x03 << 0)
133#define CAL_OFFSET_THRESHOLD_64 (0x03 << 2)
134#define VGA_OFFSET_POLARITY BIT(4)
135#define OFFSET_COMPENSATION_EN BIT(6)
136
137#define MIPHY_RX_CAL_VGA_STEP 0x9a
138#define MIPHY_RX_CAL_EYE_MIN 0x9d
139#define MIPHY_RX_CAL_OPT_LENGTH 0x9f
140#define MIPHY_RX_LOCK_CTRL_1 0xc1
141#define MIPHY_RX_LOCK_SETTINGS_OPT 0xc2
142#define MIPHY_RX_LOCK_STEP 0xc4
143
144#define MIPHY_RX_SIGDET_SLEEP_OA 0xc9
145#define MIPHY_RX_SIGDET_SLEEP_SEL 0xca
146#define MIPHY_RX_SIGDET_WAIT_SEL 0xcb
147#define MIPHY_RX_SIGDET_DATA_SEL 0xcc
148#define EN_ULTRA_LOW_POWER BIT(0)
149#define EN_FIRST_HALF BIT(1)
150#define EN_SECOND_HALF BIT(2)
151#define EN_DIGIT_SIGNAL_CHECK BIT(3)
152
153#define MIPHY_RX_POWER_CTRL_1 0xcd
154#define MIPHY_RX_POWER_CTRL_2 0xce
155#define MIPHY_PLL_CALSET_CTRL 0xd3
156#define MIPHY_PLL_CALSET_1 0xd4
157#define MIPHY_PLL_CALSET_2 0xd5
158#define MIPHY_PLL_CALSET_3 0xd6
159#define MIPHY_PLL_CALSET_4 0xd7
160#define MIPHY_PLL_SBR_1 0xe3
161#define SET_NEW_CHANGE BIT(1)
162
163#define MIPHY_PLL_SBR_2 0xe4
164#define MIPHY_PLL_SBR_3 0xe5
165#define MIPHY_PLL_SBR_4 0xe6
166#define MIPHY_PLL_COMMON_MISC_2 0xe9
167#define START_ACT_FILT BIT(6)
168
169#define MIPHY_PLL_SPAREIN 0xeb
170
171/*
172 * On STiH407 the glue logic can be different among MiPHY devices; for example:
173 * MiPHY0: OSC_FORCE_EXT means:
174 * 0: 30MHz crystal clk - 1: 100MHz ext clk routed through MiPHY1
175 * MiPHY1: OSC_FORCE_EXT means:
176 * 1: 30MHz crystal clk - 0: 100MHz ext clk routed through MiPHY1
177 * Some devices have not the possibility to check if the osc is ready.
178 */
179#define MIPHY_OSC_FORCE_EXT BIT(3)
180#define MIPHY_OSC_RDY BIT(5)
181
182#define MIPHY_CTRL_MASK 0x0f
183#define MIPHY_CTRL_DEFAULT 0
184#define MIPHY_CTRL_SYNC_D_EN BIT(2)
185
186/* SATA / PCIe defines */
187#define SATA_CTRL_MASK 0x07
188#define PCIE_CTRL_MASK 0xff
189#define SATA_CTRL_SELECT_SATA 1
190#define SATA_CTRL_SELECT_PCIE 0
191#define SYSCFG_PCIE_PCIE_VAL 0x80
192#define SATA_SPDMODE 1
193
Gabriel FERNANDEZ2b041b272014-11-04 11:51:21 +0100194#define MIPHY_SATA_BANK_NB 3
Gabriel FERNANDEZa2108de2014-11-04 11:51:22 +0100195#define MIPHY_PCIE_BANK_NB 2
Gabriel FERNANDEZ2b041b272014-11-04 11:51:21 +0100196
Gabriel FERNANDEZ2c14e9b2014-11-04 11:51:19 +0100197struct miphy28lp_phy {
198 struct phy *phy;
199 struct miphy28lp_dev *phydev;
200 void __iomem *base;
201 void __iomem *pipebase;
202
203 bool osc_force_ext;
204 bool osc_rdy;
205 bool px_rx_pol_inv;
Gabriel FERNANDEZ2b041b272014-11-04 11:51:21 +0100206 bool ssc;
Gabriel FERNANDEZ2c14e9b2014-11-04 11:51:19 +0100207
208 struct reset_control *miphy_rst;
209
210 u32 sata_gen;
211
212 /* Sysconfig registers offsets needed to configure the device */
213 u32 syscfg_miphy_ctrl;
214 u32 syscfg_miphy_status;
215 u32 syscfg_pci;
216 u32 syscfg_sata;
217 u8 type;
218};
219
220struct miphy28lp_dev {
221 struct device *dev;
222 struct regmap *regmap;
223 struct mutex miphy_mutex;
224 struct miphy28lp_phy **phys;
225};
226
227struct miphy_initval {
228 u16 reg;
229 u16 val;
230};
231
232enum miphy_sata_gen { SATA_GEN1, SATA_GEN2, SATA_GEN3 };
233
234static char *PHY_TYPE_name[] = { "sata-up", "pcie-up", "", "usb3-up" };
235
236struct pll_ratio {
237 int clk_ref;
238 int calset_1;
239 int calset_2;
240 int calset_3;
241 int calset_4;
242 int cal_ctrl;
243};
244
245static struct pll_ratio sata_pll_ratio = {
246 .clk_ref = 0x1e,
247 .calset_1 = 0xc8,
248 .calset_2 = 0x00,
249 .calset_3 = 0x00,
250 .calset_4 = 0x00,
251 .cal_ctrl = 0x00,
252};
253
254static struct pll_ratio pcie_pll_ratio = {
255 .clk_ref = 0x1e,
256 .calset_1 = 0xa6,
257 .calset_2 = 0xaa,
258 .calset_3 = 0xaa,
259 .calset_4 = 0x00,
260 .cal_ctrl = 0x00,
261};
262
263static struct pll_ratio usb3_pll_ratio = {
264 .clk_ref = 0x1e,
265 .calset_1 = 0xa6,
266 .calset_2 = 0xaa,
267 .calset_3 = 0xaa,
268 .calset_4 = 0x04,
269 .cal_ctrl = 0x00,
270};
271
272struct miphy28lp_pll_gen {
273 int bank;
274 int speed;
275 int bias_boost_1;
276 int bias_boost_2;
277 int tx_ctrl_1;
278 int tx_ctrl_2;
279 int tx_ctrl_3;
280 int rx_k_gain;
281 int rx_vga_gain;
282 int rx_equ_gain_1;
283 int rx_equ_gain_2;
284 int rx_equ_gain_3;
285 int rx_buff_ctrl;
286};
287
288static struct miphy28lp_pll_gen sata_pll_gen[] = {
289 {
290 .bank = 0x00,
291 .speed = TX_SPDSEL_80DEC | RX_SPDSEL_80DEC,
292 .bias_boost_1 = 0x00,
293 .bias_boost_2 = 0xae,
294 .tx_ctrl_2 = 0x53,
295 .tx_ctrl_3 = 0x00,
296 .rx_buff_ctrl = EQ_BOOST_GAIN | EQ_DC_GAIN | VGA_GAIN,
297 .rx_vga_gain = 0x00,
298 .rx_equ_gain_1 = 0x7d,
299 .rx_equ_gain_2 = 0x56,
300 .rx_equ_gain_3 = 0x00,
301 },
302 {
303 .bank = 0x01,
304 .speed = TX_SPDSEL_40DEC | RX_SPDSEL_40DEC,
305 .bias_boost_1 = 0x00,
306 .bias_boost_2 = 0xae,
307 .tx_ctrl_2 = 0x72,
308 .tx_ctrl_3 = 0x20,
309 .rx_buff_ctrl = EQ_BOOST_GAIN | EQ_DC_GAIN | VGA_GAIN,
310 .rx_vga_gain = 0x00,
311 .rx_equ_gain_1 = 0x7d,
312 .rx_equ_gain_2 = 0x56,
313 .rx_equ_gain_3 = 0x00,
314 },
315 {
316 .bank = 0x02,
317 .speed = TX_SPDSEL_20DEC | RX_SPDSEL_20DEC,
318 .bias_boost_1 = 0x00,
319 .bias_boost_2 = 0xae,
320 .tx_ctrl_2 = 0xc0,
321 .tx_ctrl_3 = 0x20,
322 .rx_buff_ctrl = EQ_BOOST_GAIN | EQ_DC_GAIN | VGA_GAIN,
323 .rx_vga_gain = 0x00,
324 .rx_equ_gain_1 = 0x7d,
325 .rx_equ_gain_2 = 0x56,
326 .rx_equ_gain_3 = 0x00,
327 },
328};
329
330static struct miphy28lp_pll_gen pcie_pll_gen[] = {
331 {
332 .bank = 0x00,
333 .speed = TX_SPDSEL_40DEC | RX_SPDSEL_40DEC,
334 .bias_boost_1 = 0x00,
335 .bias_boost_2 = 0xa5,
336 .tx_ctrl_1 = TX_REG_STEP_N_25MV,
337 .tx_ctrl_2 = 0x71,
338 .tx_ctrl_3 = 0x60,
339 .rx_k_gain = 0x98,
340 .rx_buff_ctrl = EQ_BOOST_GAIN | EQ_DC_GAIN | VGA_GAIN,
341 .rx_vga_gain = 0x00,
342 .rx_equ_gain_1 = 0x79,
343 .rx_equ_gain_2 = 0x56,
344 },
345 {
346 .bank = 0x01,
347 .speed = TX_SPDSEL_20DEC | RX_SPDSEL_20DEC,
348 .bias_boost_1 = 0x00,
349 .bias_boost_2 = 0xa5,
350 .tx_ctrl_1 = TX_REG_STEP_N_25MV,
351 .tx_ctrl_2 = 0x70,
352 .tx_ctrl_3 = 0x60,
353 .rx_k_gain = 0xcc,
354 .rx_buff_ctrl = EQ_BOOST_GAIN | EQ_DC_GAIN | VGA_GAIN,
355 .rx_vga_gain = 0x00,
356 .rx_equ_gain_1 = 0x78,
357 .rx_equ_gain_2 = 0x07,
358 },
359};
360
361static inline void miphy28lp_set_reset(struct miphy28lp_phy *miphy_phy)
362{
363 void *base = miphy_phy->base;
364 u8 val;
365
366 /* Putting Macro in reset */
367 writeb_relaxed(RST_APPLI_SW, base + MIPHY_CONF_RESET);
368
369 val = RST_APPLI_SW | RST_CONF_SW;
370 writeb_relaxed(val, base + MIPHY_CONF_RESET);
371
372 writeb_relaxed(RST_APPLI_SW, base + MIPHY_CONF_RESET);
373
374 /* Bringing the MIPHY-CPU registers out of reset */
375 if (miphy_phy->type == PHY_TYPE_PCIE) {
376 val = AUTO_RST_RX | TERM_EN_SW;
377 writeb_relaxed(val, base + MIPHY_CONTROL);
378 } else {
379 val = AUTO_RST_RX | TERM_EN_SW | DIS_LINK_RST;
380 writeb_relaxed(val, base + MIPHY_CONTROL);
381 }
382}
383
384static inline void miphy28lp_pll_calibration(struct miphy28lp_phy *miphy_phy,
385 struct pll_ratio *pll_ratio)
386{
387 void *base = miphy_phy->base;
388 u8 val;
389
390 /* Applying PLL Settings */
391 writeb_relaxed(0x1d, base + MIPHY_PLL_SPAREIN);
392 writeb_relaxed(pll_ratio->clk_ref, base + MIPHY_PLL_CLKREF_FREQ);
393
394 /* PLL Ratio */
395 writeb_relaxed(pll_ratio->calset_1, base + MIPHY_PLL_CALSET_1);
396 writeb_relaxed(pll_ratio->calset_2, base + MIPHY_PLL_CALSET_2);
397 writeb_relaxed(pll_ratio->calset_3, base + MIPHY_PLL_CALSET_3);
398 writeb_relaxed(pll_ratio->calset_4, base + MIPHY_PLL_CALSET_4);
399 writeb_relaxed(pll_ratio->cal_ctrl, base + MIPHY_PLL_CALSET_CTRL);
400
401 writeb_relaxed(TX_SEL, base + MIPHY_BOUNDARY_SEL);
402
403 val = (0x68 << 1) | TX_SLEW_CAL_MAN_EN;
404 writeb_relaxed(val, base + MIPHY_TX_CAL_MAN);
405
406 val = VGA_OFFSET_POLARITY | CAL_OFFSET_THRESHOLD_64 | CAL_OFFSET_VGA_64;
407
408 if (miphy_phy->type != PHY_TYPE_SATA)
409 val |= OFFSET_COMPENSATION_EN;
410
411 writeb_relaxed(val, base + MIPHY_RX_CAL_OFFSET_CTRL);
412
413 if (miphy_phy->type == PHY_TYPE_USB3) {
414 writeb_relaxed(0x00, base + MIPHY_CONF);
415 writeb_relaxed(0x70, base + MIPHY_RX_LOCK_STEP);
416 writeb_relaxed(EN_FIRST_HALF, base + MIPHY_RX_SIGDET_SLEEP_OA);
417 writeb_relaxed(EN_FIRST_HALF, base + MIPHY_RX_SIGDET_SLEEP_SEL);
418 writeb_relaxed(EN_FIRST_HALF, base + MIPHY_RX_SIGDET_WAIT_SEL);
419
420 val = EN_DIGIT_SIGNAL_CHECK | EN_FIRST_HALF;
421 writeb_relaxed(val, base + MIPHY_RX_SIGDET_DATA_SEL);
422 }
423
424}
425
426static inline void miphy28lp_sata_config_gen(struct miphy28lp_phy *miphy_phy)
427{
428 void __iomem *base = miphy_phy->base;
429 int i;
430
431 for (i = 0; i < ARRAY_SIZE(sata_pll_gen); i++) {
432 struct miphy28lp_pll_gen *gen = &sata_pll_gen[i];
433
434 /* Banked settings */
435 writeb_relaxed(gen->bank, base + MIPHY_CONF);
436 writeb_relaxed(gen->speed, base + MIPHY_SPEED);
437 writeb_relaxed(gen->bias_boost_1, base + MIPHY_BIAS_BOOST_1);
438 writeb_relaxed(gen->bias_boost_2, base + MIPHY_BIAS_BOOST_2);
439
440 /* TX buffer Settings */
441 writeb_relaxed(gen->tx_ctrl_2, base + MIPHY_TX_CTRL_2);
442 writeb_relaxed(gen->tx_ctrl_3, base + MIPHY_TX_CTRL_3);
443
444 /* RX Buffer Settings */
445 writeb_relaxed(gen->rx_buff_ctrl, base + MIPHY_RX_BUFFER_CTRL);
446 writeb_relaxed(gen->rx_vga_gain, base + MIPHY_RX_VGA_GAIN);
447 writeb_relaxed(gen->rx_equ_gain_1, base + MIPHY_RX_EQU_GAIN_1);
448 writeb_relaxed(gen->rx_equ_gain_2, base + MIPHY_RX_EQU_GAIN_2);
449 writeb_relaxed(gen->rx_equ_gain_3, base + MIPHY_RX_EQU_GAIN_3);
450 }
451}
452
453static inline void miphy28lp_pcie_config_gen(struct miphy28lp_phy *miphy_phy)
454{
455 void __iomem *base = miphy_phy->base;
456 int i;
457
458 for (i = 0; i < ARRAY_SIZE(pcie_pll_gen); i++) {
459 struct miphy28lp_pll_gen *gen = &pcie_pll_gen[i];
460
461 /* Banked settings */
462 writeb_relaxed(gen->bank, base + MIPHY_CONF);
463 writeb_relaxed(gen->speed, base + MIPHY_SPEED);
464 writeb_relaxed(gen->bias_boost_1, base + MIPHY_BIAS_BOOST_1);
465 writeb_relaxed(gen->bias_boost_2, base + MIPHY_BIAS_BOOST_2);
466
467 /* TX buffer Settings */
468 writeb_relaxed(gen->tx_ctrl_1, base + MIPHY_TX_CTRL_1);
469 writeb_relaxed(gen->tx_ctrl_2, base + MIPHY_TX_CTRL_2);
470 writeb_relaxed(gen->tx_ctrl_3, base + MIPHY_TX_CTRL_3);
471
472 writeb_relaxed(gen->rx_k_gain, base + MIPHY_RX_K_GAIN);
473
474 /* RX Buffer Settings */
475 writeb_relaxed(gen->rx_buff_ctrl, base + MIPHY_RX_BUFFER_CTRL);
476 writeb_relaxed(gen->rx_vga_gain, base + MIPHY_RX_VGA_GAIN);
477 writeb_relaxed(gen->rx_equ_gain_1, base + MIPHY_RX_EQU_GAIN_1);
478 writeb_relaxed(gen->rx_equ_gain_2, base + MIPHY_RX_EQU_GAIN_2);
479 }
480}
481
482static inline int miphy28lp_wait_compensation(struct miphy28lp_phy *miphy_phy)
483{
484 unsigned long finish = jiffies + 5 * HZ;
485 u8 val;
486
487 /* Waiting for Compensation to complete */
488 do {
489 val = readb_relaxed(miphy_phy->base + MIPHY_COMP_FSM_6);
490
491 if (time_after_eq(jiffies, finish))
492 return -EBUSY;
493 cpu_relax();
494 } while (!(val & COMP_DONE));
495
496 return 0;
497}
498
499
500static inline int miphy28lp_compensation(struct miphy28lp_phy *miphy_phy,
501 struct pll_ratio *pll_ratio)
502{
503 void __iomem *base = miphy_phy->base;
504
505 /* Poll for HFC ready after reset release */
506 /* Compensation measurement */
507 writeb_relaxed(RST_PLL_SW | RST_COMP_SW, base + MIPHY_RESET);
508
509 writeb_relaxed(0x00, base + MIPHY_PLL_COMMON_MISC_2);
510 writeb_relaxed(pll_ratio->clk_ref, base + MIPHY_PLL_CLKREF_FREQ);
511 writeb_relaxed(COMP_START, base + MIPHY_COMP_FSM_1);
512
513 if (miphy_phy->type == PHY_TYPE_PCIE)
514 writeb_relaxed(RST_PLL_SW, base + MIPHY_RESET);
515
516 writeb_relaxed(0x00, base + MIPHY_RESET);
517 writeb_relaxed(START_ACT_FILT, base + MIPHY_PLL_COMMON_MISC_2);
518 writeb_relaxed(SET_NEW_CHANGE, base + MIPHY_PLL_SBR_1);
519
520 /* TX compensation offset to re-center TX impedance */
521 writeb_relaxed(0x00, base + MIPHY_COMP_POSTP);
522
523 if (miphy_phy->type == PHY_TYPE_PCIE)
524 return miphy28lp_wait_compensation(miphy_phy);
525
526 return 0;
527}
528
529static inline void miphy28_usb3_miphy_reset(struct miphy28lp_phy *miphy_phy)
530{
531 void __iomem *base = miphy_phy->base;
532 u8 val;
533
534 /* MIPHY Reset */
535 writeb_relaxed(RST_APPLI_SW, base + MIPHY_CONF_RESET);
536 writeb_relaxed(0x00, base + MIPHY_CONF_RESET);
537 writeb_relaxed(RST_COMP_SW, base + MIPHY_RESET);
538
539 val = RST_COMP_SW | RST_PLL_SW;
540 writeb_relaxed(val, base + MIPHY_RESET);
541
542 writeb_relaxed(0x00, base + MIPHY_PLL_COMMON_MISC_2);
543 writeb_relaxed(0x1e, base + MIPHY_PLL_CLKREF_FREQ);
544 writeb_relaxed(COMP_START, base + MIPHY_COMP_FSM_1);
545 writeb_relaxed(RST_PLL_SW, base + MIPHY_RESET);
546 writeb_relaxed(0x00, base + MIPHY_RESET);
547 writeb_relaxed(START_ACT_FILT, base + MIPHY_PLL_COMMON_MISC_2);
548 writeb_relaxed(0x00, base + MIPHY_CONF);
549 writeb_relaxed(0x00, base + MIPHY_BOUNDARY_1);
550 writeb_relaxed(0x00, base + MIPHY_TST_BIAS_BOOST_2);
551 writeb_relaxed(0x00, base + MIPHY_CONF);
552 writeb_relaxed(SET_NEW_CHANGE, base + MIPHY_PLL_SBR_1);
553 writeb_relaxed(0xa5, base + MIPHY_DEBUG_BUS);
554 writeb_relaxed(0x00, base + MIPHY_CONF);
555}
556
Gabriel FERNANDEZ2b041b272014-11-04 11:51:21 +0100557static void miphy_sata_tune_ssc(struct miphy28lp_phy *miphy_phy)
558{
559 void __iomem *base = miphy_phy->base;
560 u8 val;
561
562 /* Compensate Tx impedance to avoid out of range values */
563 /*
564 * Enable the SSC on PLL for all banks
565 * SSC Modulation @ 31 KHz and 4000 ppm modulation amp
566 */
567 val = readb_relaxed(base + MIPHY_BOUNDARY_2);
568 val |= SSC_EN_SW;
569 writeb_relaxed(val, base + MIPHY_BOUNDARY_2);
570
571 val = readb_relaxed(base + MIPHY_BOUNDARY_SEL);
572 val |= SSC_SEL;
573 writeb_relaxed(val, base + MIPHY_BOUNDARY_SEL);
574
575 for (val = 0; val < MIPHY_SATA_BANK_NB; val++) {
576 writeb_relaxed(val, base + MIPHY_CONF);
577
578 /* Add value to each reference clock cycle */
579 /* and define the period length of the SSC */
580 writeb_relaxed(0x3c, base + MIPHY_PLL_SBR_2);
581 writeb_relaxed(0x6c, base + MIPHY_PLL_SBR_3);
582 writeb_relaxed(0x81, base + MIPHY_PLL_SBR_4);
583
584 /* Clear any previous request */
585 writeb_relaxed(0x00, base + MIPHY_PLL_SBR_1);
586
587 /* requests the PLL to take in account new parameters */
588 writeb_relaxed(SET_NEW_CHANGE, base + MIPHY_PLL_SBR_1);
589
590 /* To be sure there is no other pending requests */
591 writeb_relaxed(0x00, base + MIPHY_PLL_SBR_1);
592 }
593}
594
Gabriel FERNANDEZa2108de2014-11-04 11:51:22 +0100595static void miphy_pcie_tune_ssc(struct miphy28lp_phy *miphy_phy)
596{
597 void __iomem *base = miphy_phy->base;
598 u8 val;
599
600 /* Compensate Tx impedance to avoid out of range values */
601 /*
602 * Enable the SSC on PLL for all banks
603 * SSC Modulation @ 31 KHz and 4000 ppm modulation amp
604 */
605 val = readb_relaxed(base + MIPHY_BOUNDARY_2);
606 val |= SSC_EN_SW;
607 writeb_relaxed(val, base + MIPHY_BOUNDARY_2);
608
609 val = readb_relaxed(base + MIPHY_BOUNDARY_SEL);
610 val |= SSC_SEL;
611 writeb_relaxed(val, base + MIPHY_BOUNDARY_SEL);
612
613 for (val = 0; val < MIPHY_PCIE_BANK_NB; val++) {
614 writeb_relaxed(val, base + MIPHY_CONF);
615
616 /* Validate Step component */
617 writeb_relaxed(0x69, base + MIPHY_PLL_SBR_3);
618 writeb_relaxed(0x21, base + MIPHY_PLL_SBR_4);
619
620 /* Validate Period component */
621 writeb_relaxed(0x3c, base + MIPHY_PLL_SBR_2);
622 writeb_relaxed(0x21, base + MIPHY_PLL_SBR_4);
623
624 /* Clear any previous request */
625 writeb_relaxed(0x00, base + MIPHY_PLL_SBR_1);
626
627 /* requests the PLL to take in account new parameters */
628 writeb_relaxed(SET_NEW_CHANGE, base + MIPHY_PLL_SBR_1);
629
630 /* To be sure there is no other pending requests */
631 writeb_relaxed(0x00, base + MIPHY_PLL_SBR_1);
632 }
633}
634
Gabriel FERNANDEZ2c14e9b2014-11-04 11:51:19 +0100635static inline int miphy28lp_configure_sata(struct miphy28lp_phy *miphy_phy)
636{
637 void __iomem *base = miphy_phy->base;
638 int err;
639 u8 val;
640
641 /* Putting Macro in reset */
642 miphy28lp_set_reset(miphy_phy);
643
644 /* PLL calibration */
645 miphy28lp_pll_calibration(miphy_phy, &sata_pll_ratio);
646
647 /* Banked settings Gen1/Gen2/Gen3 */
648 miphy28lp_sata_config_gen(miphy_phy);
649
650 /* Power control */
651 /* Input bridge enable, manual input bridge control */
652 writeb_relaxed(0x21, base + MIPHY_RX_POWER_CTRL_1);
653
654 /* Macro out of reset */
655 writeb_relaxed(0x00, base + MIPHY_CONF_RESET);
656
657 /* Poll for HFC ready after reset release */
658 /* Compensation measurement */
659 err = miphy28lp_compensation(miphy_phy, &sata_pll_ratio);
660 if (err)
661 return err;
662
663 if (miphy_phy->px_rx_pol_inv) {
664 /* Invert Rx polarity */
665 val = readb_relaxed(miphy_phy->base + MIPHY_CONTROL);
666 val |= PX_RX_POL;
667 writeb_relaxed(val, miphy_phy->base + MIPHY_CONTROL);
668 }
669
Gabriel FERNANDEZ2b041b272014-11-04 11:51:21 +0100670 if (miphy_phy->ssc)
671 miphy_sata_tune_ssc(miphy_phy);
672
Gabriel FERNANDEZ2c14e9b2014-11-04 11:51:19 +0100673 return 0;
674}
675
676static inline int miphy28lp_configure_pcie(struct miphy28lp_phy *miphy_phy)
677{
678 void __iomem *base = miphy_phy->base;
679 int err;
680
681 /* Putting Macro in reset */
682 miphy28lp_set_reset(miphy_phy);
683
684 /* PLL calibration */
685 miphy28lp_pll_calibration(miphy_phy, &pcie_pll_ratio);
686
687 /* Banked settings Gen1/Gen2 */
688 miphy28lp_pcie_config_gen(miphy_phy);
689
690 /* Power control */
691 /* Input bridge enable, manual input bridge control */
692 writeb_relaxed(0x21, base + MIPHY_RX_POWER_CTRL_1);
693
694 /* Macro out of reset */
695 writeb_relaxed(0x00, base + MIPHY_CONF_RESET);
696
697 /* Poll for HFC ready after reset release */
698 /* Compensation measurement */
699 err = miphy28lp_compensation(miphy_phy, &pcie_pll_ratio);
700 if (err)
701 return err;
702
Gabriel FERNANDEZa2108de2014-11-04 11:51:22 +0100703 if (miphy_phy->ssc)
704 miphy_pcie_tune_ssc(miphy_phy);
705
Gabriel FERNANDEZ2c14e9b2014-11-04 11:51:19 +0100706 return 0;
707}
708
709
710static inline void miphy28lp_configure_usb3(struct miphy28lp_phy *miphy_phy)
711{
712 void __iomem *base = miphy_phy->base;
713 u8 val;
714
715 /* Putting Macro in reset */
716 miphy28lp_set_reset(miphy_phy);
717
718 /* PLL calibration */
719 miphy28lp_pll_calibration(miphy_phy, &usb3_pll_ratio);
720
721 /* Writing The Speed Rate */
722 writeb_relaxed(0x00, base + MIPHY_CONF);
723
724 val = RX_SPDSEL_20DEC | TX_SPDSEL_20DEC;
725 writeb_relaxed(val, base + MIPHY_SPEED);
726
727 /* RX Channel compensation and calibration */
728 writeb_relaxed(0x1c, base + MIPHY_RX_LOCK_SETTINGS_OPT);
729 writeb_relaxed(0x51, base + MIPHY_RX_CAL_CTRL_1);
730 writeb_relaxed(0x70, base + MIPHY_RX_CAL_CTRL_2);
731
732 val = OFFSET_COMPENSATION_EN | VGA_OFFSET_POLARITY |
733 CAL_OFFSET_THRESHOLD_64 | CAL_OFFSET_VGA_64;
734 writeb_relaxed(val, base + MIPHY_RX_CAL_OFFSET_CTRL);
735 writeb_relaxed(0x22, base + MIPHY_RX_CAL_VGA_STEP);
736 writeb_relaxed(0x0e, base + MIPHY_RX_CAL_OPT_LENGTH);
737
738 val = EQ_DC_GAIN | VGA_GAIN;
739 writeb_relaxed(val, base + MIPHY_RX_BUFFER_CTRL);
740 writeb_relaxed(0x78, base + MIPHY_RX_EQU_GAIN_1);
741 writeb_relaxed(0x1b, base + MIPHY_SYNCHAR_CONTROL);
742
743 /* TX compensation offset to re-center TX impedance */
744 writeb_relaxed(0x02, base + MIPHY_COMP_POSTP);
745
746 /* Enable GENSEL_SEL and SSC */
747 /* TX_SEL=0 swing preemp forced by pipe registres */
748 val = SSC_SEL | GENSEL_SEL;
749 writeb_relaxed(val, base + MIPHY_BOUNDARY_SEL);
750
751 /* MIPHY Bias boost */
752 writeb_relaxed(0x00, base + MIPHY_BIAS_BOOST_1);
753 writeb_relaxed(0xa7, base + MIPHY_BIAS_BOOST_2);
754
755 /* SSC modulation */
756 writeb_relaxed(SSC_EN_SW, base + MIPHY_BOUNDARY_2);
757
758 /* MIPHY TX control */
759 writeb_relaxed(0x00, base + MIPHY_CONF);
760
761 /* Validate Step component */
762 writeb_relaxed(0x5a, base + MIPHY_PLL_SBR_3);
763 writeb_relaxed(0xa0, base + MIPHY_PLL_SBR_4);
764
765 /* Validate Period component */
766 writeb_relaxed(0x3c, base + MIPHY_PLL_SBR_2);
767 writeb_relaxed(0xa1, base + MIPHY_PLL_SBR_4);
768
769 /* Clear any previous request */
770 writeb_relaxed(0x00, base + MIPHY_PLL_SBR_1);
771
772 /* requests the PLL to take in account new parameters */
773 writeb_relaxed(0x02, base + MIPHY_PLL_SBR_1);
774
775 /* To be sure there is no other pending requests */
776 writeb_relaxed(0x00, base + MIPHY_PLL_SBR_1);
777
778 /* Rx PI controller settings */
779 writeb_relaxed(0xca, base + MIPHY_RX_K_GAIN);
780
781 /* MIPHY RX input bridge control */
782 /* INPUT_BRIDGE_EN_SW=1, manual input bridge control[0]=1 */
783 writeb_relaxed(0x21, base + MIPHY_RX_POWER_CTRL_1);
784 writeb_relaxed(0x29, base + MIPHY_RX_POWER_CTRL_1);
785 writeb_relaxed(0x1a, base + MIPHY_RX_POWER_CTRL_2);
786
787 /* MIPHY Reset for usb3 */
788 miphy28_usb3_miphy_reset(miphy_phy);
789}
790
791static inline int miphy_is_ready(struct miphy28lp_phy *miphy_phy)
792{
793 unsigned long finish = jiffies + 5 * HZ;
794 u8 mask = HFC_PLL | HFC_RDY;
795 u8 val;
796
797 /*
798 * For PCIe and USB3 check only that PLL and HFC are ready
799 * For SATA check also that phy is ready!
800 */
801 if (miphy_phy->type == PHY_TYPE_SATA)
802 mask |= PHY_RDY;
803
804 do {
805 val = readb_relaxed(miphy_phy->base + MIPHY_STATUS_1);
806 if ((val & mask) != mask)
807 cpu_relax();
808 else
809 return 0;
810 } while (!time_after_eq(jiffies, finish));
811
812 return -EBUSY;
813}
814
815static int miphy_osc_is_ready(struct miphy28lp_phy *miphy_phy)
816{
817 struct miphy28lp_dev *miphy_dev = miphy_phy->phydev;
818 unsigned long finish = jiffies + 5 * HZ;
819 u32 val;
820
821 if (!miphy_phy->osc_rdy)
822 return 0;
823
824 if (!miphy_phy->syscfg_miphy_status)
825 return -EINVAL;
826
827 do {
828 regmap_read(miphy_dev->regmap, miphy_phy->syscfg_miphy_status,
829 &val);
830
831 if ((val & MIPHY_OSC_RDY) != MIPHY_OSC_RDY)
832 cpu_relax();
833 else
834 return 0;
835 } while (!time_after_eq(jiffies, finish));
836
837 return -EBUSY;
838}
839
840static int miphy28lp_get_resource_byname(struct device_node *child,
841 char *rname, struct resource *res)
842{
843 int index;
844
845 index = of_property_match_string(child, "reg-names", rname);
846 if (index < 0)
847 return -ENODEV;
848
849 return of_address_to_resource(child, index, res);
850}
851
852static int miphy28lp_get_one_addr(struct device *dev,
853 struct device_node *child, char *rname,
854 void __iomem **base)
855{
856 struct resource res;
857 int ret;
858
859 ret = miphy28lp_get_resource_byname(child, rname, &res);
860 if (!ret) {
861 *base = devm_ioremap(dev, res.start, resource_size(&res));
862 if (!*base) {
863 dev_err(dev, "failed to ioremap %s address region\n"
864 , rname);
865 return -ENOENT;
866 }
867 }
868
869 return 0;
870}
871
872/* MiPHY reset and sysconf setup */
873static int miphy28lp_setup(struct miphy28lp_phy *miphy_phy, u32 miphy_val)
874{
875 int err;
876 struct miphy28lp_dev *miphy_dev = miphy_phy->phydev;
877
878 if (!miphy_phy->syscfg_miphy_ctrl)
879 return -EINVAL;
880
881 err = reset_control_assert(miphy_phy->miphy_rst);
882 if (err) {
883 dev_err(miphy_dev->dev, "unable to bring out of miphy reset\n");
884 return err;
885 }
886
887 if (miphy_phy->osc_force_ext)
888 miphy_val |= MIPHY_OSC_FORCE_EXT;
889
890 regmap_update_bits(miphy_dev->regmap, miphy_phy->syscfg_miphy_ctrl,
891 MIPHY_CTRL_MASK, miphy_val);
892
893 err = reset_control_deassert(miphy_phy->miphy_rst);
894 if (err) {
895 dev_err(miphy_dev->dev, "unable to bring out of miphy reset\n");
896 return err;
897 }
898
899 return miphy_osc_is_ready(miphy_phy);
900}
901
902static int miphy28lp_init_sata(struct miphy28lp_phy *miphy_phy)
903{
904 struct miphy28lp_dev *miphy_dev = miphy_phy->phydev;
905 int err, sata_conf = SATA_CTRL_SELECT_SATA;
906
907 if ((!miphy_phy->syscfg_sata) || (!miphy_phy->syscfg_pci)
908 || (!miphy_phy->base))
909 return -EINVAL;
910
911 dev_info(miphy_dev->dev, "sata-up mode, addr 0x%p\n", miphy_phy->base);
912
913 /* Configure the glue-logic */
914 sata_conf |= ((miphy_phy->sata_gen - SATA_GEN1) << SATA_SPDMODE);
915
916 regmap_update_bits(miphy_dev->regmap, miphy_phy->syscfg_sata,
917 SATA_CTRL_MASK, sata_conf);
918
919 regmap_update_bits(miphy_dev->regmap, miphy_phy->syscfg_pci,
920 PCIE_CTRL_MASK, SATA_CTRL_SELECT_PCIE);
921
922 /* MiPHY path and clocking init */
923 err = miphy28lp_setup(miphy_phy, MIPHY_CTRL_DEFAULT);
924
925 if (err) {
926 dev_err(miphy_dev->dev, "SATA phy setup failed\n");
927 return err;
928 }
929
930 /* initialize miphy */
931 miphy28lp_configure_sata(miphy_phy);
932
933 return miphy_is_ready(miphy_phy);
934}
935
936static int miphy28lp_init_pcie(struct miphy28lp_phy *miphy_phy)
937{
938 struct miphy28lp_dev *miphy_dev = miphy_phy->phydev;
939 int err;
940
941 if ((!miphy_phy->syscfg_sata) || (!miphy_phy->syscfg_pci)
942 || (!miphy_phy->base) || (!miphy_phy->pipebase))
943 return -EINVAL;
944
945 dev_info(miphy_dev->dev, "pcie-up mode, addr 0x%p\n", miphy_phy->base);
946
947 /* Configure the glue-logic */
948 regmap_update_bits(miphy_dev->regmap, miphy_phy->syscfg_sata,
949 SATA_CTRL_MASK, SATA_CTRL_SELECT_PCIE);
950
951 regmap_update_bits(miphy_dev->regmap, miphy_phy->syscfg_pci,
952 PCIE_CTRL_MASK, SYSCFG_PCIE_PCIE_VAL);
953
954 /* MiPHY path and clocking init */
955 err = miphy28lp_setup(miphy_phy, MIPHY_CTRL_DEFAULT);
956
957 if (err) {
958 dev_err(miphy_dev->dev, "PCIe phy setup failed\n");
959 return err;
960 }
961
962 /* initialize miphy */
963 err = miphy28lp_configure_pcie(miphy_phy);
964 if (err)
965 return err;
966
967 /* PIPE Wrapper Configuration */
968 writeb_relaxed(0x68, miphy_phy->pipebase + 0x104); /* Rise_0 */
969 writeb_relaxed(0x61, miphy_phy->pipebase + 0x105); /* Rise_1 */
970 writeb_relaxed(0x68, miphy_phy->pipebase + 0x108); /* Fall_0 */
971 writeb_relaxed(0x61, miphy_phy->pipebase + 0x109); /* Fall-1 */
972 writeb_relaxed(0x68, miphy_phy->pipebase + 0x10c); /* Threshold_0 */
973 writeb_relaxed(0x60, miphy_phy->pipebase + 0x10d); /* Threshold_1 */
974
975 /* Wait for phy_ready */
976 return miphy_is_ready(miphy_phy);
977}
978
979static int miphy28lp_init_usb3(struct miphy28lp_phy *miphy_phy)
980{
981 struct miphy28lp_dev *miphy_dev = miphy_phy->phydev;
982 int err;
983
984 if ((!miphy_phy->base) || (!miphy_phy->pipebase))
985 return -EINVAL;
986
987 dev_info(miphy_dev->dev, "usb3-up mode, addr 0x%p\n", miphy_phy->base);
988
989 /* MiPHY path and clocking init */
990 err = miphy28lp_setup(miphy_phy, MIPHY_CTRL_SYNC_D_EN);
991 if (err) {
992 dev_err(miphy_dev->dev, "USB3 phy setup failed\n");
993 return err;
994 }
995
996 /* initialize miphy */
997 miphy28lp_configure_usb3(miphy_phy);
998
999 /* PIPE Wrapper Configuration */
1000 writeb_relaxed(0x68, miphy_phy->pipebase + 0x23);
1001 writeb_relaxed(0x61, miphy_phy->pipebase + 0x24);
1002 writeb_relaxed(0x68, miphy_phy->pipebase + 0x26);
1003 writeb_relaxed(0x61, miphy_phy->pipebase + 0x27);
1004 writeb_relaxed(0x18, miphy_phy->pipebase + 0x29);
1005 writeb_relaxed(0x61, miphy_phy->pipebase + 0x2a);
1006
1007 /* pipe Wrapper usb3 TX swing de-emph margin PREEMPH[7:4], SWING[3:0] */
1008 writeb_relaxed(0X67, miphy_phy->pipebase + 0x68);
1009 writeb_relaxed(0x0d, miphy_phy->pipebase + 0x69);
1010 writeb_relaxed(0X67, miphy_phy->pipebase + 0x6a);
1011 writeb_relaxed(0X0d, miphy_phy->pipebase + 0x6b);
1012 writeb_relaxed(0X67, miphy_phy->pipebase + 0x6c);
1013 writeb_relaxed(0X0d, miphy_phy->pipebase + 0x6d);
1014 writeb_relaxed(0X67, miphy_phy->pipebase + 0x6e);
1015 writeb_relaxed(0X0d, miphy_phy->pipebase + 0x6f);
1016
1017 return miphy_is_ready(miphy_phy);
1018}
1019
1020static int miphy28lp_init(struct phy *phy)
1021{
1022 struct miphy28lp_phy *miphy_phy = phy_get_drvdata(phy);
1023 struct miphy28lp_dev *miphy_dev = miphy_phy->phydev;
1024 int ret;
1025
1026 mutex_lock(&miphy_dev->miphy_mutex);
1027
1028 switch (miphy_phy->type) {
1029
1030 case PHY_TYPE_SATA:
1031 ret = miphy28lp_init_sata(miphy_phy);
1032 break;
1033 case PHY_TYPE_PCIE:
1034 ret = miphy28lp_init_pcie(miphy_phy);
1035 break;
1036 case PHY_TYPE_USB3:
1037 ret = miphy28lp_init_usb3(miphy_phy);
1038 break;
1039 default:
1040 return -EINVAL;
1041 }
1042
1043 mutex_unlock(&miphy_dev->miphy_mutex);
1044
1045 return ret;
1046}
1047
1048static int miphy28lp_get_addr(struct miphy28lp_phy *miphy_phy)
1049{
1050 struct miphy28lp_dev *miphy_dev = miphy_phy->phydev;
1051 struct device_node *phynode = miphy_phy->phy->dev.of_node;
1052 int err;
1053
1054 if ((miphy_phy->type != PHY_TYPE_SATA) &&
1055 (miphy_phy->type != PHY_TYPE_PCIE) &&
1056 (miphy_phy->type != PHY_TYPE_USB3)) {
1057 return -EINVAL;
1058 }
1059
1060 err = miphy28lp_get_one_addr(miphy_dev->dev, phynode,
1061 PHY_TYPE_name[miphy_phy->type - PHY_TYPE_SATA],
1062 &miphy_phy->base);
1063 if (err)
1064 return err;
1065
1066 if ((miphy_phy->type == PHY_TYPE_PCIE) ||
1067 (miphy_phy->type == PHY_TYPE_USB3)) {
1068 err = miphy28lp_get_one_addr(miphy_dev->dev, phynode, "pipew",
1069 &miphy_phy->pipebase);
1070 if (err)
1071 return err;
1072 }
1073
1074 return 0;
1075}
1076
1077static struct phy *miphy28lp_xlate(struct device *dev,
1078 struct of_phandle_args *args)
1079{
1080 struct miphy28lp_dev *miphy_dev = dev_get_drvdata(dev);
1081 struct miphy28lp_phy *miphy_phy = NULL;
1082 struct device_node *phynode = args->np;
1083 int ret, index = 0;
1084
1085 if (!of_device_is_available(phynode)) {
1086 dev_warn(dev, "Requested PHY is disabled\n");
1087 return ERR_PTR(-ENODEV);
1088 }
1089
1090 if (args->args_count != 1) {
1091 dev_err(dev, "Invalid number of cells in 'phy' property\n");
1092 return ERR_PTR(-EINVAL);
1093 }
1094
1095 for (index = 0; index < of_get_child_count(dev->of_node); index++)
1096 if (phynode == miphy_dev->phys[index]->phy->dev.of_node) {
1097 miphy_phy = miphy_dev->phys[index];
1098 break;
1099 }
1100
1101 if (!miphy_phy) {
1102 dev_err(dev, "Failed to find appropriate phy\n");
1103 return ERR_PTR(-EINVAL);
1104 }
1105
1106 miphy_phy->type = args->args[0];
1107
1108 ret = miphy28lp_get_addr(miphy_phy);
1109 if (ret < 0)
1110 return ERR_PTR(ret);
1111
1112 return miphy_phy->phy;
1113}
1114
1115static struct phy_ops miphy28lp_ops = {
1116 .init = miphy28lp_init,
1117};
1118
1119static int miphy28lp_probe_resets(struct device_node *node,
1120 struct miphy28lp_phy *miphy_phy)
1121{
1122 struct miphy28lp_dev *miphy_dev = miphy_phy->phydev;
1123 int err;
1124
1125 miphy_phy->miphy_rst = of_reset_control_get(node, "miphy-sw-rst");
1126
1127 if (IS_ERR(miphy_phy->miphy_rst)) {
1128 dev_err(miphy_dev->dev,
1129 "miphy soft reset control not defined\n");
1130 return PTR_ERR(miphy_phy->miphy_rst);
1131 }
1132
1133 err = reset_control_deassert(miphy_phy->miphy_rst);
1134 if (err) {
1135 dev_err(miphy_dev->dev, "unable to bring out of miphy reset\n");
1136 return err;
1137 }
1138
1139 return 0;
1140}
1141
1142static int miphy28lp_of_probe(struct device_node *np,
1143 struct miphy28lp_phy *miphy_phy)
1144{
1145 struct resource res;
1146
1147 miphy_phy->osc_force_ext =
1148 of_property_read_bool(np, "st,osc-force-ext");
1149
1150 miphy_phy->osc_rdy = of_property_read_bool(np, "st,osc-rdy");
1151
1152 miphy_phy->px_rx_pol_inv =
1153 of_property_read_bool(np, "st,px_rx_pol_inv");
1154
Gabriel FERNANDEZ2b041b272014-11-04 11:51:21 +01001155 miphy_phy->ssc = of_property_read_bool(np, "st,ssc-on");
1156
Gabriel FERNANDEZ2c14e9b2014-11-04 11:51:19 +01001157 of_property_read_u32(np, "st,sata-gen", &miphy_phy->sata_gen);
1158 if (!miphy_phy->sata_gen)
1159 miphy_phy->sata_gen = SATA_GEN1;
1160
1161 if (!miphy28lp_get_resource_byname(np, "miphy-ctrl-glue", &res))
1162 miphy_phy->syscfg_miphy_ctrl = res.start;
1163
1164 if (!miphy28lp_get_resource_byname(np, "miphy-status-glue", &res))
1165 miphy_phy->syscfg_miphy_status = res.start;
1166
1167 if (!miphy28lp_get_resource_byname(np, "pcie-glue", &res))
1168 miphy_phy->syscfg_pci = res.start;
1169
1170 if (!miphy28lp_get_resource_byname(np, "sata-glue", &res))
1171 miphy_phy->syscfg_sata = res.start;
1172
1173
1174 return 0;
1175}
1176
1177static int miphy28lp_probe(struct platform_device *pdev)
1178{
1179 struct device_node *child, *np = pdev->dev.of_node;
1180 struct miphy28lp_dev *miphy_dev;
1181 struct phy_provider *provider;
1182 struct phy *phy;
1183 int chancount, port = 0;
1184 int ret;
1185
1186 miphy_dev = devm_kzalloc(&pdev->dev, sizeof(*miphy_dev), GFP_KERNEL);
1187 if (!miphy_dev)
1188 return -ENOMEM;
1189
1190 chancount = of_get_child_count(np);
1191 miphy_dev->phys = devm_kzalloc(&pdev->dev, sizeof(phy) * chancount,
1192 GFP_KERNEL);
1193 if (!miphy_dev->phys)
1194 return -ENOMEM;
1195
1196 miphy_dev->regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
1197 if (IS_ERR(miphy_dev->regmap)) {
1198 dev_err(miphy_dev->dev, "No syscfg phandle specified\n");
1199 return PTR_ERR(miphy_dev->regmap);
1200 }
1201
1202 miphy_dev->dev = &pdev->dev;
1203
1204 dev_set_drvdata(&pdev->dev, miphy_dev);
1205
1206 mutex_init(&miphy_dev->miphy_mutex);
1207
1208 for_each_child_of_node(np, child) {
1209 struct miphy28lp_phy *miphy_phy;
1210
1211 miphy_phy = devm_kzalloc(&pdev->dev, sizeof(*miphy_phy),
1212 GFP_KERNEL);
1213 if (!miphy_phy)
1214 return -ENOMEM;
1215
1216 miphy_dev->phys[port] = miphy_phy;
1217
1218 phy = devm_phy_create(&pdev->dev, child, &miphy28lp_ops, NULL);
1219 if (IS_ERR(phy)) {
1220 dev_err(&pdev->dev, "failed to create PHY\n");
1221 return PTR_ERR(phy);
1222 }
1223
1224 miphy_dev->phys[port]->phy = phy;
1225 miphy_dev->phys[port]->phydev = miphy_dev;
1226
1227 ret = miphy28lp_of_probe(child, miphy_phy);
1228 if (ret)
1229 return ret;
1230
1231 ret = miphy28lp_probe_resets(child, miphy_dev->phys[port]);
1232 if (ret)
1233 return ret;
1234
1235 phy_set_drvdata(phy, miphy_dev->phys[port]);
1236 port++;
1237
1238 }
1239
1240 provider = devm_of_phy_provider_register(&pdev->dev, miphy28lp_xlate);
1241 if (IS_ERR(provider))
1242 return PTR_ERR(provider);
1243
1244 return 0;
1245}
1246
1247static const struct of_device_id miphy28lp_of_match[] = {
1248 {.compatible = "st,miphy28lp-phy", },
1249 {},
1250};
1251
1252MODULE_DEVICE_TABLE(of, miphy28lp_of_match);
1253
1254static struct platform_driver miphy28lp_driver = {
1255 .probe = miphy28lp_probe,
1256 .driver = {
1257 .name = "miphy28lp-phy",
1258 .owner = THIS_MODULE,
1259 .of_match_table = miphy28lp_of_match,
1260 }
1261};
1262
1263module_platform_driver(miphy28lp_driver);
1264
1265MODULE_AUTHOR("Alexandre Torgue <alexandre.torgue@st.com>");
1266MODULE_DESCRIPTION("STMicroelectronics miphy28lp driver");
1267MODULE_LICENSE("GPL v2");