comphy_core.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright (C) 2015-2016 Marvell International Ltd.
  3. *
  4. * Copyright (C) 2016 Stefan Roese <sr@denx.de>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <dm.h>
  10. #include <fdtdec.h>
  11. #include <linux/errno.h>
  12. #include <asm/io.h>
  13. #include "comphy.h"
  14. #define COMPHY_MAX_CHIP 4
  15. DECLARE_GLOBAL_DATA_PTR;
  16. static char *get_speed_string(u32 speed)
  17. {
  18. char *speed_strings[] = {"1.25 Gbps", "1.5 Gbps", "2.5 Gbps",
  19. "3.0 Gbps", "3.125 Gbps", "5 Gbps", "6 Gbps",
  20. "6.25 Gbps", "10.31 Gbps" };
  21. if (speed < 0 || speed > PHY_SPEED_MAX)
  22. return "invalid";
  23. return speed_strings[speed];
  24. }
  25. static char *get_type_string(u32 type)
  26. {
  27. char *type_strings[] = {"UNCONNECTED", "PEX0", "PEX1", "PEX2", "PEX3",
  28. "SATA0", "SATA1", "SATA2", "SATA3", "SGMII0",
  29. "SGMII1", "SGMII2", "SGMII3", "QSGMII",
  30. "USB3_HOST0", "USB3_HOST1", "USB3_DEVICE",
  31. "XAUI0", "XAUI1", "XAUI2", "XAUI3",
  32. "RXAUI0", "RXAUI1", "SFI", "IGNORE"};
  33. if (type < 0 || type > PHY_TYPE_MAX)
  34. return "invalid";
  35. return type_strings[type];
  36. }
  37. void reg_set(void __iomem *addr, u32 data, u32 mask)
  38. {
  39. debug("Write to address = %#010lx, data = %#010x (mask = %#010x) - ",
  40. (unsigned long)addr, data, mask);
  41. debug("old value = %#010x ==> ", readl(addr));
  42. reg_set_silent(addr, data, mask);
  43. debug("new value %#010x\n", readl(addr));
  44. }
  45. void reg_set_silent(void __iomem *addr, u32 data, u32 mask)
  46. {
  47. u32 reg_data;
  48. reg_data = readl(addr);
  49. reg_data &= ~mask;
  50. reg_data |= data;
  51. writel(reg_data, addr);
  52. }
  53. void reg_set16(void __iomem *addr, u16 data, u16 mask)
  54. {
  55. debug("Write to address = %#010lx, data = %#06x (mask = %#06x) - ",
  56. (unsigned long)addr, data, mask);
  57. debug("old value = %#06x ==> ", readw(addr));
  58. reg_set_silent16(addr, data, mask);
  59. debug("new value %#06x\n", readw(addr));
  60. }
  61. void reg_set_silent16(void __iomem *addr, u16 data, u16 mask)
  62. {
  63. u16 reg_data;
  64. reg_data = readw(addr);
  65. reg_data &= ~mask;
  66. reg_data |= data;
  67. writew(reg_data, addr);
  68. }
  69. void comphy_print(struct chip_serdes_phy_config *chip_cfg,
  70. struct comphy_map *comphy_map_data)
  71. {
  72. u32 lane;
  73. for (lane = 0; lane < chip_cfg->comphy_lanes_count;
  74. lane++, comphy_map_data++) {
  75. if (comphy_map_data->speed == PHY_SPEED_INVALID) {
  76. printf("Comphy-%d: %-13s\n", lane,
  77. get_type_string(comphy_map_data->type));
  78. } else {
  79. printf("Comphy-%d: %-13s %-10s\n", lane,
  80. get_type_string(comphy_map_data->type),
  81. get_speed_string(comphy_map_data->speed));
  82. }
  83. }
  84. }
  85. static int comphy_probe(struct udevice *dev)
  86. {
  87. const void *blob = gd->fdt_blob;
  88. int node = dev_of_offset(dev);
  89. struct chip_serdes_phy_config *chip_cfg = dev_get_priv(dev);
  90. struct comphy_map comphy_map_data[MAX_LANE_OPTIONS];
  91. int subnode;
  92. int lane;
  93. int last_idx = 0;
  94. static int current_idx;
  95. /* Save base addresses for later use */
  96. chip_cfg->comphy_base_addr = (void *)devfdt_get_addr_index(dev, 0);
  97. if (IS_ERR(chip_cfg->comphy_base_addr))
  98. return PTR_ERR(chip_cfg->comphy_base_addr);
  99. chip_cfg->hpipe3_base_addr = (void *)devfdt_get_addr_index(dev, 1);
  100. if (IS_ERR(chip_cfg->hpipe3_base_addr))
  101. return PTR_ERR(chip_cfg->hpipe3_base_addr);
  102. chip_cfg->comphy_lanes_count = fdtdec_get_int(blob, node,
  103. "max-lanes", 0);
  104. if (chip_cfg->comphy_lanes_count <= 0) {
  105. dev_err(&dev->dev, "comphy max lanes is wrong\n");
  106. return -EINVAL;
  107. }
  108. chip_cfg->comphy_mux_bitcount = fdtdec_get_int(blob, node,
  109. "mux-bitcount", 0);
  110. if (chip_cfg->comphy_mux_bitcount <= 0) {
  111. dev_err(&dev->dev, "comphy mux bit count is wrong\n");
  112. return -EINVAL;
  113. }
  114. if (device_is_compatible(dev, "marvell,comphy-armada-3700"))
  115. chip_cfg->ptr_comphy_chip_init = comphy_a3700_init;
  116. if (device_is_compatible(dev, "marvell,comphy-cp110"))
  117. chip_cfg->ptr_comphy_chip_init = comphy_cp110_init;
  118. /*
  119. * Bail out if no chip_init function is defined, e.g. no
  120. * compatible node is found
  121. */
  122. if (!chip_cfg->ptr_comphy_chip_init) {
  123. dev_err(&dev->dev, "comphy: No compatible DT node found\n");
  124. return -ENODEV;
  125. }
  126. lane = 0;
  127. fdt_for_each_subnode(subnode, blob, node) {
  128. /* Skip disabled ports */
  129. if (!fdtdec_get_is_enabled(blob, subnode))
  130. continue;
  131. comphy_map_data[lane].speed = fdtdec_get_int(
  132. blob, subnode, "phy-speed", PHY_TYPE_INVALID);
  133. comphy_map_data[lane].type = fdtdec_get_int(
  134. blob, subnode, "phy-type", PHY_SPEED_INVALID);
  135. comphy_map_data[lane].invert = fdtdec_get_int(
  136. blob, subnode, "phy-invert", PHY_POLARITY_NO_INVERT);
  137. comphy_map_data[lane].clk_src = fdtdec_get_bool(blob, subnode,
  138. "clk-src");
  139. comphy_map_data[lane].end_point = fdtdec_get_bool(blob, subnode,
  140. "end_point");
  141. if (comphy_map_data[lane].type == PHY_TYPE_INVALID) {
  142. printf("no phy type for lane %d, setting lane as unconnected\n",
  143. lane + 1);
  144. }
  145. lane++;
  146. }
  147. /* Save CP index for MultiCP devices (A8K) */
  148. chip_cfg->cp_index = current_idx++;
  149. /* PHY power UP sequence */
  150. chip_cfg->ptr_comphy_chip_init(chip_cfg, comphy_map_data);
  151. /* PHY print SerDes status */
  152. if (of_machine_is_compatible("marvell,armada8040"))
  153. printf("Comphy chip #%d:\n", chip_cfg->cp_index);
  154. comphy_print(chip_cfg, comphy_map_data);
  155. /*
  156. * Only run the dedicated PHY init code once, in the last PHY init call
  157. */
  158. if (of_machine_is_compatible("marvell,armada8040"))
  159. last_idx = 1;
  160. if (chip_cfg->cp_index == last_idx) {
  161. /* Initialize dedicated PHYs (not muxed SerDes lanes) */
  162. comphy_dedicated_phys_init();
  163. }
  164. return 0;
  165. }
  166. static const struct udevice_id comphy_ids[] = {
  167. { .compatible = "marvell,mvebu-comphy" },
  168. { }
  169. };
  170. U_BOOT_DRIVER(mvebu_comphy) = {
  171. .name = "mvebu_comphy",
  172. .id = UCLASS_MISC,
  173. .of_match = comphy_ids,
  174. .probe = comphy_probe,
  175. .priv_auto_alloc_size = sizeof(struct chip_serdes_phy_config),
  176. };