shpchp_hpc.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  1. /*
  2. * Standard PCI Hot Plug Driver
  3. *
  4. * Copyright (C) 1995,2001 Compaq Computer Corporation
  5. * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
  6. * Copyright (C) 2001 IBM Corp.
  7. * Copyright (C) 2003-2004 Intel Corporation
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or (at
  14. * your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  19. * NON INFRINGEMENT. See the GNU General Public License for more
  20. * details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. *
  26. * Send feedback to <greg@kroah.com>,<kristen.c.accardi@intel.com>
  27. *
  28. */
  29. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/types.h>
  32. #include <linux/pci.h>
  33. #include <linux/interrupt.h>
  34. #include "shpchp.h"
  35. #ifdef DEBUG
  36. #define DBG_K_TRACE_ENTRY ((unsigned int)0x00000001) /* On function entry */
  37. #define DBG_K_TRACE_EXIT ((unsigned int)0x00000002) /* On function exit */
  38. #define DBG_K_INFO ((unsigned int)0x00000004) /* Info messages */
  39. #define DBG_K_ERROR ((unsigned int)0x00000008) /* Error messages */
  40. #define DBG_K_TRACE (DBG_K_TRACE_ENTRY|DBG_K_TRACE_EXIT)
  41. #define DBG_K_STANDARD (DBG_K_INFO|DBG_K_ERROR|DBG_K_TRACE)
  42. /* Redefine this flagword to set debug level */
  43. #define DEBUG_LEVEL DBG_K_STANDARD
  44. #define DEFINE_DBG_BUFFER char __dbg_str_buf[256];
  45. #define DBG_PRINT( dbg_flags, args... ) \
  46. do { \
  47. if ( DEBUG_LEVEL & ( dbg_flags ) ) \
  48. { \
  49. int len; \
  50. len = sprintf( __dbg_str_buf, "%s:%d: %s: ", \
  51. __FILE__, __LINE__, __FUNCTION__ ); \
  52. sprintf( __dbg_str_buf + len, args ); \
  53. printk( KERN_NOTICE "%s\n", __dbg_str_buf ); \
  54. } \
  55. } while (0)
  56. #define DBG_ENTER_ROUTINE DBG_PRINT (DBG_K_TRACE_ENTRY, "%s", "[Entry]");
  57. #define DBG_LEAVE_ROUTINE DBG_PRINT (DBG_K_TRACE_EXIT, "%s", "[Exit]");
  58. #else
  59. #define DEFINE_DBG_BUFFER
  60. #define DBG_ENTER_ROUTINE
  61. #define DBG_LEAVE_ROUTINE
  62. #endif /* DEBUG */
  63. /* Slot Available Register I field definition */
  64. #define SLOT_33MHZ 0x0000001f
  65. #define SLOT_66MHZ_PCIX 0x00001f00
  66. #define SLOT_100MHZ_PCIX 0x001f0000
  67. #define SLOT_133MHZ_PCIX 0x1f000000
  68. /* Slot Available Register II field definition */
  69. #define SLOT_66MHZ 0x0000001f
  70. #define SLOT_66MHZ_PCIX_266 0x00000f00
  71. #define SLOT_100MHZ_PCIX_266 0x0000f000
  72. #define SLOT_133MHZ_PCIX_266 0x000f0000
  73. #define SLOT_66MHZ_PCIX_533 0x00f00000
  74. #define SLOT_100MHZ_PCIX_533 0x0f000000
  75. #define SLOT_133MHZ_PCIX_533 0xf0000000
  76. /* Slot Configuration */
  77. #define SLOT_NUM 0x0000001F
  78. #define FIRST_DEV_NUM 0x00001F00
  79. #define PSN 0x07FF0000
  80. #define UPDOWN 0x20000000
  81. #define MRLSENSOR 0x40000000
  82. #define ATTN_BUTTON 0x80000000
  83. /*
  84. * Interrupt Locator Register definitions
  85. */
  86. #define CMD_INTR_PENDING (1 << 0)
  87. #define SLOT_INTR_PENDING(i) (1 << (i + 1))
  88. /*
  89. * Controller SERR-INT Register
  90. */
  91. #define GLOBAL_INTR_MASK (1 << 0)
  92. #define GLOBAL_SERR_MASK (1 << 1)
  93. #define COMMAND_INTR_MASK (1 << 2)
  94. #define ARBITER_SERR_MASK (1 << 3)
  95. #define COMMAND_DETECTED (1 << 16)
  96. #define ARBITER_DETECTED (1 << 17)
  97. #define SERR_INTR_RSVDZ_MASK 0xfffc0000
  98. /*
  99. * Logical Slot Register definitions
  100. */
  101. #define SLOT_REG(i) (SLOT1 + (4 * i))
  102. #define SLOT_STATE_SHIFT (0)
  103. #define SLOT_STATE_MASK (3 << 0)
  104. #define SLOT_STATE_PWRONLY (1)
  105. #define SLOT_STATE_ENABLED (2)
  106. #define SLOT_STATE_DISABLED (3)
  107. #define PWR_LED_STATE_SHIFT (2)
  108. #define PWR_LED_STATE_MASK (3 << 2)
  109. #define ATN_LED_STATE_SHIFT (4)
  110. #define ATN_LED_STATE_MASK (3 << 4)
  111. #define ATN_LED_STATE_ON (1)
  112. #define ATN_LED_STATE_BLINK (2)
  113. #define ATN_LED_STATE_OFF (3)
  114. #define POWER_FAULT (1 << 6)
  115. #define ATN_BUTTON (1 << 7)
  116. #define MRL_SENSOR (1 << 8)
  117. #define MHZ66_CAP (1 << 9)
  118. #define PRSNT_SHIFT (10)
  119. #define PRSNT_MASK (3 << 10)
  120. #define PCIX_CAP_SHIFT (12)
  121. #define PCIX_CAP_MASK_PI1 (3 << 12)
  122. #define PCIX_CAP_MASK_PI2 (7 << 12)
  123. #define PRSNT_CHANGE_DETECTED (1 << 16)
  124. #define ISO_PFAULT_DETECTED (1 << 17)
  125. #define BUTTON_PRESS_DETECTED (1 << 18)
  126. #define MRL_CHANGE_DETECTED (1 << 19)
  127. #define CON_PFAULT_DETECTED (1 << 20)
  128. #define PRSNT_CHANGE_INTR_MASK (1 << 24)
  129. #define ISO_PFAULT_INTR_MASK (1 << 25)
  130. #define BUTTON_PRESS_INTR_MASK (1 << 26)
  131. #define MRL_CHANGE_INTR_MASK (1 << 27)
  132. #define CON_PFAULT_INTR_MASK (1 << 28)
  133. #define MRL_CHANGE_SERR_MASK (1 << 29)
  134. #define CON_PFAULT_SERR_MASK (1 << 30)
  135. #define SLOT_REG_RSVDZ_MASK (1 << 15) | (7 << 21)
  136. /*
  137. * SHPC Command Code definitnions
  138. *
  139. * Slot Operation 00h - 3Fh
  140. * Set Bus Segment Speed/Mode A 40h - 47h
  141. * Power-Only All Slots 48h
  142. * Enable All Slots 49h
  143. * Set Bus Segment Speed/Mode B (PI=2) 50h - 5Fh
  144. * Reserved Command Codes 60h - BFh
  145. * Vendor Specific Commands C0h - FFh
  146. */
  147. #define SET_SLOT_PWR 0x01 /* Slot Operation */
  148. #define SET_SLOT_ENABLE 0x02
  149. #define SET_SLOT_DISABLE 0x03
  150. #define SET_PWR_ON 0x04
  151. #define SET_PWR_BLINK 0x08
  152. #define SET_PWR_OFF 0x0c
  153. #define SET_ATTN_ON 0x10
  154. #define SET_ATTN_BLINK 0x20
  155. #define SET_ATTN_OFF 0x30
  156. #define SETA_PCI_33MHZ 0x40 /* Set Bus Segment Speed/Mode A */
  157. #define SETA_PCI_66MHZ 0x41
  158. #define SETA_PCIX_66MHZ 0x42
  159. #define SETA_PCIX_100MHZ 0x43
  160. #define SETA_PCIX_133MHZ 0x44
  161. #define SETA_RESERVED1 0x45
  162. #define SETA_RESERVED2 0x46
  163. #define SETA_RESERVED3 0x47
  164. #define SET_PWR_ONLY_ALL 0x48 /* Power-Only All Slots */
  165. #define SET_ENABLE_ALL 0x49 /* Enable All Slots */
  166. #define SETB_PCI_33MHZ 0x50 /* Set Bus Segment Speed/Mode B */
  167. #define SETB_PCI_66MHZ 0x51
  168. #define SETB_PCIX_66MHZ_PM 0x52
  169. #define SETB_PCIX_100MHZ_PM 0x53
  170. #define SETB_PCIX_133MHZ_PM 0x54
  171. #define SETB_PCIX_66MHZ_EM 0x55
  172. #define SETB_PCIX_100MHZ_EM 0x56
  173. #define SETB_PCIX_133MHZ_EM 0x57
  174. #define SETB_PCIX_66MHZ_266 0x58
  175. #define SETB_PCIX_100MHZ_266 0x59
  176. #define SETB_PCIX_133MHZ_266 0x5a
  177. #define SETB_PCIX_66MHZ_533 0x5b
  178. #define SETB_PCIX_100MHZ_533 0x5c
  179. #define SETB_PCIX_133MHZ_533 0x5d
  180. #define SETB_RESERVED1 0x5e
  181. #define SETB_RESERVED2 0x5f
  182. /*
  183. * SHPC controller command error code
  184. */
  185. #define SWITCH_OPEN 0x1
  186. #define INVALID_CMD 0x2
  187. #define INVALID_SPEED_MODE 0x4
  188. /*
  189. * For accessing SHPC Working Register Set via PCI Configuration Space
  190. */
  191. #define DWORD_SELECT 0x2
  192. #define DWORD_DATA 0x4
  193. /* Field Offset in Logical Slot Register - byte boundary */
  194. #define SLOT_EVENT_LATCH 0x2
  195. #define SLOT_SERR_INT_MASK 0x3
  196. DEFINE_DBG_BUFFER /* Debug string buffer for entire HPC defined here */
  197. static struct php_ctlr_state_s *php_ctlr_list_head; /* HPC state linked list */
  198. static int ctlr_seq_num = 0; /* Controller sequenc # */
  199. static spinlock_t list_lock;
  200. static atomic_t shpchp_num_controllers = ATOMIC_INIT(0);
  201. static irqreturn_t shpc_isr(int irq, void *dev_id);
  202. static void start_int_poll_timer(struct php_ctlr_state_s *php_ctlr, int sec);
  203. static int hpc_check_cmd_status(struct controller *ctrl);
  204. static inline u8 shpc_readb(struct controller *ctrl, int reg)
  205. {
  206. return readb(ctrl->hpc_ctlr_handle->creg + reg);
  207. }
  208. static inline void shpc_writeb(struct controller *ctrl, int reg, u8 val)
  209. {
  210. writeb(val, ctrl->hpc_ctlr_handle->creg + reg);
  211. }
  212. static inline u16 shpc_readw(struct controller *ctrl, int reg)
  213. {
  214. return readw(ctrl->hpc_ctlr_handle->creg + reg);
  215. }
  216. static inline void shpc_writew(struct controller *ctrl, int reg, u16 val)
  217. {
  218. writew(val, ctrl->hpc_ctlr_handle->creg + reg);
  219. }
  220. static inline u32 shpc_readl(struct controller *ctrl, int reg)
  221. {
  222. return readl(ctrl->hpc_ctlr_handle->creg + reg);
  223. }
  224. static inline void shpc_writel(struct controller *ctrl, int reg, u32 val)
  225. {
  226. writel(val, ctrl->hpc_ctlr_handle->creg + reg);
  227. }
  228. static inline int shpc_indirect_read(struct controller *ctrl, int index,
  229. u32 *value)
  230. {
  231. int rc;
  232. u32 cap_offset = ctrl->cap_offset;
  233. struct pci_dev *pdev = ctrl->pci_dev;
  234. rc = pci_write_config_byte(pdev, cap_offset + DWORD_SELECT, index);
  235. if (rc)
  236. return rc;
  237. return pci_read_config_dword(pdev, cap_offset + DWORD_DATA, value);
  238. }
  239. /*
  240. * This is the interrupt polling timeout function.
  241. */
  242. static void int_poll_timeout(unsigned long lphp_ctlr)
  243. {
  244. struct php_ctlr_state_s *php_ctlr =
  245. (struct php_ctlr_state_s *)lphp_ctlr;
  246. DBG_ENTER_ROUTINE
  247. /* Poll for interrupt events. regs == NULL => polling */
  248. shpc_isr(0, php_ctlr->callback_instance_id);
  249. init_timer(&php_ctlr->int_poll_timer);
  250. if (!shpchp_poll_time)
  251. shpchp_poll_time = 2; /* default polling interval is 2 sec */
  252. start_int_poll_timer(php_ctlr, shpchp_poll_time);
  253. DBG_LEAVE_ROUTINE
  254. }
  255. /*
  256. * This function starts the interrupt polling timer.
  257. */
  258. static void start_int_poll_timer(struct php_ctlr_state_s *php_ctlr, int sec)
  259. {
  260. /* Clamp to sane value */
  261. if ((sec <= 0) || (sec > 60))
  262. sec = 2;
  263. php_ctlr->int_poll_timer.function = &int_poll_timeout;
  264. php_ctlr->int_poll_timer.data = (unsigned long)php_ctlr;
  265. php_ctlr->int_poll_timer.expires = jiffies + sec * HZ;
  266. add_timer(&php_ctlr->int_poll_timer);
  267. }
  268. static inline int is_ctrl_busy(struct controller *ctrl)
  269. {
  270. u16 cmd_status = shpc_readw(ctrl, CMD_STATUS);
  271. return cmd_status & 0x1;
  272. }
  273. /*
  274. * Returns 1 if SHPC finishes executing a command within 1 sec,
  275. * otherwise returns 0.
  276. */
  277. static inline int shpc_poll_ctrl_busy(struct controller *ctrl)
  278. {
  279. int i;
  280. if (!is_ctrl_busy(ctrl))
  281. return 1;
  282. /* Check every 0.1 sec for a total of 1 sec */
  283. for (i = 0; i < 10; i++) {
  284. msleep(100);
  285. if (!is_ctrl_busy(ctrl))
  286. return 1;
  287. }
  288. return 0;
  289. }
  290. static inline int shpc_wait_cmd(struct controller *ctrl)
  291. {
  292. int retval = 0;
  293. unsigned long timeout = msecs_to_jiffies(1000);
  294. int rc;
  295. if (shpchp_poll_mode)
  296. rc = shpc_poll_ctrl_busy(ctrl);
  297. else
  298. rc = wait_event_interruptible_timeout(ctrl->queue,
  299. !ctrl->cmd_busy, timeout);
  300. if (!rc && is_ctrl_busy(ctrl)) {
  301. retval = -EIO;
  302. err("Command not completed in 1000 msec\n");
  303. } else if (rc < 0) {
  304. retval = -EINTR;
  305. info("Command was interrupted by a signal\n");
  306. }
  307. ctrl->cmd_busy = 0;
  308. return retval;
  309. }
  310. static int shpc_write_cmd(struct slot *slot, u8 t_slot, u8 cmd)
  311. {
  312. struct controller *ctrl = slot->ctrl;
  313. u16 cmd_status;
  314. int retval = 0;
  315. u16 temp_word;
  316. DBG_ENTER_ROUTINE
  317. mutex_lock(&slot->ctrl->cmd_lock);
  318. if (!shpc_poll_ctrl_busy(ctrl)) {
  319. /* After 1 sec and and the controller is still busy */
  320. err("%s : Controller is still busy after 1 sec.\n",
  321. __FUNCTION__);
  322. retval = -EBUSY;
  323. goto out;
  324. }
  325. ++t_slot;
  326. temp_word = (t_slot << 8) | (cmd & 0xFF);
  327. dbg("%s: t_slot %x cmd %x\n", __FUNCTION__, t_slot, cmd);
  328. /* To make sure the Controller Busy bit is 0 before we send out the
  329. * command.
  330. */
  331. slot->ctrl->cmd_busy = 1;
  332. shpc_writew(ctrl, CMD, temp_word);
  333. /*
  334. * Wait for command completion.
  335. */
  336. retval = shpc_wait_cmd(slot->ctrl);
  337. if (retval)
  338. goto out;
  339. cmd_status = hpc_check_cmd_status(slot->ctrl);
  340. if (cmd_status) {
  341. err("%s: Failed to issued command 0x%x (error code = %d)\n",
  342. __FUNCTION__, cmd, cmd_status);
  343. retval = -EIO;
  344. }
  345. out:
  346. mutex_unlock(&slot->ctrl->cmd_lock);
  347. DBG_LEAVE_ROUTINE
  348. return retval;
  349. }
  350. static int hpc_check_cmd_status(struct controller *ctrl)
  351. {
  352. u16 cmd_status;
  353. int retval = 0;
  354. DBG_ENTER_ROUTINE
  355. cmd_status = shpc_readw(ctrl, CMD_STATUS) & 0x000F;
  356. switch (cmd_status >> 1) {
  357. case 0:
  358. retval = 0;
  359. break;
  360. case 1:
  361. retval = SWITCH_OPEN;
  362. err("%s: Switch opened!\n", __FUNCTION__);
  363. break;
  364. case 2:
  365. retval = INVALID_CMD;
  366. err("%s: Invalid HPC command!\n", __FUNCTION__);
  367. break;
  368. case 4:
  369. retval = INVALID_SPEED_MODE;
  370. err("%s: Invalid bus speed/mode!\n", __FUNCTION__);
  371. break;
  372. default:
  373. retval = cmd_status;
  374. }
  375. DBG_LEAVE_ROUTINE
  376. return retval;
  377. }
  378. static int hpc_get_attention_status(struct slot *slot, u8 *status)
  379. {
  380. struct controller *ctrl = slot->ctrl;
  381. u32 slot_reg;
  382. u8 state;
  383. DBG_ENTER_ROUTINE
  384. slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
  385. state = (slot_reg & ATN_LED_STATE_MASK) >> ATN_LED_STATE_SHIFT;
  386. switch (state) {
  387. case ATN_LED_STATE_ON:
  388. *status = 1; /* On */
  389. break;
  390. case ATN_LED_STATE_BLINK:
  391. *status = 2; /* Blink */
  392. break;
  393. case ATN_LED_STATE_OFF:
  394. *status = 0; /* Off */
  395. break;
  396. default:
  397. *status = 0xFF; /* Reserved */
  398. break;
  399. }
  400. DBG_LEAVE_ROUTINE
  401. return 0;
  402. }
  403. static int hpc_get_power_status(struct slot * slot, u8 *status)
  404. {
  405. struct controller *ctrl = slot->ctrl;
  406. u32 slot_reg;
  407. u8 state;
  408. DBG_ENTER_ROUTINE
  409. slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
  410. state = (slot_reg & SLOT_STATE_MASK) >> SLOT_STATE_SHIFT;
  411. switch (state) {
  412. case SLOT_STATE_PWRONLY:
  413. *status = 2; /* Powered only */
  414. break;
  415. case SLOT_STATE_ENABLED:
  416. *status = 1; /* Enabled */
  417. break;
  418. case SLOT_STATE_DISABLED:
  419. *status = 0; /* Disabled */
  420. break;
  421. default:
  422. *status = 0xFF; /* Reserved */
  423. break;
  424. }
  425. DBG_LEAVE_ROUTINE
  426. return 0;
  427. }
  428. static int hpc_get_latch_status(struct slot *slot, u8 *status)
  429. {
  430. struct controller *ctrl = slot->ctrl;
  431. u32 slot_reg;
  432. DBG_ENTER_ROUTINE
  433. slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
  434. *status = !!(slot_reg & MRL_SENSOR); /* 0 -> close; 1 -> open */
  435. DBG_LEAVE_ROUTINE
  436. return 0;
  437. }
  438. static int hpc_get_adapter_status(struct slot *slot, u8 *status)
  439. {
  440. struct controller *ctrl = slot->ctrl;
  441. u32 slot_reg;
  442. u8 state;
  443. DBG_ENTER_ROUTINE
  444. slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
  445. state = (slot_reg & PRSNT_MASK) >> PRSNT_SHIFT;
  446. *status = (state != 0x3) ? 1 : 0;
  447. DBG_LEAVE_ROUTINE
  448. return 0;
  449. }
  450. static int hpc_get_prog_int(struct slot *slot, u8 *prog_int)
  451. {
  452. struct controller *ctrl = slot->ctrl;
  453. DBG_ENTER_ROUTINE
  454. *prog_int = shpc_readb(ctrl, PROG_INTERFACE);
  455. DBG_LEAVE_ROUTINE
  456. return 0;
  457. }
  458. static int hpc_get_adapter_speed(struct slot *slot, enum pci_bus_speed *value)
  459. {
  460. int retval = 0;
  461. struct controller *ctrl = slot->ctrl;
  462. u32 slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
  463. u8 m66_cap = !!(slot_reg & MHZ66_CAP);
  464. u8 pi, pcix_cap;
  465. DBG_ENTER_ROUTINE
  466. if ((retval = hpc_get_prog_int(slot, &pi)))
  467. return retval;
  468. switch (pi) {
  469. case 1:
  470. pcix_cap = (slot_reg & PCIX_CAP_MASK_PI1) >> PCIX_CAP_SHIFT;
  471. break;
  472. case 2:
  473. pcix_cap = (slot_reg & PCIX_CAP_MASK_PI2) >> PCIX_CAP_SHIFT;
  474. break;
  475. default:
  476. return -ENODEV;
  477. }
  478. dbg("%s: slot_reg = %x, pcix_cap = %x, m66_cap = %x\n",
  479. __FUNCTION__, slot_reg, pcix_cap, m66_cap);
  480. switch (pcix_cap) {
  481. case 0x0:
  482. *value = m66_cap ? PCI_SPEED_66MHz : PCI_SPEED_33MHz;
  483. break;
  484. case 0x1:
  485. *value = PCI_SPEED_66MHz_PCIX;
  486. break;
  487. case 0x3:
  488. *value = PCI_SPEED_133MHz_PCIX;
  489. break;
  490. case 0x4:
  491. *value = PCI_SPEED_133MHz_PCIX_266;
  492. break;
  493. case 0x5:
  494. *value = PCI_SPEED_133MHz_PCIX_533;
  495. break;
  496. case 0x2:
  497. default:
  498. *value = PCI_SPEED_UNKNOWN;
  499. retval = -ENODEV;
  500. break;
  501. }
  502. dbg("Adapter speed = %d\n", *value);
  503. DBG_LEAVE_ROUTINE
  504. return retval;
  505. }
  506. static int hpc_get_mode1_ECC_cap(struct slot *slot, u8 *mode)
  507. {
  508. struct controller *ctrl = slot->ctrl;
  509. u16 sec_bus_status;
  510. u8 pi;
  511. int retval = 0;
  512. DBG_ENTER_ROUTINE
  513. pi = shpc_readb(ctrl, PROG_INTERFACE);
  514. sec_bus_status = shpc_readw(ctrl, SEC_BUS_CONFIG);
  515. if (pi == 2) {
  516. *mode = (sec_bus_status & 0x0100) >> 8;
  517. } else {
  518. retval = -1;
  519. }
  520. dbg("Mode 1 ECC cap = %d\n", *mode);
  521. DBG_LEAVE_ROUTINE
  522. return retval;
  523. }
  524. static int hpc_query_power_fault(struct slot * slot)
  525. {
  526. struct controller *ctrl = slot->ctrl;
  527. u32 slot_reg;
  528. DBG_ENTER_ROUTINE
  529. slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
  530. DBG_LEAVE_ROUTINE
  531. /* Note: Logic 0 => fault */
  532. return !(slot_reg & POWER_FAULT);
  533. }
  534. static int hpc_set_attention_status(struct slot *slot, u8 value)
  535. {
  536. u8 slot_cmd = 0;
  537. switch (value) {
  538. case 0 :
  539. slot_cmd = SET_ATTN_OFF; /* OFF */
  540. break;
  541. case 1:
  542. slot_cmd = SET_ATTN_ON; /* ON */
  543. break;
  544. case 2:
  545. slot_cmd = SET_ATTN_BLINK; /* BLINK */
  546. break;
  547. default:
  548. return -1;
  549. }
  550. return shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
  551. }
  552. static void hpc_set_green_led_on(struct slot *slot)
  553. {
  554. shpc_write_cmd(slot, slot->hp_slot, SET_PWR_ON);
  555. }
  556. static void hpc_set_green_led_off(struct slot *slot)
  557. {
  558. shpc_write_cmd(slot, slot->hp_slot, SET_PWR_OFF);
  559. }
  560. static void hpc_set_green_led_blink(struct slot *slot)
  561. {
  562. shpc_write_cmd(slot, slot->hp_slot, SET_PWR_BLINK);
  563. }
  564. int shpc_get_ctlr_slot_config(struct controller *ctrl,
  565. int *num_ctlr_slots, /* number of slots in this HPC */
  566. int *first_device_num, /* PCI dev num of the first slot in this SHPC */
  567. int *physical_slot_num, /* phy slot num of the first slot in this SHPC */
  568. int *updown, /* physical_slot_num increament: 1 or -1 */
  569. int *flags)
  570. {
  571. u32 slot_config;
  572. DBG_ENTER_ROUTINE
  573. slot_config = shpc_readl(ctrl, SLOT_CONFIG);
  574. *first_device_num = (slot_config & FIRST_DEV_NUM) >> 8;
  575. *num_ctlr_slots = slot_config & SLOT_NUM;
  576. *physical_slot_num = (slot_config & PSN) >> 16;
  577. *updown = ((slot_config & UPDOWN) >> 29) ? 1 : -1;
  578. dbg("%s: physical_slot_num = %x\n", __FUNCTION__, *physical_slot_num);
  579. DBG_LEAVE_ROUTINE
  580. return 0;
  581. }
  582. static void hpc_release_ctlr(struct controller *ctrl)
  583. {
  584. struct php_ctlr_state_s *php_ctlr = ctrl->hpc_ctlr_handle;
  585. struct php_ctlr_state_s *p, *p_prev;
  586. int i;
  587. u32 slot_reg, serr_int;
  588. DBG_ENTER_ROUTINE
  589. /*
  590. * Mask event interrupts and SERRs of all slots
  591. */
  592. for (i = 0; i < ctrl->num_slots; i++) {
  593. slot_reg = shpc_readl(ctrl, SLOT_REG(i));
  594. slot_reg |= (PRSNT_CHANGE_INTR_MASK | ISO_PFAULT_INTR_MASK |
  595. BUTTON_PRESS_INTR_MASK | MRL_CHANGE_INTR_MASK |
  596. CON_PFAULT_INTR_MASK | MRL_CHANGE_SERR_MASK |
  597. CON_PFAULT_SERR_MASK);
  598. slot_reg &= ~SLOT_REG_RSVDZ_MASK;
  599. shpc_writel(ctrl, SLOT_REG(i), slot_reg);
  600. }
  601. cleanup_slots(ctrl);
  602. /*
  603. * Mask SERR and System Interrut generation
  604. */
  605. serr_int = shpc_readl(ctrl, SERR_INTR_ENABLE);
  606. serr_int |= (GLOBAL_INTR_MASK | GLOBAL_SERR_MASK |
  607. COMMAND_INTR_MASK | ARBITER_SERR_MASK);
  608. serr_int &= ~SERR_INTR_RSVDZ_MASK;
  609. shpc_writel(ctrl, SERR_INTR_ENABLE, serr_int);
  610. if (shpchp_poll_mode) {
  611. del_timer(&php_ctlr->int_poll_timer);
  612. } else {
  613. if (php_ctlr->irq) {
  614. free_irq(php_ctlr->irq, ctrl);
  615. php_ctlr->irq = 0;
  616. pci_disable_msi(php_ctlr->pci_dev);
  617. }
  618. }
  619. if (php_ctlr->pci_dev) {
  620. iounmap(php_ctlr->creg);
  621. release_mem_region(ctrl->mmio_base, ctrl->mmio_size);
  622. php_ctlr->pci_dev = NULL;
  623. }
  624. spin_lock(&list_lock);
  625. p = php_ctlr_list_head;
  626. p_prev = NULL;
  627. while (p) {
  628. if (p == php_ctlr) {
  629. if (p_prev)
  630. p_prev->pnext = p->pnext;
  631. else
  632. php_ctlr_list_head = p->pnext;
  633. break;
  634. } else {
  635. p_prev = p;
  636. p = p->pnext;
  637. }
  638. }
  639. spin_unlock(&list_lock);
  640. kfree(php_ctlr);
  641. /*
  642. * If this is the last controller to be released, destroy the
  643. * shpchpd work queue
  644. */
  645. if (atomic_dec_and_test(&shpchp_num_controllers))
  646. destroy_workqueue(shpchp_wq);
  647. DBG_LEAVE_ROUTINE
  648. }
  649. static int hpc_power_on_slot(struct slot * slot)
  650. {
  651. int retval;
  652. DBG_ENTER_ROUTINE
  653. retval = shpc_write_cmd(slot, slot->hp_slot, SET_SLOT_PWR);
  654. if (retval) {
  655. err("%s: Write command failed!\n", __FUNCTION__);
  656. return retval;
  657. }
  658. DBG_LEAVE_ROUTINE
  659. return 0;
  660. }
  661. static int hpc_slot_enable(struct slot * slot)
  662. {
  663. int retval;
  664. DBG_ENTER_ROUTINE
  665. /* Slot - Enable, Power Indicator - Blink, Attention Indicator - Off */
  666. retval = shpc_write_cmd(slot, slot->hp_slot,
  667. SET_SLOT_ENABLE | SET_PWR_BLINK | SET_ATTN_OFF);
  668. if (retval) {
  669. err("%s: Write command failed!\n", __FUNCTION__);
  670. return retval;
  671. }
  672. DBG_LEAVE_ROUTINE
  673. return 0;
  674. }
  675. static int hpc_slot_disable(struct slot * slot)
  676. {
  677. int retval;
  678. DBG_ENTER_ROUTINE
  679. /* Slot - Disable, Power Indicator - Off, Attention Indicator - On */
  680. retval = shpc_write_cmd(slot, slot->hp_slot,
  681. SET_SLOT_DISABLE | SET_PWR_OFF | SET_ATTN_ON);
  682. if (retval) {
  683. err("%s: Write command failed!\n", __FUNCTION__);
  684. return retval;
  685. }
  686. DBG_LEAVE_ROUTINE
  687. return 0;
  688. }
  689. static int hpc_set_bus_speed_mode(struct slot * slot, enum pci_bus_speed value)
  690. {
  691. int retval;
  692. struct controller *ctrl = slot->ctrl;
  693. u8 pi, cmd;
  694. DBG_ENTER_ROUTINE
  695. pi = shpc_readb(ctrl, PROG_INTERFACE);
  696. if ((pi == 1) && (value > PCI_SPEED_133MHz_PCIX))
  697. return -EINVAL;
  698. switch (value) {
  699. case PCI_SPEED_33MHz:
  700. cmd = SETA_PCI_33MHZ;
  701. break;
  702. case PCI_SPEED_66MHz:
  703. cmd = SETA_PCI_66MHZ;
  704. break;
  705. case PCI_SPEED_66MHz_PCIX:
  706. cmd = SETA_PCIX_66MHZ;
  707. break;
  708. case PCI_SPEED_100MHz_PCIX:
  709. cmd = SETA_PCIX_100MHZ;
  710. break;
  711. case PCI_SPEED_133MHz_PCIX:
  712. cmd = SETA_PCIX_133MHZ;
  713. break;
  714. case PCI_SPEED_66MHz_PCIX_ECC:
  715. cmd = SETB_PCIX_66MHZ_EM;
  716. break;
  717. case PCI_SPEED_100MHz_PCIX_ECC:
  718. cmd = SETB_PCIX_100MHZ_EM;
  719. break;
  720. case PCI_SPEED_133MHz_PCIX_ECC:
  721. cmd = SETB_PCIX_133MHZ_EM;
  722. break;
  723. case PCI_SPEED_66MHz_PCIX_266:
  724. cmd = SETB_PCIX_66MHZ_266;
  725. break;
  726. case PCI_SPEED_100MHz_PCIX_266:
  727. cmd = SETB_PCIX_100MHZ_266;
  728. break;
  729. case PCI_SPEED_133MHz_PCIX_266:
  730. cmd = SETB_PCIX_133MHZ_266;
  731. break;
  732. case PCI_SPEED_66MHz_PCIX_533:
  733. cmd = SETB_PCIX_66MHZ_533;
  734. break;
  735. case PCI_SPEED_100MHz_PCIX_533:
  736. cmd = SETB_PCIX_100MHZ_533;
  737. break;
  738. case PCI_SPEED_133MHz_PCIX_533:
  739. cmd = SETB_PCIX_133MHZ_533;
  740. break;
  741. default:
  742. return -EINVAL;
  743. }
  744. retval = shpc_write_cmd(slot, 0, cmd);
  745. if (retval)
  746. err("%s: Write command failed!\n", __FUNCTION__);
  747. DBG_LEAVE_ROUTINE
  748. return retval;
  749. }
  750. static irqreturn_t shpc_isr(int irq, void *dev_id)
  751. {
  752. struct controller *ctrl = (struct controller *)dev_id;
  753. struct php_ctlr_state_s *php_ctlr = ctrl->hpc_ctlr_handle;
  754. u32 serr_int, slot_reg, intr_loc, intr_loc2;
  755. int hp_slot;
  756. /* Check to see if it was our interrupt */
  757. intr_loc = shpc_readl(ctrl, INTR_LOC);
  758. if (!intr_loc)
  759. return IRQ_NONE;
  760. dbg("%s: intr_loc = %x\n",__FUNCTION__, intr_loc);
  761. if(!shpchp_poll_mode) {
  762. /*
  763. * Mask Global Interrupt Mask - see implementation
  764. * note on p. 139 of SHPC spec rev 1.0
  765. */
  766. serr_int = shpc_readl(ctrl, SERR_INTR_ENABLE);
  767. serr_int |= GLOBAL_INTR_MASK;
  768. serr_int &= ~SERR_INTR_RSVDZ_MASK;
  769. shpc_writel(ctrl, SERR_INTR_ENABLE, serr_int);
  770. intr_loc2 = shpc_readl(ctrl, INTR_LOC);
  771. dbg("%s: intr_loc2 = %x\n",__FUNCTION__, intr_loc2);
  772. }
  773. if (intr_loc & CMD_INTR_PENDING) {
  774. /*
  775. * Command Complete Interrupt Pending
  776. * RO only - clear by writing 1 to the Command Completion
  777. * Detect bit in Controller SERR-INT register
  778. */
  779. serr_int = shpc_readl(ctrl, SERR_INTR_ENABLE);
  780. serr_int &= ~SERR_INTR_RSVDZ_MASK;
  781. shpc_writel(ctrl, SERR_INTR_ENABLE, serr_int);
  782. ctrl->cmd_busy = 0;
  783. wake_up_interruptible(&ctrl->queue);
  784. }
  785. if (!(intr_loc & ~CMD_INTR_PENDING))
  786. goto out;
  787. for (hp_slot = 0; hp_slot < ctrl->num_slots; hp_slot++) {
  788. /* To find out which slot has interrupt pending */
  789. if (!(intr_loc & SLOT_INTR_PENDING(hp_slot)))
  790. continue;
  791. slot_reg = shpc_readl(ctrl, SLOT_REG(hp_slot));
  792. dbg("%s: Slot %x with intr, slot register = %x\n",
  793. __FUNCTION__, hp_slot, slot_reg);
  794. if (slot_reg & MRL_CHANGE_DETECTED)
  795. php_ctlr->switch_change_callback(
  796. hp_slot, php_ctlr->callback_instance_id);
  797. if (slot_reg & BUTTON_PRESS_DETECTED)
  798. php_ctlr->attention_button_callback(
  799. hp_slot, php_ctlr->callback_instance_id);
  800. if (slot_reg & PRSNT_CHANGE_DETECTED)
  801. php_ctlr->presence_change_callback(
  802. hp_slot , php_ctlr->callback_instance_id);
  803. if (slot_reg & (ISO_PFAULT_DETECTED | CON_PFAULT_DETECTED))
  804. php_ctlr->power_fault_callback(
  805. hp_slot, php_ctlr->callback_instance_id);
  806. /* Clear all slot events */
  807. slot_reg &= ~SLOT_REG_RSVDZ_MASK;
  808. shpc_writel(ctrl, SLOT_REG(hp_slot), slot_reg);
  809. }
  810. out:
  811. if (!shpchp_poll_mode) {
  812. /* Unmask Global Interrupt Mask */
  813. serr_int = shpc_readl(ctrl, SERR_INTR_ENABLE);
  814. serr_int &= ~(GLOBAL_INTR_MASK | SERR_INTR_RSVDZ_MASK);
  815. shpc_writel(ctrl, SERR_INTR_ENABLE, serr_int);
  816. }
  817. return IRQ_HANDLED;
  818. }
  819. static int hpc_get_max_bus_speed (struct slot *slot, enum pci_bus_speed *value)
  820. {
  821. int retval = 0;
  822. struct controller *ctrl = slot->ctrl;
  823. enum pci_bus_speed bus_speed = PCI_SPEED_UNKNOWN;
  824. u8 pi = shpc_readb(ctrl, PROG_INTERFACE);
  825. u32 slot_avail1 = shpc_readl(ctrl, SLOT_AVAIL1);
  826. u32 slot_avail2 = shpc_readl(ctrl, SLOT_AVAIL2);
  827. DBG_ENTER_ROUTINE
  828. if (pi == 2) {
  829. if (slot_avail2 & SLOT_133MHZ_PCIX_533)
  830. bus_speed = PCI_SPEED_133MHz_PCIX_533;
  831. else if (slot_avail2 & SLOT_100MHZ_PCIX_533)
  832. bus_speed = PCI_SPEED_100MHz_PCIX_533;
  833. else if (slot_avail2 & SLOT_66MHZ_PCIX_533)
  834. bus_speed = PCI_SPEED_66MHz_PCIX_533;
  835. else if (slot_avail2 & SLOT_133MHZ_PCIX_266)
  836. bus_speed = PCI_SPEED_133MHz_PCIX_266;
  837. else if (slot_avail2 & SLOT_100MHZ_PCIX_266)
  838. bus_speed = PCI_SPEED_100MHz_PCIX_266;
  839. else if (slot_avail2 & SLOT_66MHZ_PCIX_266)
  840. bus_speed = PCI_SPEED_66MHz_PCIX_266;
  841. }
  842. if (bus_speed == PCI_SPEED_UNKNOWN) {
  843. if (slot_avail1 & SLOT_133MHZ_PCIX)
  844. bus_speed = PCI_SPEED_133MHz_PCIX;
  845. else if (slot_avail1 & SLOT_100MHZ_PCIX)
  846. bus_speed = PCI_SPEED_100MHz_PCIX;
  847. else if (slot_avail1 & SLOT_66MHZ_PCIX)
  848. bus_speed = PCI_SPEED_66MHz_PCIX;
  849. else if (slot_avail2 & SLOT_66MHZ)
  850. bus_speed = PCI_SPEED_66MHz;
  851. else if (slot_avail1 & SLOT_33MHZ)
  852. bus_speed = PCI_SPEED_33MHz;
  853. else
  854. retval = -ENODEV;
  855. }
  856. *value = bus_speed;
  857. dbg("Max bus speed = %d\n", bus_speed);
  858. DBG_LEAVE_ROUTINE
  859. return retval;
  860. }
  861. static int hpc_get_cur_bus_speed (struct slot *slot, enum pci_bus_speed *value)
  862. {
  863. int retval = 0;
  864. struct controller *ctrl = slot->ctrl;
  865. enum pci_bus_speed bus_speed = PCI_SPEED_UNKNOWN;
  866. u16 sec_bus_reg = shpc_readw(ctrl, SEC_BUS_CONFIG);
  867. u8 pi = shpc_readb(ctrl, PROG_INTERFACE);
  868. u8 speed_mode = (pi == 2) ? (sec_bus_reg & 0xF) : (sec_bus_reg & 0x7);
  869. DBG_ENTER_ROUTINE
  870. if ((pi == 1) && (speed_mode > 4)) {
  871. *value = PCI_SPEED_UNKNOWN;
  872. return -ENODEV;
  873. }
  874. switch (speed_mode) {
  875. case 0x0:
  876. *value = PCI_SPEED_33MHz;
  877. break;
  878. case 0x1:
  879. *value = PCI_SPEED_66MHz;
  880. break;
  881. case 0x2:
  882. *value = PCI_SPEED_66MHz_PCIX;
  883. break;
  884. case 0x3:
  885. *value = PCI_SPEED_100MHz_PCIX;
  886. break;
  887. case 0x4:
  888. *value = PCI_SPEED_133MHz_PCIX;
  889. break;
  890. case 0x5:
  891. *value = PCI_SPEED_66MHz_PCIX_ECC;
  892. break;
  893. case 0x6:
  894. *value = PCI_SPEED_100MHz_PCIX_ECC;
  895. break;
  896. case 0x7:
  897. *value = PCI_SPEED_133MHz_PCIX_ECC;
  898. break;
  899. case 0x8:
  900. *value = PCI_SPEED_66MHz_PCIX_266;
  901. break;
  902. case 0x9:
  903. *value = PCI_SPEED_100MHz_PCIX_266;
  904. break;
  905. case 0xa:
  906. *value = PCI_SPEED_133MHz_PCIX_266;
  907. break;
  908. case 0xb:
  909. *value = PCI_SPEED_66MHz_PCIX_533;
  910. break;
  911. case 0xc:
  912. *value = PCI_SPEED_100MHz_PCIX_533;
  913. break;
  914. case 0xd:
  915. *value = PCI_SPEED_133MHz_PCIX_533;
  916. break;
  917. default:
  918. *value = PCI_SPEED_UNKNOWN;
  919. retval = -ENODEV;
  920. break;
  921. }
  922. dbg("Current bus speed = %d\n", bus_speed);
  923. DBG_LEAVE_ROUTINE
  924. return retval;
  925. }
  926. static struct hpc_ops shpchp_hpc_ops = {
  927. .power_on_slot = hpc_power_on_slot,
  928. .slot_enable = hpc_slot_enable,
  929. .slot_disable = hpc_slot_disable,
  930. .set_bus_speed_mode = hpc_set_bus_speed_mode,
  931. .set_attention_status = hpc_set_attention_status,
  932. .get_power_status = hpc_get_power_status,
  933. .get_attention_status = hpc_get_attention_status,
  934. .get_latch_status = hpc_get_latch_status,
  935. .get_adapter_status = hpc_get_adapter_status,
  936. .get_max_bus_speed = hpc_get_max_bus_speed,
  937. .get_cur_bus_speed = hpc_get_cur_bus_speed,
  938. .get_adapter_speed = hpc_get_adapter_speed,
  939. .get_mode1_ECC_cap = hpc_get_mode1_ECC_cap,
  940. .get_prog_int = hpc_get_prog_int,
  941. .query_power_fault = hpc_query_power_fault,
  942. .green_led_on = hpc_set_green_led_on,
  943. .green_led_off = hpc_set_green_led_off,
  944. .green_led_blink = hpc_set_green_led_blink,
  945. .release_ctlr = hpc_release_ctlr,
  946. };
  947. int shpc_init(struct controller * ctrl, struct pci_dev * pdev)
  948. {
  949. struct php_ctlr_state_s *php_ctlr, *p;
  950. void *instance_id = ctrl;
  951. int rc, num_slots = 0;
  952. u8 hp_slot;
  953. u32 shpc_base_offset;
  954. u32 tempdword, slot_reg, slot_config;
  955. u8 i;
  956. DBG_ENTER_ROUTINE
  957. ctrl->pci_dev = pdev; /* pci_dev of the P2P bridge */
  958. spin_lock_init(&list_lock);
  959. php_ctlr = kzalloc(sizeof(*php_ctlr), GFP_KERNEL);
  960. if (!php_ctlr) { /* allocate controller state data */
  961. err("%s: HPC controller memory allocation error!\n", __FUNCTION__);
  962. goto abort;
  963. }
  964. php_ctlr->pci_dev = pdev; /* save pci_dev in context */
  965. if ((pdev->vendor == PCI_VENDOR_ID_AMD) || (pdev->device ==
  966. PCI_DEVICE_ID_AMD_GOLAM_7450)) {
  967. /* amd shpc driver doesn't use Base Offset; assume 0 */
  968. ctrl->mmio_base = pci_resource_start(pdev, 0);
  969. ctrl->mmio_size = pci_resource_len(pdev, 0);
  970. } else {
  971. ctrl->cap_offset = pci_find_capability(pdev, PCI_CAP_ID_SHPC);
  972. if (!ctrl->cap_offset) {
  973. err("%s : cap_offset == 0\n", __FUNCTION__);
  974. goto abort_free_ctlr;
  975. }
  976. dbg("%s: cap_offset = %x\n", __FUNCTION__, ctrl->cap_offset);
  977. rc = shpc_indirect_read(ctrl, 0, &shpc_base_offset);
  978. if (rc) {
  979. err("%s: cannot read base_offset\n", __FUNCTION__);
  980. goto abort_free_ctlr;
  981. }
  982. rc = shpc_indirect_read(ctrl, 3, &tempdword);
  983. if (rc) {
  984. err("%s: cannot read slot config\n", __FUNCTION__);
  985. goto abort_free_ctlr;
  986. }
  987. num_slots = tempdword & SLOT_NUM;
  988. dbg("%s: num_slots (indirect) %x\n", __FUNCTION__, num_slots);
  989. for (i = 0; i < 9 + num_slots; i++) {
  990. rc = shpc_indirect_read(ctrl, i, &tempdword);
  991. if (rc) {
  992. err("%s: cannot read creg (index = %d)\n",
  993. __FUNCTION__, i);
  994. goto abort_free_ctlr;
  995. }
  996. dbg("%s: offset %d: value %x\n", __FUNCTION__,i,
  997. tempdword);
  998. }
  999. ctrl->mmio_base =
  1000. pci_resource_start(pdev, 0) + shpc_base_offset;
  1001. ctrl->mmio_size = 0x24 + 0x4 * num_slots;
  1002. }
  1003. info("HPC vendor_id %x device_id %x ss_vid %x ss_did %x\n", pdev->vendor, pdev->device, pdev->subsystem_vendor,
  1004. pdev->subsystem_device);
  1005. if (pci_enable_device(pdev))
  1006. goto abort_free_ctlr;
  1007. if (!request_mem_region(ctrl->mmio_base, ctrl->mmio_size, MY_NAME)) {
  1008. err("%s: cannot reserve MMIO region\n", __FUNCTION__);
  1009. goto abort_free_ctlr;
  1010. }
  1011. php_ctlr->creg = ioremap(ctrl->mmio_base, ctrl->mmio_size);
  1012. if (!php_ctlr->creg) {
  1013. err("%s: cannot remap MMIO region %lx @ %lx\n", __FUNCTION__,
  1014. ctrl->mmio_size, ctrl->mmio_base);
  1015. release_mem_region(ctrl->mmio_base, ctrl->mmio_size);
  1016. goto abort_free_ctlr;
  1017. }
  1018. dbg("%s: php_ctlr->creg %p\n", __FUNCTION__, php_ctlr->creg);
  1019. mutex_init(&ctrl->crit_sect);
  1020. mutex_init(&ctrl->cmd_lock);
  1021. /* Setup wait queue */
  1022. init_waitqueue_head(&ctrl->queue);
  1023. /* Find the IRQ */
  1024. php_ctlr->irq = pdev->irq;
  1025. php_ctlr->attention_button_callback = shpchp_handle_attention_button,
  1026. php_ctlr->switch_change_callback = shpchp_handle_switch_change;
  1027. php_ctlr->presence_change_callback = shpchp_handle_presence_change;
  1028. php_ctlr->power_fault_callback = shpchp_handle_power_fault;
  1029. php_ctlr->callback_instance_id = instance_id;
  1030. ctrl->hpc_ctlr_handle = php_ctlr;
  1031. ctrl->hpc_ops = &shpchp_hpc_ops;
  1032. /* Return PCI Controller Info */
  1033. slot_config = shpc_readl(ctrl, SLOT_CONFIG);
  1034. php_ctlr->slot_device_offset = (slot_config & FIRST_DEV_NUM) >> 8;
  1035. php_ctlr->num_slots = slot_config & SLOT_NUM;
  1036. dbg("%s: slot_device_offset %x\n", __FUNCTION__, php_ctlr->slot_device_offset);
  1037. dbg("%s: num_slots %x\n", __FUNCTION__, php_ctlr->num_slots);
  1038. /* Mask Global Interrupt Mask & Command Complete Interrupt Mask */
  1039. tempdword = shpc_readl(ctrl, SERR_INTR_ENABLE);
  1040. dbg("%s: SERR_INTR_ENABLE = %x\n", __FUNCTION__, tempdword);
  1041. tempdword |= (GLOBAL_INTR_MASK | GLOBAL_SERR_MASK |
  1042. COMMAND_INTR_MASK | ARBITER_SERR_MASK);
  1043. tempdword &= ~SERR_INTR_RSVDZ_MASK;
  1044. shpc_writel(ctrl, SERR_INTR_ENABLE, tempdword);
  1045. tempdword = shpc_readl(ctrl, SERR_INTR_ENABLE);
  1046. dbg("%s: SERR_INTR_ENABLE = %x\n", __FUNCTION__, tempdword);
  1047. /* Mask the MRL sensor SERR Mask of individual slot in
  1048. * Slot SERR-INT Mask & clear all the existing event if any
  1049. */
  1050. for (hp_slot = 0; hp_slot < php_ctlr->num_slots; hp_slot++) {
  1051. slot_reg = shpc_readl(ctrl, SLOT_REG(hp_slot));
  1052. dbg("%s: Default Logical Slot Register %d value %x\n", __FUNCTION__,
  1053. hp_slot, slot_reg);
  1054. slot_reg |= (PRSNT_CHANGE_INTR_MASK | ISO_PFAULT_INTR_MASK |
  1055. BUTTON_PRESS_INTR_MASK | MRL_CHANGE_INTR_MASK |
  1056. CON_PFAULT_INTR_MASK | MRL_CHANGE_SERR_MASK |
  1057. CON_PFAULT_SERR_MASK);
  1058. slot_reg &= ~SLOT_REG_RSVDZ_MASK;
  1059. shpc_writel(ctrl, SLOT_REG(hp_slot), slot_reg);
  1060. }
  1061. if (shpchp_poll_mode) {/* Install interrupt polling code */
  1062. /* Install and start the interrupt polling timer */
  1063. init_timer(&php_ctlr->int_poll_timer);
  1064. start_int_poll_timer( php_ctlr, 10 ); /* start with 10 second delay */
  1065. } else {
  1066. /* Installs the interrupt handler */
  1067. rc = pci_enable_msi(pdev);
  1068. if (rc) {
  1069. info("Can't get msi for the hotplug controller\n");
  1070. info("Use INTx for the hotplug controller\n");
  1071. } else
  1072. php_ctlr->irq = pdev->irq;
  1073. rc = request_irq(php_ctlr->irq, shpc_isr, IRQF_SHARED, MY_NAME, (void *) ctrl);
  1074. dbg("%s: request_irq %d for hpc%d (returns %d)\n", __FUNCTION__, php_ctlr->irq, ctlr_seq_num, rc);
  1075. if (rc) {
  1076. err("Can't get irq %d for the hotplug controller\n", php_ctlr->irq);
  1077. goto abort_free_ctlr;
  1078. }
  1079. }
  1080. dbg("%s: HPC at b:d:f:irq=0x%x:%x:%x:%x\n", __FUNCTION__,
  1081. pdev->bus->number, PCI_SLOT(pdev->devfn),
  1082. PCI_FUNC(pdev->devfn), pdev->irq);
  1083. get_hp_hw_control_from_firmware(pdev);
  1084. /* Add this HPC instance into the HPC list */
  1085. spin_lock(&list_lock);
  1086. if (php_ctlr_list_head == 0) {
  1087. php_ctlr_list_head = php_ctlr;
  1088. p = php_ctlr_list_head;
  1089. p->pnext = NULL;
  1090. } else {
  1091. p = php_ctlr_list_head;
  1092. while (p->pnext)
  1093. p = p->pnext;
  1094. p->pnext = php_ctlr;
  1095. }
  1096. spin_unlock(&list_lock);
  1097. ctlr_seq_num++;
  1098. /*
  1099. * If this is the first controller to be initialized,
  1100. * initialize the shpchpd work queue
  1101. */
  1102. if (atomic_add_return(1, &shpchp_num_controllers) == 1) {
  1103. shpchp_wq = create_singlethread_workqueue("shpchpd");
  1104. if (!shpchp_wq)
  1105. return -ENOMEM;
  1106. }
  1107. /*
  1108. * Unmask all event interrupts of all slots
  1109. */
  1110. for (hp_slot = 0; hp_slot < php_ctlr->num_slots; hp_slot++) {
  1111. slot_reg = shpc_readl(ctrl, SLOT_REG(hp_slot));
  1112. dbg("%s: Default Logical Slot Register %d value %x\n", __FUNCTION__,
  1113. hp_slot, slot_reg);
  1114. slot_reg &= ~(PRSNT_CHANGE_INTR_MASK | ISO_PFAULT_INTR_MASK |
  1115. BUTTON_PRESS_INTR_MASK | MRL_CHANGE_INTR_MASK |
  1116. CON_PFAULT_INTR_MASK | SLOT_REG_RSVDZ_MASK);
  1117. shpc_writel(ctrl, SLOT_REG(hp_slot), slot_reg);
  1118. }
  1119. if (!shpchp_poll_mode) {
  1120. /* Unmask all general input interrupts and SERR */
  1121. tempdword = shpc_readl(ctrl, SERR_INTR_ENABLE);
  1122. tempdword &= ~(GLOBAL_INTR_MASK | COMMAND_INTR_MASK |
  1123. SERR_INTR_RSVDZ_MASK);
  1124. shpc_writel(ctrl, SERR_INTR_ENABLE, tempdword);
  1125. tempdword = shpc_readl(ctrl, SERR_INTR_ENABLE);
  1126. dbg("%s: SERR_INTR_ENABLE = %x\n", __FUNCTION__, tempdword);
  1127. }
  1128. DBG_LEAVE_ROUTINE
  1129. return 0;
  1130. /* We end up here for the many possible ways to fail this API. */
  1131. abort_free_ctlr:
  1132. kfree(php_ctlr);
  1133. abort:
  1134. DBG_LEAVE_ROUTINE
  1135. return -1;
  1136. }