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);
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;
101 /* restricted access region */
102 static unsigned long rar_physical_address;
103 static void *rar_virtual_address;
105 /* shared memory region */
106 static unsigned long shared_physical_address;
107 static void *shared_virtual_address;
109 /* firmware regions */
110 static unsigned long cache_physical_address;
111 static unsigned long cache_size;
112 static void *cache_virtual_address;
114 static unsigned long resident_physical_address;
115 static unsigned long resident_size;
116 static void *resident_virtual_address;
118 /* device interrupt (as retrieved from PCI) */
122 unsigned long jiffies_future;
124 /*-----------------------------
126 --------------------------------*/
129 function that is activated on the succesfull probe of the SEP device
131 static int __devinit sep_probe(struct pci_dev *pdev,
132 const struct pci_device_id *ent);
134 static struct pci_device_id sep_pci_id_tbl[] = {
135 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x080c) },
139 MODULE_DEVICE_TABLE(pci, sep_pci_id_tbl);
141 static unsigned long rar_region_addr;
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,
151 /* pointer to pci dev received during probe */
152 struct pci_dev *sep_pci_dev_ptr;
155 This functions locks the area of the resisnd and cache sep code
157 void sep_lock_cache_resident_area(void)
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
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)
175 /* resident address in user space */
176 unsigned long resident_addr;
178 /* cahce address in user space */
179 unsigned long cache_addr;
181 const struct firmware *fw;
183 char *cache_name = "cache.image.bin";
184 char *res_name = "resident.image.bin";
189 /*--------------------------------
191 -------------------------------------*/
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);
201 rar_region_addr = (unsigned long)rar_virtual_address;
203 cache_physical_address = rar_physical_address;
204 cache_virtual_address = rar_virtual_address;
207 error = request_firmware(&fw, cache_name, &sep_pci_dev_ptr->dev);
209 DEBUG_PRINT_0(SEP_DEBUG_LEVEL_EXTENDED,
210 "SEP Driver:cant request cache fw\n");
214 DEBUG_PRINT_1(SEP_DEBUG_LEVEL_EXTENDED,
215 "SEP Driver:cache data loc is %p\n",
217 DEBUG_PRINT_1(SEP_DEBUG_LEVEL_EXTENDED,
218 "SEP Driver:cache data size is %08Zx\n",
221 memcpy((void *)cache_virtual_address, (void *)fw->data, fw->size);
223 cache_size = fw->size;
225 cache_addr = (unsigned long)cache_virtual_address;
227 release_firmware(fw);
229 resident_physical_address = cache_physical_address+cache_size;
230 resident_virtual_address = cache_virtual_address+cache_size;
233 error = request_firmware(&fw, res_name, &sep_pci_dev_ptr->dev);
235 DEBUG_PRINT_0(SEP_DEBUG_LEVEL_EXTENDED,
236 "SEP Driver:cant request res fw\n");
240 DEBUG_PRINT_1(SEP_DEBUG_LEVEL_EXTENDED,
241 "SEP Driver:res data loc is %p\n",
243 DEBUG_PRINT_1(SEP_DEBUG_LEVEL_EXTENDED,
244 "SEP Driver:res data size is %08Zx\n",
247 memcpy((void *)resident_virtual_address, (void *)fw->data, fw->size);
249 resident_size = fw->size;
251 release_firmware(fw);
253 resident_addr = (unsigned long)resident_virtual_address;
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);
262 DEBUG_PRINT_1(SEP_DEBUG_LEVEL_EXTENDED,
263 "SEP Driver:resident_addr (logical )is %08lx\n",
265 DEBUG_PRINT_1(SEP_DEBUG_LEVEL_EXTENDED,
266 "SEP Driver:cache_addr (logical) is %08lx\n",
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);
276 /* physical addresses */
277 *dst_new_cache_addr_ptr = cache_physical_address;
278 *dst_new_resident_addr_ptr = resident_physical_address;
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
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)
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");
307 shared_physical_address = __pa(shared_virtual_address);
308 // shared_physical_address = 0xda00000;
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;
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",
320 DEBUG_PRINT_1(SEP_DEBUG_LEVEL_EXTENDED,
321 "SEP Driver:shared_physical_addr is %08lx\n",
322 *phys_shared_area_addr_ptr);
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
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)
339 kfree((void *)kernel_shared_area_addr);
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
349 unsigned long sep_shared_area_virt_to_phys(unsigned long virt_address)
351 DEBUG_PRINT_1(SEP_DEBUG_LEVEL_EXTENDED,
352 "SEP Driver:sh virt to phys v %08lx\n",
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));
359 return (unsigned long)shared_physical_address +
360 (virt_address - (unsigned long)shared_virtual_address);
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
369 unsigned long sep_shared_area_phys_to_virt(unsigned long phys_address)
371 return (unsigned long)shared_virtual_address
372 + (phys_address - shared_physical_address);
377 function that is activaed on the succesfull probe of the SEP device
379 static int __devinit sep_probe(struct pci_dev *pdev,
380 const struct pci_device_id *ent)
385 /*------------------------
387 ---------------------------*/
389 DEBUG_PRINT_0(SEP_DEBUG_LEVEL_EXTENDED,
390 "Sep pci probe starting\n");
393 /* enable the device */
394 error = pci_enable_device(pdev);
396 DEBUG_PRINT_0(SEP_DEBUG_LEVEL_EXTENDED,
397 "error enabling pci device\n");
401 /* set the pci dev pointer */
402 sep_pci_dev_ptr = pdev;
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");
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");
420 io_memory_size = io_memory_end_physical_address -
421 io_memory_start_physical_address + 1;
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);
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);
431 DEBUG_PRINT_1(SEP_DEBUG_LEVEL_EXTENDED,
432 "SEP Driver:io_memory_size is %08lx\n",
435 io_memory_start_virtual_address =
436 ioremap_nocache(io_memory_start_physical_address,
438 if (!io_memory_start_virtual_address) {
439 DEBUG_PRINT_0(SEP_DEBUG_LEVEL_EXTENDED,
440 "SEP Driver error ioremap of io memory\n");
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);
448 g_sep_reg_base_address = (unsigned long)io_memory_start_virtual_address;
451 /* set up system base address and shared memory location */
453 rar_virtual_address = kmalloc(2 * SEP_RAR_IO_MEM_REGION_SIZE,
456 if (!rar_virtual_address) {
457 DEBUG_PRINT_0(SEP_DEBUG_LEVEL_EXTENDED,
458 "SEP Driver:cant kmalloc rar\n");
462 rar_physical_address = __pa(rar_virtual_address);
464 DEBUG_PRINT_1(SEP_DEBUG_LEVEL_EXTENDED,
465 "SEP Driver:rar_physical is %08lx\n",
466 rar_physical_address);
468 DEBUG_PRINT_1(SEP_DEBUG_LEVEL_EXTENDED,
469 "SEP Driver:rar_virtual is %p\n",
470 rar_virtual_address);
473 #if !SEP_DRIVER_POLLING_MODE
475 DEBUG_PRINT_0(SEP_DEBUG_LEVEL_EXTENDED,
476 "SEP Driver: about to write IMR and ICR REG_ADDR\n");
478 /* clear ICR register */
479 SEP_WRITE_REGISTER(g_sep_reg_base_address + HW_HOST_ICR_REG_ADDR,
482 /* set the IMR register - open only GPR 2 */
483 SEP_WRITE_REGISTER(g_sep_reg_base_address + HW_HOST_IMR_REG_ADDR,
486 /* figure out our irq */
487 error = pci_read_config_byte(pdev, PCI_INTERRUPT_LINE, (u8 *)&sep_irq);
489 DEBUG_PRINT_1(SEP_DEBUG_LEVEL_EXTENDED,
490 "SEP Driver: my irq is %d\n", sep_irq);
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);
501 DEBUG_PRINT_0(SEP_DEBUG_LEVEL_EXTENDED,
502 "SEP Driver: about to write IMR REG_ADDR");
504 /* set the IMR register - open only GPR 2 */
505 SEP_WRITE_REGISTER(g_sep_reg_base_address + HW_HOST_IMR_REG_ADDR,
508 #endif /* SEP_DRIVER_POLLING_MODE */
516 this function registers th driver to
517 the device subsystem( either PCI, USB, etc)
519 int sep_register_driver_to_device(void)
521 return pci_register_driver(&sep_pci_driver);
526 void sep_load_rom_code()
528 #if SEP_DRIVER_ARM_DEBUG_MODE
529 /* Index variables */
530 unsigned long i, k, j;
531 unsigned long regVal;
533 unsigned long warning;
535 /* Loading ROM from SEP_ROM_image.h file */
536 k = sizeof(CRYS_SEP_ROM);
538 DEBUG_PRINT_0(SEP_DEBUG_LEVEL_EXTENDED,
539 "SEP Driver: DX_CC_TST_SepRomLoader start\n");
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);
550 for (i = 0; i < 4; i++) {
552 SEP_WRITE_REGISTER(g_sep_reg_base_address
553 + SEP_ROM_BANK_register_offset, i);
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]);
563 j = CRYS_SEP_ROM_length;
570 SEP_WRITE_REGISTER(g_sep_reg_base_address
571 + HW_HOST_SEP_SW_RST_REG_ADDR, 0x1);
573 /* poll for SEP ROM boot finish */
575 SEP_READ_REGISTER(g_sep_reg_base_address
576 + HW_HOST_SEP_HOST_GPR3_REG_ADDR, regVal);
579 DEBUG_PRINT_0(SEP_DEBUG_LEVEL_EXTENDED,
580 "SEP Driver: ROM polling ended\n");
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");
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");
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");
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");
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");
622 DEBUG_PRINT_0(SEP_DEBUG_LEVEL_EXTENDED,
623 "SEP Driver: ROM polling case 32\n");