196-usb-add-sunxi-phy-driver.patch 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. From 56feaa546c5ce4152fe14f725e9fc6b85f8a565b Mon Sep 17 00:00:00 2001
  2. From: Hans de Goede <hdegoede@redhat.com>
  3. Date: Sat, 4 Jan 2014 23:56:17 +0100
  4. Subject: [PATCH] PHY: sunxi: Add driver for sunxi usb phy
  5. The Allwinner A1x / A2x SoCs have 2 or 3 usb phys which are all accessed
  6. through a single set of registers. Besides this there are also some other
  7. phy related bits which need poking, which are per phy, but shared between the
  8. ohci and ehci controllers, so these are also controlled from this new phy
  9. driver.
  10. Signed-off-by: Hans de Goede <hdegoede@redhat.com>
  11. Acked-by: Maxime Ripard <maxime.ripard@free-electrons.com>
  12. ---
  13. .../devicetree/bindings/phy/sun4i-usb-phy.txt | 26 ++
  14. drivers/phy/Kconfig | 11 +
  15. drivers/phy/Makefile | 1 +
  16. drivers/phy/phy-sun4i-usb.c | 331 +++++++++++++++++++++
  17. 4 files changed, 369 insertions(+)
  18. create mode 100644 Documentation/devicetree/bindings/phy/sun4i-usb-phy.txt
  19. create mode 100644 drivers/phy/phy-sun4i-usb.c
  20. --- /dev/null
  21. +++ b/Documentation/devicetree/bindings/phy/sun4i-usb-phy.txt
  22. @@ -0,0 +1,26 @@
  23. +Allwinner sun4i USB PHY
  24. +-----------------------
  25. +
  26. +Required properties:
  27. +- compatible : should be one of "allwinner,sun4i-a10-usb-phy",
  28. + "allwinner,sun5i-a13-usb-phy" or "allwinner,sun7i-a20-usb-phy"
  29. +- reg : a list of offset + length pairs
  30. +- reg-names : "phy_ctrl", "pmu1" and for sun4i or sun7i "pmu2"
  31. +- #phy-cells : from the generic phy bindings, must be 1
  32. +- clocks : phandle + clock specifier for the phy clock
  33. +- clock-names : "usb_phy"
  34. +- resets : a list of phandle + reset specifier pairs
  35. +- reset-names : "usb0_reset", "usb1_reset" and for sun4i or sun7i "usb2_reset"
  36. +
  37. +Example:
  38. + usbphy: phy@0x01c13400 {
  39. + #phy-cells = <1>;
  40. + compatible = "allwinner,sun4i-a10-usb-phy";
  41. + /* phy base regs, phy1 pmu reg, phy2 pmu reg */
  42. + reg = <0x01c13400 0x10 0x01c14800 0x4 0x01c1c800 0x4>;
  43. + reg-names = "phy_ctrl", "pmu1", "pmu2";
  44. + clocks = <&usb_clk 8>;
  45. + clock-names = "usb_phy";
  46. + resets = <&usb_clk 1>, <&usb_clk 2>;
  47. + reset-names = "usb1_reset", "usb2_reset";
  48. + };
  49. --- a/drivers/phy/Kconfig
  50. +++ b/drivers/phy/Kconfig
  51. @@ -65,4 +65,15 @@ config BCM_KONA_USB2_PHY
  52. help
  53. Enable this to support the Broadcom Kona USB 2.0 PHY.
  54. +config PHY_SUN4I_USB
  55. + tristate "Allwinner sunxi SoC USB PHY driver"
  56. + depends on ARCH_SUNXI && HAS_IOMEM && OF
  57. + select GENERIC_PHY
  58. + help
  59. + Enable this to support the transceiver that is part of Allwinner
  60. + sunxi SoCs.
  61. +
  62. + This driver controls the entire USB PHY block, both the USB OTG
  63. + parts, as well as the 2 regular USB 2 host PHYs.
  64. +
  65. endmenu
  66. --- a/drivers/phy/Makefile
  67. +++ b/drivers/phy/Makefile
  68. @@ -9,3 +9,4 @@ obj-$(CONFIG_PHY_EXYNOS_MIPI_VIDEO) += p
  69. obj-$(CONFIG_PHY_MVEBU_SATA) += phy-mvebu-sata.o
  70. obj-$(CONFIG_OMAP_USB2) += phy-omap-usb2.o
  71. obj-$(CONFIG_TWL4030_USB) += phy-twl4030-usb.o
  72. +obj-$(CONFIG_PHY_SUN4I_USB) += phy-sun4i-usb.o
  73. --- /dev/null
  74. +++ b/drivers/phy/phy-sun4i-usb.c
  75. @@ -0,0 +1,331 @@
  76. +/*
  77. + * Allwinner sun4i USB phy driver
  78. + *
  79. + * Copyright (C) 2014 Hans de Goede <hdegoede@redhat.com>
  80. + *
  81. + * Based on code from
  82. + * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
  83. + *
  84. + * Modelled after: Samsung S5P/EXYNOS SoC series MIPI CSIS/DSIM DPHY driver
  85. + * Copyright (C) 2013 Samsung Electronics Co., Ltd.
  86. + * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
  87. + *
  88. + * This program is free software; you can redistribute it and/or modify
  89. + * it under the terms of the GNU General Public License as published by
  90. + * the Free Software Foundation; either version 2 of the License, or
  91. + * (at your option) any later version.
  92. + *
  93. + * This program is distributed in the hope that it will be useful,
  94. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  95. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  96. + * GNU General Public License for more details.
  97. + */
  98. +
  99. +#include <linux/clk.h>
  100. +#include <linux/io.h>
  101. +#include <linux/kernel.h>
  102. +#include <linux/module.h>
  103. +#include <linux/mutex.h>
  104. +#include <linux/of.h>
  105. +#include <linux/of_address.h>
  106. +#include <linux/phy/phy.h>
  107. +#include <linux/platform_device.h>
  108. +#include <linux/regulator/consumer.h>
  109. +#include <linux/reset.h>
  110. +
  111. +#define REG_ISCR 0x00
  112. +#define REG_PHYCTL 0x04
  113. +#define REG_PHYBIST 0x08
  114. +#define REG_PHYTUNE 0x0c
  115. +
  116. +#define PHYCTL_DATA BIT(7)
  117. +
  118. +#define SUNXI_AHB_ICHR8_EN BIT(10)
  119. +#define SUNXI_AHB_INCR4_BURST_EN BIT(9)
  120. +#define SUNXI_AHB_INCRX_ALIGN_EN BIT(8)
  121. +#define SUNXI_ULPI_BYPASS_EN BIT(0)
  122. +
  123. +/* Common Control Bits for Both PHYs */
  124. +#define PHY_PLL_BW 0x03
  125. +#define PHY_RES45_CAL_EN 0x0c
  126. +
  127. +/* Private Control Bits for Each PHY */
  128. +#define PHY_TX_AMPLITUDE_TUNE 0x20
  129. +#define PHY_TX_SLEWRATE_TUNE 0x22
  130. +#define PHY_VBUSVALID_TH_SEL 0x25
  131. +#define PHY_PULLUP_RES_SEL 0x27
  132. +#define PHY_OTG_FUNC_EN 0x28
  133. +#define PHY_VBUS_DET_EN 0x29
  134. +#define PHY_DISCON_TH_SEL 0x2a
  135. +
  136. +#define MAX_PHYS 3
  137. +
  138. +struct sun4i_usb_phy_data {
  139. + struct clk *clk;
  140. + void __iomem *base;
  141. + struct mutex mutex;
  142. + int num_phys;
  143. + u32 disc_thresh;
  144. + struct sun4i_usb_phy {
  145. + struct phy *phy;
  146. + void __iomem *pmu;
  147. + struct regulator *vbus;
  148. + struct reset_control *reset;
  149. + int index;
  150. + } phys[MAX_PHYS];
  151. +};
  152. +
  153. +#define to_sun4i_usb_phy_data(phy) \
  154. + container_of((phy), struct sun4i_usb_phy_data, phys[(phy)->index])
  155. +
  156. +static void sun4i_usb_phy_write(struct sun4i_usb_phy *phy, u32 addr, u32 data,
  157. + int len)
  158. +{
  159. + struct sun4i_usb_phy_data *phy_data = to_sun4i_usb_phy_data(phy);
  160. + u32 temp, usbc_bit = BIT(phy->index * 2);
  161. + int i;
  162. +
  163. + mutex_lock(&phy_data->mutex);
  164. +
  165. + for (i = 0; i < len; i++) {
  166. + temp = readl(phy_data->base + REG_PHYCTL);
  167. +
  168. + /* clear the address portion */
  169. + temp &= ~(0xff << 8);
  170. +
  171. + /* set the address */
  172. + temp |= ((addr + i) << 8);
  173. + writel(temp, phy_data->base + REG_PHYCTL);
  174. +
  175. + /* set the data bit and clear usbc bit*/
  176. + temp = readb(phy_data->base + REG_PHYCTL);
  177. + if (data & 0x1)
  178. + temp |= PHYCTL_DATA;
  179. + else
  180. + temp &= ~PHYCTL_DATA;
  181. + temp &= ~usbc_bit;
  182. + writeb(temp, phy_data->base + REG_PHYCTL);
  183. +
  184. + /* pulse usbc_bit */
  185. + temp = readb(phy_data->base + REG_PHYCTL);
  186. + temp |= usbc_bit;
  187. + writeb(temp, phy_data->base + REG_PHYCTL);
  188. +
  189. + temp = readb(phy_data->base + REG_PHYCTL);
  190. + temp &= ~usbc_bit;
  191. + writeb(temp, phy_data->base + REG_PHYCTL);
  192. +
  193. + data >>= 1;
  194. + }
  195. + mutex_unlock(&phy_data->mutex);
  196. +}
  197. +
  198. +static void sun4i_usb_phy_passby(struct sun4i_usb_phy *phy, int enable)
  199. +{
  200. + u32 bits, reg_value;
  201. +
  202. + if (!phy->pmu)
  203. + return;
  204. +
  205. + bits = SUNXI_AHB_ICHR8_EN | SUNXI_AHB_INCR4_BURST_EN |
  206. + SUNXI_AHB_INCRX_ALIGN_EN | SUNXI_ULPI_BYPASS_EN;
  207. +
  208. + reg_value = readl(phy->pmu);
  209. +
  210. + if (enable)
  211. + reg_value |= bits;
  212. + else
  213. + reg_value &= ~bits;
  214. +
  215. + writel(reg_value, phy->pmu);
  216. +}
  217. +
  218. +static int sun4i_usb_phy_init(struct phy *_phy)
  219. +{
  220. + struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
  221. + struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
  222. + int ret;
  223. +
  224. + ret = clk_prepare_enable(data->clk);
  225. + if (ret)
  226. + return ret;
  227. +
  228. + ret = reset_control_deassert(phy->reset);
  229. + if (ret) {
  230. + clk_disable_unprepare(data->clk);
  231. + return ret;
  232. + }
  233. +
  234. + /* Adjust PHY's magnitude and rate */
  235. + sun4i_usb_phy_write(phy, PHY_TX_AMPLITUDE_TUNE, 0x14, 5);
  236. +
  237. + /* Disconnect threshold adjustment */
  238. + sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL, data->disc_thresh, 2);
  239. +
  240. + sun4i_usb_phy_passby(phy, 1);
  241. +
  242. + return 0;
  243. +}
  244. +
  245. +static int sun4i_usb_phy_exit(struct phy *_phy)
  246. +{
  247. + struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
  248. + struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
  249. +
  250. + sun4i_usb_phy_passby(phy, 0);
  251. + reset_control_assert(phy->reset);
  252. + clk_disable_unprepare(data->clk);
  253. +
  254. + return 0;
  255. +}
  256. +
  257. +static int sun4i_usb_phy_power_on(struct phy *_phy)
  258. +{
  259. + struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
  260. + int ret = 0;
  261. +
  262. + if (phy->vbus)
  263. + ret = regulator_enable(phy->vbus);
  264. +
  265. + return ret;
  266. +}
  267. +
  268. +static int sun4i_usb_phy_power_off(struct phy *_phy)
  269. +{
  270. + struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
  271. +
  272. + if (phy->vbus)
  273. + regulator_disable(phy->vbus);
  274. +
  275. + return 0;
  276. +}
  277. +
  278. +static struct phy_ops sun4i_usb_phy_ops = {
  279. + .init = sun4i_usb_phy_init,
  280. + .exit = sun4i_usb_phy_exit,
  281. + .power_on = sun4i_usb_phy_power_on,
  282. + .power_off = sun4i_usb_phy_power_off,
  283. + .owner = THIS_MODULE,
  284. +};
  285. +
  286. +static struct phy *sun4i_usb_phy_xlate(struct device *dev,
  287. + struct of_phandle_args *args)
  288. +{
  289. + struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
  290. +
  291. + if (WARN_ON(args->args[0] == 0 || args->args[0] >= data->num_phys))
  292. + return ERR_PTR(-ENODEV);
  293. +
  294. + return data->phys[args->args[0]].phy;
  295. +}
  296. +
  297. +static int sun4i_usb_phy_probe(struct platform_device *pdev)
  298. +{
  299. + struct sun4i_usb_phy_data *data;
  300. + struct device *dev = &pdev->dev;
  301. + struct device_node *np = dev->of_node;
  302. + void __iomem *pmu = NULL;
  303. + struct phy_provider *phy_provider;
  304. + struct reset_control *reset;
  305. + struct regulator *vbus;
  306. + struct resource *res;
  307. + struct phy *phy;
  308. + char name[16];
  309. + int i;
  310. +
  311. + data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  312. + if (!data)
  313. + return -ENOMEM;
  314. +
  315. + mutex_init(&data->mutex);
  316. +
  317. + if (of_device_is_compatible(np, "allwinner,sun5i-a13-usb-phy"))
  318. + data->num_phys = 2;
  319. + else
  320. + data->num_phys = 3;
  321. +
  322. + if (of_device_is_compatible(np, "allwinner,sun4i-a10-usb-phy"))
  323. + data->disc_thresh = 3;
  324. + else
  325. + data->disc_thresh = 2;
  326. +
  327. + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy_ctrl");
  328. + data->base = devm_ioremap_resource(dev, res);
  329. + if (IS_ERR(data->base))
  330. + return PTR_ERR(data->base);
  331. +
  332. + data->clk = devm_clk_get(dev, "usb_phy");
  333. + if (IS_ERR(data->clk)) {
  334. + dev_err(dev, "could not get usb_phy clock\n");
  335. + return PTR_ERR(data->clk);
  336. + }
  337. +
  338. + /* Skip 0, 0 is the phy for otg which is not yet supported. */
  339. + for (i = 1; i < data->num_phys; i++) {
  340. + snprintf(name, sizeof(name), "usb%d_vbus", i);
  341. + vbus = devm_regulator_get_optional(dev, name);
  342. + if (IS_ERR(vbus)) {
  343. + if (PTR_ERR(vbus) == -EPROBE_DEFER)
  344. + return -EPROBE_DEFER;
  345. + vbus = NULL;
  346. + }
  347. +
  348. + snprintf(name, sizeof(name), "usb%d_reset", i);
  349. + reset = devm_reset_control_get(dev, name);
  350. + if (IS_ERR(reset)) {
  351. + dev_err(dev, "failed to get reset %s\n", name);
  352. + return PTR_ERR(reset);
  353. + }
  354. +
  355. + if (i) { /* No pmu for usbc0 */
  356. + snprintf(name, sizeof(name), "pmu%d", i);
  357. + res = platform_get_resource_byname(pdev,
  358. + IORESOURCE_MEM, name);
  359. + pmu = devm_ioremap_resource(dev, res);
  360. + if (IS_ERR(pmu))
  361. + return PTR_ERR(pmu);
  362. + }
  363. +
  364. + phy = devm_phy_create(dev, &sun4i_usb_phy_ops, NULL);
  365. + if (IS_ERR(phy)) {
  366. + dev_err(dev, "failed to create PHY %d\n", i);
  367. + return PTR_ERR(phy);
  368. + }
  369. +
  370. + data->phys[i].phy = phy;
  371. + data->phys[i].pmu = pmu;
  372. + data->phys[i].vbus = vbus;
  373. + data->phys[i].reset = reset;
  374. + data->phys[i].index = i;
  375. + phy_set_drvdata(phy, &data->phys[i]);
  376. + }
  377. +
  378. + dev_set_drvdata(dev, data);
  379. + phy_provider = devm_of_phy_provider_register(dev, sun4i_usb_phy_xlate);
  380. + if (IS_ERR(phy_provider))
  381. + return PTR_ERR(phy_provider);
  382. +
  383. + return 0;
  384. +}
  385. +
  386. +static const struct of_device_id sun4i_usb_phy_of_match[] = {
  387. + { .compatible = "allwinner,sun4i-a10-usb-phy" },
  388. + { .compatible = "allwinner,sun5i-a13-usb-phy" },
  389. + { .compatible = "allwinner,sun7i-a20-usb-phy" },
  390. + { },
  391. +};
  392. +MODULE_DEVICE_TABLE(of, sun4i_usb_phy_of_match);
  393. +
  394. +static struct platform_driver sun4i_usb_phy_driver = {
  395. + .probe = sun4i_usb_phy_probe,
  396. + .driver = {
  397. + .of_match_table = sun4i_usb_phy_of_match,
  398. + .name = "sun4i-usb-phy",
  399. + .owner = THIS_MODULE,
  400. + }
  401. +};
  402. +module_platform_driver(sun4i_usb_phy_driver);
  403. +
  404. +MODULE_DESCRIPTION("Allwinner sun4i USB phy driver");
  405. +MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  406. +MODULE_LICENSE("GPL v2");