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/pci_hotplug.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 /* lock for ordering int15_bios_call() */
97 static spinlock_t int15_lock
;
100 /* This is a series of function that deals with
101 * setting & getting the hotplug resource table in some environment variable.
105 * We really shouldn't be doing this unless there is a _very_ good reason to!!!
110 static u32
add_byte(u32
**p_buffer
, u8 value
, u32
*used
, u32
*avail
)
114 if ((*used
+ 1) > *avail
)
117 *((u8
*)*p_buffer
) = value
;
118 tByte
= (u8
**)p_buffer
;
125 static u32
add_dword(u32
**p_buffer
, u32 value
, u32
*used
, u32
*avail
)
127 if ((*used
+ 4) > *avail
)
138 * check_for_compaq_ROM
140 * this routine verifies that the ROM OEM string is 'COMPAQ'
142 * returns 0 for non-Compaq ROM, 1 for Compaq ROM
144 static int check_for_compaq_ROM(void __iomem
*rom_start
)
146 u8 temp1
, temp2
, temp3
, temp4
, temp5
, temp6
;
149 temp1
= readb(rom_start
+ 0xffea + 0);
150 temp2
= readb(rom_start
+ 0xffea + 1);
151 temp3
= readb(rom_start
+ 0xffea + 2);
152 temp4
= readb(rom_start
+ 0xffea + 3);
153 temp5
= readb(rom_start
+ 0xffea + 4);
154 temp6
= readb(rom_start
+ 0xffea + 5);
155 if ((temp1
== 'C') &&
163 dbg("%s - returned %d\n", __func__
, result
);
168 static u32
access_EV(u16 operation
, u8
*ev_name
, u8
*buffer
, u32
*buf_size
)
174 if (!compaq_int15_entry_point
)
177 spin_lock_irqsave(&int15_lock
, flags
);
179 "xorl %%ebx,%%ebx\n" \
180 "xorl %%edx,%%edx\n" \
185 : "=c" (*buf_size
), "=a" (ret_val
)
186 : "a" (op
), "c" (*buf_size
), "S" (ev_name
),
187 "D" (buffer
), "m" (compaq_int15_entry_point
)
189 spin_unlock_irqrestore(&int15_lock
, flags
);
191 return((ret_val
& 0xFF00) >> 8);
198 * Read the hot plug Resource Table from NVRAM
200 static int load_HRT(void __iomem
*rom_start
)
207 if (!check_for_compaq_ROM(rom_start
))
212 /* Now load the EV */
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
224 rc
= access_EV(WRITE_EV
, "CQTHPS", &temp_byte
, &temp_dword
);
233 * Save the hot plug Resource Table in NVRAM
235 static u32
store_HRT(void __iomem
*rom_start
)
245 struct controller
*ctrl
;
246 struct pci_resource
*resNode
;
247 struct ev_hrt_header
*p_EV_header
;
248 struct ev_hrt_ctrl
*p_ev_ctrl
;
252 if (!check_for_compaq_ROM(rom_start
))
255 buffer
= (u32
*) evbuffer
;
263 p_EV_header
= (struct ev_hrt_header
*) pFill
;
265 ctrl
= cpqhp_ctrl_list
;
267 /* The revision of this structure */
268 rc
= add_byte(&pFill
, 1 + ctrl
->push_flag
, &usedbytes
, &available
);
272 /* The number of controllers */
273 rc
= add_byte(&pFill
, 1, &usedbytes
, &available
);
278 p_ev_ctrl
= (struct ev_hrt_ctrl
*) pFill
;
283 rc
= add_byte(&pFill
, ctrl
->bus
, &usedbytes
, &available
);
287 /* The device Number */
288 rc
= add_byte(&pFill
, PCI_SLOT(ctrl
->pci_dev
->devfn
), &usedbytes
, &available
);
292 /* The function Number */
293 rc
= add_byte(&pFill
, PCI_FUNC(ctrl
->pci_dev
->devfn
), &usedbytes
, &available
);
297 /* Skip the number of available entries */
298 rc
= add_dword(&pFill
, 0, &usedbytes
, &available
);
302 /* Figure out memory Available */
304 resNode
= ctrl
->mem_head
;
312 rc
= add_dword(&pFill
, resNode
->base
, &usedbytes
, &available
);
317 rc
= add_dword(&pFill
, resNode
->length
, &usedbytes
, &available
);
321 resNode
= resNode
->next
;
324 /* Fill in the number of entries */
325 p_ev_ctrl
->mem_avail
= loop
;
327 /* Figure out prefetchable memory Available */
329 resNode
= ctrl
->p_mem_head
;
337 rc
= add_dword(&pFill
, resNode
->base
, &usedbytes
, &available
);
342 rc
= add_dword(&pFill
, resNode
->length
, &usedbytes
, &available
);
346 resNode
= resNode
->next
;
349 /* Fill in the number of entries */
350 p_ev_ctrl
->p_mem_avail
= loop
;
352 /* Figure out IO Available */
354 resNode
= ctrl
->io_head
;
362 rc
= add_dword(&pFill
, resNode
->base
, &usedbytes
, &available
);
367 rc
= add_dword(&pFill
, resNode
->length
, &usedbytes
, &available
);
371 resNode
= resNode
->next
;
374 /* Fill in the number of entries */
375 p_ev_ctrl
->io_avail
= loop
;
377 /* Figure out bus Available */
379 resNode
= ctrl
->bus_head
;
387 rc
= add_dword(&pFill
, resNode
->base
, &usedbytes
, &available
);
392 rc
= add_dword(&pFill
, resNode
->length
, &usedbytes
, &available
);
396 resNode
= resNode
->next
;
399 /* Fill in the number of entries */
400 p_ev_ctrl
->bus_avail
= loop
;
405 p_EV_header
->num_of_ctrl
= numCtrl
;
407 /* Now store the EV */
409 temp_dword
= usedbytes
;
411 rc
= access_EV(WRITE_EV
, "CQTHPS", (u8
*) buffer
, &temp_dword
);
413 dbg("usedbytes = 0x%x, length = 0x%x\n", usedbytes
, temp_dword
);
415 evbuffer_length
= temp_dword
;
418 err(msg_unable_to_save
);
426 void compaq_nvram_init(void __iomem
*rom_start
)
429 compaq_int15_entry_point
= (rom_start
+ ROM_INT15_PHY_ADDR
- ROM_PHY_ADDR
);
431 dbg("int15 entry = %p\n", compaq_int15_entry_point
);
433 /* initialize our int15 lock */
434 spin_lock_init(&int15_lock
);
438 int compaq_nvram_load(void __iomem
*rom_start
, struct controller
*ctrl
)
440 u8 bus
, device
, function
;
441 u8 nummem
, numpmem
, numio
, numbus
;
444 struct pci_resource
*mem_node
;
445 struct pci_resource
*p_mem_node
;
446 struct pci_resource
*io_node
;
447 struct pci_resource
*bus_node
;
448 struct ev_hrt_ctrl
*p_ev_ctrl
;
449 struct ev_hrt_header
*p_EV_header
;
451 if (!evbuffer_init
) {
452 /* Read the resource list information in from NVRAM */
453 if (load_HRT(rom_start
))
454 memset(evbuffer
, 0, 1024);
459 /* If we saved information in NVRAM, use it now */
460 p_EV_header
= (struct ev_hrt_header
*) evbuffer
;
462 /* The following code is for systems where version 1.0 of this
463 * driver has been loaded, but doesn't support the hardware.
464 * In that case, the driver would incorrectly store something
467 if ((p_EV_header
->Version
== 2) ||
468 ((p_EV_header
->Version
== 1) && !ctrl
->push_flag
)) {
469 p_byte
= &(p_EV_header
->next
);
471 p_ev_ctrl
= (struct ev_hrt_ctrl
*) &(p_EV_header
->next
);
475 if (p_byte
> ((u8
*)p_EV_header
+ evbuffer_length
))
478 bus
= p_ev_ctrl
->bus
;
479 device
= p_ev_ctrl
->device
;
480 function
= p_ev_ctrl
->function
;
482 while ((bus
!= ctrl
->bus
) ||
483 (device
!= PCI_SLOT(ctrl
->pci_dev
->devfn
)) ||
484 (function
!= PCI_FUNC(ctrl
->pci_dev
->devfn
))) {
485 nummem
= p_ev_ctrl
->mem_avail
;
486 numpmem
= p_ev_ctrl
->p_mem_avail
;
487 numio
= p_ev_ctrl
->io_avail
;
488 numbus
= p_ev_ctrl
->bus_avail
;
492 if (p_byte
> ((u8
*)p_EV_header
+ evbuffer_length
))
495 /* Skip forward to the next entry */
496 p_byte
+= (nummem
+ numpmem
+ numio
+ numbus
) * 8;
498 if (p_byte
> ((u8
*)p_EV_header
+ evbuffer_length
))
501 p_ev_ctrl
= (struct ev_hrt_ctrl
*) p_byte
;
505 if (p_byte
> ((u8
*)p_EV_header
+ evbuffer_length
))
508 bus
= p_ev_ctrl
->bus
;
509 device
= p_ev_ctrl
->device
;
510 function
= p_ev_ctrl
->function
;
513 nummem
= p_ev_ctrl
->mem_avail
;
514 numpmem
= p_ev_ctrl
->p_mem_avail
;
515 numio
= p_ev_ctrl
->io_avail
;
516 numbus
= p_ev_ctrl
->bus_avail
;
520 if (p_byte
> ((u8
*)p_EV_header
+ evbuffer_length
))
524 mem_node
= kmalloc(sizeof(struct pci_resource
), GFP_KERNEL
);
529 mem_node
->base
= *(u32
*)p_byte
;
530 dbg("mem base = %8.8x\n", mem_node
->base
);
533 if (p_byte
> ((u8
*)p_EV_header
+ evbuffer_length
)) {
538 mem_node
->length
= *(u32
*)p_byte
;
539 dbg("mem length = %8.8x\n", mem_node
->length
);
542 if (p_byte
> ((u8
*)p_EV_header
+ evbuffer_length
)) {
547 mem_node
->next
= ctrl
->mem_head
;
548 ctrl
->mem_head
= mem_node
;
552 p_mem_node
= kmalloc(sizeof(struct pci_resource
), GFP_KERNEL
);
557 p_mem_node
->base
= *(u32
*)p_byte
;
558 dbg("pre-mem base = %8.8x\n", p_mem_node
->base
);
561 if (p_byte
> ((u8
*)p_EV_header
+ evbuffer_length
)) {
566 p_mem_node
->length
= *(u32
*)p_byte
;
567 dbg("pre-mem length = %8.8x\n", p_mem_node
->length
);
570 if (p_byte
> ((u8
*)p_EV_header
+ evbuffer_length
)) {
575 p_mem_node
->next
= ctrl
->p_mem_head
;
576 ctrl
->p_mem_head
= p_mem_node
;
580 io_node
= kmalloc(sizeof(struct pci_resource
), GFP_KERNEL
);
585 io_node
->base
= *(u32
*)p_byte
;
586 dbg("io base = %8.8x\n", io_node
->base
);
589 if (p_byte
> ((u8
*)p_EV_header
+ evbuffer_length
)) {
594 io_node
->length
= *(u32
*)p_byte
;
595 dbg("io length = %8.8x\n", io_node
->length
);
598 if (p_byte
> ((u8
*)p_EV_header
+ evbuffer_length
)) {
603 io_node
->next
= ctrl
->io_head
;
604 ctrl
->io_head
= io_node
;
608 bus_node
= kmalloc(sizeof(struct pci_resource
), GFP_KERNEL
);
613 bus_node
->base
= *(u32
*)p_byte
;
616 if (p_byte
> ((u8
*)p_EV_header
+ evbuffer_length
)) {
621 bus_node
->length
= *(u32
*)p_byte
;
624 if (p_byte
> ((u8
*)p_EV_header
+ evbuffer_length
)) {
629 bus_node
->next
= ctrl
->bus_head
;
630 ctrl
->bus_head
= bus_node
;
633 /* If all of the following fail, we don't have any resources for
637 rc
&= cpqhp_resource_sort_and_combine(&(ctrl
->mem_head
));
638 rc
&= cpqhp_resource_sort_and_combine(&(ctrl
->p_mem_head
));
639 rc
&= cpqhp_resource_sort_and_combine(&(ctrl
->io_head
));
640 rc
&= cpqhp_resource_sort_and_combine(&(ctrl
->bus_head
));
645 if ((evbuffer
[0] != 0) && (!ctrl
->push_flag
))
653 int compaq_nvram_store(void __iomem
*rom_start
)
657 if (rom_start
== NULL
)
661 rc
= store_HRT(rom_start
);
663 err(msg_unable_to_save
);