10a006a2fSYangbo Lu // SPDX-License-Identifier: GPL-2.0
20a006a2fSYangbo Lu /*
30a006a2fSYangbo Lu * Copyright 2013-2016 Freescale Semiconductor Inc.
40a006a2fSYangbo Lu * Copyright 2016-2018 NXP
5d21c784cSYangbo Lu * Copyright 2020 NXP
60a006a2fSYangbo Lu */
70a006a2fSYangbo Lu
80a006a2fSYangbo Lu #include <linux/module.h>
9d346c9e8SYangbo Lu #include <linux/of.h>
10d346c9e8SYangbo Lu #include <linux/of_address.h>
110a006a2fSYangbo Lu #include <linux/fsl/mc.h>
120a006a2fSYangbo Lu
130a006a2fSYangbo Lu #include "dpaa2-ptp.h"
140a006a2fSYangbo Lu
dpaa2_ptp_enable(struct ptp_clock_info * ptp,struct ptp_clock_request * rq,int on)158893a843SYangbo Lu static int dpaa2_ptp_enable(struct ptp_clock_info *ptp,
168893a843SYangbo Lu struct ptp_clock_request *rq, int on)
178893a843SYangbo Lu {
188893a843SYangbo Lu struct ptp_qoriq *ptp_qoriq = container_of(ptp, struct ptp_qoriq, caps);
198893a843SYangbo Lu struct fsl_mc_device *mc_dev;
208893a843SYangbo Lu struct device *dev;
218893a843SYangbo Lu u32 mask = 0;
228893a843SYangbo Lu u32 bit;
238893a843SYangbo Lu int err;
248893a843SYangbo Lu
258893a843SYangbo Lu dev = ptp_qoriq->dev;
268893a843SYangbo Lu mc_dev = to_fsl_mc_device(dev);
278893a843SYangbo Lu
288893a843SYangbo Lu switch (rq->type) {
2917568c62SYangbo Lu case PTP_CLK_REQ_EXTTS:
3017568c62SYangbo Lu switch (rq->extts.index) {
3117568c62SYangbo Lu case 0:
3217568c62SYangbo Lu bit = DPRTC_EVENT_ETS1;
3317568c62SYangbo Lu break;
3417568c62SYangbo Lu case 1:
3517568c62SYangbo Lu bit = DPRTC_EVENT_ETS2;
3617568c62SYangbo Lu break;
3717568c62SYangbo Lu default:
3817568c62SYangbo Lu return -EINVAL;
3917568c62SYangbo Lu }
4017568c62SYangbo Lu if (on)
4117568c62SYangbo Lu extts_clean_up(ptp_qoriq, rq->extts.index, false);
4217568c62SYangbo Lu break;
438893a843SYangbo Lu case PTP_CLK_REQ_PPS:
448893a843SYangbo Lu bit = DPRTC_EVENT_PPS;
458893a843SYangbo Lu break;
468893a843SYangbo Lu default:
478893a843SYangbo Lu return -EOPNOTSUPP;
488893a843SYangbo Lu }
498893a843SYangbo Lu
508893a843SYangbo Lu err = dprtc_get_irq_mask(mc_dev->mc_io, 0, mc_dev->mc_handle,
518893a843SYangbo Lu DPRTC_IRQ_INDEX, &mask);
528893a843SYangbo Lu if (err < 0) {
538893a843SYangbo Lu dev_err(dev, "dprtc_get_irq_mask(): %d\n", err);
548893a843SYangbo Lu return err;
558893a843SYangbo Lu }
568893a843SYangbo Lu
578893a843SYangbo Lu if (on)
588893a843SYangbo Lu mask |= bit;
598893a843SYangbo Lu else
608893a843SYangbo Lu mask &= ~bit;
618893a843SYangbo Lu
628893a843SYangbo Lu err = dprtc_set_irq_mask(mc_dev->mc_io, 0, mc_dev->mc_handle,
638893a843SYangbo Lu DPRTC_IRQ_INDEX, mask);
648893a843SYangbo Lu if (err < 0) {
658893a843SYangbo Lu dev_err(dev, "dprtc_set_irq_mask(): %d\n", err);
668893a843SYangbo Lu return err;
678893a843SYangbo Lu }
688893a843SYangbo Lu
698893a843SYangbo Lu return 0;
708893a843SYangbo Lu }
718893a843SYangbo Lu
72d346c9e8SYangbo Lu static const struct ptp_clock_info dpaa2_ptp_caps = {
730a006a2fSYangbo Lu .owner = THIS_MODULE,
740a006a2fSYangbo Lu .name = "DPAA2 PTP Clock",
750a006a2fSYangbo Lu .max_adj = 512000,
760a006a2fSYangbo Lu .n_alarm = 2,
770a006a2fSYangbo Lu .n_ext_ts = 2,
780a006a2fSYangbo Lu .n_per_out = 3,
790a006a2fSYangbo Lu .n_pins = 0,
800a006a2fSYangbo Lu .pps = 1,
81d346c9e8SYangbo Lu .adjfine = ptp_qoriq_adjfine,
82d346c9e8SYangbo Lu .adjtime = ptp_qoriq_adjtime,
83d346c9e8SYangbo Lu .gettime64 = ptp_qoriq_gettime,
84d346c9e8SYangbo Lu .settime64 = ptp_qoriq_settime,
858893a843SYangbo Lu .enable = dpaa2_ptp_enable,
860a006a2fSYangbo Lu };
870a006a2fSYangbo Lu
dpaa2_ptp_irq_handler_thread(int irq,void * priv)888893a843SYangbo Lu static irqreturn_t dpaa2_ptp_irq_handler_thread(int irq, void *priv)
898893a843SYangbo Lu {
908893a843SYangbo Lu struct ptp_qoriq *ptp_qoriq = priv;
918893a843SYangbo Lu struct ptp_clock_event event;
928893a843SYangbo Lu struct fsl_mc_device *mc_dev;
938893a843SYangbo Lu struct device *dev;
948893a843SYangbo Lu u32 status = 0;
958893a843SYangbo Lu int err;
968893a843SYangbo Lu
978893a843SYangbo Lu dev = ptp_qoriq->dev;
988893a843SYangbo Lu mc_dev = to_fsl_mc_device(dev);
998893a843SYangbo Lu
1008893a843SYangbo Lu err = dprtc_get_irq_status(mc_dev->mc_io, 0, mc_dev->mc_handle,
1018893a843SYangbo Lu DPRTC_IRQ_INDEX, &status);
1028893a843SYangbo Lu if (unlikely(err)) {
1038893a843SYangbo Lu dev_err(dev, "dprtc_get_irq_status err %d\n", err);
1048893a843SYangbo Lu return IRQ_NONE;
1058893a843SYangbo Lu }
1068893a843SYangbo Lu
1078893a843SYangbo Lu if (status & DPRTC_EVENT_PPS) {
1088893a843SYangbo Lu event.type = PTP_CLOCK_PPS;
1098893a843SYangbo Lu ptp_clock_event(ptp_qoriq->clock, &event);
1108893a843SYangbo Lu }
1118893a843SYangbo Lu
11217568c62SYangbo Lu if (status & DPRTC_EVENT_ETS1)
11317568c62SYangbo Lu extts_clean_up(ptp_qoriq, 0, true);
11417568c62SYangbo Lu
11517568c62SYangbo Lu if (status & DPRTC_EVENT_ETS2)
11617568c62SYangbo Lu extts_clean_up(ptp_qoriq, 1, true);
11717568c62SYangbo Lu
1188893a843SYangbo Lu err = dprtc_clear_irq_status(mc_dev->mc_io, 0, mc_dev->mc_handle,
1198893a843SYangbo Lu DPRTC_IRQ_INDEX, status);
1208893a843SYangbo Lu if (unlikely(err)) {
1218893a843SYangbo Lu dev_err(dev, "dprtc_clear_irq_status err %d\n", err);
1228893a843SYangbo Lu return IRQ_NONE;
1238893a843SYangbo Lu }
1248893a843SYangbo Lu
1258893a843SYangbo Lu return IRQ_HANDLED;
1268893a843SYangbo Lu }
1278893a843SYangbo Lu
dpaa2_ptp_probe(struct fsl_mc_device * mc_dev)128180f539dSYangbo Lu static int dpaa2_ptp_probe(struct fsl_mc_device *mc_dev)
1290a006a2fSYangbo Lu {
1300a006a2fSYangbo Lu struct device *dev = &mc_dev->dev;
131d346c9e8SYangbo Lu struct ptp_qoriq *ptp_qoriq;
132d346c9e8SYangbo Lu struct device_node *node;
133d346c9e8SYangbo Lu void __iomem *base;
1340a006a2fSYangbo Lu int err;
1350a006a2fSYangbo Lu
136d346c9e8SYangbo Lu ptp_qoriq = devm_kzalloc(dev, sizeof(*ptp_qoriq), GFP_KERNEL);
137d346c9e8SYangbo Lu if (!ptp_qoriq)
1380a006a2fSYangbo Lu return -ENOMEM;
1390a006a2fSYangbo Lu
1400a006a2fSYangbo Lu err = fsl_mc_portal_allocate(mc_dev, 0, &mc_dev->mc_io);
1410a006a2fSYangbo Lu if (err) {
1425500598aSIoana Ciornei if (err == -ENXIO)
1435500598aSIoana Ciornei err = -EPROBE_DEFER;
1445500598aSIoana Ciornei else
1450a006a2fSYangbo Lu dev_err(dev, "fsl_mc_portal_allocate err %d\n", err);
1460a006a2fSYangbo Lu goto err_exit;
1470a006a2fSYangbo Lu }
1480a006a2fSYangbo Lu
1490a006a2fSYangbo Lu err = dprtc_open(mc_dev->mc_io, 0, mc_dev->obj_desc.id,
1500a006a2fSYangbo Lu &mc_dev->mc_handle);
1510a006a2fSYangbo Lu if (err) {
1520a006a2fSYangbo Lu dev_err(dev, "dprtc_open err %d\n", err);
1530a006a2fSYangbo Lu goto err_free_mcp;
1540a006a2fSYangbo Lu }
1550a006a2fSYangbo Lu
156d346c9e8SYangbo Lu ptp_qoriq->dev = dev;
1570a006a2fSYangbo Lu
158d346c9e8SYangbo Lu node = of_find_compatible_node(NULL, NULL, "fsl,dpaa2-ptp");
159d346c9e8SYangbo Lu if (!node) {
160d346c9e8SYangbo Lu err = -ENODEV;
1610a006a2fSYangbo Lu goto err_close;
1620a006a2fSYangbo Lu }
1630a006a2fSYangbo Lu
164d346c9e8SYangbo Lu dev->of_node = node;
1650a006a2fSYangbo Lu
166d346c9e8SYangbo Lu base = of_iomap(node, 0);
167d346c9e8SYangbo Lu if (!base) {
168d346c9e8SYangbo Lu err = -ENOMEM;
1692b04bd4fSMiaoqian Lin goto err_put;
1700a006a2fSYangbo Lu }
1710a006a2fSYangbo Lu
1728893a843SYangbo Lu err = fsl_mc_allocate_irqs(mc_dev);
1738893a843SYangbo Lu if (err) {
1748893a843SYangbo Lu dev_err(dev, "MC irqs allocation failed\n");
1758893a843SYangbo Lu goto err_unmap;
1768893a843SYangbo Lu }
1778893a843SYangbo Lu
178d86a6d47SThomas Gleixner ptp_qoriq->irq = mc_dev->irqs[0]->virq;
1798893a843SYangbo Lu
180daa6eb5aSIoana Ciornei err = request_threaded_irq(ptp_qoriq->irq, NULL,
1818893a843SYangbo Lu dpaa2_ptp_irq_handler_thread,
1828893a843SYangbo Lu IRQF_NO_SUSPEND | IRQF_ONESHOT,
1838893a843SYangbo Lu dev_name(dev), ptp_qoriq);
1848893a843SYangbo Lu if (err < 0) {
1858893a843SYangbo Lu dev_err(dev, "devm_request_threaded_irq(): %d\n", err);
1868893a843SYangbo Lu goto err_free_mc_irq;
1878893a843SYangbo Lu }
1888893a843SYangbo Lu
1898893a843SYangbo Lu err = dprtc_set_irq_enable(mc_dev->mc_io, 0, mc_dev->mc_handle,
1908893a843SYangbo Lu DPRTC_IRQ_INDEX, 1);
1918893a843SYangbo Lu if (err < 0) {
1928893a843SYangbo Lu dev_err(dev, "dprtc_set_irq_enable(): %d\n", err);
193daa6eb5aSIoana Ciornei goto err_free_threaded_irq;
1948893a843SYangbo Lu }
1958893a843SYangbo Lu
196d346c9e8SYangbo Lu err = ptp_qoriq_init(ptp_qoriq, base, &dpaa2_ptp_caps);
197d346c9e8SYangbo Lu if (err)
198daa6eb5aSIoana Ciornei goto err_free_threaded_irq;
1990a006a2fSYangbo Lu
200d346c9e8SYangbo Lu dpaa2_phc_index = ptp_qoriq->phc_index;
201d21c784cSYangbo Lu dpaa2_ptp = ptp_qoriq;
202d346c9e8SYangbo Lu dev_set_drvdata(dev, ptp_qoriq);
2030a006a2fSYangbo Lu
2040a006a2fSYangbo Lu return 0;
2050a006a2fSYangbo Lu
206daa6eb5aSIoana Ciornei err_free_threaded_irq:
207daa6eb5aSIoana Ciornei free_irq(ptp_qoriq->irq, ptp_qoriq);
2088893a843SYangbo Lu err_free_mc_irq:
2098893a843SYangbo Lu fsl_mc_free_irqs(mc_dev);
210d346c9e8SYangbo Lu err_unmap:
211d346c9e8SYangbo Lu iounmap(base);
2122b04bd4fSMiaoqian Lin err_put:
2132b04bd4fSMiaoqian Lin of_node_put(node);
2140a006a2fSYangbo Lu err_close:
2150a006a2fSYangbo Lu dprtc_close(mc_dev->mc_io, 0, mc_dev->mc_handle);
2160a006a2fSYangbo Lu err_free_mcp:
2170a006a2fSYangbo Lu fsl_mc_portal_free(mc_dev->mc_io);
2180a006a2fSYangbo Lu err_exit:
2190a006a2fSYangbo Lu return err;
2200a006a2fSYangbo Lu }
2210a006a2fSYangbo Lu
dpaa2_ptp_remove(struct fsl_mc_device * mc_dev)222*59272ad8SUwe Kleine-König static void dpaa2_ptp_remove(struct fsl_mc_device *mc_dev)
2230a006a2fSYangbo Lu {
2240a006a2fSYangbo Lu struct device *dev = &mc_dev->dev;
225d346c9e8SYangbo Lu struct ptp_qoriq *ptp_qoriq;
2260a006a2fSYangbo Lu
227d346c9e8SYangbo Lu ptp_qoriq = dev_get_drvdata(dev);
228d346c9e8SYangbo Lu
229d346c9e8SYangbo Lu dpaa2_phc_index = -1;
230d346c9e8SYangbo Lu ptp_qoriq_free(ptp_qoriq);
2310a006a2fSYangbo Lu
2328893a843SYangbo Lu fsl_mc_free_irqs(mc_dev);
2330a006a2fSYangbo Lu dprtc_close(mc_dev->mc_io, 0, mc_dev->mc_handle);
2340a006a2fSYangbo Lu fsl_mc_portal_free(mc_dev->mc_io);
2350a006a2fSYangbo Lu }
2360a006a2fSYangbo Lu
237180f539dSYangbo Lu static const struct fsl_mc_device_id dpaa2_ptp_match_id_table[] = {
2380a006a2fSYangbo Lu {
2390a006a2fSYangbo Lu .vendor = FSL_MC_VENDOR_FREESCALE,
2400a006a2fSYangbo Lu .obj_type = "dprtc",
2410a006a2fSYangbo Lu },
2420a006a2fSYangbo Lu {}
2430a006a2fSYangbo Lu };
244180f539dSYangbo Lu MODULE_DEVICE_TABLE(fslmc, dpaa2_ptp_match_id_table);
2450a006a2fSYangbo Lu
246180f539dSYangbo Lu static struct fsl_mc_driver dpaa2_ptp_drv = {
2470a006a2fSYangbo Lu .driver = {
2480a006a2fSYangbo Lu .name = KBUILD_MODNAME,
2490a006a2fSYangbo Lu .owner = THIS_MODULE,
2500a006a2fSYangbo Lu },
251180f539dSYangbo Lu .probe = dpaa2_ptp_probe,
252180f539dSYangbo Lu .remove = dpaa2_ptp_remove,
253180f539dSYangbo Lu .match_id_table = dpaa2_ptp_match_id_table,
2540a006a2fSYangbo Lu };
2550a006a2fSYangbo Lu
256180f539dSYangbo Lu module_fsl_mc_driver(dpaa2_ptp_drv);
2570a006a2fSYangbo Lu
2580a006a2fSYangbo Lu MODULE_LICENSE("GPL v2");
2590a006a2fSYangbo Lu MODULE_DESCRIPTION("DPAA2 PTP Clock Driver");
260