xref: /openbmc/u-boot/drivers/misc/mpc83xx_serdes.c (revision 4e710ebb4463c8e031eb269c012fbadb2479608b)
1*d2166319SMario Six // SPDX-License-Identifier: GPL-2.0+
2*d2166319SMario Six /*
3*d2166319SMario Six  * (C) Copyright 2018
4*d2166319SMario Six  * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
5*d2166319SMario Six  *
6*d2166319SMario Six  * base on the MPC83xx serdes initialization, which is
7*d2166319SMario Six  *
8*d2166319SMario Six  * Copyright 2007,2011 Freescale Semiconductor, Inc.
9*d2166319SMario Six  * Copyright (C) 2008 MontaVista Software, Inc.
10*d2166319SMario Six  */
11*d2166319SMario Six 
12*d2166319SMario Six #include <common.h>
13*d2166319SMario Six #include <dm.h>
14*d2166319SMario Six #include <mapmem.h>
15*d2166319SMario Six #include <misc.h>
16*d2166319SMario Six 
17*d2166319SMario Six #include "mpc83xx_serdes.h"
18*d2166319SMario Six 
19*d2166319SMario Six /**
20*d2166319SMario Six  * struct mpc83xx_serdes_priv - Private structure for MPC83xx serdes
21*d2166319SMario Six  * @regs:  The device's register map
22*d2166319SMario Six  * @rfcks: Variable to keep the serdes reference clock selection set during
23*d2166319SMario Six  *	   initialization in (is or'd to every value written to SRDSCR4)
24*d2166319SMario Six  */
25*d2166319SMario Six struct mpc83xx_serdes_priv {
26*d2166319SMario Six 	struct mpc83xx_serdes_regs *regs;
27*d2166319SMario Six 	u32 rfcks;
28*d2166319SMario Six };
29*d2166319SMario Six 
30*d2166319SMario Six /**
31*d2166319SMario Six  * setup_sata() - Configure the SerDes device to SATA mode
32*d2166319SMario Six  * @dev: The device to configure
33*d2166319SMario Six  */
setup_sata(struct udevice * dev)34*d2166319SMario Six static void setup_sata(struct udevice *dev)
35*d2166319SMario Six {
36*d2166319SMario Six 	struct mpc83xx_serdes_priv *priv = dev_get_priv(dev);
37*d2166319SMario Six 
38*d2166319SMario Six 	/* Set and clear reset bits */
39*d2166319SMario Six 	setbits_be32(&priv->regs->srdsrstctl, SRDSRSTCTL_SATA_RESET);
40*d2166319SMario Six 	udelay(1000);
41*d2166319SMario Six 	clrbits_be32(&priv->regs->srdsrstctl, SRDSRSTCTL_SATA_RESET);
42*d2166319SMario Six 
43*d2166319SMario Six 	/* Configure SRDSCR0 */
44*d2166319SMario Six 	clrsetbits_be32(&priv->regs->srdscr0,
45*d2166319SMario Six 			SRDSCR0_TXEQA_MASK | SRDSCR0_TXEQE_MASK,
46*d2166319SMario Six 			SRDSCR0_TXEQA_SATA | SRDSCR0_TXEQE_SATA);
47*d2166319SMario Six 
48*d2166319SMario Six 	/* Configure SRDSCR1 */
49*d2166319SMario Six 	clrbits_be32(&priv->regs->srdscr1, SRDSCR1_PLLBW);
50*d2166319SMario Six 
51*d2166319SMario Six 	/* Configure SRDSCR2 */
52*d2166319SMario Six 	clrsetbits_be32(&priv->regs->srdscr2,
53*d2166319SMario Six 			SRDSCR2_SEIC_MASK,
54*d2166319SMario Six 			SRDSCR2_SEIC_SATA);
55*d2166319SMario Six 
56*d2166319SMario Six 	/* Configure SRDSCR3 */
57*d2166319SMario Six 	out_be32(&priv->regs->srdscr3,
58*d2166319SMario Six 		 SRDSCR3_KFR_SATA | SRDSCR3_KPH_SATA |
59*d2166319SMario Six 		 SRDSCR3_SDFM_SATA_PEX | SRDSCR3_SDTXL_SATA);
60*d2166319SMario Six 
61*d2166319SMario Six 	/* Configure SRDSCR4 */
62*d2166319SMario Six 	out_be32(&priv->regs->srdscr4, priv->rfcks | SRDSCR4_PROT_SATA);
63*d2166319SMario Six }
64*d2166319SMario Six 
65*d2166319SMario Six /**
66*d2166319SMario Six  * setup_pex() - Configure the SerDes device to PCI Express mode
67*d2166319SMario Six  * @dev:  The device to configure
68*d2166319SMario Six  * @type: The PCI Express type to configure for (x1 or x2)
69*d2166319SMario Six  */
setup_pex(struct udevice * dev,enum pex_type type)70*d2166319SMario Six static void setup_pex(struct udevice *dev, enum pex_type type)
71*d2166319SMario Six {
72*d2166319SMario Six 	struct mpc83xx_serdes_priv *priv = dev_get_priv(dev);
73*d2166319SMario Six 
74*d2166319SMario Six 	/* Configure SRDSCR1 */
75*d2166319SMario Six 	setbits_be32(&priv->regs->srdscr1, SRDSCR1_PLLBW);
76*d2166319SMario Six 
77*d2166319SMario Six 	/* Configure SRDSCR2 */
78*d2166319SMario Six 	clrsetbits_be32(&priv->regs->srdscr2,
79*d2166319SMario Six 			SRDSCR2_SEIC_MASK,
80*d2166319SMario Six 			SRDSCR2_SEIC_PEX);
81*d2166319SMario Six 
82*d2166319SMario Six 	/* Configure SRDSCR3 */
83*d2166319SMario Six 	out_be32(&priv->regs->srdscr3, SRDSCR3_SDFM_SATA_PEX);
84*d2166319SMario Six 
85*d2166319SMario Six 	/* Configure SRDSCR4 */
86*d2166319SMario Six 	if (type == PEX_X2)
87*d2166319SMario Six 		out_be32(&priv->regs->srdscr4,
88*d2166319SMario Six 			 priv->rfcks | SRDSCR4_PROT_PEX | SRDSCR4_PLANE_X2);
89*d2166319SMario Six 	else
90*d2166319SMario Six 		out_be32(&priv->regs->srdscr4,
91*d2166319SMario Six 			 priv->rfcks | SRDSCR4_PROT_PEX);
92*d2166319SMario Six }
93*d2166319SMario Six 
94*d2166319SMario Six /**
95*d2166319SMario Six  * setup_sgmii() - Configure the SerDes device to SGMII mode
96*d2166319SMario Six  * @dev: The device to configure
97*d2166319SMario Six  */
setup_sgmii(struct udevice * dev)98*d2166319SMario Six static void setup_sgmii(struct udevice *dev)
99*d2166319SMario Six {
100*d2166319SMario Six 	struct mpc83xx_serdes_priv *priv = dev_get_priv(dev);
101*d2166319SMario Six 
102*d2166319SMario Six 	/* Configure SRDSCR1 */
103*d2166319SMario Six 	clrbits_be32(&priv->regs->srdscr1, SRDSCR1_PLLBW);
104*d2166319SMario Six 
105*d2166319SMario Six 	/* Configure SRDSCR2 */
106*d2166319SMario Six 	clrsetbits_be32(&priv->regs->srdscr2,
107*d2166319SMario Six 			SRDSCR2_SEIC_MASK,
108*d2166319SMario Six 			SRDSCR2_SEIC_SGMII);
109*d2166319SMario Six 
110*d2166319SMario Six 	/* Configure SRDSCR3 */
111*d2166319SMario Six 	out_be32(&priv->regs->srdscr3, 0);
112*d2166319SMario Six 
113*d2166319SMario Six 	/* Configure SRDSCR4 */
114*d2166319SMario Six 	out_be32(&priv->regs->srdscr4, priv->rfcks | SRDSCR4_PROT_SGMII);
115*d2166319SMario Six }
116*d2166319SMario Six 
mpc83xx_serdes_probe(struct udevice * dev)117*d2166319SMario Six static int mpc83xx_serdes_probe(struct udevice *dev)
118*d2166319SMario Six {
119*d2166319SMario Six 	struct mpc83xx_serdes_priv *priv = dev_get_priv(dev);
120*d2166319SMario Six 	bool vdd;
121*d2166319SMario Six 	const char *proto;
122*d2166319SMario Six 
123*d2166319SMario Six 	priv->regs = map_sysmem(dev_read_addr(dev),
124*d2166319SMario Six 				sizeof(struct mpc83xx_serdes_regs));
125*d2166319SMario Six 
126*d2166319SMario Six 	switch (dev_read_u32_default(dev, "serdes-clk", -1)) {
127*d2166319SMario Six 	case 100:
128*d2166319SMario Six 		priv->rfcks = SRDSCR4_RFCKS_100;
129*d2166319SMario Six 		break;
130*d2166319SMario Six 	case 125:
131*d2166319SMario Six 		priv->rfcks = SRDSCR4_RFCKS_125;
132*d2166319SMario Six 		break;
133*d2166319SMario Six 	case 150:
134*d2166319SMario Six 		priv->rfcks = SRDSCR4_RFCKS_150;
135*d2166319SMario Six 		break;
136*d2166319SMario Six 	default:
137*d2166319SMario Six 		debug("%s: Could not read serdes clock value\n", dev->name);
138*d2166319SMario Six 		return -EINVAL;
139*d2166319SMario Six 	}
140*d2166319SMario Six 
141*d2166319SMario Six 	vdd = dev_read_bool(dev, "vdd");
142*d2166319SMario Six 
143*d2166319SMario Six 	/* 1.0V corevdd */
144*d2166319SMario Six 	if (vdd) {
145*d2166319SMario Six 		/* DPPE/DPPA = 0 */
146*d2166319SMario Six 		clrbits_be32(&priv->regs->srdscr0, SRDSCR0_DPP_1V2);
147*d2166319SMario Six 
148*d2166319SMario Six 		/* VDD = 0 */
149*d2166319SMario Six 		clrbits_be32(&priv->regs->srdscr0, SRDSCR2_VDD_1V2);
150*d2166319SMario Six 	}
151*d2166319SMario Six 
152*d2166319SMario Six 	proto = dev_read_string(dev, "proto");
153*d2166319SMario Six 
154*d2166319SMario Six 	/* protocol specific configuration */
155*d2166319SMario Six 	if (!strcmp(proto, "sata")) {
156*d2166319SMario Six 		setup_sata(dev);
157*d2166319SMario Six 	} else if (!strcmp(proto, "pex")) {
158*d2166319SMario Six 		setup_pex(dev, PEX_X1);
159*d2166319SMario Six 	} else if (!strcmp(proto, "pex-x2")) {
160*d2166319SMario Six 		setup_pex(dev, PEX_X2);
161*d2166319SMario Six 	} else if (!strcmp(proto, "sgmii")) {
162*d2166319SMario Six 		setup_sgmii(dev);
163*d2166319SMario Six 	} else {
164*d2166319SMario Six 		debug("%s: Invalid protocol value %s\n", dev->name, proto);
165*d2166319SMario Six 		return -EINVAL;
166*d2166319SMario Six 	}
167*d2166319SMario Six 
168*d2166319SMario Six 	/* Do a software reset */
169*d2166319SMario Six 	setbits_be32(&priv->regs->srdsrstctl, SRDSRSTCTL_RST);
170*d2166319SMario Six 
171*d2166319SMario Six 	return 0;
172*d2166319SMario Six }
173*d2166319SMario Six 
174*d2166319SMario Six static const struct udevice_id mpc83xx_serdes_ids[] = {
175*d2166319SMario Six 	{ .compatible = "fsl,mpc83xx-serdes" },
176*d2166319SMario Six 	{ }
177*d2166319SMario Six };
178*d2166319SMario Six 
179*d2166319SMario Six U_BOOT_DRIVER(mpc83xx_serdes) = {
180*d2166319SMario Six 	.name           = "mpc83xx_serdes",
181*d2166319SMario Six 	.id             = UCLASS_MISC,
182*d2166319SMario Six 	.of_match       = mpc83xx_serdes_ids,
183*d2166319SMario Six 	.probe          = mpc83xx_serdes_probe,
184*d2166319SMario Six 	.priv_auto_alloc_size = sizeof(struct mpc83xx_serdes_priv),
185*d2166319SMario Six };
186