omap-usb-host.c 24 KB

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