Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * DaVinci DM816 AHCI SATA platform driver
0004  *
0005  * Copyright (C) 2017 BayLibre SAS
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      * We need to determine the value of the multiplier (MPY) bits.
0044      * In order to include the 8.25 multiplier we need to first divide
0045      * the refclk rate by 100.
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      * We should have divided evenly - if not, return an invalid
0056      * value.
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      * We should have been supplied two clocks: the functional and
0069      * keep-alive clock and the external reference clock. We need the
0070      * rate of the latter to calculate the correct value of MPY bits.
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     /* Enable the PHY and configure the first HBA port. */
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     /* Configure the second HBA port. */
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      * There's an issue with the SATA controller on DM816 SoC: if we
0112      * enable Port Multiplier support, but the drive is connected directly
0113      * to the board, it can't be detected. As a workaround: if PMP is
0114      * enabled, we first call ahci_do_softreset() and pass it the result of
0115      * sata_srst_pmp(). If this call fails, we retry with pmp = 0.
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     { /* sentinel */ }
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");