blob: ddb9b9e8270d7131866fb9157e5fcc79a7bf0da1 [file] [log] [blame]
Brian Norris0d486802015-05-20 17:18:40 -07001/*
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
17#include <linux/device.h>
18#include <linux/init.h>
19#include <linux/interrupt.h>
20#include <linux/io.h>
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/of.h>
24#include <linux/phy/phy.h>
25#include <linux/platform_device.h>
26
27#define SATA_MDIO_BANK_OFFSET 0x23c
28#define SATA_MDIO_REG_OFFSET(ofs) ((ofs) * 4)
Brian Norris0d486802015-05-20 17:18:40 -070029
30#define MAX_PORTS 2
31
32/* Register offset between PHYs in PCB space */
33#define SATA_MDIO_REG_SPACE_SIZE 0x1000
34
35struct brcm_sata_port {
36 int portnum;
37 struct phy *phy;
38 struct brcm_sata_phy *phy_priv;
39 bool ssc_en;
40};
41
42struct brcm_sata_phy {
43 struct device *dev;
44 void __iomem *phy_base;
45
46 struct brcm_sata_port phys[MAX_PORTS];
47};
48
49enum sata_mdio_phy_regs_28nm {
50 PLL_REG_BANK_0 = 0x50,
51 PLL_REG_BANK_0_PLLCONTROL_0 = 0x81,
52
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
64static inline void __iomem *brcm_sata_phy_base(struct brcm_sata_port *port)
65{
66 struct brcm_sata_phy *priv = port->phy_priv;
67
68 return priv->phy_base + (port->portnum * SATA_MDIO_REG_SPACE_SIZE);
69}
70
71static void brcm_sata_mdio_wr(void __iomem *addr, u32 bank, u32 ofs,
72 u32 msk, u32 value)
73{
74 u32 tmp;
75
76 writel(bank, addr + SATA_MDIO_BANK_OFFSET);
77 tmp = readl(addr + SATA_MDIO_REG_OFFSET(ofs));
78 tmp = (tmp & msk) | value;
79 writel(tmp, addr + SATA_MDIO_REG_OFFSET(ofs));
80}
81
82/* These defaults were characterized by H/W group */
83#define FMIN_VAL_DEFAULT 0x3df
84#define FMAX_VAL_DEFAULT 0x3df
85#define FMAX_VAL_SSC 0x83
86
87static void brcm_sata_cfg_ssc_28nm(struct brcm_sata_port *port)
88{
89 void __iomem *base = brcm_sata_phy_base(port);
90 struct brcm_sata_phy *priv = port->phy_priv;
91 u32 tmp;
92
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
97 /* set fixed min freq */
98 brcm_sata_mdio_wr(base, TXPMD_REG_BANK, TXPMD_TX_FREQ_CTRL_CONTROL2,
99 ~TXPMD_TX_FREQ_CTRL_CONTROL2_FMIN_MASK,
100 FMIN_VAL_DEFAULT);
101
102 /* set fixed max freq depending on SSC config */
103 if (port->ssc_en) {
104 dev_info(priv->dev, "enabling SSC on port %d\n", port->portnum);
105 tmp = FMAX_VAL_SSC;
106 } else {
107 tmp = FMAX_VAL_DEFAULT;
108 }
109
110 brcm_sata_mdio_wr(base, TXPMD_REG_BANK, TXPMD_TX_FREQ_CTRL_CONTROL3,
111 ~TXPMD_TX_FREQ_CTRL_CONTROL3_FMAX_MASK, tmp);
112}
113
114static int brcm_sata_phy_init(struct phy *phy)
115{
116 struct brcm_sata_port *port = phy_get_drvdata(phy);
117
118 brcm_sata_cfg_ssc_28nm(port);
119
120 return 0;
121}
122
Axel Lin4a9e5ca2015-07-15 15:33:51 +0800123static const struct phy_ops phy_ops_28nm = {
Brian Norris0d486802015-05-20 17:18:40 -0700124 .init = brcm_sata_phy_init,
125 .owner = THIS_MODULE,
126};
127
128static const struct of_device_id brcm_sata_phy_of_match[] = {
129 { .compatible = "brcm,bcm7445-sata-phy" },
130 {},
131};
132MODULE_DEVICE_TABLE(of, brcm_sata_phy_of_match);
133
134static int brcm_sata_phy_probe(struct platform_device *pdev)
135{
136 struct device *dev = &pdev->dev;
137 struct device_node *dn = dev->of_node, *child;
138 struct brcm_sata_phy *priv;
139 struct resource *res;
140 struct phy_provider *provider;
Julia Lawall0b25ff82015-11-16 12:33:14 +0100141 int ret, count = 0;
Brian Norris0d486802015-05-20 17:18:40 -0700142
143 if (of_get_child_count(dn) == 0)
144 return -ENODEV;
145
146 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
147 if (!priv)
148 return -ENOMEM;
149 dev_set_drvdata(dev, priv);
150 priv->dev = dev;
151
152 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy");
153 priv->phy_base = devm_ioremap_resource(dev, res);
154 if (IS_ERR(priv->phy_base))
155 return PTR_ERR(priv->phy_base);
156
157 for_each_available_child_of_node(dn, child) {
158 unsigned int id;
159 struct brcm_sata_port *port;
160
161 if (of_property_read_u32(child, "reg", &id)) {
162 dev_err(dev, "missing reg property in node %s\n",
163 child->name);
Julia Lawall0b25ff82015-11-16 12:33:14 +0100164 ret = -EINVAL;
165 goto put_child;
Brian Norris0d486802015-05-20 17:18:40 -0700166 }
167
168 if (id >= MAX_PORTS) {
169 dev_err(dev, "invalid reg: %u\n", id);
Julia Lawall0b25ff82015-11-16 12:33:14 +0100170 ret = -EINVAL;
171 goto put_child;
Brian Norris0d486802015-05-20 17:18:40 -0700172 }
173 if (priv->phys[id].phy) {
174 dev_err(dev, "already registered port %u\n", id);
Julia Lawall0b25ff82015-11-16 12:33:14 +0100175 ret = -EINVAL;
176 goto put_child;
Brian Norris0d486802015-05-20 17:18:40 -0700177 }
178
179 port = &priv->phys[id];
180 port->portnum = id;
181 port->phy_priv = priv;
182 port->phy = devm_phy_create(dev, child, &phy_ops_28nm);
183 port->ssc_en = of_property_read_bool(child, "brcm,enable-ssc");
184 if (IS_ERR(port->phy)) {
185 dev_err(dev, "failed to create PHY\n");
Julia Lawall0b25ff82015-11-16 12:33:14 +0100186 ret = PTR_ERR(port->phy);
187 goto put_child;
Brian Norris0d486802015-05-20 17:18:40 -0700188 }
189
190 phy_set_drvdata(port->phy, port);
191 count++;
192 }
193
194 provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
195 if (IS_ERR(provider)) {
196 dev_err(dev, "could not register PHY provider\n");
197 return PTR_ERR(provider);
198 }
199
200 dev_info(dev, "registered %d port(s)\n", count);
201
202 return 0;
Julia Lawall0b25ff82015-11-16 12:33:14 +0100203put_child:
204 of_node_put(child);
205 return ret;
Brian Norris0d486802015-05-20 17:18:40 -0700206}
207
208static struct platform_driver brcm_sata_phy_driver = {
209 .probe = brcm_sata_phy_probe,
210 .driver = {
211 .of_match_table = brcm_sata_phy_of_match,
212 .name = "brcmstb-sata-phy",
213 }
214};
215module_platform_driver(brcm_sata_phy_driver);
216
217MODULE_DESCRIPTION("Broadcom STB SATA PHY driver");
218MODULE_LICENSE("GPL");
219MODULE_AUTHOR("Marc Carino");
220MODULE_AUTHOR("Brian Norris");
221MODULE_ALIAS("platform:phy-brcmstb-sata");