1269369Sbr/*-
2269369Sbr * Copyright (c) 2014 Ruslan Bukin <br@bsdpad.com>
3269369Sbr * All rights reserved.
4269369Sbr *
5269369Sbr * Redistribution and use in source and binary forms, with or without
6269369Sbr * modification, are permitted provided that the following conditions
7269369Sbr * are met:
8269369Sbr * 1. Redistributions of source code must retain the above copyright
9269369Sbr *    notice, this list of conditions and the following disclaimer.
10269369Sbr * 2. Redistributions in binary form must reproduce the above copyright
11269369Sbr *    notice, this list of conditions and the following disclaimer in the
12269369Sbr *    documentation and/or other materials provided with the distribution.
13269369Sbr *
14269369Sbr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15269369Sbr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16269369Sbr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17269369Sbr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18269369Sbr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19269369Sbr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20269369Sbr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21269369Sbr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22269369Sbr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23269369Sbr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24269369Sbr * SUCH DAMAGE.
25269369Sbr */
26269369Sbr
27269369Sbr/*
28269369Sbr * DWC3 USB 3.0 DRD (dual role device) PHY
29269369Sbr */
30269369Sbr
31269369Sbr#include <sys/cdefs.h>
32269369Sbr__FBSDID("$FreeBSD: releng/11.0/sys/arm/samsung/exynos/exynos5_usb_phy.c 299069 2016-05-04 15:48:59Z pfg $");
33269369Sbr
34269369Sbr#include <sys/param.h>
35269369Sbr#include <sys/systm.h>
36269369Sbr#include <sys/bus.h>
37269369Sbr#include <sys/kernel.h>
38269369Sbr#include <sys/module.h>
39269369Sbr#include <sys/malloc.h>
40269369Sbr#include <sys/rman.h>
41269369Sbr#include <sys/timeet.h>
42269369Sbr#include <sys/timetc.h>
43269369Sbr#include <sys/watchdog.h>
44269369Sbr#include <sys/gpio.h>
45269369Sbr
46269369Sbr#include <dev/ofw/openfirm.h>
47269369Sbr#include <dev/ofw/ofw_bus.h>
48269369Sbr#include <dev/ofw/ofw_bus_subr.h>
49269369Sbr
50269369Sbr#include <machine/bus.h>
51269369Sbr#include <machine/cpu.h>
52269369Sbr#include <machine/intr.h>
53269369Sbr
54269369Sbr#include <arm/samsung/exynos/exynos5_common.h>
55269369Sbr#include <arm/samsung/exynos/exynos5_pmu.h>
56269369Sbr
57269369Sbr#include "gpio_if.h"
58269369Sbr
59269369Sbr#define	USB_DRD_LINKSYSTEM			0x04
60269369Sbr#define	 LINKSYSTEM_FLADJ_MASK			(0x3f << 1)
61269369Sbr#define	 LINKSYSTEM_FLADJ(x)			((x) << 1)
62269369Sbr#define	 LINKSYSTEM_XHCI_VERSION_CTRL		(1 << 27)
63269369Sbr#define	USB_DRD_PHYUTMI				0x08
64269369Sbr#define	 PHYUTMI_OTGDISABLE			(1 << 6)
65269369Sbr#define	 PHYUTMI_FORCESUSPEND			(1 << 1)
66269369Sbr#define	 PHYUTMI_FORCESLEEP			(1 << 0)
67269369Sbr#define	USB_DRD_PHYPIPE				0x0c
68269369Sbr#define	USB_DRD_PHYCLKRST			0x10
69269369Sbr#define	 PHYCLKRST_PORTRESET			(1 << 1)
70269369Sbr#define	 PHYCLKRST_COMMONONN			(1 << 0)
71269369Sbr#define	 PHYCLKRST_EN_UTMISUSPEND		(1 << 31)
72269369Sbr#define	 PHYCLKRST_SSC_REFCLKSEL_MASK		(0xff << 23)
73269369Sbr#define	 PHYCLKRST_SSC_REFCLKSEL(x)		((x) << 23)
74269369Sbr#define	 PHYCLKRST_SSC_RANGE_MASK		(0x03 << 21)
75269369Sbr#define	 PHYCLKRST_SSC_RANGE(x)			((x) << 21)
76269369Sbr#define	 PHYCLKRST_SSC_EN			(1 << 20)
77269369Sbr#define	 PHYCLKRST_REF_SSP_EN			(1 << 19)
78269369Sbr#define	 PHYCLKRST_REF_CLKDIV2			(1 << 18)
79269369Sbr#define	 PHYCLKRST_MPLL_MLTPR_MASK		(0x7f << 11)
80269369Sbr#define	 PHYCLKRST_MPLL_MLTPR_100MHZ		(0x19 << 11)
81269369Sbr#define	 PHYCLKRST_MPLL_MLTPR_50M		(0x32 << 11)
82269369Sbr#define	 PHYCLKRST_MPLL_MLTPR_24MHZ		(0x68 << 11)
83269369Sbr#define	 PHYCLKRST_MPLL_MLTPR_20MHZ		(0x7d << 11)
84269369Sbr#define	 PHYCLKRST_MPLL_MLTPR_19200KHZ		(0x02 << 11)
85269369Sbr#define	 PHYCLKRST_FSEL_UTMI_MASK		(0x7 << 5)
86269369Sbr#define	 PHYCLKRST_FSEL_PIPE_MASK		(0x7 << 8)
87269369Sbr#define	 PHYCLKRST_FSEL(x)			((x) << 5)
88269369Sbr#define	 PHYCLKRST_FSEL_9MHZ6			0x0
89269369Sbr#define	 PHYCLKRST_FSEL_10MHZ			0x1
90269369Sbr#define	 PHYCLKRST_FSEL_12MHZ			0x2
91269369Sbr#define	 PHYCLKRST_FSEL_19MHZ2			0x3
92269369Sbr#define	 PHYCLKRST_FSEL_20MHZ			0x4
93269369Sbr#define	 PHYCLKRST_FSEL_24MHZ			0x5
94269369Sbr#define	 PHYCLKRST_FSEL_50MHZ			0x7
95269369Sbr#define	 PHYCLKRST_RETENABLEN			(1 << 4)
96269369Sbr#define	 PHYCLKRST_REFCLKSEL_MASK		(0x03 << 2)
97269369Sbr#define	 PHYCLKRST_REFCLKSEL_PAD_REFCLK		(0x2 << 2)
98269369Sbr#define	 PHYCLKRST_REFCLKSEL_EXT_REFCLK		(0x3 << 2)
99269369Sbr#define	USB_DRD_PHYREG0				0x14
100269369Sbr#define	USB_DRD_PHYREG1				0x18
101269369Sbr#define	USB_DRD_PHYPARAM0			0x1c
102269369Sbr#define	 PHYPARAM0_REF_USE_PAD			(1 << 31)
103269369Sbr#define	 PHYPARAM0_REF_LOSLEVEL_MASK		(0x1f << 26)
104269369Sbr#define	 PHYPARAM0_REF_LOSLEVEL			(0x9 << 26)
105269369Sbr#define	USB_DRD_PHYPARAM1			0x20
106269369Sbr#define	 PHYPARAM1_PCS_TXDEEMPH_MASK		(0x1f << 0)
107269369Sbr#define	 PHYPARAM1_PCS_TXDEEMPH			(0x1c)
108269369Sbr#define	USB_DRD_PHYTERM				0x24
109269369Sbr#define	USB_DRD_PHYTEST				0x28
110269369Sbr#define	 PHYTEST_POWERDOWN_SSP			(1 << 3)
111269369Sbr#define	 PHYTEST_POWERDOWN_HSP			(1 << 2)
112269369Sbr#define	USB_DRD_PHYADP				0x2c
113269369Sbr#define	USB_DRD_PHYUTMICLKSEL			0x30
114269369Sbr#define	 PHYUTMICLKSEL_UTMI_CLKSEL		(1 << 2)
115269369Sbr#define	USB_DRD_PHYRESUME			0x34
116269369Sbr#define	USB_DRD_LINKPORT			0x44
117269369Sbr
118269369Sbrstruct usb_phy_softc {
119269369Sbr	struct resource		*res[1];
120269369Sbr	bus_space_tag_t		bst;
121269369Sbr	bus_space_handle_t	bsh;
122269369Sbr	device_t		dev;
123269369Sbr};
124269369Sbr
125269369Sbrstatic struct resource_spec usb_phy_spec[] = {
126269369Sbr	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
127269369Sbr	{ -1, 0 }
128269369Sbr};
129269369Sbr
130269369Sbrstatic int
131269369Sbrusb_phy_probe(device_t dev)
132269369Sbr{
133269369Sbr
134269369Sbr	if (!ofw_bus_status_okay(dev))
135269369Sbr		return (ENXIO);
136269369Sbr
137269369Sbr	if (!ofw_bus_is_compatible(dev, "samsung,exynos5420-usbdrd-phy"))
138269369Sbr		return (ENXIO);
139269369Sbr
140269369Sbr	device_set_desc(dev, "Samsung Exynos 5 USB PHY");
141269369Sbr	return (BUS_PROBE_DEFAULT);
142269369Sbr}
143269369Sbr
144269369Sbrstatic int
145269369Sbrvbus_on(struct usb_phy_softc *sc)
146269369Sbr{
147269369Sbr	pcell_t dts_value[3];
148269369Sbr	device_t gpio_dev;
149269369Sbr	phandle_t node;
150269369Sbr	pcell_t pin;
151269369Sbr	int len;
152269369Sbr
153269369Sbr	if ((node = ofw_bus_get_node(sc->dev)) == -1)
154269369Sbr		return (-1);
155269369Sbr
156269369Sbr	/* Power pin */
157269369Sbr	if ((len = OF_getproplen(node, "vbus-supply")) <= 0)
158269369Sbr		return (-1);
159269702Snwhitehorn	OF_getencprop(node, "vbus-supply", dts_value, len);
160269702Snwhitehorn	pin = dts_value[0];
161269369Sbr
162269369Sbr	gpio_dev = devclass_get_device(devclass_find("gpio"), 0);
163269369Sbr	if (gpio_dev == NULL) {
164299069Spfg		device_printf(sc->dev, "can't find gpio_dev\n");
165269369Sbr		return (1);
166269369Sbr	}
167269369Sbr
168269369Sbr	GPIO_PIN_SETFLAGS(gpio_dev, pin, GPIO_PIN_OUTPUT);
169269369Sbr	GPIO_PIN_SET(gpio_dev, pin, GPIO_PIN_HIGH);
170269369Sbr
171269369Sbr	return (0);
172269369Sbr}
173269369Sbr
174269369Sbrstatic int
175269369Sbrusb3_phy_init(struct usb_phy_softc *sc)
176269369Sbr{
177269369Sbr	int reg;
178269369Sbr
179269369Sbr	/* Reset USB 3.0 PHY */
180269369Sbr	WRITE4(sc, USB_DRD_PHYREG0, 0);
181269369Sbr
182269369Sbr	reg = READ4(sc, USB_DRD_PHYPARAM0);
183269369Sbr	/* PHY CLK src */
184269369Sbr	reg &= ~(PHYPARAM0_REF_USE_PAD);
185269369Sbr	reg &= ~(PHYPARAM0_REF_LOSLEVEL_MASK);
186269369Sbr	reg |= (PHYPARAM0_REF_LOSLEVEL);
187269369Sbr	WRITE4(sc, USB_DRD_PHYPARAM0, reg);
188269369Sbr	WRITE4(sc, USB_DRD_PHYRESUME, 0);
189269369Sbr
190269369Sbr	reg = (LINKSYSTEM_XHCI_VERSION_CTRL |
191269369Sbr	    LINKSYSTEM_FLADJ(0x20));
192269369Sbr	WRITE4(sc, USB_DRD_LINKSYSTEM, reg);
193269369Sbr
194269369Sbr	reg = READ4(sc, USB_DRD_PHYPARAM1);
195269369Sbr	reg &= ~(PHYPARAM1_PCS_TXDEEMPH_MASK);
196269369Sbr	reg |= (PHYPARAM1_PCS_TXDEEMPH);
197269369Sbr	WRITE4(sc, USB_DRD_PHYPARAM1, reg);
198269369Sbr
199269369Sbr	reg = READ4(sc, USB_DRD_PHYUTMICLKSEL);
200269369Sbr	reg |= (PHYUTMICLKSEL_UTMI_CLKSEL);
201269369Sbr	WRITE4(sc, USB_DRD_PHYUTMICLKSEL, reg);
202269369Sbr
203269369Sbr	reg = READ4(sc, USB_DRD_PHYTEST);
204269369Sbr	reg &= ~(PHYTEST_POWERDOWN_HSP);
205269369Sbr	reg &= ~(PHYTEST_POWERDOWN_SSP);
206269369Sbr	WRITE4(sc, USB_DRD_PHYTEST, reg);
207269369Sbr
208269369Sbr	WRITE4(sc, USB_DRD_PHYUTMI, PHYUTMI_OTGDISABLE);
209269369Sbr
210269369Sbr	/* Clock */
211269369Sbr	reg = (PHYCLKRST_REFCLKSEL_EXT_REFCLK);
212269369Sbr	reg |= (PHYCLKRST_FSEL(PHYCLKRST_FSEL_24MHZ));
213269369Sbr	reg |= (PHYCLKRST_MPLL_MLTPR_24MHZ);
214269369Sbr	reg |= (PHYCLKRST_SSC_REFCLKSEL(0x88));
215269369Sbr	reg |= (PHYCLKRST_RETENABLEN |
216269369Sbr	    PHYCLKRST_REF_SSP_EN | /* Super speed */
217269369Sbr	    PHYCLKRST_SSC_EN | /* Spread spectrum */
218269369Sbr	    PHYCLKRST_COMMONONN |
219269369Sbr	    PHYCLKRST_PORTRESET);
220269369Sbr
221269369Sbr	WRITE4(sc, USB_DRD_PHYCLKRST, reg);
222269369Sbr	DELAY(50000);
223269369Sbr	reg &= ~PHYCLKRST_PORTRESET;
224269369Sbr	WRITE4(sc, USB_DRD_PHYCLKRST, reg);
225269369Sbr
226269369Sbr	return (0);
227269369Sbr}
228269369Sbr
229269369Sbrstatic int
230269369Sbrusb_phy_attach(device_t dev)
231269369Sbr{
232269369Sbr	struct usb_phy_softc *sc;
233269369Sbr
234269369Sbr	sc = device_get_softc(dev);
235269369Sbr	sc->dev = dev;
236269369Sbr
237269369Sbr	if (bus_alloc_resources(dev, usb_phy_spec, sc->res)) {
238269369Sbr		device_printf(dev, "could not allocate resources\n");
239269369Sbr		return (ENXIO);
240269369Sbr	}
241269369Sbr
242269369Sbr	/* Memory interface */
243269369Sbr	sc->bst = rman_get_bustag(sc->res[0]);
244269369Sbr	sc->bsh = rman_get_bushandle(sc->res[0]);
245269369Sbr
246269369Sbr	vbus_on(sc);
247269369Sbr
248269369Sbr	usbdrd_phy_power_on();
249269369Sbr
250269369Sbr	DELAY(100);
251269369Sbr
252269369Sbr	usb3_phy_init(sc);
253269369Sbr
254269369Sbr	return (0);
255269369Sbr}
256269369Sbr
257269369Sbrstatic device_method_t usb_phy_methods[] = {
258269369Sbr	DEVMETHOD(device_probe,		usb_phy_probe),
259269369Sbr	DEVMETHOD(device_attach,	usb_phy_attach),
260269369Sbr	{ 0, 0 }
261269369Sbr};
262269369Sbr
263269369Sbrstatic driver_t usb_phy_driver = {
264269369Sbr	"usb_phy",
265269369Sbr	usb_phy_methods,
266269369Sbr	sizeof(struct usb_phy_softc),
267269369Sbr};
268269369Sbr
269269369Sbrstatic devclass_t usb_phy_devclass;
270269369Sbr
271269369SbrDRIVER_MODULE(usb_phy, simplebus, usb_phy_driver, usb_phy_devclass, 0, 0);
272