omap-usb-host.c 23 KB

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