fmc.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (C) 2012 CERN (www.cern.ch)
  3. * Author: Alessandro Rubini <rubini@gnudd.com>
  4. *
  5. * Released according to the GNU GPL, version 2 or any later version.
  6. *
  7. * This work is part of the White Rabbit project, a research effort led
  8. * by CERN, the European Institute for Nuclear Research.
  9. */
  10. #ifndef __LINUX_FMC_H__
  11. #define __LINUX_FMC_H__
  12. #include <linux/types.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/device.h>
  15. #include <linux/list.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/io.h>
  18. struct fmc_device;
  19. struct fmc_driver;
  20. /*
  21. * This bus abstraction is developed separately from drivers, so we need
  22. * to check the version of the data structures we receive.
  23. */
  24. #define FMC_MAJOR 3
  25. #define FMC_MINOR 0
  26. #define FMC_VERSION ((FMC_MAJOR << 16) | FMC_MINOR)
  27. #define __FMC_MAJOR(x) ((x) >> 16)
  28. #define __FMC_MINOR(x) ((x) & 0xffff)
  29. /*
  30. * The device identification, as defined by the IPMI FRU (Field Replaceable
  31. * Unit) includes four different strings to describe the device. Here we
  32. * only match the "Board Manufacturer" and the "Board Product Name",
  33. * ignoring the "Board Serial Number" and "Board Part Number". All 4 are
  34. * expected to be strings, so they are treated as zero-terminated C strings.
  35. * Unspecified string (NULL) means "any", so if both are unspecified this
  36. * is a catch-all driver. So null entries are allowed and we use array
  37. * and length. This is unlike pci and usb that use null-terminated arrays
  38. */
  39. struct fmc_fru_id {
  40. char *manufacturer;
  41. char *product_name;
  42. };
  43. /*
  44. * If the FPGA is already programmed (think Etherbone or the second
  45. * SVEC slot), we can match on SDB devices in the memory image. This
  46. * match uses an array of devices that must all be present, and the
  47. * match is based on vendor and device only. Further checks are expected
  48. * to happen in the probe function. Zero means "any" and catch-all is allowed.
  49. */
  50. struct fmc_sdb_one_id {
  51. uint64_t vendor;
  52. uint32_t device;
  53. };
  54. struct fmc_sdb_id {
  55. struct fmc_sdb_one_id *cores;
  56. int cores_nr;
  57. };
  58. struct fmc_device_id {
  59. struct fmc_fru_id *fru_id;
  60. int fru_id_nr;
  61. struct fmc_sdb_id *sdb_id;
  62. int sdb_id_nr;
  63. };
  64. /* This sizes the module_param_array used by generic module parameters */
  65. #define FMC_MAX_CARDS 32
  66. /* The driver is a pretty simple thing */
  67. struct fmc_driver {
  68. unsigned long version;
  69. struct device_driver driver;
  70. int (*probe)(struct fmc_device *);
  71. int (*remove)(struct fmc_device *);
  72. const struct fmc_device_id id_table;
  73. /* What follows is for generic module parameters */
  74. int busid_n;
  75. int busid_val[FMC_MAX_CARDS];
  76. int gw_n;
  77. char *gw_val[FMC_MAX_CARDS];
  78. };
  79. #define to_fmc_driver(x) container_of((x), struct fmc_driver, driver)
  80. /* These are the generic parameters, that drivers may instantiate */
  81. #define FMC_PARAM_BUSID(_d) \
  82. module_param_array_named(busid, _d.busid_val, int, &_d.busid_n, 0444)
  83. #define FMC_PARAM_GATEWARE(_d) \
  84. module_param_array_named(gateware, _d.gw_val, charp, &_d.gw_n, 0444)
  85. /*
  86. * Drivers may need to configure gpio pins in the carrier. To read input
  87. * (a very uncommon operation, and definitely not in the hot paths), just
  88. * configure one gpio only and get 0 or 1 as retval of the config method
  89. */
  90. struct fmc_gpio {
  91. char *carrier_name; /* name or NULL for virtual pins */
  92. int gpio;
  93. int _gpio; /* internal use by the carrier */
  94. int mode; /* GPIOF_DIR_OUT etc, from <linux/gpio.h> */
  95. int irqmode; /* IRQF_TRIGGER_LOW and so on */
  96. };
  97. /* The numbering of gpio pins allows access to raw pins or virtual roles */
  98. #define FMC_GPIO_RAW(x) (x) /* 4096 of them */
  99. #define __FMC_GPIO_IS_RAW(x) ((x) < 0x1000)
  100. #define FMC_GPIO_IRQ(x) ((x) + 0x1000) /* 256 of them */
  101. #define FMC_GPIO_LED(x) ((x) + 0x1100) /* 256 of them */
  102. #define FMC_GPIO_KEY(x) ((x) + 0x1200) /* 256 of them */
  103. #define FMC_GPIO_TP(x) ((x) + 0x1300) /* 256 of them */
  104. #define FMC_GPIO_USER(x) ((x) + 0x1400) /* 256 of them */
  105. /* We may add SCL and SDA, or other roles if the need arises */
  106. /* GPIOF_DIR_IN etc are missing before 3.0. copy from <linux/gpio.h> */
  107. #ifndef GPIOF_DIR_IN
  108. # define GPIOF_DIR_OUT (0 << 0)
  109. # define GPIOF_DIR_IN (1 << 0)
  110. # define GPIOF_INIT_LOW (0 << 1)
  111. # define GPIOF_INIT_HIGH (1 << 1)
  112. #endif
  113. /*
  114. * The operations are offered by each carrier and should make driver
  115. * design completely independent of the carrier. Named GPIO pins may be
  116. * the exception.
  117. */
  118. struct fmc_operations {
  119. uint32_t (*read32)(struct fmc_device *fmc, int offset);
  120. void (*write32)(struct fmc_device *fmc, uint32_t value, int offset);
  121. int (*validate)(struct fmc_device *fmc, struct fmc_driver *drv);
  122. int (*reprogram)(struct fmc_device *f, struct fmc_driver *d, char *gw);
  123. int (*irq_request)(struct fmc_device *fmc, irq_handler_t h,
  124. char *name, int flags);
  125. void (*irq_ack)(struct fmc_device *fmc);
  126. int (*irq_free)(struct fmc_device *fmc);
  127. int (*gpio_config)(struct fmc_device *fmc, struct fmc_gpio *gpio,
  128. int ngpio);
  129. int (*read_ee)(struct fmc_device *fmc, int pos, void *d, int l);
  130. int (*write_ee)(struct fmc_device *fmc, int pos, const void *d, int l);
  131. };
  132. /* Prefer this helper rather than calling of fmc->reprogram directly */
  133. extern int fmc_reprogram(struct fmc_device *f, struct fmc_driver *d, char *gw,
  134. int sdb_entry);
  135. /*
  136. * The device reports all information needed to access hw.
  137. *
  138. * If we have eeprom_len and not contents, the core reads it.
  139. * Then, parsing of identifiers is done by the core which fills fmc_fru_id..
  140. * Similarly a device that must be matched based on SDB cores must
  141. * fill the entry point and the core will scan the bus (FIXME: sdb match)
  142. */
  143. struct fmc_device {
  144. unsigned long version;
  145. unsigned long flags;
  146. struct module *owner; /* char device must pin it */
  147. struct fmc_fru_id id; /* for EEPROM-based match */
  148. struct fmc_operations *op; /* carrier-provided */
  149. int irq; /* according to host bus. 0 == none */
  150. int eeprom_len; /* Usually 8kB, may be less */
  151. int eeprom_addr; /* 0x50, 0x52 etc */
  152. uint8_t *eeprom; /* Full contents or leading part */
  153. char *carrier_name; /* "SPEC" or similar, for special use */
  154. void *carrier_data; /* "struct spec *" or equivalent */
  155. __iomem void *fpga_base; /* May be NULL (Etherbone) */
  156. __iomem void *slot_base; /* Set by the driver */
  157. struct fmc_device **devarray; /* Allocated by the bus */
  158. int slot_id; /* Index in the slot array */
  159. int nr_slots; /* Number of slots in this carrier */
  160. unsigned long memlen; /* Used for the char device */
  161. struct device dev; /* For Linux use */
  162. struct device *hwdev; /* The underlying hardware device */
  163. unsigned long sdbfs_entry;
  164. struct sdb_array *sdb;
  165. uint32_t device_id; /* Filled by the device */
  166. char *mezzanine_name; /* Defaults to ``fmc'' */
  167. void *mezzanine_data;
  168. };
  169. #define to_fmc_device(x) container_of((x), struct fmc_device, dev)
  170. #define FMC_DEVICE_HAS_GOLDEN 1
  171. #define FMC_DEVICE_HAS_CUSTOM 2
  172. #define FMC_DEVICE_NO_MEZZANINE 4
  173. #define FMC_DEVICE_MATCH_SDB 8 /* fmc-core must scan sdb in fpga */
  174. /*
  175. * If fpga_base can be used, the carrier offers no readl/writel methods, and
  176. * this expands to a single, fast, I/O access.
  177. */
  178. static inline uint32_t fmc_readl(struct fmc_device *fmc, int offset)
  179. {
  180. if (unlikely(fmc->op->read32))
  181. return fmc->op->read32(fmc, offset);
  182. return readl(fmc->fpga_base + offset);
  183. }
  184. static inline void fmc_writel(struct fmc_device *fmc, uint32_t val, int off)
  185. {
  186. if (unlikely(fmc->op->write32))
  187. fmc->op->write32(fmc, val, off);
  188. else
  189. writel(val, fmc->fpga_base + off);
  190. }
  191. /* pci-like naming */
  192. static inline void *fmc_get_drvdata(const struct fmc_device *fmc)
  193. {
  194. return dev_get_drvdata(&fmc->dev);
  195. }
  196. static inline void fmc_set_drvdata(struct fmc_device *fmc, void *data)
  197. {
  198. dev_set_drvdata(&fmc->dev, data);
  199. }
  200. /* The 4 access points */
  201. extern int fmc_driver_register(struct fmc_driver *drv);
  202. extern void fmc_driver_unregister(struct fmc_driver *drv);
  203. extern int fmc_device_register(struct fmc_device *tdev);
  204. extern void fmc_device_unregister(struct fmc_device *tdev);
  205. /* Two more for device sets, all driven by the same FPGA */
  206. extern int fmc_device_register_n(struct fmc_device **devs, int n);
  207. extern void fmc_device_unregister_n(struct fmc_device **devs, int n);
  208. /* Internal cross-calls between files; not exported to other modules */
  209. extern int fmc_match(struct device *dev, struct device_driver *drv);
  210. extern int fmc_fill_id_info(struct fmc_device *fmc);
  211. extern void fmc_free_id_info(struct fmc_device *fmc);
  212. extern void fmc_dump_eeprom(const struct fmc_device *fmc);
  213. extern void fmc_dump_sdb(const struct fmc_device *fmc);
  214. #endif /* __LINUX_FMC_H__ */