blob: 98bffbc0f74dad1c4b6cf4892d92c3424dfadaad [file] [log] [blame]
Lee Jones6e877fe2014-07-09 12:41:12 +01001/*
2 * Copyright (C) 2014 STMicroelectronics – All Rights Reserved
3 *
4 * STMicroelectronics PHY driver MiPHY365 (for SoC STiH416).
5 *
6 * Authors: Alexandre Torgue <alexandre.torgue@st.com>
7 * Lee Jones <lee.jones@linaro.org>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2, as
11 * published by the Free Software Foundation.
12 *
13 */
14
15#include <linux/platform_device.h>
16#include <linux/io.h>
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/of.h>
20#include <linux/of_platform.h>
Lee Jones7ebdb522014-07-09 12:41:13 +010021#include <linux/of_address.h>
Lee Jones6e877fe2014-07-09 12:41:12 +010022#include <linux/clk.h>
23#include <linux/phy/phy.h>
24#include <linux/delay.h>
25#include <linux/mfd/syscon.h>
26#include <linux/regmap.h>
27
Peter Griffinfbea2302015-03-30 16:17:07 +010028#include <dt-bindings/phy/phy.h>
Lee Jones6e877fe2014-07-09 12:41:12 +010029
30#define HFC_TIMEOUT 100
31
Lee Jones7ebdb522014-07-09 12:41:13 +010032#define SYSCFG_SELECT_SATA_MASK BIT(1)
33#define SYSCFG_SELECT_SATA_POS 1
Lee Jones6e877fe2014-07-09 12:41:12 +010034
35/* MiPHY365x register definitions */
36#define RESET_REG 0x00
37#define RST_PLL BIT(1)
38#define RST_PLL_CAL BIT(2)
39#define RST_RX BIT(4)
40#define RST_MACRO BIT(7)
41
42#define STATUS_REG 0x01
43#define IDLL_RDY BIT(0)
44#define PLL_RDY BIT(1)
45#define DES_BIT_LOCK BIT(2)
46#define DES_SYMBOL_LOCK BIT(3)
47
48#define CTRL_REG 0x02
49#define TERM_EN BIT(0)
50#define PCI_EN BIT(2)
51#define DES_BIT_LOCK_EN BIT(3)
52#define TX_POL BIT(5)
53
54#define INT_CTRL_REG 0x03
55
56#define BOUNDARY1_REG 0x10
57#define SPDSEL_SEL BIT(0)
58
59#define BOUNDARY3_REG 0x12
60#define TX_SPDSEL_GEN1_VAL 0
61#define TX_SPDSEL_GEN2_VAL 0x01
62#define TX_SPDSEL_GEN3_VAL 0x02
63#define RX_SPDSEL_GEN1_VAL 0
64#define RX_SPDSEL_GEN2_VAL (0x01 << 3)
65#define RX_SPDSEL_GEN3_VAL (0x02 << 3)
66
67#define PCIE_REG 0x16
68
69#define BUF_SEL_REG 0x20
70#define CONF_GEN_SEL_GEN3 0x02
71#define CONF_GEN_SEL_GEN2 0x01
72#define PD_VDDTFILTER BIT(4)
73
74#define TXBUF1_REG 0x21
75#define SWING_VAL 0x04
76#define SWING_VAL_GEN1 0x03
77#define PREEMPH_VAL (0x3 << 5)
78
79#define TXBUF2_REG 0x22
80#define TXSLEW_VAL 0x2
81#define TXSLEW_VAL_GEN1 0x4
82
83#define RXBUF_OFFSET_CTRL_REG 0x23
84
85#define RXBUF_REG 0x25
86#define SDTHRES_VAL 0x01
87#define EQ_ON3 (0x03 << 4)
88#define EQ_ON1 (0x01 << 4)
89
90#define COMP_CTRL1_REG 0x40
91#define START_COMSR BIT(0)
92#define START_COMZC BIT(1)
93#define COMSR_DONE BIT(2)
94#define COMZC_DONE BIT(3)
95#define COMP_AUTO_LOAD BIT(4)
96
97#define COMP_CTRL2_REG 0x41
98#define COMP_2MHZ_RAT_GEN1 0x1e
99#define COMP_2MHZ_RAT 0xf
100
101#define COMP_CTRL3_REG 0x42
102#define COMSR_COMP_REF 0x33
103
104#define COMP_IDLL_REG 0x47
105#define COMZC_IDLL 0x2a
106
107#define PLL_CTRL1_REG 0x50
108#define PLL_START_CAL BIT(0)
109#define BUF_EN BIT(2)
110#define SYNCHRO_TX BIT(3)
111#define SSC_EN BIT(6)
112#define CONFIG_PLL BIT(7)
113
114#define PLL_CTRL2_REG 0x51
115#define BYPASS_PLL_CAL BIT(1)
116
117#define PLL_RAT_REG 0x52
118
119#define PLL_SSC_STEP_MSB_REG 0x56
120#define PLL_SSC_STEP_MSB_VAL 0x03
121
122#define PLL_SSC_STEP_LSB_REG 0x57
123#define PLL_SSC_STEP_LSB_VAL 0x63
124
125#define PLL_SSC_PER_MSB_REG 0x58
126#define PLL_SSC_PER_MSB_VAL 0
127
128#define PLL_SSC_PER_LSB_REG 0x59
129#define PLL_SSC_PER_LSB_VAL 0xf1
130
131#define IDLL_TEST_REG 0x72
132#define START_CLK_HF BIT(6)
133
134#define DES_BITLOCK_REG 0x86
135#define BIT_LOCK_LEVEL 0x01
136#define BIT_LOCK_CNT_512 (0x03 << 5)
137
Lee Jones6e877fe2014-07-09 12:41:12 +0100138struct miphy365x_phy {
139 struct phy *phy;
140 void __iomem *base;
Lee Jones7ebdb522014-07-09 12:41:13 +0100141 bool pcie_tx_pol_inv;
142 bool sata_tx_pol_inv;
143 u32 sata_gen;
Peter Griffin63139882015-01-07 15:04:07 +0000144 u32 ctrlreg;
Lee Jones6e877fe2014-07-09 12:41:12 +0100145 u8 type;
Lee Jones6e877fe2014-07-09 12:41:12 +0100146};
147
148struct miphy365x_dev {
149 struct device *dev;
150 struct regmap *regmap;
151 struct mutex miphy_mutex;
Lee Jones7ebdb522014-07-09 12:41:13 +0100152 struct miphy365x_phy **phys;
Lee Jones6e877fe2014-07-09 12:41:12 +0100153};
154
155/*
156 * These values are represented in Device tree. They are considered to be ABI
157 * and although they can be extended any existing values must not change.
158 */
159enum miphy_sata_gen {
160 SATA_GEN1 = 1,
161 SATA_GEN2,
162 SATA_GEN3
163};
164
165static u8 rx_tx_spd[] = {
Lee Jonesa6cc1b92014-08-28 14:59:50 +0100166 0, /* GEN0 doesn't exist. */
Lee Jones6e877fe2014-07-09 12:41:12 +0100167 TX_SPDSEL_GEN1_VAL | RX_SPDSEL_GEN1_VAL,
168 TX_SPDSEL_GEN2_VAL | RX_SPDSEL_GEN2_VAL,
169 TX_SPDSEL_GEN3_VAL | RX_SPDSEL_GEN3_VAL
170};
171
172/*
173 * This function selects the system configuration,
174 * either two SATA, one SATA and one PCIe, or two PCIe lanes.
175 */
176static int miphy365x_set_path(struct miphy365x_phy *miphy_phy,
177 struct miphy365x_dev *miphy_dev)
178{
Peter Griffinfbea2302015-03-30 16:17:07 +0100179 bool sata = (miphy_phy->type == PHY_TYPE_SATA);
Lee Jones6e877fe2014-07-09 12:41:12 +0100180
Lee Jones7ebdb522014-07-09 12:41:13 +0100181 return regmap_update_bits(miphy_dev->regmap,
Peter Griffin63139882015-01-07 15:04:07 +0000182 miphy_phy->ctrlreg,
Lee Jones7ebdb522014-07-09 12:41:13 +0100183 SYSCFG_SELECT_SATA_MASK,
184 sata << SYSCFG_SELECT_SATA_POS);
Lee Jones6e877fe2014-07-09 12:41:12 +0100185}
186
187static int miphy365x_init_pcie_port(struct miphy365x_phy *miphy_phy,
188 struct miphy365x_dev *miphy_dev)
189{
190 u8 val;
191
192 if (miphy_phy->pcie_tx_pol_inv) {
193 /* Invert Tx polarity and clear pci_txdetect_pol bit */
194 val = TERM_EN | PCI_EN | DES_BIT_LOCK_EN | TX_POL;
195 writeb_relaxed(val, miphy_phy->base + CTRL_REG);
196 writeb_relaxed(0x00, miphy_phy->base + PCIE_REG);
197 }
198
199 return 0;
200}
201
202static inline int miphy365x_hfc_not_rdy(struct miphy365x_phy *miphy_phy,
203 struct miphy365x_dev *miphy_dev)
204{
205 unsigned long timeout = jiffies + msecs_to_jiffies(HFC_TIMEOUT);
206 u8 mask = IDLL_RDY | PLL_RDY;
207 u8 regval;
208
209 do {
210 regval = readb_relaxed(miphy_phy->base + STATUS_REG);
211 if (!(regval & mask))
212 return 0;
213
214 usleep_range(2000, 2500);
215 } while (time_before(jiffies, timeout));
216
217 dev_err(miphy_dev->dev, "HFC ready timeout!\n");
218 return -EBUSY;
219}
220
221static inline int miphy365x_rdy(struct miphy365x_phy *miphy_phy,
222 struct miphy365x_dev *miphy_dev)
223{
224 unsigned long timeout = jiffies + msecs_to_jiffies(HFC_TIMEOUT);
225 u8 mask = IDLL_RDY | PLL_RDY;
226 u8 regval;
227
228 do {
229 regval = readb_relaxed(miphy_phy->base + STATUS_REG);
230 if ((regval & mask) == mask)
231 return 0;
232
233 usleep_range(2000, 2500);
234 } while (time_before(jiffies, timeout));
235
236 dev_err(miphy_dev->dev, "PHY not ready timeout!\n");
237 return -EBUSY;
238}
239
240static inline void miphy365x_set_comp(struct miphy365x_phy *miphy_phy,
241 struct miphy365x_dev *miphy_dev)
242{
243 u8 val, mask;
244
Lee Jones7ebdb522014-07-09 12:41:13 +0100245 if (miphy_phy->sata_gen == SATA_GEN1)
Lee Jones6e877fe2014-07-09 12:41:12 +0100246 writeb_relaxed(COMP_2MHZ_RAT_GEN1,
247 miphy_phy->base + COMP_CTRL2_REG);
248 else
249 writeb_relaxed(COMP_2MHZ_RAT,
250 miphy_phy->base + COMP_CTRL2_REG);
251
Lee Jones7ebdb522014-07-09 12:41:13 +0100252 if (miphy_phy->sata_gen != SATA_GEN3) {
Lee Jones6e877fe2014-07-09 12:41:12 +0100253 writeb_relaxed(COMSR_COMP_REF,
254 miphy_phy->base + COMP_CTRL3_REG);
255 /*
256 * Force VCO current to value defined by address 0x5A
257 * and disable PCIe100Mref bit
258 * Enable auto load compensation for pll_i_bias
259 */
260 writeb_relaxed(BYPASS_PLL_CAL, miphy_phy->base + PLL_CTRL2_REG);
261 writeb_relaxed(COMZC_IDLL, miphy_phy->base + COMP_IDLL_REG);
262 }
263
264 /*
265 * Force restart compensation and enable auto load
266 * for Comzc_Tx, Comzc_Rx and Comsr on macro
267 */
268 val = START_COMSR | START_COMZC | COMP_AUTO_LOAD;
269 writeb_relaxed(val, miphy_phy->base + COMP_CTRL1_REG);
270
271 mask = COMSR_DONE | COMZC_DONE;
272 while ((readb_relaxed(miphy_phy->base + COMP_CTRL1_REG) & mask) != mask)
273 cpu_relax();
274}
275
276static inline void miphy365x_set_ssc(struct miphy365x_phy *miphy_phy,
277 struct miphy365x_dev *miphy_dev)
278{
279 u8 val;
280
281 /*
282 * SSC Settings. SSC will be enabled through Link
283 * SSC Ampl. = 0.4%
284 * SSC Freq = 31KHz
285 */
286 writeb_relaxed(PLL_SSC_STEP_MSB_VAL,
287 miphy_phy->base + PLL_SSC_STEP_MSB_REG);
288 writeb_relaxed(PLL_SSC_STEP_LSB_VAL,
289 miphy_phy->base + PLL_SSC_STEP_LSB_REG);
290 writeb_relaxed(PLL_SSC_PER_MSB_VAL,
291 miphy_phy->base + PLL_SSC_PER_MSB_REG);
292 writeb_relaxed(PLL_SSC_PER_LSB_VAL,
293 miphy_phy->base + PLL_SSC_PER_LSB_REG);
294
295 /* SSC Settings complete */
Lee Jones7ebdb522014-07-09 12:41:13 +0100296 if (miphy_phy->sata_gen == SATA_GEN1) {
Lee Jones6e877fe2014-07-09 12:41:12 +0100297 val = PLL_START_CAL | BUF_EN | SYNCHRO_TX | CONFIG_PLL;
298 writeb_relaxed(val, miphy_phy->base + PLL_CTRL1_REG);
299 } else {
300 val = SSC_EN | PLL_START_CAL | BUF_EN | SYNCHRO_TX | CONFIG_PLL;
301 writeb_relaxed(val, miphy_phy->base + PLL_CTRL1_REG);
302 }
303}
304
305static int miphy365x_init_sata_port(struct miphy365x_phy *miphy_phy,
306 struct miphy365x_dev *miphy_dev)
307{
308 int ret;
309 u8 val;
310
311 /*
312 * Force PHY macro reset, PLL calibration reset, PLL reset
313 * and assert Deserializer Reset
314 */
315 val = RST_PLL | RST_PLL_CAL | RST_RX | RST_MACRO;
316 writeb_relaxed(val, miphy_phy->base + RESET_REG);
317
Lee Jones7ebdb522014-07-09 12:41:13 +0100318 if (miphy_phy->sata_tx_pol_inv)
Lee Jones6e877fe2014-07-09 12:41:12 +0100319 writeb_relaxed(TX_POL, miphy_phy->base + CTRL_REG);
320
321 /*
322 * Force macro1 to use rx_lspd, tx_lspd
323 * Force Rx_Clock on first I-DLL phase
324 * Force Des in HP mode on macro, rx_lspd, tx_lspd for Gen2/3
325 */
326 writeb_relaxed(SPDSEL_SEL, miphy_phy->base + BOUNDARY1_REG);
327 writeb_relaxed(START_CLK_HF, miphy_phy->base + IDLL_TEST_REG);
Lee Jones7ebdb522014-07-09 12:41:13 +0100328 val = rx_tx_spd[miphy_phy->sata_gen];
Lee Jones6e877fe2014-07-09 12:41:12 +0100329 writeb_relaxed(val, miphy_phy->base + BOUNDARY3_REG);
330
331 /* Wait for HFC_READY = 0 */
332 ret = miphy365x_hfc_not_rdy(miphy_phy, miphy_dev);
333 if (ret)
334 return ret;
335
336 /* Compensation Recalibration */
337 miphy365x_set_comp(miphy_phy, miphy_dev);
338
Lee Jones7ebdb522014-07-09 12:41:13 +0100339 switch (miphy_phy->sata_gen) {
Lee Jones6e877fe2014-07-09 12:41:12 +0100340 case SATA_GEN3:
341 /*
342 * TX Swing target 550-600mv peak to peak diff
343 * Tx Slew target 90-110ps rising/falling time
344 * Rx Eq ON3, Sigdet threshold SDTH1
345 */
346 val = PD_VDDTFILTER | CONF_GEN_SEL_GEN3;
347 writeb_relaxed(val, miphy_phy->base + BUF_SEL_REG);
348 val = SWING_VAL | PREEMPH_VAL;
349 writeb_relaxed(val, miphy_phy->base + TXBUF1_REG);
350 writeb_relaxed(TXSLEW_VAL, miphy_phy->base + TXBUF2_REG);
351 writeb_relaxed(0x00, miphy_phy->base + RXBUF_OFFSET_CTRL_REG);
352 val = SDTHRES_VAL | EQ_ON3;
353 writeb_relaxed(val, miphy_phy->base + RXBUF_REG);
354 break;
355 case SATA_GEN2:
356 /*
357 * conf gen sel=0x1 to program Gen2 banked registers
358 * VDDT filter ON
359 * Tx Swing target 550-600mV peak-to-peak diff
360 * Tx Slew target 90-110 ps rising/falling time
361 * RX Equalization ON1, Sigdet threshold SDTH1
362 */
363 writeb_relaxed(CONF_GEN_SEL_GEN2,
364 miphy_phy->base + BUF_SEL_REG);
365 writeb_relaxed(SWING_VAL, miphy_phy->base + TXBUF1_REG);
366 writeb_relaxed(TXSLEW_VAL, miphy_phy->base + TXBUF2_REG);
367 val = SDTHRES_VAL | EQ_ON1;
368 writeb_relaxed(val, miphy_phy->base + RXBUF_REG);
369 break;
370 case SATA_GEN1:
371 /*
372 * conf gen sel = 00b to program Gen1 banked registers
373 * VDDT filter ON
374 * Tx Swing target 500-550mV peak-to-peak diff
375 * Tx Slew target120-140 ps rising/falling time
376 */
377 writeb_relaxed(PD_VDDTFILTER, miphy_phy->base + BUF_SEL_REG);
378 writeb_relaxed(SWING_VAL_GEN1, miphy_phy->base + TXBUF1_REG);
379 writeb_relaxed(TXSLEW_VAL_GEN1, miphy_phy->base + TXBUF2_REG);
380 break;
381 default:
382 break;
383 }
384
385 /* Force Macro1 in partial mode & release pll cal reset */
386 writeb_relaxed(RST_RX, miphy_phy->base + RESET_REG);
387 usleep_range(100, 150);
388
389 miphy365x_set_ssc(miphy_phy, miphy_dev);
390
391 /* Wait for phy_ready */
392 ret = miphy365x_rdy(miphy_phy, miphy_dev);
393 if (ret)
394 return ret;
395
396 /*
397 * Enable macro1 to use rx_lspd & tx_lspd
398 * Release Rx_Clock on first I-DLL phase on macro1
399 * Assert deserializer reset
400 * des_bit_lock_en is set
401 * bit lock detection strength
402 * Deassert deserializer reset
403 */
404 writeb_relaxed(0x00, miphy_phy->base + BOUNDARY1_REG);
405 writeb_relaxed(0x00, miphy_phy->base + IDLL_TEST_REG);
406 writeb_relaxed(RST_RX, miphy_phy->base + RESET_REG);
Lee Jones7ebdb522014-07-09 12:41:13 +0100407 val = miphy_phy->sata_tx_pol_inv ?
Lee Jones6e877fe2014-07-09 12:41:12 +0100408 (TX_POL | DES_BIT_LOCK_EN) : DES_BIT_LOCK_EN;
409 writeb_relaxed(val, miphy_phy->base + CTRL_REG);
410
411 val = BIT_LOCK_CNT_512 | BIT_LOCK_LEVEL;
412 writeb_relaxed(val, miphy_phy->base + DES_BITLOCK_REG);
413 writeb_relaxed(0x00, miphy_phy->base + RESET_REG);
414
415 return 0;
416}
417
418static int miphy365x_init(struct phy *phy)
419{
420 struct miphy365x_phy *miphy_phy = phy_get_drvdata(phy);
421 struct miphy365x_dev *miphy_dev = dev_get_drvdata(phy->dev.parent);
422 int ret = 0;
423
424 mutex_lock(&miphy_dev->miphy_mutex);
425
426 ret = miphy365x_set_path(miphy_phy, miphy_dev);
427 if (ret) {
428 mutex_unlock(&miphy_dev->miphy_mutex);
429 return ret;
430 }
431
432 /* Initialise Miphy for PCIe or SATA */
Peter Griffinfbea2302015-03-30 16:17:07 +0100433 if (miphy_phy->type == PHY_TYPE_PCIE)
Lee Jones6e877fe2014-07-09 12:41:12 +0100434 ret = miphy365x_init_pcie_port(miphy_phy, miphy_dev);
435 else
436 ret = miphy365x_init_sata_port(miphy_phy, miphy_dev);
437
438 mutex_unlock(&miphy_dev->miphy_mutex);
439
440 return ret;
441}
442
Lee Jones7ebdb522014-07-09 12:41:13 +0100443int miphy365x_get_addr(struct device *dev, struct miphy365x_phy *miphy_phy,
444 int index)
445{
446 struct device_node *phynode = miphy_phy->phy->dev.of_node;
447 const char *name;
Lee Jones7ebdb522014-07-09 12:41:13 +0100448 int type = miphy_phy->type;
449 int ret;
450
451 ret = of_property_read_string_index(phynode, "reg-names", index, &name);
452 if (ret) {
453 dev_err(dev, "no reg-names property not found\n");
454 return ret;
455 }
456
Peter Griffinfbea2302015-03-30 16:17:07 +0100457 if (!((!strncmp(name, "sata", 4) && type == PHY_TYPE_SATA) ||
458 (!strncmp(name, "pcie", 4) && type == PHY_TYPE_PCIE)))
Lee Jones7ebdb522014-07-09 12:41:13 +0100459 return 0;
460
461 miphy_phy->base = of_iomap(phynode, index);
462 if (!miphy_phy->base) {
463 dev_err(dev, "Failed to map %s\n", phynode->full_name);
464 return -EINVAL;
465 }
466
467 return 0;
468}
469
Lee Jones6e877fe2014-07-09 12:41:12 +0100470static struct phy *miphy365x_xlate(struct device *dev,
471 struct of_phandle_args *args)
472{
Lee Jones7ebdb522014-07-09 12:41:13 +0100473 struct miphy365x_dev *miphy_dev = dev_get_drvdata(dev);
474 struct miphy365x_phy *miphy_phy = NULL;
475 struct device_node *phynode = args->np;
476 int ret, index;
Lee Jones6e877fe2014-07-09 12:41:12 +0100477
Lee Jones7ebdb522014-07-09 12:41:13 +0100478 if (!of_device_is_available(phynode)) {
479 dev_warn(dev, "Requested PHY is disabled\n");
480 return ERR_PTR(-ENODEV);
481 }
482
483 if (args->args_count != 1) {
Lee Jones6e877fe2014-07-09 12:41:12 +0100484 dev_err(dev, "Invalid number of cells in 'phy' property\n");
485 return ERR_PTR(-EINVAL);
486 }
487
Lee Jones7ebdb522014-07-09 12:41:13 +0100488 for (index = 0; index < of_get_child_count(dev->of_node); index++)
489 if (phynode == miphy_dev->phys[index]->phy->dev.of_node) {
490 miphy_phy = miphy_dev->phys[index];
491 break;
492 }
493
494 if (!miphy_phy) {
495 dev_err(dev, "Failed to find appropriate phy\n");
Lee Jones6e877fe2014-07-09 12:41:12 +0100496 return ERR_PTR(-EINVAL);
497 }
498
Lee Jones7ebdb522014-07-09 12:41:13 +0100499 miphy_phy->type = args->args[0];
Lee Jones6e877fe2014-07-09 12:41:12 +0100500
Peter Griffinfbea2302015-03-30 16:17:07 +0100501 if (!(miphy_phy->type == PHY_TYPE_SATA ||
502 miphy_phy->type == PHY_TYPE_PCIE)) {
Lee Jones7ebdb522014-07-09 12:41:13 +0100503 dev_err(dev, "Unsupported device type: %d\n", miphy_phy->type);
Lee Jones6e877fe2014-07-09 12:41:12 +0100504 return ERR_PTR(-EINVAL);
505 }
506
Lee Jones7ebdb522014-07-09 12:41:13 +0100507 /* Each port handles SATA and PCIE - third entry is always sysconf. */
508 for (index = 0; index < 3; index++) {
509 ret = miphy365x_get_addr(dev, miphy_phy, index);
510 if (ret < 0)
511 return ERR_PTR(ret);
512 }
Lee Jones6e877fe2014-07-09 12:41:12 +0100513
Lee Jones7ebdb522014-07-09 12:41:13 +0100514 return miphy_phy->phy;
Lee Jones6e877fe2014-07-09 12:41:12 +0100515}
516
517static struct phy_ops miphy365x_ops = {
518 .init = miphy365x_init,
519 .owner = THIS_MODULE,
520};
521
Lee Jones7ebdb522014-07-09 12:41:13 +0100522static int miphy365x_of_probe(struct device_node *phynode,
523 struct miphy365x_phy *miphy_phy)
Lee Jones6e877fe2014-07-09 12:41:12 +0100524{
Lee Jones7ebdb522014-07-09 12:41:13 +0100525 of_property_read_u32(phynode, "st,sata-gen", &miphy_phy->sata_gen);
526 if (!miphy_phy->sata_gen)
527 miphy_phy->sata_gen = SATA_GEN1;
Lee Jones6e877fe2014-07-09 12:41:12 +0100528
Lee Jones7ebdb522014-07-09 12:41:13 +0100529 miphy_phy->pcie_tx_pol_inv =
530 of_property_read_bool(phynode, "st,pcie-tx-pol-inv");
Lee Jones6e877fe2014-07-09 12:41:12 +0100531
Lee Jones7ebdb522014-07-09 12:41:13 +0100532 miphy_phy->sata_tx_pol_inv =
533 of_property_read_bool(phynode, "st,sata-tx-pol-inv");
Lee Jones6e877fe2014-07-09 12:41:12 +0100534
535 return 0;
536}
537
538static int miphy365x_probe(struct platform_device *pdev)
539{
Lee Jones7ebdb522014-07-09 12:41:13 +0100540 struct device_node *child, *np = pdev->dev.of_node;
541 struct miphy365x_dev *miphy_dev;
Lee Jones6e877fe2014-07-09 12:41:12 +0100542 struct phy_provider *provider;
Lee Jones7ebdb522014-07-09 12:41:13 +0100543 struct phy *phy;
544 int chancount, port = 0;
Lee Jones6e877fe2014-07-09 12:41:12 +0100545 int ret;
546
Lee Jones7ebdb522014-07-09 12:41:13 +0100547 miphy_dev = devm_kzalloc(&pdev->dev, sizeof(*miphy_dev), GFP_KERNEL);
548 if (!miphy_dev)
Lee Jones6e877fe2014-07-09 12:41:12 +0100549 return -ENOMEM;
550
Lee Jones7ebdb522014-07-09 12:41:13 +0100551 chancount = of_get_child_count(np);
552 miphy_dev->phys = devm_kzalloc(&pdev->dev, sizeof(phy) * chancount,
553 GFP_KERNEL);
554 if (!miphy_dev->phys)
555 return -ENOMEM;
Lee Jones6e877fe2014-07-09 12:41:12 +0100556
Lee Jones7ebdb522014-07-09 12:41:13 +0100557 miphy_dev->regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
558 if (IS_ERR(miphy_dev->regmap)) {
559 dev_err(miphy_dev->dev, "No syscfg phandle specified\n");
560 return PTR_ERR(miphy_dev->regmap);
561 }
Lee Jones6e877fe2014-07-09 12:41:12 +0100562
Lee Jones7ebdb522014-07-09 12:41:13 +0100563 miphy_dev->dev = &pdev->dev;
Lee Jones6e877fe2014-07-09 12:41:12 +0100564
Lee Jones7ebdb522014-07-09 12:41:13 +0100565 dev_set_drvdata(&pdev->dev, miphy_dev);
Lee Jones6e877fe2014-07-09 12:41:12 +0100566
Lee Jones7ebdb522014-07-09 12:41:13 +0100567 mutex_init(&miphy_dev->miphy_mutex);
Lee Jones6e877fe2014-07-09 12:41:12 +0100568
Lee Jones7ebdb522014-07-09 12:41:13 +0100569 for_each_child_of_node(np, child) {
570 struct miphy365x_phy *miphy_phy;
571
572 miphy_phy = devm_kzalloc(&pdev->dev, sizeof(*miphy_phy),
573 GFP_KERNEL);
574 if (!miphy_phy)
575 return -ENOMEM;
576
577 miphy_dev->phys[port] = miphy_phy;
578
Heikki Krogerusdbc98632014-11-19 17:28:21 +0200579 phy = devm_phy_create(&pdev->dev, child, &miphy365x_ops);
Lee Jones6e877fe2014-07-09 12:41:12 +0100580 if (IS_ERR(phy)) {
Lee Jones7ebdb522014-07-09 12:41:13 +0100581 dev_err(&pdev->dev, "failed to create PHY\n");
Lee Jones6e877fe2014-07-09 12:41:12 +0100582 return PTR_ERR(phy);
583 }
584
Lee Jones7ebdb522014-07-09 12:41:13 +0100585 miphy_dev->phys[port]->phy = phy;
Lee Jones6e877fe2014-07-09 12:41:12 +0100586
Lee Jones7ebdb522014-07-09 12:41:13 +0100587 ret = miphy365x_of_probe(child, miphy_phy);
Lee Jones6e877fe2014-07-09 12:41:12 +0100588 if (ret)
589 return ret;
590
Lee Jones7ebdb522014-07-09 12:41:13 +0100591 phy_set_drvdata(phy, miphy_dev->phys[port]);
Peter Griffin63139882015-01-07 15:04:07 +0000592
Lee Jones7ebdb522014-07-09 12:41:13 +0100593 port++;
Peter Griffin63139882015-01-07 15:04:07 +0000594 /* sysconfig offsets are indexed from 1 */
595 ret = of_property_read_u32_index(np, "st,syscfg", port,
596 &miphy_phy->ctrlreg);
597 if (ret) {
598 dev_err(&pdev->dev, "No sysconfig offset found\n");
599 return ret;
600 }
Lee Jones6e877fe2014-07-09 12:41:12 +0100601 }
602
Lee Jones7ebdb522014-07-09 12:41:13 +0100603 provider = devm_of_phy_provider_register(&pdev->dev, miphy365x_xlate);
Gregory CLEMENTc1fc0052014-11-13 12:47:43 +0100604 return PTR_ERR_OR_ZERO(provider);
Lee Jones6e877fe2014-07-09 12:41:12 +0100605}
606
607static const struct of_device_id miphy365x_of_match[] = {
608 { .compatible = "st,miphy365x-phy", },
609 { },
610};
611MODULE_DEVICE_TABLE(of, miphy365x_of_match);
612
613static struct platform_driver miphy365x_driver = {
614 .probe = miphy365x_probe,
615 .driver = {
616 .name = "miphy365x-phy",
Lee Jones6e877fe2014-07-09 12:41:12 +0100617 .of_match_table = miphy365x_of_match,
618 }
619};
620module_platform_driver(miphy365x_driver);
621
622MODULE_AUTHOR("Alexandre Torgue <alexandre.torgue@st.com>");
623MODULE_DESCRIPTION("STMicroelectronics miphy365x driver");
624MODULE_LICENSE("GPL v2");