tsi108_pci.c 11 KB

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