1/*
2 * Setup platform devices needed by the Freescale multi-port host
3 * and/or dual-role USB controller modules based on the description
4 * in flat device tree.
5 *
6 * This program is free software; you can redistribute  it and/or modify it
7 * under  the terms of  the GNU General  Public License as published by the
8 * Free Software Foundation;  either version 2 of the  License, or (at your
9 * option) any later version.
10 */
11
12#include <linux/kernel.h>
13#include <linux/platform_device.h>
14#include <linux/fsl_devices.h>
15#include <linux/err.h>
16#include <linux/io.h>
17#include <linux/of_platform.h>
18#include <linux/clk.h>
19#include <linux/module.h>
20
21struct fsl_usb2_dev_data {
22	char *dr_mode;		/* controller mode */
23	char *drivers[3];	/* drivers to instantiate for this mode */
24	enum fsl_usb2_operating_modes op_mode;	/* operating mode */
25};
26
27struct fsl_usb2_dev_data dr_mode_data[] __devinitdata = {
28	{
29		.dr_mode = "host",
30		.drivers = { "fsl-ehci", NULL, NULL, },
31		.op_mode = FSL_USB2_DR_HOST,
32	},
33	{
34		.dr_mode = "otg",
35		.drivers = { "fsl-usb2-otg", "fsl-ehci", "fsl-usb2-udc", },
36		.op_mode = FSL_USB2_DR_OTG,
37	},
38	{
39		.dr_mode = "peripheral",
40		.drivers = { "fsl-usb2-udc", NULL, NULL, },
41		.op_mode = FSL_USB2_DR_DEVICE,
42	},
43};
44
45struct fsl_usb2_dev_data * __devinit get_dr_mode_data(struct device_node *np)
46{
47	const unsigned char *prop;
48	int i;
49
50	prop = of_get_property(np, "dr_mode", NULL);
51	if (prop) {
52		for (i = 0; i < ARRAY_SIZE(dr_mode_data); i++) {
53			if (!strcmp(prop, dr_mode_data[i].dr_mode))
54				return &dr_mode_data[i];
55		}
56	}
57	pr_warn("%s: Invalid 'dr_mode' property, fallback to host mode\n",
58		np->full_name);
59	return &dr_mode_data[0]; /* mode not specified, use host */
60}
61
62static enum fsl_usb2_phy_modes __devinit determine_usb_phy(const char *phy_type)
63{
64	if (!phy_type)
65		return FSL_USB2_PHY_NONE;
66	if (!strcasecmp(phy_type, "ulpi"))
67		return FSL_USB2_PHY_ULPI;
68	if (!strcasecmp(phy_type, "utmi"))
69		return FSL_USB2_PHY_UTMI;
70	if (!strcasecmp(phy_type, "utmi_wide"))
71		return FSL_USB2_PHY_UTMI_WIDE;
72	if (!strcasecmp(phy_type, "serial"))
73		return FSL_USB2_PHY_SERIAL;
74
75	return FSL_USB2_PHY_NONE;
76}
77
78struct platform_device * __devinit fsl_usb2_device_register(
79					struct platform_device *ofdev,
80					struct fsl_usb2_platform_data *pdata,
81					const char *name, int id)
82{
83	struct platform_device *pdev;
84	const struct resource *res = ofdev->resource;
85	unsigned int num = ofdev->num_resources;
86	int retval;
87
88	pdev = platform_device_alloc(name, id);
89	if (!pdev) {
90		retval = -ENOMEM;
91		goto error;
92	}
93
94	pdev->dev.parent = &ofdev->dev;
95
96	pdev->dev.coherent_dma_mask = ofdev->dev.coherent_dma_mask;
97	*pdev->dev.dma_mask = *ofdev->dev.dma_mask;
98
99	retval = platform_device_add_data(pdev, pdata, sizeof(*pdata));
100	if (retval)
101		goto error;
102
103	if (num) {
104		retval = platform_device_add_resources(pdev, res, num);
105		if (retval)
106			goto error;
107	}
108
109	retval = platform_device_add(pdev);
110	if (retval)
111		goto error;
112
113	return pdev;
114
115error:
116	platform_device_put(pdev);
117	return ERR_PTR(retval);
118}
119
120static const struct of_device_id fsl_usb2_mph_dr_of_match[];
121
122static int __devinit fsl_usb2_mph_dr_of_probe(struct platform_device *ofdev)
123{
124	struct device_node *np = ofdev->dev.of_node;
125	struct platform_device *usb_dev;
126	struct fsl_usb2_platform_data data, *pdata;
127	struct fsl_usb2_dev_data *dev_data;
128	const struct of_device_id *match;
129	const unsigned char *prop;
130	static unsigned int idx;
131	int i;
132
133	if (!of_device_is_available(np))
134		return -ENODEV;
135
136	match = of_match_device(fsl_usb2_mph_dr_of_match, &ofdev->dev);
137	if (!match)
138		return -ENODEV;
139
140	pdata = &data;
141	if (match->data)
142		memcpy(pdata, match->data, sizeof(data));
143	else
144		memset(pdata, 0, sizeof(data));
145
146	dev_data = get_dr_mode_data(np);
147
148	if (of_device_is_compatible(np, "fsl-usb2-mph")) {
149		if (of_get_property(np, "port0", NULL))
150			pdata->port_enables |= FSL_USB2_PORT0_ENABLED;
151
152		if (of_get_property(np, "port1", NULL))
153			pdata->port_enables |= FSL_USB2_PORT1_ENABLED;
154
155		pdata->operating_mode = FSL_USB2_MPH_HOST;
156	} else {
157		if (of_get_property(np, "fsl,invert-drvvbus", NULL))
158			pdata->invert_drvvbus = 1;
159
160		if (of_get_property(np, "fsl,invert-pwr-fault", NULL))
161			pdata->invert_pwr_fault = 1;
162
163		/* setup mode selected in the device tree */
164		pdata->operating_mode = dev_data->op_mode;
165	}
166
167	prop = of_get_property(np, "phy_type", NULL);
168	pdata->phy_mode = determine_usb_phy(prop);
169
170	for (i = 0; i < ARRAY_SIZE(dev_data->drivers); i++) {
171		if (!dev_data->drivers[i])
172			continue;
173		usb_dev = fsl_usb2_device_register(ofdev, pdata,
174					dev_data->drivers[i], idx);
175		if (IS_ERR(usb_dev)) {
176			dev_err(&ofdev->dev, "Can't register usb device\n");
177			return PTR_ERR(usb_dev);
178		}
179	}
180	idx++;
181	return 0;
182}
183
184static int __devexit __unregister_subdev(struct device *dev, void *d)
185{
186	platform_device_unregister(to_platform_device(dev));
187	return 0;
188}
189
190static int __devexit fsl_usb2_mph_dr_of_remove(struct platform_device *ofdev)
191{
192	device_for_each_child(&ofdev->dev, NULL, __unregister_subdev);
193	return 0;
194}
195
196#ifdef CONFIG_PPC_MPC512x
197
198#define USBGENCTRL		0x200		/* NOTE: big endian */
199#define GC_WU_INT_CLR		(1 << 5)	/* Wakeup int clear */
200#define GC_ULPI_SEL		(1 << 4)	/* ULPI i/f select (usb0 only)*/
201#define GC_PPP			(1 << 3)	/* Inv. Port Power Polarity */
202#define GC_PFP			(1 << 2)	/* Inv. Power Fault Polarity */
203#define GC_WU_ULPI_EN		(1 << 1)	/* Wakeup on ULPI event */
204#define GC_WU_IE		(1 << 1)	/* Wakeup interrupt enable */
205
206#define ISIPHYCTRL		0x204		/* NOTE: big endian */
207#define PHYCTRL_PHYE		(1 << 4)	/* On-chip UTMI PHY enable */
208#define PHYCTRL_BSENH		(1 << 3)	/* Bit Stuff Enable High */
209#define PHYCTRL_BSEN		(1 << 2)	/* Bit Stuff Enable */
210#define PHYCTRL_LSFE		(1 << 1)	/* Line State Filter Enable */
211#define PHYCTRL_PXE		(1 << 0)	/* PHY oscillator enable */
212
213int fsl_usb2_mpc5121_init(struct platform_device *pdev)
214{
215	struct fsl_usb2_platform_data *pdata = pdev->dev.platform_data;
216	struct clk *clk;
217	char clk_name[10];
218	int base, clk_num;
219
220	base = pdev->resource->start & 0xf000;
221	if (base == 0x3000)
222		clk_num = 1;
223	else if (base == 0x4000)
224		clk_num = 2;
225	else
226		return -ENODEV;
227
228	snprintf(clk_name, sizeof(clk_name), "usb%d_clk", clk_num);
229	clk = clk_get(&pdev->dev, clk_name);
230	if (IS_ERR(clk)) {
231		dev_err(&pdev->dev, "failed to get clk\n");
232		return PTR_ERR(clk);
233	}
234
235	clk_enable(clk);
236	pdata->clk = clk;
237
238	if (pdata->phy_mode == FSL_USB2_PHY_UTMI_WIDE) {
239		u32 reg = 0;
240
241		if (pdata->invert_drvvbus)
242			reg |= GC_PPP;
243
244		if (pdata->invert_pwr_fault)
245			reg |= GC_PFP;
246
247		out_be32(pdata->regs + ISIPHYCTRL, PHYCTRL_PHYE | PHYCTRL_PXE);
248		out_be32(pdata->regs + USBGENCTRL, reg);
249	}
250	return 0;
251}
252
253static void fsl_usb2_mpc5121_exit(struct platform_device *pdev)
254{
255	struct fsl_usb2_platform_data *pdata = pdev->dev.platform_data;
256
257	pdata->regs = NULL;
258
259	if (pdata->clk) {
260		clk_disable(pdata->clk);
261		clk_put(pdata->clk);
262	}
263}
264
265static struct fsl_usb2_platform_data fsl_usb2_mpc5121_pd = {
266	.big_endian_desc = 1,
267	.big_endian_mmio = 1,
268	.es = 1,
269	.have_sysif_regs = 0,
270	.le_setup_buf = 1,
271	.init = fsl_usb2_mpc5121_init,
272	.exit = fsl_usb2_mpc5121_exit,
273};
274#endif /* CONFIG_PPC_MPC512x */
275
276static struct fsl_usb2_platform_data fsl_usb2_mpc8xxx_pd = {
277	.have_sysif_regs = 1,
278};
279
280static const struct of_device_id fsl_usb2_mph_dr_of_match[] = {
281	{ .compatible = "fsl-usb2-mph", .data = &fsl_usb2_mpc8xxx_pd, },
282	{ .compatible = "fsl-usb2-dr", .data = &fsl_usb2_mpc8xxx_pd, },
283#ifdef CONFIG_PPC_MPC512x
284	{ .compatible = "fsl,mpc5121-usb2-dr", .data = &fsl_usb2_mpc5121_pd, },
285#endif
286	{},
287};
288
289static struct platform_driver fsl_usb2_mph_dr_driver = {
290	.driver = {
291		.name = "fsl-usb2-mph-dr",
292		.owner = THIS_MODULE,
293		.of_match_table = fsl_usb2_mph_dr_of_match,
294	},
295	.probe	= fsl_usb2_mph_dr_of_probe,
296	.remove	= __devexit_p(fsl_usb2_mph_dr_of_remove),
297};
298
299module_platform_driver(fsl_usb2_mph_dr_driver);
300
301MODULE_DESCRIPTION("FSL MPH DR OF devices driver");
302MODULE_AUTHOR("Anatolij Gustschin <agust@denx.de>");
303MODULE_LICENSE("GPL");
304