|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
| 2 | +/* Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved. */ |
| 3 | + |
| 4 | +#include <linux/io.h> |
| 5 | +#include <linux/kernel.h> |
| 6 | +#include <linux/math64.h> |
| 7 | +#include <linux/mhi.h> |
| 8 | +#include <linux/mod_devicetable.h> |
| 9 | +#include <linux/module.h> |
| 10 | +#include <linux/time64.h> |
| 11 | +#include <linux/timer.h> |
| 12 | + |
| 13 | +#include "qaic.h" |
| 14 | +#include "qaic_timesync.h" |
| 15 | + |
| 16 | +#define QTIMER_REG_OFFSET 0xa28 |
| 17 | +#define QAIC_TIMESYNC_SIGNATURE 0x55aa |
| 18 | +#define QAIC_CONV_QTIMER_TO_US(qtimer) (mul_u64_u32_div(qtimer, 10, 192)) |
| 19 | + |
| 20 | +static unsigned int timesync_delay_ms = 1000; /* 1 sec default */ |
| 21 | +module_param(timesync_delay_ms, uint, 0600); |
| 22 | +MODULE_PARM_DESC(timesync_delay_ms, "Delay in ms between two consecutive timesync operations"); |
| 23 | + |
| 24 | +enum qts_msg_type { |
| 25 | + QAIC_TS_SYNC_REQ = 1, |
| 26 | + QAIC_TS_ACK_TO_HOST, |
| 27 | + QAIC_TS_MSG_TYPE_MAX |
| 28 | +}; |
| 29 | + |
| 30 | +/** |
| 31 | + * struct qts_hdr - Timesync message header structure. |
| 32 | + * @signature: Unique signature to identify the timesync message. |
| 33 | + * @reserved_1: Reserved for future use. |
| 34 | + * @reserved_2: Reserved for future use. |
| 35 | + * @msg_type: sub-type of the timesync message. |
| 36 | + * @reserved_3: Reserved for future use. |
| 37 | + */ |
| 38 | +struct qts_hdr { |
| 39 | + __le16 signature; |
| 40 | + __le16 reserved_1; |
| 41 | + u8 reserved_2; |
| 42 | + u8 msg_type; |
| 43 | + __le16 reserved_3; |
| 44 | +} __packed; |
| 45 | + |
| 46 | +/** |
| 47 | + * struct qts_timeval - Structure to carry time information. |
| 48 | + * @tv_sec: Seconds part of the time. |
| 49 | + * @tv_usec: uS (microseconds) part of the time. |
| 50 | + */ |
| 51 | +struct qts_timeval { |
| 52 | + __le64 tv_sec; |
| 53 | + __le64 tv_usec; |
| 54 | +} __packed; |
| 55 | + |
| 56 | +/** |
| 57 | + * struct qts_host_time_sync_msg_data - Structure to denote the timesync message. |
| 58 | + * @header: Header of the timesync message. |
| 59 | + * @data: Time information. |
| 60 | + */ |
| 61 | +struct qts_host_time_sync_msg_data { |
| 62 | + struct qts_hdr header; |
| 63 | + struct qts_timeval data; |
| 64 | +} __packed; |
| 65 | + |
| 66 | +/** |
| 67 | + * struct mqts_dev - MHI QAIC Timesync Control device. |
| 68 | + * @qdev: Pointer to the root device struct driven by QAIC driver. |
| 69 | + * @mhi_dev: Pointer to associated MHI device. |
| 70 | + * @timer: Timer handle used for timesync. |
| 71 | + * @qtimer_addr: Device QTimer register pointer. |
| 72 | + * @buff_in_use: atomic variable to track if the sync_msg buffer is in use. |
| 73 | + * @dev: Device pointer to qdev->pdev->dev stored for easy access. |
| 74 | + * @sync_msg: Buffer used to send timesync message over MHI. |
| 75 | + */ |
| 76 | +struct mqts_dev { |
| 77 | + struct qaic_device *qdev; |
| 78 | + struct mhi_device *mhi_dev; |
| 79 | + struct timer_list timer; |
| 80 | + void __iomem *qtimer_addr; |
| 81 | + atomic_t buff_in_use; |
| 82 | + struct device *dev; |
| 83 | + struct qts_host_time_sync_msg_data *sync_msg; |
| 84 | +}; |
| 85 | + |
| 86 | +#ifdef readq |
| 87 | +static u64 read_qtimer(const volatile void __iomem *addr) |
| 88 | +{ |
| 89 | + return readq(addr); |
| 90 | +} |
| 91 | +#else |
| 92 | +static u64 read_qtimer(const volatile void __iomem *addr) |
| 93 | +{ |
| 94 | + u64 low, high; |
| 95 | + |
| 96 | + low = readl(addr); |
| 97 | + high = readl(addr + sizeof(u32)); |
| 98 | + return low | (high << 32); |
| 99 | +} |
| 100 | +#endif |
| 101 | + |
| 102 | +static void qaic_timesync_ul_xfer_cb(struct mhi_device *mhi_dev, struct mhi_result *mhi_result) |
| 103 | +{ |
| 104 | + struct mqts_dev *mqtsdev = dev_get_drvdata(&mhi_dev->dev); |
| 105 | + |
| 106 | + dev_dbg(mqtsdev->dev, "%s status: %d xfer_len: %zu\n", __func__, |
| 107 | + mhi_result->transaction_status, mhi_result->bytes_xferd); |
| 108 | + |
| 109 | + atomic_set(&mqtsdev->buff_in_use, 0); |
| 110 | +} |
| 111 | + |
| 112 | +static void qaic_timesync_dl_xfer_cb(struct mhi_device *mhi_dev, struct mhi_result *mhi_result) |
| 113 | +{ |
| 114 | + struct mqts_dev *mqtsdev = dev_get_drvdata(&mhi_dev->dev); |
| 115 | + |
| 116 | + dev_err(mqtsdev->dev, "%s no data expected on dl channel\n", __func__); |
| 117 | +} |
| 118 | + |
| 119 | +static void qaic_timesync_timer(struct timer_list *t) |
| 120 | +{ |
| 121 | + struct mqts_dev *mqtsdev = from_timer(mqtsdev, t, timer); |
| 122 | + struct qts_host_time_sync_msg_data *sync_msg; |
| 123 | + u64 device_qtimer_us; |
| 124 | + u64 device_qtimer; |
| 125 | + u64 host_time_us; |
| 126 | + u64 offset_us; |
| 127 | + u64 host_sec; |
| 128 | + int ret; |
| 129 | + |
| 130 | + if (atomic_read(&mqtsdev->buff_in_use)) { |
| 131 | + dev_dbg(mqtsdev->dev, "%s buffer not free, schedule next cycle\n", __func__); |
| 132 | + goto mod_timer; |
| 133 | + } |
| 134 | + atomic_set(&mqtsdev->buff_in_use, 1); |
| 135 | + |
| 136 | + sync_msg = mqtsdev->sync_msg; |
| 137 | + sync_msg->header.signature = cpu_to_le16(QAIC_TIMESYNC_SIGNATURE); |
| 138 | + sync_msg->header.msg_type = QAIC_TS_SYNC_REQ; |
| 139 | + /* Read host UTC time and convert to uS*/ |
| 140 | + host_time_us = div_u64(ktime_get_real_ns(), NSEC_PER_USEC); |
| 141 | + device_qtimer = read_qtimer(mqtsdev->qtimer_addr); |
| 142 | + device_qtimer_us = QAIC_CONV_QTIMER_TO_US(device_qtimer); |
| 143 | + /* Offset between host UTC and device time */ |
| 144 | + offset_us = host_time_us - device_qtimer_us; |
| 145 | + |
| 146 | + host_sec = div_u64(offset_us, USEC_PER_SEC); |
| 147 | + sync_msg->data.tv_usec = cpu_to_le64(offset_us - host_sec * USEC_PER_SEC); |
| 148 | + sync_msg->data.tv_sec = cpu_to_le64(host_sec); |
| 149 | + ret = mhi_queue_buf(mqtsdev->mhi_dev, DMA_TO_DEVICE, sync_msg, sizeof(*sync_msg), MHI_EOT); |
| 150 | + if (ret && (ret != -EAGAIN)) { |
| 151 | + dev_err(mqtsdev->dev, "%s unable to queue to mhi:%d\n", __func__, ret); |
| 152 | + return; |
| 153 | + } else if (ret == -EAGAIN) { |
| 154 | + atomic_set(&mqtsdev->buff_in_use, 0); |
| 155 | + } |
| 156 | + |
| 157 | +mod_timer: |
| 158 | + ret = mod_timer(t, jiffies + msecs_to_jiffies(timesync_delay_ms)); |
| 159 | + if (ret) |
| 160 | + dev_err(mqtsdev->dev, "%s mod_timer error:%d\n", __func__, ret); |
| 161 | +} |
| 162 | + |
| 163 | +static int qaic_timesync_probe(struct mhi_device *mhi_dev, const struct mhi_device_id *id) |
| 164 | +{ |
| 165 | + struct qaic_device *qdev = pci_get_drvdata(to_pci_dev(mhi_dev->mhi_cntrl->cntrl_dev)); |
| 166 | + struct mqts_dev *mqtsdev; |
| 167 | + struct timer_list *timer; |
| 168 | + int ret; |
| 169 | + |
| 170 | + mqtsdev = kzalloc(sizeof(*mqtsdev), GFP_KERNEL); |
| 171 | + if (!mqtsdev) { |
| 172 | + ret = -ENOMEM; |
| 173 | + goto out; |
| 174 | + } |
| 175 | + |
| 176 | + timer = &mqtsdev->timer; |
| 177 | + mqtsdev->mhi_dev = mhi_dev; |
| 178 | + mqtsdev->qdev = qdev; |
| 179 | + mqtsdev->dev = &qdev->pdev->dev; |
| 180 | + |
| 181 | + mqtsdev->sync_msg = kzalloc(sizeof(*mqtsdev->sync_msg), GFP_KERNEL); |
| 182 | + if (!mqtsdev->sync_msg) { |
| 183 | + ret = -ENOMEM; |
| 184 | + goto free_mqts_dev; |
| 185 | + } |
| 186 | + atomic_set(&mqtsdev->buff_in_use, 0); |
| 187 | + |
| 188 | + ret = mhi_prepare_for_transfer(mhi_dev); |
| 189 | + if (ret) |
| 190 | + goto free_sync_msg; |
| 191 | + |
| 192 | + /* Qtimer register pointer */ |
| 193 | + mqtsdev->qtimer_addr = qdev->bar_0 + QTIMER_REG_OFFSET; |
| 194 | + timer_setup(timer, qaic_timesync_timer, 0); |
| 195 | + timer->expires = jiffies + msecs_to_jiffies(timesync_delay_ms); |
| 196 | + add_timer(timer); |
| 197 | + dev_set_drvdata(&mhi_dev->dev, mqtsdev); |
| 198 | + |
| 199 | + return 0; |
| 200 | + |
| 201 | +free_sync_msg: |
| 202 | + kfree(mqtsdev->sync_msg); |
| 203 | +free_mqts_dev: |
| 204 | + kfree(mqtsdev); |
| 205 | +out: |
| 206 | + return ret; |
| 207 | +}; |
| 208 | + |
| 209 | +static void qaic_timesync_remove(struct mhi_device *mhi_dev) |
| 210 | +{ |
| 211 | + struct mqts_dev *mqtsdev = dev_get_drvdata(&mhi_dev->dev); |
| 212 | + |
| 213 | + del_timer_sync(&mqtsdev->timer); |
| 214 | + mhi_unprepare_from_transfer(mqtsdev->mhi_dev); |
| 215 | + kfree(mqtsdev->sync_msg); |
| 216 | + kfree(mqtsdev); |
| 217 | +} |
| 218 | + |
| 219 | +static const struct mhi_device_id qaic_timesync_match_table[] = { |
| 220 | + { .chan = "QAIC_TIMESYNC_PERIODIC"}, |
| 221 | + {}, |
| 222 | +}; |
| 223 | + |
| 224 | +MODULE_DEVICE_TABLE(mhi, qaic_timesync_match_table); |
| 225 | + |
| 226 | +static struct mhi_driver qaic_timesync_driver = { |
| 227 | + .id_table = qaic_timesync_match_table, |
| 228 | + .remove = qaic_timesync_remove, |
| 229 | + .probe = qaic_timesync_probe, |
| 230 | + .ul_xfer_cb = qaic_timesync_ul_xfer_cb, |
| 231 | + .dl_xfer_cb = qaic_timesync_dl_xfer_cb, |
| 232 | + .driver = { |
| 233 | + .name = "qaic_timesync_periodic", |
| 234 | + }, |
| 235 | +}; |
| 236 | + |
| 237 | +int qaic_timesync_init(void) |
| 238 | +{ |
| 239 | + return mhi_driver_register(&qaic_timesync_driver); |
| 240 | +} |
| 241 | + |
| 242 | +void qaic_timesync_deinit(void) |
| 243 | +{ |
| 244 | + mhi_driver_unregister(&qaic_timesync_driver); |
| 245 | +} |
0 commit comments