1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2015 - 2023 Beijing WangXun Technology Co., Ltd. */ 3 4 #include <linux/gpio/property.h> 5 #include <linux/clk-provider.h> 6 #include <linux/clkdev.h> 7 #include <linux/i2c.h> 8 #include <linux/pci.h> 9 #include <linux/platform_device.h> 10 #include <linux/regmap.h> 11 12 #include "../libwx/wx_type.h" 13 #include "txgbe_type.h" 14 #include "txgbe_phy.h" 15 16 static int txgbe_swnodes_register(struct txgbe *txgbe) 17 { 18 struct txgbe_nodes *nodes = &txgbe->nodes; 19 struct pci_dev *pdev = txgbe->wx->pdev; 20 struct software_node *swnodes; 21 u32 id; 22 23 id = (pdev->bus->number << 8) | pdev->devfn; 24 25 snprintf(nodes->gpio_name, sizeof(nodes->gpio_name), "txgbe_gpio-%x", id); 26 snprintf(nodes->i2c_name, sizeof(nodes->i2c_name), "txgbe_i2c-%x", id); 27 snprintf(nodes->sfp_name, sizeof(nodes->sfp_name), "txgbe_sfp-%x", id); 28 snprintf(nodes->phylink_name, sizeof(nodes->phylink_name), "txgbe_phylink-%x", id); 29 30 swnodes = nodes->swnodes; 31 32 /* GPIO 0: tx fault 33 * GPIO 1: tx disable 34 * GPIO 2: sfp module absent 35 * GPIO 3: rx signal lost 36 * GPIO 4: rate select, 1G(0) 10G(1) 37 * GPIO 5: rate select, 1G(0) 10G(1) 38 */ 39 nodes->gpio_props[0] = PROPERTY_ENTRY_STRING("pinctrl-names", "default"); 40 swnodes[SWNODE_GPIO] = NODE_PROP(nodes->gpio_name, nodes->gpio_props); 41 nodes->gpio0_ref[0] = SOFTWARE_NODE_REFERENCE(&swnodes[SWNODE_GPIO], 0, GPIO_ACTIVE_HIGH); 42 nodes->gpio1_ref[0] = SOFTWARE_NODE_REFERENCE(&swnodes[SWNODE_GPIO], 1, GPIO_ACTIVE_HIGH); 43 nodes->gpio2_ref[0] = SOFTWARE_NODE_REFERENCE(&swnodes[SWNODE_GPIO], 2, GPIO_ACTIVE_LOW); 44 nodes->gpio3_ref[0] = SOFTWARE_NODE_REFERENCE(&swnodes[SWNODE_GPIO], 3, GPIO_ACTIVE_HIGH); 45 nodes->gpio4_ref[0] = SOFTWARE_NODE_REFERENCE(&swnodes[SWNODE_GPIO], 4, GPIO_ACTIVE_HIGH); 46 nodes->gpio5_ref[0] = SOFTWARE_NODE_REFERENCE(&swnodes[SWNODE_GPIO], 5, GPIO_ACTIVE_HIGH); 47 48 nodes->i2c_props[0] = PROPERTY_ENTRY_STRING("compatible", "snps,designware-i2c"); 49 nodes->i2c_props[1] = PROPERTY_ENTRY_BOOL("wx,i2c-snps-model"); 50 nodes->i2c_props[2] = PROPERTY_ENTRY_U32("clock-frequency", I2C_MAX_STANDARD_MODE_FREQ); 51 swnodes[SWNODE_I2C] = NODE_PROP(nodes->i2c_name, nodes->i2c_props); 52 nodes->i2c_ref[0] = SOFTWARE_NODE_REFERENCE(&swnodes[SWNODE_I2C]); 53 54 nodes->sfp_props[0] = PROPERTY_ENTRY_STRING("compatible", "sff,sfp"); 55 nodes->sfp_props[1] = PROPERTY_ENTRY_REF_ARRAY("i2c-bus", nodes->i2c_ref); 56 nodes->sfp_props[2] = PROPERTY_ENTRY_REF_ARRAY("tx-fault-gpios", nodes->gpio0_ref); 57 nodes->sfp_props[3] = PROPERTY_ENTRY_REF_ARRAY("tx-disable-gpios", nodes->gpio1_ref); 58 nodes->sfp_props[4] = PROPERTY_ENTRY_REF_ARRAY("mod-def0-gpios", nodes->gpio2_ref); 59 nodes->sfp_props[5] = PROPERTY_ENTRY_REF_ARRAY("los-gpios", nodes->gpio3_ref); 60 nodes->sfp_props[6] = PROPERTY_ENTRY_REF_ARRAY("rate-select1-gpios", nodes->gpio4_ref); 61 nodes->sfp_props[7] = PROPERTY_ENTRY_REF_ARRAY("rate-select0-gpios", nodes->gpio5_ref); 62 swnodes[SWNODE_SFP] = NODE_PROP(nodes->sfp_name, nodes->sfp_props); 63 nodes->sfp_ref[0] = SOFTWARE_NODE_REFERENCE(&swnodes[SWNODE_SFP]); 64 65 nodes->phylink_props[0] = PROPERTY_ENTRY_STRING("managed", "in-band-status"); 66 nodes->phylink_props[1] = PROPERTY_ENTRY_REF_ARRAY("sfp", nodes->sfp_ref); 67 swnodes[SWNODE_PHYLINK] = NODE_PROP(nodes->phylink_name, nodes->phylink_props); 68 69 nodes->group[SWNODE_GPIO] = &swnodes[SWNODE_GPIO]; 70 nodes->group[SWNODE_I2C] = &swnodes[SWNODE_I2C]; 71 nodes->group[SWNODE_SFP] = &swnodes[SWNODE_SFP]; 72 nodes->group[SWNODE_PHYLINK] = &swnodes[SWNODE_PHYLINK]; 73 74 return software_node_register_node_group(nodes->group); 75 } 76 77 static int txgbe_clock_register(struct txgbe *txgbe) 78 { 79 struct pci_dev *pdev = txgbe->wx->pdev; 80 struct clk_lookup *clock; 81 char clk_name[32]; 82 struct clk *clk; 83 84 snprintf(clk_name, sizeof(clk_name), "i2c_designware.%d", 85 (pdev->bus->number << 8) | pdev->devfn); 86 87 clk = clk_register_fixed_rate(NULL, clk_name, NULL, 0, 156250000); 88 if (IS_ERR(clk)) 89 return PTR_ERR(clk); 90 91 clock = clkdev_create(clk, NULL, clk_name); 92 if (!clock) { 93 clk_unregister(clk); 94 return -ENOMEM; 95 } 96 97 txgbe->clk = clk; 98 txgbe->clock = clock; 99 100 return 0; 101 } 102 103 static int txgbe_i2c_read(void *context, unsigned int reg, unsigned int *val) 104 { 105 struct wx *wx = context; 106 107 *val = rd32(wx, reg + TXGBE_I2C_BASE); 108 109 return 0; 110 } 111 112 static int txgbe_i2c_write(void *context, unsigned int reg, unsigned int val) 113 { 114 struct wx *wx = context; 115 116 wr32(wx, reg + TXGBE_I2C_BASE, val); 117 118 return 0; 119 } 120 121 static const struct regmap_config i2c_regmap_config = { 122 .reg_bits = 32, 123 .val_bits = 32, 124 .reg_read = txgbe_i2c_read, 125 .reg_write = txgbe_i2c_write, 126 .fast_io = true, 127 }; 128 129 static int txgbe_i2c_register(struct txgbe *txgbe) 130 { 131 struct platform_device_info info = {}; 132 struct platform_device *i2c_dev; 133 struct regmap *i2c_regmap; 134 struct pci_dev *pdev; 135 struct wx *wx; 136 137 wx = txgbe->wx; 138 pdev = wx->pdev; 139 i2c_regmap = devm_regmap_init(&pdev->dev, NULL, wx, &i2c_regmap_config); 140 if (IS_ERR(i2c_regmap)) { 141 wx_err(wx, "failed to init I2C regmap\n"); 142 return PTR_ERR(i2c_regmap); 143 } 144 145 info.parent = &pdev->dev; 146 info.fwnode = software_node_fwnode(txgbe->nodes.group[SWNODE_I2C]); 147 info.name = "i2c_designware"; 148 info.id = (pdev->bus->number << 8) | pdev->devfn; 149 150 info.res = &DEFINE_RES_IRQ(pdev->irq); 151 info.num_res = 1; 152 i2c_dev = platform_device_register_full(&info); 153 if (IS_ERR(i2c_dev)) 154 return PTR_ERR(i2c_dev); 155 156 txgbe->i2c_dev = i2c_dev; 157 158 return 0; 159 } 160 161 static int txgbe_sfp_register(struct txgbe *txgbe) 162 { 163 struct pci_dev *pdev = txgbe->wx->pdev; 164 struct platform_device_info info = {}; 165 struct platform_device *sfp_dev; 166 167 info.parent = &pdev->dev; 168 info.fwnode = software_node_fwnode(txgbe->nodes.group[SWNODE_SFP]); 169 info.name = "sfp"; 170 info.id = (pdev->bus->number << 8) | pdev->devfn; 171 sfp_dev = platform_device_register_full(&info); 172 if (IS_ERR(sfp_dev)) 173 return PTR_ERR(sfp_dev); 174 175 txgbe->sfp_dev = sfp_dev; 176 177 return 0; 178 } 179 180 int txgbe_init_phy(struct txgbe *txgbe) 181 { 182 int ret; 183 184 ret = txgbe_swnodes_register(txgbe); 185 if (ret) { 186 wx_err(txgbe->wx, "failed to register software nodes\n"); 187 return ret; 188 } 189 190 ret = txgbe_clock_register(txgbe); 191 if (ret) { 192 wx_err(txgbe->wx, "failed to register clock: %d\n", ret); 193 goto err_unregister_swnode; 194 } 195 196 ret = txgbe_i2c_register(txgbe); 197 if (ret) { 198 wx_err(txgbe->wx, "failed to init i2c interface: %d\n", ret); 199 goto err_unregister_clk; 200 } 201 202 ret = txgbe_sfp_register(txgbe); 203 if (ret) { 204 wx_err(txgbe->wx, "failed to register sfp\n"); 205 goto err_unregister_i2c; 206 } 207 208 return 0; 209 210 err_unregister_i2c: 211 platform_device_unregister(txgbe->i2c_dev); 212 err_unregister_clk: 213 clkdev_drop(txgbe->clock); 214 clk_unregister(txgbe->clk); 215 err_unregister_swnode: 216 software_node_unregister_node_group(txgbe->nodes.group); 217 218 return ret; 219 } 220 221 void txgbe_remove_phy(struct txgbe *txgbe) 222 { 223 platform_device_unregister(txgbe->sfp_dev); 224 platform_device_unregister(txgbe->i2c_dev); 225 clkdev_drop(txgbe->clock); 226 clk_unregister(txgbe->clk); 227 software_node_unregister_node_group(txgbe->nodes.group); 228 } 229