omap-usb-tll.c 13 KB

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