Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 //
0003 // mcp251xfd - Microchip MCP251xFD Family CAN controller driver
0004 //
0005 // Copyright (c) 2021 Pengutronix,
0006 //               Marc Kleine-Budde <kernel@pengutronix.de>
0007 //
0008 
0009 #include <linux/clocksource.h>
0010 #include <linux/workqueue.h>
0011 
0012 #include "mcp251xfd.h"
0013 
0014 static u64 mcp251xfd_timestamp_read(const struct cyclecounter *cc)
0015 {
0016     const struct mcp251xfd_priv *priv;
0017     u32 timestamp = 0;
0018     int err;
0019 
0020     priv = container_of(cc, struct mcp251xfd_priv, cc);
0021     err = mcp251xfd_get_timestamp(priv, &timestamp);
0022     if (err)
0023         netdev_err(priv->ndev,
0024                "Error %d while reading timestamp. HW timestamps may be inaccurate.",
0025                err);
0026 
0027     return timestamp;
0028 }
0029 
0030 static void mcp251xfd_timestamp_work(struct work_struct *work)
0031 {
0032     struct delayed_work *delayed_work = to_delayed_work(work);
0033     struct mcp251xfd_priv *priv;
0034 
0035     priv = container_of(delayed_work, struct mcp251xfd_priv, timestamp);
0036     timecounter_read(&priv->tc);
0037 
0038     schedule_delayed_work(&priv->timestamp,
0039                   MCP251XFD_TIMESTAMP_WORK_DELAY_SEC * HZ);
0040 }
0041 
0042 void mcp251xfd_skb_set_timestamp(const struct mcp251xfd_priv *priv,
0043                  struct sk_buff *skb, u32 timestamp)
0044 {
0045     struct skb_shared_hwtstamps *hwtstamps = skb_hwtstamps(skb);
0046     u64 ns;
0047 
0048     ns = timecounter_cyc2time(&priv->tc, timestamp);
0049     hwtstamps->hwtstamp = ns_to_ktime(ns);
0050 }
0051 
0052 void mcp251xfd_timestamp_init(struct mcp251xfd_priv *priv)
0053 {
0054     struct cyclecounter *cc = &priv->cc;
0055 
0056     cc->read = mcp251xfd_timestamp_read;
0057     cc->mask = CYCLECOUNTER_MASK(32);
0058     cc->shift = 1;
0059     cc->mult = clocksource_hz2mult(priv->can.clock.freq, cc->shift);
0060 
0061     timecounter_init(&priv->tc, &priv->cc, ktime_get_real_ns());
0062 
0063     INIT_DELAYED_WORK(&priv->timestamp, mcp251xfd_timestamp_work);
0064     schedule_delayed_work(&priv->timestamp,
0065                   MCP251XFD_TIMESTAMP_WORK_DELAY_SEC * HZ);
0066 }
0067 
0068 void mcp251xfd_timestamp_stop(struct mcp251xfd_priv *priv)
0069 {
0070     cancel_delayed_work_sync(&priv->timestamp);
0071 }