Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Industry-pack bus. |
| 3 | * |
| 4 | * (C) 2011 Samuel Iglesias Gonsalvez <siglesia@cern.ch>, CERN |
| 5 | * (C) 2012 Samuel Iglesias Gonsalvez <siglesias@igalia.com>, Igalia |
| 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 |
Samuel Iglesias Gonsalvez | 416289b | 2012-05-11 10:17:13 +0200 | [diff] [blame] | 9 | * Software Foundation; version 2 of the License. |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 10 | */ |
| 11 | |
Jens Taprogge | 849e0ad | 2012-09-04 17:01:13 +0200 | [diff] [blame] | 12 | #include <linux/mod_devicetable.h> |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 13 | #include <linux/device.h> |
Jens Taprogge | faa75c4 | 2012-09-12 14:55:38 +0200 | [diff] [blame] | 14 | #include <linux/interrupt.h> |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 15 | |
Jens Taprogge | 187e478 | 2012-09-04 17:01:14 +0200 | [diff] [blame] | 16 | #include "ipack_ids.h" |
| 17 | |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 18 | #define IPACK_IDPROM_OFFSET_I 0x01 |
| 19 | #define IPACK_IDPROM_OFFSET_P 0x03 |
| 20 | #define IPACK_IDPROM_OFFSET_A 0x05 |
| 21 | #define IPACK_IDPROM_OFFSET_C 0x07 |
| 22 | #define IPACK_IDPROM_OFFSET_MANUFACTURER_ID 0x09 |
| 23 | #define IPACK_IDPROM_OFFSET_MODEL 0x0B |
| 24 | #define IPACK_IDPROM_OFFSET_REVISION 0x0D |
| 25 | #define IPACK_IDPROM_OFFSET_RESERVED 0x0F |
| 26 | #define IPACK_IDPROM_OFFSET_DRIVER_ID_L 0x11 |
| 27 | #define IPACK_IDPROM_OFFSET_DRIVER_ID_H 0x13 |
| 28 | #define IPACK_IDPROM_OFFSET_NUM_BYTES 0x15 |
| 29 | #define IPACK_IDPROM_OFFSET_CRC 0x17 |
| 30 | |
| 31 | struct ipack_bus_ops; |
| 32 | struct ipack_driver; |
| 33 | |
| 34 | enum ipack_space { |
| 35 | IPACK_IO_SPACE = 0, |
| 36 | IPACK_ID_SPACE = 1, |
| 37 | IPACK_MEM_SPACE = 2, |
Jens Taprogge | e4af949 | 2012-09-13 12:32:19 +0200 | [diff] [blame] | 38 | IPACK_INT_SPACE, |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | /** |
| 42 | * struct ipack_addr_space - Virtual address space mapped for a specified type. |
| 43 | * |
| 44 | * @address: virtual address |
| 45 | * @size: size of the mapped space |
| 46 | */ |
| 47 | struct ipack_addr_space { |
Samuel Iglesias Gonsalvez | 5a81b4a | 2012-05-14 12:41:25 +0200 | [diff] [blame] | 48 | void __iomem *address; |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 49 | unsigned int size; |
| 50 | }; |
| 51 | |
| 52 | /** |
Jens Taprogge | bb29ab8 | 2012-09-27 12:37:28 +0200 | [diff] [blame^] | 53 | */ |
| 54 | struct ipack_region { |
| 55 | phys_addr_t start; |
| 56 | size_t size; |
| 57 | }; |
| 58 | |
| 59 | /** |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 60 | * struct ipack_device |
| 61 | * |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 62 | * @slot: Slot where the device is plugged in the carrier board |
Samuel Iglesias Gonsalvez | ec44033 | 2012-05-18 11:10:05 +0200 | [diff] [blame] | 63 | * @bus: ipack_bus_device where the device is plugged to. |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 64 | * @id_space: Virtual address to ID space. |
| 65 | * @io_space: Virtual address to IO space. |
| 66 | * @mem_space: Virtual address to MEM space. |
| 67 | * @dev: device in kernel representation. |
| 68 | * |
| 69 | * Warning: Direct access to mapped memory is possible but the endianness |
| 70 | * is not the same with PCI carrier or VME carrier. The endianness is managed |
Samuel Iglesias Gonsalvez | ec44033 | 2012-05-18 11:10:05 +0200 | [diff] [blame] | 71 | * by the carrier board throught bus->ops. |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 72 | */ |
| 73 | struct ipack_device { |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 74 | unsigned int slot; |
Samuel Iglesias Gonsalvez | ec44033 | 2012-05-18 11:10:05 +0200 | [diff] [blame] | 75 | struct ipack_bus_device *bus; |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 76 | struct ipack_addr_space id_space; |
| 77 | struct ipack_addr_space io_space; |
Jens Taprogge | e4af949 | 2012-09-13 12:32:19 +0200 | [diff] [blame] | 78 | struct ipack_addr_space int_space; |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 79 | struct ipack_addr_space mem_space; |
| 80 | struct device dev; |
Jens Taprogge | 1e91795 | 2012-09-27 12:37:26 +0200 | [diff] [blame] | 81 | void (*release) (struct ipack_device *dev); |
Jens Taprogge | e8ed327 | 2012-09-04 17:01:15 +0200 | [diff] [blame] | 82 | u8 *id; |
Jens Taprogge | 187e478 | 2012-09-04 17:01:14 +0200 | [diff] [blame] | 83 | size_t id_avail; |
Jens Taprogge | e8ed327 | 2012-09-04 17:01:15 +0200 | [diff] [blame] | 84 | u32 id_vendor; |
| 85 | u32 id_device; |
Jens Taprogge | 187e478 | 2012-09-04 17:01:14 +0200 | [diff] [blame] | 86 | u8 id_format; |
Jens Taprogge | a92caeb | 2012-09-11 13:35:03 +0200 | [diff] [blame] | 87 | unsigned int id_crc_correct:1; |
Jens Taprogge | 0b0f3a1 | 2012-09-11 13:34:57 +0200 | [diff] [blame] | 88 | unsigned int speed_8mhz:1; |
| 89 | unsigned int speed_32mhz:1; |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 90 | }; |
| 91 | |
Samuel Iglesias Gonsalvez | ec44033 | 2012-05-18 11:10:05 +0200 | [diff] [blame] | 92 | /** |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 93 | * struct ipack_driver_ops -- callbacks to mezzanine driver for installing/removing one device |
| 94 | * |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 95 | * @probe: Probe function |
| 96 | * @remove: tell the driver that the carrier board wants to remove one device |
| 97 | */ |
| 98 | |
| 99 | struct ipack_driver_ops { |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 100 | int (*probe) (struct ipack_device *dev); |
| 101 | void (*remove) (struct ipack_device *dev); |
| 102 | }; |
| 103 | |
| 104 | /** |
Samuel Iglesias Gonsalvez | ec44033 | 2012-05-18 11:10:05 +0200 | [diff] [blame] | 105 | * struct ipack_driver -- Specific data to each ipack board driver |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 106 | * |
| 107 | * @driver: Device driver kernel representation |
| 108 | * @ops: Mezzanine driver operations specific for the ipack bus. |
| 109 | */ |
| 110 | struct ipack_driver { |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 111 | struct device_driver driver; |
Jens Taprogge | 849e0ad | 2012-09-04 17:01:13 +0200 | [diff] [blame] | 112 | const struct ipack_device_id *id_table; |
Jens Taprogge | e801113 | 2012-09-04 17:01:17 +0200 | [diff] [blame] | 113 | const struct ipack_driver_ops *ops; |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 114 | }; |
| 115 | |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 116 | /** |
| 117 | * struct ipack_bus_ops - available operations on a bridge module |
| 118 | * |
| 119 | * @map_space: map IP address space |
| 120 | * @unmap_space: unmap IP address space |
| 121 | * @request_irq: request IRQ |
| 122 | * @free_irq: free IRQ |
Jens Taprogge | 7b6ab33 | 2012-09-11 13:34:55 +0200 | [diff] [blame] | 123 | * @get_clockrate: Returns the clockrate the carrier is currently |
| 124 | * communicating with the device at. |
| 125 | * @set_clockrate: Sets the clock-rate for carrier / module communication. |
| 126 | * Should return -EINVAL if the requested speed is not supported. |
| 127 | * @get_error: Returns the error state for the slot the device is attached |
| 128 | * to. |
| 129 | * @get_timeout: Returns 1 if the communication with the device has |
| 130 | * previously timed out. |
| 131 | * @reset_timeout: Resets the state returned by get_timeout. |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 132 | */ |
| 133 | struct ipack_bus_ops { |
Jens Taprogge | bb29ab8 | 2012-09-27 12:37:28 +0200 | [diff] [blame^] | 134 | int (*map_space) (struct ipack_device *dev, ssize_t memory_size, int space); |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 135 | int (*unmap_space) (struct ipack_device *dev, int space); |
Jens Taprogge | c6e2dfa | 2012-09-13 12:32:21 +0200 | [diff] [blame] | 136 | int (*request_irq) (struct ipack_device *dev, |
Jens Taprogge | faa75c4 | 2012-09-12 14:55:38 +0200 | [diff] [blame] | 137 | irqreturn_t (*handler)(void *), void *arg); |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 138 | int (*free_irq) (struct ipack_device *dev); |
Jens Taprogge | 7b6ab33 | 2012-09-11 13:34:55 +0200 | [diff] [blame] | 139 | int (*get_clockrate) (struct ipack_device *dev); |
| 140 | int (*set_clockrate) (struct ipack_device *dev, int mherz); |
| 141 | int (*get_error) (struct ipack_device *dev); |
| 142 | int (*get_timeout) (struct ipack_device *dev); |
| 143 | int (*reset_timeout) (struct ipack_device *dev); |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | /** |
| 147 | * struct ipack_bus_device |
| 148 | * |
| 149 | * @dev: pointer to carrier device |
| 150 | * @slots: number of slots available |
| 151 | * @bus_nr: ipack bus number |
Samuel Iglesias Gonsalvez | ec44033 | 2012-05-18 11:10:05 +0200 | [diff] [blame] | 152 | * @ops: bus operations for the mezzanine drivers |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 153 | */ |
| 154 | struct ipack_bus_device { |
Samuel Iglesias Gonsalvez | ec44033 | 2012-05-18 11:10:05 +0200 | [diff] [blame] | 155 | struct device *parent; |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 156 | int slots; |
| 157 | int bus_nr; |
Stephen Hemminger | 9869a93 | 2012-09-10 11:14:01 -0700 | [diff] [blame] | 158 | const struct ipack_bus_ops *ops; |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | /** |
| 162 | * ipack_bus_register -- register a new ipack bus |
| 163 | * |
Samuel Iglesias Gonsalvez | ec44033 | 2012-05-18 11:10:05 +0200 | [diff] [blame] | 164 | * @parent: pointer to the parent device, if any. |
| 165 | * @slots: number of slots available in the bus device. |
| 166 | * @ops: bus operations for the mezzanine drivers. |
| 167 | * |
| 168 | * The carrier board device should call this function to register itself as |
| 169 | * available bus device in ipack. |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 170 | */ |
Samuel Iglesias Gonsalvez | ec44033 | 2012-05-18 11:10:05 +0200 | [diff] [blame] | 171 | struct ipack_bus_device *ipack_bus_register(struct device *parent, int slots, |
Stephen Hemminger | 9869a93 | 2012-09-10 11:14:01 -0700 | [diff] [blame] | 172 | const struct ipack_bus_ops *ops); |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 173 | |
| 174 | /** |
| 175 | * ipack_bus_unregister -- unregister an ipack bus |
| 176 | */ |
| 177 | int ipack_bus_unregister(struct ipack_bus_device *bus); |
Samuel Iglesias Gonsalvez | ec44033 | 2012-05-18 11:10:05 +0200 | [diff] [blame] | 178 | |
| 179 | /** |
| 180 | * ipack_driver_register -- Register a new driver |
| 181 | * |
| 182 | * Called by a ipack driver to register itself as a driver |
| 183 | * that can manage ipack devices. |
| 184 | */ |
Stephen Hemminger | 9869a93 | 2012-09-10 11:14:01 -0700 | [diff] [blame] | 185 | int ipack_driver_register(struct ipack_driver *edrv, struct module *owner, |
| 186 | const char *name); |
Samuel Iglesias Gonsalvez | ec44033 | 2012-05-18 11:10:05 +0200 | [diff] [blame] | 187 | void ipack_driver_unregister(struct ipack_driver *edrv); |
| 188 | |
| 189 | /** |
Jens Taprogge | 1e91795 | 2012-09-27 12:37:26 +0200 | [diff] [blame] | 190 | * ipack_device_register -- register an IPack device with the kernel |
| 191 | * @dev: the new device to register. |
Samuel Iglesias Gonsalvez | ec44033 | 2012-05-18 11:10:05 +0200 | [diff] [blame] | 192 | * |
Jens Taprogge | 1e91795 | 2012-09-27 12:37:26 +0200 | [diff] [blame] | 193 | * Register a new IPack device ("module" in IndustryPack jargon). The call |
| 194 | * is done by the carrier driver. The carrier should populate the fields |
| 195 | * bus and slot of @dev prior to calling this function. The rest of the |
| 196 | * fields will be allocated and populated during registration. |
Samuel Iglesias Gonsalvez | ec44033 | 2012-05-18 11:10:05 +0200 | [diff] [blame] | 197 | * |
Jens Taprogge | 1e91795 | 2012-09-27 12:37:26 +0200 | [diff] [blame] | 198 | * Return zero on success or error code on failure. |
Samuel Iglesias Gonsalvez | ec44033 | 2012-05-18 11:10:05 +0200 | [diff] [blame] | 199 | */ |
Jens Taprogge | 1e91795 | 2012-09-27 12:37:26 +0200 | [diff] [blame] | 200 | int ipack_device_register(struct ipack_device *dev); |
Samuel Iglesias Gonsalvez | ec44033 | 2012-05-18 11:10:05 +0200 | [diff] [blame] | 201 | void ipack_device_unregister(struct ipack_device *dev); |
Jens Taprogge | 849e0ad | 2012-09-04 17:01:13 +0200 | [diff] [blame] | 202 | |
| 203 | /** |
| 204 | * DEFINE_IPACK_DEVICE_TABLE - macro used to describe a IndustryPack table |
| 205 | * @_table: device table name |
| 206 | * |
| 207 | * This macro is used to create a struct ipack_device_id array (a device table) |
| 208 | * in a generic manner. |
| 209 | */ |
| 210 | #define DEFINE_IPACK_DEVICE_TABLE(_table) \ |
| 211 | const struct ipack_device_id _table[] __devinitconst |
| 212 | |
| 213 | /** |
| 214 | * IPACK_DEVICE - macro used to describe a specific IndustryPack device |
| 215 | * @_format: the format version (currently either 1 or 2, 8 bit value) |
| 216 | * @vend: the 8 or 24 bit IndustryPack Vendor ID |
| 217 | * @dev: the 8 or 16 bit IndustryPack Device ID |
| 218 | * |
| 219 | * This macro is used to create a struct ipack_device_id that matches a specific |
| 220 | * device. |
| 221 | */ |
| 222 | #define IPACK_DEVICE(_format, vend, dev) \ |
| 223 | .format = (_format), \ |
| 224 | .vendor = (vend), \ |
| 225 | .device = (dev) |