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 * Logical Slot Register definitions
96 #define SLOT_REG(i) (SLOT1 + (4 * i))
98 #define SLOT_STATE_SHIFT (0)
99 #define SLOT_STATE_MASK (3 << 0)
100 #define SLOT_STATE_PWRONLY (1)
101 #define SLOT_STATE_ENABLED (2)
102 #define SLOT_STATE_DISABLED (3)
103 #define PWR_LED_STATE_SHIFT (2)
104 #define PWR_LED_STATE_MASK (3 << 2)
105 #define ATN_LED_STATE_SHIFT (4)
106 #define ATN_LED_STATE_MASK (3 << 4)
107 #define ATN_LED_STATE_ON (1)
108 #define ATN_LED_STATE_BLINK (2)
109 #define ATN_LED_STATE_OFF (3)
110 #define POWER_FAULT (1 << 6)
111 #define ATN_BUTTON (1 << 7)
112 #define MRL_SENSOR (1 << 8)
113 #define MHZ66_CAP (1 << 9)
114 #define PRSNT_SHIFT (10)
115 #define PRSNT_MASK (3 << 10)
116 #define PCIX_CAP_SHIFT (12)
117 #define PCIX_CAP_MASK_PI1 (3 << 12)
118 #define PCIX_CAP_MASK_PI2 (7 << 12)
119 #define PRSNT_CHANGE_DETECTED (1 << 16)
120 #define ISO_PFAULT_DETECTED (1 << 17)
121 #define BUTTON_PRESS_DETECTED (1 << 18)
122 #define MRL_CHANGE_DETECTED (1 << 19)
123 #define CON_PFAULT_DETECTED (1 << 20)
124 #define PRSNT_CHANGE_INTR_MASK (1 << 24)
125 #define ISO_PFAULT_INTR_MASK (1 << 25)
126 #define BUTTON_PRESS_INTR_MASK (1 << 26)
127 #define MRL_CHANGE_INTR_MASK (1 << 27)
128 #define CON_PFAULT_INTR_MASK (1 << 28)
129 #define MRL_CHANGE_SERR_MASK (1 << 29)
130 #define CON_PFAULT_SERR_MASK (1 << 30)
131 #define SLOT_REG_RSVDZ_MASK (1 << 15) | (7 << 21)
133 /* SHPC 'write' operations/commands */
135 /* Slot operation - 0x00h to 0x3Fh */
137 #define NO_CHANGE 0x00
139 /* Slot state - Bits 0 & 1 of controller command register */
140 #define SET_SLOT_PWR 0x01
141 #define SET_SLOT_ENABLE 0x02
142 #define SET_SLOT_DISABLE 0x03
144 /* Power indicator state - Bits 2 & 3 of controller command register*/
145 #define SET_PWR_ON 0x04
146 #define SET_PWR_BLINK 0x08
147 #define SET_PWR_OFF 0x0C
149 /* Attention indicator state - Bits 4 & 5 of controller command register*/
150 #define SET_ATTN_ON 0x010
151 #define SET_ATTN_BLINK 0x020
152 #define SET_ATTN_OFF 0x030
154 /* Set bus speed/mode A - 0x40h to 0x47h */
155 #define SETA_PCI_33MHZ 0x40
156 #define SETA_PCI_66MHZ 0x41
157 #define SETA_PCIX_66MHZ 0x42
158 #define SETA_PCIX_100MHZ 0x43
159 #define SETA_PCIX_133MHZ 0x44
160 #define RESERV_1 0x45
161 #define RESERV_2 0x46
162 #define RESERV_3 0x47
164 /* Set bus speed/mode B - 0x50h to 0x5fh */
165 #define SETB_PCI_33MHZ 0x50
166 #define SETB_PCI_66MHZ 0x51
167 #define SETB_PCIX_66MHZ_PM 0x52
168 #define SETB_PCIX_100MHZ_PM 0x53
169 #define SETB_PCIX_133MHZ_PM 0x54
170 #define SETB_PCIX_66MHZ_EM 0x55
171 #define SETB_PCIX_100MHZ_EM 0x56
172 #define SETB_PCIX_133MHZ_EM 0x57
173 #define SETB_PCIX_66MHZ_266 0x58
174 #define SETB_PCIX_100MHZ_266 0x59
175 #define SETB_PCIX_133MHZ_266 0x5a
176 #define SETB_PCIX_66MHZ_533 0x5b
177 #define SETB_PCIX_100MHZ_533 0x5c
178 #define SETB_PCIX_133MHZ_533 0x5d
181 /* Power-on all slots - 0x48h */
182 #define SET_PWR_ON_ALL 0x48
184 /* Enable all slots - 0x49h */
185 #define SET_ENABLE_ALL 0x49
187 /* SHPC controller command error code */
188 #define SWITCH_OPEN 0x1
189 #define INVALID_CMD 0x2
190 #define INVALID_SPEED_MODE 0x4
192 /* For accessing SHPC Working Register Set */
193 #define DWORD_SELECT 0x2
194 #define DWORD_DATA 0x4
195 #define BASE_OFFSET 0x0
197 /* Field Offset in Logical Slot Register - byte boundary */
198 #define SLOT_EVENT_LATCH 0x2
199 #define SLOT_SERR_INT_MASK 0x3
201 static spinlock_t hpc_event_lock;
203 DEFINE_DBG_BUFFER /* Debug string buffer for entire HPC defined here */
204 static struct php_ctlr_state_s *php_ctlr_list_head; /* HPC state linked list */
205 static int ctlr_seq_num = 0; /* Controller sequenc # */
206 static spinlock_t list_lock;
208 static irqreturn_t shpc_isr(int IRQ, void *dev_id, struct pt_regs *regs);
210 static void start_int_poll_timer(struct php_ctlr_state_s *php_ctlr, int seconds);
211 static int hpc_check_cmd_status(struct controller *ctrl);
213 static inline u8 shpc_readb(struct controller *ctrl, int reg)
215 return readb(ctrl->hpc_ctlr_handle->creg + reg);
218 static inline void shpc_writeb(struct controller *ctrl, int reg, u8 val)
220 writeb(val, ctrl->hpc_ctlr_handle->creg + reg);
223 static inline u16 shpc_readw(struct controller *ctrl, int reg)
225 return readw(ctrl->hpc_ctlr_handle->creg + reg);
228 static inline void shpc_writew(struct controller *ctrl, int reg, u16 val)
230 writew(val, ctrl->hpc_ctlr_handle->creg + reg);
233 static inline u32 shpc_readl(struct controller *ctrl, int reg)
235 return readl(ctrl->hpc_ctlr_handle->creg + reg);
238 static inline void shpc_writel(struct controller *ctrl, int reg, u32 val)
240 writel(val, ctrl->hpc_ctlr_handle->creg + reg);
243 static inline int shpc_indirect_read(struct controller *ctrl, int index,
247 u32 cap_offset = ctrl->cap_offset;
248 struct pci_dev *pdev = ctrl->pci_dev;
250 rc = pci_write_config_byte(pdev, cap_offset + DWORD_SELECT, index);
253 return pci_read_config_dword(pdev, cap_offset + DWORD_DATA, value);
256 /* This is the interrupt polling timeout function. */
257 static void int_poll_timeout(unsigned long lphp_ctlr)
259 struct php_ctlr_state_s *php_ctlr = (struct php_ctlr_state_s *)lphp_ctlr;
264 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
268 /* Poll for interrupt events. regs == NULL => polling */
269 shpc_isr( 0, (void *)php_ctlr, NULL );
271 init_timer(&php_ctlr->int_poll_timer);
272 if (!shpchp_poll_time)
273 shpchp_poll_time = 2; /* reset timer to poll in 2 secs if user doesn't specify at module installation*/
275 start_int_poll_timer(php_ctlr, shpchp_poll_time);
280 /* This function starts the interrupt polling timer. */
281 static void start_int_poll_timer(struct php_ctlr_state_s *php_ctlr, int seconds)
284 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
288 if ( ( seconds <= 0 ) || ( seconds > 60 ) )
289 seconds = 2; /* Clamp to sane value */
291 php_ctlr->int_poll_timer.function = &int_poll_timeout;
292 php_ctlr->int_poll_timer.data = (unsigned long)php_ctlr; /* Instance data */
293 php_ctlr->int_poll_timer.expires = jiffies + seconds * HZ;
294 add_timer(&php_ctlr->int_poll_timer);
299 static inline int shpc_wait_cmd(struct controller *ctrl)
302 unsigned int timeout_msec = shpchp_poll_mode ? 2000 : 1000;
303 unsigned long timeout = msecs_to_jiffies(timeout_msec);
304 int rc = wait_event_interruptible_timeout(ctrl->queue,
305 !ctrl->cmd_busy, timeout);
308 err("Command not completed in %d msec\n", timeout_msec);
311 info("Command was interrupted by a signal\n");
318 static int shpc_write_cmd(struct slot *slot, u8 t_slot, u8 cmd)
320 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
321 struct controller *ctrl = slot->ctrl;
329 mutex_lock(&slot->ctrl->cmd_lock);
332 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
337 for (i = 0; i < 10; i++) {
338 cmd_status = shpc_readw(ctrl, CMD_STATUS);
340 if (!(cmd_status & 0x1))
342 /* Check every 0.1 sec for a total of 1 sec*/
346 cmd_status = shpc_readw(ctrl, CMD_STATUS);
348 if (cmd_status & 0x1) {
349 /* After 1 sec and and the controller is still busy */
350 err("%s : Controller is still busy after 1 sec.\n", __FUNCTION__);
356 temp_word = (t_slot << 8) | (cmd & 0xFF);
357 dbg("%s: t_slot %x cmd %x\n", __FUNCTION__, t_slot, cmd);
359 /* To make sure the Controller Busy bit is 0 before we send out the
362 slot->ctrl->cmd_busy = 1;
363 shpc_writew(ctrl, CMD, temp_word);
366 * Wait for command completion.
368 retval = shpc_wait_cmd(slot->ctrl);
372 cmd_status = hpc_check_cmd_status(slot->ctrl);
374 err("%s: Failed to issued command 0x%x (error code = %d)\n",
375 __FUNCTION__, cmd, cmd_status);
379 mutex_unlock(&slot->ctrl->cmd_lock);
385 static int hpc_check_cmd_status(struct controller *ctrl)
392 if (!ctrl->hpc_ctlr_handle) {
393 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
397 cmd_status = shpc_readw(ctrl, CMD_STATUS) & 0x000F;
399 switch (cmd_status >> 1) {
404 retval = SWITCH_OPEN;
405 err("%s: Switch opened!\n", __FUNCTION__);
408 retval = INVALID_CMD;
409 err("%s: Invalid HPC command!\n", __FUNCTION__);
412 retval = INVALID_SPEED_MODE;
413 err("%s: Invalid bus speed/mode!\n", __FUNCTION__);
424 static int hpc_get_attention_status(struct slot *slot, u8 *status)
426 struct controller *ctrl = slot->ctrl;
432 if (!slot->ctrl->hpc_ctlr_handle) {
433 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
437 slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
438 state = (slot_reg & ATN_LED_STATE_MASK) >> ATN_LED_STATE_SHIFT;
441 case ATN_LED_STATE_ON:
442 *status = 1; /* On */
444 case ATN_LED_STATE_BLINK:
445 *status = 2; /* Blink */
447 case ATN_LED_STATE_OFF:
448 *status = 0; /* Off */
451 *status = 0xFF; /* Reserved */
459 static int hpc_get_power_status(struct slot * slot, u8 *status)
461 struct controller *ctrl = slot->ctrl;
467 if (!slot->ctrl->hpc_ctlr_handle) {
468 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
472 slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
473 state = (slot_reg & SLOT_STATE_MASK) >> SLOT_STATE_SHIFT;
476 case SLOT_STATE_PWRONLY:
477 *status = 2; /* Powered only */
479 case SLOT_STATE_ENABLED:
480 *status = 1; /* Enabled */
482 case SLOT_STATE_DISABLED:
483 *status = 0; /* Disabled */
486 *status = 0xFF; /* Reserved */
495 static int hpc_get_latch_status(struct slot *slot, u8 *status)
497 struct controller *ctrl = slot->ctrl;
502 if (!slot->ctrl->hpc_ctlr_handle) {
503 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
507 slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
508 *status = !!(slot_reg & MRL_SENSOR); /* 0 -> close; 1 -> open */
514 static int hpc_get_adapter_status(struct slot *slot, u8 *status)
516 struct controller *ctrl = slot->ctrl;
522 if (!slot->ctrl->hpc_ctlr_handle) {
523 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
527 slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
528 state = (slot_reg & PRSNT_MASK) >> PRSNT_SHIFT;
529 *status = (state != 0x3) ? 1 : 0;
535 static int hpc_get_prog_int(struct slot *slot, u8 *prog_int)
537 struct controller *ctrl = slot->ctrl;
541 if (!slot->ctrl->hpc_ctlr_handle) {
542 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
546 *prog_int = shpc_readb(ctrl, PROG_INTERFACE);
552 static int hpc_get_adapter_speed(struct slot *slot, enum pci_bus_speed *value)
555 struct controller *ctrl = slot->ctrl;
556 u32 slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
557 u8 m66_cap = !!(slot_reg & MHZ66_CAP);
562 if ((retval = hpc_get_prog_int(slot, &pi)))
567 pcix_cap = (slot_reg & PCIX_CAP_MASK_PI1) >> PCIX_CAP_SHIFT;
570 pcix_cap = (slot_reg & PCIX_CAP_MASK_PI2) >> PCIX_CAP_SHIFT;
576 dbg("%s: slot_reg = %x, pcix_cap = %x, m66_cap = %x\n",
577 __FUNCTION__, slot_reg, pcix_cap, m66_cap);
581 *value = m66_cap ? PCI_SPEED_66MHz : PCI_SPEED_33MHz;
584 *value = PCI_SPEED_66MHz_PCIX;
587 *value = PCI_SPEED_133MHz_PCIX;
590 *value = PCI_SPEED_133MHz_PCIX_266;
593 *value = PCI_SPEED_133MHz_PCIX_533;
597 *value = PCI_SPEED_UNKNOWN;
602 dbg("Adapter speed = %d\n", *value);
607 static int hpc_get_mode1_ECC_cap(struct slot *slot, u8 *mode)
609 struct controller *ctrl = slot->ctrl;
616 if (!slot->ctrl->hpc_ctlr_handle) {
617 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
621 pi = shpc_readb(ctrl, PROG_INTERFACE);
622 sec_bus_status = shpc_readw(ctrl, SEC_BUS_CONFIG);
625 *mode = (sec_bus_status & 0x0100) >> 8;
630 dbg("Mode 1 ECC cap = %d\n", *mode);
636 static int hpc_query_power_fault(struct slot * slot)
638 struct controller *ctrl = slot->ctrl;
643 if (!slot->ctrl->hpc_ctlr_handle) {
644 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
648 slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
651 /* Note: Logic 0 => fault */
652 return !(slot_reg & POWER_FAULT);
655 static int hpc_set_attention_status(struct slot *slot, u8 value)
657 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
661 if (!slot->ctrl->hpc_ctlr_handle) {
662 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
666 if (slot->hp_slot >= php_ctlr->num_slots) {
667 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
673 slot_cmd = 0x30; /* OFF */
676 slot_cmd = 0x10; /* ON */
679 slot_cmd = 0x20; /* BLINK */
685 shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
691 static void hpc_set_green_led_on(struct slot *slot)
693 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
696 if (!slot->ctrl->hpc_ctlr_handle) {
697 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
701 if (slot->hp_slot >= php_ctlr->num_slots) {
702 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
708 shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
713 static void hpc_set_green_led_off(struct slot *slot)
715 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
718 if (!slot->ctrl->hpc_ctlr_handle) {
719 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
723 if (slot->hp_slot >= php_ctlr->num_slots) {
724 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
730 shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
735 static void hpc_set_green_led_blink(struct slot *slot)
737 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
740 if (!slot->ctrl->hpc_ctlr_handle) {
741 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
745 if (slot->hp_slot >= php_ctlr->num_slots) {
746 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
752 shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
757 int shpc_get_ctlr_slot_config(struct controller *ctrl,
758 int *num_ctlr_slots, /* number of slots in this HPC */
759 int *first_device_num, /* PCI dev num of the first slot in this SHPC */
760 int *physical_slot_num, /* phy slot num of the first slot in this SHPC */
761 int *updown, /* physical_slot_num increament: 1 or -1 */
768 if (!ctrl->hpc_ctlr_handle) {
769 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
773 slot_config = shpc_readl(ctrl, SLOT_CONFIG);
774 *first_device_num = (slot_config & FIRST_DEV_NUM) >> 8;
775 *num_ctlr_slots = slot_config & SLOT_NUM;
776 *physical_slot_num = (slot_config & PSN) >> 16;
777 *updown = ((slot_config & UPDOWN) >> 29) ? 1 : -1;
779 dbg("%s: physical_slot_num = %x\n", __FUNCTION__, *physical_slot_num);
785 static void hpc_release_ctlr(struct controller *ctrl)
787 struct php_ctlr_state_s *php_ctlr = ctrl->hpc_ctlr_handle;
788 struct php_ctlr_state_s *p, *p_prev;
794 if (!ctrl->hpc_ctlr_handle) {
795 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
800 * Mask event interrupts and SERRs of all slots
802 for (i = 0; i < ctrl->num_slots; i++) {
803 slot_reg = shpc_readl(ctrl, SLOT_REG(i));
804 slot_reg |= (PRSNT_CHANGE_INTR_MASK | ISO_PFAULT_INTR_MASK |
805 BUTTON_PRESS_INTR_MASK | MRL_CHANGE_INTR_MASK |
806 CON_PFAULT_INTR_MASK | MRL_CHANGE_SERR_MASK |
807 CON_PFAULT_SERR_MASK);
808 slot_reg &= ~SLOT_REG_RSVDZ_MASK;
809 shpc_writel(ctrl, SLOT_REG(i), slot_reg);
814 if (shpchp_poll_mode) {
815 del_timer(&php_ctlr->int_poll_timer);
818 free_irq(php_ctlr->irq, ctrl);
820 pci_disable_msi(php_ctlr->pci_dev);
824 if (php_ctlr->pci_dev) {
825 iounmap(php_ctlr->creg);
826 release_mem_region(ctrl->mmio_base, ctrl->mmio_size);
827 php_ctlr->pci_dev = NULL;
830 spin_lock(&list_lock);
831 p = php_ctlr_list_head;
836 p_prev->pnext = p->pnext;
838 php_ctlr_list_head = p->pnext;
845 spin_unlock(&list_lock);
853 static int hpc_power_on_slot(struct slot * slot)
855 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
861 if (!slot->ctrl->hpc_ctlr_handle) {
862 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
866 if (slot->hp_slot >= php_ctlr->num_slots) {
867 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
872 retval = shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
875 err("%s: Write command failed!\n", __FUNCTION__);
884 static int hpc_slot_enable(struct slot * slot)
886 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
892 if (!slot->ctrl->hpc_ctlr_handle) {
893 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
897 if (slot->hp_slot >= php_ctlr->num_slots) {
898 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
901 /* 3A => Slot - Enable, Power Indicator - Blink, Attention Indicator - Off */
904 retval = shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
907 err("%s: Write command failed!\n", __FUNCTION__);
915 static int hpc_slot_disable(struct slot * slot)
917 struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
923 if (!slot->ctrl->hpc_ctlr_handle) {
924 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
928 if (slot->hp_slot >= php_ctlr->num_slots) {
929 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
933 /* 1F => Slot - Disable, Power Indicator - Off, Attention Indicator - On */
936 retval = shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
939 err("%s: Write command failed!\n", __FUNCTION__);
947 static int hpc_set_bus_speed_mode(struct slot * slot, enum pci_bus_speed value)
950 struct controller *ctrl = slot->ctrl;
955 pi = shpc_readb(ctrl, PROG_INTERFACE);
956 if ((pi == 1) && (value > PCI_SPEED_133MHz_PCIX))
960 case PCI_SPEED_33MHz:
961 cmd = SETA_PCI_33MHZ;
963 case PCI_SPEED_66MHz:
964 cmd = SETA_PCI_66MHZ;
966 case PCI_SPEED_66MHz_PCIX:
967 cmd = SETA_PCIX_66MHZ;
969 case PCI_SPEED_100MHz_PCIX:
970 cmd = SETA_PCIX_100MHZ;
972 case PCI_SPEED_133MHz_PCIX:
973 cmd = SETA_PCIX_133MHZ;
975 case PCI_SPEED_66MHz_PCIX_ECC:
976 cmd = SETB_PCIX_66MHZ_EM;
978 case PCI_SPEED_100MHz_PCIX_ECC:
979 cmd = SETB_PCIX_100MHZ_EM;
981 case PCI_SPEED_133MHz_PCIX_ECC:
982 cmd = SETB_PCIX_133MHZ_EM;
984 case PCI_SPEED_66MHz_PCIX_266:
985 cmd = SETB_PCIX_66MHZ_266;
987 case PCI_SPEED_100MHz_PCIX_266:
988 cmd = SETB_PCIX_100MHZ_266;
990 case PCI_SPEED_133MHz_PCIX_266:
991 cmd = SETB_PCIX_133MHZ_266;
993 case PCI_SPEED_66MHz_PCIX_533:
994 cmd = SETB_PCIX_66MHZ_533;
996 case PCI_SPEED_100MHz_PCIX_533:
997 cmd = SETB_PCIX_100MHZ_533;
999 case PCI_SPEED_133MHz_PCIX_533:
1000 cmd = SETB_PCIX_133MHZ_533;
1006 retval = shpc_write_cmd(slot, 0, cmd);
1008 err("%s: Write command failed!\n", __FUNCTION__);
1014 static irqreturn_t shpc_isr(int IRQ, void *dev_id, struct pt_regs *regs)
1016 struct controller *ctrl = NULL;
1017 struct php_ctlr_state_s *php_ctlr;
1018 u8 schedule_flag = 0;
1019 u32 temp_dword, intr_loc, intr_loc2;
1025 if (!shpchp_poll_mode) {
1026 ctrl = (struct controller *)dev_id;
1027 php_ctlr = ctrl->hpc_ctlr_handle;
1029 php_ctlr = (struct php_ctlr_state_s *) dev_id;
1030 ctrl = (struct controller *)php_ctlr->callback_instance_id;
1036 if (!php_ctlr || !php_ctlr->creg)
1039 /* Check to see if it was our interrupt */
1040 intr_loc = shpc_readl(ctrl, INTR_LOC);
1044 dbg("%s: intr_loc = %x\n",__FUNCTION__, intr_loc);
1046 if(!shpchp_poll_mode) {
1047 /* Mask Global Interrupt Mask - see implementation note on p. 139 */
1048 /* of SHPC spec rev 1.0*/
1049 temp_dword = shpc_readl(ctrl, SERR_INTR_ENABLE);
1050 temp_dword |= 0x00000001;
1051 shpc_writel(ctrl, SERR_INTR_ENABLE, temp_dword);
1053 intr_loc2 = shpc_readl(ctrl, INTR_LOC);
1054 dbg("%s: intr_loc2 = %x\n",__FUNCTION__, intr_loc2);
1057 if (intr_loc & 0x0001) {
1059 * Command Complete Interrupt Pending
1060 * RO only - clear by writing 1 to the Command Completion
1061 * Detect bit in Controller SERR-INT register
1063 temp_dword = shpc_readl(ctrl, SERR_INTR_ENABLE);
1064 temp_dword &= 0xfffdffff;
1065 shpc_writel(ctrl, SERR_INTR_ENABLE, temp_dword);
1067 wake_up_interruptible(&ctrl->queue);
1070 if ((intr_loc = (intr_loc >> 1)) == 0)
1073 for (hp_slot = 0; hp_slot < ctrl->num_slots; hp_slot++) {
1074 /* To find out which slot has interrupt pending */
1075 if ((intr_loc >> hp_slot) & 0x01) {
1076 temp_dword = shpc_readl(ctrl, SLOT_REG(hp_slot));
1077 dbg("%s: Slot %x with intr, slot register = %x\n",
1078 __FUNCTION__, hp_slot, temp_dword);
1079 if ((php_ctlr->switch_change_callback) &&
1080 (temp_dword & MRL_CHANGE_DETECTED))
1081 schedule_flag += php_ctlr->switch_change_callback(
1082 hp_slot, php_ctlr->callback_instance_id);
1083 if ((php_ctlr->attention_button_callback) &&
1084 (temp_dword & BUTTON_PRESS_DETECTED))
1085 schedule_flag += php_ctlr->attention_button_callback(
1086 hp_slot, php_ctlr->callback_instance_id);
1087 if ((php_ctlr->presence_change_callback) &&
1088 (temp_dword & PRSNT_CHANGE_DETECTED))
1089 schedule_flag += php_ctlr->presence_change_callback(
1090 hp_slot , php_ctlr->callback_instance_id);
1091 if ((php_ctlr->power_fault_callback) &&
1092 (temp_dword & (ISO_PFAULT_DETECTED | CON_PFAULT_DETECTED)))
1093 schedule_flag += php_ctlr->power_fault_callback(
1094 hp_slot, php_ctlr->callback_instance_id);
1096 /* Clear all slot events */
1097 temp_dword &= ~SLOT_REG_RSVDZ_MASK;
1098 shpc_writel(ctrl, SLOT_REG(hp_slot), temp_dword);
1100 intr_loc2 = shpc_readl(ctrl, INTR_LOC);
1101 dbg("%s: intr_loc2 = %x\n",__FUNCTION__, intr_loc2);
1105 if (!shpchp_poll_mode) {
1106 /* Unmask Global Interrupt Mask */
1107 temp_dword = shpc_readl(ctrl, SERR_INTR_ENABLE);
1108 temp_dword &= 0xfffffffe;
1109 shpc_writel(ctrl, SERR_INTR_ENABLE, temp_dword);
1115 static int hpc_get_max_bus_speed (struct slot *slot, enum pci_bus_speed *value)
1118 struct controller *ctrl = slot->ctrl;
1119 enum pci_bus_speed bus_speed = PCI_SPEED_UNKNOWN;
1120 u8 pi = shpc_readb(ctrl, PROG_INTERFACE);
1121 u32 slot_avail1 = shpc_readl(ctrl, SLOT_AVAIL1);
1122 u32 slot_avail2 = shpc_readl(ctrl, SLOT_AVAIL2);
1127 if (slot_avail2 & SLOT_133MHZ_PCIX_533)
1128 bus_speed = PCI_SPEED_133MHz_PCIX_533;
1129 else if (slot_avail2 & SLOT_100MHZ_PCIX_533)
1130 bus_speed = PCI_SPEED_100MHz_PCIX_533;
1131 else if (slot_avail2 & SLOT_66MHZ_PCIX_533)
1132 bus_speed = PCI_SPEED_66MHz_PCIX_533;
1133 else if (slot_avail2 & SLOT_133MHZ_PCIX_266)
1134 bus_speed = PCI_SPEED_133MHz_PCIX_266;
1135 else if (slot_avail2 & SLOT_100MHZ_PCIX_266)
1136 bus_speed = PCI_SPEED_100MHz_PCIX_266;
1137 else if (slot_avail2 & SLOT_66MHZ_PCIX_266)
1138 bus_speed = PCI_SPEED_66MHz_PCIX_266;
1141 if (bus_speed == PCI_SPEED_UNKNOWN) {
1142 if (slot_avail1 & SLOT_133MHZ_PCIX)
1143 bus_speed = PCI_SPEED_133MHz_PCIX;
1144 else if (slot_avail1 & SLOT_100MHZ_PCIX)
1145 bus_speed = PCI_SPEED_100MHz_PCIX;
1146 else if (slot_avail1 & SLOT_66MHZ_PCIX)
1147 bus_speed = PCI_SPEED_66MHz_PCIX;
1148 else if (slot_avail2 & SLOT_66MHZ)
1149 bus_speed = PCI_SPEED_66MHz;
1150 else if (slot_avail1 & SLOT_33MHZ)
1151 bus_speed = PCI_SPEED_33MHz;
1157 dbg("Max bus speed = %d\n", bus_speed);
1162 static int hpc_get_cur_bus_speed (struct slot *slot, enum pci_bus_speed *value)
1165 struct controller *ctrl = slot->ctrl;
1166 enum pci_bus_speed bus_speed = PCI_SPEED_UNKNOWN;
1167 u16 sec_bus_reg = shpc_readw(ctrl, SEC_BUS_CONFIG);
1168 u8 pi = shpc_readb(ctrl, PROG_INTERFACE);
1169 u8 speed_mode = (pi == 2) ? (sec_bus_reg & 0xF) : (sec_bus_reg & 0x7);
1173 if ((pi == 1) && (speed_mode > 4)) {
1174 *value = PCI_SPEED_UNKNOWN;
1178 switch (speed_mode) {
1180 *value = PCI_SPEED_33MHz;
1183 *value = PCI_SPEED_66MHz;
1186 *value = PCI_SPEED_66MHz_PCIX;
1189 *value = PCI_SPEED_100MHz_PCIX;
1192 *value = PCI_SPEED_133MHz_PCIX;
1195 *value = PCI_SPEED_66MHz_PCIX_ECC;
1198 *value = PCI_SPEED_100MHz_PCIX_ECC;
1201 *value = PCI_SPEED_133MHz_PCIX_ECC;
1204 *value = PCI_SPEED_66MHz_PCIX_266;
1207 *value = PCI_SPEED_100MHz_PCIX_266;
1210 *value = PCI_SPEED_133MHz_PCIX_266;
1213 *value = PCI_SPEED_66MHz_PCIX_533;
1216 *value = PCI_SPEED_100MHz_PCIX_533;
1219 *value = PCI_SPEED_133MHz_PCIX_533;
1222 *value = PCI_SPEED_UNKNOWN;
1227 dbg("Current bus speed = %d\n", bus_speed);
1232 static struct hpc_ops shpchp_hpc_ops = {
1233 .power_on_slot = hpc_power_on_slot,
1234 .slot_enable = hpc_slot_enable,
1235 .slot_disable = hpc_slot_disable,
1236 .set_bus_speed_mode = hpc_set_bus_speed_mode,
1237 .set_attention_status = hpc_set_attention_status,
1238 .get_power_status = hpc_get_power_status,
1239 .get_attention_status = hpc_get_attention_status,
1240 .get_latch_status = hpc_get_latch_status,
1241 .get_adapter_status = hpc_get_adapter_status,
1243 .get_max_bus_speed = hpc_get_max_bus_speed,
1244 .get_cur_bus_speed = hpc_get_cur_bus_speed,
1245 .get_adapter_speed = hpc_get_adapter_speed,
1246 .get_mode1_ECC_cap = hpc_get_mode1_ECC_cap,
1247 .get_prog_int = hpc_get_prog_int,
1249 .query_power_fault = hpc_query_power_fault,
1250 .green_led_on = hpc_set_green_led_on,
1251 .green_led_off = hpc_set_green_led_off,
1252 .green_led_blink = hpc_set_green_led_blink,
1254 .release_ctlr = hpc_release_ctlr,
1257 int shpc_init(struct controller * ctrl, struct pci_dev * pdev)
1259 struct php_ctlr_state_s *php_ctlr, *p;
1260 void *instance_id = ctrl;
1261 int rc, num_slots = 0;
1263 static int first = 1;
1264 u32 shpc_base_offset;
1265 u32 tempdword, slot_reg, slot_config;
1270 ctrl->pci_dev = pdev; /* pci_dev of the P2P bridge */
1272 spin_lock_init(&list_lock);
1273 php_ctlr = kzalloc(sizeof(*php_ctlr), GFP_KERNEL);
1275 if (!php_ctlr) { /* allocate controller state data */
1276 err("%s: HPC controller memory allocation error!\n", __FUNCTION__);
1280 php_ctlr->pci_dev = pdev; /* save pci_dev in context */
1282 if ((pdev->vendor == PCI_VENDOR_ID_AMD) || (pdev->device ==
1283 PCI_DEVICE_ID_AMD_GOLAM_7450)) {
1284 /* amd shpc driver doesn't use Base Offset; assume 0 */
1285 ctrl->mmio_base = pci_resource_start(pdev, 0);
1286 ctrl->mmio_size = pci_resource_len(pdev, 0);
1288 ctrl->cap_offset = pci_find_capability(pdev, PCI_CAP_ID_SHPC);
1289 if (!ctrl->cap_offset) {
1290 err("%s : cap_offset == 0\n", __FUNCTION__);
1291 goto abort_free_ctlr;
1293 dbg("%s: cap_offset = %x\n", __FUNCTION__, ctrl->cap_offset);
1295 rc = shpc_indirect_read(ctrl, 0, &shpc_base_offset);
1297 err("%s: cannot read base_offset\n", __FUNCTION__);
1298 goto abort_free_ctlr;
1301 rc = shpc_indirect_read(ctrl, 3, &tempdword);
1303 err("%s: cannot read slot config\n", __FUNCTION__);
1304 goto abort_free_ctlr;
1306 num_slots = tempdword & SLOT_NUM;
1307 dbg("%s: num_slots (indirect) %x\n", __FUNCTION__, num_slots);
1309 for (i = 0; i < 9 + num_slots; i++) {
1310 rc = shpc_indirect_read(ctrl, i, &tempdword);
1312 err("%s: cannot read creg (index = %d)\n",
1314 goto abort_free_ctlr;
1316 dbg("%s: offset %d: value %x\n", __FUNCTION__,i,
1321 pci_resource_start(pdev, 0) + shpc_base_offset;
1322 ctrl->mmio_size = 0x24 + 0x4 * num_slots;
1326 spin_lock_init(&hpc_event_lock);
1330 info("HPC vendor_id %x device_id %x ss_vid %x ss_did %x\n", pdev->vendor, pdev->device, pdev->subsystem_vendor,
1331 pdev->subsystem_device);
1333 if (pci_enable_device(pdev))
1334 goto abort_free_ctlr;
1336 if (!request_mem_region(ctrl->mmio_base, ctrl->mmio_size, MY_NAME)) {
1337 err("%s: cannot reserve MMIO region\n", __FUNCTION__);
1338 goto abort_free_ctlr;
1341 php_ctlr->creg = ioremap(ctrl->mmio_base, ctrl->mmio_size);
1342 if (!php_ctlr->creg) {
1343 err("%s: cannot remap MMIO region %lx @ %lx\n", __FUNCTION__,
1344 ctrl->mmio_size, ctrl->mmio_base);
1345 release_mem_region(ctrl->mmio_base, ctrl->mmio_size);
1346 goto abort_free_ctlr;
1348 dbg("%s: php_ctlr->creg %p\n", __FUNCTION__, php_ctlr->creg);
1350 mutex_init(&ctrl->crit_sect);
1351 mutex_init(&ctrl->cmd_lock);
1353 /* Setup wait queue */
1354 init_waitqueue_head(&ctrl->queue);
1357 php_ctlr->irq = pdev->irq;
1358 php_ctlr->attention_button_callback = shpchp_handle_attention_button,
1359 php_ctlr->switch_change_callback = shpchp_handle_switch_change;
1360 php_ctlr->presence_change_callback = shpchp_handle_presence_change;
1361 php_ctlr->power_fault_callback = shpchp_handle_power_fault;
1362 php_ctlr->callback_instance_id = instance_id;
1364 ctrl->hpc_ctlr_handle = php_ctlr;
1365 ctrl->hpc_ops = &shpchp_hpc_ops;
1367 /* Return PCI Controller Info */
1368 slot_config = shpc_readl(ctrl, SLOT_CONFIG);
1369 php_ctlr->slot_device_offset = (slot_config & FIRST_DEV_NUM) >> 8;
1370 php_ctlr->num_slots = slot_config & SLOT_NUM;
1371 dbg("%s: slot_device_offset %x\n", __FUNCTION__, php_ctlr->slot_device_offset);
1372 dbg("%s: num_slots %x\n", __FUNCTION__, php_ctlr->num_slots);
1374 /* Mask Global Interrupt Mask & Command Complete Interrupt Mask */
1375 tempdword = shpc_readl(ctrl, SERR_INTR_ENABLE);
1376 dbg("%s: SERR_INTR_ENABLE = %x\n", __FUNCTION__, tempdword);
1377 tempdword = 0x0003000f;
1378 shpc_writel(ctrl, SERR_INTR_ENABLE, tempdword);
1379 tempdword = shpc_readl(ctrl, SERR_INTR_ENABLE);
1380 dbg("%s: SERR_INTR_ENABLE = %x\n", __FUNCTION__, tempdword);
1382 /* Mask the MRL sensor SERR Mask of individual slot in
1383 * Slot SERR-INT Mask & clear all the existing event if any
1385 for (hp_slot = 0; hp_slot < php_ctlr->num_slots; hp_slot++) {
1386 slot_reg = shpc_readl(ctrl, SLOT_REG(hp_slot));
1387 dbg("%s: Default Logical Slot Register %d value %x\n", __FUNCTION__,
1389 slot_reg |= (PRSNT_CHANGE_INTR_MASK | ISO_PFAULT_INTR_MASK |
1390 BUTTON_PRESS_INTR_MASK | MRL_CHANGE_INTR_MASK |
1391 CON_PFAULT_INTR_MASK | MRL_CHANGE_SERR_MASK |
1392 CON_PFAULT_SERR_MASK);
1393 slot_reg &= ~SLOT_REG_RSVDZ_MASK;
1394 shpc_writel(ctrl, SLOT_REG(hp_slot), slot_reg);
1397 if (shpchp_poll_mode) {/* Install interrupt polling code */
1398 /* Install and start the interrupt polling timer */
1399 init_timer(&php_ctlr->int_poll_timer);
1400 start_int_poll_timer( php_ctlr, 10 ); /* start with 10 second delay */
1402 /* Installs the interrupt handler */
1403 rc = pci_enable_msi(pdev);
1405 info("Can't get msi for the hotplug controller\n");
1406 info("Use INTx for the hotplug controller\n");
1408 php_ctlr->irq = pdev->irq;
1410 rc = request_irq(php_ctlr->irq, shpc_isr, SA_SHIRQ, MY_NAME, (void *) ctrl);
1411 dbg("%s: request_irq %d for hpc%d (returns %d)\n", __FUNCTION__, php_ctlr->irq, ctlr_seq_num, rc);
1413 err("Can't get irq %d for the hotplug controller\n", php_ctlr->irq);
1414 goto abort_free_ctlr;
1417 dbg("%s: HPC at b:d:f:irq=0x%x:%x:%x:%x\n", __FUNCTION__,
1418 pdev->bus->number, PCI_SLOT(pdev->devfn),
1419 PCI_FUNC(pdev->devfn), pdev->irq);
1420 get_hp_hw_control_from_firmware(pdev);
1422 /* Add this HPC instance into the HPC list */
1423 spin_lock(&list_lock);
1424 if (php_ctlr_list_head == 0) {
1425 php_ctlr_list_head = php_ctlr;
1426 p = php_ctlr_list_head;
1429 p = php_ctlr_list_head;
1434 p->pnext = php_ctlr;
1436 spin_unlock(&list_lock);
1441 * Unmask all event interrupts of all slots
1443 for (hp_slot = 0; hp_slot < php_ctlr->num_slots; hp_slot++) {
1444 slot_reg = shpc_readl(ctrl, SLOT_REG(hp_slot));
1445 dbg("%s: Default Logical Slot Register %d value %x\n", __FUNCTION__,
1447 slot_reg &= ~(PRSNT_CHANGE_INTR_MASK | ISO_PFAULT_INTR_MASK |
1448 BUTTON_PRESS_INTR_MASK | MRL_CHANGE_INTR_MASK |
1449 CON_PFAULT_INTR_MASK | SLOT_REG_RSVDZ_MASK);
1450 shpc_writel(ctrl, SLOT_REG(hp_slot), slot_reg);
1452 if (!shpchp_poll_mode) {
1453 /* Unmask all general input interrupts and SERR */
1454 tempdword = shpc_readl(ctrl, SERR_INTR_ENABLE);
1455 tempdword = 0x0000000a;
1456 shpc_writel(ctrl, SERR_INTR_ENABLE, tempdword);
1457 tempdword = shpc_readl(ctrl, SERR_INTR_ENABLE);
1458 dbg("%s: SERR_INTR_ENABLE = %x\n", __FUNCTION__, tempdword);
1464 /* We end up here for the many possible ways to fail this API. */