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/init.h>
36 #include <linux/mutex.h>
37 #include <linux/sched.h>
38 #include <linux/kthread.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 struct mutex sem_hpcaccess; // lock access to HPC
105 static struct semaphore semOperations; // lock all operations and
106 // access to data structures
107 static struct semaphore sem_exit; // make sure polling thread goes away
108 static struct task_struct *ibmphp_poll_thread;
109 //----------------------------------------------------------------------------
110 // local function prototypes
111 //----------------------------------------------------------------------------
112 static u8 i2c_ctrl_read (struct controller *, void __iomem *, u8);
113 static u8 i2c_ctrl_write (struct controller *, void __iomem *, u8, u8);
114 static u8 hpc_writecmdtoindex (u8, u8);
115 static u8 hpc_readcmdtoindex (u8, u8);
116 static void get_hpc_access (void);
117 static void free_hpc_access (void);
118 static int poll_hpc(void *data);
119 static int process_changeinstatus (struct slot *, struct slot *);
120 static int process_changeinlatch (u8, u8, struct controller *);
121 static int hpc_wait_ctlr_notworking (int, struct controller *, void __iomem *, u8 *);
122 //----------------------------------------------------------------------------
125 /*----------------------------------------------------------------------
126 * Name: ibmphp_hpc_initvars
128 * Action: initialize semaphores and variables
129 *---------------------------------------------------------------------*/
130 void __init ibmphp_hpc_initvars (void)
132 debug ("%s - Entry\n", __func__);
134 mutex_init(&sem_hpcaccess);
135 init_MUTEX (&semOperations);
136 init_MUTEX_LOCKED (&sem_exit);
139 debug ("%s - Exit\n", __func__);
142 /*----------------------------------------------------------------------
143 * Name: i2c_ctrl_read
145 * Action: read from HPC over I2C
147 *---------------------------------------------------------------------*/
148 static u8 i2c_ctrl_read (struct controller *ctlr_ptr, void __iomem *WPGBbar, u8 index)
152 void __iomem *wpg_addr; // base addr + offset
153 unsigned long wpg_data; // data to/from WPG LOHI format
154 unsigned long ultemp;
155 unsigned long data; // actual data HILO format
157 debug_polling ("%s - Entry WPGBbar[%p] index[%x] \n", __func__, WPGBbar, index);
159 //--------------------------------------------------------------------
161 // read at address, byte length, I2C address (shifted), index
162 // or read direct, byte length, index
163 if (ctlr_ptr->ctlr_type == 0x02) {
164 data = WPG_READATADDR_MASK;
165 // fill in I2C address
166 ultemp = (unsigned long)ctlr_ptr->u.wpeg_ctlr.i2c_addr;
167 ultemp = ultemp >> 1;
168 data |= (ultemp << 8);
171 data |= (unsigned long)index;
172 } else if (ctlr_ptr->ctlr_type == 0x04) {
173 data = WPG_READDIRECT_MASK;
176 ultemp = (unsigned long)index;
177 ultemp = ultemp << 8;
180 err ("this controller type is not supported \n");
184 wpg_data = swab32 (data); // swap data before writing
185 wpg_addr = WPGBbar + WPG_I2CMOSUP_OFFSET;
186 writel (wpg_data, wpg_addr);
188 //--------------------------------------------------------------------
189 // READ - step 2 : clear the message buffer
191 wpg_data = swab32 (data);
192 wpg_addr = WPGBbar + WPG_I2CMBUFL_OFFSET;
193 writel (wpg_data, wpg_addr);
195 //--------------------------------------------------------------------
196 // READ - step 3 : issue start operation, I2C master control bit 30:ON
197 // 2020 : [20] OR operation at [20] offset 0x20
198 data = WPG_I2CMCNTL_STARTOP_MASK;
199 wpg_data = swab32 (data);
200 wpg_addr = WPGBbar + WPG_I2CMCNTL_OFFSET + WPG_I2C_OR;
201 writel (wpg_data, wpg_addr);
203 //--------------------------------------------------------------------
204 // READ - step 4 : wait until start operation bit clears
205 i = CMD_COMPLETE_TOUT_SEC;
208 wpg_addr = WPGBbar + WPG_I2CMCNTL_OFFSET;
209 wpg_data = readl (wpg_addr);
210 data = swab32 (wpg_data);
211 if (!(data & WPG_I2CMCNTL_STARTOP_MASK))
216 debug ("%s - Error : WPG timeout\n", __func__);
219 //--------------------------------------------------------------------
220 // READ - step 5 : read I2C status register
221 i = CMD_COMPLETE_TOUT_SEC;
224 wpg_addr = WPGBbar + WPG_I2CSTAT_OFFSET;
225 wpg_data = readl (wpg_addr);
226 data = swab32 (wpg_data);
227 if (HPC_I2CSTATUS_CHECK (data))
232 debug ("ctrl_read - Exit Error:I2C timeout\n");
236 //--------------------------------------------------------------------
237 // READ - step 6 : get DATA
238 wpg_addr = WPGBbar + WPG_I2CMBUFL_OFFSET;
239 wpg_data = readl (wpg_addr);
240 data = swab32 (wpg_data);
244 debug_polling ("%s - Exit index[%x] status[%x]\n", __func__, index, status);
249 /*----------------------------------------------------------------------
250 * Name: i2c_ctrl_write
252 * Action: write to HPC over I2C
254 * Return 0 or error codes
255 *---------------------------------------------------------------------*/
256 static u8 i2c_ctrl_write (struct controller *ctlr_ptr, void __iomem *WPGBbar, u8 index, u8 cmd)
259 void __iomem *wpg_addr; // base addr + offset
260 unsigned long wpg_data; // data to/from WPG LOHI format
261 unsigned long ultemp;
262 unsigned long data; // actual data HILO format
265 debug_polling ("%s - Entry WPGBbar[%p] index[%x] cmd[%x]\n", __func__, WPGBbar, index, cmd);
268 //--------------------------------------------------------------------
270 // write at address, byte length, I2C address (shifted), index
271 // or write direct, byte length, index
274 if (ctlr_ptr->ctlr_type == 0x02) {
275 data = WPG_WRITEATADDR_MASK;
276 // fill in I2C address
277 ultemp = (unsigned long)ctlr_ptr->u.wpeg_ctlr.i2c_addr;
278 ultemp = ultemp >> 1;
279 data |= (ultemp << 8);
282 data |= (unsigned long)index;
283 } else if (ctlr_ptr->ctlr_type == 0x04) {
284 data = WPG_WRITEDIRECT_MASK;
287 ultemp = (unsigned long)index;
288 ultemp = ultemp << 8;
291 err ("this controller type is not supported \n");
295 wpg_data = swab32 (data); // swap data before writing
296 wpg_addr = WPGBbar + WPG_I2CMOSUP_OFFSET;
297 writel (wpg_data, wpg_addr);
299 //--------------------------------------------------------------------
300 // WRITE - step 2 : clear the message buffer
301 data = 0x00000000 | (unsigned long)cmd;
302 wpg_data = swab32 (data);
303 wpg_addr = WPGBbar + WPG_I2CMBUFL_OFFSET;
304 writel (wpg_data, wpg_addr);
306 //--------------------------------------------------------------------
307 // WRITE - step 3 : issue start operation,I2C master control bit 30:ON
308 // 2020 : [20] OR operation at [20] offset 0x20
309 data = WPG_I2CMCNTL_STARTOP_MASK;
310 wpg_data = swab32 (data);
311 wpg_addr = WPGBbar + WPG_I2CMCNTL_OFFSET + WPG_I2C_OR;
312 writel (wpg_data, wpg_addr);
314 //--------------------------------------------------------------------
315 // WRITE - step 4 : wait until start operation bit clears
316 i = CMD_COMPLETE_TOUT_SEC;
319 wpg_addr = WPGBbar + WPG_I2CMCNTL_OFFSET;
320 wpg_data = readl (wpg_addr);
321 data = swab32 (wpg_data);
322 if (!(data & WPG_I2CMCNTL_STARTOP_MASK))
327 debug ("%s - Exit Error:WPG timeout\n", __func__);
331 //--------------------------------------------------------------------
332 // WRITE - step 5 : read I2C status register
333 i = CMD_COMPLETE_TOUT_SEC;
336 wpg_addr = WPGBbar + WPG_I2CSTAT_OFFSET;
337 wpg_data = readl (wpg_addr);
338 data = swab32 (wpg_data);
339 if (HPC_I2CSTATUS_CHECK (data))
344 debug ("ctrl_read - Error : I2C timeout\n");
348 debug_polling ("%s Exit rc[%x]\n", __func__, rc);
352 //------------------------------------------------------------
353 // Read from ISA type HPC
354 //------------------------------------------------------------
355 static u8 isa_ctrl_read (struct controller *ctlr_ptr, u8 offset)
361 start_address = ctlr_ptr->u.isa_ctlr.io_start;
362 end_address = ctlr_ptr->u.isa_ctlr.io_end;
363 data = inb (start_address + offset);
367 //--------------------------------------------------------------
368 // Write to ISA type HPC
369 //--------------------------------------------------------------
370 static void isa_ctrl_write (struct controller *ctlr_ptr, u8 offset, u8 data)
375 start_address = ctlr_ptr->u.isa_ctlr.io_start;
376 port_address = start_address + (u16) offset;
377 outb (data, port_address);
380 static u8 pci_ctrl_read (struct controller *ctrl, u8 offset)
383 debug ("inside pci_ctrl_read\n");
385 pci_read_config_byte (ctrl->ctrl_dev, HPC_PCI_OFFSET + offset, &data);
389 static u8 pci_ctrl_write (struct controller *ctrl, u8 offset, u8 data)
392 debug ("inside pci_ctrl_write\n");
393 if (ctrl->ctrl_dev) {
394 pci_write_config_byte (ctrl->ctrl_dev, HPC_PCI_OFFSET + offset, data);
400 static u8 ctrl_read (struct controller *ctlr, void __iomem *base, u8 offset)
403 switch (ctlr->ctlr_type) {
405 rc = isa_ctrl_read (ctlr, offset);
408 rc = pci_ctrl_read (ctlr, offset);
412 rc = i2c_ctrl_read (ctlr, base, offset);
420 static u8 ctrl_write (struct controller *ctlr, void __iomem *base, u8 offset, u8 data)
423 switch (ctlr->ctlr_type) {
425 isa_ctrl_write(ctlr, offset, data);
428 rc = pci_ctrl_write (ctlr, offset, data);
432 rc = i2c_ctrl_write(ctlr, base, offset, data);
439 /*----------------------------------------------------------------------
440 * Name: hpc_writecmdtoindex()
442 * Action: convert a write command to proper index within a controller
444 * Return index, HPC_ERROR
445 *---------------------------------------------------------------------*/
446 static u8 hpc_writecmdtoindex (u8 cmd, u8 index)
451 case HPC_CTLR_ENABLEIRQ: // 0x00.N.15
452 case HPC_CTLR_CLEARIRQ: // 0x06.N.15
453 case HPC_CTLR_RESET: // 0x07.N.15
454 case HPC_CTLR_IRQSTEER: // 0x08.N.15
455 case HPC_CTLR_DISABLEIRQ: // 0x01.N.15
456 case HPC_ALLSLOT_ON: // 0x11.N.15
457 case HPC_ALLSLOT_OFF: // 0x12.N.15
461 case HPC_SLOT_OFF: // 0x02.Y.0-14
462 case HPC_SLOT_ON: // 0x03.Y.0-14
463 case HPC_SLOT_ATTNOFF: // 0x04.N.0-14
464 case HPC_SLOT_ATTNON: // 0x05.N.0-14
465 case HPC_SLOT_BLINKLED: // 0x13.N.0-14
469 case HPC_BUS_33CONVMODE:
470 case HPC_BUS_66CONVMODE:
471 case HPC_BUS_66PCIXMODE:
472 case HPC_BUS_100PCIXMODE:
473 case HPC_BUS_133PCIXMODE:
474 rc = index + WPG_1ST_BUS_INDEX - 1;
478 err ("hpc_writecmdtoindex - Error invalid cmd[%x]\n", cmd);
485 /*----------------------------------------------------------------------
486 * Name: hpc_readcmdtoindex()
488 * Action: convert a read command to proper index within a controller
490 * Return index, HPC_ERROR
491 *---------------------------------------------------------------------*/
492 static u8 hpc_readcmdtoindex (u8 cmd, u8 index)
497 case READ_CTLRSTATUS:
500 case READ_SLOTSTATUS:
504 case READ_EXTSLOTSTATUS:
505 rc = index + WPG_1ST_EXTSLOT_INDEX;
508 rc = index + WPG_1ST_BUS_INDEX - 1;
510 case READ_SLOTLATCHLOWREG:
516 case READ_HPCOPTIONS:
525 /*----------------------------------------------------------------------
526 * Name: HPCreadslot()
528 * Action: issue a READ command to HPC
530 * Input: pslot - cannot be NULL for READ_ALLSTAT
531 * pstatus - can be NULL for READ_ALLSTAT
533 * Return 0 or error codes
534 *---------------------------------------------------------------------*/
535 int ibmphp_hpc_readslot (struct slot * pslot, u8 cmd, u8 * pstatus)
537 void __iomem *wpg_bbar = NULL;
538 struct controller *ctlr_ptr;
539 struct list_head *pslotlist;
544 debug_polling ("%s - Entry pslot[%p] cmd[%x] pstatus[%p]\n", __func__, pslot, cmd, pstatus);
547 || ((pstatus == NULL) && (cmd != READ_ALLSTAT) && (cmd != READ_BUSSTATUS))) {
549 err ("%s - Error invalid pointer, rc[%d]\n", __func__, rc);
553 if (cmd == READ_BUSSTATUS) {
554 busindex = ibmphp_get_bus_index (pslot->bus);
557 err ("%s - Exit Error:invalid bus, rc[%d]\n", __func__, rc);
560 index = (u8) busindex;
562 index = pslot->ctlr_index;
564 index = hpc_readcmdtoindex (cmd, index);
566 if (index == HPC_ERROR) {
568 err ("%s - Exit Error:invalid index, rc[%d]\n", __func__, rc);
572 ctlr_ptr = pslot->ctrl;
576 //--------------------------------------------------------------------
577 // map physical address to logical address
578 //--------------------------------------------------------------------
579 if ((ctlr_ptr->ctlr_type == 2) || (ctlr_ptr->ctlr_type == 4))
580 wpg_bbar = ioremap (ctlr_ptr->u.wpeg_ctlr.wpegbbar, WPG_I2C_IOREMAP_SIZE);
582 //--------------------------------------------------------------------
583 // check controller status before reading
584 //--------------------------------------------------------------------
585 rc = hpc_wait_ctlr_notworking (HPC_CTLR_WORKING_TOUT, ctlr_ptr, wpg_bbar, &status);
589 // update the slot structure
590 pslot->ctrl->status = status;
591 pslot->status = ctrl_read (ctlr_ptr, wpg_bbar, index);
592 rc = hpc_wait_ctlr_notworking (HPC_CTLR_WORKING_TOUT, ctlr_ptr, wpg_bbar,
595 pslot->ext_status = ctrl_read (ctlr_ptr, wpg_bbar, index + WPG_1ST_EXTSLOT_INDEX);
599 case READ_SLOTSTATUS:
600 // DO NOT update the slot structure
601 *pstatus = ctrl_read (ctlr_ptr, wpg_bbar, index);
604 case READ_EXTSLOTSTATUS:
605 // DO NOT update the slot structure
606 *pstatus = ctrl_read (ctlr_ptr, wpg_bbar, index);
609 case READ_CTLRSTATUS:
610 // DO NOT update the slot structure
615 pslot->busstatus = ctrl_read (ctlr_ptr, wpg_bbar, index);
618 *pstatus = ctrl_read (ctlr_ptr, wpg_bbar, index);
620 case READ_HPCOPTIONS:
621 *pstatus = ctrl_read (ctlr_ptr, wpg_bbar, index);
623 case READ_SLOTLATCHLOWREG:
624 // DO NOT update the slot structure
625 *pstatus = ctrl_read (ctlr_ptr, wpg_bbar, index);
630 list_for_each (pslotlist, &ibmphp_slot_head) {
631 pslot = list_entry (pslotlist, struct slot, ibm_slot_list);
632 index = pslot->ctlr_index;
633 rc = hpc_wait_ctlr_notworking (HPC_CTLR_WORKING_TOUT, ctlr_ptr,
636 pslot->status = ctrl_read (ctlr_ptr, wpg_bbar, index);
637 rc = hpc_wait_ctlr_notworking (HPC_CTLR_WORKING_TOUT,
638 ctlr_ptr, wpg_bbar, &status);
641 ctrl_read (ctlr_ptr, wpg_bbar,
642 index + WPG_1ST_EXTSLOT_INDEX);
644 err ("%s - Error ctrl_read failed\n", __func__);
655 //--------------------------------------------------------------------
657 //--------------------------------------------------------------------
659 // remove physical to logical address mapping
660 if ((ctlr_ptr->ctlr_type == 2) || (ctlr_ptr->ctlr_type == 4))
665 debug_polling ("%s - Exit rc[%d]\n", __func__, rc);
669 /*----------------------------------------------------------------------
670 * Name: ibmphp_hpc_writeslot()
672 * Action: issue a WRITE command to HPC
673 *---------------------------------------------------------------------*/
674 int ibmphp_hpc_writeslot (struct slot * pslot, u8 cmd)
676 void __iomem *wpg_bbar = NULL;
677 struct controller *ctlr_ptr;
684 debug_polling ("%s - Entry pslot[%p] cmd[%x]\n", __func__, pslot, cmd);
687 err ("%s - Error Exit rc[%d]\n", __func__, rc);
691 if ((cmd == HPC_BUS_33CONVMODE) || (cmd == HPC_BUS_66CONVMODE) ||
692 (cmd == HPC_BUS_66PCIXMODE) || (cmd == HPC_BUS_100PCIXMODE) ||
693 (cmd == HPC_BUS_133PCIXMODE)) {
694 busindex = ibmphp_get_bus_index (pslot->bus);
697 err ("%s - Exit Error:invalid bus, rc[%d]\n", __func__, rc);
700 index = (u8) busindex;
702 index = pslot->ctlr_index;
704 index = hpc_writecmdtoindex (cmd, index);
706 if (index == HPC_ERROR) {
708 err ("%s - Error Exit rc[%d]\n", __func__, rc);
712 ctlr_ptr = pslot->ctrl;
716 //--------------------------------------------------------------------
717 // map physical address to logical address
718 //--------------------------------------------------------------------
719 if ((ctlr_ptr->ctlr_type == 2) || (ctlr_ptr->ctlr_type == 4)) {
720 wpg_bbar = ioremap (ctlr_ptr->u.wpeg_ctlr.wpegbbar, WPG_I2C_IOREMAP_SIZE);
722 debug ("%s - ctlr id[%x] physical[%lx] logical[%lx] i2c[%x]\n", __func__,
723 ctlr_ptr->ctlr_id, (ulong) (ctlr_ptr->u.wpeg_ctlr.wpegbbar), (ulong) wpg_bbar,
724 ctlr_ptr->u.wpeg_ctlr.i2c_addr);
726 //--------------------------------------------------------------------
727 // check controller status before writing
728 //--------------------------------------------------------------------
729 rc = hpc_wait_ctlr_notworking (HPC_CTLR_WORKING_TOUT, ctlr_ptr, wpg_bbar, &status);
732 ctrl_write (ctlr_ptr, wpg_bbar, index, cmd);
734 //--------------------------------------------------------------------
735 // check controller is still not working on the command
736 //--------------------------------------------------------------------
737 timeout = CMD_COMPLETE_TOUT_SEC;
740 rc = hpc_wait_ctlr_notworking (HPC_CTLR_WORKING_TOUT, ctlr_ptr, wpg_bbar,
743 if (NEEDTOCHECK_CMDSTATUS (cmd)) {
744 if (CTLR_FINISHED (status) == HPC_CTLR_FINISHED_YES)
753 err ("%s - Error command complete timeout\n", __func__);
759 ctlr_ptr->status = status;
763 // remove physical to logical address mapping
764 if ((ctlr_ptr->ctlr_type == 2) || (ctlr_ptr->ctlr_type == 4))
768 debug_polling ("%s - Exit rc[%d]\n", __func__, rc);
772 /*----------------------------------------------------------------------
773 * Name: get_hpc_access()
775 * Action: make sure only one process can access HPC at one time
776 *---------------------------------------------------------------------*/
777 static void get_hpc_access (void)
779 mutex_lock(&sem_hpcaccess);
782 /*----------------------------------------------------------------------
783 * Name: free_hpc_access()
784 *---------------------------------------------------------------------*/
785 void free_hpc_access (void)
787 mutex_unlock(&sem_hpcaccess);
790 /*----------------------------------------------------------------------
791 * Name: ibmphp_lock_operations()
793 * Action: make sure only one process can change the data structure
794 *---------------------------------------------------------------------*/
795 void ibmphp_lock_operations (void)
797 down (&semOperations);
801 /*----------------------------------------------------------------------
802 * Name: ibmphp_unlock_operations()
803 *---------------------------------------------------------------------*/
804 void ibmphp_unlock_operations (void)
806 debug ("%s - Entry\n", __func__);
809 debug ("%s - Exit\n", __func__);
812 /*----------------------------------------------------------------------
814 *---------------------------------------------------------------------*/
815 #define POLL_LATCH_REGISTER 0
818 static int poll_hpc(void *data)
821 struct slot *pslot = NULL;
822 struct list_head *pslotlist;
824 int poll_state = POLL_LATCH_REGISTER;
825 u8 oldlatchlow = 0x00;
826 u8 curlatchlow = 0x00;
828 u8 ctrl_count = 0x00;
830 debug ("%s - Entry\n", __func__);
832 while (!kthread_should_stop()) {
833 /* try to get the lock to do some kind of hardware access */
834 down (&semOperations);
836 switch (poll_state) {
837 case POLL_LATCH_REGISTER:
838 oldlatchlow = curlatchlow;
840 list_for_each (pslotlist, &ibmphp_slot_head) {
841 if (ctrl_count >= ibmphp_get_total_controllers())
843 pslot = list_entry (pslotlist, struct slot, ibm_slot_list);
844 if (pslot->ctrl->ctlr_relative_id == ctrl_count) {
846 if (READ_SLOT_LATCH (pslot->ctrl)) {
847 rc = ibmphp_hpc_readslot (pslot,
848 READ_SLOTLATCHLOWREG,
850 if (oldlatchlow != curlatchlow)
851 process_changeinlatch (oldlatchlow,
858 poll_state = POLL_SLEEP;
861 list_for_each (pslotlist, &ibmphp_slot_head) {
862 pslot = list_entry (pslotlist, struct slot, ibm_slot_list);
863 // make a copy of the old status
864 memcpy ((void *) &myslot, (void *) pslot,
865 sizeof (struct slot));
866 rc = ibmphp_hpc_readslot (pslot, READ_ALLSTAT, NULL);
867 if ((myslot.status != pslot->status)
868 || (myslot.ext_status != pslot->ext_status))
869 process_changeinstatus (pslot, &myslot);
872 list_for_each (pslotlist, &ibmphp_slot_head) {
873 if (ctrl_count >= ibmphp_get_total_controllers())
875 pslot = list_entry (pslotlist, struct slot, ibm_slot_list);
876 if (pslot->ctrl->ctlr_relative_id == ctrl_count) {
878 if (READ_SLOT_LATCH (pslot->ctrl))
879 rc = ibmphp_hpc_readslot (pslot,
880 READ_SLOTLATCHLOWREG,
885 poll_state = POLL_SLEEP;
888 /* don't sleep with a lock on the hardware */
890 msleep(POLL_INTERVAL_SEC * 1000);
892 if (kthread_should_stop())
895 down (&semOperations);
897 if (poll_count >= POLL_LATCH_CNT) {
899 poll_state = POLL_SLOTS;
901 poll_state = POLL_LATCH_REGISTER;
904 /* give up the hardware semaphore */
906 /* sleep for a short time just for good measure */
910 debug ("%s - Exit\n", __func__);
915 /*----------------------------------------------------------------------
916 * Name: process_changeinstatus
918 * Action: compare old and new slot status, process the change in status
920 * Input: pointer to slot struct, old slot struct
922 * Return 0 or error codes
929 *---------------------------------------------------------------------*/
930 static int process_changeinstatus (struct slot *pslot, struct slot *poldslot)
937 debug ("process_changeinstatus - Entry pslot[%p], poldslot[%p]\n", pslot, poldslot);
939 // bit 0 - HPC_SLOT_POWER
940 if ((pslot->status & 0x01) != (poldslot->status & 0x01))
943 // bit 1 - HPC_SLOT_CONNECT
946 // bit 2 - HPC_SLOT_ATTN
947 if ((pslot->status & 0x04) != (poldslot->status & 0x04))
950 // bit 3 - HPC_SLOT_PRSNT2
951 // bit 4 - HPC_SLOT_PRSNT1
952 if (((pslot->status & 0x08) != (poldslot->status & 0x08))
953 || ((pslot->status & 0x10) != (poldslot->status & 0x10)))
956 // bit 5 - HPC_SLOT_PWRGD
957 if ((pslot->status & 0x20) != (poldslot->status & 0x20))
958 // OFF -> ON: ignore, ON -> OFF: disable slot
959 if ((poldslot->status & 0x20) && (SLOT_CONNECT (poldslot->status) == HPC_SLOT_CONNECTED) && (SLOT_PRESENT (poldslot->status)))
962 // bit 6 - HPC_SLOT_BUS_SPEED
965 // bit 7 - HPC_SLOT_LATCH
966 if ((pslot->status & 0x80) != (poldslot->status & 0x80)) {
969 if (pslot->status & 0x80) {
970 if (SLOT_PWRGD (pslot->status)) {
971 // power goes on and off after closing latch
972 // check again to make sure power is still ON
974 rc = ibmphp_hpc_readslot (pslot, READ_SLOTSTATUS, &status);
975 if (SLOT_PWRGD (status))
977 else // overwrite power in pslot to OFF
978 pslot->status &= ~HPC_SLOT_POWER;
982 else if ((SLOT_PWRGD (poldslot->status) == HPC_SLOT_PWRGD_GOOD)
983 && (SLOT_CONNECT (poldslot->status) == HPC_SLOT_CONNECTED) && (SLOT_PRESENT (poldslot->status))) {
988 // bit 4 - HPC_SLOT_BLINK_ATTN
989 if ((pslot->ext_status & 0x08) != (poldslot->ext_status & 0x08))
993 debug ("process_changeinstatus - disable slot\n");
995 rc = ibmphp_do_disable_slot (pslot);
998 if (update || disable) {
999 ibmphp_update_slot_info (pslot);
1002 debug ("%s - Exit rc[%d] disable[%x] update[%x]\n", __func__, rc, disable, update);
1007 /*----------------------------------------------------------------------
1008 * Name: process_changeinlatch
1010 * Action: compare old and new latch reg status, process the change
1012 * Input: old and current latch register status
1014 * Return 0 or error codes
1016 *---------------------------------------------------------------------*/
1017 static int process_changeinlatch (u8 old, u8 new, struct controller *ctrl)
1019 struct slot myslot, *pslot;
1024 debug ("%s - Entry old[%x], new[%x]\n", __func__, old, new);
1025 // bit 0 reserved, 0 is LSB, check bit 1-6 for 6 slots
1027 for (i = ctrl->starting_slot_num; i <= ctrl->ending_slot_num; i++) {
1029 if ((mask & old) != (mask & new)) {
1030 pslot = ibmphp_get_slot_from_physical_num (i);
1032 memcpy ((void *) &myslot, (void *) pslot, sizeof (struct slot));
1033 rc = ibmphp_hpc_readslot (pslot, READ_ALLSTAT, NULL);
1034 debug ("%s - call process_changeinstatus for slot[%d]\n", __func__, i);
1035 process_changeinstatus (pslot, &myslot);
1038 err ("%s - Error bad pointer for slot[%d]\n", __func__, i);
1042 debug ("%s - Exit rc[%d]\n", __func__, rc);
1046 /*----------------------------------------------------------------------
1047 * Name: ibmphp_hpc_start_poll_thread
1049 * Action: start polling thread
1050 *---------------------------------------------------------------------*/
1051 int __init ibmphp_hpc_start_poll_thread (void)
1053 debug ("%s - Entry\n", __func__);
1055 ibmphp_poll_thread = kthread_run(poll_hpc, NULL, "hpc_poll");
1056 if (IS_ERR(ibmphp_poll_thread)) {
1057 err ("%s - Error, thread not started\n", __func__);
1058 return PTR_ERR(ibmphp_poll_thread);
1063 /*----------------------------------------------------------------------
1064 * Name: ibmphp_hpc_stop_poll_thread
1066 * Action: stop polling thread and cleanup
1067 *---------------------------------------------------------------------*/
1068 void __exit ibmphp_hpc_stop_poll_thread (void)
1070 debug ("%s - Entry\n", __func__);
1072 kthread_stop(ibmphp_poll_thread);
1073 debug ("before locking operations \n");
1074 ibmphp_lock_operations ();
1075 debug ("after locking operations \n");
1077 // wait for poll thread to exit
1078 debug ("before sem_exit down \n");
1080 debug ("after sem_exit down \n");
1083 debug ("before free_hpc_access \n");
1085 debug ("after free_hpc_access \n");
1086 ibmphp_unlock_operations ();
1087 debug ("after unlock operations \n");
1089 debug ("after sem exit up\n");
1091 debug ("%s - Exit\n", __func__);
1094 /*----------------------------------------------------------------------
1095 * Name: hpc_wait_ctlr_notworking
1097 * Action: wait until the controller is in a not working state
1099 * Return 0, HPC_ERROR
1101 *---------------------------------------------------------------------*/
1102 static int hpc_wait_ctlr_notworking (int timeout, struct controller *ctlr_ptr, void __iomem *wpg_bbar,
1108 debug_polling ("hpc_wait_ctlr_notworking - Entry timeout[%d]\n", timeout);
1111 *pstatus = ctrl_read (ctlr_ptr, wpg_bbar, WPG_CTLR_INDEX);
1112 if (*pstatus == HPC_ERROR) {
1116 if (CTLR_WORKING (*pstatus) == HPC_CTLR_WORKING_NO)
1122 err ("HPCreadslot - Error ctlr timeout\n");
1128 debug_polling ("hpc_wait_ctlr_notworking - Exit rc[%x] status[%x]\n", rc, *pstatus);