2 * Standard PCI Hot Plug 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.
7 * Copyright (C) 2003-2004 Intel Corporation
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or (at
14 * your option) any later version.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
19 * NON INFRINGEMENT. See the GNU General Public License for more
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 * Send feedback to <greg@kroah.com>,<kristen.c.accardi@intel.com>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/types.h>
33 #include <linux/pci.h>
34 #include <linux/interrupt.h>
39 #define DBG_K_TRACE_ENTRY ((unsigned int)0x00000001) /* On function entry */
40 #define DBG_K_TRACE_EXIT ((unsigned int)0x00000002) /* On function exit */
41 #define DBG_K_INFO ((unsigned int)0x00000004) /* Info messages */
42 #define DBG_K_ERROR ((unsigned int)0x00000008) /* Error messages */
43 #define DBG_K_TRACE (DBG_K_TRACE_ENTRY|DBG_K_TRACE_EXIT)
44 #define DBG_K_STANDARD (DBG_K_INFO|DBG_K_ERROR|DBG_K_TRACE)
45 /* Redefine this flagword to set debug level */
46 #define DEBUG_LEVEL DBG_K_STANDARD
48 #define DEFINE_DBG_BUFFER char __dbg_str_buf[256];
50 #define DBG_PRINT( dbg_flags, args... ) \
52 if ( DEBUG_LEVEL & ( dbg_flags ) ) \
55 len = sprintf( __dbg_str_buf, "%s:%d: %s: ", \
56 __FILE__, __LINE__, __FUNCTION__ ); \
57 sprintf( __dbg_str_buf + len, args ); \
58 printk( KERN_NOTICE "%s\n", __dbg_str_buf ); \
62 #define DBG_ENTER_ROUTINE DBG_PRINT (DBG_K_TRACE_ENTRY, "%s", "[Entry]");
63 #define DBG_LEAVE_ROUTINE DBG_PRINT (DBG_K_TRACE_EXIT, "%s", "[Exit]");
65 #define DEFINE_DBG_BUFFER
66 #define DBG_ENTER_ROUTINE
67 #define DBG_LEAVE_ROUTINE
70 /* Slot Available Register I field definition */
71 #define SLOT_33MHZ 0x0000001f
72 #define SLOT_66MHZ_PCIX 0x00001f00
73 #define SLOT_100MHZ_PCIX 0x001f0000
74 #define SLOT_133MHZ_PCIX 0x1f000000
76 /* Slot Available Register II field definition */
77 #define SLOT_66MHZ 0x0000001f
78 #define SLOT_66MHZ_PCIX_266 0x00000f00
79 #define SLOT_100MHZ_PCIX_266 0x0000f000
80 #define SLOT_133MHZ_PCIX_266 0x000f0000
81 #define SLOT_66MHZ_PCIX_533 0x00f00000
82 #define SLOT_100MHZ_PCIX_533 0x0f000000
83 #define SLOT_133MHZ_PCIX_533 0xf0000000
85 /* Slot Configuration */
86 #define SLOT_NUM 0x0000001F
87 #define FIRST_DEV_NUM 0x00001F00
88 #define PSN 0x07FF0000
89 #define UPDOWN 0x20000000
90 #define MRLSENSOR 0x40000000
91 #define ATTN_BUTTON 0x80000000
93 /* Slot Status Field Definitions */
95 #define PWR_ONLY 0x0001
96 #define ENABLED 0x0002
97 #define DISABLED 0x0003
99 /* Power Indicator State */
100 #define PWR_LED_ON 0x0004
101 #define PWR_LED_BLINK 0x0008
102 #define PWR_LED_OFF 0x000c
104 /* Attention Indicator State */
105 #define ATTEN_LED_ON 0x0010
106 #define ATTEN_LED_BLINK 0x0020
107 #define ATTEN_LED_OFF 0x0030
110 #define pwr_fault 0x0040
112 /* Attention Button */
113 #define ATTEN_BUTTON 0x0080
116 #define MRL_SENSOR 0x0100
119 #define IS_66MHZ_CAP 0x0200
121 /* PRSNT1#/PRSNT2# */
122 #define SLOT_EMP 0x0c00
124 /* PCI-X Capability */
125 #define NON_PCIX 0x0000
126 #define PCIX_66 0x1000
127 #define PCIX_133 0x3000
128 #define PCIX_266 0x4000 /* For PI = 2 only */
129 #define PCIX_533 0x5000 /* For PI = 2 only */
131 /* SHPC 'write' operations/commands */
133 /* Slot operation - 0x00h to 0x3Fh */
135 #define NO_CHANGE 0x00
137 /* Slot state - Bits 0 & 1 of controller command register */
138 #define SET_SLOT_PWR 0x01
139 #define SET_SLOT_ENABLE 0x02
140 #define SET_SLOT_DISABLE 0x03
142 /* Power indicator state - Bits 2 & 3 of controller command register*/
143 #define SET_PWR_ON 0x04
144 #define SET_PWR_BLINK 0x08
145 #define SET_PWR_OFF 0x0C
147 /* Attention indicator state - Bits 4 & 5 of controller command register*/
148 #define SET_ATTN_ON 0x010
149 #define SET_ATTN_BLINK 0x020
150 #define SET_ATTN_OFF 0x030
152 /* Set bus speed/mode A - 0x40h to 0x47h */
153 #define SETA_PCI_33MHZ 0x40
154 #define SETA_PCI_66MHZ 0x41
155 #define SETA_PCIX_66MHZ 0x42
156 #define SETA_PCIX_100MHZ 0x43
157 #define SETA_PCIX_133MHZ 0x44
158 #define RESERV_1 0x45
159 #define RESERV_2 0x46
160 #define RESERV_3 0x47
162 /* Set bus speed/mode B - 0x50h to 0x5fh */
163 #define SETB_PCI_33MHZ 0x50
164 #define SETB_PCI_66MHZ 0x51
165 #define SETB_PCIX_66MHZ_PM 0x52
166 #define SETB_PCIX_100MHZ_PM 0x53
167 #define SETB_PCIX_133MHZ_PM 0x54
168 #define SETB_PCIX_66MHZ_EM 0x55
169 #define SETB_PCIX_100MHZ_EM 0x56
170 #define SETB_PCIX_133MHZ_EM 0x57
171 #define SETB_PCIX_66MHZ_266 0x58
172 #define SETB_PCIX_100MHZ_266 0x59
173 #define SETB_PCIX_133MHZ_266 0x5a
174 #define SETB_PCIX_66MHZ_533 0x5b
175 #define SETB_PCIX_100MHZ_533 0x5c
176 #define SETB_PCIX_133MHZ_533 0x5d
179 /* Power-on all slots - 0x48h */
180 #define SET_PWR_ON_ALL 0x48
182 /* Enable all slots - 0x49h */
183 #define SET_ENABLE_ALL 0x49
185 /* SHPC controller command error code */
186 #define SWITCH_OPEN 0x1
187 #define INVALID_CMD 0x2
188 #define INVALID_SPEED_MODE 0x4
190 /* For accessing SHPC Working Register Set */
191 #define DWORD_SELECT 0x2
192 #define DWORD_DATA 0x4
193 #define BASE_OFFSET 0x0
195 /* Field Offset in Logical Slot Register - byte boundary */
196 #define SLOT_EVENT_LATCH 0x2
197 #define SLOT_SERR_INT_MASK 0x3
199 static spinlock_t hpc_event_lock;
201 DEFINE_DBG_BUFFER /* Debug string buffer for entire HPC defined here */
202 static struct php_ctlr_state_s *php_ctlr_list_head; /* HPC state linked list */
203 static int ctlr_seq_num = 0; /* Controller sequenc # */
204 static spinlock_t list_lock;
206 static irqreturn_t shpc_isr(int IRQ, void *dev_id, struct pt_regs *regs);
208 static void start_int_poll_timer(struct php_ctlr_state_s *php_ctlr, int seconds);
209 static int hpc_check_cmd_status(struct controller *ctrl);
211 /* This is the interrupt polling timeout function. */
212 static void int_poll_timeout(unsigned long lphp_ctlr)
214 struct php_ctlr_state_s *php_ctlr = (struct php_ctlr_state_s *)lphp_ctlr;
219 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
223 /* Poll for interrupt events. regs == NULL => polling */
224 shpc_isr( 0, (void *)php_ctlr, NULL );
226 init_timer(&php_ctlr->int_poll_timer);
227 if (!shpchp_poll_time)
228 shpchp_poll_time = 2; /* reset timer to poll in 2 secs if user doesn't specify at module installation*/
230 start_int_poll_timer(php_ctlr, shpchp_poll_time);
235 /* This function starts the interrupt polling timer. */
236 static void start_int_poll_timer(struct php_ctlr_state_s *php_ctlr, int seconds)
239 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
243 if ( ( seconds <= 0 ) || ( seconds > 60 ) )
244 seconds = 2; /* Clamp to sane value */
246 php_ctlr->int_poll_timer.function = &int_poll_timeout;
247 php_ctlr->int_poll_timer.data = (unsigned long)php_ctlr; /* Instance data */
248 php_ctlr->int_poll_timer.expires = jiffies + seconds * HZ;
249 add_timer(&php_ctlr->int_poll_timer);
254 static inline int shpc_wait_cmd(struct controller *ctrl)
257 unsigned int timeout_msec = shpchp_poll_mode ? 2000 : 1000;
258 unsigned long timeout = msecs_to_jiffies(timeout_msec);
259 int rc = wait_event_interruptible_timeout(ctrl->queue,
260 !ctrl->cmd_busy, timeout);
263 err("Command not completed in %d msec\n", timeout_msec);
266 info("Command was interrupted by a signal\n");
273 static int shpc_write_cmd(struct slot *slot, u8 t_slot, u8 cmd)
275 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
283 mutex_lock(&slot->ctrl->cmd_lock);
286 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
291 for (i = 0; i < 10; i++) {
292 cmd_status = readw(php_ctlr->creg + CMD_STATUS);
294 if (!(cmd_status & 0x1))
296 /* Check every 0.1 sec for a total of 1 sec*/
300 cmd_status = readw(php_ctlr->creg + CMD_STATUS);
302 if (cmd_status & 0x1) {
303 /* After 1 sec and and the controller is still busy */
304 err("%s : Controller is still busy after 1 sec.\n", __FUNCTION__);
310 temp_word = (t_slot << 8) | (cmd & 0xFF);
311 dbg("%s: t_slot %x cmd %x\n", __FUNCTION__, t_slot, cmd);
313 /* To make sure the Controller Busy bit is 0 before we send out the
316 slot->ctrl->cmd_busy = 1;
317 writew(temp_word, php_ctlr->creg + CMD);
320 * Wait for command completion.
322 retval = shpc_wait_cmd(slot->ctrl);
326 cmd_status = hpc_check_cmd_status(slot->ctrl);
328 err("%s: Failed to issued command 0x%x (error code = %d)\n",
329 __FUNCTION__, cmd, cmd_status);
333 mutex_unlock(&slot->ctrl->cmd_lock);
339 static int hpc_check_cmd_status(struct controller *ctrl)
341 struct php_ctlr_state_s *php_ctlr = ctrl->hpc_ctlr_handle;
347 if (!ctrl->hpc_ctlr_handle) {
348 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
352 cmd_status = readw(php_ctlr->creg + CMD_STATUS) & 0x000F;
354 switch (cmd_status >> 1) {
359 retval = SWITCH_OPEN;
360 err("%s: Switch opened!\n", __FUNCTION__);
363 retval = INVALID_CMD;
364 err("%s: Invalid HPC command!\n", __FUNCTION__);
367 retval = INVALID_SPEED_MODE;
368 err("%s: Invalid bus speed/mode!\n", __FUNCTION__);
379 static int hpc_get_attention_status(struct slot *slot, u8 *status)
381 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
388 if (!slot->ctrl->hpc_ctlr_handle) {
389 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
393 slot_reg = readl(php_ctlr->creg + SLOT1 + 4*(slot->hp_slot));
394 slot_status = (u16) slot_reg;
395 atten_led_state = (slot_status & 0x0030) >> 4;
397 switch (atten_led_state) {
399 *status = 0xFF; /* Reserved */
402 *status = 1; /* On */
405 *status = 2; /* Blink */
408 *status = 0; /* Off */
419 static int hpc_get_power_status(struct slot * slot, u8 *status)
421 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
429 if (!slot->ctrl->hpc_ctlr_handle) {
430 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
434 slot_reg = readl(php_ctlr->creg + SLOT1 + 4*(slot->hp_slot));
435 slot_status = (u16) slot_reg;
436 slot_state = (slot_status & 0x0003);
438 switch (slot_state) {
443 *status = 2; /* Powered only */
446 *status = 1; /* Enabled */
449 *status = 0; /* Disabled */
461 static int hpc_get_latch_status(struct slot *slot, u8 *status)
463 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
469 if (!slot->ctrl->hpc_ctlr_handle) {
470 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
474 slot_reg = readl(php_ctlr->creg + SLOT1 + 4*(slot->hp_slot));
475 slot_status = (u16)slot_reg;
477 *status = ((slot_status & 0x0100) == 0) ? 0 : 1; /* 0 -> close; 1 -> open */
484 static int hpc_get_adapter_status(struct slot *slot, u8 *status)
486 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
493 if (!slot->ctrl->hpc_ctlr_handle) {
494 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
498 slot_reg = readl(php_ctlr->creg + SLOT1 + 4*(slot->hp_slot));
499 slot_status = (u16)slot_reg;
500 card_state = (u8)((slot_status & 0x0C00) >> 10);
501 *status = (card_state != 0x3) ? 1 : 0;
507 static int hpc_get_prog_int(struct slot *slot, u8 *prog_int)
509 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
513 if (!slot->ctrl->hpc_ctlr_handle) {
514 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
518 *prog_int = readb(php_ctlr->creg + PROG_INTERFACE);
524 static int hpc_get_adapter_speed(struct slot *slot, enum pci_bus_speed *value)
527 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
528 u32 slot_reg = readl(php_ctlr->creg + SLOT1 + 4 * slot->hp_slot);
529 u8 pcix_cap = (slot_reg >> 12) & 7;
530 u8 m66_cap = (slot_reg >> 9) & 1;
534 dbg("%s: slot_reg = %x, pcix_cap = %x, m66_cap = %x\n",
535 __FUNCTION__, slot_reg, pcix_cap, m66_cap);
539 *value = m66_cap ? PCI_SPEED_66MHz : PCI_SPEED_33MHz;
542 *value = PCI_SPEED_66MHz_PCIX;
545 *value = PCI_SPEED_133MHz_PCIX;
548 *value = PCI_SPEED_133MHz_PCIX_266;
551 *value = PCI_SPEED_133MHz_PCIX_533;
555 *value = PCI_SPEED_UNKNOWN;
560 dbg("Adapter speed = %d\n", *value);
565 static int hpc_get_mode1_ECC_cap(struct slot *slot, u8 *mode)
567 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
574 if (!slot->ctrl->hpc_ctlr_handle) {
575 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
579 pi = readb(php_ctlr->creg + PROG_INTERFACE);
580 sec_bus_status = readw(php_ctlr->creg + SEC_BUS_CONFIG);
583 *mode = (sec_bus_status & 0x0100) >> 8;
588 dbg("Mode 1 ECC cap = %d\n", *mode);
594 static int hpc_query_power_fault(struct slot * slot)
596 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
599 u8 pwr_fault_state, status;
603 if (!slot->ctrl->hpc_ctlr_handle) {
604 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
608 slot_reg = readl(php_ctlr->creg + SLOT1 + 4*(slot->hp_slot));
609 slot_status = (u16) slot_reg;
610 pwr_fault_state = (slot_status & 0x0040) >> 7;
611 status = (pwr_fault_state == 1) ? 0 : 1;
614 /* Note: Logic 0 => fault */
618 static int hpc_set_attention_status(struct slot *slot, u8 value)
620 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
624 if (!slot->ctrl->hpc_ctlr_handle) {
625 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
629 if (slot->hp_slot >= php_ctlr->num_slots) {
630 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
636 slot_cmd = 0x30; /* OFF */
639 slot_cmd = 0x10; /* ON */
642 slot_cmd = 0x20; /* BLINK */
648 shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
654 static void hpc_set_green_led_on(struct slot *slot)
656 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
659 if (!slot->ctrl->hpc_ctlr_handle) {
660 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
664 if (slot->hp_slot >= php_ctlr->num_slots) {
665 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
671 shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
676 static void hpc_set_green_led_off(struct slot *slot)
678 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
681 if (!slot->ctrl->hpc_ctlr_handle) {
682 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
686 if (slot->hp_slot >= php_ctlr->num_slots) {
687 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
693 shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
698 static void hpc_set_green_led_blink(struct slot *slot)
700 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
703 if (!slot->ctrl->hpc_ctlr_handle) {
704 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
708 if (slot->hp_slot >= php_ctlr->num_slots) {
709 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
715 shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
720 int shpc_get_ctlr_slot_config(struct controller *ctrl,
721 int *num_ctlr_slots, /* number of slots in this HPC */
722 int *first_device_num, /* PCI dev num of the first slot in this SHPC */
723 int *physical_slot_num, /* phy slot num of the first slot in this SHPC */
724 int *updown, /* physical_slot_num increament: 1 or -1 */
727 struct php_ctlr_state_s *php_ctlr = ctrl->hpc_ctlr_handle;
731 if (!ctrl->hpc_ctlr_handle) {
732 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
736 *first_device_num = php_ctlr->slot_device_offset; /* Obtained in shpc_init() */
737 *num_ctlr_slots = php_ctlr->num_slots; /* Obtained in shpc_init() */
739 *physical_slot_num = (readl(php_ctlr->creg + SLOT_CONFIG) & PSN) >> 16;
740 dbg("%s: physical_slot_num = %x\n", __FUNCTION__, *physical_slot_num);
741 *updown = ((readl(php_ctlr->creg + SLOT_CONFIG) & UPDOWN ) >> 29) ? 1 : -1;
747 static void hpc_release_ctlr(struct controller *ctrl)
749 struct php_ctlr_state_s *php_ctlr = ctrl->hpc_ctlr_handle;
750 struct php_ctlr_state_s *p, *p_prev;
755 if (!ctrl->hpc_ctlr_handle) {
756 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
761 * Mask all slot event interrupts
763 for (i = 0; i < ctrl->num_slots; i++)
764 writel(0xffff3fff, php_ctlr->creg + SLOT1 + (4 * i));
768 if (shpchp_poll_mode) {
769 del_timer(&php_ctlr->int_poll_timer);
772 free_irq(php_ctlr->irq, ctrl);
774 pci_disable_msi(php_ctlr->pci_dev);
778 if (php_ctlr->pci_dev) {
779 iounmap(php_ctlr->creg);
780 release_mem_region(ctrl->mmio_base, ctrl->mmio_size);
781 php_ctlr->pci_dev = NULL;
784 spin_lock(&list_lock);
785 p = php_ctlr_list_head;
790 p_prev->pnext = p->pnext;
792 php_ctlr_list_head = p->pnext;
799 spin_unlock(&list_lock);
807 static int hpc_power_on_slot(struct slot * slot)
809 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
815 if (!slot->ctrl->hpc_ctlr_handle) {
816 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
820 if (slot->hp_slot >= php_ctlr->num_slots) {
821 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
826 retval = shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
829 err("%s: Write command failed!\n", __FUNCTION__);
838 static int hpc_slot_enable(struct slot * slot)
840 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
846 if (!slot->ctrl->hpc_ctlr_handle) {
847 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
851 if (slot->hp_slot >= php_ctlr->num_slots) {
852 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
855 /* 3A => Slot - Enable, Power Indicator - Blink, Attention Indicator - Off */
858 retval = shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
861 err("%s: Write command failed!\n", __FUNCTION__);
869 static int hpc_slot_disable(struct slot * slot)
871 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
877 if (!slot->ctrl->hpc_ctlr_handle) {
878 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
882 if (slot->hp_slot >= php_ctlr->num_slots) {
883 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
887 /* 1F => Slot - Disable, Power Indicator - Off, Attention Indicator - On */
890 retval = shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
893 err("%s: Write command failed!\n", __FUNCTION__);
901 static int hpc_set_bus_speed_mode(struct slot * slot, enum pci_bus_speed value)
904 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
909 pi = readb(php_ctlr->creg + PROG_INTERFACE);
910 if ((pi == 1) && (value > PCI_SPEED_133MHz_PCIX))
914 case PCI_SPEED_33MHz:
915 cmd = SETA_PCI_33MHZ;
917 case PCI_SPEED_66MHz:
918 cmd = SETA_PCI_66MHZ;
920 case PCI_SPEED_66MHz_PCIX:
921 cmd = SETA_PCIX_66MHZ;
923 case PCI_SPEED_100MHz_PCIX:
924 cmd = SETA_PCIX_100MHZ;
926 case PCI_SPEED_133MHz_PCIX:
927 cmd = SETA_PCIX_133MHZ;
929 case PCI_SPEED_66MHz_PCIX_ECC:
930 cmd = SETB_PCIX_66MHZ_EM;
932 case PCI_SPEED_100MHz_PCIX_ECC:
933 cmd = SETB_PCIX_100MHZ_EM;
935 case PCI_SPEED_133MHz_PCIX_ECC:
936 cmd = SETB_PCIX_133MHZ_EM;
938 case PCI_SPEED_66MHz_PCIX_266:
939 cmd = SETB_PCIX_66MHZ_266;
941 case PCI_SPEED_100MHz_PCIX_266:
942 cmd = SETB_PCIX_100MHZ_266;
944 case PCI_SPEED_133MHz_PCIX_266:
945 cmd = SETB_PCIX_133MHZ_266;
947 case PCI_SPEED_66MHz_PCIX_533:
948 cmd = SETB_PCIX_66MHZ_533;
950 case PCI_SPEED_100MHz_PCIX_533:
951 cmd = SETB_PCIX_100MHZ_533;
953 case PCI_SPEED_133MHz_PCIX_533:
954 cmd = SETB_PCIX_133MHZ_533;
960 retval = shpc_write_cmd(slot, 0, cmd);
962 err("%s: Write command failed!\n", __FUNCTION__);
968 static irqreturn_t shpc_isr(int IRQ, void *dev_id, struct pt_regs *regs)
970 struct controller *ctrl = NULL;
971 struct php_ctlr_state_s *php_ctlr;
972 u8 schedule_flag = 0;
974 u32 temp_dword, intr_loc, intr_loc2;
980 if (!shpchp_poll_mode) {
981 ctrl = (struct controller *)dev_id;
982 php_ctlr = ctrl->hpc_ctlr_handle;
984 php_ctlr = (struct php_ctlr_state_s *) dev_id;
985 ctrl = (struct controller *)php_ctlr->callback_instance_id;
991 if (!php_ctlr || !php_ctlr->creg)
994 /* Check to see if it was our interrupt */
995 intr_loc = readl(php_ctlr->creg + INTR_LOC);
999 dbg("%s: intr_loc = %x\n",__FUNCTION__, intr_loc);
1001 if(!shpchp_poll_mode) {
1002 /* Mask Global Interrupt Mask - see implementation note on p. 139 */
1003 /* of SHPC spec rev 1.0*/
1004 temp_dword = readl(php_ctlr->creg + SERR_INTR_ENABLE);
1005 temp_dword |= 0x00000001;
1006 writel(temp_dword, php_ctlr->creg + SERR_INTR_ENABLE);
1008 intr_loc2 = readl(php_ctlr->creg + INTR_LOC);
1009 dbg("%s: intr_loc2 = %x\n",__FUNCTION__, intr_loc2);
1012 if (intr_loc & 0x0001) {
1014 * Command Complete Interrupt Pending
1015 * RO only - clear by writing 1 to the Command Completion
1016 * Detect bit in Controller SERR-INT register
1018 temp_dword = readl(php_ctlr->creg + SERR_INTR_ENABLE);
1019 temp_dword &= 0xfffdffff;
1020 writel(temp_dword, php_ctlr->creg + SERR_INTR_ENABLE);
1022 wake_up_interruptible(&ctrl->queue);
1025 if ((intr_loc = (intr_loc >> 1)) == 0)
1028 for (hp_slot = 0; hp_slot < ctrl->num_slots; hp_slot++) {
1029 /* To find out which slot has interrupt pending */
1030 if ((intr_loc >> hp_slot) & 0x01) {
1031 temp_dword = readl(php_ctlr->creg + SLOT1 + (4*hp_slot));
1032 dbg("%s: Slot %x with intr, slot register = %x\n",
1033 __FUNCTION__, hp_slot, temp_dword);
1034 temp_byte = (temp_dword >> 16) & 0xFF;
1035 if ((php_ctlr->switch_change_callback) && (temp_byte & 0x08))
1036 schedule_flag += php_ctlr->switch_change_callback(
1037 hp_slot, php_ctlr->callback_instance_id);
1038 if ((php_ctlr->attention_button_callback) && (temp_byte & 0x04))
1039 schedule_flag += php_ctlr->attention_button_callback(
1040 hp_slot, php_ctlr->callback_instance_id);
1041 if ((php_ctlr->presence_change_callback) && (temp_byte & 0x01))
1042 schedule_flag += php_ctlr->presence_change_callback(
1043 hp_slot , php_ctlr->callback_instance_id);
1044 if ((php_ctlr->power_fault_callback) && (temp_byte & 0x12))
1045 schedule_flag += php_ctlr->power_fault_callback(
1046 hp_slot, php_ctlr->callback_instance_id);
1048 /* Clear all slot events */
1049 temp_dword = 0xe01f3fff;
1050 writel(temp_dword, php_ctlr->creg + SLOT1 + (4*hp_slot));
1052 intr_loc2 = readl(php_ctlr->creg + INTR_LOC);
1053 dbg("%s: intr_loc2 = %x\n",__FUNCTION__, intr_loc2);
1057 if (!shpchp_poll_mode) {
1058 /* Unmask Global Interrupt Mask */
1059 temp_dword = readl(php_ctlr->creg + SERR_INTR_ENABLE);
1060 temp_dword &= 0xfffffffe;
1061 writel(temp_dword, php_ctlr->creg + SERR_INTR_ENABLE);
1067 static int hpc_get_max_bus_speed (struct slot *slot, enum pci_bus_speed *value)
1070 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
1071 enum pci_bus_speed bus_speed = PCI_SPEED_UNKNOWN;
1072 u8 pi = readb(php_ctlr->creg + PROG_INTERFACE);
1073 u32 slot_avail1 = readl(php_ctlr->creg + SLOT_AVAIL1);
1074 u32 slot_avail2 = readl(php_ctlr->creg + SLOT_AVAIL2);
1079 if (slot_avail2 & SLOT_133MHZ_PCIX_533)
1080 bus_speed = PCI_SPEED_133MHz_PCIX_533;
1081 else if (slot_avail2 & SLOT_100MHZ_PCIX_533)
1082 bus_speed = PCI_SPEED_100MHz_PCIX_533;
1083 else if (slot_avail2 & SLOT_66MHZ_PCIX_533)
1084 bus_speed = PCI_SPEED_66MHz_PCIX_533;
1085 else if (slot_avail2 & SLOT_133MHZ_PCIX_266)
1086 bus_speed = PCI_SPEED_133MHz_PCIX_266;
1087 else if (slot_avail2 & SLOT_100MHZ_PCIX_266)
1088 bus_speed = PCI_SPEED_100MHz_PCIX_266;
1089 else if (slot_avail2 & SLOT_66MHZ_PCIX_266)
1090 bus_speed = PCI_SPEED_66MHz_PCIX_266;
1093 if (bus_speed == PCI_SPEED_UNKNOWN) {
1094 if (slot_avail1 & SLOT_133MHZ_PCIX)
1095 bus_speed = PCI_SPEED_133MHz_PCIX;
1096 else if (slot_avail1 & SLOT_100MHZ_PCIX)
1097 bus_speed = PCI_SPEED_100MHz_PCIX;
1098 else if (slot_avail1 & SLOT_66MHZ_PCIX)
1099 bus_speed = PCI_SPEED_66MHz_PCIX;
1100 else if (slot_avail2 & SLOT_66MHZ)
1101 bus_speed = PCI_SPEED_66MHz;
1102 else if (slot_avail1 & SLOT_33MHZ)
1103 bus_speed = PCI_SPEED_33MHz;
1109 dbg("Max bus speed = %d\n", bus_speed);
1114 static int hpc_get_cur_bus_speed (struct slot *slot, enum pci_bus_speed *value)
1117 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
1118 enum pci_bus_speed bus_speed = PCI_SPEED_UNKNOWN;
1119 u16 sec_bus_reg = readw(php_ctlr->creg + SEC_BUS_CONFIG);
1120 u8 pi = readb(php_ctlr->creg + PROG_INTERFACE);
1121 u8 speed_mode = (pi == 2) ? (sec_bus_reg & 0xF) : (sec_bus_reg & 0x7);
1125 if ((pi == 1) && (speed_mode > 4)) {
1126 *value = PCI_SPEED_UNKNOWN;
1130 switch (speed_mode) {
1132 *value = PCI_SPEED_33MHz;
1135 *value = PCI_SPEED_66MHz;
1138 *value = PCI_SPEED_66MHz_PCIX;
1141 *value = PCI_SPEED_100MHz_PCIX;
1144 *value = PCI_SPEED_133MHz_PCIX;
1147 *value = PCI_SPEED_66MHz_PCIX_ECC;
1150 *value = PCI_SPEED_100MHz_PCIX_ECC;
1153 *value = PCI_SPEED_133MHz_PCIX_ECC;
1156 *value = PCI_SPEED_66MHz_PCIX_266;
1159 *value = PCI_SPEED_100MHz_PCIX_266;
1162 *value = PCI_SPEED_133MHz_PCIX_266;
1165 *value = PCI_SPEED_66MHz_PCIX_533;
1168 *value = PCI_SPEED_100MHz_PCIX_533;
1171 *value = PCI_SPEED_133MHz_PCIX_533;
1174 *value = PCI_SPEED_UNKNOWN;
1179 dbg("Current bus speed = %d\n", bus_speed);
1184 static struct hpc_ops shpchp_hpc_ops = {
1185 .power_on_slot = hpc_power_on_slot,
1186 .slot_enable = hpc_slot_enable,
1187 .slot_disable = hpc_slot_disable,
1188 .set_bus_speed_mode = hpc_set_bus_speed_mode,
1189 .set_attention_status = hpc_set_attention_status,
1190 .get_power_status = hpc_get_power_status,
1191 .get_attention_status = hpc_get_attention_status,
1192 .get_latch_status = hpc_get_latch_status,
1193 .get_adapter_status = hpc_get_adapter_status,
1195 .get_max_bus_speed = hpc_get_max_bus_speed,
1196 .get_cur_bus_speed = hpc_get_cur_bus_speed,
1197 .get_adapter_speed = hpc_get_adapter_speed,
1198 .get_mode1_ECC_cap = hpc_get_mode1_ECC_cap,
1199 .get_prog_int = hpc_get_prog_int,
1201 .query_power_fault = hpc_query_power_fault,
1202 .green_led_on = hpc_set_green_led_on,
1203 .green_led_off = hpc_set_green_led_off,
1204 .green_led_blink = hpc_set_green_led_blink,
1206 .release_ctlr = hpc_release_ctlr,
1209 inline static int shpc_indirect_creg_read(struct controller *ctrl, int index,
1213 u32 cap_offset = ctrl->cap_offset;
1214 struct pci_dev *pdev = ctrl->pci_dev;
1216 rc = pci_write_config_byte(pdev, cap_offset + DWORD_SELECT, index);
1219 return pci_read_config_dword(pdev, cap_offset + DWORD_DATA, value);
1222 int shpc_init(struct controller * ctrl, struct pci_dev * pdev)
1224 struct php_ctlr_state_s *php_ctlr, *p;
1225 void *instance_id = ctrl;
1226 int rc, num_slots = 0;
1228 static int first = 1;
1229 u32 shpc_base_offset;
1230 u32 tempdword, slot_reg;
1235 ctrl->pci_dev = pdev; /* pci_dev of the P2P bridge */
1237 spin_lock_init(&list_lock);
1238 php_ctlr = kzalloc(sizeof(*php_ctlr), GFP_KERNEL);
1240 if (!php_ctlr) { /* allocate controller state data */
1241 err("%s: HPC controller memory allocation error!\n", __FUNCTION__);
1245 php_ctlr->pci_dev = pdev; /* save pci_dev in context */
1247 if ((pdev->vendor == PCI_VENDOR_ID_AMD) || (pdev->device ==
1248 PCI_DEVICE_ID_AMD_GOLAM_7450)) {
1249 /* amd shpc driver doesn't use Base Offset; assume 0 */
1250 ctrl->mmio_base = pci_resource_start(pdev, 0);
1251 ctrl->mmio_size = pci_resource_len(pdev, 0);
1253 ctrl->cap_offset = pci_find_capability(pdev, PCI_CAP_ID_SHPC);
1254 if (!ctrl->cap_offset) {
1255 err("%s : cap_offset == 0\n", __FUNCTION__);
1256 goto abort_free_ctlr;
1258 dbg("%s: cap_offset = %x\n", __FUNCTION__, ctrl->cap_offset);
1260 rc = shpc_indirect_creg_read(ctrl, 0, &shpc_base_offset);
1262 err("%s: cannot read base_offset\n", __FUNCTION__);
1263 goto abort_free_ctlr;
1266 rc = shpc_indirect_creg_read(ctrl, 3, &tempdword);
1268 err("%s: cannot read slot config\n", __FUNCTION__);
1269 goto abort_free_ctlr;
1271 num_slots = tempdword & SLOT_NUM;
1272 dbg("%s: num_slots (indirect) %x\n", __FUNCTION__, num_slots);
1274 for (i = 0; i < 9 + num_slots; i++) {
1275 rc = shpc_indirect_creg_read(ctrl, i, &tempdword);
1277 err("%s: cannot read creg (index = %d)\n",
1279 goto abort_free_ctlr;
1281 dbg("%s: offset %d: value %x\n", __FUNCTION__,i,
1286 pci_resource_start(pdev, 0) + shpc_base_offset;
1287 ctrl->mmio_size = 0x24 + 0x4 * num_slots;
1291 spin_lock_init(&hpc_event_lock);
1295 info("HPC vendor_id %x device_id %x ss_vid %x ss_did %x\n", pdev->vendor, pdev->device, pdev->subsystem_vendor,
1296 pdev->subsystem_device);
1298 if (pci_enable_device(pdev))
1299 goto abort_free_ctlr;
1301 if (!request_mem_region(ctrl->mmio_base, ctrl->mmio_size, MY_NAME)) {
1302 err("%s: cannot reserve MMIO region\n", __FUNCTION__);
1303 goto abort_free_ctlr;
1306 php_ctlr->creg = ioremap(ctrl->mmio_base, ctrl->mmio_size);
1307 if (!php_ctlr->creg) {
1308 err("%s: cannot remap MMIO region %lx @ %lx\n", __FUNCTION__,
1309 ctrl->mmio_size, ctrl->mmio_base);
1310 release_mem_region(ctrl->mmio_base, ctrl->mmio_size);
1311 goto abort_free_ctlr;
1313 dbg("%s: php_ctlr->creg %p\n", __FUNCTION__, php_ctlr->creg);
1315 mutex_init(&ctrl->crit_sect);
1316 mutex_init(&ctrl->cmd_lock);
1318 /* Setup wait queue */
1319 init_waitqueue_head(&ctrl->queue);
1322 php_ctlr->irq = pdev->irq;
1323 php_ctlr->attention_button_callback = shpchp_handle_attention_button,
1324 php_ctlr->switch_change_callback = shpchp_handle_switch_change;
1325 php_ctlr->presence_change_callback = shpchp_handle_presence_change;
1326 php_ctlr->power_fault_callback = shpchp_handle_power_fault;
1327 php_ctlr->callback_instance_id = instance_id;
1329 /* Return PCI Controller Info */
1330 php_ctlr->slot_device_offset = (readl(php_ctlr->creg + SLOT_CONFIG) & FIRST_DEV_NUM ) >> 8;
1331 php_ctlr->num_slots = readl(php_ctlr->creg + SLOT_CONFIG) & SLOT_NUM;
1332 dbg("%s: slot_device_offset %x\n", __FUNCTION__, php_ctlr->slot_device_offset);
1333 dbg("%s: num_slots %x\n", __FUNCTION__, php_ctlr->num_slots);
1335 /* Mask Global Interrupt Mask & Command Complete Interrupt Mask */
1336 tempdword = readl(php_ctlr->creg + SERR_INTR_ENABLE);
1337 dbg("%s: SERR_INTR_ENABLE = %x\n", __FUNCTION__, tempdword);
1338 tempdword = 0x0003000f;
1339 writel(tempdword, php_ctlr->creg + SERR_INTR_ENABLE);
1340 tempdword = readl(php_ctlr->creg + SERR_INTR_ENABLE);
1341 dbg("%s: SERR_INTR_ENABLE = %x\n", __FUNCTION__, tempdword);
1343 /* Mask the MRL sensor SERR Mask of individual slot in
1344 * Slot SERR-INT Mask & clear all the existing event if any
1346 for (hp_slot = 0; hp_slot < php_ctlr->num_slots; hp_slot++) {
1347 slot_reg = readl(php_ctlr->creg + SLOT1 + 4*hp_slot );
1348 dbg("%s: Default Logical Slot Register %d value %x\n", __FUNCTION__,
1350 tempdword = 0xffff3fff;
1351 writel(tempdword, php_ctlr->creg + SLOT1 + (4*hp_slot));
1354 if (shpchp_poll_mode) {/* Install interrupt polling code */
1355 /* Install and start the interrupt polling timer */
1356 init_timer(&php_ctlr->int_poll_timer);
1357 start_int_poll_timer( php_ctlr, 10 ); /* start with 10 second delay */
1359 /* Installs the interrupt handler */
1360 rc = pci_enable_msi(pdev);
1362 info("Can't get msi for the hotplug controller\n");
1363 info("Use INTx for the hotplug controller\n");
1365 php_ctlr->irq = pdev->irq;
1367 rc = request_irq(php_ctlr->irq, shpc_isr, SA_SHIRQ, MY_NAME, (void *) ctrl);
1368 dbg("%s: request_irq %d for hpc%d (returns %d)\n", __FUNCTION__, php_ctlr->irq, ctlr_seq_num, rc);
1370 err("Can't get irq %d for the hotplug controller\n", php_ctlr->irq);
1371 goto abort_free_ctlr;
1374 dbg("%s: HPC at b:d:f:irq=0x%x:%x:%x:%x\n", __FUNCTION__,
1375 pdev->bus->number, PCI_SLOT(pdev->devfn),
1376 PCI_FUNC(pdev->devfn), pdev->irq);
1377 get_hp_hw_control_from_firmware(pdev);
1379 /* Add this HPC instance into the HPC list */
1380 spin_lock(&list_lock);
1381 if (php_ctlr_list_head == 0) {
1382 php_ctlr_list_head = php_ctlr;
1383 p = php_ctlr_list_head;
1386 p = php_ctlr_list_head;
1391 p->pnext = php_ctlr;
1393 spin_unlock(&list_lock);
1397 ctrl->hpc_ctlr_handle = php_ctlr;
1398 ctrl->hpc_ops = &shpchp_hpc_ops;
1400 for (hp_slot = 0; hp_slot < php_ctlr->num_slots; hp_slot++) {
1401 slot_reg = readl(php_ctlr->creg + SLOT1 + 4*hp_slot );
1402 dbg("%s: Default Logical Slot Register %d value %x\n", __FUNCTION__,
1404 tempdword = 0xe01f3fff;
1405 writel(tempdword, php_ctlr->creg + SLOT1 + (4*hp_slot));
1407 if (!shpchp_poll_mode) {
1408 /* Unmask all general input interrupts and SERR */
1409 tempdword = readl(php_ctlr->creg + SERR_INTR_ENABLE);
1410 tempdword = 0x0000000a;
1411 writel(tempdword, php_ctlr->creg + SERR_INTR_ENABLE);
1412 tempdword = readl(php_ctlr->creg + SERR_INTR_ENABLE);
1413 dbg("%s: SERR_INTR_ENABLE = %x\n", __FUNCTION__, tempdword);
1419 /* We end up here for the many possible ways to fail this API. */