OSDN Git Service

7d592e6392aca0e85e41c05ae9c4b8a18831202f
[uclinux-h8/linux.git] / drivers / phy / phy-miphy28lp.c
1 /*
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
194 struct miphy28lp_phy {
195         struct phy *phy;
196         struct miphy28lp_dev *phydev;
197         void __iomem *base;
198         void __iomem *pipebase;
199
200         bool osc_force_ext;
201         bool osc_rdy;
202         bool px_rx_pol_inv;
203
204         struct reset_control *miphy_rst;
205
206         u32 sata_gen;
207
208         /* Sysconfig registers offsets needed to configure the device */
209         u32 syscfg_miphy_ctrl;
210         u32 syscfg_miphy_status;
211         u32 syscfg_pci;
212         u32 syscfg_sata;
213         u8 type;
214 };
215
216 struct miphy28lp_dev {
217         struct device *dev;
218         struct regmap *regmap;
219         struct mutex miphy_mutex;
220         struct miphy28lp_phy **phys;
221 };
222
223 struct miphy_initval {
224         u16 reg;
225         u16 val;
226 };
227
228 enum miphy_sata_gen { SATA_GEN1, SATA_GEN2, SATA_GEN3 };
229
230 static char *PHY_TYPE_name[] = { "sata-up", "pcie-up", "", "usb3-up" };
231
232 struct pll_ratio {
233         int clk_ref;
234         int calset_1;
235         int calset_2;
236         int calset_3;
237         int calset_4;
238         int cal_ctrl;
239 };
240
241 static struct pll_ratio sata_pll_ratio = {
242         .clk_ref = 0x1e,
243         .calset_1 = 0xc8,
244         .calset_2 = 0x00,
245         .calset_3 = 0x00,
246         .calset_4 = 0x00,
247         .cal_ctrl = 0x00,
248 };
249
250 static struct pll_ratio pcie_pll_ratio = {
251         .clk_ref = 0x1e,
252         .calset_1 = 0xa6,
253         .calset_2 = 0xaa,
254         .calset_3 = 0xaa,
255         .calset_4 = 0x00,
256         .cal_ctrl = 0x00,
257 };
258
259 static struct pll_ratio usb3_pll_ratio = {
260         .clk_ref = 0x1e,
261         .calset_1 = 0xa6,
262         .calset_2 = 0xaa,
263         .calset_3 = 0xaa,
264         .calset_4 = 0x04,
265         .cal_ctrl = 0x00,
266 };
267
268 struct miphy28lp_pll_gen {
269         int bank;
270         int speed;
271         int bias_boost_1;
272         int bias_boost_2;
273         int tx_ctrl_1;
274         int tx_ctrl_2;
275         int tx_ctrl_3;
276         int rx_k_gain;
277         int rx_vga_gain;
278         int rx_equ_gain_1;
279         int rx_equ_gain_2;
280         int rx_equ_gain_3;
281         int rx_buff_ctrl;
282 };
283
284 static struct miphy28lp_pll_gen sata_pll_gen[] = {
285         {
286                 .bank           = 0x00,
287                 .speed          = TX_SPDSEL_80DEC | RX_SPDSEL_80DEC,
288                 .bias_boost_1   = 0x00,
289                 .bias_boost_2   = 0xae,
290                 .tx_ctrl_2      = 0x53,
291                 .tx_ctrl_3      = 0x00,
292                 .rx_buff_ctrl   = EQ_BOOST_GAIN | EQ_DC_GAIN | VGA_GAIN,
293                 .rx_vga_gain    = 0x00,
294                 .rx_equ_gain_1  = 0x7d,
295                 .rx_equ_gain_2  = 0x56,
296                 .rx_equ_gain_3  = 0x00,
297         },
298         {
299                 .bank           = 0x01,
300                 .speed          = TX_SPDSEL_40DEC | RX_SPDSEL_40DEC,
301                 .bias_boost_1   = 0x00,
302                 .bias_boost_2   = 0xae,
303                 .tx_ctrl_2      = 0x72,
304                 .tx_ctrl_3      = 0x20,
305                 .rx_buff_ctrl   = EQ_BOOST_GAIN | EQ_DC_GAIN | VGA_GAIN,
306                 .rx_vga_gain    = 0x00,
307                 .rx_equ_gain_1  = 0x7d,
308                 .rx_equ_gain_2  = 0x56,
309                 .rx_equ_gain_3  = 0x00,
310         },
311         {
312                 .bank           = 0x02,
313                 .speed          = TX_SPDSEL_20DEC | RX_SPDSEL_20DEC,
314                 .bias_boost_1   = 0x00,
315                 .bias_boost_2   = 0xae,
316                 .tx_ctrl_2      = 0xc0,
317                 .tx_ctrl_3      = 0x20,
318                 .rx_buff_ctrl   = EQ_BOOST_GAIN | EQ_DC_GAIN | VGA_GAIN,
319                 .rx_vga_gain    = 0x00,
320                 .rx_equ_gain_1  = 0x7d,
321                 .rx_equ_gain_2  = 0x56,
322                 .rx_equ_gain_3  = 0x00,
323         },
324 };
325
326 static struct miphy28lp_pll_gen pcie_pll_gen[] = {
327         {
328                 .bank           = 0x00,
329                 .speed          = TX_SPDSEL_40DEC | RX_SPDSEL_40DEC,
330                 .bias_boost_1   = 0x00,
331                 .bias_boost_2   = 0xa5,
332                 .tx_ctrl_1      = TX_REG_STEP_N_25MV,
333                 .tx_ctrl_2      = 0x71,
334                 .tx_ctrl_3      = 0x60,
335                 .rx_k_gain      = 0x98,
336                 .rx_buff_ctrl   = EQ_BOOST_GAIN | EQ_DC_GAIN | VGA_GAIN,
337                 .rx_vga_gain    = 0x00,
338                 .rx_equ_gain_1  = 0x79,
339                 .rx_equ_gain_2  = 0x56,
340         },
341         {
342                 .bank           = 0x01,
343                 .speed          = TX_SPDSEL_20DEC | RX_SPDSEL_20DEC,
344                 .bias_boost_1   = 0x00,
345                 .bias_boost_2   = 0xa5,
346                 .tx_ctrl_1      = TX_REG_STEP_N_25MV,
347                 .tx_ctrl_2      = 0x70,
348                 .tx_ctrl_3      = 0x60,
349                 .rx_k_gain      = 0xcc,
350                 .rx_buff_ctrl   = EQ_BOOST_GAIN | EQ_DC_GAIN | VGA_GAIN,
351                 .rx_vga_gain    = 0x00,
352                 .rx_equ_gain_1  = 0x78,
353                 .rx_equ_gain_2  = 0x07,
354         },
355 };
356
357 static inline void miphy28lp_set_reset(struct miphy28lp_phy *miphy_phy)
358 {
359         void *base = miphy_phy->base;
360         u8 val;
361
362         /* Putting Macro in reset */
363         writeb_relaxed(RST_APPLI_SW, base + MIPHY_CONF_RESET);
364
365         val = RST_APPLI_SW | RST_CONF_SW;
366         writeb_relaxed(val, base + MIPHY_CONF_RESET);
367
368         writeb_relaxed(RST_APPLI_SW, base + MIPHY_CONF_RESET);
369
370         /* Bringing the MIPHY-CPU registers out of reset */
371         if (miphy_phy->type == PHY_TYPE_PCIE) {
372                 val = AUTO_RST_RX | TERM_EN_SW;
373                 writeb_relaxed(val, base + MIPHY_CONTROL);
374         } else {
375                 val = AUTO_RST_RX | TERM_EN_SW | DIS_LINK_RST;
376                 writeb_relaxed(val, base + MIPHY_CONTROL);
377         }
378 }
379
380 static inline void miphy28lp_pll_calibration(struct miphy28lp_phy *miphy_phy,
381                 struct pll_ratio *pll_ratio)
382 {
383         void *base = miphy_phy->base;
384         u8 val;
385
386         /* Applying PLL Settings */
387         writeb_relaxed(0x1d, base + MIPHY_PLL_SPAREIN);
388         writeb_relaxed(pll_ratio->clk_ref, base + MIPHY_PLL_CLKREF_FREQ);
389
390         /* PLL Ratio */
391         writeb_relaxed(pll_ratio->calset_1, base + MIPHY_PLL_CALSET_1);
392         writeb_relaxed(pll_ratio->calset_2, base + MIPHY_PLL_CALSET_2);
393         writeb_relaxed(pll_ratio->calset_3, base + MIPHY_PLL_CALSET_3);
394         writeb_relaxed(pll_ratio->calset_4, base + MIPHY_PLL_CALSET_4);
395         writeb_relaxed(pll_ratio->cal_ctrl, base + MIPHY_PLL_CALSET_CTRL);
396
397         writeb_relaxed(TX_SEL, base + MIPHY_BOUNDARY_SEL);
398
399         val = (0x68 << 1) | TX_SLEW_CAL_MAN_EN;
400         writeb_relaxed(val, base + MIPHY_TX_CAL_MAN);
401
402         val = VGA_OFFSET_POLARITY | CAL_OFFSET_THRESHOLD_64 | CAL_OFFSET_VGA_64;
403
404         if (miphy_phy->type != PHY_TYPE_SATA)
405                 val |= OFFSET_COMPENSATION_EN;
406
407         writeb_relaxed(val, base + MIPHY_RX_CAL_OFFSET_CTRL);
408
409         if (miphy_phy->type == PHY_TYPE_USB3) {
410                 writeb_relaxed(0x00, base + MIPHY_CONF);
411                 writeb_relaxed(0x70, base + MIPHY_RX_LOCK_STEP);
412                 writeb_relaxed(EN_FIRST_HALF, base + MIPHY_RX_SIGDET_SLEEP_OA);
413                 writeb_relaxed(EN_FIRST_HALF, base + MIPHY_RX_SIGDET_SLEEP_SEL);
414                 writeb_relaxed(EN_FIRST_HALF, base + MIPHY_RX_SIGDET_WAIT_SEL);
415
416                 val = EN_DIGIT_SIGNAL_CHECK | EN_FIRST_HALF;
417                 writeb_relaxed(val, base + MIPHY_RX_SIGDET_DATA_SEL);
418         }
419
420 }
421
422 static inline void miphy28lp_sata_config_gen(struct miphy28lp_phy *miphy_phy)
423 {
424         void __iomem *base = miphy_phy->base;
425         int i;
426
427         for (i = 0; i < ARRAY_SIZE(sata_pll_gen); i++) {
428                 struct miphy28lp_pll_gen *gen = &sata_pll_gen[i];
429
430                 /* Banked settings */
431                 writeb_relaxed(gen->bank, base + MIPHY_CONF);
432                 writeb_relaxed(gen->speed, base + MIPHY_SPEED);
433                 writeb_relaxed(gen->bias_boost_1, base + MIPHY_BIAS_BOOST_1);
434                 writeb_relaxed(gen->bias_boost_2, base + MIPHY_BIAS_BOOST_2);
435
436                 /* TX buffer Settings */
437                 writeb_relaxed(gen->tx_ctrl_2, base + MIPHY_TX_CTRL_2);
438                 writeb_relaxed(gen->tx_ctrl_3, base + MIPHY_TX_CTRL_3);
439
440                 /* RX Buffer Settings */
441                 writeb_relaxed(gen->rx_buff_ctrl, base + MIPHY_RX_BUFFER_CTRL);
442                 writeb_relaxed(gen->rx_vga_gain, base + MIPHY_RX_VGA_GAIN);
443                 writeb_relaxed(gen->rx_equ_gain_1, base + MIPHY_RX_EQU_GAIN_1);
444                 writeb_relaxed(gen->rx_equ_gain_2, base + MIPHY_RX_EQU_GAIN_2);
445                 writeb_relaxed(gen->rx_equ_gain_3, base + MIPHY_RX_EQU_GAIN_3);
446         }
447 }
448
449 static inline void miphy28lp_pcie_config_gen(struct miphy28lp_phy *miphy_phy)
450 {
451         void __iomem *base = miphy_phy->base;
452         int i;
453
454         for (i = 0; i < ARRAY_SIZE(pcie_pll_gen); i++) {
455                 struct miphy28lp_pll_gen *gen = &pcie_pll_gen[i];
456
457                 /* Banked settings */
458                 writeb_relaxed(gen->bank, base + MIPHY_CONF);
459                 writeb_relaxed(gen->speed, base + MIPHY_SPEED);
460                 writeb_relaxed(gen->bias_boost_1, base + MIPHY_BIAS_BOOST_1);
461                 writeb_relaxed(gen->bias_boost_2, base + MIPHY_BIAS_BOOST_2);
462
463                 /* TX buffer Settings */
464                 writeb_relaxed(gen->tx_ctrl_1, base + MIPHY_TX_CTRL_1);
465                 writeb_relaxed(gen->tx_ctrl_2, base + MIPHY_TX_CTRL_2);
466                 writeb_relaxed(gen->tx_ctrl_3, base + MIPHY_TX_CTRL_3);
467
468                 writeb_relaxed(gen->rx_k_gain, base + MIPHY_RX_K_GAIN);
469
470                 /* RX Buffer Settings */
471                 writeb_relaxed(gen->rx_buff_ctrl, base + MIPHY_RX_BUFFER_CTRL);
472                 writeb_relaxed(gen->rx_vga_gain, base + MIPHY_RX_VGA_GAIN);
473                 writeb_relaxed(gen->rx_equ_gain_1, base + MIPHY_RX_EQU_GAIN_1);
474                 writeb_relaxed(gen->rx_equ_gain_2, base + MIPHY_RX_EQU_GAIN_2);
475         }
476 }
477
478 static inline int miphy28lp_wait_compensation(struct miphy28lp_phy *miphy_phy)
479 {
480         unsigned long finish = jiffies + 5 * HZ;
481         u8 val;
482
483         /* Waiting for Compensation to complete */
484         do {
485                 val = readb_relaxed(miphy_phy->base + MIPHY_COMP_FSM_6);
486
487                 if (time_after_eq(jiffies, finish))
488                         return -EBUSY;
489                 cpu_relax();
490         } while (!(val & COMP_DONE));
491
492         return 0;
493 }
494
495
496 static inline int miphy28lp_compensation(struct miphy28lp_phy *miphy_phy,
497                 struct pll_ratio *pll_ratio)
498 {
499         void __iomem *base = miphy_phy->base;
500
501         /* Poll for HFC ready after reset release */
502         /* Compensation measurement */
503         writeb_relaxed(RST_PLL_SW | RST_COMP_SW, base + MIPHY_RESET);
504
505         writeb_relaxed(0x00, base + MIPHY_PLL_COMMON_MISC_2);
506         writeb_relaxed(pll_ratio->clk_ref, base + MIPHY_PLL_CLKREF_FREQ);
507         writeb_relaxed(COMP_START, base + MIPHY_COMP_FSM_1);
508
509         if (miphy_phy->type == PHY_TYPE_PCIE)
510                 writeb_relaxed(RST_PLL_SW, base + MIPHY_RESET);
511
512         writeb_relaxed(0x00, base + MIPHY_RESET);
513         writeb_relaxed(START_ACT_FILT, base + MIPHY_PLL_COMMON_MISC_2);
514         writeb_relaxed(SET_NEW_CHANGE, base + MIPHY_PLL_SBR_1);
515
516         /* TX compensation offset to re-center TX impedance */
517         writeb_relaxed(0x00, base + MIPHY_COMP_POSTP);
518
519         if (miphy_phy->type == PHY_TYPE_PCIE)
520                 return miphy28lp_wait_compensation(miphy_phy);
521
522         return 0;
523 }
524
525 static inline void miphy28_usb3_miphy_reset(struct miphy28lp_phy *miphy_phy)
526 {
527         void __iomem *base = miphy_phy->base;
528         u8 val;
529
530         /* MIPHY Reset */
531         writeb_relaxed(RST_APPLI_SW, base + MIPHY_CONF_RESET);
532         writeb_relaxed(0x00, base + MIPHY_CONF_RESET);
533         writeb_relaxed(RST_COMP_SW, base + MIPHY_RESET);
534
535         val = RST_COMP_SW | RST_PLL_SW;
536         writeb_relaxed(val, base + MIPHY_RESET);
537
538         writeb_relaxed(0x00, base + MIPHY_PLL_COMMON_MISC_2);
539         writeb_relaxed(0x1e, base + MIPHY_PLL_CLKREF_FREQ);
540         writeb_relaxed(COMP_START, base + MIPHY_COMP_FSM_1);
541         writeb_relaxed(RST_PLL_SW, base + MIPHY_RESET);
542         writeb_relaxed(0x00, base + MIPHY_RESET);
543         writeb_relaxed(START_ACT_FILT, base + MIPHY_PLL_COMMON_MISC_2);
544         writeb_relaxed(0x00, base + MIPHY_CONF);
545         writeb_relaxed(0x00, base + MIPHY_BOUNDARY_1);
546         writeb_relaxed(0x00, base + MIPHY_TST_BIAS_BOOST_2);
547         writeb_relaxed(0x00, base + MIPHY_CONF);
548         writeb_relaxed(SET_NEW_CHANGE, base + MIPHY_PLL_SBR_1);
549         writeb_relaxed(0xa5, base + MIPHY_DEBUG_BUS);
550         writeb_relaxed(0x00, base + MIPHY_CONF);
551 }
552
553 static inline int miphy28lp_configure_sata(struct miphy28lp_phy *miphy_phy)
554 {
555         void __iomem *base = miphy_phy->base;
556         int err;
557         u8 val;
558
559         /* Putting Macro in reset */
560         miphy28lp_set_reset(miphy_phy);
561
562         /* PLL calibration */
563         miphy28lp_pll_calibration(miphy_phy, &sata_pll_ratio);
564
565         /* Banked settings Gen1/Gen2/Gen3 */
566         miphy28lp_sata_config_gen(miphy_phy);
567
568         /* Power control */
569         /* Input bridge enable, manual input bridge control */
570         writeb_relaxed(0x21, base + MIPHY_RX_POWER_CTRL_1);
571
572         /* Macro out of reset */
573         writeb_relaxed(0x00, base + MIPHY_CONF_RESET);
574
575         /* Poll for HFC ready after reset release */
576         /* Compensation measurement */
577         err = miphy28lp_compensation(miphy_phy, &sata_pll_ratio);
578         if (err)
579                 return err;
580
581         if (miphy_phy->px_rx_pol_inv) {
582                 /* Invert Rx polarity */
583                 val = readb_relaxed(miphy_phy->base + MIPHY_CONTROL);
584                 val |= PX_RX_POL;
585                 writeb_relaxed(val, miphy_phy->base + MIPHY_CONTROL);
586         }
587
588         return 0;
589 }
590
591 static inline int miphy28lp_configure_pcie(struct miphy28lp_phy *miphy_phy)
592 {
593         void __iomem *base = miphy_phy->base;
594         int err;
595
596         /* Putting Macro in reset */
597         miphy28lp_set_reset(miphy_phy);
598
599         /* PLL calibration */
600         miphy28lp_pll_calibration(miphy_phy, &pcie_pll_ratio);
601
602         /* Banked settings Gen1/Gen2 */
603         miphy28lp_pcie_config_gen(miphy_phy);
604
605         /* Power control */
606         /* Input bridge enable, manual input bridge control */
607         writeb_relaxed(0x21, base + MIPHY_RX_POWER_CTRL_1);
608
609         /* Macro out of reset */
610         writeb_relaxed(0x00, base + MIPHY_CONF_RESET);
611
612         /* Poll for HFC ready after reset release */
613         /* Compensation measurement */
614         err = miphy28lp_compensation(miphy_phy, &pcie_pll_ratio);
615         if (err)
616                 return err;
617
618         return 0;
619 }
620
621
622 static inline void miphy28lp_configure_usb3(struct miphy28lp_phy *miphy_phy)
623 {
624         void __iomem *base = miphy_phy->base;
625         u8 val;
626
627         /* Putting Macro in reset */
628         miphy28lp_set_reset(miphy_phy);
629
630         /* PLL calibration */
631         miphy28lp_pll_calibration(miphy_phy, &usb3_pll_ratio);
632
633         /* Writing The Speed Rate */
634         writeb_relaxed(0x00, base + MIPHY_CONF);
635
636         val = RX_SPDSEL_20DEC | TX_SPDSEL_20DEC;
637         writeb_relaxed(val, base + MIPHY_SPEED);
638
639         /* RX Channel compensation and calibration */
640         writeb_relaxed(0x1c, base + MIPHY_RX_LOCK_SETTINGS_OPT);
641         writeb_relaxed(0x51, base + MIPHY_RX_CAL_CTRL_1);
642         writeb_relaxed(0x70, base + MIPHY_RX_CAL_CTRL_2);
643
644         val = OFFSET_COMPENSATION_EN | VGA_OFFSET_POLARITY |
645               CAL_OFFSET_THRESHOLD_64 | CAL_OFFSET_VGA_64;
646         writeb_relaxed(val, base + MIPHY_RX_CAL_OFFSET_CTRL);
647         writeb_relaxed(0x22, base + MIPHY_RX_CAL_VGA_STEP);
648         writeb_relaxed(0x0e, base + MIPHY_RX_CAL_OPT_LENGTH);
649
650         val = EQ_DC_GAIN | VGA_GAIN;
651         writeb_relaxed(val, base + MIPHY_RX_BUFFER_CTRL);
652         writeb_relaxed(0x78, base + MIPHY_RX_EQU_GAIN_1);
653         writeb_relaxed(0x1b, base + MIPHY_SYNCHAR_CONTROL);
654
655         /* TX compensation offset to re-center TX impedance */
656         writeb_relaxed(0x02, base + MIPHY_COMP_POSTP);
657
658         /* Enable GENSEL_SEL and SSC */
659         /* TX_SEL=0 swing preemp forced by pipe registres */
660         val = SSC_SEL | GENSEL_SEL;
661         writeb_relaxed(val, base + MIPHY_BOUNDARY_SEL);
662
663         /* MIPHY Bias boost */
664         writeb_relaxed(0x00, base + MIPHY_BIAS_BOOST_1);
665         writeb_relaxed(0xa7, base + MIPHY_BIAS_BOOST_2);
666
667         /* SSC modulation */
668         writeb_relaxed(SSC_EN_SW, base + MIPHY_BOUNDARY_2);
669
670         /* MIPHY TX control */
671         writeb_relaxed(0x00, base + MIPHY_CONF);
672
673         /* Validate Step component */
674         writeb_relaxed(0x5a, base + MIPHY_PLL_SBR_3);
675         writeb_relaxed(0xa0, base + MIPHY_PLL_SBR_4);
676
677         /* Validate Period component */
678         writeb_relaxed(0x3c, base + MIPHY_PLL_SBR_2);
679         writeb_relaxed(0xa1, base + MIPHY_PLL_SBR_4);
680
681         /* Clear any previous request */
682         writeb_relaxed(0x00, base + MIPHY_PLL_SBR_1);
683
684         /* requests the PLL to take in account new parameters */
685         writeb_relaxed(0x02, base + MIPHY_PLL_SBR_1);
686
687         /* To be sure there is no other pending requests */
688         writeb_relaxed(0x00, base + MIPHY_PLL_SBR_1);
689
690         /* Rx PI controller settings */
691         writeb_relaxed(0xca, base + MIPHY_RX_K_GAIN);
692
693         /* MIPHY RX input bridge control */
694         /* INPUT_BRIDGE_EN_SW=1, manual input bridge control[0]=1 */
695         writeb_relaxed(0x21, base + MIPHY_RX_POWER_CTRL_1);
696         writeb_relaxed(0x29, base + MIPHY_RX_POWER_CTRL_1);
697         writeb_relaxed(0x1a, base + MIPHY_RX_POWER_CTRL_2);
698
699         /* MIPHY Reset for usb3 */
700         miphy28_usb3_miphy_reset(miphy_phy);
701 }
702
703 static inline int miphy_is_ready(struct miphy28lp_phy *miphy_phy)
704 {
705         unsigned long finish = jiffies + 5 * HZ;
706         u8 mask = HFC_PLL | HFC_RDY;
707         u8 val;
708
709         /*
710          * For PCIe and USB3 check only that PLL and HFC are ready
711          * For SATA check also that phy is ready!
712          */
713         if (miphy_phy->type == PHY_TYPE_SATA)
714                 mask |= PHY_RDY;
715
716         do {
717                 val = readb_relaxed(miphy_phy->base + MIPHY_STATUS_1);
718                 if ((val & mask) != mask)
719                         cpu_relax();
720                 else
721                         return 0;
722         } while (!time_after_eq(jiffies, finish));
723
724         return -EBUSY;
725 }
726
727 static int miphy_osc_is_ready(struct miphy28lp_phy *miphy_phy)
728 {
729         struct miphy28lp_dev *miphy_dev = miphy_phy->phydev;
730         unsigned long finish = jiffies + 5 * HZ;
731         u32 val;
732
733         if (!miphy_phy->osc_rdy)
734                 return 0;
735
736         if (!miphy_phy->syscfg_miphy_status)
737                 return -EINVAL;
738
739         do {
740                 regmap_read(miphy_dev->regmap, miphy_phy->syscfg_miphy_status,
741                             &val);
742
743                 if ((val & MIPHY_OSC_RDY) != MIPHY_OSC_RDY)
744                         cpu_relax();
745                 else
746                         return 0;
747         } while (!time_after_eq(jiffies, finish));
748
749         return -EBUSY;
750 }
751
752 static int miphy28lp_get_resource_byname(struct device_node *child,
753                                           char *rname, struct resource *res)
754 {
755         int index;
756
757         index = of_property_match_string(child, "reg-names", rname);
758         if (index < 0)
759                 return -ENODEV;
760
761         return of_address_to_resource(child, index, res);
762 }
763
764 static int miphy28lp_get_one_addr(struct device *dev,
765                                   struct device_node *child, char *rname,
766                                   void __iomem **base)
767 {
768         struct resource res;
769         int ret;
770
771         ret = miphy28lp_get_resource_byname(child, rname, &res);
772         if (!ret) {
773                 *base = devm_ioremap(dev, res.start, resource_size(&res));
774                 if (!*base) {
775                         dev_err(dev, "failed to ioremap %s address region\n"
776                                         , rname);
777                         return -ENOENT;
778                 }
779         }
780
781         return 0;
782 }
783
784 /* MiPHY reset and sysconf setup */
785 static int miphy28lp_setup(struct miphy28lp_phy *miphy_phy, u32 miphy_val)
786 {
787         int err;
788         struct miphy28lp_dev *miphy_dev = miphy_phy->phydev;
789
790         if (!miphy_phy->syscfg_miphy_ctrl)
791                 return -EINVAL;
792
793         err = reset_control_assert(miphy_phy->miphy_rst);
794         if (err) {
795                 dev_err(miphy_dev->dev, "unable to bring out of miphy reset\n");
796                 return err;
797         }
798
799         if (miphy_phy->osc_force_ext)
800                 miphy_val |= MIPHY_OSC_FORCE_EXT;
801
802         regmap_update_bits(miphy_dev->regmap, miphy_phy->syscfg_miphy_ctrl,
803                            MIPHY_CTRL_MASK, miphy_val);
804
805         err = reset_control_deassert(miphy_phy->miphy_rst);
806         if (err) {
807                 dev_err(miphy_dev->dev, "unable to bring out of miphy reset\n");
808                 return err;
809         }
810
811         return miphy_osc_is_ready(miphy_phy);
812 }
813
814 static int miphy28lp_init_sata(struct miphy28lp_phy *miphy_phy)
815 {
816         struct miphy28lp_dev *miphy_dev = miphy_phy->phydev;
817         int err, sata_conf = SATA_CTRL_SELECT_SATA;
818
819         if ((!miphy_phy->syscfg_sata) || (!miphy_phy->syscfg_pci)
820                 || (!miphy_phy->base))
821                 return -EINVAL;
822
823         dev_info(miphy_dev->dev, "sata-up mode, addr 0x%p\n", miphy_phy->base);
824
825         /* Configure the glue-logic */
826         sata_conf |= ((miphy_phy->sata_gen - SATA_GEN1) << SATA_SPDMODE);
827
828         regmap_update_bits(miphy_dev->regmap, miphy_phy->syscfg_sata,
829                            SATA_CTRL_MASK, sata_conf);
830
831         regmap_update_bits(miphy_dev->regmap, miphy_phy->syscfg_pci,
832                            PCIE_CTRL_MASK, SATA_CTRL_SELECT_PCIE);
833
834         /* MiPHY path and clocking init */
835         err = miphy28lp_setup(miphy_phy, MIPHY_CTRL_DEFAULT);
836
837         if (err) {
838                 dev_err(miphy_dev->dev, "SATA phy setup failed\n");
839                 return err;
840         }
841
842         /* initialize miphy */
843         miphy28lp_configure_sata(miphy_phy);
844
845         return miphy_is_ready(miphy_phy);
846 }
847
848 static int miphy28lp_init_pcie(struct miphy28lp_phy *miphy_phy)
849 {
850         struct miphy28lp_dev *miphy_dev = miphy_phy->phydev;
851         int err;
852
853         if ((!miphy_phy->syscfg_sata) || (!miphy_phy->syscfg_pci)
854                 || (!miphy_phy->base) || (!miphy_phy->pipebase))
855                 return -EINVAL;
856
857         dev_info(miphy_dev->dev, "pcie-up mode, addr 0x%p\n", miphy_phy->base);
858
859         /* Configure the glue-logic */
860         regmap_update_bits(miphy_dev->regmap, miphy_phy->syscfg_sata,
861                            SATA_CTRL_MASK, SATA_CTRL_SELECT_PCIE);
862
863         regmap_update_bits(miphy_dev->regmap, miphy_phy->syscfg_pci,
864                            PCIE_CTRL_MASK, SYSCFG_PCIE_PCIE_VAL);
865
866         /* MiPHY path and clocking init */
867         err = miphy28lp_setup(miphy_phy, MIPHY_CTRL_DEFAULT);
868
869         if (err) {
870                 dev_err(miphy_dev->dev, "PCIe phy setup failed\n");
871                 return err;
872         }
873
874         /* initialize miphy */
875         err = miphy28lp_configure_pcie(miphy_phy);
876         if (err)
877                 return err;
878
879         /* PIPE Wrapper Configuration */
880         writeb_relaxed(0x68, miphy_phy->pipebase + 0x104); /* Rise_0 */
881         writeb_relaxed(0x61, miphy_phy->pipebase + 0x105); /* Rise_1 */
882         writeb_relaxed(0x68, miphy_phy->pipebase + 0x108); /* Fall_0 */
883         writeb_relaxed(0x61, miphy_phy->pipebase + 0x109); /* Fall-1 */
884         writeb_relaxed(0x68, miphy_phy->pipebase + 0x10c); /* Threshold_0 */
885         writeb_relaxed(0x60, miphy_phy->pipebase + 0x10d); /* Threshold_1 */
886
887         /* Wait for phy_ready */
888         return miphy_is_ready(miphy_phy);
889 }
890
891 static int miphy28lp_init_usb3(struct miphy28lp_phy *miphy_phy)
892 {
893         struct miphy28lp_dev *miphy_dev = miphy_phy->phydev;
894         int err;
895
896         if ((!miphy_phy->base) || (!miphy_phy->pipebase))
897                 return -EINVAL;
898
899         dev_info(miphy_dev->dev, "usb3-up mode, addr 0x%p\n", miphy_phy->base);
900
901         /* MiPHY path and clocking init */
902         err = miphy28lp_setup(miphy_phy, MIPHY_CTRL_SYNC_D_EN);
903         if (err) {
904                 dev_err(miphy_dev->dev, "USB3 phy setup failed\n");
905                 return err;
906         }
907
908         /* initialize miphy */
909         miphy28lp_configure_usb3(miphy_phy);
910
911         /* PIPE Wrapper Configuration */
912         writeb_relaxed(0x68, miphy_phy->pipebase + 0x23);
913         writeb_relaxed(0x61, miphy_phy->pipebase + 0x24);
914         writeb_relaxed(0x68, miphy_phy->pipebase + 0x26);
915         writeb_relaxed(0x61, miphy_phy->pipebase + 0x27);
916         writeb_relaxed(0x18, miphy_phy->pipebase + 0x29);
917         writeb_relaxed(0x61, miphy_phy->pipebase + 0x2a);
918
919         /* pipe Wrapper usb3 TX swing de-emph margin PREEMPH[7:4], SWING[3:0] */
920         writeb_relaxed(0X67, miphy_phy->pipebase + 0x68);
921         writeb_relaxed(0x0d, miphy_phy->pipebase + 0x69);
922         writeb_relaxed(0X67, miphy_phy->pipebase + 0x6a);
923         writeb_relaxed(0X0d, miphy_phy->pipebase + 0x6b);
924         writeb_relaxed(0X67, miphy_phy->pipebase + 0x6c);
925         writeb_relaxed(0X0d, miphy_phy->pipebase + 0x6d);
926         writeb_relaxed(0X67, miphy_phy->pipebase + 0x6e);
927         writeb_relaxed(0X0d, miphy_phy->pipebase + 0x6f);
928
929         return miphy_is_ready(miphy_phy);
930 }
931
932 static int miphy28lp_init(struct phy *phy)
933 {
934         struct miphy28lp_phy *miphy_phy = phy_get_drvdata(phy);
935         struct miphy28lp_dev *miphy_dev = miphy_phy->phydev;
936         int ret;
937
938         mutex_lock(&miphy_dev->miphy_mutex);
939
940         switch (miphy_phy->type) {
941
942         case PHY_TYPE_SATA:
943                 ret = miphy28lp_init_sata(miphy_phy);
944                 break;
945         case PHY_TYPE_PCIE:
946                 ret = miphy28lp_init_pcie(miphy_phy);
947                 break;
948         case PHY_TYPE_USB3:
949                 ret = miphy28lp_init_usb3(miphy_phy);
950                 break;
951         default:
952                 return -EINVAL;
953         }
954
955         mutex_unlock(&miphy_dev->miphy_mutex);
956
957         return ret;
958 }
959
960 static int miphy28lp_get_addr(struct miphy28lp_phy *miphy_phy)
961 {
962         struct miphy28lp_dev *miphy_dev = miphy_phy->phydev;
963         struct device_node *phynode = miphy_phy->phy->dev.of_node;
964         int err;
965
966         if ((miphy_phy->type != PHY_TYPE_SATA) &&
967             (miphy_phy->type != PHY_TYPE_PCIE) &&
968             (miphy_phy->type != PHY_TYPE_USB3)) {
969                 return -EINVAL;
970         }
971
972         err = miphy28lp_get_one_addr(miphy_dev->dev, phynode,
973                         PHY_TYPE_name[miphy_phy->type - PHY_TYPE_SATA],
974                         &miphy_phy->base);
975         if (err)
976                 return err;
977
978         if ((miphy_phy->type == PHY_TYPE_PCIE) ||
979             (miphy_phy->type == PHY_TYPE_USB3)) {
980                 err = miphy28lp_get_one_addr(miphy_dev->dev, phynode, "pipew",
981                                              &miphy_phy->pipebase);
982                 if (err)
983                         return err;
984         }
985
986         return 0;
987 }
988
989 static struct phy *miphy28lp_xlate(struct device *dev,
990                                    struct of_phandle_args *args)
991 {
992         struct miphy28lp_dev *miphy_dev = dev_get_drvdata(dev);
993         struct miphy28lp_phy *miphy_phy = NULL;
994         struct device_node *phynode = args->np;
995         int ret, index = 0;
996
997         if (!of_device_is_available(phynode)) {
998                 dev_warn(dev, "Requested PHY is disabled\n");
999                 return ERR_PTR(-ENODEV);
1000         }
1001
1002         if (args->args_count != 1) {
1003                 dev_err(dev, "Invalid number of cells in 'phy' property\n");
1004                 return ERR_PTR(-EINVAL);
1005         }
1006
1007         for (index = 0; index < of_get_child_count(dev->of_node); index++)
1008                 if (phynode == miphy_dev->phys[index]->phy->dev.of_node) {
1009                         miphy_phy = miphy_dev->phys[index];
1010                         break;
1011                 }
1012
1013         if (!miphy_phy) {
1014                 dev_err(dev, "Failed to find appropriate phy\n");
1015                 return ERR_PTR(-EINVAL);
1016         }
1017
1018         miphy_phy->type = args->args[0];
1019
1020         ret = miphy28lp_get_addr(miphy_phy);
1021         if (ret < 0)
1022                 return ERR_PTR(ret);
1023
1024         return miphy_phy->phy;
1025 }
1026
1027 static struct phy_ops miphy28lp_ops = {
1028         .init = miphy28lp_init,
1029 };
1030
1031 static int miphy28lp_probe_resets(struct device_node *node,
1032                                   struct miphy28lp_phy *miphy_phy)
1033 {
1034         struct miphy28lp_dev *miphy_dev = miphy_phy->phydev;
1035         int err;
1036
1037         miphy_phy->miphy_rst = of_reset_control_get(node, "miphy-sw-rst");
1038
1039         if (IS_ERR(miphy_phy->miphy_rst)) {
1040                 dev_err(miphy_dev->dev,
1041                                 "miphy soft reset control not defined\n");
1042                 return PTR_ERR(miphy_phy->miphy_rst);
1043         }
1044
1045         err = reset_control_deassert(miphy_phy->miphy_rst);
1046         if (err) {
1047                 dev_err(miphy_dev->dev, "unable to bring out of miphy reset\n");
1048                 return err;
1049         }
1050
1051         return 0;
1052 }
1053
1054 static int miphy28lp_of_probe(struct device_node *np,
1055                               struct miphy28lp_phy *miphy_phy)
1056 {
1057         struct resource res;
1058
1059         miphy_phy->osc_force_ext =
1060                 of_property_read_bool(np, "st,osc-force-ext");
1061
1062         miphy_phy->osc_rdy = of_property_read_bool(np, "st,osc-rdy");
1063
1064         miphy_phy->px_rx_pol_inv =
1065                 of_property_read_bool(np, "st,px_rx_pol_inv");
1066
1067         of_property_read_u32(np, "st,sata-gen", &miphy_phy->sata_gen);
1068         if (!miphy_phy->sata_gen)
1069                 miphy_phy->sata_gen = SATA_GEN1;
1070
1071         if (!miphy28lp_get_resource_byname(np, "miphy-ctrl-glue", &res))
1072                 miphy_phy->syscfg_miphy_ctrl = res.start;
1073
1074         if (!miphy28lp_get_resource_byname(np, "miphy-status-glue", &res))
1075                 miphy_phy->syscfg_miphy_status = res.start;
1076
1077         if (!miphy28lp_get_resource_byname(np, "pcie-glue", &res))
1078                 miphy_phy->syscfg_pci = res.start;
1079
1080         if (!miphy28lp_get_resource_byname(np, "sata-glue", &res))
1081                 miphy_phy->syscfg_sata = res.start;
1082
1083
1084         return 0;
1085 }
1086
1087 static int miphy28lp_probe(struct platform_device *pdev)
1088 {
1089         struct device_node *child, *np = pdev->dev.of_node;
1090         struct miphy28lp_dev *miphy_dev;
1091         struct phy_provider *provider;
1092         struct phy *phy;
1093         int chancount, port = 0;
1094         int ret;
1095
1096         miphy_dev = devm_kzalloc(&pdev->dev, sizeof(*miphy_dev), GFP_KERNEL);
1097         if (!miphy_dev)
1098                 return -ENOMEM;
1099
1100         chancount = of_get_child_count(np);
1101         miphy_dev->phys = devm_kzalloc(&pdev->dev, sizeof(phy) * chancount,
1102                                        GFP_KERNEL);
1103         if (!miphy_dev->phys)
1104                 return -ENOMEM;
1105
1106         miphy_dev->regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
1107         if (IS_ERR(miphy_dev->regmap)) {
1108                 dev_err(miphy_dev->dev, "No syscfg phandle specified\n");
1109                 return PTR_ERR(miphy_dev->regmap);
1110         }
1111
1112         miphy_dev->dev = &pdev->dev;
1113
1114         dev_set_drvdata(&pdev->dev, miphy_dev);
1115
1116         mutex_init(&miphy_dev->miphy_mutex);
1117
1118         for_each_child_of_node(np, child) {
1119                 struct miphy28lp_phy *miphy_phy;
1120
1121                 miphy_phy = devm_kzalloc(&pdev->dev, sizeof(*miphy_phy),
1122                                          GFP_KERNEL);
1123                 if (!miphy_phy)
1124                         return -ENOMEM;
1125
1126                 miphy_dev->phys[port] = miphy_phy;
1127
1128                 phy = devm_phy_create(&pdev->dev, child, &miphy28lp_ops, NULL);
1129                 if (IS_ERR(phy)) {
1130                         dev_err(&pdev->dev, "failed to create PHY\n");
1131                         return PTR_ERR(phy);
1132                 }
1133
1134                 miphy_dev->phys[port]->phy = phy;
1135                 miphy_dev->phys[port]->phydev = miphy_dev;
1136
1137                 ret = miphy28lp_of_probe(child, miphy_phy);
1138                 if (ret)
1139                         return ret;
1140
1141                 ret = miphy28lp_probe_resets(child, miphy_dev->phys[port]);
1142                 if (ret)
1143                         return ret;
1144
1145                 phy_set_drvdata(phy, miphy_dev->phys[port]);
1146                 port++;
1147
1148         }
1149
1150         provider = devm_of_phy_provider_register(&pdev->dev, miphy28lp_xlate);
1151         if (IS_ERR(provider))
1152                 return PTR_ERR(provider);
1153
1154         return 0;
1155 }
1156
1157 static const struct of_device_id miphy28lp_of_match[] = {
1158         {.compatible = "st,miphy28lp-phy", },
1159         {},
1160 };
1161
1162 MODULE_DEVICE_TABLE(of, miphy28lp_of_match);
1163
1164 static struct platform_driver miphy28lp_driver = {
1165         .probe = miphy28lp_probe,
1166         .driver = {
1167                 .name = "miphy28lp-phy",
1168                 .owner = THIS_MODULE,
1169                 .of_match_table = miphy28lp_of_match,
1170         }
1171 };
1172
1173 module_platform_driver(miphy28lp_driver);
1174
1175 MODULE_AUTHOR("Alexandre Torgue <alexandre.torgue@st.com>");
1176 MODULE_DESCRIPTION("STMicroelectronics miphy28lp driver");
1177 MODULE_LICENSE("GPL v2");