1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2010 ST-Ericsson AB
4 * Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/clk.h>
12 #include <linux/err.h>
15 #include <linux/platform_device.h>
16 #include <linux/usb/musb-ux500.h>
18 #include "musb_core.h"
20 static const struct musb_hdrc_config ux500_musb_hdrc_config
= {
29 struct platform_device
*musb
;
32 #define glue_to_musb(g) platform_get_drvdata(g->musb)
34 static void ux500_musb_set_vbus(struct musb
*musb
, int is_on
)
37 unsigned long timeout
= jiffies
+ msecs_to_jiffies(1000);
38 /* HDRC controls CPEN, but beware current surges during device
39 * connect. They can trigger transient overcurrent conditions
40 * that must be ignored.
43 devctl
= musb_readb(musb
->mregs
, MUSB_DEVCTL
);
46 if (musb
->xceiv
->otg
->state
== OTG_STATE_A_IDLE
) {
47 /* start the session */
48 devctl
|= MUSB_DEVCTL_SESSION
;
49 musb_writeb(musb
->mregs
, MUSB_DEVCTL
, devctl
);
51 * Wait for the musb to set as A device to enable the
54 while (musb_readb(musb
->mregs
, MUSB_DEVCTL
) & 0x80) {
56 if (time_after(jiffies
, timeout
)) {
57 dev_err(musb
->controller
,
58 "configured as A device timeout");
65 musb
->xceiv
->otg
->default_a
= 1;
66 musb
->xceiv
->otg
->state
= OTG_STATE_A_WAIT_VRISE
;
67 devctl
|= MUSB_DEVCTL_SESSION
;
73 /* NOTE: we're skipping A_WAIT_VFALL -> A_IDLE and jumping
76 musb
->xceiv
->otg
->default_a
= 0;
77 devctl
&= ~MUSB_DEVCTL_SESSION
;
80 musb_writeb(musb
->mregs
, MUSB_DEVCTL
, devctl
);
83 * Devctl values will be updated after vbus goes below
84 * session_valid. The time taken depends on the capacitance
85 * on VBUS line. The max discharge time can be upto 1 sec
86 * as per the spec. Typically on our platform, it is 200ms
91 dev_dbg(musb
->controller
, "VBUS %s, devctl %02x\n",
92 usb_otg_state_string(musb
->xceiv
->otg
->state
),
93 musb_readb(musb
->mregs
, MUSB_DEVCTL
));
96 static int musb_otg_notifications(struct notifier_block
*nb
,
97 unsigned long event
, void *unused
)
99 struct musb
*musb
= container_of(nb
, struct musb
, nb
);
101 dev_dbg(musb
->controller
, "musb_otg_notifications %ld %s\n",
102 event
, usb_otg_state_string(musb
->xceiv
->otg
->state
));
106 dev_dbg(musb
->controller
, "ID GND\n");
107 ux500_musb_set_vbus(musb
, 1);
109 case UX500_MUSB_VBUS
:
110 dev_dbg(musb
->controller
, "VBUS Connect\n");
112 case UX500_MUSB_NONE
:
113 dev_dbg(musb
->controller
, "VBUS Disconnect\n");
114 if (is_host_active(musb
))
115 ux500_musb_set_vbus(musb
, 0);
117 musb
->xceiv
->otg
->state
= OTG_STATE_B_IDLE
;
120 dev_dbg(musb
->controller
, "ID float\n");
126 static irqreturn_t
ux500_musb_interrupt(int irq
, void *__hci
)
129 irqreturn_t retval
= IRQ_NONE
;
130 struct musb
*musb
= __hci
;
132 spin_lock_irqsave(&musb
->lock
, flags
);
134 musb
->int_usb
= musb_readb(musb
->mregs
, MUSB_INTRUSB
);
135 musb
->int_tx
= musb_readw(musb
->mregs
, MUSB_INTRTX
);
136 musb
->int_rx
= musb_readw(musb
->mregs
, MUSB_INTRRX
);
138 if (musb
->int_usb
|| musb
->int_tx
|| musb
->int_rx
)
139 retval
= musb_interrupt(musb
);
141 spin_unlock_irqrestore(&musb
->lock
, flags
);
146 static int ux500_musb_init(struct musb
*musb
)
150 musb
->xceiv
= usb_get_phy(USB_PHY_TYPE_USB2
);
151 if (IS_ERR_OR_NULL(musb
->xceiv
)) {
152 pr_err("HS USB OTG: no transceiver configured\n");
153 return -EPROBE_DEFER
;
156 musb
->nb
.notifier_call
= musb_otg_notifications
;
157 status
= usb_register_notifier(musb
->xceiv
, &musb
->nb
);
159 dev_dbg(musb
->controller
, "notification register failed\n");
163 musb
->isr
= ux500_musb_interrupt
;
168 static int ux500_musb_exit(struct musb
*musb
)
170 usb_unregister_notifier(musb
->xceiv
, &musb
->nb
);
172 usb_put_phy(musb
->xceiv
);
177 static const struct musb_platform_ops ux500_ops
= {
178 .quirks
= MUSB_DMA_UX500
| MUSB_INDEXED_EP
,
179 #ifdef CONFIG_USB_UX500_DMA
180 .dma_init
= ux500_dma_controller_create
,
181 .dma_exit
= ux500_dma_controller_destroy
,
183 .init
= ux500_musb_init
,
184 .exit
= ux500_musb_exit
,
187 .set_vbus
= ux500_musb_set_vbus
,
190 static struct musb_hdrc_platform_data
*
191 ux500_of_probe(struct platform_device
*pdev
, struct device_node
*np
)
193 struct musb_hdrc_platform_data
*pdata
;
197 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
201 mode
= of_get_property(np
, "dr_mode", &strlen
);
203 dev_err(&pdev
->dev
, "No 'dr_mode' property found\n");
208 if (!strcmp(mode
, "host"))
209 pdata
->mode
= MUSB_HOST
;
210 if (!strcmp(mode
, "otg"))
211 pdata
->mode
= MUSB_OTG
;
212 if (!strcmp(mode
, "peripheral"))
213 pdata
->mode
= MUSB_PERIPHERAL
;
219 static int ux500_probe(struct platform_device
*pdev
)
221 struct resource musb_resources
[2];
222 struct musb_hdrc_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
223 struct device_node
*np
= pdev
->dev
.of_node
;
224 struct platform_device
*musb
;
225 struct ux500_glue
*glue
;
231 pdata
= ux500_of_probe(pdev
, np
);
235 pdev
->dev
.platform_data
= pdata
;
237 dev_err(&pdev
->dev
, "no pdata or device tree found\n");
242 glue
= devm_kzalloc(&pdev
->dev
, sizeof(*glue
), GFP_KERNEL
);
246 musb
= platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO
);
248 dev_err(&pdev
->dev
, "failed to allocate musb device\n");
252 clk
= devm_clk_get(&pdev
->dev
, NULL
);
254 dev_err(&pdev
->dev
, "failed to get clock\n");
259 ret
= clk_prepare_enable(clk
);
261 dev_err(&pdev
->dev
, "failed to enable clock\n");
265 musb
->dev
.parent
= &pdev
->dev
;
266 musb
->dev
.dma_mask
= &pdev
->dev
.coherent_dma_mask
;
267 musb
->dev
.coherent_dma_mask
= pdev
->dev
.coherent_dma_mask
;
269 glue
->dev
= &pdev
->dev
;
273 pdata
->platform_ops
= &ux500_ops
;
274 pdata
->config
= &ux500_musb_hdrc_config
;
276 platform_set_drvdata(pdev
, glue
);
278 memset(musb_resources
, 0x00, sizeof(*musb_resources
) *
279 ARRAY_SIZE(musb_resources
));
281 musb_resources
[0].name
= pdev
->resource
[0].name
;
282 musb_resources
[0].start
= pdev
->resource
[0].start
;
283 musb_resources
[0].end
= pdev
->resource
[0].end
;
284 musb_resources
[0].flags
= pdev
->resource
[0].flags
;
286 musb_resources
[1].name
= pdev
->resource
[1].name
;
287 musb_resources
[1].start
= pdev
->resource
[1].start
;
288 musb_resources
[1].end
= pdev
->resource
[1].end
;
289 musb_resources
[1].flags
= pdev
->resource
[1].flags
;
291 ret
= platform_device_add_resources(musb
, musb_resources
,
292 ARRAY_SIZE(musb_resources
));
294 dev_err(&pdev
->dev
, "failed to add resources\n");
298 ret
= platform_device_add_data(musb
, pdata
, sizeof(*pdata
));
300 dev_err(&pdev
->dev
, "failed to add platform_data\n");
304 ret
= platform_device_add(musb
);
306 dev_err(&pdev
->dev
, "failed to register musb device\n");
313 clk_disable_unprepare(clk
);
316 platform_device_put(musb
);
322 static int ux500_remove(struct platform_device
*pdev
)
324 struct ux500_glue
*glue
= platform_get_drvdata(pdev
);
326 platform_device_unregister(glue
->musb
);
327 clk_disable_unprepare(glue
->clk
);
332 #ifdef CONFIG_PM_SLEEP
333 static int ux500_suspend(struct device
*dev
)
335 struct ux500_glue
*glue
= dev_get_drvdata(dev
);
336 struct musb
*musb
= glue_to_musb(glue
);
339 usb_phy_set_suspend(musb
->xceiv
, 1);
341 clk_disable_unprepare(glue
->clk
);
346 static int ux500_resume(struct device
*dev
)
348 struct ux500_glue
*glue
= dev_get_drvdata(dev
);
349 struct musb
*musb
= glue_to_musb(glue
);
352 ret
= clk_prepare_enable(glue
->clk
);
354 dev_err(dev
, "failed to enable clock\n");
359 usb_phy_set_suspend(musb
->xceiv
, 0);
365 static SIMPLE_DEV_PM_OPS(ux500_pm_ops
, ux500_suspend
, ux500_resume
);
367 static const struct of_device_id ux500_match
[] = {
368 { .compatible
= "stericsson,db8500-musb", },
372 MODULE_DEVICE_TABLE(of
, ux500_match
);
374 static struct platform_driver ux500_driver
= {
375 .probe
= ux500_probe
,
376 .remove
= ux500_remove
,
378 .name
= "musb-ux500",
380 .of_match_table
= ux500_match
,
384 MODULE_DESCRIPTION("UX500 MUSB Glue Layer");
385 MODULE_AUTHOR("Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>");
386 MODULE_LICENSE("GPL v2");
387 module_platform_driver(ux500_driver
);