phy-miphy365x.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /*
  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. #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 <dt-bindings/phy/phy-miphy365x.h>
  27. #define HFC_TIMEOUT 100
  28. #define SYSCFG_SELECT_SATA_MASK BIT(1)
  29. #define SYSCFG_SELECT_SATA_POS 1
  30. /* MiPHY365x register definitions */
  31. #define RESET_REG 0x00
  32. #define RST_PLL BIT(1)
  33. #define RST_PLL_CAL BIT(2)
  34. #define RST_RX BIT(4)
  35. #define RST_MACRO BIT(7)
  36. #define STATUS_REG 0x01
  37. #define IDLL_RDY BIT(0)
  38. #define PLL_RDY BIT(1)
  39. #define DES_BIT_LOCK BIT(2)
  40. #define DES_SYMBOL_LOCK BIT(3)
  41. #define CTRL_REG 0x02
  42. #define TERM_EN BIT(0)
  43. #define PCI_EN BIT(2)
  44. #define DES_BIT_LOCK_EN BIT(3)
  45. #define TX_POL BIT(5)
  46. #define INT_CTRL_REG 0x03
  47. #define BOUNDARY1_REG 0x10
  48. #define SPDSEL_SEL BIT(0)
  49. #define BOUNDARY3_REG 0x12
  50. #define TX_SPDSEL_GEN1_VAL 0
  51. #define TX_SPDSEL_GEN2_VAL 0x01
  52. #define TX_SPDSEL_GEN3_VAL 0x02
  53. #define RX_SPDSEL_GEN1_VAL 0
  54. #define RX_SPDSEL_GEN2_VAL (0x01 << 3)
  55. #define RX_SPDSEL_GEN3_VAL (0x02 << 3)
  56. #define PCIE_REG 0x16
  57. #define BUF_SEL_REG 0x20
  58. #define CONF_GEN_SEL_GEN3 0x02
  59. #define CONF_GEN_SEL_GEN2 0x01
  60. #define PD_VDDTFILTER BIT(4)
  61. #define TXBUF1_REG 0x21
  62. #define SWING_VAL 0x04
  63. #define SWING_VAL_GEN1 0x03
  64. #define PREEMPH_VAL (0x3 << 5)
  65. #define TXBUF2_REG 0x22
  66. #define TXSLEW_VAL 0x2
  67. #define TXSLEW_VAL_GEN1 0x4
  68. #define RXBUF_OFFSET_CTRL_REG 0x23
  69. #define RXBUF_REG 0x25
  70. #define SDTHRES_VAL 0x01
  71. #define EQ_ON3 (0x03 << 4)
  72. #define EQ_ON1 (0x01 << 4)
  73. #define COMP_CTRL1_REG 0x40
  74. #define START_COMSR BIT(0)
  75. #define START_COMZC BIT(1)
  76. #define COMSR_DONE BIT(2)
  77. #define COMZC_DONE BIT(3)
  78. #define COMP_AUTO_LOAD BIT(4)
  79. #define COMP_CTRL2_REG 0x41
  80. #define COMP_2MHZ_RAT_GEN1 0x1e
  81. #define COMP_2MHZ_RAT 0xf
  82. #define COMP_CTRL3_REG 0x42
  83. #define COMSR_COMP_REF 0x33
  84. #define COMP_IDLL_REG 0x47
  85. #define COMZC_IDLL 0x2a
  86. #define PLL_CTRL1_REG 0x50
  87. #define PLL_START_CAL BIT(0)
  88. #define BUF_EN BIT(2)
  89. #define SYNCHRO_TX BIT(3)
  90. #define SSC_EN BIT(6)
  91. #define CONFIG_PLL BIT(7)
  92. #define PLL_CTRL2_REG 0x51
  93. #define BYPASS_PLL_CAL BIT(1)
  94. #define PLL_RAT_REG 0x52
  95. #define PLL_SSC_STEP_MSB_REG 0x56
  96. #define PLL_SSC_STEP_MSB_VAL 0x03
  97. #define PLL_SSC_STEP_LSB_REG 0x57
  98. #define PLL_SSC_STEP_LSB_VAL 0x63
  99. #define PLL_SSC_PER_MSB_REG 0x58
  100. #define PLL_SSC_PER_MSB_VAL 0
  101. #define PLL_SSC_PER_LSB_REG 0x59
  102. #define PLL_SSC_PER_LSB_VAL 0xf1
  103. #define IDLL_TEST_REG 0x72
  104. #define START_CLK_HF BIT(6)
  105. #define DES_BITLOCK_REG 0x86
  106. #define BIT_LOCK_LEVEL 0x01
  107. #define BIT_LOCK_CNT_512 (0x03 << 5)
  108. struct miphy365x_phy {
  109. struct phy *phy;
  110. void __iomem *base;
  111. bool pcie_tx_pol_inv;
  112. bool sata_tx_pol_inv;
  113. u32 sata_gen;
  114. u64 ctrlreg;
  115. u8 type;
  116. };
  117. struct miphy365x_dev {
  118. struct device *dev;
  119. struct regmap *regmap;
  120. struct mutex miphy_mutex;
  121. struct miphy365x_phy **phys;
  122. };
  123. /*
  124. * These values are represented in Device tree. They are considered to be ABI
  125. * and although they can be extended any existing values must not change.
  126. */
  127. enum miphy_sata_gen {
  128. SATA_GEN1 = 1,
  129. SATA_GEN2,
  130. SATA_GEN3
  131. };
  132. static u8 rx_tx_spd[] = {
  133. 0, /* GEN0 doesn't exist. */
  134. TX_SPDSEL_GEN1_VAL | RX_SPDSEL_GEN1_VAL,
  135. TX_SPDSEL_GEN2_VAL | RX_SPDSEL_GEN2_VAL,
  136. TX_SPDSEL_GEN3_VAL | RX_SPDSEL_GEN3_VAL
  137. };
  138. /*
  139. * This function selects the system configuration,
  140. * either two SATA, one SATA and one PCIe, or two PCIe lanes.
  141. */
  142. static int miphy365x_set_path(struct miphy365x_phy *miphy_phy,
  143. struct miphy365x_dev *miphy_dev)
  144. {
  145. bool sata = (miphy_phy->type == MIPHY_TYPE_SATA);
  146. return regmap_update_bits(miphy_dev->regmap,
  147. (unsigned int)miphy_phy->ctrlreg,
  148. SYSCFG_SELECT_SATA_MASK,
  149. sata << SYSCFG_SELECT_SATA_POS);
  150. }
  151. static int miphy365x_init_pcie_port(struct miphy365x_phy *miphy_phy,
  152. struct miphy365x_dev *miphy_dev)
  153. {
  154. u8 val;
  155. if (miphy_phy->pcie_tx_pol_inv) {
  156. /* Invert Tx polarity and clear pci_txdetect_pol bit */
  157. val = TERM_EN | PCI_EN | DES_BIT_LOCK_EN | TX_POL;
  158. writeb_relaxed(val, miphy_phy->base + CTRL_REG);
  159. writeb_relaxed(0x00, miphy_phy->base + PCIE_REG);
  160. }
  161. return 0;
  162. }
  163. static inline int miphy365x_hfc_not_rdy(struct miphy365x_phy *miphy_phy,
  164. struct miphy365x_dev *miphy_dev)
  165. {
  166. unsigned long timeout = jiffies + msecs_to_jiffies(HFC_TIMEOUT);
  167. u8 mask = IDLL_RDY | PLL_RDY;
  168. u8 regval;
  169. do {
  170. regval = readb_relaxed(miphy_phy->base + STATUS_REG);
  171. if (!(regval & mask))
  172. return 0;
  173. usleep_range(2000, 2500);
  174. } while (time_before(jiffies, timeout));
  175. dev_err(miphy_dev->dev, "HFC ready timeout!\n");
  176. return -EBUSY;
  177. }
  178. static inline int miphy365x_rdy(struct miphy365x_phy *miphy_phy,
  179. struct miphy365x_dev *miphy_dev)
  180. {
  181. unsigned long timeout = jiffies + msecs_to_jiffies(HFC_TIMEOUT);
  182. u8 mask = IDLL_RDY | PLL_RDY;
  183. u8 regval;
  184. do {
  185. regval = readb_relaxed(miphy_phy->base + STATUS_REG);
  186. if ((regval & mask) == mask)
  187. return 0;
  188. usleep_range(2000, 2500);
  189. } while (time_before(jiffies, timeout));
  190. dev_err(miphy_dev->dev, "PHY not ready timeout!\n");
  191. return -EBUSY;
  192. }
  193. static inline void miphy365x_set_comp(struct miphy365x_phy *miphy_phy,
  194. struct miphy365x_dev *miphy_dev)
  195. {
  196. u8 val, mask;
  197. if (miphy_phy->sata_gen == SATA_GEN1)
  198. writeb_relaxed(COMP_2MHZ_RAT_GEN1,
  199. miphy_phy->base + COMP_CTRL2_REG);
  200. else
  201. writeb_relaxed(COMP_2MHZ_RAT,
  202. miphy_phy->base + COMP_CTRL2_REG);
  203. if (miphy_phy->sata_gen != SATA_GEN3) {
  204. writeb_relaxed(COMSR_COMP_REF,
  205. miphy_phy->base + COMP_CTRL3_REG);
  206. /*
  207. * Force VCO current to value defined by address 0x5A
  208. * and disable PCIe100Mref bit
  209. * Enable auto load compensation for pll_i_bias
  210. */
  211. writeb_relaxed(BYPASS_PLL_CAL, miphy_phy->base + PLL_CTRL2_REG);
  212. writeb_relaxed(COMZC_IDLL, miphy_phy->base + COMP_IDLL_REG);
  213. }
  214. /*
  215. * Force restart compensation and enable auto load
  216. * for Comzc_Tx, Comzc_Rx and Comsr on macro
  217. */
  218. val = START_COMSR | START_COMZC | COMP_AUTO_LOAD;
  219. writeb_relaxed(val, miphy_phy->base + COMP_CTRL1_REG);
  220. mask = COMSR_DONE | COMZC_DONE;
  221. while ((readb_relaxed(miphy_phy->base + COMP_CTRL1_REG) & mask) != mask)
  222. cpu_relax();
  223. }
  224. static inline void miphy365x_set_ssc(struct miphy365x_phy *miphy_phy,
  225. struct miphy365x_dev *miphy_dev)
  226. {
  227. u8 val;
  228. /*
  229. * SSC Settings. SSC will be enabled through Link
  230. * SSC Ampl. = 0.4%
  231. * SSC Freq = 31KHz
  232. */
  233. writeb_relaxed(PLL_SSC_STEP_MSB_VAL,
  234. miphy_phy->base + PLL_SSC_STEP_MSB_REG);
  235. writeb_relaxed(PLL_SSC_STEP_LSB_VAL,
  236. miphy_phy->base + PLL_SSC_STEP_LSB_REG);
  237. writeb_relaxed(PLL_SSC_PER_MSB_VAL,
  238. miphy_phy->base + PLL_SSC_PER_MSB_REG);
  239. writeb_relaxed(PLL_SSC_PER_LSB_VAL,
  240. miphy_phy->base + PLL_SSC_PER_LSB_REG);
  241. /* SSC Settings complete */
  242. if (miphy_phy->sata_gen == SATA_GEN1) {
  243. val = PLL_START_CAL | BUF_EN | SYNCHRO_TX | CONFIG_PLL;
  244. writeb_relaxed(val, miphy_phy->base + PLL_CTRL1_REG);
  245. } else {
  246. val = SSC_EN | PLL_START_CAL | BUF_EN | SYNCHRO_TX | CONFIG_PLL;
  247. writeb_relaxed(val, miphy_phy->base + PLL_CTRL1_REG);
  248. }
  249. }
  250. static int miphy365x_init_sata_port(struct miphy365x_phy *miphy_phy,
  251. struct miphy365x_dev *miphy_dev)
  252. {
  253. int ret;
  254. u8 val;
  255. /*
  256. * Force PHY macro reset, PLL calibration reset, PLL reset
  257. * and assert Deserializer Reset
  258. */
  259. val = RST_PLL | RST_PLL_CAL | RST_RX | RST_MACRO;
  260. writeb_relaxed(val, miphy_phy->base + RESET_REG);
  261. if (miphy_phy->sata_tx_pol_inv)
  262. writeb_relaxed(TX_POL, miphy_phy->base + CTRL_REG);
  263. /*
  264. * Force macro1 to use rx_lspd, tx_lspd
  265. * Force Rx_Clock on first I-DLL phase
  266. * Force Des in HP mode on macro, rx_lspd, tx_lspd for Gen2/3
  267. */
  268. writeb_relaxed(SPDSEL_SEL, miphy_phy->base + BOUNDARY1_REG);
  269. writeb_relaxed(START_CLK_HF, miphy_phy->base + IDLL_TEST_REG);
  270. val = rx_tx_spd[miphy_phy->sata_gen];
  271. writeb_relaxed(val, miphy_phy->base + BOUNDARY3_REG);
  272. /* Wait for HFC_READY = 0 */
  273. ret = miphy365x_hfc_not_rdy(miphy_phy, miphy_dev);
  274. if (ret)
  275. return ret;
  276. /* Compensation Recalibration */
  277. miphy365x_set_comp(miphy_phy, miphy_dev);
  278. switch (miphy_phy->sata_gen) {
  279. case SATA_GEN3:
  280. /*
  281. * TX Swing target 550-600mv peak to peak diff
  282. * Tx Slew target 90-110ps rising/falling time
  283. * Rx Eq ON3, Sigdet threshold SDTH1
  284. */
  285. val = PD_VDDTFILTER | CONF_GEN_SEL_GEN3;
  286. writeb_relaxed(val, miphy_phy->base + BUF_SEL_REG);
  287. val = SWING_VAL | PREEMPH_VAL;
  288. writeb_relaxed(val, miphy_phy->base + TXBUF1_REG);
  289. writeb_relaxed(TXSLEW_VAL, miphy_phy->base + TXBUF2_REG);
  290. writeb_relaxed(0x00, miphy_phy->base + RXBUF_OFFSET_CTRL_REG);
  291. val = SDTHRES_VAL | EQ_ON3;
  292. writeb_relaxed(val, miphy_phy->base + RXBUF_REG);
  293. break;
  294. case SATA_GEN2:
  295. /*
  296. * conf gen sel=0x1 to program Gen2 banked registers
  297. * VDDT filter ON
  298. * Tx Swing target 550-600mV peak-to-peak diff
  299. * Tx Slew target 90-110 ps rising/falling time
  300. * RX Equalization ON1, Sigdet threshold SDTH1
  301. */
  302. writeb_relaxed(CONF_GEN_SEL_GEN2,
  303. miphy_phy->base + BUF_SEL_REG);
  304. writeb_relaxed(SWING_VAL, miphy_phy->base + TXBUF1_REG);
  305. writeb_relaxed(TXSLEW_VAL, miphy_phy->base + TXBUF2_REG);
  306. val = SDTHRES_VAL | EQ_ON1;
  307. writeb_relaxed(val, miphy_phy->base + RXBUF_REG);
  308. break;
  309. case SATA_GEN1:
  310. /*
  311. * conf gen sel = 00b to program Gen1 banked registers
  312. * VDDT filter ON
  313. * Tx Swing target 500-550mV peak-to-peak diff
  314. * Tx Slew target120-140 ps rising/falling time
  315. */
  316. writeb_relaxed(PD_VDDTFILTER, miphy_phy->base + BUF_SEL_REG);
  317. writeb_relaxed(SWING_VAL_GEN1, miphy_phy->base + TXBUF1_REG);
  318. writeb_relaxed(TXSLEW_VAL_GEN1, miphy_phy->base + TXBUF2_REG);
  319. break;
  320. default:
  321. break;
  322. }
  323. /* Force Macro1 in partial mode & release pll cal reset */
  324. writeb_relaxed(RST_RX, miphy_phy->base + RESET_REG);
  325. usleep_range(100, 150);
  326. miphy365x_set_ssc(miphy_phy, miphy_dev);
  327. /* Wait for phy_ready */
  328. ret = miphy365x_rdy(miphy_phy, miphy_dev);
  329. if (ret)
  330. return ret;
  331. /*
  332. * Enable macro1 to use rx_lspd & tx_lspd
  333. * Release Rx_Clock on first I-DLL phase on macro1
  334. * Assert deserializer reset
  335. * des_bit_lock_en is set
  336. * bit lock detection strength
  337. * Deassert deserializer reset
  338. */
  339. writeb_relaxed(0x00, miphy_phy->base + BOUNDARY1_REG);
  340. writeb_relaxed(0x00, miphy_phy->base + IDLL_TEST_REG);
  341. writeb_relaxed(RST_RX, miphy_phy->base + RESET_REG);
  342. val = miphy_phy->sata_tx_pol_inv ?
  343. (TX_POL | DES_BIT_LOCK_EN) : DES_BIT_LOCK_EN;
  344. writeb_relaxed(val, miphy_phy->base + CTRL_REG);
  345. val = BIT_LOCK_CNT_512 | BIT_LOCK_LEVEL;
  346. writeb_relaxed(val, miphy_phy->base + DES_BITLOCK_REG);
  347. writeb_relaxed(0x00, miphy_phy->base + RESET_REG);
  348. return 0;
  349. }
  350. static int miphy365x_init(struct phy *phy)
  351. {
  352. struct miphy365x_phy *miphy_phy = phy_get_drvdata(phy);
  353. struct miphy365x_dev *miphy_dev = dev_get_drvdata(phy->dev.parent);
  354. int ret = 0;
  355. mutex_lock(&miphy_dev->miphy_mutex);
  356. ret = miphy365x_set_path(miphy_phy, miphy_dev);
  357. if (ret) {
  358. mutex_unlock(&miphy_dev->miphy_mutex);
  359. return ret;
  360. }
  361. /* Initialise Miphy for PCIe or SATA */
  362. if (miphy_phy->type == MIPHY_TYPE_PCIE)
  363. ret = miphy365x_init_pcie_port(miphy_phy, miphy_dev);
  364. else
  365. ret = miphy365x_init_sata_port(miphy_phy, miphy_dev);
  366. mutex_unlock(&miphy_dev->miphy_mutex);
  367. return ret;
  368. }
  369. int miphy365x_get_addr(struct device *dev, struct miphy365x_phy *miphy_phy,
  370. int index)
  371. {
  372. struct device_node *phynode = miphy_phy->phy->dev.of_node;
  373. const char *name;
  374. const __be32 *taddr;
  375. int type = miphy_phy->type;
  376. int ret;
  377. ret = of_property_read_string_index(phynode, "reg-names", index, &name);
  378. if (ret) {
  379. dev_err(dev, "no reg-names property not found\n");
  380. return ret;
  381. }
  382. if (!strncmp(name, "syscfg", 6)) {
  383. taddr = of_get_address(phynode, index, NULL, NULL);
  384. if (!taddr) {
  385. dev_err(dev, "failed to fetch syscfg address\n");
  386. return -EINVAL;
  387. }
  388. miphy_phy->ctrlreg = of_translate_address(phynode, taddr);
  389. if (miphy_phy->ctrlreg == OF_BAD_ADDR) {
  390. dev_err(dev, "failed to translate syscfg address\n");
  391. return -EINVAL;
  392. }
  393. return 0;
  394. }
  395. if (!((!strncmp(name, "sata", 4) && type == MIPHY_TYPE_SATA) ||
  396. (!strncmp(name, "pcie", 4) && type == MIPHY_TYPE_PCIE)))
  397. return 0;
  398. miphy_phy->base = of_iomap(phynode, index);
  399. if (!miphy_phy->base) {
  400. dev_err(dev, "Failed to map %s\n", phynode->full_name);
  401. return -EINVAL;
  402. }
  403. return 0;
  404. }
  405. static struct phy *miphy365x_xlate(struct device *dev,
  406. struct of_phandle_args *args)
  407. {
  408. struct miphy365x_dev *miphy_dev = dev_get_drvdata(dev);
  409. struct miphy365x_phy *miphy_phy = NULL;
  410. struct device_node *phynode = args->np;
  411. int ret, index;
  412. if (!of_device_is_available(phynode)) {
  413. dev_warn(dev, "Requested PHY is disabled\n");
  414. return ERR_PTR(-ENODEV);
  415. }
  416. if (args->args_count != 1) {
  417. dev_err(dev, "Invalid number of cells in 'phy' property\n");
  418. return ERR_PTR(-EINVAL);
  419. }
  420. for (index = 0; index < of_get_child_count(dev->of_node); index++)
  421. if (phynode == miphy_dev->phys[index]->phy->dev.of_node) {
  422. miphy_phy = miphy_dev->phys[index];
  423. break;
  424. }
  425. if (!miphy_phy) {
  426. dev_err(dev, "Failed to find appropriate phy\n");
  427. return ERR_PTR(-EINVAL);
  428. }
  429. miphy_phy->type = args->args[0];
  430. if (!(miphy_phy->type == MIPHY_TYPE_SATA ||
  431. miphy_phy->type == MIPHY_TYPE_PCIE)) {
  432. dev_err(dev, "Unsupported device type: %d\n", miphy_phy->type);
  433. return ERR_PTR(-EINVAL);
  434. }
  435. /* Each port handles SATA and PCIE - third entry is always sysconf. */
  436. for (index = 0; index < 3; index++) {
  437. ret = miphy365x_get_addr(dev, miphy_phy, index);
  438. if (ret < 0)
  439. return ERR_PTR(ret);
  440. }
  441. return miphy_phy->phy;
  442. }
  443. static struct phy_ops miphy365x_ops = {
  444. .init = miphy365x_init,
  445. .owner = THIS_MODULE,
  446. };
  447. static int miphy365x_of_probe(struct device_node *phynode,
  448. struct miphy365x_phy *miphy_phy)
  449. {
  450. of_property_read_u32(phynode, "st,sata-gen", &miphy_phy->sata_gen);
  451. if (!miphy_phy->sata_gen)
  452. miphy_phy->sata_gen = SATA_GEN1;
  453. miphy_phy->pcie_tx_pol_inv =
  454. of_property_read_bool(phynode, "st,pcie-tx-pol-inv");
  455. miphy_phy->sata_tx_pol_inv =
  456. of_property_read_bool(phynode, "st,sata-tx-pol-inv");
  457. return 0;
  458. }
  459. static int miphy365x_probe(struct platform_device *pdev)
  460. {
  461. struct device_node *child, *np = pdev->dev.of_node;
  462. struct miphy365x_dev *miphy_dev;
  463. struct phy_provider *provider;
  464. struct phy *phy;
  465. int chancount, port = 0;
  466. int ret;
  467. miphy_dev = devm_kzalloc(&pdev->dev, sizeof(*miphy_dev), GFP_KERNEL);
  468. if (!miphy_dev)
  469. return -ENOMEM;
  470. chancount = of_get_child_count(np);
  471. miphy_dev->phys = devm_kzalloc(&pdev->dev, sizeof(phy) * chancount,
  472. GFP_KERNEL);
  473. if (!miphy_dev->phys)
  474. return -ENOMEM;
  475. miphy_dev->regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
  476. if (IS_ERR(miphy_dev->regmap)) {
  477. dev_err(miphy_dev->dev, "No syscfg phandle specified\n");
  478. return PTR_ERR(miphy_dev->regmap);
  479. }
  480. miphy_dev->dev = &pdev->dev;
  481. dev_set_drvdata(&pdev->dev, miphy_dev);
  482. mutex_init(&miphy_dev->miphy_mutex);
  483. for_each_child_of_node(np, child) {
  484. struct miphy365x_phy *miphy_phy;
  485. miphy_phy = devm_kzalloc(&pdev->dev, sizeof(*miphy_phy),
  486. GFP_KERNEL);
  487. if (!miphy_phy)
  488. return -ENOMEM;
  489. miphy_dev->phys[port] = miphy_phy;
  490. phy = devm_phy_create(&pdev->dev, child, &miphy365x_ops);
  491. if (IS_ERR(phy)) {
  492. dev_err(&pdev->dev, "failed to create PHY\n");
  493. return PTR_ERR(phy);
  494. }
  495. miphy_dev->phys[port]->phy = phy;
  496. ret = miphy365x_of_probe(child, miphy_phy);
  497. if (ret)
  498. return ret;
  499. phy_set_drvdata(phy, miphy_dev->phys[port]);
  500. port++;
  501. }
  502. provider = devm_of_phy_provider_register(&pdev->dev, miphy365x_xlate);
  503. return PTR_ERR_OR_ZERO(provider);
  504. }
  505. static const struct of_device_id miphy365x_of_match[] = {
  506. { .compatible = "st,miphy365x-phy", },
  507. { },
  508. };
  509. MODULE_DEVICE_TABLE(of, miphy365x_of_match);
  510. static struct platform_driver miphy365x_driver = {
  511. .probe = miphy365x_probe,
  512. .driver = {
  513. .name = "miphy365x-phy",
  514. .of_match_table = miphy365x_of_match,
  515. }
  516. };
  517. module_platform_driver(miphy365x_driver);
  518. MODULE_AUTHOR("Alexandre Torgue <alexandre.torgue@st.com>");
  519. MODULE_DESCRIPTION("STMicroelectronics miphy365x driver");
  520. MODULE_LICENSE("GPL v2");