shpchp_hpc.c 28 KB

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