phy-bcm-ns2-usbdrd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // Copyright (C) 2017 Broadcom
  3. #include <linux/delay.h>
  4. #include <linux/extcon-provider.h>
  5. #include <linux/gpio.h>
  6. #include <linux/gpio/consumer.h>
  7. #include <linux/init.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/io.h>
  10. #include <linux/iopoll.h>
  11. #include <linux/irq.h>
  12. #include <linux/mfd/syscon.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/of_address.h>
  16. #include <linux/phy/phy.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/regmap.h>
  19. #include <linux/slab.h>
  20. #include <linux/workqueue.h>
  21. #define ICFG_DRD_AFE 0x0
  22. #define ICFG_MISC_STAT 0x18
  23. #define ICFG_DRD_P0CTL 0x1C
  24. #define ICFG_STRAP_CTRL 0x20
  25. #define ICFG_FSM_CTRL 0x24
  26. #define ICFG_DEV_BIT BIT(2)
  27. #define IDM_RST_BIT BIT(0)
  28. #define AFE_CORERDY_VDDC BIT(18)
  29. #define PHY_PLL_RESETB BIT(15)
  30. #define PHY_RESETB BIT(14)
  31. #define PHY_PLL_LOCK BIT(0)
  32. #define DRD_DEV_MODE BIT(20)
  33. #define OHCI_OVRCUR_POL BIT(11)
  34. #define ICFG_OFF_MODE BIT(6)
  35. #define PLL_LOCK_RETRY 1000
  36. #define EVT_DEVICE 0
  37. #define EVT_HOST 1
  38. #define DRD_HOST_MODE (BIT(2) | BIT(3))
  39. #define DRD_DEVICE_MODE (BIT(4) | BIT(5))
  40. #define DRD_HOST_VAL 0x803
  41. #define DRD_DEV_VAL 0x807
  42. #define GPIO_DELAY 20
  43. struct ns2_phy_data;
  44. struct ns2_phy_driver {
  45. void __iomem *icfgdrd_regs;
  46. void __iomem *idmdrd_rst_ctrl;
  47. void __iomem *crmu_usb2_ctrl;
  48. void __iomem *usb2h_strap_reg;
  49. struct ns2_phy_data *data;
  50. struct extcon_dev *edev;
  51. struct gpio_desc *vbus_gpiod;
  52. struct gpio_desc *id_gpiod;
  53. int id_irq;
  54. int vbus_irq;
  55. unsigned long debounce_jiffies;
  56. struct delayed_work wq_extcon;
  57. };
  58. struct ns2_phy_data {
  59. struct ns2_phy_driver *driver;
  60. struct phy *phy;
  61. int new_state;
  62. };
  63. static const unsigned int usb_extcon_cable[] = {
  64. EXTCON_USB,
  65. EXTCON_USB_HOST,
  66. EXTCON_NONE,
  67. };
  68. static inline int pll_lock_stat(u32 usb_reg, int reg_mask,
  69. struct ns2_phy_driver *driver)
  70. {
  71. u32 val;
  72. return readl_poll_timeout_atomic(driver->icfgdrd_regs + usb_reg,
  73. val, (val & reg_mask), 1,
  74. PLL_LOCK_RETRY);
  75. }
  76. static int ns2_drd_phy_init(struct phy *phy)
  77. {
  78. struct ns2_phy_data *data = phy_get_drvdata(phy);
  79. struct ns2_phy_driver *driver = data->driver;
  80. u32 val;
  81. val = readl(driver->icfgdrd_regs + ICFG_FSM_CTRL);
  82. if (data->new_state == EVT_HOST) {
  83. val &= ~DRD_DEVICE_MODE;
  84. val |= DRD_HOST_MODE;
  85. } else {
  86. val &= ~DRD_HOST_MODE;
  87. val |= DRD_DEVICE_MODE;
  88. }
  89. writel(val, driver->icfgdrd_regs + ICFG_FSM_CTRL);
  90. return 0;
  91. }
  92. static int ns2_drd_phy_poweroff(struct phy *phy)
  93. {
  94. struct ns2_phy_data *data = phy_get_drvdata(phy);
  95. struct ns2_phy_driver *driver = data->driver;
  96. u32 val;
  97. val = readl(driver->crmu_usb2_ctrl);
  98. val &= ~AFE_CORERDY_VDDC;
  99. writel(val, driver->crmu_usb2_ctrl);
  100. val = readl(driver->crmu_usb2_ctrl);
  101. val &= ~DRD_DEV_MODE;
  102. writel(val, driver->crmu_usb2_ctrl);
  103. /* Disable Host and Device Mode */
  104. val = readl(driver->icfgdrd_regs + ICFG_FSM_CTRL);
  105. val &= ~(DRD_HOST_MODE | DRD_DEVICE_MODE | ICFG_OFF_MODE);
  106. writel(val, driver->icfgdrd_regs + ICFG_FSM_CTRL);
  107. return 0;
  108. }
  109. static int ns2_drd_phy_poweron(struct phy *phy)
  110. {
  111. struct ns2_phy_data *data = phy_get_drvdata(phy);
  112. struct ns2_phy_driver *driver = data->driver;
  113. u32 extcon_event = data->new_state;
  114. int ret;
  115. u32 val;
  116. if (extcon_event == EVT_DEVICE) {
  117. writel(DRD_DEV_VAL, driver->icfgdrd_regs + ICFG_DRD_P0CTL);
  118. val = readl(driver->idmdrd_rst_ctrl);
  119. val &= ~IDM_RST_BIT;
  120. writel(val, driver->idmdrd_rst_ctrl);
  121. val = readl(driver->crmu_usb2_ctrl);
  122. val |= (AFE_CORERDY_VDDC | DRD_DEV_MODE);
  123. writel(val, driver->crmu_usb2_ctrl);
  124. /* Bring PHY and PHY_PLL out of Reset */
  125. val = readl(driver->crmu_usb2_ctrl);
  126. val |= (PHY_PLL_RESETB | PHY_RESETB);
  127. writel(val, driver->crmu_usb2_ctrl);
  128. ret = pll_lock_stat(ICFG_MISC_STAT, PHY_PLL_LOCK, driver);
  129. if (ret < 0) {
  130. dev_err(&phy->dev, "Phy PLL lock failed\n");
  131. return ret;
  132. }
  133. } else {
  134. writel(DRD_HOST_VAL, driver->icfgdrd_regs + ICFG_DRD_P0CTL);
  135. val = readl(driver->crmu_usb2_ctrl);
  136. val |= AFE_CORERDY_VDDC;
  137. writel(val, driver->crmu_usb2_ctrl);
  138. ret = pll_lock_stat(ICFG_MISC_STAT, PHY_PLL_LOCK, driver);
  139. if (ret < 0) {
  140. dev_err(&phy->dev, "Phy PLL lock failed\n");
  141. return ret;
  142. }
  143. val = readl(driver->idmdrd_rst_ctrl);
  144. val &= ~IDM_RST_BIT;
  145. writel(val, driver->idmdrd_rst_ctrl);
  146. /* port over current Polarity */
  147. val = readl(driver->usb2h_strap_reg);
  148. val |= OHCI_OVRCUR_POL;
  149. writel(val, driver->usb2h_strap_reg);
  150. }
  151. return 0;
  152. }
  153. static void connect_change(struct ns2_phy_driver *driver)
  154. {
  155. u32 extcon_event;
  156. u32 val;
  157. extcon_event = driver->data->new_state;
  158. val = readl(driver->icfgdrd_regs + ICFG_FSM_CTRL);
  159. switch (extcon_event) {
  160. case EVT_DEVICE:
  161. val &= ~(DRD_HOST_MODE | DRD_DEVICE_MODE);
  162. writel(val, driver->icfgdrd_regs + ICFG_FSM_CTRL);
  163. val = (val & ~DRD_HOST_MODE) | DRD_DEVICE_MODE;
  164. writel(val, driver->icfgdrd_regs + ICFG_FSM_CTRL);
  165. val = readl(driver->icfgdrd_regs + ICFG_DRD_P0CTL);
  166. val |= ICFG_DEV_BIT;
  167. writel(val, driver->icfgdrd_regs + ICFG_DRD_P0CTL);
  168. break;
  169. case EVT_HOST:
  170. val &= ~(DRD_HOST_MODE | DRD_DEVICE_MODE);
  171. writel(val, driver->icfgdrd_regs + ICFG_FSM_CTRL);
  172. val = (val & ~DRD_DEVICE_MODE) | DRD_HOST_MODE;
  173. writel(val, driver->icfgdrd_regs + ICFG_FSM_CTRL);
  174. val = readl(driver->usb2h_strap_reg);
  175. val |= OHCI_OVRCUR_POL;
  176. writel(val, driver->usb2h_strap_reg);
  177. val = readl(driver->icfgdrd_regs + ICFG_DRD_P0CTL);
  178. val &= ~ICFG_DEV_BIT;
  179. writel(val, driver->icfgdrd_regs + ICFG_DRD_P0CTL);
  180. break;
  181. default:
  182. pr_err("Invalid extcon event\n");
  183. break;
  184. }
  185. }
  186. static void extcon_work(struct work_struct *work)
  187. {
  188. struct ns2_phy_driver *driver;
  189. int vbus;
  190. int id;
  191. driver = container_of(to_delayed_work(work),
  192. struct ns2_phy_driver, wq_extcon);
  193. id = gpiod_get_value_cansleep(driver->id_gpiod);
  194. vbus = gpiod_get_value_cansleep(driver->vbus_gpiod);
  195. if (!id && vbus) { /* Host connected */
  196. extcon_set_state_sync(driver->edev, EXTCON_USB_HOST, true);
  197. pr_debug("Host cable connected\n");
  198. driver->data->new_state = EVT_HOST;
  199. connect_change(driver);
  200. } else if (id && !vbus) { /* Disconnected */
  201. extcon_set_state_sync(driver->edev, EXTCON_USB_HOST, false);
  202. extcon_set_state_sync(driver->edev, EXTCON_USB, false);
  203. pr_debug("Cable disconnected\n");
  204. } else if (id && vbus) { /* Device connected */
  205. extcon_set_state_sync(driver->edev, EXTCON_USB, true);
  206. pr_debug("Device cable connected\n");
  207. driver->data->new_state = EVT_DEVICE;
  208. connect_change(driver);
  209. }
  210. }
  211. static irqreturn_t gpio_irq_handler(int irq, void *dev_id)
  212. {
  213. struct ns2_phy_driver *driver = dev_id;
  214. queue_delayed_work(system_power_efficient_wq, &driver->wq_extcon,
  215. driver->debounce_jiffies);
  216. return IRQ_HANDLED;
  217. }
  218. static const struct phy_ops ops = {
  219. .init = ns2_drd_phy_init,
  220. .power_on = ns2_drd_phy_poweron,
  221. .power_off = ns2_drd_phy_poweroff,
  222. .owner = THIS_MODULE,
  223. };
  224. static const struct of_device_id ns2_drd_phy_dt_ids[] = {
  225. { .compatible = "brcm,ns2-drd-phy", },
  226. { }
  227. };
  228. MODULE_DEVICE_TABLE(of, ns2_drd_phy_dt_ids);
  229. static int ns2_drd_phy_probe(struct platform_device *pdev)
  230. {
  231. struct phy_provider *phy_provider;
  232. struct device *dev = &pdev->dev;
  233. struct ns2_phy_driver *driver;
  234. struct ns2_phy_data *data;
  235. int ret;
  236. u32 val;
  237. driver = devm_kzalloc(dev, sizeof(struct ns2_phy_driver),
  238. GFP_KERNEL);
  239. if (!driver)
  240. return -ENOMEM;
  241. driver->data = devm_kzalloc(dev, sizeof(struct ns2_phy_data),
  242. GFP_KERNEL);
  243. if (!driver->data)
  244. return -ENOMEM;
  245. driver->icfgdrd_regs = devm_platform_ioremap_resource_byname(pdev, "icfg");
  246. if (IS_ERR(driver->icfgdrd_regs))
  247. return PTR_ERR(driver->icfgdrd_regs);
  248. driver->idmdrd_rst_ctrl = devm_platform_ioremap_resource_byname(pdev, "rst-ctrl");
  249. if (IS_ERR(driver->idmdrd_rst_ctrl))
  250. return PTR_ERR(driver->idmdrd_rst_ctrl);
  251. driver->crmu_usb2_ctrl = devm_platform_ioremap_resource_byname(pdev, "crmu-ctrl");
  252. if (IS_ERR(driver->crmu_usb2_ctrl))
  253. return PTR_ERR(driver->crmu_usb2_ctrl);
  254. driver->usb2h_strap_reg = devm_platform_ioremap_resource_byname(pdev, "usb2-strap");
  255. if (IS_ERR(driver->usb2h_strap_reg))
  256. return PTR_ERR(driver->usb2h_strap_reg);
  257. /* create extcon */
  258. driver->id_gpiod = devm_gpiod_get(&pdev->dev, "id", GPIOD_IN);
  259. if (IS_ERR(driver->id_gpiod)) {
  260. dev_err(dev, "failed to get ID GPIO\n");
  261. return PTR_ERR(driver->id_gpiod);
  262. }
  263. driver->vbus_gpiod = devm_gpiod_get(&pdev->dev, "vbus", GPIOD_IN);
  264. if (IS_ERR(driver->vbus_gpiod)) {
  265. dev_err(dev, "failed to get VBUS GPIO\n");
  266. return PTR_ERR(driver->vbus_gpiod);
  267. }
  268. driver->edev = devm_extcon_dev_allocate(dev, usb_extcon_cable);
  269. if (IS_ERR(driver->edev)) {
  270. dev_err(dev, "failed to allocate extcon device\n");
  271. return -ENOMEM;
  272. }
  273. ret = devm_extcon_dev_register(dev, driver->edev);
  274. if (ret < 0) {
  275. dev_err(dev, "failed to register extcon device\n");
  276. return ret;
  277. }
  278. ret = gpiod_set_debounce(driver->id_gpiod, GPIO_DELAY * 1000);
  279. if (ret < 0)
  280. driver->debounce_jiffies = msecs_to_jiffies(GPIO_DELAY);
  281. INIT_DELAYED_WORK(&driver->wq_extcon, extcon_work);
  282. driver->id_irq = gpiod_to_irq(driver->id_gpiod);
  283. if (driver->id_irq < 0) {
  284. dev_err(dev, "failed to get ID IRQ\n");
  285. return driver->id_irq;
  286. }
  287. driver->vbus_irq = gpiod_to_irq(driver->vbus_gpiod);
  288. if (driver->vbus_irq < 0) {
  289. dev_err(dev, "failed to get ID IRQ\n");
  290. return driver->vbus_irq;
  291. }
  292. ret = devm_request_irq(dev, driver->id_irq, gpio_irq_handler,
  293. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  294. "usb_id", driver);
  295. if (ret < 0) {
  296. dev_err(dev, "failed to request handler for ID IRQ\n");
  297. return ret;
  298. }
  299. ret = devm_request_irq(dev, driver->vbus_irq, gpio_irq_handler,
  300. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  301. "usb_vbus", driver);
  302. if (ret < 0) {
  303. dev_err(dev, "failed to request handler for VBUS IRQ\n");
  304. return ret;
  305. }
  306. dev_set_drvdata(dev, driver);
  307. /* Shutdown all ports. They can be powered up as required */
  308. val = readl(driver->crmu_usb2_ctrl);
  309. val &= ~(AFE_CORERDY_VDDC | PHY_RESETB);
  310. writel(val, driver->crmu_usb2_ctrl);
  311. data = driver->data;
  312. data->phy = devm_phy_create(dev, dev->of_node, &ops);
  313. if (IS_ERR(data->phy)) {
  314. dev_err(dev, "Failed to create usb drd phy\n");
  315. return PTR_ERR(data->phy);
  316. }
  317. data->driver = driver;
  318. phy_set_drvdata(data->phy, data);
  319. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  320. if (IS_ERR(phy_provider)) {
  321. dev_err(dev, "Failed to register as phy provider\n");
  322. return PTR_ERR(phy_provider);
  323. }
  324. platform_set_drvdata(pdev, driver);
  325. dev_info(dev, "Registered NS2 DRD Phy device\n");
  326. queue_delayed_work(system_power_efficient_wq, &driver->wq_extcon,
  327. driver->debounce_jiffies);
  328. return 0;
  329. }
  330. static struct platform_driver ns2_drd_phy_driver = {
  331. .probe = ns2_drd_phy_probe,
  332. .driver = {
  333. .name = "bcm-ns2-usbphy",
  334. .of_match_table = of_match_ptr(ns2_drd_phy_dt_ids),
  335. },
  336. };
  337. module_platform_driver(ns2_drd_phy_driver);
  338. MODULE_ALIAS("platform:bcm-ns2-drd-phy");
  339. MODULE_AUTHOR("Broadcom");
  340. MODULE_DESCRIPTION("Broadcom NS2 USB2 PHY driver");
  341. MODULE_LICENSE("GPL v2");