omap-usb-tll.c 13 KB

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