17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/errno.h>
22 #include <linux/prefetch.h>
36 static void tusb_musb_set_vbus(
struct musb *
musb,
int is_on);
38 #define TUSB_REV_MAJOR(reg_val) ((reg_val >> 4) & 0xf)
39 #define TUSB_REV_MINOR(reg_val) (reg_val & 0xf)
63 static int tusb_print_revision(
struct musb *
musb)
70 pr_info(
"tusb: %s%i.%i %s%i.%i %s%i.%i %s%i.%i %s%i %s%i.%i\n",
91 #define WBUS_QUIRK_MASK (TUSB_PHY_OTG_CTRL_TESTM2 | TUSB_PHY_OTG_CTRL_TESTM1 \
92 | TUSB_PHY_OTG_CTRL_TESTM0)
98 static void tusb_wbus_quirk(
struct musb *musb,
int enabled)
101 static u32 phy_otg_ctrl, phy_otg_ena;
142 for (i = 0; i < (len >> 2); i++) {
144 musb_writel(fifo, 0, val);
152 musb_writel(fifo, 0, val);
156 static inline void tusb_fifo_read_unaligned(
void __iomem *fifo,
163 for (i = 0; i < (len >> 2); i++) {
164 val = musb_readl(fifo, 0);
172 val = musb_readl(fifo, 0);
179 struct musb *musb = hw_ep->
musb;
180 void __iomem *ep_conf = hw_ep->conf;
187 'T', epnum, fifo, len, buf);
196 if (
likely((0x01 & (
unsigned long) buf) == 0)) {
199 if ((0x02 & (
unsigned long) buf) == 0) {
202 buf += (len & ~0x03);
211 for (i = 0; i < (len >> 2); i++) {
214 val |= (*(
u16 *)buf) << 16;
216 musb_writel(fifo, 0, val);
224 tusb_fifo_write_unaligned(fifo, buf, len);
229 struct musb *musb = hw_ep->
musb;
230 void __iomem *ep_conf = hw_ep->conf;
235 'R', epnum, fifo, len, buf);
243 if (
likely((0x01 & (
unsigned long) buf) == 0)) {
246 if ((0x02 & (
unsigned long) buf) == 0) {
248 readsl(fifo, buf, len >> 2);
249 buf += (len & ~0x03);
258 for (i = 0; i < (len >> 2); i++) {
259 val = musb_readl(fifo, 0);
260 *(
u16 *)buf = (
u16)(val & 0xffff);
262 *(
u16 *)buf = (
u16)(val >> 16);
271 tusb_fifo_read_unaligned(fifo, buf, len);
274 static struct musb *the_musb;
282 static int tusb_draw_power(
struct usb_phy *
x,
unsigned mA)
284 struct musb *musb = the_musb;
319 static void tusb_set_clock_source(
struct musb *musb,
unsigned mode)
346 static void tusb_allow_idle(
struct musb *musb,
u32 wakeup_enables)
353 tusb_wbus_quirk(musb, 1);
355 tusb_set_clock_source(musb, 0);
383 static int tusb_musb_vbus_status(
struct musb *musb)
386 u32 otg_stat, prcm_mngmt;
412 static void musb_do_idle(
unsigned long _musb)
414 struct musb *musb = (
void *)_musb;
419 switch (musb->
xceiv->state) {
429 tusb_musb_set_vbus(musb, 0);
449 tusb_allow_idle(musb, wakeups);
452 spin_unlock_irqrestore(&musb->
lock, flags);
468 static void tusb_musb_try_idle(
struct musb *musb,
unsigned long timeout)
471 static unsigned long last_timer;
474 timeout = default_timeout;
487 if (!timer_pending(&musb_idle_timer))
488 last_timer = timeout;
494 last_timer = timeout;
503 #define DEVCLOCK 60000000
504 #define OTG_TIMER_MS(msecs) ((msecs) \
505 ? (TUSB_DEV_OTG_TIMER_VAL((DEVCLOCK/1000)*(msecs)) \
506 | TUSB_DEV_OTG_TIMER_ENABLE) \
509 static void tusb_musb_set_vbus(
struct musb *musb,
int is_on)
541 switch (musb->
xceiv->state) {
586 static int tusb_musb_set_mode(
struct musb *musb,
u8 musb_mode)
589 u32 otg_stat, phy_otg_ctrl, phy_otg_ena, dev_conf;
629 INFO(
"Cannot be peripheral with mini-A cable "
630 "otg_stat: %08x\n", otg_stat);
635 static inline unsigned long
636 tusb_otg_ints(
struct musb *musb,
u32 int_src,
void __iomem *tbase)
649 tusb_musb_set_vbus(musb, default_a);
688 switch (musb->
xceiv->state) {
691 musb_platform_set_vbus(musb, 1);
717 tusb_musb_set_vbus(musb, 1);
721 tusb_musb_set_vbus(musb, 0);
737 switch (musb->
xceiv->state) {
745 != MUSB_DEVCTL_VBUS) {
755 ERR(
"vbus too slow, devctl %02x\n", devctl);
756 tusb_musb_set_vbus(musb, 0);
777 static irqreturn_t tusb_musb_interrupt(
int irq,
void *__hci)
779 struct musb *musb = __hci;
781 unsigned long flags, idle_timeout = 0;
801 tusb_wbus_quirk(musb, 0);
806 for (i = 0xf7f7f7; i > 0xf7f7f7 - 1000; i--) {
816 tusb_set_clock_source(musb, 1);
834 if (int_src & (TUSB_INT_SRC_VBUS_SENSE_CHNG
835 | TUSB_INT_SRC_OTG_TIMEOUT
836 | TUSB_INT_SRC_ID_STATUS_CHNG))
837 idle_timeout = tusb_otg_ints(musb, int_src, tbase);
847 real_dma_src = ~real_dma_src & dma_src;
849 int tx_source = (real_dma_src & 0xffff);
852 for (i = 1; i <= 15; i++) {
853 if (tx_source & (1 << i)) {
867 musb->
int_rx = (((musb_src >> 16) & 0xffff) << 1);
868 musb->
int_tx = (musb_src & 0xffff);
881 tusb_musb_try_idle(musb, idle_timeout);
884 spin_unlock_irqrestore(&musb->
lock, flags);
896 static void tusb_musb_enable(
struct musb *musb)
928 TUSB_INT_SRC_ID_STATUS_CHNG);
940 static void tusb_musb_disable(
struct musb *musb)
965 static void tusb_setup_cpu_interface(
struct musb *musb)
992 static int tusb_musb_start(
struct musb *musb)
1014 ret = tusb_print_revision(musb);
1026 tusb_set_clock_source(musb, 1);
1035 TUSB_PRCM_MNGMT_OTG_VBUS_DET_EN |
1037 tusb_setup_cpu_interface(musb);
1048 spin_unlock_irqrestore(&musb->
lock, flags);
1053 spin_unlock_irqrestore(&musb->
lock, flags);
1061 static int tusb_musb_init(
struct musb *musb)
1070 if (IS_ERR_OR_NULL(musb->
xceiv))
1077 musb->async = mem->
start;
1082 pr_debug(
"no sync dma resource?\n");
1086 musb->sync = mem->
start;
1090 pr_debug(
"ioremap for sync failed\n");
1094 musb->sync_va =
sync;
1101 ret = tusb_musb_start(musb);
1107 musb->
isr = tusb_musb_interrupt;
1109 musb->
xceiv->set_power = tusb_draw_power;
1112 setup_timer(&musb_idle_timer, musb_do_idle, (
unsigned long) musb);
1125 static int tusb_musb_exit(
struct musb *musb)
1141 .init = tusb_musb_init,
1142 .exit = tusb_musb_exit,
1144 .enable = tusb_musb_enable,
1145 .disable = tusb_musb_disable,
1147 .set_mode = tusb_musb_set_mode,
1148 .try_idle = tusb_musb_try_idle,
1150 .vbus_status = tusb_musb_vbus_status,
1151 .set_vbus = tusb_musb_set_vbus,
1167 dev_err(&pdev->
dev,
"failed to allocate glue context\n");
1174 dev_err(&pdev->
dev,
"failed to allocate musb id\n");
1181 dev_err(&pdev->
dev,
"failed to allocate musb device\n");
1186 musb->
dev.parent = &pdev->
dev;
1187 musb->
dev.dma_mask = &tusb_dmamask;
1188 musb->
dev.coherent_dma_mask = tusb_dmamask;
1195 platform_set_drvdata(pdev, glue);
1200 dev_err(&pdev->
dev,
"failed to add resources\n");
1206 dev_err(&pdev->
dev,
"failed to add platform_data\n");
1212 dev_err(&pdev->
dev,
"failed to register musb device\n");
1244 .probe = tusb_probe,
1247 .name =
"musb-tusb",
1255 static int __init tusb_init(
void)
1261 static void __exit tusb_exit(
void)