dwmac-ipq806x.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * Qualcomm Atheros IPQ806x GMAC glue layer
  3. *
  4. * Copyright (C) 2015 The Linux Foundation
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include <linux/device.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/phy.h>
  21. #include <linux/regmap.h>
  22. #include <linux/clk.h>
  23. #include <linux/reset.h>
  24. #include <linux/of_net.h>
  25. #include <linux/mfd/syscon.h>
  26. #include <linux/stmmac.h>
  27. #include <linux/of_mdio.h>
  28. #include <linux/module.h>
  29. #include "stmmac_platform.h"
  30. #define NSS_COMMON_CLK_GATE 0x8
  31. #define NSS_COMMON_CLK_GATE_PTP_EN(x) BIT(0x10 + x)
  32. #define NSS_COMMON_CLK_GATE_RGMII_RX_EN(x) BIT(0x9 + (x * 2))
  33. #define NSS_COMMON_CLK_GATE_RGMII_TX_EN(x) BIT(0x8 + (x * 2))
  34. #define NSS_COMMON_CLK_GATE_GMII_RX_EN(x) BIT(0x4 + x)
  35. #define NSS_COMMON_CLK_GATE_GMII_TX_EN(x) BIT(0x0 + x)
  36. #define NSS_COMMON_CLK_DIV0 0xC
  37. #define NSS_COMMON_CLK_DIV_OFFSET(x) (x * 8)
  38. #define NSS_COMMON_CLK_DIV_MASK 0x7f
  39. #define NSS_COMMON_CLK_SRC_CTRL 0x14
  40. #define NSS_COMMON_CLK_SRC_CTRL_OFFSET(x) (x)
  41. /* Mode is coded on 1 bit but is different depending on the MAC ID:
  42. * MAC0: QSGMII=0 RGMII=1
  43. * MAC1: QSGMII=0 SGMII=0 RGMII=1
  44. * MAC2 & MAC3: QSGMII=0 SGMII=1
  45. */
  46. #define NSS_COMMON_CLK_SRC_CTRL_RGMII(x) 1
  47. #define NSS_COMMON_CLK_SRC_CTRL_SGMII(x) ((x >= 2) ? 1 : 0)
  48. #define NSS_COMMON_MACSEC_CTL 0x28
  49. #define NSS_COMMON_MACSEC_CTL_EXT_BYPASS_EN(x) (1 << x)
  50. #define NSS_COMMON_GMAC_CTL(x) (0x30 + (x * 4))
  51. #define NSS_COMMON_GMAC_CTL_CSYS_REQ BIT(19)
  52. #define NSS_COMMON_GMAC_CTL_PHY_IFACE_SEL BIT(16)
  53. #define NSS_COMMON_GMAC_CTL_IFG_LIMIT_OFFSET 8
  54. #define NSS_COMMON_GMAC_CTL_IFG_OFFSET 0
  55. #define NSS_COMMON_GMAC_CTL_IFG_MASK 0x3f
  56. #define NSS_COMMON_CLK_DIV_RGMII_1000 1
  57. #define NSS_COMMON_CLK_DIV_RGMII_100 9
  58. #define NSS_COMMON_CLK_DIV_RGMII_10 99
  59. #define NSS_COMMON_CLK_DIV_SGMII_1000 0
  60. #define NSS_COMMON_CLK_DIV_SGMII_100 4
  61. #define NSS_COMMON_CLK_DIV_SGMII_10 49
  62. #define QSGMII_PCS_MODE_CTL 0x68
  63. #define QSGMII_PCS_MODE_CTL_AUTONEG_EN(x) BIT((x * 8) + 7)
  64. #define QSGMII_PCS_CAL_LCKDT_CTL 0x120
  65. #define QSGMII_PCS_CAL_LCKDT_CTL_RST BIT(19)
  66. /* Only GMAC1/2/3 support SGMII and their CTL register are not contiguous */
  67. #define QSGMII_PHY_SGMII_CTL(x) ((x == 1) ? 0x134 : \
  68. (0x13c + (4 * (x - 2))))
  69. #define QSGMII_PHY_CDR_EN BIT(0)
  70. #define QSGMII_PHY_RX_FRONT_EN BIT(1)
  71. #define QSGMII_PHY_RX_SIGNAL_DETECT_EN BIT(2)
  72. #define QSGMII_PHY_TX_DRIVER_EN BIT(3)
  73. #define QSGMII_PHY_QSGMII_EN BIT(7)
  74. #define QSGMII_PHY_PHASE_LOOP_GAIN_OFFSET 12
  75. #define QSGMII_PHY_PHASE_LOOP_GAIN_MASK 0x7
  76. #define QSGMII_PHY_RX_DC_BIAS_OFFSET 18
  77. #define QSGMII_PHY_RX_DC_BIAS_MASK 0x3
  78. #define QSGMII_PHY_RX_INPUT_EQU_OFFSET 20
  79. #define QSGMII_PHY_RX_INPUT_EQU_MASK 0x3
  80. #define QSGMII_PHY_CDR_PI_SLEW_OFFSET 22
  81. #define QSGMII_PHY_CDR_PI_SLEW_MASK 0x3
  82. #define QSGMII_PHY_TX_DRV_AMP_OFFSET 28
  83. #define QSGMII_PHY_TX_DRV_AMP_MASK 0xf
  84. struct ipq806x_gmac {
  85. struct platform_device *pdev;
  86. struct regmap *nss_common;
  87. struct regmap *qsgmii_csr;
  88. uint32_t id;
  89. struct clk *core_clk;
  90. phy_interface_t phy_mode;
  91. };
  92. static int get_clk_div_sgmii(struct ipq806x_gmac *gmac, unsigned int speed)
  93. {
  94. struct device *dev = &gmac->pdev->dev;
  95. int div;
  96. switch (speed) {
  97. case SPEED_1000:
  98. div = NSS_COMMON_CLK_DIV_SGMII_1000;
  99. break;
  100. case SPEED_100:
  101. div = NSS_COMMON_CLK_DIV_SGMII_100;
  102. break;
  103. case SPEED_10:
  104. div = NSS_COMMON_CLK_DIV_SGMII_10;
  105. break;
  106. default:
  107. dev_err(dev, "Speed %dMbps not supported in SGMII\n", speed);
  108. return -EINVAL;
  109. }
  110. return div;
  111. }
  112. static int get_clk_div_rgmii(struct ipq806x_gmac *gmac, unsigned int speed)
  113. {
  114. struct device *dev = &gmac->pdev->dev;
  115. int div;
  116. switch (speed) {
  117. case SPEED_1000:
  118. div = NSS_COMMON_CLK_DIV_RGMII_1000;
  119. break;
  120. case SPEED_100:
  121. div = NSS_COMMON_CLK_DIV_RGMII_100;
  122. break;
  123. case SPEED_10:
  124. div = NSS_COMMON_CLK_DIV_RGMII_10;
  125. break;
  126. default:
  127. dev_err(dev, "Speed %dMbps not supported in RGMII\n", speed);
  128. return -EINVAL;
  129. }
  130. return div;
  131. }
  132. static int ipq806x_gmac_set_speed(struct ipq806x_gmac *gmac, unsigned int speed)
  133. {
  134. uint32_t clk_bits, val;
  135. int div;
  136. switch (gmac->phy_mode) {
  137. case PHY_INTERFACE_MODE_RGMII:
  138. div = get_clk_div_rgmii(gmac, speed);
  139. clk_bits = NSS_COMMON_CLK_GATE_RGMII_RX_EN(gmac->id) |
  140. NSS_COMMON_CLK_GATE_RGMII_TX_EN(gmac->id);
  141. break;
  142. case PHY_INTERFACE_MODE_SGMII:
  143. div = get_clk_div_sgmii(gmac, speed);
  144. clk_bits = NSS_COMMON_CLK_GATE_GMII_RX_EN(gmac->id) |
  145. NSS_COMMON_CLK_GATE_GMII_TX_EN(gmac->id);
  146. break;
  147. default:
  148. dev_err(&gmac->pdev->dev, "Unsupported PHY mode: \"%s\"\n",
  149. phy_modes(gmac->phy_mode));
  150. return -EINVAL;
  151. }
  152. /* Disable the clocks */
  153. regmap_read(gmac->nss_common, NSS_COMMON_CLK_GATE, &val);
  154. val &= ~clk_bits;
  155. regmap_write(gmac->nss_common, NSS_COMMON_CLK_GATE, val);
  156. /* Set the divider */
  157. regmap_read(gmac->nss_common, NSS_COMMON_CLK_DIV0, &val);
  158. val &= ~(NSS_COMMON_CLK_DIV_MASK
  159. << NSS_COMMON_CLK_DIV_OFFSET(gmac->id));
  160. val |= div << NSS_COMMON_CLK_DIV_OFFSET(gmac->id);
  161. regmap_write(gmac->nss_common, NSS_COMMON_CLK_DIV0, val);
  162. /* Enable the clock back */
  163. regmap_read(gmac->nss_common, NSS_COMMON_CLK_GATE, &val);
  164. val |= clk_bits;
  165. regmap_write(gmac->nss_common, NSS_COMMON_CLK_GATE, val);
  166. return 0;
  167. }
  168. static void *ipq806x_gmac_of_parse(struct ipq806x_gmac *gmac)
  169. {
  170. struct device *dev = &gmac->pdev->dev;
  171. gmac->phy_mode = of_get_phy_mode(dev->of_node);
  172. if (gmac->phy_mode < 0) {
  173. dev_err(dev, "missing phy mode property\n");
  174. return ERR_PTR(-EINVAL);
  175. }
  176. if (of_property_read_u32(dev->of_node, "qcom,id", &gmac->id) < 0) {
  177. dev_err(dev, "missing qcom id property\n");
  178. return ERR_PTR(-EINVAL);
  179. }
  180. /* The GMACs are called 1 to 4 in the documentation, but to simplify the
  181. * code and keep it consistent with the Linux convention, we'll number
  182. * them from 0 to 3 here.
  183. */
  184. if (gmac->id < 0 || gmac->id > 3) {
  185. dev_err(dev, "invalid gmac id\n");
  186. return ERR_PTR(-EINVAL);
  187. }
  188. gmac->core_clk = devm_clk_get(dev, "stmmaceth");
  189. if (IS_ERR(gmac->core_clk)) {
  190. dev_err(dev, "missing stmmaceth clk property\n");
  191. return gmac->core_clk;
  192. }
  193. clk_set_rate(gmac->core_clk, 266000000);
  194. /* Setup the register map for the nss common registers */
  195. gmac->nss_common = syscon_regmap_lookup_by_phandle(dev->of_node,
  196. "qcom,nss-common");
  197. if (IS_ERR(gmac->nss_common)) {
  198. dev_err(dev, "missing nss-common node\n");
  199. return gmac->nss_common;
  200. }
  201. /* Setup the register map for the qsgmii csr registers */
  202. gmac->qsgmii_csr = syscon_regmap_lookup_by_phandle(dev->of_node,
  203. "qcom,qsgmii-csr");
  204. if (IS_ERR(gmac->qsgmii_csr)) {
  205. dev_err(dev, "missing qsgmii-csr node\n");
  206. return gmac->qsgmii_csr;
  207. }
  208. return NULL;
  209. }
  210. static void ipq806x_gmac_fix_mac_speed(void *priv, unsigned int speed)
  211. {
  212. struct ipq806x_gmac *gmac = priv;
  213. ipq806x_gmac_set_speed(gmac, speed);
  214. }
  215. static int ipq806x_gmac_probe(struct platform_device *pdev)
  216. {
  217. struct plat_stmmacenet_data *plat_dat;
  218. struct stmmac_resources stmmac_res;
  219. struct device *dev = &pdev->dev;
  220. struct ipq806x_gmac *gmac;
  221. int val;
  222. void *err;
  223. val = stmmac_get_platform_resources(pdev, &stmmac_res);
  224. if (val)
  225. return val;
  226. plat_dat = stmmac_probe_config_dt(pdev, &stmmac_res.mac);
  227. if (IS_ERR(plat_dat))
  228. return PTR_ERR(plat_dat);
  229. gmac = devm_kzalloc(dev, sizeof(*gmac), GFP_KERNEL);
  230. if (!gmac)
  231. return -ENOMEM;
  232. gmac->pdev = pdev;
  233. err = ipq806x_gmac_of_parse(gmac);
  234. if (IS_ERR(err)) {
  235. dev_err(dev, "device tree parsing error\n");
  236. return PTR_ERR(err);
  237. }
  238. regmap_write(gmac->qsgmii_csr, QSGMII_PCS_CAL_LCKDT_CTL,
  239. QSGMII_PCS_CAL_LCKDT_CTL_RST);
  240. /* Inter frame gap is set to 12 */
  241. val = 12 << NSS_COMMON_GMAC_CTL_IFG_OFFSET |
  242. 12 << NSS_COMMON_GMAC_CTL_IFG_LIMIT_OFFSET;
  243. /* We also initiate an AXI low power exit request */
  244. val |= NSS_COMMON_GMAC_CTL_CSYS_REQ;
  245. switch (gmac->phy_mode) {
  246. case PHY_INTERFACE_MODE_RGMII:
  247. val |= NSS_COMMON_GMAC_CTL_PHY_IFACE_SEL;
  248. break;
  249. case PHY_INTERFACE_MODE_SGMII:
  250. val &= ~NSS_COMMON_GMAC_CTL_PHY_IFACE_SEL;
  251. break;
  252. default:
  253. dev_err(&pdev->dev, "Unsupported PHY mode: \"%s\"\n",
  254. phy_modes(gmac->phy_mode));
  255. return -EINVAL;
  256. }
  257. regmap_write(gmac->nss_common, NSS_COMMON_GMAC_CTL(gmac->id), val);
  258. /* Configure the clock src according to the mode */
  259. regmap_read(gmac->nss_common, NSS_COMMON_CLK_SRC_CTRL, &val);
  260. val &= ~(1 << NSS_COMMON_CLK_SRC_CTRL_OFFSET(gmac->id));
  261. switch (gmac->phy_mode) {
  262. case PHY_INTERFACE_MODE_RGMII:
  263. val |= NSS_COMMON_CLK_SRC_CTRL_RGMII(gmac->id) <<
  264. NSS_COMMON_CLK_SRC_CTRL_OFFSET(gmac->id);
  265. break;
  266. case PHY_INTERFACE_MODE_SGMII:
  267. val |= NSS_COMMON_CLK_SRC_CTRL_SGMII(gmac->id) <<
  268. NSS_COMMON_CLK_SRC_CTRL_OFFSET(gmac->id);
  269. break;
  270. default:
  271. dev_err(&pdev->dev, "Unsupported PHY mode: \"%s\"\n",
  272. phy_modes(gmac->phy_mode));
  273. return -EINVAL;
  274. }
  275. regmap_write(gmac->nss_common, NSS_COMMON_CLK_SRC_CTRL, val);
  276. /* Enable PTP clock */
  277. regmap_read(gmac->nss_common, NSS_COMMON_CLK_GATE, &val);
  278. val |= NSS_COMMON_CLK_GATE_PTP_EN(gmac->id);
  279. regmap_write(gmac->nss_common, NSS_COMMON_CLK_GATE, val);
  280. if (gmac->phy_mode == PHY_INTERFACE_MODE_SGMII) {
  281. regmap_write(gmac->qsgmii_csr, QSGMII_PHY_SGMII_CTL(gmac->id),
  282. QSGMII_PHY_CDR_EN |
  283. QSGMII_PHY_RX_FRONT_EN |
  284. QSGMII_PHY_RX_SIGNAL_DETECT_EN |
  285. QSGMII_PHY_TX_DRIVER_EN |
  286. QSGMII_PHY_QSGMII_EN |
  287. 0x4ul << QSGMII_PHY_PHASE_LOOP_GAIN_OFFSET |
  288. 0x3ul << QSGMII_PHY_RX_DC_BIAS_OFFSET |
  289. 0x1ul << QSGMII_PHY_RX_INPUT_EQU_OFFSET |
  290. 0x2ul << QSGMII_PHY_CDR_PI_SLEW_OFFSET |
  291. 0xCul << QSGMII_PHY_TX_DRV_AMP_OFFSET);
  292. }
  293. plat_dat->has_gmac = true;
  294. plat_dat->bsp_priv = gmac;
  295. plat_dat->fix_mac_speed = ipq806x_gmac_fix_mac_speed;
  296. return stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
  297. }
  298. static const struct of_device_id ipq806x_gmac_dwmac_match[] = {
  299. { .compatible = "qcom,ipq806x-gmac" },
  300. { }
  301. };
  302. MODULE_DEVICE_TABLE(of, ipq806x_gmac_dwmac_match);
  303. static struct platform_driver ipq806x_gmac_dwmac_driver = {
  304. .probe = ipq806x_gmac_probe,
  305. .remove = stmmac_pltfr_remove,
  306. .driver = {
  307. .name = "ipq806x-gmac-dwmac",
  308. .pm = &stmmac_pltfr_pm_ops,
  309. .of_match_table = ipq806x_gmac_dwmac_match,
  310. },
  311. };
  312. module_platform_driver(ipq806x_gmac_dwmac_driver);
  313. MODULE_AUTHOR("Mathieu Olivari <mathieu@codeaurora.org>");
  314. MODULE_DESCRIPTION("Qualcomm Atheros IPQ806x DWMAC specific glue layer");
  315. MODULE_LICENSE("Dual BSD/GPL");