ibmphp_hpc.c 32 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/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. u8 index, status;
  484. int rc = 0;
  485. int busindex;
  486. debug_polling("%s - Entry pslot[%p] cmd[%x] pstatus[%p]\n", __func__, 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", __func__, 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", __func__, 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", __func__, 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_entry(pslot, &ibmphp_slot_head,
  559. 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", __func__);
  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", __func__, 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", __func__, pslot, cmd);
  608. if (pslot == NULL) {
  609. rc = -EINVAL;
  610. err("%s - Error Exit rc[%d]\n", __func__, 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", __func__, 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", __func__, 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", __func__,
  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", __func__);
  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", __func__, 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", __func__);
  715. up(&semOperations);
  716. to_debug = 0;
  717. debug("%s - Exit\n", __func__);
  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. int rc;
  730. int poll_state = POLL_LATCH_REGISTER;
  731. u8 oldlatchlow = 0x00;
  732. u8 curlatchlow = 0x00;
  733. int poll_count = 0;
  734. u8 ctrl_count = 0x00;
  735. debug("%s - Entry\n", __func__);
  736. while (!kthread_should_stop()) {
  737. /* try to get the lock to do some kind of hardware access */
  738. down(&semOperations);
  739. switch (poll_state) {
  740. case POLL_LATCH_REGISTER:
  741. oldlatchlow = curlatchlow;
  742. ctrl_count = 0x00;
  743. list_for_each_entry(pslot, &ibmphp_slot_head,
  744. ibm_slot_list) {
  745. if (ctrl_count >= ibmphp_get_total_controllers())
  746. break;
  747. if (pslot->ctrl->ctlr_relative_id == ctrl_count) {
  748. ctrl_count++;
  749. if (READ_SLOT_LATCH(pslot->ctrl)) {
  750. rc = ibmphp_hpc_readslot(pslot,
  751. READ_SLOTLATCHLOWREG,
  752. &curlatchlow);
  753. if (oldlatchlow != curlatchlow)
  754. process_changeinlatch(oldlatchlow,
  755. curlatchlow,
  756. pslot->ctrl);
  757. }
  758. }
  759. }
  760. ++poll_count;
  761. poll_state = POLL_SLEEP;
  762. break;
  763. case POLL_SLOTS:
  764. list_for_each_entry(pslot, &ibmphp_slot_head,
  765. ibm_slot_list) {
  766. // make a copy of the old status
  767. memcpy((void *) &myslot, (void *) pslot,
  768. sizeof(struct slot));
  769. rc = ibmphp_hpc_readslot(pslot, READ_ALLSTAT, NULL);
  770. if ((myslot.status != pslot->status)
  771. || (myslot.ext_status != pslot->ext_status))
  772. process_changeinstatus(pslot, &myslot);
  773. }
  774. ctrl_count = 0x00;
  775. list_for_each_entry(pslot, &ibmphp_slot_head,
  776. ibm_slot_list) {
  777. if (ctrl_count >= ibmphp_get_total_controllers())
  778. break;
  779. if (pslot->ctrl->ctlr_relative_id == ctrl_count) {
  780. ctrl_count++;
  781. if (READ_SLOT_LATCH(pslot->ctrl))
  782. rc = ibmphp_hpc_readslot(pslot,
  783. READ_SLOTLATCHLOWREG,
  784. &curlatchlow);
  785. }
  786. }
  787. ++poll_count;
  788. poll_state = POLL_SLEEP;
  789. break;
  790. case POLL_SLEEP:
  791. /* don't sleep with a lock on the hardware */
  792. up(&semOperations);
  793. msleep(POLL_INTERVAL_SEC * 1000);
  794. if (kthread_should_stop())
  795. goto out_sleep;
  796. down(&semOperations);
  797. if (poll_count >= POLL_LATCH_CNT) {
  798. poll_count = 0;
  799. poll_state = POLL_SLOTS;
  800. } else
  801. poll_state = POLL_LATCH_REGISTER;
  802. break;
  803. }
  804. /* give up the hardware semaphore */
  805. up(&semOperations);
  806. /* sleep for a short time just for good measure */
  807. out_sleep:
  808. msleep(100);
  809. }
  810. up(&sem_exit);
  811. debug("%s - Exit\n", __func__);
  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. debug("%s - Exit rc[%d] disable[%x] update[%x]\n", __func__, rc, disable, update);
  890. return rc;
  891. }
  892. /*----------------------------------------------------------------------
  893. * Name: process_changeinlatch
  894. *
  895. * Action: compare old and new latch reg status, process the change
  896. *
  897. * Input: old and current latch register status
  898. *
  899. * Return 0 or error codes
  900. * Value:
  901. *---------------------------------------------------------------------*/
  902. static int process_changeinlatch(u8 old, u8 new, struct controller *ctrl)
  903. {
  904. struct slot myslot, *pslot;
  905. u8 i;
  906. u8 mask;
  907. int rc = 0;
  908. debug("%s - Entry old[%x], new[%x]\n", __func__, old, new);
  909. // bit 0 reserved, 0 is LSB, check bit 1-6 for 6 slots
  910. for (i = ctrl->starting_slot_num; i <= ctrl->ending_slot_num; i++) {
  911. mask = 0x01 << i;
  912. if ((mask & old) != (mask & new)) {
  913. pslot = ibmphp_get_slot_from_physical_num(i);
  914. if (pslot) {
  915. memcpy((void *) &myslot, (void *) pslot, sizeof(struct slot));
  916. rc = ibmphp_hpc_readslot(pslot, READ_ALLSTAT, NULL);
  917. debug("%s - call process_changeinstatus for slot[%d]\n", __func__, i);
  918. process_changeinstatus(pslot, &myslot);
  919. } else {
  920. rc = -EINVAL;
  921. err("%s - Error bad pointer for slot[%d]\n", __func__, i);
  922. }
  923. }
  924. }
  925. debug("%s - Exit rc[%d]\n", __func__, rc);
  926. return rc;
  927. }
  928. /*----------------------------------------------------------------------
  929. * Name: ibmphp_hpc_start_poll_thread
  930. *
  931. * Action: start polling thread
  932. *---------------------------------------------------------------------*/
  933. int __init ibmphp_hpc_start_poll_thread(void)
  934. {
  935. debug("%s - Entry\n", __func__);
  936. ibmphp_poll_thread = kthread_run(poll_hpc, NULL, "hpc_poll");
  937. if (IS_ERR(ibmphp_poll_thread)) {
  938. err("%s - Error, thread not started\n", __func__);
  939. return PTR_ERR(ibmphp_poll_thread);
  940. }
  941. return 0;
  942. }
  943. /*----------------------------------------------------------------------
  944. * Name: ibmphp_hpc_stop_poll_thread
  945. *
  946. * Action: stop polling thread and cleanup
  947. *---------------------------------------------------------------------*/
  948. void __exit ibmphp_hpc_stop_poll_thread(void)
  949. {
  950. debug("%s - Entry\n", __func__);
  951. kthread_stop(ibmphp_poll_thread);
  952. debug("before locking operations\n");
  953. ibmphp_lock_operations();
  954. debug("after locking operations\n");
  955. // wait for poll thread to exit
  956. debug("before sem_exit down\n");
  957. down(&sem_exit);
  958. debug("after sem_exit down\n");
  959. // cleanup
  960. debug("before free_hpc_access\n");
  961. free_hpc_access();
  962. debug("after free_hpc_access\n");
  963. ibmphp_unlock_operations();
  964. debug("after unlock operations\n");
  965. up(&sem_exit);
  966. debug("after sem exit up\n");
  967. debug("%s - Exit\n", __func__);
  968. }
  969. /*----------------------------------------------------------------------
  970. * Name: hpc_wait_ctlr_notworking
  971. *
  972. * Action: wait until the controller is in a not working state
  973. *
  974. * Return 0, HPC_ERROR
  975. * Value:
  976. *---------------------------------------------------------------------*/
  977. static int hpc_wait_ctlr_notworking(int timeout, struct controller *ctlr_ptr, void __iomem *wpg_bbar,
  978. u8 *pstatus)
  979. {
  980. int rc = 0;
  981. u8 done = 0;
  982. debug_polling("hpc_wait_ctlr_notworking - Entry timeout[%d]\n", timeout);
  983. while (!done) {
  984. *pstatus = ctrl_read(ctlr_ptr, wpg_bbar, WPG_CTLR_INDEX);
  985. if (*pstatus == HPC_ERROR) {
  986. rc = HPC_ERROR;
  987. done = 1;
  988. }
  989. if (CTLR_WORKING(*pstatus) == HPC_CTLR_WORKING_NO)
  990. done = 1;
  991. if (!done) {
  992. msleep(1000);
  993. if (timeout < 1) {
  994. done = 1;
  995. err("HPCreadslot - Error ctlr timeout\n");
  996. rc = HPC_ERROR;
  997. } else
  998. timeout--;
  999. }
  1000. }
  1001. debug_polling("hpc_wait_ctlr_notworking - Exit rc[%x] status[%x]\n", rc, *pstatus);
  1002. return rc;
  1003. }