shpchp_hpc.c 27 KB

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