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 static inline u8 shpc_readb(struct controller *ctrl, int reg)
213 return readb(ctrl->hpc_ctlr_handle->creg + reg);
216 static inline void shpc_writeb(struct controller *ctrl, int reg, u8 val)
218 writeb(val, ctrl->hpc_ctlr_handle->creg + reg);
221 static inline u16 shpc_readw(struct controller *ctrl, int reg)
223 return readw(ctrl->hpc_ctlr_handle->creg + reg);
226 static inline void shpc_writew(struct controller *ctrl, int reg, u16 val)
228 writew(val, ctrl->hpc_ctlr_handle->creg + reg);
231 static inline u32 shpc_readl(struct controller *ctrl, int reg)
233 return readl(ctrl->hpc_ctlr_handle->creg + reg);
236 static inline void shpc_writel(struct controller *ctrl, int reg, u32 val)
238 writel(val, ctrl->hpc_ctlr_handle->creg + reg);
241 static inline int shpc_indirect_read(struct controller *ctrl, int index,
245 u32 cap_offset = ctrl->cap_offset;
246 struct pci_dev *pdev = ctrl->pci_dev;
248 rc = pci_write_config_byte(pdev, cap_offset + DWORD_SELECT, index);
251 return pci_read_config_dword(pdev, cap_offset + DWORD_DATA, value);
254 /* This is the interrupt polling timeout function. */
255 static void int_poll_timeout(unsigned long lphp_ctlr)
257 struct php_ctlr_state_s *php_ctlr = (struct php_ctlr_state_s *)lphp_ctlr;
262 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
266 /* Poll for interrupt events. regs == NULL => polling */
267 shpc_isr( 0, (void *)php_ctlr, NULL );
269 init_timer(&php_ctlr->int_poll_timer);
270 if (!shpchp_poll_time)
271 shpchp_poll_time = 2; /* reset timer to poll in 2 secs if user doesn't specify at module installation*/
273 start_int_poll_timer(php_ctlr, shpchp_poll_time);
278 /* This function starts the interrupt polling timer. */
279 static void start_int_poll_timer(struct php_ctlr_state_s *php_ctlr, int seconds)
282 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
286 if ( ( seconds <= 0 ) || ( seconds > 60 ) )
287 seconds = 2; /* Clamp to sane value */
289 php_ctlr->int_poll_timer.function = &int_poll_timeout;
290 php_ctlr->int_poll_timer.data = (unsigned long)php_ctlr; /* Instance data */
291 php_ctlr->int_poll_timer.expires = jiffies + seconds * HZ;
292 add_timer(&php_ctlr->int_poll_timer);
297 static inline int shpc_wait_cmd(struct controller *ctrl)
300 unsigned int timeout_msec = shpchp_poll_mode ? 2000 : 1000;
301 unsigned long timeout = msecs_to_jiffies(timeout_msec);
302 int rc = wait_event_interruptible_timeout(ctrl->queue,
303 !ctrl->cmd_busy, timeout);
306 err("Command not completed in %d msec\n", timeout_msec);
309 info("Command was interrupted by a signal\n");
316 static int shpc_write_cmd(struct slot *slot, u8 t_slot, u8 cmd)
318 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
319 struct controller *ctrl = slot->ctrl;
327 mutex_lock(&slot->ctrl->cmd_lock);
330 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
335 for (i = 0; i < 10; i++) {
336 cmd_status = shpc_readw(ctrl, CMD_STATUS);
338 if (!(cmd_status & 0x1))
340 /* Check every 0.1 sec for a total of 1 sec*/
344 cmd_status = shpc_readw(ctrl, CMD_STATUS);
346 if (cmd_status & 0x1) {
347 /* After 1 sec and and the controller is still busy */
348 err("%s : Controller is still busy after 1 sec.\n", __FUNCTION__);
354 temp_word = (t_slot << 8) | (cmd & 0xFF);
355 dbg("%s: t_slot %x cmd %x\n", __FUNCTION__, t_slot, cmd);
357 /* To make sure the Controller Busy bit is 0 before we send out the
360 slot->ctrl->cmd_busy = 1;
361 shpc_writew(ctrl, CMD, temp_word);
364 * Wait for command completion.
366 retval = shpc_wait_cmd(slot->ctrl);
370 cmd_status = hpc_check_cmd_status(slot->ctrl);
372 err("%s: Failed to issued command 0x%x (error code = %d)\n",
373 __FUNCTION__, cmd, cmd_status);
377 mutex_unlock(&slot->ctrl->cmd_lock);
383 static int hpc_check_cmd_status(struct controller *ctrl)
390 if (!ctrl->hpc_ctlr_handle) {
391 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
395 cmd_status = shpc_readw(ctrl, CMD_STATUS) & 0x000F;
397 switch (cmd_status >> 1) {
402 retval = SWITCH_OPEN;
403 err("%s: Switch opened!\n", __FUNCTION__);
406 retval = INVALID_CMD;
407 err("%s: Invalid HPC command!\n", __FUNCTION__);
410 retval = INVALID_SPEED_MODE;
411 err("%s: Invalid bus speed/mode!\n", __FUNCTION__);
422 static int hpc_get_attention_status(struct slot *slot, u8 *status)
424 struct controller *ctrl = slot->ctrl;
431 if (!slot->ctrl->hpc_ctlr_handle) {
432 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
436 slot_reg = shpc_readl(ctrl, SLOT1 + 4*(slot->hp_slot));
437 slot_status = (u16) slot_reg;
438 atten_led_state = (slot_status & 0x0030) >> 4;
440 switch (atten_led_state) {
442 *status = 0xFF; /* Reserved */
445 *status = 1; /* On */
448 *status = 2; /* Blink */
451 *status = 0; /* Off */
462 static int hpc_get_power_status(struct slot * slot, u8 *status)
464 struct controller *ctrl = slot->ctrl;
472 if (!slot->ctrl->hpc_ctlr_handle) {
473 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
477 slot_reg = shpc_readl(ctrl, SLOT1 + 4*(slot->hp_slot));
478 slot_status = (u16) slot_reg;
479 slot_state = (slot_status & 0x0003);
481 switch (slot_state) {
486 *status = 2; /* Powered only */
489 *status = 1; /* Enabled */
492 *status = 0; /* Disabled */
504 static int hpc_get_latch_status(struct slot *slot, u8 *status)
506 struct controller *ctrl = slot->ctrl;
512 if (!slot->ctrl->hpc_ctlr_handle) {
513 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
517 slot_reg = shpc_readl(ctrl, SLOT1 + 4*(slot->hp_slot));
518 slot_status = (u16)slot_reg;
520 *status = ((slot_status & 0x0100) == 0) ? 0 : 1; /* 0 -> close; 1 -> open */
527 static int hpc_get_adapter_status(struct slot *slot, u8 *status)
529 struct controller *ctrl = slot->ctrl;
536 if (!slot->ctrl->hpc_ctlr_handle) {
537 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
541 slot_reg = shpc_readl(ctrl, SLOT1 + 4*(slot->hp_slot));
542 slot_status = (u16)slot_reg;
543 card_state = (u8)((slot_status & 0x0C00) >> 10);
544 *status = (card_state != 0x3) ? 1 : 0;
550 static int hpc_get_prog_int(struct slot *slot, u8 *prog_int)
552 struct controller *ctrl = slot->ctrl;
556 if (!slot->ctrl->hpc_ctlr_handle) {
557 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
561 *prog_int = shpc_readb(ctrl, PROG_INTERFACE);
567 static int hpc_get_adapter_speed(struct slot *slot, enum pci_bus_speed *value)
570 struct controller *ctrl = slot->ctrl;
571 u32 slot_reg = shpc_readl(ctrl, SLOT1 + 4 * slot->hp_slot);
572 u8 pcix_cap = (slot_reg >> 12) & 7;
573 u8 m66_cap = (slot_reg >> 9) & 1;
577 dbg("%s: slot_reg = %x, pcix_cap = %x, m66_cap = %x\n",
578 __FUNCTION__, slot_reg, pcix_cap, m66_cap);
582 *value = m66_cap ? PCI_SPEED_66MHz : PCI_SPEED_33MHz;
585 *value = PCI_SPEED_66MHz_PCIX;
588 *value = PCI_SPEED_133MHz_PCIX;
591 *value = PCI_SPEED_133MHz_PCIX_266;
594 *value = PCI_SPEED_133MHz_PCIX_533;
598 *value = PCI_SPEED_UNKNOWN;
603 dbg("Adapter speed = %d\n", *value);
608 static int hpc_get_mode1_ECC_cap(struct slot *slot, u8 *mode)
610 struct controller *ctrl = slot->ctrl;
617 if (!slot->ctrl->hpc_ctlr_handle) {
618 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
622 pi = shpc_readb(ctrl, PROG_INTERFACE);
623 sec_bus_status = shpc_readw(ctrl, SEC_BUS_CONFIG);
626 *mode = (sec_bus_status & 0x0100) >> 8;
631 dbg("Mode 1 ECC cap = %d\n", *mode);
637 static int hpc_query_power_fault(struct slot * slot)
639 struct controller *ctrl = slot->ctrl;
642 u8 pwr_fault_state, status;
646 if (!slot->ctrl->hpc_ctlr_handle) {
647 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
651 slot_reg = shpc_readl(ctrl, SLOT1 + 4*(slot->hp_slot));
652 slot_status = (u16) slot_reg;
653 pwr_fault_state = (slot_status & 0x0040) >> 7;
654 status = (pwr_fault_state == 1) ? 0 : 1;
657 /* Note: Logic 0 => fault */
661 static int hpc_set_attention_status(struct slot *slot, u8 value)
663 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
667 if (!slot->ctrl->hpc_ctlr_handle) {
668 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
672 if (slot->hp_slot >= php_ctlr->num_slots) {
673 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
679 slot_cmd = 0x30; /* OFF */
682 slot_cmd = 0x10; /* ON */
685 slot_cmd = 0x20; /* BLINK */
691 shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
697 static void hpc_set_green_led_on(struct slot *slot)
699 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
702 if (!slot->ctrl->hpc_ctlr_handle) {
703 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
707 if (slot->hp_slot >= php_ctlr->num_slots) {
708 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
714 shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
719 static void hpc_set_green_led_off(struct slot *slot)
721 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
724 if (!slot->ctrl->hpc_ctlr_handle) {
725 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
729 if (slot->hp_slot >= php_ctlr->num_slots) {
730 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
736 shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
741 static void hpc_set_green_led_blink(struct slot *slot)
743 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
746 if (!slot->ctrl->hpc_ctlr_handle) {
747 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
751 if (slot->hp_slot >= php_ctlr->num_slots) {
752 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
758 shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
763 int shpc_get_ctlr_slot_config(struct controller *ctrl,
764 int *num_ctlr_slots, /* number of slots in this HPC */
765 int *first_device_num, /* PCI dev num of the first slot in this SHPC */
766 int *physical_slot_num, /* phy slot num of the first slot in this SHPC */
767 int *updown, /* physical_slot_num increament: 1 or -1 */
774 if (!ctrl->hpc_ctlr_handle) {
775 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
779 slot_config = shpc_readl(ctrl, SLOT_CONFIG);
780 *first_device_num = (slot_config & FIRST_DEV_NUM) >> 8;
781 *num_ctlr_slots = slot_config & SLOT_NUM;
782 *physical_slot_num = (slot_config & PSN) >> 16;
783 *updown = ((slot_config & UPDOWN) >> 29) ? 1 : -1;
785 dbg("%s: physical_slot_num = %x\n", __FUNCTION__, *physical_slot_num);
791 static void hpc_release_ctlr(struct controller *ctrl)
793 struct php_ctlr_state_s *php_ctlr = ctrl->hpc_ctlr_handle;
794 struct php_ctlr_state_s *p, *p_prev;
799 if (!ctrl->hpc_ctlr_handle) {
800 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
805 * Mask all slot event interrupts
807 for (i = 0; i < ctrl->num_slots; i++)
808 shpc_writel(ctrl, SLOT1 + (4 * i), 0xffff3fff);
812 if (shpchp_poll_mode) {
813 del_timer(&php_ctlr->int_poll_timer);
816 free_irq(php_ctlr->irq, ctrl);
818 pci_disable_msi(php_ctlr->pci_dev);
822 if (php_ctlr->pci_dev) {
823 iounmap(php_ctlr->creg);
824 release_mem_region(ctrl->mmio_base, ctrl->mmio_size);
825 php_ctlr->pci_dev = NULL;
828 spin_lock(&list_lock);
829 p = php_ctlr_list_head;
834 p_prev->pnext = p->pnext;
836 php_ctlr_list_head = p->pnext;
843 spin_unlock(&list_lock);
851 static int hpc_power_on_slot(struct slot * slot)
853 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
859 if (!slot->ctrl->hpc_ctlr_handle) {
860 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
864 if (slot->hp_slot >= php_ctlr->num_slots) {
865 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
870 retval = shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
873 err("%s: Write command failed!\n", __FUNCTION__);
882 static int hpc_slot_enable(struct slot * slot)
884 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
890 if (!slot->ctrl->hpc_ctlr_handle) {
891 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
895 if (slot->hp_slot >= php_ctlr->num_slots) {
896 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
899 /* 3A => Slot - Enable, Power Indicator - Blink, Attention Indicator - Off */
902 retval = shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
905 err("%s: Write command failed!\n", __FUNCTION__);
913 static int hpc_slot_disable(struct slot * slot)
915 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
921 if (!slot->ctrl->hpc_ctlr_handle) {
922 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
926 if (slot->hp_slot >= php_ctlr->num_slots) {
927 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
931 /* 1F => Slot - Disable, Power Indicator - Off, Attention Indicator - On */
934 retval = shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
937 err("%s: Write command failed!\n", __FUNCTION__);
945 static int hpc_set_bus_speed_mode(struct slot * slot, enum pci_bus_speed value)
948 struct controller *ctrl = slot->ctrl;
953 pi = shpc_readb(ctrl, PROG_INTERFACE);
954 if ((pi == 1) && (value > PCI_SPEED_133MHz_PCIX))
958 case PCI_SPEED_33MHz:
959 cmd = SETA_PCI_33MHZ;
961 case PCI_SPEED_66MHz:
962 cmd = SETA_PCI_66MHZ;
964 case PCI_SPEED_66MHz_PCIX:
965 cmd = SETA_PCIX_66MHZ;
967 case PCI_SPEED_100MHz_PCIX:
968 cmd = SETA_PCIX_100MHZ;
970 case PCI_SPEED_133MHz_PCIX:
971 cmd = SETA_PCIX_133MHZ;
973 case PCI_SPEED_66MHz_PCIX_ECC:
974 cmd = SETB_PCIX_66MHZ_EM;
976 case PCI_SPEED_100MHz_PCIX_ECC:
977 cmd = SETB_PCIX_100MHZ_EM;
979 case PCI_SPEED_133MHz_PCIX_ECC:
980 cmd = SETB_PCIX_133MHZ_EM;
982 case PCI_SPEED_66MHz_PCIX_266:
983 cmd = SETB_PCIX_66MHZ_266;
985 case PCI_SPEED_100MHz_PCIX_266:
986 cmd = SETB_PCIX_100MHZ_266;
988 case PCI_SPEED_133MHz_PCIX_266:
989 cmd = SETB_PCIX_133MHZ_266;
991 case PCI_SPEED_66MHz_PCIX_533:
992 cmd = SETB_PCIX_66MHZ_533;
994 case PCI_SPEED_100MHz_PCIX_533:
995 cmd = SETB_PCIX_100MHZ_533;
997 case PCI_SPEED_133MHz_PCIX_533:
998 cmd = SETB_PCIX_133MHZ_533;
1004 retval = shpc_write_cmd(slot, 0, cmd);
1006 err("%s: Write command failed!\n", __FUNCTION__);
1012 static irqreturn_t shpc_isr(int IRQ, void *dev_id, struct pt_regs *regs)
1014 struct controller *ctrl = NULL;
1015 struct php_ctlr_state_s *php_ctlr;
1016 u8 schedule_flag = 0;
1018 u32 temp_dword, intr_loc, intr_loc2;
1024 if (!shpchp_poll_mode) {
1025 ctrl = (struct controller *)dev_id;
1026 php_ctlr = ctrl->hpc_ctlr_handle;
1028 php_ctlr = (struct php_ctlr_state_s *) dev_id;
1029 ctrl = (struct controller *)php_ctlr->callback_instance_id;
1035 if (!php_ctlr || !php_ctlr->creg)
1038 /* Check to see if it was our interrupt */
1039 intr_loc = shpc_readl(ctrl, INTR_LOC);
1043 dbg("%s: intr_loc = %x\n",__FUNCTION__, intr_loc);
1045 if(!shpchp_poll_mode) {
1046 /* Mask Global Interrupt Mask - see implementation note on p. 139 */
1047 /* of SHPC spec rev 1.0*/
1048 temp_dword = shpc_readl(ctrl, SERR_INTR_ENABLE);
1049 temp_dword |= 0x00000001;
1050 shpc_writel(ctrl, SERR_INTR_ENABLE, temp_dword);
1052 intr_loc2 = shpc_readl(ctrl, INTR_LOC);
1053 dbg("%s: intr_loc2 = %x\n",__FUNCTION__, intr_loc2);
1056 if (intr_loc & 0x0001) {
1058 * Command Complete Interrupt Pending
1059 * RO only - clear by writing 1 to the Command Completion
1060 * Detect bit in Controller SERR-INT register
1062 temp_dword = shpc_readl(ctrl, SERR_INTR_ENABLE);
1063 temp_dword &= 0xfffdffff;
1064 shpc_writel(ctrl, SERR_INTR_ENABLE, temp_dword);
1066 wake_up_interruptible(&ctrl->queue);
1069 if ((intr_loc = (intr_loc >> 1)) == 0)
1072 for (hp_slot = 0; hp_slot < ctrl->num_slots; hp_slot++) {
1073 /* To find out which slot has interrupt pending */
1074 if ((intr_loc >> hp_slot) & 0x01) {
1075 temp_dword = shpc_readl(ctrl, SLOT1 + (4*hp_slot));
1076 dbg("%s: Slot %x with intr, slot register = %x\n",
1077 __FUNCTION__, hp_slot, temp_dword);
1078 temp_byte = (temp_dword >> 16) & 0xFF;
1079 if ((php_ctlr->switch_change_callback) && (temp_byte & 0x08))
1080 schedule_flag += php_ctlr->switch_change_callback(
1081 hp_slot, php_ctlr->callback_instance_id);
1082 if ((php_ctlr->attention_button_callback) && (temp_byte & 0x04))
1083 schedule_flag += php_ctlr->attention_button_callback(
1084 hp_slot, php_ctlr->callback_instance_id);
1085 if ((php_ctlr->presence_change_callback) && (temp_byte & 0x01))
1086 schedule_flag += php_ctlr->presence_change_callback(
1087 hp_slot , php_ctlr->callback_instance_id);
1088 if ((php_ctlr->power_fault_callback) && (temp_byte & 0x12))
1089 schedule_flag += php_ctlr->power_fault_callback(
1090 hp_slot, php_ctlr->callback_instance_id);
1092 /* Clear all slot events */
1093 temp_dword = 0xe01f3fff;
1094 shpc_writel(ctrl, SLOT1 + (4*hp_slot), temp_dword);
1096 intr_loc2 = shpc_readl(ctrl, INTR_LOC);
1097 dbg("%s: intr_loc2 = %x\n",__FUNCTION__, intr_loc2);
1101 if (!shpchp_poll_mode) {
1102 /* Unmask Global Interrupt Mask */
1103 temp_dword = shpc_readl(ctrl, SERR_INTR_ENABLE);
1104 temp_dword &= 0xfffffffe;
1105 shpc_writel(ctrl, SERR_INTR_ENABLE, temp_dword);
1111 static int hpc_get_max_bus_speed (struct slot *slot, enum pci_bus_speed *value)
1114 struct controller *ctrl = slot->ctrl;
1115 enum pci_bus_speed bus_speed = PCI_SPEED_UNKNOWN;
1116 u8 pi = shpc_readb(ctrl, PROG_INTERFACE);
1117 u32 slot_avail1 = shpc_readl(ctrl, SLOT_AVAIL1);
1118 u32 slot_avail2 = shpc_readl(ctrl, SLOT_AVAIL2);
1123 if (slot_avail2 & SLOT_133MHZ_PCIX_533)
1124 bus_speed = PCI_SPEED_133MHz_PCIX_533;
1125 else if (slot_avail2 & SLOT_100MHZ_PCIX_533)
1126 bus_speed = PCI_SPEED_100MHz_PCIX_533;
1127 else if (slot_avail2 & SLOT_66MHZ_PCIX_533)
1128 bus_speed = PCI_SPEED_66MHz_PCIX_533;
1129 else if (slot_avail2 & SLOT_133MHZ_PCIX_266)
1130 bus_speed = PCI_SPEED_133MHz_PCIX_266;
1131 else if (slot_avail2 & SLOT_100MHZ_PCIX_266)
1132 bus_speed = PCI_SPEED_100MHz_PCIX_266;
1133 else if (slot_avail2 & SLOT_66MHZ_PCIX_266)
1134 bus_speed = PCI_SPEED_66MHz_PCIX_266;
1137 if (bus_speed == PCI_SPEED_UNKNOWN) {
1138 if (slot_avail1 & SLOT_133MHZ_PCIX)
1139 bus_speed = PCI_SPEED_133MHz_PCIX;
1140 else if (slot_avail1 & SLOT_100MHZ_PCIX)
1141 bus_speed = PCI_SPEED_100MHz_PCIX;
1142 else if (slot_avail1 & SLOT_66MHZ_PCIX)
1143 bus_speed = PCI_SPEED_66MHz_PCIX;
1144 else if (slot_avail2 & SLOT_66MHZ)
1145 bus_speed = PCI_SPEED_66MHz;
1146 else if (slot_avail1 & SLOT_33MHZ)
1147 bus_speed = PCI_SPEED_33MHz;
1153 dbg("Max bus speed = %d\n", bus_speed);
1158 static int hpc_get_cur_bus_speed (struct slot *slot, enum pci_bus_speed *value)
1161 struct controller *ctrl = slot->ctrl;
1162 enum pci_bus_speed bus_speed = PCI_SPEED_UNKNOWN;
1163 u16 sec_bus_reg = shpc_readw(ctrl, SEC_BUS_CONFIG);
1164 u8 pi = shpc_readb(ctrl, PROG_INTERFACE);
1165 u8 speed_mode = (pi == 2) ? (sec_bus_reg & 0xF) : (sec_bus_reg & 0x7);
1169 if ((pi == 1) && (speed_mode > 4)) {
1170 *value = PCI_SPEED_UNKNOWN;
1174 switch (speed_mode) {
1176 *value = PCI_SPEED_33MHz;
1179 *value = PCI_SPEED_66MHz;
1182 *value = PCI_SPEED_66MHz_PCIX;
1185 *value = PCI_SPEED_100MHz_PCIX;
1188 *value = PCI_SPEED_133MHz_PCIX;
1191 *value = PCI_SPEED_66MHz_PCIX_ECC;
1194 *value = PCI_SPEED_100MHz_PCIX_ECC;
1197 *value = PCI_SPEED_133MHz_PCIX_ECC;
1200 *value = PCI_SPEED_66MHz_PCIX_266;
1203 *value = PCI_SPEED_100MHz_PCIX_266;
1206 *value = PCI_SPEED_133MHz_PCIX_266;
1209 *value = PCI_SPEED_66MHz_PCIX_533;
1212 *value = PCI_SPEED_100MHz_PCIX_533;
1215 *value = PCI_SPEED_133MHz_PCIX_533;
1218 *value = PCI_SPEED_UNKNOWN;
1223 dbg("Current bus speed = %d\n", bus_speed);
1228 static struct hpc_ops shpchp_hpc_ops = {
1229 .power_on_slot = hpc_power_on_slot,
1230 .slot_enable = hpc_slot_enable,
1231 .slot_disable = hpc_slot_disable,
1232 .set_bus_speed_mode = hpc_set_bus_speed_mode,
1233 .set_attention_status = hpc_set_attention_status,
1234 .get_power_status = hpc_get_power_status,
1235 .get_attention_status = hpc_get_attention_status,
1236 .get_latch_status = hpc_get_latch_status,
1237 .get_adapter_status = hpc_get_adapter_status,
1239 .get_max_bus_speed = hpc_get_max_bus_speed,
1240 .get_cur_bus_speed = hpc_get_cur_bus_speed,
1241 .get_adapter_speed = hpc_get_adapter_speed,
1242 .get_mode1_ECC_cap = hpc_get_mode1_ECC_cap,
1243 .get_prog_int = hpc_get_prog_int,
1245 .query_power_fault = hpc_query_power_fault,
1246 .green_led_on = hpc_set_green_led_on,
1247 .green_led_off = hpc_set_green_led_off,
1248 .green_led_blink = hpc_set_green_led_blink,
1250 .release_ctlr = hpc_release_ctlr,
1253 int shpc_init(struct controller * ctrl, struct pci_dev * pdev)
1255 struct php_ctlr_state_s *php_ctlr, *p;
1256 void *instance_id = ctrl;
1257 int rc, num_slots = 0;
1259 static int first = 1;
1260 u32 shpc_base_offset;
1261 u32 tempdword, slot_reg, slot_config;
1266 ctrl->pci_dev = pdev; /* pci_dev of the P2P bridge */
1268 spin_lock_init(&list_lock);
1269 php_ctlr = kzalloc(sizeof(*php_ctlr), GFP_KERNEL);
1271 if (!php_ctlr) { /* allocate controller state data */
1272 err("%s: HPC controller memory allocation error!\n", __FUNCTION__);
1276 php_ctlr->pci_dev = pdev; /* save pci_dev in context */
1278 if ((pdev->vendor == PCI_VENDOR_ID_AMD) || (pdev->device ==
1279 PCI_DEVICE_ID_AMD_GOLAM_7450)) {
1280 /* amd shpc driver doesn't use Base Offset; assume 0 */
1281 ctrl->mmio_base = pci_resource_start(pdev, 0);
1282 ctrl->mmio_size = pci_resource_len(pdev, 0);
1284 ctrl->cap_offset = pci_find_capability(pdev, PCI_CAP_ID_SHPC);
1285 if (!ctrl->cap_offset) {
1286 err("%s : cap_offset == 0\n", __FUNCTION__);
1287 goto abort_free_ctlr;
1289 dbg("%s: cap_offset = %x\n", __FUNCTION__, ctrl->cap_offset);
1291 rc = shpc_indirect_read(ctrl, 0, &shpc_base_offset);
1293 err("%s: cannot read base_offset\n", __FUNCTION__);
1294 goto abort_free_ctlr;
1297 rc = shpc_indirect_read(ctrl, 3, &tempdword);
1299 err("%s: cannot read slot config\n", __FUNCTION__);
1300 goto abort_free_ctlr;
1302 num_slots = tempdword & SLOT_NUM;
1303 dbg("%s: num_slots (indirect) %x\n", __FUNCTION__, num_slots);
1305 for (i = 0; i < 9 + num_slots; i++) {
1306 rc = shpc_indirect_read(ctrl, i, &tempdword);
1308 err("%s: cannot read creg (index = %d)\n",
1310 goto abort_free_ctlr;
1312 dbg("%s: offset %d: value %x\n", __FUNCTION__,i,
1317 pci_resource_start(pdev, 0) + shpc_base_offset;
1318 ctrl->mmio_size = 0x24 + 0x4 * num_slots;
1322 spin_lock_init(&hpc_event_lock);
1326 info("HPC vendor_id %x device_id %x ss_vid %x ss_did %x\n", pdev->vendor, pdev->device, pdev->subsystem_vendor,
1327 pdev->subsystem_device);
1329 if (pci_enable_device(pdev))
1330 goto abort_free_ctlr;
1332 if (!request_mem_region(ctrl->mmio_base, ctrl->mmio_size, MY_NAME)) {
1333 err("%s: cannot reserve MMIO region\n", __FUNCTION__);
1334 goto abort_free_ctlr;
1337 php_ctlr->creg = ioremap(ctrl->mmio_base, ctrl->mmio_size);
1338 if (!php_ctlr->creg) {
1339 err("%s: cannot remap MMIO region %lx @ %lx\n", __FUNCTION__,
1340 ctrl->mmio_size, ctrl->mmio_base);
1341 release_mem_region(ctrl->mmio_base, ctrl->mmio_size);
1342 goto abort_free_ctlr;
1344 dbg("%s: php_ctlr->creg %p\n", __FUNCTION__, php_ctlr->creg);
1346 mutex_init(&ctrl->crit_sect);
1347 mutex_init(&ctrl->cmd_lock);
1349 /* Setup wait queue */
1350 init_waitqueue_head(&ctrl->queue);
1353 php_ctlr->irq = pdev->irq;
1354 php_ctlr->attention_button_callback = shpchp_handle_attention_button,
1355 php_ctlr->switch_change_callback = shpchp_handle_switch_change;
1356 php_ctlr->presence_change_callback = shpchp_handle_presence_change;
1357 php_ctlr->power_fault_callback = shpchp_handle_power_fault;
1358 php_ctlr->callback_instance_id = instance_id;
1360 ctrl->hpc_ctlr_handle = php_ctlr;
1361 ctrl->hpc_ops = &shpchp_hpc_ops;
1363 /* Return PCI Controller Info */
1364 slot_config = shpc_readl(ctrl, SLOT_CONFIG);
1365 php_ctlr->slot_device_offset = (slot_config & FIRST_DEV_NUM) >> 8;
1366 php_ctlr->num_slots = slot_config & SLOT_NUM;
1367 dbg("%s: slot_device_offset %x\n", __FUNCTION__, php_ctlr->slot_device_offset);
1368 dbg("%s: num_slots %x\n", __FUNCTION__, php_ctlr->num_slots);
1370 /* Mask Global Interrupt Mask & Command Complete Interrupt Mask */
1371 tempdword = shpc_readl(ctrl, SERR_INTR_ENABLE);
1372 dbg("%s: SERR_INTR_ENABLE = %x\n", __FUNCTION__, tempdword);
1373 tempdword = 0x0003000f;
1374 shpc_writel(ctrl, SERR_INTR_ENABLE, tempdword);
1375 tempdword = shpc_readl(ctrl, SERR_INTR_ENABLE);
1376 dbg("%s: SERR_INTR_ENABLE = %x\n", __FUNCTION__, tempdword);
1378 /* Mask the MRL sensor SERR Mask of individual slot in
1379 * Slot SERR-INT Mask & clear all the existing event if any
1381 for (hp_slot = 0; hp_slot < php_ctlr->num_slots; hp_slot++) {
1382 slot_reg = shpc_readl(ctrl, SLOT1 + 4*hp_slot );
1383 dbg("%s: Default Logical Slot Register %d value %x\n", __FUNCTION__,
1385 tempdword = 0xffff3fff;
1386 shpc_writel(ctrl, SLOT1 + (4*hp_slot), tempdword);
1389 if (shpchp_poll_mode) {/* Install interrupt polling code */
1390 /* Install and start the interrupt polling timer */
1391 init_timer(&php_ctlr->int_poll_timer);
1392 start_int_poll_timer( php_ctlr, 10 ); /* start with 10 second delay */
1394 /* Installs the interrupt handler */
1395 rc = pci_enable_msi(pdev);
1397 info("Can't get msi for the hotplug controller\n");
1398 info("Use INTx for the hotplug controller\n");
1400 php_ctlr->irq = pdev->irq;
1402 rc = request_irq(php_ctlr->irq, shpc_isr, SA_SHIRQ, MY_NAME, (void *) ctrl);
1403 dbg("%s: request_irq %d for hpc%d (returns %d)\n", __FUNCTION__, php_ctlr->irq, ctlr_seq_num, rc);
1405 err("Can't get irq %d for the hotplug controller\n", php_ctlr->irq);
1406 goto abort_free_ctlr;
1409 dbg("%s: HPC at b:d:f:irq=0x%x:%x:%x:%x\n", __FUNCTION__,
1410 pdev->bus->number, PCI_SLOT(pdev->devfn),
1411 PCI_FUNC(pdev->devfn), pdev->irq);
1412 get_hp_hw_control_from_firmware(pdev);
1414 /* Add this HPC instance into the HPC list */
1415 spin_lock(&list_lock);
1416 if (php_ctlr_list_head == 0) {
1417 php_ctlr_list_head = php_ctlr;
1418 p = php_ctlr_list_head;
1421 p = php_ctlr_list_head;
1426 p->pnext = php_ctlr;
1428 spin_unlock(&list_lock);
1432 for (hp_slot = 0; hp_slot < php_ctlr->num_slots; hp_slot++) {
1433 slot_reg = shpc_readl(ctrl, SLOT1 + 4*hp_slot );
1434 dbg("%s: Default Logical Slot Register %d value %x\n", __FUNCTION__,
1436 tempdword = 0xe01f3fff;
1437 shpc_writel(ctrl, SLOT1 + (4*hp_slot), tempdword);
1439 if (!shpchp_poll_mode) {
1440 /* Unmask all general input interrupts and SERR */
1441 tempdword = shpc_readl(ctrl, SERR_INTR_ENABLE);
1442 tempdword = 0x0000000a;
1443 shpc_writel(ctrl, SERR_INTR_ENABLE, tempdword);
1444 tempdword = shpc_readl(ctrl, SERR_INTR_ENABLE);
1445 dbg("%s: SERR_INTR_ENABLE = %x\n", __FUNCTION__, tempdword);
1451 /* We end up here for the many possible ways to fail this API. */