0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/kernel.h>
0009 #include <linux/module.h>
0010 #include <linux/device.h>
0011 #include <linux/pm.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/libata.h>
0014 #include <linux/ahci_platform.h>
0015
0016 #include "ahci.h"
0017
0018 #define AHCI_DM816_DRV_NAME "ahci-dm816"
0019
0020 #define AHCI_DM816_PHY_ENPLL(x) ((x) << 0)
0021 #define AHCI_DM816_PHY_MPY(x) ((x) << 1)
0022 #define AHCI_DM816_PHY_LOS(x) ((x) << 12)
0023 #define AHCI_DM816_PHY_RXCDR(x) ((x) << 13)
0024 #define AHCI_DM816_PHY_RXEQ(x) ((x) << 16)
0025 #define AHCI_DM816_PHY_TXSWING(x) ((x) << 23)
0026
0027 #define AHCI_DM816_P0PHYCR_REG 0x178
0028 #define AHCI_DM816_P1PHYCR_REG 0x1f8
0029
0030 #define AHCI_DM816_PLL_OUT 1500000000LU
0031
0032 static const unsigned long pll_mpy_table[] = {
0033 400, 500, 600, 800, 825, 1000, 1200,
0034 1250, 1500, 1600, 1650, 2000, 2200, 2500
0035 };
0036
0037 static int ahci_dm816_get_mpy_bits(unsigned long refclk_rate)
0038 {
0039 unsigned long pll_multiplier;
0040 int i;
0041
0042
0043
0044
0045
0046
0047 pll_multiplier = AHCI_DM816_PLL_OUT / (refclk_rate / 100);
0048
0049 for (i = 0; i < ARRAY_SIZE(pll_mpy_table); i++) {
0050 if (pll_mpy_table[i] == pll_multiplier)
0051 return i;
0052 }
0053
0054
0055
0056
0057
0058 return -1;
0059 }
0060
0061 static int ahci_dm816_phy_init(struct ahci_host_priv *hpriv, struct device *dev)
0062 {
0063 unsigned long refclk_rate;
0064 int mpy;
0065 u32 val;
0066
0067
0068
0069
0070
0071
0072 if (!hpriv->clks[1]) {
0073 dev_err(dev, "reference clock not supplied\n");
0074 return -EINVAL;
0075 }
0076
0077 refclk_rate = clk_get_rate(hpriv->clks[1]);
0078 if ((refclk_rate % 100) != 0) {
0079 dev_err(dev, "reference clock rate must be divisible by 100\n");
0080 return -EINVAL;
0081 }
0082
0083 mpy = ahci_dm816_get_mpy_bits(refclk_rate);
0084 if (mpy < 0) {
0085 dev_err(dev, "can't calculate the MPY bits value\n");
0086 return -EINVAL;
0087 }
0088
0089
0090 val = AHCI_DM816_PHY_MPY(mpy) | AHCI_DM816_PHY_LOS(1) |
0091 AHCI_DM816_PHY_RXCDR(4) | AHCI_DM816_PHY_RXEQ(1) |
0092 AHCI_DM816_PHY_TXSWING(3) | AHCI_DM816_PHY_ENPLL(1);
0093 writel(val, hpriv->mmio + AHCI_DM816_P0PHYCR_REG);
0094
0095
0096 val = AHCI_DM816_PHY_LOS(1) | AHCI_DM816_PHY_RXCDR(4) |
0097 AHCI_DM816_PHY_RXEQ(1) | AHCI_DM816_PHY_TXSWING(3);
0098 writel(val, hpriv->mmio + AHCI_DM816_P1PHYCR_REG);
0099
0100 return 0;
0101 }
0102
0103 static int ahci_dm816_softreset(struct ata_link *link,
0104 unsigned int *class, unsigned long deadline)
0105 {
0106 int pmp, ret;
0107
0108 pmp = sata_srst_pmp(link);
0109
0110
0111
0112
0113
0114
0115
0116
0117 ret = ahci_do_softreset(link, class, pmp, deadline, ahci_check_ready);
0118 if (pmp && ret == -EBUSY)
0119 return ahci_do_softreset(link, class, 0,
0120 deadline, ahci_check_ready);
0121
0122 return ret;
0123 }
0124
0125 static struct ata_port_operations ahci_dm816_port_ops = {
0126 .inherits = &ahci_platform_ops,
0127 .softreset = ahci_dm816_softreset,
0128 };
0129
0130 static const struct ata_port_info ahci_dm816_port_info = {
0131 .flags = AHCI_FLAG_COMMON,
0132 .pio_mask = ATA_PIO4,
0133 .udma_mask = ATA_UDMA6,
0134 .port_ops = &ahci_dm816_port_ops,
0135 };
0136
0137 static struct scsi_host_template ahci_dm816_platform_sht = {
0138 AHCI_SHT(AHCI_DM816_DRV_NAME),
0139 };
0140
0141 static int ahci_dm816_probe(struct platform_device *pdev)
0142 {
0143 struct device *dev = &pdev->dev;
0144 struct ahci_host_priv *hpriv;
0145 int rc;
0146
0147 hpriv = ahci_platform_get_resources(pdev, 0);
0148 if (IS_ERR(hpriv))
0149 return PTR_ERR(hpriv);
0150
0151 rc = ahci_platform_enable_resources(hpriv);
0152 if (rc)
0153 return rc;
0154
0155 rc = ahci_dm816_phy_init(hpriv, dev);
0156 if (rc)
0157 goto disable_resources;
0158
0159 rc = ahci_platform_init_host(pdev, hpriv,
0160 &ahci_dm816_port_info,
0161 &ahci_dm816_platform_sht);
0162 if (rc)
0163 goto disable_resources;
0164
0165 return 0;
0166
0167 disable_resources:
0168 ahci_platform_disable_resources(hpriv);
0169
0170 return rc;
0171 }
0172
0173 static SIMPLE_DEV_PM_OPS(ahci_dm816_pm_ops,
0174 ahci_platform_suspend,
0175 ahci_platform_resume);
0176
0177 static const struct of_device_id ahci_dm816_of_match[] = {
0178 { .compatible = "ti,dm816-ahci", },
0179 { }
0180 };
0181 MODULE_DEVICE_TABLE(of, ahci_dm816_of_match);
0182
0183 static struct platform_driver ahci_dm816_driver = {
0184 .probe = ahci_dm816_probe,
0185 .remove = ata_platform_remove_one,
0186 .driver = {
0187 .name = AHCI_DM816_DRV_NAME,
0188 .of_match_table = ahci_dm816_of_match,
0189 .pm = &ahci_dm816_pm_ops,
0190 },
0191 };
0192 module_platform_driver(ahci_dm816_driver);
0193
0194 MODULE_DESCRIPTION("DaVinci DM816 AHCI SATA platform driver");
0195 MODULE_AUTHOR("Bartosz Golaszewski <bgolaszewski@baylibre.com>");
0196 MODULE_LICENSE("GPL");