blob: 6760bfaf0ac4623fcca3c63d5e27d8cb80928c77 [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
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,
Jens Taprogge84a08fa2012-09-27 12:37:29 +020036 IPACK_ID_SPACE,
Jens Taproggee4af9492012-09-13 12:32:19 +020037 IPACK_INT_SPACE,
Jens Taproggefe4a3ed2012-09-27 12:37:36 +020038 IPACK_MEM8_SPACE,
Jens Taprogge48a97352012-09-27 12:37:37 +020039 IPACK_MEM16_SPACE,
Jens Taprogge84a08fa2012-09-27 12:37:29 +020040 /* Dummy for counting the number of entries. Must remain the last
41 * entry */
42 IPACK_SPACE_COUNT,
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020043};
44
45/**
Jens Taproggebb29ab82012-09-27 12:37:28 +020046 */
47struct ipack_region {
48 phys_addr_t start;
49 size_t size;
50};
51
52/**
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020053 * struct ipack_device
54 *
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020055 * @slot: Slot where the device is plugged in the carrier board
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +020056 * @bus: ipack_bus_device where the device is plugged to.
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020057 * @id_space: Virtual address to ID space.
58 * @io_space: Virtual address to IO space.
59 * @mem_space: Virtual address to MEM space.
60 * @dev: device in kernel representation.
61 *
62 * Warning: Direct access to mapped memory is possible but the endianness
63 * is not the same with PCI carrier or VME carrier. The endianness is managed
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +020064 * by the carrier board throught bus->ops.
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020065 */
66struct ipack_device {
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020067 unsigned int slot;
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +020068 struct ipack_bus_device *bus;
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020069 struct device dev;
Jens Taprogge1e917952012-09-27 12:37:26 +020070 void (*release) (struct ipack_device *dev);
Jens Taproggea19ad7d2012-09-27 12:37:31 +020071 struct ipack_region region[IPACK_SPACE_COUNT];
Jens Taproggee8ed3272012-09-04 17:01:15 +020072 u8 *id;
Jens Taprogge187e4782012-09-04 17:01:14 +020073 size_t id_avail;
Jens Taproggee8ed3272012-09-04 17:01:15 +020074 u32 id_vendor;
75 u32 id_device;
Jens Taprogge187e4782012-09-04 17:01:14 +020076 u8 id_format;
Jens Taproggea92caeb2012-09-11 13:35:03 +020077 unsigned int id_crc_correct:1;
Jens Taprogge0b0f3a12012-09-11 13:34:57 +020078 unsigned int speed_8mhz:1;
79 unsigned int speed_32mhz:1;
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020080};
81
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +020082/**
Jens Taprogge26c295c2012-09-27 12:37:40 +020083 * struct ipack_driver_ops -- Callbacks to IPack device driver
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020084 *
Jens Taprogge26c295c2012-09-27 12:37:40 +020085 * @probe: Probe function
86 * @remove: Prepare imminent removal of the device. Services provided by the
87 * device should be revoked.
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020088 */
89
90struct ipack_driver_ops {
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020091 int (*probe) (struct ipack_device *dev);
92 void (*remove) (struct ipack_device *dev);
93};
94
95/**
Jens Taprogge26c295c2012-09-27 12:37:40 +020096 * struct ipack_driver -- Specific data to each ipack device driver
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020097 *
Jens Taprogge26c295c2012-09-27 12:37:40 +020098 * @driver: Device driver kernel representation
99 * @ops: Callbacks provided by the IPack device driver
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200100 */
101struct ipack_driver {
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200102 struct device_driver driver;
Jens Taprogge849e0ad2012-09-04 17:01:13 +0200103 const struct ipack_device_id *id_table;
Jens Taproggee8011132012-09-04 17:01:17 +0200104 const struct ipack_driver_ops *ops;
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200105};
106
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200107/**
108 * struct ipack_bus_ops - available operations on a bridge module
109 *
110 * @map_space: map IP address space
111 * @unmap_space: unmap IP address space
112 * @request_irq: request IRQ
113 * @free_irq: free IRQ
Jens Taprogge7b6ab332012-09-11 13:34:55 +0200114 * @get_clockrate: Returns the clockrate the carrier is currently
115 * communicating with the device at.
116 * @set_clockrate: Sets the clock-rate for carrier / module communication.
117 * Should return -EINVAL if the requested speed is not supported.
118 * @get_error: Returns the error state for the slot the device is attached
119 * to.
120 * @get_timeout: Returns 1 if the communication with the device has
121 * previously timed out.
122 * @reset_timeout: Resets the state returned by get_timeout.
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200123 */
124struct ipack_bus_ops {
Jens Taproggec6e2dfa2012-09-13 12:32:21 +0200125 int (*request_irq) (struct ipack_device *dev,
Jens Taproggefaa75c42012-09-12 14:55:38 +0200126 irqreturn_t (*handler)(void *), void *arg);
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200127 int (*free_irq) (struct ipack_device *dev);
Jens Taprogge7b6ab332012-09-11 13:34:55 +0200128 int (*get_clockrate) (struct ipack_device *dev);
129 int (*set_clockrate) (struct ipack_device *dev, int mherz);
130 int (*get_error) (struct ipack_device *dev);
131 int (*get_timeout) (struct ipack_device *dev);
132 int (*reset_timeout) (struct ipack_device *dev);
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200133};
134
135/**
136 * struct ipack_bus_device
137 *
138 * @dev: pointer to carrier device
139 * @slots: number of slots available
140 * @bus_nr: ipack bus number
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200141 * @ops: bus operations for the mezzanine drivers
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200142 */
143struct ipack_bus_device {
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200144 struct device *parent;
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200145 int slots;
146 int bus_nr;
Stephen Hemminger9869a932012-09-10 11:14:01 -0700147 const struct ipack_bus_ops *ops;
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200148};
149
150/**
151 * ipack_bus_register -- register a new ipack bus
152 *
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200153 * @parent: pointer to the parent device, if any.
154 * @slots: number of slots available in the bus device.
155 * @ops: bus operations for the mezzanine drivers.
156 *
157 * The carrier board device should call this function to register itself as
158 * available bus device in ipack.
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200159 */
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200160struct ipack_bus_device *ipack_bus_register(struct device *parent, int slots,
Stephen Hemminger9869a932012-09-10 11:14:01 -0700161 const struct ipack_bus_ops *ops);
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200162
163/**
164 * ipack_bus_unregister -- unregister an ipack bus
165 */
166int ipack_bus_unregister(struct ipack_bus_device *bus);
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200167
168/**
Jens Taprogge26c295c2012-09-27 12:37:40 +0200169 * ipack_driver_register -- Register a new ipack device driver
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200170 *
171 * Called by a ipack driver to register itself as a driver
172 * that can manage ipack devices.
173 */
Stephen Hemminger9869a932012-09-10 11:14:01 -0700174int ipack_driver_register(struct ipack_driver *edrv, struct module *owner,
175 const char *name);
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200176void ipack_driver_unregister(struct ipack_driver *edrv);
177
178/**
Jens Taprogge1e917952012-09-27 12:37:26 +0200179 * ipack_device_register -- register an IPack device with the kernel
180 * @dev: the new device to register.
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200181 *
Jens Taprogge1e917952012-09-27 12:37:26 +0200182 * Register a new IPack device ("module" in IndustryPack jargon). The call
183 * is done by the carrier driver. The carrier should populate the fields
Jens Taproggea19ad7d2012-09-27 12:37:31 +0200184 * bus and slot as well as the region array of @dev prior to calling this
185 * function. The rest of the fields will be allocated and populated
186 * during registration.
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200187 *
Jens Taprogge1e917952012-09-27 12:37:26 +0200188 * Return zero on success or error code on failure.
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200189 */
Jens Taprogge1e917952012-09-27 12:37:26 +0200190int ipack_device_register(struct ipack_device *dev);
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200191void ipack_device_unregister(struct ipack_device *dev);
Jens Taprogge849e0ad2012-09-04 17:01:13 +0200192
193/**
194 * DEFINE_IPACK_DEVICE_TABLE - macro used to describe a IndustryPack table
195 * @_table: device table name
196 *
197 * This macro is used to create a struct ipack_device_id array (a device table)
198 * in a generic manner.
199 */
200#define DEFINE_IPACK_DEVICE_TABLE(_table) \
201 const struct ipack_device_id _table[] __devinitconst
202
203/**
204 * IPACK_DEVICE - macro used to describe a specific IndustryPack device
205 * @_format: the format version (currently either 1 or 2, 8 bit value)
206 * @vend: the 8 or 24 bit IndustryPack Vendor ID
207 * @dev: the 8 or 16 bit IndustryPack Device ID
208 *
209 * This macro is used to create a struct ipack_device_id that matches a specific
210 * device.
211 */
212#define IPACK_DEVICE(_format, vend, dev) \
213 .format = (_format), \
214 .vendor = (vend), \
215 .device = (dev)