1d0964594SHoratiu Vultur // SPDX-License-Identifier: GPL-2.0+
2d0964594SHoratiu Vultur
3d0964594SHoratiu Vultur #include <linux/ptp_classify.h>
4d0964594SHoratiu Vultur
5d0964594SHoratiu Vultur #include "lan966x_main.h"
672df3489SHoratiu Vultur #include "vcap_api.h"
772df3489SHoratiu Vultur #include "vcap_api_client.h"
8d0964594SHoratiu Vultur
9d0964594SHoratiu Vultur #define LAN966X_MAX_PTP_ID 512
10d0964594SHoratiu Vultur
11d0964594SHoratiu Vultur /* Represents 1ppm adjustment in 2^59 format with 6.037735849ns as reference
12d0964594SHoratiu Vultur * The value is calculated as following: (1/1000000)/((2^-59)/6.037735849)
13d0964594SHoratiu Vultur */
14d0964594SHoratiu Vultur #define LAN966X_1PPM_FORMAT 3480517749723LL
15d0964594SHoratiu Vultur
16d0964594SHoratiu Vultur /* Represents 1ppb adjustment in 2^29 format with 6.037735849ns as reference
17d0964594SHoratiu Vultur * The value is calculated as following: (1/1000000000)/((2^59)/6.037735849)
18d0964594SHoratiu Vultur */
19d0964594SHoratiu Vultur #define LAN966X_1PPB_FORMAT 3480517749LL
20d0964594SHoratiu Vultur
2177f2accbSHoratiu Vultur #define TOD_ACC_PIN 0x7
22d0964594SHoratiu Vultur
2372df3489SHoratiu Vultur /* This represents the base rule ID for the PTP rules that are added in the
2472df3489SHoratiu Vultur * VCAP to trap frames to CPU. This number needs to be bigger than the maximum
2572df3489SHoratiu Vultur * number of entries that can exist in the VCAP.
2672df3489SHoratiu Vultur */
2772df3489SHoratiu Vultur #define LAN966X_VCAP_PTP_RULE_ID 1000000
2872df3489SHoratiu Vultur #define LAN966X_VCAP_L2_PTP_TRAP (LAN966X_VCAP_PTP_RULE_ID + 0)
2972df3489SHoratiu Vultur #define LAN966X_VCAP_IPV4_EV_PTP_TRAP (LAN966X_VCAP_PTP_RULE_ID + 1)
3072df3489SHoratiu Vultur #define LAN966X_VCAP_IPV4_GEN_PTP_TRAP (LAN966X_VCAP_PTP_RULE_ID + 2)
3172df3489SHoratiu Vultur #define LAN966X_VCAP_IPV6_EV_PTP_TRAP (LAN966X_VCAP_PTP_RULE_ID + 3)
3272df3489SHoratiu Vultur #define LAN966X_VCAP_IPV6_GEN_PTP_TRAP (LAN966X_VCAP_PTP_RULE_ID + 4)
3372df3489SHoratiu Vultur
34d0964594SHoratiu Vultur enum {
35d0964594SHoratiu Vultur PTP_PIN_ACTION_IDLE = 0,
36d0964594SHoratiu Vultur PTP_PIN_ACTION_LOAD,
37d0964594SHoratiu Vultur PTP_PIN_ACTION_SAVE,
38d0964594SHoratiu Vultur PTP_PIN_ACTION_CLOCK,
39d0964594SHoratiu Vultur PTP_PIN_ACTION_DELTA,
40d0964594SHoratiu Vultur PTP_PIN_ACTION_TOD
41d0964594SHoratiu Vultur };
42d0964594SHoratiu Vultur
lan966x_ptp_get_nominal_value(void)43d0964594SHoratiu Vultur static u64 lan966x_ptp_get_nominal_value(void)
44d0964594SHoratiu Vultur {
45eb9c0d67SHoratiu Vultur /* This is the default value that for each system clock, the time of day
46eb9c0d67SHoratiu Vultur * is increased. It has the format 5.59 nanosecond.
47eb9c0d67SHoratiu Vultur */
48eb9c0d67SHoratiu Vultur return 0x304d4873ecade305;
49d0964594SHoratiu Vultur }
50d0964594SHoratiu Vultur
lan966x_ptp_add_trap(struct lan966x_port * port,int (* add_ptp_key)(struct vcap_rule * vrule,struct lan966x_port *),u32 rule_id,u16 proto)5172df3489SHoratiu Vultur static int lan966x_ptp_add_trap(struct lan966x_port *port,
5272df3489SHoratiu Vultur int (*add_ptp_key)(struct vcap_rule *vrule,
5372df3489SHoratiu Vultur struct lan966x_port*),
5472df3489SHoratiu Vultur u32 rule_id,
5572df3489SHoratiu Vultur u16 proto)
5672df3489SHoratiu Vultur {
5772df3489SHoratiu Vultur struct lan966x *lan966x = port->lan966x;
5872df3489SHoratiu Vultur struct vcap_rule *vrule;
5972df3489SHoratiu Vultur int err;
6072df3489SHoratiu Vultur
6172df3489SHoratiu Vultur vrule = vcap_get_rule(lan966x->vcap_ctrl, rule_id);
62*ab104318SRuan Jinjie if (!IS_ERR(vrule)) {
6372df3489SHoratiu Vultur u32 value, mask;
6472df3489SHoratiu Vultur
6572df3489SHoratiu Vultur /* Just modify the ingress port mask and exit */
6672df3489SHoratiu Vultur vcap_rule_get_key_u32(vrule, VCAP_KF_IF_IGR_PORT_MASK,
6772df3489SHoratiu Vultur &value, &mask);
6872df3489SHoratiu Vultur mask &= ~BIT(port->chip_port);
6972df3489SHoratiu Vultur vcap_rule_mod_key_u32(vrule, VCAP_KF_IF_IGR_PORT_MASK,
7072df3489SHoratiu Vultur value, mask);
7172df3489SHoratiu Vultur
7272df3489SHoratiu Vultur err = vcap_mod_rule(vrule);
7372df3489SHoratiu Vultur goto free_rule;
7472df3489SHoratiu Vultur }
7572df3489SHoratiu Vultur
7672df3489SHoratiu Vultur vrule = vcap_alloc_rule(lan966x->vcap_ctrl, port->dev,
7772df3489SHoratiu Vultur LAN966X_VCAP_CID_IS2_L0,
7872df3489SHoratiu Vultur VCAP_USER_PTP, 0, rule_id);
7972df3489SHoratiu Vultur if (IS_ERR(vrule))
8072df3489SHoratiu Vultur return PTR_ERR(vrule);
8172df3489SHoratiu Vultur
8272df3489SHoratiu Vultur err = add_ptp_key(vrule, port);
8372df3489SHoratiu Vultur if (err)
8472df3489SHoratiu Vultur goto free_rule;
8572df3489SHoratiu Vultur
864d3e050bSHoratiu Vultur err = vcap_rule_add_action_bit(vrule, VCAP_AF_CPU_COPY_ENA, VCAP_BIT_1);
8772df3489SHoratiu Vultur err |= vcap_rule_add_action_u32(vrule, VCAP_AF_MASK_MODE, LAN966X_PMM_REPLACE);
8872df3489SHoratiu Vultur err |= vcap_val_rule(vrule, proto);
8972df3489SHoratiu Vultur if (err)
9072df3489SHoratiu Vultur goto free_rule;
9172df3489SHoratiu Vultur
9272df3489SHoratiu Vultur err = vcap_add_rule(vrule);
9372df3489SHoratiu Vultur
9472df3489SHoratiu Vultur free_rule:
9572df3489SHoratiu Vultur /* Free the local copy of the rule */
9672df3489SHoratiu Vultur vcap_free_rule(vrule);
9772df3489SHoratiu Vultur return err;
9872df3489SHoratiu Vultur }
9972df3489SHoratiu Vultur
lan966x_ptp_del_trap(struct lan966x_port * port,u32 rule_id)10072df3489SHoratiu Vultur static int lan966x_ptp_del_trap(struct lan966x_port *port,
10172df3489SHoratiu Vultur u32 rule_id)
10272df3489SHoratiu Vultur {
10372df3489SHoratiu Vultur struct lan966x *lan966x = port->lan966x;
10472df3489SHoratiu Vultur struct vcap_rule *vrule;
10572df3489SHoratiu Vultur u32 value, mask;
10672df3489SHoratiu Vultur int err;
10772df3489SHoratiu Vultur
10872df3489SHoratiu Vultur vrule = vcap_get_rule(lan966x->vcap_ctrl, rule_id);
109*ab104318SRuan Jinjie if (IS_ERR(vrule))
11072df3489SHoratiu Vultur return -EEXIST;
11172df3489SHoratiu Vultur
11272df3489SHoratiu Vultur vcap_rule_get_key_u32(vrule, VCAP_KF_IF_IGR_PORT_MASK, &value, &mask);
11372df3489SHoratiu Vultur mask |= BIT(port->chip_port);
11472df3489SHoratiu Vultur
11572df3489SHoratiu Vultur /* No other port requires this trap, so it is safe to remove it */
11672df3489SHoratiu Vultur if (mask == GENMASK(lan966x->num_phys_ports, 0)) {
11772df3489SHoratiu Vultur err = vcap_del_rule(lan966x->vcap_ctrl, port->dev, rule_id);
11872df3489SHoratiu Vultur goto free_rule;
11972df3489SHoratiu Vultur }
12072df3489SHoratiu Vultur
12172df3489SHoratiu Vultur vcap_rule_mod_key_u32(vrule, VCAP_KF_IF_IGR_PORT_MASK, value, mask);
12272df3489SHoratiu Vultur err = vcap_mod_rule(vrule);
12372df3489SHoratiu Vultur
12472df3489SHoratiu Vultur free_rule:
12572df3489SHoratiu Vultur vcap_free_rule(vrule);
12672df3489SHoratiu Vultur return err;
12772df3489SHoratiu Vultur }
12872df3489SHoratiu Vultur
lan966x_ptp_add_l2_key(struct vcap_rule * vrule,struct lan966x_port * port)12972df3489SHoratiu Vultur static int lan966x_ptp_add_l2_key(struct vcap_rule *vrule,
13072df3489SHoratiu Vultur struct lan966x_port *port)
13172df3489SHoratiu Vultur {
13272df3489SHoratiu Vultur return vcap_rule_add_key_u32(vrule, VCAP_KF_ETYPE, ETH_P_1588, ~0);
13372df3489SHoratiu Vultur }
13472df3489SHoratiu Vultur
lan966x_ptp_add_ip_event_key(struct vcap_rule * vrule,struct lan966x_port * port)13572df3489SHoratiu Vultur static int lan966x_ptp_add_ip_event_key(struct vcap_rule *vrule,
13672df3489SHoratiu Vultur struct lan966x_port *port)
13772df3489SHoratiu Vultur {
13872df3489SHoratiu Vultur return vcap_rule_add_key_u32(vrule, VCAP_KF_L4_DPORT, PTP_EV_PORT, ~0) ||
13972df3489SHoratiu Vultur vcap_rule_add_key_bit(vrule, VCAP_KF_TCP_IS, VCAP_BIT_0);
14072df3489SHoratiu Vultur }
14172df3489SHoratiu Vultur
lan966x_ptp_add_ip_general_key(struct vcap_rule * vrule,struct lan966x_port * port)14272df3489SHoratiu Vultur static int lan966x_ptp_add_ip_general_key(struct vcap_rule *vrule,
14372df3489SHoratiu Vultur struct lan966x_port *port)
14472df3489SHoratiu Vultur {
14572df3489SHoratiu Vultur return vcap_rule_add_key_u32(vrule, VCAP_KF_L4_DPORT, PTP_GEN_PORT, ~0) ||
14672df3489SHoratiu Vultur vcap_rule_add_key_bit(vrule, VCAP_KF_TCP_IS, VCAP_BIT_0);
14772df3489SHoratiu Vultur }
14872df3489SHoratiu Vultur
lan966x_ptp_add_l2_rule(struct lan966x_port * port)14972df3489SHoratiu Vultur static int lan966x_ptp_add_l2_rule(struct lan966x_port *port)
15072df3489SHoratiu Vultur {
15172df3489SHoratiu Vultur return lan966x_ptp_add_trap(port, lan966x_ptp_add_l2_key,
15272df3489SHoratiu Vultur LAN966X_VCAP_L2_PTP_TRAP, ETH_P_ALL);
15372df3489SHoratiu Vultur }
15472df3489SHoratiu Vultur
lan966x_ptp_add_ipv4_rules(struct lan966x_port * port)15572df3489SHoratiu Vultur static int lan966x_ptp_add_ipv4_rules(struct lan966x_port *port)
15672df3489SHoratiu Vultur {
15772df3489SHoratiu Vultur int err;
15872df3489SHoratiu Vultur
15972df3489SHoratiu Vultur err = lan966x_ptp_add_trap(port, lan966x_ptp_add_ip_event_key,
16072df3489SHoratiu Vultur LAN966X_VCAP_IPV4_EV_PTP_TRAP, ETH_P_IP);
16172df3489SHoratiu Vultur if (err)
16272df3489SHoratiu Vultur return err;
16372df3489SHoratiu Vultur
16472df3489SHoratiu Vultur err = lan966x_ptp_add_trap(port, lan966x_ptp_add_ip_general_key,
16572df3489SHoratiu Vultur LAN966X_VCAP_IPV4_GEN_PTP_TRAP, ETH_P_IP);
16672df3489SHoratiu Vultur if (err)
16772df3489SHoratiu Vultur lan966x_ptp_del_trap(port, LAN966X_VCAP_IPV4_EV_PTP_TRAP);
16872df3489SHoratiu Vultur
16972df3489SHoratiu Vultur return err;
17072df3489SHoratiu Vultur }
17172df3489SHoratiu Vultur
lan966x_ptp_add_ipv6_rules(struct lan966x_port * port)17272df3489SHoratiu Vultur static int lan966x_ptp_add_ipv6_rules(struct lan966x_port *port)
17372df3489SHoratiu Vultur {
17472df3489SHoratiu Vultur int err;
17572df3489SHoratiu Vultur
17672df3489SHoratiu Vultur err = lan966x_ptp_add_trap(port, lan966x_ptp_add_ip_event_key,
17772df3489SHoratiu Vultur LAN966X_VCAP_IPV6_EV_PTP_TRAP, ETH_P_IPV6);
17872df3489SHoratiu Vultur if (err)
17972df3489SHoratiu Vultur return err;
18072df3489SHoratiu Vultur
18172df3489SHoratiu Vultur err = lan966x_ptp_add_trap(port, lan966x_ptp_add_ip_general_key,
18272df3489SHoratiu Vultur LAN966X_VCAP_IPV6_GEN_PTP_TRAP, ETH_P_IPV6);
18372df3489SHoratiu Vultur if (err)
18472df3489SHoratiu Vultur lan966x_ptp_del_trap(port, LAN966X_VCAP_IPV6_EV_PTP_TRAP);
18572df3489SHoratiu Vultur
18672df3489SHoratiu Vultur return err;
18772df3489SHoratiu Vultur }
18872df3489SHoratiu Vultur
lan966x_ptp_del_l2_rule(struct lan966x_port * port)18972df3489SHoratiu Vultur static int lan966x_ptp_del_l2_rule(struct lan966x_port *port)
19072df3489SHoratiu Vultur {
19172df3489SHoratiu Vultur return lan966x_ptp_del_trap(port, LAN966X_VCAP_L2_PTP_TRAP);
19272df3489SHoratiu Vultur }
19372df3489SHoratiu Vultur
lan966x_ptp_del_ipv4_rules(struct lan966x_port * port)19472df3489SHoratiu Vultur static int lan966x_ptp_del_ipv4_rules(struct lan966x_port *port)
19572df3489SHoratiu Vultur {
19672df3489SHoratiu Vultur int err;
19772df3489SHoratiu Vultur
19872df3489SHoratiu Vultur err = lan966x_ptp_del_trap(port, LAN966X_VCAP_IPV4_EV_PTP_TRAP);
19972df3489SHoratiu Vultur err |= lan966x_ptp_del_trap(port, LAN966X_VCAP_IPV4_GEN_PTP_TRAP);
20072df3489SHoratiu Vultur
20172df3489SHoratiu Vultur return err;
20272df3489SHoratiu Vultur }
20372df3489SHoratiu Vultur
lan966x_ptp_del_ipv6_rules(struct lan966x_port * port)20472df3489SHoratiu Vultur static int lan966x_ptp_del_ipv6_rules(struct lan966x_port *port)
20572df3489SHoratiu Vultur {
20672df3489SHoratiu Vultur int err;
20772df3489SHoratiu Vultur
20872df3489SHoratiu Vultur err = lan966x_ptp_del_trap(port, LAN966X_VCAP_IPV6_EV_PTP_TRAP);
20972df3489SHoratiu Vultur err |= lan966x_ptp_del_trap(port, LAN966X_VCAP_IPV6_GEN_PTP_TRAP);
21072df3489SHoratiu Vultur
21172df3489SHoratiu Vultur return err;
21272df3489SHoratiu Vultur }
21372df3489SHoratiu Vultur
lan966x_ptp_add_traps(struct lan966x_port * port)21472df3489SHoratiu Vultur static int lan966x_ptp_add_traps(struct lan966x_port *port)
21572df3489SHoratiu Vultur {
21672df3489SHoratiu Vultur int err;
21772df3489SHoratiu Vultur
21872df3489SHoratiu Vultur err = lan966x_ptp_add_l2_rule(port);
21972df3489SHoratiu Vultur if (err)
22072df3489SHoratiu Vultur goto err_l2;
22172df3489SHoratiu Vultur
22272df3489SHoratiu Vultur err = lan966x_ptp_add_ipv4_rules(port);
22372df3489SHoratiu Vultur if (err)
22472df3489SHoratiu Vultur goto err_ipv4;
22572df3489SHoratiu Vultur
22672df3489SHoratiu Vultur err = lan966x_ptp_add_ipv6_rules(port);
22772df3489SHoratiu Vultur if (err)
22872df3489SHoratiu Vultur goto err_ipv6;
22972df3489SHoratiu Vultur
23072df3489SHoratiu Vultur return err;
23172df3489SHoratiu Vultur
23272df3489SHoratiu Vultur err_ipv6:
23372df3489SHoratiu Vultur lan966x_ptp_del_ipv4_rules(port);
23472df3489SHoratiu Vultur err_ipv4:
23572df3489SHoratiu Vultur lan966x_ptp_del_l2_rule(port);
23672df3489SHoratiu Vultur err_l2:
23772df3489SHoratiu Vultur return err;
23872df3489SHoratiu Vultur }
23972df3489SHoratiu Vultur
lan966x_ptp_del_traps(struct lan966x_port * port)24072df3489SHoratiu Vultur int lan966x_ptp_del_traps(struct lan966x_port *port)
24172df3489SHoratiu Vultur {
24272df3489SHoratiu Vultur int err;
24372df3489SHoratiu Vultur
24472df3489SHoratiu Vultur err = lan966x_ptp_del_l2_rule(port);
24572df3489SHoratiu Vultur err |= lan966x_ptp_del_ipv4_rules(port);
24672df3489SHoratiu Vultur err |= lan966x_ptp_del_ipv6_rules(port);
24772df3489SHoratiu Vultur
24872df3489SHoratiu Vultur return err;
24972df3489SHoratiu Vultur }
25072df3489SHoratiu Vultur
lan966x_ptp_setup_traps(struct lan966x_port * port,struct kernel_hwtstamp_config * cfg)25154e1ed69SVladimir Oltean int lan966x_ptp_setup_traps(struct lan966x_port *port,
25254e1ed69SVladimir Oltean struct kernel_hwtstamp_config *cfg)
25372df3489SHoratiu Vultur {
25454e1ed69SVladimir Oltean if (cfg->rx_filter == HWTSTAMP_FILTER_NONE)
25572df3489SHoratiu Vultur return lan966x_ptp_del_traps(port);
25672df3489SHoratiu Vultur else
25772df3489SHoratiu Vultur return lan966x_ptp_add_traps(port);
25872df3489SHoratiu Vultur }
25972df3489SHoratiu Vultur
lan966x_ptp_hwtstamp_set(struct lan966x_port * port,struct kernel_hwtstamp_config * cfg,struct netlink_ext_ack * extack)26054e1ed69SVladimir Oltean int lan966x_ptp_hwtstamp_set(struct lan966x_port *port,
26154e1ed69SVladimir Oltean struct kernel_hwtstamp_config *cfg,
26254e1ed69SVladimir Oltean struct netlink_ext_ack *extack)
263735fec99SHoratiu Vultur {
264735fec99SHoratiu Vultur struct lan966x *lan966x = port->lan966x;
265735fec99SHoratiu Vultur struct lan966x_phc *phc;
266735fec99SHoratiu Vultur
26754e1ed69SVladimir Oltean switch (cfg->tx_type) {
268735fec99SHoratiu Vultur case HWTSTAMP_TX_ON:
269ff89ac70SHoratiu Vultur port->ptp_tx_cmd = IFH_REW_OP_TWO_STEP_PTP;
270735fec99SHoratiu Vultur break;
271735fec99SHoratiu Vultur case HWTSTAMP_TX_ONESTEP_SYNC:
272ff89ac70SHoratiu Vultur port->ptp_tx_cmd = IFH_REW_OP_ONE_STEP_PTP;
273735fec99SHoratiu Vultur break;
274735fec99SHoratiu Vultur case HWTSTAMP_TX_OFF:
275ff89ac70SHoratiu Vultur port->ptp_tx_cmd = IFH_REW_OP_NOOP;
276735fec99SHoratiu Vultur break;
277735fec99SHoratiu Vultur default:
278735fec99SHoratiu Vultur return -ERANGE;
279735fec99SHoratiu Vultur }
280735fec99SHoratiu Vultur
28154e1ed69SVladimir Oltean switch (cfg->rx_filter) {
282735fec99SHoratiu Vultur case HWTSTAMP_FILTER_NONE:
283ff89ac70SHoratiu Vultur port->ptp_rx_cmd = false;
284735fec99SHoratiu Vultur break;
285735fec99SHoratiu Vultur case HWTSTAMP_FILTER_ALL:
286735fec99SHoratiu Vultur case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
287735fec99SHoratiu Vultur case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
288735fec99SHoratiu Vultur case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
289735fec99SHoratiu Vultur case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
290735fec99SHoratiu Vultur case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
291735fec99SHoratiu Vultur case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
292735fec99SHoratiu Vultur case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
293735fec99SHoratiu Vultur case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
294735fec99SHoratiu Vultur case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
295735fec99SHoratiu Vultur case HWTSTAMP_FILTER_PTP_V2_EVENT:
296735fec99SHoratiu Vultur case HWTSTAMP_FILTER_PTP_V2_SYNC:
297735fec99SHoratiu Vultur case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
298735fec99SHoratiu Vultur case HWTSTAMP_FILTER_NTP_ALL:
299ff89ac70SHoratiu Vultur port->ptp_rx_cmd = true;
30054e1ed69SVladimir Oltean cfg->rx_filter = HWTSTAMP_FILTER_ALL;
301735fec99SHoratiu Vultur break;
302735fec99SHoratiu Vultur default:
303735fec99SHoratiu Vultur return -ERANGE;
304735fec99SHoratiu Vultur }
305735fec99SHoratiu Vultur
306735fec99SHoratiu Vultur /* Commit back the result & save it */
307735fec99SHoratiu Vultur mutex_lock(&lan966x->ptp_lock);
308735fec99SHoratiu Vultur phc = &lan966x->phc[LAN966X_PHC_PORT];
30954e1ed69SVladimir Oltean phc->hwtstamp_config = *cfg;
310735fec99SHoratiu Vultur mutex_unlock(&lan966x->ptp_lock);
311735fec99SHoratiu Vultur
31254e1ed69SVladimir Oltean return 0;
313735fec99SHoratiu Vultur }
314735fec99SHoratiu Vultur
lan966x_ptp_hwtstamp_get(struct lan966x_port * port,struct kernel_hwtstamp_config * cfg)31554e1ed69SVladimir Oltean void lan966x_ptp_hwtstamp_get(struct lan966x_port *port,
31654e1ed69SVladimir Oltean struct kernel_hwtstamp_config *cfg)
317735fec99SHoratiu Vultur {
318735fec99SHoratiu Vultur struct lan966x *lan966x = port->lan966x;
319735fec99SHoratiu Vultur struct lan966x_phc *phc;
320735fec99SHoratiu Vultur
321735fec99SHoratiu Vultur phc = &lan966x->phc[LAN966X_PHC_PORT];
32254e1ed69SVladimir Oltean *cfg = phc->hwtstamp_config;
323735fec99SHoratiu Vultur }
324735fec99SHoratiu Vultur
lan966x_ptp_classify(struct lan966x_port * port,struct sk_buff * skb)32577eecf25SHoratiu Vultur static int lan966x_ptp_classify(struct lan966x_port *port, struct sk_buff *skb)
32677eecf25SHoratiu Vultur {
32777eecf25SHoratiu Vultur struct ptp_header *header;
32877eecf25SHoratiu Vultur u8 msgtype;
32977eecf25SHoratiu Vultur int type;
33077eecf25SHoratiu Vultur
331ff89ac70SHoratiu Vultur if (port->ptp_tx_cmd == IFH_REW_OP_NOOP)
33277eecf25SHoratiu Vultur return IFH_REW_OP_NOOP;
33377eecf25SHoratiu Vultur
33477eecf25SHoratiu Vultur type = ptp_classify_raw(skb);
33577eecf25SHoratiu Vultur if (type == PTP_CLASS_NONE)
33677eecf25SHoratiu Vultur return IFH_REW_OP_NOOP;
33777eecf25SHoratiu Vultur
33877eecf25SHoratiu Vultur header = ptp_parse_header(skb, type);
33977eecf25SHoratiu Vultur if (!header)
34077eecf25SHoratiu Vultur return IFH_REW_OP_NOOP;
34177eecf25SHoratiu Vultur
342ff89ac70SHoratiu Vultur if (port->ptp_tx_cmd == IFH_REW_OP_TWO_STEP_PTP)
34377eecf25SHoratiu Vultur return IFH_REW_OP_TWO_STEP_PTP;
34477eecf25SHoratiu Vultur
34577eecf25SHoratiu Vultur /* If it is sync and run 1 step then set the correct operation,
34677eecf25SHoratiu Vultur * otherwise run as 2 step
34777eecf25SHoratiu Vultur */
34877eecf25SHoratiu Vultur msgtype = ptp_get_msgtype(header, type);
34977eecf25SHoratiu Vultur if ((msgtype & 0xf) == 0)
35077eecf25SHoratiu Vultur return IFH_REW_OP_ONE_STEP_PTP;
35177eecf25SHoratiu Vultur
35277eecf25SHoratiu Vultur return IFH_REW_OP_TWO_STEP_PTP;
35377eecf25SHoratiu Vultur }
35477eecf25SHoratiu Vultur
lan966x_ptp_txtstamp_old_release(struct lan966x_port * port)35577eecf25SHoratiu Vultur static void lan966x_ptp_txtstamp_old_release(struct lan966x_port *port)
35677eecf25SHoratiu Vultur {
35777eecf25SHoratiu Vultur struct sk_buff *skb, *skb_tmp;
35877eecf25SHoratiu Vultur unsigned long flags;
35977eecf25SHoratiu Vultur
36077eecf25SHoratiu Vultur spin_lock_irqsave(&port->tx_skbs.lock, flags);
36177eecf25SHoratiu Vultur skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
36277eecf25SHoratiu Vultur if time_after(LAN966X_SKB_CB(skb)->jiffies + LAN966X_PTP_TIMEOUT,
36377eecf25SHoratiu Vultur jiffies)
36477eecf25SHoratiu Vultur break;
36577eecf25SHoratiu Vultur
36677eecf25SHoratiu Vultur __skb_unlink(skb, &port->tx_skbs);
36777eecf25SHoratiu Vultur dev_kfree_skb_any(skb);
36877eecf25SHoratiu Vultur }
36977eecf25SHoratiu Vultur spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
37077eecf25SHoratiu Vultur }
37177eecf25SHoratiu Vultur
lan966x_ptp_txtstamp_request(struct lan966x_port * port,struct sk_buff * skb)37277eecf25SHoratiu Vultur int lan966x_ptp_txtstamp_request(struct lan966x_port *port,
37377eecf25SHoratiu Vultur struct sk_buff *skb)
37477eecf25SHoratiu Vultur {
37577eecf25SHoratiu Vultur struct lan966x *lan966x = port->lan966x;
37677eecf25SHoratiu Vultur unsigned long flags;
37777eecf25SHoratiu Vultur u8 rew_op;
37877eecf25SHoratiu Vultur
37977eecf25SHoratiu Vultur rew_op = lan966x_ptp_classify(port, skb);
38077eecf25SHoratiu Vultur LAN966X_SKB_CB(skb)->rew_op = rew_op;
38177eecf25SHoratiu Vultur
38277eecf25SHoratiu Vultur if (rew_op != IFH_REW_OP_TWO_STEP_PTP)
38377eecf25SHoratiu Vultur return 0;
38477eecf25SHoratiu Vultur
38577eecf25SHoratiu Vultur lan966x_ptp_txtstamp_old_release(port);
38677eecf25SHoratiu Vultur
38777eecf25SHoratiu Vultur spin_lock_irqsave(&lan966x->ptp_ts_id_lock, flags);
38877eecf25SHoratiu Vultur if (lan966x->ptp_skbs == LAN966X_MAX_PTP_ID) {
38977eecf25SHoratiu Vultur spin_unlock_irqrestore(&lan966x->ptp_ts_id_lock, flags);
39077eecf25SHoratiu Vultur return -EBUSY;
39177eecf25SHoratiu Vultur }
39277eecf25SHoratiu Vultur
39377eecf25SHoratiu Vultur skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
39477eecf25SHoratiu Vultur
39577eecf25SHoratiu Vultur skb_queue_tail(&port->tx_skbs, skb);
39677eecf25SHoratiu Vultur LAN966X_SKB_CB(skb)->ts_id = port->ts_id;
39777eecf25SHoratiu Vultur LAN966X_SKB_CB(skb)->jiffies = jiffies;
39877eecf25SHoratiu Vultur
39977eecf25SHoratiu Vultur lan966x->ptp_skbs++;
40077eecf25SHoratiu Vultur port->ts_id++;
40177eecf25SHoratiu Vultur if (port->ts_id == LAN966X_MAX_PTP_ID)
40277eecf25SHoratiu Vultur port->ts_id = 0;
40377eecf25SHoratiu Vultur
40477eecf25SHoratiu Vultur spin_unlock_irqrestore(&lan966x->ptp_ts_id_lock, flags);
40577eecf25SHoratiu Vultur
40677eecf25SHoratiu Vultur return 0;
40777eecf25SHoratiu Vultur }
40877eecf25SHoratiu Vultur
lan966x_ptp_txtstamp_release(struct lan966x_port * port,struct sk_buff * skb)40977eecf25SHoratiu Vultur void lan966x_ptp_txtstamp_release(struct lan966x_port *port,
41077eecf25SHoratiu Vultur struct sk_buff *skb)
41177eecf25SHoratiu Vultur {
41277eecf25SHoratiu Vultur struct lan966x *lan966x = port->lan966x;
41377eecf25SHoratiu Vultur unsigned long flags;
41477eecf25SHoratiu Vultur
41577eecf25SHoratiu Vultur spin_lock_irqsave(&lan966x->ptp_ts_id_lock, flags);
41677eecf25SHoratiu Vultur port->ts_id--;
41777eecf25SHoratiu Vultur lan966x->ptp_skbs--;
41877eecf25SHoratiu Vultur skb_unlink(skb, &port->tx_skbs);
41977eecf25SHoratiu Vultur spin_unlock_irqrestore(&lan966x->ptp_ts_id_lock, flags);
42077eecf25SHoratiu Vultur }
42177eecf25SHoratiu Vultur
lan966x_get_hwtimestamp(struct lan966x * lan966x,struct timespec64 * ts,u32 nsec)422e85a96e4SHoratiu Vultur static void lan966x_get_hwtimestamp(struct lan966x *lan966x,
423e85a96e4SHoratiu Vultur struct timespec64 *ts,
424e85a96e4SHoratiu Vultur u32 nsec)
425e85a96e4SHoratiu Vultur {
426e85a96e4SHoratiu Vultur /* Read current PTP time to get seconds */
427e85a96e4SHoratiu Vultur unsigned long flags;
428e85a96e4SHoratiu Vultur u32 curr_nsec;
429e85a96e4SHoratiu Vultur
430e85a96e4SHoratiu Vultur spin_lock_irqsave(&lan966x->ptp_clock_lock, flags);
431e85a96e4SHoratiu Vultur
432e85a96e4SHoratiu Vultur lan_rmw(PTP_PIN_CFG_PIN_ACTION_SET(PTP_PIN_ACTION_SAVE) |
433e85a96e4SHoratiu Vultur PTP_PIN_CFG_PIN_DOM_SET(LAN966X_PHC_PORT) |
434e85a96e4SHoratiu Vultur PTP_PIN_CFG_PIN_SYNC_SET(0),
435e85a96e4SHoratiu Vultur PTP_PIN_CFG_PIN_ACTION |
436e85a96e4SHoratiu Vultur PTP_PIN_CFG_PIN_DOM |
437e85a96e4SHoratiu Vultur PTP_PIN_CFG_PIN_SYNC,
438e85a96e4SHoratiu Vultur lan966x, PTP_PIN_CFG(TOD_ACC_PIN));
439e85a96e4SHoratiu Vultur
440e85a96e4SHoratiu Vultur ts->tv_sec = lan_rd(lan966x, PTP_TOD_SEC_LSB(TOD_ACC_PIN));
441e85a96e4SHoratiu Vultur curr_nsec = lan_rd(lan966x, PTP_TOD_NSEC(TOD_ACC_PIN));
442e85a96e4SHoratiu Vultur
443e85a96e4SHoratiu Vultur ts->tv_nsec = nsec;
444e85a96e4SHoratiu Vultur
445e85a96e4SHoratiu Vultur /* Sec has incremented since the ts was registered */
446e85a96e4SHoratiu Vultur if (curr_nsec < nsec)
447e85a96e4SHoratiu Vultur ts->tv_sec--;
448e85a96e4SHoratiu Vultur
449e85a96e4SHoratiu Vultur spin_unlock_irqrestore(&lan966x->ptp_clock_lock, flags);
450e85a96e4SHoratiu Vultur }
451e85a96e4SHoratiu Vultur
lan966x_ptp_irq_handler(int irq,void * args)452e85a96e4SHoratiu Vultur irqreturn_t lan966x_ptp_irq_handler(int irq, void *args)
453e85a96e4SHoratiu Vultur {
454e85a96e4SHoratiu Vultur int budget = LAN966X_MAX_PTP_ID;
455e85a96e4SHoratiu Vultur struct lan966x *lan966x = args;
456e85a96e4SHoratiu Vultur
457e85a96e4SHoratiu Vultur while (budget--) {
458e85a96e4SHoratiu Vultur struct sk_buff *skb, *skb_tmp, *skb_match = NULL;
459e85a96e4SHoratiu Vultur struct skb_shared_hwtstamps shhwtstamps;
460e85a96e4SHoratiu Vultur struct lan966x_port *port;
461e85a96e4SHoratiu Vultur struct timespec64 ts;
462e85a96e4SHoratiu Vultur unsigned long flags;
463e85a96e4SHoratiu Vultur u32 val, id, txport;
464e85a96e4SHoratiu Vultur u32 delay;
465e85a96e4SHoratiu Vultur
466e85a96e4SHoratiu Vultur val = lan_rd(lan966x, PTP_TWOSTEP_CTRL);
467e85a96e4SHoratiu Vultur
468e85a96e4SHoratiu Vultur /* Check if a timestamp can be retrieved */
469e85a96e4SHoratiu Vultur if (!(val & PTP_TWOSTEP_CTRL_VLD))
470e85a96e4SHoratiu Vultur break;
471e85a96e4SHoratiu Vultur
472e85a96e4SHoratiu Vultur WARN_ON(val & PTP_TWOSTEP_CTRL_OVFL);
473e85a96e4SHoratiu Vultur
474e85a96e4SHoratiu Vultur if (!(val & PTP_TWOSTEP_CTRL_STAMP_TX))
475e85a96e4SHoratiu Vultur continue;
476e85a96e4SHoratiu Vultur
477e85a96e4SHoratiu Vultur /* Retrieve the ts Tx port */
478e85a96e4SHoratiu Vultur txport = PTP_TWOSTEP_CTRL_STAMP_PORT_GET(val);
479e85a96e4SHoratiu Vultur
480e85a96e4SHoratiu Vultur /* Retrieve its associated skb */
481e85a96e4SHoratiu Vultur port = lan966x->ports[txport];
482e85a96e4SHoratiu Vultur
483e85a96e4SHoratiu Vultur /* Retrieve the delay */
484e85a96e4SHoratiu Vultur delay = lan_rd(lan966x, PTP_TWOSTEP_STAMP);
485e85a96e4SHoratiu Vultur delay = PTP_TWOSTEP_STAMP_STAMP_NSEC_GET(delay);
486e85a96e4SHoratiu Vultur
487e85a96e4SHoratiu Vultur /* Get next timestamp from fifo, which needs to be the
488e85a96e4SHoratiu Vultur * rx timestamp which represents the id of the frame
489e85a96e4SHoratiu Vultur */
490e85a96e4SHoratiu Vultur lan_rmw(PTP_TWOSTEP_CTRL_NXT_SET(1),
491e85a96e4SHoratiu Vultur PTP_TWOSTEP_CTRL_NXT,
492e85a96e4SHoratiu Vultur lan966x, PTP_TWOSTEP_CTRL);
493e85a96e4SHoratiu Vultur
494e85a96e4SHoratiu Vultur val = lan_rd(lan966x, PTP_TWOSTEP_CTRL);
495e85a96e4SHoratiu Vultur
496e85a96e4SHoratiu Vultur /* Check if a timestamp can be retried */
497e85a96e4SHoratiu Vultur if (!(val & PTP_TWOSTEP_CTRL_VLD))
498e85a96e4SHoratiu Vultur break;
499e85a96e4SHoratiu Vultur
500e85a96e4SHoratiu Vultur /* Read RX timestamping to get the ID */
501e85a96e4SHoratiu Vultur id = lan_rd(lan966x, PTP_TWOSTEP_STAMP);
502e85a96e4SHoratiu Vultur
503e85a96e4SHoratiu Vultur spin_lock_irqsave(&port->tx_skbs.lock, flags);
504e85a96e4SHoratiu Vultur skb_queue_walk_safe(&port->tx_skbs, skb, skb_tmp) {
505e85a96e4SHoratiu Vultur if (LAN966X_SKB_CB(skb)->ts_id != id)
506e85a96e4SHoratiu Vultur continue;
507e85a96e4SHoratiu Vultur
508e85a96e4SHoratiu Vultur __skb_unlink(skb, &port->tx_skbs);
509e85a96e4SHoratiu Vultur skb_match = skb;
510e85a96e4SHoratiu Vultur break;
511e85a96e4SHoratiu Vultur }
512e85a96e4SHoratiu Vultur spin_unlock_irqrestore(&port->tx_skbs.lock, flags);
513e85a96e4SHoratiu Vultur
514e85a96e4SHoratiu Vultur /* Next ts */
515e85a96e4SHoratiu Vultur lan_rmw(PTP_TWOSTEP_CTRL_NXT_SET(1),
516e85a96e4SHoratiu Vultur PTP_TWOSTEP_CTRL_NXT,
517e85a96e4SHoratiu Vultur lan966x, PTP_TWOSTEP_CTRL);
518e85a96e4SHoratiu Vultur
519e85a96e4SHoratiu Vultur if (WARN_ON(!skb_match))
520e85a96e4SHoratiu Vultur continue;
521e85a96e4SHoratiu Vultur
5223a70e0d4SHoratiu Vultur spin_lock_irqsave(&lan966x->ptp_ts_id_lock, flags);
523e85a96e4SHoratiu Vultur lan966x->ptp_skbs--;
5243a70e0d4SHoratiu Vultur spin_unlock_irqrestore(&lan966x->ptp_ts_id_lock, flags);
525e85a96e4SHoratiu Vultur
526e85a96e4SHoratiu Vultur /* Get the h/w timestamp */
527e85a96e4SHoratiu Vultur lan966x_get_hwtimestamp(lan966x, &ts, delay);
528e85a96e4SHoratiu Vultur
529e85a96e4SHoratiu Vultur /* Set the timestamp into the skb */
530e85a96e4SHoratiu Vultur shhwtstamps.hwtstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
531e85a96e4SHoratiu Vultur skb_tstamp_tx(skb_match, &shhwtstamps);
532e85a96e4SHoratiu Vultur
533e85a96e4SHoratiu Vultur dev_kfree_skb_any(skb_match);
534e85a96e4SHoratiu Vultur }
535e85a96e4SHoratiu Vultur
536e85a96e4SHoratiu Vultur return IRQ_HANDLED;
537e85a96e4SHoratiu Vultur }
538e85a96e4SHoratiu Vultur
lan966x_ptp_ext_irq_handler(int irq,void * args)539f3d8e0a9SHoratiu Vultur irqreturn_t lan966x_ptp_ext_irq_handler(int irq, void *args)
540f3d8e0a9SHoratiu Vultur {
541f3d8e0a9SHoratiu Vultur struct lan966x *lan966x = args;
542f3d8e0a9SHoratiu Vultur struct lan966x_phc *phc;
543f3d8e0a9SHoratiu Vultur unsigned long flags;
544f3d8e0a9SHoratiu Vultur u64 time = 0;
545f3d8e0a9SHoratiu Vultur time64_t s;
546f3d8e0a9SHoratiu Vultur int pin, i;
547f3d8e0a9SHoratiu Vultur s64 ns;
548f3d8e0a9SHoratiu Vultur
549f3d8e0a9SHoratiu Vultur if (!(lan_rd(lan966x, PTP_PIN_INTR)))
550f3d8e0a9SHoratiu Vultur return IRQ_NONE;
551f3d8e0a9SHoratiu Vultur
552f3d8e0a9SHoratiu Vultur /* Go through all domains and see which pin generated the interrupt */
553f3d8e0a9SHoratiu Vultur for (i = 0; i < LAN966X_PHC_COUNT; ++i) {
554f3d8e0a9SHoratiu Vultur struct ptp_clock_event ptp_event = {0};
555f3d8e0a9SHoratiu Vultur
556f3d8e0a9SHoratiu Vultur phc = &lan966x->phc[i];
557f3d8e0a9SHoratiu Vultur pin = ptp_find_pin_unlocked(phc->clock, PTP_PF_EXTTS, 0);
558f3d8e0a9SHoratiu Vultur if (pin == -1)
559f3d8e0a9SHoratiu Vultur continue;
560f3d8e0a9SHoratiu Vultur
561f3d8e0a9SHoratiu Vultur if (!(lan_rd(lan966x, PTP_PIN_INTR) & BIT(pin)))
562f3d8e0a9SHoratiu Vultur continue;
563f3d8e0a9SHoratiu Vultur
564f3d8e0a9SHoratiu Vultur spin_lock_irqsave(&lan966x->ptp_clock_lock, flags);
565f3d8e0a9SHoratiu Vultur
566f3d8e0a9SHoratiu Vultur /* Enable to get the new interrupt.
567f3d8e0a9SHoratiu Vultur * By writing 1 it clears the bit
568f3d8e0a9SHoratiu Vultur */
569f3d8e0a9SHoratiu Vultur lan_wr(BIT(pin), lan966x, PTP_PIN_INTR);
570f3d8e0a9SHoratiu Vultur
571f3d8e0a9SHoratiu Vultur /* Get current time */
572f3d8e0a9SHoratiu Vultur s = lan_rd(lan966x, PTP_TOD_SEC_MSB(pin));
573f3d8e0a9SHoratiu Vultur s <<= 32;
574f3d8e0a9SHoratiu Vultur s |= lan_rd(lan966x, PTP_TOD_SEC_LSB(pin));
575f3d8e0a9SHoratiu Vultur ns = lan_rd(lan966x, PTP_TOD_NSEC(pin));
576f3d8e0a9SHoratiu Vultur ns &= PTP_TOD_NSEC_TOD_NSEC;
577f3d8e0a9SHoratiu Vultur
578f3d8e0a9SHoratiu Vultur spin_unlock_irqrestore(&lan966x->ptp_clock_lock, flags);
579f3d8e0a9SHoratiu Vultur
580f3d8e0a9SHoratiu Vultur if ((ns & 0xFFFFFFF0) == 0x3FFFFFF0) {
581f3d8e0a9SHoratiu Vultur s--;
582f3d8e0a9SHoratiu Vultur ns &= 0xf;
583f3d8e0a9SHoratiu Vultur ns += 999999984;
584f3d8e0a9SHoratiu Vultur }
585f3d8e0a9SHoratiu Vultur time = ktime_set(s, ns);
586f3d8e0a9SHoratiu Vultur
587f3d8e0a9SHoratiu Vultur ptp_event.index = pin;
588f3d8e0a9SHoratiu Vultur ptp_event.timestamp = time;
589f3d8e0a9SHoratiu Vultur ptp_event.type = PTP_CLOCK_EXTTS;
590f3d8e0a9SHoratiu Vultur ptp_clock_event(phc->clock, &ptp_event);
591f3d8e0a9SHoratiu Vultur }
592f3d8e0a9SHoratiu Vultur
593f3d8e0a9SHoratiu Vultur return IRQ_HANDLED;
594f3d8e0a9SHoratiu Vultur }
595f3d8e0a9SHoratiu Vultur
lan966x_ptp_adjfine(struct ptp_clock_info * ptp,long scaled_ppm)596d0964594SHoratiu Vultur static int lan966x_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
597d0964594SHoratiu Vultur {
598d0964594SHoratiu Vultur struct lan966x_phc *phc = container_of(ptp, struct lan966x_phc, info);
599d0964594SHoratiu Vultur struct lan966x *lan966x = phc->lan966x;
600d0964594SHoratiu Vultur unsigned long flags;
601d0964594SHoratiu Vultur bool neg_adj = 0;
602d0964594SHoratiu Vultur u64 tod_inc;
603d0964594SHoratiu Vultur u64 ref;
604d0964594SHoratiu Vultur
605d0964594SHoratiu Vultur if (!scaled_ppm)
606d0964594SHoratiu Vultur return 0;
607d0964594SHoratiu Vultur
608d0964594SHoratiu Vultur if (scaled_ppm < 0) {
609d0964594SHoratiu Vultur neg_adj = 1;
610d0964594SHoratiu Vultur scaled_ppm = -scaled_ppm;
611d0964594SHoratiu Vultur }
612d0964594SHoratiu Vultur
613d0964594SHoratiu Vultur tod_inc = lan966x_ptp_get_nominal_value();
614d0964594SHoratiu Vultur
615d0964594SHoratiu Vultur /* The multiplication is split in 2 separate additions because of
616d0964594SHoratiu Vultur * overflow issues. If scaled_ppm with 16bit fractional part was bigger
617d0964594SHoratiu Vultur * than 20ppm then we got overflow.
618d0964594SHoratiu Vultur */
619d0964594SHoratiu Vultur ref = LAN966X_1PPM_FORMAT * (scaled_ppm >> 16);
620d0964594SHoratiu Vultur ref += (LAN966X_1PPM_FORMAT * (0xffff & scaled_ppm)) >> 16;
621d0964594SHoratiu Vultur tod_inc = neg_adj ? tod_inc - ref : tod_inc + ref;
622d0964594SHoratiu Vultur
623d0964594SHoratiu Vultur spin_lock_irqsave(&lan966x->ptp_clock_lock, flags);
624d0964594SHoratiu Vultur
625d0964594SHoratiu Vultur lan_rmw(PTP_DOM_CFG_CLKCFG_DIS_SET(1 << BIT(phc->index)),
626d0964594SHoratiu Vultur PTP_DOM_CFG_CLKCFG_DIS,
627d0964594SHoratiu Vultur lan966x, PTP_DOM_CFG);
628d0964594SHoratiu Vultur
629d0964594SHoratiu Vultur lan_wr((u32)tod_inc & 0xFFFFFFFF, lan966x,
630d0964594SHoratiu Vultur PTP_CLK_PER_CFG(phc->index, 0));
631d0964594SHoratiu Vultur lan_wr((u32)(tod_inc >> 32), lan966x,
632d0964594SHoratiu Vultur PTP_CLK_PER_CFG(phc->index, 1));
633d0964594SHoratiu Vultur
634d0964594SHoratiu Vultur lan_rmw(PTP_DOM_CFG_CLKCFG_DIS_SET(0),
635d0964594SHoratiu Vultur PTP_DOM_CFG_CLKCFG_DIS,
636d0964594SHoratiu Vultur lan966x, PTP_DOM_CFG);
637d0964594SHoratiu Vultur
638d0964594SHoratiu Vultur spin_unlock_irqrestore(&lan966x->ptp_clock_lock, flags);
639d0964594SHoratiu Vultur
640d0964594SHoratiu Vultur return 0;
641d0964594SHoratiu Vultur }
642d0964594SHoratiu Vultur
lan966x_ptp_settime64(struct ptp_clock_info * ptp,const struct timespec64 * ts)643d0964594SHoratiu Vultur static int lan966x_ptp_settime64(struct ptp_clock_info *ptp,
644d0964594SHoratiu Vultur const struct timespec64 *ts)
645d0964594SHoratiu Vultur {
646d0964594SHoratiu Vultur struct lan966x_phc *phc = container_of(ptp, struct lan966x_phc, info);
647d0964594SHoratiu Vultur struct lan966x *lan966x = phc->lan966x;
648d0964594SHoratiu Vultur unsigned long flags;
649d0964594SHoratiu Vultur
650d0964594SHoratiu Vultur spin_lock_irqsave(&lan966x->ptp_clock_lock, flags);
651d0964594SHoratiu Vultur
652d0964594SHoratiu Vultur /* Must be in IDLE mode before the time can be loaded */
653d0964594SHoratiu Vultur lan_rmw(PTP_PIN_CFG_PIN_ACTION_SET(PTP_PIN_ACTION_IDLE) |
654d0964594SHoratiu Vultur PTP_PIN_CFG_PIN_DOM_SET(phc->index) |
655d0964594SHoratiu Vultur PTP_PIN_CFG_PIN_SYNC_SET(0),
656d0964594SHoratiu Vultur PTP_PIN_CFG_PIN_ACTION |
657d0964594SHoratiu Vultur PTP_PIN_CFG_PIN_DOM |
658d0964594SHoratiu Vultur PTP_PIN_CFG_PIN_SYNC,
659d0964594SHoratiu Vultur lan966x, PTP_PIN_CFG(TOD_ACC_PIN));
660d0964594SHoratiu Vultur
661d0964594SHoratiu Vultur /* Set new value */
662d0964594SHoratiu Vultur lan_wr(PTP_TOD_SEC_MSB_TOD_SEC_MSB_SET(upper_32_bits(ts->tv_sec)),
663d0964594SHoratiu Vultur lan966x, PTP_TOD_SEC_MSB(TOD_ACC_PIN));
664d0964594SHoratiu Vultur lan_wr(lower_32_bits(ts->tv_sec),
665d0964594SHoratiu Vultur lan966x, PTP_TOD_SEC_LSB(TOD_ACC_PIN));
666d0964594SHoratiu Vultur lan_wr(ts->tv_nsec, lan966x, PTP_TOD_NSEC(TOD_ACC_PIN));
667d0964594SHoratiu Vultur
668d0964594SHoratiu Vultur /* Apply new values */
669d0964594SHoratiu Vultur lan_rmw(PTP_PIN_CFG_PIN_ACTION_SET(PTP_PIN_ACTION_LOAD) |
670d0964594SHoratiu Vultur PTP_PIN_CFG_PIN_DOM_SET(phc->index) |
671d0964594SHoratiu Vultur PTP_PIN_CFG_PIN_SYNC_SET(0),
672d0964594SHoratiu Vultur PTP_PIN_CFG_PIN_ACTION |
673d0964594SHoratiu Vultur PTP_PIN_CFG_PIN_DOM |
674d0964594SHoratiu Vultur PTP_PIN_CFG_PIN_SYNC,
675d0964594SHoratiu Vultur lan966x, PTP_PIN_CFG(TOD_ACC_PIN));
676d0964594SHoratiu Vultur
677d0964594SHoratiu Vultur spin_unlock_irqrestore(&lan966x->ptp_clock_lock, flags);
678d0964594SHoratiu Vultur
679d0964594SHoratiu Vultur return 0;
680d0964594SHoratiu Vultur }
681d0964594SHoratiu Vultur
lan966x_ptp_gettime64(struct ptp_clock_info * ptp,struct timespec64 * ts)682e462b271SHoratiu Vultur int lan966x_ptp_gettime64(struct ptp_clock_info *ptp, struct timespec64 *ts)
683d0964594SHoratiu Vultur {
684d0964594SHoratiu Vultur struct lan966x_phc *phc = container_of(ptp, struct lan966x_phc, info);
685d0964594SHoratiu Vultur struct lan966x *lan966x = phc->lan966x;
686d0964594SHoratiu Vultur unsigned long flags;
687d0964594SHoratiu Vultur time64_t s;
688d0964594SHoratiu Vultur s64 ns;
689d0964594SHoratiu Vultur
690d0964594SHoratiu Vultur spin_lock_irqsave(&lan966x->ptp_clock_lock, flags);
691d0964594SHoratiu Vultur
692d0964594SHoratiu Vultur lan_rmw(PTP_PIN_CFG_PIN_ACTION_SET(PTP_PIN_ACTION_SAVE) |
693d0964594SHoratiu Vultur PTP_PIN_CFG_PIN_DOM_SET(phc->index) |
694d0964594SHoratiu Vultur PTP_PIN_CFG_PIN_SYNC_SET(0),
695d0964594SHoratiu Vultur PTP_PIN_CFG_PIN_ACTION |
696d0964594SHoratiu Vultur PTP_PIN_CFG_PIN_DOM |
697d0964594SHoratiu Vultur PTP_PIN_CFG_PIN_SYNC,
698d0964594SHoratiu Vultur lan966x, PTP_PIN_CFG(TOD_ACC_PIN));
699d0964594SHoratiu Vultur
700d0964594SHoratiu Vultur s = lan_rd(lan966x, PTP_TOD_SEC_MSB(TOD_ACC_PIN));
701d0964594SHoratiu Vultur s <<= 32;
702d0964594SHoratiu Vultur s |= lan_rd(lan966x, PTP_TOD_SEC_LSB(TOD_ACC_PIN));
703d0964594SHoratiu Vultur ns = lan_rd(lan966x, PTP_TOD_NSEC(TOD_ACC_PIN));
704d0964594SHoratiu Vultur ns &= PTP_TOD_NSEC_TOD_NSEC;
705d0964594SHoratiu Vultur
706d0964594SHoratiu Vultur spin_unlock_irqrestore(&lan966x->ptp_clock_lock, flags);
707d0964594SHoratiu Vultur
708d0964594SHoratiu Vultur /* Deal with negative values */
709d0964594SHoratiu Vultur if ((ns & 0xFFFFFFF0) == 0x3FFFFFF0) {
710d0964594SHoratiu Vultur s--;
711d0964594SHoratiu Vultur ns &= 0xf;
712d0964594SHoratiu Vultur ns += 999999984;
713d0964594SHoratiu Vultur }
714d0964594SHoratiu Vultur
715d0964594SHoratiu Vultur set_normalized_timespec64(ts, s, ns);
716d0964594SHoratiu Vultur return 0;
717d0964594SHoratiu Vultur }
718d0964594SHoratiu Vultur
lan966x_ptp_adjtime(struct ptp_clock_info * ptp,s64 delta)719d0964594SHoratiu Vultur static int lan966x_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
720d0964594SHoratiu Vultur {
721d0964594SHoratiu Vultur struct lan966x_phc *phc = container_of(ptp, struct lan966x_phc, info);
722d0964594SHoratiu Vultur struct lan966x *lan966x = phc->lan966x;
723d0964594SHoratiu Vultur
724d0964594SHoratiu Vultur if (delta > -(NSEC_PER_SEC / 2) && delta < (NSEC_PER_SEC / 2)) {
725d0964594SHoratiu Vultur unsigned long flags;
726d0964594SHoratiu Vultur
727d0964594SHoratiu Vultur spin_lock_irqsave(&lan966x->ptp_clock_lock, flags);
728d0964594SHoratiu Vultur
729d0964594SHoratiu Vultur /* Must be in IDLE mode before the time can be loaded */
730d0964594SHoratiu Vultur lan_rmw(PTP_PIN_CFG_PIN_ACTION_SET(PTP_PIN_ACTION_IDLE) |
731d0964594SHoratiu Vultur PTP_PIN_CFG_PIN_DOM_SET(phc->index) |
732d0964594SHoratiu Vultur PTP_PIN_CFG_PIN_SYNC_SET(0),
733d0964594SHoratiu Vultur PTP_PIN_CFG_PIN_ACTION |
734d0964594SHoratiu Vultur PTP_PIN_CFG_PIN_DOM |
735d0964594SHoratiu Vultur PTP_PIN_CFG_PIN_SYNC,
736d0964594SHoratiu Vultur lan966x, PTP_PIN_CFG(TOD_ACC_PIN));
737d0964594SHoratiu Vultur
738d0964594SHoratiu Vultur lan_wr(PTP_TOD_NSEC_TOD_NSEC_SET(delta),
739d0964594SHoratiu Vultur lan966x, PTP_TOD_NSEC(TOD_ACC_PIN));
740d0964594SHoratiu Vultur
741d0964594SHoratiu Vultur /* Adjust time with the value of PTP_TOD_NSEC */
742d0964594SHoratiu Vultur lan_rmw(PTP_PIN_CFG_PIN_ACTION_SET(PTP_PIN_ACTION_DELTA) |
743d0964594SHoratiu Vultur PTP_PIN_CFG_PIN_DOM_SET(phc->index) |
744d0964594SHoratiu Vultur PTP_PIN_CFG_PIN_SYNC_SET(0),
745d0964594SHoratiu Vultur PTP_PIN_CFG_PIN_ACTION |
746d0964594SHoratiu Vultur PTP_PIN_CFG_PIN_DOM |
747d0964594SHoratiu Vultur PTP_PIN_CFG_PIN_SYNC,
748d0964594SHoratiu Vultur lan966x, PTP_PIN_CFG(TOD_ACC_PIN));
749d0964594SHoratiu Vultur
750d0964594SHoratiu Vultur spin_unlock_irqrestore(&lan966x->ptp_clock_lock, flags);
751d0964594SHoratiu Vultur } else {
752d0964594SHoratiu Vultur /* Fall back using lan966x_ptp_settime64 which is not exact */
753d0964594SHoratiu Vultur struct timespec64 ts;
754d0964594SHoratiu Vultur u64 now;
755d0964594SHoratiu Vultur
756d0964594SHoratiu Vultur lan966x_ptp_gettime64(ptp, &ts);
757d0964594SHoratiu Vultur
758d0964594SHoratiu Vultur now = ktime_to_ns(timespec64_to_ktime(ts));
759d0964594SHoratiu Vultur ts = ns_to_timespec64(now + delta);
760d0964594SHoratiu Vultur
761d0964594SHoratiu Vultur lan966x_ptp_settime64(ptp, &ts);
762d0964594SHoratiu Vultur }
763d0964594SHoratiu Vultur
764d0964594SHoratiu Vultur return 0;
765d0964594SHoratiu Vultur }
766d0964594SHoratiu Vultur
lan966x_ptp_verify(struct ptp_clock_info * ptp,unsigned int pin,enum ptp_pin_function func,unsigned int chan)7672b7ff258SHoratiu Vultur static int lan966x_ptp_verify(struct ptp_clock_info *ptp, unsigned int pin,
7682b7ff258SHoratiu Vultur enum ptp_pin_function func, unsigned int chan)
7692b7ff258SHoratiu Vultur {
7702b7ff258SHoratiu Vultur struct lan966x_phc *phc = container_of(ptp, struct lan966x_phc, info);
7712b7ff258SHoratiu Vultur struct lan966x *lan966x = phc->lan966x;
7722b7ff258SHoratiu Vultur struct ptp_clock_info *info;
7732b7ff258SHoratiu Vultur int i;
7742b7ff258SHoratiu Vultur
7752b7ff258SHoratiu Vultur /* Currently support only 1 channel */
7762b7ff258SHoratiu Vultur if (chan != 0)
7772b7ff258SHoratiu Vultur return -1;
7782b7ff258SHoratiu Vultur
7792b7ff258SHoratiu Vultur switch (func) {
7802b7ff258SHoratiu Vultur case PTP_PF_NONE:
7812b7ff258SHoratiu Vultur case PTP_PF_PEROUT:
782f3d8e0a9SHoratiu Vultur case PTP_PF_EXTTS:
7832b7ff258SHoratiu Vultur break;
7842b7ff258SHoratiu Vultur default:
7852b7ff258SHoratiu Vultur return -1;
7862b7ff258SHoratiu Vultur }
7872b7ff258SHoratiu Vultur
7882b7ff258SHoratiu Vultur /* The PTP pins are shared by all the PHC. So it is required to see if
7892b7ff258SHoratiu Vultur * the pin is connected to another PHC. The pin is connected to another
7902b7ff258SHoratiu Vultur * PHC if that pin already has a function on that PHC.
7912b7ff258SHoratiu Vultur */
7922b7ff258SHoratiu Vultur for (i = 0; i < LAN966X_PHC_COUNT; ++i) {
7932b7ff258SHoratiu Vultur info = &lan966x->phc[i].info;
7942b7ff258SHoratiu Vultur
7952b7ff258SHoratiu Vultur /* Ignore the check with ourself */
7962b7ff258SHoratiu Vultur if (ptp == info)
7972b7ff258SHoratiu Vultur continue;
7982b7ff258SHoratiu Vultur
799f3d8e0a9SHoratiu Vultur if (info->pin_config[pin].func == PTP_PF_PEROUT ||
800f3d8e0a9SHoratiu Vultur info->pin_config[pin].func == PTP_PF_EXTTS)
8012b7ff258SHoratiu Vultur return -1;
8022b7ff258SHoratiu Vultur }
8032b7ff258SHoratiu Vultur
8042b7ff258SHoratiu Vultur return 0;
8052b7ff258SHoratiu Vultur }
8062b7ff258SHoratiu Vultur
lan966x_ptp_perout(struct ptp_clock_info * ptp,struct ptp_clock_request * rq,int on)8072b7ff258SHoratiu Vultur static int lan966x_ptp_perout(struct ptp_clock_info *ptp,
8082b7ff258SHoratiu Vultur struct ptp_clock_request *rq, int on)
8092b7ff258SHoratiu Vultur {
8102b7ff258SHoratiu Vultur struct lan966x_phc *phc = container_of(ptp, struct lan966x_phc, info);
8112b7ff258SHoratiu Vultur struct lan966x *lan966x = phc->lan966x;
8122b7ff258SHoratiu Vultur struct timespec64 ts_phase, ts_period;
8132b7ff258SHoratiu Vultur unsigned long flags;
8142b7ff258SHoratiu Vultur s64 wf_high, wf_low;
8152b7ff258SHoratiu Vultur bool pps = false;
8162b7ff258SHoratiu Vultur int pin;
8172b7ff258SHoratiu Vultur
8182b7ff258SHoratiu Vultur if (rq->perout.flags & ~(PTP_PEROUT_DUTY_CYCLE |
8192b7ff258SHoratiu Vultur PTP_PEROUT_PHASE))
8202b7ff258SHoratiu Vultur return -EOPNOTSUPP;
8212b7ff258SHoratiu Vultur
8222b7ff258SHoratiu Vultur pin = ptp_find_pin(phc->clock, PTP_PF_PEROUT, rq->perout.index);
8232b7ff258SHoratiu Vultur if (pin == -1 || pin >= LAN966X_PHC_PINS_NUM)
8242b7ff258SHoratiu Vultur return -EINVAL;
8252b7ff258SHoratiu Vultur
8262b7ff258SHoratiu Vultur if (!on) {
8272b7ff258SHoratiu Vultur spin_lock_irqsave(&lan966x->ptp_clock_lock, flags);
8282b7ff258SHoratiu Vultur lan_rmw(PTP_PIN_CFG_PIN_ACTION_SET(PTP_PIN_ACTION_IDLE) |
8292b7ff258SHoratiu Vultur PTP_PIN_CFG_PIN_DOM_SET(phc->index) |
8302b7ff258SHoratiu Vultur PTP_PIN_CFG_PIN_SYNC_SET(0),
8312b7ff258SHoratiu Vultur PTP_PIN_CFG_PIN_ACTION |
8322b7ff258SHoratiu Vultur PTP_PIN_CFG_PIN_DOM |
8332b7ff258SHoratiu Vultur PTP_PIN_CFG_PIN_SYNC,
8342b7ff258SHoratiu Vultur lan966x, PTP_PIN_CFG(pin));
8352b7ff258SHoratiu Vultur spin_unlock_irqrestore(&lan966x->ptp_clock_lock, flags);
8362b7ff258SHoratiu Vultur return 0;
8372b7ff258SHoratiu Vultur }
8382b7ff258SHoratiu Vultur
8392b7ff258SHoratiu Vultur if (rq->perout.period.sec == 1 &&
8402b7ff258SHoratiu Vultur rq->perout.period.nsec == 0)
8412b7ff258SHoratiu Vultur pps = true;
8422b7ff258SHoratiu Vultur
8432b7ff258SHoratiu Vultur if (rq->perout.flags & PTP_PEROUT_PHASE) {
8442b7ff258SHoratiu Vultur ts_phase.tv_sec = rq->perout.phase.sec;
8452b7ff258SHoratiu Vultur ts_phase.tv_nsec = rq->perout.phase.nsec;
8462b7ff258SHoratiu Vultur } else {
8472b7ff258SHoratiu Vultur ts_phase.tv_sec = rq->perout.start.sec;
8482b7ff258SHoratiu Vultur ts_phase.tv_nsec = rq->perout.start.nsec;
8492b7ff258SHoratiu Vultur }
8502b7ff258SHoratiu Vultur
8512b7ff258SHoratiu Vultur if (ts_phase.tv_sec || (ts_phase.tv_nsec && !pps)) {
8522b7ff258SHoratiu Vultur dev_warn(lan966x->dev,
8532b7ff258SHoratiu Vultur "Absolute time not supported!\n");
8542b7ff258SHoratiu Vultur return -EINVAL;
8552b7ff258SHoratiu Vultur }
8562b7ff258SHoratiu Vultur
8572b7ff258SHoratiu Vultur if (rq->perout.flags & PTP_PEROUT_DUTY_CYCLE) {
8582b7ff258SHoratiu Vultur struct timespec64 ts_on;
8592b7ff258SHoratiu Vultur
8602b7ff258SHoratiu Vultur ts_on.tv_sec = rq->perout.on.sec;
8612b7ff258SHoratiu Vultur ts_on.tv_nsec = rq->perout.on.nsec;
8622b7ff258SHoratiu Vultur
8632b7ff258SHoratiu Vultur wf_high = timespec64_to_ns(&ts_on);
8642b7ff258SHoratiu Vultur } else {
8652b7ff258SHoratiu Vultur wf_high = 5000;
8662b7ff258SHoratiu Vultur }
8672b7ff258SHoratiu Vultur
8682b7ff258SHoratiu Vultur if (pps) {
8692b7ff258SHoratiu Vultur spin_lock_irqsave(&lan966x->ptp_clock_lock, flags);
8702b7ff258SHoratiu Vultur lan_wr(PTP_WF_LOW_PERIOD_PIN_WFL(ts_phase.tv_nsec),
8712b7ff258SHoratiu Vultur lan966x, PTP_WF_LOW_PERIOD(pin));
8722b7ff258SHoratiu Vultur lan_wr(PTP_WF_HIGH_PERIOD_PIN_WFH(wf_high),
8732b7ff258SHoratiu Vultur lan966x, PTP_WF_HIGH_PERIOD(pin));
8742b7ff258SHoratiu Vultur lan_rmw(PTP_PIN_CFG_PIN_ACTION_SET(PTP_PIN_ACTION_CLOCK) |
8752b7ff258SHoratiu Vultur PTP_PIN_CFG_PIN_DOM_SET(phc->index) |
8762b7ff258SHoratiu Vultur PTP_PIN_CFG_PIN_SYNC_SET(3),
8772b7ff258SHoratiu Vultur PTP_PIN_CFG_PIN_ACTION |
8782b7ff258SHoratiu Vultur PTP_PIN_CFG_PIN_DOM |
8792b7ff258SHoratiu Vultur PTP_PIN_CFG_PIN_SYNC,
8802b7ff258SHoratiu Vultur lan966x, PTP_PIN_CFG(pin));
8812b7ff258SHoratiu Vultur spin_unlock_irqrestore(&lan966x->ptp_clock_lock, flags);
8822b7ff258SHoratiu Vultur return 0;
8832b7ff258SHoratiu Vultur }
8842b7ff258SHoratiu Vultur
8852b7ff258SHoratiu Vultur ts_period.tv_sec = rq->perout.period.sec;
8862b7ff258SHoratiu Vultur ts_period.tv_nsec = rq->perout.period.nsec;
8872b7ff258SHoratiu Vultur
8882b7ff258SHoratiu Vultur wf_low = timespec64_to_ns(&ts_period);
8892b7ff258SHoratiu Vultur wf_low -= wf_high;
8902b7ff258SHoratiu Vultur
8912b7ff258SHoratiu Vultur spin_lock_irqsave(&lan966x->ptp_clock_lock, flags);
8922b7ff258SHoratiu Vultur lan_wr(PTP_WF_LOW_PERIOD_PIN_WFL(wf_low),
8932b7ff258SHoratiu Vultur lan966x, PTP_WF_LOW_PERIOD(pin));
8942b7ff258SHoratiu Vultur lan_wr(PTP_WF_HIGH_PERIOD_PIN_WFH(wf_high),
8952b7ff258SHoratiu Vultur lan966x, PTP_WF_HIGH_PERIOD(pin));
8962b7ff258SHoratiu Vultur lan_rmw(PTP_PIN_CFG_PIN_ACTION_SET(PTP_PIN_ACTION_CLOCK) |
8972b7ff258SHoratiu Vultur PTP_PIN_CFG_PIN_DOM_SET(phc->index) |
8982b7ff258SHoratiu Vultur PTP_PIN_CFG_PIN_SYNC_SET(0),
8992b7ff258SHoratiu Vultur PTP_PIN_CFG_PIN_ACTION |
9002b7ff258SHoratiu Vultur PTP_PIN_CFG_PIN_DOM |
9012b7ff258SHoratiu Vultur PTP_PIN_CFG_PIN_SYNC,
9022b7ff258SHoratiu Vultur lan966x, PTP_PIN_CFG(pin));
9032b7ff258SHoratiu Vultur spin_unlock_irqrestore(&lan966x->ptp_clock_lock, flags);
9042b7ff258SHoratiu Vultur
9052b7ff258SHoratiu Vultur return 0;
9062b7ff258SHoratiu Vultur }
9072b7ff258SHoratiu Vultur
lan966x_ptp_extts(struct ptp_clock_info * ptp,struct ptp_clock_request * rq,int on)908f3d8e0a9SHoratiu Vultur static int lan966x_ptp_extts(struct ptp_clock_info *ptp,
909f3d8e0a9SHoratiu Vultur struct ptp_clock_request *rq, int on)
910f3d8e0a9SHoratiu Vultur {
911f3d8e0a9SHoratiu Vultur struct lan966x_phc *phc = container_of(ptp, struct lan966x_phc, info);
912f3d8e0a9SHoratiu Vultur struct lan966x *lan966x = phc->lan966x;
913f3d8e0a9SHoratiu Vultur unsigned long flags;
914f3d8e0a9SHoratiu Vultur int pin;
915f3d8e0a9SHoratiu Vultur u32 val;
916f3d8e0a9SHoratiu Vultur
917f3d8e0a9SHoratiu Vultur if (lan966x->ptp_ext_irq <= 0)
918f3d8e0a9SHoratiu Vultur return -EOPNOTSUPP;
919f3d8e0a9SHoratiu Vultur
920f3d8e0a9SHoratiu Vultur /* Reject requests with unsupported flags */
921f3d8e0a9SHoratiu Vultur if (rq->extts.flags & ~(PTP_ENABLE_FEATURE |
922f3d8e0a9SHoratiu Vultur PTP_RISING_EDGE |
923f3d8e0a9SHoratiu Vultur PTP_STRICT_FLAGS))
924f3d8e0a9SHoratiu Vultur return -EOPNOTSUPP;
925f3d8e0a9SHoratiu Vultur
926f3d8e0a9SHoratiu Vultur pin = ptp_find_pin(phc->clock, PTP_PF_EXTTS, rq->extts.index);
927f3d8e0a9SHoratiu Vultur if (pin == -1 || pin >= LAN966X_PHC_PINS_NUM)
928f3d8e0a9SHoratiu Vultur return -EINVAL;
929f3d8e0a9SHoratiu Vultur
930f3d8e0a9SHoratiu Vultur spin_lock_irqsave(&lan966x->ptp_clock_lock, flags);
931f3d8e0a9SHoratiu Vultur lan_rmw(PTP_PIN_CFG_PIN_ACTION_SET(PTP_PIN_ACTION_SAVE) |
932f3d8e0a9SHoratiu Vultur PTP_PIN_CFG_PIN_SYNC_SET(on ? 3 : 0) |
933f3d8e0a9SHoratiu Vultur PTP_PIN_CFG_PIN_DOM_SET(phc->index) |
934f3d8e0a9SHoratiu Vultur PTP_PIN_CFG_PIN_SELECT_SET(pin),
935f3d8e0a9SHoratiu Vultur PTP_PIN_CFG_PIN_ACTION |
936f3d8e0a9SHoratiu Vultur PTP_PIN_CFG_PIN_SYNC |
937f3d8e0a9SHoratiu Vultur PTP_PIN_CFG_PIN_DOM |
938f3d8e0a9SHoratiu Vultur PTP_PIN_CFG_PIN_SELECT,
939f3d8e0a9SHoratiu Vultur lan966x, PTP_PIN_CFG(pin));
940f3d8e0a9SHoratiu Vultur
941f3d8e0a9SHoratiu Vultur val = lan_rd(lan966x, PTP_PIN_INTR_ENA);
942f3d8e0a9SHoratiu Vultur if (on)
943f3d8e0a9SHoratiu Vultur val |= BIT(pin);
944f3d8e0a9SHoratiu Vultur else
945f3d8e0a9SHoratiu Vultur val &= ~BIT(pin);
946f3d8e0a9SHoratiu Vultur lan_wr(val, lan966x, PTP_PIN_INTR_ENA);
947f3d8e0a9SHoratiu Vultur
948f3d8e0a9SHoratiu Vultur spin_unlock_irqrestore(&lan966x->ptp_clock_lock, flags);
949f3d8e0a9SHoratiu Vultur
950f3d8e0a9SHoratiu Vultur return 0;
951f3d8e0a9SHoratiu Vultur }
952f3d8e0a9SHoratiu Vultur
lan966x_ptp_enable(struct ptp_clock_info * ptp,struct ptp_clock_request * rq,int on)9532b7ff258SHoratiu Vultur static int lan966x_ptp_enable(struct ptp_clock_info *ptp,
9542b7ff258SHoratiu Vultur struct ptp_clock_request *rq, int on)
9552b7ff258SHoratiu Vultur {
9562b7ff258SHoratiu Vultur switch (rq->type) {
9572b7ff258SHoratiu Vultur case PTP_CLK_REQ_PEROUT:
9582b7ff258SHoratiu Vultur return lan966x_ptp_perout(ptp, rq, on);
959f3d8e0a9SHoratiu Vultur case PTP_CLK_REQ_EXTTS:
960f3d8e0a9SHoratiu Vultur return lan966x_ptp_extts(ptp, rq, on);
9612b7ff258SHoratiu Vultur default:
9622b7ff258SHoratiu Vultur return -EOPNOTSUPP;
9632b7ff258SHoratiu Vultur }
9642b7ff258SHoratiu Vultur
9652b7ff258SHoratiu Vultur return 0;
9662b7ff258SHoratiu Vultur }
9672b7ff258SHoratiu Vultur
968d0964594SHoratiu Vultur static struct ptp_clock_info lan966x_ptp_clock_info = {
969d0964594SHoratiu Vultur .owner = THIS_MODULE,
970d0964594SHoratiu Vultur .name = "lan966x ptp",
971d0964594SHoratiu Vultur .max_adj = 200000,
972d0964594SHoratiu Vultur .gettime64 = lan966x_ptp_gettime64,
973d0964594SHoratiu Vultur .settime64 = lan966x_ptp_settime64,
974d0964594SHoratiu Vultur .adjtime = lan966x_ptp_adjtime,
975d0964594SHoratiu Vultur .adjfine = lan966x_ptp_adjfine,
9762b7ff258SHoratiu Vultur .verify = lan966x_ptp_verify,
9772b7ff258SHoratiu Vultur .enable = lan966x_ptp_enable,
9782b7ff258SHoratiu Vultur .n_per_out = LAN966X_PHC_PINS_NUM,
979f3d8e0a9SHoratiu Vultur .n_ext_ts = LAN966X_PHC_PINS_NUM,
9802b7ff258SHoratiu Vultur .n_pins = LAN966X_PHC_PINS_NUM,
981d0964594SHoratiu Vultur };
982d0964594SHoratiu Vultur
lan966x_ptp_phc_init(struct lan966x * lan966x,int index,struct ptp_clock_info * clock_info)983d0964594SHoratiu Vultur static int lan966x_ptp_phc_init(struct lan966x *lan966x,
984d0964594SHoratiu Vultur int index,
985d0964594SHoratiu Vultur struct ptp_clock_info *clock_info)
986d0964594SHoratiu Vultur {
987d0964594SHoratiu Vultur struct lan966x_phc *phc = &lan966x->phc[index];
9882b7ff258SHoratiu Vultur struct ptp_pin_desc *p;
9892b7ff258SHoratiu Vultur int i;
9902b7ff258SHoratiu Vultur
9912b7ff258SHoratiu Vultur for (i = 0; i < LAN966X_PHC_PINS_NUM; i++) {
9922b7ff258SHoratiu Vultur p = &phc->pins[i];
9932b7ff258SHoratiu Vultur
9942b7ff258SHoratiu Vultur snprintf(p->name, sizeof(p->name), "pin%d", i);
9952b7ff258SHoratiu Vultur p->index = i;
9962b7ff258SHoratiu Vultur p->func = PTP_PF_NONE;
9972b7ff258SHoratiu Vultur }
998d0964594SHoratiu Vultur
999d0964594SHoratiu Vultur phc->info = *clock_info;
10002b7ff258SHoratiu Vultur phc->info.pin_config = &phc->pins[0];
1001d0964594SHoratiu Vultur phc->clock = ptp_clock_register(&phc->info, lan966x->dev);
1002d0964594SHoratiu Vultur if (IS_ERR(phc->clock))
1003d0964594SHoratiu Vultur return PTR_ERR(phc->clock);
1004d0964594SHoratiu Vultur
1005d0964594SHoratiu Vultur phc->index = index;
1006d0964594SHoratiu Vultur phc->lan966x = lan966x;
1007d0964594SHoratiu Vultur
1008d0964594SHoratiu Vultur return 0;
1009d0964594SHoratiu Vultur }
1010d0964594SHoratiu Vultur
lan966x_ptp_init(struct lan966x * lan966x)1011d0964594SHoratiu Vultur int lan966x_ptp_init(struct lan966x *lan966x)
1012d0964594SHoratiu Vultur {
1013d0964594SHoratiu Vultur u64 tod_adj = lan966x_ptp_get_nominal_value();
101477eecf25SHoratiu Vultur struct lan966x_port *port;
1015d0964594SHoratiu Vultur int err, i;
1016d0964594SHoratiu Vultur
1017d0964594SHoratiu Vultur if (!lan966x->ptp)
1018d0964594SHoratiu Vultur return 0;
1019d0964594SHoratiu Vultur
1020d0964594SHoratiu Vultur for (i = 0; i < LAN966X_PHC_COUNT; ++i) {
1021d0964594SHoratiu Vultur err = lan966x_ptp_phc_init(lan966x, i, &lan966x_ptp_clock_info);
1022d0964594SHoratiu Vultur if (err)
1023d0964594SHoratiu Vultur return err;
1024d0964594SHoratiu Vultur }
1025d0964594SHoratiu Vultur
1026d0964594SHoratiu Vultur spin_lock_init(&lan966x->ptp_clock_lock);
102777eecf25SHoratiu Vultur spin_lock_init(&lan966x->ptp_ts_id_lock);
1028735fec99SHoratiu Vultur mutex_init(&lan966x->ptp_lock);
1029d0964594SHoratiu Vultur
1030d0964594SHoratiu Vultur /* Disable master counters */
1031d0964594SHoratiu Vultur lan_wr(PTP_DOM_CFG_ENA_SET(0), lan966x, PTP_DOM_CFG);
1032d0964594SHoratiu Vultur
1033d0964594SHoratiu Vultur /* Configure the nominal TOD increment per clock cycle */
1034d0964594SHoratiu Vultur lan_rmw(PTP_DOM_CFG_CLKCFG_DIS_SET(0x7),
1035d0964594SHoratiu Vultur PTP_DOM_CFG_CLKCFG_DIS,
1036d0964594SHoratiu Vultur lan966x, PTP_DOM_CFG);
1037d0964594SHoratiu Vultur
1038d0964594SHoratiu Vultur for (i = 0; i < LAN966X_PHC_COUNT; ++i) {
1039d0964594SHoratiu Vultur lan_wr((u32)tod_adj & 0xFFFFFFFF, lan966x,
1040d0964594SHoratiu Vultur PTP_CLK_PER_CFG(i, 0));
1041d0964594SHoratiu Vultur lan_wr((u32)(tod_adj >> 32), lan966x,
1042d0964594SHoratiu Vultur PTP_CLK_PER_CFG(i, 1));
1043d0964594SHoratiu Vultur }
1044d0964594SHoratiu Vultur
1045d0964594SHoratiu Vultur lan_rmw(PTP_DOM_CFG_CLKCFG_DIS_SET(0),
1046d0964594SHoratiu Vultur PTP_DOM_CFG_CLKCFG_DIS,
1047d0964594SHoratiu Vultur lan966x, PTP_DOM_CFG);
1048d0964594SHoratiu Vultur
1049d0964594SHoratiu Vultur /* Enable master counters */
1050d0964594SHoratiu Vultur lan_wr(PTP_DOM_CFG_ENA_SET(0x7), lan966x, PTP_DOM_CFG);
1051d0964594SHoratiu Vultur
105277eecf25SHoratiu Vultur for (i = 0; i < lan966x->num_phys_ports; i++) {
105377eecf25SHoratiu Vultur port = lan966x->ports[i];
105477eecf25SHoratiu Vultur if (!port)
105577eecf25SHoratiu Vultur continue;
105677eecf25SHoratiu Vultur
105777eecf25SHoratiu Vultur skb_queue_head_init(&port->tx_skbs);
105877eecf25SHoratiu Vultur }
105977eecf25SHoratiu Vultur
1060d0964594SHoratiu Vultur return 0;
1061d0964594SHoratiu Vultur }
1062d0964594SHoratiu Vultur
lan966x_ptp_deinit(struct lan966x * lan966x)1063d0964594SHoratiu Vultur void lan966x_ptp_deinit(struct lan966x *lan966x)
1064d0964594SHoratiu Vultur {
106577eecf25SHoratiu Vultur struct lan966x_port *port;
1066d0964594SHoratiu Vultur int i;
1067d0964594SHoratiu Vultur
1068b0e380b5SClément Léger if (!lan966x->ptp)
1069b0e380b5SClément Léger return;
1070b0e380b5SClément Léger
107177eecf25SHoratiu Vultur for (i = 0; i < lan966x->num_phys_ports; i++) {
107277eecf25SHoratiu Vultur port = lan966x->ports[i];
107377eecf25SHoratiu Vultur if (!port)
107477eecf25SHoratiu Vultur continue;
107577eecf25SHoratiu Vultur
107677eecf25SHoratiu Vultur skb_queue_purge(&port->tx_skbs);
107777eecf25SHoratiu Vultur }
107877eecf25SHoratiu Vultur
1079d0964594SHoratiu Vultur for (i = 0; i < LAN966X_PHC_COUNT; ++i)
1080d0964594SHoratiu Vultur ptp_clock_unregister(lan966x->phc[i].clock);
1081d0964594SHoratiu Vultur }
108277eecf25SHoratiu Vultur
lan966x_ptp_rxtstamp(struct lan966x * lan966x,struct sk_buff * skb,u64 src_port,u64 timestamp)108377eecf25SHoratiu Vultur void lan966x_ptp_rxtstamp(struct lan966x *lan966x, struct sk_buff *skb,
1084ff89ac70SHoratiu Vultur u64 src_port, u64 timestamp)
108577eecf25SHoratiu Vultur {
108677eecf25SHoratiu Vultur struct skb_shared_hwtstamps *shhwtstamps;
108777eecf25SHoratiu Vultur struct lan966x_phc *phc;
108877eecf25SHoratiu Vultur struct timespec64 ts;
108977eecf25SHoratiu Vultur u64 full_ts_in_ns;
109077eecf25SHoratiu Vultur
1091ff89ac70SHoratiu Vultur if (!lan966x->ptp ||
1092ff89ac70SHoratiu Vultur !lan966x->ports[src_port]->ptp_rx_cmd)
109377eecf25SHoratiu Vultur return;
109477eecf25SHoratiu Vultur
109577eecf25SHoratiu Vultur phc = &lan966x->phc[LAN966X_PHC_PORT];
109677eecf25SHoratiu Vultur lan966x_ptp_gettime64(&phc->info, &ts);
109777eecf25SHoratiu Vultur
109877eecf25SHoratiu Vultur /* Drop the sub-ns precision */
109977eecf25SHoratiu Vultur timestamp = timestamp >> 2;
110077eecf25SHoratiu Vultur if (ts.tv_nsec < timestamp)
110177eecf25SHoratiu Vultur ts.tv_sec--;
110277eecf25SHoratiu Vultur ts.tv_nsec = timestamp;
110377eecf25SHoratiu Vultur full_ts_in_ns = ktime_set(ts.tv_sec, ts.tv_nsec);
110477eecf25SHoratiu Vultur
110577eecf25SHoratiu Vultur shhwtstamps = skb_hwtstamps(skb);
110677eecf25SHoratiu Vultur shhwtstamps->hwtstamp = full_ts_in_ns;
110777eecf25SHoratiu Vultur }
1108e462b271SHoratiu Vultur
lan966x_ptp_get_period_ps(void)1109e462b271SHoratiu Vultur u32 lan966x_ptp_get_period_ps(void)
1110e462b271SHoratiu Vultur {
1111e462b271SHoratiu Vultur /* This represents the system clock period in picoseconds */
1112e462b271SHoratiu Vultur return 15125;
1113e462b271SHoratiu Vultur }
1114