tsi108_pci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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/slab.h>
  27. #include <linux/irq.h>
  28. #include <linux/interrupt.h>
  29. #include <asm/byteorder.h>
  30. #include <asm/io.h>
  31. #include <asm/irq.h>
  32. #include <asm/uaccess.h>
  33. #include <asm/machdep.h>
  34. #include <asm/pci-bridge.h>
  35. #include <asm/tsi108.h>
  36. #include <asm/tsi108_pci.h>
  37. #include <asm/tsi108_irq.h>
  38. #include <asm/prom.h>
  39. #undef DEBUG
  40. #ifdef DEBUG
  41. #define DBG(x...) printk(x)
  42. #else
  43. #define DBG(x...)
  44. #endif
  45. #define tsi_mk_config_addr(bus, devfunc, offset) \
  46. ((((bus)<<16) | ((devfunc)<<8) | (offset & 0xfc)) + tsi108_pci_cfg_base)
  47. u32 tsi108_pci_cfg_base;
  48. static u32 tsi108_pci_cfg_phys;
  49. u32 tsi108_csr_vir_base;
  50. static struct device_node *pci_irq_node;
  51. static struct irq_host *pci_irq_host;
  52. extern u32 get_vir_csrbase(void);
  53. extern u32 tsi108_read_reg(u32 reg_offset);
  54. extern void tsi108_write_reg(u32 reg_offset, u32 val);
  55. int
  56. tsi108_direct_write_config(struct pci_bus *bus, unsigned int devfunc,
  57. int offset, int len, u32 val)
  58. {
  59. volatile unsigned char *cfg_addr;
  60. if (ppc_md.pci_exclude_device)
  61. if (ppc_md.pci_exclude_device(bus->number, devfunc))
  62. return PCIBIOS_DEVICE_NOT_FOUND;
  63. cfg_addr = (unsigned char *)(tsi_mk_config_addr(bus->number,
  64. devfunc, offset) |
  65. (offset & 0x03));
  66. #ifdef DEBUG
  67. printk("PCI CFG write : ");
  68. printk("%d:0x%x:0x%x ", bus->number, devfunc, offset);
  69. printk("%d ADDR=0x%08x ", len, (uint) cfg_addr);
  70. printk("data = 0x%08x\n", val);
  71. #endif
  72. switch (len) {
  73. case 1:
  74. out_8((u8 *) cfg_addr, val);
  75. break;
  76. case 2:
  77. out_le16((u16 *) cfg_addr, val);
  78. break;
  79. default:
  80. out_le32((u32 *) cfg_addr, val);
  81. break;
  82. }
  83. return PCIBIOS_SUCCESSFUL;
  84. }
  85. void tsi108_clear_pci_error(u32 pci_cfg_base)
  86. {
  87. u32 err_stat, err_addr, pci_stat;
  88. /*
  89. * Quietly clear PB and PCI error flags set as result
  90. * of PCI/X configuration read requests.
  91. */
  92. /* Read PB Error Log Registers */
  93. err_stat = tsi108_read_reg(TSI108_PB_OFFSET + TSI108_PB_ERRCS);
  94. err_addr = tsi108_read_reg(TSI108_PB_OFFSET + TSI108_PB_AERR);
  95. if (err_stat & TSI108_PB_ERRCS_ES) {
  96. /* Clear error flag */
  97. tsi108_write_reg(TSI108_PB_OFFSET + TSI108_PB_ERRCS,
  98. TSI108_PB_ERRCS_ES);
  99. /* Clear read error reported in PB_ISR */
  100. tsi108_write_reg(TSI108_PB_OFFSET + TSI108_PB_ISR,
  101. TSI108_PB_ISR_PBS_RD_ERR);
  102. /* Clear PCI/X bus cfg errors if applicable */
  103. if ((err_addr & 0xFF000000) == pci_cfg_base) {
  104. pci_stat =
  105. tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_CSR);
  106. tsi108_write_reg(TSI108_PCI_OFFSET + TSI108_PCI_CSR,
  107. pci_stat);
  108. }
  109. }
  110. return;
  111. }
  112. #define __tsi108_read_pci_config(x, addr, op) \
  113. __asm__ __volatile__( \
  114. " "op" %0,0,%1\n" \
  115. "1: eieio\n" \
  116. "2:\n" \
  117. ".section .fixup,\"ax\"\n" \
  118. "3: li %0,-1\n" \
  119. " b 2b\n" \
  120. ".section __ex_table,\"a\"\n" \
  121. " .align 2\n" \
  122. " .long 1b,3b\n" \
  123. ".text" \
  124. : "=r"(x) : "r"(addr))
  125. int
  126. tsi108_direct_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
  127. int len, u32 * val)
  128. {
  129. volatile unsigned char *cfg_addr;
  130. u32 temp;
  131. if (ppc_md.pci_exclude_device)
  132. if (ppc_md.pci_exclude_device(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. tsi108_direct_read_config,
  166. 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", __FUNCTION__,
  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();
  189. if (!hose) {
  190. printk("PCI Host bridge init failed\n");
  191. return -ENOMEM;
  192. }
  193. hose->arch_data = dev;
  194. hose->set_cfg_type = 1;
  195. hose->first_busno = bus_range ? bus_range[0] : 0;
  196. hose->last_busno = bus_range ? bus_range[1] : 0xff;
  197. (hose)->ops = &tsi108_direct_pci_ops;
  198. printk(KERN_INFO "Found tsi108 PCI host bridge at 0x%08x. "
  199. "Firmware bus number: %d->%d\n",
  200. rsrc.start, hose->first_busno, hose->last_busno);
  201. /* Interpret the "ranges" property */
  202. /* This also maps the I/O region and sets isa_io/mem_base */
  203. pci_process_bridge_OF_ranges(hose, dev, primary);
  204. return 0;
  205. }
  206. /*
  207. * Low level utility functions
  208. */
  209. static void tsi108_pci_int_mask(u_int irq)
  210. {
  211. u_int irp_cfg;
  212. int int_line = (irq - IRQ_PCI_INTAD_BASE);
  213. irp_cfg = tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_CFG_CTL);
  214. mb();
  215. irp_cfg |= (1 << int_line); /* INTx_DIR = output */
  216. irp_cfg &= ~(3 << (8 + (int_line * 2))); /* INTx_TYPE = unused */
  217. tsi108_write_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_CFG_CTL, irp_cfg);
  218. mb();
  219. irp_cfg = tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_CFG_CTL);
  220. }
  221. static void tsi108_pci_int_unmask(u_int irq)
  222. {
  223. u_int irp_cfg;
  224. int int_line = (irq - IRQ_PCI_INTAD_BASE);
  225. irp_cfg = tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_CFG_CTL);
  226. mb();
  227. irp_cfg &= ~(1 << int_line);
  228. irp_cfg |= (3 << (8 + (int_line * 2)));
  229. tsi108_write_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_CFG_CTL, irp_cfg);
  230. mb();
  231. }
  232. static void init_pci_source(void)
  233. {
  234. tsi108_write_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_CFG_CTL,
  235. 0x0000ff00);
  236. tsi108_write_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_ENABLE,
  237. TSI108_PCI_IRP_ENABLE_P_INT);
  238. mb();
  239. }
  240. static inline unsigned int get_pci_source(void)
  241. {
  242. u_int temp = 0;
  243. int irq = -1;
  244. int i;
  245. u_int pci_irp_stat;
  246. static int mask = 0;
  247. /* Read PCI/X block interrupt status register */
  248. pci_irp_stat = tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_STAT);
  249. mb();
  250. if (pci_irp_stat & TSI108_PCI_IRP_STAT_P_INT) {
  251. /* Process Interrupt from PCI bus INTA# - INTD# lines */
  252. temp =
  253. tsi108_read_reg(TSI108_PCI_OFFSET +
  254. TSI108_PCI_IRP_INTAD) & 0xf;
  255. mb();
  256. for (i = 0; i < 4; i++, mask++) {
  257. if (temp & (1 << mask % 4)) {
  258. irq = IRQ_PCI_INTA + mask % 4;
  259. mask++;
  260. break;
  261. }
  262. }
  263. /* Disable interrupts from PCI block */
  264. temp = tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_ENABLE);
  265. tsi108_write_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_ENABLE,
  266. temp & ~TSI108_PCI_IRP_ENABLE_P_INT);
  267. mb();
  268. (void)tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_ENABLE);
  269. mb();
  270. }
  271. #ifdef DEBUG
  272. else {
  273. printk("TSI108_PIC: error in TSI108_PCI_IRP_STAT\n");
  274. pci_irp_stat =
  275. tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_STAT);
  276. temp =
  277. tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_INTAD);
  278. mb();
  279. printk(">> stat=0x%08x intad=0x%08x ", pci_irp_stat, temp);
  280. temp =
  281. tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_CFG_CTL);
  282. mb();
  283. printk("cfg_ctl=0x%08x ", temp);
  284. temp =
  285. tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_ENABLE);
  286. mb();
  287. printk("irp_enable=0x%08x\n", temp);
  288. }
  289. #endif /* end of DEBUG */
  290. return irq;
  291. }
  292. /*
  293. * Linux descriptor level callbacks
  294. */
  295. static void tsi108_pci_irq_enable(u_int irq)
  296. {
  297. tsi108_pci_int_unmask(irq);
  298. }
  299. static void tsi108_pci_irq_disable(u_int irq)
  300. {
  301. tsi108_pci_int_mask(irq);
  302. }
  303. static void tsi108_pci_irq_ack(u_int irq)
  304. {
  305. tsi108_pci_int_mask(irq);
  306. }
  307. static void tsi108_pci_irq_end(u_int irq)
  308. {
  309. tsi108_pci_int_unmask(irq);
  310. /* Enable interrupts from PCI block */
  311. tsi108_write_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_ENABLE,
  312. tsi108_read_reg(TSI108_PCI_OFFSET +
  313. TSI108_PCI_IRP_ENABLE) |
  314. TSI108_PCI_IRP_ENABLE_P_INT);
  315. mb();
  316. }
  317. /*
  318. * Interrupt controller descriptor for cascaded PCI interrupt controller.
  319. */
  320. static struct irq_chip tsi108_pci_irq = {
  321. .typename = "tsi108_PCI_int",
  322. .mask = tsi108_pci_irq_disable,
  323. .ack = tsi108_pci_irq_ack,
  324. .end = tsi108_pci_irq_end,
  325. .unmask = tsi108_pci_irq_enable,
  326. };
  327. static int pci_irq_host_xlate(struct irq_host *h, struct device_node *ct,
  328. u32 *intspec, unsigned int intsize,
  329. irq_hw_number_t *out_hwirq, unsigned int *out_flags)
  330. {
  331. *out_hwirq = intspec[0];
  332. *out_flags = IRQ_TYPE_LEVEL_HIGH;
  333. return 0;
  334. }
  335. static int pci_irq_host_map(struct irq_host *h, unsigned int virq,
  336. irq_hw_number_t hw)
  337. { unsigned int irq;
  338. DBG("%s(%d, 0x%lx)\n", __FUNCTION__, virq, hw);
  339. if ((virq >= 1) && (virq <= 4)){
  340. irq = virq + IRQ_PCI_INTAD_BASE - 1;
  341. get_irq_desc(irq)->status |= IRQ_LEVEL;
  342. set_irq_chip(irq, &tsi108_pci_irq);
  343. }
  344. return 0;
  345. }
  346. static int pci_irq_host_match(struct irq_host *h, struct device_node *node)
  347. {
  348. return pci_irq_node == node;
  349. }
  350. static struct irq_host_ops pci_irq_host_ops = {
  351. .match = pci_irq_host_match,
  352. .map = pci_irq_host_map,
  353. .xlate = pci_irq_host_xlate,
  354. };
  355. /*
  356. * Exported functions
  357. */
  358. /*
  359. * The Tsi108 PCI interrupts initialization routine.
  360. *
  361. * The INTA# - INTD# interrupts on the PCI bus are reported by the PCI block
  362. * to the MPIC using single interrupt source (IRQ_TSI108_PCI). Therefore the
  363. * PCI block has to be treated as a cascaded interrupt controller connected
  364. * to the MPIC.
  365. */
  366. void __init tsi108_pci_int_init(struct device_node *node)
  367. {
  368. DBG("Tsi108_pci_int_init: initializing PCI interrupts\n");
  369. pci_irq_node = of_node_get(node);
  370. pci_irq_host = irq_alloc_host(IRQ_HOST_MAP_LEGACY, 0, &pci_irq_host_ops, 0);
  371. if (pci_irq_host == NULL) {
  372. printk(KERN_ERR "pci_irq_host: failed to allocate irq host !\n");
  373. return;
  374. }
  375. init_pci_source();
  376. }
  377. void tsi108_irq_cascade(unsigned int irq, struct irq_desc *desc)
  378. {
  379. unsigned int cascade_irq = get_pci_source();
  380. if (cascade_irq != NO_IRQ)
  381. generic_handle_irq(cascade_irq);
  382. desc->chip->eoi(irq);
  383. }