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