30 #include <linux/wait.h>
31 #include <linux/time.h>
33 #include <linux/module.h>
34 #include <linux/pci.h>
37 #include <linux/sched.h>
42 static int to_debug = 0;
43 #define debug_polling(fmt, arg...) do { if (to_debug) debug (fmt, arg); } while (0)
48 #define CMD_COMPLETE_TOUT_SEC 60 // give HPC 60 sec to finish cmd
49 #define HPC_CTLR_WORKING_TOUT 60 // give HPC 60 sec to finish cmd
50 #define HPC_GETACCESS_TIMEOUT 60 // seconds
51 #define POLL_INTERVAL_SEC 2 // poll HPC every 2 seconds
52 #define POLL_LATCH_CNT 5 // poll latch 5 times, then poll slots
57 #define WPG_I2CMBUFL_OFFSET 0x08 // I2C Message Buffer Low
58 #define WPG_I2CMOSUP_OFFSET 0x10 // I2C Master Operation Setup Reg
59 #define WPG_I2CMCNTL_OFFSET 0x20 // I2C Master Control Register
60 #define WPG_I2CPARM_OFFSET 0x40 // I2C Parameter Register
61 #define WPG_I2CSTAT_OFFSET 0x70 // I2C Status Register
66 #define WPG_I2C_AND 0x1000 // I2C AND operation
67 #define WPG_I2C_OR 0x2000 // I2C OR operation
72 #define WPG_READATADDR_MASK 0x00010000 // read,bytes,I2C shifted,index
73 #define WPG_WRITEATADDR_MASK 0x40010000 // write,bytes,I2C shifted,index
74 #define WPG_READDIRECT_MASK 0x10010000
75 #define WPG_WRITEDIRECT_MASK 0x60010000
81 #define WPG_I2CMCNTL_STARTOP_MASK 0x00000002 // Start the Operation
86 #define WPG_I2C_IOREMAP_SIZE 0x2044 // size of linear address interval
91 #define WPG_1ST_SLOT_INDEX 0x01 // index - 1st slot for ctlr
92 #define WPG_CTLR_INDEX 0x0F // index - ctlr
93 #define WPG_1ST_EXTSLOT_INDEX 0x10 // index - 1st ext slot for ctlr
94 #define WPG_1ST_BUS_INDEX 0x1F // index - 1st bus for ctlr
100 #define HPC_I2CSTATUS_CHECK(s) ((u8)((s & 0x00000A76) ? 0 : 1))
105 static struct mutex sem_hpcaccess;
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 int poll_hpc(
void *
data);
120 static int process_changeinstatus (
struct slot *,
struct slot *);
133 debug (
"%s - Entry\n", __func__);
136 sema_init(&semOperations, 1);
137 sema_init(&sem_exit, 0);
140 debug (
"%s - Exit\n", __func__);
154 unsigned long wpg_data;
155 unsigned long ultemp;
158 debug_polling (
"%s - Entry WPGBbar[%p] index[%x] \n", __func__, WPGBbar, index);
168 ultemp = ultemp >> 1;
169 data |= (ultemp << 8);
172 data |= (
unsigned long)index;
173 }
else if (ctlr_ptr->
ctlr_type == 0x04) {
177 ultemp = (
unsigned long)index;
178 ultemp = ultemp << 8;
181 err (
"this controller type is not supported \n");
187 writel (wpg_data, wpg_addr);
194 writel (wpg_data, wpg_addr);
202 writel (wpg_data, wpg_addr);
210 wpg_data =
readl (wpg_addr);
217 debug (
"%s - Error : WPG timeout\n", __func__);
226 wpg_data =
readl (wpg_addr);
233 debug (
"ctrl_read - Exit Error:I2C timeout\n");
240 wpg_data =
readl (wpg_addr);
245 debug_polling (
"%s - Exit index[%x] status[%x]\n", __func__, index, status);
261 unsigned long wpg_data;
262 unsigned long ultemp;
266 debug_polling (
"%s - Entry WPGBbar[%p] index[%x] cmd[%x]\n", __func__, WPGBbar, index, cmd);
279 ultemp = ultemp >> 1;
280 data |= (ultemp << 8);
283 data |= (
unsigned long)index;
284 }
else if (ctlr_ptr->
ctlr_type == 0x04) {
288 ultemp = (
unsigned long)index;
289 ultemp = ultemp << 8;
292 err (
"this controller type is not supported \n");
298 writel (wpg_data, wpg_addr);
302 data = 0x00000000 | (
unsigned long)cmd;
305 writel (wpg_data, wpg_addr);
312 wpg_addr = WPGBbar + WPG_I2CMCNTL_OFFSET +
WPG_I2C_OR;
313 writel (wpg_data, wpg_addr);
321 wpg_data =
readl (wpg_addr);
328 debug (
"%s - Exit Error:WPG timeout\n", __func__);
338 wpg_data =
readl (wpg_addr);
345 debug (
"ctrl_read - Error : I2C timeout\n");
362 start_address = ctlr_ptr->
u.
isa_ctlr.io_start;
363 end_address = ctlr_ptr->
u.
isa_ctlr.io_end;
364 data =
inb (start_address + offset);
376 start_address = ctlr_ptr->
u.
isa_ctlr.io_start;
377 port_address = start_address + (
u16) offset;
378 outb (data, port_address);
384 debug (
"inside pci_ctrl_read\n");
393 debug (
"inside pci_ctrl_write\n");
406 rc = isa_ctrl_read (ctlr, offset);
409 rc = pci_ctrl_read (ctlr, offset);
413 rc = i2c_ctrl_read (ctlr, base, offset);
426 isa_ctrl_write(ctlr, offset, data);
429 rc = pci_ctrl_write (ctlr, offset, data);
433 rc = i2c_ctrl_write(ctlr, base, offset, data);
447 static u8 hpc_writecmdtoindex (
u8 cmd,
u8 index)
479 err (
"hpc_writecmdtoindex - Error invalid cmd[%x]\n", cmd);
493 static u8 hpc_readcmdtoindex (
u8 cmd,
u8 index)
545 debug_polling (
"%s - Entry pslot[%p] cmd[%x] pstatus[%p]\n", __func__, pslot, cmd, pstatus);
550 err (
"%s - Error invalid pointer, rc[%d]\n", __func__, rc);
558 err (
"%s - Exit Error:invalid bus, rc[%d]\n", __func__, rc);
561 index = (
u8) busindex;
565 index = hpc_readcmdtoindex (cmd, index);
569 err (
"%s - Exit Error:invalid index, rc[%d]\n", __func__, rc);
573 ctlr_ptr = pslot->
ctrl;
592 pslot->
status = ctrl_read (ctlr_ptr, wpg_bbar, index);
602 *pstatus = ctrl_read (ctlr_ptr, wpg_bbar, index);
607 *pstatus = ctrl_read (ctlr_ptr, wpg_bbar, index);
616 pslot->
busstatus = ctrl_read (ctlr_ptr, wpg_bbar, index);
619 *pstatus = ctrl_read (ctlr_ptr, wpg_bbar, index);
622 *pstatus = ctrl_read (ctlr_ptr, wpg_bbar, index);
626 *pstatus = ctrl_read (ctlr_ptr, wpg_bbar, index);
637 pslot->
status = ctrl_read (ctlr_ptr, wpg_bbar, index);
639 ctlr_ptr, wpg_bbar, &status);
642 ctrl_read (ctlr_ptr, wpg_bbar,
645 err (
"%s - Error ctrl_read failed\n", __func__);
685 debug_polling (
"%s - Entry pslot[%p] cmd[%x]\n", __func__, pslot, cmd);
688 err (
"%s - Error Exit rc[%d]\n", __func__, rc);
698 err (
"%s - Exit Error:invalid bus, rc[%d]\n", __func__, rc);
701 index = (
u8) busindex;
705 index = hpc_writecmdtoindex (cmd, index);
709 err (
"%s - Error Exit rc[%d]\n", __func__, rc);
713 ctlr_ptr = pslot->
ctrl;
723 debug (
"%s - ctlr id[%x] physical[%lx] logical[%lx] i2c[%x]\n", __func__,
733 ctrl_write (ctlr_ptr, wpg_bbar, index, cmd);
754 err (
"%s - Error command complete timeout\n", __func__);
778 static void get_hpc_access (
void)
786 void free_hpc_access (
void)
798 down (&semOperations);
807 debug (
"%s - Entry\n", __func__);
810 debug (
"%s - Exit\n", __func__);
816 #define POLL_LATCH_REGISTER 0
819 static int poll_hpc(
void *data)
826 u8 oldlatchlow = 0x00;
827 u8 curlatchlow = 0x00;
829 u8 ctrl_count = 0x00;
831 debug (
"%s - Entry\n", __func__);
835 down (&semOperations);
837 switch (poll_state) {
839 oldlatchlow = curlatchlow;
845 if (pslot->
ctrl->ctlr_relative_id == ctrl_count) {
851 if (oldlatchlow != curlatchlow)
852 process_changeinlatch (oldlatchlow,
865 memcpy ((
void *) &myslot, (
void *) pslot,
866 sizeof (
struct slot));
870 process_changeinstatus (pslot, &myslot);
877 if (pslot->
ctrl->ctlr_relative_id == ctrl_count) {
896 down (&semOperations);
912 debug (
"%s - Exit\n", __func__);
932 static int process_changeinstatus (
struct slot *pslot,
struct slot *poldslot)
939 debug (
"process_changeinstatus - Entry pslot[%p], poldslot[%p]\n", pslot, poldslot);
942 if ((pslot->
status & 0x01) != (poldslot->
status & 0x01))
949 if ((pslot->
status & 0x04) != (poldslot->
status & 0x04))
954 if (((pslot->
status & 0x08) != (poldslot->
status & 0x08))
955 || ((pslot->
status & 0x10) != (poldslot->
status & 0x10)))
959 if ((pslot->
status & 0x20) != (poldslot->
status & 0x20))
968 if ((pslot->
status & 0x80) != (poldslot->
status & 0x80)) {
971 if (pslot->
status & 0x80) {
995 debug (
"process_changeinstatus - disable slot\n");
1000 if (update || disable) {
1004 debug (
"%s - Exit rc[%d] disable[%x] update[%x]\n", __func__, rc, disable, update);
1019 static int process_changeinlatch (
u8 old,
u8 new,
struct controller *ctrl)
1021 struct slot myslot, *pslot;
1026 debug (
"%s - Entry old[%x], new[%x]\n", __func__, old,
new);
1031 if ((mask & old) != (mask &
new)) {
1034 memcpy ((
void *) &myslot, (
void *) pslot,
sizeof (
struct slot));
1036 debug (
"%s - call process_changeinstatus for slot[%d]\n", __func__, i);
1037 process_changeinstatus (pslot, &myslot);
1040 err (
"%s - Error bad pointer for slot[%d]\n", __func__, i);
1044 debug (
"%s - Exit rc[%d]\n", __func__, rc);
1055 debug (
"%s - Entry\n", __func__);
1058 if (IS_ERR(ibmphp_poll_thread)) {
1059 err (
"%s - Error, thread not started\n", __func__);
1060 return PTR_ERR(ibmphp_poll_thread);
1072 debug (
"%s - Entry\n", __func__);
1075 debug (
"before locking operations \n");
1077 debug (
"after locking operations \n");
1080 debug (
"before sem_exit down \n");
1082 debug (
"after sem_exit down \n");
1085 debug (
"before free_hpc_access \n");
1087 debug (
"after free_hpc_access \n");
1089 debug (
"after unlock operations \n");
1091 debug (
"after sem exit up\n");
1093 debug (
"%s - Exit\n", __func__);
1104 static int hpc_wait_ctlr_notworking (
int timeout,
struct controller *ctlr_ptr,
void __iomem *wpg_bbar,
1110 debug_polling (
"hpc_wait_ctlr_notworking - Entry timeout[%d]\n", timeout);
1124 err (
"HPCreadslot - Error ctlr timeout\n");
1130 debug_polling (
"hpc_wait_ctlr_notworking - Exit rc[%x] status[%x]\n", rc, *pstatus);