ptp_ines.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Copyright (C) 2018 MOSER-BAER AG
  4. //
  5. #define pr_fmt(fmt) "InES_PTP: " fmt
  6. #include <linux/ethtool.h>
  7. #include <linux/export.h>
  8. #include <linux/if_vlan.h>
  9. #include <linux/mii_timestamper.h>
  10. #include <linux/module.h>
  11. #include <linux/net_tstamp.h>
  12. #include <linux/of.h>
  13. #include <linux/of_address.h>
  14. #include <linux/of_irq.h>
  15. #include <linux/phy.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/ptp_classify.h>
  18. #include <linux/ptp_clock_kernel.h>
  19. #include <linux/stddef.h>
  20. MODULE_DESCRIPTION("Driver for the ZHAW InES PTP time stamping IP core");
  21. MODULE_AUTHOR("Richard Cochran <[email protected]>");
  22. MODULE_VERSION("1.0");
  23. MODULE_LICENSE("GPL");
  24. /* GLOBAL register */
  25. #define MCAST_MAC_SELECT_SHIFT 2
  26. #define MCAST_MAC_SELECT_MASK 0x3
  27. #define IO_RESET BIT(1)
  28. #define PTP_RESET BIT(0)
  29. /* VERSION register */
  30. #define IF_MAJOR_VER_SHIFT 12
  31. #define IF_MAJOR_VER_MASK 0xf
  32. #define IF_MINOR_VER_SHIFT 8
  33. #define IF_MINOR_VER_MASK 0xf
  34. #define FPGA_MAJOR_VER_SHIFT 4
  35. #define FPGA_MAJOR_VER_MASK 0xf
  36. #define FPGA_MINOR_VER_SHIFT 0
  37. #define FPGA_MINOR_VER_MASK 0xf
  38. /* INT_STAT register */
  39. #define RX_INTR_STATUS_3 BIT(5)
  40. #define RX_INTR_STATUS_2 BIT(4)
  41. #define RX_INTR_STATUS_1 BIT(3)
  42. #define TX_INTR_STATUS_3 BIT(2)
  43. #define TX_INTR_STATUS_2 BIT(1)
  44. #define TX_INTR_STATUS_1 BIT(0)
  45. /* INT_MSK register */
  46. #define RX_INTR_MASK_3 BIT(5)
  47. #define RX_INTR_MASK_2 BIT(4)
  48. #define RX_INTR_MASK_1 BIT(3)
  49. #define TX_INTR_MASK_3 BIT(2)
  50. #define TX_INTR_MASK_2 BIT(1)
  51. #define TX_INTR_MASK_1 BIT(0)
  52. /* BUF_STAT register */
  53. #define RX_FIFO_NE_3 BIT(5)
  54. #define RX_FIFO_NE_2 BIT(4)
  55. #define RX_FIFO_NE_1 BIT(3)
  56. #define TX_FIFO_NE_3 BIT(2)
  57. #define TX_FIFO_NE_2 BIT(1)
  58. #define TX_FIFO_NE_1 BIT(0)
  59. /* PORT_CONF register */
  60. #define CM_ONE_STEP BIT(6)
  61. #define PHY_SPEED_SHIFT 4
  62. #define PHY_SPEED_MASK 0x3
  63. #define P2P_DELAY_WR_POS_SHIFT 2
  64. #define P2P_DELAY_WR_POS_MASK 0x3
  65. #define PTP_MODE_SHIFT 0
  66. #define PTP_MODE_MASK 0x3
  67. /* TS_STAT_TX register */
  68. #define TS_ENABLE BIT(15)
  69. #define DATA_READ_POS_SHIFT 8
  70. #define DATA_READ_POS_MASK 0x1f
  71. #define DISCARDED_EVENTS_SHIFT 4
  72. #define DISCARDED_EVENTS_MASK 0xf
  73. #define INES_N_PORTS 3
  74. #define INES_REGISTER_SIZE 0x80
  75. #define INES_PORT_OFFSET 0x20
  76. #define INES_PORT_SIZE 0x20
  77. #define INES_FIFO_DEPTH 90
  78. #define INES_MAX_EVENTS 100
  79. #define BC_PTP_V1 0
  80. #define BC_PTP_V2 1
  81. #define TC_E2E_PTP_V2 2
  82. #define TC_P2P_PTP_V2 3
  83. #define PHY_SPEED_10 0
  84. #define PHY_SPEED_100 1
  85. #define PHY_SPEED_1000 2
  86. #define PORT_CONF \
  87. ((PHY_SPEED_1000 << PHY_SPEED_SHIFT) | (BC_PTP_V2 << PTP_MODE_SHIFT))
  88. #define ines_read32(s, r) __raw_readl((void __iomem *)&s->regs->r)
  89. #define ines_write32(s, v, r) __raw_writel(v, (void __iomem *)&s->regs->r)
  90. #define MESSAGE_TYPE_SYNC 1
  91. #define MESSAGE_TYPE_P_DELAY_REQ 2
  92. #define MESSAGE_TYPE_P_DELAY_RESP 3
  93. #define MESSAGE_TYPE_DELAY_REQ 4
  94. static LIST_HEAD(ines_clocks);
  95. static DEFINE_MUTEX(ines_clocks_lock);
  96. struct ines_global_regs {
  97. u32 id;
  98. u32 test;
  99. u32 global;
  100. u32 version;
  101. u32 test2;
  102. u32 int_stat;
  103. u32 int_msk;
  104. u32 buf_stat;
  105. };
  106. struct ines_port_registers {
  107. u32 port_conf;
  108. u32 p_delay;
  109. u32 ts_stat_tx;
  110. u32 ts_stat_rx;
  111. u32 ts_tx;
  112. u32 ts_rx;
  113. };
  114. struct ines_timestamp {
  115. struct list_head list;
  116. unsigned long tmo;
  117. u16 tag;
  118. u64 sec;
  119. u64 nsec;
  120. u64 clkid;
  121. u16 portnum;
  122. u16 seqid;
  123. };
  124. struct ines_port {
  125. struct ines_port_registers *regs;
  126. struct mii_timestamper mii_ts;
  127. struct ines_clock *clock;
  128. bool rxts_enabled;
  129. bool txts_enabled;
  130. unsigned int index;
  131. struct delayed_work ts_work;
  132. /* lock protects event list and tx_skb */
  133. spinlock_t lock;
  134. struct sk_buff *tx_skb;
  135. struct list_head events;
  136. struct list_head pool;
  137. struct ines_timestamp pool_data[INES_MAX_EVENTS];
  138. };
  139. struct ines_clock {
  140. struct ines_port port[INES_N_PORTS];
  141. struct ines_global_regs __iomem *regs;
  142. void __iomem *base;
  143. struct device_node *node;
  144. struct device *dev;
  145. struct list_head list;
  146. };
  147. static bool ines_match(struct sk_buff *skb, unsigned int ptp_class,
  148. struct ines_timestamp *ts, struct device *dev);
  149. static int ines_rxfifo_read(struct ines_port *port);
  150. static u64 ines_rxts64(struct ines_port *port, unsigned int words);
  151. static bool ines_timestamp_expired(struct ines_timestamp *ts);
  152. static u64 ines_txts64(struct ines_port *port, unsigned int words);
  153. static void ines_txtstamp_work(struct work_struct *work);
  154. static bool is_sync_pdelay_resp(struct sk_buff *skb, int type);
  155. static u8 tag_to_msgtype(u8 tag);
  156. static void ines_clock_cleanup(struct ines_clock *clock)
  157. {
  158. struct ines_port *port;
  159. int i;
  160. for (i = 0; i < INES_N_PORTS; i++) {
  161. port = &clock->port[i];
  162. cancel_delayed_work_sync(&port->ts_work);
  163. }
  164. }
  165. static int ines_clock_init(struct ines_clock *clock, struct device *device,
  166. void __iomem *addr)
  167. {
  168. struct device_node *node = device->of_node;
  169. unsigned long port_addr;
  170. struct ines_port *port;
  171. int i, j;
  172. INIT_LIST_HEAD(&clock->list);
  173. clock->node = node;
  174. clock->dev = device;
  175. clock->base = addr;
  176. clock->regs = clock->base;
  177. for (i = 0; i < INES_N_PORTS; i++) {
  178. port = &clock->port[i];
  179. port_addr = (unsigned long) clock->base +
  180. INES_PORT_OFFSET + i * INES_PORT_SIZE;
  181. port->regs = (struct ines_port_registers *) port_addr;
  182. port->clock = clock;
  183. port->index = i;
  184. INIT_DELAYED_WORK(&port->ts_work, ines_txtstamp_work);
  185. spin_lock_init(&port->lock);
  186. INIT_LIST_HEAD(&port->events);
  187. INIT_LIST_HEAD(&port->pool);
  188. for (j = 0; j < INES_MAX_EVENTS; j++)
  189. list_add(&port->pool_data[j].list, &port->pool);
  190. }
  191. ines_write32(clock, 0xBEEF, test);
  192. ines_write32(clock, 0xBEEF, test2);
  193. dev_dbg(device, "ID 0x%x\n", ines_read32(clock, id));
  194. dev_dbg(device, "TEST 0x%x\n", ines_read32(clock, test));
  195. dev_dbg(device, "VERSION 0x%x\n", ines_read32(clock, version));
  196. dev_dbg(device, "TEST2 0x%x\n", ines_read32(clock, test2));
  197. for (i = 0; i < INES_N_PORTS; i++) {
  198. port = &clock->port[i];
  199. ines_write32(port, PORT_CONF, port_conf);
  200. }
  201. return 0;
  202. }
  203. static struct ines_port *ines_find_port(struct device_node *node, u32 index)
  204. {
  205. struct ines_port *port = NULL;
  206. struct ines_clock *clock;
  207. struct list_head *this;
  208. mutex_lock(&ines_clocks_lock);
  209. list_for_each(this, &ines_clocks) {
  210. clock = list_entry(this, struct ines_clock, list);
  211. if (clock->node == node) {
  212. port = &clock->port[index];
  213. break;
  214. }
  215. }
  216. mutex_unlock(&ines_clocks_lock);
  217. return port;
  218. }
  219. static u64 ines_find_rxts(struct ines_port *port, struct sk_buff *skb, int type)
  220. {
  221. struct list_head *this, *next;
  222. struct ines_timestamp *ts;
  223. unsigned long flags;
  224. u64 ns = 0;
  225. if (type == PTP_CLASS_NONE)
  226. return 0;
  227. spin_lock_irqsave(&port->lock, flags);
  228. ines_rxfifo_read(port);
  229. list_for_each_safe(this, next, &port->events) {
  230. ts = list_entry(this, struct ines_timestamp, list);
  231. if (ines_timestamp_expired(ts)) {
  232. list_del_init(&ts->list);
  233. list_add(&ts->list, &port->pool);
  234. continue;
  235. }
  236. if (ines_match(skb, type, ts, port->clock->dev)) {
  237. ns = ts->sec * 1000000000ULL + ts->nsec;
  238. list_del_init(&ts->list);
  239. list_add(&ts->list, &port->pool);
  240. break;
  241. }
  242. }
  243. spin_unlock_irqrestore(&port->lock, flags);
  244. return ns;
  245. }
  246. static u64 ines_find_txts(struct ines_port *port, struct sk_buff *skb)
  247. {
  248. unsigned int class = ptp_classify_raw(skb), i;
  249. u32 data_rd_pos, buf_stat, mask, ts_stat_tx;
  250. struct ines_timestamp ts;
  251. unsigned long flags;
  252. u64 ns = 0;
  253. mask = TX_FIFO_NE_1 << port->index;
  254. spin_lock_irqsave(&port->lock, flags);
  255. for (i = 0; i < INES_FIFO_DEPTH; i++) {
  256. buf_stat = ines_read32(port->clock, buf_stat);
  257. if (!(buf_stat & mask)) {
  258. dev_dbg(port->clock->dev,
  259. "Tx timestamp FIFO unexpectedly empty\n");
  260. break;
  261. }
  262. ts_stat_tx = ines_read32(port, ts_stat_tx);
  263. data_rd_pos = (ts_stat_tx >> DATA_READ_POS_SHIFT) &
  264. DATA_READ_POS_MASK;
  265. if (data_rd_pos) {
  266. dev_err(port->clock->dev,
  267. "unexpected Tx read pos %u\n", data_rd_pos);
  268. break;
  269. }
  270. ts.tag = ines_read32(port, ts_tx);
  271. ts.sec = ines_txts64(port, 3);
  272. ts.nsec = ines_txts64(port, 2);
  273. ts.clkid = ines_txts64(port, 4);
  274. ts.portnum = ines_read32(port, ts_tx);
  275. ts.seqid = ines_read32(port, ts_tx);
  276. if (ines_match(skb, class, &ts, port->clock->dev)) {
  277. ns = ts.sec * 1000000000ULL + ts.nsec;
  278. break;
  279. }
  280. }
  281. spin_unlock_irqrestore(&port->lock, flags);
  282. return ns;
  283. }
  284. static int ines_hwtstamp(struct mii_timestamper *mii_ts, struct ifreq *ifr)
  285. {
  286. struct ines_port *port = container_of(mii_ts, struct ines_port, mii_ts);
  287. u32 cm_one_step = 0, port_conf, ts_stat_rx, ts_stat_tx;
  288. struct hwtstamp_config cfg;
  289. unsigned long flags;
  290. if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
  291. return -EFAULT;
  292. switch (cfg.tx_type) {
  293. case HWTSTAMP_TX_OFF:
  294. ts_stat_tx = 0;
  295. break;
  296. case HWTSTAMP_TX_ON:
  297. ts_stat_tx = TS_ENABLE;
  298. break;
  299. case HWTSTAMP_TX_ONESTEP_P2P:
  300. ts_stat_tx = TS_ENABLE;
  301. cm_one_step = CM_ONE_STEP;
  302. break;
  303. default:
  304. return -ERANGE;
  305. }
  306. switch (cfg.rx_filter) {
  307. case HWTSTAMP_FILTER_NONE:
  308. ts_stat_rx = 0;
  309. break;
  310. case HWTSTAMP_FILTER_ALL:
  311. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  312. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  313. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  314. return -ERANGE;
  315. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  316. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  317. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  318. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  319. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  320. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  321. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  322. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  323. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  324. ts_stat_rx = TS_ENABLE;
  325. cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  326. break;
  327. default:
  328. return -ERANGE;
  329. }
  330. spin_lock_irqsave(&port->lock, flags);
  331. port_conf = ines_read32(port, port_conf);
  332. port_conf &= ~CM_ONE_STEP;
  333. port_conf |= cm_one_step;
  334. ines_write32(port, port_conf, port_conf);
  335. ines_write32(port, ts_stat_rx, ts_stat_rx);
  336. ines_write32(port, ts_stat_tx, ts_stat_tx);
  337. port->rxts_enabled = ts_stat_rx == TS_ENABLE;
  338. port->txts_enabled = ts_stat_tx == TS_ENABLE;
  339. spin_unlock_irqrestore(&port->lock, flags);
  340. return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
  341. }
  342. static void ines_link_state(struct mii_timestamper *mii_ts,
  343. struct phy_device *phydev)
  344. {
  345. struct ines_port *port = container_of(mii_ts, struct ines_port, mii_ts);
  346. u32 port_conf, speed_conf;
  347. unsigned long flags;
  348. switch (phydev->speed) {
  349. case SPEED_10:
  350. speed_conf = PHY_SPEED_10 << PHY_SPEED_SHIFT;
  351. break;
  352. case SPEED_100:
  353. speed_conf = PHY_SPEED_100 << PHY_SPEED_SHIFT;
  354. break;
  355. case SPEED_1000:
  356. speed_conf = PHY_SPEED_1000 << PHY_SPEED_SHIFT;
  357. break;
  358. default:
  359. dev_err(port->clock->dev, "bad speed: %d\n", phydev->speed);
  360. return;
  361. }
  362. spin_lock_irqsave(&port->lock, flags);
  363. port_conf = ines_read32(port, port_conf);
  364. port_conf &= ~(0x3 << PHY_SPEED_SHIFT);
  365. port_conf |= speed_conf;
  366. ines_write32(port, port_conf, port_conf);
  367. spin_unlock_irqrestore(&port->lock, flags);
  368. }
  369. static bool ines_match(struct sk_buff *skb, unsigned int ptp_class,
  370. struct ines_timestamp *ts, struct device *dev)
  371. {
  372. struct ptp_header *hdr;
  373. u16 portn, seqid;
  374. u8 msgtype;
  375. u64 clkid;
  376. if (unlikely(ptp_class & PTP_CLASS_V1))
  377. return false;
  378. hdr = ptp_parse_header(skb, ptp_class);
  379. if (!hdr)
  380. return false;
  381. msgtype = ptp_get_msgtype(hdr, ptp_class);
  382. clkid = be64_to_cpup((__be64 *)&hdr->source_port_identity.clock_identity.id[0]);
  383. portn = be16_to_cpu(hdr->source_port_identity.port_number);
  384. seqid = be16_to_cpu(hdr->sequence_id);
  385. if (tag_to_msgtype(ts->tag & 0x7) != msgtype) {
  386. dev_dbg(dev, "msgtype mismatch ts %hhu != skb %hhu\n",
  387. tag_to_msgtype(ts->tag & 0x7), msgtype);
  388. return false;
  389. }
  390. if (ts->clkid != clkid) {
  391. dev_dbg(dev, "clkid mismatch ts %llx != skb %llx\n",
  392. ts->clkid, clkid);
  393. return false;
  394. }
  395. if (ts->portnum != portn) {
  396. dev_dbg(dev, "portn mismatch ts %hu != skb %hu\n",
  397. ts->portnum, portn);
  398. return false;
  399. }
  400. if (ts->seqid != seqid) {
  401. dev_dbg(dev, "seqid mismatch ts %hu != skb %hu\n",
  402. ts->seqid, seqid);
  403. return false;
  404. }
  405. return true;
  406. }
  407. static bool ines_rxtstamp(struct mii_timestamper *mii_ts,
  408. struct sk_buff *skb, int type)
  409. {
  410. struct ines_port *port = container_of(mii_ts, struct ines_port, mii_ts);
  411. struct skb_shared_hwtstamps *ssh;
  412. u64 ns;
  413. if (!port->rxts_enabled)
  414. return false;
  415. ns = ines_find_rxts(port, skb, type);
  416. if (!ns)
  417. return false;
  418. ssh = skb_hwtstamps(skb);
  419. ssh->hwtstamp = ns_to_ktime(ns);
  420. netif_rx(skb);
  421. return true;
  422. }
  423. static int ines_rxfifo_read(struct ines_port *port)
  424. {
  425. u32 data_rd_pos, buf_stat, mask, ts_stat_rx;
  426. struct ines_timestamp *ts;
  427. unsigned int i;
  428. mask = RX_FIFO_NE_1 << port->index;
  429. for (i = 0; i < INES_FIFO_DEPTH; i++) {
  430. if (list_empty(&port->pool)) {
  431. dev_err(port->clock->dev, "event pool is empty\n");
  432. return -1;
  433. }
  434. buf_stat = ines_read32(port->clock, buf_stat);
  435. if (!(buf_stat & mask))
  436. break;
  437. ts_stat_rx = ines_read32(port, ts_stat_rx);
  438. data_rd_pos = (ts_stat_rx >> DATA_READ_POS_SHIFT) &
  439. DATA_READ_POS_MASK;
  440. if (data_rd_pos) {
  441. dev_err(port->clock->dev, "unexpected Rx read pos %u\n",
  442. data_rd_pos);
  443. break;
  444. }
  445. ts = list_first_entry(&port->pool, struct ines_timestamp, list);
  446. ts->tmo = jiffies + HZ;
  447. ts->tag = ines_read32(port, ts_rx);
  448. ts->sec = ines_rxts64(port, 3);
  449. ts->nsec = ines_rxts64(port, 2);
  450. ts->clkid = ines_rxts64(port, 4);
  451. ts->portnum = ines_read32(port, ts_rx);
  452. ts->seqid = ines_read32(port, ts_rx);
  453. list_del_init(&ts->list);
  454. list_add_tail(&ts->list, &port->events);
  455. }
  456. return 0;
  457. }
  458. static u64 ines_rxts64(struct ines_port *port, unsigned int words)
  459. {
  460. unsigned int i;
  461. u64 result;
  462. u16 word;
  463. word = ines_read32(port, ts_rx);
  464. result = word;
  465. words--;
  466. for (i = 0; i < words; i++) {
  467. word = ines_read32(port, ts_rx);
  468. result <<= 16;
  469. result |= word;
  470. }
  471. return result;
  472. }
  473. static bool ines_timestamp_expired(struct ines_timestamp *ts)
  474. {
  475. return time_after(jiffies, ts->tmo);
  476. }
  477. static int ines_ts_info(struct mii_timestamper *mii_ts,
  478. struct ethtool_ts_info *info)
  479. {
  480. info->so_timestamping =
  481. SOF_TIMESTAMPING_TX_HARDWARE |
  482. SOF_TIMESTAMPING_TX_SOFTWARE |
  483. SOF_TIMESTAMPING_RX_HARDWARE |
  484. SOF_TIMESTAMPING_RX_SOFTWARE |
  485. SOF_TIMESTAMPING_SOFTWARE |
  486. SOF_TIMESTAMPING_RAW_HARDWARE;
  487. info->phc_index = -1;
  488. info->tx_types =
  489. (1 << HWTSTAMP_TX_OFF) |
  490. (1 << HWTSTAMP_TX_ON) |
  491. (1 << HWTSTAMP_TX_ONESTEP_P2P);
  492. info->rx_filters =
  493. (1 << HWTSTAMP_FILTER_NONE) |
  494. (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
  495. return 0;
  496. }
  497. static u64 ines_txts64(struct ines_port *port, unsigned int words)
  498. {
  499. unsigned int i;
  500. u64 result;
  501. u16 word;
  502. word = ines_read32(port, ts_tx);
  503. result = word;
  504. words--;
  505. for (i = 0; i < words; i++) {
  506. word = ines_read32(port, ts_tx);
  507. result <<= 16;
  508. result |= word;
  509. }
  510. return result;
  511. }
  512. static bool ines_txts_onestep(struct ines_port *port, struct sk_buff *skb, int type)
  513. {
  514. unsigned long flags;
  515. u32 port_conf;
  516. spin_lock_irqsave(&port->lock, flags);
  517. port_conf = ines_read32(port, port_conf);
  518. spin_unlock_irqrestore(&port->lock, flags);
  519. if (port_conf & CM_ONE_STEP)
  520. return is_sync_pdelay_resp(skb, type);
  521. return false;
  522. }
  523. static void ines_txtstamp(struct mii_timestamper *mii_ts,
  524. struct sk_buff *skb, int type)
  525. {
  526. struct ines_port *port = container_of(mii_ts, struct ines_port, mii_ts);
  527. struct sk_buff *old_skb = NULL;
  528. unsigned long flags;
  529. if (!port->txts_enabled || ines_txts_onestep(port, skb, type)) {
  530. kfree_skb(skb);
  531. return;
  532. }
  533. spin_lock_irqsave(&port->lock, flags);
  534. if (port->tx_skb)
  535. old_skb = port->tx_skb;
  536. port->tx_skb = skb;
  537. spin_unlock_irqrestore(&port->lock, flags);
  538. kfree_skb(old_skb);
  539. schedule_delayed_work(&port->ts_work, 1);
  540. }
  541. static void ines_txtstamp_work(struct work_struct *work)
  542. {
  543. struct ines_port *port =
  544. container_of(work, struct ines_port, ts_work.work);
  545. struct skb_shared_hwtstamps ssh;
  546. struct sk_buff *skb;
  547. unsigned long flags;
  548. u64 ns;
  549. spin_lock_irqsave(&port->lock, flags);
  550. skb = port->tx_skb;
  551. port->tx_skb = NULL;
  552. spin_unlock_irqrestore(&port->lock, flags);
  553. ns = ines_find_txts(port, skb);
  554. if (!ns) {
  555. kfree_skb(skb);
  556. return;
  557. }
  558. ssh.hwtstamp = ns_to_ktime(ns);
  559. skb_complete_tx_timestamp(skb, &ssh);
  560. }
  561. static bool is_sync_pdelay_resp(struct sk_buff *skb, int type)
  562. {
  563. struct ptp_header *hdr;
  564. u8 msgtype;
  565. hdr = ptp_parse_header(skb, type);
  566. if (!hdr)
  567. return false;
  568. msgtype = ptp_get_msgtype(hdr, type);
  569. switch (msgtype) {
  570. case PTP_MSGTYPE_SYNC:
  571. case PTP_MSGTYPE_PDELAY_RESP:
  572. return true;
  573. default:
  574. return false;
  575. }
  576. }
  577. static u8 tag_to_msgtype(u8 tag)
  578. {
  579. switch (tag) {
  580. case MESSAGE_TYPE_SYNC:
  581. return PTP_MSGTYPE_SYNC;
  582. case MESSAGE_TYPE_P_DELAY_REQ:
  583. return PTP_MSGTYPE_PDELAY_REQ;
  584. case MESSAGE_TYPE_P_DELAY_RESP:
  585. return PTP_MSGTYPE_PDELAY_RESP;
  586. case MESSAGE_TYPE_DELAY_REQ:
  587. return PTP_MSGTYPE_DELAY_REQ;
  588. }
  589. return 0xf;
  590. }
  591. static struct mii_timestamper *ines_ptp_probe_channel(struct device *device,
  592. unsigned int index)
  593. {
  594. struct device_node *node = device->of_node;
  595. struct ines_port *port;
  596. if (index > INES_N_PORTS - 1) {
  597. dev_err(device, "bad port index %u\n", index);
  598. return ERR_PTR(-EINVAL);
  599. }
  600. port = ines_find_port(node, index);
  601. if (!port) {
  602. dev_err(device, "missing port index %u\n", index);
  603. return ERR_PTR(-ENODEV);
  604. }
  605. port->mii_ts.rxtstamp = ines_rxtstamp;
  606. port->mii_ts.txtstamp = ines_txtstamp;
  607. port->mii_ts.hwtstamp = ines_hwtstamp;
  608. port->mii_ts.link_state = ines_link_state;
  609. port->mii_ts.ts_info = ines_ts_info;
  610. return &port->mii_ts;
  611. }
  612. static void ines_ptp_release_channel(struct device *device,
  613. struct mii_timestamper *mii_ts)
  614. {
  615. }
  616. static struct mii_timestamping_ctrl ines_ctrl = {
  617. .probe_channel = ines_ptp_probe_channel,
  618. .release_channel = ines_ptp_release_channel,
  619. };
  620. static int ines_ptp_ctrl_probe(struct platform_device *pld)
  621. {
  622. struct ines_clock *clock;
  623. void __iomem *addr;
  624. int err = 0;
  625. addr = devm_platform_ioremap_resource(pld, 0);
  626. if (IS_ERR(addr)) {
  627. err = PTR_ERR(addr);
  628. goto out;
  629. }
  630. clock = kzalloc(sizeof(*clock), GFP_KERNEL);
  631. if (!clock) {
  632. err = -ENOMEM;
  633. goto out;
  634. }
  635. if (ines_clock_init(clock, &pld->dev, addr)) {
  636. kfree(clock);
  637. err = -ENOMEM;
  638. goto out;
  639. }
  640. err = register_mii_tstamp_controller(&pld->dev, &ines_ctrl);
  641. if (err) {
  642. kfree(clock);
  643. goto out;
  644. }
  645. mutex_lock(&ines_clocks_lock);
  646. list_add_tail(&ines_clocks, &clock->list);
  647. mutex_unlock(&ines_clocks_lock);
  648. dev_set_drvdata(&pld->dev, clock);
  649. out:
  650. return err;
  651. }
  652. static int ines_ptp_ctrl_remove(struct platform_device *pld)
  653. {
  654. struct ines_clock *clock = dev_get_drvdata(&pld->dev);
  655. unregister_mii_tstamp_controller(&pld->dev);
  656. mutex_lock(&ines_clocks_lock);
  657. list_del(&clock->list);
  658. mutex_unlock(&ines_clocks_lock);
  659. ines_clock_cleanup(clock);
  660. kfree(clock);
  661. return 0;
  662. }
  663. static const struct of_device_id ines_ptp_ctrl_of_match[] = {
  664. { .compatible = "ines,ptp-ctrl" },
  665. { }
  666. };
  667. MODULE_DEVICE_TABLE(of, ines_ptp_ctrl_of_match);
  668. static struct platform_driver ines_ptp_ctrl_driver = {
  669. .probe = ines_ptp_ctrl_probe,
  670. .remove = ines_ptp_ctrl_remove,
  671. .driver = {
  672. .name = "ines_ptp_ctrl",
  673. .of_match_table = of_match_ptr(ines_ptp_ctrl_of_match),
  674. },
  675. };
  676. module_platform_driver(ines_ptp_ctrl_driver);