1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * phy-keystone - USB PHY, talking to dwc3 controller in Keystone. 4 * 5 * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * Author: WingMan Kwok <w-kwok2@ti.com> 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 */ 19 20 #include <linux/module.h> 21 #include <linux/platform_device.h> 22 #include <linux/usb/usb_phy_generic.h> 23 #include <linux/io.h> 24 #include <linux/of.h> 25 26 #include "phy-generic.h" 27 28 /* USB PHY control register offsets */ 29 #define USB_PHY_CTL_UTMI 0x0000 30 #define USB_PHY_CTL_PIPE 0x0004 31 #define USB_PHY_CTL_PARAM_1 0x0008 32 #define USB_PHY_CTL_PARAM_2 0x000c 33 #define USB_PHY_CTL_CLOCK 0x0010 34 #define USB_PHY_CTL_PLL 0x0014 35 36 #define PHY_REF_SSP_EN BIT(29) 37 38 struct keystone_usbphy { 39 struct usb_phy_generic usb_phy_gen; 40 void __iomem *phy_ctrl; 41 }; 42 43 static inline u32 keystone_usbphy_readl(void __iomem *base, u32 offset) 44 { 45 return readl(base + offset); 46 } 47 48 static inline void keystone_usbphy_writel(void __iomem *base, 49 u32 offset, u32 value) 50 { 51 writel(value, base + offset); 52 } 53 54 static int keystone_usbphy_init(struct usb_phy *phy) 55 { 56 struct keystone_usbphy *k_phy = dev_get_drvdata(phy->dev); 57 u32 val; 58 59 val = keystone_usbphy_readl(k_phy->phy_ctrl, USB_PHY_CTL_CLOCK); 60 keystone_usbphy_writel(k_phy->phy_ctrl, USB_PHY_CTL_CLOCK, 61 val | PHY_REF_SSP_EN); 62 return 0; 63 } 64 65 static void keystone_usbphy_shutdown(struct usb_phy *phy) 66 { 67 struct keystone_usbphy *k_phy = dev_get_drvdata(phy->dev); 68 u32 val; 69 70 val = keystone_usbphy_readl(k_phy->phy_ctrl, USB_PHY_CTL_CLOCK); 71 keystone_usbphy_writel(k_phy->phy_ctrl, USB_PHY_CTL_CLOCK, 72 val &= ~PHY_REF_SSP_EN); 73 } 74 75 static int keystone_usbphy_probe(struct platform_device *pdev) 76 { 77 struct device *dev = &pdev->dev; 78 struct keystone_usbphy *k_phy; 79 struct resource *res; 80 int ret; 81 82 k_phy = devm_kzalloc(dev, sizeof(*k_phy), GFP_KERNEL); 83 if (!k_phy) 84 return -ENOMEM; 85 86 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 87 k_phy->phy_ctrl = devm_ioremap_resource(dev, res); 88 if (IS_ERR(k_phy->phy_ctrl)) 89 return PTR_ERR(k_phy->phy_ctrl); 90 91 ret = usb_phy_gen_create_phy(dev, &k_phy->usb_phy_gen, NULL); 92 if (ret) 93 return ret; 94 95 k_phy->usb_phy_gen.phy.init = keystone_usbphy_init; 96 k_phy->usb_phy_gen.phy.shutdown = keystone_usbphy_shutdown; 97 98 platform_set_drvdata(pdev, k_phy); 99 100 return usb_add_phy_dev(&k_phy->usb_phy_gen.phy); 101 } 102 103 static int keystone_usbphy_remove(struct platform_device *pdev) 104 { 105 struct keystone_usbphy *k_phy = platform_get_drvdata(pdev); 106 107 usb_remove_phy(&k_phy->usb_phy_gen.phy); 108 109 return 0; 110 } 111 112 static const struct of_device_id keystone_usbphy_ids[] = { 113 { .compatible = "ti,keystone-usbphy" }, 114 { } 115 }; 116 MODULE_DEVICE_TABLE(of, keystone_usbphy_ids); 117 118 static struct platform_driver keystone_usbphy_driver = { 119 .probe = keystone_usbphy_probe, 120 .remove = keystone_usbphy_remove, 121 .driver = { 122 .name = "keystone-usbphy", 123 .of_match_table = keystone_usbphy_ids, 124 }, 125 }; 126 127 module_platform_driver(keystone_usbphy_driver); 128 129 MODULE_ALIAS("platform:keystone-usbphy"); 130 MODULE_AUTHOR("Texas Instruments Inc."); 131 MODULE_DESCRIPTION("Keystone USB phy driver"); 132 MODULE_LICENSE("GPL v2"); 133