ehci-fsl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * Copyright 2005-2009 MontaVista Software, Inc.
  3. * Copyright 2008 Freescale Semiconductor, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software Foundation,
  17. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. *
  19. * Ported to 834x by Randy Vinson <rvinson@mvista.com> using code provided
  20. * by Hunter Wu.
  21. * Power Management support by Dave Liu <daveliu@freescale.com>,
  22. * Jerry Huang <Chang-Ming.Huang@freescale.com> and
  23. * Anton Vorontsov <avorontsov@ru.mvista.com>.
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/types.h>
  27. #include <linux/delay.h>
  28. #include <linux/pm.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/fsl_devices.h>
  31. #include "ehci-fsl.h"
  32. /* configure so an HC device and id are always provided */
  33. /* always called with process context; sleeping is OK */
  34. /**
  35. * usb_hcd_fsl_probe - initialize FSL-based HCDs
  36. * @drvier: Driver to be used for this HCD
  37. * @pdev: USB Host Controller being probed
  38. * Context: !in_interrupt()
  39. *
  40. * Allocates basic resources for this USB host controller.
  41. *
  42. */
  43. static int usb_hcd_fsl_probe(const struct hc_driver *driver,
  44. struct platform_device *pdev)
  45. {
  46. struct fsl_usb2_platform_data *pdata;
  47. struct usb_hcd *hcd;
  48. struct resource *res;
  49. int irq;
  50. int retval;
  51. unsigned int temp;
  52. pr_debug("initializing FSL-SOC USB Controller\n");
  53. /* Need platform data for setup */
  54. pdata = (struct fsl_usb2_platform_data *)pdev->dev.platform_data;
  55. if (!pdata) {
  56. dev_err(&pdev->dev,
  57. "No platform data for %s.\n", dev_name(&pdev->dev));
  58. return -ENODEV;
  59. }
  60. /*
  61. * This is a host mode driver, verify that we're supposed to be
  62. * in host mode.
  63. */
  64. if (!((pdata->operating_mode == FSL_USB2_DR_HOST) ||
  65. (pdata->operating_mode == FSL_USB2_MPH_HOST) ||
  66. (pdata->operating_mode == FSL_USB2_DR_OTG))) {
  67. dev_err(&pdev->dev,
  68. "Non Host Mode configured for %s. Wrong driver linked.\n",
  69. dev_name(&pdev->dev));
  70. return -ENODEV;
  71. }
  72. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  73. if (!res) {
  74. dev_err(&pdev->dev,
  75. "Found HC with no IRQ. Check %s setup!\n",
  76. dev_name(&pdev->dev));
  77. return -ENODEV;
  78. }
  79. irq = res->start;
  80. hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
  81. if (!hcd) {
  82. retval = -ENOMEM;
  83. goto err1;
  84. }
  85. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  86. if (!res) {
  87. dev_err(&pdev->dev,
  88. "Found HC with no register addr. Check %s setup!\n",
  89. dev_name(&pdev->dev));
  90. retval = -ENODEV;
  91. goto err2;
  92. }
  93. hcd->rsrc_start = res->start;
  94. hcd->rsrc_len = res->end - res->start + 1;
  95. if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len,
  96. driver->description)) {
  97. dev_dbg(&pdev->dev, "controller already in use\n");
  98. retval = -EBUSY;
  99. goto err2;
  100. }
  101. hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
  102. if (hcd->regs == NULL) {
  103. dev_dbg(&pdev->dev, "error mapping memory\n");
  104. retval = -EFAULT;
  105. goto err3;
  106. }
  107. /* Enable USB controller */
  108. temp = in_be32(hcd->regs + 0x500);
  109. out_be32(hcd->regs + 0x500, temp | 0x4);
  110. /* Set to Host mode */
  111. temp = in_le32(hcd->regs + 0x1a8);
  112. out_le32(hcd->regs + 0x1a8, temp | 0x3);
  113. retval = usb_add_hcd(hcd, irq, IRQF_DISABLED | IRQF_SHARED);
  114. if (retval != 0)
  115. goto err4;
  116. return retval;
  117. err4:
  118. iounmap(hcd->regs);
  119. err3:
  120. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  121. err2:
  122. usb_put_hcd(hcd);
  123. err1:
  124. dev_err(&pdev->dev, "init %s fail, %d\n", dev_name(&pdev->dev), retval);
  125. return retval;
  126. }
  127. /* may be called without controller electrically present */
  128. /* may be called with controller, bus, and devices active */
  129. /**
  130. * usb_hcd_fsl_remove - shutdown processing for FSL-based HCDs
  131. * @dev: USB Host Controller being removed
  132. * Context: !in_interrupt()
  133. *
  134. * Reverses the effect of usb_hcd_fsl_probe().
  135. *
  136. */
  137. static void usb_hcd_fsl_remove(struct usb_hcd *hcd,
  138. struct platform_device *pdev)
  139. {
  140. usb_remove_hcd(hcd);
  141. iounmap(hcd->regs);
  142. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  143. usb_put_hcd(hcd);
  144. }
  145. static void mpc83xx_setup_phy(struct ehci_hcd *ehci,
  146. enum fsl_usb2_phy_modes phy_mode,
  147. unsigned int port_offset)
  148. {
  149. u32 portsc = 0;
  150. switch (phy_mode) {
  151. case FSL_USB2_PHY_ULPI:
  152. portsc |= PORT_PTS_ULPI;
  153. break;
  154. case FSL_USB2_PHY_SERIAL:
  155. portsc |= PORT_PTS_SERIAL;
  156. break;
  157. case FSL_USB2_PHY_UTMI_WIDE:
  158. portsc |= PORT_PTS_PTW;
  159. /* fall through */
  160. case FSL_USB2_PHY_UTMI:
  161. portsc |= PORT_PTS_UTMI;
  162. break;
  163. case FSL_USB2_PHY_NONE:
  164. break;
  165. }
  166. ehci_writel(ehci, portsc, &ehci->regs->port_status[port_offset]);
  167. }
  168. static void mpc83xx_usb_setup(struct usb_hcd *hcd)
  169. {
  170. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  171. struct fsl_usb2_platform_data *pdata;
  172. void __iomem *non_ehci = hcd->regs;
  173. u32 temp;
  174. pdata =
  175. (struct fsl_usb2_platform_data *)hcd->self.controller->
  176. platform_data;
  177. /* Enable PHY interface in the control reg. */
  178. temp = in_be32(non_ehci + FSL_SOC_USB_CTRL);
  179. out_be32(non_ehci + FSL_SOC_USB_CTRL, temp | 0x00000004);
  180. out_be32(non_ehci + FSL_SOC_USB_SNOOP1, 0x0000001b);
  181. #if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
  182. /*
  183. * Turn on cache snooping hardware, since some PowerPC platforms
  184. * wholly rely on hardware to deal with cache coherent
  185. */
  186. /* Setup Snooping for all the 4GB space */
  187. /* SNOOP1 starts from 0x0, size 2G */
  188. out_be32(non_ehci + FSL_SOC_USB_SNOOP1, 0x0 | SNOOP_SIZE_2GB);
  189. /* SNOOP2 starts from 0x80000000, size 2G */
  190. out_be32(non_ehci + FSL_SOC_USB_SNOOP2, 0x80000000 | SNOOP_SIZE_2GB);
  191. #endif
  192. if ((pdata->operating_mode == FSL_USB2_DR_HOST) ||
  193. (pdata->operating_mode == FSL_USB2_DR_OTG))
  194. mpc83xx_setup_phy(ehci, pdata->phy_mode, 0);
  195. if (pdata->operating_mode == FSL_USB2_MPH_HOST) {
  196. unsigned int chip, rev, svr;
  197. svr = mfspr(SPRN_SVR);
  198. chip = svr >> 16;
  199. rev = (svr >> 4) & 0xf;
  200. /* Deal with USB Erratum #14 on MPC834x Rev 1.0 & 1.1 chips */
  201. if ((rev == 1) && (chip >= 0x8050) && (chip <= 0x8055))
  202. ehci->has_fsl_port_bug = 1;
  203. if (pdata->port_enables & FSL_USB2_PORT0_ENABLED)
  204. mpc83xx_setup_phy(ehci, pdata->phy_mode, 0);
  205. if (pdata->port_enables & FSL_USB2_PORT1_ENABLED)
  206. mpc83xx_setup_phy(ehci, pdata->phy_mode, 1);
  207. }
  208. /* put controller in host mode. */
  209. ehci_writel(ehci, 0x00000003, non_ehci + FSL_SOC_USB_USBMODE);
  210. #ifdef CONFIG_PPC_85xx
  211. out_be32(non_ehci + FSL_SOC_USB_PRICTRL, 0x00000008);
  212. out_be32(non_ehci + FSL_SOC_USB_AGECNTTHRSH, 0x00000080);
  213. #else
  214. out_be32(non_ehci + FSL_SOC_USB_PRICTRL, 0x0000000c);
  215. out_be32(non_ehci + FSL_SOC_USB_AGECNTTHRSH, 0x00000040);
  216. #endif
  217. out_be32(non_ehci + FSL_SOC_USB_SICTRL, 0x00000001);
  218. }
  219. /* called after powerup, by probe or system-pm "wakeup" */
  220. static int ehci_fsl_reinit(struct ehci_hcd *ehci)
  221. {
  222. mpc83xx_usb_setup(ehci_to_hcd(ehci));
  223. ehci_port_power(ehci, 0);
  224. return 0;
  225. }
  226. /* called during probe() after chip reset completes */
  227. static int ehci_fsl_setup(struct usb_hcd *hcd)
  228. {
  229. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  230. int retval;
  231. /* EHCI registers start at offset 0x100 */
  232. ehci->caps = hcd->regs + 0x100;
  233. ehci->regs = hcd->regs + 0x100 +
  234. HC_LENGTH(ehci_readl(ehci, &ehci->caps->hc_capbase));
  235. dbg_hcs_params(ehci, "reset");
  236. dbg_hcc_params(ehci, "reset");
  237. /* cache this readonly data; minimize chip reads */
  238. ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
  239. retval = ehci_halt(ehci);
  240. if (retval)
  241. return retval;
  242. /* data structure init */
  243. retval = ehci_init(hcd);
  244. if (retval)
  245. return retval;
  246. hcd->has_tt = 1;
  247. ehci->sbrn = 0x20;
  248. ehci_reset(ehci);
  249. retval = ehci_fsl_reinit(ehci);
  250. return retval;
  251. }
  252. struct ehci_fsl {
  253. struct ehci_hcd ehci;
  254. #ifdef CONFIG_PM
  255. /* Saved USB PHY settings, need to restore after deep sleep. */
  256. u32 usb_ctrl;
  257. #endif
  258. };
  259. #ifdef CONFIG_PM
  260. static struct ehci_fsl *hcd_to_ehci_fsl(struct usb_hcd *hcd)
  261. {
  262. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  263. return container_of(ehci, struct ehci_fsl, ehci);
  264. }
  265. static int ehci_fsl_drv_suspend(struct device *dev)
  266. {
  267. struct usb_hcd *hcd = dev_get_drvdata(dev);
  268. struct ehci_fsl *ehci_fsl = hcd_to_ehci_fsl(hcd);
  269. void __iomem *non_ehci = hcd->regs;
  270. if (!fsl_deep_sleep())
  271. return 0;
  272. ehci_fsl->usb_ctrl = in_be32(non_ehci + FSL_SOC_USB_CTRL);
  273. return 0;
  274. }
  275. static int ehci_fsl_drv_resume(struct device *dev)
  276. {
  277. struct usb_hcd *hcd = dev_get_drvdata(dev);
  278. struct ehci_fsl *ehci_fsl = hcd_to_ehci_fsl(hcd);
  279. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  280. void __iomem *non_ehci = hcd->regs;
  281. if (!fsl_deep_sleep())
  282. return 0;
  283. usb_root_hub_lost_power(hcd->self.root_hub);
  284. /* Restore USB PHY settings and enable the controller. */
  285. out_be32(non_ehci + FSL_SOC_USB_CTRL, ehci_fsl->usb_ctrl);
  286. ehci_reset(ehci);
  287. ehci_fsl_reinit(ehci);
  288. return 0;
  289. }
  290. static int ehci_fsl_drv_restore(struct device *dev)
  291. {
  292. struct usb_hcd *hcd = dev_get_drvdata(dev);
  293. usb_root_hub_lost_power(hcd->self.root_hub);
  294. return 0;
  295. }
  296. static struct dev_pm_ops ehci_fsl_pm_ops = {
  297. .suspend = ehci_fsl_drv_suspend,
  298. .resume = ehci_fsl_drv_resume,
  299. .restore = ehci_fsl_drv_restore,
  300. };
  301. #define EHCI_FSL_PM_OPS (&ehci_fsl_pm_ops)
  302. #else
  303. #define EHCI_FSL_PM_OPS NULL
  304. #endif /* CONFIG_PM */
  305. static const struct hc_driver ehci_fsl_hc_driver = {
  306. .description = hcd_name,
  307. .product_desc = "Freescale On-Chip EHCI Host Controller",
  308. .hcd_priv_size = sizeof(struct ehci_fsl),
  309. /*
  310. * generic hardware linkage
  311. */
  312. .irq = ehci_irq,
  313. .flags = HCD_USB2,
  314. /*
  315. * basic lifecycle operations
  316. */
  317. .reset = ehci_fsl_setup,
  318. .start = ehci_run,
  319. .stop = ehci_stop,
  320. .shutdown = ehci_shutdown,
  321. /*
  322. * managing i/o requests and associated device resources
  323. */
  324. .urb_enqueue = ehci_urb_enqueue,
  325. .urb_dequeue = ehci_urb_dequeue,
  326. .endpoint_disable = ehci_endpoint_disable,
  327. .endpoint_reset = ehci_endpoint_reset,
  328. /*
  329. * scheduling support
  330. */
  331. .get_frame_number = ehci_get_frame,
  332. /*
  333. * root hub support
  334. */
  335. .hub_status_data = ehci_hub_status_data,
  336. .hub_control = ehci_hub_control,
  337. .bus_suspend = ehci_bus_suspend,
  338. .bus_resume = ehci_bus_resume,
  339. .relinquish_port = ehci_relinquish_port,
  340. .port_handed_over = ehci_port_handed_over,
  341. .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
  342. };
  343. static int ehci_fsl_drv_probe(struct platform_device *pdev)
  344. {
  345. if (usb_disabled())
  346. return -ENODEV;
  347. /* FIXME we only want one one probe() not two */
  348. return usb_hcd_fsl_probe(&ehci_fsl_hc_driver, pdev);
  349. }
  350. static int ehci_fsl_drv_remove(struct platform_device *pdev)
  351. {
  352. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  353. /* FIXME we only want one one remove() not two */
  354. usb_hcd_fsl_remove(hcd, pdev);
  355. return 0;
  356. }
  357. MODULE_ALIAS("platform:fsl-ehci");
  358. static struct platform_driver ehci_fsl_driver = {
  359. .probe = ehci_fsl_drv_probe,
  360. .remove = ehci_fsl_drv_remove,
  361. .shutdown = usb_hcd_platform_shutdown,
  362. .driver = {
  363. .name = "fsl-ehci",
  364. .pm = EHCI_FSL_PM_OPS,
  365. },
  366. };