omap-usb-host.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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. /**
  133. * omap_usbhs_get_dt_port_mode - Get the 'enum usbhs_omap_port_mode'
  134. * from the port mode string.
  135. * @mode: The port mode string, usually obtained from device tree.
  136. *
  137. * The function returns the 'enum usbhs_omap_port_mode' that matches the
  138. * provided port mode string as per the port_modes table.
  139. * If no match is found it returns -ENODEV
  140. */
  141. static const int omap_usbhs_get_dt_port_mode(const char *mode)
  142. {
  143. int i;
  144. for (i = 0; i < ARRAY_SIZE(port_modes); i++) {
  145. if (!strcmp(mode, port_modes[i]))
  146. return i;
  147. }
  148. return -ENODEV;
  149. }
  150. static struct platform_device *omap_usbhs_alloc_child(const char *name,
  151. struct resource *res, int num_resources, void *pdata,
  152. size_t pdata_size, struct device *dev)
  153. {
  154. struct platform_device *child;
  155. int ret;
  156. child = platform_device_alloc(name, 0);
  157. if (!child) {
  158. dev_err(dev, "platform_device_alloc %s failed\n", name);
  159. goto err_end;
  160. }
  161. ret = platform_device_add_resources(child, res, num_resources);
  162. if (ret) {
  163. dev_err(dev, "platform_device_add_resources failed\n");
  164. goto err_alloc;
  165. }
  166. ret = platform_device_add_data(child, pdata, pdata_size);
  167. if (ret) {
  168. dev_err(dev, "platform_device_add_data failed\n");
  169. goto err_alloc;
  170. }
  171. child->dev.dma_mask = &usbhs_dmamask;
  172. dma_set_coherent_mask(&child->dev, DMA_BIT_MASK(32));
  173. child->dev.parent = dev;
  174. ret = platform_device_add(child);
  175. if (ret) {
  176. dev_err(dev, "platform_device_add failed\n");
  177. goto err_alloc;
  178. }
  179. return child;
  180. err_alloc:
  181. platform_device_put(child);
  182. err_end:
  183. return NULL;
  184. }
  185. static int omap_usbhs_alloc_children(struct platform_device *pdev)
  186. {
  187. struct device *dev = &pdev->dev;
  188. struct usbhs_omap_platform_data *pdata = dev_get_platdata(dev);
  189. struct platform_device *ehci;
  190. struct platform_device *ohci;
  191. struct resource *res;
  192. struct resource resources[2];
  193. int ret;
  194. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ehci");
  195. if (!res) {
  196. dev_err(dev, "EHCI get resource IORESOURCE_MEM failed\n");
  197. ret = -ENODEV;
  198. goto err_end;
  199. }
  200. resources[0] = *res;
  201. res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "ehci-irq");
  202. if (!res) {
  203. dev_err(dev, " EHCI get resource IORESOURCE_IRQ failed\n");
  204. ret = -ENODEV;
  205. goto err_end;
  206. }
  207. resources[1] = *res;
  208. ehci = omap_usbhs_alloc_child(OMAP_EHCI_DEVICE, resources, 2, pdata,
  209. sizeof(*pdata), dev);
  210. if (!ehci) {
  211. dev_err(dev, "omap_usbhs_alloc_child failed\n");
  212. ret = -ENOMEM;
  213. goto err_end;
  214. }
  215. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ohci");
  216. if (!res) {
  217. dev_err(dev, "OHCI get resource IORESOURCE_MEM failed\n");
  218. ret = -ENODEV;
  219. goto err_ehci;
  220. }
  221. resources[0] = *res;
  222. res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "ohci-irq");
  223. if (!res) {
  224. dev_err(dev, "OHCI get resource IORESOURCE_IRQ failed\n");
  225. ret = -ENODEV;
  226. goto err_ehci;
  227. }
  228. resources[1] = *res;
  229. ohci = omap_usbhs_alloc_child(OMAP_OHCI_DEVICE, resources, 2, pdata,
  230. sizeof(*pdata), dev);
  231. if (!ohci) {
  232. dev_err(dev, "omap_usbhs_alloc_child failed\n");
  233. ret = -ENOMEM;
  234. goto err_ehci;
  235. }
  236. return 0;
  237. err_ehci:
  238. platform_device_unregister(ehci);
  239. err_end:
  240. return ret;
  241. }
  242. static bool is_ohci_port(enum usbhs_omap_port_mode pmode)
  243. {
  244. switch (pmode) {
  245. case OMAP_OHCI_PORT_MODE_PHY_6PIN_DATSE0:
  246. case OMAP_OHCI_PORT_MODE_PHY_6PIN_DPDM:
  247. case OMAP_OHCI_PORT_MODE_PHY_3PIN_DATSE0:
  248. case OMAP_OHCI_PORT_MODE_PHY_4PIN_DPDM:
  249. case OMAP_OHCI_PORT_MODE_TLL_6PIN_DATSE0:
  250. case OMAP_OHCI_PORT_MODE_TLL_6PIN_DPDM:
  251. case OMAP_OHCI_PORT_MODE_TLL_3PIN_DATSE0:
  252. case OMAP_OHCI_PORT_MODE_TLL_4PIN_DPDM:
  253. case OMAP_OHCI_PORT_MODE_TLL_2PIN_DATSE0:
  254. case OMAP_OHCI_PORT_MODE_TLL_2PIN_DPDM:
  255. return true;
  256. default:
  257. return false;
  258. }
  259. }
  260. static int usbhs_runtime_resume(struct device *dev)
  261. {
  262. struct usbhs_hcd_omap *omap = dev_get_drvdata(dev);
  263. struct usbhs_omap_platform_data *pdata = omap->pdata;
  264. int i, r;
  265. dev_dbg(dev, "usbhs_runtime_resume\n");
  266. omap_tll_enable(pdata);
  267. if (!IS_ERR(omap->ehci_logic_fck))
  268. clk_prepare_enable(omap->ehci_logic_fck);
  269. for (i = 0; i < omap->nports; i++) {
  270. switch (pdata->port_mode[i]) {
  271. case OMAP_EHCI_PORT_MODE_HSIC:
  272. if (!IS_ERR(omap->hsic60m_clk[i])) {
  273. r = clk_prepare_enable(omap->hsic60m_clk[i]);
  274. if (r) {
  275. dev_err(dev,
  276. "Can't enable port %d hsic60m clk:%d\n",
  277. i, r);
  278. }
  279. }
  280. if (!IS_ERR(omap->hsic480m_clk[i])) {
  281. r = clk_prepare_enable(omap->hsic480m_clk[i]);
  282. if (r) {
  283. dev_err(dev,
  284. "Can't enable port %d hsic480m clk:%d\n",
  285. i, r);
  286. }
  287. }
  288. /* Fall through as HSIC mode needs utmi_clk */
  289. case OMAP_EHCI_PORT_MODE_TLL:
  290. if (!IS_ERR(omap->utmi_clk[i])) {
  291. r = clk_prepare_enable(omap->utmi_clk[i]);
  292. if (r) {
  293. dev_err(dev,
  294. "Can't enable port %d clk : %d\n",
  295. i, r);
  296. }
  297. }
  298. break;
  299. default:
  300. break;
  301. }
  302. }
  303. return 0;
  304. }
  305. static int usbhs_runtime_suspend(struct device *dev)
  306. {
  307. struct usbhs_hcd_omap *omap = dev_get_drvdata(dev);
  308. struct usbhs_omap_platform_data *pdata = omap->pdata;
  309. int i;
  310. dev_dbg(dev, "usbhs_runtime_suspend\n");
  311. for (i = 0; i < omap->nports; i++) {
  312. switch (pdata->port_mode[i]) {
  313. case OMAP_EHCI_PORT_MODE_HSIC:
  314. if (!IS_ERR(omap->hsic60m_clk[i]))
  315. clk_disable_unprepare(omap->hsic60m_clk[i]);
  316. if (!IS_ERR(omap->hsic480m_clk[i]))
  317. clk_disable_unprepare(omap->hsic480m_clk[i]);
  318. /* Fall through as utmi_clks were used in HSIC mode */
  319. case OMAP_EHCI_PORT_MODE_TLL:
  320. if (!IS_ERR(omap->utmi_clk[i]))
  321. clk_disable_unprepare(omap->utmi_clk[i]);
  322. break;
  323. default:
  324. break;
  325. }
  326. }
  327. if (!IS_ERR(omap->ehci_logic_fck))
  328. clk_disable_unprepare(omap->ehci_logic_fck);
  329. omap_tll_disable(pdata);
  330. return 0;
  331. }
  332. static unsigned omap_usbhs_rev1_hostconfig(struct usbhs_hcd_omap *omap,
  333. unsigned reg)
  334. {
  335. struct usbhs_omap_platform_data *pdata = omap->pdata;
  336. int i;
  337. for (i = 0; i < omap->nports; i++) {
  338. switch (pdata->port_mode[i]) {
  339. case OMAP_USBHS_PORT_MODE_UNUSED:
  340. reg &= ~(OMAP_UHH_HOSTCONFIG_P1_CONNECT_STATUS << i);
  341. break;
  342. case OMAP_EHCI_PORT_MODE_PHY:
  343. if (pdata->single_ulpi_bypass)
  344. break;
  345. if (i == 0)
  346. reg &= ~OMAP_UHH_HOSTCONFIG_ULPI_P1_BYPASS;
  347. else
  348. reg &= ~(OMAP_UHH_HOSTCONFIG_ULPI_P2_BYPASS
  349. << (i-1));
  350. break;
  351. default:
  352. if (pdata->single_ulpi_bypass)
  353. break;
  354. if (i == 0)
  355. reg |= OMAP_UHH_HOSTCONFIG_ULPI_P1_BYPASS;
  356. else
  357. reg |= OMAP_UHH_HOSTCONFIG_ULPI_P2_BYPASS
  358. << (i-1);
  359. break;
  360. }
  361. }
  362. if (pdata->single_ulpi_bypass) {
  363. /* bypass ULPI only if none of the ports use PHY mode */
  364. reg |= OMAP_UHH_HOSTCONFIG_ULPI_BYPASS;
  365. for (i = 0; i < omap->nports; i++) {
  366. if (is_ehci_phy_mode(pdata->port_mode[i])) {
  367. reg &= ~OMAP_UHH_HOSTCONFIG_ULPI_BYPASS;
  368. break;
  369. }
  370. }
  371. }
  372. return reg;
  373. }
  374. static unsigned omap_usbhs_rev2_hostconfig(struct usbhs_hcd_omap *omap,
  375. unsigned reg)
  376. {
  377. struct usbhs_omap_platform_data *pdata = omap->pdata;
  378. int i;
  379. for (i = 0; i < omap->nports; i++) {
  380. /* Clear port mode fields for PHY mode */
  381. reg &= ~(OMAP4_P1_MODE_CLEAR << 2 * i);
  382. if (is_ehci_tll_mode(pdata->port_mode[i]) ||
  383. (is_ohci_port(pdata->port_mode[i])))
  384. reg |= OMAP4_P1_MODE_TLL << 2 * i;
  385. else if (is_ehci_hsic_mode(pdata->port_mode[i]))
  386. reg |= OMAP4_P1_MODE_HSIC << 2 * i;
  387. }
  388. return reg;
  389. }
  390. static void omap_usbhs_init(struct device *dev)
  391. {
  392. struct usbhs_hcd_omap *omap = dev_get_drvdata(dev);
  393. unsigned reg;
  394. dev_dbg(dev, "starting TI HSUSB Controller\n");
  395. pm_runtime_get_sync(dev);
  396. reg = usbhs_read(omap->uhh_base, OMAP_UHH_HOSTCONFIG);
  397. /* setup ULPI bypass and burst configurations */
  398. reg |= (OMAP_UHH_HOSTCONFIG_INCR4_BURST_EN
  399. | OMAP_UHH_HOSTCONFIG_INCR8_BURST_EN
  400. | OMAP_UHH_HOSTCONFIG_INCR16_BURST_EN);
  401. reg |= OMAP4_UHH_HOSTCONFIG_APP_START_CLK;
  402. reg &= ~OMAP_UHH_HOSTCONFIG_INCRX_ALIGN_EN;
  403. switch (omap->usbhs_rev) {
  404. case OMAP_USBHS_REV1:
  405. reg = omap_usbhs_rev1_hostconfig(omap, reg);
  406. break;
  407. case OMAP_USBHS_REV2:
  408. reg = omap_usbhs_rev2_hostconfig(omap, reg);
  409. break;
  410. default: /* newer revisions */
  411. reg = omap_usbhs_rev2_hostconfig(omap, reg);
  412. break;
  413. }
  414. usbhs_write(omap->uhh_base, OMAP_UHH_HOSTCONFIG, reg);
  415. dev_dbg(dev, "UHH setup done, uhh_hostconfig=%x\n", reg);
  416. pm_runtime_put_sync(dev);
  417. }
  418. static int usbhs_omap_get_dt_pdata(struct device *dev,
  419. struct usbhs_omap_platform_data *pdata)
  420. {
  421. int ret, i;
  422. struct device_node *node = dev->of_node;
  423. ret = of_property_read_u32(node, "num-ports", &pdata->nports);
  424. if (ret)
  425. pdata->nports = 0;
  426. if (pdata->nports > OMAP3_HS_USB_PORTS) {
  427. dev_warn(dev, "Too many num_ports <%d> in device tree. Max %d\n",
  428. pdata->nports, OMAP3_HS_USB_PORTS);
  429. return -ENODEV;
  430. }
  431. /* get port modes */
  432. for (i = 0; i < OMAP3_HS_USB_PORTS; i++) {
  433. char prop[11];
  434. const char *mode;
  435. pdata->port_mode[i] = OMAP_USBHS_PORT_MODE_UNUSED;
  436. snprintf(prop, sizeof(prop), "port%d-mode", i + 1);
  437. ret = of_property_read_string(node, prop, &mode);
  438. if (ret < 0)
  439. continue;
  440. ret = omap_usbhs_get_dt_port_mode(mode);
  441. if (ret < 0) {
  442. dev_warn(dev, "Invalid port%d-mode \"%s\" in device tree\n",
  443. i, mode);
  444. return -ENODEV;
  445. }
  446. dev_dbg(dev, "port%d-mode: %s -> %d\n", i, mode, ret);
  447. pdata->port_mode[i] = ret;
  448. }
  449. /* get flags */
  450. pdata->single_ulpi_bypass = of_property_read_bool(node,
  451. "single-ulpi-bypass");
  452. return 0;
  453. }
  454. static const struct of_device_id usbhs_child_match_table[] = {
  455. { .compatible = "ti,ehci-omap", },
  456. { .compatible = "ti,ohci-omap3", },
  457. { }
  458. };
  459. /**
  460. * usbhs_omap_probe - initialize TI-based HCDs
  461. *
  462. * Allocates basic resources for this USB host controller.
  463. */
  464. static int usbhs_omap_probe(struct platform_device *pdev)
  465. {
  466. struct device *dev = &pdev->dev;
  467. struct usbhs_omap_platform_data *pdata = dev_get_platdata(dev);
  468. struct usbhs_hcd_omap *omap;
  469. struct resource *res;
  470. int ret = 0;
  471. int i;
  472. bool need_logic_fck;
  473. if (dev->of_node) {
  474. /* For DT boot we populate platform data from OF node */
  475. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  476. if (!pdata)
  477. return -ENOMEM;
  478. ret = usbhs_omap_get_dt_pdata(dev, pdata);
  479. if (ret)
  480. return ret;
  481. dev->platform_data = pdata;
  482. }
  483. if (!pdata) {
  484. dev_err(dev, "Missing platform data\n");
  485. return -ENODEV;
  486. }
  487. if (pdata->nports > OMAP3_HS_USB_PORTS) {
  488. dev_info(dev, "Too many num_ports <%d> in platform_data. Max %d\n",
  489. pdata->nports, OMAP3_HS_USB_PORTS);
  490. return -ENODEV;
  491. }
  492. omap = devm_kzalloc(dev, sizeof(*omap), GFP_KERNEL);
  493. if (!omap) {
  494. dev_err(dev, "Memory allocation failed\n");
  495. return -ENOMEM;
  496. }
  497. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  498. omap->uhh_base = devm_ioremap_resource(dev, res);
  499. if (IS_ERR(omap->uhh_base))
  500. return PTR_ERR(omap->uhh_base);
  501. omap->pdata = pdata;
  502. /* Initialize the TLL subsystem */
  503. omap_tll_init(pdata);
  504. pm_runtime_enable(dev);
  505. platform_set_drvdata(pdev, omap);
  506. pm_runtime_get_sync(dev);
  507. omap->usbhs_rev = usbhs_read(omap->uhh_base, OMAP_UHH_REVISION);
  508. /* we need to call runtime suspend before we update omap->nports
  509. * to prevent unbalanced clk_disable()
  510. */
  511. pm_runtime_put_sync(dev);
  512. /*
  513. * If platform data contains nports then use that
  514. * else make out number of ports from USBHS revision
  515. */
  516. if (pdata->nports) {
  517. omap->nports = pdata->nports;
  518. } else {
  519. switch (omap->usbhs_rev) {
  520. case OMAP_USBHS_REV1:
  521. omap->nports = 3;
  522. break;
  523. case OMAP_USBHS_REV2:
  524. omap->nports = 2;
  525. break;
  526. default:
  527. omap->nports = OMAP3_HS_USB_PORTS;
  528. dev_dbg(dev,
  529. "USB HOST Rev:0x%x not recognized, assuming %d ports\n",
  530. omap->usbhs_rev, omap->nports);
  531. break;
  532. }
  533. pdata->nports = omap->nports;
  534. }
  535. i = sizeof(struct clk *) * omap->nports;
  536. omap->utmi_clk = devm_kzalloc(dev, i, GFP_KERNEL);
  537. omap->hsic480m_clk = devm_kzalloc(dev, i, GFP_KERNEL);
  538. omap->hsic60m_clk = devm_kzalloc(dev, i, GFP_KERNEL);
  539. if (!omap->utmi_clk || !omap->hsic480m_clk || !omap->hsic60m_clk) {
  540. dev_err(dev, "Memory allocation failed\n");
  541. ret = -ENOMEM;
  542. goto err_mem;
  543. }
  544. /* Set all clocks as invalid to begin with */
  545. omap->ehci_logic_fck = ERR_PTR(-ENODEV);
  546. omap->init_60m_fclk = ERR_PTR(-ENODEV);
  547. omap->utmi_p1_gfclk = ERR_PTR(-ENODEV);
  548. omap->utmi_p2_gfclk = ERR_PTR(-ENODEV);
  549. omap->xclk60mhsp1_ck = ERR_PTR(-ENODEV);
  550. omap->xclk60mhsp2_ck = ERR_PTR(-ENODEV);
  551. for (i = 0; i < omap->nports; i++) {
  552. omap->utmi_clk[i] = ERR_PTR(-ENODEV);
  553. omap->hsic480m_clk[i] = ERR_PTR(-ENODEV);
  554. omap->hsic60m_clk[i] = ERR_PTR(-ENODEV);
  555. }
  556. /* for OMAP3 i.e. USBHS REV1 */
  557. if (omap->usbhs_rev == OMAP_USBHS_REV1) {
  558. need_logic_fck = false;
  559. for (i = 0; i < omap->nports; i++) {
  560. if (is_ehci_phy_mode(pdata->port_mode[i]) ||
  561. is_ehci_tll_mode(pdata->port_mode[i]) ||
  562. is_ehci_hsic_mode(pdata->port_mode[i]))
  563. need_logic_fck |= true;
  564. }
  565. if (need_logic_fck) {
  566. omap->ehci_logic_fck = devm_clk_get(dev,
  567. "usbhost_120m_fck");
  568. if (IS_ERR(omap->ehci_logic_fck)) {
  569. ret = PTR_ERR(omap->ehci_logic_fck);
  570. dev_err(dev, "usbhost_120m_fck failed:%d\n",
  571. ret);
  572. goto err_mem;
  573. }
  574. }
  575. goto initialize;
  576. }
  577. /* for OMAP4+ i.e. USBHS REV2+ */
  578. omap->utmi_p1_gfclk = devm_clk_get(dev, "utmi_p1_gfclk");
  579. if (IS_ERR(omap->utmi_p1_gfclk)) {
  580. ret = PTR_ERR(omap->utmi_p1_gfclk);
  581. dev_err(dev, "utmi_p1_gfclk failed error:%d\n", ret);
  582. goto err_mem;
  583. }
  584. omap->utmi_p2_gfclk = devm_clk_get(dev, "utmi_p2_gfclk");
  585. if (IS_ERR(omap->utmi_p2_gfclk)) {
  586. ret = PTR_ERR(omap->utmi_p2_gfclk);
  587. dev_err(dev, "utmi_p2_gfclk failed error:%d\n", ret);
  588. goto err_mem;
  589. }
  590. omap->xclk60mhsp1_ck = devm_clk_get(dev, "refclk_60m_ext_p1");
  591. if (IS_ERR(omap->xclk60mhsp1_ck)) {
  592. ret = PTR_ERR(omap->xclk60mhsp1_ck);
  593. dev_err(dev, "refclk_60m_ext_p1 failed error:%d\n", ret);
  594. goto err_mem;
  595. }
  596. omap->xclk60mhsp2_ck = devm_clk_get(dev, "refclk_60m_ext_p2");
  597. if (IS_ERR(omap->xclk60mhsp2_ck)) {
  598. ret = PTR_ERR(omap->xclk60mhsp2_ck);
  599. dev_err(dev, "refclk_60m_ext_p2 failed error:%d\n", ret);
  600. goto err_mem;
  601. }
  602. omap->init_60m_fclk = devm_clk_get(dev, "refclk_60m_int");
  603. if (IS_ERR(omap->init_60m_fclk)) {
  604. ret = PTR_ERR(omap->init_60m_fclk);
  605. dev_err(dev, "refclk_60m_int failed error:%d\n", ret);
  606. goto err_mem;
  607. }
  608. for (i = 0; i < omap->nports; i++) {
  609. char clkname[30];
  610. /* clock names are indexed from 1*/
  611. snprintf(clkname, sizeof(clkname),
  612. "usb_host_hs_utmi_p%d_clk", i + 1);
  613. /* If a clock is not found we won't bail out as not all
  614. * platforms have all clocks and we can function without
  615. * them
  616. */
  617. omap->utmi_clk[i] = devm_clk_get(dev, clkname);
  618. if (IS_ERR(omap->utmi_clk[i])) {
  619. ret = PTR_ERR(omap->utmi_clk[i]);
  620. dev_err(dev, "Failed to get clock : %s : %d\n",
  621. clkname, ret);
  622. goto err_mem;
  623. }
  624. snprintf(clkname, sizeof(clkname),
  625. "usb_host_hs_hsic480m_p%d_clk", i + 1);
  626. omap->hsic480m_clk[i] = devm_clk_get(dev, clkname);
  627. if (IS_ERR(omap->hsic480m_clk[i])) {
  628. ret = PTR_ERR(omap->hsic480m_clk[i]);
  629. dev_err(dev, "Failed to get clock : %s : %d\n",
  630. clkname, ret);
  631. goto err_mem;
  632. }
  633. snprintf(clkname, sizeof(clkname),
  634. "usb_host_hs_hsic60m_p%d_clk", i + 1);
  635. omap->hsic60m_clk[i] = devm_clk_get(dev, clkname);
  636. if (IS_ERR(omap->hsic60m_clk[i])) {
  637. ret = PTR_ERR(omap->hsic60m_clk[i]);
  638. dev_err(dev, "Failed to get clock : %s : %d\n",
  639. clkname, ret);
  640. goto err_mem;
  641. }
  642. }
  643. if (is_ehci_phy_mode(pdata->port_mode[0])) {
  644. ret = clk_set_parent(omap->utmi_p1_gfclk,
  645. omap->xclk60mhsp1_ck);
  646. if (ret != 0) {
  647. dev_err(dev, "xclk60mhsp1_ck set parent failed: %d\n",
  648. ret);
  649. goto err_mem;
  650. }
  651. } else if (is_ehci_tll_mode(pdata->port_mode[0])) {
  652. ret = clk_set_parent(omap->utmi_p1_gfclk,
  653. omap->init_60m_fclk);
  654. if (ret != 0) {
  655. dev_err(dev, "P0 init_60m_fclk set parent failed: %d\n",
  656. ret);
  657. goto err_mem;
  658. }
  659. }
  660. if (is_ehci_phy_mode(pdata->port_mode[1])) {
  661. ret = clk_set_parent(omap->utmi_p2_gfclk,
  662. omap->xclk60mhsp2_ck);
  663. if (ret != 0) {
  664. dev_err(dev, "xclk60mhsp2_ck set parent failed: %d\n",
  665. ret);
  666. goto err_mem;
  667. }
  668. } else if (is_ehci_tll_mode(pdata->port_mode[1])) {
  669. ret = clk_set_parent(omap->utmi_p2_gfclk,
  670. omap->init_60m_fclk);
  671. if (ret != 0) {
  672. dev_err(dev, "P1 init_60m_fclk set parent failed: %d\n",
  673. ret);
  674. goto err_mem;
  675. }
  676. }
  677. initialize:
  678. omap_usbhs_init(dev);
  679. if (dev->of_node) {
  680. ret = of_platform_populate(dev->of_node,
  681. usbhs_child_match_table, NULL, dev);
  682. if (ret) {
  683. dev_err(dev, "Failed to create DT children: %d\n", ret);
  684. goto err_mem;
  685. }
  686. } else {
  687. ret = omap_usbhs_alloc_children(pdev);
  688. if (ret) {
  689. dev_err(dev, "omap_usbhs_alloc_children failed: %d\n",
  690. ret);
  691. goto err_mem;
  692. }
  693. }
  694. return 0;
  695. err_mem:
  696. pm_runtime_disable(dev);
  697. return ret;
  698. }
  699. static int usbhs_omap_remove_child(struct device *dev, void *data)
  700. {
  701. dev_info(dev, "unregistering\n");
  702. platform_device_unregister(to_platform_device(dev));
  703. return 0;
  704. }
  705. /**
  706. * usbhs_omap_remove - shutdown processing for UHH & TLL HCDs
  707. * @pdev: USB Host Controller being removed
  708. *
  709. * Reverses the effect of usbhs_omap_probe().
  710. */
  711. static int usbhs_omap_remove(struct platform_device *pdev)
  712. {
  713. pm_runtime_disable(&pdev->dev);
  714. /* remove children */
  715. device_for_each_child(&pdev->dev, NULL, usbhs_omap_remove_child);
  716. return 0;
  717. }
  718. static const struct dev_pm_ops usbhsomap_dev_pm_ops = {
  719. .runtime_suspend = usbhs_runtime_suspend,
  720. .runtime_resume = usbhs_runtime_resume,
  721. };
  722. static const struct of_device_id usbhs_omap_dt_ids[] = {
  723. { .compatible = "ti,usbhs-host" },
  724. { }
  725. };
  726. MODULE_DEVICE_TABLE(of, usbhs_omap_dt_ids);
  727. static struct platform_driver usbhs_omap_driver = {
  728. .driver = {
  729. .name = (char *)usbhs_driver_name,
  730. .pm = &usbhsomap_dev_pm_ops,
  731. .of_match_table = usbhs_omap_dt_ids,
  732. },
  733. .probe = usbhs_omap_probe,
  734. .remove = usbhs_omap_remove,
  735. };
  736. MODULE_AUTHOR("Keshava Munegowda <keshava_mgowda@ti.com>");
  737. MODULE_AUTHOR("Roger Quadros <rogerq@ti.com>");
  738. MODULE_ALIAS("platform:" USBHS_DRIVER_NAME);
  739. MODULE_LICENSE("GPL v2");
  740. MODULE_DESCRIPTION("usb host common core driver for omap EHCI and OHCI");
  741. static int omap_usbhs_drvinit(void)
  742. {
  743. return platform_driver_register(&usbhs_omap_driver);
  744. }
  745. /*
  746. * init before ehci and ohci drivers;
  747. * The usbhs core driver should be initialized much before
  748. * the omap ehci and ohci probe functions are called.
  749. * This usbhs core driver should be initialized after
  750. * usb tll driver
  751. */
  752. fs_initcall_sync(omap_usbhs_drvinit);
  753. static void omap_usbhs_drvexit(void)
  754. {
  755. platform_driver_unregister(&usbhs_omap_driver);
  756. }
  757. module_exit(omap_usbhs_drvexit);