blob: 9c3079d08e11500c9ae737ce659b57c0e230a320 [file] [log] [blame]
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +02001/*
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 Gonsalvez416289b2012-05-11 10:17:13 +02009 * Software Foundation; version 2 of the License.
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020010 */
11
Jens Taprogge849e0ad2012-09-04 17:01:13 +020012#include <linux/mod_devicetable.h>
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020013#include <linux/device.h>
Jens Taproggefaa75c42012-09-12 14:55:38 +020014#include <linux/interrupt.h>
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020015
Jens Taprogge187e4782012-09-04 17:01:14 +020016#include "ipack_ids.h"
17
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020018#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
31struct ipack_bus_ops;
32struct ipack_driver;
33
34enum ipack_space {
35 IPACK_IO_SPACE = 0,
36 IPACK_ID_SPACE = 1,
37 IPACK_MEM_SPACE = 2,
38};
39
40/**
41 * struct ipack_addr_space - Virtual address space mapped for a specified type.
42 *
43 * @address: virtual address
44 * @size: size of the mapped space
45 */
46struct ipack_addr_space {
Samuel Iglesias Gonsalvez5a81b4a2012-05-14 12:41:25 +020047 void __iomem *address;
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020048 unsigned int size;
49};
50
51/**
52 * struct ipack_device
53 *
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020054 * @bus_nr: IP bus number where the device is plugged
55 * @slot: Slot where the device is plugged in the carrier board
56 * @irq: IRQ vector
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +020057 * @bus: ipack_bus_device where the device is plugged to.
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020058 * @id_space: Virtual address to ID space.
59 * @io_space: Virtual address to IO space.
60 * @mem_space: Virtual address to MEM space.
61 * @dev: device in kernel representation.
62 *
63 * Warning: Direct access to mapped memory is possible but the endianness
64 * is not the same with PCI carrier or VME carrier. The endianness is managed
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +020065 * by the carrier board throught bus->ops.
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020066 */
67struct ipack_device {
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020068 unsigned int bus_nr;
69 unsigned int slot;
70 unsigned int irq;
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +020071 struct ipack_bus_device *bus;
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020072 struct ipack_addr_space id_space;
73 struct ipack_addr_space io_space;
74 struct ipack_addr_space mem_space;
75 struct device dev;
Jens Taproggee8ed3272012-09-04 17:01:15 +020076 u8 *id;
Jens Taprogge187e4782012-09-04 17:01:14 +020077 size_t id_avail;
Jens Taproggee8ed3272012-09-04 17:01:15 +020078 u32 id_vendor;
79 u32 id_device;
Jens Taprogge187e4782012-09-04 17:01:14 +020080 u8 id_format;
Jens Taproggea92caeb2012-09-11 13:35:03 +020081 unsigned int id_crc_correct:1;
Jens Taprogge0b0f3a12012-09-11 13:34:57 +020082 unsigned int speed_8mhz:1;
83 unsigned int speed_32mhz:1;
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020084};
85
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +020086/**
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020087 * struct ipack_driver_ops -- callbacks to mezzanine driver for installing/removing one device
88 *
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020089 * @probe: Probe function
90 * @remove: tell the driver that the carrier board wants to remove one device
91 */
92
93struct ipack_driver_ops {
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020094 int (*probe) (struct ipack_device *dev);
95 void (*remove) (struct ipack_device *dev);
96};
97
98/**
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +020099 * struct ipack_driver -- Specific data to each ipack board driver
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200100 *
101 * @driver: Device driver kernel representation
102 * @ops: Mezzanine driver operations specific for the ipack bus.
103 */
104struct ipack_driver {
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200105 struct device_driver driver;
Jens Taprogge849e0ad2012-09-04 17:01:13 +0200106 const struct ipack_device_id *id_table;
Jens Taproggee8011132012-09-04 17:01:17 +0200107 const struct ipack_driver_ops *ops;
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200108};
109
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200110/**
111 * struct ipack_bus_ops - available operations on a bridge module
112 *
113 * @map_space: map IP address space
114 * @unmap_space: unmap IP address space
115 * @request_irq: request IRQ
116 * @free_irq: free IRQ
Jens Taprogge7b6ab332012-09-11 13:34:55 +0200117 * @get_clockrate: Returns the clockrate the carrier is currently
118 * communicating with the device at.
119 * @set_clockrate: Sets the clock-rate for carrier / module communication.
120 * Should return -EINVAL if the requested speed is not supported.
121 * @get_error: Returns the error state for the slot the device is attached
122 * to.
123 * @get_timeout: Returns 1 if the communication with the device has
124 * previously timed out.
125 * @reset_timeout: Resets the state returned by get_timeout.
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200126 */
127struct ipack_bus_ops {
128 int (*map_space) (struct ipack_device *dev, unsigned int memory_size, int space);
129 int (*unmap_space) (struct ipack_device *dev, int space);
Jens Taproggefaa75c42012-09-12 14:55:38 +0200130 int (*request_irq) (struct ipack_device *dev, int vector,
131 irqreturn_t (*handler)(void *), void *arg);
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200132 int (*free_irq) (struct ipack_device *dev);
Jens Taprogge7b6ab332012-09-11 13:34:55 +0200133 int (*get_clockrate) (struct ipack_device *dev);
134 int (*set_clockrate) (struct ipack_device *dev, int mherz);
135 int (*get_error) (struct ipack_device *dev);
136 int (*get_timeout) (struct ipack_device *dev);
137 int (*reset_timeout) (struct ipack_device *dev);
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200138};
139
140/**
141 * struct ipack_bus_device
142 *
143 * @dev: pointer to carrier device
144 * @slots: number of slots available
145 * @bus_nr: ipack bus number
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200146 * @ops: bus operations for the mezzanine drivers
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200147 */
148struct ipack_bus_device {
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200149 struct device *parent;
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200150 int slots;
151 int bus_nr;
Stephen Hemminger9869a932012-09-10 11:14:01 -0700152 const struct ipack_bus_ops *ops;
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200153};
154
155/**
156 * ipack_bus_register -- register a new ipack bus
157 *
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200158 * @parent: pointer to the parent device, if any.
159 * @slots: number of slots available in the bus device.
160 * @ops: bus operations for the mezzanine drivers.
161 *
162 * The carrier board device should call this function to register itself as
163 * available bus device in ipack.
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200164 */
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200165struct ipack_bus_device *ipack_bus_register(struct device *parent, int slots,
Stephen Hemminger9869a932012-09-10 11:14:01 -0700166 const struct ipack_bus_ops *ops);
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200167
168/**
169 * ipack_bus_unregister -- unregister an ipack bus
170 */
171int ipack_bus_unregister(struct ipack_bus_device *bus);
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200172
173/**
174 * ipack_driver_register -- Register a new driver
175 *
176 * Called by a ipack driver to register itself as a driver
177 * that can manage ipack devices.
178 */
Stephen Hemminger9869a932012-09-10 11:14:01 -0700179int ipack_driver_register(struct ipack_driver *edrv, struct module *owner,
180 const char *name);
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200181void ipack_driver_unregister(struct ipack_driver *edrv);
182
183/**
184 * ipack_device_register -- register a new mezzanine device
185 *
186 * @bus: ipack bus device it is plugged to.
187 * @slot: slot position in the bus device.
188 * @irqv: IRQ vector for the mezzanine.
189 *
190 * Register a new ipack device (mezzanine device). The call is done by
191 * the carrier device driver.
192 */
Stephen Hemminger9869a932012-09-10 11:14:01 -0700193struct ipack_device *ipack_device_register(struct ipack_bus_device *bus,
194 int slot, int irqv);
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200195void ipack_device_unregister(struct ipack_device *dev);
Jens Taprogge849e0ad2012-09-04 17:01:13 +0200196
197/**
198 * DEFINE_IPACK_DEVICE_TABLE - macro used to describe a IndustryPack table
199 * @_table: device table name
200 *
201 * This macro is used to create a struct ipack_device_id array (a device table)
202 * in a generic manner.
203 */
204#define DEFINE_IPACK_DEVICE_TABLE(_table) \
205 const struct ipack_device_id _table[] __devinitconst
206
207/**
208 * IPACK_DEVICE - macro used to describe a specific IndustryPack device
209 * @_format: the format version (currently either 1 or 2, 8 bit value)
210 * @vend: the 8 or 24 bit IndustryPack Vendor ID
211 * @dev: the 8 or 16 bit IndustryPack Device ID
212 *
213 * This macro is used to create a struct ipack_device_id that matches a specific
214 * device.
215 */
216#define IPACK_DEVICE(_format, vend, dev) \
217 .format = (_format), \
218 .vendor = (vend), \
219 .device = (dev)