ehci-pxa168.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * drivers/usb/host/ehci-pxa168.c
  3. *
  4. * Tanmay Upadhyay <tanmay.upadhyay@einfochips.com>
  5. *
  6. * Based on drivers/usb/host/ehci-orion.c
  7. *
  8. * This file is licensed under the terms of the GNU General Public
  9. * License version 2. This program is licensed "as is" without any
  10. * warranty of any kind, whether express or implied.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/clk.h>
  16. #include <mach/pxa168.h>
  17. #define USB_PHY_CTRL_REG 0x4
  18. #define USB_PHY_PLL_REG 0x8
  19. #define USB_PHY_TX_REG 0xc
  20. #define FBDIV_SHIFT 4
  21. #define ICP_SHIFT 12
  22. #define ICP_15 2
  23. #define ICP_20 3
  24. #define ICP_25 4
  25. #define KVCO_SHIFT 15
  26. #define PLLCALI12_SHIFT 25
  27. #define CALI12_VDD 0
  28. #define CALI12_09 1
  29. #define CALI12_10 2
  30. #define CALI12_11 3
  31. #define PLLVDD12_SHIFT 27
  32. #define VDD12_VDD 0
  33. #define VDD12_10 1
  34. #define VDD12_11 2
  35. #define VDD12_12 3
  36. #define PLLVDD18_SHIFT 29
  37. #define VDD18_19 0
  38. #define VDD18_20 1
  39. #define VDD18_21 2
  40. #define VDD18_22 3
  41. #define PLL_READY (1 << 23)
  42. #define VCOCAL_START (1 << 21)
  43. #define REG_RCAL_START (1 << 12)
  44. struct pxa168_usb_drv_data {
  45. struct ehci_hcd ehci;
  46. struct clk *pxa168_usb_clk;
  47. struct resource *usb_phy_res;
  48. void __iomem *usb_phy_reg_base;
  49. };
  50. static int ehci_pxa168_setup(struct usb_hcd *hcd)
  51. {
  52. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  53. int retval;
  54. ehci_reset(ehci);
  55. retval = ehci_halt(ehci);
  56. if (retval)
  57. return retval;
  58. /*
  59. * data structure init
  60. */
  61. retval = ehci_init(hcd);
  62. if (retval)
  63. return retval;
  64. hcd->has_tt = 1;
  65. ehci_port_power(ehci, 0);
  66. return retval;
  67. }
  68. static const struct hc_driver ehci_pxa168_hc_driver = {
  69. .description = hcd_name,
  70. .product_desc = "Marvell PXA168 EHCI",
  71. .hcd_priv_size = sizeof(struct pxa168_usb_drv_data),
  72. /*
  73. * generic hardware linkage
  74. */
  75. .irq = ehci_irq,
  76. .flags = HCD_MEMORY | HCD_USB2,
  77. /*
  78. * basic lifecycle operations
  79. */
  80. .reset = ehci_pxa168_setup,
  81. .start = ehci_run,
  82. .stop = ehci_stop,
  83. .shutdown = ehci_shutdown,
  84. /*
  85. * managing i/o requests and associated device resources
  86. */
  87. .urb_enqueue = ehci_urb_enqueue,
  88. .urb_dequeue = ehci_urb_dequeue,
  89. .endpoint_disable = ehci_endpoint_disable,
  90. .endpoint_reset = ehci_endpoint_reset,
  91. /*
  92. * scheduling support
  93. */
  94. .get_frame_number = ehci_get_frame,
  95. /*
  96. * root hub support
  97. */
  98. .hub_status_data = ehci_hub_status_data,
  99. .hub_control = ehci_hub_control,
  100. .bus_suspend = ehci_bus_suspend,
  101. .bus_resume = ehci_bus_resume,
  102. .relinquish_port = ehci_relinquish_port,
  103. .port_handed_over = ehci_port_handed_over,
  104. .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
  105. };
  106. static int pxa168_usb_phy_init(struct platform_device *pdev)
  107. {
  108. struct resource *res;
  109. void __iomem *usb_phy_reg_base;
  110. struct pxa168_usb_pdata *pdata;
  111. struct pxa168_usb_drv_data *drv_data;
  112. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  113. unsigned long reg_val;
  114. int pll_retry_cont = 10000, err = 0;
  115. drv_data = (struct pxa168_usb_drv_data *)hcd->hcd_priv;
  116. pdata = (struct pxa168_usb_pdata *)pdev->dev.platform_data;
  117. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  118. if (!res) {
  119. dev_err(&pdev->dev,
  120. "Found HC with no PHY register addr. Check %s setup!\n",
  121. dev_name(&pdev->dev));
  122. return -ENODEV;
  123. }
  124. if (!request_mem_region(res->start, resource_size(res),
  125. ehci_pxa168_hc_driver.description)) {
  126. dev_dbg(&pdev->dev, "controller already in use\n");
  127. return -EBUSY;
  128. }
  129. usb_phy_reg_base = ioremap(res->start, resource_size(res));
  130. if (usb_phy_reg_base == NULL) {
  131. dev_dbg(&pdev->dev, "error mapping memory\n");
  132. err = -EFAULT;
  133. goto err1;
  134. }
  135. drv_data->usb_phy_reg_base = usb_phy_reg_base;
  136. drv_data->usb_phy_res = res;
  137. /* If someone wants to init USB phy in board specific way */
  138. if (pdata && pdata->phy_init)
  139. return pdata->phy_init(usb_phy_reg_base);
  140. /* Power up the PHY and PLL */
  141. writel(readl(usb_phy_reg_base + USB_PHY_CTRL_REG) | 0x3,
  142. usb_phy_reg_base + USB_PHY_CTRL_REG);
  143. /* Configure PHY PLL */
  144. reg_val = readl(usb_phy_reg_base + USB_PHY_PLL_REG) & ~(0x7e03ffff);
  145. reg_val |= (VDD18_22 << PLLVDD18_SHIFT | VDD12_12 << PLLVDD12_SHIFT |
  146. CALI12_11 << PLLCALI12_SHIFT | 3 << KVCO_SHIFT |
  147. ICP_15 << ICP_SHIFT | 0xee << FBDIV_SHIFT | 0xb);
  148. writel(reg_val, usb_phy_reg_base + USB_PHY_PLL_REG);
  149. /* Make sure PHY PLL is ready */
  150. while (!(readl(usb_phy_reg_base + USB_PHY_PLL_REG) & PLL_READY)) {
  151. if (!(pll_retry_cont--)) {
  152. dev_dbg(&pdev->dev, "USB PHY PLL not ready\n");
  153. err = -EIO;
  154. goto err2;
  155. }
  156. }
  157. /* Toggle VCOCAL_START bit of U2PLL for PLL calibration */
  158. udelay(200);
  159. writel(readl(usb_phy_reg_base + USB_PHY_PLL_REG) | VCOCAL_START,
  160. usb_phy_reg_base + USB_PHY_PLL_REG);
  161. udelay(40);
  162. writel(readl(usb_phy_reg_base + USB_PHY_PLL_REG) & ~VCOCAL_START,
  163. usb_phy_reg_base + USB_PHY_PLL_REG);
  164. /* Toggle REG_RCAL_START bit of U2PTX for impedance calibration */
  165. udelay(400);
  166. writel(readl(usb_phy_reg_base + USB_PHY_TX_REG) | REG_RCAL_START,
  167. usb_phy_reg_base + USB_PHY_TX_REG);
  168. udelay(40);
  169. writel(readl(usb_phy_reg_base + USB_PHY_TX_REG) & ~REG_RCAL_START,
  170. usb_phy_reg_base + USB_PHY_TX_REG);
  171. /* Make sure PHY PLL is ready again */
  172. pll_retry_cont = 0;
  173. while (!(readl(usb_phy_reg_base + USB_PHY_PLL_REG) & PLL_READY)) {
  174. if (!(pll_retry_cont--)) {
  175. dev_dbg(&pdev->dev, "USB PHY PLL not ready\n");
  176. err = -EIO;
  177. goto err2;
  178. }
  179. }
  180. return 0;
  181. err2:
  182. iounmap(usb_phy_reg_base);
  183. err1:
  184. release_mem_region(res->start, resource_size(res));
  185. return err;
  186. }
  187. static int __devinit ehci_pxa168_drv_probe(struct platform_device *pdev)
  188. {
  189. struct resource *res;
  190. struct usb_hcd *hcd;
  191. struct ehci_hcd *ehci;
  192. struct pxa168_usb_drv_data *drv_data;
  193. void __iomem *regs;
  194. int irq, err = 0;
  195. if (usb_disabled())
  196. return -ENODEV;
  197. pr_debug("Initializing pxa168-SoC USB Host Controller\n");
  198. irq = platform_get_irq(pdev, 0);
  199. if (irq <= 0) {
  200. dev_err(&pdev->dev,
  201. "Found HC with no IRQ. Check %s setup!\n",
  202. dev_name(&pdev->dev));
  203. err = -ENODEV;
  204. goto err1;
  205. }
  206. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  207. if (!res) {
  208. dev_err(&pdev->dev,
  209. "Found HC with no register addr. Check %s setup!\n",
  210. dev_name(&pdev->dev));
  211. err = -ENODEV;
  212. goto err1;
  213. }
  214. if (!request_mem_region(res->start, resource_size(res),
  215. ehci_pxa168_hc_driver.description)) {
  216. dev_dbg(&pdev->dev, "controller already in use\n");
  217. err = -EBUSY;
  218. goto err1;
  219. }
  220. regs = ioremap(res->start, resource_size(res));
  221. if (regs == NULL) {
  222. dev_dbg(&pdev->dev, "error mapping memory\n");
  223. err = -EFAULT;
  224. goto err2;
  225. }
  226. hcd = usb_create_hcd(&ehci_pxa168_hc_driver,
  227. &pdev->dev, dev_name(&pdev->dev));
  228. if (!hcd) {
  229. err = -ENOMEM;
  230. goto err3;
  231. }
  232. drv_data = (struct pxa168_usb_drv_data *)hcd->hcd_priv;
  233. /* Enable USB clock */
  234. drv_data->pxa168_usb_clk = clk_get(&pdev->dev, "PXA168-USBCLK");
  235. if (IS_ERR(drv_data->pxa168_usb_clk)) {
  236. dev_err(&pdev->dev, "Couldn't get USB clock\n");
  237. err = PTR_ERR(drv_data->pxa168_usb_clk);
  238. goto err4;
  239. }
  240. clk_enable(drv_data->pxa168_usb_clk);
  241. err = pxa168_usb_phy_init(pdev);
  242. if (err) {
  243. dev_err(&pdev->dev, "USB PHY initialization failed\n");
  244. goto err5;
  245. }
  246. hcd->rsrc_start = res->start;
  247. hcd->rsrc_len = resource_size(res);
  248. hcd->regs = regs;
  249. ehci = hcd_to_ehci(hcd);
  250. ehci->caps = hcd->regs + 0x100;
  251. ehci->regs = hcd->regs + 0x100 +
  252. HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
  253. ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
  254. hcd->has_tt = 1;
  255. ehci->sbrn = 0x20;
  256. err = usb_add_hcd(hcd, irq, IRQF_SHARED | IRQF_DISABLED);
  257. if (err)
  258. goto err5;
  259. return 0;
  260. err5:
  261. clk_disable(drv_data->pxa168_usb_clk);
  262. clk_put(drv_data->pxa168_usb_clk);
  263. err4:
  264. usb_put_hcd(hcd);
  265. err3:
  266. iounmap(regs);
  267. err2:
  268. release_mem_region(res->start, resource_size(res));
  269. err1:
  270. dev_err(&pdev->dev, "init %s fail, %d\n",
  271. dev_name(&pdev->dev), err);
  272. return err;
  273. }
  274. static int __exit ehci_pxa168_drv_remove(struct platform_device *pdev)
  275. {
  276. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  277. struct pxa168_usb_drv_data *drv_data =
  278. (struct pxa168_usb_drv_data *)hcd->hcd_priv;
  279. usb_remove_hcd(hcd);
  280. /* Power down PHY & PLL */
  281. writel(readl(drv_data->usb_phy_reg_base + USB_PHY_CTRL_REG) & (~0x3),
  282. drv_data->usb_phy_reg_base + USB_PHY_CTRL_REG);
  283. clk_disable(drv_data->pxa168_usb_clk);
  284. clk_put(drv_data->pxa168_usb_clk);
  285. iounmap(hcd->regs);
  286. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  287. iounmap(drv_data->usb_phy_reg_base);
  288. release_mem_region(drv_data->usb_phy_res->start,
  289. resource_size(drv_data->usb_phy_res));
  290. usb_put_hcd(hcd);
  291. return 0;
  292. }
  293. MODULE_ALIAS("platform:pxa168-ehci");
  294. static struct platform_driver ehci_pxa168_driver = {
  295. .probe = ehci_pxa168_drv_probe,
  296. .remove = __exit_p(ehci_pxa168_drv_remove),
  297. .shutdown = usb_hcd_platform_shutdown,
  298. .driver.name = "pxa168-ehci",
  299. };