ptp_pch.c 12 KB

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