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