ptp: ocp: add TAP CPLD support for ADVA TimeCard X1
v11 cover ptp: ocp: add TAP CPLD support for ADVA TimeCard X1
The ADVA TimeCard X1 (PCI device 0x0410) carries a Lattice MachXO3 CPLD on the TMC I2C bus. This series adds access to it and an update path through the kernel firmware-upload subsystem.
The card has two I2C controllers. Linux registers only the one at 0x00150000, normally routed to the config EEPROMs; the one at 0x00120000 belongs to the on-card MicroBlaze and is routed to the TMC or M.2 bus as its firmware selects. A request/grant handshake re-routes the host controller onto the TMC bus, where the PCA9548 and the CPLD behind its channel 0 live. Everything here is built on that handshake.
Patch 1 adds the arbitration and the read-only interfaces on top of it. Patch 2 adds the flashing, as requested in review.
v10: https://lore.kernel.org/all/[email protected]/ Review: https://lore.kernel.org/all/[email protected]/
One review point did not become a code change, so to answer it here: the CPLD paths leaving the PCA9548 with all channels deselected is harmless. The mux sits on the TMC bus, which the EEPROM paths never see - by the time the grant is dropped and the host controller is back on the EEPROM segment, the mux is not reachable from that adapter at all. Deselect-all is also the PCA9548 power-on state, so the MicroBlaze finds it exactly as it would after a cold boot. v11 documents this above the CPLD helpers rather than leaving it to be re-derived.
Changes since v10:
- Split into two patches, separating the firmware flashing from the bus access and the read-only interfaces (Jakub).
- Report the Lattice device ID as the fixed devlink version "cpld.id" and drop the cpld_device_id sysfs attribute (Jakub). The value is cached, so the bus arbitration is paid once per card.
- Hold the i2c core adapter lock across the whole grant window and use __i2c_transfer(); while the host controller is routed onto the TMC bus the EEPROMs are not behind that adapter at all, so an unarbitrated transfer from ptp_ocp_read_eeprom(), the nvmem attributes or the at24 sysfs files would be issued onto the wrong bus.
- Register the firmware-upload node per card as adva-cpld.N; the fixed name made the second X1 board in a host fail with -EEXIST.
- Unregister the upload first on detach, so an in-flight programming cycle is cancelled and flushed while the I2C controller is still up.
- Restrict cpld_status to 0400 and take cpld_lock interruptibly, so a reader cannot sit unkillable for the length of an upload or stall device removal.
- Fix adva_x1_cpld_wait_ready(): it polled floor(max_ms / 100) times and never re-read after the last sleep, so the 100 ms per-page budget allowed exactly one poll. Use read_poll_timeout() with a wall-clock deadline that also accounts for the I2C transactions.
- Cancellation: keep a request that arrives before prepare() runs, annotate the flag with READ_ONCE/WRITE_ONCE, honour it inside the long waits, and report it as CANCELED rather than HW_ERROR.
- Set cpld_in_config_mode before EN_CFG_TP and clear it once REFRESH is accepted, so DIS_CFG is neither skipped after a failed wait nor sent to a device that already rebooted.
- Retry the post-REFRESH mux reselect; its return was discarded, so a NAK failed an update whose image was already running.
- Drop I2C_M_NOSTART: two messages in one transfer already give the repeated-START read the ISC protocol needs, and the flag asks for the opposite on adapters that honour it.
- Withdraw the MicroBlaze request when the grant times out, and drop the -EBUSY check that only ever read back its own write.
- select FW_LOADER and FW_UPLOAD; without them the documented update path does not exist.
- Correct the IDCODE to 0x612bc043 and the part to LCMXO3LF-2100.
- Add lockdep_assert_held() to the callbacks and context annotations for the lock handed from prepare() to cleanup().
Sagi Maimon (2):
ptp: ocp: add TAP CPLD access for ADVA TimeCard X1
ptp: ocp: add TAP CPLD firmware upload for ADVA TimeCard X1
Documentation/ABI/testing/sysfs-timecard | 24 + drivers/ptp/Kconfig | 2 + drivers/ptp/ptp_ocp.c | 655 ++++++++++++++++++++++- 3 files changed, 677 insertions(+), 4 deletions(-)
Signature · 1 line
2.47.0
PATCH v11 1/2 ptp: ocp: add TAP CPLD access for ADVA TimeCard X1
The ADVA TimeCard X1 (PCI device 0x0410) carries a Lattice MachXO3 CPLD on the TMC I2C bus. The card has two I2C controllers: Linux registers only the one at 0x00150000, normally routed to the config EEPROMs, while the one at 0x00120000 belongs to the on-card MicroBlaze and is routed to either the TMC or the M.2 bus as its firmware selects.
A request/grant handshake re-routes the host controller from the EEPROM segment onto the TMC bus, leaving the MicroBlaze with M.2 only. The PCA9548 at 0x74 and the CPLD at 0x40 behind its channel 0 are reachable from the host only while that grant is held.
Add the arbitration and the read-only interfaces built on it. Because the handshake changes what the host adapter is wired to, an operation takes the i2c core adapter lock for the whole grant window and uses __i2c_transfer() internally; without that, a concurrent transfer from ptp_ocp_read_eeprom(), from the nvmem attributes or from the at24 sysfs files would be issued onto the TMC bus instead of to the EEPROM.
The Lattice device ID is a fixed property of the part, so report it as the fixed devlink version "cpld.id" rather than as a sysfs attribute, and cache it so the arbitration is paid once per card.
Add one X1-only attribute:
/sys/class/timecard/ocpN/cpld_status
report the CPLD status register, including the DONE, BUSY and FAILED indicators
A read arbitrates for the shared bus and reprograms the on-card mux, so it is root-only and takes cpld_lock interruptibly.
--- Documentation/ABI/testing/sysfs-timecard | 19 ++ drivers/ptp/ptp_ocp.c | 344 ++++++++++++++++++++++- 2 files changed, 359 insertions(+), 4 deletions(-)
Documentation/ABI/testing/sysfs-timecard +19 -0
@@ -11,6 +11,25 @@ Contact: Jonathan Lemon <[email protected]> Description: This directory contains the attributes of the Nth timecard registered. +What: /sys/class/timecard/ocpN/cpld_status +Date: July 2026 +Contact: Sagi Maimon <[email protected]> +Description: (RO, root only) The status register of the TAP CPLD, in + human-readable form: + + done=<0|1> busy=<0|1> failed=<0|1> + + Only present on ADVA x1 TAP boards (PCI ID 0xad5a:0x0410). + + done=1 indicates the configuration flash was successfully + programmed and is active. busy=1 means an internal operation + is in progress. failed=1 means the last ISC operation failed. + + A read arbitrates for the shared I2C bus and reprograms the + on-card mux, so it is restricted to root. The Lattice device + ID of the CPLD is reported as the fixed "cpld.id" version by + devlink dev info. + What: /sys/class/timecard/ocpN/available_clock_sources Date: September 2021 Contact: Jonathan Lemon <[email protected]>
drivers/ptp/ptp_ocp.c +340 -4
424 lines not on this page · raw message
Signature · 1 line
2.47.0
PATCH v11 2/2 ptp: ocp: add TAP CPLD firmware upload for ADVA TimeCard X1
In reply to Sagi MaimonThe Lattice MachXO3 CPLD on the ADVA TimeCard X1 is programmed over I2C using in-system programming (ISP). Build on the TMC bus arbitration added previously and expose the update path through the kernel firmware-upload subsystem.
The framework acquires the bus, erases the configuration flash, programs the image page-by-page and activates it with the MachXO3 REFRESH command. The upload node is registered per card as adva-cpld.N, using the same index as the owning ocpN device, so a host with more than one X1 board gets one node each:
/sys/class/firmware/adva-cpld.N/
The whole prepare/write/poll_complete/cleanup sequence runs under cpld_lock and the i2c adapter lock, so an EEPROM read blocks for as long as programming takes; the alternative is reading the TMC bus instead. The upload is unregistered first on detach, which cancels and flushes an in-flight programming cycle while the I2C controller is still up.
Select FW_LOADER and FW_UPLOAD, as the documented update path does not exist without them.
--- Documentation/ABI/testing/sysfs-timecard | 5 + drivers/ptp/Kconfig | 2 + drivers/ptp/ptp_ocp.c | 313 ++++++++++++++++++++++- 3 files changed, 319 insertions(+), 1 deletion(-)
Documentation/ABI/testing/sysfs-timecard +5 -0
@@ -30,6 +30,11 @@ Description: (RO, root only) The status register of the TAP CPLD, in ID of the CPLD is reported as the fixed "cpld.id" version by devlink dev info. + To program new CPLD firmware use the standard kernel + firmware-upload interface, registered per card at: + /sys/class/firmware/adva-cpld.N/ + where N is the index of this ocpN device. + What: /sys/class/timecard/ocpN/available_clock_sources Date: September 2021 Contact: Jonathan Lemon <[email protected]>
drivers/ptp/Kconfig +2 -0
@@ -218,6 +218,8 @@ config PTP_1588_CLOCK_OCP select NET_DEVLINK select CRC16 select DPLL + select FW_LOADER + select FW_UPLOAD help This driver adds support for an OpenCompute time card.
drivers/ptp/ptp_ocp.c +312 -1
379 lines not on this page · raw message
Signature · 1 line
2.47.0
Vadim Fedorenko
maintainerRe: [PATCH net-next v11 2/2] ptp: ocp: add TAP CPLD firmware upload for ADVA TimeCard X1
Quoted 159 lines · Sagi Maimon
The Lattice MachXO3 CPLD on the ADVA TimeCard X1 is programmed over I2C using in-system programming (ISP). Build on the TMC bus arbitration added previously and expose the update path through the kernel firmware-upload subsystem.
The framework acquires the bus, erases the configuration flash, programs the image page-by-page and activates it with the MachXO3 REFRESH command. The upload node is registered per card as adva-cpld.N, using the same index as the owning ocpN device, so a host with more than one X1 board gets one node each:
/sys/class/firmware/adva-cpld.N/The whole prepare/write/poll_complete/cleanup sequence runs under
cpld_lockand the i2c adapter lock, so an EEPROM read blocks for as long as programming takes; the alternative is reading the TMC bus instead. The upload is unregistered first on detach, which cancels and flushes an in-flight programming cycle while the I2C controller is still up.Select
FW_LOADERandFW_UPLOAD, as the documented update path does not exist without them.Signed-off-by Sagi Maimon <[email protected]>--- Documentation/ABI/testing/sysfs-timecard | 5 + drivers/ptp/Kconfig | 2 + drivers/ptp/ptp_ocp.c | 313 ++++++++++++++++++++++- 3 files changed, 319 insertions(+), 1 deletion(-)Documentation/ABI/testing/sysfs-timecard +5
-0@@ -30,6 +30,11 @@ Description: (RO, root only) The status register of the TAP CPLD, in ID of the CPLD is reported as the fixed "cpld.id" version by devlink dev info. + To program new CPLD firmware use the standard kernel + firmware-upload interface, registered per card at: + /sys/class/firmware/adva-cpld.N/ + where N is the index of this ocpN device. + What: /sys/class/timecard/ocpN/available_clock_sources Date: September 2021 Contact: Jonathan Lemon <[email protected]>drivers/ptp/Kconfig +2
-0@@ -218,6 +218,8 @@ config PTP_1588_CLOCK_OCP select NET_DEVLINK select CRC16 select DPLL + select FW_LOADER + select FW_UPLOAD help This driver adds support for an OpenCompute time card.drivers/ptp/ptp_ocp.c +44
-1@@ -20,12 +20,14 @@#include <linux/spi/altera.h> #include <net/devlink.h> #include <linux/i2c.h> +#include <linux/iopoll.h> #include <linux/mtd/mtd.h> #include <linux/nvmem-consumer.h> #include <linux/crc16.h> #include <linux/dpll.h> #include <linux/unaligned.h> #include <linux/delay.h> +#include <linux/firmware.h> #define PCI_DEVICE_ID_META_TIMECARD 0x0400@@ -426,6 +428,9 @@ struct ptp_ocp {struct i2c_adapter *cpld_adap;/* claimed adapter; valid under cpld_lock */ u32 cpld_id;/* cached Lattice device ID; 0 if unread */ bool has_cpld;/* x1 TAP CPLD present */ +struct fw_upload *cpld_fw_upload;/* firmware upload handle; NULL if absent */ +bool cpld_cancel;/* cancellation requested */ +bool cpld_in_config_mode;/* EN_CFG_TP issued but not yet REFRESH'd */ };#define OCP_REQ_TIMESTAMP BIT(0)@@ -459,6 +464,8 @@ static int ptp_ocp_art_board_init(struct ptp_ocp *bp, struct ocp_resource *r);static int ptp_ocp_adva_board_init (struct ptp_ocp *bp,struct ocp_resource *r); +static const struct fw_upload_ops adva_cpld_upload_ops; +static const struct ocp_sma_op ocp_adva_sma_op;static const struct ocp_sma_op ocp_adva_x1_sma_op;static int adva_x1_cpld_device_id (struct ptp_ocp *bp,u32 *id);@@ -3225,6 +3232,29 @@ ptp_ocp_adva_board_init(struct ptp_ocp *bp, struct ocp_resource *r)ptp_ocp_sma_init (bp); bp->has_cpld = info->has_cpld; +if (bp->has_cpld) { +struct fw_upload *fwl; +const char *name; + +/* One instance per card, numbered like the ocpN device. +* firmware_upload_register() keeps the pointer rather than +* copying the string, so it has to outlive the registration. +*/ + name =devm_kasprintf (&bp->pdev->dev,GFP_KERNEL , +"adva-cpld.%d" , bp->id); +if (!name) +return -ENOMEM ; + + fwl =firmware_upload_register (THIS_MODULE , &bp->pdev->dev, + name, &adva_cpld_upload_ops, bp); +if (IS_ERR (fwl)) +dev_warn (&bp->pdev->dev, +"CPLD firmware upload unavailable: %pe\n" , + fwl); +else + bp->cpld_fw_upload = fwl; + } +return ptp_ocp_init_clock (bp, &info->servo); }@@ -4283,6 +4313,15 @@ static const struct ocp_attr_group art_timecard_groups[] = {/* Lattice LCMXO3LF ISC command codes */ #define CPLD_CMD_READ_ID 0xE0000000UL#define CPLD_CMD_READ_STATUS 0x3C000000UL +#define CPLD_CMD_EN_CFG_TP 0x74/* enable config, transparent mode */ +#define CPLD_CMD_DIS_CFG 0x26 +#define CPLD_CMD_ERASE 0x0E +#define CPLD_CMD_RESET_ADDR 0x46 +#define CPLD_CMD_WRITE_PAGE 0x70 +#define CPLD_CMD_SET_DONE 0x5E +#define CPLD_CMD_REFRESH 0x79 +#define CPLD_PAGE_SIZE 16 +#define CPLD_POLL_US 10000/* status poll interval while busy */ /* Status register bit positions (Lattice LCMXO3LF datasheet) */ #define CPLD_STATUS_DONE BIT(8)@@ -4408,7 +4447,8 @@ static void adva_x1_bus_release(struct ptp_ocp *bp)* Claim the TMC bus for a CPLD operation. Holding the adapter lock over * the handshake keeps ptp_ocp_read_eeprom(), the nvmem attributes and the * at24 sysfs files off the controller while it is routed away from the -* EEPROMs. +* EEPROMs. A firmware upload holds it across the whole prepare/write/poll +* sequence, so an EEPROM read blocks for as long as programming takes. */ static int adva_x1_bus_claim (struct ptp_ocp *bp) {@@ -4442,6 +4482,20 @@ static int adva_x1_mux_select(struct ptp_ocp *bp, int ch)return adva_x1_i2c_xfer (bp,ADVA_MUX_ADDR , &val,1 ,NULL ,0 ); } +/* Send 1-byte ISC command + optional arguments. */ +static int adva_x1_cpld_write (struct ptp_ocp *bp, +u8 cmd,const u8 *args,u8 nargs)
drivers/ptp/ptp_ocp.c static int adva_x1_mux_select(struct ptp_ocp *bp, int ch) · line 4497
Quoted 10 lines
drivers/ptp/ptp_ocp.c +10
-0+{ +u8 buf[1 +64 ]; + +if (nargs >64 ) +return -EINVAL ; + buf[0 ] = cmd; +if (nargs) +memcpy (&buf[1 ], args, nargs); +return adva_x1_i2c_xfer (bp,ADVA_CPLD_ADDR , buf,1 + nargs,NULL ,0 ); +}
this function looks like not needed. basically, all callers of this function use either static, or stack-allocated buffurs, which are copied to another 65-byte stack allocated buffer, which is later copied to a
heap-allocated buffer in adva_x1_i2c_xfer(). I think it's too much of
copying for a command interface. It can be simplified to actually copy
a command byte to a very last buffer only. The check can also be moved
to adva_x1_i2c_xfer(). Even static per-command data can be returned as
a pointer via helper with switch-case block. I believe it will make code
much simplier and more readable.
Quoted 8 lines
drivers/ptp/ptp_ocp.c +1
-0+/* * Send a 4-byte command then read data back without an intermediate STOP * (Lattice combined write->repeated-START->read). Two messages in one @@ -4467,6 +4521,38 @@ static int adva_x1_cpld_read_status(struct ptp_ocp *bp, u32 *status)return 0 ; }
[...]
drivers/ptp/ptp_ocp.c cpld_status_show(struct device *dev, struct device_attribute *attr, · line 4654
Quoted 6 lines
drivers/ptp/ptp_ocp.c +6
-0+static enum fw_upload_err +adva_cpld_prepare (struct fw_upload *fwl,const u8 *data,u32 size) +{ +static const u8 era_args[3 ] = {0x04 ,0x00 ,0x00 };/* cfg sector only */ +static const u8 en_args[2 ] = {0x08 ,0x00 }; +static const u8 dis_args[2 ] = {0x00 ,0x00 };
here dis_args are copying another static const buffer from adva_cpld_cleanup()
drivers/ptp/ptp_ocp.c
Quoted 1 line
drivers/ptp/ptp_ocp.c +1
-0+static const u8 zero3[3 ] = {0 };
this one is redundant, because heap-allocated buffer is zero'ed on alloc
this code needs a bit more thinking.
Quoted 92 lines
drivers/ptp/ptp_ocp.c +92
-0+enum fw_upload_err ret =FW_UPLOAD_ERR_NONE ; +struct ptp_ocp *bp = fwl->dd_handle; + +/* Do not clear cpld_cancel here: fw_upload_start() queues the work +* before this runs, so a cancel may already have arrived. It is +* cleared once the upload is over, on every exit below and in +* cleanup(). +*/ +if (!size || size %CPLD_PAGE_SIZE ) { +WRITE_ONCE (bp->cpld_cancel, false); +return FW_UPLOAD_ERR_INVALID_SIZE ; + } + + bp->cpld_in_config_mode = false; + +mutex_lock (&bp->cpld_lock); + +if (adva_x1_bus_claim (bp)) { + ret =FW_UPLOAD_ERR_TIMEOUT ; + goto err_unlock; + } + +if (adva_x1_mux_select (bp,ADVA_MUX_CHANNEL )) { + ret =FW_UPLOAD_ERR_HW_ERROR ; + goto err_release; + } + +/* Set before issuing EN_CFG_TP, not after it completes: the CPLD may +* have entered configuration mode even if the write reports an error +* or the wait below times out, and err_deselect only sends DIS_CFG +* when this is set. A DIS_CFG to a device that never entered the +* mode is harmless; leaving it enabled is not. +*/ + bp->cpld_in_config_mode = true; + +if (adva_x1_cpld_write (bp,CPLD_CMD_EN_CFG_TP , en_args,2 ) || +adva_x1_cpld_wait_ready (bp,5000 )) { + ret =adva_cpld_err (bp); + goto err_deselect; + } + +if (READ_ONCE (bp->cpld_cancel)) { + ret =FW_UPLOAD_ERR_CANCELED ; + goto err_deselect; + } + +if (adva_x1_cpld_write (bp,CPLD_CMD_ERASE , era_args,3 ) || +adva_x1_cpld_wait_ready (bp,15000 )) { + ret =adva_cpld_err (bp); + goto err_deselect; + } + +if (READ_ONCE (bp->cpld_cancel)) { + ret =FW_UPLOAD_ERR_CANCELED ; + goto err_deselect; + } + +if (adva_x1_cpld_write (bp,CPLD_CMD_RESET_ADDR , zero3,3 )) { + ret =FW_UPLOAD_ERR_HW_ERROR ; + goto err_deselect; + } + +/* cleanup() unlocks everything. fw_upload_main() only pairs it with +* a prepare() that succeeded, so the error paths below unlock here +* instead; hand the context to cleanup() for sparse's benefit. +*/ +__release (&bp->cpld_lock); +return FW_UPLOAD_ERR_NONE ; + +err_deselect: +if (bp->cpld_in_config_mode) { +adva_x1_cpld_write (bp,CPLD_CMD_DIS_CFG , dis_args,2 ); + bp->cpld_in_config_mode = false; + } +adva_x1_mux_select (bp,-1 ); +err_release: +adva_x1_bus_release (bp); +err_unlock: +WRITE_ONCE (bp->cpld_cancel, false); +mutex_unlock (&bp->cpld_lock); +return ret; +} + +static enum fw_upload_err +adva_cpld_write (struct fw_upload *fwl,const u8 *data, +u32 offset,u32 size,u32 *written) +{ +struct ptp_ocp *bp = fwl->dd_handle; +u8 page_args[3 +CPLD_PAGE_SIZE ]; + +lockdep_assert_held (&bp->cpld_lock); +
drivers/ptp/ptp_ocp.c cpld_status_show(struct device *dev, struct device_attribute *attr, · line 4757
Quoted 10 lines
drivers/ptp/ptp_ocp.c +10
-0+if (READ_ONCE (bp->cpld_cancel)) +return FW_UPLOAD_ERR_CANCELED ; + +if (size <CPLD_PAGE_SIZE ) +return FW_UPLOAD_ERR_INVALID_SIZE ; + + page_args[0 ] =0x00 ; + page_args[1 ] =0x00 ; + page_args[2 ] =0x01 ; +memcpy (&page_args[3 ], data + offset,CPLD_PAGE_SIZE );
here again, stack allocated -> stack allocated -> heap allocated. quite a lot of allocations and copying for 16 bytes of actual data transfer.
Quoted 9 lines
drivers/ptp/ptp_ocp.c +9
-0+ +if (adva_x1_cpld_write (bp,CPLD_CMD_WRITE_PAGE , + page_args,3 +CPLD_PAGE_SIZE ) || +adva_x1_cpld_wait_ready (bp,100 )) +return adva_cpld_err (bp); + + *written =CPLD_PAGE_SIZE ; +return FW_UPLOAD_ERR_NONE ; +}
Re: [PATCH net-next v11 2/2] ptp: ocp: add TAP CPLD firmware upload for ADVA TimeCard X1
Quoted 173 lines · Vadim Fedorenko
Quoted 159 lines · Sagi Maimon
The Lattice MachXO3 CPLD on the ADVA TimeCard X1 is programmed over I2C using in-system programming (ISP). Build on the TMC bus arbitration added previously and expose the update path through the kernel firmware-upload subsystem.
The framework acquires the bus, erases the configuration flash, programs the image page-by-page and activates it with the MachXO3 REFRESH command. The upload node is registered per card as adva-cpld.N, using the same index as the owning ocpN device, so a host with more than one X1 board gets one node each:
/sys/class/firmware/adva-cpld.N/The whole prepare/write/poll_complete/cleanup sequence runs under
cpld_lockand the i2c adapter lock, so an EEPROM read blocks for as long as programming takes; the alternative is reading the TMC bus instead. The upload is unregistered first on detach, which cancels and flushes an in-flight programming cycle while the I2C controller is still up.Select
FW_LOADERandFW_UPLOAD, as the documented update path does not exist without them.Signed-off-by Sagi Maimon <[email protected]>--- Documentation/ABI/testing/sysfs-timecard | 5 + drivers/ptp/Kconfig | 2 + drivers/ptp/ptp_ocp.c | 313 ++++++++++++++++++++++- 3 files changed, 319 insertions(+), 1 deletion(-)Documentation/ABI/testing/sysfs-timecard +5
-0@@ -30,6 +30,11 @@ Description: (RO, root only) The status register of the TAP CPLD, in ID of the CPLD is reported as the fixed "cpld.id" version by devlink dev info. + To program new CPLD firmware use the standard kernel + firmware-upload interface, registered per card at: + /sys/class/firmware/adva-cpld.N/ + where N is the index of this ocpN device. + What: /sys/class/timecard/ocpN/available_clock_sources Date: September 2021 Contact: Jonathan Lemon <[email protected]>drivers/ptp/Kconfig +2
-0@@ -218,6 +218,8 @@ config PTP_1588_CLOCK_OCP select NET_DEVLINK select CRC16 select DPLL + select FW_LOADER + select FW_UPLOAD help This driver adds support for an OpenCompute time card.drivers/ptp/ptp_ocp.c +44
-1@@ -20,12 +20,14 @@#include <linux/spi/altera.h> #include <net/devlink.h> #include <linux/i2c.h> +#include <linux/iopoll.h> #include <linux/mtd/mtd.h> #include <linux/nvmem-consumer.h> #include <linux/crc16.h> #include <linux/dpll.h> #include <linux/unaligned.h> #include <linux/delay.h> +#include <linux/firmware.h> #define PCI_DEVICE_ID_META_TIMECARD 0x0400@@ -426,6 +428,9 @@ struct ptp_ocp {struct i2c_adapter *cpld_adap;/* claimed adapter; valid under cpld_lock */ u32 cpld_id;/* cached Lattice device ID; 0 if unread */ bool has_cpld;/* x1 TAP CPLD present */ +struct fw_upload *cpld_fw_upload;/* firmware upload handle; NULL if absent */ +bool cpld_cancel;/* cancellation requested */ +bool cpld_in_config_mode;/* EN_CFG_TP issued but not yet REFRESH'd */ };#define OCP_REQ_TIMESTAMP BIT(0)@@ -459,6 +464,8 @@ static int ptp_ocp_art_board_init(struct ptp_ocp *bp, struct ocp_resource *r);static int ptp_ocp_adva_board_init (struct ptp_ocp *bp,struct ocp_resource *r); +static const struct fw_upload_ops adva_cpld_upload_ops; +static const struct ocp_sma_op ocp_adva_sma_op;static const struct ocp_sma_op ocp_adva_x1_sma_op;static int adva_x1_cpld_device_id (struct ptp_ocp *bp,u32 *id);@@ -3225,6 +3232,29 @@ ptp_ocp_adva_board_init(struct ptp_ocp *bp, struct ocp_resource *r)ptp_ocp_sma_init (bp); bp->has_cpld = info->has_cpld; +if (bp->has_cpld) { +struct fw_upload *fwl; +const char *name; + +/* One instance per card, numbered like the ocpN device. +* firmware_upload_register() keeps the pointer rather than +* copying the string, so it has to outlive the registration. +*/ + name =devm_kasprintf (&bp->pdev->dev,GFP_KERNEL , +"adva-cpld.%d" , bp->id); +if (!name) +return -ENOMEM ; + + fwl =firmware_upload_register (THIS_MODULE , &bp->pdev->dev, + name, &adva_cpld_upload_ops, bp); +if (IS_ERR (fwl)) +dev_warn (&bp->pdev->dev, +"CPLD firmware upload unavailable: %pe\n" , + fwl); +else + bp->cpld_fw_upload = fwl; + } +return ptp_ocp_init_clock (bp, &info->servo); }@@ -4283,6 +4313,15 @@ static const struct ocp_attr_group art_timecard_groups[] = {/* Lattice LCMXO3LF ISC command codes */ #define CPLD_CMD_READ_ID 0xE0000000UL#define CPLD_CMD_READ_STATUS 0x3C000000UL +#define CPLD_CMD_EN_CFG_TP 0x74/* enable config, transparent mode */ +#define CPLD_CMD_DIS_CFG 0x26 +#define CPLD_CMD_ERASE 0x0E +#define CPLD_CMD_RESET_ADDR 0x46 +#define CPLD_CMD_WRITE_PAGE 0x70 +#define CPLD_CMD_SET_DONE 0x5E +#define CPLD_CMD_REFRESH 0x79 +#define CPLD_PAGE_SIZE 16 +#define CPLD_POLL_US 10000/* status poll interval while busy */ /* Status register bit positions (Lattice LCMXO3LF datasheet) */ #define CPLD_STATUS_DONE BIT(8)@@ -4408,7 +4447,8 @@ static void adva_x1_bus_release(struct ptp_ocp *bp)* Claim the TMC bus for a CPLD operation. Holding the adapter lock over * the handshake keeps ptp_ocp_read_eeprom(), the nvmem attributes and the * at24 sysfs files off the controller while it is routed away from the -* EEPROMs. +* EEPROMs. A firmware upload holds it across the whole prepare/write/poll +* sequence, so an EEPROM read blocks for as long as programming takes. */ static int adva_x1_bus_claim (struct ptp_ocp *bp) {@@ -4442,6 +4482,20 @@ static int adva_x1_mux_select(struct ptp_ocp *bp, int ch)return adva_x1_i2c_xfer (bp,ADVA_MUX_ADDR , &val,1 ,NULL ,0 ); } +/* Send 1-byte ISC command + optional arguments. */ +static int adva_x1_cpld_write (struct ptp_ocp *bp, +u8 cmd,const u8 *args,u8 nargs)drivers/ptp/ptp_ocp.c static int adva_x1_mux_select(struct ptp_ocp *bp, int ch) · line 4497
Quoted 10 lines
drivers/ptp/ptp_ocp.c +10
-0+{ +u8 buf[1 +64 ]; + +if (nargs >64 ) +return -EINVAL ; + buf[0 ] = cmd; +if (nargs) +memcpy (&buf[1 ], args, nargs); +return adva_x1_i2c_xfer (bp,ADVA_CPLD_ADDR , buf,1 + nargs,NULL ,0 ); +}this function looks like not needed. basically, all callers of this
Quoted 9 lines
function use either static, or stack-allocated buffurs, which are copied to another 65-byte stack allocated buffer, which is later copied to a heap-allocated buffer in
adva_x1_i2c_xfer(). I think it's too much of copying for a command interface. It can be simplified to actually copy a command byte to a very last buffer only. The check can also be moved toadva_x1_i2c_xfer(). Even static per-command data can be returned as a pointer via helper with switch-case block. I believe it will make code much simplier and more readable.
Thanks - v12 restructures the command path as you describe, with one deviation I have called out below: I kept adva_x1_cpld_write() itself.
adva_x1_i2c_xfer() now takes the opcode as its own argument and assembles the message in a scratch buffer taken once per bus claim, so there is no intermediate buffer and no per-transfer allocation:
static int adva_x1_i2c_xfer(struct ptp_ocp *bp, u8 addr, int cmd,
const void *wdata, u8 wlen,
void *rdata, u8 rlen)
{
...
if (hdr + wlen > ADVA_CPLD_XFER_MAX ||
rlen > ADVA_CPLD_XFER_MAX)
return -EINVAL;
wbuf = bp->cpld_buf;
rbuf = bp->cpld_buf + ADVA_CPLD_XFER_MAX;
The bound check you asked for is here now, and it has something real to
bound: the scratch buffer, rather than the stack array it used to guard.
adva_x1_cpld_write() takes just the opcode, and the argument bytes come from a switch-case helper returning a pointer to the static data:
static const u8 *adva_x1_cpld_args(u8 cmd, u8 *nargs) {
static const u8 en_cfg_tp[] = { 0x08, 0x00 }; static const u8 erase_cfg[] = { 0x04, 0x00, 0x00 };
switch (cmd) {
case CPLD_CMD_EN_CFG_TP:
*nargs = sizeof(en_cfg_tp);
return en_cfg_tp;
...
}
}
static int adva_x1_cpld_write(struct ptp_ocp *bp, u8 cmd) {
const u8 *args;
u8 nargs;
args = adva_x1_cpld_args(cmd, &nargs);
return adva_x1_i2c_xfer(bp, ADVA_CPLD_ADDR, cmd, args, nargs, NULL, 0);
}
I kept adva_x1_cpld_write() itself, now four lines, because something has to consume the pointer the helper returns and issue the transfer; without it each of the eight call sites needs the lookup, two locals and the full adva_x1_i2c_xfer() argument list. If you meant drop it entirely and have the callers do that, say so and I will.
Quoted 11 lines
Quoted 8 lines
drivers/ptp/ptp_ocp.c +1
-0+/* * Send a 4-byte command then read data back without an intermediate STOP * (Lattice combined write->repeated-START->read). Two messages in one @@ -4467,6 +4521,38 @@ static int adva_x1_cpld_read_status(struct ptp_ocp *bp, u32 *status)return 0 ; }[...]
Quoted 10 lines
drivers/ptp/ptp_ocp.c cpld_status_show(struct device *dev, struct device_attribute *attr, · line 4654
Quoted 6 lines
drivers/ptp/ptp_ocp.c +6
-0+static enum fw_upload_err +adva_cpld_prepare (struct fw_upload *fwl,const u8 *data,u32 size) +{ +static const u8 era_args[3 ] = {0x04 ,0x00 ,0x00 };/* cfg sector only */ +static const u8 en_args[2 ] = {0x08 ,0x00 }; +static const u8 dis_args[2 ] = {0x00 ,0x00 };here
dis_argsare copying another static const buffer fromadva_cpld_cleanup()
Gone - both call sites are now just adva_x1_cpld_write(bp, CPLD_CMD_DIS_CFG).
Quoted 4 lines
Quoted 1 line
+ static const u8 zero3[3] = { 0 };
this one is redundant, because heap-allocated buffer is zero'ed on alloc
Right in effect, though not for that reason in v11: the write buffer was kmemdup()ed there, which does not zero, so the zero3[] arrays were doing real work. In v12 the all-zero commands (RESET_ADDR, SET_DONE, DIS_CFG, REFRESH) pass NULL and adva_x1_i2c_xfer() zeroes that part of the message, so the arrays are gone.
Quoted 2 lines
this code needs a bit more thinking.
Reworked in v12 along the lines above - see the per-command changes in this reply. No functional change: the byte sequences on the wire are unchanged, and I verified CPLD programming on an ADVA TimeCard X1.
Quoted 98 lines
Quoted 98 lines
drivers/ptp/ptp_ocp.c +98
-0+enum fw_upload_err ret =FW_UPLOAD_ERR_NONE ; +struct ptp_ocp *bp = fwl->dd_handle; + +/* Do not clear cpld_cancel here: fw_upload_start() queues the work +* before this runs, so a cancel may already have arrived. It is +* cleared once the upload is over, on every exit below and in +* cleanup(). +*/ +if (!size || size %CPLD_PAGE_SIZE ) { +WRITE_ONCE (bp->cpld_cancel, false); +return FW_UPLOAD_ERR_INVALID_SIZE ; + } + + bp->cpld_in_config_mode = false; + +mutex_lock (&bp->cpld_lock); + +if (adva_x1_bus_claim (bp)) { + ret =FW_UPLOAD_ERR_TIMEOUT ; + goto err_unlock; + } + +if (adva_x1_mux_select (bp,ADVA_MUX_CHANNEL )) { + ret =FW_UPLOAD_ERR_HW_ERROR ; + goto err_release; + } + +/* Set before issuing EN_CFG_TP, not after it completes: the CPLD may +* have entered configuration mode even if the write reports an error +* or the wait below times out, and err_deselect only sends DIS_CFG +* when this is set. A DIS_CFG to a device that never entered the +* mode is harmless; leaving it enabled is not. +*/ + bp->cpld_in_config_mode = true; + +if (adva_x1_cpld_write (bp,CPLD_CMD_EN_CFG_TP , en_args,2 ) || +adva_x1_cpld_wait_ready (bp,5000 )) { + ret =adva_cpld_err (bp); + goto err_deselect; + } + +if (READ_ONCE (bp->cpld_cancel)) { + ret =FW_UPLOAD_ERR_CANCELED ; + goto err_deselect; + } + +if (adva_x1_cpld_write (bp,CPLD_CMD_ERASE , era_args,3 ) || +adva_x1_cpld_wait_ready (bp,15000 )) { + ret =adva_cpld_err (bp); + goto err_deselect; + } + +if (READ_ONCE (bp->cpld_cancel)) { + ret =FW_UPLOAD_ERR_CANCELED ; + goto err_deselect; + } + +if (adva_x1_cpld_write (bp,CPLD_CMD_RESET_ADDR , zero3,3 )) { + ret =FW_UPLOAD_ERR_HW_ERROR ; + goto err_deselect; + } + +/* cleanup() unlocks everything. fw_upload_main() only pairs it with +* a prepare() that succeeded, so the error paths below unlock here +* instead; hand the context to cleanup() for sparse's benefit. +*/ +__release (&bp->cpld_lock); +return FW_UPLOAD_ERR_NONE ; + +err_deselect: +if (bp->cpld_in_config_mode) { +adva_x1_cpld_write (bp,CPLD_CMD_DIS_CFG , dis_args,2 ); + bp->cpld_in_config_mode = false; + } +adva_x1_mux_select (bp,-1 ); +err_release: +adva_x1_bus_release (bp); +err_unlock: +WRITE_ONCE (bp->cpld_cancel, false); +mutex_unlock (&bp->cpld_lock); +return ret; +} + +static enum fw_upload_err +adva_cpld_write (struct fw_upload *fwl,const u8 *data, +u32 offset,u32 size,u32 *written) +{ +struct ptp_ocp *bp = fwl->dd_handle; +u8 page_args[3 +CPLD_PAGE_SIZE ]; + +lockdep_assert_held (&bp->cpld_lock); + +if (READ_ONCE (bp->cpld_cancel)) +return FW_UPLOAD_ERR_CANCELED ; + +if (size <CPLD_PAGE_SIZE ) +return FW_UPLOAD_ERR_INVALID_SIZE ; +
Quoted 9 lines
drivers/ptp/ptp_ocp.c cpld_status_show(struct device *dev, struct device_attribute *attr, · line 4757
Quoted 4 lines
drivers/ptp/ptp_ocp.c +4
-0+ page_args[0 ] =0x00 ; + page_args[1 ] =0x00 ; + page_args[2 ] =0x01 ; +memcpy (&page_args[3 ], data + offset,CPLD_PAGE_SIZE );here again, stack allocated -> stack allocated -> heap allocated. quite a lot of allocations and copying for 16 bytes of actual data transfer.
Fixed in v12. The middle stack buffer is gone, and the scratch buffer is allocated once per bus claim rather than per transfer - a claim spans the whole upload, so programming an image is one allocation rather than one per page:
/* One scratch buffer per claim, not per transfer. */ bp->cpld_buf = kzalloc(2 * ADVA_CPLD_XFER_MAX, GFP_KERNEL);
adva_cpld_write() still stages the three header bytes and the page in a small stack array before the transfer copies it in, so the page data is copied once more than strictly necessary. Removing that too would mean letting the callback write into the transfer's buffer directly, which seemed a worse trade; say the word if you would rather have it.
net-next is closed at the moment, so I will post v12 with these changes once it reopens.
Quoted 9 lines
Quoted 9 lines
drivers/ptp/ptp_ocp.c +9
-0+ +if (adva_x1_cpld_write (bp,CPLD_CMD_WRITE_PAGE , + page_args,3 +CPLD_PAGE_SIZE ) || +adva_x1_cpld_wait_ready (bp,100 )) +return adva_cpld_err (bp); + + *written =CPLD_PAGE_SIZE ; +return FW_UPLOAD_ERR_NONE ; +}