2  * IBM Hot Plug Controller Driver
 
   4  * Written By: Jyoti Shah, IBM Corporation
 
   6  * Copyright (C) 2001-2003 IBM Corp.
 
  10  * This program is free software; you can redistribute it and/or modify
 
  11  * it under the terms of the GNU General Public License as published by
 
  12  * the Free Software Foundation; either version 2 of the License, or (at
 
  13  * your option) any later version.
 
  15  * This program is distributed in the hope that it will be useful, but
 
  16  * WITHOUT ANY WARRANTY; without even the implied warranty of
 
  17  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
 
  18  * NON INFRINGEMENT.  See the GNU General Public License for more
 
  21  * You should have received a copy of the GNU General Public License
 
  22  * along with this program; if not, write to the Free Software
 
  23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
  25  * Send feedback to <gregkh@us.ibm.com>
 
  30 #include <linux/wait.h>
 
  31 #include <linux/time.h>
 
  32 #include <linux/delay.h>
 
  33 #include <linux/module.h>
 
  34 #include <linux/pci.h>
 
  35 #include <linux/smp_lock.h>
 
  36 #include <linux/init.h>
 
  37 #include <linux/mutex.h>
 
  41 static int to_debug = 0;
 
  42 #define debug_polling(fmt, arg...)      do { if (to_debug) debug (fmt, arg); } while (0)
 
  44 //----------------------------------------------------------------------------
 
  46 //----------------------------------------------------------------------------
 
  47 #define CMD_COMPLETE_TOUT_SEC   60      // give HPC 60 sec to finish cmd
 
  48 #define HPC_CTLR_WORKING_TOUT   60      // give HPC 60 sec to finish cmd
 
  49 #define HPC_GETACCESS_TIMEOUT   60      // seconds
 
  50 #define POLL_INTERVAL_SEC       2       // poll HPC every 2 seconds
 
  51 #define POLL_LATCH_CNT          5       // poll latch 5 times, then poll slots
 
  53 //----------------------------------------------------------------------------
 
  54 // Winnipeg Architected Register Offsets
 
  55 //----------------------------------------------------------------------------
 
  56 #define WPG_I2CMBUFL_OFFSET     0x08    // I2C Message Buffer Low
 
  57 #define WPG_I2CMOSUP_OFFSET     0x10    // I2C Master Operation Setup Reg
 
  58 #define WPG_I2CMCNTL_OFFSET     0x20    // I2C Master Control Register
 
  59 #define WPG_I2CPARM_OFFSET      0x40    // I2C Parameter Register
 
  60 #define WPG_I2CSTAT_OFFSET      0x70    // I2C Status Register
 
  62 //----------------------------------------------------------------------------
 
  63 // Winnipeg Store Type commands (Add this commands to the register offset)
 
  64 //----------------------------------------------------------------------------
 
  65 #define WPG_I2C_AND             0x1000  // I2C AND operation
 
  66 #define WPG_I2C_OR              0x2000  // I2C OR operation
 
  68 //----------------------------------------------------------------------------
 
  69 // Command set for I2C Master Operation Setup Register
 
  70 //----------------------------------------------------------------------------
 
  71 #define WPG_READATADDR_MASK     0x00010000      // read,bytes,I2C shifted,index
 
  72 #define WPG_WRITEATADDR_MASK    0x40010000      // write,bytes,I2C shifted,index
 
  73 #define WPG_READDIRECT_MASK     0x10010000
 
  74 #define WPG_WRITEDIRECT_MASK    0x60010000
 
  77 //----------------------------------------------------------------------------
 
  78 // bit masks for I2C Master Control Register
 
  79 //----------------------------------------------------------------------------
 
  80 #define WPG_I2CMCNTL_STARTOP_MASK       0x00000002      // Start the Operation
 
  82 //----------------------------------------------------------------------------
 
  84 //----------------------------------------------------------------------------
 
  85 #define WPG_I2C_IOREMAP_SIZE    0x2044  // size of linear address interval
 
  87 //----------------------------------------------------------------------------
 
  89 //----------------------------------------------------------------------------
 
  90 #define WPG_1ST_SLOT_INDEX      0x01    // index - 1st slot for ctlr
 
  91 #define WPG_CTLR_INDEX          0x0F    // index - ctlr
 
  92 #define WPG_1ST_EXTSLOT_INDEX   0x10    // index - 1st ext slot for ctlr
 
  93 #define WPG_1ST_BUS_INDEX       0x1F    // index - 1st bus for ctlr
 
  95 //----------------------------------------------------------------------------
 
  97 //----------------------------------------------------------------------------
 
  98 // if bits 20,22,25,26,27,29,30 are OFF return 1
 
  99 #define HPC_I2CSTATUS_CHECK(s)  ((u8)((s & 0x00000A76) ? 0 : 1))
 
 101 //----------------------------------------------------------------------------
 
 103 //----------------------------------------------------------------------------
 
 104 static int ibmphp_shutdown;
 
 106 static struct mutex sem_hpcaccess;      // lock access to HPC
 
 107 static struct semaphore semOperations;  // lock all operations and
 
 108                                         // access to data structures
 
 109 static struct semaphore sem_exit;       // make sure polling thread goes away
 
 110 //----------------------------------------------------------------------------
 
 111 // local function prototypes
 
 112 //----------------------------------------------------------------------------
 
 113 static u8 i2c_ctrl_read (struct controller *, void __iomem *, u8);
 
 114 static u8 i2c_ctrl_write (struct controller *, void __iomem *, u8, u8);
 
 115 static u8 hpc_writecmdtoindex (u8, u8);
 
 116 static u8 hpc_readcmdtoindex (u8, u8);
 
 117 static void get_hpc_access (void);
 
 118 static void free_hpc_access (void);
 
 119 static void poll_hpc (void);
 
 120 static int process_changeinstatus (struct slot *, struct slot *);
 
 121 static int process_changeinlatch (u8, u8, struct controller *);
 
 122 static int hpc_poll_thread (void *);
 
 123 static int hpc_wait_ctlr_notworking (int, struct controller *, void __iomem *, u8 *);
 
 124 //----------------------------------------------------------------------------
 
 127 /*----------------------------------------------------------------------
 
 128 * Name:    ibmphp_hpc_initvars
 
 130 * Action:  initialize semaphores and variables
 
 131 *---------------------------------------------------------------------*/
 
 132 void __init ibmphp_hpc_initvars (void)
 
 134         debug ("%s - Entry\n", __FUNCTION__);
 
 136         mutex_init(&sem_hpcaccess);
 
 137         init_MUTEX (&semOperations);
 
 138         init_MUTEX_LOCKED (&sem_exit);
 
 143         debug ("%s - Exit\n", __FUNCTION__);
 
 146 /*----------------------------------------------------------------------
 
 147 * Name:    i2c_ctrl_read
 
 149 * Action:  read from HPC over I2C
 
 151 *---------------------------------------------------------------------*/
 
 152 static u8 i2c_ctrl_read (struct controller *ctlr_ptr, void __iomem *WPGBbar, u8 index)
 
 156         void __iomem *wpg_addr; // base addr + offset
 
 157         unsigned long wpg_data; // data to/from WPG LOHI format
 
 158         unsigned long ultemp;
 
 159         unsigned long data;     // actual data HILO format
 
 161         debug_polling ("%s - Entry WPGBbar[%p] index[%x] \n", __FUNCTION__, WPGBbar, index);
 
 163         //--------------------------------------------------------------------
 
 165         // read at address, byte length, I2C address (shifted), index
 
 166         // or read direct, byte length, index
 
 167         if (ctlr_ptr->ctlr_type == 0x02) {
 
 168                 data = WPG_READATADDR_MASK;
 
 169                 // fill in I2C address
 
 170                 ultemp = (unsigned long)ctlr_ptr->u.wpeg_ctlr.i2c_addr;
 
 171                 ultemp = ultemp >> 1;
 
 172                 data |= (ultemp << 8);
 
 175                 data |= (unsigned long)index;
 
 176         } else if (ctlr_ptr->ctlr_type == 0x04) {
 
 177                 data = WPG_READDIRECT_MASK;
 
 180                 ultemp = (unsigned long)index;
 
 181                 ultemp = ultemp << 8;
 
 184                 err ("this controller type is not supported \n");
 
 188         wpg_data = swab32 (data);       // swap data before writing
 
 189         wpg_addr = WPGBbar + WPG_I2CMOSUP_OFFSET;
 
 190         writel (wpg_data, wpg_addr);
 
 192         //--------------------------------------------------------------------
 
 193         // READ - step 2 : clear the message buffer
 
 195         wpg_data = swab32 (data);
 
 196         wpg_addr = WPGBbar + WPG_I2CMBUFL_OFFSET;
 
 197         writel (wpg_data, wpg_addr);
 
 199         //--------------------------------------------------------------------
 
 200         // READ - step 3 : issue start operation, I2C master control bit 30:ON
 
 201         //                 2020 : [20] OR operation at [20] offset 0x20
 
 202         data = WPG_I2CMCNTL_STARTOP_MASK;
 
 203         wpg_data = swab32 (data);
 
 204         wpg_addr = WPGBbar + WPG_I2CMCNTL_OFFSET + WPG_I2C_OR;
 
 205         writel (wpg_data, wpg_addr);
 
 207         //--------------------------------------------------------------------
 
 208         // READ - step 4 : wait until start operation bit clears
 
 209         i = CMD_COMPLETE_TOUT_SEC;
 
 212                 wpg_addr = WPGBbar + WPG_I2CMCNTL_OFFSET;
 
 213                 wpg_data = readl (wpg_addr);
 
 214                 data = swab32 (wpg_data);
 
 215                 if (!(data & WPG_I2CMCNTL_STARTOP_MASK))
 
 220                 debug ("%s - Error : WPG timeout\n", __FUNCTION__);
 
 223         //--------------------------------------------------------------------
 
 224         // READ - step 5 : read I2C status register
 
 225         i = CMD_COMPLETE_TOUT_SEC;
 
 228                 wpg_addr = WPGBbar + WPG_I2CSTAT_OFFSET;
 
 229                 wpg_data = readl (wpg_addr);
 
 230                 data = swab32 (wpg_data);
 
 231                 if (HPC_I2CSTATUS_CHECK (data))
 
 236                 debug ("ctrl_read - Exit Error:I2C timeout\n");
 
 240         //--------------------------------------------------------------------
 
 241         // READ - step 6 : get DATA
 
 242         wpg_addr = WPGBbar + WPG_I2CMBUFL_OFFSET;
 
 243         wpg_data = readl (wpg_addr);
 
 244         data = swab32 (wpg_data);
 
 248         debug_polling ("%s - Exit index[%x] status[%x]\n", __FUNCTION__, index, status);
 
 253 /*----------------------------------------------------------------------
 
 254 * Name:    i2c_ctrl_write
 
 256 * Action:  write to HPC over I2C
 
 258 * Return   0 or error codes
 
 259 *---------------------------------------------------------------------*/
 
 260 static u8 i2c_ctrl_write (struct controller *ctlr_ptr, void __iomem *WPGBbar, u8 index, u8 cmd)
 
 263         void __iomem *wpg_addr; // base addr + offset
 
 264         unsigned long wpg_data; // data to/from WPG LOHI format 
 
 265         unsigned long ultemp;
 
 266         unsigned long data;     // actual data HILO format
 
 269         debug_polling ("%s - Entry WPGBbar[%p] index[%x] cmd[%x]\n", __FUNCTION__, WPGBbar, index, cmd);
 
 272         //--------------------------------------------------------------------
 
 274         // write at address, byte length, I2C address (shifted), index
 
 275         // or write direct, byte length, index
 
 278         if (ctlr_ptr->ctlr_type == 0x02) {
 
 279                 data = WPG_WRITEATADDR_MASK;
 
 280                 // fill in I2C address
 
 281                 ultemp = (unsigned long)ctlr_ptr->u.wpeg_ctlr.i2c_addr;
 
 282                 ultemp = ultemp >> 1;
 
 283                 data |= (ultemp << 8);
 
 286                 data |= (unsigned long)index;
 
 287         } else if (ctlr_ptr->ctlr_type == 0x04) {
 
 288                 data = WPG_WRITEDIRECT_MASK;
 
 291                 ultemp = (unsigned long)index;
 
 292                 ultemp = ultemp << 8;
 
 295                 err ("this controller type is not supported \n");
 
 299         wpg_data = swab32 (data);       // swap data before writing
 
 300         wpg_addr = WPGBbar + WPG_I2CMOSUP_OFFSET;
 
 301         writel (wpg_data, wpg_addr);
 
 303         //--------------------------------------------------------------------
 
 304         // WRITE - step 2 : clear the message buffer
 
 305         data = 0x00000000 | (unsigned long)cmd;
 
 306         wpg_data = swab32 (data);
 
 307         wpg_addr = WPGBbar + WPG_I2CMBUFL_OFFSET;
 
 308         writel (wpg_data, wpg_addr);
 
 310         //--------------------------------------------------------------------
 
 311         // WRITE - step 3 : issue start operation,I2C master control bit 30:ON
 
 312         //                 2020 : [20] OR operation at [20] offset 0x20
 
 313         data = WPG_I2CMCNTL_STARTOP_MASK;
 
 314         wpg_data = swab32 (data);
 
 315         wpg_addr = WPGBbar + WPG_I2CMCNTL_OFFSET + WPG_I2C_OR;
 
 316         writel (wpg_data, wpg_addr);
 
 318         //--------------------------------------------------------------------
 
 319         // WRITE - step 4 : wait until start operation bit clears
 
 320         i = CMD_COMPLETE_TOUT_SEC;
 
 323                 wpg_addr = WPGBbar + WPG_I2CMCNTL_OFFSET;
 
 324                 wpg_data = readl (wpg_addr);
 
 325                 data = swab32 (wpg_data);
 
 326                 if (!(data & WPG_I2CMCNTL_STARTOP_MASK))
 
 331                 debug ("%s - Exit Error:WPG timeout\n", __FUNCTION__);
 
 335         //--------------------------------------------------------------------
 
 336         // WRITE - step 5 : read I2C status register
 
 337         i = CMD_COMPLETE_TOUT_SEC;
 
 340                 wpg_addr = WPGBbar + WPG_I2CSTAT_OFFSET;
 
 341                 wpg_data = readl (wpg_addr);
 
 342                 data = swab32 (wpg_data);
 
 343                 if (HPC_I2CSTATUS_CHECK (data))
 
 348                 debug ("ctrl_read - Error : I2C timeout\n");
 
 352         debug_polling ("%s Exit rc[%x]\n", __FUNCTION__, rc);
 
 356 //------------------------------------------------------------
 
 357 //  Read from ISA type HPC 
 
 358 //------------------------------------------------------------
 
 359 static u8 isa_ctrl_read (struct controller *ctlr_ptr, u8 offset)
 
 365         start_address = ctlr_ptr->u.isa_ctlr.io_start;
 
 366         end_address = ctlr_ptr->u.isa_ctlr.io_end;
 
 367         data = inb (start_address + offset);
 
 371 //--------------------------------------------------------------
 
 372 // Write to ISA type HPC
 
 373 //--------------------------------------------------------------
 
 374 static void isa_ctrl_write (struct controller *ctlr_ptr, u8 offset, u8 data)
 
 379         start_address = ctlr_ptr->u.isa_ctlr.io_start;
 
 380         port_address = start_address + (u16) offset;
 
 381         outb (data, port_address);
 
 384 static u8 pci_ctrl_read (struct controller *ctrl, u8 offset)
 
 387         debug ("inside pci_ctrl_read\n");
 
 389                 pci_read_config_byte (ctrl->ctrl_dev, HPC_PCI_OFFSET + offset, &data);
 
 393 static u8 pci_ctrl_write (struct controller *ctrl, u8 offset, u8 data)
 
 396         debug ("inside pci_ctrl_write\n");
 
 397         if (ctrl->ctrl_dev) {
 
 398                 pci_write_config_byte (ctrl->ctrl_dev, HPC_PCI_OFFSET + offset, data);
 
 404 static u8 ctrl_read (struct controller *ctlr, void __iomem *base, u8 offset)
 
 407         switch (ctlr->ctlr_type) {
 
 409                 rc = isa_ctrl_read (ctlr, offset);
 
 412                 rc = pci_ctrl_read (ctlr, offset);
 
 416                 rc = i2c_ctrl_read (ctlr, base, offset);
 
 424 static u8 ctrl_write (struct controller *ctlr, void __iomem *base, u8 offset, u8 data)
 
 427         switch (ctlr->ctlr_type) {
 
 429                 isa_ctrl_write(ctlr, offset, data);
 
 432                 rc = pci_ctrl_write (ctlr, offset, data);
 
 436                 rc = i2c_ctrl_write(ctlr, base, offset, data);
 
 443 /*----------------------------------------------------------------------
 
 444 * Name:    hpc_writecmdtoindex()
 
 446 * Action:  convert a write command to proper index within a controller
 
 448 * Return   index, HPC_ERROR
 
 449 *---------------------------------------------------------------------*/
 
 450 static u8 hpc_writecmdtoindex (u8 cmd, u8 index)
 
 455         case HPC_CTLR_ENABLEIRQ:        // 0x00.N.15
 
 456         case HPC_CTLR_CLEARIRQ: // 0x06.N.15
 
 457         case HPC_CTLR_RESET:    // 0x07.N.15
 
 458         case HPC_CTLR_IRQSTEER: // 0x08.N.15
 
 459         case HPC_CTLR_DISABLEIRQ:       // 0x01.N.15
 
 460         case HPC_ALLSLOT_ON:    // 0x11.N.15
 
 461         case HPC_ALLSLOT_OFF:   // 0x12.N.15
 
 465         case HPC_SLOT_OFF:      // 0x02.Y.0-14
 
 466         case HPC_SLOT_ON:       // 0x03.Y.0-14
 
 467         case HPC_SLOT_ATTNOFF:  // 0x04.N.0-14
 
 468         case HPC_SLOT_ATTNON:   // 0x05.N.0-14
 
 469         case HPC_SLOT_BLINKLED: // 0x13.N.0-14
 
 473         case HPC_BUS_33CONVMODE:
 
 474         case HPC_BUS_66CONVMODE:
 
 475         case HPC_BUS_66PCIXMODE:
 
 476         case HPC_BUS_100PCIXMODE:
 
 477         case HPC_BUS_133PCIXMODE:
 
 478                 rc = index + WPG_1ST_BUS_INDEX - 1;
 
 482                 err ("hpc_writecmdtoindex - Error invalid cmd[%x]\n", cmd);
 
 489 /*----------------------------------------------------------------------
 
 490 * Name:    hpc_readcmdtoindex()
 
 492 * Action:  convert a read command to proper index within a controller
 
 494 * Return   index, HPC_ERROR
 
 495 *---------------------------------------------------------------------*/
 
 496 static u8 hpc_readcmdtoindex (u8 cmd, u8 index)
 
 501         case READ_CTLRSTATUS:
 
 504         case READ_SLOTSTATUS:
 
 508         case READ_EXTSLOTSTATUS:
 
 509                 rc = index + WPG_1ST_EXTSLOT_INDEX;
 
 512                 rc = index + WPG_1ST_BUS_INDEX - 1;
 
 514         case READ_SLOTLATCHLOWREG:
 
 520         case READ_HPCOPTIONS:
 
 529 /*----------------------------------------------------------------------
 
 530 * Name:    HPCreadslot()
 
 532 * Action:  issue a READ command to HPC
 
 534 * Input:   pslot   - cannot be NULL for READ_ALLSTAT
 
 535 *          pstatus - can be NULL for READ_ALLSTAT
 
 537 * Return   0 or error codes
 
 538 *---------------------------------------------------------------------*/
 
 539 int ibmphp_hpc_readslot (struct slot * pslot, u8 cmd, u8 * pstatus)
 
 541         void __iomem *wpg_bbar = NULL;
 
 542         struct controller *ctlr_ptr;
 
 543         struct list_head *pslotlist;
 
 548         debug_polling ("%s - Entry pslot[%p] cmd[%x] pstatus[%p]\n", __FUNCTION__, pslot, cmd, pstatus);
 
 551             || ((pstatus == NULL) && (cmd != READ_ALLSTAT) && (cmd != READ_BUSSTATUS))) {
 
 553                 err ("%s - Error invalid pointer, rc[%d]\n", __FUNCTION__, rc);
 
 557         if (cmd == READ_BUSSTATUS) {
 
 558                 busindex = ibmphp_get_bus_index (pslot->bus);
 
 561                         err ("%s - Exit Error:invalid bus, rc[%d]\n", __FUNCTION__, rc);
 
 564                         index = (u8) busindex;
 
 566                 index = pslot->ctlr_index;
 
 568         index = hpc_readcmdtoindex (cmd, index);
 
 570         if (index == HPC_ERROR) {
 
 572                 err ("%s - Exit Error:invalid index, rc[%d]\n", __FUNCTION__, rc);
 
 576         ctlr_ptr = pslot->ctrl;
 
 580         //--------------------------------------------------------------------
 
 581         // map physical address to logical address
 
 582         //--------------------------------------------------------------------
 
 583         if ((ctlr_ptr->ctlr_type == 2) || (ctlr_ptr->ctlr_type == 4))
 
 584                 wpg_bbar = ioremap (ctlr_ptr->u.wpeg_ctlr.wpegbbar, WPG_I2C_IOREMAP_SIZE);
 
 586         //--------------------------------------------------------------------
 
 587         // check controller status before reading
 
 588         //--------------------------------------------------------------------
 
 589         rc = hpc_wait_ctlr_notworking (HPC_CTLR_WORKING_TOUT, ctlr_ptr, wpg_bbar, &status);
 
 593                         // update the slot structure
 
 594                         pslot->ctrl->status = status;
 
 595                         pslot->status = ctrl_read (ctlr_ptr, wpg_bbar, index);
 
 596                         rc = hpc_wait_ctlr_notworking (HPC_CTLR_WORKING_TOUT, ctlr_ptr, wpg_bbar,
 
 599                                 pslot->ext_status = ctrl_read (ctlr_ptr, wpg_bbar, index + WPG_1ST_EXTSLOT_INDEX);
 
 603                 case READ_SLOTSTATUS:
 
 604                         // DO NOT update the slot structure
 
 605                         *pstatus = ctrl_read (ctlr_ptr, wpg_bbar, index);
 
 608                 case READ_EXTSLOTSTATUS:
 
 609                         // DO NOT update the slot structure
 
 610                         *pstatus = ctrl_read (ctlr_ptr, wpg_bbar, index);
 
 613                 case READ_CTLRSTATUS:
 
 614                         // DO NOT update the slot structure
 
 619                         pslot->busstatus = ctrl_read (ctlr_ptr, wpg_bbar, index);
 
 622                         *pstatus = ctrl_read (ctlr_ptr, wpg_bbar, index);
 
 624                 case READ_HPCOPTIONS:
 
 625                         *pstatus = ctrl_read (ctlr_ptr, wpg_bbar, index);
 
 627                 case READ_SLOTLATCHLOWREG:
 
 628                         // DO NOT update the slot structure
 
 629                         *pstatus = ctrl_read (ctlr_ptr, wpg_bbar, index);
 
 634                         list_for_each (pslotlist, &ibmphp_slot_head) {
 
 635                                 pslot = list_entry (pslotlist, struct slot, ibm_slot_list);
 
 636                                 index = pslot->ctlr_index;
 
 637                                 rc = hpc_wait_ctlr_notworking (HPC_CTLR_WORKING_TOUT, ctlr_ptr,
 
 640                                         pslot->status = ctrl_read (ctlr_ptr, wpg_bbar, index);
 
 641                                         rc = hpc_wait_ctlr_notworking (HPC_CTLR_WORKING_TOUT,
 
 642                                                                         ctlr_ptr, wpg_bbar, &status);
 
 645                                                     ctrl_read (ctlr_ptr, wpg_bbar,
 
 646                                                                 index + WPG_1ST_EXTSLOT_INDEX);
 
 648                                         err ("%s - Error ctrl_read failed\n", __FUNCTION__);
 
 659         //--------------------------------------------------------------------
 
 661         //--------------------------------------------------------------------
 
 663         // remove physical to logical address mapping
 
 664         if ((ctlr_ptr->ctlr_type == 2) || (ctlr_ptr->ctlr_type == 4))
 
 669         debug_polling ("%s - Exit rc[%d]\n", __FUNCTION__, rc);
 
 673 /*----------------------------------------------------------------------
 
 674 * Name:    ibmphp_hpc_writeslot()
 
 676 * Action: issue a WRITE command to HPC
 
 677 *---------------------------------------------------------------------*/
 
 678 int ibmphp_hpc_writeslot (struct slot * pslot, u8 cmd)
 
 680         void __iomem *wpg_bbar = NULL;
 
 681         struct controller *ctlr_ptr;
 
 688         debug_polling ("%s - Entry pslot[%p] cmd[%x]\n", __FUNCTION__, pslot, cmd);
 
 691                 err ("%s - Error Exit rc[%d]\n", __FUNCTION__, rc);
 
 695         if ((cmd == HPC_BUS_33CONVMODE) || (cmd == HPC_BUS_66CONVMODE) ||
 
 696                 (cmd == HPC_BUS_66PCIXMODE) || (cmd == HPC_BUS_100PCIXMODE) ||
 
 697                 (cmd == HPC_BUS_133PCIXMODE)) {
 
 698                 busindex = ibmphp_get_bus_index (pslot->bus);
 
 701                         err ("%s - Exit Error:invalid bus, rc[%d]\n", __FUNCTION__, rc);
 
 704                         index = (u8) busindex;
 
 706                 index = pslot->ctlr_index;
 
 708         index = hpc_writecmdtoindex (cmd, index);
 
 710         if (index == HPC_ERROR) {
 
 712                 err ("%s - Error Exit rc[%d]\n", __FUNCTION__, rc);
 
 716         ctlr_ptr = pslot->ctrl;
 
 720         //--------------------------------------------------------------------
 
 721         // map physical address to logical address
 
 722         //--------------------------------------------------------------------
 
 723         if ((ctlr_ptr->ctlr_type == 2) || (ctlr_ptr->ctlr_type == 4)) {
 
 724                 wpg_bbar = ioremap (ctlr_ptr->u.wpeg_ctlr.wpegbbar, WPG_I2C_IOREMAP_SIZE);
 
 726                 debug ("%s - ctlr id[%x] physical[%lx] logical[%lx] i2c[%x]\n", __FUNCTION__,
 
 727                 ctlr_ptr->ctlr_id, (ulong) (ctlr_ptr->u.wpeg_ctlr.wpegbbar), (ulong) wpg_bbar,
 
 728                 ctlr_ptr->u.wpeg_ctlr.i2c_addr);
 
 730         //--------------------------------------------------------------------
 
 731         // check controller status before writing
 
 732         //--------------------------------------------------------------------
 
 733         rc = hpc_wait_ctlr_notworking (HPC_CTLR_WORKING_TOUT, ctlr_ptr, wpg_bbar, &status);
 
 736                 ctrl_write (ctlr_ptr, wpg_bbar, index, cmd);
 
 738                 //--------------------------------------------------------------------
 
 739                 // check controller is still not working on the command
 
 740                 //--------------------------------------------------------------------
 
 741                 timeout = CMD_COMPLETE_TOUT_SEC;
 
 744                         rc = hpc_wait_ctlr_notworking (HPC_CTLR_WORKING_TOUT, ctlr_ptr, wpg_bbar,
 
 747                                 if (NEEDTOCHECK_CMDSTATUS (cmd)) {
 
 748                                         if (CTLR_FINISHED (status) == HPC_CTLR_FINISHED_YES)
 
 757                                         err ("%s - Error command complete timeout\n", __FUNCTION__);
 
 763                 ctlr_ptr->status = status;
 
 767         // remove physical to logical address mapping
 
 768         if ((ctlr_ptr->ctlr_type == 2) || (ctlr_ptr->ctlr_type == 4))
 
 772         debug_polling ("%s - Exit rc[%d]\n", __FUNCTION__, rc);
 
 776 /*----------------------------------------------------------------------
 
 777 * Name:    get_hpc_access()
 
 779 * Action: make sure only one process can access HPC at one time
 
 780 *---------------------------------------------------------------------*/
 
 781 static void get_hpc_access (void)
 
 783         mutex_lock(&sem_hpcaccess);
 
 786 /*----------------------------------------------------------------------
 
 787 * Name:    free_hpc_access()
 
 788 *---------------------------------------------------------------------*/
 
 789 void free_hpc_access (void)
 
 791         mutex_unlock(&sem_hpcaccess);
 
 794 /*----------------------------------------------------------------------
 
 795 * Name:    ibmphp_lock_operations()
 
 797 * Action: make sure only one process can change the data structure
 
 798 *---------------------------------------------------------------------*/
 
 799 void ibmphp_lock_operations (void)
 
 801         down (&semOperations);
 
 805 /*----------------------------------------------------------------------
 
 806 * Name:    ibmphp_unlock_operations()
 
 807 *---------------------------------------------------------------------*/
 
 808 void ibmphp_unlock_operations (void)
 
 810         debug ("%s - Entry\n", __FUNCTION__);
 
 813         debug ("%s - Exit\n", __FUNCTION__);
 
 816 /*----------------------------------------------------------------------
 
 818 *---------------------------------------------------------------------*/
 
 819 #define POLL_LATCH_REGISTER     0
 
 822 static void poll_hpc (void)
 
 825         struct slot *pslot = NULL;
 
 826         struct list_head *pslotlist;
 
 828         int poll_state = POLL_LATCH_REGISTER;
 
 829         u8 oldlatchlow = 0x00;
 
 830         u8 curlatchlow = 0x00;
 
 832         u8 ctrl_count = 0x00;
 
 834         debug ("%s - Entry\n", __FUNCTION__);
 
 836         while (!ibmphp_shutdown) {
 
 840                 /* try to get the lock to do some kind of hardware access */
 
 841                 down (&semOperations);
 
 843                 switch (poll_state) {
 
 844                 case POLL_LATCH_REGISTER: 
 
 845                         oldlatchlow = curlatchlow;
 
 847                         list_for_each (pslotlist, &ibmphp_slot_head) {
 
 848                                 if (ctrl_count >= ibmphp_get_total_controllers())
 
 850                                 pslot = list_entry (pslotlist, struct slot, ibm_slot_list);
 
 851                                 if (pslot->ctrl->ctlr_relative_id == ctrl_count) {
 
 853                                         if (READ_SLOT_LATCH (pslot->ctrl)) {
 
 854                                                 rc = ibmphp_hpc_readslot (pslot,
 
 855                                                                           READ_SLOTLATCHLOWREG,
 
 857                                                 if (oldlatchlow != curlatchlow)
 
 858                                                         process_changeinlatch (oldlatchlow,
 
 865                         poll_state = POLL_SLEEP;
 
 868                         list_for_each (pslotlist, &ibmphp_slot_head) {
 
 869                                 pslot = list_entry (pslotlist, struct slot, ibm_slot_list);
 
 870                                 // make a copy of the old status
 
 871                                 memcpy ((void *) &myslot, (void *) pslot,
 
 872                                         sizeof (struct slot));
 
 873                                 rc = ibmphp_hpc_readslot (pslot, READ_ALLSTAT, NULL);
 
 874                                 if ((myslot.status != pslot->status)
 
 875                                     || (myslot.ext_status != pslot->ext_status))
 
 876                                         process_changeinstatus (pslot, &myslot);
 
 879                         list_for_each (pslotlist, &ibmphp_slot_head) {
 
 880                                 if (ctrl_count >= ibmphp_get_total_controllers())
 
 882                                 pslot = list_entry (pslotlist, struct slot, ibm_slot_list);
 
 883                                 if (pslot->ctrl->ctlr_relative_id == ctrl_count) {
 
 885                                         if (READ_SLOT_LATCH (pslot->ctrl))
 
 886                                                 rc = ibmphp_hpc_readslot (pslot,
 
 887                                                                           READ_SLOTLATCHLOWREG,
 
 892                         poll_state = POLL_SLEEP;
 
 895                         /* don't sleep with a lock on the hardware */
 
 897                         msleep(POLL_INTERVAL_SEC * 1000);
 
 902                         down (&semOperations);
 
 904                         if (poll_count >= POLL_LATCH_CNT) {
 
 906                                 poll_state = POLL_SLOTS;
 
 908                                 poll_state = POLL_LATCH_REGISTER;
 
 911                 /* give up the hardware semaphore */
 
 913                 /* sleep for a short time just for good measure */
 
 917         debug ("%s - Exit\n", __FUNCTION__);
 
 921 /*----------------------------------------------------------------------
 
 922 * Name:    process_changeinstatus
 
 924 * Action:  compare old and new slot status, process the change in status
 
 926 * Input:   pointer to slot struct, old slot struct
 
 928 * Return   0 or error codes
 
 935 *---------------------------------------------------------------------*/
 
 936 static int process_changeinstatus (struct slot *pslot, struct slot *poldslot)
 
 943         debug ("process_changeinstatus - Entry pslot[%p], poldslot[%p]\n", pslot, poldslot);
 
 945         // bit 0 - HPC_SLOT_POWER
 
 946         if ((pslot->status & 0x01) != (poldslot->status & 0x01))
 
 949         // bit 1 - HPC_SLOT_CONNECT
 
 952         // bit 2 - HPC_SLOT_ATTN
 
 953         if ((pslot->status & 0x04) != (poldslot->status & 0x04))
 
 956         // bit 3 - HPC_SLOT_PRSNT2
 
 957         // bit 4 - HPC_SLOT_PRSNT1
 
 958         if (((pslot->status & 0x08) != (poldslot->status & 0x08))
 
 959                 || ((pslot->status & 0x10) != (poldslot->status & 0x10)))
 
 962         // bit 5 - HPC_SLOT_PWRGD
 
 963         if ((pslot->status & 0x20) != (poldslot->status & 0x20))
 
 964                 // OFF -> ON: ignore, ON -> OFF: disable slot
 
 965                 if ((poldslot->status & 0x20) && (SLOT_CONNECT (poldslot->status) == HPC_SLOT_CONNECTED) && (SLOT_PRESENT (poldslot->status))) 
 
 968         // bit 6 - HPC_SLOT_BUS_SPEED
 
 971         // bit 7 - HPC_SLOT_LATCH
 
 972         if ((pslot->status & 0x80) != (poldslot->status & 0x80)) {
 
 975                 if (pslot->status & 0x80) {
 
 976                         if (SLOT_PWRGD (pslot->status)) {
 
 977                                 // power goes on and off after closing latch
 
 978                                 // check again to make sure power is still ON
 
 980                                 rc = ibmphp_hpc_readslot (pslot, READ_SLOTSTATUS, &status);
 
 981                                 if (SLOT_PWRGD (status))
 
 983                                 else    // overwrite power in pslot to OFF
 
 984                                         pslot->status &= ~HPC_SLOT_POWER;
 
 988                 else if ((SLOT_PWRGD (poldslot->status) == HPC_SLOT_PWRGD_GOOD)
 
 989                         && (SLOT_CONNECT (poldslot->status) == HPC_SLOT_CONNECTED) && (SLOT_PRESENT (poldslot->status))) {
 
 994         // bit 4 - HPC_SLOT_BLINK_ATTN
 
 995         if ((pslot->ext_status & 0x08) != (poldslot->ext_status & 0x08))
 
 999                 debug ("process_changeinstatus - disable slot\n");
 
1001                 rc = ibmphp_do_disable_slot (pslot);
 
1004         if (update || disable) {
 
1005                 ibmphp_update_slot_info (pslot);
 
1008         debug ("%s - Exit rc[%d] disable[%x] update[%x]\n", __FUNCTION__, rc, disable, update);
 
1013 /*----------------------------------------------------------------------
 
1014 * Name:    process_changeinlatch
 
1016 * Action:  compare old and new latch reg status, process the change
 
1018 * Input:   old and current latch register status
 
1020 * Return   0 or error codes
 
1022 *---------------------------------------------------------------------*/
 
1023 static int process_changeinlatch (u8 old, u8 new, struct controller *ctrl)
 
1025         struct slot myslot, *pslot;
 
1030         debug ("%s - Entry old[%x], new[%x]\n", __FUNCTION__, old, new);
 
1031         // bit 0 reserved, 0 is LSB, check bit 1-6 for 6 slots
 
1033         for (i = ctrl->starting_slot_num; i <= ctrl->ending_slot_num; i++) {
 
1035                 if ((mask & old) != (mask & new)) {
 
1036                         pslot = ibmphp_get_slot_from_physical_num (i);
 
1038                                 memcpy ((void *) &myslot, (void *) pslot, sizeof (struct slot));
 
1039                                 rc = ibmphp_hpc_readslot (pslot, READ_ALLSTAT, NULL);
 
1040                                 debug ("%s - call process_changeinstatus for slot[%d]\n", __FUNCTION__, i);
 
1041                                 process_changeinstatus (pslot, &myslot);
 
1044                                 err ("%s - Error bad pointer for slot[%d]\n", __FUNCTION__, i);
 
1048         debug ("%s - Exit rc[%d]\n", __FUNCTION__, rc);
 
1052 /*----------------------------------------------------------------------
 
1053 * Name:    hpc_poll_thread
 
1059 *---------------------------------------------------------------------*/
 
1060 static int hpc_poll_thread (void *data)
 
1062         debug ("%s - Entry\n", __FUNCTION__);
 
1064         daemonize("hpc_poll");
 
1065         allow_signal(SIGKILL);
 
1070         debug ("%s - Exit\n", __FUNCTION__);
 
1075 /*----------------------------------------------------------------------
 
1076 * Name:    ibmphp_hpc_start_poll_thread
 
1078 * Action:  start polling thread
 
1079 *---------------------------------------------------------------------*/
 
1080 int __init ibmphp_hpc_start_poll_thread (void)
 
1084         debug ("%s - Entry\n", __FUNCTION__);
 
1086         tid_poll = kernel_thread (hpc_poll_thread, NULL, 0);
 
1088                 err ("%s - Error, thread not started\n", __FUNCTION__);
 
1092         debug ("%s - Exit tid_poll[%d] rc[%d]\n", __FUNCTION__, tid_poll, rc);
 
1096 /*----------------------------------------------------------------------
 
1097 * Name:    ibmphp_hpc_stop_poll_thread
 
1099 * Action:  stop polling thread and cleanup
 
1100 *---------------------------------------------------------------------*/
 
1101 void __exit ibmphp_hpc_stop_poll_thread (void)
 
1103         debug ("%s - Entry\n", __FUNCTION__);
 
1105         ibmphp_shutdown = 1;
 
1106         debug ("before locking operations \n");
 
1107         ibmphp_lock_operations ();
 
1108         debug ("after locking operations \n");
 
1110         // wait for poll thread to exit
 
1111         debug ("before sem_exit down \n");
 
1113         debug ("after sem_exit down \n");
 
1116         debug ("before free_hpc_access \n");
 
1118         debug ("after free_hpc_access \n");
 
1119         ibmphp_unlock_operations ();
 
1120         debug ("after unlock operations \n");
 
1122         debug ("after sem exit up\n");
 
1124         debug ("%s - Exit\n", __FUNCTION__);
 
1127 /*----------------------------------------------------------------------
 
1128 * Name:    hpc_wait_ctlr_notworking
 
1130 * Action:  wait until the controller is in a not working state
 
1132 * Return   0, HPC_ERROR
 
1134 *---------------------------------------------------------------------*/
 
1135 static int hpc_wait_ctlr_notworking (int timeout, struct controller *ctlr_ptr, void __iomem *wpg_bbar,
 
1141         debug_polling ("hpc_wait_ctlr_notworking - Entry timeout[%d]\n", timeout);
 
1144                 *pstatus = ctrl_read (ctlr_ptr, wpg_bbar, WPG_CTLR_INDEX);
 
1145                 if (*pstatus == HPC_ERROR) {
 
1149                 if (CTLR_WORKING (*pstatus) == HPC_CTLR_WORKING_NO)
 
1155                                 err ("HPCreadslot - Error ctlr timeout\n");
 
1161         debug_polling ("hpc_wait_ctlr_notworking - Exit rc[%x] status[%x]\n", rc, *pstatus);