ti_usb_phy.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /**
  3. * ti_usb_phy.c - USB3 and USB3 PHY programming for dwc3
  4. *
  5. * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com
  6. *
  7. * Author: Kishon Vijay Abraham I <kishon@ti.com>
  8. *
  9. * Taken from Linux Kernel v3.16 (drivers/phy/phy-ti-pipe3.c and
  10. * drivers/phy/phy-omap-usb2.c) and ported to uboot.
  11. *
  12. * "commit 56042e : phy: ti-pipe3: Fix suspend/resume and module reload" for
  13. * phy-ti-pipe3.c
  14. *
  15. * "commit eb82a3 : phy: omap-usb2: Balance pm_runtime_enable() on probe failure
  16. * and remove" for phy-omap-usb2.c
  17. */
  18. #include <common.h>
  19. #include <malloc.h>
  20. #include <ti-usb-phy-uboot.h>
  21. #include <usb/lin_gadget_compat.h>
  22. #include <linux/ioport.h>
  23. #include <asm/io.h>
  24. #include <asm/arch/sys_proto.h>
  25. #include <dm.h>
  26. #include "linux-compat.h"
  27. #define PLL_STATUS 0x00000004
  28. #define PLL_GO 0x00000008
  29. #define PLL_CONFIGURATION1 0x0000000C
  30. #define PLL_CONFIGURATION2 0x00000010
  31. #define PLL_CONFIGURATION3 0x00000014
  32. #define PLL_CONFIGURATION4 0x00000020
  33. #define PLL_REGM_MASK 0x001FFE00
  34. #define PLL_REGM_SHIFT 0x9
  35. #define PLL_REGM_F_MASK 0x0003FFFF
  36. #define PLL_REGM_F_SHIFT 0x0
  37. #define PLL_REGN_MASK 0x000001FE
  38. #define PLL_REGN_SHIFT 0x1
  39. #define PLL_SELFREQDCO_MASK 0x0000000E
  40. #define PLL_SELFREQDCO_SHIFT 0x1
  41. #define PLL_SD_MASK 0x0003FC00
  42. #define PLL_SD_SHIFT 10
  43. #define SET_PLL_GO 0x1
  44. #define PLL_LDOPWDN BIT(15)
  45. #define PLL_TICOPWDN BIT(16)
  46. #define PLL_LOCK 0x2
  47. #define PLL_IDLE 0x1
  48. #define OMAP_CTRL_DEV_PHY_PD BIT(0)
  49. #define OMAP_CTRL_USB3_PHY_PWRCTL_CLK_CMD_MASK 0x003FC000
  50. #define OMAP_CTRL_USB3_PHY_PWRCTL_CLK_CMD_SHIFT 0xE
  51. #define OMAP_CTRL_USB3_PHY_PWRCTL_CLK_FREQ_MASK 0xFFC00000
  52. #define OMAP_CTRL_USB3_PHY_PWRCTL_CLK_FREQ_SHIFT 0x16
  53. #define OMAP_CTRL_USB3_PHY_TX_RX_POWERON 0x3
  54. #define OMAP_CTRL_USB3_PHY_TX_RX_POWEROFF 0x0
  55. #define OMAP_CTRL_USB2_PHY_PD BIT(28)
  56. #define AM437X_CTRL_USB2_PHY_PD BIT(0)
  57. #define AM437X_CTRL_USB2_OTG_PD BIT(1)
  58. #define AM437X_CTRL_USB2_OTGVDET_EN BIT(19)
  59. #define AM437X_CTRL_USB2_OTGSESSEND_EN BIT(20)
  60. static LIST_HEAD(ti_usb_phy_list);
  61. typedef unsigned int u32;
  62. struct usb3_dpll_params {
  63. u16 m;
  64. u8 n;
  65. u8 freq:3;
  66. u8 sd;
  67. u32 mf;
  68. };
  69. struct usb3_dpll_map {
  70. unsigned long rate;
  71. struct usb3_dpll_params params;
  72. struct usb3_dpll_map *dpll_map;
  73. };
  74. struct ti_usb_phy {
  75. void __iomem *pll_ctrl_base;
  76. void __iomem *usb2_phy_power;
  77. void __iomem *usb3_phy_power;
  78. struct usb3_dpll_map *dpll_map;
  79. struct list_head list;
  80. int index;
  81. };
  82. static struct usb3_dpll_map dpll_map_usb[] = {
  83. {12000000, {1250, 5, 4, 20, 0} }, /* 12 MHz */
  84. {16800000, {3125, 20, 4, 20, 0} }, /* 16.8 MHz */
  85. {19200000, {1172, 8, 4, 20, 65537} }, /* 19.2 MHz */
  86. {20000000, {1000, 7, 4, 10, 0} }, /* 20 MHz */
  87. {26000000, {1250, 12, 4, 20, 0} }, /* 26 MHz */
  88. {38400000, {3125, 47, 4, 20, 92843} }, /* 38.4 MHz */
  89. { }, /* Terminator */
  90. };
  91. static inline unsigned int ti_usb3_readl(void __iomem *base, u32 offset)
  92. {
  93. return readl(base + offset);
  94. }
  95. static inline void ti_usb3_writel(void __iomem *base, u32 offset, u32 value)
  96. {
  97. writel(value, base + offset);
  98. }
  99. #ifndef CONFIG_AM43XX
  100. static struct usb3_dpll_params *ti_usb3_get_dpll_params(struct ti_usb_phy *phy)
  101. {
  102. unsigned long rate;
  103. struct usb3_dpll_map *dpll_map = phy->dpll_map;
  104. rate = get_sys_clk_freq();
  105. for (; dpll_map->rate; dpll_map++) {
  106. if (rate == dpll_map->rate)
  107. return &dpll_map->params;
  108. }
  109. dev_err(phy->dev, "No DPLL configuration for %lu Hz SYS CLK\n", rate);
  110. return NULL;
  111. }
  112. static int ti_usb3_dpll_wait_lock(struct ti_usb_phy *phy)
  113. {
  114. u32 val;
  115. do {
  116. val = ti_usb3_readl(phy->pll_ctrl_base, PLL_STATUS);
  117. if (val & PLL_LOCK)
  118. break;
  119. } while (1);
  120. return 0;
  121. }
  122. static int ti_usb3_dpll_program(struct ti_usb_phy *phy)
  123. {
  124. u32 val;
  125. struct usb3_dpll_params *dpll_params;
  126. if (!phy->pll_ctrl_base)
  127. return -EINVAL;
  128. dpll_params = ti_usb3_get_dpll_params(phy);
  129. if (!dpll_params)
  130. return -EINVAL;
  131. val = ti_usb3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION1);
  132. val &= ~PLL_REGN_MASK;
  133. val |= dpll_params->n << PLL_REGN_SHIFT;
  134. ti_usb3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION1, val);
  135. val = ti_usb3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION2);
  136. val &= ~PLL_SELFREQDCO_MASK;
  137. val |= dpll_params->freq << PLL_SELFREQDCO_SHIFT;
  138. ti_usb3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION2, val);
  139. val = ti_usb3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION1);
  140. val &= ~PLL_REGM_MASK;
  141. val |= dpll_params->m << PLL_REGM_SHIFT;
  142. ti_usb3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION1, val);
  143. val = ti_usb3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION4);
  144. val &= ~PLL_REGM_F_MASK;
  145. val |= dpll_params->mf << PLL_REGM_F_SHIFT;
  146. ti_usb3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION4, val);
  147. val = ti_usb3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION3);
  148. val &= ~PLL_SD_MASK;
  149. val |= dpll_params->sd << PLL_SD_SHIFT;
  150. ti_usb3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION3, val);
  151. ti_usb3_writel(phy->pll_ctrl_base, PLL_GO, SET_PLL_GO);
  152. return ti_usb3_dpll_wait_lock(phy);
  153. }
  154. #endif
  155. void ti_usb2_phy_power(struct ti_usb_phy *phy, int on)
  156. {
  157. u32 val;
  158. val = readl(phy->usb2_phy_power);
  159. if (on) {
  160. #if defined(CONFIG_DRA7XX)
  161. if (phy->index == 1)
  162. val &= ~OMAP_CTRL_USB2_PHY_PD;
  163. else
  164. val &= ~OMAP_CTRL_DEV_PHY_PD;
  165. #elif defined(CONFIG_AM43XX)
  166. val &= ~(AM437X_CTRL_USB2_PHY_PD |
  167. AM437X_CTRL_USB2_OTG_PD);
  168. val |= (AM437X_CTRL_USB2_OTGVDET_EN |
  169. AM437X_CTRL_USB2_OTGSESSEND_EN);
  170. #endif
  171. } else {
  172. #if defined(CONFIG_DRA7XX)
  173. if (phy->index == 1)
  174. val |= OMAP_CTRL_USB2_PHY_PD;
  175. else
  176. val |= OMAP_CTRL_DEV_PHY_PD;
  177. #elif defined(CONFIG_AM43XX)
  178. val &= ~(AM437X_CTRL_USB2_OTGVDET_EN |
  179. AM437X_CTRL_USB2_OTGSESSEND_EN);
  180. val |= (AM437X_CTRL_USB2_PHY_PD |
  181. AM437X_CTRL_USB2_OTG_PD);
  182. #endif
  183. }
  184. writel(val, phy->usb2_phy_power);
  185. }
  186. #ifndef CONFIG_AM43XX
  187. void ti_usb3_phy_power(struct ti_usb_phy *phy, int on)
  188. {
  189. u32 val;
  190. u32 rate;
  191. rate = get_sys_clk_freq();
  192. rate = rate/1000000;
  193. if (!phy->usb3_phy_power)
  194. return;
  195. val = readl(phy->usb3_phy_power);
  196. if (on) {
  197. val &= ~(OMAP_CTRL_USB3_PHY_PWRCTL_CLK_CMD_MASK |
  198. OMAP_CTRL_USB3_PHY_PWRCTL_CLK_FREQ_MASK);
  199. val |= (OMAP_CTRL_USB3_PHY_TX_RX_POWERON) <<
  200. OMAP_CTRL_USB3_PHY_PWRCTL_CLK_CMD_SHIFT;
  201. val |= rate <<
  202. OMAP_CTRL_USB3_PHY_PWRCTL_CLK_FREQ_SHIFT;
  203. } else {
  204. val &= ~OMAP_CTRL_USB3_PHY_PWRCTL_CLK_CMD_MASK;
  205. val |= OMAP_CTRL_USB3_PHY_TX_RX_POWEROFF <<
  206. OMAP_CTRL_USB3_PHY_PWRCTL_CLK_CMD_SHIFT;
  207. }
  208. writel(val, phy->usb3_phy_power);
  209. }
  210. #endif
  211. /**
  212. * ti_usb_phy_uboot_init - usb phy uboot initialization code
  213. * @dev: struct ti_usb_phy_device containing initialization data
  214. *
  215. * Entry point for ti usb phy driver. This driver handles initialization
  216. * of both usb2 phy and usb3 phy. Pointer to ti_usb_phy_device should be
  217. * passed containing base address and other initialization data.
  218. * Returns '0' on success and a negative value on failure.
  219. *
  220. * Generally called from board_usb_init() implemented in board file.
  221. */
  222. int ti_usb_phy_uboot_init(struct ti_usb_phy_device *dev)
  223. {
  224. struct ti_usb_phy *phy;
  225. phy = devm_kzalloc(NULL, sizeof(*phy), GFP_KERNEL);
  226. if (!phy) {
  227. dev_err(NULL, "unable to alloc mem for TI USB3 PHY\n");
  228. return -ENOMEM;
  229. }
  230. phy->dpll_map = dpll_map_usb;
  231. phy->index = dev->index;
  232. phy->pll_ctrl_base = dev->pll_ctrl_base;
  233. phy->usb2_phy_power = dev->usb2_phy_power;
  234. phy->usb3_phy_power = dev->usb3_phy_power;
  235. #ifndef CONFIG_AM43XX
  236. ti_usb3_dpll_program(phy);
  237. ti_usb3_phy_power(phy, 1);
  238. #endif
  239. ti_usb2_phy_power(phy, 1);
  240. mdelay(150);
  241. list_add_tail(&phy->list, &ti_usb_phy_list);
  242. return 0;
  243. }
  244. /**
  245. * ti_usb_phy_uboot_exit - usb phy uboot cleanup code
  246. * @index: index of this controller
  247. *
  248. * Performs cleanup of memory allocated in ti_usb_phy_uboot_init.
  249. * index of _this_ controller should be passed and should match with
  250. * the index passed in ti_usb_phy_device during init.
  251. *
  252. * Generally called from board file.
  253. */
  254. void ti_usb_phy_uboot_exit(int index)
  255. {
  256. struct ti_usb_phy *phy = NULL;
  257. list_for_each_entry(phy, &ti_usb_phy_list, list) {
  258. if (phy->index != index)
  259. continue;
  260. ti_usb2_phy_power(phy, 0);
  261. #ifndef CONFIG_AM43XX
  262. ti_usb3_phy_power(phy, 0);
  263. #endif
  264. list_del(&phy->list);
  265. kfree(phy);
  266. break;
  267. }
  268. }