30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/types.h>
33 #include <linux/signal.h>
36 #include <linux/pci.h>
38 #include <linux/time.h>
39 #include <linux/slab.h>
70 #define POWER_OFF PCI_EXP_SLTCTL_PCC
76 static void int_poll_timeout(
unsigned long data)
91 static void start_int_poll_timer(
struct controller *ctrl,
int sec)
94 if ((sec <= 0) || (sec > 60))
103 static inline int pciehp_request_irq(
struct controller *ctrl)
110 start_int_poll_timer(ctrl, 10);
117 ctrl_err(ctrl,
"Cannot get irq %d for the hotplug controller\n",
122 static inline void pciehp_free_irq(
struct controller *ctrl)
130 static int pcie_poll_cmd(
struct controller *ctrl)
140 while (timeout > 0) {
144 if (!err && (slot_status & PCI_EXP_SLTSTA_CC)) {
159 rc = pcie_poll_cmd(ctrl);
163 ctrl_dbg(ctrl,
"Command not completed in 1000 msec\n");
182 ctrl_err(ctrl,
"%s: Cannot read SLOTSTATUS register\n",
187 if (slot_status & PCI_EXP_SLTSTA_CC) {
194 ctrl_dbg(ctrl,
"CMD_COMPLETED not clear after 1 sec\n");
201 ctrl_dbg(ctrl,
"Unexpected CMD_COMPLETED. Need to "
202 "wait for command completed event.\n");
205 ctrl_dbg(ctrl,
"Unexpected CMD_COMPLETED. Maybe "
206 "the controller is broken.\n");
212 ctrl_err(ctrl,
"%s: Cannot read SLOTCTRL register\n", __func__);
217 slot_ctrl |= (cmd &
mask);
222 ctrl_err(ctrl,
"Cannot write to SLOTCTRL register\n");
237 pcie_wait_cmd(ctrl, poll);
244 static bool check_link_active(
struct controller *ctrl)
255 ctrl_dbg(ctrl,
"%s: lnk_status = %x\n", __func__, lnk_status);
264 if (check_link_active(ctrl) == active)
266 while (timeout > 0) {
269 if (check_link_active(ctrl) == active)
272 ctrl_dbg(ctrl,
"Data Link Layer Link Active not %s in 1000 msec\n",
273 active ?
"set" :
"cleared");
276 static void pcie_wait_link_active(
struct controller *ctrl)
278 __pcie_wait_link_active(ctrl,
true);
281 static void pcie_wait_link_not_active(
struct controller *ctrl)
283 __pcie_wait_link_active(ctrl,
false);
305 printk(
KERN_DEBUG "pci %04x:%02x:%02x.%d id reading try %d times with interval %d ms to get %08x\n",
324 pcie_wait_link_active(ctrl);
330 found = pci_bus_check_dev(ctrl->
pcie->port->subordinate,
335 ctrl_err(ctrl,
"Cannot read LNKSTATUS register\n");
339 ctrl_dbg(ctrl,
"%s: lnk_status = %x\n", __func__, lnk_status);
342 ctrl_err(ctrl,
"Link Training Error occurs \n");
349 if (!found && !retval)
362 ctrl_err(ctrl,
"Cannot read LNKCTRL register\n");
373 ctrl_err(ctrl,
"Cannot write LNKCTRL register\n");
376 ctrl_dbg(ctrl,
"%s: lnk_ctrl = %x\n", __func__, lnk_ctrl);
381 static int pciehp_link_enable(
struct controller *ctrl)
383 return __pciehp_link_set(ctrl,
true);
386 static int pciehp_link_disable(
struct controller *ctrl)
388 return __pciehp_link_set(ctrl,
false);
400 ctrl_err(ctrl,
"%s: Cannot read SLOTCTRL register\n", __func__);
404 ctrl_dbg(ctrl,
"%s: SLOTCTRL %x, value read %x\n", __func__,
409 switch (atten_led_state) {
439 ctrl_err(ctrl,
"%s: Cannot read SLOTCTRL register\n", __func__);
442 ctrl_dbg(ctrl,
"%s: SLOTCTRL %x value read %x\n", __func__,
470 ctrl_err(ctrl,
"%s: Cannot read SLOTSTATUS register\n",
486 ctrl_err(ctrl,
"%s: Cannot read SLOTSTATUS register\n",
502 ctrl_err(ctrl,
"Cannot check for power fault\n");
528 ctrl_dbg(ctrl,
"%s: SLOTCTRL %x write cmd %x\n", __func__,
530 return pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
541 pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
542 ctrl_dbg(ctrl,
"%s: SLOTCTRL %x write cmd %x\n", __func__,
554 pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
555 ctrl_dbg(ctrl,
"%s: SLOTCTRL %x write cmd %x\n", __func__,
567 pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
568 ctrl_dbg(ctrl,
"%s: SLOTCTRL %x write cmd %x\n", __func__,
583 ctrl_err(ctrl,
"%s: Cannot read SLOTSTATUS register\n",
592 "%s: Cannot write to SLOTSTATUS register\n",
601 retval = pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
603 ctrl_err(ctrl,
"Write %x command failed!\n", slot_cmd);
606 ctrl_dbg(ctrl,
"%s: SLOTCTRL %x write cmd %x\n", __func__,
609 retval = pciehp_link_enable(ctrl);
611 ctrl_err(ctrl,
"%s: Can not enable the link!\n", __func__);
624 pciehp_link_disable(ctrl);
627 pcie_wait_link_not_active(ctrl);
633 retval = pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
635 ctrl_err(ctrl,
"Write command failed!\n");
638 ctrl_dbg(ctrl,
"%s: SLOTCTRL %x write cmd %x\n", __func__,
657 ctrl_err(ctrl,
"%s: Cannot read SLOTSTATUS\n",
665 detected &= ~intr_loc;
666 intr_loc |= detected;
670 ctrl_err(ctrl,
"%s: Cannot write to SLOTSTATUS\n",
676 ctrl_dbg(ctrl,
"%s: intr_loc %x\n", __func__, intr_loc);
679 if (intr_loc & PCI_EXP_SLTSTA_CC) {
685 if (!(intr_loc & ~PCI_EXP_SLTSTA_CC))
734 if (pcie_write_cmd(ctrl, cmd, mask)) {
735 ctrl_err(ctrl,
"Cannot enable software notification\n");
741 static void pcie_disable_notification(
struct controller *ctrl)
748 if (pcie_write_cmd(ctrl, 0, mask))
749 ctrl_warn(ctrl,
"Cannot disable software notification\n");
754 if (pciehp_request_irq(ctrl))
757 pciehp_free_irq(ctrl);
764 static void pcie_shutdown_notification(
struct controller *ctrl)
767 pcie_disable_notification(ctrl);
768 pciehp_free_irq(ctrl);
773 static int pcie_init_slot(
struct controller *ctrl)
788 static void pcie_cleanup_slot(
struct controller *ctrl)
790 struct slot *slot = ctrl->
slot;
796 static inline void dbg_ctrl(
struct controller *ctrl)
805 ctrl_info(ctrl,
"Hotplug Controller:\n");
806 ctrl_info(ctrl,
" Seg/Bus/Dev/Func/IRQ : %s IRQ %d\n",
807 pci_name(pdev), pdev->
irq);
810 ctrl_info(ctrl,
" Subsystem ID : 0x%04x\n",
812 ctrl_info(ctrl,
" Subsystem Vendor ID : 0x%04x\n",
814 ctrl_info(ctrl,
" PCIe Cap offset : 0x%02x\n",
819 ctrl_info(ctrl,
" PCI resource [%d] : %pR\n",
823 ctrl_info(ctrl,
" Physical Slot Number : %d\n",
PSN(ctrl));
824 ctrl_info(ctrl,
" Attention Button : %3s\n",
826 ctrl_info(ctrl,
" Power Controller : %3s\n",
830 ctrl_info(ctrl,
" Attention Indicator : %3s\n",
832 ctrl_info(ctrl,
" Power Indicator : %3s\n",
834 ctrl_info(ctrl,
" Hot-Plug Surprise : %3s\n",
837 EMI(ctrl) ?
"yes" :
"no");
838 ctrl_info(ctrl,
" Command Completed : %3s\n",
841 ctrl_info(ctrl,
"Slot Status : 0x%04x\n", reg16);
843 ctrl_info(ctrl,
"Slot Control : 0x%04x\n", reg16);
859 ctrl_err(ctrl,
"Cannot read SLOTCAP register\n");
879 ctrl_err(ctrl,
"%s: Cannot read LNKCAP register\n", __func__);
883 ctrl_dbg(ctrl,
"Link Active Reporting supported\n");
892 pcie_disable_notification(ctrl);
894 ctrl_info(ctrl,
"HPC vendor_id %x device_id %x ss_vid %x ss_did %x\n",
898 if (pcie_init_slot(ctrl))
911 pcie_shutdown_notification(ctrl);
912 pcie_cleanup_slot(ctrl);