ibmphp_hpc.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  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. debug ("%s - Exit rc[%d] disable[%x] update[%x]\n", __func__, rc, disable, update);
  892. return rc;
  893. }
  894. /*----------------------------------------------------------------------
  895. * Name: process_changeinlatch
  896. *
  897. * Action: compare old and new latch reg status, process the change
  898. *
  899. * Input: old and current latch register status
  900. *
  901. * Return 0 or error codes
  902. * Value:
  903. *---------------------------------------------------------------------*/
  904. static int process_changeinlatch (u8 old, u8 new, struct controller *ctrl)
  905. {
  906. struct slot myslot, *pslot;
  907. u8 i;
  908. u8 mask;
  909. int rc = 0;
  910. debug ("%s - Entry old[%x], new[%x]\n", __func__, old, new);
  911. // bit 0 reserved, 0 is LSB, check bit 1-6 for 6 slots
  912. for (i = ctrl->starting_slot_num; i <= ctrl->ending_slot_num; i++) {
  913. mask = 0x01 << i;
  914. if ((mask & old) != (mask & new)) {
  915. pslot = ibmphp_get_slot_from_physical_num (i);
  916. if (pslot) {
  917. memcpy ((void *) &myslot, (void *) pslot, sizeof (struct slot));
  918. rc = ibmphp_hpc_readslot (pslot, READ_ALLSTAT, NULL);
  919. debug ("%s - call process_changeinstatus for slot[%d]\n", __func__, i);
  920. process_changeinstatus (pslot, &myslot);
  921. } else {
  922. rc = -EINVAL;
  923. err ("%s - Error bad pointer for slot[%d]\n", __func__, i);
  924. }
  925. }
  926. }
  927. debug ("%s - Exit rc[%d]\n", __func__, rc);
  928. return rc;
  929. }
  930. /*----------------------------------------------------------------------
  931. * Name: ibmphp_hpc_start_poll_thread
  932. *
  933. * Action: start polling thread
  934. *---------------------------------------------------------------------*/
  935. int __init ibmphp_hpc_start_poll_thread (void)
  936. {
  937. debug ("%s - Entry\n", __func__);
  938. ibmphp_poll_thread = kthread_run(poll_hpc, NULL, "hpc_poll");
  939. if (IS_ERR(ibmphp_poll_thread)) {
  940. err ("%s - Error, thread not started\n", __func__);
  941. return PTR_ERR(ibmphp_poll_thread);
  942. }
  943. return 0;
  944. }
  945. /*----------------------------------------------------------------------
  946. * Name: ibmphp_hpc_stop_poll_thread
  947. *
  948. * Action: stop polling thread and cleanup
  949. *---------------------------------------------------------------------*/
  950. void __exit ibmphp_hpc_stop_poll_thread (void)
  951. {
  952. debug ("%s - Entry\n", __func__);
  953. kthread_stop(ibmphp_poll_thread);
  954. debug ("before locking operations \n");
  955. ibmphp_lock_operations ();
  956. debug ("after locking operations \n");
  957. // wait for poll thread to exit
  958. debug ("before sem_exit down \n");
  959. down (&sem_exit);
  960. debug ("after sem_exit down \n");
  961. // cleanup
  962. debug ("before free_hpc_access \n");
  963. free_hpc_access ();
  964. debug ("after free_hpc_access \n");
  965. ibmphp_unlock_operations ();
  966. debug ("after unlock operations \n");
  967. up (&sem_exit);
  968. debug ("after sem exit up\n");
  969. debug ("%s - Exit\n", __func__);
  970. }
  971. /*----------------------------------------------------------------------
  972. * Name: hpc_wait_ctlr_notworking
  973. *
  974. * Action: wait until the controller is in a not working state
  975. *
  976. * Return 0, HPC_ERROR
  977. * Value:
  978. *---------------------------------------------------------------------*/
  979. static int hpc_wait_ctlr_notworking (int timeout, struct controller *ctlr_ptr, void __iomem *wpg_bbar,
  980. u8 *pstatus)
  981. {
  982. int rc = 0;
  983. u8 done = 0;
  984. debug_polling ("hpc_wait_ctlr_notworking - Entry timeout[%d]\n", timeout);
  985. while (!done) {
  986. *pstatus = ctrl_read (ctlr_ptr, wpg_bbar, WPG_CTLR_INDEX);
  987. if (*pstatus == HPC_ERROR) {
  988. rc = HPC_ERROR;
  989. done = 1;
  990. }
  991. if (CTLR_WORKING (*pstatus) == HPC_CTLR_WORKING_NO)
  992. done = 1;
  993. if (!done) {
  994. msleep(1000);
  995. if (timeout < 1) {
  996. done = 1;
  997. err ("HPCreadslot - Error ctlr timeout\n");
  998. rc = HPC_ERROR;
  999. } else
  1000. timeout--;
  1001. }
  1002. }
  1003. debug_polling ("hpc_wait_ctlr_notworking - Exit rc[%x] status[%x]\n", rc, *pstatus);
  1004. return rc;
  1005. }