ibmphp_hpc.c 33 KB

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