phy-brcmstb-sata.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 SATA_MDIO_REG_SPACE_SIZE 0x1000
  28. #define SATA_MDIO_REG_LENGTH 0x1f00
  29. #define MAX_PORTS 2
  30. /* Register offset between PHYs in PCB space */
  31. #define SATA_MDIO_REG_SPACE_SIZE 0x1000
  32. struct brcm_sata_port {
  33. int portnum;
  34. struct phy *phy;
  35. struct brcm_sata_phy *phy_priv;
  36. bool ssc_en;
  37. };
  38. struct brcm_sata_phy {
  39. struct device *dev;
  40. void __iomem *phy_base;
  41. struct brcm_sata_port phys[MAX_PORTS];
  42. };
  43. enum sata_mdio_phy_regs_28nm {
  44. PLL_REG_BANK_0 = 0x50,
  45. PLL_REG_BANK_0_PLLCONTROL_0 = 0x81,
  46. TXPMD_REG_BANK = 0x1a0,
  47. TXPMD_CONTROL1 = 0x81,
  48. TXPMD_CONTROL1_TX_SSC_EN_FRC = BIT(0),
  49. TXPMD_CONTROL1_TX_SSC_EN_FRC_VAL = BIT(1),
  50. TXPMD_TX_FREQ_CTRL_CONTROL1 = 0x82,
  51. TXPMD_TX_FREQ_CTRL_CONTROL2 = 0x83,
  52. TXPMD_TX_FREQ_CTRL_CONTROL2_FMIN_MASK = 0x3ff,
  53. TXPMD_TX_FREQ_CTRL_CONTROL3 = 0x84,
  54. TXPMD_TX_FREQ_CTRL_CONTROL3_FMAX_MASK = 0x3ff,
  55. };
  56. static inline void __iomem *brcm_sata_phy_base(struct brcm_sata_port *port)
  57. {
  58. struct brcm_sata_phy *priv = port->phy_priv;
  59. return priv->phy_base + (port->portnum * SATA_MDIO_REG_SPACE_SIZE);
  60. }
  61. static void brcm_sata_mdio_wr(void __iomem *addr, u32 bank, u32 ofs,
  62. u32 msk, u32 value)
  63. {
  64. u32 tmp;
  65. writel(bank, addr + SATA_MDIO_BANK_OFFSET);
  66. tmp = readl(addr + SATA_MDIO_REG_OFFSET(ofs));
  67. tmp = (tmp & msk) | value;
  68. writel(tmp, addr + SATA_MDIO_REG_OFFSET(ofs));
  69. }
  70. /* These defaults were characterized by H/W group */
  71. #define FMIN_VAL_DEFAULT 0x3df
  72. #define FMAX_VAL_DEFAULT 0x3df
  73. #define FMAX_VAL_SSC 0x83
  74. static void brcm_sata_cfg_ssc_28nm(struct brcm_sata_port *port)
  75. {
  76. void __iomem *base = brcm_sata_phy_base(port);
  77. struct brcm_sata_phy *priv = port->phy_priv;
  78. u32 tmp;
  79. /* override the TX spread spectrum setting */
  80. tmp = TXPMD_CONTROL1_TX_SSC_EN_FRC_VAL | TXPMD_CONTROL1_TX_SSC_EN_FRC;
  81. brcm_sata_mdio_wr(base, TXPMD_REG_BANK, TXPMD_CONTROL1, ~tmp, tmp);
  82. /* set fixed min freq */
  83. brcm_sata_mdio_wr(base, TXPMD_REG_BANK, TXPMD_TX_FREQ_CTRL_CONTROL2,
  84. ~TXPMD_TX_FREQ_CTRL_CONTROL2_FMIN_MASK,
  85. FMIN_VAL_DEFAULT);
  86. /* set fixed max freq depending on SSC config */
  87. if (port->ssc_en) {
  88. dev_info(priv->dev, "enabling SSC on port %d\n", port->portnum);
  89. tmp = FMAX_VAL_SSC;
  90. } else {
  91. tmp = FMAX_VAL_DEFAULT;
  92. }
  93. brcm_sata_mdio_wr(base, TXPMD_REG_BANK, TXPMD_TX_FREQ_CTRL_CONTROL3,
  94. ~TXPMD_TX_FREQ_CTRL_CONTROL3_FMAX_MASK, tmp);
  95. }
  96. static int brcm_sata_phy_init(struct phy *phy)
  97. {
  98. struct brcm_sata_port *port = phy_get_drvdata(phy);
  99. brcm_sata_cfg_ssc_28nm(port);
  100. return 0;
  101. }
  102. static const struct phy_ops phy_ops_28nm = {
  103. .init = brcm_sata_phy_init,
  104. .owner = THIS_MODULE,
  105. };
  106. static const struct of_device_id brcm_sata_phy_of_match[] = {
  107. { .compatible = "brcm,bcm7445-sata-phy" },
  108. {},
  109. };
  110. MODULE_DEVICE_TABLE(of, brcm_sata_phy_of_match);
  111. static int brcm_sata_phy_probe(struct platform_device *pdev)
  112. {
  113. struct device *dev = &pdev->dev;
  114. struct device_node *dn = dev->of_node, *child;
  115. struct brcm_sata_phy *priv;
  116. struct resource *res;
  117. struct phy_provider *provider;
  118. int ret, count = 0;
  119. if (of_get_child_count(dn) == 0)
  120. return -ENODEV;
  121. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  122. if (!priv)
  123. return -ENOMEM;
  124. dev_set_drvdata(dev, priv);
  125. priv->dev = dev;
  126. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy");
  127. priv->phy_base = devm_ioremap_resource(dev, res);
  128. if (IS_ERR(priv->phy_base))
  129. return PTR_ERR(priv->phy_base);
  130. for_each_available_child_of_node(dn, child) {
  131. unsigned int id;
  132. struct brcm_sata_port *port;
  133. if (of_property_read_u32(child, "reg", &id)) {
  134. dev_err(dev, "missing reg property in node %s\n",
  135. child->name);
  136. ret = -EINVAL;
  137. goto put_child;
  138. }
  139. if (id >= MAX_PORTS) {
  140. dev_err(dev, "invalid reg: %u\n", id);
  141. ret = -EINVAL;
  142. goto put_child;
  143. }
  144. if (priv->phys[id].phy) {
  145. dev_err(dev, "already registered port %u\n", id);
  146. ret = -EINVAL;
  147. goto put_child;
  148. }
  149. port = &priv->phys[id];
  150. port->portnum = id;
  151. port->phy_priv = priv;
  152. port->phy = devm_phy_create(dev, child, &phy_ops_28nm);
  153. port->ssc_en = of_property_read_bool(child, "brcm,enable-ssc");
  154. if (IS_ERR(port->phy)) {
  155. dev_err(dev, "failed to create PHY\n");
  156. ret = PTR_ERR(port->phy);
  157. goto put_child;
  158. }
  159. phy_set_drvdata(port->phy, port);
  160. count++;
  161. }
  162. provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  163. if (IS_ERR(provider)) {
  164. dev_err(dev, "could not register PHY provider\n");
  165. return PTR_ERR(provider);
  166. }
  167. dev_info(dev, "registered %d port(s)\n", count);
  168. return 0;
  169. put_child:
  170. of_node_put(child);
  171. return ret;
  172. }
  173. static struct platform_driver brcm_sata_phy_driver = {
  174. .probe = brcm_sata_phy_probe,
  175. .driver = {
  176. .of_match_table = brcm_sata_phy_of_match,
  177. .name = "brcmstb-sata-phy",
  178. }
  179. };
  180. module_platform_driver(brcm_sata_phy_driver);
  181. MODULE_DESCRIPTION("Broadcom STB SATA PHY driver");
  182. MODULE_LICENSE("GPL");
  183. MODULE_AUTHOR("Marc Carino");
  184. MODULE_AUTHOR("Brian Norris");
  185. MODULE_ALIAS("platform:phy-brcmstb-sata");