| 1 | |
| 2 | |
| 3 | * Copyright (C) 2022 Microchip.
|
| 4 |
|
| 5 |
|
| 6 | #include <linux/device.h>
|
| 7 | #include <linux/kernel.h>
|
| 8 | #include <linux/kthread.h>
|
| 9 | #include <linux/module.h>
|
| 10 | #include <linux/rtc.h>
|
| 11 | #include <linux/tee_drv.h>
|
| 12 |
|
| 13 | #define RTC_INFO_VERSION 0x1
|
| 14 |
|
| 15 | #define TA_RTC_FEATURE_CORRECTION BIT(0)
|
| 16 | #define TA_RTC_FEATURE_ALARM BIT(1)
|
| 17 | #define TA_RTC_FEATURE_WAKEUP_ALARM BIT(2)
|
| 18 |
|
| 19 | enum rtc_optee_pta_cmd {
|
| 20 | |
| 21 | *
|
| 22 | * [out] memref[0] RTC buffer memory reference containing a struct pta_rtc_info
|
| 23 |
|
| 24 | PTA_CMD_RTC_GET_INF = 0x0,
|
| 25 |
|
| 26 | |
| 27 | * PTA_CMD_RTC_GET_TIME - Get time from RTC
|
| 28 | *
|
| 29 | * [out] memref[0] RTC buffer memory reference containing a struct pta_rtc_time
|
| 30 |
|
| 31 | PTA_CMD_RTC_GET_TIME = 0x1,
|
| 32 |
|
| 33 | |
| 34 | * PTA_CMD_RTC_SET_TIME - Set time from RTC
|
| 35 | *
|
| 36 | * [in] memref[0] RTC buffer memory reference containing a struct pta_rtc_time to be
|
| 37 | * used as RTC time
|
| 38 |
|
| 39 | PTA_CMD_RTC_SET_TIME = 0x2,
|
| 40 |
|
| 41 | |
| 42 | * PTA_CMD_RTC_GET_OFFSET - Get RTC offset
|
| 43 | *
|
| 44 | * [out] value[0].a RTC offset (signed 32bit value)
|
| 45 |
|
| 46 | PTA_CMD_RTC_GET_OFFSET = 0x3,
|
| 47 |
|
| 48 | |
| 49 | * PTA_CMD_RTC_SET_OFFSET - Set RTC offset
|
| 50 | *
|
| 51 | * [in] value[0].a RTC offset to be set (signed 32bit value)
|
| 52 |
|
| 53 | PTA_CMD_RTC_SET_OFFSET = 0x4,
|
| 54 |
|
| 55 | |
| 56 | * PTA_CMD_RTC_READ_ALARM - Read RTC alarm
|
| 57 | *
|
| 58 | * [out] memref[0] RTC buffer memory reference containing a struct pta_rtc_alarm
|
| 59 |
|
| 60 | PTA_CMD_RTC_READ_ALARM = 0x5,
|
| 61 |
|
| 62 | |
| 63 | * PTA_CMD_RTC_SET_ALARM - Set RTC alarm
|
| 64 | *
|
| 65 | * [in] memref[0] RTC buffer memory reference containing a struct pta_rtc_alarm to be
|
| 66 | * used as RTC alarm
|
| 67 |
|
| 68 | PTA_CMD_RTC_SET_ALARM = 0x6,
|
| 69 |
|
| 70 | |
| 71 | * PTA_CMD_RTC_ENABLE_ALARM - Enable Alarm
|
| 72 | *
|
| 73 | * [in] value[0].a RTC IRQ flag (uint32_t), 0 to disable the alarm, 1 to enable
|
| 74 |
|
| 75 | PTA_CMD_RTC_ENABLE_ALARM = 0x7,
|
| 76 |
|
| 77 | |
| 78 | * PTA_CMD_RTC_WAIT_ALARM - Get alarm event
|
| 79 | *
|
| 80 | * [out] value[0].a RTC wait alarm return status (uint32_t):
|
| 81 | * - 0: No alarm event
|
| 82 | * - 1: Alarm event occurred
|
| 83 | * - 2: Alarm event canceled
|
| 84 |
|
| 85 | PTA_CMD_RTC_WAIT_ALARM = 0x8,
|
| 86 |
|
| 87 | |
| 88 | * PTA_CMD_RTC_CANCEL_WAIT - Cancel wait for alarm event
|
| 89 |
|
| 90 | PTA_CMD_RTC_CANCEL_WAIT = 0x9,
|
| 91 |
|
| 92 | |
| 93 | * PTA_CMD_RTC_SET_WAKE_ALARM_STATUS - Set RTC wake alarm status flag
|
| 94 | *
|
| 95 | * [in] value[0].a RTC IRQ wake alarm flag (uint32_t), 0 to disable the wake up
|
| 96 | * capability, 1 to enable.
|
| 97 |
|
| 98 | PTA_CMD_RTC_SET_WAKE_ALARM_STATUS = 0xA,
|
| 99 | };
|
| 100 |
|
| 101 | enum rtc_wait_alarm_status {
|
| 102 | WAIT_ALARM_RESET = 0x0,
|
| 103 | WAIT_ALARM_ALARM_OCCURRED = 0x1,
|
| 104 | WAIT_ALARM_CANCELED = 0x2,
|
| 105 | };
|
| 106 |
|
| 107 | struct optee_rtc_time {
|
| 108 | u32 tm_sec;
|
| 109 | u32 tm_min;
|
| 110 | u32 tm_hour;
|
| 111 | u32 tm_mday;
|
| 112 | u32 tm_mon;
|
| 113 | u32 tm_year;
|
| 114 | u32 tm_wday;
|
| 115 | };
|
| 116 |
|
| 117 | struct optee_rtc_alarm {
|
| 118 | u8 enabled;
|
| 119 | u8 pending;
|
| 120 | struct optee_rtc_time time;
|
| 121 | };
|
| 122 |
|
| 123 | struct optee_rtc_info {
|
| 124 | u64 version;
|
| 125 | u64 features;
|
| 126 | struct optee_rtc_time range_min;
|
| 127 | struct optee_rtc_time range_max;
|
| 128 | };
|
| 129 |
|
| 130 | |
| 131 | * struct optee_rtc - OP-TEE RTC private data
|
| 132 | * @dev: OP-TEE based RTC device.
|
| 133 | * @ctx: OP-TEE context handler.
|
| 134 | * @session_id: RTC TA session identifier.
|
| 135 | * @session2_id: RTC wait alarm session identifier.
|
| 136 | * @shm: Memory pool shared with RTC device.
|
| 137 | * @features: Bitfield of RTC features
|
| 138 | * @alarm_task: RTC wait alamr task.
|
| 139 | * @rtc: RTC device.
|
| 140 |
|
| 141 | struct optee_rtc {
|
| 142 | struct device *dev;
|
| 143 | struct tee_context *ctx;
|
| 144 | u32 session_id;
|
| 145 | u32 session2_id;
|
| 146 | struct tee_shm *shm;
|
| 147 | u64 features;
|
| 148 | struct task_struct *alarm_task;
|
| 149 | struct rtc_device *rtc;
|
| 150 | };
|
| 151 |
|
| 152 | static int optee_rtc_readtime(struct device *dev, struct rtc_time *tm)
|
| 153 | {
|
| 154 | struct optee_rtc *priv = dev_get_drvdata(dev);
|
| 155 | struct tee_ioctl_invoke_arg inv_arg = {0};
|
| 156 | struct optee_rtc_time *optee_tm;
|
| 157 | struct tee_param param[4] = {0};
|
| 158 | int ret;
|
| 159 |
|
| 160 | inv_arg.func = PTA_CMD_RTC_GET_TIME;
|
| 161 | inv_arg.session = priv->session_id;
|
| 162 | inv_arg.num_params = 4;
|
| 163 |
|
| 164 |
|
| 165 | param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
|
| 166 | param[0].u.memref.shm = priv->shm;
|
| 167 | param[0].u.memref.size = sizeof(struct optee_rtc_time);
|
| 168 |
|
| 169 | ret = tee_client_invoke_func(priv->ctx, &inv_arg, param);
|
| 170 | if (ret < 0 || inv_arg.ret != 0)
|
| 171 | return ret ? ret : -EPROTO;
|
| 172 |
|
| 173 | optee_tm = tee_shm_get_va(priv->shm, 0);
|
| 174 | if (IS_ERR(optee_tm))
|
| 175 | return PTR_ERR(optee_tm);
|
| 176 |
|
| 177 | if (param[0].u.memref.size != sizeof(*optee_tm))
|
| 178 | return -EPROTO;
|
| 179 |
|
| 180 | tm->tm_sec = optee_tm->tm_sec;
|
| 181 | tm->tm_min = optee_tm->tm_min;
|
| 182 | tm->tm_hour = optee_tm->tm_hour;
|
| 183 | tm->tm_mday = optee_tm->tm_mday;
|
| 184 | tm->tm_mon = optee_tm->tm_mon;
|
| 185 | tm->tm_year = optee_tm->tm_year - 1900;
|
| 186 | tm->tm_wday = optee_tm->tm_wday;
|
| 187 | tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year);
|
| 188 |
|
| 189 | return 0;
|
| 190 | }
|
| 191 |
|
| 192 | static int optee_rtc_settime(struct device *dev, struct rtc_time *tm)
|
| 193 | {
|
| 194 | struct optee_rtc *priv = dev_get_drvdata(dev);
|
| 195 | struct tee_ioctl_invoke_arg inv_arg = {0};
|
| 196 | struct tee_param param[4] = {0};
|
| 197 | struct optee_rtc_time *optee_tm;
|
| 198 | int ret;
|
| 199 |
|
| 200 | inv_arg.func = PTA_CMD_RTC_SET_TIME;
|
| 201 | inv_arg.session = priv->session_id;
|
| 202 | inv_arg.num_params = 4;
|
| 203 |
|
| 204 | param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
|
| 205 | param[0].u.memref.shm = priv->shm;
|
| 206 | param[0].u.memref.size = sizeof(struct optee_rtc_time);
|
| 207 |
|
| 208 | optee_tm = tee_shm_get_va(priv->shm, 0);
|
| 209 | if (IS_ERR(optee_tm))
|
| 210 | return PTR_ERR(optee_tm);
|
| 211 |
|
| 212 | optee_tm->tm_min = tm->tm_min;
|
| 213 | optee_tm->tm_sec = tm->tm_sec;
|
| 214 | optee_tm->tm_hour = tm->tm_hour;
|
| 215 | optee_tm->tm_mday = tm->tm_mday;
|
| 216 | optee_tm->tm_mon = tm->tm_mon;
|
| 217 | optee_tm->tm_year = tm->tm_year + 1900;
|
| 218 | optee_tm->tm_wday = tm->tm_wday;
|
| 219 |
|
| 220 | ret = tee_client_invoke_func(priv->ctx, &inv_arg, param);
|
| 221 | if (ret < 0 || inv_arg.ret != 0)
|
| 222 | return ret ? ret : -EPROTO;
|
| 223 |
|
| 224 | return 0;
|
| 225 | }
|
| 226 |
|
| 227 | static int optee_rtc_readoffset(struct device *dev, long *offset)
|
| 228 | {
|
| 229 | struct optee_rtc *priv = dev_get_drvdata(dev);
|
| 230 | struct tee_ioctl_invoke_arg inv_arg = {0};
|
| 231 | struct tee_param param[4] = {0};
|
| 232 | int ret;
|
| 233 |
|
| 234 | if (!(priv->features & TA_RTC_FEATURE_CORRECTION))
|
| 235 | return -EOPNOTSUPP;
|
| 236 |
|
| 237 | inv_arg.func = PTA_CMD_RTC_GET_OFFSET;
|
| 238 | inv_arg.session = priv->session_id;
|
| 239 | inv_arg.num_params = 4;
|
| 240 |
|
| 241 | param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT;
|
| 242 |
|
| 243 | ret = tee_client_invoke_func(priv->ctx, &inv_arg, param);
|
| 244 | if (ret < 0 || inv_arg.ret != 0)
|
| 245 | return ret ? ret : -EPROTO;
|
| 246 |
|
| 247 | *offset = param[0].u.value.a;
|
| 248 |
|
| 249 | return 0;
|
| 250 | }
|
| 251 |
|
| 252 | static int optee_rtc_setoffset(struct device *dev, long offset)
|
| 253 | {
|
| 254 | struct optee_rtc *priv = dev_get_drvdata(dev);
|
| 255 | struct tee_ioctl_invoke_arg inv_arg = {0};
|
| 256 | struct tee_param param[4] = {0};
|
| 257 | int ret;
|
| 258 |
|
| 259 | if (!(priv->features & TA_RTC_FEATURE_CORRECTION))
|
| 260 | return -EOPNOTSUPP;
|
| 261 |
|
| 262 | inv_arg.func = PTA_CMD_RTC_SET_OFFSET;
|
| 263 | inv_arg.session = priv->session_id;
|
| 264 | inv_arg.num_params = 4;
|
| 265 |
|
| 266 | param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT;
|
| 267 | param[0].u.value.a = offset;
|
| 268 |
|
| 269 | ret = tee_client_invoke_func(priv->ctx, &inv_arg, param);
|
| 270 | if (ret < 0 || inv_arg.ret != 0)
|
| 271 | return ret ? ret : -EPROTO;
|
| 272 |
|
| 273 | return 0;
|
| 274 | }
|
| 275 |
|
| 276 | static int optee_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
|
| 277 | {
|
| 278 | struct optee_rtc *priv = dev_get_drvdata(dev);
|
| 279 | struct tee_ioctl_invoke_arg inv_arg = {0};
|
| 280 | struct optee_rtc_alarm *optee_alarm;
|
| 281 | struct tee_param param[1] = {0};
|
| 282 | int ret;
|
| 283 |
|
| 284 | if (!(priv->features & TA_RTC_FEATURE_ALARM))
|
| 285 | return -EOPNOTSUPP;
|
| 286 |
|
| 287 | inv_arg.func = PTA_CMD_RTC_READ_ALARM;
|
| 288 | inv_arg.session = priv->session_id;
|
| 289 | inv_arg.num_params = 1;
|
| 290 |
|
| 291 |
|
| 292 | param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
|
| 293 | param[0].u.memref.shm = priv->shm;
|
| 294 | param[0].u.memref.size = sizeof(struct optee_rtc_alarm);
|
| 295 |
|
| 296 | ret = tee_client_invoke_func(priv->ctx, &inv_arg, param);
|
| 297 | if (ret < 0 || inv_arg.ret != 0)
|
| 298 | return ret ? ret : -EPROTO;
|
| 299 |
|
| 300 | optee_alarm = tee_shm_get_va(priv->shm, 0);
|
| 301 | if (IS_ERR(optee_alarm))
|
| 302 | return PTR_ERR(optee_alarm);
|
| 303 |
|
| 304 | if (param[0].u.memref.size != sizeof(*optee_alarm))
|
| 305 | return -EPROTO;
|
| 306 |
|
| 307 | alarm->enabled = optee_alarm->enabled;
|
| 308 | alarm->pending = optee_alarm->pending;
|
| 309 | alarm->time.tm_sec = optee_alarm->time.tm_sec;
|
| 310 | alarm->time.tm_min = optee_alarm->time.tm_min;
|
| 311 | alarm->time.tm_hour = optee_alarm->time.tm_hour;
|
| 312 | alarm->time.tm_mday = optee_alarm->time.tm_mday;
|
| 313 | alarm->time.tm_mon = optee_alarm->time.tm_mon;
|
| 314 | alarm->time.tm_year = optee_alarm->time.tm_year - 1900;
|
| 315 | alarm->time.tm_wday = optee_alarm->time.tm_wday;
|
| 316 | alarm->time.tm_yday = rtc_year_days(alarm->time.tm_mday,
|
| 317 | alarm->time.tm_mon,
|
| 318 | alarm->time.tm_year);
|
| 319 |
|
| 320 | return 0;
|
| 321 | }
|
| 322 |
|
| 323 | static int optee_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
|
| 324 | {
|
| 325 | struct optee_rtc *priv = dev_get_drvdata(dev);
|
| 326 | struct tee_ioctl_invoke_arg inv_arg = {0};
|
| 327 | struct optee_rtc_alarm *optee_alarm;
|
| 328 | struct tee_param param[1] = {0};
|
| 329 | int ret;
|
| 330 |
|
| 331 | if (!(priv->features & TA_RTC_FEATURE_ALARM))
|
| 332 | return -EOPNOTSUPP;
|
| 333 |
|
| 334 | inv_arg.func = PTA_CMD_RTC_SET_ALARM;
|
| 335 | inv_arg.session = priv->session_id;
|
| 336 | inv_arg.num_params = 1;
|
| 337 |
|
| 338 | param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
|
| 339 | param[0].u.memref.shm = priv->shm;
|
| 340 | param[0].u.memref.size = sizeof(struct optee_rtc_alarm);
|
| 341 |
|
| 342 | optee_alarm = tee_shm_get_va(priv->shm, 0);
|
| 343 | if (IS_ERR(optee_alarm))
|
| 344 | return PTR_ERR(optee_alarm);
|
| 345 |
|
| 346 | optee_alarm->enabled = alarm->enabled;
|
| 347 | optee_alarm->pending = alarm->pending;
|
| 348 | optee_alarm->time.tm_sec = alarm->time.tm_sec;
|
| 349 | optee_alarm->time.tm_min = alarm->time.tm_min;
|
| 350 | optee_alarm->time.tm_hour = alarm->time.tm_hour;
|
| 351 | optee_alarm->time.tm_mday = alarm->time.tm_mday;
|
| 352 | optee_alarm->time.tm_mon = alarm->time.tm_mon;
|
| 353 | optee_alarm->time.tm_year = alarm->time.tm_year + 1900;
|
| 354 | optee_alarm->time.tm_wday = alarm->time.tm_wday;
|
| 355 |
|
| 356 | ret = tee_client_invoke_func(priv->ctx, &inv_arg, param);
|
| 357 | if (ret < 0 || inv_arg.ret != 0)
|
| 358 | return ret ? ret : -EPROTO;
|
| 359 |
|
| 360 | return 0;
|
| 361 | }
|
| 362 |
|
| 363 | static int optee_rtc_enable_alarm(struct device *dev, unsigned int enabled)
|
| 364 | {
|
| 365 | struct optee_rtc *priv = dev_get_drvdata(dev);
|
| 366 | struct tee_ioctl_invoke_arg inv_arg = {0};
|
| 367 | struct tee_param param[1] = {0};
|
| 368 | int ret;
|
| 369 |
|
| 370 | if (!(priv->features & TA_RTC_FEATURE_ALARM))
|
| 371 | return -EOPNOTSUPP;
|
| 372 |
|
| 373 | inv_arg.func = PTA_CMD_RTC_ENABLE_ALARM;
|
| 374 | inv_arg.session = priv->session_id;
|
| 375 | inv_arg.num_params = 1;
|
| 376 |
|
| 377 | param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT;
|
| 378 | param[0].u.value.a = (bool)enabled;
|
| 379 |
|
| 380 | ret = tee_client_invoke_func(priv->ctx, &inv_arg, param);
|
| 381 | if (ret < 0 || inv_arg.ret != 0)
|
| 382 | return ret ? ret : -EPROTO;
|
| 383 |
|
| 384 | return 0;
|
| 385 | }
|
| 386 |
|
| 387 | static const struct rtc_class_ops optee_rtc_ops = {
|
| 388 | .read_time = optee_rtc_readtime,
|
| 389 | .set_time = optee_rtc_settime,
|
| 390 | .set_offset = optee_rtc_setoffset,
|
| 391 | .read_offset = optee_rtc_readoffset,
|
| 392 | .read_alarm = optee_rtc_read_alarm,
|
| 393 | .set_alarm = optee_rtc_set_alarm,
|
| 394 | .alarm_irq_enable = optee_rtc_enable_alarm,
|
| 395 | };
|
| 396 |
|
| 397 | static int optee_rtc_wait_alarm(struct device *dev, int *return_status)
|
| 398 | {
|
| 399 | struct optee_rtc *priv = dev_get_drvdata(dev);
|
| 400 | struct tee_ioctl_invoke_arg inv_arg = {0};
|
| 401 | struct tee_param param[1] = {0};
|
| 402 | int ret;
|
| 403 |
|
| 404 | if (!(priv->features & TA_RTC_FEATURE_ALARM))
|
| 405 | return -EOPNOTSUPP;
|
| 406 |
|
| 407 | inv_arg.func = PTA_CMD_RTC_WAIT_ALARM;
|
| 408 | inv_arg.session = priv->session2_id;
|
| 409 | inv_arg.num_params = 1;
|
| 410 |
|
| 411 | param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT;
|
| 412 |
|
| 413 | ret = tee_client_invoke_func(priv->ctx, &inv_arg, param);
|
| 414 | if (ret < 0 || inv_arg.ret != 0)
|
| 415 | return ret ? ret : -EPROTO;
|
| 416 |
|
| 417 | *return_status = param[0].u.value.a;
|
| 418 |
|
| 419 | return 0;
|
| 420 | }
|
| 421 |
|
| 422 | static int optee_rtc_cancel_wait_alarm(struct device *dev)
|
| 423 | {
|
| 424 | struct optee_rtc *priv = dev_get_drvdata(dev);
|
| 425 | struct tee_ioctl_invoke_arg inv_arg = {0};
|
| 426 | struct tee_param param[1] = {0};
|
| 427 | int ret;
|
| 428 |
|
| 429 | if (!(priv->features & TA_RTC_FEATURE_ALARM))
|
| 430 | return -EOPNOTSUPP;
|
| 431 |
|
| 432 | inv_arg.func = PTA_CMD_RTC_CANCEL_WAIT;
|
| 433 | inv_arg.session = priv->session_id;
|
| 434 | inv_arg.num_params = 0;
|
| 435 |
|
| 436 | ret = tee_client_invoke_func(priv->ctx, &inv_arg, param);
|
| 437 | if (ret < 0 || inv_arg.ret != 0)
|
| 438 | return ret ? ret : -EPROTO;
|
| 439 |
|
| 440 | return 0;
|
| 441 | }
|
| 442 |
|
| 443 | static int optee_rtc_set_alarm_wake_status(struct device *dev, bool status)
|
| 444 | {
|
| 445 | struct optee_rtc *priv = dev_get_drvdata(dev);
|
| 446 | struct tee_ioctl_invoke_arg inv_arg = {0};
|
| 447 | struct tee_param param[1] = {0};
|
| 448 | int ret;
|
| 449 |
|
| 450 | if (!(priv->features & TA_RTC_FEATURE_ALARM))
|
| 451 | return -EOPNOTSUPP;
|
| 452 |
|
| 453 | inv_arg.func = PTA_CMD_RTC_SET_WAKE_ALARM_STATUS;
|
| 454 | inv_arg.session = priv->session_id;
|
| 455 | inv_arg.num_params = 1;
|
| 456 |
|
| 457 | param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT;
|
| 458 | param[0].u.value.a = status;
|
| 459 |
|
| 460 | ret = tee_client_invoke_func(priv->ctx, &inv_arg, param);
|
| 461 |
|
| 462 | if (ret < 0 || inv_arg.ret != 0)
|
| 463 | return ret ? ret : -EPROTO;
|
| 464 |
|
| 465 | return 0;
|
| 466 | }
|
| 467 |
|
| 468 | static int optee_rtc_handle_alarm_event(void *data)
|
| 469 | {
|
| 470 | struct optee_rtc *priv = (struct optee_rtc *)data;
|
| 471 | int wait_alarm_return_status = 0;
|
| 472 | int ret;
|
| 473 |
|
| 474 | while (!kthread_should_stop()) {
|
| 475 | ret = optee_rtc_wait_alarm(priv->dev, &wait_alarm_return_status);
|
| 476 | if (ret) {
|
| 477 | dev_err(priv->dev, "Failed to wait for alarm: %d\n", ret);
|
| 478 | return ret;
|
| 479 | }
|
| 480 | switch (wait_alarm_return_status) {
|
| 481 | case WAIT_ALARM_ALARM_OCCURRED:
|
| 482 | dev_dbg(priv->dev, "Alarm occurred\n");
|
| 483 | rtc_update_irq(priv->rtc, 1, RTC_IRQF | RTC_AF);
|
| 484 | break;
|
| 485 | case WAIT_ALARM_CANCELED:
|
| 486 | dev_dbg(priv->dev, "Alarm canceled\n");
|
| 487 | break;
|
| 488 | default:
|
| 489 | dev_warn(priv->dev, "Unknown return status: %d\n",
|
| 490 | wait_alarm_return_status);
|
| 491 | break;
|
| 492 | }
|
| 493 | }
|
| 494 |
|
| 495 | return 0;
|
| 496 | }
|
| 497 |
|
| 498 | static int optee_rtc_read_info(struct device *dev, struct rtc_device *rtc,
|
| 499 | u64 *features)
|
| 500 | {
|
| 501 | struct optee_rtc *priv = dev_get_drvdata(dev);
|
| 502 | struct tee_ioctl_invoke_arg inv_arg = {0};
|
| 503 | struct tee_param param[4] = {0};
|
| 504 | struct optee_rtc_info *info;
|
| 505 | struct optee_rtc_time *tm;
|
| 506 | int ret;
|
| 507 |
|
| 508 | inv_arg.func = PTA_CMD_RTC_GET_INF;
|
| 509 | inv_arg.session = priv->session_id;
|
| 510 | inv_arg.num_params = 4;
|
| 511 |
|
| 512 | param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
|
| 513 | param[0].u.memref.shm = priv->shm;
|
| 514 | param[0].u.memref.size = sizeof(*info);
|
| 515 |
|
| 516 | ret = tee_client_invoke_func(priv->ctx, &inv_arg, param);
|
| 517 | if (ret < 0 || inv_arg.ret != 0)
|
| 518 | return ret ? ret : -EPROTO;
|
| 519 |
|
| 520 | info = tee_shm_get_va(priv->shm, 0);
|
| 521 | if (IS_ERR(info))
|
| 522 | return PTR_ERR(info);
|
| 523 |
|
| 524 | if (param[0].u.memref.size != sizeof(*info))
|
| 525 | return -EPROTO;
|
| 526 |
|
| 527 | if (info->version != RTC_INFO_VERSION)
|
| 528 | return -EPROTO;
|
| 529 |
|
| 530 | *features = info->features;
|
| 531 |
|
| 532 | tm = &info->range_min;
|
| 533 | rtc->range_min = mktime64(tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min,
|
| 534 | tm->tm_sec);
|
| 535 | tm = &info->range_max;
|
| 536 | rtc->range_max = mktime64(tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min,
|
| 537 | tm->tm_sec);
|
| 538 |
|
| 539 | return 0;
|
| 540 | }
|
| 541 |
|
| 542 | static int optee_ctx_match(struct tee_ioctl_version_data *ver, const void *data)
|
| 543 | {
|
| 544 | return (ver->impl_id == TEE_IMPL_ID_OPTEE);
|
| 545 | }
|
| 546 |
|
| 547 | static int optee_rtc_probe(struct tee_client_device *rtc_device)
|
| 548 | {
|
| 549 | struct device *dev = &rtc_device->dev;
|
| 550 | struct tee_ioctl_open_session_arg sess2_arg = {0};
|
| 551 | struct tee_ioctl_open_session_arg sess_arg = {0};
|
| 552 | struct optee_rtc *priv;
|
| 553 | struct rtc_device *rtc;
|
| 554 | struct tee_shm *shm;
|
| 555 | int ret, err;
|
| 556 |
|
| 557 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
|
| 558 | if (!priv)
|
| 559 | return -ENOMEM;
|
| 560 |
|
| 561 | rtc = devm_rtc_allocate_device(dev);
|
| 562 | if (IS_ERR(rtc))
|
| 563 | return PTR_ERR(rtc);
|
| 564 |
|
| 565 | priv->rtc = rtc;
|
| 566 |
|
| 567 |
|
| 568 | priv->ctx = tee_client_open_context(NULL, optee_ctx_match, NULL, NULL);
|
| 569 | if (IS_ERR(priv->ctx))
|
| 570 | return -ENODEV;
|
| 571 |
|
| 572 |
|
| 573 | export_uuid(sess_arg.uuid, &rtc_device->id.uuid);
|
| 574 | sess_arg.clnt_login = TEE_IOCTL_LOGIN_REE_KERNEL;
|
| 575 |
|
| 576 | ret = tee_client_open_session(priv->ctx, &sess_arg, NULL);
|
| 577 | if (ret < 0 || sess_arg.ret != 0) {
|
| 578 | dev_err(dev, "tee_client_open_session failed, err: %x\n", sess_arg.ret);
|
| 579 | err = -EINVAL;
|
| 580 | goto out_ctx;
|
| 581 | }
|
| 582 | priv->session_id = sess_arg.session;
|
| 583 |
|
| 584 | |
| 585 | * Shared memory is used for passing an instance of either struct optee_rtc_info,
|
| 586 | * struct optee_rtc_time or struct optee_rtc_alarm to OP-TEE service.
|
| 587 | * The former is by definition large enough to cover both parameter cases.
|
| 588 |
|
| 589 | shm = tee_shm_alloc_kernel_buf(priv->ctx, sizeof(struct optee_rtc_info));
|
| 590 | if (IS_ERR(shm)) {
|
| 591 | dev_err(priv->dev, "tee_shm_alloc_kernel_buf failed\n");
|
| 592 | err = PTR_ERR(shm);
|
| 593 | goto out_sess;
|
| 594 | }
|
| 595 |
|
| 596 | priv->shm = shm;
|
| 597 | priv->dev = dev;
|
| 598 | dev_set_drvdata(dev, priv);
|
| 599 |
|
| 600 | rtc->ops = &optee_rtc_ops;
|
| 601 |
|
| 602 | err = optee_rtc_read_info(dev, rtc, &priv->features);
|
| 603 | if (err) {
|
| 604 | dev_err(dev, "Failed to get RTC features from OP-TEE\n");
|
| 605 | goto out_shm;
|
| 606 | }
|
| 607 |
|
| 608 |
|
| 609 | if (priv->features & TA_RTC_FEATURE_ALARM) {
|
| 610 | priv->alarm_task = kthread_create(optee_rtc_handle_alarm_event,
|
| 611 | priv, "rtc_alarm_evt");
|
| 612 | if (IS_ERR(priv->alarm_task)) {
|
| 613 | dev_err(dev, "Failed to create alarm thread\n");
|
| 614 | err = PTR_ERR(priv->alarm_task);
|
| 615 | goto out_shm;
|
| 616 | }
|
| 617 |
|
| 618 | |
| 619 | * In case of supported alarm feature on optee side, we create a kthread
|
| 620 | * that will, in a new optee session, call a PTA interface "rtc_wait_alarm".
|
| 621 | * This call return in case of alarm and in case of canceled alarm.
|
| 622 | * The new optee session is therefore only needed in this case as we cannot
|
| 623 | * use the same session for parallel calls to optee PTA.
|
| 624 | * Hence one session is reserved to wait for alarms and the other to make
|
| 625 | * standard calls to RTC PTA.
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 | export_uuid(sess2_arg.uuid, &rtc_device->id.uuid);
|
| 630 | sess2_arg.clnt_login = TEE_IOCTL_LOGIN_REE_KERNEL;
|
| 631 |
|
| 632 | ret = tee_client_open_session(priv->ctx, &sess2_arg, NULL);
|
| 633 | if (ret < 0 || sess2_arg.ret != 0) {
|
| 634 | dev_err(dev, "tee_client_open_session failed, err: %x\n", sess2_arg.ret);
|
| 635 | err = -EINVAL;
|
| 636 | goto out_thrd;
|
| 637 | }
|
| 638 | priv->session2_id = sess2_arg.session;
|
| 639 |
|
| 640 | if (priv->features & TA_RTC_FEATURE_WAKEUP_ALARM)
|
| 641 | device_init_wakeup(dev, true);
|
| 642 | }
|
| 643 |
|
| 644 | err = devm_rtc_register_device(rtc);
|
| 645 | if (err)
|
| 646 | goto out_wk;
|
| 647 |
|
| 648 | |
| 649 | * We must clear those bits after registering because registering a rtc_device
|
| 650 | * will set them if it sees that .set_offset and .set_alarm are provided.
|
| 651 |
|
| 652 | if (!(priv->features & TA_RTC_FEATURE_CORRECTION))
|
| 653 | clear_bit(RTC_FEATURE_CORRECTION, rtc->features);
|
| 654 | if (!(priv->features & TA_RTC_FEATURE_ALARM))
|
| 655 | clear_bit(RTC_FEATURE_ALARM, rtc->features);
|
| 656 |
|
| 657 |
|
| 658 | if (priv->alarm_task) {
|
| 659 | wake_up_process(priv->alarm_task);
|
| 660 | dev_dbg(dev, "Wait alarm thread successfully started\n");
|
| 661 | }
|
| 662 |
|
| 663 | return 0;
|
| 664 | out_wk:
|
| 665 | if (priv->features & TA_RTC_FEATURE_ALARM) {
|
| 666 | device_init_wakeup(dev, false);
|
| 667 | tee_client_close_session(priv->ctx, priv->session2_id);
|
| 668 | }
|
| 669 | out_thrd:
|
| 670 | if (priv->features & TA_RTC_FEATURE_ALARM)
|
| 671 | kthread_stop(priv->alarm_task);
|
| 672 | out_shm:
|
| 673 | tee_shm_free(priv->shm);
|
| 674 | out_sess:
|
| 675 | tee_client_close_session(priv->ctx, priv->session_id);
|
| 676 | out_ctx:
|
| 677 | tee_client_close_context(priv->ctx);
|
| 678 |
|
| 679 | return err;
|
| 680 | }
|
| 681 |
|
| 682 | static void optee_rtc_remove(struct tee_client_device *rtc_device)
|
| 683 | {
|
| 684 | struct device *dev = &rtc_device->dev;
|
| 685 | struct optee_rtc *priv = dev_get_drvdata(dev);
|
| 686 |
|
| 687 | if (priv->features & TA_RTC_FEATURE_ALARM) {
|
| 688 | optee_rtc_cancel_wait_alarm(dev);
|
| 689 | kthread_stop(priv->alarm_task);
|
| 690 | device_init_wakeup(dev, false);
|
| 691 | tee_client_close_session(priv->ctx, priv->session2_id);
|
| 692 | }
|
| 693 |
|
| 694 | tee_shm_free(priv->shm);
|
| 695 | tee_client_close_session(priv->ctx, priv->session_id);
|
| 696 | tee_client_close_context(priv->ctx);
|
| 697 | }
|
| 698 |
|
| 699 | static int optee_rtc_suspend(struct device *dev)
|
| 700 | {
|
| 701 | int res = optee_rtc_set_alarm_wake_status(dev, device_may_wakeup(dev));
|
| 702 |
|
| 703 | if (res) {
|
| 704 | dev_err(dev, "Unable to transmit wakeup information to optee rtc\n");
|
| 705 | return res;
|
| 706 | }
|
| 707 |
|
| 708 | return 0;
|
| 709 | }
|
| 710 |
|
| 711 | static DEFINE_SIMPLE_DEV_PM_OPS(optee_rtc_pm_ops, optee_rtc_suspend, NULL);
|
| 712 |
|
| 713 | static const struct tee_client_device_id optee_rtc_id_table[] = {
|
| 714 | {UUID_INIT(0xf389f8c8, 0x845f, 0x496c,
|
| 715 | 0x8b, 0xbe, 0xd6, 0x4b, 0xd2, 0x4c, 0x92, 0xfd)},
|
| 716 | {}
|
| 717 | };
|
| 718 |
|
| 719 | MODULE_DEVICE_TABLE(tee, optee_rtc_id_table);
|
| 720 |
|
| 721 | static struct tee_client_driver optee_rtc_driver = {
|
| 722 | .id_table = optee_rtc_id_table,
|
| 723 | .probe = optee_rtc_probe,
|
| 724 | .remove = optee_rtc_remove,
|
| 725 | .driver = {
|
| 726 | .name = "optee_rtc",
|
| 727 | .pm = pm_sleep_ptr(&optee_rtc_pm_ops),
|
| 728 | },
|
| 729 | };
|
| 730 |
|
| 731 | module_tee_client_driver(optee_rtc_driver);
|
| 732 |
|
| 733 | MODULE_LICENSE("GPL v2");
|
| 734 | MODULE_AUTHOR("Clément Léger <[email protected]>");
|
| 735 | MODULE_DESCRIPTION("OP-TEE based RTC driver");
|