ibmphp_hpc.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  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. *
  11. * Send feedback to <gregkh@us.ibm.com>
  12. * <jshah@us.ibm.com>
  13. *
  14. */
  15. #include <linux/wait.h>
  16. #include <linux/time.h>
  17. #include <linux/delay.h>
  18. #include <linux/module.h>
  19. #include <linux/pci.h>
  20. #include <linux/init.h>
  21. #include <linux/mutex.h>
  22. #include <linux/sched.h>
  23. #include <linux/semaphore.h>
  24. #include <linux/kthread.h>
  25. #include "ibmphp.h"
  26. static int to_debug = 0;
  27. #define debug_polling(fmt, arg...) do { if (to_debug) debug(fmt, arg); } while (0)
  28. //----------------------------------------------------------------------------
  29. // timeout values
  30. //----------------------------------------------------------------------------
  31. #define CMD_COMPLETE_TOUT_SEC 60 // give HPC 60 sec to finish cmd
  32. #define HPC_CTLR_WORKING_TOUT 60 // give HPC 60 sec to finish cmd
  33. #define HPC_GETACCESS_TIMEOUT 60 // seconds
  34. #define POLL_INTERVAL_SEC 2 // poll HPC every 2 seconds
  35. #define POLL_LATCH_CNT 5 // poll latch 5 times, then poll slots
  36. //----------------------------------------------------------------------------
  37. // Winnipeg Architected Register Offsets
  38. //----------------------------------------------------------------------------
  39. #define WPG_I2CMBUFL_OFFSET 0x08 // I2C Message Buffer Low
  40. #define WPG_I2CMOSUP_OFFSET 0x10 // I2C Master Operation Setup Reg
  41. #define WPG_I2CMCNTL_OFFSET 0x20 // I2C Master Control Register
  42. #define WPG_I2CPARM_OFFSET 0x40 // I2C Parameter Register
  43. #define WPG_I2CSTAT_OFFSET 0x70 // I2C Status Register
  44. //----------------------------------------------------------------------------
  45. // Winnipeg Store Type commands (Add this commands to the register offset)
  46. //----------------------------------------------------------------------------
  47. #define WPG_I2C_AND 0x1000 // I2C AND operation
  48. #define WPG_I2C_OR 0x2000 // I2C OR operation
  49. //----------------------------------------------------------------------------
  50. // Command set for I2C Master Operation Setup Register
  51. //----------------------------------------------------------------------------
  52. #define WPG_READATADDR_MASK 0x00010000 // read,bytes,I2C shifted,index
  53. #define WPG_WRITEATADDR_MASK 0x40010000 // write,bytes,I2C shifted,index
  54. #define WPG_READDIRECT_MASK 0x10010000
  55. #define WPG_WRITEDIRECT_MASK 0x60010000
  56. //----------------------------------------------------------------------------
  57. // bit masks for I2C Master Control Register
  58. //----------------------------------------------------------------------------
  59. #define WPG_I2CMCNTL_STARTOP_MASK 0x00000002 // Start the Operation
  60. //----------------------------------------------------------------------------
  61. //
  62. //----------------------------------------------------------------------------
  63. #define WPG_I2C_IOREMAP_SIZE 0x2044 // size of linear address interval
  64. //----------------------------------------------------------------------------
  65. // command index
  66. //----------------------------------------------------------------------------
  67. #define WPG_1ST_SLOT_INDEX 0x01 // index - 1st slot for ctlr
  68. #define WPG_CTLR_INDEX 0x0F // index - ctlr
  69. #define WPG_1ST_EXTSLOT_INDEX 0x10 // index - 1st ext slot for ctlr
  70. #define WPG_1ST_BUS_INDEX 0x1F // index - 1st bus for ctlr
  71. //----------------------------------------------------------------------------
  72. // macro utilities
  73. //----------------------------------------------------------------------------
  74. // if bits 20,22,25,26,27,29,30 are OFF return 1
  75. #define HPC_I2CSTATUS_CHECK(s) ((u8)((s & 0x00000A76) ? 0 : 1))
  76. //----------------------------------------------------------------------------
  77. // global variables
  78. //----------------------------------------------------------------------------
  79. static struct mutex sem_hpcaccess; // lock access to HPC
  80. static struct semaphore semOperations; // lock all operations and
  81. // access to data structures
  82. static struct semaphore sem_exit; // make sure polling thread goes away
  83. static struct task_struct *ibmphp_poll_thread;
  84. //----------------------------------------------------------------------------
  85. // local function prototypes
  86. //----------------------------------------------------------------------------
  87. static u8 i2c_ctrl_read(struct controller *, void __iomem *, u8);
  88. static u8 i2c_ctrl_write(struct controller *, void __iomem *, u8, u8);
  89. static u8 hpc_writecmdtoindex(u8, u8);
  90. static u8 hpc_readcmdtoindex(u8, u8);
  91. static void get_hpc_access(void);
  92. static void free_hpc_access(void);
  93. static int poll_hpc(void *data);
  94. static int process_changeinstatus(struct slot *, struct slot *);
  95. static int process_changeinlatch(u8, u8, struct controller *);
  96. static int hpc_wait_ctlr_notworking(int, struct controller *, void __iomem *, u8 *);
  97. //----------------------------------------------------------------------------
  98. /*----------------------------------------------------------------------
  99. * Name: ibmphp_hpc_initvars
  100. *
  101. * Action: initialize semaphores and variables
  102. *---------------------------------------------------------------------*/
  103. void __init ibmphp_hpc_initvars(void)
  104. {
  105. debug("%s - Entry\n", __func__);
  106. mutex_init(&sem_hpcaccess);
  107. sema_init(&semOperations, 1);
  108. sema_init(&sem_exit, 0);
  109. to_debug = 0;
  110. debug("%s - Exit\n", __func__);
  111. }
  112. /*----------------------------------------------------------------------
  113. * Name: i2c_ctrl_read
  114. *
  115. * Action: read from HPC over I2C
  116. *
  117. *---------------------------------------------------------------------*/
  118. static u8 i2c_ctrl_read(struct controller *ctlr_ptr, void __iomem *WPGBbar, u8 index)
  119. {
  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. debug_polling("%s - Entry WPGBbar[%p] index[%x] \n", __func__, WPGBbar, index);
  127. //--------------------------------------------------------------------
  128. // READ - step 1
  129. // read at address, byte length, I2C address (shifted), index
  130. // or read direct, byte length, index
  131. if (ctlr_ptr->ctlr_type == 0x02) {
  132. data = WPG_READATADDR_MASK;
  133. // fill in I2C address
  134. ultemp = (unsigned long)ctlr_ptr->u.wpeg_ctlr.i2c_addr;
  135. ultemp = ultemp >> 1;
  136. data |= (ultemp << 8);
  137. // fill in index
  138. data |= (unsigned long)index;
  139. } else if (ctlr_ptr->ctlr_type == 0x04) {
  140. data = WPG_READDIRECT_MASK;
  141. // fill in index
  142. ultemp = (unsigned long)index;
  143. ultemp = ultemp << 8;
  144. data |= ultemp;
  145. } else {
  146. err("this controller type is not supported \n");
  147. return HPC_ERROR;
  148. }
  149. wpg_data = swab32(data); // swap data before writing
  150. wpg_addr = WPGBbar + WPG_I2CMOSUP_OFFSET;
  151. writel(wpg_data, wpg_addr);
  152. //--------------------------------------------------------------------
  153. // READ - step 2 : clear the message buffer
  154. data = 0x00000000;
  155. wpg_data = swab32(data);
  156. wpg_addr = WPGBbar + WPG_I2CMBUFL_OFFSET;
  157. writel(wpg_data, wpg_addr);
  158. //--------------------------------------------------------------------
  159. // READ - step 3 : issue start operation, I2C master control bit 30:ON
  160. // 2020 : [20] OR operation at [20] offset 0x20
  161. data = WPG_I2CMCNTL_STARTOP_MASK;
  162. wpg_data = swab32(data);
  163. wpg_addr = WPGBbar + WPG_I2CMCNTL_OFFSET + WPG_I2C_OR;
  164. writel(wpg_data, wpg_addr);
  165. //--------------------------------------------------------------------
  166. // READ - step 4 : wait until start operation bit clears
  167. i = CMD_COMPLETE_TOUT_SEC;
  168. while (i) {
  169. msleep(10);
  170. wpg_addr = WPGBbar + WPG_I2CMCNTL_OFFSET;
  171. wpg_data = readl(wpg_addr);
  172. data = swab32(wpg_data);
  173. if (!(data & WPG_I2CMCNTL_STARTOP_MASK))
  174. break;
  175. i--;
  176. }
  177. if (i == 0) {
  178. debug("%s - Error : WPG timeout\n", __func__);
  179. return HPC_ERROR;
  180. }
  181. //--------------------------------------------------------------------
  182. // READ - step 5 : read I2C status register
  183. i = CMD_COMPLETE_TOUT_SEC;
  184. while (i) {
  185. msleep(10);
  186. wpg_addr = WPGBbar + WPG_I2CSTAT_OFFSET;
  187. wpg_data = readl(wpg_addr);
  188. data = swab32(wpg_data);
  189. if (HPC_I2CSTATUS_CHECK(data))
  190. break;
  191. i--;
  192. }
  193. if (i == 0) {
  194. debug("ctrl_read - Exit Error:I2C timeout\n");
  195. return HPC_ERROR;
  196. }
  197. //--------------------------------------------------------------------
  198. // READ - step 6 : get DATA
  199. wpg_addr = WPGBbar + WPG_I2CMBUFL_OFFSET;
  200. wpg_data = readl(wpg_addr);
  201. data = swab32(wpg_data);
  202. status = (u8) data;
  203. debug_polling("%s - Exit index[%x] status[%x]\n", __func__, index, status);
  204. return (status);
  205. }
  206. /*----------------------------------------------------------------------
  207. * Name: i2c_ctrl_write
  208. *
  209. * Action: write to HPC over I2C
  210. *
  211. * Return 0 or error codes
  212. *---------------------------------------------------------------------*/
  213. static u8 i2c_ctrl_write(struct controller *ctlr_ptr, void __iomem *WPGBbar, u8 index, u8 cmd)
  214. {
  215. u8 rc;
  216. void __iomem *wpg_addr; // base addr + offset
  217. unsigned long wpg_data; // data to/from WPG LOHI format
  218. unsigned long ultemp;
  219. unsigned long data; // actual data HILO format
  220. int i;
  221. debug_polling("%s - Entry WPGBbar[%p] index[%x] cmd[%x]\n", __func__, WPGBbar, index, cmd);
  222. rc = 0;
  223. //--------------------------------------------------------------------
  224. // WRITE - step 1
  225. // write at address, byte length, I2C address (shifted), index
  226. // or write direct, byte length, index
  227. data = 0x00000000;
  228. if (ctlr_ptr->ctlr_type == 0x02) {
  229. data = WPG_WRITEATADDR_MASK;
  230. // fill in I2C address
  231. ultemp = (unsigned long)ctlr_ptr->u.wpeg_ctlr.i2c_addr;
  232. ultemp = ultemp >> 1;
  233. data |= (ultemp << 8);
  234. // fill in index
  235. data |= (unsigned long)index;
  236. } else if (ctlr_ptr->ctlr_type == 0x04) {
  237. data = WPG_WRITEDIRECT_MASK;
  238. // fill in index
  239. ultemp = (unsigned long)index;
  240. ultemp = ultemp << 8;
  241. data |= ultemp;
  242. } else {
  243. err("this controller type is not supported \n");
  244. return HPC_ERROR;
  245. }
  246. wpg_data = swab32(data); // swap data before writing
  247. wpg_addr = WPGBbar + WPG_I2CMOSUP_OFFSET;
  248. writel(wpg_data, wpg_addr);
  249. //--------------------------------------------------------------------
  250. // WRITE - step 2 : clear the message buffer
  251. data = 0x00000000 | (unsigned long)cmd;
  252. wpg_data = swab32(data);
  253. wpg_addr = WPGBbar + WPG_I2CMBUFL_OFFSET;
  254. writel(wpg_data, wpg_addr);
  255. //--------------------------------------------------------------------
  256. // WRITE - step 3 : issue start operation,I2C master control bit 30:ON
  257. // 2020 : [20] OR operation at [20] offset 0x20
  258. data = WPG_I2CMCNTL_STARTOP_MASK;
  259. wpg_data = swab32(data);
  260. wpg_addr = WPGBbar + WPG_I2CMCNTL_OFFSET + WPG_I2C_OR;
  261. writel(wpg_data, wpg_addr);
  262. //--------------------------------------------------------------------
  263. // WRITE - step 4 : wait until start operation bit clears
  264. i = CMD_COMPLETE_TOUT_SEC;
  265. while (i) {
  266. msleep(10);
  267. wpg_addr = WPGBbar + WPG_I2CMCNTL_OFFSET;
  268. wpg_data = readl(wpg_addr);
  269. data = swab32(wpg_data);
  270. if (!(data & WPG_I2CMCNTL_STARTOP_MASK))
  271. break;
  272. i--;
  273. }
  274. if (i == 0) {
  275. debug("%s - Exit Error:WPG timeout\n", __func__);
  276. rc = HPC_ERROR;
  277. }
  278. //--------------------------------------------------------------------
  279. // WRITE - step 5 : read I2C status register
  280. i = CMD_COMPLETE_TOUT_SEC;
  281. while (i) {
  282. msleep(10);
  283. wpg_addr = WPGBbar + WPG_I2CSTAT_OFFSET;
  284. wpg_data = readl(wpg_addr);
  285. data = swab32(wpg_data);
  286. if (HPC_I2CSTATUS_CHECK(data))
  287. break;
  288. i--;
  289. }
  290. if (i == 0) {
  291. debug("ctrl_read - Error : I2C timeout\n");
  292. rc = HPC_ERROR;
  293. }
  294. debug_polling("%s Exit rc[%x]\n", __func__, rc);
  295. return (rc);
  296. }
  297. //------------------------------------------------------------
  298. // Read from ISA type HPC
  299. //------------------------------------------------------------
  300. static u8 isa_ctrl_read(struct controller *ctlr_ptr, u8 offset)
  301. {
  302. u16 start_address;
  303. u16 end_address;
  304. u8 data;
  305. start_address = ctlr_ptr->u.isa_ctlr.io_start;
  306. end_address = ctlr_ptr->u.isa_ctlr.io_end;
  307. data = inb(start_address + offset);
  308. return data;
  309. }
  310. //--------------------------------------------------------------
  311. // Write to ISA type HPC
  312. //--------------------------------------------------------------
  313. static void isa_ctrl_write(struct controller *ctlr_ptr, u8 offset, u8 data)
  314. {
  315. u16 start_address;
  316. u16 port_address;
  317. start_address = ctlr_ptr->u.isa_ctlr.io_start;
  318. port_address = start_address + (u16) offset;
  319. outb(data, port_address);
  320. }
  321. static u8 pci_ctrl_read(struct controller *ctrl, u8 offset)
  322. {
  323. u8 data = 0x00;
  324. debug("inside pci_ctrl_read\n");
  325. if (ctrl->ctrl_dev)
  326. pci_read_config_byte(ctrl->ctrl_dev, HPC_PCI_OFFSET + offset, &data);
  327. return data;
  328. }
  329. static u8 pci_ctrl_write(struct controller *ctrl, u8 offset, u8 data)
  330. {
  331. u8 rc = -ENODEV;
  332. debug("inside pci_ctrl_write\n");
  333. if (ctrl->ctrl_dev) {
  334. pci_write_config_byte(ctrl->ctrl_dev, HPC_PCI_OFFSET + offset, data);
  335. rc = 0;
  336. }
  337. return rc;
  338. }
  339. static u8 ctrl_read(struct controller *ctlr, void __iomem *base, u8 offset)
  340. {
  341. u8 rc;
  342. switch (ctlr->ctlr_type) {
  343. case 0:
  344. rc = isa_ctrl_read(ctlr, offset);
  345. break;
  346. case 1:
  347. rc = pci_ctrl_read(ctlr, offset);
  348. break;
  349. case 2:
  350. case 4:
  351. rc = i2c_ctrl_read(ctlr, base, offset);
  352. break;
  353. default:
  354. return -ENODEV;
  355. }
  356. return rc;
  357. }
  358. static u8 ctrl_write(struct controller *ctlr, void __iomem *base, u8 offset, u8 data)
  359. {
  360. u8 rc = 0;
  361. switch (ctlr->ctlr_type) {
  362. case 0:
  363. isa_ctrl_write(ctlr, offset, data);
  364. break;
  365. case 1:
  366. rc = pci_ctrl_write(ctlr, offset, data);
  367. break;
  368. case 2:
  369. case 4:
  370. rc = i2c_ctrl_write(ctlr, base, offset, data);
  371. break;
  372. default:
  373. return -ENODEV;
  374. }
  375. return rc;
  376. }
  377. /*----------------------------------------------------------------------
  378. * Name: hpc_writecmdtoindex()
  379. *
  380. * Action: convert a write command to proper index within a controller
  381. *
  382. * Return index, HPC_ERROR
  383. *---------------------------------------------------------------------*/
  384. static u8 hpc_writecmdtoindex(u8 cmd, u8 index)
  385. {
  386. u8 rc;
  387. switch (cmd) {
  388. case HPC_CTLR_ENABLEIRQ: // 0x00.N.15
  389. case HPC_CTLR_CLEARIRQ: // 0x06.N.15
  390. case HPC_CTLR_RESET: // 0x07.N.15
  391. case HPC_CTLR_IRQSTEER: // 0x08.N.15
  392. case HPC_CTLR_DISABLEIRQ: // 0x01.N.15
  393. case HPC_ALLSLOT_ON: // 0x11.N.15
  394. case HPC_ALLSLOT_OFF: // 0x12.N.15
  395. rc = 0x0F;
  396. break;
  397. case HPC_SLOT_OFF: // 0x02.Y.0-14
  398. case HPC_SLOT_ON: // 0x03.Y.0-14
  399. case HPC_SLOT_ATTNOFF: // 0x04.N.0-14
  400. case HPC_SLOT_ATTNON: // 0x05.N.0-14
  401. case HPC_SLOT_BLINKLED: // 0x13.N.0-14
  402. rc = index;
  403. break;
  404. case HPC_BUS_33CONVMODE:
  405. case HPC_BUS_66CONVMODE:
  406. case HPC_BUS_66PCIXMODE:
  407. case HPC_BUS_100PCIXMODE:
  408. case HPC_BUS_133PCIXMODE:
  409. rc = index + WPG_1ST_BUS_INDEX - 1;
  410. break;
  411. default:
  412. err("hpc_writecmdtoindex - Error invalid cmd[%x]\n", cmd);
  413. rc = HPC_ERROR;
  414. }
  415. return rc;
  416. }
  417. /*----------------------------------------------------------------------
  418. * Name: hpc_readcmdtoindex()
  419. *
  420. * Action: convert a read command to proper index within a controller
  421. *
  422. * Return index, HPC_ERROR
  423. *---------------------------------------------------------------------*/
  424. static u8 hpc_readcmdtoindex(u8 cmd, u8 index)
  425. {
  426. u8 rc;
  427. switch (cmd) {
  428. case READ_CTLRSTATUS:
  429. rc = 0x0F;
  430. break;
  431. case READ_SLOTSTATUS:
  432. case READ_ALLSTAT:
  433. rc = index;
  434. break;
  435. case READ_EXTSLOTSTATUS:
  436. rc = index + WPG_1ST_EXTSLOT_INDEX;
  437. break;
  438. case READ_BUSSTATUS:
  439. rc = index + WPG_1ST_BUS_INDEX - 1;
  440. break;
  441. case READ_SLOTLATCHLOWREG:
  442. rc = 0x28;
  443. break;
  444. case READ_REVLEVEL:
  445. rc = 0x25;
  446. break;
  447. case READ_HPCOPTIONS:
  448. rc = 0x27;
  449. break;
  450. default:
  451. rc = HPC_ERROR;
  452. }
  453. return rc;
  454. }
  455. /*----------------------------------------------------------------------
  456. * Name: HPCreadslot()
  457. *
  458. * Action: issue a READ command to HPC
  459. *
  460. * Input: pslot - cannot be NULL for READ_ALLSTAT
  461. * pstatus - can be NULL for READ_ALLSTAT
  462. *
  463. * Return 0 or error codes
  464. *---------------------------------------------------------------------*/
  465. int ibmphp_hpc_readslot(struct slot *pslot, u8 cmd, u8 *pstatus)
  466. {
  467. void __iomem *wpg_bbar = NULL;
  468. struct controller *ctlr_ptr;
  469. u8 index, status;
  470. int rc = 0;
  471. int busindex;
  472. debug_polling("%s - Entry pslot[%p] cmd[%x] pstatus[%p]\n", __func__, pslot, cmd, pstatus);
  473. if ((pslot == NULL)
  474. || ((pstatus == NULL) && (cmd != READ_ALLSTAT) && (cmd != READ_BUSSTATUS))) {
  475. rc = -EINVAL;
  476. err("%s - Error invalid pointer, rc[%d]\n", __func__, rc);
  477. return rc;
  478. }
  479. if (cmd == READ_BUSSTATUS) {
  480. busindex = ibmphp_get_bus_index(pslot->bus);
  481. if (busindex < 0) {
  482. rc = -EINVAL;
  483. err("%s - Exit Error:invalid bus, rc[%d]\n", __func__, rc);
  484. return rc;
  485. } else
  486. index = (u8) busindex;
  487. } else
  488. index = pslot->ctlr_index;
  489. index = hpc_readcmdtoindex(cmd, index);
  490. if (index == HPC_ERROR) {
  491. rc = -EINVAL;
  492. err("%s - Exit Error:invalid index, rc[%d]\n", __func__, rc);
  493. return rc;
  494. }
  495. ctlr_ptr = pslot->ctrl;
  496. get_hpc_access();
  497. //--------------------------------------------------------------------
  498. // map physical address to logical address
  499. //--------------------------------------------------------------------
  500. if ((ctlr_ptr->ctlr_type == 2) || (ctlr_ptr->ctlr_type == 4))
  501. wpg_bbar = ioremap(ctlr_ptr->u.wpeg_ctlr.wpegbbar, WPG_I2C_IOREMAP_SIZE);
  502. //--------------------------------------------------------------------
  503. // check controller status before reading
  504. //--------------------------------------------------------------------
  505. rc = hpc_wait_ctlr_notworking(HPC_CTLR_WORKING_TOUT, ctlr_ptr, wpg_bbar, &status);
  506. if (!rc) {
  507. switch (cmd) {
  508. case READ_ALLSTAT:
  509. // update the slot structure
  510. pslot->ctrl->status = status;
  511. pslot->status = ctrl_read(ctlr_ptr, wpg_bbar, index);
  512. rc = hpc_wait_ctlr_notworking(HPC_CTLR_WORKING_TOUT, ctlr_ptr, wpg_bbar,
  513. &status);
  514. if (!rc)
  515. pslot->ext_status = ctrl_read(ctlr_ptr, wpg_bbar, index + WPG_1ST_EXTSLOT_INDEX);
  516. break;
  517. case READ_SLOTSTATUS:
  518. // DO NOT update the slot structure
  519. *pstatus = ctrl_read(ctlr_ptr, wpg_bbar, index);
  520. break;
  521. case READ_EXTSLOTSTATUS:
  522. // DO NOT update the slot structure
  523. *pstatus = ctrl_read(ctlr_ptr, wpg_bbar, index);
  524. break;
  525. case READ_CTLRSTATUS:
  526. // DO NOT update the slot structure
  527. *pstatus = status;
  528. break;
  529. case READ_BUSSTATUS:
  530. pslot->busstatus = ctrl_read(ctlr_ptr, wpg_bbar, index);
  531. break;
  532. case READ_REVLEVEL:
  533. *pstatus = ctrl_read(ctlr_ptr, wpg_bbar, index);
  534. break;
  535. case READ_HPCOPTIONS:
  536. *pstatus = ctrl_read(ctlr_ptr, wpg_bbar, index);
  537. break;
  538. case READ_SLOTLATCHLOWREG:
  539. // DO NOT update the slot structure
  540. *pstatus = ctrl_read(ctlr_ptr, wpg_bbar, index);
  541. break;
  542. // Not used
  543. case READ_ALLSLOT:
  544. list_for_each_entry(pslot, &ibmphp_slot_head,
  545. ibm_slot_list) {
  546. index = pslot->ctlr_index;
  547. rc = hpc_wait_ctlr_notworking(HPC_CTLR_WORKING_TOUT, ctlr_ptr,
  548. wpg_bbar, &status);
  549. if (!rc) {
  550. pslot->status = ctrl_read(ctlr_ptr, wpg_bbar, index);
  551. rc = hpc_wait_ctlr_notworking(HPC_CTLR_WORKING_TOUT,
  552. ctlr_ptr, wpg_bbar, &status);
  553. if (!rc)
  554. pslot->ext_status =
  555. ctrl_read(ctlr_ptr, wpg_bbar,
  556. index + WPG_1ST_EXTSLOT_INDEX);
  557. } else {
  558. err("%s - Error ctrl_read failed\n", __func__);
  559. rc = -EINVAL;
  560. break;
  561. }
  562. }
  563. break;
  564. default:
  565. rc = -EINVAL;
  566. break;
  567. }
  568. }
  569. //--------------------------------------------------------------------
  570. // cleanup
  571. //--------------------------------------------------------------------
  572. // remove physical to logical address mapping
  573. if ((ctlr_ptr->ctlr_type == 2) || (ctlr_ptr->ctlr_type == 4))
  574. iounmap(wpg_bbar);
  575. free_hpc_access();
  576. debug_polling("%s - Exit rc[%d]\n", __func__, rc);
  577. return rc;
  578. }
  579. /*----------------------------------------------------------------------
  580. * Name: ibmphp_hpc_writeslot()
  581. *
  582. * Action: issue a WRITE command to HPC
  583. *---------------------------------------------------------------------*/
  584. int ibmphp_hpc_writeslot(struct slot *pslot, u8 cmd)
  585. {
  586. void __iomem *wpg_bbar = NULL;
  587. struct controller *ctlr_ptr;
  588. u8 index, status;
  589. int busindex;
  590. u8 done;
  591. int rc = 0;
  592. int timeout;
  593. debug_polling("%s - Entry pslot[%p] cmd[%x]\n", __func__, pslot, cmd);
  594. if (pslot == NULL) {
  595. rc = -EINVAL;
  596. err("%s - Error Exit rc[%d]\n", __func__, rc);
  597. return rc;
  598. }
  599. if ((cmd == HPC_BUS_33CONVMODE) || (cmd == HPC_BUS_66CONVMODE) ||
  600. (cmd == HPC_BUS_66PCIXMODE) || (cmd == HPC_BUS_100PCIXMODE) ||
  601. (cmd == HPC_BUS_133PCIXMODE)) {
  602. busindex = ibmphp_get_bus_index(pslot->bus);
  603. if (busindex < 0) {
  604. rc = -EINVAL;
  605. err("%s - Exit Error:invalid bus, rc[%d]\n", __func__, rc);
  606. return rc;
  607. } else
  608. index = (u8) busindex;
  609. } else
  610. index = pslot->ctlr_index;
  611. index = hpc_writecmdtoindex(cmd, index);
  612. if (index == HPC_ERROR) {
  613. rc = -EINVAL;
  614. err("%s - Error Exit rc[%d]\n", __func__, rc);
  615. return rc;
  616. }
  617. ctlr_ptr = pslot->ctrl;
  618. get_hpc_access();
  619. //--------------------------------------------------------------------
  620. // map physical address to logical address
  621. //--------------------------------------------------------------------
  622. if ((ctlr_ptr->ctlr_type == 2) || (ctlr_ptr->ctlr_type == 4)) {
  623. wpg_bbar = ioremap(ctlr_ptr->u.wpeg_ctlr.wpegbbar, WPG_I2C_IOREMAP_SIZE);
  624. debug("%s - ctlr id[%x] physical[%lx] logical[%lx] i2c[%x]\n", __func__,
  625. ctlr_ptr->ctlr_id, (ulong) (ctlr_ptr->u.wpeg_ctlr.wpegbbar), (ulong) wpg_bbar,
  626. ctlr_ptr->u.wpeg_ctlr.i2c_addr);
  627. }
  628. //--------------------------------------------------------------------
  629. // check controller status before writing
  630. //--------------------------------------------------------------------
  631. rc = hpc_wait_ctlr_notworking(HPC_CTLR_WORKING_TOUT, ctlr_ptr, wpg_bbar, &status);
  632. if (!rc) {
  633. ctrl_write(ctlr_ptr, wpg_bbar, index, cmd);
  634. //--------------------------------------------------------------------
  635. // check controller is still not working on the command
  636. //--------------------------------------------------------------------
  637. timeout = CMD_COMPLETE_TOUT_SEC;
  638. done = 0;
  639. while (!done) {
  640. rc = hpc_wait_ctlr_notworking(HPC_CTLR_WORKING_TOUT, ctlr_ptr, wpg_bbar,
  641. &status);
  642. if (!rc) {
  643. if (NEEDTOCHECK_CMDSTATUS(cmd)) {
  644. if (CTLR_FINISHED(status) == HPC_CTLR_FINISHED_YES)
  645. done = 1;
  646. } else
  647. done = 1;
  648. }
  649. if (!done) {
  650. msleep(1000);
  651. if (timeout < 1) {
  652. done = 1;
  653. err("%s - Error command complete timeout\n", __func__);
  654. rc = -EFAULT;
  655. } else
  656. timeout--;
  657. }
  658. }
  659. ctlr_ptr->status = status;
  660. }
  661. // cleanup
  662. // remove physical to logical address mapping
  663. if ((ctlr_ptr->ctlr_type == 2) || (ctlr_ptr->ctlr_type == 4))
  664. iounmap(wpg_bbar);
  665. free_hpc_access();
  666. debug_polling("%s - Exit rc[%d]\n", __func__, rc);
  667. return rc;
  668. }
  669. /*----------------------------------------------------------------------
  670. * Name: get_hpc_access()
  671. *
  672. * Action: make sure only one process can access HPC at one time
  673. *---------------------------------------------------------------------*/
  674. static void get_hpc_access(void)
  675. {
  676. mutex_lock(&sem_hpcaccess);
  677. }
  678. /*----------------------------------------------------------------------
  679. * Name: free_hpc_access()
  680. *---------------------------------------------------------------------*/
  681. void free_hpc_access(void)
  682. {
  683. mutex_unlock(&sem_hpcaccess);
  684. }
  685. /*----------------------------------------------------------------------
  686. * Name: ibmphp_lock_operations()
  687. *
  688. * Action: make sure only one process can change the data structure
  689. *---------------------------------------------------------------------*/
  690. void ibmphp_lock_operations(void)
  691. {
  692. down(&semOperations);
  693. to_debug = 1;
  694. }
  695. /*----------------------------------------------------------------------
  696. * Name: ibmphp_unlock_operations()
  697. *---------------------------------------------------------------------*/
  698. void ibmphp_unlock_operations(void)
  699. {
  700. debug("%s - Entry\n", __func__);
  701. up(&semOperations);
  702. to_debug = 0;
  703. debug("%s - Exit\n", __func__);
  704. }
  705. /*----------------------------------------------------------------------
  706. * Name: poll_hpc()
  707. *---------------------------------------------------------------------*/
  708. #define POLL_LATCH_REGISTER 0
  709. #define POLL_SLOTS 1
  710. #define POLL_SLEEP 2
  711. static int poll_hpc(void *data)
  712. {
  713. struct slot myslot;
  714. struct slot *pslot = NULL;
  715. int rc;
  716. int poll_state = POLL_LATCH_REGISTER;
  717. u8 oldlatchlow = 0x00;
  718. u8 curlatchlow = 0x00;
  719. int poll_count = 0;
  720. u8 ctrl_count = 0x00;
  721. debug("%s - Entry\n", __func__);
  722. while (!kthread_should_stop()) {
  723. /* try to get the lock to do some kind of hardware access */
  724. down(&semOperations);
  725. switch (poll_state) {
  726. case POLL_LATCH_REGISTER:
  727. oldlatchlow = curlatchlow;
  728. ctrl_count = 0x00;
  729. list_for_each_entry(pslot, &ibmphp_slot_head,
  730. ibm_slot_list) {
  731. if (ctrl_count >= ibmphp_get_total_controllers())
  732. break;
  733. if (pslot->ctrl->ctlr_relative_id == ctrl_count) {
  734. ctrl_count++;
  735. if (READ_SLOT_LATCH(pslot->ctrl)) {
  736. rc = ibmphp_hpc_readslot(pslot,
  737. READ_SLOTLATCHLOWREG,
  738. &curlatchlow);
  739. if (oldlatchlow != curlatchlow)
  740. process_changeinlatch(oldlatchlow,
  741. curlatchlow,
  742. pslot->ctrl);
  743. }
  744. }
  745. }
  746. ++poll_count;
  747. poll_state = POLL_SLEEP;
  748. break;
  749. case POLL_SLOTS:
  750. list_for_each_entry(pslot, &ibmphp_slot_head,
  751. ibm_slot_list) {
  752. // make a copy of the old status
  753. memcpy((void *) &myslot, (void *) pslot,
  754. sizeof(struct slot));
  755. rc = ibmphp_hpc_readslot(pslot, READ_ALLSTAT, NULL);
  756. if ((myslot.status != pslot->status)
  757. || (myslot.ext_status != pslot->ext_status))
  758. process_changeinstatus(pslot, &myslot);
  759. }
  760. ctrl_count = 0x00;
  761. list_for_each_entry(pslot, &ibmphp_slot_head,
  762. ibm_slot_list) {
  763. if (ctrl_count >= ibmphp_get_total_controllers())
  764. break;
  765. if (pslot->ctrl->ctlr_relative_id == ctrl_count) {
  766. ctrl_count++;
  767. if (READ_SLOT_LATCH(pslot->ctrl))
  768. rc = ibmphp_hpc_readslot(pslot,
  769. READ_SLOTLATCHLOWREG,
  770. &curlatchlow);
  771. }
  772. }
  773. ++poll_count;
  774. poll_state = POLL_SLEEP;
  775. break;
  776. case POLL_SLEEP:
  777. /* don't sleep with a lock on the hardware */
  778. up(&semOperations);
  779. msleep(POLL_INTERVAL_SEC * 1000);
  780. if (kthread_should_stop())
  781. goto out_sleep;
  782. down(&semOperations);
  783. if (poll_count >= POLL_LATCH_CNT) {
  784. poll_count = 0;
  785. poll_state = POLL_SLOTS;
  786. } else
  787. poll_state = POLL_LATCH_REGISTER;
  788. break;
  789. }
  790. /* give up the hardware semaphore */
  791. up(&semOperations);
  792. /* sleep for a short time just for good measure */
  793. out_sleep:
  794. msleep(100);
  795. }
  796. up(&sem_exit);
  797. debug("%s - Exit\n", __func__);
  798. return 0;
  799. }
  800. /*----------------------------------------------------------------------
  801. * Name: process_changeinstatus
  802. *
  803. * Action: compare old and new slot status, process the change in status
  804. *
  805. * Input: pointer to slot struct, old slot struct
  806. *
  807. * Return 0 or error codes
  808. * Value:
  809. *
  810. * Side
  811. * Effects: None.
  812. *
  813. * Notes:
  814. *---------------------------------------------------------------------*/
  815. static int process_changeinstatus(struct slot *pslot, struct slot *poldslot)
  816. {
  817. u8 status;
  818. int rc = 0;
  819. u8 disable = 0;
  820. u8 update = 0;
  821. debug("process_changeinstatus - Entry pslot[%p], poldslot[%p]\n", pslot, poldslot);
  822. // bit 0 - HPC_SLOT_POWER
  823. if ((pslot->status & 0x01) != (poldslot->status & 0x01))
  824. update = 1;
  825. // bit 1 - HPC_SLOT_CONNECT
  826. // ignore
  827. // bit 2 - HPC_SLOT_ATTN
  828. if ((pslot->status & 0x04) != (poldslot->status & 0x04))
  829. update = 1;
  830. // bit 3 - HPC_SLOT_PRSNT2
  831. // bit 4 - HPC_SLOT_PRSNT1
  832. if (((pslot->status & 0x08) != (poldslot->status & 0x08))
  833. || ((pslot->status & 0x10) != (poldslot->status & 0x10)))
  834. update = 1;
  835. // bit 5 - HPC_SLOT_PWRGD
  836. if ((pslot->status & 0x20) != (poldslot->status & 0x20))
  837. // OFF -> ON: ignore, ON -> OFF: disable slot
  838. if ((poldslot->status & 0x20) && (SLOT_CONNECT(poldslot->status) == HPC_SLOT_CONNECTED) && (SLOT_PRESENT(poldslot->status)))
  839. disable = 1;
  840. // bit 6 - HPC_SLOT_BUS_SPEED
  841. // ignore
  842. // bit 7 - HPC_SLOT_LATCH
  843. if ((pslot->status & 0x80) != (poldslot->status & 0x80)) {
  844. update = 1;
  845. // OPEN -> CLOSE
  846. if (pslot->status & 0x80) {
  847. if (SLOT_PWRGD(pslot->status)) {
  848. // power goes on and off after closing latch
  849. // check again to make sure power is still ON
  850. msleep(1000);
  851. rc = ibmphp_hpc_readslot(pslot, READ_SLOTSTATUS, &status);
  852. if (SLOT_PWRGD(status))
  853. update = 1;
  854. else // overwrite power in pslot to OFF
  855. pslot->status &= ~HPC_SLOT_POWER;
  856. }
  857. }
  858. // CLOSE -> OPEN
  859. else if ((SLOT_PWRGD(poldslot->status) == HPC_SLOT_PWRGD_GOOD)
  860. && (SLOT_CONNECT(poldslot->status) == HPC_SLOT_CONNECTED) && (SLOT_PRESENT(poldslot->status))) {
  861. disable = 1;
  862. }
  863. // else - ignore
  864. }
  865. // bit 4 - HPC_SLOT_BLINK_ATTN
  866. if ((pslot->ext_status & 0x08) != (poldslot->ext_status & 0x08))
  867. update = 1;
  868. if (disable) {
  869. debug("process_changeinstatus - disable slot\n");
  870. pslot->flag = 0;
  871. rc = ibmphp_do_disable_slot(pslot);
  872. }
  873. if (update || disable)
  874. ibmphp_update_slot_info(pslot);
  875. debug("%s - Exit rc[%d] disable[%x] update[%x]\n", __func__, rc, disable, update);
  876. return rc;
  877. }
  878. /*----------------------------------------------------------------------
  879. * Name: process_changeinlatch
  880. *
  881. * Action: compare old and new latch reg status, process the change
  882. *
  883. * Input: old and current latch register status
  884. *
  885. * Return 0 or error codes
  886. * Value:
  887. *---------------------------------------------------------------------*/
  888. static int process_changeinlatch(u8 old, u8 new, struct controller *ctrl)
  889. {
  890. struct slot myslot, *pslot;
  891. u8 i;
  892. u8 mask;
  893. int rc = 0;
  894. debug("%s - Entry old[%x], new[%x]\n", __func__, old, new);
  895. // bit 0 reserved, 0 is LSB, check bit 1-6 for 6 slots
  896. for (i = ctrl->starting_slot_num; i <= ctrl->ending_slot_num; i++) {
  897. mask = 0x01 << i;
  898. if ((mask & old) != (mask & new)) {
  899. pslot = ibmphp_get_slot_from_physical_num(i);
  900. if (pslot) {
  901. memcpy((void *) &myslot, (void *) pslot, sizeof(struct slot));
  902. rc = ibmphp_hpc_readslot(pslot, READ_ALLSTAT, NULL);
  903. debug("%s - call process_changeinstatus for slot[%d]\n", __func__, i);
  904. process_changeinstatus(pslot, &myslot);
  905. } else {
  906. rc = -EINVAL;
  907. err("%s - Error bad pointer for slot[%d]\n", __func__, i);
  908. }
  909. }
  910. }
  911. debug("%s - Exit rc[%d]\n", __func__, rc);
  912. return rc;
  913. }
  914. /*----------------------------------------------------------------------
  915. * Name: ibmphp_hpc_start_poll_thread
  916. *
  917. * Action: start polling thread
  918. *---------------------------------------------------------------------*/
  919. int __init ibmphp_hpc_start_poll_thread(void)
  920. {
  921. debug("%s - Entry\n", __func__);
  922. ibmphp_poll_thread = kthread_run(poll_hpc, NULL, "hpc_poll");
  923. if (IS_ERR(ibmphp_poll_thread)) {
  924. err("%s - Error, thread not started\n", __func__);
  925. return PTR_ERR(ibmphp_poll_thread);
  926. }
  927. return 0;
  928. }
  929. /*----------------------------------------------------------------------
  930. * Name: ibmphp_hpc_stop_poll_thread
  931. *
  932. * Action: stop polling thread and cleanup
  933. *---------------------------------------------------------------------*/
  934. void __exit ibmphp_hpc_stop_poll_thread(void)
  935. {
  936. debug("%s - Entry\n", __func__);
  937. kthread_stop(ibmphp_poll_thread);
  938. debug("before locking operations\n");
  939. ibmphp_lock_operations();
  940. debug("after locking operations\n");
  941. // wait for poll thread to exit
  942. debug("before sem_exit down\n");
  943. down(&sem_exit);
  944. debug("after sem_exit down\n");
  945. // cleanup
  946. debug("before free_hpc_access\n");
  947. free_hpc_access();
  948. debug("after free_hpc_access\n");
  949. ibmphp_unlock_operations();
  950. debug("after unlock operations\n");
  951. up(&sem_exit);
  952. debug("after sem exit up\n");
  953. debug("%s - Exit\n", __func__);
  954. }
  955. /*----------------------------------------------------------------------
  956. * Name: hpc_wait_ctlr_notworking
  957. *
  958. * Action: wait until the controller is in a not working state
  959. *
  960. * Return 0, HPC_ERROR
  961. * Value:
  962. *---------------------------------------------------------------------*/
  963. static int hpc_wait_ctlr_notworking(int timeout, struct controller *ctlr_ptr, void __iomem *wpg_bbar,
  964. u8 *pstatus)
  965. {
  966. int rc = 0;
  967. u8 done = 0;
  968. debug_polling("hpc_wait_ctlr_notworking - Entry timeout[%d]\n", timeout);
  969. while (!done) {
  970. *pstatus = ctrl_read(ctlr_ptr, wpg_bbar, WPG_CTLR_INDEX);
  971. if (*pstatus == HPC_ERROR) {
  972. rc = HPC_ERROR;
  973. done = 1;
  974. }
  975. if (CTLR_WORKING(*pstatus) == HPC_CTLR_WORKING_NO)
  976. done = 1;
  977. if (!done) {
  978. msleep(1000);
  979. if (timeout < 1) {
  980. done = 1;
  981. err("HPCreadslot - Error ctlr timeout\n");
  982. rc = HPC_ERROR;
  983. } else
  984. timeout--;
  985. }
  986. }
  987. debug_polling("hpc_wait_ctlr_notworking - Exit rc[%x] status[%x]\n", rc, *pstatus);
  988. return rc;
  989. }