blob: fea12cbb2aebc5019a4bd64a0212f421361c55a9 [file] [log] [blame]
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +02001/*
2 * Industry-pack bus.
3 *
Samuel Iglesias Gonsalvez76859722012-11-16 16:19:58 +01004 * Copyright (C) 2011-2012 CERN (www.cern.ch)
5 * Author: Samuel Iglesias Gonsalvez <siglesias@igalia.com>
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +02006 *
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
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020016#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
Samuel Iglesias Gonsalvez27cf2d12012-11-16 19:33:46 +010029/*
30 * IndustryPack Fromat, Vendor and Device IDs.
31 */
32
33/* ID section format versions */
34#define IPACK_ID_VERSION_INVALID 0x00
35#define IPACK_ID_VERSION_1 0x01
36#define IPACK_ID_VERSION_2 0x02
37
38/* Vendors and devices. Sort key: vendor first, device next. */
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
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020062struct ipack_bus_ops;
63struct ipack_driver;
64
65enum ipack_space {
66 IPACK_IO_SPACE = 0,
Jens Taprogge84a08fa2012-09-27 12:37:29 +020067 IPACK_ID_SPACE,
Jens Taproggee4af9492012-09-13 12:32:19 +020068 IPACK_INT_SPACE,
Jens Taproggefe4a3ed2012-09-27 12:37:36 +020069 IPACK_MEM8_SPACE,
Jens Taprogge48a97352012-09-27 12:37:37 +020070 IPACK_MEM16_SPACE,
Jens Taprogge84a08fa2012-09-27 12:37:29 +020071 /* Dummy for counting the number of entries. Must remain the last
72 * entry */
73 IPACK_SPACE_COUNT,
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020074};
75
76/**
Jens Taproggebb29ab82012-09-27 12:37:28 +020077 */
78struct ipack_region {
79 phys_addr_t start;
80 size_t size;
81};
82
83/**
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020084 * struct ipack_device
85 *
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020086 * @slot: Slot where the device is plugged in the carrier board
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +020087 * @bus: ipack_bus_device where the device is plugged to.
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020088 * @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
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +020095 * by the carrier board throught bus->ops.
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020096 */
97struct ipack_device {
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020098 unsigned int slot;
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +020099 struct ipack_bus_device *bus;
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200100 struct device dev;
Jens Taprogge1e917952012-09-27 12:37:26 +0200101 void (*release) (struct ipack_device *dev);
Jens Taproggea19ad7d2012-09-27 12:37:31 +0200102 struct ipack_region region[IPACK_SPACE_COUNT];
Jens Taproggee8ed3272012-09-04 17:01:15 +0200103 u8 *id;
Jens Taprogge187e4782012-09-04 17:01:14 +0200104 size_t id_avail;
Jens Taproggee8ed3272012-09-04 17:01:15 +0200105 u32 id_vendor;
106 u32 id_device;
Jens Taprogge187e4782012-09-04 17:01:14 +0200107 u8 id_format;
Jens Taproggea92caeb2012-09-11 13:35:03 +0200108 unsigned int id_crc_correct:1;
Jens Taprogge0b0f3a12012-09-11 13:34:57 +0200109 unsigned int speed_8mhz:1;
110 unsigned int speed_32mhz:1;
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200111};
112
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200113/**
Jens Taprogge26c295c2012-09-27 12:37:40 +0200114 * struct ipack_driver_ops -- Callbacks to IPack device driver
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200115 *
Jens Taprogge26c295c2012-09-27 12:37:40 +0200116 * @probe: Probe function
117 * @remove: Prepare imminent removal of the device. Services provided by the
118 * device should be revoked.
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200119 */
120
121struct ipack_driver_ops {
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200122 int (*probe) (struct ipack_device *dev);
123 void (*remove) (struct ipack_device *dev);
124};
125
126/**
Jens Taprogge26c295c2012-09-27 12:37:40 +0200127 * struct ipack_driver -- Specific data to each ipack device driver
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200128 *
Jens Taprogge26c295c2012-09-27 12:37:40 +0200129 * @driver: Device driver kernel representation
130 * @ops: Callbacks provided by the IPack device driver
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200131 */
132struct ipack_driver {
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200133 struct device_driver driver;
Jens Taprogge849e0ad2012-09-04 17:01:13 +0200134 const struct ipack_device_id *id_table;
Jens Taproggee8011132012-09-04 17:01:17 +0200135 const struct ipack_driver_ops *ops;
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200136};
137
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200138/**
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
Jens Taprogge7b6ab332012-09-11 13:34:55 +0200145 * @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.
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200154 */
155struct ipack_bus_ops {
Jens Taproggec6e2dfa2012-09-13 12:32:21 +0200156 int (*request_irq) (struct ipack_device *dev,
Jens Taproggefaa75c42012-09-12 14:55:38 +0200157 irqreturn_t (*handler)(void *), void *arg);
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200158 int (*free_irq) (struct ipack_device *dev);
Jens Taprogge7b6ab332012-09-11 13:34:55 +0200159 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);
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200164};
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
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200172 * @ops: bus operations for the mezzanine drivers
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200173 */
174struct ipack_bus_device {
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200175 struct device *parent;
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200176 int slots;
177 int bus_nr;
Stephen Hemminger9869a932012-09-10 11:14:01 -0700178 const struct ipack_bus_ops *ops;
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200179};
180
181/**
182 * ipack_bus_register -- register a new ipack bus
183 *
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200184 * @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.
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200190 */
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200191struct ipack_bus_device *ipack_bus_register(struct device *parent, int slots,
Stephen Hemminger9869a932012-09-10 11:14:01 -0700192 const struct ipack_bus_ops *ops);
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200193
194/**
195 * ipack_bus_unregister -- unregister an ipack bus
196 */
197int ipack_bus_unregister(struct ipack_bus_device *bus);
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200198
199/**
Jens Taprogge26c295c2012-09-27 12:37:40 +0200200 * ipack_driver_register -- Register a new ipack device driver
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200201 *
202 * Called by a ipack driver to register itself as a driver
203 * that can manage ipack devices.
204 */
Stephen Hemminger9869a932012-09-10 11:14:01 -0700205int ipack_driver_register(struct ipack_driver *edrv, struct module *owner,
206 const char *name);
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200207void ipack_driver_unregister(struct ipack_driver *edrv);
208
209/**
Jens Taprogge1e917952012-09-27 12:37:26 +0200210 * ipack_device_register -- register an IPack device with the kernel
211 * @dev: the new device to register.
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200212 *
Jens Taprogge1e917952012-09-27 12:37:26 +0200213 * Register a new IPack device ("module" in IndustryPack jargon). The call
214 * is done by the carrier driver. The carrier should populate the fields
Jens Taproggea19ad7d2012-09-27 12:37:31 +0200215 * 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 registration.
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200218 *
Jens Taprogge1e917952012-09-27 12:37:26 +0200219 * Return zero on success or error code on failure.
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200220 */
Jens Taprogge1e917952012-09-27 12:37:26 +0200221int ipack_device_register(struct ipack_device *dev);
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200222void ipack_device_unregister(struct ipack_device *dev);
Jens Taprogge849e0ad2012-09-04 17:01:13 +0200223
224/**
225 * DEFINE_IPACK_DEVICE_TABLE - macro used to describe a IndustryPack table
226 * @_table: device table name
227 *
228 * This macro is used to create a struct ipack_device_id array (a device table)
229 * in a generic manner.
230 */
231#define DEFINE_IPACK_DEVICE_TABLE(_table) \
Bill Pembertond79251f2012-11-19 13:25:25 -0500232 const struct ipack_device_id _table[]
Jens Taprogge849e0ad2012-09-04 17:01:13 +0200233/**
234 * IPACK_DEVICE - macro used to describe a specific IndustryPack device
235 * @_format: the format version (currently either 1 or 2, 8 bit value)
236 * @vend: the 8 or 24 bit IndustryPack Vendor ID
237 * @dev: the 8 or 16 bit IndustryPack Device ID
238 *
239 * This macro is used to create a struct ipack_device_id that matches a specific
240 * device.
241 */
242#define IPACK_DEVICE(_format, vend, dev) \
243 .format = (_format), \
244 .vendor = (vend), \
245 .device = (dev)