|
99 | 99 | /* Polling time to wait for fDeviceInit */
|
100 | 100 | #define FDEVICEINIT_COMPL_TIMEOUT 1500 /* millisecs */
|
101 | 101 |
|
| 102 | +/* Default RTC update every 10 seconds */ |
| 103 | +#define UFS_RTC_UPDATE_INTERVAL_MS (10 * MSEC_PER_SEC) |
| 104 | + |
102 | 105 | /* UFSHC 4.0 compliant HC support this mode. */
|
103 | 106 | static bool use_mcq_mode = true;
|
104 | 107 |
|
@@ -235,6 +238,12 @@ ufs_get_desired_pm_lvl_for_dev_link_state(enum ufs_dev_pwr_mode dev_state,
|
235 | 238 | return UFS_PM_LVL_0;
|
236 | 239 | }
|
237 | 240 |
|
| 241 | +static bool ufshcd_is_ufs_dev_busy(struct ufs_hba *hba) |
| 242 | +{ |
| 243 | + return (hba->clk_gating.active_reqs || hba->outstanding_reqs || hba->outstanding_tasks || |
| 244 | + hba->active_uic_cmd || hba->uic_async_done); |
| 245 | +} |
| 246 | + |
238 | 247 | static const struct ufs_dev_quirk ufs_fixups[] = {
|
239 | 248 | /* UFS cards deviations table */
|
240 | 249 | { .wmanufacturerid = UFS_VENDOR_MICRON,
|
@@ -679,6 +688,8 @@ static void ufshcd_device_reset(struct ufs_hba *hba)
|
679 | 688 | hba->dev_info.wb_enabled = false;
|
680 | 689 | hba->dev_info.wb_buf_flush_enabled = false;
|
681 | 690 | }
|
| 691 | + if (hba->dev_info.rtc_type == UFS_RTC_RELATIVE) |
| 692 | + hba->dev_info.rtc_time_baseline = 0; |
682 | 693 | }
|
683 | 694 | if (err != -EOPNOTSUPP)
|
684 | 695 | ufshcd_update_evt_hist(hba, UFS_EVT_DEV_RESET, err);
|
@@ -1919,10 +1930,7 @@ static void ufshcd_gate_work(struct work_struct *work)
|
1919 | 1930 | goto rel_lock;
|
1920 | 1931 | }
|
1921 | 1932 |
|
1922 |
| - if (hba->clk_gating.active_reqs |
1923 |
| - || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL |
1924 |
| - || hba->outstanding_reqs || hba->outstanding_tasks |
1925 |
| - || hba->active_uic_cmd || hba->uic_async_done) |
| 1933 | + if (ufshcd_is_ufs_dev_busy(hba) || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL) |
1926 | 1934 | goto rel_lock;
|
1927 | 1935 |
|
1928 | 1936 | spin_unlock_irqrestore(hba->host->host_lock, flags);
|
@@ -8189,6 +8197,79 @@ static void ufs_fixup_device_setup(struct ufs_hba *hba)
|
8189 | 8197 | ufshcd_vops_fixup_dev_quirks(hba);
|
8190 | 8198 | }
|
8191 | 8199 |
|
| 8200 | +static void ufshcd_update_rtc(struct ufs_hba *hba) |
| 8201 | +{ |
| 8202 | + struct timespec64 ts64; |
| 8203 | + int err; |
| 8204 | + u32 val; |
| 8205 | + |
| 8206 | + ktime_get_real_ts64(&ts64); |
| 8207 | + |
| 8208 | + if (ts64.tv_sec < hba->dev_info.rtc_time_baseline) { |
| 8209 | + dev_warn_once(hba->dev, "%s: Current time precedes previous setting!\n", __func__); |
| 8210 | + return; |
| 8211 | + } |
| 8212 | + |
| 8213 | + /* |
| 8214 | + * The Absolute RTC mode has a 136-year limit, spanning from 2010 to 2146. If a time beyond |
| 8215 | + * 2146 is required, it is recommended to choose the relative RTC mode. |
| 8216 | + */ |
| 8217 | + val = ts64.tv_sec - hba->dev_info.rtc_time_baseline; |
| 8218 | + |
| 8219 | + ufshcd_rpm_get_sync(hba); |
| 8220 | + err = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, QUERY_ATTR_IDN_SECONDS_PASSED, |
| 8221 | + 0, 0, &val); |
| 8222 | + ufshcd_rpm_put_sync(hba); |
| 8223 | + |
| 8224 | + if (err) |
| 8225 | + dev_err(hba->dev, "%s: Failed to update rtc %d\n", __func__, err); |
| 8226 | + else if (hba->dev_info.rtc_type == UFS_RTC_RELATIVE) |
| 8227 | + hba->dev_info.rtc_time_baseline = ts64.tv_sec; |
| 8228 | +} |
| 8229 | + |
| 8230 | +static void ufshcd_rtc_work(struct work_struct *work) |
| 8231 | +{ |
| 8232 | + struct ufs_hba *hba; |
| 8233 | + |
| 8234 | + hba = container_of(to_delayed_work(work), struct ufs_hba, ufs_rtc_update_work); |
| 8235 | + |
| 8236 | + /* Update RTC only when there are no requests in progress and UFSHCI is operational */ |
| 8237 | + if (!ufshcd_is_ufs_dev_busy(hba) && hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) |
| 8238 | + ufshcd_update_rtc(hba); |
| 8239 | + |
| 8240 | + if (ufshcd_is_ufs_dev_active(hba) && hba->dev_info.rtc_update_period) |
| 8241 | + schedule_delayed_work(&hba->ufs_rtc_update_work, |
| 8242 | + msecs_to_jiffies(hba->dev_info.rtc_update_period)); |
| 8243 | +} |
| 8244 | + |
| 8245 | +static void ufs_init_rtc(struct ufs_hba *hba, u8 *desc_buf) |
| 8246 | +{ |
| 8247 | + u16 periodic_rtc_update = get_unaligned_be16(&desc_buf[DEVICE_DESC_PARAM_FRQ_RTC]); |
| 8248 | + struct ufs_dev_info *dev_info = &hba->dev_info; |
| 8249 | + |
| 8250 | + if (periodic_rtc_update & UFS_RTC_TIME_BASELINE) { |
| 8251 | + dev_info->rtc_type = UFS_RTC_ABSOLUTE; |
| 8252 | + |
| 8253 | + /* |
| 8254 | + * The concept of measuring time in Linux as the number of seconds elapsed since |
| 8255 | + * 00:00:00 UTC on January 1, 1970, and UFS ABS RTC is elapsed from January 1st |
| 8256 | + * 2010 00:00, here we need to adjust ABS baseline. |
| 8257 | + */ |
| 8258 | + dev_info->rtc_time_baseline = mktime64(2010, 1, 1, 0, 0, 0) - |
| 8259 | + mktime64(1970, 1, 1, 0, 0, 0); |
| 8260 | + } else { |
| 8261 | + dev_info->rtc_type = UFS_RTC_RELATIVE; |
| 8262 | + dev_info->rtc_time_baseline = 0; |
| 8263 | + } |
| 8264 | + |
| 8265 | + /* |
| 8266 | + * We ignore TIME_PERIOD defined in wPeriodicRTCUpdate because Spec does not clearly state |
| 8267 | + * how to calculate the specific update period for each time unit. And we disable periodic |
| 8268 | + * RTC update work, let user configure by sysfs node according to specific circumstance. |
| 8269 | + */ |
| 8270 | + dev_info->rtc_update_period = 0; |
| 8271 | +} |
| 8272 | + |
8192 | 8273 | static int ufs_get_device_desc(struct ufs_hba *hba)
|
8193 | 8274 | {
|
8194 | 8275 | int err;
|
@@ -8241,6 +8322,8 @@ static int ufs_get_device_desc(struct ufs_hba *hba)
|
8241 | 8322 |
|
8242 | 8323 | ufshcd_temp_notif_probe(hba, desc_buf);
|
8243 | 8324 |
|
| 8325 | + ufs_init_rtc(hba, desc_buf); |
| 8326 | + |
8244 | 8327 | if (hba->ext_iid_sup)
|
8245 | 8328 | ufshcd_ext_iid_probe(hba, desc_buf);
|
8246 | 8329 |
|
@@ -8794,6 +8877,8 @@ static int ufshcd_device_init(struct ufs_hba *hba, bool init_dev_params)
|
8794 | 8877 | ufshcd_force_reset_auto_bkops(hba);
|
8795 | 8878 |
|
8796 | 8879 | ufshcd_set_timestamp_attr(hba);
|
| 8880 | + schedule_delayed_work(&hba->ufs_rtc_update_work, |
| 8881 | + msecs_to_jiffies(UFS_RTC_UPDATE_INTERVAL_MS)); |
8797 | 8882 |
|
8798 | 8883 | /* Gear up to HS gear if supported */
|
8799 | 8884 | if (hba->max_pwr_info.is_valid) {
|
@@ -9751,6 +9836,8 @@ static int __ufshcd_wl_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
|
9751 | 9836 | ret = ufshcd_vops_suspend(hba, pm_op, POST_CHANGE);
|
9752 | 9837 | if (ret)
|
9753 | 9838 | goto set_link_active;
|
| 9839 | + |
| 9840 | + cancel_delayed_work_sync(&hba->ufs_rtc_update_work); |
9754 | 9841 | goto out;
|
9755 | 9842 |
|
9756 | 9843 | set_link_active:
|
@@ -9845,6 +9932,8 @@ static int __ufshcd_wl_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
|
9845 | 9932 | if (ret)
|
9846 | 9933 | goto set_old_link_state;
|
9847 | 9934 | ufshcd_set_timestamp_attr(hba);
|
| 9935 | + schedule_delayed_work(&hba->ufs_rtc_update_work, |
| 9936 | + msecs_to_jiffies(UFS_RTC_UPDATE_INTERVAL_MS)); |
9848 | 9937 | }
|
9849 | 9938 |
|
9850 | 9939 | if (ufshcd_keep_autobkops_enabled_except_suspend(hba))
|
@@ -10541,8 +10630,8 @@ int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
|
10541 | 10630 | UFS_SLEEP_PWR_MODE,
|
10542 | 10631 | UIC_LINK_HIBERN8_STATE);
|
10543 | 10632 |
|
10544 |
| - INIT_DELAYED_WORK(&hba->rpm_dev_flush_recheck_work, |
10545 |
| - ufshcd_rpm_dev_flush_recheck_work); |
| 10633 | + INIT_DELAYED_WORK(&hba->rpm_dev_flush_recheck_work, ufshcd_rpm_dev_flush_recheck_work); |
| 10634 | + INIT_DELAYED_WORK(&hba->ufs_rtc_update_work, ufshcd_rtc_work); |
10546 | 10635 |
|
10547 | 10636 | /* Set the default auto-hiberate idle timer value to 150 ms */
|
10548 | 10637 | if (ufshcd_is_auto_hibern8_supported(hba) && !hba->ahit) {
|
|
0 commit comments