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/kthread.h>
  38. #include "ibmphp.h"
  39. static int to_debug = 0;
  40. #define debug_polling(fmt, arg...) do { if (to_debug) debug (fmt, arg); } while (0)
  41. //----------------------------------------------------------------------------
  42. // timeout values
  43. //----------------------------------------------------------------------------
  44. #define CMD_COMPLETE_TOUT_SEC 60 // give HPC 60 sec to finish cmd
  45. #define HPC_CTLR_WORKING_TOUT 60 // give HPC 60 sec to finish cmd
  46. #define HPC_GETACCESS_TIMEOUT 60 // seconds
  47. #define POLL_INTERVAL_SEC 2 // poll HPC every 2 seconds
  48. #define POLL_LATCH_CNT 5 // poll latch 5 times, then poll slots
  49. //----------------------------------------------------------------------------
  50. // Winnipeg Architected Register Offsets
  51. //----------------------------------------------------------------------------
  52. #define WPG_I2CMBUFL_OFFSET 0x08 // I2C Message Buffer Low
  53. #define WPG_I2CMOSUP_OFFSET 0x10 // I2C Master Operation Setup Reg
  54. #define WPG_I2CMCNTL_OFFSET 0x20 // I2C Master Control Register
  55. #define WPG_I2CPARM_OFFSET 0x40 // I2C Parameter Register
  56. #define WPG_I2CSTAT_OFFSET 0x70 // I2C Status Register
  57. //----------------------------------------------------------------------------
  58. // Winnipeg Store Type commands (Add this commands to the register offset)
  59. //----------------------------------------------------------------------------
  60. #define WPG_I2C_AND 0x1000 // I2C AND operation
  61. #define WPG_I2C_OR 0x2000 // I2C OR operation
  62. //----------------------------------------------------------------------------
  63. // Command set for I2C Master Operation Setup Register
  64. //----------------------------------------------------------------------------
  65. #define WPG_READATADDR_MASK 0x00010000 // read,bytes,I2C shifted,index
  66. #define WPG_WRITEATADDR_MASK 0x40010000 // write,bytes,I2C shifted,index
  67. #define WPG_READDIRECT_MASK 0x10010000
  68. #define WPG_WRITEDIRECT_MASK 0x60010000
  69. //----------------------------------------------------------------------------
  70. // bit masks for I2C Master Control Register
  71. //----------------------------------------------------------------------------
  72. #define WPG_I2CMCNTL_STARTOP_MASK 0x00000002 // Start the Operation
  73. //----------------------------------------------------------------------------
  74. //
  75. //----------------------------------------------------------------------------
  76. #define WPG_I2C_IOREMAP_SIZE 0x2044 // size of linear address interval
  77. //----------------------------------------------------------------------------
  78. // command index
  79. //----------------------------------------------------------------------------
  80. #define WPG_1ST_SLOT_INDEX 0x01 // index - 1st slot for ctlr
  81. #define WPG_CTLR_INDEX 0x0F // index - ctlr
  82. #define WPG_1ST_EXTSLOT_INDEX 0x10 // index - 1st ext slot for ctlr
  83. #define WPG_1ST_BUS_INDEX 0x1F // index - 1st bus for ctlr
  84. //----------------------------------------------------------------------------
  85. // macro utilities
  86. //----------------------------------------------------------------------------
  87. // if bits 20,22,25,26,27,29,30 are OFF return 1
  88. #define HPC_I2CSTATUS_CHECK(s) ((u8)((s & 0x00000A76) ? 0 : 1))
  89. //----------------------------------------------------------------------------
  90. // global variables
  91. //----------------------------------------------------------------------------
  92. static struct mutex sem_hpcaccess; // lock access to HPC
  93. static struct semaphore semOperations; // lock all operations and
  94. // access to data structures
  95. static struct semaphore sem_exit; // make sure polling thread goes away
  96. static struct task_struct *ibmphp_poll_thread;
  97. //----------------------------------------------------------------------------
  98. // local function prototypes
  99. //----------------------------------------------------------------------------
  100. static u8 i2c_ctrl_read (struct controller *, void __iomem *, u8);
  101. static u8 i2c_ctrl_write (struct controller *, void __iomem *, u8, u8);
  102. static u8 hpc_writecmdtoindex (u8, u8);
  103. static u8 hpc_readcmdtoindex (u8, u8);
  104. static void get_hpc_access (void);
  105. static void free_hpc_access (void);
  106. static int poll_hpc(void *data);
  107. static int process_changeinstatus (struct slot *, struct slot *);
  108. static int process_changeinlatch (u8, u8, struct controller *);
  109. static int hpc_wait_ctlr_notworking (int, struct controller *, void __iomem *, u8 *);
  110. //----------------------------------------------------------------------------
  111. /*----------------------------------------------------------------------
  112. * Name: ibmphp_hpc_initvars
  113. *
  114. * Action: initialize semaphores and variables
  115. *---------------------------------------------------------------------*/
  116. void __init ibmphp_hpc_initvars (void)
  117. {
  118. debug ("%s - Entry\n", __func__);
  119. mutex_init(&sem_hpcaccess);
  120. init_MUTEX (&semOperations);
  121. init_MUTEX_LOCKED (&sem_exit);
  122. to_debug = 0;
  123. debug ("%s - Exit\n", __func__);
  124. }
  125. /*----------------------------------------------------------------------
  126. * Name: i2c_ctrl_read
  127. *
  128. * Action: read from HPC over I2C
  129. *
  130. *---------------------------------------------------------------------*/
  131. static u8 i2c_ctrl_read (struct controller *ctlr_ptr, void __iomem *WPGBbar, u8 index)
  132. {
  133. u8 status;
  134. int i;
  135. void __iomem *wpg_addr; // base addr + offset
  136. unsigned long wpg_data; // data to/from WPG LOHI format
  137. unsigned long ultemp;
  138. unsigned long data; // actual data HILO format
  139. debug_polling ("%s - Entry WPGBbar[%p] index[%x] \n", __func__, WPGBbar, index);
  140. //--------------------------------------------------------------------
  141. // READ - step 1
  142. // read at address, byte length, I2C address (shifted), index
  143. // or read direct, byte length, index
  144. if (ctlr_ptr->ctlr_type == 0x02) {
  145. data = WPG_READATADDR_MASK;
  146. // fill in I2C address
  147. ultemp = (unsigned long)ctlr_ptr->u.wpeg_ctlr.i2c_addr;
  148. ultemp = ultemp >> 1;
  149. data |= (ultemp << 8);
  150. // fill in index
  151. data |= (unsigned long)index;
  152. } else if (ctlr_ptr->ctlr_type == 0x04) {
  153. data = WPG_READDIRECT_MASK;
  154. // fill in index
  155. ultemp = (unsigned long)index;
  156. ultemp = ultemp << 8;
  157. data |= ultemp;
  158. } else {
  159. err ("this controller type is not supported \n");
  160. return HPC_ERROR;
  161. }
  162. wpg_data = swab32 (data); // swap data before writing
  163. wpg_addr = WPGBbar + WPG_I2CMOSUP_OFFSET;
  164. writel (wpg_data, wpg_addr);
  165. //--------------------------------------------------------------------
  166. // READ - step 2 : clear the message buffer
  167. data = 0x00000000;
  168. wpg_data = swab32 (data);
  169. wpg_addr = WPGBbar + WPG_I2CMBUFL_OFFSET;
  170. writel (wpg_data, wpg_addr);
  171. //--------------------------------------------------------------------
  172. // READ - step 3 : issue start operation, I2C master control bit 30:ON
  173. // 2020 : [20] OR operation at [20] offset 0x20
  174. data = WPG_I2CMCNTL_STARTOP_MASK;
  175. wpg_data = swab32 (data);
  176. wpg_addr = WPGBbar + WPG_I2CMCNTL_OFFSET + WPG_I2C_OR;
  177. writel (wpg_data, wpg_addr);
  178. //--------------------------------------------------------------------
  179. // READ - step 4 : wait until start operation bit clears
  180. i = CMD_COMPLETE_TOUT_SEC;
  181. while (i) {
  182. msleep(10);
  183. wpg_addr = WPGBbar + WPG_I2CMCNTL_OFFSET;
  184. wpg_data = readl (wpg_addr);
  185. data = swab32 (wpg_data);
  186. if (!(data & WPG_I2CMCNTL_STARTOP_MASK))
  187. break;
  188. i--;
  189. }
  190. if (i == 0) {
  191. debug ("%s - Error : WPG timeout\n", __func__);
  192. return HPC_ERROR;
  193. }
  194. //--------------------------------------------------------------------
  195. // READ - step 5 : read I2C status register
  196. i = CMD_COMPLETE_TOUT_SEC;
  197. while (i) {
  198. msleep(10);
  199. wpg_addr = WPGBbar + WPG_I2CSTAT_OFFSET;
  200. wpg_data = readl (wpg_addr);
  201. data = swab32 (wpg_data);
  202. if (HPC_I2CSTATUS_CHECK (data))
  203. break;
  204. i--;
  205. }
  206. if (i == 0) {
  207. debug ("ctrl_read - Exit Error:I2C timeout\n");
  208. return HPC_ERROR;
  209. }
  210. //--------------------------------------------------------------------
  211. // READ - step 6 : get DATA
  212. wpg_addr = WPGBbar + WPG_I2CMBUFL_OFFSET;
  213. wpg_data = readl (wpg_addr);
  214. data = swab32 (wpg_data);
  215. status = (u8) data;
  216. debug_polling ("%s - Exit index[%x] status[%x]\n", __func__, index, status);
  217. return (status);
  218. }
  219. /*----------------------------------------------------------------------
  220. * Name: i2c_ctrl_write
  221. *
  222. * Action: write to HPC over I2C
  223. *
  224. * Return 0 or error codes
  225. *---------------------------------------------------------------------*/
  226. static u8 i2c_ctrl_write (struct controller *ctlr_ptr, void __iomem *WPGBbar, u8 index, u8 cmd)
  227. {
  228. u8 rc;
  229. void __iomem *wpg_addr; // base addr + offset
  230. unsigned long wpg_data; // data to/from WPG LOHI format
  231. unsigned long ultemp;
  232. unsigned long data; // actual data HILO format
  233. int i;
  234. debug_polling ("%s - Entry WPGBbar[%p] index[%x] cmd[%x]\n", __func__, WPGBbar, index, cmd);
  235. rc = 0;
  236. //--------------------------------------------------------------------
  237. // WRITE - step 1
  238. // write at address, byte length, I2C address (shifted), index
  239. // or write direct, byte length, index
  240. data = 0x00000000;
  241. if (ctlr_ptr->ctlr_type == 0x02) {
  242. data = WPG_WRITEATADDR_MASK;
  243. // fill in I2C address
  244. ultemp = (unsigned long)ctlr_ptr->u.wpeg_ctlr.i2c_addr;
  245. ultemp = ultemp >> 1;
  246. data |= (ultemp << 8);
  247. // fill in index
  248. data |= (unsigned long)index;
  249. } else if (ctlr_ptr->ctlr_type == 0x04) {
  250. data = WPG_WRITEDIRECT_MASK;
  251. // fill in index
  252. ultemp = (unsigned long)index;
  253. ultemp = ultemp << 8;
  254. data |= ultemp;
  255. } else {
  256. err ("this controller type is not supported \n");
  257. return HPC_ERROR;
  258. }
  259. wpg_data = swab32 (data); // swap data before writing
  260. wpg_addr = WPGBbar + WPG_I2CMOSUP_OFFSET;
  261. writel (wpg_data, wpg_addr);
  262. //--------------------------------------------------------------------
  263. // WRITE - step 2 : clear the message buffer
  264. data = 0x00000000 | (unsigned long)cmd;
  265. wpg_data = swab32 (data);
  266. wpg_addr = WPGBbar + WPG_I2CMBUFL_OFFSET;
  267. writel (wpg_data, wpg_addr);
  268. //--------------------------------------------------------------------
  269. // WRITE - step 3 : issue start operation,I2C master control bit 30:ON
  270. // 2020 : [20] OR operation at [20] offset 0x20
  271. data = WPG_I2CMCNTL_STARTOP_MASK;
  272. wpg_data = swab32 (data);
  273. wpg_addr = WPGBbar + WPG_I2CMCNTL_OFFSET + WPG_I2C_OR;
  274. writel (wpg_data, wpg_addr);
  275. //--------------------------------------------------------------------
  276. // WRITE - step 4 : wait until start operation bit clears
  277. i = CMD_COMPLETE_TOUT_SEC;
  278. while (i) {
  279. msleep(10);
  280. wpg_addr = WPGBbar + WPG_I2CMCNTL_OFFSET;
  281. wpg_data = readl (wpg_addr);
  282. data = swab32 (wpg_data);
  283. if (!(data & WPG_I2CMCNTL_STARTOP_MASK))
  284. break;
  285. i--;
  286. }
  287. if (i == 0) {
  288. debug ("%s - Exit Error:WPG timeout\n", __func__);
  289. rc = HPC_ERROR;
  290. }
  291. //--------------------------------------------------------------------
  292. // WRITE - step 5 : read I2C status register
  293. i = CMD_COMPLETE_TOUT_SEC;
  294. while (i) {
  295. msleep(10);
  296. wpg_addr = WPGBbar + WPG_I2CSTAT_OFFSET;
  297. wpg_data = readl (wpg_addr);
  298. data = swab32 (wpg_data);
  299. if (HPC_I2CSTATUS_CHECK (data))
  300. break;
  301. i--;
  302. }
  303. if (i == 0) {
  304. debug ("ctrl_read - Error : I2C timeout\n");
  305. rc = HPC_ERROR;
  306. }
  307. debug_polling ("%s Exit rc[%x]\n", __func__, rc);
  308. return (rc);
  309. }
  310. //------------------------------------------------------------
  311. // Read from ISA type HPC
  312. //------------------------------------------------------------
  313. static u8 isa_ctrl_read (struct controller *ctlr_ptr, u8 offset)
  314. {
  315. u16 start_address;
  316. u16 end_address;
  317. u8 data;
  318. start_address = ctlr_ptr->u.isa_ctlr.io_start;
  319. end_address = ctlr_ptr->u.isa_ctlr.io_end;
  320. data = inb (start_address + offset);
  321. return data;
  322. }
  323. //--------------------------------------------------------------
  324. // Write to ISA type HPC
  325. //--------------------------------------------------------------
  326. static void isa_ctrl_write (struct controller *ctlr_ptr, u8 offset, u8 data)
  327. {
  328. u16 start_address;
  329. u16 port_address;
  330. start_address = ctlr_ptr->u.isa_ctlr.io_start;
  331. port_address = start_address + (u16) offset;
  332. outb (data, port_address);
  333. }
  334. static u8 pci_ctrl_read (struct controller *ctrl, u8 offset)
  335. {
  336. u8 data = 0x00;
  337. debug ("inside pci_ctrl_read\n");
  338. if (ctrl->ctrl_dev)
  339. pci_read_config_byte (ctrl->ctrl_dev, HPC_PCI_OFFSET + offset, &data);
  340. return data;
  341. }
  342. static u8 pci_ctrl_write (struct controller *ctrl, u8 offset, u8 data)
  343. {
  344. u8 rc = -ENODEV;
  345. debug ("inside pci_ctrl_write\n");
  346. if (ctrl->ctrl_dev) {
  347. pci_write_config_byte (ctrl->ctrl_dev, HPC_PCI_OFFSET + offset, data);
  348. rc = 0;
  349. }
  350. return rc;
  351. }
  352. static u8 ctrl_read (struct controller *ctlr, void __iomem *base, u8 offset)
  353. {
  354. u8 rc;
  355. switch (ctlr->ctlr_type) {
  356. case 0:
  357. rc = isa_ctrl_read (ctlr, offset);
  358. break;
  359. case 1:
  360. rc = pci_ctrl_read (ctlr, offset);
  361. break;
  362. case 2:
  363. case 4:
  364. rc = i2c_ctrl_read (ctlr, base, offset);
  365. break;
  366. default:
  367. return -ENODEV;
  368. }
  369. return rc;
  370. }
  371. static u8 ctrl_write (struct controller *ctlr, void __iomem *base, u8 offset, u8 data)
  372. {
  373. u8 rc = 0;
  374. switch (ctlr->ctlr_type) {
  375. case 0:
  376. isa_ctrl_write(ctlr, offset, data);
  377. break;
  378. case 1:
  379. rc = pci_ctrl_write (ctlr, offset, data);
  380. break;
  381. case 2:
  382. case 4:
  383. rc = i2c_ctrl_write(ctlr, base, offset, data);
  384. break;
  385. default:
  386. return -ENODEV;
  387. }
  388. return rc;
  389. }
  390. /*----------------------------------------------------------------------
  391. * Name: hpc_writecmdtoindex()
  392. *
  393. * Action: convert a write command to proper index within a controller
  394. *
  395. * Return index, HPC_ERROR
  396. *---------------------------------------------------------------------*/
  397. static u8 hpc_writecmdtoindex (u8 cmd, u8 index)
  398. {
  399. u8 rc;
  400. switch (cmd) {
  401. case HPC_CTLR_ENABLEIRQ: // 0x00.N.15
  402. case HPC_CTLR_CLEARIRQ: // 0x06.N.15
  403. case HPC_CTLR_RESET: // 0x07.N.15
  404. case HPC_CTLR_IRQSTEER: // 0x08.N.15
  405. case HPC_CTLR_DISABLEIRQ: // 0x01.N.15
  406. case HPC_ALLSLOT_ON: // 0x11.N.15
  407. case HPC_ALLSLOT_OFF: // 0x12.N.15
  408. rc = 0x0F;
  409. break;
  410. case HPC_SLOT_OFF: // 0x02.Y.0-14
  411. case HPC_SLOT_ON: // 0x03.Y.0-14
  412. case HPC_SLOT_ATTNOFF: // 0x04.N.0-14
  413. case HPC_SLOT_ATTNON: // 0x05.N.0-14
  414. case HPC_SLOT_BLINKLED: // 0x13.N.0-14
  415. rc = index;
  416. break;
  417. case HPC_BUS_33CONVMODE:
  418. case HPC_BUS_66CONVMODE:
  419. case HPC_BUS_66PCIXMODE:
  420. case HPC_BUS_100PCIXMODE:
  421. case HPC_BUS_133PCIXMODE:
  422. rc = index + WPG_1ST_BUS_INDEX - 1;
  423. break;
  424. default:
  425. err ("hpc_writecmdtoindex - Error invalid cmd[%x]\n", cmd);
  426. rc = HPC_ERROR;
  427. }
  428. return rc;
  429. }
  430. /*----------------------------------------------------------------------
  431. * Name: hpc_readcmdtoindex()
  432. *
  433. * Action: convert a read command to proper index within a controller
  434. *
  435. * Return index, HPC_ERROR
  436. *---------------------------------------------------------------------*/
  437. static u8 hpc_readcmdtoindex (u8 cmd, u8 index)
  438. {
  439. u8 rc;
  440. switch (cmd) {
  441. case READ_CTLRSTATUS:
  442. rc = 0x0F;
  443. break;
  444. case READ_SLOTSTATUS:
  445. case READ_ALLSTAT:
  446. rc = index;
  447. break;
  448. case READ_EXTSLOTSTATUS:
  449. rc = index + WPG_1ST_EXTSLOT_INDEX;
  450. break;
  451. case READ_BUSSTATUS:
  452. rc = index + WPG_1ST_BUS_INDEX - 1;
  453. break;
  454. case READ_SLOTLATCHLOWREG:
  455. rc = 0x28;
  456. break;
  457. case READ_REVLEVEL:
  458. rc = 0x25;
  459. break;
  460. case READ_HPCOPTIONS:
  461. rc = 0x27;
  462. break;
  463. default:
  464. rc = HPC_ERROR;
  465. }
  466. return rc;
  467. }
  468. /*----------------------------------------------------------------------
  469. * Name: HPCreadslot()
  470. *
  471. * Action: issue a READ command to HPC
  472. *
  473. * Input: pslot - cannot be NULL for READ_ALLSTAT
  474. * pstatus - can be NULL for READ_ALLSTAT
  475. *
  476. * Return 0 or error codes
  477. *---------------------------------------------------------------------*/
  478. int ibmphp_hpc_readslot (struct slot * pslot, u8 cmd, u8 * pstatus)
  479. {
  480. void __iomem *wpg_bbar = NULL;
  481. struct controller *ctlr_ptr;
  482. struct list_head *pslotlist;
  483. u8 index, status;
  484. int rc = 0;
  485. int busindex;
  486. debug_polling ("%s - Entry pslot[%p] cmd[%x] pstatus[%p]\n", __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 (pslotlist, &ibmphp_slot_head) {
  559. pslot = list_entry (pslotlist, struct slot, ibm_slot_list);
  560. index = pslot->ctlr_index;
  561. rc = hpc_wait_ctlr_notworking (HPC_CTLR_WORKING_TOUT, ctlr_ptr,
  562. wpg_bbar, &status);
  563. if (!rc) {
  564. pslot->status = ctrl_read (ctlr_ptr, wpg_bbar, index);
  565. rc = hpc_wait_ctlr_notworking (HPC_CTLR_WORKING_TOUT,
  566. ctlr_ptr, wpg_bbar, &status);
  567. if (!rc)
  568. pslot->ext_status =
  569. ctrl_read (ctlr_ptr, wpg_bbar,
  570. index + WPG_1ST_EXTSLOT_INDEX);
  571. } else {
  572. err ("%s - Error ctrl_read failed\n", __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. struct list_head *pslotlist;
  730. int rc;
  731. int poll_state = POLL_LATCH_REGISTER;
  732. u8 oldlatchlow = 0x00;
  733. u8 curlatchlow = 0x00;
  734. int poll_count = 0;
  735. u8 ctrl_count = 0x00;
  736. debug ("%s - Entry\n", __func__);
  737. while (!kthread_should_stop()) {
  738. /* try to get the lock to do some kind of hardware access */
  739. down (&semOperations);
  740. switch (poll_state) {
  741. case POLL_LATCH_REGISTER:
  742. oldlatchlow = curlatchlow;
  743. ctrl_count = 0x00;
  744. list_for_each (pslotlist, &ibmphp_slot_head) {
  745. if (ctrl_count >= ibmphp_get_total_controllers())
  746. break;
  747. pslot = list_entry (pslotlist, struct slot, ibm_slot_list);
  748. if (pslot->ctrl->ctlr_relative_id == ctrl_count) {
  749. ctrl_count++;
  750. if (READ_SLOT_LATCH (pslot->ctrl)) {
  751. rc = ibmphp_hpc_readslot (pslot,
  752. READ_SLOTLATCHLOWREG,
  753. &curlatchlow);
  754. if (oldlatchlow != curlatchlow)
  755. process_changeinlatch (oldlatchlow,
  756. curlatchlow,
  757. pslot->ctrl);
  758. }
  759. }
  760. }
  761. ++poll_count;
  762. poll_state = POLL_SLEEP;
  763. break;
  764. case POLL_SLOTS:
  765. list_for_each (pslotlist, &ibmphp_slot_head) {
  766. pslot = list_entry (pslotlist, struct slot, ibm_slot_list);
  767. // make a copy of the old status
  768. memcpy ((void *) &myslot, (void *) pslot,
  769. sizeof (struct slot));
  770. rc = ibmphp_hpc_readslot (pslot, READ_ALLSTAT, NULL);
  771. if ((myslot.status != pslot->status)
  772. || (myslot.ext_status != pslot->ext_status))
  773. process_changeinstatus (pslot, &myslot);
  774. }
  775. ctrl_count = 0x00;
  776. list_for_each (pslotlist, &ibmphp_slot_head) {
  777. if (ctrl_count >= ibmphp_get_total_controllers())
  778. break;
  779. pslot = list_entry (pslotlist, struct slot, ibm_slot_list);
  780. if (pslot->ctrl->ctlr_relative_id == ctrl_count) {
  781. ctrl_count++;
  782. if (READ_SLOT_LATCH (pslot->ctrl))
  783. rc = ibmphp_hpc_readslot (pslot,
  784. READ_SLOTLATCHLOWREG,
  785. &curlatchlow);
  786. }
  787. }
  788. ++poll_count;
  789. poll_state = POLL_SLEEP;
  790. break;
  791. case POLL_SLEEP:
  792. /* don't sleep with a lock on the hardware */
  793. up (&semOperations);
  794. msleep(POLL_INTERVAL_SEC * 1000);
  795. if (kthread_should_stop())
  796. goto out_sleep;
  797. down (&semOperations);
  798. if (poll_count >= POLL_LATCH_CNT) {
  799. poll_count = 0;
  800. poll_state = POLL_SLOTS;
  801. } else
  802. poll_state = POLL_LATCH_REGISTER;
  803. break;
  804. }
  805. /* give up the hardware semaphore */
  806. up (&semOperations);
  807. /* sleep for a short time just for good measure */
  808. out_sleep:
  809. msleep(100);
  810. }
  811. up (&sem_exit);
  812. debug ("%s - Exit\n", __func__);
  813. return 0;
  814. }
  815. /*----------------------------------------------------------------------
  816. * Name: process_changeinstatus
  817. *
  818. * Action: compare old and new slot status, process the change in status
  819. *
  820. * Input: pointer to slot struct, old slot struct
  821. *
  822. * Return 0 or error codes
  823. * Value:
  824. *
  825. * Side
  826. * Effects: None.
  827. *
  828. * Notes:
  829. *---------------------------------------------------------------------*/
  830. static int process_changeinstatus (struct slot *pslot, struct slot *poldslot)
  831. {
  832. u8 status;
  833. int rc = 0;
  834. u8 disable = 0;
  835. u8 update = 0;
  836. debug ("process_changeinstatus - Entry pslot[%p], poldslot[%p]\n", pslot, poldslot);
  837. // bit 0 - HPC_SLOT_POWER
  838. if ((pslot->status & 0x01) != (poldslot->status & 0x01))
  839. update = 1;
  840. // bit 1 - HPC_SLOT_CONNECT
  841. // ignore
  842. // bit 2 - HPC_SLOT_ATTN
  843. if ((pslot->status & 0x04) != (poldslot->status & 0x04))
  844. update = 1;
  845. // bit 3 - HPC_SLOT_PRSNT2
  846. // bit 4 - HPC_SLOT_PRSNT1
  847. if (((pslot->status & 0x08) != (poldslot->status & 0x08))
  848. || ((pslot->status & 0x10) != (poldslot->status & 0x10)))
  849. update = 1;
  850. // bit 5 - HPC_SLOT_PWRGD
  851. if ((pslot->status & 0x20) != (poldslot->status & 0x20))
  852. // OFF -> ON: ignore, ON -> OFF: disable slot
  853. if ((poldslot->status & 0x20) && (SLOT_CONNECT (poldslot->status) == HPC_SLOT_CONNECTED) && (SLOT_PRESENT (poldslot->status)))
  854. disable = 1;
  855. // bit 6 - HPC_SLOT_BUS_SPEED
  856. // ignore
  857. // bit 7 - HPC_SLOT_LATCH
  858. if ((pslot->status & 0x80) != (poldslot->status & 0x80)) {
  859. update = 1;
  860. // OPEN -> CLOSE
  861. if (pslot->status & 0x80) {
  862. if (SLOT_PWRGD (pslot->status)) {
  863. // power goes on and off after closing latch
  864. // check again to make sure power is still ON
  865. msleep(1000);
  866. rc = ibmphp_hpc_readslot (pslot, READ_SLOTSTATUS, &status);
  867. if (SLOT_PWRGD (status))
  868. update = 1;
  869. else // overwrite power in pslot to OFF
  870. pslot->status &= ~HPC_SLOT_POWER;
  871. }
  872. }
  873. // CLOSE -> OPEN
  874. else if ((SLOT_PWRGD (poldslot->status) == HPC_SLOT_PWRGD_GOOD)
  875. && (SLOT_CONNECT (poldslot->status) == HPC_SLOT_CONNECTED) && (SLOT_PRESENT (poldslot->status))) {
  876. disable = 1;
  877. }
  878. // else - ignore
  879. }
  880. // bit 4 - HPC_SLOT_BLINK_ATTN
  881. if ((pslot->ext_status & 0x08) != (poldslot->ext_status & 0x08))
  882. update = 1;
  883. if (disable) {
  884. debug ("process_changeinstatus - disable slot\n");
  885. pslot->flag = 0;
  886. rc = ibmphp_do_disable_slot (pslot);
  887. }
  888. if (update || disable) {
  889. ibmphp_update_slot_info (pslot);
  890. }
  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. }