ipack.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Industry-pack bus.
  3. *
  4. * Copyright (C) 2011-2012 CERN (www.cern.ch)
  5. * Author: Samuel Iglesias Gonsalvez <siglesias@igalia.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; version 2 of the License.
  10. */
  11. #include <linux/mod_devicetable.h>
  12. #include <linux/device.h>
  13. #include <linux/interrupt.h>
  14. #define IPACK_IDPROM_OFFSET_I 0x01
  15. #define IPACK_IDPROM_OFFSET_P 0x03
  16. #define IPACK_IDPROM_OFFSET_A 0x05
  17. #define IPACK_IDPROM_OFFSET_C 0x07
  18. #define IPACK_IDPROM_OFFSET_MANUFACTURER_ID 0x09
  19. #define IPACK_IDPROM_OFFSET_MODEL 0x0B
  20. #define IPACK_IDPROM_OFFSET_REVISION 0x0D
  21. #define IPACK_IDPROM_OFFSET_RESERVED 0x0F
  22. #define IPACK_IDPROM_OFFSET_DRIVER_ID_L 0x11
  23. #define IPACK_IDPROM_OFFSET_DRIVER_ID_H 0x13
  24. #define IPACK_IDPROM_OFFSET_NUM_BYTES 0x15
  25. #define IPACK_IDPROM_OFFSET_CRC 0x17
  26. /*
  27. * IndustryPack Fromat, Vendor and Device IDs.
  28. */
  29. /* ID section format versions */
  30. #define IPACK_ID_VERSION_INVALID 0x00
  31. #define IPACK_ID_VERSION_1 0x01
  32. #define IPACK_ID_VERSION_2 0x02
  33. /* Vendors and devices. Sort key: vendor first, device next. */
  34. #define IPACK1_VENDOR_ID_RESERVED1 0x00
  35. #define IPACK1_VENDOR_ID_RESERVED2 0xFF
  36. #define IPACK1_VENDOR_ID_UNREGISTRED01 0x01
  37. #define IPACK1_VENDOR_ID_UNREGISTRED02 0x02
  38. #define IPACK1_VENDOR_ID_UNREGISTRED03 0x03
  39. #define IPACK1_VENDOR_ID_UNREGISTRED04 0x04
  40. #define IPACK1_VENDOR_ID_UNREGISTRED05 0x05
  41. #define IPACK1_VENDOR_ID_UNREGISTRED06 0x06
  42. #define IPACK1_VENDOR_ID_UNREGISTRED07 0x07
  43. #define IPACK1_VENDOR_ID_UNREGISTRED08 0x08
  44. #define IPACK1_VENDOR_ID_UNREGISTRED09 0x09
  45. #define IPACK1_VENDOR_ID_UNREGISTRED10 0x0A
  46. #define IPACK1_VENDOR_ID_UNREGISTRED11 0x0B
  47. #define IPACK1_VENDOR_ID_UNREGISTRED12 0x0C
  48. #define IPACK1_VENDOR_ID_UNREGISTRED13 0x0D
  49. #define IPACK1_VENDOR_ID_UNREGISTRED14 0x0E
  50. #define IPACK1_VENDOR_ID_UNREGISTRED15 0x0F
  51. #define IPACK1_VENDOR_ID_SBS 0xF0
  52. #define IPACK1_DEVICE_ID_SBS_OCTAL_232 0x22
  53. #define IPACK1_DEVICE_ID_SBS_OCTAL_422 0x2A
  54. #define IPACK1_DEVICE_ID_SBS_OCTAL_485 0x48
  55. struct ipack_bus_ops;
  56. struct ipack_driver;
  57. enum ipack_space {
  58. IPACK_IO_SPACE = 0,
  59. IPACK_ID_SPACE,
  60. IPACK_INT_SPACE,
  61. IPACK_MEM8_SPACE,
  62. IPACK_MEM16_SPACE,
  63. /* Dummy for counting the number of entries. Must remain the last
  64. * entry */
  65. IPACK_SPACE_COUNT,
  66. };
  67. /**
  68. */
  69. struct ipack_region {
  70. phys_addr_t start;
  71. size_t size;
  72. };
  73. /**
  74. * struct ipack_device
  75. *
  76. * @slot: Slot where the device is plugged in the carrier board
  77. * @bus: ipack_bus_device where the device is plugged to.
  78. * @id_space: Virtual address to ID space.
  79. * @io_space: Virtual address to IO space.
  80. * @mem_space: Virtual address to MEM space.
  81. * @dev: device in kernel representation.
  82. *
  83. * Warning: Direct access to mapped memory is possible but the endianness
  84. * is not the same with PCI carrier or VME carrier. The endianness is managed
  85. * by the carrier board throught bus->ops.
  86. */
  87. struct ipack_device {
  88. unsigned int slot;
  89. struct ipack_bus_device *bus;
  90. struct device dev;
  91. void (*release) (struct ipack_device *dev);
  92. struct ipack_region region[IPACK_SPACE_COUNT];
  93. u8 *id;
  94. size_t id_avail;
  95. u32 id_vendor;
  96. u32 id_device;
  97. u8 id_format;
  98. unsigned int id_crc_correct:1;
  99. unsigned int speed_8mhz:1;
  100. unsigned int speed_32mhz:1;
  101. };
  102. /**
  103. * struct ipack_driver_ops -- Callbacks to IPack device driver
  104. *
  105. * @probe: Probe function
  106. * @remove: Prepare imminent removal of the device. Services provided by the
  107. * device should be revoked.
  108. */
  109. struct ipack_driver_ops {
  110. int (*probe) (struct ipack_device *dev);
  111. void (*remove) (struct ipack_device *dev);
  112. };
  113. /**
  114. * struct ipack_driver -- Specific data to each ipack device driver
  115. *
  116. * @driver: Device driver kernel representation
  117. * @ops: Callbacks provided by the IPack device driver
  118. */
  119. struct ipack_driver {
  120. struct device_driver driver;
  121. const struct ipack_device_id *id_table;
  122. const struct ipack_driver_ops *ops;
  123. };
  124. /**
  125. * struct ipack_bus_ops - available operations on a bridge module
  126. *
  127. * @map_space: map IP address space
  128. * @unmap_space: unmap IP address space
  129. * @request_irq: request IRQ
  130. * @free_irq: free IRQ
  131. * @get_clockrate: Returns the clockrate the carrier is currently
  132. * communicating with the device at.
  133. * @set_clockrate: Sets the clock-rate for carrier / module communication.
  134. * Should return -EINVAL if the requested speed is not supported.
  135. * @get_error: Returns the error state for the slot the device is attached
  136. * to.
  137. * @get_timeout: Returns 1 if the communication with the device has
  138. * previously timed out.
  139. * @reset_timeout: Resets the state returned by get_timeout.
  140. */
  141. struct ipack_bus_ops {
  142. int (*request_irq) (struct ipack_device *dev,
  143. irqreturn_t (*handler)(void *), void *arg);
  144. int (*free_irq) (struct ipack_device *dev);
  145. int (*get_clockrate) (struct ipack_device *dev);
  146. int (*set_clockrate) (struct ipack_device *dev, int mherz);
  147. int (*get_error) (struct ipack_device *dev);
  148. int (*get_timeout) (struct ipack_device *dev);
  149. int (*reset_timeout) (struct ipack_device *dev);
  150. };
  151. /**
  152. * struct ipack_bus_device
  153. *
  154. * @dev: pointer to carrier device
  155. * @slots: number of slots available
  156. * @bus_nr: ipack bus number
  157. * @ops: bus operations for the mezzanine drivers
  158. */
  159. struct ipack_bus_device {
  160. struct module *owner;
  161. struct device *parent;
  162. int slots;
  163. int bus_nr;
  164. const struct ipack_bus_ops *ops;
  165. };
  166. /**
  167. * ipack_bus_register -- register a new ipack bus
  168. *
  169. * @parent: pointer to the parent device, if any.
  170. * @slots: number of slots available in the bus device.
  171. * @ops: bus operations for the mezzanine drivers.
  172. *
  173. * The carrier board device should call this function to register itself as
  174. * available bus device in ipack.
  175. */
  176. struct ipack_bus_device *ipack_bus_register(struct device *parent, int slots,
  177. const struct ipack_bus_ops *ops,
  178. struct module *owner);
  179. /**
  180. * ipack_bus_unregister -- unregister an ipack bus
  181. */
  182. int ipack_bus_unregister(struct ipack_bus_device *bus);
  183. /**
  184. * ipack_driver_register -- Register a new ipack device driver
  185. *
  186. * Called by a ipack driver to register itself as a driver
  187. * that can manage ipack devices.
  188. */
  189. int ipack_driver_register(struct ipack_driver *edrv, struct module *owner,
  190. const char *name);
  191. void ipack_driver_unregister(struct ipack_driver *edrv);
  192. /**
  193. * ipack_device_init -- initialize an IPack device
  194. * @dev: the new device to initialize.
  195. *
  196. * Initialize a new IPack device ("module" in IndustryPack jargon). The call
  197. * is done by the carrier driver. The carrier should populate the fields
  198. * bus and slot as well as the region array of @dev prior to calling this
  199. * function. The rest of the fields will be allocated and populated
  200. * during initalization.
  201. *
  202. * Return zero on success or error code on failure.
  203. *
  204. * NOTE: _Never_ directly free @dev after calling this function, even
  205. * if it returned an error! Always use ipack_put_device() to give up the
  206. * reference initialized in this function instead.
  207. */
  208. int ipack_device_init(struct ipack_device *dev);
  209. /**
  210. * ipack_device_add -- Add an IPack device
  211. * @dev: the new device to add.
  212. *
  213. * Add a new IPack device. The call is done by the carrier driver
  214. * after calling ipack_device_init().
  215. *
  216. * Return zero on success or error code on failure.
  217. *
  218. * NOTE: _Never_ directly free @dev after calling this function, even
  219. * if it returned an error! Always use ipack_put_device() to give up the
  220. * reference initialized in this function instead.
  221. */
  222. int ipack_device_add(struct ipack_device *dev);
  223. void ipack_device_del(struct ipack_device *dev);
  224. void ipack_get_device(struct ipack_device *dev);
  225. void ipack_put_device(struct ipack_device *dev);
  226. /**
  227. * DEFINE_IPACK_DEVICE_TABLE - macro used to describe a IndustryPack table
  228. * @_table: device table name
  229. *
  230. * This macro is used to create a struct ipack_device_id array (a device table)
  231. * in a generic manner.
  232. */
  233. #define DEFINE_IPACK_DEVICE_TABLE(_table) \
  234. const struct ipack_device_id _table[]
  235. /**
  236. * IPACK_DEVICE - macro used to describe a specific IndustryPack device
  237. * @_format: the format version (currently either 1 or 2, 8 bit value)
  238. * @vend: the 8 or 24 bit IndustryPack Vendor ID
  239. * @dev: the 8 or 16 bit IndustryPack Device ID
  240. *
  241. * This macro is used to create a struct ipack_device_id that matches a specific
  242. * device.
  243. */
  244. #define IPACK_DEVICE(_format, vend, dev) \
  245. .format = (_format), \
  246. .vendor = (vend), \
  247. .device = (dev)
  248. /**
  249. * ipack_get_carrier - it increase the carrier ref. counter of
  250. * the carrier module
  251. * @dev: mezzanine device which wants to get the carrier
  252. */
  253. static inline int ipack_get_carrier(struct ipack_device *dev)
  254. {
  255. return try_module_get(dev->bus->owner);
  256. }
  257. /**
  258. * ipack_get_carrier - it decrease the carrier ref. counter of
  259. * the carrier module
  260. * @dev: mezzanine device which wants to get the carrier
  261. */
  262. static inline void ipack_put_carrier(struct ipack_device *dev)
  263. {
  264. module_put(dev->bus->owner);
  265. }