2
0

0026-MIPS-lantiq-Add-GPHY-Firmware-loader.patch 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. From c8eedcadc38a5e6008d3990fbe0a5285b30335fc Mon Sep 17 00:00:00 2001
  2. From: Mathias Kresin <[email protected]>
  3. Date: Sun, 7 Jul 2019 21:48:56 +0200
  4. Subject: [PATCH] MIPS: lantiq: Add GPHY Firmware loader
  5. Upstream, the GPHY Firmware loader has been merged into the DSA switch
  6. driver. But we don't use the driver yet, so bring it back.
  7. Signed-off-by: Mathias Kresin <[email protected]>
  8. ---
  9. .../bindings/mips/lantiq/rcu-gphy.txt | 36 +++
  10. .../devicetree/bindings/mips/lantiq/rcu.txt | 18 ++
  11. arch/mips/configs/xway_defconfig | 1 +
  12. arch/mips/lantiq/Kconfig | 4 +
  13. drivers/soc/lantiq/Makefile | 1 +
  14. drivers/soc/lantiq/gphy.c | 224 ++++++++++++++++++
  15. 6 files changed, 284 insertions(+)
  16. create mode 100644 Documentation/devicetree/bindings/mips/lantiq/rcu-gphy.txt
  17. create mode 100644 drivers/soc/lantiq/gphy.c
  18. --- /dev/null
  19. +++ b/Documentation/devicetree/bindings/mips/lantiq/rcu-gphy.txt
  20. @@ -0,0 +1,37 @@
  21. +Lantiq XWAY SoC GPHY binding
  22. +============================
  23. +
  24. +This binding describes a software-defined ethernet PHY, provided by the RCU
  25. +module on newer Lantiq XWAY SoCs (xRX200 and newer).
  26. +
  27. +-------------------------------------------------------------------------------
  28. +Required properties:
  29. +- compatible : Should be one of
  30. + "lantiq,xrx200-gphy"
  31. + "lantiq,xrx200a1x-gphy"
  32. + "lantiq,xrx200a2x-gphy"
  33. + "lantiq,xrx300-gphy"
  34. + "lantiq,xrx330-gphy"
  35. +- reg : Addrress of the GPHY FW load address register
  36. +- resets : Must reference the RCU GPHY reset bit
  37. +- reset-names : One entry, value must be "gphy" or optional "gphy2"
  38. +- clocks : A reference to the (PMU) GPHY clock gate
  39. +
  40. +Optional properties:
  41. +- lantiq,gphy-mode : GPHY_MODE_GE (default) or GPHY_MODE_FE as defined in
  42. + <dt-bindings/mips/lantiq_xway_gphy.h>
  43. +
  44. +
  45. +-------------------------------------------------------------------------------
  46. +Example for the GPHys on the xRX200 SoCs:
  47. +
  48. +#include <dt-bindings/mips/lantiq_rcu_gphy.h>
  49. + gphy0: gphy@20 {
  50. + compatible = "lantiq,xrx200a2x-gphy";
  51. + reg = <0x20 0x4>;
  52. +
  53. + resets = <&reset0 31 30>, <&reset1 7 7>;
  54. + reset-names = "gphy", "gphy2";
  55. + clocks = <&pmu0 XRX200_PMU_GATE_GPHY>;
  56. + lantiq,gphy-mode = <GPHY_MODE_GE>;
  57. + };
  58. --- a/Documentation/devicetree/bindings/mips/lantiq/rcu.txt
  59. +++ b/Documentation/devicetree/bindings/mips/lantiq/rcu.txt
  60. @@ -26,6 +26,24 @@ Example of the RCU bindings on a xRX200
  61. ranges = <0x0 0x203000 0x100>;
  62. big-endian;
  63. + gphy0: gphy@20 {
  64. + compatible = "lantiq,xrx200a2x-gphy";
  65. + reg = <0x20 0x4>;
  66. +
  67. + resets = <&reset0 31 30>, <&reset1 7 7>;
  68. + reset-names = "gphy", "gphy2";
  69. + lantiq,gphy-mode = <GPHY_MODE_GE>;
  70. + };
  71. +
  72. + gphy1: gphy@68 {
  73. + compatible = "lantiq,xrx200a2x-gphy";
  74. + reg = <0x68 0x4>;
  75. +
  76. + resets = <&reset0 29 28>, <&reset1 6 6>;
  77. + reset-names = "gphy", "gphy2";
  78. + lantiq,gphy-mode = <GPHY_MODE_GE>;
  79. + };
  80. +
  81. reset0: reset-controller@10 {
  82. compatible = "lantiq,xrx200-reset";
  83. reg = <0x10 4>, <0x14 4>;
  84. --- a/arch/mips/configs/xway_defconfig
  85. +++ b/arch/mips/configs/xway_defconfig
  86. @@ -13,6 +13,7 @@ CONFIG_EMBEDDED=y
  87. # CONFIG_COMPAT_BRK is not set
  88. CONFIG_LANTIQ=y
  89. CONFIG_PCI_LANTIQ=y
  90. +CONFIG_XRX200_PHY_FW=y
  91. CONFIG_CPU_MIPS32_R2=y
  92. CONFIG_MIPS_VPE_LOADER=y
  93. CONFIG_NR_CPUS=2
  94. --- a/arch/mips/lantiq/Kconfig
  95. +++ b/arch/mips/lantiq/Kconfig
  96. @@ -62,4 +62,8 @@ config PCIE_LANTIQ_MSI
  97. depends on PCIE_LANTIQ && PCI_MSI
  98. default y
  99. +config XRX200_PHY_FW
  100. + bool "XRX200 PHY firmware loader"
  101. + depends on SOC_XWAY
  102. +
  103. endif
  104. --- a/drivers/soc/lantiq/Makefile
  105. +++ b/drivers/soc/lantiq/Makefile
  106. @@ -1,2 +1,3 @@
  107. # SPDX-License-Identifier: GPL-2.0-only
  108. obj-y += fpi-bus.o
  109. +obj-$(CONFIG_XRX200_PHY_FW) += gphy.o
  110. --- /dev/null
  111. +++ b/drivers/soc/lantiq/gphy.c
  112. @@ -0,0 +1,235 @@
  113. +/*
  114. + * This program is free software; you can redistribute it and/or modify it
  115. + * under the terms of the GNU General Public License version 2 as published
  116. + * by the Free Software Foundation.
  117. + *
  118. + * Copyright (C) 2012 John Crispin <[email protected]>
  119. + * Copyright (C) 2016 Martin Blumenstingl <[email protected]>
  120. + * Copyright (C) 2017 Hauke Mehrtens <[email protected]>
  121. + */
  122. +
  123. +#include <linux/clk.h>
  124. +#include <linux/delay.h>
  125. +#include <linux/dma-mapping.h>
  126. +#include <linux/firmware.h>
  127. +#include <linux/mfd/syscon.h>
  128. +#include <linux/module.h>
  129. +#include <linux/reboot.h>
  130. +#include <linux/regmap.h>
  131. +#include <linux/reset.h>
  132. +#include <linux/of_device.h>
  133. +#include <linux/of_platform.h>
  134. +#include <linux/property.h>
  135. +#include <dt-bindings/mips/lantiq_rcu_gphy.h>
  136. +
  137. +#include <lantiq_soc.h>
  138. +
  139. +#define XRX200_GPHY_FW_ALIGN (16 * 1024)
  140. +
  141. +struct xway_gphy_priv {
  142. + struct clk *gphy_clk_gate;
  143. + struct reset_control *gphy_reset;
  144. + struct reset_control *gphy_reset2;
  145. + void __iomem *membase;
  146. + char *fw_name;
  147. +};
  148. +
  149. +struct xway_gphy_match_data {
  150. + char *fe_firmware_name;
  151. + char *ge_firmware_name;
  152. +};
  153. +
  154. +static const struct xway_gphy_match_data xrx200a1x_gphy_data = {
  155. + .fe_firmware_name = "lantiq/xrx200_phy22f_a14.bin",
  156. + .ge_firmware_name = "lantiq/xrx200_phy11g_a14.bin",
  157. +};
  158. +
  159. +static const struct xway_gphy_match_data xrx200a2x_gphy_data = {
  160. + .fe_firmware_name = "lantiq/xrx200_phy22f_a22.bin",
  161. + .ge_firmware_name = "lantiq/xrx200_phy11g_a22.bin",
  162. +};
  163. +
  164. +static const struct xway_gphy_match_data xrx300_gphy_data = {
  165. + .fe_firmware_name = "lantiq/xrx300_phy22f_a21.bin",
  166. + .ge_firmware_name = "lantiq/xrx300_phy11g_a21.bin",
  167. +};
  168. +
  169. +static const struct of_device_id xway_gphy_match[] = {
  170. + { .compatible = "lantiq,xrx200-gphy", .data = NULL },
  171. + { .compatible = "lantiq,xrx200a1x-gphy", .data = &xrx200a1x_gphy_data },
  172. + { .compatible = "lantiq,xrx200a2x-gphy", .data = &xrx200a2x_gphy_data },
  173. + { .compatible = "lantiq,xrx300-gphy", .data = &xrx300_gphy_data },
  174. + { .compatible = "lantiq,xrx330-gphy", .data = &xrx300_gphy_data },
  175. + {},
  176. +};
  177. +MODULE_DEVICE_TABLE(of, xway_gphy_match);
  178. +
  179. +static int xway_gphy_load(struct device *dev, struct xway_gphy_priv *priv,
  180. + dma_addr_t *dev_addr)
  181. +{
  182. + const struct firmware *fw;
  183. + void *fw_addr;
  184. + dma_addr_t dma_addr;
  185. + size_t size;
  186. + int ret;
  187. +
  188. + ret = request_firmware(&fw, priv->fw_name, dev);
  189. + if (ret) {
  190. + dev_err(dev, "failed to load firmware: %s, error: %i\n",
  191. + priv->fw_name, ret);
  192. + return ret;
  193. + }
  194. +
  195. + /*
  196. + * GPHY cores need the firmware code in a persistent and contiguous
  197. + * memory area with a 16 kB boundary aligned start address.
  198. + */
  199. + size = fw->size + XRX200_GPHY_FW_ALIGN;
  200. +
  201. + fw_addr = dmam_alloc_coherent(dev, size, &dma_addr, GFP_KERNEL);
  202. + if (fw_addr) {
  203. + fw_addr = PTR_ALIGN(fw_addr, XRX200_GPHY_FW_ALIGN);
  204. + *dev_addr = ALIGN(dma_addr, XRX200_GPHY_FW_ALIGN);
  205. + memcpy(fw_addr, fw->data, fw->size);
  206. + } else {
  207. + dev_err(dev, "failed to alloc firmware memory\n");
  208. + ret = -ENOMEM;
  209. + }
  210. +
  211. + release_firmware(fw);
  212. +
  213. + return ret;
  214. +}
  215. +
  216. +static int xway_gphy_of_probe(struct platform_device *pdev,
  217. + struct xway_gphy_priv *priv)
  218. +{
  219. + struct device *dev = &pdev->dev;
  220. + const struct xway_gphy_match_data *gphy_fw_name_cfg;
  221. + u32 gphy_mode;
  222. + int ret;
  223. + struct resource *res_gphy;
  224. +
  225. + gphy_fw_name_cfg = of_device_get_match_data(dev);
  226. +
  227. + if (of_device_is_compatible(pdev->dev.of_node, "lantiq,xrx200-gphy"))
  228. + switch (ltq_soc_type()) {
  229. + case SOC_TYPE_VR9:
  230. + gphy_fw_name_cfg = &xrx200a1x_gphy_data;
  231. + break;
  232. + case SOC_TYPE_VR9_2:
  233. + gphy_fw_name_cfg = &xrx200a2x_gphy_data;
  234. + break;
  235. + }
  236. +
  237. + priv->gphy_clk_gate = devm_clk_get(dev, NULL);
  238. + if (IS_ERR(priv->gphy_clk_gate)) {
  239. + dev_err(dev, "Failed to lookup gate clock\n");
  240. + return PTR_ERR(priv->gphy_clk_gate);
  241. + }
  242. +
  243. + res_gphy = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  244. + priv->membase = devm_ioremap_resource(dev, res_gphy);
  245. + if (IS_ERR(priv->membase))
  246. + return PTR_ERR(priv->membase);
  247. +
  248. + priv->gphy_reset = devm_reset_control_get(dev, "gphy");
  249. + if (IS_ERR(priv->gphy_reset)) {
  250. + if (PTR_ERR(priv->gphy_reset) != -EPROBE_DEFER)
  251. + dev_err(dev, "Failed to lookup gphy reset\n");
  252. + return PTR_ERR(priv->gphy_reset);
  253. + }
  254. +
  255. + priv->gphy_reset2 = devm_reset_control_get_optional(dev, "gphy2");
  256. + if (IS_ERR(priv->gphy_reset2))
  257. + return PTR_ERR(priv->gphy_reset2);
  258. +
  259. + ret = device_property_read_u32(dev, "lantiq,gphy-mode", &gphy_mode);
  260. + /* Default to GE mode */
  261. + if (ret)
  262. + gphy_mode = GPHY_MODE_GE;
  263. +
  264. + switch (gphy_mode) {
  265. + case GPHY_MODE_FE:
  266. + priv->fw_name = gphy_fw_name_cfg->fe_firmware_name;
  267. + break;
  268. + case GPHY_MODE_GE:
  269. + priv->fw_name = gphy_fw_name_cfg->ge_firmware_name;
  270. + break;
  271. + default:
  272. + dev_err(dev, "Unknown GPHY mode %d\n", gphy_mode);
  273. + return -EINVAL;
  274. + }
  275. +
  276. + return 0;
  277. +}
  278. +
  279. +static int xway_gphy_probe(struct platform_device *pdev)
  280. +{
  281. + struct device *dev = &pdev->dev;
  282. + struct xway_gphy_priv *priv;
  283. + dma_addr_t fw_addr = 0;
  284. + int ret;
  285. +
  286. + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  287. + if (!priv)
  288. + return -ENOMEM;
  289. +
  290. + ret = xway_gphy_of_probe(pdev, priv);
  291. + if (ret)
  292. + return ret;
  293. +
  294. + ret = clk_prepare_enable(priv->gphy_clk_gate);
  295. + if (ret)
  296. + return ret;
  297. +
  298. + ret = xway_gphy_load(dev, priv, &fw_addr);
  299. + if (ret) {
  300. + clk_disable_unprepare(priv->gphy_clk_gate);
  301. + return ret;
  302. + }
  303. +
  304. + reset_control_assert(priv->gphy_reset);
  305. + reset_control_assert(priv->gphy_reset2);
  306. +
  307. + iowrite32be(fw_addr, priv->membase);
  308. +
  309. + reset_control_deassert(priv->gphy_reset);
  310. + reset_control_deassert(priv->gphy_reset2);
  311. +
  312. + platform_set_drvdata(pdev, priv);
  313. +
  314. + return ret;
  315. +}
  316. +
  317. +static int xway_gphy_remove(struct platform_device *pdev)
  318. +{
  319. + struct xway_gphy_priv *priv = platform_get_drvdata(pdev);
  320. +
  321. + iowrite32be(0, priv->membase);
  322. +
  323. + clk_disable_unprepare(priv->gphy_clk_gate);
  324. +
  325. + return 0;
  326. +}
  327. +
  328. +static struct platform_driver xway_gphy_driver = {
  329. + .probe = xway_gphy_probe,
  330. + .remove = xway_gphy_remove,
  331. + .driver = {
  332. + .name = "xway-rcu-gphy",
  333. + .of_match_table = xway_gphy_match,
  334. + },
  335. +};
  336. +
  337. +module_platform_driver(xway_gphy_driver);
  338. +
  339. +MODULE_FIRMWARE("lantiq/xrx300_phy11g_a21.bin");
  340. +MODULE_FIRMWARE("lantiq/xrx300_phy22f_a21.bin");
  341. +MODULE_FIRMWARE("lantiq/xrx200_phy11g_a14.bin");
  342. +MODULE_FIRMWARE("lantiq/xrx200_phy11g_a22.bin");
  343. +MODULE_FIRMWARE("lantiq/xrx200_phy22f_a14.bin");
  344. +MODULE_FIRMWARE("lantiq/xrx200_phy22f_a22.bin");
  345. +MODULE_AUTHOR("Martin Blumenstingl <[email protected]>");
  346. +MODULE_DESCRIPTION("Lantiq XWAY GPHY Firmware Loader");
  347. +MODULE_LICENSE("GPL");