blob: a5720d12e573237cb8cae77af8f1c0163837fbbb [file] [log] [blame]
Bjorn Helgaas736759e2018-01-26 14:22:04 -06001// SPDX-License-Identifier: GPL-2.0+
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * IBM Hot Plug Controller Driver
4 *
5 * Written By: Jyoti Shah, IBM Corporation
6 *
7 * Copyright (C) 2001-2003 IBM Corp.
8 *
9 * All rights reserved.
10 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Send feedback to <gregkh@us.ibm.com>
12 * <jshah@us.ibm.com>
13 *
14 */
15
16#include <linux/wait.h>
17#include <linux/time.h>
Arnd Bergmann2a727f62018-12-10 22:49:10 +010018#include <linux/completion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/delay.h>
20#include <linux/module.h>
21#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +010023#include <linux/mutex.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040024#include <linux/sched.h>
Kristen Carlson Accardi2d100fe2007-08-14 16:07:00 -070025#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "ibmphp.h"
27
Kristen Accardidc6712d2006-03-14 16:24:47 -080028static int to_debug = 0;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -080029#define debug_polling(fmt, arg...) do { if (to_debug) debug(fmt, arg); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31//----------------------------------------------------------------------------
32// timeout values
33//----------------------------------------------------------------------------
34#define CMD_COMPLETE_TOUT_SEC 60 // give HPC 60 sec to finish cmd
35#define HPC_CTLR_WORKING_TOUT 60 // give HPC 60 sec to finish cmd
36#define HPC_GETACCESS_TIMEOUT 60 // seconds
37#define POLL_INTERVAL_SEC 2 // poll HPC every 2 seconds
38#define POLL_LATCH_CNT 5 // poll latch 5 times, then poll slots
39
40//----------------------------------------------------------------------------
41// Winnipeg Architected Register Offsets
42//----------------------------------------------------------------------------
43#define WPG_I2CMBUFL_OFFSET 0x08 // I2C Message Buffer Low
44#define WPG_I2CMOSUP_OFFSET 0x10 // I2C Master Operation Setup Reg
45#define WPG_I2CMCNTL_OFFSET 0x20 // I2C Master Control Register
46#define WPG_I2CPARM_OFFSET 0x40 // I2C Parameter Register
47#define WPG_I2CSTAT_OFFSET 0x70 // I2C Status Register
48
49//----------------------------------------------------------------------------
50// Winnipeg Store Type commands (Add this commands to the register offset)
51//----------------------------------------------------------------------------
52#define WPG_I2C_AND 0x1000 // I2C AND operation
53#define WPG_I2C_OR 0x2000 // I2C OR operation
54
55//----------------------------------------------------------------------------
Steven Coleeaae4b32005-05-03 18:38:30 -060056// Command set for I2C Master Operation Setup Register
Linus Torvalds1da177e2005-04-16 15:20:36 -070057//----------------------------------------------------------------------------
58#define WPG_READATADDR_MASK 0x00010000 // read,bytes,I2C shifted,index
59#define WPG_WRITEATADDR_MASK 0x40010000 // write,bytes,I2C shifted,index
60#define WPG_READDIRECT_MASK 0x10010000
61#define WPG_WRITEDIRECT_MASK 0x60010000
62
63
64//----------------------------------------------------------------------------
65// bit masks for I2C Master Control Register
66//----------------------------------------------------------------------------
67#define WPG_I2CMCNTL_STARTOP_MASK 0x00000002 // Start the Operation
68
69//----------------------------------------------------------------------------
70//
71//----------------------------------------------------------------------------
72#define WPG_I2C_IOREMAP_SIZE 0x2044 // size of linear address interval
73
74//----------------------------------------------------------------------------
75// command index
76//----------------------------------------------------------------------------
77#define WPG_1ST_SLOT_INDEX 0x01 // index - 1st slot for ctlr
78#define WPG_CTLR_INDEX 0x0F // index - ctlr
79#define WPG_1ST_EXTSLOT_INDEX 0x10 // index - 1st ext slot for ctlr
80#define WPG_1ST_BUS_INDEX 0x1F // index - 1st bus for ctlr
81
82//----------------------------------------------------------------------------
83// macro utilities
84//----------------------------------------------------------------------------
Kristen Accardidc6712d2006-03-14 16:24:47 -080085// if bits 20,22,25,26,27,29,30 are OFF return 1
86#define HPC_I2CSTATUS_CHECK(s) ((u8)((s & 0x00000A76) ? 0 : 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88//----------------------------------------------------------------------------
89// global variables
90//----------------------------------------------------------------------------
Arnd Bergmann2a727f62018-12-10 22:49:10 +010091static DEFINE_MUTEX(sem_hpcaccess); // lock access to HPC
92static DEFINE_MUTEX(operations_mutex); // lock all operations and
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 // access to data structures
Arnd Bergmann2a727f62018-12-10 22:49:10 +010094static DECLARE_COMPLETION(exit_complete); // make sure polling thread goes away
Kristen Carlson Accardi2d100fe2007-08-14 16:07:00 -070095static struct task_struct *ibmphp_poll_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096//----------------------------------------------------------------------------
97// local function prototypes
98//----------------------------------------------------------------------------
Bogicevic Sasaff3ce482015-12-27 13:21:11 -080099static u8 i2c_ctrl_read(struct controller *, void __iomem *, u8);
100static u8 i2c_ctrl_write(struct controller *, void __iomem *, u8, u8);
101static u8 hpc_writecmdtoindex(u8, u8);
102static u8 hpc_readcmdtoindex(u8, u8);
103static void get_hpc_access(void);
104static void free_hpc_access(void);
Kristen Carlson Accardi2d100fe2007-08-14 16:07:00 -0700105static int poll_hpc(void *data);
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800106static int process_changeinstatus(struct slot *, struct slot *);
107static int process_changeinlatch(u8, u8, struct controller *);
108static int hpc_wait_ctlr_notworking(int, struct controller *, void __iomem *, u8 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109//----------------------------------------------------------------------------
110
111
112/*----------------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113* Name: i2c_ctrl_read
114*
115* Action: read from HPC over I2C
116*
117*---------------------------------------------------------------------*/
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800118static u8 i2c_ctrl_read(struct controller *ctlr_ptr, void __iomem *WPGBbar, u8 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
120 u8 status;
121 int i;
122 void __iomem *wpg_addr; // base addr + offset
123 unsigned long wpg_data; // data to/from WPG LOHI format
124 unsigned long ultemp;
125 unsigned long data; // actual data HILO format
126
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800127 debug_polling("%s - Entry WPGBbar[%p] index[%x] \n", __func__, WPGBbar, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 //--------------------------------------------------------------------
130 // READ - step 1
131 // read at address, byte length, I2C address (shifted), index
132 // or read direct, byte length, index
133 if (ctlr_ptr->ctlr_type == 0x02) {
134 data = WPG_READATADDR_MASK;
135 // fill in I2C address
136 ultemp = (unsigned long)ctlr_ptr->u.wpeg_ctlr.i2c_addr;
137 ultemp = ultemp >> 1;
138 data |= (ultemp << 8);
139
140 // fill in index
141 data |= (unsigned long)index;
142 } else if (ctlr_ptr->ctlr_type == 0x04) {
143 data = WPG_READDIRECT_MASK;
144
145 // fill in index
146 ultemp = (unsigned long)index;
147 ultemp = ultemp << 8;
148 data |= ultemp;
149 } else {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800150 err("this controller type is not supported \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return HPC_ERROR;
152 }
153
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800154 wpg_data = swab32(data); // swap data before writing
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 wpg_addr = WPGBbar + WPG_I2CMOSUP_OFFSET;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800156 writel(wpg_data, wpg_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 //--------------------------------------------------------------------
159 // READ - step 2 : clear the message buffer
160 data = 0x00000000;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800161 wpg_data = swab32(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 wpg_addr = WPGBbar + WPG_I2CMBUFL_OFFSET;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800163 writel(wpg_data, wpg_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 //--------------------------------------------------------------------
166 // READ - step 3 : issue start operation, I2C master control bit 30:ON
167 // 2020 : [20] OR operation at [20] offset 0x20
168 data = WPG_I2CMCNTL_STARTOP_MASK;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800169 wpg_data = swab32(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 wpg_addr = WPGBbar + WPG_I2CMCNTL_OFFSET + WPG_I2C_OR;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800171 writel(wpg_data, wpg_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 //--------------------------------------------------------------------
174 // READ - step 4 : wait until start operation bit clears
175 i = CMD_COMPLETE_TOUT_SEC;
176 while (i) {
177 msleep(10);
178 wpg_addr = WPGBbar + WPG_I2CMCNTL_OFFSET;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800179 wpg_data = readl(wpg_addr);
180 data = swab32(wpg_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if (!(data & WPG_I2CMCNTL_STARTOP_MASK))
182 break;
183 i--;
184 }
185 if (i == 0) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800186 debug("%s - Error : WPG timeout\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return HPC_ERROR;
188 }
189 //--------------------------------------------------------------------
190 // READ - step 5 : read I2C status register
191 i = CMD_COMPLETE_TOUT_SEC;
192 while (i) {
193 msleep(10);
194 wpg_addr = WPGBbar + WPG_I2CSTAT_OFFSET;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800195 wpg_data = readl(wpg_addr);
196 data = swab32(wpg_data);
197 if (HPC_I2CSTATUS_CHECK(data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 break;
199 i--;
200 }
201 if (i == 0) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800202 debug("ctrl_read - Exit Error:I2C timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 return HPC_ERROR;
204 }
205
206 //--------------------------------------------------------------------
207 // READ - step 6 : get DATA
208 wpg_addr = WPGBbar + WPG_I2CMBUFL_OFFSET;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800209 wpg_data = readl(wpg_addr);
210 data = swab32(wpg_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 status = (u8) data;
213
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800214 debug_polling("%s - Exit index[%x] status[%x]\n", __func__, index, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 return (status);
217}
218
219/*----------------------------------------------------------------------
220* Name: i2c_ctrl_write
221*
222* Action: write to HPC over I2C
223*
224* Return 0 or error codes
225*---------------------------------------------------------------------*/
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800226static u8 i2c_ctrl_write(struct controller *ctlr_ptr, void __iomem *WPGBbar, u8 index, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 u8 rc;
229 void __iomem *wpg_addr; // base addr + offset
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700230 unsigned long wpg_data; // data to/from WPG LOHI format
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 unsigned long ultemp;
232 unsigned long data; // actual data HILO format
233 int i;
234
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800235 debug_polling("%s - Entry WPGBbar[%p] index[%x] cmd[%x]\n", __func__, WPGBbar, index, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 rc = 0;
238 //--------------------------------------------------------------------
239 // WRITE - step 1
240 // write at address, byte length, I2C address (shifted), index
241 // or write direct, byte length, index
242 data = 0x00000000;
243
244 if (ctlr_ptr->ctlr_type == 0x02) {
245 data = WPG_WRITEATADDR_MASK;
246 // fill in I2C address
247 ultemp = (unsigned long)ctlr_ptr->u.wpeg_ctlr.i2c_addr;
248 ultemp = ultemp >> 1;
249 data |= (ultemp << 8);
250
251 // fill in index
252 data |= (unsigned long)index;
253 } else if (ctlr_ptr->ctlr_type == 0x04) {
254 data = WPG_WRITEDIRECT_MASK;
255
256 // fill in index
257 ultemp = (unsigned long)index;
258 ultemp = ultemp << 8;
259 data |= ultemp;
260 } else {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800261 err("this controller type is not supported \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return HPC_ERROR;
263 }
264
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800265 wpg_data = swab32(data); // swap data before writing
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 wpg_addr = WPGBbar + WPG_I2CMOSUP_OFFSET;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800267 writel(wpg_data, wpg_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 //--------------------------------------------------------------------
270 // WRITE - step 2 : clear the message buffer
271 data = 0x00000000 | (unsigned long)cmd;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800272 wpg_data = swab32(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 wpg_addr = WPGBbar + WPG_I2CMBUFL_OFFSET;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800274 writel(wpg_data, wpg_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 //--------------------------------------------------------------------
277 // WRITE - step 3 : issue start operation,I2C master control bit 30:ON
278 // 2020 : [20] OR operation at [20] offset 0x20
279 data = WPG_I2CMCNTL_STARTOP_MASK;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800280 wpg_data = swab32(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 wpg_addr = WPGBbar + WPG_I2CMCNTL_OFFSET + WPG_I2C_OR;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800282 writel(wpg_data, wpg_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 //--------------------------------------------------------------------
285 // WRITE - step 4 : wait until start operation bit clears
286 i = CMD_COMPLETE_TOUT_SEC;
287 while (i) {
288 msleep(10);
289 wpg_addr = WPGBbar + WPG_I2CMCNTL_OFFSET;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800290 wpg_data = readl(wpg_addr);
291 data = swab32(wpg_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (!(data & WPG_I2CMCNTL_STARTOP_MASK))
293 break;
294 i--;
295 }
296 if (i == 0) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800297 debug("%s - Exit Error:WPG timeout\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 rc = HPC_ERROR;
299 }
300
301 //--------------------------------------------------------------------
302 // WRITE - step 5 : read I2C status register
303 i = CMD_COMPLETE_TOUT_SEC;
304 while (i) {
305 msleep(10);
306 wpg_addr = WPGBbar + WPG_I2CSTAT_OFFSET;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800307 wpg_data = readl(wpg_addr);
308 data = swab32(wpg_data);
309 if (HPC_I2CSTATUS_CHECK(data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 break;
311 i--;
312 }
313 if (i == 0) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800314 debug("ctrl_read - Error : I2C timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 rc = HPC_ERROR;
316 }
317
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800318 debug_polling("%s Exit rc[%x]\n", __func__, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return (rc);
320}
321
322//------------------------------------------------------------
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700323// Read from ISA type HPC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324//------------------------------------------------------------
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800325static u8 isa_ctrl_read(struct controller *ctlr_ptr, u8 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 u16 start_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 u8 data;
329
330 start_address = ctlr_ptr->u.isa_ctlr.io_start;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800331 data = inb(start_address + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return data;
333}
334
335//--------------------------------------------------------------
336// Write to ISA type HPC
337//--------------------------------------------------------------
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800338static void isa_ctrl_write(struct controller *ctlr_ptr, u8 offset, u8 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
340 u16 start_address;
341 u16 port_address;
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 start_address = ctlr_ptr->u.isa_ctlr.io_start;
344 port_address = start_address + (u16) offset;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800345 outb(data, port_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800348static u8 pci_ctrl_read(struct controller *ctrl, u8 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 u8 data = 0x00;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800351 debug("inside pci_ctrl_read\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (ctrl->ctrl_dev)
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800353 pci_read_config_byte(ctrl->ctrl_dev, HPC_PCI_OFFSET + offset, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return data;
355}
356
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800357static u8 pci_ctrl_write(struct controller *ctrl, u8 offset, u8 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 u8 rc = -ENODEV;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800360 debug("inside pci_ctrl_write\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (ctrl->ctrl_dev) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800362 pci_write_config_byte(ctrl->ctrl_dev, HPC_PCI_OFFSET + offset, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 rc = 0;
364 }
365 return rc;
366}
367
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800368static u8 ctrl_read(struct controller *ctlr, void __iomem *base, u8 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
370 u8 rc;
371 switch (ctlr->ctlr_type) {
372 case 0:
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800373 rc = isa_ctrl_read(ctlr, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 break;
375 case 1:
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800376 rc = pci_ctrl_read(ctlr, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 break;
378 case 2:
379 case 4:
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800380 rc = i2c_ctrl_read(ctlr, base, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 break;
382 default:
383 return -ENODEV;
384 }
385 return rc;
386}
387
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800388static u8 ctrl_write(struct controller *ctlr, void __iomem *base, u8 offset, u8 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
390 u8 rc = 0;
391 switch (ctlr->ctlr_type) {
392 case 0:
393 isa_ctrl_write(ctlr, offset, data);
394 break;
395 case 1:
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800396 rc = pci_ctrl_write(ctlr, offset, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 break;
398 case 2:
399 case 4:
400 rc = i2c_ctrl_write(ctlr, base, offset, data);
401 break;
402 default:
403 return -ENODEV;
404 }
405 return rc;
406}
407/*----------------------------------------------------------------------
408* Name: hpc_writecmdtoindex()
409*
410* Action: convert a write command to proper index within a controller
411*
412* Return index, HPC_ERROR
413*---------------------------------------------------------------------*/
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800414static u8 hpc_writecmdtoindex(u8 cmd, u8 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
416 u8 rc;
417
418 switch (cmd) {
419 case HPC_CTLR_ENABLEIRQ: // 0x00.N.15
420 case HPC_CTLR_CLEARIRQ: // 0x06.N.15
421 case HPC_CTLR_RESET: // 0x07.N.15
422 case HPC_CTLR_IRQSTEER: // 0x08.N.15
423 case HPC_CTLR_DISABLEIRQ: // 0x01.N.15
424 case HPC_ALLSLOT_ON: // 0x11.N.15
425 case HPC_ALLSLOT_OFF: // 0x12.N.15
426 rc = 0x0F;
427 break;
428
429 case HPC_SLOT_OFF: // 0x02.Y.0-14
430 case HPC_SLOT_ON: // 0x03.Y.0-14
431 case HPC_SLOT_ATTNOFF: // 0x04.N.0-14
432 case HPC_SLOT_ATTNON: // 0x05.N.0-14
433 case HPC_SLOT_BLINKLED: // 0x13.N.0-14
434 rc = index;
435 break;
436
437 case HPC_BUS_33CONVMODE:
438 case HPC_BUS_66CONVMODE:
439 case HPC_BUS_66PCIXMODE:
440 case HPC_BUS_100PCIXMODE:
441 case HPC_BUS_133PCIXMODE:
442 rc = index + WPG_1ST_BUS_INDEX - 1;
443 break;
444
445 default:
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800446 err("hpc_writecmdtoindex - Error invalid cmd[%x]\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 rc = HPC_ERROR;
448 }
449
450 return rc;
451}
452
453/*----------------------------------------------------------------------
454* Name: hpc_readcmdtoindex()
455*
456* Action: convert a read command to proper index within a controller
457*
458* Return index, HPC_ERROR
459*---------------------------------------------------------------------*/
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800460static u8 hpc_readcmdtoindex(u8 cmd, u8 index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
462 u8 rc;
463
464 switch (cmd) {
465 case READ_CTLRSTATUS:
466 rc = 0x0F;
467 break;
468 case READ_SLOTSTATUS:
469 case READ_ALLSTAT:
470 rc = index;
471 break;
472 case READ_EXTSLOTSTATUS:
473 rc = index + WPG_1ST_EXTSLOT_INDEX;
474 break;
475 case READ_BUSSTATUS:
476 rc = index + WPG_1ST_BUS_INDEX - 1;
477 break;
478 case READ_SLOTLATCHLOWREG:
479 rc = 0x28;
480 break;
481 case READ_REVLEVEL:
482 rc = 0x25;
483 break;
484 case READ_HPCOPTIONS:
485 rc = 0x27;
486 break;
487 default:
488 rc = HPC_ERROR;
489 }
490 return rc;
491}
492
493/*----------------------------------------------------------------------
494* Name: HPCreadslot()
495*
496* Action: issue a READ command to HPC
497*
Matt LaPlante0779bf2d2006-11-30 05:24:39 +0100498* Input: pslot - cannot be NULL for READ_ALLSTAT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499* pstatus - can be NULL for READ_ALLSTAT
500*
501* Return 0 or error codes
502*---------------------------------------------------------------------*/
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800503int ibmphp_hpc_readslot(struct slot *pslot, u8 cmd, u8 *pstatus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
505 void __iomem *wpg_bbar = NULL;
506 struct controller *ctlr_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 u8 index, status;
508 int rc = 0;
509 int busindex;
510
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800511 debug_polling("%s - Entry pslot[%p] cmd[%x] pstatus[%p]\n", __func__, pslot, cmd, pstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 if ((pslot == NULL)
514 || ((pstatus == NULL) && (cmd != READ_ALLSTAT) && (cmd != READ_BUSSTATUS))) {
515 rc = -EINVAL;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800516 err("%s - Error invalid pointer, rc[%d]\n", __func__, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return rc;
518 }
519
520 if (cmd == READ_BUSSTATUS) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800521 busindex = ibmphp_get_bus_index(pslot->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (busindex < 0) {
523 rc = -EINVAL;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800524 err("%s - Exit Error:invalid bus, rc[%d]\n", __func__, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return rc;
526 } else
527 index = (u8) busindex;
528 } else
529 index = pslot->ctlr_index;
530
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800531 index = hpc_readcmdtoindex(cmd, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 if (index == HPC_ERROR) {
534 rc = -EINVAL;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800535 err("%s - Exit Error:invalid index, rc[%d]\n", __func__, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return rc;
537 }
538
539 ctlr_ptr = pslot->ctrl;
540
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800541 get_hpc_access();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 //--------------------------------------------------------------------
544 // map physical address to logical address
545 //--------------------------------------------------------------------
546 if ((ctlr_ptr->ctlr_type == 2) || (ctlr_ptr->ctlr_type == 4))
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800547 wpg_bbar = ioremap(ctlr_ptr->u.wpeg_ctlr.wpegbbar, WPG_I2C_IOREMAP_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 //--------------------------------------------------------------------
550 // check controller status before reading
551 //--------------------------------------------------------------------
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800552 rc = hpc_wait_ctlr_notworking(HPC_CTLR_WORKING_TOUT, ctlr_ptr, wpg_bbar, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (!rc) {
554 switch (cmd) {
555 case READ_ALLSTAT:
556 // update the slot structure
557 pslot->ctrl->status = status;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800558 pslot->status = ctrl_read(ctlr_ptr, wpg_bbar, index);
559 rc = hpc_wait_ctlr_notworking(HPC_CTLR_WORKING_TOUT, ctlr_ptr, wpg_bbar,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 &status);
561 if (!rc)
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800562 pslot->ext_status = ctrl_read(ctlr_ptr, wpg_bbar, index + WPG_1ST_EXTSLOT_INDEX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 break;
565
566 case READ_SLOTSTATUS:
567 // DO NOT update the slot structure
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800568 *pstatus = ctrl_read(ctlr_ptr, wpg_bbar, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 break;
570
571 case READ_EXTSLOTSTATUS:
572 // DO NOT update the slot structure
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800573 *pstatus = ctrl_read(ctlr_ptr, wpg_bbar, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 break;
575
576 case READ_CTLRSTATUS:
577 // DO NOT update the slot structure
578 *pstatus = status;
579 break;
580
581 case READ_BUSSTATUS:
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800582 pslot->busstatus = ctrl_read(ctlr_ptr, wpg_bbar, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 break;
584 case READ_REVLEVEL:
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800585 *pstatus = ctrl_read(ctlr_ptr, wpg_bbar, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 break;
587 case READ_HPCOPTIONS:
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800588 *pstatus = ctrl_read(ctlr_ptr, wpg_bbar, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 break;
590 case READ_SLOTLATCHLOWREG:
591 // DO NOT update the slot structure
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800592 *pstatus = ctrl_read(ctlr_ptr, wpg_bbar, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 break;
594
595 // Not used
596 case READ_ALLSLOT:
Geliang Tang2ac83cc2015-12-12 21:36:57 +0800597 list_for_each_entry(pslot, &ibmphp_slot_head,
598 ibm_slot_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 index = pslot->ctlr_index;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800600 rc = hpc_wait_ctlr_notworking(HPC_CTLR_WORKING_TOUT, ctlr_ptr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 wpg_bbar, &status);
602 if (!rc) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800603 pslot->status = ctrl_read(ctlr_ptr, wpg_bbar, index);
604 rc = hpc_wait_ctlr_notworking(HPC_CTLR_WORKING_TOUT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 ctlr_ptr, wpg_bbar, &status);
606 if (!rc)
607 pslot->ext_status =
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800608 ctrl_read(ctlr_ptr, wpg_bbar,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 index + WPG_1ST_EXTSLOT_INDEX);
610 } else {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800611 err("%s - Error ctrl_read failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 rc = -EINVAL;
613 break;
614 }
615 }
616 break;
617 default:
618 rc = -EINVAL;
619 break;
620 }
621 }
622 //--------------------------------------------------------------------
623 // cleanup
624 //--------------------------------------------------------------------
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 // remove physical to logical address mapping
627 if ((ctlr_ptr->ctlr_type == 2) || (ctlr_ptr->ctlr_type == 4))
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800628 iounmap(wpg_bbar);
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700629
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800630 free_hpc_access();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800632 debug_polling("%s - Exit rc[%d]\n", __func__, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 return rc;
634}
635
636/*----------------------------------------------------------------------
637* Name: ibmphp_hpc_writeslot()
638*
639* Action: issue a WRITE command to HPC
640*---------------------------------------------------------------------*/
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800641int ibmphp_hpc_writeslot(struct slot *pslot, u8 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
643 void __iomem *wpg_bbar = NULL;
644 struct controller *ctlr_ptr;
645 u8 index, status;
646 int busindex;
647 u8 done;
648 int rc = 0;
649 int timeout;
650
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800651 debug_polling("%s - Entry pslot[%p] cmd[%x]\n", __func__, pslot, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (pslot == NULL) {
653 rc = -EINVAL;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800654 err("%s - Error Exit rc[%d]\n", __func__, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return rc;
656 }
657
658 if ((cmd == HPC_BUS_33CONVMODE) || (cmd == HPC_BUS_66CONVMODE) ||
659 (cmd == HPC_BUS_66PCIXMODE) || (cmd == HPC_BUS_100PCIXMODE) ||
660 (cmd == HPC_BUS_133PCIXMODE)) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800661 busindex = ibmphp_get_bus_index(pslot->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (busindex < 0) {
663 rc = -EINVAL;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800664 err("%s - Exit Error:invalid bus, rc[%d]\n", __func__, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return rc;
666 } else
667 index = (u8) busindex;
668 } else
669 index = pslot->ctlr_index;
670
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800671 index = hpc_writecmdtoindex(cmd, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 if (index == HPC_ERROR) {
674 rc = -EINVAL;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800675 err("%s - Error Exit rc[%d]\n", __func__, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return rc;
677 }
678
679 ctlr_ptr = pslot->ctrl;
680
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800681 get_hpc_access();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 //--------------------------------------------------------------------
684 // map physical address to logical address
685 //--------------------------------------------------------------------
686 if ((ctlr_ptr->ctlr_type == 2) || (ctlr_ptr->ctlr_type == 4)) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800687 wpg_bbar = ioremap(ctlr_ptr->u.wpeg_ctlr.wpegbbar, WPG_I2C_IOREMAP_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800689 debug("%s - ctlr id[%x] physical[%lx] logical[%lx] i2c[%x]\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 ctlr_ptr->ctlr_id, (ulong) (ctlr_ptr->u.wpeg_ctlr.wpegbbar), (ulong) wpg_bbar,
691 ctlr_ptr->u.wpeg_ctlr.i2c_addr);
692 }
693 //--------------------------------------------------------------------
694 // check controller status before writing
695 //--------------------------------------------------------------------
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800696 rc = hpc_wait_ctlr_notworking(HPC_CTLR_WORKING_TOUT, ctlr_ptr, wpg_bbar, &status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (!rc) {
698
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800699 ctrl_write(ctlr_ptr, wpg_bbar, index, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 //--------------------------------------------------------------------
702 // check controller is still not working on the command
703 //--------------------------------------------------------------------
704 timeout = CMD_COMPLETE_TOUT_SEC;
Kristen Accardidc6712d2006-03-14 16:24:47 -0800705 done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 while (!done) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800707 rc = hpc_wait_ctlr_notworking(HPC_CTLR_WORKING_TOUT, ctlr_ptr, wpg_bbar,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 &status);
709 if (!rc) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800710 if (NEEDTOCHECK_CMDSTATUS(cmd)) {
711 if (CTLR_FINISHED(status) == HPC_CTLR_FINISHED_YES)
Kristen Accardidc6712d2006-03-14 16:24:47 -0800712 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 } else
Kristen Accardidc6712d2006-03-14 16:24:47 -0800714 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
716 if (!done) {
717 msleep(1000);
718 if (timeout < 1) {
Kristen Accardidc6712d2006-03-14 16:24:47 -0800719 done = 1;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800720 err("%s - Error command complete timeout\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 rc = -EFAULT;
722 } else
723 timeout--;
724 }
725 }
726 ctlr_ptr->status = status;
727 }
728 // cleanup
729
730 // remove physical to logical address mapping
731 if ((ctlr_ptr->ctlr_type == 2) || (ctlr_ptr->ctlr_type == 4))
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800732 iounmap(wpg_bbar);
733 free_hpc_access();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800735 debug_polling("%s - Exit rc[%d]\n", __func__, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 return rc;
737}
738
739/*----------------------------------------------------------------------
740* Name: get_hpc_access()
741*
742* Action: make sure only one process can access HPC at one time
743*---------------------------------------------------------------------*/
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800744static void get_hpc_access(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100746 mutex_lock(&sem_hpcaccess);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
749/*----------------------------------------------------------------------
750* Name: free_hpc_access()
751*---------------------------------------------------------------------*/
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800752void free_hpc_access(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100754 mutex_unlock(&sem_hpcaccess);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
757/*----------------------------------------------------------------------
758* Name: ibmphp_lock_operations()
759*
760* Action: make sure only one process can change the data structure
761*---------------------------------------------------------------------*/
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800762void ibmphp_lock_operations(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Arnd Bergmann2a727f62018-12-10 22:49:10 +0100764 mutex_lock(&operations_mutex);
Kristen Accardidc6712d2006-03-14 16:24:47 -0800765 to_debug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
768/*----------------------------------------------------------------------
769* Name: ibmphp_unlock_operations()
770*---------------------------------------------------------------------*/
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800771void ibmphp_unlock_operations(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800773 debug("%s - Entry\n", __func__);
Arnd Bergmann2a727f62018-12-10 22:49:10 +0100774 mutex_unlock(&operations_mutex);
Kristen Accardidc6712d2006-03-14 16:24:47 -0800775 to_debug = 0;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800776 debug("%s - Exit\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
778
779/*----------------------------------------------------------------------
780* Name: poll_hpc()
781*---------------------------------------------------------------------*/
782#define POLL_LATCH_REGISTER 0
783#define POLL_SLOTS 1
784#define POLL_SLEEP 2
Kristen Carlson Accardi2d100fe2007-08-14 16:07:00 -0700785static int poll_hpc(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 struct slot myslot;
788 struct slot *pslot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 int rc;
790 int poll_state = POLL_LATCH_REGISTER;
791 u8 oldlatchlow = 0x00;
792 u8 curlatchlow = 0x00;
793 int poll_count = 0;
794 u8 ctrl_count = 0x00;
795
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800796 debug("%s - Entry\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Kristen Carlson Accardi2d100fe2007-08-14 16:07:00 -0700798 while (!kthread_should_stop()) {
Steven Coleeaae4b32005-05-03 18:38:30 -0600799 /* try to get the lock to do some kind of hardware access */
Arnd Bergmann2a727f62018-12-10 22:49:10 +0100800 mutex_lock(&operations_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 switch (poll_state) {
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700803 case POLL_LATCH_REGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 oldlatchlow = curlatchlow;
805 ctrl_count = 0x00;
Geliang Tang2ac83cc2015-12-12 21:36:57 +0800806 list_for_each_entry(pslot, &ibmphp_slot_head,
807 ibm_slot_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (ctrl_count >= ibmphp_get_total_controllers())
809 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (pslot->ctrl->ctlr_relative_id == ctrl_count) {
811 ctrl_count++;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800812 if (READ_SLOT_LATCH(pslot->ctrl)) {
813 rc = ibmphp_hpc_readslot(pslot,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 READ_SLOTLATCHLOWREG,
815 &curlatchlow);
816 if (oldlatchlow != curlatchlow)
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800817 process_changeinlatch(oldlatchlow,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 curlatchlow,
819 pslot->ctrl);
820 }
821 }
822 }
823 ++poll_count;
824 poll_state = POLL_SLEEP;
825 break;
826 case POLL_SLOTS:
Geliang Tang2ac83cc2015-12-12 21:36:57 +0800827 list_for_each_entry(pslot, &ibmphp_slot_head,
828 ibm_slot_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 // make a copy of the old status
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800830 memcpy((void *) &myslot, (void *) pslot,
831 sizeof(struct slot));
832 rc = ibmphp_hpc_readslot(pslot, READ_ALLSTAT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 if ((myslot.status != pslot->status)
834 || (myslot.ext_status != pslot->ext_status))
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800835 process_changeinstatus(pslot, &myslot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
837 ctrl_count = 0x00;
Geliang Tang2ac83cc2015-12-12 21:36:57 +0800838 list_for_each_entry(pslot, &ibmphp_slot_head,
839 ibm_slot_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (ctrl_count >= ibmphp_get_total_controllers())
841 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (pslot->ctrl->ctlr_relative_id == ctrl_count) {
843 ctrl_count++;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800844 if (READ_SLOT_LATCH(pslot->ctrl))
845 rc = ibmphp_hpc_readslot(pslot,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 READ_SLOTLATCHLOWREG,
847 &curlatchlow);
848 }
849 }
850 ++poll_count;
851 poll_state = POLL_SLEEP;
852 break;
853 case POLL_SLEEP:
854 /* don't sleep with a lock on the hardware */
Arnd Bergmann2a727f62018-12-10 22:49:10 +0100855 mutex_unlock(&operations_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 msleep(POLL_INTERVAL_SEC * 1000);
857
Kristen Carlson Accardi2d100fe2007-08-14 16:07:00 -0700858 if (kthread_should_stop())
Jesse Barnes5c788a62009-12-04 15:18:01 -0800859 goto out_sleep;
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700860
Arnd Bergmann2a727f62018-12-10 22:49:10 +0100861 mutex_lock(&operations_mutex);
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (poll_count >= POLL_LATCH_CNT) {
864 poll_count = 0;
865 poll_state = POLL_SLOTS;
866 } else
867 poll_state = POLL_LATCH_REGISTER;
868 break;
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700869 }
Steven Coleeaae4b32005-05-03 18:38:30 -0600870 /* give up the hardware semaphore */
Arnd Bergmann2a727f62018-12-10 22:49:10 +0100871 mutex_unlock(&operations_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 /* sleep for a short time just for good measure */
Jesse Barnes5c788a62009-12-04 15:18:01 -0800873out_sleep:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 msleep(100);
875 }
Arnd Bergmann2a727f62018-12-10 22:49:10 +0100876 complete(&exit_complete);
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800877 debug("%s - Exit\n", __func__);
Kristen Carlson Accardi2d100fe2007-08-14 16:07:00 -0700878 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
881
882/*----------------------------------------------------------------------
883* Name: process_changeinstatus
884*
885* Action: compare old and new slot status, process the change in status
886*
887* Input: pointer to slot struct, old slot struct
888*
889* Return 0 or error codes
890* Value:
891*
892* Side
893* Effects: None.
894*
895* Notes:
896*---------------------------------------------------------------------*/
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800897static int process_changeinstatus(struct slot *pslot, struct slot *poldslot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
899 u8 status;
900 int rc = 0;
Kristen Accardidc6712d2006-03-14 16:24:47 -0800901 u8 disable = 0;
902 u8 update = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800904 debug("process_changeinstatus - Entry pslot[%p], poldslot[%p]\n", pslot, poldslot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 // bit 0 - HPC_SLOT_POWER
907 if ((pslot->status & 0x01) != (poldslot->status & 0x01))
Kristen Accardidc6712d2006-03-14 16:24:47 -0800908 update = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 // bit 1 - HPC_SLOT_CONNECT
911 // ignore
912
913 // bit 2 - HPC_SLOT_ATTN
914 if ((pslot->status & 0x04) != (poldslot->status & 0x04))
Kristen Accardidc6712d2006-03-14 16:24:47 -0800915 update = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 // bit 3 - HPC_SLOT_PRSNT2
918 // bit 4 - HPC_SLOT_PRSNT1
919 if (((pslot->status & 0x08) != (poldslot->status & 0x08))
920 || ((pslot->status & 0x10) != (poldslot->status & 0x10)))
Kristen Accardidc6712d2006-03-14 16:24:47 -0800921 update = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 // bit 5 - HPC_SLOT_PWRGD
924 if ((pslot->status & 0x20) != (poldslot->status & 0x20))
925 // OFF -> ON: ignore, ON -> OFF: disable slot
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800926 if ((poldslot->status & 0x20) && (SLOT_CONNECT(poldslot->status) == HPC_SLOT_CONNECTED) && (SLOT_PRESENT(poldslot->status)))
Kristen Accardidc6712d2006-03-14 16:24:47 -0800927 disable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 // bit 6 - HPC_SLOT_BUS_SPEED
930 // ignore
931
932 // bit 7 - HPC_SLOT_LATCH
933 if ((pslot->status & 0x80) != (poldslot->status & 0x80)) {
Kristen Accardidc6712d2006-03-14 16:24:47 -0800934 update = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 // OPEN -> CLOSE
936 if (pslot->status & 0x80) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800937 if (SLOT_PWRGD(pslot->status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 // power goes on and off after closing latch
939 // check again to make sure power is still ON
940 msleep(1000);
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800941 rc = ibmphp_hpc_readslot(pslot, READ_SLOTSTATUS, &status);
942 if (SLOT_PWRGD(status))
Kristen Accardidc6712d2006-03-14 16:24:47 -0800943 update = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 else // overwrite power in pslot to OFF
945 pslot->status &= ~HPC_SLOT_POWER;
946 }
947 }
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700948 // CLOSE -> OPEN
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800949 else if ((SLOT_PWRGD(poldslot->status) == HPC_SLOT_PWRGD_GOOD)
950 && (SLOT_CONNECT(poldslot->status) == HPC_SLOT_CONNECTED) && (SLOT_PRESENT(poldslot->status))) {
Kristen Accardidc6712d2006-03-14 16:24:47 -0800951 disable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 }
953 // else - ignore
954 }
955 // bit 4 - HPC_SLOT_BLINK_ATTN
956 if ((pslot->ext_status & 0x08) != (poldslot->ext_status & 0x08))
Kristen Accardidc6712d2006-03-14 16:24:47 -0800957 update = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 if (disable) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800960 debug("process_changeinstatus - disable slot\n");
Kristen Accardidc6712d2006-03-14 16:24:47 -0800961 pslot->flag = 0;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800962 rc = ibmphp_do_disable_slot(pslot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
964
Quentin Lambert656f9782014-09-07 20:02:47 +0200965 if (update || disable)
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800966 ibmphp_update_slot_info(pslot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800968 debug("%s - Exit rc[%d] disable[%x] update[%x]\n", __func__, rc, disable, update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 return rc;
971}
972
973/*----------------------------------------------------------------------
974* Name: process_changeinlatch
975*
976* Action: compare old and new latch reg status, process the change
977*
978* Input: old and current latch register status
979*
980* Return 0 or error codes
981* Value:
982*---------------------------------------------------------------------*/
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800983static int process_changeinlatch(u8 old, u8 new, struct controller *ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
985 struct slot myslot, *pslot;
986 u8 i;
987 u8 mask;
988 int rc = 0;
989
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800990 debug("%s - Entry old[%x], new[%x]\n", __func__, old, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 // bit 0 reserved, 0 is LSB, check bit 1-6 for 6 slots
992
993 for (i = ctrl->starting_slot_num; i <= ctrl->ending_slot_num; i++) {
994 mask = 0x01 << i;
995 if ((mask & old) != (mask & new)) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800996 pslot = ibmphp_get_slot_from_physical_num(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 if (pslot) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800998 memcpy((void *) &myslot, (void *) pslot, sizeof(struct slot));
999 rc = ibmphp_hpc_readslot(pslot, READ_ALLSTAT, NULL);
1000 debug("%s - call process_changeinstatus for slot[%d]\n", __func__, i);
1001 process_changeinstatus(pslot, &myslot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 } else {
1003 rc = -EINVAL;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001004 err("%s - Error bad pointer for slot[%d]\n", __func__, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 }
1006 }
1007 }
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001008 debug("%s - Exit rc[%d]\n", __func__, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return rc;
1010}
1011
1012/*----------------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013* Name: ibmphp_hpc_start_poll_thread
1014*
1015* Action: start polling thread
1016*---------------------------------------------------------------------*/
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001017int __init ibmphp_hpc_start_poll_thread(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001019 debug("%s - Entry\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Kristen Carlson Accardi2d100fe2007-08-14 16:07:00 -07001021 ibmphp_poll_thread = kthread_run(poll_hpc, NULL, "hpc_poll");
1022 if (IS_ERR(ibmphp_poll_thread)) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001023 err("%s - Error, thread not started\n", __func__);
Kristen Carlson Accardi2d100fe2007-08-14 16:07:00 -07001024 return PTR_ERR(ibmphp_poll_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
Kristen Carlson Accardi2d100fe2007-08-14 16:07:00 -07001026 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
1028
1029/*----------------------------------------------------------------------
1030* Name: ibmphp_hpc_stop_poll_thread
1031*
1032* Action: stop polling thread and cleanup
1033*---------------------------------------------------------------------*/
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001034void __exit ibmphp_hpc_stop_poll_thread(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001036 debug("%s - Entry\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Kristen Carlson Accardi2d100fe2007-08-14 16:07:00 -07001038 kthread_stop(ibmphp_poll_thread);
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001039 debug("before locking operations\n");
1040 ibmphp_lock_operations();
1041 debug("after locking operations\n");
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 // wait for poll thread to exit
Arnd Bergmann2a727f62018-12-10 22:49:10 +01001044 debug("before exit_complete down\n");
1045 wait_for_completion(&exit_complete);
1046 debug("after exit_completion down\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 // cleanup
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001049 debug("before free_hpc_access\n");
1050 free_hpc_access();
1051 debug("after free_hpc_access\n");
1052 ibmphp_unlock_operations();
1053 debug("after unlock operations\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001055 debug("%s - Exit\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056}
1057
1058/*----------------------------------------------------------------------
1059* Name: hpc_wait_ctlr_notworking
1060*
1061* Action: wait until the controller is in a not working state
1062*
1063* Return 0, HPC_ERROR
1064* Value:
1065*---------------------------------------------------------------------*/
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001066static int hpc_wait_ctlr_notworking(int timeout, struct controller *ctlr_ptr, void __iomem *wpg_bbar,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -04001067 u8 *pstatus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
1069 int rc = 0;
Kristen Accardidc6712d2006-03-14 16:24:47 -08001070 u8 done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001072 debug_polling("hpc_wait_ctlr_notworking - Entry timeout[%d]\n", timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 while (!done) {
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001075 *pstatus = ctrl_read(ctlr_ptr, wpg_bbar, WPG_CTLR_INDEX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 if (*pstatus == HPC_ERROR) {
1077 rc = HPC_ERROR;
Kristen Accardidc6712d2006-03-14 16:24:47 -08001078 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001080 if (CTLR_WORKING(*pstatus) == HPC_CTLR_WORKING_NO)
Kristen Accardidc6712d2006-03-14 16:24:47 -08001081 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (!done) {
1083 msleep(1000);
1084 if (timeout < 1) {
Kristen Accardidc6712d2006-03-14 16:24:47 -08001085 done = 1;
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001086 err("HPCreadslot - Error ctlr timeout\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 rc = HPC_ERROR;
1088 } else
1089 timeout--;
1090 }
1091 }
Bogicevic Sasaff3ce482015-12-27 13:21:11 -08001092 debug_polling("hpc_wait_ctlr_notworking - Exit rc[%x] status[%x]\n", rc, *pstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return rc;
1094}