ibmphp_hpc.c 33 KB

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