tsi108_pci.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Common routines for Tundra Semiconductor TSI108 host bridge.
  4. *
  5. * 2004-2005 (c) Tundra Semiconductor Corp.
  6. * Author: Alex Bounine ([email protected])
  7. * Author: Roy Zang ([email protected])
  8. * Add pci interrupt router host
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/pci.h>
  13. #include <linux/irq.h>
  14. #include <linux/irqdomain.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/of_address.h>
  17. #include <asm/byteorder.h>
  18. #include <asm/io.h>
  19. #include <asm/irq.h>
  20. #include <linux/uaccess.h>
  21. #include <asm/machdep.h>
  22. #include <asm/pci-bridge.h>
  23. #include <asm/tsi108.h>
  24. #include <asm/tsi108_pci.h>
  25. #include <asm/tsi108_irq.h>
  26. #undef DEBUG
  27. #ifdef DEBUG
  28. #define DBG(x...) printk(x)
  29. #else
  30. #define DBG(x...)
  31. #endif
  32. #define tsi_mk_config_addr(bus, devfunc, offset) \
  33. ((((bus)<<16) | ((devfunc)<<8) | (offset & 0xfc)) + tsi108_pci_cfg_base)
  34. u32 tsi108_pci_cfg_base;
  35. static u32 tsi108_pci_cfg_phys;
  36. u32 tsi108_csr_vir_base;
  37. static struct irq_domain *pci_irq_host;
  38. extern u32 get_vir_csrbase(void);
  39. extern u32 tsi108_read_reg(u32 reg_offset);
  40. extern void tsi108_write_reg(u32 reg_offset, u32 val);
  41. int
  42. tsi108_direct_write_config(struct pci_bus *bus, unsigned int devfunc,
  43. int offset, int len, u32 val)
  44. {
  45. volatile unsigned char *cfg_addr;
  46. struct pci_controller *hose = pci_bus_to_host(bus);
  47. if (ppc_md.pci_exclude_device)
  48. if (ppc_md.pci_exclude_device(hose, bus->number, devfunc))
  49. return PCIBIOS_DEVICE_NOT_FOUND;
  50. cfg_addr = (unsigned char *)(tsi_mk_config_addr(bus->number,
  51. devfunc, offset) |
  52. (offset & 0x03));
  53. #ifdef DEBUG
  54. printk("PCI CFG write : ");
  55. printk("%d:0x%x:0x%x ", bus->number, devfunc, offset);
  56. printk("%d ADDR=0x%08x ", len, (uint) cfg_addr);
  57. printk("data = 0x%08x\n", val);
  58. #endif
  59. switch (len) {
  60. case 1:
  61. out_8((u8 *) cfg_addr, val);
  62. break;
  63. case 2:
  64. out_le16((u16 *) cfg_addr, val);
  65. break;
  66. default:
  67. out_le32((u32 *) cfg_addr, val);
  68. break;
  69. }
  70. return PCIBIOS_SUCCESSFUL;
  71. }
  72. void tsi108_clear_pci_error(u32 pci_cfg_base)
  73. {
  74. u32 err_stat, err_addr, pci_stat;
  75. /*
  76. * Quietly clear PB and PCI error flags set as result
  77. * of PCI/X configuration read requests.
  78. */
  79. /* Read PB Error Log Registers */
  80. err_stat = tsi108_read_reg(TSI108_PB_OFFSET + TSI108_PB_ERRCS);
  81. err_addr = tsi108_read_reg(TSI108_PB_OFFSET + TSI108_PB_AERR);
  82. if (err_stat & TSI108_PB_ERRCS_ES) {
  83. /* Clear error flag */
  84. tsi108_write_reg(TSI108_PB_OFFSET + TSI108_PB_ERRCS,
  85. TSI108_PB_ERRCS_ES);
  86. /* Clear read error reported in PB_ISR */
  87. tsi108_write_reg(TSI108_PB_OFFSET + TSI108_PB_ISR,
  88. TSI108_PB_ISR_PBS_RD_ERR);
  89. /* Clear PCI/X bus cfg errors if applicable */
  90. if ((err_addr & 0xFF000000) == pci_cfg_base) {
  91. pci_stat =
  92. tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_CSR);
  93. tsi108_write_reg(TSI108_PCI_OFFSET + TSI108_PCI_CSR,
  94. pci_stat);
  95. }
  96. }
  97. return;
  98. }
  99. #define __tsi108_read_pci_config(x, addr, op) \
  100. __asm__ __volatile__( \
  101. " "op" %0,0,%1\n" \
  102. "1: eieio\n" \
  103. "2:\n" \
  104. ".section .fixup,\"ax\"\n" \
  105. "3: li %0,-1\n" \
  106. " b 2b\n" \
  107. ".previous\n" \
  108. EX_TABLE(1b, 3b) \
  109. : "=r"(x) : "r"(addr))
  110. int
  111. tsi108_direct_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
  112. int len, u32 * val)
  113. {
  114. volatile unsigned char *cfg_addr;
  115. struct pci_controller *hose = pci_bus_to_host(bus);
  116. u32 temp;
  117. if (ppc_md.pci_exclude_device)
  118. if (ppc_md.pci_exclude_device(hose, bus->number, devfn))
  119. return PCIBIOS_DEVICE_NOT_FOUND;
  120. cfg_addr = (unsigned char *)(tsi_mk_config_addr(bus->number,
  121. devfn,
  122. offset) | (offset &
  123. 0x03));
  124. switch (len) {
  125. case 1:
  126. __tsi108_read_pci_config(temp, cfg_addr, "lbzx");
  127. break;
  128. case 2:
  129. __tsi108_read_pci_config(temp, cfg_addr, "lhbrx");
  130. break;
  131. default:
  132. __tsi108_read_pci_config(temp, cfg_addr, "lwbrx");
  133. break;
  134. }
  135. *val = temp;
  136. #ifdef DEBUG
  137. if ((0xFFFFFFFF != temp) && (0xFFFF != temp) && (0xFF != temp)) {
  138. printk("PCI CFG read : ");
  139. printk("%d:0x%x:0x%x ", bus->number, devfn, offset);
  140. printk("%d ADDR=0x%08x ", len, (uint) cfg_addr);
  141. printk("data = 0x%x\n", *val);
  142. }
  143. #endif
  144. return PCIBIOS_SUCCESSFUL;
  145. }
  146. void tsi108_clear_pci_cfg_error(void)
  147. {
  148. tsi108_clear_pci_error(tsi108_pci_cfg_phys);
  149. }
  150. static struct pci_ops tsi108_direct_pci_ops = {
  151. .read = tsi108_direct_read_config,
  152. .write = tsi108_direct_write_config,
  153. };
  154. int __init tsi108_setup_pci(struct device_node *dev, u32 cfg_phys, int primary)
  155. {
  156. int len;
  157. struct pci_controller *hose;
  158. struct resource rsrc;
  159. const int *bus_range;
  160. int has_address = 0;
  161. /* PCI Config mapping */
  162. tsi108_pci_cfg_base = (u32)ioremap(cfg_phys, TSI108_PCI_CFG_SIZE);
  163. tsi108_pci_cfg_phys = cfg_phys;
  164. DBG("TSI_PCI: %s tsi108_pci_cfg_base=0x%x\n", __func__,
  165. tsi108_pci_cfg_base);
  166. /* Fetch host bridge registers address */
  167. has_address = (of_address_to_resource(dev, 0, &rsrc) == 0);
  168. /* Get bus range if any */
  169. bus_range = of_get_property(dev, "bus-range", &len);
  170. if (bus_range == NULL || len < 2 * sizeof(int)) {
  171. printk(KERN_WARNING "Can't get bus-range for %pOF, assume"
  172. " bus 0\n", dev);
  173. }
  174. hose = pcibios_alloc_controller(dev);
  175. if (!hose) {
  176. printk("PCI Host bridge init failed\n");
  177. return -ENOMEM;
  178. }
  179. hose->first_busno = bus_range ? bus_range[0] : 0;
  180. hose->last_busno = bus_range ? bus_range[1] : 0xff;
  181. (hose)->ops = &tsi108_direct_pci_ops;
  182. pr_info("Found tsi108 PCI host bridge at 0x%pa. Firmware bus number: %d->%d\n",
  183. &rsrc.start, hose->first_busno, hose->last_busno);
  184. /* Interpret the "ranges" property */
  185. /* This also maps the I/O region and sets isa_io/mem_base */
  186. pci_process_bridge_OF_ranges(hose, dev, primary);
  187. return 0;
  188. }
  189. /*
  190. * Low level utility functions
  191. */
  192. static void tsi108_pci_int_mask(u_int irq)
  193. {
  194. u_int irp_cfg;
  195. int int_line = (irq - IRQ_PCI_INTAD_BASE);
  196. irp_cfg = tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_CFG_CTL);
  197. mb();
  198. irp_cfg |= (1 << int_line); /* INTx_DIR = output */
  199. irp_cfg &= ~(3 << (8 + (int_line * 2))); /* INTx_TYPE = unused */
  200. tsi108_write_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_CFG_CTL, irp_cfg);
  201. mb();
  202. irp_cfg = tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_CFG_CTL);
  203. }
  204. static void tsi108_pci_int_unmask(u_int irq)
  205. {
  206. u_int irp_cfg;
  207. int int_line = (irq - IRQ_PCI_INTAD_BASE);
  208. irp_cfg = tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_CFG_CTL);
  209. mb();
  210. irp_cfg &= ~(1 << int_line);
  211. irp_cfg |= (3 << (8 + (int_line * 2)));
  212. tsi108_write_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_CFG_CTL, irp_cfg);
  213. mb();
  214. }
  215. static void __init init_pci_source(void)
  216. {
  217. tsi108_write_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_CFG_CTL,
  218. 0x0000ff00);
  219. tsi108_write_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_ENABLE,
  220. TSI108_PCI_IRP_ENABLE_P_INT);
  221. mb();
  222. }
  223. static inline unsigned int get_pci_source(void)
  224. {
  225. u_int temp = 0;
  226. int irq = -1;
  227. int i;
  228. u_int pci_irp_stat;
  229. static int mask = 0;
  230. /* Read PCI/X block interrupt status register */
  231. pci_irp_stat = tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_STAT);
  232. mb();
  233. if (pci_irp_stat & TSI108_PCI_IRP_STAT_P_INT) {
  234. /* Process Interrupt from PCI bus INTA# - INTD# lines */
  235. temp =
  236. tsi108_read_reg(TSI108_PCI_OFFSET +
  237. TSI108_PCI_IRP_INTAD) & 0xf;
  238. mb();
  239. for (i = 0; i < 4; i++, mask++) {
  240. if (temp & (1 << mask % 4)) {
  241. irq = IRQ_PCI_INTA + mask % 4;
  242. mask++;
  243. break;
  244. }
  245. }
  246. /* Disable interrupts from PCI block */
  247. temp = tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_ENABLE);
  248. tsi108_write_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_ENABLE,
  249. temp & ~TSI108_PCI_IRP_ENABLE_P_INT);
  250. mb();
  251. (void)tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_ENABLE);
  252. mb();
  253. }
  254. #ifdef DEBUG
  255. else {
  256. printk("TSI108_PIC: error in TSI108_PCI_IRP_STAT\n");
  257. pci_irp_stat =
  258. tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_STAT);
  259. temp =
  260. tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_INTAD);
  261. mb();
  262. printk(">> stat=0x%08x intad=0x%08x ", pci_irp_stat, temp);
  263. temp =
  264. tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_CFG_CTL);
  265. mb();
  266. printk("cfg_ctl=0x%08x ", temp);
  267. temp =
  268. tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_ENABLE);
  269. mb();
  270. printk("irp_enable=0x%08x\n", temp);
  271. }
  272. #endif /* end of DEBUG */
  273. return irq;
  274. }
  275. /*
  276. * Linux descriptor level callbacks
  277. */
  278. static void tsi108_pci_irq_unmask(struct irq_data *d)
  279. {
  280. tsi108_pci_int_unmask(d->irq);
  281. /* Enable interrupts from PCI block */
  282. tsi108_write_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_ENABLE,
  283. tsi108_read_reg(TSI108_PCI_OFFSET +
  284. TSI108_PCI_IRP_ENABLE) |
  285. TSI108_PCI_IRP_ENABLE_P_INT);
  286. mb();
  287. }
  288. static void tsi108_pci_irq_mask(struct irq_data *d)
  289. {
  290. tsi108_pci_int_mask(d->irq);
  291. }
  292. static void tsi108_pci_irq_ack(struct irq_data *d)
  293. {
  294. tsi108_pci_int_mask(d->irq);
  295. }
  296. /*
  297. * Interrupt controller descriptor for cascaded PCI interrupt controller.
  298. */
  299. static struct irq_chip tsi108_pci_irq = {
  300. .name = "tsi108_PCI_int",
  301. .irq_mask = tsi108_pci_irq_mask,
  302. .irq_ack = tsi108_pci_irq_ack,
  303. .irq_unmask = tsi108_pci_irq_unmask,
  304. };
  305. static int pci_irq_host_xlate(struct irq_domain *h, struct device_node *ct,
  306. const u32 *intspec, unsigned int intsize,
  307. irq_hw_number_t *out_hwirq, unsigned int *out_flags)
  308. {
  309. *out_hwirq = intspec[0];
  310. *out_flags = IRQ_TYPE_LEVEL_HIGH;
  311. return 0;
  312. }
  313. static int pci_irq_host_map(struct irq_domain *h, unsigned int virq,
  314. irq_hw_number_t hw)
  315. { unsigned int irq;
  316. DBG("%s(%d, 0x%lx)\n", __func__, virq, hw);
  317. if ((virq >= 1) && (virq <= 4)){
  318. irq = virq + IRQ_PCI_INTAD_BASE - 1;
  319. irq_set_status_flags(irq, IRQ_LEVEL);
  320. irq_set_chip(irq, &tsi108_pci_irq);
  321. }
  322. return 0;
  323. }
  324. static const struct irq_domain_ops pci_irq_domain_ops = {
  325. .map = pci_irq_host_map,
  326. .xlate = pci_irq_host_xlate,
  327. };
  328. /*
  329. * Exported functions
  330. */
  331. /*
  332. * The Tsi108 PCI interrupts initialization routine.
  333. *
  334. * The INTA# - INTD# interrupts on the PCI bus are reported by the PCI block
  335. * to the MPIC using single interrupt source (IRQ_TSI108_PCI). Therefore the
  336. * PCI block has to be treated as a cascaded interrupt controller connected
  337. * to the MPIC.
  338. */
  339. void __init tsi108_pci_int_init(struct device_node *node)
  340. {
  341. DBG("Tsi108_pci_int_init: initializing PCI interrupts\n");
  342. pci_irq_host = irq_domain_add_legacy(node, NR_IRQS_LEGACY, 0, 0,
  343. &pci_irq_domain_ops, NULL);
  344. if (pci_irq_host == NULL) {
  345. printk(KERN_ERR "pci_irq_host: failed to allocate irq domain!\n");
  346. return;
  347. }
  348. init_pci_source();
  349. }
  350. void tsi108_irq_cascade(struct irq_desc *desc)
  351. {
  352. struct irq_chip *chip = irq_desc_get_chip(desc);
  353. unsigned int cascade_irq = get_pci_source();
  354. if (cascade_irq)
  355. generic_handle_irq(cascade_irq);
  356. chip->irq_eoi(&desc->irq_data);
  357. }