tsi108_pci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. struct pci_controller *hose = bus->sysdata;
  61. if (ppc_md.pci_exclude_device)
  62. if (ppc_md.pci_exclude_device(hose, bus->number, devfunc))
  63. return PCIBIOS_DEVICE_NOT_FOUND;
  64. cfg_addr = (unsigned char *)(tsi_mk_config_addr(bus->number,
  65. devfunc, offset) |
  66. (offset & 0x03));
  67. #ifdef DEBUG
  68. printk("PCI CFG write : ");
  69. printk("%d:0x%x:0x%x ", bus->number, devfunc, offset);
  70. printk("%d ADDR=0x%08x ", len, (uint) cfg_addr);
  71. printk("data = 0x%08x\n", val);
  72. #endif
  73. switch (len) {
  74. case 1:
  75. out_8((u8 *) cfg_addr, val);
  76. break;
  77. case 2:
  78. out_le16((u16 *) cfg_addr, val);
  79. break;
  80. default:
  81. out_le32((u32 *) cfg_addr, val);
  82. break;
  83. }
  84. return PCIBIOS_SUCCESSFUL;
  85. }
  86. void tsi108_clear_pci_error(u32 pci_cfg_base)
  87. {
  88. u32 err_stat, err_addr, pci_stat;
  89. /*
  90. * Quietly clear PB and PCI error flags set as result
  91. * of PCI/X configuration read requests.
  92. */
  93. /* Read PB Error Log Registers */
  94. err_stat = tsi108_read_reg(TSI108_PB_OFFSET + TSI108_PB_ERRCS);
  95. err_addr = tsi108_read_reg(TSI108_PB_OFFSET + TSI108_PB_AERR);
  96. if (err_stat & TSI108_PB_ERRCS_ES) {
  97. /* Clear error flag */
  98. tsi108_write_reg(TSI108_PB_OFFSET + TSI108_PB_ERRCS,
  99. TSI108_PB_ERRCS_ES);
  100. /* Clear read error reported in PB_ISR */
  101. tsi108_write_reg(TSI108_PB_OFFSET + TSI108_PB_ISR,
  102. TSI108_PB_ISR_PBS_RD_ERR);
  103. /* Clear PCI/X bus cfg errors if applicable */
  104. if ((err_addr & 0xFF000000) == pci_cfg_base) {
  105. pci_stat =
  106. tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_CSR);
  107. tsi108_write_reg(TSI108_PCI_OFFSET + TSI108_PCI_CSR,
  108. pci_stat);
  109. }
  110. }
  111. return;
  112. }
  113. #define __tsi108_read_pci_config(x, addr, op) \
  114. __asm__ __volatile__( \
  115. " "op" %0,0,%1\n" \
  116. "1: eieio\n" \
  117. "2:\n" \
  118. ".section .fixup,\"ax\"\n" \
  119. "3: li %0,-1\n" \
  120. " b 2b\n" \
  121. ".section __ex_table,\"a\"\n" \
  122. " .align 2\n" \
  123. " .long 1b,3b\n" \
  124. ".text" \
  125. : "=r"(x) : "r"(addr))
  126. int
  127. tsi108_direct_read_config(struct pci_bus *bus, unsigned int devfn, int offset,
  128. int len, u32 * val)
  129. {
  130. volatile unsigned char *cfg_addr;
  131. struct pci_controller *hose = bus->sysdata;
  132. u32 temp;
  133. if (ppc_md.pci_exclude_device)
  134. if (ppc_md.pci_exclude_device(hose, bus->number, devfn))
  135. return PCIBIOS_DEVICE_NOT_FOUND;
  136. cfg_addr = (unsigned char *)(tsi_mk_config_addr(bus->number,
  137. devfn,
  138. offset) | (offset &
  139. 0x03));
  140. switch (len) {
  141. case 1:
  142. __tsi108_read_pci_config(temp, cfg_addr, "lbzx");
  143. break;
  144. case 2:
  145. __tsi108_read_pci_config(temp, cfg_addr, "lhbrx");
  146. break;
  147. default:
  148. __tsi108_read_pci_config(temp, cfg_addr, "lwbrx");
  149. break;
  150. }
  151. *val = temp;
  152. #ifdef DEBUG
  153. if ((0xFFFFFFFF != temp) && (0xFFFF != temp) && (0xFF != temp)) {
  154. printk("PCI CFG read : ");
  155. printk("%d:0x%x:0x%x ", bus->number, devfn, offset);
  156. printk("%d ADDR=0x%08x ", len, (uint) cfg_addr);
  157. printk("data = 0x%x\n", *val);
  158. }
  159. #endif
  160. return PCIBIOS_SUCCESSFUL;
  161. }
  162. void tsi108_clear_pci_cfg_error(void)
  163. {
  164. tsi108_clear_pci_error(tsi108_pci_cfg_phys);
  165. }
  166. static struct pci_ops tsi108_direct_pci_ops = {
  167. tsi108_direct_read_config,
  168. tsi108_direct_write_config
  169. };
  170. int __init tsi108_setup_pci(struct device_node *dev, u32 cfg_phys, int primary)
  171. {
  172. int len;
  173. struct pci_controller *hose;
  174. struct resource rsrc;
  175. const int *bus_range;
  176. int has_address = 0;
  177. /* PCI Config mapping */
  178. tsi108_pci_cfg_base = (u32)ioremap(cfg_phys, TSI108_PCI_CFG_SIZE);
  179. tsi108_pci_cfg_phys = cfg_phys;
  180. DBG("TSI_PCI: %s tsi108_pci_cfg_base=0x%x\n", __FUNCTION__,
  181. tsi108_pci_cfg_base);
  182. /* Fetch host bridge registers address */
  183. has_address = (of_address_to_resource(dev, 0, &rsrc) == 0);
  184. /* Get bus range if any */
  185. bus_range = of_get_property(dev, "bus-range", &len);
  186. if (bus_range == NULL || len < 2 * sizeof(int)) {
  187. printk(KERN_WARNING "Can't get bus-range for %s, assume"
  188. " bus 0\n", dev->full_name);
  189. }
  190. hose = pcibios_alloc_controller();
  191. if (!hose) {
  192. printk("PCI Host bridge init failed\n");
  193. return -ENOMEM;
  194. }
  195. hose->arch_data = dev;
  196. hose->first_busno = bus_range ? bus_range[0] : 0;
  197. hose->last_busno = bus_range ? bus_range[1] : 0xff;
  198. (hose)->ops = &tsi108_direct_pci_ops;
  199. printk(KERN_INFO "Found tsi108 PCI host bridge at 0x%08x. "
  200. "Firmware bus number: %d->%d\n",
  201. rsrc.start, hose->first_busno, hose->last_busno);
  202. /* Interpret the "ranges" property */
  203. /* This also maps the I/O region and sets isa_io/mem_base */
  204. pci_process_bridge_OF_ranges(hose, dev, primary);
  205. return 0;
  206. }
  207. /*
  208. * Low level utility functions
  209. */
  210. static void tsi108_pci_int_mask(u_int irq)
  211. {
  212. u_int irp_cfg;
  213. int int_line = (irq - IRQ_PCI_INTAD_BASE);
  214. irp_cfg = tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_CFG_CTL);
  215. mb();
  216. irp_cfg |= (1 << int_line); /* INTx_DIR = output */
  217. irp_cfg &= ~(3 << (8 + (int_line * 2))); /* INTx_TYPE = unused */
  218. tsi108_write_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_CFG_CTL, irp_cfg);
  219. mb();
  220. irp_cfg = tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_CFG_CTL);
  221. }
  222. static void tsi108_pci_int_unmask(u_int irq)
  223. {
  224. u_int irp_cfg;
  225. int int_line = (irq - IRQ_PCI_INTAD_BASE);
  226. irp_cfg = tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_CFG_CTL);
  227. mb();
  228. irp_cfg &= ~(1 << int_line);
  229. irp_cfg |= (3 << (8 + (int_line * 2)));
  230. tsi108_write_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_CFG_CTL, irp_cfg);
  231. mb();
  232. }
  233. static void init_pci_source(void)
  234. {
  235. tsi108_write_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_CFG_CTL,
  236. 0x0000ff00);
  237. tsi108_write_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_ENABLE,
  238. TSI108_PCI_IRP_ENABLE_P_INT);
  239. mb();
  240. }
  241. static inline unsigned int get_pci_source(void)
  242. {
  243. u_int temp = 0;
  244. int irq = -1;
  245. int i;
  246. u_int pci_irp_stat;
  247. static int mask = 0;
  248. /* Read PCI/X block interrupt status register */
  249. pci_irp_stat = tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_STAT);
  250. mb();
  251. if (pci_irp_stat & TSI108_PCI_IRP_STAT_P_INT) {
  252. /* Process Interrupt from PCI bus INTA# - INTD# lines */
  253. temp =
  254. tsi108_read_reg(TSI108_PCI_OFFSET +
  255. TSI108_PCI_IRP_INTAD) & 0xf;
  256. mb();
  257. for (i = 0; i < 4; i++, mask++) {
  258. if (temp & (1 << mask % 4)) {
  259. irq = IRQ_PCI_INTA + mask % 4;
  260. mask++;
  261. break;
  262. }
  263. }
  264. /* Disable interrupts from PCI block */
  265. temp = tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_ENABLE);
  266. tsi108_write_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_ENABLE,
  267. temp & ~TSI108_PCI_IRP_ENABLE_P_INT);
  268. mb();
  269. (void)tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_ENABLE);
  270. mb();
  271. }
  272. #ifdef DEBUG
  273. else {
  274. printk("TSI108_PIC: error in TSI108_PCI_IRP_STAT\n");
  275. pci_irp_stat =
  276. tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_STAT);
  277. temp =
  278. tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_INTAD);
  279. mb();
  280. printk(">> stat=0x%08x intad=0x%08x ", pci_irp_stat, temp);
  281. temp =
  282. tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_CFG_CTL);
  283. mb();
  284. printk("cfg_ctl=0x%08x ", temp);
  285. temp =
  286. tsi108_read_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_ENABLE);
  287. mb();
  288. printk("irp_enable=0x%08x\n", temp);
  289. }
  290. #endif /* end of DEBUG */
  291. return irq;
  292. }
  293. /*
  294. * Linux descriptor level callbacks
  295. */
  296. static void tsi108_pci_irq_enable(u_int irq)
  297. {
  298. tsi108_pci_int_unmask(irq);
  299. }
  300. static void tsi108_pci_irq_disable(u_int irq)
  301. {
  302. tsi108_pci_int_mask(irq);
  303. }
  304. static void tsi108_pci_irq_ack(u_int irq)
  305. {
  306. tsi108_pci_int_mask(irq);
  307. }
  308. static void tsi108_pci_irq_end(u_int irq)
  309. {
  310. tsi108_pci_int_unmask(irq);
  311. /* Enable interrupts from PCI block */
  312. tsi108_write_reg(TSI108_PCI_OFFSET + TSI108_PCI_IRP_ENABLE,
  313. tsi108_read_reg(TSI108_PCI_OFFSET +
  314. TSI108_PCI_IRP_ENABLE) |
  315. TSI108_PCI_IRP_ENABLE_P_INT);
  316. mb();
  317. }
  318. /*
  319. * Interrupt controller descriptor for cascaded PCI interrupt controller.
  320. */
  321. static struct irq_chip tsi108_pci_irq = {
  322. .typename = "tsi108_PCI_int",
  323. .mask = tsi108_pci_irq_disable,
  324. .ack = tsi108_pci_irq_ack,
  325. .end = tsi108_pci_irq_end,
  326. .unmask = tsi108_pci_irq_enable,
  327. };
  328. static int pci_irq_host_xlate(struct irq_host *h, struct device_node *ct,
  329. u32 *intspec, unsigned int intsize,
  330. irq_hw_number_t *out_hwirq, unsigned int *out_flags)
  331. {
  332. *out_hwirq = intspec[0];
  333. *out_flags = IRQ_TYPE_LEVEL_HIGH;
  334. return 0;
  335. }
  336. static int pci_irq_host_map(struct irq_host *h, unsigned int virq,
  337. irq_hw_number_t hw)
  338. { unsigned int irq;
  339. DBG("%s(%d, 0x%lx)\n", __FUNCTION__, virq, hw);
  340. if ((virq >= 1) && (virq <= 4)){
  341. irq = virq + IRQ_PCI_INTAD_BASE - 1;
  342. get_irq_desc(irq)->status |= IRQ_LEVEL;
  343. set_irq_chip(irq, &tsi108_pci_irq);
  344. }
  345. return 0;
  346. }
  347. static int pci_irq_host_match(struct irq_host *h, struct device_node *node)
  348. {
  349. return pci_irq_node == node;
  350. }
  351. static struct irq_host_ops pci_irq_host_ops = {
  352. .match = pci_irq_host_match,
  353. .map = pci_irq_host_map,
  354. .xlate = pci_irq_host_xlate,
  355. };
  356. /*
  357. * Exported functions
  358. */
  359. /*
  360. * The Tsi108 PCI interrupts initialization routine.
  361. *
  362. * The INTA# - INTD# interrupts on the PCI bus are reported by the PCI block
  363. * to the MPIC using single interrupt source (IRQ_TSI108_PCI). Therefore the
  364. * PCI block has to be treated as a cascaded interrupt controller connected
  365. * to the MPIC.
  366. */
  367. void __init tsi108_pci_int_init(struct device_node *node)
  368. {
  369. DBG("Tsi108_pci_int_init: initializing PCI interrupts\n");
  370. pci_irq_node = of_node_get(node);
  371. pci_irq_host = irq_alloc_host(IRQ_HOST_MAP_LEGACY, 0, &pci_irq_host_ops, 0);
  372. if (pci_irq_host == NULL) {
  373. printk(KERN_ERR "pci_irq_host: failed to allocate irq host !\n");
  374. return;
  375. }
  376. init_pci_source();
  377. }
  378. void tsi108_irq_cascade(unsigned int irq, struct irq_desc *desc)
  379. {
  380. unsigned int cascade_irq = get_pci_source();
  381. if (cascade_irq != NO_IRQ)
  382. generic_handle_irq(cascade_irq);
  383. desc->chip->eoi(irq);
  384. }