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