ptp_pch.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * PTP 1588 clock using the EG20T PCH
  4. *
  5. * Copyright (C) 2010 OMICRON electronics GmbH
  6. * Copyright (C) 2011-2012 LAPIS SEMICONDUCTOR Co., LTD.
  7. *
  8. * This code was derived from the IXP46X driver.
  9. */
  10. #include <linux/device.h>
  11. #include <linux/err.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/io.h>
  14. #include <linux/io-64-nonatomic-lo-hi.h>
  15. #include <linux/io-64-nonatomic-hi-lo.h>
  16. #include <linux/irq.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/pci.h>
  20. #include <linux/ptp_clock_kernel.h>
  21. #include <linux/ptp_pch.h>
  22. #include <linux/slab.h>
  23. #define STATION_ADDR_LEN 20
  24. #define PCI_DEVICE_ID_PCH_1588 0x8819
  25. #define IO_MEM_BAR 1
  26. #define DEFAULT_ADDEND 0xA0000000
  27. #define TICKS_NS_SHIFT 5
  28. #define N_EXT_TS 2
  29. enum pch_status {
  30. PCH_SUCCESS,
  31. PCH_INVALIDPARAM,
  32. PCH_NOTIMESTAMP,
  33. PCH_INTERRUPTMODEINUSE,
  34. PCH_FAILED,
  35. PCH_UNSUPPORTED,
  36. };
  37. /*
  38. * struct pch_ts_regs - IEEE 1588 registers
  39. */
  40. struct pch_ts_regs {
  41. u32 control;
  42. u32 event;
  43. u32 addend;
  44. u32 accum;
  45. u32 test;
  46. u32 ts_compare;
  47. u32 rsystime_lo;
  48. u32 rsystime_hi;
  49. u32 systime_lo;
  50. u32 systime_hi;
  51. u32 trgt_lo;
  52. u32 trgt_hi;
  53. u32 asms_lo;
  54. u32 asms_hi;
  55. u32 amms_lo;
  56. u32 amms_hi;
  57. u32 ch_control;
  58. u32 ch_event;
  59. u32 tx_snap_lo;
  60. u32 tx_snap_hi;
  61. u32 rx_snap_lo;
  62. u32 rx_snap_hi;
  63. u32 src_uuid_lo;
  64. u32 src_uuid_hi;
  65. u32 can_status;
  66. u32 can_snap_lo;
  67. u32 can_snap_hi;
  68. u32 ts_sel;
  69. u32 ts_st[6];
  70. u32 reserve1[14];
  71. u32 stl_max_set_en;
  72. u32 stl_max_set;
  73. u32 reserve2[13];
  74. u32 srst;
  75. };
  76. #define PCH_TSC_RESET (1 << 0)
  77. #define PCH_TSC_TTM_MASK (1 << 1)
  78. #define PCH_TSC_ASMS_MASK (1 << 2)
  79. #define PCH_TSC_AMMS_MASK (1 << 3)
  80. #define PCH_TSC_PPSM_MASK (1 << 4)
  81. #define PCH_TSE_TTIPEND (1 << 1)
  82. #define PCH_TSE_SNS (1 << 2)
  83. #define PCH_TSE_SNM (1 << 3)
  84. #define PCH_TSE_PPS (1 << 4)
  85. #define PCH_CC_MM (1 << 0)
  86. #define PCH_CC_TA (1 << 1)
  87. #define PCH_CC_MODE_SHIFT 16
  88. #define PCH_CC_MODE_MASK 0x001F0000
  89. #define PCH_CC_VERSION (1 << 31)
  90. #define PCH_CE_TXS (1 << 0)
  91. #define PCH_CE_RXS (1 << 1)
  92. #define PCH_CE_OVR (1 << 0)
  93. #define PCH_CE_VAL (1 << 1)
  94. #define PCH_ECS_ETH (1 << 0)
  95. #define PCH_ECS_CAN (1 << 1)
  96. #define PCH_IEEE1588_ETH (1 << 0)
  97. #define PCH_IEEE1588_CAN (1 << 1)
  98. /*
  99. * struct pch_dev - Driver private data
  100. */
  101. struct pch_dev {
  102. struct pch_ts_regs __iomem *regs;
  103. struct ptp_clock *ptp_clock;
  104. struct ptp_clock_info caps;
  105. int exts0_enabled;
  106. int exts1_enabled;
  107. u32 irq;
  108. struct pci_dev *pdev;
  109. spinlock_t register_lock;
  110. };
  111. /*
  112. * struct pch_params - 1588 module parameter
  113. */
  114. struct pch_params {
  115. u8 station[STATION_ADDR_LEN];
  116. };
  117. /* structure to hold the module parameters */
  118. static struct pch_params pch_param = {
  119. "00:00:00:00:00:00"
  120. };
  121. /*
  122. * Register access functions
  123. */
  124. static inline void pch_eth_enable_set(struct pch_dev *chip)
  125. {
  126. u32 val;
  127. /* SET the eth_enable bit */
  128. val = ioread32(&chip->regs->ts_sel) | (PCH_ECS_ETH);
  129. iowrite32(val, (&chip->regs->ts_sel));
  130. }
  131. static u64 pch_systime_read(struct pch_ts_regs __iomem *regs)
  132. {
  133. u64 ns;
  134. ns = ioread64_lo_hi(&regs->systime_lo);
  135. return ns << TICKS_NS_SHIFT;
  136. }
  137. static void pch_systime_write(struct pch_ts_regs __iomem *regs, u64 ns)
  138. {
  139. iowrite64_lo_hi(ns >> TICKS_NS_SHIFT, &regs->systime_lo);
  140. }
  141. static inline void pch_block_reset(struct pch_dev *chip)
  142. {
  143. u32 val;
  144. /* Reset Hardware Assist block */
  145. val = ioread32(&chip->regs->control) | PCH_TSC_RESET;
  146. iowrite32(val, (&chip->regs->control));
  147. val = val & ~PCH_TSC_RESET;
  148. iowrite32(val, (&chip->regs->control));
  149. }
  150. void pch_ch_control_write(struct pci_dev *pdev, u32 val)
  151. {
  152. struct pch_dev *chip = pci_get_drvdata(pdev);
  153. iowrite32(val, (&chip->regs->ch_control));
  154. }
  155. EXPORT_SYMBOL(pch_ch_control_write);
  156. u32 pch_ch_event_read(struct pci_dev *pdev)
  157. {
  158. struct pch_dev *chip = pci_get_drvdata(pdev);
  159. u32 val;
  160. val = ioread32(&chip->regs->ch_event);
  161. return val;
  162. }
  163. EXPORT_SYMBOL(pch_ch_event_read);
  164. void pch_ch_event_write(struct pci_dev *pdev, u32 val)
  165. {
  166. struct pch_dev *chip = pci_get_drvdata(pdev);
  167. iowrite32(val, (&chip->regs->ch_event));
  168. }
  169. EXPORT_SYMBOL(pch_ch_event_write);
  170. u32 pch_src_uuid_lo_read(struct pci_dev *pdev)
  171. {
  172. struct pch_dev *chip = pci_get_drvdata(pdev);
  173. u32 val;
  174. val = ioread32(&chip->regs->src_uuid_lo);
  175. return val;
  176. }
  177. EXPORT_SYMBOL(pch_src_uuid_lo_read);
  178. u32 pch_src_uuid_hi_read(struct pci_dev *pdev)
  179. {
  180. struct pch_dev *chip = pci_get_drvdata(pdev);
  181. u32 val;
  182. val = ioread32(&chip->regs->src_uuid_hi);
  183. return val;
  184. }
  185. EXPORT_SYMBOL(pch_src_uuid_hi_read);
  186. u64 pch_rx_snap_read(struct pci_dev *pdev)
  187. {
  188. struct pch_dev *chip = pci_get_drvdata(pdev);
  189. u64 ns;
  190. ns = ioread64_lo_hi(&chip->regs->rx_snap_lo);
  191. return ns << TICKS_NS_SHIFT;
  192. }
  193. EXPORT_SYMBOL(pch_rx_snap_read);
  194. u64 pch_tx_snap_read(struct pci_dev *pdev)
  195. {
  196. struct pch_dev *chip = pci_get_drvdata(pdev);
  197. u64 ns;
  198. ns = ioread64_lo_hi(&chip->regs->tx_snap_lo);
  199. return ns << TICKS_NS_SHIFT;
  200. }
  201. EXPORT_SYMBOL(pch_tx_snap_read);
  202. /* This function enables all 64 bits in system time registers [high & low].
  203. This is a work-around for non continuous value in the SystemTime Register*/
  204. static void pch_set_system_time_count(struct pch_dev *chip)
  205. {
  206. iowrite32(0x01, &chip->regs->stl_max_set_en);
  207. iowrite32(0xFFFFFFFF, &chip->regs->stl_max_set);
  208. iowrite32(0x00, &chip->regs->stl_max_set_en);
  209. }
  210. static void pch_reset(struct pch_dev *chip)
  211. {
  212. /* Reset Hardware Assist */
  213. pch_block_reset(chip);
  214. /* enable all 32 bits in system time registers */
  215. pch_set_system_time_count(chip);
  216. }
  217. /**
  218. * pch_set_station_address() - This API sets the station address used by
  219. * IEEE 1588 hardware when looking at PTP
  220. * traffic on the ethernet interface
  221. * @addr: dress which contain the column separated address to be used.
  222. * @pdev: PCI device.
  223. */
  224. int pch_set_station_address(u8 *addr, struct pci_dev *pdev)
  225. {
  226. struct pch_dev *chip = pci_get_drvdata(pdev);
  227. bool valid;
  228. u64 mac;
  229. /* Verify the parameter */
  230. if ((chip->regs == NULL) || addr == (u8 *)NULL) {
  231. dev_err(&pdev->dev,
  232. "invalid params returning PCH_INVALIDPARAM\n");
  233. return PCH_INVALIDPARAM;
  234. }
  235. valid = mac_pton(addr, (u8 *)&mac);
  236. if (!valid) {
  237. dev_err(&pdev->dev, "invalid params returning PCH_INVALIDPARAM\n");
  238. return PCH_INVALIDPARAM;
  239. }
  240. dev_dbg(&pdev->dev, "invoking pch_station_set\n");
  241. iowrite64_lo_hi(mac, &chip->regs->ts_st);
  242. return 0;
  243. }
  244. EXPORT_SYMBOL(pch_set_station_address);
  245. /*
  246. * Interrupt service routine
  247. */
  248. static irqreturn_t isr(int irq, void *priv)
  249. {
  250. struct pch_dev *pch_dev = priv;
  251. struct pch_ts_regs __iomem *regs = pch_dev->regs;
  252. struct ptp_clock_event event;
  253. u32 ack = 0, val;
  254. val = ioread32(&regs->event);
  255. if (val & PCH_TSE_SNS) {
  256. ack |= PCH_TSE_SNS;
  257. if (pch_dev->exts0_enabled) {
  258. event.type = PTP_CLOCK_EXTTS;
  259. event.index = 0;
  260. event.timestamp = ioread64_hi_lo(&regs->asms_hi);
  261. event.timestamp <<= TICKS_NS_SHIFT;
  262. ptp_clock_event(pch_dev->ptp_clock, &event);
  263. }
  264. }
  265. if (val & PCH_TSE_SNM) {
  266. ack |= PCH_TSE_SNM;
  267. if (pch_dev->exts1_enabled) {
  268. event.type = PTP_CLOCK_EXTTS;
  269. event.index = 1;
  270. event.timestamp = ioread64_hi_lo(&regs->asms_hi);
  271. event.timestamp <<= TICKS_NS_SHIFT;
  272. ptp_clock_event(pch_dev->ptp_clock, &event);
  273. }
  274. }
  275. if (val & PCH_TSE_TTIPEND)
  276. ack |= PCH_TSE_TTIPEND; /* this bit seems to be always set */
  277. if (ack) {
  278. iowrite32(ack, &regs->event);
  279. return IRQ_HANDLED;
  280. } else
  281. return IRQ_NONE;
  282. }
  283. /*
  284. * PTP clock operations
  285. */
  286. static int ptp_pch_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
  287. {
  288. u64 adj;
  289. u32 diff, addend;
  290. int neg_adj = 0;
  291. struct pch_dev *pch_dev = container_of(ptp, struct pch_dev, caps);
  292. struct pch_ts_regs __iomem *regs = pch_dev->regs;
  293. if (ppb < 0) {
  294. neg_adj = 1;
  295. ppb = -ppb;
  296. }
  297. addend = DEFAULT_ADDEND;
  298. adj = addend;
  299. adj *= ppb;
  300. diff = div_u64(adj, 1000000000ULL);
  301. addend = neg_adj ? addend - diff : addend + diff;
  302. iowrite32(addend, &regs->addend);
  303. return 0;
  304. }
  305. static int ptp_pch_adjtime(struct ptp_clock_info *ptp, s64 delta)
  306. {
  307. s64 now;
  308. unsigned long flags;
  309. struct pch_dev *pch_dev = container_of(ptp, struct pch_dev, caps);
  310. struct pch_ts_regs __iomem *regs = pch_dev->regs;
  311. spin_lock_irqsave(&pch_dev->register_lock, flags);
  312. now = pch_systime_read(regs);
  313. now += delta;
  314. pch_systime_write(regs, now);
  315. spin_unlock_irqrestore(&pch_dev->register_lock, flags);
  316. return 0;
  317. }
  318. static int ptp_pch_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
  319. {
  320. u64 ns;
  321. unsigned long flags;
  322. struct pch_dev *pch_dev = container_of(ptp, struct pch_dev, caps);
  323. struct pch_ts_regs __iomem *regs = pch_dev->regs;
  324. spin_lock_irqsave(&pch_dev->register_lock, flags);
  325. ns = pch_systime_read(regs);
  326. spin_unlock_irqrestore(&pch_dev->register_lock, flags);
  327. *ts = ns_to_timespec64(ns);
  328. return 0;
  329. }
  330. static int ptp_pch_settime(struct ptp_clock_info *ptp,
  331. const struct timespec64 *ts)
  332. {
  333. u64 ns;
  334. unsigned long flags;
  335. struct pch_dev *pch_dev = container_of(ptp, struct pch_dev, caps);
  336. struct pch_ts_regs __iomem *regs = pch_dev->regs;
  337. ns = timespec64_to_ns(ts);
  338. spin_lock_irqsave(&pch_dev->register_lock, flags);
  339. pch_systime_write(regs, ns);
  340. spin_unlock_irqrestore(&pch_dev->register_lock, flags);
  341. return 0;
  342. }
  343. static int ptp_pch_enable(struct ptp_clock_info *ptp,
  344. struct ptp_clock_request *rq, int on)
  345. {
  346. struct pch_dev *pch_dev = container_of(ptp, struct pch_dev, caps);
  347. switch (rq->type) {
  348. case PTP_CLK_REQ_EXTTS:
  349. switch (rq->extts.index) {
  350. case 0:
  351. pch_dev->exts0_enabled = on ? 1 : 0;
  352. break;
  353. case 1:
  354. pch_dev->exts1_enabled = on ? 1 : 0;
  355. break;
  356. default:
  357. return -EINVAL;
  358. }
  359. return 0;
  360. default:
  361. break;
  362. }
  363. return -EOPNOTSUPP;
  364. }
  365. static const struct ptp_clock_info ptp_pch_caps = {
  366. .owner = THIS_MODULE,
  367. .name = "PCH timer",
  368. .max_adj = 50000000,
  369. .n_ext_ts = N_EXT_TS,
  370. .n_pins = 0,
  371. .pps = 0,
  372. .adjfreq = ptp_pch_adjfreq,
  373. .adjtime = ptp_pch_adjtime,
  374. .gettime64 = ptp_pch_gettime,
  375. .settime64 = ptp_pch_settime,
  376. .enable = ptp_pch_enable,
  377. };
  378. static void pch_remove(struct pci_dev *pdev)
  379. {
  380. struct pch_dev *chip = pci_get_drvdata(pdev);
  381. free_irq(pdev->irq, chip);
  382. ptp_clock_unregister(chip->ptp_clock);
  383. }
  384. static s32
  385. pch_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  386. {
  387. s32 ret;
  388. unsigned long flags;
  389. struct pch_dev *chip;
  390. chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
  391. if (chip == NULL)
  392. return -ENOMEM;
  393. /* enable the 1588 pci device */
  394. ret = pcim_enable_device(pdev);
  395. if (ret != 0) {
  396. dev_err(&pdev->dev, "could not enable the pci device\n");
  397. return ret;
  398. }
  399. ret = pcim_iomap_regions(pdev, BIT(IO_MEM_BAR), "1588_regs");
  400. if (ret) {
  401. dev_err(&pdev->dev, "could not locate IO memory address\n");
  402. return ret;
  403. }
  404. /* get the virtual address to the 1588 registers */
  405. chip->regs = pcim_iomap_table(pdev)[IO_MEM_BAR];
  406. chip->caps = ptp_pch_caps;
  407. chip->ptp_clock = ptp_clock_register(&chip->caps, &pdev->dev);
  408. if (IS_ERR(chip->ptp_clock))
  409. return PTR_ERR(chip->ptp_clock);
  410. spin_lock_init(&chip->register_lock);
  411. ret = request_irq(pdev->irq, &isr, IRQF_SHARED, KBUILD_MODNAME, chip);
  412. if (ret != 0) {
  413. dev_err(&pdev->dev, "failed to get irq %d\n", pdev->irq);
  414. goto err_req_irq;
  415. }
  416. /* indicate success */
  417. chip->irq = pdev->irq;
  418. chip->pdev = pdev;
  419. pci_set_drvdata(pdev, chip);
  420. spin_lock_irqsave(&chip->register_lock, flags);
  421. /* reset the ieee1588 h/w */
  422. pch_reset(chip);
  423. iowrite32(DEFAULT_ADDEND, &chip->regs->addend);
  424. iowrite64_lo_hi(1, &chip->regs->trgt_lo);
  425. iowrite32(PCH_TSE_TTIPEND, &chip->regs->event);
  426. pch_eth_enable_set(chip);
  427. if (strcmp(pch_param.station, "00:00:00:00:00:00") != 0) {
  428. if (pch_set_station_address(pch_param.station, pdev) != 0) {
  429. dev_err(&pdev->dev,
  430. "Invalid station address parameter\n"
  431. "Module loaded but station address not set correctly\n"
  432. );
  433. }
  434. }
  435. spin_unlock_irqrestore(&chip->register_lock, flags);
  436. return 0;
  437. err_req_irq:
  438. ptp_clock_unregister(chip->ptp_clock);
  439. dev_err(&pdev->dev, "probe failed(ret=0x%x)\n", ret);
  440. return ret;
  441. }
  442. static const struct pci_device_id pch_ieee1588_pcidev_id[] = {
  443. {
  444. .vendor = PCI_VENDOR_ID_INTEL,
  445. .device = PCI_DEVICE_ID_PCH_1588
  446. },
  447. {0}
  448. };
  449. MODULE_DEVICE_TABLE(pci, pch_ieee1588_pcidev_id);
  450. static struct pci_driver pch_driver = {
  451. .name = KBUILD_MODNAME,
  452. .id_table = pch_ieee1588_pcidev_id,
  453. .probe = pch_probe,
  454. .remove = pch_remove,
  455. };
  456. module_pci_driver(pch_driver);
  457. module_param_string(station,
  458. pch_param.station, sizeof(pch_param.station), 0444);
  459. MODULE_PARM_DESC(station,
  460. "IEEE 1588 station address to use - colon separated hex values");
  461. MODULE_AUTHOR("LAPIS SEMICONDUCTOR, <[email protected]>");
  462. MODULE_DESCRIPTION("PTP clock using the EG20T timer");
  463. MODULE_LICENSE("GPL");