]> err.no Git - linux-2.6/blob - drivers/pci/hotplug/shpchp_hpc.c
[PATCH] SHPC: Fix SHPC Logical Slot Register bits access
[linux-2.6] / drivers / pci / hotplug / shpchp_hpc.c
1 /*
2  * Standard PCI Hot Plug Driver
3  *
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
8  *
9  * All rights reserved.
10  *
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.
15  *
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
20  * details.
21  *
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.
25  *
26  * Send feedback to <greg@kroah.com>,<kristen.c.accardi@intel.com>
27  *
28  */
29
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>
35
36 #include "shpchp.h"
37
38 #ifdef DEBUG
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
47
48 #define DEFINE_DBG_BUFFER     char __dbg_str_buf[256];
49
50 #define DBG_PRINT( dbg_flags, args... )              \
51         do {                                             \
52           if ( DEBUG_LEVEL & ( dbg_flags ) )             \
53           {                                              \
54             int len;                                     \
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 ); \
59           }                                              \
60         } while (0)
61
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]");
64 #else
65 #define DEFINE_DBG_BUFFER
66 #define DBG_ENTER_ROUTINE
67 #define DBG_LEAVE_ROUTINE
68 #endif                          /* DEBUG */
69
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
75
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
84
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
92
93 /*
94  * Logical Slot Register definitions
95  */
96 #define SLOT_REG(i)             (SLOT1 + (4 * i))
97
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)
132
133 /* SHPC 'write' operations/commands */
134
135 /* Slot operation - 0x00h to 0x3Fh */
136
137 #define NO_CHANGE               0x00
138
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    
143
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    
148
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   
153
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
163
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
179
180
181 /* Power-on all slots - 0x48h */
182 #define SET_PWR_ON_ALL          0x48
183
184 /* Enable all slots     - 0x49h */
185 #define SET_ENABLE_ALL          0x49
186
187 /*  SHPC controller command error code */
188 #define SWITCH_OPEN             0x1
189 #define INVALID_CMD             0x2
190 #define INVALID_SPEED_MODE      0x4
191
192 /* For accessing SHPC Working Register Set */
193 #define DWORD_SELECT            0x2
194 #define DWORD_DATA              0x4
195 #define BASE_OFFSET             0x0
196
197 /* Field Offset in Logical Slot Register - byte boundary */
198 #define SLOT_EVENT_LATCH        0x2
199 #define SLOT_SERR_INT_MASK      0x3
200
201 static spinlock_t hpc_event_lock;
202
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;
207
208 static irqreturn_t shpc_isr(int IRQ, void *dev_id, struct pt_regs *regs);
209
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);
212
213 static inline u8 shpc_readb(struct controller *ctrl, int reg)
214 {
215         return readb(ctrl->hpc_ctlr_handle->creg + reg);
216 }
217
218 static inline void shpc_writeb(struct controller *ctrl, int reg, u8 val)
219 {
220         writeb(val, ctrl->hpc_ctlr_handle->creg + reg);
221 }
222
223 static inline u16 shpc_readw(struct controller *ctrl, int reg)
224 {
225         return readw(ctrl->hpc_ctlr_handle->creg + reg);
226 }
227
228 static inline void shpc_writew(struct controller *ctrl, int reg, u16 val)
229 {
230         writew(val, ctrl->hpc_ctlr_handle->creg + reg);
231 }
232
233 static inline u32 shpc_readl(struct controller *ctrl, int reg)
234 {
235         return readl(ctrl->hpc_ctlr_handle->creg + reg);
236 }
237
238 static inline void shpc_writel(struct controller *ctrl, int reg, u32 val)
239 {
240         writel(val, ctrl->hpc_ctlr_handle->creg + reg);
241 }
242
243 static inline int shpc_indirect_read(struct controller *ctrl, int index,
244                                      u32 *value)
245 {
246         int rc;
247         u32 cap_offset = ctrl->cap_offset;
248         struct pci_dev *pdev = ctrl->pci_dev;
249
250         rc = pci_write_config_byte(pdev, cap_offset + DWORD_SELECT, index);
251         if (rc)
252                 return rc;
253         return pci_read_config_dword(pdev, cap_offset + DWORD_DATA, value);
254 }
255
256 /* This is the interrupt polling timeout function. */
257 static void int_poll_timeout(unsigned long lphp_ctlr)
258 {
259     struct php_ctlr_state_s *php_ctlr = (struct php_ctlr_state_s *)lphp_ctlr;
260
261     DBG_ENTER_ROUTINE
262
263     if ( !php_ctlr ) {
264                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
265                 return;
266     }
267
268     /* Poll for interrupt events.  regs == NULL => polling */
269     shpc_isr( 0, (void *)php_ctlr, NULL );
270
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*/
274
275     start_int_poll_timer(php_ctlr, shpchp_poll_time);  
276         
277         return;
278 }
279
280 /* This function starts the interrupt polling timer. */
281 static void start_int_poll_timer(struct php_ctlr_state_s *php_ctlr, int seconds)
282 {
283     if (!php_ctlr) {
284                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
285                 return;
286         }
287
288     if ( ( seconds <= 0 ) || ( seconds > 60 ) )
289         seconds = 2;            /* Clamp to sane value */
290
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);
295
296         return;
297 }
298
299 static inline int shpc_wait_cmd(struct controller *ctrl)
300 {
301         int retval = 0;
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);
306         if (!rc) {
307                 retval = -EIO;
308                 err("Command not completed in %d msec\n", timeout_msec);
309         } else if (rc < 0) {
310                 retval = -EINTR;
311                 info("Command was interrupted by a signal\n");
312         }
313         ctrl->cmd_busy = 0;
314
315         return retval;
316 }
317
318 static int shpc_write_cmd(struct slot *slot, u8 t_slot, u8 cmd)
319 {
320         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
321         struct controller *ctrl = slot->ctrl;
322         u16 cmd_status;
323         int retval = 0;
324         u16 temp_word;
325         int i;
326
327         DBG_ENTER_ROUTINE 
328
329         mutex_lock(&slot->ctrl->cmd_lock);
330
331         if (!php_ctlr) {
332                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
333                 retval = -EINVAL;
334                 goto out;
335         }
336
337         for (i = 0; i < 10; i++) {
338                 cmd_status = shpc_readw(ctrl, CMD_STATUS);
339                 
340                 if (!(cmd_status & 0x1))
341                         break;
342                 /*  Check every 0.1 sec for a total of 1 sec*/
343                 msleep(100);
344         }
345
346         cmd_status = shpc_readw(ctrl, CMD_STATUS);
347         
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__);
351                 retval = -EBUSY;
352                 goto out;
353         }
354
355         ++t_slot;
356         temp_word =  (t_slot << 8) | (cmd & 0xFF);
357         dbg("%s: t_slot %x cmd %x\n", __FUNCTION__, t_slot, cmd);
358         
359         /* To make sure the Controller Busy bit is 0 before we send out the
360          * command. 
361          */
362         slot->ctrl->cmd_busy = 1;
363         shpc_writew(ctrl, CMD, temp_word);
364
365         /*
366          * Wait for command completion.
367          */
368         retval = shpc_wait_cmd(slot->ctrl);
369         if (retval)
370                 goto out;
371
372         cmd_status = hpc_check_cmd_status(slot->ctrl);
373         if (cmd_status) {
374                 err("%s: Failed to issued command 0x%x (error code = %d)\n",
375                     __FUNCTION__, cmd, cmd_status);
376                 retval = -EIO;
377         }
378  out:
379         mutex_unlock(&slot->ctrl->cmd_lock);
380
381         DBG_LEAVE_ROUTINE 
382         return retval;
383 }
384
385 static int hpc_check_cmd_status(struct controller *ctrl)
386 {
387         u16 cmd_status;
388         int retval = 0;
389
390         DBG_ENTER_ROUTINE 
391         
392         if (!ctrl->hpc_ctlr_handle) {
393                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
394                 return -1;
395         }
396
397         cmd_status = shpc_readw(ctrl, CMD_STATUS) & 0x000F;
398         
399         switch (cmd_status >> 1) {
400         case 0:
401                 retval = 0;
402                 break;
403         case 1:
404                 retval = SWITCH_OPEN;
405                 err("%s: Switch opened!\n", __FUNCTION__);
406                 break;
407         case 2:
408                 retval = INVALID_CMD;
409                 err("%s: Invalid HPC command!\n", __FUNCTION__);
410                 break;
411         case 4:
412                 retval = INVALID_SPEED_MODE;
413                 err("%s: Invalid bus speed/mode!\n", __FUNCTION__);
414                 break;
415         default:
416                 retval = cmd_status;
417         }
418
419         DBG_LEAVE_ROUTINE 
420         return retval;
421 }
422
423
424 static int hpc_get_attention_status(struct slot *slot, u8 *status)
425 {
426         struct controller *ctrl = slot->ctrl;
427         u32 slot_reg;
428         u8 state;
429         
430         DBG_ENTER_ROUTINE 
431
432         if (!slot->ctrl->hpc_ctlr_handle) {
433                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
434                 return -1;
435         }
436
437         slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
438         state = (slot_reg & ATN_LED_STATE_MASK) >> ATN_LED_STATE_SHIFT;
439
440         switch (state) {
441         case ATN_LED_STATE_ON:
442                 *status = 1;    /* On */
443                 break;
444         case ATN_LED_STATE_BLINK:
445                 *status = 2;    /* Blink */
446                 break;
447         case ATN_LED_STATE_OFF:
448                 *status = 0;    /* Off */
449                 break;
450         default:
451                 *status = 0xFF; /* Reserved */
452                 break;
453         }
454
455         DBG_LEAVE_ROUTINE 
456         return 0;
457 }
458
459 static int hpc_get_power_status(struct slot * slot, u8 *status)
460 {
461         struct controller *ctrl = slot->ctrl;
462         u32 slot_reg;
463         u8 state;
464         
465         DBG_ENTER_ROUTINE 
466
467         if (!slot->ctrl->hpc_ctlr_handle) {
468                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
469                 return -1;
470         }
471
472         slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
473         state = (slot_reg & SLOT_STATE_MASK) >> SLOT_STATE_SHIFT;
474
475         switch (state) {
476         case SLOT_STATE_PWRONLY:
477                 *status = 2;    /* Powered only */
478                 break;
479         case SLOT_STATE_ENABLED:
480                 *status = 1;    /* Enabled */
481                 break;
482         case SLOT_STATE_DISABLED:
483                 *status = 0;    /* Disabled */
484                 break;
485         default:
486                 *status = 0xFF; /* Reserved */
487                 break;
488         }
489
490         DBG_LEAVE_ROUTINE 
491         return 0;
492 }
493
494
495 static int hpc_get_latch_status(struct slot *slot, u8 *status)
496 {
497         struct controller *ctrl = slot->ctrl;
498         u32 slot_reg;
499
500         DBG_ENTER_ROUTINE 
501
502         if (!slot->ctrl->hpc_ctlr_handle) {
503                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
504                 return -1;
505         }
506
507         slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
508         *status = !!(slot_reg & MRL_SENSOR);    /* 0 -> close; 1 -> open */
509
510         DBG_LEAVE_ROUTINE 
511         return 0;
512 }
513
514 static int hpc_get_adapter_status(struct slot *slot, u8 *status)
515 {
516         struct controller *ctrl = slot->ctrl;
517         u32 slot_reg;
518         u8 state;
519
520         DBG_ENTER_ROUTINE 
521
522         if (!slot->ctrl->hpc_ctlr_handle) {
523                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
524                 return -1;
525         }
526
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;
530
531         DBG_LEAVE_ROUTINE 
532         return 0;
533 }
534
535 static int hpc_get_prog_int(struct slot *slot, u8 *prog_int)
536 {
537         struct controller *ctrl = slot->ctrl;
538
539         DBG_ENTER_ROUTINE 
540         
541         if (!slot->ctrl->hpc_ctlr_handle) {
542                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
543                 return -1;
544         }
545
546         *prog_int = shpc_readb(ctrl, PROG_INTERFACE);
547
548         DBG_LEAVE_ROUTINE 
549         return 0;
550 }
551
552 static int hpc_get_adapter_speed(struct slot *slot, enum pci_bus_speed *value)
553 {
554         int retval = 0;
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);
558         u8 pi, pcix_cap;
559
560         DBG_ENTER_ROUTINE 
561
562         if ((retval = hpc_get_prog_int(slot, &pi)))
563                 return retval;
564
565         switch (pi) {
566         case 1:
567                 pcix_cap = (slot_reg & PCIX_CAP_MASK_PI1) >> PCIX_CAP_SHIFT;
568                 break;
569         case 2:
570                 pcix_cap = (slot_reg & PCIX_CAP_MASK_PI2) >> PCIX_CAP_SHIFT;
571                 break;
572         default:
573                 return -ENODEV;
574         }
575
576         dbg("%s: slot_reg = %x, pcix_cap = %x, m66_cap = %x\n",
577             __FUNCTION__, slot_reg, pcix_cap, m66_cap);
578
579         switch (pcix_cap) {
580         case 0x0:
581                 *value = m66_cap ? PCI_SPEED_66MHz : PCI_SPEED_33MHz;
582                 break;
583         case 0x1:
584                 *value = PCI_SPEED_66MHz_PCIX;
585                 break;
586         case 0x3:
587                 *value = PCI_SPEED_133MHz_PCIX;
588                 break;
589         case 0x4:
590                 *value = PCI_SPEED_133MHz_PCIX_266;
591                 break;
592         case 0x5:
593                 *value = PCI_SPEED_133MHz_PCIX_533;
594                 break;
595         case 0x2:
596         default:
597                 *value = PCI_SPEED_UNKNOWN;
598                 retval = -ENODEV;
599                 break;
600         }
601
602         dbg("Adapter speed = %d\n", *value);
603         DBG_LEAVE_ROUTINE 
604         return retval;
605 }
606
607 static int hpc_get_mode1_ECC_cap(struct slot *slot, u8 *mode)
608 {
609         struct controller *ctrl = slot->ctrl;
610         u16 sec_bus_status;
611         u8 pi;
612         int retval = 0;
613
614         DBG_ENTER_ROUTINE 
615
616         if (!slot->ctrl->hpc_ctlr_handle) {
617                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
618                 return -1;
619         }
620
621         pi = shpc_readb(ctrl, PROG_INTERFACE);
622         sec_bus_status = shpc_readw(ctrl, SEC_BUS_CONFIG);
623
624         if (pi == 2) {
625                 *mode = (sec_bus_status & 0x0100) >> 8;
626         } else {
627                 retval = -1;
628         }
629
630         dbg("Mode 1 ECC cap = %d\n", *mode);
631         
632         DBG_LEAVE_ROUTINE 
633         return retval;
634 }
635
636 static int hpc_query_power_fault(struct slot * slot)
637 {
638         struct controller *ctrl = slot->ctrl;
639         u32 slot_reg;
640
641         DBG_ENTER_ROUTINE 
642
643         if (!slot->ctrl->hpc_ctlr_handle) {
644                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
645                 return -1;
646         }
647
648         slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
649
650         DBG_LEAVE_ROUTINE
651         /* Note: Logic 0 => fault */
652         return !(slot_reg & POWER_FAULT);
653 }
654
655 static int hpc_set_attention_status(struct slot *slot, u8 value)
656 {
657         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
658         u8 slot_cmd = 0;
659         int rc = 0;
660
661         if (!slot->ctrl->hpc_ctlr_handle) {
662                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
663                 return -1;
664         }
665
666         if (slot->hp_slot >= php_ctlr->num_slots) {
667                 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
668                 return -1;
669         }
670
671         switch (value) {
672                 case 0 :        
673                         slot_cmd = 0x30;        /* OFF */
674                         break;
675                 case 1:
676                         slot_cmd = 0x10;        /* ON */
677                         break;
678                 case 2:
679                         slot_cmd = 0x20;        /* BLINK */
680                         break;
681                 default:
682                         return -1;
683         }
684
685         shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
686         
687         return rc;
688 }
689
690
691 static void hpc_set_green_led_on(struct slot *slot)
692 {
693         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
694         u8 slot_cmd;
695
696         if (!slot->ctrl->hpc_ctlr_handle) {
697                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
698                 return ;
699         }
700
701         if (slot->hp_slot >= php_ctlr->num_slots) {
702                 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
703                 return ;
704         }
705
706         slot_cmd = 0x04;
707
708         shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
709
710         return;
711 }
712
713 static void hpc_set_green_led_off(struct slot *slot)
714 {
715         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
716         u8 slot_cmd;
717
718         if (!slot->ctrl->hpc_ctlr_handle) {
719                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
720                 return ;
721         }
722
723         if (slot->hp_slot >= php_ctlr->num_slots) {
724                 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
725                 return ;
726         }
727
728         slot_cmd = 0x0C;
729
730         shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
731
732         return;
733 }
734
735 static void hpc_set_green_led_blink(struct slot *slot)
736 {
737         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
738         u8 slot_cmd;
739
740         if (!slot->ctrl->hpc_ctlr_handle) {
741                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
742                 return ;
743         }
744
745         if (slot->hp_slot >= php_ctlr->num_slots) {
746                 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
747                 return ;
748         }
749
750         slot_cmd = 0x08;
751
752         shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
753
754         return;
755 }
756
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        */
762         int *flags)
763 {
764         u32 slot_config;
765
766         DBG_ENTER_ROUTINE 
767
768         if (!ctrl->hpc_ctlr_handle) {
769                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
770                 return -1;
771         }
772
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;
778
779         dbg("%s: physical_slot_num = %x\n", __FUNCTION__, *physical_slot_num);
780
781         DBG_LEAVE_ROUTINE 
782         return 0;
783 }
784
785 static void hpc_release_ctlr(struct controller *ctrl)
786 {
787         struct php_ctlr_state_s *php_ctlr = ctrl->hpc_ctlr_handle;
788         struct php_ctlr_state_s *p, *p_prev;
789         int i;
790         u32 slot_reg;
791
792         DBG_ENTER_ROUTINE 
793
794         if (!ctrl->hpc_ctlr_handle) {
795                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
796                 return ;
797         }
798
799         /*
800          * Mask event interrupts and SERRs of all slots
801          */
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);
810         }
811
812         cleanup_slots(ctrl);
813
814         if (shpchp_poll_mode) {
815             del_timer(&php_ctlr->int_poll_timer);
816         } else {        
817                 if (php_ctlr->irq) {
818                         free_irq(php_ctlr->irq, ctrl);
819                         php_ctlr->irq = 0;
820                         pci_disable_msi(php_ctlr->pci_dev);
821                 }
822         }
823
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;
828         }
829
830         spin_lock(&list_lock);
831         p = php_ctlr_list_head;
832         p_prev = NULL;
833         while (p) {
834                 if (p == php_ctlr) {
835                         if (p_prev)
836                                 p_prev->pnext = p->pnext;
837                         else
838                                 php_ctlr_list_head = p->pnext;
839                         break;
840                 } else {
841                         p_prev = p;
842                         p = p->pnext;
843                 }
844         }
845         spin_unlock(&list_lock);
846
847         kfree(php_ctlr);
848
849 DBG_LEAVE_ROUTINE
850                           
851 }
852
853 static int hpc_power_on_slot(struct slot * slot)
854 {
855         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
856         u8 slot_cmd;
857         int retval = 0;
858
859         DBG_ENTER_ROUTINE 
860
861         if (!slot->ctrl->hpc_ctlr_handle) {
862                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
863                 return -1;
864         }
865
866         if (slot->hp_slot >= php_ctlr->num_slots) {
867                 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
868                 return -1;
869         }
870         slot_cmd = 0x01;
871
872         retval = shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
873
874         if (retval) {
875                 err("%s: Write command failed!\n", __FUNCTION__);
876                 return -1;
877         }
878
879         DBG_LEAVE_ROUTINE
880
881         return retval;
882 }
883
884 static int hpc_slot_enable(struct slot * slot)
885 {
886         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
887         u8 slot_cmd;
888         int retval = 0;
889
890         DBG_ENTER_ROUTINE 
891
892         if (!slot->ctrl->hpc_ctlr_handle) {
893                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
894                 return -1;
895         }
896
897         if (slot->hp_slot >= php_ctlr->num_slots) {
898                 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
899                 return -1;
900         }
901         /* 3A => Slot - Enable, Power Indicator - Blink, Attention Indicator - Off */
902         slot_cmd = 0x3A;  
903
904         retval = shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
905
906         if (retval) {
907                 err("%s: Write command failed!\n", __FUNCTION__);
908                 return -1;
909         }
910
911         DBG_LEAVE_ROUTINE
912         return retval;
913 }
914
915 static int hpc_slot_disable(struct slot * slot)
916 {
917         struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
918         u8 slot_cmd;
919         int retval = 0;
920
921         DBG_ENTER_ROUTINE 
922
923         if (!slot->ctrl->hpc_ctlr_handle) {
924                 err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
925                 return -1;
926         }
927
928         if (slot->hp_slot >= php_ctlr->num_slots) {
929                 err("%s: Invalid HPC slot number!\n", __FUNCTION__);
930                 return -1;
931         }
932
933         /* 1F => Slot - Disable, Power Indicator - Off, Attention Indicator - On */
934         slot_cmd = 0x1F;
935
936         retval = shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
937
938         if (retval) {
939                 err("%s: Write command failed!\n", __FUNCTION__);
940                 return -1;
941         }
942
943         DBG_LEAVE_ROUTINE
944         return retval;
945 }
946
947 static int hpc_set_bus_speed_mode(struct slot * slot, enum pci_bus_speed value)
948 {
949         int retval;
950         struct controller *ctrl = slot->ctrl;
951         u8 pi, cmd;
952
953         DBG_ENTER_ROUTINE 
954
955         pi = shpc_readb(ctrl, PROG_INTERFACE);
956         if ((pi == 1) && (value > PCI_SPEED_133MHz_PCIX))
957                 return -EINVAL;
958
959         switch (value) {
960         case PCI_SPEED_33MHz:
961                 cmd = SETA_PCI_33MHZ;
962                 break;
963         case PCI_SPEED_66MHz:
964                 cmd = SETA_PCI_66MHZ;
965                 break;
966         case PCI_SPEED_66MHz_PCIX:
967                 cmd = SETA_PCIX_66MHZ;
968                 break;
969         case PCI_SPEED_100MHz_PCIX:
970                 cmd = SETA_PCIX_100MHZ;
971                 break;
972         case PCI_SPEED_133MHz_PCIX:
973                 cmd = SETA_PCIX_133MHZ;
974                 break;
975         case PCI_SPEED_66MHz_PCIX_ECC:
976                 cmd = SETB_PCIX_66MHZ_EM;
977                 break;
978         case PCI_SPEED_100MHz_PCIX_ECC:
979                 cmd = SETB_PCIX_100MHZ_EM;
980                 break;
981         case PCI_SPEED_133MHz_PCIX_ECC:
982                 cmd = SETB_PCIX_133MHZ_EM;
983                 break;
984         case PCI_SPEED_66MHz_PCIX_266:
985                 cmd = SETB_PCIX_66MHZ_266;
986                 break;
987         case PCI_SPEED_100MHz_PCIX_266:
988                 cmd = SETB_PCIX_100MHZ_266;
989                 break;
990         case PCI_SPEED_133MHz_PCIX_266:
991                 cmd = SETB_PCIX_133MHZ_266;
992                 break;
993         case PCI_SPEED_66MHz_PCIX_533:
994                 cmd = SETB_PCIX_66MHZ_533;
995                 break;
996         case PCI_SPEED_100MHz_PCIX_533:
997                 cmd = SETB_PCIX_100MHZ_533;
998                 break;
999         case PCI_SPEED_133MHz_PCIX_533:
1000                 cmd = SETB_PCIX_133MHZ_533;
1001                 break;
1002         default:
1003                 return -EINVAL;
1004         }
1005
1006         retval = shpc_write_cmd(slot, 0, cmd);
1007         if (retval)
1008                 err("%s: Write command failed!\n", __FUNCTION__);
1009
1010         DBG_LEAVE_ROUTINE
1011         return retval;
1012 }
1013
1014 static irqreturn_t shpc_isr(int IRQ, void *dev_id, struct pt_regs *regs)
1015 {
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;
1020         int hp_slot;
1021
1022         if (!dev_id)
1023                 return IRQ_NONE;
1024
1025         if (!shpchp_poll_mode) { 
1026                 ctrl = (struct controller *)dev_id;
1027                 php_ctlr = ctrl->hpc_ctlr_handle;
1028         } else { 
1029                 php_ctlr = (struct php_ctlr_state_s *) dev_id;
1030                 ctrl = (struct controller *)php_ctlr->callback_instance_id;
1031         }
1032
1033         if (!ctrl)
1034                 return IRQ_NONE;
1035         
1036         if (!php_ctlr || !php_ctlr->creg)
1037                 return IRQ_NONE;
1038
1039         /* Check to see if it was our interrupt */
1040         intr_loc = shpc_readl(ctrl, INTR_LOC);
1041
1042         if (!intr_loc)
1043                 return IRQ_NONE;
1044         dbg("%s: intr_loc = %x\n",__FUNCTION__, intr_loc); 
1045
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);
1052
1053                 intr_loc2 = shpc_readl(ctrl, INTR_LOC);
1054                 dbg("%s: intr_loc2 = %x\n",__FUNCTION__, intr_loc2); 
1055         }
1056
1057         if (intr_loc & 0x0001) {
1058                 /* 
1059                  * Command Complete Interrupt Pending 
1060                  * RO only - clear by writing 1 to the Command Completion
1061                  * Detect bit in Controller SERR-INT register
1062                  */
1063                 temp_dword = shpc_readl(ctrl, SERR_INTR_ENABLE);
1064                 temp_dword &= 0xfffdffff;
1065                 shpc_writel(ctrl, SERR_INTR_ENABLE, temp_dword);
1066                 ctrl->cmd_busy = 0;
1067                 wake_up_interruptible(&ctrl->queue);
1068         }
1069
1070         if ((intr_loc = (intr_loc >> 1)) == 0)
1071                 goto out;
1072
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);
1095                         
1096                         /* Clear all slot events */
1097                         temp_dword &= ~SLOT_REG_RSVDZ_MASK;
1098                         shpc_writel(ctrl, SLOT_REG(hp_slot), temp_dword);
1099
1100                         intr_loc2 = shpc_readl(ctrl, INTR_LOC);
1101                         dbg("%s: intr_loc2 = %x\n",__FUNCTION__, intr_loc2); 
1102                 }
1103         }
1104  out:
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);
1110         }
1111         
1112         return IRQ_HANDLED;
1113 }
1114
1115 static int hpc_get_max_bus_speed (struct slot *slot, enum pci_bus_speed *value)
1116 {
1117         int retval = 0;
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);
1123
1124         DBG_ENTER_ROUTINE 
1125
1126         if (pi == 2) {
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;
1139         }
1140
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;
1152                 else
1153                         retval = -ENODEV;
1154         }
1155
1156         *value = bus_speed;
1157         dbg("Max bus speed = %d\n", bus_speed);
1158         DBG_LEAVE_ROUTINE 
1159         return retval;
1160 }
1161
1162 static int hpc_get_cur_bus_speed (struct slot *slot, enum pci_bus_speed *value)
1163 {
1164         int retval = 0;
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);
1170
1171         DBG_ENTER_ROUTINE 
1172
1173         if ((pi == 1) && (speed_mode > 4)) {
1174                 *value = PCI_SPEED_UNKNOWN;
1175                 return -ENODEV;
1176         }
1177
1178         switch (speed_mode) {
1179         case 0x0:
1180                 *value = PCI_SPEED_33MHz;
1181                 break;
1182         case 0x1:
1183                 *value = PCI_SPEED_66MHz;
1184                 break;
1185         case 0x2:
1186                 *value = PCI_SPEED_66MHz_PCIX;
1187                 break;
1188         case 0x3:
1189                 *value = PCI_SPEED_100MHz_PCIX;
1190                 break;
1191         case 0x4:
1192                 *value = PCI_SPEED_133MHz_PCIX;
1193                 break;
1194         case 0x5:
1195                 *value = PCI_SPEED_66MHz_PCIX_ECC;
1196                 break;
1197         case 0x6:
1198                 *value = PCI_SPEED_100MHz_PCIX_ECC;
1199                 break;
1200         case 0x7:
1201                 *value = PCI_SPEED_133MHz_PCIX_ECC;
1202                 break;
1203         case 0x8:
1204                 *value = PCI_SPEED_66MHz_PCIX_266;
1205                 break;
1206         case 0x9:
1207                 *value = PCI_SPEED_100MHz_PCIX_266;
1208                 break;
1209         case 0xa:
1210                 *value = PCI_SPEED_133MHz_PCIX_266;
1211                 break;
1212         case 0xb:
1213                 *value = PCI_SPEED_66MHz_PCIX_533;
1214                 break;
1215         case 0xc:
1216                 *value = PCI_SPEED_100MHz_PCIX_533;
1217                 break;
1218         case 0xd:
1219                 *value = PCI_SPEED_133MHz_PCIX_533;
1220                 break;
1221         default:
1222                 *value = PCI_SPEED_UNKNOWN;
1223                 retval = -ENODEV;
1224                 break;
1225         }
1226
1227         dbg("Current bus speed = %d\n", bus_speed);
1228         DBG_LEAVE_ROUTINE 
1229         return retval;
1230 }
1231
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,
1242
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,
1248
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,
1253         
1254         .release_ctlr                   = hpc_release_ctlr,
1255 };
1256
1257 int shpc_init(struct controller * ctrl, struct pci_dev * pdev)
1258 {
1259         struct php_ctlr_state_s *php_ctlr, *p;
1260         void *instance_id = ctrl;
1261         int rc, num_slots = 0;
1262         u8 hp_slot;
1263         static int first = 1;
1264         u32 shpc_base_offset;
1265         u32 tempdword, slot_reg, slot_config;
1266         u8 i;
1267
1268         DBG_ENTER_ROUTINE
1269
1270         ctrl->pci_dev = pdev;  /* pci_dev of the P2P bridge */
1271
1272         spin_lock_init(&list_lock);
1273         php_ctlr = kzalloc(sizeof(*php_ctlr), GFP_KERNEL);
1274
1275         if (!php_ctlr) {        /* allocate controller state data */
1276                 err("%s: HPC controller memory allocation error!\n", __FUNCTION__);
1277                 goto abort;
1278         }
1279
1280         php_ctlr->pci_dev = pdev;       /* save pci_dev in context */
1281
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);
1287         } else {
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;
1292                 }
1293                 dbg("%s: cap_offset = %x\n", __FUNCTION__, ctrl->cap_offset);
1294
1295                 rc = shpc_indirect_read(ctrl, 0, &shpc_base_offset);
1296                 if (rc) {
1297                         err("%s: cannot read base_offset\n", __FUNCTION__);
1298                         goto abort_free_ctlr;
1299                 }
1300
1301                 rc = shpc_indirect_read(ctrl, 3, &tempdword);
1302                 if (rc) {
1303                         err("%s: cannot read slot config\n", __FUNCTION__);
1304                         goto abort_free_ctlr;
1305                 }
1306                 num_slots = tempdword & SLOT_NUM;
1307                 dbg("%s: num_slots (indirect) %x\n", __FUNCTION__, num_slots);
1308
1309                 for (i = 0; i < 9 + num_slots; i++) {
1310                         rc = shpc_indirect_read(ctrl, i, &tempdword);
1311                         if (rc) {
1312                                 err("%s: cannot read creg (index = %d)\n",
1313                                     __FUNCTION__, i);
1314                                 goto abort_free_ctlr;
1315                         }
1316                         dbg("%s: offset %d: value %x\n", __FUNCTION__,i,
1317                                         tempdword);
1318                 }
1319
1320                 ctrl->mmio_base =
1321                         pci_resource_start(pdev, 0) + shpc_base_offset;
1322                 ctrl->mmio_size = 0x24 + 0x4 * num_slots;
1323         }
1324
1325         if (first) {
1326                 spin_lock_init(&hpc_event_lock);
1327                 first = 0;
1328         }
1329
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);
1332         
1333         if (pci_enable_device(pdev))
1334                 goto abort_free_ctlr;
1335
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;
1339         }
1340
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;
1347         }
1348         dbg("%s: php_ctlr->creg %p\n", __FUNCTION__, php_ctlr->creg);
1349
1350         mutex_init(&ctrl->crit_sect);
1351         mutex_init(&ctrl->cmd_lock);
1352
1353         /* Setup wait queue */
1354         init_waitqueue_head(&ctrl->queue);
1355
1356         /* Find the IRQ */
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;
1363
1364         ctrl->hpc_ctlr_handle = php_ctlr;
1365         ctrl->hpc_ops = &shpchp_hpc_ops;
1366
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);
1373
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);
1381
1382         /* Mask the MRL sensor SERR Mask of individual slot in
1383          * Slot SERR-INT Mask & clear all the existing event if any
1384          */
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__,
1388                         hp_slot, slot_reg);
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);
1395         }
1396         
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 */
1401         } else {
1402                 /* Installs the interrupt handler */
1403                 rc = pci_enable_msi(pdev);
1404                 if (rc) {
1405                         info("Can't get msi for the hotplug controller\n");
1406                         info("Use INTx for the hotplug controller\n");
1407                 } else
1408                         php_ctlr->irq = pdev->irq;
1409                 
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);
1412                 if (rc) {
1413                         err("Can't get irq %d for the hotplug controller\n", php_ctlr->irq);
1414                         goto abort_free_ctlr;
1415                 }
1416         }
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);
1421
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;
1427                 p->pnext = NULL;
1428         } else {
1429                 p = php_ctlr_list_head;
1430
1431                 while (p->pnext)
1432                         p = p->pnext;
1433
1434                 p->pnext = php_ctlr;
1435         }
1436         spin_unlock(&list_lock);
1437
1438         ctlr_seq_num++;
1439
1440         /*
1441          * Unmask all event interrupts of all slots
1442          */
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__,
1446                         hp_slot, slot_reg);
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);
1451         }
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);
1459         }
1460
1461         DBG_LEAVE_ROUTINE
1462         return 0;
1463
1464         /* We end up here for the many possible ways to fail this API.  */
1465 abort_free_ctlr:
1466         kfree(php_ctlr);
1467 abort:
1468         DBG_LEAVE_ROUTINE
1469         return -1;
1470 }