shpchp_hpc.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  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 (greg@kroah.com)
  7. * Copyright (C) 2001 IBM Corp.
  8. * Copyright (C) 2003-2004 Intel Corporation
  9. *
  10. * All rights reserved.
  11. *
  12. * Send feedback to <greg@kroah.com>,<kristen.c.accardi@intel.com>
  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 inline u8 shpc_readb(struct controller *ctrl, int reg)
  157. {
  158. return readb(ctrl->creg + reg);
  159. }
  160. static inline u16 shpc_readw(struct controller *ctrl, int reg)
  161. {
  162. return readw(ctrl->creg + reg);
  163. }
  164. static inline void shpc_writew(struct controller *ctrl, int reg, u16 val)
  165. {
  166. writew(val, ctrl->creg + reg);
  167. }
  168. static inline u32 shpc_readl(struct controller *ctrl, int reg)
  169. {
  170. return readl(ctrl->creg + reg);
  171. }
  172. static inline void shpc_writel(struct controller *ctrl, int reg, u32 val)
  173. {
  174. writel(val, ctrl->creg + reg);
  175. }
  176. static inline int shpc_indirect_read(struct controller *ctrl, int index,
  177. u32 *value)
  178. {
  179. int rc;
  180. u32 cap_offset = ctrl->cap_offset;
  181. struct pci_dev *pdev = ctrl->pci_dev;
  182. rc = pci_write_config_byte(pdev, cap_offset + DWORD_SELECT, index);
  183. if (rc)
  184. return rc;
  185. return pci_read_config_dword(pdev, cap_offset + DWORD_DATA, value);
  186. }
  187. /*
  188. * This is the interrupt polling timeout function.
  189. */
  190. static void int_poll_timeout(struct timer_list *t)
  191. {
  192. struct controller *ctrl = timer_container_of(ctrl, t, poll_timer);
  193. /* Poll for interrupt events. regs == NULL => polling */
  194. shpc_isr(0, ctrl);
  195. if (!shpchp_poll_time)
  196. shpchp_poll_time = 2; /* default polling interval is 2 sec */
  197. start_int_poll_timer(ctrl, shpchp_poll_time);
  198. }
  199. /*
  200. * This function starts the interrupt polling timer.
  201. */
  202. static void start_int_poll_timer(struct controller *ctrl, int sec)
  203. {
  204. /* Clamp to sane value */
  205. if ((sec <= 0) || (sec > 60))
  206. sec = 2;
  207. ctrl->poll_timer.expires = jiffies + sec * HZ;
  208. add_timer(&ctrl->poll_timer);
  209. }
  210. static inline int is_ctrl_busy(struct controller *ctrl)
  211. {
  212. u16 cmd_status = shpc_readw(ctrl, CMD_STATUS);
  213. return cmd_status & 0x1;
  214. }
  215. /*
  216. * Returns 1 if SHPC finishes executing a command within 1 sec,
  217. * otherwise returns 0.
  218. */
  219. static inline int shpc_poll_ctrl_busy(struct controller *ctrl)
  220. {
  221. int i;
  222. if (!is_ctrl_busy(ctrl))
  223. return 1;
  224. /* Check every 0.1 sec for a total of 1 sec */
  225. for (i = 0; i < 10; i++) {
  226. msleep(100);
  227. if (!is_ctrl_busy(ctrl))
  228. return 1;
  229. }
  230. return 0;
  231. }
  232. static inline int shpc_wait_cmd(struct controller *ctrl)
  233. {
  234. int retval = 0;
  235. unsigned long timeout = msecs_to_jiffies(1000);
  236. int rc;
  237. if (shpchp_poll_mode)
  238. rc = shpc_poll_ctrl_busy(ctrl);
  239. else
  240. rc = wait_event_interruptible_timeout(ctrl->queue,
  241. !is_ctrl_busy(ctrl), timeout);
  242. if (!rc && is_ctrl_busy(ctrl)) {
  243. retval = -EIO;
  244. ctrl_err(ctrl, "Command not completed in 1000 msec\n");
  245. } else if (rc < 0) {
  246. retval = -EINTR;
  247. ctrl_info(ctrl, "Command was interrupted by a signal\n");
  248. }
  249. return retval;
  250. }
  251. static int shpc_write_cmd(struct slot *slot, u8 t_slot, u8 cmd)
  252. {
  253. struct controller *ctrl = slot->ctrl;
  254. u16 cmd_status;
  255. int retval = 0;
  256. u16 temp_word;
  257. mutex_lock(&slot->ctrl->cmd_lock);
  258. if (!shpc_poll_ctrl_busy(ctrl)) {
  259. /* After 1 sec and the controller is still busy */
  260. ctrl_err(ctrl, "Controller is still busy after 1 sec\n");
  261. retval = -EBUSY;
  262. goto out;
  263. }
  264. ++t_slot;
  265. temp_word = (t_slot << 8) | (cmd & 0xFF);
  266. ctrl_dbg(ctrl, "%s: t_slot %x cmd %x\n", __func__, t_slot, cmd);
  267. /* To make sure the Controller Busy bit is 0 before we send out the
  268. * command.
  269. */
  270. shpc_writew(ctrl, CMD, temp_word);
  271. /*
  272. * Wait for command completion.
  273. */
  274. retval = shpc_wait_cmd(slot->ctrl);
  275. if (retval)
  276. goto out;
  277. cmd_status = shpchp_check_cmd_status(slot->ctrl);
  278. if (cmd_status) {
  279. ctrl_err(ctrl, "Failed to issued command 0x%x (error code = %d)\n",
  280. cmd, cmd_status);
  281. retval = -EIO;
  282. }
  283. out:
  284. mutex_unlock(&slot->ctrl->cmd_lock);
  285. return retval;
  286. }
  287. int shpchp_check_cmd_status(struct controller *ctrl)
  288. {
  289. int retval = 0;
  290. u16 cmd_status = shpc_readw(ctrl, CMD_STATUS) & 0x000F;
  291. switch (cmd_status >> 1) {
  292. case 0:
  293. retval = 0;
  294. break;
  295. case 1:
  296. retval = SWITCH_OPEN;
  297. ctrl_err(ctrl, "Switch opened!\n");
  298. break;
  299. case 2:
  300. retval = INVALID_CMD;
  301. ctrl_err(ctrl, "Invalid HPC command!\n");
  302. break;
  303. case 4:
  304. retval = INVALID_SPEED_MODE;
  305. ctrl_err(ctrl, "Invalid bus speed/mode!\n");
  306. break;
  307. default:
  308. retval = cmd_status;
  309. }
  310. return retval;
  311. }
  312. int shpchp_get_attention_status(struct slot *slot, u8 *status)
  313. {
  314. struct controller *ctrl = slot->ctrl;
  315. u32 slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
  316. u8 state = (slot_reg & ATN_LED_STATE_MASK) >> ATN_LED_STATE_SHIFT;
  317. switch (state) {
  318. case ATN_LED_STATE_ON:
  319. *status = 1; /* On */
  320. break;
  321. case ATN_LED_STATE_BLINK:
  322. *status = 2; /* Blink */
  323. break;
  324. case ATN_LED_STATE_OFF:
  325. *status = 0; /* Off */
  326. break;
  327. default:
  328. *status = 0xFF; /* Reserved */
  329. break;
  330. }
  331. return 0;
  332. }
  333. int shpchp_get_power_status(struct slot *slot, u8 *status)
  334. {
  335. struct controller *ctrl = slot->ctrl;
  336. u32 slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
  337. u8 state = (slot_reg & SLOT_STATE_MASK) >> SLOT_STATE_SHIFT;
  338. switch (state) {
  339. case SLOT_STATE_PWRONLY:
  340. *status = 2; /* Powered only */
  341. break;
  342. case SLOT_STATE_ENABLED:
  343. *status = 1; /* Enabled */
  344. break;
  345. case SLOT_STATE_DISABLED:
  346. *status = 0; /* Disabled */
  347. break;
  348. default:
  349. *status = 0xFF; /* Reserved */
  350. break;
  351. }
  352. return 0;
  353. }
  354. int shpchp_get_latch_status(struct slot *slot, u8 *status)
  355. {
  356. struct controller *ctrl = slot->ctrl;
  357. u32 slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
  358. *status = !!(slot_reg & MRL_SENSOR); /* 0 -> close; 1 -> open */
  359. return 0;
  360. }
  361. int shpchp_get_adapter_status(struct slot *slot, u8 *status)
  362. {
  363. struct controller *ctrl = slot->ctrl;
  364. u32 slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
  365. u8 state = (slot_reg & PRSNT_MASK) >> PRSNT_SHIFT;
  366. *status = (state != 0x3) ? 1 : 0;
  367. return 0;
  368. }
  369. int shpchp_get_prog_int(struct slot *slot, u8 *prog_int)
  370. {
  371. struct controller *ctrl = slot->ctrl;
  372. *prog_int = shpc_readb(ctrl, PROG_INTERFACE);
  373. return 0;
  374. }
  375. int shpchp_get_adapter_speed(struct slot *slot, enum pci_bus_speed *value)
  376. {
  377. int retval = 0;
  378. struct controller *ctrl = slot->ctrl;
  379. u32 slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
  380. u8 m66_cap = !!(slot_reg & MHZ66_CAP);
  381. u8 pi, pcix_cap;
  382. retval = shpchp_get_prog_int(slot, &pi);
  383. if (retval)
  384. return retval;
  385. switch (pi) {
  386. case 1:
  387. pcix_cap = (slot_reg & PCIX_CAP_MASK_PI1) >> PCIX_CAP_SHIFT;
  388. break;
  389. case 2:
  390. pcix_cap = (slot_reg & PCIX_CAP_MASK_PI2) >> PCIX_CAP_SHIFT;
  391. break;
  392. default:
  393. return -ENODEV;
  394. }
  395. ctrl_dbg(ctrl, "%s: slot_reg = %x, pcix_cap = %x, m66_cap = %x\n",
  396. __func__, slot_reg, pcix_cap, m66_cap);
  397. switch (pcix_cap) {
  398. case 0x0:
  399. *value = m66_cap ? PCI_SPEED_66MHz : PCI_SPEED_33MHz;
  400. break;
  401. case 0x1:
  402. *value = PCI_SPEED_66MHz_PCIX;
  403. break;
  404. case 0x3:
  405. *value = PCI_SPEED_133MHz_PCIX;
  406. break;
  407. case 0x4:
  408. *value = PCI_SPEED_133MHz_PCIX_266;
  409. break;
  410. case 0x5:
  411. *value = PCI_SPEED_133MHz_PCIX_533;
  412. break;
  413. case 0x2:
  414. default:
  415. *value = PCI_SPEED_UNKNOWN;
  416. retval = -ENODEV;
  417. break;
  418. }
  419. ctrl_dbg(ctrl, "Adapter speed = %d\n", *value);
  420. return retval;
  421. }
  422. int shpchp_query_power_fault(struct slot *slot)
  423. {
  424. struct controller *ctrl = slot->ctrl;
  425. u32 slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
  426. /* Note: Logic 0 => fault */
  427. return !(slot_reg & POWER_FAULT);
  428. }
  429. int shpchp_set_attention_status(struct slot *slot, u8 value)
  430. {
  431. u8 slot_cmd = 0;
  432. switch (value) {
  433. case 0:
  434. slot_cmd = SET_ATTN_OFF; /* OFF */
  435. break;
  436. case 1:
  437. slot_cmd = SET_ATTN_ON; /* ON */
  438. break;
  439. case 2:
  440. slot_cmd = SET_ATTN_BLINK; /* BLINK */
  441. break;
  442. default:
  443. return -1;
  444. }
  445. return shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
  446. }
  447. void shpchp_green_led_on(struct slot *slot)
  448. {
  449. shpc_write_cmd(slot, slot->hp_slot, SET_PWR_ON);
  450. }
  451. void shpchp_green_led_off(struct slot *slot)
  452. {
  453. shpc_write_cmd(slot, slot->hp_slot, SET_PWR_OFF);
  454. }
  455. void shpchp_green_led_blink(struct slot *slot)
  456. {
  457. shpc_write_cmd(slot, slot->hp_slot, SET_PWR_BLINK);
  458. }
  459. void shpchp_release_ctlr(struct controller *ctrl)
  460. {
  461. int i;
  462. u32 slot_reg, serr_int;
  463. /*
  464. * Mask event interrupts and SERRs of all slots
  465. */
  466. for (i = 0; i < ctrl->num_slots; i++) {
  467. slot_reg = shpc_readl(ctrl, SLOT_REG(i));
  468. slot_reg |= (PRSNT_CHANGE_INTR_MASK | ISO_PFAULT_INTR_MASK |
  469. BUTTON_PRESS_INTR_MASK | MRL_CHANGE_INTR_MASK |
  470. CON_PFAULT_INTR_MASK | MRL_CHANGE_SERR_MASK |
  471. CON_PFAULT_SERR_MASK);
  472. slot_reg &= ~SLOT_REG_RSVDZ_MASK;
  473. shpc_writel(ctrl, SLOT_REG(i), slot_reg);
  474. }
  475. cleanup_slots(ctrl);
  476. /*
  477. * Mask SERR and System Interrupt generation
  478. */
  479. serr_int = shpc_readl(ctrl, SERR_INTR_ENABLE);
  480. serr_int |= (GLOBAL_INTR_MASK | GLOBAL_SERR_MASK |
  481. COMMAND_INTR_MASK | ARBITER_SERR_MASK);
  482. serr_int &= ~SERR_INTR_RSVDZ_MASK;
  483. shpc_writel(ctrl, SERR_INTR_ENABLE, serr_int);
  484. if (shpchp_poll_mode)
  485. timer_delete(&ctrl->poll_timer);
  486. else {
  487. free_irq(ctrl->pci_dev->irq, ctrl);
  488. pci_disable_msi(ctrl->pci_dev);
  489. }
  490. iounmap(ctrl->creg);
  491. release_mem_region(ctrl->mmio_base, ctrl->mmio_size);
  492. }
  493. int shpchp_power_on_slot(struct slot *slot)
  494. {
  495. int retval;
  496. retval = shpc_write_cmd(slot, slot->hp_slot, SET_SLOT_PWR);
  497. if (retval)
  498. ctrl_err(slot->ctrl, "%s: Write command failed!\n", __func__);
  499. return retval;
  500. }
  501. int shpchp_slot_enable(struct slot *slot)
  502. {
  503. int retval;
  504. /* Slot - Enable, Power Indicator - Blink, Attention Indicator - Off */
  505. retval = shpc_write_cmd(slot, slot->hp_slot,
  506. SET_SLOT_ENABLE | SET_PWR_BLINK | SET_ATTN_OFF);
  507. if (retval)
  508. ctrl_err(slot->ctrl, "%s: Write command failed!\n", __func__);
  509. return retval;
  510. }
  511. int shpchp_slot_disable(struct slot *slot)
  512. {
  513. int retval;
  514. /* Slot - Disable, Power Indicator - Off, Attention Indicator - On */
  515. retval = shpc_write_cmd(slot, slot->hp_slot,
  516. SET_SLOT_DISABLE | SET_PWR_OFF | SET_ATTN_ON);
  517. if (retval)
  518. ctrl_err(slot->ctrl, "%s: Write command failed!\n", __func__);
  519. return retval;
  520. }
  521. static int shpc_get_cur_bus_speed(struct controller *ctrl)
  522. {
  523. int retval = 0;
  524. struct pci_bus *bus = ctrl->pci_dev->subordinate;
  525. enum pci_bus_speed bus_speed = PCI_SPEED_UNKNOWN;
  526. u16 sec_bus_reg = shpc_readw(ctrl, SEC_BUS_CONFIG);
  527. u8 pi = shpc_readb(ctrl, PROG_INTERFACE);
  528. u8 speed_mode = (pi == 2) ? (sec_bus_reg & 0xF) : (sec_bus_reg & 0x7);
  529. if ((pi == 1) && (speed_mode > 4)) {
  530. retval = -ENODEV;
  531. goto out;
  532. }
  533. switch (speed_mode) {
  534. case 0x0:
  535. bus_speed = PCI_SPEED_33MHz;
  536. break;
  537. case 0x1:
  538. bus_speed = PCI_SPEED_66MHz;
  539. break;
  540. case 0x2:
  541. bus_speed = PCI_SPEED_66MHz_PCIX;
  542. break;
  543. case 0x3:
  544. bus_speed = PCI_SPEED_100MHz_PCIX;
  545. break;
  546. case 0x4:
  547. bus_speed = PCI_SPEED_133MHz_PCIX;
  548. break;
  549. case 0x5:
  550. bus_speed = PCI_SPEED_66MHz_PCIX_ECC;
  551. break;
  552. case 0x6:
  553. bus_speed = PCI_SPEED_100MHz_PCIX_ECC;
  554. break;
  555. case 0x7:
  556. bus_speed = PCI_SPEED_133MHz_PCIX_ECC;
  557. break;
  558. case 0x8:
  559. bus_speed = PCI_SPEED_66MHz_PCIX_266;
  560. break;
  561. case 0x9:
  562. bus_speed = PCI_SPEED_100MHz_PCIX_266;
  563. break;
  564. case 0xa:
  565. bus_speed = PCI_SPEED_133MHz_PCIX_266;
  566. break;
  567. case 0xb:
  568. bus_speed = PCI_SPEED_66MHz_PCIX_533;
  569. break;
  570. case 0xc:
  571. bus_speed = PCI_SPEED_100MHz_PCIX_533;
  572. break;
  573. case 0xd:
  574. bus_speed = PCI_SPEED_133MHz_PCIX_533;
  575. break;
  576. default:
  577. retval = -ENODEV;
  578. break;
  579. }
  580. out:
  581. bus->cur_bus_speed = bus_speed;
  582. ctrl_dbg(ctrl, "Current bus speed = %d\n", bus_speed);
  583. return retval;
  584. }
  585. int shpchp_set_bus_speed_mode(struct slot *slot, enum pci_bus_speed value)
  586. {
  587. int retval;
  588. struct controller *ctrl = slot->ctrl;
  589. u8 pi, cmd;
  590. pi = shpc_readb(ctrl, PROG_INTERFACE);
  591. if ((pi == 1) && (value > PCI_SPEED_133MHz_PCIX))
  592. return -EINVAL;
  593. switch (value) {
  594. case PCI_SPEED_33MHz:
  595. cmd = SETA_PCI_33MHZ;
  596. break;
  597. case PCI_SPEED_66MHz:
  598. cmd = SETA_PCI_66MHZ;
  599. break;
  600. case PCI_SPEED_66MHz_PCIX:
  601. cmd = SETA_PCIX_66MHZ;
  602. break;
  603. case PCI_SPEED_100MHz_PCIX:
  604. cmd = SETA_PCIX_100MHZ;
  605. break;
  606. case PCI_SPEED_133MHz_PCIX:
  607. cmd = SETA_PCIX_133MHZ;
  608. break;
  609. case PCI_SPEED_66MHz_PCIX_ECC:
  610. cmd = SETB_PCIX_66MHZ_EM;
  611. break;
  612. case PCI_SPEED_100MHz_PCIX_ECC:
  613. cmd = SETB_PCIX_100MHZ_EM;
  614. break;
  615. case PCI_SPEED_133MHz_PCIX_ECC:
  616. cmd = SETB_PCIX_133MHZ_EM;
  617. break;
  618. case PCI_SPEED_66MHz_PCIX_266:
  619. cmd = SETB_PCIX_66MHZ_266;
  620. break;
  621. case PCI_SPEED_100MHz_PCIX_266:
  622. cmd = SETB_PCIX_100MHZ_266;
  623. break;
  624. case PCI_SPEED_133MHz_PCIX_266:
  625. cmd = SETB_PCIX_133MHZ_266;
  626. break;
  627. case PCI_SPEED_66MHz_PCIX_533:
  628. cmd = SETB_PCIX_66MHZ_533;
  629. break;
  630. case PCI_SPEED_100MHz_PCIX_533:
  631. cmd = SETB_PCIX_100MHZ_533;
  632. break;
  633. case PCI_SPEED_133MHz_PCIX_533:
  634. cmd = SETB_PCIX_133MHZ_533;
  635. break;
  636. default:
  637. return -EINVAL;
  638. }
  639. retval = shpc_write_cmd(slot, 0, cmd);
  640. if (retval)
  641. ctrl_err(ctrl, "%s: Write command failed!\n", __func__);
  642. else
  643. shpc_get_cur_bus_speed(ctrl);
  644. return retval;
  645. }
  646. static irqreturn_t shpc_isr(int irq, void *dev_id)
  647. {
  648. struct controller *ctrl = (struct controller *)dev_id;
  649. u32 serr_int, slot_reg, intr_loc, intr_loc2;
  650. int hp_slot;
  651. /* Check to see if it was our interrupt */
  652. intr_loc = shpc_readl(ctrl, INTR_LOC);
  653. if (!intr_loc)
  654. return IRQ_NONE;
  655. ctrl_dbg(ctrl, "%s: intr_loc = %x\n", __func__, intr_loc);
  656. if (!shpchp_poll_mode) {
  657. /*
  658. * Mask Global Interrupt Mask - see implementation
  659. * note on p. 139 of SHPC spec rev 1.0
  660. */
  661. serr_int = shpc_readl(ctrl, SERR_INTR_ENABLE);
  662. serr_int |= GLOBAL_INTR_MASK;
  663. serr_int &= ~SERR_INTR_RSVDZ_MASK;
  664. shpc_writel(ctrl, SERR_INTR_ENABLE, serr_int);
  665. intr_loc2 = shpc_readl(ctrl, INTR_LOC);
  666. ctrl_dbg(ctrl, "%s: intr_loc2 = %x\n", __func__, intr_loc2);
  667. }
  668. if (intr_loc & CMD_INTR_PENDING) {
  669. /*
  670. * Command Complete Interrupt Pending
  671. * RO only - clear by writing 1 to the Command Completion
  672. * Detect bit in Controller SERR-INT register
  673. */
  674. serr_int = shpc_readl(ctrl, SERR_INTR_ENABLE);
  675. serr_int &= ~SERR_INTR_RSVDZ_MASK;
  676. shpc_writel(ctrl, SERR_INTR_ENABLE, serr_int);
  677. wake_up_interruptible(&ctrl->queue);
  678. }
  679. if (!(intr_loc & ~CMD_INTR_PENDING))
  680. goto out;
  681. for (hp_slot = 0; hp_slot < ctrl->num_slots; hp_slot++) {
  682. /* To find out which slot has interrupt pending */
  683. if (!(intr_loc & SLOT_INTR_PENDING(hp_slot)))
  684. continue;
  685. slot_reg = shpc_readl(ctrl, SLOT_REG(hp_slot));
  686. ctrl_dbg(ctrl, "Slot %x with intr, slot register = %x\n",
  687. hp_slot, slot_reg);
  688. if (slot_reg & MRL_CHANGE_DETECTED)
  689. shpchp_handle_switch_change(hp_slot, ctrl);
  690. if (slot_reg & BUTTON_PRESS_DETECTED)
  691. shpchp_handle_attention_button(hp_slot, ctrl);
  692. if (slot_reg & PRSNT_CHANGE_DETECTED)
  693. shpchp_handle_presence_change(hp_slot, ctrl);
  694. if (slot_reg & (ISO_PFAULT_DETECTED | CON_PFAULT_DETECTED))
  695. shpchp_handle_power_fault(hp_slot, ctrl);
  696. /* Clear all slot events */
  697. slot_reg &= ~SLOT_REG_RSVDZ_MASK;
  698. shpc_writel(ctrl, SLOT_REG(hp_slot), slot_reg);
  699. }
  700. out:
  701. if (!shpchp_poll_mode) {
  702. /* Unmask Global Interrupt Mask */
  703. serr_int = shpc_readl(ctrl, SERR_INTR_ENABLE);
  704. serr_int &= ~(GLOBAL_INTR_MASK | SERR_INTR_RSVDZ_MASK);
  705. shpc_writel(ctrl, SERR_INTR_ENABLE, serr_int);
  706. }
  707. return IRQ_HANDLED;
  708. }
  709. static int shpc_get_max_bus_speed(struct controller *ctrl)
  710. {
  711. int retval = 0;
  712. struct pci_bus *bus = ctrl->pci_dev->subordinate;
  713. enum pci_bus_speed bus_speed = PCI_SPEED_UNKNOWN;
  714. u8 pi = shpc_readb(ctrl, PROG_INTERFACE);
  715. u32 slot_avail1 = shpc_readl(ctrl, SLOT_AVAIL1);
  716. u32 slot_avail2 = shpc_readl(ctrl, SLOT_AVAIL2);
  717. if (pi == 2) {
  718. if (slot_avail2 & SLOT_133MHZ_PCIX_533)
  719. bus_speed = PCI_SPEED_133MHz_PCIX_533;
  720. else if (slot_avail2 & SLOT_100MHZ_PCIX_533)
  721. bus_speed = PCI_SPEED_100MHz_PCIX_533;
  722. else if (slot_avail2 & SLOT_66MHZ_PCIX_533)
  723. bus_speed = PCI_SPEED_66MHz_PCIX_533;
  724. else if (slot_avail2 & SLOT_133MHZ_PCIX_266)
  725. bus_speed = PCI_SPEED_133MHz_PCIX_266;
  726. else if (slot_avail2 & SLOT_100MHZ_PCIX_266)
  727. bus_speed = PCI_SPEED_100MHz_PCIX_266;
  728. else if (slot_avail2 & SLOT_66MHZ_PCIX_266)
  729. bus_speed = PCI_SPEED_66MHz_PCIX_266;
  730. }
  731. if (bus_speed == PCI_SPEED_UNKNOWN) {
  732. if (slot_avail1 & SLOT_133MHZ_PCIX)
  733. bus_speed = PCI_SPEED_133MHz_PCIX;
  734. else if (slot_avail1 & SLOT_100MHZ_PCIX)
  735. bus_speed = PCI_SPEED_100MHz_PCIX;
  736. else if (slot_avail1 & SLOT_66MHZ_PCIX)
  737. bus_speed = PCI_SPEED_66MHz_PCIX;
  738. else if (slot_avail2 & SLOT_66MHZ)
  739. bus_speed = PCI_SPEED_66MHz;
  740. else if (slot_avail1 & SLOT_33MHZ)
  741. bus_speed = PCI_SPEED_33MHz;
  742. else
  743. retval = -ENODEV;
  744. }
  745. bus->max_bus_speed = bus_speed;
  746. ctrl_dbg(ctrl, "Max bus speed = %d\n", bus_speed);
  747. return retval;
  748. }
  749. int shpc_init(struct controller *ctrl, struct pci_dev *pdev)
  750. {
  751. int rc = -1, num_slots = 0;
  752. u8 hp_slot;
  753. u32 shpc_base_offset;
  754. u32 tempdword, slot_reg, slot_config;
  755. u8 i;
  756. ctrl->pci_dev = pdev; /* pci_dev of the P2P bridge */
  757. ctrl_dbg(ctrl, "Hotplug Controller:\n");
  758. if (pdev->vendor == PCI_VENDOR_ID_AMD &&
  759. pdev->device == PCI_DEVICE_ID_AMD_GOLAM_7450) {
  760. /* amd shpc driver doesn't use Base Offset; assume 0 */
  761. ctrl->mmio_base = pci_resource_start(pdev, 0);
  762. ctrl->mmio_size = pci_resource_len(pdev, 0);
  763. } else {
  764. ctrl->cap_offset = pci_find_capability(pdev, PCI_CAP_ID_SHPC);
  765. if (!ctrl->cap_offset) {
  766. ctrl_err(ctrl, "Cannot find PCI capability\n");
  767. goto abort;
  768. }
  769. ctrl_dbg(ctrl, " cap_offset = %x\n", ctrl->cap_offset);
  770. rc = shpc_indirect_read(ctrl, 0, &shpc_base_offset);
  771. if (rc) {
  772. ctrl_err(ctrl, "Cannot read base_offset\n");
  773. goto abort;
  774. }
  775. rc = shpc_indirect_read(ctrl, 3, &tempdword);
  776. if (rc) {
  777. ctrl_err(ctrl, "Cannot read slot config\n");
  778. goto abort;
  779. }
  780. num_slots = tempdword & SLOT_NUM;
  781. ctrl_dbg(ctrl, " num_slots (indirect) %x\n", num_slots);
  782. for (i = 0; i < 9 + num_slots; i++) {
  783. rc = shpc_indirect_read(ctrl, i, &tempdword);
  784. if (rc) {
  785. ctrl_err(ctrl, "Cannot read creg (index = %d)\n",
  786. i);
  787. goto abort;
  788. }
  789. ctrl_dbg(ctrl, " offset %d: value %x\n", i, tempdword);
  790. }
  791. ctrl->mmio_base =
  792. pci_resource_start(pdev, 0) + shpc_base_offset;
  793. ctrl->mmio_size = 0x24 + 0x4 * num_slots;
  794. }
  795. ctrl_info(ctrl, "HPC vendor_id %x device_id %x ss_vid %x ss_did %x\n",
  796. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  797. pdev->subsystem_device);
  798. rc = pci_enable_device(pdev);
  799. if (rc) {
  800. ctrl_err(ctrl, "pci_enable_device failed\n");
  801. goto abort;
  802. }
  803. if (!request_mem_region(ctrl->mmio_base, ctrl->mmio_size, MY_NAME)) {
  804. ctrl_err(ctrl, "Cannot reserve MMIO region\n");
  805. rc = -1;
  806. goto abort;
  807. }
  808. ctrl->creg = ioremap(ctrl->mmio_base, ctrl->mmio_size);
  809. if (!ctrl->creg) {
  810. ctrl_err(ctrl, "Cannot remap MMIO region %lx @ %lx\n",
  811. ctrl->mmio_size, ctrl->mmio_base);
  812. release_mem_region(ctrl->mmio_base, ctrl->mmio_size);
  813. rc = -1;
  814. goto abort;
  815. }
  816. ctrl_dbg(ctrl, "ctrl->creg %p\n", ctrl->creg);
  817. mutex_init(&ctrl->crit_sect);
  818. mutex_init(&ctrl->cmd_lock);
  819. /* Setup wait queue */
  820. init_waitqueue_head(&ctrl->queue);
  821. /* Return PCI Controller Info */
  822. slot_config = shpc_readl(ctrl, SLOT_CONFIG);
  823. ctrl->slot_device_offset = (slot_config & FIRST_DEV_NUM) >> 8;
  824. ctrl->num_slots = slot_config & SLOT_NUM;
  825. ctrl->first_slot = (slot_config & PSN) >> 16;
  826. ctrl->slot_num_inc = ((slot_config & UPDOWN) >> 29) ? 1 : -1;
  827. /* Mask Global Interrupt Mask & Command Complete Interrupt Mask */
  828. tempdword = shpc_readl(ctrl, SERR_INTR_ENABLE);
  829. ctrl_dbg(ctrl, "SERR_INTR_ENABLE = %x\n", tempdword);
  830. tempdword |= (GLOBAL_INTR_MASK | GLOBAL_SERR_MASK |
  831. COMMAND_INTR_MASK | ARBITER_SERR_MASK);
  832. tempdword &= ~SERR_INTR_RSVDZ_MASK;
  833. shpc_writel(ctrl, SERR_INTR_ENABLE, tempdword);
  834. tempdword = shpc_readl(ctrl, SERR_INTR_ENABLE);
  835. ctrl_dbg(ctrl, "SERR_INTR_ENABLE = %x\n", tempdword);
  836. /* Mask the MRL sensor SERR Mask of individual slot in
  837. * Slot SERR-INT Mask & clear all the existing event if any
  838. */
  839. for (hp_slot = 0; hp_slot < ctrl->num_slots; hp_slot++) {
  840. slot_reg = shpc_readl(ctrl, SLOT_REG(hp_slot));
  841. ctrl_dbg(ctrl, "Default Logical Slot Register %d value %x\n",
  842. hp_slot, slot_reg);
  843. slot_reg |= (PRSNT_CHANGE_INTR_MASK | ISO_PFAULT_INTR_MASK |
  844. BUTTON_PRESS_INTR_MASK | MRL_CHANGE_INTR_MASK |
  845. CON_PFAULT_INTR_MASK | MRL_CHANGE_SERR_MASK |
  846. CON_PFAULT_SERR_MASK);
  847. slot_reg &= ~SLOT_REG_RSVDZ_MASK;
  848. shpc_writel(ctrl, SLOT_REG(hp_slot), slot_reg);
  849. }
  850. if (shpchp_poll_mode) {
  851. /* Install interrupt polling timer. Start with 10 sec delay */
  852. timer_setup(&ctrl->poll_timer, int_poll_timeout, 0);
  853. start_int_poll_timer(ctrl, 10);
  854. } else {
  855. /* Installs the interrupt handler */
  856. rc = pci_enable_msi(pdev);
  857. if (rc) {
  858. ctrl_info(ctrl, "Can't get msi for the hotplug controller\n");
  859. ctrl_info(ctrl, "Use INTx for the hotplug controller\n");
  860. } else {
  861. pci_set_master(pdev);
  862. }
  863. rc = request_irq(ctrl->pci_dev->irq, shpc_isr, IRQF_SHARED,
  864. MY_NAME, (void *)ctrl);
  865. ctrl_dbg(ctrl, "request_irq %d (returns %d)\n",
  866. ctrl->pci_dev->irq, rc);
  867. if (rc) {
  868. ctrl_err(ctrl, "Can't get irq %d for the hotplug controller\n",
  869. ctrl->pci_dev->irq);
  870. goto abort_iounmap;
  871. }
  872. }
  873. ctrl_dbg(ctrl, "HPC at %s irq=%x\n", pci_name(pdev), pdev->irq);
  874. shpc_get_max_bus_speed(ctrl);
  875. shpc_get_cur_bus_speed(ctrl);
  876. /*
  877. * Unmask all event interrupts of all slots
  878. */
  879. for (hp_slot = 0; hp_slot < ctrl->num_slots; hp_slot++) {
  880. slot_reg = shpc_readl(ctrl, SLOT_REG(hp_slot));
  881. ctrl_dbg(ctrl, "Default Logical Slot Register %d value %x\n",
  882. hp_slot, slot_reg);
  883. slot_reg &= ~(PRSNT_CHANGE_INTR_MASK | ISO_PFAULT_INTR_MASK |
  884. BUTTON_PRESS_INTR_MASK | MRL_CHANGE_INTR_MASK |
  885. CON_PFAULT_INTR_MASK | SLOT_REG_RSVDZ_MASK);
  886. shpc_writel(ctrl, SLOT_REG(hp_slot), slot_reg);
  887. }
  888. if (!shpchp_poll_mode) {
  889. /* Unmask all general input interrupts and SERR */
  890. tempdword = shpc_readl(ctrl, SERR_INTR_ENABLE);
  891. tempdword &= ~(GLOBAL_INTR_MASK | COMMAND_INTR_MASK |
  892. SERR_INTR_RSVDZ_MASK);
  893. shpc_writel(ctrl, SERR_INTR_ENABLE, tempdword);
  894. tempdword = shpc_readl(ctrl, SERR_INTR_ENABLE);
  895. ctrl_dbg(ctrl, "SERR_INTR_ENABLE = %x\n", tempdword);
  896. }
  897. return 0;
  898. /* We end up here for the many possible ways to fail this API. */
  899. abort_iounmap:
  900. iounmap(ctrl->creg);
  901. abort:
  902. return rc;
  903. }