1 // SPDX-License-Identifier: GPL-2.0 2 /* Renesas R-Car Gen4 gPTP device driver 3 * 4 * Copyright (C) 2022 Renesas Electronics Corporation 5 */ 6 7 #include <linux/err.h> 8 #include <linux/etherdevice.h> 9 #include <linux/kernel.h> 10 #include <linux/module.h> 11 #include <linux/platform_device.h> 12 #include <linux/slab.h> 13 14 #include "rcar_gen4_ptp.h" 15 #define ptp_to_priv(ptp) container_of(ptp, struct rcar_gen4_ptp_private, info) 16 17 static const struct rcar_gen4_ptp_reg_offset s4_offs = { 18 .enable = PTPTMEC, 19 .disable = PTPTMDC, 20 .increment = PTPTIVC0, 21 .config_t0 = PTPTOVC00, 22 .config_t1 = PTPTOVC10, 23 .config_t2 = PTPTOVC20, 24 .monitor_t0 = PTPGPTPTM00, 25 .monitor_t1 = PTPGPTPTM10, 26 .monitor_t2 = PTPGPTPTM20, 27 }; 28 29 static int rcar_gen4_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm) 30 { 31 struct rcar_gen4_ptp_private *ptp_priv = ptp_to_priv(ptp); 32 bool neg_adj = scaled_ppm < 0 ? true : false; 33 s64 addend = ptp_priv->default_addend; 34 s64 diff; 35 36 if (neg_adj) 37 scaled_ppm = -scaled_ppm; 38 diff = div_s64(addend * scaled_ppm_to_ppb(scaled_ppm), NSEC_PER_SEC); 39 addend = neg_adj ? addend - diff : addend + diff; 40 41 iowrite32(addend, ptp_priv->addr + ptp_priv->offs->increment); 42 43 return 0; 44 } 45 46 /* Caller must hold the lock */ 47 static void _rcar_gen4_ptp_gettime(struct ptp_clock_info *ptp, 48 struct timespec64 *ts) 49 { 50 struct rcar_gen4_ptp_private *ptp_priv = ptp_to_priv(ptp); 51 52 ts->tv_nsec = ioread32(ptp_priv->addr + ptp_priv->offs->monitor_t0); 53 ts->tv_sec = ioread32(ptp_priv->addr + ptp_priv->offs->monitor_t1) | 54 ((s64)ioread32(ptp_priv->addr + ptp_priv->offs->monitor_t2) << 32); 55 } 56 57 static int rcar_gen4_ptp_gettime(struct ptp_clock_info *ptp, 58 struct timespec64 *ts) 59 { 60 struct rcar_gen4_ptp_private *ptp_priv = ptp_to_priv(ptp); 61 unsigned long flags; 62 63 spin_lock_irqsave(&ptp_priv->lock, flags); 64 _rcar_gen4_ptp_gettime(ptp, ts); 65 spin_unlock_irqrestore(&ptp_priv->lock, flags); 66 67 return 0; 68 } 69 70 /* Caller must hold the lock */ 71 static void _rcar_gen4_ptp_settime(struct ptp_clock_info *ptp, 72 const struct timespec64 *ts) 73 { 74 struct rcar_gen4_ptp_private *ptp_priv = ptp_to_priv(ptp); 75 76 iowrite32(1, ptp_priv->addr + ptp_priv->offs->disable); 77 iowrite32(0, ptp_priv->addr + ptp_priv->offs->config_t2); 78 iowrite32(0, ptp_priv->addr + ptp_priv->offs->config_t1); 79 iowrite32(0, ptp_priv->addr + ptp_priv->offs->config_t0); 80 iowrite32(1, ptp_priv->addr + ptp_priv->offs->enable); 81 iowrite32(ts->tv_sec >> 32, ptp_priv->addr + ptp_priv->offs->config_t2); 82 iowrite32(ts->tv_sec, ptp_priv->addr + ptp_priv->offs->config_t1); 83 iowrite32(ts->tv_nsec, ptp_priv->addr + ptp_priv->offs->config_t0); 84 } 85 86 static int rcar_gen4_ptp_settime(struct ptp_clock_info *ptp, 87 const struct timespec64 *ts) 88 { 89 struct rcar_gen4_ptp_private *ptp_priv = ptp_to_priv(ptp); 90 unsigned long flags; 91 92 spin_lock_irqsave(&ptp_priv->lock, flags); 93 _rcar_gen4_ptp_settime(ptp, ts); 94 spin_unlock_irqrestore(&ptp_priv->lock, flags); 95 96 return 0; 97 } 98 99 static int rcar_gen4_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta) 100 { 101 struct rcar_gen4_ptp_private *ptp_priv = ptp_to_priv(ptp); 102 struct timespec64 ts; 103 unsigned long flags; 104 s64 now; 105 106 spin_lock_irqsave(&ptp_priv->lock, flags); 107 _rcar_gen4_ptp_gettime(ptp, &ts); 108 now = ktime_to_ns(timespec64_to_ktime(ts)); 109 ts = ns_to_timespec64(now + delta); 110 _rcar_gen4_ptp_settime(ptp, &ts); 111 spin_unlock_irqrestore(&ptp_priv->lock, flags); 112 113 return 0; 114 } 115 116 static int rcar_gen4_ptp_enable(struct ptp_clock_info *ptp, 117 struct ptp_clock_request *rq, int on) 118 { 119 return -EOPNOTSUPP; 120 } 121 122 static struct ptp_clock_info rcar_gen4_ptp_info = { 123 .owner = THIS_MODULE, 124 .name = "rcar_gen4_ptp", 125 .max_adj = 50000000, 126 .adjfine = rcar_gen4_ptp_adjfine, 127 .adjtime = rcar_gen4_ptp_adjtime, 128 .gettime64 = rcar_gen4_ptp_gettime, 129 .settime64 = rcar_gen4_ptp_settime, 130 .enable = rcar_gen4_ptp_enable, 131 }; 132 133 static void rcar_gen4_ptp_set_offs(struct rcar_gen4_ptp_private *ptp_priv, 134 enum rcar_gen4_ptp_reg_layout layout) 135 { 136 WARN_ON(layout != RCAR_GEN4_PTP_REG_LAYOUT_S4); 137 138 ptp_priv->offs = &s4_offs; 139 } 140 141 int rcar_gen4_ptp_register(struct rcar_gen4_ptp_private *ptp_priv, 142 enum rcar_gen4_ptp_reg_layout layout, u32 clock) 143 { 144 if (ptp_priv->initialized) 145 return 0; 146 147 spin_lock_init(&ptp_priv->lock); 148 149 rcar_gen4_ptp_set_offs(ptp_priv, layout); 150 151 ptp_priv->default_addend = clock; 152 iowrite32(ptp_priv->default_addend, ptp_priv->addr + ptp_priv->offs->increment); 153 ptp_priv->clock = ptp_clock_register(&ptp_priv->info, NULL); 154 if (IS_ERR(ptp_priv->clock)) 155 return PTR_ERR(ptp_priv->clock); 156 157 iowrite32(0x01, ptp_priv->addr + ptp_priv->offs->enable); 158 ptp_priv->initialized = true; 159 160 return 0; 161 } 162 163 int rcar_gen4_ptp_unregister(struct rcar_gen4_ptp_private *ptp_priv) 164 { 165 iowrite32(1, ptp_priv->addr + ptp_priv->offs->disable); 166 167 return ptp_clock_unregister(ptp_priv->clock); 168 } 169 170 struct rcar_gen4_ptp_private *rcar_gen4_ptp_alloc(struct platform_device *pdev) 171 { 172 struct rcar_gen4_ptp_private *ptp; 173 174 ptp = devm_kzalloc(&pdev->dev, sizeof(*ptp), GFP_KERNEL); 175 if (!ptp) 176 return NULL; 177 178 ptp->info = rcar_gen4_ptp_info; 179 180 return ptp; 181 } 182