omap-usb-tll.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /**
  2. * omap-usb-tll.c - The USB TLL driver for OMAP EHCI & OHCI
  3. *
  4. * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com
  5. * Author: Keshava Munegowda <keshava_mgowda@ti.com>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 of
  9. * the License as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/types.h>
  22. #include <linux/slab.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/clk.h>
  26. #include <linux/io.h>
  27. #include <linux/err.h>
  28. #include <linux/pm_runtime.h>
  29. #include <linux/platform_data/usb-omap.h>
  30. #define USBTLL_DRIVER_NAME "usbhs_tll"
  31. /* TLL Register Set */
  32. #define OMAP_USBTLL_REVISION (0x00)
  33. #define OMAP_USBTLL_SYSCONFIG (0x10)
  34. #define OMAP_USBTLL_SYSCONFIG_CACTIVITY (1 << 8)
  35. #define OMAP_USBTLL_SYSCONFIG_SIDLEMODE (1 << 3)
  36. #define OMAP_USBTLL_SYSCONFIG_ENAWAKEUP (1 << 2)
  37. #define OMAP_USBTLL_SYSCONFIG_SOFTRESET (1 << 1)
  38. #define OMAP_USBTLL_SYSCONFIG_AUTOIDLE (1 << 0)
  39. #define OMAP_USBTLL_SYSSTATUS (0x14)
  40. #define OMAP_USBTLL_SYSSTATUS_RESETDONE (1 << 0)
  41. #define OMAP_USBTLL_IRQSTATUS (0x18)
  42. #define OMAP_USBTLL_IRQENABLE (0x1C)
  43. #define OMAP_TLL_SHARED_CONF (0x30)
  44. #define OMAP_TLL_SHARED_CONF_USB_90D_DDR_EN (1 << 6)
  45. #define OMAP_TLL_SHARED_CONF_USB_180D_SDR_EN (1 << 5)
  46. #define OMAP_TLL_SHARED_CONF_USB_DIVRATION (1 << 2)
  47. #define OMAP_TLL_SHARED_CONF_FCLK_REQ (1 << 1)
  48. #define OMAP_TLL_SHARED_CONF_FCLK_IS_ON (1 << 0)
  49. #define OMAP_TLL_CHANNEL_CONF(num) (0x040 + 0x004 * num)
  50. #define OMAP_TLL_CHANNEL_CONF_FSLSMODE_SHIFT 24
  51. #define OMAP_TLL_CHANNEL_CONF_DRVVBUS (1 << 16)
  52. #define OMAP_TLL_CHANNEL_CONF_CHRGVBUS (1 << 15)
  53. #define OMAP_TLL_CHANNEL_CONF_ULPINOBITSTUFF (1 << 11)
  54. #define OMAP_TLL_CHANNEL_CONF_ULPI_ULPIAUTOIDLE (1 << 10)
  55. #define OMAP_TLL_CHANNEL_CONF_UTMIAUTOIDLE (1 << 9)
  56. #define OMAP_TLL_CHANNEL_CONF_ULPIDDRMODE (1 << 8)
  57. #define OMAP_TLL_CHANNEL_CONF_MODE_TRANSPARENT_UTMI (2 << 1)
  58. #define OMAP_TLL_CHANNEL_CONF_CHANMODE_FSLS (1 << 1)
  59. #define OMAP_TLL_CHANNEL_CONF_CHANEN (1 << 0)
  60. #define OMAP_TLL_FSLSMODE_6PIN_PHY_DAT_SE0 0x0
  61. #define OMAP_TLL_FSLSMODE_6PIN_PHY_DP_DM 0x1
  62. #define OMAP_TLL_FSLSMODE_3PIN_PHY 0x2
  63. #define OMAP_TLL_FSLSMODE_4PIN_PHY 0x3
  64. #define OMAP_TLL_FSLSMODE_6PIN_TLL_DAT_SE0 0x4
  65. #define OMAP_TLL_FSLSMODE_6PIN_TLL_DP_DM 0x5
  66. #define OMAP_TLL_FSLSMODE_3PIN_TLL 0x6
  67. #define OMAP_TLL_FSLSMODE_4PIN_TLL 0x7
  68. #define OMAP_TLL_FSLSMODE_2PIN_TLL_DAT_SE0 0xA
  69. #define OMAP_TLL_FSLSMODE_2PIN_DAT_DP_DM 0xB
  70. #define OMAP_TLL_ULPI_FUNCTION_CTRL(num) (0x804 + 0x100 * num)
  71. #define OMAP_TLL_ULPI_INTERFACE_CTRL(num) (0x807 + 0x100 * num)
  72. #define OMAP_TLL_ULPI_OTG_CTRL(num) (0x80A + 0x100 * num)
  73. #define OMAP_TLL_ULPI_INT_EN_RISE(num) (0x80D + 0x100 * num)
  74. #define OMAP_TLL_ULPI_INT_EN_FALL(num) (0x810 + 0x100 * num)
  75. #define OMAP_TLL_ULPI_INT_STATUS(num) (0x813 + 0x100 * num)
  76. #define OMAP_TLL_ULPI_INT_LATCH(num) (0x814 + 0x100 * num)
  77. #define OMAP_TLL_ULPI_DEBUG(num) (0x815 + 0x100 * num)
  78. #define OMAP_TLL_ULPI_SCRATCH_REGISTER(num) (0x816 + 0x100 * num)
  79. #define OMAP_REV2_TLL_CHANNEL_COUNT 2
  80. #define OMAP_TLL_CHANNEL_COUNT 3
  81. #define OMAP_TLL_CHANNEL_1_EN_MASK (1 << 0)
  82. #define OMAP_TLL_CHANNEL_2_EN_MASK (1 << 1)
  83. #define OMAP_TLL_CHANNEL_3_EN_MASK (1 << 2)
  84. /* Values of USBTLL_REVISION - Note: these are not given in the TRM */
  85. #define OMAP_USBTLL_REV1 0x00000015 /* OMAP3 */
  86. #define OMAP_USBTLL_REV2 0x00000018 /* OMAP 3630 */
  87. #define OMAP_USBTLL_REV3 0x00000004 /* OMAP4 */
  88. #define OMAP_USBTLL_REV4 0x00000006 /* OMAP5 */
  89. #define is_ehci_tll_mode(x) (x == OMAP_EHCI_PORT_MODE_TLL)
  90. /* only PHY and UNUSED modes don't need TLL */
  91. #define omap_usb_mode_needs_tll(x) ((x) != OMAP_USBHS_PORT_MODE_UNUSED &&\
  92. (x) != OMAP_EHCI_PORT_MODE_PHY)
  93. struct usbtll_omap {
  94. int nch; /* num. of channels */
  95. struct usbhs_omap_platform_data *pdata;
  96. struct clk **ch_clk;
  97. };
  98. /*-------------------------------------------------------------------------*/
  99. static const char usbtll_driver_name[] = USBTLL_DRIVER_NAME;
  100. static struct device *tll_dev;
  101. static DEFINE_SPINLOCK(tll_lock); /* serialize access to tll_dev */
  102. /*-------------------------------------------------------------------------*/
  103. static inline void usbtll_write(void __iomem *base, u32 reg, u32 val)
  104. {
  105. __raw_writel(val, base + reg);
  106. }
  107. static inline u32 usbtll_read(void __iomem *base, u32 reg)
  108. {
  109. return __raw_readl(base + reg);
  110. }
  111. static inline void usbtll_writeb(void __iomem *base, u8 reg, u8 val)
  112. {
  113. __raw_writeb(val, base + reg);
  114. }
  115. static inline u8 usbtll_readb(void __iomem *base, u8 reg)
  116. {
  117. return __raw_readb(base + reg);
  118. }
  119. /*-------------------------------------------------------------------------*/
  120. static bool is_ohci_port(enum usbhs_omap_port_mode pmode)
  121. {
  122. switch (pmode) {
  123. case OMAP_OHCI_PORT_MODE_PHY_6PIN_DATSE0:
  124. case OMAP_OHCI_PORT_MODE_PHY_6PIN_DPDM:
  125. case OMAP_OHCI_PORT_MODE_PHY_3PIN_DATSE0:
  126. case OMAP_OHCI_PORT_MODE_PHY_4PIN_DPDM:
  127. case OMAP_OHCI_PORT_MODE_TLL_6PIN_DATSE0:
  128. case OMAP_OHCI_PORT_MODE_TLL_6PIN_DPDM:
  129. case OMAP_OHCI_PORT_MODE_TLL_3PIN_DATSE0:
  130. case OMAP_OHCI_PORT_MODE_TLL_4PIN_DPDM:
  131. case OMAP_OHCI_PORT_MODE_TLL_2PIN_DATSE0:
  132. case OMAP_OHCI_PORT_MODE_TLL_2PIN_DPDM:
  133. return true;
  134. default:
  135. return false;
  136. }
  137. }
  138. /*
  139. * convert the port-mode enum to a value we can use in the FSLSMODE
  140. * field of USBTLL_CHANNEL_CONF
  141. */
  142. static unsigned ohci_omap3_fslsmode(enum usbhs_omap_port_mode mode)
  143. {
  144. switch (mode) {
  145. case OMAP_USBHS_PORT_MODE_UNUSED:
  146. case OMAP_OHCI_PORT_MODE_PHY_6PIN_DATSE0:
  147. return OMAP_TLL_FSLSMODE_6PIN_PHY_DAT_SE0;
  148. case OMAP_OHCI_PORT_MODE_PHY_6PIN_DPDM:
  149. return OMAP_TLL_FSLSMODE_6PIN_PHY_DP_DM;
  150. case OMAP_OHCI_PORT_MODE_PHY_3PIN_DATSE0:
  151. return OMAP_TLL_FSLSMODE_3PIN_PHY;
  152. case OMAP_OHCI_PORT_MODE_PHY_4PIN_DPDM:
  153. return OMAP_TLL_FSLSMODE_4PIN_PHY;
  154. case OMAP_OHCI_PORT_MODE_TLL_6PIN_DATSE0:
  155. return OMAP_TLL_FSLSMODE_6PIN_TLL_DAT_SE0;
  156. case OMAP_OHCI_PORT_MODE_TLL_6PIN_DPDM:
  157. return OMAP_TLL_FSLSMODE_6PIN_TLL_DP_DM;
  158. case OMAP_OHCI_PORT_MODE_TLL_3PIN_DATSE0:
  159. return OMAP_TLL_FSLSMODE_3PIN_TLL;
  160. case OMAP_OHCI_PORT_MODE_TLL_4PIN_DPDM:
  161. return OMAP_TLL_FSLSMODE_4PIN_TLL;
  162. case OMAP_OHCI_PORT_MODE_TLL_2PIN_DATSE0:
  163. return OMAP_TLL_FSLSMODE_2PIN_TLL_DAT_SE0;
  164. case OMAP_OHCI_PORT_MODE_TLL_2PIN_DPDM:
  165. return OMAP_TLL_FSLSMODE_2PIN_DAT_DP_DM;
  166. default:
  167. pr_warn("Invalid port mode, using default\n");
  168. return OMAP_TLL_FSLSMODE_6PIN_PHY_DAT_SE0;
  169. }
  170. }
  171. /**
  172. * usbtll_omap_probe - initialize TI-based HCDs
  173. *
  174. * Allocates basic resources for this USB host controller.
  175. */
  176. static int usbtll_omap_probe(struct platform_device *pdev)
  177. {
  178. struct device *dev = &pdev->dev;
  179. struct usbhs_omap_platform_data *pdata = dev->platform_data;
  180. void __iomem *base;
  181. struct resource *res;
  182. struct usbtll_omap *tll;
  183. unsigned reg;
  184. int ret = 0;
  185. int i, ver;
  186. bool needs_tll;
  187. dev_dbg(dev, "starting TI HSUSB TLL Controller\n");
  188. tll = devm_kzalloc(dev, sizeof(struct usbtll_omap), GFP_KERNEL);
  189. if (!tll) {
  190. dev_err(dev, "Memory allocation failed\n");
  191. return -ENOMEM;
  192. }
  193. if (!pdata) {
  194. dev_err(dev, "Platform data missing\n");
  195. return -ENODEV;
  196. }
  197. tll->pdata = pdata;
  198. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  199. base = devm_request_and_ioremap(dev, res);
  200. if (!base) {
  201. ret = -EADDRNOTAVAIL;
  202. dev_err(dev, "Resource request/ioremap failed:%d\n", ret);
  203. return ret;
  204. }
  205. platform_set_drvdata(pdev, tll);
  206. pm_runtime_enable(dev);
  207. pm_runtime_get_sync(dev);
  208. ver = usbtll_read(base, OMAP_USBTLL_REVISION);
  209. switch (ver) {
  210. case OMAP_USBTLL_REV1:
  211. case OMAP_USBTLL_REV4:
  212. tll->nch = OMAP_TLL_CHANNEL_COUNT;
  213. break;
  214. case OMAP_USBTLL_REV2:
  215. case OMAP_USBTLL_REV3:
  216. tll->nch = OMAP_REV2_TLL_CHANNEL_COUNT;
  217. break;
  218. default:
  219. tll->nch = OMAP_TLL_CHANNEL_COUNT;
  220. dev_dbg(dev,
  221. "USB TLL Rev : 0x%x not recognized, assuming %d channels\n",
  222. ver, tll->nch);
  223. break;
  224. }
  225. tll->ch_clk = devm_kzalloc(dev, sizeof(struct clk * [tll->nch]),
  226. GFP_KERNEL);
  227. if (!tll->ch_clk) {
  228. ret = -ENOMEM;
  229. dev_err(dev, "Couldn't allocate memory for channel clocks\n");
  230. goto err_clk_alloc;
  231. }
  232. for (i = 0; i < tll->nch; i++) {
  233. char clkname[] = "usb_tll_hs_usb_chx_clk";
  234. snprintf(clkname, sizeof(clkname),
  235. "usb_tll_hs_usb_ch%d_clk", i);
  236. tll->ch_clk[i] = clk_get(dev, clkname);
  237. if (IS_ERR(tll->ch_clk[i]))
  238. dev_dbg(dev, "can't get clock : %s\n", clkname);
  239. }
  240. needs_tll = false;
  241. for (i = 0; i < tll->nch; i++)
  242. needs_tll |= omap_usb_mode_needs_tll(pdata->port_mode[i]);
  243. if (needs_tll) {
  244. /* Program Common TLL register */
  245. reg = usbtll_read(base, OMAP_TLL_SHARED_CONF);
  246. reg |= (OMAP_TLL_SHARED_CONF_FCLK_IS_ON
  247. | OMAP_TLL_SHARED_CONF_USB_DIVRATION);
  248. reg &= ~OMAP_TLL_SHARED_CONF_USB_90D_DDR_EN;
  249. reg &= ~OMAP_TLL_SHARED_CONF_USB_180D_SDR_EN;
  250. usbtll_write(base, OMAP_TLL_SHARED_CONF, reg);
  251. /* Enable channels now */
  252. for (i = 0; i < tll->nch; i++) {
  253. reg = usbtll_read(base, OMAP_TLL_CHANNEL_CONF(i));
  254. if (is_ohci_port(pdata->port_mode[i])) {
  255. reg |= ohci_omap3_fslsmode(pdata->port_mode[i])
  256. << OMAP_TLL_CHANNEL_CONF_FSLSMODE_SHIFT;
  257. reg |= OMAP_TLL_CHANNEL_CONF_CHANMODE_FSLS;
  258. } else if (pdata->port_mode[i] ==
  259. OMAP_EHCI_PORT_MODE_TLL) {
  260. /*
  261. * Disable AutoIdle, BitStuffing
  262. * and use SDR Mode
  263. */
  264. reg &= ~(OMAP_TLL_CHANNEL_CONF_UTMIAUTOIDLE
  265. | OMAP_TLL_CHANNEL_CONF_ULPINOBITSTUFF
  266. | OMAP_TLL_CHANNEL_CONF_ULPIDDRMODE);
  267. } else if (pdata->port_mode[i] ==
  268. OMAP_EHCI_PORT_MODE_HSIC) {
  269. /*
  270. * HSIC Mode requires UTMI port configurations
  271. */
  272. reg |= OMAP_TLL_CHANNEL_CONF_DRVVBUS
  273. | OMAP_TLL_CHANNEL_CONF_CHRGVBUS
  274. | OMAP_TLL_CHANNEL_CONF_MODE_TRANSPARENT_UTMI
  275. | OMAP_TLL_CHANNEL_CONF_ULPINOBITSTUFF;
  276. } else {
  277. continue;
  278. }
  279. reg |= OMAP_TLL_CHANNEL_CONF_CHANEN;
  280. usbtll_write(base, OMAP_TLL_CHANNEL_CONF(i), reg);
  281. usbtll_writeb(base,
  282. OMAP_TLL_ULPI_SCRATCH_REGISTER(i),
  283. 0xbe);
  284. }
  285. }
  286. pm_runtime_put_sync(dev);
  287. /* only after this can omap_tll_enable/disable work */
  288. spin_lock(&tll_lock);
  289. tll_dev = dev;
  290. spin_unlock(&tll_lock);
  291. return 0;
  292. err_clk_alloc:
  293. pm_runtime_put_sync(dev);
  294. pm_runtime_disable(dev);
  295. return ret;
  296. }
  297. /**
  298. * usbtll_omap_remove - shutdown processing for UHH & TLL HCDs
  299. * @pdev: USB Host Controller being removed
  300. *
  301. * Reverses the effect of usbtll_omap_probe().
  302. */
  303. static int usbtll_omap_remove(struct platform_device *pdev)
  304. {
  305. struct usbtll_omap *tll = platform_get_drvdata(pdev);
  306. int i;
  307. spin_lock(&tll_lock);
  308. tll_dev = NULL;
  309. spin_unlock(&tll_lock);
  310. for (i = 0; i < tll->nch; i++)
  311. if (!IS_ERR(tll->ch_clk[i]))
  312. clk_put(tll->ch_clk[i]);
  313. pm_runtime_disable(&pdev->dev);
  314. return 0;
  315. }
  316. static int usbtll_runtime_resume(struct device *dev)
  317. {
  318. struct usbtll_omap *tll = dev_get_drvdata(dev);
  319. struct usbhs_omap_platform_data *pdata = tll->pdata;
  320. int i;
  321. dev_dbg(dev, "usbtll_runtime_resume\n");
  322. for (i = 0; i < tll->nch; i++) {
  323. if (omap_usb_mode_needs_tll(pdata->port_mode[i])) {
  324. int r;
  325. if (IS_ERR(tll->ch_clk[i]))
  326. continue;
  327. r = clk_enable(tll->ch_clk[i]);
  328. if (r) {
  329. dev_err(dev,
  330. "Error enabling ch %d clock: %d\n", i, r);
  331. }
  332. }
  333. }
  334. return 0;
  335. }
  336. static int usbtll_runtime_suspend(struct device *dev)
  337. {
  338. struct usbtll_omap *tll = dev_get_drvdata(dev);
  339. struct usbhs_omap_platform_data *pdata = tll->pdata;
  340. int i;
  341. dev_dbg(dev, "usbtll_runtime_suspend\n");
  342. for (i = 0; i < tll->nch; i++) {
  343. if (omap_usb_mode_needs_tll(pdata->port_mode[i])) {
  344. if (!IS_ERR(tll->ch_clk[i]))
  345. clk_disable(tll->ch_clk[i]);
  346. }
  347. }
  348. return 0;
  349. }
  350. static const struct dev_pm_ops usbtllomap_dev_pm_ops = {
  351. SET_RUNTIME_PM_OPS(usbtll_runtime_suspend,
  352. usbtll_runtime_resume,
  353. NULL)
  354. };
  355. static struct platform_driver usbtll_omap_driver = {
  356. .driver = {
  357. .name = (char *)usbtll_driver_name,
  358. .owner = THIS_MODULE,
  359. .pm = &usbtllomap_dev_pm_ops,
  360. },
  361. .probe = usbtll_omap_probe,
  362. .remove = usbtll_omap_remove,
  363. };
  364. int omap_tll_enable(void)
  365. {
  366. int ret;
  367. spin_lock(&tll_lock);
  368. if (!tll_dev) {
  369. pr_err("%s: OMAP USB TLL not initialized\n", __func__);
  370. ret = -ENODEV;
  371. } else {
  372. ret = pm_runtime_get_sync(tll_dev);
  373. }
  374. spin_unlock(&tll_lock);
  375. return ret;
  376. }
  377. EXPORT_SYMBOL_GPL(omap_tll_enable);
  378. int omap_tll_disable(void)
  379. {
  380. int ret;
  381. spin_lock(&tll_lock);
  382. if (!tll_dev) {
  383. pr_err("%s: OMAP USB TLL not initialized\n", __func__);
  384. ret = -ENODEV;
  385. } else {
  386. ret = pm_runtime_put_sync(tll_dev);
  387. }
  388. spin_unlock(&tll_lock);
  389. return ret;
  390. }
  391. EXPORT_SYMBOL_GPL(omap_tll_disable);
  392. MODULE_AUTHOR("Keshava Munegowda <keshava_mgowda@ti.com>");
  393. MODULE_ALIAS("platform:" USBHS_DRIVER_NAME);
  394. MODULE_LICENSE("GPL v2");
  395. MODULE_DESCRIPTION("usb tll driver for TI OMAP EHCI and OHCI controllers");
  396. static int __init omap_usbtll_drvinit(void)
  397. {
  398. return platform_driver_register(&usbtll_omap_driver);
  399. }
  400. /*
  401. * init before usbhs core driver;
  402. * The usbtll driver should be initialized before
  403. * the usbhs core driver probe function is called.
  404. */
  405. fs_initcall(omap_usbtll_drvinit);
  406. static void __exit omap_usbtll_drvexit(void)
  407. {
  408. platform_driver_unregister(&usbtll_omap_driver);
  409. }
  410. module_exit(omap_usbtll_drvexit);