xref: /rk3399_rockchip-uboot/drivers/phy/marvell/comphy_core.c (revision 911f3aef357ca02ac0d06fdce6a7a3081594f272)
13335786aSStefan Roese /*
23335786aSStefan Roese  * Copyright (C) 2015-2016 Marvell International Ltd.
33335786aSStefan Roese  *
43335786aSStefan Roese  * Copyright (C) 2016 Stefan Roese <sr@denx.de>
53335786aSStefan Roese  *
63335786aSStefan Roese  * SPDX-License-Identifier:	GPL-2.0+
73335786aSStefan Roese  */
83335786aSStefan Roese 
93335786aSStefan Roese #include <common.h>
103335786aSStefan Roese #include <dm.h>
113335786aSStefan Roese #include <fdtdec.h>
123335786aSStefan Roese #include <linux/errno.h>
133335786aSStefan Roese #include <asm/io.h>
143335786aSStefan Roese 
153335786aSStefan Roese #include "comphy.h"
163335786aSStefan Roese 
173335786aSStefan Roese #define COMPHY_MAX_CHIP 4
183335786aSStefan Roese 
193335786aSStefan Roese DECLARE_GLOBAL_DATA_PTR;
203335786aSStefan Roese 
get_speed_string(u32 speed)213335786aSStefan Roese static char *get_speed_string(u32 speed)
223335786aSStefan Roese {
233335786aSStefan Roese 	char *speed_strings[] = {"1.25 Gbps", "1.5 Gbps", "2.5 Gbps",
243335786aSStefan Roese 				 "3.0 Gbps", "3.125 Gbps", "5 Gbps", "6 Gbps",
253335786aSStefan Roese 				 "6.25 Gbps", "10.31 Gbps" };
263335786aSStefan Roese 
273335786aSStefan Roese 	if (speed < 0 || speed > PHY_SPEED_MAX)
283335786aSStefan Roese 		return "invalid";
293335786aSStefan Roese 
303335786aSStefan Roese 	return speed_strings[speed];
313335786aSStefan Roese }
323335786aSStefan Roese 
get_type_string(u32 type)333335786aSStefan Roese static char *get_type_string(u32 type)
343335786aSStefan Roese {
353335786aSStefan Roese 	char *type_strings[] = {"UNCONNECTED", "PEX0", "PEX1", "PEX2", "PEX3",
363335786aSStefan Roese 				"SATA0", "SATA1", "SATA2", "SATA3", "SGMII0",
373335786aSStefan Roese 				"SGMII1", "SGMII2", "SGMII3", "QSGMII",
383335786aSStefan Roese 				"USB3_HOST0", "USB3_HOST1", "USB3_DEVICE",
393335786aSStefan Roese 				"XAUI0", "XAUI1", "XAUI2", "XAUI3",
406ecc0b1cSStefan Roese 				"RXAUI0", "RXAUI1", "SFI", "IGNORE"};
413335786aSStefan Roese 
423335786aSStefan Roese 	if (type < 0 || type > PHY_TYPE_MAX)
433335786aSStefan Roese 		return "invalid";
443335786aSStefan Roese 
453335786aSStefan Roese 	return type_strings[type];
463335786aSStefan Roese }
473335786aSStefan Roese 
reg_set(void __iomem * addr,u32 data,u32 mask)483335786aSStefan Roese void reg_set(void __iomem *addr, u32 data, u32 mask)
493335786aSStefan Roese {
503335786aSStefan Roese 	debug("Write to address = %#010lx, data = %#010x (mask = %#010x) - ",
513335786aSStefan Roese 	      (unsigned long)addr, data, mask);
523335786aSStefan Roese 	debug("old value = %#010x ==> ", readl(addr));
533335786aSStefan Roese 	reg_set_silent(addr, data, mask);
543335786aSStefan Roese 	debug("new value %#010x\n", readl(addr));
553335786aSStefan Roese }
563335786aSStefan Roese 
reg_set_silent(void __iomem * addr,u32 data,u32 mask)573335786aSStefan Roese void reg_set_silent(void __iomem *addr, u32 data, u32 mask)
583335786aSStefan Roese {
593335786aSStefan Roese 	u32 reg_data;
603335786aSStefan Roese 
613335786aSStefan Roese 	reg_data = readl(addr);
623335786aSStefan Roese 	reg_data &= ~mask;
633335786aSStefan Roese 	reg_data |= data;
643335786aSStefan Roese 	writel(reg_data, addr);
653335786aSStefan Roese }
663335786aSStefan Roese 
reg_set16(void __iomem * addr,u16 data,u16 mask)673335786aSStefan Roese void reg_set16(void __iomem *addr, u16 data, u16 mask)
683335786aSStefan Roese {
693335786aSStefan Roese 	debug("Write to address = %#010lx, data = %#06x (mask = %#06x) - ",
703335786aSStefan Roese 	      (unsigned long)addr, data, mask);
713335786aSStefan Roese 	debug("old value = %#06x ==> ", readw(addr));
723335786aSStefan Roese 	reg_set_silent16(addr, data, mask);
733335786aSStefan Roese 	debug("new value %#06x\n", readw(addr));
743335786aSStefan Roese }
753335786aSStefan Roese 
reg_set_silent16(void __iomem * addr,u16 data,u16 mask)763335786aSStefan Roese void reg_set_silent16(void __iomem *addr, u16 data, u16 mask)
773335786aSStefan Roese {
783335786aSStefan Roese 	u16 reg_data;
793335786aSStefan Roese 
803335786aSStefan Roese 	reg_data = readw(addr);
813335786aSStefan Roese 	reg_data &= ~mask;
823335786aSStefan Roese 	reg_data |= data;
833335786aSStefan Roese 	writew(reg_data, addr);
843335786aSStefan Roese }
853335786aSStefan Roese 
comphy_print(struct chip_serdes_phy_config * chip_cfg,struct comphy_map * comphy_map_data)863335786aSStefan Roese void comphy_print(struct chip_serdes_phy_config *chip_cfg,
873335786aSStefan Roese 		  struct comphy_map *comphy_map_data)
883335786aSStefan Roese {
893335786aSStefan Roese 	u32 lane;
903335786aSStefan Roese 
913335786aSStefan Roese 	for (lane = 0; lane < chip_cfg->comphy_lanes_count;
923335786aSStefan Roese 	     lane++, comphy_map_data++) {
933335786aSStefan Roese 		if (comphy_map_data->speed == PHY_SPEED_INVALID) {
943335786aSStefan Roese 			printf("Comphy-%d: %-13s\n", lane,
953335786aSStefan Roese 			       get_type_string(comphy_map_data->type));
963335786aSStefan Roese 		} else {
973335786aSStefan Roese 			printf("Comphy-%d: %-13s %-10s\n", lane,
983335786aSStefan Roese 			       get_type_string(comphy_map_data->type),
993335786aSStefan Roese 			       get_speed_string(comphy_map_data->speed));
1003335786aSStefan Roese 		}
1013335786aSStefan Roese 	}
1023335786aSStefan Roese }
1033335786aSStefan Roese 
comphy_probe(struct udevice * dev)1043335786aSStefan Roese static int comphy_probe(struct udevice *dev)
1053335786aSStefan Roese {
1063335786aSStefan Roese 	const void *blob = gd->fdt_blob;
107e160f7d4SSimon Glass 	int node = dev_of_offset(dev);
1083335786aSStefan Roese 	struct chip_serdes_phy_config *chip_cfg = dev_get_priv(dev);
1093335786aSStefan Roese 	struct comphy_map comphy_map_data[MAX_LANE_OPTIONS];
1103335786aSStefan Roese 	int subnode;
1113335786aSStefan Roese 	int lane;
112e8c3156eSStefan Roese 	int last_idx = 0;
11318797ad6SKonstantin Porotchkin 	static int current_idx;
1143335786aSStefan Roese 
1153335786aSStefan Roese 	/* Save base addresses for later use */
116a821c4afSSimon Glass 	chip_cfg->comphy_base_addr = (void *)devfdt_get_addr_index(dev, 0);
1173335786aSStefan Roese 	if (IS_ERR(chip_cfg->comphy_base_addr))
1183335786aSStefan Roese 		return PTR_ERR(chip_cfg->comphy_base_addr);
1193335786aSStefan Roese 
120a821c4afSSimon Glass 	chip_cfg->hpipe3_base_addr = (void *)devfdt_get_addr_index(dev, 1);
1213335786aSStefan Roese 	if (IS_ERR(chip_cfg->hpipe3_base_addr))
1223335786aSStefan Roese 		return PTR_ERR(chip_cfg->hpipe3_base_addr);
1233335786aSStefan Roese 
1243335786aSStefan Roese 	chip_cfg->comphy_lanes_count = fdtdec_get_int(blob, node,
1253335786aSStefan Roese 						      "max-lanes", 0);
1263335786aSStefan Roese 	if (chip_cfg->comphy_lanes_count <= 0) {
1273335786aSStefan Roese 		dev_err(&dev->dev, "comphy max lanes is wrong\n");
1283335786aSStefan Roese 		return -EINVAL;
1293335786aSStefan Roese 	}
1303335786aSStefan Roese 
1313335786aSStefan Roese 	chip_cfg->comphy_mux_bitcount = fdtdec_get_int(blob, node,
1323335786aSStefan Roese 						       "mux-bitcount", 0);
1333335786aSStefan Roese 	if (chip_cfg->comphy_mux_bitcount <= 0) {
1343335786aSStefan Roese 		dev_err(&dev->dev, "comphy mux bit count is wrong\n");
1353335786aSStefan Roese 		return -EINVAL;
1363335786aSStefan Roese 	}
1373335786aSStefan Roese 
138*911f3aefSSimon Glass 	if (device_is_compatible(dev, "marvell,comphy-armada-3700"))
1393335786aSStefan Roese 		chip_cfg->ptr_comphy_chip_init = comphy_a3700_init;
1403335786aSStefan Roese 
141*911f3aefSSimon Glass 	if (device_is_compatible(dev, "marvell,comphy-cp110"))
142c0132f60SStefan Roese 		chip_cfg->ptr_comphy_chip_init = comphy_cp110_init;
143c0132f60SStefan Roese 
1443335786aSStefan Roese 	/*
1453335786aSStefan Roese 	 * Bail out if no chip_init function is defined, e.g. no
1463335786aSStefan Roese 	 * compatible node is found
1473335786aSStefan Roese 	 */
1483335786aSStefan Roese 	if (!chip_cfg->ptr_comphy_chip_init) {
1493335786aSStefan Roese 		dev_err(&dev->dev, "comphy: No compatible DT node found\n");
1503335786aSStefan Roese 		return -ENODEV;
1513335786aSStefan Roese 	}
1523335786aSStefan Roese 
1533335786aSStefan Roese 	lane = 0;
154df87e6b1SSimon Glass 	fdt_for_each_subnode(subnode, blob, node) {
1553335786aSStefan Roese 		/* Skip disabled ports */
1563335786aSStefan Roese 		if (!fdtdec_get_is_enabled(blob, subnode))
1573335786aSStefan Roese 			continue;
1583335786aSStefan Roese 
1593335786aSStefan Roese 		comphy_map_data[lane].speed = fdtdec_get_int(
1603335786aSStefan Roese 			blob, subnode, "phy-speed", PHY_TYPE_INVALID);
1613335786aSStefan Roese 		comphy_map_data[lane].type = fdtdec_get_int(
1623335786aSStefan Roese 			blob, subnode, "phy-type", PHY_SPEED_INVALID);
1633335786aSStefan Roese 		comphy_map_data[lane].invert = fdtdec_get_int(
1643335786aSStefan Roese 			blob, subnode, "phy-invert", PHY_POLARITY_NO_INVERT);
1653335786aSStefan Roese 		comphy_map_data[lane].clk_src = fdtdec_get_bool(blob, subnode,
1663335786aSStefan Roese 								"clk-src");
1677dda98e0SStefan Roese 		comphy_map_data[lane].end_point = fdtdec_get_bool(blob, subnode,
1687dda98e0SStefan Roese 								  "end_point");
1693335786aSStefan Roese 		if (comphy_map_data[lane].type == PHY_TYPE_INVALID) {
1703335786aSStefan Roese 			printf("no phy type for lane %d, setting lane as unconnected\n",
1713335786aSStefan Roese 			       lane + 1);
1723335786aSStefan Roese 		}
1733335786aSStefan Roese 
1743335786aSStefan Roese 		lane++;
1753335786aSStefan Roese 	}
1763335786aSStefan Roese 
177528213d3SIgal Liberman 	/* Save CP index for MultiCP devices (A8K) */
178528213d3SIgal Liberman 	chip_cfg->cp_index = current_idx++;
1793335786aSStefan Roese 	/* PHY power UP sequence */
1803335786aSStefan Roese 	chip_cfg->ptr_comphy_chip_init(chip_cfg, comphy_map_data);
1813335786aSStefan Roese 	/* PHY print SerDes status */
182e8c3156eSStefan Roese 	if (of_machine_is_compatible("marvell,armada8040"))
183528213d3SIgal Liberman 		printf("Comphy chip #%d:\n", chip_cfg->cp_index);
1843335786aSStefan Roese 	comphy_print(chip_cfg, comphy_map_data);
1853335786aSStefan Roese 
186e8c3156eSStefan Roese 	/*
187e8c3156eSStefan Roese 	 * Only run the dedicated PHY init code once, in the last PHY init call
188e8c3156eSStefan Roese 	 */
189e8c3156eSStefan Roese 	if (of_machine_is_compatible("marvell,armada8040"))
190e8c3156eSStefan Roese 		last_idx = 1;
191e8c3156eSStefan Roese 
192528213d3SIgal Liberman 	if (chip_cfg->cp_index == last_idx) {
1933335786aSStefan Roese 		/* Initialize dedicated PHYs (not muxed SerDes lanes) */
1943335786aSStefan Roese 		comphy_dedicated_phys_init();
195e8c3156eSStefan Roese 	}
1963335786aSStefan Roese 
1973335786aSStefan Roese 	return 0;
1983335786aSStefan Roese }
1993335786aSStefan Roese 
2003335786aSStefan Roese static const struct udevice_id comphy_ids[] = {
2013335786aSStefan Roese 	{ .compatible = "marvell,mvebu-comphy" },
2023335786aSStefan Roese 	{ }
2033335786aSStefan Roese };
2043335786aSStefan Roese 
2053335786aSStefan Roese U_BOOT_DRIVER(mvebu_comphy) = {
2063335786aSStefan Roese 	.name	= "mvebu_comphy",
2073335786aSStefan Roese 	.id	= UCLASS_MISC,
2083335786aSStefan Roese 	.of_match = comphy_ids,
2093335786aSStefan Roese 	.probe	= comphy_probe,
2103335786aSStefan Roese 	.priv_auto_alloc_size = sizeof(struct chip_serdes_phy_config),
2113335786aSStefan Roese };
212