Staging: sep: do something about all the printk macros
[cascardo/linux.git] / drivers / staging / sep / sep_ext_with_pci_driver.c
1 /*
2  *
3  *  sep_ext_with_pci_driver.c - Security Processor Driver
4  *  pci initialization functions
5  *
6  *  Copyright(c) 2009 Intel Corporation. All rights reserved.
7  *  Copyright(c) 2009 Discretix. All rights reserved.
8  *
9  *  This program is free software; you can redistribute it and/or modify it
10  *  under the terms of the GNU General Public License as published by the Free
11  *  Software Foundation; either version 2 of the License, or (at your option)
12  *  any later version.
13  *
14  *  This program is distributed in the hope that it will be useful, but WITHOUT
15  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
17  *  more details.
18  *
19  *  You should have received a copy of the GNU General Public License along with
20  *  this program; if not, write to the Free Software Foundation, Inc., 59
21  *  Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  *  CONTACTS:
24  *
25  *  Mark Allyn          mark.a.allyn@intel.com
26  *
27  *  CHANGES:
28  *
29  *  2009.06.26  Initial publish
30  *
31  */
32
33 #include <linux/init.h>
34 #include <linux/module.h>
35 #include <linux/fs.h>
36 #include <linux/cdev.h>
37 #include <linux/kdev_t.h>
38 #include <linux/semaphore.h>
39 #include <linux/mm.h>
40 #include <linux/poll.h>
41 #include <linux/wait.h>
42 #include <linux/ioctl.h>
43 #include <linux/ioport.h>
44 #include <linux/io.h>
45 #include <linux/interrupt.h>
46 #include <linux/pagemap.h>
47 #include <linux/pci.h>
48 #include <linux/firmware.h>
49 #include <linux/sched.h>
50 #include "sep_driver_hw_defs.h"
51 #include "sep_driver_config.h"
52 #include "sep_driver_api.h"
53 #include "sep_driver_ext_api.h"
54 #include "sep_dev.h"
55
56 #if SEP_DRIVER_ARM_DEBUG_MODE
57
58 #define  CRYS_SEP_ROM_length                  0x4000
59
60 #define  CRYS_SEP_ROM_start_address           0x8000C000UL
61
62 #define  CRYS_SEP_ROM_start_address_offset    0xC000UL
63
64 #define  SEP_ROM_BANK_register                0x80008420UL
65
66 #define  SEP_ROM_BANK_register_offset         0x8420UL
67
68 #define SEP_RAR_IO_MEM_REGION_START_ADDRESS   0x82000000
69
70 /* 2M size */
71 /* #define SEP_RAR_IO_MEM_REGION_SIZE            (1024*1024*2)
72
73 static unsigned long CRYS_SEP_ROM[] = {
74         #include "SEP_ROM_image.h"
75 };
76
77 #else
78 */
79
80 /*-------------
81  THOSE 2 definitions are specific to the board - must be
82  defined during integration
83 ---------------*/
84 #define SEP_RAR_IO_MEM_REGION_START_ADDRESS   0xFF0D0000
85
86 /* 2M size */
87
88 #endif /* SEP_DRIVER_ARM_DEBUG_MODE */
89
90 #define BASE_ADDRESS_FOR_SYSTEM 0xfffc0000
91 #define SEP_RAR_IO_MEM_REGION_SIZE 0x40000
92
93 irqreturn_t sep_inthandler(int irq , void* dev_id);
94
95 /* Keep this a single static object for now to keep the conversion easy */
96
97 static struct sep_device sep_instance;
98 struct sep_device *sep_dev = &sep_instance;
99
100 /* temporary */
101 unsigned long jiffies_future;
102
103 /*-----------------------------
104     private functions
105 --------------------------------*/
106
107 /*
108   function that is activated on the succesfull probe of the SEP device
109 */
110 static int __devinit sep_probe(struct pci_dev *pdev,
111   const struct pci_device_id *ent);
112
113 static struct pci_device_id sep_pci_id_tbl[] = {
114         { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x080c) },
115         { 0 }
116 };
117
118 MODULE_DEVICE_TABLE(pci, sep_pci_id_tbl);
119
120
121
122 /* field for registering driver to PCI device */
123 static struct pci_driver sep_pci_driver = {
124         .name = "sep_sec_driver",
125         .id_table = sep_pci_id_tbl,
126         .probe = sep_probe
127 };
128
129
130 /*
131   This functions locks the area of the resisnd and cache sep code
132 */
133 void sep_lock_cache_resident_area(void)
134 {
135         return;
136 }
137
138
139 /*
140   This functions copies the cache and resident from their source location into
141   destination memory, which is external to Linux VM and is given as
142    physical address
143 */
144 int sep_copy_cache_resident_to_area(unsigned long   src_cache_addr,
145                                 unsigned long   cache_size_in_bytes,
146                                 unsigned long   src_resident_addr,
147                                 unsigned long   resident_size_in_bytes,
148                                 unsigned long *dst_new_cache_addr_ptr,
149                                 unsigned long *dst_new_resident_addr_ptr)
150 {
151         /* resident address in user space */
152         unsigned long resident_addr;
153
154         /* cahce address in user space */
155         unsigned long cache_addr;
156
157         const struct firmware *fw;
158
159         char *cache_name = "cache.image.bin";
160         char *res_name =  "resident.image.bin";
161
162         /* error */
163         int error;
164
165         /*--------------------------------
166             CODE
167         -------------------------------------*/
168         error = 0;
169
170         edbg(
171           "SEP Driver:rar_virtual is %p\n",
172           sep_dev->rar_virtual_address);
173         edbg(
174           "SEP Driver:rar_physical is %08lx\n",
175           sep_dev->rar_physical_address);
176
177         sep_dev->rar_region_addr = (unsigned long)sep_dev->rar_virtual_address;
178
179         sep_dev->cache_physical_address = sep_dev->rar_physical_address;
180         sep_dev->cache_virtual_address = sep_dev->rar_virtual_address;
181
182         /* load cache */
183         error = request_firmware(&fw, cache_name, &sep_dev->sep_pci_dev_ptr->dev);
184         if (error) {
185                 edbg(
186                   "SEP Driver:cant request cache fw\n");
187                 goto end_function;
188         }
189
190         edbg(
191           "SEP Driver:cache data loc is %p\n",
192           (void *)fw->data);
193         edbg(
194           "SEP Driver:cache data size is %08Zx\n",
195           fw->size);
196
197         memcpy((void *)sep_dev->cache_virtual_address, (void *)fw->data, fw->size);
198
199         sep_dev->cache_size = fw->size;
200
201         cache_addr = (unsigned long)sep_dev->cache_virtual_address;
202
203         release_firmware(fw);
204
205         sep_dev->resident_physical_address = sep_dev->cache_physical_address
206                                                                 + sep_dev->cache_size;
207         sep_dev->resident_virtual_address = sep_dev->cache_virtual_address
208                                                                 + sep_dev->cache_size;
209
210         /* load resident */
211         error = request_firmware(&fw, res_name, &sep_dev->sep_pci_dev_ptr->dev);
212         if (error) {
213                 edbg(
214                   "SEP Driver:cant request res fw\n");
215                 goto end_function;
216         }
217
218         edbg(
219           "SEP Driver:res data loc is %p\n",
220           (void *)fw->data);
221         edbg(
222           "SEP Driver:res data size is %08Zx\n",
223           fw->size);
224
225         memcpy((void *)sep_dev->resident_virtual_address, (void *)fw->data, fw->size);
226
227         sep_dev->resident_size = fw->size;
228
229         release_firmware(fw);
230
231         resident_addr = (unsigned long)sep_dev->resident_virtual_address;
232
233         edbg(
234           "SEP Driver:resident_addr (physical )is %08lx\n",
235           sep_dev->resident_physical_address);
236         edbg(
237           "SEP Driver:cache_addr (physical) is %08lx\n",
238           sep_dev->cache_physical_address);
239
240         edbg(
241           "SEP Driver:resident_addr (logical )is %08lx\n",
242           resident_addr);
243         edbg(
244           "SEP Driver:cache_addr (logical) is %08lx\n",
245           cache_addr);
246
247         edbg(
248           "SEP Driver:resident_size is %08lx\n", sep_dev->resident_size);
249         edbg(
250           "SEP Driver:cache_size is %08lx\n", sep_dev->cache_size);
251
252
253
254         /* physical addresses */
255         *dst_new_cache_addr_ptr = sep_dev->cache_physical_address;
256         *dst_new_resident_addr_ptr = sep_dev->resident_physical_address;
257
258 end_function:
259
260         return error;
261 }
262
263 /*
264   This functions maps and allocates the
265   shared area on the  external RAM (device)
266   The input is shared_area_size - the size of the memory to
267   allocate. The outputs
268   are kernel_shared_area_addr_ptr - the kerenl
269   address of the mapped and allocated
270   shared area, and phys_shared_area_addr_ptr
271   - the physical address of the shared area
272 */
273 int sep_map_and_alloc_shared_area(unsigned long shared_area_size,
274                                 unsigned long *kernel_shared_area_addr_ptr,
275                                 unsigned long *phys_shared_area_addr_ptr)
276 {
277         // shared_virtual_address = ioremap_nocache(0xda00000,shared_area_size);
278         sep_dev->shared_virtual_address = kmalloc(shared_area_size, GFP_KERNEL);
279         if (!sep_dev->shared_virtual_address) {
280                 edbg(
281                   "sep_driver:shared memory kmalloc failed\n");
282                 return -1;
283         }
284
285         /* FIXME */
286         sep_dev->shared_physical_address = __pa(sep_dev->shared_virtual_address);
287         // shared_physical_address = 0xda00000;
288
289         *kernel_shared_area_addr_ptr = (unsigned long)sep_dev->shared_virtual_address;
290         /* set the physical address of the shared area */
291         *phys_shared_area_addr_ptr = sep_dev->shared_physical_address;
292
293         edbg(
294           "SEP Driver:shared_virtual_address is %p\n",
295         sep_dev->shared_virtual_address);
296         edbg(
297           "SEP Driver:shared_region_size is %08lx\n",
298         shared_area_size);
299         edbg(
300           "SEP Driver:shared_physical_addr is %08lx\n",
301         *phys_shared_area_addr_ptr);
302
303         return 0;
304 }
305
306 /*
307   This functions unmaps and deallocates the shared area
308   on the  external RAM (device)
309   The input is shared_area_size - the size of the memory to deallocate,kernel_
310   shared_area_addr_ptr - the kernel address of the mapped and allocated
311   shared area,phys_shared_area_addr_ptr - the physical address of
312   the shared area
313 */
314 void sep_unmap_and_free_shared_area(unsigned long   shared_area_size,
315                                         unsigned long   kernel_shared_area_addr,
316                                         unsigned long   phys_shared_area_addr)
317 {
318         kfree((void *)kernel_shared_area_addr);
319         return;
320 }
321
322 /*
323   This functions returns the physical address inside shared area according
324   to the virtual address. It can be either on the externa RAM device
325   (ioremapped), or on the system RAM
326   This implementation is for the external RAM
327 */
328 unsigned long sep_shared_area_virt_to_phys(unsigned long virt_address)
329 {
330         edbg(
331           "SEP Driver:sh virt to phys v %08lx\n",
332           virt_address);
333         edbg(
334           "SEP Driver:sh virt to phys p %08lx\n",
335           sep_dev->shared_physical_address
336           + (virt_address - (unsigned long)sep_dev->shared_virtual_address));
337
338         return (unsigned long)sep_dev->shared_physical_address +
339           (virt_address - (unsigned long)sep_dev->shared_virtual_address);
340 }
341
342 /*
343   This functions returns the virtual address inside shared area
344   according to the physical address. It can be either on the
345   externa RAM device (ioremapped), or on the system RAM This implementation
346   is for the external RAM
347 */
348 unsigned long sep_shared_area_phys_to_virt(unsigned long phys_address)
349 {
350         return (unsigned long)sep_dev->shared_virtual_address
351           + (phys_address - sep_dev->shared_physical_address);
352 }
353
354
355 /*
356   function that is activaed on the succesfull probe of the SEP device
357 */
358 static int __devinit sep_probe(struct pci_dev *pdev,
359                         const struct pci_device_id *ent)
360 {
361         /* error */
362         int error;
363
364         /*------------------------
365         CODE
366         ---------------------------*/
367
368         edbg(
369           "Sep pci probe starting\n");
370         error = 0;
371
372         /* enable the device */
373         error = pci_enable_device(pdev);
374         if (error) {
375                 edbg(
376                   "error enabling pci device\n");
377                 goto end_function;
378         }
379
380         /* set the pci dev pointer */
381         sep_dev->sep_pci_dev_ptr = pdev;
382
383         /* get the io memory start address */
384         sep_dev->io_memory_start_physical_address = pci_resource_start(pdev, 0);
385         if (!sep_dev->io_memory_start_physical_address) {
386                 edbg(
387                   "SEP Driver error pci resource start\n");
388                 goto end_function;
389         }
390
391         /* get the io memory end address */
392         sep_dev->io_memory_end_physical_address = pci_resource_end(pdev, 0);
393         if (!sep_dev->io_memory_end_physical_address) {
394                 edbg(
395                   "SEP Driver error pci resource end\n");
396                 goto end_function;
397         }
398
399         sep_dev->io_memory_size = sep_dev->io_memory_end_physical_address -
400                                 sep_dev->io_memory_start_physical_address + 1;
401
402         edbg(
403           "SEP Driver:io_memory_start_physical_address is %08lx\n",
404         sep_dev->io_memory_start_physical_address);
405
406         edbg(
407           "SEP Driver:io_memory_end_phyaical_address is %08lx\n",
408         sep_dev->io_memory_end_physical_address);
409
410         edbg(
411           "SEP Driver:io_memory_size is %08lx\n",
412         sep_dev->io_memory_size);
413
414         sep_dev->io_memory_start_virtual_address =
415           ioremap_nocache(sep_dev->io_memory_start_physical_address,
416           sep_dev->io_memory_size);
417         if (!sep_dev->io_memory_start_virtual_address) {
418                 edbg(
419                   "SEP Driver error ioremap of io memory\n");
420                 goto end_function;
421         }
422
423         edbg(
424           "SEP Driver:io_memory_start_virtual_address is %p\n",
425         sep_dev->io_memory_start_virtual_address);
426
427         sep_dev->reg_base_address = (void __iomem *)sep_dev->io_memory_start_virtual_address;
428
429
430         /* set up system base address and shared memory location */
431
432         sep_dev->rar_virtual_address = kmalloc(2 * SEP_RAR_IO_MEM_REGION_SIZE,
433           GFP_KERNEL);
434
435         if (!sep_dev->rar_virtual_address) {
436                 edbg(
437                   "SEP Driver:cant kmalloc rar\n");
438                 goto end_function;
439                 }
440         /* FIXME */
441         sep_dev->rar_physical_address = __pa(sep_dev->rar_virtual_address);
442
443         edbg(
444           "SEP Driver:rar_physical is %08lx\n",
445         sep_dev->rar_physical_address);
446
447         edbg(
448           "SEP Driver:rar_virtual is %p\n",
449         sep_dev->rar_virtual_address);
450
451
452 #if !SEP_DRIVER_POLLING_MODE
453
454         edbg(
455           "SEP Driver: about to write IMR and ICR REG_ADDR\n");
456
457         /* clear ICR register */
458         sep_write_reg(sep_dev, HW_HOST_ICR_REG_ADDR,
459           0xFFFFFFFF);
460
461         /* set the IMR register - open only GPR 2 */
462         sep_write_reg(sep_dev, HW_HOST_IMR_REG_ADDR,
463           (~(0x1 << 13)));
464
465         /* figure out our irq */
466         /* FIXME: */
467         error = pci_read_config_byte(pdev, PCI_INTERRUPT_LINE, (u8 *)&sep_dev->sep_irq);
468
469         edbg(
470           "SEP Driver: my irq is %d\n", sep_irq);
471
472         edbg(
473           "SEP Driver: about to call request_irq\n");
474         /* get the interrupt line */
475         error = request_irq(sep_irq, sep_inthandler, IRQF_SHARED,
476           "sep_driver", &sep_dev->reg_base_address);
477         if (error)
478                 goto end_function;
479
480         goto end_function;
481         edbg(
482           "SEP Driver: about to write IMR REG_ADDR");
483
484         /* set the IMR register - open only GPR 2 */
485         sep_write_reg(sep_dev, HW_HOST_IMR_REG_ADDR,
486           (~(0x1 << 13)));
487
488 #endif /* SEP_DRIVER_POLLING_MODE */
489
490 end_function:
491
492         return error;
493 }
494
495 /*
496   this function registers th driver to
497   the device subsystem( either PCI, USB, etc)
498 */
499 int sep_register_driver_to_device(void)
500 {
501         return pci_register_driver(&sep_pci_driver);
502 }
503
504
505
506 void sep_load_rom_code(void)
507 {
508 #if SEP_DRIVER_ARM_DEBUG_MODE
509         /* Index variables */
510         unsigned long i, k, j;
511         unsigned long regVal;
512         unsigned long Error;
513         unsigned long warning;
514
515         /* Loading ROM from SEP_ROM_image.h file */
516         k = sizeof(CRYS_SEP_ROM);
517
518         edbg(
519           "SEP Driver: DX_CC_TST_SepRomLoader start\n");
520
521         edbg(
522           "SEP Driver: k is %lu\n", k);
523         edbg(
524           "SEP Driver: sep_dev->reg_base_address is %p\n",
525           sep_dev->reg_base_address);
526         edbg(
527           "SEP Driver: CRYS_SEP_ROM_start_address_offset is %p\n",
528           CRYS_SEP_ROM_start_address_offset);
529
530         for (i = 0; i < 4; i++) {
531                 /* write bank */
532                 sep_write_reg(sep_dev, SEP_ROM_BANK_register_offset, i);
533
534                 for (j = 0; j < CRYS_SEP_ROM_length / 4; j++) {
535                         sep_write_reg(sep_dev,
536                           CRYS_SEP_ROM_start_address_offset + 4*j,
537                           CRYS_SEP_ROM[i * 0x1000 + j]);
538
539                         k = k - 4;
540
541                         if (k == 0) {
542                                 j = CRYS_SEP_ROM_length;
543                                 i = 4;
544                         }
545                 }
546         }
547
548         /* reset the SEP*/
549         sep_write_reg(sep_dev, HW_HOST_SEP_SW_RST_REG_ADDR, 0x1);
550
551         /* poll for SEP ROM boot finish */
552         do {
553                 retVal = sep_read_reg(sep_dev, HW_HOST_SEP_HOST_GPR3_REG_ADDR);
554         } while (!regVal);
555
556         edbg(
557           "SEP Driver: ROM polling ended\n");
558
559         switch (regVal) {
560         case 0x1:
561                 /* fatal error - read erro status from GPRO */
562                 Error = sep_read_reg(sep_dev, HW_HOST_SEP_HOST_GPR0_REG_ADDR);
563                 edbg(
564                   "SEP Driver: ROM polling case 1\n");
565                 break;
566         case 0x2:
567                 /* Boot First Phase ended  */
568                 warning = sep_read_reg(sep_dev, HW_HOST_SEP_HOST_GPR0_REG_ADDR);
569                 edbg(
570                   "SEP Driver: ROM polling case 2\n");
571                 break;
572         case 0x4:
573                 /* Cold boot ended successfully  */
574                 warning = sep_read_reg(sep_dev, HW_HOST_SEP_HOST_GPR0_REG_ADDR);
575                 edbg(
576                   "SEP Driver: ROM polling case 4\n");
577                 Error = 0;
578                 break;
579         case 0x8:
580                 /* Warmboot ended successfully */
581                 warning = sep_read_reg(sep_dev, HW_HOST_SEP_HOST_GPR0_REG_ADDR);
582                 edbg(
583                   "SEP Driver: ROM polling case 8\n");
584                 Error = 0;
585                 break;
586         case 0x10:
587                 /* ColdWarm boot ended successfully */
588                 warning = sep_read_reg(sep_dev, HW_HOST_SEP_HOST_GPR0_REG_ADDR);
589                 edbg(
590                   "SEP Driver: ROM polling case 16\n");
591                 Error = 0;
592                 break;
593         case 0x20:
594                 edbg(
595                   "SEP Driver: ROM polling case 32\n");
596                 break;
597         }
598
599 #endif
600 }
601