shpchp_hpc.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470
  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. * Logical Slot Register definitions
  85. */
  86. #define SLOT_REG(i) (SLOT1 + (4 * i))
  87. #define SLOT_STATE_SHIFT (0)
  88. #define SLOT_STATE_MASK (3 << 0)
  89. #define SLOT_STATE_PWRONLY (1)
  90. #define SLOT_STATE_ENABLED (2)
  91. #define SLOT_STATE_DISABLED (3)
  92. #define PWR_LED_STATE_SHIFT (2)
  93. #define PWR_LED_STATE_MASK (3 << 2)
  94. #define ATN_LED_STATE_SHIFT (4)
  95. #define ATN_LED_STATE_MASK (3 << 4)
  96. #define ATN_LED_STATE_ON (1)
  97. #define ATN_LED_STATE_BLINK (2)
  98. #define ATN_LED_STATE_OFF (3)
  99. #define POWER_FAULT (1 << 6)
  100. #define ATN_BUTTON (1 << 7)
  101. #define MRL_SENSOR (1 << 8)
  102. #define MHZ66_CAP (1 << 9)
  103. #define PRSNT_SHIFT (10)
  104. #define PRSNT_MASK (3 << 10)
  105. #define PCIX_CAP_SHIFT (12)
  106. #define PCIX_CAP_MASK_PI1 (3 << 12)
  107. #define PCIX_CAP_MASK_PI2 (7 << 12)
  108. #define PRSNT_CHANGE_DETECTED (1 << 16)
  109. #define ISO_PFAULT_DETECTED (1 << 17)
  110. #define BUTTON_PRESS_DETECTED (1 << 18)
  111. #define MRL_CHANGE_DETECTED (1 << 19)
  112. #define CON_PFAULT_DETECTED (1 << 20)
  113. #define PRSNT_CHANGE_INTR_MASK (1 << 24)
  114. #define ISO_PFAULT_INTR_MASK (1 << 25)
  115. #define BUTTON_PRESS_INTR_MASK (1 << 26)
  116. #define MRL_CHANGE_INTR_MASK (1 << 27)
  117. #define CON_PFAULT_INTR_MASK (1 << 28)
  118. #define MRL_CHANGE_SERR_MASK (1 << 29)
  119. #define CON_PFAULT_SERR_MASK (1 << 30)
  120. #define SLOT_REG_RSVDZ_MASK (1 << 15) | (7 << 21)
  121. /* SHPC 'write' operations/commands */
  122. /* Slot operation - 0x00h to 0x3Fh */
  123. #define NO_CHANGE 0x00
  124. /* Slot state - Bits 0 & 1 of controller command register */
  125. #define SET_SLOT_PWR 0x01
  126. #define SET_SLOT_ENABLE 0x02
  127. #define SET_SLOT_DISABLE 0x03
  128. /* Power indicator state - Bits 2 & 3 of controller command register*/
  129. #define SET_PWR_ON 0x04
  130. #define SET_PWR_BLINK 0x08
  131. #define SET_PWR_OFF 0x0C
  132. /* Attention indicator state - Bits 4 & 5 of controller command register*/
  133. #define SET_ATTN_ON 0x010
  134. #define SET_ATTN_BLINK 0x020
  135. #define SET_ATTN_OFF 0x030
  136. /* Set bus speed/mode A - 0x40h to 0x47h */
  137. #define SETA_PCI_33MHZ 0x40
  138. #define SETA_PCI_66MHZ 0x41
  139. #define SETA_PCIX_66MHZ 0x42
  140. #define SETA_PCIX_100MHZ 0x43
  141. #define SETA_PCIX_133MHZ 0x44
  142. #define RESERV_1 0x45
  143. #define RESERV_2 0x46
  144. #define RESERV_3 0x47
  145. /* Set bus speed/mode B - 0x50h to 0x5fh */
  146. #define SETB_PCI_33MHZ 0x50
  147. #define SETB_PCI_66MHZ 0x51
  148. #define SETB_PCIX_66MHZ_PM 0x52
  149. #define SETB_PCIX_100MHZ_PM 0x53
  150. #define SETB_PCIX_133MHZ_PM 0x54
  151. #define SETB_PCIX_66MHZ_EM 0x55
  152. #define SETB_PCIX_100MHZ_EM 0x56
  153. #define SETB_PCIX_133MHZ_EM 0x57
  154. #define SETB_PCIX_66MHZ_266 0x58
  155. #define SETB_PCIX_100MHZ_266 0x59
  156. #define SETB_PCIX_133MHZ_266 0x5a
  157. #define SETB_PCIX_66MHZ_533 0x5b
  158. #define SETB_PCIX_100MHZ_533 0x5c
  159. #define SETB_PCIX_133MHZ_533 0x5d
  160. /* Power-on all slots - 0x48h */
  161. #define SET_PWR_ON_ALL 0x48
  162. /* Enable all slots - 0x49h */
  163. #define SET_ENABLE_ALL 0x49
  164. /* SHPC controller command error code */
  165. #define SWITCH_OPEN 0x1
  166. #define INVALID_CMD 0x2
  167. #define INVALID_SPEED_MODE 0x4
  168. /* For accessing SHPC Working Register Set */
  169. #define DWORD_SELECT 0x2
  170. #define DWORD_DATA 0x4
  171. #define BASE_OFFSET 0x0
  172. /* Field Offset in Logical Slot Register - byte boundary */
  173. #define SLOT_EVENT_LATCH 0x2
  174. #define SLOT_SERR_INT_MASK 0x3
  175. static spinlock_t hpc_event_lock;
  176. DEFINE_DBG_BUFFER /* Debug string buffer for entire HPC defined here */
  177. static struct php_ctlr_state_s *php_ctlr_list_head; /* HPC state linked list */
  178. static int ctlr_seq_num = 0; /* Controller sequenc # */
  179. static spinlock_t list_lock;
  180. static irqreturn_t shpc_isr(int IRQ, void *dev_id, struct pt_regs *regs);
  181. static void start_int_poll_timer(struct php_ctlr_state_s *php_ctlr, int seconds);
  182. static int hpc_check_cmd_status(struct controller *ctrl);
  183. static inline u8 shpc_readb(struct controller *ctrl, int reg)
  184. {
  185. return readb(ctrl->hpc_ctlr_handle->creg + reg);
  186. }
  187. static inline void shpc_writeb(struct controller *ctrl, int reg, u8 val)
  188. {
  189. writeb(val, ctrl->hpc_ctlr_handle->creg + reg);
  190. }
  191. static inline u16 shpc_readw(struct controller *ctrl, int reg)
  192. {
  193. return readw(ctrl->hpc_ctlr_handle->creg + reg);
  194. }
  195. static inline void shpc_writew(struct controller *ctrl, int reg, u16 val)
  196. {
  197. writew(val, ctrl->hpc_ctlr_handle->creg + reg);
  198. }
  199. static inline u32 shpc_readl(struct controller *ctrl, int reg)
  200. {
  201. return readl(ctrl->hpc_ctlr_handle->creg + reg);
  202. }
  203. static inline void shpc_writel(struct controller *ctrl, int reg, u32 val)
  204. {
  205. writel(val, ctrl->hpc_ctlr_handle->creg + reg);
  206. }
  207. static inline int shpc_indirect_read(struct controller *ctrl, int index,
  208. u32 *value)
  209. {
  210. int rc;
  211. u32 cap_offset = ctrl->cap_offset;
  212. struct pci_dev *pdev = ctrl->pci_dev;
  213. rc = pci_write_config_byte(pdev, cap_offset + DWORD_SELECT, index);
  214. if (rc)
  215. return rc;
  216. return pci_read_config_dword(pdev, cap_offset + DWORD_DATA, value);
  217. }
  218. /* This is the interrupt polling timeout function. */
  219. static void int_poll_timeout(unsigned long lphp_ctlr)
  220. {
  221. struct php_ctlr_state_s *php_ctlr = (struct php_ctlr_state_s *)lphp_ctlr;
  222. DBG_ENTER_ROUTINE
  223. if ( !php_ctlr ) {
  224. err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
  225. return;
  226. }
  227. /* Poll for interrupt events. regs == NULL => polling */
  228. shpc_isr( 0, (void *)php_ctlr, NULL );
  229. init_timer(&php_ctlr->int_poll_timer);
  230. if (!shpchp_poll_time)
  231. shpchp_poll_time = 2; /* reset timer to poll in 2 secs if user doesn't specify at module installation*/
  232. start_int_poll_timer(php_ctlr, shpchp_poll_time);
  233. return;
  234. }
  235. /* This function starts the interrupt polling timer. */
  236. static void start_int_poll_timer(struct php_ctlr_state_s *php_ctlr, int seconds)
  237. {
  238. if (!php_ctlr) {
  239. err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
  240. return;
  241. }
  242. if ( ( seconds <= 0 ) || ( seconds > 60 ) )
  243. seconds = 2; /* Clamp to sane value */
  244. php_ctlr->int_poll_timer.function = &int_poll_timeout;
  245. php_ctlr->int_poll_timer.data = (unsigned long)php_ctlr; /* Instance data */
  246. php_ctlr->int_poll_timer.expires = jiffies + seconds * HZ;
  247. add_timer(&php_ctlr->int_poll_timer);
  248. return;
  249. }
  250. static inline int shpc_wait_cmd(struct controller *ctrl)
  251. {
  252. int retval = 0;
  253. unsigned int timeout_msec = shpchp_poll_mode ? 2000 : 1000;
  254. unsigned long timeout = msecs_to_jiffies(timeout_msec);
  255. int rc = wait_event_interruptible_timeout(ctrl->queue,
  256. !ctrl->cmd_busy, timeout);
  257. if (!rc) {
  258. retval = -EIO;
  259. err("Command not completed in %d msec\n", timeout_msec);
  260. } else if (rc < 0) {
  261. retval = -EINTR;
  262. info("Command was interrupted by a signal\n");
  263. }
  264. ctrl->cmd_busy = 0;
  265. return retval;
  266. }
  267. static int shpc_write_cmd(struct slot *slot, u8 t_slot, u8 cmd)
  268. {
  269. struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
  270. struct controller *ctrl = slot->ctrl;
  271. u16 cmd_status;
  272. int retval = 0;
  273. u16 temp_word;
  274. int i;
  275. DBG_ENTER_ROUTINE
  276. mutex_lock(&slot->ctrl->cmd_lock);
  277. if (!php_ctlr) {
  278. err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
  279. retval = -EINVAL;
  280. goto out;
  281. }
  282. for (i = 0; i < 10; i++) {
  283. cmd_status = shpc_readw(ctrl, CMD_STATUS);
  284. if (!(cmd_status & 0x1))
  285. break;
  286. /* Check every 0.1 sec for a total of 1 sec*/
  287. msleep(100);
  288. }
  289. cmd_status = shpc_readw(ctrl, CMD_STATUS);
  290. if (cmd_status & 0x1) {
  291. /* After 1 sec and and the controller is still busy */
  292. err("%s : Controller is still busy after 1 sec.\n", __FUNCTION__);
  293. retval = -EBUSY;
  294. goto out;
  295. }
  296. ++t_slot;
  297. temp_word = (t_slot << 8) | (cmd & 0xFF);
  298. dbg("%s: t_slot %x cmd %x\n", __FUNCTION__, t_slot, cmd);
  299. /* To make sure the Controller Busy bit is 0 before we send out the
  300. * command.
  301. */
  302. slot->ctrl->cmd_busy = 1;
  303. shpc_writew(ctrl, CMD, temp_word);
  304. /*
  305. * Wait for command completion.
  306. */
  307. retval = shpc_wait_cmd(slot->ctrl);
  308. if (retval)
  309. goto out;
  310. cmd_status = hpc_check_cmd_status(slot->ctrl);
  311. if (cmd_status) {
  312. err("%s: Failed to issued command 0x%x (error code = %d)\n",
  313. __FUNCTION__, cmd, cmd_status);
  314. retval = -EIO;
  315. }
  316. out:
  317. mutex_unlock(&slot->ctrl->cmd_lock);
  318. DBG_LEAVE_ROUTINE
  319. return retval;
  320. }
  321. static int hpc_check_cmd_status(struct controller *ctrl)
  322. {
  323. u16 cmd_status;
  324. int retval = 0;
  325. DBG_ENTER_ROUTINE
  326. if (!ctrl->hpc_ctlr_handle) {
  327. err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
  328. return -1;
  329. }
  330. cmd_status = shpc_readw(ctrl, CMD_STATUS) & 0x000F;
  331. switch (cmd_status >> 1) {
  332. case 0:
  333. retval = 0;
  334. break;
  335. case 1:
  336. retval = SWITCH_OPEN;
  337. err("%s: Switch opened!\n", __FUNCTION__);
  338. break;
  339. case 2:
  340. retval = INVALID_CMD;
  341. err("%s: Invalid HPC command!\n", __FUNCTION__);
  342. break;
  343. case 4:
  344. retval = INVALID_SPEED_MODE;
  345. err("%s: Invalid bus speed/mode!\n", __FUNCTION__);
  346. break;
  347. default:
  348. retval = cmd_status;
  349. }
  350. DBG_LEAVE_ROUTINE
  351. return retval;
  352. }
  353. static int hpc_get_attention_status(struct slot *slot, u8 *status)
  354. {
  355. struct controller *ctrl = slot->ctrl;
  356. u32 slot_reg;
  357. u8 state;
  358. DBG_ENTER_ROUTINE
  359. if (!slot->ctrl->hpc_ctlr_handle) {
  360. err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
  361. return -1;
  362. }
  363. slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
  364. state = (slot_reg & ATN_LED_STATE_MASK) >> ATN_LED_STATE_SHIFT;
  365. switch (state) {
  366. case ATN_LED_STATE_ON:
  367. *status = 1; /* On */
  368. break;
  369. case ATN_LED_STATE_BLINK:
  370. *status = 2; /* Blink */
  371. break;
  372. case ATN_LED_STATE_OFF:
  373. *status = 0; /* Off */
  374. break;
  375. default:
  376. *status = 0xFF; /* Reserved */
  377. break;
  378. }
  379. DBG_LEAVE_ROUTINE
  380. return 0;
  381. }
  382. static int hpc_get_power_status(struct slot * slot, u8 *status)
  383. {
  384. struct controller *ctrl = slot->ctrl;
  385. u32 slot_reg;
  386. u8 state;
  387. DBG_ENTER_ROUTINE
  388. if (!slot->ctrl->hpc_ctlr_handle) {
  389. err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
  390. return -1;
  391. }
  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. if (!slot->ctrl->hpc_ctlr_handle) {
  417. err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
  418. return -1;
  419. }
  420. slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
  421. *status = !!(slot_reg & MRL_SENSOR); /* 0 -> close; 1 -> open */
  422. DBG_LEAVE_ROUTINE
  423. return 0;
  424. }
  425. static int hpc_get_adapter_status(struct slot *slot, u8 *status)
  426. {
  427. struct controller *ctrl = slot->ctrl;
  428. u32 slot_reg;
  429. u8 state;
  430. DBG_ENTER_ROUTINE
  431. if (!slot->ctrl->hpc_ctlr_handle) {
  432. err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
  433. return -1;
  434. }
  435. slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
  436. state = (slot_reg & PRSNT_MASK) >> PRSNT_SHIFT;
  437. *status = (state != 0x3) ? 1 : 0;
  438. DBG_LEAVE_ROUTINE
  439. return 0;
  440. }
  441. static int hpc_get_prog_int(struct slot *slot, u8 *prog_int)
  442. {
  443. struct controller *ctrl = slot->ctrl;
  444. DBG_ENTER_ROUTINE
  445. if (!slot->ctrl->hpc_ctlr_handle) {
  446. err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
  447. return -1;
  448. }
  449. *prog_int = shpc_readb(ctrl, PROG_INTERFACE);
  450. DBG_LEAVE_ROUTINE
  451. return 0;
  452. }
  453. static int hpc_get_adapter_speed(struct slot *slot, enum pci_bus_speed *value)
  454. {
  455. int retval = 0;
  456. struct controller *ctrl = slot->ctrl;
  457. u32 slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
  458. u8 m66_cap = !!(slot_reg & MHZ66_CAP);
  459. u8 pi, pcix_cap;
  460. DBG_ENTER_ROUTINE
  461. if ((retval = hpc_get_prog_int(slot, &pi)))
  462. return retval;
  463. switch (pi) {
  464. case 1:
  465. pcix_cap = (slot_reg & PCIX_CAP_MASK_PI1) >> PCIX_CAP_SHIFT;
  466. break;
  467. case 2:
  468. pcix_cap = (slot_reg & PCIX_CAP_MASK_PI2) >> PCIX_CAP_SHIFT;
  469. break;
  470. default:
  471. return -ENODEV;
  472. }
  473. dbg("%s: slot_reg = %x, pcix_cap = %x, m66_cap = %x\n",
  474. __FUNCTION__, slot_reg, pcix_cap, m66_cap);
  475. switch (pcix_cap) {
  476. case 0x0:
  477. *value = m66_cap ? PCI_SPEED_66MHz : PCI_SPEED_33MHz;
  478. break;
  479. case 0x1:
  480. *value = PCI_SPEED_66MHz_PCIX;
  481. break;
  482. case 0x3:
  483. *value = PCI_SPEED_133MHz_PCIX;
  484. break;
  485. case 0x4:
  486. *value = PCI_SPEED_133MHz_PCIX_266;
  487. break;
  488. case 0x5:
  489. *value = PCI_SPEED_133MHz_PCIX_533;
  490. break;
  491. case 0x2:
  492. default:
  493. *value = PCI_SPEED_UNKNOWN;
  494. retval = -ENODEV;
  495. break;
  496. }
  497. dbg("Adapter speed = %d\n", *value);
  498. DBG_LEAVE_ROUTINE
  499. return retval;
  500. }
  501. static int hpc_get_mode1_ECC_cap(struct slot *slot, u8 *mode)
  502. {
  503. struct controller *ctrl = slot->ctrl;
  504. u16 sec_bus_status;
  505. u8 pi;
  506. int retval = 0;
  507. DBG_ENTER_ROUTINE
  508. if (!slot->ctrl->hpc_ctlr_handle) {
  509. err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
  510. return -1;
  511. }
  512. pi = shpc_readb(ctrl, PROG_INTERFACE);
  513. sec_bus_status = shpc_readw(ctrl, SEC_BUS_CONFIG);
  514. if (pi == 2) {
  515. *mode = (sec_bus_status & 0x0100) >> 8;
  516. } else {
  517. retval = -1;
  518. }
  519. dbg("Mode 1 ECC cap = %d\n", *mode);
  520. DBG_LEAVE_ROUTINE
  521. return retval;
  522. }
  523. static int hpc_query_power_fault(struct slot * slot)
  524. {
  525. struct controller *ctrl = slot->ctrl;
  526. u32 slot_reg;
  527. DBG_ENTER_ROUTINE
  528. if (!slot->ctrl->hpc_ctlr_handle) {
  529. err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
  530. return -1;
  531. }
  532. slot_reg = shpc_readl(ctrl, SLOT_REG(slot->hp_slot));
  533. DBG_LEAVE_ROUTINE
  534. /* Note: Logic 0 => fault */
  535. return !(slot_reg & POWER_FAULT);
  536. }
  537. static int hpc_set_attention_status(struct slot *slot, u8 value)
  538. {
  539. struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
  540. u8 slot_cmd = 0;
  541. int rc = 0;
  542. if (!slot->ctrl->hpc_ctlr_handle) {
  543. err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
  544. return -1;
  545. }
  546. if (slot->hp_slot >= php_ctlr->num_slots) {
  547. err("%s: Invalid HPC slot number!\n", __FUNCTION__);
  548. return -1;
  549. }
  550. switch (value) {
  551. case 0 :
  552. slot_cmd = 0x30; /* OFF */
  553. break;
  554. case 1:
  555. slot_cmd = 0x10; /* ON */
  556. break;
  557. case 2:
  558. slot_cmd = 0x20; /* BLINK */
  559. break;
  560. default:
  561. return -1;
  562. }
  563. shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
  564. return rc;
  565. }
  566. static void hpc_set_green_led_on(struct slot *slot)
  567. {
  568. struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
  569. u8 slot_cmd;
  570. if (!slot->ctrl->hpc_ctlr_handle) {
  571. err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
  572. return ;
  573. }
  574. if (slot->hp_slot >= php_ctlr->num_slots) {
  575. err("%s: Invalid HPC slot number!\n", __FUNCTION__);
  576. return ;
  577. }
  578. slot_cmd = 0x04;
  579. shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
  580. return;
  581. }
  582. static void hpc_set_green_led_off(struct slot *slot)
  583. {
  584. struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
  585. u8 slot_cmd;
  586. if (!slot->ctrl->hpc_ctlr_handle) {
  587. err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
  588. return ;
  589. }
  590. if (slot->hp_slot >= php_ctlr->num_slots) {
  591. err("%s: Invalid HPC slot number!\n", __FUNCTION__);
  592. return ;
  593. }
  594. slot_cmd = 0x0C;
  595. shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
  596. return;
  597. }
  598. static void hpc_set_green_led_blink(struct slot *slot)
  599. {
  600. struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
  601. u8 slot_cmd;
  602. if (!slot->ctrl->hpc_ctlr_handle) {
  603. err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
  604. return ;
  605. }
  606. if (slot->hp_slot >= php_ctlr->num_slots) {
  607. err("%s: Invalid HPC slot number!\n", __FUNCTION__);
  608. return ;
  609. }
  610. slot_cmd = 0x08;
  611. shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
  612. return;
  613. }
  614. int shpc_get_ctlr_slot_config(struct controller *ctrl,
  615. int *num_ctlr_slots, /* number of slots in this HPC */
  616. int *first_device_num, /* PCI dev num of the first slot in this SHPC */
  617. int *physical_slot_num, /* phy slot num of the first slot in this SHPC */
  618. int *updown, /* physical_slot_num increament: 1 or -1 */
  619. int *flags)
  620. {
  621. u32 slot_config;
  622. DBG_ENTER_ROUTINE
  623. if (!ctrl->hpc_ctlr_handle) {
  624. err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
  625. return -1;
  626. }
  627. slot_config = shpc_readl(ctrl, SLOT_CONFIG);
  628. *first_device_num = (slot_config & FIRST_DEV_NUM) >> 8;
  629. *num_ctlr_slots = slot_config & SLOT_NUM;
  630. *physical_slot_num = (slot_config & PSN) >> 16;
  631. *updown = ((slot_config & UPDOWN) >> 29) ? 1 : -1;
  632. dbg("%s: physical_slot_num = %x\n", __FUNCTION__, *physical_slot_num);
  633. DBG_LEAVE_ROUTINE
  634. return 0;
  635. }
  636. static void hpc_release_ctlr(struct controller *ctrl)
  637. {
  638. struct php_ctlr_state_s *php_ctlr = ctrl->hpc_ctlr_handle;
  639. struct php_ctlr_state_s *p, *p_prev;
  640. int i;
  641. u32 slot_reg;
  642. DBG_ENTER_ROUTINE
  643. if (!ctrl->hpc_ctlr_handle) {
  644. err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
  645. return ;
  646. }
  647. /*
  648. * Mask event interrupts and SERRs of all slots
  649. */
  650. for (i = 0; i < ctrl->num_slots; i++) {
  651. slot_reg = shpc_readl(ctrl, SLOT_REG(i));
  652. slot_reg |= (PRSNT_CHANGE_INTR_MASK | ISO_PFAULT_INTR_MASK |
  653. BUTTON_PRESS_INTR_MASK | MRL_CHANGE_INTR_MASK |
  654. CON_PFAULT_INTR_MASK | MRL_CHANGE_SERR_MASK |
  655. CON_PFAULT_SERR_MASK);
  656. slot_reg &= ~SLOT_REG_RSVDZ_MASK;
  657. shpc_writel(ctrl, SLOT_REG(i), slot_reg);
  658. }
  659. cleanup_slots(ctrl);
  660. if (shpchp_poll_mode) {
  661. del_timer(&php_ctlr->int_poll_timer);
  662. } else {
  663. if (php_ctlr->irq) {
  664. free_irq(php_ctlr->irq, ctrl);
  665. php_ctlr->irq = 0;
  666. pci_disable_msi(php_ctlr->pci_dev);
  667. }
  668. }
  669. if (php_ctlr->pci_dev) {
  670. iounmap(php_ctlr->creg);
  671. release_mem_region(ctrl->mmio_base, ctrl->mmio_size);
  672. php_ctlr->pci_dev = NULL;
  673. }
  674. spin_lock(&list_lock);
  675. p = php_ctlr_list_head;
  676. p_prev = NULL;
  677. while (p) {
  678. if (p == php_ctlr) {
  679. if (p_prev)
  680. p_prev->pnext = p->pnext;
  681. else
  682. php_ctlr_list_head = p->pnext;
  683. break;
  684. } else {
  685. p_prev = p;
  686. p = p->pnext;
  687. }
  688. }
  689. spin_unlock(&list_lock);
  690. kfree(php_ctlr);
  691. DBG_LEAVE_ROUTINE
  692. }
  693. static int hpc_power_on_slot(struct slot * slot)
  694. {
  695. struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
  696. u8 slot_cmd;
  697. int retval = 0;
  698. DBG_ENTER_ROUTINE
  699. if (!slot->ctrl->hpc_ctlr_handle) {
  700. err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
  701. return -1;
  702. }
  703. if (slot->hp_slot >= php_ctlr->num_slots) {
  704. err("%s: Invalid HPC slot number!\n", __FUNCTION__);
  705. return -1;
  706. }
  707. slot_cmd = 0x01;
  708. retval = shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
  709. if (retval) {
  710. err("%s: Write command failed!\n", __FUNCTION__);
  711. return -1;
  712. }
  713. DBG_LEAVE_ROUTINE
  714. return retval;
  715. }
  716. static int hpc_slot_enable(struct slot * slot)
  717. {
  718. struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
  719. u8 slot_cmd;
  720. int retval = 0;
  721. DBG_ENTER_ROUTINE
  722. if (!slot->ctrl->hpc_ctlr_handle) {
  723. err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
  724. return -1;
  725. }
  726. if (slot->hp_slot >= php_ctlr->num_slots) {
  727. err("%s: Invalid HPC slot number!\n", __FUNCTION__);
  728. return -1;
  729. }
  730. /* 3A => Slot - Enable, Power Indicator - Blink, Attention Indicator - Off */
  731. slot_cmd = 0x3A;
  732. retval = shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
  733. if (retval) {
  734. err("%s: Write command failed!\n", __FUNCTION__);
  735. return -1;
  736. }
  737. DBG_LEAVE_ROUTINE
  738. return retval;
  739. }
  740. static int hpc_slot_disable(struct slot * slot)
  741. {
  742. struct php_ctlr_state_s *php_ctlr = slot->ctrl->hpc_ctlr_handle;
  743. u8 slot_cmd;
  744. int retval = 0;
  745. DBG_ENTER_ROUTINE
  746. if (!slot->ctrl->hpc_ctlr_handle) {
  747. err("%s: Invalid HPC controller handle!\n", __FUNCTION__);
  748. return -1;
  749. }
  750. if (slot->hp_slot >= php_ctlr->num_slots) {
  751. err("%s: Invalid HPC slot number!\n", __FUNCTION__);
  752. return -1;
  753. }
  754. /* 1F => Slot - Disable, Power Indicator - Off, Attention Indicator - On */
  755. slot_cmd = 0x1F;
  756. retval = shpc_write_cmd(slot, slot->hp_slot, slot_cmd);
  757. if (retval) {
  758. err("%s: Write command failed!\n", __FUNCTION__);
  759. return -1;
  760. }
  761. DBG_LEAVE_ROUTINE
  762. return retval;
  763. }
  764. static int hpc_set_bus_speed_mode(struct slot * slot, enum pci_bus_speed value)
  765. {
  766. int retval;
  767. struct controller *ctrl = slot->ctrl;
  768. u8 pi, cmd;
  769. DBG_ENTER_ROUTINE
  770. pi = shpc_readb(ctrl, PROG_INTERFACE);
  771. if ((pi == 1) && (value > PCI_SPEED_133MHz_PCIX))
  772. return -EINVAL;
  773. switch (value) {
  774. case PCI_SPEED_33MHz:
  775. cmd = SETA_PCI_33MHZ;
  776. break;
  777. case PCI_SPEED_66MHz:
  778. cmd = SETA_PCI_66MHZ;
  779. break;
  780. case PCI_SPEED_66MHz_PCIX:
  781. cmd = SETA_PCIX_66MHZ;
  782. break;
  783. case PCI_SPEED_100MHz_PCIX:
  784. cmd = SETA_PCIX_100MHZ;
  785. break;
  786. case PCI_SPEED_133MHz_PCIX:
  787. cmd = SETA_PCIX_133MHZ;
  788. break;
  789. case PCI_SPEED_66MHz_PCIX_ECC:
  790. cmd = SETB_PCIX_66MHZ_EM;
  791. break;
  792. case PCI_SPEED_100MHz_PCIX_ECC:
  793. cmd = SETB_PCIX_100MHZ_EM;
  794. break;
  795. case PCI_SPEED_133MHz_PCIX_ECC:
  796. cmd = SETB_PCIX_133MHZ_EM;
  797. break;
  798. case PCI_SPEED_66MHz_PCIX_266:
  799. cmd = SETB_PCIX_66MHZ_266;
  800. break;
  801. case PCI_SPEED_100MHz_PCIX_266:
  802. cmd = SETB_PCIX_100MHZ_266;
  803. break;
  804. case PCI_SPEED_133MHz_PCIX_266:
  805. cmd = SETB_PCIX_133MHZ_266;
  806. break;
  807. case PCI_SPEED_66MHz_PCIX_533:
  808. cmd = SETB_PCIX_66MHZ_533;
  809. break;
  810. case PCI_SPEED_100MHz_PCIX_533:
  811. cmd = SETB_PCIX_100MHZ_533;
  812. break;
  813. case PCI_SPEED_133MHz_PCIX_533:
  814. cmd = SETB_PCIX_133MHZ_533;
  815. break;
  816. default:
  817. return -EINVAL;
  818. }
  819. retval = shpc_write_cmd(slot, 0, cmd);
  820. if (retval)
  821. err("%s: Write command failed!\n", __FUNCTION__);
  822. DBG_LEAVE_ROUTINE
  823. return retval;
  824. }
  825. static irqreturn_t shpc_isr(int IRQ, void *dev_id, struct pt_regs *regs)
  826. {
  827. struct controller *ctrl = NULL;
  828. struct php_ctlr_state_s *php_ctlr;
  829. u8 schedule_flag = 0;
  830. u32 temp_dword, intr_loc, intr_loc2;
  831. int hp_slot;
  832. if (!dev_id)
  833. return IRQ_NONE;
  834. if (!shpchp_poll_mode) {
  835. ctrl = (struct controller *)dev_id;
  836. php_ctlr = ctrl->hpc_ctlr_handle;
  837. } else {
  838. php_ctlr = (struct php_ctlr_state_s *) dev_id;
  839. ctrl = (struct controller *)php_ctlr->callback_instance_id;
  840. }
  841. if (!ctrl)
  842. return IRQ_NONE;
  843. if (!php_ctlr || !php_ctlr->creg)
  844. return IRQ_NONE;
  845. /* Check to see if it was our interrupt */
  846. intr_loc = shpc_readl(ctrl, INTR_LOC);
  847. if (!intr_loc)
  848. return IRQ_NONE;
  849. dbg("%s: intr_loc = %x\n",__FUNCTION__, intr_loc);
  850. if(!shpchp_poll_mode) {
  851. /* Mask Global Interrupt Mask - see implementation note on p. 139 */
  852. /* of SHPC spec rev 1.0*/
  853. temp_dword = shpc_readl(ctrl, SERR_INTR_ENABLE);
  854. temp_dword |= 0x00000001;
  855. shpc_writel(ctrl, SERR_INTR_ENABLE, temp_dword);
  856. intr_loc2 = shpc_readl(ctrl, INTR_LOC);
  857. dbg("%s: intr_loc2 = %x\n",__FUNCTION__, intr_loc2);
  858. }
  859. if (intr_loc & 0x0001) {
  860. /*
  861. * Command Complete Interrupt Pending
  862. * RO only - clear by writing 1 to the Command Completion
  863. * Detect bit in Controller SERR-INT register
  864. */
  865. temp_dword = shpc_readl(ctrl, SERR_INTR_ENABLE);
  866. temp_dword &= 0xfffdffff;
  867. shpc_writel(ctrl, SERR_INTR_ENABLE, temp_dword);
  868. ctrl->cmd_busy = 0;
  869. wake_up_interruptible(&ctrl->queue);
  870. }
  871. if ((intr_loc = (intr_loc >> 1)) == 0)
  872. goto out;
  873. for (hp_slot = 0; hp_slot < ctrl->num_slots; hp_slot++) {
  874. /* To find out which slot has interrupt pending */
  875. if ((intr_loc >> hp_slot) & 0x01) {
  876. temp_dword = shpc_readl(ctrl, SLOT_REG(hp_slot));
  877. dbg("%s: Slot %x with intr, slot register = %x\n",
  878. __FUNCTION__, hp_slot, temp_dword);
  879. if ((php_ctlr->switch_change_callback) &&
  880. (temp_dword & MRL_CHANGE_DETECTED))
  881. schedule_flag += php_ctlr->switch_change_callback(
  882. hp_slot, php_ctlr->callback_instance_id);
  883. if ((php_ctlr->attention_button_callback) &&
  884. (temp_dword & BUTTON_PRESS_DETECTED))
  885. schedule_flag += php_ctlr->attention_button_callback(
  886. hp_slot, php_ctlr->callback_instance_id);
  887. if ((php_ctlr->presence_change_callback) &&
  888. (temp_dword & PRSNT_CHANGE_DETECTED))
  889. schedule_flag += php_ctlr->presence_change_callback(
  890. hp_slot , php_ctlr->callback_instance_id);
  891. if ((php_ctlr->power_fault_callback) &&
  892. (temp_dword & (ISO_PFAULT_DETECTED | CON_PFAULT_DETECTED)))
  893. schedule_flag += php_ctlr->power_fault_callback(
  894. hp_slot, php_ctlr->callback_instance_id);
  895. /* Clear all slot events */
  896. temp_dword &= ~SLOT_REG_RSVDZ_MASK;
  897. shpc_writel(ctrl, SLOT_REG(hp_slot), temp_dword);
  898. intr_loc2 = shpc_readl(ctrl, INTR_LOC);
  899. dbg("%s: intr_loc2 = %x\n",__FUNCTION__, intr_loc2);
  900. }
  901. }
  902. out:
  903. if (!shpchp_poll_mode) {
  904. /* Unmask Global Interrupt Mask */
  905. temp_dword = shpc_readl(ctrl, SERR_INTR_ENABLE);
  906. temp_dword &= 0xfffffffe;
  907. shpc_writel(ctrl, SERR_INTR_ENABLE, temp_dword);
  908. }
  909. return IRQ_HANDLED;
  910. }
  911. static int hpc_get_max_bus_speed (struct slot *slot, enum pci_bus_speed *value)
  912. {
  913. int retval = 0;
  914. struct controller *ctrl = slot->ctrl;
  915. enum pci_bus_speed bus_speed = PCI_SPEED_UNKNOWN;
  916. u8 pi = shpc_readb(ctrl, PROG_INTERFACE);
  917. u32 slot_avail1 = shpc_readl(ctrl, SLOT_AVAIL1);
  918. u32 slot_avail2 = shpc_readl(ctrl, SLOT_AVAIL2);
  919. DBG_ENTER_ROUTINE
  920. if (pi == 2) {
  921. if (slot_avail2 & SLOT_133MHZ_PCIX_533)
  922. bus_speed = PCI_SPEED_133MHz_PCIX_533;
  923. else if (slot_avail2 & SLOT_100MHZ_PCIX_533)
  924. bus_speed = PCI_SPEED_100MHz_PCIX_533;
  925. else if (slot_avail2 & SLOT_66MHZ_PCIX_533)
  926. bus_speed = PCI_SPEED_66MHz_PCIX_533;
  927. else if (slot_avail2 & SLOT_133MHZ_PCIX_266)
  928. bus_speed = PCI_SPEED_133MHz_PCIX_266;
  929. else if (slot_avail2 & SLOT_100MHZ_PCIX_266)
  930. bus_speed = PCI_SPEED_100MHz_PCIX_266;
  931. else if (slot_avail2 & SLOT_66MHZ_PCIX_266)
  932. bus_speed = PCI_SPEED_66MHz_PCIX_266;
  933. }
  934. if (bus_speed == PCI_SPEED_UNKNOWN) {
  935. if (slot_avail1 & SLOT_133MHZ_PCIX)
  936. bus_speed = PCI_SPEED_133MHz_PCIX;
  937. else if (slot_avail1 & SLOT_100MHZ_PCIX)
  938. bus_speed = PCI_SPEED_100MHz_PCIX;
  939. else if (slot_avail1 & SLOT_66MHZ_PCIX)
  940. bus_speed = PCI_SPEED_66MHz_PCIX;
  941. else if (slot_avail2 & SLOT_66MHZ)
  942. bus_speed = PCI_SPEED_66MHz;
  943. else if (slot_avail1 & SLOT_33MHZ)
  944. bus_speed = PCI_SPEED_33MHz;
  945. else
  946. retval = -ENODEV;
  947. }
  948. *value = bus_speed;
  949. dbg("Max bus speed = %d\n", bus_speed);
  950. DBG_LEAVE_ROUTINE
  951. return retval;
  952. }
  953. static int hpc_get_cur_bus_speed (struct slot *slot, enum pci_bus_speed *value)
  954. {
  955. int retval = 0;
  956. struct controller *ctrl = slot->ctrl;
  957. enum pci_bus_speed bus_speed = PCI_SPEED_UNKNOWN;
  958. u16 sec_bus_reg = shpc_readw(ctrl, SEC_BUS_CONFIG);
  959. u8 pi = shpc_readb(ctrl, PROG_INTERFACE);
  960. u8 speed_mode = (pi == 2) ? (sec_bus_reg & 0xF) : (sec_bus_reg & 0x7);
  961. DBG_ENTER_ROUTINE
  962. if ((pi == 1) && (speed_mode > 4)) {
  963. *value = PCI_SPEED_UNKNOWN;
  964. return -ENODEV;
  965. }
  966. switch (speed_mode) {
  967. case 0x0:
  968. *value = PCI_SPEED_33MHz;
  969. break;
  970. case 0x1:
  971. *value = PCI_SPEED_66MHz;
  972. break;
  973. case 0x2:
  974. *value = PCI_SPEED_66MHz_PCIX;
  975. break;
  976. case 0x3:
  977. *value = PCI_SPEED_100MHz_PCIX;
  978. break;
  979. case 0x4:
  980. *value = PCI_SPEED_133MHz_PCIX;
  981. break;
  982. case 0x5:
  983. *value = PCI_SPEED_66MHz_PCIX_ECC;
  984. break;
  985. case 0x6:
  986. *value = PCI_SPEED_100MHz_PCIX_ECC;
  987. break;
  988. case 0x7:
  989. *value = PCI_SPEED_133MHz_PCIX_ECC;
  990. break;
  991. case 0x8:
  992. *value = PCI_SPEED_66MHz_PCIX_266;
  993. break;
  994. case 0x9:
  995. *value = PCI_SPEED_100MHz_PCIX_266;
  996. break;
  997. case 0xa:
  998. *value = PCI_SPEED_133MHz_PCIX_266;
  999. break;
  1000. case 0xb:
  1001. *value = PCI_SPEED_66MHz_PCIX_533;
  1002. break;
  1003. case 0xc:
  1004. *value = PCI_SPEED_100MHz_PCIX_533;
  1005. break;
  1006. case 0xd:
  1007. *value = PCI_SPEED_133MHz_PCIX_533;
  1008. break;
  1009. default:
  1010. *value = PCI_SPEED_UNKNOWN;
  1011. retval = -ENODEV;
  1012. break;
  1013. }
  1014. dbg("Current bus speed = %d\n", bus_speed);
  1015. DBG_LEAVE_ROUTINE
  1016. return retval;
  1017. }
  1018. static struct hpc_ops shpchp_hpc_ops = {
  1019. .power_on_slot = hpc_power_on_slot,
  1020. .slot_enable = hpc_slot_enable,
  1021. .slot_disable = hpc_slot_disable,
  1022. .set_bus_speed_mode = hpc_set_bus_speed_mode,
  1023. .set_attention_status = hpc_set_attention_status,
  1024. .get_power_status = hpc_get_power_status,
  1025. .get_attention_status = hpc_get_attention_status,
  1026. .get_latch_status = hpc_get_latch_status,
  1027. .get_adapter_status = hpc_get_adapter_status,
  1028. .get_max_bus_speed = hpc_get_max_bus_speed,
  1029. .get_cur_bus_speed = hpc_get_cur_bus_speed,
  1030. .get_adapter_speed = hpc_get_adapter_speed,
  1031. .get_mode1_ECC_cap = hpc_get_mode1_ECC_cap,
  1032. .get_prog_int = hpc_get_prog_int,
  1033. .query_power_fault = hpc_query_power_fault,
  1034. .green_led_on = hpc_set_green_led_on,
  1035. .green_led_off = hpc_set_green_led_off,
  1036. .green_led_blink = hpc_set_green_led_blink,
  1037. .release_ctlr = hpc_release_ctlr,
  1038. };
  1039. int shpc_init(struct controller * ctrl, struct pci_dev * pdev)
  1040. {
  1041. struct php_ctlr_state_s *php_ctlr, *p;
  1042. void *instance_id = ctrl;
  1043. int rc, num_slots = 0;
  1044. u8 hp_slot;
  1045. static int first = 1;
  1046. u32 shpc_base_offset;
  1047. u32 tempdword, slot_reg, slot_config;
  1048. u8 i;
  1049. DBG_ENTER_ROUTINE
  1050. ctrl->pci_dev = pdev; /* pci_dev of the P2P bridge */
  1051. spin_lock_init(&list_lock);
  1052. php_ctlr = kzalloc(sizeof(*php_ctlr), GFP_KERNEL);
  1053. if (!php_ctlr) { /* allocate controller state data */
  1054. err("%s: HPC controller memory allocation error!\n", __FUNCTION__);
  1055. goto abort;
  1056. }
  1057. php_ctlr->pci_dev = pdev; /* save pci_dev in context */
  1058. if ((pdev->vendor == PCI_VENDOR_ID_AMD) || (pdev->device ==
  1059. PCI_DEVICE_ID_AMD_GOLAM_7450)) {
  1060. /* amd shpc driver doesn't use Base Offset; assume 0 */
  1061. ctrl->mmio_base = pci_resource_start(pdev, 0);
  1062. ctrl->mmio_size = pci_resource_len(pdev, 0);
  1063. } else {
  1064. ctrl->cap_offset = pci_find_capability(pdev, PCI_CAP_ID_SHPC);
  1065. if (!ctrl->cap_offset) {
  1066. err("%s : cap_offset == 0\n", __FUNCTION__);
  1067. goto abort_free_ctlr;
  1068. }
  1069. dbg("%s: cap_offset = %x\n", __FUNCTION__, ctrl->cap_offset);
  1070. rc = shpc_indirect_read(ctrl, 0, &shpc_base_offset);
  1071. if (rc) {
  1072. err("%s: cannot read base_offset\n", __FUNCTION__);
  1073. goto abort_free_ctlr;
  1074. }
  1075. rc = shpc_indirect_read(ctrl, 3, &tempdword);
  1076. if (rc) {
  1077. err("%s: cannot read slot config\n", __FUNCTION__);
  1078. goto abort_free_ctlr;
  1079. }
  1080. num_slots = tempdword & SLOT_NUM;
  1081. dbg("%s: num_slots (indirect) %x\n", __FUNCTION__, num_slots);
  1082. for (i = 0; i < 9 + num_slots; i++) {
  1083. rc = shpc_indirect_read(ctrl, i, &tempdword);
  1084. if (rc) {
  1085. err("%s: cannot read creg (index = %d)\n",
  1086. __FUNCTION__, i);
  1087. goto abort_free_ctlr;
  1088. }
  1089. dbg("%s: offset %d: value %x\n", __FUNCTION__,i,
  1090. tempdword);
  1091. }
  1092. ctrl->mmio_base =
  1093. pci_resource_start(pdev, 0) + shpc_base_offset;
  1094. ctrl->mmio_size = 0x24 + 0x4 * num_slots;
  1095. }
  1096. if (first) {
  1097. spin_lock_init(&hpc_event_lock);
  1098. first = 0;
  1099. }
  1100. info("HPC vendor_id %x device_id %x ss_vid %x ss_did %x\n", pdev->vendor, pdev->device, pdev->subsystem_vendor,
  1101. pdev->subsystem_device);
  1102. if (pci_enable_device(pdev))
  1103. goto abort_free_ctlr;
  1104. if (!request_mem_region(ctrl->mmio_base, ctrl->mmio_size, MY_NAME)) {
  1105. err("%s: cannot reserve MMIO region\n", __FUNCTION__);
  1106. goto abort_free_ctlr;
  1107. }
  1108. php_ctlr->creg = ioremap(ctrl->mmio_base, ctrl->mmio_size);
  1109. if (!php_ctlr->creg) {
  1110. err("%s: cannot remap MMIO region %lx @ %lx\n", __FUNCTION__,
  1111. ctrl->mmio_size, ctrl->mmio_base);
  1112. release_mem_region(ctrl->mmio_base, ctrl->mmio_size);
  1113. goto abort_free_ctlr;
  1114. }
  1115. dbg("%s: php_ctlr->creg %p\n", __FUNCTION__, php_ctlr->creg);
  1116. mutex_init(&ctrl->crit_sect);
  1117. mutex_init(&ctrl->cmd_lock);
  1118. /* Setup wait queue */
  1119. init_waitqueue_head(&ctrl->queue);
  1120. /* Find the IRQ */
  1121. php_ctlr->irq = pdev->irq;
  1122. php_ctlr->attention_button_callback = shpchp_handle_attention_button,
  1123. php_ctlr->switch_change_callback = shpchp_handle_switch_change;
  1124. php_ctlr->presence_change_callback = shpchp_handle_presence_change;
  1125. php_ctlr->power_fault_callback = shpchp_handle_power_fault;
  1126. php_ctlr->callback_instance_id = instance_id;
  1127. ctrl->hpc_ctlr_handle = php_ctlr;
  1128. ctrl->hpc_ops = &shpchp_hpc_ops;
  1129. /* Return PCI Controller Info */
  1130. slot_config = shpc_readl(ctrl, SLOT_CONFIG);
  1131. php_ctlr->slot_device_offset = (slot_config & FIRST_DEV_NUM) >> 8;
  1132. php_ctlr->num_slots = slot_config & SLOT_NUM;
  1133. dbg("%s: slot_device_offset %x\n", __FUNCTION__, php_ctlr->slot_device_offset);
  1134. dbg("%s: num_slots %x\n", __FUNCTION__, php_ctlr->num_slots);
  1135. /* Mask Global Interrupt Mask & Command Complete Interrupt Mask */
  1136. tempdword = shpc_readl(ctrl, SERR_INTR_ENABLE);
  1137. dbg("%s: SERR_INTR_ENABLE = %x\n", __FUNCTION__, tempdword);
  1138. tempdword = 0x0003000f;
  1139. shpc_writel(ctrl, SERR_INTR_ENABLE, tempdword);
  1140. tempdword = shpc_readl(ctrl, SERR_INTR_ENABLE);
  1141. dbg("%s: SERR_INTR_ENABLE = %x\n", __FUNCTION__, tempdword);
  1142. /* Mask the MRL sensor SERR Mask of individual slot in
  1143. * Slot SERR-INT Mask & clear all the existing event if any
  1144. */
  1145. for (hp_slot = 0; hp_slot < php_ctlr->num_slots; hp_slot++) {
  1146. slot_reg = shpc_readl(ctrl, SLOT_REG(hp_slot));
  1147. dbg("%s: Default Logical Slot Register %d value %x\n", __FUNCTION__,
  1148. hp_slot, slot_reg);
  1149. slot_reg |= (PRSNT_CHANGE_INTR_MASK | ISO_PFAULT_INTR_MASK |
  1150. BUTTON_PRESS_INTR_MASK | MRL_CHANGE_INTR_MASK |
  1151. CON_PFAULT_INTR_MASK | MRL_CHANGE_SERR_MASK |
  1152. CON_PFAULT_SERR_MASK);
  1153. slot_reg &= ~SLOT_REG_RSVDZ_MASK;
  1154. shpc_writel(ctrl, SLOT_REG(hp_slot), slot_reg);
  1155. }
  1156. if (shpchp_poll_mode) {/* Install interrupt polling code */
  1157. /* Install and start the interrupt polling timer */
  1158. init_timer(&php_ctlr->int_poll_timer);
  1159. start_int_poll_timer( php_ctlr, 10 ); /* start with 10 second delay */
  1160. } else {
  1161. /* Installs the interrupt handler */
  1162. rc = pci_enable_msi(pdev);
  1163. if (rc) {
  1164. info("Can't get msi for the hotplug controller\n");
  1165. info("Use INTx for the hotplug controller\n");
  1166. } else
  1167. php_ctlr->irq = pdev->irq;
  1168. rc = request_irq(php_ctlr->irq, shpc_isr, SA_SHIRQ, MY_NAME, (void *) ctrl);
  1169. dbg("%s: request_irq %d for hpc%d (returns %d)\n", __FUNCTION__, php_ctlr->irq, ctlr_seq_num, rc);
  1170. if (rc) {
  1171. err("Can't get irq %d for the hotplug controller\n", php_ctlr->irq);
  1172. goto abort_free_ctlr;
  1173. }
  1174. }
  1175. dbg("%s: HPC at b:d:f:irq=0x%x:%x:%x:%x\n", __FUNCTION__,
  1176. pdev->bus->number, PCI_SLOT(pdev->devfn),
  1177. PCI_FUNC(pdev->devfn), pdev->irq);
  1178. get_hp_hw_control_from_firmware(pdev);
  1179. /* Add this HPC instance into the HPC list */
  1180. spin_lock(&list_lock);
  1181. if (php_ctlr_list_head == 0) {
  1182. php_ctlr_list_head = php_ctlr;
  1183. p = php_ctlr_list_head;
  1184. p->pnext = NULL;
  1185. } else {
  1186. p = php_ctlr_list_head;
  1187. while (p->pnext)
  1188. p = p->pnext;
  1189. p->pnext = php_ctlr;
  1190. }
  1191. spin_unlock(&list_lock);
  1192. ctlr_seq_num++;
  1193. /*
  1194. * Unmask all event interrupts of all slots
  1195. */
  1196. for (hp_slot = 0; hp_slot < php_ctlr->num_slots; hp_slot++) {
  1197. slot_reg = shpc_readl(ctrl, SLOT_REG(hp_slot));
  1198. dbg("%s: Default Logical Slot Register %d value %x\n", __FUNCTION__,
  1199. hp_slot, slot_reg);
  1200. slot_reg &= ~(PRSNT_CHANGE_INTR_MASK | ISO_PFAULT_INTR_MASK |
  1201. BUTTON_PRESS_INTR_MASK | MRL_CHANGE_INTR_MASK |
  1202. CON_PFAULT_INTR_MASK | SLOT_REG_RSVDZ_MASK);
  1203. shpc_writel(ctrl, SLOT_REG(hp_slot), slot_reg);
  1204. }
  1205. if (!shpchp_poll_mode) {
  1206. /* Unmask all general input interrupts and SERR */
  1207. tempdword = shpc_readl(ctrl, SERR_INTR_ENABLE);
  1208. tempdword = 0x0000000a;
  1209. shpc_writel(ctrl, SERR_INTR_ENABLE, tempdword);
  1210. tempdword = shpc_readl(ctrl, SERR_INTR_ENABLE);
  1211. dbg("%s: SERR_INTR_ENABLE = %x\n", __FUNCTION__, tempdword);
  1212. }
  1213. DBG_LEAVE_ROUTINE
  1214. return 0;
  1215. /* We end up here for the many possible ways to fail this API. */
  1216. abort_free_ctlr:
  1217. kfree(php_ctlr);
  1218. abort:
  1219. DBG_LEAVE_ROUTINE
  1220. return -1;
  1221. }