omap-usb-host.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * omap-usb-host.c - The USBHS core driver for OMAP EHCI & OHCI
  4. *
  5. * Copyright (C) 2011-2013 Texas Instruments Incorporated - https://www.ti.com
  6. * Author: Keshava Munegowda <[email protected]>
  7. * Author: Roger Quadros <[email protected]>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/types.h>
  12. #include <linux/slab.h>
  13. #include <linux/delay.h>
  14. #include <linux/clk.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/gpio.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/platform_data/usb-omap.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/of.h>
  21. #include <linux/of_platform.h>
  22. #include <linux/err.h>
  23. #include "omap-usb.h"
  24. #define USBHS_DRIVER_NAME "usbhs_omap"
  25. #define OMAP_EHCI_DEVICE "ehci-omap"
  26. #define OMAP_OHCI_DEVICE "ohci-omap3"
  27. /* OMAP USBHOST Register addresses */
  28. /* UHH Register Set */
  29. #define OMAP_UHH_REVISION (0x00)
  30. #define OMAP_UHH_SYSCONFIG (0x10)
  31. #define OMAP_UHH_SYSCONFIG_MIDLEMODE (1 << 12)
  32. #define OMAP_UHH_SYSCONFIG_CACTIVITY (1 << 8)
  33. #define OMAP_UHH_SYSCONFIG_SIDLEMODE (1 << 3)
  34. #define OMAP_UHH_SYSCONFIG_ENAWAKEUP (1 << 2)
  35. #define OMAP_UHH_SYSCONFIG_SOFTRESET (1 << 1)
  36. #define OMAP_UHH_SYSCONFIG_AUTOIDLE (1 << 0)
  37. #define OMAP_UHH_SYSSTATUS (0x14)
  38. #define OMAP_UHH_HOSTCONFIG (0x40)
  39. #define OMAP_UHH_HOSTCONFIG_ULPI_BYPASS (1 << 0)
  40. #define OMAP_UHH_HOSTCONFIG_ULPI_P1_BYPASS (1 << 0)
  41. #define OMAP_UHH_HOSTCONFIG_ULPI_P2_BYPASS (1 << 11)
  42. #define OMAP_UHH_HOSTCONFIG_ULPI_P3_BYPASS (1 << 12)
  43. #define OMAP_UHH_HOSTCONFIG_INCR4_BURST_EN (1 << 2)
  44. #define OMAP_UHH_HOSTCONFIG_INCR8_BURST_EN (1 << 3)
  45. #define OMAP_UHH_HOSTCONFIG_INCR16_BURST_EN (1 << 4)
  46. #define OMAP_UHH_HOSTCONFIG_INCRX_ALIGN_EN (1 << 5)
  47. #define OMAP_UHH_HOSTCONFIG_P1_CONNECT_STATUS (1 << 8)
  48. #define OMAP_UHH_HOSTCONFIG_P2_CONNECT_STATUS (1 << 9)
  49. #define OMAP_UHH_HOSTCONFIG_P3_CONNECT_STATUS (1 << 10)
  50. #define OMAP4_UHH_HOSTCONFIG_APP_START_CLK (1 << 31)
  51. /* OMAP4-specific defines */
  52. #define OMAP4_UHH_SYSCONFIG_IDLEMODE_CLEAR (3 << 2)
  53. #define OMAP4_UHH_SYSCONFIG_NOIDLE (1 << 2)
  54. #define OMAP4_UHH_SYSCONFIG_STDBYMODE_CLEAR (3 << 4)
  55. #define OMAP4_UHH_SYSCONFIG_NOSTDBY (1 << 4)
  56. #define OMAP4_UHH_SYSCONFIG_SOFTRESET (1 << 0)
  57. #define OMAP4_P1_MODE_CLEAR (3 << 16)
  58. #define OMAP4_P1_MODE_TLL (1 << 16)
  59. #define OMAP4_P1_MODE_HSIC (3 << 16)
  60. #define OMAP4_P2_MODE_CLEAR (3 << 18)
  61. #define OMAP4_P2_MODE_TLL (1 << 18)
  62. #define OMAP4_P2_MODE_HSIC (3 << 18)
  63. #define OMAP_UHH_DEBUG_CSR (0x44)
  64. /* Values of UHH_REVISION - Note: these are not given in the TRM */
  65. #define OMAP_USBHS_REV1 0x00000010 /* OMAP3 */
  66. #define OMAP_USBHS_REV2 0x50700100 /* OMAP4 */
  67. #define is_omap_usbhs_rev1(x) (x->usbhs_rev == OMAP_USBHS_REV1)
  68. #define is_omap_usbhs_rev2(x) (x->usbhs_rev == OMAP_USBHS_REV2)
  69. #define is_ehci_phy_mode(x) (x == OMAP_EHCI_PORT_MODE_PHY)
  70. #define is_ehci_tll_mode(x) (x == OMAP_EHCI_PORT_MODE_TLL)
  71. #define is_ehci_hsic_mode(x) (x == OMAP_EHCI_PORT_MODE_HSIC)
  72. struct usbhs_hcd_omap {
  73. int nports;
  74. struct clk **utmi_clk;
  75. struct clk **hsic60m_clk;
  76. struct clk **hsic480m_clk;
  77. struct clk *xclk60mhsp1_ck;
  78. struct clk *xclk60mhsp2_ck;
  79. struct clk *utmi_p1_gfclk;
  80. struct clk *utmi_p2_gfclk;
  81. struct clk *init_60m_fclk;
  82. struct clk *ehci_logic_fck;
  83. void __iomem *uhh_base;
  84. struct usbhs_omap_platform_data *pdata;
  85. u32 usbhs_rev;
  86. };
  87. /*-------------------------------------------------------------------------*/
  88. static const char usbhs_driver_name[] = USBHS_DRIVER_NAME;
  89. static u64 usbhs_dmamask = DMA_BIT_MASK(32);
  90. /*-------------------------------------------------------------------------*/
  91. static inline void usbhs_write(void __iomem *base, u32 reg, u32 val)
  92. {
  93. writel_relaxed(val, base + reg);
  94. }
  95. static inline u32 usbhs_read(void __iomem *base, u32 reg)
  96. {
  97. return readl_relaxed(base + reg);
  98. }
  99. /*-------------------------------------------------------------------------*/
  100. /*
  101. * Map 'enum usbhs_omap_port_mode' found in <linux/platform_data/usb-omap.h>
  102. * to the device tree binding portN-mode found in
  103. * 'Documentation/devicetree/bindings/mfd/omap-usb-host.txt'
  104. */
  105. static const char * const port_modes[] = {
  106. [OMAP_USBHS_PORT_MODE_UNUSED] = "",
  107. [OMAP_EHCI_PORT_MODE_PHY] = "ehci-phy",
  108. [OMAP_EHCI_PORT_MODE_TLL] = "ehci-tll",
  109. [OMAP_EHCI_PORT_MODE_HSIC] = "ehci-hsic",
  110. [OMAP_OHCI_PORT_MODE_PHY_6PIN_DATSE0] = "ohci-phy-6pin-datse0",
  111. [OMAP_OHCI_PORT_MODE_PHY_6PIN_DPDM] = "ohci-phy-6pin-dpdm",
  112. [OMAP_OHCI_PORT_MODE_PHY_3PIN_DATSE0] = "ohci-phy-3pin-datse0",
  113. [OMAP_OHCI_PORT_MODE_PHY_4PIN_DPDM] = "ohci-phy-4pin-dpdm",
  114. [OMAP_OHCI_PORT_MODE_TLL_6PIN_DATSE0] = "ohci-tll-6pin-datse0",
  115. [OMAP_OHCI_PORT_MODE_TLL_6PIN_DPDM] = "ohci-tll-6pin-dpdm",
  116. [OMAP_OHCI_PORT_MODE_TLL_3PIN_DATSE0] = "ohci-tll-3pin-datse0",
  117. [OMAP_OHCI_PORT_MODE_TLL_4PIN_DPDM] = "ohci-tll-4pin-dpdm",
  118. [OMAP_OHCI_PORT_MODE_TLL_2PIN_DATSE0] = "ohci-tll-2pin-datse0",
  119. [OMAP_OHCI_PORT_MODE_TLL_2PIN_DPDM] = "ohci-tll-2pin-dpdm",
  120. };
  121. static struct platform_device *omap_usbhs_alloc_child(const char *name,
  122. struct resource *res, int num_resources, void *pdata,
  123. size_t pdata_size, struct device *dev)
  124. {
  125. struct platform_device *child;
  126. int ret;
  127. child = platform_device_alloc(name, 0);
  128. if (!child) {
  129. dev_err(dev, "platform_device_alloc %s failed\n", name);
  130. goto err_end;
  131. }
  132. ret = platform_device_add_resources(child, res, num_resources);
  133. if (ret) {
  134. dev_err(dev, "platform_device_add_resources failed\n");
  135. goto err_alloc;
  136. }
  137. ret = platform_device_add_data(child, pdata, pdata_size);
  138. if (ret) {
  139. dev_err(dev, "platform_device_add_data failed\n");
  140. goto err_alloc;
  141. }
  142. child->dev.dma_mask = &usbhs_dmamask;
  143. dma_set_coherent_mask(&child->dev, DMA_BIT_MASK(32));
  144. child->dev.parent = dev;
  145. ret = platform_device_add(child);
  146. if (ret) {
  147. dev_err(dev, "platform_device_add failed\n");
  148. goto err_alloc;
  149. }
  150. return child;
  151. err_alloc:
  152. platform_device_put(child);
  153. err_end:
  154. return NULL;
  155. }
  156. static int omap_usbhs_alloc_children(struct platform_device *pdev)
  157. {
  158. struct device *dev = &pdev->dev;
  159. struct usbhs_omap_platform_data *pdata = dev_get_platdata(dev);
  160. struct platform_device *ehci;
  161. struct platform_device *ohci;
  162. struct resource *res;
  163. struct resource resources[2];
  164. int ret;
  165. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ehci");
  166. if (!res) {
  167. dev_err(dev, "EHCI get resource IORESOURCE_MEM failed\n");
  168. ret = -ENODEV;
  169. goto err_end;
  170. }
  171. resources[0] = *res;
  172. res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "ehci-irq");
  173. if (!res) {
  174. dev_err(dev, " EHCI get resource IORESOURCE_IRQ failed\n");
  175. ret = -ENODEV;
  176. goto err_end;
  177. }
  178. resources[1] = *res;
  179. ehci = omap_usbhs_alloc_child(OMAP_EHCI_DEVICE, resources, 2, pdata,
  180. sizeof(*pdata), dev);
  181. if (!ehci) {
  182. dev_err(dev, "omap_usbhs_alloc_child failed\n");
  183. ret = -ENOMEM;
  184. goto err_end;
  185. }
  186. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ohci");
  187. if (!res) {
  188. dev_err(dev, "OHCI get resource IORESOURCE_MEM failed\n");
  189. ret = -ENODEV;
  190. goto err_ehci;
  191. }
  192. resources[0] = *res;
  193. res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "ohci-irq");
  194. if (!res) {
  195. dev_err(dev, "OHCI get resource IORESOURCE_IRQ failed\n");
  196. ret = -ENODEV;
  197. goto err_ehci;
  198. }
  199. resources[1] = *res;
  200. ohci = omap_usbhs_alloc_child(OMAP_OHCI_DEVICE, resources, 2, pdata,
  201. sizeof(*pdata), dev);
  202. if (!ohci) {
  203. dev_err(dev, "omap_usbhs_alloc_child failed\n");
  204. ret = -ENOMEM;
  205. goto err_ehci;
  206. }
  207. return 0;
  208. err_ehci:
  209. platform_device_unregister(ehci);
  210. err_end:
  211. return ret;
  212. }
  213. static bool is_ohci_port(enum usbhs_omap_port_mode pmode)
  214. {
  215. switch (pmode) {
  216. case OMAP_OHCI_PORT_MODE_PHY_6PIN_DATSE0:
  217. case OMAP_OHCI_PORT_MODE_PHY_6PIN_DPDM:
  218. case OMAP_OHCI_PORT_MODE_PHY_3PIN_DATSE0:
  219. case OMAP_OHCI_PORT_MODE_PHY_4PIN_DPDM:
  220. case OMAP_OHCI_PORT_MODE_TLL_6PIN_DATSE0:
  221. case OMAP_OHCI_PORT_MODE_TLL_6PIN_DPDM:
  222. case OMAP_OHCI_PORT_MODE_TLL_3PIN_DATSE0:
  223. case OMAP_OHCI_PORT_MODE_TLL_4PIN_DPDM:
  224. case OMAP_OHCI_PORT_MODE_TLL_2PIN_DATSE0:
  225. case OMAP_OHCI_PORT_MODE_TLL_2PIN_DPDM:
  226. return true;
  227. default:
  228. return false;
  229. }
  230. }
  231. static int usbhs_runtime_resume(struct device *dev)
  232. {
  233. struct usbhs_hcd_omap *omap = dev_get_drvdata(dev);
  234. struct usbhs_omap_platform_data *pdata = omap->pdata;
  235. int i, r;
  236. dev_dbg(dev, "usbhs_runtime_resume\n");
  237. omap_tll_enable(pdata);
  238. if (!IS_ERR(omap->ehci_logic_fck))
  239. clk_prepare_enable(omap->ehci_logic_fck);
  240. for (i = 0; i < omap->nports; i++) {
  241. switch (pdata->port_mode[i]) {
  242. case OMAP_EHCI_PORT_MODE_HSIC:
  243. if (!IS_ERR(omap->hsic60m_clk[i])) {
  244. r = clk_prepare_enable(omap->hsic60m_clk[i]);
  245. if (r) {
  246. dev_err(dev,
  247. "Can't enable port %d hsic60m clk:%d\n",
  248. i, r);
  249. }
  250. }
  251. if (!IS_ERR(omap->hsic480m_clk[i])) {
  252. r = clk_prepare_enable(omap->hsic480m_clk[i]);
  253. if (r) {
  254. dev_err(dev,
  255. "Can't enable port %d hsic480m clk:%d\n",
  256. i, r);
  257. }
  258. }
  259. fallthrough; /* as HSIC mode needs utmi_clk */
  260. case OMAP_EHCI_PORT_MODE_TLL:
  261. if (!IS_ERR(omap->utmi_clk[i])) {
  262. r = clk_prepare_enable(omap->utmi_clk[i]);
  263. if (r) {
  264. dev_err(dev,
  265. "Can't enable port %d clk : %d\n",
  266. i, r);
  267. }
  268. }
  269. break;
  270. default:
  271. break;
  272. }
  273. }
  274. return 0;
  275. }
  276. static int usbhs_runtime_suspend(struct device *dev)
  277. {
  278. struct usbhs_hcd_omap *omap = dev_get_drvdata(dev);
  279. struct usbhs_omap_platform_data *pdata = omap->pdata;
  280. int i;
  281. dev_dbg(dev, "usbhs_runtime_suspend\n");
  282. for (i = 0; i < omap->nports; i++) {
  283. switch (pdata->port_mode[i]) {
  284. case OMAP_EHCI_PORT_MODE_HSIC:
  285. if (!IS_ERR(omap->hsic60m_clk[i]))
  286. clk_disable_unprepare(omap->hsic60m_clk[i]);
  287. if (!IS_ERR(omap->hsic480m_clk[i]))
  288. clk_disable_unprepare(omap->hsic480m_clk[i]);
  289. fallthrough; /* as utmi_clks were used in HSIC mode */
  290. case OMAP_EHCI_PORT_MODE_TLL:
  291. if (!IS_ERR(omap->utmi_clk[i]))
  292. clk_disable_unprepare(omap->utmi_clk[i]);
  293. break;
  294. default:
  295. break;
  296. }
  297. }
  298. if (!IS_ERR(omap->ehci_logic_fck))
  299. clk_disable_unprepare(omap->ehci_logic_fck);
  300. omap_tll_disable(pdata);
  301. return 0;
  302. }
  303. static unsigned omap_usbhs_rev1_hostconfig(struct usbhs_hcd_omap *omap,
  304. unsigned reg)
  305. {
  306. struct usbhs_omap_platform_data *pdata = omap->pdata;
  307. int i;
  308. for (i = 0; i < omap->nports; i++) {
  309. switch (pdata->port_mode[i]) {
  310. case OMAP_USBHS_PORT_MODE_UNUSED:
  311. reg &= ~(OMAP_UHH_HOSTCONFIG_P1_CONNECT_STATUS << i);
  312. break;
  313. case OMAP_EHCI_PORT_MODE_PHY:
  314. if (pdata->single_ulpi_bypass)
  315. break;
  316. if (i == 0)
  317. reg &= ~OMAP_UHH_HOSTCONFIG_ULPI_P1_BYPASS;
  318. else
  319. reg &= ~(OMAP_UHH_HOSTCONFIG_ULPI_P2_BYPASS
  320. << (i-1));
  321. break;
  322. default:
  323. if (pdata->single_ulpi_bypass)
  324. break;
  325. if (i == 0)
  326. reg |= OMAP_UHH_HOSTCONFIG_ULPI_P1_BYPASS;
  327. else
  328. reg |= OMAP_UHH_HOSTCONFIG_ULPI_P2_BYPASS
  329. << (i-1);
  330. break;
  331. }
  332. }
  333. if (pdata->single_ulpi_bypass) {
  334. /* bypass ULPI only if none of the ports use PHY mode */
  335. reg |= OMAP_UHH_HOSTCONFIG_ULPI_BYPASS;
  336. for (i = 0; i < omap->nports; i++) {
  337. if (is_ehci_phy_mode(pdata->port_mode[i])) {
  338. reg &= ~OMAP_UHH_HOSTCONFIG_ULPI_BYPASS;
  339. break;
  340. }
  341. }
  342. }
  343. return reg;
  344. }
  345. static unsigned omap_usbhs_rev2_hostconfig(struct usbhs_hcd_omap *omap,
  346. unsigned reg)
  347. {
  348. struct usbhs_omap_platform_data *pdata = omap->pdata;
  349. int i;
  350. for (i = 0; i < omap->nports; i++) {
  351. /* Clear port mode fields for PHY mode */
  352. reg &= ~(OMAP4_P1_MODE_CLEAR << 2 * i);
  353. if (is_ehci_tll_mode(pdata->port_mode[i]) ||
  354. (is_ohci_port(pdata->port_mode[i])))
  355. reg |= OMAP4_P1_MODE_TLL << 2 * i;
  356. else if (is_ehci_hsic_mode(pdata->port_mode[i]))
  357. reg |= OMAP4_P1_MODE_HSIC << 2 * i;
  358. }
  359. return reg;
  360. }
  361. static void omap_usbhs_init(struct device *dev)
  362. {
  363. struct usbhs_hcd_omap *omap = dev_get_drvdata(dev);
  364. unsigned reg;
  365. dev_dbg(dev, "starting TI HSUSB Controller\n");
  366. pm_runtime_get_sync(dev);
  367. reg = usbhs_read(omap->uhh_base, OMAP_UHH_HOSTCONFIG);
  368. /* setup ULPI bypass and burst configurations */
  369. reg |= (OMAP_UHH_HOSTCONFIG_INCR4_BURST_EN
  370. | OMAP_UHH_HOSTCONFIG_INCR8_BURST_EN
  371. | OMAP_UHH_HOSTCONFIG_INCR16_BURST_EN);
  372. reg |= OMAP4_UHH_HOSTCONFIG_APP_START_CLK;
  373. reg &= ~OMAP_UHH_HOSTCONFIG_INCRX_ALIGN_EN;
  374. switch (omap->usbhs_rev) {
  375. case OMAP_USBHS_REV1:
  376. reg = omap_usbhs_rev1_hostconfig(omap, reg);
  377. break;
  378. case OMAP_USBHS_REV2:
  379. reg = omap_usbhs_rev2_hostconfig(omap, reg);
  380. break;
  381. default: /* newer revisions */
  382. reg = omap_usbhs_rev2_hostconfig(omap, reg);
  383. break;
  384. }
  385. usbhs_write(omap->uhh_base, OMAP_UHH_HOSTCONFIG, reg);
  386. dev_dbg(dev, "UHH setup done, uhh_hostconfig=%x\n", reg);
  387. pm_runtime_put_sync(dev);
  388. }
  389. static int usbhs_omap_get_dt_pdata(struct device *dev,
  390. struct usbhs_omap_platform_data *pdata)
  391. {
  392. int ret, i;
  393. struct device_node *node = dev->of_node;
  394. ret = of_property_read_u32(node, "num-ports", &pdata->nports);
  395. if (ret)
  396. pdata->nports = 0;
  397. if (pdata->nports > OMAP3_HS_USB_PORTS) {
  398. dev_warn(dev, "Too many num_ports <%d> in device tree. Max %d\n",
  399. pdata->nports, OMAP3_HS_USB_PORTS);
  400. return -ENODEV;
  401. }
  402. /* get port modes */
  403. for (i = 0; i < OMAP3_HS_USB_PORTS; i++) {
  404. char prop[11];
  405. const char *mode;
  406. pdata->port_mode[i] = OMAP_USBHS_PORT_MODE_UNUSED;
  407. snprintf(prop, sizeof(prop), "port%d-mode", i + 1);
  408. ret = of_property_read_string(node, prop, &mode);
  409. if (ret < 0)
  410. continue;
  411. /* get 'enum usbhs_omap_port_mode' from port mode string */
  412. ret = match_string(port_modes, ARRAY_SIZE(port_modes), mode);
  413. if (ret < 0) {
  414. dev_warn(dev, "Invalid port%d-mode \"%s\" in device tree\n",
  415. i, mode);
  416. return -ENODEV;
  417. }
  418. dev_dbg(dev, "port%d-mode: %s -> %d\n", i, mode, ret);
  419. pdata->port_mode[i] = ret;
  420. }
  421. /* get flags */
  422. pdata->single_ulpi_bypass = of_property_read_bool(node,
  423. "single-ulpi-bypass");
  424. return 0;
  425. }
  426. static const struct of_device_id usbhs_child_match_table[] = {
  427. { .compatible = "ti,ehci-omap", },
  428. { .compatible = "ti,ohci-omap3", },
  429. { }
  430. };
  431. /**
  432. * usbhs_omap_probe - initialize TI-based HCDs
  433. *
  434. * Allocates basic resources for this USB host controller.
  435. *
  436. * @pdev: Pointer to this device's platform device structure
  437. */
  438. static int usbhs_omap_probe(struct platform_device *pdev)
  439. {
  440. struct device *dev = &pdev->dev;
  441. struct usbhs_omap_platform_data *pdata = dev_get_platdata(dev);
  442. struct usbhs_hcd_omap *omap;
  443. struct resource *res;
  444. int ret = 0;
  445. int i;
  446. bool need_logic_fck;
  447. if (dev->of_node) {
  448. /* For DT boot we populate platform data from OF node */
  449. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  450. if (!pdata)
  451. return -ENOMEM;
  452. ret = usbhs_omap_get_dt_pdata(dev, pdata);
  453. if (ret)
  454. return ret;
  455. dev->platform_data = pdata;
  456. }
  457. if (!pdata) {
  458. dev_err(dev, "Missing platform data\n");
  459. return -ENODEV;
  460. }
  461. if (pdata->nports > OMAP3_HS_USB_PORTS) {
  462. dev_info(dev, "Too many num_ports <%d> in platform_data. Max %d\n",
  463. pdata->nports, OMAP3_HS_USB_PORTS);
  464. return -ENODEV;
  465. }
  466. omap = devm_kzalloc(dev, sizeof(*omap), GFP_KERNEL);
  467. if (!omap) {
  468. dev_err(dev, "Memory allocation failed\n");
  469. return -ENOMEM;
  470. }
  471. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  472. omap->uhh_base = devm_ioremap_resource(dev, res);
  473. if (IS_ERR(omap->uhh_base))
  474. return PTR_ERR(omap->uhh_base);
  475. omap->pdata = pdata;
  476. /* Initialize the TLL subsystem */
  477. omap_tll_init(pdata);
  478. pm_runtime_enable(dev);
  479. platform_set_drvdata(pdev, omap);
  480. pm_runtime_get_sync(dev);
  481. omap->usbhs_rev = usbhs_read(omap->uhh_base, OMAP_UHH_REVISION);
  482. /* we need to call runtime suspend before we update omap->nports
  483. * to prevent unbalanced clk_disable()
  484. */
  485. pm_runtime_put_sync(dev);
  486. /*
  487. * If platform data contains nports then use that
  488. * else make out number of ports from USBHS revision
  489. */
  490. if (pdata->nports) {
  491. omap->nports = pdata->nports;
  492. } else {
  493. switch (omap->usbhs_rev) {
  494. case OMAP_USBHS_REV1:
  495. omap->nports = 3;
  496. break;
  497. case OMAP_USBHS_REV2:
  498. omap->nports = 2;
  499. break;
  500. default:
  501. omap->nports = OMAP3_HS_USB_PORTS;
  502. dev_dbg(dev,
  503. "USB HOST Rev:0x%x not recognized, assuming %d ports\n",
  504. omap->usbhs_rev, omap->nports);
  505. break;
  506. }
  507. pdata->nports = omap->nports;
  508. }
  509. i = sizeof(struct clk *) * omap->nports;
  510. omap->utmi_clk = devm_kzalloc(dev, i, GFP_KERNEL);
  511. omap->hsic480m_clk = devm_kzalloc(dev, i, GFP_KERNEL);
  512. omap->hsic60m_clk = devm_kzalloc(dev, i, GFP_KERNEL);
  513. if (!omap->utmi_clk || !omap->hsic480m_clk || !omap->hsic60m_clk) {
  514. dev_err(dev, "Memory allocation failed\n");
  515. ret = -ENOMEM;
  516. goto err_mem;
  517. }
  518. /* Set all clocks as invalid to begin with */
  519. omap->ehci_logic_fck = ERR_PTR(-ENODEV);
  520. omap->init_60m_fclk = ERR_PTR(-ENODEV);
  521. omap->utmi_p1_gfclk = ERR_PTR(-ENODEV);
  522. omap->utmi_p2_gfclk = ERR_PTR(-ENODEV);
  523. omap->xclk60mhsp1_ck = ERR_PTR(-ENODEV);
  524. omap->xclk60mhsp2_ck = ERR_PTR(-ENODEV);
  525. for (i = 0; i < omap->nports; i++) {
  526. omap->utmi_clk[i] = ERR_PTR(-ENODEV);
  527. omap->hsic480m_clk[i] = ERR_PTR(-ENODEV);
  528. omap->hsic60m_clk[i] = ERR_PTR(-ENODEV);
  529. }
  530. /* for OMAP3 i.e. USBHS REV1 */
  531. if (omap->usbhs_rev == OMAP_USBHS_REV1) {
  532. need_logic_fck = false;
  533. for (i = 0; i < omap->nports; i++) {
  534. if (is_ehci_phy_mode(pdata->port_mode[i]) ||
  535. is_ehci_tll_mode(pdata->port_mode[i]) ||
  536. is_ehci_hsic_mode(pdata->port_mode[i]))
  537. need_logic_fck |= true;
  538. }
  539. if (need_logic_fck) {
  540. omap->ehci_logic_fck = devm_clk_get(dev,
  541. "usbhost_120m_fck");
  542. if (IS_ERR(omap->ehci_logic_fck)) {
  543. ret = PTR_ERR(omap->ehci_logic_fck);
  544. dev_err(dev, "usbhost_120m_fck failed:%d\n",
  545. ret);
  546. goto err_mem;
  547. }
  548. }
  549. goto initialize;
  550. }
  551. /* for OMAP4+ i.e. USBHS REV2+ */
  552. omap->utmi_p1_gfclk = devm_clk_get(dev, "utmi_p1_gfclk");
  553. if (IS_ERR(omap->utmi_p1_gfclk)) {
  554. ret = PTR_ERR(omap->utmi_p1_gfclk);
  555. dev_err(dev, "utmi_p1_gfclk failed error:%d\n", ret);
  556. goto err_mem;
  557. }
  558. omap->utmi_p2_gfclk = devm_clk_get(dev, "utmi_p2_gfclk");
  559. if (IS_ERR(omap->utmi_p2_gfclk)) {
  560. ret = PTR_ERR(omap->utmi_p2_gfclk);
  561. dev_err(dev, "utmi_p2_gfclk failed error:%d\n", ret);
  562. goto err_mem;
  563. }
  564. omap->xclk60mhsp1_ck = devm_clk_get(dev, "refclk_60m_ext_p1");
  565. if (IS_ERR(omap->xclk60mhsp1_ck)) {
  566. ret = PTR_ERR(omap->xclk60mhsp1_ck);
  567. dev_err(dev, "refclk_60m_ext_p1 failed error:%d\n", ret);
  568. goto err_mem;
  569. }
  570. omap->xclk60mhsp2_ck = devm_clk_get(dev, "refclk_60m_ext_p2");
  571. if (IS_ERR(omap->xclk60mhsp2_ck)) {
  572. ret = PTR_ERR(omap->xclk60mhsp2_ck);
  573. dev_err(dev, "refclk_60m_ext_p2 failed error:%d\n", ret);
  574. goto err_mem;
  575. }
  576. omap->init_60m_fclk = devm_clk_get(dev, "refclk_60m_int");
  577. if (IS_ERR(omap->init_60m_fclk)) {
  578. ret = PTR_ERR(omap->init_60m_fclk);
  579. dev_err(dev, "refclk_60m_int failed error:%d\n", ret);
  580. goto err_mem;
  581. }
  582. for (i = 0; i < omap->nports; i++) {
  583. char clkname[30];
  584. /* clock names are indexed from 1*/
  585. snprintf(clkname, sizeof(clkname),
  586. "usb_host_hs_utmi_p%d_clk", i + 1);
  587. /* If a clock is not found we won't bail out as not all
  588. * platforms have all clocks and we can function without
  589. * them
  590. */
  591. omap->utmi_clk[i] = devm_clk_get(dev, clkname);
  592. if (IS_ERR(omap->utmi_clk[i])) {
  593. ret = PTR_ERR(omap->utmi_clk[i]);
  594. dev_err(dev, "Failed to get clock : %s : %d\n",
  595. clkname, ret);
  596. goto err_mem;
  597. }
  598. snprintf(clkname, sizeof(clkname),
  599. "usb_host_hs_hsic480m_p%d_clk", i + 1);
  600. omap->hsic480m_clk[i] = devm_clk_get(dev, clkname);
  601. if (IS_ERR(omap->hsic480m_clk[i])) {
  602. ret = PTR_ERR(omap->hsic480m_clk[i]);
  603. dev_err(dev, "Failed to get clock : %s : %d\n",
  604. clkname, ret);
  605. goto err_mem;
  606. }
  607. snprintf(clkname, sizeof(clkname),
  608. "usb_host_hs_hsic60m_p%d_clk", i + 1);
  609. omap->hsic60m_clk[i] = devm_clk_get(dev, clkname);
  610. if (IS_ERR(omap->hsic60m_clk[i])) {
  611. ret = PTR_ERR(omap->hsic60m_clk[i]);
  612. dev_err(dev, "Failed to get clock : %s : %d\n",
  613. clkname, ret);
  614. goto err_mem;
  615. }
  616. }
  617. if (is_ehci_phy_mode(pdata->port_mode[0])) {
  618. ret = clk_set_parent(omap->utmi_p1_gfclk,
  619. omap->xclk60mhsp1_ck);
  620. if (ret != 0) {
  621. dev_err(dev, "xclk60mhsp1_ck set parent failed: %d\n",
  622. ret);
  623. goto err_mem;
  624. }
  625. } else if (is_ehci_tll_mode(pdata->port_mode[0])) {
  626. ret = clk_set_parent(omap->utmi_p1_gfclk,
  627. omap->init_60m_fclk);
  628. if (ret != 0) {
  629. dev_err(dev, "P0 init_60m_fclk set parent failed: %d\n",
  630. ret);
  631. goto err_mem;
  632. }
  633. }
  634. if (is_ehci_phy_mode(pdata->port_mode[1])) {
  635. ret = clk_set_parent(omap->utmi_p2_gfclk,
  636. omap->xclk60mhsp2_ck);
  637. if (ret != 0) {
  638. dev_err(dev, "xclk60mhsp2_ck set parent failed: %d\n",
  639. ret);
  640. goto err_mem;
  641. }
  642. } else if (is_ehci_tll_mode(pdata->port_mode[1])) {
  643. ret = clk_set_parent(omap->utmi_p2_gfclk,
  644. omap->init_60m_fclk);
  645. if (ret != 0) {
  646. dev_err(dev, "P1 init_60m_fclk set parent failed: %d\n",
  647. ret);
  648. goto err_mem;
  649. }
  650. }
  651. initialize:
  652. omap_usbhs_init(dev);
  653. if (dev->of_node) {
  654. ret = of_platform_populate(dev->of_node,
  655. usbhs_child_match_table, NULL, dev);
  656. if (ret) {
  657. dev_err(dev, "Failed to create DT children: %d\n", ret);
  658. goto err_mem;
  659. }
  660. } else {
  661. ret = omap_usbhs_alloc_children(pdev);
  662. if (ret) {
  663. dev_err(dev, "omap_usbhs_alloc_children failed: %d\n",
  664. ret);
  665. goto err_mem;
  666. }
  667. }
  668. return 0;
  669. err_mem:
  670. pm_runtime_disable(dev);
  671. return ret;
  672. }
  673. static int usbhs_omap_remove_child(struct device *dev, void *data)
  674. {
  675. dev_info(dev, "unregistering\n");
  676. platform_device_unregister(to_platform_device(dev));
  677. return 0;
  678. }
  679. /**
  680. * usbhs_omap_remove - shutdown processing for UHH & TLL HCDs
  681. * @pdev: USB Host Controller being removed
  682. *
  683. * Reverses the effect of usbhs_omap_probe().
  684. */
  685. static int usbhs_omap_remove(struct platform_device *pdev)
  686. {
  687. pm_runtime_disable(&pdev->dev);
  688. /* remove children */
  689. device_for_each_child(&pdev->dev, NULL, usbhs_omap_remove_child);
  690. return 0;
  691. }
  692. static const struct dev_pm_ops usbhsomap_dev_pm_ops = {
  693. .runtime_suspend = usbhs_runtime_suspend,
  694. .runtime_resume = usbhs_runtime_resume,
  695. };
  696. static const struct of_device_id usbhs_omap_dt_ids[] = {
  697. { .compatible = "ti,usbhs-host" },
  698. { }
  699. };
  700. MODULE_DEVICE_TABLE(of, usbhs_omap_dt_ids);
  701. static struct platform_driver usbhs_omap_driver = {
  702. .driver = {
  703. .name = usbhs_driver_name,
  704. .pm = &usbhsomap_dev_pm_ops,
  705. .of_match_table = usbhs_omap_dt_ids,
  706. },
  707. .probe = usbhs_omap_probe,
  708. .remove = usbhs_omap_remove,
  709. };
  710. MODULE_AUTHOR("Keshava Munegowda <[email protected]>");
  711. MODULE_AUTHOR("Roger Quadros <[email protected]>");
  712. MODULE_ALIAS("platform:" USBHS_DRIVER_NAME);
  713. MODULE_LICENSE("GPL v2");
  714. MODULE_DESCRIPTION("usb host common core driver for omap EHCI and OHCI");
  715. static int omap_usbhs_drvinit(void)
  716. {
  717. return platform_driver_register(&usbhs_omap_driver);
  718. }
  719. /*
  720. * init before ehci and ohci drivers;
  721. * The usbhs core driver should be initialized much before
  722. * the omap ehci and ohci probe functions are called.
  723. * This usbhs core driver should be initialized after
  724. * usb tll driver
  725. */
  726. fs_initcall_sync(omap_usbhs_drvinit);
  727. static void omap_usbhs_drvexit(void)
  728. {
  729. platform_driver_unregister(&usbhs_omap_driver);
  730. }
  731. module_exit(omap_usbhs_drvexit);