xref: /openbmc/linux/drivers/ptp/ptp_ocp.c (revision ef0cfb3460a40942c901e15e3df9822b63cb159e)
1a7e1abadSJonathan Lemon // SPDX-License-Identifier: GPL-2.0-only
2a7e1abadSJonathan Lemon /* Copyright (c) 2020 Facebook */
3a7e1abadSJonathan Lemon 
4a7e1abadSJonathan Lemon #include <linux/err.h>
5a7e1abadSJonathan Lemon #include <linux/kernel.h>
6a7e1abadSJonathan Lemon #include <linux/module.h>
7a7e1abadSJonathan Lemon #include <linux/init.h>
8a7e1abadSJonathan Lemon #include <linux/pci.h>
9773bda96SJonathan Lemon #include <linux/serial_8250.h>
10773bda96SJonathan Lemon #include <linux/clkdev.h>
11773bda96SJonathan Lemon #include <linux/clk-provider.h>
12773bda96SJonathan Lemon #include <linux/platform_device.h>
13a7e1abadSJonathan Lemon #include <linux/ptp_clock_kernel.h>
14773bda96SJonathan Lemon #include <linux/spi/spi.h>
15773bda96SJonathan Lemon #include <linux/spi/xilinx_spi.h>
16773bda96SJonathan Lemon #include <net/devlink.h>
17773bda96SJonathan Lemon #include <linux/i2c.h>
18773bda96SJonathan Lemon #include <linux/mtd/mtd.h>
19a7e1abadSJonathan Lemon 
20773bda96SJonathan Lemon #ifndef PCI_VENDOR_ID_FACEBOOK
21773bda96SJonathan Lemon #define PCI_VENDOR_ID_FACEBOOK 0x1d9b
22773bda96SJonathan Lemon #endif
23773bda96SJonathan Lemon 
24773bda96SJonathan Lemon #ifndef PCI_DEVICE_ID_FACEBOOK_TIMECARD
25773bda96SJonathan Lemon #define PCI_DEVICE_ID_FACEBOOK_TIMECARD 0x0400
26773bda96SJonathan Lemon #endif
27773bda96SJonathan Lemon 
28773bda96SJonathan Lemon static struct class timecard_class = {
29773bda96SJonathan Lemon 	.owner		= THIS_MODULE,
30773bda96SJonathan Lemon 	.name		= "timecard",
31a7e1abadSJonathan Lemon };
32a7e1abadSJonathan Lemon 
33a7e1abadSJonathan Lemon struct ocp_reg {
34a7e1abadSJonathan Lemon 	u32	ctrl;
35a7e1abadSJonathan Lemon 	u32	status;
36a7e1abadSJonathan Lemon 	u32	select;
37a7e1abadSJonathan Lemon 	u32	version;
38a7e1abadSJonathan Lemon 	u32	time_ns;
39a7e1abadSJonathan Lemon 	u32	time_sec;
40a7e1abadSJonathan Lemon 	u32	__pad0[2];
41a7e1abadSJonathan Lemon 	u32	adjust_ns;
42a7e1abadSJonathan Lemon 	u32	adjust_sec;
43a7e1abadSJonathan Lemon 	u32	__pad1[2];
44a7e1abadSJonathan Lemon 	u32	offset_ns;
45a7e1abadSJonathan Lemon 	u32	offset_window_ns;
46773bda96SJonathan Lemon 	u32	__pad2[2];
47773bda96SJonathan Lemon 	u32	drift_ns;
48773bda96SJonathan Lemon 	u32	drift_window_ns;
49773bda96SJonathan Lemon 	u32	__pad3[6];
50773bda96SJonathan Lemon 	u32	servo_offset_p;
51773bda96SJonathan Lemon 	u32	servo_offset_i;
52773bda96SJonathan Lemon 	u32	servo_drift_p;
53773bda96SJonathan Lemon 	u32	servo_drift_i;
54a7e1abadSJonathan Lemon };
55a7e1abadSJonathan Lemon 
56a7e1abadSJonathan Lemon #define OCP_CTRL_ENABLE		BIT(0)
57a7e1abadSJonathan Lemon #define OCP_CTRL_ADJUST_TIME	BIT(1)
58a7e1abadSJonathan Lemon #define OCP_CTRL_ADJUST_OFFSET	BIT(2)
59773bda96SJonathan Lemon #define OCP_CTRL_ADJUST_DRIFT	BIT(3)
60773bda96SJonathan Lemon #define OCP_CTRL_ADJUST_SERVO	BIT(8)
61a7e1abadSJonathan Lemon #define OCP_CTRL_READ_TIME_REQ	BIT(30)
62a7e1abadSJonathan Lemon #define OCP_CTRL_READ_TIME_DONE	BIT(31)
63a7e1abadSJonathan Lemon 
64a7e1abadSJonathan Lemon #define OCP_STATUS_IN_SYNC	BIT(0)
65773bda96SJonathan Lemon #define OCP_STATUS_IN_HOLDOVER	BIT(1)
66a7e1abadSJonathan Lemon 
67a7e1abadSJonathan Lemon #define OCP_SELECT_CLK_NONE	0
68773bda96SJonathan Lemon #define OCP_SELECT_CLK_REG	0xfe
69a7e1abadSJonathan Lemon 
70a7e1abadSJonathan Lemon struct tod_reg {
71a7e1abadSJonathan Lemon 	u32	ctrl;
72a7e1abadSJonathan Lemon 	u32	status;
73a7e1abadSJonathan Lemon 	u32	uart_polarity;
74a7e1abadSJonathan Lemon 	u32	version;
75a7e1abadSJonathan Lemon 	u32	correction_sec;
76a7e1abadSJonathan Lemon 	u32	__pad0[3];
77a7e1abadSJonathan Lemon 	u32	uart_baud;
78a7e1abadSJonathan Lemon 	u32	__pad1[3];
79a7e1abadSJonathan Lemon 	u32	utc_status;
80a7e1abadSJonathan Lemon 	u32	leap;
81a7e1abadSJonathan Lemon };
82a7e1abadSJonathan Lemon 
83a7e1abadSJonathan Lemon #define TOD_CTRL_PROTOCOL	BIT(28)
84a7e1abadSJonathan Lemon #define TOD_CTRL_DISABLE_FMT_A	BIT(17)
85a7e1abadSJonathan Lemon #define TOD_CTRL_DISABLE_FMT_B	BIT(16)
86a7e1abadSJonathan Lemon #define TOD_CTRL_ENABLE		BIT(0)
87a7e1abadSJonathan Lemon #define TOD_CTRL_GNSS_MASK	((1U << 4) - 1)
88a7e1abadSJonathan Lemon #define TOD_CTRL_GNSS_SHIFT	24
89a7e1abadSJonathan Lemon 
90a7e1abadSJonathan Lemon #define TOD_STATUS_UTC_MASK	0xff
91a7e1abadSJonathan Lemon #define TOD_STATUS_UTC_VALID	BIT(8)
92a7e1abadSJonathan Lemon #define TOD_STATUS_LEAP_VALID	BIT(16)
93a7e1abadSJonathan Lemon 
94773bda96SJonathan Lemon struct ts_reg {
95773bda96SJonathan Lemon 	u32	enable;
96773bda96SJonathan Lemon 	u32	error;
97773bda96SJonathan Lemon 	u32	polarity;
98773bda96SJonathan Lemon 	u32	version;
99773bda96SJonathan Lemon 	u32	__pad0[4];
100773bda96SJonathan Lemon 	u32	cable_delay;
101773bda96SJonathan Lemon 	u32	__pad1[3];
102773bda96SJonathan Lemon 	u32	intr;
103773bda96SJonathan Lemon 	u32	intr_mask;
104773bda96SJonathan Lemon 	u32	event_count;
105773bda96SJonathan Lemon 	u32	__pad2[1];
106773bda96SJonathan Lemon 	u32	ts_count;
107773bda96SJonathan Lemon 	u32	time_ns;
108773bda96SJonathan Lemon 	u32	time_sec;
109773bda96SJonathan Lemon 	u32	data_width;
110773bda96SJonathan Lemon 	u32	data;
111773bda96SJonathan Lemon };
112773bda96SJonathan Lemon 
113773bda96SJonathan Lemon struct pps_reg {
114773bda96SJonathan Lemon 	u32	ctrl;
115773bda96SJonathan Lemon 	u32	status;
1160d43d4f2SJonathan Lemon 	u32	__pad0[6];
1170d43d4f2SJonathan Lemon 	u32	cable_delay;
118773bda96SJonathan Lemon };
119773bda96SJonathan Lemon 
120773bda96SJonathan Lemon #define PPS_STATUS_FILTER_ERR	BIT(0)
121773bda96SJonathan Lemon #define PPS_STATUS_SUPERV_ERR	BIT(1)
122773bda96SJonathan Lemon 
123773bda96SJonathan Lemon struct img_reg {
124773bda96SJonathan Lemon 	u32	version;
125773bda96SJonathan Lemon };
126773bda96SJonathan Lemon 
127773bda96SJonathan Lemon struct ptp_ocp_flash_info {
128773bda96SJonathan Lemon 	const char *name;
129773bda96SJonathan Lemon 	int pci_offset;
130773bda96SJonathan Lemon 	int data_size;
131773bda96SJonathan Lemon 	void *data;
132773bda96SJonathan Lemon };
133773bda96SJonathan Lemon 
134773bda96SJonathan Lemon struct ptp_ocp_ext_info {
135773bda96SJonathan Lemon 	const char *name;
136773bda96SJonathan Lemon 	int index;
137773bda96SJonathan Lemon 	irqreturn_t (*irq_fcn)(int irq, void *priv);
138773bda96SJonathan Lemon 	int (*enable)(void *priv, bool enable);
139773bda96SJonathan Lemon };
140773bda96SJonathan Lemon 
141773bda96SJonathan Lemon struct ptp_ocp_ext_src {
142773bda96SJonathan Lemon 	void __iomem		*mem;
143773bda96SJonathan Lemon 	struct ptp_ocp		*bp;
144773bda96SJonathan Lemon 	struct ptp_ocp_ext_info	*info;
145773bda96SJonathan Lemon 	int			irq_vec;
146773bda96SJonathan Lemon };
147773bda96SJonathan Lemon 
148a7e1abadSJonathan Lemon struct ptp_ocp {
149a7e1abadSJonathan Lemon 	struct pci_dev		*pdev;
150773bda96SJonathan Lemon 	struct device		dev;
151a7e1abadSJonathan Lemon 	spinlock_t		lock;
152a7e1abadSJonathan Lemon 	struct ocp_reg __iomem	*reg;
153a7e1abadSJonathan Lemon 	struct tod_reg __iomem	*tod;
1540d43d4f2SJonathan Lemon 	struct pps_reg __iomem	*pps_to_ext;
1550d43d4f2SJonathan Lemon 	struct pps_reg __iomem	*pps_to_clk;
156773bda96SJonathan Lemon 	struct ptp_ocp_ext_src	*pps;
157773bda96SJonathan Lemon 	struct ptp_ocp_ext_src	*ts0;
158773bda96SJonathan Lemon 	struct ptp_ocp_ext_src	*ts1;
159773bda96SJonathan Lemon 	struct img_reg __iomem	*image;
160a7e1abadSJonathan Lemon 	struct ptp_clock	*ptp;
161a7e1abadSJonathan Lemon 	struct ptp_clock_info	ptp_info;
162773bda96SJonathan Lemon 	struct platform_device	*i2c_ctrl;
163773bda96SJonathan Lemon 	struct platform_device	*spi_flash;
164773bda96SJonathan Lemon 	struct clk_hw		*i2c_clk;
165773bda96SJonathan Lemon 	struct timer_list	watchdog;
166*ef0cfb34SJonathan Lemon 	time64_t		gnss_lost;
167773bda96SJonathan Lemon 	int			id;
168773bda96SJonathan Lemon 	int			n_irqs;
169*ef0cfb34SJonathan Lemon 	int			gnss_port;
170773bda96SJonathan Lemon 	int			mac_port;	/* miniature atomic clock */
171773bda96SJonathan Lemon 	u8			serial[6];
172773bda96SJonathan Lemon 	int			flash_start;
173773bda96SJonathan Lemon 	bool			has_serial;
174773bda96SJonathan Lemon 	bool			pending_image;
175a7e1abadSJonathan Lemon };
176a7e1abadSJonathan Lemon 
177773bda96SJonathan Lemon struct ocp_resource {
178773bda96SJonathan Lemon 	unsigned long offset;
179773bda96SJonathan Lemon 	int size;
180773bda96SJonathan Lemon 	int irq_vec;
181773bda96SJonathan Lemon 	int (*setup)(struct ptp_ocp *bp, struct ocp_resource *r);
182773bda96SJonathan Lemon 	void *extra;
183773bda96SJonathan Lemon 	unsigned long bp_offset;
184773bda96SJonathan Lemon };
185773bda96SJonathan Lemon 
186773bda96SJonathan Lemon static int ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r);
187773bda96SJonathan Lemon static int ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r);
188773bda96SJonathan Lemon static int ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r);
189773bda96SJonathan Lemon static int ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r);
190773bda96SJonathan Lemon static int ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r);
191773bda96SJonathan Lemon static int ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r);
192773bda96SJonathan Lemon static irqreturn_t ptp_ocp_ts_irq(int irq, void *priv);
193773bda96SJonathan Lemon static int ptp_ocp_ts_enable(void *priv, bool enable);
194773bda96SJonathan Lemon 
195773bda96SJonathan Lemon #define bp_assign_entry(bp, res, val) ({				\
196773bda96SJonathan Lemon 	uintptr_t addr = (uintptr_t)(bp) + (res)->bp_offset;		\
197773bda96SJonathan Lemon 	*(typeof(val) *)addr = val;					\
198773bda96SJonathan Lemon })
199773bda96SJonathan Lemon 
200773bda96SJonathan Lemon #define OCP_RES_LOCATION(member) \
201773bda96SJonathan Lemon 	.bp_offset = offsetof(struct ptp_ocp, member)
202773bda96SJonathan Lemon 
203773bda96SJonathan Lemon #define OCP_MEM_RESOURCE(member) \
204773bda96SJonathan Lemon 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_mem
205773bda96SJonathan Lemon 
206773bda96SJonathan Lemon #define OCP_SERIAL_RESOURCE(member) \
207773bda96SJonathan Lemon 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_serial
208773bda96SJonathan Lemon 
209773bda96SJonathan Lemon #define OCP_I2C_RESOURCE(member) \
210773bda96SJonathan Lemon 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_i2c
211773bda96SJonathan Lemon 
212773bda96SJonathan Lemon #define OCP_SPI_RESOURCE(member) \
213773bda96SJonathan Lemon 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_spi
214773bda96SJonathan Lemon 
215773bda96SJonathan Lemon #define OCP_EXT_RESOURCE(member) \
216773bda96SJonathan Lemon 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_ext
217773bda96SJonathan Lemon 
218773bda96SJonathan Lemon /* This is the MSI vector mapping used.
219773bda96SJonathan Lemon  * 0: N/C
220773bda96SJonathan Lemon  * 1: TS0
221773bda96SJonathan Lemon  * 2: TS1
222773bda96SJonathan Lemon  * 3: GPS
223773bda96SJonathan Lemon  * 4: GPS2 (n/c)
224773bda96SJonathan Lemon  * 5: MAC
225773bda96SJonathan Lemon  * 6: SPI IMU (inertial measurement unit)
226773bda96SJonathan Lemon  * 7: I2C oscillator
227773bda96SJonathan Lemon  * 8: HWICAP
228773bda96SJonathan Lemon  * 9: SPI Flash
229773bda96SJonathan Lemon  */
230773bda96SJonathan Lemon 
231773bda96SJonathan Lemon static struct ocp_resource ocp_fb_resource[] = {
232773bda96SJonathan Lemon 	{
233773bda96SJonathan Lemon 		OCP_MEM_RESOURCE(reg),
234773bda96SJonathan Lemon 		.offset = 0x01000000, .size = 0x10000,
235773bda96SJonathan Lemon 	},
236773bda96SJonathan Lemon 	{
237773bda96SJonathan Lemon 		OCP_EXT_RESOURCE(ts0),
238773bda96SJonathan Lemon 		.offset = 0x01010000, .size = 0x10000, .irq_vec = 1,
239773bda96SJonathan Lemon 		.extra = &(struct ptp_ocp_ext_info) {
240773bda96SJonathan Lemon 			.name = "ts0", .index = 0,
241773bda96SJonathan Lemon 			.irq_fcn = ptp_ocp_ts_irq,
242773bda96SJonathan Lemon 			.enable = ptp_ocp_ts_enable,
243773bda96SJonathan Lemon 		},
244773bda96SJonathan Lemon 	},
245773bda96SJonathan Lemon 	{
246773bda96SJonathan Lemon 		OCP_EXT_RESOURCE(ts1),
247773bda96SJonathan Lemon 		.offset = 0x01020000, .size = 0x10000, .irq_vec = 2,
248773bda96SJonathan Lemon 		.extra = &(struct ptp_ocp_ext_info) {
249773bda96SJonathan Lemon 			.name = "ts1", .index = 1,
250773bda96SJonathan Lemon 			.irq_fcn = ptp_ocp_ts_irq,
251773bda96SJonathan Lemon 			.enable = ptp_ocp_ts_enable,
252773bda96SJonathan Lemon 		},
253773bda96SJonathan Lemon 	},
254773bda96SJonathan Lemon 	{
2550d43d4f2SJonathan Lemon 		OCP_MEM_RESOURCE(pps_to_ext),
2560d43d4f2SJonathan Lemon 		.offset = 0x01030000, .size = 0x10000,
2570d43d4f2SJonathan Lemon 	},
2580d43d4f2SJonathan Lemon 	{
2590d43d4f2SJonathan Lemon 		OCP_MEM_RESOURCE(pps_to_clk),
260773bda96SJonathan Lemon 		.offset = 0x01040000, .size = 0x10000,
261773bda96SJonathan Lemon 	},
262773bda96SJonathan Lemon 	{
263773bda96SJonathan Lemon 		OCP_MEM_RESOURCE(tod),
264773bda96SJonathan Lemon 		.offset = 0x01050000, .size = 0x10000,
265773bda96SJonathan Lemon 	},
266773bda96SJonathan Lemon 	{
267773bda96SJonathan Lemon 		OCP_MEM_RESOURCE(image),
268773bda96SJonathan Lemon 		.offset = 0x00020000, .size = 0x1000,
269773bda96SJonathan Lemon 	},
270773bda96SJonathan Lemon 	{
271773bda96SJonathan Lemon 		OCP_I2C_RESOURCE(i2c_ctrl),
272773bda96SJonathan Lemon 		.offset = 0x00150000, .size = 0x10000, .irq_vec = 7,
273773bda96SJonathan Lemon 	},
274773bda96SJonathan Lemon 	{
275*ef0cfb34SJonathan Lemon 		OCP_SERIAL_RESOURCE(gnss_port),
276773bda96SJonathan Lemon 		.offset = 0x00160000 + 0x1000, .irq_vec = 3,
277773bda96SJonathan Lemon 	},
278773bda96SJonathan Lemon 	{
279773bda96SJonathan Lemon 		OCP_SERIAL_RESOURCE(mac_port),
280773bda96SJonathan Lemon 		.offset = 0x00180000 + 0x1000, .irq_vec = 5,
281773bda96SJonathan Lemon 	},
282773bda96SJonathan Lemon 	{
283773bda96SJonathan Lemon 		OCP_SPI_RESOURCE(spi_flash),
284773bda96SJonathan Lemon 		.offset = 0x00310000, .size = 0x10000, .irq_vec = 9,
285773bda96SJonathan Lemon 		.extra = &(struct ptp_ocp_flash_info) {
286773bda96SJonathan Lemon 			.name = "xilinx_spi", .pci_offset = 0,
287773bda96SJonathan Lemon 			.data_size = sizeof(struct xspi_platform_data),
288773bda96SJonathan Lemon 			.data = &(struct xspi_platform_data) {
289773bda96SJonathan Lemon 				.num_chipselect = 1,
290773bda96SJonathan Lemon 				.bits_per_word = 8,
291773bda96SJonathan Lemon 				.num_devices = 1,
292773bda96SJonathan Lemon 				.devices = &(struct spi_board_info) {
293773bda96SJonathan Lemon 					.modalias = "spi-nor",
294773bda96SJonathan Lemon 				},
295773bda96SJonathan Lemon 			},
296773bda96SJonathan Lemon 		},
297773bda96SJonathan Lemon 	},
298773bda96SJonathan Lemon 	{
299773bda96SJonathan Lemon 		.setup = ptp_ocp_fb_board_init,
300773bda96SJonathan Lemon 	},
301773bda96SJonathan Lemon 	{ }
302773bda96SJonathan Lemon };
303773bda96SJonathan Lemon 
304773bda96SJonathan Lemon static const struct pci_device_id ptp_ocp_pcidev_id[] = {
305773bda96SJonathan Lemon 	{ PCI_DEVICE_DATA(FACEBOOK, TIMECARD, &ocp_fb_resource) },
306773bda96SJonathan Lemon 	{ 0 }
307773bda96SJonathan Lemon };
308773bda96SJonathan Lemon MODULE_DEVICE_TABLE(pci, ptp_ocp_pcidev_id);
309773bda96SJonathan Lemon 
310773bda96SJonathan Lemon static DEFINE_MUTEX(ptp_ocp_lock);
311773bda96SJonathan Lemon static DEFINE_IDR(ptp_ocp_idr);
312773bda96SJonathan Lemon 
313773bda96SJonathan Lemon static struct {
314773bda96SJonathan Lemon 	const char *name;
315773bda96SJonathan Lemon 	int value;
316773bda96SJonathan Lemon } ptp_ocp_clock[] = {
317773bda96SJonathan Lemon 	{ .name = "NONE",	.value = 0 },
318773bda96SJonathan Lemon 	{ .name = "TOD",	.value = 1 },
319773bda96SJonathan Lemon 	{ .name = "IRIG",	.value = 2 },
320773bda96SJonathan Lemon 	{ .name = "PPS",	.value = 3 },
321773bda96SJonathan Lemon 	{ .name = "PTP",	.value = 4 },
322773bda96SJonathan Lemon 	{ .name = "RTC",	.value = 5 },
323773bda96SJonathan Lemon 	{ .name = "DCF",	.value = 6 },
324773bda96SJonathan Lemon 	{ .name = "REGS",	.value = 0xfe },
325773bda96SJonathan Lemon 	{ .name = "EXT",	.value = 0xff },
326773bda96SJonathan Lemon };
327773bda96SJonathan Lemon 
328773bda96SJonathan Lemon static const char *
329773bda96SJonathan Lemon ptp_ocp_clock_name_from_val(int val)
330773bda96SJonathan Lemon {
331773bda96SJonathan Lemon 	int i;
332773bda96SJonathan Lemon 
333773bda96SJonathan Lemon 	for (i = 0; i < ARRAY_SIZE(ptp_ocp_clock); i++)
334773bda96SJonathan Lemon 		if (ptp_ocp_clock[i].value == val)
335773bda96SJonathan Lemon 			return ptp_ocp_clock[i].name;
336773bda96SJonathan Lemon 	return NULL;
337773bda96SJonathan Lemon }
338773bda96SJonathan Lemon 
339773bda96SJonathan Lemon static int
340773bda96SJonathan Lemon ptp_ocp_clock_val_from_name(const char *name)
341773bda96SJonathan Lemon {
342773bda96SJonathan Lemon 	const char *clk;
343773bda96SJonathan Lemon 	int i;
344773bda96SJonathan Lemon 
345773bda96SJonathan Lemon 	for (i = 0; i < ARRAY_SIZE(ptp_ocp_clock); i++) {
346773bda96SJonathan Lemon 		clk = ptp_ocp_clock[i].name;
347773bda96SJonathan Lemon 		if (!strncasecmp(name, clk, strlen(clk)))
348773bda96SJonathan Lemon 			return ptp_ocp_clock[i].value;
349773bda96SJonathan Lemon 	}
350773bda96SJonathan Lemon 	return -EINVAL;
351773bda96SJonathan Lemon }
352773bda96SJonathan Lemon 
353a7e1abadSJonathan Lemon static int
354a7e1abadSJonathan Lemon __ptp_ocp_gettime_locked(struct ptp_ocp *bp, struct timespec64 *ts,
355a7e1abadSJonathan Lemon 			 struct ptp_system_timestamp *sts)
356a7e1abadSJonathan Lemon {
357a7e1abadSJonathan Lemon 	u32 ctrl, time_sec, time_ns;
358a7e1abadSJonathan Lemon 	int i;
359a7e1abadSJonathan Lemon 
360a7e1abadSJonathan Lemon 	ctrl = ioread32(&bp->reg->ctrl);
361a7e1abadSJonathan Lemon 	ctrl |= OCP_CTRL_READ_TIME_REQ;
362a7e1abadSJonathan Lemon 
363a7e1abadSJonathan Lemon 	ptp_read_system_prets(sts);
364a7e1abadSJonathan Lemon 	iowrite32(ctrl, &bp->reg->ctrl);
365a7e1abadSJonathan Lemon 
366a7e1abadSJonathan Lemon 	for (i = 0; i < 100; i++) {
367a7e1abadSJonathan Lemon 		ctrl = ioread32(&bp->reg->ctrl);
368a7e1abadSJonathan Lemon 		if (ctrl & OCP_CTRL_READ_TIME_DONE)
369a7e1abadSJonathan Lemon 			break;
370a7e1abadSJonathan Lemon 	}
371a7e1abadSJonathan Lemon 	ptp_read_system_postts(sts);
372a7e1abadSJonathan Lemon 
373a7e1abadSJonathan Lemon 	time_ns = ioread32(&bp->reg->time_ns);
374a7e1abadSJonathan Lemon 	time_sec = ioread32(&bp->reg->time_sec);
375a7e1abadSJonathan Lemon 
376a7e1abadSJonathan Lemon 	ts->tv_sec = time_sec;
377a7e1abadSJonathan Lemon 	ts->tv_nsec = time_ns;
378a7e1abadSJonathan Lemon 
379a7e1abadSJonathan Lemon 	return ctrl & OCP_CTRL_READ_TIME_DONE ? 0 : -ETIMEDOUT;
380a7e1abadSJonathan Lemon }
381a7e1abadSJonathan Lemon 
382a7e1abadSJonathan Lemon static int
383a7e1abadSJonathan Lemon ptp_ocp_gettimex(struct ptp_clock_info *ptp_info, struct timespec64 *ts,
384a7e1abadSJonathan Lemon 		 struct ptp_system_timestamp *sts)
385a7e1abadSJonathan Lemon {
386a7e1abadSJonathan Lemon 	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
387a7e1abadSJonathan Lemon 	unsigned long flags;
388a7e1abadSJonathan Lemon 	int err;
389a7e1abadSJonathan Lemon 
390a7e1abadSJonathan Lemon 	spin_lock_irqsave(&bp->lock, flags);
391a7e1abadSJonathan Lemon 	err = __ptp_ocp_gettime_locked(bp, ts, sts);
392a7e1abadSJonathan Lemon 	spin_unlock_irqrestore(&bp->lock, flags);
393a7e1abadSJonathan Lemon 
394a7e1abadSJonathan Lemon 	return err;
395a7e1abadSJonathan Lemon }
396a7e1abadSJonathan Lemon 
397a7e1abadSJonathan Lemon static void
398a7e1abadSJonathan Lemon __ptp_ocp_settime_locked(struct ptp_ocp *bp, const struct timespec64 *ts)
399a7e1abadSJonathan Lemon {
400a7e1abadSJonathan Lemon 	u32 ctrl, time_sec, time_ns;
401a7e1abadSJonathan Lemon 	u32 select;
402a7e1abadSJonathan Lemon 
403a7e1abadSJonathan Lemon 	time_ns = ts->tv_nsec;
404a7e1abadSJonathan Lemon 	time_sec = ts->tv_sec;
405a7e1abadSJonathan Lemon 
406a7e1abadSJonathan Lemon 	select = ioread32(&bp->reg->select);
407a7e1abadSJonathan Lemon 	iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select);
408a7e1abadSJonathan Lemon 
409a7e1abadSJonathan Lemon 	iowrite32(time_ns, &bp->reg->adjust_ns);
410a7e1abadSJonathan Lemon 	iowrite32(time_sec, &bp->reg->adjust_sec);
411a7e1abadSJonathan Lemon 
412a7e1abadSJonathan Lemon 	ctrl = ioread32(&bp->reg->ctrl);
413a7e1abadSJonathan Lemon 	ctrl |= OCP_CTRL_ADJUST_TIME;
414a7e1abadSJonathan Lemon 	iowrite32(ctrl, &bp->reg->ctrl);
415a7e1abadSJonathan Lemon 
416a7e1abadSJonathan Lemon 	/* restore clock selection */
417a7e1abadSJonathan Lemon 	iowrite32(select >> 16, &bp->reg->select);
418a7e1abadSJonathan Lemon }
419a7e1abadSJonathan Lemon 
420a7e1abadSJonathan Lemon static int
421a7e1abadSJonathan Lemon ptp_ocp_settime(struct ptp_clock_info *ptp_info, const struct timespec64 *ts)
422a7e1abadSJonathan Lemon {
423a7e1abadSJonathan Lemon 	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
424a7e1abadSJonathan Lemon 	unsigned long flags;
425a7e1abadSJonathan Lemon 
426a7e1abadSJonathan Lemon 	if (ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC)
427a7e1abadSJonathan Lemon 		return 0;
428a7e1abadSJonathan Lemon 
429a7e1abadSJonathan Lemon 	spin_lock_irqsave(&bp->lock, flags);
430a7e1abadSJonathan Lemon 	__ptp_ocp_settime_locked(bp, ts);
431a7e1abadSJonathan Lemon 	spin_unlock_irqrestore(&bp->lock, flags);
432a7e1abadSJonathan Lemon 
433a7e1abadSJonathan Lemon 	return 0;
434a7e1abadSJonathan Lemon }
435a7e1abadSJonathan Lemon 
436a7e1abadSJonathan Lemon static int
437a7e1abadSJonathan Lemon ptp_ocp_adjtime(struct ptp_clock_info *ptp_info, s64 delta_ns)
438a7e1abadSJonathan Lemon {
439a7e1abadSJonathan Lemon 	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
440a7e1abadSJonathan Lemon 	struct timespec64 ts;
441a7e1abadSJonathan Lemon 	unsigned long flags;
442a7e1abadSJonathan Lemon 	int err;
443a7e1abadSJonathan Lemon 
444a7e1abadSJonathan Lemon 	if (ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC)
445a7e1abadSJonathan Lemon 		return 0;
446a7e1abadSJonathan Lemon 
447a7e1abadSJonathan Lemon 	spin_lock_irqsave(&bp->lock, flags);
448a7e1abadSJonathan Lemon 	err = __ptp_ocp_gettime_locked(bp, &ts, NULL);
449a7e1abadSJonathan Lemon 	if (likely(!err)) {
450a7e1abadSJonathan Lemon 		timespec64_add_ns(&ts, delta_ns);
451a7e1abadSJonathan Lemon 		__ptp_ocp_settime_locked(bp, &ts);
452a7e1abadSJonathan Lemon 	}
453a7e1abadSJonathan Lemon 	spin_unlock_irqrestore(&bp->lock, flags);
454a7e1abadSJonathan Lemon 
455a7e1abadSJonathan Lemon 	return err;
456a7e1abadSJonathan Lemon }
457a7e1abadSJonathan Lemon 
458a7e1abadSJonathan Lemon static int
459a7e1abadSJonathan Lemon ptp_ocp_null_adjfine(struct ptp_clock_info *ptp_info, long scaled_ppm)
460a7e1abadSJonathan Lemon {
461a7e1abadSJonathan Lemon 	if (scaled_ppm == 0)
462a7e1abadSJonathan Lemon 		return 0;
463a7e1abadSJonathan Lemon 
464a7e1abadSJonathan Lemon 	return -EOPNOTSUPP;
465a7e1abadSJonathan Lemon }
466a7e1abadSJonathan Lemon 
467773bda96SJonathan Lemon static int
468773bda96SJonathan Lemon ptp_ocp_adjphase(struct ptp_clock_info *ptp_info, s32 phase_ns)
469773bda96SJonathan Lemon {
470773bda96SJonathan Lemon 	return -EOPNOTSUPP;
471773bda96SJonathan Lemon }
472773bda96SJonathan Lemon 
473773bda96SJonathan Lemon static int
474773bda96SJonathan Lemon ptp_ocp_enable(struct ptp_clock_info *ptp_info, struct ptp_clock_request *rq,
475773bda96SJonathan Lemon 	       int on)
476773bda96SJonathan Lemon {
477773bda96SJonathan Lemon 	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
478773bda96SJonathan Lemon 	struct ptp_ocp_ext_src *ext = NULL;
479773bda96SJonathan Lemon 	int err;
480773bda96SJonathan Lemon 
481773bda96SJonathan Lemon 	switch (rq->type) {
482773bda96SJonathan Lemon 	case PTP_CLK_REQ_EXTTS:
483773bda96SJonathan Lemon 		switch (rq->extts.index) {
484773bda96SJonathan Lemon 		case 0:
485773bda96SJonathan Lemon 			ext = bp->ts0;
486773bda96SJonathan Lemon 			break;
487773bda96SJonathan Lemon 		case 1:
488773bda96SJonathan Lemon 			ext = bp->ts1;
489773bda96SJonathan Lemon 			break;
490773bda96SJonathan Lemon 		}
491773bda96SJonathan Lemon 		break;
492773bda96SJonathan Lemon 	case PTP_CLK_REQ_PPS:
493773bda96SJonathan Lemon 		ext = bp->pps;
494773bda96SJonathan Lemon 		break;
495773bda96SJonathan Lemon 	default:
496773bda96SJonathan Lemon 		return -EOPNOTSUPP;
497773bda96SJonathan Lemon 	}
498773bda96SJonathan Lemon 
499773bda96SJonathan Lemon 	err = -ENXIO;
500773bda96SJonathan Lemon 	if (ext)
501773bda96SJonathan Lemon 		err = ext->info->enable(ext, on);
502773bda96SJonathan Lemon 
503773bda96SJonathan Lemon 	return err;
504773bda96SJonathan Lemon }
505773bda96SJonathan Lemon 
506a7e1abadSJonathan Lemon static const struct ptp_clock_info ptp_ocp_clock_info = {
507a7e1abadSJonathan Lemon 	.owner		= THIS_MODULE,
508a7e1abadSJonathan Lemon 	.name		= KBUILD_MODNAME,
509a7e1abadSJonathan Lemon 	.max_adj	= 100000000,
510a7e1abadSJonathan Lemon 	.gettimex64	= ptp_ocp_gettimex,
511a7e1abadSJonathan Lemon 	.settime64	= ptp_ocp_settime,
512a7e1abadSJonathan Lemon 	.adjtime	= ptp_ocp_adjtime,
513a7e1abadSJonathan Lemon 	.adjfine	= ptp_ocp_null_adjfine,
514773bda96SJonathan Lemon 	.adjphase	= ptp_ocp_adjphase,
515773bda96SJonathan Lemon 	.enable		= ptp_ocp_enable,
516773bda96SJonathan Lemon 	.pps		= true,
517773bda96SJonathan Lemon 	.n_ext_ts	= 2,
518a7e1abadSJonathan Lemon };
519a7e1abadSJonathan Lemon 
520773bda96SJonathan Lemon static void
521773bda96SJonathan Lemon __ptp_ocp_clear_drift_locked(struct ptp_ocp *bp)
522773bda96SJonathan Lemon {
523773bda96SJonathan Lemon 	u32 ctrl, select;
524773bda96SJonathan Lemon 
525773bda96SJonathan Lemon 	select = ioread32(&bp->reg->select);
526773bda96SJonathan Lemon 	iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select);
527773bda96SJonathan Lemon 
528773bda96SJonathan Lemon 	iowrite32(0, &bp->reg->drift_ns);
529773bda96SJonathan Lemon 
530773bda96SJonathan Lemon 	ctrl = ioread32(&bp->reg->ctrl);
531773bda96SJonathan Lemon 	ctrl |= OCP_CTRL_ADJUST_DRIFT;
532773bda96SJonathan Lemon 	iowrite32(ctrl, &bp->reg->ctrl);
533773bda96SJonathan Lemon 
534773bda96SJonathan Lemon 	/* restore clock selection */
535773bda96SJonathan Lemon 	iowrite32(select >> 16, &bp->reg->select);
536773bda96SJonathan Lemon }
537773bda96SJonathan Lemon 
538773bda96SJonathan Lemon static void
539773bda96SJonathan Lemon ptp_ocp_watchdog(struct timer_list *t)
540773bda96SJonathan Lemon {
541773bda96SJonathan Lemon 	struct ptp_ocp *bp = from_timer(bp, t, watchdog);
542773bda96SJonathan Lemon 	unsigned long flags;
543773bda96SJonathan Lemon 	u32 status;
544773bda96SJonathan Lemon 
5450d43d4f2SJonathan Lemon 	status = ioread32(&bp->pps_to_clk->status);
546773bda96SJonathan Lemon 
547773bda96SJonathan Lemon 	if (status & PPS_STATUS_SUPERV_ERR) {
5480d43d4f2SJonathan Lemon 		iowrite32(status, &bp->pps_to_clk->status);
549*ef0cfb34SJonathan Lemon 		if (!bp->gnss_lost) {
550773bda96SJonathan Lemon 			spin_lock_irqsave(&bp->lock, flags);
551773bda96SJonathan Lemon 			__ptp_ocp_clear_drift_locked(bp);
552773bda96SJonathan Lemon 			spin_unlock_irqrestore(&bp->lock, flags);
553*ef0cfb34SJonathan Lemon 			bp->gnss_lost = ktime_get_real_seconds();
554773bda96SJonathan Lemon 		}
555773bda96SJonathan Lemon 
556*ef0cfb34SJonathan Lemon 	} else if (bp->gnss_lost) {
557*ef0cfb34SJonathan Lemon 		bp->gnss_lost = 0;
558773bda96SJonathan Lemon 	}
559773bda96SJonathan Lemon 
560773bda96SJonathan Lemon 	mod_timer(&bp->watchdog, jiffies + HZ);
561773bda96SJonathan Lemon }
562773bda96SJonathan Lemon 
563a7e1abadSJonathan Lemon static int
564773bda96SJonathan Lemon ptp_ocp_init_clock(struct ptp_ocp *bp)
565a7e1abadSJonathan Lemon {
566a7e1abadSJonathan Lemon 	struct timespec64 ts;
567a7e1abadSJonathan Lemon 	bool sync;
568a7e1abadSJonathan Lemon 	u32 ctrl;
569a7e1abadSJonathan Lemon 
570a7e1abadSJonathan Lemon 	/* make sure clock is enabled */
571a7e1abadSJonathan Lemon 	ctrl = ioread32(&bp->reg->ctrl);
572a7e1abadSJonathan Lemon 	ctrl |= OCP_CTRL_ENABLE;
573a7e1abadSJonathan Lemon 	iowrite32(ctrl, &bp->reg->ctrl);
574a7e1abadSJonathan Lemon 
575773bda96SJonathan Lemon 	/* NO DRIFT Correction */
576773bda96SJonathan Lemon 	/* offset_p:i 1/8, offset_i: 1/16, drift_p: 0, drift_i: 0 */
577773bda96SJonathan Lemon 	iowrite32(0x2000, &bp->reg->servo_offset_p);
578773bda96SJonathan Lemon 	iowrite32(0x1000, &bp->reg->servo_offset_i);
579773bda96SJonathan Lemon 	iowrite32(0,	  &bp->reg->servo_drift_p);
580773bda96SJonathan Lemon 	iowrite32(0,	  &bp->reg->servo_drift_i);
581773bda96SJonathan Lemon 
582773bda96SJonathan Lemon 	/* latch servo values */
583773bda96SJonathan Lemon 	ctrl |= OCP_CTRL_ADJUST_SERVO;
584773bda96SJonathan Lemon 	iowrite32(ctrl, &bp->reg->ctrl);
585773bda96SJonathan Lemon 
586a7e1abadSJonathan Lemon 	if ((ioread32(&bp->reg->ctrl) & OCP_CTRL_ENABLE) == 0) {
587a7e1abadSJonathan Lemon 		dev_err(&bp->pdev->dev, "clock not enabled\n");
588a7e1abadSJonathan Lemon 		return -ENODEV;
589a7e1abadSJonathan Lemon 	}
590a7e1abadSJonathan Lemon 
591a7e1abadSJonathan Lemon 	sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC;
592a7e1abadSJonathan Lemon 	if (!sync) {
593a7e1abadSJonathan Lemon 		ktime_get_real_ts64(&ts);
594a7e1abadSJonathan Lemon 		ptp_ocp_settime(&bp->ptp_info, &ts);
595a7e1abadSJonathan Lemon 	}
596a7e1abadSJonathan Lemon 	if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, NULL))
597a7e1abadSJonathan Lemon 		dev_info(&bp->pdev->dev, "Time: %lld.%ld, %s\n",
598a7e1abadSJonathan Lemon 			 ts.tv_sec, ts.tv_nsec,
599a7e1abadSJonathan Lemon 			 sync ? "in-sync" : "UNSYNCED");
600a7e1abadSJonathan Lemon 
601773bda96SJonathan Lemon 	timer_setup(&bp->watchdog, ptp_ocp_watchdog, 0);
602773bda96SJonathan Lemon 	mod_timer(&bp->watchdog, jiffies + HZ);
603773bda96SJonathan Lemon 
604a7e1abadSJonathan Lemon 	return 0;
605a7e1abadSJonathan Lemon }
606a7e1abadSJonathan Lemon 
607a7e1abadSJonathan Lemon static void
608a7e1abadSJonathan Lemon ptp_ocp_tod_info(struct ptp_ocp *bp)
609a7e1abadSJonathan Lemon {
610a7e1abadSJonathan Lemon 	static const char * const proto_name[] = {
611a7e1abadSJonathan Lemon 		"NMEA", "NMEA_ZDA", "NMEA_RMC", "NMEA_none",
612a7e1abadSJonathan Lemon 		"UBX", "UBX_UTC", "UBX_LS", "UBX_none"
613a7e1abadSJonathan Lemon 	};
614a7e1abadSJonathan Lemon 	static const char * const gnss_name[] = {
615a7e1abadSJonathan Lemon 		"ALL", "COMBINED", "GPS", "GLONASS", "GALILEO", "BEIDOU",
616a7e1abadSJonathan Lemon 	};
617a7e1abadSJonathan Lemon 	u32 version, ctrl, reg;
618a7e1abadSJonathan Lemon 	int idx;
619a7e1abadSJonathan Lemon 
620a7e1abadSJonathan Lemon 	version = ioread32(&bp->tod->version);
621a7e1abadSJonathan Lemon 	dev_info(&bp->pdev->dev, "TOD Version %d.%d.%d\n",
622a7e1abadSJonathan Lemon 		 version >> 24, (version >> 16) & 0xff, version & 0xffff);
623a7e1abadSJonathan Lemon 
624a7e1abadSJonathan Lemon 	ctrl = ioread32(&bp->tod->ctrl);
625a7e1abadSJonathan Lemon 	ctrl |= TOD_CTRL_PROTOCOL | TOD_CTRL_ENABLE;
626a7e1abadSJonathan Lemon 	ctrl &= ~(TOD_CTRL_DISABLE_FMT_A | TOD_CTRL_DISABLE_FMT_B);
627a7e1abadSJonathan Lemon 	iowrite32(ctrl, &bp->tod->ctrl);
628a7e1abadSJonathan Lemon 
629a7e1abadSJonathan Lemon 	ctrl = ioread32(&bp->tod->ctrl);
630a7e1abadSJonathan Lemon 	idx = ctrl & TOD_CTRL_PROTOCOL ? 4 : 0;
631a7e1abadSJonathan Lemon 	idx += (ctrl >> 16) & 3;
632a7e1abadSJonathan Lemon 	dev_info(&bp->pdev->dev, "control: %x\n", ctrl);
633a7e1abadSJonathan Lemon 	dev_info(&bp->pdev->dev, "TOD Protocol %s %s\n", proto_name[idx],
634a7e1abadSJonathan Lemon 		 ctrl & TOD_CTRL_ENABLE ? "enabled" : "");
635a7e1abadSJonathan Lemon 
636a7e1abadSJonathan Lemon 	idx = (ctrl >> TOD_CTRL_GNSS_SHIFT) & TOD_CTRL_GNSS_MASK;
637a7e1abadSJonathan Lemon 	if (idx < ARRAY_SIZE(gnss_name))
638a7e1abadSJonathan Lemon 		dev_info(&bp->pdev->dev, "GNSS %s\n", gnss_name[idx]);
639a7e1abadSJonathan Lemon 
640a7e1abadSJonathan Lemon 	reg = ioread32(&bp->tod->status);
641a7e1abadSJonathan Lemon 	dev_info(&bp->pdev->dev, "status: %x\n", reg);
642a7e1abadSJonathan Lemon 
643a7e1abadSJonathan Lemon 	reg = ioread32(&bp->tod->correction_sec);
644a7e1abadSJonathan Lemon 	dev_info(&bp->pdev->dev, "correction: %d\n", reg);
645a7e1abadSJonathan Lemon 
646a7e1abadSJonathan Lemon 	reg = ioread32(&bp->tod->utc_status);
647a7e1abadSJonathan Lemon 	dev_info(&bp->pdev->dev, "utc_status: %x\n", reg);
648a7e1abadSJonathan Lemon 	dev_info(&bp->pdev->dev, "utc_offset: %d  valid:%d  leap_valid:%d\n",
649a7e1abadSJonathan Lemon 		 reg & TOD_STATUS_UTC_MASK, reg & TOD_STATUS_UTC_VALID ? 1 : 0,
650a7e1abadSJonathan Lemon 		 reg & TOD_STATUS_LEAP_VALID ? 1 : 0);
651a7e1abadSJonathan Lemon }
652a7e1abadSJonathan Lemon 
653773bda96SJonathan Lemon static int
654773bda96SJonathan Lemon ptp_ocp_firstchild(struct device *dev, void *data)
655773bda96SJonathan Lemon {
656773bda96SJonathan Lemon 	return 1;
657773bda96SJonathan Lemon }
658773bda96SJonathan Lemon 
659773bda96SJonathan Lemon static int
660773bda96SJonathan Lemon ptp_ocp_read_i2c(struct i2c_adapter *adap, u8 addr, u8 reg, u8 sz, u8 *data)
661773bda96SJonathan Lemon {
662773bda96SJonathan Lemon 	struct i2c_msg msgs[2] = {
663773bda96SJonathan Lemon 		{
664773bda96SJonathan Lemon 			.addr = addr,
665773bda96SJonathan Lemon 			.len = 1,
666773bda96SJonathan Lemon 			.buf = &reg,
667773bda96SJonathan Lemon 		},
668773bda96SJonathan Lemon 		{
669773bda96SJonathan Lemon 			.addr = addr,
670773bda96SJonathan Lemon 			.flags = I2C_M_RD,
671773bda96SJonathan Lemon 			.len = 2,
672773bda96SJonathan Lemon 			.buf = data,
673773bda96SJonathan Lemon 		},
674773bda96SJonathan Lemon 	};
675773bda96SJonathan Lemon 	int err;
676773bda96SJonathan Lemon 	u8 len;
677773bda96SJonathan Lemon 
678773bda96SJonathan Lemon 	/* xiic-i2c for some stupid reason only does 2 byte reads. */
679773bda96SJonathan Lemon 	while (sz) {
680773bda96SJonathan Lemon 		len = min_t(u8, sz, 2);
681773bda96SJonathan Lemon 		msgs[1].len = len;
682773bda96SJonathan Lemon 		err = i2c_transfer(adap, msgs, 2);
683773bda96SJonathan Lemon 		if (err != msgs[1].len)
684773bda96SJonathan Lemon 			return err;
685773bda96SJonathan Lemon 		msgs[1].buf += len;
686773bda96SJonathan Lemon 		reg += len;
687773bda96SJonathan Lemon 		sz -= len;
688773bda96SJonathan Lemon 	}
689773bda96SJonathan Lemon 	return 0;
690773bda96SJonathan Lemon }
691773bda96SJonathan Lemon 
692773bda96SJonathan Lemon static void
693773bda96SJonathan Lemon ptp_ocp_get_serial_number(struct ptp_ocp *bp)
694773bda96SJonathan Lemon {
695773bda96SJonathan Lemon 	struct i2c_adapter *adap;
696773bda96SJonathan Lemon 	struct device *dev;
697773bda96SJonathan Lemon 	int err;
698773bda96SJonathan Lemon 
699773bda96SJonathan Lemon 	dev = device_find_child(&bp->i2c_ctrl->dev, NULL, ptp_ocp_firstchild);
700773bda96SJonathan Lemon 	if (!dev) {
701773bda96SJonathan Lemon 		dev_err(&bp->pdev->dev, "Can't find I2C adapter\n");
702773bda96SJonathan Lemon 		return;
703773bda96SJonathan Lemon 	}
704773bda96SJonathan Lemon 
705773bda96SJonathan Lemon 	adap = i2c_verify_adapter(dev);
706773bda96SJonathan Lemon 	if (!adap) {
707773bda96SJonathan Lemon 		dev_err(&bp->pdev->dev, "device '%s' isn't an I2C adapter\n",
708773bda96SJonathan Lemon 			dev_name(dev));
709773bda96SJonathan Lemon 		goto out;
710773bda96SJonathan Lemon 	}
711773bda96SJonathan Lemon 
712773bda96SJonathan Lemon 	err = ptp_ocp_read_i2c(adap, 0x58, 0x9A, 6, bp->serial);
713773bda96SJonathan Lemon 	if (err) {
714773bda96SJonathan Lemon 		dev_err(&bp->pdev->dev, "could not read eeprom: %d\n", err);
715773bda96SJonathan Lemon 		goto out;
716773bda96SJonathan Lemon 	}
717773bda96SJonathan Lemon 
718773bda96SJonathan Lemon 	bp->has_serial = true;
719773bda96SJonathan Lemon 
720773bda96SJonathan Lemon out:
721773bda96SJonathan Lemon 	put_device(dev);
722773bda96SJonathan Lemon }
723773bda96SJonathan Lemon 
724a7e1abadSJonathan Lemon static void
725a7e1abadSJonathan Lemon ptp_ocp_info(struct ptp_ocp *bp)
726a7e1abadSJonathan Lemon {
727a7e1abadSJonathan Lemon 	u32 version, select;
728a7e1abadSJonathan Lemon 
729a7e1abadSJonathan Lemon 	version = ioread32(&bp->reg->version);
730a7e1abadSJonathan Lemon 	select = ioread32(&bp->reg->select);
731a7e1abadSJonathan Lemon 	dev_info(&bp->pdev->dev, "Version %d.%d.%d, clock %s, device ptp%d\n",
732a7e1abadSJonathan Lemon 		 version >> 24, (version >> 16) & 0xff, version & 0xffff,
733773bda96SJonathan Lemon 		 ptp_ocp_clock_name_from_val(select >> 16),
734a7e1abadSJonathan Lemon 		 ptp_clock_index(bp->ptp));
735a7e1abadSJonathan Lemon 
736a7e1abadSJonathan Lemon 	ptp_ocp_tod_info(bp);
737a7e1abadSJonathan Lemon }
738a7e1abadSJonathan Lemon 
739773bda96SJonathan Lemon static int
740773bda96SJonathan Lemon ptp_ocp_devlink_register(struct devlink *devlink, struct device *dev)
741773bda96SJonathan Lemon {
742773bda96SJonathan Lemon 	int err;
743773bda96SJonathan Lemon 
744773bda96SJonathan Lemon 	err = devlink_register(devlink, dev);
745773bda96SJonathan Lemon 	if (err)
746773bda96SJonathan Lemon 		return err;
747773bda96SJonathan Lemon 
748773bda96SJonathan Lemon 	return 0;
749773bda96SJonathan Lemon }
750773bda96SJonathan Lemon 
751773bda96SJonathan Lemon static void
752773bda96SJonathan Lemon ptp_ocp_devlink_unregister(struct devlink *devlink)
753773bda96SJonathan Lemon {
754773bda96SJonathan Lemon 	devlink_unregister(devlink);
755773bda96SJonathan Lemon }
756773bda96SJonathan Lemon 
757773bda96SJonathan Lemon static struct device *
758773bda96SJonathan Lemon ptp_ocp_find_flash(struct ptp_ocp *bp)
759773bda96SJonathan Lemon {
760773bda96SJonathan Lemon 	struct device *dev, *last;
761773bda96SJonathan Lemon 
762773bda96SJonathan Lemon 	last = NULL;
763773bda96SJonathan Lemon 	dev = &bp->spi_flash->dev;
764773bda96SJonathan Lemon 
765773bda96SJonathan Lemon 	while ((dev = device_find_child(dev, NULL, ptp_ocp_firstchild))) {
766773bda96SJonathan Lemon 		if (!strcmp("mtd", dev_bus_name(dev)))
767773bda96SJonathan Lemon 			break;
768773bda96SJonathan Lemon 		put_device(last);
769773bda96SJonathan Lemon 		last = dev;
770773bda96SJonathan Lemon 	}
771773bda96SJonathan Lemon 	put_device(last);
772773bda96SJonathan Lemon 
773773bda96SJonathan Lemon 	return dev;
774773bda96SJonathan Lemon }
775773bda96SJonathan Lemon 
776773bda96SJonathan Lemon static int
777773bda96SJonathan Lemon ptp_ocp_devlink_flash(struct devlink *devlink, struct device *dev,
778773bda96SJonathan Lemon 		      const struct firmware *fw)
779773bda96SJonathan Lemon {
780773bda96SJonathan Lemon 	struct mtd_info *mtd = dev_get_drvdata(dev);
781773bda96SJonathan Lemon 	struct ptp_ocp *bp = devlink_priv(devlink);
782773bda96SJonathan Lemon 	size_t off, len, resid, wrote;
783773bda96SJonathan Lemon 	struct erase_info erase;
784773bda96SJonathan Lemon 	size_t base, blksz;
785773bda96SJonathan Lemon 	int err;
786773bda96SJonathan Lemon 
787773bda96SJonathan Lemon 	off = 0;
788773bda96SJonathan Lemon 	base = bp->flash_start;
789773bda96SJonathan Lemon 	blksz = 4096;
790773bda96SJonathan Lemon 	resid = fw->size;
791773bda96SJonathan Lemon 
792773bda96SJonathan Lemon 	while (resid) {
793773bda96SJonathan Lemon 		devlink_flash_update_status_notify(devlink, "Flashing",
794773bda96SJonathan Lemon 						   NULL, off, fw->size);
795773bda96SJonathan Lemon 
796773bda96SJonathan Lemon 		len = min_t(size_t, resid, blksz);
797773bda96SJonathan Lemon 		erase.addr = base + off;
798773bda96SJonathan Lemon 		erase.len = blksz;
799773bda96SJonathan Lemon 
800773bda96SJonathan Lemon 		err = mtd_erase(mtd, &erase);
801773bda96SJonathan Lemon 		if (err)
802773bda96SJonathan Lemon 			goto out;
803773bda96SJonathan Lemon 
804773bda96SJonathan Lemon 		err = mtd_write(mtd, base + off, len, &wrote, &fw->data[off]);
805773bda96SJonathan Lemon 		if (err)
806773bda96SJonathan Lemon 			goto out;
807773bda96SJonathan Lemon 
808773bda96SJonathan Lemon 		off += blksz;
809773bda96SJonathan Lemon 		resid -= len;
810773bda96SJonathan Lemon 	}
811773bda96SJonathan Lemon out:
812773bda96SJonathan Lemon 	return err;
813773bda96SJonathan Lemon }
814773bda96SJonathan Lemon 
815773bda96SJonathan Lemon static int
816773bda96SJonathan Lemon ptp_ocp_devlink_flash_update(struct devlink *devlink,
817773bda96SJonathan Lemon 			     struct devlink_flash_update_params *params,
818773bda96SJonathan Lemon 			     struct netlink_ext_ack *extack)
819773bda96SJonathan Lemon {
820773bda96SJonathan Lemon 	struct ptp_ocp *bp = devlink_priv(devlink);
821773bda96SJonathan Lemon 	struct device *dev;
822773bda96SJonathan Lemon 	const char *msg;
823773bda96SJonathan Lemon 	int err;
824773bda96SJonathan Lemon 
825773bda96SJonathan Lemon 	dev = ptp_ocp_find_flash(bp);
826773bda96SJonathan Lemon 	if (!dev) {
827773bda96SJonathan Lemon 		dev_err(&bp->pdev->dev, "Can't find Flash SPI adapter\n");
828773bda96SJonathan Lemon 		return -ENODEV;
829773bda96SJonathan Lemon 	}
830773bda96SJonathan Lemon 
831773bda96SJonathan Lemon 	devlink_flash_update_status_notify(devlink, "Preparing to flash",
832773bda96SJonathan Lemon 					   NULL, 0, 0);
833773bda96SJonathan Lemon 
834773bda96SJonathan Lemon 	err = ptp_ocp_devlink_flash(devlink, dev, params->fw);
835773bda96SJonathan Lemon 
836773bda96SJonathan Lemon 	msg = err ? "Flash error" : "Flash complete";
837773bda96SJonathan Lemon 	devlink_flash_update_status_notify(devlink, msg, NULL, 0, 0);
838773bda96SJonathan Lemon 
839773bda96SJonathan Lemon 	bp->pending_image = true;
840773bda96SJonathan Lemon 
841773bda96SJonathan Lemon 	put_device(dev);
842773bda96SJonathan Lemon 	return err;
843773bda96SJonathan Lemon }
844773bda96SJonathan Lemon 
845773bda96SJonathan Lemon static int
846773bda96SJonathan Lemon ptp_ocp_devlink_info_get(struct devlink *devlink, struct devlink_info_req *req,
847773bda96SJonathan Lemon 			 struct netlink_ext_ack *extack)
848773bda96SJonathan Lemon {
849773bda96SJonathan Lemon 	struct ptp_ocp *bp = devlink_priv(devlink);
850773bda96SJonathan Lemon 	char buf[32];
851773bda96SJonathan Lemon 	int err;
852773bda96SJonathan Lemon 
853773bda96SJonathan Lemon 	err = devlink_info_driver_name_put(req, KBUILD_MODNAME);
854773bda96SJonathan Lemon 	if (err)
855773bda96SJonathan Lemon 		return err;
856773bda96SJonathan Lemon 
857773bda96SJonathan Lemon 	if (bp->pending_image) {
858773bda96SJonathan Lemon 		err = devlink_info_version_stored_put(req,
859773bda96SJonathan Lemon 						      "timecard", "pending");
860773bda96SJonathan Lemon 		if (err)
861773bda96SJonathan Lemon 			return err;
862773bda96SJonathan Lemon 	}
863773bda96SJonathan Lemon 
864773bda96SJonathan Lemon 	if (bp->image) {
865773bda96SJonathan Lemon 		u32 ver = ioread32(&bp->image->version);
866773bda96SJonathan Lemon 
867773bda96SJonathan Lemon 		if (ver & 0xffff) {
868773bda96SJonathan Lemon 			sprintf(buf, "%d", ver);
869773bda96SJonathan Lemon 			err = devlink_info_version_running_put(req,
870773bda96SJonathan Lemon 							       "timecard",
871773bda96SJonathan Lemon 							       buf);
872773bda96SJonathan Lemon 		} else {
873773bda96SJonathan Lemon 			sprintf(buf, "%d", ver >> 16);
874773bda96SJonathan Lemon 			err = devlink_info_version_running_put(req,
875773bda96SJonathan Lemon 							       "golden flash",
876773bda96SJonathan Lemon 							       buf);
877773bda96SJonathan Lemon 		}
878773bda96SJonathan Lemon 		if (err)
879773bda96SJonathan Lemon 			return err;
880773bda96SJonathan Lemon 	}
881773bda96SJonathan Lemon 
882773bda96SJonathan Lemon 	if (!bp->has_serial)
883773bda96SJonathan Lemon 		ptp_ocp_get_serial_number(bp);
884773bda96SJonathan Lemon 
885773bda96SJonathan Lemon 	if (bp->has_serial) {
886773bda96SJonathan Lemon 		sprintf(buf, "%pM", bp->serial);
887773bda96SJonathan Lemon 		err = devlink_info_serial_number_put(req, buf);
888773bda96SJonathan Lemon 		if (err)
889773bda96SJonathan Lemon 			return err;
890773bda96SJonathan Lemon 	}
891773bda96SJonathan Lemon 
892773bda96SJonathan Lemon 	return 0;
893773bda96SJonathan Lemon }
894773bda96SJonathan Lemon 
895773bda96SJonathan Lemon static const struct devlink_ops ptp_ocp_devlink_ops = {
896773bda96SJonathan Lemon 	.flash_update = ptp_ocp_devlink_flash_update,
897773bda96SJonathan Lemon 	.info_get = ptp_ocp_devlink_info_get,
898773bda96SJonathan Lemon };
899773bda96SJonathan Lemon 
900773bda96SJonathan Lemon static void __iomem *
901773bda96SJonathan Lemon __ptp_ocp_get_mem(struct ptp_ocp *bp, unsigned long start, int size)
902773bda96SJonathan Lemon {
903773bda96SJonathan Lemon 	struct resource res = DEFINE_RES_MEM_NAMED(start, size, "ptp_ocp");
904773bda96SJonathan Lemon 
905773bda96SJonathan Lemon 	return devm_ioremap_resource(&bp->pdev->dev, &res);
906773bda96SJonathan Lemon }
907773bda96SJonathan Lemon 
908773bda96SJonathan Lemon static void __iomem *
909773bda96SJonathan Lemon ptp_ocp_get_mem(struct ptp_ocp *bp, struct ocp_resource *r)
910773bda96SJonathan Lemon {
911773bda96SJonathan Lemon 	unsigned long start;
912773bda96SJonathan Lemon 
913773bda96SJonathan Lemon 	start = pci_resource_start(bp->pdev, 0) + r->offset;
914773bda96SJonathan Lemon 	return __ptp_ocp_get_mem(bp, start, r->size);
915773bda96SJonathan Lemon }
916773bda96SJonathan Lemon 
917773bda96SJonathan Lemon static void
918773bda96SJonathan Lemon ptp_ocp_set_irq_resource(struct resource *res, int irq)
919773bda96SJonathan Lemon {
920773bda96SJonathan Lemon 	struct resource r = DEFINE_RES_IRQ(irq);
921773bda96SJonathan Lemon 	*res = r;
922773bda96SJonathan Lemon }
923773bda96SJonathan Lemon 
924773bda96SJonathan Lemon static void
925773bda96SJonathan Lemon ptp_ocp_set_mem_resource(struct resource *res, unsigned long start, int size)
926773bda96SJonathan Lemon {
927773bda96SJonathan Lemon 	struct resource r = DEFINE_RES_MEM(start, size);
928773bda96SJonathan Lemon 	*res = r;
929773bda96SJonathan Lemon }
930773bda96SJonathan Lemon 
931773bda96SJonathan Lemon static int
932773bda96SJonathan Lemon ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r)
933773bda96SJonathan Lemon {
934773bda96SJonathan Lemon 	struct ptp_ocp_flash_info *info;
935773bda96SJonathan Lemon 	struct pci_dev *pdev = bp->pdev;
936773bda96SJonathan Lemon 	struct platform_device *p;
937773bda96SJonathan Lemon 	struct resource res[2];
938773bda96SJonathan Lemon 	unsigned long start;
939773bda96SJonathan Lemon 	int id;
940773bda96SJonathan Lemon 
941773bda96SJonathan Lemon 	/* XXX hack to work around old FPGA */
942773bda96SJonathan Lemon 	if (bp->n_irqs < 10) {
943773bda96SJonathan Lemon 		dev_err(&bp->pdev->dev, "FPGA does not have SPI devices\n");
944773bda96SJonathan Lemon 		return 0;
945773bda96SJonathan Lemon 	}
946773bda96SJonathan Lemon 
947773bda96SJonathan Lemon 	if (r->irq_vec > bp->n_irqs) {
948773bda96SJonathan Lemon 		dev_err(&bp->pdev->dev, "spi device irq %d out of range\n",
949773bda96SJonathan Lemon 			r->irq_vec);
950773bda96SJonathan Lemon 		return 0;
951773bda96SJonathan Lemon 	}
952773bda96SJonathan Lemon 
953773bda96SJonathan Lemon 	start = pci_resource_start(pdev, 0) + r->offset;
954773bda96SJonathan Lemon 	ptp_ocp_set_mem_resource(&res[0], start, r->size);
955773bda96SJonathan Lemon 	ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec));
956773bda96SJonathan Lemon 
957773bda96SJonathan Lemon 	info = r->extra;
958773bda96SJonathan Lemon 	id = pci_dev_id(pdev) << 1;
959773bda96SJonathan Lemon 	id += info->pci_offset;
960773bda96SJonathan Lemon 
961773bda96SJonathan Lemon 	p = platform_device_register_resndata(&pdev->dev, info->name, id,
962773bda96SJonathan Lemon 					      res, 2, info->data,
963773bda96SJonathan Lemon 					      info->data_size);
964773bda96SJonathan Lemon 	if (IS_ERR(p))
965773bda96SJonathan Lemon 		return PTR_ERR(p);
966773bda96SJonathan Lemon 
967773bda96SJonathan Lemon 	bp_assign_entry(bp, r, p);
968773bda96SJonathan Lemon 
969773bda96SJonathan Lemon 	return 0;
970773bda96SJonathan Lemon }
971773bda96SJonathan Lemon 
972773bda96SJonathan Lemon static struct platform_device *
973773bda96SJonathan Lemon ptp_ocp_i2c_bus(struct pci_dev *pdev, struct ocp_resource *r, int id)
974773bda96SJonathan Lemon {
975773bda96SJonathan Lemon 	struct resource res[2];
976773bda96SJonathan Lemon 	unsigned long start;
977773bda96SJonathan Lemon 
978773bda96SJonathan Lemon 	start = pci_resource_start(pdev, 0) + r->offset;
979773bda96SJonathan Lemon 	ptp_ocp_set_mem_resource(&res[0], start, r->size);
980773bda96SJonathan Lemon 	ptp_ocp_set_irq_resource(&res[1], pci_irq_vector(pdev, r->irq_vec));
981773bda96SJonathan Lemon 
982773bda96SJonathan Lemon 	return platform_device_register_resndata(&pdev->dev, "xiic-i2c",
983773bda96SJonathan Lemon 						 id, res, 2, NULL, 0);
984773bda96SJonathan Lemon }
985773bda96SJonathan Lemon 
986773bda96SJonathan Lemon static int
987773bda96SJonathan Lemon ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r)
988773bda96SJonathan Lemon {
989773bda96SJonathan Lemon 	struct pci_dev *pdev = bp->pdev;
990773bda96SJonathan Lemon 	struct platform_device *p;
991773bda96SJonathan Lemon 	struct clk_hw *clk;
992773bda96SJonathan Lemon 	char buf[32];
993773bda96SJonathan Lemon 	int id;
994773bda96SJonathan Lemon 
995773bda96SJonathan Lemon 	if (r->irq_vec > bp->n_irqs) {
996773bda96SJonathan Lemon 		dev_err(&bp->pdev->dev, "i2c device irq %d out of range\n",
997773bda96SJonathan Lemon 			r->irq_vec);
998773bda96SJonathan Lemon 		return 0;
999773bda96SJonathan Lemon 	}
1000773bda96SJonathan Lemon 
1001773bda96SJonathan Lemon 	id = pci_dev_id(bp->pdev);
1002773bda96SJonathan Lemon 
1003773bda96SJonathan Lemon 	sprintf(buf, "AXI.%d", id);
1004773bda96SJonathan Lemon 	clk = clk_hw_register_fixed_rate(&pdev->dev, buf, NULL, 0, 50000000);
1005773bda96SJonathan Lemon 	if (IS_ERR(clk))
1006773bda96SJonathan Lemon 		return PTR_ERR(clk);
1007773bda96SJonathan Lemon 	bp->i2c_clk = clk;
1008773bda96SJonathan Lemon 
1009773bda96SJonathan Lemon 	sprintf(buf, "xiic-i2c.%d", id);
1010773bda96SJonathan Lemon 	devm_clk_hw_register_clkdev(&pdev->dev, clk, NULL, buf);
1011773bda96SJonathan Lemon 	p = ptp_ocp_i2c_bus(bp->pdev, r, id);
1012773bda96SJonathan Lemon 	if (IS_ERR(p))
1013773bda96SJonathan Lemon 		return PTR_ERR(p);
1014773bda96SJonathan Lemon 
1015773bda96SJonathan Lemon 	bp_assign_entry(bp, r, p);
1016773bda96SJonathan Lemon 
1017773bda96SJonathan Lemon 	return 0;
1018773bda96SJonathan Lemon }
1019773bda96SJonathan Lemon 
1020773bda96SJonathan Lemon static irqreturn_t
1021773bda96SJonathan Lemon ptp_ocp_ts_irq(int irq, void *priv)
1022773bda96SJonathan Lemon {
1023773bda96SJonathan Lemon 	struct ptp_ocp_ext_src *ext = priv;
1024773bda96SJonathan Lemon 	struct ts_reg __iomem *reg = ext->mem;
1025773bda96SJonathan Lemon 	struct ptp_clock_event ev;
1026773bda96SJonathan Lemon 	u32 sec, nsec;
1027773bda96SJonathan Lemon 
1028773bda96SJonathan Lemon 	/* XXX should fix API - this converts s/ns -> ts -> s/ns */
1029773bda96SJonathan Lemon 	sec = ioread32(&reg->time_sec);
1030773bda96SJonathan Lemon 	nsec = ioread32(&reg->time_ns);
1031773bda96SJonathan Lemon 
1032773bda96SJonathan Lemon 	ev.type = PTP_CLOCK_EXTTS;
1033773bda96SJonathan Lemon 	ev.index = ext->info->index;
1034773bda96SJonathan Lemon 	ev.timestamp = sec * 1000000000ULL + nsec;
1035773bda96SJonathan Lemon 
1036773bda96SJonathan Lemon 	ptp_clock_event(ext->bp->ptp, &ev);
1037773bda96SJonathan Lemon 
1038773bda96SJonathan Lemon 	iowrite32(1, &reg->intr);	/* write 1 to ack */
1039773bda96SJonathan Lemon 
1040773bda96SJonathan Lemon 	return IRQ_HANDLED;
1041773bda96SJonathan Lemon }
1042773bda96SJonathan Lemon 
1043773bda96SJonathan Lemon static int
1044773bda96SJonathan Lemon ptp_ocp_ts_enable(void *priv, bool enable)
1045773bda96SJonathan Lemon {
1046773bda96SJonathan Lemon 	struct ptp_ocp_ext_src *ext = priv;
1047773bda96SJonathan Lemon 	struct ts_reg __iomem *reg = ext->mem;
1048773bda96SJonathan Lemon 
1049773bda96SJonathan Lemon 	if (enable) {
1050773bda96SJonathan Lemon 		iowrite32(1, &reg->enable);
1051773bda96SJonathan Lemon 		iowrite32(1, &reg->intr_mask);
1052773bda96SJonathan Lemon 		iowrite32(1, &reg->intr);
1053773bda96SJonathan Lemon 	} else {
1054773bda96SJonathan Lemon 		iowrite32(0, &reg->intr_mask);
1055773bda96SJonathan Lemon 		iowrite32(0, &reg->enable);
1056773bda96SJonathan Lemon 	}
1057773bda96SJonathan Lemon 
1058773bda96SJonathan Lemon 	return 0;
1059773bda96SJonathan Lemon }
1060773bda96SJonathan Lemon 
1061773bda96SJonathan Lemon static void
1062773bda96SJonathan Lemon ptp_ocp_unregister_ext(struct ptp_ocp_ext_src *ext)
1063773bda96SJonathan Lemon {
1064773bda96SJonathan Lemon 	ext->info->enable(ext, false);
1065773bda96SJonathan Lemon 	pci_free_irq(ext->bp->pdev, ext->irq_vec, ext);
1066773bda96SJonathan Lemon 	kfree(ext);
1067773bda96SJonathan Lemon }
1068773bda96SJonathan Lemon 
1069773bda96SJonathan Lemon static int
1070773bda96SJonathan Lemon ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r)
1071773bda96SJonathan Lemon {
1072773bda96SJonathan Lemon 	struct pci_dev *pdev = bp->pdev;
1073773bda96SJonathan Lemon 	struct ptp_ocp_ext_src *ext;
1074773bda96SJonathan Lemon 	int err;
1075773bda96SJonathan Lemon 
1076773bda96SJonathan Lemon 	ext = kzalloc(sizeof(*ext), GFP_KERNEL);
1077773bda96SJonathan Lemon 	if (!ext)
1078773bda96SJonathan Lemon 		return -ENOMEM;
1079773bda96SJonathan Lemon 
1080773bda96SJonathan Lemon 	err = -EINVAL;
1081773bda96SJonathan Lemon 	ext->mem = ptp_ocp_get_mem(bp, r);
1082773bda96SJonathan Lemon 	if (!ext->mem)
1083773bda96SJonathan Lemon 		goto out;
1084773bda96SJonathan Lemon 
1085773bda96SJonathan Lemon 	ext->bp = bp;
1086773bda96SJonathan Lemon 	ext->info = r->extra;
1087773bda96SJonathan Lemon 	ext->irq_vec = r->irq_vec;
1088773bda96SJonathan Lemon 
1089773bda96SJonathan Lemon 	err = pci_request_irq(pdev, r->irq_vec, ext->info->irq_fcn, NULL,
1090773bda96SJonathan Lemon 			      ext, "ocp%d.%s", bp->id, ext->info->name);
1091773bda96SJonathan Lemon 	if (err) {
1092773bda96SJonathan Lemon 		dev_err(&pdev->dev, "Could not get irq %d\n", r->irq_vec);
1093773bda96SJonathan Lemon 		goto out;
1094773bda96SJonathan Lemon 	}
1095773bda96SJonathan Lemon 
1096773bda96SJonathan Lemon 	bp_assign_entry(bp, r, ext);
1097773bda96SJonathan Lemon 
1098773bda96SJonathan Lemon 	return 0;
1099773bda96SJonathan Lemon 
1100773bda96SJonathan Lemon out:
1101773bda96SJonathan Lemon 	kfree(ext);
1102773bda96SJonathan Lemon 	return err;
1103773bda96SJonathan Lemon }
1104773bda96SJonathan Lemon 
1105773bda96SJonathan Lemon static int
1106773bda96SJonathan Lemon ptp_ocp_serial_line(struct ptp_ocp *bp, struct ocp_resource *r)
1107773bda96SJonathan Lemon {
1108773bda96SJonathan Lemon 	struct pci_dev *pdev = bp->pdev;
1109773bda96SJonathan Lemon 	struct uart_8250_port uart;
1110773bda96SJonathan Lemon 
1111773bda96SJonathan Lemon 	/* Setting UPF_IOREMAP and leaving port.membase unspecified lets
1112773bda96SJonathan Lemon 	 * the serial port device claim and release the pci resource.
1113773bda96SJonathan Lemon 	 */
1114773bda96SJonathan Lemon 	memset(&uart, 0, sizeof(uart));
1115773bda96SJonathan Lemon 	uart.port.dev = &pdev->dev;
1116773bda96SJonathan Lemon 	uart.port.iotype = UPIO_MEM;
1117773bda96SJonathan Lemon 	uart.port.regshift = 2;
1118773bda96SJonathan Lemon 	uart.port.mapbase = pci_resource_start(pdev, 0) + r->offset;
1119773bda96SJonathan Lemon 	uart.port.irq = pci_irq_vector(pdev, r->irq_vec);
1120773bda96SJonathan Lemon 	uart.port.uartclk = 50000000;
1121773bda96SJonathan Lemon 	uart.port.flags = UPF_FIXED_TYPE | UPF_IOREMAP;
1122773bda96SJonathan Lemon 	uart.port.type = PORT_16550A;
1123773bda96SJonathan Lemon 
1124773bda96SJonathan Lemon 	return serial8250_register_8250_port(&uart);
1125773bda96SJonathan Lemon }
1126773bda96SJonathan Lemon 
1127773bda96SJonathan Lemon static int
1128773bda96SJonathan Lemon ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r)
1129773bda96SJonathan Lemon {
1130773bda96SJonathan Lemon 	int port;
1131773bda96SJonathan Lemon 
1132773bda96SJonathan Lemon 	if (r->irq_vec > bp->n_irqs) {
1133773bda96SJonathan Lemon 		dev_err(&bp->pdev->dev, "serial device irq %d out of range\n",
1134773bda96SJonathan Lemon 			r->irq_vec);
1135773bda96SJonathan Lemon 		return 0;
1136773bda96SJonathan Lemon 	}
1137773bda96SJonathan Lemon 
1138773bda96SJonathan Lemon 	port = ptp_ocp_serial_line(bp, r);
1139773bda96SJonathan Lemon 	if (port < 0)
1140773bda96SJonathan Lemon 		return port;
1141773bda96SJonathan Lemon 
1142773bda96SJonathan Lemon 	bp_assign_entry(bp, r, port);
1143773bda96SJonathan Lemon 
1144773bda96SJonathan Lemon 	return 0;
1145773bda96SJonathan Lemon }
1146773bda96SJonathan Lemon 
1147773bda96SJonathan Lemon static int
1148773bda96SJonathan Lemon ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r)
1149773bda96SJonathan Lemon {
1150773bda96SJonathan Lemon 	void __iomem *mem;
1151773bda96SJonathan Lemon 
1152773bda96SJonathan Lemon 	mem = ptp_ocp_get_mem(bp, r);
1153773bda96SJonathan Lemon 	if (!mem)
1154773bda96SJonathan Lemon 		return -EINVAL;
1155773bda96SJonathan Lemon 
1156773bda96SJonathan Lemon 	bp_assign_entry(bp, r, mem);
1157773bda96SJonathan Lemon 
1158773bda96SJonathan Lemon 	return 0;
1159773bda96SJonathan Lemon }
1160773bda96SJonathan Lemon 
1161773bda96SJonathan Lemon /* FB specific board initializers; last "resource" registered. */
1162773bda96SJonathan Lemon static int
1163773bda96SJonathan Lemon ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r)
1164773bda96SJonathan Lemon {
1165773bda96SJonathan Lemon 	bp->flash_start = 1024 * 4096;
1166773bda96SJonathan Lemon 
1167773bda96SJonathan Lemon 	return ptp_ocp_init_clock(bp);
1168773bda96SJonathan Lemon }
1169773bda96SJonathan Lemon 
1170773bda96SJonathan Lemon static int
1171773bda96SJonathan Lemon ptp_ocp_register_resources(struct ptp_ocp *bp, kernel_ulong_t driver_data)
1172773bda96SJonathan Lemon {
1173773bda96SJonathan Lemon 	struct ocp_resource *r, *table;
1174773bda96SJonathan Lemon 	int err = 0;
1175773bda96SJonathan Lemon 
1176773bda96SJonathan Lemon 	table = (struct ocp_resource *)driver_data;
1177773bda96SJonathan Lemon 	for (r = table; r->setup; r++) {
1178773bda96SJonathan Lemon 		err = r->setup(bp, r);
1179773bda96SJonathan Lemon 		if (err)
1180773bda96SJonathan Lemon 			break;
1181773bda96SJonathan Lemon 	}
1182773bda96SJonathan Lemon 	return err;
1183773bda96SJonathan Lemon }
1184773bda96SJonathan Lemon 
1185773bda96SJonathan Lemon static ssize_t
1186773bda96SJonathan Lemon serialnum_show(struct device *dev, struct device_attribute *attr, char *buf)
1187773bda96SJonathan Lemon {
1188773bda96SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
1189773bda96SJonathan Lemon 
1190773bda96SJonathan Lemon 	if (!bp->has_serial)
1191773bda96SJonathan Lemon 		ptp_ocp_get_serial_number(bp);
1192773bda96SJonathan Lemon 
1193773bda96SJonathan Lemon 	return sysfs_emit(buf, "%pM\n", bp->serial);
1194773bda96SJonathan Lemon }
1195773bda96SJonathan Lemon static DEVICE_ATTR_RO(serialnum);
1196773bda96SJonathan Lemon 
1197773bda96SJonathan Lemon static ssize_t
1198*ef0cfb34SJonathan Lemon gnss_sync_show(struct device *dev, struct device_attribute *attr, char *buf)
1199773bda96SJonathan Lemon {
1200773bda96SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
1201773bda96SJonathan Lemon 	ssize_t ret;
1202773bda96SJonathan Lemon 
1203*ef0cfb34SJonathan Lemon 	if (bp->gnss_lost)
1204*ef0cfb34SJonathan Lemon 		ret = sysfs_emit(buf, "LOST @ %ptT\n", &bp->gnss_lost);
1205773bda96SJonathan Lemon 	else
1206773bda96SJonathan Lemon 		ret = sysfs_emit(buf, "SYNC\n");
1207773bda96SJonathan Lemon 
1208773bda96SJonathan Lemon 	return ret;
1209773bda96SJonathan Lemon }
1210*ef0cfb34SJonathan Lemon static DEVICE_ATTR_RO(gnss_sync);
1211773bda96SJonathan Lemon 
1212773bda96SJonathan Lemon static ssize_t
1213773bda96SJonathan Lemon clock_source_show(struct device *dev, struct device_attribute *attr, char *buf)
1214773bda96SJonathan Lemon {
1215773bda96SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
1216773bda96SJonathan Lemon 	const char *p;
1217773bda96SJonathan Lemon 	u32 select;
1218773bda96SJonathan Lemon 
1219773bda96SJonathan Lemon 	select = ioread32(&bp->reg->select);
1220773bda96SJonathan Lemon 	p = ptp_ocp_clock_name_from_val(select >> 16);
1221773bda96SJonathan Lemon 
1222773bda96SJonathan Lemon 	return sysfs_emit(buf, "%s\n", p);
1223773bda96SJonathan Lemon }
1224773bda96SJonathan Lemon 
1225773bda96SJonathan Lemon static ssize_t
1226773bda96SJonathan Lemon clock_source_store(struct device *dev, struct device_attribute *attr,
1227773bda96SJonathan Lemon 		   const char *buf, size_t count)
1228773bda96SJonathan Lemon {
1229773bda96SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
1230773bda96SJonathan Lemon 	unsigned long flags;
1231773bda96SJonathan Lemon 	int val;
1232773bda96SJonathan Lemon 
1233773bda96SJonathan Lemon 	val = ptp_ocp_clock_val_from_name(buf);
1234773bda96SJonathan Lemon 	if (val < 0)
1235773bda96SJonathan Lemon 		return val;
1236773bda96SJonathan Lemon 
1237773bda96SJonathan Lemon 	spin_lock_irqsave(&bp->lock, flags);
1238773bda96SJonathan Lemon 	iowrite32(val, &bp->reg->select);
1239773bda96SJonathan Lemon 	spin_unlock_irqrestore(&bp->lock, flags);
1240773bda96SJonathan Lemon 
1241773bda96SJonathan Lemon 	return count;
1242773bda96SJonathan Lemon }
1243773bda96SJonathan Lemon static DEVICE_ATTR_RW(clock_source);
1244773bda96SJonathan Lemon 
1245773bda96SJonathan Lemon static ssize_t
1246773bda96SJonathan Lemon available_clock_sources_show(struct device *dev,
1247773bda96SJonathan Lemon 			     struct device_attribute *attr, char *buf)
1248773bda96SJonathan Lemon {
1249773bda96SJonathan Lemon 	const char *clk;
1250773bda96SJonathan Lemon 	ssize_t count;
1251773bda96SJonathan Lemon 	int i;
1252773bda96SJonathan Lemon 
1253773bda96SJonathan Lemon 	count = 0;
1254773bda96SJonathan Lemon 	for (i = 0; i < ARRAY_SIZE(ptp_ocp_clock); i++) {
1255773bda96SJonathan Lemon 		clk = ptp_ocp_clock[i].name;
1256773bda96SJonathan Lemon 		count += sysfs_emit_at(buf, count, "%s ", clk);
1257773bda96SJonathan Lemon 	}
1258773bda96SJonathan Lemon 	if (count)
1259773bda96SJonathan Lemon 		count--;
1260773bda96SJonathan Lemon 	count += sysfs_emit_at(buf, count, "\n");
1261773bda96SJonathan Lemon 	return count;
1262773bda96SJonathan Lemon }
1263773bda96SJonathan Lemon static DEVICE_ATTR_RO(available_clock_sources);
1264773bda96SJonathan Lemon 
1265773bda96SJonathan Lemon static struct attribute *timecard_attrs[] = {
1266773bda96SJonathan Lemon 	&dev_attr_serialnum.attr,
1267*ef0cfb34SJonathan Lemon 	&dev_attr_gnss_sync.attr,
1268773bda96SJonathan Lemon 	&dev_attr_clock_source.attr,
1269773bda96SJonathan Lemon 	&dev_attr_available_clock_sources.attr,
1270773bda96SJonathan Lemon 	NULL,
1271773bda96SJonathan Lemon };
1272773bda96SJonathan Lemon ATTRIBUTE_GROUPS(timecard);
1273773bda96SJonathan Lemon 
1274773bda96SJonathan Lemon static void
1275773bda96SJonathan Lemon ptp_ocp_dev_release(struct device *dev)
1276773bda96SJonathan Lemon {
1277773bda96SJonathan Lemon 	struct ptp_ocp *bp = dev_get_drvdata(dev);
1278773bda96SJonathan Lemon 
1279773bda96SJonathan Lemon 	mutex_lock(&ptp_ocp_lock);
1280773bda96SJonathan Lemon 	idr_remove(&ptp_ocp_idr, bp->id);
1281773bda96SJonathan Lemon 	mutex_unlock(&ptp_ocp_lock);
1282773bda96SJonathan Lemon }
1283773bda96SJonathan Lemon 
1284773bda96SJonathan Lemon static int
1285773bda96SJonathan Lemon ptp_ocp_device_init(struct ptp_ocp *bp, struct pci_dev *pdev)
1286773bda96SJonathan Lemon {
1287773bda96SJonathan Lemon 	int err;
1288773bda96SJonathan Lemon 
1289773bda96SJonathan Lemon 	mutex_lock(&ptp_ocp_lock);
1290773bda96SJonathan Lemon 	err = idr_alloc(&ptp_ocp_idr, bp, 0, 0, GFP_KERNEL);
1291773bda96SJonathan Lemon 	mutex_unlock(&ptp_ocp_lock);
1292773bda96SJonathan Lemon 	if (err < 0) {
1293773bda96SJonathan Lemon 		dev_err(&pdev->dev, "idr_alloc failed: %d\n", err);
1294773bda96SJonathan Lemon 		return err;
1295773bda96SJonathan Lemon 	}
1296773bda96SJonathan Lemon 	bp->id = err;
1297773bda96SJonathan Lemon 
1298773bda96SJonathan Lemon 	bp->ptp_info = ptp_ocp_clock_info;
1299773bda96SJonathan Lemon 	spin_lock_init(&bp->lock);
1300*ef0cfb34SJonathan Lemon 	bp->gnss_port = -1;
1301773bda96SJonathan Lemon 	bp->mac_port = -1;
1302773bda96SJonathan Lemon 	bp->pdev = pdev;
1303773bda96SJonathan Lemon 
1304773bda96SJonathan Lemon 	device_initialize(&bp->dev);
1305773bda96SJonathan Lemon 	dev_set_name(&bp->dev, "ocp%d", bp->id);
1306773bda96SJonathan Lemon 	bp->dev.class = &timecard_class;
1307773bda96SJonathan Lemon 	bp->dev.parent = &pdev->dev;
1308773bda96SJonathan Lemon 	bp->dev.release = ptp_ocp_dev_release;
1309773bda96SJonathan Lemon 	dev_set_drvdata(&bp->dev, bp);
1310773bda96SJonathan Lemon 
1311773bda96SJonathan Lemon 	err = device_add(&bp->dev);
1312773bda96SJonathan Lemon 	if (err) {
1313773bda96SJonathan Lemon 		dev_err(&bp->dev, "device add failed: %d\n", err);
1314773bda96SJonathan Lemon 		goto out;
1315773bda96SJonathan Lemon 	}
1316773bda96SJonathan Lemon 
1317773bda96SJonathan Lemon 	pci_set_drvdata(pdev, bp);
1318773bda96SJonathan Lemon 
1319773bda96SJonathan Lemon 	return 0;
1320773bda96SJonathan Lemon 
1321773bda96SJonathan Lemon out:
1322773bda96SJonathan Lemon 	ptp_ocp_dev_release(&bp->dev);
1323d12f23faSJonathan Lemon 	put_device(&bp->dev);
1324773bda96SJonathan Lemon 	return err;
1325773bda96SJonathan Lemon }
1326773bda96SJonathan Lemon 
1327773bda96SJonathan Lemon static void
1328773bda96SJonathan Lemon ptp_ocp_symlink(struct ptp_ocp *bp, struct device *child, const char *link)
1329773bda96SJonathan Lemon {
1330773bda96SJonathan Lemon 	struct device *dev = &bp->dev;
1331773bda96SJonathan Lemon 
1332773bda96SJonathan Lemon 	if (sysfs_create_link(&dev->kobj, &child->kobj, link))
1333773bda96SJonathan Lemon 		dev_err(dev, "%s symlink failed\n", link);
1334773bda96SJonathan Lemon }
1335773bda96SJonathan Lemon 
1336773bda96SJonathan Lemon static void
1337773bda96SJonathan Lemon ptp_ocp_link_child(struct ptp_ocp *bp, const char *name, const char *link)
1338773bda96SJonathan Lemon {
1339773bda96SJonathan Lemon 	struct device *dev, *child;
1340773bda96SJonathan Lemon 
1341773bda96SJonathan Lemon 	dev = &bp->pdev->dev;
1342773bda96SJonathan Lemon 
1343773bda96SJonathan Lemon 	child = device_find_child_by_name(dev, name);
1344773bda96SJonathan Lemon 	if (!child) {
1345773bda96SJonathan Lemon 		dev_err(dev, "Could not find device %s\n", name);
1346773bda96SJonathan Lemon 		return;
1347773bda96SJonathan Lemon 	}
1348773bda96SJonathan Lemon 
1349773bda96SJonathan Lemon 	ptp_ocp_symlink(bp, child, link);
1350773bda96SJonathan Lemon 	put_device(child);
1351773bda96SJonathan Lemon }
1352773bda96SJonathan Lemon 
1353773bda96SJonathan Lemon static int
1354773bda96SJonathan Lemon ptp_ocp_complete(struct ptp_ocp *bp)
1355773bda96SJonathan Lemon {
1356773bda96SJonathan Lemon 	struct pps_device *pps;
1357773bda96SJonathan Lemon 	char buf[32];
1358773bda96SJonathan Lemon 
1359*ef0cfb34SJonathan Lemon 	if (bp->gnss_port != -1) {
1360*ef0cfb34SJonathan Lemon 		sprintf(buf, "ttyS%d", bp->gnss_port);
1361*ef0cfb34SJonathan Lemon 		ptp_ocp_link_child(bp, buf, "ttyGNSS");
1362773bda96SJonathan Lemon 	}
1363773bda96SJonathan Lemon 	if (bp->mac_port != -1) {
1364773bda96SJonathan Lemon 		sprintf(buf, "ttyS%d", bp->mac_port);
1365773bda96SJonathan Lemon 		ptp_ocp_link_child(bp, buf, "ttyMAC");
1366773bda96SJonathan Lemon 	}
1367773bda96SJonathan Lemon 	sprintf(buf, "ptp%d", ptp_clock_index(bp->ptp));
1368773bda96SJonathan Lemon 	ptp_ocp_link_child(bp, buf, "ptp");
1369773bda96SJonathan Lemon 
1370773bda96SJonathan Lemon 	pps = pps_lookup_dev(bp->ptp);
1371773bda96SJonathan Lemon 	if (pps)
1372773bda96SJonathan Lemon 		ptp_ocp_symlink(bp, pps->dev, "pps");
1373773bda96SJonathan Lemon 
1374773bda96SJonathan Lemon 	if (device_add_groups(&bp->dev, timecard_groups))
1375773bda96SJonathan Lemon 		pr_err("device add groups failed\n");
1376773bda96SJonathan Lemon 
1377773bda96SJonathan Lemon 	return 0;
1378773bda96SJonathan Lemon }
1379773bda96SJonathan Lemon 
1380773bda96SJonathan Lemon static void
1381773bda96SJonathan Lemon ptp_ocp_resource_summary(struct ptp_ocp *bp)
1382773bda96SJonathan Lemon {
1383773bda96SJonathan Lemon 	struct device *dev = &bp->pdev->dev;
1384773bda96SJonathan Lemon 
1385773bda96SJonathan Lemon 	if (bp->image) {
1386773bda96SJonathan Lemon 		u32 ver = ioread32(&bp->image->version);
1387773bda96SJonathan Lemon 
1388773bda96SJonathan Lemon 		dev_info(dev, "version %x\n", ver);
1389773bda96SJonathan Lemon 		if (ver & 0xffff)
1390773bda96SJonathan Lemon 			dev_info(dev, "regular image, version %d\n",
1391773bda96SJonathan Lemon 				 ver & 0xffff);
1392773bda96SJonathan Lemon 		else
1393773bda96SJonathan Lemon 			dev_info(dev, "golden image, version %d\n",
1394773bda96SJonathan Lemon 				 ver >> 16);
1395773bda96SJonathan Lemon 	}
1396*ef0cfb34SJonathan Lemon 	if (bp->gnss_port != -1)
1397*ef0cfb34SJonathan Lemon 		dev_info(dev, "GNSS @ /dev/ttyS%d 115200\n", bp->gnss_port);
1398773bda96SJonathan Lemon 	if (bp->mac_port != -1)
1399773bda96SJonathan Lemon 		dev_info(dev, "MAC @ /dev/ttyS%d   57600\n", bp->mac_port);
1400773bda96SJonathan Lemon }
1401773bda96SJonathan Lemon 
1402773bda96SJonathan Lemon static void
1403773bda96SJonathan Lemon ptp_ocp_detach_sysfs(struct ptp_ocp *bp)
1404773bda96SJonathan Lemon {
1405773bda96SJonathan Lemon 	struct device *dev = &bp->dev;
1406773bda96SJonathan Lemon 
1407*ef0cfb34SJonathan Lemon 	sysfs_remove_link(&dev->kobj, "ttyGNSS");
1408773bda96SJonathan Lemon 	sysfs_remove_link(&dev->kobj, "ttyMAC");
1409773bda96SJonathan Lemon 	sysfs_remove_link(&dev->kobj, "ptp");
1410773bda96SJonathan Lemon 	sysfs_remove_link(&dev->kobj, "pps");
1411773bda96SJonathan Lemon 	device_remove_groups(dev, timecard_groups);
1412773bda96SJonathan Lemon }
1413773bda96SJonathan Lemon 
1414773bda96SJonathan Lemon static void
1415773bda96SJonathan Lemon ptp_ocp_detach(struct ptp_ocp *bp)
1416773bda96SJonathan Lemon {
1417773bda96SJonathan Lemon 	ptp_ocp_detach_sysfs(bp);
1418773bda96SJonathan Lemon 	if (timer_pending(&bp->watchdog))
1419773bda96SJonathan Lemon 		del_timer_sync(&bp->watchdog);
1420773bda96SJonathan Lemon 	if (bp->ts0)
1421773bda96SJonathan Lemon 		ptp_ocp_unregister_ext(bp->ts0);
1422773bda96SJonathan Lemon 	if (bp->ts1)
1423773bda96SJonathan Lemon 		ptp_ocp_unregister_ext(bp->ts1);
1424773bda96SJonathan Lemon 	if (bp->pps)
1425773bda96SJonathan Lemon 		ptp_ocp_unregister_ext(bp->pps);
1426*ef0cfb34SJonathan Lemon 	if (bp->gnss_port != -1)
1427*ef0cfb34SJonathan Lemon 		serial8250_unregister_port(bp->gnss_port);
1428773bda96SJonathan Lemon 	if (bp->mac_port != -1)
1429773bda96SJonathan Lemon 		serial8250_unregister_port(bp->mac_port);
1430773bda96SJonathan Lemon 	if (bp->spi_flash)
1431773bda96SJonathan Lemon 		platform_device_unregister(bp->spi_flash);
1432773bda96SJonathan Lemon 	if (bp->i2c_ctrl)
1433773bda96SJonathan Lemon 		platform_device_unregister(bp->i2c_ctrl);
1434773bda96SJonathan Lemon 	if (bp->i2c_clk)
1435773bda96SJonathan Lemon 		clk_hw_unregister_fixed_rate(bp->i2c_clk);
1436773bda96SJonathan Lemon 	if (bp->n_irqs)
1437773bda96SJonathan Lemon 		pci_free_irq_vectors(bp->pdev);
1438773bda96SJonathan Lemon 	if (bp->ptp)
1439773bda96SJonathan Lemon 		ptp_clock_unregister(bp->ptp);
1440773bda96SJonathan Lemon 	device_unregister(&bp->dev);
1441773bda96SJonathan Lemon }
1442773bda96SJonathan Lemon 
1443a7e1abadSJonathan Lemon static int
1444a7e1abadSJonathan Lemon ptp_ocp_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1445a7e1abadSJonathan Lemon {
1446773bda96SJonathan Lemon 	struct devlink *devlink;
1447a7e1abadSJonathan Lemon 	struct ptp_ocp *bp;
1448a7e1abadSJonathan Lemon 	int err;
1449a7e1abadSJonathan Lemon 
1450773bda96SJonathan Lemon 	devlink = devlink_alloc(&ptp_ocp_devlink_ops, sizeof(*bp));
1451773bda96SJonathan Lemon 	if (!devlink) {
1452773bda96SJonathan Lemon 		dev_err(&pdev->dev, "devlink_alloc failed\n");
1453a7e1abadSJonathan Lemon 		return -ENOMEM;
1454773bda96SJonathan Lemon 	}
1455773bda96SJonathan Lemon 
1456773bda96SJonathan Lemon 	err = ptp_ocp_devlink_register(devlink, &pdev->dev);
1457773bda96SJonathan Lemon 	if (err)
1458773bda96SJonathan Lemon 		goto out_free;
1459a7e1abadSJonathan Lemon 
1460a7e1abadSJonathan Lemon 	err = pci_enable_device(pdev);
1461a7e1abadSJonathan Lemon 	if (err) {
1462a7e1abadSJonathan Lemon 		dev_err(&pdev->dev, "pci_enable_device\n");
1463773bda96SJonathan Lemon 		goto out_unregister;
1464a7e1abadSJonathan Lemon 	}
1465a7e1abadSJonathan Lemon 
1466773bda96SJonathan Lemon 	bp = devlink_priv(devlink);
1467773bda96SJonathan Lemon 	err = ptp_ocp_device_init(bp, pdev);
1468773bda96SJonathan Lemon 	if (err)
1469773bda96SJonathan Lemon 		goto out_unregister;
1470a7e1abadSJonathan Lemon 
1471773bda96SJonathan Lemon 	/* compat mode.
1472773bda96SJonathan Lemon 	 * Older FPGA firmware only returns 2 irq's.
1473773bda96SJonathan Lemon 	 * allow this - if not all of the IRQ's are returned, skip the
1474773bda96SJonathan Lemon 	 * extra devices and just register the clock.
1475773bda96SJonathan Lemon 	 */
1476773bda96SJonathan Lemon 	err = pci_alloc_irq_vectors(pdev, 1, 10, PCI_IRQ_MSI | PCI_IRQ_MSIX);
1477773bda96SJonathan Lemon 	if (err < 0) {
1478773bda96SJonathan Lemon 		dev_err(&pdev->dev, "alloc_irq_vectors err: %d\n", err);
1479773bda96SJonathan Lemon 		goto out;
1480a7e1abadSJonathan Lemon 	}
1481773bda96SJonathan Lemon 	bp->n_irqs = err;
1482773bda96SJonathan Lemon 	pci_set_master(pdev);
1483a7e1abadSJonathan Lemon 
1484773bda96SJonathan Lemon 	err = ptp_ocp_register_resources(bp, id->driver_data);
1485a7e1abadSJonathan Lemon 	if (err)
1486a7e1abadSJonathan Lemon 		goto out;
1487a7e1abadSJonathan Lemon 
1488a7e1abadSJonathan Lemon 	bp->ptp = ptp_clock_register(&bp->ptp_info, &pdev->dev);
1489a7e1abadSJonathan Lemon 	if (IS_ERR(bp->ptp)) {
1490a7e1abadSJonathan Lemon 		err = PTR_ERR(bp->ptp);
1491773bda96SJonathan Lemon 		dev_err(&pdev->dev, "ptp_clock_register: %d\n", err);
1492773bda96SJonathan Lemon 		bp->ptp = NULL;
1493a7e1abadSJonathan Lemon 		goto out;
1494a7e1abadSJonathan Lemon 	}
1495a7e1abadSJonathan Lemon 
1496773bda96SJonathan Lemon 	err = ptp_ocp_complete(bp);
1497773bda96SJonathan Lemon 	if (err)
1498773bda96SJonathan Lemon 		goto out;
1499773bda96SJonathan Lemon 
1500a7e1abadSJonathan Lemon 	ptp_ocp_info(bp);
1501773bda96SJonathan Lemon 	ptp_ocp_resource_summary(bp);
1502a7e1abadSJonathan Lemon 
1503a7e1abadSJonathan Lemon 	return 0;
1504a7e1abadSJonathan Lemon 
1505a7e1abadSJonathan Lemon out:
1506773bda96SJonathan Lemon 	ptp_ocp_detach(bp);
1507a7e1abadSJonathan Lemon 	pci_disable_device(pdev);
1508773bda96SJonathan Lemon 	pci_set_drvdata(pdev, NULL);
1509773bda96SJonathan Lemon out_unregister:
1510773bda96SJonathan Lemon 	ptp_ocp_devlink_unregister(devlink);
1511a7e1abadSJonathan Lemon out_free:
1512773bda96SJonathan Lemon 	devlink_free(devlink);
1513a7e1abadSJonathan Lemon 
1514a7e1abadSJonathan Lemon 	return err;
1515a7e1abadSJonathan Lemon }
1516a7e1abadSJonathan Lemon 
1517a7e1abadSJonathan Lemon static void
1518a7e1abadSJonathan Lemon ptp_ocp_remove(struct pci_dev *pdev)
1519a7e1abadSJonathan Lemon {
1520a7e1abadSJonathan Lemon 	struct ptp_ocp *bp = pci_get_drvdata(pdev);
1521773bda96SJonathan Lemon 	struct devlink *devlink = priv_to_devlink(bp);
1522a7e1abadSJonathan Lemon 
1523773bda96SJonathan Lemon 	ptp_ocp_detach(bp);
1524a7e1abadSJonathan Lemon 	pci_disable_device(pdev);
1525a7e1abadSJonathan Lemon 	pci_set_drvdata(pdev, NULL);
1526773bda96SJonathan Lemon 
1527773bda96SJonathan Lemon 	ptp_ocp_devlink_unregister(devlink);
1528773bda96SJonathan Lemon 	devlink_free(devlink);
1529a7e1abadSJonathan Lemon }
1530a7e1abadSJonathan Lemon 
1531a7e1abadSJonathan Lemon static struct pci_driver ptp_ocp_driver = {
1532a7e1abadSJonathan Lemon 	.name		= KBUILD_MODNAME,
1533a7e1abadSJonathan Lemon 	.id_table	= ptp_ocp_pcidev_id,
1534a7e1abadSJonathan Lemon 	.probe		= ptp_ocp_probe,
1535a7e1abadSJonathan Lemon 	.remove		= ptp_ocp_remove,
1536a7e1abadSJonathan Lemon };
1537a7e1abadSJonathan Lemon 
1538773bda96SJonathan Lemon static int
1539773bda96SJonathan Lemon ptp_ocp_i2c_notifier_call(struct notifier_block *nb,
1540773bda96SJonathan Lemon 			  unsigned long action, void *data)
1541773bda96SJonathan Lemon {
1542773bda96SJonathan Lemon 	struct device *dev, *child = data;
1543773bda96SJonathan Lemon 	struct ptp_ocp *bp;
1544773bda96SJonathan Lemon 	bool add;
1545773bda96SJonathan Lemon 
1546773bda96SJonathan Lemon 	switch (action) {
1547773bda96SJonathan Lemon 	case BUS_NOTIFY_ADD_DEVICE:
1548773bda96SJonathan Lemon 	case BUS_NOTIFY_DEL_DEVICE:
1549773bda96SJonathan Lemon 		add = action == BUS_NOTIFY_ADD_DEVICE;
1550773bda96SJonathan Lemon 		break;
1551773bda96SJonathan Lemon 	default:
1552773bda96SJonathan Lemon 		return 0;
1553773bda96SJonathan Lemon 	}
1554773bda96SJonathan Lemon 
1555773bda96SJonathan Lemon 	if (!i2c_verify_adapter(child))
1556773bda96SJonathan Lemon 		return 0;
1557773bda96SJonathan Lemon 
1558773bda96SJonathan Lemon 	dev = child;
1559773bda96SJonathan Lemon 	while ((dev = dev->parent))
1560773bda96SJonathan Lemon 		if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME))
1561773bda96SJonathan Lemon 			goto found;
1562773bda96SJonathan Lemon 	return 0;
1563773bda96SJonathan Lemon 
1564773bda96SJonathan Lemon found:
1565773bda96SJonathan Lemon 	bp = dev_get_drvdata(dev);
1566773bda96SJonathan Lemon 	if (add)
1567773bda96SJonathan Lemon 		ptp_ocp_symlink(bp, child, "i2c");
1568773bda96SJonathan Lemon 	else
1569773bda96SJonathan Lemon 		sysfs_remove_link(&bp->dev.kobj, "i2c");
1570773bda96SJonathan Lemon 
1571773bda96SJonathan Lemon 	return 0;
1572773bda96SJonathan Lemon }
1573773bda96SJonathan Lemon 
1574773bda96SJonathan Lemon static struct notifier_block ptp_ocp_i2c_notifier = {
1575773bda96SJonathan Lemon 	.notifier_call = ptp_ocp_i2c_notifier_call,
1576773bda96SJonathan Lemon };
1577773bda96SJonathan Lemon 
1578a7e1abadSJonathan Lemon static int __init
1579a7e1abadSJonathan Lemon ptp_ocp_init(void)
1580a7e1abadSJonathan Lemon {
1581773bda96SJonathan Lemon 	const char *what;
1582a7e1abadSJonathan Lemon 	int err;
1583a7e1abadSJonathan Lemon 
1584773bda96SJonathan Lemon 	what = "timecard class";
1585773bda96SJonathan Lemon 	err = class_register(&timecard_class);
1586773bda96SJonathan Lemon 	if (err)
1587773bda96SJonathan Lemon 		goto out;
1588773bda96SJonathan Lemon 
1589773bda96SJonathan Lemon 	what = "i2c notifier";
1590773bda96SJonathan Lemon 	err = bus_register_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier);
1591773bda96SJonathan Lemon 	if (err)
1592773bda96SJonathan Lemon 		goto out_notifier;
1593773bda96SJonathan Lemon 
1594773bda96SJonathan Lemon 	what = "ptp_ocp driver";
1595a7e1abadSJonathan Lemon 	err = pci_register_driver(&ptp_ocp_driver);
1596773bda96SJonathan Lemon 	if (err)
1597773bda96SJonathan Lemon 		goto out_register;
1598773bda96SJonathan Lemon 
1599773bda96SJonathan Lemon 	return 0;
1600773bda96SJonathan Lemon 
1601773bda96SJonathan Lemon out_register:
1602773bda96SJonathan Lemon 	bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier);
1603773bda96SJonathan Lemon out_notifier:
1604773bda96SJonathan Lemon 	class_unregister(&timecard_class);
1605773bda96SJonathan Lemon out:
1606773bda96SJonathan Lemon 	pr_err(KBUILD_MODNAME ": failed to register %s: %d\n", what, err);
1607a7e1abadSJonathan Lemon 	return err;
1608a7e1abadSJonathan Lemon }
1609a7e1abadSJonathan Lemon 
1610a7e1abadSJonathan Lemon static void __exit
1611a7e1abadSJonathan Lemon ptp_ocp_fini(void)
1612a7e1abadSJonathan Lemon {
1613773bda96SJonathan Lemon 	bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier);
1614a7e1abadSJonathan Lemon 	pci_unregister_driver(&ptp_ocp_driver);
1615773bda96SJonathan Lemon 	class_unregister(&timecard_class);
1616a7e1abadSJonathan Lemon }
1617a7e1abadSJonathan Lemon 
1618a7e1abadSJonathan Lemon module_init(ptp_ocp_init);
1619a7e1abadSJonathan Lemon module_exit(ptp_ocp_fini);
1620a7e1abadSJonathan Lemon 
1621a7e1abadSJonathan Lemon MODULE_DESCRIPTION("OpenCompute TimeCard driver");
1622a7e1abadSJonathan Lemon MODULE_LICENSE("GPL v2");
1623