ptp_ocp.c (4587369b6cba3772b6e92ec3d28854ec7d82a95d) ptp_ocp.c (0cfcdd1ebcfe1a9b262f6ad8419580720dc843c4)
1// SPDX-License-Identifier: GPL-2.0-only
2/* Copyright (c) 2020 Facebook */
3
4#include <linux/err.h>
5#include <linux/kernel.h>
6#include <linux/module.h>
7#include <linux/debugfs.h>
8#include <linux/init.h>
9#include <linux/pci.h>
10#include <linux/serial_8250.h>
11#include <linux/clkdev.h>
12#include <linux/clk-provider.h>
13#include <linux/platform_device.h>
1// SPDX-License-Identifier: GPL-2.0-only
2/* Copyright (c) 2020 Facebook */
3
4#include <linux/err.h>
5#include <linux/kernel.h>
6#include <linux/module.h>
7#include <linux/debugfs.h>
8#include <linux/init.h>
9#include <linux/pci.h>
10#include <linux/serial_8250.h>
11#include <linux/clkdev.h>
12#include <linux/clk-provider.h>
13#include <linux/platform_device.h>
14#include <linux/platform_data/i2c-xiic.h>
14#include <linux/ptp_clock_kernel.h>
15#include <linux/spi/spi.h>
16#include <linux/spi/xilinx_spi.h>
17#include <net/devlink.h>
18#include <linux/i2c.h>
19#include <linux/mtd/mtd.h>
15#include <linux/ptp_clock_kernel.h>
16#include <linux/spi/spi.h>
17#include <linux/spi/xilinx_spi.h>
18#include <net/devlink.h>
19#include <linux/i2c.h>
20#include <linux/mtd/mtd.h>
21#include <linux/nvmem-consumer.h>
20
21#ifndef PCI_VENDOR_ID_FACEBOOK
22#define PCI_VENDOR_ID_FACEBOOK 0x1d9b
23#endif
24
25#ifndef PCI_DEVICE_ID_FACEBOOK_TIMECARD
26#define PCI_DEVICE_ID_FACEBOOK_TIMECARD 0x0400
27#endif

--- 171 unchanged lines hidden (view full) ---

199
200struct ptp_ocp_ext_src {
201 void __iomem *mem;
202 struct ptp_ocp *bp;
203 struct ptp_ocp_ext_info *info;
204 int irq_vec;
205};
206
22
23#ifndef PCI_VENDOR_ID_FACEBOOK
24#define PCI_VENDOR_ID_FACEBOOK 0x1d9b
25#endif
26
27#ifndef PCI_DEVICE_ID_FACEBOOK_TIMECARD
28#define PCI_DEVICE_ID_FACEBOOK_TIMECARD 0x0400
29#endif

--- 171 unchanged lines hidden (view full) ---

201
202struct ptp_ocp_ext_src {
203 void __iomem *mem;
204 struct ptp_ocp *bp;
205 struct ptp_ocp_ext_info *info;
206 int irq_vec;
207};
208
209#define OCP_BOARD_ID_LEN 13
210#define OCP_SERIAL_LEN 6
211
207struct ptp_ocp {
208 struct pci_dev *pdev;
209 struct device dev;
210 spinlock_t lock;
211 struct ocp_reg __iomem *reg;
212 struct tod_reg __iomem *tod;
213 struct pps_reg __iomem *pps_to_ext;
214 struct pps_reg __iomem *pps_to_clk;

--- 10 unchanged lines hidden (view full) ---

225 struct ptp_ocp_ext_src *ts2;
226 struct img_reg __iomem *image;
227 struct ptp_clock *ptp;
228 struct ptp_clock_info ptp_info;
229 struct platform_device *i2c_ctrl;
230 struct platform_device *spi_flash;
231 struct clk_hw *i2c_clk;
232 struct timer_list watchdog;
212struct ptp_ocp {
213 struct pci_dev *pdev;
214 struct device dev;
215 spinlock_t lock;
216 struct ocp_reg __iomem *reg;
217 struct tod_reg __iomem *tod;
218 struct pps_reg __iomem *pps_to_ext;
219 struct pps_reg __iomem *pps_to_clk;

--- 10 unchanged lines hidden (view full) ---

230 struct ptp_ocp_ext_src *ts2;
231 struct img_reg __iomem *image;
232 struct ptp_clock *ptp;
233 struct ptp_clock_info ptp_info;
234 struct platform_device *i2c_ctrl;
235 struct platform_device *spi_flash;
236 struct clk_hw *i2c_clk;
237 struct timer_list watchdog;
238 const struct ptp_ocp_eeprom_map *eeprom_map;
233 struct dentry *debug_root;
234 time64_t gnss_lost;
235 int id;
236 int n_irqs;
237 int gnss_port;
238 int gnss2_port;
239 int mac_port; /* miniature atomic clock */
240 int nmea_port;
239 struct dentry *debug_root;
240 time64_t gnss_lost;
241 int id;
242 int n_irqs;
243 int gnss_port;
244 int gnss2_port;
245 int mac_port; /* miniature atomic clock */
246 int nmea_port;
241 u8 serial[6];
242 bool has_serial;
247 u8 board_id[OCP_BOARD_ID_LEN];
248 u8 serial[OCP_SERIAL_LEN];
249 bool has_eeprom_data;
243 u32 pps_req_map;
244 int flash_start;
245 u32 utc_tai_offset;
246 u32 ts_window_adjust;
247};
248
249#define OCP_REQ_TIMESTAMP BIT(0)
250#define OCP_REQ_PPS BIT(1)

--- 12 unchanged lines hidden (view full) ---

263static int ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r);
264static int ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r);
265static int ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r);
266static int ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r);
267static int ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r);
268static irqreturn_t ptp_ocp_ts_irq(int irq, void *priv);
269static int ptp_ocp_ts_enable(void *priv, u32 req, bool enable);
270
250 u32 pps_req_map;
251 int flash_start;
252 u32 utc_tai_offset;
253 u32 ts_window_adjust;
254};
255
256#define OCP_REQ_TIMESTAMP BIT(0)
257#define OCP_REQ_PPS BIT(1)

--- 12 unchanged lines hidden (view full) ---

270static int ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r);
271static int ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r);
272static int ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r);
273static int ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r);
274static int ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r);
275static irqreturn_t ptp_ocp_ts_irq(int irq, void *priv);
276static int ptp_ocp_ts_enable(void *priv, u32 req, bool enable);
277
278struct ptp_ocp_eeprom_map {
279 u16 off;
280 u16 len;
281 u32 bp_offset;
282 const void * const tag;
283};
284
285#define EEPROM_ENTRY(addr, member) \
286 .off = addr, \
287 .len = sizeof_field(struct ptp_ocp, member), \
288 .bp_offset = offsetof(struct ptp_ocp, member)
289
290#define BP_MAP_ENTRY_ADDR(bp, map) ({ \
291 (void *)((uintptr_t)(bp) + (map)->bp_offset); \
292})
293
294static struct ptp_ocp_eeprom_map fb_eeprom_map[] = {
295 { EEPROM_ENTRY(0x43, board_id) },
296 { EEPROM_ENTRY(0x00, serial), .tag = "mac" },
297 { }
298};
299
271#define bp_assign_entry(bp, res, val) ({ \
272 uintptr_t addr = (uintptr_t)(bp) + (res)->bp_offset; \
273 *(typeof(val) *)addr = val; \
274})
275
276#define OCP_RES_LOCATION(member) \
277 .name = #member, .bp_offset = offsetof(struct ptp_ocp, member)
278

--- 112 unchanged lines hidden (view full) ---

391 .offset = 0x00140000, .size = 0x1000,
392 },
393 {
394 OCP_I2C_RESOURCE(i2c_ctrl),
395 .offset = 0x00150000, .size = 0x10000, .irq_vec = 7,
396 .extra = &(struct ptp_ocp_i2c_info) {
397 .name = "xiic-i2c",
398 .fixed_rate = 50000000,
300#define bp_assign_entry(bp, res, val) ({ \
301 uintptr_t addr = (uintptr_t)(bp) + (res)->bp_offset; \
302 *(typeof(val) *)addr = val; \
303})
304
305#define OCP_RES_LOCATION(member) \
306 .name = #member, .bp_offset = offsetof(struct ptp_ocp, member)
307

--- 112 unchanged lines hidden (view full) ---

420 .offset = 0x00140000, .size = 0x1000,
421 },
422 {
423 OCP_I2C_RESOURCE(i2c_ctrl),
424 .offset = 0x00150000, .size = 0x10000, .irq_vec = 7,
425 .extra = &(struct ptp_ocp_i2c_info) {
426 .name = "xiic-i2c",
427 .fixed_rate = 50000000,
428 .data_size = sizeof(struct xiic_i2c_platform_data),
429 .data = &(struct xiic_i2c_platform_data) {
430 .num_devices = 2,
431 .devices = (struct i2c_board_info[]) {
432 { I2C_BOARD_INFO("24c02", 0x50) },
433 { I2C_BOARD_INFO("24mac402", 0x58),
434 .platform_data = "mac" },
435 },
436 },
399 },
400 },
401 {
402 OCP_SERIAL_RESOURCE(gnss_port),
403 .offset = 0x00160000 + 0x1000, .irq_vec = 3,
404 },
405 {
406 OCP_SERIAL_RESOURCE(gnss2_port),

--- 507 unchanged lines hidden (view full) ---

914 "ALL", "COMBINED", "GPS", "GLONASS", "GALILEO", "BEIDOU",
915 "Unknown"
916 };
917 if (idx >= ARRAY_SIZE(gnss_name))
918 idx = ARRAY_SIZE(gnss_name) - 1;
919 return gnss_name[idx];
920}
921
437 },
438 },
439 {
440 OCP_SERIAL_RESOURCE(gnss_port),
441 .offset = 0x00160000 + 0x1000, .irq_vec = 3,
442 },
443 {
444 OCP_SERIAL_RESOURCE(gnss2_port),

--- 507 unchanged lines hidden (view full) ---

952 "ALL", "COMBINED", "GPS", "GLONASS", "GALILEO", "BEIDOU",
953 "Unknown"
954 };
955 if (idx >= ARRAY_SIZE(gnss_name))
956 idx = ARRAY_SIZE(gnss_name) - 1;
957 return gnss_name[idx];
958}
959
960struct ptp_ocp_nvmem_match_info {
961 struct ptp_ocp *bp;
962 const void * const tag;
963};
964
922static int
965static int
923ptp_ocp_firstchild(struct device *dev, void *data)
966ptp_ocp_nvmem_match(struct device *dev, const void *data)
924{
967{
925 return 1;
968 const struct ptp_ocp_nvmem_match_info *info = data;
969
970 dev = dev->parent;
971 if (!i2c_verify_client(dev) || info->tag != dev->platform_data)
972 return 0;
973
974 while ((dev = dev->parent))
975 if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME))
976 return info->bp == dev_get_drvdata(dev);
977 return 0;
926}
927
978}
979
928static int
929ptp_ocp_read_i2c(struct i2c_adapter *adap, u8 addr, u8 reg, u8 sz, u8 *data)
980static inline struct nvmem_device *
981ptp_ocp_nvmem_device_get(struct ptp_ocp *bp, const void * const tag)
930{
982{
931 struct i2c_msg msgs[2] = {
932 {
933 .addr = addr,
934 .len = 1,
935 .buf = &reg,
936 },
937 {
938 .addr = addr,
939 .flags = I2C_M_RD,
940 .len = 2,
941 .buf = data,
942 },
943 };
944 int err;
945 u8 len;
983 struct ptp_ocp_nvmem_match_info info = { .bp = bp, .tag = tag };
946
984
947 /* xiic-i2c for some stupid reason only does 2 byte reads. */
948 while (sz) {
949 len = min_t(u8, sz, 2);
950 msgs[1].len = len;
951 err = i2c_transfer(adap, msgs, 2);
952 if (err != msgs[1].len)
953 return err;
954 msgs[1].buf += len;
955 reg += len;
956 sz -= len;
985 return nvmem_device_find(&info, ptp_ocp_nvmem_match);
986}
987
988static inline void
989ptp_ocp_nvmem_device_put(struct nvmem_device **nvmemp)
990{
991 if (*nvmemp != NULL) {
992 nvmem_device_put(*nvmemp);
993 *nvmemp = NULL;
957 }
994 }
958 return 0;
959}
960
961static void
995}
996
997static void
962ptp_ocp_get_serial_number(struct ptp_ocp *bp)
998ptp_ocp_read_eeprom(struct ptp_ocp *bp)
963{
999{
964 struct i2c_adapter *adap;
965 struct device *dev;
966 int err;
1000 const struct ptp_ocp_eeprom_map *map;
1001 struct nvmem_device *nvmem;
1002 const void *tag;
1003 int ret;
967
968 if (!bp->i2c_ctrl)
969 return;
970
1004
1005 if (!bp->i2c_ctrl)
1006 return;
1007
971 dev = device_find_child(&bp->i2c_ctrl->dev, NULL, ptp_ocp_firstchild);
972 if (!dev) {
973 dev_err(&bp->pdev->dev, "Can't find I2C adapter\n");
974 return;
975 }
1008 tag = NULL;
1009 nvmem = NULL;
976
1010
977 adap = i2c_verify_adapter(dev);
978 if (!adap) {
979 dev_err(&bp->pdev->dev, "device '%s' isn't an I2C adapter\n",
980 dev_name(dev));
981 goto out;
1011 for (map = bp->eeprom_map; map->len; map++) {
1012 if (map->tag != tag) {
1013 tag = map->tag;
1014 ptp_ocp_nvmem_device_put(&nvmem);
1015 }
1016 if (!nvmem) {
1017 nvmem = ptp_ocp_nvmem_device_get(bp, tag);
1018 if (!nvmem)
1019 goto out;
1020 }
1021 ret = nvmem_device_read(nvmem, map->off, map->len,
1022 BP_MAP_ENTRY_ADDR(bp, map));
1023 if (ret != map->len)
1024 goto read_fail;
982 }
983
1025 }
1026
984 err = ptp_ocp_read_i2c(adap, 0x58, 0x9A, 6, bp->serial);
985 if (err) {
986 dev_err(&bp->pdev->dev, "could not read eeprom: %d\n", err);
987 goto out;
988 }
1027 bp->has_eeprom_data = true;
989
1028
990 bp->has_serial = true;
991
992out:
1029out:
993 put_device(dev);
1030 ptp_ocp_nvmem_device_put(&nvmem);
1031 return;
1032
1033read_fail:
1034 dev_err(&bp->pdev->dev, "could not read eeprom: %d\n", ret);
1035 goto out;
994}
995
1036}
1037
1038static int
1039ptp_ocp_firstchild(struct device *dev, void *data)
1040{
1041 return 1;
1042}
1043
996static struct device *
997ptp_ocp_find_flash(struct ptp_ocp *bp)
998{
999 struct device *dev, *last;
1000
1001 last = NULL;
1002 dev = &bp->spi_flash->dev;
1003

--- 100 unchanged lines hidden (view full) ---

1104 err = devlink_info_version_running_put(req,
1105 "loader",
1106 buf);
1107 }
1108 if (err)
1109 return err;
1110 }
1111
1044static struct device *
1045ptp_ocp_find_flash(struct ptp_ocp *bp)
1046{
1047 struct device *dev, *last;
1048
1049 last = NULL;
1050 dev = &bp->spi_flash->dev;
1051

--- 100 unchanged lines hidden (view full) ---

1152 err = devlink_info_version_running_put(req,
1153 "loader",
1154 buf);
1155 }
1156 if (err)
1157 return err;
1158 }
1159
1112 if (!bp->has_serial)
1113 ptp_ocp_get_serial_number(bp);
1114
1115 if (bp->has_serial) {
1116 sprintf(buf, "%pM", bp->serial);
1117 err = devlink_info_serial_number_put(req, buf);
1118 if (err)
1119 return err;
1160 if (!bp->has_eeprom_data) {
1161 ptp_ocp_read_eeprom(bp);
1162 if (!bp->has_eeprom_data)
1163 return 0;
1120 }
1121
1164 }
1165
1166 sprintf(buf, "%pM", bp->serial);
1167 err = devlink_info_serial_number_put(req, buf);
1168 if (err)
1169 return err;
1170
1171 err = devlink_info_version_fixed_put(req,
1172 DEVLINK_INFO_VERSION_GENERIC_BOARD_ID,
1173 bp->board_id);
1174 if (err)
1175 return err;
1176
1122 return 0;
1123}
1124
1125static const struct devlink_ops ptp_ocp_devlink_ops = {
1126 .flash_update = ptp_ocp_devlink_flash_update,
1127 .info_get = ptp_ocp_devlink_info_get,
1128};
1129

--- 277 unchanged lines hidden (view full) ---

1407 iowrite32(1, &bp->nmea_out->ctrl); /* enable */
1408}
1409
1410/* FB specific board initializers; last "resource" registered. */
1411static int
1412ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r)
1413{
1414 bp->flash_start = 1024 * 4096;
1177 return 0;
1178}
1179
1180static const struct devlink_ops ptp_ocp_devlink_ops = {
1181 .flash_update = ptp_ocp_devlink_flash_update,
1182 .info_get = ptp_ocp_devlink_info_get,
1183};
1184

--- 277 unchanged lines hidden (view full) ---

1462 iowrite32(1, &bp->nmea_out->ctrl); /* enable */
1463}
1464
1465/* FB specific board initializers; last "resource" registered. */
1466static int
1467ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r)
1468{
1469 bp->flash_start = 1024 * 4096;
1470 bp->eeprom_map = fb_eeprom_map;
1415
1416 ptp_ocp_tod_init(bp);
1417 ptp_ocp_nmea_out_init(bp);
1418
1419 return ptp_ocp_init_clock(bp);
1420}
1421
1422static bool

--- 382 unchanged lines hidden (view full) ---

1805}
1806static DEVICE_ATTR_RO(available_sma_outputs);
1807
1808static ssize_t
1809serialnum_show(struct device *dev, struct device_attribute *attr, char *buf)
1810{
1811 struct ptp_ocp *bp = dev_get_drvdata(dev);
1812
1471
1472 ptp_ocp_tod_init(bp);
1473 ptp_ocp_nmea_out_init(bp);
1474
1475 return ptp_ocp_init_clock(bp);
1476}
1477
1478static bool

--- 382 unchanged lines hidden (view full) ---

1861}
1862static DEVICE_ATTR_RO(available_sma_outputs);
1863
1864static ssize_t
1865serialnum_show(struct device *dev, struct device_attribute *attr, char *buf)
1866{
1867 struct ptp_ocp *bp = dev_get_drvdata(dev);
1868
1813 if (!bp->has_serial)
1814 ptp_ocp_get_serial_number(bp);
1869 if (!bp->has_eeprom_data)
1870 ptp_ocp_read_eeprom(bp);
1815
1816 return sysfs_emit(buf, "%pM\n", bp->serial);
1817}
1818static DEVICE_ATTR_RO(serialnum);
1819
1820static ssize_t
1821gnss_sync_show(struct device *dev, struct device_attribute *attr, char *buf)
1822{

--- 949 unchanged lines hidden ---
1871
1872 return sysfs_emit(buf, "%pM\n", bp->serial);
1873}
1874static DEVICE_ATTR_RO(serialnum);
1875
1876static ssize_t
1877gnss_sync_show(struct device *dev, struct device_attribute *attr, char *buf)
1878{

--- 949 unchanged lines hidden ---