phy-brcmstb-sata.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Broadcom SATA3 AHCI Controller PHY Driver
  3. *
  4. * Copyright © 2009-2015 Broadcom Corporation
  5. *
  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, or (at your option)
  9. * any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/device.h>
  17. #include <linux/init.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/io.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/of.h>
  23. #include <linux/phy/phy.h>
  24. #include <linux/platform_device.h>
  25. #define SATA_MDIO_BANK_OFFSET 0x23c
  26. #define SATA_MDIO_REG_OFFSET(ofs) ((ofs) * 4)
  27. #define MAX_PORTS 2
  28. /* Register offset between PHYs in PCB space */
  29. #define SATA_MDIO_REG_28NM_SPACE_SIZE 0x1000
  30. /* The older SATA PHY registers duplicated per port registers within the map,
  31. * rather than having a separate map per port.
  32. */
  33. #define SATA_MDIO_REG_40NM_SPACE_SIZE 0x10
  34. enum brcm_sata_phy_version {
  35. BRCM_SATA_PHY_28NM,
  36. BRCM_SATA_PHY_40NM,
  37. };
  38. struct brcm_sata_port {
  39. int portnum;
  40. struct phy *phy;
  41. struct brcm_sata_phy *phy_priv;
  42. bool ssc_en;
  43. };
  44. struct brcm_sata_phy {
  45. struct device *dev;
  46. void __iomem *phy_base;
  47. enum brcm_sata_phy_version version;
  48. struct brcm_sata_port phys[MAX_PORTS];
  49. };
  50. enum sata_mdio_phy_regs {
  51. PLL_REG_BANK_0 = 0x50,
  52. PLL_REG_BANK_0_PLLCONTROL_0 = 0x81,
  53. TXPMD_REG_BANK = 0x1a0,
  54. TXPMD_CONTROL1 = 0x81,
  55. TXPMD_CONTROL1_TX_SSC_EN_FRC = BIT(0),
  56. TXPMD_CONTROL1_TX_SSC_EN_FRC_VAL = BIT(1),
  57. TXPMD_TX_FREQ_CTRL_CONTROL1 = 0x82,
  58. TXPMD_TX_FREQ_CTRL_CONTROL2 = 0x83,
  59. TXPMD_TX_FREQ_CTRL_CONTROL2_FMIN_MASK = 0x3ff,
  60. TXPMD_TX_FREQ_CTRL_CONTROL3 = 0x84,
  61. TXPMD_TX_FREQ_CTRL_CONTROL3_FMAX_MASK = 0x3ff,
  62. };
  63. static inline void __iomem *brcm_sata_phy_base(struct brcm_sata_port *port)
  64. {
  65. struct brcm_sata_phy *priv = port->phy_priv;
  66. u32 offset = 0;
  67. if (priv->version == BRCM_SATA_PHY_28NM)
  68. offset = SATA_MDIO_REG_28NM_SPACE_SIZE;
  69. else if (priv->version == BRCM_SATA_PHY_40NM)
  70. offset = SATA_MDIO_REG_40NM_SPACE_SIZE;
  71. else
  72. dev_err(priv->dev, "invalid phy version\n");
  73. return priv->phy_base + (port->portnum * offset);
  74. }
  75. static void brcm_sata_mdio_wr(void __iomem *addr, u32 bank, u32 ofs,
  76. u32 msk, u32 value)
  77. {
  78. u32 tmp;
  79. writel(bank, addr + SATA_MDIO_BANK_OFFSET);
  80. tmp = readl(addr + SATA_MDIO_REG_OFFSET(ofs));
  81. tmp = (tmp & msk) | value;
  82. writel(tmp, addr + SATA_MDIO_REG_OFFSET(ofs));
  83. }
  84. /* These defaults were characterized by H/W group */
  85. #define FMIN_VAL_DEFAULT 0x3df
  86. #define FMAX_VAL_DEFAULT 0x3df
  87. #define FMAX_VAL_SSC 0x83
  88. static void brcm_sata_cfg_ssc(struct brcm_sata_port *port)
  89. {
  90. void __iomem *base = brcm_sata_phy_base(port);
  91. struct brcm_sata_phy *priv = port->phy_priv;
  92. u32 tmp;
  93. /* override the TX spread spectrum setting */
  94. tmp = TXPMD_CONTROL1_TX_SSC_EN_FRC_VAL | TXPMD_CONTROL1_TX_SSC_EN_FRC;
  95. brcm_sata_mdio_wr(base, TXPMD_REG_BANK, TXPMD_CONTROL1, ~tmp, tmp);
  96. /* set fixed min freq */
  97. brcm_sata_mdio_wr(base, TXPMD_REG_BANK, TXPMD_TX_FREQ_CTRL_CONTROL2,
  98. ~TXPMD_TX_FREQ_CTRL_CONTROL2_FMIN_MASK,
  99. FMIN_VAL_DEFAULT);
  100. /* set fixed max freq depending on SSC config */
  101. if (port->ssc_en) {
  102. dev_info(priv->dev, "enabling SSC on port %d\n", port->portnum);
  103. tmp = FMAX_VAL_SSC;
  104. } else {
  105. tmp = FMAX_VAL_DEFAULT;
  106. }
  107. brcm_sata_mdio_wr(base, TXPMD_REG_BANK, TXPMD_TX_FREQ_CTRL_CONTROL3,
  108. ~TXPMD_TX_FREQ_CTRL_CONTROL3_FMAX_MASK, tmp);
  109. }
  110. static int brcm_sata_phy_init(struct phy *phy)
  111. {
  112. struct brcm_sata_port *port = phy_get_drvdata(phy);
  113. brcm_sata_cfg_ssc(port);
  114. return 0;
  115. }
  116. static const struct phy_ops phy_ops = {
  117. .init = brcm_sata_phy_init,
  118. .owner = THIS_MODULE,
  119. };
  120. static const struct of_device_id brcm_sata_phy_of_match[] = {
  121. { .compatible = "brcm,bcm7445-sata-phy",
  122. .data = (void *)BRCM_SATA_PHY_28NM },
  123. { .compatible = "brcm,bcm7425-sata-phy",
  124. .data = (void *)BRCM_SATA_PHY_40NM },
  125. {},
  126. };
  127. MODULE_DEVICE_TABLE(of, brcm_sata_phy_of_match);
  128. static int brcm_sata_phy_probe(struct platform_device *pdev)
  129. {
  130. struct device *dev = &pdev->dev;
  131. struct device_node *dn = dev->of_node, *child;
  132. const struct of_device_id *of_id;
  133. struct brcm_sata_phy *priv;
  134. struct resource *res;
  135. struct phy_provider *provider;
  136. int ret, count = 0;
  137. if (of_get_child_count(dn) == 0)
  138. return -ENODEV;
  139. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  140. if (!priv)
  141. return -ENOMEM;
  142. dev_set_drvdata(dev, priv);
  143. priv->dev = dev;
  144. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy");
  145. priv->phy_base = devm_ioremap_resource(dev, res);
  146. if (IS_ERR(priv->phy_base))
  147. return PTR_ERR(priv->phy_base);
  148. of_id = of_match_node(brcm_sata_phy_of_match, dn);
  149. if (of_id)
  150. priv->version = (enum brcm_sata_phy_version)of_id->data;
  151. else
  152. priv->version = BRCM_SATA_PHY_28NM;
  153. for_each_available_child_of_node(dn, child) {
  154. unsigned int id;
  155. struct brcm_sata_port *port;
  156. if (of_property_read_u32(child, "reg", &id)) {
  157. dev_err(dev, "missing reg property in node %s\n",
  158. child->name);
  159. ret = -EINVAL;
  160. goto put_child;
  161. }
  162. if (id >= MAX_PORTS) {
  163. dev_err(dev, "invalid reg: %u\n", id);
  164. ret = -EINVAL;
  165. goto put_child;
  166. }
  167. if (priv->phys[id].phy) {
  168. dev_err(dev, "already registered port %u\n", id);
  169. ret = -EINVAL;
  170. goto put_child;
  171. }
  172. port = &priv->phys[id];
  173. port->portnum = id;
  174. port->phy_priv = priv;
  175. port->phy = devm_phy_create(dev, child, &phy_ops);
  176. port->ssc_en = of_property_read_bool(child, "brcm,enable-ssc");
  177. if (IS_ERR(port->phy)) {
  178. dev_err(dev, "failed to create PHY\n");
  179. ret = PTR_ERR(port->phy);
  180. goto put_child;
  181. }
  182. phy_set_drvdata(port->phy, port);
  183. count++;
  184. }
  185. provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  186. if (IS_ERR(provider)) {
  187. dev_err(dev, "could not register PHY provider\n");
  188. return PTR_ERR(provider);
  189. }
  190. dev_info(dev, "registered %d port(s)\n", count);
  191. return 0;
  192. put_child:
  193. of_node_put(child);
  194. return ret;
  195. }
  196. static struct platform_driver brcm_sata_phy_driver = {
  197. .probe = brcm_sata_phy_probe,
  198. .driver = {
  199. .of_match_table = brcm_sata_phy_of_match,
  200. .name = "brcmstb-sata-phy",
  201. }
  202. };
  203. module_platform_driver(brcm_sata_phy_driver);
  204. MODULE_DESCRIPTION("Broadcom STB SATA PHY driver");
  205. MODULE_LICENSE("GPL");
  206. MODULE_AUTHOR("Marc Carino");
  207. MODULE_AUTHOR("Brian Norris");
  208. MODULE_ALIAS("platform:phy-brcmstb-sata");