ibmphp_hpc.c 33 KB

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