shpchp_hpc.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Standard PCI Hot Plug Driver
  4. *
  5. * Copyright (C) 1995,2001 Compaq Computer Corporation
  6. * Copyright (C) 2001 Greg Kroah-Hartman ([email protected])
  7. * Copyright (C) 2001 IBM Corp.
  8. * Copyright (C) 2003-2004 Intel Corporation
  9. *
  10. * All rights reserved.
  11. *
  12. * Send feedback to <[email protected]>,<[email protected]>
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/types.h>
  18. #include <linux/pci.h>
  19. #include <linux/interrupt.h>
  20. #include "shpchp.h"
  21. /* Slot Available Register I field definition */
  22. #define SLOT_33MHZ 0x0000001f
  23. #define SLOT_66MHZ_PCIX 0x00001f00
  24. #define SLOT_100MHZ_PCIX 0x001f0000
  25. #define SLOT_133MHZ_PCIX 0x1f000000
  26. /* Slot Available Register II field definition */
  27. #define SLOT_66MHZ 0x0000001f
  28. #define SLOT_66MHZ_PCIX_266 0x00000f00
  29. #define SLOT_100MHZ_PCIX_266 0x0000f000
  30. #define SLOT_133MHZ_PCIX_266 0x000f0000
  31. #define SLOT_66MHZ_PCIX_533 0x00f00000
  32. #define SLOT_100MHZ_PCIX_533 0x0f000000
  33. #define SLOT_133MHZ_PCIX_533 0xf0000000
  34. /* Slot Configuration */
  35. #define SLOT_NUM 0x0000001F
  36. #define FIRST_DEV_NUM 0x00001F00
  37. #define PSN 0x07FF0000
  38. #define UPDOWN 0x20000000
  39. #define MRLSENSOR 0x40000000
  40. #define ATTN_BUTTON 0x80000000
  41. /*
  42. * Interrupt Locator Register definitions
  43. */
  44. #define CMD_INTR_PENDING (1 << 0)
  45. #define SLOT_INTR_PENDING(i) (1 << (i + 1))
  46. /*
  47. * Controller SERR-INT Register
  48. */
  49. #define GLOBAL_INTR_MASK (1 << 0)
  50. #define GLOBAL_SERR_MASK (1 << 1)
  51. #define COMMAND_INTR_MASK (1 << 2)
  52. #define ARBITER_SERR_MASK (1 << 3)
  53. #define COMMAND_DETECTED (1 << 16)
  54. #define ARBITER_DETECTED (1 << 17)
  55. #define SERR_INTR_RSVDZ_MASK 0xfffc0000
  56. /*
  57. * Logical Slot Register definitions
  58. */
  59. #define SLOT_REG(i) (SLOT1 + (4 * i))
  60. #define SLOT_STATE_SHIFT (0)
  61. #define SLOT_STATE_MASK (3 << 0)
  62. #define SLOT_STATE_PWRONLY (1)
  63. #define SLOT_STATE_ENABLED (2)
  64. #define SLOT_STATE_DISABLED (3)
  65. #define PWR_LED_STATE_SHIFT (2)
  66. #define PWR_LED_STATE_MASK (3 << 2)
  67. #define ATN_LED_STATE_SHIFT (4)
  68. #define ATN_LED_STATE_MASK (3 << 4)
  69. #define ATN_LED_STATE_ON (1)
  70. #define ATN_LED_STATE_BLINK (2)
  71. #define ATN_LED_STATE_OFF (3)
  72. #define POWER_FAULT (1 << 6)
  73. #define ATN_BUTTON (1 << 7)
  74. #define MRL_SENSOR (1 << 8)
  75. #define MHZ66_CAP (1 << 9)
  76. #define PRSNT_SHIFT (10)
  77. #define PRSNT_MASK (3 << 10)
  78. #define PCIX_CAP_SHIFT (12)
  79. #define PCIX_CAP_MASK_PI1 (3 << 12)
  80. #define PCIX_CAP_MASK_PI2 (7 << 12)
  81. #define PRSNT_CHANGE_DETECTED (1 << 16)
  82. #define ISO_PFAULT_DETECTED (1 << 17)
  83. #define BUTTON_PRESS_DETECTED (1 << 18)
  84. #define MRL_CHANGE_DETECTED (1 << 19)
  85. #define CON_PFAULT_DETECTED (1 << 20)
  86. #define PRSNT_CHANGE_INTR_MASK (1 << 24)
  87. #define ISO_PFAULT_INTR_MASK (1 << 25)
  88. #define BUTTON_PRESS_INTR_MASK (1 << 26)
  89. #define MRL_CHANGE_INTR_MASK (1 << 27)
  90. #define CON_PFAULT_INTR_MASK (1 << 28)
  91. #define MRL_CHANGE_SERR_MASK (1 << 29)
  92. #define CON_PFAULT_SERR_MASK (1 << 30)
  93. #define SLOT_REG_RSVDZ_MASK ((1 << 15) | (7 << 21))
  94. /*
  95. * SHPC Command Code definitions
  96. *
  97. * Slot Operation 00h - 3Fh
  98. * Set Bus Segment Speed/Mode A 40h - 47h
  99. * Power-Only All Slots 48h
  100. * Enable All Slots 49h
  101. * Set Bus Segment Speed/Mode B (PI=2) 50h - 5Fh
  102. * Reserved Command Codes 60h - BFh
  103. * Vendor Specific Commands C0h - FFh
  104. */
  105. #define SET_SLOT_PWR 0x01 /* Slot Operation */
  106. #define SET_SLOT_ENABLE 0x02
  107. #define SET_SLOT_DISABLE 0x03
  108. #define SET_PWR_ON 0x04
  109. #define SET_PWR_BLINK 0x08
  110. #define SET_PWR_OFF 0x0c
  111. #define SET_ATTN_ON 0x10
  112. #define SET_ATTN_BLINK 0x20
  113. #define SET_ATTN_OFF 0x30
  114. #define SETA_PCI_33MHZ 0x40 /* Set Bus Segment Speed/Mode A */
  115. #define SETA_PCI_66MHZ 0x41
  116. #define SETA_PCIX_66MHZ 0x42
  117. #define SETA_PCIX_100MHZ 0x43
  118. #define SETA_PCIX_133MHZ 0x44
  119. #define SETA_RESERVED1 0x45
  120. #define SETA_RESERVED2 0x46
  121. #define SETA_RESERVED3 0x47
  122. #define SET_PWR_ONLY_ALL 0x48 /* Power-Only All Slots */
  123. #define SET_ENABLE_ALL 0x49 /* Enable All Slots */
  124. #define SETB_PCI_33MHZ 0x50 /* Set Bus Segment Speed/Mode B */
  125. #define SETB_PCI_66MHZ 0x51
  126. #define SETB_PCIX_66MHZ_PM 0x52
  127. #define SETB_PCIX_100MHZ_PM 0x53
  128. #define SETB_PCIX_133MHZ_PM 0x54
  129. #define SETB_PCIX_66MHZ_EM 0x55
  130. #define SETB_PCIX_100MHZ_EM 0x56
  131. #define SETB_PCIX_133MHZ_EM 0x57
  132. #define SETB_PCIX_66MHZ_266 0x58
  133. #define SETB_PCIX_100MHZ_266 0x59
  134. #define SETB_PCIX_133MHZ_266 0x5a
  135. #define SETB_PCIX_66MHZ_533 0x5b
  136. #define SETB_PCIX_100MHZ_533 0x5c
  137. #define SETB_PCIX_133MHZ_533 0x5d
  138. #define SETB_RESERVED1 0x5e
  139. #define SETB_RESERVED2 0x5f
  140. /*
  141. * SHPC controller command error code
  142. */
  143. #define SWITCH_OPEN 0x1
  144. #define INVALID_CMD 0x2
  145. #define INVALID_SPEED_MODE 0x4
  146. /*
  147. * For accessing SHPC Working Register Set via PCI Configuration Space
  148. */
  149. #define DWORD_SELECT 0x2
  150. #define DWORD_DATA 0x4
  151. /* Field Offset in Logical Slot Register - byte boundary */
  152. #define SLOT_EVENT_LATCH 0x2
  153. #define SLOT_SERR_INT_MASK 0x3
  154. static irqreturn_t shpc_isr(int irq, void *dev_id);
  155. static void start_int_poll_timer(struct controller *ctrl, int sec);
  156. static int hpc_check_cmd_status(struct controller *ctrl);
  157. static inline u8 shpc_readb(struct controller *ctrl, int reg)
  158. {
  159. return readb(ctrl->creg + reg);
  160. }
  161. static inline u16 shpc_readw(struct controller *ctrl, int reg)
  162. {
  163. return readw(ctrl->creg + reg);
  164. }
  165. static inline void shpc_writew(struct controller *ctrl, int reg, u16 val)
  166. {
  167. writew(val, ctrl->creg + reg);
  168. }
  169. static inline u32 shpc_readl(struct controller *ctrl, int reg)
  170. {
  171. return readl(ctrl->creg + reg);
  172. }
  173. static inline void shpc_writel(struct controller *ctrl, int reg, u32 val)
  174. {
  175. writel(val, ctrl->creg + reg);
  176. }
  177. static inline int shpc_indirect_read(struct controller *ctrl, int index,
  178. u32 *value)
  179. {
  180. int rc;
  181. u32 cap_offset = ctrl->cap_offset;
  182. struct pci_dev *pdev = ctrl->pci_dev;
  183. rc = pci_write_config_byte(pdev, cap_offset + DWORD_SELECT, index);
  184. if (rc)
  185. return rc;
  186. return pci_read_config_dword(pdev, cap_offset + DWORD_DATA, value);
  187. }
  188. /*
  189. * This is the interrupt polling timeout function.
  190. */
  191. static void int_poll_timeout(struct timer_list *t)
  192. {
  193. struct controller *ctrl = from_timer(ctrl, t, poll_timer);
  194. /* Poll for interrupt events. regs == NULL => polling */
  195. shpc_isr(0, ctrl);
  196. if (!shpchp_poll_time)
  197. shpchp_poll_time = 2; /* default polling interval is 2 sec */
  198. start_int_poll_timer(ctrl, shpchp_poll_time);
  199. }
  200. /*
  201. * This function starts the interrupt polling timer.
  202. */
  203. static void start_int_poll_timer(struct controller *ctrl, int sec)
  204. {
  205. /* Clamp to sane value */
  206. if ((sec <= 0) || (sec > 60))
  207. sec = 2;
  208. ctrl->poll_timer.expires = jiffies + sec * HZ;
  209. add_timer(&ctrl->poll_timer);
  210. }
  211. static inline int is_ctrl_busy(struct controller *ctrl)
  212. {
  213. u16 cmd_status = shpc_readw(ctrl, CMD_STATUS);
  214. return cmd_status & 0x1;
  215. }
  216. /*
  217. * Returns 1 if SHPC finishes executing a command within 1 sec,
  218. * otherwise returns 0.
  219. */
  220. static inline int shpc_poll_ctrl_busy(struct controller *ctrl)
  221. {
  222. int i;
  223. if (!is_ctrl_busy(ctrl))
  224. return 1;
  225. /* Check every 0.1 sec for a total of 1 sec */
  226. for (i = 0; i < 10; i++) {
  227. msleep(100);
  228. if (!is_ctrl_busy(ctrl))
  229. return 1;
  230. }
  231. return 0;
  232. }
  233. static inline int shpc_wait_cmd(struct controller *ctrl)
  234. {
  235. int retval = 0;
  236. unsigned long timeout = msecs_to_jiffies(1000);
  237. int rc;
  238. if (shpchp_poll_mode)
  239. rc = shpc_poll_ctrl_busy(ctrl);
  240. else
  241. rc = wait_event_interruptible_timeout(ctrl->queue,
  242. !is_ctrl_busy(ctrl), timeout);
  243. if (!rc && is_ctrl_busy(ctrl)) {
  244. retval = -EIO;
  245. ctrl_err(ctrl, "Command not completed in 1000 msec\n");
  246. } else if (rc < 0) {
  247. retval = -EINTR;
  248. ctrl_info(ctrl, "Command was interrupted by a signal\n");
  249. }
  250. return retval;
  251. }
  252. static int shpc_write_cmd(struct slot *slot, u8 t_slot, u8 cmd)
  253. {
  254. struct controller *ctrl = slot->ctrl;
  255. u16 cmd_status;
  256. int retval = 0;
  257. u16 temp_word;
  258. mutex_lock(&slot->ctrl->cmd_lock);
  259. if (!shpc_poll_ctrl_busy(ctrl)) {
  260. /* After 1 sec and the controller is still busy */
  261. ctrl_err(ctrl, "Controller is still busy after 1 sec\n");
  262. retval = -EBUSY;
  263. goto out;
  264. }
  265. ++t_slot;
  266. temp_word = (t_slot << 8) | (cmd & 0xFF);
  267. ctrl_dbg(ctrl, "%s: t_slot %x cmd %x\n", __func__, t_slot, cmd);
  268. /* To make sure the Controller Busy bit is 0 before we send out the
  269. * command.
  270. */
  271. shpc_writew(ctrl, CMD, temp_word);
  272. /*
  273. * Wait for command completion.
  274. */
  275. retval = shpc_wait_cmd(slot->ctrl);
  276. if (retval)
  277. goto out;
  278. cmd_status = hpc_check_cmd_status(slot->ctrl);
  279. if (cmd_status) {
  280. ctrl_err(ctrl, "Failed to issued command 0x%x (error code = %d)\n",
  281. cmd, cmd_status);
  282. retval = -EIO;
  283. }
  284. out:
  285. mutex_unlock(&slot->ctrl->cmd_lock);
  286. return retval;
  287. }
  288. static int hpc_check_cmd_status(struct controller *ctrl)
  289. {
  290. int retval = 0;
  291. u16 cmd_status = shpc_readw(ctrl, CMD_STATUS) & 0x000F;
  292. switch (cmd_status >> 1) {
  293. case 0:
  294. retval = 0;
  295. break;
  296. case 1:
  297. retval = SWITCH_OPEN;
  298. ctrl_err(ctrl, "Switch opened!\n");
  299. break;
  300. case 2:
  301. retval = INVALID_CMD;
  302. ctrl_err(ctrl, "Invalid HPC command!\n");
  303. break;
  304. case 4:
  305. retval = INVALID_SPEED_MODE;
  306. ctrl_err(ctrl, "Invalid bus speed/mode!\n");
  307. break;
  308. default:
  309. retval = cmd_status;
  310. }
  311. return retval;
  312. }
  313. static int hpc_get_attention_status(struct slot *slot, u8 *status)
  314. {
  315. struct controller *ctrl = slot->ctrl;
  316. u32 slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
  317. u8 state = (slot_reg & ATN_LED_STATE_MASK) >> ATN_LED_STATE_SHIFT;
  318. switch (state) {
  319. case ATN_LED_STATE_ON:
  320. *status = 1; /* On */
  321. break;
  322. case ATN_LED_STATE_BLINK:
  323. *status = 2; /* Blink */
  324. break;
  325. case ATN_LED_STATE_OFF:
  326. *status = 0; /* Off */
  327. break;
  328. default:
  329. *status = 0xFF; /* Reserved */
  330. break;
  331. }
  332. return 0;
  333. }
  334. static int hpc_get_power_status(struct slot *slot, u8 *status)
  335. {
  336. struct controller *ctrl = slot->ctrl;
  337. u32 slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
  338. u8 state = (slot_reg & SLOT_STATE_MASK) >> SLOT_STATE_SHIFT;
  339. switch (state) {
  340. case SLOT_STATE_PWRONLY:
  341. *status = 2; /* Powered only */
  342. break;
  343. case SLOT_STATE_ENABLED:
  344. *status = 1; /* Enabled */
  345. break;
  346. case SLOT_STATE_DISABLED:
  347. *status = 0; /* Disabled */
  348. break;
  349. default:
  350. *status = 0xFF; /* Reserved */
  351. break;
  352. }
  353. return 0;
  354. }
  355. static int hpc_get_latch_status(struct slot *slot, u8 *status)
  356. {
  357. struct controller *ctrl = slot->ctrl;
  358. u32 slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
  359. *status = !!(slot_reg & MRL_SENSOR); /* 0 -> close; 1 -> open */
  360. return 0;
  361. }
  362. static int hpc_get_adapter_status(struct slot *slot, u8 *status)
  363. {
  364. struct controller *ctrl = slot->ctrl;
  365. u32 slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
  366. u8 state = (slot_reg & PRSNT_MASK) >> PRSNT_SHIFT;
  367. *status = (state != 0x3) ? 1 : 0;
  368. return 0;
  369. }
  370. static int hpc_get_prog_int(struct slot *slot, u8 *prog_int)
  371. {
  372. struct controller *ctrl = slot->ctrl;
  373. *prog_int = shpc_readb(ctrl, PROG_INTERFACE);
  374. return 0;
  375. }
  376. static int hpc_get_adapter_speed(struct slot *slot, enum pci_bus_speed *value)
  377. {
  378. int retval = 0;
  379. struct controller *ctrl = slot->ctrl;
  380. u32 slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
  381. u8 m66_cap = !!(slot_reg & MHZ66_CAP);
  382. u8 pi, pcix_cap;
  383. retval = hpc_get_prog_int(slot, &pi);
  384. if (retval)
  385. return retval;
  386. switch (pi) {
  387. case 1:
  388. pcix_cap = (slot_reg & PCIX_CAP_MASK_PI1) >> PCIX_CAP_SHIFT;
  389. break;
  390. case 2:
  391. pcix_cap = (slot_reg & PCIX_CAP_MASK_PI2) >> PCIX_CAP_SHIFT;
  392. break;
  393. default:
  394. return -ENODEV;
  395. }
  396. ctrl_dbg(ctrl, "%s: slot_reg = %x, pcix_cap = %x, m66_cap = %x\n",
  397. __func__, slot_reg, pcix_cap, m66_cap);
  398. switch (pcix_cap) {
  399. case 0x0:
  400. *value = m66_cap ? PCI_SPEED_66MHz : PCI_SPEED_33MHz;
  401. break;
  402. case 0x1:
  403. *value = PCI_SPEED_66MHz_PCIX;
  404. break;
  405. case 0x3:
  406. *value = PCI_SPEED_133MHz_PCIX;
  407. break;
  408. case 0x4:
  409. *value = PCI_SPEED_133MHz_PCIX_266;
  410. break;
  411. case 0x5:
  412. *value = PCI_SPEED_133MHz_PCIX_533;
  413. break;
  414. case 0x2:
  415. default:
  416. *value = PCI_SPEED_UNKNOWN;
  417. retval = -ENODEV;
  418. break;
  419. }
  420. ctrl_dbg(ctrl, "Adapter speed = %d\n", *value);
  421. return retval;
  422. }
  423. static int hpc_get_mode1_ECC_cap(struct slot *slot, u8 *mode)
  424. {
  425. int retval = 0;
  426. struct controller *ctrl = slot->ctrl;
  427. u16 sec_bus_status = shpc_readw(ctrl, SEC_BUS_CONFIG);
  428. u8 pi = shpc_readb(ctrl, PROG_INTERFACE);
  429. if (pi == 2) {
  430. *mode = (sec_bus_status & 0x0100) >> 8;
  431. } else {
  432. retval = -1;
  433. }
  434. ctrl_dbg(ctrl, "Mode 1 ECC cap = %d\n", *mode);
  435. return retval;
  436. }
  437. static int hpc_query_power_fault(struct slot *slot)
  438. {
  439. struct controller *ctrl = slot->ctrl;
  440. u32 slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
  441. /* Note: Logic 0 => fault */
  442. return !(slot_reg & POWER_FAULT);
  443. }
  444. static int hpc_set_attention_status(struct slot *slot, u8 value)
  445. {
  446. u8 slot_cmd = 0;
  447. switch (value) {
  448. case 0:
  449. slot_cmd = SET_ATTN_OFF; /* OFF */
  450. break;
  451. case 1:
  452. slot_cmd = SET_ATTN_ON; /* ON */
  453. break;
  454. case 2:
  455. slot_cmd = SET_ATTN_BLINK; /* BLINK */
  456. break;
  457. default:
  458. return -1;
  459. }
  460. return shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
  461. }
  462. static void hpc_set_green_led_on(struct slot *slot)
  463. {
  464. shpc_write_cmd(slot, slot->hp_slot, SET_PWR_ON);
  465. }
  466. static void hpc_set_green_led_off(struct slot *slot)
  467. {
  468. shpc_write_cmd(slot, slot->hp_slot, SET_PWR_OFF);
  469. }
  470. static void hpc_set_green_led_blink(struct slot *slot)
  471. {
  472. shpc_write_cmd(slot, slot->hp_slot, SET_PWR_BLINK);
  473. }
  474. static void hpc_release_ctlr(struct controller *ctrl)
  475. {
  476. int i;
  477. u32 slot_reg, serr_int;
  478. /*
  479. * Mask event interrupts and SERRs of all slots
  480. */
  481. for (i = 0; i < ctrl->num_slots; i++) {
  482. slot_reg = shpc_readl(ctrl, SLOT_REG(i));
  483. slot_reg |= (PRSNT_CHANGE_INTR_MASK | ISO_PFAULT_INTR_MASK |
  484. BUTTON_PRESS_INTR_MASK | MRL_CHANGE_INTR_MASK |
  485. CON_PFAULT_INTR_MASK | MRL_CHANGE_SERR_MASK |
  486. CON_PFAULT_SERR_MASK);
  487. slot_reg &= ~SLOT_REG_RSVDZ_MASK;
  488. shpc_writel(ctrl, SLOT_REG(i), slot_reg);
  489. }
  490. cleanup_slots(ctrl);
  491. /*
  492. * Mask SERR and System Interrupt generation
  493. */
  494. serr_int = shpc_readl(ctrl, SERR_INTR_ENABLE);
  495. serr_int |= (GLOBAL_INTR_MASK | GLOBAL_SERR_MASK |
  496. COMMAND_INTR_MASK | ARBITER_SERR_MASK);
  497. serr_int &= ~SERR_INTR_RSVDZ_MASK;
  498. shpc_writel(ctrl, SERR_INTR_ENABLE, serr_int);
  499. if (shpchp_poll_mode)
  500. del_timer(&ctrl->poll_timer);
  501. else {
  502. free_irq(ctrl->pci_dev->irq, ctrl);
  503. pci_disable_msi(ctrl->pci_dev);
  504. }
  505. iounmap(ctrl->creg);
  506. release_mem_region(ctrl->mmio_base, ctrl->mmio_size);
  507. }
  508. static int hpc_power_on_slot(struct slot *slot)
  509. {
  510. int retval;
  511. retval = shpc_write_cmd(slot, slot->hp_slot, SET_SLOT_PWR);
  512. if (retval)
  513. ctrl_err(slot->ctrl, "%s: Write command failed!\n", __func__);
  514. return retval;
  515. }
  516. static int hpc_slot_enable(struct slot *slot)
  517. {
  518. int retval;
  519. /* Slot - Enable, Power Indicator - Blink, Attention Indicator - Off */
  520. retval = shpc_write_cmd(slot, slot->hp_slot,
  521. SET_SLOT_ENABLE | SET_PWR_BLINK | SET_ATTN_OFF);
  522. if (retval)
  523. ctrl_err(slot->ctrl, "%s: Write command failed!\n", __func__);
  524. return retval;
  525. }
  526. static int hpc_slot_disable(struct slot *slot)
  527. {
  528. int retval;
  529. /* Slot - Disable, Power Indicator - Off, Attention Indicator - On */
  530. retval = shpc_write_cmd(slot, slot->hp_slot,
  531. SET_SLOT_DISABLE | SET_PWR_OFF | SET_ATTN_ON);
  532. if (retval)
  533. ctrl_err(slot->ctrl, "%s: Write command failed!\n", __func__);
  534. return retval;
  535. }
  536. static int shpc_get_cur_bus_speed(struct controller *ctrl)
  537. {
  538. int retval = 0;
  539. struct pci_bus *bus = ctrl->pci_dev->subordinate;
  540. enum pci_bus_speed bus_speed = PCI_SPEED_UNKNOWN;
  541. u16 sec_bus_reg = shpc_readw(ctrl, SEC_BUS_CONFIG);
  542. u8 pi = shpc_readb(ctrl, PROG_INTERFACE);
  543. u8 speed_mode = (pi == 2) ? (sec_bus_reg & 0xF) : (sec_bus_reg & 0x7);
  544. if ((pi == 1) && (speed_mode > 4)) {
  545. retval = -ENODEV;
  546. goto out;
  547. }
  548. switch (speed_mode) {
  549. case 0x0:
  550. bus_speed = PCI_SPEED_33MHz;
  551. break;
  552. case 0x1:
  553. bus_speed = PCI_SPEED_66MHz;
  554. break;
  555. case 0x2:
  556. bus_speed = PCI_SPEED_66MHz_PCIX;
  557. break;
  558. case 0x3:
  559. bus_speed = PCI_SPEED_100MHz_PCIX;
  560. break;
  561. case 0x4:
  562. bus_speed = PCI_SPEED_133MHz_PCIX;
  563. break;
  564. case 0x5:
  565. bus_speed = PCI_SPEED_66MHz_PCIX_ECC;
  566. break;
  567. case 0x6:
  568. bus_speed = PCI_SPEED_100MHz_PCIX_ECC;
  569. break;
  570. case 0x7:
  571. bus_speed = PCI_SPEED_133MHz_PCIX_ECC;
  572. break;
  573. case 0x8:
  574. bus_speed = PCI_SPEED_66MHz_PCIX_266;
  575. break;
  576. case 0x9:
  577. bus_speed = PCI_SPEED_100MHz_PCIX_266;
  578. break;
  579. case 0xa:
  580. bus_speed = PCI_SPEED_133MHz_PCIX_266;
  581. break;
  582. case 0xb:
  583. bus_speed = PCI_SPEED_66MHz_PCIX_533;
  584. break;
  585. case 0xc:
  586. bus_speed = PCI_SPEED_100MHz_PCIX_533;
  587. break;
  588. case 0xd:
  589. bus_speed = PCI_SPEED_133MHz_PCIX_533;
  590. break;
  591. default:
  592. retval = -ENODEV;
  593. break;
  594. }
  595. out:
  596. bus->cur_bus_speed = bus_speed;
  597. dbg("Current bus speed = %d\n", bus_speed);
  598. return retval;
  599. }
  600. static int hpc_set_bus_speed_mode(struct slot *slot, enum pci_bus_speed value)
  601. {
  602. int retval;
  603. struct controller *ctrl = slot->ctrl;
  604. u8 pi, cmd;
  605. pi = shpc_readb(ctrl, PROG_INTERFACE);
  606. if ((pi == 1) && (value > PCI_SPEED_133MHz_PCIX))
  607. return -EINVAL;
  608. switch (value) {
  609. case PCI_SPEED_33MHz:
  610. cmd = SETA_PCI_33MHZ;
  611. break;
  612. case PCI_SPEED_66MHz:
  613. cmd = SETA_PCI_66MHZ;
  614. break;
  615. case PCI_SPEED_66MHz_PCIX:
  616. cmd = SETA_PCIX_66MHZ;
  617. break;
  618. case PCI_SPEED_100MHz_PCIX:
  619. cmd = SETA_PCIX_100MHZ;
  620. break;
  621. case PCI_SPEED_133MHz_PCIX:
  622. cmd = SETA_PCIX_133MHZ;
  623. break;
  624. case PCI_SPEED_66MHz_PCIX_ECC:
  625. cmd = SETB_PCIX_66MHZ_EM;
  626. break;
  627. case PCI_SPEED_100MHz_PCIX_ECC:
  628. cmd = SETB_PCIX_100MHZ_EM;
  629. break;
  630. case PCI_SPEED_133MHz_PCIX_ECC:
  631. cmd = SETB_PCIX_133MHZ_EM;
  632. break;
  633. case PCI_SPEED_66MHz_PCIX_266:
  634. cmd = SETB_PCIX_66MHZ_266;
  635. break;
  636. case PCI_SPEED_100MHz_PCIX_266:
  637. cmd = SETB_PCIX_100MHZ_266;
  638. break;
  639. case PCI_SPEED_133MHz_PCIX_266:
  640. cmd = SETB_PCIX_133MHZ_266;
  641. break;
  642. case PCI_SPEED_66MHz_PCIX_533:
  643. cmd = SETB_PCIX_66MHZ_533;
  644. break;
  645. case PCI_SPEED_100MHz_PCIX_533:
  646. cmd = SETB_PCIX_100MHZ_533;
  647. break;
  648. case PCI_SPEED_133MHz_PCIX_533:
  649. cmd = SETB_PCIX_133MHZ_533;
  650. break;
  651. default:
  652. return -EINVAL;
  653. }
  654. retval = shpc_write_cmd(slot, 0, cmd);
  655. if (retval)
  656. ctrl_err(ctrl, "%s: Write command failed!\n", __func__);
  657. else
  658. shpc_get_cur_bus_speed(ctrl);
  659. return retval;
  660. }
  661. static irqreturn_t shpc_isr(int irq, void *dev_id)
  662. {
  663. struct controller *ctrl = (struct controller *)dev_id;
  664. u32 serr_int, slot_reg, intr_loc, intr_loc2;
  665. int hp_slot;
  666. /* Check to see if it was our interrupt */
  667. intr_loc = shpc_readl(ctrl, INTR_LOC);
  668. if (!intr_loc)
  669. return IRQ_NONE;
  670. ctrl_dbg(ctrl, "%s: intr_loc = %x\n", __func__, intr_loc);
  671. if (!shpchp_poll_mode) {
  672. /*
  673. * Mask Global Interrupt Mask - see implementation
  674. * note on p. 139 of SHPC spec rev 1.0
  675. */
  676. serr_int = shpc_readl(ctrl, SERR_INTR_ENABLE);
  677. serr_int |= GLOBAL_INTR_MASK;
  678. serr_int &= ~SERR_INTR_RSVDZ_MASK;
  679. shpc_writel(ctrl, SERR_INTR_ENABLE, serr_int);
  680. intr_loc2 = shpc_readl(ctrl, INTR_LOC);
  681. ctrl_dbg(ctrl, "%s: intr_loc2 = %x\n", __func__, intr_loc2);
  682. }
  683. if (intr_loc & CMD_INTR_PENDING) {
  684. /*
  685. * Command Complete Interrupt Pending
  686. * RO only - clear by writing 1 to the Command Completion
  687. * Detect bit in Controller SERR-INT register
  688. */
  689. serr_int = shpc_readl(ctrl, SERR_INTR_ENABLE);
  690. serr_int &= ~SERR_INTR_RSVDZ_MASK;
  691. shpc_writel(ctrl, SERR_INTR_ENABLE, serr_int);
  692. wake_up_interruptible(&ctrl->queue);
  693. }
  694. if (!(intr_loc & ~CMD_INTR_PENDING))
  695. goto out;
  696. for (hp_slot = 0; hp_slot < ctrl->num_slots; hp_slot++) {
  697. /* To find out which slot has interrupt pending */
  698. if (!(intr_loc & SLOT_INTR_PENDING(hp_slot)))
  699. continue;
  700. slot_reg = shpc_readl(ctrl, SLOT_REG(hp_slot));
  701. ctrl_dbg(ctrl, "Slot %x with intr, slot register = %x\n",
  702. hp_slot, slot_reg);
  703. if (slot_reg & MRL_CHANGE_DETECTED)
  704. shpchp_handle_switch_change(hp_slot, ctrl);
  705. if (slot_reg & BUTTON_PRESS_DETECTED)
  706. shpchp_handle_attention_button(hp_slot, ctrl);
  707. if (slot_reg & PRSNT_CHANGE_DETECTED)
  708. shpchp_handle_presence_change(hp_slot, ctrl);
  709. if (slot_reg & (ISO_PFAULT_DETECTED | CON_PFAULT_DETECTED))
  710. shpchp_handle_power_fault(hp_slot, ctrl);
  711. /* Clear all slot events */
  712. slot_reg &= ~SLOT_REG_RSVDZ_MASK;
  713. shpc_writel(ctrl, SLOT_REG(hp_slot), slot_reg);
  714. }
  715. out:
  716. if (!shpchp_poll_mode) {
  717. /* Unmask Global Interrupt Mask */
  718. serr_int = shpc_readl(ctrl, SERR_INTR_ENABLE);
  719. serr_int &= ~(GLOBAL_INTR_MASK | SERR_INTR_RSVDZ_MASK);
  720. shpc_writel(ctrl, SERR_INTR_ENABLE, serr_int);
  721. }
  722. return IRQ_HANDLED;
  723. }
  724. static int shpc_get_max_bus_speed(struct controller *ctrl)
  725. {
  726. int retval = 0;
  727. struct pci_bus *bus = ctrl->pci_dev->subordinate;
  728. enum pci_bus_speed bus_speed = PCI_SPEED_UNKNOWN;
  729. u8 pi = shpc_readb(ctrl, PROG_INTERFACE);
  730. u32 slot_avail1 = shpc_readl(ctrl, SLOT_AVAIL1);
  731. u32 slot_avail2 = shpc_readl(ctrl, SLOT_AVAIL2);
  732. if (pi == 2) {
  733. if (slot_avail2 & SLOT_133MHZ_PCIX_533)
  734. bus_speed = PCI_SPEED_133MHz_PCIX_533;
  735. else if (slot_avail2 & SLOT_100MHZ_PCIX_533)
  736. bus_speed = PCI_SPEED_100MHz_PCIX_533;
  737. else if (slot_avail2 & SLOT_66MHZ_PCIX_533)
  738. bus_speed = PCI_SPEED_66MHz_PCIX_533;
  739. else if (slot_avail2 & SLOT_133MHZ_PCIX_266)
  740. bus_speed = PCI_SPEED_133MHz_PCIX_266;
  741. else if (slot_avail2 & SLOT_100MHZ_PCIX_266)
  742. bus_speed = PCI_SPEED_100MHz_PCIX_266;
  743. else if (slot_avail2 & SLOT_66MHZ_PCIX_266)
  744. bus_speed = PCI_SPEED_66MHz_PCIX_266;
  745. }
  746. if (bus_speed == PCI_SPEED_UNKNOWN) {
  747. if (slot_avail1 & SLOT_133MHZ_PCIX)
  748. bus_speed = PCI_SPEED_133MHz_PCIX;
  749. else if (slot_avail1 & SLOT_100MHZ_PCIX)
  750. bus_speed = PCI_SPEED_100MHz_PCIX;
  751. else if (slot_avail1 & SLOT_66MHZ_PCIX)
  752. bus_speed = PCI_SPEED_66MHz_PCIX;
  753. else if (slot_avail2 & SLOT_66MHZ)
  754. bus_speed = PCI_SPEED_66MHz;
  755. else if (slot_avail1 & SLOT_33MHZ)
  756. bus_speed = PCI_SPEED_33MHz;
  757. else
  758. retval = -ENODEV;
  759. }
  760. bus->max_bus_speed = bus_speed;
  761. ctrl_dbg(ctrl, "Max bus speed = %d\n", bus_speed);
  762. return retval;
  763. }
  764. static const struct hpc_ops shpchp_hpc_ops = {
  765. .power_on_slot = hpc_power_on_slot,
  766. .slot_enable = hpc_slot_enable,
  767. .slot_disable = hpc_slot_disable,
  768. .set_bus_speed_mode = hpc_set_bus_speed_mode,
  769. .set_attention_status = hpc_set_attention_status,
  770. .get_power_status = hpc_get_power_status,
  771. .get_attention_status = hpc_get_attention_status,
  772. .get_latch_status = hpc_get_latch_status,
  773. .get_adapter_status = hpc_get_adapter_status,
  774. .get_adapter_speed = hpc_get_adapter_speed,
  775. .get_mode1_ECC_cap = hpc_get_mode1_ECC_cap,
  776. .get_prog_int = hpc_get_prog_int,
  777. .query_power_fault = hpc_query_power_fault,
  778. .green_led_on = hpc_set_green_led_on,
  779. .green_led_off = hpc_set_green_led_off,
  780. .green_led_blink = hpc_set_green_led_blink,
  781. .release_ctlr = hpc_release_ctlr,
  782. };
  783. int shpc_init(struct controller *ctrl, struct pci_dev *pdev)
  784. {
  785. int rc = -1, num_slots = 0;
  786. u8 hp_slot;
  787. u32 shpc_base_offset;
  788. u32 tempdword, slot_reg, slot_config;
  789. u8 i;
  790. ctrl->pci_dev = pdev; /* pci_dev of the P2P bridge */
  791. ctrl_dbg(ctrl, "Hotplug Controller:\n");
  792. if (pdev->vendor == PCI_VENDOR_ID_AMD &&
  793. pdev->device == PCI_DEVICE_ID_AMD_GOLAM_7450) {
  794. /* amd shpc driver doesn't use Base Offset; assume 0 */
  795. ctrl->mmio_base = pci_resource_start(pdev, 0);
  796. ctrl->mmio_size = pci_resource_len(pdev, 0);
  797. } else {
  798. ctrl->cap_offset = pci_find_capability(pdev, PCI_CAP_ID_SHPC);
  799. if (!ctrl->cap_offset) {
  800. ctrl_err(ctrl, "Cannot find PCI capability\n");
  801. goto abort;
  802. }
  803. ctrl_dbg(ctrl, " cap_offset = %x\n", ctrl->cap_offset);
  804. rc = shpc_indirect_read(ctrl, 0, &shpc_base_offset);
  805. if (rc) {
  806. ctrl_err(ctrl, "Cannot read base_offset\n");
  807. goto abort;
  808. }
  809. rc = shpc_indirect_read(ctrl, 3, &tempdword);
  810. if (rc) {
  811. ctrl_err(ctrl, "Cannot read slot config\n");
  812. goto abort;
  813. }
  814. num_slots = tempdword & SLOT_NUM;
  815. ctrl_dbg(ctrl, " num_slots (indirect) %x\n", num_slots);
  816. for (i = 0; i < 9 + num_slots; i++) {
  817. rc = shpc_indirect_read(ctrl, i, &tempdword);
  818. if (rc) {
  819. ctrl_err(ctrl, "Cannot read creg (index = %d)\n",
  820. i);
  821. goto abort;
  822. }
  823. ctrl_dbg(ctrl, " offset %d: value %x\n", i, tempdword);
  824. }
  825. ctrl->mmio_base =
  826. pci_resource_start(pdev, 0) + shpc_base_offset;
  827. ctrl->mmio_size = 0x24 + 0x4 * num_slots;
  828. }
  829. ctrl_info(ctrl, "HPC vendor_id %x device_id %x ss_vid %x ss_did %x\n",
  830. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  831. pdev->subsystem_device);
  832. rc = pci_enable_device(pdev);
  833. if (rc) {
  834. ctrl_err(ctrl, "pci_enable_device failed\n");
  835. goto abort;
  836. }
  837. if (!request_mem_region(ctrl->mmio_base, ctrl->mmio_size, MY_NAME)) {
  838. ctrl_err(ctrl, "Cannot reserve MMIO region\n");
  839. rc = -1;
  840. goto abort;
  841. }
  842. ctrl->creg = ioremap(ctrl->mmio_base, ctrl->mmio_size);
  843. if (!ctrl->creg) {
  844. ctrl_err(ctrl, "Cannot remap MMIO region %lx @ %lx\n",
  845. ctrl->mmio_size, ctrl->mmio_base);
  846. release_mem_region(ctrl->mmio_base, ctrl->mmio_size);
  847. rc = -1;
  848. goto abort;
  849. }
  850. ctrl_dbg(ctrl, "ctrl->creg %p\n", ctrl->creg);
  851. mutex_init(&ctrl->crit_sect);
  852. mutex_init(&ctrl->cmd_lock);
  853. /* Setup wait queue */
  854. init_waitqueue_head(&ctrl->queue);
  855. ctrl->hpc_ops = &shpchp_hpc_ops;
  856. /* Return PCI Controller Info */
  857. slot_config = shpc_readl(ctrl, SLOT_CONFIG);
  858. ctrl->slot_device_offset = (slot_config & FIRST_DEV_NUM) >> 8;
  859. ctrl->num_slots = slot_config & SLOT_NUM;
  860. ctrl->first_slot = (slot_config & PSN) >> 16;
  861. ctrl->slot_num_inc = ((slot_config & UPDOWN) >> 29) ? 1 : -1;
  862. /* Mask Global Interrupt Mask & Command Complete Interrupt Mask */
  863. tempdword = shpc_readl(ctrl, SERR_INTR_ENABLE);
  864. ctrl_dbg(ctrl, "SERR_INTR_ENABLE = %x\n", tempdword);
  865. tempdword |= (GLOBAL_INTR_MASK | GLOBAL_SERR_MASK |
  866. COMMAND_INTR_MASK | ARBITER_SERR_MASK);
  867. tempdword &= ~SERR_INTR_RSVDZ_MASK;
  868. shpc_writel(ctrl, SERR_INTR_ENABLE, tempdword);
  869. tempdword = shpc_readl(ctrl, SERR_INTR_ENABLE);
  870. ctrl_dbg(ctrl, "SERR_INTR_ENABLE = %x\n", tempdword);
  871. /* Mask the MRL sensor SERR Mask of individual slot in
  872. * Slot SERR-INT Mask & clear all the existing event if any
  873. */
  874. for (hp_slot = 0; hp_slot < ctrl->num_slots; hp_slot++) {
  875. slot_reg = shpc_readl(ctrl, SLOT_REG(hp_slot));
  876. ctrl_dbg(ctrl, "Default Logical Slot Register %d value %x\n",
  877. hp_slot, slot_reg);
  878. slot_reg |= (PRSNT_CHANGE_INTR_MASK | ISO_PFAULT_INTR_MASK |
  879. BUTTON_PRESS_INTR_MASK | MRL_CHANGE_INTR_MASK |
  880. CON_PFAULT_INTR_MASK | MRL_CHANGE_SERR_MASK |
  881. CON_PFAULT_SERR_MASK);
  882. slot_reg &= ~SLOT_REG_RSVDZ_MASK;
  883. shpc_writel(ctrl, SLOT_REG(hp_slot), slot_reg);
  884. }
  885. if (shpchp_poll_mode) {
  886. /* Install interrupt polling timer. Start with 10 sec delay */
  887. timer_setup(&ctrl->poll_timer, int_poll_timeout, 0);
  888. start_int_poll_timer(ctrl, 10);
  889. } else {
  890. /* Installs the interrupt handler */
  891. rc = pci_enable_msi(pdev);
  892. if (rc) {
  893. ctrl_info(ctrl, "Can't get msi for the hotplug controller\n");
  894. ctrl_info(ctrl, "Use INTx for the hotplug controller\n");
  895. } else {
  896. pci_set_master(pdev);
  897. }
  898. rc = request_irq(ctrl->pci_dev->irq, shpc_isr, IRQF_SHARED,
  899. MY_NAME, (void *)ctrl);
  900. ctrl_dbg(ctrl, "request_irq %d (returns %d)\n",
  901. ctrl->pci_dev->irq, rc);
  902. if (rc) {
  903. ctrl_err(ctrl, "Can't get irq %d for the hotplug controller\n",
  904. ctrl->pci_dev->irq);
  905. goto abort_iounmap;
  906. }
  907. }
  908. ctrl_dbg(ctrl, "HPC at %s irq=%x\n", pci_name(pdev), pdev->irq);
  909. shpc_get_max_bus_speed(ctrl);
  910. shpc_get_cur_bus_speed(ctrl);
  911. /*
  912. * Unmask all event interrupts of all slots
  913. */
  914. for (hp_slot = 0; hp_slot < ctrl->num_slots; hp_slot++) {
  915. slot_reg = shpc_readl(ctrl, SLOT_REG(hp_slot));
  916. ctrl_dbg(ctrl, "Default Logical Slot Register %d value %x\n",
  917. hp_slot, slot_reg);
  918. slot_reg &= ~(PRSNT_CHANGE_INTR_MASK | ISO_PFAULT_INTR_MASK |
  919. BUTTON_PRESS_INTR_MASK | MRL_CHANGE_INTR_MASK |
  920. CON_PFAULT_INTR_MASK | SLOT_REG_RSVDZ_MASK);
  921. shpc_writel(ctrl, SLOT_REG(hp_slot), slot_reg);
  922. }
  923. if (!shpchp_poll_mode) {
  924. /* Unmask all general input interrupts and SERR */
  925. tempdword = shpc_readl(ctrl, SERR_INTR_ENABLE);
  926. tempdword &= ~(GLOBAL_INTR_MASK | COMMAND_INTR_MASK |
  927. SERR_INTR_RSVDZ_MASK);
  928. shpc_writel(ctrl, SERR_INTR_ENABLE, tempdword);
  929. tempdword = shpc_readl(ctrl, SERR_INTR_ENABLE);
  930. ctrl_dbg(ctrl, "SERR_INTR_ENABLE = %x\n", tempdword);
  931. }
  932. return 0;
  933. /* We end up here for the many possible ways to fail this API. */
  934. abort_iounmap:
  935. iounmap(ctrl->creg);
  936. abort:
  937. return rc;
  938. }