The Raspberry Pi CM4 and CM5 have a SYNC_OUT pin, which is brought out onto the official IO boards. This is connected to the BCM54210PE PHY, which has support for PTP. In particular, the PHY allows a PPS signal to be timestamped in hardware with respect to a clock on the PHY. This is supported by Linux. In Linux terms, the clock is called a PTP hardware clock, and the timestamping capability is called external timestamping. This is an extremely unusual capability, and it enables a PPS signal to be timestamped with an accuracy of the order of 10 ns. The normal method of timestamping a PPS signal on a Raspberry Pi is to connect the PPS signal to a GPIO pin, and then use the kernel PPS subsystem to timestamp it; the delay between the occurrence of the physical PPS edge and the kernel timestamping it is of the order of 10 us.

This feature became available in 2022 and was one of my main motivations for starting work on SatPulse. Originally I worked on developing scripts and documentation to make use of this using the existing tools in linuxptp. I found that there were a number of limitations that made this awkward, but that could be cleanly worked around by software that was designed with these limitations in mind.

Over the last four years of working on SatPulse, I have learned a lot more about the PHY. More recently, with the help of AI coding assistants, I have been able to understand the hardware and kernel support much better, to the point where I have been able to submit kernel patches.

So I thought it would be useful to catalogue the various limitations I have found, together with notes on how they can be worked around. By limitation, I mean a way in which it is less than ideal. I should say that I have yet to come across hardware that is ideal in every respect, and indeed such a thing is probably impossible to create. For those interested in precision timing, this capability of the PHY is amazing and, with awareness of the limitations, can be made to work well.

PHC operations go over the slow MDIO bus

External timestamping can be implemented either in the Ethernet PHY or in the Ethernet MAC. In the case of the CM4/CM5, it is implemented in the PHY. The kernel communicates with the PHY over an MDIO bus. This is a slow bus intended for occasional configuration of the PHY for things like link speed. It is separate from the high-speed RGMII interface used for data. With the PTP support on the PHY, it is also used for operations related to the PHC. The basic problem here is that the MDIO bus is really slow and so not well-suited for something which is timing sensitive.

Let me give you an idea of just how slow it is. On the CM5, it runs at 2.08 MHz. Communication on the MDIO bus is done in frames of 64 bits. So each frame takes about 31 us. The PHC registers are in an expanded range which requires two frames to access. Writing a PHC register takes a minimum of about 62 us, just from MDIO bus overhead. Reading needs an additional frame so takes about 92 us. On the CM4, the bus runs at 10 MHz, and MDIO operations are correspondingly faster. When a PHC is in an Ethernet MAC, the time to read or write a register is of the order of 1 us.

One interesting direction would be to try to run the MDIO bus on the CM5 at a higher speed. Currently the speed is set by the macb driver in a generic way so as not to exceed the maximum speed of 2.5 MHz specified by IEEE 802.3. I have done some initial experiments which indicate that a higher speed is likely to work; since the PHY is the same part on the CM4 as on the CM5, the PHY end should not have a problem with the higher speed.

The use of a slow MDIO bus shows up in a number of ways.

  1. Reading the PHC is very slow: using Raspberry Pi kernels, it is 335 us on the CM4 and 2.272 ms on the CM5; on a mainline kernel the CM5 is 861 us. The Raspberry Pi kernel is worse on the CM5 because of a commit, which increases the polling interval for MDIO completion.

  2. Synchronization between the system clock and the PHC is inaccurate as well as slow. Synchronization uses the PTP_SYS_OFFSET_EXTENDED ioctl, which brackets each PHC read with a system clock read. The ioctl allows you to specify the number of samples (i.e. PHC reads); the maximum is PTP_MAX_SAMPLES, which is 25, and several applications default to this. Applications then typically choose the PHC read with the smallest bracket, and use its midpoint as the estimate of the system clock time corresponding to the PHC time. The size of the bracket shows up in chrony’s estimate of the accuracy. In the worst case, a CM5 running a Raspberry Pi kernel, a single PTP_SYS_OFFSET_EXTENDED ioctl call with PTP_MAX_SAMPLES takes about 68 ms. One mitigation is to specify a smaller number of samples: 25 gains no accuracy and costs substantial time. (SatPulse uses 6.) The bigger problem is that there is a systematic bias in the midpoint estimate, which is approximately equal to the transmission time of one MDIO frame. The implication is that by default if you run a combined PTP/NTP server on a CM5 with the Raspberry Pi kernel, you will be serving time that is ahead by about 31 us. The workaround is to specify offset -31e-6 in the chrony refclock line (or offset -7e-6 for a CM4). I have also submitted a patch series to fix this properly, but it is currently stalled; I haven’t given up yet, but it is uncertain whether it will make it into mainline. The idea of the patch series is to enable the PHY driver and MDIO bus driver to cooperate in getting a tighter bracket which is centered around the point where the PHC time is captured. (The analysis in the patch series also shows why the bias is one MDIO frame.) In the meantime, you can try out an earlier version from the raspberrypi/linux PR; this kernel can be installed using the command sudo rpi-update pulls/7673.

  3. Stepping the PHC clock (using ADJ_SETOFFSET) is implemented as a separate read and write of the clock. The effect is that stepping the clock works very inaccurately: in the worst case, the clock will be about 4.7 ms behind the specified time. In SatPulse, I work around this by doing the step twice: I use the first step to estimate the error in the step, and then do a second step to correct it.

  4. The MDIO bus is also used to read the timestamps of transmitted packets, so this is similarly slow. You can work around this in ptp4l by specifying an option of tx_timestamp_timeout 200.

Packet timestamping

  1. The PHY cannot timestamp PTP 2.1 messages. LinuxPTP 4.x sends PTP 2.1 messages by default; the workaround is to specify ptp_minor_version 0 in the ptp4l configuration.

  2. The driver does not compensate for packet ingress/egress latency. You can see this by connecting a CM4/CM5 back to back to a NIC with properly calibrated timestamping, such as an Intel i210 or i226: the path delay minus the cable delay will be about 132 ns instead of 0. Left uncorrected, this will result in a PTP client of a CM4/CM5 grandmaster being ahead by about 132 ns. I estimate ingress latency at 265 ns. The fix is to add ingressLatency 265 to the ptp4l options. (The default egress latency of zero does not need changing.) The estimate is accurate to within 10 ns or so.

  3. The PHY can only timestamp PTP packets. This is a problem when trying to use chrony’s hardware timestamping, which by default needs to be able to timestamp NTP packets. The workaround is to use the NTP over PTP feature (now standardised in RFC 10030). See the documentation for the ptpport directive in chrony.conf for how to enable this.

External timestamping

  1. When the timestamper detects a pulse, it does not raise an interrupt. This requires the driver to periodically poll for timestamps. The driver does this once every 0.25 seconds. The net result is that an external timestamp may be delivered to a user-space application up to 0.25 seconds after the pulse occurred. In the typical usage of external timestamping, the PPS signal from a GNSS receiver is being timestamped, and each pulse has to be correlated with a message from the GNSS receiver (such as NMEA RMC or ZDA) saying which second the pulse is attached to. The variable delay of up to 0.25 seconds can confuse applications that are not prepared for it. SatPulse works around this by reading both the PHC time and the system time when it receives a timestamp, and using the difference between this PHC time and the PHC time in the timestamp to estimate the system time at which the pulse occurs; this can then be used to correlate the pulse with a message based on the system time at which the message was received.

  2. Reading time from the PHC can cause timestamps from external timestamping to be lost. SatPulse works around this by coordinating its reads of the PHC with external timestamping: the PHC is read just after each external timestamp is received. (This is why SatPulse handles generating samples for chrony itself, rather than leaving it to a separate program such as phc2sys.) There are actually two different windows during which a timestamp can be lost. The first is during the clock read itself: the driver has to turn off the recognition of external timestamps while it is reading the clock to avoid an external timestamping edge being mistaken for the time read from the clock. This is a hardware limitation, which I do not think it is possible for a driver to work around. This is a known issue. The second is the interval between when the pulse occurs and when the kernel polls for it: a PHC read during this interval will lose the pending timestamp. This is a new problem I found while working on this blog post: unlike the first window, this is a driver issue that can be fixed.

  3. The PHC time does not update properly when there is no carrier. SatPulse works around this by listening for notifications of changes in the interface status from netlink and then pausing use of the PHC while there is no carrier.

Periodic output

  1. The PHY supports periodic output but only through the same SYNC_OUT pin used for external timestamping. (The CM4 also brings out the SYNC_IN pin but that cannot properly timestamp pulses and is not supported by the driver.) There are some important measurements that need both pins at once: in particular, jumpering SYNC_OUT to SYNC_IN tells you the combined latency of the external timestamping and periodic output paths. Without that, it is very hard to estimate either of these. While writing this blog, I investigated this further, and it turns out that it is possible to implement a loopback mode, where the pulses generated by periodic output are read as external timestamps. Performing this measurement on a CM4 produced a result of 40 ns (which is 5 clock cycles of 8 ns each). How this 40 ns divides between input and output is not measurable. However, generally the input path has greater latency than the output path. The most probable division is 8 ns on the output path and 32 ns on the input path, but 16 ns on the output path and 24 ns on the input path is also plausible.

  2. The maximum pulse width of the periodic output is 4088 ns; this is also the default width. (Pulse widths have to be a multiple of 8 ns, and the requested width is rounded down to the nearest multiple of 8, so a request of up to 4095 will succeed.) This is a very narrow pulse width, and some hardware has difficulty detecting it. (Periodic output in my experience most commonly uses a 50% duty cycle, which is much more convenient.)

Other hardware issues

  1. Broadcom’s documentation is not available publicly. This makes it more difficult to fix problems than it might be. I got an AI agent to do some reverse engineering and create some documentation for the hardware.

  2. On the CM5, the Ethernet interface has two PHCs associated with it that are capable of timestamping packets: the PHC in the MAC (provided by the macb driver) and the PHC in the PHY. The former cannot do external timestamping but it can be synchronized with the system clock much more accurately than the latter. The PHC used by default for packet timestamping is the PHY’s. This can be changed in kernels from 6.14 using ethtool --set-hwtimestamp-cfg. However, this is a very unusual situation (I don’t know of any other case where one network interface has two PHCs) and there is a lot of scope for applications and users to get confused.

  3. The BCM54210PE PHY has the same PHY ID as the PHY in the Raspberry Pi 5, which lacks PTP support. (The PHY ID is supposed to identify the PHY.) The Raspberry Pi kernel has a patch that uses the MDIO address to distinguish them, but this isn’t in mainline. The result is that if mainline is compiled with BCM PHY PTP support enabled, then the Raspberry Pi 5 will get a PHC from a PHY that does not support PTP, and the PHY will then be the default packet timestamper for the interface, which means that PTP will be broken, unless ethtool is used to make packet timestamping use the MAC.

  4. The silkscreen on the CM5 IO board is wrong: it labels pin 9 as SYNC_OUT, but SYNC_OUT is actually pin 6. This is fixed in revision 2 of the board.

Kernel problems

  1. The PTP clock is not attached to its network interface until the network interface is up. With ts2phc this can lead to an error on boot: interface eth0 does not have a PHC. In SatPulse, I work around this by using netlink to wait for the interface to be administratively up, i.e. IFF_UP flag set. This is because MAC drivers attach the PHY only when the kernel opens the interface.

  2. Disabling external timestamping can occasionally deadlock. This leaves the driver in an unusable state and requires a reboot. I am working on a patch for this.

  3. Timestamping in P2P mode does not work. I submitted a patch to fix this. In the meantime, the workaround is to stick to the default E2E mode.

  4. PTP over UDPv6 does not work, in either E2E or P2P mode: when the PHY writes a timestamp into an outgoing message, it sets the UDP checksum to zero.

Updated:

Comments