shpchp_hpc.c 34 KB

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