1 // SPDX-License-Identifier: GPL-2.0+ 2 /* PTP 1588 clock using the Renesas Ethernet AVB 3 * 4 * Copyright (C) 2013-2015 Renesas Electronics Corporation 5 * Copyright (C) 2015 Renesas Solutions Corp. 6 * Copyright (C) 2015-2016 Cogent Embedded, Inc. <source@cogentembedded.com> 7 */ 8 9 #include "ravb.h" 10 11 static int ravb_ptp_tcr_request(struct ravb_private *priv, u32 request) 12 { 13 struct net_device *ndev = priv->ndev; 14 int error; 15 16 error = ravb_wait(ndev, GCCR, GCCR_TCR, GCCR_TCR_NOREQ); 17 if (error) 18 return error; 19 20 ravb_modify(ndev, GCCR, request, request); 21 return ravb_wait(ndev, GCCR, GCCR_TCR, GCCR_TCR_NOREQ); 22 } 23 24 /* Caller must hold the lock */ 25 static int ravb_ptp_time_read(struct ravb_private *priv, struct timespec64 *ts) 26 { 27 struct net_device *ndev = priv->ndev; 28 int error; 29 30 error = ravb_ptp_tcr_request(priv, GCCR_TCR_CAPTURE); 31 if (error) 32 return error; 33 34 ts->tv_nsec = ravb_read(ndev, GCT0); 35 ts->tv_sec = ravb_read(ndev, GCT1) | 36 ((s64)ravb_read(ndev, GCT2) << 32); 37 38 return 0; 39 } 40 41 /* Caller must hold the lock */ 42 static int ravb_ptp_time_write(struct ravb_private *priv, 43 const struct timespec64 *ts) 44 { 45 struct net_device *ndev = priv->ndev; 46 int error; 47 u32 gccr; 48 49 error = ravb_ptp_tcr_request(priv, GCCR_TCR_RESET); 50 if (error) 51 return error; 52 53 gccr = ravb_read(ndev, GCCR); 54 if (gccr & GCCR_LTO) 55 return -EBUSY; 56 ravb_write(ndev, ts->tv_nsec, GTO0); 57 ravb_write(ndev, ts->tv_sec, GTO1); 58 ravb_write(ndev, (ts->tv_sec >> 32) & 0xffff, GTO2); 59 ravb_write(ndev, gccr | GCCR_LTO, GCCR); 60 61 return 0; 62 } 63 64 /* Caller must hold the lock */ 65 static int ravb_ptp_update_compare(struct ravb_private *priv, u32 ns) 66 { 67 struct net_device *ndev = priv->ndev; 68 /* When the comparison value (GPTC.PTCV) is in range of 69 * [x-1 to x+1] (x is the configured increment value in 70 * GTI.TIV), it may happen that a comparison match is 71 * not detected when the timer wraps around. 72 */ 73 u32 gti_ns_plus_1 = (priv->ptp.current_addend >> 20) + 1; 74 u32 gccr; 75 76 if (ns < gti_ns_plus_1) 77 ns = gti_ns_plus_1; 78 else if (ns > 0 - gti_ns_plus_1) 79 ns = 0 - gti_ns_plus_1; 80 81 gccr = ravb_read(ndev, GCCR); 82 if (gccr & GCCR_LPTC) 83 return -EBUSY; 84 ravb_write(ndev, ns, GPTC); 85 ravb_write(ndev, gccr | GCCR_LPTC, GCCR); 86 87 return 0; 88 } 89 90 /* PTP clock operations */ 91 static int ravb_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm) 92 { 93 struct ravb_private *priv = container_of(ptp, struct ravb_private, 94 ptp.info); 95 struct net_device *ndev = priv->ndev; 96 unsigned long flags; 97 u32 addend; 98 u32 gccr; 99 100 addend = (u32)adjust_by_scaled_ppm(priv->ptp.default_addend, 101 scaled_ppm); 102 103 spin_lock_irqsave(&priv->lock, flags); 104 105 priv->ptp.current_addend = addend; 106 107 gccr = ravb_read(ndev, GCCR); 108 if (gccr & GCCR_LTI) { 109 spin_unlock_irqrestore(&priv->lock, flags); 110 return -EBUSY; 111 } 112 ravb_write(ndev, addend & GTI_TIV, GTI); 113 ravb_write(ndev, gccr | GCCR_LTI, GCCR); 114 115 spin_unlock_irqrestore(&priv->lock, flags); 116 117 return 0; 118 } 119 120 static int ravb_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta) 121 { 122 struct ravb_private *priv = container_of(ptp, struct ravb_private, 123 ptp.info); 124 struct timespec64 ts; 125 unsigned long flags; 126 int error; 127 128 spin_lock_irqsave(&priv->lock, flags); 129 error = ravb_ptp_time_read(priv, &ts); 130 if (!error) { 131 u64 now = ktime_to_ns(timespec64_to_ktime(ts)); 132 133 ts = ns_to_timespec64(now + delta); 134 error = ravb_ptp_time_write(priv, &ts); 135 } 136 spin_unlock_irqrestore(&priv->lock, flags); 137 138 return error; 139 } 140 141 static int ravb_ptp_gettime64(struct ptp_clock_info *ptp, struct timespec64 *ts) 142 { 143 struct ravb_private *priv = container_of(ptp, struct ravb_private, 144 ptp.info); 145 unsigned long flags; 146 int error; 147 148 spin_lock_irqsave(&priv->lock, flags); 149 error = ravb_ptp_time_read(priv, ts); 150 spin_unlock_irqrestore(&priv->lock, flags); 151 152 return error; 153 } 154 155 static int ravb_ptp_settime64(struct ptp_clock_info *ptp, 156 const struct timespec64 *ts) 157 { 158 struct ravb_private *priv = container_of(ptp, struct ravb_private, 159 ptp.info); 160 unsigned long flags; 161 int error; 162 163 spin_lock_irqsave(&priv->lock, flags); 164 error = ravb_ptp_time_write(priv, ts); 165 spin_unlock_irqrestore(&priv->lock, flags); 166 167 return error; 168 } 169 170 static int ravb_ptp_extts(struct ptp_clock_info *ptp, 171 struct ptp_extts_request *req, int on) 172 { 173 struct ravb_private *priv = container_of(ptp, struct ravb_private, 174 ptp.info); 175 const struct ravb_hw_info *info = priv->info; 176 struct net_device *ndev = priv->ndev; 177 unsigned long flags; 178 179 if (req->index) 180 return -EINVAL; 181 182 if (priv->ptp.extts[req->index] == on) 183 return 0; 184 priv->ptp.extts[req->index] = on; 185 186 spin_lock_irqsave(&priv->lock, flags); 187 if (!info->irq_en_dis) 188 ravb_modify(ndev, GIC, GIC_PTCE, on ? GIC_PTCE : 0); 189 else if (on) 190 ravb_write(ndev, GIE_PTCS, GIE); 191 else 192 ravb_write(ndev, GID_PTCD, GID); 193 spin_unlock_irqrestore(&priv->lock, flags); 194 195 return 0; 196 } 197 198 static int ravb_ptp_perout(struct ptp_clock_info *ptp, 199 struct ptp_perout_request *req, int on) 200 { 201 struct ravb_private *priv = container_of(ptp, struct ravb_private, 202 ptp.info); 203 const struct ravb_hw_info *info = priv->info; 204 struct net_device *ndev = priv->ndev; 205 struct ravb_ptp_perout *perout; 206 unsigned long flags; 207 int error = 0; 208 209 if (req->index) 210 return -EINVAL; 211 212 if (on) { 213 u64 start_ns; 214 u64 period_ns; 215 216 start_ns = req->start.sec * NSEC_PER_SEC + req->start.nsec; 217 period_ns = req->period.sec * NSEC_PER_SEC + req->period.nsec; 218 219 if (start_ns > U32_MAX) { 220 netdev_warn(ndev, 221 "ptp: start value (nsec) is over limit. Maximum size of start is only 32 bits\n"); 222 return -ERANGE; 223 } 224 225 if (period_ns > U32_MAX) { 226 netdev_warn(ndev, 227 "ptp: period value (nsec) is over limit. Maximum size of period is only 32 bits\n"); 228 return -ERANGE; 229 } 230 231 spin_lock_irqsave(&priv->lock, flags); 232 233 perout = &priv->ptp.perout[req->index]; 234 perout->target = (u32)start_ns; 235 perout->period = (u32)period_ns; 236 error = ravb_ptp_update_compare(priv, (u32)start_ns); 237 if (!error) { 238 /* Unmask interrupt */ 239 if (!info->irq_en_dis) 240 ravb_modify(ndev, GIC, GIC_PTME, GIC_PTME); 241 else 242 ravb_write(ndev, GIE_PTMS0, GIE); 243 } 244 } else { 245 spin_lock_irqsave(&priv->lock, flags); 246 247 perout = &priv->ptp.perout[req->index]; 248 perout->period = 0; 249 250 /* Mask interrupt */ 251 if (!info->irq_en_dis) 252 ravb_modify(ndev, GIC, GIC_PTME, 0); 253 else 254 ravb_write(ndev, GID_PTMD0, GID); 255 } 256 spin_unlock_irqrestore(&priv->lock, flags); 257 258 return error; 259 } 260 261 static int ravb_ptp_enable(struct ptp_clock_info *ptp, 262 struct ptp_clock_request *req, int on) 263 { 264 switch (req->type) { 265 case PTP_CLK_REQ_EXTTS: 266 return ravb_ptp_extts(ptp, &req->extts, on); 267 case PTP_CLK_REQ_PEROUT: 268 return ravb_ptp_perout(ptp, &req->perout, on); 269 default: 270 return -EOPNOTSUPP; 271 } 272 } 273 274 static const struct ptp_clock_info ravb_ptp_info = { 275 .owner = THIS_MODULE, 276 .name = "ravb clock", 277 .max_adj = 50000000, 278 .n_ext_ts = N_EXT_TS, 279 .n_per_out = N_PER_OUT, 280 .supported_extts_flags = PTP_RISING_EDGE | PTP_FALLING_EDGE, 281 .adjfine = ravb_ptp_adjfine, 282 .adjtime = ravb_ptp_adjtime, 283 .gettime64 = ravb_ptp_gettime64, 284 .settime64 = ravb_ptp_settime64, 285 .enable = ravb_ptp_enable, 286 }; 287 288 /* Caller must hold the lock */ 289 void ravb_ptp_interrupt(struct net_device *ndev) 290 { 291 struct ravb_private *priv = netdev_priv(ndev); 292 u32 gis = ravb_read(ndev, GIS); 293 294 gis &= ravb_read(ndev, GIC); 295 if (gis & GIS_PTCF) { 296 struct ptp_clock_event event; 297 298 event.type = PTP_CLOCK_EXTTS; 299 event.index = 0; 300 event.timestamp = ravb_read(ndev, GCPT); 301 ptp_clock_event(priv->ptp.clock, &event); 302 } 303 if (gis & GIS_PTMF) { 304 struct ravb_ptp_perout *perout = priv->ptp.perout; 305 306 if (perout->period) { 307 perout->target += perout->period; 308 ravb_ptp_update_compare(priv, perout->target); 309 } 310 } 311 312 ravb_write(ndev, ~(gis | GIS_RESERVED), GIS); 313 } 314 315 void ravb_ptp_init(struct net_device *ndev, struct platform_device *pdev) 316 { 317 struct ravb_private *priv = netdev_priv(ndev); 318 unsigned long flags; 319 320 priv->ptp.info = ravb_ptp_info; 321 322 priv->ptp.default_addend = ravb_read(ndev, GTI); 323 priv->ptp.current_addend = priv->ptp.default_addend; 324 325 spin_lock_irqsave(&priv->lock, flags); 326 ravb_wait(ndev, GCCR, GCCR_TCR, GCCR_TCR_NOREQ); 327 ravb_modify(ndev, GCCR, GCCR_TCSS, GCCR_TCSS_ADJGPTP); 328 spin_unlock_irqrestore(&priv->lock, flags); 329 330 priv->ptp.clock = ptp_clock_register(&priv->ptp.info, &pdev->dev); 331 } 332 333 void ravb_ptp_stop(struct net_device *ndev) 334 { 335 struct ravb_private *priv = netdev_priv(ndev); 336 337 ravb_write(ndev, 0, GIC); 338 ravb_write(ndev, 0, GIS); 339 340 ptp_clock_unregister(priv->ptp.clock); 341 } 342