mcp251xfd-timestamp.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // mcp251xfd - Microchip MCP251xFD Family CAN controller driver
  4. //
  5. // Copyright (c) 2021 Pengutronix,
  6. // Marc Kleine-Budde <[email protected]>
  7. //
  8. #include <linux/clocksource.h>
  9. #include <linux/workqueue.h>
  10. #include "mcp251xfd.h"
  11. static u64 mcp251xfd_timestamp_read(const struct cyclecounter *cc)
  12. {
  13. const struct mcp251xfd_priv *priv;
  14. u32 timestamp = 0;
  15. int err;
  16. priv = container_of(cc, struct mcp251xfd_priv, cc);
  17. err = mcp251xfd_get_timestamp(priv, &timestamp);
  18. if (err)
  19. netdev_err(priv->ndev,
  20. "Error %d while reading timestamp. HW timestamps may be inaccurate.",
  21. err);
  22. return timestamp;
  23. }
  24. static void mcp251xfd_timestamp_work(struct work_struct *work)
  25. {
  26. struct delayed_work *delayed_work = to_delayed_work(work);
  27. struct mcp251xfd_priv *priv;
  28. priv = container_of(delayed_work, struct mcp251xfd_priv, timestamp);
  29. timecounter_read(&priv->tc);
  30. schedule_delayed_work(&priv->timestamp,
  31. MCP251XFD_TIMESTAMP_WORK_DELAY_SEC * HZ);
  32. }
  33. void mcp251xfd_skb_set_timestamp(const struct mcp251xfd_priv *priv,
  34. struct sk_buff *skb, u32 timestamp)
  35. {
  36. struct skb_shared_hwtstamps *hwtstamps = skb_hwtstamps(skb);
  37. u64 ns;
  38. ns = timecounter_cyc2time(&priv->tc, timestamp);
  39. hwtstamps->hwtstamp = ns_to_ktime(ns);
  40. }
  41. void mcp251xfd_timestamp_init(struct mcp251xfd_priv *priv)
  42. {
  43. struct cyclecounter *cc = &priv->cc;
  44. cc->read = mcp251xfd_timestamp_read;
  45. cc->mask = CYCLECOUNTER_MASK(32);
  46. cc->shift = 1;
  47. cc->mult = clocksource_hz2mult(priv->can.clock.freq, cc->shift);
  48. timecounter_init(&priv->tc, &priv->cc, ktime_get_real_ns());
  49. INIT_DELAYED_WORK(&priv->timestamp, mcp251xfd_timestamp_work);
  50. schedule_delayed_work(&priv->timestamp,
  51. MCP251XFD_TIMESTAMP_WORK_DELAY_SEC * HZ);
  52. }
  53. void mcp251xfd_timestamp_stop(struct mcp251xfd_priv *priv)
  54. {
  55. cancel_delayed_work_sync(&priv->timestamp);
  56. }