musb_ark.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. /*
  2. * Texas Instruments DSPS platforms "glue layer"
  3. *
  4. * Copyright (C) 2012, by Texas Instruments
  5. *
  6. * Based on the am35x "glue layer" code.
  7. *
  8. * This file is part of the Inventra Controller Driver for Linux.
  9. *
  10. * The Inventra Controller Driver for Linux is free software; you
  11. * can redistribute it and/or modify it under the terms of the GNU
  12. * General Public License version 2 as published by the Free Software
  13. * Foundation.
  14. *
  15. * The Inventra Controller Driver for Linux is distributed in
  16. * the hope that it will be useful, but WITHOUT ANY WARRANTY;
  17. * without even the implied warranty of MERCHANTABILITY or
  18. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  19. * License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with The Inventra Controller Driver for Linux ; if not,
  23. * write to the Free Software Foundation, Inc., 59 Temple Place,
  24. * Suite 330, Boston, MA 02111-1307 USA
  25. *
  26. * musb_ark.c will be a common file for all the TI DSPS platforms
  27. * such as dm64x, dm36x, dm35x, da8x, am35x and ti81x.
  28. * For now only ti81x is using this and in future davinci.c, am35x.c
  29. * da8xx.c would be merged to this file after testing.
  30. */
  31. #include <linux/module.h>
  32. #include <linux/kernel.h>
  33. #include <linux/sched.h>
  34. #include <linux/slab.h>
  35. #include <linux/io.h>
  36. #include <linux/err.h>
  37. #include <linux/platform_device.h>
  38. #include <linux/dma-mapping.h>
  39. #include <linux/pm_runtime.h>
  40. #include <linux/usb/usb_phy_generic.h>
  41. #include <linux/platform_data/usb-omap.h>
  42. #include <linux/sizes.h>
  43. #include <linux/of.h>
  44. #include <linux/of_device.h>
  45. #include <linux/of_address.h>
  46. #include <linux/of_irq.h>
  47. #include <linux/usb/of.h>
  48. #include <linux/gpio.h>
  49. #include <linux/debugfs.h>
  50. #include <linux/usb/musb.h>
  51. #include <linux/usb.h>
  52. #include "musb_core.h"
  53. #include "musb_ark.h"
  54. #include "musb_gadget.h"
  55. #include "cppi_dma.h"
  56. #include "musb_trace.h"
  57. /**
  58. * ARK glue structure.
  59. */
  60. struct ark_glue {
  61. struct device *dev;
  62. struct platform_device *plat_dev;
  63. struct musb *musb;
  64. //struct platform_device *phy;
  65. struct clk *clk;
  66. struct timer_list timer; /* otg_workaround timer */
  67. int gpio_id;
  68. int gpio_pwr;
  69. int usb_id_reg;
  70. int usb_id_offset;
  71. void __iomem *sys_base;
  72. unsigned int usb_softrest_reg_offset;
  73. unsigned int usb_softrest_bit_offset;
  74. unsigned int usbphy_softrest_bit_offset;
  75. };
  76. /* USBSS / USB ARK */
  77. #define USBSS_IRQ_STATUS 0x28
  78. #define USBSS_IRQ_ENABLER 0x2c
  79. #define USBSS_IRQ_CLEARR 0x30
  80. #define USBSS_IRQ_PD_COMP (1 << 2)
  81. #define POLL_SECONDS 2
  82. #define is_peripheral_enabled(musb) ((musb)->port_mode != MUSB_HOST)
  83. #define is_host_enabled(musb) ((musb)->port_mode != MUSB_PERIPHERAL)
  84. #define is_otg_enabled(musb) ((musb)->port_mode == MUSB_OTG)
  85. #define portstate(stmt) stmt
  86. static int dma_off = 1;
  87. static u64 musb_dmamask = DMA_BIT_MASK(32);
  88. /**
  89. * ark_musb_enable - enable interrupts
  90. */
  91. static void ark_musb_enable(struct musb *musb)
  92. {
  93. musb_writeb(musb->ctrl_base, MUSB_INTRRXE, BIT(1) | BIT(2) | BIT(3) | BIT(4));
  94. musb_writeb(musb->ctrl_base, MUSB_INTRTXE, BIT(1) | BIT(2) | BIT(3) | BIT(4));
  95. musb_writeb(musb->ctrl_base, MUSB_INTRUSBE, (0xff & (~(BIT(3)))) );
  96. musb_readb(musb->ctrl_base, MUSB_INTRRX);
  97. musb_readb(musb->ctrl_base, MUSB_INTRTX);
  98. musb_readb(musb->ctrl_base, MUSB_INTRUSB);
  99. if (is_dma_capable() && !dma_off)
  100. printk(KERN_WARNING "%s %s: dma not reactivated\n", __FILE__, __func__);
  101. else
  102. dma_off = 0;
  103. }
  104. /**
  105. * ark_musb_disable - disable HDRC and flush interrupts
  106. */
  107. static void ark_musb_disable(struct musb *musb)
  108. {
  109. musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
  110. if (is_dma_capable() && !dma_off)
  111. WARNING("dma still active\n");
  112. }
  113. static void ark_musb_otg_timer(struct timer_list *t)
  114. {
  115. struct ark_glue *glue = from_timer(glue, t, timer);
  116. struct musb *musb = glue->musb;
  117. void __iomem *mregs = musb->mregs;
  118. unsigned long flags;
  119. u8 devctl;
  120. /* We poll because ArkMicro's won't expose several OTG-critical
  121. * status change events (from the transceiver) otherwise.
  122. */
  123. devctl = musb_readb(mregs, MUSB_DEVCTL);
  124. //dev_dbg(musb->controller, "poll devctl %02x (%s)\n", devctl,
  125. // otg_state_string(musb->xceiv->state));
  126. dev_dbg(musb->controller, "poll devctl %02x (%d)\n", devctl, musb->xceiv->otg->state);
  127. spin_lock_irqsave(&musb->lock, flags);
  128. //switch (musb->xceiv->state) {
  129. switch (musb->xceiv->otg->state) {
  130. case OTG_STATE_A_WAIT_VFALL:
  131. /* Wait till VBUS falls below SessionEnd (~0.2V); the 1.3 RTL
  132. * seems to mis-handle session "start" otherwise (or in our
  133. * case "recover"), in routine "VBUS was valid by the time
  134. * VBUSERR got reported during enumeration" cases.
  135. */
  136. if (devctl & MUSB_DEVCTL_VBUS) {
  137. mod_timer(&glue->timer, jiffies + POLL_SECONDS * HZ);
  138. break;
  139. }
  140. musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE;
  141. musb_writel(musb->ctrl_base, ARK_USB_INT_SET_REG,
  142. MUSB_INTR_VBUSERROR << ARK_USB_USBINT_SHIFT);
  143. break;
  144. case OTG_STATE_B_IDLE:
  145. if (!is_peripheral_enabled(musb))
  146. break;
  147. /* There's no ID-changed IRQ, so we have no good way to tell
  148. * when to switch to the A-Default state machine (by setting
  149. * the DEVCTL.SESSION flag).
  150. *
  151. * Workaround: whenever we're in B_IDLE, try setting the
  152. * session flag every few seconds. If it works, ID was
  153. * grounded and we're now in the A-Default state machine.
  154. *
  155. * NOTE setting the session flag is _supposed_ to trigger
  156. * SRP, but clearly it doesn't.
  157. */
  158. musb_writeb(mregs, MUSB_DEVCTL, devctl | MUSB_DEVCTL_SESSION);
  159. devctl = musb_readb(mregs, MUSB_DEVCTL);
  160. if (devctl & MUSB_DEVCTL_BDEVICE)
  161. mod_timer(&glue->timer, jiffies + POLL_SECONDS * HZ);
  162. else
  163. musb->xceiv->otg->state = OTG_STATE_A_IDLE;
  164. break;
  165. default:
  166. break;
  167. }
  168. spin_unlock_irqrestore(&musb->lock, flags);
  169. }
  170. static irqreturn_t ark_musb_interrupt(int irq, void *hci)
  171. {
  172. struct musb *musb = (struct musb *)hci;
  173. void __iomem *reg_base = musb->ctrl_base;
  174. struct device *dev = musb->controller;
  175. struct ark_glue *glue = dev_get_drvdata(dev->parent);
  176. struct cppi *cppi;
  177. irqreturn_t ret = IRQ_NONE;
  178. unsigned long flags;
  179. spin_lock_irqsave(&musb->lock, flags);
  180. cppi = container_of(musb->dma_controller, struct cppi, controller);
  181. if (is_cppi_enabled() && musb->dma_controller && !cppi->irq)
  182. ret = cppi_interrupt(irq, hci);
  183. musb->int_rx = musb_readb(reg_base, MUSB_INTRRX);
  184. musb->int_tx = musb_readb(reg_base, MUSB_INTRTX);
  185. musb->int_usb = musb_readb(reg_base, MUSB_INTRUSB);
  186. /* DRVVBUS irqs are the only proxy we have (a very poor one!) for
  187. * ArkMicro's missing ID change IRQ. We need an ID change IRQ to
  188. * switch appropriately between halves of the OTG state machine.
  189. * Managing DEVCTL.SESSION per Mentor docs requires we know its
  190. * value, but DEVCTL.BDEVICE is invalid without DEVCTL.SESSION set.
  191. * Also, DRVVBUS pulses for SRP (but not at 5V) ...
  192. */
  193. if(0) {
  194. int err = 0;
  195. struct usb_otg *otg = musb->xceiv->otg;
  196. err = is_host_enabled(musb) && (musb->int_usb & MUSB_INTR_VBUSERROR);
  197. if (err) {
  198. /* The Mentor core doesn't debounce VBUS as needed
  199. * to cope with device connect current spikes. This
  200. * means it's not uncommon for bus-powered devices
  201. * to get VBUS errors during enumeration.
  202. *
  203. * This is a workaround, but newer RTL from Mentor
  204. * seems to allow a better one: "re"starting sessions
  205. * without waiting (on EVM, a **long** time) for VBUS
  206. * to stop registering in devctl.
  207. */
  208. musb->int_usb &= ~MUSB_INTR_VBUSERROR;
  209. musb->xceiv->otg->state = OTG_STATE_A_WAIT_VFALL;
  210. mod_timer(&glue->timer, jiffies + POLL_SECONDS * HZ);
  211. WARNING("VBUS error workaround (delay coming)\n");
  212. }
  213. else if (is_host_enabled(musb)) {
  214. MUSB_HST_MODE(musb);
  215. otg->default_a = 1;
  216. musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE;
  217. portstate(musb->port1_status |= USB_PORT_STAT_POWER);
  218. del_timer(&glue->timer);
  219. }
  220. else {
  221. musb->is_active = 0;
  222. MUSB_DEV_MODE(musb);
  223. otg->default_a = 0;
  224. musb->xceiv->otg->state = OTG_STATE_B_IDLE;
  225. portstate(musb->port1_status &= ~USB_PORT_STAT_POWER);
  226. }
  227. /* NOTE: this must complete poweron within 100 msec
  228. * (OTG_TIME_A_WAIT_VRISE) but we don't check for that.
  229. */
  230. ret = IRQ_HANDLED;
  231. }
  232. if (musb->int_tx || musb->int_rx || musb->int_usb)
  233. ret |= musb_interrupt(musb);
  234. /* poll for ID change */
  235. if (is_otg_enabled(musb) && (musb->xceiv->otg->state == OTG_STATE_B_IDLE))
  236. mod_timer(&glue->timer, jiffies + POLL_SECONDS * HZ);
  237. spin_unlock_irqrestore(&musb->lock, flags);
  238. return ret;
  239. }
  240. static int ark_musb_init(struct musb *musb)
  241. {
  242. struct device *dev = musb->controller;
  243. struct ark_glue *glue = dev_get_drvdata(dev->parent);
  244. struct platform_device *parent = to_platform_device(dev->parent);
  245. u32 val;
  246. if(!glue || !parent)
  247. return -ENODEV;
  248. glue->musb = musb;
  249. if (NULL == musb->xceiv) {
  250. char name[16] = {0};
  251. struct device_node *phynode = NULL;
  252. sprintf(name, "usb%d-phy", parent->id);
  253. phynode = of_find_node_by_name(phynode, name);
  254. if (NULL == phynode) {
  255. printk(KERN_ERR "*** ERROR: Failed to of_find_node_by_name \n");
  256. return PTR_ERR(musb->xceiv);
  257. }
  258. musb->xceiv = devm_usb_get_phy_by_node(&parent->dev, phynode, NULL);
  259. of_node_put(phynode);
  260. if (IS_ERR(musb->xceiv)) {
  261. printk(KERN_ERR "*** ERROR: Failed to usb_get_phy\n");
  262. return PTR_ERR(musb->xceiv);
  263. }
  264. }
  265. if(is_host_enabled(musb)) {
  266. timer_setup(&glue->timer, ark_musb_otg_timer, 0);
  267. }
  268. /* Reset the musb */
  269. val = musb_readb(musb->ctrl_base, 0x01);
  270. val |= BIT(3);
  271. musb_writeb(musb->ctrl_base, 0x01, val);
  272. msleep(5);
  273. musb->isr = ark_musb_interrupt;
  274. if(glue->gpio_pwr >= 0) {
  275. if (devm_gpio_request(dev, glue->gpio_pwr, "usb_pwr") < 0) {
  276. dev_err(dev, "Failed to request usb pwr gpio%d\n", glue->gpio_pwr);
  277. goto fail;
  278. }
  279. gpio_direction_output(glue->gpio_pwr, 1);
  280. }
  281. if(glue->gpio_id >= 0) {
  282. if (devm_gpio_request(dev, glue->gpio_id, "usb_id") < 0) {
  283. dev_err(dev, "Failed to request usb id gpio%d\n", glue->gpio_id);
  284. goto fail;
  285. }
  286. gpio_direction_output(glue->gpio_id, 0);
  287. }
  288. return 0;
  289. fail:
  290. return -ENODEV;
  291. }
  292. static int ark_musb_exit(struct musb *musb)
  293. {
  294. struct device *dev = musb->controller;
  295. struct ark_glue *glue = dev_get_drvdata(dev->parent);
  296. if(!glue || !musb->xceiv)
  297. return -ENODEV;
  298. if (is_host_enabled(musb))
  299. del_timer_sync(&glue->timer);
  300. /* delay, to avoid problems with module reload */
  301. if (is_host_enabled(musb) && musb->xceiv->otg->default_a) {
  302. int maxdelay = 2;
  303. u8 devctl, warn = 0;
  304. /* if there's no peripheral connected, this can take a
  305. * long time to fall, especially on EVM with huge C133.
  306. */
  307. do {
  308. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  309. if (!(devctl & MUSB_DEVCTL_VBUS))
  310. break;
  311. if ((devctl & MUSB_DEVCTL_VBUS) != warn) {
  312. warn = devctl & MUSB_DEVCTL_VBUS;
  313. dev_dbg(musb->controller, "VBUS %d\n",
  314. warn >> MUSB_DEVCTL_VBUS_SHIFT);
  315. }
  316. msleep(10);
  317. maxdelay--;
  318. } while (maxdelay > 0);
  319. /* in OTG mode, another host might be connected */
  320. if (devctl & MUSB_DEVCTL_VBUS)
  321. dev_dbg(musb->controller, "VBUS off timeout (devctl %02x)\n", devctl);
  322. }
  323. usb_put_phy(musb->xceiv);
  324. return 0;
  325. }
  326. static int ark_musb_set_mode(struct musb *musb, u8 mode)
  327. {
  328. struct device *dev = musb->controller;
  329. struct ark_glue *glue = dev_get_drvdata(dev->parent);
  330. int gpio_pwr = glue->gpio_pwr;
  331. int gpio_id = glue->gpio_id;
  332. int usb_id_reg = glue->usb_id_reg;
  333. int usb_id_offset = glue->usb_id_offset;
  334. u32 bitoffset = glue->usb_softrest_bit_offset;
  335. u32 phybitoffset = glue->usbphy_softrest_bit_offset;
  336. void __iomem *sys_softrest_base = (void __iomem *)((unsigned int)glue->sys_base + glue->usb_softrest_reg_offset);
  337. u32 regval;
  338. /*usb_hcd_resume_root_hub(musb->hcd);
  339. musb_root_disconnect(musb);
  340. musb_g_reset(musb);*/
  341. switch (mode) {
  342. case MUSB_HOST:
  343. break;
  344. case MUSB_PERIPHERAL:
  345. dev_info(musb->controller, "+Switch peripheral %d %d=== \n", gpio_id, gpio_pwr);
  346. musb->xceiv->otg->state = OTG_STATE_B_IDLE;
  347. if (gpio_id >= 0)
  348. gpio_set_value(gpio_id, 1);
  349. else if((usb_id_reg >= 0) && (usb_id_offset >= 0)) {
  350. void __iomem *sys_usb_id_base = (void __iomem *)((unsigned int)glue->sys_base + glue->usb_id_reg);
  351. regval = readl(sys_usb_id_base);
  352. regval |= (1<<usb_id_offset);
  353. writel(regval, sys_usb_id_base);
  354. }
  355. regval = readl(sys_softrest_base);
  356. regval &= (~((1 << bitoffset) | (1 << phybitoffset)));//usb and usbphy
  357. writel(regval, sys_softrest_base);
  358. mdelay(1);
  359. regval = readl(sys_softrest_base);
  360. regval |= ((1 << bitoffset) | (1 << phybitoffset));
  361. writel(regval, sys_softrest_base);
  362. mdelay(1);
  363. musb_writeb(musb->mregs, MUSB_DEVCTL, 0x99);
  364. musb_writeb(musb->mregs,0x01,0xf0);
  365. musb_g_reset(musb);
  366. regval = musb_readb(musb->mregs, MUSB_INTRUSBE);
  367. musb_writeb(musb->mregs, MUSB_INTRUSBE, regval | MUSB_INTR_SUSPEND);
  368. break;
  369. case MUSB_OTG:
  370. dev_info(musb->controller, "+++Switch OTG %d %d===+++ \n", gpio_id, gpio_pwr);
  371. musb->xceiv->otg->state = OTG_STATE_A_HOST;
  372. if (gpio_id >= 0)
  373. gpio_set_value(gpio_id, 0);
  374. else if((usb_id_reg >= 0) && (usb_id_offset >= 0)) {
  375. void __iomem *sys_usb_id_base = (void __iomem *)((unsigned int)glue->sys_base + glue->usb_id_reg);
  376. regval = readl(sys_usb_id_base);
  377. regval &= ~(1<<usb_id_offset);
  378. writel(regval, sys_usb_id_base);
  379. }
  380. regval = readl(sys_softrest_base);
  381. regval &= (~(1 << bitoffset));
  382. writel(regval, sys_softrest_base);
  383. mdelay(1);
  384. regval = readl(sys_softrest_base);
  385. regval |= ((1 << bitoffset));
  386. writel(regval, sys_softrest_base);
  387. mdelay(1);
  388. musb_writeb(musb->mregs, MUSB_INTRUSBE, 0xf4);
  389. musb_writeb(musb->mregs, MUSB_DEVCTL, 0x99);
  390. if (gpio_pwr >= 0) {
  391. gpio_set_value(gpio_pwr, 0);
  392. mdelay(10);
  393. gpio_set_value(gpio_pwr, 1);
  394. }
  395. break;
  396. default:
  397. dev_err(glue->dev, "unsupported mode %d\n", mode);
  398. return -EINVAL;
  399. }
  400. return 0;
  401. }
  402. static void ark_musb_set_vbus(struct musb *musb, int is_on)
  403. {
  404. struct usb_otg *otg = musb->xceiv->otg;
  405. u8 devctl;
  406. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  407. int ret = 1;
  408. /* HDRC controls CPEN, but beware current surges during device
  409. * connect. They can trigger transient overcurrent conditions
  410. * that must be ignored.
  411. */
  412. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  413. if (is_on) {
  414. if (musb->xceiv->otg->state == OTG_STATE_A_IDLE) {
  415. /* start the session */
  416. devctl |= MUSB_DEVCTL_SESSION;
  417. musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  418. /*
  419. * Wait for the musb to set as A device to enable the VBUS
  420. */
  421. while (musb_readb(musb->mregs, MUSB_DEVCTL) & 0x80) {
  422. cpu_relax();
  423. if (time_after(jiffies, timeout)) {
  424. dev_err(musb->controller, "configured as A device timeout");
  425. ret = -EINVAL;
  426. break;
  427. }
  428. }
  429. if (ret && otg->set_vbus)
  430. otg_set_vbus(otg, 1);
  431. } else {
  432. musb->is_active = 1;
  433. otg->default_a = 1;
  434. musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE;
  435. devctl |= MUSB_DEVCTL_SESSION;
  436. MUSB_HST_MODE(musb);
  437. }
  438. } else {
  439. musb->is_active = 0;
  440. /* NOTE: we're skipping A_WAIT_VFALL -> A_IDLE and
  441. * jumping right to B_IDLE...
  442. */
  443. otg->default_a = 0;
  444. musb->xceiv->otg->state = OTG_STATE_B_IDLE;
  445. devctl &= ~MUSB_DEVCTL_SESSION;
  446. MUSB_DEV_MODE(musb);
  447. }
  448. musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  449. }
  450. #ifdef CONFIG_USB_TI_CPPI41_DMA
  451. static void ark_dma_controller_callback(struct dma_controller *c)
  452. {
  453. struct musb *musb = c->musb;
  454. struct ark_glue *glue = dev_get_drvdata(musb->controller->parent);
  455. void __iomem *usbss_base = musb->ctrl_base;
  456. u32 status;
  457. status = musb_readl(usbss_base, USBSS_IRQ_STATUS);
  458. if (status & USBSS_IRQ_PD_COMP)
  459. musb_writel(usbss_base, USBSS_IRQ_STATUS, USBSS_IRQ_PD_COMP);
  460. }
  461. static struct dma_controller *ark_dma_controller_create(struct musb *musb, void __iomem *base)
  462. {
  463. struct dma_controller *controller;
  464. struct ark_glue *glue = dev_get_drvdata(musb->controller->parent);
  465. void __iomem *usbss_base = musb->ctrl_base;
  466. controller = cppi41_dma_controller_create(musb, base);
  467. if (IS_ERR_OR_NULL(controller))
  468. return controller;
  469. musb_writel(usbss_base, USBSS_IRQ_ENABLER, USBSS_IRQ_PD_COMP);
  470. controller->dma_callback = ark_dma_controller_callback;
  471. return controller;
  472. }
  473. #endif /* CONFIG_USB_TI_CPPI41_DMA */
  474. static struct musb_platform_ops musb_ark_ops = {
  475. .init = ark_musb_init,
  476. .exit = ark_musb_exit,
  477. #if defined(CONFIG_USB_TI_CPPI41_DMA)
  478. .quirks = MUSB_DMA_CPPI41 | MUSB_INDEXED_EP,
  479. .dma_init = ark_dma_controller_create,
  480. .dma_exit = cppi41_dma_controller_destroy,
  481. #elif defined(CONFIG_USB_INVENTRA_DMA)
  482. .quirks = MUSB_INDEXED_EP | MUSB_DMA_INVENTRA,
  483. .dma_init = musbhs_dma_controller_create,
  484. .dma_exit = musbhs_dma_controller_destroy,
  485. #endif
  486. .enable = ark_musb_enable,
  487. .disable = ark_musb_disable,
  488. .set_mode = ark_musb_set_mode,
  489. .set_vbus = ark_musb_set_vbus,
  490. .fifo_mode = 2,
  491. };
  492. static int get_int_prop(struct device_node *dn, const char *s)
  493. {
  494. int ret;
  495. u32 val;
  496. ret = of_property_read_u32(dn, s, &val);
  497. if (ret)
  498. return 0;
  499. return val;
  500. }
  501. static int get_musb_port_mode(struct device *dev)
  502. {
  503. enum usb_dr_mode mode;
  504. mode = usb_get_dr_mode(dev);
  505. switch (mode) {
  506. case USB_DR_MODE_HOST:
  507. return MUSB_HOST;
  508. case USB_DR_MODE_PERIPHERAL:
  509. return MUSB_PERIPHERAL;
  510. case USB_DR_MODE_UNKNOWN:
  511. case USB_DR_MODE_OTG:
  512. default:
  513. return MUSB_OTG;
  514. }
  515. }
  516. static const struct of_device_id musb_ark_of_match[] = {
  517. { .compatible = "arkmicro,ark-musb"},
  518. { },
  519. };
  520. MODULE_DEVICE_TABLE(of, musb_ark_of_match);
  521. static int musb_ark_probe(struct platform_device *pdev)
  522. {
  523. const struct of_device_id *match;
  524. struct ark_glue *glue;
  525. struct musb_hdrc_platform_data pdata;
  526. struct resource resources[3];
  527. struct resource *res;
  528. struct musb_hdrc_config *config;
  529. struct platform_device *plat_dev;
  530. struct device_node *dn = pdev->dev.of_node;
  531. int val;
  532. int ret = 0;
  533. match = of_match_node(musb_ark_of_match, dn);
  534. if (!match) {
  535. dev_err(&pdev->dev, "Failed to get matching of_match struct\n");
  536. return -EINVAL;
  537. }
  538. memset(resources, 0, sizeof(resources));
  539. /* allocate glue */
  540. glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
  541. if (!glue) {
  542. dev_err(&pdev->dev, "Failed to devm_kzalloc flue\n");
  543. return -ENOMEM;
  544. }
  545. glue->dev = &pdev->dev;
  546. ret = of_property_read_u32(dn, "gpio-id", &glue->gpio_id);
  547. if(ret) {
  548. return -EINVAL;
  549. }
  550. ret = of_property_read_u32(dn, "usb-id-reg", &glue->usb_id_reg);
  551. if(ret == 0) {
  552. ret = of_property_read_u32(dn, "usb-id-offset", &glue->usb_id_offset);
  553. if(ret) {
  554. glue->usb_id_offset = -1;
  555. }
  556. } else {
  557. glue->usb_id_reg = -1;
  558. }
  559. ret = of_property_read_u32(dn, "gpio-pwr", &glue->gpio_pwr);
  560. if(ret) {
  561. return -EINVAL;
  562. }
  563. ret = of_property_read_u32(dn, "sys-softrest-regoffset", &glue->usb_softrest_reg_offset);
  564. if(ret) {
  565. return -EINVAL;
  566. }
  567. ret = of_property_read_u32(dn, "usb-softrest-bitoffset", &glue->usb_softrest_bit_offset);
  568. if(ret) {
  569. return -EINVAL;
  570. }
  571. ret = of_property_read_u32(dn, "usbphy-softrest-bitoffset", &glue->usbphy_softrest_bit_offset);
  572. if(ret) {
  573. glue->usbphy_softrest_bit_offset = glue->usb_softrest_bit_offset + 1;
  574. }
  575. //if (usb_get_dr_mode(&pdev->dev) == USB_DR_MODE_PERIPHERAL) {
  576. //}
  577. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  578. if (!res) {
  579. dev_err(&pdev->dev, "Failed to get usb_base memory.\n");
  580. return -EINVAL;
  581. }
  582. resources[0] = *res;
  583. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  584. if (!res) {
  585. dev_err(&pdev->dev, "Failed to get sys_base memory.\n");
  586. return -EINVAL;
  587. }
  588. glue->sys_base = ioremap(res->start, resource_size(res));
  589. if(IS_ERR(glue->sys_base)) {
  590. dev_err(&pdev->dev, "Failed to devm_ioremap_resource\n");
  591. return PTR_ERR(glue->sys_base);
  592. }
  593. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  594. if (!res) {
  595. dev_err(&pdev->dev, "Failed to get irq.\n");
  596. ret = -EINVAL;
  597. goto err_iounmap;
  598. }
  599. resources[1] = *res;
  600. res = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
  601. if (!res) {
  602. dev_err(&pdev->dev, "Failed to get dma irq.\n");
  603. ret = -EINVAL;
  604. goto err_iounmap;
  605. }
  606. resources[2] = *res;
  607. //glue->phy = usb_phy_generic_register();
  608. pdev->id = of_alias_get_id(dn, "usb");
  609. if(pdev->id < 0) {
  610. dev_err(&pdev->dev, "Failed to get usb id\n");
  611. }
  612. platform_set_drvdata(pdev, glue);
  613. /* allocate the child platform device */
  614. plat_dev = platform_device_alloc("musb-hdrc", pdev->id);
  615. if (!plat_dev) {
  616. dev_err(&pdev->dev, "Failed to allocate musb device\n");
  617. ret = -EINVAL;
  618. goto err_iounmap;
  619. }
  620. plat_dev->dev.parent = &pdev->dev;
  621. plat_dev->dev.dma_mask = &musb_dmamask;
  622. plat_dev->dev.coherent_dma_mask = musb_dmamask;
  623. glue->plat_dev = plat_dev;
  624. ret = platform_device_add_resources(plat_dev, resources,
  625. ARRAY_SIZE(resources));
  626. if (ret) {
  627. dev_err(&pdev->dev, "Failed to add resources\n");
  628. goto err;
  629. }
  630. config = devm_kzalloc(&pdev->dev, sizeof(*config), GFP_KERNEL);
  631. if (!config) {
  632. ret = -ENOMEM;
  633. goto err;
  634. }
  635. pdata.config = config;
  636. pdata.platform_ops = &musb_ark_ops;
  637. config->num_eps = get_int_prop(dn, "num-eps");
  638. config->ram_bits = get_int_prop(dn, "ram-bits");
  639. //config->host_port_deassert_reset_at_resume = 1;
  640. pdata.mode = get_musb_port_mode(&pdev->dev);
  641. /* DT keeps this entry in mA, musb expects it as per USB spec */
  642. pdata.power = get_int_prop(dn, "power") / 2;
  643. ret = of_property_read_u32(dn, "multipoint", &val);
  644. if (!ret && val)
  645. config->multipoint = true;
  646. #if 0
  647. ret = of_property_read_u32(dn, "dma", &val);
  648. if (!ret && val)
  649. config->dma = true;
  650. config->dma_channels = get_int_prop(dn, "dma_channels");
  651. #endif
  652. config->maximum_speed = usb_get_maximum_speed(&pdev->dev);
  653. switch (config->maximum_speed) {
  654. case USB_SPEED_LOW:
  655. case USB_SPEED_FULL:
  656. break;
  657. case USB_SPEED_SUPER:
  658. dev_warn(&pdev->dev, "ignore incorrect maximum_speed "
  659. "(super-speed) setting in dts");
  660. /* fall through */
  661. default:
  662. config->maximum_speed = USB_SPEED_HIGH;
  663. break;
  664. }
  665. ret = platform_device_add_data(plat_dev, &pdata, sizeof(pdata));
  666. if (ret) {
  667. dev_err(&pdev->dev, "Failed to add platform_data\n");
  668. goto err;
  669. }
  670. ret = platform_device_add(plat_dev);
  671. if (ret) {
  672. dev_err(&pdev->dev, "Failed to register musb device\n");
  673. goto err;
  674. }
  675. printk("musb_ark_probe succss\n");
  676. return 0;
  677. err:
  678. platform_device_put(plat_dev);
  679. err_iounmap:
  680. //if(glue->phy)
  681. // usb_phy_generic_unregister(glue->phy);
  682. if(glue->sys_base)
  683. iounmap(glue->sys_base);
  684. return ret;
  685. }
  686. static int musb_ark_remove(struct platform_device *pdev)
  687. {
  688. struct ark_glue *glue = platform_get_drvdata(pdev);
  689. if(glue->plat_dev)
  690. platform_device_unregister(glue->plat_dev);
  691. //if(glue->phy)
  692. // usb_phy_generic_unregister(glue->phy);
  693. if(glue->sys_base)
  694. iounmap(glue->sys_base);
  695. return 0;
  696. }
  697. #ifdef CONFIG_PM_SLEEP
  698. static int musb_ark_suspend(struct device *dev)
  699. {
  700. return 0;
  701. }
  702. static int musb_ark_resume(struct device *dev)
  703. {
  704. return 0;
  705. }
  706. #endif
  707. //static SIMPLE_DEV_PM_OPS(musb_ark_pm_ops, musb_ark_suspend, musb_ark_resume);
  708. static struct platform_driver musb_ark_driver = {
  709. .probe = musb_ark_probe,
  710. .remove = musb_ark_remove,
  711. .driver = {
  712. .name = "musb-ark",
  713. //.pm = &musb_ark_pm_ops,
  714. .of_match_table = musb_ark_of_match,
  715. },
  716. };
  717. MODULE_DESCRIPTION("ARKMICRO MUSB Glue Layer");
  718. MODULE_AUTHOR("Arkmicro");
  719. MODULE_LICENSE("GPL v2");
  720. module_platform_driver(musb_ark_driver);