tsi108_pci.c 10 KB

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