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
94 * Controller SERR-INT Register
96 #define GLOBAL_INTR_MASK (1 << 0)
97 #define GLOBAL_SERR_MASK (1 << 1)
98 #define COMMAND_INTR_MASK (1 << 2)
99 #define ARBITER_SERR_MASK (1 << 3)
100 #define COMMAND_DETECTED (1 << 16)
101 #define ARBITER_DETECTED (1 << 17)
102 #define SERR_INTR_RSVDZ_MASK 0xfffc0000
105 * Logical Slot Register definitions
107 #define SLOT_REG(i) (SLOT1 + (4 * i))
109 #define SLOT_STATE_SHIFT (0)
110 #define SLOT_STATE_MASK (3 << 0)
111 #define SLOT_STATE_PWRONLY (1)
112 #define SLOT_STATE_ENABLED (2)
113 #define SLOT_STATE_DISABLED (3)
114 #define PWR_LED_STATE_SHIFT (2)
115 #define PWR_LED_STATE_MASK (3 << 2)
116 #define ATN_LED_STATE_SHIFT (4)
117 #define ATN_LED_STATE_MASK (3 << 4)
118 #define ATN_LED_STATE_ON (1)
119 #define ATN_LED_STATE_BLINK (2)
120 #define ATN_LED_STATE_OFF (3)
121 #define POWER_FAULT (1 << 6)
122 #define ATN_BUTTON (1 << 7)
123 #define MRL_SENSOR (1 << 8)
124 #define MHZ66_CAP (1 << 9)
125 #define PRSNT_SHIFT (10)
126 #define PRSNT_MASK (3 << 10)
127 #define PCIX_CAP_SHIFT (12)
128 #define PCIX_CAP_MASK_PI1 (3 << 12)
129 #define PCIX_CAP_MASK_PI2 (7 << 12)
130 #define PRSNT_CHANGE_DETECTED (1 << 16)
131 #define ISO_PFAULT_DETECTED (1 << 17)
132 #define BUTTON_PRESS_DETECTED (1 << 18)
133 #define MRL_CHANGE_DETECTED (1 << 19)
134 #define CON_PFAULT_DETECTED (1 << 20)
135 #define PRSNT_CHANGE_INTR_MASK (1 << 24)
136 #define ISO_PFAULT_INTR_MASK (1 << 25)
137 #define BUTTON_PRESS_INTR_MASK (1 << 26)
138 #define MRL_CHANGE_INTR_MASK (1 << 27)
139 #define CON_PFAULT_INTR_MASK (1 << 28)
140 #define MRL_CHANGE_SERR_MASK (1 << 29)
141 #define CON_PFAULT_SERR_MASK (1 << 30)
142 #define SLOT_REG_RSVDZ_MASK (1 << 15) | (7 << 21)
144 /* SHPC 'write' operations/commands */
146 /* Slot operation - 0x00h to 0x3Fh */
148 #define NO_CHANGE 0x00
150 /* Slot state - Bits 0 & 1 of controller command register */
151 #define SET_SLOT_PWR 0x01
152 #define SET_SLOT_ENABLE 0x02
153 #define SET_SLOT_DISABLE 0x03
155 /* Power indicator state - Bits 2 & 3 of controller command register*/
156 #define SET_PWR_ON 0x04
157 #define SET_PWR_BLINK 0x08
158 #define SET_PWR_OFF 0x0C
160 /* Attention indicator state - Bits 4 & 5 of controller command register*/
161 #define SET_ATTN_ON 0x010
162 #define SET_ATTN_BLINK 0x020
163 #define SET_ATTN_OFF 0x030
165 /* Set bus speed/mode A - 0x40h to 0x47h */
166 #define SETA_PCI_33MHZ 0x40
167 #define SETA_PCI_66MHZ 0x41
168 #define SETA_PCIX_66MHZ 0x42
169 #define SETA_PCIX_100MHZ 0x43
170 #define SETA_PCIX_133MHZ 0x44
171 #define RESERV_1 0x45
172 #define RESERV_2 0x46
173 #define RESERV_3 0x47
175 /* Set bus speed/mode B - 0x50h to 0x5fh */
176 #define SETB_PCI_33MHZ 0x50
177 #define SETB_PCI_66MHZ 0x51
178 #define SETB_PCIX_66MHZ_PM 0x52
179 #define SETB_PCIX_100MHZ_PM 0x53
180 #define SETB_PCIX_133MHZ_PM 0x54
181 #define SETB_PCIX_66MHZ_EM 0x55
182 #define SETB_PCIX_100MHZ_EM 0x56
183 #define SETB_PCIX_133MHZ_EM 0x57
184 #define SETB_PCIX_66MHZ_266 0x58
185 #define SETB_PCIX_100MHZ_266 0x59
186 #define SETB_PCIX_133MHZ_266 0x5a
187 #define SETB_PCIX_66MHZ_533 0x5b
188 #define SETB_PCIX_100MHZ_533 0x5c
189 #define SETB_PCIX_133MHZ_533 0x5d
192 /* Power-on all slots - 0x48h */
193 #define SET_PWR_ON_ALL 0x48
195 /* Enable all slots - 0x49h */
196 #define SET_ENABLE_ALL 0x49
198 /* SHPC controller command error code */
199 #define SWITCH_OPEN 0x1
200 #define INVALID_CMD 0x2
201 #define INVALID_SPEED_MODE 0x4
203 /* For accessing SHPC Working Register Set */
204 #define DWORD_SELECT 0x2
205 #define DWORD_DATA 0x4
206 #define BASE_OFFSET 0x0
208 /* Field Offset in Logical Slot Register - byte boundary */
209 #define SLOT_EVENT_LATCH 0x2
210 #define SLOT_SERR_INT_MASK 0x3
212 static spinlock_t hpc_event_lock;
214 DEFINE_DBG_BUFFER /* Debug string buffer for entire HPC defined here */
215 static struct php_ctlr_state_s *php_ctlr_list_head; /* HPC state linked list */
216 static int ctlr_seq_num = 0; /* Controller sequenc # */
217 static spinlock_t list_lock;
219 static irqreturn_t shpc_isr(int IRQ, void *dev_id, struct pt_regs *regs);
221 static void start_int_poll_timer(struct php_ctlr_state_s *php_ctlr, int seconds);
222 static int hpc_check_cmd_status(struct controller *ctrl);
224 static inline u8 shpc_readb(struct controller *ctrl, int reg)
226 return readb(ctrl->hpc_ctlr_handle->creg + reg);
229 static inline void shpc_writeb(struct controller *ctrl, int reg, u8 val)
231 writeb(val, ctrl->hpc_ctlr_handle->creg + reg);
234 static inline u16 shpc_readw(struct controller *ctrl, int reg)
236 return readw(ctrl->hpc_ctlr_handle->creg + reg);
239 static inline void shpc_writew(struct controller *ctrl, int reg, u16 val)
241 writew(val, ctrl->hpc_ctlr_handle->creg + reg);
244 static inline u32 shpc_readl(struct controller *ctrl, int reg)
246 return readl(ctrl->hpc_ctlr_handle->creg + reg);
249 static inline void shpc_writel(struct controller *ctrl, int reg, u32 val)
251 writel(val, ctrl->hpc_ctlr_handle->creg + reg);
254 static inline int shpc_indirect_read(struct controller *ctrl, int index,
258 u32 cap_offset = ctrl->cap_offset;
259 struct pci_dev *pdev = ctrl->pci_dev;
261 rc = pci_write_config_byte(pdev, cap_offset + DWORD_SELECT, index);
264 return pci_read_config_dword(pdev, cap_offset + DWORD_DATA, value);
267 /* This is the interrupt polling timeout function. */
268 static void int_poll_timeout(unsigned long lphp_ctlr)
270 struct php_ctlr_state_s *php_ctlr = (struct php_ctlr_state_s *)lphp_ctlr;
275 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
279 /* Poll for interrupt events. regs == NULL => polling */
280 shpc_isr( 0, (void *)php_ctlr, NULL );
282 init_timer(&php_ctlr->int_poll_timer);
283 if (!shpchp_poll_time)
284 shpchp_poll_time = 2; /* reset timer to poll in 2 secs if user doesn't specify at module installation*/
286 start_int_poll_timer(php_ctlr, shpchp_poll_time);
291 /* This function starts the interrupt polling timer. */
292 static void start_int_poll_timer(struct php_ctlr_state_s *php_ctlr, int seconds)
295 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
299 if ( ( seconds <= 0 ) || ( seconds > 60 ) )
300 seconds = 2; /* Clamp to sane value */
302 php_ctlr->int_poll_timer.function = &int_poll_timeout;
303 php_ctlr->int_poll_timer.data = (unsigned long)php_ctlr; /* Instance data */
304 php_ctlr->int_poll_timer.expires = jiffies + seconds * HZ;
305 add_timer(&php_ctlr->int_poll_timer);
310 static inline int shpc_wait_cmd(struct controller *ctrl)
313 unsigned int timeout_msec = shpchp_poll_mode ? 2000 : 1000;
314 unsigned long timeout = msecs_to_jiffies(timeout_msec);
315 int rc = wait_event_interruptible_timeout(ctrl->queue,
316 !ctrl->cmd_busy, timeout);
319 err("Command not completed in %d msec\n", timeout_msec);
322 info("Command was interrupted by a signal\n");
329 static int shpc_write_cmd(struct slot *slot, u8 t_slot, u8 cmd)
331 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
332 struct controller *ctrl = slot->ctrl;
340 mutex_lock(&slot->ctrl->cmd_lock);
343 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
348 for (i = 0; i < 10; i++) {
349 cmd_status = shpc_readw(ctrl, CMD_STATUS);
351 if (!(cmd_status & 0x1))
353 /* Check every 0.1 sec for a total of 1 sec*/
357 cmd_status = shpc_readw(ctrl, CMD_STATUS);
359 if (cmd_status & 0x1) {
360 /* After 1 sec and and the controller is still busy */
361 err("%s : Controller is still busy after 1 sec.\n", __FUNCTION__);
367 temp_word = (t_slot << 8) | (cmd & 0xFF);
368 dbg("%s: t_slot %x cmd %x\n", __FUNCTION__, t_slot, cmd);
370 /* To make sure the Controller Busy bit is 0 before we send out the
373 slot->ctrl->cmd_busy = 1;
374 shpc_writew(ctrl, CMD, temp_word);
377 * Wait for command completion.
379 retval = shpc_wait_cmd(slot->ctrl);
383 cmd_status = hpc_check_cmd_status(slot->ctrl);
385 err("%s: Failed to issued command 0x%x (error code = %d)\n",
386 __FUNCTION__, cmd, cmd_status);
390 mutex_unlock(&slot->ctrl->cmd_lock);
396 static int hpc_check_cmd_status(struct controller *ctrl)
403 if (!ctrl->hpc_ctlr_handle) {
404 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
408 cmd_status = shpc_readw(ctrl, CMD_STATUS) & 0x000F;
410 switch (cmd_status >> 1) {
415 retval = SWITCH_OPEN;
416 err("%s: Switch opened!\n", __FUNCTION__);
419 retval = INVALID_CMD;
420 err("%s: Invalid HPC command!\n", __FUNCTION__);
423 retval = INVALID_SPEED_MODE;
424 err("%s: Invalid bus speed/mode!\n", __FUNCTION__);
435 static int hpc_get_attention_status(struct slot *slot, u8 *status)
437 struct controller *ctrl = slot->ctrl;
443 if (!slot->ctrl->hpc_ctlr_handle) {
444 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
448 slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
449 state = (slot_reg & ATN_LED_STATE_MASK) >> ATN_LED_STATE_SHIFT;
452 case ATN_LED_STATE_ON:
453 *status = 1; /* On */
455 case ATN_LED_STATE_BLINK:
456 *status = 2; /* Blink */
458 case ATN_LED_STATE_OFF:
459 *status = 0; /* Off */
462 *status = 0xFF; /* Reserved */
470 static int hpc_get_power_status(struct slot * slot, u8 *status)
472 struct controller *ctrl = slot->ctrl;
478 if (!slot->ctrl->hpc_ctlr_handle) {
479 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
483 slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
484 state = (slot_reg & SLOT_STATE_MASK) >> SLOT_STATE_SHIFT;
487 case SLOT_STATE_PWRONLY:
488 *status = 2; /* Powered only */
490 case SLOT_STATE_ENABLED:
491 *status = 1; /* Enabled */
493 case SLOT_STATE_DISABLED:
494 *status = 0; /* Disabled */
497 *status = 0xFF; /* Reserved */
506 static int hpc_get_latch_status(struct slot *slot, u8 *status)
508 struct controller *ctrl = slot->ctrl;
513 if (!slot->ctrl->hpc_ctlr_handle) {
514 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
518 slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
519 *status = !!(slot_reg & MRL_SENSOR); /* 0 -> close; 1 -> open */
525 static int hpc_get_adapter_status(struct slot *slot, u8 *status)
527 struct controller *ctrl = slot->ctrl;
533 if (!slot->ctrl->hpc_ctlr_handle) {
534 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
538 slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
539 state = (slot_reg & PRSNT_MASK) >> PRSNT_SHIFT;
540 *status = (state != 0x3) ? 1 : 0;
546 static int hpc_get_prog_int(struct slot *slot, u8 *prog_int)
548 struct controller *ctrl = slot->ctrl;
552 if (!slot->ctrl->hpc_ctlr_handle) {
553 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
557 *prog_int = shpc_readb(ctrl, PROG_INTERFACE);
563 static int hpc_get_adapter_speed(struct slot *slot, enum pci_bus_speed *value)
566 struct controller *ctrl = slot->ctrl;
567 u32 slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
568 u8 m66_cap = !!(slot_reg & MHZ66_CAP);
573 if ((retval = hpc_get_prog_int(slot, &pi)))
578 pcix_cap = (slot_reg & PCIX_CAP_MASK_PI1) >> PCIX_CAP_SHIFT;
581 pcix_cap = (slot_reg & PCIX_CAP_MASK_PI2) >> PCIX_CAP_SHIFT;
587 dbg("%s: slot_reg = %x, pcix_cap = %x, m66_cap = %x\n",
588 __FUNCTION__, slot_reg, pcix_cap, m66_cap);
592 *value = m66_cap ? PCI_SPEED_66MHz : PCI_SPEED_33MHz;
595 *value = PCI_SPEED_66MHz_PCIX;
598 *value = PCI_SPEED_133MHz_PCIX;
601 *value = PCI_SPEED_133MHz_PCIX_266;
604 *value = PCI_SPEED_133MHz_PCIX_533;
608 *value = PCI_SPEED_UNKNOWN;
613 dbg("Adapter speed = %d\n", *value);
618 static int hpc_get_mode1_ECC_cap(struct slot *slot, u8 *mode)
620 struct controller *ctrl = slot->ctrl;
627 if (!slot->ctrl->hpc_ctlr_handle) {
628 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
632 pi = shpc_readb(ctrl, PROG_INTERFACE);
633 sec_bus_status = shpc_readw(ctrl, SEC_BUS_CONFIG);
636 *mode = (sec_bus_status & 0x0100) >> 8;
641 dbg("Mode 1 ECC cap = %d\n", *mode);
647 static int hpc_query_power_fault(struct slot * slot)
649 struct controller *ctrl = slot->ctrl;
654 if (!slot->ctrl->hpc_ctlr_handle) {
655 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
659 slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
662 /* Note: Logic 0 => fault */
663 return !(slot_reg & POWER_FAULT);
666 static int hpc_set_attention_status(struct slot *slot, u8 value)
668 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
672 if (!slot->ctrl->hpc_ctlr_handle) {
673 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
677 if (slot->hp_slot >= php_ctlr->num_slots) {
678 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
684 slot_cmd = 0x30; /* OFF */
687 slot_cmd = 0x10; /* ON */
690 slot_cmd = 0x20; /* BLINK */
696 shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
702 static void hpc_set_green_led_on(struct slot *slot)
704 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
707 if (!slot->ctrl->hpc_ctlr_handle) {
708 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
712 if (slot->hp_slot >= php_ctlr->num_slots) {
713 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
719 shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
724 static void hpc_set_green_led_off(struct slot *slot)
726 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
729 if (!slot->ctrl->hpc_ctlr_handle) {
730 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
734 if (slot->hp_slot >= php_ctlr->num_slots) {
735 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
741 shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
746 static void hpc_set_green_led_blink(struct slot *slot)
748 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
751 if (!slot->ctrl->hpc_ctlr_handle) {
752 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
756 if (slot->hp_slot >= php_ctlr->num_slots) {
757 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
763 shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
768 int shpc_get_ctlr_slot_config(struct controller *ctrl,
769 int *num_ctlr_slots, /* number of slots in this HPC */
770 int *first_device_num, /* PCI dev num of the first slot in this SHPC */
771 int *physical_slot_num, /* phy slot num of the first slot in this SHPC */
772 int *updown, /* physical_slot_num increament: 1 or -1 */
779 if (!ctrl->hpc_ctlr_handle) {
780 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
784 slot_config = shpc_readl(ctrl, SLOT_CONFIG);
785 *first_device_num = (slot_config & FIRST_DEV_NUM) >> 8;
786 *num_ctlr_slots = slot_config & SLOT_NUM;
787 *physical_slot_num = (slot_config & PSN) >> 16;
788 *updown = ((slot_config & UPDOWN) >> 29) ? 1 : -1;
790 dbg("%s: physical_slot_num = %x\n", __FUNCTION__, *physical_slot_num);
796 static void hpc_release_ctlr(struct controller *ctrl)
798 struct php_ctlr_state_s *php_ctlr = ctrl->hpc_ctlr_handle;
799 struct php_ctlr_state_s *p, *p_prev;
805 if (!ctrl->hpc_ctlr_handle) {
806 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
811 * Mask event interrupts and SERRs of all slots
813 for (i = 0; i < ctrl->num_slots; i++) {
814 slot_reg = shpc_readl(ctrl, SLOT_REG(i));
815 slot_reg |= (PRSNT_CHANGE_INTR_MASK | ISO_PFAULT_INTR_MASK |
816 BUTTON_PRESS_INTR_MASK | MRL_CHANGE_INTR_MASK |
817 CON_PFAULT_INTR_MASK | MRL_CHANGE_SERR_MASK |
818 CON_PFAULT_SERR_MASK);
819 slot_reg &= ~SLOT_REG_RSVDZ_MASK;
820 shpc_writel(ctrl, SLOT_REG(i), slot_reg);
825 if (shpchp_poll_mode) {
826 del_timer(&php_ctlr->int_poll_timer);
829 free_irq(php_ctlr->irq, ctrl);
831 pci_disable_msi(php_ctlr->pci_dev);
835 if (php_ctlr->pci_dev) {
836 iounmap(php_ctlr->creg);
837 release_mem_region(ctrl->mmio_base, ctrl->mmio_size);
838 php_ctlr->pci_dev = NULL;
841 spin_lock(&list_lock);
842 p = php_ctlr_list_head;
847 p_prev->pnext = p->pnext;
849 php_ctlr_list_head = p->pnext;
856 spin_unlock(&list_lock);
864 static int hpc_power_on_slot(struct slot * slot)
866 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
872 if (!slot->ctrl->hpc_ctlr_handle) {
873 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
877 if (slot->hp_slot >= php_ctlr->num_slots) {
878 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
883 retval = shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
886 err("%s: Write command failed!\n", __FUNCTION__);
895 static int hpc_slot_enable(struct slot * slot)
897 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
903 if (!slot->ctrl->hpc_ctlr_handle) {
904 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
908 if (slot->hp_slot >= php_ctlr->num_slots) {
909 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
912 /* 3A => Slot - Enable, Power Indicator - Blink, Attention Indicator - Off */
915 retval = shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
918 err("%s: Write command failed!\n", __FUNCTION__);
926 static int hpc_slot_disable(struct slot * slot)
928 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
934 if (!slot->ctrl->hpc_ctlr_handle) {
935 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
939 if (slot->hp_slot >= php_ctlr->num_slots) {
940 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
944 /* 1F => Slot - Disable, Power Indicator - Off, Attention Indicator - On */
947 retval = shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
950 err("%s: Write command failed!\n", __FUNCTION__);
958 static int hpc_set_bus_speed_mode(struct slot * slot, enum pci_bus_speed value)
961 struct controller *ctrl = slot->ctrl;
966 pi = shpc_readb(ctrl, PROG_INTERFACE);
967 if ((pi == 1) && (value > PCI_SPEED_133MHz_PCIX))
971 case PCI_SPEED_33MHz:
972 cmd = SETA_PCI_33MHZ;
974 case PCI_SPEED_66MHz:
975 cmd = SETA_PCI_66MHZ;
977 case PCI_SPEED_66MHz_PCIX:
978 cmd = SETA_PCIX_66MHZ;
980 case PCI_SPEED_100MHz_PCIX:
981 cmd = SETA_PCIX_100MHZ;
983 case PCI_SPEED_133MHz_PCIX:
984 cmd = SETA_PCIX_133MHZ;
986 case PCI_SPEED_66MHz_PCIX_ECC:
987 cmd = SETB_PCIX_66MHZ_EM;
989 case PCI_SPEED_100MHz_PCIX_ECC:
990 cmd = SETB_PCIX_100MHZ_EM;
992 case PCI_SPEED_133MHz_PCIX_ECC:
993 cmd = SETB_PCIX_133MHZ_EM;
995 case PCI_SPEED_66MHz_PCIX_266:
996 cmd = SETB_PCIX_66MHZ_266;
998 case PCI_SPEED_100MHz_PCIX_266:
999 cmd = SETB_PCIX_100MHZ_266;
1001 case PCI_SPEED_133MHz_PCIX_266:
1002 cmd = SETB_PCIX_133MHZ_266;
1004 case PCI_SPEED_66MHz_PCIX_533:
1005 cmd = SETB_PCIX_66MHZ_533;
1007 case PCI_SPEED_100MHz_PCIX_533:
1008 cmd = SETB_PCIX_100MHZ_533;
1010 case PCI_SPEED_133MHz_PCIX_533:
1011 cmd = SETB_PCIX_133MHZ_533;
1017 retval = shpc_write_cmd(slot, 0, cmd);
1019 err("%s: Write command failed!\n", __FUNCTION__);
1025 static irqreturn_t shpc_isr(int IRQ, void *dev_id, struct pt_regs *regs)
1027 struct controller *ctrl = NULL;
1028 struct php_ctlr_state_s *php_ctlr;
1029 u8 schedule_flag = 0;
1030 u32 temp_dword, intr_loc, intr_loc2;
1036 if (!shpchp_poll_mode) {
1037 ctrl = (struct controller *)dev_id;
1038 php_ctlr = ctrl->hpc_ctlr_handle;
1040 php_ctlr = (struct php_ctlr_state_s *) dev_id;
1041 ctrl = (struct controller *)php_ctlr->callback_instance_id;
1047 if (!php_ctlr || !php_ctlr->creg)
1050 /* Check to see if it was our interrupt */
1051 intr_loc = shpc_readl(ctrl, INTR_LOC);
1055 dbg("%s: intr_loc = %x\n",__FUNCTION__, intr_loc);
1057 if(!shpchp_poll_mode) {
1058 /* Mask Global Interrupt Mask - see implementation note on p. 139 */
1059 /* of SHPC spec rev 1.0*/
1060 temp_dword = shpc_readl(ctrl, SERR_INTR_ENABLE);
1061 temp_dword |= GLOBAL_INTR_MASK;
1062 temp_dword &= ~SERR_INTR_RSVDZ_MASK;
1063 shpc_writel(ctrl, SERR_INTR_ENABLE, temp_dword);
1065 intr_loc2 = shpc_readl(ctrl, INTR_LOC);
1066 dbg("%s: intr_loc2 = %x\n",__FUNCTION__, intr_loc2);
1069 if (intr_loc & 0x0001) {
1071 * Command Complete Interrupt Pending
1072 * RO only - clear by writing 1 to the Command Completion
1073 * Detect bit in Controller SERR-INT register
1075 temp_dword = shpc_readl(ctrl, SERR_INTR_ENABLE);
1076 temp_dword &= ~SERR_INTR_RSVDZ_MASK;
1077 shpc_writel(ctrl, SERR_INTR_ENABLE, temp_dword);
1079 wake_up_interruptible(&ctrl->queue);
1082 if ((intr_loc = (intr_loc >> 1)) == 0)
1085 for (hp_slot = 0; hp_slot < ctrl->num_slots; hp_slot++) {
1086 /* To find out which slot has interrupt pending */
1087 if ((intr_loc >> hp_slot) & 0x01) {
1088 temp_dword = shpc_readl(ctrl, SLOT_REG(hp_slot));
1089 dbg("%s: Slot %x with intr, slot register = %x\n",
1090 __FUNCTION__, hp_slot, temp_dword);
1091 if ((php_ctlr->switch_change_callback) &&
1092 (temp_dword & MRL_CHANGE_DETECTED))
1093 schedule_flag += php_ctlr->switch_change_callback(
1094 hp_slot, php_ctlr->callback_instance_id);
1095 if ((php_ctlr->attention_button_callback) &&
1096 (temp_dword & BUTTON_PRESS_DETECTED))
1097 schedule_flag += php_ctlr->attention_button_callback(
1098 hp_slot, php_ctlr->callback_instance_id);
1099 if ((php_ctlr->presence_change_callback) &&
1100 (temp_dword & PRSNT_CHANGE_DETECTED))
1101 schedule_flag += php_ctlr->presence_change_callback(
1102 hp_slot , php_ctlr->callback_instance_id);
1103 if ((php_ctlr->power_fault_callback) &&
1104 (temp_dword & (ISO_PFAULT_DETECTED | CON_PFAULT_DETECTED)))
1105 schedule_flag += php_ctlr->power_fault_callback(
1106 hp_slot, php_ctlr->callback_instance_id);
1108 /* Clear all slot events */
1109 temp_dword &= ~SLOT_REG_RSVDZ_MASK;
1110 shpc_writel(ctrl, SLOT_REG(hp_slot), temp_dword);
1112 intr_loc2 = shpc_readl(ctrl, INTR_LOC);
1113 dbg("%s: intr_loc2 = %x\n",__FUNCTION__, intr_loc2);
1117 if (!shpchp_poll_mode) {
1118 /* Unmask Global Interrupt Mask */
1119 temp_dword = shpc_readl(ctrl, SERR_INTR_ENABLE);
1120 temp_dword &= ~(GLOBAL_INTR_MASK | SERR_INTR_RSVDZ_MASK);
1121 shpc_writel(ctrl, SERR_INTR_ENABLE, temp_dword);
1127 static int hpc_get_max_bus_speed (struct slot *slot, enum pci_bus_speed *value)
1130 struct controller *ctrl = slot->ctrl;
1131 enum pci_bus_speed bus_speed = PCI_SPEED_UNKNOWN;
1132 u8 pi = shpc_readb(ctrl, PROG_INTERFACE);
1133 u32 slot_avail1 = shpc_readl(ctrl, SLOT_AVAIL1);
1134 u32 slot_avail2 = shpc_readl(ctrl, SLOT_AVAIL2);
1139 if (slot_avail2 & SLOT_133MHZ_PCIX_533)
1140 bus_speed = PCI_SPEED_133MHz_PCIX_533;
1141 else if (slot_avail2 & SLOT_100MHZ_PCIX_533)
1142 bus_speed = PCI_SPEED_100MHz_PCIX_533;
1143 else if (slot_avail2 & SLOT_66MHZ_PCIX_533)
1144 bus_speed = PCI_SPEED_66MHz_PCIX_533;
1145 else if (slot_avail2 & SLOT_133MHZ_PCIX_266)
1146 bus_speed = PCI_SPEED_133MHz_PCIX_266;
1147 else if (slot_avail2 & SLOT_100MHZ_PCIX_266)
1148 bus_speed = PCI_SPEED_100MHz_PCIX_266;
1149 else if (slot_avail2 & SLOT_66MHZ_PCIX_266)
1150 bus_speed = PCI_SPEED_66MHz_PCIX_266;
1153 if (bus_speed == PCI_SPEED_UNKNOWN) {
1154 if (slot_avail1 & SLOT_133MHZ_PCIX)
1155 bus_speed = PCI_SPEED_133MHz_PCIX;
1156 else if (slot_avail1 & SLOT_100MHZ_PCIX)
1157 bus_speed = PCI_SPEED_100MHz_PCIX;
1158 else if (slot_avail1 & SLOT_66MHZ_PCIX)
1159 bus_speed = PCI_SPEED_66MHz_PCIX;
1160 else if (slot_avail2 & SLOT_66MHZ)
1161 bus_speed = PCI_SPEED_66MHz;
1162 else if (slot_avail1 & SLOT_33MHZ)
1163 bus_speed = PCI_SPEED_33MHz;
1169 dbg("Max bus speed = %d\n", bus_speed);
1174 static int hpc_get_cur_bus_speed (struct slot *slot, enum pci_bus_speed *value)
1177 struct controller *ctrl = slot->ctrl;
1178 enum pci_bus_speed bus_speed = PCI_SPEED_UNKNOWN;
1179 u16 sec_bus_reg = shpc_readw(ctrl, SEC_BUS_CONFIG);
1180 u8 pi = shpc_readb(ctrl, PROG_INTERFACE);
1181 u8 speed_mode = (pi == 2) ? (sec_bus_reg & 0xF) : (sec_bus_reg & 0x7);
1185 if ((pi == 1) && (speed_mode > 4)) {
1186 *value = PCI_SPEED_UNKNOWN;
1190 switch (speed_mode) {
1192 *value = PCI_SPEED_33MHz;
1195 *value = PCI_SPEED_66MHz;
1198 *value = PCI_SPEED_66MHz_PCIX;
1201 *value = PCI_SPEED_100MHz_PCIX;
1204 *value = PCI_SPEED_133MHz_PCIX;
1207 *value = PCI_SPEED_66MHz_PCIX_ECC;
1210 *value = PCI_SPEED_100MHz_PCIX_ECC;
1213 *value = PCI_SPEED_133MHz_PCIX_ECC;
1216 *value = PCI_SPEED_66MHz_PCIX_266;
1219 *value = PCI_SPEED_100MHz_PCIX_266;
1222 *value = PCI_SPEED_133MHz_PCIX_266;
1225 *value = PCI_SPEED_66MHz_PCIX_533;
1228 *value = PCI_SPEED_100MHz_PCIX_533;
1231 *value = PCI_SPEED_133MHz_PCIX_533;
1234 *value = PCI_SPEED_UNKNOWN;
1239 dbg("Current bus speed = %d\n", bus_speed);
1244 static struct hpc_ops shpchp_hpc_ops = {
1245 .power_on_slot = hpc_power_on_slot,
1246 .slot_enable = hpc_slot_enable,
1247 .slot_disable = hpc_slot_disable,
1248 .set_bus_speed_mode = hpc_set_bus_speed_mode,
1249 .set_attention_status = hpc_set_attention_status,
1250 .get_power_status = hpc_get_power_status,
1251 .get_attention_status = hpc_get_attention_status,
1252 .get_latch_status = hpc_get_latch_status,
1253 .get_adapter_status = hpc_get_adapter_status,
1255 .get_max_bus_speed = hpc_get_max_bus_speed,
1256 .get_cur_bus_speed = hpc_get_cur_bus_speed,
1257 .get_adapter_speed = hpc_get_adapter_speed,
1258 .get_mode1_ECC_cap = hpc_get_mode1_ECC_cap,
1259 .get_prog_int = hpc_get_prog_int,
1261 .query_power_fault = hpc_query_power_fault,
1262 .green_led_on = hpc_set_green_led_on,
1263 .green_led_off = hpc_set_green_led_off,
1264 .green_led_blink = hpc_set_green_led_blink,
1266 .release_ctlr = hpc_release_ctlr,
1269 int shpc_init(struct controller * ctrl, struct pci_dev * pdev)
1271 struct php_ctlr_state_s *php_ctlr, *p;
1272 void *instance_id = ctrl;
1273 int rc, num_slots = 0;
1275 static int first = 1;
1276 u32 shpc_base_offset;
1277 u32 tempdword, slot_reg, slot_config;
1282 ctrl->pci_dev = pdev; /* pci_dev of the P2P bridge */
1284 spin_lock_init(&list_lock);
1285 php_ctlr = kzalloc(sizeof(*php_ctlr), GFP_KERNEL);
1287 if (!php_ctlr) { /* allocate controller state data */
1288 err("%s: HPC controller memory allocation error!\n", __FUNCTION__);
1292 php_ctlr->pci_dev = pdev; /* save pci_dev in context */
1294 if ((pdev->vendor == PCI_VENDOR_ID_AMD) || (pdev->device ==
1295 PCI_DEVICE_ID_AMD_GOLAM_7450)) {
1296 /* amd shpc driver doesn't use Base Offset; assume 0 */
1297 ctrl->mmio_base = pci_resource_start(pdev, 0);
1298 ctrl->mmio_size = pci_resource_len(pdev, 0);
1300 ctrl->cap_offset = pci_find_capability(pdev, PCI_CAP_ID_SHPC);
1301 if (!ctrl->cap_offset) {
1302 err("%s : cap_offset == 0\n", __FUNCTION__);
1303 goto abort_free_ctlr;
1305 dbg("%s: cap_offset = %x\n", __FUNCTION__, ctrl->cap_offset);
1307 rc = shpc_indirect_read(ctrl, 0, &shpc_base_offset);
1309 err("%s: cannot read base_offset\n", __FUNCTION__);
1310 goto abort_free_ctlr;
1313 rc = shpc_indirect_read(ctrl, 3, &tempdword);
1315 err("%s: cannot read slot config\n", __FUNCTION__);
1316 goto abort_free_ctlr;
1318 num_slots = tempdword & SLOT_NUM;
1319 dbg("%s: num_slots (indirect) %x\n", __FUNCTION__, num_slots);
1321 for (i = 0; i < 9 + num_slots; i++) {
1322 rc = shpc_indirect_read(ctrl, i, &tempdword);
1324 err("%s: cannot read creg (index = %d)\n",
1326 goto abort_free_ctlr;
1328 dbg("%s: offset %d: value %x\n", __FUNCTION__,i,
1333 pci_resource_start(pdev, 0) + shpc_base_offset;
1334 ctrl->mmio_size = 0x24 + 0x4 * num_slots;
1338 spin_lock_init(&hpc_event_lock);
1342 info("HPC vendor_id %x device_id %x ss_vid %x ss_did %x\n", pdev->vendor, pdev->device, pdev->subsystem_vendor,
1343 pdev->subsystem_device);
1345 if (pci_enable_device(pdev))
1346 goto abort_free_ctlr;
1348 if (!request_mem_region(ctrl->mmio_base, ctrl->mmio_size, MY_NAME)) {
1349 err("%s: cannot reserve MMIO region\n", __FUNCTION__);
1350 goto abort_free_ctlr;
1353 php_ctlr->creg = ioremap(ctrl->mmio_base, ctrl->mmio_size);
1354 if (!php_ctlr->creg) {
1355 err("%s: cannot remap MMIO region %lx @ %lx\n", __FUNCTION__,
1356 ctrl->mmio_size, ctrl->mmio_base);
1357 release_mem_region(ctrl->mmio_base, ctrl->mmio_size);
1358 goto abort_free_ctlr;
1360 dbg("%s: php_ctlr->creg %p\n", __FUNCTION__, php_ctlr->creg);
1362 mutex_init(&ctrl->crit_sect);
1363 mutex_init(&ctrl->cmd_lock);
1365 /* Setup wait queue */
1366 init_waitqueue_head(&ctrl->queue);
1369 php_ctlr->irq = pdev->irq;
1370 php_ctlr->attention_button_callback = shpchp_handle_attention_button,
1371 php_ctlr->switch_change_callback = shpchp_handle_switch_change;
1372 php_ctlr->presence_change_callback = shpchp_handle_presence_change;
1373 php_ctlr->power_fault_callback = shpchp_handle_power_fault;
1374 php_ctlr->callback_instance_id = instance_id;
1376 ctrl->hpc_ctlr_handle = php_ctlr;
1377 ctrl->hpc_ops = &shpchp_hpc_ops;
1379 /* Return PCI Controller Info */
1380 slot_config = shpc_readl(ctrl, SLOT_CONFIG);
1381 php_ctlr->slot_device_offset = (slot_config & FIRST_DEV_NUM) >> 8;
1382 php_ctlr->num_slots = slot_config & SLOT_NUM;
1383 dbg("%s: slot_device_offset %x\n", __FUNCTION__, php_ctlr->slot_device_offset);
1384 dbg("%s: num_slots %x\n", __FUNCTION__, php_ctlr->num_slots);
1386 /* Mask Global Interrupt Mask & Command Complete Interrupt Mask */
1387 tempdword = shpc_readl(ctrl, SERR_INTR_ENABLE);
1388 dbg("%s: SERR_INTR_ENABLE = %x\n", __FUNCTION__, tempdword);
1389 tempdword |= (GLOBAL_INTR_MASK | GLOBAL_SERR_MASK |
1390 COMMAND_INTR_MASK | ARBITER_SERR_MASK);
1391 tempdword &= ~SERR_INTR_RSVDZ_MASK;
1392 shpc_writel(ctrl, SERR_INTR_ENABLE, tempdword);
1393 tempdword = shpc_readl(ctrl, SERR_INTR_ENABLE);
1394 dbg("%s: SERR_INTR_ENABLE = %x\n", __FUNCTION__, tempdword);
1396 /* Mask the MRL sensor SERR Mask of individual slot in
1397 * Slot SERR-INT Mask & clear all the existing event if any
1399 for (hp_slot = 0; hp_slot < php_ctlr->num_slots; hp_slot++) {
1400 slot_reg = shpc_readl(ctrl, SLOT_REG(hp_slot));
1401 dbg("%s: Default Logical Slot Register %d value %x\n", __FUNCTION__,
1403 slot_reg |= (PRSNT_CHANGE_INTR_MASK | ISO_PFAULT_INTR_MASK |
1404 BUTTON_PRESS_INTR_MASK | MRL_CHANGE_INTR_MASK |
1405 CON_PFAULT_INTR_MASK | MRL_CHANGE_SERR_MASK |
1406 CON_PFAULT_SERR_MASK);
1407 slot_reg &= ~SLOT_REG_RSVDZ_MASK;
1408 shpc_writel(ctrl, SLOT_REG(hp_slot), slot_reg);
1411 if (shpchp_poll_mode) {/* Install interrupt polling code */
1412 /* Install and start the interrupt polling timer */
1413 init_timer(&php_ctlr->int_poll_timer);
1414 start_int_poll_timer( php_ctlr, 10 ); /* start with 10 second delay */
1416 /* Installs the interrupt handler */
1417 rc = pci_enable_msi(pdev);
1419 info("Can't get msi for the hotplug controller\n");
1420 info("Use INTx for the hotplug controller\n");
1422 php_ctlr->irq = pdev->irq;
1424 rc = request_irq(php_ctlr->irq, shpc_isr, SA_SHIRQ, MY_NAME, (void *) ctrl);
1425 dbg("%s: request_irq %d for hpc%d (returns %d)\n", __FUNCTION__, php_ctlr->irq, ctlr_seq_num, rc);
1427 err("Can't get irq %d for the hotplug controller\n", php_ctlr->irq);
1428 goto abort_free_ctlr;
1431 dbg("%s: HPC at b:d:f:irq=0x%x:%x:%x:%x\n", __FUNCTION__,
1432 pdev->bus->number, PCI_SLOT(pdev->devfn),
1433 PCI_FUNC(pdev->devfn), pdev->irq);
1434 get_hp_hw_control_from_firmware(pdev);
1436 /* Add this HPC instance into the HPC list */
1437 spin_lock(&list_lock);
1438 if (php_ctlr_list_head == 0) {
1439 php_ctlr_list_head = php_ctlr;
1440 p = php_ctlr_list_head;
1443 p = php_ctlr_list_head;
1448 p->pnext = php_ctlr;
1450 spin_unlock(&list_lock);
1455 * Unmask all event interrupts of all slots
1457 for (hp_slot = 0; hp_slot < php_ctlr->num_slots; hp_slot++) {
1458 slot_reg = shpc_readl(ctrl, SLOT_REG(hp_slot));
1459 dbg("%s: Default Logical Slot Register %d value %x\n", __FUNCTION__,
1461 slot_reg &= ~(PRSNT_CHANGE_INTR_MASK | ISO_PFAULT_INTR_MASK |
1462 BUTTON_PRESS_INTR_MASK | MRL_CHANGE_INTR_MASK |
1463 CON_PFAULT_INTR_MASK | SLOT_REG_RSVDZ_MASK);
1464 shpc_writel(ctrl, SLOT_REG(hp_slot), slot_reg);
1466 if (!shpchp_poll_mode) {
1467 /* Unmask all general input interrupts and SERR */
1468 tempdword = shpc_readl(ctrl, SERR_INTR_ENABLE);
1469 tempdword &= ~(GLOBAL_INTR_MASK | COMMAND_INTR_MASK |
1470 SERR_INTR_RSVDZ_MASK);
1471 shpc_writel(ctrl, SERR_INTR_ENABLE, tempdword);
1472 tempdword = shpc_readl(ctrl, SERR_INTR_ENABLE);
1473 dbg("%s: SERR_INTR_ENABLE = %x\n", __FUNCTION__, tempdword);
1479 /* We end up here for the many possible ways to fail this API. */