3 * sep_ext_with_pci_driver.c - Security Processor Driver
4 * pci initialization functions
6 * Copyright(c) 2009 Intel Corporation. All rights reserved.
7 * Copyright(c) 2009 Discretix. All rights reserved.
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)
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
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.
25 * Mark Allyn mark.a.allyn@intel.com
29 * 2009.06.26 Initial publish
33 #include <linux/init.h>
34 #include <linux/module.h>
36 #include <linux/cdev.h>
37 #include <linux/kdev_t.h>
38 #include <linux/semaphore.h>
40 #include <linux/poll.h>
41 #include <linux/wait.h>
42 #include <linux/ioctl.h>
43 #include <linux/ioport.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"
55 #if SEP_DRIVER_ARM_DEBUG_MODE
57 #define CRYS_SEP_ROM_length 0x4000
59 #define CRYS_SEP_ROM_start_address 0x8000C000UL
61 #define CRYS_SEP_ROM_start_address_offset 0xC000UL
63 #define SEP_ROM_BANK_register 0x80008420UL
65 #define SEP_ROM_BANK_register_offset 0x8420UL
67 #define SEP_RAR_IO_MEM_REGION_START_ADDRESS 0x82000000
70 /* #define SEP_RAR_IO_MEM_REGION_SIZE (1024*1024*2)
72 static unsigned long CRYS_SEP_ROM[] = {
73 #include "SEP_ROM_image.h"
80 THOSE 2 definitions are specific to the board - must be
81 defined during integration
83 #define SEP_RAR_IO_MEM_REGION_START_ADDRESS 0xFF0D0000
87 #endif /* SEP_DRIVER_ARM_DEBUG_MODE */
89 #define BASE_ADDRESS_FOR_SYSTEM 0xfffc0000
90 #define SEP_RAR_IO_MEM_REGION_SIZE 0x40000
92 irqreturn_t sep_inthandler(int irq , void* dev_id);
94 /* NOTE - must be defined specific to the board */
95 #define VENDOR_ID 0x8086
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;
103 /* restricted access region */
104 static unsigned long rar_physical_address;
105 static void *rar_virtual_address;
107 /* shared memory region */
108 static unsigned long shared_physical_address;
109 static void *shared_virtual_address;
111 /* firmware regions */
112 static unsigned long cache_physical_address;
113 static unsigned long cache_size;
114 static void *cache_virtual_address;
116 static unsigned long resident_physical_address;
117 static unsigned long resident_size;
118 static void *resident_virtual_address;
120 /* device interrupt (as retrieved from PCI) */
124 unsigned long jiffies_future;
126 /*-----------------------------
128 --------------------------------*/
131 function that is activated on the succesfull probe of the SEP device
133 static int __devinit sep_probe(struct pci_dev *pdev,
134 const struct pci_device_id *ent);
136 static struct pci_device_id sep_pci_id_tbl[] = {
137 { PCI_DEVICE(VENDOR_ID, 0x080c) },
141 MODULE_DEVICE_TABLE(pci, sep_pci_id_tbl);
143 static unsigned long rar_region_addr;
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,
153 /* pointer to pci dev received during probe */
154 struct pci_dev *sep_pci_dev_ptr;
157 This functions locks the area of the resisnd and cache sep code
159 void sep_lock_cache_resident_area(void)
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
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)
177 /* resident address in user space */
178 unsigned long resident_addr;
180 /* cahce address in user space */
181 unsigned long cache_addr;
183 const struct firmware *fw;
185 char *cache_name = "cache.image.bin";
186 char *res_name = "resident.image.bin";
191 /*--------------------------------
193 -------------------------------------*/
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);
203 rar_region_addr = (unsigned long)rar_virtual_address;
205 cache_physical_address = rar_physical_address;
206 cache_virtual_address = rar_virtual_address;
209 error = request_firmware(&fw, cache_name, &sep_pci_dev_ptr->dev);
211 DEBUG_PRINT_0(SEP_DEBUG_LEVEL_EXTENDED,
212 "SEP Driver:cant request cache fw\n");
216 DEBUG_PRINT_1(SEP_DEBUG_LEVEL_EXTENDED,
217 "SEP Driver:cache data loc is %p\n",
219 DEBUG_PRINT_1(SEP_DEBUG_LEVEL_EXTENDED,
220 "SEP Driver:cache data size is %08x\n",
223 memcpy((void *)cache_virtual_address, (void *)fw->data, fw->size);
225 cache_size = fw->size;
227 cache_addr = (unsigned long)cache_virtual_address;
229 release_firmware(fw);
231 resident_physical_address = cache_physical_address+cache_size;
232 resident_virtual_address = cache_virtual_address+cache_size;
235 error = request_firmware(&fw, res_name, &sep_pci_dev_ptr->dev);
237 DEBUG_PRINT_0(SEP_DEBUG_LEVEL_EXTENDED,
238 "SEP Driver:cant request res fw\n");
242 DEBUG_PRINT_1(SEP_DEBUG_LEVEL_EXTENDED,
243 "SEP Driver:res data loc is %p\n",
245 DEBUG_PRINT_1(SEP_DEBUG_LEVEL_EXTENDED,
246 "SEP Driver:res data size is %08x\n",
249 memcpy((void *)resident_virtual_address, (void *)fw->data, fw->size);
251 resident_size = fw->size;
253 release_firmware(fw);
255 resident_addr = (unsigned long)resident_virtual_address;
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);
264 DEBUG_PRINT_1(SEP_DEBUG_LEVEL_EXTENDED,
265 "SEP Driver:resident_addr (logical )is %08lx\n",
267 DEBUG_PRINT_1(SEP_DEBUG_LEVEL_EXTENDED,
268 "SEP Driver:cache_addr (logical) is %08lx\n",
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);
278 /* physical addresses */
279 *dst_new_cache_addr_ptr = cache_physical_address;
280 *dst_new_resident_addr_ptr = resident_physical_address;
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
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)
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");
309 shared_physical_address = __pa(shared_virtual_address);
310 // shared_physical_address = 0xda00000;
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;
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",
322 DEBUG_PRINT_1(SEP_DEBUG_LEVEL_EXTENDED,
323 "SEP Driver:shared_physical_addr is %08lx\n",
324 *phys_shared_area_addr_ptr);
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
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)
341 kfree((void *)kernel_shared_area_addr);
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
351 unsigned long sep_shared_area_virt_to_phys(unsigned long virt_address)
353 DEBUG_PRINT_1(SEP_DEBUG_LEVEL_EXTENDED,
354 "SEP Driver:sh virt to phys v %08lx\n",
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));
361 return (unsigned long)shared_physical_address +
362 (virt_address - (unsigned long)shared_virtual_address);
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
371 unsigned long sep_shared_area_phys_to_virt(unsigned long phys_address)
373 return (unsigned long)shared_virtual_address
374 + (phys_address - shared_physical_address);
379 function that is activaed on the succesfull probe of the SEP device
381 static int __devinit sep_probe(struct pci_dev *pdev,
382 const struct pci_device_id *ent)
387 /*------------------------
389 ---------------------------*/
391 DEBUG_PRINT_0(SEP_DEBUG_LEVEL_EXTENDED,
392 "Sep pci probe starting\n");
395 /* enable the device */
396 error = pci_enable_device(pdev);
398 DEBUG_PRINT_0(SEP_DEBUG_LEVEL_EXTENDED,
399 "error enabling pci device\n");
403 /* set the pci dev pointer */
404 sep_pci_dev_ptr = pdev;
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");
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");
422 io_memory_size = io_memory_end_physical_address -
423 io_memory_start_physical_address + 1;
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);
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);
433 DEBUG_PRINT_1(SEP_DEBUG_LEVEL_EXTENDED,
434 "SEP Driver:io_memory_size is %08lx\n",
437 io_memory_start_virtual_address =
438 ioremap_nocache(io_memory_start_physical_address,
440 if (!io_memory_start_virtual_address) {
441 DEBUG_PRINT_0(SEP_DEBUG_LEVEL_EXTENDED,
442 "SEP Driver error ioremap of io memory\n");
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);
450 g_sep_reg_base_address = (unsigned long)io_memory_start_virtual_address;
453 /* set up system base address and shared memory location */
455 rar_virtual_address = kmalloc(2 * SEP_RAR_IO_MEM_REGION_SIZE,
458 if (!rar_virtual_address) {
459 DEBUG_PRINT_0(SEP_DEBUG_LEVEL_EXTENDED,
460 "SEP Driver:cant kmalloc rar\n");
464 rar_physical_address = __pa(rar_virtual_address);
466 DEBUG_PRINT_1(SEP_DEBUG_LEVEL_EXTENDED,
467 "SEP Driver:rar_physical is %08lx\n",
468 rar_physical_address);
470 DEBUG_PRINT_1(SEP_DEBUG_LEVEL_EXTENDED,
471 "SEP Driver:rar_virtual is %p\n",
472 rar_virtual_address);
475 #if !SEP_DRIVER_POLLING_MODE
477 DEBUG_PRINT_0(SEP_DEBUG_LEVEL_EXTENDED,
478 "SEP Driver: about to write IMR and ICR REG_ADDR\n");
480 /* clear ICR register */
481 SEP_WRITE_REGISTER(g_sep_reg_base_address + HW_HOST_ICR_REG_ADDR,
484 /* set the IMR register - open only GPR 2 */
485 SEP_WRITE_REGISTER(g_sep_reg_base_address + HW_HOST_IMR_REG_ADDR,
488 /* figure out our irq */
489 error = pci_read_config_byte(pdev, PCI_INTERRUPT_LINE, (u8 *)&sep_irq);
491 DEBUG_PRINT_1(SEP_DEBUG_LEVEL_EXTENDED,
492 "SEP Driver: my irq is %d\n", sep_irq);
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);
503 DEBUG_PRINT_0(SEP_DEBUG_LEVEL_EXTENDED,
504 "SEP Driver: about to write IMR REG_ADDR");
506 /* set the IMR register - open only GPR 2 */
507 SEP_WRITE_REGISTER(g_sep_reg_base_address + HW_HOST_IMR_REG_ADDR,
510 #endif /* SEP_DRIVER_POLLING_MODE */
518 this function registers th driver to
519 the device subsystem( either PCI, USB, etc)
521 int sep_register_driver_to_device(void)
523 return pci_register_driver(&sep_pci_driver);
528 void sep_load_rom_code()
530 #if SEP_DRIVER_ARM_DEBUG_MODE
531 /* Index variables */
532 unsigned long i, k, j;
533 unsigned long regVal;
535 unsigned long warning;
537 /* Loading ROM from SEP_ROM_image.h file */
538 k = sizeof(CRYS_SEP_ROM);
540 DEBUG_PRINT_0(SEP_DEBUG_LEVEL_EXTENDED,
541 "SEP Driver: DX_CC_TST_SepRomLoader start\n");
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);
552 for (i = 0; i < 4; i++) {
554 SEP_WRITE_REGISTER(g_sep_reg_base_address
555 + SEP_ROM_BANK_register_offset, i);
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]);
565 j = CRYS_SEP_ROM_length;
572 SEP_WRITE_REGISTER(g_sep_reg_base_address
573 + HW_HOST_SEP_SW_RST_REG_ADDR, 0x1);
575 /* poll for SEP ROM boot finish */
577 SEP_READ_REGISTER(g_sep_reg_base_address
578 + HW_HOST_SEP_HOST_GPR3_REG_ADDR, regVal);
581 DEBUG_PRINT_0(SEP_DEBUG_LEVEL_EXTENDED,
582 "SEP Driver: ROM polling ended\n");
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");
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");
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");
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");
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");
624 DEBUG_PRINT_0(SEP_DEBUG_LEVEL_EXTENDED,
625 "SEP Driver: ROM polling case 32\n");