2 * Compaq Hot Plug Controller Driver
4 * Copyright (C) 1995,2001 Compaq Computer Corporation
5 * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
6 * Copyright (C) 2001 IBM Corp.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or (at
13 * your option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
18 * NON INFRINGEMENT. See the GNU General Public License for more
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * Send feedback to <greg@kroah.com>
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/types.h>
32 #include <linux/proc_fs.h>
33 #include <linux/slab.h>
34 #include <linux/workqueue.h>
35 #include <linux/pci.h>
36 #include <linux/init.h>
37 #include <asm/uaccess.h>
39 #include "cpqphp_nvram.h"
42 #define ROM_INT15_PHY_ADDR 0x0FF859
43 #define READ_EV 0xD8A4
44 #define WRITE_EV 0xD8A5
48 unsigned long lword; /* eax */
49 unsigned short word; /* ax */
52 unsigned char low; /* al */
53 unsigned char high; /* ah */
57 unsigned char opcode; /* see below */
58 unsigned long length; /* if the reg. is a pointer, how much data */
59 } __attribute__ ((packed));
62 struct register_foo eax_reg;
63 struct register_foo ebx_reg;
64 struct register_foo ecx_reg;
65 struct register_foo edx_reg;
66 struct register_foo edi_reg;
67 struct register_foo esi_reg;
68 struct register_foo eflags_reg;
69 } __attribute__ ((packed));
72 struct ev_hrt_header {
90 static u8 evbuffer_init;
91 static u8 evbuffer_length;
92 static u8 evbuffer[1024];
94 static void __iomem *compaq_int15_entry_point;
96 static spinlock_t int15_lock; /* lock for ordering int15_bios_call() */
99 /* This is a series of function that deals with
100 setting & getting the hotplug resource table in some environment variable.
104 * We really shouldn't be doing this unless there is a _very_ good reason to!!!
109 static u32 add_byte( u32 **p_buffer, u8 value, u32 *used, u32 *avail)
113 if ((*used + 1) > *avail)
116 *((u8*)*p_buffer) = value;
117 tByte = (u8**)p_buffer;
124 static u32 add_dword( u32 **p_buffer, u32 value, u32 *used, u32 *avail)
126 if ((*used + 4) > *avail)
137 * check_for_compaq_ROM
139 * this routine verifies that the ROM OEM string is 'COMPAQ'
141 * returns 0 for non-Compaq ROM, 1 for Compaq ROM
143 static int check_for_compaq_ROM (void __iomem *rom_start)
145 u8 temp1, temp2, temp3, temp4, temp5, temp6;
148 temp1 = readb(rom_start + 0xffea + 0);
149 temp2 = readb(rom_start + 0xffea + 1);
150 temp3 = readb(rom_start + 0xffea + 2);
151 temp4 = readb(rom_start + 0xffea + 3);
152 temp5 = readb(rom_start + 0xffea + 4);
153 temp6 = readb(rom_start + 0xffea + 5);
154 if ((temp1 == 'C') &&
162 dbg ("%s - returned %d\n", __FUNCTION__, result);
167 static u32 access_EV (u16 operation, u8 *ev_name, u8 *buffer, u32 *buf_size)
173 if (!compaq_int15_entry_point)
176 spin_lock_irqsave(&int15_lock, flags);
178 "xorl %%ebx,%%ebx\n" \
179 "xorl %%edx,%%edx\n" \
184 : "=c" (*buf_size), "=a" (ret_val)
185 : "a" (op), "c" (*buf_size), "S" (ev_name),
186 "D" (buffer), "m" (compaq_int15_entry_point)
188 spin_unlock_irqrestore(&int15_lock, flags);
190 return((ret_val & 0xFF00) >> 8);
197 * Read the hot plug Resource Table from NVRAM
199 static int load_HRT (void __iomem *rom_start)
206 if (!check_for_compaq_ROM(rom_start)) {
213 temp_dword = available;
215 rc = access_EV(READ_EV, "CQTHPS", evbuffer, &temp_dword);
217 evbuffer_length = temp_dword;
219 // We're maintaining the resource lists so write FF to invalidate old info
222 rc = access_EV(WRITE_EV, "CQTHPS", &temp_byte, &temp_dword);
231 * Save the hot plug Resource Table in NVRAM
233 static u32 store_HRT (void __iomem *rom_start)
243 struct controller *ctrl;
244 struct pci_resource *resNode;
245 struct ev_hrt_header *p_EV_header;
246 struct ev_hrt_ctrl *p_ev_ctrl;
250 if (!check_for_compaq_ROM(rom_start)) {
254 buffer = (u32*) evbuffer;
262 p_EV_header = (struct ev_hrt_header *) pFill;
264 ctrl = cpqhp_ctrl_list;
266 // The revision of this structure
267 rc = add_byte( &pFill, 1 + ctrl->push_flag, &usedbytes, &available);
271 // The number of controllers
272 rc = add_byte( &pFill, 1, &usedbytes, &available);
277 p_ev_ctrl = (struct ev_hrt_ctrl *) pFill;
282 rc = add_byte( &pFill, ctrl->bus, &usedbytes, &available);
287 rc = add_byte( &pFill, PCI_SLOT(ctrl->pci_dev->devfn), &usedbytes, &available);
291 // The function Number
292 rc = add_byte( &pFill, PCI_FUNC(ctrl->pci_dev->devfn), &usedbytes, &available);
296 // Skip the number of available entries
297 rc = add_dword( &pFill, 0, &usedbytes, &available);
301 // Figure out memory Available
303 resNode = ctrl->mem_head;
311 rc = add_dword( &pFill, resNode->base, &usedbytes, &available);
316 rc = add_dword( &pFill, resNode->length, &usedbytes, &available);
320 resNode = resNode->next;
323 // Fill in the number of entries
324 p_ev_ctrl->mem_avail = loop;
326 // Figure out prefetchable memory Available
328 resNode = ctrl->p_mem_head;
336 rc = add_dword( &pFill, resNode->base, &usedbytes, &available);
341 rc = add_dword( &pFill, resNode->length, &usedbytes, &available);
345 resNode = resNode->next;
348 // Fill in the number of entries
349 p_ev_ctrl->p_mem_avail = loop;
351 // Figure out IO Available
353 resNode = ctrl->io_head;
361 rc = add_dword( &pFill, resNode->base, &usedbytes, &available);
366 rc = add_dword( &pFill, resNode->length, &usedbytes, &available);
370 resNode = resNode->next;
373 // Fill in the number of entries
374 p_ev_ctrl->io_avail = loop;
376 // Figure out bus Available
378 resNode = ctrl->bus_head;
386 rc = add_dword( &pFill, resNode->base, &usedbytes, &available);
391 rc = add_dword( &pFill, resNode->length, &usedbytes, &available);
395 resNode = resNode->next;
398 // Fill in the number of entries
399 p_ev_ctrl->bus_avail = loop;
404 p_EV_header->num_of_ctrl = numCtrl;
408 temp_dword = usedbytes;
410 rc = access_EV(WRITE_EV, "CQTHPS", (u8*) buffer, &temp_dword);
412 dbg("usedbytes = 0x%x, length = 0x%x\n", usedbytes, temp_dword);
414 evbuffer_length = temp_dword;
417 err(msg_unable_to_save);
425 void compaq_nvram_init (void __iomem *rom_start)
428 compaq_int15_entry_point = (rom_start + ROM_INT15_PHY_ADDR - ROM_PHY_ADDR);
430 dbg("int15 entry = %p\n", compaq_int15_entry_point);
432 /* initialize our int15 lock */
433 spin_lock_init(&int15_lock);
437 int compaq_nvram_load (void __iomem *rom_start, struct controller *ctrl)
439 u8 bus, device, function;
440 u8 nummem, numpmem, numio, numbus;
443 struct pci_resource *mem_node;
444 struct pci_resource *p_mem_node;
445 struct pci_resource *io_node;
446 struct pci_resource *bus_node;
447 struct ev_hrt_ctrl *p_ev_ctrl;
448 struct ev_hrt_header *p_EV_header;
450 if (!evbuffer_init) {
451 // Read the resource list information in from NVRAM
452 if (load_HRT(rom_start))
453 memset (evbuffer, 0, 1024);
458 // If we saved information in NVRAM, use it now
459 p_EV_header = (struct ev_hrt_header *) evbuffer;
461 // The following code is for systems where version 1.0 of this
462 // driver has been loaded, but doesn't support the hardware.
463 // In that case, the driver would incorrectly store something
465 if ((p_EV_header->Version == 2) ||
466 ((p_EV_header->Version == 1) && !ctrl->push_flag)) {
467 p_byte = &(p_EV_header->next);
469 p_ev_ctrl = (struct ev_hrt_ctrl *) &(p_EV_header->next);
473 if (p_byte > ((u8*)p_EV_header + evbuffer_length))
476 bus = p_ev_ctrl->bus;
477 device = p_ev_ctrl->device;
478 function = p_ev_ctrl->function;
480 while ((bus != ctrl->bus) ||
481 (device != PCI_SLOT(ctrl->pci_dev->devfn)) ||
482 (function != PCI_FUNC(ctrl->pci_dev->devfn))) {
483 nummem = p_ev_ctrl->mem_avail;
484 numpmem = p_ev_ctrl->p_mem_avail;
485 numio = p_ev_ctrl->io_avail;
486 numbus = p_ev_ctrl->bus_avail;
490 if (p_byte > ((u8*)p_EV_header + evbuffer_length))
493 // Skip forward to the next entry
494 p_byte += (nummem + numpmem + numio + numbus) * 8;
496 if (p_byte > ((u8*)p_EV_header + evbuffer_length))
499 p_ev_ctrl = (struct ev_hrt_ctrl *) p_byte;
503 if (p_byte > ((u8*)p_EV_header + evbuffer_length))
506 bus = p_ev_ctrl->bus;
507 device = p_ev_ctrl->device;
508 function = p_ev_ctrl->function;
511 nummem = p_ev_ctrl->mem_avail;
512 numpmem = p_ev_ctrl->p_mem_avail;
513 numio = p_ev_ctrl->io_avail;
514 numbus = p_ev_ctrl->bus_avail;
518 if (p_byte > ((u8*)p_EV_header + evbuffer_length))
522 mem_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
527 mem_node->base = *(u32*)p_byte;
528 dbg("mem base = %8.8x\n",mem_node->base);
531 if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
536 mem_node->length = *(u32*)p_byte;
537 dbg("mem length = %8.8x\n",mem_node->length);
540 if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
545 mem_node->next = ctrl->mem_head;
546 ctrl->mem_head = mem_node;
550 p_mem_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
555 p_mem_node->base = *(u32*)p_byte;
556 dbg("pre-mem base = %8.8x\n",p_mem_node->base);
559 if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
564 p_mem_node->length = *(u32*)p_byte;
565 dbg("pre-mem length = %8.8x\n",p_mem_node->length);
568 if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
573 p_mem_node->next = ctrl->p_mem_head;
574 ctrl->p_mem_head = p_mem_node;
578 io_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
583 io_node->base = *(u32*)p_byte;
584 dbg("io base = %8.8x\n",io_node->base);
587 if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
592 io_node->length = *(u32*)p_byte;
593 dbg("io length = %8.8x\n",io_node->length);
596 if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
601 io_node->next = ctrl->io_head;
602 ctrl->io_head = io_node;
606 bus_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
611 bus_node->base = *(u32*)p_byte;
614 if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
619 bus_node->length = *(u32*)p_byte;
622 if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
627 bus_node->next = ctrl->bus_head;
628 ctrl->bus_head = bus_node;
631 // If all of the following fail, we don't have any resources for
634 rc &= cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
635 rc &= cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
636 rc &= cpqhp_resource_sort_and_combine(&(ctrl->io_head));
637 rc &= cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
642 if ((evbuffer[0] != 0) && (!ctrl->push_flag))
650 int compaq_nvram_store (void __iomem *rom_start)
654 if (rom_start == NULL)
658 rc = store_HRT(rom_start);
660 err(msg_unable_to_save);