Staging: rar_register: renaming directory to rar_register
[pandora-kernel.git] / drivers / staging / rar_register / rar_register.c
1 #include <linux/init.h>
2 #include <linux/module.h>
3 #include <linux/fs.h>
4 #include <linux/cdev.h>
5 #include <linux/kdev_t.h>
6 #include <linux/semaphore.h>
7 #include <linux/mm.h>
8 #include <linux/poll.h>
9 #include <linux/wait.h>
10 #include <linux/ioctl.h>
11 #include <linux/ioport.h>
12 #include <linux/io.h>
13 #include <linux/interrupt.h>
14 #include <linux/pagemap.h>
15 #include <linux/pci.h>
16 #include <linux/firmware.h>
17 #include <linux/sched.h>
18 #include "rar_register.h"
19
20 /* The following defines are for the IPC process to retrieve RAR in */
21
22 /* === Lincroft Message Bus Interface === */
23 /* Message Control Register */
24 #define LNC_MCR_OFFSET 0xD0
25
26 /* Message Data Register */
27 #define LNC_MDR_OFFSET 0xD4
28
29 /* Message Opcodes */
30 #define LNC_MESSAGE_READ_OPCODE  0xD0
31 #define LNC_MESSAGE_WRITE_OPCODE 0xE0
32
33 /* Message Write Byte Enables */
34 #define LNC_MESSAGE_BYTE_WRITE_ENABLES 0xF
35
36 /* B-unit Port */
37 #define LNC_BUNIT_PORT 0x3
38
39 /* === Lincroft B-Unit Registers - Programmed by IA32 firmware === */
40 #define LNC_BRAR0L  0x10
41 #define LNC_BRAR0H  0x11
42 #define LNC_BRAR1L  0x12
43 #define LNC_BRAR1H  0x13
44
45 /* Reserved for SeP */
46 #define LNC_BRAR2L  0x14
47 #define LNC_BRAR2H  0x15
48
49
50 /* This structure is only used during module initialization. */
51 struct RAR_offsets {
52         int low; /* Register offset for low RAR physical address. */
53         int high; /* Register offset for high RAR physical address. */
54 };
55
56 struct pci_dev *rar_dev;
57 static uint32_t registered;
58
59 /* Moorestown supports three restricted access regions. */
60 #define MRST_NUM_RAR 3
61
62 struct RAR_address_struct rar_addr[MRST_NUM_RAR];
63
64 /* prototype for init */
65 static int __init rar_init_handler(void);
66 static void __exit rar_exit_handler(void);
67
68 /*
69   function that is activated on the successfull probe of the RAR device
70 */
71 static int __devinit rar_probe(struct pci_dev *pdev,
72                                const struct pci_device_id *ent);
73
74 static const struct pci_device_id rar_pci_id_tbl[] = {
75         { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x4110) },
76         { 0 }
77 };
78
79 MODULE_DEVICE_TABLE(pci, rar_pci_id_tbl);
80
81 /* field for registering driver to PCI device */
82 static struct pci_driver rar_pci_driver = {
83         .name = "rar_driver",
84         .id_table = rar_pci_id_tbl,
85         .probe = rar_probe
86 };
87
88 /* This function is used to retrieved RAR info using the IPC message
89    bus interface */
90 static int memrar_get_rar_addr(struct pci_dev *pdev,
91                                int offset,
92                                u32 *addr)
93 {
94         /*
95          * ======== The Lincroft Message Bus Interface ========
96          * Lincroft registers may be obtained from the PCI
97          * (the Host Bridge) using the Lincroft Message Bus
98          * Interface.  That message bus interface is generally
99          * comprised of two registers: a control register (MCR, 0xDO)
100          * and a data register (MDR, 0xD4).
101          *
102          * The MCR (message control register) format is the following:
103          *   1.  [31:24]: Opcode
104          *   2.  [23:16]: Port
105          *   3.  [15:8]: Register Offset
106          *   4.  [7:4]: Byte Enables (use 0xF to set all of these bits
107          *              to 1)
108          *   5.  [3:0]: reserved
109          *
110          *  Read (0xD0) and write (0xE0) opcodes are written to the
111          *  control register when reading and writing to Lincroft
112          *  registers, respectively.
113          *
114          *  We're interested in registers found in the Lincroft
115          *  B-unit.  The B-unit port is 0x3.
116          *
117          *  The six B-unit RAR register offsets we use are listed
118          *  earlier in this file.
119          *
120          *  Lastly writing to the MCR register requires the "Byte
121          *  enables" bits to be set to 1.  This may be achieved by
122          *  writing 0xF at bit 4.
123          *
124          * The MDR (message data register) format is the following:
125          *   1. [31:0]: Read/Write Data
126          *
127          *  Data being read from this register is only available after
128          *  writing the appropriate control message to the MCR
129          *  register.
130          *
131          *  Data being written to this register must be written before
132          *  writing the appropriate control message to the MCR
133          *  register.
134          */
135
136         int result = 0; /* result */
137         /* Construct control message */
138         u32 const message =
139                (LNC_MESSAGE_READ_OPCODE << 24)
140                | (LNC_BUNIT_PORT << 16)
141                | (offset << 8)
142                | (LNC_MESSAGE_BYTE_WRITE_ENABLES << 4);
143
144         printk(KERN_WARNING "rar- offset to LNC MSG is %x\n", offset);
145
146         if (addr == 0)
147                 return -EINVAL;
148
149         /* Send the control message */
150         result = pci_write_config_dword(pdev,
151                                         LNC_MCR_OFFSET,
152                                         message);
153
154         printk(KERN_WARNING "rar- result from send ctl register is %x\n",
155                result);
156
157         if (!result)
158                 result = pci_read_config_dword(pdev,
159                                                LNC_MDR_OFFSET,
160                                                addr);
161
162         printk(KERN_WARNING "rar- result from read data register is %x\n",
163                result);
164
165         printk(KERN_WARNING "rar- value read from data register is %x\n",
166                *addr);
167
168         if (result)
169                 return -1;
170         else
171                 return 0;
172 }
173
174 static int memrar_set_rar_addr(struct pci_dev *pdev,
175                                int offset,
176                                u32 addr)
177 {
178         /*
179          * ======== The Lincroft Message Bus Interface ========
180          * Lincroft registers may be obtained from the PCI
181          * (the Host Bridge) using the Lincroft Message Bus
182          * Interface.  That message bus interface is generally
183          * comprised of two registers: a control register (MCR, 0xDO)
184          * and a data register (MDR, 0xD4).
185          *
186          * The MCR (message control register) format is the following:
187          *   1.  [31:24]: Opcode
188          *   2.  [23:16]: Port
189          *   3.  [15:8]: Register Offset
190          *   4.  [7:4]: Byte Enables (use 0xF to set all of these bits
191          *              to 1)
192          *   5.  [3:0]: reserved
193          *
194          *  Read (0xD0) and write (0xE0) opcodes are written to the
195          *  control register when reading and writing to Lincroft
196          *  registers, respectively.
197          *
198          *  We're interested in registers found in the Lincroft
199          *  B-unit.  The B-unit port is 0x3.
200          *
201          *  The six B-unit RAR register offsets we use are listed
202          *  earlier in this file.
203          *
204          *  Lastly writing to the MCR register requires the "Byte
205          *  enables" bits to be set to 1.  This may be achieved by
206          *  writing 0xF at bit 4.
207          *
208          * The MDR (message data register) format is the following:
209          *   1. [31:0]: Read/Write Data
210          *
211          *  Data being read from this register is only available after
212          *  writing the appropriate control message to the MCR
213          *  register.
214          *
215          *  Data being written to this register must be written before
216          *  writing the appropriate control message to the MCR
217          *  register.
218          */
219
220         int result = 0; /* result */
221
222         /* Construct control message */
223         u32 const message =
224                (LNC_MESSAGE_WRITE_OPCODE << 24)
225                | (LNC_BUNIT_PORT << 16)
226                | (offset << 8)
227                | (LNC_MESSAGE_BYTE_WRITE_ENABLES << 4);
228
229         printk(KERN_WARNING "rar- offset to LNC MSG is %x\n", offset);
230
231         if (addr == 0)
232                 return -EINVAL;
233
234         /* Send the control message */
235         result = pci_write_config_dword(pdev,
236                                         LNC_MDR_OFFSET,
237                                         addr);
238
239         printk(KERN_WARNING "rar- result from send ctl register is %x\n",
240                result);
241
242         if (!result)
243                 result = pci_write_config_dword(pdev,
244                                                 LNC_MCR_OFFSET,
245                                                 message);
246
247         printk(KERN_WARNING "rar- result from write data register is %x\n",
248                result);
249
250         printk(KERN_WARNING "rar- value read to data register is %x\n",
251                addr);
252
253         if (result)
254                 return -1;
255         else
256                 return 0;
257 }
258
259 /*
260
261  * Initialize RAR parameters, such as physical addresses, etc.
262
263  */
264 static int memrar_init_rar_params(struct pci_dev *pdev)
265 {
266         struct RAR_offsets const offsets[] = {
267                { LNC_BRAR0L, LNC_BRAR0H },
268                { LNC_BRAR1L, LNC_BRAR1H },
269                { LNC_BRAR2L, LNC_BRAR2H }
270         };
271
272         size_t const num_offsets = sizeof(offsets) / sizeof(offsets[0]);
273         struct RAR_offsets const *end = offsets + num_offsets;
274         struct RAR_offsets const *i;
275         unsigned int n = 0;
276         int result = 0;
277
278         /* Retrieve RAR start and end physical addresses. */
279
280         /*
281          * Access the RAR registers through the Lincroft Message Bus
282          * Interface on PCI device: 00:00.0 Host bridge.
283          */
284
285         /* struct pci_dev *pdev = pci_get_bus_and_slot(0, PCI_DEVFN(0,0)); */
286
287         if (pdev == NULL)
288                 return -ENODEV;
289
290         for (i = offsets; i != end; ++i, ++n) {
291                 if (memrar_get_rar_addr(pdev,
292                                         (*i).low,
293                                         &(rar_addr[n].low)) != 0
294                     || memrar_get_rar_addr(pdev,
295                                            (*i).high,
296                                            &(rar_addr[n].high)) != 0) {
297                         result = -1;
298                         break;
299                 }
300         }
301
302         /* Done accessing the device. */
303         /* pci_dev_put(pdev); */
304
305         if (result == 0) {
306                 if (1) {
307                         size_t z;
308                         for (z = 0; z != MRST_NUM_RAR; ++z) {
309                                 printk(KERN_WARNING
310                                        "rar - BRAR[%Zd] physical address low\n"
311                                        "\tlow:  0x%08x\n"
312                                        "\thigh: 0x%08x\n",
313                                        z,
314                                        rar_addr[z].low,
315                                        rar_addr[z].high);
316                         }
317                 }
318         }
319
320         return result;
321 }
322
323 /*
324   function that is activated on the successfull probe of the RAR device
325 */
326 static int __devinit rar_probe(struct pci_dev *pdev,
327                                const struct pci_device_id *ent)
328 {
329         /* error */
330         int error;
331
332         /*------------------------
333         CODE
334         ---------------------------*/
335
336         DEBUG_PRINT_0(RAR_DEBUG_LEVEL_EXTENDED,
337           "Rar pci probe starting\n");
338         error = 0;
339
340         /* enable the device */
341         error = pci_enable_device(pdev);
342         if (error) {
343                 DEBUG_PRINT_0(RAR_DEBUG_LEVEL_EXTENDED,
344                   "error enabling pci device\n");
345                 goto end_function;
346         }
347
348         rar_dev = pdev;
349         registered = 1;
350
351         /* Initialize the RAR parameters, which have to be retrieved */
352         /* via the message bus service */
353         error = memrar_init_rar_params(rar_dev);
354
355         if (error) {
356                 DEBUG_PRINT_0(RAR_DEBUG_LEVEL_EXTENDED,
357                   "error getting RAR addresses device\n");
358                 registered = 0;
359                 goto end_function;
360                 }
361
362 end_function:
363
364         return error;
365 }
366
367 /*
368   this function registers the driver to
369   the device subsystem (either PCI, USB, etc)
370 */
371 static int __init rar_init_handler(void)
372 {
373         return pci_register_driver(&rar_pci_driver);
374 }
375
376 static void __exit rar_exit_handler(void)
377 {
378         pci_unregister_driver(&rar_pci_driver);
379 }
380
381 module_init(rar_init_handler);
382 module_exit(rar_exit_handler);
383
384 MODULE_LICENSE("GPL");
385
386
387 /* The get_rar_address function is used by other device drivers
388  * to obtain RAR address information on a RAR. It takes two
389  * parameter:
390  *
391  * int rar_index
392  * The rar_index is an index to the rar for which you wish to retrieve
393  * the address information.
394  * Values can be 0,1, or 2.
395  *
396  * struct RAR_address_struct is a pointer to a place to which the function
397  * can return the address structure for the RAR.
398  *
399  * The function returns a 0 upon success or a -1 if there is no RAR
400  * facility on this system.
401  */
402 int get_rar_address(int rar_index, struct RAR_address_struct *addresses)
403 {
404         if (registered && (rar_index < 3) && (rar_index >= 0)) {
405                 *addresses = rar_addr[rar_index];
406                 /* strip off lock bit information  */
407                 addresses->low = addresses->low & 0xfffffff0;
408                 addresses->high = addresses->high & 0xfffffff0;
409                 return 0;
410         } else
411                 return -ENODEV;
412 }
413 EXPORT_SYMBOL(get_rar_address);
414
415 /* The lock_rar function is used by other device drivers to lock an RAR.
416  * once an RAR is locked, it stays locked until the next system reboot.
417  * The function takes one parameter:
418  *
419  * int rar_index
420  * The rar_index is an index to the rar that you want to lock.
421  * Values can be 0,1, or 2.
422  *
423  * The function returns a 0 upon success or a -1 if there is no RAR
424  * facility on this system.
425  */
426 int lock_rar(int rar_index)
427 {
428         u32 working_addr;
429         int result;
430
431         if (registered && (rar_index < 3) && (rar_index >= 0)) {
432                 /* first make sure that lock bits are clear (this does lock) */
433                 working_addr = rar_addr[rar_index].low & 0xfffffff0;
434
435                 /* now send that value to the register using the IPC */
436                 result = memrar_set_rar_addr(rar_dev, rar_index, working_addr);
437                 return result;
438         } else
439                 return -ENODEV;
440 }