Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * UFS Host driver for Synopsys Designware Core
0004  *
0005  * Copyright (C) 2015-2016 Synopsys, Inc. (www.synopsys.com)
0006  *
0007  * Authors: Joao Pinto <jpinto@synopsys.com>
0008  */
0009 
0010 #include <linux/module.h>
0011 
0012 #include <ufs/ufshcd.h>
0013 #include <ufs/unipro.h>
0014 
0015 #include "ufshcd-dwc.h"
0016 #include "ufshci-dwc.h"
0017 
0018 int ufshcd_dwc_dme_set_attrs(struct ufs_hba *hba,
0019                 const struct ufshcd_dme_attr_val *v, int n)
0020 {
0021     int ret = 0;
0022     int attr_node = 0;
0023 
0024     for (attr_node = 0; attr_node < n; attr_node++) {
0025         ret = ufshcd_dme_set_attr(hba, v[attr_node].attr_sel,
0026             ATTR_SET_NOR, v[attr_node].mib_val, v[attr_node].peer);
0027 
0028         if (ret)
0029             return ret;
0030     }
0031 
0032     return 0;
0033 }
0034 EXPORT_SYMBOL(ufshcd_dwc_dme_set_attrs);
0035 
0036 /**
0037  * ufshcd_dwc_program_clk_div()
0038  * This function programs the clk divider value. This value is needed to
0039  * provide 1 microsecond tick to unipro layer.
0040  * @hba: Private Structure pointer
0041  * @divider_val: clock divider value to be programmed
0042  *
0043  */
0044 static void ufshcd_dwc_program_clk_div(struct ufs_hba *hba, u32 divider_val)
0045 {
0046     ufshcd_writel(hba, divider_val, DWC_UFS_REG_HCLKDIV);
0047 }
0048 
0049 /**
0050  * ufshcd_dwc_link_is_up()
0051  * Check if link is up
0052  * @hba: private structure pointer
0053  *
0054  * Returns 0 on success, non-zero value on failure
0055  */
0056 static int ufshcd_dwc_link_is_up(struct ufs_hba *hba)
0057 {
0058     int dme_result = 0;
0059 
0060     ufshcd_dme_get(hba, UIC_ARG_MIB(VS_POWERSTATE), &dme_result);
0061 
0062     if (dme_result == UFSHCD_LINK_IS_UP) {
0063         ufshcd_set_link_active(hba);
0064         return 0;
0065     }
0066 
0067     return 1;
0068 }
0069 
0070 /**
0071  * ufshcd_dwc_connection_setup()
0072  * This function configures both the local side (host) and the peer side
0073  * (device) unipro attributes to establish the connection to application/
0074  * cport.
0075  * This function is not required if the hardware is properly configured to
0076  * have this connection setup on reset. But invoking this function does no
0077  * harm and should be fine even working with any ufs device.
0078  *
0079  * @hba: pointer to drivers private data
0080  *
0081  * Returns 0 on success non-zero value on failure
0082  */
0083 static int ufshcd_dwc_connection_setup(struct ufs_hba *hba)
0084 {
0085     static const struct ufshcd_dme_attr_val setup_attrs[] = {
0086         { UIC_ARG_MIB(T_CONNECTIONSTATE), 0, DME_LOCAL },
0087         { UIC_ARG_MIB(N_DEVICEID), 0, DME_LOCAL },
0088         { UIC_ARG_MIB(N_DEVICEID_VALID), 0, DME_LOCAL },
0089         { UIC_ARG_MIB(T_PEERDEVICEID), 1, DME_LOCAL },
0090         { UIC_ARG_MIB(T_PEERCPORTID), 0, DME_LOCAL },
0091         { UIC_ARG_MIB(T_TRAFFICCLASS), 0, DME_LOCAL },
0092         { UIC_ARG_MIB(T_CPORTFLAGS), 0x6, DME_LOCAL },
0093         { UIC_ARG_MIB(T_CPORTMODE), 1, DME_LOCAL },
0094         { UIC_ARG_MIB(T_CONNECTIONSTATE), 1, DME_LOCAL },
0095         { UIC_ARG_MIB(T_CONNECTIONSTATE), 0, DME_PEER },
0096         { UIC_ARG_MIB(N_DEVICEID), 1, DME_PEER },
0097         { UIC_ARG_MIB(N_DEVICEID_VALID), 1, DME_PEER },
0098         { UIC_ARG_MIB(T_PEERDEVICEID), 1, DME_PEER },
0099         { UIC_ARG_MIB(T_PEERCPORTID), 0, DME_PEER },
0100         { UIC_ARG_MIB(T_TRAFFICCLASS), 0, DME_PEER },
0101         { UIC_ARG_MIB(T_CPORTFLAGS), 0x6, DME_PEER },
0102         { UIC_ARG_MIB(T_CPORTMODE), 1, DME_PEER },
0103         { UIC_ARG_MIB(T_CONNECTIONSTATE), 1, DME_PEER }
0104     };
0105 
0106     return ufshcd_dwc_dme_set_attrs(hba, setup_attrs, ARRAY_SIZE(setup_attrs));
0107 }
0108 
0109 /**
0110  * ufshcd_dwc_link_startup_notify()
0111  * UFS Host DWC specific link startup sequence
0112  * @hba: private structure pointer
0113  * @status: Callback notify status
0114  *
0115  * Returns 0 on success, non-zero value on failure
0116  */
0117 int ufshcd_dwc_link_startup_notify(struct ufs_hba *hba,
0118                     enum ufs_notify_change_status status)
0119 {
0120     int err = 0;
0121 
0122     if (status == PRE_CHANGE) {
0123         ufshcd_dwc_program_clk_div(hba, DWC_UFS_REG_HCLKDIV_DIV_125);
0124 
0125         err = ufshcd_vops_phy_initialization(hba);
0126         if (err) {
0127             dev_err(hba->dev, "Phy setup failed (%d)\n", err);
0128             goto out;
0129         }
0130     } else { /* POST_CHANGE */
0131         err = ufshcd_dwc_link_is_up(hba);
0132         if (err) {
0133             dev_err(hba->dev, "Link is not up\n");
0134             goto out;
0135         }
0136 
0137         err = ufshcd_dwc_connection_setup(hba);
0138         if (err)
0139             dev_err(hba->dev, "Connection setup failed (%d)\n",
0140                                     err);
0141     }
0142 
0143 out:
0144     return err;
0145 }
0146 EXPORT_SYMBOL(ufshcd_dwc_link_startup_notify);
0147 
0148 MODULE_AUTHOR("Joao Pinto <Joao.Pinto@synopsys.com>");
0149 MODULE_DESCRIPTION("UFS Host driver for Synopsys Designware Core");
0150 MODULE_LICENSE("Dual BSD/GPL");