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