omap-usb-tll.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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_ULPINOBITSTUFF (1 << 11)
  52. #define OMAP_TLL_CHANNEL_CONF_ULPI_ULPIAUTOIDLE (1 << 10)
  53. #define OMAP_TLL_CHANNEL_CONF_UTMIAUTOIDLE (1 << 9)
  54. #define OMAP_TLL_CHANNEL_CONF_ULPIDDRMODE (1 << 8)
  55. #define OMAP_TLL_CHANNEL_CONF_CHANMODE_FSLS (1 << 1)
  56. #define OMAP_TLL_CHANNEL_CONF_CHANEN (1 << 0)
  57. #define OMAP_TLL_FSLSMODE_6PIN_PHY_DAT_SE0 0x0
  58. #define OMAP_TLL_FSLSMODE_6PIN_PHY_DP_DM 0x1
  59. #define OMAP_TLL_FSLSMODE_3PIN_PHY 0x2
  60. #define OMAP_TLL_FSLSMODE_4PIN_PHY 0x3
  61. #define OMAP_TLL_FSLSMODE_6PIN_TLL_DAT_SE0 0x4
  62. #define OMAP_TLL_FSLSMODE_6PIN_TLL_DP_DM 0x5
  63. #define OMAP_TLL_FSLSMODE_3PIN_TLL 0x6
  64. #define OMAP_TLL_FSLSMODE_4PIN_TLL 0x7
  65. #define OMAP_TLL_FSLSMODE_2PIN_TLL_DAT_SE0 0xA
  66. #define OMAP_TLL_FSLSMODE_2PIN_DAT_DP_DM 0xB
  67. #define OMAP_TLL_ULPI_FUNCTION_CTRL(num) (0x804 + 0x100 * num)
  68. #define OMAP_TLL_ULPI_INTERFACE_CTRL(num) (0x807 + 0x100 * num)
  69. #define OMAP_TLL_ULPI_OTG_CTRL(num) (0x80A + 0x100 * num)
  70. #define OMAP_TLL_ULPI_INT_EN_RISE(num) (0x80D + 0x100 * num)
  71. #define OMAP_TLL_ULPI_INT_EN_FALL(num) (0x810 + 0x100 * num)
  72. #define OMAP_TLL_ULPI_INT_STATUS(num) (0x813 + 0x100 * num)
  73. #define OMAP_TLL_ULPI_INT_LATCH(num) (0x814 + 0x100 * num)
  74. #define OMAP_TLL_ULPI_DEBUG(num) (0x815 + 0x100 * num)
  75. #define OMAP_TLL_ULPI_SCRATCH_REGISTER(num) (0x816 + 0x100 * num)
  76. #define OMAP_REV2_TLL_CHANNEL_COUNT 2
  77. #define OMAP_TLL_CHANNEL_COUNT 3
  78. #define OMAP_TLL_CHANNEL_1_EN_MASK (1 << 0)
  79. #define OMAP_TLL_CHANNEL_2_EN_MASK (1 << 1)
  80. #define OMAP_TLL_CHANNEL_3_EN_MASK (1 << 2)
  81. /* Values of USBTLL_REVISION - Note: these are not given in the TRM */
  82. #define OMAP_USBTLL_REV1 0x00000015 /* OMAP3 */
  83. #define OMAP_USBTLL_REV2 0x00000018 /* OMAP 3630 */
  84. #define OMAP_USBTLL_REV3 0x00000004 /* OMAP4 */
  85. #define is_ehci_tll_mode(x) (x == OMAP_EHCI_PORT_MODE_TLL)
  86. struct usbtll_omap {
  87. struct clk *usbtll_p1_fck;
  88. struct clk *usbtll_p2_fck;
  89. struct usbhs_omap_platform_data *pdata;
  90. /* secure the register updates */
  91. spinlock_t lock;
  92. };
  93. /*-------------------------------------------------------------------------*/
  94. const char usbtll_driver_name[] = USBTLL_DRIVER_NAME;
  95. struct platform_device *tll_pdev;
  96. /*-------------------------------------------------------------------------*/
  97. static inline void usbtll_write(void __iomem *base, u32 reg, u32 val)
  98. {
  99. __raw_writel(val, base + reg);
  100. }
  101. static inline u32 usbtll_read(void __iomem *base, u32 reg)
  102. {
  103. return __raw_readl(base + reg);
  104. }
  105. static inline void usbtll_writeb(void __iomem *base, u8 reg, u8 val)
  106. {
  107. __raw_writeb(val, base + reg);
  108. }
  109. static inline u8 usbtll_readb(void __iomem *base, u8 reg)
  110. {
  111. return __raw_readb(base + reg);
  112. }
  113. /*-------------------------------------------------------------------------*/
  114. static bool is_ohci_port(enum usbhs_omap_port_mode pmode)
  115. {
  116. switch (pmode) {
  117. case OMAP_OHCI_PORT_MODE_PHY_6PIN_DATSE0:
  118. case OMAP_OHCI_PORT_MODE_PHY_6PIN_DPDM:
  119. case OMAP_OHCI_PORT_MODE_PHY_3PIN_DATSE0:
  120. case OMAP_OHCI_PORT_MODE_PHY_4PIN_DPDM:
  121. case OMAP_OHCI_PORT_MODE_TLL_6PIN_DATSE0:
  122. case OMAP_OHCI_PORT_MODE_TLL_6PIN_DPDM:
  123. case OMAP_OHCI_PORT_MODE_TLL_3PIN_DATSE0:
  124. case OMAP_OHCI_PORT_MODE_TLL_4PIN_DPDM:
  125. case OMAP_OHCI_PORT_MODE_TLL_2PIN_DATSE0:
  126. case OMAP_OHCI_PORT_MODE_TLL_2PIN_DPDM:
  127. return true;
  128. default:
  129. return false;
  130. }
  131. }
  132. /*
  133. * convert the port-mode enum to a value we can use in the FSLSMODE
  134. * field of USBTLL_CHANNEL_CONF
  135. */
  136. static unsigned ohci_omap3_fslsmode(enum usbhs_omap_port_mode mode)
  137. {
  138. switch (mode) {
  139. case OMAP_USBHS_PORT_MODE_UNUSED:
  140. case OMAP_OHCI_PORT_MODE_PHY_6PIN_DATSE0:
  141. return OMAP_TLL_FSLSMODE_6PIN_PHY_DAT_SE0;
  142. case OMAP_OHCI_PORT_MODE_PHY_6PIN_DPDM:
  143. return OMAP_TLL_FSLSMODE_6PIN_PHY_DP_DM;
  144. case OMAP_OHCI_PORT_MODE_PHY_3PIN_DATSE0:
  145. return OMAP_TLL_FSLSMODE_3PIN_PHY;
  146. case OMAP_OHCI_PORT_MODE_PHY_4PIN_DPDM:
  147. return OMAP_TLL_FSLSMODE_4PIN_PHY;
  148. case OMAP_OHCI_PORT_MODE_TLL_6PIN_DATSE0:
  149. return OMAP_TLL_FSLSMODE_6PIN_TLL_DAT_SE0;
  150. case OMAP_OHCI_PORT_MODE_TLL_6PIN_DPDM:
  151. return OMAP_TLL_FSLSMODE_6PIN_TLL_DP_DM;
  152. case OMAP_OHCI_PORT_MODE_TLL_3PIN_DATSE0:
  153. return OMAP_TLL_FSLSMODE_3PIN_TLL;
  154. case OMAP_OHCI_PORT_MODE_TLL_4PIN_DPDM:
  155. return OMAP_TLL_FSLSMODE_4PIN_TLL;
  156. case OMAP_OHCI_PORT_MODE_TLL_2PIN_DATSE0:
  157. return OMAP_TLL_FSLSMODE_2PIN_TLL_DAT_SE0;
  158. case OMAP_OHCI_PORT_MODE_TLL_2PIN_DPDM:
  159. return OMAP_TLL_FSLSMODE_2PIN_DAT_DP_DM;
  160. default:
  161. pr_warn("Invalid port mode, using default\n");
  162. return OMAP_TLL_FSLSMODE_6PIN_PHY_DAT_SE0;
  163. }
  164. }
  165. /**
  166. * usbtll_omap_probe - initialize TI-based HCDs
  167. *
  168. * Allocates basic resources for this USB host controller.
  169. */
  170. static int usbtll_omap_probe(struct platform_device *pdev)
  171. {
  172. struct device *dev = &pdev->dev;
  173. struct usbhs_omap_platform_data *pdata = dev->platform_data;
  174. void __iomem *base;
  175. struct resource *res;
  176. struct usbtll_omap *tll;
  177. unsigned reg;
  178. unsigned long flags;
  179. int ret = 0;
  180. int i, ver, count;
  181. dev_dbg(dev, "starting TI HSUSB TLL Controller\n");
  182. tll = kzalloc(sizeof(struct usbtll_omap), GFP_KERNEL);
  183. if (!tll) {
  184. dev_err(dev, "Memory allocation failed\n");
  185. ret = -ENOMEM;
  186. goto end;
  187. }
  188. spin_lock_init(&tll->lock);
  189. tll->pdata = pdata;
  190. tll->usbtll_p1_fck = clk_get(dev, "usb_tll_hs_usb_ch0_clk");
  191. if (IS_ERR(tll->usbtll_p1_fck)) {
  192. ret = PTR_ERR(tll->usbtll_p1_fck);
  193. dev_err(dev, "usbtll_p1_fck failed error:%d\n", ret);
  194. goto err_tll;
  195. }
  196. tll->usbtll_p2_fck = clk_get(dev, "usb_tll_hs_usb_ch1_clk");
  197. if (IS_ERR(tll->usbtll_p2_fck)) {
  198. ret = PTR_ERR(tll->usbtll_p2_fck);
  199. dev_err(dev, "usbtll_p2_fck failed error:%d\n", ret);
  200. goto err_usbtll_p1_fck;
  201. }
  202. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  203. if (!res) {
  204. dev_err(dev, "usb tll get resource failed\n");
  205. ret = -ENODEV;
  206. goto err_usbtll_p2_fck;
  207. }
  208. base = ioremap(res->start, resource_size(res));
  209. if (!base) {
  210. dev_err(dev, "TLL ioremap failed\n");
  211. ret = -ENOMEM;
  212. goto err_usbtll_p2_fck;
  213. }
  214. platform_set_drvdata(pdev, tll);
  215. pm_runtime_enable(dev);
  216. pm_runtime_get_sync(dev);
  217. spin_lock_irqsave(&tll->lock, flags);
  218. ver = usbtll_read(base, OMAP_USBTLL_REVISION);
  219. switch (ver) {
  220. case OMAP_USBTLL_REV1:
  221. case OMAP_USBTLL_REV2:
  222. count = OMAP_TLL_CHANNEL_COUNT;
  223. break;
  224. case OMAP_USBTLL_REV3:
  225. count = OMAP_REV2_TLL_CHANNEL_COUNT;
  226. break;
  227. default:
  228. dev_err(dev, "TLL version failed\n");
  229. ret = -ENODEV;
  230. goto err_ioremap;
  231. }
  232. if (is_ehci_tll_mode(pdata->port_mode[0]) ||
  233. is_ehci_tll_mode(pdata->port_mode[1]) ||
  234. is_ehci_tll_mode(pdata->port_mode[2]) ||
  235. is_ohci_port(pdata->port_mode[0]) ||
  236. is_ohci_port(pdata->port_mode[1]) ||
  237. is_ohci_port(pdata->port_mode[2])) {
  238. /* Program Common TLL register */
  239. reg = usbtll_read(base, OMAP_TLL_SHARED_CONF);
  240. reg |= (OMAP_TLL_SHARED_CONF_FCLK_IS_ON
  241. | OMAP_TLL_SHARED_CONF_USB_DIVRATION);
  242. reg &= ~OMAP_TLL_SHARED_CONF_USB_90D_DDR_EN;
  243. reg &= ~OMAP_TLL_SHARED_CONF_USB_180D_SDR_EN;
  244. usbtll_write(base, OMAP_TLL_SHARED_CONF, reg);
  245. /* Enable channels now */
  246. for (i = 0; i < count; i++) {
  247. reg = usbtll_read(base, OMAP_TLL_CHANNEL_CONF(i));
  248. if (is_ohci_port(pdata->port_mode[i])) {
  249. reg |= ohci_omap3_fslsmode(pdata->port_mode[i])
  250. << OMAP_TLL_CHANNEL_CONF_FSLSMODE_SHIFT;
  251. reg |= OMAP_TLL_CHANNEL_CONF_CHANMODE_FSLS;
  252. } else if (pdata->port_mode[i] ==
  253. OMAP_EHCI_PORT_MODE_TLL) {
  254. /*
  255. * Disable AutoIdle, BitStuffing
  256. * and use SDR Mode
  257. */
  258. reg &= ~(OMAP_TLL_CHANNEL_CONF_UTMIAUTOIDLE
  259. | OMAP_TLL_CHANNEL_CONF_ULPINOBITSTUFF
  260. | OMAP_TLL_CHANNEL_CONF_ULPIDDRMODE);
  261. } else {
  262. continue;
  263. }
  264. reg |= OMAP_TLL_CHANNEL_CONF_CHANEN;
  265. usbtll_write(base, OMAP_TLL_CHANNEL_CONF(i), reg);
  266. usbtll_writeb(base,
  267. OMAP_TLL_ULPI_SCRATCH_REGISTER(i),
  268. 0xbe);
  269. }
  270. }
  271. err_ioremap:
  272. spin_unlock_irqrestore(&tll->lock, flags);
  273. iounmap(base);
  274. pm_runtime_put_sync(dev);
  275. tll_pdev = pdev;
  276. if (!ret)
  277. goto end;
  278. pm_runtime_disable(dev);
  279. err_usbtll_p2_fck:
  280. clk_put(tll->usbtll_p2_fck);
  281. err_usbtll_p1_fck:
  282. clk_put(tll->usbtll_p1_fck);
  283. err_tll:
  284. kfree(tll);
  285. end:
  286. return ret;
  287. }
  288. /**
  289. * usbtll_omap_remove - shutdown processing for UHH & TLL HCDs
  290. * @pdev: USB Host Controller being removed
  291. *
  292. * Reverses the effect of usbtll_omap_probe().
  293. */
  294. static int usbtll_omap_remove(struct platform_device *pdev)
  295. {
  296. struct usbtll_omap *tll = platform_get_drvdata(pdev);
  297. clk_put(tll->usbtll_p2_fck);
  298. clk_put(tll->usbtll_p1_fck);
  299. pm_runtime_disable(&pdev->dev);
  300. kfree(tll);
  301. return 0;
  302. }
  303. static int usbtll_runtime_resume(struct device *dev)
  304. {
  305. struct usbtll_omap *tll = dev_get_drvdata(dev);
  306. struct usbhs_omap_platform_data *pdata = tll->pdata;
  307. unsigned long flags;
  308. dev_dbg(dev, "usbtll_runtime_resume\n");
  309. if (!pdata) {
  310. dev_dbg(dev, "missing platform_data\n");
  311. return -ENODEV;
  312. }
  313. spin_lock_irqsave(&tll->lock, flags);
  314. if (is_ehci_tll_mode(pdata->port_mode[0]))
  315. clk_enable(tll->usbtll_p1_fck);
  316. if (is_ehci_tll_mode(pdata->port_mode[1]))
  317. clk_enable(tll->usbtll_p2_fck);
  318. spin_unlock_irqrestore(&tll->lock, flags);
  319. return 0;
  320. }
  321. static int usbtll_runtime_suspend(struct device *dev)
  322. {
  323. struct usbtll_omap *tll = dev_get_drvdata(dev);
  324. struct usbhs_omap_platform_data *pdata = tll->pdata;
  325. unsigned long flags;
  326. dev_dbg(dev, "usbtll_runtime_suspend\n");
  327. if (!pdata) {
  328. dev_dbg(dev, "missing platform_data\n");
  329. return -ENODEV;
  330. }
  331. spin_lock_irqsave(&tll->lock, flags);
  332. if (is_ehci_tll_mode(pdata->port_mode[0]))
  333. clk_disable(tll->usbtll_p1_fck);
  334. if (is_ehci_tll_mode(pdata->port_mode[1]))
  335. clk_disable(tll->usbtll_p2_fck);
  336. spin_unlock_irqrestore(&tll->lock, flags);
  337. return 0;
  338. }
  339. static const struct dev_pm_ops usbtllomap_dev_pm_ops = {
  340. SET_RUNTIME_PM_OPS(usbtll_runtime_suspend,
  341. usbtll_runtime_resume,
  342. NULL)
  343. };
  344. static struct platform_driver usbtll_omap_driver = {
  345. .driver = {
  346. .name = (char *)usbtll_driver_name,
  347. .owner = THIS_MODULE,
  348. .pm = &usbtllomap_dev_pm_ops,
  349. },
  350. .probe = usbtll_omap_probe,
  351. .remove = usbtll_omap_remove,
  352. };
  353. int omap_tll_enable(void)
  354. {
  355. if (!tll_pdev) {
  356. pr_err("missing omap usbhs tll platform_data\n");
  357. return -ENODEV;
  358. }
  359. return pm_runtime_get_sync(&tll_pdev->dev);
  360. }
  361. EXPORT_SYMBOL_GPL(omap_tll_enable);
  362. int omap_tll_disable(void)
  363. {
  364. if (!tll_pdev) {
  365. pr_err("missing omap usbhs tll platform_data\n");
  366. return -ENODEV;
  367. }
  368. return pm_runtime_put_sync(&tll_pdev->dev);
  369. }
  370. EXPORT_SYMBOL_GPL(omap_tll_disable);
  371. MODULE_AUTHOR("Keshava Munegowda <keshava_mgowda@ti.com>");
  372. MODULE_ALIAS("platform:" USBHS_DRIVER_NAME);
  373. MODULE_LICENSE("GPL v2");
  374. MODULE_DESCRIPTION("usb tll driver for TI OMAP EHCI and OHCI controllers");
  375. static int __init omap_usbtll_drvinit(void)
  376. {
  377. return platform_driver_register(&usbtll_omap_driver);
  378. }
  379. /*
  380. * init before usbhs core driver;
  381. * The usbtll driver should be initialized before
  382. * the usbhs core driver probe function is called.
  383. */
  384. fs_initcall(omap_usbtll_drvinit);
  385. static void __exit omap_usbtll_drvexit(void)
  386. {
  387. platform_driver_unregister(&usbtll_omap_driver);
  388. }
  389. module_exit(omap_usbtll_drvexit);