shpchp_hpc.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  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. !is_ctrl_busy(ctrl), 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. return retval;
  308. }
  309. static int shpc_write_cmd(struct slot *slot, u8 t_slot, u8 cmd)
  310. {
  311. struct controller *ctrl = slot->ctrl;
  312. u16 cmd_status;
  313. int retval = 0;
  314. u16 temp_word;
  315. DBG_ENTER_ROUTINE
  316. mutex_lock(&slot->ctrl->cmd_lock);
  317. if (!shpc_poll_ctrl_busy(ctrl)) {
  318. /* After 1 sec and and the controller is still busy */
  319. err("%s : Controller is still busy after 1 sec.\n",
  320. __FUNCTION__);
  321. retval = -EBUSY;
  322. goto out;
  323. }
  324. ++t_slot;
  325. temp_word = (t_slot << 8) | (cmd & 0xFF);
  326. dbg("%s: t_slot %x cmd %x\n", __FUNCTION__, t_slot, cmd);
  327. /* To make sure the Controller Busy bit is 0 before we send out the
  328. * command.
  329. */
  330. shpc_writew(ctrl, CMD, temp_word);
  331. /*
  332. * Wait for command completion.
  333. */
  334. retval = shpc_wait_cmd(slot->ctrl);
  335. if (retval)
  336. goto out;
  337. cmd_status = hpc_check_cmd_status(slot->ctrl);
  338. if (cmd_status) {
  339. err("%s: Failed to issued command 0x%x (error code = %d)\n",
  340. __FUNCTION__, cmd, cmd_status);
  341. retval = -EIO;
  342. }
  343. out:
  344. mutex_unlock(&slot->ctrl->cmd_lock);
  345. DBG_LEAVE_ROUTINE
  346. return retval;
  347. }
  348. static int hpc_check_cmd_status(struct controller *ctrl)
  349. {
  350. u16 cmd_status;
  351. int retval = 0;
  352. DBG_ENTER_ROUTINE
  353. cmd_status = shpc_readw(ctrl, CMD_STATUS) & 0x000F;
  354. switch (cmd_status >> 1) {
  355. case 0:
  356. retval = 0;
  357. break;
  358. case 1:
  359. retval = SWITCH_OPEN;
  360. err("%s: Switch opened!\n", __FUNCTION__);
  361. break;
  362. case 2:
  363. retval = INVALID_CMD;
  364. err("%s: Invalid HPC command!\n", __FUNCTION__);
  365. break;
  366. case 4:
  367. retval = INVALID_SPEED_MODE;
  368. err("%s: Invalid bus speed/mode!\n", __FUNCTION__);
  369. break;
  370. default:
  371. retval = cmd_status;
  372. }
  373. DBG_LEAVE_ROUTINE
  374. return retval;
  375. }
  376. static int hpc_get_attention_status(struct slot *slot, u8 *status)
  377. {
  378. struct controller *ctrl = slot->ctrl;
  379. u32 slot_reg;
  380. u8 state;
  381. DBG_ENTER_ROUTINE
  382. slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
  383. state = (slot_reg & ATN_LED_STATE_MASK) >> ATN_LED_STATE_SHIFT;
  384. switch (state) {
  385. case ATN_LED_STATE_ON:
  386. *status = 1; /* On */
  387. break;
  388. case ATN_LED_STATE_BLINK:
  389. *status = 2; /* Blink */
  390. break;
  391. case ATN_LED_STATE_OFF:
  392. *status = 0; /* Off */
  393. break;
  394. default:
  395. *status = 0xFF; /* Reserved */
  396. break;
  397. }
  398. DBG_LEAVE_ROUTINE
  399. return 0;
  400. }
  401. static int hpc_get_power_status(struct slot * slot, u8 *status)
  402. {
  403. struct controller *ctrl = slot->ctrl;
  404. u32 slot_reg;
  405. u8 state;
  406. DBG_ENTER_ROUTINE
  407. slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
  408. state = (slot_reg & SLOT_STATE_MASK) >> SLOT_STATE_SHIFT;
  409. switch (state) {
  410. case SLOT_STATE_PWRONLY:
  411. *status = 2; /* Powered only */
  412. break;
  413. case SLOT_STATE_ENABLED:
  414. *status = 1; /* Enabled */
  415. break;
  416. case SLOT_STATE_DISABLED:
  417. *status = 0; /* Disabled */
  418. break;
  419. default:
  420. *status = 0xFF; /* Reserved */
  421. break;
  422. }
  423. DBG_LEAVE_ROUTINE
  424. return 0;
  425. }
  426. static int hpc_get_latch_status(struct slot *slot, u8 *status)
  427. {
  428. struct controller *ctrl = slot->ctrl;
  429. u32 slot_reg;
  430. DBG_ENTER_ROUTINE
  431. slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
  432. *status = !!(slot_reg & MRL_SENSOR); /* 0 -> close; 1 -> open */
  433. DBG_LEAVE_ROUTINE
  434. return 0;
  435. }
  436. static int hpc_get_adapter_status(struct slot *slot, u8 *status)
  437. {
  438. struct controller *ctrl = slot->ctrl;
  439. u32 slot_reg;
  440. u8 state;
  441. DBG_ENTER_ROUTINE
  442. slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
  443. state = (slot_reg & PRSNT_MASK) >> PRSNT_SHIFT;
  444. *status = (state != 0x3) ? 1 : 0;
  445. DBG_LEAVE_ROUTINE
  446. return 0;
  447. }
  448. static int hpc_get_prog_int(struct slot *slot, u8 *prog_int)
  449. {
  450. struct controller *ctrl = slot->ctrl;
  451. DBG_ENTER_ROUTINE
  452. *prog_int = shpc_readb(ctrl, PROG_INTERFACE);
  453. DBG_LEAVE_ROUTINE
  454. return 0;
  455. }
  456. static int hpc_get_adapter_speed(struct slot *slot, enum pci_bus_speed *value)
  457. {
  458. int retval = 0;
  459. struct controller *ctrl = slot->ctrl;
  460. u32 slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
  461. u8 m66_cap = !!(slot_reg & MHZ66_CAP);
  462. u8 pi, pcix_cap;
  463. DBG_ENTER_ROUTINE
  464. if ((retval = hpc_get_prog_int(slot, &pi)))
  465. return retval;
  466. switch (pi) {
  467. case 1:
  468. pcix_cap = (slot_reg & PCIX_CAP_MASK_PI1) >> PCIX_CAP_SHIFT;
  469. break;
  470. case 2:
  471. pcix_cap = (slot_reg & PCIX_CAP_MASK_PI2) >> PCIX_CAP_SHIFT;
  472. break;
  473. default:
  474. return -ENODEV;
  475. }
  476. dbg("%s: slot_reg = %x, pcix_cap = %x, m66_cap = %x\n",
  477. __FUNCTION__, slot_reg, pcix_cap, m66_cap);
  478. switch (pcix_cap) {
  479. case 0x0:
  480. *value = m66_cap ? PCI_SPEED_66MHz : PCI_SPEED_33MHz;
  481. break;
  482. case 0x1:
  483. *value = PCI_SPEED_66MHz_PCIX;
  484. break;
  485. case 0x3:
  486. *value = PCI_SPEED_133MHz_PCIX;
  487. break;
  488. case 0x4:
  489. *value = PCI_SPEED_133MHz_PCIX_266;
  490. break;
  491. case 0x5:
  492. *value = PCI_SPEED_133MHz_PCIX_533;
  493. break;
  494. case 0x2:
  495. default:
  496. *value = PCI_SPEED_UNKNOWN;
  497. retval = -ENODEV;
  498. break;
  499. }
  500. dbg("Adapter speed = %d\n", *value);
  501. DBG_LEAVE_ROUTINE
  502. return retval;
  503. }
  504. static int hpc_get_mode1_ECC_cap(struct slot *slot, u8 *mode)
  505. {
  506. struct controller *ctrl = slot->ctrl;
  507. u16 sec_bus_status;
  508. u8 pi;
  509. int retval = 0;
  510. DBG_ENTER_ROUTINE
  511. pi = shpc_readb(ctrl, PROG_INTERFACE);
  512. sec_bus_status = shpc_readw(ctrl, SEC_BUS_CONFIG);
  513. if (pi == 2) {
  514. *mode = (sec_bus_status & 0x0100) >> 8;
  515. } else {
  516. retval = -1;
  517. }
  518. dbg("Mode 1 ECC cap = %d\n", *mode);
  519. DBG_LEAVE_ROUTINE
  520. return retval;
  521. }
  522. static int hpc_query_power_fault(struct slot * slot)
  523. {
  524. struct controller *ctrl = slot->ctrl;
  525. u32 slot_reg;
  526. DBG_ENTER_ROUTINE
  527. slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
  528. DBG_LEAVE_ROUTINE
  529. /* Note: Logic 0 => fault */
  530. return !(slot_reg & POWER_FAULT);
  531. }
  532. static int hpc_set_attention_status(struct slot *slot, u8 value)
  533. {
  534. u8 slot_cmd = 0;
  535. switch (value) {
  536. case 0 :
  537. slot_cmd = SET_ATTN_OFF; /* OFF */
  538. break;
  539. case 1:
  540. slot_cmd = SET_ATTN_ON; /* ON */
  541. break;
  542. case 2:
  543. slot_cmd = SET_ATTN_BLINK; /* BLINK */
  544. break;
  545. default:
  546. return -1;
  547. }
  548. return shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
  549. }
  550. static void hpc_set_green_led_on(struct slot *slot)
  551. {
  552. shpc_write_cmd(slot, slot->hp_slot, SET_PWR_ON);
  553. }
  554. static void hpc_set_green_led_off(struct slot *slot)
  555. {
  556. shpc_write_cmd(slot, slot->hp_slot, SET_PWR_OFF);
  557. }
  558. static void hpc_set_green_led_blink(struct slot *slot)
  559. {
  560. shpc_write_cmd(slot, slot->hp_slot, SET_PWR_BLINK);
  561. }
  562. int shpc_get_ctlr_slot_config(struct controller *ctrl,
  563. int *num_ctlr_slots, /* number of slots in this HPC */
  564. int *first_device_num, /* PCI dev num of the first slot in this SHPC */
  565. int *physical_slot_num, /* phy slot num of the first slot in this SHPC */
  566. int *updown, /* physical_slot_num increament: 1 or -1 */
  567. int *flags)
  568. {
  569. u32 slot_config;
  570. DBG_ENTER_ROUTINE
  571. slot_config = shpc_readl(ctrl, SLOT_CONFIG);
  572. *first_device_num = (slot_config & FIRST_DEV_NUM) >> 8;
  573. *num_ctlr_slots = slot_config & SLOT_NUM;
  574. *physical_slot_num = (slot_config & PSN) >> 16;
  575. *updown = ((slot_config & UPDOWN) >> 29) ? 1 : -1;
  576. dbg("%s: physical_slot_num = %x\n", __FUNCTION__, *physical_slot_num);
  577. DBG_LEAVE_ROUTINE
  578. return 0;
  579. }
  580. static void hpc_release_ctlr(struct controller *ctrl)
  581. {
  582. struct php_ctlr_state_s *php_ctlr = ctrl->hpc_ctlr_handle;
  583. struct php_ctlr_state_s *p, *p_prev;
  584. int i;
  585. u32 slot_reg, serr_int;
  586. DBG_ENTER_ROUTINE
  587. /*
  588. * Mask event interrupts and SERRs of all slots
  589. */
  590. for (i = 0; i < ctrl->num_slots; i++) {
  591. slot_reg = shpc_readl(ctrl, SLOT_REG(i));
  592. slot_reg |= (PRSNT_CHANGE_INTR_MASK | ISO_PFAULT_INTR_MASK |
  593. BUTTON_PRESS_INTR_MASK | MRL_CHANGE_INTR_MASK |
  594. CON_PFAULT_INTR_MASK | MRL_CHANGE_SERR_MASK |
  595. CON_PFAULT_SERR_MASK);
  596. slot_reg &= ~SLOT_REG_RSVDZ_MASK;
  597. shpc_writel(ctrl, SLOT_REG(i), slot_reg);
  598. }
  599. cleanup_slots(ctrl);
  600. /*
  601. * Mask SERR and System Interrut generation
  602. */
  603. serr_int = shpc_readl(ctrl, SERR_INTR_ENABLE);
  604. serr_int |= (GLOBAL_INTR_MASK | GLOBAL_SERR_MASK |
  605. COMMAND_INTR_MASK | ARBITER_SERR_MASK);
  606. serr_int &= ~SERR_INTR_RSVDZ_MASK;
  607. shpc_writel(ctrl, SERR_INTR_ENABLE, serr_int);
  608. if (shpchp_poll_mode) {
  609. del_timer(&php_ctlr->int_poll_timer);
  610. } else {
  611. if (php_ctlr->irq) {
  612. free_irq(php_ctlr->irq, ctrl);
  613. php_ctlr->irq = 0;
  614. pci_disable_msi(php_ctlr->pci_dev);
  615. }
  616. }
  617. if (php_ctlr->pci_dev) {
  618. iounmap(php_ctlr->creg);
  619. release_mem_region(ctrl->mmio_base, ctrl->mmio_size);
  620. php_ctlr->pci_dev = NULL;
  621. }
  622. spin_lock(&list_lock);
  623. p = php_ctlr_list_head;
  624. p_prev = NULL;
  625. while (p) {
  626. if (p == php_ctlr) {
  627. if (p_prev)
  628. p_prev->pnext = p->pnext;
  629. else
  630. php_ctlr_list_head = p->pnext;
  631. break;
  632. } else {
  633. p_prev = p;
  634. p = p->pnext;
  635. }
  636. }
  637. spin_unlock(&list_lock);
  638. kfree(php_ctlr);
  639. /*
  640. * If this is the last controller to be released, destroy the
  641. * shpchpd work queue
  642. */
  643. if (atomic_dec_and_test(&shpchp_num_controllers))
  644. destroy_workqueue(shpchp_wq);
  645. DBG_LEAVE_ROUTINE
  646. }
  647. static int hpc_power_on_slot(struct slot * slot)
  648. {
  649. int retval;
  650. DBG_ENTER_ROUTINE
  651. retval = shpc_write_cmd(slot, slot->hp_slot, SET_SLOT_PWR);
  652. if (retval) {
  653. err("%s: Write command failed!\n", __FUNCTION__);
  654. return retval;
  655. }
  656. DBG_LEAVE_ROUTINE
  657. return 0;
  658. }
  659. static int hpc_slot_enable(struct slot * slot)
  660. {
  661. int retval;
  662. DBG_ENTER_ROUTINE
  663. /* Slot - Enable, Power Indicator - Blink, Attention Indicator - Off */
  664. retval = shpc_write_cmd(slot, slot->hp_slot,
  665. SET_SLOT_ENABLE | SET_PWR_BLINK | SET_ATTN_OFF);
  666. if (retval) {
  667. err("%s: Write command failed!\n", __FUNCTION__);
  668. return retval;
  669. }
  670. DBG_LEAVE_ROUTINE
  671. return 0;
  672. }
  673. static int hpc_slot_disable(struct slot * slot)
  674. {
  675. int retval;
  676. DBG_ENTER_ROUTINE
  677. /* Slot - Disable, Power Indicator - Off, Attention Indicator - On */
  678. retval = shpc_write_cmd(slot, slot->hp_slot,
  679. SET_SLOT_DISABLE | SET_PWR_OFF | SET_ATTN_ON);
  680. if (retval) {
  681. err("%s: Write command failed!\n", __FUNCTION__);
  682. return retval;
  683. }
  684. DBG_LEAVE_ROUTINE
  685. return 0;
  686. }
  687. static int hpc_set_bus_speed_mode(struct slot * slot, enum pci_bus_speed value)
  688. {
  689. int retval;
  690. struct controller *ctrl = slot->ctrl;
  691. u8 pi, cmd;
  692. DBG_ENTER_ROUTINE
  693. pi = shpc_readb(ctrl, PROG_INTERFACE);
  694. if ((pi == 1) && (value > PCI_SPEED_133MHz_PCIX))
  695. return -EINVAL;
  696. switch (value) {
  697. case PCI_SPEED_33MHz:
  698. cmd = SETA_PCI_33MHZ;
  699. break;
  700. case PCI_SPEED_66MHz:
  701. cmd = SETA_PCI_66MHZ;
  702. break;
  703. case PCI_SPEED_66MHz_PCIX:
  704. cmd = SETA_PCIX_66MHZ;
  705. break;
  706. case PCI_SPEED_100MHz_PCIX:
  707. cmd = SETA_PCIX_100MHZ;
  708. break;
  709. case PCI_SPEED_133MHz_PCIX:
  710. cmd = SETA_PCIX_133MHZ;
  711. break;
  712. case PCI_SPEED_66MHz_PCIX_ECC:
  713. cmd = SETB_PCIX_66MHZ_EM;
  714. break;
  715. case PCI_SPEED_100MHz_PCIX_ECC:
  716. cmd = SETB_PCIX_100MHZ_EM;
  717. break;
  718. case PCI_SPEED_133MHz_PCIX_ECC:
  719. cmd = SETB_PCIX_133MHZ_EM;
  720. break;
  721. case PCI_SPEED_66MHz_PCIX_266:
  722. cmd = SETB_PCIX_66MHZ_266;
  723. break;
  724. case PCI_SPEED_100MHz_PCIX_266:
  725. cmd = SETB_PCIX_100MHZ_266;
  726. break;
  727. case PCI_SPEED_133MHz_PCIX_266:
  728. cmd = SETB_PCIX_133MHZ_266;
  729. break;
  730. case PCI_SPEED_66MHz_PCIX_533:
  731. cmd = SETB_PCIX_66MHZ_533;
  732. break;
  733. case PCI_SPEED_100MHz_PCIX_533:
  734. cmd = SETB_PCIX_100MHZ_533;
  735. break;
  736. case PCI_SPEED_133MHz_PCIX_533:
  737. cmd = SETB_PCIX_133MHZ_533;
  738. break;
  739. default:
  740. return -EINVAL;
  741. }
  742. retval = shpc_write_cmd(slot, 0, cmd);
  743. if (retval)
  744. err("%s: Write command failed!\n", __FUNCTION__);
  745. DBG_LEAVE_ROUTINE
  746. return retval;
  747. }
  748. static irqreturn_t shpc_isr(int irq, void *dev_id)
  749. {
  750. struct controller *ctrl = (struct controller *)dev_id;
  751. struct php_ctlr_state_s *php_ctlr = ctrl->hpc_ctlr_handle;
  752. u32 serr_int, slot_reg, intr_loc, intr_loc2;
  753. int hp_slot;
  754. /* Check to see if it was our interrupt */
  755. intr_loc = shpc_readl(ctrl, INTR_LOC);
  756. if (!intr_loc)
  757. return IRQ_NONE;
  758. dbg("%s: intr_loc = %x\n",__FUNCTION__, intr_loc);
  759. if(!shpchp_poll_mode) {
  760. /*
  761. * Mask Global Interrupt Mask - see implementation
  762. * note on p. 139 of SHPC spec rev 1.0
  763. */
  764. serr_int = shpc_readl(ctrl, SERR_INTR_ENABLE);
  765. serr_int |= GLOBAL_INTR_MASK;
  766. serr_int &= ~SERR_INTR_RSVDZ_MASK;
  767. shpc_writel(ctrl, SERR_INTR_ENABLE, serr_int);
  768. intr_loc2 = shpc_readl(ctrl, INTR_LOC);
  769. dbg("%s: intr_loc2 = %x\n",__FUNCTION__, intr_loc2);
  770. }
  771. if (intr_loc & CMD_INTR_PENDING) {
  772. /*
  773. * Command Complete Interrupt Pending
  774. * RO only - clear by writing 1 to the Command Completion
  775. * Detect bit in Controller SERR-INT register
  776. */
  777. serr_int = shpc_readl(ctrl, SERR_INTR_ENABLE);
  778. serr_int &= ~SERR_INTR_RSVDZ_MASK;
  779. shpc_writel(ctrl, SERR_INTR_ENABLE, serr_int);
  780. wake_up_interruptible(&ctrl->queue);
  781. }
  782. if (!(intr_loc & ~CMD_INTR_PENDING))
  783. goto out;
  784. for (hp_slot = 0; hp_slot < ctrl->num_slots; hp_slot++) {
  785. /* To find out which slot has interrupt pending */
  786. if (!(intr_loc & SLOT_INTR_PENDING(hp_slot)))
  787. continue;
  788. slot_reg = shpc_readl(ctrl, SLOT_REG(hp_slot));
  789. dbg("%s: Slot %x with intr, slot register = %x\n",
  790. __FUNCTION__, hp_slot, slot_reg);
  791. if (slot_reg & MRL_CHANGE_DETECTED)
  792. php_ctlr->switch_change_callback(
  793. hp_slot, php_ctlr->callback_instance_id);
  794. if (slot_reg & BUTTON_PRESS_DETECTED)
  795. php_ctlr->attention_button_callback(
  796. hp_slot, php_ctlr->callback_instance_id);
  797. if (slot_reg & PRSNT_CHANGE_DETECTED)
  798. php_ctlr->presence_change_callback(
  799. hp_slot , php_ctlr->callback_instance_id);
  800. if (slot_reg & (ISO_PFAULT_DETECTED | CON_PFAULT_DETECTED))
  801. php_ctlr->power_fault_callback(
  802. hp_slot, php_ctlr->callback_instance_id);
  803. /* Clear all slot events */
  804. slot_reg &= ~SLOT_REG_RSVDZ_MASK;
  805. shpc_writel(ctrl, SLOT_REG(hp_slot), slot_reg);
  806. }
  807. out:
  808. if (!shpchp_poll_mode) {
  809. /* Unmask Global Interrupt Mask */
  810. serr_int = shpc_readl(ctrl, SERR_INTR_ENABLE);
  811. serr_int &= ~(GLOBAL_INTR_MASK | SERR_INTR_RSVDZ_MASK);
  812. shpc_writel(ctrl, SERR_INTR_ENABLE, serr_int);
  813. }
  814. return IRQ_HANDLED;
  815. }
  816. static int hpc_get_max_bus_speed (struct slot *slot, enum pci_bus_speed *value)
  817. {
  818. int retval = 0;
  819. struct controller *ctrl = slot->ctrl;
  820. enum pci_bus_speed bus_speed = PCI_SPEED_UNKNOWN;
  821. u8 pi = shpc_readb(ctrl, PROG_INTERFACE);
  822. u32 slot_avail1 = shpc_readl(ctrl, SLOT_AVAIL1);
  823. u32 slot_avail2 = shpc_readl(ctrl, SLOT_AVAIL2);
  824. DBG_ENTER_ROUTINE
  825. if (pi == 2) {
  826. if (slot_avail2 & SLOT_133MHZ_PCIX_533)
  827. bus_speed = PCI_SPEED_133MHz_PCIX_533;
  828. else if (slot_avail2 & SLOT_100MHZ_PCIX_533)
  829. bus_speed = PCI_SPEED_100MHz_PCIX_533;
  830. else if (slot_avail2 & SLOT_66MHZ_PCIX_533)
  831. bus_speed = PCI_SPEED_66MHz_PCIX_533;
  832. else if (slot_avail2 & SLOT_133MHZ_PCIX_266)
  833. bus_speed = PCI_SPEED_133MHz_PCIX_266;
  834. else if (slot_avail2 & SLOT_100MHZ_PCIX_266)
  835. bus_speed = PCI_SPEED_100MHz_PCIX_266;
  836. else if (slot_avail2 & SLOT_66MHZ_PCIX_266)
  837. bus_speed = PCI_SPEED_66MHz_PCIX_266;
  838. }
  839. if (bus_speed == PCI_SPEED_UNKNOWN) {
  840. if (slot_avail1 & SLOT_133MHZ_PCIX)
  841. bus_speed = PCI_SPEED_133MHz_PCIX;
  842. else if (slot_avail1 & SLOT_100MHZ_PCIX)
  843. bus_speed = PCI_SPEED_100MHz_PCIX;
  844. else if (slot_avail1 & SLOT_66MHZ_PCIX)
  845. bus_speed = PCI_SPEED_66MHz_PCIX;
  846. else if (slot_avail2 & SLOT_66MHZ)
  847. bus_speed = PCI_SPEED_66MHz;
  848. else if (slot_avail1 & SLOT_33MHZ)
  849. bus_speed = PCI_SPEED_33MHz;
  850. else
  851. retval = -ENODEV;
  852. }
  853. *value = bus_speed;
  854. dbg("Max bus speed = %d\n", bus_speed);
  855. DBG_LEAVE_ROUTINE
  856. return retval;
  857. }
  858. static int hpc_get_cur_bus_speed (struct slot *slot, enum pci_bus_speed *value)
  859. {
  860. int retval = 0;
  861. struct controller *ctrl = slot->ctrl;
  862. enum pci_bus_speed bus_speed = PCI_SPEED_UNKNOWN;
  863. u16 sec_bus_reg = shpc_readw(ctrl, SEC_BUS_CONFIG);
  864. u8 pi = shpc_readb(ctrl, PROG_INTERFACE);
  865. u8 speed_mode = (pi == 2) ? (sec_bus_reg & 0xF) : (sec_bus_reg & 0x7);
  866. DBG_ENTER_ROUTINE
  867. if ((pi == 1) && (speed_mode > 4)) {
  868. *value = PCI_SPEED_UNKNOWN;
  869. return -ENODEV;
  870. }
  871. switch (speed_mode) {
  872. case 0x0:
  873. *value = PCI_SPEED_33MHz;
  874. break;
  875. case 0x1:
  876. *value = PCI_SPEED_66MHz;
  877. break;
  878. case 0x2:
  879. *value = PCI_SPEED_66MHz_PCIX;
  880. break;
  881. case 0x3:
  882. *value = PCI_SPEED_100MHz_PCIX;
  883. break;
  884. case 0x4:
  885. *value = PCI_SPEED_133MHz_PCIX;
  886. break;
  887. case 0x5:
  888. *value = PCI_SPEED_66MHz_PCIX_ECC;
  889. break;
  890. case 0x6:
  891. *value = PCI_SPEED_100MHz_PCIX_ECC;
  892. break;
  893. case 0x7:
  894. *value = PCI_SPEED_133MHz_PCIX_ECC;
  895. break;
  896. case 0x8:
  897. *value = PCI_SPEED_66MHz_PCIX_266;
  898. break;
  899. case 0x9:
  900. *value = PCI_SPEED_100MHz_PCIX_266;
  901. break;
  902. case 0xa:
  903. *value = PCI_SPEED_133MHz_PCIX_266;
  904. break;
  905. case 0xb:
  906. *value = PCI_SPEED_66MHz_PCIX_533;
  907. break;
  908. case 0xc:
  909. *value = PCI_SPEED_100MHz_PCIX_533;
  910. break;
  911. case 0xd:
  912. *value = PCI_SPEED_133MHz_PCIX_533;
  913. break;
  914. default:
  915. *value = PCI_SPEED_UNKNOWN;
  916. retval = -ENODEV;
  917. break;
  918. }
  919. dbg("Current bus speed = %d\n", bus_speed);
  920. DBG_LEAVE_ROUTINE
  921. return retval;
  922. }
  923. static struct hpc_ops shpchp_hpc_ops = {
  924. .power_on_slot = hpc_power_on_slot,
  925. .slot_enable = hpc_slot_enable,
  926. .slot_disable = hpc_slot_disable,
  927. .set_bus_speed_mode = hpc_set_bus_speed_mode,
  928. .set_attention_status = hpc_set_attention_status,
  929. .get_power_status = hpc_get_power_status,
  930. .get_attention_status = hpc_get_attention_status,
  931. .get_latch_status = hpc_get_latch_status,
  932. .get_adapter_status = hpc_get_adapter_status,
  933. .get_max_bus_speed = hpc_get_max_bus_speed,
  934. .get_cur_bus_speed = hpc_get_cur_bus_speed,
  935. .get_adapter_speed = hpc_get_adapter_speed,
  936. .get_mode1_ECC_cap = hpc_get_mode1_ECC_cap,
  937. .get_prog_int = hpc_get_prog_int,
  938. .query_power_fault = hpc_query_power_fault,
  939. .green_led_on = hpc_set_green_led_on,
  940. .green_led_off = hpc_set_green_led_off,
  941. .green_led_blink = hpc_set_green_led_blink,
  942. .release_ctlr = hpc_release_ctlr,
  943. };
  944. int shpc_init(struct controller * ctrl, struct pci_dev * pdev)
  945. {
  946. struct php_ctlr_state_s *php_ctlr, *p;
  947. void *instance_id = ctrl;
  948. int rc = -1, num_slots = 0;
  949. u8 hp_slot;
  950. u32 shpc_base_offset;
  951. u32 tempdword, slot_reg, slot_config;
  952. u8 i;
  953. DBG_ENTER_ROUTINE
  954. ctrl->pci_dev = pdev; /* pci_dev of the P2P bridge */
  955. spin_lock_init(&list_lock);
  956. php_ctlr = kzalloc(sizeof(*php_ctlr), GFP_KERNEL);
  957. if (!php_ctlr) { /* allocate controller state data */
  958. err("%s: HPC controller memory allocation error!\n", __FUNCTION__);
  959. goto abort;
  960. }
  961. php_ctlr->pci_dev = pdev; /* save pci_dev in context */
  962. if ((pdev->vendor == PCI_VENDOR_ID_AMD) || (pdev->device ==
  963. PCI_DEVICE_ID_AMD_GOLAM_7450)) {
  964. /* amd shpc driver doesn't use Base Offset; assume 0 */
  965. ctrl->mmio_base = pci_resource_start(pdev, 0);
  966. ctrl->mmio_size = pci_resource_len(pdev, 0);
  967. } else {
  968. ctrl->cap_offset = pci_find_capability(pdev, PCI_CAP_ID_SHPC);
  969. if (!ctrl->cap_offset) {
  970. err("%s : cap_offset == 0\n", __FUNCTION__);
  971. goto abort_free_ctlr;
  972. }
  973. dbg("%s: cap_offset = %x\n", __FUNCTION__, ctrl->cap_offset);
  974. rc = shpc_indirect_read(ctrl, 0, &shpc_base_offset);
  975. if (rc) {
  976. err("%s: cannot read base_offset\n", __FUNCTION__);
  977. goto abort_free_ctlr;
  978. }
  979. rc = shpc_indirect_read(ctrl, 3, &tempdword);
  980. if (rc) {
  981. err("%s: cannot read slot config\n", __FUNCTION__);
  982. goto abort_free_ctlr;
  983. }
  984. num_slots = tempdword & SLOT_NUM;
  985. dbg("%s: num_slots (indirect) %x\n", __FUNCTION__, num_slots);
  986. for (i = 0; i < 9 + num_slots; i++) {
  987. rc = shpc_indirect_read(ctrl, i, &tempdword);
  988. if (rc) {
  989. err("%s: cannot read creg (index = %d)\n",
  990. __FUNCTION__, i);
  991. goto abort_free_ctlr;
  992. }
  993. dbg("%s: offset %d: value %x\n", __FUNCTION__,i,
  994. tempdword);
  995. }
  996. ctrl->mmio_base =
  997. pci_resource_start(pdev, 0) + shpc_base_offset;
  998. ctrl->mmio_size = 0x24 + 0x4 * num_slots;
  999. }
  1000. info("HPC vendor_id %x device_id %x ss_vid %x ss_did %x\n", pdev->vendor, pdev->device, pdev->subsystem_vendor,
  1001. pdev->subsystem_device);
  1002. rc = pci_enable_device(pdev);
  1003. if (rc) {
  1004. err("%s: pci_enable_device failed\n", __FUNCTION__);
  1005. goto abort_free_ctlr;
  1006. }
  1007. if (!request_mem_region(ctrl->mmio_base, ctrl->mmio_size, MY_NAME)) {
  1008. err("%s: cannot reserve MMIO region\n", __FUNCTION__);
  1009. rc = -1;
  1010. goto abort_free_ctlr;
  1011. }
  1012. php_ctlr->creg = ioremap(ctrl->mmio_base, ctrl->mmio_size);
  1013. if (!php_ctlr->creg) {
  1014. err("%s: cannot remap MMIO region %lx @ %lx\n", __FUNCTION__,
  1015. ctrl->mmio_size, ctrl->mmio_base);
  1016. release_mem_region(ctrl->mmio_base, ctrl->mmio_size);
  1017. rc = -1;
  1018. goto abort_free_ctlr;
  1019. }
  1020. dbg("%s: php_ctlr->creg %p\n", __FUNCTION__, php_ctlr->creg);
  1021. mutex_init(&ctrl->crit_sect);
  1022. mutex_init(&ctrl->cmd_lock);
  1023. /* Setup wait queue */
  1024. init_waitqueue_head(&ctrl->queue);
  1025. /* Find the IRQ */
  1026. php_ctlr->irq = pdev->irq;
  1027. php_ctlr->attention_button_callback = shpchp_handle_attention_button,
  1028. php_ctlr->switch_change_callback = shpchp_handle_switch_change;
  1029. php_ctlr->presence_change_callback = shpchp_handle_presence_change;
  1030. php_ctlr->power_fault_callback = shpchp_handle_power_fault;
  1031. php_ctlr->callback_instance_id = instance_id;
  1032. ctrl->hpc_ctlr_handle = php_ctlr;
  1033. ctrl->hpc_ops = &shpchp_hpc_ops;
  1034. /* Return PCI Controller Info */
  1035. slot_config = shpc_readl(ctrl, SLOT_CONFIG);
  1036. php_ctlr->slot_device_offset = (slot_config & FIRST_DEV_NUM) >> 8;
  1037. php_ctlr->num_slots = slot_config & SLOT_NUM;
  1038. dbg("%s: slot_device_offset %x\n", __FUNCTION__, php_ctlr->slot_device_offset);
  1039. dbg("%s: num_slots %x\n", __FUNCTION__, php_ctlr->num_slots);
  1040. /* Mask Global Interrupt Mask & Command Complete Interrupt Mask */
  1041. tempdword = shpc_readl(ctrl, SERR_INTR_ENABLE);
  1042. dbg("%s: SERR_INTR_ENABLE = %x\n", __FUNCTION__, tempdword);
  1043. tempdword |= (GLOBAL_INTR_MASK | GLOBAL_SERR_MASK |
  1044. COMMAND_INTR_MASK | ARBITER_SERR_MASK);
  1045. tempdword &= ~SERR_INTR_RSVDZ_MASK;
  1046. shpc_writel(ctrl, SERR_INTR_ENABLE, tempdword);
  1047. tempdword = shpc_readl(ctrl, SERR_INTR_ENABLE);
  1048. dbg("%s: SERR_INTR_ENABLE = %x\n", __FUNCTION__, tempdword);
  1049. /* Mask the MRL sensor SERR Mask of individual slot in
  1050. * Slot SERR-INT Mask & clear all the existing event if any
  1051. */
  1052. for (hp_slot = 0; hp_slot < php_ctlr->num_slots; hp_slot++) {
  1053. slot_reg = shpc_readl(ctrl, SLOT_REG(hp_slot));
  1054. dbg("%s: Default Logical Slot Register %d value %x\n", __FUNCTION__,
  1055. hp_slot, slot_reg);
  1056. slot_reg |= (PRSNT_CHANGE_INTR_MASK | ISO_PFAULT_INTR_MASK |
  1057. BUTTON_PRESS_INTR_MASK | MRL_CHANGE_INTR_MASK |
  1058. CON_PFAULT_INTR_MASK | MRL_CHANGE_SERR_MASK |
  1059. CON_PFAULT_SERR_MASK);
  1060. slot_reg &= ~SLOT_REG_RSVDZ_MASK;
  1061. shpc_writel(ctrl, SLOT_REG(hp_slot), slot_reg);
  1062. }
  1063. if (shpchp_poll_mode) {/* Install interrupt polling code */
  1064. /* Install and start the interrupt polling timer */
  1065. init_timer(&php_ctlr->int_poll_timer);
  1066. start_int_poll_timer( php_ctlr, 10 ); /* start with 10 second delay */
  1067. } else {
  1068. /* Installs the interrupt handler */
  1069. rc = pci_enable_msi(pdev);
  1070. if (rc) {
  1071. info("Can't get msi for the hotplug controller\n");
  1072. info("Use INTx for the hotplug controller\n");
  1073. } else
  1074. php_ctlr->irq = pdev->irq;
  1075. rc = request_irq(php_ctlr->irq, shpc_isr, IRQF_SHARED, MY_NAME, (void *) ctrl);
  1076. dbg("%s: request_irq %d for hpc%d (returns %d)\n", __FUNCTION__, php_ctlr->irq, ctlr_seq_num, rc);
  1077. if (rc) {
  1078. err("Can't get irq %d for the hotplug controller\n", php_ctlr->irq);
  1079. goto abort_free_ctlr;
  1080. }
  1081. }
  1082. dbg("%s: HPC at b:d:f:irq=0x%x:%x:%x:%x\n", __FUNCTION__,
  1083. pdev->bus->number, PCI_SLOT(pdev->devfn),
  1084. PCI_FUNC(pdev->devfn), pdev->irq);
  1085. get_hp_hw_control_from_firmware(pdev);
  1086. /* Add this HPC instance into the HPC list */
  1087. spin_lock(&list_lock);
  1088. if (php_ctlr_list_head == 0) {
  1089. php_ctlr_list_head = php_ctlr;
  1090. p = php_ctlr_list_head;
  1091. p->pnext = NULL;
  1092. } else {
  1093. p = php_ctlr_list_head;
  1094. while (p->pnext)
  1095. p = p->pnext;
  1096. p->pnext = php_ctlr;
  1097. }
  1098. spin_unlock(&list_lock);
  1099. ctlr_seq_num++;
  1100. /*
  1101. * If this is the first controller to be initialized,
  1102. * initialize the shpchpd work queue
  1103. */
  1104. if (atomic_add_return(1, &shpchp_num_controllers) == 1) {
  1105. shpchp_wq = create_singlethread_workqueue("shpchpd");
  1106. if (!shpchp_wq) {
  1107. rc = -ENOMEM;
  1108. goto abort_free_ctlr;
  1109. }
  1110. }
  1111. /*
  1112. * Unmask all event interrupts of all slots
  1113. */
  1114. for (hp_slot = 0; hp_slot < php_ctlr->num_slots; hp_slot++) {
  1115. slot_reg = shpc_readl(ctrl, SLOT_REG(hp_slot));
  1116. dbg("%s: Default Logical Slot Register %d value %x\n", __FUNCTION__,
  1117. hp_slot, slot_reg);
  1118. slot_reg &= ~(PRSNT_CHANGE_INTR_MASK | ISO_PFAULT_INTR_MASK |
  1119. BUTTON_PRESS_INTR_MASK | MRL_CHANGE_INTR_MASK |
  1120. CON_PFAULT_INTR_MASK | SLOT_REG_RSVDZ_MASK);
  1121. shpc_writel(ctrl, SLOT_REG(hp_slot), slot_reg);
  1122. }
  1123. if (!shpchp_poll_mode) {
  1124. /* Unmask all general input interrupts and SERR */
  1125. tempdword = shpc_readl(ctrl, SERR_INTR_ENABLE);
  1126. tempdword &= ~(GLOBAL_INTR_MASK | COMMAND_INTR_MASK |
  1127. SERR_INTR_RSVDZ_MASK);
  1128. shpc_writel(ctrl, SERR_INTR_ENABLE, tempdword);
  1129. tempdword = shpc_readl(ctrl, SERR_INTR_ENABLE);
  1130. dbg("%s: SERR_INTR_ENABLE = %x\n", __FUNCTION__, tempdword);
  1131. }
  1132. DBG_LEAVE_ROUTINE
  1133. return 0;
  1134. /* We end up here for the many possible ways to fail this API. */
  1135. abort_free_ctlr:
  1136. if (php_ctlr->creg)
  1137. iounmap(php_ctlr->creg);
  1138. kfree(php_ctlr);
  1139. abort:
  1140. DBG_LEAVE_ROUTINE
  1141. return rc;
  1142. }