ibmphp_hpc.c 33 KB

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