cpcihp_zt5550.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * cpcihp_zt5550.c
  4. *
  5. * Intel/Ziatech ZT5550 CompactPCI Host Controller driver
  6. *
  7. * Copyright 2002 SOMA Networks, Inc.
  8. * Copyright 2001 Intel San Luis Obispo
  9. * Copyright 2000,2001 MontaVista Software Inc.
  10. *
  11. * Send feedback to <[email protected]>
  12. */
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/init.h>
  16. #include <linux/errno.h>
  17. #include <linux/pci.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/signal.h> /* IRQF_SHARED */
  20. #include "cpci_hotplug.h"
  21. #include "cpcihp_zt5550.h"
  22. #define DRIVER_VERSION "0.2"
  23. #define DRIVER_AUTHOR "Scott Murray <[email protected]>"
  24. #define DRIVER_DESC "ZT5550 CompactPCI Hot Plug Driver"
  25. #define MY_NAME "cpcihp_zt5550"
  26. #define dbg(format, arg...) \
  27. do { \
  28. if (debug) \
  29. printk(KERN_DEBUG "%s: " format "\n", \
  30. MY_NAME, ## arg); \
  31. } while (0)
  32. #define err(format, arg...) printk(KERN_ERR "%s: " format "\n", MY_NAME, ## arg)
  33. #define info(format, arg...) printk(KERN_INFO "%s: " format "\n", MY_NAME, ## arg)
  34. #define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n", MY_NAME, ## arg)
  35. /* local variables */
  36. static bool debug;
  37. static bool poll;
  38. static struct cpci_hp_controller_ops zt5550_hpc_ops;
  39. static struct cpci_hp_controller zt5550_hpc;
  40. /* Primary cPCI bus bridge device */
  41. static struct pci_dev *bus0_dev;
  42. static struct pci_bus *bus0;
  43. /* Host controller device */
  44. static struct pci_dev *hc_dev;
  45. /* Host controller register addresses */
  46. static void __iomem *hc_registers;
  47. static void __iomem *csr_hc_index;
  48. static void __iomem *csr_hc_data;
  49. static void __iomem *csr_int_status;
  50. static void __iomem *csr_int_mask;
  51. static int zt5550_hc_config(struct pci_dev *pdev)
  52. {
  53. int ret;
  54. /* Since we know that no boards exist with two HC chips, treat it as an error */
  55. if (hc_dev) {
  56. err("too many host controller devices?");
  57. return -EBUSY;
  58. }
  59. ret = pci_enable_device(pdev);
  60. if (ret) {
  61. err("cannot enable %s\n", pci_name(pdev));
  62. return ret;
  63. }
  64. hc_dev = pdev;
  65. dbg("hc_dev = %p", hc_dev);
  66. dbg("pci resource start %llx", (unsigned long long)pci_resource_start(hc_dev, 1));
  67. dbg("pci resource len %llx", (unsigned long long)pci_resource_len(hc_dev, 1));
  68. if (!request_mem_region(pci_resource_start(hc_dev, 1),
  69. pci_resource_len(hc_dev, 1), MY_NAME)) {
  70. err("cannot reserve MMIO region");
  71. ret = -ENOMEM;
  72. goto exit_disable_device;
  73. }
  74. hc_registers =
  75. ioremap(pci_resource_start(hc_dev, 1), pci_resource_len(hc_dev, 1));
  76. if (!hc_registers) {
  77. err("cannot remap MMIO region %llx @ %llx",
  78. (unsigned long long)pci_resource_len(hc_dev, 1),
  79. (unsigned long long)pci_resource_start(hc_dev, 1));
  80. ret = -ENODEV;
  81. goto exit_release_region;
  82. }
  83. csr_hc_index = hc_registers + CSR_HCINDEX;
  84. csr_hc_data = hc_registers + CSR_HCDATA;
  85. csr_int_status = hc_registers + CSR_INTSTAT;
  86. csr_int_mask = hc_registers + CSR_INTMASK;
  87. /*
  88. * Disable host control, fault and serial interrupts
  89. */
  90. dbg("disabling host control, fault and serial interrupts");
  91. writeb((u8) HC_INT_MASK_REG, csr_hc_index);
  92. writeb((u8) ALL_INDEXED_INTS_MASK, csr_hc_data);
  93. dbg("disabled host control, fault and serial interrupts");
  94. /*
  95. * Disable timer0, timer1 and ENUM interrupts
  96. */
  97. dbg("disabling timer0, timer1 and ENUM interrupts");
  98. writeb((u8) ALL_DIRECT_INTS_MASK, csr_int_mask);
  99. dbg("disabled timer0, timer1 and ENUM interrupts");
  100. return 0;
  101. exit_release_region:
  102. release_mem_region(pci_resource_start(hc_dev, 1),
  103. pci_resource_len(hc_dev, 1));
  104. exit_disable_device:
  105. pci_disable_device(hc_dev);
  106. return ret;
  107. }
  108. static int zt5550_hc_cleanup(void)
  109. {
  110. if (!hc_dev)
  111. return -ENODEV;
  112. iounmap(hc_registers);
  113. release_mem_region(pci_resource_start(hc_dev, 1),
  114. pci_resource_len(hc_dev, 1));
  115. pci_disable_device(hc_dev);
  116. return 0;
  117. }
  118. static int zt5550_hc_query_enum(void)
  119. {
  120. u8 value;
  121. value = inb_p(ENUM_PORT);
  122. return ((value & ENUM_MASK) == ENUM_MASK);
  123. }
  124. static int zt5550_hc_check_irq(void *dev_id)
  125. {
  126. int ret;
  127. u8 reg;
  128. ret = 0;
  129. if (dev_id == zt5550_hpc.dev_id) {
  130. reg = readb(csr_int_status);
  131. if (reg)
  132. ret = 1;
  133. }
  134. return ret;
  135. }
  136. static int zt5550_hc_enable_irq(void)
  137. {
  138. u8 reg;
  139. if (hc_dev == NULL)
  140. return -ENODEV;
  141. reg = readb(csr_int_mask);
  142. reg = reg & ~ENUM_INT_MASK;
  143. writeb(reg, csr_int_mask);
  144. return 0;
  145. }
  146. static int zt5550_hc_disable_irq(void)
  147. {
  148. u8 reg;
  149. if (hc_dev == NULL)
  150. return -ENODEV;
  151. reg = readb(csr_int_mask);
  152. reg = reg | ENUM_INT_MASK;
  153. writeb(reg, csr_int_mask);
  154. return 0;
  155. }
  156. static int zt5550_hc_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  157. {
  158. int status;
  159. status = zt5550_hc_config(pdev);
  160. if (status != 0)
  161. return status;
  162. dbg("returned from zt5550_hc_config");
  163. memset(&zt5550_hpc, 0, sizeof(struct cpci_hp_controller));
  164. zt5550_hpc_ops.query_enum = zt5550_hc_query_enum;
  165. zt5550_hpc.ops = &zt5550_hpc_ops;
  166. if (!poll) {
  167. zt5550_hpc.irq = hc_dev->irq;
  168. zt5550_hpc.irq_flags = IRQF_SHARED;
  169. zt5550_hpc.dev_id = hc_dev;
  170. zt5550_hpc_ops.enable_irq = zt5550_hc_enable_irq;
  171. zt5550_hpc_ops.disable_irq = zt5550_hc_disable_irq;
  172. zt5550_hpc_ops.check_irq = zt5550_hc_check_irq;
  173. } else {
  174. info("using ENUM# polling mode");
  175. }
  176. status = cpci_hp_register_controller(&zt5550_hpc);
  177. if (status != 0) {
  178. err("could not register cPCI hotplug controller");
  179. goto init_hc_error;
  180. }
  181. dbg("registered controller");
  182. /* Look for first device matching cPCI bus's bridge vendor and device IDs */
  183. bus0_dev = pci_get_device(PCI_VENDOR_ID_DEC,
  184. PCI_DEVICE_ID_DEC_21154, NULL);
  185. if (!bus0_dev) {
  186. status = -ENODEV;
  187. goto init_register_error;
  188. }
  189. bus0 = bus0_dev->subordinate;
  190. pci_dev_put(bus0_dev);
  191. status = cpci_hp_register_bus(bus0, 0x0a, 0x0f);
  192. if (status != 0) {
  193. err("could not register cPCI hotplug bus");
  194. goto init_register_error;
  195. }
  196. dbg("registered bus");
  197. status = cpci_hp_start();
  198. if (status != 0) {
  199. err("could not started cPCI hotplug system");
  200. cpci_hp_unregister_bus(bus0);
  201. goto init_register_error;
  202. }
  203. dbg("started cpci hp system");
  204. return 0;
  205. init_register_error:
  206. cpci_hp_unregister_controller(&zt5550_hpc);
  207. init_hc_error:
  208. err("status = %d", status);
  209. zt5550_hc_cleanup();
  210. return status;
  211. }
  212. static void zt5550_hc_remove_one(struct pci_dev *pdev)
  213. {
  214. cpci_hp_stop();
  215. cpci_hp_unregister_bus(bus0);
  216. cpci_hp_unregister_controller(&zt5550_hpc);
  217. zt5550_hc_cleanup();
  218. }
  219. static const struct pci_device_id zt5550_hc_pci_tbl[] = {
  220. { PCI_VENDOR_ID_ZIATECH, PCI_DEVICE_ID_ZIATECH_5550_HC, PCI_ANY_ID, PCI_ANY_ID, },
  221. { 0, }
  222. };
  223. MODULE_DEVICE_TABLE(pci, zt5550_hc_pci_tbl);
  224. static struct pci_driver zt5550_hc_driver = {
  225. .name = "zt5550_hc",
  226. .id_table = zt5550_hc_pci_tbl,
  227. .probe = zt5550_hc_init_one,
  228. .remove = zt5550_hc_remove_one,
  229. };
  230. static int __init zt5550_init(void)
  231. {
  232. struct resource *r;
  233. int rc;
  234. info(DRIVER_DESC " version: " DRIVER_VERSION);
  235. r = request_region(ENUM_PORT, 1, "#ENUM hotswap signal register");
  236. if (!r)
  237. return -EBUSY;
  238. rc = pci_register_driver(&zt5550_hc_driver);
  239. if (rc < 0)
  240. release_region(ENUM_PORT, 1);
  241. return rc;
  242. }
  243. static void __exit
  244. zt5550_exit(void)
  245. {
  246. pci_unregister_driver(&zt5550_hc_driver);
  247. release_region(ENUM_PORT, 1);
  248. }
  249. module_init(zt5550_init);
  250. module_exit(zt5550_exit);
  251. MODULE_AUTHOR(DRIVER_AUTHOR);
  252. MODULE_DESCRIPTION(DRIVER_DESC);
  253. MODULE_LICENSE("GPL");
  254. module_param(debug, bool, 0644);
  255. MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
  256. module_param(poll, bool, 0644);
  257. MODULE_PARM_DESC(poll, "#ENUM polling mode enabled or not");