omap-usb-tll.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. __raw_writel(val, base + reg);
  108. }
  109. static inline u32 usbtll_read(void __iomem *base, u32 reg)
  110. {
  111. return __raw_readl(base + reg);
  112. }
  113. static inline void usbtll_writeb(void __iomem *base, u8 reg, u8 val)
  114. {
  115. __raw_writeb(val, base + reg);
  116. }
  117. static inline u8 usbtll_readb(void __iomem *base, u8 reg)
  118. {
  119. return __raw_readb(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_request_and_ioremap(dev, res);
  193. if (!tll->base) {
  194. ret = -EADDRNOTAVAIL;
  195. dev_err(dev, "Resource request/ioremap failed:%d\n", ret);
  196. return ret;
  197. }
  198. platform_set_drvdata(pdev, tll);
  199. pm_runtime_enable(dev);
  200. pm_runtime_get_sync(dev);
  201. ver = usbtll_read(tll->base, OMAP_USBTLL_REVISION);
  202. switch (ver) {
  203. case OMAP_USBTLL_REV1:
  204. case OMAP_USBTLL_REV4:
  205. tll->nch = OMAP_TLL_CHANNEL_COUNT;
  206. break;
  207. case OMAP_USBTLL_REV2:
  208. case OMAP_USBTLL_REV3:
  209. tll->nch = OMAP_REV2_TLL_CHANNEL_COUNT;
  210. break;
  211. default:
  212. tll->nch = OMAP_TLL_CHANNEL_COUNT;
  213. dev_dbg(dev,
  214. "USB TLL Rev : 0x%x not recognized, assuming %d channels\n",
  215. ver, tll->nch);
  216. break;
  217. }
  218. tll->ch_clk = devm_kzalloc(dev, sizeof(struct clk * [tll->nch]),
  219. GFP_KERNEL);
  220. if (!tll->ch_clk) {
  221. ret = -ENOMEM;
  222. dev_err(dev, "Couldn't allocate memory for channel clocks\n");
  223. goto err_clk_alloc;
  224. }
  225. for (i = 0; i < tll->nch; i++) {
  226. char clkname[] = "usb_tll_hs_usb_chx_clk";
  227. snprintf(clkname, sizeof(clkname),
  228. "usb_tll_hs_usb_ch%d_clk", i);
  229. tll->ch_clk[i] = clk_get(dev, clkname);
  230. if (IS_ERR(tll->ch_clk[i]))
  231. dev_dbg(dev, "can't get clock : %s\n", clkname);
  232. }
  233. pm_runtime_put_sync(dev);
  234. /* only after this can omap_tll_enable/disable work */
  235. spin_lock(&tll_lock);
  236. tll_dev = dev;
  237. spin_unlock(&tll_lock);
  238. return 0;
  239. err_clk_alloc:
  240. pm_runtime_put_sync(dev);
  241. pm_runtime_disable(dev);
  242. return ret;
  243. }
  244. /**
  245. * usbtll_omap_remove - shutdown processing for UHH & TLL HCDs
  246. * @pdev: USB Host Controller being removed
  247. *
  248. * Reverses the effect of usbtll_omap_probe().
  249. */
  250. static int usbtll_omap_remove(struct platform_device *pdev)
  251. {
  252. struct usbtll_omap *tll = platform_get_drvdata(pdev);
  253. int i;
  254. spin_lock(&tll_lock);
  255. tll_dev = NULL;
  256. spin_unlock(&tll_lock);
  257. for (i = 0; i < tll->nch; i++)
  258. if (!IS_ERR(tll->ch_clk[i]))
  259. clk_put(tll->ch_clk[i]);
  260. pm_runtime_disable(&pdev->dev);
  261. return 0;
  262. }
  263. static const struct of_device_id usbtll_omap_dt_ids[] = {
  264. { .compatible = "ti,usbhs-tll" },
  265. { }
  266. };
  267. MODULE_DEVICE_TABLE(of, usbtll_omap_dt_ids);
  268. static struct platform_driver usbtll_omap_driver = {
  269. .driver = {
  270. .name = (char *)usbtll_driver_name,
  271. .owner = THIS_MODULE,
  272. .of_match_table = of_match_ptr(usbtll_omap_dt_ids),
  273. },
  274. .probe = usbtll_omap_probe,
  275. .remove = usbtll_omap_remove,
  276. };
  277. int omap_tll_init(struct usbhs_omap_platform_data *pdata)
  278. {
  279. int i;
  280. bool needs_tll;
  281. unsigned reg;
  282. struct usbtll_omap *tll;
  283. spin_lock(&tll_lock);
  284. if (!tll_dev) {
  285. spin_unlock(&tll_lock);
  286. return -ENODEV;
  287. }
  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. pm_runtime_get_sync(tll_dev);
  293. if (needs_tll) {
  294. void __iomem *base = tll->base;
  295. /* Program Common TLL register */
  296. reg = usbtll_read(base, OMAP_TLL_SHARED_CONF);
  297. reg |= (OMAP_TLL_SHARED_CONF_FCLK_IS_ON
  298. | OMAP_TLL_SHARED_CONF_USB_DIVRATION);
  299. reg &= ~OMAP_TLL_SHARED_CONF_USB_90D_DDR_EN;
  300. reg &= ~OMAP_TLL_SHARED_CONF_USB_180D_SDR_EN;
  301. usbtll_write(base, OMAP_TLL_SHARED_CONF, reg);
  302. /* Enable channels now */
  303. for (i = 0; i < tll->nch; i++) {
  304. reg = usbtll_read(base, OMAP_TLL_CHANNEL_CONF(i));
  305. if (is_ohci_port(pdata->port_mode[i])) {
  306. reg |= ohci_omap3_fslsmode(pdata->port_mode[i])
  307. << OMAP_TLL_CHANNEL_CONF_FSLSMODE_SHIFT;
  308. reg |= OMAP_TLL_CHANNEL_CONF_CHANMODE_FSLS;
  309. } else if (pdata->port_mode[i] ==
  310. OMAP_EHCI_PORT_MODE_TLL) {
  311. /*
  312. * Disable AutoIdle, BitStuffing
  313. * and use SDR Mode
  314. */
  315. reg &= ~(OMAP_TLL_CHANNEL_CONF_UTMIAUTOIDLE
  316. | OMAP_TLL_CHANNEL_CONF_ULPINOBITSTUFF
  317. | OMAP_TLL_CHANNEL_CONF_ULPIDDRMODE);
  318. } else if (pdata->port_mode[i] ==
  319. OMAP_EHCI_PORT_MODE_HSIC) {
  320. /*
  321. * HSIC Mode requires UTMI port configurations
  322. */
  323. reg |= OMAP_TLL_CHANNEL_CONF_DRVVBUS
  324. | OMAP_TLL_CHANNEL_CONF_CHRGVBUS
  325. | OMAP_TLL_CHANNEL_CONF_MODE_TRANSPARENT_UTMI
  326. | OMAP_TLL_CHANNEL_CONF_ULPINOBITSTUFF;
  327. } else {
  328. continue;
  329. }
  330. reg |= OMAP_TLL_CHANNEL_CONF_CHANEN;
  331. usbtll_write(base, OMAP_TLL_CHANNEL_CONF(i), reg);
  332. usbtll_writeb(base,
  333. OMAP_TLL_ULPI_SCRATCH_REGISTER(i),
  334. 0xbe);
  335. }
  336. }
  337. pm_runtime_put_sync(tll_dev);
  338. spin_unlock(&tll_lock);
  339. return 0;
  340. }
  341. EXPORT_SYMBOL_GPL(omap_tll_init);
  342. int omap_tll_enable(struct usbhs_omap_platform_data *pdata)
  343. {
  344. int i;
  345. struct usbtll_omap *tll;
  346. spin_lock(&tll_lock);
  347. if (!tll_dev) {
  348. spin_unlock(&tll_lock);
  349. return -ENODEV;
  350. }
  351. tll = dev_get_drvdata(tll_dev);
  352. pm_runtime_get_sync(tll_dev);
  353. for (i = 0; i < tll->nch; i++) {
  354. if (omap_usb_mode_needs_tll(pdata->port_mode[i])) {
  355. int r;
  356. if (IS_ERR(tll->ch_clk[i]))
  357. continue;
  358. r = clk_enable(tll->ch_clk[i]);
  359. if (r) {
  360. dev_err(tll_dev,
  361. "Error enabling ch %d clock: %d\n", i, r);
  362. }
  363. }
  364. }
  365. spin_unlock(&tll_lock);
  366. return 0;
  367. }
  368. EXPORT_SYMBOL_GPL(omap_tll_enable);
  369. int omap_tll_disable(struct usbhs_omap_platform_data *pdata)
  370. {
  371. int i;
  372. struct usbtll_omap *tll;
  373. spin_lock(&tll_lock);
  374. if (!tll_dev) {
  375. spin_unlock(&tll_lock);
  376. return -ENODEV;
  377. }
  378. tll = dev_get_drvdata(tll_dev);
  379. for (i = 0; i < tll->nch; i++) {
  380. if (omap_usb_mode_needs_tll(pdata->port_mode[i])) {
  381. if (!IS_ERR(tll->ch_clk[i]))
  382. clk_disable(tll->ch_clk[i]);
  383. }
  384. }
  385. pm_runtime_put_sync(tll_dev);
  386. spin_unlock(&tll_lock);
  387. return 0;
  388. }
  389. EXPORT_SYMBOL_GPL(omap_tll_disable);
  390. MODULE_AUTHOR("Keshava Munegowda <keshava_mgowda@ti.com>");
  391. MODULE_AUTHOR("Roger Quadros <rogerq@ti.com>");
  392. MODULE_ALIAS("platform:" USBHS_DRIVER_NAME);
  393. MODULE_LICENSE("GPL v2");
  394. MODULE_DESCRIPTION("usb tll driver for TI OMAP EHCI and OHCI controllers");
  395. static int __init omap_usbtll_drvinit(void)
  396. {
  397. return platform_driver_register(&usbtll_omap_driver);
  398. }
  399. /*
  400. * init before usbhs core driver;
  401. * The usbtll driver should be initialized before
  402. * the usbhs core driver probe function is called.
  403. */
  404. fs_initcall(omap_usbtll_drvinit);
  405. static void __exit omap_usbtll_drvexit(void)
  406. {
  407. platform_driver_unregister(&usbtll_omap_driver);
  408. }
  409. module_exit(omap_usbtll_drvexit);